Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/*
 * Copyright (c) 2001-2003
 *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
 * 	All rights reserved.
 * Copyright (c) 2004
 *	Hartmut Brandt
 *
 * Author: Hartmut Brandt <harti@freebsd.org>
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
 *
 * $Begemot: libunimsg/netnatm/api/unisap.c,v 1.4 2004/07/08 08:22:01 brandt Exp $
 */

#include <sys/types.h>
#ifdef _KERNEL
#include <sys/ctype.h>
#include <sys/libkern.h>
#else
#include <ctype.h>
#include <string.h>
#endif
#include <netnatm/msg/unistruct.h>
#include <netnatm/api/unisap.h>

int
unisve_check_addr(const struct unisve_addr *sve)
{
	if (sve->tag == UNISVE_ABSENT)
		return (UNISVE_OK);
	if (sve->tag == UNISVE_ANY)
		return (UNISVE_OK);
	if (sve->tag != UNISVE_PRESENT)
		return (UNISVE_ERROR_BAD_TAG);

	if (sve->type == UNI_ADDR_INTERNATIONAL) {
		if (sve->plan != UNI_ADDR_E164)
			return (UNISVE_ERROR_TYPE_PLAN_CONFLICT);
		if (sve->len == 0 || sve->len > 15)
			return (UNISVE_ERROR_ADDR_LEN);

	} else if (sve->type == UNI_ADDR_UNKNOWN) {
		if (sve->plan != UNI_ADDR_ATME)
			return (UNISVE_ERROR_TYPE_PLAN_CONFLICT);
		if (sve->len != 19)
			return (UNISVE_ERROR_ADDR_LEN);
	} else
		return (UNISVE_ERROR_BAD_ADDR_TYPE);

	return (UNISVE_OK);
}

int
unisve_check_selector(const struct unisve_selector *sve)
{
	if (sve->tag != UNISVE_PRESENT &&
	    sve->tag != UNISVE_ABSENT &&
	    sve->tag != UNISVE_ANY)
		return (UNISVE_ERROR_BAD_TAG);
	return (UNISVE_OK);
}

/*
 * We don't want to check the protocol values here.
 */
int
unisve_check_blli_id2(const struct unisve_blli_id2 *sve)
{
	if (sve->tag != UNISVE_PRESENT &&
	    sve->tag != UNISVE_ABSENT &&
	    sve->tag != UNISVE_ANY)
		return (UNISVE_ERROR_BAD_TAG);
	return (UNISVE_OK);
}

/*
 * We don't want to check the protocol values here.
 */
int
unisve_check_blli_id3(const struct unisve_blli_id3 *sve)
{
	if (sve->tag != UNISVE_PRESENT &&
	    sve->tag != UNISVE_ABSENT &&
	    sve->tag != UNISVE_ANY)
		return (UNISVE_ERROR_BAD_TAG);
	return (UNISVE_OK);
}

int
unisve_check_bhli(const struct unisve_bhli *sve)
{
	if (sve->tag == UNISVE_ABSENT)
		return (UNISVE_OK);
	if (sve->tag == UNISVE_ANY)
		return (UNISVE_OK);

	if (sve->tag != UNISVE_PRESENT)
		return (UNISVE_ERROR_BAD_TAG);

	if (sve->type != UNI_BHLI_ISO &&
	    sve->type != UNI_BHLI_USER &&
	    sve->type != UNI_BHLI_VENDOR)
		return (UNISVE_ERROR_BAD_BHLI_TYPE);

	if (sve->len > sizeof(sve->info))
		return (UNISVE_ERROR_BAD_BHLI_LEN);

	return (UNISVE_OK);
}

int
unisve_check_sap(const struct uni_sap *sap)
{
	int err;

	if ((err = unisve_check_addr(&sap->addr)) != 0 ||
	    (err = unisve_check_selector(&sap->selector)) != 0 ||
	    (err = unisve_check_blli_id2(&sap->blli_id2)) != 0 ||
	    (err = unisve_check_blli_id3(&sap->blli_id3)) != 0 ||
	    (err = unisve_check_bhli(&sap->bhli)) != 0)
		return (err);

	if (sap->addr.plan == UNI_ADDR_E164) {
		if (sap->selector.tag == UNISVE_PRESENT)
			return (UNISVE_ERROR_ADDR_SEL_CONFLICT);
	} else if (sap->addr.plan == UNI_ADDR_ATME) {
		if (sap->selector.tag == UNISVE_ABSENT)
			return (UNISVE_ERROR_ADDR_SEL_CONFLICT);
	}
	return (0);
}

#define COMMON_OVERLAP(A1,A2)						\
	if ((A1->tag == UNISVE_ABSENT && A2->tag == UNISVE_ABSENT) ||	\
	    A1->tag == UNISVE_ANY || A2->tag == UNISVE_ANY)		\
		return (1);						\
	if ((A1->tag == UNISVE_ABSENT && A2->tag == UNISVE_PRESENT) ||	\
	    (A2->tag == UNISVE_ABSENT && A1->tag == UNISVE_PRESENT))	\
		return (0);

int
unisve_overlap_addr(const struct unisve_addr *s1, const struct unisve_addr *s2)
{
	COMMON_OVERLAP(s1, s2);

	return (s1->type == s2->type && s1->plan == s2->plan &&
	    s1->len == s2->len && memcmp(s1->addr, s2->addr, s1->len) == 0);
}

int
unisve_overlap_selector(const struct unisve_selector *s1,
    const struct unisve_selector *s2)
{
	COMMON_OVERLAP(s1, s2);

	return (s1->selector == s2->selector);
}

int
unisve_overlap_blli_id2(const struct unisve_blli_id2 *s1,
    const struct unisve_blli_id2 *s2)
{
	COMMON_OVERLAP(s1, s2);

	return (s1->proto == s2->proto &&
	    (s1->proto != UNI_BLLI_L2_USER || s1->user == s2->user));
}

int
unisve_overlap_blli_id3(const struct unisve_blli_id3 *s1,
    const struct unisve_blli_id3 *s2)
{
	COMMON_OVERLAP(s1, s2);

	if (s1->proto != s2->proto)
		return (0);
	if (s1->proto == UNI_BLLI_L3_USER)
		return (s1->user == s2->user);
	if (s1->proto == UNI_BLLI_L3_TR9577) {
		if (s1->noipi && s2->noipi)
			return (1);
		if (!s1->noipi && !s2->noipi) {
			if (s1->ipi == s2->ipi) {
				if (s1->ipi != UNI_BLLI_L3_SNAP)
					return (1);
				if (s1->oui == s2->oui && s1->pid == s2->pid)
					return (1);
			}
		}
		return (0);
	}
	return (1);
}

int
unisve_overlap_bhli(const struct unisve_bhli *s1, const struct unisve_bhli *s2)
{
	COMMON_OVERLAP(s1, s2);

	return (s1->type == s2->type && s1->len == s2->len &&
	    memcmp(s1->info, s2->info, s1->len) == 0);
}

int
unisve_overlap_sap(const struct uni_sap *s1, const struct uni_sap *s2)
{
	int any1, any2;

	/*
	 * Two catch-all's SAP's are not allowed. A catch-all does never
	 * overlap with a non-catch all SAP.
	 */
	any1 = unisve_is_catchall(s1);
	any2 = unisve_is_catchall(s2);

	if (any1 && any2)
		return (1);
	 if(any1 || any2)
		return (0);

	return (unisve_overlap_addr(&s1->addr, &s2->addr) &&
	    unisve_overlap_selector(&s1->selector, &s2->selector) &&
	    unisve_overlap_blli_id2(&s1->blli_id2, &s2->blli_id2) &&
	    unisve_overlap_blli_id3(&s1->blli_id3, &s2->blli_id3) &&
	    unisve_overlap_bhli(&s1->bhli, &s2->bhli));
}

int
unisve_is_catchall(const struct uni_sap *sap)
{
	return (sap->addr.tag == UNISVE_ANY &&
	    sap->selector.tag == UNISVE_ANY &&
	    sap->blli_id2.tag == UNISVE_ANY &&
	    sap->blli_id3.tag == UNISVE_ANY &&
	    sap->bhli.tag == UNISVE_ANY);
}

int
unisve_match(const struct uni_sap *sap, const struct uni_ie_called *called,
	const struct uni_ie_blli *blli, const struct uni_ie_bhli *bhli)
{
	switch (sap->addr.tag) {
	  case UNISVE_ABSENT:
		if (IE_ISGOOD(*called))
			return (0);
		break;

	  case UNISVE_ANY:
		break;

	  case UNISVE_PRESENT:
		if (!IE_ISGOOD(*called))
			return (0);
		if (called->addr.type != sap->addr.type ||
		    called->addr.plan != sap->addr.plan)
			return (0);
		if (called->addr.plan == UNI_ADDR_E164) {
			if (called->addr.len != sap->addr.len ||
			    memcmp(called->addr.addr, sap->addr.addr,
			    called->addr.len) != 0)
				return (0);
		} else if (called->addr.plan == UNI_ADDR_ATME) {
			if (called->addr.len != 20 ||
			    memcmp(called->addr.addr, sap->addr.addr, 19) != 0)
				return (0);
		}
		break;

	  default:
		return (0);
	}

	switch (sap->selector.tag) {

	  case UNISVE_ABSENT:
		if (IE_ISGOOD(*called) && called->addr.plan == UNI_ADDR_ATME)
			return (0);
		break;

	  case UNISVE_ANY:
		break;

	  case UNISVE_PRESENT:
		if (!IE_ISGOOD(*called))
			return (0);
		if (called->addr.plan != UNI_ADDR_ATME)
			return (0);
		if (called->addr.addr[19] != sap->selector.selector)
			return (0);
		break;

	  default:
		return (0);
	}

	switch (sap->blli_id2.tag) {

	  case UNISVE_ABSENT:
		if (IE_ISGOOD(*blli) && (blli->h.present & UNI_BLLI_L2_P))
			return (0);
		break;

	  case UNISVE_ANY:
		break;

	  case UNISVE_PRESENT:
		if (!IE_ISGOOD(*blli) || (blli->h.present & UNI_BLLI_L2_P) == 0)
			return (0);
		if (blli->l2 != sap->blli_id2.proto)
			return (0);
		if (blli->l2 == UNI_BLLI_L2_USER) {
			if ((blli->h.present & UNI_BLLI_L2_USER_P) == 0)
				return (0);
			if (blli->l2_user != sap->blli_id2.user)
				return (0);
		}
		break;

	  default:
		return (0);
	}

	switch (sap->blli_id3.tag) {

	  case UNISVE_ABSENT:
		if (IE_ISGOOD(*blli) && (blli->h.present & UNI_BLLI_L3_P))
			return (0);
		break;

	  case UNISVE_ANY:
		break;

	  case UNISVE_PRESENT:
		if (!IE_ISGOOD(*blli) || (blli->h.present & UNI_BLLI_L3_P) == 0)
			return (0);
		if (blli->l3 != sap->blli_id3.proto)
			return (0);
		if (blli->l3 == UNI_BLLI_L3_USER) {
			if ((blli->h.present & UNI_BLLI_L3_USER_P) == 0)
				return (0);
			if (blli->l3_user != sap->blli_id3.user)
				return (0);
			break;
		}
		if (blli->l3 == UNI_BLLI_L3_TR9577) {
			if (sap->blli_id3.noipi) {
				if (blli->h.present & UNI_BLLI_L3_IPI_P)
					return (0);
			} else {
				if (!(blli->h.present & UNI_BLLI_L3_IPI_P))
					return (0);
				if (blli->l3_ipi != sap->blli_id3.ipi)
					return (0);
				if (blli->l3_ipi == UNI_BLLI_L3_SNAP) {
					if (!(blli->h.present &
					    UNI_BLLI_L3_SNAP_P))
						return (0);
					if (blli->oui != sap->blli_id3.oui ||
					    blli->pid != sap->blli_id3.pid)
						return (0);
				}
			}
		}
		break;

	  default:
		return (0);
	}

	switch (sap->bhli.tag) {

	  case UNISVE_ABSENT:
		if (IE_ISGOOD(*bhli))
			return (0);
		break;

	  case UNISVE_ANY:
		break;

	  case UNISVE_PRESENT:
		if (!IE_ISGOOD(*bhli))
			return (0);
		if (sap->bhli.type != bhli->type)
			return (0);
		if (sap->bhli.len != bhli->len)
			return (0);
		if (memcmp(sap->bhli.info, bhli->info, bhli->len) != 0)
			return (0);
		break;

	  default:
		return (0);
	}
	/* Uff */
	return (1);
}