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