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