nf_conntrack_h323_asn1.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * BER and PER decoding library for H.323 conntrack/NAT module.
  4. *
  5. * Copyright (c) 2006 by Jing Min Zhao <zhaojingmin@users.sourceforge.net>
  6. *
  7. * See nf_conntrack_helper_h323_asn1.h for details.
  8. */
  9. #ifdef __KERNEL__
  10. #include <linux/kernel.h>
  11. #else
  12. #include <stdio.h>
  13. #endif
  14. #include <linux/netfilter/nf_conntrack_h323_asn1.h>
  15. /* Trace Flag */
  16. #ifndef H323_TRACE
  17. #define H323_TRACE 0
  18. #endif
  19. #if H323_TRACE
  20. #define TAB_SIZE 4
  21. #define IFTHEN(cond, act) if(cond){act;}
  22. #ifdef __KERNEL__
  23. #define PRINT printk
  24. #else
  25. #define PRINT printf
  26. #endif
  27. #define FNAME(name) name,
  28. #else
  29. #define IFTHEN(cond, act)
  30. #define PRINT(fmt, args...)
  31. #define FNAME(name)
  32. #endif
  33. /* ASN.1 Types */
  34. #define NUL 0
  35. #define BOOL 1
  36. #define OID 2
  37. #define INT 3
  38. #define ENUM 4
  39. #define BITSTR 5
  40. #define NUMSTR 6
  41. #define NUMDGT 6
  42. #define TBCDSTR 6
  43. #define OCTSTR 7
  44. #define PRTSTR 7
  45. #define IA5STR 7
  46. #define GENSTR 7
  47. #define BMPSTR 8
  48. #define SEQ 9
  49. #define SET 9
  50. #define SEQOF 10
  51. #define SETOF 10
  52. #define CHOICE 11
  53. /* Constraint Types */
  54. #define FIXD 0
  55. /* #define BITS 1-8 */
  56. #define BYTE 9
  57. #define WORD 10
  58. #define CONS 11
  59. #define SEMI 12
  60. #define UNCO 13
  61. /* ASN.1 Type Attributes */
  62. #define SKIP 0
  63. #define STOP 1
  64. #define DECODE 2
  65. #define EXT 4
  66. #define OPEN 8
  67. #define OPT 16
  68. /* ASN.1 Field Structure */
  69. typedef struct field_t {
  70. #if H323_TRACE
  71. char *name;
  72. #endif
  73. unsigned char type;
  74. unsigned char sz;
  75. unsigned char lb;
  76. unsigned char ub;
  77. unsigned short attr;
  78. unsigned short offset;
  79. const struct field_t *fields;
  80. } field_t;
  81. /* Bit Stream */
  82. struct bitstr {
  83. unsigned char *buf;
  84. unsigned char *beg;
  85. unsigned char *end;
  86. unsigned char *cur;
  87. unsigned int bit;
  88. };
  89. /* Tool Functions */
  90. #define INC_BIT(bs) if((++(bs)->bit)>7){(bs)->cur++;(bs)->bit=0;}
  91. #define INC_BITS(bs,b) if(((bs)->bit+=(b))>7){(bs)->cur+=(bs)->bit>>3;(bs)->bit&=7;}
  92. #define BYTE_ALIGN(bs) if((bs)->bit){(bs)->cur++;(bs)->bit=0;}
  93. static unsigned int get_len(struct bitstr *bs);
  94. static unsigned int get_bit(struct bitstr *bs);
  95. static unsigned int get_bits(struct bitstr *bs, unsigned int b);
  96. static unsigned int get_bitmap(struct bitstr *bs, unsigned int b);
  97. static unsigned int get_uint(struct bitstr *bs, int b);
  98. /* Decoder Functions */
  99. static int decode_nul(struct bitstr *bs, const struct field_t *f, char *base, int level);
  100. static int decode_bool(struct bitstr *bs, const struct field_t *f, char *base, int level);
  101. static int decode_oid(struct bitstr *bs, const struct field_t *f, char *base, int level);
  102. static int decode_int(struct bitstr *bs, const struct field_t *f, char *base, int level);
  103. static int decode_enum(struct bitstr *bs, const struct field_t *f, char *base, int level);
  104. static int decode_bitstr(struct bitstr *bs, const struct field_t *f, char *base, int level);
  105. static int decode_numstr(struct bitstr *bs, const struct field_t *f, char *base, int level);
  106. static int decode_octstr(struct bitstr *bs, const struct field_t *f, char *base, int level);
  107. static int decode_bmpstr(struct bitstr *bs, const struct field_t *f, char *base, int level);
  108. static int decode_seq(struct bitstr *bs, const struct field_t *f, char *base, int level);
  109. static int decode_seqof(struct bitstr *bs, const struct field_t *f, char *base, int level);
  110. static int decode_choice(struct bitstr *bs, const struct field_t *f, char *base, int level);
  111. /* Decoder Functions Vector */
  112. typedef int (*decoder_t)(struct bitstr *, const struct field_t *, char *, int);
  113. static const decoder_t Decoders[] = {
  114. decode_nul,
  115. decode_bool,
  116. decode_oid,
  117. decode_int,
  118. decode_enum,
  119. decode_bitstr,
  120. decode_numstr,
  121. decode_octstr,
  122. decode_bmpstr,
  123. decode_seq,
  124. decode_seqof,
  125. decode_choice,
  126. };
  127. /*
  128. * H.323 Types
  129. */
  130. #include "nf_conntrack_h323_types.c"
  131. /*
  132. * Functions
  133. */
  134. /* Assume bs is aligned && v < 16384 */
  135. static unsigned int get_len(struct bitstr *bs)
  136. {
  137. unsigned int v;
  138. v = *bs->cur++;
  139. if (v & 0x80) {
  140. v &= 0x3f;
  141. v <<= 8;
  142. v += *bs->cur++;
  143. }
  144. return v;
  145. }
  146. static int nf_h323_error_boundary(struct bitstr *bs, size_t bytes, size_t bits)
  147. {
  148. bits += bs->bit;
  149. bytes += bits / BITS_PER_BYTE;
  150. if (bits % BITS_PER_BYTE > 0)
  151. bytes++;
  152. if (bs->cur + bytes > bs->end)
  153. return 1;
  154. return 0;
  155. }
  156. static unsigned int get_bit(struct bitstr *bs)
  157. {
  158. unsigned int b = (*bs->cur) & (0x80 >> bs->bit);
  159. INC_BIT(bs);
  160. return b;
  161. }
  162. /* Assume b <= 8 */
  163. static unsigned int get_bits(struct bitstr *bs, unsigned int b)
  164. {
  165. unsigned int v, l;
  166. v = (*bs->cur) & (0xffU >> bs->bit);
  167. l = b + bs->bit;
  168. if (l < 8) {
  169. v >>= 8 - l;
  170. bs->bit = l;
  171. } else if (l == 8) {
  172. bs->cur++;
  173. bs->bit = 0;
  174. } else { /* l > 8 */
  175. v <<= 8;
  176. v += *(++bs->cur);
  177. v >>= 16 - l;
  178. bs->bit = l - 8;
  179. }
  180. return v;
  181. }
  182. /* Assume b <= 32 */
  183. static unsigned int get_bitmap(struct bitstr *bs, unsigned int b)
  184. {
  185. unsigned int v, l, shift, bytes;
  186. if (!b)
  187. return 0;
  188. l = bs->bit + b;
  189. if (l < 8) {
  190. v = (unsigned int)(*bs->cur) << (bs->bit + 24);
  191. bs->bit = l;
  192. } else if (l == 8) {
  193. v = (unsigned int)(*bs->cur++) << (bs->bit + 24);
  194. bs->bit = 0;
  195. } else {
  196. for (bytes = l >> 3, shift = 24, v = 0; bytes;
  197. bytes--, shift -= 8)
  198. v |= (unsigned int)(*bs->cur++) << shift;
  199. if (l < 32) {
  200. v |= (unsigned int)(*bs->cur) << shift;
  201. v <<= bs->bit;
  202. } else if (l > 32) {
  203. v <<= bs->bit;
  204. v |= (*bs->cur) >> (8 - bs->bit);
  205. }
  206. bs->bit = l & 0x7;
  207. }
  208. v &= 0xffffffff << (32 - b);
  209. return v;
  210. }
  211. /*
  212. * Assume bs is aligned and sizeof(unsigned int) == 4
  213. */
  214. static unsigned int get_uint(struct bitstr *bs, int b)
  215. {
  216. unsigned int v = 0;
  217. switch (b) {
  218. case 4:
  219. v |= *bs->cur++;
  220. v <<= 8;
  221. fallthrough;
  222. case 3:
  223. v |= *bs->cur++;
  224. v <<= 8;
  225. fallthrough;
  226. case 2:
  227. v |= *bs->cur++;
  228. v <<= 8;
  229. fallthrough;
  230. case 1:
  231. v |= *bs->cur++;
  232. break;
  233. }
  234. return v;
  235. }
  236. static int decode_nul(struct bitstr *bs, const struct field_t *f,
  237. char *base, int level)
  238. {
  239. PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
  240. return H323_ERROR_NONE;
  241. }
  242. static int decode_bool(struct bitstr *bs, const struct field_t *f,
  243. char *base, int level)
  244. {
  245. PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
  246. INC_BIT(bs);
  247. if (nf_h323_error_boundary(bs, 0, 0))
  248. return H323_ERROR_BOUND;
  249. return H323_ERROR_NONE;
  250. }
  251. static int decode_oid(struct bitstr *bs, const struct field_t *f,
  252. char *base, int level)
  253. {
  254. int len;
  255. PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
  256. BYTE_ALIGN(bs);
  257. if (nf_h323_error_boundary(bs, 1, 0))
  258. return H323_ERROR_BOUND;
  259. len = *bs->cur++;
  260. bs->cur += len;
  261. if (nf_h323_error_boundary(bs, 0, 0))
  262. return H323_ERROR_BOUND;
  263. return H323_ERROR_NONE;
  264. }
  265. static int decode_int(struct bitstr *bs, const struct field_t *f,
  266. char *base, int level)
  267. {
  268. unsigned int len;
  269. PRINT("%*.s%s", level * TAB_SIZE, " ", f->name);
  270. switch (f->sz) {
  271. case BYTE: /* Range == 256 */
  272. BYTE_ALIGN(bs);
  273. bs->cur++;
  274. break;
  275. case WORD: /* 257 <= Range <= 64K */
  276. BYTE_ALIGN(bs);
  277. bs->cur += 2;
  278. break;
  279. case CONS: /* 64K < Range < 4G */
  280. if (nf_h323_error_boundary(bs, 0, 2))
  281. return H323_ERROR_BOUND;
  282. len = get_bits(bs, 2) + 1;
  283. BYTE_ALIGN(bs);
  284. if (base && (f->attr & DECODE)) { /* timeToLive */
  285. unsigned int v = get_uint(bs, len) + f->lb;
  286. PRINT(" = %u", v);
  287. *((unsigned int *)(base + f->offset)) = v;
  288. }
  289. bs->cur += len;
  290. break;
  291. case UNCO:
  292. BYTE_ALIGN(bs);
  293. if (nf_h323_error_boundary(bs, 2, 0))
  294. return H323_ERROR_BOUND;
  295. len = get_len(bs);
  296. bs->cur += len;
  297. break;
  298. default: /* 2 <= Range <= 255 */
  299. INC_BITS(bs, f->sz);
  300. break;
  301. }
  302. PRINT("\n");
  303. if (nf_h323_error_boundary(bs, 0, 0))
  304. return H323_ERROR_BOUND;
  305. return H323_ERROR_NONE;
  306. }
  307. static int decode_enum(struct bitstr *bs, const struct field_t *f,
  308. char *base, int level)
  309. {
  310. PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
  311. if ((f->attr & EXT) && get_bit(bs)) {
  312. INC_BITS(bs, 7);
  313. } else {
  314. INC_BITS(bs, f->sz);
  315. }
  316. if (nf_h323_error_boundary(bs, 0, 0))
  317. return H323_ERROR_BOUND;
  318. return H323_ERROR_NONE;
  319. }
  320. static int decode_bitstr(struct bitstr *bs, const struct field_t *f,
  321. char *base, int level)
  322. {
  323. unsigned int len;
  324. PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
  325. BYTE_ALIGN(bs);
  326. switch (f->sz) {
  327. case FIXD: /* fixed length > 16 */
  328. len = f->lb;
  329. break;
  330. case WORD: /* 2-byte length */
  331. if (nf_h323_error_boundary(bs, 2, 0))
  332. return H323_ERROR_BOUND;
  333. len = (*bs->cur++) << 8;
  334. len += (*bs->cur++) + f->lb;
  335. break;
  336. case SEMI:
  337. if (nf_h323_error_boundary(bs, 2, 0))
  338. return H323_ERROR_BOUND;
  339. len = get_len(bs);
  340. break;
  341. default:
  342. len = 0;
  343. break;
  344. }
  345. bs->cur += len >> 3;
  346. bs->bit = len & 7;
  347. if (nf_h323_error_boundary(bs, 0, 0))
  348. return H323_ERROR_BOUND;
  349. return H323_ERROR_NONE;
  350. }
  351. static int decode_numstr(struct bitstr *bs, const struct field_t *f,
  352. char *base, int level)
  353. {
  354. unsigned int len;
  355. PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
  356. /* 2 <= Range <= 255 */
  357. if (nf_h323_error_boundary(bs, 0, f->sz))
  358. return H323_ERROR_BOUND;
  359. len = get_bits(bs, f->sz) + f->lb;
  360. BYTE_ALIGN(bs);
  361. INC_BITS(bs, (len << 2));
  362. if (nf_h323_error_boundary(bs, 0, 0))
  363. return H323_ERROR_BOUND;
  364. return H323_ERROR_NONE;
  365. }
  366. static int decode_octstr(struct bitstr *bs, const struct field_t *f,
  367. char *base, int level)
  368. {
  369. unsigned int len;
  370. PRINT("%*.s%s", level * TAB_SIZE, " ", f->name);
  371. switch (f->sz) {
  372. case FIXD: /* Range == 1 */
  373. if (f->lb > 2) {
  374. BYTE_ALIGN(bs);
  375. if (base && (f->attr & DECODE)) {
  376. /* The IP Address */
  377. IFTHEN(f->lb == 4,
  378. PRINT(" = %d.%d.%d.%d:%d",
  379. bs->cur[0], bs->cur[1],
  380. bs->cur[2], bs->cur[3],
  381. bs->cur[4] * 256 + bs->cur[5]));
  382. *((unsigned int *)(base + f->offset)) =
  383. bs->cur - bs->buf;
  384. }
  385. }
  386. len = f->lb;
  387. break;
  388. case BYTE: /* Range == 256 */
  389. BYTE_ALIGN(bs);
  390. if (nf_h323_error_boundary(bs, 1, 0))
  391. return H323_ERROR_BOUND;
  392. len = (*bs->cur++) + f->lb;
  393. break;
  394. case SEMI:
  395. BYTE_ALIGN(bs);
  396. if (nf_h323_error_boundary(bs, 2, 0))
  397. return H323_ERROR_BOUND;
  398. len = get_len(bs) + f->lb;
  399. break;
  400. default: /* 2 <= Range <= 255 */
  401. if (nf_h323_error_boundary(bs, 0, f->sz))
  402. return H323_ERROR_BOUND;
  403. len = get_bits(bs, f->sz) + f->lb;
  404. BYTE_ALIGN(bs);
  405. break;
  406. }
  407. bs->cur += len;
  408. PRINT("\n");
  409. if (nf_h323_error_boundary(bs, 0, 0))
  410. return H323_ERROR_BOUND;
  411. return H323_ERROR_NONE;
  412. }
  413. static int decode_bmpstr(struct bitstr *bs, const struct field_t *f,
  414. char *base, int level)
  415. {
  416. unsigned int len;
  417. PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
  418. switch (f->sz) {
  419. case BYTE: /* Range == 256 */
  420. BYTE_ALIGN(bs);
  421. if (nf_h323_error_boundary(bs, 1, 0))
  422. return H323_ERROR_BOUND;
  423. len = (*bs->cur++) + f->lb;
  424. break;
  425. default: /* 2 <= Range <= 255 */
  426. if (nf_h323_error_boundary(bs, 0, f->sz))
  427. return H323_ERROR_BOUND;
  428. len = get_bits(bs, f->sz) + f->lb;
  429. BYTE_ALIGN(bs);
  430. break;
  431. }
  432. bs->cur += len << 1;
  433. if (nf_h323_error_boundary(bs, 0, 0))
  434. return H323_ERROR_BOUND;
  435. return H323_ERROR_NONE;
  436. }
  437. static int decode_seq(struct bitstr *bs, const struct field_t *f,
  438. char *base, int level)
  439. {
  440. unsigned int ext, bmp, i, opt, len = 0, bmp2, bmp2_len;
  441. int err;
  442. const struct field_t *son;
  443. unsigned char *beg = NULL;
  444. PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
  445. /* Decode? */
  446. base = (base && (f->attr & DECODE)) ? base + f->offset : NULL;
  447. /* Extensible? */
  448. if (nf_h323_error_boundary(bs, 0, 1))
  449. return H323_ERROR_BOUND;
  450. ext = (f->attr & EXT) ? get_bit(bs) : 0;
  451. /* Get fields bitmap */
  452. if (nf_h323_error_boundary(bs, 0, f->sz))
  453. return H323_ERROR_BOUND;
  454. bmp = get_bitmap(bs, f->sz);
  455. if (base)
  456. *(unsigned int *)base = bmp;
  457. /* Decode the root components */
  458. for (i = opt = 0, son = f->fields; i < f->lb; i++, son++) {
  459. if (son->attr & STOP) {
  460. PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
  461. son->name);
  462. return H323_ERROR_STOP;
  463. }
  464. if (son->attr & OPT) { /* Optional component */
  465. if (!((0x80000000U >> (opt++)) & bmp)) /* Not exist */
  466. continue;
  467. }
  468. /* Decode */
  469. if (son->attr & OPEN) { /* Open field */
  470. if (nf_h323_error_boundary(bs, 2, 0))
  471. return H323_ERROR_BOUND;
  472. len = get_len(bs);
  473. if (nf_h323_error_boundary(bs, len, 0))
  474. return H323_ERROR_BOUND;
  475. if (!base || !(son->attr & DECODE)) {
  476. PRINT("%*.s%s\n", (level + 1) * TAB_SIZE,
  477. " ", son->name);
  478. bs->cur += len;
  479. continue;
  480. }
  481. beg = bs->cur;
  482. /* Decode */
  483. if ((err = (Decoders[son->type]) (bs, son, base,
  484. level + 1)) <
  485. H323_ERROR_NONE)
  486. return err;
  487. bs->cur = beg + len;
  488. bs->bit = 0;
  489. } else if ((err = (Decoders[son->type]) (bs, son, base,
  490. level + 1)) <
  491. H323_ERROR_NONE)
  492. return err;
  493. }
  494. /* No extension? */
  495. if (!ext)
  496. return H323_ERROR_NONE;
  497. /* Get the extension bitmap */
  498. if (nf_h323_error_boundary(bs, 0, 7))
  499. return H323_ERROR_BOUND;
  500. bmp2_len = get_bits(bs, 7) + 1;
  501. if (nf_h323_error_boundary(bs, 0, bmp2_len))
  502. return H323_ERROR_BOUND;
  503. bmp2 = get_bitmap(bs, bmp2_len);
  504. bmp |= bmp2 >> f->sz;
  505. if (base)
  506. *(unsigned int *)base = bmp;
  507. BYTE_ALIGN(bs);
  508. /* Decode the extension components */
  509. for (opt = 0; opt < bmp2_len; opt++, i++, son++) {
  510. /* Check Range */
  511. if (i >= f->ub) { /* Newer Version? */
  512. if (nf_h323_error_boundary(bs, 2, 0))
  513. return H323_ERROR_BOUND;
  514. len = get_len(bs);
  515. if (nf_h323_error_boundary(bs, len, 0))
  516. return H323_ERROR_BOUND;
  517. bs->cur += len;
  518. continue;
  519. }
  520. if (son->attr & STOP) {
  521. PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
  522. son->name);
  523. return H323_ERROR_STOP;
  524. }
  525. if (!((0x80000000 >> opt) & bmp2)) /* Not present */
  526. continue;
  527. if (nf_h323_error_boundary(bs, 2, 0))
  528. return H323_ERROR_BOUND;
  529. len = get_len(bs);
  530. if (nf_h323_error_boundary(bs, len, 0))
  531. return H323_ERROR_BOUND;
  532. if (!base || !(son->attr & DECODE)) {
  533. PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
  534. son->name);
  535. bs->cur += len;
  536. continue;
  537. }
  538. beg = bs->cur;
  539. if ((err = (Decoders[son->type]) (bs, son, base,
  540. level + 1)) <
  541. H323_ERROR_NONE)
  542. return err;
  543. bs->cur = beg + len;
  544. bs->bit = 0;
  545. }
  546. return H323_ERROR_NONE;
  547. }
  548. static int decode_seqof(struct bitstr *bs, const struct field_t *f,
  549. char *base, int level)
  550. {
  551. unsigned int count, effective_count = 0, i, len = 0;
  552. int err;
  553. const struct field_t *son;
  554. unsigned char *beg = NULL;
  555. PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
  556. /* Decode? */
  557. base = (base && (f->attr & DECODE)) ? base + f->offset : NULL;
  558. /* Decode item count */
  559. switch (f->sz) {
  560. case BYTE:
  561. BYTE_ALIGN(bs);
  562. if (nf_h323_error_boundary(bs, 1, 0))
  563. return H323_ERROR_BOUND;
  564. count = *bs->cur++;
  565. break;
  566. case WORD:
  567. BYTE_ALIGN(bs);
  568. if (nf_h323_error_boundary(bs, 2, 0))
  569. return H323_ERROR_BOUND;
  570. count = *bs->cur++;
  571. count <<= 8;
  572. count += *bs->cur++;
  573. break;
  574. case SEMI:
  575. BYTE_ALIGN(bs);
  576. if (nf_h323_error_boundary(bs, 2, 0))
  577. return H323_ERROR_BOUND;
  578. count = get_len(bs);
  579. break;
  580. default:
  581. if (nf_h323_error_boundary(bs, 0, f->sz))
  582. return H323_ERROR_BOUND;
  583. count = get_bits(bs, f->sz);
  584. break;
  585. }
  586. count += f->lb;
  587. /* Write Count */
  588. if (base) {
  589. effective_count = count > f->ub ? f->ub : count;
  590. *(unsigned int *)base = effective_count;
  591. base += sizeof(unsigned int);
  592. }
  593. /* Decode nested field */
  594. son = f->fields;
  595. if (base)
  596. base -= son->offset;
  597. for (i = 0; i < count; i++) {
  598. if (son->attr & OPEN) {
  599. BYTE_ALIGN(bs);
  600. if (nf_h323_error_boundary(bs, 2, 0))
  601. return H323_ERROR_BOUND;
  602. len = get_len(bs);
  603. if (nf_h323_error_boundary(bs, len, 0))
  604. return H323_ERROR_BOUND;
  605. if (!base || !(son->attr & DECODE)) {
  606. PRINT("%*.s%s\n", (level + 1) * TAB_SIZE,
  607. " ", son->name);
  608. bs->cur += len;
  609. continue;
  610. }
  611. beg = bs->cur;
  612. if ((err = (Decoders[son->type]) (bs, son,
  613. i <
  614. effective_count ?
  615. base : NULL,
  616. level + 1)) <
  617. H323_ERROR_NONE)
  618. return err;
  619. bs->cur = beg + len;
  620. bs->bit = 0;
  621. } else
  622. if ((err = (Decoders[son->type]) (bs, son,
  623. i <
  624. effective_count ?
  625. base : NULL,
  626. level + 1)) <
  627. H323_ERROR_NONE)
  628. return err;
  629. if (base)
  630. base += son->offset;
  631. }
  632. return H323_ERROR_NONE;
  633. }
  634. static int decode_choice(struct bitstr *bs, const struct field_t *f,
  635. char *base, int level)
  636. {
  637. unsigned int type, ext, len = 0;
  638. int err;
  639. const struct field_t *son;
  640. unsigned char *beg = NULL;
  641. PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
  642. /* Decode? */
  643. base = (base && (f->attr & DECODE)) ? base + f->offset : NULL;
  644. /* Decode the choice index number */
  645. if (nf_h323_error_boundary(bs, 0, 1))
  646. return H323_ERROR_BOUND;
  647. if ((f->attr & EXT) && get_bit(bs)) {
  648. ext = 1;
  649. if (nf_h323_error_boundary(bs, 0, 7))
  650. return H323_ERROR_BOUND;
  651. type = get_bits(bs, 7) + f->lb;
  652. } else {
  653. ext = 0;
  654. if (nf_h323_error_boundary(bs, 0, f->sz))
  655. return H323_ERROR_BOUND;
  656. type = get_bits(bs, f->sz);
  657. if (type >= f->lb)
  658. return H323_ERROR_RANGE;
  659. }
  660. /* Write Type */
  661. if (base)
  662. *(unsigned int *)base = type;
  663. /* Check Range */
  664. if (type >= f->ub) { /* Newer version? */
  665. BYTE_ALIGN(bs);
  666. if (nf_h323_error_boundary(bs, 2, 0))
  667. return H323_ERROR_BOUND;
  668. len = get_len(bs);
  669. if (nf_h323_error_boundary(bs, len, 0))
  670. return H323_ERROR_BOUND;
  671. bs->cur += len;
  672. return H323_ERROR_NONE;
  673. }
  674. /* Transfer to son level */
  675. son = &f->fields[type];
  676. if (son->attr & STOP) {
  677. PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ", son->name);
  678. return H323_ERROR_STOP;
  679. }
  680. if (ext || (son->attr & OPEN)) {
  681. BYTE_ALIGN(bs);
  682. if (nf_h323_error_boundary(bs, len, 0))
  683. return H323_ERROR_BOUND;
  684. len = get_len(bs);
  685. if (nf_h323_error_boundary(bs, len, 0))
  686. return H323_ERROR_BOUND;
  687. if (!base || !(son->attr & DECODE)) {
  688. PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
  689. son->name);
  690. bs->cur += len;
  691. return H323_ERROR_NONE;
  692. }
  693. beg = bs->cur;
  694. if ((err = (Decoders[son->type]) (bs, son, base, level + 1)) <
  695. H323_ERROR_NONE)
  696. return err;
  697. bs->cur = beg + len;
  698. bs->bit = 0;
  699. } else if ((err = (Decoders[son->type]) (bs, son, base, level + 1)) <
  700. H323_ERROR_NONE)
  701. return err;
  702. return H323_ERROR_NONE;
  703. }
  704. int DecodeRasMessage(unsigned char *buf, size_t sz, RasMessage *ras)
  705. {
  706. static const struct field_t ras_message = {
  707. FNAME("RasMessage") CHOICE, 5, 24, 32, DECODE | EXT,
  708. 0, _RasMessage
  709. };
  710. struct bitstr bs;
  711. bs.buf = bs.beg = bs.cur = buf;
  712. bs.end = buf + sz;
  713. bs.bit = 0;
  714. return decode_choice(&bs, &ras_message, (char *) ras, 0);
  715. }
  716. static int DecodeH323_UserInformation(unsigned char *buf, unsigned char *beg,
  717. size_t sz, H323_UserInformation *uuie)
  718. {
  719. static const struct field_t h323_userinformation = {
  720. FNAME("H323-UserInformation") SEQ, 1, 2, 2, DECODE | EXT,
  721. 0, _H323_UserInformation
  722. };
  723. struct bitstr bs;
  724. bs.buf = buf;
  725. bs.beg = bs.cur = beg;
  726. bs.end = beg + sz;
  727. bs.bit = 0;
  728. return decode_seq(&bs, &h323_userinformation, (char *) uuie, 0);
  729. }
  730. int DecodeMultimediaSystemControlMessage(unsigned char *buf, size_t sz,
  731. MultimediaSystemControlMessage *
  732. mscm)
  733. {
  734. static const struct field_t multimediasystemcontrolmessage = {
  735. FNAME("MultimediaSystemControlMessage") CHOICE, 2, 4, 4,
  736. DECODE | EXT, 0, _MultimediaSystemControlMessage
  737. };
  738. struct bitstr bs;
  739. bs.buf = bs.beg = bs.cur = buf;
  740. bs.end = buf + sz;
  741. bs.bit = 0;
  742. return decode_choice(&bs, &multimediasystemcontrolmessage,
  743. (char *) mscm, 0);
  744. }
  745. int DecodeQ931(unsigned char *buf, size_t sz, Q931 *q931)
  746. {
  747. unsigned char *p = buf;
  748. int len;
  749. if (!p || sz < 1)
  750. return H323_ERROR_BOUND;
  751. /* Protocol Discriminator */
  752. if (*p != 0x08) {
  753. PRINT("Unknown Protocol Discriminator\n");
  754. return H323_ERROR_RANGE;
  755. }
  756. p++;
  757. sz--;
  758. /* CallReferenceValue */
  759. if (sz < 1)
  760. return H323_ERROR_BOUND;
  761. len = *p++;
  762. sz--;
  763. if (sz < len)
  764. return H323_ERROR_BOUND;
  765. p += len;
  766. sz -= len;
  767. /* Message Type */
  768. if (sz < 2)
  769. return H323_ERROR_BOUND;
  770. q931->MessageType = *p++;
  771. sz--;
  772. PRINT("MessageType = %02X\n", q931->MessageType);
  773. if (*p & 0x80) {
  774. p++;
  775. sz--;
  776. }
  777. /* Decode Information Elements */
  778. while (sz > 0) {
  779. if (*p == 0x7e) { /* UserUserIE */
  780. if (sz < 3)
  781. break;
  782. p++;
  783. len = *p++ << 8;
  784. len |= *p++;
  785. sz -= 3;
  786. if (sz < len)
  787. break;
  788. p++;
  789. len--;
  790. return DecodeH323_UserInformation(buf, p, len,
  791. &q931->UUIE);
  792. }
  793. p++;
  794. sz--;
  795. if (sz < 1)
  796. break;
  797. len = *p++;
  798. sz--;
  799. if (sz < len)
  800. break;
  801. p += len;
  802. sz -= len;
  803. }
  804. PRINT("Q.931 UUIE not found\n");
  805. return H323_ERROR_BOUND;
  806. }