nft_masq.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014 Arturo Borrero Gonzalez <arturo@debian.org>
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/init.h>
  7. #include <linux/module.h>
  8. #include <linux/netlink.h>
  9. #include <linux/netfilter.h>
  10. #include <linux/netfilter/nf_tables.h>
  11. #include <net/netfilter/nf_tables.h>
  12. #include <net/netfilter/nf_nat.h>
  13. #include <net/netfilter/nf_nat_masquerade.h>
  14. struct nft_masq {
  15. u32 flags;
  16. enum nft_registers sreg_proto_min:8;
  17. enum nft_registers sreg_proto_max:8;
  18. };
  19. static const struct nla_policy nft_masq_policy[NFTA_MASQ_MAX + 1] = {
  20. [NFTA_MASQ_FLAGS] = { .type = NLA_U32 },
  21. [NFTA_MASQ_REG_PROTO_MIN] = { .type = NLA_U32 },
  22. [NFTA_MASQ_REG_PROTO_MAX] = { .type = NLA_U32 },
  23. };
  24. static int nft_masq_validate(const struct nft_ctx *ctx,
  25. const struct nft_expr *expr,
  26. const struct nft_data **data)
  27. {
  28. int err;
  29. err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT);
  30. if (err < 0)
  31. return err;
  32. return nft_chain_validate_hooks(ctx->chain,
  33. (1 << NF_INET_POST_ROUTING));
  34. }
  35. static int nft_masq_init(const struct nft_ctx *ctx,
  36. const struct nft_expr *expr,
  37. const struct nlattr * const tb[])
  38. {
  39. u32 plen = sizeof_field(struct nf_nat_range, min_addr.all);
  40. struct nft_masq *priv = nft_expr_priv(expr);
  41. int err;
  42. if (tb[NFTA_MASQ_FLAGS]) {
  43. priv->flags = ntohl(nla_get_be32(tb[NFTA_MASQ_FLAGS]));
  44. if (priv->flags & ~NF_NAT_RANGE_MASK)
  45. return -EINVAL;
  46. }
  47. if (tb[NFTA_MASQ_REG_PROTO_MIN]) {
  48. priv->sreg_proto_min =
  49. nft_parse_register(tb[NFTA_MASQ_REG_PROTO_MIN]);
  50. err = nft_validate_register_load(priv->sreg_proto_min, plen);
  51. if (err < 0)
  52. return err;
  53. if (tb[NFTA_MASQ_REG_PROTO_MAX]) {
  54. priv->sreg_proto_max =
  55. nft_parse_register(tb[NFTA_MASQ_REG_PROTO_MAX]);
  56. err = nft_validate_register_load(priv->sreg_proto_max,
  57. plen);
  58. if (err < 0)
  59. return err;
  60. } else {
  61. priv->sreg_proto_max = priv->sreg_proto_min;
  62. }
  63. }
  64. return nf_ct_netns_get(ctx->net, ctx->family);
  65. }
  66. static int nft_masq_dump(struct sk_buff *skb, const struct nft_expr *expr)
  67. {
  68. const struct nft_masq *priv = nft_expr_priv(expr);
  69. if (priv->flags != 0 &&
  70. nla_put_be32(skb, NFTA_MASQ_FLAGS, htonl(priv->flags)))
  71. goto nla_put_failure;
  72. if (priv->sreg_proto_min) {
  73. if (nft_dump_register(skb, NFTA_MASQ_REG_PROTO_MIN,
  74. priv->sreg_proto_min) ||
  75. nft_dump_register(skb, NFTA_MASQ_REG_PROTO_MAX,
  76. priv->sreg_proto_max))
  77. goto nla_put_failure;
  78. }
  79. return 0;
  80. nla_put_failure:
  81. return -1;
  82. }
  83. static void nft_masq_ipv4_eval(const struct nft_expr *expr,
  84. struct nft_regs *regs,
  85. const struct nft_pktinfo *pkt)
  86. {
  87. struct nft_masq *priv = nft_expr_priv(expr);
  88. struct nf_nat_range2 range;
  89. memset(&range, 0, sizeof(range));
  90. range.flags = priv->flags;
  91. if (priv->sreg_proto_min) {
  92. range.min_proto.all = (__force __be16)nft_reg_load16(
  93. &regs->data[priv->sreg_proto_min]);
  94. range.max_proto.all = (__force __be16)nft_reg_load16(
  95. &regs->data[priv->sreg_proto_max]);
  96. }
  97. regs->verdict.code = nf_nat_masquerade_ipv4(pkt->skb, nft_hook(pkt),
  98. &range, nft_out(pkt));
  99. }
  100. static void
  101. nft_masq_ipv4_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
  102. {
  103. nf_ct_netns_put(ctx->net, NFPROTO_IPV4);
  104. }
  105. static struct nft_expr_type nft_masq_ipv4_type;
  106. static const struct nft_expr_ops nft_masq_ipv4_ops = {
  107. .type = &nft_masq_ipv4_type,
  108. .size = NFT_EXPR_SIZE(sizeof(struct nft_masq)),
  109. .eval = nft_masq_ipv4_eval,
  110. .init = nft_masq_init,
  111. .destroy = nft_masq_ipv4_destroy,
  112. .dump = nft_masq_dump,
  113. .validate = nft_masq_validate,
  114. };
  115. static struct nft_expr_type nft_masq_ipv4_type __read_mostly = {
  116. .family = NFPROTO_IPV4,
  117. .name = "masq",
  118. .ops = &nft_masq_ipv4_ops,
  119. .policy = nft_masq_policy,
  120. .maxattr = NFTA_MASQ_MAX,
  121. .owner = THIS_MODULE,
  122. };
  123. #ifdef CONFIG_NF_TABLES_IPV6
  124. static void nft_masq_ipv6_eval(const struct nft_expr *expr,
  125. struct nft_regs *regs,
  126. const struct nft_pktinfo *pkt)
  127. {
  128. struct nft_masq *priv = nft_expr_priv(expr);
  129. struct nf_nat_range2 range;
  130. memset(&range, 0, sizeof(range));
  131. range.flags = priv->flags;
  132. if (priv->sreg_proto_min) {
  133. range.min_proto.all = (__force __be16)nft_reg_load16(
  134. &regs->data[priv->sreg_proto_min]);
  135. range.max_proto.all = (__force __be16)nft_reg_load16(
  136. &regs->data[priv->sreg_proto_max]);
  137. }
  138. regs->verdict.code = nf_nat_masquerade_ipv6(pkt->skb, &range,
  139. nft_out(pkt));
  140. }
  141. static void
  142. nft_masq_ipv6_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
  143. {
  144. nf_ct_netns_put(ctx->net, NFPROTO_IPV6);
  145. }
  146. static struct nft_expr_type nft_masq_ipv6_type;
  147. static const struct nft_expr_ops nft_masq_ipv6_ops = {
  148. .type = &nft_masq_ipv6_type,
  149. .size = NFT_EXPR_SIZE(sizeof(struct nft_masq)),
  150. .eval = nft_masq_ipv6_eval,
  151. .init = nft_masq_init,
  152. .destroy = nft_masq_ipv6_destroy,
  153. .dump = nft_masq_dump,
  154. .validate = nft_masq_validate,
  155. };
  156. static struct nft_expr_type nft_masq_ipv6_type __read_mostly = {
  157. .family = NFPROTO_IPV6,
  158. .name = "masq",
  159. .ops = &nft_masq_ipv6_ops,
  160. .policy = nft_masq_policy,
  161. .maxattr = NFTA_MASQ_MAX,
  162. .owner = THIS_MODULE,
  163. };
  164. static int __init nft_masq_module_init_ipv6(void)
  165. {
  166. return nft_register_expr(&nft_masq_ipv6_type);
  167. }
  168. static void nft_masq_module_exit_ipv6(void)
  169. {
  170. nft_unregister_expr(&nft_masq_ipv6_type);
  171. }
  172. #else
  173. static inline int nft_masq_module_init_ipv6(void) { return 0; }
  174. static inline void nft_masq_module_exit_ipv6(void) {}
  175. #endif
  176. #ifdef CONFIG_NF_TABLES_INET
  177. static void nft_masq_inet_eval(const struct nft_expr *expr,
  178. struct nft_regs *regs,
  179. const struct nft_pktinfo *pkt)
  180. {
  181. switch (nft_pf(pkt)) {
  182. case NFPROTO_IPV4:
  183. return nft_masq_ipv4_eval(expr, regs, pkt);
  184. case NFPROTO_IPV6:
  185. return nft_masq_ipv6_eval(expr, regs, pkt);
  186. }
  187. WARN_ON_ONCE(1);
  188. }
  189. static void
  190. nft_masq_inet_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
  191. {
  192. nf_ct_netns_put(ctx->net, NFPROTO_INET);
  193. }
  194. static struct nft_expr_type nft_masq_inet_type;
  195. static const struct nft_expr_ops nft_masq_inet_ops = {
  196. .type = &nft_masq_inet_type,
  197. .size = NFT_EXPR_SIZE(sizeof(struct nft_masq)),
  198. .eval = nft_masq_inet_eval,
  199. .init = nft_masq_init,
  200. .destroy = nft_masq_inet_destroy,
  201. .dump = nft_masq_dump,
  202. .validate = nft_masq_validate,
  203. };
  204. static struct nft_expr_type nft_masq_inet_type __read_mostly = {
  205. .family = NFPROTO_INET,
  206. .name = "masq",
  207. .ops = &nft_masq_inet_ops,
  208. .policy = nft_masq_policy,
  209. .maxattr = NFTA_MASQ_MAX,
  210. .owner = THIS_MODULE,
  211. };
  212. static int __init nft_masq_module_init_inet(void)
  213. {
  214. return nft_register_expr(&nft_masq_inet_type);
  215. }
  216. static void nft_masq_module_exit_inet(void)
  217. {
  218. nft_unregister_expr(&nft_masq_inet_type);
  219. }
  220. #else
  221. static inline int nft_masq_module_init_inet(void) { return 0; }
  222. static inline void nft_masq_module_exit_inet(void) {}
  223. #endif
  224. static int __init nft_masq_module_init(void)
  225. {
  226. int ret;
  227. ret = nft_masq_module_init_ipv6();
  228. if (ret < 0)
  229. return ret;
  230. ret = nft_masq_module_init_inet();
  231. if (ret < 0) {
  232. nft_masq_module_exit_ipv6();
  233. return ret;
  234. }
  235. ret = nft_register_expr(&nft_masq_ipv4_type);
  236. if (ret < 0) {
  237. nft_masq_module_exit_inet();
  238. nft_masq_module_exit_ipv6();
  239. return ret;
  240. }
  241. ret = nf_nat_masquerade_inet_register_notifiers();
  242. if (ret < 0) {
  243. nft_masq_module_exit_ipv6();
  244. nft_masq_module_exit_inet();
  245. nft_unregister_expr(&nft_masq_ipv4_type);
  246. return ret;
  247. }
  248. return ret;
  249. }
  250. static void __exit nft_masq_module_exit(void)
  251. {
  252. nft_masq_module_exit_ipv6();
  253. nft_masq_module_exit_inet();
  254. nft_unregister_expr(&nft_masq_ipv4_type);
  255. nf_nat_masquerade_inet_unregister_notifiers();
  256. }
  257. module_init(nft_masq_module_init);
  258. module_exit(nft_masq_module_exit);
  259. MODULE_LICENSE("GPL");
  260. MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo@debian.org>");
  261. MODULE_ALIAS_NFT_EXPR("masq");
  262. MODULE_DESCRIPTION("Netfilter nftables masquerade expression support");