2 * ip_conntrack_helper_h323_asn1.c - BER and PER decoding library for H.323
3 * conntrack/NAT module.
5 * Copyright (c) 2006 by Jing Min Zhao <zhaojingmin@users.sourceforge.net>
7 * This source code is licensed under General Public License version 2.
9 * See ip_conntrack_helper_h323_asn1.h for details.
14 #include <linux/kernel.h>
18 #include <linux/netfilter/nf_conntrack_h323_asn1.h>
27 #define IFTHEN(cond, act) if(cond){act;}
33 #define FNAME(name) name,
35 #define IFTHEN(cond, act)
36 #define PRINT(fmt, args...)
61 /* Constraint Types */
63 /* #define BITS 1-8 */
70 /* ASN.1 Type Attributes */
79 /* ASN.1 Field Structure */
80 typedef struct field_t {
89 unsigned short offset;
90 const struct field_t *fields;
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;}
106 static unsigned int get_len(struct bitstr *bs);
107 static unsigned int get_bit(struct bitstr *bs);
108 static unsigned int get_bits(struct bitstr *bs, unsigned int b);
109 static unsigned int get_bitmap(struct bitstr *bs, unsigned int b);
110 static unsigned int get_uint(struct bitstr *bs, int b);
112 /* Decoder Functions */
113 static int decode_nul(struct bitstr *bs, const struct field_t *f, char *base, int level);
114 static int decode_bool(struct bitstr *bs, const struct field_t *f, char *base, int level);
115 static int decode_oid(struct bitstr *bs, const struct field_t *f, char *base, int level);
116 static int decode_int(struct bitstr *bs, const struct field_t *f, char *base, int level);
117 static int decode_enum(struct bitstr *bs, const struct field_t *f, char *base, int level);
118 static int decode_bitstr(struct bitstr *bs, const struct field_t *f, char *base, int level);
119 static int decode_numstr(struct bitstr *bs, const struct field_t *f, char *base, int level);
120 static int decode_octstr(struct bitstr *bs, const struct field_t *f, char *base, int level);
121 static int decode_bmpstr(struct bitstr *bs, const struct field_t *f, char *base, int level);
122 static int decode_seq(struct bitstr *bs, const struct field_t *f, char *base, int level);
123 static int decode_seqof(struct bitstr *bs, const struct field_t *f, char *base, int level);
124 static int decode_choice(struct bitstr *bs, const struct field_t *f, char *base, int level);
126 /* Decoder Functions Vector */
127 typedef int (*decoder_t)(struct bitstr *, const struct field_t *, char *, int);
128 static const decoder_t Decoders[] = {
146 #include "nf_conntrack_h323_types.c"
152 /* Assume bs is aligned && v < 16384 */
153 static unsigned int get_len(struct bitstr *bs)
168 static int nf_h323_error_boundary(struct bitstr *bs, size_t bytes, size_t bits)
171 bytes += bits / BITS_PER_BYTE;
172 if (bits % BITS_PER_BYTE > 0)
175 if (*bs->cur + bytes > *bs->end)
181 static unsigned int get_bit(struct bitstr *bs)
183 unsigned int b = (*bs->cur) & (0x80 >> bs->bit);
191 static unsigned int get_bits(struct bitstr *bs, unsigned int b)
195 v = (*bs->cur) & (0xffU >> bs->bit);
216 static unsigned int get_bitmap(struct bitstr *bs, unsigned int b)
218 unsigned int v, l, shift, bytes;
226 v = (unsigned int)(*bs->cur) << (bs->bit + 24);
229 v = (unsigned int)(*bs->cur++) << (bs->bit + 24);
232 for (bytes = l >> 3, shift = 24, v = 0; bytes;
234 v |= (unsigned int)(*bs->cur++) << shift;
237 v |= (unsigned int)(*bs->cur) << shift;
241 v |= (*bs->cur) >> (8 - bs->bit);
247 v &= 0xffffffff << (32 - b);
253 * Assume bs is aligned and sizeof(unsigned int) == 4
255 static unsigned int get_uint(struct bitstr *bs, int b)
279 static int decode_nul(struct bitstr *bs, const struct field_t *f,
280 char *base, int level)
282 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
284 return H323_ERROR_NONE;
287 static int decode_bool(struct bitstr *bs, const struct field_t *f,
288 char *base, int level)
290 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
293 if (nf_h323_error_boundary(bs, 0, 0))
294 return H323_ERROR_BOUND;
295 return H323_ERROR_NONE;
298 static int decode_oid(struct bitstr *bs, const struct field_t *f,
299 char *base, int level)
303 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
306 if (nf_h323_error_boundary(bs, 1, 0))
307 return H323_ERROR_BOUND;
311 if (nf_h323_error_boundary(bs, 0, 0))
312 return H323_ERROR_BOUND;
314 return H323_ERROR_NONE;
317 static int decode_int(struct bitstr *bs, const struct field_t *f,
318 char *base, int level)
322 PRINT("%*.s%s", level * TAB_SIZE, " ", f->name);
325 case BYTE: /* Range == 256 */
329 case WORD: /* 257 <= Range <= 64K */
333 case CONS: /* 64K < Range < 4G */
334 if (nf_h323_error_boundary(bs, 0, 2))
335 return H323_ERROR_BOUND;
336 len = get_bits(bs, 2) + 1;
338 if (base && (f->attr & DECODE)) { /* timeToLive */
339 unsigned int v = get_uint(bs, len) + f->lb;
341 *((unsigned int *)(base + f->offset)) = v;
347 if (nf_h323_error_boundary(bs, 2, 0))
348 return H323_ERROR_BOUND;
352 default: /* 2 <= Range <= 255 */
359 if (nf_h323_error_boundary(bs, 0, 0))
360 return H323_ERROR_BOUND;
361 return H323_ERROR_NONE;
364 static int decode_enum(struct bitstr *bs, const struct field_t *f,
365 char *base, int level)
367 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
369 if ((f->attr & EXT) && get_bit(bs)) {
375 if (nf_h323_error_boundary(bs, 0, 0))
376 return H323_ERROR_BOUND;
377 return H323_ERROR_NONE;
380 static int decode_bitstr(struct bitstr *bs, const struct field_t *f,
381 char *base, int level)
385 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
389 case FIXD: /* fixed length > 16 */
392 case WORD: /* 2-byte length */
393 if (nf_h323_error_boundary(bs, 2, 0))
394 return H323_ERROR_BOUND;
395 len = (*bs->cur++) << 8;
396 len += (*bs->cur++) + f->lb;
399 if (nf_h323_error_boundary(bs, 2, 0))
400 return H323_ERROR_BOUND;
411 if (nf_h323_error_boundary(bs, 0, 0))
412 return H323_ERROR_BOUND;
413 return H323_ERROR_NONE;
416 static int decode_numstr(struct bitstr *bs, const struct field_t *f,
417 char *base, int level)
421 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
423 /* 2 <= Range <= 255 */
424 if (nf_h323_error_boundary(bs, 0, f->sz))
425 return H323_ERROR_BOUND;
426 len = get_bits(bs, f->sz) + f->lb;
429 INC_BITS(bs, (len << 2));
431 if (nf_h323_error_boundary(bs, 0, 0))
432 return H323_ERROR_BOUND;
433 return H323_ERROR_NONE;
436 static int decode_octstr(struct bitstr *bs, const struct field_t *f,
437 char *base, int level)
441 PRINT("%*.s%s", level * TAB_SIZE, " ", f->name);
444 case FIXD: /* Range == 1 */
447 if (base && (f->attr & DECODE)) {
450 PRINT(" = %d.%d.%d.%d:%d",
451 bs->cur[0], bs->cur[1],
452 bs->cur[2], bs->cur[3],
453 bs->cur[4] * 256 + bs->cur[5]));
454 *((unsigned int *)(base + f->offset)) =
460 case BYTE: /* Range == 256 */
462 if (nf_h323_error_boundary(bs, 1, 0))
463 return H323_ERROR_BOUND;
464 len = (*bs->cur++) + f->lb;
468 if (nf_h323_error_boundary(bs, 2, 0))
469 return H323_ERROR_BOUND;
470 len = get_len(bs) + f->lb;
472 default: /* 2 <= Range <= 255 */
473 if (nf_h323_error_boundary(bs, 0, f->sz))
474 return H323_ERROR_BOUND;
475 len = get_bits(bs, f->sz) + f->lb;
484 if (nf_h323_error_boundary(bs, 0, 0))
485 return H323_ERROR_BOUND;
486 return H323_ERROR_NONE;
489 static int decode_bmpstr(struct bitstr *bs, const struct field_t *f,
490 char *base, int level)
494 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
497 case BYTE: /* Range == 256 */
499 if (nf_h323_error_boundary(bs, 1, 0))
500 return H323_ERROR_BOUND;
501 len = (*bs->cur++) + f->lb;
503 default: /* 2 <= Range <= 255 */
504 if (nf_h323_error_boundary(bs, 0, f->sz))
505 return H323_ERROR_BOUND;
506 len = get_bits(bs, f->sz) + f->lb;
513 if (nf_h323_error_boundary(bs, 0, 0))
514 return H323_ERROR_BOUND;
515 return H323_ERROR_NONE;
518 static int decode_seq(struct bitstr *bs, const struct field_t *f,
519 char *base, int level)
521 unsigned int ext, bmp, i, opt, len = 0, bmp2, bmp2_len;
523 const struct field_t *son;
524 unsigned char *beg = NULL;
526 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
529 base = (base && (f->attr & DECODE)) ? base + f->offset : NULL;
532 if (nf_h323_error_boundary(bs, 0, 1))
533 return H323_ERROR_BOUND;
534 ext = (f->attr & EXT) ? get_bit(bs) : 0;
536 /* Get fields bitmap */
537 if (nf_h323_error_boundary(bs, 0, f->sz))
538 return H323_ERROR_BOUND;
539 bmp = get_bitmap(bs, f->sz);
541 *(unsigned int *)base = bmp;
543 /* Decode the root components */
544 for (i = opt = 0, son = f->fields; i < f->lb; i++, son++) {
545 if (son->attr & STOP) {
546 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
548 return H323_ERROR_STOP;
551 if (son->attr & OPT) { /* Optional component */
552 if (!((0x80000000U >> (opt++)) & bmp)) /* Not exist */
557 if (son->attr & OPEN) { /* Open field */
558 if (nf_h323_error_boundary(bs, 2, 0))
559 return H323_ERROR_BOUND;
561 if (nf_h323_error_boundary(bs, len, 0))
562 return H323_ERROR_BOUND;
563 if (!base || !(son->attr & DECODE)) {
564 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE,
572 if ((err = (Decoders[son->type]) (bs, son, base,
579 } else if ((err = (Decoders[son->type]) (bs, son, base,
587 return H323_ERROR_NONE;
589 /* Get the extension bitmap */
590 if (nf_h323_error_boundary(bs, 0, 7))
591 return H323_ERROR_BOUND;
592 bmp2_len = get_bits(bs, 7) + 1;
593 if (nf_h323_error_boundary(bs, 0, bmp2_len))
594 return H323_ERROR_BOUND;
595 bmp2 = get_bitmap(bs, bmp2_len);
596 bmp |= bmp2 >> f->sz;
598 *(unsigned int *)base = bmp;
601 /* Decode the extension components */
602 for (opt = 0; opt < bmp2_len; opt++, i++, son++) {
604 if (i >= f->ub) { /* Newer Version? */
605 if (nf_h323_error_boundary(bs, 2, 0))
606 return H323_ERROR_BOUND;
608 if (nf_h323_error_boundary(bs, len, 0))
609 return H323_ERROR_BOUND;
614 if (son->attr & STOP) {
615 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
617 return H323_ERROR_STOP;
620 if (!((0x80000000 >> opt) & bmp2)) /* Not present */
623 if (nf_h323_error_boundary(bs, 2, 0))
624 return H323_ERROR_BOUND;
626 if (nf_h323_error_boundary(bs, len, 0))
627 return H323_ERROR_BOUND;
628 if (!base || !(son->attr & DECODE)) {
629 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
636 if ((err = (Decoders[son->type]) (bs, son, base,
644 return H323_ERROR_NONE;
647 static int decode_seqof(struct bitstr *bs, const struct field_t *f,
648 char *base, int level)
650 unsigned int count, effective_count = 0, i, len = 0;
652 const struct field_t *son;
653 unsigned char *beg = NULL;
655 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
658 base = (base && (f->attr & DECODE)) ? base + f->offset : NULL;
660 /* Decode item count */
664 if (nf_h323_error_boundary(bs, 1, 0))
665 return H323_ERROR_BOUND;
670 if (nf_h323_error_boundary(bs, 2, 0))
671 return H323_ERROR_BOUND;
678 if (nf_h323_error_boundary(bs, 2, 0))
679 return H323_ERROR_BOUND;
683 if (nf_h323_error_boundary(bs, 0, f->sz))
684 return H323_ERROR_BOUND;
685 count = get_bits(bs, f->sz);
692 effective_count = count > f->ub ? f->ub : count;
693 *(unsigned int *)base = effective_count;
694 base += sizeof(unsigned int);
697 /* Decode nested field */
701 for (i = 0; i < count; i++) {
702 if (son->attr & OPEN) {
704 if (nf_h323_error_boundary(bs, 2, 0))
705 return H323_ERROR_BOUND;
707 if (nf_h323_error_boundary(bs, len, 0))
708 return H323_ERROR_BOUND;
709 if (!base || !(son->attr & DECODE)) {
710 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE,
717 if ((err = (Decoders[son->type]) (bs, son,
728 if ((err = (Decoders[son->type]) (bs, son,
740 return H323_ERROR_NONE;
743 static int decode_choice(struct bitstr *bs, const struct field_t *f,
744 char *base, int level)
746 unsigned int type, ext, len = 0;
748 const struct field_t *son;
749 unsigned char *beg = NULL;
751 PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
754 base = (base && (f->attr & DECODE)) ? base + f->offset : NULL;
756 /* Decode the choice index number */
757 if (nf_h323_error_boundary(bs, 0, 1))
758 return H323_ERROR_BOUND;
759 if ((f->attr & EXT) && get_bit(bs)) {
761 if (nf_h323_error_boundary(bs, 0, 7))
762 return H323_ERROR_BOUND;
763 type = get_bits(bs, 7) + f->lb;
766 if (nf_h323_error_boundary(bs, 0, f->sz))
767 return H323_ERROR_BOUND;
768 type = get_bits(bs, f->sz);
770 return H323_ERROR_RANGE;
775 *(unsigned int *)base = type;
778 if (type >= f->ub) { /* Newer version? */
780 if (nf_h323_error_boundary(bs, 2, 0))
781 return H323_ERROR_BOUND;
783 if (nf_h323_error_boundary(bs, len, 0))
784 return H323_ERROR_BOUND;
786 return H323_ERROR_NONE;
789 /* Transfer to son level */
790 son = &f->fields[type];
791 if (son->attr & STOP) {
792 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ", son->name);
793 return H323_ERROR_STOP;
796 if (ext || (son->attr & OPEN)) {
798 if (nf_h323_error_boundary(bs, len, 0))
799 return H323_ERROR_BOUND;
801 if (nf_h323_error_boundary(bs, len, 0))
802 return H323_ERROR_BOUND;
803 if (!base || !(son->attr & DECODE)) {
804 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
807 return H323_ERROR_NONE;
811 if ((err = (Decoders[son->type]) (bs, son, base, level + 1)) <
817 } else if ((err = (Decoders[son->type]) (bs, son, base, level + 1)) <
821 return H323_ERROR_NONE;
824 int DecodeRasMessage(unsigned char *buf, size_t sz, RasMessage *ras)
826 static const struct field_t ras_message = {
827 FNAME("RasMessage") CHOICE, 5, 24, 32, DECODE | EXT,
832 bs.buf = bs.beg = bs.cur = buf;
836 return decode_choice(&bs, &ras_message, (char *) ras, 0);
839 static int DecodeH323_UserInformation(unsigned char *buf, unsigned char *beg,
840 size_t sz, H323_UserInformation *uuie)
842 static const struct field_t h323_userinformation = {
843 FNAME("H323-UserInformation") SEQ, 1, 2, 2, DECODE | EXT,
844 0, _H323_UserInformation
849 bs.beg = bs.cur = beg;
853 return decode_seq(&bs, &h323_userinformation, (char *) uuie, 0);
856 int DecodeMultimediaSystemControlMessage(unsigned char *buf, size_t sz,
857 MultimediaSystemControlMessage *
860 static const struct field_t multimediasystemcontrolmessage = {
861 FNAME("MultimediaSystemControlMessage") CHOICE, 2, 4, 4,
862 DECODE | EXT, 0, _MultimediaSystemControlMessage
866 bs.buf = bs.beg = bs.cur = buf;
870 return decode_choice(&bs, &multimediasystemcontrolmessage,
874 int DecodeQ931(unsigned char *buf, size_t sz, Q931 *q931)
876 unsigned char *p = buf;
880 return H323_ERROR_BOUND;
882 /* Protocol Discriminator */
884 PRINT("Unknown Protocol Discriminator\n");
885 return H323_ERROR_RANGE;
890 /* CallReferenceValue */
892 return H323_ERROR_BOUND;
896 return H323_ERROR_BOUND;
902 return H323_ERROR_BOUND;
903 q931->MessageType = *p++;
905 PRINT("MessageType = %02X\n", q931->MessageType);
911 /* Decode Information Elements */
913 if (*p == 0x7e) { /* UserUserIE */
924 return DecodeH323_UserInformation(buf, p, len,
939 PRINT("Q.931 UUIE not found\n");
941 return H323_ERROR_BOUND;