summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/inet/ip/ip_listutils.c
blob: 0314a9ea793ebf3767b4bea887cc5ebbe71598b1 (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/types.h>
#include <sys/stream.h>
#include <sys/dlpi.h>
#include <sys/stropts.h>
#include <sys/strlog.h>
#include <sys/systm.h>
#include <sys/ddi.h>
#include <sys/cmn_err.h>

#include <sys/param.h>
#include <sys/tihdr.h>
#include <netinet/in.h>
#include <netinet/ip6.h>

#include <inet/common.h>
#include <inet/mi.h>
#include <inet/ip.h>
#include <inet/ip6.h>
#include <inet/ip_listutils.h>

/*
 * These functions perform set operations on sets of ipv6 addresses.
 * The sets are formatted as slist_t's (defined in <inet/ip.h>):
 *	typedef struct slist_s {
 *		int		sl_nusmrc;
 *		in6_addr_t	sl_addr[MAX_FILTER_SIZE];
 *	} slist_t;
 *
 * The functions were designed specifically for the implementation of
 * IGMPv3 and MLDv2 in ip; they were not meant to be general-purpose.
 */

/*
 * Tells if lists A and B are different or not - true if different;
 * caller guarantees that lists are <= MAX_FILTER_SIZE
 */
boolean_t
lists_are_different(const slist_t *a, const slist_t *b)
{
	int i, j;
	int acnt = SLIST_CNT(a);
	int bcnt = SLIST_CNT(b);
	boolean_t found;

	if (acnt != bcnt)
		return (B_TRUE);

	ASSERT(acnt <= MAX_FILTER_SIZE);
	ASSERT(bcnt <= MAX_FILTER_SIZE);

	for (i = 0; i < acnt; i++) {
		found = B_FALSE;
		for (j = 0; j < bcnt; j++) {
			if (IN6_ARE_ADDR_EQUAL(
			    &a->sl_addr[i], &b->sl_addr[j])) {
				found = B_TRUE;
				break;
			}
		}
		if (!found)
			return (B_TRUE);
	}
	return (B_FALSE);
}

/*
 * Tells if list a contains address addr - true if it does, false if not;
 * caller guarantees that list is <= MAX_FILTER_SIZE.
 */
boolean_t
list_has_addr(const slist_t *a, const in6_addr_t *addr)
{
	int i;

	if (SLIST_IS_EMPTY(a))
		return (B_FALSE);

	ASSERT(a->sl_numsrc <= MAX_FILTER_SIZE);

	for (i = 0; i < a->sl_numsrc; i++) {
		if (IN6_ARE_ADDR_EQUAL(&a->sl_addr[i], addr))
			return (B_TRUE);
	}
	return (B_FALSE);
}

/*
 * Implements a * b and stores the result in target; caller guarantees
 * that a and b are <= MAX_FILTER_SIZE, and that target is a valid pointer.
 * target must not be the same as a or b; for that case see
 * l_intersection_in_a().
 */
void
l_intersection(const slist_t *a, const slist_t *b, slist_t *target)
{
	int i, j;

	target->sl_numsrc = 0;

	if (SLIST_IS_EMPTY(a) || SLIST_IS_EMPTY(b))
		return;

	ASSERT(a->sl_numsrc <= MAX_FILTER_SIZE);
	ASSERT(b->sl_numsrc <= MAX_FILTER_SIZE);

	for (i = 0; i < a->sl_numsrc; i++) {
		for (j = 0; j < b->sl_numsrc; j++) {
			if (IN6_ARE_ADDR_EQUAL(
			    &a->sl_addr[i], &b->sl_addr[j])) {
				target->sl_addr[target->sl_numsrc++] =
				    a->sl_addr[i];
				break;
			}
		}
	}
}

/*
 * Implements a - b and stores the result in target; caller guarantees
 * that a and b are <= MAX_FILTER_SIZE, and that target is a valid pointer.
 * target must not be the same as a or b; for that case see l_difference_in_a().
 */
void
l_difference(const slist_t *a, const slist_t *b, slist_t *target)
{
	int i, j;
	boolean_t found = B_FALSE;

	target->sl_numsrc = 0;

	if (SLIST_IS_EMPTY(a))
		return;

	if (SLIST_IS_EMPTY(b)) {
		l_copy(a, target);
		return;
	}

	ASSERT(a->sl_numsrc <= MAX_FILTER_SIZE);
	ASSERT(b->sl_numsrc <= MAX_FILTER_SIZE);

	for (i = 0; i < a->sl_numsrc; i++) {
		for (j = 0; j < b->sl_numsrc; j++) {
			if (IN6_ARE_ADDR_EQUAL(
			    &a->sl_addr[i], &b->sl_addr[j])) {
				found = B_TRUE;
				break;
			}
		}
		if (!found) {
			target->sl_addr[target->sl_numsrc++] = a->sl_addr[i];
		} else {
			found = B_FALSE;
		}
	}
}

/*
 * Removes addr from list a.  Caller guarantees that addr is a valid
 * pointer, and that a <= MAX_FILTER_SIZE.  addr will only be removed
 * once from the list; if it appears in the list multiple times, extra
 * copies may remain.
 */
void
l_remove(slist_t *a, const in6_addr_t *addr)
{
	int i, mvsize;

	if (SLIST_IS_EMPTY(a))
		return;

	ASSERT(a->sl_numsrc <= MAX_FILTER_SIZE);

	for (i = 0; i < a->sl_numsrc; i++) {
		if (IN6_ARE_ADDR_EQUAL(&a->sl_addr[i], addr)) {
			a->sl_numsrc--;
			mvsize = (a->sl_numsrc - i) * sizeof (in6_addr_t);
			(void) memmove(&a->sl_addr[i], &a->sl_addr[i + 1],
			    mvsize);
			break;
		}
	}
}

/*
 * Make a copy of list a by allocating a new slist_t and copying only
 * a->sl_numsrc addrs.  Caller guarantees that a <= MAX_FILTER_SIZE.
 * Return a pointer to the newly alloc'd list, or NULL if a is empty
 * (no memory is alloc'd in this case).
 */
slist_t *
l_alloc_copy(const slist_t *a)
{
	slist_t *b;

	if (SLIST_IS_EMPTY(a))
		return (NULL);

	if ((b = l_alloc()) == NULL)
		return (NULL);

	l_copy(a, b);

	return (b);
}

/*
 * Copy the address list from slist a into slist b, overwriting anything
 * that might already be in slist b.  Assumes that a <= MAX_FILTER_SIZE
 * and that b points to a properly allocated slist.
 */
void
l_copy(const slist_t *a, slist_t *b)
{
	if (SLIST_IS_EMPTY(a)) {
		b->sl_numsrc = 0;
		return;
	}

	ASSERT(a->sl_numsrc <= MAX_FILTER_SIZE);

	b->sl_numsrc = a->sl_numsrc;
	(void) memcpy(b->sl_addr, a->sl_addr,
	    a->sl_numsrc * sizeof (in6_addr_t));
}

/*
 * Append to a any addrs in b that are not already in a (i.e. perform
 * a + b and store the result in a).  If b is empty, the function returns
 * without taking any action.
 *
 * Caller guarantees that a and b are <= MAX_FILTER_SIZE, and that a
 * and overflow are valid pointers.
 *
 * If an overflow occurs (a + b > MAX_FILTER_SIZE), a will contain the
 * first MAX_FILTER_SIZE addresses of the union, and *overflow will be
 * set to true.  Otherwise, *overflow will be set to false.
 */
void
l_union_in_a(slist_t *a, const slist_t *b, boolean_t *overflow)
{
	int i, j;
	boolean_t found;

	*overflow = B_FALSE;

	if (SLIST_IS_EMPTY(b))
		return;

	ASSERT(a->sl_numsrc <= MAX_FILTER_SIZE);
	ASSERT(b->sl_numsrc <= MAX_FILTER_SIZE);

	for (i = 0; i < b->sl_numsrc; i++) {
		found = B_FALSE;
		for (j = 0; j < a->sl_numsrc; j++) {
			if (IN6_ARE_ADDR_EQUAL(
			    &b->sl_addr[i], &a->sl_addr[j])) {
				found = B_TRUE;
				break;
			}
		}
		if (!found) {
			if (a->sl_numsrc == MAX_FILTER_SIZE) {
				*overflow = B_TRUE;
				break;
			} else {
				a->sl_addr[a->sl_numsrc++] = b->sl_addr[i];
			}
		}
	}
}

/*
 * Remove from list a any addresses that are not also in list b
 * (i.e. perform a * b and store the result in a).
 *
 * Caller guarantees that a and b are <= MAX_FILTER_SIZE, and that
 * a is a valid pointer.
 */
void
l_intersection_in_a(slist_t *a, const slist_t *b)
{
	int i, j, shift;
	boolean_t found;

	if (SLIST_IS_EMPTY(b)) {
		a->sl_numsrc = 0;
		return;
	}

	ASSERT(a->sl_numsrc <= MAX_FILTER_SIZE);
	ASSERT(b->sl_numsrc <= MAX_FILTER_SIZE);

	shift = 0;
	for (i = 0; i < a->sl_numsrc; i++) {
		found = B_FALSE;
		for (j = 0; j < b->sl_numsrc; j++) {
			if (IN6_ARE_ADDR_EQUAL(
			    &a->sl_addr[i], &b->sl_addr[j])) {
				found = B_TRUE;
				break;
			}
		}
		if (!found)
			shift++;
		else if (shift > 0)
			a->sl_addr[i - shift] = a->sl_addr[i];
	}
	a->sl_numsrc -= shift;
}

/*
 * Remove from list a any addresses that are in list b (i.e. perform
 * a - b and store the result in a).
 *
 * Caller guarantees that a and b are <= MAX_FILTER_SIZE.  If either
 * list is empty (or a null pointer), the function returns without
 * taking any action.
 */
void
l_difference_in_a(slist_t *a, const slist_t *b)
{
	int i, j, shift;
	boolean_t found;

	if (SLIST_IS_EMPTY(a) || SLIST_IS_EMPTY(b))
		return;

	ASSERT(a->sl_numsrc <= MAX_FILTER_SIZE);
	ASSERT(b->sl_numsrc <= MAX_FILTER_SIZE);

	shift = 0;
	for (i = 0; i < a->sl_numsrc; i++) {
		found = B_FALSE;
		for (j = 0; j < b->sl_numsrc; j++) {
			if (IN6_ARE_ADDR_EQUAL(
			    &a->sl_addr[i], &b->sl_addr[j])) {
				found = B_TRUE;
				break;
			}
		}
		if (found)
			shift++;
		else if (shift > 0)
			a->sl_addr[i - shift] = a->sl_addr[i];
	}
	a->sl_numsrc -= shift;
}

/*
 * Wrapper function to alloc an slist_t.
 */
slist_t *
l_alloc()
{
	slist_t *p;

	p = (slist_t *)mi_alloc(sizeof (slist_t), BPRI_MED);
	if (p != NULL)
		p->sl_numsrc = 0;
	return (p);
}

/*
 * Frees an slist_t structure.  Provided for symmetry with l_alloc().
 */
void
l_free(slist_t *a)
{
	mi_free(a);
}