1 // SPDX-License-Identifier: GPL-2.0-only
3 * ip_conntrack_helper_h323_asn1.c - BER and PER decoding library for H.323
4 * conntrack/NAT module.
6 * Copyright (c) 2006 by Jing Min Zhao <zhaojingmin@users.sourceforge.net>
8 * See ip_conntrack_helper_h323_asn1.h for details.
12 #include <linux/kernel.h>
16 #include <linux/netfilter/nf_conntrack_h323_asn1.h>
25 #define IFTHEN(cond, act) if(cond){act;}
31 #define FNAME(name) name,
33 #define IFTHEN(cond, act)
34 #define PRINT(fmt, args...)
59 /* Constraint Types */
61 /* #define BITS 1-8 */
68 /* ASN.1 Type Attributes */
77 /* ASN.1 Field Structure */
78 typedef struct field_t {
87 unsigned short offset;
88 const struct field_t *fields;
101 #define INC_BIT(bs) if((++(bs)->bit)>7){(bs)->cur++;(bs)->bit=0;}
102 #define INC_BITS(bs,b) if(((bs)->bit+=(b))>7){(bs)->cur+=(bs)->bit>>3;(bs)->bit&=7;}
103 #define BYTE_ALIGN(bs) if((bs)->bit){(bs)->cur++;(bs)->bit=0;}
104 static unsigned int get_len(struct bitstr *bs);
105 static unsigned int get_bit(struct bitstr *bs);
106 static unsigned int get_bits(struct bitstr *bs, unsigned int b);
107 static unsigned int get_bitmap(struct bitstr *bs, unsigned int b);
108 static unsigned int get_uint(struct bitstr *bs, int b);
110 /* Decoder Functions */
111 static int decode_nul(struct bitstr *bs, const struct field_t *f, char *base, int level);
112 static int decode_bool(struct bitstr *bs, const struct field_t *f, char *base, int level);
113 static int decode_oid(struct bitstr *bs, const struct field_t *f, char *base, int level);
114 static int decode_int(struct bitstr *bs, const struct field_t *f, char *base, int level);
115 static int decode_enum(struct bitstr *bs, const struct field_t *f, char *base, int level);
116 static int decode_bitstr(struct bitstr *bs, const struct field_t *f, char *base, int level);
117 static int decode_numstr(struct bitstr *bs, const struct field_t *f, char *base, int level);
118 static int decode_octstr(struct bitstr *bs, const struct field_t *f, char *base, int level);
119 static int decode_bmpstr(struct bitstr *bs, const struct field_t *f, char *base, int level);
120 static int decode_seq(struct bitstr *bs, const struct field_t *f, char *base, int level);
121 static int decode_seqof(struct bitstr *bs, const struct field_t *f, char *base, int level);
122 static int decode_choice(struct bitstr *bs, const struct field_t *f, char *base, int level);
124 /* Decoder Functions Vector */
125 typedef int (*decoder_t)(struct bitstr *, const struct field_t *, char *, int);
126 static const decoder_t Decoders[] = {
144 #include "nf_conntrack_h323_types.c"
150 /* Assume bs is aligned && v < 16384 */
151 static unsigned int get_len(struct bitstr *bs)
166 static int nf_h323_error_boundary(struct bitstr *bs, size_t bytes, size_t bits)
169 bytes += bits / BITS_PER_BYTE;
170 if (bits % BITS_PER_BYTE > 0)
173 if (bs->cur + bytes > bs->end)
179 static unsigned int get_bit(struct bitstr *bs)
181 unsigned int b = (*bs->cur) & (0x80 >> bs->bit);
189 static unsigned int get_bits(struct bitstr *bs, unsigned int b)
193 v = (*bs->cur) & (0xffU >> bs->bit);
214 static unsigned int get_bitmap(struct bitstr *bs, unsigned int b)
216 unsigned int v, l, shift, bytes;
224 v = (unsigned int)(*bs->cur) << (bs->bit + 24);
227 v = (unsigned int)(*bs->cur++) << (bs->bit + 24);
230 for (bytes = l >> 3, shift = 24, v = 0; bytes;
232 v |= (unsigned int)(*bs->cur++) << shift;
235 v |= (unsigned int)(*bs->cur) << shift;
239 v |= (*bs->cur) >> (8 - bs->bit);
245 v &= 0xffffffff << (32 - b);
251 * Assume bs is aligned and sizeof(unsigned int) == 4
253 static unsigned int get_uint(struct bitstr *bs, int b)
277 static int decode_nul(struct bitstr *bs, const struct field_t *f,
278 char *base, int level)
280 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
282 return H323_ERROR_NONE;
285 static int decode_bool(struct bitstr *bs, const struct field_t *f,
286 char *base, int level)
288 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
291 if (nf_h323_error_boundary(bs, 0, 0))
292 return H323_ERROR_BOUND;
293 return H323_ERROR_NONE;
296 static int decode_oid(struct bitstr *bs, const struct field_t *f,
297 char *base, int level)
301 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
304 if (nf_h323_error_boundary(bs, 1, 0))
305 return H323_ERROR_BOUND;
309 if (nf_h323_error_boundary(bs, 0, 0))
310 return H323_ERROR_BOUND;
312 return H323_ERROR_NONE;
315 static int decode_int(struct bitstr *bs, const struct field_t *f,
316 char *base, int level)
320 PRINT("%*.s%s", level * TAB_SIZE, " ", f->name);
323 case BYTE: /* Range == 256 */
327 case WORD: /* 257 <= Range <= 64K */
331 case CONS: /* 64K < Range < 4G */
332 if (nf_h323_error_boundary(bs, 0, 2))
333 return H323_ERROR_BOUND;
334 len = get_bits(bs, 2) + 1;
336 if (base && (f->attr & DECODE)) { /* timeToLive */
337 unsigned int v = get_uint(bs, len) + f->lb;
339 *((unsigned int *)(base + f->offset)) = v;
345 if (nf_h323_error_boundary(bs, 2, 0))
346 return H323_ERROR_BOUND;
350 default: /* 2 <= Range <= 255 */
357 if (nf_h323_error_boundary(bs, 0, 0))
358 return H323_ERROR_BOUND;
359 return H323_ERROR_NONE;
362 static int decode_enum(struct bitstr *bs, const struct field_t *f,
363 char *base, int level)
365 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
367 if ((f->attr & EXT) && get_bit(bs)) {
373 if (nf_h323_error_boundary(bs, 0, 0))
374 return H323_ERROR_BOUND;
375 return H323_ERROR_NONE;
378 static int decode_bitstr(struct bitstr *bs, const struct field_t *f,
379 char *base, int level)
383 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
387 case FIXD: /* fixed length > 16 */
390 case WORD: /* 2-byte length */
391 if (nf_h323_error_boundary(bs, 2, 0))
392 return H323_ERROR_BOUND;
393 len = (*bs->cur++) << 8;
394 len += (*bs->cur++) + f->lb;
397 if (nf_h323_error_boundary(bs, 2, 0))
398 return H323_ERROR_BOUND;
409 if (nf_h323_error_boundary(bs, 0, 0))
410 return H323_ERROR_BOUND;
411 return H323_ERROR_NONE;
414 static int decode_numstr(struct bitstr *bs, const struct field_t *f,
415 char *base, int level)
419 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
421 /* 2 <= Range <= 255 */
422 if (nf_h323_error_boundary(bs, 0, f->sz))
423 return H323_ERROR_BOUND;
424 len = get_bits(bs, f->sz) + f->lb;
427 INC_BITS(bs, (len << 2));
429 if (nf_h323_error_boundary(bs, 0, 0))
430 return H323_ERROR_BOUND;
431 return H323_ERROR_NONE;
434 static int decode_octstr(struct bitstr *bs, const struct field_t *f,
435 char *base, int level)
439 PRINT("%*.s%s", level * TAB_SIZE, " ", f->name);
442 case FIXD: /* Range == 1 */
445 if (base && (f->attr & DECODE)) {
448 PRINT(" = %d.%d.%d.%d:%d",
449 bs->cur[0], bs->cur[1],
450 bs->cur[2], bs->cur[3],
451 bs->cur[4] * 256 + bs->cur[5]));
452 *((unsigned int *)(base + f->offset)) =
458 case BYTE: /* Range == 256 */
460 if (nf_h323_error_boundary(bs, 1, 0))
461 return H323_ERROR_BOUND;
462 len = (*bs->cur++) + f->lb;
466 if (nf_h323_error_boundary(bs, 2, 0))
467 return H323_ERROR_BOUND;
468 len = get_len(bs) + f->lb;
470 default: /* 2 <= Range <= 255 */
471 if (nf_h323_error_boundary(bs, 0, f->sz))
472 return H323_ERROR_BOUND;
473 len = get_bits(bs, f->sz) + f->lb;
482 if (nf_h323_error_boundary(bs, 0, 0))
483 return H323_ERROR_BOUND;
484 return H323_ERROR_NONE;
487 static int decode_bmpstr(struct bitstr *bs, const struct field_t *f,
488 char *base, int level)
492 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
495 case BYTE: /* Range == 256 */
497 if (nf_h323_error_boundary(bs, 1, 0))
498 return H323_ERROR_BOUND;
499 len = (*bs->cur++) + f->lb;
501 default: /* 2 <= Range <= 255 */
502 if (nf_h323_error_boundary(bs, 0, f->sz))
503 return H323_ERROR_BOUND;
504 len = get_bits(bs, f->sz) + f->lb;
511 if (nf_h323_error_boundary(bs, 0, 0))
512 return H323_ERROR_BOUND;
513 return H323_ERROR_NONE;
516 static int decode_seq(struct bitstr *bs, const struct field_t *f,
517 char *base, int level)
519 unsigned int ext, bmp, i, opt, len = 0, bmp2, bmp2_len;
521 const struct field_t *son;
522 unsigned char *beg = NULL;
524 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
527 base = (base && (f->attr & DECODE)) ? base + f->offset : NULL;
530 if (nf_h323_error_boundary(bs, 0, 1))
531 return H323_ERROR_BOUND;
532 ext = (f->attr & EXT) ? get_bit(bs) : 0;
534 /* Get fields bitmap */
535 if (nf_h323_error_boundary(bs, 0, f->sz))
536 return H323_ERROR_BOUND;
537 bmp = get_bitmap(bs, f->sz);
539 *(unsigned int *)base = bmp;
541 /* Decode the root components */
542 for (i = opt = 0, son = f->fields; i < f->lb; i++, son++) {
543 if (son->attr & STOP) {
544 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
546 return H323_ERROR_STOP;
549 if (son->attr & OPT) { /* Optional component */
550 if (!((0x80000000U >> (opt++)) & bmp)) /* Not exist */
555 if (son->attr & OPEN) { /* Open field */
556 if (nf_h323_error_boundary(bs, 2, 0))
557 return H323_ERROR_BOUND;
559 if (nf_h323_error_boundary(bs, len, 0))
560 return H323_ERROR_BOUND;
561 if (!base || !(son->attr & DECODE)) {
562 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE,
570 if ((err = (Decoders[son->type]) (bs, son, base,
577 } else if ((err = (Decoders[son->type]) (bs, son, base,
585 return H323_ERROR_NONE;
587 /* Get the extension bitmap */
588 if (nf_h323_error_boundary(bs, 0, 7))
589 return H323_ERROR_BOUND;
590 bmp2_len = get_bits(bs, 7) + 1;
591 if (nf_h323_error_boundary(bs, 0, bmp2_len))
592 return H323_ERROR_BOUND;
593 bmp2 = get_bitmap(bs, bmp2_len);
594 bmp |= bmp2 >> f->sz;
596 *(unsigned int *)base = bmp;
599 /* Decode the extension components */
600 for (opt = 0; opt < bmp2_len; opt++, i++, son++) {
602 if (i >= f->ub) { /* Newer Version? */
603 if (nf_h323_error_boundary(bs, 2, 0))
604 return H323_ERROR_BOUND;
606 if (nf_h323_error_boundary(bs, len, 0))
607 return H323_ERROR_BOUND;
612 if (son->attr & STOP) {
613 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
615 return H323_ERROR_STOP;
618 if (!((0x80000000 >> opt) & bmp2)) /* Not present */
621 if (nf_h323_error_boundary(bs, 2, 0))
622 return H323_ERROR_BOUND;
624 if (nf_h323_error_boundary(bs, len, 0))
625 return H323_ERROR_BOUND;
626 if (!base || !(son->attr & DECODE)) {
627 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
634 if ((err = (Decoders[son->type]) (bs, son, base,
642 return H323_ERROR_NONE;
645 static int decode_seqof(struct bitstr *bs, const struct field_t *f,
646 char *base, int level)
648 unsigned int count, effective_count = 0, i, len = 0;
650 const struct field_t *son;
651 unsigned char *beg = NULL;
653 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
656 base = (base && (f->attr & DECODE)) ? base + f->offset : NULL;
658 /* Decode item count */
662 if (nf_h323_error_boundary(bs, 1, 0))
663 return H323_ERROR_BOUND;
668 if (nf_h323_error_boundary(bs, 2, 0))
669 return H323_ERROR_BOUND;
676 if (nf_h323_error_boundary(bs, 2, 0))
677 return H323_ERROR_BOUND;
681 if (nf_h323_error_boundary(bs, 0, f->sz))
682 return H323_ERROR_BOUND;
683 count = get_bits(bs, f->sz);
690 effective_count = count > f->ub ? f->ub : count;
691 *(unsigned int *)base = effective_count;
692 base += sizeof(unsigned int);
695 /* Decode nested field */
699 for (i = 0; i < count; i++) {
700 if (son->attr & OPEN) {
702 if (nf_h323_error_boundary(bs, 2, 0))
703 return H323_ERROR_BOUND;
705 if (nf_h323_error_boundary(bs, len, 0))
706 return H323_ERROR_BOUND;
707 if (!base || !(son->attr & DECODE)) {
708 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE,
715 if ((err = (Decoders[son->type]) (bs, son,
726 if ((err = (Decoders[son->type]) (bs, son,
738 return H323_ERROR_NONE;
741 static int decode_choice(struct bitstr *bs, const struct field_t *f,
742 char *base, int level)
744 unsigned int type, ext, len = 0;
746 const struct field_t *son;
747 unsigned char *beg = NULL;
749 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
752 base = (base && (f->attr & DECODE)) ? base + f->offset : NULL;
754 /* Decode the choice index number */
755 if (nf_h323_error_boundary(bs, 0, 1))
756 return H323_ERROR_BOUND;
757 if ((f->attr & EXT) && get_bit(bs)) {
759 if (nf_h323_error_boundary(bs, 0, 7))
760 return H323_ERROR_BOUND;
761 type = get_bits(bs, 7) + f->lb;
764 if (nf_h323_error_boundary(bs, 0, f->sz))
765 return H323_ERROR_BOUND;
766 type = get_bits(bs, f->sz);
768 return H323_ERROR_RANGE;
773 *(unsigned int *)base = type;
776 if (type >= f->ub) { /* Newer version? */
778 if (nf_h323_error_boundary(bs, 2, 0))
779 return H323_ERROR_BOUND;
781 if (nf_h323_error_boundary(bs, len, 0))
782 return H323_ERROR_BOUND;
784 return H323_ERROR_NONE;
787 /* Transfer to son level */
788 son = &f->fields[type];
789 if (son->attr & STOP) {
790 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ", son->name);
791 return H323_ERROR_STOP;
794 if (ext || (son->attr & OPEN)) {
796 if (nf_h323_error_boundary(bs, len, 0))
797 return H323_ERROR_BOUND;
799 if (nf_h323_error_boundary(bs, len, 0))
800 return H323_ERROR_BOUND;
801 if (!base || !(son->attr & DECODE)) {
802 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
805 return H323_ERROR_NONE;
809 if ((err = (Decoders[son->type]) (bs, son, base, level + 1)) <
815 } else if ((err = (Decoders[son->type]) (bs, son, base, level + 1)) <
819 return H323_ERROR_NONE;
822 int DecodeRasMessage(unsigned char *buf, size_t sz, RasMessage *ras)
824 static const struct field_t ras_message = {
825 FNAME("RasMessage") CHOICE, 5, 24, 32, DECODE | EXT,
830 bs.buf = bs.beg = bs.cur = buf;
834 return decode_choice(&bs, &ras_message, (char *) ras, 0);
837 static int DecodeH323_UserInformation(unsigned char *buf, unsigned char *beg,
838 size_t sz, H323_UserInformation *uuie)
840 static const struct field_t h323_userinformation = {
841 FNAME("H323-UserInformation") SEQ, 1, 2, 2, DECODE | EXT,
842 0, _H323_UserInformation
847 bs.beg = bs.cur = beg;
851 return decode_seq(&bs, &h323_userinformation, (char *) uuie, 0);
854 int DecodeMultimediaSystemControlMessage(unsigned char *buf, size_t sz,
855 MultimediaSystemControlMessage *
858 static const struct field_t multimediasystemcontrolmessage = {
859 FNAME("MultimediaSystemControlMessage") CHOICE, 2, 4, 4,
860 DECODE | EXT, 0, _MultimediaSystemControlMessage
864 bs.buf = bs.beg = bs.cur = buf;
868 return decode_choice(&bs, &multimediasystemcontrolmessage,
872 int DecodeQ931(unsigned char *buf, size_t sz, Q931 *q931)
874 unsigned char *p = buf;
878 return H323_ERROR_BOUND;
880 /* Protocol Discriminator */
882 PRINT("Unknown Protocol Discriminator\n");
883 return H323_ERROR_RANGE;
888 /* CallReferenceValue */
890 return H323_ERROR_BOUND;
894 return H323_ERROR_BOUND;
900 return H323_ERROR_BOUND;
901 q931->MessageType = *p++;
903 PRINT("MessageType = %02X\n", q931->MessageType);
909 /* Decode Information Elements */
911 if (*p == 0x7e) { /* UserUserIE */
922 return DecodeH323_UserInformation(buf, p, len,
937 PRINT("Q.931 UUIE not found\n");
939 return H323_ERROR_BOUND;