1 /* $NetBSD: vif.c,v 1.17 2006/05/25 01:38:41 christos Exp $ */
4 * The mrouted program is covered by the license in the accompanying file
5 * named "LICENSE". Use of the mrouted program represents acceptance of
6 * the terms and conditions listed in that file.
8 * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
9 * Leland Stanford Junior University.
19 struct uvif uvifs
[MAXVIFS
]; /* array of virtual interfaces */
20 vifi_t numvifs
; /* number of vifs in use */
21 int vifs_down
; /* 1=>some interfaces are down */
22 int phys_vif
; /* An enabled vif */
23 int udp_socket
; /* Since the honkin' kernel doesn't support */
24 /* ioctls on raw IP sockets, we need a UDP */
25 /* socket as well as our IGMP (raw) socket. */
27 int vifs_with_neighbors
; /* == 1 if I am a leaf */
36 * Forward declarations.
38 static void start_vif(vifi_t vifi
);
39 static void start_vif2(vifi_t vifi
);
40 static void stop_vif(vifi_t vifi
);
41 static void age_old_hosts(void);
42 static void send_probe_on_vif(struct uvif
*v
);
43 static int info_version(char *p
, size_t);
44 static void DelVif(void *arg
);
45 static int SetTimer(vifi_t vifi
, struct listaddr
*g
);
46 static int DeleteTimer(int id
);
47 static void SendQuery(void *arg
);
48 static int SetQueryTimer(struct listaddr
*g
, vifi_t vifi
, int to_expire
,
53 * Initialize the virtual interfaces, but do not install
54 * them in the kernel. Start routing on all vifs that are
55 * not down or disabled.
62 int enabled_vifs
, enabled_phyints
;
63 extern char *configfilename
;
66 vifs_with_neighbors
= 0;
70 * Configure the vifs based on the interface configuration of the
71 * the kernel and the contents of the configuration file.
72 * (Open a UDP socket for ioctl use in the config procedures.)
74 if ((udp_socket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
75 logit(LOG_ERR
, errno
, "UDP socket");
76 logit(LOG_INFO
,0,"Getting vifs from kernel interfaces");
77 config_vifs_from_kernel();
78 logit(LOG_INFO
,0,"Getting vifs from %s",configfilename
);
79 config_vifs_from_file();
82 * Quit if there are fewer than two enabled vifs.
87 for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; ++vifi
, ++v
) {
88 if (!(v
->uv_flags
& VIFF_DISABLED
)) {
90 if (!(v
->uv_flags
& VIFF_TUNNEL
)) {
98 logit(LOG_ERR
, 0, "can't forward: %s",
99 enabled_vifs
== 0 ? "no enabled vifs" : "only one enabled vif");
101 if (enabled_phyints
== 0)
102 logit(LOG_WARNING
, 0,
103 "no enabled interfaces, forwarding via tunnels only");
105 logit(LOG_INFO
, 0, "Installing vifs in mrouted...");
106 for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; ++vifi
, ++v
) {
107 if (!(v
->uv_flags
& VIFF_DISABLED
)) {
108 if (!(v
->uv_flags
& VIFF_DOWN
)) {
109 if (v
->uv_flags
& VIFF_TUNNEL
)
110 logit(LOG_INFO
, 0, "vif #%d, tunnel %s -> %s", vifi
,
111 inet_fmt(v
->uv_lcl_addr
),
112 inet_fmt(v
->uv_rmt_addr
));
114 logit(LOG_INFO
, 0, "vif #%d, phyint %s", vifi
,
115 inet_fmt(v
->uv_lcl_addr
));
117 } else logit(LOG_INFO
, 0,
118 "%s is not yet up; vif #%u not in service",
125 * Start routing on all virtual interfaces that are not down or
126 * administratively disabled.
129 init_installvifs(void)
134 logit(LOG_INFO
, 0, "Installing vifs in kernel...");
135 for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; ++vifi
, ++v
) {
136 if (!(v
->uv_flags
& VIFF_DISABLED
)) {
137 if (!(v
->uv_flags
& VIFF_DOWN
)) {
138 if (v
->uv_flags
& VIFF_TUNNEL
)
139 logit(LOG_INFO
, 0, "vif #%d, tunnel %s -> %s", vifi
,
140 inet_fmt(v
->uv_lcl_addr
),
141 inet_fmt(v
->uv_rmt_addr
));
143 logit(LOG_INFO
, 0, "vif #%d, phyint %s", vifi
,
144 inet_fmt(v
->uv_lcl_addr
));
145 k_add_vif(vifi
, &uvifs
[vifi
]);
146 } else logit(LOG_INFO
, 0,
147 "%s is not yet up; vif #%u not in service",
154 * See if any interfaces have changed from up state to down, or vice versa,
155 * including any non-multicast-capable interfaces that are in use as local
156 * tunnel end-points. Ignore interfaces that have been administratively
160 check_vif_state(void)
167 for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; ++vifi
, ++v
) {
169 if (v
->uv_flags
& VIFF_DISABLED
) continue;
171 strncpy(ifr
.ifr_name
, v
->uv_name
, IFNAMSIZ
);
172 if (ioctl(udp_socket
, SIOCGIFFLAGS
, (char *)&ifr
) < 0)
173 logit(LOG_ERR
, errno
,
174 "ioctl SIOCGIFFLAGS for %s", ifr
.ifr_name
);
176 if (v
->uv_flags
& VIFF_DOWN
) {
177 if (ifr
.ifr_flags
& IFF_UP
) {
178 v
->uv_flags
&= ~VIFF_DOWN
;
181 "%s has come up; vif #%u now in service",
184 else vifs_down
= TRUE
;
187 if (!(ifr
.ifr_flags
& IFF_UP
)) {
189 v
->uv_flags
|= VIFF_DOWN
;
191 "%s has gone down; vif #%u taken out of service",
200 * Send a probe message on vif v
203 send_probe_on_vif(struct uvif
*v
)
207 struct listaddr
*nbr
;
210 p
= send_buf
+ MIN_IP_HEADER_LEN
+ IGMP_MINLEN
;
212 for (i
= 0; i
< 4; i
++)
213 *p
++ = ((char *)&(dvmrp_genid
))[i
];
217 * add the neighbor list on the interface to the message
219 nbr
= v
->uv_neighbors
;
222 for (i
= 0; i
< 4; i
++)
223 *p
++ = ((char *)&nbr
->al_addr
)[i
];
228 send_igmp(v
->uv_lcl_addr
,
229 (v
->uv_flags
& VIFF_TUNNEL
) ? v
->uv_rmt_addr
231 IGMP_DVMRP
, DVMRP_PROBE
,
232 htonl(MROUTED_LEVEL
|
233 ((v
->uv_flags
& VIFF_LEAF
) ? 0 : LEAF_FLAGS
)),
238 * Add a vifi to the kernel and start routing on it.
241 start_vif(vifi_t vifi
)
244 * Install the interface in the kernel's vif structure.
246 k_add_vif(vifi
, &uvifs
[vifi
]);
252 * Add a vifi to all the user-level data structures but don't add
253 * it to the kernel yet.
256 start_vif2(vifi_t vifi
)
263 src
= v
->uv_lcl_addr
;
266 * Update the existing route entries to take into account the new vif.
268 add_vif_to_routes(vifi
);
270 if (!(v
->uv_flags
& VIFF_TUNNEL
)) {
272 * Join the DVMRP multicast group on the interface.
273 * (This is not strictly necessary, since the kernel promiscuously
274 * receives IGMP packets addressed to ANY IP multicast group while
275 * multicast routing is enabled. However, joining the group allows
276 * this host to receive non-IGMP packets as well, such as 'pings'.)
278 k_join(dvmrp_group
, src
);
281 * Join the ALL-ROUTERS multicast group on the interface.
282 * This allows mtrace requests to loop back if they are run
283 * on the multicast router.
285 k_join(allrtrs_group
, src
);
288 * Install an entry in the routing table for the subnet to which
289 * the interface is connected.
291 start_route_updates();
292 update_route(v
->uv_subnet
, v
->uv_subnetmask
, 0, 0, vifi
);
293 for (p
= v
->uv_addrs
; p
; p
= p
->pa_next
) {
294 start_route_updates();
295 update_route(p
->pa_subnet
, p
->pa_subnetmask
, 0, 0, vifi
);
299 * Until neighbors are discovered, assume responsibility for sending
300 * periodic group membership queries to the subnet. Send the first
303 v
->uv_flags
|= VIFF_QUERIER
;
304 send_igmp(src
, allhosts_group
, IGMP_HOST_MEMBERSHIP_QUERY
,
305 (v
->uv_flags
& VIFF_IGMPV1
) ? 0 :
306 IGMP_MAX_HOST_REPORT_DELAY
* IGMP_TIMER_SCALE
, 0, 0);
310 v
->uv_leaf_timer
= LEAF_CONFIRMATION_TIME
;
313 * Send a probe via the new vif to look for neighbors.
315 send_probe_on_vif(v
);
319 * Stop routing on the specified virtual interface.
322 stop_vif(vifi_t vifi
)
330 if (!(v
->uv_flags
& VIFF_TUNNEL
)) {
332 * Depart from the DVMRP multicast group on the interface.
334 k_leave(dvmrp_group
, v
->uv_lcl_addr
);
337 * Depart from the ALL-ROUTERS multicast group on the interface.
339 k_leave(allrtrs_group
, v
->uv_lcl_addr
);
342 * Update the entry in the routing table for the subnet to which
343 * the interface is connected, to take into account the interface
346 start_route_updates();
347 update_route(v
->uv_subnet
, v
->uv_subnetmask
, UNREACHABLE
, 0, vifi
);
348 for (p
= v
->uv_addrs
; p
; p
= p
->pa_next
) {
349 start_route_updates();
350 update_route(p
->pa_subnet
, p
->pa_subnetmask
, UNREACHABLE
, 0, vifi
);
354 * Discard all group addresses. (No need to tell kernel;
355 * the k_del_vif() call, below, will clean up kernel state.)
357 while (v
->uv_groups
!= NULL
) {
359 v
->uv_groups
= a
->al_next
;
363 v
->uv_flags
&= ~VIFF_QUERIER
;
367 * Update the existing route entries to take into account the vif failure.
369 delete_vif_from_routes(vifi
);
372 * Delete the interface from the kernel's vif structure.
377 * Discard all neighbor addresses.
380 vifs_with_neighbors
--;
382 while (v
->uv_neighbors
!= NULL
) {
384 v
->uv_neighbors
= a
->al_next
;
391 * stop routing on all vifs
401 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
403 while (v
->uv_groups
!= NULL
) {
405 v
->uv_groups
= a
->al_next
;
408 while (v
->uv_neighbors
!= NULL
) {
410 v
->uv_neighbors
= a
->al_next
;
413 while (v
->uv_acl
!= NULL
) {
415 v
->uv_acl
= acl
->acl_next
;
423 * Find the virtual interface from which an incoming packet arrived,
424 * based on the packet's source and destination IP addresses.
427 find_vif(u_int32_t src
, u_int32_t dst
)
433 for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; ++vifi
, ++v
) {
434 if (!(v
->uv_flags
& (VIFF_DOWN
|VIFF_DISABLED
))) {
435 if (v
->uv_flags
& VIFF_TUNNEL
) {
436 if (src
== v
->uv_rmt_addr
&& dst
== v
->uv_lcl_addr
)
440 if ((src
& v
->uv_subnetmask
) == v
->uv_subnet
&&
441 ((v
->uv_subnetmask
== 0xffffffff) ||
442 (src
!= v
->uv_subnetbcast
)))
444 for (p
=v
->uv_addrs
; p
; p
=p
->pa_next
) {
445 if ((src
& p
->pa_subnetmask
) == p
->pa_subnet
&&
446 ((p
->pa_subnetmask
== 0xffffffff) ||
447 (src
!= p
->pa_subnetbcast
)))
464 * Decrement the old-hosts-present timer for each
465 * active group on each vif.
467 for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; vifi
++, v
++)
468 for (g
= v
->uv_groups
; g
!= NULL
; g
= g
->al_next
)
475 * Send group membership queries to all subnets for which I am querier.
483 for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; vifi
++, v
++) {
484 if (v
->uv_flags
& VIFF_QUERIER
) {
485 send_igmp(v
->uv_lcl_addr
, allhosts_group
,
486 IGMP_HOST_MEMBERSHIP_QUERY
,
487 (v
->uv_flags
& VIFF_IGMPV1
) ? 0 :
488 IGMP_MAX_HOST_REPORT_DELAY
* IGMP_TIMER_SCALE
, 0, 0);
495 * Process an incoming host membership query
498 accept_membership_query(u_int32_t src
, u_int32_t dst
, u_int32_t group
, int tmo
)
503 if ((vifi
= find_vif(src
, dst
)) == NO_VIF
||
504 (uvifs
[vifi
].uv_flags
& VIFF_TUNNEL
)) {
506 "ignoring group membership query from non-adjacent host %s",
514 * If we consider ourselves the querier for this vif, but hear a
515 * query from a router with a lower IP address, yield to them.
517 * This is done here as well as in the neighbor discovery in case
518 * there is a querier that doesn't speak DVMRP.
520 * XXX If this neighbor doesn't speak DVMRP, then we need to create
521 * some neighbor state for him so that we can time him out!
523 if ((v
->uv_flags
& VIFF_QUERIER
) &&
524 (ntohl(src
) < ntohl(v
->uv_lcl_addr
))) {
525 v
->uv_flags
&= ~VIFF_QUERIER
;
531 * Process an incoming group membership report.
534 accept_group_report(u_int32_t src
, u_int32_t dst
, u_int32_t group
, int r_type
)
540 if ((vifi
= find_vif(src
, dst
)) == NO_VIF
||
541 (uvifs
[vifi
].uv_flags
& VIFF_TUNNEL
)) {
543 "ignoring group membership report from non-adjacent host %s",
551 * Look for the group in our group list; if found, reset its timer.
553 for (g
= v
->uv_groups
; g
!= NULL
; g
= g
->al_next
) {
554 if (group
== g
->al_addr
) {
555 if (r_type
== IGMP_v1_HOST_MEMBERSHIP_REPORT
)
556 g
->al_old
= OLD_AGE_THRESHOLD
;
561 /** delete old timers, set a timer for expiration **/
562 g
->al_timer
= GROUP_EXPIRE_TIME
;
564 g
->al_query
= DeleteTimer(g
->al_query
);
566 g
->al_timerid
= DeleteTimer(g
->al_timerid
);
567 g
->al_timerid
= SetTimer(vifi
, g
);
573 * If not found, add it to the list and update kernel cache.
576 g
= (struct listaddr
*)malloc(sizeof(struct listaddr
));
578 logit(LOG_ERR
, 0, "ran out of memory"); /* fatal */
583 if (r_type
== IGMP_v2_HOST_MEMBERSHIP_REPORT
)
586 g
->al_old
= OLD_AGE_THRESHOLD
;
591 /** set a timer for expiration **/
593 g
->al_timer
= GROUP_EXPIRE_TIME
;
595 g
->al_timerid
= SetTimer(vifi
, g
);
596 g
->al_next
= v
->uv_groups
;
599 update_lclgrp(vifi
, group
);
603 * Check if a graft is necessary for this group
605 chkgrp_graft(vifi
, group
);
610 accept_leave_message(u_int32_t src
, u_int32_t dst
, u_int32_t group
)
616 if ((vifi
= find_vif(src
, dst
)) == NO_VIF
||
617 (uvifs
[vifi
].uv_flags
& VIFF_TUNNEL
)) {
619 "ignoring group leave report from non-adjacent host %s",
626 if (!(v
->uv_flags
& VIFF_QUERIER
) || (v
->uv_flags
& VIFF_IGMPV1
))
630 * Look for the group in our group list in order to set up a short-timeout
633 for (g
= v
->uv_groups
; g
!= NULL
; g
= g
->al_next
) {
634 if (group
== g
->al_addr
) {
636 "[vif.c, _accept_leave_message] %d %ld\n",
637 g
->al_old
, g
->al_query
);
639 /* Ignore the leave message if there are old hosts present */
643 /* still waiting for a reply to a query, ignore the leave */
647 /** delete old timer set a timer for expiration **/
649 g
->al_timerid
= DeleteTimer(g
->al_timerid
);
651 /** send a group specific querry **/
652 g
->al_timer
= LEAVE_EXPIRE_TIME
;
653 send_igmp(v
->uv_lcl_addr
, g
->al_addr
,
654 IGMP_HOST_MEMBERSHIP_QUERY
,
655 LEAVE_EXPIRE_TIME
/ 3 * IGMP_TIMER_SCALE
,
657 g
->al_query
= SetQueryTimer(g
, vifi
, g
->al_timer
/ 3,
658 LEAVE_EXPIRE_TIME
/ 3 * IGMP_TIMER_SCALE
);
659 g
->al_timerid
= SetTimer(vifi
, g
);
667 * Send a periodic probe on all vifs.
668 * Useful to determine one-way interfaces.
669 * Detect neighbor loss faster.
672 probe_for_neighbors(void)
677 for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; vifi
++, v
++) {
678 if (!(v
->uv_flags
& (VIFF_DOWN
|VIFF_DISABLED
))) {
679 send_probe_on_vif(v
);
686 * Send a list of all of our neighbors to the requestor, `src'.
689 accept_neighbor_request(u_int32_t src
, u_int32_t dst
)
696 u_int32_t temp_addr
, us
, them
= src
;
698 /* Determine which of our addresses to use as the source of our response
701 if (IN_MULTICAST(ntohl(dst
))) { /* query sent to a multicast group */
702 int udp
; /* find best interface to reply on */
703 struct sockaddr_in addr
;
704 socklen_t addrlen
= sizeof(addr
);
706 memset(&addr
, 0, sizeof(addr
));
707 addr
.sin_family
= AF_INET
;
708 #if (defined(BSD) && (BSD >= 199103))
709 addr
.sin_len
= sizeof addr
;
711 addr
.sin_addr
.s_addr
= dst
;
712 addr
.sin_port
= htons(2000); /* any port over 1024 will do... */
713 if ((udp
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0
714 || connect(udp
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0
715 || getsockname(udp
, (struct sockaddr
*) &addr
, &addrlen
) < 0) {
716 logit(LOG_WARNING
, errno
, "Determining local address");
721 us
= addr
.sin_addr
.s_addr
;
722 } else /* query sent to us alone */
725 #define PUT_ADDR(a) temp_addr = ntohl(a); \
726 *p++ = temp_addr >> 24; \
727 *p++ = (temp_addr >> 16) & 0xFF; \
728 *p++ = (temp_addr >> 8) & 0xFF; \
729 *p++ = temp_addr & 0xFF;
731 p
= (u_char
*) (send_buf
+ MIN_IP_HEADER_LEN
+ IGMP_MINLEN
);
734 for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; vifi
++, v
++) {
735 if (v
->uv_flags
& VIFF_DISABLED
)
740 for (la
= v
->uv_neighbors
; la
; la
= la
->al_next
) {
742 /* Make sure that there's room for this neighbor... */
743 if (datalen
+ (ncount
== 0 ? 4 + 3 + 4 : 4) > MAX_DVMRP_DATA_LEN
) {
744 send_igmp(us
, them
, IGMP_DVMRP
, DVMRP_NEIGHBORS
,
745 htonl(MROUTED_LEVEL
), datalen
);
746 p
= (u_char
*) (send_buf
+ MIN_IP_HEADER_LEN
+ IGMP_MINLEN
);
751 /* Put out the header for this neighbor list... */
753 PUT_ADDR(v
->uv_lcl_addr
);
755 *p
++ = v
->uv_threshold
;
761 PUT_ADDR(la
->al_addr
);
768 send_igmp(us
, them
, IGMP_DVMRP
, DVMRP_NEIGHBORS
, htonl(MROUTED_LEVEL
),
773 * Send a list of all of our neighbors to the requestor, `src'.
776 accept_neighbor_request2(u_int32_t src
, u_int32_t dst
)
783 u_int32_t us
, them
= src
;
785 /* Determine which of our addresses to use as the source of our response
788 if (IN_MULTICAST(ntohl(dst
))) { /* query sent to a multicast group */
789 int udp
; /* find best interface to reply on */
790 struct sockaddr_in addr
;
791 socklen_t addrlen
= sizeof(addr
);
793 memset(&addr
, 0, sizeof(addr
));
794 addr
.sin_family
= AF_INET
;
795 #if (defined(BSD) && (BSD >= 199103))
796 addr
.sin_len
= sizeof addr
;
798 addr
.sin_addr
.s_addr
= dst
;
799 addr
.sin_port
= htons(2000); /* any port over 1024 will do... */
800 if ((udp
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0
801 || connect(udp
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0
802 || getsockname(udp
, (struct sockaddr
*) &addr
, &addrlen
) < 0) {
803 logit(LOG_WARNING
, errno
, "Determining local address");
808 us
= addr
.sin_addr
.s_addr
;
809 } else /* query sent to us alone */
812 p
= (u_char
*) (send_buf
+ MIN_IP_HEADER_LEN
+ IGMP_MINLEN
);
815 for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; vifi
++, v
++) {
816 u_short vflags
= v
->uv_flags
;
818 if (vflags
& VIFF_TUNNEL
)
819 rflags
|= DVMRP_NF_TUNNEL
;
820 if (vflags
& VIFF_SRCRT
)
821 rflags
|= DVMRP_NF_SRCRT
;
822 if (vflags
& VIFF_DOWN
)
823 rflags
|= DVMRP_NF_DOWN
;
824 if (vflags
& VIFF_DISABLED
)
825 rflags
|= DVMRP_NF_DISABLED
;
826 if (vflags
& VIFF_QUERIER
)
827 rflags
|= DVMRP_NF_QUERIER
;
828 if (vflags
& VIFF_LEAF
)
829 rflags
|= DVMRP_NF_LEAF
;
831 la
= v
->uv_neighbors
;
834 * include down & disabled interfaces and interfaces on
837 if (rflags
& DVMRP_NF_TUNNEL
)
838 rflags
|= DVMRP_NF_DOWN
;
839 if (datalen
> MAX_DVMRP_DATA_LEN
- 12) {
840 send_igmp(us
, them
, IGMP_DVMRP
, DVMRP_NEIGHBORS2
,
841 htonl(MROUTED_LEVEL
), datalen
);
842 p
= (u_char
*) (send_buf
+ MIN_IP_HEADER_LEN
+ IGMP_MINLEN
);
845 *(u_int
*)p
= v
->uv_lcl_addr
;
848 *p
++ = v
->uv_threshold
;
851 *(u_int
*)p
= v
->uv_rmt_addr
;
855 for ( ; la
; la
= la
->al_next
) {
856 /* Make sure that there's room for this neighbor... */
857 if (datalen
+ (ncount
== 0 ? 4+4+4 : 4) > MAX_DVMRP_DATA_LEN
) {
858 send_igmp(us
, them
, IGMP_DVMRP
, DVMRP_NEIGHBORS2
,
859 htonl(MROUTED_LEVEL
), datalen
);
860 p
= (u_char
*) (send_buf
+ MIN_IP_HEADER_LEN
+ IGMP_MINLEN
);
864 /* Put out the header for this neighbor list... */
866 *(u_int
*)p
= v
->uv_lcl_addr
;
869 *p
++ = v
->uv_threshold
;
875 *(u_int
*)p
= la
->al_addr
;
883 send_igmp(us
, them
, IGMP_DVMRP
, DVMRP_NEIGHBORS2
, htonl(MROUTED_LEVEL
),
888 accept_info_request(u_int32_t src
, u_int32_t dst
, u_char
*p
, int datalen
)
894 q
= (u_char
*) (send_buf
+ MIN_IP_HEADER_LEN
+ IGMP_MINLEN
);
896 /* To be general, this must deal properly with breaking up over-sized
897 * packets. That implies passing a length to each function, and
898 * allowing each function to request to be called again. Right now,
899 * we're only implementing the one thing we are positive will fit into
900 * a single packet, so we wimp out.
902 while (datalen
> 0) {
905 case DVMRP_INFO_VERSION
:
906 len
= info_version((char *)q
,
907 send_buflen
- MIN_IP_HEADER_LEN
- IGMP_MINLEN
);
910 case DVMRP_INFO_NEIGHBORS
:
912 logit(LOG_INFO
, 0, "ignoring unknown info type %d", *p
);
918 len
= (*(p
+1) + 1) * 4;
924 send_igmp(INADDR_ANY
, src
, IGMP_DVMRP
, DVMRP_INFO_REPLY
,
925 htonl(MROUTED_LEVEL
), outlen
);
929 * Information response -- return version string
932 info_version(char *p
, size_t l
)
935 extern char versionstring
[];
937 if (l
< 4 + strlen(versionstring
) + 1)
939 p
[0] = DVMRP_INFO_VERSION
;
940 /* skip over length */
941 p
[2] = 0; /* zero out */
942 p
[3] = 0; /* reserved fields */
943 strlcpy(p
+ 4, versionstring
, l
- 4);
945 len
= strlen(versionstring
);
946 return ((len
+ 3) / 4);
950 * Process an incoming neighbor-list message.
953 accept_neighbors(u_int32_t src
, u_int32_t dst
, u_char
*p
, int datalen
,
956 logit(LOG_INFO
, 0, "ignoring spurious DVMRP neighbor list from %s to %s",
957 inet_fmt(src
), inet_fmt(dst
));
962 * Process an incoming neighbor-list message.
965 accept_neighbors2(u_int32_t src
, u_int32_t dst
, u_char
*p
, int datalen
,
968 logit(LOG_INFO
, 0, "ignoring spurious DVMRP neighbor list2 from %s to %s",
969 inet_fmt(src
), inet_fmt(dst
));
973 * Process an incoming info reply message.
976 accept_info_reply(u_int32_t src
, u_int32_t dst
, u_char
*p
, int datalen
)
978 logit(LOG_INFO
, 0, "ignoring spurious DVMRP info reply from %s to %s",
979 inet_fmt(src
), inet_fmt(dst
));
984 * Update the neighbor entry for neighbor 'addr' on vif 'vifi'.
985 * 'msgtype' is the type of DVMRP message received from the neighbor.
986 * Return TRUE if 'addr' is a valid neighbor, FALSE otherwise.
989 update_neighbor(vifi_t vifi
, u_int32_t addr
, int msgtype
, char *p
, int datalen
, u_int32_t level
)
995 u_int32_t send_tables
= 0;
996 int do_reset
= FALSE
;
1000 nflags
= (level
>> 16) & 0xff;
1003 * Confirm that 'addr' is a valid neighbor address on vif 'vifi'.
1004 * IT IS ASSUMED that this was preceded by a call to find_vif(), which
1005 * checks that 'addr' is either a valid remote tunnel endpoint or a
1006 * non-broadcast address belonging to a directly-connected subnet.
1007 * Therefore, here we check only that 'addr' is not our own address
1008 * (due to an impostor or erroneous loopback) or an address of the form
1009 * {subnet,0} ("the unknown host"). These checks are not performed in
1010 * find_vif() because those types of address are acceptable for some
1011 * types of IGMP message (such as group membership reports).
1013 if (!(v
->uv_flags
& VIFF_TUNNEL
) &&
1014 (addr
== v
->uv_lcl_addr
||
1015 addr
== v
->uv_subnet
)) {
1016 logit(LOG_WARNING
, 0,
1017 "received DVMRP message from 'the unknown host' or self: %s",
1023 * Look for addr in list of neighbors.
1025 for (n
= v
->uv_neighbors
; n
!= NULL
; n
= n
->al_next
) {
1026 if (addr
== n
->al_addr
) {
1032 * Found it. Reset its timer, and check for a version change
1038 * update the neighbors version and protocol number
1039 * if changed => router went down and came up,
1040 * so take action immediately.
1042 if ((n
->al_pv
!= (level
& 0xff)) ||
1043 (n
->al_mv
!= ((level
>> 8) & 0xff))) {
1047 "version change neighbor %s [old:%d.%d, new:%d.%d]",
1049 n
->al_pv
, n
->al_mv
, level
&0xff, (level
>> 8) & 0xff);
1051 n
->al_pv
= level
& 0xff;
1052 n
->al_mv
= (level
>> 8) & 0xff;
1056 * If not found, add it to the list. If the neighbor has a lower
1057 * IP address than me, yield querier duties to it.
1059 logit(LOG_DEBUG
, 0, "New neighbor %s on vif %d v%d.%d nf 0x%02x",
1060 inet_fmt(addr
), vifi
,
1061 level
& 0xff, (level
>> 8) & 0xff, (level
>> 16) & 0xff);
1063 n
= (struct listaddr
*)malloc(sizeof(struct listaddr
));
1065 logit(LOG_ERR
, 0, "ran out of memory"); /* fatal */
1070 n
->al_pv
= level
& 0xff;
1071 n
->al_mv
= (level
>> 8) & 0xff;
1076 n
->al_next
= v
->uv_neighbors
;
1079 * If we thought that we had no neighbors on this vif, send a route
1080 * report to the vif. If this is just a new neighbor on the same
1081 * vif, send the route report just to the new neighbor.
1083 if (v
->uv_neighbors
== NULL
) {
1084 send_tables
= (v
->uv_flags
& VIFF_TUNNEL
) ? addr
: dvmrp_group
;
1085 vifs_with_neighbors
++;
1090 v
->uv_neighbors
= n
;
1092 if (!(v
->uv_flags
& VIFF_TUNNEL
) &&
1093 ntohl(addr
) < ntohl(v
->uv_lcl_addr
))
1094 v
->uv_flags
&= ~VIFF_QUERIER
;
1098 * Check if the router gen-ids are the same.
1099 * Need to reset the prune state of the router if not.
1100 * Also check for one-way interfaces by seeing if we are in our
1101 * neighbor's list of known routers.
1103 if (msgtype
== DVMRP_PROBE
) {
1105 /* Check genid neighbor flag. Also check version number; 3.3 and
1106 * 3.4 didn't set this flag. */
1107 if ((((level
>> 16) & 0xff) & NF_GENID
) ||
1108 (((level
& 0xff) == 3) && (((level
>> 8) & 0xff) > 2))) {
1113 logit(LOG_WARNING
, 0,
1114 "received truncated probe message from %s (len %d)",
1115 inet_fmt(addr
), datalen
);
1119 for (i
= 0; i
< 4; i
++)
1120 ((char *)&genid
)[i
] = *p
++;
1123 if (n
->al_genid
== 0)
1124 n
->al_genid
= genid
;
1125 else if (n
->al_genid
!= genid
) {
1127 "new genid neigbor %s on vif %d [old:%x, new:%x]",
1128 inet_fmt(addr
), vifi
, n
->al_genid
, genid
);
1130 n
->al_genid
= genid
;
1135 * loop through router list and check for one-way ifs.
1138 v
->uv_flags
|= VIFF_ONEWAY
;
1140 while (datalen
> 0) {
1142 logit(LOG_WARNING
, 0,
1143 "received truncated probe message from %s (len %d)",
1144 inet_fmt(addr
), datalen
);
1147 for (i
= 0; i
< 4; i
++)
1148 ((char *)&router
)[i
] = *p
++;
1150 if (router
== v
->uv_lcl_addr
) {
1151 v
->uv_flags
&= ~VIFF_ONEWAY
;
1157 if (n
->al_flags
!= nflags
) {
1158 n
->al_flags
= nflags
;
1160 if (n
->al_flags
& NF_LEAF
) {
1161 /*XXX If we have non-leaf neighbors then we know we shouldn't
1162 * mark this vif as a leaf. For now we just count on other
1163 * probes and/or reports resetting the timer. */
1164 if (!v
->uv_leaf_timer
)
1165 v
->uv_leaf_timer
= LEAF_CONFIRMATION_TIME
;
1167 /* If we get a leaf to non-leaf transition, we *must* update
1168 * the routing table. */
1169 if (v
->uv_flags
& VIFF_LEAF
&& send_tables
== 0)
1171 v
->uv_flags
&= ~VIFF_LEAF
;
1172 v
->uv_leaf_timer
= 0;
1176 reset_neighbor_state(vifi
, addr
);
1181 report(ALL_ROUTES
, vifi
, send_tables
);
1188 * On every timer interrupt, advance the timer in each neighbor and
1189 * group entry on every vif.
1196 struct listaddr
*a
, *prev_a
, *n
;
1199 for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; ++vifi
, ++v
) {
1200 if (v
->uv_leaf_timer
&& (v
->uv_leaf_timer
-= TIMER_INTERVAL
== 0)) {
1201 v
->uv_flags
|= VIFF_LEAF
;
1204 for (prev_a
= a
= v
->uv_neighbors
; a
!= NULL
;) {
1206 if ((a
->al_timer
+= TIMER_INTERVAL
) < NEIGHBOR_EXPIRE_TIME
)
1210 * Neighbor has expired; delete it from the neighbor list,
1211 * delete it from the 'dominants' and 'subordinates arrays of
1212 * any route entries and assume querier duties unless there is
1213 * another neighbor with a lower IP address than mine.
1216 if (a
== v
->uv_neighbors
) {
1217 v
->uv_neighbors
= a
->al_next
;
1219 prev_a
= a
= v
->uv_neighbors
;
1221 prev_a
->al_next
= a
->al_next
;
1223 prev_a
= a
= prev_a
->al_next
;
1226 delete_neighbor_from_routes(addr
, vifi
);
1228 if (v
->uv_neighbors
== NULL
)
1229 vifs_with_neighbors
--;
1231 v
->uv_leaf_timer
= LEAF_CONFIRMATION_TIME
;
1233 if (!(v
->uv_flags
& VIFF_TUNNEL
)) {
1234 v
->uv_flags
|= VIFF_QUERIER
;
1235 for (n
= v
->uv_neighbors
; n
!= NULL
; n
= n
->al_next
) {
1236 if (ntohl(n
->al_addr
) < ntohl(v
->uv_lcl_addr
)) {
1237 v
->uv_flags
&= ~VIFF_QUERIER
;
1239 if (!(n
->al_flags
& NF_LEAF
)) {
1240 v
->uv_leaf_timer
= 0;
1249 * Returns the neighbor info struct for a given neighbor
1252 neighbor_info(vifi_t vifi
, u_int32_t addr
)
1256 for (u
= uvifs
[vifi
].uv_neighbors
; u
; u
= u
->al_next
)
1257 if (u
->al_addr
== addr
)
1264 * Print the contents of the uvifs array on file 'fp'.
1273 struct sioc_vif_req v_req
;
1275 fprintf(fp
, "vifs_with_neighbors = %d\n", vifs_with_neighbors
);
1277 if (vifs_with_neighbors
== 1)
1278 fprintf(fp
,"[This host is a leaf]\n\n");
1281 "\nVirtual Interface Table\n%s",
1282 "Vif Name Local-Address ");
1284 "M Thr Rate Flags\n");
1286 for (vifi
= 0, v
= uvifs
; vifi
< numvifs
; vifi
++, v
++) {
1288 fprintf(fp
, "%2u %6s %-15s %6s: %-18s %2u %3u %5u ",
1291 inet_fmt(v
->uv_lcl_addr
),
1292 (v
->uv_flags
& VIFF_TUNNEL
) ?
1295 (v
->uv_flags
& VIFF_TUNNEL
) ?
1296 inet_fmt(v
->uv_rmt_addr
) :
1297 inet_fmts(v
->uv_subnet
, v
->uv_subnetmask
),
1302 if (v
->uv_flags
& VIFF_ONEWAY
) fprintf(fp
, " one-way");
1303 if (v
->uv_flags
& VIFF_DOWN
) fprintf(fp
, " down");
1304 if (v
->uv_flags
& VIFF_DISABLED
) fprintf(fp
, " disabled");
1305 if (v
->uv_flags
& VIFF_QUERIER
) fprintf(fp
, " querier");
1306 if (v
->uv_flags
& VIFF_SRCRT
) fprintf(fp
, " src-rt");
1307 if (v
->uv_flags
& VIFF_LEAF
) fprintf(fp
, " leaf");
1308 if (v
->uv_flags
& VIFF_IGMPV1
) fprintf(fp
, " IGMPv1");
1311 if (v
->uv_addrs
!= NULL
) {
1312 fprintf(fp
, " alternate subnets: %s\n",
1313 inet_fmts(v
->uv_addrs
->pa_subnet
,
1314 v
->uv_addrs
->pa_subnetmask
));
1315 for (p
= v
->uv_addrs
->pa_next
; p
; p
= p
->pa_next
) {
1316 fprintf(fp
, " %s\n",
1317 inet_fmts(p
->pa_subnet
, p
->pa_subnetmask
));
1321 if (v
->uv_neighbors
!= NULL
) {
1322 fprintf(fp
, " peers: %s (%d.%d) (0x%x)\n",
1323 inet_fmt(v
->uv_neighbors
->al_addr
),
1324 v
->uv_neighbors
->al_pv
, v
->uv_neighbors
->al_mv
,
1325 v
->uv_neighbors
->al_flags
);
1326 for (a
= v
->uv_neighbors
->al_next
; a
!= NULL
; a
= a
->al_next
) {
1327 fprintf(fp
, " %s (%d.%d) (0x%x)\n",
1328 inet_fmt(a
->al_addr
), a
->al_pv
,
1329 a
->al_mv
, a
->al_flags
);
1333 if (v
->uv_groups
!= NULL
) {
1334 fprintf(fp
, " groups: %-15s\n",
1335 inet_fmt(v
->uv_groups
->al_addr
));
1336 for (a
= v
->uv_groups
->al_next
; a
!= NULL
; a
= a
->al_next
) {
1337 fprintf(fp
, " %-15s\n",
1338 inet_fmt(a
->al_addr
));
1341 if (v
->uv_acl
!= NULL
) {
1342 struct vif_acl
*acl
;
1344 fprintf(fp
, " boundaries: %-18s\n",
1345 inet_fmts(v
->uv_acl
->acl_addr
, v
->uv_acl
->acl_mask
));
1346 for (acl
= v
->uv_acl
->acl_next
; acl
!= NULL
; acl
= acl
->acl_next
) {
1347 fprintf(fp
, " : %-18s\n",
1348 inet_fmts(acl
->acl_addr
, acl
->acl_mask
));
1352 if (ioctl(igmp_socket
, SIOCGETVIFCNT
, (char *)&v_req
) < 0) {
1353 logit(LOG_WARNING
, 0,
1354 "SIOCGETVIFCNT fails");
1357 fprintf(fp
, " pkts in : %ld\n",
1359 fprintf(fp
, " pkts out: %ld\n",
1368 * Time out record of a group membership on a vif
1373 cbk_t
*cbk
= (cbk_t
*)arg
;
1374 vifi_t vifi
= cbk
->vifi
;
1375 struct uvif
*v
= &uvifs
[vifi
];
1376 struct listaddr
*a
, **anp
, *g
= cbk
->g
;
1380 * delete all kernel cache entries with this group
1383 DeleteTimer(g
->al_query
);
1385 delete_lclgrp(vifi
, g
->al_addr
);
1387 anp
= &(v
->uv_groups
);
1388 while ((a
= *anp
) != NULL
) {
1401 * Set a timer to delete the record of a group membership on a vif.
1404 SetTimer(vifi_t vifi
, struct listaddr
*g
)
1408 cbk
= (cbk_t
*) malloc(sizeof(cbk_t
));
1411 return timer_setTimer(g
->al_timer
, (cfunc_t
)DelVif
, (void *)cbk
);
1415 * Delete a timer that was set above.
1420 timer_clearTimer(id
);
1425 * Send a group-specific query.
1428 SendQuery(void *arg
)
1430 cbk_t
*cbk
= (cbk_t
*)arg
;
1431 struct uvif
*v
= &uvifs
[cbk
->vifi
];
1433 send_igmp(v
->uv_lcl_addr
, cbk
->g
->al_addr
,
1434 IGMP_HOST_MEMBERSHIP_QUERY
,
1435 cbk
->q_time
, cbk
->g
->al_addr
, 0);
1436 cbk
->g
->al_query
= 0;
1441 * Set a timer to send a group-specific query.
1444 SetQueryTimer(struct listaddr
*g
, vifi_t vifi
, int to_expire
, int q_time
)
1448 cbk
= (cbk_t
*) malloc(sizeof(cbk_t
));
1450 cbk
->q_time
= q_time
;
1452 return timer_setTimer(to_expire
, (cfunc_t
)SendQuery
, (void *)cbk
);