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