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)151 static int nft_byteorder_dump(struct sk_buff *skb, const struct nft_expr *expr)
152 {
153 const struct nft_byteorder *priv = nft_expr_priv(expr);
154
155 if (nft_dump_register(skb, NFTA_BYTEORDER_SREG, priv->sreg))
156 goto nla_put_failure;
157 if (nft_dump_register(skb, NFTA_BYTEORDER_DREG, priv->dreg))
158 goto nla_put_failure;
159 if (nla_put_be32(skb, NFTA_BYTEORDER_OP, htonl(priv->op)))
160 goto nla_put_failure;
161 if (nla_put_be32(skb, NFTA_BYTEORDER_LEN, htonl(priv->len)))
162 goto nla_put_failure;
163 if (nla_put_be32(skb, NFTA_BYTEORDER_SIZE, htonl(priv->size)))
164 goto nla_put_failure;
165 return 0;
166
167 nla_put_failure:
168 return -1;
169 }
170
nft_byteorder_reduce(struct nft_regs_track * track,const struct nft_expr * expr)171 static bool nft_byteorder_reduce(struct nft_regs_track *track,
172 const struct nft_expr *expr)
173 {
174 struct nft_byteorder *priv = nft_expr_priv(expr);
175
176 nft_reg_track_cancel(track, priv->dreg, priv->len);
177
178 return false;
179 }
180
181 static const struct nft_expr_ops nft_byteorder_ops = {
182 .type = &nft_byteorder_type,
183 .size = NFT_EXPR_SIZE(sizeof(struct nft_byteorder)),
184 .eval = nft_byteorder_eval,
185 .init = nft_byteorder_init,
186 .dump = nft_byteorder_dump,
187 .reduce = nft_byteorder_reduce,
188 };
189
190 struct nft_expr_type nft_byteorder_type __read_mostly = {
191 .name = "byteorder",
192 .ops = &nft_byteorder_ops,
193 .policy = nft_byteorder_policy,
194 .maxattr = NFTA_BYTEORDER_MAX,
195 .owner = THIS_MODULE,
196 };
197