summaryrefslogtreecommitdiffstats
path: root/net/net.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/net.c')
-rw-r--r--net/net.c91
1 files changed, 80 insertions, 11 deletions
diff --git a/net/net.c b/net/net.c
index eae45e1843..c06e88fe53 100644
--- a/net/net.c
+++ b/net/net.c
@@ -25,10 +25,10 @@
#include <init.h>
#include <globalvar.h>
#include <magicvar.h>
+#include <machine_id.h>
#include <linux/ctype.h>
#include <linux/err.h>
-unsigned char *NetRxPackets[PKTBUFSRX]; /* Receive packets */
static unsigned int net_ip_id;
char *net_server;
@@ -325,9 +325,14 @@ void net_set_serverip(IPaddr_t ip)
net_server = xasprintf("%pI4", &ip);
}
+const char *net_get_server(void)
+{
+ return net_server && *net_server ? net_server : NULL;
+}
+
void net_set_serverip_empty(IPaddr_t ip)
{
- if (net_server && *net_server)
+ if (net_get_server())
return;
net_set_serverip(ip);
@@ -360,6 +365,43 @@ IPaddr_t net_get_gateway(void)
static LIST_HEAD(connection_list);
+/**
+ * generate_ether_addr - Generates stable software assigned Ethernet address
+ * @addr: Pointer to a six-byte array to contain the Ethernet address
+ * @ethid: index of the Ethernet interface
+ *
+ * Derives an Ethernet address (MAC) from the machine ID, that's stable
+ * per board that is not multicast and has the local assigned bit set.
+ *
+ * Return 0 if an address could be generated or a negative error code otherwise.
+ */
+int generate_ether_addr(u8 *ethaddr, int ethid)
+{
+ const char *hostname;
+ uuid_t id;
+ int ret;
+
+ if (!IS_ENABLED(CONFIG_NET_ETHADDR_FROM_MACHINE_ID))
+ return -ENOSYS;
+
+ hostname = barebox_get_hostname();
+ if (!hostname)
+ return -EINVAL;
+
+ ret = machine_id_get_app_specific(&id, ARRAY_AND_SIZE("barebox-macaddr:"),
+ hostname, strlen(hostname), NULL);
+ if (ret)
+ return ret;
+
+ memcpy(ethaddr, &id.b, ETH_ALEN);
+ eth_addr_add(ethaddr, ethid);
+
+ ethaddr[0] &= 0xfe; /* clear multicast bit */
+ ethaddr[0] |= 0x02; /* set local assignment bit (IEEE802) */
+
+ return 0;
+}
+
static struct net_connection *net_new(struct eth_device *edev, IPaddr_t dest,
rx_handler_f *handler, void *ctx)
{
@@ -375,10 +417,13 @@ static struct net_connection *net_new(struct eth_device *edev, IPaddr_t dest,
}
if (!is_valid_ether_addr(edev->ethaddr)) {
- char str[sizeof("xx:xx:xx:xx:xx:xx")];
- random_ether_addr(edev->ethaddr);
- ethaddr_to_string(edev->ethaddr, str);
- dev_warn(&edev->dev, "No MAC address set. Using random address %s\n", str);
+ ret = generate_ether_addr(edev->ethaddr, edev->dev.id);
+ if (ret)
+ random_ether_addr(edev->ethaddr);
+
+ dev_warn(&edev->dev, "No MAC address set. Using %s %pM\n",
+ ret == 1 ? "address computed from unique ID" : "random address",
+ edev->ethaddr);
eth_set_ethaddr(edev, edev->ethaddr);
}
@@ -634,7 +679,7 @@ static int ping_reply(struct eth_device *edev, unsigned char *pkt, int len)
free(packet);
- return 0;
+ return ret;
}
static int net_handle_icmp(struct eth_device *edev, unsigned char *pkt, int len)
@@ -673,7 +718,12 @@ static int net_handle_ip(struct eth_device *edev, unsigned char *pkt, int len)
if ((ip->hl_v & 0xf0) != 0x40)
goto bad;
- if (ip->frag_off & htons(0x1fff)) /* Can't deal w/ fragments */
+ /* Can't deal w/ fragments.
+ * Either a fragment offset (13 bits), or
+ * MF (More Fragments) from fragment flags (3 bits).
+ * MF - because first fragment has fragment offset 0
+ */
+ if (ip->frag_off & htons(0x3fff))
goto bad;
if (!net_checksum_ok((unsigned char *)ip, sizeof(struct iphdr)))
goto bad;
@@ -738,13 +788,32 @@ out:
return ret;
}
-static int net_init(void)
+void net_free_packets(void **packets, unsigned count)
{
+ while (count-- > 0)
+ net_free_packet(packets[count]);
+}
+
+int net_alloc_packets(void **packets, int count)
+{
+ void *packet;
int i;
- for (i = 0; i < PKTBUFSRX; i++)
- NetRxPackets[i] = net_alloc_packet();
+ for (i = 0; i < count; i++) {
+ packet = net_alloc_packet();
+ if (!packet)
+ goto free;
+ packets[i] = packet;
+ }
+
+ return 0;
+free:
+ net_free_packets(packets, i);
+ return -ENOMEM;
+}
+static int net_init(void)
+{
globalvar_add_simple_ip("net.nameserver", &net_nameserver);
globalvar_add_simple_string("net.domainname", &net_domainname);
globalvar_add_simple_string("net.server", &net_server);