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