]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/route.c
o Revamp API between flowtable and netinet, netinet6.
[FreeBSD/FreeBSD.git] / sys / net / route.c
1 /*-
2  * Copyright (c) 1980, 1986, 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)route.c     8.3.1.1 (Berkeley) 2/23/95
30  * $FreeBSD$
31  */
32 /************************************************************************
33  * Note: In this file a 'fib' is a "forwarding information base"        *
34  * Which is the new name for an in kernel routing (next hop) table.     *
35  ***********************************************************************/
36
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39 #include "opt_route.h"
40 #include "opt_sctp.h"
41 #include "opt_mrouting.h"
42 #include "opt_mpath.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/syslog.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/sysctl.h>
51 #include <sys/syslog.h>
52 #include <sys/sysproto.h>
53 #include <sys/proc.h>
54 #include <sys/domain.h>
55 #include <sys/kernel.h>
56
57 #include <net/if.h>
58 #include <net/if_var.h>
59 #include <net/if_dl.h>
60 #include <net/route.h>
61 #include <net/vnet.h>
62 #include <net/flowtable.h>
63
64 #ifdef RADIX_MPATH
65 #include <net/radix_mpath.h>
66 #endif
67
68 #include <netinet/in.h>
69 #include <netinet/ip_mroute.h>
70
71 #include <vm/uma.h>
72
73 #define RT_MAXFIBS      UINT16_MAX
74
75 /* Kernel config default option. */
76 #ifdef ROUTETABLES
77 #if ROUTETABLES <= 0
78 #error "ROUTETABLES defined too low"
79 #endif
80 #if ROUTETABLES > RT_MAXFIBS
81 #error "ROUTETABLES defined too big"
82 #endif
83 #define RT_NUMFIBS      ROUTETABLES
84 #endif /* ROUTETABLES */
85 /* Initialize to default if not otherwise set. */
86 #ifndef RT_NUMFIBS
87 #define RT_NUMFIBS      1
88 #endif
89
90 #if defined(INET) || defined(INET6)
91 #ifdef SCTP
92 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
93 #endif /* SCTP */
94 #endif
95
96
97 /* This is read-only.. */
98 u_int rt_numfibs = RT_NUMFIBS;
99 SYSCTL_UINT(_net, OID_AUTO, fibs, CTLFLAG_RD, &rt_numfibs, 0, "");
100 /* and this can be set too big but will be fixed before it is used */
101 TUNABLE_INT("net.fibs", &rt_numfibs);
102
103 /*
104  * By default add routes to all fibs for new interfaces.
105  * Once this is set to 0 then only allocate routes on interface
106  * changes for the FIB of the caller when adding a new set of addresses
107  * to an interface.  XXX this is a shotgun aproach to a problem that needs
108  * a more fine grained solution.. that will come.
109  * XXX also has the problems getting the FIB from curthread which will not
110  * always work given the fib can be overridden and prefixes can be added
111  * from the network stack context.
112  */
113 u_int rt_add_addr_allfibs = 1;
114 SYSCTL_UINT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RW,
115     &rt_add_addr_allfibs, 0, "");
116 TUNABLE_INT("net.add_addr_allfibs", &rt_add_addr_allfibs);
117
118 VNET_DEFINE(struct rtstat, rtstat);
119 #define V_rtstat        VNET(rtstat)
120
121 VNET_DEFINE(struct radix_node_head *, rt_tables);
122 #define V_rt_tables     VNET(rt_tables)
123
124 VNET_DEFINE(int, rttrash);              /* routes not in table but not freed */
125 #define V_rttrash       VNET(rttrash)
126
127
128 /* compare two sockaddr structures */
129 #define sa_equal(a1, a2) (((a1)->sa_len == (a2)->sa_len) && \
130     (bcmp((a1), (a2), (a1)->sa_len) == 0))
131
132 /*
133  * Convert a 'struct radix_node *' to a 'struct rtentry *'.
134  * The operation can be done safely (in this code) because a
135  * 'struct rtentry' starts with two 'struct radix_node''s, the first
136  * one representing leaf nodes in the routing tree, which is
137  * what the code in radix.c passes us as a 'struct radix_node'.
138  *
139  * But because there are a lot of assumptions in this conversion,
140  * do not cast explicitly, but always use the macro below.
141  */
142 #define RNTORT(p)       ((struct rtentry *)(p))
143
144 static VNET_DEFINE(uma_zone_t, rtzone);         /* Routing table UMA zone. */
145 #define V_rtzone        VNET(rtzone)
146
147 /*
148  * handler for net.my_fibnum
149  */
150 static int
151 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
152 {
153         int fibnum;
154         int error;
155  
156         fibnum = curthread->td_proc->p_fibnum;
157         error = sysctl_handle_int(oidp, &fibnum, 0, req);
158         return (error);
159 }
160
161 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD,
162             NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller");
163
164 static __inline struct radix_node_head **
165 rt_tables_get_rnh_ptr(int table, int fam)
166 {
167         struct radix_node_head **rnh;
168
169         KASSERT(table >= 0 && table < rt_numfibs, ("%s: table out of bounds.",
170             __func__));
171         KASSERT(fam >= 0 && fam < (AF_MAX+1), ("%s: fam out of bounds.",
172             __func__));
173
174         /* rnh is [fib=0][af=0]. */
175         rnh = (struct radix_node_head **)V_rt_tables;
176         /* Get the offset to the requested table and fam. */
177         rnh += table * (AF_MAX+1) + fam;
178
179         return (rnh);
180 }
181
182 struct radix_node_head *
183 rt_tables_get_rnh(int table, int fam)
184 {
185
186         return (*rt_tables_get_rnh_ptr(table, fam));
187 }
188
189 /*
190  * route initialization must occur before ip6_init2(), which happenas at
191  * SI_ORDER_MIDDLE.
192  */
193 static void
194 route_init(void)
195 {
196
197         /* whack the tunable ints into  line. */
198         if (rt_numfibs > RT_MAXFIBS)
199                 rt_numfibs = RT_MAXFIBS;
200         if (rt_numfibs == 0)
201                 rt_numfibs = 1;
202 }
203 SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);
204
205 static void
206 vnet_route_init(const void *unused __unused)
207 {
208         struct domain *dom;
209         struct radix_node_head **rnh;
210         int table;
211         int fam;
212
213         V_rt_tables = malloc(rt_numfibs * (AF_MAX+1) *
214             sizeof(struct radix_node_head *), M_RTABLE, M_WAITOK|M_ZERO);
215
216         V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), NULL, NULL,
217             NULL, NULL, UMA_ALIGN_PTR, 0);
218         for (dom = domains; dom; dom = dom->dom_next) {
219                 if (dom->dom_rtattach == NULL)
220                         continue;
221
222                 for  (table = 0; table < rt_numfibs; table++) {
223                         fam = dom->dom_family;
224                         if (table != 0 && fam != AF_INET6 && fam != AF_INET)
225                                 break;
226
227                         /*
228                          * XXX MRT rtattach will be also called from
229                          * vfs_export.c but the offset will be 0 (only for
230                          * AF_INET and AF_INET6 which don't need it anyhow).
231                          */
232                         rnh = rt_tables_get_rnh_ptr(table, fam);
233                         if (rnh == NULL)
234                                 panic("%s: rnh NULL", __func__);
235                         dom->dom_rtattach((void **)rnh, dom->dom_rtoffset);
236                 }
237         }
238 }
239 VNET_SYSINIT(vnet_route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
240     vnet_route_init, 0);
241
242 #ifdef VIMAGE
243 static void
244 vnet_route_uninit(const void *unused __unused)
245 {
246         int table;
247         int fam;
248         struct domain *dom;
249         struct radix_node_head **rnh;
250
251         for (dom = domains; dom; dom = dom->dom_next) {
252                 if (dom->dom_rtdetach == NULL)
253                         continue;
254
255                 for (table = 0; table < rt_numfibs; table++) {
256                         fam = dom->dom_family;
257
258                         if (table != 0 && fam != AF_INET6 && fam != AF_INET)
259                                 break;
260
261                         rnh = rt_tables_get_rnh_ptr(table, fam);
262                         if (rnh == NULL)
263                                 panic("%s: rnh NULL", __func__);
264                         dom->dom_rtdetach((void **)rnh, dom->dom_rtoffset);
265                 }
266         }
267
268         free(V_rt_tables, M_RTABLE);
269         uma_zdestroy(V_rtzone);
270 }
271 VNET_SYSUNINIT(vnet_route_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
272     vnet_route_uninit, 0);
273 #endif
274
275 #ifndef _SYS_SYSPROTO_H_
276 struct setfib_args {
277         int     fibnum;
278 };
279 #endif
280 int
281 sys_setfib(struct thread *td, struct setfib_args *uap)
282 {
283         if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs)
284                 return EINVAL;
285         td->td_proc->p_fibnum = uap->fibnum;
286         return (0);
287 }
288
289 /*
290  * Packet routing routines.
291  */
292 void
293 rtalloc(struct route *ro)
294 {
295
296         rtalloc_ign_fib(ro, 0UL, RT_DEFAULT_FIB);
297 }
298
299 void
300 rtalloc_fib(struct route *ro, u_int fibnum)
301 {
302         rtalloc_ign_fib(ro, 0UL, fibnum);
303 }
304
305 void
306 rtalloc_ign(struct route *ro, u_long ignore)
307 {
308         struct rtentry *rt;
309
310         if ((rt = ro->ro_rt) != NULL) {
311                 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
312                         return;
313                 RTFREE(rt);
314                 ro->ro_rt = NULL;
315         }
316         ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, RT_DEFAULT_FIB);
317         if (ro->ro_rt)
318                 RT_UNLOCK(ro->ro_rt);
319 }
320
321 void
322 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum)
323 {
324         struct rtentry *rt;
325
326         if ((rt = ro->ro_rt) != NULL) {
327                 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
328                         return;
329                 RTFREE(rt);
330                 ro->ro_rt = NULL;
331         }
332         ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum);
333         if (ro->ro_rt)
334                 RT_UNLOCK(ro->ro_rt);
335 }
336
337 /*
338  * Look up the route that matches the address given
339  * Or, at least try.. Create a cloned route if needed.
340  *
341  * The returned route, if any, is locked.
342  */
343 struct rtentry *
344 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
345 {
346
347         return (rtalloc1_fib(dst, report, ignflags, RT_DEFAULT_FIB));
348 }
349
350 struct rtentry *
351 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags,
352                     u_int fibnum)
353 {
354         struct radix_node_head *rnh;
355         struct radix_node *rn;
356         struct rtentry *newrt;
357         struct rt_addrinfo info;
358         int err = 0, msgtype = RTM_MISS;
359         int needlock;
360
361         KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum"));
362         switch (dst->sa_family) {
363         case AF_INET6:
364         case AF_INET:
365                 /* We support multiple FIBs. */
366                 break;
367         default:
368                 fibnum = RT_DEFAULT_FIB;
369                 break;
370         }
371         rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
372         newrt = NULL;
373         if (rnh == NULL)
374                 goto miss;
375
376         /*
377          * Look up the address in the table for that Address Family
378          */
379         needlock = !(ignflags & RTF_RNH_LOCKED);
380         if (needlock)
381                 RADIX_NODE_HEAD_RLOCK(rnh);
382 #ifdef INVARIANTS       
383         else
384                 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
385 #endif
386         rn = rnh->rnh_matchaddr(dst, rnh);
387         if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
388                 newrt = RNTORT(rn);
389                 RT_LOCK(newrt);
390                 RT_ADDREF(newrt);
391                 if (needlock)
392                         RADIX_NODE_HEAD_RUNLOCK(rnh);
393                 goto done;
394
395         } else if (needlock)
396                 RADIX_NODE_HEAD_RUNLOCK(rnh);
397         
398         /*
399          * Either we hit the root or couldn't find any match,
400          * Which basically means
401          * "caint get there frm here"
402          */
403 miss:
404         V_rtstat.rts_unreach++;
405
406         if (report) {
407                 /*
408                  * If required, report the failure to the supervising
409                  * Authorities.
410                  * For a delete, this is not an error. (report == 0)
411                  */
412                 bzero(&info, sizeof(info));
413                 info.rti_info[RTAX_DST] = dst;
414                 rt_missmsg_fib(msgtype, &info, 0, err, fibnum);
415         }       
416 done:
417         if (newrt)
418                 RT_LOCK_ASSERT(newrt);
419         return (newrt);
420 }
421
422 /*
423  * Remove a reference count from an rtentry.
424  * If the count gets low enough, take it out of the routing table
425  */
426 void
427 rtfree(struct rtentry *rt)
428 {
429         struct radix_node_head *rnh;
430
431         KASSERT(rt != NULL,("%s: NULL rt", __func__));
432         rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family);
433         KASSERT(rnh != NULL,("%s: NULL rnh", __func__));
434
435         RT_LOCK_ASSERT(rt);
436
437         /*
438          * The callers should use RTFREE_LOCKED() or RTFREE(), so
439          * we should come here exactly with the last reference.
440          */
441         RT_REMREF(rt);
442         if (rt->rt_refcnt > 0) {
443                 log(LOG_DEBUG, "%s: %p has %d refs\n", __func__, rt, rt->rt_refcnt);
444                 goto done;
445         }
446
447         /*
448          * On last reference give the "close method" a chance
449          * to cleanup private state.  This also permits (for
450          * IPv4 and IPv6) a chance to decide if the routing table
451          * entry should be purged immediately or at a later time.
452          * When an immediate purge is to happen the close routine
453          * typically calls rtexpunge which clears the RTF_UP flag
454          * on the entry so that the code below reclaims the storage.
455          */
456         if (rt->rt_refcnt == 0 && rnh->rnh_close)
457                 rnh->rnh_close((struct radix_node *)rt, rnh);
458
459         /*
460          * If we are no longer "up" (and ref == 0)
461          * then we can free the resources associated
462          * with the route.
463          */
464         if ((rt->rt_flags & RTF_UP) == 0) {
465                 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
466                         panic("rtfree 2");
467                 /*
468                  * the rtentry must have been removed from the routing table
469                  * so it is represented in rttrash.. remove that now.
470                  */
471                 V_rttrash--;
472 #ifdef  DIAGNOSTIC
473                 if (rt->rt_refcnt < 0) {
474                         printf("rtfree: %p not freed (neg refs)\n", rt);
475                         goto done;
476                 }
477 #endif
478                 /*
479                  * release references on items we hold them on..
480                  * e.g other routes and ifaddrs.
481                  */
482                 if (rt->rt_ifa)
483                         ifa_free(rt->rt_ifa);
484                 /*
485                  * The key is separatly alloc'd so free it (see rt_setgate()).
486                  * This also frees the gateway, as they are always malloc'd
487                  * together.
488                  */
489                 Free(rt_key(rt));
490
491                 /*
492                  * and the rtentry itself of course
493                  */
494                 RT_LOCK_DESTROY(rt);
495                 uma_zfree(V_rtzone, rt);
496                 return;
497         }
498 done:
499         RT_UNLOCK(rt);
500 }
501
502
503 /*
504  * Force a routing table entry to the specified
505  * destination to go through the given gateway.
506  * Normally called as a result of a routing redirect
507  * message from the network layer.
508  */
509 void
510 rtredirect(struct sockaddr *dst,
511         struct sockaddr *gateway,
512         struct sockaddr *netmask,
513         int flags,
514         struct sockaddr *src)
515 {
516
517         rtredirect_fib(dst, gateway, netmask, flags, src, RT_DEFAULT_FIB);
518 }
519
520 void
521 rtredirect_fib(struct sockaddr *dst,
522         struct sockaddr *gateway,
523         struct sockaddr *netmask,
524         int flags,
525         struct sockaddr *src,
526         u_int fibnum)
527 {
528         struct rtentry *rt, *rt0 = NULL;
529         int error = 0;
530         short *stat = NULL;
531         struct rt_addrinfo info;
532         struct ifaddr *ifa;
533         struct radix_node_head *rnh;
534
535         ifa = NULL;
536         rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
537         if (rnh == NULL) {
538                 error = EAFNOSUPPORT;
539                 goto out;
540         }
541
542         /* verify the gateway is directly reachable */
543         if ((ifa = ifa_ifwithnet(gateway, 0)) == NULL) {
544                 error = ENETUNREACH;
545                 goto out;
546         }
547         rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */
548         /*
549          * If the redirect isn't from our current router for this dst,
550          * it's either old or wrong.  If it redirects us to ourselves,
551          * we have a routing loop, perhaps as a result of an interface
552          * going down recently.
553          */
554         if (!(flags & RTF_DONE) && rt &&
555              (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
556                 error = EINVAL;
557         else if (ifa_ifwithaddr_check(gateway))
558                 error = EHOSTUNREACH;
559         if (error)
560                 goto done;
561         /*
562          * Create a new entry if we just got back a wildcard entry
563          * or the lookup failed.  This is necessary for hosts
564          * which use routing redirects generated by smart gateways
565          * to dynamically build the routing tables.
566          */
567         if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
568                 goto create;
569         /*
570          * Don't listen to the redirect if it's
571          * for a route to an interface.
572          */
573         if (rt->rt_flags & RTF_GATEWAY) {
574                 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
575                         /*
576                          * Changing from route to net => route to host.
577                          * Create new route, rather than smashing route to net.
578                          */
579                 create:
580                         rt0 = rt;
581                         rt = NULL;
582                 
583                         flags |=  RTF_GATEWAY | RTF_DYNAMIC;
584                         bzero((caddr_t)&info, sizeof(info));
585                         info.rti_info[RTAX_DST] = dst;
586                         info.rti_info[RTAX_GATEWAY] = gateway;
587                         info.rti_info[RTAX_NETMASK] = netmask;
588                         info.rti_ifa = ifa;
589                         info.rti_flags = flags;
590                         if (rt0 != NULL)
591                                 RT_UNLOCK(rt0); /* drop lock to avoid LOR with RNH */
592                         error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum);
593                         if (rt != NULL) {
594                                 RT_LOCK(rt);
595                                 if (rt0 != NULL)
596                                         EVENTHANDLER_INVOKE(route_redirect_event, rt0, rt, dst);
597                                 flags = rt->rt_flags;
598                         }
599                         if (rt0 != NULL)
600                                 RTFREE(rt0);
601                         
602                         stat = &V_rtstat.rts_dynamic;
603                 } else {
604                         struct rtentry *gwrt;
605
606                         /*
607                          * Smash the current notion of the gateway to
608                          * this destination.  Should check about netmask!!!
609                          */
610                         rt->rt_flags |= RTF_MODIFIED;
611                         flags |= RTF_MODIFIED;
612                         stat = &V_rtstat.rts_newgateway;
613                         /*
614                          * add the key and gateway (in one malloc'd chunk).
615                          */
616                         RT_UNLOCK(rt);
617                         RADIX_NODE_HEAD_LOCK(rnh);
618                         RT_LOCK(rt);
619                         rt_setgate(rt, rt_key(rt), gateway);
620                         gwrt = rtalloc1(gateway, 1, RTF_RNH_LOCKED);
621                         RADIX_NODE_HEAD_UNLOCK(rnh);
622                         EVENTHANDLER_INVOKE(route_redirect_event, rt, gwrt, dst);
623                         RTFREE_LOCKED(gwrt);
624                 }
625         } else
626                 error = EHOSTUNREACH;
627 done:
628         if (rt)
629                 RTFREE_LOCKED(rt);
630 out:
631         if (error)
632                 V_rtstat.rts_badredirect++;
633         else if (stat != NULL)
634                 (*stat)++;
635         bzero((caddr_t)&info, sizeof(info));
636         info.rti_info[RTAX_DST] = dst;
637         info.rti_info[RTAX_GATEWAY] = gateway;
638         info.rti_info[RTAX_NETMASK] = netmask;
639         info.rti_info[RTAX_AUTHOR] = src;
640         rt_missmsg_fib(RTM_REDIRECT, &info, flags, error, fibnum);
641         if (ifa != NULL)
642                 ifa_free(ifa);
643 }
644
645 int
646 rtioctl(u_long req, caddr_t data)
647 {
648
649         return (rtioctl_fib(req, data, RT_DEFAULT_FIB));
650 }
651
652 /*
653  * Routing table ioctl interface.
654  */
655 int
656 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
657 {
658
659         /*
660          * If more ioctl commands are added here, make sure the proper
661          * super-user checks are being performed because it is possible for
662          * prison-root to make it this far if raw sockets have been enabled
663          * in jails.
664          */
665 #ifdef INET
666         /* Multicast goop, grrr... */
667         return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
668 #else /* INET */
669         return ENXIO;
670 #endif /* INET */
671 }
672
673 /*
674  * For both ifa_ifwithroute() routines, 'ifa' is returned referenced.
675  */
676 struct ifaddr *
677 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
678 {
679
680         return (ifa_ifwithroute_fib(flags, dst, gateway, RT_DEFAULT_FIB));
681 }
682
683 struct ifaddr *
684 ifa_ifwithroute_fib(int flags, struct sockaddr *dst, struct sockaddr *gateway,
685                                 u_int fibnum)
686 {
687         register struct ifaddr *ifa;
688         int not_found = 0;
689
690         if ((flags & RTF_GATEWAY) == 0) {
691                 /*
692                  * If we are adding a route to an interface,
693                  * and the interface is a pt to pt link
694                  * we should search for the destination
695                  * as our clue to the interface.  Otherwise
696                  * we can use the local address.
697                  */
698                 ifa = NULL;
699                 if (flags & RTF_HOST)
700                         ifa = ifa_ifwithdstaddr(dst);
701                 if (ifa == NULL)
702                         ifa = ifa_ifwithaddr(gateway);
703         } else {
704                 /*
705                  * If we are adding a route to a remote net
706                  * or host, the gateway may still be on the
707                  * other end of a pt to pt link.
708                  */
709                 ifa = ifa_ifwithdstaddr(gateway);
710         }
711         if (ifa == NULL)
712                 ifa = ifa_ifwithnet(gateway, 0);
713         if (ifa == NULL) {
714                 struct rtentry *rt = rtalloc1_fib(gateway, 0, RTF_RNH_LOCKED, fibnum);
715                 if (rt == NULL)
716                         return (NULL);
717                 /*
718                  * dismiss a gateway that is reachable only
719                  * through the default router
720                  */
721                 switch (gateway->sa_family) {
722                 case AF_INET:
723                         if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY)
724                                 not_found = 1;
725                         break;
726                 case AF_INET6:
727                         if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr))
728                                 not_found = 1;
729                         break;
730                 default:
731                         break;
732                 }
733                 if (!not_found && rt->rt_ifa != NULL) {
734                         ifa = rt->rt_ifa;
735                         ifa_ref(ifa);
736                 }
737                 RT_REMREF(rt);
738                 RT_UNLOCK(rt);
739                 if (not_found || ifa == NULL)
740                         return (NULL);
741         }
742         if (ifa->ifa_addr->sa_family != dst->sa_family) {
743                 struct ifaddr *oifa = ifa;
744                 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
745                 if (ifa == NULL)
746                         ifa = oifa;
747                 else
748                         ifa_free(oifa);
749         }
750         return (ifa);
751 }
752
753 /*
754  * Do appropriate manipulations of a routing tree given
755  * all the bits of info needed
756  */
757 int
758 rtrequest(int req,
759         struct sockaddr *dst,
760         struct sockaddr *gateway,
761         struct sockaddr *netmask,
762         int flags,
763         struct rtentry **ret_nrt)
764 {
765
766         return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt,
767             RT_DEFAULT_FIB));
768 }
769
770 int
771 rtrequest_fib(int req,
772         struct sockaddr *dst,
773         struct sockaddr *gateway,
774         struct sockaddr *netmask,
775         int flags,
776         struct rtentry **ret_nrt,
777         u_int fibnum)
778 {
779         struct rt_addrinfo info;
780
781         if (dst->sa_len == 0)
782                 return(EINVAL);
783
784         bzero((caddr_t)&info, sizeof(info));
785         info.rti_flags = flags;
786         info.rti_info[RTAX_DST] = dst;
787         info.rti_info[RTAX_GATEWAY] = gateway;
788         info.rti_info[RTAX_NETMASK] = netmask;
789         return rtrequest1_fib(req, &info, ret_nrt, fibnum);
790 }
791
792 /*
793  * These (questionable) definitions of apparent local variables apply
794  * to the next two functions.  XXXXXX!!!
795  */
796 #define dst     info->rti_info[RTAX_DST]
797 #define gateway info->rti_info[RTAX_GATEWAY]
798 #define netmask info->rti_info[RTAX_NETMASK]
799 #define ifaaddr info->rti_info[RTAX_IFA]
800 #define ifpaddr info->rti_info[RTAX_IFP]
801 #define flags   info->rti_flags
802
803 int
804 rt_getifa(struct rt_addrinfo *info)
805 {
806
807         return (rt_getifa_fib(info, RT_DEFAULT_FIB));
808 }
809
810 /*
811  * Look up rt_addrinfo for a specific fib.  Note that if rti_ifa is defined,
812  * it will be referenced so the caller must free it.
813  */
814 int
815 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
816 {
817         struct ifaddr *ifa;
818         int error = 0;
819
820         /*
821          * ifp may be specified by sockaddr_dl
822          * when protocol address is ambiguous.
823          */
824         if (info->rti_ifp == NULL && ifpaddr != NULL &&
825             ifpaddr->sa_family == AF_LINK &&
826             (ifa = ifa_ifwithnet(ifpaddr, 0)) != NULL) {
827                 info->rti_ifp = ifa->ifa_ifp;
828                 ifa_free(ifa);
829         }
830         if (info->rti_ifa == NULL && ifaaddr != NULL)
831                 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
832         if (info->rti_ifa == NULL) {
833                 struct sockaddr *sa;
834
835                 sa = ifaaddr != NULL ? ifaaddr :
836                     (gateway != NULL ? gateway : dst);
837                 if (sa != NULL && info->rti_ifp != NULL)
838                         info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
839                 else if (dst != NULL && gateway != NULL)
840                         info->rti_ifa = ifa_ifwithroute_fib(flags, dst, gateway,
841                                                         fibnum);
842                 else if (sa != NULL)
843                         info->rti_ifa = ifa_ifwithroute_fib(flags, sa, sa,
844                                                         fibnum);
845         }
846         if ((ifa = info->rti_ifa) != NULL) {
847                 if (info->rti_ifp == NULL)
848                         info->rti_ifp = ifa->ifa_ifp;
849         } else
850                 error = ENETUNREACH;
851         return (error);
852 }
853
854 /*
855  * Expunges references to a route that's about to be reclaimed.
856  * The route must be locked.
857  */
858 int
859 rtexpunge(struct rtentry *rt)
860 {
861 #if !defined(RADIX_MPATH)
862         struct radix_node *rn;
863 #else
864         struct rt_addrinfo info;
865         int fib;
866         struct rtentry *rt0;
867 #endif
868         struct radix_node_head *rnh;
869         struct ifaddr *ifa;
870         int error = 0;
871
872         /*
873          * Find the correct routing tree to use for this Address Family
874          */
875         rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family);
876         RT_LOCK_ASSERT(rt);
877         if (rnh == NULL)
878                 return (EAFNOSUPPORT);
879         RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
880
881 #ifdef RADIX_MPATH
882         fib = rt->rt_fibnum;
883         bzero(&info, sizeof(info));
884         info.rti_ifp = rt->rt_ifp;
885         info.rti_flags = RTF_RNH_LOCKED;
886         info.rti_info[RTAX_DST] = rt_key(rt);
887         info.rti_info[RTAX_GATEWAY] = rt->rt_ifa->ifa_addr;
888
889         RT_UNLOCK(rt);
890         error = rtrequest1_fib(RTM_DELETE, &info, &rt0, fib);
891
892         if (error == 0 && rt0 != NULL) {
893                 rt = rt0;
894                 RT_LOCK(rt);
895         } else if (error != 0) {
896                 RT_LOCK(rt);
897                 return (error);
898         }
899 #else
900         /*
901          * Remove the item from the tree; it should be there,
902          * but when callers invoke us blindly it may not (sigh).
903          */
904         rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh);
905         if (rn == NULL) {
906                 error = ESRCH;
907                 goto bad;
908         }
909         KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0,
910                 ("unexpected flags 0x%x", rn->rn_flags));
911         KASSERT(rt == RNTORT(rn),
912                 ("lookup mismatch, rt %p rn %p", rt, rn));
913 #endif /* RADIX_MPATH */
914
915         rt->rt_flags &= ~RTF_UP;
916
917         /*
918          * Give the protocol a chance to keep things in sync.
919          */
920         if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) {
921                 struct rt_addrinfo info;
922
923                 bzero((caddr_t)&info, sizeof(info));
924                 info.rti_flags = rt->rt_flags;
925                 info.rti_info[RTAX_DST] = rt_key(rt);
926                 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
927                 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
928                 ifa->ifa_rtrequest(RTM_DELETE, rt, &info);
929         }
930
931         /*
932          * one more rtentry floating around that is not
933          * linked to the routing table.
934          */
935         V_rttrash++;
936 #if !defined(RADIX_MPATH)
937 bad:
938 #endif
939         return (error);
940 }
941
942 #if 0
943 int p_sockaddr(char *buf, int buflen, struct sockaddr *s);
944 int rt_print(char *buf, int buflen, struct rtentry *rt);
945
946 int
947 p_sockaddr(char *buf, int buflen, struct sockaddr *s)
948 {
949         void *paddr = NULL;
950
951         switch (s->sa_family) {
952         case AF_INET:
953                 paddr = &((struct sockaddr_in *)s)->sin_addr;
954                 break;
955         case AF_INET6:
956                 paddr = &((struct sockaddr_in6 *)s)->sin6_addr;
957                 break;
958         }
959
960         if (paddr == NULL)
961                 return (0);
962
963         if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL)
964                 return (0);
965         
966         return (strlen(buf));
967 }
968
969 int
970 rt_print(char *buf, int buflen, struct rtentry *rt)
971 {
972         struct sockaddr *addr, *mask;
973         int i = 0;
974
975         addr = rt_key(rt);
976         mask = rt_mask(rt);
977
978         i = p_sockaddr(buf, buflen, addr);
979         if (!(rt->rt_flags & RTF_HOST)) {
980                 buf[i++] = '/';
981                 i += p_sockaddr(buf + i, buflen - i, mask);
982         }
983
984         if (rt->rt_flags & RTF_GATEWAY) {
985                 buf[i++] = '>';
986                 i += p_sockaddr(buf + i, buflen - i, rt->rt_gateway);
987         }
988
989         return (i);
990 }
991 #endif
992
993 #ifdef RADIX_MPATH
994 static int
995 rn_mpath_update(int req, struct rt_addrinfo *info,
996     struct radix_node_head *rnh, struct rtentry **ret_nrt)
997 {
998         /*
999          * if we got multipath routes, we require users to specify
1000          * a matching RTAX_GATEWAY.
1001          */
1002         struct rtentry *rt, *rto = NULL;
1003         register struct radix_node *rn;
1004         int error = 0;
1005
1006         rn = rnh->rnh_lookup(dst, netmask, rnh);
1007         if (rn == NULL)
1008                 return (ESRCH);
1009         rto = rt = RNTORT(rn);
1010
1011         rt = rt_mpath_matchgate(rt, gateway);
1012         if (rt == NULL)
1013                 return (ESRCH);
1014         /*
1015          * this is the first entry in the chain
1016          */
1017         if (rto == rt) {
1018                 rn = rn_mpath_next((struct radix_node *)rt);
1019                 /*
1020                  * there is another entry, now it's active
1021                  */
1022                 if (rn) {
1023                         rto = RNTORT(rn);
1024                         RT_LOCK(rto);
1025                         rto->rt_flags |= RTF_UP;
1026                         RT_UNLOCK(rto);
1027                 } else if (rt->rt_flags & RTF_GATEWAY) {
1028                         /*
1029                          * For gateway routes, we need to 
1030                          * make sure that we we are deleting
1031                          * the correct gateway. 
1032                          * rt_mpath_matchgate() does not 
1033                          * check the case when there is only
1034                          * one route in the chain.  
1035                          */
1036                         if (gateway &&
1037                             (rt->rt_gateway->sa_len != gateway->sa_len ||
1038                                 memcmp(rt->rt_gateway, gateway, gateway->sa_len)))
1039                                 error = ESRCH;
1040                         else {
1041                                 /*
1042                                  * remove from tree before returning it
1043                                  * to the caller
1044                                  */
1045                                 rn = rnh->rnh_deladdr(dst, netmask, rnh);
1046                                 KASSERT(rt == RNTORT(rn), ("radix node disappeared"));
1047                                 goto gwdelete;
1048                         }
1049                         
1050                 }
1051                 /*
1052                  * use the normal delete code to remove
1053                  * the first entry
1054                  */
1055                 if (req != RTM_DELETE) 
1056                         goto nondelete;
1057
1058                 error = ENOENT;
1059                 goto done;
1060         }
1061                 
1062         /*
1063          * if the entry is 2nd and on up
1064          */
1065         if ((req == RTM_DELETE) && !rt_mpath_deldup(rto, rt))
1066                 panic ("rtrequest1: rt_mpath_deldup");
1067 gwdelete:
1068         RT_LOCK(rt);
1069         RT_ADDREF(rt);
1070         if (req == RTM_DELETE) {
1071                 rt->rt_flags &= ~RTF_UP;
1072                 /*
1073                  * One more rtentry floating around that is not
1074                  * linked to the routing table. rttrash will be decremented
1075                  * when RTFREE(rt) is eventually called.
1076                  */
1077                 V_rttrash++;
1078         }
1079         
1080 nondelete:
1081         if (req != RTM_DELETE)
1082                 panic("unrecognized request %d", req);
1083         
1084
1085         /*
1086          * If the caller wants it, then it can have it,
1087          * but it's up to it to free the rtentry as we won't be
1088          * doing it.
1089          */
1090         if (ret_nrt) {
1091                 *ret_nrt = rt;
1092                 RT_UNLOCK(rt);
1093         } else
1094                 RTFREE_LOCKED(rt);
1095 done:
1096         return (error);
1097 }
1098 #endif
1099
1100 int
1101 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt,
1102                                 u_int fibnum)
1103 {
1104         int error = 0, needlock = 0;
1105         register struct rtentry *rt;
1106 #ifdef FLOWTABLE
1107         register struct rtentry *rt0;
1108 #endif
1109         register struct radix_node *rn;
1110         register struct radix_node_head *rnh;
1111         struct ifaddr *ifa;
1112         struct sockaddr *ndst;
1113         struct sockaddr_storage mdst;
1114 #define senderr(x) { error = x ; goto bad; }
1115
1116         KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum"));
1117         switch (dst->sa_family) {
1118         case AF_INET6:
1119         case AF_INET:
1120                 /* We support multiple FIBs. */
1121                 break;
1122         default:
1123                 fibnum = RT_DEFAULT_FIB;
1124                 break;
1125         }
1126
1127         /*
1128          * Find the correct routing tree to use for this Address Family
1129          */
1130         rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
1131         if (rnh == NULL)
1132                 return (EAFNOSUPPORT);
1133         needlock = ((flags & RTF_RNH_LOCKED) == 0);
1134         flags &= ~RTF_RNH_LOCKED;
1135         if (needlock)
1136                 RADIX_NODE_HEAD_LOCK(rnh);
1137         else
1138                 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
1139         /*
1140          * If we are adding a host route then we don't want to put
1141          * a netmask in the tree, nor do we want to clone it.
1142          */
1143         if (flags & RTF_HOST)
1144                 netmask = NULL;
1145
1146         switch (req) {
1147         case RTM_DELETE:
1148                 if (netmask) {
1149                         rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask);
1150                         dst = (struct sockaddr *)&mdst;
1151                 }
1152 #ifdef RADIX_MPATH
1153                 if (rn_mpath_capable(rnh)) {
1154                         error = rn_mpath_update(req, info, rnh, ret_nrt);
1155                         /*
1156                          * "bad" holds true for the success case
1157                          * as well
1158                          */
1159                         if (error != ENOENT)
1160                                 goto bad;
1161                         error = 0;
1162                 }
1163 #endif
1164                 if ((flags & RTF_PINNED) == 0) {
1165                         /* Check if target route can be deleted */
1166                         rt = (struct rtentry *)rnh->rnh_lookup(dst,
1167                             netmask, rnh);
1168                         if ((rt != NULL) && (rt->rt_flags & RTF_PINNED))
1169                                 senderr(EADDRINUSE);
1170                 }
1171
1172                 /*
1173                  * Remove the item from the tree and return it.
1174                  * Complain if it is not there and do no more processing.
1175                  */
1176                 rn = rnh->rnh_deladdr(dst, netmask, rnh);
1177                 if (rn == NULL)
1178                         senderr(ESRCH);
1179                 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
1180                         panic ("rtrequest delete");
1181                 rt = RNTORT(rn);
1182                 RT_LOCK(rt);
1183                 RT_ADDREF(rt);
1184                 rt->rt_flags &= ~RTF_UP;
1185
1186                 /*
1187                  * give the protocol a chance to keep things in sync.
1188                  */
1189                 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
1190                         ifa->ifa_rtrequest(RTM_DELETE, rt, info);
1191
1192                 /*
1193                  * One more rtentry floating around that is not
1194                  * linked to the routing table. rttrash will be decremented
1195                  * when RTFREE(rt) is eventually called.
1196                  */
1197                 V_rttrash++;
1198
1199                 /*
1200                  * If the caller wants it, then it can have it,
1201                  * but it's up to it to free the rtentry as we won't be
1202                  * doing it.
1203                  */
1204                 if (ret_nrt) {
1205                         *ret_nrt = rt;
1206                         RT_UNLOCK(rt);
1207                 } else
1208                         RTFREE_LOCKED(rt);
1209                 break;
1210         case RTM_RESOLVE:
1211                 /*
1212                  * resolve was only used for route cloning
1213                  * here for compat
1214                  */
1215                 break;
1216         case RTM_ADD:
1217                 if ((flags & RTF_GATEWAY) && !gateway)
1218                         senderr(EINVAL);
1219                 if (dst && gateway && (dst->sa_family != gateway->sa_family) && 
1220                     (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
1221                         senderr(EINVAL);
1222
1223                 if (info->rti_ifa == NULL) {
1224                         error = rt_getifa_fib(info, fibnum);
1225                         if (error)
1226                                 senderr(error);
1227                 } else
1228                         ifa_ref(info->rti_ifa);
1229                 ifa = info->rti_ifa;
1230                 rt = uma_zalloc(V_rtzone, M_NOWAIT | M_ZERO);
1231                 if (rt == NULL) {
1232                         ifa_free(ifa);
1233                         senderr(ENOBUFS);
1234                 }
1235                 RT_LOCK_INIT(rt);
1236                 rt->rt_flags = RTF_UP | flags;
1237                 rt->rt_fibnum = fibnum;
1238                 /*
1239                  * Add the gateway. Possibly re-malloc-ing the storage for it.
1240                  */
1241                 RT_LOCK(rt);
1242                 if ((error = rt_setgate(rt, dst, gateway)) != 0) {
1243                         RT_LOCK_DESTROY(rt);
1244                         ifa_free(ifa);
1245                         uma_zfree(V_rtzone, rt);
1246                         senderr(error);
1247                 }
1248
1249                 /*
1250                  * point to the (possibly newly malloc'd) dest address.
1251                  */
1252                 ndst = (struct sockaddr *)rt_key(rt);
1253
1254                 /*
1255                  * make sure it contains the value we want (masked if needed).
1256                  */
1257                 if (netmask) {
1258                         rt_maskedcopy(dst, ndst, netmask);
1259                 } else
1260                         bcopy(dst, ndst, dst->sa_len);
1261
1262                 /*
1263                  * We use the ifa reference returned by rt_getifa_fib().
1264                  * This moved from below so that rnh->rnh_addaddr() can
1265                  * examine the ifa and  ifa->ifa_ifp if it so desires.
1266                  */
1267                 rt->rt_ifa = ifa;
1268                 rt->rt_ifp = ifa->ifa_ifp;
1269                 rt->rt_rmx.rmx_weight = 1;
1270
1271 #ifdef RADIX_MPATH
1272                 /* do not permit exactly the same dst/mask/gw pair */
1273                 if (rn_mpath_capable(rnh) &&
1274                         rt_mpath_conflict(rnh, rt, netmask)) {
1275                         ifa_free(rt->rt_ifa);
1276                         Free(rt_key(rt));
1277                         RT_LOCK_DESTROY(rt);
1278                         uma_zfree(V_rtzone, rt);
1279                         senderr(EEXIST);
1280                 }
1281 #endif
1282
1283 #ifdef FLOWTABLE
1284                 rt0 = NULL;
1285                 /* "flow-table" only supports IPv6 and IPv4 at the moment. */
1286                 switch (dst->sa_family) {
1287 #ifdef INET6
1288                 case AF_INET6:
1289 #endif
1290 #ifdef INET
1291                 case AF_INET:
1292 #endif
1293 #if defined(INET6) || defined(INET)
1294                         rn = rnh->rnh_matchaddr(dst, rnh);
1295                         if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
1296                                 struct sockaddr *mask;
1297                                 u_char *m, *n;
1298                                 int len;
1299                                 
1300                                 /*
1301                                  * compare mask to see if the new route is
1302                                  * more specific than the existing one
1303                                  */
1304                                 rt0 = RNTORT(rn);
1305                                 RT_LOCK(rt0);
1306                                 RT_ADDREF(rt0);
1307                                 RT_UNLOCK(rt0);
1308                                 /*
1309                                  * A host route is already present, so 
1310                                  * leave the flow-table entries as is.
1311                                  */
1312                                 if (rt0->rt_flags & RTF_HOST) {
1313                                         RTFREE(rt0);
1314                                         rt0 = NULL;
1315                                 } else if (!(flags & RTF_HOST) && netmask) {
1316                                         mask = rt_mask(rt0);
1317                                         len = mask->sa_len;
1318                                         m = (u_char *)mask;
1319                                         n = (u_char *)netmask;
1320                                         while (len-- > 0) {
1321                                                 if (*n != *m)
1322                                                         break;
1323                                                 n++;
1324                                                 m++;
1325                                         }
1326                                         if (len == 0 || (*n < *m)) {
1327                                                 RTFREE(rt0);
1328                                                 rt0 = NULL;
1329                                         }
1330                                 }
1331                         }
1332 #endif/* INET6 || INET */
1333                 }
1334 #endif /* FLOWTABLE */
1335
1336                 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
1337                 rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
1338                 /*
1339                  * If it still failed to go into the tree,
1340                  * then un-make it (this should be a function)
1341                  */
1342                 if (rn == NULL) {
1343                         ifa_free(rt->rt_ifa);
1344                         Free(rt_key(rt));
1345                         RT_LOCK_DESTROY(rt);
1346                         uma_zfree(V_rtzone, rt);
1347 #ifdef FLOWTABLE
1348                         if (rt0 != NULL)
1349                                 RTFREE(rt0);
1350 #endif
1351                         senderr(EEXIST);
1352                 } 
1353 #ifdef FLOWTABLE
1354                 else if (rt0 != NULL) {
1355                         flowtable_route_flush(dst->sa_family, rt0);
1356                         RTFREE(rt0);
1357                 }
1358 #endif
1359
1360                 /*
1361                  * If this protocol has something to add to this then
1362                  * allow it to do that as well.
1363                  */
1364                 if (ifa->ifa_rtrequest)
1365                         ifa->ifa_rtrequest(req, rt, info);
1366
1367                 /*
1368                  * actually return a resultant rtentry and
1369                  * give the caller a single reference.
1370                  */
1371                 if (ret_nrt) {
1372                         *ret_nrt = rt;
1373                         RT_ADDREF(rt);
1374                 }
1375                 RT_UNLOCK(rt);
1376                 break;
1377         default:
1378                 error = EOPNOTSUPP;
1379         }
1380 bad:
1381         if (needlock)
1382                 RADIX_NODE_HEAD_UNLOCK(rnh);
1383         return (error);
1384 #undef senderr
1385 }
1386
1387 #undef dst
1388 #undef gateway
1389 #undef netmask
1390 #undef ifaaddr
1391 #undef ifpaddr
1392 #undef flags
1393
1394 int
1395 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
1396 {
1397         /* XXX dst may be overwritten, can we move this to below */
1398         int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
1399 #ifdef INVARIANTS
1400         struct radix_node_head *rnh;
1401
1402         rnh = rt_tables_get_rnh(rt->rt_fibnum, dst->sa_family);
1403 #endif
1404
1405         RT_LOCK_ASSERT(rt);
1406         RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
1407         
1408         /*
1409          * Prepare to store the gateway in rt->rt_gateway.
1410          * Both dst and gateway are stored one after the other in the same
1411          * malloc'd chunk. If we have room, we can reuse the old buffer,
1412          * rt_gateway already points to the right place.
1413          * Otherwise, malloc a new block and update the 'dst' address.
1414          */
1415         if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
1416                 caddr_t new;
1417
1418                 R_Malloc(new, caddr_t, dlen + glen);
1419                 if (new == NULL)
1420                         return ENOBUFS;
1421                 /*
1422                  * XXX note, we copy from *dst and not *rt_key(rt) because
1423                  * rt_setgate() can be called to initialize a newly
1424                  * allocated route entry, in which case rt_key(rt) == NULL
1425                  * (and also rt->rt_gateway == NULL).
1426                  * Free()/free() handle a NULL argument just fine.
1427                  */
1428                 bcopy(dst, new, dlen);
1429                 Free(rt_key(rt));       /* free old block, if any */
1430                 rt_key(rt) = (struct sockaddr *)new;
1431                 rt->rt_gateway = (struct sockaddr *)(new + dlen);
1432         }
1433
1434         /*
1435          * Copy the new gateway value into the memory chunk.
1436          */
1437         bcopy(gate, rt->rt_gateway, glen);
1438
1439         return (0);
1440 }
1441
1442 void
1443 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
1444 {
1445         register u_char *cp1 = (u_char *)src;
1446         register u_char *cp2 = (u_char *)dst;
1447         register u_char *cp3 = (u_char *)netmask;
1448         u_char *cplim = cp2 + *cp3;
1449         u_char *cplim2 = cp2 + *cp1;
1450
1451         *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1452         cp3 += 2;
1453         if (cplim > cplim2)
1454                 cplim = cplim2;
1455         while (cp2 < cplim)
1456                 *cp2++ = *cp1++ & *cp3++;
1457         if (cp2 < cplim2)
1458                 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1459 }
1460
1461 /*
1462  * Set up a routing table entry, normally
1463  * for an interface.
1464  */
1465 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */
1466 static inline  int
1467 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum)
1468 {
1469         struct sockaddr *dst;
1470         struct sockaddr *netmask;
1471         struct rtentry *rt = NULL;
1472         struct rt_addrinfo info;
1473         int error = 0;
1474         int startfib, endfib;
1475         char tempbuf[_SOCKADDR_TMPSIZE];
1476         int didwork = 0;
1477         int a_failure = 0;
1478         static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1479         struct radix_node_head *rnh;
1480
1481         if (flags & RTF_HOST) {
1482                 dst = ifa->ifa_dstaddr;
1483                 netmask = NULL;
1484         } else {
1485                 dst = ifa->ifa_addr;
1486                 netmask = ifa->ifa_netmask;
1487         }
1488         if (dst->sa_len == 0)
1489                 return(EINVAL);
1490         switch (dst->sa_family) {
1491         case AF_INET6:
1492         case AF_INET:
1493                 /* We support multiple FIBs. */
1494                 break;
1495         default:
1496                 fibnum = RT_DEFAULT_FIB;
1497                 break;
1498         }
1499         if (fibnum == RT_ALL_FIBS) {
1500                 if (rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD) {
1501                         startfib = endfib = curthread->td_proc->p_fibnum;
1502                 } else {
1503                         startfib = 0;
1504                         endfib = rt_numfibs - 1;
1505                 }
1506         } else {
1507                 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum"));
1508                 startfib = fibnum;
1509                 endfib = fibnum;
1510         }
1511
1512         /*
1513          * If it's a delete, check that if it exists,
1514          * it's on the correct interface or we might scrub
1515          * a route to another ifa which would
1516          * be confusing at best and possibly worse.
1517          */
1518         if (cmd == RTM_DELETE) {
1519                 /*
1520                  * It's a delete, so it should already exist..
1521                  * If it's a net, mask off the host bits
1522                  * (Assuming we have a mask)
1523                  * XXX this is kinda inet specific..
1524                  */
1525                 if (netmask != NULL) {
1526                         rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask);
1527                         dst = (struct sockaddr *)tempbuf;
1528                 }
1529         }
1530         /*
1531          * Now go through all the requested tables (fibs) and do the
1532          * requested action. Realistically, this will either be fib 0
1533          * for protocols that don't do multiple tables or all the
1534          * tables for those that do.
1535          */
1536         for ( fibnum = startfib; fibnum <= endfib; fibnum++) {
1537                 if (cmd == RTM_DELETE) {
1538                         struct radix_node *rn;
1539                         /*
1540                          * Look up an rtentry that is in the routing tree and
1541                          * contains the correct info.
1542                          */
1543                         rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
1544                         if (rnh == NULL)
1545                                 /* this table doesn't exist but others might */
1546                                 continue;
1547                         RADIX_NODE_HEAD_RLOCK(rnh);
1548                         rn = rnh->rnh_lookup(dst, netmask, rnh);
1549 #ifdef RADIX_MPATH
1550                         if (rn_mpath_capable(rnh)) {
1551
1552                                 if (rn == NULL) 
1553                                         error = ESRCH;
1554                                 else {
1555                                         rt = RNTORT(rn);
1556                                         /*
1557                                          * for interface route the
1558                                          * rt->rt_gateway is sockaddr_intf
1559                                          * for cloning ARP entries, so
1560                                          * rt_mpath_matchgate must use the
1561                                          * interface address
1562                                          */
1563                                         rt = rt_mpath_matchgate(rt,
1564                                             ifa->ifa_addr);
1565                                         if (rt == NULL) 
1566                                                 error = ESRCH;
1567                                 }
1568                         }
1569 #endif
1570                         error = (rn == NULL ||
1571                             (rn->rn_flags & RNF_ROOT) ||
1572                             RNTORT(rn)->rt_ifa != ifa);
1573                         RADIX_NODE_HEAD_RUNLOCK(rnh);
1574                         if (error) {
1575                                 /* this is only an error if bad on ALL tables */
1576                                 continue;
1577                         }
1578                 }
1579                 /*
1580                  * Do the actual request
1581                  */
1582                 bzero((caddr_t)&info, sizeof(info));
1583                 info.rti_ifa = ifa;
1584                 info.rti_flags = flags |
1585                     (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED;
1586                 info.rti_info[RTAX_DST] = dst;
1587                 /* 
1588                  * doing this for compatibility reasons
1589                  */
1590                 if (cmd == RTM_ADD)
1591                         info.rti_info[RTAX_GATEWAY] =
1592                             (struct sockaddr *)&null_sdl;
1593                 else
1594                         info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1595                 info.rti_info[RTAX_NETMASK] = netmask;
1596                 error = rtrequest1_fib(cmd, &info, &rt, fibnum);
1597
1598                 if ((error == EEXIST) && (cmd == RTM_ADD)) {
1599                         /*
1600                          * Interface route addition failed.
1601                          * Atomically delete current prefix generating
1602                          * RTM_DELETE message, and retry adding
1603                          * interface prefix.
1604                          */
1605                         rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
1606                         RADIX_NODE_HEAD_LOCK(rnh);
1607
1608                         /* Delete old prefix */
1609                         info.rti_ifa = NULL;
1610                         info.rti_flags = RTF_RNH_LOCKED;
1611
1612                         error = rtrequest1_fib(RTM_DELETE, &info, NULL, fibnum);
1613                         if (error == 0) {
1614                                 info.rti_ifa = ifa;
1615                                 info.rti_flags = flags | RTF_RNH_LOCKED |
1616                                     (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED;
1617                                 error = rtrequest1_fib(cmd, &info, &rt, fibnum);
1618                         }
1619
1620                         RADIX_NODE_HEAD_UNLOCK(rnh);
1621                 }
1622
1623
1624                 if (error == 0 && rt != NULL) {
1625                         /*
1626                          * notify any listening routing agents of the change
1627                          */
1628                         RT_LOCK(rt);
1629 #ifdef RADIX_MPATH
1630                         /*
1631                          * in case address alias finds the first address
1632                          * e.g. ifconfig bge0 192.0.2.246/24
1633                          * e.g. ifconfig bge0 192.0.2.247/24
1634                          * the address set in the route is 192.0.2.246
1635                          * so we need to replace it with 192.0.2.247
1636                          */
1637                         if (memcmp(rt->rt_ifa->ifa_addr,
1638                             ifa->ifa_addr, ifa->ifa_addr->sa_len)) {
1639                                 ifa_free(rt->rt_ifa);
1640                                 ifa_ref(ifa);
1641                                 rt->rt_ifp = ifa->ifa_ifp;
1642                                 rt->rt_ifa = ifa;
1643                         }
1644 #endif
1645                         /* 
1646                          * doing this for compatibility reasons
1647                          */
1648                         if (cmd == RTM_ADD) {
1649                             ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type  =
1650                                 rt->rt_ifp->if_type;
1651                             ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1652                                 rt->rt_ifp->if_index;
1653                         }
1654                         RT_ADDREF(rt);
1655                         RT_UNLOCK(rt);
1656                         rt_newaddrmsg_fib(cmd, ifa, error, rt, fibnum);
1657                         RT_LOCK(rt);
1658                         RT_REMREF(rt);
1659                         if (cmd == RTM_DELETE) {
1660                                 /*
1661                                  * If we are deleting, and we found an entry,
1662                                  * then it's been removed from the tree..
1663                                  * now throw it away.
1664                                  */
1665                                 RTFREE_LOCKED(rt);
1666                         } else {
1667                                 if (cmd == RTM_ADD) {
1668                                         /*
1669                                          * We just wanted to add it..
1670                                          * we don't actually need a reference.
1671                                          */
1672                                         RT_REMREF(rt);
1673                                 }
1674                                 RT_UNLOCK(rt);
1675                         }
1676                         didwork = 1;
1677                 }
1678                 if (error)
1679                         a_failure = error;
1680         }
1681         if (cmd == RTM_DELETE) {
1682                 if (didwork) {
1683                         error = 0;
1684                 } else {
1685                         /* we only give an error if it wasn't in any table */
1686                         error = ((flags & RTF_HOST) ?
1687                             EHOSTUNREACH : ENETUNREACH);
1688                 }
1689         } else {
1690                 if (a_failure) {
1691                         /* return an error if any of them failed */
1692                         error = a_failure;
1693                 }
1694         }
1695         return (error);
1696 }
1697
1698 #ifndef BURN_BRIDGES
1699 /* special one for inet internal use. may not use. */
1700 int
1701 rtinit_fib(struct ifaddr *ifa, int cmd, int flags)
1702 {
1703         return (rtinit1(ifa, cmd, flags, RT_ALL_FIBS));
1704 }
1705 #endif
1706
1707 /*
1708  * Set up a routing table entry, normally
1709  * for an interface.
1710  */
1711 int
1712 rtinit(struct ifaddr *ifa, int cmd, int flags)
1713 {
1714         struct sockaddr *dst;
1715         int fib = RT_DEFAULT_FIB;
1716
1717         if (flags & RTF_HOST) {
1718                 dst = ifa->ifa_dstaddr;
1719         } else {
1720                 dst = ifa->ifa_addr;
1721         }
1722
1723         switch (dst->sa_family) {
1724         case AF_INET6:
1725         case AF_INET:
1726                 /* We do support multiple FIBs. */
1727                 fib = RT_ALL_FIBS;
1728                 break;
1729         }
1730         return (rtinit1(ifa, cmd, flags, fib));
1731 }
1732
1733 /*
1734  * Announce interface address arrival/withdraw
1735  * Returns 0 on success.
1736  */
1737 int
1738 rt_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1739 {
1740
1741         KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1742             ("unexpected cmd %d", cmd));
1743         
1744         KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
1745             ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
1746
1747         return (rtsock_addrmsg(cmd, ifa, fibnum));
1748 }
1749
1750 /*
1751  * Announce route addition/removal.
1752  * Users of this function MUST validate input data BEFORE calling.
1753  * However we have to be able to handle invalid data:
1754  * if some userland app sends us "invalid" route message (invalid mask,
1755  * no dst, wrong address families, etc...) we need to pass it back
1756  * to app (and any other rtsock consumers) with rtm_errno field set to
1757  * non-zero value.
1758  * Returns 0 on success.
1759  */
1760 int
1761 rt_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1762     int fibnum)
1763 {
1764
1765         KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1766             ("unexpected cmd %d", cmd));
1767         
1768         KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
1769             ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
1770
1771         KASSERT(rt_key(rt) != NULL, (":%s: rt_key must be supplied", __func__));
1772
1773         return (rtsock_routemsg(cmd, ifp, error, rt, fibnum));
1774 }
1775
1776 void
1777 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1778 {
1779
1780         rt_newaddrmsg_fib(cmd, ifa, error, rt, RT_ALL_FIBS);
1781 }
1782
1783 /*
1784  * This is called to generate messages from the routing socket
1785  * indicating a network interface has had addresses associated with it.
1786  */
1787 void
1788 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
1789     int fibnum)
1790 {
1791
1792         KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1793                 ("unexpected cmd %u", cmd));
1794         KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
1795             ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
1796
1797 #if defined(INET) || defined(INET6)
1798 #ifdef SCTP
1799         /*
1800          * notify the SCTP stack
1801          * this will only get called when an address is added/deleted
1802          * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1803          */
1804         sctp_addr_change(ifa, cmd);
1805 #endif /* SCTP */
1806 #endif
1807         if (cmd == RTM_ADD) {
1808                 rt_addrmsg(cmd, ifa, fibnum);
1809                 if (rt != NULL)
1810                         rt_routemsg(cmd, ifa->ifa_ifp, error, rt, fibnum);
1811         } else {
1812                 if (rt != NULL)
1813                         rt_routemsg(cmd, ifa->ifa_ifp, error, rt, fibnum);
1814                 rt_addrmsg(cmd, ifa, fibnum);
1815         }
1816 }
1817