1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
4 *
5 * Development of this code funded by Astaro AG (http://www.astaro.com/)
6 */
7
8 #include <asm/unaligned.h>
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_tables_core.h>
16 #include <net/netfilter/nf_tables.h>
17
18 struct nft_byteorder {
19 u8 sreg;
20 u8 dreg;
21 enum nft_byteorder_ops op:8;
22 u8 len;
23 u8 size;
24 };
25
nft_byteorder_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)26 void nft_byteorder_eval(const struct nft_expr *expr,
27 struct nft_regs *regs,
28 const struct nft_pktinfo *pkt)
29 {
30 const struct nft_byteorder *priv = nft_expr_priv(expr);
31 u32 *src = ®s->data[priv->sreg];
32 u32 *dst = ®s->data[priv->dreg];
33 union { u32 u32; u16 u16; } *s, *d;
34 unsigned int i;
35
36 s = (void *)src;
37 d = (void *)dst;
38
39 switch (priv->size) {
40 case 8: {
41 u64 src64;
42
43 switch (priv->op) {
44 case NFT_BYTEORDER_NTOH:
45 for (i = 0; i < priv->len / 8; i++) {
46 src64 = nft_reg_load64(&src[i]);
47 nft_reg_store64(&dst[i],
48 be64_to_cpu((__force __be64)src64));
49 }
50 break;
51 case NFT_BYTEORDER_HTON:
52 for (i = 0; i < priv->len / 8; i++) {
53 src64 = (__force __u64)
54 cpu_to_be64(nft_reg_load64(&src[i]));
55 nft_reg_store64(&dst[i], src64);
56 }
57 break;
58 }
59 break;
60 }
61 case 4:
62 switch (priv->op) {
63 case NFT_BYTEORDER_NTOH:
64 for (i = 0; i < priv->len / 4; i++)
65 d[i].u32 = ntohl((__force __be32)s[i].u32);
66 break;
67 case NFT_BYTEORDER_HTON:
68 for (i = 0; i < priv->len / 4; i++)
69 d[i].u32 = (__force __u32)htonl(s[i].u32);
70 break;
71 }
72 break;
73 case 2:
74 switch (priv->op) {
75 case NFT_BYTEORDER_NTOH:
76 for (i = 0; i < priv->len / 2; i++)
77 d[i].u16 = ntohs((__force __be16)s[i].u16);
78 break;
79 case NFT_BYTEORDER_HTON:
80 for (i = 0; i < priv->len / 2; i++)
81 d[i].u16 = (__force __u16)htons(s[i].u16);
82 break;
83 }
84 break;
85 }
86 }
87
88 static const struct nla_policy nft_byteorder_policy[NFTA_BYTEORDER_MAX + 1] = {
89 [NFTA_BYTEORDER_SREG] = { .type = NLA_U32 },
90 [NFTA_BYTEORDER_DREG] = { .type = NLA_U32 },
91 [NFTA_BYTEORDER_OP] = { .type = NLA_U32 },
92 [NFTA_BYTEORDER_LEN] = { .type = NLA_U32 },
93 [NFTA_BYTEORDER_SIZE] = { .type = NLA_U32 },
94 };
95
nft_byteorder_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])96 static int nft_byteorder_init(const struct nft_ctx *ctx,
97 const struct nft_expr *expr,
98 const struct nlattr * const tb[])
99 {
100 struct nft_byteorder *priv = nft_expr_priv(expr);
101 u32 size, len;
102 int err;
103
104 if (tb[NFTA_BYTEORDER_SREG] == NULL ||
105 tb[NFTA_BYTEORDER_DREG] == NULL ||
106 tb[NFTA_BYTEORDER_LEN] == NULL ||
107 tb[NFTA_BYTEORDER_SIZE] == NULL ||
108 tb[NFTA_BYTEORDER_OP] == NULL)
109 return -EINVAL;
110
111 priv->op = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_OP]));
112 switch (priv->op) {
113 case NFT_BYTEORDER_NTOH:
114 case NFT_BYTEORDER_HTON:
115 break;
116 default:
117 return -EINVAL;
118 }
119
120 err = nft_parse_u32_check(tb[NFTA_BYTEORDER_SIZE], U8_MAX, &size);
121 if (err < 0)
122 return err;
123
124 priv->size = size;
125
126 switch (priv->size) {
127 case 2:
128 case 4:
129 case 8:
130 break;
131 default:
132 return -EINVAL;
133 }
134
135 err = nft_parse_u32_check(tb[NFTA_BYTEORDER_LEN], U8_MAX, &len);
136 if (err < 0)
137 return err;
138
139 priv->len = len;
140
141 err = nft_parse_register_load(tb[NFTA_BYTEORDER_SREG], &priv->sreg,
142 priv->len);
143 if (err < 0)
144 return err;
145
146 return nft_parse_register_store(ctx, tb[NFTA_BYTEORDER_DREG],
147 &priv->dreg, NULL, NFT_DATA_VALUE,
148 priv->len);
149 }
150
nft_byteorder_dump(struct sk_buff * skb,const struct nft_expr * expr,bool reset)151 static int nft_byteorder_dump(struct sk_buff *skb,
152 const struct nft_expr *expr, bool reset)
153 {
154 const struct nft_byteorder *priv = nft_expr_priv(expr);
155
156 if (nft_dump_register(skb, NFTA_BYTEORDER_SREG, priv->sreg))
157 goto nla_put_failure;
158 if (nft_dump_register(skb, NFTA_BYTEORDER_DREG, priv->dreg))
159 goto nla_put_failure;
160 if (nla_put_be32(skb, NFTA_BYTEORDER_OP, htonl(priv->op)))
161 goto nla_put_failure;
162 if (nla_put_be32(skb, NFTA_BYTEORDER_LEN, htonl(priv->len)))
163 goto nla_put_failure;
164 if (nla_put_be32(skb, NFTA_BYTEORDER_SIZE, htonl(priv->size)))
165 goto nla_put_failure;
166 return 0;
167
168 nla_put_failure:
169 return -1;
170 }
171
nft_byteorder_reduce(struct nft_regs_track * track,const struct nft_expr * expr)172 static bool nft_byteorder_reduce(struct nft_regs_track *track,
173 const struct nft_expr *expr)
174 {
175 struct nft_byteorder *priv = nft_expr_priv(expr);
176
177 nft_reg_track_cancel(track, priv->dreg, priv->len);
178
179 return false;
180 }
181
182 static const struct nft_expr_ops nft_byteorder_ops = {
183 .type = &nft_byteorder_type,
184 .size = NFT_EXPR_SIZE(sizeof(struct nft_byteorder)),
185 .eval = nft_byteorder_eval,
186 .init = nft_byteorder_init,
187 .dump = nft_byteorder_dump,
188 .reduce = nft_byteorder_reduce,
189 };
190
191 struct nft_expr_type nft_byteorder_type __read_mostly = {
192 .name = "byteorder",
193 .ops = &nft_byteorder_ops,
194 .policy = nft_byteorder_policy,
195 .maxattr = NFTA_BYTEORDER_MAX,
196 .owner = THIS_MODULE,
197 };
198