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