Merge remote-tracking branch 'origin/vendor/LIBARCHIVE'
[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) 1980, 1986, 1991, 1993
35  *      The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *      @(#)route.c     8.3 (Berkeley) 1/9/95
62  * $FreeBSD: src/sys/net/route.c,v 1.59.2.10 2003/01/17 08:04:00 ru Exp $
63  */
64
65 #include "opt_inet.h"
66 #include "opt_mpls.h"
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/malloc.h>
71 #include <sys/mbuf.h>
72 #include <sys/socket.h>
73 #include <sys/domain.h>
74 #include <sys/kernel.h>
75 #include <sys/sysctl.h>
76 #include <sys/globaldata.h>
77 #include <sys/thread.h>
78
79 #include <net/if.h>
80 #include <net/if_var.h>
81 #include <net/route.h>
82 #include <net/netisr.h>
83
84 #include <netinet/in.h>
85 #include <net/ip_mroute/ip_mroute.h>
86
87 #include <sys/thread2.h>
88 #include <sys/msgport2.h>
89 #include <net/netmsg2.h>
90 #include <net/netisr2.h>
91
92 #ifdef MPLS
93 #include <netproto/mpls/mpls.h>
94 #endif
95
96 static struct rtstatistics rtstatistics_percpu[MAXCPU] __cachealign;
97 #define rtstat  rtstatistics_percpu[mycpuid]
98
99 struct radix_node_head *rt_tables[MAXCPU][AF_MAX+1];
100
101 static void     rt_maskedcopy (struct sockaddr *, struct sockaddr *,
102                                struct sockaddr *);
103 static void rtable_init(void);
104 static void rtinit_rtrequest_callback(int, int, struct rt_addrinfo *,
105                                       struct rtentry *, void *);
106
107 static void rtredirect_msghandler(netmsg_t msg);
108 static void rtrequest1_msghandler(netmsg_t msg);
109 static void rtsearch_msghandler(netmsg_t msg);
110 static void rtmask_add_msghandler(netmsg_t msg);
111
112 static int rt_setshims(struct rtentry *, struct sockaddr **);
113
114 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RW, 0, "Routing");
115
116 #ifdef ROUTE_DEBUG
117 static int route_debug = 1;
118 SYSCTL_INT(_net_route, OID_AUTO, route_debug, CTLFLAG_RW,
119            &route_debug, 0, "");
120 #endif
121
122 u_long route_kmalloc_limit = 0;
123 TUNABLE_ULONG("net.route.kmalloc_limit", &route_kmalloc_limit);
124
125 /*
126  * Initialize the route table(s) for protocol domains and
127  * create a helper thread which will be responsible for updating
128  * route table entries on each cpu.
129  */
130 void
131 route_init(void)
132 {
133         int cpu;
134
135         if (route_kmalloc_limit)
136                 kmalloc_raise_limit(M_RTABLE, route_kmalloc_limit);
137
138         for (cpu = 0; cpu < netisr_ncpus; ++cpu)
139                 bzero(&rtstatistics_percpu[cpu], sizeof(struct rtstatistics));
140         rn_init();      /* initialize all zeroes, all ones, mask table */
141         rtable_init();  /* call dom_rtattach() on each cpu */
142 }
143
144 static void
145 rtable_init_oncpu(netmsg_t msg)
146 {
147         struct domain *dom;
148         int cpu = mycpuid;
149
150         ASSERT_NETISR_NCPUS(cpu);
151
152         SLIST_FOREACH(dom, &domains, dom_next) {
153                 if (dom->dom_rtattach) {
154                         dom->dom_rtattach(
155                                 (void **)&rt_tables[cpu][dom->dom_family],
156                                 dom->dom_rtoffset);
157                 }
158         }
159         netisr_forwardmsg(&msg->base, cpu + 1);
160 }
161
162 static void
163 rtable_init(void)
164 {
165         struct netmsg_base msg;
166
167         netmsg_init(&msg, NULL, &curthread->td_msgport, 0, rtable_init_oncpu);
168         netisr_domsg_global(&msg);
169 }
170
171 /*
172  * Routing statistics.
173  */
174 static int
175 sysctl_rtstatistics(SYSCTL_HANDLER_ARGS)
176 {
177         int cpu, error = 0;
178
179         for (cpu = 0; cpu < netisr_ncpus; ++cpu) {
180                 if ((error = SYSCTL_OUT(req, &rtstatistics_percpu[cpu],
181                                         sizeof(struct rtstatistics))))
182                                 break;
183                 if ((error = SYSCTL_IN(req, &rtstatistics_percpu[cpu],
184                                         sizeof(struct rtstatistics))))
185                                 break;
186         }
187
188         return (error);
189 }
190 SYSCTL_PROC(_net_route, OID_AUTO, stats, (CTLTYPE_OPAQUE|CTLFLAG_RW),
191         0, 0, sysctl_rtstatistics, "S,rtstatistics", "Routing statistics");
192
193 /*
194  * Packet routing routines.
195  */
196
197 /*
198  * Look up and fill in the "ro_rt" rtentry field in a route structure given
199  * an address in the "ro_dst" field.  Always send a report on a miss and
200  * always clone routes.
201  */
202 void
203 rtalloc(struct route *ro)
204 {
205         rtalloc_ign(ro, 0UL);
206 }
207
208 /*
209  * Look up and fill in the "ro_rt" rtentry field in a route structure given
210  * an address in the "ro_dst" field.  Always send a report on a miss and
211  * optionally clone routes when RTF_CLONING or RTF_PRCLONING are not being
212  * ignored.
213  */
214 void
215 rtalloc_ign(struct route *ro, u_long ignoreflags)
216 {
217         if (ro->ro_rt != NULL) {
218                 if (ro->ro_rt->rt_ifp != NULL && ro->ro_rt->rt_flags & RTF_UP)
219                         return;
220                 rtfree(ro->ro_rt);
221                 ro->ro_rt = NULL;
222         }
223         ro->ro_rt = _rtlookup(&ro->ro_dst, RTL_REPORTMSG, ignoreflags);
224 }
225
226 /*
227  * Look up the route that matches the given "dst" address.
228  *
229  * Route lookup can have the side-effect of creating and returning
230  * a cloned route instead when "dst" matches a cloning route and the
231  * RTF_CLONING and RTF_PRCLONING flags are not being ignored.
232  *
233  * Any route returned has its reference count incremented.
234  */
235 struct rtentry *
236 _rtlookup(struct sockaddr *dst, boolean_t generate_report, u_long ignore)
237 {
238         struct radix_node_head *rnh = rt_tables[mycpuid][dst->sa_family];
239         struct rtentry *rt;
240
241         ASSERT_NETISR_NCPUS(mycpuid);
242
243         if (rnh == NULL)
244                 goto unreach;
245
246         /*
247          * Look up route in the radix tree.
248          */
249         rt = (struct rtentry *) rnh->rnh_matchaddr((char *)dst, rnh);
250         if (rt == NULL)
251                 goto unreach;
252
253         /*
254          * Handle cloning routes.
255          */
256         if ((rt->rt_flags & ~ignore & (RTF_CLONING | RTF_PRCLONING)) != 0) {
257                 struct rtentry *clonedroute;
258                 int error;
259
260                 clonedroute = rt;       /* copy in/copy out parameter */
261                 error = rtrequest(RTM_RESOLVE, dst, NULL, NULL, 0,
262                                   &clonedroute);        /* clone the route */
263                 if (error != 0) {       /* cloning failed */
264                         if (generate_report)
265                                 rt_dstmsg(RTM_MISS, dst, error);
266                         rt->rt_refcnt++;
267                         return (rt);    /* return the uncloned route */
268                 }
269                 if (generate_report) {
270                         if (clonedroute->rt_flags & RTF_XRESOLVE)
271                                 rt_dstmsg(RTM_RESOLVE, dst, 0);
272                         else
273                                 rt_rtmsg(RTM_ADD, clonedroute,
274                                          clonedroute->rt_ifp, 0);
275                 }
276                 return (clonedroute);   /* return cloned route */
277         }
278
279         /*
280          * Increment the reference count of the matched route and return.
281          */
282         rt->rt_refcnt++;
283         return (rt);
284
285 unreach:
286         rtstat.rts_unreach++;
287         if (generate_report)
288                 rt_dstmsg(RTM_MISS, dst, 0);
289         return (NULL);
290 }
291
292 void
293 rtfree(struct rtentry *rt)
294 {
295
296         ASSERT_NETISR_NCPUS(rt->rt_cpuid);
297         KASSERT(rt->rt_refcnt > 0, ("rtfree: rt_refcnt %ld", rt->rt_refcnt));
298
299         --rt->rt_refcnt;
300         if (rt->rt_refcnt == 0) {
301                 struct radix_node_head *rnh =
302                     rt_tables[mycpuid][rt_key(rt)->sa_family];
303
304                 if (rnh->rnh_close)
305                         rnh->rnh_close((struct radix_node *)rt, rnh);
306                 if (!(rt->rt_flags & RTF_UP)) {
307                         /* deallocate route */
308                         if (rt->rt_ifa != NULL)
309                                 IFAFREE(rt->rt_ifa);
310                         if (rt->rt_parent != NULL)
311                                 RTFREE(rt->rt_parent);  /* recursive call! */
312                         Free(rt_key(rt));
313                         Free(rt);
314                 }
315         }
316 }
317
318 static void
319 rtfree_async_dispatch(netmsg_t msg)
320 {
321         struct rtentry *rt = msg->lmsg.u.ms_resultp;
322
323         rtfree(rt);
324         netisr_replymsg(&msg->base, 0);
325 }
326
327 void
328 rtfree_async(struct rtentry *rt)
329 {
330         struct netmsg_base *msg;
331
332         if (IN_NETISR_NCPUS(rt->rt_cpuid)) {
333                 rtfree(rt);
334                 return;
335         }
336
337         KASSERT(rt->rt_refcnt > 0,
338             ("rtfree_async: rt_refcnt %ld", rt->rt_refcnt));
339
340         msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_INTWAIT);
341         netmsg_init(msg, NULL, &netisr_afree_rport, 0, rtfree_async_dispatch);
342         msg->lmsg.u.ms_resultp = rt;
343
344         netisr_sendmsg(msg, rt->rt_cpuid);
345 }
346
347 int
348 rtredirect_oncpu(struct sockaddr *dst, struct sockaddr *gateway,
349                  struct sockaddr *netmask, int flags, struct sockaddr *src)
350 {
351         struct rtentry *rt = NULL;
352         struct rt_addrinfo rtinfo;
353         struct ifaddr *ifa;
354         u_long *stat = NULL;
355         int error;
356
357         ASSERT_NETISR_NCPUS(mycpuid);
358
359         /* verify the gateway is directly reachable */
360         if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
361                 error = ENETUNREACH;
362                 goto out;
363         }
364
365         /*
366          * If the redirect isn't from our current router for this destination,
367          * it's either old or wrong.
368          */
369         if (!(flags & RTF_DONE) &&              /* XXX JH */
370             (rt = rtpurelookup(dst)) != NULL &&
371             (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) {
372                 error = EINVAL;
373                 goto done;
374         }
375
376         /*
377          * If it redirects us to ourselves, we have a routing loop,
378          * perhaps as a result of an interface going down recently.
379          */
380         if (ifa_ifwithaddr(gateway)) {
381                 error = EHOSTUNREACH;
382                 goto done;
383         }
384
385         /*
386          * Create a new entry if the lookup failed or if we got back
387          * a wildcard entry for the default route.  This is necessary
388          * for hosts which use routing redirects generated by smart
389          * gateways to dynamically build the routing tables.
390          */
391         if (rt == NULL)
392                 goto create;
393         if ((rt_mask(rt) != NULL && rt_mask(rt)->sa_len < 2)) {
394                 rtfree(rt);
395                 goto create;
396         }
397
398         /* Ignore redirects for directly connected hosts. */
399         if (!(rt->rt_flags & RTF_GATEWAY)) {
400                 error = EHOSTUNREACH;
401                 goto done;
402         }
403
404         if (!(rt->rt_flags & RTF_HOST) && (flags & RTF_HOST)) {
405                 /*
406                  * Changing from a network route to a host route.
407                  * Create a new host route rather than smashing the
408                  * network route.
409                  */
410 create:
411                 flags |=  RTF_GATEWAY | RTF_DYNAMIC;
412                 bzero(&rtinfo, sizeof(struct rt_addrinfo));
413                 rtinfo.rti_info[RTAX_DST] = dst;
414                 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
415                 rtinfo.rti_info[RTAX_NETMASK] = netmask;
416                 rtinfo.rti_flags = flags;
417                 rtinfo.rti_ifa = ifa;
418                 rt = NULL;      /* copy-in/copy-out parameter */
419                 error = rtrequest1(RTM_ADD, &rtinfo, &rt);
420                 if (rt != NULL)
421                         flags = rt->rt_flags;
422                 stat = &rtstat.rts_dynamic;
423         } else {
424                 /*
425                  * Smash the current notion of the gateway to this destination.
426                  * Should check about netmask!!!
427                  */
428                 rt->rt_flags |= RTF_MODIFIED;
429                 flags |= RTF_MODIFIED;
430
431                 /* We only need to report rtmsg on CPU0 */
432                 rt_setgate(rt, rt_key(rt), gateway,
433                            mycpuid == 0 ? RTL_REPORTMSG : RTL_DONTREPORT);
434                 error = 0;
435                 stat = &rtstat.rts_newgateway;
436         }
437
438 done:
439         if (rt != NULL)
440                 rtfree(rt);
441 out:
442         if (error != 0)
443                 rtstat.rts_badredirect++;
444         else if (stat != NULL)
445                 (*stat)++;
446
447         return error;
448 }
449
450 struct netmsg_rtredirect {
451         struct netmsg_base base;
452         struct sockaddr *dst;
453         struct sockaddr *gateway;
454         struct sockaddr *netmask;
455         int             flags;
456         struct sockaddr *src;
457 };
458
459 /*
460  * Force a routing table entry to the specified
461  * destination to go through the given gateway.
462  * Normally called as a result of a routing redirect
463  * message from the network layer.
464  */
465 void
466 rtredirect(struct sockaddr *dst, struct sockaddr *gateway,
467            struct sockaddr *netmask, int flags, struct sockaddr *src)
468 {
469         struct rt_addrinfo rtinfo;
470         int error;
471         struct netmsg_rtredirect msg;
472
473         netmsg_init(&msg.base, NULL, &curthread->td_msgport,
474                     0, rtredirect_msghandler);
475         msg.dst = dst;
476         msg.gateway = gateway;
477         msg.netmask = netmask;
478         msg.flags = flags;
479         msg.src = src;
480         error = netisr_domsg_global(&msg.base);
481
482         bzero(&rtinfo, sizeof(struct rt_addrinfo));
483         rtinfo.rti_info[RTAX_DST] = dst;
484         rtinfo.rti_info[RTAX_GATEWAY] = gateway;
485         rtinfo.rti_info[RTAX_NETMASK] = netmask;
486         rtinfo.rti_info[RTAX_AUTHOR] = src;
487         rt_missmsg(RTM_REDIRECT, &rtinfo, flags, error);
488 }
489
490 static void
491 rtredirect_msghandler(netmsg_t msg)
492 {
493         struct netmsg_rtredirect *rmsg = (void *)msg;
494
495         rtredirect_oncpu(rmsg->dst, rmsg->gateway, rmsg->netmask,
496                          rmsg->flags, rmsg->src);
497         netisr_forwardmsg(&msg->base, mycpuid + 1);
498 }
499
500 /*
501 * Routing table ioctl interface.
502 */
503 int
504 rtioctl(u_long req, caddr_t data, struct ucred *cred)
505 {
506 #ifdef INET
507         /* Multicast goop, grrr... */
508         return mrt_ioctl ? mrt_ioctl(req, data) : EOPNOTSUPP;
509 #else
510         return ENXIO;
511 #endif
512 }
513
514 struct ifaddr *
515 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
516 {
517         struct ifaddr *ifa;
518
519         if (!(flags & RTF_GATEWAY)) {
520                 /*
521                  * If we are adding a route to an interface,
522                  * and the interface is a point-to-point link,
523                  * we should search for the destination
524                  * as our clue to the interface.  Otherwise
525                  * we can use the local address.
526                  */
527                 ifa = NULL;
528                 if (flags & RTF_HOST) {
529                         ifa = ifa_ifwithdstaddr(dst);
530                 }
531                 if (ifa == NULL)
532                         ifa = ifa_ifwithaddr(gateway);
533         } else {
534                 /*
535                  * If we are adding a route to a remote net
536                  * or host, the gateway may still be on the
537                  * other end of a pt to pt link.
538                  */
539                 ifa = ifa_ifwithdstaddr(gateway);
540         }
541         if (ifa == NULL)
542                 ifa = ifa_ifwithnet(gateway);
543         if (ifa == NULL) {
544                 struct rtentry *rt;
545
546                 rt = rtpurelookup(gateway);
547                 if (rt == NULL)
548                         return (NULL);
549                 rt->rt_refcnt--;
550                 if ((ifa = rt->rt_ifa) == NULL)
551                         return (NULL);
552         }
553         if (ifa->ifa_addr->sa_family != dst->sa_family) {
554                 struct ifaddr *oldifa = ifa;
555
556                 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
557                 if (ifa == NULL)
558                         ifa = oldifa;
559         }
560         return (ifa);
561 }
562
563 static int rt_fixdelete (struct radix_node *, void *);
564 static int rt_fixchange (struct radix_node *, void *);
565
566 struct rtfc_arg {
567         struct rtentry *rt0;
568         struct radix_node_head *rnh;
569 };
570
571 /*
572  * Set rtinfo->rti_ifa and rtinfo->rti_ifp.
573  */
574 int
575 rt_getifa(struct rt_addrinfo *rtinfo)
576 {
577         struct sockaddr *gateway = rtinfo->rti_info[RTAX_GATEWAY];
578         struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
579         struct sockaddr *ifaaddr = rtinfo->rti_info[RTAX_IFA];
580         int flags = rtinfo->rti_flags;
581
582         /*
583          * ifp may be specified by sockaddr_dl
584          * when protocol address is ambiguous.
585          */
586         if (rtinfo->rti_ifp == NULL) {
587                 struct sockaddr *ifpaddr;
588
589                 ifpaddr = rtinfo->rti_info[RTAX_IFP];
590                 if (ifpaddr != NULL && ifpaddr->sa_family == AF_LINK) {
591                         struct ifaddr *ifa;
592
593                         ifa = ifa_ifwithnet(ifpaddr);
594                         if (ifa != NULL)
595                                 rtinfo->rti_ifp = ifa->ifa_ifp;
596                 }
597         }
598
599         if (rtinfo->rti_ifa == NULL && ifaaddr != NULL)
600                 rtinfo->rti_ifa = ifa_ifwithaddr(ifaaddr);
601         if (rtinfo->rti_ifa == NULL) {
602                 struct sockaddr *sa;
603
604                 sa = ifaaddr != NULL ? ifaaddr :
605                     (gateway != NULL ? gateway : dst);
606                 if (sa != NULL && rtinfo->rti_ifp != NULL)
607                         rtinfo->rti_ifa = ifaof_ifpforaddr(sa, rtinfo->rti_ifp);
608                 else if (dst != NULL && gateway != NULL)
609                         rtinfo->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
610                 else if (sa != NULL)
611                         rtinfo->rti_ifa = ifa_ifwithroute(flags, sa, sa);
612         }
613         if (rtinfo->rti_ifa == NULL)
614                 return (ENETUNREACH);
615
616         if (rtinfo->rti_ifp == NULL)
617                 rtinfo->rti_ifp = rtinfo->rti_ifa->ifa_ifp;
618         return (0);
619 }
620
621 /*
622  * Do appropriate manipulations of a routing tree given
623  * all the bits of info needed
624  */
625 int
626 rtrequest(
627         int req,
628         struct sockaddr *dst,
629         struct sockaddr *gateway,
630         struct sockaddr *netmask,
631         int flags,
632         struct rtentry **ret_nrt)
633 {
634         struct rt_addrinfo rtinfo;
635
636         bzero(&rtinfo, sizeof(struct rt_addrinfo));
637         rtinfo.rti_info[RTAX_DST] = dst;
638         rtinfo.rti_info[RTAX_GATEWAY] = gateway;
639         rtinfo.rti_info[RTAX_NETMASK] = netmask;
640         rtinfo.rti_flags = flags;
641         return rtrequest1(req, &rtinfo, ret_nrt);
642 }
643
644 int
645 rtrequest_global(
646         int req,
647         struct sockaddr *dst,
648         struct sockaddr *gateway,
649         struct sockaddr *netmask,
650         int flags)
651 {
652         struct rt_addrinfo rtinfo;
653
654         bzero(&rtinfo, sizeof(struct rt_addrinfo));
655         rtinfo.rti_info[RTAX_DST] = dst;
656         rtinfo.rti_info[RTAX_GATEWAY] = gateway;
657         rtinfo.rti_info[RTAX_NETMASK] = netmask;
658         rtinfo.rti_flags = flags;
659         return rtrequest1_global(req, &rtinfo, NULL, NULL, RTREQ_PRIO_NORM);
660 }
661
662 struct netmsg_rtq {
663         struct netmsg_base      base;
664         int                     req;
665         struct rt_addrinfo      *rtinfo;
666         rtrequest1_callback_func_t callback;
667         void                    *arg;
668 };
669
670 int
671 rtrequest1_global(int req, struct rt_addrinfo *rtinfo,
672     rtrequest1_callback_func_t callback, void *arg, boolean_t req_prio)
673 {
674         struct netmsg_rtq msg;
675         int flags = 0;
676
677         if (req_prio)
678                 flags = MSGF_PRIORITY;
679         netmsg_init(&msg.base, NULL, &curthread->td_msgport, flags,
680             rtrequest1_msghandler);
681         msg.base.lmsg.ms_error = -1;
682         msg.req = req;
683         msg.rtinfo = rtinfo;
684         msg.callback = callback;
685         msg.arg = arg;
686         return (netisr_domsg_global(&msg.base));
687 }
688
689 /*
690  * Handle a route table request on the current cpu.  Since the route table's
691  * are supposed to be identical on each cpu, an error occuring later in the
692  * message chain is considered system-fatal.
693  */
694 static void
695 rtrequest1_msghandler(netmsg_t msg)
696 {
697         struct netmsg_rtq *rmsg = (void *)msg;
698         struct rt_addrinfo rtinfo;
699         struct rtentry *rt = NULL;
700         int error;
701
702         /*
703          * Copy the rtinfo.  We need to make sure that the original
704          * rtinfo, which is setup by the caller, in the netmsg will
705          * _not_ be changed; else the next CPU on the netmsg forwarding
706          * path will see a different rtinfo than what this CPU has seen.
707          */
708         rtinfo = *rmsg->rtinfo;
709
710         error = rtrequest1(rmsg->req, &rtinfo, &rt);
711         if (rt)
712                 --rt->rt_refcnt;
713         if (rmsg->callback)
714                 rmsg->callback(rmsg->req, error, &rtinfo, rt, rmsg->arg);
715
716         /*
717          * RTM_DELETE's are propogated even if an error occurs, since a
718          * cloned route might be undergoing deletion and cloned routes
719          * are not necessarily replicated.  An overall error is returned
720          * only if no cpus have the route in question.
721          */
722         if (rmsg->base.lmsg.ms_error < 0 || error == 0)
723                 rmsg->base.lmsg.ms_error = error;
724
725         if (error && rmsg->req != RTM_DELETE) {
726                 if (mycpuid != 0) {
727                         panic("rtrequest1_msghandler: rtrequest table req %d, "
728                             "failed on cpu%d, error %d\n",
729                             rmsg->req, mycpuid, error);
730                 }
731                 netisr_replymsg(&rmsg->base, error);
732         } else {
733                 netisr_forwardmsg_error(&rmsg->base, mycpuid + 1,
734                     rmsg->base.lmsg.ms_error);
735         }
736 }
737
738 int
739 rtrequest1(int req, struct rt_addrinfo *rtinfo, struct rtentry **ret_nrt)
740 {
741         struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
742         struct rtentry *rt;
743         struct radix_node *rn;
744         struct radix_node_head *rnh;
745         struct ifaddr *ifa;
746         struct sockaddr *ndst;
747         boolean_t reportmsg;
748         int error = 0;
749
750         ASSERT_NETISR_NCPUS(mycpuid);
751
752 #define gotoerr(x) { error = x ; goto bad; }
753
754 #ifdef ROUTE_DEBUG
755         if (route_debug)
756                 rt_addrinfo_print(req, rtinfo);
757 #endif
758
759         crit_enter();
760         /*
761          * Find the correct routing tree to use for this Address Family
762          */
763         if ((rnh = rt_tables[mycpuid][dst->sa_family]) == NULL)
764                 gotoerr(EAFNOSUPPORT);
765
766         /*
767          * If we are adding a host route then we don't want to put
768          * a netmask in the tree, nor do we want to clone it.
769          */
770         if (rtinfo->rti_flags & RTF_HOST) {
771                 rtinfo->rti_info[RTAX_NETMASK] = NULL;
772                 rtinfo->rti_flags &= ~(RTF_CLONING | RTF_PRCLONING);
773         }
774
775         switch (req) {
776         case RTM_DELETE:
777                 /* Remove the item from the tree. */
778                 rn = rnh->rnh_deladdr((char *)rtinfo->rti_info[RTAX_DST],
779                                       (char *)rtinfo->rti_info[RTAX_NETMASK],
780                                       rnh);
781                 if (rn == NULL)
782                         gotoerr(ESRCH);
783                 KASSERT(!(rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)),
784                         ("rnh_deladdr returned flags 0x%x", rn->rn_flags));
785                 rt = (struct rtentry *)rn;
786
787                 /* ref to prevent a deletion race */
788                 ++rt->rt_refcnt;
789
790                 /* Free any routes cloned from this one. */
791                 if ((rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) &&
792                     rt_mask(rt) != NULL) {
793                         rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
794                                                (char *)rt_mask(rt),
795                                                rt_fixdelete, rt);
796                 }
797
798                 if (rt->rt_gwroute != NULL) {
799                         RTFREE(rt->rt_gwroute);
800                         rt->rt_gwroute = NULL;
801                 }
802
803                 /*
804                  * NB: RTF_UP must be set during the search above,
805                  * because we might delete the last ref, causing
806                  * rt to get freed prematurely.
807                  */
808                 rt->rt_flags &= ~RTF_UP;
809
810 #ifdef ROUTE_DEBUG
811                 if (route_debug)
812                         rt_print(rtinfo, rt);
813 #endif
814
815                 /* Give the protocol a chance to keep things in sync. */
816                 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
817                         ifa->ifa_rtrequest(RTM_DELETE, rt);
818
819                 /*
820                  * If the caller wants it, then it can have it,
821                  * but it's up to it to free the rtentry as we won't be
822                  * doing it.
823                  */
824                 KASSERT(rt->rt_refcnt >= 0,
825                         ("rtrequest1(DELETE): refcnt %ld", rt->rt_refcnt));
826                 if (ret_nrt != NULL) {
827                         /* leave ref intact for return */
828                         *ret_nrt = rt;
829                 } else {
830                         /* deref / attempt to destroy */
831                         rtfree(rt);
832                 }
833                 break;
834
835         case RTM_RESOLVE:
836                 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
837                         gotoerr(EINVAL);
838
839                 KASSERT(rt->rt_cpuid == mycpuid,
840                     ("rt resolve rt_cpuid %d, mycpuid %d",
841                      rt->rt_cpuid, mycpuid));
842
843                 ifa = rt->rt_ifa;
844                 rtinfo->rti_flags =
845                     rt->rt_flags & ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC);
846                 rtinfo->rti_flags |= RTF_WASCLONED;
847                 rtinfo->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
848                 if ((rtinfo->rti_info[RTAX_NETMASK] = rt->rt_genmask) == NULL)
849                         rtinfo->rti_flags |= RTF_HOST;
850                 rtinfo->rti_info[RTAX_MPLS1] = rt->rt_shim[0];
851                 rtinfo->rti_info[RTAX_MPLS2] = rt->rt_shim[1];
852                 rtinfo->rti_info[RTAX_MPLS3] = rt->rt_shim[2];
853                 goto makeroute;
854
855         case RTM_ADD:
856                 KASSERT(!(rtinfo->rti_flags & RTF_GATEWAY) ||
857                         rtinfo->rti_info[RTAX_GATEWAY] != NULL,
858                     ("rtrequest: GATEWAY but no gateway"));
859
860                 if (rtinfo->rti_ifa == NULL && (error = rt_getifa(rtinfo)))
861                         gotoerr(error);
862                 ifa = rtinfo->rti_ifa;
863 makeroute:
864                 R_Malloc(rt, struct rtentry *, sizeof(struct rtentry));
865                 if (rt == NULL) {
866                         if (req == RTM_ADD) {
867                                 kprintf("rtrequest1: alloc rtentry failed on "
868                                     "cpu%d\n", mycpuid);
869                         }
870                         gotoerr(ENOBUFS);
871                 }
872                 bzero(rt, sizeof(struct rtentry));
873                 rt->rt_flags = RTF_UP | rtinfo->rti_flags;
874                 rt->rt_cpuid = mycpuid;
875
876                 if (mycpuid != 0 && req == RTM_ADD) {
877                         /* For RTM_ADD, we have already sent rtmsg on CPU0. */
878                         reportmsg = RTL_DONTREPORT;
879                 } else {
880                         /*
881                          * For RTM_ADD, we only send rtmsg on CPU0.
882                          * For RTM_RESOLVE, we always send rtmsg. XXX
883                          */
884                         reportmsg = RTL_REPORTMSG;
885                 }
886                 error = rt_setgate(rt, dst, rtinfo->rti_info[RTAX_GATEWAY],
887                                    reportmsg);
888                 if (error != 0) {
889                         Free(rt);
890                         gotoerr(error);
891                 }
892
893                 ndst = rt_key(rt);
894                 if (rtinfo->rti_info[RTAX_NETMASK] != NULL)
895                         rt_maskedcopy(dst, ndst,
896                                       rtinfo->rti_info[RTAX_NETMASK]);
897                 else
898                         bcopy(dst, ndst, dst->sa_len);
899
900                 if (rtinfo->rti_info[RTAX_MPLS1] != NULL)
901                         rt_setshims(rt, rtinfo->rti_info);
902
903                 /*
904                  * Note that we now have a reference to the ifa.
905                  * This moved from below so that rnh->rnh_addaddr() can
906                  * examine the ifa and  ifa->ifa_ifp if it so desires.
907                  */
908                 IFAREF(ifa);
909                 rt->rt_ifa = ifa;
910                 rt->rt_ifp = ifa->ifa_ifp;
911                 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
912
913                 rn = rnh->rnh_addaddr((char *)ndst,
914                                       (char *)rtinfo->rti_info[RTAX_NETMASK],
915                                       rnh, rt->rt_nodes);
916                 if (rn == NULL) {
917                         struct rtentry *oldrt;
918
919                         /*
920                          * We already have one of these in the tree.
921                          * We do a special hack: if the old route was
922                          * cloned, then we blow it away and try
923                          * re-inserting the new one.
924                          */
925                         oldrt = rtpurelookup(ndst);
926                         if (oldrt != NULL) {
927                                 --oldrt->rt_refcnt;
928                                 if (oldrt->rt_flags & RTF_WASCLONED) {
929                                         rtrequest(RTM_DELETE, rt_key(oldrt),
930                                                   oldrt->rt_gateway,
931                                                   rt_mask(oldrt),
932                                                   oldrt->rt_flags, NULL);
933                                         rn = rnh->rnh_addaddr((char *)ndst,
934                                             (char *)
935                                                 rtinfo->rti_info[RTAX_NETMASK],
936                                             rnh, rt->rt_nodes);
937                                 }
938                         }
939                 }
940                 /* NOTE: rt_ifa may have been changed */
941                 ifa = rt->rt_ifa;
942
943                 /*
944                  * If it still failed to go into the tree,
945                  * then un-make it (this should be a function).
946                  */
947                 if (rn == NULL) {
948                         if (rt->rt_gwroute != NULL)
949                                 rtfree(rt->rt_gwroute);
950                         IFAFREE(ifa);
951                         Free(rt_key(rt));
952                         Free(rt);
953                         gotoerr(EEXIST);
954                 }
955
956                 /*
957                  * If we got here from RESOLVE, then we are cloning
958                  * so clone the rest, and note that we
959                  * are a clone (and increment the parent's references)
960                  */
961                 if (req == RTM_RESOLVE) {
962                         rt->rt_rmx = (*ret_nrt)->rt_rmx;    /* copy metrics */
963                         rt->rt_rmx.rmx_pksent = 0;  /* reset packet counter */
964                         if ((*ret_nrt)->rt_flags &
965                                        (RTF_CLONING | RTF_PRCLONING)) {
966                                 rt->rt_parent = *ret_nrt;
967                                 (*ret_nrt)->rt_refcnt++;
968                         }
969                 }
970
971                 /*
972                  * if this protocol has something to add to this then
973                  * allow it to do that as well.
974                  */
975                 if (ifa->ifa_rtrequest != NULL)
976                         ifa->ifa_rtrequest(req, rt);
977
978                 /*
979                  * We repeat the same procedure from rt_setgate() here because
980                  * it doesn't fire when we call it there because the node
981                  * hasn't been added to the tree yet.
982                  */
983                 if (req == RTM_ADD && !(rt->rt_flags & RTF_HOST) &&
984                     rt_mask(rt) != NULL) {
985                         struct rtfc_arg arg = { rt, rnh };
986
987                         rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
988                                                (char *)rt_mask(rt),
989                                                rt_fixchange, &arg);
990                 }
991
992 #ifdef ROUTE_DEBUG
993                 if (route_debug)
994                         rt_print(rtinfo, rt);
995 #endif
996                 /*
997                  * Return the resulting rtentry,
998                  * increasing the number of references by one.
999                  */
1000                 if (ret_nrt != NULL) {
1001                         rt->rt_refcnt++;
1002                         *ret_nrt = rt;
1003                 }
1004                 break;
1005         default:
1006                 error = EOPNOTSUPP;
1007         }
1008 bad:
1009 #ifdef ROUTE_DEBUG
1010         if (route_debug) {
1011                 if (error)
1012                         kprintf("rti %p failed error %d\n", rtinfo, error);
1013                 else
1014                         kprintf("rti %p succeeded\n", rtinfo);
1015         }
1016 #endif
1017         crit_exit();
1018         return (error);
1019 }
1020
1021 /*
1022  * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
1023  * (i.e., the routes related to it by the operation of cloning).  This
1024  * routine is iterated over all potential former-child-routes by way of
1025  * rnh->rnh_walktree_from() above, and those that actually are children of
1026  * the late parent (passed in as VP here) are themselves deleted.
1027  */
1028 static int
1029 rt_fixdelete(struct radix_node *rn, void *vp)
1030 {
1031         struct rtentry *rt = (struct rtentry *)rn;
1032         struct rtentry *rt0 = vp;
1033
1034         if (rt->rt_parent == rt0 &&
1035             !(rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) {
1036                 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1037                                  rt->rt_flags, NULL);
1038         }
1039         return 0;
1040 }
1041
1042 /*
1043  * This routine is called from rt_setgate() to do the analogous thing for
1044  * adds and changes.  There is the added complication in this case of a
1045  * middle insert; i.e., insertion of a new network route between an older
1046  * network route and (cloned) host routes.  For this reason, a simple check
1047  * of rt->rt_parent is insufficient; each candidate route must be tested
1048  * against the (mask, value) of the new route (passed as before in vp)
1049  * to see if the new route matches it.
1050  *
1051  * XXX - it may be possible to do fixdelete() for changes and reserve this
1052  * routine just for adds.  I'm not sure why I thought it was necessary to do
1053  * changes this way.
1054  */
1055 #ifdef DEBUG
1056 static int rtfcdebug = 0;
1057 #endif
1058
1059 static int
1060 rt_fixchange(struct radix_node *rn, void *vp)
1061 {
1062         struct rtentry *rt = (struct rtentry *)rn;
1063         struct rtfc_arg *ap = vp;
1064         struct rtentry *rt0 = ap->rt0;
1065         struct radix_node_head *rnh = ap->rnh;
1066         u_char *xk1, *xm1, *xk2, *xmp;
1067         int i, len, mlen;
1068
1069 #ifdef DEBUG
1070         if (rtfcdebug)
1071                 kprintf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0);
1072 #endif
1073
1074         if (rt->rt_parent == NULL ||
1075             (rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) {
1076 #ifdef DEBUG
1077                 if (rtfcdebug) kprintf("no parent, pinned or cloning\n");
1078 #endif
1079                 return 0;
1080         }
1081
1082         if (rt->rt_parent == rt0) {
1083 #ifdef DEBUG
1084                 if (rtfcdebug) kprintf("parent match\n");
1085 #endif
1086                 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1087                                  rt->rt_flags, NULL);
1088         }
1089
1090         /*
1091          * There probably is a function somewhere which does this...
1092          * if not, there should be.
1093          */
1094         len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len);
1095
1096         xk1 = (u_char *)rt_key(rt0);
1097         xm1 = (u_char *)rt_mask(rt0);
1098         xk2 = (u_char *)rt_key(rt);
1099
1100         /* avoid applying a less specific route */
1101         xmp = (u_char *)rt_mask(rt->rt_parent);
1102         mlen = rt_key(rt->rt_parent)->sa_len;
1103         if (mlen > rt_key(rt0)->sa_len) {
1104 #ifdef DEBUG
1105                 if (rtfcdebug)
1106                         kprintf("rt_fixchange: inserting a less "
1107                                "specific route\n");
1108 #endif
1109                 return 0;
1110         }
1111         for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++) {
1112                 if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i]) {
1113 #ifdef DEBUG
1114                         if (rtfcdebug)
1115                                 kprintf("rt_fixchange: inserting a less "
1116                                        "specific route\n");
1117 #endif
1118                         return 0;
1119                 }
1120         }
1121
1122         for (i = rnh->rnh_treetop->rn_offset; i < len; i++) {
1123                 if ((xk2[i] & xm1[i]) != xk1[i]) {
1124 #ifdef DEBUG
1125                         if (rtfcdebug) kprintf("no match\n");
1126 #endif
1127                         return 0;
1128                 }
1129         }
1130
1131         /*
1132          * OK, this node is a clone, and matches the node currently being
1133          * changed/added under the node's mask.  So, get rid of it.
1134          */
1135 #ifdef DEBUG
1136         if (rtfcdebug) kprintf("deleting\n");
1137 #endif
1138         return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1139                          rt->rt_flags, NULL);
1140 }
1141
1142 int
1143 rt_setgate(struct rtentry *rt0, struct sockaddr *dst, struct sockaddr *gate,
1144            boolean_t generate_report)
1145 {
1146         char *space, *oldspace;
1147         int dlen = RT_ROUNDUP(dst->sa_len), glen = RT_ROUNDUP(gate->sa_len);
1148         struct rtentry *rt = rt0;
1149         struct radix_node_head *rnh = rt_tables[mycpuid][dst->sa_family];
1150
1151         ASSERT_NETISR_NCPUS(mycpuid);
1152
1153         /*
1154          * A host route with the destination equal to the gateway
1155          * will interfere with keeping LLINFO in the routing
1156          * table, so disallow it.
1157          */
1158         if (((rt0->rt_flags & (RTF_HOST | RTF_GATEWAY | RTF_LLINFO)) ==
1159                               (RTF_HOST | RTF_GATEWAY)) &&
1160             dst->sa_len == gate->sa_len &&
1161             sa_equal(dst, gate)) {
1162                 /*
1163                  * The route might already exist if this is an RTM_CHANGE
1164                  * or a routing redirect, so try to delete it.
1165                  */
1166                 if (rt_key(rt0) != NULL)
1167                         rtrequest(RTM_DELETE, rt_key(rt0), rt0->rt_gateway,
1168                                   rt_mask(rt0), rt0->rt_flags, NULL);
1169                 return EADDRNOTAVAIL;
1170         }
1171
1172         /*
1173          * Both dst and gateway are stored in the same malloc'ed chunk
1174          * (If I ever get my hands on....)
1175          * if we need to malloc a new chunk, then keep the old one around
1176          * till we don't need it any more.
1177          */
1178         if (rt->rt_gateway == NULL ||
1179             glen > RT_ROUNDUP(rt->rt_gateway->sa_len)) {
1180                 oldspace = (char *)rt_key(rt);
1181                 R_Malloc(space, char *, dlen + glen);
1182                 if (space == NULL)
1183                         return ENOBUFS;
1184                 rt->rt_nodes->rn_key = space;
1185         } else {
1186                 space = (char *)rt_key(rt);     /* Just use the old space. */
1187                 oldspace = NULL;
1188         }
1189
1190         /* Set the gateway value. */
1191         rt->rt_gateway = (struct sockaddr *)(space + dlen);
1192         bcopy(gate, rt->rt_gateway, glen);
1193
1194         if (oldspace != NULL) {
1195                 /*
1196                  * If we allocated a new chunk, preserve the original dst.
1197                  * This way, rt_setgate() really just sets the gate
1198                  * and leaves the dst field alone.
1199                  */
1200                 bcopy(dst, space, dlen);
1201                 Free(oldspace);
1202         }
1203
1204         /*
1205          * If there is already a gwroute, it's now almost definitely wrong
1206          * so drop it.
1207          */
1208         if (rt->rt_gwroute != NULL) {
1209                 RTFREE(rt->rt_gwroute);
1210                 rt->rt_gwroute = NULL;
1211         }
1212         if (rt->rt_flags & RTF_GATEWAY) {
1213                 /*
1214                  * Cloning loop avoidance: In the presence of
1215                  * protocol-cloning and bad configuration, it is
1216                  * possible to get stuck in bottomless mutual recursion
1217                  * (rtrequest rt_setgate rtlookup).  We avoid this
1218                  * by not allowing protocol-cloning to operate for
1219                  * gateways (which is probably the correct choice
1220                  * anyway), and avoid the resulting reference loops
1221                  * by disallowing any route to run through itself as
1222                  * a gateway.  This is obviously mandatory when we
1223                  * get rt->rt_output().
1224                  *
1225                  * This breaks TTCP for hosts outside the gateway!  XXX JH
1226                  */
1227                 rt->rt_gwroute = _rtlookup(gate, generate_report,
1228                                            RTF_PRCLONING);
1229                 if (rt->rt_gwroute == rt) {
1230                         rt->rt_gwroute = NULL;
1231                         --rt->rt_refcnt;
1232                         return EDQUOT; /* failure */
1233                 }
1234         }
1235
1236         /*
1237          * This isn't going to do anything useful for host routes, so
1238          * don't bother.  Also make sure we have a reasonable mask
1239          * (we don't yet have one during adds).
1240          */
1241         if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) {
1242                 struct rtfc_arg arg = { rt, rnh };
1243
1244                 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
1245                                        (char *)rt_mask(rt),
1246                                        rt_fixchange, &arg);
1247         }
1248
1249         return 0;
1250 }
1251
1252 static void
1253 rt_maskedcopy(
1254         struct sockaddr *src,
1255         struct sockaddr *dst,
1256         struct sockaddr *netmask)
1257 {
1258         u_char *cp1 = (u_char *)src;
1259         u_char *cp2 = (u_char *)dst;
1260         u_char *cp3 = (u_char *)netmask;
1261         u_char *cplim = cp2 + *cp3;
1262         u_char *cplim2 = cp2 + *cp1;
1263
1264         *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1265         cp3 += 2;
1266         if (cplim > cplim2)
1267                 cplim = cplim2;
1268         while (cp2 < cplim)
1269                 *cp2++ = *cp1++ & *cp3++;
1270         if (cp2 < cplim2)
1271                 bzero(cp2, cplim2 - cp2);
1272 }
1273
1274 int
1275 rt_llroute(struct sockaddr *dst, struct rtentry *rt0, struct rtentry **drt)
1276 {
1277         struct rtentry *up_rt, *rt;
1278
1279         ASSERT_NETISR_NCPUS(mycpuid);
1280
1281         if (!(rt0->rt_flags & RTF_UP)) {
1282                 up_rt = rtlookup(dst);
1283                 if (up_rt == NULL)
1284                         return (EHOSTUNREACH);
1285                 up_rt->rt_refcnt--;
1286         } else
1287                 up_rt = rt0;
1288         if (up_rt->rt_flags & RTF_GATEWAY) {
1289                 if (up_rt->rt_gwroute == NULL) {
1290                         up_rt->rt_gwroute = rtlookup(up_rt->rt_gateway);
1291                         if (up_rt->rt_gwroute == NULL)
1292                                 return (EHOSTUNREACH);
1293                 } else if (!(up_rt->rt_gwroute->rt_flags & RTF_UP)) {
1294                         rtfree(up_rt->rt_gwroute);
1295                         up_rt->rt_gwroute = rtlookup(up_rt->rt_gateway);
1296                         if (up_rt->rt_gwroute == NULL)
1297                                 return (EHOSTUNREACH);
1298                 }
1299                 rt = up_rt->rt_gwroute;
1300         } else
1301                 rt = up_rt;
1302         if (rt->rt_flags & RTF_REJECT &&
1303             (rt->rt_rmx.rmx_expire == 0 ||              /* rt doesn't expire */
1304              time_uptime < rt->rt_rmx.rmx_expire))      /* rt not expired */
1305                 return (rt->rt_flags & RTF_HOST ?  EHOSTDOWN : EHOSTUNREACH);
1306         *drt = rt;
1307         return 0;
1308 }
1309
1310 static int
1311 rt_setshims(struct rtentry *rt, struct sockaddr **rt_shim){
1312         int i;
1313         
1314         for (i=0; i<3; i++) {
1315                 struct sockaddr *shim = rt_shim[RTAX_MPLS1 + i];
1316                 int shimlen;
1317
1318                 if (shim == NULL)
1319                         break;
1320
1321                 shimlen = RT_ROUNDUP(shim->sa_len);
1322                 R_Malloc(rt->rt_shim[i], struct sockaddr *, shimlen);
1323                 bcopy(shim, rt->rt_shim[i], shimlen);
1324         }
1325
1326         return 0;
1327 }
1328
1329 #ifdef ROUTE_DEBUG
1330
1331 /*
1332  * Print out a route table entry
1333  */
1334 void
1335 rt_print(struct rt_addrinfo *rtinfo, struct rtentry *rn)
1336 {
1337         kprintf("rti %p cpu %d route %p flags %08lx: ", 
1338                 rtinfo, mycpuid, rn, rn->rt_flags);
1339         sockaddr_print(rt_key(rn));
1340         kprintf(" mask ");
1341         sockaddr_print(rt_mask(rn));
1342         kprintf(" gw ");
1343         sockaddr_print(rn->rt_gateway);
1344         kprintf(" ifc \"%s\"", rn->rt_ifp ? rn->rt_ifp->if_dname : "?");
1345         kprintf(" ifa %p\n", rn->rt_ifa);
1346 }
1347
1348 void
1349 rt_addrinfo_print(int cmd, struct rt_addrinfo *rti)
1350 {
1351         int didit = 0;
1352         int i;
1353
1354 #ifdef ROUTE_DEBUG
1355         if (cmd == RTM_DELETE && route_debug > 1)
1356                 print_backtrace(-1);
1357 #endif
1358
1359         switch(cmd) {
1360         case RTM_ADD:
1361                 kprintf("ADD ");
1362                 break;
1363         case RTM_RESOLVE:
1364                 kprintf("RES ");
1365                 break;
1366         case RTM_DELETE:
1367                 kprintf("DEL ");
1368                 break;
1369         default:
1370                 kprintf("C%02d ", cmd);
1371                 break;
1372         }
1373         kprintf("rti %p cpu %d ", rti, mycpuid);
1374         for (i = 0; i < rti->rti_addrs; ++i) {
1375                 if (rti->rti_info[i] == NULL)
1376                         continue;
1377                 if (didit)
1378                         kprintf(" ,");
1379                 switch(i) {
1380                 case RTAX_DST:
1381                         kprintf("(DST ");
1382                         break;
1383                 case RTAX_GATEWAY:
1384                         kprintf("(GWY ");
1385                         break;
1386                 case RTAX_NETMASK:
1387                         kprintf("(MSK ");
1388                         break;
1389                 case RTAX_GENMASK:
1390                         kprintf("(GEN ");
1391                         break;
1392                 case RTAX_IFP:
1393                         kprintf("(IFP ");
1394                         break;
1395                 case RTAX_IFA:
1396                         kprintf("(IFA ");
1397                         break;
1398                 case RTAX_AUTHOR:
1399                         kprintf("(AUT ");
1400                         break;
1401                 case RTAX_BRD:
1402                         kprintf("(BRD ");
1403                         break;
1404                 default:
1405                         kprintf("(?%02d ", i);
1406                         break;
1407                 }
1408                 sockaddr_print(rti->rti_info[i]);
1409                 kprintf(")");
1410                 didit = 1;
1411         }
1412         kprintf("\n");
1413 }
1414
1415 void
1416 sockaddr_print(struct sockaddr *sa)
1417 {
1418         struct sockaddr_in *sa4;
1419         struct sockaddr_in6 *sa6;
1420         int len;
1421         int i;
1422
1423         if (sa == NULL) {
1424                 kprintf("NULL");
1425                 return;
1426         }
1427
1428         len = sa->sa_len - offsetof(struct sockaddr, sa_data[0]);
1429
1430         switch(sa->sa_family) {
1431         case AF_INET:
1432         case AF_INET6:
1433         default:
1434                 switch(sa->sa_family) {
1435                 case AF_INET:
1436                         sa4 = (struct sockaddr_in *)sa;
1437                         kprintf("INET %d %d.%d.%d.%d",
1438                                 ntohs(sa4->sin_port),
1439                                 (ntohl(sa4->sin_addr.s_addr) >> 24) & 255,
1440                                 (ntohl(sa4->sin_addr.s_addr) >> 16) & 255,
1441                                 (ntohl(sa4->sin_addr.s_addr) >> 8) & 255,
1442                                 (ntohl(sa4->sin_addr.s_addr) >> 0) & 255
1443                         );
1444                         break;
1445                 case AF_INET6:
1446                         sa6 = (struct sockaddr_in6 *)sa;
1447                         kprintf("INET6 %d %04x:%04x%04x:%04x:%04x:%04x:%04x:%04x",
1448                                 ntohs(sa6->sin6_port),
1449                                 sa6->sin6_addr.s6_addr16[0],
1450                                 sa6->sin6_addr.s6_addr16[1],
1451                                 sa6->sin6_addr.s6_addr16[2],
1452                                 sa6->sin6_addr.s6_addr16[3],
1453                                 sa6->sin6_addr.s6_addr16[4],
1454                                 sa6->sin6_addr.s6_addr16[5],
1455                                 sa6->sin6_addr.s6_addr16[6],
1456                                 sa6->sin6_addr.s6_addr16[7]
1457                         );
1458                         break;
1459                 default:
1460                         kprintf("AF%d ", sa->sa_family);
1461                         while (len > 0 && sa->sa_data[len-1] == 0)
1462                                 --len;
1463
1464                         for (i = 0; i < len; ++i) {
1465                                 if (i)
1466                                         kprintf(".");
1467                                 kprintf("%d", (unsigned char)sa->sa_data[i]);
1468                         }
1469                         break;
1470                 }
1471         }
1472 }
1473
1474 #endif
1475
1476 /*
1477  * Set up a routing table entry, normally for an interface.
1478  */
1479 int
1480 rtinit(struct ifaddr *ifa, int cmd, int flags)
1481 {
1482         struct sockaddr *dst, *deldst, *netmask;
1483         struct mbuf *m = NULL;
1484         struct radix_node_head *rnh;
1485         struct radix_node *rn;
1486         struct rt_addrinfo rtinfo;
1487         int error;
1488
1489         ASSERT_NETISR0;
1490
1491         if (flags & RTF_HOST) {
1492                 dst = ifa->ifa_dstaddr;
1493                 netmask = NULL;
1494         } else {
1495                 dst = ifa->ifa_addr;
1496                 netmask = ifa->ifa_netmask;
1497         }
1498         /*
1499          * If it's a delete, check that if it exists, it's on the correct
1500          * interface or we might scrub a route to another ifa which would
1501          * be confusing at best and possibly worse.
1502          */
1503         if (cmd == RTM_DELETE) {
1504                 /*
1505                  * It's a delete, so it should already exist..
1506                  * If it's a net, mask off the host bits
1507                  * (Assuming we have a mask)
1508                  */
1509                 if (netmask != NULL) {
1510                         m = m_get(M_NOWAIT, MT_SONAME);
1511                         if (m == NULL)
1512                                 return (ENOBUFS);
1513                         mbuftrackid(m, 34);
1514                         deldst = mtod(m, struct sockaddr *);
1515                         rt_maskedcopy(dst, deldst, netmask);
1516                         dst = deldst;
1517                 }
1518                 /*
1519                  * Look up an rtentry that is in the routing tree and
1520                  * contains the correct info.
1521                  */
1522                 if ((rnh = rt_tables[mycpuid][dst->sa_family]) == NULL ||
1523                     (rn = rnh->rnh_lookup((char *)dst,
1524                                           (char *)netmask, rnh)) == NULL ||
1525                     ((struct rtentry *)rn)->rt_ifa != ifa ||
1526                     !sa_equal((struct sockaddr *)rn->rn_key, dst)) {
1527                         if (m != NULL)
1528                                 m_free(m);
1529                         return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1530                 }
1531                 /* XXX */
1532 #if 0
1533                 else {
1534                         /*
1535                          * One would think that as we are deleting, and we know
1536                          * it doesn't exist, we could just return at this point
1537                          * with an "ELSE" clause, but apparently not..
1538                          */
1539                         return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1540                 }
1541 #endif
1542         }
1543         /*
1544          * Do the actual request
1545          */
1546         bzero(&rtinfo, sizeof(struct rt_addrinfo));
1547         rtinfo.rti_info[RTAX_DST] = dst;
1548         rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1549         rtinfo.rti_info[RTAX_NETMASK] = netmask;
1550         rtinfo.rti_flags = flags | ifa->ifa_flags;
1551         rtinfo.rti_ifa = ifa;
1552         error = rtrequest1_global(cmd, &rtinfo, rtinit_rtrequest_callback, ifa,
1553             RTREQ_PRIO_HIGH);
1554         if (m != NULL)
1555                 m_free(m);
1556         return (error);
1557 }
1558
1559 static void
1560 rtinit_rtrequest_callback(int cmd, int error,
1561                           struct rt_addrinfo *rtinfo, struct rtentry *rt,
1562                           void *arg)
1563 {
1564         struct ifaddr *ifa = arg;
1565
1566         if (error == 0 && rt) {
1567                 if (mycpuid == 0) {
1568                         ++rt->rt_refcnt;
1569                         rt_newaddrmsg(cmd, ifa, error, rt);
1570                         --rt->rt_refcnt;
1571                 }
1572                 if (cmd == RTM_DELETE) {
1573                         if (rt->rt_refcnt == 0) {
1574                                 ++rt->rt_refcnt;
1575                                 rtfree(rt);
1576                         }
1577                 }
1578         }
1579 }
1580
1581 struct netmsg_rts {
1582         struct netmsg_base      base;
1583         int                     req;
1584         struct rt_addrinfo      *rtinfo;
1585         rtsearch_callback_func_t callback;
1586         void                    *arg;
1587         boolean_t               exact_match;
1588         int                     found_cnt;
1589 };
1590
1591 int
1592 rtsearch_global(int req, struct rt_addrinfo *rtinfo,
1593     rtsearch_callback_func_t callback, void *arg, boolean_t exact_match,
1594     boolean_t req_prio)
1595 {
1596         struct netmsg_rts msg;
1597         int flags = 0;
1598
1599         if (req_prio)
1600                 flags = MSGF_PRIORITY;
1601         netmsg_init(&msg.base, NULL, &curthread->td_msgport, flags,
1602             rtsearch_msghandler);
1603         msg.req = req;
1604         msg.rtinfo = rtinfo;
1605         msg.callback = callback;
1606         msg.arg = arg;
1607         msg.exact_match = exact_match;
1608         msg.found_cnt = 0;
1609         return (netisr_domsg_global(&msg.base));
1610 }
1611
1612 static void
1613 rtsearch_msghandler(netmsg_t msg)
1614 {
1615         struct netmsg_rts *rmsg = (void *)msg;
1616         struct rt_addrinfo rtinfo;
1617         struct radix_node_head *rnh;
1618         struct rtentry *rt;
1619         int error;
1620
1621         ASSERT_NETISR_NCPUS(mycpuid);
1622
1623         /*
1624          * Copy the rtinfo.  We need to make sure that the original
1625          * rtinfo, which is setup by the caller, in the netmsg will
1626          * _not_ be changed; else the next CPU on the netmsg forwarding
1627          * path will see a different rtinfo than what this CPU has seen.
1628          */
1629         rtinfo = *rmsg->rtinfo;
1630
1631         /*
1632          * Find the correct routing tree to use for this Address Family
1633          */
1634         if ((rnh = rt_tables[mycpuid][rtinfo.rti_dst->sa_family]) == NULL) {
1635                 if (mycpuid != 0)
1636                         panic("partially initialized routing tables");
1637                 netisr_replymsg(&rmsg->base, EAFNOSUPPORT);
1638                 return;
1639         }
1640
1641         /*
1642          * Correct rtinfo for the host route searching.
1643          */
1644         if (rtinfo.rti_flags & RTF_HOST) {
1645                 rtinfo.rti_netmask = NULL;
1646                 rtinfo.rti_flags &= ~(RTF_CLONING | RTF_PRCLONING);
1647         }
1648
1649         rt = (struct rtentry *)
1650              rnh->rnh_lookup((char *)rtinfo.rti_dst,
1651                              (char *)rtinfo.rti_netmask, rnh);
1652
1653         /*
1654          * If we are asked to do the "exact match", we need to make sure
1655          * that host route searching got a host route while a network
1656          * route searching got a network route.
1657          */
1658         if (rt != NULL && rmsg->exact_match &&
1659             ((rt->rt_flags ^ rtinfo.rti_flags) & RTF_HOST))
1660                 rt = NULL;
1661
1662         if (rt == NULL) {
1663                 /*
1664                  * No matching routes have been found, don't count this
1665                  * as a critical error (here, we set 'error' to 0), just
1666                  * keep moving on, since at least prcloned routes are not
1667                  * duplicated onto each CPU.
1668                  */
1669                 error = 0;
1670         } else {
1671                 rmsg->found_cnt++;
1672
1673                 rt->rt_refcnt++;
1674                 error = rmsg->callback(rmsg->req, &rtinfo, rt, rmsg->arg,
1675                                       rmsg->found_cnt);
1676                 rt->rt_refcnt--;
1677
1678                 if (error == EJUSTRETURN) {
1679                         netisr_replymsg(&rmsg->base, 0);
1680                         return;
1681                 }
1682         }
1683
1684         if (error) {
1685                 KKASSERT(rmsg->found_cnt > 0);
1686
1687                 /*
1688                  * Under following cases, unrecoverable error has
1689                  * not occured:
1690                  * o  Request is RTM_GET
1691                  * o  The first time that we find the route, but the
1692                  *    modification fails.
1693                  */
1694                 if (rmsg->req != RTM_GET && rmsg->found_cnt > 1) {
1695                         panic("rtsearch_msghandler: unrecoverable error "
1696                               "cpu %d", mycpuid);
1697                 }
1698                 netisr_replymsg(&rmsg->base, error);
1699         } else {
1700                 if (rmsg->found_cnt == 0) {
1701                         /* The requested route has not been seen ... */
1702                         error = ESRCH;
1703                 }
1704                 netisr_forwardmsg_error(&rmsg->base, mycpuid + 1, error);
1705         }
1706 }
1707
1708 int
1709 rtmask_add_global(struct sockaddr *mask, boolean_t req_prio)
1710 {
1711         struct netmsg_base msg;
1712         int flags = 0;
1713
1714         if (req_prio)
1715                 flags = MSGF_PRIORITY;
1716         netmsg_init(&msg, NULL, &curthread->td_msgport, flags,
1717             rtmask_add_msghandler);
1718         msg.lmsg.u.ms_resultp = mask;
1719
1720         return (netisr_domsg_global(&msg));
1721 }
1722
1723 struct sockaddr *
1724 _rtmask_lookup(struct sockaddr *mask, boolean_t search)
1725 {
1726         struct radix_node *n;
1727
1728 #define clen(s) (*(u_char *)(s))
1729         n = rn_addmask((char *)mask, search, 1, rn_cpumaskhead(mycpuid));
1730         if (n != NULL &&
1731             mask->sa_len >= clen(n->rn_key) &&
1732             bcmp((char *)mask + 1,
1733                  (char *)n->rn_key + 1, clen(n->rn_key) - 1) == 0) {
1734                 return (struct sockaddr *)n->rn_key;
1735         } else {
1736                 return NULL;
1737         }
1738 #undef clen
1739 }
1740
1741 static void
1742 rtmask_add_msghandler(netmsg_t msg)
1743 {
1744         struct sockaddr *mask = msg->lmsg.u.ms_resultp;
1745
1746         ASSERT_NETISR_NCPUS(mycpuid);
1747
1748         if (rtmask_lookup(mask) == NULL) {
1749                 netisr_replymsg(&msg->base, ENOBUFS);
1750                 return;
1751         }
1752         netisr_forwardmsg(&msg->base, mycpuid + 1);
1753 }
1754
1755 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1756 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);
1757
1758 struct rtchange_arg {
1759         struct ifaddr   *old_ifa;
1760         struct ifaddr   *new_ifa;
1761         struct rtentry  *rt;
1762         int             changed;
1763 };
1764
1765 static void
1766 rtchange_ifa(struct rtentry *rt, struct rtchange_arg *ap)
1767 {
1768         if (rt->rt_ifa->ifa_rtrequest != NULL)
1769                 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt);
1770         IFAFREE(rt->rt_ifa);
1771
1772         IFAREF(ap->new_ifa);
1773         rt->rt_ifa = ap->new_ifa;
1774         rt->rt_ifp = ap->new_ifa->ifa_ifp;
1775         if (rt->rt_ifa->ifa_rtrequest != NULL)
1776                 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt);
1777
1778         ap->changed = 1;
1779 }
1780
1781 static int
1782 rtchange_callback(struct radix_node *rn, void *xap)
1783 {
1784         struct rtchange_arg *ap = xap;
1785         struct rtentry *rt = (struct rtentry *)rn;
1786
1787         if (rt->rt_ifa == ap->old_ifa) {
1788                 if (rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) {
1789                         /*
1790                          * We could saw the branch off when we are
1791                          * still sitting on it, if the ifa_rtrequest
1792                          * DEL/ADD are called directly from here.
1793                          */
1794                         ap->rt = rt;
1795                         return EJUSTRETURN;
1796                 }
1797                 rtchange_ifa(rt, ap);
1798         }
1799         return 0;
1800 }
1801
1802 struct netmsg_rtchange {
1803         struct netmsg_base      base;
1804         struct ifaddr           *old_ifa;
1805         struct ifaddr           *new_ifa;
1806         int                     changed;
1807 };
1808
1809 static void
1810 rtchange_dispatch(netmsg_t msg)
1811 {
1812         struct netmsg_rtchange *rmsg = (void *)msg;
1813         struct radix_node_head *rnh;
1814         struct rtchange_arg arg;
1815         int cpu;
1816
1817         cpu = mycpuid;
1818         ASSERT_NETISR_NCPUS(cpu);
1819
1820         memset(&arg, 0, sizeof(arg));
1821         arg.old_ifa = rmsg->old_ifa;
1822         arg.new_ifa = rmsg->new_ifa;
1823
1824         rnh = rt_tables[cpu][AF_INET];
1825         for (;;) {
1826                 int error;
1827
1828                 KKASSERT(arg.rt == NULL);
1829                 error = rnh->rnh_walktree(rnh, rtchange_callback, &arg);
1830                 if (arg.rt != NULL) {
1831                         struct rtentry *rt;
1832
1833                         rt = arg.rt;
1834                         arg.rt = NULL;
1835                         rtchange_ifa(rt, &arg);
1836                 } else {
1837                         break;
1838                 }
1839         }
1840         if (arg.changed)
1841                 rmsg->changed = 1;
1842
1843         netisr_forwardmsg(&rmsg->base, cpu + 1);
1844 }
1845
1846 int
1847 rtchange(struct ifaddr *old_ifa, struct ifaddr *new_ifa)
1848 {
1849         struct netmsg_rtchange msg;
1850
1851         /*
1852          * XXX individual requests are not independantly chained,
1853          * which means that the per-cpu route tables will not be
1854          * consistent in the middle of the operation.  If routes
1855          * related to the interface are manipulated while we are
1856          * doing this the inconsistancy could trigger a panic.
1857          */
1858         netmsg_init(&msg.base, NULL, &curthread->td_msgport, MSGF_PRIORITY,
1859             rtchange_dispatch);
1860         msg.old_ifa = old_ifa;
1861         msg.new_ifa = new_ifa;
1862         msg.changed = 0;
1863         netisr_domsg_global(&msg.base);
1864
1865         if (msg.changed) {
1866                 old_ifa->ifa_flags &= ~IFA_ROUTE;
1867                 new_ifa->ifa_flags |= IFA_ROUTE;
1868                 return 0;
1869         } else {
1870                 return ENOENT;
1871         }
1872 }