libip6t_mh.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* Shared library add-on to ip6tables to add mobility header support. */
  2. /*
  3. * Copyright (C)2006 USAGI/WIDE Project
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Author:
  10. * Masahide NAKAMURA @USAGI <masahide.nakamura.cz@hitachi.com>
  11. *
  12. * Based on libip6t_{icmpv6,udp}.c
  13. */
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <xtables.h>
  19. #include <linux/netfilter_ipv6/ip6t_mh.h>
  20. enum {
  21. O_MH_TYPE = 0,
  22. };
  23. struct mh_name {
  24. const char *name;
  25. uint8_t type;
  26. };
  27. static const struct mh_name mh_names[] = {
  28. { "binding-refresh-request", 0, },
  29. /* Alias */ { "brr", 0, },
  30. { "home-test-init", 1, },
  31. /* Alias */ { "hoti", 1, },
  32. { "careof-test-init", 2, },
  33. /* Alias */ { "coti", 2, },
  34. { "home-test", 3, },
  35. /* Alias */ { "hot", 3, },
  36. { "careof-test", 4, },
  37. /* Alias */ { "cot", 4, },
  38. { "binding-update", 5, },
  39. /* Alias */ { "bu", 5, },
  40. { "binding-acknowledgement", 6, },
  41. /* Alias */ { "ba", 6, },
  42. { "binding-error", 7, },
  43. /* Alias */ { "be", 7, },
  44. };
  45. static void print_types_all(void)
  46. {
  47. unsigned int i;
  48. printf("Valid MH types:");
  49. for (i = 0; i < ARRAY_SIZE(mh_names); ++i) {
  50. if (i && mh_names[i].type == mh_names[i-1].type)
  51. printf(" (%s)", mh_names[i].name);
  52. else
  53. printf("\n%s", mh_names[i].name);
  54. }
  55. printf("\n");
  56. }
  57. static void mh_help(void)
  58. {
  59. printf(
  60. "mh match options:\n"
  61. "[!] --mh-type type[:type] match mh type\n");
  62. print_types_all();
  63. }
  64. static void mh_init(struct xt_entry_match *m)
  65. {
  66. struct ip6t_mh *mhinfo = (struct ip6t_mh *)m->data;
  67. mhinfo->types[1] = 0xFF;
  68. }
  69. static unsigned int name_to_type(const char *name)
  70. {
  71. int namelen = strlen(name);
  72. static const unsigned int limit = ARRAY_SIZE(mh_names);
  73. unsigned int match = limit;
  74. unsigned int i;
  75. for (i = 0; i < limit; i++) {
  76. if (strncasecmp(mh_names[i].name, name, namelen) == 0) {
  77. int len = strlen(mh_names[i].name);
  78. if (match == limit || len == namelen)
  79. match = i;
  80. }
  81. }
  82. if (match != limit) {
  83. return mh_names[match].type;
  84. } else {
  85. unsigned int number;
  86. if (!xtables_strtoui(name, NULL, &number, 0, UINT8_MAX))
  87. xtables_error(PARAMETER_PROBLEM,
  88. "Invalid MH type `%s'\n", name);
  89. return number;
  90. }
  91. }
  92. static void parse_mh_types(const char *mhtype, uint8_t *types)
  93. {
  94. char *buffer;
  95. char *cp;
  96. buffer = strdup(mhtype);
  97. if ((cp = strchr(buffer, ':')) == NULL)
  98. types[0] = types[1] = name_to_type(buffer);
  99. else {
  100. *cp = '\0';
  101. cp++;
  102. types[0] = buffer[0] ? name_to_type(buffer) : 0;
  103. types[1] = cp[0] ? name_to_type(cp) : 0xFF;
  104. if (types[0] > types[1])
  105. xtables_error(PARAMETER_PROBLEM,
  106. "Invalid MH type range (min > max)");
  107. }
  108. free(buffer);
  109. }
  110. static void mh_parse(struct xt_option_call *cb)
  111. {
  112. struct ip6t_mh *mhinfo = cb->data;
  113. xtables_option_parse(cb);
  114. parse_mh_types(cb->arg, mhinfo->types);
  115. if (cb->invert)
  116. mhinfo->invflags |= IP6T_MH_INV_TYPE;
  117. }
  118. static const char *type_to_name(uint8_t type)
  119. {
  120. unsigned int i;
  121. for (i = 0; i < ARRAY_SIZE(mh_names); ++i)
  122. if (mh_names[i].type == type)
  123. return mh_names[i].name;
  124. return NULL;
  125. }
  126. static void print_type(uint8_t type, int numeric)
  127. {
  128. const char *name;
  129. if (numeric || !(name = type_to_name(type)))
  130. printf("%u", type);
  131. else
  132. printf("%s", name);
  133. }
  134. static void print_types(uint8_t min, uint8_t max, int invert, int numeric)
  135. {
  136. const char *inv = invert ? "!" : "";
  137. if (min != 0 || max != 0xFF || invert) {
  138. printf(" ");
  139. if (min == max) {
  140. printf("%s", inv);
  141. print_type(min, numeric);
  142. } else {
  143. printf("%s", inv);
  144. print_type(min, numeric);
  145. printf(":");
  146. print_type(max, numeric);
  147. }
  148. }
  149. }
  150. static void mh_print(const void *ip, const struct xt_entry_match *match,
  151. int numeric)
  152. {
  153. const struct ip6t_mh *mhinfo = (struct ip6t_mh *)match->data;
  154. printf(" mh");
  155. print_types(mhinfo->types[0], mhinfo->types[1],
  156. mhinfo->invflags & IP6T_MH_INV_TYPE,
  157. numeric);
  158. if (mhinfo->invflags & ~IP6T_MH_INV_MASK)
  159. printf(" Unknown invflags: 0x%X",
  160. mhinfo->invflags & ~IP6T_MH_INV_MASK);
  161. }
  162. static void mh_save(const void *ip, const struct xt_entry_match *match)
  163. {
  164. const struct ip6t_mh *mhinfo = (struct ip6t_mh *)match->data;
  165. if (mhinfo->types[0] == 0 && mhinfo->types[1] == 0xFF)
  166. return;
  167. if (mhinfo->invflags & IP6T_MH_INV_TYPE)
  168. printf(" !");
  169. if (mhinfo->types[0] != mhinfo->types[1])
  170. printf(" --mh-type %u:%u", mhinfo->types[0], mhinfo->types[1]);
  171. else
  172. printf(" --mh-type %u", mhinfo->types[0]);
  173. }
  174. static const struct xt_option_entry mh_opts[] = {
  175. {.name = "mh-type", .id = O_MH_TYPE, .type = XTTYPE_STRING,
  176. .flags = XTOPT_INVERT},
  177. XTOPT_TABLEEND,
  178. };
  179. static struct xtables_match mh_mt6_reg = {
  180. .name = "mh",
  181. .version = XTABLES_VERSION,
  182. .family = NFPROTO_IPV6,
  183. .size = XT_ALIGN(sizeof(struct ip6t_mh)),
  184. .userspacesize = XT_ALIGN(sizeof(struct ip6t_mh)),
  185. .help = mh_help,
  186. .init = mh_init,
  187. .x6_parse = mh_parse,
  188. .print = mh_print,
  189. .save = mh_save,
  190. .x6_options = mh_opts,
  191. };
  192. void _init(void)
  193. {
  194. xtables_register_match(&mh_mt6_reg);
  195. }