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