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