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