xref: /openbmc/linux/net/netfilter/nft_redir.c (revision d894fc60)
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_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 }
40 EXPORT_SYMBOL_GPL(nft_redir_validate);
41 
42 int 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);
47 	int err;
48 
49 	err = nft_redir_validate(ctx, expr, NULL);
50 	if (err < 0)
51 		return err;
52 
53 	if (tb[NFTA_REDIR_REG_PROTO_MIN]) {
54 		priv->sreg_proto_min =
55 			ntohl(nla_get_be32(tb[NFTA_REDIR_REG_PROTO_MIN]));
56 
57 		err = nft_validate_input_register(priv->sreg_proto_min);
58 		if (err < 0)
59 			return err;
60 
61 		if (tb[NFTA_REDIR_REG_PROTO_MAX]) {
62 			priv->sreg_proto_max =
63 				ntohl(nla_get_be32(tb[NFTA_REDIR_REG_PROTO_MAX]));
64 
65 			err = nft_validate_input_register(priv->sreg_proto_max);
66 			if (err < 0)
67 				return err;
68 		} else {
69 			priv->sreg_proto_max = priv->sreg_proto_min;
70 		}
71 	}
72 
73 	if (tb[NFTA_REDIR_FLAGS]) {
74 		priv->flags = ntohl(nla_get_be32(tb[NFTA_REDIR_FLAGS]));
75 		if (priv->flags & ~NF_NAT_RANGE_MASK)
76 			return -EINVAL;
77 	}
78 
79 	return 0;
80 }
81 EXPORT_SYMBOL_GPL(nft_redir_init);
82 
83 int nft_redir_dump(struct sk_buff *skb, const struct nft_expr *expr)
84 {
85 	const struct nft_redir *priv = nft_expr_priv(expr);
86 
87 	if (priv->sreg_proto_min) {
88 		if (nla_put_be32(skb, NFTA_REDIR_REG_PROTO_MIN,
89 				 htonl(priv->sreg_proto_min)))
90 			goto nla_put_failure;
91 		if (nla_put_be32(skb, NFTA_REDIR_REG_PROTO_MAX,
92 				 htonl(priv->sreg_proto_max)))
93 			goto nla_put_failure;
94 	}
95 
96 	if (priv->flags != 0 &&
97 	    nla_put_be32(skb, NFTA_REDIR_FLAGS, htonl(priv->flags)))
98 			goto nla_put_failure;
99 
100 	return 0;
101 
102 nla_put_failure:
103 	return -1;
104 }
105 EXPORT_SYMBOL_GPL(nft_redir_dump);
106 
107 MODULE_LICENSE("GPL");
108 MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>");
109