libip6t_SNPT.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <xtables.h>
  4. #include <linux/netfilter_ipv6/ip6_tables.h>
  5. #include <linux/netfilter_ipv6/ip6t_NPT.h>
  6. enum {
  7. O_SRC_PFX = 1 << 0,
  8. O_DST_PFX = 1 << 1,
  9. };
  10. static const struct xt_option_entry SNPT_options[] = {
  11. { .name = "src-pfx", .id = O_SRC_PFX, .type = XTTYPE_HOSTMASK,
  12. .flags = XTOPT_MAND },
  13. { .name = "dst-pfx", .id = O_DST_PFX, .type = XTTYPE_HOSTMASK,
  14. .flags = XTOPT_MAND },
  15. { }
  16. };
  17. static void SNPT_help(void)
  18. {
  19. printf("SNPT target options:"
  20. "\n"
  21. " --src-pfx prefix/length\n"
  22. " --dst-pfx prefix/length\n"
  23. "\n");
  24. }
  25. static void SNPT_parse(struct xt_option_call *cb)
  26. {
  27. struct ip6t_npt_tginfo *npt = cb->data;
  28. xtables_option_parse(cb);
  29. switch (cb->entry->id) {
  30. case O_SRC_PFX:
  31. npt->src_pfx = cb->val.haddr;
  32. npt->src_pfx_len = cb->val.hlen;
  33. break;
  34. case O_DST_PFX:
  35. npt->dst_pfx = cb->val.haddr;
  36. npt->dst_pfx_len = cb->val.hlen;
  37. break;
  38. }
  39. }
  40. static void SNPT_print(const void *ip, const struct xt_entry_target *target,
  41. int numeric)
  42. {
  43. const struct ip6t_npt_tginfo *npt = (const void *)target->data;
  44. printf("src-pfx %s/%u ", xtables_ip6addr_to_numeric(&npt->src_pfx.in6),
  45. npt->src_pfx_len);
  46. printf("dst-pfx %s/%u ", xtables_ip6addr_to_numeric(&npt->dst_pfx.in6),
  47. npt->dst_pfx_len);
  48. }
  49. static void SNPT_save(const void *ip, const struct xt_entry_target *target)
  50. {
  51. static const struct in6_addr zero_addr;
  52. const struct ip6t_npt_tginfo *info = (const void *)target->data;
  53. if (memcmp(&info->src_pfx.in6, &zero_addr, sizeof(zero_addr)) != 0 ||
  54. info->src_pfx_len != 0)
  55. printf("--src-pfx %s/%u ",
  56. xtables_ip6addr_to_numeric(&info->src_pfx.in6),
  57. info->src_pfx_len);
  58. if (memcmp(&info->dst_pfx.in6, &zero_addr, sizeof(zero_addr)) != 0 ||
  59. info->dst_pfx_len != 0)
  60. printf("--dst-pfx %s/%u ",
  61. xtables_ip6addr_to_numeric(&info->dst_pfx.in6),
  62. info->dst_pfx_len);
  63. }
  64. static struct xtables_target snpt_tg_reg = {
  65. .name = "SNPT",
  66. .version = XTABLES_VERSION,
  67. .family = NFPROTO_IPV6,
  68. .size = XT_ALIGN(sizeof(struct ip6t_npt_tginfo)),
  69. .userspacesize = offsetof(struct ip6t_npt_tginfo, adjustment),
  70. .help = SNPT_help,
  71. .x6_parse = SNPT_parse,
  72. .print = SNPT_print,
  73. .save = SNPT_save,
  74. .x6_options = SNPT_options,
  75. };
  76. void _init(void)
  77. {
  78. xtables_register_target(&snpt_tg_reg);
  79. }