Home | History | Annotate | Line # | Download | only in routed
output.c revision 1.12
      1 /*	$NetBSD: output.c,v 1.12 1997/02/03 22:03:01 christos 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 static char rcsid[] = "$NetBSD: output.c,v 1.12 1997/02/03 22:03:01 christos Exp $";
     40 #endif
     41 
     42 #include "defs.h"
     43 
     44 
     45 int update_seqno;
     46 
     47 
     48 /* walk the tree of routes with this for output
     49  */
     50 struct {
     51 	struct sockaddr_in to;
     52 	naddr	to_mask;
     53 	naddr	to_net;
     54 	naddr	to_std_mask;
     55 	naddr	to_std_net;
     56 	struct interface *ifp;		/* usually output interface */
     57 	struct auth *a;
     58 	char	metric;			/* adjust metrics by interface */
     59 	int	npackets;
     60 	int	gen_limit;
     61 	u_int	state;
     62 #define	    WS_ST_FLASH	    0x001	/* send only changed routes */
     63 #define	    WS_ST_RIP2_ALL  0x002	/* send full featured RIPv2 */
     64 #define	    WS_ST_AG	    0x004	/* ok to aggregate subnets */
     65 #define	    WS_ST_SUPER_AG  0x008	/* ok to aggregate networks */
     66 #define	    WS_ST_SUB_AG    0x010	/* aggregate subnets in odd case */
     67 #define	    WS_ST_QUERY	    0x020	/* responding to a query */
     68 #define	    WS_ST_TO_ON_NET 0x040	/* sending onto one of our nets */
     69 #define	    WS_ST_DEFAULT   0x080	/* 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 	bzero(wb->n, 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 		bcopy(ap->key, na->au.au_pw, 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 	bcopy(ap->key, na2->au.au_pw, 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 	/* Skip this route if required by split-horizon.
    360 	 */
    361 	if (ag->ag_state & AGS_SPLIT_HZ)
    362 		return;
    363 
    364 	dst_h = ag->ag_dst_h;
    365 	mask = ag->ag_mask;
    366 	v1_mask = ripv1_mask_host(htonl(dst_h),
    367 				  (ws.state & WS_ST_TO_ON_NET) ? ws.ifp : 0);
    368 	i = 0;
    369 
    370 	/* If we are sending RIPv2 packets that cannot (or must not) be
    371 	 * heard by RIPv1 listeners, do not worry about sub- or supernets.
    372 	 * Subnets (from other networks) can only be sent via multicast.
    373 	 * A pair of subnet routes might have been promoted so that they
    374 	 * are legal to send by RIPv1.
    375 	 * If RIPv1 is off, use the multicast buffer.
    376 	 */
    377 	if ((ws.state & WS_ST_RIP2_ALL)
    378 	    || ((ag->ag_state & AGS_RIPV2) && v1_mask != mask)) {
    379 		/* use the RIPv2-only buffer */
    380 		wb = &v2buf;
    381 
    382 	} else {
    383 		/* use the RIPv1-or-RIPv2 buffer */
    384 		wb = &v12buf;
    385 
    386 		/* Convert supernet route into corresponding set of network
    387 		 * routes for RIPv1, but leave non-contiguous netmasks
    388 		 * to ag_check().
    389 		 */
    390 		if (v1_mask > mask
    391 		    && mask + (mask & -mask) == 0) {
    392 			ddst_h = v1_mask & -v1_mask;
    393 			i = (v1_mask & ~mask)/ddst_h;
    394 
    395 			if (i > ws.gen_limit) {
    396 				/* Punt if we would have to generate an
    397 				 * unreasonable number of routes.
    398 				 */
    399 #ifdef DEBUG
    400 				msglog("sending %s to %s as 1 instead"
    401 				       " of %d routes",
    402 				       addrname(htonl(dst_h),mask,1),
    403 				       naddr_ntoa(ws.to.sin_addr.s_addr),
    404 				       i+1);
    405 #endif
    406 				i = 0;
    407 
    408 			} else {
    409 				mask = v1_mask;
    410 				ws.gen_limit -= i;
    411 			}
    412 		}
    413 	}
    414 
    415 	do {
    416 		wb->n->n_family = RIP_AF_INET;
    417 		wb->n->n_dst = htonl(dst_h);
    418 		/* If the route is from router-discovery or we are
    419 		 * shutting down, admit only a bad metric.
    420 		 */
    421 		wb->n->n_metric = ((stopint || ag->ag_metric < 1)
    422 				   ? HOPCNT_INFINITY
    423 				   : ag->ag_metric);
    424 		HTONL(wb->n->n_metric);
    425 		/* Any non-zero bits in the supposedly unused RIPv1 fields
    426 		 * cause the old `routed` to ignore the route.
    427 		 * That means the mask and so forth cannot be sent
    428 		 * in the hybrid RIPv1/RIPv2 mode.
    429 		 */
    430 		if (ws.state & WS_ST_RIP2_ALL) {
    431 			if (ag->ag_nhop != 0
    432 			    && ((ws.state & WS_ST_QUERY)
    433 				|| (ag->ag_nhop != ws.ifp->int_addr
    434 				    && on_net(ag->ag_nhop,
    435 					      ws.ifp->int_net,
    436 					      ws.ifp->int_mask))))
    437 				wb->n->n_nhop = ag->ag_nhop;
    438 			wb->n->n_mask = htonl(mask);
    439 			wb->n->n_tag = ag->ag_tag;
    440 		}
    441 		dst_h += ddst_h;
    442 
    443 		if (++wb->n >= wb->lim)
    444 			supply_write(wb);
    445 	} while (i-- != 0);
    446 }
    447 
    448 
    449 /* supply one route from the table
    450  */
    451 /* ARGSUSED */
    452 static int
    453 walk_supply(struct radix_node *rn, struct walkarg *argp)
    454 {
    455 #define RT ((struct rt_entry *)rn)
    456 	u_short ags;
    457 	char metric, pref;
    458 	naddr dst, nhop;
    459 	struct rt_spare *rts;
    460 	int i;
    461 
    462 
    463 	/* Do not advertise external remote interfaces or passive interfaces.
    464 	 */
    465 	if ((RT->rt_state & RS_IF)
    466 	    && RT->rt_ifp != 0
    467 	    && (RT->rt_ifp->int_if_flags & IS_PASSIVE)
    468 	    && !(RT->rt_state & RS_MHOME))
    469 		return 0;
    470 
    471 	/* If being quiet about our ability to forward, then
    472 	 * do not say anything unless responding to a query,
    473 	 * except about our main interface.
    474 	 */
    475 	if (!supplier && !(ws.state & WS_ST_QUERY)
    476 	    && !(RT->rt_state & RS_MHOME))
    477 		return 0;
    478 
    479 	dst = RT->rt_dst;
    480 
    481 	/* do not collide with the fake default route */
    482 	if (dst == RIP_DEFAULT
    483 	    && (ws.state & WS_ST_DEFAULT))
    484 		return 0;
    485 
    486 	if (RT->rt_state & RS_NET_SYN) {
    487 		if (RT->rt_state & RS_NET_INT) {
    488 			/* Do not send manual synthetic network routes
    489 			 * into the subnet.
    490 			 */
    491 			if (on_net(ws.to.sin_addr.s_addr,
    492 				   ntohl(dst), RT->rt_mask))
    493 				return 0;
    494 
    495 		} else {
    496 			/* Do not send automatic synthetic network routes
    497 			 * if they are not needed becaus no RIPv1 listeners
    498 			 * can hear them.
    499 			 */
    500 			if (ws.state & WS_ST_RIP2_ALL)
    501 				return 0;
    502 
    503 			/* Do not send automatic synthetic network routes to
    504 			 * the real subnet.
    505 			 */
    506 			if (on_net(ws.to.sin_addr.s_addr,
    507 				   ntohl(dst), RT->rt_mask))
    508 				return 0;
    509 		}
    510 		nhop = 0;
    511 
    512 	} else {
    513 		/* Advertise the next hop if this is not a route for one
    514 		 * of our interfaces and the next hop is on the same
    515 		 * network as the target.
    516 		 */
    517 		if (!(RT->rt_state & RS_IF)
    518 		    && RT->rt_gate != myaddr
    519 		    && RT->rt_gate != loopaddr)
    520 			nhop = RT->rt_gate;
    521 		else
    522 			nhop = 0;
    523 	}
    524 
    525 	metric = RT->rt_metric;
    526 	ags = 0;
    527 
    528 	if (RT->rt_state & RS_MHOME) {
    529 		/* retain host route of multi-homed servers */
    530 		;
    531 
    532 	} else if (RT_ISHOST(RT)) {
    533 		/* We should always aggregate the host routes
    534 		 * for the local end of our point-to-point links.
    535 		 * If we are suppressing host routes in general, then do so.
    536 		 * Avoid advertising host routes onto their own network,
    537 		 * where they should be handled by proxy-ARP.
    538 		 */
    539 		if ((RT->rt_state & RS_LOCAL)
    540 		    || ridhosts
    541 		    || (ws.state & WS_ST_SUPER_AG)
    542 		    || on_net(dst, ws.to_net, ws.to_mask))
    543 			ags |= AGS_SUPPRESS;
    544 
    545 		if (ws.state & WS_ST_SUPER_AG)
    546 			ags |= AGS_PROMOTE;
    547 
    548 	} else if (ws.state & WS_ST_AG) {
    549 		/* Aggregate network routes, if we are allowed.
    550 		 */
    551 		ags |= AGS_SUPPRESS;
    552 
    553 		/* Generate supernets if allowed.
    554 		 * If we can be heard by RIPv1 systems, we will
    555 		 * later convert back to ordinary nets.
    556 		 * This unifies dealing with received supernets.
    557 		 */
    558 		if ((RT->rt_state & RS_SUBNET)
    559 		    || (ws.state & WS_ST_SUPER_AG))
    560 			ags |= AGS_PROMOTE;
    561 
    562 	}
    563 
    564 	/* Do not send RIPv1 advertisements of subnets to other
    565 	 * networks. If possible, multicast them by RIPv2.
    566 	 */
    567 	if ((RT->rt_state & RS_SUBNET)
    568 	    && !(ws.state & WS_ST_RIP2_ALL)
    569 	    && !on_net(dst, ws.to_std_net, ws.to_std_mask)) {
    570 		ags |= AGS_RIPV2 | AGS_PROMOTE;
    571 		if (ws.state & WS_ST_SUB_AG)
    572 			ags |= AGS_SUPPRESS;
    573 	}
    574 
    575 	/* Do not send a route back to where it came from, except in
    576 	 * response to a query.  This is "split-horizon".  That means not
    577 	 * advertising back to the same network	and so via the same interface.
    578 	 *
    579 	 * We want to suppress routes that might have been fragmented
    580 	 * from this route by a RIPv1 router and sent back to us, and so we
    581 	 * cannot forget this route here.  Let the split-horizon route
    582 	 * aggregate (suppress) the fragmented routes and then itself be
    583 	 * forgotten.
    584 	 *
    585 	 * Include the routes for both ends of point-to-point interfaces
    586 	 * among those suppressed by split-horizon, since the other side
    587 	 * should knows them as well as we do.
    588 	 *
    589 	 * Notice spare routes with the same metric that we are about to
    590 	 * advertise, to split the horizon on redunant, inactive paths.
    591 	 */
    592 	if (ws.ifp != 0
    593 	    && !(ws.state & WS_ST_QUERY)
    594 	    && (ws.state & WS_ST_TO_ON_NET)
    595 	    && (!(RT->rt_state & RS_IF)
    596 		|| ws.ifp->int_if_flags & IFF_POINTOPOINT)) {
    597 		for (rts = RT->rt_spares, i = NUM_SPARES; i != 0; i--, rts++) {
    598 			if (rts->rts_ifp == ws.ifp
    599 			    && rts->rts_metric <= metric)
    600 				break;
    601 		}
    602 		if (i != 0) {
    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_PROMOTE | AGS_SUPPRESS);
    622 			}
    623 			metric = HOPCNT_INFINITY;
    624 		}
    625 	}
    626 
    627 	/* Keep track of the best metric with which the
    628 	 * route has been advertised recently.
    629 	 */
    630 	if (RT->rt_poison_metric >= metric
    631 	    || RT->rt_poison_time < now_expire) {
    632 		RT->rt_poison_time = now.tv_sec;
    633 		RT->rt_poison_metric = metric;
    634 	}
    635 
    636 	/* Adjust the outgoing metric by the cost of the link.
    637 	 * Avoid aggregation when a route is counting to infinity.
    638 	 */
    639 	pref = RT->rt_poison_metric + ws.metric;
    640 	metric += ws.metric;
    641 
    642 	/* Do not advertise stable routes that will be ignored,
    643 	 * unless we are answering a query.
    644 	 * If the route recently was advertised with a metric that
    645 	 * would have been less than infinity through this interface,
    646 	 * we need to continue to advertise it in order to poison it.
    647 	 */
    648 	if (metric >= HOPCNT_INFINITY) {
    649 		if (!(ws.state & WS_ST_QUERY)
    650 		    && (pref >= HOPCNT_INFINITY
    651 			|| RT->rt_poison_time < now_garbage))
    652 			return 0;
    653 
    654 		metric = HOPCNT_INFINITY;
    655 	}
    656 
    657 	ag_check(dst, RT->rt_mask, 0, nhop, metric, pref,
    658 		 RT->rt_seqno, RT->rt_tag, ags, supply_out);
    659 	return 0;
    660 #undef RT
    661 }
    662 
    663 
    664 /* Supply dst with the contents of the routing tables.
    665  * If this won't fit in one packet, chop it up into several.
    666  */
    667 void
    668 supply(struct sockaddr_in *dst,
    669        struct interface *ifp,		/* output interface */
    670        enum output_type type,
    671        int flash,			/* 1=flash update */
    672        int vers,			/* RIP version */
    673        int passwd_ok)			/* OK to include cleartext password */
    674 {
    675 	struct rt_entry *rt;
    676 	int def_metric;
    677 
    678 
    679 	ws.state = 0;
    680 	ws.gen_limit = 1024;
    681 
    682 	ws.to = *dst;
    683 	ws.to_std_mask = std_mask(ws.to.sin_addr.s_addr);
    684 	ws.to_std_net = ntohl(ws.to.sin_addr.s_addr) & ws.to_std_mask;
    685 
    686 	if (ifp != 0) {
    687 		ws.to_mask = ifp->int_mask;
    688 		ws.to_net = ifp->int_net;
    689 		if (on_net(ws.to.sin_addr.s_addr, ws.to_net, ws.to_mask))
    690 			ws.state |= WS_ST_TO_ON_NET;
    691 
    692 	} else {
    693 		ws.to_mask = ripv1_mask_net(ws.to.sin_addr.s_addr, 0);
    694 		ws.to_net = ntohl(ws.to.sin_addr.s_addr) & ws.to_mask;
    695 		rt = rtfind(dst->sin_addr.s_addr);
    696 		if (rt)
    697 			ifp = rt->rt_ifp;
    698 	}
    699 
    700 	ws.npackets = 0;
    701 	if (flash)
    702 		ws.state |= WS_ST_FLASH;
    703 	if (type == OUT_QUERY)
    704 		ws.state |= WS_ST_QUERY;
    705 
    706 	if ((ws.ifp = ifp) == 0) {
    707 		ws.metric = 1;
    708 	} else {
    709 		/* Adjust the advertised metric by the outgoing interface
    710 		 * metric.
    711 		 */
    712 		ws.metric = ifp->int_metric+1;
    713 	}
    714 
    715 	ripv12_buf.rip.rip_vers = vers;
    716 
    717 	switch (type) {
    718 	case OUT_BROADCAST:
    719 		v2buf.type = ((ifp != 0 && (ifp->int_if_flags & IFF_MULTICAST))
    720 			      ? OUT_MULTICAST
    721 			      : NO_OUT_MULTICAST);
    722 		v12buf.type = OUT_BROADCAST;
    723 		break;
    724 	case OUT_MULTICAST:
    725 		v2buf.type = ((ifp != 0 && (ifp->int_if_flags & IFF_MULTICAST))
    726 			      ? OUT_MULTICAST
    727 			      : NO_OUT_MULTICAST);
    728 		v12buf.type = OUT_BROADCAST;
    729 		break;
    730 	case OUT_UNICAST:
    731 	case OUT_QUERY:
    732 		v2buf.type = (vers == RIPv2) ? type : NO_OUT_RIPV2;
    733 		v12buf.type = type;
    734 		break;
    735 	default:
    736 		v2buf.type = type;
    737 		v12buf.type = type;
    738 		break;
    739 	}
    740 
    741 	if (vers == RIPv2) {
    742 		/* full RIPv2 only if cannot be heard by RIPv1 listeners */
    743 		if (type != OUT_BROADCAST)
    744 			ws.state |= WS_ST_RIP2_ALL;
    745 		if (!(ws.state & WS_ST_TO_ON_NET)) {
    746 			ws.state |= (WS_ST_AG | WS_ST_SUPER_AG);
    747 		} else if (ifp == 0 || !(ifp->int_state & IS_NO_AG)) {
    748 			ws.state |= WS_ST_AG;
    749 			if (type != OUT_BROADCAST
    750 			    && (ifp == 0 || !(ifp->int_state&IS_NO_SUPER_AG)))
    751 				ws.state |= WS_ST_SUPER_AG;
    752 		}
    753 
    754 	} else if (ifp == 0 || !(ifp->int_state & IS_NO_AG)) {
    755 		ws.state |= WS_ST_SUB_AG;
    756 	}
    757 
    758 	ws.a = (vers == RIPv2) ? find_auth(ifp) : 0;
    759 	if (!passwd_ok && ws.a != 0 && ws.a->type == RIP_AUTH_PW)
    760 		ws.a = 0;
    761 	clr_ws_buf(&v12buf,ws.a);
    762 	clr_ws_buf(&v2buf,ws.a);
    763 
    764 	/*  Fake a default route if asked and if there is not already
    765 	 * a better, real default route.
    766 	 */
    767 	if (supplier && (def_metric = ifp->int_d_metric) != 0) {
    768 		if (0 == (rt = rtget(RIP_DEFAULT, 0))
    769 		    || rt->rt_metric+ws.metric >= def_metric) {
    770 			ws.state |= WS_ST_DEFAULT;
    771 			ag_check(0, 0, 0, 0, def_metric, def_metric,
    772 				 0, 0, 0, supply_out);
    773 		} else {
    774 			def_metric = rt->rt_metric+ws.metric;
    775 		}
    776 
    777 		/* If both RIPv2 and the poor-man's router discovery
    778 		 * kludge are on, arrange to advertise an extra
    779 		 * default route via RIPv1.
    780 		 */
    781 		if ((ws.state & WS_ST_RIP2_ALL)
    782 		    && (ifp->int_state & IS_PM_RDISC)) {
    783 			ripv12_buf.rip.rip_vers = RIPv1;
    784 			v12buf.n->n_family = RIP_AF_INET;
    785 			v12buf.n->n_dst = htonl(RIP_DEFAULT);
    786 			v12buf.n->n_metric = htonl(def_metric);
    787 			v12buf.n++;
    788 		}
    789 	}
    790 
    791 	(void)rn_walktree(rhead, walk_supply, 0);
    792 	ag_flush(0,0,supply_out);
    793 
    794 	/* Flush the packet buffers, provided they are not empty and
    795 	 * do not contain only the password.
    796 	 */
    797 	if (v12buf.n != v12buf.base
    798 	    && (v12buf.n > v12buf.base+1
    799 		|| v12buf.base->n_family != RIP_AF_AUTH))
    800 		supply_write(&v12buf);
    801 	if (v2buf.n != v2buf.base
    802 	    && (v2buf.n > v2buf.base+1
    803 		|| v2buf.base->n_family != RIP_AF_AUTH))
    804 		supply_write(&v2buf);
    805 
    806 	/* If we sent nothing and this is an answer to a query, send
    807 	 * an empty buffer.
    808 	 */
    809 	if (ws.npackets == 0
    810 	    && (ws.state & WS_ST_QUERY))
    811 		supply_write(&v12buf);
    812 }
    813 
    814 
    815 /* send all of the routing table or just do a flash update
    816  */
    817 void
    818 rip_bcast(int flash)
    819 {
    820 #ifdef _HAVE_SIN_LEN
    821 	static struct sockaddr_in dst = {sizeof(dst), AF_INET};
    822 #else
    823 	static struct sockaddr_in dst = {AF_INET};
    824 #endif
    825 	struct interface *ifp;
    826 	enum output_type type;
    827 	int vers;
    828 	struct timeval rtime;
    829 
    830 
    831 	need_flash = 0;
    832 	intvl_random(&rtime, MIN_WAITTIME, MAX_WAITTIME);
    833 	no_flash = rtime;
    834 	timevaladd(&no_flash, &now);
    835 
    836 	if (rip_sock < 0)
    837 		return;
    838 
    839 	trace_act("send %s and inhibit dynamic updates for %.3f sec",
    840 		  flash ? "dynamic update" : "all routes",
    841 		  rtime.tv_sec + ((float)rtime.tv_usec)/1000000.0);
    842 
    843 	for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
    844 		/* Skip interfaces not doing RIP.
    845 		 * Do try broken interfaces to see if they have healed.
    846 		 */
    847 		if (IS_RIP_OUT_OFF(ifp->int_state))
    848 			continue;
    849 
    850 		/* skip turned off interfaces */
    851 		if (!iff_alive(ifp->int_if_flags))
    852 			continue;
    853 
    854 		vers = (ifp->int_state & IS_NO_RIPV1_OUT) ? RIPv2 : RIPv1;
    855 
    856 		if (ifp->int_if_flags & IFF_BROADCAST) {
    857 			/* ordinary, hardware interface */
    858 			dst.sin_addr.s_addr = ifp->int_brdaddr;
    859 
    860 			/* If RIPv1 is not turned off, then broadcast so
    861 			 * that RIPv1 listeners can hear.
    862 			 */
    863 			if (vers == RIPv2
    864 			    && (ifp->int_state & IS_NO_RIPV1_OUT)) {
    865 				type = OUT_MULTICAST;
    866 			} else {
    867 				type = OUT_BROADCAST;
    868 			}
    869 
    870 		} else if (ifp->int_if_flags & IFF_POINTOPOINT) {
    871 			/* point-to-point hardware interface */
    872 			dst.sin_addr.s_addr = ifp->int_dstaddr;
    873 			type = OUT_UNICAST;
    874 
    875 		} else if (ifp->int_state & IS_REMOTE) {
    876 			/* remote interface */
    877 			dst.sin_addr.s_addr = ifp->int_addr;
    878 			type = OUT_UNICAST;
    879 
    880 		} else {
    881 			/* ATM, HIPPI, etc. */
    882 			continue;
    883 		}
    884 
    885 		supply(&dst, ifp, type, flash, vers, 1);
    886 	}
    887 
    888 	update_seqno++;			/* all routes are up to date */
    889 }
    890 
    891 
    892 /* Ask for routes
    893  * Do it only once to an interface, and not even after the interface
    894  * was broken and recovered.
    895  */
    896 void
    897 rip_query(void)
    898 {
    899 #ifdef _HAVE_SIN_LEN
    900 	static struct sockaddr_in dst = {sizeof(dst), AF_INET};
    901 #else
    902 	static struct sockaddr_in dst = {AF_INET};
    903 #endif
    904 	struct interface *ifp;
    905 	struct rip buf;
    906 	enum output_type type;
    907 
    908 
    909 	if (rip_sock < 0)
    910 		return;
    911 
    912 	bzero(&buf, sizeof(buf));
    913 
    914 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
    915 		/* Skip interfaces those already queried.
    916 		 * Do not ask via interfaces through which we don't
    917 		 * accept input.  Do not ask via interfaces that cannot
    918 		 * send RIP packets.
    919 		 * Do try broken interfaces to see if they have healed.
    920 		 */
    921 		if (IS_RIP_IN_OFF(ifp->int_state)
    922 		    || ifp->int_query_time != NEVER)
    923 			continue;
    924 
    925 		/* skip turned off interfaces */
    926 		if (!iff_alive(ifp->int_if_flags))
    927 			continue;
    928 
    929 		buf.rip_vers = (ifp->int_state&IS_NO_RIPV1_OUT) ? RIPv2:RIPv1;
    930 		buf.rip_cmd = RIPCMD_REQUEST;
    931 		buf.rip_nets[0].n_family = RIP_AF_UNSPEC;
    932 		buf.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY);
    933 
    934 		if (ifp->int_if_flags & IFF_BROADCAST) {
    935 			/* ordinary, hardware interface */
    936 			dst.sin_addr.s_addr = ifp->int_brdaddr;
    937 			/* if RIPv1 is not turned off, then broadcast so
    938 			 * that RIPv1 listeners can hear.
    939 			 */
    940 			if (buf.rip_vers == RIPv2
    941 			    && (ifp->int_state & IS_NO_RIPV1_OUT)) {
    942 				type = OUT_MULTICAST;
    943 			} else {
    944 				type = OUT_BROADCAST;
    945 			}
    946 
    947 		} else if (ifp->int_if_flags & IFF_POINTOPOINT) {
    948 			/* point-to-point hardware interface */
    949 			dst.sin_addr.s_addr = ifp->int_dstaddr;
    950 			type = OUT_UNICAST;
    951 
    952 		} else if (ifp->int_state & IS_REMOTE) {
    953 			/* remote interface */
    954 			dst.sin_addr.s_addr = ifp->int_addr;
    955 			type = OUT_UNICAST;
    956 
    957 		} else {
    958 			/* ATM, HIPPI, etc. */
    959 			continue;
    960 		}
    961 
    962 		ifp->int_query_time = now.tv_sec+SUPPLY_INTERVAL;
    963 		if (output(type, &dst, ifp, &buf, sizeof(buf)) < 0)
    964 			if_sick(ifp);
    965 	}
    966 }
    967