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 *
3850479Speter * $FreeBSD$
3911820Sjulian */
4011820Sjulian
4111820Sjulian#ifndef lint
42122760Strhodesstatic const 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
5827244Sjhaytoall(f, except, changesonly)
5927244Sjhay	void (*f)(struct sockaddr *, int, struct interface *, int);
6011820Sjulian	struct rt_entry *except;
6127244Sjhay	int changesonly;
6211820Sjulian{
6311820Sjulian	register struct interface *ifp;
6411820Sjulian	register struct sockaddr *dst;
6511820Sjulian	register int flags;
6611820Sjulian	register struct rt_entry *trt;
6711820Sjulian	int onlist;
6811820Sjulian	extern struct interface *ifnet;
6911820Sjulian
7011820Sjulian	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
7111820Sjulian		if (ifp->int_flags & IFF_PASSIVE)
7211820Sjulian			continue;
7311820Sjulian
7411820Sjulian		/*
7511820Sjulian		 * Don't send it on interfaces in the except list.
7611820Sjulian		 */
7711820Sjulian		onlist = 0;
7811820Sjulian		trt = except;
7911820Sjulian		while(trt) {
8011820Sjulian			if (ifp == trt->rt_ifp) {
8111820Sjulian				onlist = 1;
8211820Sjulian				break;
8311820Sjulian			}
8411820Sjulian			trt = trt->rt_clone;
8511820Sjulian		}
8611820Sjulian		if (onlist)
8711820Sjulian			continue;
8811820Sjulian
8911820Sjulian		dst = ifp->int_flags & IFF_BROADCAST ? &ifp->int_broadaddr :
9011820Sjulian		      ifp->int_flags & IFF_POINTOPOINT ? &ifp->int_dstaddr :
9111820Sjulian		      &ifp->int_addr;
9211820Sjulian		flags = ifp->int_flags & IFF_INTERFACE ? MSG_DONTROUTE : 0;
9327244Sjhay		(*f)(dst, flags, ifp, changesonly);
9411820Sjulian	}
9511820Sjulian}
9611820Sjulian
9711820Sjulian/*
9811820Sjulian * Output a preformed packet.
9911820Sjulian */
10011820Sjulianvoid
10127244Sjhaysndmsg(dst, flags, ifp, changesonly)
10211820Sjulian	struct sockaddr *dst;
10311820Sjulian	int flags;
10411820Sjulian	struct interface *ifp;
10527244Sjhay	int changesonly;
10611820Sjulian{
10711820Sjulian
10811820Sjulian	(*afswitch[dst->sa_family].af_output)
10911820Sjulian		(ripsock, flags, dst, sizeof (struct rip));
11011820Sjulian	TRACE_OUTPUT(ifp, dst, sizeof (struct rip));
11111820Sjulian}
11211820Sjulian
11311820Sjulian/*
11411820Sjulian * Supply dst with the contents of the routing tables.
11511820Sjulian * If this won't fit in one packet, chop it up into several.
11611820Sjulian *
11711820Sjulian * This must be done using the split horizon algorithm.
11811820Sjulian * 1. Don't send routing info to the interface from where it was received.
11911820Sjulian * 2. Don't publish an interface to itself.
12011820Sjulian * 3. If a route is received from more than one interface and the cost is
12111820Sjulian *    the same, don't publish it on either interface. I am calling this
12211820Sjulian *    clones.
12311820Sjulian */
12411820Sjulianvoid
12527244Sjhaysupply(dst, flags, ifp, changesonly)
12611820Sjulian	struct sockaddr *dst;
12711820Sjulian	int flags;
12811820Sjulian	struct interface *ifp;
12927244Sjhay	int changesonly;
13011820Sjulian{
13111820Sjulian	register struct rt_entry *rt;
13211820Sjulian	register struct rt_entry *crt; /* Clone route */
13311820Sjulian	register struct rthash *rh;
13411820Sjulian	register struct netinfo *nn;
13511820Sjulian	register struct netinfo *n = msg->rip_nets;
13611820Sjulian	struct sockaddr_ipx *sipx =  (struct sockaddr_ipx *) dst;
13711820Sjulian	af_output_t *output = afswitch[dst->sa_family].af_output;
13827244Sjhay	int size, metric, ticks;
13911820Sjulian	union ipx_net net;
14019948Sjhay	int delay = 0;
14111820Sjulian
14211820Sjulian	if (sipx->sipx_port == 0)
14311820Sjulian		sipx->sipx_port = htons(IPXPORT_RIP);
14411820Sjulian
14511820Sjulian	msg->rip_cmd = ntohs(RIPCMD_RESPONSE);
14627244Sjhay	for (rh = nethash; rh < &nethash[ROUTEHASHSIZ]; rh++)
14711820Sjulian	for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
14811820Sjulian		size = (char *)n - (char *)msg;
14915248Sjhay		if (size >= ((MAXRIPNETS * sizeof (struct netinfo)) +
15015248Sjhay				sizeof (msg->rip_cmd))) {
15111820Sjulian			(*output)(ripsock, flags, dst, size);
15211820Sjulian			TRACE_OUTPUT(ifp, dst, size);
15311820Sjulian			n = msg->rip_nets;
15419948Sjhay			delay++;
15519948Sjhay			if(delay == 2) {
15627244Sjhay				usleep(50000);
15719948Sjhay				delay = 0;
15819948Sjhay			}
15911820Sjulian		}
16011820Sjulian
16127244Sjhay		if (changesonly && !(rt->rt_state & RTS_CHANGED))
16227244Sjhay			continue;
16327244Sjhay
16411820Sjulian		/*
16511820Sjulian		 * This should do rule one and two of the split horizon
16611820Sjulian		 * algorithm.
16711820Sjulian		 */
16811820Sjulian		if (rt->rt_ifp == ifp)
16911820Sjulian			continue;
17011820Sjulian
17111820Sjulian		/*
17211820Sjulian		 * Rule 3.
17311820Sjulian		 * Look if we have clones (different routes to the same
17411820Sjulian		 * place with exactly the same cost).
17511820Sjulian		 *
17611820Sjulian		 * We should not publish on any of the clone interfaces.
17711820Sjulian		 */
17811820Sjulian		crt = rt->rt_clone;
17911820Sjulian		while (crt) {
18011820Sjulian			if (crt->rt_ifp == ifp)
18112630Sjulian				goto next;
18211820Sjulian			crt = crt->rt_clone;
18311820Sjulian		}
18411820Sjulian
18511820Sjulian		sipx = (struct sockaddr_ipx *)&rt->rt_dst;
18611820Sjulian	        if ((rt->rt_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
18711820Sjulian			sipx = (struct sockaddr_ipx *)&rt->rt_router;
18811820Sjulian		if (rt->rt_metric == HOPCNT_INFINITY)
18911820Sjulian			metric = HOPCNT_INFINITY;
19011820Sjulian		else {
19111820Sjulian			metric = rt->rt_metric + 1;
19211820Sjulian			/*
19311820Sjulian			 * We don't advertize routes with more than 15 hops.
19411820Sjulian			 */
19511820Sjulian			if (metric >= HOPCNT_INFINITY)
19611820Sjulian				continue;
19711820Sjulian		}
19811820Sjulian		/* XXX One day we should cater for slow interfaces also. */
19911820Sjulian		ticks = rt->rt_ticks + 1;
20011820Sjulian		net = sipx->sipx_addr.x_net;
20111820Sjulian
20211820Sjulian		/*
20311820Sjulian		 * Make sure that we don't put out a two net entries
20411820Sjulian		 * for a pt to pt link (one for the G route, one for the if)
20511820Sjulian		 * This is a kludge, and won't work if there are lots of nets.
20611820Sjulian		 */
20711820Sjulian		for (nn = msg->rip_nets; nn < n; nn++) {
20811820Sjulian			if (ipx_neteqnn(net, nn->rip_dst)) {
20911820Sjulian				if (ticks < ntohs(nn->rip_ticks)) {
21011820Sjulian					nn->rip_metric = htons(metric);
21111820Sjulian					nn->rip_ticks = htons(ticks);
21211820Sjulian				} else if ((ticks == ntohs(nn->rip_ticks)) &&
21311820Sjulian					   (metric < ntohs(nn->rip_metric))) {
21411820Sjulian					nn->rip_metric = htons(metric);
21511820Sjulian					nn->rip_ticks = htons(ticks);
21611820Sjulian				}
21711820Sjulian				goto next;
21811820Sjulian			}
21911820Sjulian		}
22011820Sjulian		n->rip_dst = net;
22111820Sjulian		n->rip_metric = htons(metric);
22211820Sjulian		n->rip_ticks = htons(ticks);
22311820Sjulian		n++;
22411820Sjulian	next:;
22511820Sjulian	}
22611820Sjulian	if (n != msg->rip_nets) {
22711820Sjulian		size = (char *)n - (char *)msg;
22811820Sjulian		(*output)(ripsock, flags, dst, size);
22911820Sjulian		TRACE_OUTPUT(ifp, dst, size);
23011820Sjulian	}
23111820Sjulian}
232