libxt_TPROXY.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * shared library add-on to iptables to add TPROXY target support.
  3. *
  4. * Copyright (C) 2002-2008 BalaBit IT Ltd.
  5. */
  6. #include <stdio.h>
  7. #include <limits.h>
  8. #include <xtables.h>
  9. #include <linux/netfilter/xt_TPROXY.h>
  10. #include <arpa/inet.h>
  11. enum {
  12. P_PORT = 0,
  13. P_ADDR,
  14. P_MARK,
  15. F_PORT = 1 << P_PORT,
  16. F_ADDR = 1 << P_ADDR,
  17. F_MARK = 1 << P_MARK,
  18. };
  19. #define s struct xt_tproxy_target_info
  20. static const struct xt_option_entry tproxy_tg0_opts[] = {
  21. {.name = "on-port", .id = P_PORT, .type = XTTYPE_PORT,
  22. .flags = XTOPT_MAND | XTOPT_NBO | XTOPT_PUT, XTOPT_POINTER(s, lport)},
  23. {.name = "on-ip", .id = P_ADDR, .type = XTTYPE_HOST},
  24. {.name = "tproxy-mark", .id = P_MARK, .type = XTTYPE_MARKMASK32},
  25. XTOPT_TABLEEND,
  26. };
  27. #undef s
  28. #define s struct xt_tproxy_target_info_v1
  29. static const struct xt_option_entry tproxy_tg1_opts[] = {
  30. {.name = "on-port", .id = P_PORT, .type = XTTYPE_PORT,
  31. .flags = XTOPT_MAND | XTOPT_NBO | XTOPT_PUT, XTOPT_POINTER(s, lport)},
  32. {.name = "on-ip", .id = P_ADDR, .type = XTTYPE_HOST,
  33. .flags = XTOPT_PUT, XTOPT_POINTER(s, laddr)},
  34. {.name = "tproxy-mark", .id = P_MARK, .type = XTTYPE_MARKMASK32},
  35. XTOPT_TABLEEND,
  36. };
  37. #undef s
  38. static void tproxy_tg_help(void)
  39. {
  40. printf(
  41. "TPROXY target options:\n"
  42. " --on-port port Redirect connection to port, or the original port if 0\n"
  43. " --on-ip ip Optionally redirect to the given IP\n"
  44. " --tproxy-mark value[/mask] Mark packets with the given value/mask\n\n");
  45. }
  46. static void tproxy_tg_print(const void *ip, const struct xt_entry_target *target,
  47. int numeric)
  48. {
  49. const struct xt_tproxy_target_info *info = (const void *)target->data;
  50. printf(" TPROXY redirect %s:%u mark 0x%x/0x%x",
  51. xtables_ipaddr_to_numeric((const struct in_addr *)&info->laddr),
  52. ntohs(info->lport), (unsigned int)info->mark_value,
  53. (unsigned int)info->mark_mask);
  54. }
  55. static void
  56. tproxy_tg_print4(const void *ip, const struct xt_entry_target *target,
  57. int numeric)
  58. {
  59. const struct xt_tproxy_target_info_v1 *info =
  60. (const void *)target->data;
  61. printf(" TPROXY redirect %s:%u mark 0x%x/0x%x",
  62. xtables_ipaddr_to_numeric(&info->laddr.in),
  63. ntohs(info->lport), (unsigned int)info->mark_value,
  64. (unsigned int)info->mark_mask);
  65. }
  66. static void
  67. tproxy_tg_print6(const void *ip, const struct xt_entry_target *target,
  68. int numeric)
  69. {
  70. const struct xt_tproxy_target_info_v1 *info =
  71. (const void *)target->data;
  72. printf(" TPROXY redirect %s:%u mark 0x%x/0x%x",
  73. xtables_ip6addr_to_numeric(&info->laddr.in6),
  74. ntohs(info->lport), (unsigned int)info->mark_value,
  75. (unsigned int)info->mark_mask);
  76. }
  77. static void tproxy_tg_save(const void *ip, const struct xt_entry_target *target)
  78. {
  79. const struct xt_tproxy_target_info *info = (const void *)target->data;
  80. printf(" --on-port %u", ntohs(info->lport));
  81. printf(" --on-ip %s",
  82. xtables_ipaddr_to_numeric((const struct in_addr *)&info->laddr));
  83. printf(" --tproxy-mark 0x%x/0x%x",
  84. (unsigned int)info->mark_value, (unsigned int)info->mark_mask);
  85. }
  86. static void
  87. tproxy_tg_save4(const void *ip, const struct xt_entry_target *target)
  88. {
  89. const struct xt_tproxy_target_info_v1 *info;
  90. info = (const void *)target->data;
  91. printf(" --on-port %u", ntohs(info->lport));
  92. printf(" --on-ip %s", xtables_ipaddr_to_numeric(&info->laddr.in));
  93. printf(" --tproxy-mark 0x%x/0x%x",
  94. (unsigned int)info->mark_value, (unsigned int)info->mark_mask);
  95. }
  96. static void
  97. tproxy_tg_save6(const void *ip, const struct xt_entry_target *target)
  98. {
  99. const struct xt_tproxy_target_info_v1 *info;
  100. info = (const void *)target->data;
  101. printf(" --on-port %u", ntohs(info->lport));
  102. printf(" --on-ip %s", xtables_ip6addr_to_numeric(&info->laddr.in6));
  103. printf(" --tproxy-mark 0x%x/0x%x",
  104. (unsigned int)info->mark_value, (unsigned int)info->mark_mask);
  105. }
  106. static void tproxy_tg0_parse(struct xt_option_call *cb)
  107. {
  108. struct xt_tproxy_target_info *info = cb->data;
  109. xtables_option_parse(cb);
  110. switch (cb->entry->id) {
  111. case P_MARK:
  112. info->mark_value = cb->val.mark;
  113. info->mark_mask = cb->val.mask;
  114. break;
  115. case P_ADDR:
  116. info->laddr = cb->val.haddr.ip;
  117. break;
  118. }
  119. }
  120. static void tproxy_tg1_parse(struct xt_option_call *cb)
  121. {
  122. struct xt_tproxy_target_info_v1 *info = cb->data;
  123. xtables_option_parse(cb);
  124. switch (cb->entry->id) {
  125. case P_MARK:
  126. info->mark_value = cb->val.mark;
  127. info->mark_mask = cb->val.mask;
  128. break;
  129. }
  130. }
  131. static struct xtables_target tproxy_tg_reg[] = {
  132. {
  133. .name = "TPROXY",
  134. .revision = 0,
  135. .family = NFPROTO_IPV4,
  136. .version = XTABLES_VERSION,
  137. .size = XT_ALIGN(sizeof(struct xt_tproxy_target_info)),
  138. .userspacesize = XT_ALIGN(sizeof(struct xt_tproxy_target_info)),
  139. .help = tproxy_tg_help,
  140. .print = tproxy_tg_print,
  141. .save = tproxy_tg_save,
  142. .x6_options = tproxy_tg0_opts,
  143. .x6_parse = tproxy_tg0_parse,
  144. },
  145. {
  146. .name = "TPROXY",
  147. .revision = 1,
  148. .family = NFPROTO_IPV4,
  149. .version = XTABLES_VERSION,
  150. .size = XT_ALIGN(sizeof(struct xt_tproxy_target_info_v1)),
  151. .userspacesize = XT_ALIGN(sizeof(struct xt_tproxy_target_info_v1)),
  152. .help = tproxy_tg_help,
  153. .print = tproxy_tg_print4,
  154. .save = tproxy_tg_save4,
  155. .x6_options = tproxy_tg1_opts,
  156. .x6_parse = tproxy_tg1_parse,
  157. },
  158. {
  159. .name = "TPROXY",
  160. .revision = 1,
  161. .family = NFPROTO_IPV6,
  162. .version = XTABLES_VERSION,
  163. .size = XT_ALIGN(sizeof(struct xt_tproxy_target_info_v1)),
  164. .userspacesize = XT_ALIGN(sizeof(struct xt_tproxy_target_info_v1)),
  165. .help = tproxy_tg_help,
  166. .print = tproxy_tg_print6,
  167. .save = tproxy_tg_save6,
  168. .x6_options = tproxy_tg1_opts,
  169. .x6_parse = tproxy_tg1_parse,
  170. },
  171. };
  172. void _init(void)
  173. {
  174. xtables_register_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
  175. }