1 /*- 2 * Copyright (c) 1980, 1986, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)route.c 8.3.1.1 (Berkeley) 2/23/95 30 * $FreeBSD$ 31 */ 32 /************************************************************************ 33 * Note: In this file a 'fib' is a "forwarding information base" * 34 * Which is the new name for an in kernel routing (next hop) table. * 35 ***********************************************************************/ 36 37 #include "opt_inet.h" 38 #include "opt_inet6.h" 39 #include "opt_route.h" 40 #include "opt_sctp.h" 41 #include "opt_mrouting.h" 42 #include "opt_mpath.h" 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/syslog.h> 47 #include <sys/malloc.h> 48 #include <sys/mbuf.h> 49 #include <sys/socket.h> 50 #include <sys/sysctl.h> 51 #include <sys/syslog.h> 52 #include <sys/sysproto.h> 53 #include <sys/proc.h> 54 #include <sys/domain.h> 55 #include <sys/kernel.h> 56 57 #include <net/if.h> 58 #include <net/if_var.h> 59 #include <net/if_dl.h> 60 #include <net/route.h> 61 #include <net/vnet.h> 62 #include <net/flowtable.h> 63 64 #ifdef RADIX_MPATH 65 #include <net/radix_mpath.h> 66 #endif 67 68 #include <netinet/in.h> 69 #include <netinet/ip_mroute.h> 70 71 #include <vm/uma.h> 72 73 #define RT_MAXFIBS UINT16_MAX 74 75 /* Kernel config default option. */ 76 #ifdef ROUTETABLES 77 #if ROUTETABLES <= 0 78 #error "ROUTETABLES defined too low" 79 #endif 80 #if ROUTETABLES > RT_MAXFIBS 81 #error "ROUTETABLES defined too big" 82 #endif 83 #define RT_NUMFIBS ROUTETABLES 84 #endif /* ROUTETABLES */ 85 /* Initialize to default if not otherwise set. */ 86 #ifndef RT_NUMFIBS 87 #define RT_NUMFIBS 1 88 #endif 89 90 #if defined(INET) || defined(INET6) 91 #ifdef SCTP 92 extern void sctp_addr_change(struct ifaddr *ifa, int cmd); 93 #endif /* SCTP */ 94 #endif 95 96 97 /* This is read-only.. */ 98 u_int rt_numfibs = RT_NUMFIBS; 99 SYSCTL_UINT(_net, OID_AUTO, fibs, CTLFLAG_RD, &rt_numfibs, 0, ""); 100 /* and this can be set too big but will be fixed before it is used */ 101 TUNABLE_INT("net.fibs", &rt_numfibs); 102 103 /* 104 * By default add routes to all fibs for new interfaces. 105 * Once this is set to 0 then only allocate routes on interface 106 * changes for the FIB of the caller when adding a new set of addresses 107 * to an interface. XXX this is a shotgun aproach to a problem that needs 108 * a more fine grained solution.. that will come. 109 * XXX also has the problems getting the FIB from curthread which will not 110 * always work given the fib can be overridden and prefixes can be added 111 * from the network stack context. 112 */ 113 u_int rt_add_addr_allfibs = 1; 114 SYSCTL_UINT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RW, 115 &rt_add_addr_allfibs, 0, ""); 116 TUNABLE_INT("net.add_addr_allfibs", &rt_add_addr_allfibs); 117 118 VNET_DEFINE(struct rtstat, rtstat); 119 #define V_rtstat VNET(rtstat) 120 121 VNET_DEFINE(struct radix_node_head *, rt_tables); 122 #define V_rt_tables VNET(rt_tables) 123 124 VNET_DEFINE(int, rttrash); /* routes not in table but not freed */ 125 #define V_rttrash VNET(rttrash) 126 127 128 /* 129 * Convert a 'struct radix_node *' to a 'struct rtentry *'. 130 * The operation can be done safely (in this code) because a 131 * 'struct rtentry' starts with two 'struct radix_node''s, the first 132 * one representing leaf nodes in the routing tree, which is 133 * what the code in radix.c passes us as a 'struct radix_node'. 134 * 135 * But because there are a lot of assumptions in this conversion, 136 * do not cast explicitly, but always use the macro below. 137 */ 138 #define RNTORT(p) ((struct rtentry *)(p)) 139 140 static VNET_DEFINE(uma_zone_t, rtzone); /* Routing table UMA zone. */ 141 #define V_rtzone VNET(rtzone) 142 143 static int rtrequest1_fib_change(struct radix_node_head *, struct rt_addrinfo *, 144 struct rtentry **, u_int); 145 146 /* 147 * handler for net.my_fibnum 148 */ 149 static int 150 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS) 151 { 152 int fibnum; 153 int error; 154 155 fibnum = curthread->td_proc->p_fibnum; 156 error = sysctl_handle_int(oidp, &fibnum, 0, req); 157 return (error); 158 } 159 160 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD, 161 NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller"); 162 163 static __inline struct radix_node_head ** 164 rt_tables_get_rnh_ptr(int table, int fam) 165 { 166 struct radix_node_head **rnh; 167 168 KASSERT(table >= 0 && table < rt_numfibs, ("%s: table out of bounds.", 169 __func__)); 170 KASSERT(fam >= 0 && fam < (AF_MAX+1), ("%s: fam out of bounds.", 171 __func__)); 172 173 /* rnh is [fib=0][af=0]. */ 174 rnh = (struct radix_node_head **)V_rt_tables; 175 /* Get the offset to the requested table and fam. */ 176 rnh += table * (AF_MAX+1) + fam; 177 178 return (rnh); 179 } 180 181 struct radix_node_head * 182 rt_tables_get_rnh(int table, int fam) 183 { 184 185 return (*rt_tables_get_rnh_ptr(table, fam)); 186 } 187 188 /* 189 * route initialization must occur before ip6_init2(), which happenas at 190 * SI_ORDER_MIDDLE. 191 */ 192 static void 193 route_init(void) 194 { 195 196 /* whack the tunable ints into line. */ 197 if (rt_numfibs > RT_MAXFIBS) 198 rt_numfibs = RT_MAXFIBS; 199 if (rt_numfibs == 0) 200 rt_numfibs = 1; 201 } 202 SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0); 203 204 static int 205 rtentry_zinit(void *mem, int size, int how) 206 { 207 struct rtentry *rt = mem; 208 209 rt->rt_pksent = counter_u64_alloc(how); 210 if (rt->rt_pksent == NULL) 211 return (ENOMEM); 212 213 RT_LOCK_INIT(rt); 214 215 return (0); 216 } 217 218 static void 219 rtentry_zfini(void *mem, int size) 220 { 221 struct rtentry *rt = mem; 222 223 RT_LOCK_DESTROY(rt); 224 counter_u64_free(rt->rt_pksent); 225 } 226 227 static int 228 rtentry_ctor(void *mem, int size, void *arg, int how) 229 { 230 struct rtentry *rt = mem; 231 232 bzero(rt, offsetof(struct rtentry, rt_endzero)); 233 counter_u64_zero(rt->rt_pksent); 234 235 return (0); 236 } 237 238 static void 239 rtentry_dtor(void *mem, int size, void *arg) 240 { 241 struct rtentry *rt = mem; 242 243 RT_UNLOCK_COND(rt); 244 } 245 246 static void 247 vnet_route_init(const void *unused __unused) 248 { 249 struct domain *dom; 250 struct radix_node_head **rnh; 251 int table; 252 int fam; 253 254 V_rt_tables = malloc(rt_numfibs * (AF_MAX+1) * 255 sizeof(struct radix_node_head *), M_RTABLE, M_WAITOK|M_ZERO); 256 257 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), 258 rtentry_ctor, rtentry_dtor, 259 rtentry_zinit, rtentry_zfini, UMA_ALIGN_PTR, 0); 260 for (dom = domains; dom; dom = dom->dom_next) { 261 if (dom->dom_rtattach == NULL) 262 continue; 263 264 for (table = 0; table < rt_numfibs; table++) { 265 fam = dom->dom_family; 266 if (table != 0 && fam != AF_INET6 && fam != AF_INET) 267 break; 268 269 /* 270 * XXX MRT rtattach will be also called from 271 * vfs_export.c but the offset will be 0 (only for 272 * AF_INET and AF_INET6 which don't need it anyhow). 273 */ 274 rnh = rt_tables_get_rnh_ptr(table, fam); 275 if (rnh == NULL) 276 panic("%s: rnh NULL", __func__); 277 dom->dom_rtattach((void **)rnh, dom->dom_rtoffset); 278 } 279 } 280 } 281 VNET_SYSINIT(vnet_route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, 282 vnet_route_init, 0); 283 284 #ifdef VIMAGE 285 static void 286 vnet_route_uninit(const void *unused __unused) 287 { 288 int table; 289 int fam; 290 struct domain *dom; 291 struct radix_node_head **rnh; 292 293 for (dom = domains; dom; dom = dom->dom_next) { 294 if (dom->dom_rtdetach == NULL) 295 continue; 296 297 for (table = 0; table < rt_numfibs; table++) { 298 fam = dom->dom_family; 299 300 if (table != 0 && fam != AF_INET6 && fam != AF_INET) 301 break; 302 303 rnh = rt_tables_get_rnh_ptr(table, fam); 304 if (rnh == NULL) 305 panic("%s: rnh NULL", __func__); 306 dom->dom_rtdetach((void **)rnh, dom->dom_rtoffset); 307 } 308 } 309 310 free(V_rt_tables, M_RTABLE); 311 uma_zdestroy(V_rtzone); 312 } 313 VNET_SYSUNINIT(vnet_route_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, 314 vnet_route_uninit, 0); 315 #endif 316 317 #ifndef _SYS_SYSPROTO_H_ 318 struct setfib_args { 319 int fibnum; 320 }; 321 #endif 322 int 323 sys_setfib(struct thread *td, struct setfib_args *uap) 324 { 325 if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs) 326 return EINVAL; 327 td->td_proc->p_fibnum = uap->fibnum; 328 return (0); 329 } 330 331 /* 332 * Packet routing routines. 333 */ 334 void 335 rtalloc(struct route *ro) 336 { 337 338 rtalloc_ign_fib(ro, 0UL, RT_DEFAULT_FIB); 339 } 340 341 void 342 rtalloc_fib(struct route *ro, u_int fibnum) 343 { 344 rtalloc_ign_fib(ro, 0UL, fibnum); 345 } 346 347 void 348 rtalloc_ign(struct route *ro, u_long ignore) 349 { 350 struct rtentry *rt; 351 352 if ((rt = ro->ro_rt) != NULL) { 353 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP) 354 return; 355 RTFREE(rt); 356 ro->ro_rt = NULL; 357 } 358 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, RT_DEFAULT_FIB); 359 if (ro->ro_rt) 360 RT_UNLOCK(ro->ro_rt); 361 } 362 363 void 364 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum) 365 { 366 struct rtentry *rt; 367 368 if ((rt = ro->ro_rt) != NULL) { 369 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP) 370 return; 371 RTFREE(rt); 372 ro->ro_rt = NULL; 373 } 374 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum); 375 if (ro->ro_rt) 376 RT_UNLOCK(ro->ro_rt); 377 } 378 379 /* 380 * Look up the route that matches the address given 381 * Or, at least try.. Create a cloned route if needed. 382 * 383 * The returned route, if any, is locked. 384 */ 385 struct rtentry * 386 rtalloc1(struct sockaddr *dst, int report, u_long ignflags) 387 { 388 389 return (rtalloc1_fib(dst, report, ignflags, RT_DEFAULT_FIB)); 390 } 391 392 struct rtentry * 393 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags, 394 u_int fibnum) 395 { 396 struct radix_node_head *rnh; 397 struct radix_node *rn; 398 struct rtentry *newrt; 399 struct rt_addrinfo info; 400 int err = 0, msgtype = RTM_MISS; 401 int needlock; 402 403 KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum")); 404 switch (dst->sa_family) { 405 case AF_INET6: 406 case AF_INET: 407 /* We support multiple FIBs. */ 408 break; 409 default: 410 fibnum = RT_DEFAULT_FIB; 411 break; 412 } 413 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 414 newrt = NULL; 415 if (rnh == NULL) 416 goto miss; 417 418 /* 419 * Look up the address in the table for that Address Family 420 */ 421 needlock = !(ignflags & RTF_RNH_LOCKED); 422 if (needlock) 423 RADIX_NODE_HEAD_RLOCK(rnh); 424 #ifdef INVARIANTS 425 else 426 RADIX_NODE_HEAD_LOCK_ASSERT(rnh); 427 #endif 428 rn = rnh->rnh_matchaddr(dst, rnh); 429 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) { 430 newrt = RNTORT(rn); 431 RT_LOCK(newrt); 432 RT_ADDREF(newrt); 433 if (needlock) 434 RADIX_NODE_HEAD_RUNLOCK(rnh); 435 goto done; 436 437 } else if (needlock) 438 RADIX_NODE_HEAD_RUNLOCK(rnh); 439 440 /* 441 * Either we hit the root or couldn't find any match, 442 * Which basically means 443 * "caint get there frm here" 444 */ 445 miss: 446 V_rtstat.rts_unreach++; 447 448 if (report) { 449 /* 450 * If required, report the failure to the supervising 451 * Authorities. 452 * For a delete, this is not an error. (report == 0) 453 */ 454 bzero(&info, sizeof(info)); 455 info.rti_info[RTAX_DST] = dst; 456 rt_missmsg_fib(msgtype, &info, 0, err, fibnum); 457 } 458 done: 459 if (newrt) 460 RT_LOCK_ASSERT(newrt); 461 return (newrt); 462 } 463 464 /* 465 * Remove a reference count from an rtentry. 466 * If the count gets low enough, take it out of the routing table 467 */ 468 void 469 rtfree(struct rtentry *rt) 470 { 471 struct radix_node_head *rnh; 472 473 KASSERT(rt != NULL,("%s: NULL rt", __func__)); 474 rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family); 475 KASSERT(rnh != NULL,("%s: NULL rnh", __func__)); 476 477 RT_LOCK_ASSERT(rt); 478 479 /* 480 * The callers should use RTFREE_LOCKED() or RTFREE(), so 481 * we should come here exactly with the last reference. 482 */ 483 RT_REMREF(rt); 484 if (rt->rt_refcnt > 0) { 485 log(LOG_DEBUG, "%s: %p has %d refs\n", __func__, rt, rt->rt_refcnt); 486 goto done; 487 } 488 489 /* 490 * On last reference give the "close method" a chance 491 * to cleanup private state. This also permits (for 492 * IPv4 and IPv6) a chance to decide if the routing table 493 * entry should be purged immediately or at a later time. 494 * When an immediate purge is to happen the close routine 495 * typically calls rtexpunge which clears the RTF_UP flag 496 * on the entry so that the code below reclaims the storage. 497 */ 498 if (rt->rt_refcnt == 0 && rnh->rnh_close) 499 rnh->rnh_close((struct radix_node *)rt, rnh); 500 501 /* 502 * If we are no longer "up" (and ref == 0) 503 * then we can free the resources associated 504 * with the route. 505 */ 506 if ((rt->rt_flags & RTF_UP) == 0) { 507 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 508 panic("rtfree 2"); 509 /* 510 * the rtentry must have been removed from the routing table 511 * so it is represented in rttrash.. remove that now. 512 */ 513 V_rttrash--; 514 #ifdef DIAGNOSTIC 515 if (rt->rt_refcnt < 0) { 516 printf("rtfree: %p not freed (neg refs)\n", rt); 517 goto done; 518 } 519 #endif 520 /* 521 * release references on items we hold them on.. 522 * e.g other routes and ifaddrs. 523 */ 524 if (rt->rt_ifa) 525 ifa_free(rt->rt_ifa); 526 /* 527 * The key is separatly alloc'd so free it (see rt_setgate()). 528 * This also frees the gateway, as they are always malloc'd 529 * together. 530 */ 531 Free(rt_key(rt)); 532 533 /* 534 * and the rtentry itself of course 535 */ 536 uma_zfree(V_rtzone, rt); 537 return; 538 } 539 done: 540 RT_UNLOCK(rt); 541 } 542 543 544 /* 545 * Force a routing table entry to the specified 546 * destination to go through the given gateway. 547 * Normally called as a result of a routing redirect 548 * message from the network layer. 549 */ 550 void 551 rtredirect(struct sockaddr *dst, 552 struct sockaddr *gateway, 553 struct sockaddr *netmask, 554 int flags, 555 struct sockaddr *src) 556 { 557 558 rtredirect_fib(dst, gateway, netmask, flags, src, RT_DEFAULT_FIB); 559 } 560 561 void 562 rtredirect_fib(struct sockaddr *dst, 563 struct sockaddr *gateway, 564 struct sockaddr *netmask, 565 int flags, 566 struct sockaddr *src, 567 u_int fibnum) 568 { 569 struct rtentry *rt, *rt0 = NULL; 570 int error = 0; 571 short *stat = NULL; 572 struct rt_addrinfo info; 573 struct ifaddr *ifa; 574 struct radix_node_head *rnh; 575 576 ifa = NULL; 577 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 578 if (rnh == NULL) { 579 error = EAFNOSUPPORT; 580 goto out; 581 } 582 583 /* verify the gateway is directly reachable */ 584 if ((ifa = ifa_ifwithnet(gateway, 0, fibnum)) == NULL) { 585 error = ENETUNREACH; 586 goto out; 587 } 588 rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */ 589 /* 590 * If the redirect isn't from our current router for this dst, 591 * it's either old or wrong. If it redirects us to ourselves, 592 * we have a routing loop, perhaps as a result of an interface 593 * going down recently. 594 */ 595 if (!(flags & RTF_DONE) && rt && 596 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) 597 error = EINVAL; 598 else if (ifa_ifwithaddr_check(gateway)) 599 error = EHOSTUNREACH; 600 if (error) 601 goto done; 602 /* 603 * Create a new entry if we just got back a wildcard entry 604 * or the lookup failed. This is necessary for hosts 605 * which use routing redirects generated by smart gateways 606 * to dynamically build the routing tables. 607 */ 608 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2)) 609 goto create; 610 /* 611 * Don't listen to the redirect if it's 612 * for a route to an interface. 613 */ 614 if (rt->rt_flags & RTF_GATEWAY) { 615 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) { 616 /* 617 * Changing from route to net => route to host. 618 * Create new route, rather than smashing route to net. 619 */ 620 create: 621 rt0 = rt; 622 rt = NULL; 623 624 flags |= RTF_GATEWAY | RTF_DYNAMIC; 625 bzero((caddr_t)&info, sizeof(info)); 626 info.rti_info[RTAX_DST] = dst; 627 info.rti_info[RTAX_GATEWAY] = gateway; 628 info.rti_info[RTAX_NETMASK] = netmask; 629 info.rti_ifa = ifa; 630 info.rti_flags = flags; 631 if (rt0 != NULL) 632 RT_UNLOCK(rt0); /* drop lock to avoid LOR with RNH */ 633 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum); 634 if (rt != NULL) { 635 RT_LOCK(rt); 636 if (rt0 != NULL) 637 EVENTHANDLER_INVOKE(route_redirect_event, rt0, rt, dst); 638 flags = rt->rt_flags; 639 } 640 if (rt0 != NULL) 641 RTFREE(rt0); 642 643 stat = &V_rtstat.rts_dynamic; 644 } else { 645 struct rtentry *gwrt; 646 647 /* 648 * Smash the current notion of the gateway to 649 * this destination. Should check about netmask!!! 650 */ 651 rt->rt_flags |= RTF_MODIFIED; 652 flags |= RTF_MODIFIED; 653 stat = &V_rtstat.rts_newgateway; 654 /* 655 * add the key and gateway (in one malloc'd chunk). 656 */ 657 RT_UNLOCK(rt); 658 RADIX_NODE_HEAD_LOCK(rnh); 659 RT_LOCK(rt); 660 rt_setgate(rt, rt_key(rt), gateway); 661 gwrt = rtalloc1(gateway, 1, RTF_RNH_LOCKED); 662 RADIX_NODE_HEAD_UNLOCK(rnh); 663 EVENTHANDLER_INVOKE(route_redirect_event, rt, gwrt, dst); 664 RTFREE_LOCKED(gwrt); 665 } 666 } else 667 error = EHOSTUNREACH; 668 done: 669 if (rt) 670 RTFREE_LOCKED(rt); 671 out: 672 if (error) 673 V_rtstat.rts_badredirect++; 674 else if (stat != NULL) 675 (*stat)++; 676 bzero((caddr_t)&info, sizeof(info)); 677 info.rti_info[RTAX_DST] = dst; 678 info.rti_info[RTAX_GATEWAY] = gateway; 679 info.rti_info[RTAX_NETMASK] = netmask; 680 info.rti_info[RTAX_AUTHOR] = src; 681 rt_missmsg_fib(RTM_REDIRECT, &info, flags, error, fibnum); 682 if (ifa != NULL) 683 ifa_free(ifa); 684 } 685 686 int 687 rtioctl(u_long req, caddr_t data) 688 { 689 690 return (rtioctl_fib(req, data, RT_DEFAULT_FIB)); 691 } 692 693 /* 694 * Routing table ioctl interface. 695 */ 696 int 697 rtioctl_fib(u_long req, caddr_t data, u_int fibnum) 698 { 699 700 /* 701 * If more ioctl commands are added here, make sure the proper 702 * super-user checks are being performed because it is possible for 703 * prison-root to make it this far if raw sockets have been enabled 704 * in jails. 705 */ 706 #ifdef INET 707 /* Multicast goop, grrr... */ 708 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP; 709 #else /* INET */ 710 return ENXIO; 711 #endif /* INET */ 712 } 713 714 /* 715 * For both ifa_ifwithroute() routines, 'ifa' is returned referenced. 716 */ 717 struct ifaddr * 718 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway) 719 { 720 721 return (ifa_ifwithroute_fib(flags, dst, gateway, RT_DEFAULT_FIB)); 722 } 723 724 struct ifaddr * 725 ifa_ifwithroute_fib(int flags, struct sockaddr *dst, struct sockaddr *gateway, 726 u_int fibnum) 727 { 728 struct ifaddr *ifa; 729 int not_found = 0; 730 731 if ((flags & RTF_GATEWAY) == 0) { 732 /* 733 * If we are adding a route to an interface, 734 * and the interface is a pt to pt link 735 * we should search for the destination 736 * as our clue to the interface. Otherwise 737 * we can use the local address. 738 */ 739 ifa = NULL; 740 if (flags & RTF_HOST) 741 ifa = ifa_ifwithdstaddr(dst, fibnum); 742 if (ifa == NULL) 743 ifa = ifa_ifwithaddr(gateway); 744 } else { 745 /* 746 * If we are adding a route to a remote net 747 * or host, the gateway may still be on the 748 * other end of a pt to pt link. 749 */ 750 ifa = ifa_ifwithdstaddr(gateway, fibnum); 751 } 752 if (ifa == NULL) 753 ifa = ifa_ifwithnet(gateway, 0, fibnum); 754 if (ifa == NULL) { 755 struct rtentry *rt = rtalloc1_fib(gateway, 0, RTF_RNH_LOCKED, fibnum); 756 if (rt == NULL) 757 return (NULL); 758 /* 759 * dismiss a gateway that is reachable only 760 * through the default router 761 */ 762 switch (gateway->sa_family) { 763 case AF_INET: 764 if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY) 765 not_found = 1; 766 break; 767 case AF_INET6: 768 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr)) 769 not_found = 1; 770 break; 771 default: 772 break; 773 } 774 if (!not_found && rt->rt_ifa != NULL) { 775 ifa = rt->rt_ifa; 776 ifa_ref(ifa); 777 } 778 RT_REMREF(rt); 779 RT_UNLOCK(rt); 780 if (not_found || ifa == NULL) 781 return (NULL); 782 } 783 if (ifa->ifa_addr->sa_family != dst->sa_family) { 784 struct ifaddr *oifa = ifa; 785 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 786 if (ifa == NULL) 787 ifa = oifa; 788 else 789 ifa_free(oifa); 790 } 791 return (ifa); 792 } 793 794 /* 795 * Do appropriate manipulations of a routing tree given 796 * all the bits of info needed 797 */ 798 int 799 rtrequest(int req, 800 struct sockaddr *dst, 801 struct sockaddr *gateway, 802 struct sockaddr *netmask, 803 int flags, 804 struct rtentry **ret_nrt) 805 { 806 807 return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt, 808 RT_DEFAULT_FIB)); 809 } 810 811 int 812 rtrequest_fib(int req, 813 struct sockaddr *dst, 814 struct sockaddr *gateway, 815 struct sockaddr *netmask, 816 int flags, 817 struct rtentry **ret_nrt, 818 u_int fibnum) 819 { 820 struct rt_addrinfo info; 821 822 if (dst->sa_len == 0) 823 return(EINVAL); 824 825 bzero((caddr_t)&info, sizeof(info)); 826 info.rti_flags = flags; 827 info.rti_info[RTAX_DST] = dst; 828 info.rti_info[RTAX_GATEWAY] = gateway; 829 info.rti_info[RTAX_NETMASK] = netmask; 830 return rtrequest1_fib(req, &info, ret_nrt, fibnum); 831 } 832 833 /* 834 * These (questionable) definitions of apparent local variables apply 835 * to the next two functions. XXXXXX!!! 836 */ 837 #define dst info->rti_info[RTAX_DST] 838 #define gateway info->rti_info[RTAX_GATEWAY] 839 #define netmask info->rti_info[RTAX_NETMASK] 840 #define ifaaddr info->rti_info[RTAX_IFA] 841 #define ifpaddr info->rti_info[RTAX_IFP] 842 #define flags info->rti_flags 843 844 int 845 rt_getifa(struct rt_addrinfo *info) 846 { 847 848 return (rt_getifa_fib(info, RT_DEFAULT_FIB)); 849 } 850 851 /* 852 * Look up rt_addrinfo for a specific fib. Note that if rti_ifa is defined, 853 * it will be referenced so the caller must free it. 854 */ 855 int 856 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum) 857 { 858 struct ifaddr *ifa; 859 int error = 0; 860 861 /* 862 * ifp may be specified by sockaddr_dl 863 * when protocol address is ambiguous. 864 */ 865 if (info->rti_ifp == NULL && ifpaddr != NULL && 866 ifpaddr->sa_family == AF_LINK && 867 (ifa = ifa_ifwithnet(ifpaddr, 0, fibnum)) != NULL) { 868 info->rti_ifp = ifa->ifa_ifp; 869 ifa_free(ifa); 870 } 871 if (info->rti_ifa == NULL && ifaaddr != NULL) 872 info->rti_ifa = ifa_ifwithaddr(ifaaddr); 873 if (info->rti_ifa == NULL) { 874 struct sockaddr *sa; 875 876 sa = ifaaddr != NULL ? ifaaddr : 877 (gateway != NULL ? gateway : dst); 878 if (sa != NULL && info->rti_ifp != NULL) 879 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp); 880 else if (dst != NULL && gateway != NULL) 881 info->rti_ifa = ifa_ifwithroute_fib(flags, dst, gateway, 882 fibnum); 883 else if (sa != NULL) 884 info->rti_ifa = ifa_ifwithroute_fib(flags, sa, sa, 885 fibnum); 886 } 887 if ((ifa = info->rti_ifa) != NULL) { 888 if (info->rti_ifp == NULL) 889 info->rti_ifp = ifa->ifa_ifp; 890 } else 891 error = ENETUNREACH; 892 return (error); 893 } 894 895 /* 896 * Expunges references to a route that's about to be reclaimed. 897 * The route must be locked. 898 */ 899 int 900 rtexpunge(struct rtentry *rt) 901 { 902 #if !defined(RADIX_MPATH) 903 struct radix_node *rn; 904 #else 905 struct rt_addrinfo info; 906 int fib; 907 struct rtentry *rt0; 908 #endif 909 struct radix_node_head *rnh; 910 struct ifaddr *ifa; 911 int error = 0; 912 913 /* 914 * Find the correct routing tree to use for this Address Family 915 */ 916 rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family); 917 RT_LOCK_ASSERT(rt); 918 if (rnh == NULL) 919 return (EAFNOSUPPORT); 920 RADIX_NODE_HEAD_LOCK_ASSERT(rnh); 921 922 #ifdef RADIX_MPATH 923 fib = rt->rt_fibnum; 924 bzero(&info, sizeof(info)); 925 info.rti_ifp = rt->rt_ifp; 926 info.rti_flags = RTF_RNH_LOCKED; 927 info.rti_info[RTAX_DST] = rt_key(rt); 928 info.rti_info[RTAX_GATEWAY] = rt->rt_ifa->ifa_addr; 929 930 RT_UNLOCK(rt); 931 error = rtrequest1_fib(RTM_DELETE, &info, &rt0, fib); 932 933 if (error == 0 && rt0 != NULL) { 934 rt = rt0; 935 RT_LOCK(rt); 936 } else if (error != 0) { 937 RT_LOCK(rt); 938 return (error); 939 } 940 #else 941 /* 942 * Remove the item from the tree; it should be there, 943 * but when callers invoke us blindly it may not (sigh). 944 */ 945 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh); 946 if (rn == NULL) { 947 error = ESRCH; 948 goto bad; 949 } 950 KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0, 951 ("unexpected flags 0x%x", rn->rn_flags)); 952 KASSERT(rt == RNTORT(rn), 953 ("lookup mismatch, rt %p rn %p", rt, rn)); 954 #endif /* RADIX_MPATH */ 955 956 rt->rt_flags &= ~RTF_UP; 957 958 /* 959 * Give the protocol a chance to keep things in sync. 960 */ 961 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) { 962 struct rt_addrinfo info; 963 964 bzero((caddr_t)&info, sizeof(info)); 965 info.rti_flags = rt->rt_flags; 966 info.rti_info[RTAX_DST] = rt_key(rt); 967 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 968 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 969 ifa->ifa_rtrequest(RTM_DELETE, rt, &info); 970 } 971 972 /* 973 * one more rtentry floating around that is not 974 * linked to the routing table. 975 */ 976 V_rttrash++; 977 #if !defined(RADIX_MPATH) 978 bad: 979 #endif 980 return (error); 981 } 982 983 #if 0 984 int p_sockaddr(char *buf, int buflen, struct sockaddr *s); 985 int rt_print(char *buf, int buflen, struct rtentry *rt); 986 987 int 988 p_sockaddr(char *buf, int buflen, struct sockaddr *s) 989 { 990 void *paddr = NULL; 991 992 switch (s->sa_family) { 993 case AF_INET: 994 paddr = &((struct sockaddr_in *)s)->sin_addr; 995 break; 996 case AF_INET6: 997 paddr = &((struct sockaddr_in6 *)s)->sin6_addr; 998 break; 999 } 1000 1001 if (paddr == NULL) 1002 return (0); 1003 1004 if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL) 1005 return (0); 1006 1007 return (strlen(buf)); 1008 } 1009 1010 int 1011 rt_print(char *buf, int buflen, struct rtentry *rt) 1012 { 1013 struct sockaddr *addr, *mask; 1014 int i = 0; 1015 1016 addr = rt_key(rt); 1017 mask = rt_mask(rt); 1018 1019 i = p_sockaddr(buf, buflen, addr); 1020 if (!(rt->rt_flags & RTF_HOST)) { 1021 buf[i++] = '/'; 1022 i += p_sockaddr(buf + i, buflen - i, mask); 1023 } 1024 1025 if (rt->rt_flags & RTF_GATEWAY) { 1026 buf[i++] = '>'; 1027 i += p_sockaddr(buf + i, buflen - i, rt->rt_gateway); 1028 } 1029 1030 return (i); 1031 } 1032 #endif 1033 1034 #ifdef RADIX_MPATH 1035 static int 1036 rn_mpath_update(int req, struct rt_addrinfo *info, 1037 struct radix_node_head *rnh, struct rtentry **ret_nrt) 1038 { 1039 /* 1040 * if we got multipath routes, we require users to specify 1041 * a matching RTAX_GATEWAY. 1042 */ 1043 struct rtentry *rt, *rto = NULL; 1044 struct radix_node *rn; 1045 int error = 0; 1046 1047 rn = rnh->rnh_lookup(dst, netmask, rnh); 1048 if (rn == NULL) 1049 return (ESRCH); 1050 rto = rt = RNTORT(rn); 1051 1052 rt = rt_mpath_matchgate(rt, gateway); 1053 if (rt == NULL) 1054 return (ESRCH); 1055 /* 1056 * this is the first entry in the chain 1057 */ 1058 if (rto == rt) { 1059 rn = rn_mpath_next((struct radix_node *)rt); 1060 /* 1061 * there is another entry, now it's active 1062 */ 1063 if (rn) { 1064 rto = RNTORT(rn); 1065 RT_LOCK(rto); 1066 rto->rt_flags |= RTF_UP; 1067 RT_UNLOCK(rto); 1068 } else if (rt->rt_flags & RTF_GATEWAY) { 1069 /* 1070 * For gateway routes, we need to 1071 * make sure that we we are deleting 1072 * the correct gateway. 1073 * rt_mpath_matchgate() does not 1074 * check the case when there is only 1075 * one route in the chain. 1076 */ 1077 if (gateway && 1078 (rt->rt_gateway->sa_len != gateway->sa_len || 1079 memcmp(rt->rt_gateway, gateway, gateway->sa_len))) 1080 error = ESRCH; 1081 else { 1082 /* 1083 * remove from tree before returning it 1084 * to the caller 1085 */ 1086 rn = rnh->rnh_deladdr(dst, netmask, rnh); 1087 KASSERT(rt == RNTORT(rn), ("radix node disappeared")); 1088 goto gwdelete; 1089 } 1090 1091 } 1092 /* 1093 * use the normal delete code to remove 1094 * the first entry 1095 */ 1096 if (req != RTM_DELETE) 1097 goto nondelete; 1098 1099 error = ENOENT; 1100 goto done; 1101 } 1102 1103 /* 1104 * if the entry is 2nd and on up 1105 */ 1106 if ((req == RTM_DELETE) && !rt_mpath_deldup(rto, rt)) 1107 panic ("rtrequest1: rt_mpath_deldup"); 1108 gwdelete: 1109 RT_LOCK(rt); 1110 RT_ADDREF(rt); 1111 if (req == RTM_DELETE) { 1112 rt->rt_flags &= ~RTF_UP; 1113 /* 1114 * One more rtentry floating around that is not 1115 * linked to the routing table. rttrash will be decremented 1116 * when RTFREE(rt) is eventually called. 1117 */ 1118 V_rttrash++; 1119 } 1120 1121 nondelete: 1122 if (req != RTM_DELETE) 1123 panic("unrecognized request %d", req); 1124 1125 1126 /* 1127 * If the caller wants it, then it can have it, 1128 * but it's up to it to free the rtentry as we won't be 1129 * doing it. 1130 */ 1131 if (ret_nrt) { 1132 *ret_nrt = rt; 1133 RT_UNLOCK(rt); 1134 } else 1135 RTFREE_LOCKED(rt); 1136 done: 1137 return (error); 1138 } 1139 #endif 1140 1141 int 1142 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt, 1143 u_int fibnum) 1144 { 1145 int error = 0, needlock = 0; 1146 struct rtentry *rt; 1147 #ifdef FLOWTABLE 1148 struct rtentry *rt0; 1149 #endif 1150 struct radix_node *rn; 1151 struct radix_node_head *rnh; 1152 struct ifaddr *ifa; 1153 struct sockaddr *ndst; 1154 struct sockaddr_storage mdst; 1155 #define senderr(x) { error = x ; goto bad; } 1156 1157 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum")); 1158 switch (dst->sa_family) { 1159 case AF_INET6: 1160 case AF_INET: 1161 /* We support multiple FIBs. */ 1162 break; 1163 default: 1164 fibnum = RT_DEFAULT_FIB; 1165 break; 1166 } 1167 1168 /* 1169 * Find the correct routing tree to use for this Address Family 1170 */ 1171 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 1172 if (rnh == NULL) 1173 return (EAFNOSUPPORT); 1174 needlock = ((flags & RTF_RNH_LOCKED) == 0); 1175 flags &= ~RTF_RNH_LOCKED; 1176 if (needlock) 1177 RADIX_NODE_HEAD_LOCK(rnh); 1178 else 1179 RADIX_NODE_HEAD_LOCK_ASSERT(rnh); 1180 /* 1181 * If we are adding a host route then we don't want to put 1182 * a netmask in the tree, nor do we want to clone it. 1183 */ 1184 if (flags & RTF_HOST) 1185 netmask = NULL; 1186 1187 switch (req) { 1188 case RTM_DELETE: 1189 if (netmask) { 1190 rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask); 1191 dst = (struct sockaddr *)&mdst; 1192 } 1193 #ifdef RADIX_MPATH 1194 if (rn_mpath_capable(rnh)) { 1195 error = rn_mpath_update(req, info, rnh, ret_nrt); 1196 /* 1197 * "bad" holds true for the success case 1198 * as well 1199 */ 1200 if (error != ENOENT) 1201 goto bad; 1202 error = 0; 1203 } 1204 #endif 1205 if ((flags & RTF_PINNED) == 0) { 1206 /* Check if target route can be deleted */ 1207 rt = (struct rtentry *)rnh->rnh_lookup(dst, 1208 netmask, rnh); 1209 if ((rt != NULL) && (rt->rt_flags & RTF_PINNED)) 1210 senderr(EADDRINUSE); 1211 } 1212 1213 /* 1214 * Remove the item from the tree and return it. 1215 * Complain if it is not there and do no more processing. 1216 */ 1217 rn = rnh->rnh_deladdr(dst, netmask, rnh); 1218 if (rn == NULL) 1219 senderr(ESRCH); 1220 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 1221 panic ("rtrequest delete"); 1222 rt = RNTORT(rn); 1223 RT_LOCK(rt); 1224 RT_ADDREF(rt); 1225 rt->rt_flags &= ~RTF_UP; 1226 1227 /* 1228 * give the protocol a chance to keep things in sync. 1229 */ 1230 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) 1231 ifa->ifa_rtrequest(RTM_DELETE, rt, info); 1232 1233 /* 1234 * One more rtentry floating around that is not 1235 * linked to the routing table. rttrash will be decremented 1236 * when RTFREE(rt) is eventually called. 1237 */ 1238 V_rttrash++; 1239 1240 /* 1241 * If the caller wants it, then it can have it, 1242 * but it's up to it to free the rtentry as we won't be 1243 * doing it. 1244 */ 1245 if (ret_nrt) { 1246 *ret_nrt = rt; 1247 RT_UNLOCK(rt); 1248 } else 1249 RTFREE_LOCKED(rt); 1250 break; 1251 case RTM_RESOLVE: 1252 /* 1253 * resolve was only used for route cloning 1254 * here for compat 1255 */ 1256 break; 1257 case RTM_ADD: 1258 if ((flags & RTF_GATEWAY) && !gateway) 1259 senderr(EINVAL); 1260 if (dst && gateway && (dst->sa_family != gateway->sa_family) && 1261 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK)) 1262 senderr(EINVAL); 1263 1264 if (info->rti_ifa == NULL) { 1265 error = rt_getifa_fib(info, fibnum); 1266 if (error) 1267 senderr(error); 1268 } else 1269 ifa_ref(info->rti_ifa); 1270 ifa = info->rti_ifa; 1271 rt = uma_zalloc(V_rtzone, M_NOWAIT); 1272 if (rt == NULL) { 1273 ifa_free(ifa); 1274 senderr(ENOBUFS); 1275 } 1276 rt->rt_flags = RTF_UP | flags; 1277 rt->rt_fibnum = fibnum; 1278 /* 1279 * Add the gateway. Possibly re-malloc-ing the storage for it. 1280 */ 1281 RT_LOCK(rt); 1282 if ((error = rt_setgate(rt, dst, gateway)) != 0) { 1283 ifa_free(ifa); 1284 uma_zfree(V_rtzone, rt); 1285 senderr(error); 1286 } 1287 1288 /* 1289 * point to the (possibly newly malloc'd) dest address. 1290 */ 1291 ndst = (struct sockaddr *)rt_key(rt); 1292 1293 /* 1294 * make sure it contains the value we want (masked if needed). 1295 */ 1296 if (netmask) { 1297 rt_maskedcopy(dst, ndst, netmask); 1298 } else 1299 bcopy(dst, ndst, dst->sa_len); 1300 1301 /* 1302 * We use the ifa reference returned by rt_getifa_fib(). 1303 * This moved from below so that rnh->rnh_addaddr() can 1304 * examine the ifa and ifa->ifa_ifp if it so desires. 1305 */ 1306 rt->rt_ifa = ifa; 1307 rt->rt_ifp = ifa->ifa_ifp; 1308 rt->rt_weight = 1; 1309 1310 #ifdef RADIX_MPATH 1311 /* do not permit exactly the same dst/mask/gw pair */ 1312 if (rn_mpath_capable(rnh) && 1313 rt_mpath_conflict(rnh, rt, netmask)) { 1314 ifa_free(rt->rt_ifa); 1315 Free(rt_key(rt)); 1316 uma_zfree(V_rtzone, rt); 1317 senderr(EEXIST); 1318 } 1319 #endif 1320 1321 #ifdef FLOWTABLE 1322 rt0 = NULL; 1323 /* "flow-table" only supports IPv6 and IPv4 at the moment. */ 1324 switch (dst->sa_family) { 1325 #ifdef INET6 1326 case AF_INET6: 1327 #endif 1328 #ifdef INET 1329 case AF_INET: 1330 #endif 1331 #if defined(INET6) || defined(INET) 1332 rn = rnh->rnh_matchaddr(dst, rnh); 1333 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) { 1334 struct sockaddr *mask; 1335 u_char *m, *n; 1336 int len; 1337 1338 /* 1339 * compare mask to see if the new route is 1340 * more specific than the existing one 1341 */ 1342 rt0 = RNTORT(rn); 1343 RT_LOCK(rt0); 1344 RT_ADDREF(rt0); 1345 RT_UNLOCK(rt0); 1346 /* 1347 * A host route is already present, so 1348 * leave the flow-table entries as is. 1349 */ 1350 if (rt0->rt_flags & RTF_HOST) { 1351 RTFREE(rt0); 1352 rt0 = NULL; 1353 } else if (!(flags & RTF_HOST) && netmask) { 1354 mask = rt_mask(rt0); 1355 len = mask->sa_len; 1356 m = (u_char *)mask; 1357 n = (u_char *)netmask; 1358 while (len-- > 0) { 1359 if (*n != *m) 1360 break; 1361 n++; 1362 m++; 1363 } 1364 if (len == 0 || (*n < *m)) { 1365 RTFREE(rt0); 1366 rt0 = NULL; 1367 } 1368 } 1369 } 1370 #endif/* INET6 || INET */ 1371 } 1372 #endif /* FLOWTABLE */ 1373 1374 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */ 1375 rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes); 1376 /* 1377 * If it still failed to go into the tree, 1378 * then un-make it (this should be a function) 1379 */ 1380 if (rn == NULL) { 1381 ifa_free(rt->rt_ifa); 1382 Free(rt_key(rt)); 1383 uma_zfree(V_rtzone, rt); 1384 #ifdef FLOWTABLE 1385 if (rt0 != NULL) 1386 RTFREE(rt0); 1387 #endif 1388 senderr(EEXIST); 1389 } 1390 #ifdef FLOWTABLE 1391 else if (rt0 != NULL) { 1392 flowtable_route_flush(dst->sa_family, rt0); 1393 RTFREE(rt0); 1394 } 1395 #endif 1396 1397 /* 1398 * If this protocol has something to add to this then 1399 * allow it to do that as well. 1400 */ 1401 if (ifa->ifa_rtrequest) 1402 ifa->ifa_rtrequest(req, rt, info); 1403 1404 /* 1405 * actually return a resultant rtentry and 1406 * give the caller a single reference. 1407 */ 1408 if (ret_nrt) { 1409 *ret_nrt = rt; 1410 RT_ADDREF(rt); 1411 } 1412 RT_UNLOCK(rt); 1413 break; 1414 case RTM_CHANGE: 1415 error = rtrequest1_fib_change(rnh, info, ret_nrt, fibnum); 1416 break; 1417 default: 1418 error = EOPNOTSUPP; 1419 } 1420 bad: 1421 if (needlock) 1422 RADIX_NODE_HEAD_UNLOCK(rnh); 1423 return (error); 1424 #undef senderr 1425 } 1426 1427 #undef dst 1428 #undef gateway 1429 #undef netmask 1430 #undef ifaaddr 1431 #undef ifpaddr 1432 #undef flags 1433 1434 #define senderr(e) { error = e; goto bad; } 1435 static int 1436 rtrequest1_fib_change(struct radix_node_head *rnh, struct rt_addrinfo *info, 1437 struct rtentry **ret_nrt, u_int fibnum) 1438 { 1439 struct rtentry *rt = NULL; 1440 int error = 0; 1441 int free_ifa = 0; 1442 1443 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST], 1444 info->rti_info[RTAX_NETMASK], rnh); 1445 1446 if (rt == NULL) 1447 return (ESRCH); 1448 1449 #ifdef RADIX_MPATH 1450 /* 1451 * If we got multipath routes, 1452 * we require users to specify a matching RTAX_GATEWAY. 1453 */ 1454 if (rn_mpath_capable(rnh)) { 1455 rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]); 1456 if (rt == NULL) 1457 return (ESRCH); 1458 } 1459 #endif 1460 1461 RT_LOCK(rt); 1462 1463 /* 1464 * New gateway could require new ifaddr, ifp; 1465 * flags may also be different; ifp may be specified 1466 * by ll sockaddr when protocol address is ambiguous 1467 */ 1468 if (((rt->rt_flags & RTF_GATEWAY) && 1469 info->rti_info[RTAX_GATEWAY] != NULL) || 1470 info->rti_info[RTAX_IFP] != NULL || 1471 (info->rti_info[RTAX_IFA] != NULL && 1472 !sa_equal(info->rti_info[RTAX_IFA], rt->rt_ifa->ifa_addr))) { 1473 1474 error = rt_getifa_fib(info, fibnum); 1475 if (info->rti_ifa != NULL) 1476 free_ifa = 1; 1477 1478 if (error != 0) 1479 senderr(error); 1480 } 1481 1482 /* Check if outgoing interface has changed */ 1483 if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa && 1484 rt->rt_ifa != NULL && rt->rt_ifa->ifa_rtrequest != NULL) { 1485 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, info); 1486 ifa_free(rt->rt_ifa); 1487 } 1488 /* Update gateway address */ 1489 if (info->rti_info[RTAX_GATEWAY] != NULL) { 1490 error = rt_setgate(rt, rt_key(rt), info->rti_info[RTAX_GATEWAY]); 1491 if (error != 0) 1492 senderr(error); 1493 1494 rt->rt_flags &= ~RTF_GATEWAY; 1495 rt->rt_flags |= (RTF_GATEWAY & info->rti_flags); 1496 } 1497 1498 if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa) { 1499 ifa_ref(info->rti_ifa); 1500 rt->rt_ifa = info->rti_ifa; 1501 rt->rt_ifp = info->rti_ifp; 1502 } 1503 /* Allow some flags to be toggled on change. */ 1504 rt->rt_flags &= ~RTF_FMASK; 1505 rt->rt_flags |= info->rti_flags & RTF_FMASK; 1506 1507 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest != NULL) 1508 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, info); 1509 1510 if (ret_nrt) { 1511 *ret_nrt = rt; 1512 RT_ADDREF(rt); 1513 } 1514 bad: 1515 RT_UNLOCK(rt); 1516 if (free_ifa != 0) 1517 ifa_free(info->rti_ifa); 1518 return (error); 1519 } 1520 #undef senderr 1521 1522 1523 int 1524 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate) 1525 { 1526 /* XXX dst may be overwritten, can we move this to below */ 1527 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate); 1528 #ifdef INVARIANTS 1529 struct radix_node_head *rnh; 1530 1531 rnh = rt_tables_get_rnh(rt->rt_fibnum, dst->sa_family); 1532 #endif 1533 1534 RT_LOCK_ASSERT(rt); 1535 RADIX_NODE_HEAD_LOCK_ASSERT(rnh); 1536 1537 /* 1538 * Prepare to store the gateway in rt->rt_gateway. 1539 * Both dst and gateway are stored one after the other in the same 1540 * malloc'd chunk. If we have room, we can reuse the old buffer, 1541 * rt_gateway already points to the right place. 1542 * Otherwise, malloc a new block and update the 'dst' address. 1543 */ 1544 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) { 1545 caddr_t new; 1546 1547 R_Malloc(new, caddr_t, dlen + glen); 1548 if (new == NULL) 1549 return ENOBUFS; 1550 /* 1551 * XXX note, we copy from *dst and not *rt_key(rt) because 1552 * rt_setgate() can be called to initialize a newly 1553 * allocated route entry, in which case rt_key(rt) == NULL 1554 * (and also rt->rt_gateway == NULL). 1555 * Free()/free() handle a NULL argument just fine. 1556 */ 1557 bcopy(dst, new, dlen); 1558 Free(rt_key(rt)); /* free old block, if any */ 1559 rt_key(rt) = (struct sockaddr *)new; 1560 rt->rt_gateway = (struct sockaddr *)(new + dlen); 1561 } 1562 1563 /* 1564 * Copy the new gateway value into the memory chunk. 1565 */ 1566 bcopy(gate, rt->rt_gateway, glen); 1567 1568 return (0); 1569 } 1570 1571 void 1572 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask) 1573 { 1574 u_char *cp1 = (u_char *)src; 1575 u_char *cp2 = (u_char *)dst; 1576 u_char *cp3 = (u_char *)netmask; 1577 u_char *cplim = cp2 + *cp3; 1578 u_char *cplim2 = cp2 + *cp1; 1579 1580 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */ 1581 cp3 += 2; 1582 if (cplim > cplim2) 1583 cplim = cplim2; 1584 while (cp2 < cplim) 1585 *cp2++ = *cp1++ & *cp3++; 1586 if (cp2 < cplim2) 1587 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2)); 1588 } 1589 1590 /* 1591 * Set up a routing table entry, normally 1592 * for an interface. 1593 */ 1594 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */ 1595 static inline int 1596 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum) 1597 { 1598 struct sockaddr *dst; 1599 struct sockaddr *netmask; 1600 struct rtentry *rt = NULL; 1601 struct rt_addrinfo info; 1602 int error = 0; 1603 int startfib, endfib; 1604 char tempbuf[_SOCKADDR_TMPSIZE]; 1605 int didwork = 0; 1606 int a_failure = 0; 1607 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 1608 struct radix_node_head *rnh; 1609 1610 if (flags & RTF_HOST) { 1611 dst = ifa->ifa_dstaddr; 1612 netmask = NULL; 1613 } else { 1614 dst = ifa->ifa_addr; 1615 netmask = ifa->ifa_netmask; 1616 } 1617 if (dst->sa_len == 0) 1618 return(EINVAL); 1619 switch (dst->sa_family) { 1620 case AF_INET6: 1621 case AF_INET: 1622 /* We support multiple FIBs. */ 1623 break; 1624 default: 1625 fibnum = RT_DEFAULT_FIB; 1626 break; 1627 } 1628 if (fibnum == RT_ALL_FIBS) { 1629 if (rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD) { 1630 startfib = endfib = ifa->ifa_ifp->if_fib; 1631 } else { 1632 startfib = 0; 1633 endfib = rt_numfibs - 1; 1634 } 1635 } else { 1636 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum")); 1637 startfib = fibnum; 1638 endfib = fibnum; 1639 } 1640 1641 /* 1642 * If it's a delete, check that if it exists, 1643 * it's on the correct interface or we might scrub 1644 * a route to another ifa which would 1645 * be confusing at best and possibly worse. 1646 */ 1647 if (cmd == RTM_DELETE) { 1648 /* 1649 * It's a delete, so it should already exist.. 1650 * If it's a net, mask off the host bits 1651 * (Assuming we have a mask) 1652 * XXX this is kinda inet specific.. 1653 */ 1654 if (netmask != NULL) { 1655 rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask); 1656 dst = (struct sockaddr *)tempbuf; 1657 } 1658 } 1659 /* 1660 * Now go through all the requested tables (fibs) and do the 1661 * requested action. Realistically, this will either be fib 0 1662 * for protocols that don't do multiple tables or all the 1663 * tables for those that do. 1664 */ 1665 for ( fibnum = startfib; fibnum <= endfib; fibnum++) { 1666 if (cmd == RTM_DELETE) { 1667 struct radix_node *rn; 1668 /* 1669 * Look up an rtentry that is in the routing tree and 1670 * contains the correct info. 1671 */ 1672 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 1673 if (rnh == NULL) 1674 /* this table doesn't exist but others might */ 1675 continue; 1676 RADIX_NODE_HEAD_RLOCK(rnh); 1677 rn = rnh->rnh_lookup(dst, netmask, rnh); 1678 #ifdef RADIX_MPATH 1679 if (rn_mpath_capable(rnh)) { 1680 1681 if (rn == NULL) 1682 error = ESRCH; 1683 else { 1684 rt = RNTORT(rn); 1685 /* 1686 * for interface route the 1687 * rt->rt_gateway is sockaddr_intf 1688 * for cloning ARP entries, so 1689 * rt_mpath_matchgate must use the 1690 * interface address 1691 */ 1692 rt = rt_mpath_matchgate(rt, 1693 ifa->ifa_addr); 1694 if (rt == NULL) 1695 error = ESRCH; 1696 } 1697 } 1698 #endif 1699 error = (rn == NULL || 1700 (rn->rn_flags & RNF_ROOT) || 1701 RNTORT(rn)->rt_ifa != ifa); 1702 RADIX_NODE_HEAD_RUNLOCK(rnh); 1703 if (error) { 1704 /* this is only an error if bad on ALL tables */ 1705 continue; 1706 } 1707 } 1708 /* 1709 * Do the actual request 1710 */ 1711 bzero((caddr_t)&info, sizeof(info)); 1712 info.rti_ifa = ifa; 1713 info.rti_flags = flags | 1714 (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED; 1715 info.rti_info[RTAX_DST] = dst; 1716 /* 1717 * doing this for compatibility reasons 1718 */ 1719 if (cmd == RTM_ADD) 1720 info.rti_info[RTAX_GATEWAY] = 1721 (struct sockaddr *)&null_sdl; 1722 else 1723 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr; 1724 info.rti_info[RTAX_NETMASK] = netmask; 1725 error = rtrequest1_fib(cmd, &info, &rt, fibnum); 1726 1727 if ((error == EEXIST) && (cmd == RTM_ADD)) { 1728 /* 1729 * Interface route addition failed. 1730 * Atomically delete current prefix generating 1731 * RTM_DELETE message, and retry adding 1732 * interface prefix. 1733 */ 1734 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 1735 RADIX_NODE_HEAD_LOCK(rnh); 1736 1737 /* Delete old prefix */ 1738 info.rti_ifa = NULL; 1739 info.rti_flags = RTF_RNH_LOCKED; 1740 1741 error = rtrequest1_fib(RTM_DELETE, &info, NULL, fibnum); 1742 if (error == 0) { 1743 info.rti_ifa = ifa; 1744 info.rti_flags = flags | RTF_RNH_LOCKED | 1745 (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED; 1746 error = rtrequest1_fib(cmd, &info, &rt, fibnum); 1747 } 1748 1749 RADIX_NODE_HEAD_UNLOCK(rnh); 1750 } 1751 1752 1753 if (error == 0 && rt != NULL) { 1754 /* 1755 * notify any listening routing agents of the change 1756 */ 1757 RT_LOCK(rt); 1758 #ifdef RADIX_MPATH 1759 /* 1760 * in case address alias finds the first address 1761 * e.g. ifconfig bge0 192.0.2.246/24 1762 * e.g. ifconfig bge0 192.0.2.247/24 1763 * the address set in the route is 192.0.2.246 1764 * so we need to replace it with 192.0.2.247 1765 */ 1766 if (memcmp(rt->rt_ifa->ifa_addr, 1767 ifa->ifa_addr, ifa->ifa_addr->sa_len)) { 1768 ifa_free(rt->rt_ifa); 1769 ifa_ref(ifa); 1770 rt->rt_ifp = ifa->ifa_ifp; 1771 rt->rt_ifa = ifa; 1772 } 1773 #endif 1774 /* 1775 * doing this for compatibility reasons 1776 */ 1777 if (cmd == RTM_ADD) { 1778 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type = 1779 rt->rt_ifp->if_type; 1780 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index = 1781 rt->rt_ifp->if_index; 1782 } 1783 RT_ADDREF(rt); 1784 RT_UNLOCK(rt); 1785 rt_newaddrmsg_fib(cmd, ifa, error, rt, fibnum); 1786 RT_LOCK(rt); 1787 RT_REMREF(rt); 1788 if (cmd == RTM_DELETE) { 1789 /* 1790 * If we are deleting, and we found an entry, 1791 * then it's been removed from the tree.. 1792 * now throw it away. 1793 */ 1794 RTFREE_LOCKED(rt); 1795 } else { 1796 if (cmd == RTM_ADD) { 1797 /* 1798 * We just wanted to add it.. 1799 * we don't actually need a reference. 1800 */ 1801 RT_REMREF(rt); 1802 } 1803 RT_UNLOCK(rt); 1804 } 1805 didwork = 1; 1806 } 1807 if (error) 1808 a_failure = error; 1809 } 1810 if (cmd == RTM_DELETE) { 1811 if (didwork) { 1812 error = 0; 1813 } else { 1814 /* we only give an error if it wasn't in any table */ 1815 error = ((flags & RTF_HOST) ? 1816 EHOSTUNREACH : ENETUNREACH); 1817 } 1818 } else { 1819 if (a_failure) { 1820 /* return an error if any of them failed */ 1821 error = a_failure; 1822 } 1823 } 1824 return (error); 1825 } 1826 1827 /* 1828 * Set up a routing table entry, normally 1829 * for an interface. 1830 */ 1831 int 1832 rtinit(struct ifaddr *ifa, int cmd, int flags) 1833 { 1834 struct sockaddr *dst; 1835 int fib = RT_DEFAULT_FIB; 1836 1837 if (flags & RTF_HOST) { 1838 dst = ifa->ifa_dstaddr; 1839 } else { 1840 dst = ifa->ifa_addr; 1841 } 1842 1843 switch (dst->sa_family) { 1844 case AF_INET6: 1845 case AF_INET: 1846 /* We do support multiple FIBs. */ 1847 fib = RT_ALL_FIBS; 1848 break; 1849 } 1850 return (rtinit1(ifa, cmd, flags, fib)); 1851 } 1852 1853 /* 1854 * Announce interface address arrival/withdraw 1855 * Returns 0 on success. 1856 */ 1857 int 1858 rt_addrmsg(int cmd, struct ifaddr *ifa, int fibnum) 1859 { 1860 1861 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 1862 ("unexpected cmd %d", cmd)); 1863 1864 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs), 1865 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs)); 1866 1867 #if defined(INET) || defined(INET6) 1868 #ifdef SCTP 1869 /* 1870 * notify the SCTP stack 1871 * this will only get called when an address is added/deleted 1872 * XXX pass the ifaddr struct instead if ifa->ifa_addr... 1873 */ 1874 sctp_addr_change(ifa, cmd); 1875 #endif /* SCTP */ 1876 #endif 1877 return (rtsock_addrmsg(cmd, ifa, fibnum)); 1878 } 1879 1880 /* 1881 * Announce route addition/removal. 1882 * Users of this function MUST validate input data BEFORE calling. 1883 * However we have to be able to handle invalid data: 1884 * if some userland app sends us "invalid" route message (invalid mask, 1885 * no dst, wrong address families, etc...) we need to pass it back 1886 * to app (and any other rtsock consumers) with rtm_errno field set to 1887 * non-zero value. 1888 * Returns 0 on success. 1889 */ 1890 int 1891 rt_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt, 1892 int fibnum) 1893 { 1894 1895 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 1896 ("unexpected cmd %d", cmd)); 1897 1898 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs), 1899 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs)); 1900 1901 KASSERT(rt_key(rt) != NULL, (":%s: rt_key must be supplied", __func__)); 1902 1903 return (rtsock_routemsg(cmd, ifp, error, rt, fibnum)); 1904 } 1905 1906 void 1907 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt) 1908 { 1909 1910 rt_newaddrmsg_fib(cmd, ifa, error, rt, RT_ALL_FIBS); 1911 } 1912 1913 /* 1914 * This is called to generate messages from the routing socket 1915 * indicating a network interface has had addresses associated with it. 1916 */ 1917 void 1918 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt, 1919 int fibnum) 1920 { 1921 1922 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 1923 ("unexpected cmd %u", cmd)); 1924 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs), 1925 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs)); 1926 1927 if (cmd == RTM_ADD) { 1928 rt_addrmsg(cmd, ifa, fibnum); 1929 if (rt != NULL) 1930 rt_routemsg(cmd, ifa->ifa_ifp, error, rt, fibnum); 1931 } else { 1932 if (rt != NULL) 1933 rt_routemsg(cmd, ifa->ifa_ifp, error, rt, fibnum); 1934 rt_addrmsg(cmd, ifa, fibnum); 1935 } 1936 } 1937 1938