changeset 1861:b580e72f6408 draft

Use a TAILQ macro for our interface list.
author Roy Marples <roy@marples.name>
date Tue, 19 Feb 2013 13:37:42 +0000
parents ad57240e2976
children 53174e89d78a
files dhcp.c dhcp6.c dhcpcd.c dhcpcd.h if-pref.c ipv4.c ipv6.c ipv6ns.c ipv6rs.c net.c net.h script.c
diffstat 12 files changed, 129 insertions(+), 138 deletions(-) [+]
line wrap: on
line diff
--- a/dhcp.c	Tue Feb 19 11:18:04 2013 +0000
+++ b/dhcp.c	Tue Feb 19 13:37:42 2013 +0000
@@ -2441,9 +2441,14 @@
 	struct interface *ifp;
 	struct dhcp_state *state;
 
-	ifaces = ifp = calloc(1, sizeof(*ifp));
+	ifaces = malloc(sizeof(*ifaces));
+	if (ifaces == NULL)
+		goto eexit;
+	TAILQ_INIT(ifaces);
+	ifp = calloc(1, sizeof(*ifp));
 	if (ifp == NULL)
 		goto eexit;
+	TAILQ_INSERT_HEAD(ifaces, ifp, next);
 	ifp->if_data[IF_DATA_DHCP] = state = calloc(1, sizeof(*state));
 	if (state == NULL)
 		goto eexit;
--- a/dhcp6.c	Tue Feb 19 11:18:04 2013 +0000
+++ b/dhcp6.c	Tue Feb 19 13:37:42 2013 +0000
@@ -920,7 +920,7 @@
 	const struct dhcp6_state *state;
 	const struct ipv6_addr *ap;
 
-	for (ifp = ifaces; ifp; ifp = ifp->next) {
+	TAILQ_FOREACH(ifp, ifaces, next) {
 		state = D6_CSTATE(ifp);
 		if (state == NULL)
 			continue;
@@ -1195,9 +1195,10 @@
 		return;
 	}
 
-	for (ifp = ifaces; ifp; ifp = ifp->next)
+	TAILQ_FOREACH(ifp, ifaces, next) {
 		if (ifp->index == (unsigned int)pkt.ipi6_ifindex)
 			break;
+	}
 	if (ifp == NULL) {
 		syslog(LOG_ERR, "DHCPv6 reply for unexpected interface from %s",
 		    sfrom);
@@ -1545,9 +1546,12 @@
 
 	/* If we don't have any more DHCP6 enabled interfaces,
 	 * close the global socket */
-	for (ifp = ifaces; ifp; ifp = ifp->next)
-		if (D6_STATE(ifp))
-			break;
+	if (ifaces) {
+		TAILQ_FOREACH(ifp, ifaces, next) {
+			if (D6_STATE(ifp))
+				break;
+		}
+	}
 	if (ifp == NULL && sock != -1) {
 		close(sock);
 		eloop_event_delete(sock);
--- a/dhcpcd.c	Tue Feb 19 11:18:04 2013 +0000
+++ b/dhcpcd.c	Tue Feb 19 13:37:42 2013 +0000
@@ -73,9 +73,9 @@
 #define RELEASE_DELAY_S		0
 #define RELEASE_DELAY_NS	10000000
 
+struct if_head *ifaces;
 char vendor[VENDORCLASSID_MAX_LEN];
 int pidfd = -1;
-struct interface *ifaces = NULL;
 struct if_options *if_options = NULL;
 int ifac = 0;
 char **ifav = NULL;
@@ -135,16 +135,16 @@
 cleanup(void)
 {
 #ifdef DEBUG_MEMORY
-	struct interface *iface;
+	struct interface *ifp;
 	int i;
 
 	free_options(if_options);
 
-	while (ifaces) {
-		iface = ifaces;
-		ifaces = iface->next;
-		free_interface(iface);
+	while ((ifp = TAILQ_FIRST(ifaces))) {
+		TAILQ_REMOVE(ifaces, ifp, next);
+		free_interface(ifp);
 	}
+	free(ifaces);
 
 	for (i = 0; i < ifac; i++)
 		free(ifav[i]);
@@ -256,37 +256,28 @@
 find_interface(const char *ifname)
 {
 	struct interface *ifp;
-	
-	for (ifp = ifaces; ifp; ifp = ifp->next)
+
+	TAILQ_FOREACH(ifp, ifaces, next) {
 		if (strcmp(ifp->name, ifname) == 0)
 			return ifp;
+	}
 	return NULL;
 }
 
 static void
-stop_interface(struct interface *iface)
+stop_interface(struct interface *ifp)
 {
-	struct interface *ifp, *ifl = NULL;
 
-	syslog(LOG_INFO, "%s: removing interface", iface->name);
+	syslog(LOG_INFO, "%s: removing interface", ifp->name);
 
 	// Remove the interface from our list
-	for (ifp = ifaces; ifp; ifp = ifp->next) {
-		if (ifp == iface)
-			break;
-		ifl = ifp;
-	}
-	if (ifl)
-		ifl->next = ifp->next;
-	else
-		ifaces = ifp->next;
-
-	dhcp6_drop(iface, NULL);
-	ipv6rs_drop(iface);
-//	if (strcmp(iface->state->reason, "RELEASE") != 0)
-		dhcp_drop(iface, "STOP");
-	dhcp_close(iface);
-	eloop_timeout_delete(NULL, iface);
+	TAILQ_REMOVE(ifaces, ifp, next);
+	dhcp6_drop(ifp, NULL);
+	ipv6rs_drop(ifp);
+//	if (strcmp(ifp->state->reason, "RELEASE") != 0)
+		dhcp_drop(ifp, "STOP");
+	dhcp_close(ifp);
+	eloop_timeout_delete(NULL, ifp);
 	free_interface(ifp);
 	if (!(options & (DHCPCD_MASTER | DHCPCD_TEST)))
 		exit(EXIT_FAILURE);
@@ -380,10 +371,8 @@
 
 	if (!(options & DHCPCD_LINK))
 		return;
-	for (ifp = ifaces; ifp; ifp = ifp->next)
-		if (strcmp(ifp->name, ifname) == 0)
-			break;
-	if (!ifp) {
+	ifp = find_interface(ifname);
+	if (ifp == NULL) {
 		if (options & DHCPCD_LINK)
 			handle_interface(1, ifname);
 		return;
@@ -487,7 +476,6 @@
 		syslog(LOG_ERR, "ipv6_init: %m");
 	}
 
-
 	if (!(options & DHCPCD_TEST))
 		script_runreason(ifp, "PREINIT");
 
@@ -514,7 +502,8 @@
 void
 handle_interface(int action, const char *ifname)
 {
-	struct interface *ifs, *ifp, *ifn, *ifl = NULL;
+	struct if_head *ifs;
+	struct interface *ifp, *ifn, *ifl = NULL;
 	const char * const argv[] = { ifname }; 
 	int i;
 
@@ -535,30 +524,31 @@
 	}
 
 	ifs = discover_interfaces(-1, UNCONST(argv));
-	for (ifp = ifs; ifp; ifp = ifp->next) {
+	TAILQ_FOREACH_SAFE(ifp, ifs, next, ifn) {
 		if (strcmp(ifp->name, ifname) != 0)
 			continue;
 		/* Check if we already have the interface */
-		for (ifn = ifaces; ifn; ifn = ifn->next) {
-			if (strcmp(ifn->name, ifp->name) == 0)
-				break;
-			ifl = ifn;
-		}
-		if (ifn) {
+		ifl = find_interface(ifp->name);
+		if (ifl) {
 			/* The flags and hwaddr could have changed */
-			ifn->flags = ifp->flags;
-			ifn->hwlen = ifp->hwlen;
+			ifl->flags = ifp->flags;
+			ifl->hwlen = ifp->hwlen;
 			if (ifp->hwlen != 0)
-				memcpy(ifn->hwaddr, ifp->hwaddr, ifn->hwlen);
+				memcpy(ifl->hwaddr, ifp->hwaddr, ifl->hwlen);
 		} else {
-			if (ifl)
-				ifl->next = ifp;
-			else
-				ifaces = ifp;
+			TAILQ_REMOVE(ifs, ifp, next);
+			TAILQ_INSERT_TAIL(ifaces, ifp, next);
 		}
 		init_state(ifp, 0, NULL);
 		start_interface(ifp);
 	}
+
+	/* Free our discovered list */
+	while ((ifp = TAILQ_FIRST(ifs))) {
+		TAILQ_REMOVE(ifs, ifp, next);
+		free_interface(ifp);
+	}
+	free(ifs);
 }
 
 #ifdef RTM_CHGADDR
@@ -569,7 +559,7 @@
 	struct if_options *ifo;
 	struct dhcp_state *state;
 
-	for (ifp = ifaces; ifp; ifp = ifp->next)
+	TAILQ_FOREACH(ifp, ifaces, next) {
 		if (strcmp(ifp->name, ifname) == 0 && ifp->hwlen <= hwlen) {
 			state = D_STATE(ifp);
 			if (state == NULL)
@@ -598,6 +588,7 @@
 				start_interface(ifp);
 			}
 		}
+	}
 	free(hwaddr);
 }
 #endif
@@ -616,19 +607,16 @@
 static void
 reconf_reboot(int action, int argc, char **argv, int oi)
 {
-	struct interface *ifl, *ifn, *ifp, *ifs, *ift;
+	struct if_head *ifs;
+	struct interface *ifn, *ifp;
 	
 	ifs = discover_interfaces(argc - oi, argv + oi);
 	if (ifs == NULL)
 		return;
 
-	for (ifp = ifs; ifp && (ift = ifp->next, 1); ifp = ift) {
-		ifl = NULL;
-		for (ifn = ifaces; ifn; ifn = ifn->next) {
-			if (strcmp(ifn->name, ifp->name) == 0)
-				break;
-			ifl = ifn;
-		}
+	while ((ifp = TAILQ_FIRST(ifs))) {
+		TAILQ_REMOVE(ifs, ifp, next);
+		ifn = find_interface(ifp->name);
 		if (ifn) {
 			if (action)
 				if_reboot(ifn, argc, argv);
@@ -636,15 +624,12 @@
 				ipv4_applyaddr(ifn);
 			free_interface(ifp);
 		} else {
-			ifp->next = NULL;
+			TAILQ_INSERT_TAIL(ifaces, ifp, next);
 			init_state(ifp, argc, argv);
 			start_interface(ifp);
-			if (ifl)
-				ifl->next = ifp;
-			else
-				ifaces = ifp;
 		}
 	}
+	free(ifs);
 
 	sort_interfaces();
 }
@@ -704,8 +689,9 @@
 		break;
 	case SIGUSR1:
 		syslog(LOG_INFO, "received SIGUSR, reconfiguring");
-		for (ifp = ifaces; ifp; ifp = ifp->next)
+		TAILQ_FOREACH(ifp, ifaces, next) {
 			ipv4_applyaddr(ifp);
+		}
 		return;
 	case SIGPIPE:
 		syslog(LOG_WARNING, "received SIGPIPE");
@@ -723,10 +709,7 @@
 	/* As drop_dhcp could re-arrange the order, we do it like this. */
 	for (;;) {
 		/* Be sane and drop the last config first */
-		for (ifp = ifaces; ifp; ifp = ifp->next) {
-			if (ifp->next == NULL)
-				break;
-		}
+		ifp = TAILQ_LAST(ifaces, if_head);
 		if (ifp == NULL)
 			break;
 		if (ifp->carrier != LINK_DOWN &&
@@ -776,7 +759,7 @@
 		} else if (strcmp(*argv, "--getinterfaces") == 0) {
 			len = 0;
 			if (argc == 1) {
-				for (ifp = ifaces; ifp; ifp = ifp->next) {
+				TAILQ_FOREACH(ifp, ifaces, next) {
 					len++;
 					if (D6_STATE_RUNNING(ifp))
 						len++;
@@ -786,13 +769,14 @@
 				len = write(fd->fd, &len, sizeof(len));
 				if (len != sizeof(len))
 					return -1;
-				for (ifp = ifaces; ifp; ifp = ifp->next)
+				TAILQ_FOREACH(ifp, ifaces, next) {
 					send_interface(fd->fd, ifp);
+				}
 				return 0;
 			}
 			opt = 0;
 			while (argv[++opt] != NULL) {
-				for (ifp = ifaces; ifp; ifp = ifp->next)
+				TAILQ_FOREACH(ifp, ifaces, next) {
 					if (strcmp(argv[opt], ifp->name) == 0) {
 						len++;
 						if (D6_STATE_RUNNING(ifp))
@@ -800,15 +784,17 @@
 						if (ipv6rs_has_ra(ifp))
 							len++;
 					}
+				}
 			}
 			len = write(fd->fd, &len, sizeof(len));
 			if (len != sizeof(len))
 				return -1;
 			opt = 0;
 			while (argv[++opt] != NULL) {
-				for (ifp = ifaces; ifp; ifp = ifp->next)
+				TAILQ_FOREACH(ifp, ifaces, next) {
 					if (strcmp(argv[opt], ifp->name) == 0)
 						send_interface(fd->fd, ifp);
+				}
 			}
 			return 0;
 		} else if (strcmp(*argv, "--listen") == 0) {
@@ -863,10 +849,7 @@
 
 	if (do_release || do_exit) {
 		for (oi = optind; oi < argc; oi++) {
-			for (ifp = ifaces; ifp; ifp = ifp->next)
-				if (strcmp(ifp->name, argv[oi]) == 0)
-					break;
-			if (!ifp)
+			if ((ifp = find_interface(argv[oi])) == NULL)
 				continue;
 			if (do_release)
 				ifp->options->options |= DHCPCD_RELEASE;
@@ -885,7 +868,7 @@
 int
 main(int argc, char **argv)
 {
-	struct interface *iface;
+	struct interface *ifp;
 	uint16_t family = 0;
 	int opt, oi = 0, sig = 0, i, control_fd;
 	size_t len;
@@ -1158,14 +1141,11 @@
 
 	ifaces = discover_interfaces(ifc, ifv);
 	for (i = 0; i < ifc; i++) {
-		for (iface = ifaces; iface; iface = iface->next)
-			if (strcmp(iface->name, ifv[i]) == 0)
-				break;
-		if (!iface)
+		if (find_interface(ifv[i]) == NULL)
 			syslog(LOG_ERR, "%s: interface not found or invalid",
 			    ifv[i]);
 	}
-	if (!ifaces) {
+	if (ifaces == NULL) {
 		if (ifc == 0)
 			syslog(LOG_ERR, "no valid interfaces found");
 		else
@@ -1181,9 +1161,9 @@
 		daemonise();
 
 	opt = 0;
-	for (iface = ifaces; iface; iface = iface->next) {
-		init_state(iface, argc, argv);
-		if (iface->carrier != LINK_DOWN)
+	TAILQ_FOREACH(ifp, ifaces, next) {
+		init_state(ifp, argc, argv);
+		if (ifp->carrier != LINK_DOWN)
 			opt = 1;
 	}
 
@@ -1198,9 +1178,9 @@
 			ts.tv_sec = 1;
 			ts.tv_nsec = 0;
 			nanosleep(&ts, NULL);
-			for (iface = ifaces; iface; iface = iface->next) {
-				handle_carrier(0, 0, iface->name);
-				if (iface->carrier != LINK_DOWN) {
+			TAILQ_FOREACH(ifp, ifaces, next) {
+				handle_carrier(0, 0, ifp->name);
+				if (ifp->carrier != LINK_DOWN) {
 					opt = 1;
 					break;
 				}
@@ -1208,8 +1188,8 @@
 		}
 		if (options & DHCPCD_MASTER)
 			i = if_options->timeout;
-		else if (ifaces)
-			i = ifaces->options->timeout;
+		else if ((ifp = TAILQ_FIRST(ifaces)))
+			i = ifp->options->timeout;
 		else
 			i = 0;
 		if (opt == 0 &&
@@ -1228,8 +1208,9 @@
 	if_options = NULL;
 
 	sort_interfaces();
-	for (iface = ifaces; iface; iface = iface->next)
-		eloop_timeout_add_sec(0, start_interface, iface);
+	TAILQ_FOREACH(ifp, ifaces, next) {
+		eloop_timeout_add_sec(0, start_interface, ifp);
+	}
 
 	eloop_start(&dhcpcd_sigset);
 	exit(EXIT_SUCCESS);
--- a/dhcpcd.h	Tue Feb 19 11:18:04 2013 +0000
+++ b/dhcpcd.h	Tue Feb 19 13:37:42 2013 +0000
@@ -28,6 +28,7 @@
 #ifndef DHCPCD_H
 #define DHCPCD_H
 
+#include <sys/queue.h>
 #include <sys/socket.h>
 #include <net/if.h>
 
@@ -51,6 +52,7 @@
 #define IF_DATA_MAX	3
 
 struct interface {
+	TAILQ_ENTRY(interface) next;
 	char name[IF_NAMESIZE];
 	unsigned int index;
 	int flags;
@@ -65,8 +67,8 @@
 	char profile[PROFILE_LEN];
 	struct if_options *options;
 	void *if_data[IF_DATA_MAX];
-	struct interface *next;
 };
+extern TAILQ_HEAD(if_head, interface) *ifaces;
 
 extern char vendor[VENDORCLASSID_MAX_LEN];
 extern sigset_t dhcpcd_sigset;
@@ -76,7 +78,6 @@
 extern int ifdc;
 extern char **ifdv;
 extern struct if_options *if_options;
-extern struct interface *ifaces;
 
 pid_t daemonise(void);
 struct interface *find_interface(const char *);
--- a/if-pref.c	Tue Feb 19 11:18:04 2013 +0000
+++ b/if-pref.c	Tue Feb 19 13:37:42 2013 +0000
@@ -79,33 +79,27 @@
 void
 sort_interfaces(void)
 {
-	struct interface *sorted, *ifp, *ifn, *ift;
+	struct if_head sorted;
+	struct interface *ifp, *ift;
 
-	if (!ifaces || !ifaces->next)
+	if (ifaces == NULL ||
+	    (ifp = TAILQ_FIRST(ifaces)) == NULL ||
+	    TAILQ_NEXT(ifp, next) == NULL)
 		return;
-	sorted = ifaces;
-	ifaces = ifaces->next;
-	sorted->next = NULL;
-	for (ifp = ifaces; ifp && (ifn = ifp->next, 1); ifp = ifn) {
-		/* Are we the new head? */
-		if (ifcmp(ifp, sorted) == -1) {
-			ifp->next = sorted;
-			sorted = ifp;
-			continue;
-		}
-		/* Do we fit in the middle? */
-		for (ift = sorted; ift->next; ift = ift->next) {
-			if (ifcmp(ifp, ift->next) == -1) {
-				ifp->next = ift->next;
-				ift->next = ifp;
+
+	TAILQ_INIT(&sorted);
+	TAILQ_REMOVE(ifaces, ifp, next);
+	TAILQ_INSERT_HEAD(&sorted, ifp, next);
+	while ((ifp = TAILQ_FIRST(ifaces))) {
+		TAILQ_REMOVE(ifaces, ifp, next);
+		TAILQ_FOREACH(ift, &sorted, next) {
+			if (ifcmp(ifp, ift) == -1) {
+				TAILQ_INSERT_BEFORE(ift, ifp, next);
 				break;
 			}
 		}
-		/* We must be at the end */
-		if (!ift->next) {
-			ift->next = ifp;
-			ifp->next = NULL;
-		}
+		if (ift == NULL)
+			TAILQ_INSERT_TAIL(&sorted, ifp, next);
 	}
-	ifaces = sorted;
+	TAILQ_CONCAT(ifaces, &sorted, next);
 }
--- a/ipv4.c	Tue Feb 19 11:18:04 2013 +0000
+++ b/ipv4.c	Tue Feb 19 13:37:42 2013 +0000
@@ -469,7 +469,7 @@
 	struct interface *ifp;
 	const struct dhcp_state *state;
 
-	for (ifp = ifaces; ifp; ifp = ifp->next) {
+	TAILQ_FOREACH(ifp, ifaces, next) {
 		state = D_CSTATE(ifp);
 		if (state == NULL || state->new == NULL)
 			continue;
@@ -632,9 +632,10 @@
 
 	if (addr->s_addr == INADDR_ANY)
 		return;
-	for (ifp = ifaces; ifp; ifp = ifp->next)
+	TAILQ_FOREACH(ifp, ifaces, next) {
 		if (strcmp(ifp->name, ifname) == 0)
 			break;
+	}
 	if (ifp == NULL)
 		return;
 
--- a/ipv6.c	Tue Feb 19 11:18:04 2013 +0000
+++ b/ipv6.c	Tue Feb 19 13:37:42 2013 +0000
@@ -479,7 +479,7 @@
 		return;
 
 	TAILQ_INIT(&dnr);
-	for (ifp = ifaces; ifp; ifp = ifp->next) {
+	TAILQ_FOREACH(ifp, ifaces, next) {
 		d6_state = D6_CSTATE(ifp);
 		if (d6_state && d6_state->state == DH6S_BOUND) {
 			TAILQ_FOREACH(addr, &d6_state->addrs, next) {
--- a/ipv6ns.c	Tue Feb 19 11:18:04 2013 +0000
+++ b/ipv6ns.c	Tue Feb 19 13:37:42 2013 +0000
@@ -319,9 +319,10 @@
 		return;
 	}
 
-	for (ifp = ifaces; ifp; ifp = ifp->next)
+	TAILQ_FOREACH(ifp, ifaces, next) {
 		if (ifp->index == (unsigned int)pkt.ipi6_ifindex)
 			break;
+	}
 	if (ifp == NULL) {
 #ifdef DEBUG_NS
 		syslog(LOG_DEBUG, "NA for unexpected interface from %s", sfrom);
--- a/ipv6rs.c	Tue Feb 19 11:18:04 2013 +0000
+++ b/ipv6rs.c	Tue Feb 19 13:37:42 2013 +0000
@@ -473,9 +473,10 @@
 		return;
 	}
 
-	for (ifp = ifaces; ifp; ifp = ifp->next)
+	TAILQ_FOREACH(ifp, ifaces, next) {
 		if (ifp->index == (unsigned int)pkt.ipi6_ifindex)
 			break;
+	}
 	if (ifp == NULL) {
 #ifdef DEBUG_RS
 		syslog(LOG_DEBUG, "RA for unexpected interface from %s", sfrom);
--- a/net.c	Tue Feb 19 11:18:04 2013 +0000
+++ b/net.c	Tue Feb 19 13:37:42 2013 +0000
@@ -199,13 +199,14 @@
 	return retval;
 }
 
-struct interface *
+struct if_head *
 discover_interfaces(int argc, char * const *argv)
 {
 	struct ifaddrs *ifaddrs, *ifa;
 	char *p;
 	int i, sdl_type;
-	struct interface *ifp, *ifs, *ifl;
+	struct if_head *ifs;
+	struct interface *ifp;
 #ifdef __linux__
 	char ifn[IF_NAMESIZE];
 #endif
@@ -227,7 +228,11 @@
 	if (getifaddrs(&ifaddrs) == -1)
 		return NULL;
 
-	ifs = ifl = 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
@@ -241,9 +246,10 @@
 
 		/* It's possible for an interface to have >1 AF_LINK.
 		 * For our purposes, we use the first one. */
-		for (ifp = ifs; ifp; ifp = ifp->next)
+		TAILQ_FOREACH(ifp, ifs, next) {
 			if (strcmp(ifp->name, ifa->ifa_name) == 0)
 				break;
+		}
 		if (ifp)
 			continue;
 		if (argc > 0) {
@@ -409,11 +415,7 @@
 			ifp->metric += 100;
 		}
 
-		if (ifl)
-			ifl->next = ifp; 
-		else
-			ifs = ifp;
-		ifl = ifp;
+		TAILQ_INSERT_TAIL(ifs, ifp, next);
 	}
 	freeifaddrs(ifaddrs);
 
--- a/net.h	Tue Feb 19 11:18:04 2013 +0000
+++ b/net.h	Tue Feb 19 13:37:42 2013 +0000
@@ -97,7 +97,7 @@
 size_t hwaddr_aton(unsigned char *, const char *);
 
 int getifssid(const char *, char *);
-struct interface *discover_interfaces(int, char * const *);
+struct if_head *discover_interfaces(int, char * const *);
 void free_interface(struct interface *);
 int do_mtu(const char *, short int);
 #define get_mtu(iface) do_mtu(iface, 0)
--- a/script.c	Tue Feb 19 11:18:04 2013 +0000
+++ b/script.c	Tue Feb 19 13:37:42 2013 +0000
@@ -259,14 +259,15 @@
 	EMALLOC(6, e);
 	snprintf(env[6], e, "ifmtu=%d", get_mtu(ifp->name));
 	l = e = strlen("interface_order=");
-	for (ifp2 = ifaces; ifp2; ifp2 = ifp2->next)
+	TAILQ_FOREACH(ifp2, ifaces, next) {
 		e += strlen(ifp2->name) + 1;
+	}
 	EMALLOC(7, e);
 	p = env[7];
 	strlcpy(p, "interface_order=", e);
 	e -= l;
 	p += l;
-	for (ifp2 = ifaces; ifp2; ifp2 = ifp2->next) {
+	TAILQ_FOREACH(ifp2, ifaces, next) {
 		l = strlcpy(p, ifp2->name, e);
 		p += l;
 		e -= l;