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