* Copyright (c) 1983, 1988, 1993
* The Regents of the University of California. All rights reserved.
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)tables.c 8.1 (Berkeley) %G%";
* 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
];
struct sockaddr wildcard
; /* zero valued cookie for wildcard searches */
* 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
;
* Check for wildcard gateway, by convention network 0.
dst
= &wildcard
, hash
= 0;
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_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_dst
);
rt
->rt_ifp
= if_ifwithnet(&rt
->rt_router
);
if ((state
& RTS_INTERFACE
) == 0)
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 ((rt
->rt_state
& (RTS_INTERNAL
| RTS_EXTERNAL
)) == 0 &&
rtioctl(ADD
, &rt
->rt_rt
) < 0) {
if (errno
!= EEXIST
&& 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 add
= 0, delete = 0, newgateway
= 0;
struct rtuentry oldroute
;
FIXLEN(&(rt
->rt_router
));
if (!equal(&rt
->rt_router
, gate
)) {
TRACE_ACTION("CHANGE FROM ", rt
);
} else if (metric
!= rt
->rt_metric
)
TRACE_NEWMETRIC(rt
, metric
);
if ((rt
->rt_state
& RTS_INTERNAL
) == 0) {
* If changing to different router, we need to add
* new route and delete old one if in the kernel.
* If the router is the same, we need to delete
* the route if has become unreachable, or re-add
* it if it had been unreachable.
if (rt
->rt_metric
!= HOPCNT_INFINITY
)
} else if (metric
== HOPCNT_INFINITY
)
else if (rt
->rt_metric
== HOPCNT_INFINITY
)
if ((rt
->rt_state
& RTS_INTERFACE
) && delete) {
rt
->rt_state
&= ~RTS_INTERFACE
;
rt
->rt_flags
|= RTF_GATEWAY
;
if (metric
> rt
->rt_metric
&& delete)
syslog(LOG_ERR
, "%s route to interface %s (timed out)",
add
? "changing" : "deleting",
rt
->rt_ifp
? rt
->rt_ifp
->int_name
: "?");
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 (add
&& rtioctl(ADD
, &rt
->rt_rt
) < 0)
if (delete && rtioctl(DELETE
, &oldroute
) < 0)
if (rtioctl(DELETE
, &oldroute
) < 0)
} else if (!delete && add
) {
if (rtioctl(ADD
, &rt
->rt_rt
) < 0)
} else if (delete && add
) {
if (rtioctl(CHANGE
, &rt
->rt_rt
) < 0)
TRACE_ACTION("DELETE", rt
);
FIXLEN(&(rt
->rt_router
));
if (rt
->rt_metric
< HOPCNT_INFINITY
) {
if ((rt
->rt_state
& (RTS_INTERFACE
|RTS_INTERNAL
)) == RTS_INTERFACE
)
"deleting route to interface %s? (timed out?)",
if ((rt
->rt_state
& (RTS_INTERNAL
| RTS_EXTERNAL
)) == 0 &&
rtioctl(DELETE
, &rt
->rt_rt
) < 0)
register struct rthash
*rh
;
register struct rt_entry
*rt
;
struct rthash
*base
= hosthash
;
for (rh
= base
; rh
< &base
[ROUTEHASHSIZ
]; rh
++) {
for (; rt
!= (struct rt_entry
*)rh
; rt
= rt
->rt_forw
) {
if (rt
->rt_state
& RTS_INTERFACE
||
rt
->rt_metric
>= HOPCNT_INFINITY
)
TRACE_ACTION("DELETE", rt
);
if ((rt
->rt_state
& (RTS_INTERNAL
|RTS_EXTERNAL
)) == 0 &&
rtioctl(DELETE
, &rt
->rt_rt
) < 0)
* 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
, 1,
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
;
ort
->rtu_rtflags
= ort
->rtu_flags
;
return (ioctl(s
, SIOCADDRT
, (char *)ort
));
return (ioctl(s
, SIOCDELRT
, (char *)ort
));
struct sockaddr_in w_dst
;
struct sockaddr_in w_netmask
;
bzero((char *)&w
, sizeof(w
));
rtm
.rtm_msglen
= sizeof(w
);
rtm
.rtm_version
= RTM_VERSION
;
rtm
.rtm_type
= (action
== ADD
? RTM_ADD
:
(action
== DELETE
? RTM_DELETE
: RTM_CHANGE
));
rtm
.rtm_flags
= ort
->rtu_flags
;
rtm
.rtm_addrs
= RTA_DST
|RTA_GATEWAY
;
bcopy((char *)&ort
->rtu_dst
, (char *)&w
.w_dst
, sizeof(w
.w_dst
));
bcopy((char *)&ort
->rtu_router
, (char *)&w
.w_gate
, sizeof(w
.w_gate
));
w
.w_dst
.sin_family
= AF_INET
;
w
.w_dst
.sin_len
= sizeof(w
.w_dst
);
w
.w_gate
.sa_family
= AF_INET
;
w
.w_gate
.sa_len
= sizeof(w
.w_gate
);
if (rtm
.rtm_flags
& RTF_HOST
) {
rtm
.rtm_msglen
-= sizeof(w
.w_netmask
);
rtm
.rtm_addrs
|= RTA_NETMASK
;
w
.w_netmask
.sin_addr
.s_addr
=
inet_maskof(w
.w_dst
.sin_addr
.s_addr
);
for (cp
= (char *)(1 + &w
.w_netmask
.sin_addr
);
--cp
> (char *) &w
.w_netmask
; )
len
= cp
- (char *)&w
.w_netmask
;
w
.w_netmask
.sin_len
= len
;
len
= 1 + ((len
- 1) | (sizeof(long) - 1));
rtm
.rtm_msglen
-= (sizeof(w
.w_netmask
) - len
);
return (install
? write(r
, (char *)&w
, rtm
.rtm_msglen
) : (errno
= 0));