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: releng/6.3/sys/net/route.c 173886 2007-11-24 19:45:58Z cvs2svn $
31 */
32
33 #include "opt_inet.h"
34 #include "opt_mrouting.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/socket.h>
41 #include <sys/domain.h>
42 #include <sys/kernel.h>
43
44 #include <net/if.h>
45 #include <net/route.h>
46
47 #include <netinet/in.h>
48 #include <netinet/ip_mroute.h>
49
50 #include <vm/uma.h>
51
52 static struct rtstat rtstat;
53 struct radix_node_head *rt_tables[AF_MAX+1];
54
55 static int rttrash; /* routes not in table but not freed */
56
57 static void rt_maskedcopy(struct sockaddr *,
58 struct sockaddr *, struct sockaddr *);
59 static void rtable_init(void **);
60
61 /* compare two sockaddr structures */
62 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
63
64 /*
65 * Convert a 'struct radix_node *' to a 'struct rtentry *'.
66 * The operation can be done safely (in this code) because a
67 * 'struct rtentry' starts with two 'struct radix_node''s, the first
68 * one representing leaf nodes in the routing tree, which is
69 * what the code in radix.c passes us as a 'struct radix_node'.
70 *
71 * But because there are a lot of assumptions in this conversion,
72 * do not cast explicitly, but always use the macro below.
73 */
74 #define RNTORT(p) ((struct rtentry *)(p))
75
76 static void
77 rtable_init(void **table)
78 {
79 struct domain *dom;
80 for (dom = domains; dom; dom = dom->dom_next)
81 if (dom->dom_rtattach)
82 dom->dom_rtattach(&table[dom->dom_family],
83 dom->dom_rtoffset);
84 }
85
86 static uma_zone_t rtzone; /* Routing table UMA zone. */
87
88 static void
89 route_init(void)
90 {
91 rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), NULL, NULL,
92 NULL, NULL, UMA_ALIGN_PTR, 0);
93 rn_init(); /* initialize all zeroes, all ones, mask table */
94 rtable_init((void **)rt_tables);
95 }
96
97 /*
98 * Packet routing routines.
99 */
100 void
101 rtalloc(struct route *ro)
102 {
103 rtalloc_ign(ro, 0UL);
104 }
105
106 void
107 rtalloc_ign(struct route *ro, u_long ignore)
108 {
109 struct rtentry *rt;
110
111 if ((rt = ro->ro_rt) != NULL) {
112 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
113 return;
114 RTFREE(rt);
115 ro->ro_rt = NULL;
116 }
117 ro->ro_rt = rtalloc1(&ro->ro_dst, 1, ignore);
118 if (ro->ro_rt)
119 RT_UNLOCK(ro->ro_rt);
120 }
121
122 /*
123 * Look up the route that matches the address given
124 * Or, at least try.. Create a cloned route if needed.
125 *
126 * The returned route, if any, is locked.
127 */
128 struct rtentry *
129 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
130 {
131 struct radix_node_head *rnh = rt_tables[dst->sa_family];
132 struct rtentry *rt;
133 struct radix_node *rn;
134 struct rtentry *newrt;
135 struct rt_addrinfo info;
136 u_long nflags;
137 int err = 0, msgtype = RTM_MISS;
138
139 newrt = NULL;
140 /*
141 * Look up the address in the table for that Address Family
142 */
143 if (rnh == NULL) {
144 rtstat.rts_unreach++;
145 goto miss2;
146 }
147 RADIX_NODE_HEAD_LOCK(rnh);
148 if ((rn = rnh->rnh_matchaddr(dst, rnh)) &&
149 (rn->rn_flags & RNF_ROOT) == 0) {
150 /*
151 * If we find it and it's not the root node, then
152 * get a refernce on the rtentry associated.
153 */
154 newrt = rt = RNTORT(rn);
155 nflags = rt->rt_flags & ~ignflags;
156 if (report && (nflags & RTF_CLONING)) {
157 /*
158 * We are apparently adding (report = 0 in delete).
159 * If it requires that it be cloned, do so.
160 * (This implies it wasn't a HOST route.)
161 */
162 err = rtrequest(RTM_RESOLVE, dst, NULL,
163 NULL, 0, &newrt);
164 if (err) {
165 /*
166 * If the cloning didn't succeed, maybe
167 * what we have will do. Return that.
168 */
169 newrt = rt; /* existing route */
170 RT_LOCK(newrt);
171 RT_ADDREF(newrt);
172 goto miss;
173 }
174 KASSERT(newrt, ("no route and no error"));
175 RT_LOCK(newrt);
176 if (newrt->rt_flags & RTF_XRESOLVE) {
177 /*
178 * If the new route specifies it be
179 * externally resolved, then go do that.
180 */
181 msgtype = RTM_RESOLVE;
182 goto miss;
183 }
184 /* Inform listeners of the new route. */
185 bzero(&info, sizeof(info));
186 info.rti_info[RTAX_DST] = rt_key(newrt);
187 info.rti_info[RTAX_NETMASK] = rt_mask(newrt);
188 info.rti_info[RTAX_GATEWAY] = newrt->rt_gateway;
189 if (newrt->rt_ifp != NULL) {
190 info.rti_info[RTAX_IFP] =
191 ifaddr_byindex(newrt->rt_ifp->if_index)->ifa_addr;
192 info.rti_info[RTAX_IFA] = newrt->rt_ifa->ifa_addr;
193 }
194 rt_missmsg(RTM_ADD, &info, newrt->rt_flags, 0);
195 } else {
196 KASSERT(rt == newrt, ("locking wrong route"));
197 RT_LOCK(newrt);
198 RT_ADDREF(newrt);
199 }
200 RADIX_NODE_HEAD_UNLOCK(rnh);
201 } else {
202 /*
203 * Either we hit the root or couldn't find any match,
204 * Which basically means
205 * "caint get there frm here"
206 */
207 rtstat.rts_unreach++;
208 miss:
209 RADIX_NODE_HEAD_UNLOCK(rnh);
210 miss2: if (report) {
211 /*
212 * If required, report the failure to the supervising
213 * Authorities.
214 * For a delete, this is not an error. (report == 0)
215 */
216 bzero(&info, sizeof(info));
217 info.rti_info[RTAX_DST] = dst;
218 rt_missmsg(msgtype, &info, 0, err);
219 }
220 }
221 if (newrt)
222 RT_LOCK_ASSERT(newrt);
223 return (newrt);
224 }
225
226 /*
227 * Remove a reference count from an rtentry.
228 * If the count gets low enough, take it out of the routing table
229 */
230 void
231 rtfree(struct rtentry *rt)
232 {
233 struct radix_node_head *rnh;
234
235 /* XXX the NULL checks are probably useless */
236 if (rt == NULL)
237 panic("rtfree: NULL rt");
238 rnh = rt_tables[rt_key(rt)->sa_family];
239 if (rnh == NULL)
240 panic("rtfree: NULL rnh");
241
242 RT_LOCK_ASSERT(rt);
243
244 /*
245 * decrement the reference count by one and if it reaches 0,
246 * and there is a close function defined, call the close function
247 */
248 RT_REMREF(rt);
249 if (rt->rt_refcnt > 0)
250 goto done;
251
252 /*
253 * On last reference give the "close method" a chance
254 * to cleanup private state. This also permits (for
255 * IPv4 and IPv6) a chance to decide if the routing table
256 * entry should be purged immediately or at a later time.
257 * When an immediate purge is to happen the close routine
258 * typically calls rtexpunge which clears the RTF_UP flag
259 * on the entry so that the code below reclaims the storage.
260 */
261 if (rt->rt_refcnt == 0 && rnh->rnh_close)
262 rnh->rnh_close((struct radix_node *)rt, rnh);
263
264 /*
265 * If we are no longer "up" (and ref == 0)
266 * then we can free the resources associated
267 * with the route.
268 */
269 if ((rt->rt_flags & RTF_UP) == 0) {
270 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
271 panic ("rtfree 2");
272 /*
273 * the rtentry must have been removed from the routing table
274 * so it is represented in rttrash.. remove that now.
275 */
276 rttrash--;
277 #ifdef DIAGNOSTIC
278 if (rt->rt_refcnt < 0) {
279 printf("rtfree: %p not freed (neg refs)\n", rt);
280 goto done;
281 }
282 #endif
283 /*
284 * release references on items we hold them on..
285 * e.g other routes and ifaddrs.
286 */
287 if (rt->rt_ifa)
288 IFAFREE(rt->rt_ifa);
289 rt->rt_parent = NULL; /* NB: no refcnt on parent */
290
291 /*
292 * The key is separatly alloc'd so free it (see rt_setgate()).
293 * This also frees the gateway, as they are always malloc'd
294 * together.
295 */
296 Free(rt_key(rt));
297
298 /*
299 * and the rtentry itself of course
300 */
301 RT_LOCK_DESTROY(rt);
302 uma_zfree(rtzone, rt);
303 return;
304 }
305 done:
306 RT_UNLOCK(rt);
307 }
308
309
310 /*
311 * Force a routing table entry to the specified
312 * destination to go through the given gateway.
313 * Normally called as a result of a routing redirect
314 * message from the network layer.
315 */
316 void
317 rtredirect(struct sockaddr *dst,
318 struct sockaddr *gateway,
319 struct sockaddr *netmask,
320 int flags,
321 struct sockaddr *src)
322 {
323 struct rtentry *rt;
324 int error = 0;
325 short *stat = NULL;
326 struct rt_addrinfo info;
327 struct ifaddr *ifa;
328
329 /* verify the gateway is directly reachable */
330 if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
331 error = ENETUNREACH;
332 goto out;
333 }
334 rt = rtalloc1(dst, 0, 0UL); /* NB: rt is locked */
335 /*
336 * If the redirect isn't from our current router for this dst,
337 * it's either old or wrong. If it redirects us to ourselves,
338 * we have a routing loop, perhaps as a result of an interface
339 * going down recently.
340 */
341 if (!(flags & RTF_DONE) && rt &&
342 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
343 error = EINVAL;
344 else if (ifa_ifwithaddr(gateway))
345 error = EHOSTUNREACH;
346 if (error)
347 goto done;
348 /*
349 * Create a new entry if we just got back a wildcard entry
350 * or the the lookup failed. This is necessary for hosts
351 * which use routing redirects generated by smart gateways
352 * to dynamically build the routing tables.
353 */
354 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
355 goto create;
356 /*
357 * Don't listen to the redirect if it's
358 * for a route to an interface.
359 */
360 if (rt->rt_flags & RTF_GATEWAY) {
361 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
362 /*
363 * Changing from route to net => route to host.
364 * Create new route, rather than smashing route to net.
365 */
366 create:
367 if (rt)
368 rtfree(rt);
369 flags |= RTF_GATEWAY | RTF_DYNAMIC;
370 bzero((caddr_t)&info, sizeof(info));
371 info.rti_info[RTAX_DST] = dst;
372 info.rti_info[RTAX_GATEWAY] = gateway;
373 info.rti_info[RTAX_NETMASK] = netmask;
374 info.rti_ifa = ifa;
375 info.rti_flags = flags;
376 rt = NULL;
377 error = rtrequest1(RTM_ADD, &info, &rt);
378 if (rt != NULL) {
379 RT_LOCK(rt);
380 flags = rt->rt_flags;
381 }
382 stat = &rtstat.rts_dynamic;
383 } else {
384 /*
385 * Smash the current notion of the gateway to
386 * this destination. Should check about netmask!!!
387 */
388 rt->rt_flags |= RTF_MODIFIED;
389 flags |= RTF_MODIFIED;
390 stat = &rtstat.rts_newgateway;
391 /*
392 * add the key and gateway (in one malloc'd chunk).
393 */
394 rt_setgate(rt, rt_key(rt), gateway);
395 }
396 } else
397 error = EHOSTUNREACH;
398 done:
399 if (rt)
400 rtfree(rt);
401 out:
402 if (error)
403 rtstat.rts_badredirect++;
404 else if (stat != NULL)
405 (*stat)++;
406 bzero((caddr_t)&info, sizeof(info));
407 info.rti_info[RTAX_DST] = dst;
408 info.rti_info[RTAX_GATEWAY] = gateway;
409 info.rti_info[RTAX_NETMASK] = netmask;
410 info.rti_info[RTAX_AUTHOR] = src;
411 rt_missmsg(RTM_REDIRECT, &info, flags, error);
412 }
413
414 /*
415 * Routing table ioctl interface.
416 */
417 int
418 rtioctl(u_long req, caddr_t data)
419 {
420
421 /*
422 * If more ioctl commands are added here, make sure the proper
423 * super-user checks are being performed because it is possible for
424 * prison-root to make it this far if raw sockets have been enabled
425 * in jails.
426 */
427 #ifdef INET
428 /* Multicast goop, grrr... */
429 return mrt_ioctl ? mrt_ioctl(req, data) : EOPNOTSUPP;
430 #else /* INET */
431 return ENXIO;
432 #endif /* INET */
433 }
434
435 struct ifaddr *
436 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
437 {
438 register struct ifaddr *ifa;
439
440 if ((flags & RTF_GATEWAY) == 0) {
441 /*
442 * If we are adding a route to an interface,
443 * and the interface is a pt to pt link
444 * we should search for the destination
445 * as our clue to the interface. Otherwise
446 * we can use the local address.
447 */
448 ifa = NULL;
449 if (flags & RTF_HOST)
450 ifa = ifa_ifwithdstaddr(dst);
451 if (ifa == NULL)
452 ifa = ifa_ifwithaddr(gateway);
453 } else {
454 /*
455 * If we are adding a route to a remote net
456 * or host, the gateway may still be on the
457 * other end of a pt to pt link.
458 */
459 ifa = ifa_ifwithdstaddr(gateway);
460 }
461 if (ifa == NULL)
462 ifa = ifa_ifwithnet(gateway);
463 if (ifa == NULL) {
464 struct rtentry *rt = rtalloc1(gateway, 0, 0UL);
465 if (rt == NULL)
466 return (NULL);
467 RT_REMREF(rt);
468 RT_UNLOCK(rt);
469 if ((ifa = rt->rt_ifa) == NULL)
470 return (NULL);
471 }
472 if (ifa->ifa_addr->sa_family != dst->sa_family) {
473 struct ifaddr *oifa = ifa;
474 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
475 if (ifa == NULL)
476 ifa = oifa;
477 }
478 return (ifa);
479 }
480
481 static walktree_f_t rt_fixdelete;
482 static walktree_f_t rt_fixchange;
483
484 struct rtfc_arg {
485 struct rtentry *rt0;
486 struct radix_node_head *rnh;
487 };
488
489 /*
490 * Do appropriate manipulations of a routing tree given
491 * all the bits of info needed
492 */
493 int
494 rtrequest(int req,
495 struct sockaddr *dst,
496 struct sockaddr *gateway,
497 struct sockaddr *netmask,
498 int flags,
499 struct rtentry **ret_nrt)
500 {
501 struct rt_addrinfo info;
502
503 bzero((caddr_t)&info, sizeof(info));
504 info.rti_flags = flags;
505 info.rti_info[RTAX_DST] = dst;
506 info.rti_info[RTAX_GATEWAY] = gateway;
507 info.rti_info[RTAX_NETMASK] = netmask;
508 return rtrequest1(req, &info, ret_nrt);
509 }
510
511 /*
512 * These (questionable) definitions of apparent local variables apply
513 * to the next two functions. XXXXXX!!!
514 */
515 #define dst info->rti_info[RTAX_DST]
516 #define gateway info->rti_info[RTAX_GATEWAY]
517 #define netmask info->rti_info[RTAX_NETMASK]
518 #define ifaaddr info->rti_info[RTAX_IFA]
519 #define ifpaddr info->rti_info[RTAX_IFP]
520 #define flags info->rti_flags
521
522 int
523 rt_getifa(struct rt_addrinfo *info)
524 {
525 struct ifaddr *ifa;
526 int error = 0;
527
528 /*
529 * ifp may be specified by sockaddr_dl
530 * when protocol address is ambiguous.
531 */
532 if (info->rti_ifp == NULL && ifpaddr != NULL &&
533 ifpaddr->sa_family == AF_LINK &&
534 (ifa = ifa_ifwithnet(ifpaddr)) != NULL)
535 info->rti_ifp = ifa->ifa_ifp;
536 if (info->rti_ifa == NULL && ifaaddr != NULL)
537 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
538 if (info->rti_ifa == NULL) {
539 struct sockaddr *sa;
540
541 sa = ifaaddr != NULL ? ifaaddr :
542 (gateway != NULL ? gateway : dst);
543 if (sa != NULL && info->rti_ifp != NULL)
544 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
545 else if (dst != NULL && gateway != NULL)
546 info->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
547 else if (sa != NULL)
548 info->rti_ifa = ifa_ifwithroute(flags, sa, sa);
549 }
550 if ((ifa = info->rti_ifa) != NULL) {
551 if (info->rti_ifp == NULL)
552 info->rti_ifp = ifa->ifa_ifp;
553 } else
554 error = ENETUNREACH;
555 return (error);
556 }
557
558 /*
559 * Expunges references to a route that's about to be reclaimed.
560 * The route must be locked.
561 */
562 int
563 rtexpunge(struct rtentry *rt)
564 {
565 struct radix_node *rn;
566 struct radix_node_head *rnh;
567 struct ifaddr *ifa;
568 int error = 0;
569
570 RT_LOCK_ASSERT(rt);
571 #if 0
572 /*
573 * We cannot assume anything about the reference count
574 * because protocols call us in many situations; often
575 * before unwinding references to the table entry.
576 */
577 KASSERT(rt->rt_refcnt <= 1, ("bogus refcnt %ld", rt->rt_refcnt));
578 #endif
579 /*
580 * Find the correct routing tree to use for this Address Family
581 */
582 rnh = rt_tables[rt_key(rt)->sa_family];
583 if (rnh == NULL)
584 return (EAFNOSUPPORT);
585
586 RADIX_NODE_HEAD_LOCK(rnh);
587
588 /*
589 * Remove the item from the tree; it should be there,
590 * but when callers invoke us blindly it may not (sigh).
591 */
592 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh);
593 if (rn == NULL) {
594 error = ESRCH;
595 goto bad;
596 }
597 KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0,
598 ("unexpected flags 0x%x", rn->rn_flags));
599 KASSERT(rt == RNTORT(rn),
600 ("lookup mismatch, rt %p rn %p", rt, rn));
601
602 rt->rt_flags &= ~RTF_UP;
603
604 /*
605 * Now search what's left of the subtree for any cloned
606 * routes which might have been formed from this node.
607 */
608 if ((rt->rt_flags & RTF_CLONING) && rt_mask(rt))
609 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
610 rt_fixdelete, rt);
611
612 /*
613 * Remove any external references we may have.
614 * This might result in another rtentry being freed if
615 * we held its last reference.
616 */
617 if (rt->rt_gwroute) {
618 RTFREE(rt->rt_gwroute);
619 rt->rt_gwroute = NULL;
620 }
621
622 /*
623 * Give the protocol a chance to keep things in sync.
624 */
625 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) {
626 struct rt_addrinfo info;
627
628 bzero((caddr_t)&info, sizeof(info));
629 info.rti_flags = rt->rt_flags;
630 info.rti_info[RTAX_DST] = rt_key(rt);
631 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
632 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
633 ifa->ifa_rtrequest(RTM_DELETE, rt, &info);
634 }
635
636 /*
637 * one more rtentry floating around that is not
638 * linked to the routing table.
639 */
640 rttrash++;
641 bad:
642 RADIX_NODE_HEAD_UNLOCK(rnh);
643 return (error);
644 }
645
646 int
647 rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
648 {
649 int error = 0;
650 register struct rtentry *rt;
651 register struct radix_node *rn;
652 register struct radix_node_head *rnh;
653 struct ifaddr *ifa;
654 struct sockaddr *ndst;
655 #define senderr(x) { error = x ; goto bad; }
656
657 /*
658 * Find the correct routing tree to use for this Address Family
659 */
660 rnh = rt_tables[dst->sa_family];
661 if (rnh == NULL)
662 return (EAFNOSUPPORT);
663 RADIX_NODE_HEAD_LOCK(rnh);
664 /*
665 * If we are adding a host route then we don't want to put
666 * a netmask in the tree, nor do we want to clone it.
667 */
668 if (flags & RTF_HOST) {
669 netmask = NULL;
670 flags &= ~RTF_CLONING;
671 }
672 switch (req) {
673 case RTM_DELETE:
674 /*
675 * Remove the item from the tree and return it.
676 * Complain if it is not there and do no more processing.
677 */
678 rn = rnh->rnh_deladdr(dst, netmask, rnh);
679 if (rn == NULL)
680 senderr(ESRCH);
681 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
682 panic ("rtrequest delete");
683 rt = RNTORT(rn);
684 RT_LOCK(rt);
685 RT_ADDREF(rt);
686 rt->rt_flags &= ~RTF_UP;
687
688 /*
689 * Now search what's left of the subtree for any cloned
690 * routes which might have been formed from this node.
691 */
692 if ((rt->rt_flags & RTF_CLONING) &&
693 rt_mask(rt)) {
694 rnh->rnh_walktree_from(rnh, dst, rt_mask(rt),
695 rt_fixdelete, rt);
696 }
697
698 /*
699 * Remove any external references we may have.
700 * This might result in another rtentry being freed if
701 * we held its last reference.
702 */
703 if (rt->rt_gwroute) {
704 RTFREE(rt->rt_gwroute);
705 rt->rt_gwroute = NULL;
706 }
707
708 /*
709 * give the protocol a chance to keep things in sync.
710 */
711 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
712 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
713
714 /*
715 * One more rtentry floating around that is not
716 * linked to the routing table. rttrash will be decremented
717 * when RTFREE(rt) is eventually called.
718 */
719 rttrash++;
720
721 /*
722 * If the caller wants it, then it can have it,
723 * but it's up to it to free the rtentry as we won't be
724 * doing it.
725 */
726 if (ret_nrt) {
727 *ret_nrt = rt;
728 RT_UNLOCK(rt);
729 } else
730 RTFREE_LOCKED(rt);
731 break;
732
733 case RTM_RESOLVE:
734 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
735 senderr(EINVAL);
736 ifa = rt->rt_ifa;
737 /* XXX locking? */
738 flags = rt->rt_flags &
739 ~(RTF_CLONING | RTF_STATIC);
740 flags |= RTF_WASCLONED;
741 gateway = rt->rt_gateway;
742 if ((netmask = rt->rt_genmask) == NULL)
743 flags |= RTF_HOST;
744 goto makeroute;
745
746 case RTM_ADD:
747 if ((flags & RTF_GATEWAY) && !gateway)
748 senderr(EINVAL);
749 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
750 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
751 senderr(EINVAL);
752
753 if (info->rti_ifa == NULL && (error = rt_getifa(info)))
754 senderr(error);
755 ifa = info->rti_ifa;
756
757 makeroute:
758 rt = uma_zalloc(rtzone, M_NOWAIT | M_ZERO);
759 if (rt == NULL)
760 senderr(ENOBUFS);
761 RT_LOCK_INIT(rt);
762 rt->rt_flags = RTF_UP | flags;
763 /*
764 * Add the gateway. Possibly re-malloc-ing the storage for it
765 * also add the rt_gwroute if possible.
766 */
767 RT_LOCK(rt);
768 if ((error = rt_setgate(rt, dst, gateway)) != 0) {
769 RT_LOCK_DESTROY(rt);
770 uma_zfree(rtzone, rt);
771 senderr(error);
772 }
773
774 /*
775 * point to the (possibly newly malloc'd) dest address.
776 */
777 ndst = (struct sockaddr *)rt_key(rt);
778
779 /*
780 * make sure it contains the value we want (masked if needed).
781 */
782 if (netmask) {
783 rt_maskedcopy(dst, ndst, netmask);
784 } else
785 bcopy(dst, ndst, dst->sa_len);
786
787 /*
788 * Note that we now have a reference to the ifa.
789 * This moved from below so that rnh->rnh_addaddr() can
790 * examine the ifa and ifa->ifa_ifp if it so desires.
791 */
792 IFAREF(ifa);
793 rt->rt_ifa = ifa;
794 rt->rt_ifp = ifa->ifa_ifp;
795
796 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
797 rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
798 if (rn == NULL) {
799 struct rtentry *rt2;
800 /*
801 * Uh-oh, we already have one of these in the tree.
802 * We do a special hack: if the route that's already
803 * there was generated by the cloning mechanism
804 * then we just blow it away and retry the insertion
805 * of the new one.
806 */
807 rt2 = rtalloc1(dst, 0, 0);
808 if (rt2 && rt2->rt_parent) {
809 rtexpunge(rt2);
810 RT_UNLOCK(rt2);
811 rn = rnh->rnh_addaddr(ndst, netmask,
812 rnh, rt->rt_nodes);
813 } else if (rt2) {
814 /* undo the extra ref we got */
815 RTFREE_LOCKED(rt2);
816 }
817 }
818
819 /*
820 * If it still failed to go into the tree,
821 * then un-make it (this should be a function)
822 */
823 if (rn == NULL) {
824 if (rt->rt_gwroute)
825 RTFREE(rt->rt_gwroute);
826 if (rt->rt_ifa)
827 IFAFREE(rt->rt_ifa);
828 Free(rt_key(rt));
829 RT_LOCK_DESTROY(rt);
830 uma_zfree(rtzone, rt);
831 senderr(EEXIST);
832 }
833
834 rt->rt_parent = NULL;
835
836 /*
837 * If we got here from RESOLVE, then we are cloning
838 * so clone the rest, and note that we
839 * are a clone (and increment the parent's references)
840 */
841 if (req == RTM_RESOLVE) {
842 KASSERT(ret_nrt && *ret_nrt,
843 ("no route to clone from"));
844 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
845 rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */
846 if ((*ret_nrt)->rt_flags & RTF_CLONING) {
847 /*
848 * NB: We do not bump the refcnt on the parent
849 * entry under the assumption that it will
850 * remain so long as we do. This is
851 * important when deleting the parent route
852 * as this operation requires traversing
853 * the tree to delete all clones and futzing
854 * with refcnts requires us to double-lock
855 * parent through this back reference.
856 */
857 rt->rt_parent = *ret_nrt;
858 }
859 }
860
861 /*
862 * if this protocol has something to add to this then
863 * allow it to do that as well.
864 */
865 if (ifa->ifa_rtrequest)
866 ifa->ifa_rtrequest(req, rt, info);
867
868 /*
869 * We repeat the same procedure from rt_setgate() here because
870 * it doesn't fire when we call it there because the node
871 * hasn't been added to the tree yet.
872 */
873 if (req == RTM_ADD &&
874 !(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) {
875 struct rtfc_arg arg;
876 arg.rnh = rnh;
877 arg.rt0 = rt;
878 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
879 rt_fixchange, &arg);
880 }
881
882 /*
883 * actually return a resultant rtentry and
884 * give the caller a single reference.
885 */
886 if (ret_nrt) {
887 *ret_nrt = rt;
888 RT_ADDREF(rt);
889 }
890 RT_UNLOCK(rt);
891 break;
892 default:
893 error = EOPNOTSUPP;
894 }
895 bad:
896 RADIX_NODE_HEAD_UNLOCK(rnh);
897 return (error);
898 #undef senderr
899 }
900
901 #undef dst
902 #undef gateway
903 #undef netmask
904 #undef ifaaddr
905 #undef ifpaddr
906 #undef flags
907
908 /*
909 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
910 * (i.e., the routes related to it by the operation of cloning). This
911 * routine is iterated over all potential former-child-routes by way of
912 * rnh->rnh_walktree_from() above, and those that actually are children of
913 * the late parent (passed in as VP here) are themselves deleted.
914 */
915 static int
916 rt_fixdelete(struct radix_node *rn, void *vp)
917 {
918 struct rtentry *rt = RNTORT(rn);
919 struct rtentry *rt0 = vp;
920
921 if (rt->rt_parent == rt0 &&
922 !(rt->rt_flags & (RTF_PINNED | RTF_CLONING))) {
923 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
924 rt->rt_flags, NULL);
925 }
926 return 0;
927 }
928
929 /*
930 * This routine is called from rt_setgate() to do the analogous thing for
931 * adds and changes. There is the added complication in this case of a
932 * middle insert; i.e., insertion of a new network route between an older
933 * network route and (cloned) host routes. For this reason, a simple check
934 * of rt->rt_parent is insufficient; each candidate route must be tested
935 * against the (mask, value) of the new route (passed as before in vp)
936 * to see if the new route matches it.
937 *
938 * XXX - it may be possible to do fixdelete() for changes and reserve this
939 * routine just for adds. I'm not sure why I thought it was necessary to do
940 * changes this way.
941 */
942
943 static int
944 rt_fixchange(struct radix_node *rn, void *vp)
945 {
946 struct rtentry *rt = RNTORT(rn);
947 struct rtfc_arg *ap = vp;
948 struct rtentry *rt0 = ap->rt0;
949 struct radix_node_head *rnh = ap->rnh;
950 u_char *xk1, *xm1, *xk2, *xmp;
951 int i, len, mlen;
952
953 /* make sure we have a parent, and route is not pinned or cloning */
954 if (!rt->rt_parent ||
955 (rt->rt_flags & (RTF_PINNED | RTF_CLONING)))
956 return 0;
957
958 if (rt->rt_parent == rt0) /* parent match */
959 goto delete_rt;
960 /*
961 * There probably is a function somewhere which does this...
962 * if not, there should be.
963 */
964 len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len);
965
966 xk1 = (u_char *)rt_key(rt0);
967 xm1 = (u_char *)rt_mask(rt0);
968 xk2 = (u_char *)rt_key(rt);
969
970 /* avoid applying a less specific route */
971 xmp = (u_char *)rt_mask(rt->rt_parent);
972 mlen = rt_key(rt->rt_parent)->sa_len;
973 if (mlen > rt_key(rt0)->sa_len) /* less specific route */
974 return 0;
975 for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++)
976 if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i])
977 return 0; /* less specific route */
978
979 for (i = rnh->rnh_treetop->rn_offset; i < len; i++)
980 if ((xk2[i] & xm1[i]) != xk1[i])
981 return 0; /* no match */
982
983 /*
984 * OK, this node is a clone, and matches the node currently being
985 * changed/added under the node's mask. So, get rid of it.
986 */
987 delete_rt:
988 return rtrequest(RTM_DELETE, rt_key(rt), NULL,
989 rt_mask(rt), rt->rt_flags, NULL);
990 }
991
992 int
993 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
994 {
995 /* XXX dst may be overwritten, can we move this to below */
996 struct radix_node_head *rnh = rt_tables[dst->sa_family];
997 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
998
999 RT_LOCK_ASSERT(rt);
1000
1001 /*
1002 * A host route with the destination equal to the gateway
1003 * will interfere with keeping LLINFO in the routing
1004 * table, so disallow it.
1005 */
1006 if (((rt->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) ==
1007 (RTF_HOST|RTF_GATEWAY)) &&
1008 dst->sa_len == gate->sa_len &&
1009 bcmp(dst, gate, dst->sa_len) == 0) {
1010 /*
1011 * The route might already exist if this is an RTM_CHANGE
1012 * or a routing redirect, so try to delete it.
1013 */
1014 if (rt_key(rt))
1015 rtexpunge(rt);
1016 return EADDRNOTAVAIL;
1017 }
1018
1019 /*
1020 * Cloning loop avoidance in case of bad configuration.
1021 */
1022 if (rt->rt_flags & RTF_GATEWAY) {
1023 struct rtentry *gwrt;
1024
1025 RT_UNLOCK(rt); /* XXX workaround LOR */
1026 gwrt = rtalloc1(gate, 1, 0);
1027 if (gwrt == rt) {
1028 RT_LOCK_ASSERT(rt);
1029 RT_REMREF(rt);
1030 return (EADDRINUSE); /* failure */
1031 }
1032 RT_LOCK(rt);
1033 /*
1034 * If there is already a gwroute, then drop it. If we
1035 * are asked to replace route with itself, then do
1036 * not leak its refcounter.
1037 */
1038 if (rt->rt_gwroute != NULL) {
1039 if (rt->rt_gwroute == gwrt) {
1040 RT_REMREF(rt->rt_gwroute);
1041 } else
1042 RTFREE(rt->rt_gwroute);
1043 }
1044
1045 if ((rt->rt_gwroute = gwrt) != NULL)
1046 RT_UNLOCK(rt->rt_gwroute);
1047 }
1048
1049 /*
1050 * Prepare to store the gateway in rt->rt_gateway.
1051 * Both dst and gateway are stored one after the other in the same
1052 * malloc'd chunk. If we have room, we can reuse the old buffer,
1053 * rt_gateway already points to the right place.
1054 * Otherwise, malloc a new block and update the 'dst' address.
1055 */
1056 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
1057 caddr_t new;
1058
1059 R_Malloc(new, caddr_t, dlen + glen);
1060 if (new == NULL)
1061 return ENOBUFS;
1062 /*
1063 * XXX note, we copy from *dst and not *rt_key(rt) because
1064 * rt_setgate() can be called to initialize a newly
1065 * allocated route entry, in which case rt_key(rt) == NULL
1066 * (and also rt->rt_gateway == NULL).
1067 * Free()/free() handle a NULL argument just fine.
1068 */
1069 bcopy(dst, new, dlen);
1070 Free(rt_key(rt)); /* free old block, if any */
1071 rt_key(rt) = (struct sockaddr *)new;
1072 rt->rt_gateway = (struct sockaddr *)(new + dlen);
1073 }
1074
1075 /*
1076 * Copy the new gateway value into the memory chunk.
1077 */
1078 bcopy(gate, rt->rt_gateway, glen);
1079
1080 /*
1081 * This isn't going to do anything useful for host routes, so
1082 * don't bother. Also make sure we have a reasonable mask
1083 * (we don't yet have one during adds).
1084 */
1085 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) {
1086 struct rtfc_arg arg;
1087
1088 arg.rnh = rnh;
1089 arg.rt0 = rt;
1090 RT_UNLOCK(rt); /* XXX workaround LOR */
1091 RADIX_NODE_HEAD_LOCK(rnh);
1092 RT_LOCK(rt);
1093 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
1094 rt_fixchange, &arg);
1095 RADIX_NODE_HEAD_UNLOCK(rnh);
1096 }
1097
1098 return 0;
1099 }
1100
1101 static void
1102 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
1103 {
1104 register u_char *cp1 = (u_char *)src;
1105 register u_char *cp2 = (u_char *)dst;
1106 register u_char *cp3 = (u_char *)netmask;
1107 u_char *cplim = cp2 + *cp3;
1108 u_char *cplim2 = cp2 + *cp1;
1109
1110 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1111 cp3 += 2;
1112 if (cplim > cplim2)
1113 cplim = cplim2;
1114 while (cp2 < cplim)
1115 *cp2++ = *cp1++ & *cp3++;
1116 if (cp2 < cplim2)
1117 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1118 }
1119
1120 /*
1121 * Set up a routing table entry, normally
1122 * for an interface.
1123 */
1124 int
1125 rtinit(struct ifaddr *ifa, int cmd, int flags)
1126 {
1127 struct sockaddr *dst;
1128 struct sockaddr *netmask;
1129 struct mbuf *m = NULL;
1130 struct rtentry *rt = NULL;
1131 struct rt_addrinfo info;
1132 int error;
1133
1134 if (flags & RTF_HOST) {
1135 dst = ifa->ifa_dstaddr;
1136 netmask = NULL;
1137 } else {
1138 dst = ifa->ifa_addr;
1139 netmask = ifa->ifa_netmask;
1140 }
1141 /*
1142 * If it's a delete, check that if it exists, it's on the correct
1143 * interface or we might scrub a route to another ifa which would
1144 * be confusing at best and possibly worse.
1145 */
1146 if (cmd == RTM_DELETE) {
1147 struct sockaddr *deldst;
1148 struct radix_node_head *rnh;
1149 struct radix_node *rn;
1150
1151 /*
1152 * It's a delete, so it should already exist..
1153 * If it's a net, mask off the host bits
1154 * (Assuming we have a mask)
1155 */
1156 if (netmask != NULL) {
1157 m = m_get(M_DONTWAIT, MT_SONAME);
1158 if (m == NULL)
1159 return(ENOBUFS);
1160 deldst = mtod(m, struct sockaddr *);
1161 rt_maskedcopy(dst, deldst, netmask);
1162 dst = deldst;
1163 }
1164 /*
1165 * Look up an rtentry that is in the routing tree and
1166 * contains the correct info.
1167 */
1168 if ((rnh = rt_tables[dst->sa_family]) == NULL)
1169 goto bad;
1170 RADIX_NODE_HEAD_LOCK(rnh);
1171 error = ((rn = rnh->rnh_lookup(dst, netmask, rnh)) == NULL ||
1172 (rn->rn_flags & RNF_ROOT) ||
1173 RNTORT(rn)->rt_ifa != ifa ||
1174 !sa_equal((struct sockaddr *)rn->rn_key, dst));
1175 RADIX_NODE_HEAD_UNLOCK(rnh);
1176 if (error) {
1177 bad:
1178 if (m)
1179 (void) m_free(m);
1180 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1181 }
1182 }
1183 /*
1184 * Do the actual request
1185 */
1186 bzero((caddr_t)&info, sizeof(info));
1187 info.rti_ifa = ifa;
1188 info.rti_flags = flags | ifa->ifa_flags;
1189 info.rti_info[RTAX_DST] = dst;
1190 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1191 info.rti_info[RTAX_NETMASK] = netmask;
1192 error = rtrequest1(cmd, &info, &rt);
1193 if (error == 0 && rt != NULL) {
1194 /*
1195 * notify any listening routing agents of the change
1196 */
1197 RT_LOCK(rt);
1198 rt_newaddrmsg(cmd, ifa, error, rt);
1199 if (cmd == RTM_DELETE) {
1200 /*
1201 * If we are deleting, and we found an entry, then
1202 * it's been removed from the tree.. now throw it away.
1203 */
1204 RTFREE_LOCKED(rt);
1205 } else {
1206 if (cmd == RTM_ADD) {
1207 /*
1208 * We just wanted to add it.. we don't actually
1209 * need a reference.
1210 */
1211 RT_REMREF(rt);
1212 }
1213 RT_UNLOCK(rt);
1214 }
1215 }
1216 if (m)
1217 (void) m_free(m);
1218 return (error);
1219 }
1220
1221 /*
1222 * rt_check() is invoked on each layer 2 output path, prior to
1223 * encapsulating outbound packets.
1224 *
1225 * The function is mostly used to find a routing entry for the gateway,
1226 * which in some protocol families could also point to the link-level
1227 * address for the gateway itself (the side effect of revalidating the
1228 * route to the destination is rather pointless at this stage, we did it
1229 * already a moment before in the pr_output() routine to locate the ifp
1230 * and gateway to use).
1231 *
1232 * When we remove the layer-3 to layer-2 mapping tables from the
1233 * routing table, this function can be removed.
1234 *
1235 * === On input ===
1236 * *dst is the address of the NEXT HOP (which coincides with the
1237 * final destination if directly reachable);
1238 * *lrt0 points to the cached route to the final destination;
1239 * *lrt is not meaningful;
1240 *
1241 * === Operation ===
1242 * If the route is marked down try to find a new route. If the route
1243 * to the gateway is gone, try to setup a new route. Otherwise,
1244 * if the route is marked for packets to be rejected, enforce that.
1245 *
1246 * === On return ===
1247 * *dst is unchanged;
1248 * *lrt0 points to the (possibly new) route to the final destination
1249 * *lrt points to the route to the next hop
1250 *
1251 * Their values are meaningful ONLY if no error is returned.
1252 */
1253 int
1254 rt_check(struct rtentry **lrt, struct rtentry **lrt0, struct sockaddr *dst)
1255 {
1256 #define senderr(x) { error = x ; goto bad; }
1257 struct rtentry *rt;
1258 struct rtentry *rt0;
1259 int error;
1260
1261 KASSERT(*lrt0 != NULL, ("rt_check"));
1262 rt = rt0 = *lrt0;
1263
1264 /* NB: the locking here is tortuous... */
1265 RT_LOCK(rt);
1266 if ((rt->rt_flags & RTF_UP) == 0) {
1267 RT_UNLOCK(rt);
1268 rt = rtalloc1(dst, 1, 0UL);
1269 if (rt != NULL) {
1270 RT_REMREF(rt);
1271 /* XXX what about if change? */
1272 } else
1273 senderr(EHOSTUNREACH);
1274 rt0 = rt;
1275 }
1276 /* XXX BSD/OS checks dst->sa_family != AF_NS */
1277 if (rt->rt_flags & RTF_GATEWAY) {
1278 if (rt->rt_gwroute == NULL)
1279 goto lookup;
1280 rt = rt->rt_gwroute;
1281 RT_LOCK(rt); /* NB: gwroute */
1282 if ((rt->rt_flags & RTF_UP) == 0) {
1283 rtfree(rt); /* unlock gwroute */
1284 rt = rt0;
1285 rt0->rt_gwroute = NULL;
1286 lookup:
1287 RT_UNLOCK(rt0);
1288 rt = rtalloc1(rt->rt_gateway, 1, 0UL);
1289 if (rt == rt0) {
1290 RT_REMREF(rt0);
1291 RT_UNLOCK(rt0);
1292 senderr(ENETUNREACH);
1293 }
1294 RT_LOCK(rt0);
1295 if (rt0->rt_gwroute != NULL)
1296 RTFREE(rt0->rt_gwroute);
1297 rt0->rt_gwroute = rt;
1298 if (rt == NULL) {
1299 RT_UNLOCK(rt0);
1300 senderr(EHOSTUNREACH);
1301 }
1302 }
1303 RT_UNLOCK(rt0);
1304 }
1305 /* XXX why are we inspecting rmx_expire? */
1306 error = (rt->rt_flags & RTF_REJECT) &&
1307 (rt->rt_rmx.rmx_expire == 0 ||
1308 time_second < rt->rt_rmx.rmx_expire);
1309 if (error) {
1310 RT_UNLOCK(rt);
1311 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
1312 }
1313
1314 *lrt = rt;
1315 *lrt0 = rt0;
1316 return (0);
1317 bad:
1318 /* NB: lrt and lrt0 should not be interpreted if error is non-zero */
1319 return (error);
1320 #undef senderr
1321 }
1322
1323 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1324 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);
Cache object: 6313c1375589b087abb7eb1678f726e7
|