Home | History | Annotate | Line # | Download | only in routed
output.c revision 1.8
      1  1.8      cgd /*	$NetBSD: output.c,v 1.8 1995/03/18 15:00:35 cgd 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.1      cgd #ifndef lint
     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.8      cgd static char rcsid[] = "$NetBSD: output.c,v 1.8 1995/03/18 15:00:35 cgd Exp $";
     41  1.8      cgd #endif
     42  1.1      cgd #endif /* not lint */
     43  1.1      cgd 
     44  1.1      cgd /*
     45  1.1      cgd  * Routing Table Management Daemon
     46  1.1      cgd  */
     47  1.1      cgd #include "defs.h"
     48  1.1      cgd 
     49  1.1      cgd /*
     50  1.1      cgd  * Apply the function "f" to all non-passive
     51  1.1      cgd  * interfaces.  If the interface supports the
     52  1.1      cgd  * use of broadcasting use it, otherwise address
     53  1.1      cgd  * the output to the known router.
     54  1.1      cgd  */
     55  1.7      cgd void
     56  1.1      cgd toall(f, rtstate, skipif)
     57  1.1      cgd 	int (*f)();
     58  1.1      cgd 	int rtstate;
     59  1.1      cgd 	struct interface *skipif;
     60  1.1      cgd {
     61  1.1      cgd 	register struct interface *ifp;
     62  1.1      cgd 	register struct sockaddr *dst;
     63  1.1      cgd 	register int flags;
     64  1.1      cgd 	extern struct interface *ifnet;
     65  1.1      cgd 
     66  1.1      cgd 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
     67  1.1      cgd 		if (ifp->int_flags & IFF_PASSIVE || ifp == skipif)
     68  1.1      cgd 			continue;
     69  1.1      cgd 		dst = ifp->int_flags & IFF_BROADCAST ? &ifp->int_broadaddr :
     70  1.1      cgd 		      ifp->int_flags & IFF_POINTOPOINT ? &ifp->int_dstaddr :
     71  1.1      cgd 		      &ifp->int_addr;
     72  1.1      cgd 		flags = ifp->int_flags & IFF_INTERFACE ? MSG_DONTROUTE : 0;
     73  1.1      cgd 		(*f)(dst, flags, ifp, rtstate);
     74  1.1      cgd 	}
     75  1.1      cgd }
     76  1.1      cgd 
     77  1.1      cgd /*
     78  1.1      cgd  * Output a preformed packet.
     79  1.1      cgd  */
     80  1.1      cgd /*ARGSUSED*/
     81  1.7      cgd int
     82  1.1      cgd sndmsg(dst, flags, ifp, rtstate)
     83  1.1      cgd 	struct sockaddr *dst;
     84  1.1      cgd 	int flags;
     85  1.1      cgd 	struct interface *ifp;
     86  1.1      cgd 	int rtstate;
     87  1.1      cgd {
     88  1.1      cgd 
     89  1.1      cgd 	(*afswitch[dst->sa_family].af_output)(s, flags,
     90  1.1      cgd 		dst, sizeof (struct rip));
     91  1.1      cgd 	TRACE_OUTPUT(ifp, dst, sizeof (struct rip));
     92  1.1      cgd }
     93  1.1      cgd 
     94  1.1      cgd /*
     95  1.1      cgd  * Supply dst with the contents of the routing tables.
     96  1.1      cgd  * If this won't fit in one packet, chop it up into several.
     97  1.1      cgd  */
     98  1.7      cgd int
     99  1.1      cgd supply(dst, flags, ifp, rtstate)
    100  1.1      cgd 	struct sockaddr *dst;
    101  1.1      cgd 	int flags;
    102  1.1      cgd 	register struct interface *ifp;
    103  1.1      cgd 	int rtstate;
    104  1.1      cgd {
    105  1.1      cgd 	register struct rt_entry *rt;
    106  1.1      cgd 	register struct netinfo *n = msg->rip_nets;
    107  1.1      cgd 	register struct rthash *rh;
    108  1.1      cgd 	struct rthash *base = hosthash;
    109  1.1      cgd 	int doinghost = 1, size;
    110  1.1      cgd 	int (*output)() = afswitch[dst->sa_family].af_output;
    111  1.1      cgd 	int (*sendroute)() = afswitch[dst->sa_family].af_sendroute;
    112  1.1      cgd 	int npackets = 0;
    113  1.1      cgd 
    114  1.1      cgd 	msg->rip_cmd = RIPCMD_RESPONSE;
    115  1.1      cgd 	msg->rip_vers = RIPVERSION;
    116  1.6  mycroft 	memset(msg->rip_res1, 0, sizeof(msg->rip_res1));
    117  1.1      cgd again:
    118  1.1      cgd 	for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++)
    119  1.1      cgd 	for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
    120  1.1      cgd 		/*
    121  1.1      cgd 		 * Don't resend the information on the network
    122  1.1      cgd 		 * from which it was received (unless sending
    123  1.1      cgd 		 * in response to a query).
    124  1.1      cgd 		 */
    125  1.1      cgd 		if (ifp && rt->rt_ifp == ifp &&
    126  1.1      cgd 		    (rt->rt_state & RTS_INTERFACE) == 0)
    127  1.1      cgd 			continue;
    128  1.1      cgd 		if (rt->rt_state & RTS_EXTERNAL)
    129  1.1      cgd 			continue;
    130  1.1      cgd 		/*
    131  1.1      cgd 		 * For dynamic updates, limit update to routes
    132  1.1      cgd 		 * with the specified state.
    133  1.1      cgd 		 */
    134  1.1      cgd 		if (rtstate && (rt->rt_state & rtstate) == 0)
    135  1.1      cgd 			continue;
    136  1.1      cgd 		/*
    137  1.1      cgd 		 * Limit the spread of subnet information
    138  1.1      cgd 		 * to those who are interested.
    139  1.1      cgd 		 */
    140  1.1      cgd 		if (doinghost == 0 && rt->rt_state & RTS_SUBNET) {
    141  1.1      cgd 			if (rt->rt_dst.sa_family != dst->sa_family)
    142  1.1      cgd 				continue;
    143  1.1      cgd 			if ((*sendroute)(rt, dst) == 0)
    144  1.1      cgd 				continue;
    145  1.1      cgd 		}
    146  1.1      cgd 		size = (char *)n - packet;
    147  1.1      cgd 		if (size > MAXPACKETSIZE - sizeof (struct netinfo)) {
    148  1.1      cgd 			TRACE_OUTPUT(ifp, dst, size);
    149  1.1      cgd 			(*output)(s, flags, dst, size);
    150  1.1      cgd 			/*
    151  1.1      cgd 			 * If only sending to ourselves,
    152  1.1      cgd 			 * one packet is enough to monitor interface.
    153  1.1      cgd 			 */
    154  1.1      cgd 			if (ifp && (ifp->int_flags &
    155  1.1      cgd 			   (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0)
    156  1.1      cgd 				return;
    157  1.1      cgd 			n = msg->rip_nets;
    158  1.1      cgd 			npackets++;
    159  1.1      cgd 		}
    160  1.1      cgd 		n->rip_dst = rt->rt_dst;
    161  1.1      cgd #if BSD < 198810
    162  1.1      cgd 		if (sizeof(n->rip_dst.sa_family) > 1)	/* XXX */
    163  1.1      cgd 		n->rip_dst.sa_family = htons(n->rip_dst.sa_family);
    164  1.1      cgd #else
    165  1.1      cgd #define osa(x) ((struct osockaddr *)(&(x)))
    166  1.1      cgd 		osa(n->rip_dst)->sa_family = htons(n->rip_dst.sa_family);
    167  1.1      cgd #endif
    168  1.1      cgd 		n->rip_metric = htonl(rt->rt_metric);
    169  1.1      cgd 		n++;
    170  1.1      cgd 	}
    171  1.1      cgd 	if (doinghost) {
    172  1.1      cgd 		doinghost = 0;
    173  1.1      cgd 		base = nethash;
    174  1.1      cgd 		goto again;
    175  1.1      cgd 	}
    176  1.1      cgd 	if (n != msg->rip_nets || (npackets == 0 && rtstate == 0)) {
    177  1.1      cgd 		size = (char *)n - packet;
    178  1.1      cgd 		TRACE_OUTPUT(ifp, dst, size);
    179  1.1      cgd 		(*output)(s, flags, dst, size);
    180  1.1      cgd 	}
    181  1.1      cgd }
    182