]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/netfilter/nft_payload.c
netfilter: nft_payload: fix C-VLAN offload support
[mirror_ubuntu-jammy-kernel.git] / net / netfilter / nft_payload.c
CommitLineData
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
27static 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 */
41static bool
42nft_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
82void 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 = &regs->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;
123err:
a55e22e9 124 regs->verdict.code = NFT_BREAK;
96518518
PM
125}
126
127static 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
138static 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]));
96518518 147
345023b0
PNA
148 return nft_parse_register_store(ctx, tb[NFTA_PAYLOAD_DREG],
149 &priv->dreg, NULL, NFT_DATA_VALUE,
150 priv->len);
96518518
PM
151}
152
153static 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
164nla_put_failure:
165 return -1;
166}
167
a5d45bc0
PNA
168static bool nft_payload_offload_mask(struct nft_offload_reg *reg,
169 u32 priv_len, u32 field_len)
170{
171 unsigned int remainder, delta, k;
172 struct nft_data mask = {};
173 __be32 remainder_mask;
174
175 if (priv_len == field_len) {
176 memset(&reg->mask, 0xff, priv_len);
177 return true;
178 } else if (priv_len > field_len) {
179 return false;
180 }
181
182 memset(&mask, 0xff, field_len);
183 remainder = priv_len % sizeof(u32);
184 if (remainder) {
185 k = priv_len / sizeof(u32);
186 delta = field_len - priv_len;
187 remainder_mask = htonl(~((1 << (delta * BITS_PER_BYTE)) - 1));
188 mask.data[k] = (__force u32)remainder_mask;
189 }
190
191 memcpy(&reg->mask, &mask, field_len);
192
193 return true;
194}
195
c9626a2c
PNA
196static int nft_payload_offload_ll(struct nft_offload_ctx *ctx,
197 struct nft_flow_rule *flow,
198 const struct nft_payload *priv)
199{
200 struct nft_offload_reg *reg = &ctx->regs[priv->dreg];
201
202 switch (priv->offset) {
203 case offsetof(struct ethhdr, h_source):
a5d45bc0 204 if (!nft_payload_offload_mask(reg, priv->len, ETH_ALEN))
a69a85da 205 return -EOPNOTSUPP;
206
c9626a2c
PNA
207 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_ETH_ADDRS, eth_addrs,
208 src, ETH_ALEN, reg);
209 break;
210 case offsetof(struct ethhdr, h_dest):
a5d45bc0 211 if (!nft_payload_offload_mask(reg, priv->len, ETH_ALEN))
a69a85da 212 return -EOPNOTSUPP;
213
c9626a2c
PNA
214 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_ETH_ADDRS, eth_addrs,
215 dst, ETH_ALEN, reg);
216 break;
a82055af 217 case offsetof(struct ethhdr, h_proto):
a5d45bc0 218 if (!nft_payload_offload_mask(reg, priv->len, sizeof(__be16)))
a82055af
PNA
219 return -EOPNOTSUPP;
220
221 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_BASIC, basic,
222 n_proto, sizeof(__be16), reg);
223 nft_offload_set_dependency(ctx, NFT_OFFLOAD_DEP_NETWORK);
224 break;
225 case offsetof(struct vlan_ethhdr, h_vlan_TCI):
a5d45bc0 226 if (!nft_payload_offload_mask(reg, priv->len, sizeof(__be16)))
a82055af
PNA
227 return -EOPNOTSUPP;
228
229 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_VLAN, vlan,
230 vlan_tci, sizeof(__be16), reg);
231 break;
232 case offsetof(struct vlan_ethhdr, h_vlan_encapsulated_proto):
a5d45bc0 233 if (!nft_payload_offload_mask(reg, priv->len, sizeof(__be16)))
a82055af
PNA
234 return -EOPNOTSUPP;
235
236 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_VLAN, vlan,
237 vlan_tpid, sizeof(__be16), reg);
89d8fd44
PNA
238 nft_offload_set_dependency(ctx, NFT_OFFLOAD_DEP_NETWORK);
239 break;
240 case offsetof(struct vlan_ethhdr, h_vlan_TCI) + sizeof(struct vlan_hdr):
a5d45bc0 241 if (!nft_payload_offload_mask(reg, priv->len, sizeof(__be16)))
89d8fd44
PNA
242 return -EOPNOTSUPP;
243
14c20643 244 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_CVLAN, cvlan,
89d8fd44
PNA
245 vlan_tci, sizeof(__be16), reg);
246 break;
247 case offsetof(struct vlan_ethhdr, h_vlan_encapsulated_proto) +
248 sizeof(struct vlan_hdr):
a5d45bc0 249 if (!nft_payload_offload_mask(reg, priv->len, sizeof(__be16)))
89d8fd44
PNA
250 return -EOPNOTSUPP;
251
14c20643 252 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_CVLAN, cvlan,
89d8fd44 253 vlan_tpid, sizeof(__be16), reg);
14c20643 254 nft_offload_set_dependency(ctx, NFT_OFFLOAD_DEP_NETWORK);
a82055af 255 break;
a69a85da 256 default:
257 return -EOPNOTSUPP;
c9626a2c
PNA
258 }
259
260 return 0;
261}
262
263static int nft_payload_offload_ip(struct nft_offload_ctx *ctx,
264 struct nft_flow_rule *flow,
265 const struct nft_payload *priv)
266{
267 struct nft_offload_reg *reg = &ctx->regs[priv->dreg];
268
269 switch (priv->offset) {
270 case offsetof(struct iphdr, saddr):
a5d45bc0
PNA
271 if (!nft_payload_offload_mask(reg, priv->len,
272 sizeof(struct in_addr)))
a69a85da 273 return -EOPNOTSUPP;
274
c9626a2c
PNA
275 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4, src,
276 sizeof(struct in_addr), reg);
3c78e9e0 277 nft_flow_rule_set_addr_type(flow, FLOW_DISSECTOR_KEY_IPV4_ADDRS);
c9626a2c
PNA
278 break;
279 case offsetof(struct iphdr, daddr):
a5d45bc0
PNA
280 if (!nft_payload_offload_mask(reg, priv->len,
281 sizeof(struct in_addr)))
a69a85da 282 return -EOPNOTSUPP;
283
c9626a2c
PNA
284 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4, dst,
285 sizeof(struct in_addr), reg);
3c78e9e0 286 nft_flow_rule_set_addr_type(flow, FLOW_DISSECTOR_KEY_IPV4_ADDRS);
c9626a2c
PNA
287 break;
288 case offsetof(struct iphdr, protocol):
a5d45bc0 289 if (!nft_payload_offload_mask(reg, priv->len, sizeof(__u8)))
a69a85da 290 return -EOPNOTSUPP;
291
c9626a2c
PNA
292 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_BASIC, basic, ip_proto,
293 sizeof(__u8), reg);
294 nft_offload_set_dependency(ctx, NFT_OFFLOAD_DEP_TRANSPORT);
295 break;
296 default:
297 return -EOPNOTSUPP;
298 }
299
300 return 0;
301}
302
303static int nft_payload_offload_ip6(struct nft_offload_ctx *ctx,
304 struct nft_flow_rule *flow,
305 const struct nft_payload *priv)
306{
307 struct nft_offload_reg *reg = &ctx->regs[priv->dreg];
308
309 switch (priv->offset) {
310 case offsetof(struct ipv6hdr, saddr):
a5d45bc0
PNA
311 if (!nft_payload_offload_mask(reg, priv->len,
312 sizeof(struct in6_addr)))
a69a85da 313 return -EOPNOTSUPP;
314
c9626a2c
PNA
315 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6, src,
316 sizeof(struct in6_addr), reg);
3c78e9e0 317 nft_flow_rule_set_addr_type(flow, FLOW_DISSECTOR_KEY_IPV6_ADDRS);
c9626a2c
PNA
318 break;
319 case offsetof(struct ipv6hdr, daddr):
a5d45bc0
PNA
320 if (!nft_payload_offload_mask(reg, priv->len,
321 sizeof(struct in6_addr)))
a69a85da 322 return -EOPNOTSUPP;
323
c9626a2c
PNA
324 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6, dst,
325 sizeof(struct in6_addr), reg);
3c78e9e0 326 nft_flow_rule_set_addr_type(flow, FLOW_DISSECTOR_KEY_IPV6_ADDRS);
c9626a2c
PNA
327 break;
328 case offsetof(struct ipv6hdr, nexthdr):
a5d45bc0 329 if (!nft_payload_offload_mask(reg, priv->len, sizeof(__u8)))
a69a85da 330 return -EOPNOTSUPP;
331
c9626a2c
PNA
332 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_BASIC, basic, ip_proto,
333 sizeof(__u8), reg);
334 nft_offload_set_dependency(ctx, NFT_OFFLOAD_DEP_TRANSPORT);
335 break;
336 default:
337 return -EOPNOTSUPP;
338 }
339
340 return 0;
341}
342
343static int nft_payload_offload_nh(struct nft_offload_ctx *ctx,
344 struct nft_flow_rule *flow,
345 const struct nft_payload *priv)
346{
347 int err;
348
349 switch (ctx->dep.l3num) {
350 case htons(ETH_P_IP):
351 err = nft_payload_offload_ip(ctx, flow, priv);
352 break;
353 case htons(ETH_P_IPV6):
354 err = nft_payload_offload_ip6(ctx, flow, priv);
355 break;
356 default:
357 return -EOPNOTSUPP;
358 }
359
360 return err;
361}
362
363static int nft_payload_offload_tcp(struct nft_offload_ctx *ctx,
364 struct nft_flow_rule *flow,
365 const struct nft_payload *priv)
366{
367 struct nft_offload_reg *reg = &ctx->regs[priv->dreg];
368
369 switch (priv->offset) {
370 case offsetof(struct tcphdr, source):
a5d45bc0 371 if (!nft_payload_offload_mask(reg, priv->len, sizeof(__be16)))
a69a85da 372 return -EOPNOTSUPP;
373
c9626a2c
PNA
374 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, src,
375 sizeof(__be16), reg);
376 break;
377 case offsetof(struct tcphdr, dest):
a5d45bc0 378 if (!nft_payload_offload_mask(reg, priv->len, sizeof(__be16)))
a69a85da 379 return -EOPNOTSUPP;
380
c9626a2c
PNA
381 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, dst,
382 sizeof(__be16), reg);
383 break;
384 default:
385 return -EOPNOTSUPP;
386 }
387
388 return 0;
389}
390
391static int nft_payload_offload_udp(struct nft_offload_ctx *ctx,
392 struct nft_flow_rule *flow,
393 const struct nft_payload *priv)
394{
395 struct nft_offload_reg *reg = &ctx->regs[priv->dreg];
396
397 switch (priv->offset) {
398 case offsetof(struct udphdr, source):
a5d45bc0 399 if (!nft_payload_offload_mask(reg, priv->len, sizeof(__be16)))
a69a85da 400 return -EOPNOTSUPP;
401
c9626a2c
PNA
402 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, src,
403 sizeof(__be16), reg);
404 break;
405 case offsetof(struct udphdr, dest):
a5d45bc0 406 if (!nft_payload_offload_mask(reg, priv->len, sizeof(__be16)))
a69a85da 407 return -EOPNOTSUPP;
408
c9626a2c
PNA
409 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, dst,
410 sizeof(__be16), reg);
411 break;
412 default:
413 return -EOPNOTSUPP;
414 }
415
416 return 0;
417}
418
419static int nft_payload_offload_th(struct nft_offload_ctx *ctx,
420 struct nft_flow_rule *flow,
421 const struct nft_payload *priv)
422{
423 int err;
424
425 switch (ctx->dep.protonum) {
426 case IPPROTO_TCP:
427 err = nft_payload_offload_tcp(ctx, flow, priv);
428 break;
429 case IPPROTO_UDP:
430 err = nft_payload_offload_udp(ctx, flow, priv);
431 break;
432 default:
433 return -EOPNOTSUPP;
434 }
435
436 return err;
437}
438
439static int nft_payload_offload(struct nft_offload_ctx *ctx,
440 struct nft_flow_rule *flow,
441 const struct nft_expr *expr)
442{
443 const struct nft_payload *priv = nft_expr_priv(expr);
444 int err;
445
446 switch (priv->base) {
447 case NFT_PAYLOAD_LL_HEADER:
448 err = nft_payload_offload_ll(ctx, flow, priv);
449 break;
450 case NFT_PAYLOAD_NETWORK_HEADER:
451 err = nft_payload_offload_nh(ctx, flow, priv);
452 break;
453 case NFT_PAYLOAD_TRANSPORT_HEADER:
454 err = nft_payload_offload_th(ctx, flow, priv);
455 break;
456 default:
457 err = -EOPNOTSUPP;
458 break;
459 }
460 return err;
461}
462
ef1f7df9
PM
463static const struct nft_expr_ops nft_payload_ops = {
464 .type = &nft_payload_type,
96518518 465 .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)),
96518518
PM
466 .eval = nft_payload_eval,
467 .init = nft_payload_init,
468 .dump = nft_payload_dump,
c9626a2c 469 .offload = nft_payload_offload,
ef1f7df9
PM
470};
471
c29b72e0
PM
472const struct nft_expr_ops nft_payload_fast_ops = {
473 .type = &nft_payload_type,
474 .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)),
475 .eval = nft_payload_eval,
476 .init = nft_payload_init,
477 .dump = nft_payload_dump,
c9626a2c 478 .offload = nft_payload_offload,
c29b72e0
PM
479};
480
18140969
PNA
481static inline void nft_csum_replace(__sum16 *sum, __wsum fsum, __wsum tsum)
482{
483 *sum = csum_fold(csum_add(csum_sub(~csum_unfold(*sum), fsum), tsum));
484 if (*sum == 0)
485 *sum = CSUM_MANGLED_0;
486}
487
488static bool nft_payload_udp_checksum(struct sk_buff *skb, unsigned int thoff)
489{
490 struct udphdr *uh, _uh;
491
492 uh = skb_header_pointer(skb, thoff, sizeof(_uh), &_uh);
493 if (!uh)
494 return false;
495
5fd02ebe 496 return (__force bool)uh->check;
18140969
PNA
497}
498
499static int nft_payload_l4csum_offset(const struct nft_pktinfo *pkt,
500 struct sk_buff *skb,
501 unsigned int *l4csum_offset)
502{
503 switch (pkt->tprot) {
504 case IPPROTO_TCP:
505 *l4csum_offset = offsetof(struct tcphdr, check);
506 break;
507 case IPPROTO_UDP:
508 if (!nft_payload_udp_checksum(skb, pkt->xt.thoff))
509 return -1;
954d8297 510 fallthrough;
18140969
PNA
511 case IPPROTO_UDPLITE:
512 *l4csum_offset = offsetof(struct udphdr, check);
513 break;
514 case IPPROTO_ICMPV6:
515 *l4csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
516 break;
517 default:
518 return -1;
519 }
520
521 *l4csum_offset += pkt->xt.thoff;
522 return 0;
523}
524
346e320c
DC
525static int nft_payload_csum_sctp(struct sk_buff *skb, int offset)
526{
527 struct sctphdr *sh;
528
529 if (skb_ensure_writable(skb, offset + sizeof(*sh)))
530 return -1;
531
532 sh = (struct sctphdr *)(skb->data + offset);
533 sh->checksum = sctp_compute_cksum(skb, offset);
534 skb->ip_summed = CHECKSUM_UNNECESSARY;
535 return 0;
536}
537
18140969
PNA
538static int nft_payload_l4csum_update(const struct nft_pktinfo *pkt,
539 struct sk_buff *skb,
540 __wsum fsum, __wsum tsum)
541{
542 int l4csum_offset;
543 __sum16 sum;
544
545 /* If we cannot determine layer 4 checksum offset or this packet doesn't
546 * require layer 4 checksum recalculation, skip this packet.
547 */
548 if (nft_payload_l4csum_offset(pkt, skb, &l4csum_offset) < 0)
549 return 0;
550
551 if (skb_copy_bits(skb, l4csum_offset, &sum, sizeof(sum)) < 0)
552 return -1;
553
554 /* Checksum mangling for an arbitrary amount of bytes, based on
555 * inet_proto_csum_replace*() functions.
556 */
557 if (skb->ip_summed != CHECKSUM_PARTIAL) {
558 nft_csum_replace(&sum, fsum, tsum);
559 if (skb->ip_summed == CHECKSUM_COMPLETE) {
560 skb->csum = ~csum_add(csum_sub(~(skb->csum), fsum),
561 tsum);
562 }
563 } else {
564 sum = ~csum_fold(csum_add(csum_sub(csum_unfold(sum), fsum),
565 tsum));
566 }
567
7418ee4c 568 if (skb_ensure_writable(skb, l4csum_offset + sizeof(sum)) ||
18140969
PNA
569 skb_store_bits(skb, l4csum_offset, &sum, sizeof(sum)) < 0)
570 return -1;
571
572 return 0;
573}
574
053d20f5
PNA
575static int nft_payload_csum_inet(struct sk_buff *skb, const u32 *src,
576 __wsum fsum, __wsum tsum, int csum_offset)
577{
578 __sum16 sum;
579
580 if (skb_copy_bits(skb, csum_offset, &sum, sizeof(sum)) < 0)
581 return -1;
582
583 nft_csum_replace(&sum, fsum, tsum);
7418ee4c 584 if (skb_ensure_writable(skb, csum_offset + sizeof(sum)) ||
053d20f5
PNA
585 skb_store_bits(skb, csum_offset, &sum, sizeof(sum)) < 0)
586 return -1;
587
588 return 0;
589}
590
7ec3f7b4
PM
591static void nft_payload_set_eval(const struct nft_expr *expr,
592 struct nft_regs *regs,
593 const struct nft_pktinfo *pkt)
594{
595 const struct nft_payload_set *priv = nft_expr_priv(expr);
596 struct sk_buff *skb = pkt->skb;
597 const u32 *src = &regs->data[priv->sreg];
598 int offset, csum_offset;
599 __wsum fsum, tsum;
7ec3f7b4
PM
600
601 switch (priv->base) {
602 case NFT_PAYLOAD_LL_HEADER:
603 if (!skb_mac_header_was_set(skb))
604 goto err;
605 offset = skb_mac_header(skb) - skb->data;
606 break;
607 case NFT_PAYLOAD_NETWORK_HEADER:
608 offset = skb_network_offset(skb);
609 break;
610 case NFT_PAYLOAD_TRANSPORT_HEADER:
a20877b5
LZ
611 if (!pkt->tprot_set)
612 goto err;
7ec3f7b4
PM
613 offset = pkt->xt.thoff;
614 break;
615 default:
616 BUG();
617 }
618
619 csum_offset = offset + priv->csum_offset;
620 offset += priv->offset;
621
053d20f5 622 if ((priv->csum_type == NFT_PAYLOAD_CSUM_INET || priv->csum_flags) &&
7ec3f7b4
PM
623 (priv->base != NFT_PAYLOAD_TRANSPORT_HEADER ||
624 skb->ip_summed != CHECKSUM_PARTIAL)) {
7ec3f7b4
PM
625 fsum = skb_checksum(skb, offset, priv->len, 0);
626 tsum = csum_partial(src, priv->len, 0);
7ec3f7b4 627
053d20f5
PNA
628 if (priv->csum_type == NFT_PAYLOAD_CSUM_INET &&
629 nft_payload_csum_inet(skb, src, fsum, tsum, csum_offset))
7ec3f7b4 630 goto err;
18140969
PNA
631
632 if (priv->csum_flags &&
633 nft_payload_l4csum_update(pkt, skb, fsum, tsum) < 0)
634 goto err;
7ec3f7b4
PM
635 }
636
7418ee4c 637 if (skb_ensure_writable(skb, max(offset + priv->len, 0)) ||
7ec3f7b4
PM
638 skb_store_bits(skb, offset, src, priv->len) < 0)
639 goto err;
640
346e320c
DC
641 if (priv->csum_type == NFT_PAYLOAD_CSUM_SCTP &&
642 pkt->tprot == IPPROTO_SCTP &&
643 skb->ip_summed != CHECKSUM_PARTIAL) {
644 if (nft_payload_csum_sctp(skb, pkt->xt.thoff))
645 goto err;
646 }
647
7ec3f7b4
PM
648 return;
649err:
650 regs->verdict.code = NFT_BREAK;
651}
652
653static int nft_payload_set_init(const struct nft_ctx *ctx,
654 const struct nft_expr *expr,
655 const struct nlattr * const tb[])
656{
657 struct nft_payload_set *priv = nft_expr_priv(expr);
658
659 priv->base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
660 priv->offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
661 priv->len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
7ec3f7b4
PM
662
663 if (tb[NFTA_PAYLOAD_CSUM_TYPE])
664 priv->csum_type =
665 ntohl(nla_get_be32(tb[NFTA_PAYLOAD_CSUM_TYPE]));
666 if (tb[NFTA_PAYLOAD_CSUM_OFFSET])
667 priv->csum_offset =
668 ntohl(nla_get_be32(tb[NFTA_PAYLOAD_CSUM_OFFSET]));
18140969
PNA
669 if (tb[NFTA_PAYLOAD_CSUM_FLAGS]) {
670 u32 flags;
671
672 flags = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_CSUM_FLAGS]));
673 if (flags & ~NFT_PAYLOAD_L4CSUM_PSEUDOHDR)
674 return -EINVAL;
675
676 priv->csum_flags = flags;
677 }
7ec3f7b4
PM
678
679 switch (priv->csum_type) {
680 case NFT_PAYLOAD_CSUM_NONE:
681 case NFT_PAYLOAD_CSUM_INET:
682 break;
346e320c
DC
683 case NFT_PAYLOAD_CSUM_SCTP:
684 if (priv->base != NFT_PAYLOAD_TRANSPORT_HEADER)
685 return -EINVAL;
686
687 if (priv->csum_offset != offsetof(struct sctphdr, checksum))
688 return -EINVAL;
689 break;
7ec3f7b4
PM
690 default:
691 return -EOPNOTSUPP;
692 }
693
4f16d25c
PNA
694 return nft_parse_register_load(tb[NFTA_PAYLOAD_SREG], &priv->sreg,
695 priv->len);
7ec3f7b4
PM
696}
697
698static int nft_payload_set_dump(struct sk_buff *skb, const struct nft_expr *expr)
699{
700 const struct nft_payload_set *priv = nft_expr_priv(expr);
701
702 if (nft_dump_register(skb, NFTA_PAYLOAD_SREG, priv->sreg) ||
703 nla_put_be32(skb, NFTA_PAYLOAD_BASE, htonl(priv->base)) ||
704 nla_put_be32(skb, NFTA_PAYLOAD_OFFSET, htonl(priv->offset)) ||
705 nla_put_be32(skb, NFTA_PAYLOAD_LEN, htonl(priv->len)) ||
706 nla_put_be32(skb, NFTA_PAYLOAD_CSUM_TYPE, htonl(priv->csum_type)) ||
707 nla_put_be32(skb, NFTA_PAYLOAD_CSUM_OFFSET,
18140969
PNA
708 htonl(priv->csum_offset)) ||
709 nla_put_be32(skb, NFTA_PAYLOAD_CSUM_FLAGS, htonl(priv->csum_flags)))
7ec3f7b4
PM
710 goto nla_put_failure;
711 return 0;
712
713nla_put_failure:
714 return -1;
715}
716
717static const struct nft_expr_ops nft_payload_set_ops = {
718 .type = &nft_payload_type,
719 .size = NFT_EXPR_SIZE(sizeof(struct nft_payload_set)),
720 .eval = nft_payload_set_eval,
721 .init = nft_payload_set_init,
722 .dump = nft_payload_set_dump,
723};
724
0ca743a5
PNA
725static const struct nft_expr_ops *
726nft_payload_select_ops(const struct nft_ctx *ctx,
727 const struct nlattr * const tb[])
c29b72e0
PM
728{
729 enum nft_payload_bases base;
730 unsigned int offset, len;
731
7ec3f7b4 732 if (tb[NFTA_PAYLOAD_BASE] == NULL ||
c29b72e0
PM
733 tb[NFTA_PAYLOAD_OFFSET] == NULL ||
734 tb[NFTA_PAYLOAD_LEN] == NULL)
735 return ERR_PTR(-EINVAL);
736
737 base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
738 switch (base) {
739 case NFT_PAYLOAD_LL_HEADER:
740 case NFT_PAYLOAD_NETWORK_HEADER:
741 case NFT_PAYLOAD_TRANSPORT_HEADER:
742 break;
743 default:
744 return ERR_PTR(-EOPNOTSUPP);
745 }
746
7ec3f7b4
PM
747 if (tb[NFTA_PAYLOAD_SREG] != NULL) {
748 if (tb[NFTA_PAYLOAD_DREG] != NULL)
749 return ERR_PTR(-EINVAL);
750 return &nft_payload_set_ops;
751 }
752
753 if (tb[NFTA_PAYLOAD_DREG] == NULL)
754 return ERR_PTR(-EINVAL);
755
c29b72e0 756 offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
45d9bcda 757 len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
c29b72e0 758
f627ed91
NA
759 if (len <= 4 && is_power_of_2(len) && IS_ALIGNED(offset, len) &&
760 base != NFT_PAYLOAD_LL_HEADER)
c29b72e0
PM
761 return &nft_payload_fast_ops;
762 else
763 return &nft_payload_ops;
764}
765
4e24877e 766struct nft_expr_type nft_payload_type __read_mostly = {
ef1f7df9 767 .name = "payload",
c29b72e0 768 .select_ops = nft_payload_select_ops,
96518518
PM
769 .policy = nft_payload_policy,
770 .maxattr = NFTA_PAYLOAD_MAX,
ef1f7df9 771 .owner = THIS_MODULE,
96518518 772};