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.3 2004/03/11 09:38:59 hmp Exp $
41 * @(#)input.c 8.1 (Berkeley) 6/5/93
45 * IPX Routing Table Management Daemon
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.
66 rip_input(struct sockaddr
*from
, int size
)
72 struct interface
*ifp
= 0;
74 struct sockaddr_ipx
*ipxp
;
76 ifp
= if_ifwithnet(from
);
77 ipxp
= (struct sockaddr_ipx
*)from
;
80 fprintf(ftrace
, "Received bogus packet from %s\n",
81 ipxdp_ntoa(&ipxp
->sipx_addr
));
86 TRACE_INPUT(ifp
, from
, size
);
87 if (from
->sa_family
>= AF_MAX
)
89 afp
= &afswitch
[from
->sa_family
];
91 size
-= sizeof (u_short
) /* command */;
94 switch (ntohs(msg
->rip_cmd
)) {
97 if (ipx_hosteq(satoipx_addr(ifp
->int_addr
), ipxp
->sipx_addr
))
101 if (size
< sizeof (struct netinfo
))
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
&&
116 supply(from
, 0, ifp
, 0);
120 * request for specific nets
122 rt
= rtlookup(ipx_nettosa(n
->rip_dst
));
125 "specific request for %s",
126 ipxdp_nettoa(n
->rip_dst
));
128 " yields route %x\n",
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.
139 * According to the spec we should not include
140 * information about networks for which the number
143 if (rt
->rt_metric
== (HOPCNT_INFINITY
-1))
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
;
159 if ((trt
->rt_ifp
== ifp
) &&
160 !ipx_neteqnn(n
->rip_dst
,
161 satoipx_addr(ifp
->int_addr
).x_net
))
166 newsize
+= sizeof (struct netinfo
);
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
);
176 /* XXX This should not happen anymore. */
178 fprintf(ftrace
, "--- ifp = 0\n");
181 "request arrived on interface %s\n",
187 case RIPCMD_RESPONSE
:
188 /* verify message came from a router */
189 if ((*afp
->af_portmatch
)(from
) == 0)
191 (*afp
->af_canon
)(from
);
192 /* are we talking to ourselves? */
193 if ((ifp
= if_ifwithaddr(from
)) != 0) {
195 if (rt
== 0 || (rt
->rt_state
& RTS_INTERFACE
) == 0) {
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");
209 } else if ((ifp
= if_ifwithdstaddr(from
)) != 0) {
210 if(ftrace
) fprintf(ftrace
, "Got partner\n");
214 for (; size
> 0; size
-= sizeof (struct netinfo
), n
++) {
216 if (size
< sizeof (struct netinfo
))
218 if ((unsigned) ntohs(n
->rip_metric
) > HOPCNT_INFINITY
)
220 rt
= rtfind(sa
= ipx_nettosa(n
->rip_dst
));
222 if (ntohs(n
->rip_metric
) == HOPCNT_INFINITY
)
224 rtadd(sa
, from
, ntohs(n
->rip_metric
),
225 ntohs(n
->rip_ticks
), 0);
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
;
250 if (equal(from
, &trt
->rt_router
)) {
257 rtadd_clone(rt
, sa
, from
,
258 ntohs(n
->rip_metric
),
259 ntohs(n
->rip_ticks
), 0);
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
;
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
)) {
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
;
295 rt
->rt_state
&= ~RTS_CHANGED
;