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/malloc.h> 47 #include <sys/mbuf.h> 48 #include <sys/socket.h> 49 #include <sys/sysctl.h> 50 #include <sys/syslog.h> 51 #include <sys/sysproto.h> 52 #include <sys/proc.h> 53 #include <sys/domain.h> 54 #include <sys/kernel.h> 55 56 #include <net/if.h> 57 #include <net/if_var.h> 58 #include <net/if_dl.h> 59 #include <net/route.h> 60 #include <net/route_var.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 rib_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 rib_head *, struct rt_addrinfo *, 141 struct rtentry **, u_int); 142 static void rt_setmetrics(const struct rt_addrinfo *, struct rtentry *); 143 static int rt_ifdelroute(const struct rtentry *rt, void *arg); 144 static struct rtentry *rt_unlinkrte(struct rib_head *rnh, 145 struct rt_addrinfo *info, int *perror); 146 static void rt_notifydelete(struct rtentry *rt, struct rt_addrinfo *info); 147 #ifdef RADIX_MPATH 148 static struct radix_node *rt_mpath_unlink(struct rib_head *rnh, 149 struct rt_addrinfo *info, struct rtentry *rto, int *perror); 150 #endif 151 static int rt_exportinfo(struct rtentry *rt, struct rt_addrinfo *info, 152 int flags); 153 154 struct if_mtuinfo 155 { 156 struct ifnet *ifp; 157 int mtu; 158 }; 159 160 static int if_updatemtu_cb(struct radix_node *, void *); 161 162 /* 163 * handler for net.my_fibnum 164 */ 165 static int 166 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS) 167 { 168 int fibnum; 169 int error; 170 171 fibnum = curthread->td_proc->p_fibnum; 172 error = sysctl_handle_int(oidp, &fibnum, 0, req); 173 return (error); 174 } 175 176 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD, 177 NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller"); 178 179 static __inline struct rib_head ** 180 rt_tables_get_rnh_ptr(int table, int fam) 181 { 182 struct rib_head **rnh; 183 184 KASSERT(table >= 0 && table < rt_numfibs, ("%s: table out of bounds.", 185 __func__)); 186 KASSERT(fam >= 0 && fam < (AF_MAX+1), ("%s: fam out of bounds.", 187 __func__)); 188 189 /* rnh is [fib=0][af=0]. */ 190 rnh = (struct rib_head **)V_rt_tables; 191 /* Get the offset to the requested table and fam. */ 192 rnh += table * (AF_MAX+1) + fam; 193 194 return (rnh); 195 } 196 197 struct rib_head * 198 rt_tables_get_rnh(int table, int fam) 199 { 200 201 return (*rt_tables_get_rnh_ptr(table, fam)); 202 } 203 204 u_int 205 rt_tables_get_gen(int table, int fam) 206 { 207 struct rib_head *rnh; 208 209 rnh = *rt_tables_get_rnh_ptr(table, fam); 210 return (rnh->rnh_gen); 211 } 212 213 214 /* 215 * route initialization must occur before ip6_init2(), which happenas at 216 * SI_ORDER_MIDDLE. 217 */ 218 static void 219 route_init(void) 220 { 221 222 /* whack the tunable ints into line. */ 223 if (rt_numfibs > RT_MAXFIBS) 224 rt_numfibs = RT_MAXFIBS; 225 if (rt_numfibs == 0) 226 rt_numfibs = 1; 227 } 228 SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0); 229 230 static int 231 rtentry_zinit(void *mem, int size, int how) 232 { 233 struct rtentry *rt = mem; 234 235 rt->rt_pksent = counter_u64_alloc(how); 236 if (rt->rt_pksent == NULL) 237 return (ENOMEM); 238 239 RT_LOCK_INIT(rt); 240 241 return (0); 242 } 243 244 static void 245 rtentry_zfini(void *mem, int size) 246 { 247 struct rtentry *rt = mem; 248 249 RT_LOCK_DESTROY(rt); 250 counter_u64_free(rt->rt_pksent); 251 } 252 253 static int 254 rtentry_ctor(void *mem, int size, void *arg, int how) 255 { 256 struct rtentry *rt = mem; 257 258 bzero(rt, offsetof(struct rtentry, rt_endzero)); 259 counter_u64_zero(rt->rt_pksent); 260 rt->rt_chain = NULL; 261 262 return (0); 263 } 264 265 static void 266 rtentry_dtor(void *mem, int size, void *arg) 267 { 268 struct rtentry *rt = mem; 269 270 RT_UNLOCK_COND(rt); 271 } 272 273 static void 274 vnet_route_init(const void *unused __unused) 275 { 276 struct domain *dom; 277 struct rib_head **rnh; 278 int table; 279 int fam; 280 281 V_rt_tables = malloc(rt_numfibs * (AF_MAX+1) * 282 sizeof(struct rib_head *), M_RTABLE, M_WAITOK|M_ZERO); 283 284 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), 285 rtentry_ctor, rtentry_dtor, 286 rtentry_zinit, rtentry_zfini, UMA_ALIGN_PTR, 0); 287 for (dom = domains; dom; dom = dom->dom_next) { 288 if (dom->dom_rtattach == NULL) 289 continue; 290 291 for (table = 0; table < rt_numfibs; table++) { 292 fam = dom->dom_family; 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_rtattach((void **)rnh, 0); 300 } 301 } 302 } 303 VNET_SYSINIT(vnet_route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, 304 vnet_route_init, 0); 305 306 #ifdef VIMAGE 307 static void 308 vnet_route_uninit(const void *unused __unused) 309 { 310 int table; 311 int fam; 312 struct domain *dom; 313 struct rib_head **rnh; 314 315 for (dom = domains; dom; dom = dom->dom_next) { 316 if (dom->dom_rtdetach == NULL) 317 continue; 318 319 for (table = 0; table < rt_numfibs; table++) { 320 fam = dom->dom_family; 321 322 if (table != 0 && fam != AF_INET6 && fam != AF_INET) 323 break; 324 325 rnh = rt_tables_get_rnh_ptr(table, fam); 326 if (rnh == NULL) 327 panic("%s: rnh NULL", __func__); 328 dom->dom_rtdetach((void **)rnh, 0); 329 } 330 } 331 332 free(V_rt_tables, M_RTABLE); 333 uma_zdestroy(V_rtzone); 334 } 335 VNET_SYSUNINIT(vnet_route_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, 336 vnet_route_uninit, 0); 337 #endif 338 339 struct rib_head * 340 rt_table_init(int offset) 341 { 342 struct rib_head *rh; 343 344 rh = malloc(sizeof(struct rib_head), M_RTABLE, M_WAITOK | M_ZERO); 345 346 /* TODO: These details should be hidded inside radix.c */ 347 /* Init masks tree */ 348 rn_inithead_internal(&rh->head, rh->rnh_nodes, offset); 349 rn_inithead_internal(&rh->rmhead.head, rh->rmhead.mask_nodes, 0); 350 rh->head.rnh_masks = &rh->rmhead; 351 352 /* Init locks */ 353 rw_init(&rh->rib_lock, "rib head lock"); 354 355 /* Finally, set base callbacks */ 356 rh->rnh_addaddr = rn_addroute; 357 rh->rnh_deladdr = rn_delete; 358 rh->rnh_matchaddr = rn_match; 359 rh->rnh_lookup = rn_lookup; 360 rh->rnh_walktree = rn_walktree; 361 rh->rnh_walktree_from = rn_walktree_from; 362 363 return (rh); 364 } 365 366 static int 367 rt_freeentry(struct radix_node *rn, void *arg) 368 { 369 struct radix_head * const rnh = arg; 370 struct radix_node *x; 371 372 x = (struct radix_node *)rn_delete(rn + 2, NULL, rnh); 373 if (x != NULL) 374 R_Free(x); 375 return (0); 376 } 377 378 void 379 rt_table_destroy(struct rib_head *rh) 380 { 381 382 rn_walktree(&rh->rmhead.head, rt_freeentry, &rh->rmhead.head); 383 384 /* Assume table is already empty */ 385 rw_destroy(&rh->rib_lock); 386 free(rh, M_RTABLE); 387 } 388 389 390 #ifndef _SYS_SYSPROTO_H_ 391 struct setfib_args { 392 int fibnum; 393 }; 394 #endif 395 int 396 sys_setfib(struct thread *td, struct setfib_args *uap) 397 { 398 if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs) 399 return EINVAL; 400 td->td_proc->p_fibnum = uap->fibnum; 401 return (0); 402 } 403 404 /* 405 * Packet routing routines. 406 */ 407 void 408 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum) 409 { 410 struct rtentry *rt; 411 412 if ((rt = ro->ro_rt) != NULL) { 413 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP) 414 return; 415 RTFREE(rt); 416 ro->ro_rt = NULL; 417 } 418 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum); 419 if (ro->ro_rt) 420 RT_UNLOCK(ro->ro_rt); 421 } 422 423 /* 424 * Look up the route that matches the address given 425 * Or, at least try.. Create a cloned route if needed. 426 * 427 * The returned route, if any, is locked. 428 */ 429 struct rtentry * 430 rtalloc1(struct sockaddr *dst, int report, u_long ignflags) 431 { 432 433 return (rtalloc1_fib(dst, report, ignflags, RT_DEFAULT_FIB)); 434 } 435 436 struct rtentry * 437 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags, 438 u_int fibnum) 439 { 440 struct rib_head *rh; 441 struct radix_node *rn; 442 struct rtentry *newrt; 443 struct rt_addrinfo info; 444 int err = 0, msgtype = RTM_MISS; 445 446 KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum")); 447 rh = rt_tables_get_rnh(fibnum, dst->sa_family); 448 newrt = NULL; 449 if (rh == NULL) 450 goto miss; 451 452 /* 453 * Look up the address in the table for that Address Family 454 */ 455 RIB_RLOCK(rh); 456 rn = rh->rnh_matchaddr(dst, &rh->head); 457 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) { 458 newrt = RNTORT(rn); 459 RT_LOCK(newrt); 460 RT_ADDREF(newrt); 461 RIB_RUNLOCK(rh); 462 return (newrt); 463 464 } else 465 RIB_RUNLOCK(rh); 466 467 /* 468 * Either we hit the root or couldn't find any match, 469 * Which basically means 470 * "caint get there frm here" 471 */ 472 miss: 473 V_rtstat.rts_unreach++; 474 475 if (report) { 476 /* 477 * If required, report the failure to the supervising 478 * Authorities. 479 * For a delete, this is not an error. (report == 0) 480 */ 481 bzero(&info, sizeof(info)); 482 info.rti_info[RTAX_DST] = dst; 483 rt_missmsg_fib(msgtype, &info, 0, err, fibnum); 484 } 485 return (newrt); 486 } 487 488 /* 489 * Remove a reference count from an rtentry. 490 * If the count gets low enough, take it out of the routing table 491 */ 492 void 493 rtfree(struct rtentry *rt) 494 { 495 struct rib_head *rnh; 496 497 KASSERT(rt != NULL,("%s: NULL rt", __func__)); 498 rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family); 499 KASSERT(rnh != NULL,("%s: NULL rnh", __func__)); 500 501 RT_LOCK_ASSERT(rt); 502 503 /* 504 * The callers should use RTFREE_LOCKED() or RTFREE(), so 505 * we should come here exactly with the last reference. 506 */ 507 RT_REMREF(rt); 508 if (rt->rt_refcnt > 0) { 509 log(LOG_DEBUG, "%s: %p has %d refs\n", __func__, rt, rt->rt_refcnt); 510 goto done; 511 } 512 513 /* 514 * On last reference give the "close method" a chance 515 * to cleanup private state. This also permits (for 516 * IPv4 and IPv6) a chance to decide if the routing table 517 * entry should be purged immediately or at a later time. 518 * When an immediate purge is to happen the close routine 519 * typically calls rtexpunge which clears the RTF_UP flag 520 * on the entry so that the code below reclaims the storage. 521 */ 522 if (rt->rt_refcnt == 0 && rnh->rnh_close) 523 rnh->rnh_close((struct radix_node *)rt, &rnh->head); 524 525 /* 526 * If we are no longer "up" (and ref == 0) 527 * then we can free the resources associated 528 * with the route. 529 */ 530 if ((rt->rt_flags & RTF_UP) == 0) { 531 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 532 panic("rtfree 2"); 533 /* 534 * the rtentry must have been removed from the routing table 535 * so it is represented in rttrash.. remove that now. 536 */ 537 V_rttrash--; 538 #ifdef DIAGNOSTIC 539 if (rt->rt_refcnt < 0) { 540 printf("rtfree: %p not freed (neg refs)\n", rt); 541 goto done; 542 } 543 #endif 544 /* 545 * release references on items we hold them on.. 546 * e.g other routes and ifaddrs. 547 */ 548 if (rt->rt_ifa) 549 ifa_free(rt->rt_ifa); 550 /* 551 * The key is separatly alloc'd so free it (see rt_setgate()). 552 * This also frees the gateway, as they are always malloc'd 553 * together. 554 */ 555 R_Free(rt_key(rt)); 556 557 /* 558 * and the rtentry itself of course 559 */ 560 uma_zfree(V_rtzone, rt); 561 return; 562 } 563 done: 564 RT_UNLOCK(rt); 565 } 566 567 568 /* 569 * Force a routing table entry to the specified 570 * destination to go through the given gateway. 571 * Normally called as a result of a routing redirect 572 * message from the network layer. 573 */ 574 void 575 rtredirect_fib(struct sockaddr *dst, 576 struct sockaddr *gateway, 577 struct sockaddr *netmask, 578 int flags, 579 struct sockaddr *src, 580 u_int fibnum) 581 { 582 struct rtentry *rt; 583 int error = 0; 584 short *stat = NULL; 585 struct rt_addrinfo info; 586 struct ifaddr *ifa; 587 struct rib_head *rnh; 588 589 ifa = NULL; 590 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 591 if (rnh == NULL) { 592 error = EAFNOSUPPORT; 593 goto out; 594 } 595 596 /* verify the gateway is directly reachable */ 597 if ((ifa = ifa_ifwithnet(gateway, 0, fibnum)) == NULL) { 598 error = ENETUNREACH; 599 goto out; 600 } 601 rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */ 602 /* 603 * If the redirect isn't from our current router for this dst, 604 * it's either old or wrong. If it redirects us to ourselves, 605 * we have a routing loop, perhaps as a result of an interface 606 * going down recently. 607 */ 608 if (!(flags & RTF_DONE) && rt) { 609 if (!sa_equal(src, rt->rt_gateway)) { 610 error = EINVAL; 611 goto done; 612 } 613 if (rt->rt_ifa != ifa && ifa->ifa_addr->sa_family != AF_LINK) { 614 error = EINVAL; 615 goto done; 616 } 617 } 618 if ((flags & RTF_GATEWAY) && ifa_ifwithaddr_check(gateway)) { 619 error = EHOSTUNREACH; 620 goto done; 621 } 622 /* 623 * Create a new entry if we just got back a wildcard entry 624 * or the lookup failed. This is necessary for hosts 625 * which use routing redirects generated by smart gateways 626 * to dynamically build the routing tables. 627 */ 628 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2)) 629 goto create; 630 /* 631 * Don't listen to the redirect if it's 632 * for a route to an interface. 633 */ 634 if (rt->rt_flags & RTF_GATEWAY) { 635 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) { 636 /* 637 * Changing from route to net => route to host. 638 * Create new route, rather than smashing route to net. 639 */ 640 create: 641 if (rt != NULL) 642 RTFREE_LOCKED(rt); 643 644 flags |= RTF_DYNAMIC; 645 bzero((caddr_t)&info, sizeof(info)); 646 info.rti_info[RTAX_DST] = dst; 647 info.rti_info[RTAX_GATEWAY] = gateway; 648 info.rti_info[RTAX_NETMASK] = netmask; 649 info.rti_ifa = ifa; 650 info.rti_flags = flags; 651 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum); 652 if (rt != NULL) { 653 RT_LOCK(rt); 654 flags = rt->rt_flags; 655 } 656 657 stat = &V_rtstat.rts_dynamic; 658 } else { 659 660 /* 661 * Smash the current notion of the gateway to 662 * this destination. Should check about netmask!!! 663 */ 664 if ((flags & RTF_GATEWAY) == 0) 665 rt->rt_flags &= ~RTF_GATEWAY; 666 rt->rt_flags |= RTF_MODIFIED; 667 flags |= RTF_MODIFIED; 668 stat = &V_rtstat.rts_newgateway; 669 /* 670 * add the key and gateway (in one malloc'd chunk). 671 */ 672 RT_UNLOCK(rt); 673 RIB_WLOCK(rnh); 674 RT_LOCK(rt); 675 rt_setgate(rt, rt_key(rt), gateway); 676 RIB_WUNLOCK(rnh); 677 } 678 } else 679 error = EHOSTUNREACH; 680 done: 681 if (rt) 682 RTFREE_LOCKED(rt); 683 out: 684 if (error) 685 V_rtstat.rts_badredirect++; 686 else if (stat != NULL) 687 (*stat)++; 688 bzero((caddr_t)&info, sizeof(info)); 689 info.rti_info[RTAX_DST] = dst; 690 info.rti_info[RTAX_GATEWAY] = gateway; 691 info.rti_info[RTAX_NETMASK] = netmask; 692 info.rti_info[RTAX_AUTHOR] = src; 693 rt_missmsg_fib(RTM_REDIRECT, &info, flags, error, fibnum); 694 if (ifa != NULL) 695 ifa_free(ifa); 696 } 697 698 /* 699 * Routing table ioctl interface. 700 */ 701 int 702 rtioctl_fib(u_long req, caddr_t data, u_int fibnum) 703 { 704 705 /* 706 * If more ioctl commands are added here, make sure the proper 707 * super-user checks are being performed because it is possible for 708 * prison-root to make it this far if raw sockets have been enabled 709 * in jails. 710 */ 711 #ifdef INET 712 /* Multicast goop, grrr... */ 713 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP; 714 #else /* INET */ 715 return ENXIO; 716 #endif /* INET */ 717 } 718 719 struct ifaddr * 720 ifa_ifwithroute(int flags, const struct sockaddr *dst, struct sockaddr *gateway, 721 u_int fibnum) 722 { 723 struct ifaddr *ifa; 724 int not_found = 0; 725 726 if ((flags & RTF_GATEWAY) == 0) { 727 /* 728 * If we are adding a route to an interface, 729 * and the interface is a pt to pt link 730 * we should search for the destination 731 * as our clue to the interface. Otherwise 732 * we can use the local address. 733 */ 734 ifa = NULL; 735 if (flags & RTF_HOST) 736 ifa = ifa_ifwithdstaddr(dst, fibnum); 737 if (ifa == NULL) 738 ifa = ifa_ifwithaddr(gateway); 739 } else { 740 /* 741 * If we are adding a route to a remote net 742 * or host, the gateway may still be on the 743 * other end of a pt to pt link. 744 */ 745 ifa = ifa_ifwithdstaddr(gateway, fibnum); 746 } 747 if (ifa == NULL) 748 ifa = ifa_ifwithnet(gateway, 0, fibnum); 749 if (ifa == NULL) { 750 struct rtentry *rt = rtalloc1_fib(gateway, 0, 0, fibnum); 751 if (rt == NULL) 752 return (NULL); 753 /* 754 * dismiss a gateway that is reachable only 755 * through the default router 756 */ 757 switch (gateway->sa_family) { 758 case AF_INET: 759 if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY) 760 not_found = 1; 761 break; 762 case AF_INET6: 763 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr)) 764 not_found = 1; 765 break; 766 default: 767 break; 768 } 769 if (!not_found && rt->rt_ifa != NULL) { 770 ifa = rt->rt_ifa; 771 ifa_ref(ifa); 772 } 773 RT_REMREF(rt); 774 RT_UNLOCK(rt); 775 if (not_found || ifa == NULL) 776 return (NULL); 777 } 778 if (ifa->ifa_addr->sa_family != dst->sa_family) { 779 struct ifaddr *oifa = ifa; 780 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 781 if (ifa == NULL) 782 ifa = oifa; 783 else 784 ifa_free(oifa); 785 } 786 return (ifa); 787 } 788 789 /* 790 * Do appropriate manipulations of a routing tree given 791 * all the bits of info needed 792 */ 793 int 794 rtrequest_fib(int req, 795 struct sockaddr *dst, 796 struct sockaddr *gateway, 797 struct sockaddr *netmask, 798 int flags, 799 struct rtentry **ret_nrt, 800 u_int fibnum) 801 { 802 struct rt_addrinfo info; 803 804 if (dst->sa_len == 0) 805 return(EINVAL); 806 807 bzero((caddr_t)&info, sizeof(info)); 808 info.rti_flags = flags; 809 info.rti_info[RTAX_DST] = dst; 810 info.rti_info[RTAX_GATEWAY] = gateway; 811 info.rti_info[RTAX_NETMASK] = netmask; 812 return rtrequest1_fib(req, &info, ret_nrt, fibnum); 813 } 814 815 816 /* 817 * Copy most of @rt data into @info. 818 * 819 * If @flags contains NHR_COPY, copies dst,netmask and gw to the 820 * pointers specified by @info structure. Assume such pointers 821 * are zeroed sockaddr-like structures with sa_len field initialized 822 * to reflect size of the provided buffer. if no NHR_COPY is specified, 823 * point dst,netmask and gw @info fields to appropriate @rt values. 824 * 825 * if @flags contains NHR_REF, do refcouting on rt_ifp. 826 * 827 * Returns 0 on success. 828 */ 829 int 830 rt_exportinfo(struct rtentry *rt, struct rt_addrinfo *info, int flags) 831 { 832 struct rt_metrics *rmx; 833 struct sockaddr *src, *dst; 834 int sa_len; 835 836 if (flags & NHR_COPY) { 837 /* Copy destination if dst is non-zero */ 838 src = rt_key(rt); 839 dst = info->rti_info[RTAX_DST]; 840 sa_len = src->sa_len; 841 if (dst != NULL) { 842 if (src->sa_len > dst->sa_len) 843 return (ENOMEM); 844 memcpy(dst, src, src->sa_len); 845 info->rti_addrs |= RTA_DST; 846 } 847 848 /* Copy mask if set && dst is non-zero */ 849 src = rt_mask(rt); 850 dst = info->rti_info[RTAX_NETMASK]; 851 if (src != NULL && dst != NULL) { 852 853 /* 854 * Radix stores different value in sa_len, 855 * assume rt_mask() to have the same length 856 * as rt_key() 857 */ 858 if (sa_len > dst->sa_len) 859 return (ENOMEM); 860 memcpy(dst, src, src->sa_len); 861 info->rti_addrs |= RTA_NETMASK; 862 } 863 864 /* Copy gateway is set && dst is non-zero */ 865 src = rt->rt_gateway; 866 dst = info->rti_info[RTAX_GATEWAY]; 867 if ((rt->rt_flags & RTF_GATEWAY) && src != NULL && dst != NULL){ 868 if (src->sa_len > dst->sa_len) 869 return (ENOMEM); 870 memcpy(dst, src, src->sa_len); 871 info->rti_addrs |= RTA_GATEWAY; 872 } 873 } else { 874 info->rti_info[RTAX_DST] = rt_key(rt); 875 info->rti_addrs |= RTA_DST; 876 if (rt_mask(rt) != NULL) { 877 info->rti_info[RTAX_NETMASK] = rt_mask(rt); 878 info->rti_addrs |= RTA_NETMASK; 879 } 880 if (rt->rt_flags & RTF_GATEWAY) { 881 info->rti_info[RTAX_GATEWAY] = rt->rt_gateway; 882 info->rti_addrs |= RTA_GATEWAY; 883 } 884 } 885 886 rmx = info->rti_rmx; 887 if (rmx != NULL) { 888 info->rti_mflags |= RTV_MTU; 889 rmx->rmx_mtu = rt->rt_mtu; 890 } 891 892 info->rti_flags = rt->rt_flags; 893 info->rti_ifp = rt->rt_ifp; 894 info->rti_ifa = rt->rt_ifa; 895 896 if (flags & NHR_REF) { 897 /* Do 'traditional' refcouting */ 898 if_ref(info->rti_ifp); 899 } 900 901 return (0); 902 } 903 904 /* 905 * Lookups up route entry for @dst in RIB database for fib @fibnum. 906 * Exports entry data to @info using rt_exportinfo(). 907 * 908 * if @flags contains NHR_REF, refcouting is performed on rt_ifp. 909 * All references can be released later by calling rib_free_info() 910 * 911 * Returns 0 on success. 912 * Returns ENOENT for lookup failure, ENOMEM for export failure. 913 */ 914 int 915 rib_lookup_info(uint32_t fibnum, const struct sockaddr *dst, uint32_t flags, 916 uint32_t flowid, struct rt_addrinfo *info) 917 { 918 struct rib_head *rh; 919 struct radix_node *rn; 920 struct rtentry *rt; 921 int error; 922 923 KASSERT((fibnum < rt_numfibs), ("rib_lookup_rte: bad fibnum")); 924 rh = rt_tables_get_rnh(fibnum, dst->sa_family); 925 if (rh == NULL) 926 return (ENOENT); 927 928 RIB_RLOCK(rh); 929 rn = rh->rnh_matchaddr(__DECONST(void *, dst), &rh->head); 930 if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) { 931 rt = RNTORT(rn); 932 /* Ensure route & ifp is UP */ 933 if (RT_LINK_IS_UP(rt->rt_ifp)) { 934 flags = (flags & NHR_REF) | NHR_COPY; 935 error = rt_exportinfo(rt, info, flags); 936 RIB_RUNLOCK(rh); 937 938 return (error); 939 } 940 } 941 RIB_RUNLOCK(rh); 942 943 return (ENOENT); 944 } 945 946 /* 947 * Releases all references acquired by rib_lookup_info() when 948 * called with NHR_REF flags. 949 */ 950 void 951 rib_free_info(struct rt_addrinfo *info) 952 { 953 954 if_rele(info->rti_ifp); 955 } 956 957 /* 958 * Iterates over all existing fibs in system calling 959 * @setwa_f function prior to traversing each fib. 960 * Calls @wa_f function for each element in current fib. 961 * If af is not AF_UNSPEC, iterates over fibs in particular 962 * address family. 963 */ 964 void 965 rt_foreach_fib_walk(int af, rt_setwarg_t *setwa_f, rt_walktree_f_t *wa_f, 966 void *arg) 967 { 968 struct rib_head *rnh; 969 uint32_t fibnum; 970 int i; 971 972 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 973 /* Do we want some specific family? */ 974 if (af != AF_UNSPEC) { 975 rnh = rt_tables_get_rnh(fibnum, af); 976 if (rnh == NULL) 977 continue; 978 if (setwa_f != NULL) 979 setwa_f(rnh, fibnum, af, arg); 980 981 RIB_WLOCK(rnh); 982 rnh->rnh_walktree(&rnh->head, (walktree_f_t *)wa_f,arg); 983 RIB_WUNLOCK(rnh); 984 continue; 985 } 986 987 for (i = 1; i <= AF_MAX; i++) { 988 rnh = rt_tables_get_rnh(fibnum, i); 989 if (rnh == NULL) 990 continue; 991 if (setwa_f != NULL) 992 setwa_f(rnh, fibnum, i, arg); 993 994 RIB_WLOCK(rnh); 995 rnh->rnh_walktree(&rnh->head, (walktree_f_t *)wa_f,arg); 996 RIB_WUNLOCK(rnh); 997 } 998 } 999 } 1000 1001 struct rt_delinfo 1002 { 1003 struct rt_addrinfo info; 1004 struct rib_head *rnh; 1005 struct rtentry *head; 1006 }; 1007 1008 /* 1009 * Conditionally unlinks @rn from radix tree based 1010 * on info data passed in @arg. 1011 */ 1012 static int 1013 rt_checkdelroute(struct radix_node *rn, void *arg) 1014 { 1015 struct rt_delinfo *di; 1016 struct rt_addrinfo *info; 1017 struct rtentry *rt; 1018 int error; 1019 1020 di = (struct rt_delinfo *)arg; 1021 rt = (struct rtentry *)rn; 1022 info = &di->info; 1023 error = 0; 1024 1025 info->rti_info[RTAX_DST] = rt_key(rt); 1026 info->rti_info[RTAX_NETMASK] = rt_mask(rt); 1027 info->rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1028 1029 rt = rt_unlinkrte(di->rnh, info, &error); 1030 if (rt == NULL) { 1031 /* Either not allowed or not matched. Skip entry */ 1032 return (0); 1033 } 1034 1035 /* Entry was unlinked. Add to the list and return */ 1036 rt->rt_chain = di->head; 1037 di->head = rt; 1038 1039 return (0); 1040 } 1041 1042 /* 1043 * Iterates over all existing fibs in system. 1044 * Deletes each element for which @filter_f function returned 1045 * non-zero value. 1046 * If @af is not AF_UNSPEC, iterates over fibs in particular 1047 * address family. 1048 */ 1049 void 1050 rt_foreach_fib_walk_del(int af, rt_filter_f_t *filter_f, void *arg) 1051 { 1052 struct rib_head *rnh; 1053 struct rt_delinfo di; 1054 struct rtentry *rt; 1055 uint32_t fibnum; 1056 int i, start, end; 1057 1058 bzero(&di, sizeof(di)); 1059 di.info.rti_filter = filter_f; 1060 di.info.rti_filterdata = arg; 1061 1062 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 1063 /* Do we want some specific family? */ 1064 if (af != AF_UNSPEC) { 1065 start = af; 1066 end = af; 1067 } else { 1068 start = 1; 1069 end = AF_MAX; 1070 } 1071 1072 for (i = start; i <= end; i++) { 1073 rnh = rt_tables_get_rnh(fibnum, i); 1074 if (rnh == NULL) 1075 continue; 1076 di.rnh = rnh; 1077 1078 RIB_WLOCK(rnh); 1079 rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di); 1080 RIB_WUNLOCK(rnh); 1081 1082 if (di.head == NULL) 1083 continue; 1084 1085 /* We might have something to reclaim */ 1086 while (di.head != NULL) { 1087 rt = di.head; 1088 di.head = rt->rt_chain; 1089 rt->rt_chain = NULL; 1090 1091 /* TODO std rt -> rt_addrinfo export */ 1092 di.info.rti_info[RTAX_DST] = rt_key(rt); 1093 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt); 1094 1095 rt_notifydelete(rt, &di.info); 1096 RTFREE_LOCKED(rt); 1097 } 1098 1099 } 1100 } 1101 } 1102 1103 /* 1104 * Delete Routes for a Network Interface 1105 * 1106 * Called for each routing entry via the rnh->rnh_walktree() call above 1107 * to delete all route entries referencing a detaching network interface. 1108 * 1109 * Arguments: 1110 * rt pointer to rtentry 1111 * arg argument passed to rnh->rnh_walktree() - detaching interface 1112 * 1113 * Returns: 1114 * 0 successful 1115 * errno failed - reason indicated 1116 */ 1117 static int 1118 rt_ifdelroute(const struct rtentry *rt, void *arg) 1119 { 1120 struct ifnet *ifp = arg; 1121 1122 if (rt->rt_ifp != ifp) 1123 return (0); 1124 1125 /* 1126 * Protect (sorta) against walktree recursion problems 1127 * with cloned routes 1128 */ 1129 if ((rt->rt_flags & RTF_UP) == 0) 1130 return (0); 1131 1132 return (1); 1133 } 1134 1135 /* 1136 * Delete all remaining routes using this interface 1137 * Unfortuneatly the only way to do this is to slog through 1138 * the entire routing table looking for routes which point 1139 * to this interface...oh well... 1140 */ 1141 void 1142 rt_flushifroutes(struct ifnet *ifp) 1143 { 1144 1145 rt_foreach_fib_walk_del(AF_UNSPEC, rt_ifdelroute, ifp); 1146 } 1147 1148 /* 1149 * Conditionally unlinks rtentry matching data inside @info from @rnh. 1150 * Returns unlinked, locked and referenced @rtentry on success, 1151 * Returns NULL and sets @perror to: 1152 * ESRCH - if prefix was not found, 1153 * EADDRINUSE - if trying to delete PINNED route without appropriate flag. 1154 * ENOENT - if supplied filter function returned 0 (not matched). 1155 */ 1156 static struct rtentry * 1157 rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info, int *perror) 1158 { 1159 struct sockaddr *dst, *netmask; 1160 struct rtentry *rt; 1161 struct radix_node *rn; 1162 1163 dst = info->rti_info[RTAX_DST]; 1164 netmask = info->rti_info[RTAX_NETMASK]; 1165 1166 rt = (struct rtentry *)rnh->rnh_lookup(dst, netmask, &rnh->head); 1167 if (rt == NULL) { 1168 *perror = ESRCH; 1169 return (NULL); 1170 } 1171 1172 if ((info->rti_flags & RTF_PINNED) == 0) { 1173 /* Check if target route can be deleted */ 1174 if (rt->rt_flags & RTF_PINNED) { 1175 *perror = EADDRINUSE; 1176 return (NULL); 1177 } 1178 } 1179 1180 if (info->rti_filter != NULL) { 1181 if (info->rti_filter(rt, info->rti_filterdata) == 0) { 1182 /* Not matched */ 1183 *perror = ENOENT; 1184 return (NULL); 1185 } 1186 1187 /* 1188 * Filter function requested rte deletion. 1189 * Ease the caller work by filling in remaining info 1190 * from that particular entry. 1191 */ 1192 info->rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1193 } 1194 1195 /* 1196 * Remove the item from the tree and return it. 1197 * Complain if it is not there and do no more processing. 1198 */ 1199 *perror = ESRCH; 1200 #ifdef RADIX_MPATH 1201 if (rt_mpath_capable(rnh)) 1202 rn = rt_mpath_unlink(rnh, info, rt, perror); 1203 else 1204 #endif 1205 rn = rnh->rnh_deladdr(dst, netmask, &rnh->head); 1206 if (rn == NULL) 1207 return (NULL); 1208 1209 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 1210 panic ("rtrequest delete"); 1211 1212 rt = RNTORT(rn); 1213 RT_LOCK(rt); 1214 RT_ADDREF(rt); 1215 rt->rt_flags &= ~RTF_UP; 1216 1217 *perror = 0; 1218 1219 return (rt); 1220 } 1221 1222 static void 1223 rt_notifydelete(struct rtentry *rt, struct rt_addrinfo *info) 1224 { 1225 struct ifaddr *ifa; 1226 1227 /* 1228 * give the protocol a chance to keep things in sync. 1229 */ 1230 ifa = rt->rt_ifa; 1231 if (ifa != NULL && ifa->ifa_rtrequest != NULL) 1232 ifa->ifa_rtrequest(RTM_DELETE, rt, info); 1233 1234 /* 1235 * One more rtentry floating around that is not 1236 * linked to the routing table. rttrash will be decremented 1237 * when RTFREE(rt) is eventually called. 1238 */ 1239 V_rttrash++; 1240 } 1241 1242 1243 /* 1244 * These (questionable) definitions of apparent local variables apply 1245 * to the next two functions. XXXXXX!!! 1246 */ 1247 #define dst info->rti_info[RTAX_DST] 1248 #define gateway info->rti_info[RTAX_GATEWAY] 1249 #define netmask info->rti_info[RTAX_NETMASK] 1250 #define ifaaddr info->rti_info[RTAX_IFA] 1251 #define ifpaddr info->rti_info[RTAX_IFP] 1252 #define flags info->rti_flags 1253 1254 /* 1255 * Look up rt_addrinfo for a specific fib. Note that if rti_ifa is defined, 1256 * it will be referenced so the caller must free it. 1257 */ 1258 int 1259 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum) 1260 { 1261 struct ifaddr *ifa; 1262 int error = 0; 1263 1264 /* 1265 * ifp may be specified by sockaddr_dl 1266 * when protocol address is ambiguous. 1267 */ 1268 if (info->rti_ifp == NULL && ifpaddr != NULL && 1269 ifpaddr->sa_family == AF_LINK && 1270 (ifa = ifa_ifwithnet(ifpaddr, 0, fibnum)) != NULL) { 1271 info->rti_ifp = ifa->ifa_ifp; 1272 ifa_free(ifa); 1273 } 1274 if (info->rti_ifa == NULL && ifaaddr != NULL) 1275 info->rti_ifa = ifa_ifwithaddr(ifaaddr); 1276 if (info->rti_ifa == NULL) { 1277 struct sockaddr *sa; 1278 1279 sa = ifaaddr != NULL ? ifaaddr : 1280 (gateway != NULL ? gateway : dst); 1281 if (sa != NULL && info->rti_ifp != NULL) 1282 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp); 1283 else if (dst != NULL && gateway != NULL) 1284 info->rti_ifa = ifa_ifwithroute(flags, dst, gateway, 1285 fibnum); 1286 else if (sa != NULL) 1287 info->rti_ifa = ifa_ifwithroute(flags, sa, sa, 1288 fibnum); 1289 } 1290 if ((ifa = info->rti_ifa) != NULL) { 1291 if (info->rti_ifp == NULL) 1292 info->rti_ifp = ifa->ifa_ifp; 1293 } else 1294 error = ENETUNREACH; 1295 return (error); 1296 } 1297 1298 static int 1299 if_updatemtu_cb(struct radix_node *rn, void *arg) 1300 { 1301 struct rtentry *rt; 1302 struct if_mtuinfo *ifmtu; 1303 1304 rt = (struct rtentry *)rn; 1305 ifmtu = (struct if_mtuinfo *)arg; 1306 1307 if (rt->rt_ifp != ifmtu->ifp) 1308 return (0); 1309 1310 if (rt->rt_mtu >= ifmtu->mtu) { 1311 /* We have to decrease mtu regardless of flags */ 1312 rt->rt_mtu = ifmtu->mtu; 1313 return (0); 1314 } 1315 1316 /* 1317 * New MTU is bigger. Check if are allowed to alter it 1318 */ 1319 if ((rt->rt_flags & (RTF_FIXEDMTU | RTF_GATEWAY | RTF_HOST)) != 0) { 1320 1321 /* 1322 * Skip routes with user-supplied MTU and 1323 * non-interface routes 1324 */ 1325 return (0); 1326 } 1327 1328 /* We are safe to update route MTU */ 1329 rt->rt_mtu = ifmtu->mtu; 1330 1331 return (0); 1332 } 1333 1334 void 1335 rt_updatemtu(struct ifnet *ifp) 1336 { 1337 struct if_mtuinfo ifmtu; 1338 struct rib_head *rnh; 1339 int i, j; 1340 1341 ifmtu.ifp = ifp; 1342 1343 /* 1344 * Try to update rt_mtu for all routes using this interface 1345 * Unfortunately the only way to do this is to traverse all 1346 * routing tables in all fibs/domains. 1347 */ 1348 for (i = 1; i <= AF_MAX; i++) { 1349 ifmtu.mtu = if_getmtu_family(ifp, i); 1350 for (j = 0; j < rt_numfibs; j++) { 1351 rnh = rt_tables_get_rnh(j, i); 1352 if (rnh == NULL) 1353 continue; 1354 RIB_WLOCK(rnh); 1355 rnh->rnh_walktree(&rnh->head, if_updatemtu_cb, &ifmtu); 1356 RIB_WUNLOCK(rnh); 1357 } 1358 } 1359 } 1360 1361 1362 #if 0 1363 int p_sockaddr(char *buf, int buflen, struct sockaddr *s); 1364 int rt_print(char *buf, int buflen, struct rtentry *rt); 1365 1366 int 1367 p_sockaddr(char *buf, int buflen, struct sockaddr *s) 1368 { 1369 void *paddr = NULL; 1370 1371 switch (s->sa_family) { 1372 case AF_INET: 1373 paddr = &((struct sockaddr_in *)s)->sin_addr; 1374 break; 1375 case AF_INET6: 1376 paddr = &((struct sockaddr_in6 *)s)->sin6_addr; 1377 break; 1378 } 1379 1380 if (paddr == NULL) 1381 return (0); 1382 1383 if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL) 1384 return (0); 1385 1386 return (strlen(buf)); 1387 } 1388 1389 int 1390 rt_print(char *buf, int buflen, struct rtentry *rt) 1391 { 1392 struct sockaddr *addr, *mask; 1393 int i = 0; 1394 1395 addr = rt_key(rt); 1396 mask = rt_mask(rt); 1397 1398 i = p_sockaddr(buf, buflen, addr); 1399 if (!(rt->rt_flags & RTF_HOST)) { 1400 buf[i++] = '/'; 1401 i += p_sockaddr(buf + i, buflen - i, mask); 1402 } 1403 1404 if (rt->rt_flags & RTF_GATEWAY) { 1405 buf[i++] = '>'; 1406 i += p_sockaddr(buf + i, buflen - i, rt->rt_gateway); 1407 } 1408 1409 return (i); 1410 } 1411 #endif 1412 1413 #ifdef RADIX_MPATH 1414 /* 1415 * Deletes key for single-path routes, unlinks rtentry with 1416 * gateway specified in @info from multi-path routes. 1417 * 1418 * Returnes unlinked entry. In case of failure, returns NULL 1419 * and sets @perror to ESRCH. 1420 */ 1421 static struct radix_node * 1422 rt_mpath_unlink(struct rib_head *rnh, struct rt_addrinfo *info, 1423 struct rtentry *rto, int *perror) 1424 { 1425 /* 1426 * if we got multipath routes, we require users to specify 1427 * a matching RTAX_GATEWAY. 1428 */ 1429 struct rtentry *rt; // *rto = NULL; 1430 struct radix_node *rn; 1431 struct sockaddr *gw; 1432 1433 gw = info->rti_info[RTAX_GATEWAY]; 1434 rt = rt_mpath_matchgate(rto, gw); 1435 if (rt == NULL) { 1436 *perror = ESRCH; 1437 return (NULL); 1438 } 1439 1440 /* 1441 * this is the first entry in the chain 1442 */ 1443 if (rto == rt) { 1444 rn = rn_mpath_next((struct radix_node *)rt); 1445 /* 1446 * there is another entry, now it's active 1447 */ 1448 if (rn) { 1449 rto = RNTORT(rn); 1450 RT_LOCK(rto); 1451 rto->rt_flags |= RTF_UP; 1452 RT_UNLOCK(rto); 1453 } else if (rt->rt_flags & RTF_GATEWAY) { 1454 /* 1455 * For gateway routes, we need to 1456 * make sure that we we are deleting 1457 * the correct gateway. 1458 * rt_mpath_matchgate() does not 1459 * check the case when there is only 1460 * one route in the chain. 1461 */ 1462 if (gw && 1463 (rt->rt_gateway->sa_len != gw->sa_len || 1464 memcmp(rt->rt_gateway, gw, gw->sa_len))) { 1465 *perror = ESRCH; 1466 return (NULL); 1467 } 1468 } 1469 1470 /* 1471 * use the normal delete code to remove 1472 * the first entry 1473 */ 1474 rn = rnh->rnh_deladdr(dst, netmask, &rnh->head); 1475 *perror = 0; 1476 return (rn); 1477 } 1478 1479 /* 1480 * if the entry is 2nd and on up 1481 */ 1482 if (rt_mpath_deldup(rto, rt) == 0) 1483 panic ("rtrequest1: rt_mpath_deldup"); 1484 *perror = 0; 1485 rn = (struct radix_node *)rt; 1486 return (rn); 1487 } 1488 #endif 1489 1490 #ifdef FLOWTABLE 1491 static struct rtentry * 1492 rt_flowtable_check_route(struct rib_head *rnh, struct rt_addrinfo *info) 1493 { 1494 #if defined(INET6) || defined(INET) 1495 struct radix_node *rn; 1496 #endif 1497 struct rtentry *rt0; 1498 1499 rt0 = NULL; 1500 /* "flow-table" only supports IPv6 and IPv4 at the moment. */ 1501 switch (dst->sa_family) { 1502 #ifdef INET6 1503 case AF_INET6: 1504 #endif 1505 #ifdef INET 1506 case AF_INET: 1507 #endif 1508 #if defined(INET6) || defined(INET) 1509 rn = rnh->rnh_matchaddr(dst, &rnh->head); 1510 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) { 1511 struct sockaddr *mask; 1512 u_char *m, *n; 1513 int len; 1514 1515 /* 1516 * compare mask to see if the new route is 1517 * more specific than the existing one 1518 */ 1519 rt0 = RNTORT(rn); 1520 RT_LOCK(rt0); 1521 RT_ADDREF(rt0); 1522 RT_UNLOCK(rt0); 1523 /* 1524 * A host route is already present, so 1525 * leave the flow-table entries as is. 1526 */ 1527 if (rt0->rt_flags & RTF_HOST) { 1528 RTFREE(rt0); 1529 rt0 = NULL; 1530 } else if (!(flags & RTF_HOST) && netmask) { 1531 mask = rt_mask(rt0); 1532 len = mask->sa_len; 1533 m = (u_char *)mask; 1534 n = (u_char *)netmask; 1535 while (len-- > 0) { 1536 if (*n != *m) 1537 break; 1538 n++; 1539 m++; 1540 } 1541 if (len == 0 || (*n < *m)) { 1542 RTFREE(rt0); 1543 rt0 = NULL; 1544 } 1545 } 1546 } 1547 #endif/* INET6 || INET */ 1548 } 1549 1550 return (rt0); 1551 } 1552 #endif 1553 1554 int 1555 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt, 1556 u_int fibnum) 1557 { 1558 int error = 0; 1559 struct rtentry *rt, *rt_old; 1560 #ifdef FLOWTABLE 1561 struct rtentry *rt0; 1562 #endif 1563 struct radix_node *rn; 1564 struct rib_head *rnh; 1565 struct ifaddr *ifa; 1566 struct sockaddr *ndst; 1567 struct sockaddr_storage mdst; 1568 1569 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum")); 1570 KASSERT((flags & RTF_RNH_LOCKED) == 0, ("rtrequest1_fib: locked")); 1571 switch (dst->sa_family) { 1572 case AF_INET6: 1573 case AF_INET: 1574 /* We support multiple FIBs. */ 1575 break; 1576 default: 1577 fibnum = RT_DEFAULT_FIB; 1578 break; 1579 } 1580 1581 /* 1582 * Find the correct routing tree to use for this Address Family 1583 */ 1584 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 1585 if (rnh == NULL) 1586 return (EAFNOSUPPORT); 1587 1588 /* 1589 * If we are adding a host route then we don't want to put 1590 * a netmask in the tree, nor do we want to clone it. 1591 */ 1592 if (flags & RTF_HOST) 1593 netmask = NULL; 1594 1595 switch (req) { 1596 case RTM_DELETE: 1597 if (netmask) { 1598 rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask); 1599 dst = (struct sockaddr *)&mdst; 1600 } 1601 1602 RIB_WLOCK(rnh); 1603 rt = rt_unlinkrte(rnh, info, &error); 1604 RIB_WUNLOCK(rnh); 1605 if (error != 0) 1606 return (error); 1607 1608 rt_notifydelete(rt, info); 1609 1610 /* 1611 * If the caller wants it, then it can have it, 1612 * but it's up to it to free the rtentry as we won't be 1613 * doing it. 1614 */ 1615 if (ret_nrt) { 1616 *ret_nrt = rt; 1617 RT_UNLOCK(rt); 1618 } else 1619 RTFREE_LOCKED(rt); 1620 break; 1621 case RTM_RESOLVE: 1622 /* 1623 * resolve was only used for route cloning 1624 * here for compat 1625 */ 1626 break; 1627 case RTM_ADD: 1628 if ((flags & RTF_GATEWAY) && !gateway) 1629 return (EINVAL); 1630 if (dst && gateway && (dst->sa_family != gateway->sa_family) && 1631 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK)) 1632 return (EINVAL); 1633 1634 if (info->rti_ifa == NULL) { 1635 error = rt_getifa_fib(info, fibnum); 1636 if (error) 1637 return (error); 1638 } else 1639 ifa_ref(info->rti_ifa); 1640 ifa = info->rti_ifa; 1641 rt = uma_zalloc(V_rtzone, M_NOWAIT); 1642 if (rt == NULL) { 1643 ifa_free(ifa); 1644 return (ENOBUFS); 1645 } 1646 rt->rt_flags = RTF_UP | flags; 1647 rt->rt_fibnum = fibnum; 1648 /* 1649 * Add the gateway. Possibly re-malloc-ing the storage for it. 1650 */ 1651 if ((error = rt_setgate(rt, dst, gateway)) != 0) { 1652 ifa_free(ifa); 1653 uma_zfree(V_rtzone, rt); 1654 return (error); 1655 } 1656 1657 /* 1658 * point to the (possibly newly malloc'd) dest address. 1659 */ 1660 ndst = (struct sockaddr *)rt_key(rt); 1661 1662 /* 1663 * make sure it contains the value we want (masked if needed). 1664 */ 1665 if (netmask) { 1666 rt_maskedcopy(dst, ndst, netmask); 1667 } else 1668 bcopy(dst, ndst, dst->sa_len); 1669 1670 /* 1671 * We use the ifa reference returned by rt_getifa_fib(). 1672 * This moved from below so that rnh->rnh_addaddr() can 1673 * examine the ifa and ifa->ifa_ifp if it so desires. 1674 */ 1675 rt->rt_ifa = ifa; 1676 rt->rt_ifp = ifa->ifa_ifp; 1677 rt->rt_weight = 1; 1678 1679 rt_setmetrics(info, rt); 1680 1681 RIB_WLOCK(rnh); 1682 RT_LOCK(rt); 1683 #ifdef RADIX_MPATH 1684 /* do not permit exactly the same dst/mask/gw pair */ 1685 if (rt_mpath_capable(rnh) && 1686 rt_mpath_conflict(rnh, rt, netmask)) { 1687 RIB_WUNLOCK(rnh); 1688 1689 ifa_free(rt->rt_ifa); 1690 R_Free(rt_key(rt)); 1691 uma_zfree(V_rtzone, rt); 1692 return (EEXIST); 1693 } 1694 #endif 1695 1696 #ifdef FLOWTABLE 1697 rt0 = rt_flowtable_check_route(rnh, info); 1698 #endif /* FLOWTABLE */ 1699 1700 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */ 1701 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, rt->rt_nodes); 1702 1703 rt_old = NULL; 1704 if (rn == NULL && (info->rti_flags & RTF_PINNED) != 0) { 1705 1706 /* 1707 * Force removal and re-try addition 1708 * TODO: better multipath&pinned support 1709 */ 1710 struct sockaddr *info_dst = info->rti_info[RTAX_DST]; 1711 info->rti_info[RTAX_DST] = ndst; 1712 /* Do not delete existing PINNED(interface) routes */ 1713 info->rti_flags &= ~RTF_PINNED; 1714 rt_old = rt_unlinkrte(rnh, info, &error); 1715 info->rti_flags |= RTF_PINNED; 1716 info->rti_info[RTAX_DST] = info_dst; 1717 if (rt_old != NULL) 1718 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, 1719 rt->rt_nodes); 1720 } 1721 RIB_WUNLOCK(rnh); 1722 1723 if (rt_old != NULL) 1724 RT_UNLOCK(rt_old); 1725 1726 /* 1727 * If it still failed to go into the tree, 1728 * then un-make it (this should be a function) 1729 */ 1730 if (rn == NULL) { 1731 ifa_free(rt->rt_ifa); 1732 R_Free(rt_key(rt)); 1733 uma_zfree(V_rtzone, rt); 1734 #ifdef FLOWTABLE 1735 if (rt0 != NULL) 1736 RTFREE(rt0); 1737 #endif 1738 return (EEXIST); 1739 } 1740 #ifdef FLOWTABLE 1741 else if (rt0 != NULL) { 1742 flowtable_route_flush(dst->sa_family, rt0); 1743 RTFREE(rt0); 1744 } 1745 #endif 1746 1747 if (rt_old != NULL) { 1748 rt_notifydelete(rt_old, info); 1749 RTFREE(rt_old); 1750 } 1751 1752 /* 1753 * If this protocol has something to add to this then 1754 * allow it to do that as well. 1755 */ 1756 if (ifa->ifa_rtrequest) 1757 ifa->ifa_rtrequest(req, rt, info); 1758 1759 /* 1760 * actually return a resultant rtentry and 1761 * give the caller a single reference. 1762 */ 1763 if (ret_nrt) { 1764 *ret_nrt = rt; 1765 RT_ADDREF(rt); 1766 } 1767 rnh->rnh_gen++; /* Routing table updated */ 1768 RT_UNLOCK(rt); 1769 break; 1770 case RTM_CHANGE: 1771 RIB_WLOCK(rnh); 1772 error = rtrequest1_fib_change(rnh, info, ret_nrt, fibnum); 1773 RIB_WUNLOCK(rnh); 1774 break; 1775 default: 1776 error = EOPNOTSUPP; 1777 } 1778 1779 return (error); 1780 } 1781 1782 #undef dst 1783 #undef gateway 1784 #undef netmask 1785 #undef ifaaddr 1786 #undef ifpaddr 1787 #undef flags 1788 1789 static int 1790 rtrequest1_fib_change(struct rib_head *rnh, struct rt_addrinfo *info, 1791 struct rtentry **ret_nrt, u_int fibnum) 1792 { 1793 struct rtentry *rt = NULL; 1794 int error = 0; 1795 int free_ifa = 0; 1796 int family, mtu; 1797 struct if_mtuinfo ifmtu; 1798 1799 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST], 1800 info->rti_info[RTAX_NETMASK], &rnh->head); 1801 1802 if (rt == NULL) 1803 return (ESRCH); 1804 1805 #ifdef RADIX_MPATH 1806 /* 1807 * If we got multipath routes, 1808 * we require users to specify a matching RTAX_GATEWAY. 1809 */ 1810 if (rt_mpath_capable(rnh)) { 1811 rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]); 1812 if (rt == NULL) 1813 return (ESRCH); 1814 } 1815 #endif 1816 1817 RT_LOCK(rt); 1818 1819 rt_setmetrics(info, rt); 1820 1821 /* 1822 * New gateway could require new ifaddr, ifp; 1823 * flags may also be different; ifp may be specified 1824 * by ll sockaddr when protocol address is ambiguous 1825 */ 1826 if (((rt->rt_flags & RTF_GATEWAY) && 1827 info->rti_info[RTAX_GATEWAY] != NULL) || 1828 info->rti_info[RTAX_IFP] != NULL || 1829 (info->rti_info[RTAX_IFA] != NULL && 1830 !sa_equal(info->rti_info[RTAX_IFA], rt->rt_ifa->ifa_addr))) { 1831 1832 error = rt_getifa_fib(info, fibnum); 1833 if (info->rti_ifa != NULL) 1834 free_ifa = 1; 1835 1836 if (error != 0) 1837 goto bad; 1838 } 1839 1840 /* Check if outgoing interface has changed */ 1841 if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa && 1842 rt->rt_ifa != NULL && rt->rt_ifa->ifa_rtrequest != NULL) { 1843 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, info); 1844 ifa_free(rt->rt_ifa); 1845 } 1846 /* Update gateway address */ 1847 if (info->rti_info[RTAX_GATEWAY] != NULL) { 1848 error = rt_setgate(rt, rt_key(rt), info->rti_info[RTAX_GATEWAY]); 1849 if (error != 0) 1850 goto bad; 1851 1852 rt->rt_flags &= ~RTF_GATEWAY; 1853 rt->rt_flags |= (RTF_GATEWAY & info->rti_flags); 1854 } 1855 1856 if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa) { 1857 ifa_ref(info->rti_ifa); 1858 rt->rt_ifa = info->rti_ifa; 1859 rt->rt_ifp = info->rti_ifp; 1860 } 1861 /* Allow some flags to be toggled on change. */ 1862 rt->rt_flags &= ~RTF_FMASK; 1863 rt->rt_flags |= info->rti_flags & RTF_FMASK; 1864 1865 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest != NULL) 1866 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, info); 1867 1868 /* Alter route MTU if necessary */ 1869 if (rt->rt_ifp != NULL) { 1870 family = info->rti_info[RTAX_DST]->sa_family; 1871 mtu = if_getmtu_family(rt->rt_ifp, family); 1872 /* Set default MTU */ 1873 if (rt->rt_mtu == 0) 1874 rt->rt_mtu = mtu; 1875 if (rt->rt_mtu != mtu) { 1876 /* Check if we really need to update */ 1877 ifmtu.ifp = rt->rt_ifp; 1878 ifmtu.mtu = mtu; 1879 if_updatemtu_cb(rt->rt_nodes, &ifmtu); 1880 } 1881 } 1882 1883 if (ret_nrt) { 1884 *ret_nrt = rt; 1885 RT_ADDREF(rt); 1886 } 1887 bad: 1888 RT_UNLOCK(rt); 1889 if (free_ifa != 0) 1890 ifa_free(info->rti_ifa); 1891 return (error); 1892 } 1893 1894 static void 1895 rt_setmetrics(const struct rt_addrinfo *info, struct rtentry *rt) 1896 { 1897 1898 if (info->rti_mflags & RTV_MTU) { 1899 if (info->rti_rmx->rmx_mtu != 0) { 1900 1901 /* 1902 * MTU was explicitly provided by user. 1903 * Keep it. 1904 */ 1905 rt->rt_flags |= RTF_FIXEDMTU; 1906 } else { 1907 1908 /* 1909 * User explicitly sets MTU to 0. 1910 * Assume rollback to default. 1911 */ 1912 rt->rt_flags &= ~RTF_FIXEDMTU; 1913 } 1914 rt->rt_mtu = info->rti_rmx->rmx_mtu; 1915 } 1916 if (info->rti_mflags & RTV_WEIGHT) 1917 rt->rt_weight = info->rti_rmx->rmx_weight; 1918 /* Kernel -> userland timebase conversion. */ 1919 if (info->rti_mflags & RTV_EXPIRE) 1920 rt->rt_expire = info->rti_rmx->rmx_expire ? 1921 info->rti_rmx->rmx_expire - time_second + time_uptime : 0; 1922 } 1923 1924 int 1925 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate) 1926 { 1927 /* XXX dst may be overwritten, can we move this to below */ 1928 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate); 1929 1930 /* 1931 * Prepare to store the gateway in rt->rt_gateway. 1932 * Both dst and gateway are stored one after the other in the same 1933 * malloc'd chunk. If we have room, we can reuse the old buffer, 1934 * rt_gateway already points to the right place. 1935 * Otherwise, malloc a new block and update the 'dst' address. 1936 */ 1937 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) { 1938 caddr_t new; 1939 1940 R_Malloc(new, caddr_t, dlen + glen); 1941 if (new == NULL) 1942 return ENOBUFS; 1943 /* 1944 * XXX note, we copy from *dst and not *rt_key(rt) because 1945 * rt_setgate() can be called to initialize a newly 1946 * allocated route entry, in which case rt_key(rt) == NULL 1947 * (and also rt->rt_gateway == NULL). 1948 * Free()/free() handle a NULL argument just fine. 1949 */ 1950 bcopy(dst, new, dlen); 1951 R_Free(rt_key(rt)); /* free old block, if any */ 1952 rt_key(rt) = (struct sockaddr *)new; 1953 rt->rt_gateway = (struct sockaddr *)(new + dlen); 1954 } 1955 1956 /* 1957 * Copy the new gateway value into the memory chunk. 1958 */ 1959 bcopy(gate, rt->rt_gateway, glen); 1960 1961 return (0); 1962 } 1963 1964 void 1965 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask) 1966 { 1967 u_char *cp1 = (u_char *)src; 1968 u_char *cp2 = (u_char *)dst; 1969 u_char *cp3 = (u_char *)netmask; 1970 u_char *cplim = cp2 + *cp3; 1971 u_char *cplim2 = cp2 + *cp1; 1972 1973 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */ 1974 cp3 += 2; 1975 if (cplim > cplim2) 1976 cplim = cplim2; 1977 while (cp2 < cplim) 1978 *cp2++ = *cp1++ & *cp3++; 1979 if (cp2 < cplim2) 1980 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2)); 1981 } 1982 1983 /* 1984 * Set up a routing table entry, normally 1985 * for an interface. 1986 */ 1987 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */ 1988 static inline int 1989 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum) 1990 { 1991 struct sockaddr *dst; 1992 struct sockaddr *netmask; 1993 struct rtentry *rt = NULL; 1994 struct rt_addrinfo info; 1995 int error = 0; 1996 int startfib, endfib; 1997 char tempbuf[_SOCKADDR_TMPSIZE]; 1998 int didwork = 0; 1999 int a_failure = 0; 2000 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 2001 struct rib_head *rnh; 2002 2003 if (flags & RTF_HOST) { 2004 dst = ifa->ifa_dstaddr; 2005 netmask = NULL; 2006 } else { 2007 dst = ifa->ifa_addr; 2008 netmask = ifa->ifa_netmask; 2009 } 2010 if (dst->sa_len == 0) 2011 return(EINVAL); 2012 switch (dst->sa_family) { 2013 case AF_INET6: 2014 case AF_INET: 2015 /* We support multiple FIBs. */ 2016 break; 2017 default: 2018 fibnum = RT_DEFAULT_FIB; 2019 break; 2020 } 2021 if (fibnum == RT_ALL_FIBS) { 2022 if (V_rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD) 2023 startfib = endfib = ifa->ifa_ifp->if_fib; 2024 else { 2025 startfib = 0; 2026 endfib = rt_numfibs - 1; 2027 } 2028 } else { 2029 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum")); 2030 startfib = fibnum; 2031 endfib = fibnum; 2032 } 2033 2034 /* 2035 * If it's a delete, check that if it exists, 2036 * it's on the correct interface or we might scrub 2037 * a route to another ifa which would 2038 * be confusing at best and possibly worse. 2039 */ 2040 if (cmd == RTM_DELETE) { 2041 /* 2042 * It's a delete, so it should already exist.. 2043 * If it's a net, mask off the host bits 2044 * (Assuming we have a mask) 2045 * XXX this is kinda inet specific.. 2046 */ 2047 if (netmask != NULL) { 2048 rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask); 2049 dst = (struct sockaddr *)tempbuf; 2050 } 2051 } 2052 /* 2053 * Now go through all the requested tables (fibs) and do the 2054 * requested action. Realistically, this will either be fib 0 2055 * for protocols that don't do multiple tables or all the 2056 * tables for those that do. 2057 */ 2058 for ( fibnum = startfib; fibnum <= endfib; fibnum++) { 2059 if (cmd == RTM_DELETE) { 2060 struct radix_node *rn; 2061 /* 2062 * Look up an rtentry that is in the routing tree and 2063 * contains the correct info. 2064 */ 2065 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 2066 if (rnh == NULL) 2067 /* this table doesn't exist but others might */ 2068 continue; 2069 RIB_RLOCK(rnh); 2070 rn = rnh->rnh_lookup(dst, netmask, &rnh->head); 2071 #ifdef RADIX_MPATH 2072 if (rt_mpath_capable(rnh)) { 2073 2074 if (rn == NULL) 2075 error = ESRCH; 2076 else { 2077 rt = RNTORT(rn); 2078 /* 2079 * for interface route the 2080 * rt->rt_gateway is sockaddr_intf 2081 * for cloning ARP entries, so 2082 * rt_mpath_matchgate must use the 2083 * interface address 2084 */ 2085 rt = rt_mpath_matchgate(rt, 2086 ifa->ifa_addr); 2087 if (rt == NULL) 2088 error = ESRCH; 2089 } 2090 } 2091 #endif 2092 error = (rn == NULL || 2093 (rn->rn_flags & RNF_ROOT) || 2094 RNTORT(rn)->rt_ifa != ifa); 2095 RIB_RUNLOCK(rnh); 2096 if (error) { 2097 /* this is only an error if bad on ALL tables */ 2098 continue; 2099 } 2100 } 2101 /* 2102 * Do the actual request 2103 */ 2104 bzero((caddr_t)&info, sizeof(info)); 2105 info.rti_ifa = ifa; 2106 info.rti_flags = flags | 2107 (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED; 2108 info.rti_info[RTAX_DST] = dst; 2109 /* 2110 * doing this for compatibility reasons 2111 */ 2112 if (cmd == RTM_ADD) 2113 info.rti_info[RTAX_GATEWAY] = 2114 (struct sockaddr *)&null_sdl; 2115 else 2116 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr; 2117 info.rti_info[RTAX_NETMASK] = netmask; 2118 error = rtrequest1_fib(cmd, &info, &rt, fibnum); 2119 2120 if (error == 0 && rt != NULL) { 2121 /* 2122 * notify any listening routing agents of the change 2123 */ 2124 RT_LOCK(rt); 2125 #ifdef RADIX_MPATH 2126 /* 2127 * in case address alias finds the first address 2128 * e.g. ifconfig bge0 192.0.2.246/24 2129 * e.g. ifconfig bge0 192.0.2.247/24 2130 * the address set in the route is 192.0.2.246 2131 * so we need to replace it with 192.0.2.247 2132 */ 2133 if (memcmp(rt->rt_ifa->ifa_addr, 2134 ifa->ifa_addr, ifa->ifa_addr->sa_len)) { 2135 ifa_free(rt->rt_ifa); 2136 ifa_ref(ifa); 2137 rt->rt_ifp = ifa->ifa_ifp; 2138 rt->rt_ifa = ifa; 2139 } 2140 #endif 2141 /* 2142 * doing this for compatibility reasons 2143 */ 2144 if (cmd == RTM_ADD) { 2145 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type = 2146 rt->rt_ifp->if_type; 2147 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index = 2148 rt->rt_ifp->if_index; 2149 } 2150 RT_ADDREF(rt); 2151 RT_UNLOCK(rt); 2152 rt_newaddrmsg_fib(cmd, ifa, error, rt, fibnum); 2153 RT_LOCK(rt); 2154 RT_REMREF(rt); 2155 if (cmd == RTM_DELETE) { 2156 /* 2157 * If we are deleting, and we found an entry, 2158 * then it's been removed from the tree.. 2159 * now throw it away. 2160 */ 2161 RTFREE_LOCKED(rt); 2162 } else { 2163 if (cmd == RTM_ADD) { 2164 /* 2165 * We just wanted to add it.. 2166 * we don't actually need a reference. 2167 */ 2168 RT_REMREF(rt); 2169 } 2170 RT_UNLOCK(rt); 2171 } 2172 didwork = 1; 2173 } 2174 if (error) 2175 a_failure = error; 2176 } 2177 if (cmd == RTM_DELETE) { 2178 if (didwork) { 2179 error = 0; 2180 } else { 2181 /* we only give an error if it wasn't in any table */ 2182 error = ((flags & RTF_HOST) ? 2183 EHOSTUNREACH : ENETUNREACH); 2184 } 2185 } else { 2186 if (a_failure) { 2187 /* return an error if any of them failed */ 2188 error = a_failure; 2189 } 2190 } 2191 return (error); 2192 } 2193 2194 /* 2195 * Set up a routing table entry, normally 2196 * for an interface. 2197 */ 2198 int 2199 rtinit(struct ifaddr *ifa, int cmd, int flags) 2200 { 2201 struct sockaddr *dst; 2202 int fib = RT_DEFAULT_FIB; 2203 2204 if (flags & RTF_HOST) { 2205 dst = ifa->ifa_dstaddr; 2206 } else { 2207 dst = ifa->ifa_addr; 2208 } 2209 2210 switch (dst->sa_family) { 2211 case AF_INET6: 2212 case AF_INET: 2213 /* We do support multiple FIBs. */ 2214 fib = RT_ALL_FIBS; 2215 break; 2216 } 2217 return (rtinit1(ifa, cmd, flags, fib)); 2218 } 2219 2220 /* 2221 * Announce interface address arrival/withdraw 2222 * Returns 0 on success. 2223 */ 2224 int 2225 rt_addrmsg(int cmd, struct ifaddr *ifa, int fibnum) 2226 { 2227 2228 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 2229 ("unexpected cmd %d", cmd)); 2230 2231 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs), 2232 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs)); 2233 2234 #if defined(INET) || defined(INET6) 2235 #ifdef SCTP 2236 /* 2237 * notify the SCTP stack 2238 * this will only get called when an address is added/deleted 2239 * XXX pass the ifaddr struct instead if ifa->ifa_addr... 2240 */ 2241 sctp_addr_change(ifa, cmd); 2242 #endif /* SCTP */ 2243 #endif 2244 return (rtsock_addrmsg(cmd, ifa, fibnum)); 2245 } 2246 2247 /* 2248 * Announce route addition/removal. 2249 * Users of this function MUST validate input data BEFORE calling. 2250 * However we have to be able to handle invalid data: 2251 * if some userland app sends us "invalid" route message (invalid mask, 2252 * no dst, wrong address families, etc...) we need to pass it back 2253 * to app (and any other rtsock consumers) with rtm_errno field set to 2254 * non-zero value. 2255 * Returns 0 on success. 2256 */ 2257 int 2258 rt_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt, 2259 int fibnum) 2260 { 2261 2262 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 2263 ("unexpected cmd %d", cmd)); 2264 2265 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs), 2266 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs)); 2267 2268 KASSERT(rt_key(rt) != NULL, (":%s: rt_key must be supplied", __func__)); 2269 2270 return (rtsock_routemsg(cmd, ifp, error, rt, fibnum)); 2271 } 2272 2273 void 2274 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt) 2275 { 2276 2277 rt_newaddrmsg_fib(cmd, ifa, error, rt, RT_ALL_FIBS); 2278 } 2279 2280 /* 2281 * This is called to generate messages from the routing socket 2282 * indicating a network interface has had addresses associated with it. 2283 */ 2284 void 2285 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt, 2286 int fibnum) 2287 { 2288 2289 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 2290 ("unexpected cmd %u", cmd)); 2291 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs), 2292 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs)); 2293 2294 if (cmd == RTM_ADD) { 2295 rt_addrmsg(cmd, ifa, fibnum); 2296 if (rt != NULL) 2297 rt_routemsg(cmd, ifa->ifa_ifp, error, rt, fibnum); 2298 } else { 2299 if (rt != NULL) 2300 rt_routemsg(cmd, ifa->ifa_ifp, error, rt, fibnum); 2301 rt_addrmsg(cmd, ifa, fibnum); 2302 } 2303 } 2304 2305