]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
96518518 | 2 | /* |
ef1f7df9 | 3 | * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net> |
18140969 | 4 | * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org> |
96518518 | 5 | * |
96518518 PM |
6 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
7 | */ | |
8 | ||
9 | #include <linux/kernel.h> | |
8cfd23e6 | 10 | #include <linux/if_vlan.h> |
96518518 PM |
11 | #include <linux/init.h> |
12 | #include <linux/module.h> | |
13 | #include <linux/netlink.h> | |
14 | #include <linux/netfilter.h> | |
15 | #include <linux/netfilter/nf_tables.h> | |
16 | #include <net/netfilter/nf_tables_core.h> | |
17 | #include <net/netfilter/nf_tables.h> | |
c9626a2c | 18 | #include <net/netfilter/nf_tables_offload.h> |
18140969 PNA |
19 | /* For layer 4 checksum field offset. */ |
20 | #include <linux/tcp.h> | |
21 | #include <linux/udp.h> | |
22 | #include <linux/icmpv6.h> | |
c9626a2c PNA |
23 | #include <linux/ip.h> |
24 | #include <linux/ipv6.h> | |
346e320c | 25 | #include <net/sctp/checksum.h> |
96518518 | 26 | |
8dfd8b09 PNA |
27 | static bool nft_payload_rebuild_vlan_hdr(const struct sk_buff *skb, int mac_off, |
28 | struct vlan_ethhdr *veth) | |
29 | { | |
30 | if (skb_copy_bits(skb, mac_off, veth, ETH_HLEN)) | |
31 | return false; | |
32 | ||
33 | veth->h_vlan_proto = skb->vlan_proto; | |
34 | veth->h_vlan_TCI = htons(skb_vlan_tag_get(skb)); | |
35 | veth->h_vlan_encapsulated_proto = skb->protocol; | |
36 | ||
37 | return true; | |
38 | } | |
39 | ||
8cfd23e6 FW |
40 | /* add vlan header into the user buffer for if tag was removed by offloads */ |
41 | static bool | |
42 | nft_payload_copy_vlan(u32 *d, const struct sk_buff *skb, u8 offset, u8 len) | |
43 | { | |
44 | int mac_off = skb_mac_header(skb) - skb->data; | |
f41f72d0 | 45 | u8 *vlanh, *dst_u8 = (u8 *) d; |
8cfd23e6 | 46 | struct vlan_ethhdr veth; |
f6ae9f12 PNA |
47 | u8 vlan_hlen = 0; |
48 | ||
49 | if ((skb->protocol == htons(ETH_P_8021AD) || | |
50 | skb->protocol == htons(ETH_P_8021Q)) && | |
51 | offset >= VLAN_ETH_HLEN && offset < VLAN_ETH_HLEN + VLAN_HLEN) | |
52 | vlan_hlen += VLAN_HLEN; | |
8cfd23e6 FW |
53 | |
54 | vlanh = (u8 *) &veth; | |
f6ae9f12 | 55 | if (offset < VLAN_ETH_HLEN + vlan_hlen) { |
f41f72d0 | 56 | u8 ethlen = len; |
8cfd23e6 | 57 | |
f6ae9f12 PNA |
58 | if (vlan_hlen && |
59 | skb_copy_bits(skb, mac_off, &veth, VLAN_ETH_HLEN) < 0) | |
60 | return false; | |
61 | else if (!nft_payload_rebuild_vlan_hdr(skb, mac_off, &veth)) | |
8cfd23e6 FW |
62 | return false; |
63 | ||
f6ae9f12 PNA |
64 | if (offset + len > VLAN_ETH_HLEN + vlan_hlen) |
65 | ethlen -= offset + len - VLAN_ETH_HLEN + vlan_hlen; | |
8cfd23e6 | 66 | |
f6ae9f12 | 67 | memcpy(dst_u8, vlanh + offset - vlan_hlen, ethlen); |
8cfd23e6 FW |
68 | |
69 | len -= ethlen; | |
70 | if (len == 0) | |
71 | return true; | |
72 | ||
73 | dst_u8 += ethlen; | |
f6ae9f12 | 74 | offset = ETH_HLEN + vlan_hlen; |
f41f72d0 | 75 | } else { |
f6ae9f12 | 76 | offset -= VLAN_HLEN + vlan_hlen; |
8cfd23e6 FW |
77 | } |
78 | ||
8cfd23e6 FW |
79 | return skb_copy_bits(skb, offset + mac_off, dst_u8, len) == 0; |
80 | } | |
81 | ||
10870dd8 FW |
82 | void nft_payload_eval(const struct nft_expr *expr, |
83 | struct nft_regs *regs, | |
84 | const struct nft_pktinfo *pkt) | |
96518518 PM |
85 | { |
86 | const struct nft_payload *priv = nft_expr_priv(expr); | |
87 | const struct sk_buff *skb = pkt->skb; | |
49499c3e | 88 | u32 *dest = ®s->data[priv->dreg]; |
96518518 PM |
89 | int offset; |
90 | ||
1e105e6a FW |
91 | if (priv->len % NFT_REG32_SIZE) |
92 | dest[priv->len / NFT_REG32_SIZE] = 0; | |
93 | ||
96518518 PM |
94 | switch (priv->base) { |
95 | case NFT_PAYLOAD_LL_HEADER: | |
96 | if (!skb_mac_header_was_set(skb)) | |
97 | goto err; | |
8cfd23e6 FW |
98 | |
99 | if (skb_vlan_tag_present(skb)) { | |
100 | if (!nft_payload_copy_vlan(dest, skb, | |
101 | priv->offset, priv->len)) | |
102 | goto err; | |
103 | return; | |
104 | } | |
96518518 PM |
105 | offset = skb_mac_header(skb) - skb->data; |
106 | break; | |
107 | case NFT_PAYLOAD_NETWORK_HEADER: | |
108 | offset = skb_network_offset(skb); | |
109 | break; | |
110 | case NFT_PAYLOAD_TRANSPORT_HEADER: | |
a20877b5 LZ |
111 | if (!pkt->tprot_set) |
112 | goto err; | |
c54032e0 | 113 | offset = pkt->xt.thoff; |
96518518 PM |
114 | break; |
115 | default: | |
116 | BUG(); | |
117 | } | |
118 | offset += priv->offset; | |
119 | ||
fad136ea | 120 | if (skb_copy_bits(skb, offset, dest, priv->len) < 0) |
96518518 PM |
121 | goto err; |
122 | return; | |
123 | err: | |
a55e22e9 | 124 | regs->verdict.code = NFT_BREAK; |
96518518 PM |
125 | } |
126 | ||
127 | static const struct nla_policy nft_payload_policy[NFTA_PAYLOAD_MAX + 1] = { | |
7ec3f7b4 PM |
128 | [NFTA_PAYLOAD_SREG] = { .type = NLA_U32 }, |
129 | [NFTA_PAYLOAD_DREG] = { .type = NLA_U32 }, | |
130 | [NFTA_PAYLOAD_BASE] = { .type = NLA_U32 }, | |
131 | [NFTA_PAYLOAD_OFFSET] = { .type = NLA_U32 }, | |
132 | [NFTA_PAYLOAD_LEN] = { .type = NLA_U32 }, | |
133 | [NFTA_PAYLOAD_CSUM_TYPE] = { .type = NLA_U32 }, | |
134 | [NFTA_PAYLOAD_CSUM_OFFSET] = { .type = NLA_U32 }, | |
9d6effb2 | 135 | [NFTA_PAYLOAD_CSUM_FLAGS] = { .type = NLA_U32 }, |
96518518 PM |
136 | }; |
137 | ||
138 | static int nft_payload_init(const struct nft_ctx *ctx, | |
139 | const struct nft_expr *expr, | |
140 | const struct nlattr * const tb[]) | |
141 | { | |
142 | struct nft_payload *priv = nft_expr_priv(expr); | |
96518518 | 143 | |
c29b72e0 | 144 | priv->base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE])); |
96518518 PM |
145 | priv->offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET])); |
146 | priv->len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN])); | |
b1c96ed3 | 147 | priv->dreg = nft_parse_register(tb[NFTA_PAYLOAD_DREG]); |
96518518 | 148 | |
1ec10212 PM |
149 | return nft_validate_register_store(ctx, priv->dreg, NULL, |
150 | NFT_DATA_VALUE, priv->len); | |
96518518 PM |
151 | } |
152 | ||
153 | static int nft_payload_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
154 | { | |
155 | const struct nft_payload *priv = nft_expr_priv(expr); | |
156 | ||
b1c96ed3 | 157 | if (nft_dump_register(skb, NFTA_PAYLOAD_DREG, priv->dreg) || |
96518518 PM |
158 | nla_put_be32(skb, NFTA_PAYLOAD_BASE, htonl(priv->base)) || |
159 | nla_put_be32(skb, NFTA_PAYLOAD_OFFSET, htonl(priv->offset)) || | |
160 | nla_put_be32(skb, NFTA_PAYLOAD_LEN, htonl(priv->len))) | |
161 | goto nla_put_failure; | |
162 | return 0; | |
163 | ||
164 | nla_put_failure: | |
165 | return -1; | |
166 | } | |
167 | ||
c9626a2c PNA |
168 | static int nft_payload_offload_ll(struct nft_offload_ctx *ctx, |
169 | struct nft_flow_rule *flow, | |
170 | const struct nft_payload *priv) | |
171 | { | |
172 | struct nft_offload_reg *reg = &ctx->regs[priv->dreg]; | |
173 | ||
174 | switch (priv->offset) { | |
175 | case offsetof(struct ethhdr, h_source): | |
a69a85da | 176 | if (priv->len != ETH_ALEN) |
177 | return -EOPNOTSUPP; | |
178 | ||
c9626a2c PNA |
179 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_ETH_ADDRS, eth_addrs, |
180 | src, ETH_ALEN, reg); | |
181 | break; | |
182 | case offsetof(struct ethhdr, h_dest): | |
a69a85da | 183 | if (priv->len != ETH_ALEN) |
184 | return -EOPNOTSUPP; | |
185 | ||
c9626a2c PNA |
186 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_ETH_ADDRS, eth_addrs, |
187 | dst, ETH_ALEN, reg); | |
188 | break; | |
a82055af PNA |
189 | case offsetof(struct ethhdr, h_proto): |
190 | if (priv->len != sizeof(__be16)) | |
191 | return -EOPNOTSUPP; | |
192 | ||
193 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_BASIC, basic, | |
194 | n_proto, sizeof(__be16), reg); | |
195 | nft_offload_set_dependency(ctx, NFT_OFFLOAD_DEP_NETWORK); | |
196 | break; | |
197 | case offsetof(struct vlan_ethhdr, h_vlan_TCI): | |
198 | if (priv->len != sizeof(__be16)) | |
199 | return -EOPNOTSUPP; | |
200 | ||
201 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_VLAN, vlan, | |
202 | vlan_tci, sizeof(__be16), reg); | |
203 | break; | |
204 | case offsetof(struct vlan_ethhdr, h_vlan_encapsulated_proto): | |
205 | if (priv->len != sizeof(__be16)) | |
206 | return -EOPNOTSUPP; | |
207 | ||
208 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_VLAN, vlan, | |
209 | vlan_tpid, sizeof(__be16), reg); | |
89d8fd44 PNA |
210 | nft_offload_set_dependency(ctx, NFT_OFFLOAD_DEP_NETWORK); |
211 | break; | |
212 | case offsetof(struct vlan_ethhdr, h_vlan_TCI) + sizeof(struct vlan_hdr): | |
213 | if (priv->len != sizeof(__be16)) | |
214 | return -EOPNOTSUPP; | |
215 | ||
216 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_CVLAN, vlan, | |
217 | vlan_tci, sizeof(__be16), reg); | |
218 | break; | |
219 | case offsetof(struct vlan_ethhdr, h_vlan_encapsulated_proto) + | |
220 | sizeof(struct vlan_hdr): | |
221 | if (priv->len != sizeof(__be16)) | |
222 | return -EOPNOTSUPP; | |
223 | ||
224 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_CVLAN, vlan, | |
225 | vlan_tpid, sizeof(__be16), reg); | |
a82055af | 226 | break; |
a69a85da | 227 | default: |
228 | return -EOPNOTSUPP; | |
c9626a2c PNA |
229 | } |
230 | ||
231 | return 0; | |
232 | } | |
233 | ||
234 | static int nft_payload_offload_ip(struct nft_offload_ctx *ctx, | |
235 | struct nft_flow_rule *flow, | |
236 | const struct nft_payload *priv) | |
237 | { | |
238 | struct nft_offload_reg *reg = &ctx->regs[priv->dreg]; | |
239 | ||
240 | switch (priv->offset) { | |
241 | case offsetof(struct iphdr, saddr): | |
a69a85da | 242 | if (priv->len != sizeof(struct in_addr)) |
243 | return -EOPNOTSUPP; | |
244 | ||
c9626a2c PNA |
245 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4, src, |
246 | sizeof(struct in_addr), reg); | |
3c78e9e0 | 247 | nft_flow_rule_set_addr_type(flow, FLOW_DISSECTOR_KEY_IPV4_ADDRS); |
c9626a2c PNA |
248 | break; |
249 | case offsetof(struct iphdr, daddr): | |
a69a85da | 250 | if (priv->len != sizeof(struct in_addr)) |
251 | return -EOPNOTSUPP; | |
252 | ||
c9626a2c PNA |
253 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4, dst, |
254 | sizeof(struct in_addr), reg); | |
3c78e9e0 | 255 | nft_flow_rule_set_addr_type(flow, FLOW_DISSECTOR_KEY_IPV4_ADDRS); |
c9626a2c PNA |
256 | break; |
257 | case offsetof(struct iphdr, protocol): | |
a69a85da | 258 | if (priv->len != sizeof(__u8)) |
259 | return -EOPNOTSUPP; | |
260 | ||
c9626a2c PNA |
261 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_BASIC, basic, ip_proto, |
262 | sizeof(__u8), reg); | |
263 | nft_offload_set_dependency(ctx, NFT_OFFLOAD_DEP_TRANSPORT); | |
264 | break; | |
265 | default: | |
266 | return -EOPNOTSUPP; | |
267 | } | |
268 | ||
269 | return 0; | |
270 | } | |
271 | ||
272 | static int nft_payload_offload_ip6(struct nft_offload_ctx *ctx, | |
273 | struct nft_flow_rule *flow, | |
274 | const struct nft_payload *priv) | |
275 | { | |
276 | struct nft_offload_reg *reg = &ctx->regs[priv->dreg]; | |
277 | ||
278 | switch (priv->offset) { | |
279 | case offsetof(struct ipv6hdr, saddr): | |
a69a85da | 280 | if (priv->len != sizeof(struct in6_addr)) |
281 | return -EOPNOTSUPP; | |
282 | ||
c9626a2c PNA |
283 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6, src, |
284 | sizeof(struct in6_addr), reg); | |
3c78e9e0 | 285 | nft_flow_rule_set_addr_type(flow, FLOW_DISSECTOR_KEY_IPV6_ADDRS); |
c9626a2c PNA |
286 | break; |
287 | case offsetof(struct ipv6hdr, daddr): | |
a69a85da | 288 | if (priv->len != sizeof(struct in6_addr)) |
289 | return -EOPNOTSUPP; | |
290 | ||
c9626a2c PNA |
291 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6, dst, |
292 | sizeof(struct in6_addr), reg); | |
3c78e9e0 | 293 | nft_flow_rule_set_addr_type(flow, FLOW_DISSECTOR_KEY_IPV6_ADDRS); |
c9626a2c PNA |
294 | break; |
295 | case offsetof(struct ipv6hdr, nexthdr): | |
a69a85da | 296 | if (priv->len != sizeof(__u8)) |
297 | return -EOPNOTSUPP; | |
298 | ||
c9626a2c PNA |
299 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_BASIC, basic, ip_proto, |
300 | sizeof(__u8), reg); | |
301 | nft_offload_set_dependency(ctx, NFT_OFFLOAD_DEP_TRANSPORT); | |
302 | break; | |
303 | default: | |
304 | return -EOPNOTSUPP; | |
305 | } | |
306 | ||
307 | return 0; | |
308 | } | |
309 | ||
310 | static int nft_payload_offload_nh(struct nft_offload_ctx *ctx, | |
311 | struct nft_flow_rule *flow, | |
312 | const struct nft_payload *priv) | |
313 | { | |
314 | int err; | |
315 | ||
316 | switch (ctx->dep.l3num) { | |
317 | case htons(ETH_P_IP): | |
318 | err = nft_payload_offload_ip(ctx, flow, priv); | |
319 | break; | |
320 | case htons(ETH_P_IPV6): | |
321 | err = nft_payload_offload_ip6(ctx, flow, priv); | |
322 | break; | |
323 | default: | |
324 | return -EOPNOTSUPP; | |
325 | } | |
326 | ||
327 | return err; | |
328 | } | |
329 | ||
330 | static int nft_payload_offload_tcp(struct nft_offload_ctx *ctx, | |
331 | struct nft_flow_rule *flow, | |
332 | const struct nft_payload *priv) | |
333 | { | |
334 | struct nft_offload_reg *reg = &ctx->regs[priv->dreg]; | |
335 | ||
336 | switch (priv->offset) { | |
337 | case offsetof(struct tcphdr, source): | |
a69a85da | 338 | if (priv->len != sizeof(__be16)) |
339 | return -EOPNOTSUPP; | |
340 | ||
c9626a2c PNA |
341 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, src, |
342 | sizeof(__be16), reg); | |
343 | break; | |
344 | case offsetof(struct tcphdr, dest): | |
a69a85da | 345 | if (priv->len != sizeof(__be16)) |
346 | return -EOPNOTSUPP; | |
347 | ||
c9626a2c PNA |
348 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, dst, |
349 | sizeof(__be16), reg); | |
350 | break; | |
351 | default: | |
352 | return -EOPNOTSUPP; | |
353 | } | |
354 | ||
355 | return 0; | |
356 | } | |
357 | ||
358 | static int nft_payload_offload_udp(struct nft_offload_ctx *ctx, | |
359 | struct nft_flow_rule *flow, | |
360 | const struct nft_payload *priv) | |
361 | { | |
362 | struct nft_offload_reg *reg = &ctx->regs[priv->dreg]; | |
363 | ||
364 | switch (priv->offset) { | |
365 | case offsetof(struct udphdr, source): | |
a69a85da | 366 | if (priv->len != sizeof(__be16)) |
367 | return -EOPNOTSUPP; | |
368 | ||
c9626a2c PNA |
369 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, src, |
370 | sizeof(__be16), reg); | |
371 | break; | |
372 | case offsetof(struct udphdr, dest): | |
a69a85da | 373 | if (priv->len != sizeof(__be16)) |
374 | return -EOPNOTSUPP; | |
375 | ||
c9626a2c PNA |
376 | NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, dst, |
377 | sizeof(__be16), reg); | |
378 | break; | |
379 | default: | |
380 | return -EOPNOTSUPP; | |
381 | } | |
382 | ||
383 | return 0; | |
384 | } | |
385 | ||
386 | static int nft_payload_offload_th(struct nft_offload_ctx *ctx, | |
387 | struct nft_flow_rule *flow, | |
388 | const struct nft_payload *priv) | |
389 | { | |
390 | int err; | |
391 | ||
392 | switch (ctx->dep.protonum) { | |
393 | case IPPROTO_TCP: | |
394 | err = nft_payload_offload_tcp(ctx, flow, priv); | |
395 | break; | |
396 | case IPPROTO_UDP: | |
397 | err = nft_payload_offload_udp(ctx, flow, priv); | |
398 | break; | |
399 | default: | |
400 | return -EOPNOTSUPP; | |
401 | } | |
402 | ||
403 | return err; | |
404 | } | |
405 | ||
406 | static int nft_payload_offload(struct nft_offload_ctx *ctx, | |
407 | struct nft_flow_rule *flow, | |
408 | const struct nft_expr *expr) | |
409 | { | |
410 | const struct nft_payload *priv = nft_expr_priv(expr); | |
411 | int err; | |
412 | ||
413 | switch (priv->base) { | |
414 | case NFT_PAYLOAD_LL_HEADER: | |
415 | err = nft_payload_offload_ll(ctx, flow, priv); | |
416 | break; | |
417 | case NFT_PAYLOAD_NETWORK_HEADER: | |
418 | err = nft_payload_offload_nh(ctx, flow, priv); | |
419 | break; | |
420 | case NFT_PAYLOAD_TRANSPORT_HEADER: | |
421 | err = nft_payload_offload_th(ctx, flow, priv); | |
422 | break; | |
423 | default: | |
424 | err = -EOPNOTSUPP; | |
425 | break; | |
426 | } | |
427 | return err; | |
428 | } | |
429 | ||
ef1f7df9 PM |
430 | static const struct nft_expr_ops nft_payload_ops = { |
431 | .type = &nft_payload_type, | |
96518518 | 432 | .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)), |
96518518 PM |
433 | .eval = nft_payload_eval, |
434 | .init = nft_payload_init, | |
435 | .dump = nft_payload_dump, | |
c9626a2c | 436 | .offload = nft_payload_offload, |
ef1f7df9 PM |
437 | }; |
438 | ||
c29b72e0 PM |
439 | const struct nft_expr_ops nft_payload_fast_ops = { |
440 | .type = &nft_payload_type, | |
441 | .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)), | |
442 | .eval = nft_payload_eval, | |
443 | .init = nft_payload_init, | |
444 | .dump = nft_payload_dump, | |
c9626a2c | 445 | .offload = nft_payload_offload, |
c29b72e0 PM |
446 | }; |
447 | ||
18140969 PNA |
448 | static inline void nft_csum_replace(__sum16 *sum, __wsum fsum, __wsum tsum) |
449 | { | |
450 | *sum = csum_fold(csum_add(csum_sub(~csum_unfold(*sum), fsum), tsum)); | |
451 | if (*sum == 0) | |
452 | *sum = CSUM_MANGLED_0; | |
453 | } | |
454 | ||
455 | static bool nft_payload_udp_checksum(struct sk_buff *skb, unsigned int thoff) | |
456 | { | |
457 | struct udphdr *uh, _uh; | |
458 | ||
459 | uh = skb_header_pointer(skb, thoff, sizeof(_uh), &_uh); | |
460 | if (!uh) | |
461 | return false; | |
462 | ||
5fd02ebe | 463 | return (__force bool)uh->check; |
18140969 PNA |
464 | } |
465 | ||
466 | static int nft_payload_l4csum_offset(const struct nft_pktinfo *pkt, | |
467 | struct sk_buff *skb, | |
468 | unsigned int *l4csum_offset) | |
469 | { | |
470 | switch (pkt->tprot) { | |
471 | case IPPROTO_TCP: | |
472 | *l4csum_offset = offsetof(struct tcphdr, check); | |
473 | break; | |
474 | case IPPROTO_UDP: | |
475 | if (!nft_payload_udp_checksum(skb, pkt->xt.thoff)) | |
476 | return -1; | |
954d8297 | 477 | fallthrough; |
18140969 PNA |
478 | case IPPROTO_UDPLITE: |
479 | *l4csum_offset = offsetof(struct udphdr, check); | |
480 | break; | |
481 | case IPPROTO_ICMPV6: | |
482 | *l4csum_offset = offsetof(struct icmp6hdr, icmp6_cksum); | |
483 | break; | |
484 | default: | |
485 | return -1; | |
486 | } | |
487 | ||
488 | *l4csum_offset += pkt->xt.thoff; | |
489 | return 0; | |
490 | } | |
491 | ||
346e320c DC |
492 | static int nft_payload_csum_sctp(struct sk_buff *skb, int offset) |
493 | { | |
494 | struct sctphdr *sh; | |
495 | ||
496 | if (skb_ensure_writable(skb, offset + sizeof(*sh))) | |
497 | return -1; | |
498 | ||
499 | sh = (struct sctphdr *)(skb->data + offset); | |
500 | sh->checksum = sctp_compute_cksum(skb, offset); | |
501 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
502 | return 0; | |
503 | } | |
504 | ||
18140969 PNA |
505 | static int nft_payload_l4csum_update(const struct nft_pktinfo *pkt, |
506 | struct sk_buff *skb, | |
507 | __wsum fsum, __wsum tsum) | |
508 | { | |
509 | int l4csum_offset; | |
510 | __sum16 sum; | |
511 | ||
512 | /* If we cannot determine layer 4 checksum offset or this packet doesn't | |
513 | * require layer 4 checksum recalculation, skip this packet. | |
514 | */ | |
515 | if (nft_payload_l4csum_offset(pkt, skb, &l4csum_offset) < 0) | |
516 | return 0; | |
517 | ||
518 | if (skb_copy_bits(skb, l4csum_offset, &sum, sizeof(sum)) < 0) | |
519 | return -1; | |
520 | ||
521 | /* Checksum mangling for an arbitrary amount of bytes, based on | |
522 | * inet_proto_csum_replace*() functions. | |
523 | */ | |
524 | if (skb->ip_summed != CHECKSUM_PARTIAL) { | |
525 | nft_csum_replace(&sum, fsum, tsum); | |
526 | if (skb->ip_summed == CHECKSUM_COMPLETE) { | |
527 | skb->csum = ~csum_add(csum_sub(~(skb->csum), fsum), | |
528 | tsum); | |
529 | } | |
530 | } else { | |
531 | sum = ~csum_fold(csum_add(csum_sub(csum_unfold(sum), fsum), | |
532 | tsum)); | |
533 | } | |
534 | ||
7418ee4c | 535 | if (skb_ensure_writable(skb, l4csum_offset + sizeof(sum)) || |
18140969 PNA |
536 | skb_store_bits(skb, l4csum_offset, &sum, sizeof(sum)) < 0) |
537 | return -1; | |
538 | ||
539 | return 0; | |
540 | } | |
541 | ||
053d20f5 PNA |
542 | static int nft_payload_csum_inet(struct sk_buff *skb, const u32 *src, |
543 | __wsum fsum, __wsum tsum, int csum_offset) | |
544 | { | |
545 | __sum16 sum; | |
546 | ||
547 | if (skb_copy_bits(skb, csum_offset, &sum, sizeof(sum)) < 0) | |
548 | return -1; | |
549 | ||
550 | nft_csum_replace(&sum, fsum, tsum); | |
7418ee4c | 551 | if (skb_ensure_writable(skb, csum_offset + sizeof(sum)) || |
053d20f5 PNA |
552 | skb_store_bits(skb, csum_offset, &sum, sizeof(sum)) < 0) |
553 | return -1; | |
554 | ||
555 | return 0; | |
556 | } | |
557 | ||
7ec3f7b4 PM |
558 | static void nft_payload_set_eval(const struct nft_expr *expr, |
559 | struct nft_regs *regs, | |
560 | const struct nft_pktinfo *pkt) | |
561 | { | |
562 | const struct nft_payload_set *priv = nft_expr_priv(expr); | |
563 | struct sk_buff *skb = pkt->skb; | |
564 | const u32 *src = ®s->data[priv->sreg]; | |
565 | int offset, csum_offset; | |
566 | __wsum fsum, tsum; | |
7ec3f7b4 PM |
567 | |
568 | switch (priv->base) { | |
569 | case NFT_PAYLOAD_LL_HEADER: | |
570 | if (!skb_mac_header_was_set(skb)) | |
571 | goto err; | |
572 | offset = skb_mac_header(skb) - skb->data; | |
573 | break; | |
574 | case NFT_PAYLOAD_NETWORK_HEADER: | |
575 | offset = skb_network_offset(skb); | |
576 | break; | |
577 | case NFT_PAYLOAD_TRANSPORT_HEADER: | |
a20877b5 LZ |
578 | if (!pkt->tprot_set) |
579 | goto err; | |
7ec3f7b4 PM |
580 | offset = pkt->xt.thoff; |
581 | break; | |
582 | default: | |
583 | BUG(); | |
584 | } | |
585 | ||
586 | csum_offset = offset + priv->csum_offset; | |
587 | offset += priv->offset; | |
588 | ||
053d20f5 | 589 | if ((priv->csum_type == NFT_PAYLOAD_CSUM_INET || priv->csum_flags) && |
7ec3f7b4 PM |
590 | (priv->base != NFT_PAYLOAD_TRANSPORT_HEADER || |
591 | skb->ip_summed != CHECKSUM_PARTIAL)) { | |
7ec3f7b4 PM |
592 | fsum = skb_checksum(skb, offset, priv->len, 0); |
593 | tsum = csum_partial(src, priv->len, 0); | |
7ec3f7b4 | 594 | |
053d20f5 PNA |
595 | if (priv->csum_type == NFT_PAYLOAD_CSUM_INET && |
596 | nft_payload_csum_inet(skb, src, fsum, tsum, csum_offset)) | |
7ec3f7b4 | 597 | goto err; |
18140969 PNA |
598 | |
599 | if (priv->csum_flags && | |
600 | nft_payload_l4csum_update(pkt, skb, fsum, tsum) < 0) | |
601 | goto err; | |
7ec3f7b4 PM |
602 | } |
603 | ||
7418ee4c | 604 | if (skb_ensure_writable(skb, max(offset + priv->len, 0)) || |
7ec3f7b4 PM |
605 | skb_store_bits(skb, offset, src, priv->len) < 0) |
606 | goto err; | |
607 | ||
346e320c DC |
608 | if (priv->csum_type == NFT_PAYLOAD_CSUM_SCTP && |
609 | pkt->tprot == IPPROTO_SCTP && | |
610 | skb->ip_summed != CHECKSUM_PARTIAL) { | |
611 | if (nft_payload_csum_sctp(skb, pkt->xt.thoff)) | |
612 | goto err; | |
613 | } | |
614 | ||
7ec3f7b4 PM |
615 | return; |
616 | err: | |
617 | regs->verdict.code = NFT_BREAK; | |
618 | } | |
619 | ||
620 | static int nft_payload_set_init(const struct nft_ctx *ctx, | |
621 | const struct nft_expr *expr, | |
622 | const struct nlattr * const tb[]) | |
623 | { | |
624 | struct nft_payload_set *priv = nft_expr_priv(expr); | |
625 | ||
626 | priv->base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE])); | |
627 | priv->offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET])); | |
628 | priv->len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN])); | |
629 | priv->sreg = nft_parse_register(tb[NFTA_PAYLOAD_SREG]); | |
630 | ||
631 | if (tb[NFTA_PAYLOAD_CSUM_TYPE]) | |
632 | priv->csum_type = | |
633 | ntohl(nla_get_be32(tb[NFTA_PAYLOAD_CSUM_TYPE])); | |
634 | if (tb[NFTA_PAYLOAD_CSUM_OFFSET]) | |
635 | priv->csum_offset = | |
636 | ntohl(nla_get_be32(tb[NFTA_PAYLOAD_CSUM_OFFSET])); | |
18140969 PNA |
637 | if (tb[NFTA_PAYLOAD_CSUM_FLAGS]) { |
638 | u32 flags; | |
639 | ||
640 | flags = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_CSUM_FLAGS])); | |
641 | if (flags & ~NFT_PAYLOAD_L4CSUM_PSEUDOHDR) | |
642 | return -EINVAL; | |
643 | ||
644 | priv->csum_flags = flags; | |
645 | } | |
7ec3f7b4 PM |
646 | |
647 | switch (priv->csum_type) { | |
648 | case NFT_PAYLOAD_CSUM_NONE: | |
649 | case NFT_PAYLOAD_CSUM_INET: | |
650 | break; | |
346e320c DC |
651 | case NFT_PAYLOAD_CSUM_SCTP: |
652 | if (priv->base != NFT_PAYLOAD_TRANSPORT_HEADER) | |
653 | return -EINVAL; | |
654 | ||
655 | if (priv->csum_offset != offsetof(struct sctphdr, checksum)) | |
656 | return -EINVAL; | |
657 | break; | |
7ec3f7b4 PM |
658 | default: |
659 | return -EOPNOTSUPP; | |
660 | } | |
661 | ||
662 | return nft_validate_register_load(priv->sreg, priv->len); | |
663 | } | |
664 | ||
665 | static int nft_payload_set_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
666 | { | |
667 | const struct nft_payload_set *priv = nft_expr_priv(expr); | |
668 | ||
669 | if (nft_dump_register(skb, NFTA_PAYLOAD_SREG, priv->sreg) || | |
670 | nla_put_be32(skb, NFTA_PAYLOAD_BASE, htonl(priv->base)) || | |
671 | nla_put_be32(skb, NFTA_PAYLOAD_OFFSET, htonl(priv->offset)) || | |
672 | nla_put_be32(skb, NFTA_PAYLOAD_LEN, htonl(priv->len)) || | |
673 | nla_put_be32(skb, NFTA_PAYLOAD_CSUM_TYPE, htonl(priv->csum_type)) || | |
674 | nla_put_be32(skb, NFTA_PAYLOAD_CSUM_OFFSET, | |
18140969 PNA |
675 | htonl(priv->csum_offset)) || |
676 | nla_put_be32(skb, NFTA_PAYLOAD_CSUM_FLAGS, htonl(priv->csum_flags))) | |
7ec3f7b4 PM |
677 | goto nla_put_failure; |
678 | return 0; | |
679 | ||
680 | nla_put_failure: | |
681 | return -1; | |
682 | } | |
683 | ||
684 | static const struct nft_expr_ops nft_payload_set_ops = { | |
685 | .type = &nft_payload_type, | |
686 | .size = NFT_EXPR_SIZE(sizeof(struct nft_payload_set)), | |
687 | .eval = nft_payload_set_eval, | |
688 | .init = nft_payload_set_init, | |
689 | .dump = nft_payload_set_dump, | |
690 | }; | |
691 | ||
0ca743a5 PNA |
692 | static const struct nft_expr_ops * |
693 | nft_payload_select_ops(const struct nft_ctx *ctx, | |
694 | const struct nlattr * const tb[]) | |
c29b72e0 PM |
695 | { |
696 | enum nft_payload_bases base; | |
697 | unsigned int offset, len; | |
698 | ||
7ec3f7b4 | 699 | if (tb[NFTA_PAYLOAD_BASE] == NULL || |
c29b72e0 PM |
700 | tb[NFTA_PAYLOAD_OFFSET] == NULL || |
701 | tb[NFTA_PAYLOAD_LEN] == NULL) | |
702 | return ERR_PTR(-EINVAL); | |
703 | ||
704 | base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE])); | |
705 | switch (base) { | |
706 | case NFT_PAYLOAD_LL_HEADER: | |
707 | case NFT_PAYLOAD_NETWORK_HEADER: | |
708 | case NFT_PAYLOAD_TRANSPORT_HEADER: | |
709 | break; | |
710 | default: | |
711 | return ERR_PTR(-EOPNOTSUPP); | |
712 | } | |
713 | ||
7ec3f7b4 PM |
714 | if (tb[NFTA_PAYLOAD_SREG] != NULL) { |
715 | if (tb[NFTA_PAYLOAD_DREG] != NULL) | |
716 | return ERR_PTR(-EINVAL); | |
717 | return &nft_payload_set_ops; | |
718 | } | |
719 | ||
720 | if (tb[NFTA_PAYLOAD_DREG] == NULL) | |
721 | return ERR_PTR(-EINVAL); | |
722 | ||
c29b72e0 | 723 | offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET])); |
45d9bcda | 724 | len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN])); |
c29b72e0 | 725 | |
f627ed91 NA |
726 | if (len <= 4 && is_power_of_2(len) && IS_ALIGNED(offset, len) && |
727 | base != NFT_PAYLOAD_LL_HEADER) | |
c29b72e0 PM |
728 | return &nft_payload_fast_ops; |
729 | else | |
730 | return &nft_payload_ops; | |
731 | } | |
732 | ||
4e24877e | 733 | struct nft_expr_type nft_payload_type __read_mostly = { |
ef1f7df9 | 734 | .name = "payload", |
c29b72e0 | 735 | .select_ops = nft_payload_select_ops, |
96518518 PM |
736 | .policy = nft_payload_policy, |
737 | .maxattr = NFTA_PAYLOAD_MAX, | |
ef1f7df9 | 738 | .owner = THIS_MODULE, |
96518518 | 739 | }; |