]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/netfilter/nft_redir.c
Merge tag 'armsoc-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[mirror_ubuntu-artful-kernel.git] / net / netfilter / nft_redir.c
CommitLineData
e9105f1b 1/*
cd727514 2 * Copyright (c) 2014 Arturo Borrero Gonzalez <arturo@debian.org>
e9105f1b
AB
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
75e8d06d
PNA
26int 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
e9105f1b
AB
42int 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);
d07db988 47 unsigned int plen;
e9105f1b
AB
48 int err;
49
d07db988 50 plen = FIELD_SIZEOF(struct nf_nat_range, min_addr.all);
e9105f1b 51 if (tb[NFTA_REDIR_REG_PROTO_MIN]) {
baf4750d 52 priv->sreg_proto_min =
b1c96ed3 53 nft_parse_register(tb[NFTA_REDIR_REG_PROTO_MIN]);
baf4750d 54
d07db988 55 err = nft_validate_register_load(priv->sreg_proto_min, plen);
e9105f1b
AB
56 if (err < 0)
57 return err;
58
59 if (tb[NFTA_REDIR_REG_PROTO_MAX]) {
baf4750d 60 priv->sreg_proto_max =
b1c96ed3 61 nft_parse_register(tb[NFTA_REDIR_REG_PROTO_MAX]);
baf4750d 62
d07db988
PM
63 err = nft_validate_register_load(priv->sreg_proto_max,
64 plen);
e9105f1b
AB
65 if (err < 0)
66 return err;
67 } else {
68 priv->sreg_proto_max = priv->sreg_proto_min;
69 }
70 }
71
72 if (tb[NFTA_REDIR_FLAGS]) {
73 priv->flags = ntohl(nla_get_be32(tb[NFTA_REDIR_FLAGS]));
74 if (priv->flags & ~NF_NAT_RANGE_MASK)
75 return -EINVAL;
76 }
77
20afd423 78 return nf_ct_netns_get(ctx->net, ctx->afi->family);
e9105f1b
AB
79}
80EXPORT_SYMBOL_GPL(nft_redir_init);
81
82int nft_redir_dump(struct sk_buff *skb, const struct nft_expr *expr)
83{
84 const struct nft_redir *priv = nft_expr_priv(expr);
85
86 if (priv->sreg_proto_min) {
b1c96ed3
PM
87 if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MIN,
88 priv->sreg_proto_min))
e9105f1b 89 goto nla_put_failure;
b1c96ed3
PM
90 if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MAX,
91 priv->sreg_proto_max))
e9105f1b
AB
92 goto nla_put_failure;
93 }
94
95 if (priv->flags != 0 &&
96 nla_put_be32(skb, NFTA_REDIR_FLAGS, htonl(priv->flags)))
97 goto nla_put_failure;
98
99 return 0;
100
101nla_put_failure:
102 return -1;
103}
104EXPORT_SYMBOL_GPL(nft_redir_dump);
105
e9105f1b 106MODULE_LICENSE("GPL");
cd727514 107MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo@debian.org>");