Home | History | Annotate | Line # | Download | only in mrouted
route.c revision 1.7
      1  1.7      wiz /*	$NetBSD: route.c,v 1.7 2002/07/14 16:30:42 wiz Exp $	*/
      2  1.4  thorpej 
      3  1.1   brezak /*
      4  1.1   brezak  * The mrouted program is covered by the license in the accompanying file
      5  1.1   brezak  * named "LICENSE".  Use of the mrouted program represents acceptance of
      6  1.1   brezak  * the terms and conditions listed in that file.
      7  1.1   brezak  *
      8  1.1   brezak  * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
      9  1.1   brezak  * Leland Stanford Junior University.
     10  1.1   brezak  */
     11  1.1   brezak 
     12  1.1   brezak 
     13  1.1   brezak #include "defs.h"
     14  1.1   brezak 
     15  1.1   brezak 
     16  1.1   brezak /*
     17  1.3  mycroft  * This define statement saves a lot of space later
     18  1.3  mycroft  */
     19  1.3  mycroft #define RT_ADDR	(struct rtentry *)&routing_table
     20  1.3  mycroft 
     21  1.3  mycroft /*
     22  1.1   brezak  * Exported variables.
     23  1.1   brezak  */
     24  1.1   brezak int routes_changed;			/* 1=>some routes have changed */
     25  1.1   brezak int delay_change_reports;		/* 1=>postpone change reports  */
     26  1.1   brezak 
     27  1.1   brezak 
     28  1.1   brezak /*
     29  1.3  mycroft  * The routing table is shared with prune.c , so must not be static.
     30  1.3  mycroft  */
     31  1.3  mycroft struct rtentry *routing_table;		/* pointer to list of route entries */
     32  1.3  mycroft 
     33  1.3  mycroft /*
     34  1.1   brezak  * Private variables.
     35  1.1   brezak  */
     36  1.1   brezak static struct rtentry *rtp;		/* pointer to a route entry         */
     37  1.3  mycroft static struct rtentry *rt_end;		/* pointer to last route entry      */
     38  1.3  mycroft unsigned int nroutes;			/* current number of route entries  */
     39  1.1   brezak 
     40  1.1   brezak /*
     41  1.5  mycroft  * Private functions.
     42  1.5  mycroft  */
     43  1.7      wiz static int init_children_and_leaves(struct rtentry *r, vifi_t parent);
     44  1.7      wiz static int find_route(u_int32_t origin, u_int32_t mask);
     45  1.7      wiz static void create_route(u_int32_t origin, u_int32_t mask);
     46  1.7      wiz static void discard_route(struct rtentry *prev_r);
     47  1.7      wiz static int compare_rts(const void *rt1, const void *rt2);
     48  1.7      wiz static int report_chunk(struct rtentry *start_rt, vifi_t vifi, u_int32_t dst);
     49  1.5  mycroft 
     50  1.5  mycroft /*
     51  1.1   brezak  * Initialize the routing table and associated variables.
     52  1.1   brezak  */
     53  1.3  mycroft void
     54  1.7      wiz init_routes(void)
     55  1.1   brezak {
     56  1.1   brezak     routing_table        = NULL;
     57  1.5  mycroft     rt_end		 = RT_ADDR;
     58  1.1   brezak     nroutes		 = 0;
     59  1.1   brezak     routes_changed       = FALSE;
     60  1.1   brezak     delay_change_reports = FALSE;
     61  1.1   brezak }
     62  1.1   brezak 
     63  1.1   brezak 
     64  1.1   brezak /*
     65  1.1   brezak  * Initialize the children and leaf bits for route 'r', along with the
     66  1.1   brezak  * associated dominant, subordinate, and leaf timing data structures.
     67  1.1   brezak  * Return TRUE if this changes the value of either the children or
     68  1.1   brezak  * leaf bitmaps for 'r'.
     69  1.1   brezak  */
     70  1.3  mycroft static int
     71  1.7      wiz init_children_and_leaves(struct rtentry *r, vifi_t parent)
     72  1.1   brezak {
     73  1.7      wiz     vifi_t vifi;
     74  1.7      wiz     struct uvif *v;
     75  1.1   brezak     vifbitmap_t old_children, old_leaves;
     76  1.1   brezak 
     77  1.1   brezak     VIFM_COPY(r->rt_children, old_children);
     78  1.1   brezak     VIFM_COPY(r->rt_leaves,   old_leaves  );
     79  1.1   brezak 
     80  1.1   brezak     VIFM_CLRALL(r->rt_children);
     81  1.1   brezak     VIFM_CLRALL(r->rt_leaves);
     82  1.1   brezak     r->rt_flags &= ~RTF_LEAF_TIMING;
     83  1.1   brezak 
     84  1.1   brezak     for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
     85  1.1   brezak 	r->rt_dominants   [vifi] = 0;
     86  1.1   brezak 	r->rt_subordinates[vifi] = 0;
     87  1.1   brezak 
     88  1.1   brezak 	if (vifi != parent && !(v->uv_flags & (VIFF_DOWN|VIFF_DISABLED))) {
     89  1.1   brezak 	    VIFM_SET(vifi, r->rt_children);
     90  1.1   brezak 	    if (v->uv_neighbors == NULL) {
     91  1.1   brezak 		VIFM_SET(vifi, r->rt_leaves);
     92  1.1   brezak 		r->rt_leaf_timers[vifi] = 0;
     93  1.1   brezak 	    }
     94  1.1   brezak 	    else {
     95  1.1   brezak 		r->rt_leaf_timers[vifi] = LEAF_CONFIRMATION_TIME;
     96  1.1   brezak 		r->rt_flags |= RTF_LEAF_TIMING;
     97  1.1   brezak 	    }
     98  1.1   brezak 	}
     99  1.1   brezak 	else {
    100  1.1   brezak 	    r->rt_leaf_timers[vifi] = 0;
    101  1.1   brezak 	}
    102  1.1   brezak     }
    103  1.1   brezak 
    104  1.1   brezak     return (!VIFM_SAME(r->rt_children, old_children) ||
    105  1.1   brezak 	    !VIFM_SAME(r->rt_leaves,   old_leaves));
    106  1.1   brezak }
    107  1.1   brezak 
    108  1.1   brezak 
    109  1.1   brezak /*
    110  1.1   brezak  * A new vif has come up -- update the children and leaf bitmaps in all route
    111  1.1   brezak  * entries to take that into account.
    112  1.1   brezak  */
    113  1.3  mycroft void
    114  1.7      wiz add_vif_to_routes(vifi_t vifi)
    115  1.1   brezak {
    116  1.7      wiz     struct rtentry *r;
    117  1.7      wiz     struct uvif *v;
    118  1.1   brezak 
    119  1.1   brezak     v = &uvifs[vifi];
    120  1.1   brezak     for (r = routing_table; r != NULL; r = r->rt_next) {
    121  1.1   brezak 	if (r->rt_metric != UNREACHABLE &&
    122  1.1   brezak 	    !VIFM_ISSET(vifi, r->rt_children)) {
    123  1.1   brezak 	    VIFM_SET(vifi, r->rt_children);
    124  1.1   brezak 	    r->rt_dominants   [vifi] = 0;
    125  1.1   brezak 	    r->rt_subordinates[vifi] = 0;
    126  1.1   brezak 	    if (v->uv_neighbors == NULL) {
    127  1.1   brezak 		VIFM_SET(vifi, r->rt_leaves);
    128  1.1   brezak 		r->rt_leaf_timers[vifi] = 0;
    129  1.1   brezak 	    }
    130  1.1   brezak 	    else {
    131  1.1   brezak 		VIFM_CLR(vifi, r->rt_leaves);
    132  1.1   brezak 		r->rt_leaf_timers[vifi] = LEAF_CONFIRMATION_TIME;
    133  1.1   brezak 		r->rt_flags |= RTF_LEAF_TIMING;
    134  1.1   brezak 	    }
    135  1.3  mycroft 	    update_table_entry(r);
    136  1.1   brezak 	}
    137  1.1   brezak     }
    138  1.1   brezak }
    139  1.1   brezak 
    140  1.1   brezak 
    141  1.1   brezak /*
    142  1.1   brezak  * A vif has gone down -- expire all routes that have that vif as parent,
    143  1.1   brezak  * and update the children bitmaps in all other route entries to take into
    144  1.1   brezak  * account the failed vif.
    145  1.1   brezak  */
    146  1.3  mycroft void
    147  1.7      wiz delete_vif_from_routes(vifi_t vifi)
    148  1.1   brezak {
    149  1.7      wiz     struct rtentry *r;
    150  1.1   brezak 
    151  1.1   brezak     for (r = routing_table; r != NULL; r = r->rt_next) {
    152  1.1   brezak 	if (r->rt_metric != UNREACHABLE) {
    153  1.1   brezak 	    if (vifi == r->rt_parent) {
    154  1.3  mycroft 		del_table_entry(r, 0, DEL_ALL_ROUTES);
    155  1.1   brezak 		r->rt_timer    = ROUTE_EXPIRE_TIME;
    156  1.1   brezak 		r->rt_metric   = UNREACHABLE;
    157  1.1   brezak 		r->rt_flags   |= RTF_CHANGED;
    158  1.1   brezak 		routes_changed = TRUE;
    159  1.1   brezak 	    }
    160  1.1   brezak 	    else if (VIFM_ISSET(vifi, r->rt_children)) {
    161  1.1   brezak 		VIFM_CLR(vifi, r->rt_children);
    162  1.1   brezak 		VIFM_CLR(vifi, r->rt_leaves);
    163  1.1   brezak 		r->rt_subordinates[vifi] = 0;
    164  1.1   brezak 		r->rt_leaf_timers [vifi] = 0;
    165  1.3  mycroft 		update_table_entry(r);
    166  1.1   brezak 	    }
    167  1.1   brezak 	    else {
    168  1.1   brezak 		r->rt_dominants[vifi] = 0;
    169  1.1   brezak 	    }
    170  1.1   brezak 	}
    171  1.1   brezak     }
    172  1.1   brezak }
    173  1.1   brezak 
    174  1.1   brezak 
    175  1.1   brezak /*
    176  1.1   brezak  * A neighbor has failed or become unreachable.  If that neighbor was
    177  1.1   brezak  * considered a dominant or subordinate router in any route entries,
    178  1.1   brezak  * take appropriate action.
    179  1.1   brezak  */
    180  1.3  mycroft void
    181  1.7      wiz delete_neighbor_from_routes(u_int32_t addr, vifi_t vifi)
    182  1.1   brezak {
    183  1.7      wiz     struct rtentry *r;
    184  1.7      wiz     struct uvif *v;
    185  1.1   brezak 
    186  1.1   brezak     v = &uvifs[vifi];
    187  1.1   brezak     for (r = routing_table; r != NULL; r = r->rt_next) {
    188  1.1   brezak 	if (r->rt_metric != UNREACHABLE) {
    189  1.1   brezak 	    if (r->rt_dominants[vifi] == addr) {
    190  1.1   brezak 		VIFM_SET(vifi, r->rt_children);
    191  1.1   brezak 		r->rt_dominants   [vifi] = 0;
    192  1.1   brezak 		r->rt_subordinates[vifi] = 0;
    193  1.1   brezak 		if (v->uv_neighbors == NULL) {
    194  1.1   brezak 		    VIFM_SET(vifi, r->rt_leaves);
    195  1.1   brezak 		    r->rt_leaf_timers[vifi] = 0;
    196  1.1   brezak 		}
    197  1.1   brezak 		else {
    198  1.1   brezak 		    VIFM_CLR(vifi, r->rt_leaves);
    199  1.1   brezak 		    r->rt_leaf_timers[vifi] = LEAF_CONFIRMATION_TIME;
    200  1.1   brezak 		    r->rt_flags |= RTF_LEAF_TIMING;
    201  1.1   brezak 		}
    202  1.3  mycroft 		update_table_entry(r);
    203  1.1   brezak 	    }
    204  1.1   brezak 	    else if (r->rt_subordinates[vifi] == addr) {
    205  1.1   brezak 		r->rt_subordinates[vifi] = 0;
    206  1.1   brezak 		if (v->uv_neighbors == NULL) {
    207  1.1   brezak 		    VIFM_SET(vifi, r->rt_leaves);
    208  1.3  mycroft 		    update_table_entry(r);
    209  1.1   brezak 		}
    210  1.1   brezak 		else {
    211  1.1   brezak 		    r->rt_leaf_timers[vifi] = LEAF_CONFIRMATION_TIME;
    212  1.1   brezak 		    r->rt_flags |= RTF_LEAF_TIMING;
    213  1.1   brezak 		}
    214  1.1   brezak 	    }
    215  1.1   brezak 	    else if (v->uv_neighbors == NULL &&
    216  1.1   brezak 		     r->rt_leaf_timers[vifi] != 0) {
    217  1.1   brezak 		VIFM_SET(vifi, r->rt_leaves);
    218  1.1   brezak 		r->rt_leaf_timers[vifi] = 0;
    219  1.3  mycroft 		update_table_entry(r);
    220  1.1   brezak 	    }
    221  1.1   brezak 	}
    222  1.1   brezak     }
    223  1.1   brezak }
    224  1.1   brezak 
    225  1.1   brezak 
    226  1.1   brezak /*
    227  1.1   brezak  * Prepare for a sequence of ordered route updates by initializing a pointer
    228  1.1   brezak  * to the start of the routing table.  The pointer is used to remember our
    229  1.1   brezak  * position in the routing table in order to avoid searching from the
    230  1.1   brezak  * beginning for each update; this relies on having the route reports in
    231  1.1   brezak  * a single message be in the same order as the route entries in the routing
    232  1.1   brezak  * table.
    233  1.1   brezak  */
    234  1.3  mycroft void
    235  1.7      wiz start_route_updates(void)
    236  1.1   brezak {
    237  1.3  mycroft     rtp = RT_ADDR;
    238  1.1   brezak }
    239  1.1   brezak 
    240  1.1   brezak 
    241  1.1   brezak /*
    242  1.1   brezak  * Starting at the route entry following the one to which 'rtp' points,
    243  1.1   brezak  * look for a route entry matching the specified origin and mask.  If a
    244  1.1   brezak  * match is found, return TRUE and leave 'rtp' pointing at the found entry.
    245  1.1   brezak  * If no match is found, return FALSE and leave 'rtp' pointing to the route
    246  1.1   brezak  * entry preceding the point at which the new origin should be inserted.
    247  1.1   brezak  * This code is optimized for the normal case in which the first entry to
    248  1.1   brezak  * be examined is the matching entry.
    249  1.1   brezak  */
    250  1.3  mycroft static int
    251  1.7      wiz find_route(u_int32_t origin, u_int32_t mask)
    252  1.1   brezak {
    253  1.7      wiz     struct rtentry *r;
    254  1.1   brezak 
    255  1.1   brezak     r = rtp->rt_next;
    256  1.1   brezak     while (r != NULL) {
    257  1.1   brezak 	if (origin == r->rt_origin && mask == r->rt_originmask) {
    258  1.1   brezak 	    rtp = r;
    259  1.1   brezak 	    return (TRUE);
    260  1.1   brezak 	}
    261  1.3  mycroft 	if (ntohl(mask) < ntohl(r->rt_originmask) ||
    262  1.1   brezak 	    (mask == r->rt_originmask &&
    263  1.3  mycroft 	     ntohl(origin) < ntohl(r->rt_origin))) {
    264  1.1   brezak 	    rtp = r;
    265  1.1   brezak 	    r = r->rt_next;
    266  1.1   brezak 	}
    267  1.1   brezak 	else break;
    268  1.1   brezak     }
    269  1.1   brezak     return (FALSE);
    270  1.1   brezak }
    271  1.1   brezak 
    272  1.1   brezak /*
    273  1.1   brezak  * Create a new routing table entry for the specified origin and link it into
    274  1.1   brezak  * the routing table.  The shared variable 'rtp' is assumed to point to the
    275  1.1   brezak  * routing entry after which the new one should be inserted.  It is left
    276  1.1   brezak  * pointing to the new entry.
    277  1.1   brezak  *
    278  1.1   brezak  * Only the origin, originmask, originwidth and flags fields are initialized
    279  1.6    soren  * in the new route entry; the caller is responsible for filling in the rest.
    280  1.1   brezak  */
    281  1.3  mycroft static void
    282  1.7      wiz create_route(u_int32_t origin, u_int32_t mask)
    283  1.1   brezak {
    284  1.7      wiz     struct rtentry *r;
    285  1.1   brezak 
    286  1.3  mycroft     if ((r = (struct rtentry *) malloc(sizeof(struct rtentry) +
    287  1.3  mycroft 				       (2 * numvifs * sizeof(u_int32_t)) +
    288  1.5  mycroft 				       (numvifs * sizeof(u_int)))) == NULL) {
    289  1.1   brezak 	log(LOG_ERR, 0, "ran out of memory");	/* fatal */
    290  1.1   brezak     }
    291  1.1   brezak     r->rt_origin     = origin;
    292  1.1   brezak     r->rt_originmask = mask;
    293  1.1   brezak     if      (((char *)&mask)[3] != 0) r->rt_originwidth = 4;
    294  1.1   brezak     else if (((char *)&mask)[2] != 0) r->rt_originwidth = 3;
    295  1.1   brezak     else if (((char *)&mask)[1] != 0) r->rt_originwidth = 2;
    296  1.1   brezak     else                              r->rt_originwidth = 1;
    297  1.1   brezak     r->rt_flags        = 0;
    298  1.3  mycroft     r->rt_dominants    = (u_int32_t *)(r + 1);
    299  1.3  mycroft     r->rt_subordinates = (u_int32_t *)(r->rt_dominants + numvifs);
    300  1.5  mycroft     r->rt_leaf_timers  = (u_int *)(r->rt_subordinates + numvifs);
    301  1.3  mycroft     r->rt_groups       = NULL;
    302  1.1   brezak 
    303  1.1   brezak     r->rt_next = rtp->rt_next;
    304  1.1   brezak     rtp->rt_next = r;
    305  1.3  mycroft     r->rt_prev = rtp;
    306  1.3  mycroft     if (r->rt_next != NULL)
    307  1.3  mycroft       (r->rt_next)->rt_prev = r;
    308  1.3  mycroft     else
    309  1.3  mycroft       rt_end = r;
    310  1.1   brezak     rtp = r;
    311  1.1   brezak     ++nroutes;
    312  1.1   brezak }
    313  1.1   brezak 
    314  1.1   brezak 
    315  1.1   brezak /*
    316  1.1   brezak  * Discard the routing table entry following the one to which 'prev_r' points.
    317  1.1   brezak  */
    318  1.3  mycroft static void
    319  1.7      wiz discard_route(struct rtentry *prev_r)
    320  1.1   brezak {
    321  1.7      wiz     struct rtentry *r;
    322  1.1   brezak 
    323  1.1   brezak     r = prev_r->rt_next;
    324  1.1   brezak     prev_r->rt_next = r->rt_next;
    325  1.3  mycroft     if (prev_r->rt_next != NULL)
    326  1.3  mycroft       (prev_r->rt_next)->rt_prev = prev_r;
    327  1.3  mycroft     else
    328  1.3  mycroft       rt_end = prev_r;
    329  1.1   brezak     free((char *)r);
    330  1.1   brezak     --nroutes;
    331  1.1   brezak }
    332  1.1   brezak 
    333  1.1   brezak 
    334  1.1   brezak /*
    335  1.1   brezak  * Process a route report for a single origin, creating or updating the
    336  1.1   brezak  * corresponding routing table entry if necessary.  'src' is either the
    337  1.1   brezak  * address of a neighboring router from which the report arrived, or zero
    338  1.1   brezak  * to indicate a change of status of one of our own interfaces.
    339  1.1   brezak  */
    340  1.3  mycroft void
    341  1.7      wiz update_route(u_int32_t origin, u_int32_t mask, u_int metric, u_int32_t src,
    342  1.7      wiz 	     vifi_t vifi)
    343  1.1   brezak {
    344  1.7      wiz     struct rtentry *r;
    345  1.5  mycroft     u_int adj_metric;
    346  1.1   brezak 
    347  1.1   brezak     /*
    348  1.1   brezak      * Compute an adjusted metric, taking into account the cost of the
    349  1.1   brezak      * subnet or tunnel over which the report arrived, and normalizing
    350  1.1   brezak      * all unreachable/poisoned metrics into a single value.
    351  1.1   brezak      */
    352  1.1   brezak     if (src != 0 && (metric < 1 || metric >= 2*UNREACHABLE)) {
    353  1.1   brezak 	log(LOG_WARNING, 0,
    354  1.1   brezak 	    "%s reports out-of-range metric %u for origin %s",
    355  1.1   brezak 	    inet_fmt(src, s1), metric, inet_fmts(origin, mask, s2));
    356  1.1   brezak 	return;
    357  1.1   brezak     }
    358  1.1   brezak     adj_metric = metric + uvifs[vifi].uv_metric;
    359  1.1   brezak     if (adj_metric > UNREACHABLE) adj_metric = UNREACHABLE;
    360  1.1   brezak 
    361  1.1   brezak     /*
    362  1.1   brezak      * Look up the reported origin in the routing table.
    363  1.1   brezak      */
    364  1.1   brezak     if (!find_route(origin, mask)) {
    365  1.1   brezak 	/*
    366  1.1   brezak 	 * Not found.
    367  1.1   brezak 	 * Don't create a new entry if the report says it's unreachable,
    368  1.1   brezak 	 * or if the reported origin and mask are invalid.
    369  1.1   brezak 	 */
    370  1.1   brezak 	if (adj_metric == UNREACHABLE) {
    371  1.1   brezak 	    return;
    372  1.1   brezak 	}
    373  1.1   brezak 	if (src != 0 && !inet_valid_subnet(origin, mask)) {
    374  1.1   brezak 	    log(LOG_WARNING, 0,
    375  1.1   brezak 		"%s reports an invalid origin (%s) and/or mask (%08x)",
    376  1.1   brezak 		inet_fmt(src, s1), inet_fmt(origin, s2), ntohl(mask));
    377  1.1   brezak 	    return;
    378  1.1   brezak 	}
    379  1.1   brezak 
    380  1.1   brezak 	/*
    381  1.3  mycroft 	 * OK, create the new routing entry.  'rtp' will be left pointing
    382  1.3  mycroft 	 * to the new entry.
    383  1.1   brezak 	 */
    384  1.3  mycroft 	create_route(origin, mask);
    385  1.1   brezak 
    386  1.1   brezak 	/*
    387  1.3  mycroft 	 * Now "steal away" any sources that belong under this route
    388  1.3  mycroft 	 * by deleting any cache entries they might have created
    389  1.3  mycroft 	 * and allowing the kernel to re-request them.
    390  1.1   brezak 	 */
    391  1.3  mycroft 	steal_sources(rtp);
    392  1.1   brezak 
    393  1.1   brezak 	rtp->rt_metric = UNREACHABLE;	/* temporary; updated below */
    394  1.1   brezak     }
    395  1.1   brezak 
    396  1.1   brezak     /*
    397  1.1   brezak      * We now have a routing entry for the reported origin.  Update it?
    398  1.1   brezak      */
    399  1.1   brezak     r = rtp;
    400  1.1   brezak     if (r->rt_metric == UNREACHABLE) {
    401  1.1   brezak 	/*
    402  1.1   brezak 	 * The routing entry is for a formerly-unreachable or new origin.
    403  1.1   brezak 	 * If the report claims reachability, update the entry to use
    404  1.1   brezak 	 * the reported route.
    405  1.1   brezak 	 */
    406  1.1   brezak 	if (adj_metric == UNREACHABLE)
    407  1.1   brezak 	    return;
    408  1.1   brezak 
    409  1.1   brezak 	r->rt_parent   = vifi;
    410  1.1   brezak 	init_children_and_leaves(r, vifi);
    411  1.3  mycroft 
    412  1.1   brezak 	r->rt_gateway  = src;
    413  1.1   brezak 	r->rt_timer    = 0;
    414  1.1   brezak 	r->rt_metric   = adj_metric;
    415  1.1   brezak 	r->rt_flags   |= RTF_CHANGED;
    416  1.1   brezak 	routes_changed = TRUE;
    417  1.3  mycroft 	update_table_entry(r);
    418  1.1   brezak     }
    419  1.1   brezak     else if (src == r->rt_gateway) {
    420  1.1   brezak 	/*
    421  1.1   brezak 	 * The report has come either from the interface directly-connected
    422  1.1   brezak 	 * to the origin subnet (src and r->rt_gateway both equal zero) or
    423  1.1   brezak 	 * from the gateway we have chosen as the best first-hop gateway back
    424  1.1   brezak 	 * towards the origin (src and r->rt_gateway not equal zero).  Reset
    425  1.1   brezak 	 * the route timer and, if the reported metric has changed, update
    426  1.1   brezak 	 * our entry accordingly.
    427  1.1   brezak 	 */
    428  1.1   brezak 	r->rt_timer = 0;
    429  1.1   brezak 	if (adj_metric == r->rt_metric)
    430  1.1   brezak 	    return;
    431  1.1   brezak 
    432  1.1   brezak 	if (adj_metric == UNREACHABLE) {
    433  1.3  mycroft 	    del_table_entry(r, 0, DEL_ALL_ROUTES);
    434  1.1   brezak 	    r->rt_timer = ROUTE_EXPIRE_TIME;
    435  1.1   brezak 	}
    436  1.1   brezak 	else if (adj_metric < r->rt_metric) {
    437  1.1   brezak 	    if (init_children_and_leaves(r, vifi)) {
    438  1.3  mycroft 		update_table_entry(r);
    439  1.1   brezak 	    }
    440  1.1   brezak 	}
    441  1.1   brezak 	r->rt_metric   = adj_metric;
    442  1.1   brezak 	r->rt_flags   |= RTF_CHANGED;
    443  1.1   brezak 	routes_changed = TRUE;
    444  1.1   brezak     }
    445  1.1   brezak     else if (src == 0 ||
    446  1.3  mycroft 	     (r->rt_gateway != 0 &&
    447  1.3  mycroft 	      (adj_metric < r->rt_metric ||
    448  1.3  mycroft 	       (adj_metric == r->rt_metric &&
    449  1.5  mycroft 		(ntohl(src) < ntohl(r->rt_gateway) ||
    450  1.5  mycroft 		 r->rt_timer >= ROUTE_SWITCH_TIME))))) {
    451  1.1   brezak 	/*
    452  1.1   brezak 	 * The report is for an origin we consider reachable; the report
    453  1.1   brezak 	 * comes either from one of our own interfaces or from a gateway
    454  1.1   brezak 	 * other than the one we have chosen as the best first-hop gateway
    455  1.1   brezak 	 * back towards the origin.  If the source of the update is one of
    456  1.1   brezak 	 * our own interfaces, or if the origin is not a directly-connected
    457  1.1   brezak 	 * subnet and the reported metric for that origin is better than
    458  1.1   brezak 	 * what our routing entry says, update the entry to use the new
    459  1.1   brezak 	 * gateway and metric.  We also switch gateways if the reported
    460  1.1   brezak 	 * metric is the same as the one in the route entry and the gateway
    461  1.5  mycroft 	 * associated with the route entry has not been heard from recently,
    462  1.5  mycroft 	 * or if the metric is the same but the reporting gateway has a lower
    463  1.5  mycroft 	 * IP address than the gateway associated with the route entry.
    464  1.1   brezak 	 * Did you get all that?
    465  1.1   brezak 	 */
    466  1.1   brezak 	if (r->rt_parent != vifi || adj_metric < r->rt_metric) {
    467  1.5  mycroft 	    /*
    468  1.5  mycroft 	     * XXX Why do we do this if we are just changing the metric?
    469  1.5  mycroft 	     */
    470  1.1   brezak 	    r->rt_parent = vifi;
    471  1.1   brezak 	    if (init_children_and_leaves(r, vifi)) {
    472  1.3  mycroft 		update_table_entry(r);
    473  1.1   brezak 	    }
    474  1.1   brezak 	}
    475  1.1   brezak 	r->rt_gateway  = src;
    476  1.1   brezak 	r->rt_timer    = 0;
    477  1.1   brezak 	r->rt_metric   = adj_metric;
    478  1.1   brezak 	r->rt_flags   |= RTF_CHANGED;
    479  1.1   brezak 	routes_changed = TRUE;
    480  1.1   brezak     }
    481  1.1   brezak     else if (vifi != r->rt_parent) {
    482  1.1   brezak 	/*
    483  1.1   brezak 	 * The report came from a vif other than the route's parent vif.
    484  1.1   brezak 	 * Update the children and leaf info, if necessary.
    485  1.1   brezak 	 */
    486  1.1   brezak 	if (VIFM_ISSET(vifi, r->rt_children)) {
    487  1.1   brezak 	    /*
    488  1.1   brezak 	     * Vif is a child vif for this route.
    489  1.1   brezak 	     */
    490  1.1   brezak 	    if (metric  < r->rt_metric ||
    491  1.3  mycroft 		(metric == r->rt_metric &&
    492  1.3  mycroft 		 ntohl(src) < ntohl(uvifs[vifi].uv_lcl_addr))) {
    493  1.1   brezak 		/*
    494  1.1   brezak 		 * Neighbor has lower metric to origin (or has same metric
    495  1.1   brezak 		 * and lower IP address) -- it becomes the dominant router,
    496  1.1   brezak 		 * and vif is no longer a child for me.
    497  1.1   brezak 		 */
    498  1.1   brezak 		VIFM_CLR(vifi, r->rt_children);
    499  1.1   brezak 		VIFM_CLR(vifi, r->rt_leaves);
    500  1.1   brezak 		r->rt_dominants   [vifi] = src;
    501  1.1   brezak 		r->rt_subordinates[vifi] = 0;
    502  1.1   brezak 		r->rt_leaf_timers [vifi] = 0;
    503  1.3  mycroft 		update_table_entry(r);
    504  1.1   brezak 	    }
    505  1.1   brezak 	    else if (metric > UNREACHABLE) {	/* "poisoned reverse" */
    506  1.1   brezak 		/*
    507  1.1   brezak 		 * Neighbor considers this vif to be on path to route's
    508  1.1   brezak 		 * origin; if no subordinate recorded, record this neighbor
    509  1.1   brezak 		 * as subordinate and clear the leaf flag.
    510  1.1   brezak 		 */
    511  1.1   brezak 		if (r->rt_subordinates[vifi] == 0) {
    512  1.1   brezak 		    VIFM_CLR(vifi, r->rt_leaves);
    513  1.1   brezak 		    r->rt_subordinates[vifi] = src;
    514  1.1   brezak 		    r->rt_leaf_timers [vifi] = 0;
    515  1.3  mycroft 		    update_table_entry(r);
    516  1.1   brezak 		}
    517  1.1   brezak 	    }
    518  1.1   brezak 	    else if (src == r->rt_subordinates[vifi]) {
    519  1.1   brezak 		/*
    520  1.1   brezak 		 * Current subordinate no longer considers this vif to be on
    521  1.1   brezak 		 * path to route's origin; it is no longer a subordinate
    522  1.1   brezak 		 * router, and we set the leaf confirmation timer to give
    523  1.1   brezak 		 * us time to hear from other subordinates.
    524  1.1   brezak 		 */
    525  1.1   brezak 		r->rt_subordinates[vifi] = 0;
    526  1.1   brezak 		if (uvifs[vifi].uv_neighbors == NULL ||
    527  1.1   brezak 		    uvifs[vifi].uv_neighbors->al_next == NULL) {
    528  1.1   brezak 		    VIFM_SET(vifi, r->rt_leaves);
    529  1.3  mycroft 		    update_table_entry(r);
    530  1.1   brezak 		}
    531  1.1   brezak 		else {
    532  1.1   brezak 		    r->rt_leaf_timers [vifi] = LEAF_CONFIRMATION_TIME;
    533  1.1   brezak 		    r->rt_flags |= RTF_LEAF_TIMING;
    534  1.1   brezak 		}
    535  1.1   brezak 	    }
    536  1.1   brezak 
    537  1.1   brezak 	}
    538  1.1   brezak 	else if (src == r->rt_dominants[vifi] &&
    539  1.3  mycroft 		 (metric  > r->rt_metric ||
    540  1.3  mycroft 		  (metric == r->rt_metric &&
    541  1.3  mycroft 		   ntohl(src) > ntohl(uvifs[vifi].uv_lcl_addr)))) {
    542  1.1   brezak 	    /*
    543  1.1   brezak 	     * Current dominant no longer has a lower metric to origin
    544  1.1   brezak 	     * (or same metric and lower IP address); we adopt the vif
    545  1.1   brezak 	     * as our own child.
    546  1.1   brezak 	     */
    547  1.1   brezak 	    VIFM_SET(vifi, r->rt_children);
    548  1.1   brezak 	    r->rt_dominants  [vifi] = 0;
    549  1.1   brezak 	    if (metric > UNREACHABLE) {
    550  1.1   brezak 		r->rt_subordinates[vifi] = src;
    551  1.1   brezak 	    }
    552  1.1   brezak 	    else if (uvifs[vifi].uv_neighbors == NULL ||
    553  1.1   brezak 		     uvifs[vifi].uv_neighbors->al_next == NULL) {
    554  1.1   brezak 		VIFM_SET(vifi, r->rt_leaves);
    555  1.1   brezak 	    }
    556  1.1   brezak 	    else {
    557  1.1   brezak 		r->rt_leaf_timers[vifi] = LEAF_CONFIRMATION_TIME;
    558  1.1   brezak 		r->rt_flags |= RTF_LEAF_TIMING;
    559  1.1   brezak 	    }
    560  1.3  mycroft 	    update_table_entry(r);
    561  1.1   brezak 	}
    562  1.1   brezak     }
    563  1.1   brezak }
    564  1.1   brezak 
    565  1.1   brezak 
    566  1.1   brezak /*
    567  1.1   brezak  * On every timer interrupt, advance the timer in each routing entry.
    568  1.1   brezak  */
    569  1.3  mycroft void
    570  1.7      wiz age_routes(void)
    571  1.1   brezak {
    572  1.7      wiz     struct rtentry *r;
    573  1.7      wiz     struct rtentry *prev_r;
    574  1.7      wiz     vifi_t vifi;
    575  1.1   brezak 
    576  1.3  mycroft     for (prev_r = RT_ADDR, r = routing_table;
    577  1.1   brezak 	 r != NULL;
    578  1.1   brezak 	 prev_r = r, r = r->rt_next) {
    579  1.1   brezak 
    580  1.1   brezak 	if ((r->rt_timer += TIMER_INTERVAL) < ROUTE_EXPIRE_TIME) {
    581  1.1   brezak 	    /*
    582  1.1   brezak 	     * Route is still good; see if any leaf timers need to be
    583  1.1   brezak 	     * advanced.
    584  1.1   brezak 	     */
    585  1.1   brezak 	    if (r->rt_flags & RTF_LEAF_TIMING) {
    586  1.1   brezak 		r->rt_flags &= ~RTF_LEAF_TIMING;
    587  1.1   brezak 		for (vifi = 0; vifi < numvifs; ++vifi) {
    588  1.1   brezak 		    if (r->rt_leaf_timers[vifi] != 0) {
    589  1.1   brezak 			/*
    590  1.1   brezak 			 * Unlike other timers, leaf timers decrement.
    591  1.1   brezak 			 */
    592  1.1   brezak 			if ((r->rt_leaf_timers[vifi] -= TIMER_INTERVAL) == 0){
    593  1.3  mycroft #ifdef NOTYET
    594  1.3  mycroft 			    /* If the vif is a physical leaf but has neighbors,
    595  1.3  mycroft 			     * it is not a tree leaf.  If I am a leaf, then no
    596  1.3  mycroft 			     * interface with neighbors is a tree leaf. */
    597  1.3  mycroft 			    if (!(((uvifs[vifi].uv_flags & VIFF_LEAF) ||
    598  1.3  mycroft 				   (vifs_with_neighbors == 1)) &&
    599  1.3  mycroft 				  (uvifs[vifi].uv_neighbors != NULL))) {
    600  1.3  mycroft #endif
    601  1.3  mycroft 				VIFM_SET(vifi, r->rt_leaves);
    602  1.3  mycroft 				update_table_entry(r);
    603  1.3  mycroft #ifdef NOTYET
    604  1.3  mycroft 			    }
    605  1.3  mycroft #endif
    606  1.1   brezak 			}
    607  1.1   brezak 			else {
    608  1.1   brezak 			    r->rt_flags |= RTF_LEAF_TIMING;
    609  1.1   brezak 			}
    610  1.1   brezak 		    }
    611  1.1   brezak 		}
    612  1.1   brezak 	    }
    613  1.1   brezak 	}
    614  1.1   brezak 	else if (r->rt_timer >= ROUTE_DISCARD_TIME) {
    615  1.1   brezak 	    /*
    616  1.1   brezak 	     * Time to garbage-collect the route entry.
    617  1.1   brezak 	     */
    618  1.3  mycroft 	    del_table_entry(r, 0, DEL_ALL_ROUTES);
    619  1.1   brezak 	    discard_route(prev_r);
    620  1.1   brezak 	    r = prev_r;
    621  1.1   brezak 	}
    622  1.1   brezak 	else if (r->rt_metric != UNREACHABLE) {
    623  1.1   brezak 	    /*
    624  1.1   brezak 	     * Time to expire the route entry.  If the gateway is zero,
    625  1.1   brezak 	     * i.e., it is a route to a directly-connected subnet, just
    626  1.1   brezak 	     * set the timer back to zero; such routes expire only when
    627  1.1   brezak 	     * the interface to the subnet goes down.
    628  1.1   brezak 	     */
    629  1.1   brezak 	    if (r->rt_gateway == 0) {
    630  1.1   brezak 		r->rt_timer = 0;
    631  1.3  mycroft 	    }
    632  1.1   brezak 	    else {
    633  1.3  mycroft 		del_table_entry(r, 0, DEL_ALL_ROUTES);
    634  1.1   brezak 		r->rt_metric   = UNREACHABLE;
    635  1.1   brezak 		r->rt_flags   |= RTF_CHANGED;
    636  1.1   brezak 		routes_changed = TRUE;
    637  1.1   brezak 	    }
    638  1.1   brezak 	}
    639  1.1   brezak     }
    640  1.1   brezak }
    641  1.1   brezak 
    642  1.1   brezak 
    643  1.1   brezak /*
    644  1.1   brezak  * Mark all routes as unreachable.  This function is called only from
    645  1.1   brezak  * hup() in preparation for informing all neighbors that we are going
    646  1.1   brezak  * off the air.  For consistency, we ought also to delete all reachable
    647  1.1   brezak  * route entries from the kernel, but since we are about to exit we rely
    648  1.1   brezak  * on the kernel to do its own cleanup -- no point in making all those
    649  1.1   brezak  * expensive kernel calls now.
    650  1.1   brezak  */
    651  1.3  mycroft void
    652  1.7      wiz expire_all_routes(void)
    653  1.1   brezak {
    654  1.7      wiz     struct rtentry *r;
    655  1.1   brezak 
    656  1.1   brezak     for (r = routing_table; r != NULL; r = r->rt_next) {
    657  1.1   brezak 	r->rt_metric   = UNREACHABLE;
    658  1.1   brezak 	r->rt_flags   |= RTF_CHANGED;
    659  1.1   brezak 	routes_changed = TRUE;
    660  1.1   brezak     }
    661  1.1   brezak }
    662  1.1   brezak 
    663  1.1   brezak 
    664  1.1   brezak /*
    665  1.3  mycroft  * Delete all the routes in the routing table.
    666  1.3  mycroft  */
    667  1.3  mycroft void
    668  1.7      wiz free_all_routes(void)
    669  1.3  mycroft {
    670  1.7      wiz     struct rtentry *r;
    671  1.3  mycroft 
    672  1.3  mycroft     r = RT_ADDR;
    673  1.3  mycroft 
    674  1.3  mycroft     while (r->rt_next)
    675  1.3  mycroft 	discard_route(r);
    676  1.3  mycroft }
    677  1.3  mycroft 
    678  1.3  mycroft 
    679  1.3  mycroft /*
    680  1.1   brezak  * Process an incoming neighbor probe message.
    681  1.1   brezak  */
    682  1.3  mycroft void
    683  1.7      wiz accept_probe(u_int32_t src, u_int32_t dst, char *p, int datalen,
    684  1.7      wiz 	     u_int32_t level)
    685  1.1   brezak {
    686  1.1   brezak     vifi_t vifi;
    687  1.1   brezak 
    688  1.1   brezak     if ((vifi = find_vif(src, dst)) == NO_VIF) {
    689  1.1   brezak 	log(LOG_INFO, 0,
    690  1.1   brezak     	    "ignoring probe from non-neighbor %s", inet_fmt(src, s1));
    691  1.1   brezak 	return;
    692  1.1   brezak     }
    693  1.1   brezak 
    694  1.5  mycroft     update_neighbor(vifi, src, DVMRP_PROBE, p, datalen, level);
    695  1.1   brezak }
    696  1.1   brezak 
    697  1.2   brezak struct newrt {
    698  1.3  mycroft 	u_int32_t mask;
    699  1.3  mycroft 	u_int32_t origin;
    700  1.2   brezak 	int metric;
    701  1.2   brezak 	int pad;
    702  1.2   brezak };
    703  1.2   brezak 
    704  1.5  mycroft static int
    705  1.7      wiz compare_rts(const void *rt1, const void *rt2)
    706  1.7      wiz {
    707  1.7      wiz     struct newrt *r1 = (struct newrt *)rt1;
    708  1.7      wiz     struct newrt *r2 = (struct newrt *)rt2;
    709  1.7      wiz     u_int32_t m1 = ntohl(r1->mask);
    710  1.7      wiz     u_int32_t m2 = ntohl(r2->mask);
    711  1.7      wiz     u_int32_t o1, o2;
    712  1.2   brezak 
    713  1.2   brezak     if (m1 > m2)
    714  1.3  mycroft 	return (-1);
    715  1.3  mycroft     if (m1 < m2)
    716  1.2   brezak 	return (1);
    717  1.2   brezak 
    718  1.2   brezak     /* masks are equal */
    719  1.2   brezak     o1 = ntohl(r1->origin);
    720  1.2   brezak     o2 = ntohl(r2->origin);
    721  1.2   brezak     if (o1 > o2)
    722  1.3  mycroft 	return (-1);
    723  1.3  mycroft     if (o1 < o2)
    724  1.2   brezak 	return (1);
    725  1.2   brezak     return (0);
    726  1.2   brezak }
    727  1.1   brezak 
    728  1.1   brezak /*
    729  1.1   brezak  * Process an incoming route report message.
    730  1.1   brezak  */
    731  1.3  mycroft void
    732  1.7      wiz accept_report(u_int32_t src, u_int32_t dst, char *p, int datalen,
    733  1.7      wiz 	      u_int32_t level)
    734  1.1   brezak {
    735  1.1   brezak     vifi_t vifi;
    736  1.7      wiz     int width, i, nrt = 0;
    737  1.1   brezak     int metric;
    738  1.3  mycroft     u_int32_t mask;
    739  1.3  mycroft     u_int32_t origin;
    740  1.2   brezak     struct newrt rt[4096];
    741  1.1   brezak 
    742  1.1   brezak     if ((vifi = find_vif(src, dst)) == NO_VIF) {
    743  1.1   brezak 	log(LOG_INFO, 0,
    744  1.1   brezak     	    "ignoring route report from non-neighbor %s", inet_fmt(src, s1));
    745  1.1   brezak 	return;
    746  1.1   brezak     }
    747  1.1   brezak 
    748  1.3  mycroft     if (!update_neighbor(vifi, src, DVMRP_REPORT, NULL, 0, level))
    749  1.1   brezak 	return;
    750  1.1   brezak 
    751  1.2   brezak     if (datalen > 2*4096) {
    752  1.2   brezak 	log(LOG_INFO, 0,
    753  1.2   brezak     	    "ignoring oversize (%d bytes) route report from %s",
    754  1.2   brezak 	    datalen, inet_fmt(src, s1));
    755  1.2   brezak 	return;
    756  1.2   brezak     }
    757  1.1   brezak 
    758  1.1   brezak     while (datalen > 0) {	/* Loop through per-mask lists. */
    759  1.3  mycroft 
    760  1.1   brezak 	if (datalen < 3) {
    761  1.1   brezak 	    log(LOG_WARNING, 0,
    762  1.3  mycroft 		"received truncated route report from %s",
    763  1.3  mycroft 		inet_fmt(src, s1));
    764  1.1   brezak 	    return;
    765  1.1   brezak 	}
    766  1.3  mycroft 	((u_char *)&mask)[0] = 0xff;            width = 1;
    767  1.3  mycroft 	if ((((u_char *)&mask)[1] = *p++) != 0) width = 2;
    768  1.3  mycroft 	if ((((u_char *)&mask)[2] = *p++) != 0) width = 3;
    769  1.3  mycroft 	if ((((u_char *)&mask)[3] = *p++) != 0) width = 4;
    770  1.5  mycroft 	if (!inet_valid_mask(ntohl(mask))) {
    771  1.5  mycroft 	    log(LOG_WARNING, 0,
    772  1.5  mycroft 		"%s reports bogus netmask 0x%08x (%s)",
    773  1.5  mycroft 		inet_fmt(src, s1), ntohl(mask), inet_fmt(mask, s2));
    774  1.5  mycroft 	    return;
    775  1.5  mycroft 	}
    776  1.1   brezak 	datalen -= 3;
    777  1.1   brezak 
    778  1.1   brezak 	do {			/* Loop through (origin, metric) pairs */
    779  1.1   brezak 	    if (datalen < width + 1) {
    780  1.1   brezak 		log(LOG_WARNING, 0,
    781  1.3  mycroft 		    "received truncated route report from %s",
    782  1.3  mycroft 		    inet_fmt(src, s1));
    783  1.1   brezak 		return;
    784  1.1   brezak 	    }
    785  1.1   brezak 	    origin = 0;
    786  1.1   brezak 	    for (i = 0; i < width; ++i)
    787  1.1   brezak 		((char *)&origin)[i] = *p++;
    788  1.1   brezak 	    metric = *p++;
    789  1.1   brezak 	    datalen -= width + 1;
    790  1.2   brezak 	    rt[nrt].mask   = mask;
    791  1.2   brezak 	    rt[nrt].origin = origin;
    792  1.3  mycroft 	    rt[nrt].metric = (metric & 0x7f);
    793  1.2   brezak 	    ++nrt;
    794  1.1   brezak 	} while (!(metric & 0x80));
    795  1.1   brezak     }
    796  1.5  mycroft 
    797  1.2   brezak     qsort((char*)rt, nrt, sizeof(rt[0]), compare_rts);
    798  1.2   brezak     start_route_updates();
    799  1.3  mycroft     /*
    800  1.3  mycroft      * If the last entry is default, change mask from 0xff000000 to 0
    801  1.3  mycroft      */
    802  1.3  mycroft     if (rt[nrt-1].origin == 0)
    803  1.3  mycroft 	rt[nrt-1].mask = 0;
    804  1.3  mycroft 
    805  1.3  mycroft     log(LOG_DEBUG, 0, "Updating %d routes from %s to %s", nrt,
    806  1.3  mycroft 		inet_fmt(src, s1), inet_fmt(dst, s2));
    807  1.5  mycroft     for (i = 0; i < nrt; ++i) {
    808  1.5  mycroft 	if (i != 0 && rt[i].origin == rt[i-1].origin &&
    809  1.5  mycroft 		      rt[i].mask == rt[i-1].mask) {
    810  1.5  mycroft 	    log(LOG_WARNING, 0, "%s reports duplicate route for %s",
    811  1.5  mycroft 		inet_fmt(src, s1), inet_fmts(rt[i].origin, rt[i].mask, s2));
    812  1.5  mycroft 	    continue;
    813  1.5  mycroft 	}
    814  1.3  mycroft 	update_route(rt[i].origin, rt[i].mask, rt[i].metric,
    815  1.3  mycroft 		     src, vifi);
    816  1.5  mycroft     }
    817  1.1   brezak 
    818  1.1   brezak     if (routes_changed && !delay_change_reports)
    819  1.1   brezak 	report_to_all_neighbors(CHANGED_ROUTES);
    820  1.1   brezak }
    821  1.1   brezak 
    822  1.1   brezak 
    823  1.1   brezak /*
    824  1.1   brezak  * Send a route report message to destination 'dst', via virtual interface
    825  1.1   brezak  * 'vifi'.  'which_routes' specifies ALL_ROUTES or CHANGED_ROUTES.
    826  1.1   brezak  */
    827  1.3  mycroft void
    828  1.7      wiz report(int which_routes, vifi_t vifi, u_int32_t dst)
    829  1.1   brezak {
    830  1.7      wiz     struct rtentry *r;
    831  1.7      wiz     char *p;
    832  1.7      wiz     int i;
    833  1.5  mycroft     int datalen = 0;
    834  1.5  mycroft     int width = 0;
    835  1.5  mycroft     u_int32_t mask = 0;
    836  1.3  mycroft     u_int32_t src;
    837  1.3  mycroft     u_int32_t nflags;
    838  1.1   brezak 
    839  1.1   brezak     src = uvifs[vifi].uv_lcl_addr;
    840  1.1   brezak 
    841  1.1   brezak     p = send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN;
    842  1.1   brezak 
    843  1.3  mycroft #ifdef NOTYET
    844  1.3  mycroft     /* If I'm not a leaf, but the neighbor is a leaf, only advertise default */
    845  1.3  mycroft     if ((vifs_with_neighbors != 1) && (uvifs[vifi].uv_flags & VIFF_LEAF)) {
    846  1.3  mycroft       *p++ = 0;       /* 0xff000000 mask */
    847  1.3  mycroft       *p++ = 0;
    848  1.3  mycroft       *p++ = 0;
    849  1.3  mycroft       *p++ = 0;       /* class A net 0.0.0.0 == default */
    850  1.3  mycroft       *p++ = 0x81;    /*XXX metric 1, is this safe? */
    851  1.3  mycroft       datalen += 5;
    852  1.3  mycroft       send_igmp(src, dst, IGMP_DVMRP, DVMRP_REPORT,
    853  1.3  mycroft                 htonl(MROUTED_LEVEL), datalen);
    854  1.3  mycroft       return;
    855  1.3  mycroft     }
    856  1.3  mycroft #endif
    857  1.3  mycroft 
    858  1.3  mycroft     nflags = (uvifs[vifi].uv_flags & VIFF_LEAF) ? 0 : LEAF_FLAGS;
    859  1.3  mycroft 
    860  1.3  mycroft     for (r = rt_end; r != RT_ADDR; r = r->rt_prev) {
    861  1.1   brezak 
    862  1.1   brezak 	if (which_routes == CHANGED_ROUTES && !(r->rt_flags & RTF_CHANGED))
    863  1.1   brezak 	    continue;
    864  1.1   brezak 
    865  1.1   brezak 	/*
    866  1.1   brezak 	 * If there is no room for this route in the current message,
    867  1.1   brezak 	 * send the message and start a new one.
    868  1.1   brezak 	 */
    869  1.1   brezak 	if (datalen + ((r->rt_originmask == mask) ?
    870  1.3  mycroft 		       (width + 1) :
    871  1.3  mycroft 		       (r->rt_originwidth + 4)) > MAX_DVMRP_DATA_LEN) {
    872  1.1   brezak 	    *(p-1) |= 0x80;
    873  1.1   brezak 	    send_igmp(src, dst, IGMP_DVMRP, DVMRP_REPORT,
    874  1.3  mycroft 		      htonl(MROUTED_LEVEL | nflags), datalen);
    875  1.1   brezak 
    876  1.1   brezak 	    p = send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN;
    877  1.1   brezak 	    datalen = 0;
    878  1.1   brezak 	    mask = 0;
    879  1.1   brezak 	}
    880  1.1   brezak 
    881  1.5  mycroft 	if (r->rt_originmask != mask || datalen == 0) {
    882  1.1   brezak 	    mask  = r->rt_originmask;
    883  1.1   brezak 	    width = r->rt_originwidth;
    884  1.1   brezak 	    if (datalen != 0) *(p-1) |= 0x80;
    885  1.1   brezak 	    *p++ = ((char *)&mask)[1];
    886  1.1   brezak 	    *p++ = ((char *)&mask)[2];
    887  1.1   brezak 	    *p++ = ((char *)&mask)[3];
    888  1.1   brezak 	    datalen += 3;
    889  1.1   brezak 	}
    890  1.1   brezak 
    891  1.1   brezak 	for (i = 0; i < width; ++i)
    892  1.1   brezak 	    *p++ = ((char *)&(r->rt_origin))[i];
    893  1.1   brezak 
    894  1.1   brezak 	*p++ = (r->rt_parent == vifi && r->rt_metric != UNREACHABLE) ?
    895  1.3  mycroft 	    (char)(r->rt_metric + UNREACHABLE) :  /* "poisoned reverse" */
    896  1.1   brezak 		(char)(r->rt_metric);
    897  1.1   brezak 
    898  1.1   brezak 	datalen += width + 1;
    899  1.1   brezak     }
    900  1.1   brezak 
    901  1.1   brezak     if (datalen != 0) {
    902  1.1   brezak 	*(p-1) |= 0x80;
    903  1.1   brezak 	send_igmp(src, dst, IGMP_DVMRP, DVMRP_REPORT,
    904  1.3  mycroft 		  htonl(MROUTED_LEVEL | nflags), datalen);
    905  1.1   brezak     }
    906  1.1   brezak }
    907  1.1   brezak 
    908  1.1   brezak 
    909  1.1   brezak /*
    910  1.1   brezak  * Send a route report message to all neighboring routers.
    911  1.1   brezak  * 'which_routes' specifies ALL_ROUTES or CHANGED_ROUTES.
    912  1.1   brezak  */
    913  1.3  mycroft void
    914  1.7      wiz report_to_all_neighbors(int which_routes)
    915  1.1   brezak {
    916  1.7      wiz     vifi_t vifi;
    917  1.7      wiz     struct uvif *v;
    918  1.7      wiz     struct rtentry *r;
    919  1.1   brezak     int routes_changed_before;
    920  1.1   brezak 
    921  1.1   brezak     /*
    922  1.1   brezak      * Remember the state of the global routes_changed flag before
    923  1.1   brezak      * generating the reports, and clear the flag.
    924  1.1   brezak      */
    925  1.1   brezak     routes_changed_before = routes_changed;
    926  1.1   brezak     routes_changed = FALSE;
    927  1.1   brezak 
    928  1.1   brezak 
    929  1.1   brezak     for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
    930  1.1   brezak 	if (v->uv_neighbors != NULL) {
    931  1.1   brezak 	    report(which_routes, vifi,
    932  1.1   brezak 		   (v->uv_flags & VIFF_TUNNEL) ? v->uv_rmt_addr
    933  1.3  mycroft 		   : dvmrp_group);
    934  1.1   brezak 	}
    935  1.1   brezak     }
    936  1.1   brezak 
    937  1.1   brezak     /*
    938  1.1   brezak      * If there were changed routes before we sent the reports AND
    939  1.1   brezak      * if no new changes occurred while sending the reports, clear
    940  1.1   brezak      * the change flags in the individual route entries.  If changes
    941  1.1   brezak      * did occur while sending the reports, new reports will be
    942  1.1   brezak      * generated at the next timer interrupt.
    943  1.1   brezak      */
    944  1.1   brezak     if (routes_changed_before && !routes_changed) {
    945  1.1   brezak 	for (r = routing_table; r != NULL; r = r->rt_next) {
    946  1.1   brezak 	    r->rt_flags &= ~RTF_CHANGED;
    947  1.1   brezak 	}
    948  1.1   brezak     }
    949  1.1   brezak 
    950  1.1   brezak     /*
    951  1.1   brezak      * Set a flag to inhibit further reports of changed routes until the
    952  1.1   brezak      * next timer interrupt.  This is to alleviate update storms.
    953  1.1   brezak      */
    954  1.1   brezak     delay_change_reports = TRUE;
    955  1.2   brezak }
    956  1.2   brezak 
    957  1.2   brezak /*
    958  1.2   brezak  * Send a route report message to destination 'dst', via virtual interface
    959  1.2   brezak  * 'vifi'.  'which_routes' specifies ALL_ROUTES or CHANGED_ROUTES.
    960  1.2   brezak  */
    961  1.5  mycroft static int
    962  1.7      wiz report_chunk(struct rtentry *start_rt, vifi_t vifi, u_int32_t dst)
    963  1.2   brezak {
    964  1.7      wiz     struct rtentry *r;
    965  1.7      wiz     char *p;
    966  1.7      wiz     int i;
    967  1.7      wiz     int nrt = 0;
    968  1.5  mycroft     int datalen = 0;
    969  1.5  mycroft     int width = 0;
    970  1.5  mycroft     u_int32_t mask = 0;
    971  1.3  mycroft     u_int32_t src;
    972  1.3  mycroft     u_int32_t nflags;
    973  1.2   brezak 
    974  1.2   brezak     src = uvifs[vifi].uv_lcl_addr;
    975  1.2   brezak     p = send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN;
    976  1.2   brezak 
    977  1.3  mycroft     nflags = (uvifs[vifi].uv_flags & VIFF_LEAF) ? 0 : LEAF_FLAGS;
    978  1.3  mycroft 
    979  1.3  mycroft     for (r = start_rt; r != RT_ADDR; r = r->rt_prev) {
    980  1.3  mycroft 
    981  1.3  mycroft #ifdef NOTYET
    982  1.3  mycroft 	/* Don't send poisoned routes back to parents if I am a leaf */
    983  1.3  mycroft 	if ((vifs_with_neighbors == 1) && (r->rt_parent == vifi)
    984  1.3  mycroft 		&& (r->rt_metric > 1)) {
    985  1.3  mycroft 	    ++nrt;
    986  1.3  mycroft 	    continue;
    987  1.3  mycroft 	}
    988  1.3  mycroft #endif
    989  1.3  mycroft 
    990  1.2   brezak 	/*
    991  1.2   brezak 	 * If there is no room for this route in the current message,
    992  1.2   brezak 	 * send it & return how many routes we sent.
    993  1.2   brezak 	 */
    994  1.2   brezak 	if (datalen + ((r->rt_originmask == mask) ?
    995  1.3  mycroft 		       (width + 1) :
    996  1.3  mycroft 		       (r->rt_originwidth + 4)) > MAX_DVMRP_DATA_LEN) {
    997  1.2   brezak 	    *(p-1) |= 0x80;
    998  1.2   brezak 	    send_igmp(src, dst, IGMP_DVMRP, DVMRP_REPORT,
    999  1.3  mycroft 		      htonl(MROUTED_LEVEL | nflags), datalen);
   1000  1.2   brezak 	    return (nrt);
   1001  1.2   brezak 	}
   1002  1.5  mycroft 	if (r->rt_originmask != mask || datalen == 0) {
   1003  1.2   brezak 	    mask  = r->rt_originmask;
   1004  1.2   brezak 	    width = r->rt_originwidth;
   1005  1.2   brezak 	    if (datalen != 0) *(p-1) |= 0x80;
   1006  1.2   brezak 	    *p++ = ((char *)&mask)[1];
   1007  1.2   brezak 	    *p++ = ((char *)&mask)[2];
   1008  1.2   brezak 	    *p++ = ((char *)&mask)[3];
   1009  1.2   brezak 	    datalen += 3;
   1010  1.2   brezak 	}
   1011  1.2   brezak 	for (i = 0; i < width; ++i)
   1012  1.2   brezak 	    *p++ = ((char *)&(r->rt_origin))[i];
   1013  1.2   brezak 
   1014  1.2   brezak 	*p++ = (r->rt_parent == vifi && r->rt_metric != UNREACHABLE) ?
   1015  1.3  mycroft 	    (char)(r->rt_metric + UNREACHABLE) :  /* "poisoned reverse" */
   1016  1.2   brezak 		(char)(r->rt_metric);
   1017  1.2   brezak 	++nrt;
   1018  1.2   brezak 	datalen += width + 1;
   1019  1.2   brezak     }
   1020  1.2   brezak     if (datalen != 0) {
   1021  1.2   brezak 	*(p-1) |= 0x80;
   1022  1.2   brezak 	send_igmp(src, dst, IGMP_DVMRP, DVMRP_REPORT,
   1023  1.3  mycroft 		  htonl(MROUTED_LEVEL | nflags), datalen);
   1024  1.2   brezak     }
   1025  1.2   brezak     return (nrt);
   1026  1.2   brezak }
   1027  1.2   brezak 
   1028  1.2   brezak /*
   1029  1.2   brezak  * send the next chunk of our routing table to all neighbors.
   1030  1.3  mycroft  * return the length of the smallest chunk we sent out.
   1031  1.2   brezak  */
   1032  1.3  mycroft int
   1033  1.7      wiz report_next_chunk(void)
   1034  1.2   brezak {
   1035  1.7      wiz     vifi_t vifi;
   1036  1.7      wiz     struct uvif *v;
   1037  1.7      wiz     struct rtentry *sr;
   1038  1.7      wiz     int i, n = 0, min = 20000;
   1039  1.2   brezak     static int start_rt;
   1040  1.2   brezak 
   1041  1.2   brezak     if (nroutes <= 0)
   1042  1.2   brezak 	return (0);
   1043  1.2   brezak 
   1044  1.2   brezak     /*
   1045  1.2   brezak      * find this round's starting route.
   1046  1.2   brezak      */
   1047  1.3  mycroft     for (sr = rt_end, i = start_rt; --i >= 0; ) {
   1048  1.3  mycroft 	sr = sr->rt_prev;
   1049  1.3  mycroft 	if (sr == RT_ADDR)
   1050  1.3  mycroft 	    sr = rt_end;
   1051  1.2   brezak     }
   1052  1.3  mycroft 
   1053  1.2   brezak     /*
   1054  1.2   brezak      * send one chunk of routes starting at this round's start to
   1055  1.2   brezak      * all our neighbors.
   1056  1.2   brezak      */
   1057  1.2   brezak     for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
   1058  1.3  mycroft 	if ((v->uv_neighbors != NULL)
   1059  1.3  mycroft #ifdef NOTYET
   1060  1.3  mycroft 	&& !(v->uv_flags & VIFF_LEAF)
   1061  1.3  mycroft #endif
   1062  1.3  mycroft 		) {
   1063  1.2   brezak 	    n = report_chunk(sr, vifi,
   1064  1.3  mycroft 			     (v->uv_flags & VIFF_TUNNEL) ? v->uv_rmt_addr
   1065  1.3  mycroft 			     : dvmrp_group);
   1066  1.3  mycroft 	    if (n < min)
   1067  1.3  mycroft 		min = n;
   1068  1.2   brezak 	}
   1069  1.2   brezak     }
   1070  1.3  mycroft     if (min == 20000)
   1071  1.3  mycroft 	min = 0;	/* Neighborless router didn't send any routes */
   1072  1.3  mycroft 
   1073  1.3  mycroft     n = min;
   1074  1.3  mycroft     log(LOG_INFO, 0, "update %d starting at %d of %d",
   1075  1.3  mycroft 	n, (nroutes - start_rt), nroutes);
   1076  1.3  mycroft 
   1077  1.2   brezak     start_rt = (start_rt + n) % nroutes;
   1078  1.2   brezak     return (n);
   1079  1.1   brezak }
   1080  1.1   brezak 
   1081  1.1   brezak 
   1082  1.1   brezak /*
   1083  1.1   brezak  * Print the contents of the routing table on file 'fp'.
   1084  1.1   brezak  */
   1085  1.3  mycroft void
   1086  1.7      wiz dump_routes(FILE *fp)
   1087  1.1   brezak {
   1088  1.7      wiz     struct rtentry *r;
   1089  1.7      wiz     vifi_t i;
   1090  1.3  mycroft 
   1091  1.1   brezak 
   1092  1.1   brezak     fprintf(fp,
   1093  1.3  mycroft 	    "Multicast Routing Table (%u %s)\n%s\n",
   1094  1.3  mycroft 	    nroutes, (nroutes == 1) ? "entry" : "entries",
   1095  1.3  mycroft 	    " Origin-Subnet      From-Gateway    Metric Tmr In-Vif  Out-Vifs");
   1096  1.1   brezak 
   1097  1.1   brezak     for (r = routing_table; r != NULL; r = r->rt_next) {
   1098  1.1   brezak 
   1099  1.3  mycroft 	fprintf(fp, " %-18s %-15s ",
   1100  1.1   brezak 		inet_fmts(r->rt_origin, r->rt_originmask, s1),
   1101  1.1   brezak 		(r->rt_gateway == 0) ? "" : inet_fmt(r->rt_gateway, s2));
   1102  1.1   brezak 
   1103  1.1   brezak 	fprintf(fp, (r->rt_metric == UNREACHABLE) ? "  NR " : "%4u ",
   1104  1.1   brezak 		r->rt_metric);
   1105  1.1   brezak 
   1106  1.3  mycroft 	fprintf(fp, "  %3u %3u   ", r->rt_timer, r->rt_parent);
   1107  1.1   brezak 
   1108  1.1   brezak 	for (i = 0; i < numvifs; ++i) {
   1109  1.1   brezak 	    if (VIFM_ISSET(i, r->rt_children)) {
   1110  1.1   brezak 		fprintf(fp, " %u%c",
   1111  1.1   brezak 			i, VIFM_ISSET(i, r->rt_leaves) ? '*' : ' ');
   1112  1.1   brezak 	    }
   1113  1.1   brezak 	}
   1114  1.1   brezak 	fprintf(fp, "\n");
   1115  1.1   brezak     }
   1116  1.1   brezak     fprintf(fp, "\n");
   1117  1.3  mycroft }
   1118  1.3  mycroft 
   1119  1.3  mycroft struct rtentry *
   1120  1.7      wiz determine_route(u_int32_t src)
   1121  1.3  mycroft {
   1122  1.3  mycroft     struct rtentry *rt;
   1123  1.3  mycroft 
   1124  1.3  mycroft     for (rt = routing_table; rt != NULL; rt = rt->rt_next) {
   1125  1.3  mycroft 	if (rt->rt_origin == (src & rt->rt_originmask))
   1126  1.3  mycroft 	    break;
   1127  1.3  mycroft     }
   1128  1.3  mycroft     return rt;
   1129  1.1   brezak }
   1130