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