* Copyright (c) 1985 The Regents of the University of California.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)tables.c 5.9 (Berkeley) 6/1/90";
* Routing Table Management Daemon
#define FIXLEN(s) { if ((s)->sa_len == 0) (s)->sa_len = sizeof (*(s));}
int install
= !DEBUG
; /* if 1 call kernel */
* Lookup dst in the tables for an exact match.
register struct rt_entry
*rt
;
register struct rthash
*rh
;
if (dst
->sa_family
>= AF_MAX
)
(*afswitch
[dst
->sa_family
].af_hash
)(dst
, &h
);
rh
= &hosthash
[hash
& ROUTEHASHMASK
];
for (rt
= rh
->rt_forw
; rt
!= (struct rt_entry
*)rh
; rt
= rt
->rt_forw
) {
if (equal(&rt
->rt_dst
, dst
))
rh
= &nethash
[hash
& ROUTEHASHMASK
];
* Find a route to dst as the kernel would.
register struct rt_entry
*rt
;
register struct rthash
*rh
;
int doinghost
= 1, (*match
)();
(*afswitch
[af
].af_hash
)(dst
, &h
);
rh
= &hosthash
[hash
& ROUTEHASHMASK
];
for (rt
= rh
->rt_forw
; rt
!= (struct rt_entry
*)rh
; rt
= rt
->rt_forw
) {
if (equal(&rt
->rt_dst
, dst
))
if (rt
->rt_dst
.sa_family
== af
&&
(*match
)(&rt
->rt_dst
, dst
))
rh
= &nethash
[hash
& ROUTEHASHMASK
];
match
= afswitch
[af
].af_netmatch
;
rtadd(dst
, gate
, metric
, state
)
struct sockaddr
*dst
, *gate
;
register struct rt_entry
*rt
;
int af
= dst
->sa_family
, flags
;
(*afswitch
[af
].af_hash
)(dst
, &h
);
flags
= (*afswitch
[af
].af_ishost
)(dst
) ? RTF_HOST
: 0;
rh
= &hosthash
[hash
& ROUTEHASHMASK
];
rh
= &nethash
[hash
& ROUTEHASHMASK
];
rt
= (struct rt_entry
*)malloc(sizeof (*rt
));
rt
->rt_flags
= RTF_UP
| flags
;
rt
->rt_state
= state
| RTS_CHANGED
;
rt
->rt_ifp
= if_ifwithnet(&rt
->rt_router
);
rt
->rt_flags
|= RTF_GATEWAY
;
* If the ioctl fails because the gateway is unreachable
* from this host, discard the entry. This should only
* occur because of an incorrect entry in /etc/gateways.
if (install
&& ioctl(s
, SIOCADDRT
, (char *)&rt
->rt_rt
) < 0) {
if (errno
== ENETUNREACH
) {
TRACE_ACTION(DELETE
, rt
);
rtchange(rt
, gate
, metric
)
int doioctl
= 0, metricchanged
= 0;
if (!equal(&rt
->rt_router
, gate
))
if (metric
!= rt
->rt_metric
)
if (doioctl
|| metricchanged
) {
TRACE_ACTION(CHANGE FROM
, rt
);
if ((rt
->rt_state
& RTS_INTERFACE
) && metric
) {
rt
->rt_state
&= ~RTS_INTERFACE
;
"changing route from interface %s (timed out)",
rt
->rt_flags
|= RTF_GATEWAY
;
rt
->rt_flags
&= ~RTF_GATEWAY
;
rt
->rt_state
|= RTS_CHANGED
;
TRACE_ACTION(CHANGE TO
, rt
);
if (doioctl
&& install
) {
if (ioctl(s
, SIOCADDRT
, (char *)&rt
->rt_rt
) < 0)
syslog(LOG_ERR
, "SIOCADDRT dst %s, gw %s: %m",
xns_ntoa(&((struct sockaddr_ns
*)&rt
->rt_dst
)->sns_addr
),
xns_ntoa(&((struct sockaddr_ns
*)&rt
->rt_router
)->sns_addr
));
if (delete && ioctl(s
, SIOCDELRT
, (char *)&oldroute
) < 0)
if (delete && ioctl(s
, SIOCDELRT
, (char *)&oldroute
) < 0)
if (ioctl(s
, SIOCADDRT
, (char *)&rt
->rt_rt
) < 0)
syslog(LOG_ERR
, "SIOCADDRT dst %s, gw %s: %m",
xns_ntoa(&((struct sockaddr_ns
*)&rt
->rt_dst
)->sns_addr
),
xns_ntoa(&((struct sockaddr_ns
*)&rt
->rt_router
)->sns_addr
));
struct sockaddr
*sa
= &(rt
->rt_rt
.rt_gateway
);
sa
= &(rt
->rt_rt
.rt_dst
);
if (rt
->rt_state
& RTS_INTERFACE
) {
syslog(LOG_ERR
, "deleting route to interface %s (timed out)",
TRACE_ACTION(DELETE
, rt
);
if (install
&& ioctl(s
, SIOCDELRT
, (char *)&rt
->rt_rt
))
register struct rthash
*rh
;
for (rh
= nethash
; rh
< &nethash
[ROUTEHASHSIZ
]; rh
++)
rh
->rt_forw
= rh
->rt_back
= (struct rt_entry
*)rh
;
for (rh
= hosthash
; rh
< &hosthash
[ROUTEHASHSIZ
]; rh
++)
rh
->rt_forw
= rh
->rt_back
= (struct rt_entry
*)rh
;