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