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