changeset 2678:1203b28e5ddc draft

Remove all instances of if_indextoname and add a replacement if_findindex as we should already have the interface at this point.
author Roy Marples <roy@marples.name>
date Thu, 18 Sep 2014 00:35:03 +0000
parents be36e1d2ae43
children c6374f67937d
files dhcpcd.c if-bsd.c if-linux.c if.c if.h
diffstat 5 files changed, 63 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/dhcpcd.c	Tue Sep 16 22:09:42 2014 +0000
+++ b/dhcpcd.c	Thu Sep 18 00:35:03 2014 +0000
@@ -378,7 +378,7 @@
 	if (ifo->options & DHCPCD_IPV6RS) {
 		ra_global = if_checkipv6(ifp->ctx, NULL,
 		    ifp->ctx->options & DHCPCD_IPV6RA_OWN ? 1 : 0);
-		ra_iface = if_checkipv6(ifp->ctx, ifp->name,
+		ra_iface = if_checkipv6(ifp->ctx, ifp,
 		    ifp->options->options & DHCPCD_IPV6RA_OWN ? 1 : 0);
 		if (ra_global == -1 || ra_iface == -1)
 			ifo->options &= ~DHCPCD_IPV6RS;
--- a/if-bsd.c	Tue Sep 16 22:09:42 2014 +0000
+++ b/if-bsd.c	Thu Sep 18 00:35:03 2014 +0000
@@ -769,7 +769,7 @@
 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];
+	char msg[2048], *p, *e, *cp;
 	ssize_t bytes;
 	struct rt_msghdr *rtm;
 	struct if_announcemsghdr *ifan;
@@ -778,6 +778,7 @@
 	struct sockaddr *sa, *rti_info[RTAX_MAX];
 	int len;
 	struct sockaddr_dl sdl;
+	struct interface *ifp;
 #ifdef INET
 	struct rt rt;
 #endif
@@ -816,8 +817,7 @@
 #endif
 		case RTM_IFINFO:
 			ifm = (struct if_msghdr *)(void *)p;
-			memset(ifname, 0, sizeof(ifname));
-			if (!(if_indextoname(ifm->ifm_index, ifname)))
+			if ((ifp = if_findindex(ctx, ifm->ifm_index)) == NULL)
 				break;
 			switch (ifm->ifm_data.ifi_link_state) {
 			case LINK_STATE_DOWN:
@@ -838,7 +838,7 @@
 				break;
 			}
 			dhcpcd_handlecarrier(ctx, len,
-			    (unsigned int)ifm->ifm_flags, ifname);
+			    (unsigned int)ifm->ifm_flags, ifp->name);
 			break;
 		case RTM_DELETE:
 			if (~rtm->rtm_addrs &
@@ -864,7 +864,7 @@
 		case RTM_DELADDR:	/* FALLTHROUGH */
 		case RTM_NEWADDR:
 			ifam = (struct ifa_msghdr *)(void *)p;
-			if (!if_indextoname(ifam->ifam_index, ifname))
+			if ((ifp = if_findindex(ctx, ifam->ifam_index)) == NULL)
 				break;
 			cp = (char *)(void *)(ifam + 1);
 			get_addrs(ifam->ifam_addrs, cp, rti_info);
@@ -881,7 +881,7 @@
 #endif
 				memcpy(&sdl, rti_info[RTAX_IFA],
 				    rti_info[RTAX_IFA]->sa_len);
-				dhcpcd_handlehwaddr(ctx, ifname,
+				dhcpcd_handlehwaddr(ctx, ifp->name,
 				    (const unsigned char*)CLLADDR(&sdl),
 				    sdl.sdl_alen);
 				break;
@@ -892,7 +892,7 @@
 				COPYOUT(rt.net, rti_info[RTAX_NETMASK]);
 				COPYOUT(rt.gate, rti_info[RTAX_BRD]);
 				ipv4_handleifa(ctx, rtm->rtm_type,
-				    NULL, ifname,
+				    NULL, ifp->name,
 				    &rt.dest, &rt.net, &rt.gate);
 				break;
 #endif
@@ -902,13 +902,14 @@
 				    rti_info[RTAX_IFA];
 				ia6 = sin6->sin6_addr;
 				if (rtm->rtm_type == RTM_NEWADDR) {
-					ifa_flags = if_addrflags6(ifname, &ia6);
+					ifa_flags = if_addrflags6(ifp->name,
+					    &ia6);
 					if (ifa_flags == -1)
 						break;
 				} else
 					ifa_flags = 0;
 				ipv6_handleifa(ctx, rtm->rtm_type, NULL,
-				    ifname, &ia6, ifa_flags);
+				    ifp->name, &ia6, ifa_flags);
 				break;
 #endif
 			}
@@ -1048,17 +1049,17 @@
 }
 
 int
-if_checkipv6(struct dhcpcd_ctx *ctx, const char *ifname, int own)
+if_checkipv6(struct dhcpcd_ctx *ctx, const struct interface *ifp, int own)
 {
 	int ra;
 
-	if (ifname) {
+	if (ifp) {
 #ifdef ND6_IFF_OVERRIDE_RTADV
 		int override;
 #endif
 
 #ifdef ND6_IFF_IFDISABLED
-		if (del_if_nd6_flag(ifname, ND6_IFF_IFDISABLED) == -1) {
+		if (del_if_nd6_flag(ifp->name, ND6_IFF_IFDISABLED) == -1) {
 			syslog(LOG_ERR,
 			    "%s: del_if_nd6_flag: ND6_IFF_IFDISABLED: %m",
 			    ifname);
@@ -1067,7 +1068,7 @@
 #endif
 
 #ifdef ND6_IFF_PERFORMNUD
-		if (set_if_nd6_flag(ifname, ND6_IFF_PERFORMNUD) == -1) {
+		if (set_if_nd6_flag(ifp->name, ND6_IFF_PERFORMNUD) == -1) {
 			syslog(LOG_ERR,
 			    "%s: set_if_nd6_flag: ND6_IFF_PERFORMNUD: %m",
 			    ifname);
@@ -1079,24 +1080,24 @@
 		if (own) {
 			int all;
 
-			all = get_if_nd6_flag(ifname, ND6_IFF_AUTO_LINKLOCAL);
+			all = get_if_nd6_flag(ifp->name,ND6_IFF_AUTO_LINKLOCAL);
 			if (all == -1)
 				syslog(LOG_ERR,
 				    "%s: get_if_nd6_flag: "
 				    "ND6_IFF_AUTO_LINKLOCAL: %m",
-				    ifname);
+				    ifp->name);
 			else if (all != 0) {
 				syslog(LOG_DEBUG,
 				    "%s: disabling Kernel IPv6 "
 				    "auto link-local support",
-				    ifname);
-				if (del_if_nd6_flag(ifname,
+				    ifp->name);
+				if (del_if_nd6_flag(ifp->name,
 				    ND6_IFF_AUTO_LINKLOCAL) == -1)
 				{
 					syslog(LOG_ERR,
 					    "%s: del_if_nd6_flag: "
 					    "ND6_IFF_AUTO_LINKLOCAL: %m",
-					    ifname);
+					    ifp->name);
 					return -1;
 				}
 			}
@@ -1104,43 +1105,43 @@
 #endif
 
 #ifdef ND6_IFF_OVERRIDE_RTADV
-		override = get_if_nd6_flag(ifname, ND6_IFF_OVERRIDE_RTADV);
+		override = get_if_nd6_flag(ifp->name, ND6_IFF_OVERRIDE_RTADV);
 		if (override == -1)
 			syslog(LOG_ERR,
 			    "%s: get_if_nd6_flag: ND6_IFF_OVERRIDE_RTADV: %m",
-			    ifname);
+			    ifp->name);
 		else if (override == 0 && !own)
 			return 0;
 #endif
 
 #ifdef ND6_IFF_ACCEPT_RTADV
-		ra = get_if_nd6_flag(ifname, ND6_IFF_ACCEPT_RTADV);
+		ra = get_if_nd6_flag(ifp->name, ND6_IFF_ACCEPT_RTADV);
 		if (ra == -1)
 			syslog(LOG_ERR,
 			    "%s: get_if_nd6_flag: ND6_IFF_ACCEPT_RTADV: %m",
-			    ifname);
+			    ifp->name);
 		else if (ra != 0 && own) {
 			syslog(LOG_DEBUG,
 			    "%s: disabling Kernel IPv6 RA support",
-			    ifname);
-			if (del_if_nd6_flag(ifname, ND6_IFF_ACCEPT_RTADV)
+			    ifp->name);
+			if (del_if_nd6_flag(ifp->name, ND6_IFF_ACCEPT_RTADV)
 			    == -1)
 			{
 				syslog(LOG_ERR,
 				    "%s: del_if_nd6_flag: "
 				    "ND6_IFF_ACCEPT_RTADV: %m",
-				    ifname);
+				    ifp->name);
 				return ra;
 			}
 #ifdef ND6_IFF_OVERRIDE_RTADV
 			if (override == 0 &&
-			    set_if_nd6_flag(ifname, ND6_IFF_OVERRIDE_RTADV)
+			    set_if_nd6_flag(ifp->name, ND6_IFF_OVERRIDE_RTADV)
 			    == -1)
 			{
 				syslog(LOG_ERR,
 				    "%s: set_if_nd6_flag: "
 				    "ND6_IFF_OVERRIDE_RTADV: %m",
-				    ifname);
+				    ifp->name);
 				return ra;
 			}
 #endif
--- a/if-linux.c	Tue Sep 16 22:09:42 2014 +0000
+++ b/if-linux.c	Thu Sep 18 00:35:03 2014 +0000
@@ -405,7 +405,7 @@
     struct nlmsghdr *nlm)
 {
 	size_t len;
-	unsigned int idx, metric;
+	unsigned int metric;
 	struct rtattr *rta;
 	struct rtmsg *rtm;
 	struct rt rt;
@@ -449,9 +449,8 @@
 			    sizeof(rt.gate.s_addr));
 			break;
 		case RTA_OIF:
-			idx = *(unsigned int *)RTA_DATA(rta);
-			if (if_indextoname(idx, ifn))
-				rt.iface = if_find(ctx, ifn);
+			rt.iface = if_findindex(ctx,
+			    *(unsigned int *)RTA_DATA(rta));
 			break;
 		case RTA_PRIORITY:
 			metric = *(unsigned int *)RTA_DATA(rta);
@@ -495,11 +494,11 @@
 		return -1;
 	}
 	ifa = NLMSG_DATA(nlm);
-	if (if_indextoname(ifa->ifa_index, ifn) == NULL)
-		return -1;
-	iface = if_find(ctx, ifn);
-	if (iface == NULL)
+	if (if_findindex(ctx, ifa->ifa_index) == NULL) {
+		/* We don't know about the interface the address is for
+		 * so it's not really an error */
 		return 1;
+	}
 	rta = (struct rtattr *) IFA_RTA(ifa);
 	len = NLMSG_PAYLOAD(nlm, sizeof(*ifa));
 	switch (ifa->ifa_family) {
@@ -1480,7 +1479,7 @@
 }
 
 static int
-if_disable_autolinklocal(struct dhcpcd_ctx *ctx, const char *ifname)
+if_disable_autolinklocal(struct dhcpcd_ctx *ctx, int ifindex)
 {
 	struct nlml nlm;
 	struct rtattr *afs, *afs6;
@@ -1490,7 +1489,7 @@
 	nlm.hdr.nlmsg_type = RTM_NEWLINK;
 	nlm.hdr.nlmsg_flags = NLM_F_REQUEST;
 	nlm.i.ifi_family = AF_INET6;
-	nlm.i.ifi_index = (int)if_nametoindex(ifname);
+	nlm.i.ifi_index = ifindex;
 	afs = add_attr_nest(&nlm.hdr, sizeof(nlm), IFLA_AF_SPEC);
 	afs6 = add_attr_nest(&nlm.hdr, sizeof(nlm), AF_INET6);
 	add_attr_8(&nlm.hdr, sizeof(nlm), IFLA_INET6_ADDR_GEN_MODE,
@@ -1504,18 +1503,21 @@
 static const char *prefix = "/proc/sys/net/ipv6/conf";
 
 int
-if_checkipv6(struct dhcpcd_ctx *ctx, const char *ifname, int own)
+if_checkipv6(struct dhcpcd_ctx *ctx, const struct interface *ifp, int own)
 {
+	const char *ifname;
 	int ra;
 	char path[256];
 
-	if (ifname == NULL)
+	if (ifp == NULL)
 		ifname = "all";
 	else if (own) {
-		if (if_disable_autolinklocal(ctx, ifname) == -1)
+		if (if_disable_autolinklocal(ctx, ifp->index) == -1)
 			syslog(LOG_DEBUG, "%s: if_disable_autolinklocal: %m",
 			    ifname);
 	}
+	if (ifp)
+		ifname = ifp->name;
 
 	snprintf(path, sizeof(path), "%s/%s/autoconf", prefix, ifname);
 	ra = check_proc_int(path);
--- a/if.c	Tue Sep 16 22:09:42 2014 +0000
+++ b/if.c	Thu Sep 18 00:35:03 2014 +0000
@@ -479,7 +479,7 @@
 }
 
 struct interface *
-if_find(struct dhcpcd_ctx *ctx, const char *ifname)
+if_findindexname(struct dhcpcd_ctx *ctx, unsigned int idx, const char *name)
 {
 	struct interface *ifp;
 
@@ -487,13 +487,28 @@
 		TAILQ_FOREACH(ifp, ctx->ifaces, next) {
 			if ((ifp->options == NULL ||
 			    !(ifp->options->options & DHCPCD_PFXDLGONLY)) &&
-			    strcmp(ifp->name, ifname) == 0)
+			    ((name && strcmp(ifp->name, name) == 0) ||
+			    (!name && ifp->index == idx)))
 				return ifp;
 		}
 	}
 	return NULL;
 }
 
+struct interface *
+if_find(struct dhcpcd_ctx *ctx, const char *name)
+{
+
+	return if_findindexname(ctx, 0, name);
+}
+
+struct interface *
+if_findindex(struct dhcpcd_ctx *ctx, unsigned int idx)
+{
+
+	return if_findindexname(ctx, idx, NULL);
+}
+
 int
 if_domtu(const char *ifname, short int mtu)
 {
--- a/if.h	Tue Sep 16 22:09:42 2014 +0000
+++ b/if.h	Thu Sep 18 00:35:03 2014 +0000
@@ -94,6 +94,7 @@
 #define if_up(ifp) if_setflag((ifp), (IFF_UP | IFF_RUNNING))
 struct if_head *if_discover(struct dhcpcd_ctx *, int, char * const *);
 struct interface *if_find(struct dhcpcd_ctx *, const char *);
+struct interface *if_findindex(struct dhcpcd_ctx *, unsigned int);
 void if_free(struct interface *);
 int if_domtu(const char *, short int);
 #define if_getmtu(iface) if_domtu(iface, 0)
@@ -131,7 +132,7 @@
 #endif
 
 #ifdef INET6
-int if_checkipv6(struct dhcpcd_ctx *ctx, const char *, int);
+int if_checkipv6(struct dhcpcd_ctx *ctx, const struct interface *, int);
 int if_nd6reachable(const char *ifname, struct in6_addr *addr);
 
 int if_address6(const struct ipv6_addr *, int);