* The mrouted program is covered by the license in the accompanying file
* named "LICENSE". Use of the mrouted program represents acceptance of
* the terms and conditions listed in that file.
* The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
* Leland Stanford Junior University.
* $Id: vif.c,v 1.5 1993/06/24 05:11:16 deering Exp $
struct uvif uvifs
[MAXVIFS
]; /* array of virtual interfaces */
vifi_t numvifs
; /* number of vifs in use */
int vifs_down
; /* 1=>some interfaces are down */
int udp_socket
; /* Since the honkin' kernel doesn't support */
/* ioctls on raw IP sockets, we need a UDP */
/* socket as well as our IGMP (raw) socket. */
* Initialize the virtual interfaces.
int enabled_vifs
, enabled_phyints
;
* Configure the vifs based on the interface configuration of the
* the kernel and the contents of the configuration file.
* (Open a UDP socket for ioctl use in the config procedures.)
if ((udp_socket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
log(LOG_ERR
, errno
, "UDP socket");
config_vifs_from_kernel();
* Quit if there are fewer than two enabled vifs.
for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; ++vifi
, ++v
) {
if (!(v
->uv_flags
& VIFF_DISABLED
)) {
if (!(v
->uv_flags
& VIFF_TUNNEL
))
log(LOG_ERR
, 0, "can't forward: %s",
enabled_vifs
== 0 ? "no enabled vifs" : "only one enabled vif");
if (enabled_phyints
== 0)
"no enabled interfaces, forwarding via tunnels only");
* Start routing on all virtual interfaces that are not down or
* administratively disabled.
for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; ++vifi
, ++v
) {
if (!(v
->uv_flags
& VIFF_DISABLED
)) {
if (!(v
->uv_flags
& VIFF_DOWN
))
"%s is not yet up; vif #%u not in service",
* See if any interfaces have changed from up state to down, or vice versa,
* including any non-multicast-capable interfaces that are in use as local
* tunnel end-points. Ignore interfaces that have been administratively
for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; ++vifi
, ++v
) {
if (v
->uv_flags
& VIFF_DISABLED
) continue;
strncpy(ifr
.ifr_name
, v
->uv_name
, IFNAMSIZ
);
if (ioctl(udp_socket
, SIOCGIFFLAGS
, (char *)&ifr
) < 0)
"ioctl SIOCGIFFLAGS for %s", ifr
.ifr_name
);
if (v
->uv_flags
& VIFF_DOWN
) {
if (ifr
.ifr_flags
& IFF_UP
) {
v
->uv_flags
&= ~VIFF_DOWN
;
"%s has come up; vif #%u now in service",
if (!(ifr
.ifr_flags
& IFF_UP
)) {
v
->uv_flags
|= VIFF_DOWN
;
"%s has gone down; vif #%u taken out of service",
* Start routing on the specified virtual interface.
static void start_vif(vifi
)
dst
= (v
->uv_flags
& VIFF_TUNNEL
) ? v
->uv_rmt_addr
: dvmrp_group
;
* Install the interface in the kernel's vif structure.
k_add_vif(vifi
, &uvifs
[vifi
]);
* Update the existing route entries to take into account the new vif.
if (!(v
->uv_flags
& VIFF_TUNNEL
)) {
* Join the DVMRP multicast group on the interface.
* (This is not strictly necessary, since the kernel promiscuously
* receives IGMP packets addressed to ANY IP multicast group while
* multicast routing is enabled. However, joining the group allows
* this host to receive non-IGMP packets as well, such as 'pings'.)
k_join(dvmrp_group
, src
);
* Install an entry in the routing table for the subnet to which
* the interface is connected.
update_route(v
->uv_subnet
, v
->uv_subnetmask
, 0, 0, vifi
);
* Until neighbors are discovered, assume responsibility for sending
* periodic group membership queries to the subnet. Send the first
v
->uv_flags
|= VIFF_QUERIER
;
send_igmp(src
, allhosts_group
, IGMP_HOST_MEMBERSHIP_QUERY
, 0, 0, 0);
* Send a probe via the new vif to look for neighbors.
send_igmp(src
, dst
, IGMP_DVMRP
, DVMRP_PROBE
, htonl(MROUTED_LEVEL
), 0);
* Stop routing on the specified virtual interface.
static void stop_vif(vifi
)
if (!(v
->uv_flags
& VIFF_TUNNEL
)) {
* Depart from the DVMRP multicast group on the interface.
k_leave(dvmrp_group
, v
->uv_lcl_addr
);
* Update the entry in the routing table for the subnet to which
* the interface is connected, to take into account the interface
update_route(v
->uv_subnet
, v
->uv_subnetmask
, UNREACHABLE
, 0, vifi
);
* Discard all group addresses. (No need to tell kernel;
* the k_del_vif() call, below, will clean up kernel state.)
while (v
->uv_groups
!= NULL
) {
v
->uv_groups
= a
->al_next
;
v
->uv_flags
&= ~VIFF_QUERIER
;
* Update the existing route entries to take into account the vif failure.
delete_vif_from_routes(vifi
);
* Delete the interface from the kernel's vif structure.
* Discard all neighbor addresses.
while (v
->uv_neighbors
!= NULL
) {
v
->uv_neighbors
= a
->al_next
;
* Find the virtual interface from which an incoming packet arrived,
* based on the packet's source and destination IP addresses.
vifi_t
find_vif(src
, dst
)
for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; ++vifi
, ++v
) {
if (!(v
->uv_flags
& (VIFF_DOWN
|VIFF_DISABLED
))) {
if (v
->uv_flags
& VIFF_TUNNEL
) {
if (src
== v
->uv_rmt_addr
&& dst
== v
->uv_lcl_addr
)
if ((src
& v
->uv_subnetmask
) == v
->uv_subnet
&&
src
!= v
->uv_subnetbcast
)
* Send group membership queries to all subnets for which I am querier.
for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; vifi
++, v
++) {
if (v
->uv_flags
& VIFF_QUERIER
) {
send_igmp(v
->uv_lcl_addr
, allhosts_group
,
IGMP_HOST_MEMBERSHIP_QUERY
, 0, 0, 0);
* Process an incoming group membership report.
void accept_group_report(src
, dst
, group
)
register struct listaddr
*g
;
if ((vifi
= find_vif(src
, dst
)) == NO_VIF
||
(uvifs
[vifi
].uv_flags
& VIFF_TUNNEL
)) {
"ignoring group membership report from non-adjacent host %s",
* Look for the group in our group list; if found, reset its timer.
for (g
= v
->uv_groups
; g
!= NULL
; g
= g
->al_next
) {
if (group
== g
->al_addr
) {
* If not found, add it to the list and tell the kernel.
g
= (struct listaddr
*)malloc(sizeof(struct listaddr
));
log(LOG_ERR
, 0, "ran out of memory"); /* fatal */
g
->al_next
= v
->uv_groups
;
k_add_group(vifi
, group
);
* Send a probe on all vifs from which no neighbors have been heard recently.
void probe_for_neighbors()
for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; vifi
++, v
++) {
if (!(v
->uv_flags
& (VIFF_DOWN
|VIFF_DISABLED
)) &&
v
->uv_neighbors
== NULL
) {
send_igmp(v
->uv_lcl_addr
,
(v
->uv_flags
& VIFF_TUNNEL
) ? v
->uv_rmt_addr
IGMP_DVMRP
, DVMRP_PROBE
, htonl(MROUTED_LEVEL
), 0);
* Send a list of all of our neighbors to the requestor, `src'.
void accept_neighbor_request(src
, dst
)
u_long temp_addr
, us
, them
= src
;
/* Determine which of our addresses to use as the source of our response
if (IN_MULTICAST(ntohl(dst
))) { /* query sent to a multicast group */
int udp
; /* find best interface to reply on */
int addrlen
= sizeof(addr
);
addr
.sin_family
= AF_INET
;
addr
.sin_addr
.s_addr
= dst
;
addr
.sin_port
= htons(2000); /* any port over 1024 will do... */
if ((udp
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0
|| connect(udp
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0
|| getsockname(udp
, (struct sockaddr
*) &addr
, &addrlen
) < 0) {
log(LOG_WARNING
, errno
, "Determining local address");
us
= addr
.sin_addr
.s_addr
;
} else /* query sent to us alone */
#define PUT_ADDR(a) temp_addr = ntohl(a); \
*p++ = temp_addr >> 24; \
*p++ = (temp_addr >> 16) & 0xFF; \
*p++ = (temp_addr >> 8) & 0xFF; \
p
= (u_char
*) (send_buf
+ MIN_IP_HEADER_LEN
+ IGMP_MINLEN
);
for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; vifi
++, v
++) {
if (v
->uv_flags
& VIFF_DISABLED
)
for (la
= v
->uv_neighbors
; la
; la
= la
->al_next
) {
/* Make sure that there's room for this neighbor... */
if (datalen
+ (ncount
== 0 ? 4 + 3 + 4 : 4) > MAX_DVMRP_DATA_LEN
) {
send_igmp(us
, them
, IGMP_DVMRP
, DVMRP_NEIGHBORS
,
htonl(MROUTED_LEVEL
), datalen
);
p
= (u_char
*) (send_buf
+ MIN_IP_HEADER_LEN
+ IGMP_MINLEN
);
/* Put out the header for this neighbor list... */
PUT_ADDR(v
->uv_lcl_addr
);
send_igmp(us
, them
, IGMP_DVMRP
, DVMRP_NEIGHBORS
, htonl(MROUTED_LEVEL
),
* Send a list of all of our neighbors to the requestor, `src'.
void accept_neighbor_request2(src
, dst
)
u_long temp_addr
, us
, them
= src
;
/* Determine which of our addresses to use as the source of our response
if (IN_MULTICAST(ntohl(dst
))) { /* query sent to a multicast group */
int udp
; /* find best interface to reply on */
int addrlen
= sizeof(addr
);
addr
.sin_family
= AF_INET
;
addr
.sin_addr
.s_addr
= dst
;
addr
.sin_port
= htons(2000); /* any port over 1024 will do... */
if ((udp
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0
|| connect(udp
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0
|| getsockname(udp
, (struct sockaddr
*) &addr
, &addrlen
) < 0) {
log(LOG_WARNING
, errno
, "Determining local address");
us
= addr
.sin_addr
.s_addr
;
} else /* query sent to us alone */
p
= (u_char
*) (send_buf
+ MIN_IP_HEADER_LEN
+ IGMP_MINLEN
);
for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; vifi
++, v
++) {
register u_short vflags
= v
->uv_flags
;
register u_char rflags
= 0;
if (vflags
& VIFF_TUNNEL
)
rflags
|= DVMRP_NF_TUNNEL
;
rflags
|= DVMRP_NF_SRCRT
;
if (vflags
& VIFF_DISABLED
)
rflags
|= DVMRP_NF_DISABLED
;
if (vflags
& VIFF_QUERIER
)
rflags
|= DVMRP_NF_QUERIER
;
* include down & disabled interfaces and interfaces on
if (rflags
& DVMRP_NF_TUNNEL
)
if (datalen
> MAX_DVMRP_DATA_LEN
- 12) {
send_igmp(us
, them
, IGMP_DVMRP
, DVMRP_NEIGHBORS2
,
htonl(MROUTED_LEVEL
), datalen
);
p
= (u_char
*) (send_buf
+ MIN_IP_HEADER_LEN
+ IGMP_MINLEN
);
*(u_int
*)p
= v
->uv_lcl_addr
;
*(u_int
*)p
= v
->uv_rmt_addr
;
for ( ; la
; la
= la
->al_next
) {
/* Make sure that there's room for this neighbor... */
if (datalen
+ (ncount
== 0 ? 4+4+4 : 4) > MAX_DVMRP_DATA_LEN
) {
send_igmp(us
, them
, IGMP_DVMRP
, DVMRP_NEIGHBORS2
,
htonl(MROUTED_LEVEL
), datalen
);
p
= (u_char
*) (send_buf
+ MIN_IP_HEADER_LEN
+ IGMP_MINLEN
);
/* Put out the header for this neighbor list... */
*(u_int
*)p
= v
->uv_lcl_addr
;
*(u_int
*)p
= la
->al_addr
;
send_igmp(us
, them
, IGMP_DVMRP
, DVMRP_NEIGHBORS2
, htonl(MROUTED_LEVEL
),
* Process an incoming neighbor-list message.
void accept_neighbors(src
, dst
, p
, datalen
, level
)
log(LOG_INFO
, 0, "ignoring spurious DVMRP neighbor list from %s to %s",
inet_fmt(src
, s1
), inet_fmt(dst
, s2
));
* Process an incoming neighbor-list message.
void accept_neighbors2(src
, dst
, p
, datalen
, level
)
log(LOG_INFO
, 0, "ignoring spurious DVMRP neighbor list2 from %s to %s",
inet_fmt(src
, s1
), inet_fmt(dst
, s2
));
* Update the neighbor entry for neighbor 'addr' on vif 'vifi'.
* 'msgtype' is the type of DVMRP message received from the neighbor.
* Return TRUE if 'addr' is a valid neighbor, FALSE otherwise.
int update_neighbor(vifi
, addr
, msgtype
)
register struct listaddr
*n
;
* Confirm that 'addr' is a valid neighbor address on vif 'vifi'.
* IT IS ASSUMED that this was preceded by a call to find_vif(), which
* checks that 'addr' is either a valid remote tunnel endpoint or a
* non-broadcast address belonging to a directly-connected subnet.
* Therefore, here we check only that 'addr' is not our own address
* (due to an impostor or erroneous loopback) or an address of the form
* {subnet,0} ("the unknown host"). These checks are not performed in
* find_vif() because those types of address are acceptable for some
* types of IGMP message (such as group membership reports).
if (!(v
->uv_flags
& VIFF_TUNNEL
) &&
(addr
== v
->uv_lcl_addr
||
addr
== v
->uv_subnet
)) {
"received DVMRP message from 'the unknown host' or self: %s",
* If we have received a route report from a neighbor, and we believed
* that we had no neighbors on this vif, send a full route report to
* all neighbors on the vif.
if (msgtype
== DVMRP_REPORT
&& v
->uv_neighbors
== NULL
)
(v
->uv_flags
& VIFF_TUNNEL
) ? addr
: dvmrp_group
);
* Look for addr in list of neighbors; if found, reset its timer.
for (n
= v
->uv_neighbors
; n
!= NULL
; n
= n
->al_next
) {
if (addr
== n
->al_addr
) {
* If not found, add it to the list. If the neighbor has a lower
* IP address than me, yield querier duties to it.
n
= (struct listaddr
*)malloc(sizeof(struct listaddr
));
log(LOG_ERR
, 0, "ran out of memory"); /* fatal */
n
->al_next
= v
->uv_neighbors
;
if (!(v
->uv_flags
& VIFF_TUNNEL
) &&
ntohl(addr
) < ntohl(v
->uv_lcl_addr
))
v
->uv_flags
&= ~VIFF_QUERIER
;
* On every timer interrupt, advance the timer in each neighbor and
* group entry on every vif.
register struct listaddr
*a
, *prev_a
, *n
;
for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; ++vifi
, ++v
) {
for (prev_a
= (struct listaddr
*)&(v
->uv_neighbors
),
prev_a
= a
, a
= a
->al_next
) {
if ((a
->al_timer
+= TIMER_INTERVAL
) < NEIGHBOR_EXPIRE_TIME
)
* Neighbor has expired; delete it from the neighbor list,
* delete it from the 'dominants' and 'subordinates arrays of
* any route entries and assume querier duties unless there is
* another neighbor with a lower IP address than mine.
prev_a
->al_next
= a
->al_next
;
delete_neighbor_from_routes(addr
, vifi
);
if (!(v
->uv_flags
& VIFF_TUNNEL
)) {
v
->uv_flags
|= VIFF_QUERIER
;
for (n
= v
->uv_neighbors
; n
!= NULL
; n
= n
->al_next
) {
if (ntohl(n
->al_addr
) < ntohl(v
->uv_lcl_addr
)) {
v
->uv_flags
&= ~VIFF_QUERIER
;
for (prev_a
= (struct listaddr
*)&(v
->uv_groups
),
prev_a
= a
, a
= a
->al_next
) {
if ((a
->al_timer
+= TIMER_INTERVAL
) < GROUP_EXPIRE_TIME
)
* Group has expired; tell kernel and delete from group list.
k_del_group(vifi
, a
->al_addr
);
prev_a
->al_next
= a
->al_next
;
* Print the contents of the uvifs array on file 'fp'.
register struct listaddr
*a
;
"\nVirtual Interface Table\n%s",
" Vif Local-Address Metric Thresh Flags\n");
for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; vifi
++, v
++) {
fprintf(fp
, " %2u %-15s %6s: %-15s %4u %7u ",
inet_fmt(v
->uv_lcl_addr
, s1
),
(v
->uv_flags
& VIFF_TUNNEL
) ?
(v
->uv_flags
& VIFF_TUNNEL
) ?
inet_fmt(v
->uv_rmt_addr
, s2
) :
inet_fmts(v
->uv_subnet
, v
->uv_subnetmask
, s3
),
if (v
->uv_flags
& VIFF_DOWN
) fprintf(fp
, " down");
if (v
->uv_flags
& VIFF_DISABLED
) fprintf(fp
, " disabled");
if (v
->uv_flags
& VIFF_QUERIER
) fprintf(fp
, " querier");
if (v
->uv_flags
& VIFF_SRCRT
) fprintf(fp
, " src-rt");
if (v
->uv_neighbors
!= NULL
) {
fprintf(fp
, " peers : %-15s\n",
inet_fmt(v
->uv_neighbors
->al_addr
, s1
));
for (a
= v
->uv_neighbors
->al_next
; a
!= NULL
; a
= a
->al_next
) {
inet_fmt(a
->al_addr
, s1
));
if (v
->uv_groups
!= NULL
) {
fprintf(fp
, " groups: %-15s\n",
inet_fmt(v
->uv_groups
->al_addr
, s1
));
for (a
= v
->uv_groups
->al_next
; a
!= NULL
; a
= a
->al_next
) {
inet_fmt(a
->al_addr
, s1
));