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 static void rt_setmetrics(const struct rt_addrinfo *, struct rtentry *); 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 rtentry_dtor(void *mem, int size, void *arg) 241 { 242 struct rtentry *rt = mem; 243 244 RT_UNLOCK_COND(rt); 245 } 246 247 static void 248 vnet_route_init(const void *unused __unused) 249 { 250 struct domain *dom; 251 struct radix_node_head **rnh; 252 int table; 253 int fam; 254 255 V_rt_tables = malloc(rt_numfibs * (AF_MAX+1) * 256 sizeof(struct radix_node_head *), M_RTABLE, M_WAITOK|M_ZERO); 257 258 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), 259 rtentry_ctor, rtentry_dtor, 260 rtentry_zinit, rtentry_zfini, UMA_ALIGN_PTR, 0); 261 for (dom = domains; dom; dom = dom->dom_next) { 262 if (dom->dom_rtattach == NULL) 263 continue; 264 265 for (table = 0; table < rt_numfibs; table++) { 266 fam = dom->dom_family; 267 if (table != 0 && fam != AF_INET6 && fam != AF_INET) 268 break; 269 270 /* 271 * XXX MRT rtattach will be also called from 272 * vfs_export.c but the offset will be 0 (only for 273 * AF_INET and AF_INET6 which don't need it anyhow). 274 */ 275 rnh = rt_tables_get_rnh_ptr(table, fam); 276 if (rnh == NULL) 277 panic("%s: rnh NULL", __func__); 278 dom->dom_rtattach((void **)rnh, dom->dom_rtoffset); 279 } 280 } 281 } 282 VNET_SYSINIT(vnet_route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, 283 vnet_route_init, 0); 284 285 #ifdef VIMAGE 286 static void 287 vnet_route_uninit(const void *unused __unused) 288 { 289 int table; 290 int fam; 291 struct domain *dom; 292 struct radix_node_head **rnh; 293 294 for (dom = domains; dom; dom = dom->dom_next) { 295 if (dom->dom_rtdetach == NULL) 296 continue; 297 298 for (table = 0; table < rt_numfibs; table++) { 299 fam = dom->dom_family; 300 301 if (table != 0 && fam != AF_INET6 && fam != AF_INET) 302 break; 303 304 rnh = rt_tables_get_rnh_ptr(table, fam); 305 if (rnh == NULL) 306 panic("%s: rnh NULL", __func__); 307 dom->dom_rtdetach((void **)rnh, dom->dom_rtoffset); 308 } 309 } 310 311 free(V_rt_tables, M_RTABLE); 312 uma_zdestroy(V_rtzone); 313 } 314 VNET_SYSUNINIT(vnet_route_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, 315 vnet_route_uninit, 0); 316 #endif 317 318 #ifndef _SYS_SYSPROTO_H_ 319 struct setfib_args { 320 int fibnum; 321 }; 322 #endif 323 int 324 sys_setfib(struct thread *td, struct setfib_args *uap) 325 { 326 if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs) 327 return EINVAL; 328 td->td_proc->p_fibnum = uap->fibnum; 329 return (0); 330 } 331 332 /* 333 * Packet routing routines. 334 */ 335 void 336 rtalloc(struct route *ro) 337 { 338 339 rtalloc_ign_fib(ro, 0UL, RT_DEFAULT_FIB); 340 } 341 342 void 343 rtalloc_fib(struct route *ro, u_int fibnum) 344 { 345 rtalloc_ign_fib(ro, 0UL, fibnum); 346 } 347 348 void 349 rtalloc_ign(struct route *ro, u_long ignore) 350 { 351 struct rtentry *rt; 352 353 if ((rt = ro->ro_rt) != NULL) { 354 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP) 355 return; 356 RTFREE(rt); 357 ro->ro_rt = NULL; 358 } 359 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, RT_DEFAULT_FIB); 360 if (ro->ro_rt) 361 RT_UNLOCK(ro->ro_rt); 362 } 363 364 void 365 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum) 366 { 367 struct rtentry *rt; 368 369 if ((rt = ro->ro_rt) != NULL) { 370 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP) 371 return; 372 RTFREE(rt); 373 ro->ro_rt = NULL; 374 } 375 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum); 376 if (ro->ro_rt) 377 RT_UNLOCK(ro->ro_rt); 378 } 379 380 /* 381 * Look up the route that matches the address given 382 * Or, at least try.. Create a cloned route if needed. 383 * 384 * The returned route, if any, is locked. 385 */ 386 struct rtentry * 387 rtalloc1(struct sockaddr *dst, int report, u_long ignflags) 388 { 389 390 return (rtalloc1_fib(dst, report, ignflags, RT_DEFAULT_FIB)); 391 } 392 393 struct rtentry * 394 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags, 395 u_int fibnum) 396 { 397 struct radix_node_head *rnh; 398 struct radix_node *rn; 399 struct rtentry *newrt; 400 struct rt_addrinfo info; 401 int err = 0, msgtype = RTM_MISS; 402 int needlock; 403 404 KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum")); 405 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 406 newrt = NULL; 407 if (rnh == NULL) 408 goto miss; 409 410 /* 411 * Look up the address in the table for that Address Family 412 */ 413 needlock = !(ignflags & RTF_RNH_LOCKED); 414 if (needlock) 415 RADIX_NODE_HEAD_RLOCK(rnh); 416 #ifdef INVARIANTS 417 else 418 RADIX_NODE_HEAD_LOCK_ASSERT(rnh); 419 #endif 420 rn = rnh->rnh_matchaddr(dst, rnh); 421 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) { 422 newrt = RNTORT(rn); 423 RT_LOCK(newrt); 424 RT_ADDREF(newrt); 425 if (needlock) 426 RADIX_NODE_HEAD_RUNLOCK(rnh); 427 goto done; 428 429 } else if (needlock) 430 RADIX_NODE_HEAD_RUNLOCK(rnh); 431 432 /* 433 * Either we hit the root or couldn't find any match, 434 * Which basically means 435 * "caint get there frm here" 436 */ 437 miss: 438 V_rtstat.rts_unreach++; 439 440 if (report) { 441 /* 442 * If required, report the failure to the supervising 443 * Authorities. 444 * For a delete, this is not an error. (report == 0) 445 */ 446 bzero(&info, sizeof(info)); 447 info.rti_info[RTAX_DST] = dst; 448 rt_missmsg_fib(msgtype, &info, 0, err, fibnum); 449 } 450 done: 451 if (newrt) 452 RT_LOCK_ASSERT(newrt); 453 return (newrt); 454 } 455 456 /* 457 * Remove a reference count from an rtentry. 458 * If the count gets low enough, take it out of the routing table 459 */ 460 void 461 rtfree(struct rtentry *rt) 462 { 463 struct radix_node_head *rnh; 464 465 KASSERT(rt != NULL,("%s: NULL rt", __func__)); 466 rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family); 467 KASSERT(rnh != NULL,("%s: NULL rnh", __func__)); 468 469 RT_LOCK_ASSERT(rt); 470 471 /* 472 * The callers should use RTFREE_LOCKED() or RTFREE(), so 473 * we should come here exactly with the last reference. 474 */ 475 RT_REMREF(rt); 476 if (rt->rt_refcnt > 0) { 477 log(LOG_DEBUG, "%s: %p has %d refs\n", __func__, rt, rt->rt_refcnt); 478 goto done; 479 } 480 481 /* 482 * On last reference give the "close method" a chance 483 * to cleanup private state. This also permits (for 484 * IPv4 and IPv6) a chance to decide if the routing table 485 * entry should be purged immediately or at a later time. 486 * When an immediate purge is to happen the close routine 487 * typically calls rtexpunge which clears the RTF_UP flag 488 * on the entry so that the code below reclaims the storage. 489 */ 490 if (rt->rt_refcnt == 0 && rnh->rnh_close) 491 rnh->rnh_close((struct radix_node *)rt, rnh); 492 493 /* 494 * If we are no longer "up" (and ref == 0) 495 * then we can free the resources associated 496 * with the route. 497 */ 498 if ((rt->rt_flags & RTF_UP) == 0) { 499 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 500 panic("rtfree 2"); 501 /* 502 * the rtentry must have been removed from the routing table 503 * so it is represented in rttrash.. remove that now. 504 */ 505 V_rttrash--; 506 #ifdef DIAGNOSTIC 507 if (rt->rt_refcnt < 0) { 508 printf("rtfree: %p not freed (neg refs)\n", rt); 509 goto done; 510 } 511 #endif 512 /* 513 * release references on items we hold them on.. 514 * e.g other routes and ifaddrs. 515 */ 516 if (rt->rt_ifa) 517 ifa_free(rt->rt_ifa); 518 /* 519 * The key is separatly alloc'd so free it (see rt_setgate()). 520 * This also frees the gateway, as they are always malloc'd 521 * together. 522 */ 523 Free(rt_key(rt)); 524 525 /* 526 * and the rtentry itself of course 527 */ 528 uma_zfree(V_rtzone, rt); 529 return; 530 } 531 done: 532 RT_UNLOCK(rt); 533 } 534 535 536 /* 537 * Force a routing table entry to the specified 538 * destination to go through the given gateway. 539 * Normally called as a result of a routing redirect 540 * message from the network layer. 541 */ 542 void 543 rtredirect(struct sockaddr *dst, 544 struct sockaddr *gateway, 545 struct sockaddr *netmask, 546 int flags, 547 struct sockaddr *src) 548 { 549 550 rtredirect_fib(dst, gateway, netmask, flags, src, RT_DEFAULT_FIB); 551 } 552 553 void 554 rtredirect_fib(struct sockaddr *dst, 555 struct sockaddr *gateway, 556 struct sockaddr *netmask, 557 int flags, 558 struct sockaddr *src, 559 u_int fibnum) 560 { 561 struct rtentry *rt, *rt0 = NULL; 562 int error = 0; 563 short *stat = NULL; 564 struct rt_addrinfo info; 565 struct ifaddr *ifa; 566 struct radix_node_head *rnh; 567 568 ifa = NULL; 569 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 570 if (rnh == NULL) { 571 error = EAFNOSUPPORT; 572 goto out; 573 } 574 575 /* verify the gateway is directly reachable */ 576 if ((ifa = ifa_ifwithnet_fib(gateway, 0, fibnum)) == NULL) { 577 error = ENETUNREACH; 578 goto out; 579 } 580 rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */ 581 /* 582 * If the redirect isn't from our current router for this dst, 583 * it's either old or wrong. If it redirects us to ourselves, 584 * we have a routing loop, perhaps as a result of an interface 585 * going down recently. 586 */ 587 if (!(flags & RTF_DONE) && rt && 588 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) 589 error = EINVAL; 590 else if (ifa_ifwithaddr_check(gateway)) 591 error = EHOSTUNREACH; 592 if (error) 593 goto done; 594 /* 595 * Create a new entry if we just got back a wildcard entry 596 * or the lookup failed. This is necessary for hosts 597 * which use routing redirects generated by smart gateways 598 * to dynamically build the routing tables. 599 */ 600 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2)) 601 goto create; 602 /* 603 * Don't listen to the redirect if it's 604 * for a route to an interface. 605 */ 606 if (rt->rt_flags & RTF_GATEWAY) { 607 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) { 608 /* 609 * Changing from route to net => route to host. 610 * Create new route, rather than smashing route to net. 611 */ 612 create: 613 rt0 = rt; 614 rt = NULL; 615 616 flags |= RTF_GATEWAY | RTF_DYNAMIC; 617 bzero((caddr_t)&info, sizeof(info)); 618 info.rti_info[RTAX_DST] = dst; 619 info.rti_info[RTAX_GATEWAY] = gateway; 620 info.rti_info[RTAX_NETMASK] = netmask; 621 info.rti_ifa = ifa; 622 info.rti_flags = flags; 623 if (rt0 != NULL) 624 RT_UNLOCK(rt0); /* drop lock to avoid LOR with RNH */ 625 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum); 626 if (rt != NULL) { 627 RT_LOCK(rt); 628 if (rt0 != NULL) 629 EVENTHANDLER_INVOKE(route_redirect_event, rt0, rt, dst); 630 flags = rt->rt_flags; 631 } 632 if (rt0 != NULL) 633 RTFREE(rt0); 634 635 stat = &V_rtstat.rts_dynamic; 636 } else { 637 struct rtentry *gwrt; 638 639 /* 640 * Smash the current notion of the gateway to 641 * this destination. Should check about netmask!!! 642 */ 643 rt->rt_flags |= RTF_MODIFIED; 644 flags |= RTF_MODIFIED; 645 stat = &V_rtstat.rts_newgateway; 646 /* 647 * add the key and gateway (in one malloc'd chunk). 648 */ 649 RT_UNLOCK(rt); 650 RADIX_NODE_HEAD_LOCK(rnh); 651 RT_LOCK(rt); 652 rt_setgate(rt, rt_key(rt), gateway); 653 gwrt = rtalloc1(gateway, 1, RTF_RNH_LOCKED); 654 RADIX_NODE_HEAD_UNLOCK(rnh); 655 EVENTHANDLER_INVOKE(route_redirect_event, rt, gwrt, dst); 656 RTFREE_LOCKED(gwrt); 657 } 658 } else 659 error = EHOSTUNREACH; 660 done: 661 if (rt) 662 RTFREE_LOCKED(rt); 663 out: 664 if (error) 665 V_rtstat.rts_badredirect++; 666 else if (stat != NULL) 667 (*stat)++; 668 bzero((caddr_t)&info, sizeof(info)); 669 info.rti_info[RTAX_DST] = dst; 670 info.rti_info[RTAX_GATEWAY] = gateway; 671 info.rti_info[RTAX_NETMASK] = netmask; 672 info.rti_info[RTAX_AUTHOR] = src; 673 rt_missmsg_fib(RTM_REDIRECT, &info, flags, error, fibnum); 674 if (ifa != NULL) 675 ifa_free(ifa); 676 } 677 678 int 679 rtioctl(u_long req, caddr_t data) 680 { 681 682 return (rtioctl_fib(req, data, RT_DEFAULT_FIB)); 683 } 684 685 /* 686 * Routing table ioctl interface. 687 */ 688 int 689 rtioctl_fib(u_long req, caddr_t data, u_int fibnum) 690 { 691 692 /* 693 * If more ioctl commands are added here, make sure the proper 694 * super-user checks are being performed because it is possible for 695 * prison-root to make it this far if raw sockets have been enabled 696 * in jails. 697 */ 698 #ifdef INET 699 /* Multicast goop, grrr... */ 700 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP; 701 #else /* INET */ 702 return ENXIO; 703 #endif /* INET */ 704 } 705 706 /* 707 * For both ifa_ifwithroute() routines, 'ifa' is returned referenced. 708 */ 709 struct ifaddr * 710 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway) 711 { 712 713 return (ifa_ifwithroute_fib(flags, dst, gateway, RT_DEFAULT_FIB)); 714 } 715 716 struct ifaddr * 717 ifa_ifwithroute_fib(int flags, struct sockaddr *dst, struct sockaddr *gateway, 718 u_int fibnum) 719 { 720 struct ifaddr *ifa; 721 int not_found = 0; 722 723 if ((flags & RTF_GATEWAY) == 0) { 724 /* 725 * If we are adding a route to an interface, 726 * and the interface is a pt to pt link 727 * we should search for the destination 728 * as our clue to the interface. Otherwise 729 * we can use the local address. 730 */ 731 ifa = NULL; 732 if (flags & RTF_HOST) 733 ifa = ifa_ifwithdstaddr_fib(dst, fibnum); 734 if (ifa == NULL) 735 ifa = ifa_ifwithaddr(gateway); 736 } else { 737 /* 738 * If we are adding a route to a remote net 739 * or host, the gateway may still be on the 740 * other end of a pt to pt link. 741 */ 742 ifa = ifa_ifwithdstaddr_fib(gateway, fibnum); 743 } 744 if (ifa == NULL) 745 ifa = ifa_ifwithnet_fib(gateway, 0, fibnum); 746 if (ifa == NULL) { 747 struct rtentry *rt = rtalloc1_fib(gateway, 0, RTF_RNH_LOCKED, fibnum); 748 if (rt == NULL) 749 return (NULL); 750 /* 751 * dismiss a gateway that is reachable only 752 * through the default router 753 */ 754 switch (gateway->sa_family) { 755 case AF_INET: 756 if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY) 757 not_found = 1; 758 break; 759 case AF_INET6: 760 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr)) 761 not_found = 1; 762 break; 763 default: 764 break; 765 } 766 if (!not_found && rt->rt_ifa != NULL) { 767 ifa = rt->rt_ifa; 768 ifa_ref(ifa); 769 } 770 RT_REMREF(rt); 771 RT_UNLOCK(rt); 772 if (not_found || ifa == NULL) 773 return (NULL); 774 } 775 if (ifa->ifa_addr->sa_family != dst->sa_family) { 776 struct ifaddr *oifa = ifa; 777 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 778 if (ifa == NULL) 779 ifa = oifa; 780 else 781 ifa_free(oifa); 782 } 783 return (ifa); 784 } 785 786 /* 787 * Do appropriate manipulations of a routing tree given 788 * all the bits of info needed 789 */ 790 int 791 rtrequest(int req, 792 struct sockaddr *dst, 793 struct sockaddr *gateway, 794 struct sockaddr *netmask, 795 int flags, 796 struct rtentry **ret_nrt) 797 { 798 799 return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt, 800 RT_DEFAULT_FIB)); 801 } 802 803 int 804 rtrequest_fib(int req, 805 struct sockaddr *dst, 806 struct sockaddr *gateway, 807 struct sockaddr *netmask, 808 int flags, 809 struct rtentry **ret_nrt, 810 u_int fibnum) 811 { 812 struct rt_addrinfo info; 813 814 if (dst->sa_len == 0) 815 return(EINVAL); 816 817 bzero((caddr_t)&info, sizeof(info)); 818 info.rti_flags = flags; 819 info.rti_info[RTAX_DST] = dst; 820 info.rti_info[RTAX_GATEWAY] = gateway; 821 info.rti_info[RTAX_NETMASK] = netmask; 822 return rtrequest1_fib(req, &info, ret_nrt, fibnum); 823 } 824 825 /* 826 * These (questionable) definitions of apparent local variables apply 827 * to the next two functions. XXXXXX!!! 828 */ 829 #define dst info->rti_info[RTAX_DST] 830 #define gateway info->rti_info[RTAX_GATEWAY] 831 #define netmask info->rti_info[RTAX_NETMASK] 832 #define ifaaddr info->rti_info[RTAX_IFA] 833 #define ifpaddr info->rti_info[RTAX_IFP] 834 #define flags info->rti_flags 835 836 int 837 rt_getifa(struct rt_addrinfo *info) 838 { 839 840 return (rt_getifa_fib(info, RT_DEFAULT_FIB)); 841 } 842 843 /* 844 * Look up rt_addrinfo for a specific fib. Note that if rti_ifa is defined, 845 * it will be referenced so the caller must free it. 846 */ 847 int 848 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum) 849 { 850 struct ifaddr *ifa; 851 int error = 0; 852 853 /* 854 * ifp may be specified by sockaddr_dl 855 * when protocol address is ambiguous. 856 */ 857 if (info->rti_ifp == NULL && ifpaddr != NULL && 858 ifpaddr->sa_family == AF_LINK && 859 (ifa = ifa_ifwithnet_fib(ifpaddr, 0, fibnum)) != NULL) { 860 info->rti_ifp = ifa->ifa_ifp; 861 ifa_free(ifa); 862 } 863 if (info->rti_ifa == NULL && ifaaddr != NULL) 864 info->rti_ifa = ifa_ifwithaddr(ifaaddr); 865 if (info->rti_ifa == NULL) { 866 struct sockaddr *sa; 867 868 sa = ifaaddr != NULL ? ifaaddr : 869 (gateway != NULL ? gateway : dst); 870 if (sa != NULL && info->rti_ifp != NULL) 871 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp); 872 else if (dst != NULL && gateway != NULL) 873 info->rti_ifa = ifa_ifwithroute_fib(flags, dst, gateway, 874 fibnum); 875 else if (sa != NULL) 876 info->rti_ifa = ifa_ifwithroute_fib(flags, sa, sa, 877 fibnum); 878 } 879 if ((ifa = info->rti_ifa) != NULL) { 880 if (info->rti_ifp == NULL) 881 info->rti_ifp = ifa->ifa_ifp; 882 } else 883 error = ENETUNREACH; 884 return (error); 885 } 886 887 /* 888 * Expunges references to a route that's about to be reclaimed. 889 * The route must be locked. 890 */ 891 int 892 rt_expunge(struct radix_node_head *rnh, struct rtentry *rt) 893 { 894 #if !defined(RADIX_MPATH) 895 struct radix_node *rn; 896 #else 897 struct rt_addrinfo info; 898 int fib; 899 struct rtentry *rt0; 900 #endif 901 struct ifaddr *ifa; 902 int error = 0; 903 904 RT_LOCK_ASSERT(rt); 905 RADIX_NODE_HEAD_LOCK_ASSERT(rnh); 906 907 #ifdef RADIX_MPATH 908 fib = rt->rt_fibnum; 909 bzero(&info, sizeof(info)); 910 info.rti_ifp = rt->rt_ifp; 911 info.rti_flags = RTF_RNH_LOCKED; 912 info.rti_info[RTAX_DST] = rt_key(rt); 913 info.rti_info[RTAX_GATEWAY] = rt->rt_ifa->ifa_addr; 914 915 RT_UNLOCK(rt); 916 error = rtrequest1_fib(RTM_DELETE, &info, &rt0, fib); 917 918 if (error == 0 && rt0 != NULL) { 919 rt = rt0; 920 RT_LOCK(rt); 921 } else if (error != 0) { 922 RT_LOCK(rt); 923 return (error); 924 } 925 #else 926 /* 927 * Remove the item from the tree; it should be there, 928 * but when callers invoke us blindly it may not (sigh). 929 */ 930 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh); 931 if (rn == NULL) { 932 error = ESRCH; 933 goto bad; 934 } 935 KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0, 936 ("unexpected flags 0x%x", rn->rn_flags)); 937 KASSERT(rt == RNTORT(rn), 938 ("lookup mismatch, rt %p rn %p", rt, rn)); 939 #endif /* RADIX_MPATH */ 940 941 rt->rt_flags &= ~RTF_UP; 942 943 /* 944 * Give the protocol a chance to keep things in sync. 945 */ 946 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) { 947 struct rt_addrinfo info; 948 949 bzero((caddr_t)&info, sizeof(info)); 950 info.rti_flags = rt->rt_flags; 951 info.rti_info[RTAX_DST] = rt_key(rt); 952 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 953 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 954 ifa->ifa_rtrequest(RTM_DELETE, rt, &info); 955 } 956 957 /* 958 * one more rtentry floating around that is not 959 * linked to the routing table. 960 */ 961 V_rttrash++; 962 #if !defined(RADIX_MPATH) 963 bad: 964 #endif 965 return (error); 966 } 967 968 #if 0 969 int p_sockaddr(char *buf, int buflen, struct sockaddr *s); 970 int rt_print(char *buf, int buflen, struct rtentry *rt); 971 972 int 973 p_sockaddr(char *buf, int buflen, struct sockaddr *s) 974 { 975 void *paddr = NULL; 976 977 switch (s->sa_family) { 978 case AF_INET: 979 paddr = &((struct sockaddr_in *)s)->sin_addr; 980 break; 981 case AF_INET6: 982 paddr = &((struct sockaddr_in6 *)s)->sin6_addr; 983 break; 984 } 985 986 if (paddr == NULL) 987 return (0); 988 989 if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL) 990 return (0); 991 992 return (strlen(buf)); 993 } 994 995 int 996 rt_print(char *buf, int buflen, struct rtentry *rt) 997 { 998 struct sockaddr *addr, *mask; 999 int i = 0; 1000 1001 addr = rt_key(rt); 1002 mask = rt_mask(rt); 1003 1004 i = p_sockaddr(buf, buflen, addr); 1005 if (!(rt->rt_flags & RTF_HOST)) { 1006 buf[i++] = '/'; 1007 i += p_sockaddr(buf + i, buflen - i, mask); 1008 } 1009 1010 if (rt->rt_flags & RTF_GATEWAY) { 1011 buf[i++] = '>'; 1012 i += p_sockaddr(buf + i, buflen - i, rt->rt_gateway); 1013 } 1014 1015 return (i); 1016 } 1017 #endif 1018 1019 #ifdef RADIX_MPATH 1020 static int 1021 rn_mpath_update(int req, struct rt_addrinfo *info, 1022 struct radix_node_head *rnh, struct rtentry **ret_nrt) 1023 { 1024 /* 1025 * if we got multipath routes, we require users to specify 1026 * a matching RTAX_GATEWAY. 1027 */ 1028 struct rtentry *rt, *rto = NULL; 1029 struct radix_node *rn; 1030 int error = 0; 1031 1032 rn = rnh->rnh_lookup(dst, netmask, rnh); 1033 if (rn == NULL) 1034 return (ESRCH); 1035 rto = rt = RNTORT(rn); 1036 1037 rt = rt_mpath_matchgate(rt, gateway); 1038 if (rt == NULL) 1039 return (ESRCH); 1040 /* 1041 * this is the first entry in the chain 1042 */ 1043 if (rto == rt) { 1044 rn = rn_mpath_next((struct radix_node *)rt); 1045 /* 1046 * there is another entry, now it's active 1047 */ 1048 if (rn) { 1049 rto = RNTORT(rn); 1050 RT_LOCK(rto); 1051 rto->rt_flags |= RTF_UP; 1052 RT_UNLOCK(rto); 1053 } else if (rt->rt_flags & RTF_GATEWAY) { 1054 /* 1055 * For gateway routes, we need to 1056 * make sure that we we are deleting 1057 * the correct gateway. 1058 * rt_mpath_matchgate() does not 1059 * check the case when there is only 1060 * one route in the chain. 1061 */ 1062 if (gateway && 1063 (rt->rt_gateway->sa_len != gateway->sa_len || 1064 memcmp(rt->rt_gateway, gateway, gateway->sa_len))) 1065 error = ESRCH; 1066 else { 1067 /* 1068 * remove from tree before returning it 1069 * to the caller 1070 */ 1071 rn = rnh->rnh_deladdr(dst, netmask, rnh); 1072 KASSERT(rt == RNTORT(rn), ("radix node disappeared")); 1073 goto gwdelete; 1074 } 1075 1076 } 1077 /* 1078 * use the normal delete code to remove 1079 * the first entry 1080 */ 1081 if (req != RTM_DELETE) 1082 goto nondelete; 1083 1084 error = ENOENT; 1085 goto done; 1086 } 1087 1088 /* 1089 * if the entry is 2nd and on up 1090 */ 1091 if ((req == RTM_DELETE) && !rt_mpath_deldup(rto, rt)) 1092 panic ("rtrequest1: rt_mpath_deldup"); 1093 gwdelete: 1094 RT_LOCK(rt); 1095 RT_ADDREF(rt); 1096 if (req == RTM_DELETE) { 1097 rt->rt_flags &= ~RTF_UP; 1098 /* 1099 * One more rtentry floating around that is not 1100 * linked to the routing table. rttrash will be decremented 1101 * when RTFREE(rt) is eventually called. 1102 */ 1103 V_rttrash++; 1104 } 1105 1106 nondelete: 1107 if (req != RTM_DELETE) 1108 panic("unrecognized request %d", req); 1109 1110 1111 /* 1112 * If the caller wants it, then it can have it, 1113 * but it's up to it to free the rtentry as we won't be 1114 * doing it. 1115 */ 1116 if (ret_nrt) { 1117 *ret_nrt = rt; 1118 RT_UNLOCK(rt); 1119 } else 1120 RTFREE_LOCKED(rt); 1121 done: 1122 return (error); 1123 } 1124 #endif 1125 1126 int 1127 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt, 1128 u_int fibnum) 1129 { 1130 int error = 0, needlock = 0; 1131 struct rtentry *rt; 1132 #ifdef FLOWTABLE 1133 struct rtentry *rt0; 1134 #endif 1135 struct radix_node *rn; 1136 struct radix_node_head *rnh; 1137 struct ifaddr *ifa; 1138 struct sockaddr *ndst; 1139 struct sockaddr_storage mdst; 1140 #define senderr(x) { error = x ; goto bad; } 1141 1142 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum")); 1143 switch (dst->sa_family) { 1144 case AF_INET6: 1145 case AF_INET: 1146 /* We support multiple FIBs. */ 1147 break; 1148 default: 1149 fibnum = RT_DEFAULT_FIB; 1150 break; 1151 } 1152 1153 /* 1154 * Find the correct routing tree to use for this Address Family 1155 */ 1156 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 1157 if (rnh == NULL) 1158 return (EAFNOSUPPORT); 1159 needlock = ((flags & RTF_RNH_LOCKED) == 0); 1160 flags &= ~RTF_RNH_LOCKED; 1161 if (needlock) 1162 RADIX_NODE_HEAD_LOCK(rnh); 1163 else 1164 RADIX_NODE_HEAD_LOCK_ASSERT(rnh); 1165 /* 1166 * If we are adding a host route then we don't want to put 1167 * a netmask in the tree, nor do we want to clone it. 1168 */ 1169 if (flags & RTF_HOST) 1170 netmask = NULL; 1171 1172 switch (req) { 1173 case RTM_DELETE: 1174 if (netmask) { 1175 rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask); 1176 dst = (struct sockaddr *)&mdst; 1177 } 1178 #ifdef RADIX_MPATH 1179 if (rn_mpath_capable(rnh)) { 1180 error = rn_mpath_update(req, info, rnh, ret_nrt); 1181 /* 1182 * "bad" holds true for the success case 1183 * as well 1184 */ 1185 if (error != ENOENT) 1186 goto bad; 1187 error = 0; 1188 } 1189 #endif 1190 if ((flags & RTF_PINNED) == 0) { 1191 /* Check if target route can be deleted */ 1192 rt = (struct rtentry *)rnh->rnh_lookup(dst, 1193 netmask, rnh); 1194 if ((rt != NULL) && (rt->rt_flags & RTF_PINNED)) 1195 senderr(EADDRINUSE); 1196 } 1197 1198 /* 1199 * Remove the item from the tree and return it. 1200 * Complain if it is not there and do no more processing. 1201 */ 1202 rn = rnh->rnh_deladdr(dst, netmask, rnh); 1203 if (rn == NULL) 1204 senderr(ESRCH); 1205 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 1206 panic ("rtrequest delete"); 1207 rt = RNTORT(rn); 1208 RT_LOCK(rt); 1209 RT_ADDREF(rt); 1210 rt->rt_flags &= ~RTF_UP; 1211 1212 /* 1213 * give the protocol a chance to keep things in sync. 1214 */ 1215 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) 1216 ifa->ifa_rtrequest(RTM_DELETE, rt, info); 1217 1218 /* 1219 * One more rtentry floating around that is not 1220 * linked to the routing table. rttrash will be decremented 1221 * when RTFREE(rt) is eventually called. 1222 */ 1223 V_rttrash++; 1224 1225 /* 1226 * If the caller wants it, then it can have it, 1227 * but it's up to it to free the rtentry as we won't be 1228 * doing it. 1229 */ 1230 if (ret_nrt) { 1231 *ret_nrt = rt; 1232 RT_UNLOCK(rt); 1233 } else 1234 RTFREE_LOCKED(rt); 1235 break; 1236 case RTM_RESOLVE: 1237 /* 1238 * resolve was only used for route cloning 1239 * here for compat 1240 */ 1241 break; 1242 case RTM_ADD: 1243 if ((flags & RTF_GATEWAY) && !gateway) 1244 senderr(EINVAL); 1245 if (dst && gateway && (dst->sa_family != gateway->sa_family) && 1246 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK)) 1247 senderr(EINVAL); 1248 1249 if (info->rti_ifa == NULL) { 1250 error = rt_getifa_fib(info, fibnum); 1251 if (error) 1252 senderr(error); 1253 } else 1254 ifa_ref(info->rti_ifa); 1255 ifa = info->rti_ifa; 1256 rt = uma_zalloc(V_rtzone, M_NOWAIT); 1257 if (rt == NULL) { 1258 ifa_free(ifa); 1259 senderr(ENOBUFS); 1260 } 1261 rt->rt_flags = RTF_UP | flags; 1262 rt->rt_fibnum = fibnum; 1263 /* 1264 * Add the gateway. Possibly re-malloc-ing the storage for it. 1265 */ 1266 RT_LOCK(rt); 1267 if ((error = rt_setgate(rt, dst, gateway)) != 0) { 1268 ifa_free(ifa); 1269 uma_zfree(V_rtzone, rt); 1270 senderr(error); 1271 } 1272 1273 /* 1274 * point to the (possibly newly malloc'd) dest address. 1275 */ 1276 ndst = (struct sockaddr *)rt_key(rt); 1277 1278 /* 1279 * make sure it contains the value we want (masked if needed). 1280 */ 1281 if (netmask) { 1282 rt_maskedcopy(dst, ndst, netmask); 1283 } else 1284 bcopy(dst, ndst, dst->sa_len); 1285 1286 /* 1287 * We use the ifa reference returned by rt_getifa_fib(). 1288 * This moved from below so that rnh->rnh_addaddr() can 1289 * examine the ifa and ifa->ifa_ifp if it so desires. 1290 */ 1291 rt->rt_ifa = ifa; 1292 rt->rt_ifp = ifa->ifa_ifp; 1293 rt->rt_weight = 1; 1294 1295 #ifdef RADIX_MPATH 1296 /* do not permit exactly the same dst/mask/gw pair */ 1297 if (rn_mpath_capable(rnh) && 1298 rt_mpath_conflict(rnh, rt, netmask)) { 1299 ifa_free(rt->rt_ifa); 1300 Free(rt_key(rt)); 1301 uma_zfree(V_rtzone, rt); 1302 senderr(EEXIST); 1303 } 1304 #endif 1305 1306 #ifdef FLOWTABLE 1307 rt0 = NULL; 1308 /* "flow-table" only supports IPv6 and IPv4 at the moment. */ 1309 switch (dst->sa_family) { 1310 #ifdef INET6 1311 case AF_INET6: 1312 #endif 1313 #ifdef INET 1314 case AF_INET: 1315 #endif 1316 #if defined(INET6) || defined(INET) 1317 rn = rnh->rnh_matchaddr(dst, rnh); 1318 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) { 1319 struct sockaddr *mask; 1320 u_char *m, *n; 1321 int len; 1322 1323 /* 1324 * compare mask to see if the new route is 1325 * more specific than the existing one 1326 */ 1327 rt0 = RNTORT(rn); 1328 RT_LOCK(rt0); 1329 RT_ADDREF(rt0); 1330 RT_UNLOCK(rt0); 1331 /* 1332 * A host route is already present, so 1333 * leave the flow-table entries as is. 1334 */ 1335 if (rt0->rt_flags & RTF_HOST) { 1336 RTFREE(rt0); 1337 rt0 = NULL; 1338 } else if (!(flags & RTF_HOST) && netmask) { 1339 mask = rt_mask(rt0); 1340 len = mask->sa_len; 1341 m = (u_char *)mask; 1342 n = (u_char *)netmask; 1343 while (len-- > 0) { 1344 if (*n != *m) 1345 break; 1346 n++; 1347 m++; 1348 } 1349 if (len == 0 || (*n < *m)) { 1350 RTFREE(rt0); 1351 rt0 = NULL; 1352 } 1353 } 1354 } 1355 #endif/* INET6 || INET */ 1356 } 1357 #endif /* FLOWTABLE */ 1358 1359 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */ 1360 rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes); 1361 /* 1362 * If it still failed to go into the tree, 1363 * then un-make it (this should be a function) 1364 */ 1365 if (rn == NULL) { 1366 ifa_free(rt->rt_ifa); 1367 Free(rt_key(rt)); 1368 uma_zfree(V_rtzone, rt); 1369 #ifdef FLOWTABLE 1370 if (rt0 != NULL) 1371 RTFREE(rt0); 1372 #endif 1373 senderr(EEXIST); 1374 } 1375 #ifdef FLOWTABLE 1376 else if (rt0 != NULL) { 1377 flowtable_route_flush(dst->sa_family, rt0); 1378 RTFREE(rt0); 1379 } 1380 #endif 1381 1382 /* 1383 * If this protocol has something to add to this then 1384 * allow it to do that as well. 1385 */ 1386 if (ifa->ifa_rtrequest) 1387 ifa->ifa_rtrequest(req, rt, info); 1388 1389 rt_setmetrics(info, rt); 1390 1391 /* 1392 * actually return a resultant rtentry and 1393 * give the caller a single reference. 1394 */ 1395 if (ret_nrt) { 1396 *ret_nrt = rt; 1397 RT_ADDREF(rt); 1398 } 1399 RT_UNLOCK(rt); 1400 break; 1401 case RTM_CHANGE: 1402 error = rtrequest1_fib_change(rnh, info, ret_nrt, fibnum); 1403 break; 1404 default: 1405 error = EOPNOTSUPP; 1406 } 1407 bad: 1408 if (needlock) 1409 RADIX_NODE_HEAD_UNLOCK(rnh); 1410 return (error); 1411 #undef senderr 1412 } 1413 1414 #undef dst 1415 #undef gateway 1416 #undef netmask 1417 #undef ifaaddr 1418 #undef ifpaddr 1419 #undef flags 1420 1421 static int 1422 rtrequest1_fib_change(struct radix_node_head *rnh, struct rt_addrinfo *info, 1423 struct rtentry **ret_nrt, u_int fibnum) 1424 { 1425 struct rtentry *rt = NULL; 1426 int error = 0; 1427 int free_ifa = 0; 1428 1429 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST], 1430 info->rti_info[RTAX_NETMASK], rnh); 1431 1432 if (rt == NULL) 1433 return (ESRCH); 1434 1435 #ifdef RADIX_MPATH 1436 /* 1437 * If we got multipath routes, 1438 * we require users to specify a matching RTAX_GATEWAY. 1439 */ 1440 if (rn_mpath_capable(rnh)) { 1441 rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]); 1442 if (rt == NULL) 1443 return (ESRCH); 1444 } 1445 #endif 1446 1447 RT_LOCK(rt); 1448 1449 /* 1450 * New gateway could require new ifaddr, ifp; 1451 * flags may also be different; ifp may be specified 1452 * by ll sockaddr when protocol address is ambiguous 1453 */ 1454 if (((rt->rt_flags & RTF_GATEWAY) && 1455 info->rti_info[RTAX_GATEWAY] != NULL) || 1456 info->rti_info[RTAX_IFP] != NULL || 1457 (info->rti_info[RTAX_IFA] != NULL && 1458 !sa_equal(info->rti_info[RTAX_IFA], rt->rt_ifa->ifa_addr))) { 1459 1460 error = rt_getifa_fib(info, fibnum); 1461 if (info->rti_ifa != NULL) 1462 free_ifa = 1; 1463 1464 if (error != 0) 1465 goto bad; 1466 } 1467 1468 /* Check if outgoing interface has changed */ 1469 if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa && 1470 rt->rt_ifa != NULL && rt->rt_ifa->ifa_rtrequest != NULL) { 1471 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, info); 1472 ifa_free(rt->rt_ifa); 1473 } 1474 /* Update gateway address */ 1475 if (info->rti_info[RTAX_GATEWAY] != NULL) { 1476 error = rt_setgate(rt, rt_key(rt), info->rti_info[RTAX_GATEWAY]); 1477 if (error != 0) 1478 goto bad; 1479 1480 rt->rt_flags &= ~RTF_GATEWAY; 1481 rt->rt_flags |= (RTF_GATEWAY & info->rti_flags); 1482 } 1483 1484 if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa) { 1485 ifa_ref(info->rti_ifa); 1486 rt->rt_ifa = info->rti_ifa; 1487 rt->rt_ifp = info->rti_ifp; 1488 } 1489 /* Allow some flags to be toggled on change. */ 1490 rt->rt_flags &= ~RTF_FMASK; 1491 rt->rt_flags |= info->rti_flags & RTF_FMASK; 1492 1493 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest != NULL) 1494 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, info); 1495 1496 rt_setmetrics(info, rt); 1497 1498 if (ret_nrt) { 1499 *ret_nrt = rt; 1500 RT_ADDREF(rt); 1501 } 1502 bad: 1503 RT_UNLOCK(rt); 1504 if (free_ifa != 0) 1505 ifa_free(info->rti_ifa); 1506 return (error); 1507 } 1508 1509 static void 1510 rt_setmetrics(const struct rt_addrinfo *info, struct rtentry *rt) 1511 { 1512 1513 if (info->rti_mflags & RTV_MTU) 1514 rt->rt_mtu = info->rti_rmx->rmx_mtu; 1515 if (info->rti_mflags & RTV_WEIGHT) 1516 rt->rt_weight = info->rti_rmx->rmx_weight; 1517 /* Kernel -> userland timebase conversion. */ 1518 if (info->rti_mflags & RTV_EXPIRE) 1519 rt->rt_expire = info->rti_rmx->rmx_expire ? 1520 info->rti_rmx->rmx_expire - time_second + time_uptime : 0; 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