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