More copy{in,out} removal for ctloutput routines
[dragonfly.git] / usr.sbin / mrouted / vif.c
blobc31463c1345bef23e66ab8ad7e2c4c69c2db1081
1 /*
2 * The mrouted program is covered by the license in the accompanying file
3 * named "LICENSE". Use of the mrouted program represents acceptance of
4 * the terms and conditions listed in that file.
6 * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
7 * Leland Stanford Junior University.
10 * vif.c,v 3.8.4.56.2.1 1999/01/20 05:18:50 fenner Exp
12 * $FreeBSD: src/usr.sbin/mrouted/vif.c,v 1.15 1999/08/28 01:17:09 peter Exp $
13 * $DragonFly: src/usr.sbin/mrouted/vif.c,v 1.4 2004/03/15 18:10:28 dillon Exp $
16 #include "defs.h"
17 #include <fcntl.h>
20 * Exported variables.
22 struct uvif uvifs[MAXVIFS]; /* array of virtual interfaces */
23 vifi_t numvifs; /* number of vifs in use */
24 int vifs_down; /* 1=>some interfaces are down */
25 int phys_vif; /* An enabled vif */
26 int udp_socket; /* Since the honkin' kernel doesn't support */
27 /* ioctls on raw IP sockets, we need a UDP */
28 /* socket as well as our IGMP (raw) socket. */
29 /* How dumb. */
30 int vifs_with_neighbors; /* == 1 if I am a leaf */
33 * Private variables.
35 struct listaddr *nbrs[MAXNBRS]; /* array of neighbors */
37 typedef struct {
38 vifi_t vifi;
39 struct listaddr *g;
40 int q_time;
41 } cbk_t;
44 * Forward declarations.
46 static void start_vif(vifi_t vifi);
47 static void start_vif2(vifi_t vifi);
48 static void stop_vif(vifi_t vifi);
49 static void age_old_hosts(void);
50 static void send_probe_on_vif(struct uvif *v);
51 static void send_query(struct uvif *v);
52 static int info_version(char *p, int plen);
53 static void DelVif(void *arg);
54 static int SetTimer(vifi_t vifi, struct listaddr *g);
55 static int DeleteTimer(int id);
56 static void SendQuery(void *arg);
57 static int SetQueryTimer(struct listaddr *g, vifi_t vifi, int to_expire,
58 int q_time);
62 * Initialize the virtual interfaces, but do not install
63 * them in the kernel. Start routing on all vifs that are
64 * not down or disabled.
66 void
67 init_vifs(void)
69 vifi_t vifi;
70 struct uvif *v;
71 int enabled_vifs, enabled_phyints;
72 extern char *configfilename;
74 numvifs = 0;
75 vifs_with_neighbors = 0;
76 vifs_down = FALSE;
79 * Configure the vifs based on the interface configuration of the
80 * the kernel and the contents of the configuration file.
81 * (Open a UDP socket for ioctl use in the config procedures if
82 * the kernel can't handle IOCTL's on the IGMP socket.)
84 #ifdef IOCTL_OK_ON_RAW_SOCKET
85 udp_socket = igmp_socket;
86 #else
87 if ((udp_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
88 log(LOG_ERR, errno, "UDP socket");
89 #endif
90 log(LOG_INFO,0,"Getting vifs from kernel interfaces");
91 config_vifs_from_kernel();
92 log(LOG_INFO,0,"Getting vifs from %s",configfilename);
93 config_vifs_from_file();
96 * Quit if there are fewer than two enabled vifs.
98 enabled_vifs = 0;
99 enabled_phyints = 0;
100 phys_vif = -1;
101 for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
102 if (!(v->uv_flags & VIFF_DISABLED)) {
103 ++enabled_vifs;
104 if (!(v->uv_flags & VIFF_TUNNEL)) {
105 if (phys_vif == -1)
106 phys_vif = vifi;
107 ++enabled_phyints;
111 if (enabled_vifs < 2)
112 log(LOG_ERR, 0, "can't forward: %s",
113 enabled_vifs == 0 ? "no enabled vifs" : "only one enabled vif");
115 if (enabled_phyints == 0)
116 log(LOG_WARNING, 0,
117 "no enabled interfaces, forwarding via tunnels only");
119 log(LOG_INFO, 0, "Installing vifs in mrouted...");
120 for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
121 if (!(v->uv_flags & VIFF_DISABLED)) {
122 if (!(v->uv_flags & VIFF_DOWN)) {
123 if (v->uv_flags & VIFF_TUNNEL)
124 log(LOG_INFO, 0, "vif #%d, tunnel %s -> %s", vifi,
125 inet_fmt(v->uv_lcl_addr, s1),
126 inet_fmt(v->uv_rmt_addr, s2));
127 else
128 log(LOG_INFO, 0, "vif #%d, phyint %s", vifi,
129 inet_fmt(v->uv_lcl_addr, s1));
130 start_vif2(vifi);
131 } else log(LOG_INFO, 0,
132 "%s is not yet up; vif #%u not in service",
133 v->uv_name, vifi);
139 * Initialize the passed vif with all appropriate default values.
140 * "t" is true if a tunnel, or false if a phyint.
142 void
143 zero_vif(struct uvif *v, int t)
146 v->uv_flags = 0;
147 v->uv_metric = DEFAULT_METRIC;
148 v->uv_admetric = 0;
149 v->uv_threshold = DEFAULT_THRESHOLD;
150 v->uv_rate_limit = t ? DEFAULT_TUN_RATE_LIMIT : DEFAULT_PHY_RATE_LIMIT;
151 v->uv_lcl_addr = 0;
152 v->uv_rmt_addr = 0;
153 v->uv_dst_addr = t ? 0 : dvmrp_group;
154 v->uv_subnet = 0;
155 v->uv_subnetmask = 0;
156 v->uv_subnetbcast = 0;
157 v->uv_name[0] = '\0';
158 v->uv_groups = NULL;
159 v->uv_neighbors = NULL;
160 NBRM_CLRALL(v->uv_nbrmap);
161 v->uv_querier = NULL;
162 v->uv_igmpv1_warn = 0;
163 v->uv_prune_lifetime = 0;
164 v->uv_leaf_timer = 0;
165 v->uv_acl = NULL;
166 v->uv_addrs = NULL;
167 v->uv_filter = NULL;
168 v->uv_blasterbuf = NULL;
169 v->uv_blastercur = NULL;
170 v->uv_blasterend = NULL;
171 v->uv_blasterlen = 0;
172 v->uv_blastertimer = 0;
173 v->uv_nbrup = 0;
174 v->uv_icmp_warn = 0;
175 v->uv_nroutes = 0;
179 * Start routing on all virtual interfaces that are not down or
180 * administratively disabled.
182 void
183 init_installvifs(void)
185 vifi_t vifi;
186 struct uvif *v;
188 log(LOG_INFO, 0, "Installing vifs in kernel...");
189 for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
190 if (!(v->uv_flags & VIFF_DISABLED)) {
191 if (!(v->uv_flags & VIFF_DOWN)) {
192 if (v->uv_flags & VIFF_TUNNEL)
193 log(LOG_INFO, 0, "vif #%d, tunnel %s -> %s", vifi,
194 inet_fmt(v->uv_lcl_addr, s1),
195 inet_fmt(v->uv_rmt_addr, s2));
196 else
197 log(LOG_INFO, 0, "vif #%d, phyint %s", vifi,
198 inet_fmt(v->uv_lcl_addr, s1));
199 k_add_vif(vifi, &uvifs[vifi]);
200 } else log(LOG_INFO, 0,
201 "%s is not yet up; vif #%u not in service",
202 v->uv_name, vifi);
208 * See if any interfaces have changed from up state to down, or vice versa,
209 * including any non-multicast-capable interfaces that are in use as local
210 * tunnel end-points. Ignore interfaces that have been administratively
211 * disabled.
213 void
214 check_vif_state(void)
216 vifi_t vifi;
217 struct uvif *v;
218 struct ifreq ifr;
219 static int checking_vifs = 0;
222 * If we get an error while checking, (e.g. two interfaces go down
223 * at once, and we decide to send a prune out one of the failed ones)
224 * then don't go into an infinite loop!
226 if (checking_vifs)
227 return;
229 vifs_down = FALSE;
230 checking_vifs = 1;
231 for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
233 if (v->uv_flags & VIFF_DISABLED) continue;
235 strncpy(ifr.ifr_name, v->uv_name, IFNAMSIZ);
236 if (ioctl(udp_socket, SIOCGIFFLAGS, (char *)&ifr) < 0)
237 log(LOG_ERR, errno,
238 "ioctl SIOCGIFFLAGS for %s", ifr.ifr_name);
240 if (v->uv_flags & VIFF_DOWN) {
241 if (ifr.ifr_flags & IFF_UP) {
242 log(LOG_NOTICE, 0,
243 "%s has come up; vif #%u now in service",
244 v->uv_name, vifi);
245 v->uv_flags &= ~VIFF_DOWN;
246 start_vif(vifi);
248 else vifs_down = TRUE;
250 else {
251 if (!(ifr.ifr_flags & IFF_UP)) {
252 log(LOG_NOTICE, 0,
253 "%s has gone down; vif #%u taken out of service",
254 v->uv_name, vifi);
255 stop_vif(vifi);
256 v->uv_flags |= VIFF_DOWN;
257 vifs_down = TRUE;
261 checking_vifs = 0;
265 * Send a DVMRP message on the specified vif. If DVMRP messages are
266 * to be encapsulated and sent "inside" the tunnel, use the special
267 * encapsulator. If it's not a tunnel or DVMRP messages are to be
268 * sent "beside" the tunnel, as required by earlier versions of mrouted,
269 * then just send the message.
271 void
272 send_on_vif(struct uvif *v, u_int32 dst, int code, int datalen)
274 u_int32 group = htonl(MROUTED_LEVEL |
275 ((v->uv_flags & VIFF_LEAF) ? 0 : LEAF_FLAGS));
278 * The UNIX kernel will not decapsulate unicasts.
279 * Therefore, we don't send encapsulated unicasts.
281 if ((v->uv_flags & (VIFF_TUNNEL|VIFF_OTUNNEL)) == VIFF_TUNNEL &&
282 ((dst == 0) || IN_MULTICAST(ntohl(dst))))
283 send_ipip(v->uv_lcl_addr, dst ? dst : dvmrp_group, IGMP_DVMRP,
284 code, group, datalen, v);
285 else
286 send_igmp(v->uv_lcl_addr, dst ? dst : v->uv_dst_addr, IGMP_DVMRP,
287 code, group, datalen);
292 * Send a probe message on vif v
294 static void
295 send_probe_on_vif(struct uvif *v)
297 char *p;
298 int datalen = 0;
299 struct listaddr *nbr;
300 int i;
302 if ((v->uv_flags & VIFF_PASSIVE && v->uv_neighbors == NULL) ||
303 (v->uv_flags & VIFF_FORCE_LEAF))
304 return;
306 p = send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN;
308 for (i = 0; i < 4; i++)
309 *p++ = ((char *)&(dvmrp_genid))[i];
310 datalen += 4;
313 * add the neighbor list on the interface to the message
315 nbr = v->uv_neighbors;
317 while (nbr) {
318 for (i = 0; i < 4; i++)
319 *p++ = ((char *)&nbr->al_addr)[i];
320 datalen +=4;
321 nbr = nbr->al_next;
324 send_on_vif(v, 0, DVMRP_PROBE, datalen);
327 static void
328 send_query(struct uvif *v)
330 IF_DEBUG(DEBUG_IGMP)
331 log(LOG_DEBUG, 0, "sending %squery on vif %d",
332 (v->uv_flags & VIFF_IGMPV1) ? "v1 " : "",
333 v - uvifs);
334 send_igmp(v->uv_lcl_addr, allhosts_group,
335 IGMP_MEMBERSHIP_QUERY,
336 (v->uv_flags & VIFF_IGMPV1) ? 0 :
337 IGMP_MAX_HOST_REPORT_DELAY * IGMP_TIMER_SCALE, 0, 0);
341 * Add a vifi to the kernel and start routing on it.
343 static void
344 start_vif(vifi_t vifi)
347 * Install the interface in the kernel's vif structure.
349 k_add_vif(vifi, &uvifs[vifi]);
351 start_vif2(vifi);
355 * Add a vifi to all the user-level data structures but don't add
356 * it to the kernel yet.
358 static void
359 start_vif2(vifi_t vifi)
361 struct uvif *v;
362 u_int32 src;
363 struct phaddr *p;
365 v = &uvifs[vifi];
366 src = v->uv_lcl_addr;
369 * Update the existing route entries to take into account the new vif.
371 add_vif_to_routes(vifi);
373 if (!(v->uv_flags & VIFF_TUNNEL)) {
375 * Join the DVMRP multicast group on the interface.
376 * (This is not strictly necessary, since the kernel promiscuously
377 * receives IGMP packets addressed to ANY IP multicast group while
378 * multicast routing is enabled. However, joining the group allows
379 * this host to receive non-IGMP packets as well, such as 'pings'.)
381 k_join(dvmrp_group, src);
384 * Join the ALL-ROUTERS multicast group on the interface.
385 * This allows mtrace requests to loop back if they are run
386 * on the multicast router.
388 k_join(allrtrs_group, src);
391 * Install an entry in the routing table for the subnet to which
392 * the interface is connected.
394 start_route_updates();
395 update_route(v->uv_subnet, v->uv_subnetmask, 0, 0, vifi, NULL);
396 for (p = v->uv_addrs; p; p = p->pa_next) {
397 start_route_updates();
398 update_route(p->pa_subnet, p->pa_subnetmask, 0, 0, vifi, NULL);
402 * Until neighbors are discovered, assume responsibility for sending
403 * periodic group membership queries to the subnet. Send the first
404 * query.
406 v->uv_flags |= VIFF_QUERIER;
407 IF_DEBUG(DEBUG_IGMP)
408 log(LOG_DEBUG, 0, "assuming querier duties on vif %d", vifi);
409 send_query(v);
412 v->uv_leaf_timer = LEAF_CONFIRMATION_TIME;
415 * Send a probe via the new vif to look for neighbors.
417 send_probe_on_vif(v);
421 * Stop routing on the specified virtual interface.
423 static void
424 stop_vif(vifi_t vifi)
426 struct uvif *v;
427 struct listaddr *a;
428 struct phaddr *p;
430 v = &uvifs[vifi];
432 if (!(v->uv_flags & VIFF_TUNNEL)) {
434 * Depart from the DVMRP multicast group on the interface.
436 k_leave(dvmrp_group, v->uv_lcl_addr);
439 * Depart from the ALL-ROUTERS multicast group on the interface.
441 k_leave(allrtrs_group, v->uv_lcl_addr);
444 * Update the entry in the routing table for the subnet to which
445 * the interface is connected, to take into account the interface
446 * failure.
448 start_route_updates();
449 update_route(v->uv_subnet, v->uv_subnetmask, UNREACHABLE, 0, vifi, NULL);
450 for (p = v->uv_addrs; p; p = p->pa_next) {
451 start_route_updates();
452 update_route(p->pa_subnet, p->pa_subnetmask, UNREACHABLE, 0, vifi, NULL);
456 * Discard all group addresses. (No need to tell kernel;
457 * the k_del_vif() call, below, will clean up kernel state.)
459 while (v->uv_groups != NULL) {
460 a = v->uv_groups;
461 v->uv_groups = a->al_next;
462 free((char *)a);
465 IF_DEBUG(DEBUG_IGMP)
466 log(LOG_DEBUG, 0, "releasing querier duties on vif %d", vifi);
467 v->uv_flags &= ~VIFF_QUERIER;
471 * Update the existing route entries to take into account the vif failure.
473 delete_vif_from_routes(vifi);
476 * Delete the interface from the kernel's vif structure.
478 k_del_vif(vifi);
481 * Discard all neighbor addresses.
483 if (!NBRM_ISEMPTY(v->uv_nbrmap))
484 vifs_with_neighbors--;
486 while (v->uv_neighbors != NULL) {
487 a = v->uv_neighbors;
488 v->uv_neighbors = a->al_next;
489 nbrs[a->al_index] = NULL;
490 free((char *)a);
492 NBRM_CLRALL(v->uv_nbrmap);
497 * stop routing on all vifs
499 void
500 stop_all_vifs(void)
502 vifi_t vifi;
503 struct uvif *v;
504 struct listaddr *a;
505 struct vif_acl *acl;
507 for (vifi = 0; vifi < numvifs; vifi++) {
508 v = &uvifs[vifi];
509 while (v->uv_groups != NULL) {
510 a = v->uv_groups;
511 v->uv_groups = a->al_next;
512 free((char *)a);
514 while (v->uv_neighbors != NULL) {
515 a = v->uv_neighbors;
516 v->uv_neighbors = a->al_next;
517 nbrs[a->al_index] = NULL;
518 free((char *)a);
520 while (v->uv_acl != NULL) {
521 acl = v->uv_acl;
522 v->uv_acl = acl->acl_next;
523 free((char *)acl);
530 * Find the virtual interface from which an incoming packet arrived,
531 * based on the packet's source and destination IP addresses.
533 vifi_t
534 find_vif(u_int32 src, u_int32 dst)
536 vifi_t vifi;
537 struct uvif *v;
538 struct phaddr *p;
540 for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
541 if (!(v->uv_flags & (VIFF_DOWN|VIFF_DISABLED))) {
542 if (v->uv_flags & VIFF_TUNNEL) {
543 if (src == v->uv_rmt_addr && (dst == v->uv_lcl_addr ||
544 dst == dvmrp_group))
545 return(vifi);
547 else {
548 if ((src & v->uv_subnetmask) == v->uv_subnet &&
549 ((v->uv_subnetmask == 0xffffffff) ||
550 (src != v->uv_subnetbcast)))
551 return(vifi);
552 for (p=v->uv_addrs; p; p=p->pa_next) {
553 if ((src & p->pa_subnetmask) == p->pa_subnet &&
554 ((p->pa_subnetmask == 0xffffffff) ||
555 (src != p->pa_subnetbcast)))
556 return(vifi);
561 return (NO_VIF);
564 static void
565 age_old_hosts(void)
567 vifi_t vifi;
568 struct uvif *v;
569 struct listaddr *g;
572 * Decrement the old-hosts-present timer for each
573 * active group on each vif.
575 for (vifi = 0, v = uvifs; vifi < numvifs; vifi++, v++)
576 for (g = v->uv_groups; g != NULL; g = g->al_next)
577 if (g->al_old)
578 g->al_old--;
583 * Send group membership queries on each interface for which I am querier.
584 * Note that technically, there should be a timer per interface, as the
585 * dynamics of querier election can cause the "right" time to send a
586 * query to be different on different interfaces. However, this simple
587 * implementation only ever sends queries sooner than the "right" time,
588 * so can not cause loss of membership (but can send more packets than
589 * necessary)
591 void
592 query_groups(void)
594 vifi_t vifi;
595 struct uvif *v;
597 for (vifi = 0, v = uvifs; vifi < numvifs; vifi++, v++) {
598 if (v->uv_flags & VIFF_QUERIER) {
599 send_query(v);
602 age_old_hosts();
606 * Process an incoming host membership query. Warn about
607 * IGMP version mismatches, perform querier election, and
608 * handle group-specific queries when we're not the querier.
610 void
611 accept_membership_query(u_int32 src, u_int32 dst, u_int32 group, int tmo)
613 vifi_t vifi;
614 struct uvif *v;
616 if ((vifi = find_vif(src, dst)) == NO_VIF ||
617 (uvifs[vifi].uv_flags & VIFF_TUNNEL)) {
618 log(LOG_INFO, 0,
619 "ignoring group membership query from non-adjacent host %s",
620 inet_fmt(src, s1));
621 return;
624 v = &uvifs[vifi];
626 if ((tmo == 0 && !(v->uv_flags & VIFF_IGMPV1)) ||
627 (tmo != 0 && (v->uv_flags & VIFF_IGMPV1))) {
628 int i;
631 * Exponentially back-off warning rate
633 i = ++v->uv_igmpv1_warn;
634 while (i && !(i & 1))
635 i >>= 1;
636 if (i == 1)
637 log(LOG_WARNING, 0, "%s %s on vif %d, %s",
638 tmo == 0 ? "Received IGMPv1 report from"
639 : "Received IGMPv2 report from",
640 inet_fmt(src, s1),
641 vifi,
642 tmo == 0 ? "please configure vif for IGMPv1"
643 : "but I am configured for IGMPv1");
646 if (v->uv_querier == NULL || v->uv_querier->al_addr != src) {
648 * This might be:
649 * - A query from a new querier, with a lower source address
650 * than the current querier (who might be me)
651 * - A query from a new router that just started up and doesn't
652 * know who the querier is.
654 if (ntohl(src) < (v->uv_querier ? ntohl(v->uv_querier->al_addr)
655 : ntohl(v->uv_lcl_addr))) {
656 IF_DEBUG(DEBUG_IGMP)
657 log(LOG_DEBUG, 0, "new querier %s (was %s) on vif %d",
658 inet_fmt(src, s1),
659 v->uv_querier ? inet_fmt(v->uv_querier->al_addr, s2) :
660 "me", vifi);
661 if (!v->uv_querier) {
662 v->uv_querier = (struct listaddr *)
663 malloc(sizeof(struct listaddr));
664 v->uv_flags &= ~VIFF_QUERIER;
666 time(&v->uv_querier->al_ctime);
667 v->uv_querier->al_addr = src;
668 } else {
669 IF_DEBUG(DEBUG_IGMP)
670 log(LOG_DEBUG, 0, "ignoring query from %s; querier on vif %d is still %s",
671 inet_fmt(src, s1), vifi,
672 v->uv_querier ? inet_fmt(v->uv_querier->al_addr, s2) :
673 "me");
675 return;
680 * Reset the timer since we've received a query.
682 if (v->uv_querier && src == v->uv_querier->al_addr)
683 v->uv_querier->al_timer = 0;
686 * If this is a Group-Specific query which we did not source,
687 * we must set our membership timer to [Last Member Query Count] *
688 * the [Max Response Time] in the packet.
690 if (!(v->uv_flags & (VIFF_IGMPV1|VIFF_QUERIER)) && group != 0 &&
691 src != v->uv_lcl_addr) {
692 struct listaddr *g;
694 IF_DEBUG(DEBUG_IGMP)
695 log(LOG_DEBUG, 0,
696 "%s for %s from %s on vif %d, timer %d",
697 "Group-specific membership query",
698 inet_fmt(group, s2), inet_fmt(src, s1), vifi, tmo);
700 for (g = v->uv_groups; g != NULL; g = g->al_next) {
701 if (group == g->al_addr && g->al_query == 0) {
702 /* setup a timeout to remove the group membership */
703 if (g->al_timerid)
704 g->al_timerid = DeleteTimer(g->al_timerid);
705 g->al_timer = IGMP_LAST_MEMBER_QUERY_COUNT *
706 tmo / IGMP_TIMER_SCALE;
707 /* use al_query to record our presence in last-member state */
708 g->al_query = -1;
709 g->al_timerid = SetTimer(vifi, g);
710 IF_DEBUG(DEBUG_IGMP)
711 log(LOG_DEBUG, 0,
712 "timer for grp %s on vif %d set to %d",
713 inet_fmt(group, s2), vifi, g->al_timer);
714 break;
721 * Process an incoming group membership report.
723 void
724 accept_group_report(u_int32 src, u_int32 dst, u_int32 group, int r_type)
726 vifi_t vifi;
727 struct uvif *v;
728 struct listaddr *g;
730 if ((vifi = find_vif(src, dst)) == NO_VIF ||
731 (uvifs[vifi].uv_flags & VIFF_TUNNEL)) {
732 log(LOG_INFO, 0,
733 "ignoring group membership report from non-adjacent host %s",
734 inet_fmt(src, s1));
735 return;
738 v = &uvifs[vifi];
741 * Look for the group in our group list; if found, reset its timer.
743 for (g = v->uv_groups; g != NULL; g = g->al_next) {
744 if (group == g->al_addr) {
745 if (r_type == IGMP_V1_MEMBERSHIP_REPORT)
746 g->al_old = OLD_AGE_THRESHOLD;
748 g->al_reporter = src;
750 /** delete old timers, set a timer for expiration **/
751 g->al_timer = IGMP_GROUP_MEMBERSHIP_INTERVAL;
752 if (g->al_query)
753 g->al_query = DeleteTimer(g->al_query);
754 if (g->al_timerid)
755 g->al_timerid = DeleteTimer(g->al_timerid);
756 g->al_timerid = SetTimer(vifi, g);
757 break;
762 * If not found, add it to the list and update kernel cache.
764 if (g == NULL) {
765 g = (struct listaddr *)malloc(sizeof(struct listaddr));
766 if (g == NULL)
767 log(LOG_ERR, 0, "ran out of memory"); /* fatal */
769 g->al_addr = group;
770 if (r_type == IGMP_V1_MEMBERSHIP_REPORT)
771 g->al_old = OLD_AGE_THRESHOLD;
772 else
773 g->al_old = 0;
775 /** set a timer for expiration **/
776 g->al_query = 0;
777 g->al_timer = IGMP_GROUP_MEMBERSHIP_INTERVAL;
778 g->al_reporter = src;
779 g->al_timerid = SetTimer(vifi, g);
780 g->al_next = v->uv_groups;
781 v->uv_groups = g;
782 time(&g->al_ctime);
784 update_lclgrp(vifi, group);
788 * Check if a graft is necessary for this group
790 chkgrp_graft(vifi, group);
794 * Process an incoming IGMPv2 Leave Group message.
796 void
797 accept_leave_message(u_int32 src, u_int32 dst, u_int32 group)
799 vifi_t vifi;
800 struct uvif *v;
801 struct listaddr *g;
803 if ((vifi = find_vif(src, dst)) == NO_VIF ||
804 (uvifs[vifi].uv_flags & VIFF_TUNNEL)) {
805 log(LOG_INFO, 0,
806 "ignoring group leave report from non-adjacent host %s",
807 inet_fmt(src, s1));
808 return;
811 v = &uvifs[vifi];
813 if (!(v->uv_flags & VIFF_QUERIER) || (v->uv_flags & VIFF_IGMPV1))
814 return;
817 * Look for the group in our group list in order to set up a short-timeout
818 * query.
820 for (g = v->uv_groups; g != NULL; g = g->al_next) {
821 if (group == g->al_addr) {
822 IF_DEBUG(DEBUG_IGMP)
823 log(LOG_DEBUG, 0,
824 "[vif.c, _accept_leave_message] %d %d \n",
825 g->al_old, g->al_query);
827 /* Ignore the leave message if there are old hosts present */
828 if (g->al_old)
829 return;
831 /* still waiting for a reply to a query, ignore the leave */
832 if (g->al_query)
833 return;
835 /** delete old timer set a timer for expiration **/
836 if (g->al_timerid)
837 g->al_timerid = DeleteTimer(g->al_timerid);
839 #if IGMP_LAST_MEMBER_QUERY_COUNT != 2
840 This code needs to be updated to keep a counter of the number
841 of queries remaining.
842 #endif
843 /** send a group specific querry **/
844 g->al_timer = IGMP_LAST_MEMBER_QUERY_INTERVAL *
845 (IGMP_LAST_MEMBER_QUERY_COUNT + 1);
846 send_igmp(v->uv_lcl_addr, g->al_addr,
847 IGMP_MEMBERSHIP_QUERY,
848 IGMP_LAST_MEMBER_QUERY_INTERVAL * IGMP_TIMER_SCALE,
849 g->al_addr, 0);
850 g->al_query = SetQueryTimer(g, vifi,
851 IGMP_LAST_MEMBER_QUERY_INTERVAL,
852 IGMP_LAST_MEMBER_QUERY_INTERVAL * IGMP_TIMER_SCALE);
853 g->al_timerid = SetTimer(vifi, g);
854 break;
861 * Send a periodic probe on all vifs.
862 * Useful to determine one-way interfaces.
863 * Detect neighbor loss faster.
865 void
866 probe_for_neighbors(void)
868 vifi_t vifi;
869 struct uvif *v;
871 for (vifi = 0, v = uvifs; vifi < numvifs; vifi++, v++) {
872 if (!(v->uv_flags & (VIFF_DOWN|VIFF_DISABLED))) {
873 send_probe_on_vif(v);
880 * Send a list of all of our neighbors to the requestor, `src'.
882 void
883 accept_neighbor_request(u_int32 src, u_int32 dst)
885 vifi_t vifi;
886 struct uvif *v;
887 u_char *p, *ncount;
888 struct listaddr *la;
889 int datalen;
890 u_int32 temp_addr, them = src;
892 #define PUT_ADDR(a) temp_addr = ntohl(a); \
893 *p++ = temp_addr >> 24; \
894 *p++ = (temp_addr >> 16) & 0xFF; \
895 *p++ = (temp_addr >> 8) & 0xFF; \
896 *p++ = temp_addr & 0xFF;
898 p = (u_char *) (send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
899 datalen = 0;
901 for (vifi = 0, v = uvifs; vifi < numvifs; vifi++, v++) {
902 if (v->uv_flags & VIFF_DISABLED)
903 continue;
905 ncount = 0;
907 for (la = v->uv_neighbors; la; la = la->al_next) {
909 /* Make sure that there's room for this neighbor... */
910 if (datalen + (ncount == 0 ? 4 + 3 + 4 : 4) > MAX_DVMRP_DATA_LEN) {
911 send_igmp(INADDR_ANY, them, IGMP_DVMRP, DVMRP_NEIGHBORS,
912 htonl(MROUTED_LEVEL), datalen);
913 p = (u_char *) (send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
914 datalen = 0;
915 ncount = 0;
918 /* Put out the header for this neighbor list... */
919 if (ncount == 0) {
920 PUT_ADDR(v->uv_lcl_addr);
921 *p++ = v->uv_metric;
922 *p++ = v->uv_threshold;
923 ncount = p;
924 *p++ = 0;
925 datalen += 4 + 3;
928 PUT_ADDR(la->al_addr);
929 datalen += 4;
930 (*ncount)++;
934 if (datalen != 0)
935 send_igmp(INADDR_ANY, them, IGMP_DVMRP, DVMRP_NEIGHBORS,
936 htonl(MROUTED_LEVEL), datalen);
940 * Send a list of all of our neighbors to the requestor, `src'.
942 void
943 accept_neighbor_request2(u_int32 src, u_int32 dst)
945 vifi_t vifi;
946 struct uvif *v;
947 u_char *p, *ncount;
948 struct listaddr *la;
949 int datalen;
950 u_int32 them = src;
952 p = (u_char *) (send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
953 datalen = 0;
955 for (vifi = 0, v = uvifs; vifi < numvifs; vifi++, v++) {
956 u_short vflags = v->uv_flags;
957 u_char rflags = 0;
959 if (vflags & VIFF_TUNNEL)
960 rflags |= DVMRP_NF_TUNNEL;
961 if (vflags & VIFF_SRCRT)
962 rflags |= DVMRP_NF_SRCRT;
963 if (vflags & VIFF_DOWN)
964 rflags |= DVMRP_NF_DOWN;
965 if (vflags & VIFF_DISABLED)
966 rflags |= DVMRP_NF_DISABLED;
967 if (vflags & VIFF_QUERIER)
968 rflags |= DVMRP_NF_QUERIER;
969 if (vflags & VIFF_LEAF)
970 rflags |= DVMRP_NF_LEAF;
971 ncount = 0;
972 la = v->uv_neighbors;
973 if (la == NULL) {
975 * include down & disabled interfaces and interfaces on
976 * leaf nets.
978 if (rflags & DVMRP_NF_TUNNEL)
979 rflags |= DVMRP_NF_DOWN;
980 if (datalen > MAX_DVMRP_DATA_LEN - 12) {
981 send_igmp(INADDR_ANY, them, IGMP_DVMRP, DVMRP_NEIGHBORS2,
982 htonl(MROUTED_LEVEL), datalen);
983 p = (u_char *) (send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
984 datalen = 0;
986 *(u_int*)p = v->uv_lcl_addr;
987 p += 4;
988 *p++ = v->uv_metric;
989 *p++ = v->uv_threshold;
990 *p++ = rflags;
991 *p++ = 1;
992 *(u_int*)p = v->uv_rmt_addr;
993 p += 4;
994 datalen += 12;
995 } else {
996 for ( ; la; la = la->al_next) {
997 /* Make sure that there's room for this neighbor... */
998 if (datalen + (ncount == 0 ? 4+4+4 : 4) > MAX_DVMRP_DATA_LEN) {
999 send_igmp(INADDR_ANY, them, IGMP_DVMRP, DVMRP_NEIGHBORS2,
1000 htonl(MROUTED_LEVEL), datalen);
1001 p = (u_char *) (send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
1002 datalen = 0;
1003 ncount = 0;
1005 /* Put out the header for this neighbor list... */
1006 if (ncount == 0) {
1007 /* If it's a one-way tunnel, mark it down. */
1008 if (rflags & DVMRP_NF_TUNNEL && la->al_flags & NBRF_ONEWAY)
1009 rflags |= DVMRP_NF_DOWN;
1010 *(u_int*)p = v->uv_lcl_addr;
1011 p += 4;
1012 *p++ = v->uv_metric;
1013 *p++ = v->uv_threshold;
1014 *p++ = rflags;
1015 ncount = p;
1016 *p++ = 0;
1017 datalen += 4 + 4;
1019 /* Don't report one-way peering on phyint at all */
1020 if (!(rflags & DVMRP_NF_TUNNEL) && la->al_flags & NBRF_ONEWAY)
1021 continue;
1022 *(u_int*)p = la->al_addr;
1023 p += 4;
1024 datalen += 4;
1025 (*ncount)++;
1027 if (*ncount == 0) {
1028 *(u_int*)p = v->uv_rmt_addr;
1029 p += 4;
1030 datalen += 4;
1031 (*ncount)++;
1035 if (datalen != 0)
1036 send_igmp(INADDR_ANY, them, IGMP_DVMRP, DVMRP_NEIGHBORS2,
1037 htonl(MROUTED_LEVEL), datalen);
1040 void
1041 accept_info_request(u_int32 src, u_int32 dst, u_char *p, int datalen)
1043 u_char *q;
1044 int len;
1045 int outlen = 0;
1047 q = (u_char *) (send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
1049 /* To be general, this must deal properly with breaking up over-sized
1050 * packets. That implies passing a length to each function, and
1051 * allowing each function to request to be called again. Right now,
1052 * we're only implementing the one thing we are positive will fit into
1053 * a single packet, so we wimp out.
1055 while (datalen > 0) {
1056 len = 0;
1057 switch (*p) {
1058 case DVMRP_INFO_VERSION:
1059 len = info_version(q, RECV_BUF_SIZE-(q-(u_char *)send_buf));
1060 break;
1062 case DVMRP_INFO_NEIGHBORS:
1063 default:
1064 log(LOG_INFO, 0, "ignoring unknown info type %d", *p);
1065 break;
1067 *(q+1) = len++;
1068 outlen += len * 4;
1069 q += len * 4;
1070 len = (*(p+1) + 1) * 4;
1071 p += len;
1072 datalen -= len;
1075 if (outlen != 0)
1076 send_igmp(INADDR_ANY, src, IGMP_DVMRP, DVMRP_INFO_REPLY,
1077 htonl(MROUTED_LEVEL), outlen);
1081 * Information response -- return version string
1083 static int
1084 info_version(char *p, int plen)
1086 int len;
1087 extern char versionstring[];
1089 *p++ = DVMRP_INFO_VERSION;
1090 p++; /* skip over length */
1091 *p++ = 0; /* zero out */
1092 *p++ = 0; /* reserved fields */
1093 strncpy(p, versionstring, plen - 4);
1094 p[plen-5] = '\0';
1096 len = strlen(versionstring);
1097 return ((len + 3) / 4);
1101 * Process an incoming neighbor-list message.
1103 void
1104 accept_neighbors(u_int32 src, u_int32 dst, u_char *p, int datalen,
1105 u_int32 level)
1107 log(LOG_INFO, 0, "ignoring spurious DVMRP neighbor list from %s to %s",
1108 inet_fmt(src, s1), inet_fmt(dst, s2));
1113 * Process an incoming neighbor-list message.
1115 void
1116 accept_neighbors2(u_int32 src, u_int32 dst, u_char *p, int datalen,
1117 u_int32 level)
1119 IF_DEBUG(DEBUG_PKT)
1120 log(LOG_DEBUG, 0, "ignoring spurious DVMRP neighbor list2 from %s to %s",
1121 inet_fmt(src, s1), inet_fmt(dst, s2));
1125 * Process an incoming info reply message.
1127 void
1128 accept_info_reply(u_int32 src, u_int32 dst, u_char *p, int datalen)
1130 IF_DEBUG(DEBUG_PKT)
1131 log(LOG_DEBUG, 0, "ignoring spurious DVMRP info reply from %s to %s",
1132 inet_fmt(src, s1), inet_fmt(dst, s2));
1137 * Update the neighbor entry for neighbor 'addr' on vif 'vifi'.
1138 * 'msgtype' is the type of DVMRP message received from the neighbor.
1139 * Return the neighbor entry if 'addr' is a valid neighbor, FALSE otherwise.
1141 struct listaddr *
1142 update_neighbor(vifi_t vifi, u_int32 addr, int msgtype, char *p, int datalen,
1143 u_int32 level)
1145 struct uvif *v;
1146 struct listaddr *n;
1147 int pv = level & 0xff;
1148 int mv = (level >> 8) & 0xff;
1149 int has_genid = 0;
1150 int in_router_list = 0;
1151 int dvmrpspec = 0;
1152 u_int32 genid;
1153 u_int32 send_tables = 0;
1154 int i;
1155 int do_reset = FALSE;
1157 v = &uvifs[vifi];
1160 * Confirm that 'addr' is a valid neighbor address on vif 'vifi'.
1161 * IT IS ASSUMED that this was preceded by a call to find_vif(), which
1162 * checks that 'addr' is either a valid remote tunnel endpoint or a
1163 * non-broadcast address belonging to a directly-connected subnet.
1164 * Therefore, here we check only that 'addr' is not our own address
1165 * (due to an impostor or erroneous loopback) or an address of the form
1166 * {subnet,0} ("the unknown host"). These checks are not performed in
1167 * find_vif() because those types of address are acceptable for some
1168 * types of IGMP message (such as group membership reports).
1170 if (!(v->uv_flags & VIFF_TUNNEL) &&
1171 (addr == v->uv_lcl_addr ||
1172 addr == v->uv_subnet )) {
1173 log(LOG_WARNING, 0,
1174 "received DVMRP message from %s: %s",
1175 (addr == v->uv_lcl_addr) ? "self (check device loopback)" :
1176 "'the unknown host'",
1177 inet_fmt(addr, s1));
1178 return NULL;
1182 * Ignore all neighbors on vifs forced into leaf mode
1184 if (v->uv_flags & VIFF_FORCE_LEAF) {
1185 return NULL;
1189 * mrouted's version 3.3 and later include the generation ID
1190 * and the list of neighbors on the vif in their probe messages.
1192 if (msgtype == DVMRP_PROBE && ((pv == 3 && mv > 2) ||
1193 (pv > 3 && pv < 10))) {
1194 u_int32 router;
1196 IF_DEBUG(DEBUG_PEER)
1197 log(LOG_DEBUG, 0, "checking probe from %s (%d.%d) on vif %d",
1198 inet_fmt(addr, s1), pv, mv, vifi);
1200 if (datalen < 4) {
1201 log(LOG_WARNING, 0,
1202 "received truncated probe message from %s (len %d)",
1203 inet_fmt(addr, s1), datalen);
1204 return NULL;
1207 has_genid = 1;
1209 for (i = 0; i < 4; i++)
1210 ((char *)&genid)[i] = *p++;
1211 datalen -= 4;
1213 while (datalen > 0) {
1214 if (datalen < 4) {
1215 log(LOG_WARNING, 0,
1216 "received truncated probe message from %s (len %d)",
1217 inet_fmt(addr, s1), datalen);
1218 return NULL;
1220 for (i = 0; i < 4; i++)
1221 ((char *)&router)[i] = *p++;
1222 datalen -= 4;
1224 if (router == v->uv_lcl_addr) {
1225 in_router_list = 1;
1226 break;
1231 if ((pv == 3 && mv == 255) || (pv > 3 && pv < 10))
1232 dvmrpspec = 1;
1235 * Look for addr in list of neighbors.
1237 for (n = v->uv_neighbors; n != NULL; n = n->al_next) {
1238 if (addr == n->al_addr) {
1239 break;
1243 if (n == NULL) {
1245 * New neighbor.
1247 * If this neighbor follows the DVMRP spec, start the probe
1248 * handshake. If not, then it doesn't require the probe
1249 * handshake, so establish the peering immediately.
1251 if (dvmrpspec && (msgtype != DVMRP_PROBE))
1252 return NULL;
1254 for (i = 0; i < MAXNBRS; i++)
1255 if (nbrs[i] == NULL)
1256 break;
1258 if (i == MAXNBRS) {
1259 /* XXX This is a severe new restriction. */
1260 /* XXX want extensible bitmaps! */
1261 log(LOG_ERR, 0, "Can't handle %dth neighbor %s on vif %d!",
1262 MAXNBRS, inet_fmt(addr, s1), vifi);
1263 /*NOTREACHED*/
1267 * Add it to our list of neighbors.
1269 IF_DEBUG(DEBUG_PEER)
1270 log(LOG_DEBUG, 0, "New neighbor %s on vif %d v%d.%d nf 0x%02x idx %d",
1271 inet_fmt(addr, s1), vifi, level & 0xff, (level >> 8) & 0xff,
1272 (level >> 16) & 0xff, i);
1274 n = (struct listaddr *)malloc(sizeof(struct listaddr));
1275 if (n == NULL)
1276 log(LOG_ERR, 0, "ran out of memory"); /* fatal */
1278 n->al_addr = addr;
1279 n->al_pv = pv;
1280 n->al_mv = mv;
1281 n->al_genid = has_genid ? genid : 0;
1282 n->al_index = i;
1283 nbrs[i] = n;
1285 time(&n->al_ctime);
1286 n->al_timer = 0;
1287 n->al_flags = has_genid ? NBRF_GENID : 0;
1288 n->al_next = v->uv_neighbors;
1289 v->uv_neighbors = n;
1292 * If we are not configured to peer with non-pruning routers,
1293 * check the deprecated "I-know-how-to-prune" bit. This bit
1294 * was MBZ in early mrouted implementations (<3.5) and is required
1295 * to be set by the DVMRPv3 specification.
1297 if (!(v->uv_flags & VIFF_ALLOW_NONPRUNERS) &&
1298 !((level & 0x020000) || (pv == 3 && mv < 5))) {
1299 n->al_flags |= NBRF_TOOOLD;
1303 * If this router implements the DVMRPv3 spec, then don't peer
1304 * with him if we haven't yet established a bidirectional connection.
1306 if (dvmrpspec) {
1307 if (!in_router_list) {
1308 IF_DEBUG(DEBUG_PEER)
1309 log(LOG_DEBUG, 0, "waiting for probe from %s with my addr",
1310 inet_fmt(addr, s1));
1311 n->al_flags |= NBRF_WAITING;
1312 return NULL;
1316 if (n->al_flags & NBRF_DONTPEER) {
1317 IF_DEBUG(DEBUG_PEER)
1318 log(LOG_DEBUG, 0, "not peering with %s on vif %d because %x",
1319 inet_fmt(addr, s1), vifi, n->al_flags & NBRF_DONTPEER);
1320 return NULL;
1324 * If we thought that we had no neighbors on this vif, send a route
1325 * report to the vif. If this is just a new neighbor on the same
1326 * vif, send the route report just to the new neighbor.
1328 if (NBRM_ISEMPTY(v->uv_nbrmap)) {
1329 send_tables = v->uv_dst_addr;
1330 vifs_with_neighbors++;
1331 } else {
1332 send_tables = addr;
1336 NBRM_SET(i, v->uv_nbrmap);
1337 add_neighbor_to_routes(vifi, i);
1338 } else {
1340 * Found it. Reset its timer.
1342 n->al_timer = 0;
1344 if (n->al_flags & NBRF_WAITING && msgtype == DVMRP_PROBE) {
1345 n->al_flags &= ~NBRF_WAITING;
1346 if (!in_router_list) {
1347 log(LOG_WARNING, 0, "possible one-way peering with %s on vif %d",
1348 inet_fmt(addr, s1), vifi);
1349 n->al_flags |= NBRF_ONEWAY;
1350 return NULL;
1351 } else {
1352 if (NBRM_ISEMPTY(v->uv_nbrmap)) {
1353 send_tables = v->uv_dst_addr;
1354 vifs_with_neighbors++;
1355 } else {
1356 send_tables = addr;
1358 NBRM_SET(n->al_index, v->uv_nbrmap);
1359 add_neighbor_to_routes(vifi, n->al_index);
1360 IF_DEBUG(DEBUG_PEER)
1361 log(LOG_DEBUG, 0, "%s on vif %d exits WAITING",
1362 inet_fmt(addr, s1), vifi);
1366 if (n->al_flags & NBRF_ONEWAY && msgtype == DVMRP_PROBE) {
1367 if (in_router_list) {
1368 if (NBRM_ISEMPTY(v->uv_nbrmap))
1369 vifs_with_neighbors++;
1370 NBRM_SET(n->al_index, v->uv_nbrmap);
1371 add_neighbor_to_routes(vifi, n->al_index);
1372 log(LOG_NOTICE, 0, "peering with %s on vif %d is no longer one-way",
1373 inet_fmt(addr, s1), vifi);
1374 n->al_flags &= ~NBRF_ONEWAY;
1375 } else {
1376 /* XXX rate-limited warning message? */
1377 IF_DEBUG(DEBUG_PEER)
1378 log(LOG_DEBUG, 0, "%s on vif %d is still ONEWAY",
1379 inet_fmt(addr, s1), vifi);
1384 * When peering with a genid-capable but pre-DVMRP spec peer,
1385 * we might bring up the peering with a route report and not
1386 * remember his genid. Assume that he doesn't send a route
1387 * report and then reboot before sending a probe.
1389 if (has_genid && !(n->al_flags & NBRF_GENID)) {
1390 n->al_flags |= NBRF_GENID;
1391 n->al_genid = genid;
1395 * update the neighbors version and protocol number and genid
1396 * if changed => router went down and came up,
1397 * so take action immediately.
1399 if ((n->al_pv != pv) ||
1400 (n->al_mv != mv) ||
1401 (has_genid && n->al_genid != genid)) {
1403 do_reset = TRUE;
1404 IF_DEBUG(DEBUG_PEER)
1405 log(LOG_DEBUG, 0,
1406 "version/genid change neighbor %s [old:%d.%d/%8x, new:%d.%d/%8x]",
1407 inet_fmt(addr, s1),
1408 n->al_pv, n->al_mv, n->al_genid, pv, mv, genid);
1410 n->al_pv = pv;
1411 n->al_mv = mv;
1412 n->al_genid = genid;
1413 time(&n->al_ctime);
1416 if ((pv == 3 && mv > 2) || (pv > 3 && pv < 10)) {
1417 if (!(n->al_flags & VIFF_ONEWAY) && has_genid && !in_router_list &&
1418 (time(NULL) - n->al_ctime > 20)) {
1419 if (NBRM_ISSET(n->al_index, v->uv_nbrmap)) {
1420 NBRM_CLR(n->al_index, v->uv_nbrmap);
1421 if (NBRM_ISEMPTY(v->uv_nbrmap))
1422 vifs_with_neighbors--;
1424 delete_neighbor_from_routes(addr, vifi, n->al_index);
1425 reset_neighbor_state(vifi, addr);
1426 log(LOG_WARNING, 0, "peering with %s on vif %d is one-way",
1427 inet_fmt(addr, s1), vifi);
1428 n->al_flags |= NBRF_ONEWAY;
1432 if (n->al_flags & NBRF_DONTPEER) {
1433 IF_DEBUG(DEBUG_PEER)
1434 log(LOG_DEBUG, 0, "not peering with %s on vif %d because %x",
1435 inet_fmt(addr, s1), vifi, n->al_flags & NBRF_DONTPEER);
1436 return NULL;
1439 /* check "leaf" flag */
1441 if (do_reset) {
1442 reset_neighbor_state(vifi, addr);
1443 if (!send_tables)
1444 send_tables = addr;
1446 if (send_tables) {
1447 send_probe_on_vif(v);
1448 report(ALL_ROUTES, vifi, send_tables);
1450 v->uv_leaf_timer = 0;
1451 v->uv_flags &= ~VIFF_LEAF;
1453 return n;
1458 * On every timer interrupt, advance the timer in each neighbor and
1459 * group entry on every vif.
1461 void
1462 age_vifs(void)
1464 vifi_t vifi;
1465 struct uvif *v;
1466 struct listaddr *a, *prev_a;
1467 u_int32 addr;
1469 for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v ) {
1470 if (v->uv_leaf_timer && (v->uv_leaf_timer -= TIMER_INTERVAL == 0)) {
1471 v->uv_flags |= VIFF_LEAF;
1474 for (prev_a = (struct listaddr *)&(v->uv_neighbors),
1475 a = v->uv_neighbors;
1476 a != NULL;
1477 prev_a = a, a = a->al_next) {
1478 int exp_time;
1479 int idx;
1481 if (((a->al_pv == 3) && (a->al_mv >= 3)) ||
1482 ((a->al_pv > 3) && (a->al_pv < 10)))
1483 exp_time = NEIGHBOR_EXPIRE_TIME;
1484 else
1485 exp_time = OLD_NEIGHBOR_EXPIRE_TIME;
1487 if ((a->al_timer += TIMER_INTERVAL) < exp_time)
1488 continue;
1490 IF_DEBUG(DEBUG_PEER)
1491 log(LOG_DEBUG, 0, "Neighbor %s (%d.%d) expired after %d seconds",
1492 inet_fmt(a->al_addr, s1), a->al_pv, a->al_mv, exp_time);
1495 * Neighbor has expired; delete it from the neighbor list,
1496 * delete it from the 'dominants' and 'subordinates arrays of
1497 * any route entries.
1499 NBRM_CLR(a->al_index, v->uv_nbrmap);
1500 nbrs[a->al_index] = NULL; /* XXX is it a good idea to reuse indxs? */
1501 idx = a->al_index;
1502 addr = a->al_addr;
1503 prev_a->al_next = a->al_next;
1504 free((char *)a);
1505 a = prev_a;/*XXX use ** */
1507 delete_neighbor_from_routes(addr, vifi, idx);
1508 reset_neighbor_state(vifi, addr);
1510 if (NBRM_ISEMPTY(v->uv_nbrmap))
1511 vifs_with_neighbors--;
1513 v->uv_leaf_timer = LEAF_CONFIRMATION_TIME;
1516 if (v->uv_querier &&
1517 (v->uv_querier->al_timer += TIMER_INTERVAL) >
1518 IGMP_OTHER_QUERIER_PRESENT_INTERVAL) {
1520 * The current querier has timed out. We must become the
1521 * querier.
1523 IF_DEBUG(DEBUG_IGMP)
1524 log(LOG_DEBUG, 0, "querier %s timed out",
1525 inet_fmt(v->uv_querier->al_addr, s1));
1526 free(v->uv_querier);
1527 v->uv_querier = NULL;
1528 v->uv_flags |= VIFF_QUERIER;
1529 send_query(v);
1535 * Returns the neighbor info struct for a given neighbor
1537 struct listaddr *
1538 neighbor_info(vifi_t vifi, u_int32 addr)
1540 struct listaddr *u;
1542 for (u = uvifs[vifi].uv_neighbors; u; u = u->al_next)
1543 if (u->al_addr == addr)
1544 return u;
1546 return NULL;
1549 static struct vnflags {
1550 int vn_flag;
1551 char *vn_name;
1552 } vifflags[] = {
1553 { VIFF_DOWN, "down" },
1554 { VIFF_DISABLED, "disabled" },
1555 { VIFF_QUERIER, "querier" },
1556 { VIFF_ONEWAY, "one-way" },
1557 { VIFF_LEAF, "leaf" },
1558 { VIFF_IGMPV1, "IGMPv1" },
1559 { VIFF_REXMIT_PRUNES, "rexmit_prunes" },
1560 { VIFF_PASSIVE, "passive" },
1561 { VIFF_ALLOW_NONPRUNERS,"allow_nonpruners" },
1562 { VIFF_NOFLOOD, "noflood" },
1563 { VIFF_NOTRANSIT, "notransit" },
1564 { VIFF_BLASTER, "blaster" },
1565 { VIFF_FORCE_LEAF, "force_leaf" },
1566 { VIFF_OTUNNEL, "old-tunnel" },
1569 static struct vnflags nbrflags[] = {
1570 { NBRF_LEAF, "leaf" },
1571 { NBRF_GENID, "have-genid" },
1572 { NBRF_WAITING, "waiting" },
1573 { NBRF_ONEWAY, "one-way" },
1574 { NBRF_TOOOLD, "too old" },
1575 { NBRF_TOOMANYROUTES, "too many routes" },
1576 { NBRF_NOTPRUNING, "not pruning?" },
1580 * Print the contents of the uvifs array on file 'fp'.
1582 void
1583 dump_vifs(FILE *fp)
1585 vifi_t vifi;
1586 struct uvif *v;
1587 struct listaddr *a;
1588 struct phaddr *p;
1589 struct vif_acl *acl;
1590 int i;
1591 struct sioc_vif_req v_req;
1592 time_t now;
1593 char *label;
1595 time(&now);
1596 fprintf(fp, "vifs_with_neighbors = %d\n", vifs_with_neighbors);
1598 if (vifs_with_neighbors == 1)
1599 fprintf(fp,"[This host is a leaf]\n\n");
1601 fprintf(fp,
1602 "\nVirtual Interface Table\n%s",
1603 "Vif Name Local-Address ");
1604 fprintf(fp,
1605 "M Thr Rate Flags\n");
1607 for (vifi = 0, v = uvifs; vifi < numvifs; vifi++, v++) {
1609 fprintf(fp, "%2u %6s %-15s %6s: %-18s %2u %3u %5u ",
1610 vifi,
1611 v->uv_name,
1612 inet_fmt(v->uv_lcl_addr, s1),
1613 (v->uv_flags & VIFF_TUNNEL) ?
1614 "tunnel":
1615 "subnet",
1616 (v->uv_flags & VIFF_TUNNEL) ?
1617 inet_fmt(v->uv_rmt_addr, s2) :
1618 inet_fmts(v->uv_subnet, v->uv_subnetmask, s3),
1619 v->uv_metric,
1620 v->uv_threshold,
1621 v->uv_rate_limit);
1623 for (i = 0; i < sizeof(vifflags) / sizeof(struct vnflags); i++)
1624 if (v->uv_flags & vifflags[i].vn_flag)
1625 fprintf(fp, " %s", vifflags[i].vn_name);
1627 fprintf(fp, "\n");
1629 fprintf(fp, " #routes: %d\n", v->uv_nroutes);
1631 if (v->uv_admetric != 0)
1632 fprintf(fp, " advert-metric %2u\n",
1633 v->uv_admetric);
1635 label = "alternate subnets:";
1636 for (p = v->uv_addrs; p; p = p->pa_next) {
1637 fprintf(fp, " %18s %s\n", label,
1638 inet_fmts(p->pa_subnet, p->pa_subnetmask, s1));
1639 label = "";
1642 label = "peers:";
1643 for (a = v->uv_neighbors; a != NULL; a = a->al_next) {
1644 fprintf(fp, " %6s %s (%d.%d) [%d]",
1645 label, inet_fmt(a->al_addr, s1), a->al_pv, a->al_mv,
1646 a->al_index);
1647 for (i = 0; i < sizeof(nbrflags) / sizeof(struct vnflags); i++)
1648 if (a->al_flags & nbrflags[i].vn_flag)
1649 fprintf(fp, " %s", nbrflags[i].vn_name);
1650 fprintf(fp, " up %s\n", scaletime(now - a->al_ctime));
1651 /*fprintf(fp, " #routes %d\n", a->al_nroutes);*/
1652 label = "";
1655 label = "group host (time left):";
1656 for (a = v->uv_groups; a != NULL; a = a->al_next) {
1657 fprintf(fp, " %23s %-15s %-15s (%s)\n",
1658 label,
1659 inet_fmt(a->al_addr, s1),
1660 inet_fmt(a->al_reporter, s2),
1661 scaletime(timer_leftTimer(a->al_timerid)));
1662 label = "";
1664 label = "boundaries:";
1665 for (acl = v->uv_acl; acl != NULL; acl = acl->acl_next) {
1666 fprintf(fp, " %11s %-18s\n", label,
1667 inet_fmts(acl->acl_addr, acl->acl_mask, s1));
1668 label = "";
1670 if (v->uv_filter) {
1671 struct vf_element *vfe;
1672 char lbuf[100];
1674 sprintf(lbuf, "%5s %7s filter:",
1675 v->uv_filter->vf_flags & VFF_BIDIR ? "bidir"
1676 : " ",
1677 v->uv_filter->vf_type == VFT_ACCEPT ? "accept"
1678 : "deny");
1679 label = lbuf;
1680 for (vfe = v->uv_filter->vf_filter;
1681 vfe != NULL; vfe = vfe->vfe_next) {
1682 fprintf(fp, " %23s %-18s%s\n",
1683 label,
1684 inet_fmts(vfe->vfe_addr, vfe->vfe_mask, s1),
1685 vfe->vfe_flags & VFEF_EXACT ? " (exact)" : "");
1686 label = "";
1689 if (!(v->uv_flags & (VIFF_TUNNEL|VIFF_DOWN|VIFF_DISABLED))) {
1690 fprintf(fp, " IGMP querier: ");
1691 if (v->uv_querier == NULL)
1692 if (v->uv_flags & VIFF_QUERIER)
1693 fprintf(fp, "%-18s (this system)\n",
1694 inet_fmt(v->uv_lcl_addr, s1));
1695 else
1696 fprintf(fp, "NONE - querier election failure?\n");
1697 else
1698 fprintf(fp, "%-18s up %s last heard %s ago\n",
1699 inet_fmt(v->uv_querier->al_addr, s1),
1700 scaletime(now - v->uv_querier->al_ctime),
1701 scaletime(v->uv_querier->al_timer));
1703 if (v->uv_flags & VIFF_BLASTER)
1704 fprintf(fp, " blasterbuf size: %dk\n",
1705 v->uv_blasterlen / 1024);
1706 fprintf(fp, " Nbr bitmaps: 0x%08lx%08lx\n",/*XXX*/
1707 v->uv_nbrmap.hi, v->uv_nbrmap.lo);
1708 if (v->uv_prune_lifetime != 0)
1709 fprintf(fp, " Prune Lifetime: %d seconds\n",
1710 v->uv_prune_lifetime);
1712 v_req.vifi = vifi;
1713 if (did_final_init)
1714 if (ioctl(udp_socket, SIOCGETVIFCNT, (char *)&v_req) < 0) {
1715 log(LOG_WARNING, errno,
1716 "SIOCGETVIFCNT fails on vif %d", vifi);
1717 } else {
1718 fprintf(fp, " pkts/bytes in : %lu/%lu\n",
1719 v_req.icount, v_req.ibytes);
1720 fprintf(fp, " pkts/bytes out: %lu/%lu\n",
1721 v_req.ocount, v_req.obytes);
1723 fprintf(fp, "\n");
1725 fprintf(fp, "\n");
1729 * Time out record of a group membership on a vif
1731 static void
1732 DelVif(void *arg)
1734 cbk_t *cbk = (cbk_t *)arg;
1735 vifi_t vifi = cbk->vifi;
1736 struct uvif *v = &uvifs[vifi];
1737 struct listaddr *a, **anp, *g = cbk->g;
1740 * Group has expired
1741 * delete all kernel cache entries with this group
1743 if (g->al_query)
1744 DeleteTimer(g->al_query);
1746 delete_lclgrp(vifi, g->al_addr);
1748 anp = &(v->uv_groups);
1749 while ((a = *anp) != NULL) {
1750 if (a == g) {
1751 *anp = a->al_next;
1752 free((char *)a);
1753 } else {
1754 anp = &a->al_next;
1758 free(cbk);
1762 * Set a timer to delete the record of a group membership on a vif.
1764 static int
1765 SetTimer(vifi_t vifi, struct listaddr *g)
1767 cbk_t *cbk;
1769 cbk = (cbk_t *) malloc(sizeof(cbk_t));
1770 cbk->g = g;
1771 cbk->vifi = vifi;
1772 return timer_setTimer(g->al_timer, DelVif, cbk);
1776 * Delete a timer that was set above.
1778 static int
1779 DeleteTimer(int id)
1781 timer_clearTimer(id);
1782 return 0;
1786 * Send a group-specific query.
1788 static void
1789 SendQuery(void *arg)
1791 cbk_t *cbk = (cbk_t *)arg;
1792 struct uvif *v = &uvifs[cbk->vifi];
1794 send_igmp(v->uv_lcl_addr, cbk->g->al_addr,
1795 IGMP_MEMBERSHIP_QUERY,
1796 cbk->q_time, cbk->g->al_addr, 0);
1797 cbk->g->al_query = 0;
1798 free(cbk);
1802 * Set a timer to send a group-specific query.
1804 static int
1805 SetQueryTimer(struct listaddr *g, vifi_t vifi, int to_expire, int q_time)
1807 cbk_t *cbk;
1809 cbk = (cbk_t *) malloc(sizeof(cbk_t));
1810 cbk->g = g;
1811 cbk->q_time = q_time;
1812 cbk->vifi = vifi;
1813 return timer_setTimer(to_expire, SendQuery, cbk);