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