Linux Audio

Check our new training course

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