diff options
| author | Arturo Borrero <arturo.borrero.glez@gmail.com> | 2014-09-08 07:45:00 -0400 |
|---|---|---|
| committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2014-09-09 10:31:30 -0400 |
| commit | 9ba1f726bec090399eb9bb9157eb32dedc8e8c45 (patch) | |
| tree | 4a2875982feb3cad10ccb8f3da4a1f452bd1f068 | |
| parent | be6b635cd674add9410efa9ac6f03e0040848b12 (diff) | |
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>
| -rw-r--r-- | include/net/netfilter/nft_masq.h | 16 | ||||
| -rw-r--r-- | include/uapi/linux/netfilter/nf_tables.h | 11 | ||||
| -rw-r--r-- | net/ipv4/netfilter/Kconfig | 6 | ||||
| -rw-r--r-- | net/ipv4/netfilter/Makefile | 1 | ||||
| -rw-r--r-- | net/ipv4/netfilter/nft_masq_ipv4.c | 89 | ||||
| -rw-r--r-- | net/ipv6/netfilter/Kconfig | 6 | ||||
| -rw-r--r-- | net/ipv6/netfilter/Makefile | 1 | ||||
| -rw-r--r-- | net/ipv6/netfilter/nft_masq_ipv6.c | 89 | ||||
| -rw-r--r-- | net/netfilter/Kconfig | 9 | ||||
| -rw-r--r-- | net/netfilter/Makefile | 1 | ||||
| -rw-r--r-- | net/netfilter/nft_masq.c | 59 |
11 files changed, 288 insertions, 0 deletions
diff --git a/include/net/netfilter/nft_masq.h b/include/net/netfilter/nft_masq.h new file mode 100644 index 000000000000..c72729f954f4 --- /dev/null +++ b/include/net/netfilter/nft_masq.h | |||
| @@ -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_ */ | ||
diff --git a/include/uapi/linux/netfilter/nf_tables.h b/include/uapi/linux/netfilter/nf_tables.h index 6022c6e6be18..eeec0ae845ef 100644 --- a/include/uapi/linux/netfilter/nf_tables.h +++ b/include/uapi/linux/netfilter/nf_tables.h | |||
| @@ -800,4 +800,15 @@ enum nft_nat_attributes { | |||
| 800 | }; | 800 | }; |
| 801 | #define NFTA_NAT_MAX (__NFTA_NAT_MAX - 1) | 801 | #define NFTA_NAT_MAX (__NFTA_NAT_MAX - 1) |
| 802 | 802 | ||
| 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 | |||
| 803 | #endif /* _LINUX_NF_TABLES_H */ | 814 | #endif /* _LINUX_NF_TABLES_H */ |
diff --git a/net/ipv4/netfilter/Kconfig b/net/ipv4/netfilter/Kconfig index 4be3e541350e..8dd3d9f19d82 100644 --- a/net/ipv4/netfilter/Kconfig +++ b/net/ipv4/netfilter/Kconfig | |||
| @@ -190,6 +190,12 @@ config NF_NAT_MASQUERADE_IPV4 | |||
| 190 | This is the kernel functionality to provide NAT in the masquerade | 190 | This is the kernel functionality to provide NAT in the masquerade |
| 191 | flavour (automatic source address selection). | 191 | flavour (automatic source address selection). |
| 192 | 192 | ||
| 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 | |||
| 193 | config IP_NF_TARGET_MASQUERADE | 199 | config IP_NF_TARGET_MASQUERADE |
| 194 | tristate "MASQUERADE target support" | 200 | tristate "MASQUERADE target support" |
| 195 | select NF_NAT_MASQUERADE_IPV4 | 201 | select NF_NAT_MASQUERADE_IPV4 |
diff --git a/net/ipv4/netfilter/Makefile b/net/ipv4/netfilter/Makefile index 42056b2fd0e3..7d019aefb0ed 100644 --- a/net/ipv4/netfilter/Makefile +++ b/net/ipv4/netfilter/Makefile | |||
| @@ -36,6 +36,7 @@ obj-$(CONFIG_NF_TABLES_IPV4) += nf_tables_ipv4.o | |||
| 36 | obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV4) += nft_chain_route_ipv4.o | 36 | obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV4) += nft_chain_route_ipv4.o |
| 37 | obj-$(CONFIG_NFT_CHAIN_NAT_IPV4) += nft_chain_nat_ipv4.o | 37 | obj-$(CONFIG_NFT_CHAIN_NAT_IPV4) += nft_chain_nat_ipv4.o |
| 38 | obj-$(CONFIG_NFT_REJECT_IPV4) += nft_reject_ipv4.o | 38 | obj-$(CONFIG_NFT_REJECT_IPV4) += nft_reject_ipv4.o |
| 39 | obj-$(CONFIG_NFT_MASQ_IPV4) += nft_masq_ipv4.o | ||
| 39 | obj-$(CONFIG_NF_TABLES_ARP) += nf_tables_arp.o | 40 | obj-$(CONFIG_NF_TABLES_ARP) += nf_tables_arp.o |
| 40 | 41 | ||
| 41 | # generic IP tables | 42 | # generic IP tables |
diff --git a/net/ipv4/netfilter/nft_masq_ipv4.c b/net/ipv4/netfilter/nft_masq_ipv4.c new file mode 100644 index 000000000000..6ea1d207b6a5 --- /dev/null +++ b/net/ipv4/netfilter/nft_masq_ipv4.c | |||
| @@ -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"); | ||
diff --git a/net/ipv6/netfilter/Kconfig b/net/ipv6/netfilter/Kconfig index 6c8cfec6836a..24c535f66df0 100644 --- a/net/ipv6/netfilter/Kconfig +++ b/net/ipv6/netfilter/Kconfig | |||
| @@ -252,6 +252,12 @@ config NF_NAT_MASQUERADE_IPV6 | |||
| 252 | This is the kernel functionality to provide NAT in the masquerade | 252 | This is the kernel functionality to provide NAT in the masquerade |
| 253 | flavour (automatic source address selection) for IPv6. | 253 | flavour (automatic source address selection) for IPv6. |
| 254 | 254 | ||
| 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 | |||
| 255 | config IP6_NF_TARGET_MASQUERADE | 261 | config IP6_NF_TARGET_MASQUERADE |
| 256 | tristate "MASQUERADE target support" | 262 | tristate "MASQUERADE target support" |
| 257 | select NF_NAT_MASQUERADE_IPV6 | 263 | select NF_NAT_MASQUERADE_IPV6 |
diff --git a/net/ipv6/netfilter/Makefile b/net/ipv6/netfilter/Makefile index 89a0bd751f82..482c4dff273f 100644 --- a/net/ipv6/netfilter/Makefile +++ b/net/ipv6/netfilter/Makefile | |||
