1 // SPDX-License-Identifier: GPL-2.0-only
3 * BER and PER decoding library for H.323 conntrack/NAT module.
5 * Copyright (c) 2006 by Jing Min Zhao <zhaojingmin@users.sourceforge.net>
7 * See nf_conntrack_helper_h323_asn1.h for details.
11 #include <linux/kernel.h>
15 #include <linux/netfilter/nf_conntrack_h323_asn1.h>
24 #define IFTHEN(cond, act) if(cond){act;}
30 #define FNAME(name) name,
32 #define IFTHEN(cond, act)
33 #define PRINT(fmt, args...)
58 /* Constraint Types */
60 /* #define BITS 1-8 */
67 /* ASN.1 Type Attributes */
76 /* ASN.1 Field Structure */
77 typedef struct field_t {
86 unsigned short offset;
87 const struct field_t *fields;
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;}
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);
109 /* Decoder Functions */
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);
123 /* Decoder Functions Vector */
124 typedef int (*decoder_t)(struct bitstr *, const struct field_t *, char *, int);
125 static const decoder_t Decoders[] = {
143 #include "nf_conntrack_h323_types.c"
149 /* Assume bs is aligned && v < 16384 */
150 static unsigned int get_len(struct bitstr *bs)
165 static int nf_h323_error_boundary(struct bitstr *bs, size_t bytes, size_t bits)
168 bytes += bits / BITS_PER_BYTE;
169 if (bits % BITS_PER_BYTE > 0)
172 if (bs->cur + bytes > bs->end)
178 static unsigned int get_bit(struct bitstr *bs)
180 unsigned int b = (*bs->cur) & (0x80 >> bs->bit);
188 static unsigned int get_bits(struct bitstr *bs, unsigned int b)
192 v = (*bs->cur) & (0xffU >> bs->bit);
213 static unsigned int get_bitmap(struct bitstr *bs, unsigned int b)
215 unsigned int v, l, shift, bytes;
223 v = (unsigned int)(*bs->cur) << (bs->bit + 24);
226 v = (unsigned int)(*bs->cur++) << (bs->bit + 24);
229 for (bytes = l >> 3, shift = 24, v = 0; bytes;
231 v |= (unsigned int)(*bs->cur++) << shift;
234 v |= (unsigned int)(*bs->cur) << shift;
238 v |= (*bs->cur) >> (8 - bs->bit);
244 v &= 0xffffffff << (32 - b);
250 * Assume bs is aligned and sizeof(unsigned int) == 4
252 static unsigned int get_uint(struct bitstr *bs, int b)
276 static int decode_nul(struct bitstr *bs, const struct field_t *f,
277 char *base, int level)
279 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
281 return H323_ERROR_NONE;
284 static int decode_bool(struct bitstr *bs, const struct field_t *f,
285 char *base, int level)
287 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
290 if (nf_h323_error_boundary(bs, 0, 0))
291 return H323_ERROR_BOUND;
292 return H323_ERROR_NONE;
295 static int decode_oid(struct bitstr *bs, const struct field_t *f,
296 char *base, int level)
300 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
303 if (nf_h323_error_boundary(bs, 1, 0))
304 return H323_ERROR_BOUND;
308 if (nf_h323_error_boundary(bs, 0, 0))
309 return H323_ERROR_BOUND;
311 return H323_ERROR_NONE;
314 static int decode_int(struct bitstr *bs, const struct field_t *f,
315 char *base, int level)
319 PRINT("%*.s%s", level * TAB_SIZE, " ", f->name);
322 case BYTE: /* Range == 256 */
326 case WORD: /* 257 <= Range <= 64K */
330 case CONS: /* 64K < Range < 4G */
331 if (nf_h323_error_boundary(bs, 0, 2))
332 return H323_ERROR_BOUND;
333 len = get_bits(bs, 2) + 1;
335 if (base && (f->attr & DECODE)) { /* timeToLive */
336 unsigned int v = get_uint(bs, len) + f->lb;
338 *((unsigned int *)(base + f->offset)) = v;
344 if (nf_h323_error_boundary(bs, 2, 0))
345 return H323_ERROR_BOUND;
349 default: /* 2 <= Range <= 255 */
356 if (nf_h323_error_boundary(bs, 0, 0))
357 return H323_ERROR_BOUND;
358 return H323_ERROR_NONE;
361 static int decode_enum(struct bitstr *bs, const struct field_t *f,
362 char *base, int level)
364 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
366 if ((f->attr & EXT) && get_bit(bs)) {
372 if (nf_h323_error_boundary(bs, 0, 0))
373 return H323_ERROR_BOUND;
374 return H323_ERROR_NONE;
377 static int decode_bitstr(struct bitstr *bs, const struct field_t *f,
378 char *base, int level)
382 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
386 case FIXD: /* fixed length > 16 */
389 case WORD: /* 2-byte length */
390 if (nf_h323_error_boundary(bs, 2, 0))
391 return H323_ERROR_BOUND;
392 len = (*bs->cur++) << 8;
393 len += (*bs->cur++) + f->lb;
396 if (nf_h323_error_boundary(bs, 2, 0))
397 return H323_ERROR_BOUND;
408 if (nf_h323_error_boundary(bs, 0, 0))
409 return H323_ERROR_BOUND;
410 return H323_ERROR_NONE;
413 static int decode_numstr(struct bitstr *bs, const struct field_t *f,
414 char *base, int level)
418 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
420 /* 2 <= Range <= 255 */
421 if (nf_h323_error_boundary(bs, 0, f->sz))
422 return H323_ERROR_BOUND;
423 len = get_bits(bs, f->sz) + f->lb;
426 INC_BITS(bs, (len << 2));
428 if (nf_h323_error_boundary(bs, 0, 0))
429 return H323_ERROR_BOUND;
430 return H323_ERROR_NONE;
433 static int decode_octstr(struct bitstr *bs, const struct field_t *f,
434 char *base, int level)
438 PRINT("%*.s%s", level * TAB_SIZE, " ", f->name);
441 case FIXD: /* Range == 1 */
444 if (base && (f->attr & DECODE)) {
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]));
451 *((unsigned int *)(base + f->offset)) =
457 case BYTE: /* Range == 256 */
459 if (nf_h323_error_boundary(bs, 1, 0))
460 return H323_ERROR_BOUND;
461 len = (*bs->cur++) + f->lb;
465 if (nf_h323_error_boundary(bs, 2, 0))
466 return H323_ERROR_BOUND;
467 len = get_len(bs) + f->lb;
469 default: /* 2 <= Range <= 255 */
470 if (nf_h323_error_boundary(bs, 0, f->sz))
471 return H323_ERROR_BOUND;
472 len = get_bits(bs, f->sz) + f->lb;
481 if (nf_h323_error_boundary(bs, 0, 0))
482 return H323_ERROR_BOUND;
483 return H323_ERROR_NONE;
486 static int decode_bmpstr(struct bitstr *bs, const struct field_t *f,
487 char *base, int level)
491 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
494 case BYTE: /* Range == 256 */
496 if (nf_h323_error_boundary(bs, 1, 0))
497 return H323_ERROR_BOUND;
498 len = (*bs->cur++) + f->lb;
500 default: /* 2 <= Range <= 255 */
501 if (nf_h323_error_boundary(bs, 0, f->sz))
502 return H323_ERROR_BOUND;
503 len = get_bits(bs, f->sz) + f->lb;
510 if (nf_h323_error_boundary(bs, 0, 0))
511 return H323_ERROR_BOUND;
512 return H323_ERROR_NONE;
515 static int decode_seq(struct bitstr *bs, const struct field_t *f,
516 char *base, int level)
518 unsigned int ext, bmp, i, opt, len = 0, bmp2, bmp2_len;
520 const struct field_t *son;
521 unsigned char *beg = NULL;
523 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
526 base = (base && (f->attr & DECODE)) ? base + f->offset : NULL;
529 if (nf_h323_error_boundary(bs, 0, 1))
530 return H323_ERROR_BOUND;
531 ext = (f->attr & EXT) ? get_bit(bs) : 0;
533 /* Get fields bitmap */
534 if (nf_h323_error_boundary(bs, 0, f->sz))
535 return H323_ERROR_BOUND;
537 return H323_ERROR_RANGE;
538 bmp = get_bitmap(bs, f->sz);
540 *(unsigned int *)base = bmp;
542 /* Decode the root components */
543 for (i = opt = 0, son = f->fields; i < f->lb; i++, son++) {
544 if (son->attr & STOP) {
545 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
547 return H323_ERROR_STOP;
550 if (son->attr & OPT) { /* Optional component */
551 if (!((0x80000000U >> (opt++)) & bmp)) /* Not exist */
556 if (son->attr & OPEN) { /* Open field */
557 if (nf_h323_error_boundary(bs, 2, 0))
558 return H323_ERROR_BOUND;
560 if (nf_h323_error_boundary(bs, len, 0))
561 return H323_ERROR_BOUND;
562 if (!base || !(son->attr & DECODE)) {
563 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE,
571 if ((err = (Decoders[son->type]) (bs, son, base,
578 } else if ((err = (Decoders[son->type]) (bs, son, base,
586 return H323_ERROR_NONE;
588 /* Get the extension bitmap */
589 if (nf_h323_error_boundary(bs, 0, 7))
590 return H323_ERROR_BOUND;
591 bmp2_len = get_bits(bs, 7) + 1;
592 if (nf_h323_error_boundary(bs, 0, bmp2_len))
593 return H323_ERROR_BOUND;
595 return H323_ERROR_RANGE;
596 bmp2 = get_bitmap(bs, bmp2_len);
597 bmp |= bmp2 >> f->sz;
599 *(unsigned int *)base = bmp;
602 /* Decode the extension components */
603 for (opt = 0; opt < bmp2_len; opt++, i++, son++) {
605 if (i >= f->ub) { /* Newer Version? */
606 if (nf_h323_error_boundary(bs, 2, 0))
607 return H323_ERROR_BOUND;
609 if (nf_h323_error_boundary(bs, len, 0))
610 return H323_ERROR_BOUND;
615 if (son->attr & STOP) {
616 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
618 return H323_ERROR_STOP;
621 if (!((0x80000000 >> opt) & bmp2)) /* Not present */
624 if (nf_h323_error_boundary(bs, 2, 0))
625 return H323_ERROR_BOUND;
627 if (nf_h323_error_boundary(bs, len, 0))
628 return H323_ERROR_BOUND;
629 if (!base || !(son->attr & DECODE)) {
630 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
637 if ((err = (Decoders[son->type]) (bs, son, base,
645 return H323_ERROR_NONE;
648 static int decode_seqof(struct bitstr *bs, const struct field_t *f,
649 char *base, int level)
651 unsigned int count, effective_count = 0, i, len = 0;
653 const struct field_t *son;
654 unsigned char *beg = NULL;
656 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
659 base = (base && (f->attr & DECODE)) ? base + f->offset : NULL;
661 /* Decode item count */
665 if (nf_h323_error_boundary(bs, 1, 0))
666 return H323_ERROR_BOUND;
671 if (nf_h323_error_boundary(bs, 2, 0))
672 return H323_ERROR_BOUND;
679 if (nf_h323_error_boundary(bs, 2, 0))
680 return H323_ERROR_BOUND;
684 if (nf_h323_error_boundary(bs, 0, f->sz))
685 return H323_ERROR_BOUND;
686 count = get_bits(bs, f->sz);
693 effective_count = count > f->ub ? f->ub : count;
694 *(unsigned int *)base = effective_count;
695 base += sizeof(unsigned int);
698 /* Decode nested field */
702 for (i = 0; i < count; i++) {
703 if (son->attr & OPEN) {
705 if (nf_h323_error_boundary(bs, 2, 0))
706 return H323_ERROR_BOUND;
708 if (nf_h323_error_boundary(bs, len, 0))
709 return H323_ERROR_BOUND;
710 if (!base || !(son->attr & DECODE)) {
711 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE,
718 if ((err = (Decoders[son->type]) (bs, son,
729 if ((err = (Decoders[son->type]) (bs, son,
741 return H323_ERROR_NONE;
744 static int decode_choice(struct bitstr *bs, const struct field_t *f,
745 char *base, int level)
747 unsigned int type, ext, len = 0;
749 const struct field_t *son;
750 unsigned char *beg = NULL;
752 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
755 base = (base && (f->attr & DECODE)) ? base + f->offset : NULL;
757 /* Decode the choice index number */
758 if (nf_h323_error_boundary(bs, 0, 1))
759 return H323_ERROR_BOUND;
760 if ((f->attr & EXT) && get_bit(bs)) {
762 if (nf_h323_error_boundary(bs, 0, 7))
763 return H323_ERROR_BOUND;
764 type = get_bits(bs, 7) + f->lb;
767 if (nf_h323_error_boundary(bs, 0, f->sz))
768 return H323_ERROR_BOUND;
769 type = get_bits(bs, f->sz);
771 return H323_ERROR_RANGE;
776 *(unsigned int *)base = type;
779 if (type >= f->ub) { /* Newer version? */
781 if (nf_h323_error_boundary(bs, 2, 0))
782 return H323_ERROR_BOUND;
784 if (nf_h323_error_boundary(bs, len, 0))
785 return H323_ERROR_BOUND;
787 return H323_ERROR_NONE;
790 /* Transfer to son level */
791 son = &f->fields[type];
792 if (son->attr & STOP) {
793 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ", son->name);
794 return H323_ERROR_STOP;
797 if (ext || (son->attr & OPEN)) {
799 if (nf_h323_error_boundary(bs, len, 0))
800 return H323_ERROR_BOUND;
802 if (nf_h323_error_boundary(bs, len, 0))
803 return H323_ERROR_BOUND;
804 if (!base || !(son->attr & DECODE)) {
805 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
808 return H323_ERROR_NONE;
812 if ((err = (Decoders[son->type]) (bs, son, base, level + 1)) <
818 } else if ((err = (Decoders[son->type]) (bs, son, base, level + 1)) <
822 return H323_ERROR_NONE;
825 int DecodeRasMessage(unsigned char *buf, size_t sz, RasMessage *ras)
827 static const struct field_t ras_message = {
828 FNAME("RasMessage") CHOICE, 5, 24, 32, DECODE | EXT,
833 bs.buf = bs.beg = bs.cur = buf;
837 return decode_choice(&bs, &ras_message, (char *) ras, 0);
840 static int DecodeH323_UserInformation(unsigned char *buf, unsigned char *beg,
841 size_t sz, H323_UserInformation *uuie)
843 static const struct field_t h323_userinformation = {
844 FNAME("H323-UserInformation") SEQ, 1, 2, 2, DECODE | EXT,
845 0, _H323_UserInformation
850 bs.beg = bs.cur = beg;
854 return decode_seq(&bs, &h323_userinformation, (char *) uuie, 0);
857 int DecodeMultimediaSystemControlMessage(unsigned char *buf, size_t sz,
858 MultimediaSystemControlMessage *
861 static const struct field_t multimediasystemcontrolmessage = {
862 FNAME("MultimediaSystemControlMessage") CHOICE, 2, 4, 4,
863 DECODE | EXT, 0, _MultimediaSystemControlMessage
867 bs.buf = bs.beg = bs.cur = buf;
871 return decode_choice(&bs, &multimediasystemcontrolmessage,
875 int DecodeQ931(unsigned char *buf, size_t sz, Q931 *q931)
877 unsigned char *p = buf;
881 return H323_ERROR_BOUND;
883 /* Protocol Discriminator */
885 PRINT("Unknown Protocol Discriminator\n");
886 return H323_ERROR_RANGE;
891 /* CallReferenceValue */
893 return H323_ERROR_BOUND;
897 return H323_ERROR_BOUND;
903 return H323_ERROR_BOUND;
904 q931->MessageType = *p++;
906 PRINT("MessageType = %02X\n", q931->MessageType);
912 /* Decode Information Elements */
914 if (*p == 0x7e) { /* UserUserIE */
925 return DecodeH323_UserInformation(buf, p, len,
940 PRINT("Q.931 UUIE not found\n");
942 return H323_ERROR_BOUND;