1 /* $NetBSD: route.c,v 1.17 1997/04/02 21:17:28 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1986, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)route.c 8.3 (Berkeley) 1/9/95 36 */ 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/proc.h> 41 #include <sys/mbuf.h> 42 #include <sys/socket.h> 43 #include <sys/socketvar.h> 44 #include <sys/domain.h> 45 #include <sys/protosw.h> 46 #include <sys/ioctl.h> 47 48 #include <net/if.h> 49 #include <net/route.h> 50 #include <net/raw_cb.h> 51 52 #include <netinet/in.h> 53 #include <netinet/in_var.h> 54 55 #ifdef NS 56 #include <netns/ns.h> 57 #endif 58 59 #define SA(p) ((struct sockaddr *)(p)) 60 61 int rttrash; /* routes not in table but not freed */ 62 struct sockaddr wildcard; /* zero valued cookie for wildcard searches */ 63 64 void 65 rtable_init(table) 66 void **table; 67 { 68 struct domain *dom; 69 for (dom = domains; dom; dom = dom->dom_next) 70 if (dom->dom_rtattach) 71 dom->dom_rtattach(&table[dom->dom_family], 72 dom->dom_rtoffset); 73 } 74 75 void 76 route_init() 77 { 78 rn_init(); /* initialize all zeroes, all ones, mask table */ 79 rtable_init((void **)rt_tables); 80 } 81 82 /* 83 * Packet routing routines. 84 */ 85 void 86 rtalloc(ro) 87 register struct route *ro; 88 { 89 if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP)) 90 return; /* XXX */ 91 ro->ro_rt = rtalloc1(&ro->ro_dst, 1); 92 } 93 94 struct rtentry * 95 rtalloc1(dst, report) 96 register struct sockaddr *dst; 97 int report; 98 { 99 register struct radix_node_head *rnh = rt_tables[dst->sa_family]; 100 register struct rtentry *rt; 101 register struct radix_node *rn; 102 struct rtentry *newrt = 0; 103 struct rt_addrinfo info; 104 int s = splsoftnet(), err = 0, msgtype = RTM_MISS; 105 106 if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) && 107 ((rn->rn_flags & RNF_ROOT) == 0)) { 108 newrt = rt = (struct rtentry *)rn; 109 if (report && (rt->rt_flags & RTF_CLONING)) { 110 err = rtrequest(RTM_RESOLVE, dst, SA(0), 111 SA(0), 0, &newrt); 112 if (err) { 113 newrt = rt; 114 rt->rt_refcnt++; 115 goto miss; 116 } 117 if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) { 118 msgtype = RTM_RESOLVE; 119 goto miss; 120 } 121 } else 122 rt->rt_refcnt++; 123 } else { 124 rtstat.rts_unreach++; 125 miss: if (report) { 126 bzero((caddr_t)&info, sizeof(info)); 127 info.rti_info[RTAX_DST] = dst; 128 rt_missmsg(msgtype, &info, 0, err); 129 } 130 } 131 splx(s); 132 return (newrt); 133 } 134 135 void 136 rtfree(rt) 137 register struct rtentry *rt; 138 { 139 register struct ifaddr *ifa; 140 141 if (rt == 0) 142 panic("rtfree"); 143 rt->rt_refcnt--; 144 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) { 145 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 146 panic ("rtfree 2"); 147 rttrash--; 148 if (rt->rt_refcnt < 0) { 149 printf("rtfree: %p not freed (neg refs)\n", rt); 150 return; 151 } 152 ifa = rt->rt_ifa; 153 IFAFREE(ifa); 154 Free(rt_key(rt)); 155 Free(rt); 156 } 157 } 158 159 void 160 ifafree(ifa) 161 register struct ifaddr *ifa; 162 { 163 if (ifa == NULL) 164 panic("ifafree"); 165 if (ifa->ifa_refcnt == 0) 166 free(ifa, M_IFADDR); 167 else 168 ifa->ifa_refcnt--; 169 } 170 171 /* 172 * Force a routing table entry to the specified 173 * destination to go through the given gateway. 174 * Normally called as a result of a routing redirect 175 * message from the network layer. 176 * 177 * N.B.: must be called at splsoftnet 178 * 179 */ 180 void 181 rtredirect(dst, gateway, netmask, flags, src, rtp) 182 struct sockaddr *dst, *gateway, *netmask, *src; 183 int flags; 184 struct rtentry **rtp; 185 { 186 register struct rtentry *rt; 187 int error = 0; 188 short *stat = 0; 189 struct rt_addrinfo info; 190 struct ifaddr *ifa; 191 192 /* verify the gateway is directly reachable */ 193 if ((ifa = ifa_ifwithnet(gateway)) == 0) { 194 error = ENETUNREACH; 195 goto out; 196 } 197 rt = rtalloc1(dst, 0); 198 /* 199 * If the redirect isn't from our current router for this dst, 200 * it's either old or wrong. If it redirects us to ourselves, 201 * we have a routing loop, perhaps as a result of an interface 202 * going down recently. 203 */ 204 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0) 205 if (!(flags & RTF_DONE) && rt && 206 (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) 207 error = EINVAL; 208 else if (ifa_ifwithaddr(gateway)) 209 error = EHOSTUNREACH; 210 if (error) 211 goto done; 212 /* 213 * Create a new entry if we just got back a wildcard entry 214 * or the the lookup failed. This is necessary for hosts 215 * which use routing redirects generated by smart gateways 216 * to dynamically build the routing tables. 217 */ 218 if ((rt == 0) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2)) 219 goto create; 220 /* 221 * Don't listen to the redirect if it's 222 * for a route to an interface. 223 */ 224 if (rt->rt_flags & RTF_GATEWAY) { 225 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) { 226 /* 227 * Changing from route to net => route to host. 228 * Create new route, rather than smashing route to net. 229 */ 230 create: 231 flags |= RTF_GATEWAY | RTF_DYNAMIC; 232 error = rtrequest((int)RTM_ADD, dst, gateway, 233 netmask, flags, 234 (struct rtentry **)0); 235 stat = &rtstat.rts_dynamic; 236 } else { 237 /* 238 * Smash the current notion of the gateway to 239 * this destination. Should check about netmask!!! 240 */ 241 rt->rt_flags |= RTF_MODIFIED; 242 flags |= RTF_MODIFIED; 243 stat = &rtstat.rts_newgateway; 244 rt_setgate(rt, rt_key(rt), gateway); 245 } 246 } else 247 error = EHOSTUNREACH; 248 done: 249 if (rt) { 250 if (rtp && !error) 251 *rtp = rt; 252 else 253 rtfree(rt); 254 } 255 out: 256 if (error) 257 rtstat.rts_badredirect++; 258 else if (stat != NULL) 259 (*stat)++; 260 bzero((caddr_t)&info, sizeof(info)); 261 info.rti_info[RTAX_DST] = dst; 262 info.rti_info[RTAX_GATEWAY] = gateway; 263 info.rti_info[RTAX_NETMASK] = netmask; 264 info.rti_info[RTAX_AUTHOR] = src; 265 rt_missmsg(RTM_REDIRECT, &info, flags, error); 266 } 267 268 /* 269 * Routing table ioctl interface. 270 */ 271 int 272 rtioctl(req, data, p) 273 u_long req; 274 caddr_t data; 275 struct proc *p; 276 { 277 return (EOPNOTSUPP); 278 } 279 280 struct ifaddr * 281 ifa_ifwithroute(flags, dst, gateway) 282 int flags; 283 struct sockaddr *dst, *gateway; 284 { 285 register struct ifaddr *ifa; 286 if ((flags & RTF_GATEWAY) == 0) { 287 /* 288 * If we are adding a route to an interface, 289 * and the interface is a pt to pt link 290 * we should search for the destination 291 * as our clue to the interface. Otherwise 292 * we can use the local address. 293 */ 294 ifa = 0; 295 if (flags & RTF_HOST) 296 ifa = ifa_ifwithdstaddr(dst); 297 if (ifa == 0) 298 ifa = ifa_ifwithaddr(gateway); 299 } else { 300 /* 301 * If we are adding a route to a remote net 302 * or host, the gateway may still be on the 303 * other end of a pt to pt link. 304 */ 305 ifa = ifa_ifwithdstaddr(gateway); 306 } 307 if (ifa == 0) 308 ifa = ifa_ifwithnet(gateway); 309 if (ifa == 0) { 310 struct rtentry *rt = rtalloc1(dst, 0); 311 if (rt == 0) 312 return (0); 313 rt->rt_refcnt--; 314 if ((ifa = rt->rt_ifa) == 0) 315 return (0); 316 } 317 if (ifa->ifa_addr->sa_family != dst->sa_family) { 318 struct ifaddr *oifa = ifa; 319 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 320 if (ifa == 0) 321 ifa = oifa; 322 } 323 return (ifa); 324 } 325 326 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 327 328 int 329 rtrequest(req, dst, gateway, netmask, flags, ret_nrt) 330 int req, flags; 331 struct sockaddr *dst, *gateway, *netmask; 332 struct rtentry **ret_nrt; 333 { 334 int s = splsoftnet(); int error = 0; 335 register struct rtentry *rt; 336 register struct radix_node *rn; 337 register struct radix_node_head *rnh; 338 struct ifaddr *ifa; 339 struct sockaddr *ndst; 340 #define senderr(x) { error = x ; goto bad; } 341 342 if ((rnh = rt_tables[dst->sa_family]) == 0) 343 senderr(ESRCH); 344 if (flags & RTF_HOST) 345 netmask = 0; 346 switch (req) { 347 case RTM_DELETE: 348 if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == 0) 349 senderr(ESRCH); 350 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 351 panic ("rtrequest delete"); 352 rt = (struct rtentry *)rn; 353 rt->rt_flags &= ~RTF_UP; 354 if (rt->rt_gwroute) { 355 rt = rt->rt_gwroute; RTFREE(rt); 356 (rt = (struct rtentry *)rn)->rt_gwroute = 0; 357 } 358 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) 359 ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0)); 360 rttrash++; 361 if (ret_nrt) 362 *ret_nrt = rt; 363 else if (rt->rt_refcnt <= 0) { 364 rt->rt_refcnt++; 365 rtfree(rt); 366 } 367 break; 368 369 case RTM_RESOLVE: 370 if (ret_nrt == 0 || (rt = *ret_nrt) == 0) 371 senderr(EINVAL); 372 ifa = rt->rt_ifa; 373 flags = rt->rt_flags & ~RTF_CLONING; 374 gateway = rt->rt_gateway; 375 if ((netmask = rt->rt_genmask) == 0) 376 flags |= RTF_HOST; 377 goto makeroute; 378 379 case RTM_ADD: 380 if ((ifa = ifa_ifwithroute(flags, dst, gateway)) == 0) 381 senderr(ENETUNREACH); 382 makeroute: 383 R_Malloc(rt, struct rtentry *, sizeof(*rt)); 384 if (rt == 0) 385 senderr(ENOBUFS); 386 Bzero(rt, sizeof(*rt)); 387 rt->rt_flags = RTF_UP | flags; 388 if (rt_setgate(rt, dst, gateway)) { 389 Free(rt); 390 senderr(ENOBUFS); 391 } 392 ndst = rt_key(rt); 393 if (netmask) { 394 rt_maskedcopy(dst, ndst, netmask); 395 } else 396 Bcopy(dst, ndst, dst->sa_len); 397 rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask, 398 rnh, rt->rt_nodes); 399 if (rn == 0) { 400 if (rt->rt_gwroute) 401 rtfree(rt->rt_gwroute); 402 Free(rt_key(rt)); 403 Free(rt); 404 senderr(EEXIST); 405 } 406 ifa->ifa_refcnt++; 407 rt->rt_ifa = ifa; 408 rt->rt_ifp = ifa->ifa_ifp; 409 if (req == RTM_RESOLVE) 410 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */ 411 if (ifa->ifa_rtrequest) 412 ifa->ifa_rtrequest(req, rt, SA(ret_nrt ? *ret_nrt : 0)); 413 if (ret_nrt) { 414 *ret_nrt = rt; 415 rt->rt_refcnt++; 416 } 417 break; 418 } 419 bad: 420 splx(s); 421 return (error); 422 } 423 424 int 425 rt_setgate(rt0, dst, gate) 426 struct rtentry *rt0; 427 struct sockaddr *dst, *gate; 428 { 429 caddr_t new, old; 430 int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len); 431 register struct rtentry *rt = rt0; 432 433 if (rt->rt_gateway == 0 || glen > ROUNDUP(rt->rt_gateway->sa_len)) { 434 old = (caddr_t)rt_key(rt); 435 R_Malloc(new, caddr_t, dlen + glen); 436 if (new == 0) 437 return 1; 438 rt->rt_nodes->rn_key = new; 439 } else { 440 new = rt->rt_nodes->rn_key; 441 old = 0; 442 } 443 Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen); 444 if (old) { 445 Bcopy(dst, new, dlen); 446 Free(old); 447 } 448 if (rt->rt_gwroute) { 449 rt = rt->rt_gwroute; RTFREE(rt); 450 rt = rt0; rt->rt_gwroute = 0; 451 } 452 if (rt->rt_flags & RTF_GATEWAY) { 453 rt->rt_gwroute = rtalloc1(gate, 1); 454 } 455 return 0; 456 } 457 458 void 459 rt_maskedcopy(src, dst, netmask) 460 struct sockaddr *src, *dst, *netmask; 461 { 462 register u_char *cp1 = (u_char *)src; 463 register u_char *cp2 = (u_char *)dst; 464 register u_char *cp3 = (u_char *)netmask; 465 u_char *cplim = cp2 + *cp3; 466 u_char *cplim2 = cp2 + *cp1; 467 468 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */ 469 cp3 += 2; 470 if (cplim > cplim2) 471 cplim = cplim2; 472 while (cp2 < cplim) 473 *cp2++ = *cp1++ & *cp3++; 474 if (cp2 < cplim2) 475 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2)); 476 } 477 478 /* 479 * Set up a routing table entry, normally 480 * for an interface. 481 */ 482 int 483 rtinit(ifa, cmd, flags) 484 register struct ifaddr *ifa; 485 int cmd, flags; 486 { 487 register struct rtentry *rt; 488 register struct sockaddr *dst; 489 register struct sockaddr *deldst; 490 struct mbuf *m = 0; 491 struct rtentry *nrt = 0; 492 int error; 493 494 dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr; 495 if (cmd == RTM_DELETE) { 496 if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) { 497 m = m_get(M_WAIT, MT_SONAME); 498 deldst = mtod(m, struct sockaddr *); 499 rt_maskedcopy(dst, deldst, ifa->ifa_netmask); 500 dst = deldst; 501 } 502 if ((rt = rtalloc1(dst, 0)) != NULL) { 503 rt->rt_refcnt--; 504 if (rt->rt_ifa != ifa) { 505 if (m) 506 (void) m_free(m); 507 return (flags & RTF_HOST ? EHOSTUNREACH 508 : ENETUNREACH); 509 } 510 } 511 } 512 error = rtrequest(cmd, dst, ifa->ifa_addr, ifa->ifa_netmask, 513 flags | ifa->ifa_flags, &nrt); 514 if (m) 515 (void) m_free(m); 516 if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) { 517 rt_newaddrmsg(cmd, ifa, error, nrt); 518 if (rt->rt_refcnt <= 0) { 519 rt->rt_refcnt++; 520 rtfree(rt); 521 } 522 } 523 if (cmd == RTM_ADD && error == 0 && (rt = nrt)) { 524 rt->rt_refcnt--; 525 if (rt->rt_ifa != ifa) { 526 printf("rtinit: wrong ifa (%p) was (%p)\n", ifa, 527 rt->rt_ifa); 528 if (rt->rt_ifa->ifa_rtrequest) 529 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0)); 530 IFAFREE(rt->rt_ifa); 531 rt->rt_ifa = ifa; 532 rt->rt_ifp = ifa->ifa_ifp; 533 ifa->ifa_refcnt++; 534 if (ifa->ifa_rtrequest) 535 ifa->ifa_rtrequest(RTM_ADD, rt, SA(0)); 536 } 537 rt_newaddrmsg(cmd, ifa, error, nrt); 538 } 539 return (error); 540 } 541