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