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
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.
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/skbuff.h>
16 #include <linux/string.h>
17 #include <linux/netlink.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter_ipv4.h>
20 #include <linux/netfilter/nfnetlink.h>
21 #include <linux/netfilter/nf_tables.h>
22 #include <net/netfilter/nf_conntrack.h>
23 #include <net/netfilter/nf_nat.h>
24 #include <net/netfilter/nf_nat_core.h>
25 #include <net/netfilter/nf_tables.h>
26 #include <net/netfilter/nf_nat_l3proto.h>
34 enum nf_nat_manip_type type:8;
39 static void nft_nat_eval(const struct nft_expr *expr,
40 struct nft_regs *regs,
41 const struct nft_pktinfo *pkt)
43 const struct nft_nat *priv = nft_expr_priv(expr);
44 enum ip_conntrack_info ctinfo;
45 struct nf_conn *ct = nf_ct_get(pkt->skb, &ctinfo);
46 struct nf_nat_range2 range;
48 memset(&range, 0, sizeof(range));
49 if (priv->sreg_addr_min) {
50 if (priv->family == AF_INET) {
51 range.min_addr.ip = (__force __be32)
52 regs->data[priv->sreg_addr_min];
53 range.max_addr.ip = (__force __be32)
54 regs->data[priv->sreg_addr_max];
57 memcpy(range.min_addr.ip6,
58 ®s->data[priv->sreg_addr_min],
59 sizeof(range.min_addr.ip6));
60 memcpy(range.max_addr.ip6,
61 ®s->data[priv->sreg_addr_max],
62 sizeof(range.max_addr.ip6));
64 range.flags |= NF_NAT_RANGE_MAP_IPS;
67 if (priv->sreg_proto_min) {
68 range.min_proto.all = (__force __be16)nft_reg_load16(
69 ®s->data[priv->sreg_proto_min]);
70 range.max_proto.all = (__force __be16)nft_reg_load16(
71 ®s->data[priv->sreg_proto_max]);
72 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
75 range.flags |= priv->flags;
77 regs->verdict.code = nf_nat_setup_info(ct, &range, priv->type);
80 static const struct nla_policy nft_nat_policy[NFTA_NAT_MAX + 1] = {
81 [NFTA_NAT_TYPE] = { .type = NLA_U32 },
82 [NFTA_NAT_FAMILY] = { .type = NLA_U32 },
83 [NFTA_NAT_REG_ADDR_MIN] = { .type = NLA_U32 },
84 [NFTA_NAT_REG_ADDR_MAX] = { .type = NLA_U32 },
85 [NFTA_NAT_REG_PROTO_MIN] = { .type = NLA_U32 },
86 [NFTA_NAT_REG_PROTO_MAX] = { .type = NLA_U32 },
87 [NFTA_NAT_FLAGS] = { .type = NLA_U32 },
90 static int nft_nat_validate(const struct nft_ctx *ctx,
91 const struct nft_expr *expr,
92 const struct nft_data **data)
94 struct nft_nat *priv = nft_expr_priv(expr);
97 err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT);
101 switch (priv->type) {
103 err = nft_chain_validate_hooks(ctx->chain,
104 (1 << NF_INET_POST_ROUTING) |
105 (1 << NF_INET_LOCAL_IN));
108 err = nft_chain_validate_hooks(ctx->chain,
109 (1 << NF_INET_PRE_ROUTING) |
110 (1 << NF_INET_LOCAL_OUT));
117 static int nft_nat_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
118 const struct nlattr * const tb[])
120 struct nft_nat *priv = nft_expr_priv(expr);
121 unsigned int alen, plen;
125 if (tb[NFTA_NAT_TYPE] == NULL ||
126 (tb[NFTA_NAT_REG_ADDR_MIN] == NULL &&
127 tb[NFTA_NAT_REG_PROTO_MIN] == NULL))
130 switch (ntohl(nla_get_be32(tb[NFTA_NAT_TYPE]))) {
132 priv->type = NF_NAT_MANIP_SRC;
135 priv->type = NF_NAT_MANIP_DST;
141 if (tb[NFTA_NAT_FAMILY] == NULL)
144 family = ntohl(nla_get_be32(tb[NFTA_NAT_FAMILY]));
145 if (family != ctx->family)
150 alen = FIELD_SIZEOF(struct nf_nat_range, min_addr.ip);
153 alen = FIELD_SIZEOF(struct nf_nat_range, min_addr.ip6);
156 if (tb[NFTA_NAT_REG_ADDR_MIN])
157 return -EAFNOSUPPORT;
160 priv->family = family;
162 if (tb[NFTA_NAT_REG_ADDR_MIN]) {
163 err = nft_parse_register_load(tb[NFTA_NAT_REG_ADDR_MIN],
164 &priv->sreg_addr_min, alen);
168 if (tb[NFTA_NAT_REG_ADDR_MAX]) {
169 err = nft_parse_register_load(tb[NFTA_NAT_REG_ADDR_MAX],
170 &priv->sreg_addr_max,
175 priv->sreg_addr_max = priv->sreg_addr_min;
179 plen = FIELD_SIZEOF(struct nf_nat_range, min_addr.all);
180 if (tb[NFTA_NAT_REG_PROTO_MIN]) {
181 err = nft_parse_register_load(tb[NFTA_NAT_REG_PROTO_MIN],
182 &priv->sreg_proto_min, plen);
186 if (tb[NFTA_NAT_REG_PROTO_MAX]) {
187 err = nft_parse_register_load(tb[NFTA_NAT_REG_PROTO_MAX],
188 &priv->sreg_proto_max,
193 priv->sreg_proto_max = priv->sreg_proto_min;
197 if (tb[NFTA_NAT_FLAGS]) {
198 priv->flags = ntohl(nla_get_be32(tb[NFTA_NAT_FLAGS]));
199 if (priv->flags & ~NF_NAT_RANGE_MASK)
203 return nf_ct_netns_get(ctx->net, family);
206 static int nft_nat_dump(struct sk_buff *skb, const struct nft_expr *expr)
208 const struct nft_nat *priv = nft_expr_priv(expr);
210 switch (priv->type) {
211 case NF_NAT_MANIP_SRC:
212 if (nla_put_be32(skb, NFTA_NAT_TYPE, htonl(NFT_NAT_SNAT)))
213 goto nla_put_failure;
215 case NF_NAT_MANIP_DST:
216 if (nla_put_be32(skb, NFTA_NAT_TYPE, htonl(NFT_NAT_DNAT)))
217 goto nla_put_failure;
221 if (nla_put_be32(skb, NFTA_NAT_FAMILY, htonl(priv->family)))
222 goto nla_put_failure;
224 if (priv->sreg_addr_min) {
225 if (nft_dump_register(skb, NFTA_NAT_REG_ADDR_MIN,
226 priv->sreg_addr_min) ||
227 nft_dump_register(skb, NFTA_NAT_REG_ADDR_MAX,
228 priv->sreg_addr_max))
229 goto nla_put_failure;
232 if (priv->sreg_proto_min) {
233 if (nft_dump_register(skb, NFTA_NAT_REG_PROTO_MIN,
234 priv->sreg_proto_min) ||
235 nft_dump_register(skb, NFTA_NAT_REG_PROTO_MAX,
236 priv->sreg_proto_max))
237 goto nla_put_failure;
240 if (priv->flags != 0) {
241 if (nla_put_be32(skb, NFTA_NAT_FLAGS, htonl(priv->flags)))
242 goto nla_put_failure;
252 nft_nat_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
254 const struct nft_nat *priv = nft_expr_priv(expr);
256 nf_ct_netns_put(ctx->net, priv->family);
259 static struct nft_expr_type nft_nat_type;
260 static const struct nft_expr_ops nft_nat_ops = {
261 .type = &nft_nat_type,
262 .size = NFT_EXPR_SIZE(sizeof(struct nft_nat)),
263 .eval = nft_nat_eval,
264 .init = nft_nat_init,
265 .destroy = nft_nat_destroy,
266 .dump = nft_nat_dump,
267 .validate = nft_nat_validate,
270 static struct nft_expr_type nft_nat_type __read_mostly = {
273 .policy = nft_nat_policy,
274 .maxattr = NFTA_NAT_MAX,
275 .owner = THIS_MODULE,
278 static int __init nft_nat_module_init(void)
280 return nft_register_expr(&nft_nat_type);
283 static void __exit nft_nat_module_exit(void)
285 nft_unregister_expr(&nft_nat_type);
288 module_init(nft_nat_module_init);
289 module_exit(nft_nat_module_exit);
291 MODULE_LICENSE("GPL");
292 MODULE_AUTHOR("Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>");
293 MODULE_ALIAS_NFT_EXPR("nat");