libxt_iprange.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <xtables.h>
  6. #include <linux/netfilter.h>
  7. #include <linux/netfilter/xt_iprange.h>
  8. struct ipt_iprange {
  9. /* Inclusive: network order. */
  10. __be32 min_ip, max_ip;
  11. };
  12. struct ipt_iprange_info {
  13. struct ipt_iprange src;
  14. struct ipt_iprange dst;
  15. /* Flags from above */
  16. uint8_t flags;
  17. };
  18. enum {
  19. O_SRC_RANGE = 0,
  20. O_DST_RANGE,
  21. };
  22. static void iprange_mt_help(void)
  23. {
  24. printf(
  25. "iprange match options:\n"
  26. "[!] --src-range ip[-ip] Match source IP in the specified range\n"
  27. "[!] --dst-range ip[-ip] Match destination IP in the specified range\n");
  28. }
  29. static const struct xt_option_entry iprange_mt_opts[] = {
  30. {.name = "src-range", .id = O_SRC_RANGE, .type = XTTYPE_STRING,
  31. .flags = XTOPT_INVERT},
  32. {.name = "dst-range", .id = O_DST_RANGE, .type = XTTYPE_STRING,
  33. .flags = XTOPT_INVERT},
  34. XTOPT_TABLEEND,
  35. };
  36. static void
  37. iprange_parse_spec(const char *from, const char *to, union nf_inet_addr *range,
  38. uint8_t family, const char *optname)
  39. {
  40. const char *spec[2] = {from, to};
  41. struct in6_addr *ia6;
  42. struct in_addr *ia4;
  43. unsigned int i;
  44. memset(range, 0, sizeof(union nf_inet_addr) * 2);
  45. if (family == NFPROTO_IPV6) {
  46. for (i = 0; i < ARRAY_SIZE(spec); ++i) {
  47. ia6 = xtables_numeric_to_ip6addr(spec[i]);
  48. if (ia6 == NULL)
  49. xtables_param_act(XTF_BAD_VALUE, "iprange",
  50. optname, spec[i]);
  51. range[i].in6 = *ia6;
  52. }
  53. } else {
  54. for (i = 0; i < ARRAY_SIZE(spec); ++i) {
  55. ia4 = xtables_numeric_to_ipaddr(spec[i]);
  56. if (ia4 == NULL)
  57. xtables_param_act(XTF_BAD_VALUE, "iprange",
  58. optname, spec[i]);
  59. range[i].in = *ia4;
  60. }
  61. }
  62. }
  63. static void iprange_parse_range(const char *oarg, union nf_inet_addr *range,
  64. uint8_t family, const char *optname)
  65. {
  66. char *arg = strdup(oarg);
  67. char *dash;
  68. if (arg == NULL)
  69. xtables_error(RESOURCE_PROBLEM, "strdup");
  70. dash = strchr(arg, '-');
  71. if (dash == NULL) {
  72. iprange_parse_spec(arg, arg, range, family, optname);
  73. free(arg);
  74. return;
  75. }
  76. *dash = '\0';
  77. iprange_parse_spec(arg, dash + 1, range, family, optname);
  78. if (memcmp(&range[0], &range[1], sizeof(*range)) > 0)
  79. fprintf(stderr, "xt_iprange: range %s-%s is reversed and "
  80. "will never match\n", arg, dash + 1);
  81. free(arg);
  82. }
  83. static void iprange_parse(struct xt_option_call *cb)
  84. {
  85. struct ipt_iprange_info *info = cb->data;
  86. union nf_inet_addr range[2];
  87. xtables_option_parse(cb);
  88. switch (cb->entry->id) {
  89. case O_SRC_RANGE:
  90. info->flags |= IPRANGE_SRC;
  91. if (cb->invert)
  92. info->flags |= IPRANGE_SRC_INV;
  93. iprange_parse_range(cb->arg, range, NFPROTO_IPV4, "--src-range");
  94. info->src.min_ip = range[0].ip;
  95. info->src.max_ip = range[1].ip;
  96. break;
  97. case O_DST_RANGE:
  98. info->flags |= IPRANGE_DST;
  99. if (cb->invert)
  100. info->flags |= IPRANGE_DST_INV;
  101. iprange_parse_range(cb->arg, range, NFPROTO_IPV4, "--dst-range");
  102. info->dst.min_ip = range[0].ip;
  103. info->dst.max_ip = range[1].ip;
  104. break;
  105. }
  106. }
  107. static void iprange_mt_parse(struct xt_option_call *cb, uint8_t nfproto)
  108. {
  109. struct xt_iprange_mtinfo *info = cb->data;
  110. xtables_option_parse(cb);
  111. switch (cb->entry->id) {
  112. case O_SRC_RANGE:
  113. iprange_parse_range(cb->arg, &info->src_min, nfproto,
  114. "--src-range");
  115. info->flags |= IPRANGE_SRC;
  116. if (cb->invert)
  117. info->flags |= IPRANGE_SRC_INV;
  118. break;
  119. case O_DST_RANGE:
  120. iprange_parse_range(cb->arg, &info->dst_min, nfproto,
  121. "--dst-range");
  122. info->flags |= IPRANGE_DST;
  123. if (cb->invert)
  124. info->flags |= IPRANGE_DST_INV;
  125. break;
  126. }
  127. }
  128. static void iprange_mt4_parse(struct xt_option_call *cb)
  129. {
  130. iprange_mt_parse(cb, NFPROTO_IPV4);
  131. }
  132. static void iprange_mt6_parse(struct xt_option_call *cb)
  133. {
  134. iprange_mt_parse(cb, NFPROTO_IPV6);
  135. }
  136. static void iprange_mt_check(struct xt_fcheck_call *cb)
  137. {
  138. if (cb->xflags == 0)
  139. xtables_error(PARAMETER_PROBLEM,
  140. "iprange match: You must specify `--src-range' or `--dst-range'");
  141. }
  142. static void
  143. print_iprange(const struct ipt_iprange *range)
  144. {
  145. const unsigned char *byte_min, *byte_max;
  146. byte_min = (const unsigned char *)&range->min_ip;
  147. byte_max = (const unsigned char *)&range->max_ip;
  148. printf(" %u.%u.%u.%u-%u.%u.%u.%u",
  149. byte_min[0], byte_min[1], byte_min[2], byte_min[3],
  150. byte_max[0], byte_max[1], byte_max[2], byte_max[3]);
  151. }
  152. static void iprange_print(const void *ip, const struct xt_entry_match *match,
  153. int numeric)
  154. {
  155. const struct ipt_iprange_info *info = (const void *)match->data;
  156. if (info->flags & IPRANGE_SRC) {
  157. printf(" source IP range");
  158. if (info->flags & IPRANGE_SRC_INV)
  159. printf(" !");
  160. print_iprange(&info->src);
  161. }
  162. if (info->flags & IPRANGE_DST) {
  163. printf(" destination IP range");
  164. if (info->flags & IPRANGE_DST_INV)
  165. printf(" !");
  166. print_iprange(&info->dst);
  167. }
  168. }
  169. static void
  170. iprange_mt4_print(const void *ip, const struct xt_entry_match *match,
  171. int numeric)
  172. {
  173. const struct xt_iprange_mtinfo *info = (const void *)match->data;
  174. if (info->flags & IPRANGE_SRC) {
  175. printf(" source IP range");
  176. if (info->flags & IPRANGE_SRC_INV)
  177. printf(" !");
  178. /*
  179. * ipaddr_to_numeric() uses a static buffer, so cannot
  180. * combine the printf() calls.
  181. */
  182. printf(" %s", xtables_ipaddr_to_numeric(&info->src_min.in));
  183. printf("-%s", xtables_ipaddr_to_numeric(&info->src_max.in));
  184. }
  185. if (info->flags & IPRANGE_DST) {
  186. printf(" destination IP range");
  187. if (info->flags & IPRANGE_DST_INV)
  188. printf(" !");
  189. printf(" %s", xtables_ipaddr_to_numeric(&info->dst_min.in));
  190. printf("-%s", xtables_ipaddr_to_numeric(&info->dst_max.in));
  191. }
  192. }
  193. static void
  194. iprange_mt6_print(const void *ip, const struct xt_entry_match *match,
  195. int numeric)
  196. {
  197. const struct xt_iprange_mtinfo *info = (const void *)match->data;
  198. if (info->flags & IPRANGE_SRC) {
  199. printf(" source IP range");
  200. if (info->flags & IPRANGE_SRC_INV)
  201. printf(" !");
  202. /*
  203. * ipaddr_to_numeric() uses a static buffer, so cannot
  204. * combine the printf() calls.
  205. */
  206. printf(" %s", xtables_ip6addr_to_numeric(&info->src_min.in6));
  207. printf("-%s", xtables_ip6addr_to_numeric(&info->src_max.in6));
  208. }
  209. if (info->flags & IPRANGE_DST) {
  210. printf(" destination IP range");
  211. if (info->flags & IPRANGE_DST_INV)
  212. printf(" !");
  213. printf(" %s", xtables_ip6addr_to_numeric(&info->dst_min.in6));
  214. printf("-%s", xtables_ip6addr_to_numeric(&info->dst_max.in6));
  215. }
  216. }
  217. static void iprange_save(const void *ip, const struct xt_entry_match *match)
  218. {
  219. const struct ipt_iprange_info *info = (const void *)match->data;
  220. if (info->flags & IPRANGE_SRC) {
  221. if (info->flags & IPRANGE_SRC_INV)
  222. printf(" !");
  223. printf(" --src-range");
  224. print_iprange(&info->src);
  225. }
  226. if (info->flags & IPRANGE_DST) {
  227. if (info->flags & IPRANGE_DST_INV)
  228. printf(" !");
  229. printf(" --dst-range");
  230. print_iprange(&info->dst);
  231. }
  232. }
  233. static void iprange_mt4_save(const void *ip, const struct xt_entry_match *match)
  234. {
  235. const struct xt_iprange_mtinfo *info = (const void *)match->data;
  236. if (info->flags & IPRANGE_SRC) {
  237. if (info->flags & IPRANGE_SRC_INV)
  238. printf(" !");
  239. printf(" --src-range %s", xtables_ipaddr_to_numeric(&info->src_min.in));
  240. printf("-%s", xtables_ipaddr_to_numeric(&info->src_max.in));
  241. }
  242. if (info->flags & IPRANGE_DST) {
  243. if (info->flags & IPRANGE_DST_INV)
  244. printf(" !");
  245. printf(" --dst-range %s", xtables_ipaddr_to_numeric(&info->dst_min.in));
  246. printf("-%s", xtables_ipaddr_to_numeric(&info->dst_max.in));
  247. }
  248. }
  249. static void iprange_mt6_save(const void *ip, const struct xt_entry_match *match)
  250. {
  251. const struct xt_iprange_mtinfo *info = (const void *)match->data;
  252. if (info->flags & IPRANGE_SRC) {
  253. if (info->flags & IPRANGE_SRC_INV)
  254. printf(" !");
  255. printf(" --src-range %s", xtables_ip6addr_to_numeric(&info->src_min.in6));
  256. printf("-%s", xtables_ip6addr_to_numeric(&info->src_max.in6));
  257. }
  258. if (info->flags & IPRANGE_DST) {
  259. if (info->flags & IPRANGE_DST_INV)
  260. printf(" !");
  261. printf(" --dst-range %s", xtables_ip6addr_to_numeric(&info->dst_min.in6));
  262. printf("-%s", xtables_ip6addr_to_numeric(&info->dst_max.in6));
  263. }
  264. }
  265. static struct xtables_match iprange_mt_reg[] = {
  266. {
  267. .version = XTABLES_VERSION,
  268. .name = "iprange",
  269. .revision = 0,
  270. .family = NFPROTO_IPV4,
  271. .size = XT_ALIGN(sizeof(struct ipt_iprange_info)),
  272. .userspacesize = XT_ALIGN(sizeof(struct ipt_iprange_info)),
  273. .help = iprange_mt_help,
  274. .x6_parse = iprange_parse,
  275. .x6_fcheck = iprange_mt_check,
  276. .print = iprange_print,
  277. .save = iprange_save,
  278. .x6_options = iprange_mt_opts,
  279. },
  280. {
  281. .version = XTABLES_VERSION,
  282. .name = "iprange",
  283. .revision = 1,
  284. .family = NFPROTO_IPV4,
  285. .size = XT_ALIGN(sizeof(struct xt_iprange_mtinfo)),
  286. .userspacesize = XT_ALIGN(sizeof(struct xt_iprange_mtinfo)),
  287. .help = iprange_mt_help,
  288. .x6_parse = iprange_mt4_parse,
  289. .x6_fcheck = iprange_mt_check,
  290. .print = iprange_mt4_print,
  291. .save = iprange_mt4_save,
  292. .x6_options = iprange_mt_opts,
  293. },
  294. {
  295. .version = XTABLES_VERSION,
  296. .name = "iprange",
  297. .revision = 1,
  298. .family = NFPROTO_IPV6,
  299. .size = XT_ALIGN(sizeof(struct xt_iprange_mtinfo)),
  300. .userspacesize = XT_ALIGN(sizeof(struct xt_iprange_mtinfo)),
  301. .help = iprange_mt_help,
  302. .x6_parse = iprange_mt6_parse,
  303. .x6_fcheck = iprange_mt_check,
  304. .print = iprange_mt6_print,
  305. .save = iprange_mt6_save,
  306. .x6_options = iprange_mt_opts,
  307. },
  308. };
  309. void _init(void)
  310. {
  311. xtables_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
  312. }