blob: 03f7bf40ae752d3c8d48425abc5611e4a2af2b63 [file] [log] [blame]
Arturo Borreroe9105f12014-10-17 12:39:09 +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_nat.h>
16#include <net/netfilter/nf_tables.h>
17#include <net/netfilter/nft_redir.h>
18
19const struct nla_policy nft_redir_policy[NFTA_REDIR_MAX + 1] = {
20 [NFTA_REDIR_REG_PROTO_MIN] = { .type = NLA_U32 },
21 [NFTA_REDIR_REG_PROTO_MAX] = { .type = NLA_U32 },
22 [NFTA_REDIR_FLAGS] = { .type = NLA_U32 },
23};
24EXPORT_SYMBOL_GPL(nft_redir_policy);
25
Pablo Neira Ayuso75e8d062015-01-14 15:33:57 +010026int nft_redir_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_PRE_ROUTING) |
38 (1 << NF_INET_LOCAL_OUT));
39}
40EXPORT_SYMBOL_GPL(nft_redir_validate);
41
Arturo Borreroe9105f12014-10-17 12:39:09 +020042int nft_redir_init(const struct nft_ctx *ctx,
43 const struct nft_expr *expr,
44 const struct nlattr * const tb[])
45{
46 struct nft_redir *priv = nft_expr_priv(expr);
Patrick McHardyd07db982015-04-11 02:27:30 +010047 unsigned int plen;
Arturo Borreroe9105f12014-10-17 12:39:09 +020048 int err;
49
Pablo Neira Ayuso75e8d062015-01-14 15:33:57 +010050 err = nft_redir_validate(ctx, expr, NULL);
Arturo Borreroe9105f12014-10-17 12:39:09 +020051 if (err < 0)
52 return err;
53
Patrick McHardyd07db982015-04-11 02:27:30 +010054 plen = FIELD_SIZEOF(struct nf_nat_range, min_addr.all);
Arturo Borreroe9105f12014-10-17 12:39:09 +020055 if (tb[NFTA_REDIR_REG_PROTO_MIN]) {
Pablo Neira Ayusobaf47502014-11-12 11:56:47 +010056 priv->sreg_proto_min =
Patrick McHardyb1c96ed2015-04-11 02:27:36 +010057 nft_parse_register(tb[NFTA_REDIR_REG_PROTO_MIN]);
Pablo Neira Ayusobaf47502014-11-12 11:56:47 +010058
Patrick McHardyd07db982015-04-11 02:27:30 +010059 err = nft_validate_register_load(priv->sreg_proto_min, plen);
Arturo Borreroe9105f12014-10-17 12:39:09 +020060 if (err < 0)
61 return err;
62
63 if (tb[NFTA_REDIR_REG_PROTO_MAX]) {
Pablo Neira Ayusobaf47502014-11-12 11:56:47 +010064 priv->sreg_proto_max =
Patrick McHardyb1c96ed2015-04-11 02:27:36 +010065 nft_parse_register(tb[NFTA_REDIR_REG_PROTO_MAX]);
Pablo Neira Ayusobaf47502014-11-12 11:56:47 +010066
Patrick McHardyd07db982015-04-11 02:27:30 +010067 err = nft_validate_register_load(priv->sreg_proto_max,
68 plen);
Arturo Borreroe9105f12014-10-17 12:39:09 +020069 if (err < 0)
70 return err;
71 } else {
72 priv->sreg_proto_max = priv->sreg_proto_min;
73 }
74 }
75
76 if (tb[NFTA_REDIR_FLAGS]) {
77 priv->flags = ntohl(nla_get_be32(tb[NFTA_REDIR_FLAGS]));
78 if (priv->flags & ~NF_NAT_RANGE_MASK)
79 return -EINVAL;
80 }
81
82 return 0;
83}
84EXPORT_SYMBOL_GPL(nft_redir_init);
85
86int nft_redir_dump(struct sk_buff *skb, const struct nft_expr *expr)
87{
88 const struct nft_redir *priv = nft_expr_priv(expr);
89
90 if (priv->sreg_proto_min) {
Patrick McHardyb1c96ed2015-04-11 02:27:36 +010091 if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MIN,
92 priv->sreg_proto_min))
Arturo Borreroe9105f12014-10-17 12:39:09 +020093 goto nla_put_failure;
Patrick McHardyb1c96ed2015-04-11 02:27:36 +010094 if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MAX,
95 priv->sreg_proto_max))
Arturo Borreroe9105f12014-10-17 12:39:09 +020096 goto nla_put_failure;
97 }
98
99 if (priv->flags != 0 &&
100 nla_put_be32(skb, NFTA_REDIR_FLAGS, htonl(priv->flags)))
101 goto nla_put_failure;
102
103 return 0;
104
105nla_put_failure:
106 return -1;
107}
108EXPORT_SYMBOL_GPL(nft_redir_dump);
109
Arturo Borreroe9105f12014-10-17 12:39:09 +0200110MODULE_LICENSE("GPL");
111MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>");