nft_tproxy.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <linux/module.h>
  3. #include <linux/netfilter/nf_tables.h>
  4. #include <net/netfilter/nf_tables.h>
  5. #include <net/netfilter/nf_tables_core.h>
  6. #include <net/netfilter/nf_tproxy.h>
  7. #include <net/inet_sock.h>
  8. #include <net/tcp.h>
  9. #include <linux/if_ether.h>
  10. #include <net/netfilter/ipv4/nf_defrag_ipv4.h>
  11. #if IS_ENABLED(CONFIG_NF_TABLES_IPV6)
  12. #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
  13. #endif
  14. struct nft_tproxy {
  15. enum nft_registers sreg_addr:8;
  16. enum nft_registers sreg_port:8;
  17. u8 family;
  18. };
  19. static void nft_tproxy_eval_v4(const struct nft_expr *expr,
  20. struct nft_regs *regs,
  21. const struct nft_pktinfo *pkt)
  22. {
  23. const struct nft_tproxy *priv = nft_expr_priv(expr);
  24. struct sk_buff *skb = pkt->skb;
  25. const struct iphdr *iph = ip_hdr(skb);
  26. struct udphdr _hdr, *hp;
  27. __be32 taddr = 0;
  28. __be16 tport = 0;
  29. struct sock *sk;
  30. hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr);
  31. if (!hp) {
  32. regs->verdict.code = NFT_BREAK;
  33. return;
  34. }
  35. /* check if there's an ongoing connection on the packet addresses, this
  36. * happens if the redirect already happened and the current packet
  37. * belongs to an already established connection
  38. */
  39. sk = nf_tproxy_get_sock_v4(nft_net(pkt), skb, iph->protocol,
  40. iph->saddr, iph->daddr,
  41. hp->source, hp->dest,
  42. skb->dev, NF_TPROXY_LOOKUP_ESTABLISHED);
  43. if (priv->sreg_addr)
  44. taddr = regs->data[priv->sreg_addr];
  45. taddr = nf_tproxy_laddr4(skb, taddr, iph->daddr);
  46. if (priv->sreg_port)
  47. tport = nft_reg_load16(&regs->data[priv->sreg_port]);
  48. if (!tport)
  49. tport = hp->dest;
  50. /* UDP has no TCP_TIME_WAIT state, so we never enter here */
  51. if (sk && sk->sk_state == TCP_TIME_WAIT) {
  52. /* reopening a TIME_WAIT connection needs special handling */
  53. sk = nf_tproxy_handle_time_wait4(nft_net(pkt), skb, taddr, tport, sk);
  54. } else if (!sk) {
  55. /* no, there's no established connection, check if
  56. * there's a listener on the redirected addr/port
  57. */
  58. sk = nf_tproxy_get_sock_v4(nft_net(pkt), skb, iph->protocol,
  59. iph->saddr, taddr,
  60. hp->source, tport,
  61. skb->dev, NF_TPROXY_LOOKUP_LISTENER);
  62. }
  63. if (sk && nf_tproxy_sk_is_transparent(sk))
  64. nf_tproxy_assign_sock(skb, sk);
  65. else
  66. regs->verdict.code = NFT_BREAK;
  67. }
  68. #if IS_ENABLED(CONFIG_NF_TABLES_IPV6)
  69. static void nft_tproxy_eval_v6(const struct nft_expr *expr,
  70. struct nft_regs *regs,
  71. const struct nft_pktinfo *pkt)
  72. {
  73. const struct nft_tproxy *priv = nft_expr_priv(expr);
  74. struct sk_buff *skb = pkt->skb;
  75. const struct ipv6hdr *iph = ipv6_hdr(skb);
  76. struct in6_addr taddr;
  77. int thoff = pkt->xt.thoff;
  78. struct udphdr _hdr, *hp;
  79. __be16 tport = 0;
  80. struct sock *sk;
  81. int l4proto;
  82. memset(&taddr, 0, sizeof(taddr));
  83. if (!pkt->tprot_set) {
  84. regs->verdict.code = NFT_BREAK;
  85. return;
  86. }
  87. l4proto = pkt->tprot;
  88. hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
  89. if (hp == NULL) {
  90. regs->verdict.code = NFT_BREAK;
  91. return;
  92. }
  93. /* check if there's an ongoing connection on the packet addresses, this
  94. * happens if the redirect already happened and the current packet
  95. * belongs to an already established connection
  96. */
  97. sk = nf_tproxy_get_sock_v6(nft_net(pkt), skb, thoff, l4proto,
  98. &iph->saddr, &iph->daddr,
  99. hp->source, hp->dest,
  100. nft_in(pkt), NF_TPROXY_LOOKUP_ESTABLISHED);
  101. if (priv->sreg_addr)
  102. memcpy(&taddr, &regs->data[priv->sreg_addr], sizeof(taddr));
  103. taddr = *nf_tproxy_laddr6(skb, &taddr, &iph->daddr);
  104. if (priv->sreg_port)
  105. tport = nft_reg_load16(&regs->data[priv->sreg_port]);
  106. if (!tport)
  107. tport = hp->dest;
  108. /* UDP has no TCP_TIME_WAIT state, so we never enter here */
  109. if (sk && sk->sk_state == TCP_TIME_WAIT) {
  110. /* reopening a TIME_WAIT connection needs special handling */
  111. sk = nf_tproxy_handle_time_wait6(skb, l4proto, thoff,
  112. nft_net(pkt),
  113. &taddr,
  114. tport,
  115. sk);
  116. } else if (!sk) {
  117. /* no there's no established connection, check if
  118. * there's a listener on the redirected addr/port
  119. */
  120. sk = nf_tproxy_get_sock_v6(nft_net(pkt), skb, thoff,
  121. l4proto, &iph->saddr, &taddr,
  122. hp->source, tport,
  123. nft_in(pkt), NF_TPROXY_LOOKUP_LISTENER);
  124. }
  125. /* NOTE: assign_sock consumes our sk reference */
  126. if (sk && nf_tproxy_sk_is_transparent(sk))
  127. nf_tproxy_assign_sock(skb, sk);
  128. else
  129. regs->verdict.code = NFT_BREAK;
  130. }
  131. #endif
  132. static void nft_tproxy_eval(const struct nft_expr *expr,
  133. struct nft_regs *regs,
  134. const struct nft_pktinfo *pkt)
  135. {
  136. const struct nft_tproxy *priv = nft_expr_priv(expr);
  137. switch (nft_pf(pkt)) {
  138. case NFPROTO_IPV4:
  139. switch (priv->family) {
  140. case NFPROTO_IPV4:
  141. case NFPROTO_UNSPEC:
  142. nft_tproxy_eval_v4(expr, regs, pkt);
  143. return;
  144. }
  145. break;
  146. #if IS_ENABLED(CONFIG_NF_TABLES_IPV6)
  147. case NFPROTO_IPV6:
  148. switch (priv->family) {
  149. case NFPROTO_IPV6:
  150. case NFPROTO_UNSPEC:
  151. nft_tproxy_eval_v6(expr, regs, pkt);
  152. return;
  153. }
  154. #endif
  155. }
  156. regs->verdict.code = NFT_BREAK;
  157. }
  158. static const struct nla_policy nft_tproxy_policy[NFTA_TPROXY_MAX + 1] = {
  159. [NFTA_TPROXY_FAMILY] = { .type = NLA_U32 },
  160. [NFTA_TPROXY_REG_ADDR] = { .type = NLA_U32 },
  161. [NFTA_TPROXY_REG_PORT] = { .type = NLA_U32 },
  162. };
  163. static int nft_tproxy_init(const struct nft_ctx *ctx,
  164. const struct nft_expr *expr,
  165. const struct nlattr * const tb[])
  166. {
  167. struct nft_tproxy *priv = nft_expr_priv(expr);
  168. unsigned int alen = 0;
  169. int err;
  170. if (!tb[NFTA_TPROXY_FAMILY] ||
  171. (!tb[NFTA_TPROXY_REG_ADDR] && !tb[NFTA_TPROXY_REG_PORT]))
  172. return -EINVAL;
  173. priv->family = ntohl(nla_get_be32(tb[NFTA_TPROXY_FAMILY]));
  174. switch (ctx->family) {
  175. case NFPROTO_IPV4:
  176. if (priv->family != NFPROTO_IPV4)
  177. return -EINVAL;
  178. break;
  179. #if IS_ENABLED(CONFIG_NF_TABLES_IPV6)
  180. case NFPROTO_IPV6:
  181. if (priv->family != NFPROTO_IPV6)
  182. return -EINVAL;
  183. break;
  184. #endif
  185. case NFPROTO_INET:
  186. break;
  187. default:
  188. return -EOPNOTSUPP;
  189. }
  190. /* Address is specified but the rule family is not set accordingly */
  191. if (priv->family == NFPROTO_UNSPEC && tb[NFTA_TPROXY_REG_ADDR])
  192. return -EINVAL;
  193. switch (priv->family) {
  194. case NFPROTO_IPV4:
  195. alen = FIELD_SIZEOF(union nf_inet_addr, in);
  196. err = nf_defrag_ipv4_enable(ctx->net);
  197. if (err)
  198. return err;
  199. break;
  200. #if IS_ENABLED(CONFIG_NF_TABLES_IPV6)
  201. case NFPROTO_IPV6:
  202. alen = FIELD_SIZEOF(union nf_inet_addr, in6);
  203. err = nf_defrag_ipv6_enable(ctx->net);
  204. if (err)
  205. return err;
  206. break;
  207. #endif
  208. case NFPROTO_UNSPEC:
  209. /* No address is specified here */
  210. err = nf_defrag_ipv4_enable(ctx->net);
  211. if (err)
  212. return err;
  213. #if IS_ENABLED(CONFIG_NF_TABLES_IPV6)
  214. err = nf_defrag_ipv6_enable(ctx->net);
  215. if (err)
  216. return err;
  217. #endif
  218. break;
  219. default:
  220. return -EOPNOTSUPP;
  221. }
  222. if (tb[NFTA_TPROXY_REG_ADDR]) {
  223. priv->sreg_addr = nft_parse_register(tb[NFTA_TPROXY_REG_ADDR]);
  224. err = nft_validate_register_load(priv->sreg_addr, alen);
  225. if (err < 0)
  226. return err;
  227. }
  228. if (tb[NFTA_TPROXY_REG_PORT]) {
  229. priv->sreg_port = nft_parse_register(tb[NFTA_TPROXY_REG_PORT]);
  230. err = nft_validate_register_load(priv->sreg_port, sizeof(u16));
  231. if (err < 0)
  232. return err;
  233. }
  234. return 0;
  235. }
  236. static int nft_tproxy_dump(struct sk_buff *skb,
  237. const struct nft_expr *expr)
  238. {
  239. const struct nft_tproxy *priv = nft_expr_priv(expr);
  240. if (nla_put_be32(skb, NFTA_TPROXY_FAMILY, htonl(priv->family)))
  241. return -1;
  242. if (priv->sreg_addr &&
  243. nft_dump_register(skb, NFTA_TPROXY_REG_ADDR, priv->sreg_addr))
  244. return -1;
  245. if (priv->sreg_port &&
  246. nft_dump_register(skb, NFTA_TPROXY_REG_PORT, priv->sreg_port))
  247. return -1;
  248. return 0;
  249. }
  250. static struct nft_expr_type nft_tproxy_type;
  251. static const struct nft_expr_ops nft_tproxy_ops = {
  252. .type = &nft_tproxy_type,
  253. .size = NFT_EXPR_SIZE(sizeof(struct nft_tproxy)),
  254. .eval = nft_tproxy_eval,
  255. .init = nft_tproxy_init,
  256. .dump = nft_tproxy_dump,
  257. };
  258. static struct nft_expr_type nft_tproxy_type __read_mostly = {
  259. .name = "tproxy",
  260. .ops = &nft_tproxy_ops,
  261. .policy = nft_tproxy_policy,
  262. .maxattr = NFTA_TPROXY_MAX,
  263. .owner = THIS_MODULE,
  264. };
  265. static int __init nft_tproxy_module_init(void)
  266. {
  267. return nft_register_expr(&nft_tproxy_type);
  268. }
  269. static void __exit nft_tproxy_module_exit(void)
  270. {
  271. nft_unregister_expr(&nft_tproxy_type);
  272. }
  273. module_init(nft_tproxy_module_init);
  274. module_exit(nft_tproxy_module_exit);
  275. MODULE_LICENSE("GPL");
  276. MODULE_AUTHOR("Máté Eckl");
  277. MODULE_DESCRIPTION("nf_tables tproxy support module");
  278. MODULE_ALIAS_NFT_EXPR("tproxy");