Home | History | Annotate | Line # | Download | only in routed
output.c revision 1.15
      1 /*	$NetBSD: output.c,v 1.15 1998/06/02 18:02:55 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1988, 1993
      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 acknowledgement:
     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[] = "@(#)output.c	8.1 (Berkeley) 6/5/93";
     38 #elif defined(__NetBSD__)
     39 #include <sys/cdefs.h>
     40 __RCSID("$NetBSD: output.c,v 1.15 1998/06/02 18:02:55 thorpej Exp $");
     41 #endif
     42 
     43 #include "defs.h"
     44 
     45 
     46 int update_seqno;
     47 
     48 
     49 /* walk the tree of routes with this for output
     50  */
     51 struct {
     52 	struct sockaddr_in to;
     53 	naddr	to_mask;
     54 	naddr	to_net;
     55 	naddr	to_std_mask;
     56 	naddr	to_std_net;
     57 	struct interface *ifp;		/* usually output interface */
     58 	struct auth *a;
     59 	char	metric;			/* adjust metrics by interface */
     60 	int	npackets;
     61 	int	gen_limit;
     62 	u_int	state;
     63 #define	    WS_ST_FLASH	    0x001	/* send only changed routes */
     64 #define	    WS_ST_RIP2_ALL  0x002	/* send full featured RIPv2 */
     65 #define	    WS_ST_AG	    0x004	/* ok to aggregate subnets */
     66 #define	    WS_ST_SUPER_AG  0x008	/* ok to aggregate networks */
     67 #define	    WS_ST_QUERY	    0x010	/* responding to a query */
     68 #define	    WS_ST_TO_ON_NET 0x020	/* sending onto one of our nets */
     69 #define	    WS_ST_DEFAULT   0x040	/* faking a default */
     70 } ws;
     71 
     72 /* A buffer for what can be heard by both RIPv1 and RIPv2 listeners */
     73 struct ws_buf v12buf;
     74 union pkt_buf ripv12_buf;
     75 
     76 /* Another for only RIPv2 listeners */
     77 struct ws_buf v2buf;
     78 union pkt_buf rip_v2_buf;
     79 
     80 
     81 
     82 void
     83 bufinit(void)
     84 {
     85 	ripv12_buf.rip.rip_cmd = RIPCMD_RESPONSE;
     86 	v12buf.buf = &ripv12_buf.rip;
     87 	v12buf.base = &v12buf.buf->rip_nets[0];
     88 
     89 	rip_v2_buf.rip.rip_cmd = RIPCMD_RESPONSE;
     90 	rip_v2_buf.rip.rip_vers = RIPv2;
     91 	v2buf.buf = &rip_v2_buf.rip;
     92 	v2buf.base = &v2buf.buf->rip_nets[0];
     93 }
     94 
     95 
     96 /* Send the contents of the global buffer via the non-multicast socket
     97  */
     98 int					/* <0 on failure */
     99 output(enum output_type type,
    100        struct sockaddr_in *dst,		/* send to here */
    101        struct interface *ifp,
    102        struct rip *buf,
    103        int size)			/* this many bytes */
    104 {
    105 	struct sockaddr_in sin;
    106 	int flags;
    107 	char *msg;
    108 	int res;
    109 	naddr tgt_mcast;
    110 	int soc;
    111 	int serrno;
    112 
    113 	sin = *dst;
    114 	if (sin.sin_port == 0)
    115 		sin.sin_port = htons(RIP_PORT);
    116 #ifdef _HAVE_SIN_LEN
    117 	if (sin.sin_len == 0)
    118 		sin.sin_len = sizeof(sin);
    119 #endif
    120 
    121 	soc = rip_sock;
    122 	flags = 0;
    123 
    124 	switch (type) {
    125 	case OUT_QUERY:
    126 		msg = "Answer Query";
    127 		if (soc < 0)
    128 			soc = ifp->int_rip_sock;
    129 		break;
    130 	case OUT_UNICAST:
    131 		msg = "Send";
    132 		if (soc < 0)
    133 			soc = ifp->int_rip_sock;
    134 		flags = MSG_DONTROUTE;
    135 		break;
    136 	case OUT_BROADCAST:
    137 		if (ifp->int_if_flags & IFF_POINTOPOINT) {
    138 			msg = "Send";
    139 		} else {
    140 			msg = "Send bcast";
    141 		}
    142 		flags = MSG_DONTROUTE;
    143 		break;
    144 	case OUT_MULTICAST:
    145 		if (ifp->int_if_flags & IFF_POINTOPOINT) {
    146 			msg = "Send pt-to-pt";
    147 		} else if (ifp->int_state & IS_DUP) {
    148 			trace_act("abort multicast output via %s"
    149 				  " with duplicate address",
    150 				  ifp->int_name);
    151 			return 0;
    152 		} else {
    153 			msg = "Send mcast";
    154 			if (rip_sock_mcast != ifp) {
    155 #ifdef MCAST_PPP_BUG
    156 				/* Do not specifiy the primary interface
    157 				 * explicitly if we have the multicast
    158 				 * point-to-point kernel bug, since the
    159 				 * kernel will do the wrong thing if the
    160 				 * local address of a point-to-point link
    161 				 * is the same as the address of an ordinary
    162 				 * interface.
    163 				 */
    164 				if (ifp->int_addr == myaddr) {
    165 					tgt_mcast = 0;
    166 				} else
    167 #endif
    168 				tgt_mcast = ifp->int_addr;
    169 				if (0 > setsockopt(rip_sock,
    170 						   IPPROTO_IP, IP_MULTICAST_IF,
    171 						   &tgt_mcast,
    172 						   sizeof(tgt_mcast))) {
    173 					serrno = errno;
    174 					LOGERR("setsockopt(rip_sock,"
    175 					       "IP_MULTICAST_IF)");
    176 					errno = serrno;
    177 					ifp = 0;
    178 					return -1;
    179 				}
    180 				rip_sock_mcast = ifp;
    181 			}
    182 			sin.sin_addr.s_addr = htonl(INADDR_RIP_GROUP);
    183 		}
    184 		break;
    185 
    186 	case NO_OUT_MULTICAST:
    187 	case NO_OUT_RIPV2:
    188 	default:
    189 #ifdef DEBUG
    190 		abort();
    191 #endif
    192 		return -1;
    193 	}
    194 
    195 	trace_rip(msg, "to", &sin, ifp, buf, size);
    196 
    197 	res = sendto(soc, buf, size, flags,
    198 		     (struct sockaddr *)&sin, sizeof(sin));
    199 	if (res < 0
    200 	    && (ifp == 0 || !(ifp->int_state & IS_BROKE))) {
    201 		serrno = errno;
    202 		msglog("%s sendto(%s%s%s.%d): %s", msg,
    203 		       ifp != 0 ? ifp->int_name : "",
    204 		       ifp != 0 ? ", " : "",
    205 		       inet_ntoa(sin.sin_addr),
    206 		       ntohs(sin.sin_port),
    207 		       strerror(errno));
    208 		errno = serrno;
    209 	}
    210 
    211 	return res;
    212 }
    213 
    214 
    215 /* Find the first key for a packet to send.
    216  * Try for a key that is eligable and has not expired, but settle for
    217  * the last key if they have all expired.
    218  * If no key is ready yet, give up.
    219  */
    220 struct auth *
    221 find_auth(struct interface *ifp)
    222 {
    223 	struct auth *ap, *res;
    224 	int i;
    225 
    226 
    227 	if (ifp == 0)
    228 		return 0;
    229 
    230 	res = 0;
    231 	ap = ifp->int_auth;
    232 	for (i = 0; i < MAX_AUTH_KEYS; i++, ap++) {
    233 		/* stop looking after the last key */
    234 		if (ap->type == RIP_AUTH_NONE)
    235 			break;
    236 
    237 		/* ignore keys that are not ready yet */
    238 		if ((u_long)ap->start > (u_long)clk.tv_sec)
    239 			continue;
    240 
    241 		if ((u_long)ap->end < (u_long)clk.tv_sec) {
    242 			/* note best expired password as a fall-back */
    243 			if (res == 0 || (u_long)ap->end > (u_long)res->end)
    244 				res = ap;
    245 			continue;
    246 		}
    247 
    248 		/* note key with the best future */
    249 		if (res == 0 || (u_long)res->end < (u_long)ap->end)
    250 			res = ap;
    251 	}
    252 	return res;
    253 }
    254 
    255 
    256 void
    257 clr_ws_buf(struct ws_buf *wb,
    258 	   struct auth *ap)
    259 {
    260 	struct netauth *na;
    261 
    262 	wb->lim = wb->base + NETS_LEN;
    263 	wb->n = wb->base;
    264 	memset(wb->n, 0, NETS_LEN*sizeof(*wb->n));
    265 
    266 	/* install authentication if appropriate
    267 	 */
    268 	if (ap == 0)
    269 		return;
    270 	na = (struct netauth*)wb->n;
    271 	if (ap->type == RIP_AUTH_PW) {
    272 		na->a_family = RIP_AF_AUTH;
    273 		na->a_type = RIP_AUTH_PW;
    274 		memmove(na->au.au_pw, ap->key, sizeof(na->au.au_pw));
    275 		wb->n++;
    276 
    277 	} else if (ap->type ==  RIP_AUTH_MD5) {
    278 		na->a_family = RIP_AF_AUTH;
    279 		na->a_type = RIP_AUTH_MD5;
    280 		na->au.a_md5.md5_keyid = ap->keyid;
    281 		na->au.a_md5.md5_auth_len = RIP_AUTH_PW_LEN;
    282 		na->au.a_md5.md5_seqno = clk.tv_sec;
    283 		wb->n++;
    284 		wb->lim--;		/* make room for trailer */
    285 	}
    286 }
    287 
    288 
    289 void
    290 end_md5_auth(struct ws_buf *wb,
    291 	     struct auth *ap)
    292 {
    293 	struct netauth *na, *na2;
    294 	MD5_CTX md5_ctx;
    295 
    296 
    297 	na = (struct netauth*)wb->base;
    298 	na2 = (struct netauth*)wb->n;
    299 	na2->a_family = RIP_AF_AUTH;
    300 	na2->a_type = 1;
    301 	memmove(na2->au.au_pw, ap->key, sizeof(na2->au.au_pw));
    302 	na->au.a_md5.md5_pkt_len = (char *)na2-(char *)(na+1);
    303 	MD5Init(&md5_ctx);
    304 	MD5Update(&md5_ctx, (u_char *)na,
    305 		  (char *)(na2+1) - (char *)na);
    306 	MD5Final(na2->au.au_pw, &md5_ctx);
    307 	wb->n++;
    308 }
    309 
    310 
    311 /* Send the buffer
    312  */
    313 static void
    314 supply_write(struct ws_buf *wb)
    315 {
    316 	/* Output multicast only if legal.
    317 	 * If we would multcast and it would be illegal, then discard the
    318 	 * packet.
    319 	 */
    320 	switch (wb->type) {
    321 	case NO_OUT_MULTICAST:
    322 		trace_pkt("skip multicast to %s because impossible",
    323 			  naddr_ntoa(ws.to.sin_addr.s_addr));
    324 		break;
    325 	case NO_OUT_RIPV2:
    326 		break;
    327 	default:
    328 		if (ws.a != 0 && ws.a->type == RIP_AUTH_MD5)
    329 			end_md5_auth(wb,ws.a);
    330 		if (output(wb->type, &ws.to, ws.ifp, wb->buf,
    331 			   ((char *)wb->n - (char*)wb->buf)) < 0
    332 		    && ws.ifp != 0)
    333 			if_sick(ws.ifp);
    334 		ws.npackets++;
    335 		break;
    336 	}
    337 
    338 	clr_ws_buf(wb,ws.a);
    339 }
    340 
    341 
    342 /* put an entry into the packet
    343  */
    344 static void
    345 supply_out(struct ag_info *ag)
    346 {
    347 	int i;
    348 	naddr mask, v1_mask, dst_h, ddst_h = 0;
    349 	struct ws_buf *wb;
    350 
    351 
    352 	/* Skip this route if doing a flash update and it and the routes
    353 	 * it aggregates have not changed recently.
    354 	 */
    355 	if (ag->ag_seqno < update_seqno
    356 	    && (ws.state & WS_ST_FLASH))
    357 		return;
    358 
    359 	dst_h = ag->ag_dst_h;
    360 	mask = ag->ag_mask;
    361 	v1_mask = ripv1_mask_host(htonl(dst_h),
    362 				  (ws.state & WS_ST_TO_ON_NET) ? ws.ifp : 0);
    363 	i = 0;
    364 
    365 	/* If we are sending RIPv2 packets that cannot (or must not) be
    366 	 * heard by RIPv1 listeners, do not worry about sub- or supernets.
    367 	 * Subnets (from other networks) can only be sent via multicast.
    368 	 * A pair of subnet routes might have been promoted so that they
    369 	 * are legal to send by RIPv1.
    370 	 * If RIPv1 is off, use the multicast buffer.
    371 	 */
    372 	if ((ws.state & WS_ST_RIP2_ALL)
    373 	    || ((ag->ag_state & AGS_RIPV2) && v1_mask != mask)) {
    374 		/* use the RIPv2-only buffer */
    375 		wb = &v2buf;
    376 
    377 	} else {
    378 		/* use the RIPv1-or-RIPv2 buffer */
    379 		wb = &v12buf;
    380 
    381 		/* Convert supernet route into corresponding set of network
    382 		 * routes for RIPv1, but leave non-contiguous netmasks
    383 		 * to ag_check().
    384 		 */
    385 		if (v1_mask > mask
    386 		    && mask + (mask & -mask) == 0) {
    387 			ddst_h = v1_mask & -v1_mask;
    388 			i = (v1_mask & ~mask)/ddst_h;
    389 
    390 			if (i > ws.gen_limit) {
    391 				/* Punt if we would have to generate an
    392 				 * unreasonable number of routes.
    393 				 */
    394 				if (TRACECONTENTS)
    395 					trace_misc("sending %s-->%s as 1"
    396 						   " instead of %d routes",
    397 						   addrname(htonl(dst_h), mask,
    398 							1),
    399 						   naddr_ntoa(ws.to.sin_addr
    400 							.s_addr),
    401 						   i+1);
    402 				i = 0;
    403 
    404 			} else {
    405 				mask = v1_mask;
    406 				ws.gen_limit -= i;
    407 			}
    408 		}
    409 	}
    410 
    411 	do {
    412 		wb->n->n_family = RIP_AF_INET;
    413 		wb->n->n_dst = htonl(dst_h);
    414 		/* If the route is from router-discovery or we are
    415 		 * shutting down, admit only a bad metric.
    416 		 */
    417 		wb->n->n_metric = ((stopint || ag->ag_metric < 1)
    418 				   ? HOPCNT_INFINITY
    419 				   : ag->ag_metric);
    420 		HTONL(wb->n->n_metric);
    421 		/* Any non-zero bits in the supposedly unused RIPv1 fields
    422 		 * cause the old `routed` to ignore the route.
    423 		 * That means the mask and so forth cannot be sent
    424 		 * in the hybrid RIPv1/RIPv2 mode.
    425 		 */
    426 		if (ws.state & WS_ST_RIP2_ALL) {
    427 			if (ag->ag_nhop != 0
    428 			    && ((ws.state & WS_ST_QUERY)
    429 				|| (ag->ag_nhop != ws.ifp->int_addr
    430 				    && on_net(ag->ag_nhop,
    431 					      ws.ifp->int_net,
    432 					      ws.ifp->int_mask))))
    433 				wb->n->n_nhop = ag->ag_nhop;
    434 			wb->n->n_mask = htonl(mask);
    435 			wb->n->n_tag = ag->ag_tag;
    436 		}
    437 		dst_h += ddst_h;
    438 
    439 		if (++wb->n >= wb->lim)
    440 			supply_write(wb);
    441 	} while (i-- != 0);
    442 }
    443 
    444 
    445 /* supply one route from the table
    446  */
    447 /* ARGSUSED */
    448 static int
    449 walk_supply(struct radix_node *rn, struct walkarg *argp)
    450 {
    451 #define RT ((struct rt_entry *)rn)
    452 	u_short ags;
    453 	char metric, pref;
    454 	naddr dst, nhop;
    455 	struct rt_spare *rts;
    456 	int i;
    457 
    458 
    459 	/* Do not advertise external remote interfaces or passive interfaces.
    460 	 */
    461 	if ((RT->rt_state & RS_IF)
    462 	    && RT->rt_ifp != 0
    463 	    && (RT->rt_ifp->int_if_flags & IS_PASSIVE)
    464 	    && !(RT->rt_state & RS_MHOME))
    465 		return 0;
    466 
    467 	/* If being quiet about our ability to forward, then
    468 	 * do not say anything unless responding to a query,
    469 	 * except about our main interface.
    470 	 */
    471 	if (!supplier && !(ws.state & WS_ST_QUERY)
    472 	    && !(RT->rt_state & RS_MHOME))
    473 		return 0;
    474 
    475 	dst = RT->rt_dst;
    476 
    477 	/* do not collide with the fake default route */
    478 	if (dst == RIP_DEFAULT
    479 	    && (ws.state & WS_ST_DEFAULT))
    480 		return 0;
    481 
    482 	if (RT->rt_state & RS_NET_SYN) {
    483 		if (RT->rt_state & RS_NET_INT) {
    484 			/* Do not send manual synthetic network routes
    485 			 * into the subnet.
    486 			 */
    487 			if (on_net(ws.to.sin_addr.s_addr,
    488 				   ntohl(dst), RT->rt_mask))
    489 				return 0;
    490 
    491 		} else {
    492 			/* Do not send automatic synthetic network routes
    493 			 * if they are not needed because no RIPv1 listeners
    494 			 * can hear them.
    495 			 */
    496 			if (ws.state & WS_ST_RIP2_ALL)
    497 				return 0;
    498 
    499 			/* Do not send automatic synthetic network routes to
    500 			 * the real subnet.
    501 			 */
    502 			if (on_net(ws.to.sin_addr.s_addr,
    503 				   ntohl(dst), RT->rt_mask))
    504 				return 0;
    505 		}
    506 		nhop = 0;
    507 
    508 	} else {
    509 		/* Advertise the next hop if this is not a route for one
    510 		 * of our interfaces and the next hop is on the same
    511 		 * network as the target.
    512 		 * The final determination is made by supply_out().
    513 		 */
    514 		if (!(RT->rt_state & RS_IF)
    515 		    && RT->rt_gate != myaddr
    516 		    && RT->rt_gate != loopaddr)
    517 			nhop = RT->rt_gate;
    518 		else
    519 			nhop = 0;
    520 	}
    521 
    522 	metric = RT->rt_metric;
    523 	ags = 0;
    524 
    525 	if (RT->rt_state & RS_MHOME) {
    526 		/* retain host route of multi-homed servers */
    527 		;
    528 
    529 	} else if (RT_ISHOST(RT)) {
    530 		/* We should always suppress (into existing network routes)
    531 		 * the host routes for the local end of our point-to-point
    532 		 * links.
    533 		 * If we are suppressing host routes in general, then do so.
    534 		 * Avoid advertising host routes onto their own network,
    535 		 * where they should be handled by proxy-ARP.
    536 		 */
    537 		if ((RT->rt_state & RS_LOCAL)
    538 		    || ridhosts
    539 		    || on_net(dst, ws.to_net, ws.to_mask))
    540 			ags |= AGS_SUPPRESS;
    541 
    542 		/* Aggregate stray host routes into network routes if allowed.
    543 		 * We cannot aggregate host routes into small network routes
    544 		 * without confusing RIPv1 listeners into thinking the
    545 		 * network routes are host routes.
    546 		 */
    547 		if ((ws.state & WS_ST_AG)
    548 		    && !(ws.state & WS_ST_RIP2_ALL))
    549 			ags |= AGS_AGGREGATE;
    550 
    551 	} else {
    552 		/* Always suppress network routes into other, existing
    553 		 * network routes
    554 		 */
    555 		ags |= AGS_SUPPRESS;
    556 
    557 		/* Generate supernets if allowed.
    558 		 * If we can be heard by RIPv1 systems, we will
    559 		 * later convert back to ordinary nets.
    560 		 * This unifies dealing with received supernets.
    561 		 */
    562 		if ((ws.state & WS_ST_AG)
    563 		    && ((RT->rt_state & RS_SUBNET)
    564 			|| (ws.state & WS_ST_SUPER_AG)))
    565 			ags |= AGS_AGGREGATE;
    566 	}
    567 
    568 	/* Do not send RIPv1 advertisements of subnets to other
    569 	 * networks. If possible, multicast them by RIPv2.
    570 	 */
    571 	if ((RT->rt_state & RS_SUBNET)
    572 	    && !(ws.state & WS_ST_RIP2_ALL)
    573 	    && !on_net(dst, ws.to_std_net, ws.to_std_mask))
    574 		ags |= AGS_RIPV2 | AGS_AGGREGATE;
    575 
    576 
    577 	/* Do not send a route back to where it came from, except in
    578 	 * response to a query.  This is "split-horizon".  That means not
    579 	 * advertising back to the same network	and so via the same interface.
    580 	 *
    581 	 * We want to suppress routes that might have been fragmented
    582 	 * from this route by a RIPv1 router and sent back to us, and so we
    583 	 * cannot forget this route here.  Let the split-horizon route
    584 	 * suppress the fragmented routes and then itself be forgotten.
    585 	 *
    586 	 * Include the routes for both ends of point-to-point interfaces
    587 	 * among those suppressed by split-horizon, since the other side
    588 	 * should knows them as well as we do.
    589 	 *
    590 	 * Notice spare routes with the same metric that we are about to
    591 	 * advertise, to split the horizon on redunant, inactive paths.
    592 	 */
    593 	if (ws.ifp != 0
    594 	    && !(ws.state & WS_ST_QUERY)
    595 	    && (ws.state & WS_ST_TO_ON_NET)
    596 	    && (!(RT->rt_state & RS_IF)
    597 		|| ws.ifp->int_if_flags & IFF_POINTOPOINT)) {
    598 		for (rts = RT->rt_spares, i = NUM_SPARES; i != 0; i--, rts++) {
    599 			if (rts->rts_metric > metric
    600 			    || rts->rts_ifp != ws.ifp)
    601 				continue;
    602 
    603 			/* If we do not mark the route with AGS_SPLIT_HZ here,
    604 			 * it will be poisoned-reverse, or advertised back
    605 			 * toward its source with an infinite metric.
    606 			 * If we have recently advertised the route with a
    607 			 * better metric than we now have, then we should
    608 			 * poison-reverse the route before suppressing it for
    609 			 * split-horizon.
    610 			 *
    611 			 * In almost all cases, if there is no spare for the
    612 			 * route then it is either old and dead or a brand
    613 			 * new route. If it is brand new, there is no need
    614 			 * for poison-reverse. If it is old and dead, it
    615 			 * is already poisoned.
    616 			 */
    617 			if (RT->rt_poison_time < now_expire
    618 			    || RT->rt_poison_metric >= metric
    619 			    || RT->rt_spares[1].rts_gate == 0) {
    620 				ags |= AGS_SPLIT_HZ;
    621 				ags &= ~AGS_SUPPRESS;
    622 			}
    623 			metric = HOPCNT_INFINITY;
    624 			break;
    625 		}
    626 	}
    627 
    628 	/* Keep track of the best metric with which the
    629 	 * route has been advertised recently.
    630 	 */
    631 	if (RT->rt_poison_metric >= metric
    632 	    || RT->rt_poison_time < now_expire) {
    633 		RT->rt_poison_time = now.tv_sec;
    634 		RT->rt_poison_metric = metric;
    635 	}
    636 
    637 	/* Adjust the outgoing metric by the cost of the link.
    638 	 * Avoid aggregation when a route is counting to infinity.
    639 	 */
    640 	pref = RT->rt_poison_metric + ws.metric;
    641 	metric += ws.metric;
    642 
    643 	/* Do not advertise stable routes that will be ignored,
    644 	 * unless we are answering a query.
    645 	 * If the route recently was advertised with a metric that
    646 	 * would have been less than infinity through this interface,
    647 	 * we need to continue to advertise it in order to poison it.
    648 	 */
    649 	if (metric >= HOPCNT_INFINITY) {
    650 		if (!(ws.state & WS_ST_QUERY)
    651 		    && (pref >= HOPCNT_INFINITY
    652 			|| RT->rt_poison_time < now_garbage))
    653 			return 0;
    654 
    655 		metric = HOPCNT_INFINITY;
    656 	}
    657 
    658 	ag_check(dst, RT->rt_mask, 0, nhop, metric, pref,
    659 		 RT->rt_seqno, RT->rt_tag, ags, supply_out);
    660 	return 0;
    661 #undef RT
    662 }
    663 
    664 
    665 /* Supply dst with the contents of the routing tables.
    666  * If this won't fit in one packet, chop it up into several.
    667  */
    668 void
    669 supply(struct sockaddr_in *dst,
    670        struct interface *ifp,		/* output interface */
    671        enum output_type type,
    672        int flash,			/* 1=flash update */
    673        int vers,			/* RIP version */
    674        int passwd_ok)			/* OK to include cleartext password */
    675 {
    676 	struct rt_entry *rt;
    677 	int def_metric;
    678 
    679 
    680 	ws.state = 0;
    681 	ws.gen_limit = 1024;
    682 
    683 	ws.to = *dst;
    684 	ws.to_std_mask = std_mask(ws.to.sin_addr.s_addr);
    685 	ws.to_std_net = ntohl(ws.to.sin_addr.s_addr) & ws.to_std_mask;
    686 
    687 	if (ifp != 0) {
    688 		ws.to_mask = ifp->int_mask;
    689 		ws.to_net = ifp->int_net;
    690 		if (on_net(ws.to.sin_addr.s_addr, ws.to_net, ws.to_mask))
    691 			ws.state |= WS_ST_TO_ON_NET;
    692 
    693 	} else {
    694 		ws.to_mask = ripv1_mask_net(ws.to.sin_addr.s_addr, 0);
    695 		ws.to_net = ntohl(ws.to.sin_addr.s_addr) & ws.to_mask;
    696 		rt = rtfind(dst->sin_addr.s_addr);
    697 		if (rt)
    698 			ifp = rt->rt_ifp;
    699 	}
    700 
    701 	ws.npackets = 0;
    702 	if (flash)
    703 		ws.state |= WS_ST_FLASH;
    704 
    705 	if ((ws.ifp = ifp) == 0) {
    706 		ws.metric = 1;
    707 	} else {
    708 		/* Adjust the advertised metric by the outgoing interface
    709 		 * metric.
    710 		 */
    711 		ws.metric = ifp->int_metric+1;
    712 	}
    713 
    714 	ripv12_buf.rip.rip_vers = vers;
    715 
    716 	switch (type) {
    717 	case OUT_MULTICAST:
    718 		if (ifp->int_if_flags & IFF_MULTICAST)
    719 			v2buf.type = OUT_MULTICAST;
    720 		else
    721 			v2buf.type = NO_OUT_MULTICAST;
    722 		v12buf.type = OUT_BROADCAST;
    723 		break;
    724 
    725 	case OUT_QUERY:
    726 		ws.state |= WS_ST_QUERY;
    727 		/* fall through */
    728 	case OUT_BROADCAST:
    729 	case OUT_UNICAST:
    730 		v2buf.type = (vers == RIPv2) ? type : NO_OUT_RIPV2;
    731 		v12buf.type = type;
    732 		break;
    733 
    734 	case NO_OUT_MULTICAST:
    735 	case NO_OUT_RIPV2:
    736 		break;			/* no output */
    737 	}
    738 
    739 	if (vers == RIPv2) {
    740 		/* full RIPv2 only if cannot be heard by RIPv1 listeners */
    741 		if (type != OUT_BROADCAST)
    742 			ws.state |= WS_ST_RIP2_ALL;
    743 		if ((ws.state & WS_ST_QUERY)
    744 		    || !(ws.state & WS_ST_TO_ON_NET)) {
    745 			ws.state |= (WS_ST_AG | WS_ST_SUPER_AG);
    746 		} else if (ifp == 0 || !(ifp->int_state & IS_NO_AG)) {
    747 			ws.state |= WS_ST_AG;
    748 			if (type != OUT_BROADCAST
    749 			    && (ifp == 0
    750 				|| !(ifp->int_state & IS_NO_SUPER_AG)))
    751 				ws.state |= WS_ST_SUPER_AG;
    752 		}
    753 	}
    754 
    755 	ws.a = (vers == RIPv2) ? find_auth(ifp) : 0;
    756 	if (!passwd_ok && ws.a != 0 && ws.a->type == RIP_AUTH_PW)
    757 		ws.a = 0;
    758 	clr_ws_buf(&v12buf,ws.a);
    759 	clr_ws_buf(&v2buf,ws.a);
    760 
    761 	/*  Fake a default route if asked and if there is not already
    762 	 * a better, real default route.
    763 	 */
    764 	if (supplier && (def_metric = ifp->int_d_metric) != 0) {
    765 		if (0 == (rt = rtget(RIP_DEFAULT, 0))
    766 		    || rt->rt_metric+ws.metric >= def_metric) {
    767 			ws.state |= WS_ST_DEFAULT;
    768 			ag_check(0, 0, 0, 0, def_metric, def_metric,
    769 				 0, 0, 0, supply_out);
    770 		} else {
    771 			def_metric = rt->rt_metric+ws.metric;
    772 		}
    773 
    774 		/* If both RIPv2 and the poor-man's router discovery
    775 		 * kludge are on, arrange to advertise an extra
    776 		 * default route via RIPv1.
    777 		 */
    778 		if ((ws.state & WS_ST_RIP2_ALL)
    779 		    && (ifp->int_state & IS_PM_RDISC)) {
    780 			ripv12_buf.rip.rip_vers = RIPv1;
    781 			v12buf.n->n_family = RIP_AF_INET;
    782 			v12buf.n->n_dst = htonl(RIP_DEFAULT);
    783 			v12buf.n->n_metric = htonl(def_metric);
    784 			v12buf.n++;
    785 		}
    786 	}
    787 
    788 	(void)rn_walktree(rhead, walk_supply, 0);
    789 	ag_flush(0,0,supply_out);
    790 
    791 	/* Flush the packet buffers, provided they are not empty and
    792 	 * do not contain only the password.
    793 	 */
    794 	if (v12buf.n != v12buf.base
    795 	    && (v12buf.n > v12buf.base+1
    796 		|| v12buf.base->n_family != RIP_AF_AUTH))
    797 		supply_write(&v12buf);
    798 	if (v2buf.n != v2buf.base
    799 	    && (v2buf.n > v2buf.base+1
    800 		|| v2buf.base->n_family != RIP_AF_AUTH))
    801 		supply_write(&v2buf);
    802 
    803 	/* If we sent nothing and this is an answer to a query, send
    804 	 * an empty buffer.
    805 	 */
    806 	if (ws.npackets == 0
    807 	    && (ws.state & WS_ST_QUERY))
    808 		supply_write(&v12buf);
    809 }
    810 
    811 
    812 /* send all of the routing table or just do a flash update
    813  */
    814 void
    815 rip_bcast(int flash)
    816 {
    817 #ifdef _HAVE_SIN_LEN
    818 	static struct sockaddr_in dst = {sizeof(dst), AF_INET};
    819 #else
    820 	static struct sockaddr_in dst = {AF_INET};
    821 #endif
    822 	struct interface *ifp;
    823 	enum output_type type;
    824 	int vers;
    825 	struct timeval rtime;
    826 
    827 
    828 	need_flash = 0;
    829 	intvl_random(&rtime, MIN_WAITTIME, MAX_WAITTIME);
    830 	no_flash = rtime;
    831 	timevaladd(&no_flash, &now);
    832 
    833 	if (rip_sock < 0)
    834 		return;
    835 
    836 	trace_act("send %s and inhibit dynamic updates for %.3f sec",
    837 		  flash ? "dynamic update" : "all routes",
    838 		  rtime.tv_sec + ((float)rtime.tv_usec)/1000000.0);
    839 
    840 	for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
    841 		/* Skip interfaces not doing RIP.
    842 		 * Do try broken interfaces to see if they have healed.
    843 		 */
    844 		if (IS_RIP_OUT_OFF(ifp->int_state))
    845 			continue;
    846 
    847 		/* skip turned off interfaces */
    848 		if (!iff_up(ifp->int_if_flags))
    849 			continue;
    850 
    851 		vers = (ifp->int_state & IS_NO_RIPV1_OUT) ? RIPv2 : RIPv1;
    852 
    853 		if (ifp->int_if_flags & IFF_BROADCAST) {
    854 			/* ordinary, hardware interface */
    855 			dst.sin_addr.s_addr = ifp->int_brdaddr;
    856 
    857 			if (vers == RIPv2
    858 			    && !(ifp->int_state  & IS_NO_RIP_MCAST)) {
    859 				type = OUT_MULTICAST;
    860 			} else {
    861 				type = OUT_BROADCAST;
    862 			}
    863 
    864 		} else if (ifp->int_if_flags & IFF_POINTOPOINT) {
    865 			/* point-to-point hardware interface */
    866 			dst.sin_addr.s_addr = ifp->int_dstaddr;
    867 			type = OUT_UNICAST;
    868 
    869 		} else if (ifp->int_state & IS_REMOTE) {
    870 			/* remote interface */
    871 			dst.sin_addr.s_addr = ifp->int_addr;
    872 			type = OUT_UNICAST;
    873 
    874 		} else {
    875 			/* ATM, HIPPI, etc. */
    876 			continue;
    877 		}
    878 
    879 		supply(&dst, ifp, type, flash, vers, 1);
    880 	}
    881 
    882 	update_seqno++;			/* all routes are up to date */
    883 }
    884 
    885 
    886 /* Ask for routes
    887  * Do it only once to an interface, and not even after the interface
    888  * was broken and recovered.
    889  */
    890 void
    891 rip_query(void)
    892 {
    893 #ifdef _HAVE_SIN_LEN
    894 	static struct sockaddr_in dst = {sizeof(dst), AF_INET};
    895 #else
    896 	static struct sockaddr_in dst = {AF_INET};
    897 #endif
    898 	struct interface *ifp;
    899 	struct rip buf;
    900 	enum output_type type;
    901 
    902 
    903 	if (rip_sock < 0)
    904 		return;
    905 
    906 	memset(&buf, 0, sizeof(buf));
    907 
    908 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
    909 		/* Skip interfaces those already queried.
    910 		 * Do not ask via interfaces through which we don't
    911 		 * accept input.  Do not ask via interfaces that cannot
    912 		 * send RIP packets.
    913 		 * Do try broken interfaces to see if they have healed.
    914 		 */
    915 		if (IS_RIP_IN_OFF(ifp->int_state)
    916 		    || ifp->int_query_time != NEVER)
    917 			continue;
    918 
    919 		/* skip turned off interfaces */
    920 		if (!iff_up(ifp->int_if_flags))
    921 			continue;
    922 
    923 		buf.rip_vers = (ifp->int_state&IS_NO_RIPV1_OUT) ? RIPv2:RIPv1;
    924 		buf.rip_cmd = RIPCMD_REQUEST;
    925 		buf.rip_nets[0].n_family = RIP_AF_UNSPEC;
    926 		buf.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY);
    927 
    928 		/* Send a RIPv1 query only if allowed and if we will
    929 		 * listen to RIPv1 routers.
    930 		 */
    931 		if ((ifp->int_state & IS_NO_RIPV1_OUT)
    932 		    || (ifp->int_state & IS_NO_RIPV1_IN)) {
    933 			buf.rip_vers = RIPv2;
    934 		} else {
    935 			buf.rip_vers = RIPv1;
    936 		}
    937 
    938 		if (ifp->int_if_flags & IFF_BROADCAST) {
    939 			/* ordinary, hardware interface */
    940 			dst.sin_addr.s_addr = ifp->int_brdaddr;
    941 
    942 			/* Broadcast RIPv1 queries and RIPv2 queries
    943 			 * when the hardware cannot multicast.
    944 			 */
    945 			if (buf.rip_vers == RIPv2
    946 			    && (ifp->int_if_flags & IFF_MULTICAST)
    947 			    && !(ifp->int_state  & IS_NO_RIP_MCAST)) {
    948 				type = OUT_MULTICAST;
    949 			} else {
    950 				type = OUT_BROADCAST;
    951 			}
    952 
    953 		} else if (ifp->int_if_flags & IFF_POINTOPOINT) {
    954 			/* point-to-point hardware interface */
    955 			dst.sin_addr.s_addr = ifp->int_dstaddr;
    956 			type = OUT_UNICAST;
    957 
    958 		} else if (ifp->int_state & IS_REMOTE) {
    959 			/* remote interface */
    960 			dst.sin_addr.s_addr = ifp->int_addr;
    961 			type = OUT_UNICAST;
    962 
    963 		} else {
    964 			/* ATM, HIPPI, etc. */
    965 			continue;
    966 		}
    967 
    968 		ifp->int_query_time = now.tv_sec+SUPPLY_INTERVAL;
    969 		if (output(type, &dst, ifp, &buf, sizeof(buf)) < 0)
    970 			if_sick(ifp);
    971 	}
    972 }
    973