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