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_route.h" 39 #include "opt_mrouting.h" 40 #include "opt_mpath.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/syslog.h> 45 #include <sys/malloc.h> 46 #include <sys/mbuf.h> 47 #include <sys/socket.h> 48 #include <sys/sysctl.h> 49 #include <sys/syslog.h> 50 #include <sys/sysproto.h> 51 #include <sys/proc.h> 52 #include <sys/domain.h> 53 #include <sys/kernel.h> 54 #include <sys/vimage.h> 55 56 #include <net/if.h> 57 #include <net/if_dl.h> 58 #include <net/route.h> 59 60 #ifdef RADIX_MPATH 61 #include <net/radix_mpath.h> 62 #endif 63 #include <net/vnet.h> 64 65 #include <netinet/in.h> 66 #include <netinet/ip_mroute.h> 67 #include <netinet/vinet.h> 68 69 #include <vm/uma.h> 70 71 u_int rt_numfibs = RT_NUMFIBS; 72 SYSCTL_INT(_net, OID_AUTO, fibs, CTLFLAG_RD, &rt_numfibs, 0, ""); 73 /* 74 * Allow the boot code to allow LESS than RT_MAXFIBS to be used. 75 * We can't do more because storage is statically allocated for now. 76 * (for compatibility reasons.. this will change). 77 */ 78 TUNABLE_INT("net.fibs", &rt_numfibs); 79 80 /* 81 * By default add routes to all fibs for new interfaces. 82 * Once this is set to 0 then only allocate routes on interface 83 * changes for the FIB of the caller when adding a new set of addresses 84 * to an interface. XXX this is a shotgun aproach to a problem that needs 85 * a more fine grained solution.. that will come. 86 */ 87 u_int rt_add_addr_allfibs = 1; 88 SYSCTL_INT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RW, 89 &rt_add_addr_allfibs, 0, ""); 90 TUNABLE_INT("net.add_addr_allfibs", &rt_add_addr_allfibs); 91 92 #ifdef VIMAGE_GLOBALS 93 static struct rtstat rtstat; 94 struct radix_node_head *rt_tables; 95 96 static int rttrash; /* routes not in table but not freed */ 97 #endif 98 99 static void rt_maskedcopy(struct sockaddr *, 100 struct sockaddr *, struct sockaddr *); 101 static int vnet_route_iattach(const void *); 102 103 #ifndef VIMAGE_GLOBALS 104 static const vnet_modinfo_t vnet_rtable_modinfo = { 105 .vmi_id = VNET_MOD_RTABLE, 106 .vmi_name = "rtable", 107 .vmi_iattach = vnet_route_iattach 108 }; 109 #endif /* !VIMAGE_GLOBALS */ 110 111 /* compare two sockaddr structures */ 112 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0) 113 114 /* 115 * Convert a 'struct radix_node *' to a 'struct rtentry *'. 116 * The operation can be done safely (in this code) because a 117 * 'struct rtentry' starts with two 'struct radix_node''s, the first 118 * one representing leaf nodes in the routing tree, which is 119 * what the code in radix.c passes us as a 'struct radix_node'. 120 * 121 * But because there are a lot of assumptions in this conversion, 122 * do not cast explicitly, but always use the macro below. 123 */ 124 #define RNTORT(p) ((struct rtentry *)(p)) 125 126 #ifdef VIMAGE_GLOBALS 127 static uma_zone_t rtzone; /* Routing table UMA zone. */ 128 #endif 129 130 #if 0 131 /* default fib for tunnels to use */ 132 u_int tunnel_fib = 0; 133 SYSCTL_INT(_net, OID_AUTO, tunnelfib, CTLFLAG_RD, &tunnel_fib, 0, ""); 134 #endif 135 136 /* 137 * handler for net.my_fibnum 138 */ 139 static int 140 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS) 141 { 142 int fibnum; 143 int error; 144 145 fibnum = curthread->td_proc->p_fibnum; 146 error = sysctl_handle_int(oidp, &fibnum, 0, req); 147 return (error); 148 } 149 150 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD, 151 NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller"); 152 153 static __inline struct radix_node_head ** 154 rt_tables_get_rnh_ptr(int table, int fam) 155 { 156 INIT_VNET_NET(curvnet); 157 struct radix_node_head **rnh; 158 159 KASSERT(table >= 0 && table < rt_numfibs, ("%s: table out of bounds.", 160 __func__)); 161 KASSERT(fam >= 0 && fam < (AF_MAX+1), ("%s: fam out of bounds.", 162 __func__)); 163 164 /* rnh is [fib=0][af=0]. */ 165 rnh = (struct radix_node_head **)V_rt_tables; 166 /* Get the offset to the requested table and fam. */ 167 rnh += table * (AF_MAX+1) + fam; 168 169 return (rnh); 170 } 171 172 struct radix_node_head * 173 rt_tables_get_rnh(int table, int fam) 174 { 175 176 return (*rt_tables_get_rnh_ptr(table, fam)); 177 } 178 179 static void 180 route_init(void) 181 { 182 183 /* whack the tunable ints into line. */ 184 if (rt_numfibs > RT_MAXFIBS) 185 rt_numfibs = RT_MAXFIBS; 186 if (rt_numfibs == 0) 187 rt_numfibs = 1; 188 rn_init(); /* initialize all zeroes, all ones, mask table */ 189 190 #ifndef VIMAGE_GLOBALS 191 vnet_mod_register(&vnet_rtable_modinfo); 192 #else 193 vnet_route_iattach(NULL); 194 #endif 195 } 196 197 static int vnet_route_iattach(const void *unused __unused) 198 { 199 INIT_VNET_NET(curvnet); 200 struct domain *dom; 201 struct radix_node_head **rnh; 202 int table; 203 int fam; 204 205 V_rt_tables = malloc(rt_numfibs * (AF_MAX+1) * 206 sizeof(struct radix_node_head *), M_RTABLE, M_WAITOK|M_ZERO); 207 208 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), NULL, NULL, 209 NULL, NULL, UMA_ALIGN_PTR, 0); 210 for (dom = domains; dom; dom = dom->dom_next) { 211 if (dom->dom_rtattach) { 212 for (table = 0; table < rt_numfibs; table++) { 213 if ( (fam = dom->dom_family) == AF_INET || 214 table == 0) { 215 /* for now only AF_INET has > 1 table */ 216 /* XXX MRT 217 * rtattach will be also called 218 * from vfs_export.c but the 219 * offset will be 0 220 * (only for AF_INET and AF_INET6 221 * which don't need it anyhow) 222 */ 223 rnh = rt_tables_get_rnh_ptr(table, fam); 224 if (rnh == NULL) 225 panic("%s: rnh NULL", __func__); 226 dom->dom_rtattach((void **)rnh, 227 dom->dom_rtoffset); 228 } else { 229 break; 230 } 231 } 232 } 233 } 234 235 return (0); 236 } 237 238 #ifndef _SYS_SYSPROTO_H_ 239 struct setfib_args { 240 int fibnum; 241 }; 242 #endif 243 int 244 setfib(struct thread *td, struct setfib_args *uap) 245 { 246 if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs) 247 return EINVAL; 248 td->td_proc->p_fibnum = uap->fibnum; 249 return (0); 250 } 251 252 /* 253 * Packet routing routines. 254 */ 255 void 256 rtalloc(struct route *ro) 257 { 258 rtalloc_ign_fib(ro, 0UL, 0); 259 } 260 261 void 262 rtalloc_fib(struct route *ro, u_int fibnum) 263 { 264 rtalloc_ign_fib(ro, 0UL, fibnum); 265 } 266 267 void 268 rtalloc_ign(struct route *ro, u_long ignore) 269 { 270 struct rtentry *rt; 271 272 if ((rt = ro->ro_rt) != NULL) { 273 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP) 274 return; 275 RTFREE(rt); 276 ro->ro_rt = NULL; 277 } 278 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, 0); 279 if (ro->ro_rt) 280 RT_UNLOCK(ro->ro_rt); 281 } 282 283 void 284 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum) 285 { 286 struct rtentry *rt; 287 288 if ((rt = ro->ro_rt) != NULL) { 289 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP) 290 return; 291 RTFREE(rt); 292 ro->ro_rt = NULL; 293 } 294 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum); 295 if (ro->ro_rt) 296 RT_UNLOCK(ro->ro_rt); 297 } 298 299 /* 300 * Look up the route that matches the address given 301 * Or, at least try.. Create a cloned route if needed. 302 * 303 * The returned route, if any, is locked. 304 */ 305 struct rtentry * 306 rtalloc1(struct sockaddr *dst, int report, u_long ignflags) 307 { 308 return (rtalloc1_fib(dst, report, ignflags, 0)); 309 } 310 311 struct rtentry * 312 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags, 313 u_int fibnum) 314 { 315 INIT_VNET_NET(curvnet); 316 struct radix_node_head *rnh; 317 struct rtentry *rt; 318 struct radix_node *rn; 319 struct rtentry *newrt; 320 struct rt_addrinfo info; 321 int err = 0, msgtype = RTM_MISS; 322 int needlock; 323 324 KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum")); 325 if (dst->sa_family != AF_INET) /* Only INET supports > 1 fib now */ 326 fibnum = 0; 327 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 328 newrt = NULL; 329 /* 330 * Look up the address in the table for that Address Family 331 */ 332 if (rnh == NULL) { 333 V_rtstat.rts_unreach++; 334 goto miss; 335 } 336 needlock = !(ignflags & RTF_RNH_LOCKED); 337 if (needlock) 338 RADIX_NODE_HEAD_RLOCK(rnh); 339 #ifdef INVARIANTS 340 else 341 RADIX_NODE_HEAD_LOCK_ASSERT(rnh); 342 #endif 343 rn = rnh->rnh_matchaddr(dst, rnh); 344 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) { 345 newrt = rt = RNTORT(rn); 346 RT_LOCK(newrt); 347 RT_ADDREF(newrt); 348 if (needlock) 349 RADIX_NODE_HEAD_RUNLOCK(rnh); 350 goto done; 351 352 } else if (needlock) 353 RADIX_NODE_HEAD_RUNLOCK(rnh); 354 355 /* 356 * Either we hit the root or couldn't find any match, 357 * Which basically means 358 * "caint get there frm here" 359 */ 360 V_rtstat.rts_unreach++; 361 miss: 362 if (report) { 363 /* 364 * If required, report the failure to the supervising 365 * Authorities. 366 * For a delete, this is not an error. (report == 0) 367 */ 368 bzero(&info, sizeof(info)); 369 info.rti_info[RTAX_DST] = dst; 370 rt_missmsg(msgtype, &info, 0, err); 371 } 372 done: 373 if (newrt) 374 RT_LOCK_ASSERT(newrt); 375 return (newrt); 376 } 377 378 /* 379 * Remove a reference count from an rtentry. 380 * If the count gets low enough, take it out of the routing table 381 */ 382 void 383 rtfree(struct rtentry *rt) 384 { 385 INIT_VNET_NET(curvnet); 386 struct radix_node_head *rnh; 387 388 KASSERT(rt != NULL,("%s: NULL rt", __func__)); 389 rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family); 390 KASSERT(rnh != NULL,("%s: NULL rnh", __func__)); 391 392 RT_LOCK_ASSERT(rt); 393 394 /* 395 * The callers should use RTFREE_LOCKED() or RTFREE(), so 396 * we should come here exactly with the last reference. 397 */ 398 RT_REMREF(rt); 399 if (rt->rt_refcnt > 0) { 400 log(LOG_DEBUG, "%s: %p has %d refs\n", __func__, rt, rt->rt_refcnt); 401 goto done; 402 } 403 404 /* 405 * On last reference give the "close method" a chance 406 * to cleanup private state. This also permits (for 407 * IPv4 and IPv6) a chance to decide if the routing table 408 * entry should be purged immediately or at a later time. 409 * When an immediate purge is to happen the close routine 410 * typically calls rtexpunge which clears the RTF_UP flag 411 * on the entry so that the code below reclaims the storage. 412 */ 413 if (rt->rt_refcnt == 0 && rnh->rnh_close) 414 rnh->rnh_close((struct radix_node *)rt, rnh); 415 416 /* 417 * If we are no longer "up" (and ref == 0) 418 * then we can free the resources associated 419 * with the route. 420 */ 421 if ((rt->rt_flags & RTF_UP) == 0) { 422 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 423 panic("rtfree 2"); 424 /* 425 * the rtentry must have been removed from the routing table 426 * so it is represented in rttrash.. remove that now. 427 */ 428 V_rttrash--; 429 #ifdef DIAGNOSTIC 430 if (rt->rt_refcnt < 0) { 431 printf("rtfree: %p not freed (neg refs)\n", rt); 432 goto done; 433 } 434 #endif 435 /* 436 * release references on items we hold them on.. 437 * e.g other routes and ifaddrs. 438 */ 439 if (rt->rt_ifa) 440 IFAFREE(rt->rt_ifa); 441 /* 442 * The key is separatly alloc'd so free it (see rt_setgate()). 443 * This also frees the gateway, as they are always malloc'd 444 * together. 445 */ 446 Free(rt_key(rt)); 447 448 /* 449 * and the rtentry itself of course 450 */ 451 RT_LOCK_DESTROY(rt); 452 uma_zfree(V_rtzone, rt); 453 return; 454 } 455 done: 456 RT_UNLOCK(rt); 457 } 458 459 460 /* 461 * Force a routing table entry to the specified 462 * destination to go through the given gateway. 463 * Normally called as a result of a routing redirect 464 * message from the network layer. 465 */ 466 void 467 rtredirect(struct sockaddr *dst, 468 struct sockaddr *gateway, 469 struct sockaddr *netmask, 470 int flags, 471 struct sockaddr *src) 472 { 473 rtredirect_fib(dst, gateway, netmask, flags, src, 0); 474 } 475 476 void 477 rtredirect_fib(struct sockaddr *dst, 478 struct sockaddr *gateway, 479 struct sockaddr *netmask, 480 int flags, 481 struct sockaddr *src, 482 u_int fibnum) 483 { 484 INIT_VNET_NET(curvnet); 485 struct rtentry *rt, *rt0 = NULL; 486 int error = 0; 487 short *stat = NULL; 488 struct rt_addrinfo info; 489 struct ifaddr *ifa; 490 struct radix_node_head *rnh; 491 492 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 493 if (rnh == NULL) { 494 error = EAFNOSUPPORT; 495 goto out; 496 } 497 498 /* verify the gateway is directly reachable */ 499 if ((ifa = ifa_ifwithnet(gateway)) == NULL) { 500 error = ENETUNREACH; 501 goto out; 502 } 503 rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */ 504 /* 505 * If the redirect isn't from our current router for this dst, 506 * it's either old or wrong. If it redirects us to ourselves, 507 * we have a routing loop, perhaps as a result of an interface 508 * going down recently. 509 */ 510 if (!(flags & RTF_DONE) && rt && 511 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) 512 error = EINVAL; 513 else if (ifa_ifwithaddr(gateway)) 514 error = EHOSTUNREACH; 515 if (error) 516 goto done; 517 /* 518 * Create a new entry if we just got back a wildcard entry 519 * or the the lookup failed. This is necessary for hosts 520 * which use routing redirects generated by smart gateways 521 * to dynamically build the routing tables. 522 */ 523 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2)) 524 goto create; 525 /* 526 * Don't listen to the redirect if it's 527 * for a route to an interface. 528 */ 529 if (rt->rt_flags & RTF_GATEWAY) { 530 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) { 531 /* 532 * Changing from route to net => route to host. 533 * Create new route, rather than smashing route to net. 534 */ 535 create: 536 rt0 = rt; 537 rt = NULL; 538 539 flags |= RTF_GATEWAY | RTF_DYNAMIC; 540 bzero((caddr_t)&info, sizeof(info)); 541 info.rti_info[RTAX_DST] = dst; 542 info.rti_info[RTAX_GATEWAY] = gateway; 543 info.rti_info[RTAX_NETMASK] = netmask; 544 info.rti_ifa = ifa; 545 info.rti_flags = flags; 546 if (rt0 != NULL) 547 RT_UNLOCK(rt0); /* drop lock to avoid LOR with RNH */ 548 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum); 549 if (rt != NULL) { 550 RT_LOCK(rt); 551 if (rt0 != NULL) 552 EVENTHANDLER_INVOKE(route_redirect_event, rt0, rt, dst); 553 flags = rt->rt_flags; 554 } 555 if (rt0 != NULL) 556 RTFREE(rt0); 557 558 stat = &V_rtstat.rts_dynamic; 559 } else { 560 struct rtentry *gwrt; 561 562 /* 563 * Smash the current notion of the gateway to 564 * this destination. Should check about netmask!!! 565 */ 566 rt->rt_flags |= RTF_MODIFIED; 567 flags |= RTF_MODIFIED; 568 stat = &V_rtstat.rts_newgateway; 569 /* 570 * add the key and gateway (in one malloc'd chunk). 571 */ 572 RT_UNLOCK(rt); 573 RADIX_NODE_HEAD_LOCK(rnh); 574 RT_LOCK(rt); 575 rt_setgate(rt, rt_key(rt), gateway); 576 gwrt = rtalloc1(gateway, 1, RTF_RNH_LOCKED); 577 RADIX_NODE_HEAD_UNLOCK(rnh); 578 EVENTHANDLER_INVOKE(route_redirect_event, rt, gwrt, dst); 579 RTFREE_LOCKED(gwrt); 580 } 581 } else 582 error = EHOSTUNREACH; 583 done: 584 if (rt) 585 RTFREE_LOCKED(rt); 586 out: 587 if (error) 588 V_rtstat.rts_badredirect++; 589 else if (stat != NULL) 590 (*stat)++; 591 bzero((caddr_t)&info, sizeof(info)); 592 info.rti_info[RTAX_DST] = dst; 593 info.rti_info[RTAX_GATEWAY] = gateway; 594 info.rti_info[RTAX_NETMASK] = netmask; 595 info.rti_info[RTAX_AUTHOR] = src; 596 rt_missmsg(RTM_REDIRECT, &info, flags, error); 597 } 598 599 int 600 rtioctl(u_long req, caddr_t data) 601 { 602 return (rtioctl_fib(req, data, 0)); 603 } 604 605 /* 606 * Routing table ioctl interface. 607 */ 608 int 609 rtioctl_fib(u_long req, caddr_t data, u_int fibnum) 610 { 611 612 /* 613 * If more ioctl commands are added here, make sure the proper 614 * super-user checks are being performed because it is possible for 615 * prison-root to make it this far if raw sockets have been enabled 616 * in jails. 617 */ 618 #ifdef INET 619 /* Multicast goop, grrr... */ 620 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP; 621 #else /* INET */ 622 return ENXIO; 623 #endif /* INET */ 624 } 625 626 struct ifaddr * 627 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway) 628 { 629 return (ifa_ifwithroute_fib(flags, dst, gateway, 0)); 630 } 631 632 struct ifaddr * 633 ifa_ifwithroute_fib(int flags, struct sockaddr *dst, struct sockaddr *gateway, 634 u_int fibnum) 635 { 636 register struct ifaddr *ifa; 637 int not_found = 0; 638 639 if ((flags & RTF_GATEWAY) == 0) { 640 /* 641 * If we are adding a route to an interface, 642 * and the interface is a pt to pt link 643 * we should search for the destination 644 * as our clue to the interface. Otherwise 645 * we can use the local address. 646 */ 647 ifa = NULL; 648 if (flags & RTF_HOST) 649 ifa = ifa_ifwithdstaddr(dst); 650 if (ifa == NULL) 651 ifa = ifa_ifwithaddr(gateway); 652 } else { 653 /* 654 * If we are adding a route to a remote net 655 * or host, the gateway may still be on the 656 * other end of a pt to pt link. 657 */ 658 ifa = ifa_ifwithdstaddr(gateway); 659 } 660 if (ifa == NULL) 661 ifa = ifa_ifwithnet(gateway); 662 if (ifa == NULL) { 663 struct rtentry *rt = rtalloc1_fib(gateway, 0, RTF_RNH_LOCKED, fibnum); 664 if (rt == NULL) 665 return (NULL); 666 /* 667 * dismiss a gateway that is reachable only 668 * through the default router 669 */ 670 switch (gateway->sa_family) { 671 case AF_INET: 672 if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY) 673 not_found = 1; 674 break; 675 case AF_INET6: 676 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr)) 677 not_found = 1; 678 break; 679 default: 680 break; 681 } 682 RT_REMREF(rt); 683 RT_UNLOCK(rt); 684 if (not_found) 685 return (NULL); 686 if ((ifa = rt->rt_ifa) == NULL) 687 return (NULL); 688 } 689 if (ifa->ifa_addr->sa_family != dst->sa_family) { 690 struct ifaddr *oifa = ifa; 691 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 692 if (ifa == NULL) 693 ifa = oifa; 694 } 695 return (ifa); 696 } 697 698 /* 699 * Do appropriate manipulations of a routing tree given 700 * all the bits of info needed 701 */ 702 int 703 rtrequest(int req, 704 struct sockaddr *dst, 705 struct sockaddr *gateway, 706 struct sockaddr *netmask, 707 int flags, 708 struct rtentry **ret_nrt) 709 { 710 return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt, 0)); 711 } 712 713 int 714 rtrequest_fib(int req, 715 struct sockaddr *dst, 716 struct sockaddr *gateway, 717 struct sockaddr *netmask, 718 int flags, 719 struct rtentry **ret_nrt, 720 u_int fibnum) 721 { 722 struct rt_addrinfo info; 723 724 if (dst->sa_len == 0) 725 return(EINVAL); 726 727 bzero((caddr_t)&info, sizeof(info)); 728 info.rti_flags = flags; 729 info.rti_info[RTAX_DST] = dst; 730 info.rti_info[RTAX_GATEWAY] = gateway; 731 info.rti_info[RTAX_NETMASK] = netmask; 732 return rtrequest1_fib(req, &info, ret_nrt, fibnum); 733 } 734 735 /* 736 * These (questionable) definitions of apparent local variables apply 737 * to the next two functions. XXXXXX!!! 738 */ 739 #define dst info->rti_info[RTAX_DST] 740 #define gateway info->rti_info[RTAX_GATEWAY] 741 #define netmask info->rti_info[RTAX_NETMASK] 742 #define ifaaddr info->rti_info[RTAX_IFA] 743 #define ifpaddr info->rti_info[RTAX_IFP] 744 #define flags info->rti_flags 745 746 int 747 rt_getifa(struct rt_addrinfo *info) 748 { 749 return (rt_getifa_fib(info, 0)); 750 } 751 752 int 753 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum) 754 { 755 struct ifaddr *ifa; 756 int error = 0; 757 758 /* 759 * ifp may be specified by sockaddr_dl 760 * when protocol address is ambiguous. 761 */ 762 if (info->rti_ifp == NULL && ifpaddr != NULL && 763 ifpaddr->sa_family == AF_LINK && 764 (ifa = ifa_ifwithnet(ifpaddr)) != NULL) 765 info->rti_ifp = ifa->ifa_ifp; 766 if (info->rti_ifa == NULL && ifaaddr != NULL) 767 info->rti_ifa = ifa_ifwithaddr(ifaaddr); 768 if (info->rti_ifa == NULL) { 769 struct sockaddr *sa; 770 771 sa = ifaaddr != NULL ? ifaaddr : 772 (gateway != NULL ? gateway : dst); 773 if (sa != NULL && info->rti_ifp != NULL) 774 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp); 775 else if (dst != NULL && gateway != NULL) 776 info->rti_ifa = ifa_ifwithroute_fib(flags, dst, gateway, 777 fibnum); 778 else if (sa != NULL) 779 info->rti_ifa = ifa_ifwithroute_fib(flags, sa, sa, 780 fibnum); 781 } 782 if ((ifa = info->rti_ifa) != NULL) { 783 if (info->rti_ifp == NULL) 784 info->rti_ifp = ifa->ifa_ifp; 785 } else 786 error = ENETUNREACH; 787 return (error); 788 } 789 790 /* 791 * Expunges references to a route that's about to be reclaimed. 792 * The route must be locked. 793 */ 794 int 795 rtexpunge(struct rtentry *rt) 796 { 797 INIT_VNET_NET(curvnet); 798 struct radix_node *rn; 799 struct radix_node_head *rnh; 800 struct ifaddr *ifa; 801 int error = 0; 802 803 /* 804 * Find the correct routing tree to use for this Address Family 805 */ 806 rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family); 807 RT_LOCK_ASSERT(rt); 808 if (rnh == NULL) 809 return (EAFNOSUPPORT); 810 RADIX_NODE_HEAD_LOCK_ASSERT(rnh); 811 #if 0 812 /* 813 * We cannot assume anything about the reference count 814 * because protocols call us in many situations; often 815 * before unwinding references to the table entry. 816 */ 817 KASSERT(rt->rt_refcnt <= 1, ("bogus refcnt %ld", rt->rt_refcnt)); 818 #endif 819 /* 820 * Remove the item from the tree; it should be there, 821 * but when callers invoke us blindly it may not (sigh). 822 */ 823 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh); 824 if (rn == NULL) { 825 error = ESRCH; 826 goto bad; 827 } 828 KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0, 829 ("unexpected flags 0x%x", rn->rn_flags)); 830 KASSERT(rt == RNTORT(rn), 831 ("lookup mismatch, rt %p rn %p", rt, rn)); 832 833 rt->rt_flags &= ~RTF_UP; 834 835 /* 836 * Give the protocol a chance to keep things in sync. 837 */ 838 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) { 839 struct rt_addrinfo info; 840 841 bzero((caddr_t)&info, sizeof(info)); 842 info.rti_flags = rt->rt_flags; 843 info.rti_info[RTAX_DST] = rt_key(rt); 844 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 845 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 846 ifa->ifa_rtrequest(RTM_DELETE, rt, &info); 847 } 848 849 /* 850 * one more rtentry floating around that is not 851 * linked to the routing table. 852 */ 853 V_rttrash++; 854 bad: 855 return (error); 856 } 857 858 #ifdef RADIX_MPATH 859 static int 860 rn_mpath_update(int req, struct rt_addrinfo *info, 861 struct radix_node_head *rnh, struct rtentry **ret_nrt) 862 { 863 /* 864 * if we got multipath routes, we require users to specify 865 * a matching RTAX_GATEWAY. 866 */ 867 struct rtentry *rt, *rto = NULL; 868 register struct radix_node *rn; 869 int error = 0; 870 871 rn = rnh->rnh_matchaddr(dst, rnh); 872 if (rn == NULL) 873 return (ESRCH); 874 rto = rt = RNTORT(rn); 875 rt = rt_mpath_matchgate(rt, gateway); 876 if (rt == NULL) 877 return (ESRCH); 878 /* 879 * this is the first entry in the chain 880 */ 881 if (rto == rt) { 882 rn = rn_mpath_next((struct radix_node *)rt); 883 /* 884 * there is another entry, now it's active 885 */ 886 if (rn) { 887 rto = RNTORT(rn); 888 RT_LOCK(rto); 889 rto->rt_flags |= RTF_UP; 890 RT_UNLOCK(rto); 891 } else if (rt->rt_flags & RTF_GATEWAY) { 892 /* 893 * For gateway routes, we need to 894 * make sure that we we are deleting 895 * the correct gateway. 896 * rt_mpath_matchgate() does not 897 * check the case when there is only 898 * one route in the chain. 899 */ 900 if (gateway && 901 (rt->rt_gateway->sa_len != gateway->sa_len || 902 memcmp(rt->rt_gateway, gateway, gateway->sa_len))) 903 error = ESRCH; 904 goto done; 905 } 906 /* 907 * use the normal delete code to remove 908 * the first entry 909 */ 910 if (req != RTM_DELETE) 911 goto nondelete; 912 913 error = ENOENT; 914 goto done; 915 } 916 917 /* 918 * if the entry is 2nd and on up 919 */ 920 if ((req == RTM_DELETE) && !rt_mpath_deldup(rto, rt)) 921 panic ("rtrequest1: rt_mpath_deldup"); 922 RT_LOCK(rt); 923 RT_ADDREF(rt); 924 if (req == RTM_DELETE) { 925 rt->rt_flags &= ~RTF_UP; 926 /* 927 * One more rtentry floating around that is not 928 * linked to the routing table. rttrash will be decremented 929 * when RTFREE(rt) is eventually called. 930 */ 931 V_rttrash++; 932 933 } 934 935 nondelete: 936 if (req != RTM_DELETE) 937 panic("unrecognized request %d", req); 938 939 940 /* 941 * If the caller wants it, then it can have it, 942 * but it's up to it to free the rtentry as we won't be 943 * doing it. 944 */ 945 if (ret_nrt) { 946 *ret_nrt = rt; 947 RT_UNLOCK(rt); 948 } else 949 RTFREE_LOCKED(rt); 950 done: 951 return (error); 952 } 953 #endif 954 955 int 956 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt, 957 u_int fibnum) 958 { 959 INIT_VNET_NET(curvnet); 960 int error = 0, needlock = 0; 961 register struct rtentry *rt; 962 register struct radix_node *rn; 963 register struct radix_node_head *rnh; 964 struct ifaddr *ifa; 965 struct sockaddr *ndst; 966 #define senderr(x) { error = x ; goto bad; } 967 968 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum")); 969 if (dst->sa_family != AF_INET) /* Only INET supports > 1 fib now */ 970 fibnum = 0; 971 /* 972 * Find the correct routing tree to use for this Address Family 973 */ 974 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 975 if (rnh == NULL) 976 return (EAFNOSUPPORT); 977 needlock = ((flags & RTF_RNH_LOCKED) == 0); 978 flags &= ~RTF_RNH_LOCKED; 979 if (needlock) 980 RADIX_NODE_HEAD_LOCK(rnh); 981 else 982 RADIX_NODE_HEAD_LOCK_ASSERT(rnh); 983 /* 984 * If we are adding a host route then we don't want to put 985 * a netmask in the tree, nor do we want to clone it. 986 */ 987 if (flags & RTF_HOST) 988 netmask = NULL; 989 990 switch (req) { 991 case RTM_DELETE: 992 #ifdef RADIX_MPATH 993 if (rn_mpath_capable(rnh)) { 994 error = rn_mpath_update(req, info, rnh, ret_nrt); 995 /* 996 * "bad" holds true for the success case 997 * as well 998 */ 999 if (error != ENOENT) 1000 goto bad; 1001 } 1002 #endif 1003 /* 1004 * Remove the item from the tree and return it. 1005 * Complain if it is not there and do no more processing. 1006 */ 1007 rn = rnh->rnh_deladdr(dst, netmask, rnh); 1008 if (rn == NULL) 1009 senderr(ESRCH); 1010 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 1011 panic ("rtrequest delete"); 1012 rt = RNTORT(rn); 1013 RT_LOCK(rt); 1014 RT_ADDREF(rt); 1015 rt->rt_flags &= ~RTF_UP; 1016 1017 /* 1018 * give the protocol a chance to keep things in sync. 1019 */ 1020 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) 1021 ifa->ifa_rtrequest(RTM_DELETE, rt, info); 1022 1023 /* 1024 * One more rtentry floating around that is not 1025 * linked to the routing table. rttrash will be decremented 1026 * when RTFREE(rt) is eventually called. 1027 */ 1028 V_rttrash++; 1029 1030 /* 1031 * If the caller wants it, then it can have it, 1032 * but it's up to it to free the rtentry as we won't be 1033 * doing it. 1034 */ 1035 if (ret_nrt) { 1036 *ret_nrt = rt; 1037 RT_UNLOCK(rt); 1038 } else 1039 RTFREE_LOCKED(rt); 1040 break; 1041 case RTM_RESOLVE: 1042 /* 1043 * resolve was only used for route cloning 1044 * here for compat 1045 */ 1046 break; 1047 case RTM_ADD: 1048 if ((flags & RTF_GATEWAY) && !gateway) 1049 senderr(EINVAL); 1050 if (dst && gateway && (dst->sa_family != gateway->sa_family) && 1051 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK)) 1052 senderr(EINVAL); 1053 1054 if (info->rti_ifa == NULL && (error = rt_getifa_fib(info, fibnum))) 1055 senderr(error); 1056 ifa = info->rti_ifa; 1057 rt = uma_zalloc(V_rtzone, M_NOWAIT | M_ZERO); 1058 if (rt == NULL) 1059 senderr(ENOBUFS); 1060 RT_LOCK_INIT(rt); 1061 rt->rt_flags = RTF_UP | flags; 1062 rt->rt_fibnum = fibnum; 1063 /* 1064 * Add the gateway. Possibly re-malloc-ing the storage for it 1065 * 1066 */ 1067 RT_LOCK(rt); 1068 if ((error = rt_setgate(rt, dst, gateway)) != 0) { 1069 RT_LOCK_DESTROY(rt); 1070 uma_zfree(V_rtzone, rt); 1071 senderr(error); 1072 } 1073 1074 /* 1075 * point to the (possibly newly malloc'd) dest address. 1076 */ 1077 ndst = (struct sockaddr *)rt_key(rt); 1078 1079 /* 1080 * make sure it contains the value we want (masked if needed). 1081 */ 1082 if (netmask) { 1083 rt_maskedcopy(dst, ndst, netmask); 1084 } else 1085 bcopy(dst, ndst, dst->sa_len); 1086 1087 /* 1088 * Note that we now have a reference to the ifa. 1089 * This moved from below so that rnh->rnh_addaddr() can 1090 * examine the ifa and ifa->ifa_ifp if it so desires. 1091 */ 1092 IFAREF(ifa); 1093 rt->rt_ifa = ifa; 1094 rt->rt_ifp = ifa->ifa_ifp; 1095 rt->rt_rmx.rmx_weight = 1; 1096 1097 #ifdef RADIX_MPATH 1098 /* do not permit exactly the same dst/mask/gw pair */ 1099 if (rn_mpath_capable(rnh) && 1100 rt_mpath_conflict(rnh, rt, netmask)) { 1101 if (rt->rt_ifa) { 1102 IFAFREE(rt->rt_ifa); 1103 } 1104 Free(rt_key(rt)); 1105 RT_LOCK_DESTROY(rt); 1106 uma_zfree(V_rtzone, rt); 1107 senderr(EEXIST); 1108 } 1109 #endif 1110 1111 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */ 1112 rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes); 1113 /* 1114 * If it still failed to go into the tree, 1115 * then un-make it (this should be a function) 1116 */ 1117 if (rn == NULL) { 1118 if (rt->rt_ifa) 1119 IFAFREE(rt->rt_ifa); 1120 Free(rt_key(rt)); 1121 RT_LOCK_DESTROY(rt); 1122 uma_zfree(V_rtzone, rt); 1123 senderr(EEXIST); 1124 } 1125 1126 /* 1127 * If this protocol has something to add to this then 1128 * allow it to do that as well. 1129 */ 1130 if (ifa->ifa_rtrequest) 1131 ifa->ifa_rtrequest(req, rt, info); 1132 1133 /* 1134 * actually return a resultant rtentry and 1135 * give the caller a single reference. 1136 */ 1137 if (ret_nrt) { 1138 *ret_nrt = rt; 1139 RT_ADDREF(rt); 1140 } 1141 RT_UNLOCK(rt); 1142 break; 1143 default: 1144 error = EOPNOTSUPP; 1145 } 1146 bad: 1147 if (needlock) 1148 RADIX_NODE_HEAD_UNLOCK(rnh); 1149 return (error); 1150 #undef senderr 1151 } 1152 1153 #undef dst 1154 #undef gateway 1155 #undef netmask 1156 #undef ifaaddr 1157 #undef ifpaddr 1158 #undef flags 1159 1160 int 1161 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate) 1162 { 1163 /* XXX dst may be overwritten, can we move this to below */ 1164 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate); 1165 #ifdef INVARIANTS 1166 struct radix_node_head *rnh; 1167 1168 rnh = rt_tables_get_rnh(rt->rt_fibnum, dst->sa_family); 1169 #endif 1170 1171 RT_LOCK_ASSERT(rt); 1172 RADIX_NODE_HEAD_LOCK_ASSERT(rnh); 1173 1174 /* 1175 * Prepare to store the gateway in rt->rt_gateway. 1176 * Both dst and gateway are stored one after the other in the same 1177 * malloc'd chunk. If we have room, we can reuse the old buffer, 1178 * rt_gateway already points to the right place. 1179 * Otherwise, malloc a new block and update the 'dst' address. 1180 */ 1181 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) { 1182 caddr_t new; 1183 1184 R_Malloc(new, caddr_t, dlen + glen); 1185 if (new == NULL) 1186 return ENOBUFS; 1187 /* 1188 * XXX note, we copy from *dst and not *rt_key(rt) because 1189 * rt_setgate() can be called to initialize a newly 1190 * allocated route entry, in which case rt_key(rt) == NULL 1191 * (and also rt->rt_gateway == NULL). 1192 * Free()/free() handle a NULL argument just fine. 1193 */ 1194 bcopy(dst, new, dlen); 1195 Free(rt_key(rt)); /* free old block, if any */ 1196 rt_key(rt) = (struct sockaddr *)new; 1197 rt->rt_gateway = (struct sockaddr *)(new + dlen); 1198 } 1199 1200 /* 1201 * Copy the new gateway value into the memory chunk. 1202 */ 1203 bcopy(gate, rt->rt_gateway, glen); 1204 1205 return (0); 1206 } 1207 1208 static void 1209 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask) 1210 { 1211 register u_char *cp1 = (u_char *)src; 1212 register u_char *cp2 = (u_char *)dst; 1213 register u_char *cp3 = (u_char *)netmask; 1214 u_char *cplim = cp2 + *cp3; 1215 u_char *cplim2 = cp2 + *cp1; 1216 1217 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */ 1218 cp3 += 2; 1219 if (cplim > cplim2) 1220 cplim = cplim2; 1221 while (cp2 < cplim) 1222 *cp2++ = *cp1++ & *cp3++; 1223 if (cp2 < cplim2) 1224 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2)); 1225 } 1226 1227 /* 1228 * Set up a routing table entry, normally 1229 * for an interface. 1230 */ 1231 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */ 1232 static inline int 1233 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum) 1234 { 1235 struct sockaddr *dst; 1236 struct sockaddr *netmask; 1237 struct rtentry *rt = NULL; 1238 struct rt_addrinfo info; 1239 int error = 0; 1240 int startfib, endfib; 1241 char tempbuf[_SOCKADDR_TMPSIZE]; 1242 int didwork = 0; 1243 int a_failure = 0; 1244 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 1245 1246 if (flags & RTF_HOST) { 1247 dst = ifa->ifa_dstaddr; 1248 netmask = NULL; 1249 } else { 1250 dst = ifa->ifa_addr; 1251 netmask = ifa->ifa_netmask; 1252 } 1253 if ( dst->sa_family != AF_INET) 1254 fibnum = 0; 1255 if (fibnum == -1) { 1256 if (rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD) { 1257 startfib = endfib = curthread->td_proc->p_fibnum; 1258 } else { 1259 startfib = 0; 1260 endfib = rt_numfibs - 1; 1261 } 1262 } else { 1263 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum")); 1264 startfib = fibnum; 1265 endfib = fibnum; 1266 } 1267 if (dst->sa_len == 0) 1268 return(EINVAL); 1269 1270 /* 1271 * If it's a delete, check that if it exists, 1272 * it's on the correct interface or we might scrub 1273 * a route to another ifa which would 1274 * be confusing at best and possibly worse. 1275 */ 1276 if (cmd == RTM_DELETE) { 1277 /* 1278 * It's a delete, so it should already exist.. 1279 * If it's a net, mask off the host bits 1280 * (Assuming we have a mask) 1281 * XXX this is kinda inet specific.. 1282 */ 1283 if (netmask != NULL) { 1284 rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask); 1285 dst = (struct sockaddr *)tempbuf; 1286 } 1287 } 1288 /* 1289 * Now go through all the requested tables (fibs) and do the 1290 * requested action. Realistically, this will either be fib 0 1291 * for protocols that don't do multiple tables or all the 1292 * tables for those that do. XXX For this version only AF_INET. 1293 * When that changes code should be refactored to protocol 1294 * independent parts and protocol dependent parts. 1295 */ 1296 for ( fibnum = startfib; fibnum <= endfib; fibnum++) { 1297 if (cmd == RTM_DELETE) { 1298 struct radix_node_head *rnh; 1299 struct radix_node *rn; 1300 /* 1301 * Look up an rtentry that is in the routing tree and 1302 * contains the correct info. 1303 */ 1304 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 1305 if (rnh == NULL) 1306 /* this table doesn't exist but others might */ 1307 continue; 1308 RADIX_NODE_HEAD_LOCK(rnh); 1309 #ifdef RADIX_MPATH 1310 if (rn_mpath_capable(rnh)) { 1311 1312 rn = rnh->rnh_matchaddr(dst, rnh); 1313 if (rn == NULL) 1314 error = ESRCH; 1315 else { 1316 rt = RNTORT(rn); 1317 /* 1318 * for interface route the 1319 * rt->rt_gateway is sockaddr_intf 1320 * for cloning ARP entries, so 1321 * rt_mpath_matchgate must use the 1322 * interface address 1323 */ 1324 rt = rt_mpath_matchgate(rt, 1325 ifa->ifa_addr); 1326 if (!rt) 1327 error = ESRCH; 1328 } 1329 } 1330 else 1331 #endif 1332 rn = rnh->rnh_lookup(dst, netmask, rnh); 1333 error = (rn == NULL || 1334 (rn->rn_flags & RNF_ROOT) || 1335 RNTORT(rn)->rt_ifa != ifa || 1336 !sa_equal((struct sockaddr *)rn->rn_key, dst)); 1337 RADIX_NODE_HEAD_UNLOCK(rnh); 1338 if (error) { 1339 /* this is only an error if bad on ALL tables */ 1340 continue; 1341 } 1342 } 1343 /* 1344 * Do the actual request 1345 */ 1346 bzero((caddr_t)&info, sizeof(info)); 1347 info.rti_ifa = ifa; 1348 info.rti_flags = flags | ifa->ifa_flags; 1349 info.rti_info[RTAX_DST] = dst; 1350 /* 1351 * doing this for compatibility reasons 1352 */ 1353 if (cmd == RTM_ADD) 1354 info.rti_info[RTAX_GATEWAY] = 1355 (struct sockaddr *)&null_sdl; 1356 else 1357 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr; 1358 info.rti_info[RTAX_NETMASK] = netmask; 1359 error = rtrequest1_fib(cmd, &info, &rt, fibnum); 1360 if (error == 0 && rt != NULL) { 1361 /* 1362 * notify any listening routing agents of the change 1363 */ 1364 RT_LOCK(rt); 1365 #ifdef RADIX_MPATH 1366 /* 1367 * in case address alias finds the first address 1368 * e.g. ifconfig bge0 192.103.54.246/24 1369 * e.g. ifconfig bge0 192.103.54.247/24 1370 * the address set in the route is 192.103.54.246 1371 * so we need to replace it with 192.103.54.247 1372 */ 1373 if (memcmp(rt->rt_ifa->ifa_addr, 1374 ifa->ifa_addr, ifa->ifa_addr->sa_len)) { 1375 IFAFREE(rt->rt_ifa); 1376 IFAREF(ifa); 1377 rt->rt_ifp = ifa->ifa_ifp; 1378 rt->rt_ifa = ifa; 1379 } 1380 #endif 1381 /* 1382 * doing this for compatibility reasons 1383 */ 1384 if (cmd == RTM_ADD) { 1385 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type = 1386 rt->rt_ifp->if_type; 1387 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index = 1388 rt->rt_ifp->if_index; 1389 } 1390 rt_newaddrmsg(cmd, ifa, error, rt); 1391 if (cmd == RTM_DELETE) { 1392 /* 1393 * If we are deleting, and we found an entry, 1394 * then it's been removed from the tree.. 1395 * now throw it away. 1396 */ 1397 RTFREE_LOCKED(rt); 1398 } else { 1399 if (cmd == RTM_ADD) { 1400 /* 1401 * We just wanted to add it.. 1402 * we don't actually need a reference. 1403 */ 1404 RT_REMREF(rt); 1405 } 1406 RT_UNLOCK(rt); 1407 } 1408 didwork = 1; 1409 } 1410 if (error) 1411 a_failure = error; 1412 } 1413 if (cmd == RTM_DELETE) { 1414 if (didwork) { 1415 error = 0; 1416 } else { 1417 /* we only give an error if it wasn't in any table */ 1418 error = ((flags & RTF_HOST) ? 1419 EHOSTUNREACH : ENETUNREACH); 1420 } 1421 } else { 1422 if (a_failure) { 1423 /* return an error if any of them failed */ 1424 error = a_failure; 1425 } 1426 } 1427 return (error); 1428 } 1429 1430 /* special one for inet internal use. may not use. */ 1431 int 1432 rtinit_fib(struct ifaddr *ifa, int cmd, int flags) 1433 { 1434 return (rtinit1(ifa, cmd, flags, -1)); 1435 } 1436 1437 /* 1438 * Set up a routing table entry, normally 1439 * for an interface. 1440 */ 1441 int 1442 rtinit(struct ifaddr *ifa, int cmd, int flags) 1443 { 1444 struct sockaddr *dst; 1445 int fib = 0; 1446 1447 if (flags & RTF_HOST) { 1448 dst = ifa->ifa_dstaddr; 1449 } else { 1450 dst = ifa->ifa_addr; 1451 } 1452 1453 if (dst->sa_family == AF_INET) 1454 fib = -1; 1455 return (rtinit1(ifa, cmd, flags, fib)); 1456 } 1457 1458 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */ 1459 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0); 1460