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