summaryrefslogtreecommitdiffstats
path: root/usr.sbin/pim6sd/vif.c
blob: 2664e46571b9f2ba54033f2a33d67f229d55a265 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
/*
 *  Copyright (c) 1998 by the University of Southern California.
 *  All rights reserved.
 *
 *  Permission to use, copy, modify, and distribute this software and
 *  its documentation in source and binary forms for lawful
 *  purposes and without fee is hereby granted, provided
 *  that the above copyright notice appear in all copies and that both
 *  the copyright notice and this permission notice appear in supporting
 *  documentation, and that any documentation, advertising materials,
 *  and other materials related to such distribution and use acknowledge
 *  that the software was developed by the University of Southern
 *  California and/or Information Sciences Institute.
 *  The name of the University of Southern California may not
 *  be used to endorse or promote products derived from this software
 *  without specific prior written permission.
 *
 *  THE UNIVERSITY OF SOUTHERN CALIFORNIA DOES NOT MAKE ANY REPRESENTATIONS
 *  ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE.  THIS SOFTWARE IS
 *  PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
 *  INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND
 *  NON-INFRINGEMENT.
 *
 *  IN NO EVENT SHALL USC, OR ANY OTHER CONTRIBUTOR BE LIABLE FOR ANY
 *  SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES, WHETHER IN CONTRACT,
 *  TORT, OR OTHER FORM OF ACTION, ARISING OUT OF OR IN CONNECTION WITH,
 *  THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 *  Other copyrights might apply to parts of this software and are so
 *  noted when applicable.
 */
/*
 *  Questions concerning this software should be directed to
 *  Mickael Hoerdt (hoerdt@clarinet.u-strasbg.fr) LSIIT Strasbourg.
 *
 */
/*
 * This program has been derived from pim6dd.        
 * The pim6dd program is covered by the license in the accompanying file
 * named "LICENSE.pim6dd".
 */
/*
 * This program has been derived from pimd.        
 * The pimd program is covered by the license in the accompanying file
 * named "LICENSE.pimd".
 *
 */
/*
 * Part of this program has been derived from mrouted.
 * The mrouted program is covered by the license in the accompanying file
 * named "LICENSE.mrouted".
 *
 * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
 * Leland Stanford Junior University.
 *
 * $FreeBSD$
 */

#include <sys/ioctl.h>
#include <errno.h>
#include <syslog.h>
#include <string.h>
#include <stdlib.h>
#include "vif.h"
#include "mld6.h"
#include "pim6.h"
#include "pimd.h"
#include "route.h"
#include "config.h"
#include "inet6.h"
#include "kern.h"
#include "mld6_proto.h"
#include "pim6_proto.h"
#include "mrt.h"
#include "debug.h"
#include "timer.h"

struct uvif	uvifs[MAXMIFS];	/*the list of virtualsinterfaces */
vifi_t numvifs;				/*total number of interface */
int vifs_down;
vifi_t reg_vif_num;		   /*register interface*/
int phys_vif; /* An enabled vif that has a global address */
int udp_socket;
int total_interfaces;
if_set			if_nullset;
if_set			if_result;

int init_reg_vif __P((void));
void start_all_vifs __P((void));
void start_vif __P((vifi_t vifi));
void stop_vif __P((vifi_t vivi));
int update_reg_vif __P((vifi_t register_vifi));

extern int cfparse __P((int, int));

void init_vifs()
{
	vifi_t vifi;
	struct uvif *v;
	int enabled_vifs;

	numvifs = 0;
	reg_vif_num = NO_VIF;

	/*
	 * Configure the vifs based on the interface configuration of
	 * the kernel and the contents of the configuration file.
	 * (Open a UDP socket for ioctl use in the config procedures if
	 * the kernel can't handle IOCTL's on the MLD socket.)
	 */
#ifdef IOCTL_OK_ON_RAW_SOCKET
	udp_socket = mld6_socket;
#else
	if ((udp_socket = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
		log(LOG_ERR, errno, "UDP6 socket");
#endif

	/* clean all the interfaces ... */

	for(vifi = 0,v=uvifs; vifi < MAXVIFS; ++ vifi, ++v)
	{
		memset(v,0,sizeof(*v)); /* everything is zeroed  => NULL , pointer NULL , addrANY ...) */
		v->uv_metric = DEFAULT_METRIC;
		v->uv_rate_limit = DEFAULT_PHY_RATE_LIMIT;
		strncpy(v->uv_name,"",IFNAMSIZ);
		v->uv_local_pref = default_source_preference;
		v->uv_local_metric = default_source_metric;
	}
	IF_DEBUG(DEBUG_IF)
		log(LOG_DEBUG,0,"Interfaces world initialized...");
	IF_DEBUG(DEBUG_IF)
		log(LOG_DEBUG,0,"Getting vifs from kernel");
	config_vifs_from_kernel();
	if (max_global_address() == NULL)
		log(LOG_ERR, 0, "There's no global address");
	IF_DEBUG(DEBUG_IF)
		log(LOG_DEBUG,0,"Getting vifs from %s",configfilename);

	/* read config from file */
	if (cfparse(1, 0) != 0)
		log(LOG_ERR, 0, "fatal error in parsing the config file");

	enabled_vifs = 0;
	phys_vif = -1;

	for( vifi = 0, v = uvifs ; vifi < numvifs ; ++ vifi,++v)
	{
		if(v->uv_flags & (VIFF_DISABLED | VIFF_DOWN | MIFF_REGISTER))
			continue;
		if(v->uv_linklocal == NULL)
			log(LOG_ERR,0,"there is no link-local address on vif %s",v->uv_name);
		if (phys_vif == -1) {
			struct phaddr *p;

			/*
			 * If this vif has a global address, set its id
			 * to phys_vif.
			 */
			for(p = v->uv_addrs; p; p = p->pa_next) {
				if (!IN6_IS_ADDR_LINKLOCAL(&p->pa_addr.sin6_addr) &&
				    !IN6_IS_ADDR_SITELOCAL(&p->pa_addr.sin6_addr)) {
					phys_vif = vifi;
					break;
				}
			}
		}
		enabled_vifs++;
	}
	if (enabled_vifs < 2)
		log(LOG_ERR,0,"can't forward: %s",
		enabled_vifs == 0 ? "no enabled vifs" : "only one enabled vif" );

	memset(&if_nullset,0,sizeof(if_nullset));
	k_init_pim(mld6_socket);	
	IF_DEBUG(DEBUG_PIM_DETAIL)
		log(LOG_DEBUG,0,"Pim kernel initialization done");


	/* Add a dummy virtual interface to support Registers in the kernel. */
 	init_reg_vif();

	start_all_vifs();

}
int init_reg_vif()
{
	struct uvif *v;
	vifi_t i;

	v = &uvifs[numvifs];
	if (( numvifs+1 ) == MAXVIFS )
	{
     /* Exit the program! The PIM router must have a Register vif */
    log(LOG_ERR, 0,
        "cannot install the Register vif: too many interfaces");
    /* To make lint happy */
    return (FALSE);
	}

    /*
     * So far in PIM we need only one register vif and we save its number in
     * the global reg_vif_num.
     */


	reg_vif_num = numvifs;

    /* Use the address of the first available physical interface to
     * create the register vif.
     */

	for(i =0 ; i < numvifs ; i++)
	{
		if(uvifs[i].uv_flags & (VIFF_DOWN | VIFF_DISABLED | MIFF_REGISTER))
			continue;
	else
		break;
	}
	if( i >= numvifs)
	{
		  log(LOG_ERR, 0, "No physical interface enabled");
    	return -1;
    }

	
	memcpy(v,&uvifs[i],sizeof(*v));
	strncpy(v->uv_name,"register_mif0",IFNAMSIZ);
	v->uv_flags = MIFF_REGISTER;

#ifdef PIM_EXPERIMENTAL
	v->uv_flags |= MIFF_REGISTER_KERNEL_ENCAP;
#endif

    IF_DEBUG(DEBUG_IF)
		log(LOG_DEBUG,0,"Interface %s (subnet %s) ,installed on vif #%u - rate = %d",
    	v->uv_name,net6name(&v->uv_prefix.sin6_addr,&v->uv_subnetmask),
    	reg_vif_num,v->uv_rate_limit);

	numvifs++;
	total_interfaces++;
	return 0;	
}

void start_all_vifs()
{
	vifi_t vifi;
	struct uvif *v;
	u_int action;


   /* Start first the NON-REGISTER vifs */

	for(action=0; ;action = MIFF_REGISTER )
	{
		for(vifi= 0,v = uvifs;vifi < numvifs ; ++vifi, ++v)
		{
			if (( v->uv_flags & MIFF_REGISTER ) ^ action )
        /* If starting non-registers but the vif is a register
         * or if starting registers, but the interface is not
         * a register, then just continue.
         */
				continue;

			if ( v->uv_flags & (VIFF_DISABLED | VIFF_DOWN ))
			{
				IF_DEBUG(DEBUG_IF)
				{
					if ( v-> uv_flags & VIFF_DISABLED)
						log(LOG_DEBUG,0,"%s is DISABLED ; vif #%u out of service",v->uv_name,vifi); 
					else
						log(LOG_DEBUG,0,"%s is DOWN ; vif #%u out of service",v->uv_name,vifi);
				}	
			}
			else
				start_vif(vifi);
		}
		if ( action == MIFF_REGISTER)
			break;
	}	
}

/*
 * Initialize the vif and add to the kernel. The vif can be either
 * physical, register or tunnel (tunnels will be used in the future
 * when this code becomes PIM multicast boarder router.
 */


void start_vif (vifi_t vifi)
{
	struct uvif *v;

	v = &uvifs[vifi];

	/* Initialy no router on any vif */

	if( v-> uv_flags & MIFF_REGISTER)
		v->uv_flags = v->uv_flags & ~VIFF_DOWN;
	else
	{
		v->uv_flags = (v->uv_flags | VIFF_DR | VIFF_NONBRS) & ~ VIFF_DOWN;
		v->uv_pim_hello_timer = 1 + RANDOM() % pim_hello_period;
		v->uv_jp_timer = 1 + RANDOM() % pim_join_prune_period;
	}

	/* Tell kernel to add, i.e. start this vif */

	k_add_vif(mld6_socket,vifi,&uvifs[vifi]);
	IF_DEBUG(DEBUG_IF)
		log(LOG_DEBUG,0,"%s comes up ,vif #%u now in service",v->uv_name,vifi);

	if (!(v->uv_flags & MIFF_REGISTER)) {
	    /*
	     * Join the PIM multicast group on the interface.
	     */
	    k_join(mld6_socket, &allpim6routers_group.sin6_addr,
		   v->uv_ifindex);

	    /*
	     * Join the ALL-ROUTERS multicast group on the interface.
	     * This allows mtrace requests to loop back if they are run
	     * on the multicast router.this allow receiving mld6 messages too.
	     */
	    k_join(mld6_socket, &allrouters_group.sin6_addr, v->uv_ifindex);

	    /*
	     * Until neighbors are discovered, assume responsibility for sending
	     * periodic group membership queries to the subnet.  Send the first
	     * query.
	     */
	    v->uv_flags |= VIFF_QUERIER;
	    if (!v->uv_querier) {
		v->uv_querier = (struct listaddr *)malloc(sizeof(struct listaddr));
		memset(v->uv_querier, 0, sizeof(struct listaddr));
	    }
	    v->uv_querier->al_addr = v->uv_linklocal->pa_addr;
	    v->uv_querier->al_timer = MLD6_OTHER_QUERIER_PRESENT_INTERVAL;
	    time(&v->uv_querier->al_ctime); /* reset timestamp */
	    query_groups(v);
  
	    /*
	     * Send a probe via the new vif to look for neighbors.
	     */
	    send_pim6_hello(v, pim_hello_holdtime);
	}
}

/*
 * Stop a vif (either physical interface, tunnel or
 * register.) If we are running only PIM we don't have tunnels.
 */ 


void stop_vif( vifi_t vifi )
{
	struct uvif *v;
	struct listaddr *a;
	register pim_nbr_entry_t *n;
	register pim_nbr_entry_t *next;
	struct vif_acl *acl;

 
    /*
     * TODO: make sure that the kernel viftable is
     * consistent with the daemon table
     */

	v=&uvifs[vifi];
	if( !( v->uv_flags&MIFF_REGISTER ) )
	{
		k_leave( mld6_socket , &allpim6routers_group.sin6_addr , v->uv_ifindex );
		k_leave( mld6_socket , &allrouters_group.sin6_addr , v->uv_ifindex );
    /*
     * Discard all group addresses.  (No need to tell kernel;
     * the k_del_vif() call will clean up kernel state.)
     */

		while( v->uv_groups!=NULL )
		{
			a=v->uv_groups;
			v->uv_groups=a->al_next;
			free((char *)a);
		}
	}

    /*
     * TODO: inform (eventually) the neighbors I am going down by sending
     * PIM_HELLO with holdtime=0 so someone else should become a DR.
     */
    /* TODO: dummy! Implement it!! Any problems if don't use it? */
    delete_vif_from_mrt(vifi);

    /*
     * Delete the interface from the kernel's vif structure.
     */

	k_del_vif( mld6_socket , vifi );
	v->uv_flags=(v->uv_flags & ~VIFF_DR & ~VIFF_QUERIER & ~VIFF_NONBRS) | VIFF_DOWN;
	if( !(v->uv_flags & MIFF_REGISTER ))
	{
    	RESET_TIMER(v->uv_pim_hello_timer);
    	RESET_TIMER(v->uv_jp_timer);
    	RESET_TIMER(v->uv_gq_timer);

		for( n=v->uv_pim_neighbors ; n!=NULL ; n = next )
		{
			next=n->next;			/* Free the space for each neighbour */
			free((char *)n);
		}
		v->uv_pim_neighbors=NULL;
	}

	
   /* TODO: currently not used */
   /* The Access Control List (list with the scoped addresses) */

	while( v->uv_acl!=NULL )
	{
		acl=v->uv_acl;
		v->uv_acl=acl->acl_next;
		free((char *)acl);
	}

	vifs_down=TRUE;

	IF_DEBUG(DEBUG_IF)
		log( LOG_DEBUG ,0,"%s goes down , vif #%u out of service" , v->uv_name , vifi);
}

/*
 * Update the register vif in the multicast routing daemon and the
 * kernel because the interface used initially to get its local address
 * is DOWN. register_vifi is the index to the Register vif which needs
 * to be updated. As a result the Register vif has a new uv_lcl_addr and
 * is UP (virtually :))
 */
int
update_reg_vif( vifi_t register_vifi )
{
    register struct uvif *v;
    register vifi_t vifi;

    /* Find the first useable vif with solid physical background */
    for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
	if (v->uv_flags & (VIFF_DISABLED | VIFF_DOWN | VIFF_TUNNEL
			   | MIFF_REGISTER))
	    continue;
        /* Found. Stop the bogus Register vif first */
	stop_vif(register_vifi);
	uvifs[register_vifi].uv_linklocal->pa_addr =
	    uvifs[vifi].uv_linklocal->pa_addr;
	start_vif(register_vifi);
	IF_DEBUG(DEBUG_PIM_REGISTER | DEBUG_IF)
	    log(LOG_NOTICE, 0, "%s has come up; vif #%u now in service",
		uvifs[register_vifi].uv_name, register_vifi);
	return 0;
    }
    vifs_down = TRUE;
    log(LOG_WARNING, 0, "Cannot start Register vif: %s",
	uvifs[vifi].uv_name);
    return(-1);
}

/*
 * return the max global Ipv6 address of an UP and ENABLED interface
 * other than the MIFF_REGISTER interface.
*/
struct sockaddr_in6 *
max_global_address()
{
	vifi_t vifi;
	struct uvif *v;
	struct phaddr *p;
	struct phaddr *pmax = NULL;

	for(vifi=0,v=uvifs;vifi< numvifs;++vifi,++v)
	{
		if(v->uv_flags & (VIFF_DISABLED | VIFF_DOWN | MIFF_REGISTER))
			continue;
		/*
		 * take first the max global address of the interface
		 * (without link local) => aliasing
		 */
		for(p=v->uv_addrs;p!=NULL;p=p->pa_next)
		{
			/*
			 * If this is the first global address, take it anyway.
			 */
			if (pmax == NULL) {
				if (!IN6_IS_ADDR_LINKLOCAL(&p->pa_addr.sin6_addr) &&
				    !IN6_IS_ADDR_SITELOCAL(&p->pa_addr.sin6_addr))
					pmax = p;
			}
			else {
				if (inet6_lessthan(&pmax->pa_addr,
						   &p->pa_addr) &&
				    !IN6_IS_ADDR_LINKLOCAL(&p->pa_addr.sin6_addr) &&
				    !IN6_IS_ADDR_SITELOCAL(&p->pa_addr.sin6_addr))
					pmax=p;	
			}
		}
	}

	return(pmax ? &pmax->pa_addr : NULL);
}

struct sockaddr_in6 *
uv_global(vifi)
	vifi_t vifi;
{
	struct uvif *v = &uvifs[vifi];
	struct phaddr *p;

	for (p = v->uv_addrs; p; p = p->pa_next) {
		if (!IN6_IS_ADDR_LINKLOCAL(&p->pa_addr.sin6_addr) &&
		    !IN6_IS_ADDR_SITELOCAL(&p->pa_addr.sin6_addr))
			return(&p->pa_addr);
	}

	return(NULL);
}

/*
 * Check if the interface exists in the mif table. If true 
 * return the highest address of the interface else return NULL.
 */
struct sockaddr_in6 *
local_iface(char *ifname)
{
	register struct uvif *v;
	vifi_t vifi;
	struct phaddr *p;
	struct phaddr *pmax = NULL;

	for(vifi=0,v=uvifs;vifi<numvifs;++vifi,++v)
	{
		if (v->uv_flags & (VIFF_DISABLED | VIFF_DOWN | MIFF_REGISTER))
			continue;
		if(EQUAL(v->uv_name, ifname))
		{
			for(p=v->uv_addrs; p!=NULL; p=p->pa_next)
			{
				if (!IN6_IS_ADDR_LINKLOCAL(&p->pa_addr.sin6_addr)&&
				    !IN6_IS_ADDR_SITELOCAL(&p->pa_addr.sin6_addr)) {
					/*
					 * If this is the first global address
					 * or larger than the current MAX global
					 * address, remember it.
					 */
					if (pmax == NULL ||
					    inet6_lessthan(&pmax->pa_addr,
							   &p->pa_addr))
						pmax = p;
				}
			}
			if (pmax)
				return(&pmax->pa_addr);
		}
	}

	return NULL;
}

/*  
 * See if any interfaces have changed from up state to down, or vice versa,
 * including any non-multicast-capable interfaces that are in use as local
 * tunnel end-points.  Ignore interfaces that have been administratively
 * disabled.
 */     
void
check_vif_state()
{
    register vifi_t vifi;
    register struct uvif *v;
    struct ifreq ifr;
    static int checking_vifs=0;

    /*
     * XXX: TODO: True only for DVMRP?? Check.
     * If we get an error while checking, (e.g. two interfaces go down
     * at once, and we decide to send a prune out one of the failed ones)
     * then don't go into an infinite loop!
     */
    if( checking_vifs )
	return;

    vifs_down=FALSE;
    checking_vifs=TRUE;

    /* TODO: Check all potential interfaces!!! */
    /* Check the physical and tunnels only */
    for( vifi=0 , v=uvifs ; vifi<numvifs ; ++vifi , ++v )
    {
	if( v->uv_flags & ( VIFF_DISABLED|MIFF_REGISTER	) )
	    continue;

	strncpy( ifr.ifr_name , v->uv_name , IFNAMSIZ );
  
	/* get the interface flags */
	if( ioctl( udp_socket , SIOCGIFFLAGS , (char *)&ifr )<0 )
	    log(LOG_ERR, errno,
        	"check_vif_state: ioctl SIOCGIFFLAGS for %s", ifr.ifr_name);

	if( v->uv_flags & VIFF_DOWN )
	{
	    if ( ifr.ifr_flags & IFF_UP )
	    {
		start_vif( vifi );
	    }
	    else
		vifs_down=TRUE;
	}
	else
	{
	    if( !( ifr.ifr_flags & IFF_UP ))
	    {
		log( LOG_NOTICE ,0,
		     "%s has gone down ; vif #%u taken out of  service",
		     v->uv_name , vifi );
		stop_vif ( vifi );
		vifs_down = TRUE;
	    }
	}
    }

    /* Check the register(s) vif(s) */
    for( vifi=0 , v=uvifs ; vifi<numvifs ; ++vifi , ++v )
    {
	register vifi_t vifi2;
	register struct uvif *v2;
	int found;

	if( !(v->uv_flags & MIFF_REGISTER ) )
	    continue;
	else
	{
	    found=0;

	    /* Find a physical vif with the same IP address as the
	     * Register vif.
	     */
	    for( vifi2=0 , v2=uvifs ; vifi2<numvifs ; ++vifi2 , ++v2 )
	    {
		if( v2->uv_flags & ( VIFF_DISABLED|VIFF_DOWN|VIFF_TUNNEL|MIFF_REGISTER ))
		    continue;
		if( IN6_ARE_ADDR_EQUAL( &v->uv_linklocal->pa_addr.sin6_addr,
					&v2->uv_linklocal->pa_addr.sin6_addr ))
		{
		    found=1;
		    break;
		}
	    }
	    if(!found)
		/* The physical interface with the IP address as the Register
		 * vif is probably DOWN. Get a replacement.
		 */
		update_reg_vif( vifi );
	}
    }
    checking_vifs=0;
}

/*
 * If the source is directly connected to us, find the vif number for
 * the corresponding physical interface (tunnels excluded).
 * Local addresses are excluded.
 * Return the vif number or NO_VIF if not found.
 */

vifi_t
find_vif_direct(src)
    struct sockaddr_in6 *src;
{
    vifi_t vifi;
    register struct uvif *v;
    register struct phaddr *p;
   
    for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) 
    {
    	if (v->uv_flags & (VIFF_DISABLED | VIFF_DOWN | VIFF_TUNNEL|MIFF_REGISTER))
        	continue;
	for (p = v->uv_addrs; p; p = p->pa_next) 
	{
            if (inet6_equal(src, &p->pa_addr))
                return(NO_VIF);
            if (inet6_match_prefix(src, &p->pa_prefix, &p->pa_subnetmask))
            	return(vifi);
    	}
    }

    return (NO_VIF);
}

/*
 * Checks if src is local address. If "yes" return the vif index,
 * otherwise return value is NO_VIF.
 */

vifi_t
local_address(src)
    struct sockaddr_in6 *src;
{
    vifi_t vifi;
    register struct uvif *v;
    register struct phaddr *p;

    for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
	if (v->uv_flags & (VIFF_DISABLED | VIFF_DOWN | MIFF_REGISTER))
	    continue;
	for (p = v->uv_addrs; p; p = p->pa_next) {
	    if (inet6_equal(src, &p->pa_addr))
		return(vifi);
	}
    }
    /* Returning NO_VIF means not a local address */
    return (NO_VIF);
}


/*  
 * If the source is directly connected, or is local address,
 * find the vif number for the corresponding physical interface
 * (tunnels excluded).
 * Return the vif number or NO_VIF if not found.
 */ 

vifi_t
find_vif_direct_local(src)
    struct sockaddr_in6 *src;
{ 
    vifi_t vifi;
    register struct uvif *v; 
    register struct phaddr *p;
   

    for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
    	if (v->uv_flags & (VIFF_DISABLED | VIFF_DOWN | VIFF_TUNNEL |MIFF_REGISTER))
        	continue;
    	for (p = v->uv_addrs; p; p = p->pa_next) {
        	if (inet6_equal(src, &p->pa_addr) ||
            	inet6_match_prefix(src, &p->pa_prefix, &p->pa_subnetmask))  
        		return(vifi);
    	}
    }
    return (NO_VIF);
}

int
vif_forwarder(if_set *p1 , if_set *p2)
{
	int idx;

	for(idx=0 ; idx < sizeof(*p1)/sizeof(fd_mask) ; idx++)
	{
		if (p1->ifs_bits[idx] & p2->ifs_bits[idx])
			return(TRUE);
		
	}

	/* (p1 & p2) is empty. We're not the forwarder */
	return(FALSE);
}

if_set *
vif_and(if_set *p1 , if_set *p2, if_set *result)
{
	int idx;

	IF_ZERO(result);

	for(idx=0 ; idx < sizeof(*p1)/sizeof(fd_mask) ; idx++)
	{
		result->ifs_bits[idx] = p1->ifs_bits[idx] & p2->ifs_bits[idx];
	}

	return(result);
}

if_set *
vif_xor(if_set *p1 , if_set *p2, if_set *result)
{
	int idx;

	IF_ZERO(result);

	for(idx=0 ; idx < sizeof(*p1)/sizeof(fd_mask) ; idx++)
	{
		result->ifs_bits[idx] =
			p1->ifs_bits[idx] ^ p2->ifs_bits[idx];
	}

	return(result);
}
/*  
 * stop all vifs
 */ 
void
stop_all_vifs()
{
    vifi_t vifi;
    struct uvif *v;
 
    for (vifi = 0, v=uvifs; vifi < numvifs; ++vifi, ++v) {
	if (!(v->uv_flags &  VIFF_DOWN)) {
	    stop_vif(vifi);
	}
    }
}

struct uvif *
find_vif(ifname)
	char *ifname;
{
	struct uvif *v;
	vifi_t vifi;

	for (vifi = 0, v = uvifs; vifi < numvifs ; ++vifi , ++v) {
		if (strcasecmp(v->uv_name, ifname) == 0)
			return(v);
	}

	return(NULL);
}
OpenPOWER on IntegriCloud