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