libip6t_ipv6header.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* ipv6header match - matches IPv6 packets based
  2. on whether they contain certain headers */
  3. /* Original idea: Brad Chapman
  4. * Rewritten by: Andras Kis-Szabo <kisza@sch.bme.hu> */
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <netdb.h>
  10. #include <xtables.h>
  11. #include <linux/netfilter_ipv6/ip6t_ipv6header.h>
  12. enum {
  13. O_HEADER = 0,
  14. O_SOFT,
  15. };
  16. /* A few hardcoded protocols for 'all' and in case the user has no
  17. * /etc/protocols */
  18. struct pprot {
  19. char *name;
  20. uint8_t num;
  21. };
  22. struct numflag {
  23. uint8_t proto;
  24. uint8_t flag;
  25. };
  26. static const struct pprot chain_protos[] = {
  27. { "hop-by-hop", IPPROTO_HOPOPTS },
  28. { "protocol", IPPROTO_RAW },
  29. { "hop", IPPROTO_HOPOPTS },
  30. { "dst", IPPROTO_DSTOPTS },
  31. { "route", IPPROTO_ROUTING },
  32. { "frag", IPPROTO_FRAGMENT },
  33. { "auth", IPPROTO_AH },
  34. { "esp", IPPROTO_ESP },
  35. { "none", IPPROTO_NONE },
  36. { "prot", IPPROTO_RAW },
  37. { "0", IPPROTO_HOPOPTS },
  38. { "60", IPPROTO_DSTOPTS },
  39. { "43", IPPROTO_ROUTING },
  40. { "44", IPPROTO_FRAGMENT },
  41. { "51", IPPROTO_AH },
  42. { "50", IPPROTO_ESP },
  43. { "59", IPPROTO_NONE },
  44. { "255", IPPROTO_RAW },
  45. /* { "all", 0 }, */
  46. };
  47. static const struct numflag chain_flags[] = {
  48. { IPPROTO_HOPOPTS, MASK_HOPOPTS },
  49. { IPPROTO_DSTOPTS, MASK_DSTOPTS },
  50. { IPPROTO_ROUTING, MASK_ROUTING },
  51. { IPPROTO_FRAGMENT, MASK_FRAGMENT },
  52. { IPPROTO_AH, MASK_AH },
  53. { IPPROTO_ESP, MASK_ESP },
  54. { IPPROTO_NONE, MASK_NONE },
  55. { IPPROTO_RAW, MASK_PROTO },
  56. };
  57. static const char *
  58. proto_to_name(uint8_t proto, int nolookup)
  59. {
  60. unsigned int i;
  61. if (proto && !nolookup) {
  62. const struct protoent *pent = getprotobynumber(proto);
  63. if (pent)
  64. return pent->p_name;
  65. }
  66. for (i = 0; i < ARRAY_SIZE(chain_protos); ++i)
  67. if (chain_protos[i].num == proto)
  68. return chain_protos[i].name;
  69. return NULL;
  70. }
  71. static uint16_t
  72. name_to_proto(const char *s)
  73. {
  74. unsigned int proto=0;
  75. const struct protoent *pent;
  76. if ((pent = getprotobyname(s)))
  77. proto = pent->p_proto;
  78. else {
  79. unsigned int i;
  80. for (i = 0; i < ARRAY_SIZE(chain_protos); ++i)
  81. if (strcmp(s, chain_protos[i].name) == 0) {
  82. proto = chain_protos[i].num;
  83. break;
  84. }
  85. if (i == ARRAY_SIZE(chain_protos))
  86. xtables_error(PARAMETER_PROBLEM,
  87. "unknown header `%s' specified",
  88. s);
  89. }
  90. return proto;
  91. }
  92. static unsigned int
  93. add_proto_to_mask(int proto){
  94. unsigned int i=0, flag=0;
  95. for (i = 0; i < ARRAY_SIZE(chain_flags); ++i)
  96. if (proto == chain_flags[i].proto){
  97. flag = chain_flags[i].flag;
  98. break;
  99. }
  100. if (i == ARRAY_SIZE(chain_flags))
  101. xtables_error(PARAMETER_PROBLEM,
  102. "unknown header `%d' specified",
  103. proto);
  104. return flag;
  105. }
  106. static void ipv6header_help(void)
  107. {
  108. printf(
  109. "ipv6header match options:\n"
  110. "[!] --header headers Type of header to match, by name\n"
  111. " names: hop,dst,route,frag,auth,esp,none,proto\n"
  112. " long names: hop-by-hop,ipv6-opts,ipv6-route,\n"
  113. " ipv6-frag,ah,esp,ipv6-nonxt,protocol\n"
  114. " numbers: 0,60,43,44,51,50,59\n"
  115. "--soft The header CONTAINS the specified extensions\n");
  116. }
  117. static const struct xt_option_entry ipv6header_opts[] = {
  118. {.name = "header", .id = O_HEADER, .type = XTTYPE_STRING,
  119. .flags = XTOPT_MAND | XTOPT_INVERT},
  120. {.name = "soft", .id = O_SOFT, .type = XTTYPE_NONE},
  121. XTOPT_TABLEEND,
  122. };
  123. static unsigned int
  124. parse_header(const char *flags) {
  125. unsigned int ret = 0;
  126. char *ptr;
  127. char *buffer;
  128. buffer = strdup(flags);
  129. for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ","))
  130. ret |= add_proto_to_mask(name_to_proto(ptr));
  131. free(buffer);
  132. return ret;
  133. }
  134. static void ipv6header_parse(struct xt_option_call *cb)
  135. {
  136. struct ip6t_ipv6header_info *info = cb->data;
  137. xtables_option_parse(cb);
  138. switch (cb->entry->id) {
  139. case O_HEADER:
  140. if (!(info->matchflags = parse_header(cb->arg)))
  141. xtables_error(PARAMETER_PROBLEM, "ip6t_ipv6header: cannot parse header names");
  142. if (cb->invert)
  143. info->invflags |= 0xFF;
  144. break;
  145. case O_SOFT:
  146. info->modeflag |= 0xFF;
  147. break;
  148. }
  149. }
  150. static void
  151. print_header(uint8_t flags){
  152. int have_flag = 0;
  153. while (flags) {
  154. unsigned int i;
  155. for (i = 0; (flags & chain_flags[i].flag) == 0; i++);
  156. if (have_flag)
  157. printf(",");
  158. printf("%s", proto_to_name(chain_flags[i].proto,0));
  159. have_flag = 1;
  160. flags &= ~chain_flags[i].flag;
  161. }
  162. if (!have_flag)
  163. printf("NONE");
  164. }
  165. static void ipv6header_print(const void *ip,
  166. const struct xt_entry_match *match, int numeric)
  167. {
  168. const struct ip6t_ipv6header_info *info = (const struct ip6t_ipv6header_info *)match->data;
  169. printf(" ipv6header");
  170. if (info->matchflags || info->invflags) {
  171. printf(" flags:%s", info->invflags ? "!" : "");
  172. if (numeric)
  173. printf("0x%02X", info->matchflags);
  174. else {
  175. print_header(info->matchflags);
  176. }
  177. }
  178. if (info->modeflag)
  179. printf(" soft");
  180. }
  181. static void ipv6header_save(const void *ip, const struct xt_entry_match *match)
  182. {
  183. const struct ip6t_ipv6header_info *info = (const struct ip6t_ipv6header_info *)match->data;
  184. printf("%s --header ", info->invflags ? " !" : "");
  185. print_header(info->matchflags);
  186. if (info->modeflag)
  187. printf(" --soft");
  188. }
  189. static struct xtables_match ipv6header_mt6_reg = {
  190. .name = "ipv6header",
  191. .version = XTABLES_VERSION,
  192. .family = NFPROTO_IPV6,
  193. .size = XT_ALIGN(sizeof(struct ip6t_ipv6header_info)),
  194. .userspacesize = XT_ALIGN(sizeof(struct ip6t_ipv6header_info)),
  195. .help = ipv6header_help,
  196. .print = ipv6header_print,
  197. .save = ipv6header_save,
  198. .x6_parse = ipv6header_parse,
  199. .x6_options = ipv6header_opts,
  200. };
  201. void _init(void)
  202. {
  203. xtables_register_match(&ipv6header_mt6_reg);
  204. }