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
;
536 bmp
= get_bitmap(bs
, f
->sz
);
538 *(unsigned int *)base
= bmp
;
540 /* Decode the root components */
541 for (i
= opt
= 0, son
= f
->fields
; i
< f
->lb
; i
++, son
++) {
542 if (son
->attr
& STOP
) {
543 PRINT("%*.s%s\n", (level
+ 1) * TAB_SIZE
, " ",
545 return H323_ERROR_STOP
;
548 if (son
->attr
& OPT
) { /* Optional component */
549 if (!((0x80000000U
>> (opt
++)) & bmp
)) /* Not exist */
554 if (son
->attr
& OPEN
) { /* Open field */
555 if (nf_h323_error_boundary(bs
, 2, 0))
556 return H323_ERROR_BOUND
;
558 if (nf_h323_error_boundary(bs
, len
, 0))
559 return H323_ERROR_BOUND
;
560 if (!base
|| !(son
->attr
& DECODE
)) {
561 PRINT("%*.s%s\n", (level
+ 1) * TAB_SIZE
,
569 if ((err
= (Decoders
[son
->type
]) (bs
, son
, base
,
576 } else if ((err
= (Decoders
[son
->type
]) (bs
, son
, base
,
584 return H323_ERROR_NONE
;
586 /* Get the extension bitmap */
587 if (nf_h323_error_boundary(bs
, 0, 7))
588 return H323_ERROR_BOUND
;
589 bmp2_len
= get_bits(bs
, 7) + 1;
590 if (nf_h323_error_boundary(bs
, 0, bmp2_len
))
591 return H323_ERROR_BOUND
;
592 bmp2
= get_bitmap(bs
, bmp2_len
);
593 bmp
|= bmp2
>> f
->sz
;
595 *(unsigned int *)base
= bmp
;
598 /* Decode the extension components */
599 for (opt
= 0; opt
< bmp2_len
; opt
++, i
++, son
++) {
601 if (i
>= f
->ub
) { /* Newer Version? */
602 if (nf_h323_error_boundary(bs
, 2, 0))
603 return H323_ERROR_BOUND
;
605 if (nf_h323_error_boundary(bs
, len
, 0))
606 return H323_ERROR_BOUND
;
611 if (son
->attr
& STOP
) {
612 PRINT("%*.s%s\n", (level
+ 1) * TAB_SIZE
, " ",
614 return H323_ERROR_STOP
;
617 if (!((0x80000000 >> opt
) & bmp2
)) /* Not present */
620 if (nf_h323_error_boundary(bs
, 2, 0))
621 return H323_ERROR_BOUND
;
623 if (nf_h323_error_boundary(bs
, len
, 0))
624 return H323_ERROR_BOUND
;
625 if (!base
|| !(son
->attr
& DECODE
)) {
626 PRINT("%*.s%s\n", (level
+ 1) * TAB_SIZE
, " ",
633 if ((err
= (Decoders
[son
->type
]) (bs
, son
, base
,
641 return H323_ERROR_NONE
;
644 static int decode_seqof(struct bitstr
*bs
, const struct field_t
*f
,
645 char *base
, int level
)
647 unsigned int count
, effective_count
= 0, i
, len
= 0;
649 const struct field_t
*son
;
650 unsigned char *beg
= NULL
;
652 PRINT("%*.s%s\n", level
* TAB_SIZE
, " ", f
->name
);
655 base
= (base
&& (f
->attr
& DECODE
)) ? base
+ f
->offset
: NULL
;
657 /* Decode item count */
661 if (nf_h323_error_boundary(bs
, 1, 0))
662 return H323_ERROR_BOUND
;
667 if (nf_h323_error_boundary(bs
, 2, 0))
668 return H323_ERROR_BOUND
;
675 if (nf_h323_error_boundary(bs
, 2, 0))
676 return H323_ERROR_BOUND
;
680 if (nf_h323_error_boundary(bs
, 0, f
->sz
))
681 return H323_ERROR_BOUND
;
682 count
= get_bits(bs
, f
->sz
);
689 effective_count
= count
> f
->ub
? f
->ub
: count
;
690 *(unsigned int *)base
= effective_count
;
691 base
+= sizeof(unsigned int);
694 /* Decode nested field */
698 for (i
= 0; i
< count
; i
++) {
699 if (son
->attr
& OPEN
) {
701 if (nf_h323_error_boundary(bs
, 2, 0))
702 return H323_ERROR_BOUND
;
704 if (nf_h323_error_boundary(bs
, len
, 0))
705 return H323_ERROR_BOUND
;
706 if (!base
|| !(son
->attr
& DECODE
)) {
707 PRINT("%*.s%s\n", (level
+ 1) * TAB_SIZE
,
714 if ((err
= (Decoders
[son
->type
]) (bs
, son
,
725 if ((err
= (Decoders
[son
->type
]) (bs
, son
,
737 return H323_ERROR_NONE
;
740 static int decode_choice(struct bitstr
*bs
, const struct field_t
*f
,
741 char *base
, int level
)
743 unsigned int type
, ext
, len
= 0;
745 const struct field_t
*son
;
746 unsigned char *beg
= NULL
;
748 PRINT("%*.s%s\n", level
* TAB_SIZE
, " ", f
->name
);
751 base
= (base
&& (f
->attr
& DECODE
)) ? base
+ f
->offset
: NULL
;
753 /* Decode the choice index number */
754 if (nf_h323_error_boundary(bs
, 0, 1))
755 return H323_ERROR_BOUND
;
756 if ((f
->attr
& EXT
) && get_bit(bs
)) {
758 if (nf_h323_error_boundary(bs
, 0, 7))
759 return H323_ERROR_BOUND
;
760 type
= get_bits(bs
, 7) + f
->lb
;
763 if (nf_h323_error_boundary(bs
, 0, f
->sz
))
764 return H323_ERROR_BOUND
;
765 type
= get_bits(bs
, f
->sz
);
767 return H323_ERROR_RANGE
;
772 *(unsigned int *)base
= type
;
775 if (type
>= f
->ub
) { /* Newer version? */
777 if (nf_h323_error_boundary(bs
, 2, 0))
778 return H323_ERROR_BOUND
;
780 if (nf_h323_error_boundary(bs
, len
, 0))
781 return H323_ERROR_BOUND
;
783 return H323_ERROR_NONE
;
786 /* Transfer to son level */
787 son
= &f
->fields
[type
];
788 if (son
->attr
& STOP
) {
789 PRINT("%*.s%s\n", (level
+ 1) * TAB_SIZE
, " ", son
->name
);
790 return H323_ERROR_STOP
;
793 if (ext
|| (son
->attr
& OPEN
)) {
795 if (nf_h323_error_boundary(bs
, len
, 0))
796 return H323_ERROR_BOUND
;
798 if (nf_h323_error_boundary(bs
, len
, 0))
799 return H323_ERROR_BOUND
;
800 if (!base
|| !(son
->attr
& DECODE
)) {
801 PRINT("%*.s%s\n", (level
+ 1) * TAB_SIZE
, " ",
804 return H323_ERROR_NONE
;
808 if ((err
= (Decoders
[son
->type
]) (bs
, son
, base
, level
+ 1)) <
814 } else if ((err
= (Decoders
[son
->type
]) (bs
, son
, base
, level
+ 1)) <
818 return H323_ERROR_NONE
;
821 int DecodeRasMessage(unsigned char *buf
, size_t sz
, RasMessage
*ras
)
823 static const struct field_t ras_message
= {
824 FNAME("RasMessage") CHOICE
, 5, 24, 32, DECODE
| EXT
,
829 bs
.buf
= bs
.beg
= bs
.cur
= buf
;
833 return decode_choice(&bs
, &ras_message
, (char *) ras
, 0);
836 static int DecodeH323_UserInformation(unsigned char *buf
, unsigned char *beg
,
837 size_t sz
, H323_UserInformation
*uuie
)
839 static const struct field_t h323_userinformation
= {
840 FNAME("H323-UserInformation") SEQ
, 1, 2, 2, DECODE
| EXT
,
841 0, _H323_UserInformation
846 bs
.beg
= bs
.cur
= beg
;
850 return decode_seq(&bs
, &h323_userinformation
, (char *) uuie
, 0);
853 int DecodeMultimediaSystemControlMessage(unsigned char *buf
, size_t sz
,
854 MultimediaSystemControlMessage
*
857 static const struct field_t multimediasystemcontrolmessage
= {
858 FNAME("MultimediaSystemControlMessage") CHOICE
, 2, 4, 4,
859 DECODE
| EXT
, 0, _MultimediaSystemControlMessage
863 bs
.buf
= bs
.beg
= bs
.cur
= buf
;
867 return decode_choice(&bs
, &multimediasystemcontrolmessage
,
871 int DecodeQ931(unsigned char *buf
, size_t sz
, Q931
*q931
)
873 unsigned char *p
= buf
;
877 return H323_ERROR_BOUND
;
879 /* Protocol Discriminator */
881 PRINT("Unknown Protocol Discriminator\n");
882 return H323_ERROR_RANGE
;
887 /* CallReferenceValue */
889 return H323_ERROR_BOUND
;
893 return H323_ERROR_BOUND
;
899 return H323_ERROR_BOUND
;
900 q931
->MessageType
= *p
++;
902 PRINT("MessageType = %02X\n", q931
->MessageType
);
908 /* Decode Information Elements */
910 if (*p
== 0x7e) { /* UserUserIE */
921 return DecodeH323_UserInformation(buf
, p
, len
,
936 PRINT("Q.931 UUIE not found\n");
938 return H323_ERROR_BOUND
;