2 * The mrouted program is covered by the license in the accompanying file
3 * named "LICENSE". Use of the mrouted program represents acceptance of
4 * the terms and conditions listed in that file.
6 * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
7 * Leland Stanford Junior University.
10 * route.c,v 3.8.4.41 1998/01/15 00:08:34 fenner Exp
12 * $FreeBSD: src/usr.sbin/mrouted/route.c,v 1.12 1999/08/28 01:17:08 peter Exp $
13 * $DragonFly: src/usr.sbin/mrouted/route.c,v 1.3 2003/11/03 19:31:38 eirikn Exp $
19 * This define statement saves a lot of space later
21 #define RT_ADDR (struct rtentry *)&routing_table
26 int routes_changed; /* 1=>some routes have changed */
27 int delay_change_reports; /* 1=>postpone change reports */
31 * The routing table is shared with prune.c , so must not be static.
33 struct rtentry *routing_table; /* pointer to list of route entries */
38 static struct rtentry *rtp; /* pointer to a route entry */
39 static struct rtentry *rt_end; /* pointer to last route entry */
40 unsigned int nroutes; /* current number of route entries */
45 static int init_children_and_leaves(struct rtentry *r,
46 vifi_t parent, int first);
47 static int find_route (u_int32 origin, u_int32 mask);
48 static void create_route(u_int32 origin, u_int32 mask);
49 static void discard_route(struct rtentry *prev_r);
50 static int compare_rts (const void *rt1, const void *rt2);
51 static int report_chunk (int, struct rtentry *start_rt, vifi_t vifi,
53 static void queue_blaster_report(vifi_t, u_int32, u_int32, char *,
55 static void process_blaster_report(void *);
58 #include <sys/types.h>
62 * Return pointer to a specific route entry. This must be a separate
63 * function from find_route() which modifies rtp.
66 snmp_find_route(src, mask)
67 register u_int32 src, mask;
69 register struct rtentry *rt;
71 for (rt = routing_table; rt; rt = rt->rt_next) {
72 if (src == rt->rt_origin && mask == rt->rt_originmask)
79 * Find next route entry > specification
82 next_route(rtpp, src, mask)
83 struct rtentry **rtpp;
87 struct rtentry *rt, *rbest = NULL;
89 /* Among all entries > spec, find "lowest" one in order */
90 for (rt = routing_table; rt; rt=rt->rt_next) {
91 if ((ntohl(rt->rt_origin) > ntohl(src)
92 || (ntohl(rt->rt_origin) == ntohl(src)
93 && ntohl(rt->rt_originmask) > ntohl(mask)))
94 && (!rbest || (ntohl(rt->rt_origin) < ntohl(rbest->rt_origin))
95 || (ntohl(rt->rt_origin) == ntohl(rbest->rt_origin)
96 && ntohl(rt->rt_originmask) < ntohl(rbest->rt_originmask))))
104 * Given a routing table entry, and a vifi, find the next vifi/entry
107 next_route_child(rtpp, src, mask, vifi)
108 struct rtentry **rtpp;
111 vifi_t *vifi; /* vif at which to start looking */
113 /* Get (S,M) entry */
114 if (!((*rtpp) = snmp_find_route(src,mask)))
115 if (!next_route(rtpp, src, mask))
118 /* Continue until we get one with a valid next vif */
120 for (; (*rtpp)->rt_children && *vifi<numvifs; (*vifi)++)
121 if (VIFM_ISSET(*vifi, (*rtpp)->rt_children))
124 } while( next_route(rtpp, (*rtpp)->rt_origin, (*rtpp)->rt_originmask) );
131 * Initialize the routing table and associated variables.
136 routing_table = NULL;
139 routes_changed = FALSE;
140 delay_change_reports = FALSE;
145 * Initialize the children bits for route 'r', along with the
146 * associated dominant and subordinate data structures.
147 * If first is set, initialize dominants, otherwise keep old
148 * dominants on non-parent interfaces.
149 * XXX Does this need a return value?
152 init_children_and_leaves(r, parent, first)
153 register struct rtentry *r;
154 register vifi_t parent;
157 register vifi_t vifi;
158 register struct uvif *v;
159 vifbitmap_t old_children;
160 nbrbitmap_t old_subords;
162 VIFM_COPY(r->rt_children, old_children);
163 NBRM_COPY(r->rt_subordinates, old_subords);
165 VIFM_CLRALL(r->rt_children);
167 for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
168 if (first || vifi == parent)
169 r->rt_dominants [vifi] = 0;
170 if (vifi == parent || uvifs[vifi].uv_flags & VIFF_NOFLOOD ||
171 AVOID_TRANSIT(vifi, r) || (!first && r->rt_dominants[vifi]))
172 NBRM_CLRMASK(r->rt_subordinates, uvifs[vifi].uv_nbrmap);
174 NBRM_SETMASK(r->rt_subordinates, uvifs[vifi].uv_nbrmap);
176 if (vifi != parent && !(v->uv_flags & (VIFF_DOWN|VIFF_DISABLED)) &&
177 !(!first && r->rt_dominants[vifi])) {
178 VIFM_SET(vifi, r->rt_children);
182 return (!VIFM_SAME(r->rt_children, old_children) ||
183 !NBRM_SAME(r->rt_subordinates, old_subords));
188 * A new vif has come up -- update the children bitmaps in all route
189 * entries to take that into account.
192 add_vif_to_routes(vifi)
193 register vifi_t vifi;
195 register struct rtentry *r;
196 register struct uvif *v;
199 for (r = routing_table; r != NULL; r = r->rt_next) {
200 if (r->rt_metric != UNREACHABLE &&
201 !VIFM_ISSET(vifi, r->rt_children)) {
202 VIFM_SET(vifi, r->rt_children);
203 r->rt_dominants [vifi] = 0;
204 /*XXX isn't uv_nbrmap going to be empty?*/
205 NBRM_CLRMASK(r->rt_subordinates, v->uv_nbrmap);
206 update_table_entry(r, r->rt_gateway);
213 * A vif has gone down -- expire all routes that have that vif as parent,
214 * and update the children bitmaps in all other route entries to take into
215 * account the failed vif.
218 delete_vif_from_routes(vifi)
219 register vifi_t vifi;
221 register struct rtentry *r;
223 for (r = routing_table; r != NULL; r = r->rt_next) {
224 if (r->rt_metric != UNREACHABLE) {
225 if (vifi == r->rt_parent) {
226 del_table_entry(r, 0, DEL_ALL_ROUTES);
227 r->rt_timer = ROUTE_EXPIRE_TIME;
228 r->rt_metric = UNREACHABLE;
229 r->rt_flags |= RTF_CHANGED;
230 routes_changed = TRUE;
232 else if (VIFM_ISSET(vifi, r->rt_children)) {
233 VIFM_CLR(vifi, r->rt_children);
234 NBRM_CLRMASK(r->rt_subordinates, uvifs[vifi].uv_nbrmap);
235 update_table_entry(r, r->rt_gateway);
238 r->rt_dominants[vifi] = 0;
246 * A new neighbor has come up. If we're flooding on the neighbor's
247 * vif, mark that neighbor as subordinate for all routes whose parent
251 add_neighbor_to_routes(vifi, index)
252 register vifi_t vifi;
255 register struct rtentry *r;
256 register struct uvif *v;
259 if (v->uv_flags & VIFF_NOFLOOD)
261 for (r = routing_table; r != NULL; r = r->rt_next) {
262 if (r->rt_metric != UNREACHABLE && r->rt_parent != vifi &&
263 !AVOID_TRANSIT(vifi, r)) {
264 NBRM_SET(index, r->rt_subordinates);
265 update_table_entry(r, r->rt_gateway);
272 * A neighbor has failed or become unreachable. If that neighbor was
273 * considered a dominant or subordinate router in any route entries,
274 * take appropriate action. Expire all routes this neighbor advertised
278 delete_neighbor_from_routes(addr, vifi, index)
279 register u_int32 addr;
280 register vifi_t vifi;
283 register struct rtentry *r;
284 register struct uvif *v;
287 for (r = routing_table; r != NULL; r = r->rt_next) {
288 if (r->rt_metric != UNREACHABLE) {
289 if (r->rt_parent == vifi && r->rt_gateway == addr) {
290 del_table_entry(r, 0, DEL_ALL_ROUTES);
291 r->rt_timer = ROUTE_EXPIRE_TIME;
292 r->rt_metric = UNREACHABLE;
293 r->rt_flags |= RTF_CHANGED;
294 routes_changed = TRUE;
295 } else if (r->rt_dominants[vifi] == addr) {
296 VIFM_SET(vifi, r->rt_children);
297 r->rt_dominants[vifi] = 0;
298 if ((uvifs[vifi].uv_flags & VIFF_NOFLOOD) ||
299 AVOID_TRANSIT(vifi, r))
300 NBRM_CLRMASK(r->rt_subordinates, uvifs[vifi].uv_nbrmap);
302 NBRM_SETMASK(r->rt_subordinates, uvifs[vifi].uv_nbrmap);
303 update_table_entry(r, r->rt_gateway);
304 } else if (NBRM_ISSET(index, r->rt_subordinates)) {
305 NBRM_CLR(index, r->rt_subordinates);
306 update_table_entry(r, r->rt_gateway);
314 * Prepare for a sequence of ordered route updates by initializing a pointer
315 * to the start of the routing table. The pointer is used to remember our
316 * position in the routing table in order to avoid searching from the
317 * beginning for each update; this relies on having the route reports in
318 * a single message be in the same order as the route entries in the routing
322 start_route_updates()
329 * Starting at the route entry following the one to which 'rtp' points,
330 * look for a route entry matching the specified origin and mask. If a
331 * match is found, return TRUE and leave 'rtp' pointing at the found entry.
332 * If no match is found, return FALSE and leave 'rtp' pointing to the route
333 * entry preceding the point at which the new origin should be inserted.
334 * This code is optimized for the normal case in which the first entry to
335 * be examined is the matching entry.
338 find_route(origin, mask)
339 register u_int32 origin, mask;
341 register struct rtentry *r;
345 if (origin == r->rt_origin && mask == r->rt_originmask) {
349 if (ntohl(mask) < ntohl(r->rt_originmask) ||
350 (mask == r->rt_originmask &&
351 ntohl(origin) < ntohl(r->rt_origin))) {
361 * Create a new routing table entry for the specified origin and link it into
362 * the routing table. The shared variable 'rtp' is assumed to point to the
363 * routing entry after which the new one should be inserted. It is left
364 * pointing to the new entry.
366 * Only the origin, originmask, originwidth and flags fields are initialized
367 * in the new route entry; the caller is responsible for filling in the the
371 create_route(origin, mask)
372 u_int32 origin, mask;
374 register struct rtentry *r;
376 if ((r = (struct rtentry *) malloc(sizeof(struct rtentry) +
377 (numvifs * sizeof(u_int32)))) == NULL) {
378 log(LOG_ERR, 0, "ran out of memory"); /* fatal */
380 r->rt_origin = origin;
381 r->rt_originmask = mask;
382 if (((char *)&mask)[3] != 0) r->rt_originwidth = 4;
383 else if (((char *)&mask)[2] != 0) r->rt_originwidth = 3;
384 else if (((char *)&mask)[1] != 0) r->rt_originwidth = 2;
385 else r->rt_originwidth = 1;
387 r->rt_dominants = (u_int32 *)(r + 1);
388 bzero(r->rt_dominants, numvifs * sizeof(u_int32));
390 VIFM_CLRALL(r->rt_children);
391 NBRM_CLRALL(r->rt_subordinates);
392 NBRM_CLRALL(r->rt_subordadv);
394 r->rt_next = rtp->rt_next;
397 if (r->rt_next != NULL)
398 (r->rt_next)->rt_prev = r;
407 * Discard the routing table entry following the one to which 'prev_r' points.
410 discard_route(prev_r)
411 register struct rtentry *prev_r;
413 register struct rtentry *r;
416 uvifs[r->rt_parent].uv_nroutes--;
417 /*???nbr???.al_nroutes--;*/
418 prev_r->rt_next = r->rt_next;
419 if (prev_r->rt_next != NULL)
420 (prev_r->rt_next)->rt_prev = prev_r;
429 * Process a route report for a single origin, creating or updating the
430 * corresponding routing table entry if necessary. 'src' is either the
431 * address of a neighboring router from which the report arrived, or zero
432 * to indicate a change of status of one of our own interfaces.
435 update_route(origin, mask, metric, src, vifi, n)
436 u_int32 origin, mask;
442 register struct rtentry *r;
446 * Compute an adjusted metric, taking into account the cost of the
447 * subnet or tunnel over which the report arrived, and normalizing
448 * all unreachable/poisoned metrics into a single value.
450 if (src != 0 && (metric < 1 || metric >= 2*UNREACHABLE)) {
452 "%s reports out-of-range metric %u for origin %s",
453 inet_fmt(src, s1), metric, inet_fmts(origin, mask, s2));
456 adj_metric = metric + uvifs[vifi].uv_metric;
457 if (adj_metric > UNREACHABLE) adj_metric = UNREACHABLE;
460 * Look up the reported origin in the routing table.
462 if (!find_route(origin, mask)) {
465 * Don't create a new entry if the report says it's unreachable,
466 * or if the reported origin and mask are invalid.
468 if (adj_metric == UNREACHABLE) {
471 if (src != 0 && !inet_valid_subnet(origin, mask)) {
473 "%s reports an invalid origin (%s) and/or mask (%08x)",
474 inet_fmt(src, s1), inet_fmt(origin, s2), ntohl(mask));
478 IF_DEBUG(DEBUG_RTDETAIL)
479 log(LOG_DEBUG, 0, "%s advertises new route %s",
480 inet_fmt(src, s1), inet_fmts(origin, mask, s2));
483 * OK, create the new routing entry. 'rtp' will be left pointing
486 create_route(origin, mask);
487 uvifs[vifi].uv_nroutes++;
490 rtp->rt_metric = UNREACHABLE; /* temporary; updated below */
494 * We now have a routing entry for the reported origin. Update it?
497 if (r->rt_metric == UNREACHABLE) {
499 * The routing entry is for a formerly-unreachable or new origin.
500 * If the report claims reachability, update the entry to use
501 * the reported route.
503 if (adj_metric == UNREACHABLE)
506 IF_DEBUG(DEBUG_RTDETAIL)
507 log(LOG_DEBUG, 0, "%s advertises %s with adj_metric %d (ours was %d)",
508 inet_fmt(src, s1), inet_fmts(origin, mask, s2),
509 adj_metric, r->rt_metric);
512 * Now "steal away" any sources that belong under this route
513 * by deleting any cache entries they might have created
514 * and allowing the kernel to re-request them.
516 * If we haven't performed final initialization yet and are
517 * just collecting the routing table, we can't have any
518 * sources so we don't perform this step.
525 init_children_and_leaves(r, vifi, 1);
528 r->rt_metric = adj_metric;
529 r->rt_flags |= RTF_CHANGED;
530 routes_changed = TRUE;
531 update_table_entry(r, r->rt_gateway);
533 else if (src == r->rt_gateway) {
535 * The report has come either from the interface directly-connected
536 * to the origin subnet (src and r->rt_gateway both equal zero) or
537 * from the gateway we have chosen as the best first-hop gateway back
538 * towards the origin (src and r->rt_gateway not equal zero). Reset
539 * the route timer and, if the reported metric has changed, update
540 * our entry accordingly.
544 IF_DEBUG(DEBUG_RTDETAIL)
545 log(LOG_DEBUG, 0, "%s (current parent) advertises %s with adj_metric %d (ours was %d)",
546 inet_fmt(src, s1), inet_fmts(origin, mask, s2),
547 adj_metric, r->rt_metric);
549 if (adj_metric == r->rt_metric)
552 if (adj_metric == UNREACHABLE) {
553 del_table_entry(r, 0, DEL_ALL_ROUTES);
554 r->rt_timer = ROUTE_EXPIRE_TIME;
556 r->rt_metric = adj_metric;
557 r->rt_flags |= RTF_CHANGED;
558 routes_changed = TRUE;
561 (r->rt_gateway != 0 &&
562 (adj_metric < r->rt_metric ||
563 (adj_metric == r->rt_metric &&
564 (ntohl(src) < ntohl(r->rt_gateway) ||
565 r->rt_timer >= ROUTE_SWITCH_TIME))))) {
567 * The report is for an origin we consider reachable; the report
568 * comes either from one of our own interfaces or from a gateway
569 * other than the one we have chosen as the best first-hop gateway
570 * back towards the origin. If the source of the update is one of
571 * our own interfaces, or if the origin is not a directly-connected
572 * subnet and the reported metric for that origin is better than
573 * what our routing entry says, update the entry to use the new
574 * gateway and metric. We also switch gateways if the reported
575 * metric is the same as the one in the route entry and the gateway
576 * associated with the route entry has not been heard from recently,
577 * or if the metric is the same but the reporting gateway has a lower
578 * IP address than the gateway associated with the route entry.
579 * Did you get all that?
583 old_gateway = r->rt_gateway;
584 old_parent = r->rt_parent;
588 IF_DEBUG(DEBUG_RTDETAIL)
589 log(LOG_DEBUG, 0, "%s (new parent) on vif %d advertises %s with adj_metric %d (old parent was %s on vif %d, metric %d)",
590 inet_fmt(src, s1), vifi, inet_fmts(origin, mask, s2),
591 adj_metric, inet_fmt(old_gateway, s3), old_parent,
594 if (old_parent != vifi) {
595 init_children_and_leaves(r, vifi, 0);
596 uvifs[old_parent].uv_nroutes--;
597 uvifs[vifi].uv_nroutes++;
599 if (old_gateway != src) {
600 update_table_entry(r, old_gateway);
601 /*???old_gateway???->al_nroutes--;*/
605 r->rt_metric = adj_metric;
606 r->rt_flags |= RTF_CHANGED;
607 routes_changed = TRUE;
609 else if (vifi != r->rt_parent) {
611 * The report came from a vif other than the route's parent vif.
612 * Update the children info, if necessary.
614 if (AVOID_TRANSIT(vifi, r)) {
616 * The route's parent is a vif from which we're not supposed
617 * to transit onto this vif. Simply ignore the update.
619 IF_DEBUG(DEBUG_RTDETAIL)
620 log(LOG_DEBUG, 0, "%s on vif %d advertises %s with metric %d (ignored due to NOTRANSIT)",
621 inet_fmt(src, s1), vifi, inet_fmts(origin, mask, s2),
623 } else if (VIFM_ISSET(vifi, r->rt_children)) {
625 * Vif is a child vif for this route.
627 if (metric < r->rt_metric ||
628 (metric == r->rt_metric &&
629 ntohl(src) < ntohl(uvifs[vifi].uv_lcl_addr))) {
631 * Neighbor has lower metric to origin (or has same metric
632 * and lower IP address) -- it becomes the dominant router,
633 * and vif is no longer a child for me.
635 VIFM_CLR(vifi, r->rt_children);
636 r->rt_dominants [vifi] = src;
638 * We don't necessarily want to forget about subordinateness
639 * so that we can become the dominant quickly if the current
642 NBRM_CLRMASK(r->rt_subordinates, uvifs[vifi].uv_nbrmap);
643 update_table_entry(r, r->rt_gateway);
644 IF_DEBUG(DEBUG_RTDETAIL)
645 log(LOG_DEBUG, 0, "%s on vif %d becomes dominant for %s with metric %d",
646 inet_fmt(src, s1), vifi, inet_fmts(origin, mask, s2),
649 else if (metric > UNREACHABLE) { /* "poisoned reverse" */
651 * Neighbor considers this vif to be on path to route's
652 * origin; record this neighbor as subordinate
654 if (!NBRM_ISSET(n->al_index, r->rt_subordinates)) {
655 IF_DEBUG(DEBUG_RTDETAIL)
656 log(LOG_DEBUG, 0, "%s on vif %d becomes subordinate for %s with poison-reverse metric %d",
657 inet_fmt(src, s1), vifi, inet_fmts(origin, mask, s2),
658 metric - UNREACHABLE);
659 NBRM_SET(n->al_index, r->rt_subordinates);
660 update_table_entry(r, r->rt_gateway);
662 IF_DEBUG(DEBUG_RTDETAIL)
663 log(LOG_DEBUG, 0, "%s on vif %d confirms subordinateness for %s with poison-reverse metric %d",
664 inet_fmt(src, s1), vifi, inet_fmts(origin, mask, s2),
665 metric - UNREACHABLE);
667 NBRM_SET(n->al_index, r->rt_subordadv);
669 else if (NBRM_ISSET(n->al_index, r->rt_subordinates)) {
671 * Current subordinate no longer considers this vif to be on
672 * path to route's origin; it is no longer a subordinate
675 IF_DEBUG(DEBUG_RTDETAIL)
676 log(LOG_DEBUG, 0, "%s on vif %d is no longer a subordinate for %s with metric %d",
677 inet_fmt(src, s1), vifi, inet_fmts(origin, mask, s2),
679 NBRM_CLR(n->al_index, r->rt_subordinates);
680 update_table_entry(r, r->rt_gateway);
684 else if (src == r->rt_dominants[vifi] &&
685 (metric > r->rt_metric ||
686 (metric == r->rt_metric &&
687 ntohl(src) > ntohl(uvifs[vifi].uv_lcl_addr)))) {
689 * Current dominant no longer has a lower metric to origin
690 * (or same metric and lower IP address); we adopt the vif
693 IF_DEBUG(DEBUG_RTDETAIL)
694 log(LOG_DEBUG, 0, "%s (current dominant) on vif %d is no longer dominant for %s with metric %d",
695 inet_fmt(src, s1), vifi, inet_fmts(origin, mask, s2),
697 VIFM_SET(vifi, r->rt_children);
698 r->rt_dominants[vifi] = 0;
699 if (uvifs[vifi].uv_flags & VIFF_NOFLOOD)
700 NBRM_CLRMASK(r->rt_subordinates, uvifs[vifi].uv_nbrmap);
702 NBRM_SETMASK(r->rt_subordinates, uvifs[vifi].uv_nbrmap);
703 if (metric > UNREACHABLE) {
704 NBRM_SET(n->al_index, r->rt_subordinates);
705 NBRM_SET(n->al_index, r->rt_subordadv);
707 update_table_entry(r, r->rt_gateway);
709 IF_DEBUG(DEBUG_RTDETAIL)
710 log(LOG_DEBUG, 0, "%s on vif %d advertises %s with metric %d (ignored)",
711 inet_fmt(src, s1), vifi, inet_fmts(origin, mask, s2),
719 * On every timer interrupt, advance the timer in each routing entry.
724 register struct rtentry *r;
725 register struct rtentry *prev_r;
726 extern u_long virtual_time; /* from main.c */
728 for (prev_r = RT_ADDR, r = routing_table;
730 prev_r = r, r = r->rt_next) {
732 if ((r->rt_timer += TIMER_INTERVAL) >= ROUTE_DISCARD_TIME) {
734 * Time to garbage-collect the route entry.
736 del_table_entry(r, 0, DEL_ALL_ROUTES);
737 discard_route(prev_r);
740 else if (r->rt_timer >= ROUTE_EXPIRE_TIME &&
741 r->rt_metric != UNREACHABLE) {
743 * Time to expire the route entry. If the gateway is zero,
744 * i.e., it is a route to a directly-connected subnet, just
745 * set the timer back to zero; such routes expire only when
746 * the interface to the subnet goes down.
748 if (r->rt_gateway == 0) {
752 del_table_entry(r, 0, DEL_ALL_ROUTES);
753 r->rt_metric = UNREACHABLE;
754 r->rt_flags |= RTF_CHANGED;
755 routes_changed = TRUE;
758 else if (virtual_time % (ROUTE_REPORT_INTERVAL * 2) == 0) {
760 * Time out subordinateness that hasn't been reported in
761 * the last 2 intervals.
763 if (!NBRM_SAME(r->rt_subordinates, r->rt_subordadv)) {
764 IF_DEBUG(DEBUG_ROUTE)
765 log(LOG_DEBUG, 0, "rt %s sub 0x%08x%08x subadv 0x%08x%08x metric %d",
767 r->rt_subordinates.hi, r->rt_subordinates.lo,
768 r->rt_subordadv.hi, r->rt_subordadv.lo, r->rt_metric);
769 NBRM_MASK(r->rt_subordinates, r->rt_subordadv);
770 update_table_entry(r, r->rt_gateway);
772 NBRM_CLRALL(r->rt_subordadv);
779 * Mark all routes as unreachable. This function is called only from
780 * hup() in preparation for informing all neighbors that we are going
781 * off the air. For consistency, we ought also to delete all reachable
782 * route entries from the kernel, but since we are about to exit we rely
783 * on the kernel to do its own cleanup -- no point in making all those
784 * expensive kernel calls now.
789 register struct rtentry *r;
791 for (r = routing_table; r != NULL; r = r->rt_next) {
792 r->rt_metric = UNREACHABLE;
793 r->rt_flags |= RTF_CHANGED;
794 routes_changed = TRUE;
800 * Delete all the routes in the routing table.
805 register struct rtentry *r;
815 * Process an incoming neighbor probe message.
818 accept_probe(src, dst, p, datalen, level)
826 static struct listaddr *unknowns = NULL;
828 if ((vifi = find_vif(src, dst)) == NO_VIF) {
829 struct listaddr *a, **prev;
830 struct listaddr *match = NULL;
831 time_t now = time(0);
833 for (prev = &unknowns, a = *prev; a; a = *prev) {
834 if (a->al_addr == src)
836 if (a->al_ctime + 2 * a->al_timer < now) {
837 /* We haven't heard from it in a long time */
845 match = *prev = (struct listaddr *)malloc(sizeof(struct listaddr));
846 match->al_next = NULL;
847 match->al_addr = src;
848 match->al_timer = OLD_NEIGHBOR_EXPIRE_TIME;
849 match->al_ctime = now - match->al_timer;
852 if (match->al_ctime + match->al_timer <= now) {
854 "ignoring probe from non-neighbor %s, check for misconfigured tunnel or routing on %s",
855 inet_fmt(src, s1), s1);
856 match->al_timer *= 2;
860 "ignoring probe from non-neighbor %s (%d seconds until next warning)", inet_fmt(src, s1), match->al_ctime + match->al_timer - now);
864 update_neighbor(vifi, src, DVMRP_PROBE, p, datalen, level);
875 compare_rts(rt1, rt2)
879 register struct newrt *r1 = (struct newrt *)rt1;
880 register struct newrt *r2 = (struct newrt *)rt2;
881 register u_int32 m1 = ntohl(r1->mask);
882 register u_int32 m2 = ntohl(r2->mask);
883 register u_int32 o1, o2;
890 /* masks are equal */
891 o1 = ntohl(r1->origin);
892 o2 = ntohl(r2->origin);
904 register struct uvif *v;
907 if (v->uv_blasterbuf)
908 free(v->uv_blasterbuf);
910 v->uv_blasterlen = 64*1024;
911 v->uv_blasterbuf = malloc(v->uv_blasterlen);
912 v->uv_blastercur = v->uv_blasterend = v->uv_blasterbuf;
913 if (v->uv_blastertimer)
914 timer_clearTimer(v->uv_blastertimer);
915 v->uv_blastertimer = 0;
926 * Queue a route report from a route-blaster.
927 * If the timer isn't running to process these reports,
931 queue_blaster_report(vifi, src, dst, p, datalen, level)
933 u_int32 src, dst, level;
935 register int datalen;
937 register struct blaster_hdr *bh;
938 register struct uvif *v;
939 int bblen = sizeof(*bh) + ((datalen + 3) & ~3);
942 if (v->uv_blasterend - v->uv_blasterbuf +
943 bblen > v->uv_blasterlen) {
944 int end = v->uv_blasterend - v->uv_blasterbuf;
945 int cur = v->uv_blastercur - v->uv_blasterbuf;
947 v->uv_blasterlen *= 2;
949 log(LOG_DEBUG, 0, "increasing blasterbuf to %d bytes",
951 v->uv_blasterbuf = realloc(v->uv_blasterbuf,
953 if (v->uv_blasterbuf == NULL) {
954 log(LOG_WARNING, ENOMEM, "turning off blaster on vif %d", vifi);
955 v->uv_blasterlen = 0;
956 v->uv_blasterend = v->uv_blastercur = NULL;
957 v->uv_flags &= ~VIFF_BLASTER;
960 v->uv_blasterend = v->uv_blasterbuf + end;
961 v->uv_blastercur = v->uv_blasterbuf + cur;
963 bh = (struct blaster_hdr *)v->uv_blasterend;
966 bh->bh_level = level;
967 bh->bh_datalen = datalen;
968 bcopy(p, (char *)(bh + 1), datalen);
969 v->uv_blasterend += bblen;
971 if (v->uv_blastertimer == 0) {
972 int *i = (int *)malloc(sizeof(int *));
975 log(LOG_ERR, 0, "out of memory");
979 v->uv_blastertimer = timer_setTimer(5,
980 process_blaster_report, i);
985 * Periodic process; process up to 5 of the routes in the route-blaster
986 * queue. If there are more routes remaining, reschedule myself to run
990 process_blaster_report(vifip)
993 vifi_t vifi = *(int *)vifip;
994 register struct uvif *v;
995 register struct blaster_hdr *bh;
998 IF_DEBUG(DEBUG_ROUTE)
999 log(LOG_DEBUG, 0, "processing vif %d blasted routes", vifi);
1001 for (i = 0; i < 5; i++) {
1002 if (v->uv_blastercur >= v->uv_blasterend)
1004 bh = (struct blaster_hdr *)v->uv_blastercur;
1005 v->uv_blastercur += sizeof(*bh) + ((bh->bh_datalen + 3) & ~3);
1006 accept_report(bh->bh_src, bh->bh_dst, (char *)(bh + 1),
1007 -bh->bh_datalen, bh->bh_level);
1010 if (v->uv_blastercur >= v->uv_blasterend) {
1011 v->uv_blastercur = v->uv_blasterbuf;
1012 v->uv_blasterend = v->uv_blasterbuf;
1013 v->uv_blastertimer = 0;
1015 IF_DEBUG(DEBUG_ROUTE)
1016 log(LOG_DEBUG, 0, "finish processing vif %d blaster", vifi);
1018 IF_DEBUG(DEBUG_ROUTE)
1019 log(LOG_DEBUG, 0, "more blasted routes to come on vif %d", vifi);
1020 v->uv_blastertimer = timer_setTimer(1,
1021 process_blaster_report, vifip);
1026 * Process an incoming route report message.
1027 * If the report arrived on a vif marked as a "blaster", then just
1028 * queue it and return; queue_blaster_report() will schedule it for
1029 * processing later. If datalen is negative, then this is actually
1030 * a queued report so actually process it instead of queueing it.
1033 accept_report(src, dst, p, datalen, level)
1034 u_int32 src, dst, level;
1036 register int datalen;
1039 register int width, i, nrt = 0;
1043 struct newrt rt[4096];
1044 struct listaddr *nbr;
1046 if ((vifi = find_vif(src, dst)) == NO_VIF) {
1048 "ignoring route report from non-neighbor %s", inet_fmt(src, s1));
1052 if (uvifs[vifi].uv_flags & VIFF_BLASTER)
1054 queue_blaster_report(vifi, src, dst, p, datalen, level);
1060 if (!(nbr = update_neighbor(vifi, src, DVMRP_REPORT, NULL, 0, level)))
1063 if (datalen > 2*4096) {
1065 "ignoring oversize (%d bytes) route report from %s",
1066 datalen, inet_fmt(src, s1));
1070 while (datalen > 0) { /* Loop through per-mask lists. */
1074 "received truncated route report from %s",
1078 ((u_char *)&mask)[0] = 0xff; width = 1;
1079 if ((((u_char *)&mask)[1] = *p++) != 0) width = 2;
1080 if ((((u_char *)&mask)[2] = *p++) != 0) width = 3;
1081 if ((((u_char *)&mask)[3] = *p++) != 0) width = 4;
1082 if (!inet_valid_mask(ntohl(mask))) {
1084 "%s reports bogus netmask 0x%08x (%s)",
1085 inet_fmt(src, s1), ntohl(mask), inet_fmt(mask, s2));
1090 do { /* Loop through (origin, metric) pairs */
1091 if (datalen < width + 1) {
1093 "received truncated route report from %s",
1098 for (i = 0; i < width; ++i)
1099 ((char *)&origin)[i] = *p++;
1101 datalen -= width + 1;
1102 rt[nrt].mask = mask;
1103 rt[nrt].origin = origin;
1104 rt[nrt].metric = (metric & 0x7f);
1106 } while (!(metric & 0x80));
1109 qsort((char*)rt, nrt, sizeof(rt[0]), compare_rts);
1110 start_route_updates();
1112 * If the last entry is default, change mask from 0xff000000 to 0
1114 if (rt[nrt-1].origin == 0)
1117 IF_DEBUG(DEBUG_ROUTE)
1118 log(LOG_DEBUG, 0, "Updating %d routes from %s to %s", nrt,
1119 inet_fmt(src, s1), inet_fmt(dst, s2));
1120 for (i = 0; i < nrt; ++i) {
1121 if (i != 0 && rt[i].origin == rt[i-1].origin &&
1122 rt[i].mask == rt[i-1].mask) {
1123 log(LOG_WARNING, 0, "%s reports duplicate route for %s",
1124 inet_fmt(src, s1), inet_fmts(rt[i].origin, rt[i].mask, s2));
1127 /* Only filter non-poisoned updates. */
1128 if (uvifs[vifi].uv_filter && rt[i].metric < UNREACHABLE) {
1129 struct vf_element *vfe;
1132 for (vfe = uvifs[vifi].uv_filter->vf_filter; vfe; vfe = vfe->vfe_next) {
1133 if (vfe->vfe_flags & VFEF_EXACT) {
1134 if ((vfe->vfe_addr == rt[i].origin) &&
1135 (vfe->vfe_mask == rt[i].mask)) {
1140 if ((rt[i].origin & vfe->vfe_mask) == vfe->vfe_addr) {
1146 if ((uvifs[vifi].uv_filter->vf_type == VFT_ACCEPT && match == 0) ||
1147 (uvifs[vifi].uv_filter->vf_type == VFT_DENY && match == 1)) {
1148 IF_DEBUG(DEBUG_ROUTE)
1149 log(LOG_DEBUG, 0, "%s skipped on vif %d because it %s %s",
1150 inet_fmts(rt[i].origin, rt[i].mask, s1),
1152 match ? "matches" : "doesn't match",
1153 match ? inet_fmts(vfe->vfe_addr, vfe->vfe_mask, s2) :
1156 rt[i].metric += vfe->vfe_addmetric;
1157 if (rt[i].metric > UNREACHABLE)
1159 rt[i].metric = UNREACHABLE;
1162 update_route(rt[i].origin, rt[i].mask, rt[i].metric,
1166 if (routes_changed && !delay_change_reports)
1167 report_to_all_neighbors(CHANGED_ROUTES);
1172 * Send a route report message to destination 'dst', via virtual interface
1173 * 'vifi'. 'which_routes' specifies ALL_ROUTES or CHANGED_ROUTES.
1176 report(which_routes, vifi, dst)
1181 register struct rtentry *r;
1185 while (r != RT_ADDR) {
1186 i = report_chunk(which_routes, r, vifi, dst);
1194 * Send a route report message to all neighboring routers.
1195 * 'which_routes' specifies ALL_ROUTES or CHANGED_ROUTES.
1198 report_to_all_neighbors(which_routes)
1201 register vifi_t vifi;
1202 register struct uvif *v;
1203 register struct rtentry *r;
1204 int routes_changed_before;
1207 * Remember the state of the global routes_changed flag before
1208 * generating the reports, and clear the flag.
1210 routes_changed_before = routes_changed;
1211 routes_changed = FALSE;
1214 for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
1215 if (!NBRM_ISEMPTY(v->uv_nbrmap)) {
1216 report(which_routes, vifi, v->uv_dst_addr);
1221 * If there were changed routes before we sent the reports AND
1222 * if no new changes occurred while sending the reports, clear
1223 * the change flags in the individual route entries. If changes
1224 * did occur while sending the reports, new reports will be
1225 * generated at the next timer interrupt.
1227 if (routes_changed_before && !routes_changed) {
1228 for (r = routing_table; r != NULL; r = r->rt_next) {
1229 r->rt_flags &= ~RTF_CHANGED;
1234 * Set a flag to inhibit further reports of changed routes until the
1235 * next timer interrupt. This is to alleviate update storms.
1237 delay_change_reports = TRUE;
1241 * Send a route report message to destination 'dst', via virtual interface
1242 * 'vifi'. 'which_routes' specifies ALL_ROUTES or CHANGED_ROUTES.
1245 report_chunk(which_routes, start_rt, vifi, dst)
1247 register struct rtentry *start_rt;
1251 register struct rtentry *r;
1254 register int nrt = 0;
1255 struct uvif *v = &uvifs[vifi];
1260 int admetric = v->uv_admetric;
1263 src = v->uv_lcl_addr;
1264 p = send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN;
1266 for (r = start_rt; r != RT_ADDR; r = r->rt_prev) {
1267 if (which_routes == CHANGED_ROUTES && !(r->rt_flags & RTF_CHANGED)) {
1273 * Do not poison-reverse a route for a directly-connected
1274 * subnetwork on that subnetwork. This can cause loops when
1275 * some router on the subnetwork is misconfigured.
1277 if (r->rt_gateway == 0 && r->rt_parent == vifi) {
1282 if (v->uv_filter && v->uv_filter->vf_flags & VFF_BIDIR) {
1283 struct vf_element *vfe;
1286 for (vfe = v->uv_filter->vf_filter; vfe; vfe = vfe->vfe_next) {
1287 if (vfe->vfe_flags & VFEF_EXACT) {
1288 if ((vfe->vfe_addr == r->rt_origin) &&
1289 (vfe->vfe_mask == r->rt_originmask)) {
1294 if ((r->rt_origin & vfe->vfe_mask) == vfe->vfe_addr) {
1300 if ((v->uv_filter->vf_type == VFT_ACCEPT && match == 0) ||
1301 (v->uv_filter->vf_type == VFT_DENY && match == 1)) {
1302 IF_DEBUG(DEBUG_ROUTE)
1303 log(LOG_DEBUG, 0, "%s not reported on vif %d because it %s %s",
1304 RT_FMT(r, s1), vifi,
1305 match ? "matches" : "doesn't match",
1306 match ? inet_fmts(vfe->vfe_addr, vfe->vfe_mask, s2) :
1314 * If there is no room for this route in the current message,
1315 * send it & return how many routes we sent.
1317 if (datalen + ((r->rt_originmask == mask) ?
1319 (r->rt_originwidth + 4)) > MAX_DVMRP_DATA_LEN) {
1321 send_on_vif(v, 0, DVMRP_REPORT, datalen);
1325 if (r->rt_originmask != mask || datalen == 0) {
1326 mask = r->rt_originmask;
1327 width = r->rt_originwidth;
1328 if (datalen != 0) *(p-1) |= 0x80;
1329 *p++ = ((char *)&mask)[1];
1330 *p++ = ((char *)&mask)[2];
1331 *p++ = ((char *)&mask)[3];
1334 for (i = 0; i < width; ++i)
1335 *p++ = ((char *)&(r->rt_origin))[i];
1337 metric = r->rt_metric + admetric;
1338 if (metric > UNREACHABLE)
1339 metric = UNREACHABLE;
1340 if (r->rt_parent != vifi && AVOID_TRANSIT(vifi, r))
1341 metric = UNREACHABLE;
1342 *p++ = (r->rt_parent == vifi && metric != UNREACHABLE) ?
1343 (char)(metric + UNREACHABLE) : /* "poisoned reverse" */
1346 datalen += width + 1;
1350 send_on_vif(v, 0, DVMRP_REPORT, datalen);
1356 * send the next chunk of our routing table to all neighbors.
1357 * return the length of the smallest chunk we sent out.
1362 register vifi_t vifi;
1363 register struct uvif *v;
1364 register struct rtentry *sr;
1365 register int i, n = 0, min = 20000;
1366 static int start_rt;
1372 * find this round's starting route.
1374 for (sr = rt_end, i = start_rt; --i >= 0; ) {
1381 * send one chunk of routes starting at this round's start to
1382 * all our neighbors.
1384 for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
1385 if (!NBRM_ISEMPTY(v->uv_nbrmap)) {
1386 n = report_chunk(ALL_ROUTES, sr, vifi, v->uv_dst_addr);
1392 min = 0; /* Neighborless router didn't send any routes */
1395 IF_DEBUG(DEBUG_ROUTE)
1396 log(LOG_INFO, 0, "update %d starting at %d of %d",
1397 n, (nroutes - start_rt), nroutes);
1399 start_rt = (start_rt + n) % nroutes;
1405 * Print the contents of the routing table on file 'fp'.
1411 register struct rtentry *r;
1416 "Multicast Routing Table (%u %s)\n%s\n",
1417 nroutes, (nroutes == 1) ? "entry" : "entries",
1418 " Origin-Subnet From-Gateway Metric Tmr Fl In-Vif Out-Vifs");
1420 for (r = routing_table; r != NULL; r = r->rt_next) {
1422 fprintf(fp, " %-18s %-15s ",
1423 inet_fmts(r->rt_origin, r->rt_originmask, s1),
1424 (r->rt_gateway == 0) ? "" : inet_fmt(r->rt_gateway, s2));
1426 fprintf(fp, (r->rt_metric == UNREACHABLE) ? " NR " : "%4u ",
1429 fprintf(fp, " %3u %c%c %3u ", r->rt_timer,
1430 (r->rt_flags & RTF_CHANGED) ? 'C' : '.',
1431 (r->rt_flags & RTF_HOLDDOWN) ? 'H' : '.',
1434 for (i = 0; i < numvifs; ++i) {
1438 if (VIFM_ISSET(i, r->rt_children)) {
1439 if ((uvifs[i].uv_flags & VIFF_TUNNEL) &&
1440 !NBRM_ISSETMASK(uvifs[i].uv_nbrmap, r->rt_subordinates))
1441 /* Don't print out parenthood of a leaf tunnel. */
1443 fprintf(fp, " %u", i);
1444 if (!NBRM_ISSETMASK(uvifs[i].uv_nbrmap, r->rt_subordinates))
1446 for (n = uvifs[i].uv_neighbors; n; n = n->al_next) {
1447 if (NBRM_ISSET(n->al_index, r->rt_subordinates)) {
1448 fprintf(fp, "%c%d", l, n->al_index);
1462 determine_route(src)
1467 for (rt = routing_table; rt != NULL; rt = rt->rt_next) {
1468 if (rt->rt_origin == (src & rt->rt_originmask) &&
1469 rt->rt_metric != UNREACHABLE)