netfilter: nf_tables: introduce nft_validate_register_load()
[deliverable/linux.git] / net / netfilter / nft_byteorder.c
1 /*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
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 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/netlink.h>
15 #include <linux/netfilter.h>
16 #include <linux/netfilter/nf_tables.h>
17 #include <net/netfilter/nf_tables_core.h>
18 #include <net/netfilter/nf_tables.h>
19
20 struct nft_byteorder {
21 enum nft_registers sreg:8;
22 enum nft_registers dreg:8;
23 enum nft_byteorder_ops op:8;
24 u8 len;
25 u8 size;
26 };
27
28 static void nft_byteorder_eval(const struct nft_expr *expr,
29 struct nft_data data[NFT_REG_MAX + 1],
30 const struct nft_pktinfo *pkt)
31 {
32 const struct nft_byteorder *priv = nft_expr_priv(expr);
33 struct nft_data *src = &data[priv->sreg], *dst = &data[priv->dreg];
34 union { u32 u32; u16 u16; } *s, *d;
35 unsigned int i;
36
37 s = (void *)src->data;
38 d = (void *)dst->data;
39
40 switch (priv->size) {
41 case 4:
42 switch (priv->op) {
43 case NFT_BYTEORDER_NTOH:
44 for (i = 0; i < priv->len / 4; i++)
45 d[i].u32 = ntohl((__force __be32)s[i].u32);
46 break;
47 case NFT_BYTEORDER_HTON:
48 for (i = 0; i < priv->len / 4; i++)
49 d[i].u32 = (__force __u32)htonl(s[i].u32);
50 break;
51 }
52 break;
53 case 2:
54 switch (priv->op) {
55 case NFT_BYTEORDER_NTOH:
56 for (i = 0; i < priv->len / 2; i++)
57 d[i].u16 = ntohs((__force __be16)s[i].u16);
58 break;
59 case NFT_BYTEORDER_HTON:
60 for (i = 0; i < priv->len / 2; i++)
61 d[i].u16 = (__force __u16)htons(s[i].u16);
62 break;
63 }
64 break;
65 }
66 }
67
68 static const struct nla_policy nft_byteorder_policy[NFTA_BYTEORDER_MAX + 1] = {
69 [NFTA_BYTEORDER_SREG] = { .type = NLA_U32 },
70 [NFTA_BYTEORDER_DREG] = { .type = NLA_U32 },
71 [NFTA_BYTEORDER_OP] = { .type = NLA_U32 },
72 [NFTA_BYTEORDER_LEN] = { .type = NLA_U32 },
73 [NFTA_BYTEORDER_SIZE] = { .type = NLA_U32 },
74 };
75
76 static int nft_byteorder_init(const struct nft_ctx *ctx,
77 const struct nft_expr *expr,
78 const struct nlattr * const tb[])
79 {
80 struct nft_byteorder *priv = nft_expr_priv(expr);
81 int err;
82
83 if (tb[NFTA_BYTEORDER_SREG] == NULL ||
84 tb[NFTA_BYTEORDER_DREG] == NULL ||
85 tb[NFTA_BYTEORDER_LEN] == NULL ||
86 tb[NFTA_BYTEORDER_SIZE] == NULL ||
87 tb[NFTA_BYTEORDER_OP] == NULL)
88 return -EINVAL;
89
90 priv->op = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_OP]));
91 switch (priv->op) {
92 case NFT_BYTEORDER_NTOH:
93 case NFT_BYTEORDER_HTON:
94 break;
95 default:
96 return -EINVAL;
97 }
98
99 priv->size = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_SIZE]));
100 switch (priv->size) {
101 case 2:
102 case 4:
103 break;
104 default:
105 return -EINVAL;
106 }
107
108 priv->sreg = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_SREG]));
109 priv->len = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_LEN]));
110 err = nft_validate_register_load(priv->sreg, priv->len);
111 if (err < 0)
112 return err;
113
114 priv->dreg = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_DREG]));
115 return nft_validate_register_store(ctx, priv->dreg, NULL,
116 NFT_DATA_VALUE, priv->len);
117 }
118
119 static int nft_byteorder_dump(struct sk_buff *skb, const struct nft_expr *expr)
120 {
121 const struct nft_byteorder *priv = nft_expr_priv(expr);
122
123 if (nla_put_be32(skb, NFTA_BYTEORDER_SREG, htonl(priv->sreg)))
124 goto nla_put_failure;
125 if (nla_put_be32(skb, NFTA_BYTEORDER_DREG, htonl(priv->dreg)))
126 goto nla_put_failure;
127 if (nla_put_be32(skb, NFTA_BYTEORDER_OP, htonl(priv->op)))
128 goto nla_put_failure;
129 if (nla_put_be32(skb, NFTA_BYTEORDER_LEN, htonl(priv->len)))
130 goto nla_put_failure;
131 if (nla_put_be32(skb, NFTA_BYTEORDER_SIZE, htonl(priv->size)))
132 goto nla_put_failure;
133 return 0;
134
135 nla_put_failure:
136 return -1;
137 }
138
139 static struct nft_expr_type nft_byteorder_type;
140 static const struct nft_expr_ops nft_byteorder_ops = {
141 .type = &nft_byteorder_type,
142 .size = NFT_EXPR_SIZE(sizeof(struct nft_byteorder)),
143 .eval = nft_byteorder_eval,
144 .init = nft_byteorder_init,
145 .dump = nft_byteorder_dump,
146 };
147
148 static struct nft_expr_type nft_byteorder_type __read_mostly = {
149 .name = "byteorder",
150 .ops = &nft_byteorder_ops,
151 .policy = nft_byteorder_policy,
152 .maxattr = NFTA_BYTEORDER_MAX,
153 .owner = THIS_MODULE,
154 };
155
156 int __init nft_byteorder_module_init(void)
157 {
158 return nft_register_expr(&nft_byteorder_type);
159 }
160
161 void nft_byteorder_module_exit(void)
162 {
163 nft_unregister_expr(&nft_byteorder_type);
164 }
This page took 0.051707 seconds and 5 git commands to generate.