From ebc90701ac6c1f814c5bd6f3e19f0113ebe06156 Mon Sep 17 00:00:00 2001 From: Qing Li Date: Tue, 12 May 2009 07:41:20 +0000 Subject: [PATCH] This patch adds a host route to an interface address (that is assigned to a non loopback/ppp link types) through the loopback interface. Prior to the new L2/L3 rewrite, this host route is implicitly added by the L2 code during RTM_RESOLVE of that interface address. This host route is deleted when that interface is removed. Reviewed by: kmacy --- sys/netinet/in.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/sys/netinet/in.c b/sys/netinet/in.c index 81803265398ffe..84750a6365b606 100644 --- a/sys/netinet/in.c +++ b/sys/netinet/in.c @@ -45,12 +45,15 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include +#include #include #include #include +#include #include #include @@ -814,6 +817,9 @@ in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin, INIT_VNET_INET(ifp->if_vnet); register u_long i = ntohl(sin->sin_addr.s_addr); struct sockaddr_in oldaddr; + struct rtentry *rt = NULL; + struct rt_addrinfo info; + static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; int s = splimp(), flags = RTF_UP, error = 0; oldaddr = ia->ia_addr; @@ -900,6 +906,29 @@ in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin, if ((error = in_addprefix(ia, flags)) != 0) return (error); + /* + * add a loopback route to self + */ + if (!(ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) { + bzero(&info, sizeof(info)); + info.rti_ifp = V_loif; + info.rti_flags = ia->ia_flags | RTF_HOST | RTF_STATIC; + info.rti_info[RTAX_DST] = (struct sockaddr *)&ia->ia_addr; + info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; + error = rtrequest1_fib(RTM_ADD, &info, &rt, 0); + + if (error == 0 && rt != NULL) { + RT_LOCK(rt); + ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type = + rt->rt_ifp->if_type; + ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index = + rt->rt_ifp->if_index; + RT_REMREF(rt); + RT_UNLOCK(rt); + } else if (error != 0) + log(LOG_INFO, "in_ifinit: insertion failed\n"); + } + return (error); } @@ -979,10 +1008,28 @@ in_scrubprefix(struct in_ifaddr *target) struct in_ifaddr *ia; struct in_addr prefix, mask, p; int error; + struct rt_addrinfo info; + struct sockaddr_dl null_sdl; if ((target->ia_flags & IFA_ROUTE) == 0) return (0); + if (!(target->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) { + bzero(&null_sdl, sizeof(null_sdl)); + null_sdl.sdl_len = sizeof(null_sdl); + null_sdl.sdl_family = AF_LINK; + null_sdl.sdl_type = V_loif->if_type; + null_sdl.sdl_index = V_loif->if_index; + bzero(&info, sizeof(info)); + info.rti_flags = target->ia_flags | RTF_HOST | RTF_STATIC; + info.rti_info[RTAX_DST] = (struct sockaddr *)&target->ia_addr; + info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; + error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0); + + if (error != 0) + log(LOG_INFO, "in_scrubprefix: deletion failed\n"); + } + if (rtinitflags(target)) prefix = target->ia_dstaddr.sin_addr; else { @@ -1136,7 +1183,6 @@ in_purgemaddrs(struct ifnet *ifp) IN_MULTI_UNLOCK(); } -#include #include #include