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>
30 enum nft_registers sreg_addr_min:8;
31 enum nft_registers sreg_addr_max:8;
32 enum nft_registers sreg_proto_min:8;
33 enum nft_registers sreg_proto_max:8;
34 enum nf_nat_manip_type type:8;
39 static void nft_nat_eval(const struct nft_expr *expr,
40 struct nft_data data[NFT_REG_MAX + 1],
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_range 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 data[priv->sreg_addr_min].data[0];
53 range.max_addr.ip = (__force __be32)
54 data[priv->sreg_addr_max].data[0];
57 memcpy(range.min_addr.ip6,
58 data[priv->sreg_addr_min].data,
59 sizeof(struct nft_data));
60 memcpy(range.max_addr.ip6,
61 data[priv->sreg_addr_max].data,
62 sizeof(struct nft_data));
64 range.flags |= NF_NAT_RANGE_MAP_IPS;
67 if (priv->sreg_proto_min) {
68 range.min_proto.all = (__force __be16)
69 data[priv->sreg_proto_min].data[0];
70 range.max_proto.all = (__force __be16)
71 data[priv->sreg_proto_max].data[0];
72 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
75 range.flags |= priv->flags;
77 data[NFT_REG_VERDICT].verdict =
78 nf_nat_setup_info(ct, &range, priv->type);
81 static const struct nla_policy nft_nat_policy[NFTA_NAT_MAX + 1] = {
82 [NFTA_NAT_TYPE] = { .type = NLA_U32 },
83 [NFTA_NAT_FAMILY] = { .type = NLA_U32 },
84 [NFTA_NAT_REG_ADDR_MIN] = { .type = NLA_U32 },
85 [NFTA_NAT_REG_ADDR_MAX] = { .type = NLA_U32 },
86 [NFTA_NAT_REG_PROTO_MIN] = { .type = NLA_U32 },
87 [NFTA_NAT_REG_PROTO_MAX] = { .type = NLA_U32 },
88 [NFTA_NAT_FLAGS] = { .type = NLA_U32 },
91 static int nft_nat_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
92 const struct nlattr * const tb[])
94 struct nft_nat *priv = nft_expr_priv(expr);
98 if (tb[NFTA_NAT_TYPE] == NULL)
101 switch (ntohl(nla_get_be32(tb[NFTA_NAT_TYPE]))) {
103 priv->type = NF_NAT_MANIP_SRC;
106 priv->type = NF_NAT_MANIP_DST;
112 if (tb[NFTA_NAT_FAMILY] == NULL)
115 family = ntohl(nla_get_be32(tb[NFTA_NAT_FAMILY]));
116 if (family != AF_INET && family != AF_INET6)
117 return -EAFNOSUPPORT;
118 if (family != ctx->afi->family)
120 priv->family = family;
122 if (tb[NFTA_NAT_REG_ADDR_MIN]) {
123 priv->sreg_addr_min = ntohl(nla_get_be32(
124 tb[NFTA_NAT_REG_ADDR_MIN]));
125 err = nft_validate_input_register(priv->sreg_addr_min);
130 if (tb[NFTA_NAT_REG_ADDR_MAX]) {
131 priv->sreg_addr_max = ntohl(nla_get_be32(
132 tb[NFTA_NAT_REG_ADDR_MAX]));
133 err = nft_validate_input_register(priv->sreg_addr_max);
137 priv->sreg_addr_max = priv->sreg_addr_min;
139 if (tb[NFTA_NAT_REG_PROTO_MIN]) {
140 priv->sreg_proto_min = ntohl(nla_get_be32(
141 tb[NFTA_NAT_REG_PROTO_MIN]));
142 err = nft_validate_input_register(priv->sreg_proto_min);
147 if (tb[NFTA_NAT_REG_PROTO_MAX]) {
148 priv->sreg_proto_max = ntohl(nla_get_be32(
149 tb[NFTA_NAT_REG_PROTO_MAX]));
150 err = nft_validate_input_register(priv->sreg_proto_max);
154 priv->sreg_proto_max = priv->sreg_proto_min;
156 if (tb[NFTA_NAT_FLAGS]) {
157 priv->flags = ntohl(nla_get_be32(tb[NFTA_NAT_FLAGS]));
158 if (priv->flags & ~NF_NAT_RANGE_MASK)
165 static int nft_nat_dump(struct sk_buff *skb, const struct nft_expr *expr)
167 const struct nft_nat *priv = nft_expr_priv(expr);
169 switch (priv->type) {
170 case NF_NAT_MANIP_SRC:
171 if (nla_put_be32(skb, NFTA_NAT_TYPE, htonl(NFT_NAT_SNAT)))
172 goto nla_put_failure;
174 case NF_NAT_MANIP_DST:
175 if (nla_put_be32(skb, NFTA_NAT_TYPE, htonl(NFT_NAT_DNAT)))
176 goto nla_put_failure;
180 if (nla_put_be32(skb, NFTA_NAT_FAMILY, htonl(priv->family)))
181 goto nla_put_failure;
182 if (nla_put_be32(skb,
183 NFTA_NAT_REG_ADDR_MIN, htonl(priv->sreg_addr_min)))
184 goto nla_put_failure;
185 if (nla_put_be32(skb,
186 NFTA_NAT_REG_ADDR_MAX, htonl(priv->sreg_addr_max)))
187 goto nla_put_failure;
188 if (priv->sreg_proto_min) {
189 if (nla_put_be32(skb, NFTA_NAT_REG_PROTO_MIN,
190 htonl(priv->sreg_proto_min)))
191 goto nla_put_failure;
192 if (nla_put_be32(skb, NFTA_NAT_REG_PROTO_MAX,
193 htonl(priv->sreg_proto_max)))
194 goto nla_put_failure;
197 if (priv->flags != 0) {
198 if (nla_put_be32(skb, NFTA_NAT_FLAGS, htonl(priv->flags)))
199 goto nla_put_failure;
208 static struct nft_expr_type nft_nat_type;
209 static const struct nft_expr_ops nft_nat_ops = {
210 .type = &nft_nat_type,
211 .size = NFT_EXPR_SIZE(sizeof(struct nft_nat)),
212 .eval = nft_nat_eval,
213 .init = nft_nat_init,
214 .dump = nft_nat_dump,
217 static struct nft_expr_type nft_nat_type __read_mostly = {
220 .policy = nft_nat_policy,
221 .maxattr = NFTA_NAT_MAX,
222 .owner = THIS_MODULE,
225 static int __init nft_nat_module_init(void)
227 return nft_register_expr(&nft_nat_type);
230 static void __exit nft_nat_module_exit(void)
232 nft_unregister_expr(&nft_nat_type);
235 module_init(nft_nat_module_init);
236 module_exit(nft_nat_module_exit);
238 MODULE_LICENSE("GPL");
240 MODULE_ALIAS_NFT_EXPR("nat");