1 /* $NetBSD: output.c,v 1.24 2006/03/21 21:50:44 christos Exp $ */
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
39 __RCSID("$NetBSD: output.c,v 1.24 2006/03/21 21:50:44 christos Exp $");
40 #elif defined(__FreeBSD__)
43 __RCSID("Revision: 2.27 ");
44 #ident "Revision: 2.27 "
51 /* walk the tree of routes with this for output
54 struct sockaddr_in to
;
59 struct interface
*ifp
; /* usually output interface */
61 char metric
; /* adjust metrics by interface */
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 */
74 /* A buffer for what can be heard by both RIPv1 and RIPv2 listeners */
76 union pkt_buf ripv12_buf
;
78 /* Another for only RIPv2 listeners */
80 union pkt_buf rip_v2_buf
;
87 ripv12_buf
.rip
.rip_cmd
= RIPCMD_RESPONSE
;
88 v12buf
.buf
= &ripv12_buf
.rip
;
89 v12buf
.base
= &v12buf
.buf
->rip_nets
[0];
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];
98 /* Send the contents of the global buffer via the non-multicast socket
100 int /* <0 on failure */
101 output(enum output_type type
,
102 struct sockaddr_in
*dst
, /* send to here */
103 struct interface
*ifp
,
105 int size
) /* this many bytes */
107 struct sockaddr_in osin
;
116 if (osin
.sin_port
== 0)
117 osin
.sin_port
= htons(RIP_PORT
);
119 if (osin
.sin_len
== 0)
120 osin
.sin_len
= sizeof(osin
);
128 msg
= "Answer Query";
130 soc
= ifp
->int_rip_sock
;
135 soc
= ifp
->int_rip_sock
;
136 flags
= MSG_DONTROUTE
;
139 if (ifp
->int_if_flags
& IFF_POINTOPOINT
) {
144 flags
= MSG_DONTROUTE
;
147 if ((ifp
->int_if_flags
& IFF_POINTOPOINT
)
148 && !(ifp
->int_if_flags
& IFF_MULTICAST
)) {
149 msg
= "Send pt-to-pt";
150 } else if (ifp
->int_state
& IS_DUP
) {
151 trace_act("abort multicast output via %s"
152 " with duplicate address",
157 if (rip_sock_mcast
!= ifp
) {
159 /* specify ifindex */
160 tgt_mcast
= htonl(ifp
->int_index
);
163 /* Do not specify the primary interface
164 * explicitly if we have the multicast
165 * point-to-point kernel bug, since the
166 * kernel will do the wrong thing if the
167 * local address of a point-to-point link
168 * is the same as the address of an ordinary
171 if (ifp
->int_addr
== myaddr
) {
175 tgt_mcast
= ifp
->int_addr
;
177 if (0 > setsockopt(rip_sock
,
178 IPPROTO_IP
, IP_MULTICAST_IF
,
180 sizeof(tgt_mcast
))) {
182 LOGERR("setsockopt(rip_sock,"
188 rip_sock_mcast
= ifp
;
190 osin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
194 case NO_OUT_MULTICAST
:
203 trace_rip(msg
, "to", &osin
, ifp
, buf
, size
);
205 res
= sendto(soc
, buf
, size
, flags
,
206 (struct sockaddr
*)&osin
, sizeof(osin
));
208 && (ifp
== 0 || !(ifp
->int_state
& IS_BROKE
))) {
210 msglog("%s sendto(%s%s%s.%d): %s", msg
,
211 ifp
!= 0 ? ifp
->int_name
: "",
212 ifp
!= 0 ? ", " : "",
213 inet_ntoa(osin
.sin_addr
),
214 ntohs(osin
.sin_port
),
223 /* Find the first key for a packet to send.
224 * Try for a key that is eligible and has not expired, but settle for
225 * the last key if they have all expired.
226 * If no key is ready yet, give up.
229 find_auth(struct interface
*ifp
)
231 struct auth
*ap
, *res
;
240 for (i
= 0; i
< MAX_AUTH_KEYS
; i
++, ap
++) {
241 /* stop looking after the last key */
242 if (ap
->type
== RIP_AUTH_NONE
)
245 /* ignore keys that are not ready yet */
246 if ((u_long
)ap
->start
> (u_long
)clk
.tv_sec
)
249 if ((u_long
)ap
->end
< (u_long
)clk
.tv_sec
) {
250 /* note best expired password as a fall-back */
251 if (res
== 0 || (u_long
)ap
->end
> (u_long
)res
->end
)
256 /* note key with the best future */
257 if (res
== 0 || (u_long
)res
->end
< (u_long
)ap
->end
)
265 clr_ws_buf(struct ws_buf
*wb
,
270 wb
->lim
= wb
->base
+ NETS_LEN
;
272 memset(wb
->n
, 0, NETS_LEN
*sizeof(*wb
->n
));
274 /* (start to) install authentication if appropriate
279 na
= (struct netauth
*)wb
->n
;
280 if (ap
->type
== RIP_AUTH_PW
) {
281 na
->a_family
= RIP_AF_AUTH
;
282 na
->a_type
= RIP_AUTH_PW
;
283 memcpy(na
->au
.au_pw
, ap
->key
, sizeof(na
->au
.au_pw
));
286 } else if (ap
->type
== RIP_AUTH_MD5
) {
287 na
->a_family
= RIP_AF_AUTH
;
288 na
->a_type
= RIP_AUTH_MD5
;
289 na
->au
.a_md5
.md5_keyid
= ap
->keyid
;
290 na
->au
.a_md5
.md5_auth_len
= RIP_AUTH_MD5_KEY_LEN
;
291 na
->au
.a_md5
.md5_seqno
= htonl(clk
.tv_sec
);
293 wb
->lim
--; /* make room for trailer */
299 end_md5_auth(struct ws_buf
*wb
,
302 struct netauth
*na
, *na2
;
307 na
= (struct netauth
*)wb
->base
;
308 na2
= (struct netauth
*)wb
->n
;
309 len
= (char *)na2
-(char *)wb
->buf
;
310 na2
->a_family
= RIP_AF_AUTH
;
311 na2
->a_type
= htons(1);
312 na
->au
.a_md5
.md5_pkt_len
= htons(len
);
314 MD5Update(&md5_ctx
, (u_char
*)wb
->buf
, len
+ RIP_AUTH_MD5_HASH_XTRA
);
315 MD5Update(&md5_ctx
, ap
->key
, RIP_AUTH_MD5_KEY_LEN
);
316 MD5Final(na2
->au
.au_pw
, &md5_ctx
);
324 supply_write(struct ws_buf
*wb
)
326 /* Output multicast only if legal.
327 * If we would multicast and it would be illegal, then discard the
331 case NO_OUT_MULTICAST
:
332 trace_pkt("skip multicast to %s because impossible",
333 naddr_ntoa(ws
.to
.sin_addr
.s_addr
));
338 if (ws
.a
!= 0 && ws
.a
->type
== RIP_AUTH_MD5
)
339 end_md5_auth(wb
,ws
.a
);
340 if (output(wb
->type
, &ws
.to
, ws
.ifp
, wb
->buf
,
341 ((char *)wb
->n
- (char*)wb
->buf
)) < 0
352 /* put an entry into the packet
355 supply_out(struct ag_info
*ag
)
358 naddr mask
, v1_mask
, dst_h
, ddst_h
= 0;
362 /* Skip this route if doing a flash update and it and the routes
363 * it aggregates have not changed recently.
365 if (ag
->ag_seqno
< update_seqno
366 && (ws
.state
& WS_ST_FLASH
))
369 dst_h
= ag
->ag_dst_h
;
371 v1_mask
= ripv1_mask_host(htonl(dst_h
),
372 (ws
.state
& WS_ST_TO_ON_NET
) ? ws
.ifp
: 0);
375 /* If we are sending RIPv2 packets that cannot (or must not) be
376 * heard by RIPv1 listeners, do not worry about sub- or supernets.
377 * Subnets (from other networks) can only be sent via multicast.
378 * A pair of subnet routes might have been promoted so that they
379 * are legal to send by RIPv1.
380 * If RIPv1 is off, use the multicast buffer.
382 if ((ws
.state
& WS_ST_RIP2_ALL
)
383 || ((ag
->ag_state
& AGS_RIPV2
) && v1_mask
!= mask
)) {
384 /* use the RIPv2-only buffer */
388 /* use the RIPv1-or-RIPv2 buffer */
391 /* Convert supernet route into corresponding set of network
392 * routes for RIPv1, but leave non-contiguous netmasks
396 && mask
+ (mask
& -mask
) == 0) {
397 ddst_h
= v1_mask
& -v1_mask
;
398 i
= (v1_mask
& ~mask
)/ddst_h
;
400 if (i
> ws
.gen_limit
) {
401 /* Punt if we would have to generate an
402 * unreasonable number of routes.
405 trace_misc("sending %s-->%s as 1"
406 " instead of %d routes",
407 addrname(htonl(dst_h
), mask
,
409 naddr_ntoa(ws
.to
.sin_addr
422 wb
->n
->n_family
= RIP_AF_INET
;
423 wb
->n
->n_dst
= htonl(dst_h
);
424 /* If the route is from router-discovery or we are
425 * shutting down, admit only a bad metric.
427 wb
->n
->n_metric
= ((stopint
|| ag
->ag_metric
< 1)
430 wb
->n
->n_metric
= htonl(wb
->n
->n_metric
);
431 /* Any non-zero bits in the supposedly unused RIPv1 fields
432 * cause the old `routed` to ignore the route.
433 * That means the mask and so forth cannot be sent
434 * in the hybrid RIPv1/RIPv2 mode.
436 if (ws
.state
& WS_ST_RIP2_ALL
) {
438 && ((ws
.state
& WS_ST_QUERY
)
439 || (ag
->ag_nhop
!= ws
.ifp
->int_addr
440 && on_net(ag
->ag_nhop
,
443 wb
->n
->n_nhop
= ag
->ag_nhop
;
444 wb
->n
->n_mask
= htonl(mask
);
445 wb
->n
->n_tag
= ag
->ag_tag
;
449 if (++wb
->n
>= wb
->lim
)
455 /* supply one route from the table
459 walk_supply(struct radix_node
*rn
,
460 struct walkarg
*argp UNUSED
)
462 #define RT ((struct rt_entry *)rn)
466 struct rt_spare
*rts
;
470 /* Do not advertise external remote interfaces or passive interfaces.
472 if ((RT
->rt_state
& RS_IF
)
474 && (RT
->rt_ifp
->int_state
& IS_PASSIVE
)
475 && !(RT
->rt_state
& RS_MHOME
))
478 /* If being quiet about our ability to forward, then
479 * do not say anything unless responding to a query,
480 * except about our main interface.
482 if (!supplier
&& !(ws
.state
& WS_ST_QUERY
)
483 && !(RT
->rt_state
& RS_MHOME
))
488 /* do not collide with the fake default route */
489 if (dst
== RIP_DEFAULT
490 && (ws
.state
& WS_ST_DEFAULT
))
493 if (RT
->rt_state
& RS_NET_SYN
) {
494 if (RT
->rt_state
& RS_NET_INT
) {
495 /* Do not send manual synthetic network routes
498 if (on_net(ws
.to
.sin_addr
.s_addr
,
499 ntohl(dst
), RT
->rt_mask
))
503 /* Do not send automatic synthetic network routes
504 * if they are not needed because no RIPv1 listeners
507 if (ws
.state
& WS_ST_RIP2_ALL
)
510 /* Do not send automatic synthetic network routes to
513 if (on_net(ws
.to
.sin_addr
.s_addr
,
514 ntohl(dst
), RT
->rt_mask
))
520 /* Advertise the next hop if this is not a route for one
521 * of our interfaces and the next hop is on the same
522 * network as the target.
523 * The final determination is made by supply_out().
525 if (!(RT
->rt_state
& RS_IF
)
526 && RT
->rt_gate
!= myaddr
527 && RT
->rt_gate
!= loopaddr
)
533 metric
= RT
->rt_metric
;
536 if (RT
->rt_state
& RS_MHOME
) {
537 /* retain host route of multi-homed servers */
540 } else if (RT_ISHOST(RT
)) {
541 /* We should always suppress (into existing network routes)
542 * the host routes for the local end of our point-to-point
544 * If we are suppressing host routes in general, then do so.
545 * Avoid advertising host routes onto their own network,
546 * where they should be handled by proxy-ARP.
548 if ((RT
->rt_state
& RS_LOCAL
)
550 || on_net(dst
, ws
.to_net
, ws
.to_mask
))
553 /* Aggregate stray host routes into network routes if allowed.
554 * We cannot aggregate host routes into small network routes
555 * without confusing RIPv1 listeners into thinking the
556 * network routes are host routes.
558 if ((ws
.state
& WS_ST_AG
) && (ws
.state
& WS_ST_RIP2_ALL
))
559 ags
|= AGS_AGGREGATE
;
562 /* Always suppress network routes into other, existing
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.
572 if ((ws
.state
& WS_ST_AG
)
573 && ((RT
->rt_state
& RS_SUBNET
)
574 || (ws
.state
& WS_ST_SUPER_AG
)))
575 ags
|= AGS_AGGREGATE
;
578 /* Do not send RIPv1 advertisements of subnets to other
579 * networks. If possible, multicast them by RIPv2.
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
;
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.
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.
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.
600 * Notice spare routes with the same metric that we are about to
601 * advertise, to split the horizon on redundant, inactive paths.
603 * Do not suppress advertisements of interface-related addresses on
604 * non-point-to-point interfaces. This ensures that we have something
605 * to say every 30 seconds to help detect broken Ethernets or
606 * other interfaces where one packet every 30 seconds costs nothing.
609 && !(ws
.state
& WS_ST_QUERY
)
610 && (ws
.state
& WS_ST_TO_ON_NET
)
611 && (!(RT
->rt_state
& RS_IF
)
612 || ws
.ifp
->int_if_flags
& IFF_POINTOPOINT
)) {
613 for (rts
= RT
->rt_spares
, i
= NUM_SPARES
; i
!= 0; i
--, rts
++) {
614 if (rts
->rts_metric
> metric
615 || rts
->rts_ifp
!= ws
.ifp
)
618 /* If we do not mark the route with AGS_SPLIT_HZ here,
619 * it will be poisoned-reverse, or advertised back
620 * toward its source with an infinite metric.
621 * If we have recently advertised the route with a
622 * better metric than we now have, then we should
623 * poison-reverse the route before suppressing it for
626 * In almost all cases, if there is no spare for the
627 * route then it is either old and dead or a brand
628 * new route. If it is brand new, there is no need
629 * for poison-reverse. If it is old and dead, it
630 * is already poisoned.
632 if (RT
->rt_poison_time
< now_expire
633 || RT
->rt_poison_metric
>= metric
634 || RT
->rt_spares
[1].rts_gate
== 0) {
636 ags
&= ~AGS_SUPPRESS
;
638 metric
= HOPCNT_INFINITY
;
643 /* Keep track of the best metric with which the
644 * route has been advertised recently.
646 if (RT
->rt_poison_metric
>= metric
647 || RT
->rt_poison_time
< now_expire
) {
648 RT
->rt_poison_time
= now
.tv_sec
;
649 RT
->rt_poison_metric
= metric
;
652 /* Adjust the outgoing metric by the cost of the link.
653 * Avoid aggregation when a route is counting to infinity.
655 pref
= RT
->rt_poison_metric
+ ws
.metric
;
658 /* Do not advertise stable routes that will be ignored,
659 * unless we are answering a query.
660 * If the route recently was advertised with a metric that
661 * would have been less than infinity through this interface,
662 * we need to continue to advertise it in order to poison it.
664 if (metric
>= HOPCNT_INFINITY
) {
665 if (!(ws
.state
& WS_ST_QUERY
)
666 && (pref
>= HOPCNT_INFINITY
667 || RT
->rt_poison_time
< now_garbage
))
670 metric
= HOPCNT_INFINITY
;
673 ag_check(dst
, RT
->rt_mask
, 0, nhop
, metric
, pref
,
674 RT
->rt_seqno
, RT
->rt_tag
, ags
, supply_out
);
680 /* Supply dst with the contents of the routing tables.
681 * If this won't fit in one packet, chop it up into several.
684 supply(struct sockaddr_in
*dst
,
685 struct interface
*ifp
, /* output interface */
686 enum output_type type
,
687 int flash
, /* 1=flash update */
688 int vers
, /* RIP version */
689 int passwd_ok
) /* OK to include cleartext password */
699 ws
.to_std_mask
= std_mask(ws
.to
.sin_addr
.s_addr
);
700 ws
.to_std_net
= ntohl(ws
.to
.sin_addr
.s_addr
) & ws
.to_std_mask
;
703 ws
.to_mask
= ifp
->int_mask
;
704 ws
.to_net
= ifp
->int_net
;
705 if (on_net(ws
.to
.sin_addr
.s_addr
, ws
.to_net
, ws
.to_mask
))
706 ws
.state
|= WS_ST_TO_ON_NET
;
709 ws
.to_mask
= ripv1_mask_net(ws
.to
.sin_addr
.s_addr
, 0);
710 ws
.to_net
= ntohl(ws
.to
.sin_addr
.s_addr
) & ws
.to_mask
;
711 rt
= rtfind(dst
->sin_addr
.s_addr
);
718 ws
.state
|= WS_ST_FLASH
;
720 if ((ws
.ifp
= ifp
) == 0) {
723 /* Adjust the advertised metric by the outgoing interface
726 ws
.metric
= ifp
->int_metric
+ 1 + ifp
->int_adj_outmetric
;
729 ripv12_buf
.rip
.rip_vers
= vers
;
733 if (ifp
!= NULL
&& ifp
->int_if_flags
& IFF_MULTICAST
)
734 v2buf
.type
= OUT_MULTICAST
;
736 v2buf
.type
= NO_OUT_MULTICAST
;
737 v12buf
.type
= OUT_BROADCAST
;
741 ws
.state
|= WS_ST_QUERY
;
745 v2buf
.type
= (vers
== RIPv2
) ? type
: NO_OUT_RIPV2
;
749 case NO_OUT_MULTICAST
:
751 break; /* no output */
755 /* full RIPv2 only if cannot be heard by RIPv1 listeners */
756 if (type
!= OUT_BROADCAST
)
757 ws
.state
|= WS_ST_RIP2_ALL
;
758 if ((ws
.state
& WS_ST_QUERY
)
759 || !(ws
.state
& WS_ST_TO_ON_NET
)) {
760 ws
.state
|= (WS_ST_AG
| WS_ST_SUPER_AG
);
761 } else if (ifp
== 0 || !(ifp
->int_state
& IS_NO_AG
)) {
762 ws
.state
|= WS_ST_AG
;
763 if (type
!= OUT_BROADCAST
765 || !(ifp
->int_state
& IS_NO_SUPER_AG
)))
766 ws
.state
|= WS_ST_SUPER_AG
;
770 ws
.a
= (vers
== RIPv2
) ? find_auth(ifp
) : 0;
771 if (!passwd_ok
&& ws
.a
!= 0 && ws
.a
->type
== RIP_AUTH_PW
)
773 clr_ws_buf(&v12buf
,ws
.a
);
774 clr_ws_buf(&v2buf
,ws
.a
);
776 /* Fake a default route if asked and if there is not already
777 * a better, real default route.
779 if (supplier
&& ifp
&& (def_metric
= ifp
->int_d_metric
) != 0) {
780 if (0 == (rt
= rtget(RIP_DEFAULT
, 0))
781 || rt
->rt_metric
+ws
.metric
>= def_metric
) {
782 ws
.state
|= WS_ST_DEFAULT
;
783 ag_check(0, 0, 0, 0, def_metric
, def_metric
,
784 0, 0, 0, supply_out
);
786 def_metric
= rt
->rt_metric
+ws
.metric
;
789 /* If both RIPv2 and the poor-man's router discovery
790 * kludge are on, arrange to advertise an extra
791 * default route via RIPv1.
793 if ((ws
.state
& WS_ST_RIP2_ALL
)
794 && (ifp
->int_state
& IS_PM_RDISC
)) {
795 ripv12_buf
.rip
.rip_vers
= RIPv1
;
796 v12buf
.n
->n_family
= RIP_AF_INET
;
797 v12buf
.n
->n_dst
= htonl(RIP_DEFAULT
);
798 v12buf
.n
->n_metric
= htonl(def_metric
);
803 (void)rn_walktree(rhead
, walk_supply
, 0);
804 ag_flush(0,0,supply_out
);
806 /* Flush the packet buffers, provided they are not empty and
807 * do not contain only the password.
809 if (v12buf
.n
!= v12buf
.base
810 && (v12buf
.n
> v12buf
.base
+1
811 || v12buf
.base
->n_family
!= RIP_AF_AUTH
))
812 supply_write(&v12buf
);
813 if (v2buf
.n
!= v2buf
.base
814 && (v2buf
.n
> v2buf
.base
+1
815 || v2buf
.base
->n_family
!= RIP_AF_AUTH
))
816 supply_write(&v2buf
);
818 /* If we sent nothing and this is an answer to a query, send
822 && (ws
.state
& WS_ST_QUERY
))
823 supply_write(&v12buf
);
827 /* send all of the routing table or just do a flash update
833 static struct sockaddr_in dst
= {sizeof(dst
), AF_INET
, 0, {0}, {0}};
835 static struct sockaddr_in dst
= {AF_INET
};
837 struct interface
*ifp
;
838 enum output_type type
;
840 struct timeval rtime
;
844 intvl_random(&rtime
, MIN_WAITTIME
, MAX_WAITTIME
);
846 timevaladd(&no_flash
, &now
);
851 trace_act("send %s and inhibit dynamic updates for %.3f sec",
852 flash
? "dynamic update" : "all routes",
853 rtime
.tv_sec
+ ((float)rtime
.tv_usec
)/1000000.0);
855 for (ifp
= ifnet
; ifp
!= 0; ifp
= ifp
->int_next
) {
856 /* Skip interfaces not doing RIP.
857 * Do try broken interfaces to see if they have healed.
859 if (IS_RIP_OUT_OFF(ifp
->int_state
))
862 /* skip turned off interfaces */
863 if (!iff_up(ifp
->int_if_flags
))
866 vers
= (ifp
->int_state
& IS_NO_RIPV1_OUT
) ? RIPv2
: RIPv1
;
868 if (ifp
->int_if_flags
& IFF_BROADCAST
) {
869 /* ordinary, hardware interface */
870 dst
.sin_addr
.s_addr
= ifp
->int_brdaddr
;
873 && !(ifp
->int_state
& IS_NO_RIP_MCAST
)) {
874 type
= OUT_MULTICAST
;
876 type
= OUT_BROADCAST
;
879 } else if (ifp
->int_if_flags
& IFF_POINTOPOINT
) {
880 /* point-to-point hardware interface */
881 dst
.sin_addr
.s_addr
= ifp
->int_dstaddr
;
882 /* use multicast if the interface allows (e.g. GRE) */
884 && (ifp
->int_if_flags
& IFF_MULTICAST
)
885 && !(ifp
->int_state
& IS_NO_RIP_MCAST
)) {
886 type
= OUT_MULTICAST
;
891 } else if (ifp
->int_state
& IS_REMOTE
) {
892 /* remote interface */
893 dst
.sin_addr
.s_addr
= ifp
->int_addr
;
897 /* ATM, HIPPI, etc. */
901 supply(&dst
, ifp
, type
, flash
, vers
, 1);
904 update_seqno
++; /* all routes are up to date */
909 * Do it only once to an interface, and not even after the interface
910 * was broken and recovered.
916 static struct sockaddr_in dst
= {sizeof(dst
), AF_INET
, 0, {0}, {0}};
918 static struct sockaddr_in dst
= {AF_INET
};
920 struct interface
*ifp
;
922 enum output_type type
;
928 memset(&buf
, 0, sizeof(buf
));
930 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
) {
931 /* Skip interfaces those already queried.
932 * Do not ask via interfaces through which we don't
933 * accept input. Do not ask via interfaces that cannot
935 * Do try broken interfaces to see if they have healed.
937 if (IS_RIP_IN_OFF(ifp
->int_state
)
938 || ifp
->int_query_time
!= NEVER
)
941 /* skip turned off interfaces */
942 if (!iff_up(ifp
->int_if_flags
))
945 buf
.rip_vers
= (ifp
->int_state
&IS_NO_RIPV1_OUT
) ? RIPv2
:RIPv1
;
946 buf
.rip_cmd
= RIPCMD_REQUEST
;
947 buf
.rip_nets
[0].n_family
= RIP_AF_UNSPEC
;
948 buf
.rip_nets
[0].n_metric
= htonl(HOPCNT_INFINITY
);
950 /* Send a RIPv1 query only if allowed and if we will
951 * listen to RIPv1 routers.
953 if ((ifp
->int_state
& IS_NO_RIPV1_OUT
)
954 || (ifp
->int_state
& IS_NO_RIPV1_IN
)) {
955 buf
.rip_vers
= RIPv2
;
957 buf
.rip_vers
= RIPv1
;
960 if (ifp
->int_if_flags
& IFF_BROADCAST
) {
961 /* ordinary, hardware interface */
962 dst
.sin_addr
.s_addr
= ifp
->int_brdaddr
;
964 /* Broadcast RIPv1 queries and RIPv2 queries
965 * when the hardware cannot multicast.
967 if (buf
.rip_vers
== RIPv2
968 && (ifp
->int_if_flags
& IFF_MULTICAST
)
969 && !(ifp
->int_state
& IS_NO_RIP_MCAST
)) {
970 type
= OUT_MULTICAST
;
972 type
= OUT_BROADCAST
;
975 } else if (ifp
->int_if_flags
& IFF_POINTOPOINT
) {
976 /* point-to-point hardware interface */
977 dst
.sin_addr
.s_addr
= ifp
->int_dstaddr
;
978 /* use multicast if the interface allows (e.g. GRE) */
979 if (buf
.rip_vers
== RIPv2
980 && (ifp
->int_if_flags
& IFF_MULTICAST
)
981 && !(ifp
->int_state
& IS_NO_RIP_MCAST
)) {
982 type
= OUT_MULTICAST
;
987 } else if (ifp
->int_state
& IS_REMOTE
) {
988 /* remote interface */
989 dst
.sin_addr
.s_addr
= ifp
->int_addr
;
993 /* ATM, HIPPI, etc. */
997 ifp
->int_query_time
= now
.tv_sec
+SUPPLY_INTERVAL
;
998 if (output(type
, &dst
, ifp
, &buf
, sizeof(buf
)) < 0)