Commit | Line | Data |
---|---|---|
40b0b3f8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
ca9b0147 | 2 | /* |
05ba4c89 | 3 | * BER and PER decoding library for H.323 conntrack/NAT module. |
5e35941d | 4 | * |
7582e9d1 | 5 | * Copyright (c) 2006 by Jing Min Zhao <zhaojingmin@users.sourceforge.net> |
5e35941d | 6 | * |
05ba4c89 | 7 | * See nf_conntrack_helper_h323_asn1.h for details. |
ca9b0147 | 8 | */ |
5e35941d JMZ |
9 | |
10 | #ifdef __KERNEL__ | |
11 | #include <linux/kernel.h> | |
12 | #else | |
13 | #include <stdio.h> | |
14 | #endif | |
f587de0e | 15 | #include <linux/netfilter/nf_conntrack_h323_asn1.h> |
5e35941d JMZ |
16 | |
17 | /* Trace Flag */ | |
18 | #ifndef H323_TRACE | |
19 | #define H323_TRACE 0 | |
20 | #endif | |
21 | ||
22 | #if H323_TRACE | |
23 | #define TAB_SIZE 4 | |
24 | #define IFTHEN(cond, act) if(cond){act;} | |
25 | #ifdef __KERNEL__ | |
26 | #define PRINT printk | |
27 | #else | |
28 | #define PRINT printf | |
29 | #endif | |
30 | #define FNAME(name) name, | |
31 | #else | |
32 | #define IFTHEN(cond, act) | |
33 | #define PRINT(fmt, args...) | |
34 | #define FNAME(name) | |
35 | #endif | |
36 | ||
37 | /* ASN.1 Types */ | |
38 | #define NUL 0 | |
39 | #define BOOL 1 | |
40 | #define OID 2 | |
41 | #define INT 3 | |
42 | #define ENUM 4 | |
43 | #define BITSTR 5 | |
44 | #define NUMSTR 6 | |
45 | #define NUMDGT 6 | |
46 | #define TBCDSTR 6 | |
47 | #define OCTSTR 7 | |
48 | #define PRTSTR 7 | |
49 | #define IA5STR 7 | |
50 | #define GENSTR 7 | |
51 | #define BMPSTR 8 | |
52 | #define SEQ 9 | |
53 | #define SET 9 | |
54 | #define SEQOF 10 | |
55 | #define SETOF 10 | |
56 | #define CHOICE 11 | |
57 | ||
58 | /* Constraint Types */ | |
59 | #define FIXD 0 | |
60 | /* #define BITS 1-8 */ | |
61 | #define BYTE 9 | |
62 | #define WORD 10 | |
63 | #define CONS 11 | |
64 | #define SEMI 12 | |
65 | #define UNCO 13 | |
66 | ||
67 | /* ASN.1 Type Attributes */ | |
68 | #define SKIP 0 | |
69 | #define STOP 1 | |
70 | #define DECODE 2 | |
71 | #define EXT 4 | |
72 | #define OPEN 8 | |
73 | #define OPT 16 | |
74 | ||
75 | ||
76 | /* ASN.1 Field Structure */ | |
77 | typedef struct field_t { | |
78 | #if H323_TRACE | |
79 | char *name; | |
80 | #endif | |
81 | unsigned char type; | |
82 | unsigned char sz; | |
83 | unsigned char lb; | |
84 | unsigned char ub; | |
85 | unsigned short attr; | |
86 | unsigned short offset; | |
905e3e8e | 87 | const struct field_t *fields; |
5e35941d JMZ |
88 | } field_t; |
89 | ||
90 | /* Bit Stream */ | |
67704c2a | 91 | struct bitstr { |
5e35941d JMZ |
92 | unsigned char *buf; |
93 | unsigned char *beg; | |
94 | unsigned char *end; | |
95 | unsigned char *cur; | |
1f807d6e | 96 | unsigned int bit; |
67704c2a | 97 | }; |
5e35941d JMZ |
98 | |
99 | /* Tool Functions */ | |
e79ec50b JE |
100 | #define INC_BIT(bs) if((++(bs)->bit)>7){(bs)->cur++;(bs)->bit=0;} |
101 | #define INC_BITS(bs,b) if(((bs)->bit+=(b))>7){(bs)->cur+=(bs)->bit>>3;(bs)->bit&=7;} | |
102 | #define BYTE_ALIGN(bs) if((bs)->bit){(bs)->cur++;(bs)->bit=0;} | |
67704c2a HS |
103 | static unsigned int get_len(struct bitstr *bs); |
104 | static unsigned int get_bit(struct bitstr *bs); | |
105 | static unsigned int get_bits(struct bitstr *bs, unsigned int b); | |
106 | static unsigned int get_bitmap(struct bitstr *bs, unsigned int b); | |
107 | static unsigned int get_uint(struct bitstr *bs, int b); | |
5e35941d JMZ |
108 | |
109 | /* Decoder Functions */ | |
67704c2a HS |
110 | static int decode_nul(struct bitstr *bs, const struct field_t *f, char *base, int level); |
111 | static int decode_bool(struct bitstr *bs, const struct field_t *f, char *base, int level); | |
112 | static int decode_oid(struct bitstr *bs, const struct field_t *f, char *base, int level); | |
113 | static int decode_int(struct bitstr *bs, const struct field_t *f, char *base, int level); | |
114 | static int decode_enum(struct bitstr *bs, const struct field_t *f, char *base, int level); | |
115 | static int decode_bitstr(struct bitstr *bs, const struct field_t *f, char *base, int level); | |
116 | static int decode_numstr(struct bitstr *bs, const struct field_t *f, char *base, int level); | |
117 | static int decode_octstr(struct bitstr *bs, const struct field_t *f, char *base, int level); | |
118 | static int decode_bmpstr(struct bitstr *bs, const struct field_t *f, char *base, int level); | |
119 | static int decode_seq(struct bitstr *bs, const struct field_t *f, char *base, int level); | |
120 | static int decode_seqof(struct bitstr *bs, const struct field_t *f, char *base, int level); | |
121 | static int decode_choice(struct bitstr *bs, const struct field_t *f, char *base, int level); | |
5e35941d JMZ |
122 | |
123 | /* Decoder Functions Vector */ | |
67704c2a | 124 | typedef int (*decoder_t)(struct bitstr *, const struct field_t *, char *, int); |
dc64d02b | 125 | static const decoder_t Decoders[] = { |
5e35941d JMZ |
126 | decode_nul, |
127 | decode_bool, | |
128 | decode_oid, | |
129 | decode_int, | |
130 | decode_enum, | |
131 | decode_bitstr, | |
132 | decode_numstr, | |
133 | decode_octstr, | |
134 | decode_bmpstr, | |
135 | decode_seq, | |
136 | decode_seqof, | |
137 | decode_choice, | |
138 | }; | |
139 | ||
ca9b0147 | 140 | /* |
5e35941d | 141 | * H.323 Types |
ca9b0147 | 142 | */ |
f587de0e | 143 | #include "nf_conntrack_h323_types.c" |
5e35941d | 144 | |
ca9b0147 | 145 | /* |
5e35941d | 146 | * Functions |
ca9b0147 VR |
147 | */ |
148 | ||
5e35941d | 149 | /* Assume bs is aligned && v < 16384 */ |
67704c2a | 150 | static unsigned int get_len(struct bitstr *bs) |
5e35941d | 151 | { |
1f807d6e | 152 | unsigned int v; |
5e35941d JMZ |
153 | |
154 | v = *bs->cur++; | |
155 | ||
156 | if (v & 0x80) { | |
157 | v &= 0x3f; | |
158 | v <<= 8; | |
159 | v += *bs->cur++; | |
160 | } | |
161 | ||
162 | return v; | |
163 | } | |
164 | ||
ec8a8f3c | 165 | static int nf_h323_error_boundary(struct bitstr *bs, size_t bytes, size_t bits) |
bc7d811a | 166 | { |
ec8a8f3c ES |
167 | bits += bs->bit; |
168 | bytes += bits / BITS_PER_BYTE; | |
169 | if (bits % BITS_PER_BYTE > 0) | |
170 | bytes++; | |
171 | ||
f5e85ce8 | 172 | if (bs->cur + bytes > bs->end) |
bc7d811a ES |
173 | return 1; |
174 | ||
175 | return 0; | |
176 | } | |
177 | ||
67704c2a | 178 | static unsigned int get_bit(struct bitstr *bs) |
5e35941d | 179 | { |
1f807d6e | 180 | unsigned int b = (*bs->cur) & (0x80 >> bs->bit); |
5e35941d JMZ |
181 | |
182 | INC_BIT(bs); | |
183 | ||
184 | return b; | |
185 | } | |
186 | ||
5e35941d | 187 | /* Assume b <= 8 */ |
67704c2a | 188 | static unsigned int get_bits(struct bitstr *bs, unsigned int b) |
5e35941d | 189 | { |
1f807d6e | 190 | unsigned int v, l; |
5e35941d JMZ |
191 | |
192 | v = (*bs->cur) & (0xffU >> bs->bit); | |
193 | l = b + bs->bit; | |
194 | ||
195 | if (l < 8) { | |
196 | v >>= 8 - l; | |
197 | bs->bit = l; | |
198 | } else if (l == 8) { | |
199 | bs->cur++; | |
200 | bs->bit = 0; | |
201 | } else { /* l > 8 */ | |
202 | ||
203 | v <<= 8; | |
204 | v += *(++bs->cur); | |
205 | v >>= 16 - l; | |
206 | bs->bit = l - 8; | |
207 | } | |
208 | ||
209 | return v; | |
210 | } | |
211 | ||
5e35941d | 212 | /* Assume b <= 32 */ |
67704c2a | 213 | static unsigned int get_bitmap(struct bitstr *bs, unsigned int b) |
5e35941d | 214 | { |
1f807d6e | 215 | unsigned int v, l, shift, bytes; |
5e35941d JMZ |
216 | |
217 | if (!b) | |
218 | return 0; | |
219 | ||
220 | l = bs->bit + b; | |
221 | ||
222 | if (l < 8) { | |
1f807d6e | 223 | v = (unsigned int)(*bs->cur) << (bs->bit + 24); |
5e35941d JMZ |
224 | bs->bit = l; |
225 | } else if (l == 8) { | |
1f807d6e | 226 | v = (unsigned int)(*bs->cur++) << (bs->bit + 24); |
5e35941d JMZ |
227 | bs->bit = 0; |
228 | } else { | |
229 | for (bytes = l >> 3, shift = 24, v = 0; bytes; | |
230 | bytes--, shift -= 8) | |
1f807d6e | 231 | v |= (unsigned int)(*bs->cur++) << shift; |
5e35941d JMZ |
232 | |
233 | if (l < 32) { | |
1f807d6e | 234 | v |= (unsigned int)(*bs->cur) << shift; |
5e35941d JMZ |
235 | v <<= bs->bit; |
236 | } else if (l > 32) { | |
237 | v <<= bs->bit; | |
238 | v |= (*bs->cur) >> (8 - bs->bit); | |
239 | } | |
240 | ||
241 | bs->bit = l & 0x7; | |
242 | } | |
243 | ||
244 | v &= 0xffffffff << (32 - b); | |
245 | ||
246 | return v; | |
247 | } | |
248 | ||
ca9b0147 | 249 | /* |
5e35941d | 250 | * Assume bs is aligned and sizeof(unsigned int) == 4 |
ca9b0147 | 251 | */ |
67704c2a | 252 | static unsigned int get_uint(struct bitstr *bs, int b) |
5e35941d | 253 | { |
1f807d6e | 254 | unsigned int v = 0; |
5e35941d JMZ |
255 | |
256 | switch (b) { | |
257 | case 4: | |
258 | v |= *bs->cur++; | |
259 | v <<= 8; | |
e8542dce | 260 | /* fall through */ |
5e35941d JMZ |
261 | case 3: |
262 | v |= *bs->cur++; | |
263 | v <<= 8; | |
e8542dce | 264 | /* fall through */ |
5e35941d JMZ |
265 | case 2: |
266 | v |= *bs->cur++; | |
267 | v <<= 8; | |
e8542dce | 268 | /* fall through */ |
5e35941d JMZ |
269 | case 1: |
270 | v |= *bs->cur++; | |
271 | break; | |
272 | } | |
273 | return v; | |
274 | } | |
275 | ||
67704c2a | 276 | static int decode_nul(struct bitstr *bs, const struct field_t *f, |
905e3e8e | 277 | char *base, int level) |
5e35941d JMZ |
278 | { |
279 | PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name); | |
280 | ||
281 | return H323_ERROR_NONE; | |
282 | } | |
283 | ||
67704c2a | 284 | static int decode_bool(struct bitstr *bs, const struct field_t *f, |
905e3e8e | 285 | char *base, int level) |
5e35941d JMZ |
286 | { |
287 | PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name); | |
288 | ||
289 | INC_BIT(bs); | |
ec8a8f3c | 290 | if (nf_h323_error_boundary(bs, 0, 0)) |
bc7d811a | 291 | return H323_ERROR_BOUND; |
5e35941d JMZ |
292 | return H323_ERROR_NONE; |
293 | } | |
294 | ||
67704c2a | 295 | static int decode_oid(struct bitstr *bs, const struct field_t *f, |
905e3e8e | 296 | char *base, int level) |
5e35941d JMZ |
297 | { |
298 | int len; | |
299 | ||
300 | PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name); | |
301 | ||
302 | BYTE_ALIGN(bs); | |
ec8a8f3c | 303 | if (nf_h323_error_boundary(bs, 1, 0)) |
bc7d811a ES |
304 | return H323_ERROR_BOUND; |
305 | ||
5e35941d JMZ |
306 | len = *bs->cur++; |
307 | bs->cur += len; | |
ec8a8f3c | 308 | if (nf_h323_error_boundary(bs, 0, 0)) |
bc7d811a | 309 | return H323_ERROR_BOUND; |
5e35941d | 310 | |
5e35941d JMZ |
311 | return H323_ERROR_NONE; |
312 | } | |
313 | ||
67704c2a | 314 | static int decode_int(struct bitstr *bs, const struct field_t *f, |
905e3e8e | 315 | char *base, int level) |
5e35941d | 316 | { |
1f807d6e | 317 | unsigned int len; |
5e35941d JMZ |
318 | |
319 | PRINT("%*.s%s", level * TAB_SIZE, " ", f->name); | |
320 | ||
321 | switch (f->sz) { | |
322 | case BYTE: /* Range == 256 */ | |
323 | BYTE_ALIGN(bs); | |
324 | bs->cur++; | |
325 | break; | |
326 | case WORD: /* 257 <= Range <= 64K */ | |
327 | BYTE_ALIGN(bs); | |
328 | bs->cur += 2; | |
329 | break; | |
330 | case CONS: /* 64K < Range < 4G */ | |
ec8a8f3c ES |
331 | if (nf_h323_error_boundary(bs, 0, 2)) |
332 | return H323_ERROR_BOUND; | |
5e35941d JMZ |
333 | len = get_bits(bs, 2) + 1; |
334 | BYTE_ALIGN(bs); | |
335 | if (base && (f->attr & DECODE)) { /* timeToLive */ | |
1f807d6e | 336 | unsigned int v = get_uint(bs, len) + f->lb; |
5e35941d | 337 | PRINT(" = %u", v); |
1f807d6e | 338 | *((unsigned int *)(base + f->offset)) = v; |
5e35941d JMZ |
339 | } |
340 | bs->cur += len; | |
341 | break; | |
342 | case UNCO: | |
343 | BYTE_ALIGN(bs); | |
ec8a8f3c | 344 | if (nf_h323_error_boundary(bs, 2, 0)) |
bc7d811a | 345 | return H323_ERROR_BOUND; |
5e35941d JMZ |
346 | len = get_len(bs); |
347 | bs->cur += len; | |
348 | break; | |
349 | default: /* 2 <= Range <= 255 */ | |
350 | INC_BITS(bs, f->sz); | |
351 | break; | |
352 | } | |
353 | ||
354 | PRINT("\n"); | |
355 | ||
ec8a8f3c | 356 | if (nf_h323_error_boundary(bs, 0, 0)) |
bc7d811a | 357 | return H323_ERROR_BOUND; |
5e35941d JMZ |
358 | return H323_ERROR_NONE; |
359 | } | |
360 | ||
67704c2a | 361 | static int decode_enum(struct bitstr *bs, const struct field_t *f, |
905e3e8e | 362 | char *base, int level) |
5e35941d JMZ |
363 | { |
364 | PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name); | |
365 | ||
366 | if ((f->attr & EXT) && get_bit(bs)) { | |
367 | INC_BITS(bs, 7); | |
368 | } else { | |
369 | INC_BITS(bs, f->sz); | |
370 | } | |
371 | ||
ec8a8f3c | 372 | if (nf_h323_error_boundary(bs, 0, 0)) |
bc7d811a | 373 | return H323_ERROR_BOUND; |
5e35941d JMZ |
374 | return H323_ERROR_NONE; |
375 | } | |
376 | ||
67704c2a | 377 | static int decode_bitstr(struct bitstr *bs, const struct field_t *f, |
905e3e8e | 378 | char *base, int level) |
5e35941d | 379 | { |
1f807d6e | 380 | unsigned int len; |
5e35941d JMZ |
381 | |
382 | PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name); | |
383 | ||
384 | BYTE_ALIGN(bs); | |
385 | switch (f->sz) { | |
386 | case FIXD: /* fixed length > 16 */ | |
387 | len = f->lb; | |
388 | break; | |
389 | case WORD: /* 2-byte length */ | |
ec8a8f3c | 390 | if (nf_h323_error_boundary(bs, 2, 0)) |
bc7d811a | 391 | return H323_ERROR_BOUND; |
5e35941d JMZ |
392 | len = (*bs->cur++) << 8; |
393 | len += (*bs->cur++) + f->lb; | |
394 | break; | |
395 | case SEMI: | |
ec8a8f3c | 396 | if (nf_h323_error_boundary(bs, 2, 0)) |
bc7d811a | 397 | return H323_ERROR_BOUND; |
5e35941d JMZ |
398 | len = get_len(bs); |
399 | break; | |
400 | default: | |
401 | len = 0; | |
402 | break; | |
403 | } | |
404 | ||
405 | bs->cur += len >> 3; | |
406 | bs->bit = len & 7; | |
407 | ||
ec8a8f3c | 408 | if (nf_h323_error_boundary(bs, 0, 0)) |
bc7d811a | 409 | return H323_ERROR_BOUND; |
5e35941d JMZ |
410 | return H323_ERROR_NONE; |
411 | } | |
412 | ||
67704c2a | 413 | static int decode_numstr(struct bitstr *bs, const struct field_t *f, |
905e3e8e | 414 | char *base, int level) |
5e35941d | 415 | { |
1f807d6e | 416 | unsigned int len; |
5e35941d JMZ |
417 | |
418 | PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name); | |
419 | ||
420 | /* 2 <= Range <= 255 */ | |
ec8a8f3c ES |
421 | if (nf_h323_error_boundary(bs, 0, f->sz)) |
422 | return H323_ERROR_BOUND; | |
5e35941d JMZ |
423 | len = get_bits(bs, f->sz) + f->lb; |
424 | ||
425 | BYTE_ALIGN(bs); | |
426 | INC_BITS(bs, (len << 2)); | |
427 | ||
ec8a8f3c | 428 | if (nf_h323_error_boundary(bs, 0, 0)) |
bc7d811a | 429 | return H323_ERROR_BOUND; |
5e35941d JMZ |
430 | return H323_ERROR_NONE; |
431 | } | |
432 | ||
67704c2a | 433 | static int decode_octstr(struct bitstr *bs, const struct field_t *f, |
905e3e8e | 434 | char *base, int level) |
5e35941d | 435 | { |
1f807d6e | 436 | unsigned int len; |
5e35941d JMZ |
437 | |
438 | PRINT("%*.s%s", level * TAB_SIZE, " ", f->name); | |
439 | ||
440 | switch (f->sz) { | |
441 | case FIXD: /* Range == 1 */ | |
442 | if (f->lb > 2) { | |
443 | BYTE_ALIGN(bs); | |
444 | if (base && (f->attr & DECODE)) { | |
445 | /* The IP Address */ | |
446 | IFTHEN(f->lb == 4, | |
447 | PRINT(" = %d.%d.%d.%d:%d", | |
448 | bs->cur[0], bs->cur[1], | |
449 | bs->cur[2], bs->cur[3], | |
450 | bs->cur[4] * 256 + bs->cur[5])); | |
1f807d6e | 451 | *((unsigned int *)(base + f->offset)) = |
5e35941d JMZ |
452 | bs->cur - bs->buf; |
453 | } | |
454 | } | |
455 | len = f->lb; | |
456 | break; | |
457 | case BYTE: /* Range == 256 */ | |
458 | BYTE_ALIGN(bs); | |
ec8a8f3c | 459 | if (nf_h323_error_boundary(bs, 1, 0)) |
bc7d811a | 460 | return H323_ERROR_BOUND; |
5e35941d JMZ |
461 | len = (*bs->cur++) + f->lb; |
462 | break; | |
463 | case SEMI: | |
464 | BYTE_ALIGN(bs); | |
ec8a8f3c | 465 | if (nf_h323_error_boundary(bs, 2, 0)) |
bc7d811a | 466 | return H323_ERROR_BOUND; |
5e35941d JMZ |
467 | len = get_len(bs) + f->lb; |
468 | break; | |
469 | default: /* 2 <= Range <= 255 */ | |
ec8a8f3c ES |
470 | if (nf_h323_error_boundary(bs, 0, f->sz)) |
471 | return H323_ERROR_BOUND; | |
5e35941d JMZ |
472 | len = get_bits(bs, f->sz) + f->lb; |
473 | BYTE_ALIGN(bs); | |
474 | break; | |
475 | } | |
476 | ||
477 | bs->cur += len; | |
478 | ||
479 | PRINT("\n"); | |
480 | ||
ec8a8f3c | 481 | if (nf_h323_error_boundary(bs, 0, 0)) |
bc7d811a | 482 | return H323_ERROR_BOUND; |
5e35941d JMZ |
483 | return H323_ERROR_NONE; |
484 | } | |
485 | ||
67704c2a | 486 | static int decode_bmpstr(struct bitstr *bs, const struct field_t *f, |
905e3e8e | 487 | char *base, int level) |
5e35941d | 488 | { |
1f807d6e | 489 | unsigned int len; |
5e35941d JMZ |
490 | |
491 | PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name); | |
492 | ||
493 | switch (f->sz) { | |
494 | case BYTE: /* Range == 256 */ | |
495 | BYTE_ALIGN(bs); | |
ec8a8f3c | 496 | if (nf_h323_error_boundary(bs, 1, 0)) |
bc7d811a | 497 | return H323_ERROR_BOUND; |
5e35941d JMZ |
498 | len = (*bs->cur++) + f->lb; |
499 | break; | |
500 | default: /* 2 <= Range <= 255 */ | |
ec8a8f3c ES |
501 | if (nf_h323_error_boundary(bs, 0, f->sz)) |
502 | return H323_ERROR_BOUND; | |
5e35941d JMZ |
503 | len = get_bits(bs, f->sz) + f->lb; |
504 | BYTE_ALIGN(bs); | |
505 | break; | |
506 | } | |
507 | ||
508 | bs->cur += len << 1; | |
509 | ||
ec8a8f3c | 510 | if (nf_h323_error_boundary(bs, 0, 0)) |
bc7d811a | 511 | return H323_ERROR_BOUND; |
5e35941d JMZ |
512 | return H323_ERROR_NONE; |
513 | } | |
514 | ||
67704c2a | 515 | static int decode_seq(struct bitstr *bs, const struct field_t *f, |
905e3e8e | 516 | char *base, int level) |
5e35941d | 517 | { |
1f807d6e | 518 | unsigned int ext, bmp, i, opt, len = 0, bmp2, bmp2_len; |
5e35941d | 519 | int err; |
905e3e8e | 520 | const struct field_t *son; |
5e35941d JMZ |
521 | unsigned char *beg = NULL; |
522 | ||
523 | PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name); | |
524 | ||
525 | /* Decode? */ | |
526 | base = (base && (f->attr & DECODE)) ? base + f->offset : NULL; | |
527 | ||
528 | /* Extensible? */ | |
ec8a8f3c ES |
529 | if (nf_h323_error_boundary(bs, 0, 1)) |
530 | return H323_ERROR_BOUND; | |
5e35941d JMZ |
531 | ext = (f->attr & EXT) ? get_bit(bs) : 0; |
532 | ||
533 | /* Get fields bitmap */ | |
ec8a8f3c ES |
534 | if (nf_h323_error_boundary(bs, 0, f->sz)) |
535 | return H323_ERROR_BOUND; | |
5e35941d JMZ |
536 | bmp = get_bitmap(bs, f->sz); |
537 | if (base) | |
1f807d6e | 538 | *(unsigned int *)base = bmp; |
5e35941d JMZ |
539 | |
540 | /* Decode the root components */ | |
541 | for (i = opt = 0, son = f->fields; i < f->lb; i++, son++) { | |
542 | if (son->attr & STOP) { | |
543 | PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ", | |
544 | son->name); | |
545 | return H323_ERROR_STOP; | |
546 | } | |
547 | ||
548 | if (son->attr & OPT) { /* Optional component */ | |
549 | if (!((0x80000000U >> (opt++)) & bmp)) /* Not exist */ | |
550 | continue; | |
551 | } | |
552 | ||
553 | /* Decode */ | |
554 | if (son->attr & OPEN) { /* Open field */ | |
ec8a8f3c | 555 | if (nf_h323_error_boundary(bs, 2, 0)) |
bc7d811a | 556 | return H323_ERROR_BOUND; |
5e35941d | 557 | len = get_len(bs); |
ec8a8f3c | 558 | if (nf_h323_error_boundary(bs, len, 0)) |
bc7d811a | 559 | return H323_ERROR_BOUND; |
25845b51 | 560 | if (!base || !(son->attr & DECODE)) { |
5e35941d JMZ |
561 | PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, |
562 | " ", son->name); | |
563 | bs->cur += len; | |
564 | continue; | |
565 | } | |
566 | beg = bs->cur; | |
567 | ||
568 | /* Decode */ | |
569 | if ((err = (Decoders[son->type]) (bs, son, base, | |
7185989d PM |
570 | level + 1)) < |
571 | H323_ERROR_NONE) | |
5e35941d JMZ |
572 | return err; |
573 | ||
574 | bs->cur = beg + len; | |
575 | bs->bit = 0; | |
576 | } else if ((err = (Decoders[son->type]) (bs, son, base, | |
7185989d PM |
577 | level + 1)) < |
578 | H323_ERROR_NONE) | |
5e35941d JMZ |
579 | return err; |
580 | } | |
581 | ||
582 | /* No extension? */ | |
583 | if (!ext) | |
584 | return H323_ERROR_NONE; | |
585 | ||
586 | /* Get the extension bitmap */ | |
ec8a8f3c ES |
587 | if (nf_h323_error_boundary(bs, 0, 7)) |
588 | return H323_ERROR_BOUND; | |
5e35941d | 589 | bmp2_len = get_bits(bs, 7) + 1; |
ec8a8f3c | 590 | if (nf_h323_error_boundary(bs, 0, bmp2_len)) |
bc7d811a | 591 | return H323_ERROR_BOUND; |
5e35941d JMZ |
592 | bmp2 = get_bitmap(bs, bmp2_len); |
593 | bmp |= bmp2 >> f->sz; | |
594 | if (base) | |
1f807d6e | 595 | *(unsigned int *)base = bmp; |
5e35941d JMZ |
596 | BYTE_ALIGN(bs); |
597 | ||
598 | /* Decode the extension components */ | |
599 | for (opt = 0; opt < bmp2_len; opt++, i++, son++) { | |
5e35941d JMZ |
600 | /* Check Range */ |
601 | if (i >= f->ub) { /* Newer Version? */ | |
ec8a8f3c | 602 | if (nf_h323_error_boundary(bs, 2, 0)) |
bc7d811a | 603 | return H323_ERROR_BOUND; |
5e35941d | 604 | len = get_len(bs); |
ec8a8f3c | 605 | if (nf_h323_error_boundary(bs, len, 0)) |
bc7d811a | 606 | return H323_ERROR_BOUND; |
5e35941d JMZ |
607 | bs->cur += len; |
608 | continue; | |
609 | } | |
610 | ||
558585aa JMZ |
611 | if (son->attr & STOP) { |
612 | PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ", | |
613 | son->name); | |
614 | return H323_ERROR_STOP; | |
615 | } | |
616 | ||
617 | if (!((0x80000000 >> opt) & bmp2)) /* Not present */ | |
618 | continue; | |
619 | ||
ec8a8f3c | 620 | if (nf_h323_error_boundary(bs, 2, 0)) |
bc7d811a | 621 | return H323_ERROR_BOUND; |
5e35941d | 622 | len = get_len(bs); |
ec8a8f3c | 623 | if (nf_h323_error_boundary(bs, len, 0)) |
bc7d811a | 624 | return H323_ERROR_BOUND; |
5e35941d JMZ |
625 | if (!base || !(son->attr & DECODE)) { |
626 | PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ", | |
627 | son->name); | |
628 | bs->cur += len; | |
629 | continue; | |
630 | } | |
631 | beg = bs->cur; | |
632 | ||
633 | if ((err = (Decoders[son->type]) (bs, son, base, | |
7185989d PM |
634 | level + 1)) < |
635 | H323_ERROR_NONE) | |
5e35941d JMZ |
636 | return err; |
637 | ||
638 | bs->cur = beg + len; | |
639 | bs->bit = 0; | |
640 | } | |
641 | return H323_ERROR_NONE; | |
642 | } | |
643 | ||
67704c2a | 644 | static int decode_seqof(struct bitstr *bs, const struct field_t *f, |
905e3e8e | 645 | char *base, int level) |
5e35941d | 646 | { |
1f807d6e | 647 | unsigned int count, effective_count = 0, i, len = 0; |
5e35941d | 648 | int err; |
905e3e8e | 649 | const struct field_t *son; |
5e35941d JMZ |
650 | unsigned char *beg = NULL; |
651 | ||
652 | PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name); | |
653 | ||
654 | /* Decode? */ | |
655 | base = (base && (f->attr & DECODE)) ? base + f->offset : NULL; | |
656 | ||
657 | /* Decode item count */ | |
658 | switch (f->sz) { | |
659 | case BYTE: | |
660 | BYTE_ALIGN(bs); | |
ec8a8f3c | 661 | if (nf_h323_error_boundary(bs, 1, 0)) |
bc7d811a | 662 | return H323_ERROR_BOUND; |
5e35941d JMZ |
663 | count = *bs->cur++; |
664 | break; | |
665 | case WORD: | |
666 | BYTE_ALIGN(bs); | |
ec8a8f3c | 667 | if (nf_h323_error_boundary(bs, 2, 0)) |
bc7d811a | 668 | return H323_ERROR_BOUND; |
5e35941d JMZ |
669 | count = *bs->cur++; |
670 | count <<= 8; | |
b4232a22 | 671 | count += *bs->cur++; |
5e35941d JMZ |
672 | break; |
673 | case SEMI: | |
674 | BYTE_ALIGN(bs); | |
ec8a8f3c | 675 | if (nf_h323_error_boundary(bs, 2, 0)) |
bc7d811a | 676 | return H323_ERROR_BOUND; |
5e35941d JMZ |
677 | count = get_len(bs); |
678 | break; | |
679 | default: | |
ec8a8f3c ES |
680 | if (nf_h323_error_boundary(bs, 0, f->sz)) |
681 | return H323_ERROR_BOUND; | |
5e35941d JMZ |
682 | count = get_bits(bs, f->sz); |
683 | break; | |
684 | } | |
685 | count += f->lb; | |
686 | ||
687 | /* Write Count */ | |
688 | if (base) { | |
689 | effective_count = count > f->ub ? f->ub : count; | |
1f807d6e JE |
690 | *(unsigned int *)base = effective_count; |
691 | base += sizeof(unsigned int); | |
5e35941d JMZ |
692 | } |
693 | ||
694 | /* Decode nested field */ | |
695 | son = f->fields; | |
696 | if (base) | |
697 | base -= son->offset; | |
698 | for (i = 0; i < count; i++) { | |
699 | if (son->attr & OPEN) { | |
700 | BYTE_ALIGN(bs); | |
ec8a8f3c ES |
701 | if (nf_h323_error_boundary(bs, 2, 0)) |
702 | return H323_ERROR_BOUND; | |
5e35941d | 703 | len = get_len(bs); |
ec8a8f3c | 704 | if (nf_h323_error_boundary(bs, len, 0)) |
bc7d811a | 705 | return H323_ERROR_BOUND; |
5e35941d JMZ |
706 | if (!base || !(son->attr & DECODE)) { |
707 | PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, | |
708 | " ", son->name); | |
709 | bs->cur += len; | |
710 | continue; | |
711 | } | |
712 | beg = bs->cur; | |
713 | ||
714 | if ((err = (Decoders[son->type]) (bs, son, | |
715 | i < | |
716 | effective_count ? | |
717 | base : NULL, | |
7185989d PM |
718 | level + 1)) < |
719 | H323_ERROR_NONE) | |
5e35941d JMZ |
720 | return err; |
721 | ||
722 | bs->cur = beg + len; | |
723 | bs->bit = 0; | |
724 | } else | |
7185989d PM |
725 | if ((err = (Decoders[son->type]) (bs, son, |
726 | i < | |
727 | effective_count ? | |
728 | base : NULL, | |
729 | level + 1)) < | |
730 | H323_ERROR_NONE) | |
731 | return err; | |
5e35941d JMZ |
732 | |
733 | if (base) | |
734 | base += son->offset; | |
735 | } | |
736 | ||
737 | return H323_ERROR_NONE; | |
738 | } | |
739 | ||
67704c2a | 740 | static int decode_choice(struct bitstr *bs, const struct field_t *f, |
905e3e8e | 741 | char *base, int level) |
5e35941d | 742 | { |
1f807d6e | 743 | unsigned int type, ext, len = 0; |
5e35941d | 744 | int err; |
905e3e8e | 745 | const struct field_t *son; |
5e35941d JMZ |
746 | unsigned char *beg = NULL; |
747 | ||
748 | PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name); | |
749 | ||
750 | /* Decode? */ | |
751 | base = (base && (f->attr & DECODE)) ? base + f->offset : NULL; | |
752 | ||
753 | /* Decode the choice index number */ | |
ec8a8f3c ES |
754 | if (nf_h323_error_boundary(bs, 0, 1)) |
755 | return H323_ERROR_BOUND; | |
5e35941d JMZ |
756 | if ((f->attr & EXT) && get_bit(bs)) { |
757 | ext = 1; | |
ec8a8f3c ES |
758 | if (nf_h323_error_boundary(bs, 0, 7)) |
759 | return H323_ERROR_BOUND; | |
5e35941d JMZ |
760 | type = get_bits(bs, 7) + f->lb; |
761 | } else { | |
762 | ext = 0; | |
ec8a8f3c ES |
763 | if (nf_h323_error_boundary(bs, 0, f->sz)) |
764 | return H323_ERROR_BOUND; | |
5e35941d | 765 | type = get_bits(bs, f->sz); |
25845b51 JMZ |
766 | if (type >= f->lb) |
767 | return H323_ERROR_RANGE; | |
5e35941d JMZ |
768 | } |
769 | ||
4228e2a9 PM |
770 | /* Write Type */ |
771 | if (base) | |
1f807d6e | 772 | *(unsigned int *)base = type; |
4228e2a9 | 773 | |
5e35941d JMZ |
774 | /* Check Range */ |
775 | if (type >= f->ub) { /* Newer version? */ | |
776 | BYTE_ALIGN(bs); | |
ec8a8f3c ES |
777 | if (nf_h323_error_boundary(bs, 2, 0)) |
778 | return H323_ERROR_BOUND; | |
5e35941d | 779 | len = get_len(bs); |
ec8a8f3c | 780 | if (nf_h323_error_boundary(bs, len, 0)) |
bc7d811a | 781 | return H323_ERROR_BOUND; |
5e35941d JMZ |
782 | bs->cur += len; |
783 | return H323_ERROR_NONE; | |
784 | } | |
785 | ||
5e35941d JMZ |
786 | /* Transfer to son level */ |
787 | son = &f->fields[type]; | |
788 | if (son->attr & STOP) { | |
789 | PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ", son->name); | |
790 | return H323_ERROR_STOP; | |
791 | } | |
792 | ||
793 | if (ext || (son->attr & OPEN)) { | |
794 | BYTE_ALIGN(bs); | |
ec8a8f3c ES |
795 | if (nf_h323_error_boundary(bs, len, 0)) |
796 | return H323_ERROR_BOUND; | |
5e35941d | 797 | len = get_len(bs); |
ec8a8f3c | 798 | if (nf_h323_error_boundary(bs, len, 0)) |
bc7d811a | 799 | return H323_ERROR_BOUND; |
5e35941d JMZ |
800 | if (!base || !(son->attr & DECODE)) { |
801 | PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ", | |
802 | son->name); | |
803 | bs->cur += len; | |
804 | return H323_ERROR_NONE; | |
805 | } | |
806 | beg = bs->cur; | |
807 | ||
7185989d PM |
808 | if ((err = (Decoders[son->type]) (bs, son, base, level + 1)) < |
809 | H323_ERROR_NONE) | |
5e35941d JMZ |
810 | return err; |
811 | ||
812 | bs->cur = beg + len; | |
813 | bs->bit = 0; | |
7185989d PM |
814 | } else if ((err = (Decoders[son->type]) (bs, son, base, level + 1)) < |
815 | H323_ERROR_NONE) | |
5e35941d JMZ |
816 | return err; |
817 | ||
818 | return H323_ERROR_NONE; | |
819 | } | |
820 | ||
1f807d6e | 821 | int DecodeRasMessage(unsigned char *buf, size_t sz, RasMessage *ras) |
5e35941d | 822 | { |
905e3e8e | 823 | static const struct field_t ras_message = { |
5e35941d JMZ |
824 | FNAME("RasMessage") CHOICE, 5, 24, 32, DECODE | EXT, |
825 | 0, _RasMessage | |
826 | }; | |
67704c2a | 827 | struct bitstr bs; |
5e35941d JMZ |
828 | |
829 | bs.buf = bs.beg = bs.cur = buf; | |
830 | bs.end = buf + sz; | |
831 | bs.bit = 0; | |
832 | ||
833 | return decode_choice(&bs, &ras_message, (char *) ras, 0); | |
834 | } | |
835 | ||
5e35941d | 836 | static int DecodeH323_UserInformation(unsigned char *buf, unsigned char *beg, |
1f807d6e | 837 | size_t sz, H323_UserInformation *uuie) |
5e35941d | 838 | { |
905e3e8e | 839 | static const struct field_t h323_userinformation = { |
5e35941d JMZ |
840 | FNAME("H323-UserInformation") SEQ, 1, 2, 2, DECODE | EXT, |
841 | 0, _H323_UserInformation | |
842 | }; | |
67704c2a | 843 | struct bitstr bs; |
5e35941d JMZ |
844 | |
845 | bs.buf = buf; | |
846 | bs.beg = bs.cur = beg; | |
847 | bs.end = beg + sz; | |
848 | bs.bit = 0; | |
849 | ||
850 | return decode_seq(&bs, &h323_userinformation, (char *) uuie, 0); | |
851 | } | |
852 | ||
5e35941d JMZ |
853 | int DecodeMultimediaSystemControlMessage(unsigned char *buf, size_t sz, |
854 | MultimediaSystemControlMessage * | |
855 | mscm) | |
856 | { | |
905e3e8e | 857 | static const struct field_t multimediasystemcontrolmessage = { |
5e35941d JMZ |
858 | FNAME("MultimediaSystemControlMessage") CHOICE, 2, 4, 4, |
859 | DECODE | EXT, 0, _MultimediaSystemControlMessage | |
860 | }; | |
67704c2a | 861 | struct bitstr bs; |
5e35941d JMZ |
862 | |
863 | bs.buf = bs.beg = bs.cur = buf; | |
864 | bs.end = buf + sz; | |
865 | bs.bit = 0; | |
866 | ||
867 | return decode_choice(&bs, &multimediasystemcontrolmessage, | |
868 | (char *) mscm, 0); | |
869 | } | |
870 | ||
1f807d6e | 871 | int DecodeQ931(unsigned char *buf, size_t sz, Q931 *q931) |
5e35941d JMZ |
872 | { |
873 | unsigned char *p = buf; | |
874 | int len; | |
875 | ||
876 | if (!p || sz < 1) | |
877 | return H323_ERROR_BOUND; | |
878 | ||
879 | /* Protocol Discriminator */ | |
880 | if (*p != 0x08) { | |
881 | PRINT("Unknown Protocol Discriminator\n"); | |
882 | return H323_ERROR_RANGE; | |
883 | } | |
884 | p++; | |
885 | sz--; | |
886 | ||
887 | /* CallReferenceValue */ | |
888 | if (sz < 1) | |
889 | return H323_ERROR_BOUND; | |
890 | len = *p++; | |
891 | sz--; | |
892 | if (sz < len) | |
893 | return H323_ERROR_BOUND; | |
894 | p += len; | |
895 | sz -= len; | |
896 | ||
897 | /* Message Type */ | |
c2b9b4fe | 898 | if (sz < 2) |
5e35941d JMZ |
899 | return H323_ERROR_BOUND; |
900 | q931->MessageType = *p++; | |
c2b9b4fe | 901 | sz--; |
5e35941d JMZ |
902 | PRINT("MessageType = %02X\n", q931->MessageType); |
903 | if (*p & 0x80) { | |
904 | p++; | |
905 | sz--; | |
906 | } | |
907 | ||
908 | /* Decode Information Elements */ | |
909 | while (sz > 0) { | |
910 | if (*p == 0x7e) { /* UserUserIE */ | |
911 | if (sz < 3) | |
912 | break; | |
913 | p++; | |
914 | len = *p++ << 8; | |
915 | len |= *p++; | |
916 | sz -= 3; | |
917 | if (sz < len) | |
918 | break; | |
919 | p++; | |
920 | len--; | |
921 | return DecodeH323_UserInformation(buf, p, len, | |
922 | &q931->UUIE); | |
923 | } | |
924 | p++; | |
925 | sz--; | |
926 | if (sz < 1) | |
927 | break; | |
928 | len = *p++; | |
e8daf27c | 929 | sz--; |
5e35941d JMZ |
930 | if (sz < len) |
931 | break; | |
932 | p += len; | |
933 | sz -= len; | |
934 | } | |
935 | ||
936 | PRINT("Q.931 UUIE not found\n"); | |
937 | ||
938 | return H323_ERROR_BOUND; | |
939 | } |