Pull morse(6) into the new millenium and teach it to use sound(4).
[dragonfly.git] / usr.sbin / IPXrouted / input.c
blobd1fa38112349aed0292f1e48ffe4793a0b12b9dd
1 /*
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
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.
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.
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.3 2004/03/11 09:38:59 hmp Exp $
41 * @(#)input.c 8.1 (Berkeley) 6/5/93
45 * IPX Routing Table Management Daemon
47 #include "defs.h"
49 struct sockaddr *
50 ipx_nettosa(union ipx_net net)
52 static struct sockaddr_ipx sxn;
54 bzero(&sxn, sizeof (struct sockaddr_ipx));
55 sxn.sipx_family = AF_IPX;
56 sxn.sipx_len = sizeof (sxn);
57 sxn.sipx_addr.x_net = net;
58 return( (struct sockaddr *)&sxn);
63 * Process a newly received packet.
65 void
66 rip_input(struct sockaddr *from, int size)
68 int newsize;
69 int rtchanged = 0;
70 struct rt_entry *rt;
71 struct netinfo *n;
72 struct interface *ifp = 0;
73 struct afswitch *afp;
74 struct sockaddr_ipx *ipxp;
76 ifp = if_ifwithnet(from);
77 ipxp = (struct sockaddr_ipx *)from;
78 if (ifp == 0) {
79 if(ftrace) {
80 fprintf(ftrace, "Received bogus packet from %s\n",
81 ipxdp_ntoa(&ipxp->sipx_addr));
83 return;
86 TRACE_INPUT(ifp, from, size);
87 if (from->sa_family >= AF_MAX)
88 return;
89 afp = &afswitch[from->sa_family];
91 size -= sizeof (u_short) /* command */;
92 n = msg->rip_nets;
94 switch (ntohs(msg->rip_cmd)) {
96 case RIPCMD_REQUEST:
97 if (ipx_hosteq(satoipx_addr(ifp->int_addr), ipxp->sipx_addr))
98 return;
99 newsize = 0;
100 while (size > 0) {
101 if (size < sizeof (struct netinfo))
102 break;
103 size -= sizeof (struct netinfo);
106 * A single entry with rip_dst == DSTNETS_ALL and
107 * metric ``infinity'' means ``all routes''.
109 * XXX According to the IPX RIP spec the metric
110 * and tick fields can be anything. So maybe we
111 * should not check the metric???
113 if (ipx_neteqnn(n->rip_dst, ipx_anynet) &&
114 ntohs(n->rip_metric) == HOPCNT_INFINITY &&
115 size == 0) {
116 supply(from, 0, ifp, 0);
117 return;
120 * request for specific nets
122 rt = rtlookup(ipx_nettosa(n->rip_dst));
123 if (ftrace) {
124 fprintf(ftrace,
125 "specific request for %s",
126 ipxdp_nettoa(n->rip_dst));
127 fprintf(ftrace,
128 " yields route %x\n",
129 (u_int)rt);
132 * XXX We break out on the first net that isn't
133 * found. The specs is a bit vague here. I'm not
134 * sure what we should do.
136 if (rt == 0)
137 return;
138 /* XXX
139 * According to the spec we should not include
140 * information about networks for which the number
141 * of hops is 16.
143 if (rt->rt_metric == (HOPCNT_INFINITY-1))
144 return;
145 n->rip_metric = htons( rt == 0 ? HOPCNT_INFINITY :
146 min(rt->rt_metric+1, HOPCNT_INFINITY));
147 n->rip_ticks = htons(rt->rt_ticks+1);
150 * We use split horizon with a twist. If the requested
151 * net is the directly connected net we supply an
152 * answer. This is so that the host can learn about
153 * the routers on its net.
156 struct rt_entry *trt = rt;
158 while (trt) {
159 if ((trt->rt_ifp == ifp) &&
160 !ipx_neteqnn(n->rip_dst,
161 satoipx_addr(ifp->int_addr).x_net))
162 return;
163 trt = trt->rt_clone;
165 n++;
166 newsize += sizeof (struct netinfo);
169 if (newsize > 0) {
170 msg->rip_cmd = htons(RIPCMD_RESPONSE);
171 newsize += sizeof (u_short);
172 /* should check for if with dstaddr(from) first */
173 (*afp->af_output)(ripsock, 0, from, newsize);
174 TRACE_OUTPUT(ifp, from, newsize);
175 if (ftrace) {
176 /* XXX This should not happen anymore. */
177 if(ifp == 0)
178 fprintf(ftrace, "--- ifp = 0\n");
179 else
180 fprintf(ftrace,
181 "request arrived on interface %s\n",
182 ifp->int_name);
185 return;
187 case RIPCMD_RESPONSE:
188 /* verify message came from a router */
189 if ((*afp->af_portmatch)(from) == 0)
190 return;
191 (*afp->af_canon)(from);
192 /* are we talking to ourselves? */
193 if ((ifp = if_ifwithaddr(from)) != 0) {
194 rt = rtfind(from);
195 if (rt == 0 || (rt->rt_state & RTS_INTERFACE) == 0) {
196 addrouteforif(ifp);
197 rtchanged = 1;
198 } else
199 rt->rt_timer = 0;
200 return;
202 /* Update timer for interface on which the packet arrived.
203 * If from other end of a point-to-point link that isn't
204 * in the routing tables, (re-)add the route.
206 if ((rt = rtfind(from)) && (rt->rt_state & RTS_INTERFACE)) {
207 if(ftrace) fprintf(ftrace, "Got route\n");
208 rt->rt_timer = 0;
209 } else if ((ifp = if_ifwithdstaddr(from)) != 0) {
210 if(ftrace) fprintf(ftrace, "Got partner\n");
211 addrouteforif(ifp);
212 rtchanged = 1;
214 for (; size > 0; size -= sizeof (struct netinfo), n++) {
215 struct sockaddr *sa;
216 if (size < sizeof (struct netinfo))
217 break;
218 if ((unsigned) ntohs(n->rip_metric) > HOPCNT_INFINITY)
219 continue;
220 rt = rtfind(sa = ipx_nettosa(n->rip_dst));
221 if (rt == 0) {
222 if (ntohs(n->rip_metric) == HOPCNT_INFINITY)
223 continue;
224 rtadd(sa, from, ntohs(n->rip_metric),
225 ntohs(n->rip_ticks), 0);
226 rtchanged = 1;
227 continue;
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.
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.
243 if (!equal(from, &rt->rt_router) &&
244 ntohs(n->rip_ticks) == rt->rt_ticks &&
245 ntohs(n->rip_metric) == rt->rt_metric &&
246 ntohs(n->rip_metric) != HOPCNT_INFINITY) {
247 struct rt_entry *trt = rt->rt_clone;
249 while (trt) {
250 if (equal(from, &trt->rt_router)) {
251 trt->rt_timer = 0;
252 break;
254 trt = trt->rt_clone;
256 if (trt == NULL) {
257 rtadd_clone(rt, sa, from,
258 ntohs(n->rip_metric),
259 ntohs(n->rip_ticks), 0);
261 continue;
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 ntohs(n->rip_metric) != HOPCNT_INFINITY)) {
272 rtchange(rt, from, ntohs(n->rip_metric),
273 ntohs(n->rip_ticks));
274 if (ntohs(n->rip_metric) == HOPCNT_INFINITY)
275 rt->rt_timer = EXPIRE_TIME;
276 else
277 rt->rt_timer = 0;
278 rtchanged = 1;
279 } else if (equal(from, &rt->rt_router) &&
280 (ntohs(n->rip_ticks) == rt->rt_ticks) &&
281 (ntohs(n->rip_metric) == rt->rt_metric) &&
282 (ntohs(n->rip_metric) != HOPCNT_INFINITY)) {
283 rt->rt_timer = 0;
286 if (rtchanged) {
287 struct rthash *rh;
288 struct rt_entry *rt;
290 toall(supply, NULL, 1);
291 for (rh = nethash; rh < &nethash[ROUTEHASHSIZ]; rh++)
292 for (rt = rh->rt_forw;
293 rt != (struct rt_entry *)rh;
294 rt = rt->rt_forw)
295 rt->rt_state &= ~RTS_CHANGED;
298 return;