libxt_HMARK.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * (C) 2012 by Hans Schillstrom <hans.schillstrom@ericsson.com>
  3. * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
  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. * Description: shared library add-on to iptables to add HMARK target support
  10. *
  11. * Initial development by Hans Schillstrom. Pablo's improvements to this piece
  12. * of software has been sponsored by Sophos Astaro <http://www.sophos.com>.
  13. */
  14. #include <stdbool.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "xtables.h"
  18. #include <linux/netfilter/xt_HMARK.h>
  19. static void HMARK_help(void)
  20. {
  21. printf(
  22. "HMARK target options, i.e. modify hash calculation by:\n"
  23. " --hmark-tuple [src|dst|sport|dport|spi|proto|ct][,...]\n"
  24. " --hmark-mod value nfmark modulus value\n"
  25. " --hmark-offset value Last action add value to nfmark\n\n"
  26. " --hmark-rnd Random see for hashing\n"
  27. " Alternatively, fine tuning of what will be included in hash calculation\n"
  28. " --hmark-src-prefix length Source address mask CIDR prefix\n"
  29. " --hmark-dst-prefix length Dest address mask CIDR prefix\n"
  30. " --hmark-sport-mask value Mask src port with value\n"
  31. " --hmark-dport-mask value Mask dst port with value\n"
  32. " --hmark-spi-mask value For esp and ah AND spi with value\n"
  33. " --hmark-sport value OR src port with value\n"
  34. " --hmark-dport value OR dst port with value\n"
  35. " --hmark-spi value For esp and ah OR spi with value\n"
  36. " --hmark-proto-mask value Mask Protocol with value\n");
  37. }
  38. #define hi struct xt_hmark_info
  39. enum {
  40. O_HMARK_SADDR_MASK,
  41. O_HMARK_DADDR_MASK,
  42. O_HMARK_SPI,
  43. O_HMARK_SPI_MASK,
  44. O_HMARK_SPORT,
  45. O_HMARK_DPORT,
  46. O_HMARK_SPORT_MASK,
  47. O_HMARK_DPORT_MASK,
  48. O_HMARK_PROTO_MASK,
  49. O_HMARK_RND,
  50. O_HMARK_MODULUS,
  51. O_HMARK_OFFSET,
  52. O_HMARK_CT,
  53. O_HMARK_TYPE,
  54. };
  55. #define HMARK_OPT_PKT_MASK \
  56. ((1 << O_HMARK_SADDR_MASK) | \
  57. (1 << O_HMARK_DADDR_MASK) | \
  58. (1 << O_HMARK_SPI_MASK) | \
  59. (1 << O_HMARK_SPORT_MASK) | \
  60. (1 << O_HMARK_DPORT_MASK) | \
  61. (1 << O_HMARK_PROTO_MASK) | \
  62. (1 << O_HMARK_SPI_MASK) | \
  63. (1 << O_HMARK_SPORT) | \
  64. (1 << O_HMARK_DPORT) | \
  65. (1 << O_HMARK_SPI))
  66. static const struct xt_option_entry HMARK_opts[] = {
  67. { .name = "hmark-tuple",
  68. .type = XTTYPE_STRING,
  69. .id = O_HMARK_TYPE,
  70. },
  71. { .name = "hmark-src-prefix",
  72. .type = XTTYPE_PLENMASK,
  73. .id = O_HMARK_SADDR_MASK,
  74. .flags = XTOPT_PUT, XTOPT_POINTER(hi, src_mask)
  75. },
  76. { .name = "hmark-dst-prefix",
  77. .type = XTTYPE_PLENMASK,
  78. .id = O_HMARK_DADDR_MASK,
  79. .flags = XTOPT_PUT, XTOPT_POINTER(hi, dst_mask)
  80. },
  81. { .name = "hmark-sport-mask",
  82. .type = XTTYPE_UINT16,
  83. .id = O_HMARK_SPORT_MASK,
  84. .flags = XTOPT_PUT, XTOPT_POINTER(hi, port_mask.p16.src)
  85. },
  86. { .name = "hmark-dport-mask",
  87. .type = XTTYPE_UINT16,
  88. .id = O_HMARK_DPORT_MASK,
  89. .flags = XTOPT_PUT, XTOPT_POINTER(hi, port_mask.p16.dst)
  90. },
  91. { .name = "hmark-spi-mask",
  92. .type = XTTYPE_UINT32,
  93. .id = O_HMARK_SPI_MASK,
  94. .flags = XTOPT_PUT, XTOPT_POINTER(hi, port_mask.v32)
  95. },
  96. { .name = "hmark-sport",
  97. .type = XTTYPE_UINT16,
  98. .id = O_HMARK_SPORT,
  99. .flags = XTOPT_PUT, XTOPT_POINTER(hi, port_set.p16.src)
  100. },
  101. { .name = "hmark-dport",
  102. .type = XTTYPE_UINT16,
  103. .id = O_HMARK_DPORT,
  104. .flags = XTOPT_PUT, XTOPT_POINTER(hi, port_set.p16.dst)
  105. },
  106. { .name = "hmark-spi",
  107. .type = XTTYPE_UINT32,
  108. .id = O_HMARK_SPI,
  109. .flags = XTOPT_PUT, XTOPT_POINTER(hi, port_set.v32)
  110. },
  111. { .name = "hmark-proto-mask",
  112. .type = XTTYPE_UINT16,
  113. .id = O_HMARK_PROTO_MASK,
  114. .flags = XTOPT_PUT, XTOPT_POINTER(hi, proto_mask)
  115. },
  116. { .name = "hmark-rnd",
  117. .type = XTTYPE_UINT32,
  118. .id = O_HMARK_RND,
  119. .flags = XTOPT_PUT, XTOPT_POINTER(hi, hashrnd)
  120. },
  121. { .name = "hmark-mod",
  122. .type = XTTYPE_UINT32,
  123. .id = O_HMARK_MODULUS,
  124. .min = 1,
  125. .flags = XTOPT_PUT | XTOPT_MAND, XTOPT_POINTER(hi, hmodulus)
  126. },
  127. { .name = "hmark-offset",
  128. .type = XTTYPE_UINT32,
  129. .id = O_HMARK_OFFSET,
  130. .flags = XTOPT_PUT, XTOPT_POINTER(hi, hoffset)
  131. },
  132. XTOPT_TABLEEND,
  133. };
  134. static int
  135. hmark_parse(const char *type, size_t len, struct xt_hmark_info *info,
  136. unsigned int *xflags)
  137. {
  138. if (strncasecmp(type, "ct", len) == 0) {
  139. info->flags |= XT_HMARK_FLAG(XT_HMARK_CT);
  140. *xflags |= (1 << O_HMARK_CT);
  141. } else if (strncasecmp(type, "src", len) == 0) {
  142. memset(&info->src_mask, 0xff, sizeof(info->src_mask));
  143. info->flags |= XT_HMARK_FLAG(XT_HMARK_SADDR_MASK);
  144. *xflags |= (1 << O_HMARK_SADDR_MASK);
  145. } else if (strncasecmp(type, "dst", len) == 0) {
  146. memset(&info->dst_mask, 0xff, sizeof(info->dst_mask));
  147. info->flags |= XT_HMARK_FLAG(XT_HMARK_DADDR_MASK);
  148. *xflags |= (1 << O_HMARK_DADDR_MASK);
  149. } else if (strncasecmp(type, "sport", len) == 0) {
  150. memset(&info->port_mask.p16.src, 0xff,
  151. sizeof(info->port_mask.p16.src));
  152. info->flags |= XT_HMARK_FLAG(XT_HMARK_SPORT_MASK);
  153. *xflags |= (1 << O_HMARK_SPORT_MASK);
  154. } else if (strncasecmp(type, "dport", len) == 0) {
  155. memset(&info->port_mask.p16.dst, 0xff,
  156. sizeof(info->port_mask.p16.dst));
  157. info->flags |= XT_HMARK_FLAG(XT_HMARK_DPORT_MASK);
  158. *xflags |= (1 << O_HMARK_DPORT_MASK);
  159. } else if (strncasecmp(type, "proto", len) == 0) {
  160. memset(&info->proto_mask, 0xff, sizeof(info->proto_mask));
  161. info->flags |= XT_HMARK_FLAG(XT_HMARK_PROTO_MASK);
  162. *xflags |= (1 << O_HMARK_PROTO_MASK);
  163. } else if (strncasecmp(type, "spi", len) == 0) {
  164. memset(&info->port_mask.v32, 0xff, sizeof(info->port_mask.v32));
  165. info->flags |= XT_HMARK_FLAG(XT_HMARK_SPI_MASK);
  166. *xflags |= (1 << O_HMARK_SPI_MASK);
  167. } else
  168. return 0;
  169. return 1;
  170. }
  171. static void
  172. hmark_parse_type(struct xt_option_call *cb)
  173. {
  174. const char *arg = cb->arg;
  175. struct xt_hmark_info *info = cb->data;
  176. const char *comma;
  177. while ((comma = strchr(arg, ',')) != NULL) {
  178. if (comma == arg ||
  179. !hmark_parse(arg, comma-arg, info, &cb->xflags))
  180. xtables_error(PARAMETER_PROBLEM, "Bad type \"%s\"", arg);
  181. arg = comma+1;
  182. }
  183. if (!*arg)
  184. xtables_error(PARAMETER_PROBLEM, "\"--hmark-tuple\" requires "
  185. "a list of types with no "
  186. "spaces, e.g. "
  187. "src,dst,sport,dport,proto");
  188. if (strlen(arg) == 0 ||
  189. !hmark_parse(arg, strlen(arg), info, &cb->xflags))
  190. xtables_error(PARAMETER_PROBLEM, "Bad type \"%s\"", arg);
  191. }
  192. static void HMARK_parse(struct xt_option_call *cb, int plen)
  193. {
  194. struct xt_hmark_info *info = cb->data;
  195. xtables_option_parse(cb);
  196. switch (cb->entry->id) {
  197. case O_HMARK_TYPE:
  198. hmark_parse_type(cb);
  199. break;
  200. case O_HMARK_SADDR_MASK:
  201. info->flags |= XT_HMARK_FLAG(XT_HMARK_SADDR_MASK);
  202. break;
  203. case O_HMARK_DADDR_MASK:
  204. info->flags |= XT_HMARK_FLAG(XT_HMARK_DADDR_MASK);
  205. break;
  206. case O_HMARK_SPI:
  207. info->port_set.v32 = htonl(cb->val.u32);
  208. info->flags |= XT_HMARK_FLAG(XT_HMARK_SPI);
  209. break;
  210. case O_HMARK_SPORT:
  211. info->port_set.p16.src = htons(cb->val.u16);
  212. info->flags |= XT_HMARK_FLAG(XT_HMARK_SPORT);
  213. break;
  214. case O_HMARK_DPORT:
  215. info->port_set.p16.dst = htons(cb->val.u16);
  216. info->flags |= XT_HMARK_FLAG(XT_HMARK_DPORT);
  217. break;
  218. case O_HMARK_SPORT_MASK:
  219. info->port_mask.p16.src = htons(cb->val.u16);
  220. info->flags |= XT_HMARK_FLAG(XT_HMARK_SPORT_MASK);
  221. break;
  222. case O_HMARK_DPORT_MASK:
  223. info->port_mask.p16.dst = htons(cb->val.u16);
  224. info->flags |= XT_HMARK_FLAG(XT_HMARK_DPORT_MASK);
  225. break;
  226. case O_HMARK_SPI_MASK:
  227. info->port_mask.v32 = htonl(cb->val.u32);
  228. info->flags |= XT_HMARK_FLAG(XT_HMARK_SPI_MASK);
  229. break;
  230. case O_HMARK_PROTO_MASK:
  231. info->flags |= XT_HMARK_FLAG(XT_HMARK_PROTO_MASK);
  232. break;
  233. case O_HMARK_RND:
  234. info->flags |= XT_HMARK_FLAG(XT_HMARK_RND);
  235. break;
  236. case O_HMARK_MODULUS:
  237. info->flags |= XT_HMARK_FLAG(XT_HMARK_MODULUS);
  238. break;
  239. case O_HMARK_OFFSET:
  240. info->flags |= XT_HMARK_FLAG(XT_HMARK_OFFSET);
  241. break;
  242. case O_HMARK_CT:
  243. info->flags |= XT_HMARK_FLAG(XT_HMARK_CT);
  244. break;
  245. }
  246. cb->xflags |= (1 << cb->entry->id);
  247. }
  248. static void HMARK_ip4_parse(struct xt_option_call *cb)
  249. {
  250. HMARK_parse(cb, 32);
  251. }
  252. static void HMARK_ip6_parse(struct xt_option_call *cb)
  253. {
  254. HMARK_parse(cb, 128);
  255. }
  256. static void HMARK_check(struct xt_fcheck_call *cb)
  257. {
  258. if (!(cb->xflags & (1 << O_HMARK_MODULUS)))
  259. xtables_error(PARAMETER_PROBLEM, "--hmark-mod is mandatory");
  260. if (!(cb->xflags & (1 << O_HMARK_RND)))
  261. xtables_error(PARAMETER_PROBLEM, "--hmark-rnd is mandatory");
  262. if (cb->xflags & (1 << O_HMARK_SPI_MASK) &&
  263. (cb->xflags & ((1 << O_HMARK_SPORT_MASK) |
  264. (1 << O_HMARK_DPORT_MASK))))
  265. xtables_error(PARAMETER_PROBLEM, "you cannot use "
  266. "--hmark-spi-mask and --hmark-?port-mask,"
  267. "at the same time");
  268. if (!((cb->xflags & HMARK_OPT_PKT_MASK) ||
  269. cb->xflags & (1 << O_HMARK_CT)))
  270. xtables_error(PARAMETER_PROBLEM, "you have to specify "
  271. "--hmark-tuple at least");
  272. }
  273. static void HMARK_print(const struct xt_hmark_info *info)
  274. {
  275. if (info->flags & XT_HMARK_FLAG(XT_HMARK_SPORT_MASK))
  276. printf("sport-mask 0x%x ", htons(info->port_mask.p16.src));
  277. if (info->flags & XT_HMARK_FLAG(XT_HMARK_DPORT_MASK))
  278. printf("dport-mask 0x%x ", htons(info->port_mask.p16.dst));
  279. if (info->flags & XT_HMARK_FLAG(XT_HMARK_SPI_MASK))
  280. printf("spi-mask 0x%x ", htonl(info->port_mask.v32));
  281. if (info->flags & XT_HMARK_FLAG(XT_HMARK_SPORT))
  282. printf("sport 0x%x ", htons(info->port_set.p16.src));
  283. if (info->flags & XT_HMARK_FLAG(XT_HMARK_DPORT))
  284. printf("dport 0x%x ", htons(info->port_set.p16.dst));
  285. if (info->flags & XT_HMARK_FLAG(XT_HMARK_SPI))
  286. printf("spi 0x%x ", htonl(info->port_set.v32));
  287. if (info->flags & XT_HMARK_FLAG(XT_HMARK_PROTO_MASK))
  288. printf("proto-mask 0x%x ", info->proto_mask);
  289. if (info->flags & XT_HMARK_FLAG(XT_HMARK_RND))
  290. printf("rnd 0x%x ", info->hashrnd);
  291. }
  292. static void HMARK_ip6_print(const void *ip,
  293. const struct xt_entry_target *target, int numeric)
  294. {
  295. const struct xt_hmark_info *info =
  296. (const struct xt_hmark_info *)target->data;
  297. printf(" HMARK ");
  298. if (info->flags & XT_HMARK_FLAG(XT_HMARK_MODULUS))
  299. printf("mod %u ", info->hmodulus);
  300. if (info->flags & XT_HMARK_FLAG(XT_HMARK_OFFSET))
  301. printf("+ 0x%x ", info->hoffset);
  302. if (info->flags & XT_HMARK_FLAG(XT_HMARK_CT))
  303. printf("ct, ");
  304. if (info->flags & XT_HMARK_FLAG(XT_HMARK_SADDR_MASK))
  305. printf("src-prefix %s ",
  306. xtables_ip6mask_to_numeric(&info->src_mask.in6) + 1);
  307. if (info->flags & XT_HMARK_FLAG(XT_HMARK_DADDR_MASK))
  308. printf("dst-prefix %s ",
  309. xtables_ip6mask_to_numeric(&info->dst_mask.in6) + 1);
  310. HMARK_print(info);
  311. }
  312. static void HMARK_ip4_print(const void *ip,
  313. const struct xt_entry_target *target, int numeric)
  314. {
  315. const struct xt_hmark_info *info =
  316. (const struct xt_hmark_info *)target->data;
  317. printf(" HMARK ");
  318. if (info->flags & XT_HMARK_FLAG(XT_HMARK_MODULUS))
  319. printf("mod %u ", info->hmodulus);
  320. if (info->flags & XT_HMARK_FLAG(XT_HMARK_OFFSET))
  321. printf("+ 0x%x ", info->hoffset);
  322. if (info->flags & XT_HMARK_FLAG(XT_HMARK_CT))
  323. printf("ct, ");
  324. if (info->flags & XT_HMARK_FLAG(XT_HMARK_SADDR_MASK))
  325. printf("src-prefix %u ",
  326. xtables_ipmask_to_cidr(&info->src_mask.in));
  327. if (info->flags & XT_HMARK_FLAG(XT_HMARK_DADDR_MASK))
  328. printf("dst-prefix %u ",
  329. xtables_ipmask_to_cidr(&info->dst_mask.in));
  330. HMARK_print(info);
  331. }
  332. static void HMARK_save(const struct xt_hmark_info *info)
  333. {
  334. if (info->flags & XT_HMARK_FLAG(XT_HMARK_SPORT_MASK))
  335. printf(" --hmark-sport-mask 0x%04x",
  336. htons(info->port_mask.p16.src));
  337. if (info->flags & XT_HMARK_FLAG(XT_HMARK_DPORT_MASK))
  338. printf(" --hmark-dport-mask 0x%04x",
  339. htons(info->port_mask.p16.dst));
  340. if (info->flags & XT_HMARK_FLAG(XT_HMARK_SPI_MASK))
  341. printf(" --hmark-spi-mask 0x%08x",
  342. htonl(info->port_mask.v32));
  343. if (info->flags & XT_HMARK_FLAG(XT_HMARK_SPORT))
  344. printf(" --hmark-sport 0x%04x",
  345. htons(info->port_set.p16.src));
  346. if (info->flags & XT_HMARK_FLAG(XT_HMARK_DPORT))
  347. printf(" --hmark-dport 0x%04x",
  348. htons(info->port_set.p16.dst));
  349. if (info->flags & XT_HMARK_FLAG(XT_HMARK_SPI))
  350. printf(" --hmark-spi 0x%08x", htonl(info->port_set.v32));
  351. if (info->flags & XT_HMARK_FLAG(XT_HMARK_PROTO_MASK))
  352. printf(" --hmark-proto-mask 0x%02x", info->proto_mask);
  353. if (info->flags & XT_HMARK_FLAG(XT_HMARK_RND))
  354. printf(" --hmark-rnd 0x%08x", info->hashrnd);
  355. if (info->flags & XT_HMARK_FLAG(XT_HMARK_MODULUS))
  356. printf(" --hmark-mod %u", info->hmodulus);
  357. if (info->flags & XT_HMARK_FLAG(XT_HMARK_OFFSET))
  358. printf(" --hmark-offset %u", info->hoffset);
  359. if (info->flags & XT_HMARK_FLAG(XT_HMARK_CT))
  360. printf(" --hmark-tuple ct");
  361. }
  362. static void HMARK_ip6_save(const void *ip, const struct xt_entry_target *target)
  363. {
  364. const struct xt_hmark_info *info =
  365. (const struct xt_hmark_info *)target->data;
  366. int ret;
  367. if (info->flags & XT_HMARK_FLAG(XT_HMARK_SADDR_MASK)) {
  368. ret = xtables_ip6mask_to_cidr(&info->src_mask.in6);
  369. printf(" --hmark-src-prefix %d", ret);
  370. }
  371. if (info->flags & XT_HMARK_FLAG(XT_HMARK_DADDR_MASK)) {
  372. ret = xtables_ip6mask_to_cidr(&info->dst_mask.in6);
  373. printf(" --hmark-dst-prefix %d", ret);
  374. }
  375. HMARK_save(info);
  376. }
  377. static void HMARK_ip4_save(const void *ip, const struct xt_entry_target *target)
  378. {
  379. const struct xt_hmark_info *info =
  380. (const struct xt_hmark_info *)target->data;
  381. int ret;
  382. if (info->flags & XT_HMARK_FLAG(XT_HMARK_SADDR_MASK)) {
  383. ret = xtables_ipmask_to_cidr(&info->src_mask.in);
  384. printf(" --hmark-src-prefix %d", ret);
  385. }
  386. if (info->flags & XT_HMARK_FLAG(XT_HMARK_DADDR_MASK)) {
  387. ret = xtables_ipmask_to_cidr(&info->dst_mask.in);
  388. printf(" --hmark-dst-prefix %d", ret);
  389. }
  390. HMARK_save(info);
  391. }
  392. static struct xtables_target mark_tg_reg[] = {
  393. {
  394. .family = NFPROTO_IPV4,
  395. .name = "HMARK",
  396. .version = XTABLES_VERSION,
  397. .size = XT_ALIGN(sizeof(struct xt_hmark_info)),
  398. .userspacesize = XT_ALIGN(sizeof(struct xt_hmark_info)),
  399. .help = HMARK_help,
  400. .print = HMARK_ip4_print,
  401. .save = HMARK_ip4_save,
  402. .x6_parse = HMARK_ip4_parse,
  403. .x6_fcheck = HMARK_check,
  404. .x6_options = HMARK_opts,
  405. },
  406. {
  407. .family = NFPROTO_IPV6,
  408. .name = "HMARK",
  409. .version = XTABLES_VERSION,
  410. .size = XT_ALIGN(sizeof(struct xt_hmark_info)),
  411. .userspacesize = XT_ALIGN(sizeof(struct xt_hmark_info)),
  412. .help = HMARK_help,
  413. .print = HMARK_ip6_print,
  414. .save = HMARK_ip6_save,
  415. .x6_parse = HMARK_ip6_parse,
  416. .x6_fcheck = HMARK_check,
  417. .x6_options = HMARK_opts,
  418. },
  419. };
  420. void _init(void)
  421. {
  422. xtables_register_targets(mark_tg_reg, ARRAY_SIZE(mark_tg_reg));
  423. }