From 08625885fa23d3f7da2942711e681d3e63bddd33 Mon Sep 17 00:00:00 2001 From: mpi Date: Tue, 22 Apr 2014 12:35:00 +0000 Subject: [PATCH] ifa_ifwithroute() is the only magic place where an AF_LINK sockaddr can be given to ifa_ifwithnet(). Handle this specific case directly and let ifa_ifwithnet() do only one thing: iterate on all the addresses of all the interfaces in a given routing domain to return the most specific matching address. ok mikeb@ --- sys/net/if.c | 20 +++++++------------- sys/net/route.c | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/sys/net/if.c b/sys/net/if.c index 1786279c13b..5f35a6d5a37 100644 --- a/sys/net/if.c +++ b/sys/net/if.c @@ -1,4 +1,4 @@ -/* $OpenBSD: if.c,v 1.285 2014/04/20 11:25:18 claudio Exp $ */ +/* $OpenBSD: if.c,v 1.286 2014/04/22 12:35:00 mpi Exp $ */ /* $NetBSD: if.c,v 1.35 1996/05/07 05:26:04 thorpej Exp $ */ /* @@ -891,27 +891,21 @@ ifa_ifwithdstaddr(struct sockaddr *addr, u_int rdomain) * is most specific found. */ struct ifaddr * -ifa_ifwithnet(struct sockaddr *addr, u_int rdomain) +ifa_ifwithnet(struct sockaddr *sa, u_int rtableid) { struct ifnet *ifp; - struct ifaddr *ifa; - struct ifaddr *ifa_maybe = 0; - u_int af = addr->sa_family; - char *addr_data = addr->sa_data, *cplim; + struct ifaddr *ifa, *ifa_maybe = NULL; + char *cplim, *addr_data = sa->sa_data; + u_int rdomain; - rdomain = rtable_l2(rdomain); - if (af == AF_LINK) { - struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; - if (sdl->sdl_index && (ifp = if_get(sdl->sdl_index)) != NULL) - return (ifp->if_lladdr); - } + rdomain = rtable_l2(rtableid); TAILQ_FOREACH(ifp, &ifnet, if_list) { if (ifp->if_rdomain != rdomain) continue; TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { char *cp, *cp2, *cp3; - if (ifa->ifa_addr->sa_family != af || + if (ifa->ifa_addr->sa_family != sa->sa_family || ifa->ifa_netmask == 0) next: continue; cp = addr_data; diff --git a/sys/net/route.c b/sys/net/route.c index c5f596a861e..6442a7661dc 100644 --- a/sys/net/route.c +++ b/sys/net/route.c @@ -1,4 +1,4 @@ -/* $OpenBSD: route.c,v 1.161 2014/04/11 00:06:30 krw Exp $ */ +/* $OpenBSD: route.c,v 1.162 2014/04/22 12:35:00 mpi Exp $ */ /* $NetBSD: route.c,v 1.14 1996/02/13 22:00:46 christos Exp $ */ /* @@ -650,8 +650,17 @@ ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway, */ ifa = ifa_ifwithdstaddr(gateway, rtableid); } - if (ifa == NULL) - ifa = ifa_ifwithnet(gateway, rtableid); + if (ifa == NULL) { + if (gateway->sa_family == AF_LINK) { + struct sockaddr_dl *sdl = (struct sockaddr_dl *)gateway; + struct ifnet *ifp = if_get(sdl->sdl_index); + + if (ifp != NULL) + ifa = ifp->if_lladdr; + } else { + ifa = ifa_ifwithnet(gateway, rtableid); + } + } if (ifa == NULL) { struct rtentry *rt = rtalloc1(gateway, 0, rtable_l2(rtableid)); if (rt == NULL) -- 2.20.1