nft_nat.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
  4. * Copyright (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>
  5. * Copyright (c) 2012 Intel Corporation
  6. */
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/ip.h>
  11. #include <linux/string.h>
  12. #include <linux/netlink.h>
  13. #include <linux/netfilter.h>
  14. #include <linux/netfilter_ipv4.h>
  15. #include <linux/netfilter/nfnetlink.h>
  16. #include <linux/netfilter/nf_tables.h>
  17. #include <net/netfilter/nf_conntrack.h>
  18. #include <net/netfilter/nf_nat.h>
  19. #include <net/netfilter/nf_tables.h>
  20. #include <net/ip.h>
  21. struct nft_nat {
  22. enum nft_registers sreg_addr_min:8;
  23. enum nft_registers sreg_addr_max:8;
  24. enum nft_registers sreg_proto_min:8;
  25. enum nft_registers sreg_proto_max:8;
  26. enum nf_nat_manip_type type:8;
  27. u8 family;
  28. u16 flags;
  29. };
  30. static void nft_nat_setup_addr(struct nf_nat_range2 *range,
  31. const struct nft_regs *regs,
  32. const struct nft_nat *priv)
  33. {
  34. switch (priv->family) {
  35. case AF_INET:
  36. range->min_addr.ip = (__force __be32)
  37. regs->data[priv->sreg_addr_min];
  38. range->max_addr.ip = (__force __be32)
  39. regs->data[priv->sreg_addr_max];
  40. break;
  41. case AF_INET6:
  42. memcpy(range->min_addr.ip6, &regs->data[priv->sreg_addr_min],
  43. sizeof(range->min_addr.ip6));
  44. memcpy(range->max_addr.ip6, &regs->data[priv->sreg_addr_max],
  45. sizeof(range->max_addr.ip6));
  46. break;
  47. }
  48. }
  49. static void nft_nat_setup_proto(struct nf_nat_range2 *range,
  50. const struct nft_regs *regs,
  51. const struct nft_nat *priv)
  52. {
  53. range->min_proto.all = (__force __be16)
  54. nft_reg_load16(&regs->data[priv->sreg_proto_min]);
  55. range->max_proto.all = (__force __be16)
  56. nft_reg_load16(&regs->data[priv->sreg_proto_max]);
  57. }
  58. static void nft_nat_setup_netmap(struct nf_nat_range2 *range,
  59. const struct nft_pktinfo *pkt,
  60. const struct nft_nat *priv)
  61. {
  62. struct sk_buff *skb = pkt->skb;
  63. union nf_inet_addr new_addr;
  64. __be32 netmask;
  65. int i, len = 0;
  66. switch (priv->type) {
  67. case NFT_NAT_SNAT:
  68. if (nft_pf(pkt) == NFPROTO_IPV4) {
  69. new_addr.ip = ip_hdr(skb)->saddr;
  70. len = sizeof(struct in_addr);
  71. } else {
  72. new_addr.in6 = ipv6_hdr(skb)->saddr;
  73. len = sizeof(struct in6_addr);
  74. }
  75. break;
  76. case NFT_NAT_DNAT:
  77. if (nft_pf(pkt) == NFPROTO_IPV4) {
  78. new_addr.ip = ip_hdr(skb)->daddr;
  79. len = sizeof(struct in_addr);
  80. } else {
  81. new_addr.in6 = ipv6_hdr(skb)->daddr;
  82. len = sizeof(struct in6_addr);
  83. }
  84. break;
  85. }
  86. for (i = 0; i < len / sizeof(__be32); i++) {
  87. netmask = ~(range->min_addr.ip6[i] ^ range->max_addr.ip6[i]);
  88. new_addr.ip6[i] &= ~netmask;
  89. new_addr.ip6[i] |= range->min_addr.ip6[i] & netmask;
  90. }
  91. range->min_addr = new_addr;
  92. range->max_addr = new_addr;
  93. }
  94. static void nft_nat_eval(const struct nft_expr *expr,
  95. struct nft_regs *regs,
  96. const struct nft_pktinfo *pkt)
  97. {
  98. const struct nft_nat *priv = nft_expr_priv(expr);
  99. enum ip_conntrack_info ctinfo;
  100. struct nf_conn *ct = nf_ct_get(pkt->skb, &ctinfo);
  101. struct nf_nat_range2 range;
  102. memset(&range, 0, sizeof(range));
  103. if (priv->sreg_addr_min) {
  104. nft_nat_setup_addr(&range, regs, priv);
  105. if (priv->flags & NF_NAT_RANGE_NETMAP)
  106. nft_nat_setup_netmap(&range, pkt, priv);
  107. }
  108. if (priv->sreg_proto_min)
  109. nft_nat_setup_proto(&range, regs, priv);
  110. range.flags = priv->flags;
  111. regs->verdict.code = nf_nat_setup_info(ct, &range, priv->type);
  112. }
  113. static const struct nla_policy nft_nat_policy[NFTA_NAT_MAX + 1] = {
  114. [NFTA_NAT_TYPE] = { .type = NLA_U32 },
  115. [NFTA_NAT_FAMILY] = { .type = NLA_U32 },
  116. [NFTA_NAT_REG_ADDR_MIN] = { .type = NLA_U32 },
  117. [NFTA_NAT_REG_ADDR_MAX] = { .type = NLA_U32 },
  118. [NFTA_NAT_REG_PROTO_MIN] = { .type = NLA_U32 },
  119. [NFTA_NAT_REG_PROTO_MAX] = { .type = NLA_U32 },
  120. [NFTA_NAT_FLAGS] = { .type = NLA_U32 },
  121. };
  122. static int nft_nat_validate(const struct nft_ctx *ctx,
  123. const struct nft_expr *expr,
  124. const struct nft_data **data)
  125. {
  126. struct nft_nat *priv = nft_expr_priv(expr);
  127. int err;
  128. err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT);
  129. if (err < 0)
  130. return err;
  131. switch (priv->type) {
  132. case NFT_NAT_SNAT:
  133. err = nft_chain_validate_hooks(ctx->chain,
  134. (1 << NF_INET_POST_ROUTING) |
  135. (1 << NF_INET_LOCAL_IN));
  136. break;
  137. case NFT_NAT_DNAT:
  138. err = nft_chain_validate_hooks(ctx->chain,
  139. (1 << NF_INET_PRE_ROUTING) |
  140. (1 << NF_INET_LOCAL_OUT));
  141. break;
  142. }
  143. return err;
  144. }
  145. static int nft_nat_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
  146. const struct nlattr * const tb[])
  147. {
  148. struct nft_nat *priv = nft_expr_priv(expr);
  149. unsigned int alen, plen;
  150. u32 family;
  151. int err;
  152. if (tb[NFTA_NAT_TYPE] == NULL ||
  153. (tb[NFTA_NAT_REG_ADDR_MIN] == NULL &&
  154. tb[NFTA_NAT_REG_PROTO_MIN] == NULL))
  155. return -EINVAL;
  156. switch (ntohl(nla_get_be32(tb[NFTA_NAT_TYPE]))) {
  157. case NFT_NAT_SNAT:
  158. priv->type = NF_NAT_MANIP_SRC;
  159. break;
  160. case NFT_NAT_DNAT:
  161. priv->type = NF_NAT_MANIP_DST;
  162. break;
  163. default:
  164. return -EOPNOTSUPP;
  165. }
  166. if (tb[NFTA_NAT_FAMILY] == NULL)
  167. return -EINVAL;
  168. family = ntohl(nla_get_be32(tb[NFTA_NAT_FAMILY]));
  169. if (ctx->family != NFPROTO_INET && ctx->family != family)
  170. return -EOPNOTSUPP;
  171. switch (family) {
  172. case NFPROTO_IPV4:
  173. alen = sizeof_field(struct nf_nat_range, min_addr.ip);
  174. break;
  175. case NFPROTO_IPV6:
  176. alen = sizeof_field(struct nf_nat_range, min_addr.ip6);
  177. break;
  178. default:
  179. if (tb[NFTA_NAT_REG_ADDR_MIN])
  180. return -EAFNOSUPPORT;
  181. break;
  182. }
  183. priv->family = family;
  184. if (tb[NFTA_NAT_REG_ADDR_MIN]) {
  185. priv->sreg_addr_min =
  186. nft_parse_register(tb[NFTA_NAT_REG_ADDR_MIN]);
  187. err = nft_validate_register_load(priv->sreg_addr_min, alen);
  188. if (err < 0)
  189. return err;
  190. if (tb[NFTA_NAT_REG_ADDR_MAX]) {
  191. priv->sreg_addr_max =
  192. nft_parse_register(tb[NFTA_NAT_REG_ADDR_MAX]);
  193. err = nft_validate_register_load(priv->sreg_addr_max,
  194. alen);
  195. if (err < 0)
  196. return err;
  197. } else {
  198. priv->sreg_addr_max = priv->sreg_addr_min;
  199. }
  200. priv->flags |= NF_NAT_RANGE_MAP_IPS;
  201. }
  202. plen = sizeof_field(struct nf_nat_range, min_addr.all);
  203. if (tb[NFTA_NAT_REG_PROTO_MIN]) {
  204. priv->sreg_proto_min =
  205. nft_parse_register(tb[NFTA_NAT_REG_PROTO_MIN]);
  206. err = nft_validate_register_load(priv->sreg_proto_min, plen);
  207. if (err < 0)
  208. return err;
  209. if (tb[NFTA_NAT_REG_PROTO_MAX]) {
  210. priv->sreg_proto_max =
  211. nft_parse_register(tb[NFTA_NAT_REG_PROTO_MAX]);
  212. err = nft_validate_register_load(priv->sreg_proto_max,
  213. plen);
  214. if (err < 0)
  215. return err;
  216. } else {
  217. priv->sreg_proto_max = priv->sreg_proto_min;
  218. }
  219. priv->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  220. }
  221. if (tb[NFTA_NAT_FLAGS]) {
  222. priv->flags |= ntohl(nla_get_be32(tb[NFTA_NAT_FLAGS]));
  223. if (priv->flags & ~NF_NAT_RANGE_MASK)
  224. return -EOPNOTSUPP;
  225. }
  226. return nf_ct_netns_get(ctx->net, family);
  227. }
  228. static int nft_nat_dump(struct sk_buff *skb, const struct nft_expr *expr)
  229. {
  230. const struct nft_nat *priv = nft_expr_priv(expr);
  231. switch (priv->type) {
  232. case NF_NAT_MANIP_SRC:
  233. if (nla_put_be32(skb, NFTA_NAT_TYPE, htonl(NFT_NAT_SNAT)))
  234. goto nla_put_failure;
  235. break;
  236. case NF_NAT_MANIP_DST:
  237. if (nla_put_be32(skb, NFTA_NAT_TYPE, htonl(NFT_NAT_DNAT)))
  238. goto nla_put_failure;
  239. break;
  240. }
  241. if (nla_put_be32(skb, NFTA_NAT_FAMILY, htonl(priv->family)))
  242. goto nla_put_failure;
  243. if (priv->sreg_addr_min) {
  244. if (nft_dump_register(skb, NFTA_NAT_REG_ADDR_MIN,
  245. priv->sreg_addr_min) ||
  246. nft_dump_register(skb, NFTA_NAT_REG_ADDR_MAX,
  247. priv->sreg_addr_max))
  248. goto nla_put_failure;
  249. }
  250. if (priv->sreg_proto_min) {
  251. if (nft_dump_register(skb, NFTA_NAT_REG_PROTO_MIN,
  252. priv->sreg_proto_min) ||
  253. nft_dump_register(skb, NFTA_NAT_REG_PROTO_MAX,
  254. priv->sreg_proto_max))
  255. goto nla_put_failure;
  256. }
  257. if (priv->flags != 0) {
  258. if (nla_put_be32(skb, NFTA_NAT_FLAGS, htonl(priv->flags)))
  259. goto nla_put_failure;
  260. }
  261. return 0;
  262. nla_put_failure:
  263. return -1;
  264. }
  265. static void
  266. nft_nat_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
  267. {
  268. const struct nft_nat *priv = nft_expr_priv(expr);
  269. nf_ct_netns_put(ctx->net, priv->family);
  270. }
  271. static struct nft_expr_type nft_nat_type;
  272. static const struct nft_expr_ops nft_nat_ops = {
  273. .type = &nft_nat_type,
  274. .size = NFT_EXPR_SIZE(sizeof(struct nft_nat)),
  275. .eval = nft_nat_eval,
  276. .init = nft_nat_init,
  277. .destroy = nft_nat_destroy,
  278. .dump = nft_nat_dump,
  279. .validate = nft_nat_validate,
  280. };
  281. static struct nft_expr_type nft_nat_type __read_mostly = {
  282. .name = "nat",
  283. .ops = &nft_nat_ops,
  284. .policy = nft_nat_policy,
  285. .maxattr = NFTA_NAT_MAX,
  286. .owner = THIS_MODULE,
  287. };
  288. #ifdef CONFIG_NF_TABLES_INET
  289. static void nft_nat_inet_eval(const struct nft_expr *expr,
  290. struct nft_regs *regs,
  291. const struct nft_pktinfo *pkt)
  292. {
  293. const struct nft_nat *priv = nft_expr_priv(expr);
  294. if (priv->family == nft_pf(pkt))
  295. nft_nat_eval(expr, regs, pkt);
  296. }
  297. static const struct nft_expr_ops nft_nat_inet_ops = {
  298. .type = &nft_nat_type,
  299. .size = NFT_EXPR_SIZE(sizeof(struct nft_nat)),
  300. .eval = nft_nat_inet_eval,
  301. .init = nft_nat_init,
  302. .destroy = nft_nat_destroy,
  303. .dump = nft_nat_dump,
  304. .validate = nft_nat_validate,
  305. };
  306. static struct nft_expr_type nft_inet_nat_type __read_mostly = {
  307. .name = "nat",
  308. .family = NFPROTO_INET,
  309. .ops = &nft_nat_inet_ops,
  310. .policy = nft_nat_policy,
  311. .maxattr = NFTA_NAT_MAX,
  312. .owner = THIS_MODULE,
  313. };
  314. static int nft_nat_inet_module_init(void)
  315. {
  316. return nft_register_expr(&nft_inet_nat_type);
  317. }
  318. static void nft_nat_inet_module_exit(void)
  319. {
  320. nft_unregister_expr(&nft_inet_nat_type);
  321. }
  322. #else
  323. static int nft_nat_inet_module_init(void) { return 0; }
  324. static void nft_nat_inet_module_exit(void) { }
  325. #endif
  326. static int __init nft_nat_module_init(void)
  327. {
  328. int ret = nft_nat_inet_module_init();
  329. if (ret)
  330. return ret;
  331. ret = nft_register_expr(&nft_nat_type);
  332. if (ret)
  333. nft_nat_inet_module_exit();
  334. return ret;
  335. }
  336. static void __exit nft_nat_module_exit(void)
  337. {
  338. nft_nat_inet_module_exit();
  339. nft_unregister_expr(&nft_nat_type);
  340. }
  341. module_init(nft_nat_module_init);
  342. module_exit(nft_nat_module_exit);
  343. MODULE_LICENSE("GPL");
  344. MODULE_AUTHOR("Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>");
  345. MODULE_ALIAS_NFT_EXPR("nat");
  346. MODULE_DESCRIPTION("Network Address Translation support");