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. 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.
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
38 * $FreeBSD: src/usr.sbin/IPXrouted/input.c,v 1.7 1999/08/28 01:15:02 peter Exp $
39 * $DragonFly: src/usr.sbin/IPXrouted/input.c,v 1.2 2003/06/17 04:29:52 dillon Exp $
41 * @(#)input.c 8.1 (Berkeley) 6/5/93
45 * IPX Routing Table Management Daemon
53 static struct sockaddr_ipx sxn;
55 bzero(&sxn, sizeof (struct sockaddr_ipx));
56 sxn.sipx_family = AF_IPX;
57 sxn.sipx_len = sizeof (sxn);
58 sxn.sipx_addr.x_net = net;
59 return( (struct sockaddr *)&sxn);
64 * Process a newly received packet.
68 struct sockaddr *from;
75 struct interface *ifp = 0;
77 struct sockaddr_ipx *ipxp;
79 ifp = if_ifwithnet(from);
80 ipxp = (struct sockaddr_ipx *)from;
83 fprintf(ftrace, "Received bogus packet from %s\n",
84 ipxdp_ntoa(&ipxp->sipx_addr));
89 TRACE_INPUT(ifp, from, size);
90 if (from->sa_family >= AF_MAX)
92 afp = &afswitch[from->sa_family];
94 size -= sizeof (u_short) /* command */;
97 switch (ntohs(msg->rip_cmd)) {
100 if (ipx_hosteq(satoipx_addr(ifp->int_addr), ipxp->sipx_addr))
104 if (size < sizeof (struct netinfo))
106 size -= sizeof (struct netinfo);
109 * A single entry with rip_dst == DSTNETS_ALL and
110 * metric ``infinity'' means ``all routes''.
112 * XXX According to the IPX RIP spec the metric
113 * and tick fields can be anything. So maybe we
114 * should not check the metric???
116 if (ipx_neteqnn(n->rip_dst, ipx_anynet) &&
117 ntohs(n->rip_metric) == HOPCNT_INFINITY &&
119 supply(from, 0, ifp, 0);
123 * request for specific nets
125 rt = rtlookup(ipx_nettosa(n->rip_dst));
128 "specific request for %s",
129 ipxdp_nettoa(n->rip_dst));
131 " yields route %x\n",
135 * XXX We break out on the first net that isn't
136 * found. The specs is a bit vague here. I'm not
137 * sure what we should do.
142 * According to the spec we should not include
143 * information about networks for which the number
146 if (rt->rt_metric == (HOPCNT_INFINITY-1))
148 n->rip_metric = htons( rt == 0 ? HOPCNT_INFINITY :
149 min(rt->rt_metric+1, HOPCNT_INFINITY));
150 n->rip_ticks = htons(rt->rt_ticks+1);
153 * We use split horizon with a twist. If the requested
154 * net is the directly connected net we supply an
155 * answer. This is so that the host can learn about
156 * the routers on its net.
159 register struct rt_entry *trt = rt;
162 if ((trt->rt_ifp == ifp) &&
163 !ipx_neteqnn(n->rip_dst,
164 satoipx_addr(ifp->int_addr).x_net))
169 newsize += sizeof (struct netinfo);
173 msg->rip_cmd = htons(RIPCMD_RESPONSE);
174 newsize += sizeof (u_short);
175 /* should check for if with dstaddr(from) first */
176 (*afp->af_output)(ripsock, 0, from, newsize);
177 TRACE_OUTPUT(ifp, from, newsize);
179 /* XXX This should not happen anymore. */
181 fprintf(ftrace, "--- ifp = 0\n");
184 "request arrived on interface %s\n",
190 case RIPCMD_RESPONSE:
191 /* verify message came from a router */
192 if ((*afp->af_portmatch)(from) == 0)
194 (*afp->af_canon)(from);
195 /* are we talking to ourselves? */
196 if ((ifp = if_ifwithaddr(from)) != 0) {
198 if (rt == 0 || (rt->rt_state & RTS_INTERFACE) == 0) {
205 /* Update timer for interface on which the packet arrived.
206 * If from other end of a point-to-point link that isn't
207 * in the routing tables, (re-)add the route.
209 if ((rt = rtfind(from)) && (rt->rt_state & RTS_INTERFACE)) {
210 if(ftrace) fprintf(ftrace, "Got route\n");
212 } else if ((ifp = if_ifwithdstaddr(from)) != 0) {
213 if(ftrace) fprintf(ftrace, "Got partner\n");
217 for (; size > 0; size -= sizeof (struct netinfo), n++) {
219 if (size < sizeof (struct netinfo))
221 if ((unsigned) ntohs(n->rip_metric) > HOPCNT_INFINITY)
223 rt = rtfind(sa = ipx_nettosa(n->rip_dst));
225 if (ntohs(n->rip_metric) == HOPCNT_INFINITY)
227 rtadd(sa, from, ntohs(n->rip_metric),
228 ntohs(n->rip_ticks), 0);
234 * A clone is a different route to the same net
235 * with exactly the same cost (ticks and metric).
236 * They must all be recorded because those interfaces
237 * must be handled in the same way as the first route
238 * to that net. ie When using the split horizon
239 * algorithm we must look at these interfaces also.
241 * Update if from gateway and different,
242 * from anywhere and less ticks or
243 * if same ticks and shorter,
244 * or getting stale and equivalent.
246 if (!equal(from, &rt->rt_router) &&
247 ntohs(n->rip_ticks) == rt->rt_ticks &&
248 ntohs(n->rip_metric) == rt->rt_metric &&
249 ntohs(n->rip_metric) != HOPCNT_INFINITY) {
250 register struct rt_entry *trt = rt->rt_clone;
253 if (equal(from, &trt->rt_router)) {
260 rtadd_clone(rt, sa, from,
261 ntohs(n->rip_metric),
262 ntohs(n->rip_ticks), 0);
266 if ((equal(from, &rt->rt_router) &&
267 ((ntohs(n->rip_ticks) != rt->rt_ticks) ||
268 (ntohs(n->rip_metric) != rt->rt_metric))) ||
269 (ntohs(n->rip_ticks) < rt->rt_ticks) ||
270 ((ntohs(n->rip_ticks) == rt->rt_ticks) &&
271 (ntohs(n->rip_metric) < rt->rt_metric)) ||
272 (rt->rt_timer > (EXPIRE_TIME*2/3) &&
273 rt->rt_metric == ntohs(n->rip_metric) &&
274 ntohs(n->rip_metric) != HOPCNT_INFINITY)) {
275 rtchange(rt, from, ntohs(n->rip_metric),
276 ntohs(n->rip_ticks));
277 if (ntohs(n->rip_metric) == HOPCNT_INFINITY)
278 rt->rt_timer = EXPIRE_TIME;
282 } else if (equal(from, &rt->rt_router) &&
283 (ntohs(n->rip_ticks) == rt->rt_ticks) &&
284 (ntohs(n->rip_metric) == rt->rt_metric) &&
285 (ntohs(n->rip_metric) != HOPCNT_INFINITY)) {
290 register struct rthash *rh;
291 register struct rt_entry *rt;
293 toall(supply, NULL, 1);
294 for (rh = nethash; rh < &nethash[ROUTEHASHSIZ]; rh++)
295 for (rt = rh->rt_forw;
296 rt != (struct rt_entry *)rh;
298 rt->rt_state &= ~RTS_CHANGED;