ba5706e086c2e694aa45ae56ac06eeea8e466b8f
* Copyright (c) 1983 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
static char sccsid
[] = "@(#)tables.c 5.9 (Berkeley) %G%";
* Routing Table Management Daemon
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
, ifmetric
;
(*afswitch
[af
].af_hash
)(dst
, &h
);
flags
= (*afswitch
[af
].af_rtflags
)(dst
);
* Subnet flag isn't visible to kernel, move to state. XXX
if (flags
& RTF_SUBNET
) {
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_ifwithdstaddr(&rt
->rt_router
);
rt
->rt_ifp
= if_ifwithnet(&rt
->rt_router
);
ifmetric
= rt
->rt_ifp
->int_metric
;
if ((state
& RTS_INTERFACE
) == 0)
rt
->rt_flags
|= RTF_GATEWAY
;
* Set rt_ifmetric to the amount by which we
* increment the route when sending it to others.
if (state
& RTS_INTERFACE
) {
rt
->rt_ifmetric
= metric
+ 1;
rt
->rt_ifmetric
= ifmetric
+ 1;
* 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
&& (rt
->rt_state
& (RTS_INTERNAL
| RTS_EXTERNAL
)) == 0 &&
ioctl(s
, SIOCADDRT
, (char *)&rt
->rt_rt
) < 0) {
dst
->sa_family
< af_max
&& gate
->sa_family
< af_max
)
"adding route to net/host %s through gateway %s: %m\n",
(*afswitch
[dst
->sa_family
].af_format
)(dst
),
(*afswitch
[gate
->sa_family
].af_format
)(gate
));
if (errno
== ENETUNREACH
) {
TRACE_ACTION(DELETE
, rt
);
rtchange(rt
, gate
, metric
)
int doioctl
= 0, metricchanged
= 0, delete = 0;
if (!equal(&rt
->rt_router
, gate
) && (rt
->rt_state
& RTS_INTERNAL
) == 0)
if (metric
!= rt
->rt_metric
) {
if (metric
== HOPCNT_INFINITY
)
if (doioctl
|| metricchanged
) {
TRACE_ACTION(CHANGE FROM
, rt
);
if ((rt
->rt_state
& RTS_INTERFACE
) &&
metric
> rt
->rt_ifp
->int_metric
) {
rt
->rt_state
&= ~RTS_INTERFACE
;
rt
->rt_flags
|= RTF_GATEWAY
;
"changing route from interface %s (timed out)",
rt
->rt_ifp
= if_ifwithdstaddr(&rt
->rt_router
);
rt
->rt_ifp
= if_ifwithnet(&rt
->rt_router
);
rt
->rt_state
|= RTS_CHANGED
;
TRACE_ACTION(CHANGE TO
, rt
);
if (ioctl(s
, SIOCADDRT
, (char *)&rt
->rt_rt
) < 0)
if ((doioctl
|| delete) && install
)
if (ioctl(s
, SIOCDELRT
, (char *)&oldroute
) < 0)
if (rt
->rt_state
& RTS_INTERFACE
)
syslog(LOG_ERR
, "deleting route to interface %s (timed out)",
TRACE_ACTION(DELETE
, rt
);
if (install
&& (rt
->rt_state
& (RTS_INTERNAL
| RTS_EXTERNAL
)) == 0 &&
ioctl(s
, SIOCDELRT
, (char *)&rt
->rt_rt
))
* If we have an interface to the wide, wide world,
* add an entry for an Internet default route (wildcard) to the internal
* tables and advertise it. This route is not added to the kernel routes,
* but this entry prevents us from listening to other people's defaults
* and installing them in the kernel here.
extern struct sockaddr inet_default
;
rtadd(&inet_default
, &inet_default
, 0,
RTS_CHANGED
| RTS_PASSIVE
| RTS_INTERNAL
);
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
;