Convert the struct domain next pointer to an SLIST.
[dragonfly.git] / sys / net / route.c
1 /*
2  * Copyright (c) 2004, 2005 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Jeffrey M. Hsu.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of The DragonFly Project nor the names of its
16  *    contributors may be used to endorse or promote products derived
17  *    from this software without specific, prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 /*
34  * Copyright (c) 2004, 2005 Jeffrey M. Hsu.  All rights reserved.
35  *
36  * License terms: all terms for the DragonFly license above plus the following:
37  *
38  * 4. All advertising materials mentioning features or use of this software
39  *    must display the following acknowledgement:
40  *
41  *      This product includes software developed by Jeffrey M. Hsu
42  *      for the DragonFly Project.
43  *
44  *    This requirement may be waived with permission from Jeffrey Hsu.
45  *    Permission will be granted to any DragonFly user for free.
46  *    This requirement will sunset and may be removed on Jan 31, 2006,
47  *    after which the standard DragonFly license (as shown above) will
48  *    apply.
49  */
50
51 /*
52  * Copyright (c) 1980, 1986, 1991, 1993
53  *      The Regents of the University of California.  All rights reserved.
54  *
55  * Redistribution and use in source and binary forms, with or without
56  * modification, are permitted provided that the following conditions
57  * are met:
58  * 1. Redistributions of source code must retain the above copyright
59  *    notice, this list of conditions and the following disclaimer.
60  * 2. Redistributions in binary form must reproduce the above copyright
61  *    notice, this list of conditions and the following disclaimer in the
62  *    documentation and/or other materials provided with the distribution.
63  * 3. All advertising materials mentioning features or use of this software
64  *    must display the following acknowledgement:
65  *      This product includes software developed by the University of
66  *      California, Berkeley and its contributors.
67  * 4. Neither the name of the University nor the names of its contributors
68  *    may be used to endorse or promote products derived from this software
69  *    without specific prior written permission.
70  *
71  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
81  * SUCH DAMAGE.
82  *
83  *      @(#)route.c     8.3 (Berkeley) 1/9/95
84  * $FreeBSD: src/sys/net/route.c,v 1.59.2.10 2003/01/17 08:04:00 ru Exp $
85  * $DragonFly: src/sys/net/route.c,v 1.17 2005/03/04 02:21:48 hsu Exp $
86  */
87
88 #include "opt_inet.h"
89
90 #include <sys/param.h>
91 #include <sys/systm.h>
92 #include <sys/malloc.h>
93 #include <sys/mbuf.h>
94 #include <sys/socket.h>
95 #include <sys/domain.h>
96 #include <sys/kernel.h>
97
98 #include <net/if.h>
99 #include <net/route.h>
100
101 #include <netinet/in.h>
102 #include <net/ip_mroute/ip_mroute.h>
103
104 static struct rtstat rtstat;
105 struct radix_node_head *rt_tables[AF_MAX+1];
106
107 static void     rt_maskedcopy (struct sockaddr *, struct sockaddr *,
108                                struct sockaddr *);
109 static void     rtable_init (void **);
110
111 static void
112 rtable_init(void **table)
113 {
114         struct domain *dom;
115
116         SLIST_FOREACH(dom, &domains, dom_next)
117                 if (dom->dom_rtattach)
118                         dom->dom_rtattach(&table[dom->dom_family],
119                                           dom->dom_rtoffset);
120 }
121
122 void
123 route_init()
124 {
125         rn_init();      /* initialize all zeroes, all ones, mask table */
126         rtable_init((void **)rt_tables);
127 }
128
129 /*
130  * Packet routing routines.
131  */
132
133 /*
134  * Look up and fill in the "ro_rt" rtentry field in a route structure given
135  * an address in the "ro_dst" field.  Always send a report on a miss and
136  * always clone routes.
137  */
138 void
139 rtalloc(struct route *ro)
140 {
141         rtalloc_ign(ro, 0UL);
142 }
143
144 /*
145  * Look up and fill in the "ro_rt" rtentry field in a route structure given
146  * an address in the "ro_dst" field.  Always send a report on a miss and
147  * optionally clone routes when RTF_CLONING or RTF_PRCLONING are not being
148  * ignored.
149  */
150 void
151 rtalloc_ign(struct route *ro, u_long ignoreflags)
152 {
153         if (ro->ro_rt != NULL) {
154                 if (ro->ro_rt->rt_ifp != NULL && ro->ro_rt->rt_flags & RTF_UP)
155                         return;
156                 rtfree(ro->ro_rt);
157                 ro->ro_rt = NULL;
158         }
159         ro->ro_rt = _rtlookup(&ro->ro_dst, RTL_REPORTMSG, ignoreflags);
160 }
161
162 /*
163  * Look up the route that matches the given "dst" address.
164  *
165  * Route lookup can have the side-effect of creating and returning
166  * a cloned route instead if "dst" matches a cloning route and the
167  * RTF_CLONING and RTF_PRCLONING flags are not being ignored.
168  *
169  * Any route returned has its reference count incremented.
170  */
171 struct rtentry *
172 _rtlookup(struct sockaddr *dst, boolean_t report, u_long ignore)
173 {
174         struct radix_node_head *rnh = rt_tables[dst->sa_family];
175         struct rtentry *rt;
176
177         if (rnh == NULL)
178                 goto unreach;
179
180         /*
181          * Look up route in the radix tree.
182          */
183         rt = (struct rtentry *) rnh->rnh_matchaddr((char *)dst, rnh);
184         if (rt == NULL)
185                 goto unreach;
186
187         /*
188          * Handle cloning routes.
189          */
190         if ((rt->rt_flags & ~ignore & (RTF_CLONING | RTF_PRCLONING)) != 0) {
191                 struct rtentry *clonedroute;
192                 int error;
193
194                 clonedroute = rt;       /* copy in/copy out parameter */
195                 error = rtrequest(RTM_RESOLVE, dst, NULL, NULL, 0,
196                                   &clonedroute);        /* clone the route */
197                 if (error != 0) {       /* cloning failed */
198                         if (report)
199                                 rt_dstmsg(RTM_MISS, dst, error);
200                         rt->rt_refcnt++;
201                         return (rt);    /* return the uncloned route */
202                 }
203                 if (report) {
204                         if (clonedroute->rt_flags & RTF_XRESOLVE)
205                                 rt_dstmsg(RTM_RESOLVE, dst, 0);
206                         else
207                                 rt_rtmsg(RTM_ADD, clonedroute,
208                                          clonedroute->rt_ifp, 0);
209                 }
210                 return (clonedroute);   /* return cloned route */
211         }
212
213         /*
214          * Increment the reference count of the matched route and return.
215          */
216         rt->rt_refcnt++;
217         return (rt);
218
219 unreach:
220         rtstat.rts_unreach++;
221         if (report)
222                 rt_dstmsg(RTM_MISS, dst, 0);
223         return (NULL);
224 }
225
226 void
227 rtfree(struct rtentry *rt)
228 {
229         KASSERT(rt->rt_refcnt > 0, ("rtfree: rt_refcnt %ld", rt->rt_refcnt));
230
231         --rt->rt_refcnt;
232         if (rt->rt_refcnt == 0) {
233                 struct radix_node_head *rnh = rt_tables[rt_key(rt)->sa_family];
234
235                 if (rnh->rnh_close)
236                         rnh->rnh_close((struct radix_node *)rt, rnh);
237                 if (!(rt->rt_flags & RTF_UP)) {
238                         /* deallocate route */
239                         if (rt->rt_ifa != NULL)
240                                 IFAFREE(rt->rt_ifa);
241                         if (rt->rt_parent != NULL)
242                                 RTFREE(rt->rt_parent);  /* recursive call! */
243                         Free(rt_key(rt));
244                         Free(rt);
245                 }
246         }
247 }
248
249 /*
250  * Force a routing table entry to the specified
251  * destination to go through the given gateway.
252  * Normally called as a result of a routing redirect
253  * message from the network layer.
254  *
255  * N.B.: must be called at splnet
256  */
257 void
258 rtredirect(
259         struct sockaddr *dst,
260         struct sockaddr *gateway,
261         struct sockaddr *netmask,
262         int flags,
263         struct sockaddr *src,
264         struct rtentry **rtp)
265 {
266         struct rtentry *rt;
267         struct rt_addrinfo info;
268         struct ifaddr *ifa;
269         short *stat = NULL;
270         int error;
271
272         /* verify the gateway is directly reachable */
273         if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
274                 error = ENETUNREACH;
275                 goto out;
276         }
277
278         /*
279          * If the redirect isn't from our current router for this dst,
280          * it's either old or wrong.  If it redirects us to ourselves,
281          * we have a routing loop, perhaps as a result of an interface
282          * going down recently.
283          */
284         if (!(flags & RTF_DONE) &&
285             (rt = rtpurelookup(dst)) != NULL &&
286             (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) {
287                 error = EINVAL;
288                 goto done;
289         } else if (ifa_ifwithaddr(gateway)) {
290                 error = EHOSTUNREACH;
291                 goto done;
292         }
293
294         /*
295          * Create a new entry if we just got back a wildcard entry
296          * or the the lookup failed.  This is necessary for hosts
297          * which use routing redirects generated by smart gateways
298          * to dynamically build the routing tables.
299          */
300         if (rt == NULL || (rt_mask(rt) != NULL && rt_mask(rt)->sa_len < 2))
301                 goto create;
302
303         /*
304          * Don't listen to the redirect if it's for a route to an interface.
305          */
306         if (rt->rt_flags & RTF_GATEWAY) {
307                 if ((!(rt->rt_flags & RTF_HOST)) && (flags & RTF_HOST)) {
308                         /*
309                          * Changing from route to net => route to host.
310                          * Create new route, rather than smashing route to net.
311                          */
312 create:
313                         if (rt != NULL)
314                                 rtfree(rt);
315                         flags |=  RTF_GATEWAY | RTF_DYNAMIC;
316                         bzero(&info, sizeof info);
317                         info.rti_info[RTAX_DST] = dst;
318                         info.rti_info[RTAX_GATEWAY] = gateway;
319                         info.rti_info[RTAX_NETMASK] = netmask;
320                         info.rti_ifa = ifa;
321                         info.rti_flags = flags;
322                         rt = NULL;
323                         error = rtrequest1(RTM_ADD, &info, &rt);
324                         if (rt != NULL)
325                                 flags = rt->rt_flags;
326                         stat = &rtstat.rts_dynamic;
327                 } else {
328                         /*
329                          * Smash the current notion of the gateway to
330                          * this destination.  Should check about netmask!!!
331                          */
332                         rt->rt_flags |= RTF_MODIFIED;
333                         flags |= RTF_MODIFIED;
334                         stat = &rtstat.rts_newgateway;
335                         /* Add the key and gateway (in one malloc'ed chunk). */
336                         rt_setgate(rt, rt_key(rt), gateway);
337                         error = 0;
338                 }
339         } else {
340                 error = EHOSTUNREACH;
341         }
342
343 done:
344         if (rt != NULL) {
345                 if (rtp != NULL && error == 0)
346                         *rtp = rt;
347                 else
348                         rtfree(rt);
349         }
350
351 out:
352         if (error != 0)
353                 rtstat.rts_badredirect++;
354         else if (stat != NULL)
355                 (*stat)++;
356
357         bzero(&info, sizeof info);
358         info.rti_info[RTAX_DST] = dst;
359         info.rti_info[RTAX_GATEWAY] = gateway;
360         info.rti_info[RTAX_NETMASK] = netmask;
361         info.rti_info[RTAX_AUTHOR] = src;
362         rt_missmsg(RTM_REDIRECT, &info, flags, error);
363 }
364
365 /*
366 * Routing table ioctl interface.
367 */
368 int
369 rtioctl(u_long req, caddr_t data, struct thread *td)
370 {
371 #ifdef INET
372         /* Multicast goop, grrr... */
373         return mrt_ioctl ? mrt_ioctl(req, data) : EOPNOTSUPP;
374 #else
375         return ENXIO;
376 #endif
377 }
378
379 struct ifaddr *
380 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
381 {
382         struct ifaddr *ifa;
383
384         if (!(flags & RTF_GATEWAY)) {
385                 /*
386                  * If we are adding a route to an interface,
387                  * and the interface is a point-to-point link,
388                  * we should search for the destination
389                  * as our clue to the interface.  Otherwise
390                  * we can use the local address.
391                  */
392                 ifa = NULL;
393                 if (flags & RTF_HOST) {
394                         ifa = ifa_ifwithdstaddr(dst);
395                 }
396                 if (ifa == NULL)
397                         ifa = ifa_ifwithaddr(gateway);
398         } else {
399                 /*
400                  * If we are adding a route to a remote net
401                  * or host, the gateway may still be on the
402                  * other end of a pt to pt link.
403                  */
404                 ifa = ifa_ifwithdstaddr(gateway);
405         }
406         if (ifa == NULL)
407                 ifa = ifa_ifwithnet(gateway);
408         if (ifa == NULL) {
409                 struct rtentry *rt;
410
411                 rt = rtpurelookup(gateway);
412                 if (rt == NULL)
413                         return (NULL);
414                 rt->rt_refcnt--;
415                 if ((ifa = rt->rt_ifa) == NULL)
416                         return (NULL);
417         }
418         if (ifa->ifa_addr->sa_family != dst->sa_family) {
419                 struct ifaddr *oifa = ifa;
420
421                 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
422                 if (ifa == NULL)
423                         ifa = oifa;
424         }
425         return (ifa);
426 }
427
428 static int rt_fixdelete (struct radix_node *, void *);
429 static int rt_fixchange (struct radix_node *, void *);
430
431 struct rtfc_arg {
432         struct rtentry *rt0;
433         struct radix_node_head *rnh;
434 };
435
436 /*
437  * Set rtinfo->rti_ifa and rtinfo->rti_ifp.
438  */
439 int
440 rt_getifa(struct rt_addrinfo *rtinfo)
441 {
442         struct sockaddr *gateway = rtinfo->rti_info[RTAX_GATEWAY];
443         struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
444         struct sockaddr *ifaaddr = rtinfo->rti_info[RTAX_IFA];
445         int flags = rtinfo->rti_flags;
446
447         /*
448          * ifp may be specified by sockaddr_dl
449          * when protocol address is ambiguous.
450          */
451         if (rtinfo->rti_ifp == NULL) {
452                 struct sockaddr *ifpaddr;
453
454                 ifpaddr = rtinfo->rti_info[RTAX_IFP];
455                 if (ifpaddr != NULL && ifpaddr->sa_family == AF_LINK) {
456                         struct ifaddr *ifa;
457
458                         ifa = ifa_ifwithnet(ifpaddr);
459                         if (ifa != NULL)
460                                 rtinfo->rti_ifp = ifa->ifa_ifp;
461                 }
462         }
463
464         if (rtinfo->rti_ifa == NULL && ifaaddr != NULL)
465                 rtinfo->rti_ifa = ifa_ifwithaddr(ifaaddr);
466         if (rtinfo->rti_ifa == NULL) {
467                 struct sockaddr *sa;
468
469                 sa = ifaaddr != NULL ? ifaaddr :
470                     (gateway != NULL ? gateway : dst);
471                 if (sa != NULL && rtinfo->rti_ifp != NULL)
472                         rtinfo->rti_ifa = ifaof_ifpforaddr(sa, rtinfo->rti_ifp);
473                 else if (dst != NULL && gateway != NULL)
474                         rtinfo->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
475                 else if (sa != NULL)
476                         rtinfo->rti_ifa = ifa_ifwithroute(flags, sa, sa);
477         }
478         if (rtinfo->rti_ifa == NULL)
479                 return (ENETUNREACH);
480
481         if (rtinfo->rti_ifp == NULL)
482                 rtinfo->rti_ifp = rtinfo->rti_ifa->ifa_ifp;
483         return (0);
484 }
485
486 /*
487  * Do appropriate manipulations of a routing tree given
488  * all the bits of info needed
489  */
490 int
491 rtrequest(
492         int req,
493         struct sockaddr *dst,
494         struct sockaddr *gateway,
495         struct sockaddr *netmask,
496         int flags,
497         struct rtentry **ret_nrt)
498 {
499         struct rt_addrinfo info;
500
501         bzero(&info, sizeof info);
502         info.rti_flags = flags;
503         info.rti_info[RTAX_DST] = dst;
504         info.rti_info[RTAX_GATEWAY] = gateway;
505         info.rti_info[RTAX_NETMASK] = netmask;
506         return rtrequest1(req, &info, ret_nrt);
507 }
508
509 int
510 rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
511 {
512         struct sockaddr *dst = info->rti_info[RTAX_DST];
513         struct rtentry *rt;
514         struct radix_node *rn;
515         struct radix_node_head *rnh;
516         struct ifaddr *ifa;
517         struct sockaddr *ndst;
518         int error = 0;
519         int s;
520
521 #define gotoerr(x) { error = x ; goto bad; }
522
523         s = splnet();
524         /*
525          * Find the correct routing tree to use for this Address Family
526          */
527         if ((rnh = rt_tables[dst->sa_family]) == NULL)
528                 gotoerr(EAFNOSUPPORT);
529
530         /*
531          * If we are adding a host route then we don't want to put
532          * a netmask in the tree, nor do we want to clone it.
533          */
534         if (info->rti_flags & RTF_HOST) {
535                 info->rti_info[RTAX_NETMASK] = NULL;
536                 info->rti_flags &= ~(RTF_CLONING | RTF_PRCLONING);
537         }
538
539         switch (req) {
540         case RTM_DELETE:
541                 /* Remove the item from the tree. */
542                 rn = rnh->rnh_deladdr((char *)info->rti_info[RTAX_DST],
543                                       (char *)info->rti_info[RTAX_NETMASK],
544                                       rnh);
545                 if (rn == NULL)
546                         gotoerr(ESRCH);
547                 KASSERT(!(rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)),
548                         ("rnh_deladdr returned flags 0x%x", rn->rn_flags));
549                 rt = (struct rtentry *)rn;
550
551                 /* Free any routes cloned from this one. */
552                 if ((rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) &&
553                     rt_mask(rt) != NULL) {
554                         rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
555                                                (char *)rt_mask(rt),
556                                                rt_fixdelete, rt);
557                 }
558
559                 if (rt->rt_gwroute != NULL) {
560                         RTFREE(rt->rt_gwroute);
561                         rt->rt_gwroute = NULL;
562                 }
563
564                 /*
565                  * NB: RTF_UP must be set during the search above,
566                  * because we might delete the last ref, causing
567                  * rt to get freed prematurely.
568                  */
569                 rt->rt_flags &= ~RTF_UP;
570
571                 /* Give the protocol a chance to keep things in sync. */
572                 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
573                         ifa->ifa_rtrequest(RTM_DELETE, rt, info);
574
575                 /*
576                  * If the caller wants it, then it can have it,
577                  * but it's up to it to free the rtentry as we won't be
578                  * doing it.
579                  */
580                 KASSERT(rt->rt_refcnt >= 0,
581                         ("rtrequest1(DELETE): refcnt %ld", rt->rt_refcnt));
582                 if (ret_nrt != NULL) {
583                         *ret_nrt = rt;
584                 } else if (rt->rt_refcnt == 0) {
585                         rt->rt_refcnt++;  /* refcnt > 0 required for rtfree() */
586                         rtfree(rt);
587                 }
588                 break;
589
590         case RTM_RESOLVE:
591                 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
592                         gotoerr(EINVAL);
593                 ifa = rt->rt_ifa;
594                 info->rti_flags =
595                     rt->rt_flags & ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC);
596                 info->rti_flags |= RTF_WASCLONED;
597                 info->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
598                 if ((info->rti_info[RTAX_NETMASK] = rt->rt_genmask) == NULL)
599                         info->rti_flags |= RTF_HOST;
600                 goto makeroute;
601
602         case RTM_ADD:
603                 KASSERT(!(info->rti_flags & RTF_GATEWAY) ||
604                         info->rti_info[RTAX_GATEWAY] != NULL,
605                     ("rtrequest: GATEWAY but no gateway"));
606
607                 if (info->rti_ifa == NULL && (error = rt_getifa(info)))
608                         gotoerr(error);
609                 ifa = info->rti_ifa;
610 makeroute:
611                 R_Malloc(rt, struct rtentry *, sizeof *rt);
612                 if (rt == NULL)
613                         gotoerr(ENOBUFS);
614                 bzero(rt, sizeof *rt);
615                 rt->rt_flags = RTF_UP | info->rti_flags;
616                 error = rt_setgate(rt, dst, info->rti_info[RTAX_GATEWAY]);
617                 if (error != 0) {
618                         Free(rt);
619                         gotoerr(error);
620                 }
621
622                 ndst = rt_key(rt);
623                 if (info->rti_info[RTAX_NETMASK] != NULL)
624                         rt_maskedcopy(dst, ndst, info->rti_info[RTAX_NETMASK]);
625                 else
626                         bcopy(dst, ndst, dst->sa_len);
627
628                 /*
629                  * Note that we now have a reference to the ifa.
630                  * This moved from below so that rnh->rnh_addaddr() can
631                  * examine the ifa and  ifa->ifa_ifp if it so desires.
632                  */
633                 IFAREF(ifa);
634                 rt->rt_ifa = ifa;
635                 rt->rt_ifp = ifa->ifa_ifp;
636                 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
637
638                 rn = rnh->rnh_addaddr((char *)ndst,
639                                       (char *)info->rti_info[RTAX_NETMASK],
640                                       rnh, rt->rt_nodes);
641                 if (rn == NULL) {
642                         struct rtentry *oldrt;
643
644                         /*
645                          * We already have one of these in the tree.
646                          * We do a special hack: if the old route was
647                          * cloned, then we blow it away and try
648                          * re-inserting the new one.
649                          */
650                         oldrt = rtpurelookup(ndst);
651                         if (oldrt != NULL) {
652                                 --oldrt->rt_refcnt;
653                                 if (oldrt->rt_flags & RTF_WASCLONED) {
654                                         rtrequest(RTM_DELETE, rt_key(oldrt),
655                                                   oldrt->rt_gateway,
656                                                   rt_mask(oldrt),
657                                                   oldrt->rt_flags, NULL);
658                                         rn = rnh->rnh_addaddr((char *)ndst,
659                                                   (char *)
660                                                   info->rti_info[RTAX_NETMASK],
661                                                   rnh, rt->rt_nodes);
662                                 }
663                         }
664                 }
665
666                 /*
667                  * If it still failed to go into the tree,
668                  * then un-make it (this should be a function).
669                  */
670                 if (rn == NULL) {
671                         if (rt->rt_gwroute != NULL)
672                                 rtfree(rt->rt_gwroute);
673                         IFAFREE(ifa);
674                         Free(rt_key(rt));
675                         Free(rt);
676                         gotoerr(EEXIST);
677                 }
678
679                 /*
680                  * If we got here from RESOLVE, then we are cloning
681                  * so clone the rest, and note that we
682                  * are a clone (and increment the parent's references)
683                  */
684                 if (req == RTM_RESOLVE) {
685                         rt->rt_rmx = (*ret_nrt)->rt_rmx;    /* copy metrics */
686                         rt->rt_rmx.rmx_pksent = 0;  /* reset packet counter */
687                         if ((*ret_nrt)->rt_flags &
688                                        (RTF_CLONING | RTF_PRCLONING)) {
689                                 rt->rt_parent = *ret_nrt;
690                                 (*ret_nrt)->rt_refcnt++;
691                         }
692                 }
693
694                 /*
695                  * if this protocol has something to add to this then
696                  * allow it to do that as well.
697                  */
698                 if (ifa->ifa_rtrequest != NULL)
699                         ifa->ifa_rtrequest(req, rt, info);
700
701                 /*
702                  * We repeat the same procedure from rt_setgate() here because
703                  * it doesn't fire when we call it there because the node
704                  * hasn't been added to the tree yet.
705                  */
706                 if (req == RTM_ADD && !(rt->rt_flags & RTF_HOST) &&
707                     rt_mask(rt) != NULL) {
708                         struct rtfc_arg arg = { rt, rnh };
709
710                         rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
711                                                (char *)rt_mask(rt),
712                                                rt_fixchange, &arg);
713                 }
714
715                 /*
716                  * Return the resulting rtentry,
717                  * increasing the number of references by one.
718                  */
719                 if (ret_nrt != NULL) {
720                         rt->rt_refcnt++;
721                         *ret_nrt = rt;
722                 }
723                 break;
724         default:
725                 error = EOPNOTSUPP;
726         }
727 bad:
728         splx(s);
729         return (error);
730 }
731
732 /*
733  * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
734  * (i.e., the routes related to it by the operation of cloning).  This
735  * routine is iterated over all potential former-child-routes by way of
736  * rnh->rnh_walktree_from() above, and those that actually are children of
737  * the late parent (passed in as VP here) are themselves deleted.
738  */
739 static int
740 rt_fixdelete(struct radix_node *rn, void *vp)
741 {
742         struct rtentry *rt = (struct rtentry *)rn;
743         struct rtentry *rt0 = vp;
744
745         if (rt->rt_parent == rt0 &&
746             !(rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) {
747                 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
748                                  rt->rt_flags, NULL);
749         }
750         return 0;
751 }
752
753 /*
754  * This routine is called from rt_setgate() to do the analogous thing for
755  * adds and changes.  There is the added complication in this case of a
756  * middle insert; i.e., insertion of a new network route between an older
757  * network route and (cloned) host routes.  For this reason, a simple check
758  * of rt->rt_parent is insufficient; each candidate route must be tested
759  * against the (mask, value) of the new route (passed as before in vp)
760  * to see if the new route matches it.
761  *
762  * XXX - it may be possible to do fixdelete() for changes and reserve this
763  * routine just for adds.  I'm not sure why I thought it was necessary to do
764  * changes this way.
765  */
766 #ifdef DEBUG
767 static int rtfcdebug = 0;
768 #endif
769
770 static int
771 rt_fixchange(struct radix_node *rn, void *vp)
772 {
773         struct rtentry *rt = (struct rtentry *)rn;
774         struct rtfc_arg *ap = vp;
775         struct rtentry *rt0 = ap->rt0;
776         struct radix_node_head *rnh = ap->rnh;
777         u_char *xk1, *xm1, *xk2, *xmp;
778         int i, len, mlen;
779
780 #ifdef DEBUG
781         if (rtfcdebug)
782                 printf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0);
783 #endif
784
785         if (rt->rt_parent == NULL ||
786             (rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) {
787 #ifdef DEBUG
788                 if (rtfcdebug) printf("no parent, pinned or cloning\n");
789 #endif
790                 return 0;
791         }
792
793         if (rt->rt_parent == rt0) {
794 #ifdef DEBUG
795                 if (rtfcdebug) printf("parent match\n");
796 #endif
797                 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
798                                  rt->rt_flags, NULL);
799         }
800
801         /*
802          * There probably is a function somewhere which does this...
803          * if not, there should be.
804          */
805         len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len);
806
807         xk1 = (u_char *)rt_key(rt0);
808         xm1 = (u_char *)rt_mask(rt0);
809         xk2 = (u_char *)rt_key(rt);
810
811         /* avoid applying a less specific route */
812         xmp = (u_char *)rt_mask(rt->rt_parent);
813         mlen = rt_key(rt->rt_parent)->sa_len;
814         if (mlen > rt_key(rt0)->sa_len) {
815 #ifdef DEBUG
816                 if (rtfcdebug)
817                         printf("rt_fixchange: inserting a less "
818                                "specific route\n");
819 #endif
820                 return 0;
821         }
822         for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++) {
823                 if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i]) {
824 #ifdef DEBUG
825                         if (rtfcdebug)
826                                 printf("rt_fixchange: inserting a less "
827                                        "specific route\n");
828 #endif
829                         return 0;
830                 }
831         }
832
833         for (i = rnh->rnh_treetop->rn_offset; i < len; i++) {
834                 if ((xk2[i] & xm1[i]) != xk1[i]) {
835 #ifdef DEBUG
836                         if (rtfcdebug) printf("no match\n");
837 #endif
838                         return 0;
839                 }
840         }
841
842         /*
843          * OK, this node is a clone, and matches the node currently being
844          * changed/added under the node's mask.  So, get rid of it.
845          */
846 #ifdef DEBUG
847         if (rtfcdebug) printf("deleting\n");
848 #endif
849         return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
850                          rt->rt_flags, NULL);
851 }
852
853 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
854
855 int
856 rt_setgate(struct rtentry *rt0, struct sockaddr *dst, struct sockaddr *gate)
857 {
858         char *space, *oldspace;
859         int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
860         struct rtentry *rt = rt0;
861         struct radix_node_head *rnh = rt_tables[dst->sa_family];
862
863         /*
864          * A host route with the destination equal to the gateway
865          * will interfere with keeping LLINFO in the routing
866          * table, so disallow it.
867          */
868         if (((rt0->rt_flags & (RTF_HOST | RTF_GATEWAY | RTF_LLINFO)) ==
869                               (RTF_HOST | RTF_GATEWAY)) &&
870             dst->sa_len == gate->sa_len &&
871             sa_equal(dst, gate)) {
872                 /*
873                  * The route might already exist if this is an RTM_CHANGE
874                  * or a routing redirect, so try to delete it.
875                  */
876                 if (rt_key(rt0) != NULL)
877                         rtrequest(RTM_DELETE, rt_key(rt0), rt0->rt_gateway,
878                                   rt_mask(rt0), rt0->rt_flags, NULL);
879                 return EADDRNOTAVAIL;
880         }
881
882         /*
883          * Both dst and gateway are stored in the same malloc'ed chunk
884          * (If I ever get my hands on....)
885          * if we need to malloc a new chunk, then keep the old one around
886          * till we don't need it any more.
887          */
888         if (rt->rt_gateway == NULL || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
889                 oldspace = (char *)rt_key(rt);
890                 R_Malloc(space, char *, dlen + glen);
891                 if (space == NULL)
892                         return ENOBUFS;
893                 rt->rt_nodes->rn_key = space;
894         } else {
895                 space = (char *)rt_key(rt);     /* Just use the old space. */
896                 oldspace = NULL;
897         }
898
899         /* Set the gateway value. */
900         rt->rt_gateway = (struct sockaddr *)(space + dlen);
901         bcopy(gate, rt->rt_gateway, glen);
902
903         if (oldspace != NULL) {
904                 /*
905                  * If we allocated a new chunk, preserve the original dst.
906                  * This way, rt_setgate() really just sets the gate
907                  * and leaves the dst field alone.
908                  */
909                 bcopy(dst, space, dlen);
910                 Free(oldspace);
911         }
912
913         /*
914          * If there is already a gwroute, it's now almost definitely wrong
915          * so drop it.
916          */
917         if (rt->rt_gwroute != NULL) {
918                 RTFREE(rt->rt_gwroute);
919                 rt->rt_gwroute = NULL;
920         }
921         if (rt->rt_flags & RTF_GATEWAY) {
922                 /*
923                  * Cloning loop avoidance: In the presence of
924                  * protocol-cloning and bad configuration, it is
925                  * possible to get stuck in bottomless mutual recursion
926                  * (rtrequest rt_setgate rtlookup).  We avoid this
927                  * by not allowing protocol-cloning to operate for
928                  * gateways (which is probably the correct choice
929                  * anyway), and avoid the resulting reference loops
930                  * by disallowing any route to run through itself as
931                  * a gateway.  This is obviously mandatory when we
932                  * get rt->rt_output().
933                  *
934                  * This breaks TTCP for hosts outside the gateway!  XXX JH
935                  */
936                 rt->rt_gwroute = _rtlookup(gate, RTL_REPORTMSG, RTF_PRCLONING);
937                 if (rt->rt_gwroute == rt) {
938                         rt->rt_gwroute = NULL;
939                         --rt->rt_refcnt;
940                         return EDQUOT; /* failure */
941                 }
942         }
943
944         /*
945          * This isn't going to do anything useful for host routes, so
946          * don't bother.  Also make sure we have a reasonable mask
947          * (we don't yet have one during adds).
948          */
949         if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) {
950                 struct rtfc_arg arg = { rt, rnh };
951
952                 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
953                                        (char *)rt_mask(rt),
954                                        rt_fixchange, &arg);
955         }
956
957         return 0;
958 }
959
960 static void
961 rt_maskedcopy(
962         struct sockaddr *src,
963         struct sockaddr *dst,
964         struct sockaddr *netmask)
965 {
966         u_char *cp1 = (u_char *)src;
967         u_char *cp2 = (u_char *)dst;
968         u_char *cp3 = (u_char *)netmask;
969         u_char *cplim = cp2 + *cp3;
970         u_char *cplim2 = cp2 + *cp1;
971
972         *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
973         cp3 += 2;
974         if (cplim > cplim2)
975                 cplim = cplim2;
976         while (cp2 < cplim)
977                 *cp2++ = *cp1++ & *cp3++;
978         if (cp2 < cplim2)
979                 bzero(cp2, cplim2 - cp2);
980 }
981
982 int
983 rt_llroute(struct sockaddr *dst, struct rtentry *rt0, struct rtentry **drt)
984 {
985         struct rtentry *up_rt, *rt;
986
987         if (!(rt0->rt_flags & RTF_UP)) {
988                 up_rt = rtlookup(dst);
989                 if (up_rt == NULL)
990                         return (EHOSTUNREACH);
991                 up_rt->rt_refcnt--;
992         } else
993                 up_rt = rt0;
994         if (up_rt->rt_flags & RTF_GATEWAY) {
995                 if (up_rt->rt_gwroute == NULL) {
996                         up_rt->rt_gwroute = rtlookup(up_rt->rt_gateway);
997                         if (up_rt->rt_gwroute == NULL)
998                                 return (EHOSTUNREACH);
999                 } else if (!(up_rt->rt_gwroute->rt_flags & RTF_UP)) {
1000                         rtfree(up_rt->rt_gwroute);
1001                         up_rt->rt_gwroute = rtlookup(up_rt->rt_gateway);
1002                         if (up_rt->rt_gwroute == NULL)
1003                                 return (EHOSTUNREACH);
1004                 }
1005                 rt = up_rt->rt_gwroute;
1006         } else
1007                 rt = up_rt;
1008         if (rt->rt_flags & RTF_REJECT &&
1009             (rt->rt_rmx.rmx_expire == 0 ||              /* rt doesn't expire */
1010              time_second < rt->rt_rmx.rmx_expire))      /* rt not expired */
1011                 return (rt->rt_flags & RTF_HOST ?  EHOSTDOWN : EHOSTUNREACH);
1012         *drt = rt;
1013         return 0;
1014 }
1015
1016 /*
1017  * Set up a routing table entry, normally
1018  * for an interface.
1019  */
1020 int
1021 rtinit(struct ifaddr *ifa, int cmd, int flags)
1022 {
1023         struct sockaddr *dst, *deldst, *netmask;
1024         struct rtentry *rt;
1025         struct rtentry *nrt = NULL;
1026         struct mbuf *m = NULL;
1027         struct radix_node_head *rnh;
1028         struct radix_node *rn;
1029         struct rt_addrinfo info;
1030         int error;
1031
1032         if (flags & RTF_HOST) {
1033                 dst = ifa->ifa_dstaddr;
1034                 netmask = NULL;
1035         } else {
1036                 dst = ifa->ifa_addr;
1037                 netmask = ifa->ifa_netmask;
1038         }
1039         /*
1040          * If it's a delete, check that if it exists, it's on the correct
1041          * interface or we might scrub a route to another ifa which would
1042          * be confusing at best and possibly worse.
1043          */
1044         if (cmd == RTM_DELETE) {
1045                 /*
1046                  * It's a delete, so it should already exist..
1047                  * If it's a net, mask off the host bits
1048                  * (Assuming we have a mask)
1049                  */
1050                 if (netmask != NULL) {
1051                         m = m_get(MB_DONTWAIT, MT_SONAME);
1052                         if (m == NULL)
1053                                 return (ENOBUFS);
1054                         deldst = mtod(m, struct sockaddr *);
1055                         rt_maskedcopy(dst, deldst, netmask);
1056                         dst = deldst;
1057                 }
1058                 /*
1059                  * Look up an rtentry that is in the routing tree and
1060                  * contains the correct info.
1061                  */
1062                 if ((rnh = rt_tables[dst->sa_family]) == NULL ||
1063                     (rn = rnh->rnh_lookup((char *)dst,
1064                                           (char *)netmask, rnh)) == NULL ||
1065                     ((struct rtentry *)rn)->rt_ifa != ifa ||
1066                     !sa_equal((struct sockaddr *)rn->rn_key, dst)) {
1067                         if (m != NULL)
1068                                 m_free(m);
1069                         return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1070                 }
1071                 /* XXX */
1072 #if 0
1073                 else {
1074                         /*
1075                          * One would think that as we are deleting, and we know
1076                          * it doesn't exist, we could just return at this point
1077                          * with an "ELSE" clause, but apparently not..
1078                          */
1079                         return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1080                 }
1081 #endif
1082         }
1083         /*
1084          * Do the actual request
1085          */
1086         bzero(&info, sizeof info);
1087         info.rti_ifa = ifa;
1088         info.rti_flags = flags | ifa->ifa_flags;
1089         info.rti_info[RTAX_DST] = dst;
1090         info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1091         info.rti_info[RTAX_NETMASK] = netmask;
1092         error = rtrequest1(cmd, &info, &nrt);
1093         if (error == 0 && (rt = nrt) != NULL) {
1094                 /*
1095                  * notify any listening routing agents of the change
1096                  */
1097                 rt_newaddrmsg(cmd, ifa, error, rt);
1098                 if (cmd == RTM_DELETE) {
1099                         /*
1100                          * If we are deleting, and we found an entry, then
1101                          * it's been removed from the tree.. now throw it away.
1102                          */
1103                         if (rt->rt_refcnt == 0) {
1104                                 rt->rt_refcnt++; /* make a 1->0 transition */
1105                                 rtfree(rt);
1106                         }
1107                 } else if (cmd == RTM_ADD) {
1108                         /*
1109                          * We just wanted to add it.. we don't actually
1110                          * need a reference.
1111                          */
1112                         rt->rt_refcnt--;
1113                 }
1114         }
1115         if (m != NULL)
1116                 m_free(m);
1117         return (error);
1118 }
1119
1120 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1121 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);