libip6t_hbh.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <xtables.h>
  6. #include <linux/netfilter_ipv6/ip6t_opts.h>
  7. #define DEBUG 0
  8. enum {
  9. O_HBH_LEN = 0,
  10. O_HBH_OPTS,
  11. };
  12. static void hbh_help(void)
  13. {
  14. printf(
  15. "hbh match options:\n"
  16. "[!] --hbh-len length total length of this header\n"
  17. " --hbh-opts TYPE[:LEN][,TYPE[:LEN]...] \n"
  18. " Options and its length (list, max: %d)\n",
  19. IP6T_OPTS_OPTSNR);
  20. }
  21. static const struct xt_option_entry hbh_opts[] = {
  22. {.name = "hbh-len", .id = O_HBH_LEN, .type = XTTYPE_UINT32,
  23. .flags = XTOPT_INVERT | XTOPT_PUT,
  24. XTOPT_POINTER(struct ip6t_opts, hdrlen)},
  25. {.name = "hbh-opts", .id = O_HBH_OPTS, .type = XTTYPE_STRING},
  26. XTOPT_TABLEEND,
  27. };
  28. static uint32_t
  29. parse_opts_num(const char *idstr, const char *typestr)
  30. {
  31. unsigned long int id;
  32. char* ep;
  33. id = strtoul(idstr,&ep,0) ;
  34. if ( idstr == ep ) {
  35. xtables_error(PARAMETER_PROBLEM,
  36. "hbh: no valid digits in %s `%s'", typestr, idstr);
  37. }
  38. if ( id == ULONG_MAX && errno == ERANGE ) {
  39. xtables_error(PARAMETER_PROBLEM,
  40. "%s `%s' specified too big: would overflow",
  41. typestr, idstr);
  42. }
  43. if ( *idstr != '\0' && *ep != '\0' ) {
  44. xtables_error(PARAMETER_PROBLEM,
  45. "hbh: error parsing %s `%s'", typestr, idstr);
  46. }
  47. return id;
  48. }
  49. static int
  50. parse_options(const char *optsstr, uint16_t *opts)
  51. {
  52. char *buffer, *cp, *next, *range;
  53. unsigned int i;
  54. buffer = strdup(optsstr);
  55. if (!buffer) xtables_error(OTHER_PROBLEM, "strdup failed");
  56. for (cp=buffer, i=0; cp && i<IP6T_OPTS_OPTSNR; cp=next,i++)
  57. {
  58. next=strchr(cp, ',');
  59. if (next) *next++='\0';
  60. range = strchr(cp, ':');
  61. if (range) {
  62. if (i == IP6T_OPTS_OPTSNR-1)
  63. xtables_error(PARAMETER_PROBLEM,
  64. "too many ports specified");
  65. *range++ = '\0';
  66. }
  67. opts[i] = (parse_opts_num(cp, "opt") & 0xFF) << 8;
  68. if (range) {
  69. if (opts[i] == 0)
  70. xtables_error(PARAMETER_PROBLEM, "PAD0 has not got length");
  71. opts[i] |= parse_opts_num(range, "length") & 0xFF;
  72. } else {
  73. opts[i] |= (0x00FF);
  74. }
  75. #if DEBUG
  76. printf("opts str: %s %s\n", cp, range);
  77. printf("opts opt: %04X\n", opts[i]);
  78. #endif
  79. }
  80. if (cp) xtables_error(PARAMETER_PROBLEM, "too many addresses specified");
  81. free(buffer);
  82. #if DEBUG
  83. printf("addr nr: %d\n", i);
  84. #endif
  85. return i;
  86. }
  87. static void hbh_parse(struct xt_option_call *cb)
  88. {
  89. struct ip6t_opts *optinfo = cb->data;
  90. xtables_option_parse(cb);
  91. switch (cb->entry->id) {
  92. case O_HBH_LEN:
  93. if (cb->invert)
  94. optinfo->invflags |= IP6T_OPTS_INV_LEN;
  95. optinfo->flags |= IP6T_OPTS_LEN;
  96. break;
  97. case O_HBH_OPTS:
  98. optinfo->optsnr = parse_options(cb->arg, optinfo->opts);
  99. optinfo->flags |= IP6T_OPTS_OPTS;
  100. break;
  101. }
  102. }
  103. static void
  104. print_options(unsigned int optsnr, uint16_t *optsp)
  105. {
  106. unsigned int i;
  107. for(i=0; i<optsnr; i++){
  108. printf("%c", (i==0)?' ':',');
  109. printf("%d", (optsp[i] & 0xFF00)>>8);
  110. if ((optsp[i] & 0x00FF) != 0x00FF){
  111. printf(":%d", (optsp[i] & 0x00FF));
  112. }
  113. }
  114. }
  115. static void hbh_print(const void *ip, const struct xt_entry_match *match,
  116. int numeric)
  117. {
  118. const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data;
  119. printf(" hbh");
  120. if (optinfo->flags & IP6T_OPTS_LEN) {
  121. printf(" length");
  122. printf(":%s", optinfo->invflags & IP6T_OPTS_INV_LEN ? "!" : "");
  123. printf("%u", optinfo->hdrlen);
  124. }
  125. if (optinfo->flags & IP6T_OPTS_OPTS) printf(" opts");
  126. print_options(optinfo->optsnr, (uint16_t *)optinfo->opts);
  127. if (optinfo->invflags & ~IP6T_OPTS_INV_MASK)
  128. printf(" Unknown invflags: 0x%X",
  129. optinfo->invflags & ~IP6T_OPTS_INV_MASK);
  130. }
  131. static void hbh_save(const void *ip, const struct xt_entry_match *match)
  132. {
  133. const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data;
  134. if (optinfo->flags & IP6T_OPTS_LEN) {
  135. printf("%s --hbh-len %u",
  136. (optinfo->invflags & IP6T_OPTS_INV_LEN) ? " !" : "",
  137. optinfo->hdrlen);
  138. }
  139. if (optinfo->flags & IP6T_OPTS_OPTS)
  140. printf(" --hbh-opts");
  141. print_options(optinfo->optsnr, (uint16_t *)optinfo->opts);
  142. }
  143. static struct xtables_match hbh_mt6_reg = {
  144. .name = "hbh",
  145. .version = XTABLES_VERSION,
  146. .family = NFPROTO_IPV6,
  147. .size = XT_ALIGN(sizeof(struct ip6t_opts)),
  148. .userspacesize = XT_ALIGN(sizeof(struct ip6t_opts)),
  149. .help = hbh_help,
  150. .print = hbh_print,
  151. .save = hbh_save,
  152. .x6_parse = hbh_parse,
  153. .x6_options = hbh_opts,
  154. };
  155. void
  156. _init(void)
  157. {
  158. xtables_register_match(&hbh_mt6_reg);
  159. }