1 /* $OpenBSD: route.c,v 1.27 2001/12/18 23:07:49 deraadt 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. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)route.c 8.2 (Berkeley) 11/15/93 66 */ 67 68 /* 69 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 70 * 71 * NRL grants permission for redistribution and use in source and binary 72 * forms, with or without modification, of the software and documentation 73 * created at NRL provided that the following conditions are met: 74 * 75 * 1. Redistributions of source code must retain the above copyright 76 * notice, this list of conditions and the following disclaimer. 77 * 2. Redistributions in binary form must reproduce the above copyright 78 * notice, this list of conditions and the following disclaimer in the 79 * documentation and/or other materials provided with the distribution. 80 * 3. All advertising materials mentioning features or use of this software 81 * must display the following acknowledgements: 82 * This product includes software developed by the University of 83 * California, Berkeley and its contributors. 84 * This product includes software developed at the Information 85 * Technology Division, US Naval Research Laboratory. 86 * 4. Neither the name of the NRL nor the names of its contributors 87 * may be used to endorse or promote products derived from this software 88 * without specific prior written permission. 89 * 90 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 91 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 92 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 93 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 94 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 95 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 96 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 97 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 98 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 99 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 100 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 101 * 102 * The views and conclusions contained in the software and documentation 103 * are those of the authors and should not be interpreted as representing 104 * official policies, either expressed or implied, of the US Naval 105 * Research Laboratory (NRL). 106 */ 107 108 #include <sys/param.h> 109 #include <sys/systm.h> 110 #include <sys/proc.h> 111 #include <sys/mbuf.h> 112 #include <sys/socket.h> 113 #include <sys/socketvar.h> 114 #include <sys/domain.h> 115 #include <sys/protosw.h> 116 #include <sys/ioctl.h> 117 #include <sys/kernel.h> 118 119 #include <net/if.h> 120 #include <net/route.h> 121 #include <net/raw_cb.h> 122 123 #include <netinet/in.h> 124 #include <netinet/in_var.h> 125 126 #ifdef NS 127 #include <netns/ns.h> 128 #endif 129 130 #ifdef IPSEC 131 #include <netinet/ip_ipsp.h> 132 133 extern struct ifnet encif; 134 #endif 135 136 #define SA(p) ((struct sockaddr *)(p)) 137 138 int rttrash; /* routes not in table but not freed */ 139 struct sockaddr wildcard; /* zero valued cookie for wildcard searches */ 140 141 static int okaytoclone __P((u_int, int)); 142 143 #ifdef IPSEC 144 145 static struct ifaddr * 146 encap_findgwifa(struct sockaddr *gw) 147 { 148 return TAILQ_FIRST(&encif.if_addrlist); 149 } 150 151 #endif 152 153 void 154 rtable_init(table) 155 void **table; 156 { 157 struct domain *dom; 158 for (dom = domains; dom; dom = dom->dom_next) 159 if (dom->dom_rtattach) 160 dom->dom_rtattach(&table[dom->dom_family], 161 dom->dom_rtoffset); 162 } 163 164 void 165 route_init() 166 { 167 rn_init(); /* initialize all zeroes, all ones, mask table */ 168 rtable_init((void **)rt_tables); 169 } 170 171 void 172 rtalloc_noclone(ro, howstrict) 173 register struct route *ro; 174 int howstrict; 175 { 176 if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP)) 177 return; /* XXX */ 178 ro->ro_rt = rtalloc2(&ro->ro_dst, 1, howstrict); 179 } 180 181 static int 182 okaytoclone(flags, howstrict) 183 u_int flags; 184 int howstrict; 185 { 186 if (howstrict == ALL_CLONING) 187 return 1; 188 if (howstrict == ONNET_CLONING && !(flags & RTF_GATEWAY)) 189 return 1; 190 return 0; 191 } 192 193 struct rtentry * 194 rtalloc2(dst, report,howstrict) 195 register struct sockaddr *dst; 196 int report,howstrict; 197 { 198 register struct radix_node_head *rnh = rt_tables[dst->sa_family]; 199 register struct rtentry *rt; 200 register struct radix_node *rn; 201 struct rtentry *newrt = 0; 202 struct rt_addrinfo info; 203 int s = splnet(), err = 0, msgtype = RTM_MISS; 204 205 if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) && 206 ((rn->rn_flags & RNF_ROOT) == 0)) { 207 newrt = rt = (struct rtentry *)rn; 208 if (report && (rt->rt_flags & RTF_CLONING) && 209 okaytoclone(rt->rt_flags, howstrict)) { 210 err = rtrequest(RTM_RESOLVE, dst, SA(0), SA(0), 0, 211 &newrt); 212 if (err) { 213 newrt = rt; 214 rt->rt_refcnt++; 215 goto miss; 216 } 217 if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) { 218 msgtype = RTM_RESOLVE; 219 goto miss; 220 } 221 } else 222 rt->rt_refcnt++; 223 } else { 224 rtstat.rts_unreach++; 225 miss: if (report) { 226 bzero((caddr_t)&info, sizeof(info)); 227 info.rti_info[RTAX_DST] = dst; 228 rt_missmsg(msgtype, &info, 0, err); 229 } 230 } 231 splx(s); 232 return (newrt); 233 } 234 235 /* 236 * Packet routing routines. 237 */ 238 void 239 rtalloc(ro) 240 register struct route *ro; 241 { 242 if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP)) 243 return; /* XXX */ 244 ro->ro_rt = rtalloc1(&ro->ro_dst, 1); 245 } 246 247 struct rtentry * 248 rtalloc1(dst, report) 249 register struct sockaddr *dst; 250 int report; 251 { 252 register struct radix_node_head *rnh = rt_tables[dst->sa_family]; 253 register struct rtentry *rt; 254 register struct radix_node *rn; 255 struct rtentry *newrt = 0; 256 struct rt_addrinfo info; 257 int s = splsoftnet(), err = 0, msgtype = RTM_MISS; 258 259 if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) && 260 ((rn->rn_flags & RNF_ROOT) == 0)) { 261 newrt = rt = (struct rtentry *)rn; 262 if (report && (rt->rt_flags & RTF_CLONING)) { 263 err = rtrequest(RTM_RESOLVE, dst, SA(NULL), 264 SA(NULL), 0, &newrt); 265 if (err) { 266 newrt = rt; 267 rt->rt_refcnt++; 268 goto miss; 269 } 270 if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) { 271 msgtype = RTM_RESOLVE; 272 goto miss; 273 } 274 /* Inform listeners of the new route */ 275 bzero(&info, sizeof(info)); 276 info.rti_info[RTAX_DST] = rt_key(rt); 277 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 278 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 279 if (rt->rt_ifp != NULL) { 280 info.rti_info[RTAX_IFP] = 281 TAILQ_FIRST(&rt->rt_ifp->if_addrlist)->ifa_addr; 282 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 283 } 284 rt_missmsg(RTM_ADD, &info, rt->rt_flags, 0); 285 } else 286 rt->rt_refcnt++; 287 } else { 288 if (dst->sa_family != PF_KEY) 289 rtstat.rts_unreach++; 290 /* 291 * IP encapsulation does lots of lookups where we don't need nor want 292 * the RTM_MISSes that would be generated. It causes RTM_MISS storms 293 * sent upward breaking user-level routing queries. 294 */ 295 miss: if (report && dst->sa_family != PF_KEY) { 296 bzero((caddr_t)&info, sizeof(info)); 297 info.rti_info[RTAX_DST] = dst; 298 rt_missmsg(msgtype, &info, 0, err); 299 } 300 } 301 splx(s); 302 return (newrt); 303 } 304 305 void 306 rtfree(rt) 307 register struct rtentry *rt; 308 { 309 register struct ifaddr *ifa; 310 311 if (rt == NULL) 312 panic("rtfree"); 313 rt->rt_refcnt--; 314 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) { 315 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 316 panic ("rtfree 2"); 317 rttrash--; 318 if (rt->rt_refcnt < 0) { 319 printf("rtfree: %p not freed (neg refs)\n", rt); 320 return; 321 } 322 rt_timer_remove_all(rt); 323 ifa = rt->rt_ifa; 324 if (ifa) 325 IFAFREE(ifa); 326 Free(rt_key(rt)); 327 Free(rt); 328 } 329 } 330 331 void 332 ifafree(ifa) 333 register struct ifaddr *ifa; 334 { 335 if (ifa == NULL) 336 panic("ifafree"); 337 if (ifa->ifa_refcnt == 0) 338 free(ifa, M_IFADDR); 339 else 340 ifa->ifa_refcnt--; 341 } 342 343 /* 344 * Force a routing table entry to the specified 345 * destination to go through the given gateway. 346 * Normally called as a result of a routing redirect 347 * message from the network layer. 348 * 349 * N.B.: must be called at splsoftnet 350 */ 351 void 352 rtredirect(dst, gateway, netmask, flags, src, rtp) 353 struct sockaddr *dst, *gateway, *netmask, *src; 354 int flags; 355 struct rtentry **rtp; 356 { 357 struct rtentry *rt; 358 int error = 0; 359 u_int32_t *stat = NULL; 360 struct rt_addrinfo info; 361 struct ifaddr *ifa; 362 363 /* verify the gateway is directly reachable */ 364 if ((ifa = ifa_ifwithnet(gateway)) == NULL) { 365 error = ENETUNREACH; 366 goto out; 367 } 368 rt = rtalloc1(dst, 0); 369 /* 370 * If the redirect isn't from our current router for this dst, 371 * it's either old or wrong. If it redirects us to ourselves, 372 * we have a routing loop, perhaps as a result of an interface 373 * going down recently. 374 */ 375 #define equal(a1, a2) \ 376 ((a1)->sa_len == (a2)->sa_len && \ 377 bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0) 378 if (!(flags & RTF_DONE) && rt && 379 (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) 380 error = EINVAL; 381 else if (ifa_ifwithaddr(gateway) != NULL) 382 error = EHOSTUNREACH; 383 if (error) 384 goto done; 385 /* 386 * Create a new entry if we just got back a wildcard entry 387 * or the lookup failed. This is necessary for hosts 388 * which use routing redirects generated by smart gateways 389 * to dynamically build the routing tables. 390 */ 391 if ((rt == NULL) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2)) 392 goto create; 393 /* 394 * Don't listen to the redirect if it's 395 * for a route to an interface. 396 */ 397 if (rt->rt_flags & RTF_GATEWAY) { 398 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) { 399 /* 400 * Changing from route to net => route to host. 401 * Create new route, rather than smashing route to net. 402 */ 403 create: 404 if (rt) 405 rtfree(rt); 406 flags |= RTF_GATEWAY | RTF_DYNAMIC; 407 info.rti_info[RTAX_DST] = dst; 408 info.rti_info[RTAX_GATEWAY] = gateway; 409 info.rti_info[RTAX_NETMASK] = netmask; 410 info.rti_ifa = ifa; 411 info.rti_flags = flags; 412 rt = NULL; 413 error = rtrequest1(RTM_ADD, &info, &rt); 414 if (rt != NULL) 415 flags = rt->rt_flags; 416 stat = &rtstat.rts_dynamic; 417 } else { 418 /* 419 * Smash the current notion of the gateway to 420 * this destination. Should check about netmask!!! 421 */ 422 rt->rt_flags |= RTF_MODIFIED; 423 flags |= RTF_MODIFIED; 424 stat = &rtstat.rts_newgateway; 425 rt_setgate(rt, rt_key(rt), gateway); 426 } 427 } else 428 error = EHOSTUNREACH; 429 done: 430 if (rt) { 431 if (rtp && !error) 432 *rtp = rt; 433 else 434 rtfree(rt); 435 } 436 out: 437 if (error) 438 rtstat.rts_badredirect++; 439 else if (stat != NULL) 440 (*stat)++; 441 bzero((caddr_t)&info, sizeof(info)); 442 info.rti_info[RTAX_DST] = dst; 443 info.rti_info[RTAX_GATEWAY] = gateway; 444 info.rti_info[RTAX_NETMASK] = netmask; 445 info.rti_info[RTAX_AUTHOR] = src; 446 rt_missmsg(RTM_REDIRECT, &info, flags, error); 447 } 448 449 /* 450 * Routing table ioctl interface. 451 */ 452 int 453 rtioctl(req, data, p) 454 u_long req; 455 caddr_t data; 456 struct proc *p; 457 { 458 return (EOPNOTSUPP); 459 } 460 461 struct ifaddr * 462 ifa_ifwithroute(flags, dst, gateway) 463 int flags; 464 struct sockaddr *dst, *gateway; 465 { 466 register struct ifaddr *ifa; 467 468 #ifdef IPSEC 469 /* 470 * If the destination is a PF_KEY address, we'll look 471 * for the existence of a encap interface number or address 472 * in the options list of the gateway. By default, we'll return 473 * enc0. 474 */ 475 if (dst && (dst->sa_family == PF_KEY)) 476 return encap_findgwifa(gateway); 477 #endif 478 479 if ((flags & RTF_GATEWAY) == 0) { 480 /* 481 * If we are adding a route to an interface, 482 * and the interface is a pt to pt link 483 * we should search for the destination 484 * as our clue to the interface. Otherwise 485 * we can use the local address. 486 */ 487 ifa = NULL; 488 if (flags & RTF_HOST) 489 ifa = ifa_ifwithdstaddr(dst); 490 if (ifa == NULL) 491 ifa = ifa_ifwithaddr(gateway); 492 } else { 493 /* 494 * If we are adding a route to a remote net 495 * or host, the gateway may still be on the 496 * other end of a pt to pt link. 497 */ 498 ifa = ifa_ifwithdstaddr(gateway); 499 } 500 if (ifa == NULL) 501 ifa = ifa_ifwithnet(gateway); 502 if (ifa == NULL) { 503 struct rtentry *rt = rtalloc1(gateway, 0); 504 if (rt == NULL) 505 return (NULL); 506 rt->rt_refcnt--; 507 /* The gateway must be local if the same address family. */ 508 if ((rt->rt_flags & RTF_GATEWAY) && 509 rt_key(rt)->sa_family == dst->sa_family) 510 return (0); 511 if ((ifa = rt->rt_ifa) == NULL) 512 return (NULL); 513 } 514 if (ifa->ifa_addr->sa_family != dst->sa_family) { 515 struct ifaddr *oifa = ifa; 516 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 517 if (ifa == NULL) 518 ifa = oifa; 519 } 520 return (ifa); 521 } 522 523 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 524 525 int 526 rtrequest(req, dst, gateway, netmask, flags, ret_nrt) 527 int req, flags; 528 struct sockaddr *dst, *gateway, *netmask; 529 struct rtentry **ret_nrt; 530 { 531 struct rt_addrinfo info; 532 533 bzero(&info, sizeof(info)); 534 info.rti_flags = flags; 535 info.rti_info[RTAX_DST] = dst; 536 info.rti_info[RTAX_GATEWAY] = gateway; 537 info.rti_info[RTAX_NETMASK] = netmask; 538 return rtrequest1(req, &info, ret_nrt); 539 } 540 541 /* 542 * These (questionable) definitions of apparent local variables apply 543 * to the next function. XXXXXX!!! 544 */ 545 #define dst info->rti_info[RTAX_DST] 546 #define gateway info->rti_info[RTAX_GATEWAY] 547 #define netmask info->rti_info[RTAX_NETMASK] 548 #define ifaaddr info->rti_info[RTAX_IFA] 549 #define ifpaddr info->rti_info[RTAX_IFP] 550 #define flags info->rti_flags 551 552 int 553 rt_getifa(info) 554 struct rt_addrinfo *info; 555 { 556 struct ifaddr *ifa; 557 int error = 0; 558 559 /* 560 * ifp may be specified by sockaddr_dl when protocol address 561 * is ambiguous 562 */ 563 if (info->rti_ifp == NULL && ifpaddr != NULL 564 && ifpaddr->sa_family == AF_LINK && 565 (ifa = ifa_ifwithnet((struct sockaddr *)ifpaddr)) != NULL) 566 info->rti_ifp = ifa->ifa_ifp; 567 if (info->rti_ifa == NULL && ifaaddr != NULL) 568 info->rti_ifa = ifa_ifwithaddr(ifaaddr); 569 if (info->rti_ifa == NULL) { 570 struct sockaddr *sa; 571 572 sa = ifaaddr != NULL ? ifaaddr : 573 (gateway != NULL ? gateway : dst); 574 if (sa != NULL && info->rti_ifp != NULL) 575 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp); 576 else if (dst != NULL && gateway != NULL) 577 info->rti_ifa = ifa_ifwithroute(flags, dst, gateway); 578 else if (sa != NULL) 579 info->rti_ifa = ifa_ifwithroute(flags, sa, sa); 580 } 581 if ((ifa = info->rti_ifa) != NULL) { 582 if (info->rti_ifp == NULL) 583 info->rti_ifp = ifa->ifa_ifp; 584 } else 585 error = ENETUNREACH; 586 return (error); 587 } 588 589 int 590 rtrequest1(req, info, ret_nrt) 591 int req; 592 struct rt_addrinfo *info; 593 struct rtentry **ret_nrt; 594 { 595 int s = splsoftnet(); int error = 0; 596 register struct rtentry *rt; 597 register struct radix_node *rn; 598 register struct radix_node_head *rnh; 599 struct ifaddr *ifa; 600 struct sockaddr *ndst; 601 #define senderr(x) { error = x ; goto bad; } 602 603 if ((rnh = rt_tables[dst->sa_family]) == 0) 604 senderr(EAFNOSUPPORT); 605 if (flags & RTF_HOST) 606 netmask = 0; 607 switch (req) { 608 case RTM_DELETE: 609 if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == NULL) 610 senderr(ESRCH); 611 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 612 panic ("rtrequest delete"); 613 rt = (struct rtentry *)rn; 614 if (rt->rt_gwroute) { 615 rt = rt->rt_gwroute; RTFREE(rt); 616 (rt = (struct rtentry *)rn)->rt_gwroute = NULL; 617 } 618 rt->rt_flags &= ~RTF_UP; 619 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) 620 ifa->ifa_rtrequest(RTM_DELETE, rt, info); 621 rttrash++; 622 if (ret_nrt) 623 *ret_nrt = rt; 624 else if (rt->rt_refcnt <= 0) { 625 rt->rt_refcnt++; 626 rtfree(rt); 627 } 628 break; 629 630 case RTM_RESOLVE: 631 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL) 632 senderr(EINVAL); 633 ifa = rt->rt_ifa; 634 flags = rt->rt_flags & ~RTF_CLONING; 635 gateway = rt->rt_gateway; 636 if ((netmask = rt->rt_genmask) == NULL) 637 flags |= RTF_HOST; 638 goto makeroute; 639 640 case RTM_ADD: 641 if (info->rti_ifa == 0 && (error = rt_getifa(info))) 642 senderr(error); 643 ifa = info->rti_ifa; 644 makeroute: 645 R_Malloc(rt, struct rtentry *, sizeof(*rt)); 646 if (rt == NULL) 647 senderr(ENOBUFS); 648 Bzero(rt, sizeof(*rt)); 649 rt->rt_flags = RTF_UP | flags; 650 LIST_INIT(&rt->rt_timer); 651 if (rt_setgate(rt, dst, gateway)) { 652 Free(rt); 653 senderr(ENOBUFS); 654 } 655 ndst = rt_key(rt); 656 if (netmask) { 657 rt_maskedcopy(dst, ndst, netmask); 658 } else 659 Bcopy(dst, ndst, dst->sa_len); 660 rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask, 661 rnh, rt->rt_nodes); 662 if (rn == NULL) { 663 if (rt->rt_gwroute) 664 rtfree(rt->rt_gwroute); 665 Free(rt_key(rt)); 666 Free(rt); 667 senderr(EEXIST); 668 } 669 ifa->ifa_refcnt++; 670 rt->rt_ifa = ifa; 671 rt->rt_ifp = ifa->ifa_ifp; 672 if (req == RTM_RESOLVE) { 673 /* 674 * Copy both metrics and a back pointer to the cloned 675 * route's parent. 676 */ 677 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */ 678 rt->rt_parent = *ret_nrt; /* Back ptr. to parent. */ 679 } else if (!rt->rt_rmx.rmx_mtu && 680 !(rt->rt_rmx.rmx_locks & RTV_MTU)) { /* XXX */ 681 if (rt->rt_gwroute) { 682 rt->rt_rmx.rmx_mtu = 683 rt->rt_gwroute->rt_rmx.rmx_mtu; 684 } else { 685 rt->rt_rmx.rmx_mtu = ifa->ifa_ifp->if_mtu; 686 } 687 } 688 if (ifa->ifa_rtrequest) 689 ifa->ifa_rtrequest(req, rt, info); 690 if (ret_nrt) { 691 *ret_nrt = rt; 692 rt->rt_refcnt++; 693 } 694 break; 695 } 696 bad: 697 splx(s); 698 return (error); 699 } 700 701 #undef dst 702 #undef gateway 703 #undef netmask 704 #undef ifaaddr 705 #undef ifpaddr 706 #undef flags 707 708 int 709 rt_setgate(rt0, dst, gate) 710 struct rtentry *rt0; 711 struct sockaddr *dst, *gate; 712 { 713 caddr_t new, old; 714 int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len); 715 register struct rtentry *rt = rt0; 716 717 if (rt->rt_gateway == NULL || glen > ROUNDUP(rt->rt_gateway->sa_len)) { 718 old = (caddr_t)rt_key(rt); 719 R_Malloc(new, caddr_t, dlen + glen); 720 if (new == NULL) 721 return 1; 722 rt->rt_nodes->rn_key = new; 723 } else { 724 new = rt->rt_nodes->rn_key; 725 old = NULL; 726 } 727 Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen); 728 if (old) { 729 Bcopy(dst, new, dlen); 730 Free(old); 731 } 732 if (rt->rt_gwroute != NULL) { 733 rt = rt->rt_gwroute; 734 RTFREE(rt); 735 rt = rt0; 736 rt->rt_gwroute = NULL; 737 } 738 if (rt->rt_flags & RTF_GATEWAY) { 739 rt->rt_gwroute = rtalloc1(gate, 1); 740 /* 741 * If we switched gateways, grab the MTU from the new 742 * gateway route if the current MTU is 0 or greater 743 * than the MTU of gateway. 744 */ 745 if (rt->rt_gwroute && !(rt->rt_rmx.rmx_locks & RTV_MTU) && 746 (rt->rt_rmx.rmx_mtu == 0 || 747 rt->rt_rmx.rmx_mtu > rt->rt_gwroute->rt_rmx.rmx_mtu)) { 748 rt->rt_rmx.rmx_mtu = rt->rt_gwroute->rt_rmx.rmx_mtu; 749 } 750 } 751 return 0; 752 } 753 754 void 755 rt_maskedcopy(src, dst, netmask) 756 struct sockaddr *src, *dst, *netmask; 757 { 758 register u_char *cp1 = (u_char *)src; 759 register u_char *cp2 = (u_char *)dst; 760 register u_char *cp3 = (u_char *)netmask; 761 u_char *cplim = cp2 + *cp3; 762 u_char *cplim2 = cp2 + *cp1; 763 764 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */ 765 cp3 += 2; 766 if (cplim > cplim2) 767 cplim = cplim2; 768 while (cp2 < cplim) 769 *cp2++ = *cp1++ & *cp3++; 770 if (cp2 < cplim2) 771 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2)); 772 } 773 774 /* 775 * Set up a routing table entry, normally 776 * for an interface. 777 */ 778 int 779 rtinit(ifa, cmd, flags) 780 register struct ifaddr *ifa; 781 int cmd, flags; 782 { 783 register struct rtentry *rt; 784 register struct sockaddr *dst; 785 register struct sockaddr *deldst; 786 struct mbuf *m = NULL; 787 struct rtentry *nrt = NULL; 788 int error; 789 struct rt_addrinfo info; 790 791 dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr; 792 if (cmd == RTM_DELETE) { 793 if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) { 794 m = m_get(M_DONTWAIT, MT_SONAME); 795 if (m == NULL) 796 return(ENOBUFS); 797 deldst = mtod(m, struct sockaddr *); 798 rt_maskedcopy(dst, deldst, ifa->ifa_netmask); 799 dst = deldst; 800 } 801 if ((rt = rtalloc1(dst, 0)) != NULL) { 802 rt->rt_refcnt--; 803 if (rt->rt_ifa != ifa) { 804 if (m != NULL) 805 (void) m_free(m); 806 return (flags & RTF_HOST ? EHOSTUNREACH 807 : ENETUNREACH); 808 } 809 } 810 } 811 bzero(&info, sizeof(info)); 812 info.rti_ifa = ifa; 813 info.rti_flags = flags | ifa->ifa_flags; 814 info.rti_info[RTAX_DST] = dst; 815 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr; 816 /* 817 * XXX here, it seems that we are assuming that ifa_netmask is NULL 818 * for RTF_HOST. bsdi4 passes NULL explicitly (via intermediate 819 * variable) when RTF_HOST is 1. still not sure if i can safely 820 * change it to meet bsdi4 behavior. 821 */ 822 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 823 error = rtrequest1(cmd, &info, &nrt); 824 if (cmd == RTM_DELETE && error == 0 && (rt = nrt) != NULL) { 825 rt_newaddrmsg(cmd, ifa, error, nrt); 826 if (rt->rt_refcnt <= 0) { 827 rt->rt_refcnt++; 828 rtfree(rt); 829 } 830 } 831 if (cmd == RTM_ADD && error == 0 && (rt = nrt) != NULL) { 832 rt->rt_refcnt--; 833 if (rt->rt_ifa != ifa) { 834 printf("rtinit: wrong ifa (%p) was (%p)\n", 835 ifa, rt->rt_ifa); 836 if (rt->rt_ifa->ifa_rtrequest) 837 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, NULL); 838 IFAFREE(rt->rt_ifa); 839 rt->rt_ifa = ifa; 840 rt->rt_ifp = ifa->ifa_ifp; 841 rt->rt_rmx.rmx_mtu = ifa->ifa_ifp->if_mtu; /*XXX*/ 842 ifa->ifa_refcnt++; 843 if (ifa->ifa_rtrequest) 844 ifa->ifa_rtrequest(RTM_ADD, rt, NULL); 845 } 846 rt_newaddrmsg(cmd, ifa, error, nrt); 847 } 848 return (error); 849 } 850 851 /* 852 * Route timer routines. These routes allow functions to be called 853 * for various routes at any time. This is useful in supporting 854 * path MTU discovery and redirect route deletion. 855 * 856 * This is similar to some BSDI internal functions, but it provides 857 * for multiple queues for efficiency's sake... 858 */ 859 860 LIST_HEAD(, rttimer_queue) rttimer_queue_head; 861 static int rt_init_done = 0; 862 863 #define RTTIMER_CALLOUT(r) { \ 864 if (r->rtt_func != NULL) { \ 865 (*r->rtt_func)(r->rtt_rt, r); \ 866 } else { \ 867 rtrequest((int) RTM_DELETE, \ 868 (struct sockaddr *)rt_key(r->rtt_rt), \ 869 0, 0, 0, 0); \ 870 } \ 871 } 872 873 /* 874 * Some subtle order problems with domain initialization mean that 875 * we cannot count on this being run from rt_init before various 876 * protocol initializations are done. Therefore, we make sure 877 * that this is run when the first queue is added... 878 */ 879 880 void 881 rt_timer_init() 882 { 883 static struct timeout rt_timer_timeout; 884 885 assert(rt_init_done == 0); 886 887 #if 0 888 pool_init(&rttimer_pool, sizeof(struct rttimer), 0, 0, 0, "rttmrpl", 889 0, NULL, NULL, M_RTABLE); 890 #endif 891 892 LIST_INIT(&rttimer_queue_head); 893 timeout_set(&rt_timer_timeout, rt_timer_timer, &rt_timer_timeout); 894 timeout_add(&rt_timer_timeout, hz); /* every second */ 895 rt_init_done = 1; 896 } 897 898 struct rttimer_queue * 899 rt_timer_queue_create(timeout) 900 u_int timeout; 901 { 902 struct rttimer_queue *rtq; 903 904 if (rt_init_done == 0) 905 rt_timer_init(); 906 907 R_Malloc(rtq, struct rttimer_queue *, sizeof *rtq); 908 if (rtq == NULL) 909 return (NULL); 910 Bzero(rtq, sizeof *rtq); 911 912 rtq->rtq_timeout = timeout; 913 rtq->rtq_count = 0; 914 TAILQ_INIT(&rtq->rtq_head); 915 LIST_INSERT_HEAD(&rttimer_queue_head, rtq, rtq_link); 916 917 return (rtq); 918 } 919 920 void 921 rt_timer_queue_change(rtq, timeout) 922 struct rttimer_queue *rtq; 923 long timeout; 924 { 925 926 rtq->rtq_timeout = timeout; 927 } 928 929 void 930 rt_timer_queue_destroy(rtq, destroy) 931 struct rttimer_queue *rtq; 932 int destroy; 933 { 934 struct rttimer *r; 935 936 while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL) { 937 LIST_REMOVE(r, rtt_link); 938 TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next); 939 if (destroy) 940 RTTIMER_CALLOUT(r); 941 #if 0 942 pool_put(&rttimer_pool, r); 943 #else 944 free(r, M_RTABLE); 945 #endif 946 if (rtq->rtq_count > 0) 947 rtq->rtq_count--; 948 else 949 printf("rt_timer_queue_destroy: rtq_count reached 0\n"); 950 } 951 952 LIST_REMOVE(rtq, rtq_link); 953 954 /* 955 * Caller is responsible for freeing the rttimer_queue structure. 956 */ 957 } 958 959 unsigned long 960 rt_timer_count(rtq) 961 struct rttimer_queue *rtq; 962 { 963 964 return rtq->rtq_count; 965 } 966 967 void 968 rt_timer_remove_all(rt) 969 struct rtentry *rt; 970 { 971 struct rttimer *r; 972 973 while ((r = LIST_FIRST(&rt->rt_timer)) != NULL) { 974 LIST_REMOVE(r, rtt_link); 975 TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next); 976 if (r->rtt_queue->rtq_count > 0) 977 r->rtt_queue->rtq_count--; 978 else 979 printf("rt_timer_remove_all: rtq_count reached 0\n"); 980 #if 0 981 pool_put(&rttimer_pool, r); 982 #else 983 free(r, M_RTABLE); 984 #endif 985 } 986 } 987 988 int 989 rt_timer_add(rt, func, queue) 990 struct rtentry *rt; 991 void(*func) __P((struct rtentry *, struct rttimer *)); 992 struct rttimer_queue *queue; 993 { 994 struct rttimer *r; 995 long current_time; 996 int s; 997 998 s = splclock(); 999 current_time = mono_time.tv_sec; 1000 splx(s); 1001 1002 /* 1003 * If there's already a timer with this action, destroy it before 1004 * we add a new one. 1005 */ 1006 for (r = LIST_FIRST(&rt->rt_timer); r != NULL; 1007 r = LIST_NEXT(r, rtt_link)) { 1008 if (r->rtt_func == func) { 1009 LIST_REMOVE(r, rtt_link); 1010 TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next); 1011 if (r->rtt_queue->rtq_count > 0) 1012 r->rtt_queue->rtq_count--; 1013 else 1014 printf("rt_timer_add: rtq_count reached 0\n"); 1015 #if 0 1016 pool_put(&rttimer_pool, r); 1017 #else 1018 free(r, M_RTABLE); 1019 #endif 1020 break; /* only one per list, so we can quit... */ 1021 } 1022 } 1023 1024 #if 0 1025 r = pool_get(&rttimer_pool, PR_NOWAIT); 1026 #else 1027 r = (struct rttimer *)malloc(sizeof(*r), M_RTABLE, M_NOWAIT); 1028 #endif 1029 if (r == NULL) 1030 return (ENOBUFS); 1031 Bzero(r, sizeof(*r)); 1032 1033 r->rtt_rt = rt; 1034 r->rtt_time = current_time; 1035 r->rtt_func = func; 1036 r->rtt_queue = queue; 1037 LIST_INSERT_HEAD(&rt->rt_timer, r, rtt_link); 1038 TAILQ_INSERT_TAIL(&queue->rtq_head, r, rtt_next); 1039 r->rtt_queue->rtq_count++; 1040 1041 return (0); 1042 } 1043 1044 /* ARGSUSED */ 1045 void 1046 rt_timer_timer(arg) 1047 void *arg; 1048 { 1049 struct timeout *to = (struct timeout *)arg; 1050 struct rttimer_queue *rtq; 1051 struct rttimer *r; 1052 long current_time; 1053 int s; 1054 1055 s = splclock(); 1056 current_time = mono_time.tv_sec; 1057 splx(s); 1058 1059 s = splsoftnet(); 1060 for (rtq = LIST_FIRST(&rttimer_queue_head); rtq != NULL; 1061 rtq = LIST_NEXT(rtq, rtq_link)) { 1062 while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL && 1063 (r->rtt_time + rtq->rtq_timeout) < current_time) { 1064 LIST_REMOVE(r, rtt_link); 1065 TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next); 1066 RTTIMER_CALLOUT(r); 1067 #if 0 1068 pool_put(&rttimer_pool, r); 1069 #else 1070 free(r, M_RTABLE); 1071 #endif 1072 if (rtq->rtq_count > 0) 1073 rtq->rtq_count--; 1074 else 1075 printf("rt_timer_timer: rtq_count reached 0\n"); 1076 } 1077 } 1078 splx(s); 1079 1080 timeout_add(to, hz); /* every second */ 1081 } 1082