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