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