2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 acknowledgment:
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.
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
33 * $FreeBSD: src/sbin/routed/output.c,v 1.5.2.1 2000/08/14 17:00:03 sheldonh Exp $
34 * $DragonFly: src/sbin/routed/output.c,v 1.5 2005/03/16 21:21:34 cpressey Exp $
39 #if !defined(sgi) && !defined(__NetBSD__)
40 static char sccsid
[] __attribute__((unused
)) = "@(#)output.c 8.1 (Berkeley) 6/5/93";
41 #elif defined(__NetBSD__)
44 #ident "$FreeBSD: src/sbin/routed/output.c,v 1.5.2.1 2000/08/14 17:00:03 sheldonh Exp $"
50 /* walk the tree of routes with this for output
53 struct sockaddr_in to
;
58 struct interface
*ifp
; /* usually output interface */
60 char metric
; /* adjust metrics by interface */
64 #define WS_ST_FLASH 0x001 /* send only changed routes */
65 #define WS_ST_RIP2_ALL 0x002 /* send full featured RIPv2 */
66 #define WS_ST_AG 0x004 /* ok to aggregate subnets */
67 #define WS_ST_SUPER_AG 0x008 /* ok to aggregate networks */
68 #define WS_ST_QUERY 0x010 /* responding to a query */
69 #define WS_ST_TO_ON_NET 0x020 /* sending onto one of our nets */
70 #define WS_ST_DEFAULT 0x040 /* faking a default */
73 /* A buffer for what can be heard by both RIPv1 and RIPv2 listeners */
75 union pkt_buf ripv12_buf
;
77 /* Another for only RIPv2 listeners */
79 union pkt_buf rip_v2_buf
;
86 ripv12_buf
.rip
.rip_cmd
= RIPCMD_RESPONSE
;
87 v12buf
.buf
= &ripv12_buf
.rip
;
88 v12buf
.base
= &v12buf
.buf
->rip_nets
[0];
90 rip_v2_buf
.rip
.rip_cmd
= RIPCMD_RESPONSE
;
91 rip_v2_buf
.rip
.rip_vers
= RIPv2
;
92 v2buf
.buf
= &rip_v2_buf
.rip
;
93 v2buf
.base
= &v2buf
.buf
->rip_nets
[0];
97 /* Send the contents of the global buffer via the non-multicast socket
99 int /* <0 on failure */
100 output(enum output_type type
,
101 struct sockaddr_in
*dst
, /* send to here */
102 struct interface
*ifp
,
104 int size
) /* this many bytes */
106 struct sockaddr_in in
;
115 if (in
.sin_port
== 0)
116 in
.sin_port
= htons(RIP_PORT
);
119 in
.sin_len
= sizeof(in
);
127 msg
= "Answer Query";
129 soc
= ifp
->int_rip_sock
;
134 soc
= ifp
->int_rip_sock
;
135 flags
= MSG_DONTROUTE
;
138 if (ifp
->int_if_flags
& IFF_POINTOPOINT
) {
143 flags
= MSG_DONTROUTE
;
146 if (ifp
->int_if_flags
& IFF_POINTOPOINT
) {
147 msg
= "Send pt-to-pt";
148 } else if (ifp
->int_state
& IS_DUP
) {
149 trace_act("abort multicast output via %s"
150 " with duplicate address",
155 if (rip_sock_mcast
!= ifp
) {
157 /* Do not specify the primary interface
158 * explicitly if we have the multicast
159 * point-to-point kernel bug, since the
160 * kernel will do the wrong thing if the
161 * local address of a point-to-point link
162 * is the same as the address of an ordinary
165 if (ifp
->int_addr
== myaddr
) {
169 tgt_mcast
= ifp
->int_addr
;
170 if (0 > setsockopt(rip_sock
,
171 IPPROTO_IP
, IP_MULTICAST_IF
,
173 sizeof(tgt_mcast
))) {
175 LOGERR("setsockopt(rip_sock,"
181 rip_sock_mcast
= ifp
;
183 in
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
187 case NO_OUT_MULTICAST
:
196 trace_rip(msg
, "to", &in
, ifp
, buf
, size
);
198 res
= sendto(soc
, buf
, size
, flags
,
199 (struct sockaddr
*)&in
, sizeof(in
));
201 && (ifp
== 0 || !(ifp
->int_state
& IS_BROKE
))) {
203 msglog("%s sendto(%s%s%s.%d): %s", msg
,
204 ifp
!= 0 ? ifp
->int_name
: "",
205 ifp
!= 0 ? ", " : "",
206 inet_ntoa(in
.sin_addr
),
216 /* Find the first key for a packet to send.
217 * Try for a key that is eligible and has not expired, but settle for
218 * the last key if they have all expired.
219 * If no key is ready yet, give up.
222 find_auth(struct interface
*ifp
)
224 struct auth
*ap
, *res
;
233 for (i
= 0; i
< MAX_AUTH_KEYS
; i
++, ap
++) {
234 /* stop looking after the last key */
235 if (ap
->type
== RIP_AUTH_NONE
)
238 /* ignore keys that are not ready yet */
239 if ((u_long
)ap
->start
> (u_long
)clk
.tv_sec
)
242 if ((u_long
)ap
->end
< (u_long
)clk
.tv_sec
) {
243 /* note best expired password as a fall-back */
244 if (res
== 0 || (u_long
)ap
->end
> (u_long
)res
->end
)
249 /* note key with the best future */
250 if (res
== 0 || (u_long
)res
->end
< (u_long
)ap
->end
)
258 clr_ws_buf(struct ws_buf
*wb
,
263 wb
->lim
= wb
->base
+ NETS_LEN
;
265 memset(wb
->n
, 0, NETS_LEN
*sizeof(*wb
->n
));
267 /* (start to) install authentication if appropriate
272 na
= (struct netauth
*)wb
->n
;
273 if (ap
->type
== RIP_AUTH_PW
) {
274 na
->a_family
= RIP_AF_AUTH
;
275 na
->a_type
= RIP_AUTH_PW
;
276 memcpy(na
->au
.au_pw
, ap
->key
, sizeof(na
->au
.au_pw
));
279 } else if (ap
->type
== RIP_AUTH_MD5
) {
280 na
->a_family
= RIP_AF_AUTH
;
281 na
->a_type
= RIP_AUTH_MD5
;
282 na
->au
.a_md5
.md5_keyid
= ap
->keyid
;
283 na
->au
.a_md5
.md5_auth_len
= RIP_AUTH_MD5_LEN
;
284 na
->au
.a_md5
.md5_seqno
= htonl(clk
.tv_sec
);
286 wb
->lim
--; /* make room for trailer */
292 end_md5_auth(struct ws_buf
*wb
,
295 struct netauth
*na
, *na2
;
300 na
= (struct netauth
*)wb
->base
;
301 na2
= (struct netauth
*)wb
->n
;
302 len
= (char *)na2
-(char *)wb
->buf
;
303 na2
->a_family
= RIP_AF_AUTH
;
304 na2
->a_type
= htons(1);
305 na
->au
.a_md5
.md5_pkt_len
= htons(len
);
307 MD5Update(&md5_ctx
, (u_char
*)wb
->buf
, len
);
308 MD5Update(&md5_ctx
, ap
->key
, RIP_AUTH_MD5_LEN
);
309 MD5Final(na2
->au
.au_pw
, &md5_ctx
);
317 supply_write(struct ws_buf
*wb
)
319 /* Output multicast only if legal.
320 * If we would multicast and it would be illegal, then discard the
324 case NO_OUT_MULTICAST
:
325 trace_pkt("skip multicast to %s because impossible",
326 naddr_ntoa(ws
.to
.sin_addr
.s_addr
));
331 if (ws
.a
!= 0 && ws
.a
->type
== RIP_AUTH_MD5
)
332 end_md5_auth(wb
,ws
.a
);
333 if (output(wb
->type
, &ws
.to
, ws
.ifp
, wb
->buf
,
334 ((char *)wb
->n
- (char*)wb
->buf
)) < 0
345 /* put an entry into the packet
348 supply_out(struct ag_info
*ag
)
351 naddr mask
, v1_mask
, dst_h
, ddst_h
= 0;
355 /* Skip this route if doing a flash update and it and the routes
356 * it aggregates have not changed recently.
358 if (ag
->ag_seqno
< update_seqno
359 && (ws
.state
& WS_ST_FLASH
))
362 dst_h
= ag
->ag_dst_h
;
364 v1_mask
= ripv1_mask_host(htonl(dst_h
),
365 (ws
.state
& WS_ST_TO_ON_NET
) ? ws
.ifp
: 0);
368 /* If we are sending RIPv2 packets that cannot (or must not) be
369 * heard by RIPv1 listeners, do not worry about sub- or supernets.
370 * Subnets (from other networks) can only be sent via multicast.
371 * A pair of subnet routes might have been promoted so that they
372 * are legal to send by RIPv1.
373 * If RIPv1 is off, use the multicast buffer.
375 if ((ws
.state
& WS_ST_RIP2_ALL
)
376 || ((ag
->ag_state
& AGS_RIPV2
) && v1_mask
!= mask
)) {
377 /* use the RIPv2-only buffer */
381 /* use the RIPv1-or-RIPv2 buffer */
384 /* Convert supernet route into corresponding set of network
385 * routes for RIPv1, but leave non-contiguous netmasks
389 && mask
+ (mask
& -mask
) == 0) {
390 ddst_h
= v1_mask
& -v1_mask
;
391 i
= (v1_mask
& ~mask
)/ddst_h
;
393 if (i
> ws
.gen_limit
) {
394 /* Punt if we would have to generate an
395 * unreasonable number of routes.
398 trace_misc("sending %s-->%s as 1"
399 " instead of %d routes",
400 addrname(htonl(dst_h
), mask
,
402 naddr_ntoa(ws
.to
.sin_addr
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.
420 wb
->n
->n_metric
= ((stopint
|| ag
->ag_metric
< 1)
423 wb
->n
->n_metric
= 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.
429 if (ws
.state
& WS_ST_RIP2_ALL
) {
431 && ((ws
.state
& WS_ST_QUERY
)
432 || (ag
->ag_nhop
!= ws
.ifp
->int_addr
433 && on_net(ag
->ag_nhop
,
436 wb
->n
->n_nhop
= ag
->ag_nhop
;
437 wb
->n
->n_mask
= htonl(mask
);
438 wb
->n
->n_tag
= ag
->ag_tag
;
442 if (++wb
->n
>= wb
->lim
)
448 /* supply one route from the table
452 walk_supply(struct radix_node
*rn
,
453 struct walkarg
*argp UNUSED
)
455 #define RT ((struct rt_entry *)rn)
459 struct rt_spare
*rts
;
463 /* Do not advertise external remote interfaces or passive interfaces.
465 if ((RT
->rt_state
& RS_IF
)
467 && (RT
->rt_ifp
->int_state
& IS_PASSIVE
)
468 && !(RT
->rt_state
& RS_MHOME
))
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.
475 if (!supplier
&& !(ws
.state
& WS_ST_QUERY
)
476 && !(RT
->rt_state
& RS_MHOME
))
481 /* do not collide with the fake default route */
482 if (dst
== RIP_DEFAULT
483 && (ws
.state
& WS_ST_DEFAULT
))
486 if (RT
->rt_state
& RS_NET_SYN
) {
487 if (RT
->rt_state
& RS_NET_INT
) {
488 /* Do not send manual synthetic network routes
491 if (on_net(ws
.to
.sin_addr
.s_addr
,
492 ntohl(dst
), RT
->rt_mask
))
496 /* Do not send automatic synthetic network routes
497 * if they are not needed because no RIPv1 listeners
500 if (ws
.state
& WS_ST_RIP2_ALL
)
503 /* Do not send automatic synthetic network routes to
506 if (on_net(ws
.to
.sin_addr
.s_addr
,
507 ntohl(dst
), RT
->rt_mask
))
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 * The final determination is made by supply_out().
518 if (!(RT
->rt_state
& RS_IF
)
519 && RT
->rt_gate
!= myaddr
520 && RT
->rt_gate
!= loopaddr
)
526 metric
= RT
->rt_metric
;
529 if (RT
->rt_state
& RS_MHOME
) {
530 /* retain host route of multi-homed servers */
533 } else if (RT_ISHOST(RT
)) {
534 /* We should always suppress (into existing network routes)
535 * the host routes for the local end of our point-to-point
537 * If we are suppressing host routes in general, then do so.
538 * Avoid advertising host routes onto their own network,
539 * where they should be handled by proxy-ARP.
541 if ((RT
->rt_state
& RS_LOCAL
)
543 || on_net(dst
, ws
.to_net
, ws
.to_mask
))
546 /* Aggregate stray host routes into network routes if allowed.
547 * We cannot aggregate host routes into small network routes
548 * without confusing RIPv1 listeners into thinking the
549 * network routes are host routes.
551 if ((ws
.state
& WS_ST_AG
)
552 && !(ws
.state
& WS_ST_RIP2_ALL
))
553 ags
|= AGS_AGGREGATE
;
556 /* Always suppress network routes into other, existing
561 /* Generate supernets if allowed.
562 * If we can be heard by RIPv1 systems, we will
563 * later convert back to ordinary nets.
564 * This unifies dealing with received supernets.
566 if ((ws
.state
& WS_ST_AG
)
567 && ((RT
->rt_state
& RS_SUBNET
)
568 || (ws
.state
& WS_ST_SUPER_AG
)))
569 ags
|= AGS_AGGREGATE
;
572 /* Do not send RIPv1 advertisements of subnets to other
573 * networks. If possible, multicast them by RIPv2.
575 if ((RT
->rt_state
& RS_SUBNET
)
576 && !(ws
.state
& WS_ST_RIP2_ALL
)
577 && !on_net(dst
, ws
.to_std_net
, ws
.to_std_mask
))
578 ags
|= AGS_RIPV2
| AGS_AGGREGATE
;
581 /* Do not send a route back to where it came from, except in
582 * response to a query. This is "split-horizon". That means not
583 * advertising back to the same network and so via the same interface.
585 * We want to suppress routes that might have been fragmented
586 * from this route by a RIPv1 router and sent back to us, and so we
587 * cannot forget this route here. Let the split-horizon route
588 * suppress the fragmented routes and then itself be forgotten.
590 * Include the routes for both ends of point-to-point interfaces
591 * among those suppressed by split-horizon, since the other side
592 * should knows them as well as we do.
594 * Notice spare routes with the same metric that we are about to
595 * advertise, to split the horizon on redundant, inactive paths.
598 && !(ws
.state
& WS_ST_QUERY
)
599 && (ws
.state
& WS_ST_TO_ON_NET
)
600 && (!(RT
->rt_state
& RS_IF
)
601 || ws
.ifp
->int_if_flags
& IFF_POINTOPOINT
)) {
602 for (rts
= RT
->rt_spares
, i
= NUM_SPARES
; i
!= 0; i
--, rts
++) {
603 if (rts
->rts_metric
> metric
604 || rts
->rts_ifp
!= ws
.ifp
)
607 /* If we do not mark the route with AGS_SPLIT_HZ here,
608 * it will be poisoned-reverse, or advertised back
609 * toward its source with an infinite metric.
610 * If we have recently advertised the route with a
611 * better metric than we now have, then we should
612 * poison-reverse the route before suppressing it for
615 * In almost all cases, if there is no spare for the
616 * route then it is either old and dead or a brand
617 * new route. If it is brand new, there is no need
618 * for poison-reverse. If it is old and dead, it
619 * is already poisoned.
621 if (RT
->rt_poison_time
< now_expire
622 || RT
->rt_poison_metric
>= metric
623 || RT
->rt_spares
[1].rts_gate
== 0) {
625 ags
&= ~AGS_SUPPRESS
;
627 metric
= HOPCNT_INFINITY
;
632 /* Keep track of the best metric with which the
633 * route has been advertised recently.
635 if (RT
->rt_poison_metric
>= metric
636 || RT
->rt_poison_time
< now_expire
) {
637 RT
->rt_poison_time
= now
.tv_sec
;
638 RT
->rt_poison_metric
= metric
;
641 /* Adjust the outgoing metric by the cost of the link.
642 * Avoid aggregation when a route is counting to infinity.
644 pref
= RT
->rt_poison_metric
+ ws
.metric
;
647 /* Do not advertise stable routes that will be ignored,
648 * unless we are answering a query.
649 * If the route recently was advertised with a metric that
650 * would have been less than infinity through this interface,
651 * we need to continue to advertise it in order to poison it.
653 if (metric
>= HOPCNT_INFINITY
) {
654 if (!(ws
.state
& WS_ST_QUERY
)
655 && (pref
>= HOPCNT_INFINITY
656 || RT
->rt_poison_time
< now_garbage
))
659 metric
= HOPCNT_INFINITY
;
662 ag_check(dst
, RT
->rt_mask
, 0, nhop
, metric
, pref
,
663 RT
->rt_seqno
, RT
->rt_tag
, ags
, supply_out
);
669 /* Supply dst with the contents of the routing tables.
670 * If this won't fit in one packet, chop it up into several.
673 supply(struct sockaddr_in
*dst
,
674 struct interface
*ifp
, /* output interface */
675 enum output_type type
,
676 int flash
, /* 1=flash update */
677 int vers
, /* RIP version */
678 int passwd_ok
) /* OK to include cleartext password */
688 ws
.to_std_mask
= std_mask(ws
.to
.sin_addr
.s_addr
);
689 ws
.to_std_net
= ntohl(ws
.to
.sin_addr
.s_addr
) & ws
.to_std_mask
;
692 ws
.to_mask
= ifp
->int_mask
;
693 ws
.to_net
= ifp
->int_net
;
694 if (on_net(ws
.to
.sin_addr
.s_addr
, ws
.to_net
, ws
.to_mask
))
695 ws
.state
|= WS_ST_TO_ON_NET
;
698 ws
.to_mask
= ripv1_mask_net(ws
.to
.sin_addr
.s_addr
, 0);
699 ws
.to_net
= ntohl(ws
.to
.sin_addr
.s_addr
) & ws
.to_mask
;
700 rt
= rtfind(dst
->sin_addr
.s_addr
);
707 ws
.state
|= WS_ST_FLASH
;
709 if ((ws
.ifp
= ifp
) == 0) {
712 /* Adjust the advertised metric by the outgoing interface
715 ws
.metric
= ifp
->int_metric
+1;
718 ripv12_buf
.rip
.rip_vers
= vers
;
722 if (ifp
->int_if_flags
& IFF_MULTICAST
)
723 v2buf
.type
= OUT_MULTICAST
;
725 v2buf
.type
= NO_OUT_MULTICAST
;
726 v12buf
.type
= OUT_BROADCAST
;
730 ws
.state
|= WS_ST_QUERY
;
734 v2buf
.type
= (vers
== RIPv2
) ? type
: NO_OUT_RIPV2
;
738 case NO_OUT_MULTICAST
:
740 break; /* no output */
744 /* full RIPv2 only if cannot be heard by RIPv1 listeners */
745 if (type
!= OUT_BROADCAST
)
746 ws
.state
|= WS_ST_RIP2_ALL
;
747 if ((ws
.state
& WS_ST_QUERY
)
748 || !(ws
.state
& WS_ST_TO_ON_NET
)) {
749 ws
.state
|= (WS_ST_AG
| WS_ST_SUPER_AG
);
750 } else if (ifp
== 0 || !(ifp
->int_state
& IS_NO_AG
)) {
751 ws
.state
|= WS_ST_AG
;
752 if (type
!= OUT_BROADCAST
754 || !(ifp
->int_state
& IS_NO_SUPER_AG
)))
755 ws
.state
|= WS_ST_SUPER_AG
;
759 ws
.a
= (vers
== RIPv2
) ? find_auth(ifp
) : 0;
760 if (!passwd_ok
&& ws
.a
!= 0 && ws
.a
->type
== RIP_AUTH_PW
)
762 clr_ws_buf(&v12buf
,ws
.a
);
763 clr_ws_buf(&v2buf
,ws
.a
);
765 /* Fake a default route if asked and if there is not already
766 * a better, real default route.
768 if (supplier
&& (def_metric
= ifp
->int_d_metric
) != 0) {
769 if (0 == (rt
= rtget(RIP_DEFAULT
, 0))
770 || rt
->rt_metric
+ws
.metric
>= def_metric
) {
771 ws
.state
|= WS_ST_DEFAULT
;
772 ag_check(0, 0, 0, 0, def_metric
, def_metric
,
773 0, 0, 0, supply_out
);
775 def_metric
= rt
->rt_metric
+ws
.metric
;
778 /* If both RIPv2 and the poor-man's router discovery
779 * kludge are on, arrange to advertise an extra
780 * default route via RIPv1.
782 if ((ws
.state
& WS_ST_RIP2_ALL
)
783 && (ifp
->int_state
& IS_PM_RDISC
)) {
784 ripv12_buf
.rip
.rip_vers
= RIPv1
;
785 v12buf
.n
->n_family
= RIP_AF_INET
;
786 v12buf
.n
->n_dst
= htonl(RIP_DEFAULT
);
787 v12buf
.n
->n_metric
= htonl(def_metric
);
792 rn_walktree(rhead
, walk_supply
, 0);
793 ag_flush(0,0,supply_out
);
795 /* Flush the packet buffers, provided they are not empty and
796 * do not contain only the password.
798 if (v12buf
.n
!= v12buf
.base
799 && (v12buf
.n
> v12buf
.base
+1
800 || v12buf
.base
->n_family
!= RIP_AF_AUTH
))
801 supply_write(&v12buf
);
802 if (v2buf
.n
!= v2buf
.base
803 && (v2buf
.n
> v2buf
.base
+1
804 || v2buf
.base
->n_family
!= RIP_AF_AUTH
))
805 supply_write(&v2buf
);
807 /* If we sent nothing and this is an answer to a query, send
811 && (ws
.state
& WS_ST_QUERY
))
812 supply_write(&v12buf
);
816 /* send all of the routing table or just do a flash update
822 static struct sockaddr_in dst
= {sizeof(dst
), AF_INET
, 0, {0}, {0}};
824 static struct sockaddr_in dst
= {AF_INET
};
826 struct interface
*ifp
;
827 enum output_type type
;
829 struct timeval rtime
;
833 intvl_random(&rtime
, MIN_WAITTIME
, MAX_WAITTIME
);
835 timevaladd(&no_flash
, &now
);
840 trace_act("send %s and inhibit dynamic updates for %.3f sec",
841 flash
? "dynamic update" : "all routes",
842 rtime
.tv_sec
+ ((float)rtime
.tv_usec
)/1000000.0);
844 for (ifp
= ifnet
; ifp
!= 0; ifp
= ifp
->int_next
) {
845 /* Skip interfaces not doing RIP.
846 * Do try broken interfaces to see if they have healed.
848 if (IS_RIP_OUT_OFF(ifp
->int_state
))
851 /* skip turned off interfaces */
852 if (!iff_up(ifp
->int_if_flags
))
855 vers
= (ifp
->int_state
& IS_NO_RIPV1_OUT
) ? RIPv2
: RIPv1
;
857 if (ifp
->int_if_flags
& IFF_BROADCAST
) {
858 /* ordinary, hardware interface */
859 dst
.sin_addr
.s_addr
= ifp
->int_brdaddr
;
862 && !(ifp
->int_state
& IS_NO_RIP_MCAST
)) {
863 type
= OUT_MULTICAST
;
865 type
= OUT_BROADCAST
;
868 } else if (ifp
->int_if_flags
& IFF_POINTOPOINT
) {
869 /* point-to-point hardware interface */
870 dst
.sin_addr
.s_addr
= ifp
->int_dstaddr
;
873 } else if (ifp
->int_state
& IS_REMOTE
) {
874 /* remote interface */
875 dst
.sin_addr
.s_addr
= ifp
->int_addr
;
879 /* ATM, HIPPI, etc. */
883 supply(&dst
, ifp
, type
, flash
, vers
, 1);
886 update_seqno
++; /* all routes are up to date */
891 * Do it only once to an interface, and not even after the interface
892 * was broken and recovered.
898 static struct sockaddr_in dst
= {sizeof(dst
), AF_INET
, 0, {0}, {0}};
900 static struct sockaddr_in dst
= {AF_INET
};
902 struct interface
*ifp
;
904 enum output_type type
;
910 memset(&buf
, 0, sizeof(buf
));
912 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
) {
913 /* Skip interfaces those already queried.
914 * Do not ask via interfaces through which we don't
915 * accept input. Do not ask via interfaces that cannot
917 * Do try broken interfaces to see if they have healed.
919 if (IS_RIP_IN_OFF(ifp
->int_state
)
920 || ifp
->int_query_time
!= NEVER
)
923 /* skip turned off interfaces */
924 if (!iff_up(ifp
->int_if_flags
))
927 buf
.rip_vers
= (ifp
->int_state
&IS_NO_RIPV1_OUT
) ? RIPv2
:RIPv1
;
928 buf
.rip_cmd
= RIPCMD_REQUEST
;
929 buf
.rip_nets
[0].n_family
= RIP_AF_UNSPEC
;
930 buf
.rip_nets
[0].n_metric
= htonl(HOPCNT_INFINITY
);
932 /* Send a RIPv1 query only if allowed and if we will
933 * listen to RIPv1 routers.
935 if ((ifp
->int_state
& IS_NO_RIPV1_OUT
)
936 || (ifp
->int_state
& IS_NO_RIPV1_IN
)) {
937 buf
.rip_vers
= RIPv2
;
939 buf
.rip_vers
= RIPv1
;
942 if (ifp
->int_if_flags
& IFF_BROADCAST
) {
943 /* ordinary, hardware interface */
944 dst
.sin_addr
.s_addr
= ifp
->int_brdaddr
;
946 /* Broadcast RIPv1 queries and RIPv2 queries
947 * when the hardware cannot multicast.
949 if (buf
.rip_vers
== RIPv2
950 && (ifp
->int_if_flags
& IFF_MULTICAST
)
951 && !(ifp
->int_state
& IS_NO_RIP_MCAST
)) {
952 type
= OUT_MULTICAST
;
954 type
= OUT_BROADCAST
;
957 } else if (ifp
->int_if_flags
& IFF_POINTOPOINT
) {
958 /* point-to-point hardware interface */
959 dst
.sin_addr
.s_addr
= ifp
->int_dstaddr
;
962 } else if (ifp
->int_state
& IS_REMOTE
) {
963 /* remote interface */
964 dst
.sin_addr
.s_addr
= ifp
->int_addr
;
968 /* ATM, HIPPI, etc. */
972 ifp
->int_query_time
= now
.tv_sec
+SUPPLY_INTERVAL
;
973 if (output(type
, &dst
, ifp
, &buf
, sizeof(buf
)) < 0)