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