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