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