2014-09-08 11:45:00 +00:00
|
|
|
/*
|
2016-10-18 12:02:29 +00:00
|
|
|
* Copyright (c) 2014 Arturo Borrero Gonzalez <arturo@debian.org>
|
2014-09-08 11:45:00 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/netlink.h>
|
|
|
|
#include <linux/netfilter.h>
|
|
|
|
#include <linux/netfilter/nf_tables.h>
|
|
|
|
#include <net/netfilter/nf_tables.h>
|
|
|
|
#include <net/netfilter/nft_masq.h>
|
|
|
|
#include <net/netfilter/ipv4/nf_nat_masquerade.h>
|
|
|
|
|
|
|
|
static void nft_masq_ipv4_eval(const struct nft_expr *expr,
|
2015-04-11 01:27:31 +00:00
|
|
|
struct nft_regs *regs,
|
2014-09-08 11:45:00 +00:00
|
|
|
const struct nft_pktinfo *pkt)
|
|
|
|
{
|
|
|
|
struct nft_masq *priv = nft_expr_priv(expr);
|
|
|
|
struct nf_nat_range range;
|
|
|
|
|
2014-11-07 14:34:54 +00:00
|
|
|
memset(&range, 0, sizeof(range));
|
2014-09-08 11:45:00 +00:00
|
|
|
range.flags = priv->flags;
|
2016-03-01 18:55:14 +00:00
|
|
|
if (priv->sreg_proto_min) {
|
netfilter: nf_tables: fix mismatch in big-endian system
Currently, there are two different methods to store an u16 integer to
the u32 data register. For example:
u32 *dest = ®s->data[priv->dreg];
1. *dest = 0; *(u16 *) dest = val_u16;
2. *dest = val_u16;
For method 1, the u16 value will be stored like this, either in
big-endian or little-endian system:
0 15 31
+-+-+-+-+-+-+-+-+-+-+-+-+
| Value | 0 |
+-+-+-+-+-+-+-+-+-+-+-+-+
For method 2, in little-endian system, the u16 value will be the same
as listed above. But in big-endian system, the u16 value will be stored
like this:
0 15 31
+-+-+-+-+-+-+-+-+-+-+-+-+
| 0 | Value |
+-+-+-+-+-+-+-+-+-+-+-+-+
So later we use "memcmp(®s->data[priv->sreg], data, 2);" to do
compare in nft_cmp, nft_lookup expr ..., method 2 will get the wrong
result in big-endian system, as 0~15 bits will always be zero.
For the similar reason, when loading an u16 value from the u32 data
register, we should use "*(u16 *) sreg;" instead of "(u16)*sreg;",
the 2nd method will get the wrong value in the big-endian system.
So introduce some wrapper functions to store/load an u8 or u16
integer to/from the u32 data register, and use them in the right
place.
Signed-off-by: Liping Zhang <zlpnobody@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2017-03-08 14:54:18 +00:00
|
|
|
range.min_proto.all = (__force __be16)nft_reg_load16(
|
|
|
|
®s->data[priv->sreg_proto_min]);
|
|
|
|
range.max_proto.all = (__force __be16)nft_reg_load16(
|
|
|
|
®s->data[priv->sreg_proto_max]);
|
2016-03-01 18:55:14 +00:00
|
|
|
}
|
2016-11-03 09:56:26 +00:00
|
|
|
regs->verdict.code = nf_nat_masquerade_ipv4(pkt->skb, nft_hook(pkt),
|
|
|
|
&range, nft_out(pkt));
|
2014-09-08 11:45:00 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 20:36:42 +00:00
|
|
|
static void
|
|
|
|
nft_masq_ipv4_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
|
|
|
|
{
|
|
|
|
nf_ct_netns_put(ctx->net, NFPROTO_IPV4);
|
|
|
|
}
|
|
|
|
|
2014-09-08 11:45:00 +00:00
|
|
|
static struct nft_expr_type nft_masq_ipv4_type;
|
|
|
|
static const struct nft_expr_ops nft_masq_ipv4_ops = {
|
|
|
|
.type = &nft_masq_ipv4_type,
|
|
|
|
.size = NFT_EXPR_SIZE(sizeof(struct nft_masq)),
|
|
|
|
.eval = nft_masq_ipv4_eval,
|
2014-10-03 12:13:36 +00:00
|
|
|
.init = nft_masq_init,
|
2016-11-15 20:36:42 +00:00
|
|
|
.destroy = nft_masq_ipv4_destroy,
|
2014-09-08 11:45:00 +00:00
|
|
|
.dump = nft_masq_dump,
|
2014-10-13 17:50:22 +00:00
|
|
|
.validate = nft_masq_validate,
|
2014-09-08 11:45:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct nft_expr_type nft_masq_ipv4_type __read_mostly = {
|
|
|
|
.family = NFPROTO_IPV4,
|
|
|
|
.name = "masq",
|
|
|
|
.ops = &nft_masq_ipv4_ops,
|
|
|
|
.policy = nft_masq_policy,
|
|
|
|
.maxattr = NFTA_MASQ_MAX,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init nft_masq_ipv4_module_init(void)
|
|
|
|
{
|
2014-10-03 12:13:36 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = nft_register_expr(&nft_masq_ipv4_type);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
nf_nat_masquerade_ipv4_register_notifier();
|
|
|
|
|
|
|
|
return ret;
|
2014-09-08 11:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit nft_masq_ipv4_module_exit(void)
|
|
|
|
{
|
|
|
|
nft_unregister_expr(&nft_masq_ipv4_type);
|
2014-10-03 12:13:36 +00:00
|
|
|
nf_nat_masquerade_ipv4_unregister_notifier();
|
2014-09-08 11:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(nft_masq_ipv4_module_init);
|
|
|
|
module_exit(nft_masq_ipv4_module_exit);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
2016-10-18 12:02:29 +00:00
|
|
|
MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo@debian.org");
|
2014-09-08 11:45:00 +00:00
|
|
|
MODULE_ALIAS_NFT_AF_EXPR(AF_INET, "masq");
|