summaryrefslogtreecommitdiffstats
path: root/usr.sbin/pim6dd/mld6_proto.c
blob: a87973d49e1ae2b40fa7b49fdb65826e2d5c0864 (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
/*
 * Copyright (C) 1998 WIDE Project.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 *  Copyright (c) 1998 by the University of Oregon.
 *  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 Oregon.
 *  The name of the University of Oregon may not be used to endorse or 
 *  promote products derived from this software without specific prior 
 *  written permission.
 *
 *  THE UNIVERSITY OF OREGON 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 UO, 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 
 *  Kurt Windisch (kurtw@antc.uoregon.edu)
 *
 *  $Id: mld6_proto.c,v 1.5 2000/10/05 22:20:38 itojun Exp $
 */
/*
 * Part of this program has been derived from PIM sparse-mode pimd.
 * The pimd program is covered by the license in the accompanying file
 * named "LICENSE.pimd".
 *  
 * The pimd program is COPYRIGHT 1998 by University of Southern California.
 *
 * 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 "defs.h"

extern struct in6_addr in6addr_any;

typedef struct {
	mifi_t  mifi;
	struct listaddr *g;
	int    q_time;
} cbk_t;


/*
 * Forward declarations.
 */
static void DelVif __P((void *arg));
static int SetTimer __P((int mifi, struct listaddr *g));
static int DeleteTimer __P((int id));
static void SendQuery __P((void *arg));
static int SetQueryTimer __P((struct listaddr *g, int mifi, int to_expire,
			      int q_time));

/*
 * Send group membership queries on that interface if I am querier.
 */
void
query_groups(v)
	register struct uvif *v;
{
	register struct listaddr *g;
    
	v->uv_gq_timer = MLD6_QUERY_INTERVAL;
	if (v->uv_flags & VIFF_QUERIER && (v->uv_flags & VIFF_NOLISTENER) == 0)
		send_mld6(MLD6_LISTENER_QUERY, 0, &v->uv_linklocal->pa_addr,
			  NULL, (struct in6_addr *)&in6addr_any,
			  v->uv_ifindex, MLD6_QUERY_RESPONSE_INTERVAL, 0, 1);

	/*
	 * Decrement the old-hosts-present timer for each
	 * active group on that vif.
	 */
	for (g = v->uv_groups; g != NULL; g = g->al_next)
		if (g->al_old > TIMER_INTERVAL)
			g->al_old -= TIMER_INTERVAL;
		else
			g->al_old = 0;
}


/*
 * Process an incoming host membership query
 */
void
accept_listener_query(src, dst, group, tmo)
	struct sockaddr_in6 *src;
	struct in6_addr *dst, *group;
	int tmo;
{
	register int mifi;
	register struct uvif *v;
	struct sockaddr_in6 group_sa = {sizeof(group_sa), AF_INET6};

	/* Ignore my own membership query */
	if (local_address(src) != NO_VIF)
		return;

	if ((mifi = find_vif_direct(src)) == NO_VIF) {
		IF_DEBUG(DEBUG_MLD)
			log(LOG_INFO, 0,
			    "accept_listener_query: can't find a mif");
		return;
	}

	v = &uvifs[mifi];

	if (v->uv_querier == NULL || !inet6_equal(&v->uv_querier->al_addr, src))
	{
		/*
		 * This might be:
		 * - A query from a new querier, with a lower source address
		 *   than the current querier (who might be me)
		 * - A query from a new router that just started up and doesn't
		 *   know who the querier is.
		 * - A query from the current querier
		 */
		if (inet6_lessthan(src, (v->uv_querier ? &v->uv_querier->al_addr
					 : &v->uv_linklocal->pa_addr))) {
			IF_DEBUG(DEBUG_MLD)
				log(LOG_DEBUG, 0, "new querier %s (was %s) "
				    "on mif %d",
				    inet6_fmt(&src->sin6_addr),
				    v->uv_querier ?
				    inet6_fmt(&v->uv_querier->al_addr.sin6_addr) :
				    "me", mifi);
			if (!v->uv_querier) {
				v->uv_querier = (struct listaddr *)
					malloc(sizeof(struct listaddr));
				memset(v->uv_querier, 0,
				       sizeof(struct listaddr));
			}
			v->uv_flags &= ~VIFF_QUERIER;
			v->uv_querier->al_addr = *src;
			time(&v->uv_querier->al_ctime);
		}
	}
    
	/*
	 * Reset the timer since we've received a query.
	 */
	if (v->uv_querier && inet6_equal(src, &v->uv_querier->al_addr))
		v->uv_querier->al_timer = MLD6_OTHER_QUERIER_PRESENT_INTERVAL;

	/*
	 * If this is a Group-Specific query which we did not source,
	 * we must set our membership timer to [Last Member Query Count] *
	 * the [Max Response Time] in the packet.
	 */
	if (!IN6_IS_ADDR_UNSPECIFIED(group) &&
	    inet6_equal(src, &v->uv_linklocal->pa_addr)) {
		register struct listaddr *g;

		IF_DEBUG(DEBUG_MLD)
			log(LOG_DEBUG, 0,
			    "%s for %s from %s on mif %d, timer %d",
			    "Group-specific membership query",
			    inet6_fmt(group),
			    inet6_fmt(&src->sin6_addr), mifi, tmo);

		group_sa.sin6_addr = *group;
		group_sa.sin6_scope_id = inet6_uvif2scopeid(&group_sa, v);
		for (g = v->uv_groups; g != NULL; g = g->al_next) {
			if (inet6_equal(&group_sa, &g->al_addr)
			    && g->al_query == 0) {
				/* setup a timeout to remove the group membership */
				if (g->al_timerid)
					g->al_timerid = DeleteTimer(g->al_timerid);
				g->al_timer = MLD6_LAST_LISTENER_QUERY_COUNT *
		                        tmo / MLD6_TIMER_SCALE;
				/*
				 * use al_query to record our presence
				 * in last-member state
				 */
				g->al_query = -1;
				g->al_timerid = SetTimer(mifi, g);
				IF_DEBUG(DEBUG_MLD)
					log(LOG_DEBUG, 0,
					    "timer for grp %s on mif %d "
					    "set to %ld",
					    inet6_fmt(group),
					    mifi, g->al_timer);
				break;
			}
		}
	}
}


/*
 * Process an incoming group membership report.
 */
void
accept_listener_report(src, dst, group)
	struct sockaddr_in6 *src;
	struct in6_addr *dst, *group;
{
	register mifi_t mifi;
	register struct uvif *v;
	register struct listaddr *g;
	struct sockaddr_in6 group_sa = {sizeof(group_sa), AF_INET6};

	if (IN6_IS_ADDR_MC_LINKLOCAL(group)) {
		IF_DEBUG(DEBUG_MLD)
			log(LOG_DEBUG, 0,
			    "accept_listener_report: group(%s) has the "
			    "link-local scope. discard", inet6_fmt(group));
		return;
	}

	if ((mifi = find_vif_direct_local(src)) == NO_VIF) {
		IF_DEBUG(DEBUG_MLD)
			log(LOG_INFO, 0,
			    "accept_listener_report: can't find a mif");
		return;
	}
    
	IF_DEBUG(DEBUG_MLD)
		log(LOG_INFO, 0,
		    "accepting multicast listener report: "
		    "src %s, dst %s, grp %s",
		    inet6_fmt(&src->sin6_addr), inet6_fmt(dst),
		    inet6_fmt(group));

	v = &uvifs[mifi];
    
	/*
	 * Look for the group in our group list; if found, reset its timer.
	 */
	group_sa.sin6_addr = *group;
	group_sa.sin6_scope_id = inet6_uvif2scopeid(&group_sa, v);
	for (g = v->uv_groups; g != NULL; g = g->al_next) {
		if (inet6_equal(&group_sa, &g->al_addr)) {
			g->al_reporter = *src;

			/* delete old timers, set a timer for expiration */
			g->al_timer = MLD6_LISTENER_INTERVAL;
			if (g->al_query)
				g->al_query = DeleteTimer(g->al_query);
			if (g->al_timerid)
				g->al_timerid = DeleteTimer(g->al_timerid);
			g->al_timerid = SetTimer(mifi, g);
			add_leaf(mifi, NULL, &group_sa);
			break;
		}
	}

	/*
	 * If not found, add it to the list and update kernel cache.
	 */
	if (g == NULL) {
		g = (struct listaddr *)malloc(sizeof(struct listaddr));
		if (g == NULL)
			log(LOG_ERR, 0, "ran out of memory");    /* fatal */

		g->al_addr   = group_sa;
		g->al_old = 0;

		/** set a timer for expiration **/
		g->al_query     = 0;
		g->al_timer     = MLD6_LISTENER_INTERVAL;
		g->al_reporter  = *src;
		g->al_timerid   = SetTimer(mifi, g);
		g->al_next      = v->uv_groups;
		v->uv_groups    = g;
		time(&g->al_ctime);

		add_leaf(mifi, NULL, &group_sa);
	}
}


/* TODO: send PIM prune message if the last member? */
void
accept_listener_done(src, dst, group)
	struct sockaddr_in6 *src;
	struct in6_addr *dst, *group;
{
	register mifi_t mifi;
	register struct uvif *v;
	register struct listaddr *g;
	struct sockaddr_in6 group_sa = {sizeof(group_sa), AF_INET6};

	if ((mifi = find_vif_direct_local(src)) == NO_VIF) {
		IF_DEBUG(DEBUG_MLD)
			log(LOG_INFO, 0,
			    "accept_listener_done: can't find a mif");
		return;
	}

	IF_DEBUG(DEBUG_MLD)
		log(LOG_INFO, 0,
		    "accepting listener done message: src %s, dst %s, grp %s",
		    inet6_fmt(&src->sin6_addr),
		    inet6_fmt(dst), inet6_fmt(group));

	v = &uvifs[mifi];
    
	if (!(v->uv_flags & (VIFF_QUERIER | VIFF_DR)))
		return;

	/*
	 * Look for the group in our group list in order to set up a
	 * short-timeout query.
	 */
	group_sa.sin6_addr = *group;
	group_sa.sin6_scope_id = inet6_uvif2scopeid(&group_sa, v);
	for (g = v->uv_groups; g != NULL; g = g->al_next) {
		if (inet6_equal(&group_sa, &g->al_addr)) {
			IF_DEBUG(DEBUG_MLD)
				log(LOG_DEBUG, 0,
				    "[accept_done_message] %d %ld\n",
				    g->al_old, g->al_query);

			/*
			 * Ignore the done message if there are old
			 * hosts present
			 */
			if (g->al_old)
				return;
	    
			/*
			 * still waiting for a reply to a query,
			 * ignore the done
			 */
			if (g->al_query)
				return;
	    
			/** delete old timer set a timer for expiration **/
			if (g->al_timerid)
				g->al_timerid = DeleteTimer(g->al_timerid);

			/** send a group specific querry **/
			g->al_timer = (MLD6_LAST_LISTENER_QUERY_INTERVAL/MLD6_TIMER_SCALE) *
				(MLD6_LAST_LISTENER_QUERY_COUNT + 1);
			if (v->uv_flags & VIFF_QUERIER &&
			    (v->uv_flags & VIFF_NOLISTENER) == 0)
				send_mld6(MLD6_LISTENER_QUERY, 0,
					  &v->uv_linklocal->pa_addr, NULL,
					  &g->al_addr.sin6_addr,
					  v->uv_ifindex,
					  MLD6_LAST_LISTENER_QUERY_INTERVAL, 0, 1);
			g->al_query = SetQueryTimer(g, mifi,
						    MLD6_LAST_LISTENER_QUERY_INTERVAL/MLD6_TIMER_SCALE,
						    MLD6_LAST_LISTENER_QUERY_INTERVAL);
			g->al_timerid = SetTimer(mifi, g);
			break;
		}
	}
}


/*
 * Time out record of a group membership on a vif
 */
static void
DelVif(arg)
	void *arg;
{
	cbk_t *cbk = (cbk_t *)arg;
	mifi_t mifi = cbk->mifi;
	struct uvif *v = &uvifs[mifi];
	struct listaddr *a, **anp, *g = cbk->g;

	/*
	 * Group has expired
	 * delete all kernel cache entries with this group
	 */
	if (g->al_query)
		DeleteTimer(g->al_query);

	delete_leaf(mifi, NULL, &g->al_addr);

	anp = &(v->uv_groups);
	while ((a = *anp) != NULL) {
		if (a == g) {
			*anp = a->al_next;
			free((char *)a);
		} else {
			anp = &a->al_next;
		}
	}

	free(cbk);
}


/*
 * Set a timer to delete the record of a group membership on a vif.
 */
static int
SetTimer(mifi, g)
	mifi_t mifi;
	struct listaddr *g;
{
	cbk_t *cbk;
    
	cbk = (cbk_t *) malloc(sizeof(cbk_t));
	cbk->mifi = mifi;
	cbk->g = g;
	return timer_setTimer(g->al_timer, DelVif, cbk);
}


/*
 * Delete a timer that was set above.
 */
static int
DeleteTimer(id)
	int id;
{
	timer_clearTimer(id);
	return 0;
}


/*
 * Send a group-specific query.
 */
static void
SendQuery(arg)
	void *arg;
{
	cbk_t *cbk = (cbk_t *)arg;
	register struct uvif *v = &uvifs[cbk->mifi];

	if (v->uv_flags & VIFF_QUERIER && (v->uv_flags & VIFF_NOLISTENER) == 0)
		send_mld6(MLD6_LISTENER_QUERY, 0, &v->uv_linklocal->pa_addr,
			  NULL, &cbk->g->al_addr.sin6_addr,
			  v->uv_ifindex, cbk->q_time, 0, 1);
	cbk->g->al_query = 0;
	free(cbk);
}


/*
 * Set a timer to send a group-specific query.
 */
static int
SetQueryTimer(g, mifi, to_expire, q_time)
	struct listaddr *g;
	mifi_t mifi;
	int to_expire;
	int q_time;
{
	cbk_t *cbk;

	cbk = (cbk_t *) malloc(sizeof(cbk_t));
	cbk->g = g;
	cbk->q_time = q_time;
	cbk->mifi = mifi;
	return timer_setTimer(to_expire, SendQuery, cbk);
}

/* Checks for MLD listener: returns TRUE if there is a receiver for the
 * group on the given uvif, or returns FALSE otherwise.
 */
int
check_multicast_listener(v, group)
	struct uvif *v;
	struct sockaddr_in6 *group;
{
	register struct listaddr *g;

	/*
	 * Look for the group in our listener list;
	 */
	for (g = v->uv_groups; g != NULL; g = g->al_next) {
		if (inet6_equal(group, &g->al_addr)) 
			return TRUE;
	}
	return FALSE;
}
OpenPOWER on IntegriCloud