nft_nat.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
  3. * Copyright (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>
  4. * Copyright (c) 2012 Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/ip.h>
  15. #include <linux/string.h>
  16. #include <linux/netlink.h>
  17. #include <linux/netfilter.h>
  18. #include <linux/netfilter_ipv4.h>
  19. #include <linux/netfilter/nfnetlink.h>
  20. #include <linux/netfilter/nf_tables.h>
  21. #include <net/netfilter/nf_conntrack.h>
  22. #include <net/netfilter/nf_nat.h>
  23. #include <net/netfilter/nf_nat_core.h>
  24. #include <net/netfilter/nf_tables.h>
  25. #include <net/netfilter/nf_nat_l3proto.h>
  26. #include <net/ip.h>
  27. struct nft_nat {
  28. enum nft_registers sreg_addr_min:8;
  29. enum nft_registers sreg_addr_max:8;
  30. enum nft_registers sreg_proto_min:8;
  31. enum nft_registers sreg_proto_max:8;
  32. enum nf_nat_manip_type type:8;
  33. u8 family;
  34. u16 flags;
  35. };
  36. static void nft_nat_eval(const struct nft_expr *expr,
  37. struct nft_data data[NFT_REG_MAX + 1],
  38. const struct nft_pktinfo *pkt)
  39. {
  40. const struct nft_nat *priv = nft_expr_priv(expr);
  41. enum ip_conntrack_info ctinfo;
  42. struct nf_conn *ct = nf_ct_get(pkt->skb, &ctinfo);
  43. struct nf_nat_range range;
  44. memset(&range, 0, sizeof(range));
  45. if (priv->sreg_addr_min) {
  46. if (priv->family == AF_INET) {
  47. range.min_addr.ip = (__force __be32)
  48. data[priv->sreg_addr_min].data[0];
  49. range.max_addr.ip = (__force __be32)
  50. data[priv->sreg_addr_max].data[0];
  51. } else {
  52. memcpy(range.min_addr.ip6,
  53. data[priv->sreg_addr_min].data,
  54. sizeof(struct nft_data));
  55. memcpy(range.max_addr.ip6,
  56. data[priv->sreg_addr_max].data,
  57. sizeof(struct nft_data));
  58. }
  59. range.flags |= NF_NAT_RANGE_MAP_IPS;
  60. }
  61. if (priv->sreg_proto_min) {
  62. range.min_proto.all = (__force __be16)
  63. data[priv->sreg_proto_min].data[0];
  64. range.max_proto.all = (__force __be16)
  65. data[priv->sreg_proto_max].data[0];
  66. range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  67. }
  68. range.flags |= priv->flags;
  69. data[NFT_REG_VERDICT].verdict =
  70. nf_nat_setup_info(ct, &range, priv->type);
  71. }
  72. static const struct nla_policy nft_nat_policy[NFTA_NAT_MAX + 1] = {
  73. [NFTA_NAT_TYPE] = { .type = NLA_U32 },
  74. [NFTA_NAT_FAMILY] = { .type = NLA_U32 },
  75. [NFTA_NAT_REG_ADDR_MIN] = { .type = NLA_U32 },
  76. [NFTA_NAT_REG_ADDR_MAX] = { .type = NLA_U32 },
  77. [NFTA_NAT_REG_PROTO_MIN] = { .type = NLA_U32 },
  78. [NFTA_NAT_REG_PROTO_MAX] = { .type = NLA_U32 },
  79. [NFTA_NAT_FLAGS] = { .type = NLA_U32 },
  80. };
  81. static int nft_nat_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
  82. const struct nlattr * const tb[])
  83. {
  84. struct nft_nat *priv = nft_expr_priv(expr);
  85. u32 family;
  86. int err;
  87. err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT);
  88. if (err < 0)
  89. return err;
  90. if (tb[NFTA_NAT_TYPE] == NULL ||
  91. (tb[NFTA_NAT_REG_ADDR_MIN] == NULL &&
  92. tb[NFTA_NAT_REG_PROTO_MIN] == NULL))
  93. return -EINVAL;
  94. switch (ntohl(nla_get_be32(tb[NFTA_NAT_TYPE]))) {
  95. case NFT_NAT_SNAT:
  96. priv->type = NF_NAT_MANIP_SRC;
  97. break;
  98. case NFT_NAT_DNAT:
  99. priv->type = NF_NAT_MANIP_DST;
  100. break;
  101. default:
  102. return -EINVAL;
  103. }
  104. if (tb[NFTA_NAT_FAMILY] == NULL)
  105. return -EINVAL;
  106. family = ntohl(nla_get_be32(tb[NFTA_NAT_FAMILY]));
  107. if (family != AF_INET && family != AF_INET6)
  108. return -EAFNOSUPPORT;
  109. if (family != ctx->afi->family)
  110. return -EOPNOTSUPP;
  111. priv->family = family;
  112. if (tb[NFTA_NAT_REG_ADDR_MIN]) {
  113. priv->sreg_addr_min =
  114. ntohl(nla_get_be32(tb[NFTA_NAT_REG_ADDR_MIN]));
  115. err = nft_validate_input_register(priv->sreg_addr_min);
  116. if (err < 0)
  117. return err;
  118. if (tb[NFTA_NAT_REG_ADDR_MAX]) {
  119. priv->sreg_addr_max =
  120. ntohl(nla_get_be32(tb[NFTA_NAT_REG_ADDR_MAX]));
  121. err = nft_validate_input_register(priv->sreg_addr_max);
  122. if (err < 0)
  123. return err;
  124. } else {
  125. priv->sreg_addr_max = priv->sreg_addr_min;
  126. }
  127. }
  128. if (tb[NFTA_NAT_REG_PROTO_MIN]) {
  129. priv->sreg_proto_min =
  130. ntohl(nla_get_be32(tb[NFTA_NAT_REG_PROTO_MIN]));
  131. err = nft_validate_input_register(priv->sreg_proto_min);
  132. if (err < 0)
  133. return err;
  134. if (tb[NFTA_NAT_REG_PROTO_MAX]) {
  135. priv->sreg_proto_max =
  136. ntohl(nla_get_be32(tb[NFTA_NAT_REG_PROTO_MAX]));
  137. err = nft_validate_input_register(priv->sreg_proto_max);
  138. if (err < 0)
  139. return err;
  140. } else {
  141. priv->sreg_proto_max = priv->sreg_proto_min;
  142. }
  143. }
  144. if (tb[NFTA_NAT_FLAGS]) {
  145. priv->flags = ntohl(nla_get_be32(tb[NFTA_NAT_FLAGS]));
  146. if (priv->flags & ~NF_NAT_RANGE_MASK)
  147. return -EINVAL;
  148. }
  149. return 0;
  150. }
  151. static int nft_nat_dump(struct sk_buff *skb, const struct nft_expr *expr)
  152. {
  153. const struct nft_nat *priv = nft_expr_priv(expr);
  154. switch (priv->type) {
  155. case NF_NAT_MANIP_SRC:
  156. if (nla_put_be32(skb, NFTA_NAT_TYPE, htonl(NFT_NAT_SNAT)))
  157. goto nla_put_failure;
  158. break;
  159. case NF_NAT_MANIP_DST:
  160. if (nla_put_be32(skb, NFTA_NAT_TYPE, htonl(NFT_NAT_DNAT)))
  161. goto nla_put_failure;
  162. break;
  163. }
  164. if (nla_put_be32(skb, NFTA_NAT_FAMILY, htonl(priv->family)))
  165. goto nla_put_failure;
  166. if (priv->sreg_addr_min) {
  167. if (nla_put_be32(skb, NFTA_NAT_REG_ADDR_MIN,
  168. htonl(priv->sreg_addr_min)) ||
  169. nla_put_be32(skb, NFTA_NAT_REG_ADDR_MAX,
  170. htonl(priv->sreg_addr_max)))
  171. goto nla_put_failure;
  172. }
  173. if (priv->sreg_proto_min) {
  174. if (nla_put_be32(skb, NFTA_NAT_REG_PROTO_MIN,
  175. htonl(priv->sreg_proto_min)) ||
  176. nla_put_be32(skb, NFTA_NAT_REG_PROTO_MAX,
  177. htonl(priv->sreg_proto_max)))
  178. goto nla_put_failure;
  179. }
  180. if (priv->flags != 0) {
  181. if (nla_put_be32(skb, NFTA_NAT_FLAGS, htonl(priv->flags)))
  182. goto nla_put_failure;
  183. }
  184. return 0;
  185. nla_put_failure:
  186. return -1;
  187. }
  188. static int nft_nat_validate(const struct nft_ctx *ctx,
  189. const struct nft_expr *expr,
  190. const struct nft_data **data)
  191. {
  192. return nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT);
  193. }
  194. static struct nft_expr_type nft_nat_type;
  195. static const struct nft_expr_ops nft_nat_ops = {
  196. .type = &nft_nat_type,
  197. .size = NFT_EXPR_SIZE(sizeof(struct nft_nat)),
  198. .eval = nft_nat_eval,
  199. .init = nft_nat_init,
  200. .dump = nft_nat_dump,
  201. .validate = nft_nat_validate,
  202. };
  203. static struct nft_expr_type nft_nat_type __read_mostly = {
  204. .name = "nat",
  205. .ops = &nft_nat_ops,
  206. .policy = nft_nat_policy,
  207. .maxattr = NFTA_NAT_MAX,
  208. .owner = THIS_MODULE,
  209. };
  210. static int __init nft_nat_module_init(void)
  211. {
  212. return nft_register_expr(&nft_nat_type);
  213. }
  214. static void __exit nft_nat_module_exit(void)
  215. {
  216. nft_unregister_expr(&nft_nat_type);
  217. }
  218. module_init(nft_nat_module_init);
  219. module_exit(nft_nat_module_exit);
  220. MODULE_LICENSE("GPL");
  221. MODULE_AUTHOR("Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>");
  222. MODULE_ALIAS_NFT_EXPR("nat");