1c398230bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1980, 1986, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 2942e9e16dSRuslan Ermilov * @(#)route.c 8.3.1.1 (Berkeley) 2/23/95 30c3aac50fSPeter Wemm * $FreeBSD$ 31df8bae1dSRodney W. Grimes */ 32df8bae1dSRodney W. Grimes 331d5e9e22SEivind Eklund #include "opt_inet.h" 344bd49128SPeter Wemm #include "opt_mrouting.h" 354bd49128SPeter Wemm 36df8bae1dSRodney W. Grimes #include <sys/param.h> 37df8bae1dSRodney W. Grimes #include <sys/systm.h> 384d1d4912SBruce Evans #include <sys/malloc.h> 39df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 40df8bae1dSRodney W. Grimes #include <sys/socket.h> 41df8bae1dSRodney W. Grimes #include <sys/domain.h> 42cb64988fSLuoqi Chen #include <sys/kernel.h> 43df8bae1dSRodney W. Grimes 44df8bae1dSRodney W. Grimes #include <net/if.h> 45df8bae1dSRodney W. Grimes #include <net/route.h> 46df8bae1dSRodney W. Grimes 47df8bae1dSRodney W. Grimes #include <netinet/in.h> 48b5e8ce9fSBruce Evans #include <netinet/ip_mroute.h> 49df8bae1dSRodney W. Grimes 502dc1d581SAndre Oppermann #include <vm/uma.h> 512dc1d581SAndre Oppermann 52f708ef1bSPoul-Henning Kamp static struct rtstat rtstat; 5328f8db14SBruce Evans struct radix_node_head *rt_tables[AF_MAX+1]; 5428f8db14SBruce Evans 55f708ef1bSPoul-Henning Kamp static int rttrash; /* routes not in table but not freed */ 56df8bae1dSRodney W. Grimes 57929ddbbbSAlfred Perlstein static void rt_maskedcopy(struct sockaddr *, 58929ddbbbSAlfred Perlstein struct sockaddr *, struct sockaddr *); 59929ddbbbSAlfred Perlstein static void rtable_init(void **); 60f708ef1bSPoul-Henning Kamp 61d6941ce9SLuigi Rizzo /* compare two sockaddr structures */ 62d6941ce9SLuigi Rizzo #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0) 63d6941ce9SLuigi Rizzo 64d6941ce9SLuigi Rizzo /* 65d6941ce9SLuigi Rizzo * Convert a 'struct radix_node *' to a 'struct rtentry *'. 66d6941ce9SLuigi Rizzo * The operation can be done safely (in this code) because a 67d6941ce9SLuigi Rizzo * 'struct rtentry' starts with two 'struct radix_node''s, the first 68d6941ce9SLuigi Rizzo * one representing leaf nodes in the routing tree, which is 69d6941ce9SLuigi Rizzo * what the code in radix.c passes us as a 'struct radix_node'. 70d6941ce9SLuigi Rizzo * 71d6941ce9SLuigi Rizzo * But because there are a lot of assumptions in this conversion, 72d6941ce9SLuigi Rizzo * do not cast explicitly, but always use the macro below. 73d6941ce9SLuigi Rizzo */ 74d6941ce9SLuigi Rizzo #define RNTORT(p) ((struct rtentry *)(p)) 75d6941ce9SLuigi Rizzo 76f708ef1bSPoul-Henning Kamp static void 77d1dd20beSSam Leffler rtable_init(void **table) 78df8bae1dSRodney W. Grimes { 79df8bae1dSRodney W. Grimes struct domain *dom; 80df8bae1dSRodney W. Grimes for (dom = domains; dom; dom = dom->dom_next) 81df8bae1dSRodney W. Grimes if (dom->dom_rtattach) 82df8bae1dSRodney W. Grimes dom->dom_rtattach(&table[dom->dom_family], 83df8bae1dSRodney W. Grimes dom->dom_rtoffset); 84df8bae1dSRodney W. Grimes } 85df8bae1dSRodney W. Grimes 862dc1d581SAndre Oppermann static uma_zone_t rtzone; /* Routing table UMA zone. */ 872dc1d581SAndre Oppermann 882eb5613fSLuigi Rizzo static void 892eb5613fSLuigi Rizzo route_init(void) 90df8bae1dSRodney W. Grimes { 912dc1d581SAndre Oppermann rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), NULL, NULL, 922dc1d581SAndre Oppermann NULL, NULL, UMA_ALIGN_PTR, 0); 93df8bae1dSRodney W. Grimes rn_init(); /* initialize all zeroes, all ones, mask table */ 94df8bae1dSRodney W. Grimes rtable_init((void **)rt_tables); 95df8bae1dSRodney W. Grimes } 96df8bae1dSRodney W. Grimes 97df8bae1dSRodney W. Grimes /* 98df8bae1dSRodney W. Grimes * Packet routing routines. 99df8bae1dSRodney W. Grimes */ 100df8bae1dSRodney W. Grimes void 101d1dd20beSSam Leffler rtalloc(struct route *ro) 102df8bae1dSRodney W. Grimes { 10368f956b8SJohn Polstra rtalloc_ign(ro, 0UL); 104df8bae1dSRodney W. Grimes } 105df8bae1dSRodney W. Grimes 106652082e6SGarrett Wollman void 107d1dd20beSSam Leffler rtalloc_ign(struct route *ro, u_long ignore) 108652082e6SGarrett Wollman { 10968f956b8SJohn Polstra struct rtentry *rt; 11068f956b8SJohn Polstra 11168f956b8SJohn Polstra if ((rt = ro->ro_rt) != NULL) { 11268f956b8SJohn Polstra if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP) 11368f956b8SJohn Polstra return; 11468f956b8SJohn Polstra RTFREE(rt); 11566810dd0SYoshinobu Inoue ro->ro_rt = NULL; 11668f956b8SJohn Polstra } 117652082e6SGarrett Wollman ro->ro_rt = rtalloc1(&ro->ro_dst, 1, ignore); 118d1dd20beSSam Leffler if (ro->ro_rt) 119d1dd20beSSam Leffler RT_UNLOCK(ro->ro_rt); 120652082e6SGarrett Wollman } 121652082e6SGarrett Wollman 122b0a76b88SJulian Elischer /* 123b0a76b88SJulian Elischer * Look up the route that matches the address given 124b0a76b88SJulian Elischer * Or, at least try.. Create a cloned route if needed. 125d1dd20beSSam Leffler * 126d1dd20beSSam Leffler * The returned route, if any, is locked. 127b0a76b88SJulian Elischer */ 128df8bae1dSRodney W. Grimes struct rtentry * 129d1dd20beSSam Leffler rtalloc1(struct sockaddr *dst, int report, u_long ignflags) 130df8bae1dSRodney W. Grimes { 131d1dd20beSSam Leffler struct radix_node_head *rnh = rt_tables[dst->sa_family]; 132d1dd20beSSam Leffler struct rtentry *rt; 133d1dd20beSSam Leffler struct radix_node *rn; 134d1dd20beSSam Leffler struct rtentry *newrt; 135df8bae1dSRodney W. Grimes struct rt_addrinfo info; 136995add1aSGarrett Wollman u_long nflags; 137d1dd20beSSam Leffler int err = 0, msgtype = RTM_MISS; 138df8bae1dSRodney W. Grimes 13985911824SLuigi Rizzo newrt = NULL; 1403e6a836eSSam Leffler bzero(&info, sizeof(info)); 141b0a76b88SJulian Elischer /* 142b0a76b88SJulian Elischer * Look up the address in the table for that Address Family 143b0a76b88SJulian Elischer */ 144956b0b65SJeffrey Hsu if (rnh == NULL) { 145956b0b65SJeffrey Hsu rtstat.rts_unreach++; 146956b0b65SJeffrey Hsu goto miss2; 147956b0b65SJeffrey Hsu } 148956b0b65SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rnh); 149d1dd20beSSam Leffler if ((rn = rnh->rnh_matchaddr(dst, rnh)) && 150d1dd20beSSam Leffler (rn->rn_flags & RNF_ROOT) == 0) { 151b0a76b88SJulian Elischer /* 152b0a76b88SJulian Elischer * If we find it and it's not the root node, then 153b0a76b88SJulian Elischer * get a refernce on the rtentry associated. 154b0a76b88SJulian Elischer */ 155d6941ce9SLuigi Rizzo newrt = rt = RNTORT(rn); 156995add1aSGarrett Wollman nflags = rt->rt_flags & ~ignflags; 15726d02ca7SAndre Oppermann if (report && (nflags & RTF_CLONING)) { 158b0a76b88SJulian Elischer /* 159b0a76b88SJulian Elischer * We are apparently adding (report = 0 in delete). 160b0a76b88SJulian Elischer * If it requires that it be cloned, do so. 161b0a76b88SJulian Elischer * (This implies it wasn't a HOST route.) 162b0a76b88SJulian Elischer */ 16385911824SLuigi Rizzo err = rtrequest(RTM_RESOLVE, dst, NULL, 16485911824SLuigi Rizzo NULL, 0, &newrt); 165df8bae1dSRodney W. Grimes if (err) { 166b0a76b88SJulian Elischer /* 167b0a76b88SJulian Elischer * If the cloning didn't succeed, maybe 168b0a76b88SJulian Elischer * what we have will do. Return that. 169b0a76b88SJulian Elischer */ 170d1dd20beSSam Leffler newrt = rt; /* existing route */ 171d1dd20beSSam Leffler RT_LOCK(newrt); 1727138d65cSSam Leffler RT_ADDREF(newrt); 173df8bae1dSRodney W. Grimes goto miss; 174df8bae1dSRodney W. Grimes } 175d1dd20beSSam Leffler KASSERT(newrt, ("no route and no error")); 176d1dd20beSSam Leffler RT_LOCK(newrt); 177d1dd20beSSam Leffler if (newrt->rt_flags & RTF_XRESOLVE) { 178b0a76b88SJulian Elischer /* 179b0a76b88SJulian Elischer * If the new route specifies it be 180b0a76b88SJulian Elischer * externally resolved, then go do that. 181b0a76b88SJulian Elischer */ 182df8bae1dSRodney W. Grimes msgtype = RTM_RESOLVE; 183df8bae1dSRodney W. Grimes goto miss; 184df8bae1dSRodney W. Grimes } 1858071913dSRuslan Ermilov /* Inform listeners of the new route. */ 186d1dd20beSSam Leffler info.rti_info[RTAX_DST] = rt_key(newrt); 187d1dd20beSSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(newrt); 188d1dd20beSSam Leffler info.rti_info[RTAX_GATEWAY] = newrt->rt_gateway; 189d1dd20beSSam Leffler if (newrt->rt_ifp != NULL) { 1908071913dSRuslan Ermilov info.rti_info[RTAX_IFP] = 1914a0d6638SRuslan Ermilov newrt->rt_ifp->if_addr->ifa_addr; 192d1dd20beSSam Leffler info.rti_info[RTAX_IFA] = newrt->rt_ifa->ifa_addr; 1938071913dSRuslan Ermilov } 194d1dd20beSSam Leffler rt_missmsg(RTM_ADD, &info, newrt->rt_flags, 0); 195d1dd20beSSam Leffler } else { 196d1dd20beSSam Leffler KASSERT(rt == newrt, ("locking wrong route")); 197d1dd20beSSam Leffler RT_LOCK(newrt); 1987138d65cSSam Leffler RT_ADDREF(newrt); 199d1dd20beSSam Leffler } 200956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 201df8bae1dSRodney W. Grimes } else { 202b0a76b88SJulian Elischer /* 203b0a76b88SJulian Elischer * Either we hit the root or couldn't find any match, 204b0a76b88SJulian Elischer * Which basically means 205b0a76b88SJulian Elischer * "caint get there frm here" 206b0a76b88SJulian Elischer */ 207df8bae1dSRodney W. Grimes rtstat.rts_unreach++; 208956b0b65SJeffrey Hsu miss: 209956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 210956b0b65SJeffrey Hsu miss2: if (report) { 211b0a76b88SJulian Elischer /* 212b0a76b88SJulian Elischer * If required, report the failure to the supervising 213b0a76b88SJulian Elischer * Authorities. 214b0a76b88SJulian Elischer * For a delete, this is not an error. (report == 0) 215b0a76b88SJulian Elischer */ 216df8bae1dSRodney W. Grimes info.rti_info[RTAX_DST] = dst; 217df8bae1dSRodney W. Grimes rt_missmsg(msgtype, &info, 0, err); 218df8bae1dSRodney W. Grimes } 219df8bae1dSRodney W. Grimes } 220d1dd20beSSam Leffler if (newrt) 221d1dd20beSSam Leffler RT_LOCK_ASSERT(newrt); 222df8bae1dSRodney W. Grimes return (newrt); 223df8bae1dSRodney W. Grimes } 224df8bae1dSRodney W. Grimes 225499676dfSJulian Elischer /* 226499676dfSJulian Elischer * Remove a reference count from an rtentry. 227499676dfSJulian Elischer * If the count gets low enough, take it out of the routing table 228499676dfSJulian Elischer */ 229df8bae1dSRodney W. Grimes void 230d1dd20beSSam Leffler rtfree(struct rtentry *rt) 231df8bae1dSRodney W. Grimes { 23285911824SLuigi Rizzo struct radix_node_head *rnh; 233df8bae1dSRodney W. Grimes 23485911824SLuigi Rizzo /* XXX the NULL checks are probably useless */ 23585911824SLuigi Rizzo if (rt == NULL) 23685911824SLuigi Rizzo panic("rtfree: NULL rt"); 23785911824SLuigi Rizzo rnh = rt_tables[rt_key(rt)->sa_family]; 23885911824SLuigi Rizzo if (rnh == NULL) 23985911824SLuigi Rizzo panic("rtfree: NULL rnh"); 240499676dfSJulian Elischer 241d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 242d1dd20beSSam Leffler 243499676dfSJulian Elischer /* 244499676dfSJulian Elischer * decrement the reference count by one and if it reaches 0, 245499676dfSJulian Elischer * and there is a close function defined, call the close function 246499676dfSJulian Elischer */ 2477138d65cSSam Leffler RT_REMREF(rt); 2487138d65cSSam Leffler if (rt->rt_refcnt > 0) 249d1dd20beSSam Leffler goto done; 2509c63e9dbSSam Leffler 2519c63e9dbSSam Leffler /* 2529c63e9dbSSam Leffler * On last reference give the "close method" a chance 2539c63e9dbSSam Leffler * to cleanup private state. This also permits (for 2549c63e9dbSSam Leffler * IPv4 and IPv6) a chance to decide if the routing table 2559c63e9dbSSam Leffler * entry should be purged immediately or at a later time. 2569c63e9dbSSam Leffler * When an immediate purge is to happen the close routine 2579c63e9dbSSam Leffler * typically calls rtexpunge which clears the RTF_UP flag 2589c63e9dbSSam Leffler * on the entry so that the code below reclaims the storage. 2599c63e9dbSSam Leffler */ 260d1dd20beSSam Leffler if (rt->rt_refcnt == 0 && rnh->rnh_close) 2615c2dae8eSGarrett Wollman rnh->rnh_close((struct radix_node *)rt, rnh); 262499676dfSJulian Elischer 263499676dfSJulian Elischer /* 264499676dfSJulian Elischer * If we are no longer "up" (and ref == 0) 265499676dfSJulian Elischer * then we can free the resources associated 266499676dfSJulian Elischer * with the route. 267499676dfSJulian Elischer */ 268d1dd20beSSam Leffler if ((rt->rt_flags & RTF_UP) == 0) { 269df8bae1dSRodney W. Grimes if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 270df8bae1dSRodney W. Grimes panic ("rtfree 2"); 271499676dfSJulian Elischer /* 272499676dfSJulian Elischer * the rtentry must have been removed from the routing table 273499676dfSJulian Elischer * so it is represented in rttrash.. remove that now. 274499676dfSJulian Elischer */ 275df8bae1dSRodney W. Grimes rttrash--; 276499676dfSJulian Elischer #ifdef DIAGNOSTIC 277df8bae1dSRodney W. Grimes if (rt->rt_refcnt < 0) { 278623ae52eSPoul-Henning Kamp printf("rtfree: %p not freed (neg refs)\n", rt); 279d1dd20beSSam Leffler goto done; 280df8bae1dSRodney W. Grimes } 281499676dfSJulian Elischer #endif 282499676dfSJulian Elischer /* 283499676dfSJulian Elischer * release references on items we hold them on.. 284499676dfSJulian Elischer * e.g other routes and ifaddrs. 285499676dfSJulian Elischer */ 28619fc74fbSJeffrey Hsu if (rt->rt_ifa) 28719fc74fbSJeffrey Hsu IFAFREE(rt->rt_ifa); 288d1dd20beSSam Leffler rt->rt_parent = NULL; /* NB: no refcnt on parent */ 289499676dfSJulian Elischer 290499676dfSJulian Elischer /* 291499676dfSJulian Elischer * The key is separatly alloc'd so free it (see rt_setgate()). 292499676dfSJulian Elischer * This also frees the gateway, as they are always malloc'd 293499676dfSJulian Elischer * together. 294499676dfSJulian Elischer */ 295df8bae1dSRodney W. Grimes Free(rt_key(rt)); 296499676dfSJulian Elischer 297499676dfSJulian Elischer /* 298499676dfSJulian Elischer * and the rtentry itself of course 299499676dfSJulian Elischer */ 300d1dd20beSSam Leffler RT_LOCK_DESTROY(rt); 3012dc1d581SAndre Oppermann uma_zfree(rtzone, rt); 302d1dd20beSSam Leffler return; 303df8bae1dSRodney W. Grimes } 304d1dd20beSSam Leffler done: 305d1dd20beSSam Leffler RT_UNLOCK(rt); 306df8bae1dSRodney W. Grimes } 307df8bae1dSRodney W. Grimes 308df8bae1dSRodney W. Grimes 309df8bae1dSRodney W. Grimes /* 310df8bae1dSRodney W. Grimes * Force a routing table entry to the specified 311df8bae1dSRodney W. Grimes * destination to go through the given gateway. 312df8bae1dSRodney W. Grimes * Normally called as a result of a routing redirect 313df8bae1dSRodney W. Grimes * message from the network layer. 314df8bae1dSRodney W. Grimes */ 31526f9a767SRodney W. Grimes void 316d1dd20beSSam Leffler rtredirect(struct sockaddr *dst, 317d1dd20beSSam Leffler struct sockaddr *gateway, 318d1dd20beSSam Leffler struct sockaddr *netmask, 319d1dd20beSSam Leffler int flags, 320d1dd20beSSam Leffler struct sockaddr *src) 321df8bae1dSRodney W. Grimes { 3228071913dSRuslan Ermilov struct rtentry *rt; 323df8bae1dSRodney W. Grimes int error = 0; 32485911824SLuigi Rizzo short *stat = NULL; 325df8bae1dSRodney W. Grimes struct rt_addrinfo info; 326df8bae1dSRodney W. Grimes struct ifaddr *ifa; 327df8bae1dSRodney W. Grimes 328df8bae1dSRodney W. Grimes /* verify the gateway is directly reachable */ 32985911824SLuigi Rizzo if ((ifa = ifa_ifwithnet(gateway)) == NULL) { 330df8bae1dSRodney W. Grimes error = ENETUNREACH; 331df8bae1dSRodney W. Grimes goto out; 332df8bae1dSRodney W. Grimes } 333d1dd20beSSam Leffler rt = rtalloc1(dst, 0, 0UL); /* NB: rt is locked */ 334df8bae1dSRodney W. Grimes /* 335df8bae1dSRodney W. Grimes * If the redirect isn't from our current router for this dst, 336df8bae1dSRodney W. Grimes * it's either old or wrong. If it redirects us to ourselves, 337df8bae1dSRodney W. Grimes * we have a routing loop, perhaps as a result of an interface 338df8bae1dSRodney W. Grimes * going down recently. 339df8bae1dSRodney W. Grimes */ 340df8bae1dSRodney W. Grimes if (!(flags & RTF_DONE) && rt && 341956b0b65SJeffrey Hsu (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) 342df8bae1dSRodney W. Grimes error = EINVAL; 343df8bae1dSRodney W. Grimes else if (ifa_ifwithaddr(gateway)) 344df8bae1dSRodney W. Grimes error = EHOSTUNREACH; 345df8bae1dSRodney W. Grimes if (error) 346df8bae1dSRodney W. Grimes goto done; 347df8bae1dSRodney W. Grimes /* 348df8bae1dSRodney W. Grimes * Create a new entry if we just got back a wildcard entry 349df8bae1dSRodney W. Grimes * or the the lookup failed. This is necessary for hosts 350df8bae1dSRodney W. Grimes * which use routing redirects generated by smart gateways 351df8bae1dSRodney W. Grimes * to dynamically build the routing tables. 352df8bae1dSRodney W. Grimes */ 35385911824SLuigi Rizzo if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2)) 354df8bae1dSRodney W. Grimes goto create; 355df8bae1dSRodney W. Grimes /* 356df8bae1dSRodney W. Grimes * Don't listen to the redirect if it's 357df8bae1dSRodney W. Grimes * for a route to an interface. 358df8bae1dSRodney W. Grimes */ 359df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_GATEWAY) { 360df8bae1dSRodney W. Grimes if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) { 361df8bae1dSRodney W. Grimes /* 362df8bae1dSRodney W. Grimes * Changing from route to net => route to host. 363df8bae1dSRodney W. Grimes * Create new route, rather than smashing route to net. 364df8bae1dSRodney W. Grimes */ 365df8bae1dSRodney W. Grimes create: 3668071913dSRuslan Ermilov if (rt) 3678071913dSRuslan Ermilov rtfree(rt); 368df8bae1dSRodney W. Grimes flags |= RTF_GATEWAY | RTF_DYNAMIC; 3698071913dSRuslan Ermilov bzero((caddr_t)&info, sizeof(info)); 3708071913dSRuslan Ermilov info.rti_info[RTAX_DST] = dst; 3718071913dSRuslan Ermilov info.rti_info[RTAX_GATEWAY] = gateway; 3728071913dSRuslan Ermilov info.rti_info[RTAX_NETMASK] = netmask; 3738071913dSRuslan Ermilov info.rti_ifa = ifa; 3748071913dSRuslan Ermilov info.rti_flags = flags; 3758071913dSRuslan Ermilov rt = NULL; 3768071913dSRuslan Ermilov error = rtrequest1(RTM_ADD, &info, &rt); 377d1dd20beSSam Leffler if (rt != NULL) { 3784de5d90cSSam Leffler RT_LOCK(rt); 3798071913dSRuslan Ermilov flags = rt->rt_flags; 380d1dd20beSSam Leffler } 381df8bae1dSRodney W. Grimes stat = &rtstat.rts_dynamic; 382df8bae1dSRodney W. Grimes } else { 383df8bae1dSRodney W. Grimes /* 384df8bae1dSRodney W. Grimes * Smash the current notion of the gateway to 385df8bae1dSRodney W. Grimes * this destination. Should check about netmask!!! 386df8bae1dSRodney W. Grimes */ 387df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_MODIFIED; 388df8bae1dSRodney W. Grimes flags |= RTF_MODIFIED; 389df8bae1dSRodney W. Grimes stat = &rtstat.rts_newgateway; 390499676dfSJulian Elischer /* 391499676dfSJulian Elischer * add the key and gateway (in one malloc'd chunk). 392499676dfSJulian Elischer */ 393df8bae1dSRodney W. Grimes rt_setgate(rt, rt_key(rt), gateway); 394df8bae1dSRodney W. Grimes } 395df8bae1dSRodney W. Grimes } else 396df8bae1dSRodney W. Grimes error = EHOSTUNREACH; 397df8bae1dSRodney W. Grimes done: 398d1dd20beSSam Leffler if (rt) 399df8bae1dSRodney W. Grimes rtfree(rt); 400df8bae1dSRodney W. Grimes out: 401df8bae1dSRodney W. Grimes if (error) 402df8bae1dSRodney W. Grimes rtstat.rts_badredirect++; 403df8bae1dSRodney W. Grimes else if (stat != NULL) 404df8bae1dSRodney W. Grimes (*stat)++; 405df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 406df8bae1dSRodney W. Grimes info.rti_info[RTAX_DST] = dst; 407df8bae1dSRodney W. Grimes info.rti_info[RTAX_GATEWAY] = gateway; 408df8bae1dSRodney W. Grimes info.rti_info[RTAX_NETMASK] = netmask; 409df8bae1dSRodney W. Grimes info.rti_info[RTAX_AUTHOR] = src; 410df8bae1dSRodney W. Grimes rt_missmsg(RTM_REDIRECT, &info, flags, error); 411df8bae1dSRodney W. Grimes } 412df8bae1dSRodney W. Grimes 413df8bae1dSRodney W. Grimes /* 414df8bae1dSRodney W. Grimes * Routing table ioctl interface. 415df8bae1dSRodney W. Grimes */ 416df8bae1dSRodney W. Grimes int 417d1dd20beSSam Leffler rtioctl(u_long req, caddr_t data) 418df8bae1dSRodney W. Grimes { 4195090559bSChristian S.J. Peron 4205090559bSChristian S.J. Peron /* 4215090559bSChristian S.J. Peron * If more ioctl commands are added here, make sure the proper 4225090559bSChristian S.J. Peron * super-user checks are being performed because it is possible for 4235090559bSChristian S.J. Peron * prison-root to make it this far if raw sockets have been enabled 4245090559bSChristian S.J. Peron * in jails. 4255090559bSChristian S.J. Peron */ 426623ae52eSPoul-Henning Kamp #ifdef INET 427f0068c4aSGarrett Wollman /* Multicast goop, grrr... */ 428bbb4330bSLuigi Rizzo return mrt_ioctl ? mrt_ioctl(req, data) : EOPNOTSUPP; 429623ae52eSPoul-Henning Kamp #else /* INET */ 430623ae52eSPoul-Henning Kamp return ENXIO; 431623ae52eSPoul-Henning Kamp #endif /* INET */ 432df8bae1dSRodney W. Grimes } 433df8bae1dSRodney W. Grimes 434df8bae1dSRodney W. Grimes struct ifaddr * 435d1dd20beSSam Leffler ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway) 436df8bae1dSRodney W. Grimes { 437df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 438e034e82cSQing Li int not_found = 0; 439d1dd20beSSam Leffler 440df8bae1dSRodney W. Grimes if ((flags & RTF_GATEWAY) == 0) { 441df8bae1dSRodney W. Grimes /* 442df8bae1dSRodney W. Grimes * If we are adding a route to an interface, 443df8bae1dSRodney W. Grimes * and the interface is a pt to pt link 444df8bae1dSRodney W. Grimes * we should search for the destination 445df8bae1dSRodney W. Grimes * as our clue to the interface. Otherwise 446df8bae1dSRodney W. Grimes * we can use the local address. 447df8bae1dSRodney W. Grimes */ 44885911824SLuigi Rizzo ifa = NULL; 44985911824SLuigi Rizzo if (flags & RTF_HOST) 450df8bae1dSRodney W. Grimes ifa = ifa_ifwithdstaddr(dst); 45185911824SLuigi Rizzo if (ifa == NULL) 452df8bae1dSRodney W. Grimes ifa = ifa_ifwithaddr(gateway); 453df8bae1dSRodney W. Grimes } else { 454df8bae1dSRodney W. Grimes /* 455df8bae1dSRodney W. Grimes * If we are adding a route to a remote net 456df8bae1dSRodney W. Grimes * or host, the gateway may still be on the 457df8bae1dSRodney W. Grimes * other end of a pt to pt link. 458df8bae1dSRodney W. Grimes */ 459df8bae1dSRodney W. Grimes ifa = ifa_ifwithdstaddr(gateway); 460df8bae1dSRodney W. Grimes } 46185911824SLuigi Rizzo if (ifa == NULL) 462df8bae1dSRodney W. Grimes ifa = ifa_ifwithnet(gateway); 46385911824SLuigi Rizzo if (ifa == NULL) { 464ffdc316dSRuslan Ermilov struct rtentry *rt = rtalloc1(gateway, 0, 0UL); 46585911824SLuigi Rizzo if (rt == NULL) 46685911824SLuigi Rizzo return (NULL); 467e034e82cSQing Li /* 468e034e82cSQing Li * dismiss a gateway that is reachable only 469e034e82cSQing Li * through the default router 470e034e82cSQing Li */ 471e034e82cSQing Li switch (gateway->sa_family) { 472e034e82cSQing Li case AF_INET: 473e034e82cSQing Li if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY) 474e034e82cSQing Li not_found = 1; 475e034e82cSQing Li break; 476e034e82cSQing Li case AF_INET6: 477e034e82cSQing Li if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr)) 478e034e82cSQing Li not_found = 1; 479e034e82cSQing Li break; 480e034e82cSQing Li default: 481e034e82cSQing Li break; 482e034e82cSQing Li } 4837138d65cSSam Leffler RT_REMREF(rt); 484d1dd20beSSam Leffler RT_UNLOCK(rt); 485e034e82cSQing Li if (not_found) 486e034e82cSQing Li return (NULL); 48785911824SLuigi Rizzo if ((ifa = rt->rt_ifa) == NULL) 48885911824SLuigi Rizzo return (NULL); 489df8bae1dSRodney W. Grimes } 490df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family != dst->sa_family) { 491df8bae1dSRodney W. Grimes struct ifaddr *oifa = ifa; 492df8bae1dSRodney W. Grimes ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 49385911824SLuigi Rizzo if (ifa == NULL) 494df8bae1dSRodney W. Grimes ifa = oifa; 495df8bae1dSRodney W. Grimes } 496df8bae1dSRodney W. Grimes return (ifa); 497df8bae1dSRodney W. Grimes } 498df8bae1dSRodney W. Grimes 49985911824SLuigi Rizzo static walktree_f_t rt_fixdelete; 50085911824SLuigi Rizzo static walktree_f_t rt_fixchange; 501cd02a0b7SGarrett Wollman 502cd02a0b7SGarrett Wollman struct rtfc_arg { 503cd02a0b7SGarrett Wollman struct rtentry *rt0; 504cd02a0b7SGarrett Wollman struct radix_node_head *rnh; 505cd02a0b7SGarrett Wollman }; 50618e1f1f1SGarrett Wollman 507b0a76b88SJulian Elischer /* 508b0a76b88SJulian Elischer * Do appropriate manipulations of a routing tree given 509b0a76b88SJulian Elischer * all the bits of info needed 510b0a76b88SJulian Elischer */ 511df8bae1dSRodney W. Grimes int 512d1dd20beSSam Leffler rtrequest(int req, 513d1dd20beSSam Leffler struct sockaddr *dst, 514d1dd20beSSam Leffler struct sockaddr *gateway, 515d1dd20beSSam Leffler struct sockaddr *netmask, 516d1dd20beSSam Leffler int flags, 517d1dd20beSSam Leffler struct rtentry **ret_nrt) 518df8bae1dSRodney W. Grimes { 5198071913dSRuslan Ermilov struct rt_addrinfo info; 5208071913dSRuslan Ermilov 521ac4a76ebSBjoern A. Zeeb if (dst->sa_len == 0) 522ac4a76ebSBjoern A. Zeeb return(EINVAL); 523ac4a76ebSBjoern A. Zeeb 5248071913dSRuslan Ermilov bzero((caddr_t)&info, sizeof(info)); 5258071913dSRuslan Ermilov info.rti_flags = flags; 5268071913dSRuslan Ermilov info.rti_info[RTAX_DST] = dst; 5278071913dSRuslan Ermilov info.rti_info[RTAX_GATEWAY] = gateway; 5288071913dSRuslan Ermilov info.rti_info[RTAX_NETMASK] = netmask; 5298071913dSRuslan Ermilov return rtrequest1(req, &info, ret_nrt); 5308071913dSRuslan Ermilov } 5318071913dSRuslan Ermilov 5328071913dSRuslan Ermilov /* 5338071913dSRuslan Ermilov * These (questionable) definitions of apparent local variables apply 5348071913dSRuslan Ermilov * to the next two functions. XXXXXX!!! 5358071913dSRuslan Ermilov */ 5368071913dSRuslan Ermilov #define dst info->rti_info[RTAX_DST] 5378071913dSRuslan Ermilov #define gateway info->rti_info[RTAX_GATEWAY] 5388071913dSRuslan Ermilov #define netmask info->rti_info[RTAX_NETMASK] 5398071913dSRuslan Ermilov #define ifaaddr info->rti_info[RTAX_IFA] 5408071913dSRuslan Ermilov #define ifpaddr info->rti_info[RTAX_IFP] 5418071913dSRuslan Ermilov #define flags info->rti_flags 5428071913dSRuslan Ermilov 5438071913dSRuslan Ermilov int 544d1dd20beSSam Leffler rt_getifa(struct rt_addrinfo *info) 5458071913dSRuslan Ermilov { 5468071913dSRuslan Ermilov struct ifaddr *ifa; 5478071913dSRuslan Ermilov int error = 0; 5488071913dSRuslan Ermilov 5498071913dSRuslan Ermilov /* 5508071913dSRuslan Ermilov * ifp may be specified by sockaddr_dl 5518071913dSRuslan Ermilov * when protocol address is ambiguous. 5528071913dSRuslan Ermilov */ 5538071913dSRuslan Ermilov if (info->rti_ifp == NULL && ifpaddr != NULL && 5548071913dSRuslan Ermilov ifpaddr->sa_family == AF_LINK && 5558071913dSRuslan Ermilov (ifa = ifa_ifwithnet(ifpaddr)) != NULL) 5568071913dSRuslan Ermilov info->rti_ifp = ifa->ifa_ifp; 5578071913dSRuslan Ermilov if (info->rti_ifa == NULL && ifaaddr != NULL) 5588071913dSRuslan Ermilov info->rti_ifa = ifa_ifwithaddr(ifaaddr); 5598071913dSRuslan Ermilov if (info->rti_ifa == NULL) { 5608071913dSRuslan Ermilov struct sockaddr *sa; 5618071913dSRuslan Ermilov 5628071913dSRuslan Ermilov sa = ifaaddr != NULL ? ifaaddr : 5638071913dSRuslan Ermilov (gateway != NULL ? gateway : dst); 5648071913dSRuslan Ermilov if (sa != NULL && info->rti_ifp != NULL) 5658071913dSRuslan Ermilov info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp); 5668071913dSRuslan Ermilov else if (dst != NULL && gateway != NULL) 5678071913dSRuslan Ermilov info->rti_ifa = ifa_ifwithroute(flags, dst, gateway); 5688071913dSRuslan Ermilov else if (sa != NULL) 5698071913dSRuslan Ermilov info->rti_ifa = ifa_ifwithroute(flags, sa, sa); 5708071913dSRuslan Ermilov } 5718071913dSRuslan Ermilov if ((ifa = info->rti_ifa) != NULL) { 5728071913dSRuslan Ermilov if (info->rti_ifp == NULL) 5738071913dSRuslan Ermilov info->rti_ifp = ifa->ifa_ifp; 5748071913dSRuslan Ermilov } else 5758071913dSRuslan Ermilov error = ENETUNREACH; 5768071913dSRuslan Ermilov return (error); 5778071913dSRuslan Ermilov } 5788071913dSRuslan Ermilov 5799c63e9dbSSam Leffler /* 5809c63e9dbSSam Leffler * Expunges references to a route that's about to be reclaimed. 5819c63e9dbSSam Leffler * The route must be locked. 5829c63e9dbSSam Leffler */ 5839c63e9dbSSam Leffler int 5849c63e9dbSSam Leffler rtexpunge(struct rtentry *rt) 5859c63e9dbSSam Leffler { 5869c63e9dbSSam Leffler struct radix_node *rn; 5879c63e9dbSSam Leffler struct radix_node_head *rnh; 5889c63e9dbSSam Leffler struct ifaddr *ifa; 5899c63e9dbSSam Leffler int error = 0; 5909c63e9dbSSam Leffler 5919c63e9dbSSam Leffler RT_LOCK_ASSERT(rt); 5929c63e9dbSSam Leffler #if 0 5939c63e9dbSSam Leffler /* 5949c63e9dbSSam Leffler * We cannot assume anything about the reference count 5959c63e9dbSSam Leffler * because protocols call us in many situations; often 5969c63e9dbSSam Leffler * before unwinding references to the table entry. 5979c63e9dbSSam Leffler */ 5989c63e9dbSSam Leffler KASSERT(rt->rt_refcnt <= 1, ("bogus refcnt %ld", rt->rt_refcnt)); 5999c63e9dbSSam Leffler #endif 6009c63e9dbSSam Leffler /* 6019c63e9dbSSam Leffler * Find the correct routing tree to use for this Address Family 6029c63e9dbSSam Leffler */ 6039c63e9dbSSam Leffler rnh = rt_tables[rt_key(rt)->sa_family]; 60485911824SLuigi Rizzo if (rnh == NULL) 6059c63e9dbSSam Leffler return (EAFNOSUPPORT); 6069c63e9dbSSam Leffler 6079c63e9dbSSam Leffler RADIX_NODE_HEAD_LOCK(rnh); 6089c63e9dbSSam Leffler 6099c63e9dbSSam Leffler /* 6109c63e9dbSSam Leffler * Remove the item from the tree; it should be there, 6119c63e9dbSSam Leffler * but when callers invoke us blindly it may not (sigh). 6129c63e9dbSSam Leffler */ 6139c63e9dbSSam Leffler rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh); 61485911824SLuigi Rizzo if (rn == NULL) { 6159c63e9dbSSam Leffler error = ESRCH; 6169c63e9dbSSam Leffler goto bad; 6179c63e9dbSSam Leffler } 6189c63e9dbSSam Leffler KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0, 6199c63e9dbSSam Leffler ("unexpected flags 0x%x", rn->rn_flags)); 620d6941ce9SLuigi Rizzo KASSERT(rt == RNTORT(rn), 6219c63e9dbSSam Leffler ("lookup mismatch, rt %p rn %p", rt, rn)); 6229c63e9dbSSam Leffler 6239c63e9dbSSam Leffler rt->rt_flags &= ~RTF_UP; 6249c63e9dbSSam Leffler 6259c63e9dbSSam Leffler /* 6269c63e9dbSSam Leffler * Now search what's left of the subtree for any cloned 6279c63e9dbSSam Leffler * routes which might have been formed from this node. 6289c63e9dbSSam Leffler */ 62926d02ca7SAndre Oppermann if ((rt->rt_flags & RTF_CLONING) && rt_mask(rt)) 6309c63e9dbSSam Leffler rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), 6319c63e9dbSSam Leffler rt_fixdelete, rt); 6329c63e9dbSSam Leffler 6339c63e9dbSSam Leffler /* 6349c63e9dbSSam Leffler * Remove any external references we may have. 6359c63e9dbSSam Leffler * This might result in another rtentry being freed if 6369c63e9dbSSam Leffler * we held its last reference. 6379c63e9dbSSam Leffler */ 6389c63e9dbSSam Leffler if (rt->rt_gwroute) { 639d6941ce9SLuigi Rizzo RTFREE(rt->rt_gwroute); 64085911824SLuigi Rizzo rt->rt_gwroute = NULL; 6419c63e9dbSSam Leffler } 6429c63e9dbSSam Leffler 6439c63e9dbSSam Leffler /* 6449c63e9dbSSam Leffler * Give the protocol a chance to keep things in sync. 6459c63e9dbSSam Leffler */ 6469c63e9dbSSam Leffler if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) { 6479c63e9dbSSam Leffler struct rt_addrinfo info; 6489c63e9dbSSam Leffler 6499c63e9dbSSam Leffler bzero((caddr_t)&info, sizeof(info)); 6509c63e9dbSSam Leffler info.rti_flags = rt->rt_flags; 6519c63e9dbSSam Leffler info.rti_info[RTAX_DST] = rt_key(rt); 6529c63e9dbSSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 6539c63e9dbSSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 6549c63e9dbSSam Leffler ifa->ifa_rtrequest(RTM_DELETE, rt, &info); 6559c63e9dbSSam Leffler } 6569c63e9dbSSam Leffler 6579c63e9dbSSam Leffler /* 6589c63e9dbSSam Leffler * one more rtentry floating around that is not 6599c63e9dbSSam Leffler * linked to the routing table. 6609c63e9dbSSam Leffler */ 6619c63e9dbSSam Leffler rttrash++; 6629c63e9dbSSam Leffler bad: 6639c63e9dbSSam Leffler RADIX_NODE_HEAD_UNLOCK(rnh); 6649c63e9dbSSam Leffler return (error); 6659c63e9dbSSam Leffler } 6669c63e9dbSSam Leffler 6678071913dSRuslan Ermilov int 668d1dd20beSSam Leffler rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt) 6698071913dSRuslan Ermilov { 670d1dd20beSSam Leffler int error = 0; 671df8bae1dSRodney W. Grimes register struct rtentry *rt; 672df8bae1dSRodney W. Grimes register struct radix_node *rn; 673df8bae1dSRodney W. Grimes register struct radix_node_head *rnh; 674df8bae1dSRodney W. Grimes struct ifaddr *ifa; 675df8bae1dSRodney W. Grimes struct sockaddr *ndst; 676df8bae1dSRodney W. Grimes #define senderr(x) { error = x ; goto bad; } 677df8bae1dSRodney W. Grimes 678b0a76b88SJulian Elischer /* 679b0a76b88SJulian Elischer * Find the correct routing tree to use for this Address Family 680b0a76b88SJulian Elischer */ 681d1dd20beSSam Leffler rnh = rt_tables[dst->sa_family]; 68285911824SLuigi Rizzo if (rnh == NULL) 683983985c1SJeffrey Hsu return (EAFNOSUPPORT); 684956b0b65SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rnh); 685b0a76b88SJulian Elischer /* 686b0a76b88SJulian Elischer * If we are adding a host route then we don't want to put 68766953138SRuslan Ermilov * a netmask in the tree, nor do we want to clone it. 688b0a76b88SJulian Elischer */ 68966953138SRuslan Ermilov if (flags & RTF_HOST) { 69085911824SLuigi Rizzo netmask = NULL; 69126d02ca7SAndre Oppermann flags &= ~RTF_CLONING; 69266953138SRuslan Ermilov } 693df8bae1dSRodney W. Grimes switch (req) { 694df8bae1dSRodney W. Grimes case RTM_DELETE: 695b0a76b88SJulian Elischer /* 696b0a76b88SJulian Elischer * Remove the item from the tree and return it. 697b0a76b88SJulian Elischer * Complain if it is not there and do no more processing. 698b0a76b88SJulian Elischer */ 699d1dd20beSSam Leffler rn = rnh->rnh_deladdr(dst, netmask, rnh); 70085911824SLuigi Rizzo if (rn == NULL) 701df8bae1dSRodney W. Grimes senderr(ESRCH); 702df8bae1dSRodney W. Grimes if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 703df8bae1dSRodney W. Grimes panic ("rtrequest delete"); 704d6941ce9SLuigi Rizzo rt = RNTORT(rn); 705d1dd20beSSam Leffler RT_LOCK(rt); 7067138d65cSSam Leffler RT_ADDREF(rt); 70771eba915SRuslan Ermilov rt->rt_flags &= ~RTF_UP; 708c2bed6a3SGarrett Wollman 709c2bed6a3SGarrett Wollman /* 710c2bed6a3SGarrett Wollman * Now search what's left of the subtree for any cloned 711c2bed6a3SGarrett Wollman * routes which might have been formed from this node. 712c2bed6a3SGarrett Wollman */ 71326d02ca7SAndre Oppermann if ((rt->rt_flags & RTF_CLONING) && 714089cdfadSRuslan Ermilov rt_mask(rt)) { 715089cdfadSRuslan Ermilov rnh->rnh_walktree_from(rnh, dst, rt_mask(rt), 716c2bed6a3SGarrett Wollman rt_fixdelete, rt); 717c2bed6a3SGarrett Wollman } 7183545b048SGarrett Wollman 719b0a76b88SJulian Elischer /* 720b0a76b88SJulian Elischer * Remove any external references we may have. 721b0a76b88SJulian Elischer * This might result in another rtentry being freed if 722dc733423SDag-Erling Smørgrav * we held its last reference. 723b0a76b88SJulian Elischer */ 7246ac3b69dSBill Fenner if (rt->rt_gwroute) { 725d6941ce9SLuigi Rizzo RTFREE(rt->rt_gwroute); 72685911824SLuigi Rizzo rt->rt_gwroute = NULL; 7276ac3b69dSBill Fenner } 7286ac3b69dSBill Fenner 7293545b048SGarrett Wollman /* 730499676dfSJulian Elischer * give the protocol a chance to keep things in sync. 731b0a76b88SJulian Elischer */ 732df8bae1dSRodney W. Grimes if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) 7338071913dSRuslan Ermilov ifa->ifa_rtrequest(RTM_DELETE, rt, info); 734499676dfSJulian Elischer 735b0a76b88SJulian Elischer /* 736d6941ce9SLuigi Rizzo * One more rtentry floating around that is not 737d6941ce9SLuigi Rizzo * linked to the routing table. rttrash will be decremented 738d6941ce9SLuigi Rizzo * when RTFREE(rt) is eventually called. 739499676dfSJulian Elischer */ 740499676dfSJulian Elischer rttrash++; 741499676dfSJulian Elischer 742499676dfSJulian Elischer /* 743499676dfSJulian Elischer * If the caller wants it, then it can have it, 744499676dfSJulian Elischer * but it's up to it to free the rtentry as we won't be 745499676dfSJulian Elischer * doing it. 746b0a76b88SJulian Elischer */ 747d1dd20beSSam Leffler if (ret_nrt) { 748df8bae1dSRodney W. Grimes *ret_nrt = rt; 749d1dd20beSSam Leffler RT_UNLOCK(rt); 750d1dd20beSSam Leffler } else 751d1dd20beSSam Leffler RTFREE_LOCKED(rt); 752df8bae1dSRodney W. Grimes break; 753df8bae1dSRodney W. Grimes 754df8bae1dSRodney W. Grimes case RTM_RESOLVE: 75585911824SLuigi Rizzo if (ret_nrt == NULL || (rt = *ret_nrt) == NULL) 756df8bae1dSRodney W. Grimes senderr(EINVAL); 757df8bae1dSRodney W. Grimes ifa = rt->rt_ifa; 758d1dd20beSSam Leffler /* XXX locking? */ 7593682d2baSDavid Greenman flags = rt->rt_flags & 76026d02ca7SAndre Oppermann ~(RTF_CLONING | RTF_STATIC); 761995add1aSGarrett Wollman flags |= RTF_WASCLONED; 762df8bae1dSRodney W. Grimes gateway = rt->rt_gateway; 76385911824SLuigi Rizzo if ((netmask = rt->rt_genmask) == NULL) 764df8bae1dSRodney W. Grimes flags |= RTF_HOST; 765df8bae1dSRodney W. Grimes goto makeroute; 766df8bae1dSRodney W. Grimes 767df8bae1dSRodney W. Grimes case RTM_ADD: 7685df72964SGarrett Wollman if ((flags & RTF_GATEWAY) && !gateway) 76916a2e0a6SQing Li senderr(EINVAL); 77016a2e0a6SQing Li if (dst && gateway && (dst->sa_family != gateway->sa_family) && 77116a2e0a6SQing Li (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK)) 77216a2e0a6SQing Li senderr(EINVAL); 7735df72964SGarrett Wollman 7748071913dSRuslan Ermilov if (info->rti_ifa == NULL && (error = rt_getifa(info))) 7758071913dSRuslan Ermilov senderr(error); 7768071913dSRuslan Ermilov ifa = info->rti_ifa; 7775df72964SGarrett Wollman 778df8bae1dSRodney W. Grimes makeroute: 7792dc1d581SAndre Oppermann rt = uma_zalloc(rtzone, M_NOWAIT | M_ZERO); 78085911824SLuigi Rizzo if (rt == NULL) 781df8bae1dSRodney W. Grimes senderr(ENOBUFS); 782d1dd20beSSam Leffler RT_LOCK_INIT(rt); 783df8bae1dSRodney W. Grimes rt->rt_flags = RTF_UP | flags; 784499676dfSJulian Elischer /* 785499676dfSJulian Elischer * Add the gateway. Possibly re-malloc-ing the storage for it 786499676dfSJulian Elischer * also add the rt_gwroute if possible. 787499676dfSJulian Elischer */ 788d1dd20beSSam Leffler RT_LOCK(rt); 789831a80b0SMatthew Dillon if ((error = rt_setgate(rt, dst, gateway)) != 0) { 790d1dd20beSSam Leffler RT_LOCK_DESTROY(rt); 7912dc1d581SAndre Oppermann uma_zfree(rtzone, rt); 792704b0666SBill Fenner senderr(error); 793df8bae1dSRodney W. Grimes } 794499676dfSJulian Elischer 795499676dfSJulian Elischer /* 796499676dfSJulian Elischer * point to the (possibly newly malloc'd) dest address. 797499676dfSJulian Elischer */ 798d1dd20beSSam Leffler ndst = (struct sockaddr *)rt_key(rt); 799499676dfSJulian Elischer 800499676dfSJulian Elischer /* 801499676dfSJulian Elischer * make sure it contains the value we want (masked if needed). 802499676dfSJulian Elischer */ 803df8bae1dSRodney W. Grimes if (netmask) { 804df8bae1dSRodney W. Grimes rt_maskedcopy(dst, ndst, netmask); 805df8bae1dSRodney W. Grimes } else 8061838a647SLuigi Rizzo bcopy(dst, ndst, dst->sa_len); 8078e718bb4SGarrett Wollman 8088e718bb4SGarrett Wollman /* 809499676dfSJulian Elischer * Note that we now have a reference to the ifa. 8108e718bb4SGarrett Wollman * This moved from below so that rnh->rnh_addaddr() can 811499676dfSJulian Elischer * examine the ifa and ifa->ifa_ifp if it so desires. 8128e718bb4SGarrett Wollman */ 81319fc74fbSJeffrey Hsu IFAREF(ifa); 8148e718bb4SGarrett Wollman rt->rt_ifa = ifa; 8158e718bb4SGarrett Wollman rt->rt_ifp = ifa->ifa_ifp; 8168e718bb4SGarrett Wollman 817d1dd20beSSam Leffler /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */ 818d1dd20beSSam Leffler rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes); 81985911824SLuigi Rizzo if (rn == NULL) { 820aca1a47cSGarrett Wollman struct rtentry *rt2; 821aca1a47cSGarrett Wollman /* 822aca1a47cSGarrett Wollman * Uh-oh, we already have one of these in the tree. 823aca1a47cSGarrett Wollman * We do a special hack: if the route that's already 82426d02ca7SAndre Oppermann * there was generated by the cloning mechanism 82526d02ca7SAndre Oppermann * then we just blow it away and retry the insertion 82626d02ca7SAndre Oppermann * of the new one. 827aca1a47cSGarrett Wollman */ 82826d02ca7SAndre Oppermann rt2 = rtalloc1(dst, 0, 0); 829aca1a47cSGarrett Wollman if (rt2 && rt2->rt_parent) { 8309c63e9dbSSam Leffler rtexpunge(rt2); 8319c63e9dbSSam Leffler RT_UNLOCK(rt2); 832d1dd20beSSam Leffler rn = rnh->rnh_addaddr(ndst, netmask, 833aca1a47cSGarrett Wollman rnh, rt->rt_nodes); 834fde327d6SGarrett Wollman } else if (rt2) { 835499676dfSJulian Elischer /* undo the extra ref we got */ 836d1dd20beSSam Leffler RTFREE_LOCKED(rt2); 837aca1a47cSGarrett Wollman } 838aca1a47cSGarrett Wollman } 839aca1a47cSGarrett Wollman 840499676dfSJulian Elischer /* 841499676dfSJulian Elischer * If it still failed to go into the tree, 842499676dfSJulian Elischer * then un-make it (this should be a function) 843499676dfSJulian Elischer */ 84485911824SLuigi Rizzo if (rn == NULL) { 845df8bae1dSRodney W. Grimes if (rt->rt_gwroute) 846d1dd20beSSam Leffler RTFREE(rt->rt_gwroute); 847d1dd20beSSam Leffler if (rt->rt_ifa) 8488e718bb4SGarrett Wollman IFAFREE(rt->rt_ifa); 849df8bae1dSRodney W. Grimes Free(rt_key(rt)); 850d1dd20beSSam Leffler RT_LOCK_DESTROY(rt); 8512dc1d581SAndre Oppermann uma_zfree(rtzone, rt); 852df8bae1dSRodney W. Grimes senderr(EEXIST); 853df8bae1dSRodney W. Grimes } 854499676dfSJulian Elischer 85585911824SLuigi Rizzo rt->rt_parent = NULL; 856771edb14SGarrett Wollman 857499676dfSJulian Elischer /* 858499676dfSJulian Elischer * If we got here from RESOLVE, then we are cloning 859499676dfSJulian Elischer * so clone the rest, and note that we 860499676dfSJulian Elischer * are a clone (and increment the parent's references) 861499676dfSJulian Elischer */ 862c2bed6a3SGarrett Wollman if (req == RTM_RESOLVE) { 863d1dd20beSSam Leffler KASSERT(ret_nrt && *ret_nrt, 864d1dd20beSSam Leffler ("no route to clone from")); 865df8bae1dSRodney W. Grimes rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */ 86654e84abbSMike Silbersack rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */ 86726d02ca7SAndre Oppermann if ((*ret_nrt)->rt_flags & RTF_CLONING) { 868d1dd20beSSam Leffler /* 869d1dd20beSSam Leffler * NB: We do not bump the refcnt on the parent 870d1dd20beSSam Leffler * entry under the assumption that it will 871d1dd20beSSam Leffler * remain so long as we do. This is 872d1dd20beSSam Leffler * important when deleting the parent route 873d1dd20beSSam Leffler * as this operation requires traversing 874d1dd20beSSam Leffler * the tree to delete all clones and futzing 875d1dd20beSSam Leffler * with refcnts requires us to double-lock 876d1dd20beSSam Leffler * parent through this back reference. 877d1dd20beSSam Leffler */ 878d1dd20beSSam Leffler rt->rt_parent = *ret_nrt; 879771edb14SGarrett Wollman } 88018e1f1f1SGarrett Wollman } 881499676dfSJulian Elischer 882499676dfSJulian Elischer /* 883499676dfSJulian Elischer * if this protocol has something to add to this then 884499676dfSJulian Elischer * allow it to do that as well. 885499676dfSJulian Elischer */ 886df8bae1dSRodney W. Grimes if (ifa->ifa_rtrequest) 8878071913dSRuslan Ermilov ifa->ifa_rtrequest(req, rt, info); 888499676dfSJulian Elischer 889cd02a0b7SGarrett Wollman /* 890cd02a0b7SGarrett Wollman * We repeat the same procedure from rt_setgate() here because 891cd02a0b7SGarrett Wollman * it doesn't fire when we call it there because the node 892cd02a0b7SGarrett Wollman * hasn't been added to the tree yet. 893cd02a0b7SGarrett Wollman */ 89436fea5deSRuslan Ermilov if (req == RTM_ADD && 89585911824SLuigi Rizzo !(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) { 896cd02a0b7SGarrett Wollman struct rtfc_arg arg; 897cd02a0b7SGarrett Wollman arg.rnh = rnh; 898cd02a0b7SGarrett Wollman arg.rt0 = rt; 899cd02a0b7SGarrett Wollman rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), 900cd02a0b7SGarrett Wollman rt_fixchange, &arg); 901cd02a0b7SGarrett Wollman } 902cd02a0b7SGarrett Wollman 903499676dfSJulian Elischer /* 904499676dfSJulian Elischer * actually return a resultant rtentry and 905499676dfSJulian Elischer * give the caller a single reference. 906499676dfSJulian Elischer */ 907df8bae1dSRodney W. Grimes if (ret_nrt) { 908df8bae1dSRodney W. Grimes *ret_nrt = rt; 9097138d65cSSam Leffler RT_ADDREF(rt); 910df8bae1dSRodney W. Grimes } 911d1dd20beSSam Leffler RT_UNLOCK(rt); 912df8bae1dSRodney W. Grimes break; 9138071913dSRuslan Ermilov default: 9148071913dSRuslan Ermilov error = EOPNOTSUPP; 915df8bae1dSRodney W. Grimes } 916df8bae1dSRodney W. Grimes bad: 917956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 918df8bae1dSRodney W. Grimes return (error); 919d1dd20beSSam Leffler #undef senderr 920d1dd20beSSam Leffler } 921d1dd20beSSam Leffler 9228071913dSRuslan Ermilov #undef dst 9238071913dSRuslan Ermilov #undef gateway 9248071913dSRuslan Ermilov #undef netmask 9258071913dSRuslan Ermilov #undef ifaaddr 9268071913dSRuslan Ermilov #undef ifpaddr 9278071913dSRuslan Ermilov #undef flags 928df8bae1dSRodney W. Grimes 92918e1f1f1SGarrett Wollman /* 93018e1f1f1SGarrett Wollman * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family'' 93118e1f1f1SGarrett Wollman * (i.e., the routes related to it by the operation of cloning). This 932c2bed6a3SGarrett Wollman * routine is iterated over all potential former-child-routes by way of 933c2bed6a3SGarrett Wollman * rnh->rnh_walktree_from() above, and those that actually are children of 934c2bed6a3SGarrett Wollman * the late parent (passed in as VP here) are themselves deleted. 93518e1f1f1SGarrett Wollman */ 936c2bed6a3SGarrett Wollman static int 937d1dd20beSSam Leffler rt_fixdelete(struct radix_node *rn, void *vp) 93818e1f1f1SGarrett Wollman { 939d6941ce9SLuigi Rizzo struct rtentry *rt = RNTORT(rn); 940c2bed6a3SGarrett Wollman struct rtentry *rt0 = vp; 94118e1f1f1SGarrett Wollman 94236fea5deSRuslan Ermilov if (rt->rt_parent == rt0 && 94326d02ca7SAndre Oppermann !(rt->rt_flags & (RTF_PINNED | RTF_CLONING))) { 94485911824SLuigi Rizzo return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 94585911824SLuigi Rizzo rt->rt_flags, NULL); 94618e1f1f1SGarrett Wollman } 947c2bed6a3SGarrett Wollman return 0; 94818e1f1f1SGarrett Wollman } 94918e1f1f1SGarrett Wollman 950cd02a0b7SGarrett Wollman /* 951cd02a0b7SGarrett Wollman * This routine is called from rt_setgate() to do the analogous thing for 952cd02a0b7SGarrett Wollman * adds and changes. There is the added complication in this case of a 953cd02a0b7SGarrett Wollman * middle insert; i.e., insertion of a new network route between an older 954cd02a0b7SGarrett Wollman * network route and (cloned) host routes. For this reason, a simple check 955cd02a0b7SGarrett Wollman * of rt->rt_parent is insufficient; each candidate route must be tested 956cd02a0b7SGarrett Wollman * against the (mask, value) of the new route (passed as before in vp) 9579a701516SHajimu UMEMOTO * to see if the new route matches it. 958cd02a0b7SGarrett Wollman * 959cd02a0b7SGarrett Wollman * XXX - it may be possible to do fixdelete() for changes and reserve this 960cd02a0b7SGarrett Wollman * routine just for adds. I'm not sure why I thought it was necessary to do 961cd02a0b7SGarrett Wollman * changes this way. 962cd02a0b7SGarrett Wollman */ 963cd02a0b7SGarrett Wollman 964cd02a0b7SGarrett Wollman static int 965d1dd20beSSam Leffler rt_fixchange(struct radix_node *rn, void *vp) 966cd02a0b7SGarrett Wollman { 967d6941ce9SLuigi Rizzo struct rtentry *rt = RNTORT(rn); 968cd02a0b7SGarrett Wollman struct rtfc_arg *ap = vp; 969cd02a0b7SGarrett Wollman struct rtentry *rt0 = ap->rt0; 970cd02a0b7SGarrett Wollman struct radix_node_head *rnh = ap->rnh; 9719a701516SHajimu UMEMOTO u_char *xk1, *xm1, *xk2, *xmp; 9729a701516SHajimu UMEMOTO int i, len, mlen; 973cd02a0b7SGarrett Wollman 97485911824SLuigi Rizzo /* make sure we have a parent, and route is not pinned or cloning */ 97536fea5deSRuslan Ermilov if (!rt->rt_parent || 97685911824SLuigi Rizzo (rt->rt_flags & (RTF_PINNED | RTF_CLONING))) 977cd02a0b7SGarrett Wollman return 0; 978cd02a0b7SGarrett Wollman 97985911824SLuigi Rizzo if (rt->rt_parent == rt0) /* parent match */ 98085911824SLuigi Rizzo goto delete_rt; 981cd02a0b7SGarrett Wollman /* 982cd02a0b7SGarrett Wollman * There probably is a function somewhere which does this... 983cd02a0b7SGarrett Wollman * if not, there should be. 984cd02a0b7SGarrett Wollman */ 985d1dd20beSSam Leffler len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len); 986cd02a0b7SGarrett Wollman 987cd02a0b7SGarrett Wollman xk1 = (u_char *)rt_key(rt0); 988cd02a0b7SGarrett Wollman xm1 = (u_char *)rt_mask(rt0); 989cd02a0b7SGarrett Wollman xk2 = (u_char *)rt_key(rt); 990cd02a0b7SGarrett Wollman 9919a701516SHajimu UMEMOTO /* avoid applying a less specific route */ 9929a701516SHajimu UMEMOTO xmp = (u_char *)rt_mask(rt->rt_parent); 993d1dd20beSSam Leffler mlen = rt_key(rt->rt_parent)->sa_len; 99485911824SLuigi Rizzo if (mlen > rt_key(rt0)->sa_len) /* less specific route */ 9959a701516SHajimu UMEMOTO return 0; 99685911824SLuigi Rizzo for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++) 99785911824SLuigi Rizzo if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i]) 99885911824SLuigi Rizzo return 0; /* less specific route */ 9999a701516SHajimu UMEMOTO 100085911824SLuigi Rizzo for (i = rnh->rnh_treetop->rn_offset; i < len; i++) 100185911824SLuigi Rizzo if ((xk2[i] & xm1[i]) != xk1[i]) 100285911824SLuigi Rizzo return 0; /* no match */ 1003cd02a0b7SGarrett Wollman 1004cd02a0b7SGarrett Wollman /* 1005cd02a0b7SGarrett Wollman * OK, this node is a clone, and matches the node currently being 1006cd02a0b7SGarrett Wollman * changed/added under the node's mask. So, get rid of it. 1007cd02a0b7SGarrett Wollman */ 100885911824SLuigi Rizzo delete_rt: 100985911824SLuigi Rizzo return rtrequest(RTM_DELETE, rt_key(rt), NULL, 101085911824SLuigi Rizzo rt_mask(rt), rt->rt_flags, NULL); 1011cd02a0b7SGarrett Wollman } 1012cd02a0b7SGarrett Wollman 1013df8bae1dSRodney W. Grimes int 1014d1dd20beSSam Leffler rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate) 1015df8bae1dSRodney W. Grimes { 1016d1dd20beSSam Leffler /* XXX dst may be overwritten, can we move this to below */ 1017d1dd20beSSam Leffler struct radix_node_head *rnh = rt_tables[dst->sa_family]; 1018e74642dfSLuigi Rizzo int dlen = SA_SIZE(dst), glen = SA_SIZE(gate); 1019d1dd20beSSam Leffler 1020d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 1021df8bae1dSRodney W. Grimes 10221db1fffaSBill Fenner /* 10231db1fffaSBill Fenner * A host route with the destination equal to the gateway 10241db1fffaSBill Fenner * will interfere with keeping LLINFO in the routing 10251db1fffaSBill Fenner * table, so disallow it. 10261db1fffaSBill Fenner */ 1027d1dd20beSSam Leffler if (((rt->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) == 10281db1fffaSBill Fenner (RTF_HOST|RTF_GATEWAY)) && 1029d1dd20beSSam Leffler dst->sa_len == gate->sa_len && 1030d1dd20beSSam Leffler bcmp(dst, gate, dst->sa_len) == 0) { 10311db1fffaSBill Fenner /* 10321db1fffaSBill Fenner * The route might already exist if this is an RTM_CHANGE 10331db1fffaSBill Fenner * or a routing redirect, so try to delete it. 10341db1fffaSBill Fenner */ 1035d1dd20beSSam Leffler if (rt_key(rt)) 10369c63e9dbSSam Leffler rtexpunge(rt); 10371db1fffaSBill Fenner return EADDRNOTAVAIL; 10381db1fffaSBill Fenner } 10391db1fffaSBill Fenner 1040499676dfSJulian Elischer /* 10412d7e9eadSGleb Smirnoff * Cloning loop avoidance in case of bad configuration. 10422d7e9eadSGleb Smirnoff */ 10432d7e9eadSGleb Smirnoff if (rt->rt_flags & RTF_GATEWAY) { 10442d7e9eadSGleb Smirnoff struct rtentry *gwrt; 10452d7e9eadSGleb Smirnoff 10462d7e9eadSGleb Smirnoff RT_UNLOCK(rt); /* XXX workaround LOR */ 10472d7e9eadSGleb Smirnoff gwrt = rtalloc1(gate, 1, 0); 10482d7e9eadSGleb Smirnoff if (gwrt == rt) { 10492d7e9eadSGleb Smirnoff RT_LOCK_ASSERT(rt); 10502d7e9eadSGleb Smirnoff RT_REMREF(rt); 10512d7e9eadSGleb Smirnoff return (EADDRINUSE); /* failure */ 10522d7e9eadSGleb Smirnoff } 10532d7e9eadSGleb Smirnoff RT_LOCK(rt); 10542d7e9eadSGleb Smirnoff /* 10552d7e9eadSGleb Smirnoff * If there is already a gwroute, then drop it. If we 10562d7e9eadSGleb Smirnoff * are asked to replace route with itself, then do 10572d7e9eadSGleb Smirnoff * not leak its refcounter. 10582d7e9eadSGleb Smirnoff */ 10592d7e9eadSGleb Smirnoff if (rt->rt_gwroute != NULL) { 10602d7e9eadSGleb Smirnoff if (rt->rt_gwroute == gwrt) { 10612d7e9eadSGleb Smirnoff RT_REMREF(rt->rt_gwroute); 10622d7e9eadSGleb Smirnoff } else 10632d7e9eadSGleb Smirnoff RTFREE(rt->rt_gwroute); 10642d7e9eadSGleb Smirnoff } 10652d7e9eadSGleb Smirnoff 10662d7e9eadSGleb Smirnoff if ((rt->rt_gwroute = gwrt) != NULL) 10672d7e9eadSGleb Smirnoff RT_UNLOCK(rt->rt_gwroute); 10682d7e9eadSGleb Smirnoff } 10692d7e9eadSGleb Smirnoff 10702d7e9eadSGleb Smirnoff /* 107185911824SLuigi Rizzo * Prepare to store the gateway in rt->rt_gateway. 107285911824SLuigi Rizzo * Both dst and gateway are stored one after the other in the same 107385911824SLuigi Rizzo * malloc'd chunk. If we have room, we can reuse the old buffer, 107485911824SLuigi Rizzo * rt_gateway already points to the right place. 107585911824SLuigi Rizzo * Otherwise, malloc a new block and update the 'dst' address. 1076499676dfSJulian Elischer */ 107785911824SLuigi Rizzo if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) { 107885911824SLuigi Rizzo caddr_t new; 107985911824SLuigi Rizzo 1080df8bae1dSRodney W. Grimes R_Malloc(new, caddr_t, dlen + glen); 108185911824SLuigi Rizzo if (new == NULL) 10821db1fffaSBill Fenner return ENOBUFS; 1083499676dfSJulian Elischer /* 108485911824SLuigi Rizzo * XXX note, we copy from *dst and not *rt_key(rt) because 108585911824SLuigi Rizzo * rt_setgate() can be called to initialize a newly 108685911824SLuigi Rizzo * allocated route entry, in which case rt_key(rt) == NULL 108785911824SLuigi Rizzo * (and also rt->rt_gateway == NULL). 108885911824SLuigi Rizzo * Free()/free() handle a NULL argument just fine. 1089499676dfSJulian Elischer */ 10901838a647SLuigi Rizzo bcopy(dst, new, dlen); 109185911824SLuigi Rizzo Free(rt_key(rt)); /* free old block, if any */ 1092445e045bSAlexander Kabaev rt_key(rt) = (struct sockaddr *)new; 109385911824SLuigi Rizzo rt->rt_gateway = (struct sockaddr *)(new + dlen); 1094df8bae1dSRodney W. Grimes } 1095499676dfSJulian Elischer 1096499676dfSJulian Elischer /* 109785911824SLuigi Rizzo * Copy the new gateway value into the memory chunk. 109885911824SLuigi Rizzo */ 109985911824SLuigi Rizzo bcopy(gate, rt->rt_gateway, glen); 110085911824SLuigi Rizzo 110185911824SLuigi Rizzo /* 1102cd02a0b7SGarrett Wollman * This isn't going to do anything useful for host routes, so 1103cd02a0b7SGarrett Wollman * don't bother. Also make sure we have a reasonable mask 1104cd02a0b7SGarrett Wollman * (we don't yet have one during adds). 1105cd02a0b7SGarrett Wollman */ 1106cd02a0b7SGarrett Wollman if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) { 1107cd02a0b7SGarrett Wollman struct rtfc_arg arg; 1108d1dd20beSSam Leffler 1109cd02a0b7SGarrett Wollman arg.rnh = rnh; 1110cd02a0b7SGarrett Wollman arg.rt0 = rt; 1111e21afc60SSam Leffler RT_UNLOCK(rt); /* XXX workaround LOR */ 1112956b0b65SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rnh); 111372b9c8c9SSam Leffler RT_LOCK(rt); 1114cd02a0b7SGarrett Wollman rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), 1115cd02a0b7SGarrett Wollman rt_fixchange, &arg); 1116956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 1117cd02a0b7SGarrett Wollman } 1118cd02a0b7SGarrett Wollman 1119df8bae1dSRodney W. Grimes return 0; 1120df8bae1dSRodney W. Grimes } 1121df8bae1dSRodney W. Grimes 1122f708ef1bSPoul-Henning Kamp static void 1123d1dd20beSSam Leffler rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask) 1124df8bae1dSRodney W. Grimes { 1125df8bae1dSRodney W. Grimes register u_char *cp1 = (u_char *)src; 1126df8bae1dSRodney W. Grimes register u_char *cp2 = (u_char *)dst; 1127df8bae1dSRodney W. Grimes register u_char *cp3 = (u_char *)netmask; 1128df8bae1dSRodney W. Grimes u_char *cplim = cp2 + *cp3; 1129df8bae1dSRodney W. Grimes u_char *cplim2 = cp2 + *cp1; 1130df8bae1dSRodney W. Grimes 1131df8bae1dSRodney W. Grimes *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */ 1132df8bae1dSRodney W. Grimes cp3 += 2; 1133df8bae1dSRodney W. Grimes if (cplim > cplim2) 1134df8bae1dSRodney W. Grimes cplim = cplim2; 1135df8bae1dSRodney W. Grimes while (cp2 < cplim) 1136df8bae1dSRodney W. Grimes *cp2++ = *cp1++ & *cp3++; 1137df8bae1dSRodney W. Grimes if (cp2 < cplim2) 1138df8bae1dSRodney W. Grimes bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2)); 1139df8bae1dSRodney W. Grimes } 1140df8bae1dSRodney W. Grimes 1141df8bae1dSRodney W. Grimes /* 1142df8bae1dSRodney W. Grimes * Set up a routing table entry, normally 1143df8bae1dSRodney W. Grimes * for an interface. 1144df8bae1dSRodney W. Grimes */ 1145df8bae1dSRodney W. Grimes int 1146d1dd20beSSam Leffler rtinit(struct ifaddr *ifa, int cmd, int flags) 1147df8bae1dSRodney W. Grimes { 11485aca0b30SLuigi Rizzo struct sockaddr *dst; 11498071913dSRuslan Ermilov struct sockaddr *netmask; 115085911824SLuigi Rizzo struct mbuf *m = NULL; 115185911824SLuigi Rizzo struct rtentry *rt = NULL; 11528071913dSRuslan Ermilov struct rt_addrinfo info; 11535aca0b30SLuigi Rizzo int error; 1154df8bae1dSRodney W. Grimes 11558071913dSRuslan Ermilov if (flags & RTF_HOST) { 11568071913dSRuslan Ermilov dst = ifa->ifa_dstaddr; 11578071913dSRuslan Ermilov netmask = NULL; 11588071913dSRuslan Ermilov } else { 11598071913dSRuslan Ermilov dst = ifa->ifa_addr; 11608071913dSRuslan Ermilov netmask = ifa->ifa_netmask; 11618071913dSRuslan Ermilov } 1162ac4a76ebSBjoern A. Zeeb if (dst->sa_len == 0) 1163ac4a76ebSBjoern A. Zeeb return(EINVAL); 1164ac4a76ebSBjoern A. Zeeb 1165b0a76b88SJulian Elischer /* 1166b0a76b88SJulian Elischer * If it's a delete, check that if it exists, it's on the correct 1167b0a76b88SJulian Elischer * interface or we might scrub a route to another ifa which would 1168b0a76b88SJulian Elischer * be confusing at best and possibly worse. 1169b0a76b88SJulian Elischer */ 1170df8bae1dSRodney W. Grimes if (cmd == RTM_DELETE) { 11715aca0b30SLuigi Rizzo struct sockaddr *deldst; 11725aca0b30SLuigi Rizzo struct radix_node_head *rnh; 11735aca0b30SLuigi Rizzo struct radix_node *rn; 11745aca0b30SLuigi Rizzo 1175b0a76b88SJulian Elischer /* 1176b0a76b88SJulian Elischer * It's a delete, so it should already exist.. 1177b0a76b88SJulian Elischer * If it's a net, mask off the host bits 1178b0a76b88SJulian Elischer * (Assuming we have a mask) 1179b0a76b88SJulian Elischer */ 11808071913dSRuslan Ermilov if (netmask != NULL) { 1181a163d034SWarner Losh m = m_get(M_DONTWAIT, MT_SONAME); 118282cd038dSYoshinobu Inoue if (m == NULL) 118382cd038dSYoshinobu Inoue return(ENOBUFS); 1184df8bae1dSRodney W. Grimes deldst = mtod(m, struct sockaddr *); 11858071913dSRuslan Ermilov rt_maskedcopy(dst, deldst, netmask); 1186df8bae1dSRodney W. Grimes dst = deldst; 1187df8bae1dSRodney W. Grimes } 1188b0a76b88SJulian Elischer /* 11898071913dSRuslan Ermilov * Look up an rtentry that is in the routing tree and 11908071913dSRuslan Ermilov * contains the correct info. 1191b0a76b88SJulian Elischer */ 1192956b0b65SJeffrey Hsu if ((rnh = rt_tables[dst->sa_family]) == NULL) 1193956b0b65SJeffrey Hsu goto bad; 1194956b0b65SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rnh); 1195956b0b65SJeffrey Hsu error = ((rn = rnh->rnh_lookup(dst, netmask, rnh)) == NULL || 11968071913dSRuslan Ermilov (rn->rn_flags & RNF_ROOT) || 1197d6941ce9SLuigi Rizzo RNTORT(rn)->rt_ifa != ifa || 119885911824SLuigi Rizzo !sa_equal((struct sockaddr *)rn->rn_key, dst)); 1199956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 1200956b0b65SJeffrey Hsu if (error) { 1201956b0b65SJeffrey Hsu bad: 1202df8bae1dSRodney W. Grimes if (m) 1203df8bae1dSRodney W. Grimes (void) m_free(m); 12048071913dSRuslan Ermilov return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 1205df8bae1dSRodney W. Grimes } 1206b0a76b88SJulian Elischer } 1207b0a76b88SJulian Elischer /* 1208b0a76b88SJulian Elischer * Do the actual request 1209b0a76b88SJulian Elischer */ 12108071913dSRuslan Ermilov bzero((caddr_t)&info, sizeof(info)); 12118071913dSRuslan Ermilov info.rti_ifa = ifa; 12128071913dSRuslan Ermilov info.rti_flags = flags | ifa->ifa_flags; 12138071913dSRuslan Ermilov info.rti_info[RTAX_DST] = dst; 12148071913dSRuslan Ermilov info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr; 12158071913dSRuslan Ermilov info.rti_info[RTAX_NETMASK] = netmask; 12165aca0b30SLuigi Rizzo error = rtrequest1(cmd, &info, &rt); 12175aca0b30SLuigi Rizzo if (error == 0 && rt != NULL) { 12188071913dSRuslan Ermilov /* 12196f99b44cSBrian Somers * notify any listening routing agents of the change 12208071913dSRuslan Ermilov */ 1221d1dd20beSSam Leffler RT_LOCK(rt); 12228071913dSRuslan Ermilov rt_newaddrmsg(cmd, ifa, error, rt); 12238071913dSRuslan Ermilov if (cmd == RTM_DELETE) { 1224b0a76b88SJulian Elischer /* 1225b0a76b88SJulian Elischer * If we are deleting, and we found an entry, then 1226b0a76b88SJulian Elischer * it's been removed from the tree.. now throw it away. 1227b0a76b88SJulian Elischer */ 1228d1dd20beSSam Leffler RTFREE_LOCKED(rt); 1229d1dd20beSSam Leffler } else { 1230d1dd20beSSam Leffler if (cmd == RTM_ADD) { 1231b0a76b88SJulian Elischer /* 12328071913dSRuslan Ermilov * We just wanted to add it.. we don't actually 12338071913dSRuslan Ermilov * need a reference. 1234b0a76b88SJulian Elischer */ 12357138d65cSSam Leffler RT_REMREF(rt); 1236df8bae1dSRodney W. Grimes } 1237d1dd20beSSam Leffler RT_UNLOCK(rt); 1238d1dd20beSSam Leffler } 1239df8bae1dSRodney W. Grimes } 12408071913dSRuslan Ermilov if (m) 12418071913dSRuslan Ermilov (void) m_free(m); 12423ec66d6cSDavid Greenman return (error); 12433ec66d6cSDavid Greenman } 1244cb64988fSLuoqi Chen 1245d1dd20beSSam Leffler /* 124676927022SLuigi Rizzo * rt_check() is invoked on each layer 2 output path, prior to 124776927022SLuigi Rizzo * encapsulating outbound packets. 124876927022SLuigi Rizzo * 124976927022SLuigi Rizzo * The function is mostly used to find a routing entry for the gateway, 125076927022SLuigi Rizzo * which in some protocol families could also point to the link-level 125176927022SLuigi Rizzo * address for the gateway itself (the side effect of revalidating the 125276927022SLuigi Rizzo * route to the destination is rather pointless at this stage, we did it 125376927022SLuigi Rizzo * already a moment before in the pr_output() routine to locate the ifp 125476927022SLuigi Rizzo * and gateway to use). 125576927022SLuigi Rizzo * 125676927022SLuigi Rizzo * When we remove the layer-3 to layer-2 mapping tables from the 125776927022SLuigi Rizzo * routing table, this function can be removed. 125876927022SLuigi Rizzo * 125976927022SLuigi Rizzo * === On input === 126076927022SLuigi Rizzo * *dst is the address of the NEXT HOP (which coincides with the 126176927022SLuigi Rizzo * final destination if directly reachable); 126276927022SLuigi Rizzo * *lrt0 points to the cached route to the final destination; 126376927022SLuigi Rizzo * *lrt is not meaningful; 126476927022SLuigi Rizzo * 126576927022SLuigi Rizzo * === Operation === 126676927022SLuigi Rizzo * If the route is marked down try to find a new route. If the route 1267d1dd20beSSam Leffler * to the gateway is gone, try to setup a new route. Otherwise, 1268d1dd20beSSam Leffler * if the route is marked for packets to be rejected, enforce that. 1269d1dd20beSSam Leffler * 127076927022SLuigi Rizzo * === On return === 127176927022SLuigi Rizzo * *dst is unchanged; 127276927022SLuigi Rizzo * *lrt0 points to the (possibly new) route to the final destination 127376927022SLuigi Rizzo * *lrt points to the route to the next hop 1274d1dd20beSSam Leffler * 1275490b9d88SLuigi Rizzo * Their values are meaningful ONLY if no error is returned. 1276d1dd20beSSam Leffler */ 12777f760c48SMatthew N. Dodd int 1278d1dd20beSSam Leffler rt_check(struct rtentry **lrt, struct rtentry **lrt0, struct sockaddr *dst) 12797f760c48SMatthew N. Dodd { 1280d1dd20beSSam Leffler #define senderr(x) { error = x ; goto bad; } 12817f760c48SMatthew N. Dodd struct rtentry *rt; 12827f760c48SMatthew N. Dodd struct rtentry *rt0; 12837f760c48SMatthew N. Dodd int error; 12847f760c48SMatthew N. Dodd 1285530f95fcSGleb Smirnoff KASSERT(*lrt0 != NULL, ("rt_check")); 1286530f95fcSGleb Smirnoff rt = rt0 = *lrt0; 1287530f95fcSGleb Smirnoff 1288d1dd20beSSam Leffler /* NB: the locking here is tortuous... */ 1289d1dd20beSSam Leffler RT_LOCK(rt); 1290d1dd20beSSam Leffler if ((rt->rt_flags & RTF_UP) == 0) { 1291d1dd20beSSam Leffler RT_UNLOCK(rt); 1292d1dd20beSSam Leffler rt = rtalloc1(dst, 1, 0UL); 12937f760c48SMatthew N. Dodd if (rt != NULL) { 12947138d65cSSam Leffler RT_REMREF(rt); 1295d4b2657fSSam Leffler /* XXX what about if change? */ 1296d1dd20beSSam Leffler } else 12977f760c48SMatthew N. Dodd senderr(EHOSTUNREACH); 1298d1dd20beSSam Leffler rt0 = rt; 12997f760c48SMatthew N. Dodd } 1300d1dd20beSSam Leffler /* XXX BSD/OS checks dst->sa_family != AF_NS */ 13017f760c48SMatthew N. Dodd if (rt->rt_flags & RTF_GATEWAY) { 130285911824SLuigi Rizzo if (rt->rt_gwroute == NULL) 13037f760c48SMatthew N. Dodd goto lookup; 13047f760c48SMatthew N. Dodd rt = rt->rt_gwroute; 1305d1dd20beSSam Leffler RT_LOCK(rt); /* NB: gwroute */ 13067f760c48SMatthew N. Dodd if ((rt->rt_flags & RTF_UP) == 0) { 1307d1dd20beSSam Leffler rtfree(rt); /* unlock gwroute */ 13087f760c48SMatthew N. Dodd rt = rt0; 13097f760c48SMatthew N. Dodd lookup: 1310d1dd20beSSam Leffler RT_UNLOCK(rt0); 1311d1dd20beSSam Leffler rt = rtalloc1(rt->rt_gateway, 1, 0UL); 1312d1dd20beSSam Leffler RT_LOCK(rt0); 1313d1dd20beSSam Leffler rt0->rt_gwroute = rt; 131485911824SLuigi Rizzo if (rt == NULL) { 1315d1dd20beSSam Leffler RT_UNLOCK(rt0); 13167f760c48SMatthew N. Dodd senderr(EHOSTUNREACH); 13177f760c48SMatthew N. Dodd } 13187f760c48SMatthew N. Dodd } 1319d1dd20beSSam Leffler RT_UNLOCK(rt0); 1320d1dd20beSSam Leffler } 1321d1dd20beSSam Leffler /* XXX why are we inspecting rmx_expire? */ 1322d1dd20beSSam Leffler error = (rt->rt_flags & RTF_REJECT) && 1323d1dd20beSSam Leffler (rt->rt_rmx.rmx_expire == 0 || 1324fe53256dSAndre Oppermann time_uptime < rt->rt_rmx.rmx_expire); 13259bd8ca30SGleb Smirnoff if (error) { 1326d1dd20beSSam Leffler RT_UNLOCK(rt); 13277f760c48SMatthew N. Dodd senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 13287f760c48SMatthew N. Dodd } 1329530f95fcSGleb Smirnoff 13309bd8ca30SGleb Smirnoff *lrt = rt; 13317f760c48SMatthew N. Dodd *lrt0 = rt0; 1332d1dd20beSSam Leffler return (0); 1333d1dd20beSSam Leffler bad: 1334d1dd20beSSam Leffler /* NB: lrt and lrt0 should not be interpreted if error is non-zero */ 13357f760c48SMatthew N. Dodd return (error); 1336d1dd20beSSam Leffler #undef senderr 13377f760c48SMatthew N. Dodd } 13387f760c48SMatthew N. Dodd 13396a800098SYoshinobu Inoue /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */ 13406a800098SYoshinobu Inoue SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0); 1341