input.c revision 11820
1/*
2 * Copyright (c) 1985, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Copyright (c) 1995 John Hay.  All rights reserved.
6 *
7 * This file includes significant work done at Cornell University by
8 * Bill Nesheim.  That work included by permission.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	$Id: input.c,v 1.9 1995/10/11 18:57:17 jhay Exp $
39 */
40
41#ifndef lint
42static char sccsid[] = "@(#)input.c	8.1 (Berkeley) 6/5/93";
43#endif /* not lint */
44
45/*
46 * IPX Routing Table Management Daemon
47 */
48#include "defs.h"
49
50struct sockaddr *
51ipx_nettosa(net)
52union ipx_net net;
53{
54	static struct sockaddr_ipx sxn;
55	extern char ether_broadcast_addr[6];
56
57	bzero(&sxn, sizeof (struct sockaddr_ipx));
58	sxn.sipx_family = AF_IPX;
59	sxn.sipx_len = sizeof (sxn);
60	sxn.sipx_addr.x_net = net;
61	sxn.sipx_addr.x_host = *(union ipx_host *)ether_broadcast_addr;
62	return( (struct sockaddr *)&sxn);
63
64}
65
66/*
67 * Process a newly received packet.
68 */
69void
70rip_input(from, size)
71	struct sockaddr *from;
72	int size;
73{
74	struct rt_entry *rt;
75	struct netinfo *n;
76	struct interface *ifp = 0;
77	int newsize;
78	struct afswitch *afp;
79	struct sockaddr_ipx *ipxp;
80
81	ifp = if_ifwithnet(from);
82	ipxp = (struct sockaddr_ipx *)from;
83	if (ifp == 0) {
84		if(ftrace) {
85			fprintf(ftrace, "Received bogus packet from %s\n",
86				ipxdp_ntoa(&ipxp->sipx_addr));
87		}
88		return;
89	}
90
91	TRACE_INPUT(ifp, from, size);
92	if (from->sa_family >= AF_MAX)
93		return;
94	afp = &afswitch[from->sa_family];
95
96	size -= sizeof (u_short)	/* command */;
97	n = msg->rip_nets;
98
99	switch (ntohs(msg->rip_cmd)) {
100
101	case RIPCMD_REQUEST:
102		if (ipx_hosteq(satoipx_addr(ifp->int_addr), ipxp->sipx_addr))
103			return;
104		newsize = 0;
105		while (size > 0) {
106			if (size < sizeof (struct netinfo))
107				break;
108			size -= sizeof (struct netinfo);
109
110			/*
111			 * A single entry with rip_dst == DSTNETS_ALL and
112			 * metric ``infinity'' means ``all routes''.
113			 *
114			 * XXX According to the IPX RIP spec the metric
115			 * and tick fields can be anything. So maybe we
116			 * should not check the metric???
117			 */
118			if (ipx_neteqnn(n->rip_dst, ipx_anynet) &&
119		            ntohs(n->rip_metric) == HOPCNT_INFINITY &&
120			    size == 0) {
121				supply(from, 0, ifp);
122				return;
123			}
124			/*
125			 * request for specific nets
126			 */
127			rt = rtlookup(ipx_nettosa(n->rip_dst));
128			if (ftrace) {
129				fprintf(ftrace,
130					"specific request for %s",
131					ipxdp_nettoa(n->rip_dst));
132				fprintf(ftrace,
133					" yields route %x\n",
134					(u_int)rt);
135			}
136			/*
137			 * XXX We break out on the first net that isn't
138			 * found. The specs is a bit vague here. I'm not
139			 * sure what we should do.
140			 */
141			if (rt == 0)
142				return;
143			/* XXX
144			 * According to the spec we should not include
145			 * information about networks for which the number
146			 * of hops is 16.
147			 */
148			if (rt->rt_metric == (HOPCNT_INFINITY-1))
149				return;
150			n->rip_metric = htons( rt == 0 ? HOPCNT_INFINITY :
151				min(rt->rt_metric+1, HOPCNT_INFINITY));
152			n->rip_ticks = htons(rt->rt_ticks+1);
153
154			/*
155			 * We use split horizon with a twist. If the requested
156			 * net is the directly connected net we supply an
157			 * answer. This is so that the host can learn about
158			 * the routers on its net.
159			 */
160			{
161				register struct rt_entry *trt = rt;
162
163				while (trt) {
164					if ((trt->rt_ifp == ifp) &&
165					    !ipx_neteqnn(n->rip_dst,
166						satoipx_addr(ifp->int_addr).x_net))
167						return;
168					trt = trt->rt_clone;
169				}
170				n++;
171		        	newsize += sizeof (struct netinfo);
172			}
173		}
174		if (newsize > 0) {
175			msg->rip_cmd = htons(RIPCMD_RESPONSE);
176			newsize += sizeof (u_short);
177			/* should check for if with dstaddr(from) first */
178			(*afp->af_output)(ripsock, 0, from, newsize);
179			TRACE_OUTPUT(ifp, from, newsize);
180			if (ftrace) {
181				/* XXX This should not happen anymore. */
182				if(ifp == 0)
183					fprintf(ftrace, "--- ifp = 0\n");
184				else
185					fprintf(ftrace,
186						"request arrived on interface %s\n",
187						ifp->int_name);
188			}
189		}
190		return;
191
192	case RIPCMD_RESPONSE:
193		/* verify message came from a router */
194		if ((*afp->af_portmatch)(from) == 0)
195			return;
196		(*afp->af_canon)(from);
197		/* are we talking to ourselves? */
198		if ((ifp = if_ifwithaddr(from)) != 0) {
199			rt = rtfind(from);
200			if (rt == 0 || (rt->rt_state & RTS_INTERFACE) == 0)
201				addrouteforif(ifp);
202			else
203				rt->rt_timer = 0;
204			return;
205		}
206		/* Update timer for interface on which the packet arrived.
207		 * If from other end of a point-to-point link that isn't
208		 * in the routing tables, (re-)add the route.
209		 */
210		if ((rt = rtfind(from)) && (rt->rt_state & RTS_INTERFACE)) {
211			if(ftrace) fprintf(ftrace, "Got route\n");
212			rt->rt_timer = 0;
213		} else if ((ifp = if_ifwithdstaddr(from)) != 0) {
214			if(ftrace) fprintf(ftrace, "Got partner\n");
215			addrouteforif(ifp);
216		}
217		for (; size > 0; size -= sizeof (struct netinfo), n++) {
218			struct sockaddr *sa;
219			if (size < sizeof (struct netinfo))
220				break;
221			if ((unsigned) ntohs(n->rip_metric) >= HOPCNT_INFINITY)
222				continue;
223			rt = rtfind(sa = ipx_nettosa(n->rip_dst));
224			if (rt == 0) {
225				rtadd(sa, from, ntohs(n->rip_metric),
226					ntohs(n->rip_ticks), 0);
227				continue;
228			}
229
230			/*
231			 * A clone is a different route to the same net
232			 * with exactly the same cost (ticks and metric).
233			 * They must all be recorded because those interfaces
234			 * must be handled in the same way as the first route
235			 * to that net. ie When using the split horizon
236			 * algorithm we must look at these interfaces also.
237			 *
238			 * Update if from gateway and different,
239			 * from anywhere and less ticks or
240			 * if same ticks and shorter,
241			 * or getting stale and equivalent.
242			 *
243			 * XXX I don't think this is quite right.
244			 */
245			if (!equal(from, &rt->rt_router) &&
246			    ntohs(n->rip_ticks == rt->rt_ticks) &&
247			    ntohs(n->rip_metric == rt->rt_metric)) {
248				register struct rt_entry *trt = rt->rt_clone;
249
250				while (trt) {
251					if (equal(from, &trt->rt_router)) {
252						trt->rt_timer = 0;
253						break;
254					}
255				}
256				if (trt == NULL) {
257					rtadd_clone(rt, sa, from,
258						    ntohs(n->rip_metric),
259						    ntohs(n->rip_ticks), 0);
260				}
261				continue;
262			}
263			if ((equal(from, &rt->rt_router) &&
264			    ((ntohs(n->rip_ticks) != rt->rt_ticks) ||
265			    (ntohs(n->rip_metric) != rt->rt_metric))) ||
266			    (ntohs(n->rip_ticks) < rt->rt_ticks) ||
267			    ((ntohs(n->rip_ticks) == rt->rt_ticks) &&
268			    (ntohs(n->rip_metric) < rt->rt_metric)) ||
269			    (rt->rt_timer > (EXPIRE_TIME*2/3) &&
270			    rt->rt_metric == ntohs(n->rip_metric))) {
271				rtchange(rt, from, ntohs(n->rip_metric),
272					ntohs(n->rip_ticks));
273				rt->rt_timer = 0;
274			} else if (equal(from, &rt->rt_router) &&
275				   (ntohs(n->rip_ticks) == rt->rt_ticks) &&
276				   (ntohs(n->rip_metric) == rt->rt_metric)) {
277				rt->rt_timer = 0;
278			}
279		}
280		return;
281	}
282}
283