Skip to content

Commit 9ba1f72

Browse files
aborreroummakynes
authored andcommitted
netfilter: nf_tables: add new nft_masq expression
The nft_masq expression is intended to perform NAT in the masquerade flavour. We decided to have the masquerade functionality in a separated expression other than nft_nat. Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent be6b635 commit 9ba1f72

11 files changed

Lines changed: 288 additions & 0 deletions

File tree

‎include/net/netfilter/nft_masq.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef _NFT_MASQ_H_
2+
#define _NFT_MASQ_H_
3+
4+
struct nft_masq {
5+
u32 flags;
6+
};
7+
8+
extern const struct nla_policy nft_masq_policy[];
9+
10+
int nft_masq_init(const struct nft_ctx *ctx,
11+
const struct nft_expr *expr,
12+
const struct nlattr * const tb[]);
13+
14+
int nft_masq_dump(struct sk_buff *skb, const struct nft_expr *expr);
15+
16+
#endif /* _NFT_MASQ_H_ */

‎include/uapi/linux/netfilter/nf_tables.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,4 +800,15 @@ enum nft_nat_attributes {
800800
};
801801
#define NFTA_NAT_MAX (__NFTA_NAT_MAX - 1)
802802

803+
/**
804+
* enum nft_masq_attributes - nf_tables masquerade expression attributes
805+
*
806+
* @NFTA_MASQ_FLAGS: NAT flags (see NF_NAT_RANGE_* in linux/netfilter/nf_nat.h) (NLA_U32)
807+
*/
808+
enum nft_masq_attributes {
809+
NFTA_MASQ_FLAGS,
810+
__NFTA_MASQ_MAX
811+
};
812+
#define NFTA_MASQ_MAX (__NFTA_MASQ_MAX - 1)
813+
803814
#endif /* _LINUX_NF_TABLES_H */

‎net/ipv4/netfilter/Kconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ config NF_NAT_MASQUERADE_IPV4
190190
This is the kernel functionality to provide NAT in the masquerade
191191
flavour (automatic source address selection).
192192

193+
config NFT_MASQ_IPV4
194+
tristate "IPv4 masquerading support for nf_tables"
195+
depends on NF_TABLES_IPV4
196+
depends on NFT_MASQ
197+
select NF_NAT_MASQUERADE_IPV4
198+
193199
config IP_NF_TARGET_MASQUERADE
194200
tristate "MASQUERADE target support"
195201
select NF_NAT_MASQUERADE_IPV4

‎net/ipv4/netfilter/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ obj-$(CONFIG_NF_TABLES_IPV4) += nf_tables_ipv4.o
3636
obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV4) += nft_chain_route_ipv4.o
3737
obj-$(CONFIG_NFT_CHAIN_NAT_IPV4) += nft_chain_nat_ipv4.o
3838
obj-$(CONFIG_NFT_REJECT_IPV4) += nft_reject_ipv4.o
39+
obj-$(CONFIG_NFT_MASQ_IPV4) += nft_masq_ipv4.o
3940
obj-$(CONFIG_NF_TABLES_ARP) += nf_tables_arp.o
4041

4142
# generic IP tables

‎net/ipv4/netfilter/nft_masq_ipv4.c‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2014 Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License version 2 as
6+
* published by the Free Software Foundation.
7+
*/
8+
9+
#include <linux/kernel.h>
10+
#include <linux/init.h>
11+
#include <linux/module.h>
12+
#include <linux/netlink.h>
13+
#include <linux/netfilter.h>
14+
#include <linux/netfilter/nf_tables.h>
15+
#include <net/netfilter/nf_tables.h>
16+
#include <net/netfilter/nft_masq.h>
17+
#include <net/netfilter/ipv4/nf_nat_masquerade.h>
18+
19+
static void nft_masq_ipv4_eval(const struct nft_expr *expr,
20+
struct nft_data data[NFT_REG_MAX + 1],
21+
const struct nft_pktinfo *pkt)
22+
{
23+
struct nft_masq *priv = nft_expr_priv(expr);
24+
struct nf_nat_range range;
25+
unsigned int verdict;
26+
27+
range.flags = priv->flags;
28+
29+
verdict = nf_nat_masquerade_ipv4(pkt->skb, pkt->ops->hooknum,
30+
&range, pkt->out);
31+
32+
data[NFT_REG_VERDICT].verdict = verdict;
33+
}
34+
35+
static int nft_masq_ipv4_init(const struct nft_ctx *ctx,
36+
const struct nft_expr *expr,
37+
const struct nlattr * const tb[])
38+
{
39+
int err;
40+
41+
err = nft_masq_init(ctx, expr, tb);
42+
if (err < 0)
43+
return err;
44+
45+
nf_nat_masquerade_ipv4_register_notifier();
46+
return 0;
47+
}
48+
49+
static void nft_masq_ipv4_destroy(const struct nft_ctx *ctx,
50+
const struct nft_expr *expr)
51+
{
52+
nf_nat_masquerade_ipv4_unregister_notifier();
53+
}
54+
55+
static struct nft_expr_type nft_masq_ipv4_type;
56+
static const struct nft_expr_ops nft_masq_ipv4_ops = {
57+
.type = &nft_masq_ipv4_type,
58+
.size = NFT_EXPR_SIZE(sizeof(struct nft_masq)),
59+
.eval = nft_masq_ipv4_eval,
60+
.init = nft_masq_ipv4_init,
61+
.destroy = nft_masq_ipv4_destroy,
62+
.dump = nft_masq_dump,
63+
};
64+
65+
static struct nft_expr_type nft_masq_ipv4_type __read_mostly = {
66+
.family = NFPROTO_IPV4,
67+
.name = "masq",
68+
.ops = &nft_masq_ipv4_ops,
69+
.policy = nft_masq_policy,
70+
.maxattr = NFTA_MASQ_MAX,
71+
.owner = THIS_MODULE,
72+
};
73+
74+
static int __init nft_masq_ipv4_module_init(void)
75+
{
76+
return nft_register_expr(&nft_masq_ipv4_type);
77+
}
78+
79+
static void __exit nft_masq_ipv4_module_exit(void)
80+
{
81+
nft_unregister_expr(&nft_masq_ipv4_type);
82+
}
83+
84+
module_init(nft_masq_ipv4_module_init);
85+
module_exit(nft_masq_ipv4_module_exit);
86+
87+
MODULE_LICENSE("GPL");
88+
MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>");
89+
MODULE_ALIAS_NFT_AF_EXPR(AF_INET, "masq");

‎net/ipv6/netfilter/Kconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ config NF_NAT_MASQUERADE_IPV6
252252
This is the kernel functionality to provide NAT in the masquerade
253253
flavour (automatic source address selection) for IPv6.
254254

255+
config NFT_MASQ_IPV6
256+
tristate "IPv6 masquerade support for nf_tables"
257+
depends on NF_TABLES_IPV6
258+
depends on NFT_MASQ
259+
select NF_NAT_MASQUERADE_IPV6
260+
255261
config IP6_NF_TARGET_MASQUERADE
256262
tristate "MASQUERADE target support"
257263
select NF_NAT_MASQUERADE_IPV6

‎net/ipv6/netfilter/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ obj-$(CONFIG_NF_TABLES_IPV6) += nf_tables_ipv6.o
3232
obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV6) += nft_chain_route_ipv6.o
3333
obj-$(CONFIG_NFT_CHAIN_NAT_IPV6) += nft_chain_nat_ipv6.o
3434
obj-$(CONFIG_NFT_REJECT_IPV6) += nft_reject_ipv6.o
35+
obj-$(CONFIG_NFT_MASQ_IPV6) += nft_masq_ipv6.o
3536

3637
# matches
3738
obj-$(CONFIG_IP6_NF_MATCH_AH) += ip6t_ah.o

‎net/ipv6/netfilter/nft_masq_ipv6.c‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2014 Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License version 2 as
6+
* published by the Free Software Foundation.
7+
*/
8+
9+
#include <linux/kernel.h>
10+
#include <linux/init.h>
11+
#include <linux/module.h>
12+
#include <linux/netlink.h>
13+
#include <linux/netfilter.h>
14+
#include <linux/netfilter/nf_tables.h>
15+
#include <net/netfilter/nf_tables.h>
16+
#include <net/netfilter/nf_nat.h>
17+
#include <net/netfilter/nft_masq.h>
18+
#include <net/netfilter/ipv6/nf_nat_masquerade.h>
19+
20+
static void nft_masq_ipv6_eval(const struct nft_expr *expr,
21+
struct nft_data data[NFT_REG_MAX + 1],
22+
const struct nft_pktinfo *pkt)
23+
{
24+
struct nft_masq *priv = nft_expr_priv(expr);
25+
struct nf_nat_range range;
26+
unsigned int verdict;
27+
28+
range.flags = priv->flags;
29+
30+
verdict = nf_nat_masquerade_ipv6(pkt->skb, &range, pkt->out);
31+
32+
data[NFT_REG_VERDICT].verdict = verdict;
33+
}
34+
35+
static int nft_masq_ipv6_init(const struct nft_ctx *ctx,
36+
const struct nft_expr *expr,
37+
const struct nlattr * const tb[])
38+
{
39+
int err;
40+
41+
err = nft_masq_init(ctx, expr, tb);
42+
if (err < 0)
43+
return err;
44+
45+
nf_nat_masquerade_ipv6_register_notifier();
46+
return 0;
47+
}
48+
49+
static void nft_masq_ipv6_destroy(const struct nft_ctx *ctx,
50+
const struct nft_expr *expr)
51+
{
52+
nf_nat_masquerade_ipv6_unregister_notifier();
53+
}
54+
55+
static struct nft_expr_type nft_masq_ipv6_type;
56+
static const struct nft_expr_ops nft_masq_ipv6_ops = {
57+
.type = &nft_masq_ipv6_type,
58+
.size = NFT_EXPR_SIZE(sizeof(struct nft_masq)),
59+
.eval = nft_masq_ipv6_eval,
60+
.init = nft_masq_ipv6_init,
61+
.destroy = nft_masq_ipv6_destroy,
62+
.dump = nft_masq_dump,
63+
};
64+
65+
static struct nft_expr_type nft_masq_ipv6_type __read_mostly = {
66+
.family = NFPROTO_IPV6,
67+
.name = "masq",
68+
.ops = &nft_masq_ipv6_ops,
69+
.policy = nft_masq_policy,
70+
.maxattr = NFTA_MASQ_MAX,
71+
.owner = THIS_MODULE,
72+
};
73+
74+
static int __init nft_masq_ipv6_module_init(void)
75+
{
76+
return nft_register_expr(&nft_masq_ipv6_type);
77+
}
78+
79+
static void __exit nft_masq_ipv6_module_exit(void)
80+
{
81+
nft_unregister_expr(&nft_masq_ipv6_type);
82+
}
83+
84+
module_init(nft_masq_ipv6_module_init);
85+
module_exit(nft_masq_ipv6_module_exit);
86+
87+
MODULE_LICENSE("GPL");
88+
MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>");
89+
MODULE_ALIAS_NFT_AF_EXPR(AF_INET6, "masq");

‎net/netfilter/Kconfig‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,15 @@ config NFT_LIMIT
496496
This option adds the "limit" expression that you can use to
497497
ratelimit rule matchings.
498498

499+
config NFT_MASQ
500+
depends on NF_TABLES
501+
depends on NF_CONNTRACK
502+
depends on NF_NAT
503+
tristate "Netfilter nf_tables masquerade support"
504+
help
505+
This option adds the "masquerade" expression that you can use
506+
to perform NAT in the masquerade flavour.
507+
499508
config NFT_NAT
500509
depends on NF_TABLES
501510
depends on NF_CONNTRACK

‎net/netfilter/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ obj-$(CONFIG_NFT_RBTREE) += nft_rbtree.o
8787
obj-$(CONFIG_NFT_HASH) += nft_hash.o
8888
obj-$(CONFIG_NFT_COUNTER) += nft_counter.o
8989
obj-$(CONFIG_NFT_LOG) += nft_log.o
90+
obj-$(CONFIG_NFT_MASQ) += nft_masq.o
9091

9192
# generic X tables
9293
obj-$(CONFIG_NETFILTER_XTABLES) += x_tables.o xt_tcpudp.o

0 commit comments

Comments
 (0)