output.c revision 15248
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 *
3815248Sjhay *	$Id: output.c,v 1.2 1995/12/05 04:59:54 julian 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 */
4811820Sjulian#include "defs.h"
4911820Sjulian
5011820Sjulian/*
5111820Sjulian * Apply the function "f" to all non-passive
5211820Sjulian * interfaces.  If the interface supports the
5311820Sjulian * use of broadcasting use it, otherwise address
5411820Sjulian * the output to the known router.
5511820Sjulian */
5611820Sjulianvoid
5711820Sjuliantoall(f, except)
5811820Sjulian	void (*f)(struct sockaddr *, int, struct interface *);
5911820Sjulian	struct rt_entry *except;
6011820Sjulian{
6111820Sjulian	register struct interface *ifp;
6211820Sjulian	register struct sockaddr *dst;
6311820Sjulian	register int flags;
6411820Sjulian	register struct rt_entry *trt;
6511820Sjulian	int onlist;
6611820Sjulian	extern struct interface *ifnet;
6711820Sjulian
6811820Sjulian	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
6911820Sjulian		if (ifp->int_flags & IFF_PASSIVE)
7011820Sjulian			continue;
7111820Sjulian
7211820Sjulian		/*
7311820Sjulian		 * Don't send it on interfaces in the except list.
7411820Sjulian		 */
7511820Sjulian		onlist = 0;
7611820Sjulian		trt = except;
7711820Sjulian		while(trt) {
7811820Sjulian			if (ifp == trt->rt_ifp) {
7911820Sjulian				onlist = 1;
8011820Sjulian				break;
8111820Sjulian			}
8211820Sjulian			trt = trt->rt_clone;
8311820Sjulian		}
8411820Sjulian		if (onlist)
8511820Sjulian			continue;
8611820Sjulian
8711820Sjulian		dst = ifp->int_flags & IFF_BROADCAST ? &ifp->int_broadaddr :
8811820Sjulian		      ifp->int_flags & IFF_POINTOPOINT ? &ifp->int_dstaddr :
8911820Sjulian		      &ifp->int_addr;
9011820Sjulian		flags = ifp->int_flags & IFF_INTERFACE ? MSG_DONTROUTE : 0;
9111820Sjulian		(*f)(dst, flags, ifp);
9211820Sjulian	}
9311820Sjulian}
9411820Sjulian
9511820Sjulian/*
9611820Sjulian * Output a preformed packet.
9711820Sjulian */
9811820Sjulianvoid
9911820Sjuliansndmsg(dst, flags, ifp)
10011820Sjulian	struct sockaddr *dst;
10111820Sjulian	int flags;
10211820Sjulian	struct interface *ifp;
10311820Sjulian{
10411820Sjulian
10511820Sjulian	(*afswitch[dst->sa_family].af_output)
10611820Sjulian		(ripsock, flags, dst, sizeof (struct rip));
10711820Sjulian	TRACE_OUTPUT(ifp, dst, sizeof (struct rip));
10811820Sjulian}
10911820Sjulian
11011820Sjulian/*
11111820Sjulian * Supply dst with the contents of the routing tables.
11211820Sjulian * If this won't fit in one packet, chop it up into several.
11311820Sjulian *
11411820Sjulian * This must be done using the split horizon algorithm.
11511820Sjulian * 1. Don't send routing info to the interface from where it was received.
11611820Sjulian * 2. Don't publish an interface to itself.
11711820Sjulian * 3. If a route is received from more than one interface and the cost is
11811820Sjulian *    the same, don't publish it on either interface. I am calling this
11911820Sjulian *    clones.
12011820Sjulian */
12111820Sjulianvoid
12211820Sjuliansupply(dst, flags, ifp)
12311820Sjulian	struct sockaddr *dst;
12411820Sjulian	int flags;
12511820Sjulian	struct interface *ifp;
12611820Sjulian{
12711820Sjulian	register struct rt_entry *rt;
12811820Sjulian	register struct rt_entry *crt; /* Clone route */
12911820Sjulian	register struct rthash *rh;
13011820Sjulian	register struct netinfo *nn;
13111820Sjulian	register struct netinfo *n = msg->rip_nets;
13211820Sjulian	struct rthash *base = hosthash;
13311820Sjulian	struct sockaddr_ipx *sipx =  (struct sockaddr_ipx *) dst;
13411820Sjulian	af_output_t *output = afswitch[dst->sa_family].af_output;
13511820Sjulian	int doinghost = 1, size, metric, ticks;
13611820Sjulian	union ipx_net net;
13711820Sjulian
13811820Sjulian	if (sipx->sipx_port == 0)
13911820Sjulian		sipx->sipx_port = htons(IPXPORT_RIP);
14011820Sjulian
14111820Sjulian	msg->rip_cmd = ntohs(RIPCMD_RESPONSE);
14211820Sjulianagain:
14311820Sjulian	for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++)
14411820Sjulian	for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
14511820Sjulian		size = (char *)n - (char *)msg;
14615248Sjhay		if (size >= ((MAXRIPNETS * sizeof (struct netinfo)) +
14715248Sjhay				sizeof (msg->rip_cmd))) {
14811820Sjulian			(*output)(ripsock, flags, dst, size);
14911820Sjulian			TRACE_OUTPUT(ifp, dst, size);
15011820Sjulian			n = msg->rip_nets;
15111820Sjulian		}
15211820Sjulian
15311820Sjulian		/*
15411820Sjulian		 * This should do rule one and two of the split horizon
15511820Sjulian		 * algorithm.
15611820Sjulian		 */
15711820Sjulian		if (rt->rt_ifp == ifp)
15811820Sjulian			continue;
15911820Sjulian
16011820Sjulian		/*
16111820Sjulian		 * Rule 3.
16211820Sjulian		 * Look if we have clones (different routes to the same
16311820Sjulian		 * place with exactly the same cost).
16411820Sjulian		 *
16511820Sjulian		 * We should not publish on any of the clone interfaces.
16611820Sjulian		 */
16711820Sjulian		crt = rt->rt_clone;
16811820Sjulian		while (crt) {
16911820Sjulian			if (crt->rt_ifp == ifp)
17012630Sjulian				goto next;
17111820Sjulian			crt = crt->rt_clone;
17211820Sjulian		}
17311820Sjulian
17411820Sjulian		sipx = (struct sockaddr_ipx *)&rt->rt_dst;
17511820Sjulian	        if ((rt->rt_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
17611820Sjulian			sipx = (struct sockaddr_ipx *)&rt->rt_router;
17711820Sjulian		if (rt->rt_metric == HOPCNT_INFINITY)
17811820Sjulian			metric = HOPCNT_INFINITY;
17911820Sjulian		else {
18011820Sjulian			metric = rt->rt_metric + 1;
18111820Sjulian			/*
18211820Sjulian			 * We don't advertize routes with more than 15 hops.
18311820Sjulian			 */
18411820Sjulian			if (metric >= HOPCNT_INFINITY)
18511820Sjulian				continue;
18611820Sjulian		}
18711820Sjulian		/* XXX One day we should cater for slow interfaces also. */
18811820Sjulian		ticks = rt->rt_ticks + 1;
18911820Sjulian		net = sipx->sipx_addr.x_net;
19011820Sjulian
19111820Sjulian		/*
19211820Sjulian		 * Make sure that we don't put out a two net entries
19311820Sjulian		 * for a pt to pt link (one for the G route, one for the if)
19411820Sjulian		 * This is a kludge, and won't work if there are lots of nets.
19511820Sjulian		 */
19611820Sjulian		for (nn = msg->rip_nets; nn < n; nn++) {
19711820Sjulian			if (ipx_neteqnn(net, nn->rip_dst)) {
19811820Sjulian				if (ticks < ntohs(nn->rip_ticks)) {
19911820Sjulian					nn->rip_metric = htons(metric);
20011820Sjulian					nn->rip_ticks = htons(ticks);
20111820Sjulian				} else if ((ticks == ntohs(nn->rip_ticks)) &&
20211820Sjulian					   (metric < ntohs(nn->rip_metric))) {
20311820Sjulian					nn->rip_metric = htons(metric);
20411820Sjulian					nn->rip_ticks = htons(ticks);
20511820Sjulian				}
20611820Sjulian				goto next;
20711820Sjulian			}
20811820Sjulian		}
20911820Sjulian		n->rip_dst = net;
21011820Sjulian		n->rip_metric = htons(metric);
21111820Sjulian		n->rip_ticks = htons(ticks);
21211820Sjulian		n++;
21311820Sjulian	next:;
21411820Sjulian	}
21511820Sjulian	if (doinghost) {
21611820Sjulian		doinghost = 0;
21711820Sjulian		base = nethash;
21811820Sjulian		goto again;
21911820Sjulian	}
22011820Sjulian	if (n != msg->rip_nets) {
22111820Sjulian		size = (char *)n - (char *)msg;
22211820Sjulian		(*output)(ripsock, flags, dst, size);
22311820Sjulian		TRACE_OUTPUT(ifp, dst, size);
22411820Sjulian	}
22511820Sjulian}
226