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.2 (Berkeley) 11/15/93
34 * $FreeBSD$
35 */
36
37 #include "opt_inet.h"
38 #include "opt_mrouting.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/socket.h>
45 #include <sys/domain.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 splx(s);
108 }
109 ro->ro_rt = rtalloc1(&ro->ro_dst, 1, ignore);
110 }
111
112 /*
113 * Look up the route that matches the address given
114 * Or, at least try.. Create a cloned route if needed.
115 */
116 struct rtentry *
117 rtalloc1(dst, report, ignflags)
118 register struct sockaddr *dst;
119 int report;
120 u_long ignflags;
121 {
122 register struct radix_node_head *rnh = rt_tables[dst->sa_family];
123 register struct rtentry *rt;
124 register struct radix_node *rn;
125 struct rtentry *newrt = 0;
126 struct rt_addrinfo info;
127 u_long nflags;
128 int s = splnet(), err = 0, msgtype = RTM_MISS;
129
130 /*
131 * Look up the address in the table for that Address Family
132 */
133 if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) &&
134 ((rn->rn_flags & RNF_ROOT) == 0)) {
135 /*
136 * If we find it and it's not the root node, then
137 * get a refernce on the rtentry associated.
138 */
139 newrt = rt = (struct rtentry *)rn;
140 nflags = rt->rt_flags & ~ignflags;
141 if (report && (nflags & (RTF_CLONING | RTF_PRCLONING))) {
142 /*
143 * We are apparently adding (report = 0 in delete).
144 * If it requires that it be cloned, do so.
145 * (This implies it wasn't a HOST route.)
146 */
147 err = rtrequest(RTM_RESOLVE, dst, SA(0),
148 SA(0), 0, &newrt);
149 if (err) {
150 /*
151 * If the cloning didn't succeed, maybe
152 * what we have will do. Return that.
153 */
154 newrt = rt;
155 rt->rt_refcnt++;
156 goto miss;
157 }
158 if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) {
159 /*
160 * If the new route specifies it be
161 * externally resolved, then go do that.
162 */
163 msgtype = RTM_RESOLVE;
164 goto miss;
165 }
166 } else
167 rt->rt_refcnt++;
168 } else {
169 /*
170 * Either we hit the root or couldn't find any match,
171 * Which basically means
172 * "caint get there frm here"
173 */
174 rtstat.rts_unreach++;
175 miss: if (report) {
176 /*
177 * If required, report the failure to the supervising
178 * Authorities.
179 * For a delete, this is not an error. (report == 0)
180 */
181 bzero((caddr_t)&info, sizeof(info));
182 info.rti_info[RTAX_DST] = dst;
183 rt_missmsg(msgtype, &info, 0, err);
184 }
185 }
186 splx(s);
187 return (newrt);
188 }
189
190 /*
191 * Remove a reference count from an rtentry.
192 * If the count gets low enough, take it out of the routing table
193 */
194 void
195 rtfree(rt)
196 register struct rtentry *rt;
197 {
198 /*
199 * find the tree for that address family
200 */
201 register struct radix_node_head *rnh =
202 rt_tables[rt_key(rt)->sa_family];
203 register struct ifaddr *ifa;
204
205 if (rt == 0 || rnh == 0)
206 panic("rtfree");
207
208 /*
209 * decrement the reference count by one and if it reaches 0,
210 * and there is a close function defined, call the close function
211 */
212 rt->rt_refcnt--;
213 if(rnh->rnh_close && rt->rt_refcnt == 0) {
214 rnh->rnh_close((struct radix_node *)rt, rnh);
215 }
216
217 /*
218 * If we are no longer "up" (and ref == 0)
219 * then we can free the resources associated
220 * with the route.
221 */
222 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) {
223 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
224 panic ("rtfree 2");
225 /*
226 * the rtentry must have been removed from the routing table
227 * so it is represented in rttrash.. remove that now.
228 */
229 rttrash--;
230
231 #ifdef DIAGNOSTIC
232 if (rt->rt_refcnt < 0) {
233 printf("rtfree: %p not freed (neg refs)\n", rt);
234 return;
235 }
236 #endif
237
238 /*
239 * release references on items we hold them on..
240 * e.g other routes and ifaddrs.
241 */
242 if((ifa = rt->rt_ifa))
243 IFAFREE(ifa);
244 if (rt->rt_parent) {
245 RTFREE(rt->rt_parent);
246 }
247
248 /*
249 * The key is separatly alloc'd so free it (see rt_setgate()).
250 * This also frees the gateway, as they are always malloc'd
251 * together.
252 */
253 Free(rt_key(rt));
254
255 /*
256 * and the rtentry itself of course
257 */
258 Free(rt);
259 }
260 }
261
262 void
263 ifafree(ifa)
264 register struct ifaddr *ifa;
265 {
266 if (ifa == NULL)
267 panic("ifafree");
268 if (ifa->ifa_refcnt == 0)
269 free(ifa, M_IFADDR);
270 else
271 ifa->ifa_refcnt--;
272 }
273
274 /*
275 * Force a routing table entry to the specified
276 * destination to go through the given gateway.
277 * Normally called as a result of a routing redirect
278 * message from the network layer.
279 *
280 * N.B.: must be called at splnet
281 *
282 */
283 void
284 rtredirect(dst, gateway, netmask, flags, src, rtp)
285 struct sockaddr *dst, *gateway, *netmask, *src;
286 int flags;
287 struct rtentry **rtp;
288 {
289 register struct rtentry *rt;
290 int error = 0;
291 short *stat = 0;
292 struct rt_addrinfo info;
293 struct ifaddr *ifa;
294
295 /* verify the gateway is directly reachable */
296 if ((ifa = ifa_ifwithnet(gateway)) == 0) {
297 error = ENETUNREACH;
298 goto out;
299 }
300 rt = rtalloc1(dst, 0, 0UL);
301 /*
302 * If the redirect isn't from our current router for this dst,
303 * it's either old or wrong. If it redirects us to ourselves,
304 * we have a routing loop, perhaps as a result of an interface
305 * going down recently.
306 */
307 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
308 if (!(flags & RTF_DONE) && rt &&
309 (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
310 error = EINVAL;
311 else if (ifa_ifwithaddr(gateway))
312 error = EHOSTUNREACH;
313 if (error)
314 goto done;
315 /*
316 * Create a new entry if we just got back a wildcard entry
317 * or the the lookup failed. This is necessary for hosts
318 * which use routing redirects generated by smart gateways
319 * to dynamically build the routing tables.
320 */
321 if ((rt == 0) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
322 goto create;
323 /*
324 * Don't listen to the redirect if it's
325 * for a route to an interface.
326 */
327 if (rt->rt_flags & RTF_GATEWAY) {
328 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
329 /*
330 * Changing from route to net => route to host.
331 * Create new route, rather than smashing route to net.
332 */
333 create:
334 flags |= RTF_GATEWAY | RTF_DYNAMIC;
335 error = rtrequest((int)RTM_ADD, dst, gateway,
336 netmask, flags,
337 (struct rtentry **)0);
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 /*
348 * add the key and gateway (in one malloc'd chunk).
349 */
350 rt_setgate(rt, rt_key(rt), gateway);
351 }
352 } else
353 error = EHOSTUNREACH;
354 done:
355 if (rt) {
356 if (rtp && !error)
357 *rtp = rt;
358 else
359 rtfree(rt);
360 }
361 out:
362 if (error)
363 rtstat.rts_badredirect++;
364 else if (stat != NULL)
365 (*stat)++;
366 bzero((caddr_t)&info, sizeof(info));
367 info.rti_info[RTAX_DST] = dst;
368 info.rti_info[RTAX_GATEWAY] = gateway;
369 info.rti_info[RTAX_NETMASK] = netmask;
370 info.rti_info[RTAX_AUTHOR] = src;
371 rt_missmsg(RTM_REDIRECT, &info, flags, error);
372 }
373
374 /*
375 * Routing table ioctl interface.
376 */
377 int
378 rtioctl(req, data, p)
379 int req;
380 caddr_t data;
381 struct proc *p;
382 {
383 #ifdef INET
384 /* Multicast goop, grrr... */
385 #ifdef MROUTING
386 return mrt_ioctl(req, data);
387 #else
388 return mrt_ioctl(req, data, p);
389 #endif
390 #else /* INET */
391 return ENXIO;
392 #endif /* INET */
393 }
394
395 struct ifaddr *
396 ifa_ifwithroute(flags, dst, gateway)
397 int flags;
398 struct sockaddr *dst, *gateway;
399 {
400 register struct ifaddr *ifa;
401 if ((flags & RTF_GATEWAY) == 0) {
402 /*
403 * If we are adding a route to an interface,
404 * and the interface is a pt to pt link
405 * we should search for the destination
406 * as our clue to the interface. Otherwise
407 * we can use the local address.
408 */
409 ifa = 0;
410 if (flags & RTF_HOST) {
411 ifa = ifa_ifwithdstaddr(dst);
412 }
413 if (ifa == 0)
414 ifa = ifa_ifwithaddr(gateway);
415 } else {
416 /*
417 * If we are adding a route to a remote net
418 * or host, the gateway may still be on the
419 * other end of a pt to pt link.
420 */
421 ifa = ifa_ifwithdstaddr(gateway);
422 }
423 if (ifa == 0)
424 ifa = ifa_ifwithnet(gateway);
425 if (ifa == 0) {
426 struct rtentry *rt = rtalloc1(dst, 0, 0UL);
427 if (rt == 0)
428 return (0);
429 rt->rt_refcnt--;
430 if ((ifa = rt->rt_ifa) == 0)
431 return (0);
432 }
433 if (ifa->ifa_addr->sa_family != dst->sa_family) {
434 struct ifaddr *oifa = ifa;
435 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
436 if (ifa == 0)
437 ifa = oifa;
438 }
439 return (ifa);
440 }
441
442 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
443
444 static int rt_fixdelete __P((struct radix_node *, void *));
445 static int rt_fixchange __P((struct radix_node *, void *));
446
447 struct rtfc_arg {
448 struct rtentry *rt0;
449 struct radix_node_head *rnh;
450 };
451
452 /*
453 * Do appropriate manipulations of a routing tree given
454 * all the bits of info needed
455 */
456 int
457 rtrequest(req, dst, gateway, netmask, flags, ret_nrt)
458 int req, flags;
459 struct sockaddr *dst, *gateway, *netmask;
460 struct rtentry **ret_nrt;
461 {
462 int s = splnet(); int error = 0;
463 register struct rtentry *rt;
464 register struct radix_node *rn;
465 register struct radix_node_head *rnh;
466 struct ifaddr *ifa;
467 struct sockaddr *ndst;
468 #define senderr(x) { error = x ; goto bad; }
469
470 /*
471 * Find the correct routing tree to use for this Address Family
472 */
473 if ((rnh = rt_tables[dst->sa_family]) == 0)
474 senderr(ESRCH);
475 /*
476 * If we are adding a host route then we don't want to put
477 * a netmask in the tree
478 */
479 if (flags & RTF_HOST)
480 netmask = 0;
481 switch (req) {
482 case RTM_DELETE:
483 /*
484 * Remove the item from the tree and return it.
485 * Complain if it is not there and do no more processing.
486 */
487 if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == 0)
488 senderr(ESRCH);
489 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
490 panic ("rtrequest delete");
491 rt = (struct rtentry *)rn;
492
493 /*
494 * Now search what's left of the subtree for any cloned
495 * routes which might have been formed from this node.
496 */
497 if ((rt->rt_flags & RTF_PRCLONING) && netmask) {
498 rnh->rnh_walktree_from(rnh, dst, netmask,
499 rt_fixdelete, rt);
500 }
501
502 /*
503 * Remove any external references we may have.
504 * This might result in another rtentry being freed if
505 * we held its last reference.
506 */
507 if (rt->rt_gwroute) {
508 rt = rt->rt_gwroute;
509 RTFREE(rt);
510 (rt = (struct rtentry *)rn)->rt_gwroute = 0;
511 }
512
513 /*
514 * NB: RTF_UP must be set during the search above,
515 * because we might delete the last ref, causing
516 * rt to get freed prematurely.
517 * eh? then why not just add a reference?
518 * I'm not sure how RTF_UP helps matters. (JRE)
519 */
520 rt->rt_flags &= ~RTF_UP;
521
522 /*
523 * give the protocol a chance to keep things in sync.
524 */
525 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
526 ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0));
527
528 /*
529 * one more rtentry floating around that is not
530 * linked to the routing table.
531 */
532 rttrash++;
533
534 /*
535 * If the caller wants it, then it can have it,
536 * but it's up to it to free the rtentry as we won't be
537 * doing it.
538 */
539 if (ret_nrt)
540 *ret_nrt = rt;
541 else if (rt->rt_refcnt <= 0) {
542 rt->rt_refcnt++; /* make a 1->0 transition */
543 rtfree(rt);
544 }
545 break;
546
547 case RTM_RESOLVE:
548 if (ret_nrt == 0 || (rt = *ret_nrt) == 0)
549 senderr(EINVAL);
550 ifa = rt->rt_ifa;
551 flags = rt->rt_flags &
552 ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC);
553 flags |= RTF_WASCLONED;
554 gateway = rt->rt_gateway;
555 if ((netmask = rt->rt_genmask) == 0)
556 flags |= RTF_HOST;
557 goto makeroute;
558
559 case RTM_ADD:
560 if ((flags & RTF_GATEWAY) && !gateway)
561 panic("rtrequest: GATEWAY but no gateway");
562
563 if ((ifa = ifa_ifwithroute(flags, dst, gateway)) == 0)
564 senderr(ENETUNREACH);
565
566 makeroute:
567 R_Malloc(rt, struct rtentry *, sizeof(*rt));
568 if (rt == 0)
569 senderr(ENOBUFS);
570 Bzero(rt, sizeof(*rt));
571 rt->rt_flags = RTF_UP | flags;
572 /*
573 * Add the gateway. Possibly re-malloc-ing the storage for it
574 * also add the rt_gwroute if possible.
575 */
576 if (error = rt_setgate(rt, dst, gateway)) {
577 Free(rt);
578 senderr(error);
579 }
580
581 /*
582 * point to the (possibly newly malloc'd) dest address.
583 */
584 ndst = rt_key(rt);
585
586 /*
587 * make sure it contains the value we want (masked if needed).
588 */
589 if (netmask) {
590 rt_maskedcopy(dst, ndst, netmask);
591 } else
592 Bcopy(dst, ndst, dst->sa_len);
593
594 /*
595 * Note that we now have a reference to the ifa.
596 * This moved from below so that rnh->rnh_addaddr() can
597 * examine the ifa and ifa->ifa_ifp if it so desires.
598 */
599 ifa->ifa_refcnt++;
600 rt->rt_ifa = ifa;
601 rt->rt_ifp = ifa->ifa_ifp;
602
603 rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask,
604 rnh, rt->rt_nodes);
605 if (rn == 0) {
606 struct rtentry *rt2;
607 /*
608 * Uh-oh, we already have one of these in the tree.
609 * We do a special hack: if the route that's already
610 * there was generated by the protocol-cloning
611 * mechanism, then we just blow it away and retry
612 * the insertion of the new one.
613 */
614 rt2 = rtalloc1(dst, 0, RTF_PRCLONING);
615 if (rt2 && rt2->rt_parent) {
616 rtrequest(RTM_DELETE,
617 (struct sockaddr *)rt_key(rt2),
618 rt2->rt_gateway,
619 rt_mask(rt2), rt2->rt_flags, 0);
620 RTFREE(rt2);
621 rn = rnh->rnh_addaddr((caddr_t)ndst,
622 (caddr_t)netmask,
623 rnh, rt->rt_nodes);
624 } else if (rt2) {
625 /* undo the extra ref we got */
626 RTFREE(rt2);
627 }
628 }
629
630 /*
631 * If it still failed to go into the tree,
632 * then un-make it (this should be a function)
633 */
634 if (rn == 0) {
635 if (rt->rt_gwroute)
636 rtfree(rt->rt_gwroute);
637 if (rt->rt_ifa) {
638 IFAFREE(rt->rt_ifa);
639 }
640 Free(rt_key(rt));
641 Free(rt);
642 senderr(EEXIST);
643 }
644
645 rt->rt_parent = 0;
646
647 /*
648 * If we got here from RESOLVE, then we are cloning
649 * so clone the rest, and note that we
650 * are a clone (and increment the parent's references)
651 */
652 if (req == RTM_RESOLVE) {
653 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
654 if ((*ret_nrt)->rt_flags & RTF_PRCLONING) {
655 rt->rt_parent = (*ret_nrt);
656 (*ret_nrt)->rt_refcnt++;
657 }
658 }
659
660 /*
661 * if this protocol has something to add to this then
662 * allow it to do that as well.
663 */
664 if (ifa->ifa_rtrequest)
665 ifa->ifa_rtrequest(req, rt, SA(ret_nrt ? *ret_nrt : 0));
666
667 /*
668 * We repeat the same procedure from rt_setgate() here because
669 * it doesn't fire when we call it there because the node
670 * hasn't been added to the tree yet.
671 */
672 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) {
673 struct rtfc_arg arg;
674 arg.rnh = rnh;
675 arg.rt0 = rt;
676 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
677 rt_fixchange, &arg);
678 }
679
680 /*
681 * actually return a resultant rtentry and
682 * give the caller a single reference.
683 */
684 if (ret_nrt) {
685 *ret_nrt = rt;
686 rt->rt_refcnt++;
687 }
688 break;
689 }
690 bad:
691 splx(s);
692 return (error);
693 }
694
695 /*
696 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
697 * (i.e., the routes related to it by the operation of cloning). This
698 * routine is iterated over all potential former-child-routes by way of
699 * rnh->rnh_walktree_from() above, and those that actually are children of
700 * the late parent (passed in as VP here) are themselves deleted.
701 */
702 static int
703 rt_fixdelete(rn, vp)
704 struct radix_node *rn;
705 void *vp;
706 {
707 struct rtentry *rt = (struct rtentry *)rn;
708 struct rtentry *rt0 = vp;
709
710 if (rt->rt_parent == rt0 && !(rt->rt_flags & RTF_PINNED)) {
711 return rtrequest(RTM_DELETE, rt_key(rt),
712 (struct sockaddr *)0, rt_mask(rt),
713 rt->rt_flags, (struct rtentry **)0);
714 }
715 return 0;
716 }
717
718 /*
719 * This routine is called from rt_setgate() to do the analogous thing for
720 * adds and changes. There is the added complication in this case of a
721 * middle insert; i.e., insertion of a new network route between an older
722 * network route and (cloned) host routes. For this reason, a simple check
723 * of rt->rt_parent is insufficient; each candidate route must be tested
724 * against the (mask, value) of the new route (passed as before in vp)
725 * to see if the new route matches it. Unfortunately, this has the obnoxious
726 * property of also triggering for insertion /above/ a pre-existing network
727 * route and clones. Sigh. This may be fixed some day.
728 *
729 * XXX - it may be possible to do fixdelete() for changes and reserve this
730 * routine just for adds. I'm not sure why I thought it was necessary to do
731 * changes this way.
732 */
733 #ifdef DEBUG
734 static int rtfcdebug = 0;
735 #endif
736
737 static int
738 rt_fixchange(rn, vp)
739 struct radix_node *rn;
740 void *vp;
741 {
742 struct rtentry *rt = (struct rtentry *)rn;
743 struct rtfc_arg *ap = vp;
744 struct rtentry *rt0 = ap->rt0;
745 struct radix_node_head *rnh = ap->rnh;
746 u_char *xk1, *xm1, *xk2;
747 int i, len;
748
749 #ifdef DEBUG
750 if (rtfcdebug)
751 printf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0);
752 #endif
753
754 if (!rt->rt_parent || (rt->rt_flags & RTF_PINNED)) {
755 #ifdef DEBUG
756 if(rtfcdebug) printf("no parent or pinned\n");
757 #endif
758 return 0;
759 }
760
761 if (rt->rt_parent == rt0) {
762 #ifdef DEBUG
763 if(rtfcdebug) printf("parent match\n");
764 #endif
765 return rtrequest(RTM_DELETE, rt_key(rt),
766 (struct sockaddr *)0, rt_mask(rt),
767 rt->rt_flags, (struct rtentry **)0);
768 }
769
770 /*
771 * There probably is a function somewhere which does this...
772 * if not, there should be.
773 */
774 len = imin(((struct sockaddr *)rt_key(rt0))->sa_len,
775 ((struct sockaddr *)rt_key(rt))->sa_len);
776
777 xk1 = (u_char *)rt_key(rt0);
778 xm1 = (u_char *)rt_mask(rt0);
779 xk2 = (u_char *)rt_key(rt);
780
781 for (i = rnh->rnh_treetop->rn_off; i < len; i++) {
782 if ((xk2[i] & xm1[i]) != xk1[i]) {
783 #ifdef DEBUG
784 if(rtfcdebug) printf("no match\n");
785 #endif
786 return 0;
787 }
788 }
789
790 /*
791 * OK, this node is a clone, and matches the node currently being
792 * changed/added under the node's mask. So, get rid of it.
793 */
794 #ifdef DEBUG
795 if(rtfcdebug) printf("deleting\n");
796 #endif
797 return rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
798 rt_mask(rt), rt->rt_flags, (struct rtentry **)0);
799 }
800
801 int
802 rt_setgate(rt0, dst, gate)
803 struct rtentry *rt0;
804 struct sockaddr *dst, *gate;
805 {
806 caddr_t new, old;
807 int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
808 register struct rtentry *rt = rt0;
809 struct radix_node_head *rnh = rt_tables[dst->sa_family];
810
811 /*
812 * A host route with the destination equal to the gateway
813 * will interfere with keeping LLINFO in the routing
814 * table, so disallow it.
815 */
816 if (((rt0->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) ==
817 (RTF_HOST|RTF_GATEWAY)) &&
818 (dst->sa_len == gate->sa_len) &&
819 (bcmp(dst, gate, dst->sa_len) == 0)) {
820 /*
821 * The route might already exist if this is an RTM_CHANGE
822 * or a routing redirect, so try to delete it.
823 */
824 if (rt_key(rt0))
825 rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt0),
826 rt0->rt_gateway, rt_mask(rt0), rt0->rt_flags, 0);
827 return EADDRNOTAVAIL;
828 }
829
830 /*
831 * Both dst and gateway are stored in the same malloc'd chunk
832 * (If I ever get my hands on....)
833 * if we need to malloc a new chunk, then keep the old one around
834 * till we don't need it any more.
835 */
836 if (rt->rt_gateway == 0 || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
837 old = (caddr_t)rt_key(rt);
838 R_Malloc(new, caddr_t, dlen + glen);
839 if (new == 0)
840 return ENOBUFS;
841 rt->rt_nodes->rn_key = new;
842 } else {
843 /*
844 * otherwise just overwrite the old one
845 */
846 new = rt->rt_nodes->rn_key;
847 old = 0;
848 }
849
850 /*
851 * copy the new gateway value into the memory chunk
852 */
853 Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen);
854
855 /*
856 * if we are replacing the chunk (or it's new) we need to
857 * replace the dst as well
858 */
859 if (old) {
860 Bcopy(dst, new, dlen);
861 Free(old);
862 }
863
864 /*
865 * If there is already a gwroute, it's now almost definitly wrong
866 * so drop it.
867 */
868 if (rt->rt_gwroute) {
869 rt = rt->rt_gwroute; RTFREE(rt);
870 rt = rt0; rt->rt_gwroute = 0;
871 }
872 /*
873 * Cloning loop avoidance:
874 * In the presence of protocol-cloning and bad configuration,
875 * it is possible to get stuck in bottomless mutual recursion
876 * (rtrequest rt_setgate rtalloc1). We avoid this by not allowing
877 * protocol-cloning to operate for gateways (which is probably the
878 * correct choice anyway), and avoid the resulting reference loops
879 * by disallowing any route to run through itself as a gateway.
880 * This is obviously mandatory when we get rt->rt_output().
881 */
882 if (rt->rt_flags & RTF_GATEWAY) {
883 rt->rt_gwroute = rtalloc1(gate, 1, RTF_PRCLONING);
884 if (rt->rt_gwroute == rt) {
885 RTFREE(rt->rt_gwroute);
886 rt->rt_gwroute = 0;
887 return EDQUOT; /* failure */
888 }
889 }
890
891 /*
892 * This isn't going to do anything useful for host routes, so
893 * don't bother. Also make sure we have a reasonable mask
894 * (we don't yet have one during adds).
895 */
896 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) {
897 struct rtfc_arg arg;
898 arg.rnh = rnh;
899 arg.rt0 = rt;
900 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
901 rt_fixchange, &arg);
902 }
903
904 return 0;
905 }
906
907 static void
908 rt_maskedcopy(src, dst, netmask)
909 struct sockaddr *src, *dst, *netmask;
910 {
911 register u_char *cp1 = (u_char *)src;
912 register u_char *cp2 = (u_char *)dst;
913 register u_char *cp3 = (u_char *)netmask;
914 u_char *cplim = cp2 + *cp3;
915 u_char *cplim2 = cp2 + *cp1;
916
917 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
918 cp3 += 2;
919 if (cplim > cplim2)
920 cplim = cplim2;
921 while (cp2 < cplim)
922 *cp2++ = *cp1++ & *cp3++;
923 if (cp2 < cplim2)
924 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
925 }
926
927 /*
928 * Set up a routing table entry, normally
929 * for an interface.
930 */
931 int
932 rtinit(ifa, cmd, flags)
933 register struct ifaddr *ifa;
934 int cmd, flags;
935 {
936 register struct rtentry *rt;
937 register struct sockaddr *dst;
938 register struct sockaddr *deldst;
939 struct mbuf *m = 0;
940 struct rtentry *nrt = 0;
941 int error;
942
943 dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
944 /*
945 * If it's a delete, check that if it exists, it's on the correct
946 * interface or we might scrub a route to another ifa which would
947 * be confusing at best and possibly worse.
948 */
949 if (cmd == RTM_DELETE) {
950 /*
951 * It's a delete, so it should already exist..
952 * If it's a net, mask off the host bits
953 * (Assuming we have a mask)
954 */
955 if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
956 m = m_get(M_WAIT, MT_SONAME);
957 deldst = mtod(m, struct sockaddr *);
958 rt_maskedcopy(dst, deldst, ifa->ifa_netmask);
959 dst = deldst;
960 }
961 /*
962 * Get an rtentry that is in the routing tree and
963 * contains the correct info. (if this fails, can't get there).
964 * We set "report" to FALSE so that if it doesn't exist,
965 * it doesn't report an error or clone a route, etc. etc.
966 */
967 rt = rtalloc1(dst, 0, 0UL);
968 if (rt) {
969 /*
970 * Ok so we found the rtentry. it has an extra reference
971 * for us at this stage. we won't need that so
972 * lop that off now.
973 */
974 rt->rt_refcnt--;
975 if (rt->rt_ifa != ifa) {
976 /*
977 * If the interface in the rtentry doesn't match
978 * the interface we are using, then we don't
979 * want to delete it, so return an error.
980 * This seems to be the only point of
981 * this whole RTM_DELETE clause.
982 */
983 if (m)
984 (void) m_free(m);
985 return (flags & RTF_HOST ? EHOSTUNREACH
986 : ENETUNREACH);
987 }
988 }
989 /* XXX */
990 #if 0
991 else {
992 /*
993 * One would think that as we are deleting, and we know
994 * it doesn't exist, we could just return at this point
995 * with an "ELSE" clause, but apparently not..
996 */
997 return (flags & RTF_HOST ? EHOSTUNREACH
998 : ENETUNREACH);
999 }
1000 #endif
1001 }
1002 /*
1003 * Do the actual request
1004 */
1005 error = rtrequest(cmd, dst, ifa->ifa_addr, ifa->ifa_netmask,
1006 flags | ifa->ifa_flags, &nrt);
1007 if (m)
1008 (void) m_free(m);
1009 /*
1010 * If we are deleting, and we found an entry, then
1011 * it's been removed from the tree.. now throw it away.
1012 */
1013 if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) {
1014 /*
1015 * notify any listenning routing agents of the change
1016 */
1017 rt_newaddrmsg(cmd, ifa, error, nrt);
1018 if (rt->rt_refcnt <= 0) {
1019 rt->rt_refcnt++; /* need a 1->0 transition to free */
1020 rtfree(rt);
1021 }
1022 }
1023
1024 /*
1025 * We are adding, and we have a returned routing entry.
1026 * We need to sanity check the result.
1027 */
1028 if (cmd == RTM_ADD && error == 0 && (rt = nrt)) {
1029 /*
1030 * We just wanted to add it.. we don't actually need a reference
1031 */
1032 rt->rt_refcnt--;
1033 /*
1034 * If it came back with an unexpected interface, then it must
1035 * have already existed or something. (XXX)
1036 */
1037 if (rt->rt_ifa != ifa) {
1038 printf("rtinit: wrong ifa (%p) was (%p)\n", ifa,
1039 rt->rt_ifa);
1040 /*
1041 * Ask that the protocol in question
1042 * remove anything it has associated with
1043 * this route and ifaddr.
1044 */
1045 if (rt->rt_ifa->ifa_rtrequest)
1046 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0));
1047 /*
1048 * Remove the referenve to the it's ifaddr.
1049 */
1050 IFAFREE(rt->rt_ifa);
1051 /*
1052 * And substitute in references to the ifaddr
1053 * we are adding.
1054 */
1055 rt->rt_ifa = ifa;
1056 rt->rt_ifp = ifa->ifa_ifp;
1057 ifa->ifa_refcnt++;
1058 /*
1059 * Now ask the protocol to check if it needs
1060 * any special processing in its new form.
1061 */
1062 if (ifa->ifa_rtrequest)
1063 ifa->ifa_rtrequest(RTM_ADD, rt, SA(0));
1064 }
1065 /*
1066 * notify any listenning routing agents of the change
1067 */
1068 rt_newaddrmsg(cmd, ifa, error, nrt);
1069 }
1070 return (error);
1071 }
Cache object: 1f570d10a000c6cf5b364475b3b66c04
|