libip6t_dst.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. enum {
  8. O_DSTLEN = 0,
  9. O_DSTOPTS,
  10. };
  11. static void dst_help(void)
  12. {
  13. printf(
  14. "dst match options:\n"
  15. "[!] --dst-len length total length of this header\n"
  16. " --dst-opts TYPE[:LEN][,TYPE[:LEN]...]\n"
  17. " Options and its length (list, max: %d)\n",
  18. IP6T_OPTS_OPTSNR);
  19. }
  20. static const struct xt_option_entry dst_opts[] = {
  21. {.name = "dst-len", .id = O_DSTLEN, .type = XTTYPE_UINT32,
  22. .flags = XTOPT_INVERT | XTOPT_PUT,
  23. XTOPT_POINTER(struct ip6t_opts, hdrlen)},
  24. {.name = "dst-opts", .id = O_DSTOPTS, .type = XTTYPE_STRING},
  25. XTOPT_TABLEEND,
  26. };
  27. static uint32_t
  28. parse_opts_num(const char *idstr, const char *typestr)
  29. {
  30. unsigned long int id;
  31. char* ep;
  32. id = strtoul(idstr, &ep, 0);
  33. if ( idstr == ep ) {
  34. xtables_error(PARAMETER_PROBLEM,
  35. "dst: no valid digits in %s `%s'", typestr, idstr);
  36. }
  37. if ( id == ULONG_MAX && errno == ERANGE ) {
  38. xtables_error(PARAMETER_PROBLEM,
  39. "%s `%s' specified too big: would overflow",
  40. typestr, idstr);
  41. }
  42. if ( *idstr != '\0' && *ep != '\0' ) {
  43. xtables_error(PARAMETER_PROBLEM,
  44. "dst: error parsing %s `%s'", typestr, idstr);
  45. }
  46. return id;
  47. }
  48. static int
  49. parse_options(const char *optsstr, uint16_t *opts)
  50. {
  51. char *buffer, *cp, *next, *range;
  52. unsigned int i;
  53. buffer = strdup(optsstr);
  54. if (!buffer)
  55. 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)
  60. *next++='\0';
  61. range = strchr(cp, ':');
  62. if (range) {
  63. if (i == IP6T_OPTS_OPTSNR-1)
  64. xtables_error(PARAMETER_PROBLEM,
  65. "too many ports specified");
  66. *range++ = '\0';
  67. }
  68. opts[i] = (parse_opts_num(cp, "opt") & 0xFF) << 8;
  69. if (range) {
  70. if (opts[i] == 0)
  71. xtables_error(PARAMETER_PROBLEM,
  72. "PAD0 hasn't got length");
  73. opts[i] |= parse_opts_num(range, "length") & 0xFF;
  74. } else
  75. opts[i] |= (0x00FF);
  76. #ifdef DEBUG
  77. printf("opts str: %s %s\n", cp, range);
  78. printf("opts opt: %04X\n", opts[i]);
  79. #endif
  80. }
  81. if (cp)
  82. xtables_error(PARAMETER_PROBLEM, "too many addresses specified");
  83. free(buffer);
  84. #ifdef DEBUG
  85. printf("addr nr: %d\n", i);
  86. #endif
  87. return i;
  88. }
  89. static void dst_parse(struct xt_option_call *cb)
  90. {
  91. struct ip6t_opts *optinfo = cb->data;
  92. xtables_option_parse(cb);
  93. switch (cb->entry->id) {
  94. case O_DSTLEN:
  95. optinfo->flags |= IP6T_OPTS_LEN;
  96. break;
  97. case O_DSTOPTS:
  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. printf(" ");
  108. for(i = 0; i < optsnr; i++) {
  109. printf("%d", (optsp[i] & 0xFF00) >> 8);
  110. if ((optsp[i] & 0x00FF) != 0x00FF)
  111. printf(":%d", (optsp[i] & 0x00FF));
  112. printf("%c", (i != optsnr - 1) ? ',' : ' ');
  113. }
  114. }
  115. static void dst_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(" dst");
  120. if (optinfo->flags & IP6T_OPTS_LEN)
  121. printf(" length:%s%u",
  122. optinfo->invflags & IP6T_OPTS_INV_LEN ? "!" : "",
  123. optinfo->hdrlen);
  124. if (optinfo->flags & IP6T_OPTS_OPTS)
  125. 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 dst_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 --dst-len %u",
  136. (optinfo->invflags & IP6T_OPTS_INV_LEN) ? " !" : "",
  137. optinfo->hdrlen);
  138. }
  139. if (optinfo->flags & IP6T_OPTS_OPTS)
  140. printf(" --dst-opts");
  141. print_options(optinfo->optsnr, (uint16_t *)optinfo->opts);
  142. }
  143. static struct xtables_match dst_mt6_reg = {
  144. .name = "dst",
  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 = dst_help,
  150. .print = dst_print,
  151. .save = dst_save,
  152. .x6_parse = dst_parse,
  153. .x6_options = dst_opts,
  154. };
  155. void
  156. _init(void)
  157. {
  158. xtables_register_match(&dst_mt6_reg);
  159. }