Home | History | Annotate | Line # | Download | only in routed
output.c revision 1.9
      1  1.9  christos /*	$NetBSD: output.c,v 1.9 1995/06/20 22:27:54 christos 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.9  christos static char rcsid[] = "$NetBSD: output.c,v 1.9 1995/06/20 22:27:54 christos 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.9  christos 	void (*f) __P((struct sockaddr *, int, struct interface *, int));
     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.9  christos void
     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.9  christos void
     99  1.1       cgd supply(dst, flags, ifp, rtstate)
    100  1.1       cgd 	struct sockaddr *dst;
    101  1.1       cgd 	int flags;
    102  1.9  christos 	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.9  christos 	void (*output) __P((int, int, struct sockaddr *, int)) =
    111  1.9  christos 		afswitch[dst->sa_family].af_output;
    112  1.9  christos 	int (*sendroute) __P((struct rt_entry *, struct sockaddr *)) =
    113  1.9  christos 		afswitch[dst->sa_family].af_sendroute;
    114  1.1       cgd 	int npackets = 0;
    115  1.1       cgd 
    116  1.1       cgd 	msg->rip_cmd = RIPCMD_RESPONSE;
    117  1.9  christos 	msg->rip_vers = RIP_VERSION_1;
    118  1.6   mycroft 	memset(msg->rip_res1, 0, sizeof(msg->rip_res1));
    119  1.1       cgd again:
    120  1.1       cgd 	for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++)
    121  1.9  christos 	for (rt = rh->cqh_first; rt != (void *) rh; rt = rt->rt_entry.cqe_next) {
    122  1.1       cgd 		/*
    123  1.1       cgd 		 * Don't resend the information on the network
    124  1.1       cgd 		 * from which it was received (unless sending
    125  1.1       cgd 		 * in response to a query).
    126  1.1       cgd 		 */
    127  1.1       cgd 		if (ifp && rt->rt_ifp == ifp &&
    128  1.1       cgd 		    (rt->rt_state & RTS_INTERFACE) == 0)
    129  1.1       cgd 			continue;
    130  1.1       cgd 		if (rt->rt_state & RTS_EXTERNAL)
    131  1.1       cgd 			continue;
    132  1.1       cgd 		/*
    133  1.1       cgd 		 * For dynamic updates, limit update to routes
    134  1.1       cgd 		 * with the specified state.
    135  1.1       cgd 		 */
    136  1.1       cgd 		if (rtstate && (rt->rt_state & rtstate) == 0)
    137  1.1       cgd 			continue;
    138  1.1       cgd 		/*
    139  1.1       cgd 		 * Limit the spread of subnet information
    140  1.1       cgd 		 * to those who are interested.
    141  1.1       cgd 		 */
    142  1.1       cgd 		if (doinghost == 0 && rt->rt_state & RTS_SUBNET) {
    143  1.1       cgd 			if (rt->rt_dst.sa_family != dst->sa_family)
    144  1.1       cgd 				continue;
    145  1.1       cgd 			if ((*sendroute)(rt, dst) == 0)
    146  1.1       cgd 				continue;
    147  1.1       cgd 		}
    148  1.1       cgd 		size = (char *)n - packet;
    149  1.1       cgd 		if (size > MAXPACKETSIZE - sizeof (struct netinfo)) {
    150  1.1       cgd 			TRACE_OUTPUT(ifp, dst, size);
    151  1.1       cgd 			(*output)(s, flags, dst, size);
    152  1.1       cgd 			/*
    153  1.1       cgd 			 * If only sending to ourselves,
    154  1.1       cgd 			 * one packet is enough to monitor interface.
    155  1.1       cgd 			 */
    156  1.1       cgd 			if (ifp && (ifp->int_flags &
    157  1.1       cgd 			   (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0)
    158  1.1       cgd 				return;
    159  1.1       cgd 			n = msg->rip_nets;
    160  1.1       cgd 			npackets++;
    161  1.1       cgd 		}
    162  1.9  christos 		(*afswitch[rt->rt_dst.sa_family].af_put)(n, &rt->rt_dst);
    163  1.1       cgd 		n->rip_metric = htonl(rt->rt_metric);
    164  1.1       cgd 		n++;
    165  1.1       cgd 	}
    166  1.1       cgd 	if (doinghost) {
    167  1.1       cgd 		doinghost = 0;
    168  1.1       cgd 		base = nethash;
    169  1.1       cgd 		goto again;
    170  1.1       cgd 	}
    171  1.1       cgd 	if (n != msg->rip_nets || (npackets == 0 && rtstate == 0)) {
    172  1.1       cgd 		size = (char *)n - packet;
    173  1.1       cgd 		TRACE_OUTPUT(ifp, dst, size);
    174  1.1       cgd 		(*output)(s, flags, dst, size);
    175  1.1       cgd 	}
    176  1.1       cgd }
    177