2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1995 John Hay. All rights reserved.
7 * This file includes significant work done at Cornell University by
8 * Bill Nesheim. That work included by permission.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $FreeBSD: src/usr.sbin/IPXrouted/input.c,v 1.7 1999/08/28 01:15:02 peter Exp $
36 * @(#)input.c 8.1 (Berkeley) 6/5/93
40 * IPX Routing Table Management Daemon
45 ipx_nettosa(union ipx_net net)
47 static struct sockaddr_ipx sxn;
49 bzero(&sxn, sizeof (struct sockaddr_ipx));
50 sxn.sipx_family = AF_IPX;
51 sxn.sipx_len = sizeof (sxn);
52 sxn.sipx_addr.x_net = net;
53 return( (struct sockaddr *)&sxn);
58 * Process a newly received packet.
61 rip_input(struct sockaddr *from, int size)
67 struct interface *ifp = NULL;
69 struct sockaddr_ipx *ipxp;
71 ifp = if_ifwithnet(from);
72 ipxp = (struct sockaddr_ipx *)from;
75 fprintf(ftrace, "Received bogus packet from %s\n",
76 ipxdp_ntoa(&ipxp->sipx_addr));
81 TRACE_INPUT(ifp, from, size);
82 if (from->sa_family >= AF_MAX)
84 afp = &afswitch[from->sa_family];
86 size -= sizeof (u_short) /* command */;
89 switch (ntohs(msg->rip_cmd)) {
92 if (ipx_hosteq(satoipx_addr(ifp->int_addr), ipxp->sipx_addr))
96 if (size < sizeof (struct netinfo))
98 size -= sizeof (struct netinfo);
101 * A single entry with rip_dst == DSTNETS_ALL and
102 * metric ``infinity'' means ``all routes''.
104 * XXX According to the IPX RIP spec the metric
105 * and tick fields can be anything. So maybe we
106 * should not check the metric???
108 if (ipx_neteqnn(n->rip_dst, ipx_anynet) &&
109 ntohs(n->rip_metric) == HOPCNT_INFINITY &&
111 supply(from, 0, ifp, 0);
115 * request for specific nets
117 rt = rtlookup(ipx_nettosa(n->rip_dst));
120 "specific request for %s",
121 ipxdp_nettoa(n->rip_dst));
123 " yields route %p\n",
127 * XXX We break out on the first net that isn't
128 * found. The specs is a bit vague here. I'm not
129 * sure what we should do.
134 * According to the spec we should not include
135 * information about networks for which the number
138 if (rt->rt_metric == (HOPCNT_INFINITY-1))
140 n->rip_metric = htons( rt == NULL ? HOPCNT_INFINITY :
141 min(rt->rt_metric+1, HOPCNT_INFINITY));
142 n->rip_ticks = htons(rt->rt_ticks+1);
145 * We use split horizon with a twist. If the requested
146 * net is the directly connected net we supply an
147 * answer. This is so that the host can learn about
148 * the routers on its net.
151 struct rt_entry *trt = rt;
154 if ((trt->rt_ifp == ifp) &&
155 !ipx_neteqnn(n->rip_dst,
156 satoipx_addr(ifp->int_addr).x_net))
161 newsize += sizeof (struct netinfo);
165 msg->rip_cmd = htons(RIPCMD_RESPONSE);
166 newsize += sizeof (u_short);
167 /* should check for if with dstaddr(from) first */
168 (*afp->af_output)(ripsock, 0, from, newsize);
169 TRACE_OUTPUT(ifp, from, newsize);
171 /* XXX This should not happen anymore. */
173 fprintf(ftrace, "--- ifp = 0\n");
176 "request arrived on interface %s\n",
182 case RIPCMD_RESPONSE:
183 /* verify message came from a router */
184 if ((*afp->af_portmatch)(from) == 0)
186 (*afp->af_canon)(from);
187 /* are we talking to ourselves? */
188 if ((ifp = if_ifwithaddr(from)) != NULL) {
190 if (rt == NULL || (rt->rt_state & RTS_INTERFACE) == 0) {
197 /* Update timer for interface on which the packet arrived.
198 * If from other end of a point-to-point link that isn't
199 * in the routing tables, (re-)add the route.
201 if ((rt = rtfind(from)) && (rt->rt_state & RTS_INTERFACE)) {
202 if(ftrace) fprintf(ftrace, "Got route\n");
204 } else if ((ifp = if_ifwithdstaddr(from)) != NULL) {
205 if(ftrace) fprintf(ftrace, "Got partner\n");
209 for (; size > 0; size -= sizeof (struct netinfo), n++) {
211 if (size < sizeof (struct netinfo))
213 if ((unsigned) ntohs(n->rip_metric) > HOPCNT_INFINITY)
215 rt = rtfind(sa = ipx_nettosa(n->rip_dst));
217 if (ntohs(n->rip_metric) == HOPCNT_INFINITY)
219 rtadd(sa, from, ntohs(n->rip_metric),
220 ntohs(n->rip_ticks), 0);
226 * A clone is a different route to the same net
227 * with exactly the same cost (ticks and metric).
228 * They must all be recorded because those interfaces
229 * must be handled in the same way as the first route
230 * to that net. ie When using the split horizon
231 * algorithm we must look at these interfaces also.
233 * Update if from gateway and different,
234 * from anywhere and less ticks or
235 * if same ticks and shorter,
236 * or getting stale and equivalent.
238 if (!equal(from, &rt->rt_router) &&
239 ntohs(n->rip_ticks) == rt->rt_ticks &&
240 ntohs(n->rip_metric) == rt->rt_metric &&
241 ntohs(n->rip_metric) != HOPCNT_INFINITY) {
242 struct rt_entry *trt = rt->rt_clone;
245 if (equal(from, &trt->rt_router)) {
252 rtadd_clone(rt, sa, from,
253 ntohs(n->rip_metric),
254 ntohs(n->rip_ticks), 0);
258 if ((equal(from, &rt->rt_router) &&
259 ((ntohs(n->rip_ticks) != rt->rt_ticks) ||
260 (ntohs(n->rip_metric) != rt->rt_metric))) ||
261 (ntohs(n->rip_ticks) < rt->rt_ticks) ||
262 ((ntohs(n->rip_ticks) == rt->rt_ticks) &&
263 (ntohs(n->rip_metric) < rt->rt_metric)) ||
264 (rt->rt_timer > (EXPIRE_TIME*2/3) &&
265 rt->rt_metric == ntohs(n->rip_metric) &&
266 ntohs(n->rip_metric) != HOPCNT_INFINITY)) {
267 rtchange(rt, from, ntohs(n->rip_metric),
268 ntohs(n->rip_ticks));
269 if (ntohs(n->rip_metric) == HOPCNT_INFINITY)
270 rt->rt_timer = EXPIRE_TIME;
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 (ntohs(n->rip_metric) != HOPCNT_INFINITY)) {
285 toall(supply, NULL, 1);
286 for (rh = nethash; rh < &nethash[ROUTEHASHSIZ]; rh++)
287 for (rt = rh->rt_forw;
288 rt != (struct rt_entry *)rh;
290 rt->rt_state &= ~RTS_CHANGED;