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