libxt_TEE.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * "TEE" target extension for iptables
  3. * Copyright © Sebastian Claßen <sebastian.classen [at] freenet.ag>, 2007
  4. * Jan Engelhardt <jengelh [at] medozas de>, 2007 - 2010
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License; either
  8. * version 2 of the License, or any later version, as published by the
  9. * Free Software Foundation.
  10. */
  11. #include <sys/socket.h>
  12. #include <getopt.h>
  13. #include <stdbool.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <arpa/inet.h>
  18. #include <net/if.h>
  19. #include <netinet/in.h>
  20. #include <xtables.h>
  21. #include <linux/netfilter.h>
  22. #include <linux/netfilter/x_tables.h>
  23. #include <linux/netfilter/xt_TEE.h>
  24. enum {
  25. O_GATEWAY = 0,
  26. O_OIF,
  27. };
  28. #define s struct xt_tee_tginfo
  29. static const struct xt_option_entry tee_tg_opts[] = {
  30. {.name = "gateway", .id = O_GATEWAY, .type = XTTYPE_HOST,
  31. .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, gw)},
  32. {.name = "oif", .id = O_OIF, .type = XTTYPE_STRING,
  33. .flags = XTOPT_PUT, XTOPT_POINTER(s, oif)},
  34. XTOPT_TABLEEND,
  35. };
  36. #undef s
  37. static void tee_tg_help(void)
  38. {
  39. printf(
  40. "TEE target options:\n"
  41. " --gateway IPADDR Route packet via the gateway given by address\n"
  42. " --oif NAME Include oif in route calculation\n"
  43. "\n");
  44. }
  45. static void tee_tg_print(const void *ip, const struct xt_entry_target *target,
  46. int numeric)
  47. {
  48. const struct xt_tee_tginfo *info = (const void *)target->data;
  49. if (numeric)
  50. printf(" TEE gw:%s", xtables_ipaddr_to_numeric(&info->gw.in));
  51. else
  52. printf(" TEE gw:%s", xtables_ipaddr_to_anyname(&info->gw.in));
  53. if (*info->oif != '\0')
  54. printf(" oif=%s", info->oif);
  55. }
  56. static void tee_tg6_print(const void *ip, const struct xt_entry_target *target,
  57. int numeric)
  58. {
  59. const struct xt_tee_tginfo *info = (const void *)target->data;
  60. if (numeric)
  61. printf(" TEE gw:%s", xtables_ip6addr_to_numeric(&info->gw.in6));
  62. else
  63. printf(" TEE gw:%s", xtables_ip6addr_to_anyname(&info->gw.in6));
  64. if (*info->oif != '\0')
  65. printf(" oif=%s", info->oif);
  66. }
  67. static void tee_tg_save(const void *ip, const struct xt_entry_target *target)
  68. {
  69. const struct xt_tee_tginfo *info = (const void *)target->data;
  70. printf(" --gateway %s", xtables_ipaddr_to_numeric(&info->gw.in));
  71. if (*info->oif != '\0')
  72. printf(" --oif %s", info->oif);
  73. }
  74. static void tee_tg6_save(const void *ip, const struct xt_entry_target *target)
  75. {
  76. const struct xt_tee_tginfo *info = (const void *)target->data;
  77. printf(" --gateway %s", xtables_ip6addr_to_numeric(&info->gw.in6));
  78. if (*info->oif != '\0')
  79. printf(" --oif %s", info->oif);
  80. }
  81. static struct xtables_target tee_tg_reg[] = {
  82. {
  83. .name = "TEE",
  84. .version = XTABLES_VERSION,
  85. .revision = 1,
  86. .family = NFPROTO_IPV4,
  87. .size = XT_ALIGN(sizeof(struct xt_tee_tginfo)),
  88. .userspacesize = XT_ALIGN(sizeof(struct xt_tee_tginfo)),
  89. .help = tee_tg_help,
  90. .print = tee_tg_print,
  91. .save = tee_tg_save,
  92. .x6_parse = xtables_option_parse,
  93. .x6_options = tee_tg_opts,
  94. },
  95. {
  96. .name = "TEE",
  97. .version = XTABLES_VERSION,
  98. .revision = 1,
  99. .family = NFPROTO_IPV6,
  100. .size = XT_ALIGN(sizeof(struct xt_tee_tginfo)),
  101. .userspacesize = XT_ALIGN(sizeof(struct xt_tee_tginfo)),
  102. .help = tee_tg_help,
  103. .print = tee_tg6_print,
  104. .save = tee_tg6_save,
  105. .x6_parse = xtables_option_parse,
  106. .x6_options = tee_tg_opts,
  107. },
  108. };
  109. void _init(void)
  110. {
  111. xtables_register_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg));
  112. }