1 /****************************************************************************
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.
11 ****************************************************************************/
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
[] = {
143 /****************************************************************************
145 ****************************************************************************/
146 #include "nf_conntrack_h323_types.c"
148 /****************************************************************************
150 ****************************************************************************/
151 /* Assume bs is aligned && v < 16384 */
152 static unsigned int get_len(struct bitstr
*bs
)
167 static int nf_h323_error_boundary(struct bitstr
*bs
, size_t bytes
, size_t bits
)
170 bytes
+= bits
/ BITS_PER_BYTE
;
171 if (bits
% BITS_PER_BYTE
> 0)
174 if (*bs
->cur
+ bytes
> *bs
->end
)
180 /****************************************************************************/
181 static unsigned int get_bit(struct bitstr
*bs
)
183 unsigned int b
= (*bs
->cur
) & (0x80 >> bs
->bit
);
190 /****************************************************************************/
192 static unsigned int get_bits(struct bitstr
*bs
, unsigned int b
)
196 v
= (*bs
->cur
) & (0xffU
>> bs
->bit
);
216 /****************************************************************************/
218 static unsigned int get_bitmap(struct bitstr
*bs
, unsigned int b
)
220 unsigned int v
, l
, shift
, bytes
;
228 v
= (unsigned int)(*bs
->cur
) << (bs
->bit
+ 24);
231 v
= (unsigned int)(*bs
->cur
++) << (bs
->bit
+ 24);
234 for (bytes
= l
>> 3, shift
= 24, v
= 0; bytes
;
236 v
|= (unsigned int)(*bs
->cur
++) << shift
;
239 v
|= (unsigned int)(*bs
->cur
) << shift
;
243 v
|= (*bs
->cur
) >> (8 - bs
->bit
);
249 v
&= 0xffffffff << (32 - b
);
254 /****************************************************************************
255 * Assume bs is aligned and sizeof(unsigned int) == 4
256 ****************************************************************************/
257 static unsigned int get_uint(struct bitstr
*bs
, int b
)
278 /****************************************************************************/
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 /****************************************************************************/
288 static int decode_bool(struct bitstr
*bs
, const struct field_t
*f
,
289 char *base
, int level
)
291 PRINT("%*.s%s\n", level
* TAB_SIZE
, " ", f
->name
);
294 if (nf_h323_error_boundary(bs
, 0, 0))
295 return H323_ERROR_BOUND
;
296 return H323_ERROR_NONE
;
299 /****************************************************************************/
300 static int decode_oid(struct bitstr
*bs
, const struct field_t
*f
,
301 char *base
, int level
)
305 PRINT("%*.s%s\n", level
* TAB_SIZE
, " ", f
->name
);
308 if (nf_h323_error_boundary(bs
, 1, 0))
309 return H323_ERROR_BOUND
;
313 if (nf_h323_error_boundary(bs
, 0, 0))
314 return H323_ERROR_BOUND
;
316 return H323_ERROR_NONE
;
319 /****************************************************************************/
320 static int decode_int(struct bitstr
*bs
, const struct field_t
*f
,
321 char *base
, int level
)
325 PRINT("%*.s%s", level
* TAB_SIZE
, " ", f
->name
);
328 case BYTE
: /* Range == 256 */
332 case WORD
: /* 257 <= Range <= 64K */
336 case CONS
: /* 64K < Range < 4G */
337 if (nf_h323_error_boundary(bs
, 0, 2))
338 return H323_ERROR_BOUND
;
339 len
= get_bits(bs
, 2) + 1;
341 if (base
&& (f
->attr
& DECODE
)) { /* timeToLive */
342 unsigned int v
= get_uint(bs
, len
) + f
->lb
;
344 *((unsigned int *)(base
+ f
->offset
)) = v
;
350 if (nf_h323_error_boundary(bs
, 2, 0))
351 return H323_ERROR_BOUND
;
355 default: /* 2 <= Range <= 255 */
362 if (nf_h323_error_boundary(bs
, 0, 0))
363 return H323_ERROR_BOUND
;
364 return H323_ERROR_NONE
;
367 /****************************************************************************/
368 static int decode_enum(struct bitstr
*bs
, const struct field_t
*f
,
369 char *base
, int level
)
371 PRINT("%*.s%s\n", level
* TAB_SIZE
, " ", f
->name
);
373 if ((f
->attr
& EXT
) && get_bit(bs
)) {
379 if (nf_h323_error_boundary(bs
, 0, 0))
380 return H323_ERROR_BOUND
;
381 return H323_ERROR_NONE
;
384 /****************************************************************************/
385 static int decode_bitstr(struct bitstr
*bs
, const struct field_t
*f
,
386 char *base
, int level
)
390 PRINT("%*.s%s\n", level
* TAB_SIZE
, " ", f
->name
);
394 case FIXD
: /* fixed length > 16 */
397 case WORD
: /* 2-byte length */
398 if (nf_h323_error_boundary(bs
, 2, 0))
399 return H323_ERROR_BOUND
;
400 len
= (*bs
->cur
++) << 8;
401 len
+= (*bs
->cur
++) + f
->lb
;
404 if (nf_h323_error_boundary(bs
, 2, 0))
405 return H323_ERROR_BOUND
;
416 if (nf_h323_error_boundary(bs
, 0, 0))
417 return H323_ERROR_BOUND
;
418 return H323_ERROR_NONE
;
421 /****************************************************************************/
422 static int decode_numstr(struct bitstr
*bs
, const struct field_t
*f
,
423 char *base
, int level
)
427 PRINT("%*.s%s\n", level
* TAB_SIZE
, " ", f
->name
);
429 /* 2 <= Range <= 255 */
430 if (nf_h323_error_boundary(bs
, 0, f
->sz
))
431 return H323_ERROR_BOUND
;
432 len
= get_bits(bs
, f
->sz
) + f
->lb
;
435 INC_BITS(bs
, (len
<< 2));
437 if (nf_h323_error_boundary(bs
, 0, 0))
438 return H323_ERROR_BOUND
;
439 return H323_ERROR_NONE
;
442 /****************************************************************************/
443 static int decode_octstr(struct bitstr
*bs
, const struct field_t
*f
,
444 char *base
, int level
)
448 PRINT("%*.s%s", level
* TAB_SIZE
, " ", f
->name
);
451 case FIXD
: /* Range == 1 */
454 if (base
&& (f
->attr
& DECODE
)) {
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]));
461 *((unsigned int *)(base
+ f
->offset
)) =
467 case BYTE
: /* Range == 256 */
469 if (nf_h323_error_boundary(bs
, 1, 0))
470 return H323_ERROR_BOUND
;
471 len
= (*bs
->cur
++) + f
->lb
;
475 if (nf_h323_error_boundary(bs
, 2, 0))
476 return H323_ERROR_BOUND
;
477 len
= get_len(bs
) + f
->lb
;
479 default: /* 2 <= Range <= 255 */
480 if (nf_h323_error_boundary(bs
, 0, f
->sz
))
481 return H323_ERROR_BOUND
;
482 len
= get_bits(bs
, f
->sz
) + f
->lb
;
491 if (nf_h323_error_boundary(bs
, 0, 0))
492 return H323_ERROR_BOUND
;
493 return H323_ERROR_NONE
;
496 /****************************************************************************/
497 static int decode_bmpstr(struct bitstr
*bs
, const struct field_t
*f
,
498 char *base
, int level
)
502 PRINT("%*.s%s\n", level
* TAB_SIZE
, " ", f
->name
);
505 case BYTE
: /* Range == 256 */
507 if (nf_h323_error_boundary(bs
, 1, 0))
508 return H323_ERROR_BOUND
;
509 len
= (*bs
->cur
++) + f
->lb
;
511 default: /* 2 <= Range <= 255 */
512 if (nf_h323_error_boundary(bs
, 0, f
->sz
))
513 return H323_ERROR_BOUND
;
514 len
= get_bits(bs
, f
->sz
) + f
->lb
;
521 if (nf_h323_error_boundary(bs
, 0, 0))
522 return H323_ERROR_BOUND
;
523 return H323_ERROR_NONE
;
526 /****************************************************************************/
527 static int decode_seq(struct bitstr
*bs
, const struct field_t
*f
,
528 char *base
, int level
)
530 unsigned int ext
, bmp
, i
, opt
, len
= 0, bmp2
, bmp2_len
;
532 const struct field_t
*son
;
533 unsigned char *beg
= NULL
;
535 PRINT("%*.s%s\n", level
* TAB_SIZE
, " ", f
->name
);
538 base
= (base
&& (f
->attr
& DECODE
)) ? base
+ f
->offset
: NULL
;
541 if (nf_h323_error_boundary(bs
, 0, 1))
542 return H323_ERROR_BOUND
;
543 ext
= (f
->attr
& EXT
) ? get_bit(bs
) : 0;
545 /* Get fields bitmap */
546 if (nf_h323_error_boundary(bs
, 0, f
->sz
))
547 return H323_ERROR_BOUND
;
548 bmp
= get_bitmap(bs
, f
->sz
);
550 *(unsigned int *)base
= bmp
;
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
, " ",
557 return H323_ERROR_STOP
;
560 if (son
->attr
& OPT
) { /* Optional component */
561 if (!((0x80000000U
>> (opt
++)) & bmp
)) /* Not exist */
566 if (son
->attr
& OPEN
) { /* Open field */
567 if (nf_h323_error_boundary(bs
, 2, 0))
568 return H323_ERROR_BOUND
;
570 if (nf_h323_error_boundary(bs
, len
, 0))
571 return H323_ERROR_BOUND
;
572 if (!base
|| !(son
->attr
& DECODE
)) {
573 PRINT("%*.s%s\n", (level
+ 1) * TAB_SIZE
,
581 if ((err
= (Decoders
[son
->type
]) (bs
, son
, base
,
588 } else if ((err
= (Decoders
[son
->type
]) (bs
, son
, base
,
596 return H323_ERROR_NONE
;
598 /* Get the extension bitmap */
599 if (nf_h323_error_boundary(bs
, 0, 7))
600 return H323_ERROR_BOUND
;
601 bmp2_len
= get_bits(bs
, 7) + 1;
602 if (nf_h323_error_boundary(bs
, 0, bmp2_len
))
603 return H323_ERROR_BOUND
;
604 bmp2
= get_bitmap(bs
, bmp2_len
);
605 bmp
|= bmp2
>> f
->sz
;
607 *(unsigned int *)base
= bmp
;
610 /* Decode the extension components */
611 for (opt
= 0; opt
< bmp2_len
; opt
++, i
++, son
++) {
613 if (i
>= f
->ub
) { /* Newer Version? */
614 if (nf_h323_error_boundary(bs
, 2, 0))
615 return H323_ERROR_BOUND
;
617 if (nf_h323_error_boundary(bs
, len
, 0))
618 return H323_ERROR_BOUND
;
623 if (son
->attr
& STOP
) {
624 PRINT("%*.s%s\n", (level
+ 1) * TAB_SIZE
, " ",
626 return H323_ERROR_STOP
;
629 if (!((0x80000000 >> opt
) & bmp2
)) /* Not present */
632 if (nf_h323_error_boundary(bs
, 2, 0))
633 return H323_ERROR_BOUND
;
635 if (nf_h323_error_boundary(bs
, len
, 0))
636 return H323_ERROR_BOUND
;
637 if (!base
|| !(son
->attr
& DECODE
)) {
638 PRINT("%*.s%s\n", (level
+ 1) * TAB_SIZE
, " ",
645 if ((err
= (Decoders
[son
->type
]) (bs
, son
, base
,
653 return H323_ERROR_NONE
;
656 /****************************************************************************/
657 static int decode_seqof(struct bitstr
*bs
, const struct field_t
*f
,
658 char *base
, int level
)
660 unsigned int count
, effective_count
= 0, i
, len
= 0;
662 const struct field_t
*son
;
663 unsigned char *beg
= NULL
;
665 PRINT("%*.s%s\n", level
* TAB_SIZE
, " ", f
->name
);
668 base
= (base
&& (f
->attr
& DECODE
)) ? base
+ f
->offset
: NULL
;
670 /* Decode item count */
674 if (nf_h323_error_boundary(bs
, 1, 0))
675 return H323_ERROR_BOUND
;
680 if (nf_h323_error_boundary(bs
, 2, 0))
681 return H323_ERROR_BOUND
;
688 if (nf_h323_error_boundary(bs
, 2, 0))
689 return H323_ERROR_BOUND
;
693 if (nf_h323_error_boundary(bs
, 0, f
->sz
))
694 return H323_ERROR_BOUND
;
695 count
= get_bits(bs
, f
->sz
);
702 effective_count
= count
> f
->ub
? f
->ub
: count
;
703 *(unsigned int *)base
= effective_count
;
704 base
+= sizeof(unsigned int);
707 /* Decode nested field */
711 for (i
= 0; i
< count
; i
++) {
712 if (son
->attr
& OPEN
) {
714 if (nf_h323_error_boundary(bs
, 2, 0))
715 return H323_ERROR_BOUND
;
717 if (nf_h323_error_boundary(bs
, len
, 0))
718 return H323_ERROR_BOUND
;
719 if (!base
|| !(son
->attr
& DECODE
)) {
720 PRINT("%*.s%s\n", (level
+ 1) * TAB_SIZE
,
727 if ((err
= (Decoders
[son
->type
]) (bs
, son
,
738 if ((err
= (Decoders
[son
->type
]) (bs
, son
,
750 return H323_ERROR_NONE
;
754 /****************************************************************************/
755 static int decode_choice(struct bitstr
*bs
, const struct field_t
*f
,
756 char *base
, int level
)
758 unsigned int type
, ext
, len
= 0;
760 const struct field_t
*son
;
761 unsigned char *beg
= NULL
;
763 PRINT("%*.s%s\n", level
* TAB_SIZE
, " ", f
->name
);
766 base
= (base
&& (f
->attr
& DECODE
)) ? base
+ f
->offset
: NULL
;
768 /* Decode the choice index number */
769 if (nf_h323_error_boundary(bs
, 0, 1))
770 return H323_ERROR_BOUND
;
771 if ((f
->attr
& EXT
) && get_bit(bs
)) {
773 if (nf_h323_error_boundary(bs
, 0, 7))
774 return H323_ERROR_BOUND
;
775 type
= get_bits(bs
, 7) + f
->lb
;
778 if (nf_h323_error_boundary(bs
, 0, f
->sz
))
779 return H323_ERROR_BOUND
;
780 type
= get_bits(bs
, f
->sz
);
782 return H323_ERROR_RANGE
;
787 *(unsigned int *)base
= type
;
790 if (type
>= f
->ub
) { /* Newer version? */
792 if (nf_h323_error_boundary(bs
, 2, 0))
793 return H323_ERROR_BOUND
;
795 if (nf_h323_error_boundary(bs
, len
, 0))
796 return H323_ERROR_BOUND
;
798 return H323_ERROR_NONE
;
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
;
808 if (ext
|| (son
->attr
& OPEN
)) {
810 if (nf_h323_error_boundary(bs
, len
, 0))
811 return H323_ERROR_BOUND
;
813 if (nf_h323_error_boundary(bs
, len
, 0))
814 return H323_ERROR_BOUND
;
815 if (!base
|| !(son
->attr
& DECODE
)) {
816 PRINT("%*.s%s\n", (level
+ 1) * TAB_SIZE
, " ",
819 return H323_ERROR_NONE
;
823 if ((err
= (Decoders
[son
->type
]) (bs
, son
, base
, level
+ 1)) <
829 } else if ((err
= (Decoders
[son
->type
]) (bs
, son
, base
, level
+ 1)) <
833 return H323_ERROR_NONE
;
836 /****************************************************************************/
837 int DecodeRasMessage(unsigned char *buf
, size_t sz
, RasMessage
*ras
)
839 static const struct field_t ras_message
= {
840 FNAME("RasMessage") CHOICE
, 5, 24, 32, DECODE
| EXT
,
845 bs
.buf
= bs
.beg
= bs
.cur
= buf
;
849 return decode_choice(&bs
, &ras_message
, (char *) ras
, 0);
852 /****************************************************************************/
853 static int DecodeH323_UserInformation(unsigned char *buf
, unsigned char *beg
,
854 size_t sz
, H323_UserInformation
*uuie
)
856 static const struct field_t h323_userinformation
= {
857 FNAME("H323-UserInformation") SEQ
, 1, 2, 2, DECODE
| EXT
,
858 0, _H323_UserInformation
863 bs
.beg
= bs
.cur
= beg
;
867 return decode_seq(&bs
, &h323_userinformation
, (char *) uuie
, 0);
870 /****************************************************************************/
871 int DecodeMultimediaSystemControlMessage(unsigned char *buf
, size_t sz
,
872 MultimediaSystemControlMessage
*
875 static const struct field_t multimediasystemcontrolmessage
= {
876 FNAME("MultimediaSystemControlMessage") CHOICE
, 2, 4, 4,
877 DECODE
| EXT
, 0, _MultimediaSystemControlMessage
881 bs
.buf
= bs
.beg
= bs
.cur
= buf
;
885 return decode_choice(&bs
, &multimediasystemcontrolmessage
,
889 /****************************************************************************/
890 int DecodeQ931(unsigned char *buf
, size_t sz
, Q931
*q931
)
892 unsigned char *p
= buf
;
896 return H323_ERROR_BOUND
;
898 /* Protocol Discriminator */
900 PRINT("Unknown Protocol Discriminator\n");
901 return H323_ERROR_RANGE
;
906 /* CallReferenceValue */
908 return H323_ERROR_BOUND
;
912 return H323_ERROR_BOUND
;
918 return H323_ERROR_BOUND
;
919 q931
->MessageType
= *p
++;
921 PRINT("MessageType = %02X\n", q931
->MessageType
);
927 /* Decode Information Elements */
929 if (*p
== 0x7e) { /* UserUserIE */
940 return DecodeH323_UserInformation(buf
, p
, len
,
955 PRINT("Q.931 UUIE not found\n");
957 return H323_ERROR_BOUND
;