Linux 4.11-rc6
[linux/fpc-iii.git] / net / netfilter / nft_redir.c
blob40dcd05146d5fb0f346e6e170d16de9813e92804
1 /*
2 * Copyright (c) 2014 Arturo Borrero Gonzalez <arturo@debian.org>
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 */
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>
19 const 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 },
24 EXPORT_SYMBOL_GPL(nft_redir_policy);
26 int nft_redir_validate(const struct nft_ctx *ctx,
27 const struct nft_expr *expr,
28 const struct nft_data **data)
30 int err;
32 err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT);
33 if (err < 0)
34 return err;
36 return nft_chain_validate_hooks(ctx->chain,
37 (1 << NF_INET_PRE_ROUTING) |
38 (1 << NF_INET_LOCAL_OUT));
40 EXPORT_SYMBOL_GPL(nft_redir_validate);
42 int nft_redir_init(const struct nft_ctx *ctx,
43 const struct nft_expr *expr,
44 const struct nlattr * const tb[])
46 struct nft_redir *priv = nft_expr_priv(expr);
47 unsigned int plen;
48 int err;
50 err = nft_redir_validate(ctx, expr, NULL);
51 if (err < 0)
52 return err;
54 plen = FIELD_SIZEOF(struct nf_nat_range, min_addr.all);
55 if (tb[NFTA_REDIR_REG_PROTO_MIN]) {
56 priv->sreg_proto_min =
57 nft_parse_register(tb[NFTA_REDIR_REG_PROTO_MIN]);
59 err = nft_validate_register_load(priv->sreg_proto_min, plen);
60 if (err < 0)
61 return err;
63 if (tb[NFTA_REDIR_REG_PROTO_MAX]) {
64 priv->sreg_proto_max =
65 nft_parse_register(tb[NFTA_REDIR_REG_PROTO_MAX]);
67 err = nft_validate_register_load(priv->sreg_proto_max,
68 plen);
69 if (err < 0)
70 return err;
71 } else {
72 priv->sreg_proto_max = priv->sreg_proto_min;
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;
82 return nf_ct_netns_get(ctx->net, ctx->afi->family);
84 EXPORT_SYMBOL_GPL(nft_redir_init);
86 int nft_redir_dump(struct sk_buff *skb, const struct nft_expr *expr)
88 const struct nft_redir *priv = nft_expr_priv(expr);
90 if (priv->sreg_proto_min) {
91 if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MIN,
92 priv->sreg_proto_min))
93 goto nla_put_failure;
94 if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MAX,
95 priv->sreg_proto_max))
96 goto nla_put_failure;
99 if (priv->flags != 0 &&
100 nla_put_be32(skb, NFTA_REDIR_FLAGS, htonl(priv->flags)))
101 goto nla_put_failure;
103 return 0;
105 nla_put_failure:
106 return -1;
108 EXPORT_SYMBOL_GPL(nft_redir_dump);
110 MODULE_LICENSE("GPL");
111 MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo@debian.org>");