xref: /netbsd-src/usr.sbin/mrouted/vif.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /*	$NetBSD: vif.c,v 1.10 2002/07/14 16:30:42 wiz Exp $	*/
2 
3 /*
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.
7  *
8  * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
9  * Leland Stanford Junior University.
10  */
11 
12 
13 #include "defs.h"
14 #include <fcntl.h>
15 
16 /*
17  * Exported variables.
18  */
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. */
26 				/* How dumb.                                */
27 int		vifs_with_neighbors;	/* == 1 if I am a leaf		    */
28 
29 typedef struct {
30         vifi_t  vifi;
31         struct listaddr *g;
32 	int    q_time;
33 } cbk_t;
34 
35 /*
36  * Forward declarations.
37  */
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);
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,
49 			 int q_time);
50 
51 
52 /*
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.
56  */
57 void
58 init_vifs(void)
59 {
60     vifi_t vifi;
61     struct uvif *v;
62     int enabled_vifs, enabled_phyints;
63     extern char *configfilename;
64 
65     numvifs = 0;
66     vifs_with_neighbors = 0;
67     vifs_down = FALSE;
68 
69     /*
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.)
73      */
74     if ((udp_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
75 	log(LOG_ERR, errno, "UDP socket");
76     log(LOG_INFO,0,"Getting vifs from kernel interfaces");
77     config_vifs_from_kernel();
78     log(LOG_INFO,0,"Getting vifs from %s",configfilename);
79     config_vifs_from_file();
80 
81     /*
82      * Quit if there are fewer than two enabled vifs.
83      */
84     enabled_vifs    = 0;
85     enabled_phyints = 0;
86     phys_vif	    = -1;
87     for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
88 	if (!(v->uv_flags & VIFF_DISABLED)) {
89 	    ++enabled_vifs;
90 	    if (!(v->uv_flags & VIFF_TUNNEL)) {
91     	    	if (phys_vif == -1)
92     	    	    phys_vif = vifi;
93 		++enabled_phyints;
94 	    }
95 	}
96     }
97     if (enabled_vifs < 2)
98 	log(LOG_ERR, 0, "can't forward: %s",
99 	    enabled_vifs == 0 ? "no enabled vifs" : "only one enabled vif");
100 
101     if (enabled_phyints == 0)
102 	log(LOG_WARNING, 0,
103 	    "no enabled interfaces, forwarding via tunnels only");
104 
105     log(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 		    log(LOG_INFO, 0, "vif #%d, tunnel %s -> %s", vifi,
111 				inet_fmt(v->uv_lcl_addr, s1),
112 				inet_fmt(v->uv_rmt_addr, s2));
113 		else
114 		    log(LOG_INFO, 0, "vif #%d, phyint %s", vifi,
115 				inet_fmt(v->uv_lcl_addr, s1));
116 		start_vif2(vifi);
117 	    } else log(LOG_INFO, 0,
118 		     "%s is not yet up; vif #%u not in service",
119 		     v->uv_name, vifi);
120 	}
121     }
122 }
123 
124 /*
125  * Start routing on all virtual interfaces that are not down or
126  * administratively disabled.
127  */
128 void
129 init_installvifs(void)
130 {
131     vifi_t vifi;
132     struct uvif *v;
133 
134     log(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 		    log(LOG_INFO, 0, "vif #%d, tunnel %s -> %s", vifi,
140 				inet_fmt(v->uv_lcl_addr, s1),
141 				inet_fmt(v->uv_rmt_addr, s2));
142 		else
143 		    log(LOG_INFO, 0, "vif #%d, phyint %s", vifi,
144 				inet_fmt(v->uv_lcl_addr, s1));
145 		k_add_vif(vifi, &uvifs[vifi]);
146 	    } else log(LOG_INFO, 0,
147 		     "%s is not yet up; vif #%u not in service",
148 		     v->uv_name, vifi);
149 	}
150     }
151 }
152 
153 /*
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
157  * disabled.
158  */
159 void
160 check_vif_state(void)
161 {
162     vifi_t vifi;
163     struct uvif *v;
164     struct ifreq ifr;
165 
166     vifs_down = FALSE;
167     for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
168 
169 	if (v->uv_flags & VIFF_DISABLED) continue;
170 
171 	strncpy(ifr.ifr_name, v->uv_name, IFNAMSIZ);
172 	if (ioctl(udp_socket, SIOCGIFFLAGS, (char *)&ifr) < 0)
173 	    log(LOG_ERR, errno,
174 		"ioctl SIOCGIFFLAGS for %s", ifr.ifr_name);
175 
176 	if (v->uv_flags & VIFF_DOWN) {
177 	    if (ifr.ifr_flags & IFF_UP) {
178 		v->uv_flags &= ~VIFF_DOWN;
179 		start_vif(vifi);
180 		log(LOG_INFO, 0,
181 		    "%s has come up; vif #%u now in service",
182 		    v->uv_name, vifi);
183 	    }
184 	    else vifs_down = TRUE;
185 	}
186 	else {
187 	    if (!(ifr.ifr_flags & IFF_UP)) {
188 		stop_vif(vifi);
189 		v->uv_flags |= VIFF_DOWN;
190 		log(LOG_INFO, 0,
191 		    "%s has gone down; vif #%u taken out of service",
192 		    v->uv_name, vifi);
193 		vifs_down = TRUE;
194 	    }
195 	}
196     }
197 }
198 
199 /*
200  * Send a probe message on vif v
201  */
202 static void
203 send_probe_on_vif(struct uvif *v)
204 {
205     char *p;
206     int datalen = 0;
207     struct listaddr *nbr;
208     int i;
209 
210     p = send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN;
211 
212     for (i = 0; i < 4; i++)
213 	*p++ = ((char *)&(dvmrp_genid))[i];
214     datalen += 4;
215 
216     /*
217      * add the neighbor list on the interface to the message
218      */
219     nbr = v->uv_neighbors;
220 
221     while (nbr) {
222 	for (i = 0; i < 4; i++)
223 	    *p++ = ((char *)&nbr->al_addr)[i];
224 	datalen +=4;
225 	nbr = nbr->al_next;
226     }
227 
228     send_igmp(v->uv_lcl_addr,
229 	      (v->uv_flags & VIFF_TUNNEL) ? v->uv_rmt_addr
230 	      : dvmrp_group,
231 	      IGMP_DVMRP, DVMRP_PROBE,
232 	      htonl(MROUTED_LEVEL |
233 	      ((v->uv_flags & VIFF_LEAF) ? 0 : LEAF_FLAGS)),
234 	      datalen);
235 }
236 
237 /*
238  * Add a vifi to the kernel and start routing on it.
239  */
240 static void
241 start_vif(vifi_t vifi)
242 {
243     /*
244      * Install the interface in the kernel's vif structure.
245      */
246     k_add_vif(vifi, &uvifs[vifi]);
247 
248     start_vif2(vifi);
249 }
250 
251 /*
252  * Add a vifi to all the user-level data structures but don't add
253  * it to the kernel yet.
254  */
255 static void
256 start_vif2(vifi_t vifi)
257 {
258     struct uvif *v;
259     u_int32_t src;
260     struct phaddr *p;
261 
262     v   = &uvifs[vifi];
263     src = v->uv_lcl_addr;
264 
265     /*
266      * Update the existing route entries to take into account the new vif.
267      */
268     add_vif_to_routes(vifi);
269 
270     if (!(v->uv_flags & VIFF_TUNNEL)) {
271 	/*
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'.)
277 	 */
278 	k_join(dvmrp_group, src);
279 
280 	/*
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.
284 	 */
285 	k_join(allrtrs_group, src);
286 
287 	/*
288 	 * Install an entry in the routing table for the subnet to which
289 	 * the interface is connected.
290 	 */
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);
296 	}
297 
298 	/*
299 	 * Until neighbors are discovered, assume responsibility for sending
300 	 * periodic group membership queries to the subnet.  Send the first
301 	 * query.
302 	 */
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);
307 	age_old_hosts();
308     }
309 
310     v->uv_leaf_timer = LEAF_CONFIRMATION_TIME;
311 
312     /*
313      * Send a probe via the new vif to look for neighbors.
314      */
315     send_probe_on_vif(v);
316 }
317 
318 /*
319  * Stop routing on the specified virtual interface.
320  */
321 static void
322 stop_vif(vifi_t vifi)
323 {
324     struct uvif *v;
325     struct listaddr *a;
326     struct phaddr *p;
327 
328     v = &uvifs[vifi];
329 
330     if (!(v->uv_flags & VIFF_TUNNEL)) {
331 	/*
332 	 * Depart from the DVMRP multicast group on the interface.
333 	 */
334 	k_leave(dvmrp_group, v->uv_lcl_addr);
335 
336 	/*
337 	 * Depart from the ALL-ROUTERS multicast group on the interface.
338 	 */
339 	k_leave(allrtrs_group, v->uv_lcl_addr);
340 
341 	/*
342 	 * Update the entry in the routing table for the subnet to which
343 	 * the interface is connected, to take into account the interface
344 	 * failure.
345 	 */
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);
351 	}
352 
353 	/*
354 	 * Discard all group addresses.  (No need to tell kernel;
355 	 * the k_del_vif() call, below, will clean up kernel state.)
356 	 */
357 	while (v->uv_groups != NULL) {
358 	    a = v->uv_groups;
359 	    v->uv_groups = a->al_next;
360 	    free((char *)a);
361 	}
362 
363 	v->uv_flags &= ~VIFF_QUERIER;
364     }
365 
366     /*
367      * Update the existing route entries to take into account the vif failure.
368      */
369     delete_vif_from_routes(vifi);
370 
371     /*
372      * Delete the interface from the kernel's vif structure.
373      */
374     k_del_vif(vifi);
375 
376     /*
377      * Discard all neighbor addresses.
378      */
379     if (v->uv_neighbors)
380 	vifs_with_neighbors--;
381 
382     while (v->uv_neighbors != NULL) {
383 	a = v->uv_neighbors;
384 	v->uv_neighbors = a->al_next;
385 	free((char *)a);
386     }
387 }
388 
389 
390 /*
391  * stop routing on all vifs
392  */
393 void
394 stop_all_vifs(void)
395 {
396     vifi_t vifi;
397     struct uvif *v;
398     struct listaddr *a;
399     struct vif_acl *acl;
400 
401     for (vifi = 0; vifi < numvifs; vifi++) {
402 	v = &uvifs[vifi];
403 	while (v->uv_groups != NULL) {
404 	    a = v->uv_groups;
405 	    v->uv_groups = a->al_next;
406 	    free((char *)a);
407 	}
408 	while (v->uv_neighbors != NULL) {
409 	    a = v->uv_neighbors;
410 	    v->uv_neighbors = a->al_next;
411 	    free((char *)a);
412 	}
413 	while (v->uv_acl != NULL) {
414 	    acl = v->uv_acl;
415 	    v->uv_acl = acl->acl_next;
416 	    free((char *)acl);
417 	}
418     }
419 }
420 
421 
422 /*
423  * Find the virtual interface from which an incoming packet arrived,
424  * based on the packet's source and destination IP addresses.
425  */
426 vifi_t
427 find_vif(u_int32_t src, u_int32_t dst)
428 {
429     vifi_t vifi;
430     struct uvif *v;
431     struct phaddr *p;
432 
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)
437 		    return(vifi);
438 	    }
439 	    else {
440 		if ((src & v->uv_subnetmask) == v->uv_subnet &&
441 		    ((v->uv_subnetmask == 0xffffffff) ||
442 		     (src != v->uv_subnetbcast)))
443 		    return(vifi);
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)))
448 			return(vifi);
449 		}
450 	    }
451 	}
452     }
453     return (NO_VIF);
454 }
455 
456 static void
457 age_old_hosts(void)
458 {
459     vifi_t vifi;
460     struct uvif *v;
461     struct listaddr *g;
462 
463     /*
464      * Decrement the old-hosts-present timer for each
465      * active group on each vif.
466      */
467     for (vifi = 0, v = uvifs; vifi < numvifs; vifi++, v++)
468         for (g = v->uv_groups; g != NULL; g = g->al_next)
469 	    if (g->al_old)
470 		g->al_old--;
471 }
472 
473 
474 /*
475  * Send group membership queries to all subnets for which I am querier.
476  */
477 void
478 query_groups(void)
479 {
480     vifi_t vifi;
481     struct uvif *v;
482 
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);
489 	}
490     }
491     age_old_hosts();
492 }
493 
494 /*
495  * Process an incoming host membership query
496  */
497 void
498 accept_membership_query(u_int32_t src, u_int32_t dst, u_int32_t group, int tmo)
499 {
500     vifi_t vifi;
501     struct uvif *v;
502 
503     if ((vifi = find_vif(src, dst)) == NO_VIF ||
504 	(uvifs[vifi].uv_flags & VIFF_TUNNEL)) {
505 	log(LOG_INFO, 0,
506 	    "ignoring group membership query from non-adjacent host %s",
507 	    inet_fmt(src, s1));
508 	return;
509     }
510 
511     v = &uvifs[vifi];
512 
513     /*
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.
516      *
517      * This is done here as well as in the neighbor discovery in case
518      * there is a querier that doesn't speak DVMRP.
519      *
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!
522      */
523     if ((v->uv_flags & VIFF_QUERIER) &&
524 	(ntohl(src) < ntohl(v->uv_lcl_addr))) {
525 	    v->uv_flags &= ~VIFF_QUERIER;
526 
527     }
528 }
529 
530 /*
531  * Process an incoming group membership report.
532  */
533 void
534 accept_group_report(u_int32_t src, u_int32_t dst, u_int32_t group, int r_type)
535 {
536     vifi_t vifi;
537     struct uvif *v;
538     struct listaddr *g;
539 
540     if ((vifi = find_vif(src, dst)) == NO_VIF ||
541 	(uvifs[vifi].uv_flags & VIFF_TUNNEL)) {
542 	log(LOG_INFO, 0,
543 	    "ignoring group membership report from non-adjacent host %s",
544 	    inet_fmt(src, s1));
545 	return;
546     }
547 
548     v = &uvifs[vifi];
549 
550     /*
551      * Look for the group in our group list; if found, reset its timer.
552      */
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;
557 #ifdef SNMP
558 	    g->al_genid = src;
559 #endif /* SNMP */
560 
561 	    /** delete old timers, set a timer for expiration **/
562 	    g->al_timer = GROUP_EXPIRE_TIME;
563 	    if (g->al_query)
564 		g->al_query = DeleteTimer(g->al_query);
565 	    if (g->al_timerid)
566 		g->al_timerid = DeleteTimer(g->al_timerid);
567 	    g->al_timerid = SetTimer(vifi, g);
568 	    break;
569 	}
570     }
571 
572     /*
573      * If not found, add it to the list and update kernel cache.
574      */
575     if (g == NULL) {
576 	g = (struct listaddr *)malloc(sizeof(struct listaddr));
577 	if (g == NULL)
578 	    log(LOG_ERR, 0, "ran out of memory");    /* fatal */
579 
580 	g->al_addr   = group;
581 	if (r_type == IGMP_v2_HOST_MEMBERSHIP_REPORT)
582 	    g->al_old = 0;
583 	else
584 	    g->al_old = OLD_AGE_THRESHOLD;
585 #ifdef SNMP
586 	g->al_genid = src;
587 #endif
588 
589 	/** set a timer for expiration **/
590         g->al_query = 0;
591 	g->al_timer  = GROUP_EXPIRE_TIME;
592 	time(&g->al_ctime);
593 	g->al_timerid = SetTimer(vifi, g);
594 	g->al_next   = v->uv_groups;
595 	v->uv_groups = g;
596 
597 	update_lclgrp(vifi, group);
598     }
599 
600     /*
601      * Check if a graft is necessary for this group
602      */
603     chkgrp_graft(vifi, group);
604 }
605 
606 
607 void
608 accept_leave_message(u_int32_t src, u_int32_t dst, u_int32_t group)
609 {
610     vifi_t vifi;
611     struct uvif *v;
612     struct listaddr *g;
613 
614     if ((vifi = find_vif(src, dst)) == NO_VIF ||
615 	(uvifs[vifi].uv_flags & VIFF_TUNNEL)) {
616 	log(LOG_INFO, 0,
617 	    "ignoring group leave report from non-adjacent host %s",
618 	    inet_fmt(src, s1));
619 	return;
620     }
621 
622     v = &uvifs[vifi];
623 
624     if (!(v->uv_flags & VIFF_QUERIER) || (v->uv_flags & VIFF_IGMPV1))
625 	return;
626 
627     /*
628      * Look for the group in our group list in order to set up a short-timeout
629      * query.
630      */
631     for (g = v->uv_groups; g != NULL; g = g->al_next) {
632 	if (group == g->al_addr) {
633 	    log(LOG_DEBUG, 0,
634 		"[vif.c, _accept_leave_message] %d %ld\n",
635 		g->al_old, g->al_query);
636 
637 	    /* Ignore the leave message if there are old hosts present */
638 	    if (g->al_old)
639 		return;
640 
641 	    /* still waiting for a reply to a query, ignore the leave */
642 	    if (g->al_query)
643 		return;
644 
645 	    /** delete old timer set a timer for expiration **/
646 	    if (g->al_timerid)
647 		g->al_timerid = DeleteTimer(g->al_timerid);
648 
649 	    /** send a group specific querry **/
650 	    g->al_timer = LEAVE_EXPIRE_TIME;
651 	    send_igmp(v->uv_lcl_addr, g->al_addr,
652 		      IGMP_HOST_MEMBERSHIP_QUERY,
653 		      LEAVE_EXPIRE_TIME / 3 * IGMP_TIMER_SCALE,
654 		      g->al_addr, 0);
655 	    g->al_query = SetQueryTimer(g, vifi, g->al_timer / 3,
656 			 	LEAVE_EXPIRE_TIME / 3 * IGMP_TIMER_SCALE);
657 	    g->al_timerid = SetTimer(vifi, g);
658 	    break;
659 	}
660     }
661 }
662 
663 
664 /*
665  * Send a periodic probe on all vifs.
666  * Useful to determine one-way interfaces.
667  * Detect neighbor loss faster.
668  */
669 void
670 probe_for_neighbors(void)
671 {
672     vifi_t vifi;
673     struct uvif *v;
674 
675     for (vifi = 0, v = uvifs; vifi < numvifs; vifi++, v++) {
676 	if (!(v->uv_flags & (VIFF_DOWN|VIFF_DISABLED))) {
677 	    send_probe_on_vif(v);
678 	}
679     }
680 }
681 
682 
683 /*
684  * Send a list of all of our neighbors to the requestor, `src'.
685  */
686 void
687 accept_neighbor_request(u_int32_t src, u_int32_t dst)
688 {
689     vifi_t vifi;
690     struct uvif *v;
691     u_char *p, *ncount;
692     struct listaddr *la;
693     int	datalen;
694     u_int32_t temp_addr, us, them = src;
695 
696     /* Determine which of our addresses to use as the source of our response
697      * to this query.
698      */
699     if (IN_MULTICAST(ntohl(dst))) { /* query sent to a multicast group */
700 	int udp;		/* find best interface to reply on */
701 	struct sockaddr_in addr;
702 	int addrlen = sizeof(addr);
703 
704 	memset(&addr, 0, sizeof(addr));
705 	addr.sin_family = AF_INET;
706 #if (defined(BSD) && (BSD >= 199103))
707 	addr.sin_len = sizeof addr;
708 #endif
709 	addr.sin_addr.s_addr = dst;
710 	addr.sin_port = htons(2000); /* any port over 1024 will do... */
711 	if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0
712 	    || connect(udp, (struct sockaddr *) &addr, sizeof(addr)) < 0
713 	    || getsockname(udp, (struct sockaddr *) &addr, &addrlen) < 0) {
714 	    log(LOG_WARNING, errno, "Determining local address");
715 	    close(udp);
716 	    return;
717 	}
718 	close(udp);
719 	us = addr.sin_addr.s_addr;
720     } else			/* query sent to us alone */
721 	us = dst;
722 
723 #define PUT_ADDR(a)	temp_addr = ntohl(a); \
724 			*p++ = temp_addr >> 24; \
725 			*p++ = (temp_addr >> 16) & 0xFF; \
726 			*p++ = (temp_addr >> 8) & 0xFF; \
727 			*p++ = temp_addr & 0xFF;
728 
729     p = (u_char *) (send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
730     datalen = 0;
731 
732     for (vifi = 0, v = uvifs; vifi < numvifs; vifi++, v++) {
733 	if (v->uv_flags & VIFF_DISABLED)
734 	    continue;
735 
736 	ncount = 0;
737 
738 	for (la = v->uv_neighbors; la; la = la->al_next) {
739 
740 	    /* Make sure that there's room for this neighbor... */
741 	    if (datalen + (ncount == 0 ? 4 + 3 + 4 : 4) > MAX_DVMRP_DATA_LEN) {
742 		send_igmp(us, them, IGMP_DVMRP, DVMRP_NEIGHBORS,
743 			  htonl(MROUTED_LEVEL), datalen);
744 		p = (u_char *) (send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
745 		datalen = 0;
746 		ncount = 0;
747 	    }
748 
749 	    /* Put out the header for this neighbor list... */
750 	    if (ncount == 0) {
751 		PUT_ADDR(v->uv_lcl_addr);
752 		*p++ = v->uv_metric;
753 		*p++ = v->uv_threshold;
754 		ncount = p;
755 		*p++ = 0;
756 		datalen += 4 + 3;
757 	    }
758 
759 	    PUT_ADDR(la->al_addr);
760 	    datalen += 4;
761 	    (*ncount)++;
762 	}
763     }
764 
765     if (datalen != 0)
766 	send_igmp(us, them, IGMP_DVMRP, DVMRP_NEIGHBORS, htonl(MROUTED_LEVEL),
767 		  datalen);
768 }
769 
770 /*
771  * Send a list of all of our neighbors to the requestor, `src'.
772  */
773 void
774 accept_neighbor_request2(u_int32_t src, u_int32_t dst)
775 {
776     vifi_t vifi;
777     struct uvif *v;
778     u_char *p, *ncount;
779     struct listaddr *la;
780     int	datalen;
781     u_int32_t us, them = src;
782 
783     /* Determine which of our addresses to use as the source of our response
784      * to this query.
785      */
786     if (IN_MULTICAST(ntohl(dst))) { /* query sent to a multicast group */
787 	int udp;		/* find best interface to reply on */
788 	struct sockaddr_in addr;
789 	int addrlen = sizeof(addr);
790 
791 	memset(&addr, 0, sizeof(addr));
792 	addr.sin_family = AF_INET;
793 #if (defined(BSD) && (BSD >= 199103))
794 	addr.sin_len = sizeof addr;
795 #endif
796 	addr.sin_addr.s_addr = dst;
797 	addr.sin_port = htons(2000); /* any port over 1024 will do... */
798 	if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0
799 	    || connect(udp, (struct sockaddr *) &addr, sizeof(addr)) < 0
800 	    || getsockname(udp, (struct sockaddr *) &addr, &addrlen) < 0) {
801 	    log(LOG_WARNING, errno, "Determining local address");
802 	    close(udp);
803 	    return;
804 	}
805 	close(udp);
806 	us = addr.sin_addr.s_addr;
807     } else			/* query sent to us alone */
808 	us = dst;
809 
810     p = (u_char *) (send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
811     datalen = 0;
812 
813     for (vifi = 0, v = uvifs; vifi < numvifs; vifi++, v++) {
814 	u_short vflags = v->uv_flags;
815 	u_char rflags = 0;
816 	if (vflags & VIFF_TUNNEL)
817 	    rflags |= DVMRP_NF_TUNNEL;
818 	if (vflags & VIFF_SRCRT)
819 	    rflags |= DVMRP_NF_SRCRT;
820 	if (vflags & VIFF_DOWN)
821 	    rflags |= DVMRP_NF_DOWN;
822 	if (vflags & VIFF_DISABLED)
823 	    rflags |= DVMRP_NF_DISABLED;
824 	if (vflags & VIFF_QUERIER)
825 	    rflags |= DVMRP_NF_QUERIER;
826 	if (vflags & VIFF_LEAF)
827 	    rflags |= DVMRP_NF_LEAF;
828 	ncount = 0;
829 	la = v->uv_neighbors;
830 	if (la == NULL) {
831 	    /*
832 	     * include down & disabled interfaces and interfaces on
833 	     * leaf nets.
834 	     */
835 	    if (rflags & DVMRP_NF_TUNNEL)
836 		rflags |= DVMRP_NF_DOWN;
837 	    if (datalen > MAX_DVMRP_DATA_LEN - 12) {
838 		send_igmp(us, them, IGMP_DVMRP, DVMRP_NEIGHBORS2,
839 			  htonl(MROUTED_LEVEL), datalen);
840 		p = (u_char *) (send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
841 		datalen = 0;
842 	    }
843 	    *(u_int*)p = v->uv_lcl_addr;
844 	    p += 4;
845 	    *p++ = v->uv_metric;
846 	    *p++ = v->uv_threshold;
847 	    *p++ = rflags;
848 	    *p++ = 1;
849 	    *(u_int*)p =  v->uv_rmt_addr;
850 	    p += 4;
851 	    datalen += 12;
852 	} else {
853 	    for ( ; la; la = la->al_next) {
854 		/* Make sure that there's room for this neighbor... */
855 		if (datalen + (ncount == 0 ? 4+4+4 : 4) > MAX_DVMRP_DATA_LEN) {
856 		    send_igmp(us, them, IGMP_DVMRP, DVMRP_NEIGHBORS2,
857 			      htonl(MROUTED_LEVEL), datalen);
858 		    p = (u_char *) (send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
859 		    datalen = 0;
860 		    ncount = 0;
861 		}
862 		/* Put out the header for this neighbor list... */
863 		if (ncount == 0) {
864 		    *(u_int*)p = v->uv_lcl_addr;
865 		    p += 4;
866 		    *p++ = v->uv_metric;
867 		    *p++ = v->uv_threshold;
868 		    *p++ = rflags;
869 		    ncount = p;
870 		    *p++ = 0;
871 		    datalen += 4 + 4;
872 		}
873 		*(u_int*)p = la->al_addr;
874 		p += 4;
875 		datalen += 4;
876 		(*ncount)++;
877 	    }
878 	}
879     }
880     if (datalen != 0)
881 	send_igmp(us, them, IGMP_DVMRP, DVMRP_NEIGHBORS2, htonl(MROUTED_LEVEL),
882 		  datalen);
883 }
884 
885 void
886 accept_info_request(u_int32_t src, u_int32_t dst, u_char *p, int datalen)
887 {
888     u_char *q;
889     int len;
890     int outlen = 0;
891 
892     q = (u_char *) (send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
893 
894     /* To be general, this must deal properly with breaking up over-sized
895      * packets.  That implies passing a length to each function, and
896      * allowing each function to request to be called again.  Right now,
897      * we're only implementing the one thing we are positive will fit into
898      * a single packet, so we wimp out.
899      */
900     while (datalen > 0) {
901 	len = 0;
902 	switch (*p) {
903 	    case DVMRP_INFO_VERSION:
904 		len = info_version(q);
905 		break;
906 
907 	    case DVMRP_INFO_NEIGHBORS:
908 	    default:
909 		log(LOG_INFO, 0, "ignoring unknown info type %d", *p);
910 		break;
911 	}
912 	*(q+1) = len++;
913 	outlen += len * 4;
914 	q += len * 4;
915 	len = (*(p+1) + 1) * 4;
916 	p += len;
917 	datalen -= len;
918     }
919 
920     if (outlen != 0)
921 	send_igmp(INADDR_ANY, src, IGMP_DVMRP, DVMRP_INFO_REPLY,
922 			htonl(MROUTED_LEVEL), outlen);
923 }
924 
925 /*
926  * Information response -- return version string
927  */
928 static int
929 info_version(char *p)
930 {
931     int len;
932     extern char versionstring[];
933 
934     *p++ = DVMRP_INFO_VERSION;
935     p++;	/* skip over length */
936     *p++ = 0;	/* zero out */
937     *p++ = 0;	/* reserved fields */
938     strcpy(p, versionstring);	/* XXX strncpy!!! */
939 
940     len = strlen(versionstring);
941     return ((len + 3) / 4);
942 }
943 
944 /*
945  * Process an incoming neighbor-list message.
946  */
947 void
948 accept_neighbors(u_int32_t src, u_int32_t dst, u_char *p, int datalen,
949 		 u_int32_t level)
950 {
951     log(LOG_INFO, 0, "ignoring spurious DVMRP neighbor list from %s to %s",
952 	inet_fmt(src, s1), inet_fmt(dst, s2));
953 }
954 
955 
956 /*
957  * Process an incoming neighbor-list message.
958  */
959 void
960 accept_neighbors2(u_int32_t src, u_int32_t dst, u_char *p, int datalen,
961 		  u_int32_t level)
962 {
963     log(LOG_INFO, 0, "ignoring spurious DVMRP neighbor list2 from %s to %s",
964 	inet_fmt(src, s1), inet_fmt(dst, s2));
965 }
966 
967 /*
968  * Process an incoming info reply message.
969  */
970 void
971 accept_info_reply(u_int32_t src, u_int32_t dst, u_char *p, int datalen)
972 {
973     log(LOG_INFO, 0, "ignoring spurious DVMRP info reply from %s to %s",
974 	inet_fmt(src, s1), inet_fmt(dst, s2));
975 }
976 
977 
978 /*
979  * Update the neighbor entry for neighbor 'addr' on vif 'vifi'.
980  * 'msgtype' is the type of DVMRP message received from the neighbor.
981  * Return TRUE if 'addr' is a valid neighbor, FALSE otherwise.
982  */
983 int
984 update_neighbor(vifi_t vifi, u_int32_t addr, int msgtype, char *p, int datalen, u_int32_t level)
985 {
986     struct uvif *v;
987     struct listaddr *n;
988     u_int32_t genid = 0;
989     u_int32_t router;
990     u_int32_t send_tables = 0;
991     int do_reset = FALSE;
992     int nflags;
993 
994     v = &uvifs[vifi];
995     nflags = (level >> 16) & 0xff;
996 
997     /*
998      * Confirm that 'addr' is a valid neighbor address on vif 'vifi'.
999      * IT IS ASSUMED that this was preceded by a call to find_vif(), which
1000      * checks that 'addr' is either a valid remote tunnel endpoint or a
1001      * non-broadcast address belonging to a directly-connected subnet.
1002      * Therefore, here we check only that 'addr' is not our own address
1003      * (due to an impostor or erroneous loopback) or an address of the form
1004      * {subnet,0} ("the unknown host").  These checks are not performed in
1005      * find_vif() because those types of address are acceptable for some
1006      * types of IGMP message (such as group membership reports).
1007      */
1008     if (!(v->uv_flags & VIFF_TUNNEL) &&
1009 	(addr == v->uv_lcl_addr ||
1010 	 addr == v->uv_subnet )) {
1011 	log(LOG_WARNING, 0,
1012 	    "received DVMRP message from 'the unknown host' or self: %s",
1013 	    inet_fmt(addr, s1));
1014 	return (FALSE);
1015     }
1016 
1017     /*
1018      * Look for addr in list of neighbors.
1019      */
1020     for (n = v->uv_neighbors; n != NULL; n = n->al_next) {
1021 	if (addr == n->al_addr) {
1022 	    break;
1023 	}
1024     }
1025 
1026     /*
1027      * Found it.  Reset its timer, and check for a version change
1028      */
1029     if (n) {
1030 	n->al_timer = 0;
1031 
1032 	/*
1033 	 * update the neighbors version and protocol number
1034 	 * if changed => router went down and came up,
1035 	 * so take action immediately.
1036 	 */
1037 	if ((n->al_pv != (level & 0xff)) ||
1038 	    (n->al_mv != ((level >> 8) & 0xff))) {
1039 
1040 	    do_reset = TRUE;
1041 	    log(LOG_DEBUG, 0,
1042 		"version change neighbor %s [old:%d.%d, new:%d.%d]",
1043 		inet_fmt(addr, s1),
1044 		n->al_pv, n->al_mv, level&0xff, (level >> 8) & 0xff);
1045 
1046 	    n->al_pv = level & 0xff;
1047 	    n->al_mv = (level >> 8) & 0xff;
1048 	}
1049     } else {
1050 	/*
1051 	 * If not found, add it to the list.  If the neighbor has a lower
1052 	 * IP address than me, yield querier duties to it.
1053 	 */
1054 	log(LOG_DEBUG, 0, "New neighbor %s on vif %d v%d.%d nf 0x%02x",
1055 	    inet_fmt(addr, s1), vifi, level & 0xff, (level >> 8) & 0xff,
1056 	    (level >> 16) & 0xff);
1057 
1058 	n = (struct listaddr *)malloc(sizeof(struct listaddr));
1059 	if (n == NULL)
1060 	    log(LOG_ERR, 0, "ran out of memory");    /* fatal */
1061 
1062 	n->al_addr      = addr;
1063 	n->al_pv	= level & 0xff;
1064 	n->al_mv	= (level >> 8) & 0xff;
1065 	n->al_genid	= 0;
1066 
1067 	time(&n->al_ctime);
1068 	n->al_timer     = 0;
1069 	n->al_next      = v->uv_neighbors;
1070 
1071 	/*
1072 	 * If we thought that we had no neighbors on this vif, send a route
1073 	 * report to the vif.  If this is just a new neighbor on the same
1074 	 * vif, send the route report just to the new neighbor.
1075 	 */
1076 	if (v->uv_neighbors == NULL) {
1077 	    send_tables = (v->uv_flags & VIFF_TUNNEL) ? addr : dvmrp_group;
1078 	    vifs_with_neighbors++;
1079 	} else {
1080 	    send_tables = addr;
1081 	}
1082 
1083 	v->uv_neighbors = n;
1084 
1085 	if (!(v->uv_flags & VIFF_TUNNEL) &&
1086 	    ntohl(addr) < ntohl(v->uv_lcl_addr))
1087 	    v->uv_flags &= ~VIFF_QUERIER;
1088     }
1089 
1090     /*
1091      * Check if the router gen-ids are the same.
1092      * Need to reset the prune state of the router if not.
1093      * Also check for one-way interfaces by seeing if we are in our
1094      * neighbor's list of known routers.
1095      */
1096     if (msgtype == DVMRP_PROBE) {
1097 
1098 	/* Check genid neighbor flag.  Also check version number; 3.3 and
1099 	 * 3.4 didn't set this flag. */
1100 	if ((((level >> 16) & 0xff) & NF_GENID) ||
1101 	    (((level & 0xff) == 3) && (((level >> 8) & 0xff) > 2))) {
1102 
1103 	    int i;
1104 
1105 	    if (datalen < 4) {
1106 		log(LOG_WARNING, 0,
1107 		    "received truncated probe message from %s (len %d)",
1108 		    inet_fmt(addr, s1), datalen);
1109 		return (FALSE);
1110 	    }
1111 
1112 	    for (i = 0; i < 4; i++)
1113 	      ((char *)&genid)[i] = *p++;
1114 	    datalen -= 4;
1115 
1116 	    if (n->al_genid == 0)
1117 		n->al_genid = genid;
1118 	    else if (n->al_genid != genid) {
1119 		log(LOG_DEBUG, 0,
1120 		    "new genid neigbor %s on vif %d [old:%x, new:%x]",
1121 		    inet_fmt(addr, s1), vifi, n->al_genid, genid);
1122 
1123 		n->al_genid = genid;
1124 		do_reset = TRUE;
1125 	    }
1126 
1127 	    /*
1128 	     * loop through router list and check for one-way ifs.
1129 	     */
1130 
1131 	    v->uv_flags |= VIFF_ONEWAY;
1132 
1133 	    while (datalen > 0) {
1134 		if (datalen < 4) {
1135 		    log(LOG_WARNING, 0,
1136 			"received truncated probe message from %s (len %d)",
1137 			inet_fmt(addr, s1), datalen);
1138 		    return (FALSE);
1139 		}
1140 		for (i = 0; i < 4; i++)
1141 		  ((char *)&router)[i] = *p++;
1142 		datalen -= 4;
1143 		if (router == v->uv_lcl_addr) {
1144 		    v->uv_flags &= ~VIFF_ONEWAY;
1145 		    break;
1146 		}
1147 	    }
1148 	}
1149     }
1150     if (n->al_flags != nflags) {
1151 	n->al_flags = nflags;
1152 
1153 	if (n->al_flags & NF_LEAF) {
1154 	    /*XXX If we have non-leaf neighbors then we know we shouldn't
1155 	     * mark this vif as a leaf.  For now we just count on other
1156 	     * probes and/or reports resetting the timer. */
1157 	    if (!v->uv_leaf_timer)
1158 		v->uv_leaf_timer = LEAF_CONFIRMATION_TIME;
1159 	} else {
1160 	    /* If we get a leaf to non-leaf transition, we *must* update
1161 	     * the routing table. */
1162 	    if (v->uv_flags & VIFF_LEAF && send_tables == 0)
1163 		send_tables = addr;
1164 	    v->uv_flags &= ~VIFF_LEAF;
1165 	    v->uv_leaf_timer = 0;
1166 	}
1167     }
1168     if (do_reset) {
1169 	reset_neighbor_state(vifi, addr);
1170 	if (!send_tables)
1171 	    send_tables = addr;
1172     }
1173     if (send_tables)
1174 	report(ALL_ROUTES, vifi, send_tables);
1175 
1176     return (TRUE);
1177 }
1178 
1179 
1180 /*
1181  * On every timer interrupt, advance the timer in each neighbor and
1182  * group entry on every vif.
1183  */
1184 void
1185 age_vifs(void)
1186 {
1187     vifi_t vifi;
1188     struct uvif *v;
1189     struct listaddr *a, *prev_a, *n;
1190     u_int32_t addr;
1191 
1192     for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v ) {
1193 	if (v->uv_leaf_timer && (v->uv_leaf_timer -= TIMER_INTERVAL == 0)) {
1194 		v->uv_flags |= VIFF_LEAF;
1195 	}
1196 
1197 	for (prev_a = (struct listaddr *)&(v->uv_neighbors),
1198 	     a = v->uv_neighbors;
1199 	     a != NULL;
1200 	     prev_a = a, a = a->al_next) {
1201 
1202 	    if ((a->al_timer += TIMER_INTERVAL) < NEIGHBOR_EXPIRE_TIME)
1203 		continue;
1204 
1205 	    /*
1206 	     * Neighbor has expired; delete it from the neighbor list,
1207 	     * delete it from the 'dominants' and 'subordinates arrays of
1208 	     * any route entries and assume querier duties unless there is
1209 	     * another neighbor with a lower IP address than mine.
1210 	     */
1211 	    addr = a->al_addr;
1212 	    prev_a->al_next = a->al_next;
1213 	    free((char *)a);
1214 	    a = prev_a;
1215 
1216 	    delete_neighbor_from_routes(addr, vifi);
1217 
1218 	    if (v->uv_neighbors == NULL)
1219 		vifs_with_neighbors--;
1220 
1221 	    v->uv_leaf_timer = LEAF_CONFIRMATION_TIME;
1222 
1223 	    if (!(v->uv_flags & VIFF_TUNNEL)) {
1224 		v->uv_flags |= VIFF_QUERIER;
1225 		for (n = v->uv_neighbors; n != NULL; n = n->al_next) {
1226 		    if (ntohl(n->al_addr) < ntohl(v->uv_lcl_addr)) {
1227 			v->uv_flags &= ~VIFF_QUERIER;
1228 		    }
1229 		    if (!(n->al_flags & NF_LEAF)) {
1230 			v->uv_leaf_timer = 0;
1231 		    }
1232 		}
1233 	    }
1234 	}
1235     }
1236 }
1237 
1238 /*
1239  * Returns the neighbor info struct for a given neighbor
1240  */
1241 struct listaddr *
1242 neighbor_info(vifi_t vifi, u_int32_t addr)
1243 {
1244     struct listaddr *u;
1245 
1246     for (u = uvifs[vifi].uv_neighbors; u; u = u->al_next)
1247 	if (u->al_addr == addr)
1248 	    return u;
1249 
1250     return NULL;
1251 }
1252 
1253 /*
1254  * Print the contents of the uvifs array on file 'fp'.
1255  */
1256 void
1257 dump_vifs(FILE *fp)
1258 {
1259     vifi_t vifi;
1260     struct uvif *v;
1261     struct listaddr *a;
1262     struct phaddr *p;
1263     struct sioc_vif_req v_req;
1264 
1265     fprintf(fp, "vifs_with_neighbors = %d\n", vifs_with_neighbors);
1266 
1267     if (vifs_with_neighbors == 1)
1268 	fprintf(fp,"[This host is a leaf]\n\n");
1269 
1270     fprintf(fp,
1271     "\nVirtual Interface Table\n%s",
1272     "Vif  Name  Local-Address                               ");
1273     fprintf(fp,
1274     "M  Thr  Rate   Flags\n");
1275 
1276     for (vifi = 0, v = uvifs; vifi < numvifs; vifi++, v++) {
1277 
1278 	fprintf(fp, "%2u %6s  %-15s %6s: %-18s %2u %3u  %5u  ",
1279 		vifi,
1280 		v->uv_name,
1281 		inet_fmt(v->uv_lcl_addr, s1),
1282 		(v->uv_flags & VIFF_TUNNEL) ?
1283 			"tunnel":
1284 			"subnet",
1285 		(v->uv_flags & VIFF_TUNNEL) ?
1286 			inet_fmt(v->uv_rmt_addr, s2) :
1287 			inet_fmts(v->uv_subnet, v->uv_subnetmask, s3),
1288 		v->uv_metric,
1289 		v->uv_threshold,
1290 		v->uv_rate_limit);
1291 
1292 	if (v->uv_flags & VIFF_ONEWAY)   fprintf(fp, " one-way");
1293 	if (v->uv_flags & VIFF_DOWN)     fprintf(fp, " down");
1294 	if (v->uv_flags & VIFF_DISABLED) fprintf(fp, " disabled");
1295 	if (v->uv_flags & VIFF_QUERIER)  fprintf(fp, " querier");
1296 	if (v->uv_flags & VIFF_SRCRT)    fprintf(fp, " src-rt");
1297 	if (v->uv_flags & VIFF_LEAF)	 fprintf(fp, " leaf");
1298 	if (v->uv_flags & VIFF_IGMPV1)	 fprintf(fp, " IGMPv1");
1299 	fprintf(fp, "\n");
1300 
1301 	if (v->uv_addrs != NULL) {
1302 	    fprintf(fp, "                alternate subnets: %s\n",
1303 		    inet_fmts(v->uv_addrs->pa_subnet, v->uv_addrs->pa_subnetmask, s1));
1304 	    for (p = v->uv_addrs->pa_next; p; p = p->pa_next) {
1305 		fprintf(fp, "                                   %s\n",
1306 			inet_fmts(p->pa_subnet, p->pa_subnetmask, s1));
1307 	    }
1308 	}
1309 
1310 	if (v->uv_neighbors != NULL) {
1311 	    fprintf(fp, "                            peers: %s (%d.%d) (0x%x)\n",
1312 		    inet_fmt(v->uv_neighbors->al_addr, s1),
1313 		    v->uv_neighbors->al_pv, v->uv_neighbors->al_mv,
1314 		    v->uv_neighbors->al_flags);
1315 	    for (a = v->uv_neighbors->al_next; a != NULL; a = a->al_next) {
1316 		fprintf(fp, "                                   %s (%d.%d) (0x%x)\n",
1317 			inet_fmt(a->al_addr, s1), a->al_pv, a->al_mv,
1318 			a->al_flags);
1319 	    }
1320 	}
1321 
1322 	if (v->uv_groups != NULL) {
1323 	    fprintf(fp, "                           groups: %-15s\n",
1324 		    inet_fmt(v->uv_groups->al_addr, s1));
1325 	    for (a = v->uv_groups->al_next; a != NULL; a = a->al_next) {
1326 		fprintf(fp, "                                   %-15s\n",
1327 			inet_fmt(a->al_addr, s1));
1328 	    }
1329 	}
1330 	if (v->uv_acl != NULL) {
1331 	    struct vif_acl *acl;
1332 
1333 	    fprintf(fp, "                       boundaries: %-18s\n",
1334 		    inet_fmts(v->uv_acl->acl_addr, v->uv_acl->acl_mask, s1));
1335 	    for (acl = v->uv_acl->acl_next; acl != NULL; acl = acl->acl_next) {
1336 		fprintf(fp, "                                 : %-18s\n",
1337 			inet_fmts(acl->acl_addr, acl->acl_mask, s1));
1338 	    }
1339 	}
1340 	v_req.vifi = vifi;
1341 	if (ioctl(igmp_socket, SIOCGETVIFCNT, (char *)&v_req) < 0) {
1342 	    log(LOG_WARNING, 0,
1343 		"SIOCGETVIFCNT fails");
1344 	}
1345 	else {
1346 	    fprintf(fp, "                         pkts in : %ld\n",
1347 		    v_req.icount);
1348 	    fprintf(fp, "                         pkts out: %ld\n",
1349 		    v_req.ocount);
1350 	}
1351 	fprintf(fp, "\n");
1352     }
1353     fprintf(fp, "\n");
1354 }
1355 
1356 /*
1357  * Time out record of a group membership on a vif
1358  */
1359 static void
1360 DelVif(void *arg)
1361 {
1362     cbk_t *cbk = (cbk_t *)arg;
1363     vifi_t vifi = cbk->vifi;
1364     struct uvif *v = &uvifs[vifi];
1365     struct listaddr *a, **anp, *g = cbk->g;
1366 
1367     /*
1368      * Group has expired
1369      * delete all kernel cache entries with this group
1370      */
1371     if (g->al_query)
1372 	DeleteTimer(g->al_query);
1373 
1374     delete_lclgrp(vifi, g->al_addr);
1375 
1376     anp = &(v->uv_groups);
1377     while ((a = *anp) != NULL) {
1378     	if (a == g) {
1379 	    *anp = a->al_next;
1380 	    free((char *)a);
1381 	} else {
1382 	    anp = &a->al_next;
1383 	}
1384     }
1385 
1386     free(cbk);
1387 }
1388 
1389 /*
1390  * Set a timer to delete the record of a group membership on a vif.
1391  */
1392 static int
1393 SetTimer(vifi_t vifi, struct listaddr *g)
1394 {
1395     cbk_t *cbk;
1396 
1397     cbk = (cbk_t *) malloc(sizeof(cbk_t));
1398     cbk->g = g;
1399     cbk->vifi = vifi;
1400     return timer_setTimer(g->al_timer, (cfunc_t)DelVif, (void *)cbk);
1401 }
1402 
1403 /*
1404  * Delete a timer that was set above.
1405  */
1406 static int
1407 DeleteTimer(int id)
1408 {
1409     timer_clearTimer(id);
1410     return 0;
1411 }
1412 
1413 /*
1414  * Send a group-specific query.
1415  */
1416 static void
1417 SendQuery(void *arg)
1418 {
1419     cbk_t *cbk = (cbk_t *)arg;
1420     struct uvif *v = &uvifs[cbk->vifi];
1421 
1422     send_igmp(v->uv_lcl_addr, cbk->g->al_addr,
1423 	      IGMP_HOST_MEMBERSHIP_QUERY,
1424 	      cbk->q_time, cbk->g->al_addr, 0);
1425     cbk->g->al_query = 0;
1426     free(cbk);
1427 }
1428 
1429 /*
1430  * Set a timer to send a group-specific query.
1431  */
1432 static int
1433 SetQueryTimer(struct listaddr *g, vifi_t vifi, int to_expire, int q_time)
1434 {
1435     cbk_t *cbk;
1436 
1437     cbk = (cbk_t *) malloc(sizeof(cbk_t));
1438     cbk->g = g;
1439     cbk->q_time = q_time;
1440     cbk->vifi = vifi;
1441     return timer_setTimer(to_expire, (cfunc_t)SendQuery, (void *)cbk);
1442 }
1443