FreeBSD/Linux Kernel Cross Reference
sys/net/route.c
1 /*
2 * Copyright (c) 1980, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)route.c 8.3 (Berkeley) 1/9/95
34 * $FreeBSD$
35 */
36
37 #include "opt_inet.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/domain.h>
45 #include <sys/kernel.h>
46
47 #include <net/if.h>
48 #include <net/route.h>
49
50 #include <netinet/in.h>
51 #include <netinet/ip_mroute.h>
52
53 #define SA(p) ((struct sockaddr *)(p))
54
55 struct route_cb route_cb;
56 static struct rtstat rtstat;
57 struct radix_node_head *rt_tables[AF_MAX+1];
58
59 static int rttrash; /* routes not in table but not freed */
60
61 static void rt_maskedcopy __P((struct sockaddr *,
62 struct sockaddr *, struct sockaddr *));
63 static void rtable_init __P((void **));
64
65 static void
66 rtable_init(table)
67 void **table;
68 {
69 struct domain *dom;
70 for (dom = domains; dom; dom = dom->dom_next)
71 if (dom->dom_rtattach)
72 dom->dom_rtattach(&table[dom->dom_family],
73 dom->dom_rtoffset);
74 }
75
76 void
77 route_init()
78 {
79 rn_init(); /* initialize all zeroes, all ones, mask table */
80 rtable_init((void **)rt_tables);
81 }
82
83 /*
84 * Packet routing routines.
85 */
86 void
87 rtalloc(ro)
88 register struct route *ro;
89 {
90 rtalloc_ign(ro, 0UL);
91 }
92
93 void
94 rtalloc_ign(ro, ignore)
95 register struct route *ro;
96 u_long ignore;
97 {
98 struct rtentry *rt;
99 int s;
100
101 if ((rt = ro->ro_rt) != NULL) {
102 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
103 return;
104 /* XXX - We are probably always at splnet here already. */
105 s = splnet();
106 RTFREE(rt);
107 ro->ro_rt = NULL;
108 splx(s);
109 }
110 ro->ro_rt = rtalloc1(&ro->ro_dst, 1, ignore);
111 }
112
113 /*
114 * Look up the route that matches the address given
115 * Or, at least try.. Create a cloned route if needed.
116 */
117 struct rtentry *
118 rtalloc1(dst, report, ignflags)
119 register struct sockaddr *dst;
120 int report;
121 u_long ignflags;
122 {
123 register struct radix_node_head *rnh = rt_tables[dst->sa_family];
124 register struct rtentry *rt;
125 register struct radix_node *rn;
126 struct rtentry *newrt = 0;
127 struct rt_addrinfo info;
128 u_long nflags;
129 int s = splnet(), err = 0, msgtype = RTM_MISS;
130
131 /*
132 * Look up the address in the table for that Address Family
133 */
134 if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) &&
135 ((rn->rn_flags & RNF_ROOT) == 0)) {
136 /*
137 * If we find it and it's not the root node, then
138 * get a refernce on the rtentry associated.
139 */
140 newrt = rt = (struct rtentry *)rn;
141 nflags = rt->rt_flags & ~ignflags;
142 if (report && (nflags & (RTF_CLONING | RTF_PRCLONING))) {
143 /*
144 * We are apparently adding (report = 0 in delete).
145 * If it requires that it be cloned, do so.
146 * (This implies it wasn't a HOST route.)
147 */
148 err = rtrequest(RTM_RESOLVE, dst, SA(0),
149 SA(0), 0, &newrt);
150 if (err) {
151 /*
152 * If the cloning didn't succeed, maybe
153 * what we have will do. Return that.
154 */
155 newrt = rt;
156 rt->rt_refcnt++;
157 goto miss;
158 }
159 if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) {
160 /*
161 * If the new route specifies it be
162 * externally resolved, then go do that.
163 */
164 msgtype = RTM_RESOLVE;
165 goto miss;
166 }
167 /* Inform listeners of the new route. */
168 bzero(&info, sizeof(info));
169 info.rti_info[RTAX_DST] = rt_key(rt);
170 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
171 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
172 if (rt->rt_ifp != NULL) {
173 info.rti_info[RTAX_IFP] =
174 TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr;
175 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
176 }
177 rt_missmsg(RTM_ADD, &info, rt->rt_flags, 0);
178 } else
179 rt->rt_refcnt++;
180 } else {
181 /*
182 * Either we hit the root or couldn't find any match,
183 * Which basically means
184 * "caint get there frm here"
185 */
186 rtstat.rts_unreach++;
187 miss: if (report) {
188 /*
189 * If required, report the failure to the supervising
190 * Authorities.
191 * For a delete, this is not an error. (report == 0)
192 */
193 bzero((caddr_t)&info, sizeof(info));
194 info.rti_info[RTAX_DST] = dst;
195 rt_missmsg(msgtype, &info, 0, err);
196 }
197 }
198 splx(s);
199 return (newrt);
200 }
201
202 /*
203 * Remove a reference count from an rtentry.
204 * If the count gets low enough, take it out of the routing table
205 */
206 void
207 rtfree(rt)
208 register struct rtentry *rt;
209 {
210 /*
211 * find the tree for that address family
212 */
213 register struct radix_node_head *rnh =
214 rt_tables[rt_key(rt)->sa_family];
215 register struct ifaddr *ifa;
216
217 if (rt == 0 || rnh == 0)
218 panic("rtfree");
219
220 /*
221 * decrement the reference count by one and if it reaches 0,
222 * and there is a close function defined, call the close function
223 */
224 rt->rt_refcnt--;
225 if(rnh->rnh_close && rt->rt_refcnt == 0) {
226 rnh->rnh_close((struct radix_node *)rt, rnh);
227 }
228
229 /*
230 * If we are no longer "up" (and ref == 0)
231 * then we can free the resources associated
232 * with the route.
233 */
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 /*
238 * the rtentry must have been removed from the routing table
239 * so it is represented in rttrash.. remove that now.
240 */
241 rttrash--;
242
243 #ifdef DIAGNOSTIC
244 if (rt->rt_refcnt < 0) {
245 printf("rtfree: %p not freed (neg refs)\n", rt);
246 return;
247 }
248 #endif
249
250 /*
251 * release references on items we hold them on..
252 * e.g other routes and ifaddrs.
253 */
254 if((ifa = rt->rt_ifa))
255 IFAFREE(ifa);
256 if (rt->rt_parent) {
257 RTFREE(rt->rt_parent);
258 }
259
260 /*
261 * The key is separatly alloc'd so free it (see rt_setgate()).
262 * This also frees the gateway, as they are always malloc'd
263 * together.
264 */
265 Free(rt_key(rt));
266
267 /*
268 * and the rtentry itself of course
269 */
270 Free(rt);
271 }
272 }
273
274 void
275 ifafree(ifa)
276 register struct ifaddr *ifa;
277 {
278 if (ifa == NULL)
279 panic("ifafree");
280 if (ifa->ifa_refcnt == 0)
281 free(ifa, M_IFADDR);
282 else
283 ifa->ifa_refcnt--;
284 }
285
286 /*
287 * Force a routing table entry to the specified
288 * destination to go through the given gateway.
289 * Normally called as a result of a routing redirect
290 * message from the network layer.
291 *
292 * N.B.: must be called at splnet
293 *
294 */
295 void
296 rtredirect(dst, gateway, netmask, flags, src, rtp)
297 struct sockaddr *dst, *gateway, *netmask, *src;
298 int flags;
299 struct rtentry **rtp;
300 {
301 struct rtentry *rt;
302 int error = 0;
303 short *stat = 0;
304 struct rt_addrinfo info;
305 struct ifaddr *ifa;
306
307 /* verify the gateway is directly reachable */
308 if ((ifa = ifa_ifwithnet(gateway)) == 0) {
309 error = ENETUNREACH;
310 goto out;
311 }
312 rt = rtalloc1(dst, 0, 0UL);
313 /*
314 * If the redirect isn't from our current router for this dst,
315 * it's either old or wrong. If it redirects us to ourselves,
316 * we have a routing loop, perhaps as a result of an interface
317 * going down recently.
318 */
319 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
320 if (!(flags & RTF_DONE) && rt &&
321 (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
322 error = EINVAL;
323 else if (ifa_ifwithaddr(gateway))
324 error = EHOSTUNREACH;
325 if (error)
326 goto done;
327 /*
328 * Create a new entry if we just got back a wildcard entry
329 * or the the lookup failed. This is necessary for hosts
330 * which use routing redirects generated by smart gateways
331 * to dynamically build the routing tables.
332 */
333 if ((rt == 0) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
334 goto create;
335 /*
336 * Don't listen to the redirect if it's
337 * for a route to an interface.
338 */
339 if (rt->rt_flags & RTF_GATEWAY) {
340 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
341 /*
342 * Changing from route to net => route to host.
343 * Create new route, rather than smashing route to net.
344 */
345 create:
346 if (rt)
347 rtfree(rt);
348 flags |= RTF_GATEWAY | RTF_DYNAMIC;
349 bzero((caddr_t)&info, sizeof(info));
350 info.rti_info[RTAX_DST] = dst;
351 info.rti_info[RTAX_GATEWAY] = gateway;
352 info.rti_info[RTAX_NETMASK] = netmask;
353 info.rti_ifa = ifa;
354 info.rti_flags = flags;
355 rt = NULL;
356 error = rtrequest1(RTM_ADD, &info, &rt);
357 if (rt != NULL)
358 flags = rt->rt_flags;
359 stat = &rtstat.rts_dynamic;
360 } else {
361 /*
362 * Smash the current notion of the gateway to
363 * this destination. Should check about netmask!!!
364 */
365 rt->rt_flags |= RTF_MODIFIED;
366 flags |= RTF_MODIFIED;
367 stat = &rtstat.rts_newgateway;
368 /*
369 * add the key and gateway (in one malloc'd chunk).
370 */
371 rt_setgate(rt, rt_key(rt), gateway);
372 }
373 } else
374 error = EHOSTUNREACH;
375 done:
376 if (rt) {
377 if (rtp && !error)
378 *rtp = rt;
379 else
380 rtfree(rt);
381 }
382 out:
383 if (error)
384 rtstat.rts_badredirect++;
385 else if (stat != NULL)
386 (*stat)++;
387 bzero((caddr_t)&info, sizeof(info));
388 info.rti_info[RTAX_DST] = dst;
389 info.rti_info[RTAX_GATEWAY] = gateway;
390 info.rti_info[RTAX_NETMASK] = netmask;
391 info.rti_info[RTAX_AUTHOR] = src;
392 rt_missmsg(RTM_REDIRECT, &info, flags, error);
393 }
394
395 /*
396 * Routing table ioctl interface.
397 */
398 int
399 rtioctl(req, data, p)
400 u_long req;
401 caddr_t data;
402 struct proc *p;
403 {
404 #ifdef INET
405 /* Multicast goop, grrr... */
406 return mrt_ioctl ? mrt_ioctl(req, data) : EOPNOTSUPP;
407 #else /* INET */
408 return ENXIO;
409 #endif /* INET */
410 }
411
412 struct ifaddr *
413 ifa_ifwithroute(flags, dst, gateway)
414 int flags;
415 struct sockaddr *dst, *gateway;
416 {
417 register struct ifaddr *ifa;
418 if ((flags & RTF_GATEWAY) == 0) {
419 /*
420 * If we are adding a route to an interface,
421 * and the interface is a pt to pt link
422 * we should search for the destination
423 * as our clue to the interface. Otherwise
424 * we can use the local address.
425 */
426 ifa = 0;
427 if (flags & RTF_HOST) {
428 ifa = ifa_ifwithdstaddr(dst);
429 }
430 if (ifa == 0)
431 ifa = ifa_ifwithaddr(gateway);
432 } else {
433 /*
434 * If we are adding a route to a remote net
435 * or host, the gateway may still be on the
436 * other end of a pt to pt link.
437 */
438 ifa = ifa_ifwithdstaddr(gateway);
439 }
440 if (ifa == 0)
441 ifa = ifa_ifwithnet(gateway);
442 if (ifa == 0) {
443 struct rtentry *rt = rtalloc1(gateway, 0, 0UL);
444 if (rt == 0)
445 return (0);
446 rt->rt_refcnt--;
447 if ((ifa = rt->rt_ifa) == 0)
448 return (0);
449 }
450 if (ifa->ifa_addr->sa_family != dst->sa_family) {
451 struct ifaddr *oifa = ifa;
452 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
453 if (ifa == 0)
454 ifa = oifa;
455 }
456 return (ifa);
457 }
458
459 static int rt_fixdelete __P((struct radix_node *, void *));
460 static int rt_fixchange __P((struct radix_node *, void *));
461
462 struct rtfc_arg {
463 struct rtentry *rt0;
464 struct radix_node_head *rnh;
465 };
466
467 /*
468 * Do appropriate manipulations of a routing tree given
469 * all the bits of info needed
470 */
471 int
472 rtrequest(req, dst, gateway, netmask, flags, ret_nrt)
473 int req, flags;
474 struct sockaddr *dst, *gateway, *netmask;
475 struct rtentry **ret_nrt;
476 {
477 struct rt_addrinfo info;
478
479 bzero((caddr_t)&info, sizeof(info));
480 info.rti_flags = flags;
481 info.rti_info[RTAX_DST] = dst;
482 info.rti_info[RTAX_GATEWAY] = gateway;
483 info.rti_info[RTAX_NETMASK] = netmask;
484 return rtrequest1(req, &info, ret_nrt);
485 }
486
487 /*
488 * These (questionable) definitions of apparent local variables apply
489 * to the next two functions. XXXXXX!!!
490 */
491 #define dst info->rti_info[RTAX_DST]
492 #define gateway info->rti_info[RTAX_GATEWAY]
493 #define netmask info->rti_info[RTAX_NETMASK]
494 #define ifaaddr info->rti_info[RTAX_IFA]
495 #define ifpaddr info->rti_info[RTAX_IFP]
496 #define flags info->rti_flags
497
498 int
499 rt_getifa(info)
500 struct rt_addrinfo *info;
501 {
502 struct ifaddr *ifa;
503 int error = 0;
504
505 /*
506 * ifp may be specified by sockaddr_dl
507 * when protocol address is ambiguous.
508 */
509 if (info->rti_ifp == NULL && ifpaddr != NULL &&
510 ifpaddr->sa_family == AF_LINK &&
511 (ifa = ifa_ifwithnet(ifpaddr)) != NULL)
512 info->rti_ifp = ifa->ifa_ifp;
513 if (info->rti_ifa == NULL && ifaaddr != NULL)
514 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
515 if (info->rti_ifa == NULL) {
516 struct sockaddr *sa;
517
518 sa = ifaaddr != NULL ? ifaaddr :
519 (gateway != NULL ? gateway : dst);
520 if (sa != NULL && info->rti_ifp != NULL)
521 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
522 else if (dst != NULL && gateway != NULL)
523 info->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
524 else if (sa != NULL)
525 info->rti_ifa = ifa_ifwithroute(flags, sa, sa);
526 }
527 if ((ifa = info->rti_ifa) != NULL) {
528 if (info->rti_ifp == NULL)
529 info->rti_ifp = ifa->ifa_ifp;
530 } else
531 error = ENETUNREACH;
532 return (error);
533 }
534
535 int
536 rtrequest1(req, info, ret_nrt)
537 int req;
538 struct rt_addrinfo *info;
539 struct rtentry **ret_nrt;
540 {
541 int s = splnet(); int error = 0;
542 register struct rtentry *rt;
543 register struct radix_node *rn;
544 register struct radix_node_head *rnh;
545 struct ifaddr *ifa;
546 struct sockaddr *ndst;
547 #define senderr(x) { error = x ; goto bad; }
548
549 /*
550 * Find the correct routing tree to use for this Address Family
551 */
552 if ((rnh = rt_tables[dst->sa_family]) == 0)
553 senderr(EAFNOSUPPORT);
554 /*
555 * If we are adding a host route then we don't want to put
556 * a netmask in the tree, nor do we want to clone it.
557 */
558 if (flags & RTF_HOST) {
559 netmask = 0;
560 flags &= ~(RTF_CLONING | RTF_PRCLONING);
561 }
562 switch (req) {
563 case RTM_DELETE:
564 /*
565 * Remove the item from the tree and return it.
566 * Complain if it is not there and do no more processing.
567 */
568 if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == 0)
569 senderr(ESRCH);
570 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
571 panic ("rtrequest delete");
572 rt = (struct rtentry *)rn;
573
574 /*
575 * Now search what's left of the subtree for any cloned
576 * routes which might have been formed from this node.
577 */
578 if ((rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) &&
579 rt_mask(rt)) {
580 rnh->rnh_walktree_from(rnh, dst, rt_mask(rt),
581 rt_fixdelete, rt);
582 }
583
584 /*
585 * Remove any external references we may have.
586 * This might result in another rtentry being freed if
587 * we held its last reference.
588 */
589 if (rt->rt_gwroute) {
590 rt = rt->rt_gwroute;
591 RTFREE(rt);
592 (rt = (struct rtentry *)rn)->rt_gwroute = 0;
593 }
594
595 /*
596 * NB: RTF_UP must be set during the search above,
597 * because we might delete the last ref, causing
598 * rt to get freed prematurely.
599 * eh? then why not just add a reference?
600 * I'm not sure how RTF_UP helps matters. (JRE)
601 */
602 rt->rt_flags &= ~RTF_UP;
603
604 /*
605 * give the protocol a chance to keep things in sync.
606 */
607 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
608 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
609
610 /*
611 * one more rtentry floating around that is not
612 * linked to the routing table.
613 */
614 rttrash++;
615
616 /*
617 * If the caller wants it, then it can have it,
618 * but it's up to it to free the rtentry as we won't be
619 * doing it.
620 */
621 if (ret_nrt)
622 *ret_nrt = rt;
623 else if (rt->rt_refcnt <= 0) {
624 rt->rt_refcnt++; /* make a 1->0 transition */
625 rtfree(rt);
626 }
627 break;
628
629 case RTM_RESOLVE:
630 if (ret_nrt == 0 || (rt = *ret_nrt) == 0)
631 senderr(EINVAL);
632 ifa = rt->rt_ifa;
633 flags = rt->rt_flags &
634 ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC);
635 flags |= RTF_WASCLONED;
636 gateway = rt->rt_gateway;
637 if ((netmask = rt->rt_genmask) == 0)
638 flags |= RTF_HOST;
639 goto makeroute;
640
641 case RTM_ADD:
642 if ((flags & RTF_GATEWAY) && !gateway)
643 panic("rtrequest: GATEWAY but no gateway");
644
645 if (info->rti_ifa == NULL && (error = rt_getifa(info)))
646 senderr(error);
647 ifa = info->rti_ifa;
648
649 makeroute:
650 R_Malloc(rt, struct rtentry *, sizeof(*rt));
651 if (rt == 0)
652 senderr(ENOBUFS);
653 Bzero(rt, sizeof(*rt));
654 rt->rt_flags = RTF_UP | flags;
655 /*
656 * Add the gateway. Possibly re-malloc-ing the storage for it
657 * also add the rt_gwroute if possible.
658 */
659 if ((error = rt_setgate(rt, dst, gateway)) != 0) {
660 Free(rt);
661 senderr(error);
662 }
663
664 /*
665 * point to the (possibly newly malloc'd) dest address.
666 */
667 ndst = rt_key(rt);
668
669 /*
670 * make sure it contains the value we want (masked if needed).
671 */
672 if (netmask) {
673 rt_maskedcopy(dst, ndst, netmask);
674 } else
675 Bcopy(dst, ndst, dst->sa_len);
676
677 /*
678 * Note that we now have a reference to the ifa.
679 * This moved from below so that rnh->rnh_addaddr() can
680 * examine the ifa and ifa->ifa_ifp if it so desires.
681 */
682 ifa->ifa_refcnt++;
683 rt->rt_ifa = ifa;
684 rt->rt_ifp = ifa->ifa_ifp;
685 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
686
687 rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask,
688 rnh, rt->rt_nodes);
689 if (rn == 0) {
690 struct rtentry *rt2;
691 /*
692 * Uh-oh, we already have one of these in the tree.
693 * We do a special hack: if the route that's already
694 * there was generated by the protocol-cloning
695 * mechanism, then we just blow it away and retry
696 * the insertion of the new one.
697 */
698 rt2 = rtalloc1(dst, 0, RTF_PRCLONING);
699 if (rt2 && rt2->rt_parent) {
700 rtrequest(RTM_DELETE,
701 (struct sockaddr *)rt_key(rt2),
702 rt2->rt_gateway,
703 rt_mask(rt2), rt2->rt_flags, 0);
704 RTFREE(rt2);
705 rn = rnh->rnh_addaddr((caddr_t)ndst,
706 (caddr_t)netmask,
707 rnh, rt->rt_nodes);
708 } else if (rt2) {
709 /* undo the extra ref we got */
710 RTFREE(rt2);
711 }
712 }
713
714 /*
715 * If it still failed to go into the tree,
716 * then un-make it (this should be a function)
717 */
718 if (rn == 0) {
719 if (rt->rt_gwroute)
720 rtfree(rt->rt_gwroute);
721 if (rt->rt_ifa) {
722 IFAFREE(rt->rt_ifa);
723 }
724 Free(rt_key(rt));
725 Free(rt);
726 senderr(EEXIST);
727 }
728
729 rt->rt_parent = 0;
730
731 /*
732 * If we got here from RESOLVE, then we are cloning
733 * so clone the rest, and note that we
734 * are a clone (and increment the parent's references)
735 */
736 if (req == RTM_RESOLVE) {
737 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
738 rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */
739 if ((*ret_nrt)->rt_flags & (RTF_CLONING | RTF_PRCLONING)) {
740 rt->rt_parent = (*ret_nrt);
741 (*ret_nrt)->rt_refcnt++;
742 }
743 }
744
745 /*
746 * if this protocol has something to add to this then
747 * allow it to do that as well.
748 */
749 if (ifa->ifa_rtrequest)
750 ifa->ifa_rtrequest(req, rt, info);
751
752 /*
753 * We repeat the same procedure from rt_setgate() here because
754 * it doesn't fire when we call it there because the node
755 * hasn't been added to the tree yet.
756 */
757 if (req == RTM_ADD &&
758 !(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) {
759 struct rtfc_arg arg;
760 arg.rnh = rnh;
761 arg.rt0 = rt;
762 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
763 rt_fixchange, &arg);
764 }
765
766 /*
767 * actually return a resultant rtentry and
768 * give the caller a single reference.
769 */
770 if (ret_nrt) {
771 *ret_nrt = rt;
772 rt->rt_refcnt++;
773 }
774 break;
775 default:
776 error = EOPNOTSUPP;
777 }
778 bad:
779 splx(s);
780 return (error);
781 #undef dst
782 #undef gateway
783 #undef netmask
784 #undef ifaaddr
785 #undef ifpaddr
786 #undef flags
787 }
788
789 /*
790 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
791 * (i.e., the routes related to it by the operation of cloning). This
792 * routine is iterated over all potential former-child-routes by way of
793 * rnh->rnh_walktree_from() above, and those that actually are children of
794 * the late parent (passed in as VP here) are themselves deleted.
795 */
796 static int
797 rt_fixdelete(rn, vp)
798 struct radix_node *rn;
799 void *vp;
800 {
801 struct rtentry *rt = (struct rtentry *)rn;
802 struct rtentry *rt0 = vp;
803
804 if (rt->rt_parent == rt0 &&
805 !(rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) {
806 return rtrequest(RTM_DELETE, rt_key(rt),
807 (struct sockaddr *)0, rt_mask(rt),
808 rt->rt_flags, (struct rtentry **)0);
809 }
810 return 0;
811 }
812
813 /*
814 * This routine is called from rt_setgate() to do the analogous thing for
815 * adds and changes. There is the added complication in this case of a
816 * middle insert; i.e., insertion of a new network route between an older
817 * network route and (cloned) host routes. For this reason, a simple check
818 * of rt->rt_parent is insufficient; each candidate route must be tested
819 * against the (mask, value) of the new route (passed as before in vp)
820 * to see if the new route matches it.
821 *
822 * XXX - it may be possible to do fixdelete() for changes and reserve this
823 * routine just for adds. I'm not sure why I thought it was necessary to do
824 * changes this way.
825 */
826 #ifdef DEBUG
827 static int rtfcdebug = 0;
828 #endif
829
830 static int
831 rt_fixchange(rn, vp)
832 struct radix_node *rn;
833 void *vp;
834 {
835 struct rtentry *rt = (struct rtentry *)rn;
836 struct rtfc_arg *ap = vp;
837 struct rtentry *rt0 = ap->rt0;
838 struct radix_node_head *rnh = ap->rnh;
839 u_char *xk1, *xm1, *xk2, *xmp;
840 int i, len, mlen;
841
842 #ifdef DEBUG
843 if (rtfcdebug)
844 printf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0);
845 #endif
846
847 if (!rt->rt_parent ||
848 (rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) {
849 #ifdef DEBUG
850 if(rtfcdebug) printf("no parent, pinned or cloning\n");
851 #endif
852 return 0;
853 }
854
855 if (rt->rt_parent == rt0) {
856 #ifdef DEBUG
857 if(rtfcdebug) printf("parent match\n");
858 #endif
859 return rtrequest(RTM_DELETE, rt_key(rt),
860 (struct sockaddr *)0, rt_mask(rt),
861 rt->rt_flags, (struct rtentry **)0);
862 }
863
864 /*
865 * There probably is a function somewhere which does this...
866 * if not, there should be.
867 */
868 len = imin(((struct sockaddr *)rt_key(rt0))->sa_len,
869 ((struct sockaddr *)rt_key(rt))->sa_len);
870
871 xk1 = (u_char *)rt_key(rt0);
872 xm1 = (u_char *)rt_mask(rt0);
873 xk2 = (u_char *)rt_key(rt);
874
875 /* avoid applying a less specific route */
876 xmp = (u_char *)rt_mask(rt->rt_parent);
877 mlen = ((struct sockaddr *)rt_key(rt->rt_parent))->sa_len;
878 if (mlen > ((struct sockaddr *)rt_key(rt0))->sa_len) {
879 #ifdef DEBUG
880 if (rtfcdebug)
881 printf("rt_fixchange: inserting a less "
882 "specific route\n");
883 #endif
884 return 0;
885 }
886 for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++) {
887 if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i]) {
888 #ifdef DEBUG
889 if (rtfcdebug)
890 printf("rt_fixchange: inserting a less "
891 "specific route\n");
892 #endif
893 return 0;
894 }
895 }
896
897 for (i = rnh->rnh_treetop->rn_offset; i < len; i++) {
898 if ((xk2[i] & xm1[i]) != xk1[i]) {
899 #ifdef DEBUG
900 if(rtfcdebug) printf("no match\n");
901 #endif
902 return 0;
903 }
904 }
905
906 /*
907 * OK, this node is a clone, and matches the node currently being
908 * changed/added under the node's mask. So, get rid of it.
909 */
910 #ifdef DEBUG
911 if(rtfcdebug) printf("deleting\n");
912 #endif
913 return rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
914 rt_mask(rt), rt->rt_flags, (struct rtentry **)0);
915 }
916
917 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
918
919 int
920 rt_setgate(rt0, dst, gate)
921 struct rtentry *rt0;
922 struct sockaddr *dst, *gate;
923 {
924 caddr_t new, old;
925 int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
926 register struct rtentry *rt = rt0;
927 struct radix_node_head *rnh = rt_tables[dst->sa_family];
928
929 /*
930 * A host route with the destination equal to the gateway
931 * will interfere with keeping LLINFO in the routing
932 * table, so disallow it.
933 */
934 if (((rt0->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) ==
935 (RTF_HOST|RTF_GATEWAY)) &&
936 (dst->sa_len == gate->sa_len) &&
937 (bcmp(dst, gate, dst->sa_len) == 0)) {
938 /*
939 * The route might already exist if this is an RTM_CHANGE
940 * or a routing redirect, so try to delete it.
941 */
942 if (rt_key(rt0))
943 rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt0),
944 rt0->rt_gateway, rt_mask(rt0), rt0->rt_flags, 0);
945 return EADDRNOTAVAIL;
946 }
947
948 /*
949 * Both dst and gateway are stored in the same malloc'd chunk
950 * (If I ever get my hands on....)
951 * if we need to malloc a new chunk, then keep the old one around
952 * till we don't need it any more.
953 */
954 if (rt->rt_gateway == 0 || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
955 old = (caddr_t)rt_key(rt);
956 R_Malloc(new, caddr_t, dlen + glen);
957 if (new == 0)
958 return ENOBUFS;
959 rt->rt_nodes->rn_key = new;
960 } else {
961 /*
962 * otherwise just overwrite the old one
963 */
964 new = rt->rt_nodes->rn_key;
965 old = 0;
966 }
967
968 /*
969 * copy the new gateway value into the memory chunk
970 */
971 Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen);
972
973 /*
974 * if we are replacing the chunk (or it's new) we need to
975 * replace the dst as well
976 */
977 if (old) {
978 Bcopy(dst, new, dlen);
979 Free(old);
980 }
981
982 /*
983 * If there is already a gwroute, it's now almost definitly wrong
984 * so drop it.
985 */
986 if (rt->rt_gwroute != NULL) {
987 RTFREE(rt->rt_gwroute);
988 rt->rt_gwroute = NULL;
989 }
990 /*
991 * Cloning loop avoidance:
992 * In the presence of protocol-cloning and bad configuration,
993 * it is possible to get stuck in bottomless mutual recursion
994 * (rtrequest rt_setgate rtalloc1). We avoid this by not allowing
995 * protocol-cloning to operate for gateways (which is probably the
996 * correct choice anyway), and avoid the resulting reference loops
997 * by disallowing any route to run through itself as a gateway.
998 * This is obviously mandatory when we get rt->rt_output().
999 */
1000 if (rt->rt_flags & RTF_GATEWAY) {
1001 rt->rt_gwroute = rtalloc1(gate, 1, RTF_PRCLONING);
1002 if (rt->rt_gwroute == rt) {
1003 RTFREE(rt->rt_gwroute);
1004 rt->rt_gwroute = 0;
1005 return EDQUOT; /* failure */
1006 }
1007 }
1008
1009 /*
1010 * This isn't going to do anything useful for host routes, so
1011 * don't bother. Also make sure we have a reasonable mask
1012 * (we don't yet have one during adds).
1013 */
1014 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) {
1015 struct rtfc_arg arg;
1016 arg.rnh = rnh;
1017 arg.rt0 = rt;
1018 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
1019 rt_fixchange, &arg);
1020 }
1021
1022 return 0;
1023 }
1024
1025 static void
1026 rt_maskedcopy(src, dst, netmask)
1027 struct sockaddr *src, *dst, *netmask;
1028 {
1029 register u_char *cp1 = (u_char *)src;
1030 register u_char *cp2 = (u_char *)dst;
1031 register u_char *cp3 = (u_char *)netmask;
1032 u_char *cplim = cp2 + *cp3;
1033 u_char *cplim2 = cp2 + *cp1;
1034
1035 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1036 cp3 += 2;
1037 if (cplim > cplim2)
1038 cplim = cplim2;
1039 while (cp2 < cplim)
1040 *cp2++ = *cp1++ & *cp3++;
1041 if (cp2 < cplim2)
1042 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1043 }
1044
1045 /*
1046 * Set up a routing table entry, normally
1047 * for an interface.
1048 */
1049 int
1050 rtinit(ifa, cmd, flags)
1051 register struct ifaddr *ifa;
1052 int cmd, flags;
1053 {
1054 register struct rtentry *rt;
1055 register struct sockaddr *dst;
1056 register struct sockaddr *deldst;
1057 struct sockaddr *netmask;
1058 struct mbuf *m = 0;
1059 struct rtentry *nrt = 0;
1060 struct radix_node_head *rnh;
1061 struct radix_node *rn;
1062 int error;
1063 struct rt_addrinfo info;
1064
1065 if (flags & RTF_HOST) {
1066 dst = ifa->ifa_dstaddr;
1067 netmask = NULL;
1068 } else {
1069 dst = ifa->ifa_addr;
1070 netmask = ifa->ifa_netmask;
1071 }
1072 /*
1073 * If it's a delete, check that if it exists, it's on the correct
1074 * interface or we might scrub a route to another ifa which would
1075 * be confusing at best and possibly worse.
1076 */
1077 if (cmd == RTM_DELETE) {
1078 /*
1079 * It's a delete, so it should already exist..
1080 * If it's a net, mask off the host bits
1081 * (Assuming we have a mask)
1082 */
1083 if (netmask != NULL) {
1084 m = m_get(M_DONTWAIT, MT_SONAME);
1085 if (m == NULL)
1086 return(ENOBUFS);
1087 deldst = mtod(m, struct sockaddr *);
1088 rt_maskedcopy(dst, deldst, netmask);
1089 dst = deldst;
1090 }
1091 /*
1092 * Look up an rtentry that is in the routing tree and
1093 * contains the correct info.
1094 */
1095 if ((rnh = rt_tables[dst->sa_family]) == NULL ||
1096 (rn = rnh->rnh_lookup(dst, netmask, rnh)) == NULL ||
1097 (rn->rn_flags & RNF_ROOT) ||
1098 ((struct rtentry *)rn)->rt_ifa != ifa ||
1099 !equal(SA(rn->rn_key), dst)) {
1100 if (m)
1101 (void) m_free(m);
1102 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1103 }
1104 /* XXX */
1105 #if 0
1106 else {
1107 /*
1108 * One would think that as we are deleting, and we know
1109 * it doesn't exist, we could just return at this point
1110 * with an "ELSE" clause, but apparently not..
1111 */
1112 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1113 }
1114 #endif
1115 }
1116 /*
1117 * Do the actual request
1118 */
1119 bzero((caddr_t)&info, sizeof(info));
1120 info.rti_ifa = ifa;
1121 info.rti_flags = flags | ifa->ifa_flags;
1122 info.rti_info[RTAX_DST] = dst;
1123 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1124 info.rti_info[RTAX_NETMASK] = netmask;
1125 error = rtrequest1(cmd, &info, &nrt);
1126 if (error == 0 && (rt = nrt) != NULL) {
1127 /*
1128 * notify any listening routing agents of the change
1129 */
1130 rt_newaddrmsg(cmd, ifa, error, rt);
1131 if (cmd == RTM_DELETE) {
1132 /*
1133 * If we are deleting, and we found an entry, then
1134 * it's been removed from the tree.. now throw it away.
1135 */
1136 if (rt->rt_refcnt <= 0) {
1137 rt->rt_refcnt++; /* make a 1->0 transition */
1138 rtfree(rt);
1139 }
1140 } else if (cmd == RTM_ADD) {
1141 /*
1142 * We just wanted to add it.. we don't actually
1143 * need a reference.
1144 */
1145 rt->rt_refcnt--;
1146 }
1147 }
1148 if (m)
1149 (void) m_free(m);
1150 return (error);
1151 }
1152
1153 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1154 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);
Cache object: 1d61bc4d2d5508263d5329b4c3169549
|