net: Move napi polling code out of net_rx_action
[cascardo/linux.git] / net / netfilter / nft_redir.c
1 /*
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
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 },
23 };
24 EXPORT_SYMBOL_GPL(nft_redir_policy);
25
26 int nft_redir_init(const struct nft_ctx *ctx,
27                    const struct nft_expr *expr,
28                    const struct nlattr * const tb[])
29 {
30         struct nft_redir *priv = nft_expr_priv(expr);
31         int err;
32
33         err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT);
34         if (err < 0)
35                 return err;
36
37         if (tb[NFTA_REDIR_REG_PROTO_MIN]) {
38                 priv->sreg_proto_min =
39                         ntohl(nla_get_be32(tb[NFTA_REDIR_REG_PROTO_MIN]));
40
41                 err = nft_validate_input_register(priv->sreg_proto_min);
42                 if (err < 0)
43                         return err;
44
45                 if (tb[NFTA_REDIR_REG_PROTO_MAX]) {
46                         priv->sreg_proto_max =
47                                 ntohl(nla_get_be32(tb[NFTA_REDIR_REG_PROTO_MAX]));
48
49                         err = nft_validate_input_register(priv->sreg_proto_max);
50                         if (err < 0)
51                                 return err;
52                 } else {
53                         priv->sreg_proto_max = priv->sreg_proto_min;
54                 }
55         }
56
57         if (tb[NFTA_REDIR_FLAGS]) {
58                 priv->flags = ntohl(nla_get_be32(tb[NFTA_REDIR_FLAGS]));
59                 if (priv->flags & ~NF_NAT_RANGE_MASK)
60                         return -EINVAL;
61         }
62
63         return 0;
64 }
65 EXPORT_SYMBOL_GPL(nft_redir_init);
66
67 int nft_redir_dump(struct sk_buff *skb, const struct nft_expr *expr)
68 {
69         const struct nft_redir *priv = nft_expr_priv(expr);
70
71         if (priv->sreg_proto_min) {
72                 if (nla_put_be32(skb, NFTA_REDIR_REG_PROTO_MIN,
73                                  htonl(priv->sreg_proto_min)))
74                         goto nla_put_failure;
75                 if (nla_put_be32(skb, NFTA_REDIR_REG_PROTO_MAX,
76                                  htonl(priv->sreg_proto_max)))
77                         goto nla_put_failure;
78         }
79
80         if (priv->flags != 0 &&
81             nla_put_be32(skb, NFTA_REDIR_FLAGS, htonl(priv->flags)))
82                         goto nla_put_failure;
83
84         return 0;
85
86 nla_put_failure:
87         return -1;
88 }
89 EXPORT_SYMBOL_GPL(nft_redir_dump);
90
91 int nft_redir_validate(const struct nft_ctx *ctx, const struct nft_expr *expr,
92                        const struct nft_data **data)
93 {
94         return nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT);
95 }
96 EXPORT_SYMBOL_GPL(nft_redir_validate);
97
98 MODULE_LICENSE("GPL");
99 MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>");