1 /* $OpenBSD: route.c,v 1.353 2017/03/02 03:09:50 renato Exp $ */ 2 /* $NetBSD: route.c,v 1.14 1996/02/13 22:00:46 christos Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1980, 1986, 1991, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)route.c 8.2 (Berkeley) 11/15/93 62 */ 63 64 /* 65 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 66 * 67 * NRL grants permission for redistribution and use in source and binary 68 * forms, with or without modification, of the software and documentation 69 * created at NRL provided that the following conditions are met: 70 * 71 * 1. Redistributions of source code must retain the above copyright 72 * notice, this list of conditions and the following disclaimer. 73 * 2. Redistributions in binary form must reproduce the above copyright 74 * notice, this list of conditions and the following disclaimer in the 75 * documentation and/or other materials provided with the distribution. 76 * 3. All advertising materials mentioning features or use of this software 77 * must display the following acknowledgements: 78 * This product includes software developed by the University of 79 * California, Berkeley and its contributors. 80 * This product includes software developed at the Information 81 * Technology Division, US Naval Research Laboratory. 82 * 4. Neither the name of the NRL nor the names of its contributors 83 * may be used to endorse or promote products derived from this software 84 * without specific prior written permission. 85 * 86 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 87 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 88 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 89 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 90 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 91 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 92 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 93 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 94 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 95 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 96 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 97 * 98 * The views and conclusions contained in the software and documentation 99 * are those of the authors and should not be interpreted as representing 100 * official policies, either expressed or implied, of the US Naval 101 * Research Laboratory (NRL). 102 */ 103 104 #include <sys/param.h> 105 #include <sys/systm.h> 106 #include <sys/mbuf.h> 107 #include <sys/socket.h> 108 #include <sys/socketvar.h> 109 #include <sys/timeout.h> 110 #include <sys/domain.h> 111 #include <sys/protosw.h> 112 #include <sys/ioctl.h> 113 #include <sys/kernel.h> 114 #include <sys/queue.h> 115 #include <sys/pool.h> 116 #include <sys/atomic.h> 117 118 #include <net/if.h> 119 #include <net/if_var.h> 120 #include <net/if_dl.h> 121 #include <net/route.h> 122 123 #include <netinet/in.h> 124 #include <netinet/ip_var.h> 125 #include <netinet/in_var.h> 126 127 #ifdef INET6 128 #include <netinet/ip6.h> 129 #include <netinet6/ip6_var.h> 130 #include <netinet6/in6_var.h> 131 #endif 132 133 #ifdef MPLS 134 #include <netmpls/mpls.h> 135 #endif 136 137 #ifdef IPSEC 138 #include <netinet/ip_ipsp.h> 139 #include <net/if_enc.h> 140 #endif 141 142 #ifdef BFD 143 #include <net/bfd.h> 144 #endif 145 146 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 147 148 /* Give some jitter to hash, to avoid synchronization between routers. */ 149 static uint32_t rt_hashjitter; 150 151 extern unsigned int rtmap_limit; 152 153 struct cpumem * rtcounters; 154 int rttrash; /* routes not in table but not freed */ 155 int ifatrash; /* ifas not in ifp list but not free */ 156 157 struct pool rtentry_pool; /* pool for rtentry structures */ 158 struct pool rttimer_pool; /* pool for rttimer structures */ 159 160 void rt_timer_init(void); 161 int rt_setgwroute(struct rtentry *, u_int); 162 void rt_putgwroute(struct rtentry *); 163 int rtflushclone1(struct rtentry *, void *, u_int); 164 void rtflushclone(unsigned int, struct rtentry *); 165 int rt_ifa_purge_walker(struct rtentry *, void *, unsigned int); 166 struct rtentry *rt_match(struct sockaddr *, uint32_t *, int, unsigned int); 167 struct sockaddr *rt_plentosa(sa_family_t, int, struct sockaddr_in6 *); 168 169 struct ifaddr *ifa_ifwithroute(int, struct sockaddr *, struct sockaddr *, 170 u_int); 171 int rtrequest_delete(struct rt_addrinfo *, u_int8_t, struct ifnet *, 172 struct rtentry **, u_int); 173 174 #ifdef DDB 175 void db_print_sa(struct sockaddr *); 176 void db_print_ifa(struct ifaddr *); 177 int db_show_rtentry(struct rtentry *, void *, unsigned int); 178 #endif 179 180 #define LABELID_MAX 50000 181 182 struct rt_label { 183 TAILQ_ENTRY(rt_label) rtl_entry; 184 char rtl_name[RTLABEL_LEN]; 185 u_int16_t rtl_id; 186 int rtl_ref; 187 }; 188 189 TAILQ_HEAD(rt_labels, rt_label) rt_labels = TAILQ_HEAD_INITIALIZER(rt_labels); 190 191 void 192 route_init(void) 193 { 194 rtcounters = counters_alloc(rts_ncounters); 195 196 pool_init(&rtentry_pool, sizeof(struct rtentry), 0, IPL_SOFTNET, 0, 197 "rtentry", NULL); 198 199 while (rt_hashjitter == 0) 200 rt_hashjitter = arc4random(); 201 202 #ifdef BFD 203 bfdinit(); 204 #endif 205 } 206 207 /* 208 * Returns 1 if the (cached) ``rt'' entry is still valid, 0 otherwise. 209 */ 210 int 211 rtisvalid(struct rtentry *rt) 212 { 213 if (rt == NULL) 214 return (0); 215 216 if (!ISSET(rt->rt_flags, RTF_UP)) 217 return (0); 218 219 if (ISSET(rt->rt_flags, RTF_GATEWAY)) { 220 KASSERT(rt->rt_gwroute != NULL); 221 KASSERT(!ISSET(rt->rt_gwroute->rt_flags, RTF_GATEWAY)); 222 if (!ISSET(rt->rt_gwroute->rt_flags, RTF_UP)) 223 return (0); 224 } 225 226 return (1); 227 } 228 229 /* 230 * Do the actual lookup for rtalloc(9), do not use directly! 231 * 232 * Return the best matching entry for the destination ``dst''. 233 * 234 * "RT_RESOLVE" means that a corresponding L2 entry should 235 * be added to the routing table and resolved (via ARP or 236 * NDP), if it does not exist. 237 */ 238 struct rtentry * 239 rt_match(struct sockaddr *dst, uint32_t *src, int flags, unsigned int tableid) 240 { 241 struct rtentry *rt0, *rt = NULL; 242 int error = 0; 243 244 splsoftassert(IPL_SOFTNET); 245 246 rt = rtable_match(tableid, dst, src); 247 if (rt != NULL) { 248 if ((rt->rt_flags & RTF_CLONING) && ISSET(flags, RT_RESOLVE)) { 249 struct rt_addrinfo info; 250 251 rt0 = rt; 252 253 memset(&info, 0, sizeof(info)); 254 info.rti_info[RTAX_DST] = dst; 255 256 KERNEL_LOCK(); 257 /* 258 * The priority of cloned route should be different 259 * to avoid conflict with /32 cloning routes. 260 * 261 * It should also be higher to let the ARP layer find 262 * cloned routes instead of the cloning one. 263 */ 264 error = rtrequest(RTM_RESOLVE, &info, 265 rt->rt_priority - 1, &rt, tableid); 266 if (error) { 267 rt_missmsg(RTM_MISS, &info, 0, RTP_NONE, 0, 268 error, tableid); 269 } else { 270 /* Inform listeners of the new route */ 271 rt_sendmsg(rt, RTM_ADD, tableid); 272 rtfree(rt0); 273 } 274 KERNEL_UNLOCK(); 275 } 276 rt->rt_use++; 277 } else 278 rtstat_inc(rts_unreach); 279 return (rt); 280 } 281 282 #ifndef SMALL_KERNEL 283 /* 284 * Originated from bridge_hash() in if_bridge.c 285 */ 286 #define mix(a, b, c) do { \ 287 a -= b; a -= c; a ^= (c >> 13); \ 288 b -= c; b -= a; b ^= (a << 8); \ 289 c -= a; c -= b; c ^= (b >> 13); \ 290 a -= b; a -= c; a ^= (c >> 12); \ 291 b -= c; b -= a; b ^= (a << 16); \ 292 c -= a; c -= b; c ^= (b >> 5); \ 293 a -= b; a -= c; a ^= (c >> 3); \ 294 b -= c; b -= a; b ^= (a << 10); \ 295 c -= a; c -= b; c ^= (b >> 15); \ 296 } while (0) 297 298 int 299 rt_hash(struct rtentry *rt, struct sockaddr *dst, uint32_t *src) 300 { 301 uint32_t a, b, c; 302 303 if (src == NULL || !rtisvalid(rt) || !ISSET(rt->rt_flags, RTF_MPATH)) 304 return (-1); 305 306 a = b = 0x9e3779b9; 307 c = rt_hashjitter; 308 309 switch (dst->sa_family) { 310 case AF_INET: 311 { 312 struct sockaddr_in *sin; 313 314 if (!ipmultipath) 315 return (-1); 316 317 sin = satosin(dst); 318 a += sin->sin_addr.s_addr; 319 b += (src != NULL) ? src[0] : 0; 320 mix(a, b, c); 321 break; 322 } 323 #ifdef INET6 324 case AF_INET6: 325 { 326 struct sockaddr_in6 *sin6; 327 328 if (!ip6_multipath) 329 return (-1); 330 331 sin6 = satosin6(dst); 332 a += sin6->sin6_addr.s6_addr32[0]; 333 b += sin6->sin6_addr.s6_addr32[2]; 334 c += (src != NULL) ? src[0] : 0; 335 mix(a, b, c); 336 a += sin6->sin6_addr.s6_addr32[1]; 337 b += sin6->sin6_addr.s6_addr32[3]; 338 c += (src != NULL) ? src[1] : 0; 339 mix(a, b, c); 340 a += sin6->sin6_addr.s6_addr32[2]; 341 b += sin6->sin6_addr.s6_addr32[1]; 342 c += (src != NULL) ? src[2] : 0; 343 mix(a, b, c); 344 a += sin6->sin6_addr.s6_addr32[3]; 345 b += sin6->sin6_addr.s6_addr32[0]; 346 c += (src != NULL) ? src[3] : 0; 347 mix(a, b, c); 348 break; 349 } 350 #endif /* INET6 */ 351 } 352 353 return (c & 0xffff); 354 } 355 356 /* 357 * Allocate a route, potentially using multipath to select the peer. 358 */ 359 struct rtentry * 360 rtalloc_mpath(struct sockaddr *dst, uint32_t *src, unsigned int rtableid) 361 { 362 return (rt_match(dst, src, RT_RESOLVE, rtableid)); 363 } 364 #endif /* SMALL_KERNEL */ 365 366 /* 367 * Look in the routing table for the best matching entry for 368 * ``dst''. 369 * 370 * If a route with a gateway is found and its next hop is no 371 * longer valid, try to cache it. 372 */ 373 struct rtentry * 374 rtalloc(struct sockaddr *dst, int flags, unsigned int rtableid) 375 { 376 return (rt_match(dst, NULL, flags, rtableid)); 377 } 378 379 /* 380 * Cache the route entry corresponding to a reachable next hop in 381 * the gateway entry ``rt''. 382 */ 383 int 384 rt_setgwroute(struct rtentry *rt, u_int rtableid) 385 { 386 struct rtentry *nhrt; 387 388 KERNEL_ASSERT_LOCKED(); 389 390 KASSERT(ISSET(rt->rt_flags, RTF_GATEWAY)); 391 392 /* If we cannot find a valid next hop bail. */ 393 nhrt = rt_match(rt->rt_gateway, NULL, RT_RESOLVE, rtable_l2(rtableid)); 394 if (nhrt == NULL) 395 return (ENOENT); 396 397 /* Next hop entry must be on the same interface. */ 398 if (nhrt->rt_ifidx != rt->rt_ifidx) { 399 rtfree(nhrt); 400 return (EHOSTUNREACH); 401 } 402 403 /* 404 * Next hop must be reachable, this also prevents rtentry 405 * loops for example when rt->rt_gwroute points to rt. 406 */ 407 if (ISSET(nhrt->rt_flags, RTF_CLONING|RTF_GATEWAY)) { 408 rtfree(nhrt); 409 return (ENETUNREACH); 410 } 411 412 /* Next hop is valid so remove possible old cache. */ 413 rt_putgwroute(rt); 414 KASSERT(rt->rt_gwroute == NULL); 415 416 /* 417 * If the MTU of next hop is 0, this will reset the MTU of the 418 * route to run PMTUD again from scratch. 419 */ 420 if (!ISSET(rt->rt_locks, RTV_MTU) && (rt->rt_mtu > nhrt->rt_mtu)) 421 rt->rt_mtu = nhrt->rt_mtu; 422 423 /* 424 * To avoid reference counting problems when writting link-layer 425 * addresses in an outgoing packet, we ensure that the lifetime 426 * of a cached entry is greater that the bigger lifetime of the 427 * gateway entries it is pointed by. 428 */ 429 nhrt->rt_flags |= RTF_CACHED; 430 nhrt->rt_cachecnt++; 431 432 rt->rt_gwroute = nhrt; 433 434 return (0); 435 } 436 437 /* 438 * Invalidate the cached route entry of the gateway entry ``rt''. 439 */ 440 void 441 rt_putgwroute(struct rtentry *rt) 442 { 443 struct rtentry *nhrt = rt->rt_gwroute; 444 445 KERNEL_ASSERT_LOCKED(); 446 447 if (!ISSET(rt->rt_flags, RTF_GATEWAY) || nhrt == NULL) 448 return; 449 450 KASSERT(ISSET(nhrt->rt_flags, RTF_CACHED)); 451 KASSERT(nhrt->rt_cachecnt > 0); 452 453 --nhrt->rt_cachecnt; 454 if (nhrt->rt_cachecnt == 0) 455 nhrt->rt_flags &= ~RTF_CACHED; 456 457 rtfree(rt->rt_gwroute); 458 rt->rt_gwroute = NULL; 459 } 460 461 void 462 rtref(struct rtentry *rt) 463 { 464 atomic_inc_int(&rt->rt_refcnt); 465 } 466 467 void 468 rtfree(struct rtentry *rt) 469 { 470 int refcnt; 471 472 if (rt == NULL) 473 return; 474 475 refcnt = (int)atomic_dec_int_nv(&rt->rt_refcnt); 476 if (refcnt <= 0) { 477 KASSERT(!ISSET(rt->rt_flags, RTF_UP)); 478 KASSERT(!RT_ROOT(rt)); 479 atomic_dec_int(&rttrash); 480 if (refcnt < 0) { 481 printf("rtfree: %p not freed (neg refs)\n", rt); 482 return; 483 } 484 485 KERNEL_LOCK(); 486 rt_timer_remove_all(rt); 487 ifafree(rt->rt_ifa); 488 rtlabel_unref(rt->rt_labelid); 489 #ifdef MPLS 490 if (rt->rt_flags & RTF_MPLS) 491 free(rt->rt_llinfo, M_TEMP, sizeof(struct rt_mpls)); 492 #endif 493 free(rt->rt_gateway, M_RTABLE, ROUNDUP(rt->rt_gateway->sa_len)); 494 free(rt_key(rt), M_RTABLE, rt_key(rt)->sa_len); 495 KERNEL_UNLOCK(); 496 497 pool_put(&rtentry_pool, rt); 498 } 499 } 500 501 void 502 rt_sendmsg(struct rtentry *rt, int cmd, u_int rtableid) 503 { 504 struct rt_addrinfo info; 505 struct ifnet *ifp; 506 struct sockaddr_rtlabel sa_rl; 507 struct sockaddr_in6 sa_mask; 508 509 memset(&info, 0, sizeof(info)); 510 info.rti_info[RTAX_DST] = rt_key(rt); 511 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 512 if (!ISSET(rt->rt_flags, RTF_HOST)) 513 info.rti_info[RTAX_NETMASK] = rt_plen2mask(rt, &sa_mask); 514 info.rti_info[RTAX_LABEL] = rtlabel_id2sa(rt->rt_labelid, &sa_rl); 515 ifp = if_get(rt->rt_ifidx); 516 if (ifp != NULL) { 517 info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl); 518 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 519 } 520 521 rt_missmsg(cmd, &info, rt->rt_flags, rt->rt_priority, rt->rt_ifidx, 0, 522 rtableid); 523 if_put(ifp); 524 } 525 526 void 527 ifafree(struct ifaddr *ifa) 528 { 529 if (ifa == NULL) 530 panic("ifafree"); 531 if (ifa->ifa_refcnt == 0) { 532 ifatrash--; 533 free(ifa, M_IFADDR, 0); 534 } else 535 ifa->ifa_refcnt--; 536 } 537 538 /* 539 * Force a routing table entry to the specified 540 * destination to go through the given gateway. 541 * Normally called as a result of a routing redirect 542 * message from the network layer. 543 */ 544 void 545 rtredirect(struct sockaddr *dst, struct sockaddr *gateway, 546 struct sockaddr *src, struct rtentry **rtp, unsigned int rdomain) 547 { 548 struct rtentry *rt; 549 int error = 0; 550 enum rtstat_counters stat = rts_ncounters; 551 struct rt_addrinfo info; 552 struct ifaddr *ifa; 553 unsigned int ifidx = 0; 554 int flags = RTF_GATEWAY|RTF_HOST; 555 uint8_t prio = RTP_NONE; 556 557 NET_ASSERT_LOCKED(); 558 559 /* verify the gateway is directly reachable */ 560 rt = rtalloc(gateway, 0, rdomain); 561 if (!rtisvalid(rt) || ISSET(rt->rt_flags, RTF_GATEWAY)) { 562 rtfree(rt); 563 error = ENETUNREACH; 564 goto out; 565 } 566 ifidx = rt->rt_ifidx; 567 ifa = rt->rt_ifa; 568 rtfree(rt); 569 rt = NULL; 570 571 rt = rtable_lookup(rdomain, dst, NULL, NULL, RTP_ANY); 572 /* 573 * If the redirect isn't from our current router for this dst, 574 * it's either old or wrong. If it redirects us to ourselves, 575 * we have a routing loop, perhaps as a result of an interface 576 * going down recently. 577 */ 578 #define equal(a1, a2) \ 579 ((a1)->sa_len == (a2)->sa_len && \ 580 bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0) 581 if (rt != NULL && (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) 582 error = EINVAL; 583 else if (ifa_ifwithaddr(gateway, rdomain) != NULL || 584 (gateway->sa_family = AF_INET && 585 in_broadcast(satosin(gateway)->sin_addr, rdomain))) 586 error = EHOSTUNREACH; 587 if (error) 588 goto done; 589 /* 590 * Create a new entry if we just got back a wildcard entry 591 * or the lookup failed. This is necessary for hosts 592 * which use routing redirects generated by smart gateways 593 * to dynamically build the routing tables. 594 */ 595 if (rt == NULL) 596 goto create; 597 /* 598 * Don't listen to the redirect if it's 599 * for a route to an interface. 600 */ 601 if (ISSET(rt->rt_flags, RTF_GATEWAY)) { 602 if (!ISSET(rt->rt_flags, RTF_HOST)) { 603 /* 604 * Changing from route to net => route to host. 605 * Create new route, rather than smashing route to net. 606 */ 607 create: 608 rtfree(rt); 609 flags |= RTF_DYNAMIC; 610 bzero(&info, sizeof(info)); 611 info.rti_info[RTAX_DST] = dst; 612 info.rti_info[RTAX_GATEWAY] = gateway; 613 info.rti_ifa = ifa; 614 info.rti_flags = flags; 615 rt = NULL; 616 error = rtrequest(RTM_ADD, &info, RTP_DEFAULT, &rt, 617 rdomain); 618 if (error == 0) { 619 flags = rt->rt_flags; 620 prio = rt->rt_priority; 621 } 622 stat = rts_dynamic; 623 } else { 624 /* 625 * Smash the current notion of the gateway to 626 * this destination. Should check about netmask!!! 627 */ 628 rt->rt_flags |= RTF_MODIFIED; 629 flags |= RTF_MODIFIED; 630 prio = rt->rt_priority; 631 stat = rts_newgateway; 632 rt_setgate(rt, gateway, rdomain); 633 } 634 } else 635 error = EHOSTUNREACH; 636 done: 637 if (rt) { 638 if (rtp && !error) 639 *rtp = rt; 640 else 641 rtfree(rt); 642 } 643 out: 644 if (error) 645 rtstat_inc(rts_badredirect); 646 else if (stat != rts_ncounters) 647 rtstat_inc(stat); 648 bzero((caddr_t)&info, sizeof(info)); 649 info.rti_info[RTAX_DST] = dst; 650 info.rti_info[RTAX_GATEWAY] = gateway; 651 info.rti_info[RTAX_AUTHOR] = src; 652 rt_missmsg(RTM_REDIRECT, &info, flags, prio, ifidx, error, rdomain); 653 } 654 655 /* 656 * Delete a route and generate a message 657 */ 658 int 659 rtdeletemsg(struct rtentry *rt, struct ifnet *ifp, u_int tableid) 660 { 661 int error; 662 struct rt_addrinfo info; 663 unsigned int ifidx; 664 struct sockaddr_in6 sa_mask; 665 666 KASSERT(rt->rt_ifidx == ifp->if_index); 667 668 /* 669 * Request the new route so that the entry is not actually 670 * deleted. That will allow the information being reported to 671 * be accurate (and consistent with route_output()). 672 */ 673 bzero((caddr_t)&info, sizeof(info)); 674 info.rti_info[RTAX_DST] = rt_key(rt); 675 if (!ISSET(rt->rt_flags, RTF_HOST)) 676 info.rti_info[RTAX_NETMASK] = rt_plen2mask(rt, &sa_mask); 677 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 678 info.rti_flags = rt->rt_flags; 679 ifidx = rt->rt_ifidx; 680 error = rtrequest_delete(&info, rt->rt_priority, ifp, &rt, tableid); 681 rt_missmsg(RTM_DELETE, &info, info.rti_flags, rt->rt_priority, ifidx, 682 error, tableid); 683 if (error == 0) 684 rtfree(rt); 685 return (error); 686 } 687 688 static inline int 689 rtequal(struct rtentry *a, struct rtentry *b) 690 { 691 if (a == b) 692 return 1; 693 694 if (memcmp(rt_key(a), rt_key(b), rt_key(a)->sa_len) == 0 && 695 rt_plen(a) == rt_plen(b)) 696 return 1; 697 else 698 return 0; 699 } 700 701 int 702 rtflushclone1(struct rtentry *rt, void *arg, u_int id) 703 { 704 struct rtentry *parent = arg; 705 struct ifnet *ifp; 706 int error; 707 708 ifp = if_get(rt->rt_ifidx); 709 710 /* 711 * This happens when an interface with a RTF_CLONING route is 712 * being detached. In this case it's safe to bail because all 713 * the routes are being purged by rt_ifa_purge(). 714 */ 715 if (ifp == NULL) 716 return 0; 717 718 if (ISSET(rt->rt_flags, RTF_CLONED) && rtequal(rt->rt_parent, parent)) { 719 error = rtdeletemsg(rt, ifp, id); 720 if (error == 0) 721 error = EAGAIN; 722 } else 723 error = 0; 724 725 if_put(ifp); 726 return error; 727 } 728 729 void 730 rtflushclone(unsigned int rtableid, struct rtentry *parent) 731 { 732 733 #ifdef DIAGNOSTIC 734 if (!parent || (parent->rt_flags & RTF_CLONING) == 0) 735 panic("rtflushclone: called with a non-cloning route"); 736 #endif 737 rtable_walk(rtableid, rt_key(parent)->sa_family, rtflushclone1, parent); 738 } 739 740 struct ifaddr * 741 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway, 742 u_int rtableid) 743 { 744 struct ifaddr *ifa; 745 746 if ((flags & RTF_GATEWAY) == 0) { 747 /* 748 * If we are adding a route to an interface, 749 * and the interface is a pt to pt link 750 * we should search for the destination 751 * as our clue to the interface. Otherwise 752 * we can use the local address. 753 */ 754 ifa = NULL; 755 if (flags & RTF_HOST) 756 ifa = ifa_ifwithdstaddr(dst, rtableid); 757 if (ifa == NULL) 758 ifa = ifa_ifwithaddr(gateway, rtableid); 759 } else { 760 /* 761 * If we are adding a route to a remote net 762 * or host, the gateway may still be on the 763 * other end of a pt to pt link. 764 */ 765 ifa = ifa_ifwithdstaddr(gateway, rtableid); 766 } 767 if (ifa == NULL) { 768 if (gateway->sa_family == AF_LINK) { 769 struct sockaddr_dl *sdl = satosdl(gateway); 770 struct ifnet *ifp = if_get(sdl->sdl_index); 771 772 if (ifp != NULL) 773 ifa = ifaof_ifpforaddr(dst, ifp); 774 if_put(ifp); 775 } else { 776 struct rtentry *rt; 777 778 rt = rtalloc(gateway, RT_RESOLVE, rtableid); 779 if (rt != NULL) 780 ifa = rt->rt_ifa; 781 rtfree(rt); 782 } 783 } 784 if (ifa == NULL) 785 return (NULL); 786 if (ifa->ifa_addr->sa_family != dst->sa_family) { 787 struct ifaddr *oifa = ifa; 788 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 789 if (ifa == NULL) 790 ifa = oifa; 791 } 792 return (ifa); 793 } 794 795 int 796 rt_getifa(struct rt_addrinfo *info, u_int rtid) 797 { 798 struct ifnet *ifp = NULL; 799 800 /* 801 * ifp may be specified by sockaddr_dl when protocol address 802 * is ambiguous 803 */ 804 if (info->rti_info[RTAX_IFP] != NULL) { 805 struct sockaddr_dl *sdl; 806 807 sdl = satosdl(info->rti_info[RTAX_IFP]); 808 ifp = if_get(sdl->sdl_index); 809 } 810 811 #ifdef IPSEC 812 /* 813 * If the destination is a PF_KEY address, we'll look 814 * for the existence of a encap interface number or address 815 * in the options list of the gateway. By default, we'll return 816 * enc0. 817 */ 818 if (info->rti_info[RTAX_DST] && 819 info->rti_info[RTAX_DST]->sa_family == PF_KEY) 820 info->rti_ifa = enc_getifa(rtid, 0); 821 #endif 822 823 if (info->rti_ifa == NULL && info->rti_info[RTAX_IFA] != NULL) 824 info->rti_ifa = ifa_ifwithaddr(info->rti_info[RTAX_IFA], rtid); 825 826 if (info->rti_ifa == NULL) { 827 struct sockaddr *sa; 828 829 if ((sa = info->rti_info[RTAX_IFA]) == NULL) 830 if ((sa = info->rti_info[RTAX_GATEWAY]) == NULL) 831 sa = info->rti_info[RTAX_DST]; 832 833 if (sa != NULL && ifp != NULL) 834 info->rti_ifa = ifaof_ifpforaddr(sa, ifp); 835 else if (info->rti_info[RTAX_DST] != NULL && 836 info->rti_info[RTAX_GATEWAY] != NULL) 837 info->rti_ifa = ifa_ifwithroute(info->rti_flags, 838 info->rti_info[RTAX_DST], 839 info->rti_info[RTAX_GATEWAY], 840 rtid); 841 else if (sa != NULL) 842 info->rti_ifa = ifa_ifwithroute(info->rti_flags, 843 sa, sa, rtid); 844 } 845 846 if_put(ifp); 847 848 if (info->rti_ifa == NULL) 849 return (ENETUNREACH); 850 851 return (0); 852 } 853 854 int 855 rtrequest_delete(struct rt_addrinfo *info, u_int8_t prio, struct ifnet *ifp, 856 struct rtentry **ret_nrt, u_int tableid) 857 { 858 struct rtentry *rt; 859 int error; 860 861 splsoftassert(IPL_SOFTNET); 862 863 if (!rtable_exists(tableid)) 864 return (EAFNOSUPPORT); 865 rt = rtable_lookup(tableid, info->rti_info[RTAX_DST], 866 info->rti_info[RTAX_NETMASK], info->rti_info[RTAX_GATEWAY], prio); 867 if (rt == NULL) 868 return (ESRCH); 869 870 /* Make sure that's the route the caller want to delete. */ 871 if (ifp != NULL && ifp->if_index != rt->rt_ifidx) { 872 rtfree(rt); 873 return (ESRCH); 874 } 875 876 #ifndef SMALL_KERNEL 877 /* 878 * If we got multipath routes, we require users to specify 879 * a matching gateway. 880 */ 881 if ((rt->rt_flags & RTF_MPATH) && 882 info->rti_info[RTAX_GATEWAY] == NULL) { 883 rtfree(rt); 884 return (ESRCH); 885 } 886 #endif 887 888 #ifdef BFD 889 if (ISSET(rt->rt_flags, RTF_BFD)) 890 bfdclear(rt); 891 #endif 892 893 error = rtable_delete(tableid, info->rti_info[RTAX_DST], 894 info->rti_info[RTAX_NETMASK], rt); 895 if (error != 0) { 896 rtfree(rt); 897 return (ESRCH); 898 } 899 900 /* Release next hop cache before flushing cloned entries. */ 901 rt_putgwroute(rt); 902 903 /* Clean up any cloned children. */ 904 if (ISSET(rt->rt_flags, RTF_CLONING)) 905 rtflushclone(tableid, rt); 906 907 rtfree(rt->rt_parent); 908 rt->rt_parent = NULL; 909 910 rt->rt_flags &= ~RTF_UP; 911 912 if (ifp == NULL) { 913 ifp = if_get(rt->rt_ifidx); 914 if (ifp != NULL) { 915 ifp->if_rtrequest(ifp, RTM_DELETE, rt); 916 if_put(ifp); 917 } 918 } else { 919 KASSERT(ifp->if_index == rt->rt_ifidx); 920 ifp->if_rtrequest(ifp, RTM_DELETE, rt); 921 } 922 923 atomic_inc_int(&rttrash); 924 925 if (ret_nrt != NULL) 926 *ret_nrt = rt; 927 else 928 rtfree(rt); 929 930 return (0); 931 } 932 933 int 934 rtrequest(int req, struct rt_addrinfo *info, u_int8_t prio, 935 struct rtentry **ret_nrt, u_int tableid) 936 { 937 struct ifnet *ifp; 938 struct rtentry *rt, *crt; 939 struct ifaddr *ifa; 940 struct sockaddr *ndst; 941 struct sockaddr_rtlabel *sa_rl, sa_rl2; 942 struct sockaddr_dl sa_dl = { sizeof(sa_dl), AF_LINK }; 943 int dlen, error; 944 #ifdef MPLS 945 struct sockaddr_mpls *sa_mpls; 946 #endif 947 948 splsoftassert(IPL_SOFTNET); 949 950 if (!rtable_exists(tableid)) 951 return (EAFNOSUPPORT); 952 if (info->rti_flags & RTF_HOST) 953 info->rti_info[RTAX_NETMASK] = NULL; 954 switch (req) { 955 case RTM_DELETE: 956 error = rtrequest_delete(info, prio, NULL, ret_nrt, tableid); 957 if (error) 958 return (error); 959 break; 960 961 case RTM_RESOLVE: 962 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL) 963 return (EINVAL); 964 if ((rt->rt_flags & RTF_CLONING) == 0) 965 return (EINVAL); 966 KASSERT(rt->rt_ifa->ifa_ifp != NULL); 967 info->rti_ifa = rt->rt_ifa; 968 info->rti_flags = rt->rt_flags | (RTF_CLONED|RTF_HOST); 969 info->rti_flags &= ~(RTF_CLONING|RTF_CONNECTED|RTF_STATIC); 970 info->rti_info[RTAX_GATEWAY] = sdltosa(&sa_dl); 971 info->rti_info[RTAX_LABEL] = 972 rtlabel_id2sa(rt->rt_labelid, &sa_rl2); 973 /* FALLTHROUGH */ 974 975 case RTM_ADD: 976 if (info->rti_ifa == NULL && (error = rt_getifa(info, tableid))) 977 return (error); 978 ifa = info->rti_ifa; 979 ifp = ifa->ifa_ifp; 980 if (prio == 0) 981 prio = ifp->if_priority + RTP_STATIC; 982 983 dlen = info->rti_info[RTAX_DST]->sa_len; 984 ndst = malloc(dlen, M_RTABLE, M_NOWAIT); 985 if (ndst == NULL) 986 return (ENOBUFS); 987 988 if (info->rti_info[RTAX_NETMASK] != NULL) 989 rt_maskedcopy(info->rti_info[RTAX_DST], ndst, 990 info->rti_info[RTAX_NETMASK]); 991 else 992 memcpy(ndst, info->rti_info[RTAX_DST], dlen); 993 994 rt = pool_get(&rtentry_pool, PR_NOWAIT | PR_ZERO); 995 if (rt == NULL) { 996 free(ndst, M_RTABLE, dlen); 997 return (ENOBUFS); 998 } 999 1000 rt->rt_refcnt = 1; 1001 rt->rt_flags = info->rti_flags | RTF_UP; 1002 rt->rt_priority = prio; /* init routing priority */ 1003 LIST_INIT(&rt->rt_timer); 1004 1005 #ifndef SMALL_KERNEL 1006 /* Check the link state if the table supports it. */ 1007 if (rtable_mpath_capable(tableid, ndst->sa_family) && 1008 !ISSET(rt->rt_flags, RTF_LOCAL) && 1009 (!LINK_STATE_IS_UP(ifp->if_link_state) || 1010 !ISSET(ifp->if_flags, IFF_UP))) { 1011 rt->rt_flags &= ~RTF_UP; 1012 rt->rt_priority |= RTP_DOWN; 1013 } 1014 #endif 1015 1016 if (info->rti_info[RTAX_LABEL] != NULL) { 1017 sa_rl = (struct sockaddr_rtlabel *) 1018 info->rti_info[RTAX_LABEL]; 1019 rt->rt_labelid = rtlabel_name2id(sa_rl->sr_label); 1020 } 1021 1022 #ifdef MPLS 1023 /* We have to allocate additional space for MPLS infos */ 1024 if (info->rti_flags & RTF_MPLS && 1025 (info->rti_info[RTAX_SRC] != NULL || 1026 info->rti_info[RTAX_DST]->sa_family == AF_MPLS)) { 1027 struct rt_mpls *rt_mpls; 1028 1029 sa_mpls = (struct sockaddr_mpls *) 1030 info->rti_info[RTAX_SRC]; 1031 1032 rt->rt_llinfo = malloc(sizeof(struct rt_mpls), 1033 M_TEMP, M_NOWAIT|M_ZERO); 1034 1035 if (rt->rt_llinfo == NULL) { 1036 free(ndst, M_RTABLE, dlen); 1037 pool_put(&rtentry_pool, rt); 1038 return (ENOMEM); 1039 } 1040 1041 rt_mpls = (struct rt_mpls *)rt->rt_llinfo; 1042 1043 if (sa_mpls != NULL) 1044 rt_mpls->mpls_label = sa_mpls->smpls_label; 1045 1046 rt_mpls->mpls_operation = info->rti_mpls; 1047 1048 /* XXX: set experimental bits */ 1049 1050 rt->rt_flags |= RTF_MPLS; 1051 } else 1052 rt->rt_flags &= ~RTF_MPLS; 1053 #endif 1054 1055 ifa->ifa_refcnt++; 1056 rt->rt_ifa = ifa; 1057 rt->rt_ifidx = ifp->if_index; 1058 /* 1059 * Copy metrics and a back pointer from the cloned 1060 * route's parent. 1061 */ 1062 if (ISSET(rt->rt_flags, RTF_CLONED)) { 1063 rtref(*ret_nrt); 1064 rt->rt_parent = *ret_nrt; 1065 rt->rt_rmx = (*ret_nrt)->rt_rmx; 1066 } 1067 1068 /* 1069 * We must set rt->rt_gateway before adding ``rt'' to 1070 * the routing table because the radix MPATH code use 1071 * it to (re)order routes. 1072 */ 1073 if ((error = rt_setgate(rt, info->rti_info[RTAX_GATEWAY], 1074 tableid))) { 1075 ifafree(ifa); 1076 rtfree(rt->rt_parent); 1077 rt_putgwroute(rt); 1078 free(rt->rt_gateway, M_RTABLE, 0); 1079 free(ndst, M_RTABLE, dlen); 1080 pool_put(&rtentry_pool, rt); 1081 return (error); 1082 } 1083 1084 error = rtable_insert(tableid, ndst, 1085 info->rti_info[RTAX_NETMASK], info->rti_info[RTAX_GATEWAY], 1086 rt->rt_priority, rt); 1087 if (error != 0 && 1088 (crt = rtable_match(tableid, ndst, NULL)) != NULL) { 1089 /* overwrite cloned route */ 1090 if (ISSET(crt->rt_flags, RTF_CLONED)) { 1091 struct ifnet *cifp; 1092 1093 cifp = if_get(crt->rt_ifidx); 1094 KASSERT(cifp != NULL); 1095 rtdeletemsg(crt, cifp, tableid); 1096 if_put(cifp); 1097 1098 error = rtable_insert(tableid, ndst, 1099 info->rti_info[RTAX_NETMASK], 1100 info->rti_info[RTAX_GATEWAY], 1101 rt->rt_priority, rt); 1102 } 1103 rtfree(crt); 1104 } 1105 if (error != 0) { 1106 ifafree(ifa); 1107 rtfree(rt->rt_parent); 1108 rt_putgwroute(rt); 1109 free(rt->rt_gateway, M_RTABLE, 0); 1110 free(ndst, M_RTABLE, dlen); 1111 pool_put(&rtentry_pool, rt); 1112 return (EEXIST); 1113 } 1114 ifp->if_rtrequest(ifp, req, rt); 1115 1116 if_group_routechange(info->rti_info[RTAX_DST], 1117 info->rti_info[RTAX_NETMASK]); 1118 1119 if (ret_nrt != NULL) 1120 *ret_nrt = rt; 1121 else 1122 rtfree(rt); 1123 break; 1124 } 1125 1126 return (0); 1127 } 1128 1129 int 1130 rt_setgate(struct rtentry *rt, struct sockaddr *gate, u_int rtableid) 1131 { 1132 int glen = ROUNDUP(gate->sa_len); 1133 struct sockaddr *sa; 1134 1135 if (rt->rt_gateway == NULL || glen > ROUNDUP(rt->rt_gateway->sa_len)) { 1136 sa = malloc(glen, M_RTABLE, M_NOWAIT); 1137 if (sa == NULL) 1138 return (ENOBUFS); 1139 free(rt->rt_gateway, M_RTABLE, 0); 1140 rt->rt_gateway = sa; 1141 } 1142 memmove(rt->rt_gateway, gate, glen); 1143 1144 if (ISSET(rt->rt_flags, RTF_GATEWAY)) 1145 return (rt_setgwroute(rt, rtableid)); 1146 1147 return (0); 1148 } 1149 1150 /* 1151 * Return the route entry containing the next hop link-layer 1152 * address corresponding to ``rt''. 1153 */ 1154 struct rtentry * 1155 rt_getll(struct rtentry *rt) 1156 { 1157 if (ISSET(rt->rt_flags, RTF_GATEWAY)) { 1158 KASSERT(rt->rt_gwroute != NULL); 1159 return (rt->rt_gwroute); 1160 } 1161 1162 return (rt); 1163 } 1164 1165 void 1166 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, 1167 struct sockaddr *netmask) 1168 { 1169 u_char *cp1 = (u_char *)src; 1170 u_char *cp2 = (u_char *)dst; 1171 u_char *cp3 = (u_char *)netmask; 1172 u_char *cplim = cp2 + *cp3; 1173 u_char *cplim2 = cp2 + *cp1; 1174 1175 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */ 1176 cp3 += 2; 1177 if (cplim > cplim2) 1178 cplim = cplim2; 1179 while (cp2 < cplim) 1180 *cp2++ = *cp1++ & *cp3++; 1181 if (cp2 < cplim2) 1182 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2)); 1183 } 1184 1185 int 1186 rt_ifa_add(struct ifaddr *ifa, int flags, struct sockaddr *dst) 1187 { 1188 struct ifnet *ifp = ifa->ifa_ifp; 1189 struct rtentry *rt; 1190 struct sockaddr_rtlabel sa_rl; 1191 struct rt_addrinfo info; 1192 unsigned int rtableid = ifp->if_rdomain; 1193 uint8_t prio = ifp->if_priority + RTP_STATIC; 1194 int error; 1195 1196 memset(&info, 0, sizeof(info)); 1197 info.rti_ifa = ifa; 1198 info.rti_flags = flags | RTF_MPATH; 1199 info.rti_info[RTAX_DST] = dst; 1200 if (flags & RTF_LLINFO) 1201 info.rti_info[RTAX_GATEWAY] = sdltosa(ifp->if_sadl); 1202 else 1203 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr; 1204 info.rti_info[RTAX_LABEL] = rtlabel_id2sa(ifp->if_rtlabelid, &sa_rl); 1205 1206 #ifdef MPLS 1207 if ((flags & RTF_MPLS) == RTF_MPLS) 1208 info.rti_mpls = MPLS_OP_POP; 1209 #endif /* MPLS */ 1210 1211 if ((flags & RTF_HOST) == 0) 1212 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1213 1214 if (flags & (RTF_LOCAL|RTF_BROADCAST)) 1215 prio = RTP_LOCAL; 1216 1217 if (flags & RTF_CONNECTED) 1218 prio = ifp->if_priority + RTP_CONNECTED; 1219 1220 error = rtrequest(RTM_ADD, &info, prio, &rt, rtableid); 1221 if (error == 0) { 1222 /* 1223 * A local route is created for every address configured 1224 * on an interface, so use this information to notify 1225 * userland that a new address has been added. 1226 */ 1227 if (flags & RTF_LOCAL) 1228 rt_sendaddrmsg(rt, RTM_NEWADDR, ifa); 1229 rt_sendmsg(rt, RTM_ADD, rtableid); 1230 rtfree(rt); 1231 } 1232 return (error); 1233 } 1234 1235 int 1236 rt_ifa_del(struct ifaddr *ifa, int flags, struct sockaddr *dst) 1237 { 1238 struct ifnet *ifp = ifa->ifa_ifp; 1239 struct rtentry *rt; 1240 struct mbuf *m = NULL; 1241 struct sockaddr *deldst; 1242 struct rt_addrinfo info; 1243 struct sockaddr_rtlabel sa_rl; 1244 unsigned int rtableid = ifp->if_rdomain; 1245 uint8_t prio = ifp->if_priority + RTP_STATIC; 1246 int error; 1247 1248 #ifdef MPLS 1249 if ((flags & RTF_MPLS) == RTF_MPLS) 1250 /* MPLS routes only exist in rdomain 0 */ 1251 rtableid = 0; 1252 #endif /* MPLS */ 1253 1254 if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) { 1255 m = m_get(M_DONTWAIT, MT_SONAME); 1256 if (m == NULL) 1257 return (ENOBUFS); 1258 deldst = mtod(m, struct sockaddr *); 1259 rt_maskedcopy(dst, deldst, ifa->ifa_netmask); 1260 dst = deldst; 1261 } 1262 1263 memset(&info, 0, sizeof(info)); 1264 info.rti_ifa = ifa; 1265 info.rti_flags = flags; 1266 info.rti_info[RTAX_DST] = dst; 1267 if ((flags & RTF_LLINFO) == 0) 1268 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr; 1269 info.rti_info[RTAX_LABEL] = rtlabel_id2sa(ifp->if_rtlabelid, &sa_rl); 1270 1271 if ((flags & RTF_HOST) == 0) 1272 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1273 1274 if (flags & (RTF_LOCAL|RTF_BROADCAST)) 1275 prio = RTP_LOCAL; 1276 1277 if (flags & RTF_CONNECTED) 1278 prio = ifp->if_priority + RTP_CONNECTED; 1279 1280 error = rtrequest_delete(&info, prio, ifp, &rt, rtableid); 1281 if (error == 0) { 1282 rt_sendmsg(rt, RTM_DELETE, rtableid); 1283 if (flags & RTF_LOCAL) 1284 rt_sendaddrmsg(rt, RTM_DELADDR, ifa); 1285 rtfree(rt); 1286 } 1287 m_free(m); 1288 1289 return (error); 1290 } 1291 1292 /* 1293 * Add ifa's address as a local rtentry. 1294 */ 1295 int 1296 rt_ifa_addlocal(struct ifaddr *ifa) 1297 { 1298 struct rtentry *rt; 1299 u_int flags = RTF_HOST|RTF_LOCAL; 1300 int error = 0; 1301 1302 /* 1303 * If the configured address correspond to the magical "any" 1304 * address do not add a local route entry because that might 1305 * corrupt the routing tree which uses this value for the 1306 * default routes. 1307 */ 1308 switch (ifa->ifa_addr->sa_family) { 1309 case AF_INET: 1310 if (satosin(ifa->ifa_addr)->sin_addr.s_addr == INADDR_ANY) 1311 return (0); 1312 break; 1313 #ifdef INET6 1314 case AF_INET6: 1315 if (IN6_ARE_ADDR_EQUAL(&satosin6(ifa->ifa_addr)->sin6_addr, 1316 &in6addr_any)) 1317 return (0); 1318 break; 1319 #endif 1320 default: 1321 break; 1322 } 1323 1324 if (!ISSET(ifa->ifa_ifp->if_flags, (IFF_LOOPBACK|IFF_POINTOPOINT))) 1325 flags |= RTF_LLINFO; 1326 1327 /* If there is no local entry, allocate one. */ 1328 rt = rtalloc(ifa->ifa_addr, 0, ifa->ifa_ifp->if_rdomain); 1329 if (rt == NULL || ISSET(rt->rt_flags, flags) != flags) 1330 error = rt_ifa_add(ifa, flags, ifa->ifa_addr); 1331 rtfree(rt); 1332 1333 return (error); 1334 } 1335 1336 /* 1337 * Remove local rtentry of ifa's addresss if it exists. 1338 */ 1339 int 1340 rt_ifa_dellocal(struct ifaddr *ifa) 1341 { 1342 struct rtentry *rt; 1343 u_int flags = RTF_HOST|RTF_LOCAL; 1344 int error = 0; 1345 1346 /* 1347 * We do not add local routes for such address, so do not bother 1348 * removing them. 1349 */ 1350 switch (ifa->ifa_addr->sa_family) { 1351 case AF_INET: 1352 if (satosin(ifa->ifa_addr)->sin_addr.s_addr == INADDR_ANY) 1353 return (0); 1354 break; 1355 #ifdef INET6 1356 case AF_INET6: 1357 if (IN6_ARE_ADDR_EQUAL(&satosin6(ifa->ifa_addr)->sin6_addr, 1358 &in6addr_any)) 1359 return (0); 1360 break; 1361 #endif 1362 default: 1363 break; 1364 } 1365 1366 if (!ISSET(ifa->ifa_ifp->if_flags, (IFF_LOOPBACK|IFF_POINTOPOINT))) 1367 flags |= RTF_LLINFO; 1368 1369 /* 1370 * Before deleting, check if a corresponding local host 1371 * route surely exists. With this check, we can avoid to 1372 * delete an interface direct route whose destination is same 1373 * as the address being removed. This can happen when removing 1374 * a subnet-router anycast address on an interface attached 1375 * to a shared medium. 1376 */ 1377 rt = rtalloc(ifa->ifa_addr, 0, ifa->ifa_ifp->if_rdomain); 1378 if (rt != NULL && ISSET(rt->rt_flags, flags) == flags) 1379 error = rt_ifa_del(ifa, flags, ifa->ifa_addr); 1380 rtfree(rt); 1381 1382 return (error); 1383 } 1384 1385 /* 1386 * Remove all addresses attached to ``ifa''. 1387 */ 1388 void 1389 rt_ifa_purge(struct ifaddr *ifa) 1390 { 1391 struct ifnet *ifp = ifa->ifa_ifp; 1392 unsigned int rtableid; 1393 int i; 1394 1395 KASSERT(ifp != NULL); 1396 1397 for (rtableid = 0; rtableid < rtmap_limit; rtableid++) { 1398 /* skip rtables that are not in the rdomain of the ifp */ 1399 if (rtable_l2(rtableid) != ifp->if_rdomain) 1400 continue; 1401 for (i = 1; i <= AF_MAX; i++) { 1402 rtable_walk(rtableid, i, rt_ifa_purge_walker, ifa); 1403 } 1404 } 1405 } 1406 1407 int 1408 rt_ifa_purge_walker(struct rtentry *rt, void *vifa, unsigned int rtableid) 1409 { 1410 struct ifaddr *ifa = vifa; 1411 struct ifnet *ifp = ifa->ifa_ifp; 1412 int error; 1413 1414 if (rt->rt_ifa != ifa) 1415 return (0); 1416 1417 if ((error = rtdeletemsg(rt, ifp, rtableid))) { 1418 return (error); 1419 } 1420 1421 return (EAGAIN); 1422 1423 } 1424 1425 /* 1426 * Route timer routines. These routes allow functions to be called 1427 * for various routes at any time. This is useful in supporting 1428 * path MTU discovery and redirect route deletion. 1429 * 1430 * This is similar to some BSDI internal functions, but it provides 1431 * for multiple queues for efficiency's sake... 1432 */ 1433 1434 LIST_HEAD(, rttimer_queue) rttimer_queue_head; 1435 static int rt_init_done = 0; 1436 1437 #define RTTIMER_CALLOUT(r) { \ 1438 if (r->rtt_func != NULL) { \ 1439 (*r->rtt_func)(r->rtt_rt, r); \ 1440 } else { \ 1441 struct rt_addrinfo info; \ 1442 bzero(&info, sizeof(info)); \ 1443 info.rti_info[RTAX_DST] = rt_key(r->rtt_rt); \ 1444 rtrequest(RTM_DELETE, &info, \ 1445 r->rtt_rt->rt_priority, NULL, r->rtt_tableid); \ 1446 } \ 1447 } 1448 1449 /* 1450 * Some subtle order problems with domain initialization mean that 1451 * we cannot count on this being run from rt_init before various 1452 * protocol initializations are done. Therefore, we make sure 1453 * that this is run when the first queue is added... 1454 */ 1455 1456 void 1457 rt_timer_init(void) 1458 { 1459 static struct timeout rt_timer_timeout; 1460 1461 if (rt_init_done) 1462 panic("rt_timer_init: already initialized"); 1463 1464 pool_init(&rttimer_pool, sizeof(struct rttimer), 0, IPL_SOFTNET, 0, 1465 "rttmr", NULL); 1466 1467 LIST_INIT(&rttimer_queue_head); 1468 timeout_set_proc(&rt_timer_timeout, rt_timer_timer, &rt_timer_timeout); 1469 timeout_add_sec(&rt_timer_timeout, 1); 1470 rt_init_done = 1; 1471 } 1472 1473 struct rttimer_queue * 1474 rt_timer_queue_create(u_int timeout) 1475 { 1476 struct rttimer_queue *rtq; 1477 1478 if (rt_init_done == 0) 1479 rt_timer_init(); 1480 1481 if ((rtq = malloc(sizeof(*rtq), M_RTABLE, M_NOWAIT|M_ZERO)) == NULL) 1482 return (NULL); 1483 1484 rtq->rtq_timeout = timeout; 1485 rtq->rtq_count = 0; 1486 TAILQ_INIT(&rtq->rtq_head); 1487 LIST_INSERT_HEAD(&rttimer_queue_head, rtq, rtq_link); 1488 1489 return (rtq); 1490 } 1491 1492 void 1493 rt_timer_queue_change(struct rttimer_queue *rtq, long timeout) 1494 { 1495 rtq->rtq_timeout = timeout; 1496 } 1497 1498 void 1499 rt_timer_queue_destroy(struct rttimer_queue *rtq) 1500 { 1501 struct rttimer *r; 1502 1503 NET_ASSERT_LOCKED(); 1504 1505 while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL) { 1506 LIST_REMOVE(r, rtt_link); 1507 TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next); 1508 RTTIMER_CALLOUT(r); 1509 pool_put(&rttimer_pool, r); 1510 if (rtq->rtq_count > 0) 1511 rtq->rtq_count--; 1512 else 1513 printf("rt_timer_queue_destroy: rtq_count reached 0\n"); 1514 } 1515 1516 LIST_REMOVE(rtq, rtq_link); 1517 free(rtq, M_RTABLE, sizeof(*rtq)); 1518 } 1519 1520 unsigned long 1521 rt_timer_queue_count(struct rttimer_queue *rtq) 1522 { 1523 return (rtq->rtq_count); 1524 } 1525 1526 void 1527 rt_timer_remove_all(struct rtentry *rt) 1528 { 1529 struct rttimer *r; 1530 1531 while ((r = LIST_FIRST(&rt->rt_timer)) != NULL) { 1532 LIST_REMOVE(r, rtt_link); 1533 TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next); 1534 if (r->rtt_queue->rtq_count > 0) 1535 r->rtt_queue->rtq_count--; 1536 else 1537 printf("rt_timer_remove_all: rtq_count reached 0\n"); 1538 pool_put(&rttimer_pool, r); 1539 } 1540 } 1541 1542 int 1543 rt_timer_add(struct rtentry *rt, void (*func)(struct rtentry *, 1544 struct rttimer *), struct rttimer_queue *queue, u_int rtableid) 1545 { 1546 struct rttimer *r; 1547 long current_time; 1548 1549 current_time = time_uptime; 1550 rt->rt_rmx.rmx_expire = time_uptime + queue->rtq_timeout; 1551 1552 /* 1553 * If there's already a timer with this action, destroy it before 1554 * we add a new one. 1555 */ 1556 LIST_FOREACH(r, &rt->rt_timer, rtt_link) { 1557 if (r->rtt_func == func) { 1558 LIST_REMOVE(r, rtt_link); 1559 TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next); 1560 if (r->rtt_queue->rtq_count > 0) 1561 r->rtt_queue->rtq_count--; 1562 else 1563 printf("rt_timer_add: rtq_count reached 0\n"); 1564 pool_put(&rttimer_pool, r); 1565 break; /* only one per list, so we can quit... */ 1566 } 1567 } 1568 1569 r = pool_get(&rttimer_pool, PR_NOWAIT | PR_ZERO); 1570 if (r == NULL) 1571 return (ENOBUFS); 1572 1573 r->rtt_rt = rt; 1574 r->rtt_time = current_time; 1575 r->rtt_func = func; 1576 r->rtt_queue = queue; 1577 r->rtt_tableid = rtableid; 1578 LIST_INSERT_HEAD(&rt->rt_timer, r, rtt_link); 1579 TAILQ_INSERT_TAIL(&queue->rtq_head, r, rtt_next); 1580 r->rtt_queue->rtq_count++; 1581 1582 return (0); 1583 } 1584 1585 void 1586 rt_timer_timer(void *arg) 1587 { 1588 struct timeout *to = (struct timeout *)arg; 1589 struct rttimer_queue *rtq; 1590 struct rttimer *r; 1591 long current_time; 1592 int s; 1593 1594 current_time = time_uptime; 1595 1596 NET_LOCK(s); 1597 LIST_FOREACH(rtq, &rttimer_queue_head, rtq_link) { 1598 while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL && 1599 (r->rtt_time + rtq->rtq_timeout) < current_time) { 1600 LIST_REMOVE(r, rtt_link); 1601 TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next); 1602 RTTIMER_CALLOUT(r); 1603 pool_put(&rttimer_pool, r); 1604 if (rtq->rtq_count > 0) 1605 rtq->rtq_count--; 1606 else 1607 printf("rt_timer_timer: rtq_count reached 0\n"); 1608 } 1609 } 1610 NET_UNLOCK(s); 1611 1612 timeout_add_sec(to, 1); 1613 } 1614 1615 u_int16_t 1616 rtlabel_name2id(char *name) 1617 { 1618 struct rt_label *label, *p; 1619 u_int16_t new_id = 1; 1620 1621 if (!name[0]) 1622 return (0); 1623 1624 TAILQ_FOREACH(label, &rt_labels, rtl_entry) 1625 if (strcmp(name, label->rtl_name) == 0) { 1626 label->rtl_ref++; 1627 return (label->rtl_id); 1628 } 1629 1630 /* 1631 * to avoid fragmentation, we do a linear search from the beginning 1632 * and take the first free slot we find. if there is none or the list 1633 * is empty, append a new entry at the end. 1634 */ 1635 TAILQ_FOREACH(p, &rt_labels, rtl_entry) { 1636 if (p->rtl_id != new_id) 1637 break; 1638 new_id = p->rtl_id + 1; 1639 } 1640 if (new_id > LABELID_MAX) 1641 return (0); 1642 1643 label = malloc(sizeof(*label), M_RTABLE, M_NOWAIT|M_ZERO); 1644 if (label == NULL) 1645 return (0); 1646 strlcpy(label->rtl_name, name, sizeof(label->rtl_name)); 1647 label->rtl_id = new_id; 1648 label->rtl_ref++; 1649 1650 if (p != NULL) /* insert new entry before p */ 1651 TAILQ_INSERT_BEFORE(p, label, rtl_entry); 1652 else /* either list empty or no free slot in between */ 1653 TAILQ_INSERT_TAIL(&rt_labels, label, rtl_entry); 1654 1655 return (label->rtl_id); 1656 } 1657 1658 const char * 1659 rtlabel_id2name(u_int16_t id) 1660 { 1661 struct rt_label *label; 1662 1663 TAILQ_FOREACH(label, &rt_labels, rtl_entry) 1664 if (label->rtl_id == id) 1665 return (label->rtl_name); 1666 1667 return (NULL); 1668 } 1669 1670 struct sockaddr * 1671 rtlabel_id2sa(u_int16_t labelid, struct sockaddr_rtlabel *sa_rl) 1672 { 1673 const char *label; 1674 1675 if (labelid == 0 || (label = rtlabel_id2name(labelid)) == NULL) 1676 return (NULL); 1677 1678 bzero(sa_rl, sizeof(*sa_rl)); 1679 sa_rl->sr_len = sizeof(*sa_rl); 1680 sa_rl->sr_family = AF_UNSPEC; 1681 strlcpy(sa_rl->sr_label, label, sizeof(sa_rl->sr_label)); 1682 1683 return ((struct sockaddr *)sa_rl); 1684 } 1685 1686 void 1687 rtlabel_unref(u_int16_t id) 1688 { 1689 struct rt_label *p, *next; 1690 1691 if (id == 0) 1692 return; 1693 1694 TAILQ_FOREACH_SAFE(p, &rt_labels, rtl_entry, next) { 1695 if (id == p->rtl_id) { 1696 if (--p->rtl_ref == 0) { 1697 TAILQ_REMOVE(&rt_labels, p, rtl_entry); 1698 free(p, M_RTABLE, sizeof(*p)); 1699 } 1700 break; 1701 } 1702 } 1703 } 1704 1705 #ifndef SMALL_KERNEL 1706 void 1707 rt_if_track(struct ifnet *ifp) 1708 { 1709 int i; 1710 u_int tid; 1711 1712 for (tid = 0; tid < rtmap_limit; tid++) { 1713 /* skip rtables that are not in the rdomain of the ifp */ 1714 if (rtable_l2(tid) != ifp->if_rdomain) 1715 continue; 1716 for (i = 1; i <= AF_MAX; i++) { 1717 if (!rtable_mpath_capable(tid, i)) 1718 continue; 1719 1720 rtable_walk(tid, i, rt_if_linkstate_change, ifp); 1721 } 1722 } 1723 } 1724 1725 int 1726 rt_if_linkstate_change(struct rtentry *rt, void *arg, u_int id) 1727 { 1728 struct ifnet *ifp = arg; 1729 struct sockaddr_in6 sa_mask; 1730 1731 if (rt->rt_ifidx != ifp->if_index) 1732 return (0); 1733 1734 /* Local routes are always usable. */ 1735 if (rt->rt_flags & RTF_LOCAL) { 1736 rt->rt_flags |= RTF_UP; 1737 return (0); 1738 } 1739 1740 if (LINK_STATE_IS_UP(ifp->if_link_state) && ifp->if_flags & IFF_UP) { 1741 if (!(rt->rt_flags & RTF_UP)) { 1742 /* bring route up */ 1743 rt->rt_flags |= RTF_UP; 1744 rtable_mpath_reprio(id, rt_key(rt), 1745 rt_plen2mask(rt, &sa_mask), 1746 rt->rt_priority & RTP_MASK, rt); 1747 } 1748 } else { 1749 if (rt->rt_flags & RTF_UP) { 1750 /* 1751 * Remove redirected and cloned routes (mainly ARP) 1752 * from down interfaces so we have a chance to get 1753 * new routes from a better source. 1754 */ 1755 if (ISSET(rt->rt_flags, RTF_CLONED|RTF_DYNAMIC) && 1756 !ISSET(rt->rt_flags, RTF_CACHED|RTF_BFD)) { 1757 int error; 1758 1759 if ((error = rtdeletemsg(rt, ifp, id))) 1760 return (error); 1761 return (EAGAIN); 1762 } 1763 /* take route down */ 1764 rt->rt_flags &= ~RTF_UP; 1765 rtable_mpath_reprio(id, rt_key(rt), 1766 rt_plen2mask(rt, &sa_mask), 1767 rt->rt_priority | RTP_DOWN, rt); 1768 } 1769 } 1770 if_group_routechange(rt_key(rt), rt_plen2mask(rt, &sa_mask)); 1771 1772 return (0); 1773 } 1774 #endif 1775 1776 struct sockaddr * 1777 rt_plentosa(sa_family_t af, int plen, struct sockaddr_in6 *sa_mask) 1778 { 1779 struct sockaddr_in *sin = (struct sockaddr_in *)sa_mask; 1780 #ifdef INET6 1781 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa_mask; 1782 #endif 1783 1784 KASSERT(plen >= 0 || plen == -1); 1785 1786 if (plen == -1) 1787 return (NULL); 1788 1789 memset(sa_mask, 0, sizeof(*sa_mask)); 1790 1791 switch (af) { 1792 case AF_INET: 1793 sin->sin_family = AF_INET; 1794 sin->sin_len = sizeof(struct sockaddr_in); 1795 in_prefixlen2mask(&sin->sin_addr, plen); 1796 break; 1797 #ifdef INET6 1798 case AF_INET6: 1799 sin6->sin6_family = AF_INET6; 1800 sin6->sin6_len = sizeof(struct sockaddr_in6); 1801 in6_prefixlen2mask(&sin6->sin6_addr, plen); 1802 break; 1803 #endif /* INET6 */ 1804 default: 1805 return (NULL); 1806 } 1807 1808 return ((struct sockaddr *)sa_mask); 1809 } 1810 1811 struct sockaddr * 1812 rt_plen2mask(struct rtentry *rt, struct sockaddr_in6 *sa_mask) 1813 { 1814 #ifndef ART 1815 return (rt_mask(rt)); 1816 #else 1817 return (rt_plentosa(rt_key(rt)->sa_family, rt_plen(rt), sa_mask)); 1818 #endif /* ART */ 1819 } 1820 1821 #ifdef DDB 1822 #include <machine/db_machdep.h> 1823 #include <ddb/db_output.h> 1824 1825 void 1826 db_print_sa(struct sockaddr *sa) 1827 { 1828 int len; 1829 u_char *p; 1830 1831 if (sa == NULL) { 1832 db_printf("[NULL]"); 1833 return; 1834 } 1835 1836 p = (u_char *)sa; 1837 len = sa->sa_len; 1838 db_printf("["); 1839 while (len > 0) { 1840 db_printf("%d", *p); 1841 p++; 1842 len--; 1843 if (len) 1844 db_printf(","); 1845 } 1846 db_printf("]\n"); 1847 } 1848 1849 void 1850 db_print_ifa(struct ifaddr *ifa) 1851 { 1852 if (ifa == NULL) 1853 return; 1854 db_printf(" ifa_addr="); 1855 db_print_sa(ifa->ifa_addr); 1856 db_printf(" ifa_dsta="); 1857 db_print_sa(ifa->ifa_dstaddr); 1858 db_printf(" ifa_mask="); 1859 db_print_sa(ifa->ifa_netmask); 1860 db_printf(" flags=0x%x, refcnt=%d, metric=%d\n", 1861 ifa->ifa_flags, ifa->ifa_refcnt, ifa->ifa_metric); 1862 } 1863 1864 /* 1865 * Function to pass to rtalble_walk(). 1866 * Return non-zero error to abort walk. 1867 */ 1868 int 1869 db_show_rtentry(struct rtentry *rt, void *w, unsigned int id) 1870 { 1871 db_printf("rtentry=%p", rt); 1872 1873 db_printf(" flags=0x%x refcnt=%d use=%llu expire=%lld rtableid=%u\n", 1874 rt->rt_flags, rt->rt_refcnt, rt->rt_use, rt->rt_expire, id); 1875 1876 db_printf(" key="); db_print_sa(rt_key(rt)); 1877 db_printf(" plen=%d", rt_plen(rt)); 1878 db_printf(" gw="); db_print_sa(rt->rt_gateway); 1879 db_printf(" ifidx=%u ", rt->rt_ifidx); 1880 db_printf(" ifa=%p\n", rt->rt_ifa); 1881 db_print_ifa(rt->rt_ifa); 1882 1883 db_printf(" gwroute=%p llinfo=%p\n", rt->rt_gwroute, rt->rt_llinfo); 1884 return (0); 1885 } 1886 1887 /* 1888 * Function to print all the route trees. 1889 * Use this from ddb: "call db_show_arptab" 1890 */ 1891 int 1892 db_show_arptab(void) 1893 { 1894 db_printf("Route tree for AF_INET\n"); 1895 rtable_walk(0, AF_INET, db_show_rtentry, NULL); 1896 return (0); 1897 } 1898 #endif /* DDB */ 1899