1df8bae1dSRodney W. Grimes /* 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 50df8bae1dSRodney W. Grimes #define SA(p) ((struct sockaddr *)(p)) 51df8bae1dSRodney W. Grimes 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 61f708ef1bSPoul-Henning Kamp static void 62d1dd20beSSam Leffler rtable_init(void **table) 63df8bae1dSRodney W. Grimes { 64df8bae1dSRodney W. Grimes struct domain *dom; 65df8bae1dSRodney W. Grimes for (dom = domains; dom; dom = dom->dom_next) 66df8bae1dSRodney W. Grimes if (dom->dom_rtattach) 67df8bae1dSRodney W. Grimes dom->dom_rtattach(&table[dom->dom_family], 68df8bae1dSRodney W. Grimes dom->dom_rtoffset); 69df8bae1dSRodney W. Grimes } 70df8bae1dSRodney W. Grimes 712eb5613fSLuigi Rizzo static void 722eb5613fSLuigi Rizzo route_init(void) 73df8bae1dSRodney W. Grimes { 74df8bae1dSRodney W. Grimes rn_init(); /* initialize all zeroes, all ones, mask table */ 75df8bae1dSRodney W. Grimes rtable_init((void **)rt_tables); 76df8bae1dSRodney W. Grimes } 77df8bae1dSRodney W. Grimes 78df8bae1dSRodney W. Grimes /* 79df8bae1dSRodney W. Grimes * Packet routing routines. 80df8bae1dSRodney W. Grimes */ 81df8bae1dSRodney W. Grimes void 82d1dd20beSSam Leffler rtalloc(struct route *ro) 83df8bae1dSRodney W. Grimes { 8468f956b8SJohn Polstra rtalloc_ign(ro, 0UL); 85df8bae1dSRodney W. Grimes } 86df8bae1dSRodney W. Grimes 87652082e6SGarrett Wollman void 88d1dd20beSSam Leffler rtalloc_ign(struct route *ro, u_long ignore) 89652082e6SGarrett Wollman { 9068f956b8SJohn Polstra struct rtentry *rt; 9168f956b8SJohn Polstra 9268f956b8SJohn Polstra if ((rt = ro->ro_rt) != NULL) { 9368f956b8SJohn Polstra if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP) 9468f956b8SJohn Polstra return; 9568f956b8SJohn Polstra RTFREE(rt); 9666810dd0SYoshinobu Inoue ro->ro_rt = NULL; 9768f956b8SJohn Polstra } 98652082e6SGarrett Wollman ro->ro_rt = rtalloc1(&ro->ro_dst, 1, ignore); 99d1dd20beSSam Leffler if (ro->ro_rt) 100d1dd20beSSam Leffler RT_UNLOCK(ro->ro_rt); 101652082e6SGarrett Wollman } 102652082e6SGarrett Wollman 103b0a76b88SJulian Elischer /* 104b0a76b88SJulian Elischer * Look up the route that matches the address given 105b0a76b88SJulian Elischer * Or, at least try.. Create a cloned route if needed. 106d1dd20beSSam Leffler * 107d1dd20beSSam Leffler * The returned route, if any, is locked. 108b0a76b88SJulian Elischer */ 109df8bae1dSRodney W. Grimes struct rtentry * 110d1dd20beSSam Leffler rtalloc1(struct sockaddr *dst, int report, u_long ignflags) 111df8bae1dSRodney W. Grimes { 112d1dd20beSSam Leffler struct radix_node_head *rnh = rt_tables[dst->sa_family]; 113d1dd20beSSam Leffler struct rtentry *rt; 114d1dd20beSSam Leffler struct radix_node *rn; 115d1dd20beSSam Leffler struct rtentry *newrt; 116df8bae1dSRodney W. Grimes struct rt_addrinfo info; 117995add1aSGarrett Wollman u_long nflags; 118d1dd20beSSam Leffler int err = 0, msgtype = RTM_MISS; 119df8bae1dSRodney W. Grimes 120d1dd20beSSam Leffler newrt = 0; 1213e6a836eSSam Leffler bzero(&info, sizeof(info)); 122b0a76b88SJulian Elischer /* 123b0a76b88SJulian Elischer * Look up the address in the table for that Address Family 124b0a76b88SJulian Elischer */ 125956b0b65SJeffrey Hsu if (rnh == NULL) { 126956b0b65SJeffrey Hsu rtstat.rts_unreach++; 127956b0b65SJeffrey Hsu goto miss2; 128956b0b65SJeffrey Hsu } 129956b0b65SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rnh); 130d1dd20beSSam Leffler if ((rn = rnh->rnh_matchaddr(dst, rnh)) && 131d1dd20beSSam Leffler (rn->rn_flags & RNF_ROOT) == 0) { 132b0a76b88SJulian Elischer /* 133b0a76b88SJulian Elischer * If we find it and it's not the root node, then 134b0a76b88SJulian Elischer * get a refernce on the rtentry associated. 135b0a76b88SJulian Elischer */ 136df8bae1dSRodney W. Grimes newrt = rt = (struct rtentry *)rn; 137995add1aSGarrett Wollman nflags = rt->rt_flags & ~ignflags; 13826d02ca7SAndre Oppermann if (report && (nflags & RTF_CLONING)) { 139b0a76b88SJulian Elischer /* 140b0a76b88SJulian Elischer * We are apparently adding (report = 0 in delete). 141b0a76b88SJulian Elischer * If it requires that it be cloned, do so. 142b0a76b88SJulian Elischer * (This implies it wasn't a HOST route.) 143b0a76b88SJulian Elischer */ 144df8bae1dSRodney W. Grimes err = rtrequest(RTM_RESOLVE, dst, SA(0), 145df8bae1dSRodney W. Grimes SA(0), 0, &newrt); 146df8bae1dSRodney W. Grimes if (err) { 147b0a76b88SJulian Elischer /* 148b0a76b88SJulian Elischer * If the cloning didn't succeed, maybe 149b0a76b88SJulian Elischer * what we have will do. Return that. 150b0a76b88SJulian Elischer */ 151d1dd20beSSam Leffler newrt = rt; /* existing route */ 152d1dd20beSSam Leffler RT_LOCK(newrt); 1537138d65cSSam Leffler RT_ADDREF(newrt); 154df8bae1dSRodney W. Grimes goto miss; 155df8bae1dSRodney W. Grimes } 156d1dd20beSSam Leffler KASSERT(newrt, ("no route and no error")); 157d1dd20beSSam Leffler RT_LOCK(newrt); 158d1dd20beSSam Leffler if (newrt->rt_flags & RTF_XRESOLVE) { 159b0a76b88SJulian Elischer /* 160b0a76b88SJulian Elischer * If the new route specifies it be 161b0a76b88SJulian Elischer * externally resolved, then go do that. 162b0a76b88SJulian Elischer */ 163df8bae1dSRodney W. Grimes msgtype = RTM_RESOLVE; 164df8bae1dSRodney W. Grimes goto miss; 165df8bae1dSRodney W. Grimes } 1668071913dSRuslan Ermilov /* Inform listeners of the new route. */ 167d1dd20beSSam Leffler info.rti_info[RTAX_DST] = rt_key(newrt); 168d1dd20beSSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(newrt); 169d1dd20beSSam Leffler info.rti_info[RTAX_GATEWAY] = newrt->rt_gateway; 170d1dd20beSSam Leffler if (newrt->rt_ifp != NULL) { 1718071913dSRuslan Ermilov info.rti_info[RTAX_IFP] = 1729b98ee2cSLuigi Rizzo ifaddr_byindex(newrt->rt_ifp->if_index)->ifa_addr; 173d1dd20beSSam Leffler info.rti_info[RTAX_IFA] = newrt->rt_ifa->ifa_addr; 1748071913dSRuslan Ermilov } 175d1dd20beSSam Leffler rt_missmsg(RTM_ADD, &info, newrt->rt_flags, 0); 176d1dd20beSSam Leffler } else { 177d1dd20beSSam Leffler KASSERT(rt == newrt, ("locking wrong route")); 178d1dd20beSSam Leffler RT_LOCK(newrt); 1797138d65cSSam Leffler RT_ADDREF(newrt); 180d1dd20beSSam Leffler } 181956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 182df8bae1dSRodney W. Grimes } else { 183b0a76b88SJulian Elischer /* 184b0a76b88SJulian Elischer * Either we hit the root or couldn't find any match, 185b0a76b88SJulian Elischer * Which basically means 186b0a76b88SJulian Elischer * "caint get there frm here" 187b0a76b88SJulian Elischer */ 188df8bae1dSRodney W. Grimes rtstat.rts_unreach++; 189956b0b65SJeffrey Hsu miss: 190956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 191956b0b65SJeffrey Hsu miss2: if (report) { 192b0a76b88SJulian Elischer /* 193b0a76b88SJulian Elischer * If required, report the failure to the supervising 194b0a76b88SJulian Elischer * Authorities. 195b0a76b88SJulian Elischer * For a delete, this is not an error. (report == 0) 196b0a76b88SJulian Elischer */ 197df8bae1dSRodney W. Grimes info.rti_info[RTAX_DST] = dst; 198df8bae1dSRodney W. Grimes rt_missmsg(msgtype, &info, 0, err); 199df8bae1dSRodney W. Grimes } 200df8bae1dSRodney W. Grimes } 201d1dd20beSSam Leffler if (newrt) 202d1dd20beSSam Leffler RT_LOCK_ASSERT(newrt); 203df8bae1dSRodney W. Grimes return (newrt); 204df8bae1dSRodney W. Grimes } 205df8bae1dSRodney W. Grimes 206499676dfSJulian Elischer /* 207499676dfSJulian Elischer * Remove a reference count from an rtentry. 208499676dfSJulian Elischer * If the count gets low enough, take it out of the routing table 209499676dfSJulian Elischer */ 210df8bae1dSRodney W. Grimes void 211d1dd20beSSam Leffler rtfree(struct rtentry *rt) 212df8bae1dSRodney W. Grimes { 213499676dfSJulian Elischer /* 214499676dfSJulian Elischer * find the tree for that address family 215499676dfSJulian Elischer */ 216956b0b65SJeffrey Hsu struct radix_node_head *rnh = rt_tables[rt_key(rt)->sa_family]; 217df8bae1dSRodney W. Grimes 2183545b048SGarrett Wollman if (rt == 0 || rnh == 0) 219df8bae1dSRodney W. Grimes panic("rtfree"); 220499676dfSJulian Elischer 221d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 222d1dd20beSSam Leffler 223499676dfSJulian Elischer /* 224499676dfSJulian Elischer * decrement the reference count by one and if it reaches 0, 225499676dfSJulian Elischer * and there is a close function defined, call the close function 226499676dfSJulian Elischer */ 2277138d65cSSam Leffler RT_REMREF(rt); 2287138d65cSSam Leffler if (rt->rt_refcnt > 0) 229d1dd20beSSam Leffler goto done; 2309c63e9dbSSam Leffler 2319c63e9dbSSam Leffler /* 2329c63e9dbSSam Leffler * On last reference give the "close method" a chance 2339c63e9dbSSam Leffler * to cleanup private state. This also permits (for 2349c63e9dbSSam Leffler * IPv4 and IPv6) a chance to decide if the routing table 2359c63e9dbSSam Leffler * entry should be purged immediately or at a later time. 2369c63e9dbSSam Leffler * When an immediate purge is to happen the close routine 2379c63e9dbSSam Leffler * typically calls rtexpunge which clears the RTF_UP flag 2389c63e9dbSSam Leffler * on the entry so that the code below reclaims the storage. 2399c63e9dbSSam Leffler */ 240d1dd20beSSam Leffler if (rt->rt_refcnt == 0 && rnh->rnh_close) 2415c2dae8eSGarrett Wollman rnh->rnh_close((struct radix_node *)rt, rnh); 242499676dfSJulian Elischer 243499676dfSJulian Elischer /* 244499676dfSJulian Elischer * If we are no longer "up" (and ref == 0) 245499676dfSJulian Elischer * then we can free the resources associated 246499676dfSJulian Elischer * with the route. 247499676dfSJulian Elischer */ 248d1dd20beSSam Leffler if ((rt->rt_flags & RTF_UP) == 0) { 249df8bae1dSRodney W. Grimes if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 250df8bae1dSRodney W. Grimes panic ("rtfree 2"); 251499676dfSJulian Elischer /* 252499676dfSJulian Elischer * the rtentry must have been removed from the routing table 253499676dfSJulian Elischer * so it is represented in rttrash.. remove that now. 254499676dfSJulian Elischer */ 255df8bae1dSRodney W. Grimes rttrash--; 256499676dfSJulian Elischer #ifdef DIAGNOSTIC 257df8bae1dSRodney W. Grimes if (rt->rt_refcnt < 0) { 258623ae52eSPoul-Henning Kamp printf("rtfree: %p not freed (neg refs)\n", rt); 259d1dd20beSSam Leffler goto done; 260df8bae1dSRodney W. Grimes } 261499676dfSJulian Elischer #endif 262499676dfSJulian Elischer /* 263499676dfSJulian Elischer * release references on items we hold them on.. 264499676dfSJulian Elischer * e.g other routes and ifaddrs. 265499676dfSJulian Elischer */ 26619fc74fbSJeffrey Hsu if (rt->rt_ifa) 26719fc74fbSJeffrey Hsu IFAFREE(rt->rt_ifa); 268d1dd20beSSam Leffler rt->rt_parent = NULL; /* NB: no refcnt on parent */ 269499676dfSJulian Elischer 270499676dfSJulian Elischer /* 271499676dfSJulian Elischer * The key is separatly alloc'd so free it (see rt_setgate()). 272499676dfSJulian Elischer * This also frees the gateway, as they are always malloc'd 273499676dfSJulian Elischer * together. 274499676dfSJulian Elischer */ 275df8bae1dSRodney W. Grimes Free(rt_key(rt)); 276499676dfSJulian Elischer 277499676dfSJulian Elischer /* 278499676dfSJulian Elischer * and the rtentry itself of course 279499676dfSJulian Elischer */ 280d1dd20beSSam Leffler RT_LOCK_DESTROY(rt); 281df8bae1dSRodney W. Grimes Free(rt); 282d1dd20beSSam Leffler return; 283df8bae1dSRodney W. Grimes } 284d1dd20beSSam Leffler done: 285d1dd20beSSam Leffler RT_UNLOCK(rt); 286df8bae1dSRodney W. Grimes } 287df8bae1dSRodney W. Grimes 288956b0b65SJeffrey Hsu /* compare two sockaddr structures */ 289956b0b65SJeffrey Hsu #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0) 290df8bae1dSRodney W. Grimes 291df8bae1dSRodney W. Grimes /* 292df8bae1dSRodney W. Grimes * Force a routing table entry to the specified 293df8bae1dSRodney W. Grimes * destination to go through the given gateway. 294df8bae1dSRodney W. Grimes * Normally called as a result of a routing redirect 295df8bae1dSRodney W. Grimes * message from the network layer. 296df8bae1dSRodney W. Grimes */ 29726f9a767SRodney W. Grimes void 298d1dd20beSSam Leffler rtredirect(struct sockaddr *dst, 299d1dd20beSSam Leffler struct sockaddr *gateway, 300d1dd20beSSam Leffler struct sockaddr *netmask, 301d1dd20beSSam Leffler int flags, 302d1dd20beSSam Leffler struct sockaddr *src) 303df8bae1dSRodney W. Grimes { 3048071913dSRuslan Ermilov struct rtentry *rt; 305df8bae1dSRodney W. Grimes int error = 0; 306df8bae1dSRodney W. Grimes short *stat = 0; 307df8bae1dSRodney W. Grimes struct rt_addrinfo info; 308df8bae1dSRodney W. Grimes struct ifaddr *ifa; 309df8bae1dSRodney W. Grimes 310df8bae1dSRodney W. Grimes /* verify the gateway is directly reachable */ 311df8bae1dSRodney W. Grimes if ((ifa = ifa_ifwithnet(gateway)) == 0) { 312df8bae1dSRodney W. Grimes error = ENETUNREACH; 313df8bae1dSRodney W. Grimes goto out; 314df8bae1dSRodney W. Grimes } 315d1dd20beSSam Leffler rt = rtalloc1(dst, 0, 0UL); /* NB: rt is locked */ 316df8bae1dSRodney W. Grimes /* 317df8bae1dSRodney W. Grimes * If the redirect isn't from our current router for this dst, 318df8bae1dSRodney W. Grimes * it's either old or wrong. If it redirects us to ourselves, 319df8bae1dSRodney W. Grimes * we have a routing loop, perhaps as a result of an interface 320df8bae1dSRodney W. Grimes * going down recently. 321df8bae1dSRodney W. Grimes */ 322df8bae1dSRodney W. Grimes if (!(flags & RTF_DONE) && rt && 323956b0b65SJeffrey Hsu (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) 324df8bae1dSRodney W. Grimes error = EINVAL; 325df8bae1dSRodney W. Grimes else if (ifa_ifwithaddr(gateway)) 326df8bae1dSRodney W. Grimes error = EHOSTUNREACH; 327df8bae1dSRodney W. Grimes if (error) 328df8bae1dSRodney W. Grimes goto done; 329df8bae1dSRodney W. Grimes /* 330df8bae1dSRodney W. Grimes * Create a new entry if we just got back a wildcard entry 331df8bae1dSRodney W. Grimes * or the the lookup failed. This is necessary for hosts 332df8bae1dSRodney W. Grimes * which use routing redirects generated by smart gateways 333df8bae1dSRodney W. Grimes * to dynamically build the routing tables. 334df8bae1dSRodney W. Grimes */ 335d1dd20beSSam Leffler if (rt == 0 || (rt_mask(rt) && rt_mask(rt)->sa_len < 2)) 336df8bae1dSRodney W. Grimes goto create; 337df8bae1dSRodney W. Grimes /* 338df8bae1dSRodney W. Grimes * Don't listen to the redirect if it's 339df8bae1dSRodney W. Grimes * for a route to an interface. 340df8bae1dSRodney W. Grimes */ 341df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_GATEWAY) { 342df8bae1dSRodney W. Grimes if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) { 343df8bae1dSRodney W. Grimes /* 344df8bae1dSRodney W. Grimes * Changing from route to net => route to host. 345df8bae1dSRodney W. Grimes * Create new route, rather than smashing route to net. 346df8bae1dSRodney W. Grimes */ 347df8bae1dSRodney W. Grimes create: 3488071913dSRuslan Ermilov if (rt) 3498071913dSRuslan Ermilov rtfree(rt); 350df8bae1dSRodney W. Grimes flags |= RTF_GATEWAY | RTF_DYNAMIC; 3518071913dSRuslan Ermilov bzero((caddr_t)&info, sizeof(info)); 3528071913dSRuslan Ermilov info.rti_info[RTAX_DST] = dst; 3538071913dSRuslan Ermilov info.rti_info[RTAX_GATEWAY] = gateway; 3548071913dSRuslan Ermilov info.rti_info[RTAX_NETMASK] = netmask; 3558071913dSRuslan Ermilov info.rti_ifa = ifa; 3568071913dSRuslan Ermilov info.rti_flags = flags; 3578071913dSRuslan Ermilov rt = NULL; 3588071913dSRuslan Ermilov error = rtrequest1(RTM_ADD, &info, &rt); 359d1dd20beSSam Leffler if (rt != NULL) { 3604de5d90cSSam Leffler RT_LOCK(rt); 3618071913dSRuslan Ermilov flags = rt->rt_flags; 362d1dd20beSSam Leffler } 363df8bae1dSRodney W. Grimes stat = &rtstat.rts_dynamic; 364df8bae1dSRodney W. Grimes } else { 365df8bae1dSRodney W. Grimes /* 366df8bae1dSRodney W. Grimes * Smash the current notion of the gateway to 367df8bae1dSRodney W. Grimes * this destination. Should check about netmask!!! 368df8bae1dSRodney W. Grimes */ 369df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_MODIFIED; 370df8bae1dSRodney W. Grimes flags |= RTF_MODIFIED; 371df8bae1dSRodney W. Grimes stat = &rtstat.rts_newgateway; 372499676dfSJulian Elischer /* 373499676dfSJulian Elischer * add the key and gateway (in one malloc'd chunk). 374499676dfSJulian Elischer */ 375df8bae1dSRodney W. Grimes rt_setgate(rt, rt_key(rt), gateway); 376df8bae1dSRodney W. Grimes } 377df8bae1dSRodney W. Grimes } else 378df8bae1dSRodney W. Grimes error = EHOSTUNREACH; 379df8bae1dSRodney W. Grimes done: 380d1dd20beSSam Leffler if (rt) 381df8bae1dSRodney W. Grimes rtfree(rt); 382df8bae1dSRodney W. Grimes out: 383df8bae1dSRodney W. Grimes if (error) 384df8bae1dSRodney W. Grimes rtstat.rts_badredirect++; 385df8bae1dSRodney W. Grimes else if (stat != NULL) 386df8bae1dSRodney W. Grimes (*stat)++; 387df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 388df8bae1dSRodney W. Grimes info.rti_info[RTAX_DST] = dst; 389df8bae1dSRodney W. Grimes info.rti_info[RTAX_GATEWAY] = gateway; 390df8bae1dSRodney W. Grimes info.rti_info[RTAX_NETMASK] = netmask; 391df8bae1dSRodney W. Grimes info.rti_info[RTAX_AUTHOR] = src; 392df8bae1dSRodney W. Grimes rt_missmsg(RTM_REDIRECT, &info, flags, error); 393df8bae1dSRodney W. Grimes } 394df8bae1dSRodney W. Grimes 395df8bae1dSRodney W. Grimes /* 396df8bae1dSRodney W. Grimes * Routing table ioctl interface. 397df8bae1dSRodney W. Grimes */ 398df8bae1dSRodney W. Grimes int 399d1dd20beSSam Leffler rtioctl(u_long req, caddr_t data) 400df8bae1dSRodney W. Grimes { 401623ae52eSPoul-Henning Kamp #ifdef INET 402f0068c4aSGarrett Wollman /* Multicast goop, grrr... */ 403bbb4330bSLuigi Rizzo return mrt_ioctl ? mrt_ioctl(req, data) : EOPNOTSUPP; 404623ae52eSPoul-Henning Kamp #else /* INET */ 405623ae52eSPoul-Henning Kamp return ENXIO; 406623ae52eSPoul-Henning Kamp #endif /* INET */ 407df8bae1dSRodney W. Grimes } 408df8bae1dSRodney W. Grimes 409df8bae1dSRodney W. Grimes struct ifaddr * 410d1dd20beSSam Leffler ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway) 411df8bae1dSRodney W. Grimes { 412df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 413d1dd20beSSam Leffler 414df8bae1dSRodney W. Grimes if ((flags & RTF_GATEWAY) == 0) { 415df8bae1dSRodney W. Grimes /* 416df8bae1dSRodney W. Grimes * If we are adding a route to an interface, 417df8bae1dSRodney W. Grimes * and the interface is a pt to pt link 418df8bae1dSRodney W. Grimes * we should search for the destination 419df8bae1dSRodney W. Grimes * as our clue to the interface. Otherwise 420df8bae1dSRodney W. Grimes * we can use the local address. 421df8bae1dSRodney W. Grimes */ 422df8bae1dSRodney W. Grimes ifa = 0; 4235df72964SGarrett Wollman if (flags & RTF_HOST) { 424df8bae1dSRodney W. Grimes ifa = ifa_ifwithdstaddr(dst); 4255df72964SGarrett Wollman } 426df8bae1dSRodney W. Grimes if (ifa == 0) 427df8bae1dSRodney W. Grimes ifa = ifa_ifwithaddr(gateway); 428df8bae1dSRodney W. Grimes } else { 429df8bae1dSRodney W. Grimes /* 430df8bae1dSRodney W. Grimes * If we are adding a route to a remote net 431df8bae1dSRodney W. Grimes * or host, the gateway may still be on the 432df8bae1dSRodney W. Grimes * other end of a pt to pt link. 433df8bae1dSRodney W. Grimes */ 434df8bae1dSRodney W. Grimes ifa = ifa_ifwithdstaddr(gateway); 435df8bae1dSRodney W. Grimes } 436df8bae1dSRodney W. Grimes if (ifa == 0) 437df8bae1dSRodney W. Grimes ifa = ifa_ifwithnet(gateway); 438df8bae1dSRodney W. Grimes if (ifa == 0) { 439ffdc316dSRuslan Ermilov struct rtentry *rt = rtalloc1(gateway, 0, 0UL); 440df8bae1dSRodney W. Grimes if (rt == 0) 441df8bae1dSRodney W. Grimes return (0); 4427138d65cSSam Leffler RT_REMREF(rt); 443d1dd20beSSam Leffler RT_UNLOCK(rt); 444df8bae1dSRodney W. Grimes if ((ifa = rt->rt_ifa) == 0) 445df8bae1dSRodney W. Grimes return (0); 446df8bae1dSRodney W. Grimes } 447df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family != dst->sa_family) { 448df8bae1dSRodney W. Grimes struct ifaddr *oifa = ifa; 449df8bae1dSRodney W. Grimes ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 450df8bae1dSRodney W. Grimes if (ifa == 0) 451df8bae1dSRodney W. Grimes ifa = oifa; 452df8bae1dSRodney W. Grimes } 453df8bae1dSRodney W. Grimes return (ifa); 454df8bae1dSRodney W. Grimes } 455df8bae1dSRodney W. Grimes 456929ddbbbSAlfred Perlstein static int rt_fixdelete(struct radix_node *, void *); 457929ddbbbSAlfred Perlstein static int rt_fixchange(struct radix_node *, void *); 458cd02a0b7SGarrett Wollman 459cd02a0b7SGarrett Wollman struct rtfc_arg { 460cd02a0b7SGarrett Wollman struct rtentry *rt0; 461cd02a0b7SGarrett Wollman struct radix_node_head *rnh; 462cd02a0b7SGarrett Wollman }; 46318e1f1f1SGarrett Wollman 464b0a76b88SJulian Elischer /* 465b0a76b88SJulian Elischer * Do appropriate manipulations of a routing tree given 466b0a76b88SJulian Elischer * all the bits of info needed 467b0a76b88SJulian Elischer */ 468df8bae1dSRodney W. Grimes int 469d1dd20beSSam Leffler rtrequest(int req, 470d1dd20beSSam Leffler struct sockaddr *dst, 471d1dd20beSSam Leffler struct sockaddr *gateway, 472d1dd20beSSam Leffler struct sockaddr *netmask, 473d1dd20beSSam Leffler int flags, 474d1dd20beSSam Leffler struct rtentry **ret_nrt) 475df8bae1dSRodney W. Grimes { 4768071913dSRuslan Ermilov struct rt_addrinfo info; 4778071913dSRuslan Ermilov 4788071913dSRuslan Ermilov bzero((caddr_t)&info, sizeof(info)); 4798071913dSRuslan Ermilov info.rti_flags = flags; 4808071913dSRuslan Ermilov info.rti_info[RTAX_DST] = dst; 4818071913dSRuslan Ermilov info.rti_info[RTAX_GATEWAY] = gateway; 4828071913dSRuslan Ermilov info.rti_info[RTAX_NETMASK] = netmask; 4838071913dSRuslan Ermilov return rtrequest1(req, &info, ret_nrt); 4848071913dSRuslan Ermilov } 4858071913dSRuslan Ermilov 4868071913dSRuslan Ermilov /* 4878071913dSRuslan Ermilov * These (questionable) definitions of apparent local variables apply 4888071913dSRuslan Ermilov * to the next two functions. XXXXXX!!! 4898071913dSRuslan Ermilov */ 4908071913dSRuslan Ermilov #define dst info->rti_info[RTAX_DST] 4918071913dSRuslan Ermilov #define gateway info->rti_info[RTAX_GATEWAY] 4928071913dSRuslan Ermilov #define netmask info->rti_info[RTAX_NETMASK] 4938071913dSRuslan Ermilov #define ifaaddr info->rti_info[RTAX_IFA] 4948071913dSRuslan Ermilov #define ifpaddr info->rti_info[RTAX_IFP] 4958071913dSRuslan Ermilov #define flags info->rti_flags 4968071913dSRuslan Ermilov 4978071913dSRuslan Ermilov int 498d1dd20beSSam Leffler rt_getifa(struct rt_addrinfo *info) 4998071913dSRuslan Ermilov { 5008071913dSRuslan Ermilov struct ifaddr *ifa; 5018071913dSRuslan Ermilov int error = 0; 5028071913dSRuslan Ermilov 5038071913dSRuslan Ermilov /* 5048071913dSRuslan Ermilov * ifp may be specified by sockaddr_dl 5058071913dSRuslan Ermilov * when protocol address is ambiguous. 5068071913dSRuslan Ermilov */ 5078071913dSRuslan Ermilov if (info->rti_ifp == NULL && ifpaddr != NULL && 5088071913dSRuslan Ermilov ifpaddr->sa_family == AF_LINK && 5098071913dSRuslan Ermilov (ifa = ifa_ifwithnet(ifpaddr)) != NULL) 5108071913dSRuslan Ermilov info->rti_ifp = ifa->ifa_ifp; 5118071913dSRuslan Ermilov if (info->rti_ifa == NULL && ifaaddr != NULL) 5128071913dSRuslan Ermilov info->rti_ifa = ifa_ifwithaddr(ifaaddr); 5138071913dSRuslan Ermilov if (info->rti_ifa == NULL) { 5148071913dSRuslan Ermilov struct sockaddr *sa; 5158071913dSRuslan Ermilov 5168071913dSRuslan Ermilov sa = ifaaddr != NULL ? ifaaddr : 5178071913dSRuslan Ermilov (gateway != NULL ? gateway : dst); 5188071913dSRuslan Ermilov if (sa != NULL && info->rti_ifp != NULL) 5198071913dSRuslan Ermilov info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp); 5208071913dSRuslan Ermilov else if (dst != NULL && gateway != NULL) 5218071913dSRuslan Ermilov info->rti_ifa = ifa_ifwithroute(flags, dst, gateway); 5228071913dSRuslan Ermilov else if (sa != NULL) 5238071913dSRuslan Ermilov info->rti_ifa = ifa_ifwithroute(flags, sa, sa); 5248071913dSRuslan Ermilov } 5258071913dSRuslan Ermilov if ((ifa = info->rti_ifa) != NULL) { 5268071913dSRuslan Ermilov if (info->rti_ifp == NULL) 5278071913dSRuslan Ermilov info->rti_ifp = ifa->ifa_ifp; 5288071913dSRuslan Ermilov } else 5298071913dSRuslan Ermilov error = ENETUNREACH; 5308071913dSRuslan Ermilov return (error); 5318071913dSRuslan Ermilov } 5328071913dSRuslan Ermilov 5339c63e9dbSSam Leffler /* 5349c63e9dbSSam Leffler * Expunges references to a route that's about to be reclaimed. 5359c63e9dbSSam Leffler * The route must be locked. 5369c63e9dbSSam Leffler */ 5379c63e9dbSSam Leffler int 5389c63e9dbSSam Leffler rtexpunge(struct rtentry *rt) 5399c63e9dbSSam Leffler { 5409c63e9dbSSam Leffler struct radix_node *rn; 5419c63e9dbSSam Leffler struct radix_node_head *rnh; 5429c63e9dbSSam Leffler struct ifaddr *ifa; 5439c63e9dbSSam Leffler int error = 0; 5449c63e9dbSSam Leffler 5459c63e9dbSSam Leffler RT_LOCK_ASSERT(rt); 5469c63e9dbSSam Leffler #if 0 5479c63e9dbSSam Leffler /* 5489c63e9dbSSam Leffler * We cannot assume anything about the reference count 5499c63e9dbSSam Leffler * because protocols call us in many situations; often 5509c63e9dbSSam Leffler * before unwinding references to the table entry. 5519c63e9dbSSam Leffler */ 5529c63e9dbSSam Leffler KASSERT(rt->rt_refcnt <= 1, ("bogus refcnt %ld", rt->rt_refcnt)); 5539c63e9dbSSam Leffler #endif 5549c63e9dbSSam Leffler /* 5559c63e9dbSSam Leffler * Find the correct routing tree to use for this Address Family 5569c63e9dbSSam Leffler */ 5579c63e9dbSSam Leffler rnh = rt_tables[rt_key(rt)->sa_family]; 5589c63e9dbSSam Leffler if (rnh == 0) 5599c63e9dbSSam Leffler return (EAFNOSUPPORT); 5609c63e9dbSSam Leffler 5619c63e9dbSSam Leffler RADIX_NODE_HEAD_LOCK(rnh); 5629c63e9dbSSam Leffler 5639c63e9dbSSam Leffler /* 5649c63e9dbSSam Leffler * Remove the item from the tree; it should be there, 5659c63e9dbSSam Leffler * but when callers invoke us blindly it may not (sigh). 5669c63e9dbSSam Leffler */ 5679c63e9dbSSam Leffler rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh); 5689c63e9dbSSam Leffler if (rn == 0) { 5699c63e9dbSSam Leffler error = ESRCH; 5709c63e9dbSSam Leffler goto bad; 5719c63e9dbSSam Leffler } 5729c63e9dbSSam Leffler KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0, 5739c63e9dbSSam Leffler ("unexpected flags 0x%x", rn->rn_flags)); 5749c63e9dbSSam Leffler KASSERT(rt == (struct rtentry *)rn, 5759c63e9dbSSam Leffler ("lookup mismatch, rt %p rn %p", rt, rn)); 5769c63e9dbSSam Leffler 5779c63e9dbSSam Leffler rt->rt_flags &= ~RTF_UP; 5789c63e9dbSSam Leffler 5799c63e9dbSSam Leffler /* 5809c63e9dbSSam Leffler * Now search what's left of the subtree for any cloned 5819c63e9dbSSam Leffler * routes which might have been formed from this node. 5829c63e9dbSSam Leffler */ 58326d02ca7SAndre Oppermann if ((rt->rt_flags & RTF_CLONING) && rt_mask(rt)) 5849c63e9dbSSam Leffler rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), 5859c63e9dbSSam Leffler rt_fixdelete, rt); 5869c63e9dbSSam Leffler 5879c63e9dbSSam Leffler /* 5889c63e9dbSSam Leffler * Remove any external references we may have. 5899c63e9dbSSam Leffler * This might result in another rtentry being freed if 5909c63e9dbSSam Leffler * we held its last reference. 5919c63e9dbSSam Leffler */ 5929c63e9dbSSam Leffler if (rt->rt_gwroute) { 5939c63e9dbSSam Leffler struct rtentry *gwrt = rt->rt_gwroute; 5949c63e9dbSSam Leffler RTFREE(gwrt); 5959c63e9dbSSam Leffler rt->rt_gwroute = 0; 5969c63e9dbSSam Leffler } 5979c63e9dbSSam Leffler 5989c63e9dbSSam Leffler /* 5999c63e9dbSSam Leffler * Give the protocol a chance to keep things in sync. 6009c63e9dbSSam Leffler */ 6019c63e9dbSSam Leffler if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) { 6029c63e9dbSSam Leffler struct rt_addrinfo info; 6039c63e9dbSSam Leffler 6049c63e9dbSSam Leffler bzero((caddr_t)&info, sizeof(info)); 6059c63e9dbSSam Leffler info.rti_flags = rt->rt_flags; 6069c63e9dbSSam Leffler info.rti_info[RTAX_DST] = rt_key(rt); 6079c63e9dbSSam Leffler info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 6089c63e9dbSSam Leffler info.rti_info[RTAX_NETMASK] = rt_mask(rt); 6099c63e9dbSSam Leffler ifa->ifa_rtrequest(RTM_DELETE, rt, &info); 6109c63e9dbSSam Leffler } 6119c63e9dbSSam Leffler 6129c63e9dbSSam Leffler /* 6139c63e9dbSSam Leffler * one more rtentry floating around that is not 6149c63e9dbSSam Leffler * linked to the routing table. 6159c63e9dbSSam Leffler */ 6169c63e9dbSSam Leffler rttrash++; 6179c63e9dbSSam Leffler bad: 6189c63e9dbSSam Leffler RADIX_NODE_HEAD_UNLOCK(rnh); 6199c63e9dbSSam Leffler return (error); 6209c63e9dbSSam Leffler } 6219c63e9dbSSam Leffler 6228071913dSRuslan Ermilov int 623d1dd20beSSam Leffler rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt) 6248071913dSRuslan Ermilov { 625d1dd20beSSam Leffler int error = 0; 626df8bae1dSRodney W. Grimes register struct rtentry *rt; 627df8bae1dSRodney W. Grimes register struct radix_node *rn; 628df8bae1dSRodney W. Grimes register struct radix_node_head *rnh; 629df8bae1dSRodney W. Grimes struct ifaddr *ifa; 630df8bae1dSRodney W. Grimes struct sockaddr *ndst; 631df8bae1dSRodney W. Grimes #define senderr(x) { error = x ; goto bad; } 632df8bae1dSRodney W. Grimes 633b0a76b88SJulian Elischer /* 634b0a76b88SJulian Elischer * Find the correct routing tree to use for this Address Family 635b0a76b88SJulian Elischer */ 636d1dd20beSSam Leffler rnh = rt_tables[dst->sa_family]; 637d1dd20beSSam Leffler if (rnh == 0) 638983985c1SJeffrey Hsu return (EAFNOSUPPORT); 639956b0b65SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rnh); 640b0a76b88SJulian Elischer /* 641b0a76b88SJulian Elischer * If we are adding a host route then we don't want to put 64266953138SRuslan Ermilov * a netmask in the tree, nor do we want to clone it. 643b0a76b88SJulian Elischer */ 64466953138SRuslan Ermilov if (flags & RTF_HOST) { 645df8bae1dSRodney W. Grimes netmask = 0; 64626d02ca7SAndre Oppermann flags &= ~RTF_CLONING; 64766953138SRuslan Ermilov } 648df8bae1dSRodney W. Grimes switch (req) { 649df8bae1dSRodney W. Grimes case RTM_DELETE: 650b0a76b88SJulian Elischer /* 651b0a76b88SJulian Elischer * Remove the item from the tree and return it. 652b0a76b88SJulian Elischer * Complain if it is not there and do no more processing. 653b0a76b88SJulian Elischer */ 654d1dd20beSSam Leffler rn = rnh->rnh_deladdr(dst, netmask, rnh); 655d1dd20beSSam Leffler if (rn == 0) 656df8bae1dSRodney W. Grimes senderr(ESRCH); 657df8bae1dSRodney W. Grimes if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 658df8bae1dSRodney W. Grimes panic ("rtrequest delete"); 659df8bae1dSRodney W. Grimes rt = (struct rtentry *)rn; 660d1dd20beSSam Leffler RT_LOCK(rt); 6617138d65cSSam Leffler RT_ADDREF(rt); 66271eba915SRuslan Ermilov rt->rt_flags &= ~RTF_UP; 663c2bed6a3SGarrett Wollman 664c2bed6a3SGarrett Wollman /* 665c2bed6a3SGarrett Wollman * Now search what's left of the subtree for any cloned 666c2bed6a3SGarrett Wollman * routes which might have been formed from this node. 667c2bed6a3SGarrett Wollman */ 66826d02ca7SAndre Oppermann if ((rt->rt_flags & RTF_CLONING) && 669089cdfadSRuslan Ermilov rt_mask(rt)) { 670089cdfadSRuslan Ermilov rnh->rnh_walktree_from(rnh, dst, rt_mask(rt), 671c2bed6a3SGarrett Wollman rt_fixdelete, rt); 672c2bed6a3SGarrett Wollman } 6733545b048SGarrett Wollman 674b0a76b88SJulian Elischer /* 675b0a76b88SJulian Elischer * Remove any external references we may have. 676b0a76b88SJulian Elischer * This might result in another rtentry being freed if 677dc733423SDag-Erling Smørgrav * we held its last reference. 678b0a76b88SJulian Elischer */ 6796ac3b69dSBill Fenner if (rt->rt_gwroute) { 680d1dd20beSSam Leffler struct rtentry *gwrt = rt->rt_gwroute; 681d1dd20beSSam Leffler RTFREE(gwrt); 682d1dd20beSSam Leffler rt->rt_gwroute = 0; 6836ac3b69dSBill Fenner } 6846ac3b69dSBill Fenner 6853545b048SGarrett Wollman /* 686499676dfSJulian Elischer * give the protocol a chance to keep things in sync. 687b0a76b88SJulian Elischer */ 688df8bae1dSRodney W. Grimes if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) 6898071913dSRuslan Ermilov ifa->ifa_rtrequest(RTM_DELETE, rt, info); 690499676dfSJulian Elischer 691b0a76b88SJulian Elischer /* 692499676dfSJulian Elischer * one more rtentry floating around that is not 693499676dfSJulian Elischer * linked to the routing table. 694499676dfSJulian Elischer */ 695499676dfSJulian Elischer rttrash++; 696499676dfSJulian Elischer 697499676dfSJulian Elischer /* 698499676dfSJulian Elischer * If the caller wants it, then it can have it, 699499676dfSJulian Elischer * but it's up to it to free the rtentry as we won't be 700499676dfSJulian Elischer * doing it. 701b0a76b88SJulian Elischer */ 702d1dd20beSSam Leffler if (ret_nrt) { 703df8bae1dSRodney W. Grimes *ret_nrt = rt; 704d1dd20beSSam Leffler RT_UNLOCK(rt); 705d1dd20beSSam Leffler } else 706d1dd20beSSam Leffler RTFREE_LOCKED(rt); 707df8bae1dSRodney W. Grimes break; 708df8bae1dSRodney W. Grimes 709df8bae1dSRodney W. Grimes case RTM_RESOLVE: 710df8bae1dSRodney W. Grimes if (ret_nrt == 0 || (rt = *ret_nrt) == 0) 711df8bae1dSRodney W. Grimes senderr(EINVAL); 712df8bae1dSRodney W. Grimes ifa = rt->rt_ifa; 713d1dd20beSSam Leffler /* XXX locking? */ 7143682d2baSDavid Greenman flags = rt->rt_flags & 71526d02ca7SAndre Oppermann ~(RTF_CLONING | RTF_STATIC); 716995add1aSGarrett Wollman flags |= RTF_WASCLONED; 717df8bae1dSRodney W. Grimes gateway = rt->rt_gateway; 718df8bae1dSRodney W. Grimes if ((netmask = rt->rt_genmask) == 0) 719df8bae1dSRodney W. Grimes flags |= RTF_HOST; 720df8bae1dSRodney W. Grimes goto makeroute; 721df8bae1dSRodney W. Grimes 722df8bae1dSRodney W. Grimes case RTM_ADD: 7235df72964SGarrett Wollman if ((flags & RTF_GATEWAY) && !gateway) 7245df72964SGarrett Wollman panic("rtrequest: GATEWAY but no gateway"); 7255df72964SGarrett Wollman 7268071913dSRuslan Ermilov if (info->rti_ifa == NULL && (error = rt_getifa(info))) 7278071913dSRuslan Ermilov senderr(error); 7288071913dSRuslan Ermilov ifa = info->rti_ifa; 7295df72964SGarrett Wollman 730df8bae1dSRodney W. Grimes makeroute: 731d1dd20beSSam Leffler R_Zalloc(rt, struct rtentry *, sizeof(*rt)); 732df8bae1dSRodney W. Grimes if (rt == 0) 733df8bae1dSRodney W. Grimes senderr(ENOBUFS); 734d1dd20beSSam Leffler RT_LOCK_INIT(rt); 735df8bae1dSRodney W. Grimes rt->rt_flags = RTF_UP | flags; 736499676dfSJulian Elischer /* 737499676dfSJulian Elischer * Add the gateway. Possibly re-malloc-ing the storage for it 738499676dfSJulian Elischer * also add the rt_gwroute if possible. 739499676dfSJulian Elischer */ 740d1dd20beSSam Leffler RT_LOCK(rt); 741831a80b0SMatthew Dillon if ((error = rt_setgate(rt, dst, gateway)) != 0) { 742d1dd20beSSam Leffler RT_LOCK_DESTROY(rt); 743df8bae1dSRodney W. Grimes Free(rt); 744704b0666SBill Fenner senderr(error); 745df8bae1dSRodney W. Grimes } 746499676dfSJulian Elischer 747499676dfSJulian Elischer /* 748499676dfSJulian Elischer * point to the (possibly newly malloc'd) dest address. 749499676dfSJulian Elischer */ 750d1dd20beSSam Leffler ndst = (struct sockaddr *)rt_key(rt); 751499676dfSJulian Elischer 752499676dfSJulian Elischer /* 753499676dfSJulian Elischer * make sure it contains the value we want (masked if needed). 754499676dfSJulian Elischer */ 755df8bae1dSRodney W. Grimes if (netmask) { 756df8bae1dSRodney W. Grimes rt_maskedcopy(dst, ndst, netmask); 757df8bae1dSRodney W. Grimes } else 758df8bae1dSRodney W. Grimes Bcopy(dst, ndst, dst->sa_len); 7598e718bb4SGarrett Wollman 7608e718bb4SGarrett Wollman /* 761499676dfSJulian Elischer * Note that we now have a reference to the ifa. 7628e718bb4SGarrett Wollman * This moved from below so that rnh->rnh_addaddr() can 763499676dfSJulian Elischer * examine the ifa and ifa->ifa_ifp if it so desires. 7648e718bb4SGarrett Wollman */ 76519fc74fbSJeffrey Hsu IFAREF(ifa); 7668e718bb4SGarrett Wollman rt->rt_ifa = ifa; 7678e718bb4SGarrett Wollman rt->rt_ifp = ifa->ifa_ifp; 7688e718bb4SGarrett Wollman 769d1dd20beSSam Leffler /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */ 770d1dd20beSSam Leffler rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes); 771df8bae1dSRodney W. Grimes if (rn == 0) { 772aca1a47cSGarrett Wollman struct rtentry *rt2; 773aca1a47cSGarrett Wollman /* 774aca1a47cSGarrett Wollman * Uh-oh, we already have one of these in the tree. 775aca1a47cSGarrett Wollman * We do a special hack: if the route that's already 77626d02ca7SAndre Oppermann * there was generated by the cloning mechanism 77726d02ca7SAndre Oppermann * then we just blow it away and retry the insertion 77826d02ca7SAndre Oppermann * of the new one. 779aca1a47cSGarrett Wollman */ 78026d02ca7SAndre Oppermann rt2 = rtalloc1(dst, 0, 0); 781aca1a47cSGarrett Wollman if (rt2 && rt2->rt_parent) { 7829c63e9dbSSam Leffler rtexpunge(rt2); 7839c63e9dbSSam Leffler RT_UNLOCK(rt2); 784d1dd20beSSam Leffler rn = rnh->rnh_addaddr(ndst, netmask, 785aca1a47cSGarrett Wollman rnh, rt->rt_nodes); 786fde327d6SGarrett Wollman } else if (rt2) { 787499676dfSJulian Elischer /* undo the extra ref we got */ 788d1dd20beSSam Leffler RTFREE_LOCKED(rt2); 789aca1a47cSGarrett Wollman } 790aca1a47cSGarrett Wollman } 791aca1a47cSGarrett Wollman 792499676dfSJulian Elischer /* 793499676dfSJulian Elischer * If it still failed to go into the tree, 794499676dfSJulian Elischer * then un-make it (this should be a function) 795499676dfSJulian Elischer */ 796aca1a47cSGarrett Wollman if (rn == 0) { 797df8bae1dSRodney W. Grimes if (rt->rt_gwroute) 798d1dd20beSSam Leffler RTFREE(rt->rt_gwroute); 799d1dd20beSSam Leffler if (rt->rt_ifa) 8008e718bb4SGarrett Wollman IFAFREE(rt->rt_ifa); 801df8bae1dSRodney W. Grimes Free(rt_key(rt)); 802d1dd20beSSam Leffler RT_LOCK_DESTROY(rt); 803df8bae1dSRodney W. Grimes Free(rt); 804df8bae1dSRodney W. Grimes senderr(EEXIST); 805df8bae1dSRodney W. Grimes } 806499676dfSJulian Elischer 807771edb14SGarrett Wollman rt->rt_parent = 0; 808771edb14SGarrett Wollman 809499676dfSJulian Elischer /* 810499676dfSJulian Elischer * If we got here from RESOLVE, then we are cloning 811499676dfSJulian Elischer * so clone the rest, and note that we 812499676dfSJulian Elischer * are a clone (and increment the parent's references) 813499676dfSJulian Elischer */ 814c2bed6a3SGarrett Wollman if (req == RTM_RESOLVE) { 815d1dd20beSSam Leffler KASSERT(ret_nrt && *ret_nrt, 816d1dd20beSSam Leffler ("no route to clone from")); 817df8bae1dSRodney W. Grimes rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */ 81854e84abbSMike Silbersack rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */ 81926d02ca7SAndre Oppermann if ((*ret_nrt)->rt_flags & RTF_CLONING) { 820d1dd20beSSam Leffler /* 821d1dd20beSSam Leffler * NB: We do not bump the refcnt on the parent 822d1dd20beSSam Leffler * entry under the assumption that it will 823d1dd20beSSam Leffler * remain so long as we do. This is 824d1dd20beSSam Leffler * important when deleting the parent route 825d1dd20beSSam Leffler * as this operation requires traversing 826d1dd20beSSam Leffler * the tree to delete all clones and futzing 827d1dd20beSSam Leffler * with refcnts requires us to double-lock 828d1dd20beSSam Leffler * parent through this back reference. 829d1dd20beSSam Leffler */ 830d1dd20beSSam Leffler rt->rt_parent = *ret_nrt; 831771edb14SGarrett Wollman } 83218e1f1f1SGarrett Wollman } 833499676dfSJulian Elischer 834499676dfSJulian Elischer /* 835499676dfSJulian Elischer * if this protocol has something to add to this then 836499676dfSJulian Elischer * allow it to do that as well. 837499676dfSJulian Elischer */ 838df8bae1dSRodney W. Grimes if (ifa->ifa_rtrequest) 8398071913dSRuslan Ermilov ifa->ifa_rtrequest(req, rt, info); 840499676dfSJulian Elischer 841cd02a0b7SGarrett Wollman /* 842cd02a0b7SGarrett Wollman * We repeat the same procedure from rt_setgate() here because 843cd02a0b7SGarrett Wollman * it doesn't fire when we call it there because the node 844cd02a0b7SGarrett Wollman * hasn't been added to the tree yet. 845cd02a0b7SGarrett Wollman */ 84636fea5deSRuslan Ermilov if (req == RTM_ADD && 84736fea5deSRuslan Ermilov !(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) { 848cd02a0b7SGarrett Wollman struct rtfc_arg arg; 849cd02a0b7SGarrett Wollman arg.rnh = rnh; 850cd02a0b7SGarrett Wollman arg.rt0 = rt; 851cd02a0b7SGarrett Wollman rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), 852cd02a0b7SGarrett Wollman rt_fixchange, &arg); 853cd02a0b7SGarrett Wollman } 854cd02a0b7SGarrett Wollman 855499676dfSJulian Elischer /* 856499676dfSJulian Elischer * actually return a resultant rtentry and 857499676dfSJulian Elischer * give the caller a single reference. 858499676dfSJulian Elischer */ 859df8bae1dSRodney W. Grimes if (ret_nrt) { 860df8bae1dSRodney W. Grimes *ret_nrt = rt; 8617138d65cSSam Leffler RT_ADDREF(rt); 862df8bae1dSRodney W. Grimes } 863d1dd20beSSam Leffler RT_UNLOCK(rt); 864df8bae1dSRodney W. Grimes break; 8658071913dSRuslan Ermilov default: 8668071913dSRuslan Ermilov error = EOPNOTSUPP; 867df8bae1dSRodney W. Grimes } 868df8bae1dSRodney W. Grimes bad: 869956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 870df8bae1dSRodney W. Grimes return (error); 871d1dd20beSSam Leffler #undef senderr 872d1dd20beSSam Leffler } 873d1dd20beSSam Leffler 8748071913dSRuslan Ermilov #undef dst 8758071913dSRuslan Ermilov #undef gateway 8768071913dSRuslan Ermilov #undef netmask 8778071913dSRuslan Ermilov #undef ifaaddr 8788071913dSRuslan Ermilov #undef ifpaddr 8798071913dSRuslan Ermilov #undef flags 880df8bae1dSRodney W. Grimes 88118e1f1f1SGarrett Wollman /* 88218e1f1f1SGarrett Wollman * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family'' 88318e1f1f1SGarrett Wollman * (i.e., the routes related to it by the operation of cloning). This 884c2bed6a3SGarrett Wollman * routine is iterated over all potential former-child-routes by way of 885c2bed6a3SGarrett Wollman * rnh->rnh_walktree_from() above, and those that actually are children of 886c2bed6a3SGarrett Wollman * the late parent (passed in as VP here) are themselves deleted. 88718e1f1f1SGarrett Wollman */ 888c2bed6a3SGarrett Wollman static int 889d1dd20beSSam Leffler rt_fixdelete(struct radix_node *rn, void *vp) 89018e1f1f1SGarrett Wollman { 891c2bed6a3SGarrett Wollman struct rtentry *rt = (struct rtentry *)rn; 892c2bed6a3SGarrett Wollman struct rtentry *rt0 = vp; 89318e1f1f1SGarrett Wollman 89436fea5deSRuslan Ermilov if (rt->rt_parent == rt0 && 89526d02ca7SAndre Oppermann !(rt->rt_flags & (RTF_PINNED | RTF_CLONING))) { 896c2bed6a3SGarrett Wollman return rtrequest(RTM_DELETE, rt_key(rt), 89718e1f1f1SGarrett Wollman (struct sockaddr *)0, rt_mask(rt), 89818e1f1f1SGarrett Wollman rt->rt_flags, (struct rtentry **)0); 89918e1f1f1SGarrett Wollman } 900c2bed6a3SGarrett Wollman return 0; 90118e1f1f1SGarrett Wollman } 90218e1f1f1SGarrett Wollman 903cd02a0b7SGarrett Wollman /* 904cd02a0b7SGarrett Wollman * This routine is called from rt_setgate() to do the analogous thing for 905cd02a0b7SGarrett Wollman * adds and changes. There is the added complication in this case of a 906cd02a0b7SGarrett Wollman * middle insert; i.e., insertion of a new network route between an older 907cd02a0b7SGarrett Wollman * network route and (cloned) host routes. For this reason, a simple check 908cd02a0b7SGarrett Wollman * of rt->rt_parent is insufficient; each candidate route must be tested 909cd02a0b7SGarrett Wollman * against the (mask, value) of the new route (passed as before in vp) 9109a701516SHajimu UMEMOTO * to see if the new route matches it. 911cd02a0b7SGarrett Wollman * 912cd02a0b7SGarrett Wollman * XXX - it may be possible to do fixdelete() for changes and reserve this 913cd02a0b7SGarrett Wollman * routine just for adds. I'm not sure why I thought it was necessary to do 914cd02a0b7SGarrett Wollman * changes this way. 915cd02a0b7SGarrett Wollman */ 916cd02a0b7SGarrett Wollman #ifdef DEBUG 917303b270bSEivind Eklund static int rtfcdebug = 0; 918cd02a0b7SGarrett Wollman #endif 919cd02a0b7SGarrett Wollman 920cd02a0b7SGarrett Wollman static int 921d1dd20beSSam Leffler rt_fixchange(struct radix_node *rn, void *vp) 922cd02a0b7SGarrett Wollman { 923cd02a0b7SGarrett Wollman struct rtentry *rt = (struct rtentry *)rn; 924cd02a0b7SGarrett Wollman struct rtfc_arg *ap = vp; 925cd02a0b7SGarrett Wollman struct rtentry *rt0 = ap->rt0; 926cd02a0b7SGarrett Wollman struct radix_node_head *rnh = ap->rnh; 9279a701516SHajimu UMEMOTO u_char *xk1, *xm1, *xk2, *xmp; 9289a701516SHajimu UMEMOTO int i, len, mlen; 929cd02a0b7SGarrett Wollman 930cd02a0b7SGarrett Wollman #ifdef DEBUG 931cd02a0b7SGarrett Wollman if (rtfcdebug) 932cd02a0b7SGarrett Wollman printf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0); 933cd02a0b7SGarrett Wollman #endif 934cd02a0b7SGarrett Wollman 93536fea5deSRuslan Ermilov if (!rt->rt_parent || 93626d02ca7SAndre Oppermann (rt->rt_flags & (RTF_PINNED | RTF_CLONING))) { 937cd02a0b7SGarrett Wollman #ifdef DEBUG 93836fea5deSRuslan Ermilov if(rtfcdebug) printf("no parent, pinned or cloning\n"); 939cd02a0b7SGarrett Wollman #endif 940cd02a0b7SGarrett Wollman return 0; 941cd02a0b7SGarrett Wollman } 942cd02a0b7SGarrett Wollman 943cd02a0b7SGarrett Wollman if (rt->rt_parent == rt0) { 944cd02a0b7SGarrett Wollman #ifdef DEBUG 945cd02a0b7SGarrett Wollman if(rtfcdebug) printf("parent match\n"); 946cd02a0b7SGarrett Wollman #endif 947cd02a0b7SGarrett Wollman return rtrequest(RTM_DELETE, rt_key(rt), 948cd02a0b7SGarrett Wollman (struct sockaddr *)0, rt_mask(rt), 949cd02a0b7SGarrett Wollman rt->rt_flags, (struct rtentry **)0); 950cd02a0b7SGarrett Wollman } 951cd02a0b7SGarrett Wollman 952cd02a0b7SGarrett Wollman /* 953cd02a0b7SGarrett Wollman * There probably is a function somewhere which does this... 954cd02a0b7SGarrett Wollman * if not, there should be. 955cd02a0b7SGarrett Wollman */ 956d1dd20beSSam Leffler len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len); 957cd02a0b7SGarrett Wollman 958cd02a0b7SGarrett Wollman xk1 = (u_char *)rt_key(rt0); 959cd02a0b7SGarrett Wollman xm1 = (u_char *)rt_mask(rt0); 960cd02a0b7SGarrett Wollman xk2 = (u_char *)rt_key(rt); 961cd02a0b7SGarrett Wollman 9629a701516SHajimu UMEMOTO /* avoid applying a less specific route */ 9639a701516SHajimu UMEMOTO xmp = (u_char *)rt_mask(rt->rt_parent); 964d1dd20beSSam Leffler mlen = rt_key(rt->rt_parent)->sa_len; 965d1dd20beSSam Leffler if (mlen > rt_key(rt0)->sa_len) { 9669a701516SHajimu UMEMOTO #ifdef DEBUG 9679a701516SHajimu UMEMOTO if (rtfcdebug) 9689a701516SHajimu UMEMOTO printf("rt_fixchange: inserting a less " 9699a701516SHajimu UMEMOTO "specific route\n"); 9709a701516SHajimu UMEMOTO #endif 9719a701516SHajimu UMEMOTO return 0; 9729a701516SHajimu UMEMOTO } 9739a701516SHajimu UMEMOTO for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++) { 9749a701516SHajimu UMEMOTO if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i]) { 9759a701516SHajimu UMEMOTO #ifdef DEBUG 9769a701516SHajimu UMEMOTO if (rtfcdebug) 9779a701516SHajimu UMEMOTO printf("rt_fixchange: inserting a less " 9789a701516SHajimu UMEMOTO "specific route\n"); 9799a701516SHajimu UMEMOTO #endif 9809a701516SHajimu UMEMOTO return 0; 9819a701516SHajimu UMEMOTO } 9829a701516SHajimu UMEMOTO } 9839a701516SHajimu UMEMOTO 9841a11e63eSGarrett Wollman for (i = rnh->rnh_treetop->rn_offset; i < len; i++) { 985cd02a0b7SGarrett Wollman if ((xk2[i] & xm1[i]) != xk1[i]) { 986cd02a0b7SGarrett Wollman #ifdef DEBUG 987cd02a0b7SGarrett Wollman if(rtfcdebug) printf("no match\n"); 988cd02a0b7SGarrett Wollman #endif 989cd02a0b7SGarrett Wollman return 0; 990cd02a0b7SGarrett Wollman } 991cd02a0b7SGarrett Wollman } 992cd02a0b7SGarrett Wollman 993cd02a0b7SGarrett Wollman /* 994cd02a0b7SGarrett Wollman * OK, this node is a clone, and matches the node currently being 995cd02a0b7SGarrett Wollman * changed/added under the node's mask. So, get rid of it. 996cd02a0b7SGarrett Wollman */ 997cd02a0b7SGarrett Wollman #ifdef DEBUG 998cd02a0b7SGarrett Wollman if(rtfcdebug) printf("deleting\n"); 999cd02a0b7SGarrett Wollman #endif 1000cd02a0b7SGarrett Wollman return rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, 1001cd02a0b7SGarrett Wollman rt_mask(rt), rt->rt_flags, (struct rtentry **)0); 1002cd02a0b7SGarrett Wollman } 1003cd02a0b7SGarrett Wollman 1004df8bae1dSRodney W. Grimes int 1005d1dd20beSSam Leffler rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate) 1006df8bae1dSRodney W. Grimes { 1007d1dd20beSSam Leffler /* XXX dst may be overwritten, can we move this to below */ 1008d1dd20beSSam Leffler struct radix_node_head *rnh = rt_tables[dst->sa_family]; 1009df8bae1dSRodney W. Grimes caddr_t new, old; 1010e74642dfSLuigi Rizzo int dlen = SA_SIZE(dst), glen = SA_SIZE(gate); 1011d1dd20beSSam Leffler 1012d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 1013df8bae1dSRodney W. Grimes 10141db1fffaSBill Fenner /* 10151db1fffaSBill Fenner * A host route with the destination equal to the gateway 10161db1fffaSBill Fenner * will interfere with keeping LLINFO in the routing 10171db1fffaSBill Fenner * table, so disallow it. 10181db1fffaSBill Fenner */ 1019d1dd20beSSam Leffler if (((rt->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) == 10201db1fffaSBill Fenner (RTF_HOST|RTF_GATEWAY)) && 1021d1dd20beSSam Leffler dst->sa_len == gate->sa_len && 1022d1dd20beSSam Leffler bcmp(dst, gate, dst->sa_len) == 0) { 10231db1fffaSBill Fenner /* 10241db1fffaSBill Fenner * The route might already exist if this is an RTM_CHANGE 10251db1fffaSBill Fenner * or a routing redirect, so try to delete it. 10261db1fffaSBill Fenner */ 1027d1dd20beSSam Leffler if (rt_key(rt)) 10289c63e9dbSSam Leffler rtexpunge(rt); 10291db1fffaSBill Fenner return EADDRNOTAVAIL; 10301db1fffaSBill Fenner } 10311db1fffaSBill Fenner 1032499676dfSJulian Elischer /* 1033499676dfSJulian Elischer * Both dst and gateway are stored in the same malloc'd chunk 1034499676dfSJulian Elischer * (If I ever get my hands on....) 1035499676dfSJulian Elischer * if we need to malloc a new chunk, then keep the old one around 1036499676dfSJulian Elischer * till we don't need it any more. 1037499676dfSJulian Elischer */ 1038e74642dfSLuigi Rizzo if (rt->rt_gateway == 0 || glen > SA_SIZE(rt->rt_gateway)) { 1039df8bae1dSRodney W. Grimes old = (caddr_t)rt_key(rt); 1040df8bae1dSRodney W. Grimes R_Malloc(new, caddr_t, dlen + glen); 1041df8bae1dSRodney W. Grimes if (new == 0) 10421db1fffaSBill Fenner return ENOBUFS; 1043d1dd20beSSam Leffler rt_key(rt) = new; 1044df8bae1dSRodney W. Grimes } else { 1045499676dfSJulian Elischer /* 1046499676dfSJulian Elischer * otherwise just overwrite the old one 1047499676dfSJulian Elischer */ 1048d1dd20beSSam Leffler new = (caddr_t)rt_key(rt); 1049df8bae1dSRodney W. Grimes old = 0; 1050df8bae1dSRodney W. Grimes } 1051499676dfSJulian Elischer 1052499676dfSJulian Elischer /* 1053499676dfSJulian Elischer * copy the new gateway value into the memory chunk 1054499676dfSJulian Elischer */ 1055df8bae1dSRodney W. Grimes Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen); 1056499676dfSJulian Elischer 1057499676dfSJulian Elischer /* 1058499676dfSJulian Elischer * if we are replacing the chunk (or it's new) we need to 1059499676dfSJulian Elischer * replace the dst as well 1060499676dfSJulian Elischer */ 1061df8bae1dSRodney W. Grimes if (old) { 1062df8bae1dSRodney W. Grimes Bcopy(dst, new, dlen); 1063df8bae1dSRodney W. Grimes Free(old); 10643299a156SSam Leffler old = 0; 1065df8bae1dSRodney W. Grimes } 1066499676dfSJulian Elischer 1067499676dfSJulian Elischer /* 1068499676dfSJulian Elischer * If there is already a gwroute, it's now almost definitly wrong 1069499676dfSJulian Elischer * so drop it. 1070499676dfSJulian Elischer */ 10718071913dSRuslan Ermilov if (rt->rt_gwroute != NULL) { 10728071913dSRuslan Ermilov RTFREE(rt->rt_gwroute); 10738071913dSRuslan Ermilov rt->rt_gwroute = NULL; 1074df8bae1dSRodney W. Grimes } 1075cd02a0b7SGarrett Wollman /* 1076cd02a0b7SGarrett Wollman * Cloning loop avoidance: 1077cd02a0b7SGarrett Wollman * In the presence of protocol-cloning and bad configuration, 1078cd02a0b7SGarrett Wollman * it is possible to get stuck in bottomless mutual recursion 1079cd02a0b7SGarrett Wollman * (rtrequest rt_setgate rtalloc1). We avoid this by not allowing 1080cd02a0b7SGarrett Wollman * protocol-cloning to operate for gateways (which is probably the 1081cd02a0b7SGarrett Wollman * correct choice anyway), and avoid the resulting reference loops 1082cd02a0b7SGarrett Wollman * by disallowing any route to run through itself as a gateway. 1083499676dfSJulian Elischer * This is obviously mandatory when we get rt->rt_output(). 108426d02ca7SAndre Oppermann * XXX: After removal of PRCLONING this is probably not needed anymore. 1085cd02a0b7SGarrett Wollman */ 1086df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_GATEWAY) { 1087e21afc60SSam Leffler struct rtentry *gwrt; 1088e21afc60SSam Leffler 1089e21afc60SSam Leffler RT_UNLOCK(rt); /* XXX workaround LOR */ 1090e21afc60SSam Leffler gwrt = rtalloc1(gate, 1, 0); 1091e21afc60SSam Leffler RT_LOCK(rt); 1092e21afc60SSam Leffler rt->rt_gwroute = gwrt; 1093cd02a0b7SGarrett Wollman if (rt->rt_gwroute == rt) { 1094d1dd20beSSam Leffler RTFREE_LOCKED(rt->rt_gwroute); 1095cd02a0b7SGarrett Wollman rt->rt_gwroute = 0; 10961db1fffaSBill Fenner return EDQUOT; /* failure */ 1097df8bae1dSRodney W. Grimes } 1098ea045210SSam Leffler if (rt->rt_gwroute != NULL) 1099d1dd20beSSam Leffler RT_UNLOCK(rt->rt_gwroute); 1100cd02a0b7SGarrett Wollman } 1101cd02a0b7SGarrett Wollman 1102cd02a0b7SGarrett Wollman /* 1103cd02a0b7SGarrett Wollman * This isn't going to do anything useful for host routes, so 1104cd02a0b7SGarrett Wollman * don't bother. Also make sure we have a reasonable mask 1105cd02a0b7SGarrett Wollman * (we don't yet have one during adds). 1106cd02a0b7SGarrett Wollman */ 1107cd02a0b7SGarrett Wollman if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) { 1108cd02a0b7SGarrett Wollman struct rtfc_arg arg; 1109d1dd20beSSam Leffler 1110cd02a0b7SGarrett Wollman arg.rnh = rnh; 1111cd02a0b7SGarrett Wollman arg.rt0 = rt; 1112e21afc60SSam Leffler RT_UNLOCK(rt); /* XXX workaround LOR */ 1113956b0b65SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rnh); 111472b9c8c9SSam Leffler RT_LOCK(rt); 1115cd02a0b7SGarrett Wollman rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), 1116cd02a0b7SGarrett Wollman rt_fixchange, &arg); 1117956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 1118cd02a0b7SGarrett Wollman } 1119cd02a0b7SGarrett Wollman 1120df8bae1dSRodney W. Grimes return 0; 1121df8bae1dSRodney W. Grimes } 1122df8bae1dSRodney W. Grimes 1123f708ef1bSPoul-Henning Kamp static void 1124d1dd20beSSam Leffler rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask) 1125df8bae1dSRodney W. Grimes { 1126df8bae1dSRodney W. Grimes register u_char *cp1 = (u_char *)src; 1127df8bae1dSRodney W. Grimes register u_char *cp2 = (u_char *)dst; 1128df8bae1dSRodney W. Grimes register u_char *cp3 = (u_char *)netmask; 1129df8bae1dSRodney W. Grimes u_char *cplim = cp2 + *cp3; 1130df8bae1dSRodney W. Grimes u_char *cplim2 = cp2 + *cp1; 1131df8bae1dSRodney W. Grimes 1132df8bae1dSRodney W. Grimes *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */ 1133df8bae1dSRodney W. Grimes cp3 += 2; 1134df8bae1dSRodney W. Grimes if (cplim > cplim2) 1135df8bae1dSRodney W. Grimes cplim = cplim2; 1136df8bae1dSRodney W. Grimes while (cp2 < cplim) 1137df8bae1dSRodney W. Grimes *cp2++ = *cp1++ & *cp3++; 1138df8bae1dSRodney W. Grimes if (cp2 < cplim2) 1139df8bae1dSRodney W. Grimes bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2)); 1140df8bae1dSRodney W. Grimes } 1141df8bae1dSRodney W. Grimes 1142df8bae1dSRodney W. Grimes /* 1143df8bae1dSRodney W. Grimes * Set up a routing table entry, normally 1144df8bae1dSRodney W. Grimes * for an interface. 1145df8bae1dSRodney W. Grimes */ 1146df8bae1dSRodney W. Grimes int 1147d1dd20beSSam Leffler rtinit(struct ifaddr *ifa, int cmd, int flags) 1148df8bae1dSRodney W. Grimes { 11495aca0b30SLuigi Rizzo struct sockaddr *dst; 11508071913dSRuslan Ermilov struct sockaddr *netmask; 1151df8bae1dSRodney W. Grimes struct mbuf *m = 0; 11525aca0b30SLuigi Rizzo struct rtentry *rt = 0; 11538071913dSRuslan Ermilov struct rt_addrinfo info; 11545aca0b30SLuigi Rizzo int error; 1155df8bae1dSRodney W. Grimes 11568071913dSRuslan Ermilov if (flags & RTF_HOST) { 11578071913dSRuslan Ermilov dst = ifa->ifa_dstaddr; 11588071913dSRuslan Ermilov netmask = NULL; 11598071913dSRuslan Ermilov } else { 11608071913dSRuslan Ermilov dst = ifa->ifa_addr; 11618071913dSRuslan Ermilov netmask = ifa->ifa_netmask; 11628071913dSRuslan Ermilov } 1163b0a76b88SJulian Elischer /* 1164b0a76b88SJulian Elischer * If it's a delete, check that if it exists, it's on the correct 1165b0a76b88SJulian Elischer * interface or we might scrub a route to another ifa which would 1166b0a76b88SJulian Elischer * be confusing at best and possibly worse. 1167b0a76b88SJulian Elischer */ 1168df8bae1dSRodney W. Grimes if (cmd == RTM_DELETE) { 11695aca0b30SLuigi Rizzo struct sockaddr *deldst; 11705aca0b30SLuigi Rizzo struct radix_node_head *rnh; 11715aca0b30SLuigi Rizzo struct radix_node *rn; 11725aca0b30SLuigi Rizzo 1173b0a76b88SJulian Elischer /* 1174b0a76b88SJulian Elischer * It's a delete, so it should already exist.. 1175b0a76b88SJulian Elischer * If it's a net, mask off the host bits 1176b0a76b88SJulian Elischer * (Assuming we have a mask) 1177b0a76b88SJulian Elischer */ 11788071913dSRuslan Ermilov if (netmask != NULL) { 1179a163d034SWarner Losh m = m_get(M_DONTWAIT, MT_SONAME); 118082cd038dSYoshinobu Inoue if (m == NULL) 118182cd038dSYoshinobu Inoue return(ENOBUFS); 1182df8bae1dSRodney W. Grimes deldst = mtod(m, struct sockaddr *); 11838071913dSRuslan Ermilov rt_maskedcopy(dst, deldst, netmask); 1184df8bae1dSRodney W. Grimes dst = deldst; 1185df8bae1dSRodney W. Grimes } 1186b0a76b88SJulian Elischer /* 11878071913dSRuslan Ermilov * Look up an rtentry that is in the routing tree and 11888071913dSRuslan Ermilov * contains the correct info. 1189b0a76b88SJulian Elischer */ 1190956b0b65SJeffrey Hsu if ((rnh = rt_tables[dst->sa_family]) == NULL) 1191956b0b65SJeffrey Hsu goto bad; 1192956b0b65SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rnh); 1193956b0b65SJeffrey Hsu error = ((rn = rnh->rnh_lookup(dst, netmask, rnh)) == NULL || 11948071913dSRuslan Ermilov (rn->rn_flags & RNF_ROOT) || 11958071913dSRuslan Ermilov ((struct rtentry *)rn)->rt_ifa != ifa || 1196956b0b65SJeffrey Hsu !sa_equal(SA(rn->rn_key), dst)); 1197956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 1198956b0b65SJeffrey Hsu if (error) { 1199956b0b65SJeffrey Hsu bad: 1200df8bae1dSRodney W. Grimes if (m) 1201df8bae1dSRodney W. Grimes (void) m_free(m); 12028071913dSRuslan Ermilov return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 1203df8bae1dSRodney W. Grimes } 1204b0a76b88SJulian Elischer } 1205b0a76b88SJulian Elischer /* 1206b0a76b88SJulian Elischer * Do the actual request 1207b0a76b88SJulian Elischer */ 12088071913dSRuslan Ermilov bzero((caddr_t)&info, sizeof(info)); 12098071913dSRuslan Ermilov info.rti_ifa = ifa; 12108071913dSRuslan Ermilov info.rti_flags = flags | ifa->ifa_flags; 12118071913dSRuslan Ermilov info.rti_info[RTAX_DST] = dst; 12128071913dSRuslan Ermilov info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr; 12138071913dSRuslan Ermilov info.rti_info[RTAX_NETMASK] = netmask; 12145aca0b30SLuigi Rizzo error = rtrequest1(cmd, &info, &rt); 12155aca0b30SLuigi Rizzo if (error == 0 && rt != NULL) { 12168071913dSRuslan Ermilov /* 12176f99b44cSBrian Somers * notify any listening routing agents of the change 12188071913dSRuslan Ermilov */ 1219d1dd20beSSam Leffler RT_LOCK(rt); 12208071913dSRuslan Ermilov rt_newaddrmsg(cmd, ifa, error, rt); 12218071913dSRuslan Ermilov if (cmd == RTM_DELETE) { 1222b0a76b88SJulian Elischer /* 1223b0a76b88SJulian Elischer * If we are deleting, and we found an entry, then 1224b0a76b88SJulian Elischer * it's been removed from the tree.. now throw it away. 1225b0a76b88SJulian Elischer */ 1226d1dd20beSSam Leffler RTFREE_LOCKED(rt); 1227d1dd20beSSam Leffler } else { 1228d1dd20beSSam Leffler if (cmd == RTM_ADD) { 1229b0a76b88SJulian Elischer /* 12308071913dSRuslan Ermilov * We just wanted to add it.. we don't actually 12318071913dSRuslan Ermilov * need a reference. 1232b0a76b88SJulian Elischer */ 12337138d65cSSam Leffler RT_REMREF(rt); 1234df8bae1dSRodney W. Grimes } 1235d1dd20beSSam Leffler RT_UNLOCK(rt); 1236d1dd20beSSam Leffler } 1237df8bae1dSRodney W. Grimes } 12388071913dSRuslan Ermilov if (m) 12398071913dSRuslan Ermilov (void) m_free(m); 12403ec66d6cSDavid Greenman return (error); 12413ec66d6cSDavid Greenman } 1242cb64988fSLuoqi Chen 1243d1dd20beSSam Leffler /* 1244d1dd20beSSam Leffler * Validate the route rt0 to the specified destination. If the 1245d1dd20beSSam Leffler * route is marked down try to find a new route. If the route 1246d1dd20beSSam Leffler * to the gateway is gone, try to setup a new route. Otherwise, 1247d1dd20beSSam Leffler * if the route is marked for packets to be rejected, enforce that. 1248d1dd20beSSam Leffler * 1249d1dd20beSSam Leffler * On return lrt contains the route to the destination and lrt0 1250d1dd20beSSam Leffler * contains the route to the next hop. Their values are meaningul 1251d1dd20beSSam Leffler * ONLY if no error is returned. 1252d1dd20beSSam Leffler * 1253d1dd20beSSam Leffler * This routine is invoked on each layer 2 output path, prior to 1254d1dd20beSSam Leffler * encapsulating outbound packets. 1255d1dd20beSSam Leffler */ 12567f760c48SMatthew N. Dodd int 1257d1dd20beSSam Leffler rt_check(struct rtentry **lrt, struct rtentry **lrt0, struct sockaddr *dst) 12587f760c48SMatthew N. Dodd { 1259d1dd20beSSam Leffler #define senderr(x) { error = x ; goto bad; } 12607f760c48SMatthew N. Dodd struct rtentry *rt; 12617f760c48SMatthew N. Dodd struct rtentry *rt0; 12627f760c48SMatthew N. Dodd int error; 12637f760c48SMatthew N. Dodd 12647f760c48SMatthew N. Dodd rt0 = *lrt0; 12657f760c48SMatthew N. Dodd rt = rt0; 1266d1dd20beSSam Leffler if (rt) { 1267d1dd20beSSam Leffler /* NB: the locking here is tortuous... */ 1268d1dd20beSSam Leffler RT_LOCK(rt); 1269d1dd20beSSam Leffler if ((rt->rt_flags & RTF_UP) == 0) { 1270d1dd20beSSam Leffler RT_UNLOCK(rt); 1271d1dd20beSSam Leffler rt = rtalloc1(dst, 1, 0UL); 12727f760c48SMatthew N. Dodd if (rt != NULL) { 12737138d65cSSam Leffler RT_REMREF(rt); 1274d4b2657fSSam Leffler /* XXX what about if change? */ 1275d1dd20beSSam Leffler } else 12767f760c48SMatthew N. Dodd senderr(EHOSTUNREACH); 1277d1dd20beSSam Leffler rt0 = rt; 12787f760c48SMatthew N. Dodd } 1279d1dd20beSSam Leffler /* XXX BSD/OS checks dst->sa_family != AF_NS */ 12807f760c48SMatthew N. Dodd if (rt->rt_flags & RTF_GATEWAY) { 1281d1dd20beSSam Leffler if (rt->rt_gwroute == 0) 12827f760c48SMatthew N. Dodd goto lookup; 12837f760c48SMatthew N. Dodd rt = rt->rt_gwroute; 1284d1dd20beSSam Leffler RT_LOCK(rt); /* NB: gwroute */ 12857f760c48SMatthew N. Dodd if ((rt->rt_flags & RTF_UP) == 0) { 1286d1dd20beSSam Leffler rtfree(rt); /* unlock gwroute */ 12877f760c48SMatthew N. Dodd rt = rt0; 12887f760c48SMatthew N. Dodd lookup: 1289d1dd20beSSam Leffler RT_UNLOCK(rt0); 1290d1dd20beSSam Leffler rt = rtalloc1(rt->rt_gateway, 1, 0UL); 1291d1dd20beSSam Leffler RT_LOCK(rt0); 1292d1dd20beSSam Leffler rt0->rt_gwroute = rt; 1293d1dd20beSSam Leffler if (rt == 0) { 1294d1dd20beSSam Leffler RT_UNLOCK(rt0); 12957f760c48SMatthew N. Dodd senderr(EHOSTUNREACH); 12967f760c48SMatthew N. Dodd } 12977f760c48SMatthew N. Dodd } 1298d1dd20beSSam Leffler RT_UNLOCK(rt0); 1299d1dd20beSSam Leffler } 1300d1dd20beSSam Leffler /* XXX why are we inspecting rmx_expire? */ 1301d1dd20beSSam Leffler error = (rt->rt_flags & RTF_REJECT) && 1302d1dd20beSSam Leffler (rt->rt_rmx.rmx_expire == 0 || 1303d1dd20beSSam Leffler time_second < rt->rt_rmx.rmx_expire); 1304d1dd20beSSam Leffler RT_UNLOCK(rt); 1305d1dd20beSSam Leffler if (error) 13067f760c48SMatthew N. Dodd senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 13077f760c48SMatthew N. Dodd } 1308d1dd20beSSam Leffler *lrt = rt; /* NB: return unlocked */ 13097f760c48SMatthew N. Dodd *lrt0 = rt0; 1310d1dd20beSSam Leffler return (0); 1311d1dd20beSSam Leffler bad: 1312d1dd20beSSam Leffler /* NB: lrt and lrt0 should not be interpreted if error is non-zero */ 13137f760c48SMatthew N. Dodd return (error); 1314d1dd20beSSam Leffler #undef senderr 13157f760c48SMatthew N. Dodd } 13167f760c48SMatthew N. Dodd 13176a800098SYoshinobu Inoue /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */ 13186a800098SYoshinobu Inoue SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0); 1319