blob: 81b5ad6165ac7e4c51101fc696ee0e5447996fef [file] [log] [blame]
Arturo Borrero9ba1f7262014-09-08 13:45:00 +02001/*
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
19const struct nla_policy nft_masq_policy[NFTA_MASQ_MAX + 1] = {
Pablo Neira Ayuso8a6bf5d2016-03-01 19:55:14 +010020 [NFTA_MASQ_FLAGS] = { .type = NLA_U32 },
21 [NFTA_MASQ_REG_PROTO_MIN] = { .type = NLA_U32 },
22 [NFTA_MASQ_REG_PROTO_MAX] = { .type = NLA_U32 },
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020023};
24EXPORT_SYMBOL_GPL(nft_masq_policy);
25
Pablo Neira Ayuso75e8d062015-01-14 15:33:57 +010026int nft_masq_validate(const struct nft_ctx *ctx,
27 const struct nft_expr *expr,
28 const struct nft_data **data)
29{
30 int err;
31
32 err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT);
33 if (err < 0)
34 return err;
35
36 return nft_chain_validate_hooks(ctx->chain,
37 (1 << NF_INET_POST_ROUTING));
38}
39EXPORT_SYMBOL_GPL(nft_masq_validate);
40
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020041int nft_masq_init(const struct nft_ctx *ctx,
42 const struct nft_expr *expr,
43 const struct nlattr * const tb[])
44{
Pablo Neira Ayuso8a6bf5d2016-03-01 19:55:14 +010045 u32 plen = FIELD_SIZEOF(struct nf_nat_range, min_addr.all);
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020046 struct nft_masq *priv = nft_expr_priv(expr);
Pablo Neira Ayuso7210e4e2014-10-13 19:50:22 +020047 int err;
48
Pablo Neira Ayuso75e8d062015-01-14 15:33:57 +010049 err = nft_masq_validate(ctx, expr, NULL);
50 if (err)
Pablo Neira Ayuso7210e4e2014-10-13 19:50:22 +020051 return err;
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020052
Pablo Neira Ayuso8a6bf5d2016-03-01 19:55:14 +010053 if (tb[NFTA_MASQ_FLAGS]) {
54 priv->flags = ntohl(nla_get_be32(tb[NFTA_MASQ_FLAGS]));
55 if (priv->flags & ~NF_NAT_RANGE_MASK)
56 return -EINVAL;
57 }
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020058
Pablo Neira Ayuso8a6bf5d2016-03-01 19:55:14 +010059 if (tb[NFTA_MASQ_REG_PROTO_MIN]) {
60 priv->sreg_proto_min =
61 nft_parse_register(tb[NFTA_MASQ_REG_PROTO_MIN]);
62
63 err = nft_validate_register_load(priv->sreg_proto_min, plen);
64 if (err < 0)
65 return err;
66
67 if (tb[NFTA_MASQ_REG_PROTO_MAX]) {
68 priv->sreg_proto_max =
69 nft_parse_register(tb[NFTA_MASQ_REG_PROTO_MAX]);
70
71 err = nft_validate_register_load(priv->sreg_proto_max,
72 plen);
73 if (err < 0)
74 return err;
75 } else {
76 priv->sreg_proto_max = priv->sreg_proto_min;
77 }
78 }
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020079
80 return 0;
81}
82EXPORT_SYMBOL_GPL(nft_masq_init);
83
84int nft_masq_dump(struct sk_buff *skb, const struct nft_expr *expr)
85{
86 const struct nft_masq *priv = nft_expr_priv(expr);
87
Pablo Neira Ayuso8a6bf5d2016-03-01 19:55:14 +010088 if (priv->flags != 0 &&
89 nla_put_be32(skb, NFTA_MASQ_FLAGS, htonl(priv->flags)))
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020090 goto nla_put_failure;
91
Pablo Neira Ayuso8a6bf5d2016-03-01 19:55:14 +010092 if (priv->sreg_proto_min) {
93 if (nft_dump_register(skb, NFTA_MASQ_REG_PROTO_MIN,
94 priv->sreg_proto_min) ||
95 nft_dump_register(skb, NFTA_MASQ_REG_PROTO_MAX,
96 priv->sreg_proto_max))
97 goto nla_put_failure;
98 }
99
Arturo Borrero9ba1f7262014-09-08 13:45:00 +0200100 return 0;
101
102nla_put_failure:
103 return -1;
104}
105EXPORT_SYMBOL_GPL(nft_masq_dump);
106
Arturo Borrero9ba1f7262014-09-08 13:45:00 +0200107MODULE_LICENSE("GPL");
108MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>");