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 ((rt = ro->ro_rt) != NULL) { 445 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP) 446 return; 447 RTFREE(rt); 448 ro->ro_rt = NULL; 449 } 450 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum); 451 if (ro->ro_rt) 452 RT_UNLOCK(ro->ro_rt); 453 } 454 455 /* 456 * Look up the route that matches the address given 457 * Or, at least try.. Create a cloned route if needed. 458 * 459 * The returned route, if any, is locked. 460 */ 461 struct rtentry * 462 rtalloc1(struct sockaddr *dst, int report, u_long ignflags) 463 { 464 465 return (rtalloc1_fib(dst, report, ignflags, RT_DEFAULT_FIB)); 466 } 467 468 struct rtentry * 469 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags, 470 u_int fibnum) 471 { 472 RIB_RLOCK_TRACKER; 473 struct rib_head *rh; 474 struct radix_node *rn; 475 struct rtentry *newrt; 476 struct rt_addrinfo info; 477 int err = 0, msgtype = RTM_MISS; 478 479 KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum")); 480 rh = rt_tables_get_rnh(fibnum, dst->sa_family); 481 newrt = NULL; 482 if (rh == NULL) 483 goto miss; 484 485 /* 486 * Look up the address in the table for that Address Family 487 */ 488 if ((ignflags & RTF_RNH_LOCKED) == 0) 489 RIB_RLOCK(rh); 490 #ifdef INVARIANTS 491 else 492 RIB_LOCK_ASSERT(rh); 493 #endif 494 rn = rh->rnh_matchaddr(dst, &rh->head); 495 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) { 496 newrt = RNTORT(rn); 497 RT_LOCK(newrt); 498 RT_ADDREF(newrt); 499 if ((ignflags & RTF_RNH_LOCKED) == 0) 500 RIB_RUNLOCK(rh); 501 return (newrt); 502 503 } else if ((ignflags & RTF_RNH_LOCKED) == 0) 504 RIB_RUNLOCK(rh); 505 /* 506 * Either we hit the root or could not find any match, 507 * which basically means: "cannot get there from here". 508 */ 509 miss: 510 RTSTAT_INC(rts_unreach); 511 512 if (report) { 513 /* 514 * If required, report the failure to the supervising 515 * Authorities. 516 * For a delete, this is not an error. (report == 0) 517 */ 518 bzero(&info, sizeof(info)); 519 info.rti_info[RTAX_DST] = dst; 520 rt_missmsg_fib(msgtype, &info, 0, err, fibnum); 521 } 522 return (newrt); 523 } 524 525 /* 526 * Remove a reference count from an rtentry. 527 * If the count gets low enough, take it out of the routing table 528 */ 529 void 530 rtfree(struct rtentry *rt) 531 { 532 struct rib_head *rnh; 533 534 KASSERT(rt != NULL,("%s: NULL rt", __func__)); 535 rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family); 536 KASSERT(rnh != NULL,("%s: NULL rnh", __func__)); 537 538 RT_LOCK_ASSERT(rt); 539 540 /* 541 * The callers should use RTFREE_LOCKED() or RTFREE(), so 542 * we should come here exactly with the last reference. 543 */ 544 RT_REMREF(rt); 545 if (rt->rt_refcnt > 0) { 546 log(LOG_DEBUG, "%s: %p has %d refs\n", __func__, rt, rt->rt_refcnt); 547 goto done; 548 } 549 550 /* 551 * On last reference give the "close method" a chance 552 * to cleanup private state. This also permits (for 553 * IPv4 and IPv6) a chance to decide if the routing table 554 * entry should be purged immediately or at a later time. 555 * When an immediate purge is to happen the close routine 556 * typically calls rtexpunge which clears the RTF_UP flag 557 * on the entry so that the code below reclaims the storage. 558 */ 559 if (rt->rt_refcnt == 0 && rnh->rnh_close) 560 rnh->rnh_close((struct radix_node *)rt, &rnh->head); 561 562 /* 563 * If we are no longer "up" (and ref == 0) 564 * then we can free the resources associated 565 * with the route. 566 */ 567 if ((rt->rt_flags & RTF_UP) == 0) { 568 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 569 panic("rtfree 2"); 570 /* 571 * the rtentry must have been removed from the routing table 572 * so it is represented in rttrash.. remove that now. 573 */ 574 V_rttrash--; 575 #ifdef DIAGNOSTIC 576 if (rt->rt_refcnt < 0) { 577 printf("rtfree: %p not freed (neg refs)\n", rt); 578 goto done; 579 } 580 #endif 581 /* 582 * release references on items we hold them on.. 583 * e.g other routes and ifaddrs. 584 */ 585 if (rt->rt_ifa) 586 ifa_free(rt->rt_ifa); 587 /* 588 * The key is separatly alloc'd so free it (see rt_setgate()). 589 * This also frees the gateway, as they are always malloc'd 590 * together. 591 */ 592 R_Free(rt_key(rt)); 593 594 /* Unreference nexthop */ 595 nhop_free(rt->rt_nhop); 596 597 /* 598 * and the rtentry itself of course 599 */ 600 uma_zfree(V_rtzone, rt); 601 return; 602 } 603 done: 604 RT_UNLOCK(rt); 605 } 606 607 /* 608 * Temporary RTFREE() function wrapper. 609 * Intended to use in control plane code to 610 * avoid exposing internal layout of 'struct rtentry'. 611 */ 612 void 613 rtfree_func(struct rtentry *rt) 614 { 615 616 RTFREE(rt); 617 } 618 619 /* 620 * Adds a temporal redirect entry to the routing table. 621 * @fibnum: fib number 622 * @dst: destination to install redirect to 623 * @gateway: gateway to go via 624 * @author: sockaddr of originating router, can be NULL 625 * @ifp: interface to use for the redirected route 626 * @flags: set of flags to add. Allowed: RTF_GATEWAY 627 * @lifetime_sec: time in seconds to expire this redirect. 628 * 629 * Retuns 0 on success, errno otherwise. 630 */ 631 int 632 rib_add_redirect(u_int fibnum, struct sockaddr *dst, struct sockaddr *gateway, 633 struct sockaddr *author, struct ifnet *ifp, int flags, int lifetime_sec) 634 { 635 struct rtentry *rt; 636 int error; 637 struct rt_addrinfo info; 638 struct rt_metrics rti_rmx; 639 struct ifaddr *ifa; 640 641 NET_EPOCH_ASSERT(); 642 643 if (rt_tables_get_rnh(fibnum, dst->sa_family) == NULL) 644 return (EAFNOSUPPORT); 645 646 /* Verify the allowed flag mask. */ 647 KASSERT(((flags & ~(RTF_GATEWAY)) == 0), 648 ("invalid redirect flags: %x", flags)); 649 650 /* Get the best ifa for the given interface and gateway. */ 651 if ((ifa = ifaof_ifpforaddr(gateway, ifp)) == NULL) 652 return (ENETUNREACH); 653 ifa_ref(ifa); 654 655 bzero(&info, sizeof(info)); 656 info.rti_info[RTAX_DST] = dst; 657 info.rti_info[RTAX_GATEWAY] = gateway; 658 info.rti_ifa = ifa; 659 info.rti_ifp = ifp; 660 info.rti_flags = flags | RTF_HOST | RTF_DYNAMIC; 661 662 /* Setup route metrics to define expire time. */ 663 bzero(&rti_rmx, sizeof(rti_rmx)); 664 /* Set expire time as absolute. */ 665 rti_rmx.rmx_expire = lifetime_sec + time_second; 666 info.rti_mflags |= RTV_EXPIRE; 667 info.rti_rmx = &rti_rmx; 668 669 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum); 670 ifa_free(ifa); 671 672 if (error != 0) { 673 /* TODO: add per-fib redirect stats. */ 674 return (error); 675 } 676 677 RT_LOCK(rt); 678 flags = rt->rt_flags; 679 RTFREE_LOCKED(rt); 680 681 RTSTAT_INC(rts_dynamic); 682 683 /* Send notification of a route addition to userland. */ 684 bzero(&info, sizeof(info)); 685 info.rti_info[RTAX_DST] = dst; 686 info.rti_info[RTAX_GATEWAY] = gateway; 687 info.rti_info[RTAX_AUTHOR] = author; 688 rt_missmsg_fib(RTM_REDIRECT, &info, flags, error, fibnum); 689 690 return (0); 691 } 692 693 /* 694 * Routing table ioctl interface. 695 */ 696 int 697 rtioctl_fib(u_long req, caddr_t data, u_int fibnum) 698 { 699 700 /* 701 * If more ioctl commands are added here, make sure the proper 702 * super-user checks are being performed because it is possible for 703 * prison-root to make it this far if raw sockets have been enabled 704 * in jails. 705 */ 706 #ifdef INET 707 /* Multicast goop, grrr... */ 708 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP; 709 #else /* INET */ 710 return ENXIO; 711 #endif /* INET */ 712 } 713 714 struct ifaddr * 715 ifa_ifwithroute(int flags, const struct sockaddr *dst, struct sockaddr *gateway, 716 u_int fibnum) 717 { 718 struct ifaddr *ifa; 719 int not_found = 0; 720 721 NET_EPOCH_ASSERT(); 722 if ((flags & RTF_GATEWAY) == 0) { 723 /* 724 * If we are adding a route to an interface, 725 * and the interface is a pt to pt link 726 * we should search for the destination 727 * as our clue to the interface. Otherwise 728 * we can use the local address. 729 */ 730 ifa = NULL; 731 if (flags & RTF_HOST) 732 ifa = ifa_ifwithdstaddr(dst, fibnum); 733 if (ifa == NULL) 734 ifa = ifa_ifwithaddr(gateway); 735 } else { 736 /* 737 * If we are adding a route to a remote net 738 * or host, the gateway may still be on the 739 * other end of a pt to pt link. 740 */ 741 ifa = ifa_ifwithdstaddr(gateway, fibnum); 742 } 743 if (ifa == NULL) 744 ifa = ifa_ifwithnet(gateway, 0, fibnum); 745 if (ifa == NULL) { 746 struct rtentry *rt; 747 748 rt = rtalloc1_fib(gateway, 0, flags, fibnum); 749 if (rt == NULL) 750 goto out; 751 /* 752 * dismiss a gateway that is reachable only 753 * through the default router 754 */ 755 switch (gateway->sa_family) { 756 case AF_INET: 757 if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY) 758 not_found = 1; 759 break; 760 case AF_INET6: 761 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr)) 762 not_found = 1; 763 break; 764 default: 765 break; 766 } 767 if (!not_found && rt->rt_ifa != NULL) { 768 ifa = rt->rt_ifa; 769 } 770 RT_REMREF(rt); 771 RT_UNLOCK(rt); 772 if (not_found || ifa == NULL) 773 goto out; 774 } 775 if (ifa->ifa_addr->sa_family != dst->sa_family) { 776 struct ifaddr *oifa = ifa; 777 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 778 if (ifa == NULL) 779 ifa = oifa; 780 } 781 out: 782 return (ifa); 783 } 784 785 /* 786 * Do appropriate manipulations of a routing tree given 787 * all the bits of info needed 788 */ 789 int 790 rtrequest_fib(int req, 791 struct sockaddr *dst, 792 struct sockaddr *gateway, 793 struct sockaddr *netmask, 794 int flags, 795 struct rtentry **ret_nrt, 796 u_int fibnum) 797 { 798 struct rt_addrinfo info; 799 800 if (dst->sa_len == 0) 801 return(EINVAL); 802 803 bzero((caddr_t)&info, sizeof(info)); 804 info.rti_flags = flags; 805 info.rti_info[RTAX_DST] = dst; 806 info.rti_info[RTAX_GATEWAY] = gateway; 807 info.rti_info[RTAX_NETMASK] = netmask; 808 return rtrequest1_fib(req, &info, ret_nrt, fibnum); 809 } 810 811 812 /* 813 * Copy most of @rt data into @info. 814 * 815 * If @flags contains NHR_COPY, copies dst,netmask and gw to the 816 * pointers specified by @info structure. Assume such pointers 817 * are zeroed sockaddr-like structures with sa_len field initialized 818 * to reflect size of the provided buffer. if no NHR_COPY is specified, 819 * point dst,netmask and gw @info fields to appropriate @rt values. 820 * 821 * if @flags contains NHR_REF, do refcouting on rt_ifp and rt_ifa. 822 * 823 * Returns 0 on success. 824 */ 825 int 826 rt_exportinfo(struct rtentry *rt, struct rt_addrinfo *info, int flags) 827 { 828 struct rt_metrics *rmx; 829 struct sockaddr *src, *dst; 830 struct nhop_object *nh; 831 int sa_len; 832 833 if (flags & NHR_COPY) { 834 /* Copy destination if dst is non-zero */ 835 src = rt_key(rt); 836 dst = info->rti_info[RTAX_DST]; 837 sa_len = src->sa_len; 838 if (dst != NULL) { 839 if (src->sa_len > dst->sa_len) 840 return (ENOMEM); 841 memcpy(dst, src, src->sa_len); 842 info->rti_addrs |= RTA_DST; 843 } 844 845 /* Copy mask if set && dst is non-zero */ 846 src = rt_mask(rt); 847 dst = info->rti_info[RTAX_NETMASK]; 848 if (src != NULL && dst != NULL) { 849 850 /* 851 * Radix stores different value in sa_len, 852 * assume rt_mask() to have the same length 853 * as rt_key() 854 */ 855 if (sa_len > dst->sa_len) 856 return (ENOMEM); 857 memcpy(dst, src, src->sa_len); 858 info->rti_addrs |= RTA_NETMASK; 859 } 860 861 /* Copy gateway is set && dst is non-zero */ 862 src = &rt->rt_nhop->gw_sa; 863 dst = info->rti_info[RTAX_GATEWAY]; 864 if ((rt->rt_flags & RTF_GATEWAY) && src != NULL && dst != NULL){ 865 if (src->sa_len > dst->sa_len) 866 return (ENOMEM); 867 memcpy(dst, src, src->sa_len); 868 info->rti_addrs |= RTA_GATEWAY; 869 } 870 } else { 871 info->rti_info[RTAX_DST] = rt_key(rt); 872 info->rti_addrs |= RTA_DST; 873 if (rt_mask(rt) != NULL) { 874 info->rti_info[RTAX_NETMASK] = rt_mask(rt); 875 info->rti_addrs |= RTA_NETMASK; 876 } 877 if (rt->rt_flags & RTF_GATEWAY) { 878 info->rti_info[RTAX_GATEWAY] = &rt->rt_nhop->gw_sa; 879 info->rti_addrs |= RTA_GATEWAY; 880 } 881 } 882 883 nh = rt->rt_nhop; 884 rmx = info->rti_rmx; 885 if (rmx != NULL) { 886 info->rti_mflags |= RTV_MTU; 887 rmx->rmx_mtu = nh->nh_mtu; 888 } 889 890 info->rti_flags = rt->rt_flags | nhop_get_rtflags(nh); 891 info->rti_ifp = nh->nh_ifp; 892 info->rti_ifa = nh->nh_ifa; 893 if (flags & NHR_REF) { 894 if_ref(info->rti_ifp); 895 ifa_ref(info->rti_ifa); 896 } 897 898 return (0); 899 } 900 901 /* 902 * Lookups up route entry for @dst in RIB database for fib @fibnum. 903 * Exports entry data to @info using rt_exportinfo(). 904 * 905 * If @flags contains NHR_REF, refcouting is performed on rt_ifp and rt_ifa. 906 * All references can be released later by calling rib_free_info(). 907 * 908 * Returns 0 on success. 909 * Returns ENOENT for lookup failure, ENOMEM for export failure. 910 */ 911 int 912 rib_lookup_info(uint32_t fibnum, const struct sockaddr *dst, uint32_t flags, 913 uint32_t flowid, struct rt_addrinfo *info) 914 { 915 RIB_RLOCK_TRACKER; 916 struct rib_head *rh; 917 struct radix_node *rn; 918 struct rtentry *rt; 919 int error; 920 921 KASSERT((fibnum < rt_numfibs), ("rib_lookup_rte: bad fibnum")); 922 rh = rt_tables_get_rnh(fibnum, dst->sa_family); 923 if (rh == NULL) 924 return (ENOENT); 925 926 RIB_RLOCK(rh); 927 rn = rh->rnh_matchaddr(__DECONST(void *, dst), &rh->head); 928 if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) { 929 rt = RNTORT(rn); 930 /* Ensure route & ifp is UP */ 931 if (RT_LINK_IS_UP(rt->rt_ifp)) { 932 flags = (flags & NHR_REF) | NHR_COPY; 933 error = rt_exportinfo(rt, info, flags); 934 RIB_RUNLOCK(rh); 935 936 return (error); 937 } 938 } 939 RIB_RUNLOCK(rh); 940 941 return (ENOENT); 942 } 943 944 /* 945 * Releases all references acquired by rib_lookup_info() when 946 * called with NHR_REF flags. 947 */ 948 void 949 rib_free_info(struct rt_addrinfo *info) 950 { 951 952 ifa_free(info->rti_ifa); 953 if_rele(info->rti_ifp); 954 } 955 956 /* 957 * Iterates over all existing fibs in system calling 958 * @setwa_f function prior to traversing each fib. 959 * Calls @wa_f function for each element in current fib. 960 * If af is not AF_UNSPEC, iterates over fibs in particular 961 * address family. 962 */ 963 void 964 rt_foreach_fib_walk(int af, rt_setwarg_t *setwa_f, rt_walktree_f_t *wa_f, 965 void *arg) 966 { 967 struct rib_head *rnh; 968 uint32_t fibnum; 969 int i; 970 971 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 972 /* Do we want some specific family? */ 973 if (af != AF_UNSPEC) { 974 rnh = rt_tables_get_rnh(fibnum, af); 975 if (rnh == NULL) 976 continue; 977 if (setwa_f != NULL) 978 setwa_f(rnh, fibnum, af, arg); 979 980 RIB_WLOCK(rnh); 981 rnh->rnh_walktree(&rnh->head, (walktree_f_t *)wa_f,arg); 982 RIB_WUNLOCK(rnh); 983 continue; 984 } 985 986 for (i = 1; i <= AF_MAX; i++) { 987 rnh = rt_tables_get_rnh(fibnum, i); 988 if (rnh == NULL) 989 continue; 990 if (setwa_f != NULL) 991 setwa_f(rnh, fibnum, i, arg); 992 993 RIB_WLOCK(rnh); 994 rnh->rnh_walktree(&rnh->head, (walktree_f_t *)wa_f,arg); 995 RIB_WUNLOCK(rnh); 996 } 997 } 998 } 999 1000 struct rt_delinfo 1001 { 1002 struct rt_addrinfo info; 1003 struct rib_head *rnh; 1004 struct rtentry *head; 1005 }; 1006 1007 /* 1008 * Conditionally unlinks @rn from radix tree based 1009 * on info data passed in @arg. 1010 */ 1011 static int 1012 rt_checkdelroute(struct radix_node *rn, void *arg) 1013 { 1014 struct rt_delinfo *di; 1015 struct rt_addrinfo *info; 1016 struct rtentry *rt; 1017 int error; 1018 1019 di = (struct rt_delinfo *)arg; 1020 rt = (struct rtentry *)rn; 1021 info = &di->info; 1022 error = 0; 1023 1024 info->rti_info[RTAX_DST] = rt_key(rt); 1025 info->rti_info[RTAX_NETMASK] = rt_mask(rt); 1026 info->rti_info[RTAX_GATEWAY] = &rt->rt_nhop->gw_sa; 1027 1028 rt = rt_unlinkrte(di->rnh, info, &error); 1029 if (rt == NULL) { 1030 /* Either not allowed or not matched. Skip entry */ 1031 return (0); 1032 } 1033 1034 /* Entry was unlinked. Add to the list and return */ 1035 rt->rt_chain = di->head; 1036 di->head = rt; 1037 1038 return (0); 1039 } 1040 1041 /* 1042 * Iterates over a routing table specified by @fibnum and @family and 1043 * deletes elements marked by @filter_f. 1044 * @fibnum: rtable id 1045 * @family: AF_ address family 1046 * @filter_f: function returning non-zero value for items to delete 1047 * @arg: data to pass to the @filter_f function 1048 * @report: true if rtsock notification is needed. 1049 */ 1050 void 1051 rib_walk_del(u_int fibnum, int family, rt_filter_f_t *filter_f, void *arg, bool report) 1052 { 1053 struct rib_head *rnh; 1054 struct rt_delinfo di; 1055 struct rtentry *rt; 1056 1057 rnh = rt_tables_get_rnh(fibnum, family); 1058 if (rnh == NULL) 1059 return; 1060 1061 bzero(&di, sizeof(di)); 1062 di.info.rti_filter = filter_f; 1063 di.info.rti_filterdata = arg; 1064 di.rnh = rnh; 1065 1066 RIB_WLOCK(rnh); 1067 rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di); 1068 RIB_WUNLOCK(rnh); 1069 1070 if (di.head == NULL) 1071 return; 1072 1073 /* We might have something to reclaim. */ 1074 while (di.head != NULL) { 1075 rt = di.head; 1076 di.head = rt->rt_chain; 1077 rt->rt_chain = NULL; 1078 1079 /* TODO std rt -> rt_addrinfo export */ 1080 di.info.rti_info[RTAX_DST] = rt_key(rt); 1081 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt); 1082 1083 rt_notifydelete(rt, &di.info); 1084 1085 if (report) 1086 rt_routemsg(RTM_DELETE, rt, rt->rt_ifp, 0, fibnum); 1087 RTFREE_LOCKED(rt); 1088 } 1089 } 1090 1091 /* 1092 * Iterates over all existing fibs in system and deletes each element 1093 * for which @filter_f function returns non-zero value. 1094 * If @family is not AF_UNSPEC, iterates over fibs in particular 1095 * address family. 1096 */ 1097 void 1098 rt_foreach_fib_walk_del(int family, rt_filter_f_t *filter_f, void *arg) 1099 { 1100 u_int fibnum; 1101 int i, start, end; 1102 1103 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 1104 /* Do we want some specific family? */ 1105 if (family != AF_UNSPEC) { 1106 start = family; 1107 end = family; 1108 } else { 1109 start = 1; 1110 end = AF_MAX; 1111 } 1112 1113 for (i = start; i <= end; i++) { 1114 if (rt_tables_get_rnh(fibnum, i) == NULL) 1115 continue; 1116 1117 rib_walk_del(fibnum, i, filter_f, arg, 0); 1118 } 1119 } 1120 } 1121 1122 /* 1123 * Delete Routes for a Network Interface 1124 * 1125 * Called for each routing entry via the rnh->rnh_walktree() call above 1126 * to delete all route entries referencing a detaching network interface. 1127 * 1128 * Arguments: 1129 * rt pointer to rtentry 1130 * nh pointer to nhop 1131 * arg argument passed to rnh->rnh_walktree() - detaching interface 1132 * 1133 * Returns: 1134 * 0 successful 1135 * errno failed - reason indicated 1136 */ 1137 static int 1138 rt_ifdelroute(const struct rtentry *rt, const struct nhop_object *nh, void *arg) 1139 { 1140 struct ifnet *ifp = arg; 1141 1142 if (nh->nh_ifp != ifp) 1143 return (0); 1144 1145 /* 1146 * Protect (sorta) against walktree recursion problems 1147 * with cloned routes 1148 */ 1149 if ((rt->rt_flags & RTF_UP) == 0) 1150 return (0); 1151 1152 return (1); 1153 } 1154 1155 /* 1156 * Delete all remaining routes using this interface 1157 * Unfortuneatly the only way to do this is to slog through 1158 * the entire routing table looking for routes which point 1159 * to this interface...oh well... 1160 */ 1161 void 1162 rt_flushifroutes_af(struct ifnet *ifp, int af) 1163 { 1164 KASSERT((af >= 1 && af <= AF_MAX), ("%s: af %d not >= 1 and <= %d", 1165 __func__, af, AF_MAX)); 1166 1167 rt_foreach_fib_walk_del(af, rt_ifdelroute, ifp); 1168 } 1169 1170 void 1171 rt_flushifroutes(struct ifnet *ifp) 1172 { 1173 1174 rt_foreach_fib_walk_del(AF_UNSPEC, rt_ifdelroute, ifp); 1175 } 1176 1177 /* 1178 * Conditionally unlinks rtentry matching data inside @info from @rnh. 1179 * Returns unlinked, locked and referenced @rtentry on success, 1180 * Returns NULL and sets @perror to: 1181 * ESRCH - if prefix was not found, 1182 * EADDRINUSE - if trying to delete PINNED route without appropriate flag. 1183 * ENOENT - if supplied filter function returned 0 (not matched). 1184 */ 1185 static struct rtentry * 1186 rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info, int *perror) 1187 { 1188 struct sockaddr *dst, *netmask; 1189 struct rtentry *rt; 1190 struct radix_node *rn; 1191 1192 dst = info->rti_info[RTAX_DST]; 1193 netmask = info->rti_info[RTAX_NETMASK]; 1194 1195 rt = (struct rtentry *)rnh->rnh_lookup(dst, netmask, &rnh->head); 1196 if (rt == NULL) { 1197 *perror = ESRCH; 1198 return (NULL); 1199 } 1200 1201 if ((info->rti_flags & RTF_PINNED) == 0) { 1202 /* Check if target route can be deleted */ 1203 if (rt->rt_flags & RTF_PINNED) { 1204 *perror = EADDRINUSE; 1205 return (NULL); 1206 } 1207 } 1208 1209 if (info->rti_filter != NULL) { 1210 if (info->rti_filter(rt, rt->rt_nhop, info->rti_filterdata)==0){ 1211 /* Not matched */ 1212 *perror = ENOENT; 1213 return (NULL); 1214 } 1215 1216 /* 1217 * Filter function requested rte deletion. 1218 * Ease the caller work by filling in remaining info 1219 * from that particular entry. 1220 */ 1221 info->rti_info[RTAX_GATEWAY] = &rt->rt_nhop->gw_sa; 1222 } 1223 1224 /* 1225 * Remove the item from the tree and return it. 1226 * Complain if it is not there and do no more processing. 1227 */ 1228 *perror = ESRCH; 1229 #ifdef RADIX_MPATH 1230 if (rt_mpath_capable(rnh)) 1231 rn = rt_mpath_unlink(rnh, info, rt, perror); 1232 else 1233 #endif 1234 rn = rnh->rnh_deladdr(dst, netmask, &rnh->head); 1235 if (rn == NULL) 1236 return (NULL); 1237 1238 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 1239 panic ("rtrequest delete"); 1240 1241 rt = RNTORT(rn); 1242 RT_LOCK(rt); 1243 RT_ADDREF(rt); 1244 rt->rt_flags &= ~RTF_UP; 1245 1246 *perror = 0; 1247 1248 return (rt); 1249 } 1250 1251 static void 1252 rt_notifydelete(struct rtentry *rt, struct rt_addrinfo *info) 1253 { 1254 struct ifaddr *ifa; 1255 1256 /* 1257 * give the protocol a chance to keep things in sync. 1258 */ 1259 ifa = rt->rt_ifa; 1260 if (ifa != NULL && ifa->ifa_rtrequest != NULL) 1261 ifa->ifa_rtrequest(RTM_DELETE, rt, info); 1262 1263 /* 1264 * One more rtentry floating around that is not 1265 * linked to the routing table. rttrash will be decremented 1266 * when RTFREE(rt) is eventually called. 1267 */ 1268 V_rttrash++; 1269 } 1270 1271 1272 /* 1273 * These (questionable) definitions of apparent local variables apply 1274 * to the next two functions. XXXXXX!!! 1275 */ 1276 #define dst info->rti_info[RTAX_DST] 1277 #define gateway info->rti_info[RTAX_GATEWAY] 1278 #define netmask info->rti_info[RTAX_NETMASK] 1279 #define ifaaddr info->rti_info[RTAX_IFA] 1280 #define ifpaddr info->rti_info[RTAX_IFP] 1281 #define flags info->rti_flags 1282 1283 /* 1284 * Look up rt_addrinfo for a specific fib. Note that if rti_ifa is defined, 1285 * it will be referenced so the caller must free it. 1286 * 1287 * Assume basic consistency checks are executed by callers: 1288 * RTAX_DST exists, if RTF_GATEWAY is set, RTAX_GATEWAY exists as well. 1289 */ 1290 int 1291 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum) 1292 { 1293 struct epoch_tracker et; 1294 int needref, error; 1295 1296 /* 1297 * ifp may be specified by sockaddr_dl 1298 * when protocol address is ambiguous. 1299 */ 1300 error = 0; 1301 needref = (info->rti_ifa == NULL); 1302 NET_EPOCH_ENTER(et); 1303 1304 /* If we have interface specified by the ifindex in the address, use it */ 1305 if (info->rti_ifp == NULL && ifpaddr != NULL && 1306 ifpaddr->sa_family == AF_LINK) { 1307 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)ifpaddr; 1308 if (sdl->sdl_index != 0) 1309 info->rti_ifp = ifnet_byindex(sdl->sdl_index); 1310 } 1311 /* 1312 * If we have source address specified, try to find it 1313 * TODO: avoid enumerating all ifas on all interfaces. 1314 */ 1315 if (info->rti_ifa == NULL && ifaaddr != NULL) 1316 info->rti_ifa = ifa_ifwithaddr(ifaaddr); 1317 if (info->rti_ifa == NULL) { 1318 struct sockaddr *sa; 1319 1320 /* 1321 * Most common use case for the userland-supplied routes. 1322 * 1323 * Choose sockaddr to select ifa. 1324 * -- if ifp is set -- 1325 * Order of preference: 1326 * 1) IFA address 1327 * 2) gateway address 1328 * Note: for interface routes link-level gateway address 1329 * is specified to indicate the interface index without 1330 * specifying RTF_GATEWAY. In this case, ignore gateway 1331 * Note: gateway AF may be different from dst AF. In this case, 1332 * ignore gateway 1333 * 3) final destination. 1334 * 4) if all of these fails, try to get at least link-level ifa. 1335 * -- else -- 1336 * try to lookup gateway or dst in the routing table to get ifa 1337 */ 1338 if (info->rti_info[RTAX_IFA] != NULL) 1339 sa = info->rti_info[RTAX_IFA]; 1340 else if ((info->rti_flags & RTF_GATEWAY) != 0 && 1341 gateway->sa_family == dst->sa_family) 1342 sa = gateway; 1343 else 1344 sa = dst; 1345 if (info->rti_ifp != NULL) { 1346 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp); 1347 /* Case 4 */ 1348 if (info->rti_ifa == NULL && gateway != NULL) 1349 info->rti_ifa = ifaof_ifpforaddr(gateway, info->rti_ifp); 1350 } else if (dst != NULL && gateway != NULL) 1351 info->rti_ifa = ifa_ifwithroute(flags, dst, gateway, 1352 fibnum); 1353 else if (sa != NULL) 1354 info->rti_ifa = ifa_ifwithroute(flags, sa, sa, 1355 fibnum); 1356 } 1357 if (needref && info->rti_ifa != NULL) { 1358 if (info->rti_ifp == NULL) 1359 info->rti_ifp = info->rti_ifa->ifa_ifp; 1360 ifa_ref(info->rti_ifa); 1361 } else 1362 error = ENETUNREACH; 1363 NET_EPOCH_EXIT(et); 1364 return (error); 1365 } 1366 1367 static int 1368 if_updatemtu_cb(struct radix_node *rn, void *arg) 1369 { 1370 struct rtentry *rt; 1371 struct if_mtuinfo *ifmtu; 1372 1373 rt = (struct rtentry *)rn; 1374 ifmtu = (struct if_mtuinfo *)arg; 1375 1376 if (rt->rt_ifp != ifmtu->ifp) 1377 return (0); 1378 1379 if (rt->rt_mtu >= ifmtu->mtu) { 1380 /* We have to decrease mtu regardless of flags */ 1381 rt->rt_mtu = ifmtu->mtu; 1382 return (0); 1383 } 1384 1385 /* 1386 * New MTU is bigger. Check if are allowed to alter it 1387 */ 1388 if ((rt->rt_flags & (RTF_FIXEDMTU | RTF_GATEWAY | RTF_HOST)) != 0) { 1389 1390 /* 1391 * Skip routes with user-supplied MTU and 1392 * non-interface routes 1393 */ 1394 return (0); 1395 } 1396 1397 /* We are safe to update route MTU */ 1398 rt->rt_mtu = ifmtu->mtu; 1399 1400 return (0); 1401 } 1402 1403 void 1404 rt_updatemtu(struct ifnet *ifp) 1405 { 1406 struct if_mtuinfo ifmtu; 1407 struct rib_head *rnh; 1408 int i, j; 1409 1410 ifmtu.ifp = ifp; 1411 1412 /* 1413 * Try to update rt_mtu for all routes using this interface 1414 * Unfortunately the only way to do this is to traverse all 1415 * routing tables in all fibs/domains. 1416 */ 1417 for (i = 1; i <= AF_MAX; i++) { 1418 ifmtu.mtu = if_getmtu_family(ifp, i); 1419 for (j = 0; j < rt_numfibs; j++) { 1420 rnh = rt_tables_get_rnh(j, i); 1421 if (rnh == NULL) 1422 continue; 1423 RIB_WLOCK(rnh); 1424 rnh->rnh_walktree(&rnh->head, if_updatemtu_cb, &ifmtu); 1425 RIB_WUNLOCK(rnh); 1426 nhops_update_ifmtu(rnh, ifp, ifmtu.mtu); 1427 } 1428 } 1429 } 1430 1431 1432 #if 0 1433 int p_sockaddr(char *buf, int buflen, struct sockaddr *s); 1434 int rt_print(char *buf, int buflen, struct rtentry *rt); 1435 1436 int 1437 p_sockaddr(char *buf, int buflen, struct sockaddr *s) 1438 { 1439 void *paddr = NULL; 1440 1441 switch (s->sa_family) { 1442 case AF_INET: 1443 paddr = &((struct sockaddr_in *)s)->sin_addr; 1444 break; 1445 case AF_INET6: 1446 paddr = &((struct sockaddr_in6 *)s)->sin6_addr; 1447 break; 1448 } 1449 1450 if (paddr == NULL) 1451 return (0); 1452 1453 if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL) 1454 return (0); 1455 1456 return (strlen(buf)); 1457 } 1458 1459 int 1460 rt_print(char *buf, int buflen, struct rtentry *rt) 1461 { 1462 struct sockaddr *addr, *mask; 1463 int i = 0; 1464 1465 addr = rt_key(rt); 1466 mask = rt_mask(rt); 1467 1468 i = p_sockaddr(buf, buflen, addr); 1469 if (!(rt->rt_flags & RTF_HOST)) { 1470 buf[i++] = '/'; 1471 i += p_sockaddr(buf + i, buflen - i, mask); 1472 } 1473 1474 if (rt->rt_flags & RTF_GATEWAY) { 1475 buf[i++] = '>'; 1476 i += p_sockaddr(buf + i, buflen - i, &rt->rt_nhop->gw_sa); 1477 } 1478 1479 return (i); 1480 } 1481 #endif 1482 1483 #ifdef RADIX_MPATH 1484 /* 1485 * Deletes key for single-path routes, unlinks rtentry with 1486 * gateway specified in @info from multi-path routes. 1487 * 1488 * Returnes unlinked entry. In case of failure, returns NULL 1489 * and sets @perror to ESRCH. 1490 */ 1491 static struct radix_node * 1492 rt_mpath_unlink(struct rib_head *rnh, struct rt_addrinfo *info, 1493 struct rtentry *rto, int *perror) 1494 { 1495 /* 1496 * if we got multipath routes, we require users to specify 1497 * a matching RTAX_GATEWAY. 1498 */ 1499 struct rtentry *rt; // *rto = NULL; 1500 struct radix_node *rn; 1501 struct sockaddr *gw; 1502 1503 gw = info->rti_info[RTAX_GATEWAY]; 1504 rt = rt_mpath_matchgate(rto, gw); 1505 if (rt == NULL) { 1506 *perror = ESRCH; 1507 return (NULL); 1508 } 1509 1510 /* 1511 * this is the first entry in the chain 1512 */ 1513 if (rto == rt) { 1514 rn = rn_mpath_next((struct radix_node *)rt); 1515 /* 1516 * there is another entry, now it's active 1517 */ 1518 if (rn) { 1519 rto = RNTORT(rn); 1520 RT_LOCK(rto); 1521 rto->rt_flags |= RTF_UP; 1522 RT_UNLOCK(rto); 1523 } else if (rt->rt_flags & RTF_GATEWAY) { 1524 /* 1525 * For gateway routes, we need to 1526 * make sure that we we are deleting 1527 * the correct gateway. 1528 * rt_mpath_matchgate() does not 1529 * check the case when there is only 1530 * one route in the chain. 1531 */ 1532 if (gw && 1533 (rt->rt_nhop->gw_sa.sa_len != gw->sa_len || 1534 memcmp(&rt->rt_nhop->gw_sa, gw, gw->sa_len))) { 1535 *perror = ESRCH; 1536 return (NULL); 1537 } 1538 } 1539 1540 /* 1541 * use the normal delete code to remove 1542 * the first entry 1543 */ 1544 rn = rnh->rnh_deladdr(dst, netmask, &rnh->head); 1545 *perror = 0; 1546 return (rn); 1547 } 1548 1549 /* 1550 * if the entry is 2nd and on up 1551 */ 1552 if (rt_mpath_deldup(rto, rt) == 0) 1553 panic ("rtrequest1: rt_mpath_deldup"); 1554 *perror = 0; 1555 rn = (struct radix_node *)rt; 1556 return (rn); 1557 } 1558 #endif 1559 1560 #undef dst 1561 #undef gateway 1562 #undef netmask 1563 #undef ifaaddr 1564 #undef ifpaddr 1565 #undef flags 1566 1567 int 1568 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt, 1569 u_int fibnum) 1570 { 1571 struct epoch_tracker et; 1572 const struct sockaddr *dst; 1573 struct rib_head *rnh; 1574 int error; 1575 1576 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum")); 1577 KASSERT((info->rti_flags & RTF_RNH_LOCKED) == 0, ("rtrequest1_fib: locked")); 1578 1579 dst = info->rti_info[RTAX_DST]; 1580 1581 switch (dst->sa_family) { 1582 case AF_INET6: 1583 case AF_INET: 1584 /* We support multiple FIBs. */ 1585 break; 1586 default: 1587 fibnum = RT_DEFAULT_FIB; 1588 break; 1589 } 1590 1591 /* 1592 * Find the correct routing tree to use for this Address Family 1593 */ 1594 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 1595 if (rnh == NULL) 1596 return (EAFNOSUPPORT); 1597 1598 /* 1599 * If we are adding a host route then we don't want to put 1600 * a netmask in the tree, nor do we want to clone it. 1601 */ 1602 if (info->rti_flags & RTF_HOST) 1603 info->rti_info[RTAX_NETMASK] = NULL; 1604 1605 error = 0; 1606 switch (req) { 1607 case RTM_DELETE: 1608 error = del_route(rnh, info, ret_nrt); 1609 break; 1610 case RTM_RESOLVE: 1611 /* 1612 * resolve was only used for route cloning 1613 * here for compat 1614 */ 1615 break; 1616 case RTM_ADD: 1617 error = add_route(rnh, info, ret_nrt); 1618 break; 1619 case RTM_CHANGE: 1620 NET_EPOCH_ENTER(et); 1621 RIB_WLOCK(rnh); 1622 error = change_route(rnh, info, ret_nrt); 1623 RIB_WUNLOCK(rnh); 1624 NET_EPOCH_EXIT(et); 1625 break; 1626 default: 1627 error = EOPNOTSUPP; 1628 } 1629 1630 return (error); 1631 } 1632 1633 static int 1634 add_route(struct rib_head *rnh, struct rt_addrinfo *info, 1635 struct rtentry **ret_nrt) 1636 { 1637 struct sockaddr *dst, *ndst, *gateway, *netmask; 1638 struct rtentry *rt, *rt_old; 1639 struct nhop_object *nh; 1640 struct radix_node *rn; 1641 struct ifaddr *ifa; 1642 int error, flags; 1643 struct epoch_tracker et; 1644 1645 dst = info->rti_info[RTAX_DST]; 1646 gateway = info->rti_info[RTAX_GATEWAY]; 1647 netmask = info->rti_info[RTAX_NETMASK]; 1648 flags = info->rti_flags; 1649 1650 if ((flags & RTF_GATEWAY) && !gateway) 1651 return (EINVAL); 1652 if (dst && gateway && (dst->sa_family != gateway->sa_family) && 1653 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK)) 1654 return (EINVAL); 1655 1656 if (info->rti_ifa == NULL) { 1657 error = rt_getifa_fib(info, rnh->rib_fibnum); 1658 if (error) 1659 return (error); 1660 } else { 1661 ifa_ref(info->rti_ifa); 1662 } 1663 1664 NET_EPOCH_ENTER(et); 1665 error = nhop_create_from_info(rnh, info, &nh); 1666 NET_EPOCH_EXIT(et); 1667 if (error != 0) { 1668 ifa_free(info->rti_ifa); 1669 return (error); 1670 } 1671 1672 rt = uma_zalloc(V_rtzone, M_NOWAIT); 1673 if (rt == NULL) { 1674 ifa_free(info->rti_ifa); 1675 nhop_free(nh); 1676 return (ENOBUFS); 1677 } 1678 rt->rt_flags = RTF_UP | flags; 1679 rt->rt_fibnum = rnh->rib_fibnum; 1680 rt->rt_nhop = nh; 1681 /* 1682 * Add the gateway. Possibly re-malloc-ing the storage for it. 1683 */ 1684 if ((error = rt_setgate(rt, dst, gateway)) != 0) { 1685 ifa_free(info->rti_ifa); 1686 nhop_free(nh); 1687 uma_zfree(V_rtzone, rt); 1688 return (error); 1689 } 1690 1691 /* 1692 * point to the (possibly newly malloc'd) dest address. 1693 */ 1694 ndst = (struct sockaddr *)rt_key(rt); 1695 1696 /* 1697 * make sure it contains the value we want (masked if needed). 1698 */ 1699 if (netmask) { 1700 rt_maskedcopy(dst, ndst, netmask); 1701 } else 1702 bcopy(dst, ndst, dst->sa_len); 1703 1704 /* 1705 * We use the ifa reference returned by rt_getifa_fib(). 1706 * This moved from below so that rnh->rnh_addaddr() can 1707 * examine the ifa and ifa->ifa_ifp if it so desires. 1708 */ 1709 ifa = info->rti_ifa; 1710 rt->rt_ifa = ifa; 1711 rt->rt_ifp = ifa->ifa_ifp; 1712 rt->rt_weight = 1; 1713 1714 rt_setmetrics(info, rt); 1715 1716 RIB_WLOCK(rnh); 1717 RT_LOCK(rt); 1718 #ifdef RADIX_MPATH 1719 /* do not permit exactly the same dst/mask/gw pair */ 1720 if (rt_mpath_capable(rnh) && 1721 rt_mpath_conflict(rnh, rt, netmask)) { 1722 RIB_WUNLOCK(rnh); 1723 1724 ifa_free(rt->rt_ifa); 1725 R_Free(rt_key(rt)); 1726 nhop_free(nh); 1727 uma_zfree(V_rtzone, rt); 1728 return (EEXIST); 1729 } 1730 #endif 1731 1732 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */ 1733 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, rt->rt_nodes); 1734 1735 if (rn != NULL && rt->rt_expire > 0) 1736 tmproutes_update(rnh, rt); 1737 1738 rt_old = NULL; 1739 if (rn == NULL && (info->rti_flags & RTF_PINNED) != 0) { 1740 1741 /* 1742 * Force removal and re-try addition 1743 * TODO: better multipath&pinned support 1744 */ 1745 struct sockaddr *info_dst = info->rti_info[RTAX_DST]; 1746 info->rti_info[RTAX_DST] = ndst; 1747 /* Do not delete existing PINNED(interface) routes */ 1748 info->rti_flags &= ~RTF_PINNED; 1749 rt_old = rt_unlinkrte(rnh, info, &error); 1750 info->rti_flags |= RTF_PINNED; 1751 info->rti_info[RTAX_DST] = info_dst; 1752 if (rt_old != NULL) 1753 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, 1754 rt->rt_nodes); 1755 } 1756 RIB_WUNLOCK(rnh); 1757 1758 if (rt_old != NULL) 1759 RT_UNLOCK(rt_old); 1760 1761 /* 1762 * If it still failed to go into the tree, 1763 * then un-make it (this should be a function) 1764 */ 1765 if (rn == NULL) { 1766 ifa_free(rt->rt_ifa); 1767 R_Free(rt_key(rt)); 1768 nhop_free(nh); 1769 uma_zfree(V_rtzone, rt); 1770 return (EEXIST); 1771 } 1772 1773 if (rt_old != NULL) { 1774 rt_notifydelete(rt_old, info); 1775 RTFREE(rt_old); 1776 } 1777 1778 /* 1779 * If this protocol has something to add to this then 1780 * allow it to do that as well. 1781 */ 1782 if (ifa->ifa_rtrequest) 1783 ifa->ifa_rtrequest(RTM_ADD, rt, info); 1784 1785 /* 1786 * actually return a resultant rtentry and 1787 * give the caller a single reference. 1788 */ 1789 if (ret_nrt) { 1790 *ret_nrt = rt; 1791 RT_ADDREF(rt); 1792 } 1793 rnh->rnh_gen++; /* Routing table updated */ 1794 RT_UNLOCK(rt); 1795 1796 return (0); 1797 } 1798 1799 static int 1800 del_route(struct rib_head *rnh, struct rt_addrinfo *info, 1801 struct rtentry **ret_nrt) 1802 { 1803 struct sockaddr *dst, *netmask; 1804 struct sockaddr_storage mdst; 1805 struct rtentry *rt; 1806 int error; 1807 1808 dst = info->rti_info[RTAX_DST]; 1809 netmask = info->rti_info[RTAX_NETMASK]; 1810 1811 if (netmask) { 1812 if (dst->sa_len > sizeof(mdst)) 1813 return (EINVAL); 1814 rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask); 1815 dst = (struct sockaddr *)&mdst; 1816 } 1817 1818 RIB_WLOCK(rnh); 1819 rt = rt_unlinkrte(rnh, info, &error); 1820 RIB_WUNLOCK(rnh); 1821 if (error != 0) 1822 return (error); 1823 1824 rt_notifydelete(rt, info); 1825 1826 /* 1827 * If the caller wants it, then it can have it, 1828 * but it's up to it to free the rtentry as we won't be 1829 * doing it. 1830 */ 1831 if (ret_nrt) { 1832 *ret_nrt = rt; 1833 RT_UNLOCK(rt); 1834 } else 1835 RTFREE_LOCKED(rt); 1836 1837 return (0); 1838 } 1839 1840 static int 1841 change_route(struct rib_head *rnh, struct rt_addrinfo *info, 1842 struct rtentry **ret_nrt) 1843 { 1844 struct rtentry *rt = NULL; 1845 int error = 0; 1846 int free_ifa = 0; 1847 int family, mtu; 1848 struct nhop_object *nh; 1849 struct if_mtuinfo ifmtu; 1850 1851 RIB_WLOCK_ASSERT(rnh); 1852 1853 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST], 1854 info->rti_info[RTAX_NETMASK], &rnh->head); 1855 1856 if (rt == NULL) 1857 return (ESRCH); 1858 1859 #ifdef RADIX_MPATH 1860 /* 1861 * If we got multipath routes, 1862 * we require users to specify a matching RTAX_GATEWAY. 1863 */ 1864 if (rt_mpath_capable(rnh)) { 1865 rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]); 1866 if (rt == NULL) 1867 return (ESRCH); 1868 } 1869 #endif 1870 1871 nh = NULL; 1872 RT_LOCK(rt); 1873 1874 rt_setmetrics(info, rt); 1875 1876 /* 1877 * New gateway could require new ifaddr, ifp; 1878 * flags may also be different; ifp may be specified 1879 * by ll sockaddr when protocol address is ambiguous 1880 */ 1881 if (((rt->rt_flags & RTF_GATEWAY) && 1882 info->rti_info[RTAX_GATEWAY] != NULL) || 1883 info->rti_info[RTAX_IFP] != NULL || 1884 (info->rti_info[RTAX_IFA] != NULL && 1885 !sa_equal(info->rti_info[RTAX_IFA], rt->rt_ifa->ifa_addr))) { 1886 /* 1887 * XXX: Temporarily set RTF_RNH_LOCKED flag in the rti_flags 1888 * to avoid rlock in the ifa_ifwithroute(). 1889 */ 1890 info->rti_flags |= RTF_RNH_LOCKED; 1891 error = rt_getifa_fib(info, rnh->rib_fibnum); 1892 info->rti_flags &= ~RTF_RNH_LOCKED; 1893 if (info->rti_ifa != NULL) 1894 free_ifa = 1; 1895 1896 if (error != 0) 1897 goto bad; 1898 } 1899 1900 error = nhop_create_from_nhop(rnh, rt->rt_nhop, info, &nh); 1901 if (error != 0) 1902 goto bad; 1903 1904 /* Check if outgoing interface has changed */ 1905 if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa && 1906 rt->rt_ifa != NULL) { 1907 if (rt->rt_ifa->ifa_rtrequest != NULL) 1908 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, info); 1909 ifa_free(rt->rt_ifa); 1910 rt->rt_ifa = NULL; 1911 } 1912 /* Update gateway address */ 1913 if (info->rti_info[RTAX_GATEWAY] != NULL) { 1914 error = rt_setgate(rt, rt_key(rt), info->rti_info[RTAX_GATEWAY]); 1915 if (error != 0) 1916 goto bad; 1917 1918 rt->rt_flags &= ~RTF_GATEWAY; 1919 rt->rt_flags |= (RTF_GATEWAY & info->rti_flags); 1920 } 1921 1922 if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa) { 1923 ifa_ref(info->rti_ifa); 1924 rt->rt_ifa = info->rti_ifa; 1925 rt->rt_ifp = info->rti_ifp; 1926 } 1927 /* Allow some flags to be toggled on change. */ 1928 rt->rt_flags &= ~RTF_FMASK; 1929 rt->rt_flags |= info->rti_flags & RTF_FMASK; 1930 1931 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest != NULL) 1932 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, info); 1933 1934 /* Alter route MTU if necessary */ 1935 if (rt->rt_ifp != NULL) { 1936 family = info->rti_info[RTAX_DST]->sa_family; 1937 mtu = if_getmtu_family(rt->rt_ifp, family); 1938 /* Set default MTU */ 1939 if (rt->rt_mtu == 0) 1940 rt->rt_mtu = mtu; 1941 if (rt->rt_mtu != mtu) { 1942 /* Check if we really need to update */ 1943 ifmtu.ifp = rt->rt_ifp; 1944 ifmtu.mtu = mtu; 1945 if_updatemtu_cb(rt->rt_nodes, &ifmtu); 1946 } 1947 } 1948 1949 /* Update nexthop */ 1950 nhop_free(rt->rt_nhop); 1951 rt->rt_nhop = nh; 1952 nh = NULL; 1953 1954 /* 1955 * This route change may have modified the route's gateway. In that 1956 * case, any inpcbs that have cached this route need to invalidate their 1957 * llentry cache. 1958 */ 1959 rnh->rnh_gen++; 1960 1961 if (ret_nrt) { 1962 *ret_nrt = rt; 1963 RT_ADDREF(rt); 1964 } 1965 bad: 1966 RT_UNLOCK(rt); 1967 if (nh != NULL) 1968 nhop_free(nh); 1969 if (free_ifa != 0) { 1970 ifa_free(info->rti_ifa); 1971 info->rti_ifa = NULL; 1972 } 1973 return (error); 1974 } 1975 1976 static void 1977 rt_setmetrics(const struct rt_addrinfo *info, struct rtentry *rt) 1978 { 1979 1980 if (info->rti_mflags & RTV_MTU) { 1981 if (info->rti_rmx->rmx_mtu != 0) { 1982 1983 /* 1984 * MTU was explicitly provided by user. 1985 * Keep it. 1986 */ 1987 rt->rt_flags |= RTF_FIXEDMTU; 1988 } else { 1989 1990 /* 1991 * User explicitly sets MTU to 0. 1992 * Assume rollback to default. 1993 */ 1994 rt->rt_flags &= ~RTF_FIXEDMTU; 1995 } 1996 rt->rt_mtu = info->rti_rmx->rmx_mtu; 1997 } 1998 if (info->rti_mflags & RTV_WEIGHT) 1999 rt->rt_weight = info->rti_rmx->rmx_weight; 2000 /* Kernel -> userland timebase conversion. */ 2001 if (info->rti_mflags & RTV_EXPIRE) 2002 rt->rt_expire = info->rti_rmx->rmx_expire ? 2003 info->rti_rmx->rmx_expire - time_second + time_uptime : 0; 2004 } 2005 2006 int 2007 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate) 2008 { 2009 /* XXX dst may be overwritten, can we move this to below */ 2010 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate); 2011 2012 /* 2013 * Prepare to store the gateway in rt->rt_gateway. 2014 * Both dst and gateway are stored one after the other in the same 2015 * malloc'd chunk. If we have room, we can reuse the old buffer, 2016 * rt_gateway already points to the right place. 2017 * Otherwise, malloc a new block and update the 'dst' address. 2018 */ 2019 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) { 2020 caddr_t new; 2021 2022 R_Malloc(new, caddr_t, dlen + glen); 2023 if (new == NULL) 2024 return ENOBUFS; 2025 /* 2026 * XXX note, we copy from *dst and not *rt_key(rt) because 2027 * rt_setgate() can be called to initialize a newly 2028 * allocated route entry, in which case rt_key(rt) == NULL 2029 * (and also rt->rt_gateway == NULL). 2030 * Free()/free() handle a NULL argument just fine. 2031 */ 2032 bcopy(dst, new, dlen); 2033 R_Free(rt_key(rt)); /* free old block, if any */ 2034 rt_key(rt) = (struct sockaddr *)new; 2035 rt->rt_gateway = (struct sockaddr *)(new + dlen); 2036 } 2037 2038 /* 2039 * Copy the new gateway value into the memory chunk. 2040 */ 2041 bcopy(gate, rt->rt_gateway, glen); 2042 2043 return (0); 2044 } 2045 2046 void 2047 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask) 2048 { 2049 u_char *cp1 = (u_char *)src; 2050 u_char *cp2 = (u_char *)dst; 2051 u_char *cp3 = (u_char *)netmask; 2052 u_char *cplim = cp2 + *cp3; 2053 u_char *cplim2 = cp2 + *cp1; 2054 2055 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */ 2056 cp3 += 2; 2057 if (cplim > cplim2) 2058 cplim = cplim2; 2059 while (cp2 < cplim) 2060 *cp2++ = *cp1++ & *cp3++; 2061 if (cp2 < cplim2) 2062 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2)); 2063 } 2064 2065 /* 2066 * Set up a routing table entry, normally 2067 * for an interface. 2068 */ 2069 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */ 2070 static inline int 2071 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum) 2072 { 2073 RIB_RLOCK_TRACKER; 2074 struct sockaddr *dst; 2075 struct sockaddr *netmask; 2076 struct rtentry *rt = NULL; 2077 struct rt_addrinfo info; 2078 int error = 0; 2079 int startfib, endfib; 2080 char tempbuf[_SOCKADDR_TMPSIZE]; 2081 int didwork = 0; 2082 int a_failure = 0; 2083 struct sockaddr_dl_short *sdl = NULL; 2084 struct rib_head *rnh; 2085 2086 if (flags & RTF_HOST) { 2087 dst = ifa->ifa_dstaddr; 2088 netmask = NULL; 2089 } else { 2090 dst = ifa->ifa_addr; 2091 netmask = ifa->ifa_netmask; 2092 } 2093 if (dst->sa_len == 0) 2094 return(EINVAL); 2095 switch (dst->sa_family) { 2096 case AF_INET6: 2097 case AF_INET: 2098 /* We support multiple FIBs. */ 2099 break; 2100 default: 2101 fibnum = RT_DEFAULT_FIB; 2102 break; 2103 } 2104 if (fibnum == RT_ALL_FIBS) { 2105 if (V_rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD) 2106 startfib = endfib = ifa->ifa_ifp->if_fib; 2107 else { 2108 startfib = 0; 2109 endfib = rt_numfibs - 1; 2110 } 2111 } else { 2112 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum")); 2113 startfib = fibnum; 2114 endfib = fibnum; 2115 } 2116 2117 /* 2118 * If it's a delete, check that if it exists, 2119 * it's on the correct interface or we might scrub 2120 * a route to another ifa which would 2121 * be confusing at best and possibly worse. 2122 */ 2123 if (cmd == RTM_DELETE) { 2124 /* 2125 * It's a delete, so it should already exist.. 2126 * If it's a net, mask off the host bits 2127 * (Assuming we have a mask) 2128 * XXX this is kinda inet specific.. 2129 */ 2130 if (netmask != NULL) { 2131 rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask); 2132 dst = (struct sockaddr *)tempbuf; 2133 } 2134 } else if (cmd == RTM_ADD) { 2135 sdl = (struct sockaddr_dl_short *)tempbuf; 2136 bzero(sdl, sizeof(struct sockaddr_dl_short)); 2137 sdl->sdl_family = AF_LINK; 2138 sdl->sdl_len = sizeof(struct sockaddr_dl_short); 2139 sdl->sdl_type = ifa->ifa_ifp->if_type; 2140 sdl->sdl_index = ifa->ifa_ifp->if_index; 2141 } 2142 /* 2143 * Now go through all the requested tables (fibs) and do the 2144 * requested action. Realistically, this will either be fib 0 2145 * for protocols that don't do multiple tables or all the 2146 * tables for those that do. 2147 */ 2148 for ( fibnum = startfib; fibnum <= endfib; fibnum++) { 2149 if (cmd == RTM_DELETE) { 2150 struct radix_node *rn; 2151 /* 2152 * Look up an rtentry that is in the routing tree and 2153 * contains the correct info. 2154 */ 2155 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 2156 if (rnh == NULL) 2157 /* this table doesn't exist but others might */ 2158 continue; 2159 RIB_RLOCK(rnh); 2160 rn = rnh->rnh_lookup(dst, netmask, &rnh->head); 2161 #ifdef RADIX_MPATH 2162 if (rt_mpath_capable(rnh)) { 2163 2164 if (rn == NULL) 2165 error = ESRCH; 2166 else { 2167 rt = RNTORT(rn); 2168 /* 2169 * for interface route the 2170 * rt->rt_gateway is sockaddr_dl, so 2171 * rt_mpath_matchgate must use the 2172 * interface address 2173 */ 2174 rt = rt_mpath_matchgate(rt, 2175 ifa->ifa_addr); 2176 if (rt == NULL) 2177 error = ESRCH; 2178 } 2179 } 2180 #endif 2181 error = (rn == NULL || 2182 (rn->rn_flags & RNF_ROOT) || 2183 RNTORT(rn)->rt_ifa != ifa); 2184 RIB_RUNLOCK(rnh); 2185 if (error) { 2186 /* this is only an error if bad on ALL tables */ 2187 continue; 2188 } 2189 } 2190 /* 2191 * Do the actual request 2192 */ 2193 bzero((caddr_t)&info, sizeof(info)); 2194 info.rti_ifa = ifa; 2195 info.rti_flags = flags | 2196 (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED; 2197 info.rti_info[RTAX_DST] = dst; 2198 /* 2199 * doing this for compatibility reasons 2200 */ 2201 if (cmd == RTM_ADD) 2202 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)sdl; 2203 else 2204 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr; 2205 info.rti_info[RTAX_NETMASK] = netmask; 2206 error = rtrequest1_fib(cmd, &info, &rt, fibnum); 2207 if (error == 0 && rt != NULL) { 2208 /* 2209 * notify any listening routing agents of the change 2210 */ 2211 RT_LOCK(rt); 2212 #ifdef RADIX_MPATH 2213 /* 2214 * in case address alias finds the first address 2215 * e.g. ifconfig bge0 192.0.2.246/24 2216 * e.g. ifconfig bge0 192.0.2.247/24 2217 * the address set in the route is 192.0.2.246 2218 * so we need to replace it with 192.0.2.247 2219 */ 2220 if (memcmp(rt->rt_ifa->ifa_addr, 2221 ifa->ifa_addr, ifa->ifa_addr->sa_len)) { 2222 ifa_free(rt->rt_ifa); 2223 ifa_ref(ifa); 2224 rt->rt_ifp = ifa->ifa_ifp; 2225 rt->rt_ifa = ifa; 2226 } 2227 #endif 2228 RT_ADDREF(rt); 2229 RT_UNLOCK(rt); 2230 rt_newaddrmsg_fib(cmd, ifa, rt, fibnum); 2231 RT_LOCK(rt); 2232 RT_REMREF(rt); 2233 if (cmd == RTM_DELETE) { 2234 /* 2235 * If we are deleting, and we found an entry, 2236 * then it's been removed from the tree.. 2237 * now throw it away. 2238 */ 2239 RTFREE_LOCKED(rt); 2240 } else { 2241 if (cmd == RTM_ADD) { 2242 /* 2243 * We just wanted to add it.. 2244 * we don't actually need a reference. 2245 */ 2246 RT_REMREF(rt); 2247 } 2248 RT_UNLOCK(rt); 2249 } 2250 didwork = 1; 2251 } 2252 if (error) 2253 a_failure = error; 2254 } 2255 if (cmd == RTM_DELETE) { 2256 if (didwork) { 2257 error = 0; 2258 } else { 2259 /* we only give an error if it wasn't in any table */ 2260 error = ((flags & RTF_HOST) ? 2261 EHOSTUNREACH : ENETUNREACH); 2262 } 2263 } else { 2264 if (a_failure) { 2265 /* return an error if any of them failed */ 2266 error = a_failure; 2267 } 2268 } 2269 return (error); 2270 } 2271 2272 /* 2273 * Set up a routing table entry, normally 2274 * for an interface. 2275 */ 2276 int 2277 rtinit(struct ifaddr *ifa, int cmd, int flags) 2278 { 2279 struct sockaddr *dst; 2280 int fib = RT_DEFAULT_FIB; 2281 2282 if (flags & RTF_HOST) { 2283 dst = ifa->ifa_dstaddr; 2284 } else { 2285 dst = ifa->ifa_addr; 2286 } 2287 2288 switch (dst->sa_family) { 2289 case AF_INET6: 2290 case AF_INET: 2291 /* We do support multiple FIBs. */ 2292 fib = RT_ALL_FIBS; 2293 break; 2294 } 2295 return (rtinit1(ifa, cmd, flags, fib)); 2296 } 2297 2298 /* 2299 * Announce interface address arrival/withdraw 2300 * Returns 0 on success. 2301 */ 2302 int 2303 rt_addrmsg(int cmd, struct ifaddr *ifa, int fibnum) 2304 { 2305 2306 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 2307 ("unexpected cmd %d", cmd)); 2308 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs), 2309 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs)); 2310 2311 EVENTHANDLER_DIRECT_INVOKE(rt_addrmsg, ifa, cmd); 2312 return (rtsock_addrmsg(cmd, ifa, fibnum)); 2313 } 2314 2315 /* 2316 * Announce kernel-originated route addition/removal to rtsock based on @rt data. 2317 * cmd: RTM_ cmd 2318 * @rt: valid rtentry 2319 * @ifp: target route interface 2320 * @fibnum: fib id or RT_ALL_FIBS 2321 * 2322 * Returns 0 on success. 2323 */ 2324 int 2325 rt_routemsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int rti_addrs, 2326 int fibnum) 2327 { 2328 2329 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 2330 ("unexpected cmd %d", cmd)); 2331 2332 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs), 2333 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs)); 2334 2335 KASSERT(rt_key(rt) != NULL, (":%s: rt_key must be supplied", __func__)); 2336 2337 return (rtsock_routemsg(cmd, rt, ifp, 0, fibnum)); 2338 } 2339 2340 /* 2341 * Announce kernel-originated route addition/removal to rtsock based on @rt data. 2342 * cmd: RTM_ cmd 2343 * @info: addrinfo structure with valid data. 2344 * @fibnum: fib id or RT_ALL_FIBS 2345 * 2346 * Returns 0 on success. 2347 */ 2348 int 2349 rt_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum) 2350 { 2351 2352 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE || cmd == RTM_CHANGE, 2353 ("unexpected cmd %d", cmd)); 2354 2355 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs), 2356 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs)); 2357 2358 KASSERT(info->rti_info[RTAX_DST] != NULL, (":%s: RTAX_DST must be supplied", __func__)); 2359 2360 return (rtsock_routemsg_info(cmd, info, fibnum)); 2361 } 2362 2363 2364 /* 2365 * This is called to generate messages from the routing socket 2366 * indicating a network interface has had addresses associated with it. 2367 */ 2368 void 2369 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, struct rtentry *rt, int fibnum) 2370 { 2371 2372 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 2373 ("unexpected cmd %u", cmd)); 2374 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs), 2375 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs)); 2376 2377 if (cmd == RTM_ADD) { 2378 rt_addrmsg(cmd, ifa, fibnum); 2379 if (rt != NULL) 2380 rt_routemsg(cmd, rt, ifa->ifa_ifp, 0, fibnum); 2381 } else { 2382 if (rt != NULL) 2383 rt_routemsg(cmd, rt, ifa->ifa_ifp, 0, fibnum); 2384 rt_addrmsg(cmd, ifa, fibnum); 2385 } 2386 } 2387 2388