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 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * $FreeBSD: src/usr.sbin/IPXrouted/tables.c,v 1.7 1999/08/28 01:15:05 peter Exp $
39 static char sccsid[] = "@(#)tables.c 8.1 (Berkeley) 6/5/93";
43 * Routing Table Management Daemon
46 #include <sys/ioctl.h>
55 #define FIXLEN(s) { if ((s)->sa_len == 0) (s)->sa_len = sizeof (*(s));}
57 int install = !DEBUG; /* if 1 call kernel */
60 struct rthash nethash[ROUTEHASHSIZ];
63 * Lookup dst in the tables for an exact match.
69 register struct rt_entry *rt;
70 register struct rthash *rh;
74 if (dst->sa_family >= AF_MAX)
76 (*afswitch[dst->sa_family].af_hash)(dst, &h);
78 rh = &nethash[hash & ROUTEHASHMASK];
79 for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
80 if (rt->rt_hash != hash)
82 if (equal(&rt->rt_dst, dst))
89 * Find a route to dst as the kernel would.
95 register struct rt_entry *rt;
96 register struct rthash *rh;
99 int af = dst->sa_family;
104 (*afswitch[af].af_hash)(dst, &h);
106 hash = h.afh_nethash;
107 rh = &nethash[hash & ROUTEHASHMASK];
108 match = afswitch[af].af_netmatch;
109 for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
110 if (rt->rt_hash != hash)
112 if (rt->rt_dst.sa_family == af &&
113 (*match)(&rt->rt_dst, dst))
120 rtadd(dst, gate, metric, ticks, state)
121 struct sockaddr *dst, *gate;
126 register struct rt_entry *rt;
128 int af = dst->sa_family, flags;
135 (*afswitch[af].af_hash)(dst, &h);
136 flags = (*afswitch[af].af_ishost)(dst) ? RTF_HOST : 0;
137 hash = h.afh_nethash;
138 rh = &nethash[hash & ROUTEHASHMASK];
139 rt = (struct rt_entry *)malloc(sizeof (*rt));
144 rt->rt_router = *gate;
145 rt->rt_metric = metric;
146 rt->rt_ticks = ticks;
148 rt->rt_flags = RTF_UP | flags;
149 rt->rt_state = state | RTS_CHANGED;
150 rt->rt_ifp = if_ifwithnet(&rt->rt_router);
153 rt->rt_flags |= RTF_GATEWAY;
155 TRACE_ACTION("ADD", rt);
157 * If the ioctl fails because the gateway is unreachable
158 * from this host, discard the entry. This should only
159 * occur because of an incorrect entry in /etc/gateways.
161 if (install && rtioctl(ADD, &rt->rt_rt) < 0) {
164 if (errno == ENETUNREACH) {
165 TRACE_ACTION("DELETE", rt);
173 rtadd_clone(ort, dst, gate, metric, ticks, state)
174 struct rt_entry *ort;
175 struct sockaddr *dst, *gate;
180 register struct rt_entry *rt;
182 int af = dst->sa_family, flags;
189 (*afswitch[af].af_hash)(dst, &h);
190 flags = (*afswitch[af].af_ishost)(dst) ? RTF_HOST : 0;
191 hash = h.afh_nethash;
192 rh = &nethash[hash & ROUTEHASHMASK];
193 rt = (struct rt_entry *)malloc(sizeof (*rt));
198 rt->rt_router = *gate;
199 rt->rt_metric = metric;
200 rt->rt_ticks = ticks;
202 rt->rt_flags = RTF_UP | flags;
203 rt->rt_state = state | RTS_CHANGED;
204 rt->rt_ifp = if_ifwithnet(&rt->rt_router);
209 rt->rt_flags |= RTF_GATEWAY;
211 while(ort->rt_clone != NULL)
214 TRACE_ACTION("ADD_CLONE", rt);
218 rtchange(rt, gate, metric, ticks)
220 struct sockaddr *gate;
223 int doioctl = 0, metricchanged = 0;
224 struct rtuentry oldroute;
228 * Handling of clones.
229 * When the route changed and it had clones, handle it special.
230 * 1. If the new route is cheaper than the clone(s), free the clones.
231 * 2. If the new route is the same cost, it may be one of the clones,
232 * search for it and free it.
233 * 3. If the new route is more expensive than the clone(s), use the
234 * values of the clone(s).
237 if ((ticks < rt->rt_clone->rt_ticks) ||
238 ((ticks == rt->rt_clone->rt_ticks) &&
239 (metric < rt->rt_clone->rt_metric))) {
243 struct rt_entry *trt, *nrt;
252 } else if ((ticks == rt->rt_clone->rt_ticks) &&
253 (metric == rt->rt_clone->rt_metric)) {
254 struct rt_entry *prt, *trt;
260 if (equal(&trt->rt_router, gate)) {
261 prt->rt_clone = trt->rt_clone;
271 * Use the values of the first clone.
272 * Delete the corresponding clone.
274 struct rt_entry *trt;
277 rt->rt_clone = rt->rt_clone->rt_clone;
278 metric = trt->rt_metric;
279 ticks = trt->rt_ticks;
280 *gate = trt->rt_router;
285 if (!equal(&rt->rt_router, gate))
287 if ((metric != rt->rt_metric) || (ticks != rt->rt_ticks))
289 if (doioctl || metricchanged) {
290 TRACE_ACTION("CHANGE FROM", rt);
292 oldroute = rt->rt_rt;
293 rt->rt_router = *gate;
295 rt->rt_metric = metric;
296 rt->rt_ticks = ticks;
297 if ((rt->rt_state & RTS_INTERFACE) && metric) {
298 rt->rt_state &= ~RTS_INTERFACE;
301 "changing route from interface %s (timed out)",
302 rt->rt_ifp->int_name);
305 "changing route from interface ??? (timed out)");
308 rt->rt_flags |= RTF_GATEWAY;
310 rt->rt_flags &= ~RTF_GATEWAY;
311 rt->rt_ifp = if_ifwithnet(&rt->rt_router);
312 rt->rt_state |= RTS_CHANGED;
313 TRACE_ACTION("CHANGE TO", rt);
315 if (doioctl && install) {
317 if (rtioctl(ADD, &rt->rt_rt) < 0)
318 syslog(LOG_ERR, "rtioctl ADD dst %s, gw %s: %m",
319 ipx_ntoa(&((struct sockaddr_ipx *)&rt->rt_dst)->sipx_addr),
320 ipx_ntoa(&((struct sockaddr_ipx *)&rt->rt_router)->sipx_addr));
321 if (delete && rtioctl(DELETE, &oldroute) < 0)
322 perror("rtioctl DELETE");
325 if (rtioctl(ADD, &rt->rt_rt) >= 0)
328 if (rtioctl(CHANGE, &rt->rt_rt) >= 0)
331 syslog(LOG_ERR, "rtioctl ADD dst %s, gw %s: %m",
332 ipxdp_ntoa(&((struct sockaddr_ipx *)&rt->rt_dst)->sipx_addr),
333 ipxdp_ntoa(&((struct sockaddr_ipx *)&rt->rt_router)->sipx_addr));
343 struct sockaddr *sa = &(rt->rt_router);
349 * If there is a clone we just do a rt_change to it.
351 struct rt_entry *trt = rt->rt_clone;
352 rtchange(rt, &trt->rt_router, trt->rt_metric, trt->rt_ticks);
355 if (rt->rt_state & RTS_INTERFACE) {
358 "deleting route to interface %s (timed out)",
359 rt->rt_ifp->int_name);
362 "deleting route to interface ??? (timed out)");
364 TRACE_ACTION("DELETE", rt);
365 if (install && rtioctl(DELETE, &rt->rt_rt) < 0)
366 perror("rtioctl DELETE");
374 register struct rthash *rh;
376 for (rh = nethash; rh < &nethash[ROUTEHASHSIZ]; rh++)
377 rh->rt_forw = rh->rt_back = (struct rt_entry *)rh;
384 struct rtuentry *ort;
390 ort->rtu_rtflags = ort->rtu_flags;
395 return (ioctl(s, SIOCADDRT, (char *)ort));
398 return (ioctl(s, SIOCDELRT, (char *)ort));
405 struct rt_msghdr w_rtm;
406 struct sockaddr w_dst;
407 struct sockaddr w_gate;
408 struct sockaddr_ipx w_netmask;
412 bzero((char *)&w, sizeof(w));
413 rtm.rtm_msglen = sizeof(w);
414 rtm.rtm_version = RTM_VERSION;
415 rtm.rtm_type = (action == ADD ? RTM_ADD :
416 (action == DELETE ? RTM_DELETE : RTM_CHANGE));
417 rtm.rtm_flags = ort->rtu_flags;
418 rtm.rtm_seq = ++seqno;
419 rtm.rtm_addrs = RTA_DST|RTA_GATEWAY;
420 bcopy((char *)&ort->rtu_dst, (char *)&w.w_dst, sizeof(w.w_dst));
421 bcopy((char *)&ort->rtu_router, (char *)&w.w_gate, sizeof(w.w_gate));
422 w.w_gate.sa_family = w.w_dst.sa_family = AF_IPX;
423 w.w_gate.sa_len = w.w_dst.sa_len = sizeof(w.w_dst);
424 if (rtm.rtm_flags & RTF_HOST) {
425 rtm.rtm_msglen -= sizeof(w.w_netmask);
427 rtm.rtm_addrs |= RTA_NETMASK;
428 w.w_netmask = ipx_netmask;
429 rtm.rtm_msglen -= sizeof(w.w_netmask) - ipx_netmask.sipx_len;
432 return write(r, (char *)&w, rtm.rtm_msglen);