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