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
39 static const char sccsid[] = "@(#)tables.c 8.1 (Berkeley) 6/5/93";
43 * Routing Table Management Daemon
46 #include <sys/ioctl.h>
56 #define FIXLEN(s) { if ((s)->sa_len == 0) (s)->sa_len = sizeof (*(s));}
58 int install = !DEBUG; /* if 1 call kernel */
61 struct rthash nethash[ROUTEHASHSIZ];
64 * Lookup dst in the tables for an exact match.
67 rtlookup(struct sockaddr *dst)
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.
92 rtfind(struct sockaddr *dst)
94 register struct rt_entry *rt;
95 register struct rthash *rh;
98 int af = dst->sa_family;
103 (*afswitch[af].af_hash)(dst, &h);
105 hash = h.afh_nethash;
106 rh = &nethash[hash & ROUTEHASHMASK];
107 match = afswitch[af].af_netmatch;
108 for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
109 if (rt->rt_hash != hash)
111 if (rt->rt_dst.sa_family == af &&
112 (*match)(&rt->rt_dst, dst))
119 rtadd(struct sockaddr *dst, struct sockaddr *gate, short metric,
120 short ticks, int state)
123 register struct rt_entry *rt;
125 int af = dst->sa_family, flags;
132 (*afswitch[af].af_hash)(dst, &h);
133 flags = (*afswitch[af].af_ishost)(dst) ? RTF_HOST : 0;
134 hash = h.afh_nethash;
135 rh = &nethash[hash & ROUTEHASHMASK];
136 rt = (struct rt_entry *)malloc(sizeof (*rt));
141 rt->rt_router = *gate;
142 rt->rt_metric = metric;
143 rt->rt_ticks = ticks;
145 rt->rt_flags = RTF_UP | flags;
146 rt->rt_state = state | RTS_CHANGED;
147 rt->rt_ifp = if_ifwithnet(&rt->rt_router);
150 rt->rt_flags |= RTF_GATEWAY;
152 TRACE_ACTION("ADD", rt);
154 * If the ioctl fails because the gateway is unreachable
155 * from this host, discard the entry. This should only
156 * occur because of an incorrect entry in /etc/gateways.
158 if (install && rtioctl(ADD, &rt->rt_rt) < 0) {
161 if (errno == ENETUNREACH) {
162 TRACE_ACTION("DELETE", rt);
170 rtadd_clone(struct rt_entry *ort, struct sockaddr *dst,
171 struct sockaddr *gate, short metric, short ticks, int state)
174 register struct rt_entry *rt;
175 int af = dst->sa_family, flags;
182 (*afswitch[af].af_hash)(dst, &h);
183 flags = (*afswitch[af].af_ishost)(dst) ? RTF_HOST : 0;
184 hash = h.afh_nethash;
185 rt = (struct rt_entry *)malloc(sizeof (*rt));
190 rt->rt_router = *gate;
191 rt->rt_metric = metric;
192 rt->rt_ticks = ticks;
194 rt->rt_flags = RTF_UP | flags;
195 rt->rt_state = state | RTS_CHANGED;
196 rt->rt_ifp = if_ifwithnet(&rt->rt_router);
201 rt->rt_flags |= RTF_GATEWAY;
203 while(ort->rt_clone != NULL)
206 TRACE_ACTION("ADD_CLONE", rt);
210 rtchange(struct rt_entry *rt, struct sockaddr *gate, short metric,
213 int doioctl = 0, metricchanged = 0;
217 * Handling of clones.
218 * When the route changed and it had clones, handle it special.
219 * 1. If the new route is cheaper than the clone(s), free the clones.
220 * 2. If the new route is the same cost, it may be one of the clones,
221 * search for it and free it.
222 * 3. If the new route is more expensive than the clone(s), use the
223 * values of the clone(s).
226 if ((ticks < rt->rt_clone->rt_ticks) ||
227 ((ticks == rt->rt_clone->rt_ticks) &&
228 (metric < rt->rt_clone->rt_metric))) {
232 struct rt_entry *trt, *nrt;
241 } else if ((ticks == rt->rt_clone->rt_ticks) &&
242 (metric == rt->rt_clone->rt_metric)) {
243 struct rt_entry *prt, *trt;
249 if (equal(&trt->rt_router, gate)) {
250 prt->rt_clone = trt->rt_clone;
260 * Use the values of the first clone.
261 * Delete the corresponding clone.
263 struct rt_entry *trt;
266 rt->rt_clone = rt->rt_clone->rt_clone;
267 metric = trt->rt_metric;
268 ticks = trt->rt_ticks;
269 *gate = trt->rt_router;
274 if (!equal(&rt->rt_router, gate))
276 if ((metric != rt->rt_metric) || (ticks != rt->rt_ticks))
278 if (doioctl || metricchanged) {
279 TRACE_ACTION("CHANGE FROM", rt);
281 rt->rt_router = *gate;
283 rt->rt_metric = metric;
284 rt->rt_ticks = ticks;
285 if ((rt->rt_state & RTS_INTERFACE) && metric) {
286 rt->rt_state &= ~RTS_INTERFACE;
289 "changing route from interface %s (timed out)",
290 rt->rt_ifp->int_name);
293 "changing route from interface ??? (timed out)");
296 rt->rt_flags |= RTF_GATEWAY;
298 rt->rt_flags &= ~RTF_GATEWAY;
299 rt->rt_ifp = if_ifwithnet(&rt->rt_router);
300 rt->rt_state |= RTS_CHANGED;
301 TRACE_ACTION("CHANGE TO", rt);
303 if (doioctl && install) {
305 if (rtioctl(ADD, &rt->rt_rt) < 0)
306 syslog(LOG_ERR, "rtioctl ADD dst %s, gw %s: %m",
307 ipx_ntoa(&((struct sockaddr_ipx *)&rt->rt_dst)->sipx_addr),
308 ipx_ntoa(&((struct sockaddr_ipx *)&rt->rt_router)->sipx_addr));
309 if (delete && rtioctl(DELETE, &oldroute) < 0)
310 perror("rtioctl DELETE");
313 if (rtioctl(ADD, &rt->rt_rt) >= 0)
316 if (rtioctl(CHANGE, &rt->rt_rt) >= 0)
319 syslog(LOG_ERR, "rtioctl ADD dst %s, gw %s: %m",
320 ipxdp_ntoa(&((struct sockaddr_ipx *)&rt->rt_dst)->sipx_addr),
321 ipxdp_ntoa(&((struct sockaddr_ipx *)&rt->rt_router)->sipx_addr));
327 rtdelete(struct rt_entry *rt)
330 struct sockaddr *sa = &(rt->rt_router);
336 * If there is a clone we just do a rt_change to it.
338 struct rt_entry *trt = rt->rt_clone;
339 rtchange(rt, &trt->rt_router, trt->rt_metric, trt->rt_ticks);
342 if (rt->rt_state & RTS_INTERFACE) {
345 "deleting route to interface %s (timed out)",
346 rt->rt_ifp->int_name);
349 "deleting route to interface ??? (timed out)");
351 TRACE_ACTION("DELETE", rt);
352 if (install && rtioctl(DELETE, &rt->rt_rt) < 0)
353 perror("rtioctl DELETE");
361 register struct rthash *rh;
363 for (rh = nethash; rh < &nethash[ROUTEHASHSIZ]; rh++)
364 rh->rt_forw = rh->rt_back = (struct rt_entry *)rh;
369 rtioctl(int action, struct rtuentry *ort)
375 ort->rtu_rtflags = ort->rtu_flags;
380 return (ioctl(s, SIOCADDRT, (char *)ort));
383 return (ioctl(s, SIOCDELRT, (char *)ort));
390 struct rt_msghdr w_rtm;
391 struct sockaddr w_dst;
392 struct sockaddr w_gate;
393 struct sockaddr_ipx w_netmask;
397 bzero((char *)&w, sizeof(w));
398 rtm.rtm_msglen = sizeof(w);
399 rtm.rtm_version = RTM_VERSION;
400 rtm.rtm_type = (action == ADD ? RTM_ADD :
401 (action == DELETE ? RTM_DELETE : RTM_CHANGE));
402 rtm.rtm_flags = ort->rtu_flags;
403 rtm.rtm_seq = ++seqno;
404 rtm.rtm_addrs = RTA_DST|RTA_GATEWAY;
405 bcopy((char *)&ort->rtu_dst, (char *)&w.w_dst, sizeof(w.w_dst));
406 bcopy((char *)&ort->rtu_router, (char *)&w.w_gate, sizeof(w.w_gate));
407 w.w_gate.sa_family = w.w_dst.sa_family = AF_IPX;
408 w.w_gate.sa_len = w.w_dst.sa_len = sizeof(w.w_dst);
409 if (rtm.rtm_flags & RTF_HOST) {
410 rtm.rtm_msglen -= sizeof(w.w_netmask);
412 rtm.rtm_addrs |= RTA_NETMASK;
413 w.w_netmask = ipx_netmask;
414 rtm.rtm_msglen -= sizeof(w.w_netmask) - ipx_netmask.sipx_len;
417 return write(r, (char *)&w, rtm.rtm_msglen);