net: Move the mac address and ethernet header types out of UIP.

These types aren't specific to ipv4 or ipv6 and can be shared by both. The MAC
address type is also used by the network interface drivers, so pulling it out
reduces the width of the interface between the drivers and the network stack.

Change-Id: I54f01705c63c11239898cd456dddf717d9466f54
diff --git a/src/drivers/net/asix.c b/src/drivers/net/asix.c
index 5d54cfa..f2edf8f 100644
--- a/src/drivers/net/asix.c
+++ b/src/drivers/net/asix.c
@@ -33,6 +33,7 @@
 #include "drivers/net/mii.h"
 #include "drivers/net/net.h"
 #include "drivers/net/usb_eth.h"
+#include "net/ethernet.h"
 
 enum {
 	SramRead = 0x2,
@@ -130,7 +131,7 @@
 	UsbEndpoint *bulk_in;
 	UsbEndpoint *bulk_out;
 
-	uip_eth_addr mac_addr;
+	MacAddress mac_addr;
 
 	int phy_id;
 } AsixDev;
@@ -399,12 +400,12 @@
 	return 0;
 }
 
-static const uip_eth_addr *asix_get_mac(NetDevice *net_dev)
+static const MacAddress *asix_get_mac(NetDevice *net_dev)
 {
 	AsixDev *asix = container_of(net_dev, AsixDev, usb_eth_dev.net_dev);
 	UsbDev *usb_dev = asix->usb_eth_dev.gen_dev->dev;
 
-	if (usb_eth_read_reg(usb_dev, NodeIdRead, 0, 0, sizeof(uip_eth_addr),
+	if (usb_eth_read_reg(usb_dev, NodeIdRead, 0, 0, sizeof(MacAddress),
 			&asix_dev.mac_addr)) {
 		printf("ASIX: Failed to retrieve MAC address.\n");
 		return NULL;
diff --git a/src/drivers/net/net.c b/src/drivers/net/net.c
index afcbfa0..9815090 100644
--- a/src/drivers/net/net.c
+++ b/src/drivers/net/net.c
@@ -26,6 +26,7 @@
 
 #include "base/time.h"
 #include "drivers/net/net.h"
+#include "net/ethernet.h"
 #include "net/uip/arp.h"
 #include "net/uip/uip.h"
 
@@ -103,21 +104,21 @@
 		return;
 	}
 
-	struct uip_eth_hdr *hdr = (struct uip_eth_hdr *)uip_buf;
+	EtherHdr *hdr = (EtherHdr *)uip_buf;
 	if (net_device->recv(net_device, uip_buf, &uip_len,
 			     CONFIG_UIP_BUFSIZE)) {
 		printf("Receive failed.\n");
 		return;
 	}
 	if (uip_len) {
-		if (hdr->type == htonw(UIP_ETHTYPE_IP)) {
+		if (hdr->type == htonw(EthType_Ip)) {
 			uip_arp_ipin();
 			uip_input();
 			if (uip_len > 0) {
 				uip_arp_out();
 				net_device->send(net_device, uip_buf, uip_len);
 			}
-		} else if (hdr->type == htonw(UIP_ETHTYPE_ARP)) {
+		} else if (hdr->type == htonw(EthType_Arp)) {
 			uip_arp_arpin();
 			if (uip_len > 0)
 				net_device->send(net_device, uip_buf, uip_len);
@@ -134,7 +135,7 @@
 	return net_device->send(net_device, buf, len);
 }
 
-const uip_eth_addr *net_get_mac(void)
+const MacAddress *net_get_mac(void)
 {
 	if (!net_device) {
 		printf("No network device.\n");
diff --git a/src/drivers/net/net.h b/src/drivers/net/net.h
index 79d536f..610658d 100644
--- a/src/drivers/net/net.h
+++ b/src/drivers/net/net.h
@@ -26,7 +26,7 @@
 #include <stdint.h>
 
 #include "base/list.h"
-#include "net/uip/uip.h"
+#include "net/ethernet.h"
 
 typedef struct NetDevice {
 	ListNode list_node;
@@ -34,7 +34,7 @@
 	int (*recv)(struct NetDevice *dev, void *buf, uint16_t *len,
 		int maxlen);
 	int (*send)(struct NetDevice *dev, void *buf, uint16_t len);
-	const uip_eth_addr *(*get_mac)(struct NetDevice *dev);
+	const MacAddress *(*get_mac)(struct NetDevice *dev);
 } NetDevice;
 
 typedef struct NetPoller {
@@ -47,7 +47,7 @@
 void net_poll(void);
 int net_send(void *buf, uint16_t len);
 void net_wait_for_link(void);
-const uip_eth_addr *net_get_mac(void);
+const MacAddress *net_get_mac(void);
 
 extern ListNode net_pollers;
 
diff --git a/src/drivers/net/smsc95xx.c b/src/drivers/net/smsc95xx.c
index 021ac72..4ab06cf 100644
--- a/src/drivers/net/smsc95xx.c
+++ b/src/drivers/net/smsc95xx.c
@@ -30,6 +30,7 @@
 #include "drivers/net/mii.h"
 #include "drivers/net/net.h"
 #include "drivers/net/usb_eth.h"
+#include "net/ethernet.h"
 
 static const int Smsc95xxPhyId = 1;
 static const int EthP8021Q = 0x8100;
@@ -141,7 +142,7 @@
 	UsbEndpoint *bulk_in;
 	UsbEndpoint *bulk_out;
 
-	uip_eth_addr mac_addr;
+	MacAddress mac_addr;
 } Smsc95xxDev;
 
 static Smsc95xxDev smsc_dev;
@@ -488,7 +489,7 @@
 	return 0;
 }
 
-static const uip_eth_addr *smsc95xx_get_mac(NetDevice *net_dev)
+static const MacAddress *smsc95xx_get_mac(NetDevice *net_dev)
 {
 	UsbEthDevice *eth = container_of(net_dev, UsbEthDevice, net_dev);
 	UsbDev *usb_dev = eth->gen_dev->dev;
@@ -497,17 +498,17 @@
 	uint32_t addrl;
 
 	if (smsc95xx_read_eeprom(usb_dev, EepromMacOffset,
-				 sizeof(uip_eth_addr),
+				 sizeof(MacAddress),
 				 (uint8_t *)&smsc_dev.mac_addr)) {
 		printf("SMSC95xx: Failed to retrieve MAC address.\n");
 		return NULL;
 	} else {
-		addrh = (smsc_dev.mac_addr.addr[5] << 8) |
-			(smsc_dev.mac_addr.addr[4]);
-		addrl = (smsc_dev.mac_addr.addr[3] << 24) |
-			(smsc_dev.mac_addr.addr[2] << 16) |
-			(smsc_dev.mac_addr.addr[1] << 8) |
-			(smsc_dev.mac_addr.addr[0]);
+		addrh = (smsc_dev.mac_addr.octet[5] << 8) |
+			(smsc_dev.mac_addr.octet[4]);
+		addrl = (smsc_dev.mac_addr.octet[3] << 24) |
+			(smsc_dev.mac_addr.octet[2] << 16) |
+			(smsc_dev.mac_addr.octet[1] << 8) |
+			(smsc_dev.mac_addr.octet[0]);
 		if (smsc95xx_write_reg(usb_dev, AddrHReg, addrh) ||
 		    smsc95xx_write_reg(usb_dev, AddrLReg, addrl)) {
 			printf("SMSC95xx: Failed to write MAC address.\n");
diff --git a/src/drivers/net/uefi.c b/src/drivers/net/uefi.c
index 3dce04c..358a76e 100644
--- a/src/drivers/net/uefi.c
+++ b/src/drivers/net/uefi.c
@@ -31,6 +31,7 @@
 #include "base/list.h"
 #include "base/xalloc.h"
 #include "drivers/net/net.h"
+#include "net/ethernet.h"
 
 #include "uefi/edk/Protocol/SimpleNetwork.h"
 #include "uefi/uefi.h"
@@ -45,7 +46,7 @@
 	int initialized;
 
 	EFI_SIMPLE_NETWORK_PROTOCOL *snp;
-	uip_eth_addr mac;
+	MacAddress mac;
 } UefiNetDevice;
 
 ListNode uefinet_devices;
@@ -183,7 +184,7 @@
 	return 0;
 }
 
-static const uip_eth_addr *uefinet_get_mac(NetDevice *net_dev)
+static const MacAddress *uefinet_get_mac(NetDevice *net_dev)
 {
 	UefiNetDevice *dev = container_of(net_dev, UefiNetDevice, dev);
 
diff --git a/src/net/ethernet.h b/src/net/ethernet.h
new file mode 100644
index 0000000..90cc26c
--- /dev/null
+++ b/src/net/ethernet.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but without any warranty; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#ifndef __NET_ETHERNET_H__
+#define __NET_ETHERNET_H__
+
+#include <stdint.h>
+
+typedef struct {
+	uint8_t octet[6];
+} MacAddress;
+
+typedef struct __attribute__((packed)) {
+	MacAddress dest;
+	MacAddress src;
+	uint16_t type;
+} EtherHdr;
+
+enum {
+	EthType_Arp = 0x0806,
+	EthType_Ip = 0x0800,
+};
+
+#endif /* __NET_ETHERNET_H__ */
diff --git a/src/net/netboot/netboot.c b/src/net/netboot/netboot.c
index ad97ee1..897806c 100644
--- a/src/net/netboot/netboot.c
+++ b/src/net/netboot/netboot.c
@@ -27,6 +27,7 @@
 #include "base/algorithm.h"
 #include "boot/boot.h"
 #include "drivers/net/net.h"
+#include "net/ethernet.h"
 #include "net/netboot/dhcp.h"
 #include "net/netboot/netboot.h"
 #include "net/netboot/params.h"
@@ -40,10 +41,10 @@
 		uip_ipaddr3(ip), uip_ipaddr4(ip));
 }
 
-static void print_mac_addr(const uip_eth_addr *mac)
+static void print_mac_addr(const MacAddress *mac)
 {
-	for (int i = 0; i < ARRAY_SIZE(mac->addr); i++)
-		printf("%s%02x", i ? ":" : "", mac->addr[i]);
+	for (int i = 0; i < ARRAY_SIZE(mac->octet); i++)
+		printf("%s%02x", i ? ":" : "", mac->octet[i]);
 }
 
 static void * const payload = (void *)(uintptr_t)CONFIG_KERNEL_START;
@@ -59,7 +60,7 @@
 	uip_init();
 
 	// Plug in the MAC address.
-	const uip_eth_addr *mac_addr = net_get_mac();
+	const MacAddress *mac_addr = net_get_mac();
 	if (!mac_addr)
 		halt();
 	printf("MAC: ");
diff --git a/src/net/uip/arp.c b/src/net/uip/arp.c
index 4070058..dab8b0f 100644
--- a/src/net/uip/arp.c
+++ b/src/net/uip/arp.c
@@ -59,25 +59,26 @@
  */
 
 
+#include "net/ethernet.h"
 #include "net/uip/arp.h"
 
 #include <string.h>
 
 struct arp_hdr {
-  struct uip_eth_hdr ethhdr;
+  EtherHdr ethhdr;
   uint16_t hwtype;
   uint16_t protocol;
   uint8_t hwlen;
   uint8_t protolen;
   uint16_t opcode;
-  struct uip_eth_addr shwaddr;
+  MacAddress shwaddr;
   uip_ipaddr_t sipaddr;
-  struct uip_eth_addr dhwaddr;
+  MacAddress dhwaddr;
   uip_ipaddr_t dipaddr;
 };
 
 struct ethip_hdr {
-  struct uip_eth_hdr ethhdr;
+  EtherHdr ethhdr;
   /* IP header. */
   uint8_t vhl,
     tos,
@@ -97,11 +98,11 @@
 
 struct arp_entry {
   uip_ipaddr_t ipaddr;
-  struct uip_eth_addr ethaddr;
+  MacAddress ethaddr;
   uint8_t time;
 };
 
-static const struct uip_eth_addr broadcast_ethaddr =
+static const MacAddress broadcast_ethaddr =
   {{0xff,0xff,0xff,0xff,0xff,0xff}};
 static const uint16_t broadcast_ipaddr[2] = {0xffff,0xffff};
 
@@ -164,7 +165,7 @@
 
 /*-----------------------------------------------------------------------------------*/
 static void
-uip_arp_update(uip_ipaddr_t *ipaddr, struct uip_eth_addr *ethaddr)
+uip_arp_update(uip_ipaddr_t *ipaddr, MacAddress *ethaddr)
 {
   register struct arp_entry *tabptr = arp_table;
 
@@ -182,7 +183,7 @@
       if(uip_ipaddr_cmp(ipaddr, &tabptr->ipaddr)) {
 	 
 	/* An old entry found, update this and return. */
-	memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
+	memcpy(tabptr->ethaddr.octet, ethaddr->octet, 6);
 	tabptr->time = arptime;
 
 	return;
@@ -221,7 +222,7 @@
   /* Now, i is the ARP table entry which we will fill with the new
      information. */
   uip_ipaddr_copy(&tabptr->ipaddr, ipaddr);
-  memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
+  memcpy(tabptr->ethaddr.octet, ethaddr->octet, 6);
   tabptr->time = arptime;
 }
 /*-----------------------------------------------------------------------------------*/
@@ -242,7 +243,7 @@
 void
 uip_arp_ipin(void)
 {
-  uip_len -= sizeof(struct uip_eth_hdr);
+  uip_len -= sizeof(EtherHdr);
 	
   /* Only insert/update an entry if the source IP address of the
      incoming IP packet comes from a host on the local network. */
@@ -311,15 +312,15 @@
       
       BUF->opcode = UIP_HTONS(ARP_REPLY);
 
-      memcpy(BUF->dhwaddr.addr, BUF->shwaddr.addr, 6);
-      memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6);
-      memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
-      memcpy(BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6);
+      memcpy(BUF->dhwaddr.octet, BUF->shwaddr.octet, 6);
+      memcpy(BUF->shwaddr.octet, uip_ethaddr.octet, 6);
+      memcpy(BUF->ethhdr.src.octet, uip_ethaddr.octet, 6);
+      memcpy(BUF->ethhdr.dest.octet, BUF->dhwaddr.octet, 6);
       
       uip_ipaddr_copy(&BUF->dipaddr, &BUF->sipaddr);
       uip_ipaddr_copy(&BUF->sipaddr, &uip_hostaddr);
 
-      BUF->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_ARP);
+      BUF->ethhdr.type = UIP_HTONS(EthType_Arp);
       uip_len = sizeof(struct arp_hdr);
     }
     break;
@@ -376,15 +377,15 @@
 
   /* First check if destination is a local broadcast. */
   if(uip_ipaddr_cmp(&IPBUF->destipaddr, &uip_broadcast_addr)) {
-    memcpy(IPBUF->ethhdr.dest.addr, broadcast_ethaddr.addr, 6);
+    memcpy(IPBUF->ethhdr.dest.octet, broadcast_ethaddr.octet, 6);
   } else if(IPBUF->destipaddr.u8[0] == 224) {
     /* Multicast. */
-    IPBUF->ethhdr.dest.addr[0] = 0x01;
-    IPBUF->ethhdr.dest.addr[1] = 0x00;
-    IPBUF->ethhdr.dest.addr[2] = 0x5e;
-    IPBUF->ethhdr.dest.addr[3] = IPBUF->destipaddr.u8[1];
-    IPBUF->ethhdr.dest.addr[4] = IPBUF->destipaddr.u8[2];
-    IPBUF->ethhdr.dest.addr[5] = IPBUF->destipaddr.u8[3];
+    IPBUF->ethhdr.dest.octet[0] = 0x01;
+    IPBUF->ethhdr.dest.octet[1] = 0x00;
+    IPBUF->ethhdr.dest.octet[2] = 0x5e;
+    IPBUF->ethhdr.dest.octet[3] = IPBUF->destipaddr.u8[1];
+    IPBUF->ethhdr.dest.octet[4] = IPBUF->destipaddr.u8[2];
+    IPBUF->ethhdr.dest.octet[5] = IPBUF->destipaddr.u8[3];
   } else {
     /* Check if the destination address is on the local network. */
     if(!uip_ipaddr_maskcmp(&IPBUF->destipaddr, &uip_hostaddr, &uip_netmask)) {
@@ -407,19 +408,19 @@
       /* The destination address was not in our ARP table, so we
 	 overwrite the IP packet with an ARP request. */
 
-      memset(BUF->ethhdr.dest.addr, 0xff, 6);
-      memset(BUF->dhwaddr.addr, 0x00, 6);
-      memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
-      memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6);
+      memset(BUF->ethhdr.dest.octet, 0xff, 6);
+      memset(BUF->dhwaddr.octet, 0x00, 6);
+      memcpy(BUF->ethhdr.src.octet, uip_ethaddr.octet, 6);
+      memcpy(BUF->shwaddr.octet, uip_ethaddr.octet, 6);
     
       uip_ipaddr_copy(&BUF->dipaddr, &ipaddr);
       uip_ipaddr_copy(&BUF->sipaddr, &uip_hostaddr);
       BUF->opcode = UIP_HTONS(ARP_REQUEST); /* ARP request. */
       BUF->hwtype = UIP_HTONS(ARP_HWTYPE_ETH);
-      BUF->protocol = UIP_HTONS(UIP_ETHTYPE_IP);
+      BUF->protocol = UIP_HTONS(EthType_Ip);
       BUF->hwlen = 6;
       BUF->protolen = 4;
-      BUF->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_ARP);
+      BUF->ethhdr.type = UIP_HTONS(EthType_Arp);
 
       uip_appdata = &uip_buf[UIP_TCPIP_HLEN + CONFIG_UIP_LLH_LEN];
     
@@ -428,13 +429,13 @@
     }
 
     /* Build an ethernet header. */
-    memcpy(IPBUF->ethhdr.dest.addr, tabptr->ethaddr.addr, 6);
+    memcpy(IPBUF->ethhdr.dest.octet, tabptr->ethaddr.octet, 6);
   }
-  memcpy(IPBUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
+  memcpy(IPBUF->ethhdr.src.octet, uip_ethaddr.octet, 6);
   
-  IPBUF->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_IP);
+  IPBUF->ethhdr.type = UIP_HTONS(EthType_Ip);
 
-  uip_len += sizeof(struct uip_eth_hdr);
+  uip_len += sizeof(EtherHdr);
 }
 /*-----------------------------------------------------------------------------------*/
 
diff --git a/src/net/uip/arp.h b/src/net/uip/arp.h
index 6eb4453..2cdffa2 100644
--- a/src/net/uip/arp.h
+++ b/src/net/uip/arp.h
@@ -52,22 +52,11 @@
 #ifndef __NET_UIP_ARP_H__
 #define __NET_UIP_ARP_H__
 
+#include "net/ethernet.h"
 #include "net/uip/uip.h"
 
 
-extern struct uip_eth_addr uip_ethaddr;
-
-/**
- * The Ethernet header.
- */
-struct uip_eth_hdr {
-  struct uip_eth_addr dest;
-  struct uip_eth_addr src;
-  uint16_t type;
-};
-
-#define UIP_ETHTYPE_ARP  0x0806
-#define UIP_ETHTYPE_IP   0x0800
+extern MacAddress uip_ethaddr;
 
 
 /* The uip_arp_init() function must be called before any of the other
@@ -125,17 +114,17 @@
  * code. It cannot be used to change the MAC address of the Ethernet
  * card.
  *
- * \param eaddr A pointer to a struct uip_eth_addr containing the
+ * \param eaddr A pointer to a MacAddress containing the
  * Ethernet MAC address of the Ethernet card.
  *
  * \hideinitializer
  */
-#define uip_setethaddr(eaddr) do {uip_ethaddr.addr[0] = (eaddr).addr[0]; \
-                              uip_ethaddr.addr[1] = (eaddr).addr[1];\
-                              uip_ethaddr.addr[2] = (eaddr).addr[2];\
-                              uip_ethaddr.addr[3] = (eaddr).addr[3];\
-                              uip_ethaddr.addr[4] = (eaddr).addr[4];\
-                              uip_ethaddr.addr[5] = (eaddr).addr[5];} while(0)
+#define uip_setethaddr(eaddr) do {uip_ethaddr.octet[0] = (eaddr).octet[0]; \
+                              uip_ethaddr.octet[1] = (eaddr).octet[1];\
+                              uip_ethaddr.octet[2] = (eaddr).octet[2];\
+                              uip_ethaddr.octet[3] = (eaddr).octet[3];\
+                              uip_ethaddr.octet[4] = (eaddr).octet[4];\
+                              uip_ethaddr.octet[5] = (eaddr).octet[5];} while(0)
 
 /** @} */
 
diff --git a/src/net/uip/uip.c b/src/net/uip/uip.c
index 962b9cd..15b2cd1 100644
--- a/src/net/uip/uip.c
+++ b/src/net/uip/uip.c
@@ -74,6 +74,7 @@
 #include <string.h>
 
 #include "base/algorithm.h"
+#include "net/ethernet.h"
 #include "net/uip/arch.h"
 #include "net/uip/arp.h"
 #include "net/uip/opt.h"
@@ -97,12 +98,12 @@
 const uip_ipaddr_t uip_broadcast_addr = { { 0xff, 0xff, 0xff, 0xff } };
 const uip_ipaddr_t uip_all_zeroes_addr = { { 0x0, /* rest is 0 */ } };
 
-struct uip_eth_addr uip_ethaddr = {{CONFIG_UIP_ETHADDR0,
-				    CONFIG_UIP_ETHADDR1,
-				    CONFIG_UIP_ETHADDR2,
-				    CONFIG_UIP_ETHADDR3,
-				    CONFIG_UIP_ETHADDR4,
-				    CONFIG_UIP_ETHADDR5}};
+MacAddress uip_ethaddr = {{CONFIG_UIP_ETHADDR0,
+			   CONFIG_UIP_ETHADDR1,
+			   CONFIG_UIP_ETHADDR2,
+			   CONFIG_UIP_ETHADDR3,
+			   CONFIG_UIP_ETHADDR4,
+			   CONFIG_UIP_ETHADDR5}};
 
 /* The packet buffer that contains incoming packets. */
 uip_buf_t uip_aligned_buf;
diff --git a/src/net/uip/uip.h b/src/net/uip/uip.h
index 976ce1d..b3a26a3 100644
--- a/src/net/uip/uip.h
+++ b/src/net/uip/uip.h
@@ -275,7 +275,7 @@
  * Ethernet, you will need to call the uIP ARP code before calling
  * this function:
  \code
- #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
+ #define BUF ((EtherHdr *)&uip_buf[0])
  uip_len = ethernet_devicedrver_poll();
  if(uip_len > 0) {
  if(BUF->type == UIP_HTONS(UIP_ETHTYPE_IP)) {