Home | History | Annotate | Line # | Download | only in routed
rdisc.c revision 1.8
      1 /*	$NetBSD: rdisc.c,v 1.8 1998/10/25 14:56:08 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgment:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #if !defined(lint) && !defined(sgi) && !defined(__NetBSD__)
     37 static char sccsid[] = "@(#)rdisc.c	8.1 (Berkeley) x/y/95";
     38 #elif defined(__NetBSD__)
     39 #include <sys/cdefs.h>
     40 __RCSID("$NetBSD: rdisc.c,v 1.8 1998/10/25 14:56:08 christos Exp $");
     41 #endif
     42 
     43 #include "defs.h"
     44 #include <netinet/in_systm.h>
     45 #include <netinet/ip.h>
     46 #include <netinet/ip_icmp.h>
     47 #if defined(sgi) && !defined(PRE_KUDZU)
     48 #include <cap_net.h>
     49 #else
     50 #define cap_socket socket
     51 #endif
     52 
     53 /* router advertisement ICMP packet */
     54 struct icmp_ad {
     55 	u_int8_t    icmp_type;		/* type of message */
     56 	u_int8_t    icmp_code;		/* type sub code */
     57 	u_int16_t   icmp_cksum;		/* ones complement cksum of struct */
     58 	u_int8_t    icmp_ad_num;	/* # of following router addresses */
     59 	u_int8_t    icmp_ad_asize;	/* 2--words in each advertisement */
     60 	u_int16_t   icmp_ad_life;	/* seconds of validity */
     61 	struct icmp_ad_info {
     62 	    n_long  icmp_ad_addr;
     63 	    n_long  icmp_ad_pref;
     64 	} icmp_ad_info[1];
     65 };
     66 
     67 /* router solicitation ICMP packet */
     68 struct icmp_so {
     69 	u_int8_t    icmp_type;		/* type of message */
     70 	u_int8_t    icmp_code;		/* type sub code */
     71 	u_int16_t   icmp_cksum;		/* ones complement cksum of struct */
     72 	n_long	    icmp_so_rsvd;
     73 };
     74 
     75 union ad_u {
     76 	struct icmp icmp;
     77 	struct icmp_ad ad;
     78 	struct icmp_so so;
     79 };
     80 
     81 
     82 int	rdisc_sock = -1;		/* router-discovery raw socket */
     83 struct interface *rdisc_sock_mcast;	/* current multicast interface */
     84 
     85 struct timeval rdisc_timer;
     86 int rdisc_ok;				/* using solicited route */
     87 
     88 
     89 #define MAX_ADS 16			/* at least one per interface */
     90 struct dr {				/* accumulated advertisements */
     91     struct interface *dr_ifp;
     92     naddr   dr_gate;			/* gateway */
     93     time_t  dr_ts;			/* when received */
     94     time_t  dr_life;			/* lifetime */
     95     n_long  dr_recv_pref;		/* received but biased preference */
     96     n_long  dr_pref;			/* preference adjusted by metric */
     97 } *cur_drp, drs[MAX_ADS];
     98 
     99 /* convert between signed, balanced around zero,
    100  * and unsigned zero-based preferences */
    101 #define SIGN_PREF(p) ((p) ^ MIN_PreferenceLevel)
    102 #define UNSIGN_PREF(p) SIGN_PREF(p)
    103 /* adjust unsigned preference by interface metric,
    104  * without driving it to infinity */
    105 #define PREF(p, ifp) ((p) <= (ifp)->int_metric ? ((p) != 0 ? 1 : 0) \
    106 		      : (p) - ((ifp)->int_metric))
    107 
    108 static void rdisc_sort(void);
    109 
    110 
    111 /* dump an ICMP Router Discovery Advertisement Message
    112  */
    113 static void
    114 trace_rdisc(char	*act,
    115 	    naddr	from,
    116 	    naddr	to,
    117 	    struct interface *ifp,
    118 	    union ad_u	*p,
    119 	    u_int	len)
    120 {
    121 	int i;
    122 	n_long *wp, *lim;
    123 
    124 
    125 	if (!TRACEPACKETS || ftrace == 0)
    126 		return;
    127 
    128 	lastlog();
    129 
    130 	if (p->icmp.icmp_type == ICMP_ROUTERADVERT) {
    131 		(void)fprintf(ftrace, "%s Router Ad"
    132 			      " from %s to %s via %s life=%d\n",
    133 			      act, naddr_ntoa(from), naddr_ntoa(to),
    134 			      ifp ? ifp->int_name : "?",
    135 			      ntohs(p->ad.icmp_ad_life));
    136 		if (!TRACECONTENTS)
    137 			return;
    138 
    139 		wp = &p->ad.icmp_ad_info[0].icmp_ad_addr;
    140 		lim = &wp[(len - sizeof(p->ad)) / sizeof(*wp)];
    141 		for (i = 0; i < p->ad.icmp_ad_num && wp <= lim; i++) {
    142 			(void)fprintf(ftrace, "\t%s preference=%d",
    143 				      naddr_ntoa(wp[0]), (int)ntohl(wp[1]));
    144 			wp += p->ad.icmp_ad_asize;
    145 		}
    146 		(void)fputc('\n',ftrace);
    147 
    148 	} else {
    149 		trace_act("%s Router Solic. from %s to %s via %s value=%#x",
    150 			  act, naddr_ntoa(from), naddr_ntoa(to),
    151 			  ifp ? ifp->int_name : "?",
    152 			  ntohl(p->so.icmp_so_rsvd));
    153 	}
    154 }
    155 
    156 /* prepare Router Discovery socket.
    157  */
    158 static void
    159 get_rdisc_sock(void)
    160 {
    161 	if (rdisc_sock < 0) {
    162 		rdisc_sock = cap_socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
    163 		if (rdisc_sock < 0)
    164 			BADERR(1,"rdisc_sock = socket()");
    165 		fix_sock(rdisc_sock,"rdisc_sock");
    166 		fix_select();
    167 	}
    168 }
    169 
    170 
    171 /* Pick multicast group for router-discovery socket
    172  */
    173 void
    174 set_rdisc_mg(struct interface *ifp,
    175 	     int on)			/* 0=turn it off */
    176 {
    177 	struct ip_mreq m;
    178 
    179 	if (rdisc_sock < 0) {
    180 		/* Create the raw socket so that we can hear at least
    181 		 * broadcast router discovery packets.
    182 		 */
    183 		if ((ifp->int_state & IS_NO_RDISC) == IS_NO_RDISC
    184 		    || !on)
    185 			return;
    186 		get_rdisc_sock();
    187 	}
    188 
    189 	if (!(ifp->int_if_flags & IFF_MULTICAST)) {
    190 		ifp->int_state &= ~(IS_ALL_HOSTS | IS_ALL_ROUTERS);
    191 		return;
    192 	}
    193 
    194 #ifdef MCAST_PPP_BUG
    195 	if (ifp->int_if_flags & IFF_POINTOPOINT)
    196 		return;
    197 #endif
    198 	memset(&m, 0, sizeof(m));
    199 	m.imr_interface.s_addr = ((ifp->int_if_flags & IFF_POINTOPOINT)
    200 				  ? ifp->int_dstaddr
    201 				  : ifp->int_addr);
    202 	if (supplier
    203 	    || (ifp->int_state & IS_NO_ADV_IN)
    204 	    || !on) {
    205 		/* stop listening to advertisements
    206 		 */
    207 		if (ifp->int_state & IS_ALL_HOSTS) {
    208 			m.imr_multiaddr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
    209 			if (setsockopt(rdisc_sock, IPPROTO_IP,
    210 				       IP_DROP_MEMBERSHIP,
    211 				       &m, sizeof(m)) < 0)
    212 				LOGERR("IP_DROP_MEMBERSHIP ALLHOSTS");
    213 			ifp->int_state &= ~IS_ALL_HOSTS;
    214 		}
    215 
    216 	} else if (!(ifp->int_state & IS_ALL_HOSTS)) {
    217 		/* start listening to advertisements
    218 		 */
    219 		m.imr_multiaddr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
    220 		if (setsockopt(rdisc_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
    221 			       &m, sizeof(m)) < 0) {
    222 			LOGERR("IP_ADD_MEMBERSHIP ALLHOSTS");
    223 		} else {
    224 			ifp->int_state |= IS_ALL_HOSTS;
    225 		}
    226 	}
    227 
    228 	if (!supplier
    229 	    || (ifp->int_state & IS_NO_ADV_OUT)
    230 	    || !on) {
    231 		/* stop listening to solicitations
    232 		 */
    233 		if (ifp->int_state & IS_ALL_ROUTERS) {
    234 			m.imr_multiaddr.s_addr=htonl(INADDR_ALLROUTERS_GROUP);
    235 			if (setsockopt(rdisc_sock, IPPROTO_IP,
    236 				       IP_DROP_MEMBERSHIP,
    237 				       &m, sizeof(m)) < 0)
    238 				LOGERR("IP_DROP_MEMBERSHIP ALLROUTERS");
    239 			ifp->int_state &= ~IS_ALL_ROUTERS;
    240 		}
    241 
    242 	} else if (!(ifp->int_state & IS_ALL_ROUTERS)) {
    243 		/* start hearing solicitations
    244 		 */
    245 		m.imr_multiaddr.s_addr=htonl(INADDR_ALLROUTERS_GROUP);
    246 		if (setsockopt(rdisc_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
    247 			       &m, sizeof(m)) < 0) {
    248 			LOGERR("IP_ADD_MEMBERSHIP ALLROUTERS");
    249 		} else {
    250 			ifp->int_state |= IS_ALL_ROUTERS;
    251 		}
    252 	}
    253 }
    254 
    255 
    256 /* start supplying routes
    257  */
    258 void
    259 set_supplier(void)
    260 {
    261 	struct interface *ifp;
    262 	struct dr *drp;
    263 
    264 	if (supplier_set)
    265 		return;
    266 
    267 	trace_act("start supplying routes");
    268 
    269 	/* Forget discovered routes.
    270 	 */
    271 	for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
    272 		drp->dr_recv_pref = 0;
    273 		drp->dr_life = 0;
    274 	}
    275 	rdisc_age(0);
    276 
    277 	supplier_set = 1;
    278 	supplier = 1;
    279 
    280 	/* Do not start advertising until we have heard some RIP routes */
    281 	LIM_SEC(rdisc_timer, now.tv_sec+MIN_WAITTIME);
    282 
    283 	/* Switch router discovery multicast groups from soliciting
    284 	 * to advertising.
    285 	 */
    286 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
    287 		if (ifp->int_state & IS_BROKE)
    288 			continue;
    289 		ifp->int_rdisc_cnt = 0;
    290 		ifp->int_rdisc_timer.tv_usec = rdisc_timer.tv_usec;
    291 		ifp->int_rdisc_timer.tv_sec = now.tv_sec+MIN_WAITTIME;
    292 		set_rdisc_mg(ifp, 1);
    293 	}
    294 
    295 	/* get rid of any redirects */
    296 	del_redirects(0,0);
    297 }
    298 
    299 
    300 /* age discovered routes and find the best one
    301  */
    302 void
    303 rdisc_age(naddr bad_gate)
    304 {
    305 	time_t sec;
    306 	struct dr *drp;
    307 
    308 
    309 	/* If only advertising, then do only that. */
    310 	if (supplier) {
    311 		/* If switching from client to server, get rid of old
    312 		 * default routes.
    313 		 */
    314 		if (cur_drp != 0)
    315 			rdisc_sort();
    316 		rdisc_adv();
    317 		return;
    318 	}
    319 
    320 	/* If we are being told about a bad router,
    321 	 * then age the discovered default route, and if there is
    322 	 * no alternative, solicit a replacement.
    323 	 */
    324 	if (bad_gate != 0) {
    325 		/* Look for the bad discovered default route.
    326 		 * Age it and note its interface.
    327 		 */
    328 		for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
    329 			if (drp->dr_ts == 0)
    330 				continue;
    331 
    332 			/* When we find the bad router, then age the route
    333 			 * to at most SUPPLY_INTERVAL.
    334 			 * This is contrary to RFC 1256, but defends against
    335 			 * black holes.
    336 			 */
    337 			if (drp->dr_gate == bad_gate) {
    338 				sec = (now.tv_sec - drp->dr_life
    339 				       + SUPPLY_INTERVAL);
    340 				if (drp->dr_ts > sec) {
    341 					trace_act("age 0.0.0.0 --> %s via %s",
    342 						  naddr_ntoa(drp->dr_gate),
    343 						  drp->dr_ifp->int_name);
    344 					drp->dr_ts = sec;
    345 				}
    346 				break;
    347 			}
    348 		}
    349 	}
    350 
    351 	rdisc_sol();
    352 	rdisc_sort();
    353 
    354 	/* Delete old redirected routes to keep the kernel table small,
    355 	 * and to prevent black holes.  Check that the kernel table
    356 	 * matches the daemon table (i.e. has the default route).
    357 	 * But only if RIP is not running and we are not dealing with
    358 	 * a bad gateway, since otherwise age() will be called.
    359 	 */
    360 	if (rip_sock < 0 && bad_gate == 0)
    361 		age(0);
    362 }
    363 
    364 
    365 /* Zap all routes discovered via an interface that has gone bad
    366  *	This should only be called when !(ifp->int_state & IS_ALIAS)
    367  */
    368 void
    369 if_bad_rdisc(struct interface *ifp)
    370 {
    371 	struct dr *drp;
    372 
    373 	for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
    374 		if (drp->dr_ifp != ifp)
    375 			continue;
    376 		drp->dr_recv_pref = 0;
    377 		drp->dr_ts = 0;
    378 		drp->dr_life = 0;
    379 	}
    380 
    381 	/* make a note to re-solicit, turn RIP on or off, etc. */
    382 	rdisc_timer.tv_sec = 0;
    383 }
    384 
    385 
    386 /* mark an interface ok for router discovering.
    387  */
    388 void
    389 if_ok_rdisc(struct interface *ifp)
    390 {
    391 	set_rdisc_mg(ifp, 1);
    392 
    393 	ifp->int_rdisc_cnt = 0;
    394 	ifp->int_rdisc_timer.tv_sec = now.tv_sec + (supplier
    395 						    ? MIN_WAITTIME
    396 						    : MAX_SOLICITATION_DELAY);
    397 	if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
    398 		rdisc_timer = ifp->int_rdisc_timer;
    399 }
    400 
    401 
    402 /* get rid of a dead discovered router
    403  */
    404 static void
    405 del_rdisc(struct dr *drp)
    406 {
    407 	struct interface *ifp;
    408 	naddr gate;
    409 	int i;
    410 
    411 
    412 	del_redirects(gate = drp->dr_gate, 0);
    413 	drp->dr_ts = 0;
    414 	drp->dr_life = 0;
    415 
    416 
    417 	/* Count the other discovered routes on the interface.
    418 	 */
    419 	i = 0;
    420 	ifp = drp->dr_ifp;
    421 	for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
    422 		if (drp->dr_ts != 0
    423 		    && drp->dr_ifp == ifp)
    424 			i++;
    425 	}
    426 
    427 	/* If that was the last good discovered router on the interface,
    428 	 * then solicit a new one.
    429 	 * This is contrary to RFC 1256, but defends against black holes.
    430 	 */
    431 	if (i != 0) {
    432 		trace_act("discovered router %s via %s"
    433 			  " is bad--have %d remaining",
    434 			  naddr_ntoa(gate), ifp->int_name, i);
    435 	} else if (ifp->int_rdisc_cnt >= MAX_SOLICITATIONS) {
    436 		trace_act("last discovered router %s via %s"
    437 			  " is bad--re-solicit",
    438 			  naddr_ntoa(gate), ifp->int_name);
    439 		ifp->int_rdisc_cnt = 0;
    440 		ifp->int_rdisc_timer.tv_sec = 0;
    441 		rdisc_sol();
    442 	} else {
    443 		trace_act("last discovered router %s via %s"
    444 			  " is bad--wait to solicit",
    445 			  naddr_ntoa(gate), ifp->int_name);
    446 	}
    447 }
    448 
    449 
    450 /* Find the best discovered route,
    451  * and discard stale routers.
    452  */
    453 static void
    454 rdisc_sort(void)
    455 {
    456 	struct dr *drp, *new_drp;
    457 	struct rt_entry *rt;
    458 	struct rt_spare new;
    459 	struct interface *ifp;
    460 	u_int new_st = 0;
    461 	n_long new_pref = 0;
    462 
    463 
    464 	/* Find the best discovered route.
    465 	 */
    466 	new_drp = 0;
    467 	for (drp = drs; drp < &drs[MAX_ADS]; drp++) {
    468 		if (drp->dr_ts == 0)
    469 			continue;
    470 		ifp = drp->dr_ifp;
    471 
    472 		/* Get rid of expired discovered routers.
    473 		 */
    474 		if (drp->dr_ts + drp->dr_life <= now.tv_sec) {
    475 			del_rdisc(drp);
    476 			continue;
    477 		}
    478 
    479 		LIM_SEC(rdisc_timer, drp->dr_ts+drp->dr_life+1);
    480 
    481 		/* Update preference with possibly changed interface
    482 		 * metric.
    483 		 */
    484 		drp->dr_pref = PREF(drp->dr_recv_pref, ifp);
    485 
    486 		/* Prefer the current route to prevent thrashing.
    487 		 * Prefer shorter lifetimes to speed the detection of
    488 		 * bad routers.
    489 		 * Avoid sick interfaces.
    490 		 */
    491 		if (new_drp == 0
    492 		    || (!((new_st ^ drp->dr_ifp->int_state) & IS_SICK)
    493 			&& (new_pref < drp->dr_pref
    494 			    || (new_pref == drp->dr_pref
    495 				&& (drp == cur_drp
    496 				    || (new_drp != cur_drp
    497 					&& new_drp->dr_life > drp->dr_life)))))
    498 		    || ((new_st & IS_SICK)
    499 			&& !(drp->dr_ifp->int_state & IS_SICK))) {
    500 			    new_drp = drp;
    501 			    new_st = drp->dr_ifp->int_state;
    502 			    new_pref = drp->dr_pref;
    503 		}
    504 	}
    505 
    506 	/* switch to a better default route
    507 	 */
    508 	if (new_drp != cur_drp) {
    509 		rt = rtget(RIP_DEFAULT, 0);
    510 
    511 		/* Stop using discovered routes if they are all bad
    512 		 */
    513 		if (new_drp == 0) {
    514 			trace_act("turn off Router Discovery client");
    515 			rdisc_ok = 0;
    516 
    517 			if (rt != 0
    518 			    && (rt->rt_state & RS_RDISC)) {
    519 				new = rt->rt_spares[0];
    520 				new.rts_metric = HOPCNT_INFINITY;
    521 				new.rts_time = now.tv_sec - GARBAGE_TIME;
    522 				rtchange(rt, rt->rt_state & ~RS_RDISC,
    523 					 &new, 0);
    524 				rtswitch(rt, 0);
    525 			}
    526 
    527 		} else {
    528 			if (cur_drp == 0) {
    529 				trace_act("turn on Router Discovery client"
    530 					  " using %s via %s",
    531 					  naddr_ntoa(new_drp->dr_gate),
    532 					  new_drp->dr_ifp->int_name);
    533 				rdisc_ok = 1;
    534 
    535 			} else {
    536 				trace_act("switch Router Discovery from"
    537 					  " %s via %s to %s via %s",
    538 					  naddr_ntoa(cur_drp->dr_gate),
    539 					  cur_drp->dr_ifp->int_name,
    540 					  naddr_ntoa(new_drp->dr_gate),
    541 					  new_drp->dr_ifp->int_name);
    542 			}
    543 
    544 			memset(&new, 0, sizeof(new));
    545 			new.rts_ifp = new_drp->dr_ifp;
    546 			new.rts_gate = new_drp->dr_gate;
    547 			new.rts_router = new_drp->dr_gate;
    548 			new.rts_metric = HOPCNT_INFINITY-1;
    549 			new.rts_time = now.tv_sec;
    550 			if (rt != 0) {
    551 				rtchange(rt, rt->rt_state | RS_RDISC, &new, 0);
    552 			} else {
    553 				rtadd(RIP_DEFAULT, 0, RS_RDISC, &new);
    554 			}
    555 		}
    556 
    557 		cur_drp = new_drp;
    558 	}
    559 
    560 	/* turn RIP on or off */
    561 	if (!rdisc_ok || rip_interfaces > 1) {
    562 		rip_on(0);
    563 	} else {
    564 		rip_off();
    565 	}
    566 }
    567 
    568 
    569 /* handle a single address in an advertisement
    570  */
    571 static void
    572 parse_ad(naddr from,
    573 	 naddr gate,
    574 	 n_long pref,			/* signed and in network order */
    575 	 u_short life,
    576 	 struct interface *ifp)
    577 {
    578 	static struct msg_limit bad_gate;
    579 	struct dr *drp, *new_drp;
    580 
    581 
    582 	if (gate == RIP_DEFAULT
    583 	    || !check_dst(gate)) {
    584 		msglim(&bad_gate, from,"router %s advertising bad gateway %s",
    585 		       naddr_ntoa(from),
    586 		       naddr_ntoa(gate));
    587 		return;
    588 	}
    589 
    590 	/* ignore pointers to ourself and routes via unreachable networks
    591 	 */
    592 	if (ifwithaddr(gate, 1, 0) != 0) {
    593 		trace_pkt("    discard Router Discovery Ad pointing at us");
    594 		return;
    595 	}
    596 	if (!on_net(gate, ifp->int_net, ifp->int_mask)) {
    597 		trace_pkt("    discard Router Discovery Ad"
    598 			  " toward unreachable net");
    599 		return;
    600 	}
    601 
    602 	/* Convert preference to an unsigned value
    603 	 * and later bias it by the metric of the interface.
    604 	 */
    605 	pref = UNSIGN_PREF(ntohl(pref));
    606 
    607 	if (pref == 0 || life < MinMaxAdvertiseInterval) {
    608 		pref = 0;
    609 		life = 0;
    610 	}
    611 
    612 	for (new_drp = 0, drp = drs; drp < &drs[MAX_ADS]; drp++) {
    613 		/* accept new info for a familiar entry
    614 		 */
    615 		if (drp->dr_gate == gate) {
    616 			new_drp = drp;
    617 			break;
    618 		}
    619 
    620 		if (life == 0)
    621 			continue;	/* do not worry about dead ads */
    622 
    623 		if (drp->dr_ts == 0) {
    624 			new_drp = drp;	/* use unused entry */
    625 
    626 		} else if (new_drp == 0) {
    627 			/* look for an entry worse than the new one to
    628 			 * reuse.
    629 			 */
    630 			if ((!(ifp->int_state & IS_SICK)
    631 			     && (drp->dr_ifp->int_state & IS_SICK))
    632 			    || (pref > drp->dr_pref
    633 				&& !((ifp->int_state ^ drp->dr_ifp->int_state)
    634 				     & IS_SICK)))
    635 				new_drp = drp;
    636 
    637 		} else if (new_drp->dr_ts != 0) {
    638 			/* look for the least valuable entry to reuse
    639 			 */
    640 			if ((!(new_drp->dr_ifp->int_state & IS_SICK)
    641 			     && (drp->dr_ifp->int_state & IS_SICK))
    642 			    || (new_drp->dr_pref > drp->dr_pref
    643 				&& !((new_drp->dr_ifp->int_state
    644 				      ^ drp->dr_ifp->int_state)
    645 				     & IS_SICK)))
    646 				new_drp = drp;
    647 		}
    648 	}
    649 
    650 	/* forget it if all of the current entries are better */
    651 	if (new_drp == 0)
    652 		return;
    653 
    654 	new_drp->dr_ifp = ifp;
    655 	new_drp->dr_gate = gate;
    656 	new_drp->dr_ts = now.tv_sec;
    657 	new_drp->dr_life = ntohs(life);
    658 	new_drp->dr_recv_pref = pref;
    659 	/* bias functional preference by metric of the interface */
    660 	new_drp->dr_pref = PREF(pref,ifp);
    661 
    662 	/* after hearing a good advertisement, stop asking
    663 	 */
    664 	if (!(ifp->int_state & IS_SICK))
    665 		ifp->int_rdisc_cnt = MAX_SOLICITATIONS;
    666 }
    667 
    668 
    669 /* Compute the IP checksum
    670  *	This assumes the packet is less than 32K long.
    671  */
    672 static u_short
    673 in_cksum(u_short *p,
    674 	 u_int len)
    675 {
    676 	u_int sum = 0;
    677 	int nwords = len >> 1;
    678 
    679 	while (nwords-- != 0)
    680 		sum += *p++;
    681 
    682 	if (len & 1)
    683 		sum += *(u_char *)p;
    684 
    685 	/* end-around-carry */
    686 	sum = (sum >> 16) + (sum & 0xffff);
    687 	sum += (sum >> 16);
    688 	return (~sum);
    689 }
    690 
    691 
    692 /* Send a router discovery advertisement or solicitation ICMP packet.
    693  */
    694 static void
    695 send_rdisc(union ad_u *p,
    696 	   int p_size,
    697 	   struct interface *ifp,
    698 	   naddr dst,			/* 0 or unicast destination */
    699 	   int	type)			/* 0=unicast, 1=bcast, 2=mcast */
    700 {
    701 	struct sockaddr_in sin;
    702 	int flags;
    703 	char *msg;
    704 	naddr tgt_mcast;
    705 
    706 
    707 	memset(&sin, 0, sizeof(sin));
    708 	sin.sin_addr.s_addr = dst;
    709 	sin.sin_family = AF_INET;
    710 #ifdef _HAVE_SIN_LEN
    711 	sin.sin_len = sizeof(sin);
    712 #endif
    713 	flags = MSG_DONTROUTE;
    714 
    715 	switch (type) {
    716 	case 0:				/* unicast */
    717 	default:
    718 		msg = "Send";
    719 		break;
    720 
    721 	case 1:				/* broadcast */
    722 		if (ifp->int_if_flags & IFF_POINTOPOINT) {
    723 			msg = "Send pt-to-pt";
    724 			sin.sin_addr.s_addr = ifp->int_dstaddr;
    725 		} else {
    726 			msg = "Send broadcast";
    727 			sin.sin_addr.s_addr = ifp->int_brdaddr;
    728 		}
    729 		break;
    730 
    731 	case 2:				/* multicast */
    732 		msg = "Send multicast";
    733 		if (ifp->int_state & IS_DUP) {
    734 			trace_act("abort multicast output via %s"
    735 				  " with duplicate address",
    736 				  ifp->int_name);
    737 			return;
    738 		}
    739 		if (rdisc_sock_mcast != ifp) {
    740 			/* select the right interface. */
    741 #ifdef MCAST_PPP_BUG
    742 			/* Do not specify the primary interface explicitly
    743 			 * if we have the multicast point-to-point kernel
    744 			 * bug, since the kernel will do the wrong thing
    745 			 * if the local address of a point-to-point link
    746 			 * is the same as the address of an ordinary
    747 			 * interface.
    748 			 */
    749 			if (ifp->int_addr == myaddr) {
    750 				tgt_mcast = 0;
    751 			} else
    752 #endif
    753 			tgt_mcast = ifp->int_addr;
    754 			if (0 > setsockopt(rdisc_sock,
    755 					   IPPROTO_IP, IP_MULTICAST_IF,
    756 					   &tgt_mcast, sizeof(tgt_mcast))) {
    757 				LOGERR("setsockopt(rdisc_sock,"
    758 				       "IP_MULTICAST_IF)");
    759 				rdisc_sock_mcast = 0;
    760 				return;
    761 			}
    762 			rdisc_sock_mcast = ifp;
    763 		}
    764 		flags = 0;
    765 		break;
    766 	}
    767 
    768 	if (rdisc_sock < 0)
    769 		get_rdisc_sock();
    770 
    771 	trace_rdisc(msg, ifp->int_addr, sin.sin_addr.s_addr, ifp,
    772 		    p, p_size);
    773 
    774 	if (0 > sendto(rdisc_sock, p, p_size, flags,
    775 		       (struct sockaddr *)&sin, sizeof(sin))) {
    776 		if (ifp == 0 || !(ifp->int_state & IS_BROKE))
    777 			msglog("sendto(%s%s%s): %s",
    778 			       ifp != 0 ? ifp->int_name : "",
    779 			       ifp != 0 ? ", " : "",
    780 			       inet_ntoa(sin.sin_addr),
    781 			       strerror(errno));
    782 		if (ifp != 0)
    783 			if_sick(ifp);
    784 	}
    785 }
    786 
    787 
    788 /* Send an advertisement
    789  */
    790 static void
    791 send_adv(struct interface *ifp,
    792 	 naddr	dst,			/* 0 or unicast destination */
    793 	 int	type)			/* 0=unicast, 1=bcast, 2=mcast */
    794 {
    795 	union ad_u u;
    796 	n_long pref;
    797 
    798 
    799 	memset(&u, 0, sizeof(u.ad));
    800 
    801 	u.ad.icmp_type = ICMP_ROUTERADVERT;
    802 	u.ad.icmp_ad_num = 1;
    803 	u.ad.icmp_ad_asize = sizeof(u.ad.icmp_ad_info[0])/4;
    804 
    805 	u.ad.icmp_ad_life = stopint ? 0 : htons(ifp->int_rdisc_int*3);
    806 
    807 	/* Convert the configured preference to an unsigned value,
    808 	 * bias it by the interface metric, and then send it as a
    809 	 * signed, network byte order value.
    810 	 */
    811 	pref = UNSIGN_PREF(ifp->int_rdisc_pref);
    812 	u.ad.icmp_ad_info[0].icmp_ad_pref = htonl(SIGN_PREF(PREF(pref, ifp)));
    813 
    814 	u.ad.icmp_ad_info[0].icmp_ad_addr = ifp->int_addr;
    815 
    816 	u.ad.icmp_cksum = in_cksum((u_short*)&u.ad, sizeof(u.ad));
    817 
    818 	send_rdisc(&u, sizeof(u.ad), ifp, dst, type);
    819 }
    820 
    821 
    822 /* Advertise for Router Discovery
    823  */
    824 void
    825 rdisc_adv(void)
    826 {
    827 	struct interface *ifp;
    828 
    829 	if (!supplier)
    830 		return;
    831 
    832 	rdisc_timer.tv_sec = now.tv_sec + NEVER;
    833 
    834 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
    835 		if (0 != (ifp->int_state & (IS_NO_ADV_OUT | IS_BROKE)))
    836 			continue;
    837 
    838 		if (!timercmp(&ifp->int_rdisc_timer, &now, >)
    839 		    || stopint) {
    840 			send_adv(ifp, htonl(INADDR_ALLHOSTS_GROUP),
    841 				 (ifp->int_state&IS_BCAST_RDISC) ? 1 : 2);
    842 			ifp->int_rdisc_cnt++;
    843 
    844 			intvl_random(&ifp->int_rdisc_timer,
    845 				     (ifp->int_rdisc_int*3)/4,
    846 				     ifp->int_rdisc_int);
    847 			if (ifp->int_rdisc_cnt < MAX_INITIAL_ADVERTS
    848 			    && (ifp->int_rdisc_timer.tv_sec
    849 				> MAX_INITIAL_ADVERT_INTERVAL)) {
    850 				ifp->int_rdisc_timer.tv_sec
    851 				= MAX_INITIAL_ADVERT_INTERVAL;
    852 			}
    853 			timevaladd(&ifp->int_rdisc_timer, &now);
    854 		}
    855 
    856 		if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
    857 			rdisc_timer = ifp->int_rdisc_timer;
    858 	}
    859 }
    860 
    861 
    862 /* Solicit for Router Discovery
    863  */
    864 void
    865 rdisc_sol(void)
    866 {
    867 	struct interface *ifp;
    868 	union ad_u u;
    869 
    870 
    871 	if (supplier)
    872 		return;
    873 
    874 	rdisc_timer.tv_sec = now.tv_sec + NEVER;
    875 
    876 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
    877 		if (0 != (ifp->int_state & (IS_NO_SOL_OUT | IS_BROKE))
    878 		    || ifp->int_rdisc_cnt >= MAX_SOLICITATIONS)
    879 			continue;
    880 
    881 		if (!timercmp(&ifp->int_rdisc_timer, &now, >)) {
    882 			memset(&u, 0, sizeof(u.so));
    883 			u.so.icmp_type = ICMP_ROUTERSOLICIT;
    884 			u.so.icmp_cksum = in_cksum((u_short*)&u.so,
    885 						   sizeof(u.so));
    886 			send_rdisc(&u, sizeof(u.so), ifp,
    887 				   htonl(INADDR_ALLROUTERS_GROUP),
    888 				   ((ifp->int_state&IS_BCAST_RDISC) ? 1 : 2));
    889 
    890 			if (++ifp->int_rdisc_cnt >= MAX_SOLICITATIONS)
    891 				continue;
    892 
    893 			ifp->int_rdisc_timer.tv_sec = SOLICITATION_INTERVAL;
    894 			ifp->int_rdisc_timer.tv_usec = 0;
    895 			timevaladd(&ifp->int_rdisc_timer, &now);
    896 		}
    897 
    898 		if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >))
    899 			rdisc_timer = ifp->int_rdisc_timer;
    900 	}
    901 }
    902 
    903 
    904 /* check the IP header of a possible Router Discovery ICMP packet */
    905 static struct interface *		/* 0 if bad */
    906 ck_icmp(char	*act,
    907 	naddr	from,
    908 	struct interface *ifp,
    909 	naddr	to,
    910 	union ad_u *p,
    911 	u_int	len)
    912 {
    913 	char *type;
    914 
    915 
    916 	if (p->icmp.icmp_type == ICMP_ROUTERADVERT) {
    917 		type = "advertisement";
    918 	} else if (p->icmp.icmp_type == ICMP_ROUTERSOLICIT) {
    919 		type = "solicitation";
    920 	} else {
    921 		return 0;
    922 	}
    923 
    924 	if (p->icmp.icmp_code != 0) {
    925 		trace_pkt("unrecognized ICMP Router %s code=%d from %s to %s",
    926 			  type, p->icmp.icmp_code,
    927 			  naddr_ntoa(from), naddr_ntoa(to));
    928 		return 0;
    929 	}
    930 
    931 	trace_rdisc(act, from, to, ifp, p, len);
    932 
    933 	if (ifp == 0)
    934 		trace_pkt("unknown interface for router-discovery %s"
    935 			  " from %s to %s",
    936 			  type, naddr_ntoa(from), naddr_ntoa(to));
    937 
    938 	return ifp;
    939 }
    940 
    941 
    942 /* read packets from the router discovery socket
    943  */
    944 void
    945 read_d(void)
    946 {
    947 	static struct msg_limit bad_asize, bad_len;
    948 #ifdef USE_PASSIFNAME
    949 	static struct msg_limit  bad_name;
    950 #endif
    951 	struct sockaddr_in from;
    952 	int n, fromlen, cc, hlen;
    953 	struct {
    954 #ifdef USE_PASSIFNAME
    955 		char	ifname[IFNAMSIZ];
    956 #endif
    957 		union {
    958 			struct ip ip;
    959 			u_short s[512/2];
    960 			u_char	b[512];
    961 		} pkt;
    962 	} buf;
    963 	union ad_u *p;
    964 	n_long *wp;
    965 	struct interface *ifp;
    966 
    967 
    968 	for (;;) {
    969 		fromlen = sizeof(from);
    970 		cc = recvfrom(rdisc_sock, &buf, sizeof(buf), 0,
    971 			      (struct sockaddr*)&from,
    972 			      &fromlen);
    973 		if (cc <= 0) {
    974 			if (cc < 0 && errno != EWOULDBLOCK)
    975 				LOGERR("recvfrom(rdisc_sock)");
    976 			break;
    977 		}
    978 		if (fromlen != sizeof(struct sockaddr_in))
    979 			logbad(1,"impossible recvfrom(rdisc_sock) fromlen=%d",
    980 			       fromlen);
    981 #ifdef USE_PASSIFNAME
    982 		if ((cc -= sizeof(buf.ifname)) < 0)
    983 			logbad(0,"missing USE_PASSIFNAME; only %d bytes",
    984 			       cc+sizeof(buf.ifname));
    985 #endif
    986 
    987 		hlen = buf.pkt.ip.ip_hl << 2;
    988 		if (cc < hlen + ICMP_MINLEN)
    989 			continue;
    990 		p = (union ad_u *)&buf.pkt.b[hlen];
    991 		cc -= hlen;
    992 
    993 #ifdef USE_PASSIFNAME
    994 		ifp = ifwithname(buf.ifname, 0);
    995 		if (ifp == 0)
    996 			msglim(&bad_name, from.sin_addr.s_addr,
    997 			       "impossible rdisc if_ name %.*s",
    998 			       IFNAMSIZ, buf.ifname);
    999 #else
   1000 		/* If we could tell the interface on which a packet from
   1001 		 * address 0 arrived, we could deal with such solicitations.
   1002 		 */
   1003 		ifp = ((from.sin_addr.s_addr == 0)
   1004 		       ? 0 : iflookup(from.sin_addr.s_addr));
   1005 #endif
   1006 		ifp = ck_icmp("Recv", from.sin_addr.s_addr, ifp,
   1007 			      buf.pkt.ip.ip_dst.s_addr, p, cc);
   1008 		if (ifp == 0)
   1009 			continue;
   1010 		if (ifwithaddr(from.sin_addr.s_addr, 0, 0)) {
   1011 			trace_pkt("    "
   1012 				  "discard our own Router Discovery message");
   1013 			continue;
   1014 		}
   1015 
   1016 		switch (p->icmp.icmp_type) {
   1017 		case ICMP_ROUTERADVERT:
   1018 			if (p->ad.icmp_ad_asize*4
   1019 			    < sizeof(p->ad.icmp_ad_info[0])) {
   1020 				msglim(&bad_asize, from.sin_addr.s_addr,
   1021 				       "intolerable rdisc address size=%d",
   1022 				       p->ad.icmp_ad_asize);
   1023 				continue;
   1024 			}
   1025 			if (p->ad.icmp_ad_num == 0) {
   1026 				trace_pkt("    empty?");
   1027 				continue;
   1028 			}
   1029 			if (cc != (sizeof(p->ad) - sizeof(p->ad.icmp_ad_info)
   1030 				   + (p->ad.icmp_ad_num
   1031 				      * sizeof(p->ad.icmp_ad_info[0])))) {
   1032 				msglim(&bad_len, from.sin_addr.s_addr,
   1033 				       "rdisc length %d does not match ad_num"
   1034 				       " %d", cc, p->ad.icmp_ad_num);
   1035 				continue;
   1036 			}
   1037 			if (supplier)
   1038 				continue;
   1039 			if (ifp->int_state & IS_NO_ADV_IN)
   1040 				continue;
   1041 
   1042 			wp = &p->ad.icmp_ad_info[0].icmp_ad_addr;
   1043 			for (n = 0; n < p->ad.icmp_ad_num; n++) {
   1044 				parse_ad(from.sin_addr.s_addr,
   1045 					 wp[0], wp[1],
   1046 					 ntohs(p->ad.icmp_ad_life),
   1047 					 ifp);
   1048 				wp += p->ad.icmp_ad_asize;
   1049 			}
   1050 			break;
   1051 
   1052 
   1053 		case ICMP_ROUTERSOLICIT:
   1054 			if (!supplier)
   1055 				continue;
   1056 			if (ifp->int_state & IS_NO_ADV_OUT)
   1057 				continue;
   1058 			if (stopint)
   1059 				continue;
   1060 
   1061 			/* XXX
   1062 			 * We should handle messages from address 0.
   1063 			 */
   1064 
   1065 			/* Respond with a point-to-point advertisement */
   1066 			send_adv(ifp, from.sin_addr.s_addr, 0);
   1067 			break;
   1068 		}
   1069 	}
   1070 
   1071 	rdisc_sort();
   1072 }
   1073