changeset 2441:1cc6480fcc9a draft

Move net.c to if.c. Ensure that if.c and if-KERNEL.c are namespaced correctly.
author Roy Marples <roy@marples.name>
date Fri, 25 Apr 2014 10:42:37 +0000
parents ccf0842319fe
children 8809d75691b7
files Makefile arp.c common.c common.h dhcp-common.c dhcp.c dhcp.h dhcp6.c dhcpcd.c duid.c duid.h if-bsd.c if-linux-wireless.c if-linux.c if-options.c if-options.h if-pref.c if.c if.h ipv4.c ipv4.h ipv4ll.c ipv6.c ipv6.h net.c net.h platform.h script.c script.h
diffstat 29 files changed, 807 insertions(+), 871 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Fri Apr 25 08:46:08 2014 +0000
+++ b/Makefile	Fri Apr 25 10:42:37 2014 +0000
@@ -2,7 +2,7 @@
 
 PROG=		dhcpcd
 SRCS=		common.c control.c dhcpcd.c duid.c eloop.c
-SRCS+=		if-options.c if-pref.c net.c script.c
+SRCS+=		if.c if-options.c if-pref.c script.c
 SRCS+=		dhcp-common.c
 
 CFLAGS?=	-O2
--- a/arp.c	Fri Apr 25 08:46:08 2014 +0000
+++ b/arp.c	Fri Apr 25 10:42:37 2014 +0000
@@ -25,6 +25,8 @@
  * SUCH DAMAGE.
  */
 
+#include <netinet/if_ether.h>
+
 #include <errno.h>
 #include <signal.h>
 #include <stdlib.h>
@@ -39,9 +41,9 @@
 #include "dhcp.h"
 #include "dhcpcd.h"
 #include "eloop.h"
+#include "if.h"
 #include "if-options.h"
 #include "ipv4ll.h"
-#include "net.h"
 
 #define ARP_LEN								      \
 	(sizeof(struct arphdr) + (2 * sizeof(uint32_t)) + (2 * HWADDR_LEN))
@@ -79,7 +81,7 @@
 	APPEND(&sip, sizeof(sip));
 	ZERO(ifp->hwlen);
 	APPEND(&tip, sizeof(tip));
-	return ipv4_sendrawpacket(ifp, ETHERTYPE_ARP, arp_buffer, len);
+	return if_sendrawpacket(ifp, ETHERTYPE_ARP, arp_buffer, len);
 
 eexit:
 	errno = ENOBUFS;
@@ -132,7 +134,7 @@
 	state = D_STATE(ifp);
 	state->fail.s_addr = 0;
 	for(;;) {
-		bytes = ipv4_getrawpacket(ifp, ETHERTYPE_ARP,
+		bytes = if_readrawpacket(ifp, ETHERTYPE_ARP,
 		    arp_buffer, sizeof(arp_buffer), NULL);
 		if (bytes == 0 || bytes == -1)
 			return;
@@ -232,7 +234,7 @@
 	if (state->new == NULL)
 		return;
 	if (state->arp_fd == -1) {
-		state->arp_fd = ipv4_opensocket(ifp, ETHERTYPE_ARP);
+		state->arp_fd = if_openrawsocket(ifp, ETHERTYPE_ARP);
 		if (state->arp_fd == -1) {
 			syslog(LOG_ERR, "%s: %s: %m", __func__, ifp->name);
 			return;
@@ -288,7 +290,7 @@
 	int arping = 0;
 
 	if (state->arp_fd == -1) {
-		state->arp_fd = ipv4_opensocket(ifp, ETHERTYPE_ARP);
+		state->arp_fd = if_openrawsocket(ifp, ETHERTYPE_ARP);
 		if (state->arp_fd == -1) {
 			syslog(LOG_ERR, "%s: %s: %m", __func__, ifp->name);
 			return;
--- a/common.c	Fri Apr 25 08:46:08 2014 +0000
+++ b/common.c	Fri Apr 25 10:42:37 2014 +0000
@@ -40,6 +40,7 @@
 #include <sys/param.h>
 #include <sys/time.h>
 
+#include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <limits.h>
@@ -180,3 +181,65 @@
 	return tv.tv_sec;
 }
 
+char *
+hwaddr_ntoa(const unsigned char *hwaddr, size_t hwlen, char *buf, size_t buflen)
+{
+	char *p;
+	size_t i;
+
+	if (buf == NULL) {
+		return NULL;
+	}
+
+	if (hwlen * 3 > buflen) {
+		errno = ENOBUFS;
+		return 0;
+	}
+
+	p = buf;
+	for (i = 0; i < hwlen; i++) {
+		if (i > 0)
+			*p ++= ':';
+		p += snprintf(p, 3, "%.2x", hwaddr[i]);
+	}
+	*p ++= '\0';
+	return buf;
+}
+
+size_t
+hwaddr_aton(unsigned char *buffer, const char *addr)
+{
+	char c[3];
+	const char *p = addr;
+	unsigned char *bp = buffer;
+	size_t len = 0;
+
+	c[2] = '\0';
+	while (*p) {
+		c[0] = *p++;
+		c[1] = *p++;
+		/* Ensure that digits are hex */
+		if (isxdigit((unsigned char)c[0]) == 0 ||
+		    isxdigit((unsigned char)c[1]) == 0)
+		{
+			errno = EINVAL;
+			return 0;
+		}
+		/* We should have at least two entries 00:01 */
+		if (len == 0 && *p == '\0') {
+			errno = EINVAL;
+			return 0;
+		}
+		/* Ensure that next data is EOL or a seperator with data */
+		if (!(*p == '\0' || (*p == ':' && *(p + 1) != '\0'))) {
+			errno = EINVAL;
+			return 0;
+		}
+		if (*p)
+			p++;
+		if (bp)
+			*bp++ = (unsigned char)strtol(c, NULL, 16);
+		len++;
+	}
+	return len;
+}
--- a/common.h	Fri Apr 25 08:46:08 2014 +0000
+++ b/common.h	Fri Apr 25 10:42:37 2014 +0000
@@ -108,4 +108,6 @@
 ssize_t setvard(char ***, const char *, const char *, size_t);
 time_t uptime(void);
 
+char *hwaddr_ntoa(const unsigned char *, size_t, char *, size_t);
+size_t hwaddr_aton(unsigned char *, const char *);
 #endif
--- a/dhcp-common.c	Fri Apr 25 08:46:08 2014 +0000
+++ b/dhcp-common.c	Fri Apr 25 10:42:37 2014 +0000
@@ -39,7 +39,8 @@
 #include "common.h"
 #include "dhcp-common.h"
 #include "dhcp.h"
-#include "platform.h"
+#include "if.h"
+#include "ipv6.h"
 
 struct dhcp_opt *
 vivso_find(uint32_t iana_en, const void *arg)
@@ -78,7 +79,7 @@
 	    utn.sysname, utn.release, utn.machine);
 	p += l;
 	len -= (size_t)l;
-	l = hardware_platform(p, len);
+	l = if_machinearch(p, len);
 	return (size_t)(p - str);
 }
 
--- a/dhcp.c	Fri Apr 25 08:46:08 2014 +0000
+++ b/dhcp.c	Fri Apr 25 10:42:37 2014 +0000
@@ -37,6 +37,7 @@
 #include <arpa/inet.h>
 #include <net/route.h>
 
+#include <netinet/if_ether.h>
 #include <netinet/in_systm.h>
 #include <netinet/in.h>
 #include <netinet/ip.h>
@@ -62,6 +63,7 @@
 #include "dhcp-common.h"
 #include "duid.h"
 #include "eloop.h"
+#include "if.h"
 #include "ipv4.h"
 #include "ipv4ll.h"
 #include "script.h"
@@ -783,9 +785,9 @@
 
 		*p++ = DHO_MAXMESSAGESIZE;
 		*p++ = 2;
-		mtu = get_mtu(iface->name);
+		mtu = if_getmtu(iface->name);
 		if (mtu < MTU_MIN) {
-			if (set_mtu(iface->name, MTU_MIN) == 0)
+			if (if_setmtu(iface->name, MTU_MIN) == 0)
 				sz = MTU_MIN;
 		} else if (mtu > MTU_MAX) {
 			/* Even though our MTU could be greater than
@@ -1590,7 +1592,7 @@
 		if (udp == NULL) {
 			syslog(LOG_ERR, "dhcp_makeudppacket: %m");
 		} else {
-			r = ipv4_sendrawpacket(iface, ETHERTYPE_IP,
+			r = if_sendrawpacket(iface, ETHERTYPE_IP,
 			    (uint8_t *)udp, ulen);
 			free(udp);
 		}
@@ -1600,7 +1602,7 @@
 		 * As such we remove it from consideration without actually
 		 * stopping the interface. */
 		if (r == -1) {
-			syslog(LOG_ERR, "%s: ipv4_sendrawpacket: %m",
+			syslog(LOG_ERR, "%s: if_sendrawpacket: %m",
 			    iface->name);
 			if (!(iface->ctx->options & DHCPCD_TEST))
 				dhcp_drop(iface, "FAIL");
@@ -2558,7 +2560,7 @@
 	 * The benefit is that if we get >1 DHCP packet in our buffer and
 	 * the first one fails for any reason, we can use the next. */
 	for(;;) {
-		bytes = (size_t)ipv4_getrawpacket(iface, ETHERTYPE_IP,
+		bytes = (size_t)if_readrawpacket(iface, ETHERTYPE_IP,
 		    iface->ctx->packet, udp_dhcp_len, &partialcsum);
 		if (bytes == 0 || (ssize_t)bytes == -1)
 			break;
@@ -2679,7 +2681,7 @@
 
 	state = D_STATE(ifp);
 	if (state->raw_fd == -1) {
-		state->raw_fd = ipv4_opensocket(ifp, ETHERTYPE_IP);
+		state->raw_fd = if_openrawsocket(ifp, ETHERTYPE_IP);
 		if (state->raw_fd == -1) {
 			syslog(LOG_ERR, "%s: %s: %m", __func__, ifp->name);
 			return -1;
--- a/dhcp.h	Fri Apr 25 08:46:08 2014 +0000
+++ b/dhcp.h	Fri Apr 25 10:42:37 2014 +0000
@@ -243,7 +243,6 @@
 
 #include "dhcpcd.h"
 #include "if-options.h"
-#include "net.h"
 
 #ifdef INET
 char *decode_rfc3361(const uint8_t *, size_t);
--- a/dhcp6.c	Fri Apr 25 08:46:08 2014 +0000
+++ b/dhcp6.c	Fri Apr 25 10:42:37 2014 +0000
@@ -54,7 +54,6 @@
 #include "duid.h"
 #include "eloop.h"
 #include "ipv6nd.h"
-#include "platform.h"
 #include "script.h"
 
 #ifndef __UNCONST
--- a/dhcpcd.c	Fri Apr 25 08:46:08 2014 +0000
+++ b/dhcpcd.c	Fri Apr 25 10:42:37 2014 +0000
@@ -60,13 +60,12 @@
 #include "dhcp6.h"
 #include "duid.h"
 #include "eloop.h"
+#include "if.h"
 #include "if-options.h"
 #include "if-pref.h"
 #include "ipv4.h"
 #include "ipv6.h"
 #include "ipv6nd.h"
-#include "net.h"
-#include "platform.h"
 #include "script.h"
 
 #ifdef USE_SIGNALS
@@ -325,7 +324,7 @@
 	eloop_timeout_delete(ctx->eloop, NULL, ifp);
 	if (ifp->options->options & DHCPCD_DEPARTED)
 		script_runreason(ifp, "DEPARTED");
-	free_interface(ifp);
+	if_free(ifp);
 	if (!(ctx->options & (DHCPCD_MASTER | DHCPCD_TEST)))
 		eloop_exit(ctx->eloop, EXIT_FAILURE);
 }
@@ -351,7 +350,7 @@
 		ifo->options &= ~(DHCPCD_ARP | DHCPCD_IPV4LL);
 	if (!(ifp->flags & (IFF_POINTOPOINT | IFF_LOOPBACK | IFF_MULTICAST)))
 		ifo->options &= ~DHCPCD_IPV6RS;
-	if (ifo->options & DHCPCD_LINK && carrier_status(ifp) == LINK_UNKNOWN)
+	if (ifo->options & DHCPCD_LINK && if_carrier(ifp) == LINK_UNKNOWN)
 		ifo->options &= ~DHCPCD_LINK;
 
 	if (ifo->metric != -1)
@@ -362,9 +361,9 @@
 
 	/* We want to disable kernel interface RA as early as possible. */
 	if (ifo->options & DHCPCD_IPV6RS) {
-		ra_global = check_ipv6(ifp->ctx, NULL,
+		ra_global = if_checkipv6(ifp->ctx, NULL,
 		    ifp->ctx->options & DHCPCD_IPV6RA_OWN ? 1 : 0);
-		ra_iface = check_ipv6(ifp->ctx, ifp->name,
+		ra_iface = if_checkipv6(ifp->ctx, ifp->name,
 		    ifp->options->options & DHCPCD_IPV6RA_OWN ? 1 : 0);
 		if (ra_global == -1 || ra_iface == -1)
 			ifo->options &= ~DHCPCD_IPV6RS;
@@ -504,7 +503,7 @@
 		return;
 
 	if (carrier == LINK_UNKNOWN)
-		carrier = carrier_status(ifp); /* will set ifp->flags */
+		carrier = if_carrier(ifp); /* will set ifp->flags */
 	else
 		ifp->flags = flags;
 
@@ -537,7 +536,7 @@
 			handle_interface(ctx, 0, ifp->name);
 #endif
 			if (ifp->wireless)
-				getifssid(ifp->name, ifp->ssid);
+				if_getssid(ifp->name, ifp->ssid);
 			configure_interface(ifp, ctx->argc, ctx->argv);
 			script_runreason(ifp, "CARRIER");
 			start_interface(ifp);
@@ -657,8 +656,8 @@
 	struct dhcpcd_ctx *ctx;
 
 	ctx = arg;
-	if (manage_link(ctx) == -1 && errno != ENXIO && errno != ENODEV)
-		syslog(LOG_ERR, "manage_link: %m");
+	if (if_managelink(ctx) == -1 && errno != ENXIO && errno != ENODEV)
+		syslog(LOG_ERR, "if_managelink: %m");
 }
 
 static void
@@ -681,7 +680,7 @@
 
 	reason = NULL; /* appease gcc */
 	if (ifo->options & DHCPCD_LINK) {
-		switch (carrier_status(ifp)) {
+		switch (if_carrier(ifp)) {
 		case LINK_DOWN:
 			ifp->carrier = LINK_DOWN;
 			reason = "NOCARRIER";
@@ -732,7 +731,7 @@
 			return;
 	}
 
-	ifs = discover_interfaces(ctx, -1, UNCONST(argv));
+	ifs = if_discover(ctx, -1, UNCONST(argv));
 	TAILQ_FOREACH_SAFE(ifp, ifs, next, ifn) {
 		if (strcmp(ifp->name, ifname) != 0)
 			continue;
@@ -757,7 +756,7 @@
 	/* Free our discovered list */
 	while ((ifp = TAILQ_FIRST(ifs))) {
 		TAILQ_REMOVE(ifs, ifp, next);
-		free_interface(ifp);
+		if_free(ifp);
 	}
 	free(ifs);
 }
@@ -807,7 +806,7 @@
 	struct if_head *ifs;
 	struct interface *ifn, *ifp;
 
-	ifs = discover_interfaces(ctx, argc - oi, argv + oi);
+	ifs = if_discover(ctx, argc - oi, argv + oi);
 	if (ifs == NULL)
 		return;
 
@@ -819,7 +818,7 @@
 				if_reboot(ifn, argc, argv);
 			else
 				ipv4_applyaddr(ifn);
-			free_interface(ifp);
+			if_free(ifp);
 		} else {
 			init_state(ifp, argc, argv);
 			TAILQ_INSERT_TAIL(ctx->ifaces, ifp, next);
@@ -1442,7 +1441,7 @@
 	 * instead.
 	 * We also need to open this before checking for interfaces below
 	 * so that we pickup any new addresses during the discover phase. */
-	ctx.link_fd = open_link_socket();
+	ctx.link_fd = if_openlinksocket();
 	if (ctx.link_fd == -1)
 		syslog(LOG_ERR, "open_link_socket: %m");
 	else
@@ -1454,7 +1453,7 @@
 	    (DHCPCD_MASTER | DHCPCD_DEV))
 		dev_start(&ctx);
 
-	ctx.ifaces = discover_interfaces(&ctx, ctx.ifc, ctx.ifv);
+	ctx.ifaces = if_discover(&ctx, ctx.ifc, ctx.ifv);
 	for (i = 0; i < ctx.ifc; i++) {
 		if (find_interface(&ctx, ctx.ifv[i]) == NULL)
 			syslog(LOG_ERR, "%s: interface not found or invalid",
@@ -1545,7 +1544,7 @@
 	if (ctx.ifaces) {
 		while ((ifp = TAILQ_FIRST(ctx.ifaces))) {
 			TAILQ_REMOVE(ctx.ifaces, ifp, next);
-			free_interface(ifp);
+			if_free(ifp);
 		}
 		free(ctx.ifaces);
 	}
@@ -1557,7 +1556,7 @@
 
 	free_options(ifo);
 	free_globals(&ctx);
-	restore_kernel_ra(&ctx);
+	if_rarestore(&ctx);
 	ipv4_ctxfree(&ctx);
 	ipv6_ctxfree(&ctx);
 	dev_stop(&ctx, !(ctx.options & DHCPCD_FORKED));
--- a/duid.c	Fri Apr 25 08:46:08 2014 +0000
+++ b/duid.c	Fri Apr 25 10:42:37 2014 +0000
@@ -48,8 +48,8 @@
 #endif
 
 #include "common.h"
+#include "dhcpcd.h"
 #include "duid.h"
-#include "net.h"
 
 static size_t
 duid_make(unsigned char *d, const struct interface *ifp, uint16_t type)
--- a/duid.h	Fri Apr 25 08:46:08 2014 +0000
+++ b/duid.h	Fri Apr 25 10:42:37 2014 +0000
@@ -28,11 +28,7 @@
 #ifndef DUID_H
 #define DUID_H
 
-#include "net.h"
-
-#ifndef DUID_LEN
-#  define DUID_LEN	128 + 2
-#endif
+#define DUID_LEN	128 + 2
 
 size_t duid_init(const struct interface *);
 
--- a/if-bsd.c	Fri Apr 25 08:46:08 2014 +0000
+++ b/if-bsd.c	Fri Apr 25 10:42:37 2014 +0000
@@ -42,6 +42,7 @@
 #ifdef __FreeBSD__ /* Needed so that including netinet6/in6_var.h works */
 #  include <net/if_var.h>
 #endif
+#include <net/if_ether.h>
 #include <net/if_media.h>
 #include <net/route.h>
 #include <netinet/in.h>
@@ -69,10 +70,10 @@
 #include "config.h"
 #include "common.h"
 #include "dhcp.h"
+#include "if.h"
 #include "if-options.h"
 #include "ipv4.h"
 #include "ipv6.h"
-#include "platform.h"
 
 #include "bpf-filter.h"
 
@@ -109,7 +110,7 @@
 }
 
 int
-open_link_socket(void)
+if_openlinksocket(void)
 {
 
 #ifdef SOCK_CLOEXEC
@@ -136,7 +137,7 @@
 }
 
 int
-getifssid(const char *ifname, char *ssid)
+if_getssid(const char *ifname, char *ssid)
 {
 	int s, retval = -1;
 #if defined(SIOCG80211NWID)
@@ -206,7 +207,7 @@
 	if (ifmr.ifm_status & IFM_AVALID &&
 	    IFM_TYPE(ifmr.ifm_active) == IFM_IEEE80211)
 	{
-		if (getifssid(ifname, NULL) == -1)
+		if (if_getssid(ifname, NULL) == -1)
 			return 1;
 	}
 	return 0;
@@ -214,7 +215,7 @@
 
 #ifdef INET
 int
-ipv4_opensocket(struct interface *ifp, int protocol)
+if_openrawsocket(struct interface *ifp, int protocol)
 {
 	struct dhcp_state *state;
 	int fd = -1;
@@ -303,7 +304,7 @@
 }
 
 ssize_t
-ipv4_sendrawpacket(const struct interface *ifp, int protocol,
+if_sendrawpacket(const struct interface *ifp, int protocol,
     const void *data, size_t len)
 {
 	struct iovec iov[2];
@@ -329,7 +330,7 @@
 /* BPF requires that we read the entire buffer.
  * So we pass the buffer in the API so we can loop on >1 packet. */
 ssize_t
-ipv4_getrawpacket(struct interface *ifp, int protocol,
+if_readrawpacket(struct interface *ifp, int protocol,
     void *data, size_t len, int *partialcsum)
 {
 	int fd = -1;
@@ -380,6 +381,7 @@
 			return bytes;
 	}
 }
+
 int
 if_address(const struct interface *iface, const struct in_addr *address,
     const struct in_addr *netmask, const struct in_addr *broadcast,
@@ -728,7 +730,7 @@
 
 #ifdef INET6
 int
-in6_addr_flags(const char *ifname, const struct in6_addr *addr)
+if_addrflags6(const char *ifname, const struct in6_addr *addr)
 {
 	int s, flags;
 	struct in6_ifreq ifr6;
@@ -749,7 +751,7 @@
 #endif
 
 int
-manage_link(struct dhcpcd_ctx *ctx)
+if_managelink(struct dhcpcd_ctx *ctx)
 {
 	/* route and ifwatchd like a msg buf size of 2048 */
 	char msg[2048], *p, *e, *cp, ifname[IF_NAMESIZE];
@@ -891,7 +893,7 @@
 					    sin6->sin6_addr.s6_addr,
 					    sizeof(ia6.s6_addr));
 					if (rtm->rtm_type == RTM_NEWADDR) {
-						ifa_flags = in6_addr_flags(
+						ifa_flags = if_addrflags6(
 								ifname,
 								&ia6);
 						if (ifa_flags == -1)
@@ -918,7 +920,7 @@
 #  endif
 #endif
 int
-hardware_platform(char *str, size_t len)
+if_machinearch(char *str, size_t len)
 {
 	int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
 	char march[SYS_NMLN];
@@ -989,7 +991,7 @@
 }
 
 void
-restore_kernel_ra(struct dhcpcd_ctx *ctx)
+if_rarestore(struct dhcpcd_ctx *ctx)
 {
 
 	if (ctx->options & DHCPCD_FORKED)
@@ -1018,7 +1020,7 @@
 }
 
 static int
-ipv6_ra_flush(void)
+if_raflush(void)
 {
 	int s;
 	char dummy[IFNAMSIZ + 8];
@@ -1036,7 +1038,7 @@
 }
 
 int
-check_ipv6(struct dhcpcd_ctx *ctx, const char *ifname, int own)
+if_checkipv6(struct dhcpcd_ctx *ctx, const char *ifname, int own)
 {
 	int ra;
 
@@ -1120,19 +1122,10 @@
 		/* Flush the kernel knowledge of advertised routers
 		 * and prefixes so the kernel does not expire prefixes
 		 * and default routes we are trying to own. */
-		ipv6_ra_flush();
+		if_raflush();
 	}
 
 	ctx->ra_global = ra;
 	return ra;
 }
-
-int
-ipv6_dadtransmits(__unused const char *ifname)
-{
-	int r;
-
-	r = get_inet6_sysctl(IPV6CTL_DAD_COUNT);
-	return r < 0 ? 0 : r;
-}
 #endif
--- a/if-linux-wireless.c	Fri Apr 25 08:46:08 2014 +0000
+++ b/if-linux-wireless.c	Fri Apr 25 10:42:37 2014 +0000
@@ -1,6 +1,6 @@
 /*
  * dhcpcd - DHCP client daemon
- * Copyright (c) 2009-2013 Roy Marples <roy@marples.name>
+ * Copyright (c) 2009-2014 Roy Marples <roy@marples.name>
  * All rights reserved
 
  * Redistribution and use in source and binary forms, with or without
@@ -57,13 +57,13 @@
 #include "common.h"
 #include "config.h"
 
-/* We can't include net.h or dhcpcd.h because
+/* We can't include if.h or dhcpcd.h because
  * they would pull in net/if.h, which defeats the purpose of this hack. */
 #define IF_SSIDSIZE 33
-int getifssid(const char *ifname, char *ssid);
+int if_getssid(const char *ifname, char *ssid);
 
 int
-getifssid(const char *ifname, char *ssid)
+if_getssid(const char *ifname, char *ssid)
 {
 #ifdef SIOCGIWESSID
 	int s, retval;
--- a/if-linux.c	Fri Apr 25 08:46:08 2014 +0000
+++ b/if-linux.c	Fri Apr 25 10:42:37 2014 +0000
@@ -39,6 +39,7 @@
 
 #include <arpa/inet.h>
 #include <net/if.h>
+#include <net/if_ether.h>
 #include <netinet/in_systm.h>
 #include <netinet/in.h>
 #include <net/route.h>
@@ -70,20 +71,15 @@
 #include "common.h"
 #include "dev.h"
 #include "dhcp.h"
+#include "if.h"
 #include "ipv4.h"
 #include "ipv6.h"
-#include "net.h"
-#include "platform.h"
 
 #define bpf_insn		sock_filter
 #define BPF_SKIPTYPE
 #define BPF_ETHCOOK		-ETH_HLEN
 #define BPF_WHOLEPACKET	0x0fffffff /* work around buggy LPF filters */
 
-#include "config.h"
-#include "common.h"
-#include "dhcp.h"
-#include "ipv4.h"
 #include "bpf-filter.h"
 
 /* Broadcast address for IPoIB */
@@ -136,7 +132,7 @@
 	;
 
 int
-hardware_platform(char *str, size_t len)
+if_machinearch(char *str, size_t len)
 {
 	FILE *fp;
 	char buf[256];
@@ -241,7 +237,7 @@
 }
 
 int
-open_link_socket(void)
+if_openlinksocket(void)
 {
 	struct sockaddr_nl snl;
 
@@ -640,7 +636,7 @@
 }
 
 int
-manage_link(struct dhcpcd_ctx *ctx)
+if_managelink(struct dhcpcd_ctx *ctx)
 {
 
 	return get_netlink(ctx, ctx->link_fd, MSG_DONTWAIT, &link_netlink);
@@ -739,7 +735,7 @@
 
 #ifdef INET
 int
-ipv4_opensocket(struct interface *ifp, int protocol)
+if_openrawsocket(struct interface *ifp, int protocol)
 {
 	int s;
 	union sockunion {
@@ -806,7 +802,7 @@
 }
 
 ssize_t
-ipv4_sendrawpacket(const struct interface *ifp, int protocol,
+if_sendrawpacket(const struct interface *ifp, int protocol,
     const void *data, size_t len)
 {
 	const struct dhcp_state *state;
@@ -838,7 +834,7 @@
 }
 
 ssize_t
-ipv4_getrawpacket(struct interface *ifp, int protocol,
+if_readrawpacket(struct interface *ifp, int protocol,
     void *data, size_t len, int *partialcsum)
 {
 	struct iovec iov = {
@@ -1110,7 +1106,7 @@
 }
 
 int
-in6_addr_flags(const char *ifname, const struct in6_addr *addr)
+if_addrflags6(const char *ifname, const struct in6_addr *addr)
 {
 	FILE *fp;
 	char *p, ifaddress[33], address[33], name[IF_NAMESIZE + 1];
@@ -1180,7 +1176,7 @@
 static const char *prefix = "/proc/sys/net/ipv6/conf";
 
 void
-restore_kernel_ra(struct dhcpcd_ctx *ctx)
+if_rarestore(struct dhcpcd_ctx *ctx)
 {
 	char path[256];
 
@@ -1200,7 +1196,7 @@
 }
 
 int
-check_ipv6(struct dhcpcd_ctx *ctx, const char *ifname, int own)
+if_checkipv6(struct dhcpcd_ctx *ctx, const char *ifname, int own)
 {
 	int ra;
 	size_t i;
@@ -1255,18 +1251,4 @@
 
 	return ra;
 }
-
-int
-ipv6_dadtransmits(const char *ifname)
-{
-	char path[256];
-	int r;
-
-	if (ifname == NULL)
-		ifname = "default";
-
-	snprintf(path, sizeof(path), "%s/%s/dad_transmits", prefix, ifname);
-	r = check_proc_int(path);
-	return r < 0 ? 0 : r;
-}
 #endif
--- a/if-options.c	Fri Apr 25 08:46:08 2014 +0000
+++ b/if-options.c	Fri Apr 25 10:42:37 2014 +0000
@@ -52,7 +52,6 @@
 #include "dhcpcd-embedded.h"
 #include "if-options.h"
 #include "ipv4.h"
-#include "platform.h"
 
 /* These options only make sense in the config file, so don't use any
    valid short options for them */
@@ -1861,7 +1860,6 @@
 #ifdef INET6
 	ifo->options |= DHCPCD_IPV6 | DHCPCD_IPV6RS | DHCPCD_IPV6RA_REQRDNSS;
 	ifo->options |= DHCPCD_DHCP6;
-	ifo->dadtransmits = ipv6_dadtransmits(ifname);
 #endif
 	ifo->timeout = DEFAULT_TIMEOUT;
 	ifo->reboot = DEFAULT_REBOOT;
--- a/if-options.h	Fri Apr 25 08:46:08 2014 +0000
+++ b/if-options.h	Fri Apr 25 10:42:37 2014 +0000
@@ -166,9 +166,6 @@
 	uint16_t ia_type;
 	struct if_ia *ia;
 	size_t ia_len;
-#ifdef INET6
-	int dadtransmits;
-#endif
 
 	struct dhcp_opt *dhcp_override;
 	size_t dhcp_override_len;
--- a/if-pref.c	Fri Apr 25 08:46:08 2014 +0000
+++ b/if-pref.c	Fri Apr 25 10:42:37 2014 +0000
@@ -29,8 +29,8 @@
 
 #include "config.h"
 #include "dhcp.h"
+#include "dhcpcd.h"
 #include "if-pref.h"
-#include "net.h"
 
 /* Interface comparer for working out ordering. */
 static int
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/if.c	Fri Apr 25 10:42:37 2014 +0000
@@ -0,0 +1,501 @@
+/*
+ * dhcpcd - DHCP client daemon
+ * Copyright (c) 2006-2014 Roy Marples <roy@marples.name>
+ * All rights reserved
+
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+
+#include <net/if.h>
+#include <net/if_arp.h>
+#include <netinet/in.h>
+#ifdef __FreeBSD__ /* Needed so that including netinet6/in6_var.h works */
+#  include <net/if_var.h>
+#endif
+#ifdef AF_LINK
+#  include <net/if_dl.h>
+#  include <net/if_types.h>
+#  include <netinet/in_var.h>
+#endif
+#ifdef AF_PACKET
+#  include <netpacket/packet.h>
+#endif
+#ifdef SIOCGIFMEDIA
+#  include <net/if_media.h>
+#endif
+
+#include <net/route.h>
+#ifdef __linux__
+#  include <asm/types.h> /* for systems with broken headers */
+#  include <linux/rtnetlink.h>
+#endif
+
+#include <ctype.h>
+#include <errno.h>
+#include <ifaddrs.h>
+#include <fnmatch.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+#include <unistd.h>
+
+#include "config.h"
+#include "common.h"
+#include "dev.h"
+#include "dhcp.h"
+#include "dhcp6.h"
+#include "if.h"
+#include "if-options.h"
+#include "ipv4.h"
+#include "ipv6nd.h"
+
+void
+if_free(struct interface *ifp)
+{
+
+	if (ifp == NULL)
+		return;
+	ipv4_free(ifp);
+	dhcp_free(ifp);
+	ipv6_free(ifp);
+	dhcp6_free(ifp);
+	ipv6nd_free(ifp);
+	free_options(ifp->options);
+	free(ifp);
+}
+
+int
+if_carrier(struct interface *iface)
+{
+	int s, r;
+	struct ifreq ifr;
+#ifdef SIOCGIFMEDIA
+	struct ifmediareq ifmr;
+#endif
+#ifdef __linux__
+	char *p;
+#endif
+
+	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
+		return LINK_UNKNOWN;
+	memset(&ifr, 0, sizeof(ifr));
+	strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
+#ifdef __linux__
+	/* We can only test the real interface up */
+	if ((p = strchr(ifr.ifr_name, ':')))
+		*p = '\0';
+#endif
+
+	if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1) {
+		close(s);
+		return LINK_UNKNOWN;
+	}
+	iface->flags = (unsigned int)ifr.ifr_flags;
+
+	r = LINK_UNKNOWN;
+#ifdef SIOCGIFMEDIA
+	memset(&ifmr, 0, sizeof(ifmr));
+	strlcpy(ifmr.ifm_name, iface->name, sizeof(ifmr.ifm_name));
+	if (ioctl(s, SIOCGIFMEDIA, &ifmr) != -1 &&
+	    ifmr.ifm_status & IFM_AVALID)
+		r = (ifmr.ifm_status & IFM_ACTIVE) ? LINK_UP : LINK_DOWN;
+#endif
+	if (r == LINK_UNKNOWN)
+		r = (ifr.ifr_flags & IFF_RUNNING) ? LINK_UP : LINK_DOWN;
+	close(s);
+	return r;
+}
+
+static int
+up_interface(struct interface *iface)
+{
+	struct ifreq ifr;
+	int s, r;
+#ifdef __linux__
+	char *p;
+#endif
+
+	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
+		return -1;
+	memset(&ifr, 0, sizeof(ifr));
+	strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
+#ifdef __linux__
+	/* We can only bring the real interface up */
+	if ((p = strchr(ifr.ifr_name, ':')))
+		*p = '\0';
+#endif
+	r = -1;
+	if (ioctl(s, SIOCGIFFLAGS, &ifr) == 0) {
+		if ((ifr.ifr_flags & IFF_UP))
+			r = 0;
+		else {
+			ifr.ifr_flags |= IFF_UP;
+			if (ioctl(s, SIOCSIFFLAGS, &ifr) == 0)
+				r = 0;
+		}
+		iface->flags = (unsigned int)ifr.ifr_flags;
+	}
+	close(s);
+	return r;
+}
+
+struct if_head *
+if_discover(struct dhcpcd_ctx *ctx, int argc, char * const *argv)
+{
+	struct ifaddrs *ifaddrs, *ifa;
+	char *p;
+	int i;
+	sa_family_t sdl_type;
+	struct if_head *ifs;
+	struct interface *ifp;
+#ifdef __linux__
+	char ifn[IF_NAMESIZE];
+#endif
+#ifdef INET
+	const struct sockaddr_in *addr;
+	const struct sockaddr_in *net;
+	const struct sockaddr_in *dst;
+#endif
+#ifdef INET6
+	const struct sockaddr_in6 *sin6;
+	int ifa_flags;
+#endif
+#ifdef AF_LINK
+	const struct sockaddr_dl *sdl;
+#ifdef SIOCGIFPRIORITY
+	struct ifreq ifr;
+	int s_inet;
+#endif
+#ifdef IFLR_ACTIVE
+	struct if_laddrreq iflr;
+	int s_link;
+#endif
+
+#ifdef SIOCGIFPRIORITY
+	if ((s_inet = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
+		return NULL;
+#endif
+#ifdef IFLR_ACTIVE
+	if ((s_link = socket(AF_LINK, SOCK_DGRAM, 0)) == -1) {
+#ifdef SIOCGIFPRIORITY
+		close(s_inet);
+#endif
+		return NULL;
+	}
+	memset(&iflr, 0, sizeof(iflr));
+#endif
+#elif AF_PACKET
+	const struct sockaddr_ll *sll;
+#endif
+
+	if (getifaddrs(&ifaddrs) == -1)
+		return NULL;
+	ifs = malloc(sizeof(*ifs));
+	if (ifs == NULL)
+		return NULL;
+	TAILQ_INIT(ifs);
+
+	for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
+		if (ifa->ifa_addr != NULL) {
+#ifdef AF_LINK
+			if (ifa->ifa_addr->sa_family != AF_LINK)
+				continue;
+#elif AF_PACKET
+			if (ifa->ifa_addr->sa_family != AF_PACKET)
+				continue;
+#endif
+		}
+
+		/* Ensure that the interface name has settled */
+		if (!dev_initialized(ctx, ifa->ifa_name))
+			continue;
+
+		/* It's possible for an interface to have >1 AF_LINK.
+		 * For our purposes, we use the first one. */
+		TAILQ_FOREACH(ifp, ifs, next) {
+			if (strcmp(ifp->name, ifa->ifa_name) == 0)
+				break;
+		}
+		if (ifp)
+			continue;
+		if (argc > 0) {
+			for (i = 0; i < argc; i++) {
+#ifdef __linux__
+				/* Check the real interface name */
+				strlcpy(ifn, argv[i], sizeof(ifn));
+				p = strchr(ifn, ':');
+				if (p)
+					*p = '\0';
+				if (strcmp(ifn, ifa->ifa_name) == 0)
+					break;
+#else
+				if (strcmp(argv[i], ifa->ifa_name) == 0)
+					break;
+#endif
+			}
+			if (i == argc)
+				continue;
+			p = argv[i];
+		} else {
+			p = ifa->ifa_name;
+			/* -1 means we're discovering against a specific
+			 * interface, but we still need the below rules
+			 * to apply. */
+			if (argc == -1 && strcmp(argv[0], ifa->ifa_name) != 0)
+				continue;
+		}
+		for (i = 0; i < ctx->ifdc; i++)
+			if (!fnmatch(ctx->ifdv[i], p, 0))
+				break;
+		if (i < ctx->ifdc)
+			continue;
+		for (i = 0; i < ctx->ifac; i++)
+			if (!fnmatch(ctx->ifav[i], p, 0))
+				break;
+		if (ctx->ifac && i == ctx->ifac)
+			continue;
+
+		if (if_vimaster(ifa->ifa_name) == 1) {
+			syslog(argc ? LOG_ERR : LOG_DEBUG,
+				"%s: is a Virtual Interface Master, skipping",
+				ifa->ifa_name);
+			continue;
+		}
+
+		ifp = calloc(1, sizeof(*ifp));
+		if (ifp == NULL) {
+			syslog(LOG_ERR, "%s: %m", __func__);
+			break;
+		}
+		ifp->ctx = ctx;
+		strlcpy(ifp->name, p, sizeof(ifp->name));
+		ifp->flags = ifa->ifa_flags;
+
+		/* Bring the interface up if not already */
+		if (!(ifp->flags & IFF_UP)
+#ifdef SIOCGIFMEDIA
+		    && if_carrier(ifp) != LINK_UNKNOWN
+#endif
+		   )
+		{
+			if (up_interface(ifp) == 0)
+				ctx->options |= DHCPCD_WAITUP;
+			else
+				syslog(LOG_ERR, "%s: up_interface: %m",
+				    ifp->name);
+		}
+
+		sdl_type = 0;
+		/* Don't allow loopback unless explicit */
+		if (ifp->flags & IFF_LOOPBACK) {
+			if (argc == 0 && ctx->ifac == 0) {
+				if_free(ifp);
+				continue;
+			}
+		} else if (ifa->ifa_addr != NULL) {
+#ifdef AF_LINK
+			sdl = (const struct sockaddr_dl *)(void *)ifa->ifa_addr;
+
+#ifdef IFLR_ACTIVE
+			/* We need to check for active address */
+			strlcpy(iflr.iflr_name, ifp->name,
+			    sizeof(iflr.iflr_name));
+			memcpy(&iflr.addr, ifa->ifa_addr,
+			    MIN(ifa->ifa_addr->sa_len, sizeof(iflr.addr)));
+			iflr.flags = IFLR_PREFIX;
+			iflr.prefixlen = sdl->sdl_alen * NBBY;
+			if (ioctl(s_link, SIOCGLIFADDR, &iflr) == -1 ||
+			    !(iflr.flags & IFLR_ACTIVE))
+			{
+				if_free(ifp);
+				continue;
+			}
+#endif
+
+			ifp->index = sdl->sdl_index;
+			sdl_type = sdl->sdl_type;
+			switch(sdl->sdl_type) {
+			case IFT_BRIDGE: /* FALLTHROUGH */
+			case IFT_L2VLAN: /* FALLTHOUGH */
+			case IFT_L3IPVLAN: /* FALLTHROUGH */
+			case IFT_ETHER:
+				ifp->family = ARPHRD_ETHER;
+				break;
+			case IFT_IEEE1394:
+				ifp->family = ARPHRD_IEEE1394;
+				break;
+#ifdef IFT_INFINIBAND
+			case IFT_INFINIBAND:
+				ifp->family = ARPHRD_INFINIBAND;
+				break;
+#endif
+			}
+			ifp->hwlen = sdl->sdl_alen;
+#ifndef CLLADDR
+#  define CLLADDR(s) ((const char *)((s)->sdl_data + (s)->sdl_nlen))
+#endif
+			memcpy(ifp->hwaddr, CLLADDR(sdl), ifp->hwlen);
+#elif AF_PACKET
+			sll = (const struct sockaddr_ll *)(void *)ifa->ifa_addr;
+			ifp->index = (unsigned int)sll->sll_ifindex;
+			ifp->family = sdl_type = sll->sll_hatype;
+			ifp->hwlen = sll->sll_halen;
+			if (ifp->hwlen != 0)
+				memcpy(ifp->hwaddr, sll->sll_addr, ifp->hwlen);
+#endif
+		}
+#ifdef __linux__
+		/* PPP addresses on Linux don't have hardware addresses */
+		else
+			ifp->index = if_nametoindex(ifp->name);
+#endif
+
+		/* We only work on ethernet by default */
+		if (!(ifp->flags & IFF_POINTOPOINT) &&
+		    ifp->family != ARPHRD_ETHER)
+		{
+			if (argc == 0 && ctx->ifac == 0) {
+				if_free(ifp);
+				continue;
+			}
+			switch (ifp->family) {
+			case ARPHRD_IEEE1394: /* FALLTHROUGH */
+			case ARPHRD_INFINIBAND:
+				/* We don't warn for supported families */
+				break;
+			default:
+				syslog(LOG_WARNING,
+				    "%s: unsupported interface type %.2x"
+				    ", falling back to ethernet",
+				    ifp->name, sdl_type);
+				ifp->family = ARPHRD_ETHER;
+				break;
+			}
+		}
+
+		/* Handle any platform init for the interface */
+		if (if_init(ifp) == -1) {
+			syslog(LOG_ERR, "%s: if_init: %m", p);
+			if_free(ifp);
+			continue;
+		}
+
+		/* Ensure that the MTU is big enough for DHCP */
+		if (if_getmtu(ifp->name) < MTU_MIN &&
+		    if_setmtu(ifp->name, MTU_MIN) == -1)
+		{
+			syslog(LOG_ERR, "%s: set_mtu: %m", p);
+			if_free(ifp);
+			continue;
+		}
+
+#ifdef SIOCGIFPRIORITY
+		/* Respect the interface priority */
+		memset(&ifr, 0, sizeof(ifr));
+		strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
+		if (ioctl(s_inet, SIOCGIFPRIORITY, &ifr) == 0)
+			ifp->metric = ifr.ifr_metric;
+#else
+		/* We reserve the 100 range for virtual interfaces, if and when
+		 * we can work them out. */
+		ifp->metric = 200 + ifp->index;
+		if (if_getssid(ifp->name, ifp->ssid) != -1) {
+			ifp->wireless = 1;
+			ifp->metric += 100;
+		}
+#endif
+
+		TAILQ_INSERT_TAIL(ifs, ifp, next);
+	}
+
+	for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
+		if (ifa->ifa_addr == NULL)
+			continue;
+		switch(ifa->ifa_addr->sa_family) {
+#ifdef INET
+		case AF_INET:
+			addr = (const struct sockaddr_in *)
+			    (void *)ifa->ifa_addr;
+			net = (const struct sockaddr_in *)
+			    (void *)ifa->ifa_netmask;
+			if (ifa->ifa_flags & IFF_POINTOPOINT)
+				dst = (const struct sockaddr_in *)
+				    (void *)ifa->ifa_dstaddr;
+			else
+				dst = NULL;
+			ipv4_handleifa(ctx, RTM_NEWADDR, ifs, ifa->ifa_name,
+				&addr->sin_addr,
+				&net->sin_addr,
+				dst ? &dst->sin_addr : NULL);
+			break;
+#endif
+#ifdef INET6
+		case AF_INET6:
+			sin6 = (const struct sockaddr_in6 *)
+			    (void *)ifa->ifa_addr;
+			ifa_flags = if_addrflags6(ifa->ifa_name,
+			    &sin6->sin6_addr);
+			if (ifa_flags != -1)
+				ipv6_handleifa(ctx, RTM_NEWADDR, ifs,
+				    ifa->ifa_name,
+				    &sin6->sin6_addr, ifa_flags);
+			break;
+#endif
+		}
+	}
+
+	freeifaddrs(ifaddrs);
+
+#ifdef SIOCGIFPRIORITY
+	close(s_inet);
+#endif
+#ifdef IFLR_ACTIVE
+	close(s_link);
+#endif
+
+	return ifs;
+}
+
+int
+if_domtu(const char *ifname, short int mtu)
+{
+	int s, r;
+	struct ifreq ifr;
+
+	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
+		return -1;
+	memset(&ifr, 0, sizeof(ifr));
+	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+	ifr.ifr_mtu = mtu;
+	r = ioctl(s, mtu ? SIOCSIFMTU : SIOCGIFMTU, &ifr);
+	close(s);
+	if (r == -1)
+		return -1;
+	return ifr.ifr_mtu;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/if.h	Fri Apr 25 10:42:37 2014 +0000
@@ -0,0 +1,139 @@
+/*
+ * dhcpcd - DHCP client daemon
+ * Copyright (c) 2006-2014 Roy Marples <roy@marples.name>
+ * All rights reserved
+
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef INTERFACE_H
+#define INTERFACE_H
+
+#include <sys/socket.h>
+
+#include <net/if.h>
+#include <netinet/in.h>
+
+#include "config.h"
+#include "dhcpcd.h"
+#include "ipv4.h"
+#include "ipv6.h"
+
+/* Some systems have route metrics */
+#ifndef HAVE_ROUTE_METRIC
+# if defined(__linux__) || defined(SIOCGIFPRIORITY)
+#  define HAVE_ROUTE_METRIC 1
+# endif
+# ifndef HAVE_ROUTE_METRIC
+#  define HAVE_ROUTE_METRIC 0
+# endif
+#endif
+
+#define EUI64_ADDR_LEN			8
+#define INFINIBAND_ADDR_LEN		20
+
+/* Linux 2.4 doesn't define this */
+#ifndef ARPHRD_IEEE1394
+#  define ARPHRD_IEEE1394		24
+#endif
+
+/* The BSD's don't define this yet */
+#ifndef ARPHRD_INFINIBAND
+#  define ARPHRD_INFINIBAND		32
+#endif
+
+/* Work out if we have a private address or not
+ * 10/8
+ * 172.16/12
+ * 192.168/16
+ */
+#ifndef IN_PRIVATE
+# define IN_PRIVATE(addr) (((addr & IN_CLASSA_NET) == 0x0a000000) ||	      \
+	    ((addr & 0xfff00000)    == 0xac100000) ||			      \
+	    ((addr & IN_CLASSB_NET) == 0xc0a80000))
+#endif
+
+#define LINKLOCAL_ADDR	0xa9fe0000
+#define LINKLOCAL_MASK	IN_CLASSB_NET
+#define LINKLOCAL_BRDC	(LINKLOCAL_ADDR | ~LINKLOCAL_MASK)
+
+#ifndef IN_LINKLOCAL
+# define IN_LINKLOCAL(addr) ((addr & IN_CLASSB_NET) == LINKLOCAL_ADDR)
+#endif
+
+struct if_head *if_discover(struct dhcpcd_ctx *, int, char * const *);
+void if_free(struct interface *);
+int if_domtu(const char *, short int);
+#define if_getmtu(iface) if_domtu(iface, 0)
+#define if_setmtu(iface, mtu) if_domtu(iface, mtu)
+int if_carrier(struct interface *);
+
+/* The below functions are provided by if-KERNEL.c */
+int if_conf(struct interface *);
+int if_init(struct interface *);
+int if_getssid(const char *, char *);
+int if_vimaster(const char *);
+int if_openlinksocket(void);
+int if_managelink(struct dhcpcd_ctx *);
+
+#ifdef INET
+int if_openrawsocket(struct interface *, int);
+ssize_t if_sendrawpacket(const struct interface *,
+    int, const void *, size_t);
+ssize_t if_readrawpacket(struct interface *, int, void *, size_t, int *);
+
+int if_address(const struct interface *,
+    const struct in_addr *, const struct in_addr *,
+    const struct in_addr *, int);
+#define if_addaddress(iface, addr, net, brd)				      \
+	if_address(iface, addr, net, brd, 1)
+#define if_setaddress(iface, addr, net, brd)				      \
+	if_address(iface, addr, net, brd, 2)
+#define if_deladdress(iface, addr, net)				      \
+	if_address(iface, addr, net, NULL, -1)
+
+int if_route(const struct rt *rt, int);
+#define if_addroute(rt) if_route(rt, 1)
+#define if_chgroute(rt) if_route(rt, 0)
+#define if_delroute(rt) if_route(rt, -1)
+#endif
+
+#ifdef INET6
+int if_checkipv6(struct dhcpcd_ctx *ctx, const char *, int);
+void if_rarestore(struct dhcpcd_ctx *);
+
+int if_address6(const struct ipv6_addr *, int);
+#define if_addaddress6(a) if_address6(a, 1)
+#define if_deladdress6(a) if_address6(a, -1)
+int if_addrflags6(const char *, const struct in6_addr *);
+
+int if_route6(const struct rt6 *rt, int);
+#define if_addroute6(rt) if_route6(rt, 1)
+#define if_chgroute6(rt) if_route6(rt, 0)
+#define if_delroute6(rt) if_route6(rt, -1)
+#else
+#define if_checkipv6(a, b, c) (-1)
+#define if_rarestore(a)
+#endif
+
+int if_machinearch(char *, size_t);
+#endif
--- a/ipv4.c	Fri Apr 25 08:46:08 2014 +0000
+++ b/ipv4.c	Fri Apr 25 10:42:37 2014 +0000
@@ -49,10 +49,10 @@
 #include "common.h"
 #include "dhcpcd.h"
 #include "dhcp.h"
+#include "if.h"
 #include "if-options.h"
 #include "if-pref.h"
 #include "ipv4.h"
-#include "net.h"
 #include "script.h"
 
 #define IPV4_LOOPBACK_ROUTE
@@ -259,11 +259,11 @@
 	 * prefer the interface.
 	 * This also has the nice side effect of flushing ARP entries so
 	 * we don't have to do that manually. */
-	if (ipv4_deleteroute(ort) == -1 && errno != ESRCH)
-		syslog(LOG_ERR, "%s: ipv4_deleteroute: %m", ort->iface->name);
-	if (!ipv4_addroute(nrt))
+	if (if_delroute(ort) == -1 && errno != ESRCH)
+		syslog(LOG_ERR, "%s: ipv4_delroute: %m", ort->iface->name);
+	if (!if_addroute(nrt))
 		return 0;
-	syslog(LOG_ERR, "%s: ipv4_addroute: %m", nrt->iface->name);
+	syslog(LOG_ERR, "%s: if_addroute: %m", nrt->iface->name);
 	return -1;
 }
 
@@ -273,9 +273,9 @@
 	int retval;
 
 	desc_route("deleting", rt);
-	retval = ipv4_deleteroute(rt);
+	retval = if_delroute(rt);
 	if (retval != 0 && errno != ENOENT && errno != ESRCH)
-		syslog(LOG_ERR,"%s: ipv4_deleteroute: %m", rt->iface->name);
+		syslog(LOG_ERR,"%s: if_delroute: %m", rt->iface->name);
 	return retval;
 }
 
@@ -564,7 +564,7 @@
 
 	syslog(LOG_DEBUG, "%s: deleting IP address %s/%d",
 	    ifp->name, inet_ntoa(*addr), inet_ntocidr(*net));
-	r = ipv4_deleteaddress(ifp, addr, net);
+	r = if_deladdress(ifp, addr, net);
 	if (r == -1 && errno != EADDRNOTAVAIL && errno != ENXIO &&
 	    errno != ENODEV)
 		syslog(LOG_ERR, "%s: %s: %m", ifp->name, __func__);
@@ -666,13 +666,13 @@
 		    ifp->name, inet_ntoa(lease->addr),
 		    inet_ntocidr(lease->net));
 		if (ifo->options & DHCPCD_NOALIAS)
-			r = ipv4_setaddress(ifp,
+			r = if_setaddress(ifp,
 			    &lease->addr, &lease->net, &lease->brd);
 		else
-			r = ipv4_addaddress(ifp,
+			r = if_addaddress(ifp,
 			    &lease->addr, &lease->net, &lease->brd);
 		if (r == -1 && errno != EEXIST) {
-			syslog(LOG_ERR, "%s: ipv4_addaddress: %m", __func__);
+			syslog(LOG_ERR, "%s: if_addaddress: %m", __func__);
 			return;
 		}
 		istate = ipv4_getstate(ifp);
@@ -698,7 +698,7 @@
 		rt->iface = ifp;
 		rt->metric = 0;
 		if (!find_route(ifp->ctx->ipv4_routes, rt, NULL))
-			ipv4_deleteroute(rt);
+			if_delroute(rt);
 		free(rt);
 	}
 
--- a/ipv4.h	Fri Apr 25 08:46:08 2014 +0000
+++ b/ipv4.h	Fri Apr 25 10:42:37 2014 +0000
@@ -74,27 +74,8 @@
 void ipv4_handleifa(struct dhcpcd_ctx *, int, struct if_head *, const char *,
     const struct in_addr *, const struct in_addr *, const struct in_addr *);
 
-int if_address(const struct interface *,
-    const struct in_addr *, const struct in_addr *,
-    const struct in_addr *, int);
-#define ipv4_addaddress(iface, addr, net, brd)				      \
-	if_address(iface, addr, net, brd, 1)
-#define ipv4_setaddress(iface, addr, net, brd)				      \
-	if_address(iface, addr, net, brd, 2)
-#define ipv4_deleteaddress(iface, addr, net)				      \
-	if_address(iface, addr, net, NULL, -1)
-
-int if_route(const struct rt *rt, int);
-#define ipv4_addroute(rt) if_route(rt, 1)
-#define ipv4_changeroute(rt) if_route(rt, 0)
-#define ipv4_deleteroute(rt) if_route(rt, -1)
-#define del_src_route(rt) i_route(rt, -2);
 void ipv4_freeroutes(struct rt_head *);
 
-int ipv4_opensocket(struct interface *, int);
-ssize_t ipv4_sendrawpacket(const struct interface *,
-    int, const void *, size_t);
-ssize_t ipv4_getrawpacket(struct interface *, int, void *, size_t, int *);
 void ipv4_free(struct interface *);
 void ipv4_ctxfree(struct dhcpcd_ctx *);
 #else
--- a/ipv4ll.c	Fri Apr 25 08:46:08 2014 +0000
+++ b/ipv4ll.c	Fri Apr 25 10:42:37 2014 +0000
@@ -37,9 +37,9 @@
 #include "common.h"
 #include "dhcp.h"
 #include "eloop.h"
+#include "if.h"
 #include "if-options.h"
 #include "ipv4ll.h"
-#include "net.h"
 
 static struct dhcp_message *
 ipv4ll_make_lease(uint32_t addr)
--- a/ipv6.c	Fri Apr 25 08:46:08 2014 +0000
+++ b/ipv6.c	Fri Apr 25 10:42:37 2014 +0000
@@ -67,6 +67,7 @@
 #include "dhcpcd.h"
 #include "dhcp6.h"
 #include "eloop.h"
+#include "if.h"
 #include "ipv6.h"
 #include "ipv6nd.h"
 
@@ -406,8 +407,8 @@
 	if (!(ap->flags & IPV6_AF_DADCOMPLETED) &&
 	    ipv6_findaddr(ap->iface, &ap->addr))
 		ap->flags |= IPV6_AF_DADCOMPLETED;
-	if (add_address6(ap) == -1) {
-		syslog(LOG_ERR, "add_address6 %m");
+	if (if_addaddress6(ap) == -1) {
+		syslog(LOG_ERR, "if_addaddress6: %m");
 		return -1;
 	}
 	ap->flags &= ~IPV6_AF_NEW;
@@ -416,7 +417,7 @@
 		ap->flags |= IPV6_AF_DELEGATED;
 	if (ap->iface->options->options & DHCPCD_IPV6RA_OWN &&
 	    ipv6_removesubnet(ap->iface, ap) == -1)
-		syslog(LOG_ERR,"ipv6_removesubnet %m");
+		syslog(LOG_ERR,"ipv6_removesubnet: %m");
 	if (ap->prefix_pltime == ND6_INFINITE_LIFETIME &&
 	    ap->prefix_vltime == ND6_INFINITE_LIFETIME)
 		syslog(LOG_DEBUG,
@@ -465,9 +466,9 @@
 				    ap->iface->name, ap->saddr);
 				i++;
 				if (!IN6_IS_ADDR_UNSPECIFIED(&ap->addr) &&
-				    del_address6(ap) == -1 &&
+				    if_deladdress6(ap) == -1 &&
 				    errno != EADDRNOTAVAIL && errno != ENXIO)
-					syslog(LOG_ERR, "del_address6 %m");
+					syslog(LOG_ERR, "if_deladdress6: %m");
 			}
 			eloop_q_timeout_delete(ap->iface->ctx->eloop,
 			    0, NULL, ap);
@@ -506,9 +507,9 @@
 		{
 			syslog(LOG_INFO, "%s: deleting address %s",
 			    ap->iface->name, ap->saddr);
-			if (del_address6(ap) == -1 &&
+			if (if_deladdress6(ap) == -1 &&
 			    errno != EADDRNOTAVAIL && errno != ENXIO)
-				syslog(LOG_ERR, "del_address6 %m");
+				syslog(LOG_ERR, "if_deladdress6: :%m");
 		}
 		free(ap);
 	}
@@ -820,11 +821,11 @@
 	desc_route(add ? "adding" : "changing", nrt);
 	/* We delete and add the route so that we can change metric and
 	 * prefer the interface. */
-	if (del_route6(ort) == -1 && errno != ESRCH)
-		syslog(LOG_ERR, "%s: del_route6: %m", ort->iface->name);
-	if (add_route6(nrt) == 0)
+	if (if_delroute6(ort) == -1 && errno != ESRCH)
+		syslog(LOG_ERR, "%s: if_delroute6: %m", ort->iface->name);
+	if (if_addroute6(nrt) == 0)
 		return 0;
-	syslog(LOG_ERR, "%s: add_route6: %m", nrt->iface->name);
+	syslog(LOG_ERR, "%s: if_addroute6: %m", nrt->iface->name);
 	return -1;
 }
 
@@ -834,9 +835,9 @@
 	int retval;
 
 	desc_route("deleting", rt);
-	retval = del_route6(rt);
+	retval = if_delroute6(rt);
 	if (retval != 0 && errno != ENOENT && errno != ESRCH)
-		syslog(LOG_ERR,"%s: del_route6: %m", rt->iface->name);
+		syslog(LOG_ERR,"%s: if_delroute6: %m", rt->iface->name);
 	return retval;
 }
 
@@ -938,7 +939,7 @@
 		if (!find_route6(ifp->ctx->ipv6->routes, rt))
 #endif
 		{
-			r = del_route6(rt);
+			r = if_delroute6(rt);
 			if (r == -1 && errno == ESRCH)
 				r = 0;
 		}
--- a/ipv6.h	Fri Apr 25 08:46:08 2014 +0000
+++ b/ipv6.h	Fri Apr 25 10:42:37 2014 +0000
@@ -186,17 +186,6 @@
 int ipv6_removesubnet(struct interface *, struct ipv6_addr *);
 void ipv6_buildroutes(struct dhcpcd_ctx *);
 
-int if_address6(const struct ipv6_addr *, int);
-#define add_address6(a) if_address6(a, 1)
-#define del_address6(a) if_address6(a, -1)
-int in6_addr_flags(const char *, const struct in6_addr *);
-
-int if_route6(const struct rt6 *rt, int);
-#define add_route6(rt) if_route6(rt, 1)
-#define change_route6(rt) if_route6(rt, 0)
-#define del_route6(rt) if_route6(rt, -1)
-#define del_src_route6(rt) if_route6(rt, -2);
-
 #else
 #define ipv6_init(a) NULL
 #define ipv6_free_ll_callbacks(a)
--- a/net.c	Fri Apr 25 08:46:08 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,564 +0,0 @@
-/*
- * dhcpcd - DHCP client daemon
- * Copyright (c) 2006-2014 Roy Marples <roy@marples.name>
- * All rights reserved
-
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/param.h>
-#include <sys/types.h>
-#include <sys/ioctl.h>
-#include <sys/socket.h>
-
-#include <net/if.h>
-#include <net/if_arp.h>
-#include <netinet/in.h>
-#ifdef __FreeBSD__ /* Needed so that including netinet6/in6_var.h works */
-#  include <net/if_var.h>
-#endif
-#ifdef AF_LINK
-#  include <net/if_dl.h>
-#  include <net/if_types.h>
-#  include <netinet/in_var.h>
-#endif
-#ifdef AF_PACKET
-#  include <netpacket/packet.h>
-#endif
-#ifdef SIOCGIFMEDIA
-#  include <net/if_media.h>
-#endif
-
-#include <net/route.h>
-#ifdef __linux__
-#  include <asm/types.h> /* for systems with broken headers */
-#  include <linux/rtnetlink.h>
-#endif
-
-#include <ctype.h>
-#include <errno.h>
-#include <ifaddrs.h>
-#include <fnmatch.h>
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <syslog.h>
-#include <unistd.h>
-
-#include "config.h"
-#include "common.h"
-#include "dev.h"
-#include "dhcp.h"
-#include "dhcp6.h"
-#include "if-options.h"
-#include "ipv4.h"
-#include "ipv6nd.h"
-#include "net.h"
-
-char *
-hwaddr_ntoa(const unsigned char *hwaddr, size_t hwlen, char *buf, size_t buflen)
-{
-	char *p;
-	size_t i;
-
-	if (buf == NULL) {
-		return NULL;
-	}
-
-	if (hwlen * 3 > buflen) {
-		errno = ENOBUFS;
-		return 0;
-	}
-
-	p = buf;
-	for (i = 0; i < hwlen; i++) {
-		if (i > 0)
-			*p ++= ':';
-		p += snprintf(p, 3, "%.2x", hwaddr[i]);
-	}
-	*p ++= '\0';
-	return buf;
-}
-
-size_t
-hwaddr_aton(unsigned char *buffer, const char *addr)
-{
-	char c[3];
-	const char *p = addr;
-	unsigned char *bp = buffer;
-	size_t len = 0;
-
-	c[2] = '\0';
-	while (*p) {
-		c[0] = *p++;
-		c[1] = *p++;
-		/* Ensure that digits are hex */
-		if (isxdigit((unsigned char)c[0]) == 0 ||
-		    isxdigit((unsigned char)c[1]) == 0)
-		{
-			errno = EINVAL;
-			return 0;
-		}
-		/* We should have at least two entries 00:01 */
-		if (len == 0 && *p == '\0') {
-			errno = EINVAL;
-			return 0;
-		}
-		/* Ensure that next data is EOL or a seperator with data */
-		if (!(*p == '\0' || (*p == ':' && *(p + 1) != '\0'))) {
-			errno = EINVAL;
-			return 0;
-		}
-		if (*p)
-			p++;
-		if (bp)
-			*bp++ = (unsigned char)strtol(c, NULL, 16);
-		len++;
-	}
-	return len;
-}
-
-void
-free_interface(struct interface *ifp)
-{
-
-	if (ifp == NULL)
-		return;
-	ipv4_free(ifp);
-	dhcp_free(ifp);
-	ipv6_free(ifp);
-	dhcp6_free(ifp);
-	ipv6nd_free(ifp);
-	free_options(ifp->options);
-	free(ifp);
-}
-
-int
-carrier_status(struct interface *iface)
-{
-	int s, r;
-	struct ifreq ifr;
-#ifdef SIOCGIFMEDIA
-	struct ifmediareq ifmr;
-#endif
-#ifdef __linux__
-	char *p;
-#endif
-
-	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
-		return LINK_UNKNOWN;
-	memset(&ifr, 0, sizeof(ifr));
-	strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
-#ifdef __linux__
-	/* We can only test the real interface up */
-	if ((p = strchr(ifr.ifr_name, ':')))
-		*p = '\0';
-#endif
-
-	if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1) {
-		close(s);
-		return LINK_UNKNOWN;
-	}
-	iface->flags = (unsigned int)ifr.ifr_flags;
-
-	r = LINK_UNKNOWN;
-#ifdef SIOCGIFMEDIA
-	memset(&ifmr, 0, sizeof(ifmr));
-	strlcpy(ifmr.ifm_name, iface->name, sizeof(ifmr.ifm_name));
-	if (ioctl(s, SIOCGIFMEDIA, &ifmr) != -1 &&
-	    ifmr.ifm_status & IFM_AVALID)
-		r = (ifmr.ifm_status & IFM_ACTIVE) ? LINK_UP : LINK_DOWN;
-#endif
-	if (r == LINK_UNKNOWN)
-		r = (ifr.ifr_flags & IFF_RUNNING) ? LINK_UP : LINK_DOWN;
-	close(s);
-	return r;
-}
-
-static int
-up_interface(struct interface *iface)
-{
-	struct ifreq ifr;
-	int s, r;
-#ifdef __linux__
-	char *p;
-#endif
-
-	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
-		return -1;
-	memset(&ifr, 0, sizeof(ifr));
-	strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
-#ifdef __linux__
-	/* We can only bring the real interface up */
-	if ((p = strchr(ifr.ifr_name, ':')))
-		*p = '\0';
-#endif
-	r = -1;
-	if (ioctl(s, SIOCGIFFLAGS, &ifr) == 0) {
-		if ((ifr.ifr_flags & IFF_UP))
-			r = 0;
-		else {
-			ifr.ifr_flags |= IFF_UP;
-			if (ioctl(s, SIOCSIFFLAGS, &ifr) == 0)
-				r = 0;
-		}
-		iface->flags = (unsigned int)ifr.ifr_flags;
-	}
-	close(s);
-	return r;
-}
-
-struct if_head *
-discover_interfaces(struct dhcpcd_ctx *ctx, int argc, char * const *argv)
-{
-	struct ifaddrs *ifaddrs, *ifa;
-	char *p;
-	int i;
-	sa_family_t sdl_type;
-	struct if_head *ifs;
-	struct interface *ifp;
-#ifdef __linux__
-	char ifn[IF_NAMESIZE];
-#endif
-#ifdef INET
-	const struct sockaddr_in *addr;
-	const struct sockaddr_in *net;
-	const struct sockaddr_in *dst;
-#endif
-#ifdef INET6
-	const struct sockaddr_in6 *sin6;
-	int ifa_flags;
-#endif
-#ifdef AF_LINK
-	const struct sockaddr_dl *sdl;
-#ifdef SIOCGIFPRIORITY
-	struct ifreq ifr;
-	int s_inet;
-#endif
-#ifdef IFLR_ACTIVE
-	struct if_laddrreq iflr;
-	int s_link;
-#endif
-
-#ifdef SIOCGIFPRIORITY
-	if ((s_inet = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
-		return NULL;
-#endif
-#ifdef IFLR_ACTIVE
-	if ((s_link = socket(AF_LINK, SOCK_DGRAM, 0)) == -1) {
-#ifdef SIOCGIFPRIORITY
-		close(s_inet);
-#endif
-		return NULL;
-	}
-	memset(&iflr, 0, sizeof(iflr));
-#endif
-#elif AF_PACKET
-	const struct sockaddr_ll *sll;
-#endif
-
-	if (getifaddrs(&ifaddrs) == -1)
-		return NULL;
-	ifs = malloc(sizeof(*ifs));
-	if (ifs == NULL)
-		return NULL;
-	TAILQ_INIT(ifs);
-
-	for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
-		if (ifa->ifa_addr != NULL) {
-#ifdef AF_LINK
-			if (ifa->ifa_addr->sa_family != AF_LINK)
-				continue;
-#elif AF_PACKET
-			if (ifa->ifa_addr->sa_family != AF_PACKET)
-				continue;
-#endif
-		}
-
-		/* Ensure that the interface name has settled */
-		if (!dev_initialized(ctx, ifa->ifa_name))
-			continue;
-
-		/* It's possible for an interface to have >1 AF_LINK.
-		 * For our purposes, we use the first one. */
-		TAILQ_FOREACH(ifp, ifs, next) {
-			if (strcmp(ifp->name, ifa->ifa_name) == 0)
-				break;
-		}
-		if (ifp)
-			continue;
-		if (argc > 0) {
-			for (i = 0; i < argc; i++) {
-#ifdef __linux__
-				/* Check the real interface name */
-				strlcpy(ifn, argv[i], sizeof(ifn));
-				p = strchr(ifn, ':');
-				if (p)
-					*p = '\0';
-				if (strcmp(ifn, ifa->ifa_name) == 0)
-					break;
-#else
-				if (strcmp(argv[i], ifa->ifa_name) == 0)
-					break;
-#endif
-			}
-			if (i == argc)
-				continue;
-			p = argv[i];
-		} else {
-			p = ifa->ifa_name;
-			/* -1 means we're discovering against a specific
-			 * interface, but we still need the below rules
-			 * to apply. */
-			if (argc == -1 && strcmp(argv[0], ifa->ifa_name) != 0)
-				continue;
-		}
-		for (i = 0; i < ctx->ifdc; i++)
-			if (!fnmatch(ctx->ifdv[i], p, 0))
-				break;
-		if (i < ctx->ifdc)
-			continue;
-		for (i = 0; i < ctx->ifac; i++)
-			if (!fnmatch(ctx->ifav[i], p, 0))
-				break;
-		if (ctx->ifac && i == ctx->ifac)
-			continue;
-
-		if (if_vimaster(ifa->ifa_name) == 1) {
-			syslog(argc ? LOG_ERR : LOG_DEBUG,
-				"%s: is a Virtual Interface Master, skipping",
-				ifa->ifa_name);
-			continue;
-		}
-
-		ifp = calloc(1, sizeof(*ifp));
-		if (ifp == NULL) {
-			syslog(LOG_ERR, "%s: %m", __func__);
-			break;
-		}
-		ifp->ctx = ctx;
-		strlcpy(ifp->name, p, sizeof(ifp->name));
-		ifp->flags = ifa->ifa_flags;
-
-		/* Bring the interface up if not already */
-		if (!(ifp->flags & IFF_UP)
-#ifdef SIOCGIFMEDIA
-		    && carrier_status(ifp) != LINK_UNKNOWN
-#endif
-		   )
-		{
-			if (up_interface(ifp) == 0)
-				ctx->options |= DHCPCD_WAITUP;
-			else
-				syslog(LOG_ERR, "%s: up_interface: %m",
-				    ifp->name);
-		}
-
-		sdl_type = 0;
-		/* Don't allow loopback unless explicit */
-		if (ifp->flags & IFF_LOOPBACK) {
-			if (argc == 0 && ctx->ifac == 0) {
-				free_interface(ifp);
-				continue;
-			}
-		} else if (ifa->ifa_addr != NULL) {
-#ifdef AF_LINK
-			sdl = (const struct sockaddr_dl *)(void *)ifa->ifa_addr;
-
-#ifdef IFLR_ACTIVE
-			/* We need to check for active address */
-			strlcpy(iflr.iflr_name, ifp->name,
-			    sizeof(iflr.iflr_name));
-			memcpy(&iflr.addr, ifa->ifa_addr,
-			    MIN(ifa->ifa_addr->sa_len, sizeof(iflr.addr)));
-			iflr.flags = IFLR_PREFIX;
-			iflr.prefixlen = sdl->sdl_alen * NBBY;
-			if (ioctl(s_link, SIOCGLIFADDR, &iflr) == -1 ||
-			    !(iflr.flags & IFLR_ACTIVE))
-			{
-				free_interface(ifp);
-				continue;
-			}
-#endif
-
-			ifp->index = sdl->sdl_index;
-			sdl_type = sdl->sdl_type;
-			switch(sdl->sdl_type) {
-			case IFT_BRIDGE: /* FALLTHROUGH */
-			case IFT_L2VLAN: /* FALLTHOUGH */
-			case IFT_L3IPVLAN: /* FALLTHROUGH */
-			case IFT_ETHER:
-				ifp->family = ARPHRD_ETHER;
-				break;
-			case IFT_IEEE1394:
-				ifp->family = ARPHRD_IEEE1394;
-				break;
-#ifdef IFT_INFINIBAND
-			case IFT_INFINIBAND:
-				ifp->family = ARPHRD_INFINIBAND;
-				break;
-#endif
-			}
-			ifp->hwlen = sdl->sdl_alen;
-#ifndef CLLADDR
-#  define CLLADDR(s) ((const char *)((s)->sdl_data + (s)->sdl_nlen))
-#endif
-			memcpy(ifp->hwaddr, CLLADDR(sdl), ifp->hwlen);
-#elif AF_PACKET
-			sll = (const struct sockaddr_ll *)(void *)ifa->ifa_addr;
-			ifp->index = (unsigned int)sll->sll_ifindex;
-			ifp->family = sdl_type = sll->sll_hatype;
-			ifp->hwlen = sll->sll_halen;
-			if (ifp->hwlen != 0)
-				memcpy(ifp->hwaddr, sll->sll_addr, ifp->hwlen);
-#endif
-		}
-#ifdef __linux__
-		/* PPP addresses on Linux don't have hardware addresses */
-		else
-			ifp->index = if_nametoindex(ifp->name);
-#endif
-
-		/* We only work on ethernet by default */
-		if (!(ifp->flags & IFF_POINTOPOINT) &&
-		    ifp->family != ARPHRD_ETHER)
-		{
-			if (argc == 0 && ctx->ifac == 0) {
-				free_interface(ifp);
-				continue;
-			}
-			switch (ifp->family) {
-			case ARPHRD_IEEE1394: /* FALLTHROUGH */
-			case ARPHRD_INFINIBAND:
-				/* We don't warn for supported families */
-				break;
-			default:
-				syslog(LOG_WARNING,
-				    "%s: unsupported interface type %.2x"
-				    ", falling back to ethernet",
-				    ifp->name, sdl_type);
-				ifp->family = ARPHRD_ETHER;
-				break;
-			}
-		}
-
-		/* Handle any platform init for the interface */
-		if (if_init(ifp) == -1) {
-			syslog(LOG_ERR, "%s: if_init: %m", p);
-			free_interface(ifp);
-			continue;
-		}
-
-		/* Ensure that the MTU is big enough for DHCP */
-		if (get_mtu(ifp->name) < MTU_MIN &&
-		    set_mtu(ifp->name, MTU_MIN) == -1)
-		{
-			syslog(LOG_ERR, "%s: set_mtu: %m", p);
-			free_interface(ifp);
-			continue;
-		}
-
-#ifdef SIOCGIFPRIORITY
-		/* Respect the interface priority */
-		memset(&ifr, 0, sizeof(ifr));
-		strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
-		if (ioctl(s_inet, SIOCGIFPRIORITY, &ifr) == 0)
-			ifp->metric = ifr.ifr_metric;
-#else
-		/* We reserve the 100 range for virtual interfaces, if and when
-		 * we can work them out. */
-		ifp->metric = 200 + ifp->index;
-		if (getifssid(ifp->name, ifp->ssid) != -1) {
-			ifp->wireless = 1;
-			ifp->metric += 100;
-		}
-#endif
-
-		TAILQ_INSERT_TAIL(ifs, ifp, next);
-	}
-
-	for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
-		if (ifa->ifa_addr == NULL)
-			continue;
-		switch(ifa->ifa_addr->sa_family) {
-#ifdef INET
-		case AF_INET:
-			addr = (const struct sockaddr_in *)
-			    (void *)ifa->ifa_addr;
-			net = (const struct sockaddr_in *)
-			    (void *)ifa->ifa_netmask;
-			if (ifa->ifa_flags & IFF_POINTOPOINT)
-				dst = (const struct sockaddr_in *)
-				    (void *)ifa->ifa_dstaddr;
-			else
-				dst = NULL;
-			ipv4_handleifa(ctx, RTM_NEWADDR, ifs, ifa->ifa_name,
-				&addr->sin_addr,
-				&net->sin_addr,
-				dst ? &dst->sin_addr : NULL);
-			break;
-#endif
-#ifdef INET6
-		case AF_INET6:
-			sin6 = (const struct sockaddr_in6 *)
-			    (void *)ifa->ifa_addr;
-			ifa_flags = in6_addr_flags(ifa->ifa_name,
-			    &sin6->sin6_addr);
-			if (ifa_flags != -1)
-				ipv6_handleifa(ctx, RTM_NEWADDR, ifs,
-				    ifa->ifa_name,
-				    &sin6->sin6_addr, ifa_flags);
-			break;
-#endif
-		}
-	}
-
-	freeifaddrs(ifaddrs);
-
-#ifdef SIOCGIFPRIORITY
-	close(s_inet);
-#endif
-#ifdef IFLR_ACTIVE
-	close(s_link);
-#endif
-
-	return ifs;
-}
-
-int
-do_mtu(const char *ifname, short int mtu)
-{
-	int s, r;
-	struct ifreq ifr;
-
-	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
-		return -1;
-	memset(&ifr, 0, sizeof(ifr));
-	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
-	ifr.ifr_mtu = mtu;
-	r = ioctl(s, mtu ? SIOCSIFMTU : SIOCGIFMTU, &ifr);
-	close(s);
-	if (r == -1)
-		return -1;
-	return ifr.ifr_mtu;
-}
--- a/net.h	Fri Apr 25 08:46:08 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,101 +0,0 @@
-/*
- * dhcpcd - DHCP client daemon
- * Copyright (c) 2006-2014 Roy Marples <roy@marples.name>
- * All rights reserved
-
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef INTERFACE_H
-#define INTERFACE_H
-
-#include <sys/socket.h>
-
-#include <net/if.h>
-#include <netinet/in.h>
-#include <netinet/if_ether.h>
-
-#include "config.h"
-#include "dhcp.h"
-#include "dhcpcd.h"
-#include "ipv6.h"
-
-/* Some systems have route metrics */
-#ifndef HAVE_ROUTE_METRIC
-# if defined(__linux__) || defined(SIOCGIFPRIORITY)
-#  define HAVE_ROUTE_METRIC 1
-# endif
-# ifndef HAVE_ROUTE_METRIC
-#  define HAVE_ROUTE_METRIC 0
-# endif
-#endif
-
-#define EUI64_ADDR_LEN			8
-#define INFINIBAND_ADDR_LEN		20
-
-/* Linux 2.4 doesn't define this */
-#ifndef ARPHRD_IEEE1394
-#  define ARPHRD_IEEE1394		24
-#endif
-
-/* The BSD's don't define this yet */
-#ifndef ARPHRD_INFINIBAND
-#  define ARPHRD_INFINIBAND		32
-#endif
-
-/* Work out if we have a private address or not
- * 10/8
- * 172.16/12
- * 192.168/16
- */
-#ifndef IN_PRIVATE
-# define IN_PRIVATE(addr) (((addr & IN_CLASSA_NET) == 0x0a000000) ||	      \
-	    ((addr & 0xfff00000)    == 0xac100000) ||			      \
-	    ((addr & IN_CLASSB_NET) == 0xc0a80000))
-#endif
-
-#define LINKLOCAL_ADDR	0xa9fe0000
-#define LINKLOCAL_MASK	IN_CLASSB_NET
-#define LINKLOCAL_BRDC	(LINKLOCAL_ADDR | ~LINKLOCAL_MASK)
-
-#ifndef IN_LINKLOCAL
-# define IN_LINKLOCAL(addr) ((addr & IN_CLASSB_NET) == LINKLOCAL_ADDR)
-#endif
-
-char *hwaddr_ntoa(const unsigned char *, size_t, char *, size_t);
-size_t hwaddr_aton(unsigned char *, const char *);
-
-int getifssid(const char *, char *);
-int if_vimaster(const char *);
-struct if_head *discover_interfaces(struct dhcpcd_ctx *, int, char * const *);
-void free_interface(struct interface *);
-int do_mtu(const char *, short int);
-#define get_mtu(iface) do_mtu(iface, 0)
-#define set_mtu(iface, mtu) do_mtu(iface, mtu)
-
-int if_conf(struct interface *);
-int if_init(struct interface *);
-
-int open_link_socket(void);
-int manage_link(struct dhcpcd_ctx *);
-int carrier_status(struct interface *);
-#endif
--- a/platform.h	Fri Apr 25 08:46:08 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,41 +0,0 @@
-/*
- * dhcpcd - DHCP client daemon
- * Copyright (c) 2006-2014 Roy Marples <roy@marples.name>
- * All rights reserved
-
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef PLATFORM_H
-#define PLATFORM_H
-
-int hardware_platform(char *, size_t);
-#ifdef INET6
-int check_ipv6(struct dhcpcd_ctx *ctx, const char *, int);
-int ipv6_dadtransmits(const char *);
-void restore_kernel_ra(struct dhcpcd_ctx *);
-#else
-#define check_ipv6(a, b,c ) -1
-#define restore_kernel_ra(a)
-#endif
-
-#endif
--- a/script.c	Fri Apr 25 08:46:08 2014 +0000
+++ b/script.c	Fri Apr 25 10:42:37 2014 +0000
@@ -46,10 +46,10 @@
 #include "common.h"
 #include "dhcp.h"
 #include "dhcp6.h"
+#include "if.h"
 #include "if-options.h"
 #include "if-pref.h"
 #include "ipv6nd.h"
-#include "net.h"
 #include "script.h"
 
 #ifdef HAVE_SPAWN_H
@@ -303,7 +303,7 @@
 	EMALLOC(6, e);
 	snprintf(env[6], e, "ifflags=%u", ifp->flags);
 	EMALLOC(7, e);
-	snprintf(env[7], e, "ifmtu=%d", get_mtu(ifp->name));
+	snprintf(env[7], e, "ifmtu=%d", if_getmtu(ifp->name));
 	l = e = strlen("interface_order=");
 	TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) {
 		e += strlen(ifp2->name) + 1;
--- a/script.h	Fri Apr 25 08:46:08 2014 +0000
+++ b/script.h	Fri Apr 25 10:42:37 2014 +0000
@@ -28,8 +28,6 @@
 #ifndef SCRIPT_H
 #define SCRIPT_H
 
-#include "net.h"
-
 void if_printoptions(void);
 int send_interface(int, const struct interface *);
 int script_runreason(const struct interface *, const char *);