output.c revision 19948
111820Sjulian/*
211820Sjulian * Copyright (c) 1985, 1993
311820Sjulian *	The Regents of the University of California.  All rights reserved.
411820Sjulian *
511820Sjulian * Copyright (c) 1995 John Hay.  All rights reserved.
611820Sjulian *
711820Sjulian * This file includes significant work done at Cornell University by
811820Sjulian * Bill Nesheim.  That work included by permission.
911820Sjulian *
1011820Sjulian * Redistribution and use in source and binary forms, with or without
1111820Sjulian * modification, are permitted provided that the following conditions
1211820Sjulian * are met:
1311820Sjulian * 1. Redistributions of source code must retain the above copyright
1411820Sjulian *    notice, this list of conditions and the following disclaimer.
1511820Sjulian * 2. Redistributions in binary form must reproduce the above copyright
1611820Sjulian *    notice, this list of conditions and the following disclaimer in the
1711820Sjulian *    documentation and/or other materials provided with the distribution.
1811820Sjulian * 3. All advertising materials mentioning features or use of this software
1911820Sjulian *    must display the following acknowledgement:
2011820Sjulian *	This product includes software developed by the University of
2111820Sjulian *	California, Berkeley and its contributors.
2211820Sjulian * 4. Neither the name of the University nor the names of its contributors
2311820Sjulian *    may be used to endorse or promote products derived from this software
2411820Sjulian *    without specific prior written permission.
2511820Sjulian *
2611820Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2711820Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2811820Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2911820Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3011820Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3111820Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3211820Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3311820Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3411820Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3511820Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3611820Sjulian * SUCH DAMAGE.
3711820Sjulian *
3819948Sjhay *	$Id: output.c,v 1.3 1996/04/13 15:13:20 jhay Exp $
3911820Sjulian */
4011820Sjulian
4111820Sjulian#ifndef lint
4211820Sjulianstatic char sccsid[] = "@(#)output.c	8.1 (Berkeley) 6/5/93";
4311820Sjulian#endif /* not lint */
4411820Sjulian
4511820Sjulian/*
4611820Sjulian * Routing Table Management Daemon
4711820Sjulian */
4819948Sjhay#include <unistd.h>
4911820Sjulian#include "defs.h"
5011820Sjulian
5111820Sjulian/*
5211820Sjulian * Apply the function "f" to all non-passive
5311820Sjulian * interfaces.  If the interface supports the
5411820Sjulian * use of broadcasting use it, otherwise address
5511820Sjulian * the output to the known router.
5611820Sjulian */
5711820Sjulianvoid
5811820Sjuliantoall(f, except)
5911820Sjulian	void (*f)(struct sockaddr *, int, struct interface *);
6011820Sjulian	struct rt_entry *except;
6111820Sjulian{
6211820Sjulian	register struct interface *ifp;
6311820Sjulian	register struct sockaddr *dst;
6411820Sjulian	register int flags;
6511820Sjulian	register struct rt_entry *trt;
6611820Sjulian	int onlist;
6711820Sjulian	extern struct interface *ifnet;
6811820Sjulian
6911820Sjulian	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
7011820Sjulian		if (ifp->int_flags & IFF_PASSIVE)
7111820Sjulian			continue;
7211820Sjulian
7311820Sjulian		/*
7411820Sjulian		 * Don't send it on interfaces in the except list.
7511820Sjulian		 */
7611820Sjulian		onlist = 0;
7711820Sjulian		trt = except;
7811820Sjulian		while(trt) {
7911820Sjulian			if (ifp == trt->rt_ifp) {
8011820Sjulian				onlist = 1;
8111820Sjulian				break;
8211820Sjulian			}
8311820Sjulian			trt = trt->rt_clone;
8411820Sjulian		}
8511820Sjulian		if (onlist)
8611820Sjulian			continue;
8711820Sjulian
8811820Sjulian		dst = ifp->int_flags & IFF_BROADCAST ? &ifp->int_broadaddr :
8911820Sjulian		      ifp->int_flags & IFF_POINTOPOINT ? &ifp->int_dstaddr :
9011820Sjulian		      &ifp->int_addr;
9111820Sjulian		flags = ifp->int_flags & IFF_INTERFACE ? MSG_DONTROUTE : 0;
9211820Sjulian		(*f)(dst, flags, ifp);
9311820Sjulian	}
9411820Sjulian}
9511820Sjulian
9611820Sjulian/*
9711820Sjulian * Output a preformed packet.
9811820Sjulian */
9911820Sjulianvoid
10011820Sjuliansndmsg(dst, flags, ifp)
10111820Sjulian	struct sockaddr *dst;
10211820Sjulian	int flags;
10311820Sjulian	struct interface *ifp;
10411820Sjulian{
10511820Sjulian
10611820Sjulian	(*afswitch[dst->sa_family].af_output)
10711820Sjulian		(ripsock, flags, dst, sizeof (struct rip));
10811820Sjulian	TRACE_OUTPUT(ifp, dst, sizeof (struct rip));
10911820Sjulian}
11011820Sjulian
11111820Sjulian/*
11211820Sjulian * Supply dst with the contents of the routing tables.
11311820Sjulian * If this won't fit in one packet, chop it up into several.
11411820Sjulian *
11511820Sjulian * This must be done using the split horizon algorithm.
11611820Sjulian * 1. Don't send routing info to the interface from where it was received.
11711820Sjulian * 2. Don't publish an interface to itself.
11811820Sjulian * 3. If a route is received from more than one interface and the cost is
11911820Sjulian *    the same, don't publish it on either interface. I am calling this
12011820Sjulian *    clones.
12111820Sjulian */
12211820Sjulianvoid
12311820Sjuliansupply(dst, flags, ifp)
12411820Sjulian	struct sockaddr *dst;
12511820Sjulian	int flags;
12611820Sjulian	struct interface *ifp;
12711820Sjulian{
12811820Sjulian	register struct rt_entry *rt;
12911820Sjulian	register struct rt_entry *crt; /* Clone route */
13011820Sjulian	register struct rthash *rh;
13111820Sjulian	register struct netinfo *nn;
13211820Sjulian	register struct netinfo *n = msg->rip_nets;
13311820Sjulian	struct rthash *base = hosthash;
13411820Sjulian	struct sockaddr_ipx *sipx =  (struct sockaddr_ipx *) dst;
13511820Sjulian	af_output_t *output = afswitch[dst->sa_family].af_output;
13611820Sjulian	int doinghost = 1, size, metric, ticks;
13711820Sjulian	union ipx_net net;
13819948Sjhay	int delay = 0;
13911820Sjulian
14011820Sjulian	if (sipx->sipx_port == 0)
14111820Sjulian		sipx->sipx_port = htons(IPXPORT_RIP);
14211820Sjulian
14311820Sjulian	msg->rip_cmd = ntohs(RIPCMD_RESPONSE);
14411820Sjulianagain:
14511820Sjulian	for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++)
14611820Sjulian	for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
14711820Sjulian		size = (char *)n - (char *)msg;
14815248Sjhay		if (size >= ((MAXRIPNETS * sizeof (struct netinfo)) +
14915248Sjhay				sizeof (msg->rip_cmd))) {
15011820Sjulian			(*output)(ripsock, flags, dst, size);
15111820Sjulian			TRACE_OUTPUT(ifp, dst, size);
15211820Sjulian			n = msg->rip_nets;
15319948Sjhay			delay++;
15419948Sjhay			if(delay == 2) {
15519948Sjhay				usleep(20000);
15619948Sjhay				delay = 0;
15719948Sjhay			}
15811820Sjulian		}
15911820Sjulian
16011820Sjulian		/*
16111820Sjulian		 * This should do rule one and two of the split horizon
16211820Sjulian		 * algorithm.
16311820Sjulian		 */
16411820Sjulian		if (rt->rt_ifp == ifp)
16511820Sjulian			continue;
16611820Sjulian
16711820Sjulian		/*
16811820Sjulian		 * Rule 3.
16911820Sjulian		 * Look if we have clones (different routes to the same
17011820Sjulian		 * place with exactly the same cost).
17111820Sjulian		 *
17211820Sjulian		 * We should not publish on any of the clone interfaces.
17311820Sjulian		 */
17411820Sjulian		crt = rt->rt_clone;
17511820Sjulian		while (crt) {
17611820Sjulian			if (crt->rt_ifp == ifp)
17712630Sjulian				goto next;
17811820Sjulian			crt = crt->rt_clone;
17911820Sjulian		}
18011820Sjulian
18111820Sjulian		sipx = (struct sockaddr_ipx *)&rt->rt_dst;
18211820Sjulian	        if ((rt->rt_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
18311820Sjulian			sipx = (struct sockaddr_ipx *)&rt->rt_router;
18411820Sjulian		if (rt->rt_metric == HOPCNT_INFINITY)
18511820Sjulian			metric = HOPCNT_INFINITY;
18611820Sjulian		else {
18711820Sjulian			metric = rt->rt_metric + 1;
18811820Sjulian			/*
18911820Sjulian			 * We don't advertize routes with more than 15 hops.
19011820Sjulian			 */
19111820Sjulian			if (metric >= HOPCNT_INFINITY)
19211820Sjulian				continue;
19311820Sjulian		}
19411820Sjulian		/* XXX One day we should cater for slow interfaces also. */
19511820Sjulian		ticks = rt->rt_ticks + 1;
19611820Sjulian		net = sipx->sipx_addr.x_net;
19711820Sjulian
19811820Sjulian		/*
19911820Sjulian		 * Make sure that we don't put out a two net entries
20011820Sjulian		 * for a pt to pt link (one for the G route, one for the if)
20111820Sjulian		 * This is a kludge, and won't work if there are lots of nets.
20211820Sjulian		 */
20311820Sjulian		for (nn = msg->rip_nets; nn < n; nn++) {
20411820Sjulian			if (ipx_neteqnn(net, nn->rip_dst)) {
20511820Sjulian				if (ticks < ntohs(nn->rip_ticks)) {
20611820Sjulian					nn->rip_metric = htons(metric);
20711820Sjulian					nn->rip_ticks = htons(ticks);
20811820Sjulian				} else if ((ticks == ntohs(nn->rip_ticks)) &&
20911820Sjulian					   (metric < ntohs(nn->rip_metric))) {
21011820Sjulian					nn->rip_metric = htons(metric);
21111820Sjulian					nn->rip_ticks = htons(ticks);
21211820Sjulian				}
21311820Sjulian				goto next;
21411820Sjulian			}
21511820Sjulian		}
21611820Sjulian		n->rip_dst = net;
21711820Sjulian		n->rip_metric = htons(metric);
21811820Sjulian		n->rip_ticks = htons(ticks);
21911820Sjulian		n++;
22011820Sjulian	next:;
22111820Sjulian	}
22211820Sjulian	if (doinghost) {
22311820Sjulian		doinghost = 0;
22411820Sjulian		base = nethash;
22511820Sjulian		goto again;
22611820Sjulian	}
22711820Sjulian	if (n != msg->rip_nets) {
22811820Sjulian		size = (char *)n - (char *)msg;
22911820Sjulian		(*output)(ripsock, flags, dst, size);
23011820Sjulian		TRACE_OUTPUT(ifp, dst, size);
23111820Sjulian	}
23211820Sjulian}
233