Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux-2.6-microblaze.git] / net / netfilter / nft_payload.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
4  * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
5  *
6  * Development of this code funded by Astaro AG (http://www.astaro.com/)
7  */
8
9 #include <linux/kernel.h>
10 #include <linux/if_vlan.h>
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>
18 #include <net/netfilter/nf_tables_offload.h>
19 /* For layer 4 checksum field offset. */
20 #include <linux/tcp.h>
21 #include <linux/udp.h>
22 #include <linux/icmpv6.h>
23 #include <linux/ip.h>
24 #include <linux/ipv6.h>
25 #include <net/sctp/checksum.h>
26
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
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;
45         u8 *vlanh, *dst_u8 = (u8 *) d;
46         struct vlan_ethhdr veth;
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;
53
54         vlanh = (u8 *) &veth;
55         if (offset < VLAN_ETH_HLEN + vlan_hlen) {
56                 u8 ethlen = len;
57
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))
62                         return false;
63
64                 if (offset + len > VLAN_ETH_HLEN + vlan_hlen)
65                         ethlen -= offset + len - VLAN_ETH_HLEN + vlan_hlen;
66
67                 memcpy(dst_u8, vlanh + offset - vlan_hlen, ethlen);
68
69                 len -= ethlen;
70                 if (len == 0)
71                         return true;
72
73                 dst_u8 += ethlen;
74                 offset = ETH_HLEN + vlan_hlen;
75         } else {
76                 offset -= VLAN_HLEN + vlan_hlen;
77         }
78
79         return skb_copy_bits(skb, offset + mac_off, dst_u8, len) == 0;
80 }
81
82 void nft_payload_eval(const struct nft_expr *expr,
83                       struct nft_regs *regs,
84                       const struct nft_pktinfo *pkt)
85 {
86         const struct nft_payload *priv = nft_expr_priv(expr);
87         const struct sk_buff *skb = pkt->skb;
88         u32 *dest = &regs->data[priv->dreg];
89         int offset;
90
91         if (priv->len % NFT_REG32_SIZE)
92                 dest[priv->len / NFT_REG32_SIZE] = 0;
93
94         switch (priv->base) {
95         case NFT_PAYLOAD_LL_HEADER:
96                 if (!skb_mac_header_was_set(skb))
97                         goto err;
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                 }
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:
111                 if (!pkt->tprot_set)
112                         goto err;
113                 offset = pkt->xt.thoff;
114                 break;
115         default:
116                 BUG();
117         }
118         offset += priv->offset;
119
120         if (skb_copy_bits(skb, offset, dest, priv->len) < 0)
121                 goto err;
122         return;
123 err:
124         regs->verdict.code = NFT_BREAK;
125 }
126
127 static const struct nla_policy nft_payload_policy[NFTA_PAYLOAD_MAX + 1] = {
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 },
135         [NFTA_PAYLOAD_CSUM_FLAGS]       = { .type = NLA_U32 },
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);
143
144         priv->base   = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
145         priv->offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
146         priv->len    = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
147         priv->dreg   = nft_parse_register(tb[NFTA_PAYLOAD_DREG]);
148
149         return nft_validate_register_store(ctx, priv->dreg, NULL,
150                                            NFT_DATA_VALUE, priv->len);
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
157         if (nft_dump_register(skb, NFTA_PAYLOAD_DREG, priv->dreg) ||
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
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):
176                 if (priv->len != ETH_ALEN)
177                         return -EOPNOTSUPP;
178
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):
183                 if (priv->len != ETH_ALEN)
184                         return -EOPNOTSUPP;
185
186                 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_ETH_ADDRS, eth_addrs,
187                                   dst, ETH_ALEN, reg);
188                 break;
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);
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);
226                 break;
227         default:
228                 return -EOPNOTSUPP;
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):
242                 if (priv->len != sizeof(struct in_addr))
243                         return -EOPNOTSUPP;
244
245                 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4, src,
246                                   sizeof(struct in_addr), reg);
247                 break;
248         case offsetof(struct iphdr, daddr):
249                 if (priv->len != sizeof(struct in_addr))
250                         return -EOPNOTSUPP;
251
252                 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4, dst,
253                                   sizeof(struct in_addr), reg);
254                 break;
255         case offsetof(struct iphdr, protocol):
256                 if (priv->len != sizeof(__u8))
257                         return -EOPNOTSUPP;
258
259                 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_BASIC, basic, ip_proto,
260                                   sizeof(__u8), reg);
261                 nft_offload_set_dependency(ctx, NFT_OFFLOAD_DEP_TRANSPORT);
262                 break;
263         default:
264                 return -EOPNOTSUPP;
265         }
266
267         return 0;
268 }
269
270 static int nft_payload_offload_ip6(struct nft_offload_ctx *ctx,
271                                   struct nft_flow_rule *flow,
272                                   const struct nft_payload *priv)
273 {
274         struct nft_offload_reg *reg = &ctx->regs[priv->dreg];
275
276         switch (priv->offset) {
277         case offsetof(struct ipv6hdr, saddr):
278                 if (priv->len != sizeof(struct in6_addr))
279                         return -EOPNOTSUPP;
280
281                 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6, src,
282                                   sizeof(struct in6_addr), reg);
283                 break;
284         case offsetof(struct ipv6hdr, daddr):
285                 if (priv->len != sizeof(struct in6_addr))
286                         return -EOPNOTSUPP;
287
288                 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6, dst,
289                                   sizeof(struct in6_addr), reg);
290                 break;
291         case offsetof(struct ipv6hdr, nexthdr):
292                 if (priv->len != sizeof(__u8))
293                         return -EOPNOTSUPP;
294
295                 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_BASIC, basic, ip_proto,
296                                   sizeof(__u8), reg);
297                 nft_offload_set_dependency(ctx, NFT_OFFLOAD_DEP_TRANSPORT);
298                 break;
299         default:
300                 return -EOPNOTSUPP;
301         }
302
303         return 0;
304 }
305
306 static int nft_payload_offload_nh(struct nft_offload_ctx *ctx,
307                                   struct nft_flow_rule *flow,
308                                   const struct nft_payload *priv)
309 {
310         int err;
311
312         switch (ctx->dep.l3num) {
313         case htons(ETH_P_IP):
314                 err = nft_payload_offload_ip(ctx, flow, priv);
315                 break;
316         case htons(ETH_P_IPV6):
317                 err = nft_payload_offload_ip6(ctx, flow, priv);
318                 break;
319         default:
320                 return -EOPNOTSUPP;
321         }
322
323         return err;
324 }
325
326 static int nft_payload_offload_tcp(struct nft_offload_ctx *ctx,
327                                    struct nft_flow_rule *flow,
328                                    const struct nft_payload *priv)
329 {
330         struct nft_offload_reg *reg = &ctx->regs[priv->dreg];
331
332         switch (priv->offset) {
333         case offsetof(struct tcphdr, source):
334                 if (priv->len != sizeof(__be16))
335                         return -EOPNOTSUPP;
336
337                 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, src,
338                                   sizeof(__be16), reg);
339                 break;
340         case offsetof(struct tcphdr, dest):
341                 if (priv->len != sizeof(__be16))
342                         return -EOPNOTSUPP;
343
344                 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, dst,
345                                   sizeof(__be16), reg);
346                 break;
347         default:
348                 return -EOPNOTSUPP;
349         }
350
351         return 0;
352 }
353
354 static int nft_payload_offload_udp(struct nft_offload_ctx *ctx,
355                                    struct nft_flow_rule *flow,
356                                    const struct nft_payload *priv)
357 {
358         struct nft_offload_reg *reg = &ctx->regs[priv->dreg];
359
360         switch (priv->offset) {
361         case offsetof(struct udphdr, source):
362                 if (priv->len != sizeof(__be16))
363                         return -EOPNOTSUPP;
364
365                 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, src,
366                                   sizeof(__be16), reg);
367                 break;
368         case offsetof(struct udphdr, dest):
369                 if (priv->len != sizeof(__be16))
370                         return -EOPNOTSUPP;
371
372                 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, dst,
373                                   sizeof(__be16), reg);
374                 break;
375         default:
376                 return -EOPNOTSUPP;
377         }
378
379         return 0;
380 }
381
382 static int nft_payload_offload_th(struct nft_offload_ctx *ctx,
383                                   struct nft_flow_rule *flow,
384                                   const struct nft_payload *priv)
385 {
386         int err;
387
388         switch (ctx->dep.protonum) {
389         case IPPROTO_TCP:
390                 err = nft_payload_offload_tcp(ctx, flow, priv);
391                 break;
392         case IPPROTO_UDP:
393                 err = nft_payload_offload_udp(ctx, flow, priv);
394                 break;
395         default:
396                 return -EOPNOTSUPP;
397         }
398
399         return err;
400 }
401
402 static int nft_payload_offload(struct nft_offload_ctx *ctx,
403                                struct nft_flow_rule *flow,
404                                const struct nft_expr *expr)
405 {
406         const struct nft_payload *priv = nft_expr_priv(expr);
407         int err;
408
409         switch (priv->base) {
410         case NFT_PAYLOAD_LL_HEADER:
411                 err = nft_payload_offload_ll(ctx, flow, priv);
412                 break;
413         case NFT_PAYLOAD_NETWORK_HEADER:
414                 err = nft_payload_offload_nh(ctx, flow, priv);
415                 break;
416         case NFT_PAYLOAD_TRANSPORT_HEADER:
417                 err = nft_payload_offload_th(ctx, flow, priv);
418                 break;
419         default:
420                 err = -EOPNOTSUPP;
421                 break;
422         }
423         return err;
424 }
425
426 static const struct nft_expr_ops nft_payload_ops = {
427         .type           = &nft_payload_type,
428         .size           = NFT_EXPR_SIZE(sizeof(struct nft_payload)),
429         .eval           = nft_payload_eval,
430         .init           = nft_payload_init,
431         .dump           = nft_payload_dump,
432         .offload        = nft_payload_offload,
433 };
434
435 const struct nft_expr_ops nft_payload_fast_ops = {
436         .type           = &nft_payload_type,
437         .size           = NFT_EXPR_SIZE(sizeof(struct nft_payload)),
438         .eval           = nft_payload_eval,
439         .init           = nft_payload_init,
440         .dump           = nft_payload_dump,
441         .offload        = nft_payload_offload,
442 };
443
444 static inline void nft_csum_replace(__sum16 *sum, __wsum fsum, __wsum tsum)
445 {
446         *sum = csum_fold(csum_add(csum_sub(~csum_unfold(*sum), fsum), tsum));
447         if (*sum == 0)
448                 *sum = CSUM_MANGLED_0;
449 }
450
451 static bool nft_payload_udp_checksum(struct sk_buff *skb, unsigned int thoff)
452 {
453         struct udphdr *uh, _uh;
454
455         uh = skb_header_pointer(skb, thoff, sizeof(_uh), &_uh);
456         if (!uh)
457                 return false;
458
459         return (__force bool)uh->check;
460 }
461
462 static int nft_payload_l4csum_offset(const struct nft_pktinfo *pkt,
463                                      struct sk_buff *skb,
464                                      unsigned int *l4csum_offset)
465 {
466         switch (pkt->tprot) {
467         case IPPROTO_TCP:
468                 *l4csum_offset = offsetof(struct tcphdr, check);
469                 break;
470         case IPPROTO_UDP:
471                 if (!nft_payload_udp_checksum(skb, pkt->xt.thoff))
472                         return -1;
473                 fallthrough;
474         case IPPROTO_UDPLITE:
475                 *l4csum_offset = offsetof(struct udphdr, check);
476                 break;
477         case IPPROTO_ICMPV6:
478                 *l4csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
479                 break;
480         default:
481                 return -1;
482         }
483
484         *l4csum_offset += pkt->xt.thoff;
485         return 0;
486 }
487
488 static int nft_payload_csum_sctp(struct sk_buff *skb, int offset)
489 {
490         struct sctphdr *sh;
491
492         if (skb_ensure_writable(skb, offset + sizeof(*sh)))
493                 return -1;
494
495         sh = (struct sctphdr *)(skb->data + offset);
496         sh->checksum = sctp_compute_cksum(skb, offset);
497         skb->ip_summed = CHECKSUM_UNNECESSARY;
498         return 0;
499 }
500
501 static int nft_payload_l4csum_update(const struct nft_pktinfo *pkt,
502                                      struct sk_buff *skb,
503                                      __wsum fsum, __wsum tsum)
504 {
505         int l4csum_offset;
506         __sum16 sum;
507
508         /* If we cannot determine layer 4 checksum offset or this packet doesn't
509          * require layer 4 checksum recalculation, skip this packet.
510          */
511         if (nft_payload_l4csum_offset(pkt, skb, &l4csum_offset) < 0)
512                 return 0;
513
514         if (skb_copy_bits(skb, l4csum_offset, &sum, sizeof(sum)) < 0)
515                 return -1;
516
517         /* Checksum mangling for an arbitrary amount of bytes, based on
518          * inet_proto_csum_replace*() functions.
519          */
520         if (skb->ip_summed != CHECKSUM_PARTIAL) {
521                 nft_csum_replace(&sum, fsum, tsum);
522                 if (skb->ip_summed == CHECKSUM_COMPLETE) {
523                         skb->csum = ~csum_add(csum_sub(~(skb->csum), fsum),
524                                               tsum);
525                 }
526         } else {
527                 sum = ~csum_fold(csum_add(csum_sub(csum_unfold(sum), fsum),
528                                           tsum));
529         }
530
531         if (skb_ensure_writable(skb, l4csum_offset + sizeof(sum)) ||
532             skb_store_bits(skb, l4csum_offset, &sum, sizeof(sum)) < 0)
533                 return -1;
534
535         return 0;
536 }
537
538 static int nft_payload_csum_inet(struct sk_buff *skb, const u32 *src,
539                                  __wsum fsum, __wsum tsum, int csum_offset)
540 {
541         __sum16 sum;
542
543         if (skb_copy_bits(skb, csum_offset, &sum, sizeof(sum)) < 0)
544                 return -1;
545
546         nft_csum_replace(&sum, fsum, tsum);
547         if (skb_ensure_writable(skb, csum_offset + sizeof(sum)) ||
548             skb_store_bits(skb, csum_offset, &sum, sizeof(sum)) < 0)
549                 return -1;
550
551         return 0;
552 }
553
554 static void nft_payload_set_eval(const struct nft_expr *expr,
555                                  struct nft_regs *regs,
556                                  const struct nft_pktinfo *pkt)
557 {
558         const struct nft_payload_set *priv = nft_expr_priv(expr);
559         struct sk_buff *skb = pkt->skb;
560         const u32 *src = &regs->data[priv->sreg];
561         int offset, csum_offset;
562         __wsum fsum, tsum;
563
564         switch (priv->base) {
565         case NFT_PAYLOAD_LL_HEADER:
566                 if (!skb_mac_header_was_set(skb))
567                         goto err;
568                 offset = skb_mac_header(skb) - skb->data;
569                 break;
570         case NFT_PAYLOAD_NETWORK_HEADER:
571                 offset = skb_network_offset(skb);
572                 break;
573         case NFT_PAYLOAD_TRANSPORT_HEADER:
574                 if (!pkt->tprot_set)
575                         goto err;
576                 offset = pkt->xt.thoff;
577                 break;
578         default:
579                 BUG();
580         }
581
582         csum_offset = offset + priv->csum_offset;
583         offset += priv->offset;
584
585         if ((priv->csum_type == NFT_PAYLOAD_CSUM_INET || priv->csum_flags) &&
586             (priv->base != NFT_PAYLOAD_TRANSPORT_HEADER ||
587              skb->ip_summed != CHECKSUM_PARTIAL)) {
588                 fsum = skb_checksum(skb, offset, priv->len, 0);
589                 tsum = csum_partial(src, priv->len, 0);
590
591                 if (priv->csum_type == NFT_PAYLOAD_CSUM_INET &&
592                     nft_payload_csum_inet(skb, src, fsum, tsum, csum_offset))
593                         goto err;
594
595                 if (priv->csum_flags &&
596                     nft_payload_l4csum_update(pkt, skb, fsum, tsum) < 0)
597                         goto err;
598         }
599
600         if (skb_ensure_writable(skb, max(offset + priv->len, 0)) ||
601             skb_store_bits(skb, offset, src, priv->len) < 0)
602                 goto err;
603
604         if (priv->csum_type == NFT_PAYLOAD_CSUM_SCTP &&
605             pkt->tprot == IPPROTO_SCTP &&
606             skb->ip_summed != CHECKSUM_PARTIAL) {
607                 if (nft_payload_csum_sctp(skb, pkt->xt.thoff))
608                         goto err;
609         }
610
611         return;
612 err:
613         regs->verdict.code = NFT_BREAK;
614 }
615
616 static int nft_payload_set_init(const struct nft_ctx *ctx,
617                                 const struct nft_expr *expr,
618                                 const struct nlattr * const tb[])
619 {
620         struct nft_payload_set *priv = nft_expr_priv(expr);
621
622         priv->base        = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
623         priv->offset      = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
624         priv->len         = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
625         priv->sreg        = nft_parse_register(tb[NFTA_PAYLOAD_SREG]);
626
627         if (tb[NFTA_PAYLOAD_CSUM_TYPE])
628                 priv->csum_type =
629                         ntohl(nla_get_be32(tb[NFTA_PAYLOAD_CSUM_TYPE]));
630         if (tb[NFTA_PAYLOAD_CSUM_OFFSET])
631                 priv->csum_offset =
632                         ntohl(nla_get_be32(tb[NFTA_PAYLOAD_CSUM_OFFSET]));
633         if (tb[NFTA_PAYLOAD_CSUM_FLAGS]) {
634                 u32 flags;
635
636                 flags = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_CSUM_FLAGS]));
637                 if (flags & ~NFT_PAYLOAD_L4CSUM_PSEUDOHDR)
638                         return -EINVAL;
639
640                 priv->csum_flags = flags;
641         }
642
643         switch (priv->csum_type) {
644         case NFT_PAYLOAD_CSUM_NONE:
645         case NFT_PAYLOAD_CSUM_INET:
646                 break;
647         case NFT_PAYLOAD_CSUM_SCTP:
648                 if (priv->base != NFT_PAYLOAD_TRANSPORT_HEADER)
649                         return -EINVAL;
650
651                 if (priv->csum_offset != offsetof(struct sctphdr, checksum))
652                         return -EINVAL;
653                 break;
654         default:
655                 return -EOPNOTSUPP;
656         }
657
658         return nft_validate_register_load(priv->sreg, priv->len);
659 }
660
661 static int nft_payload_set_dump(struct sk_buff *skb, const struct nft_expr *expr)
662 {
663         const struct nft_payload_set *priv = nft_expr_priv(expr);
664
665         if (nft_dump_register(skb, NFTA_PAYLOAD_SREG, priv->sreg) ||
666             nla_put_be32(skb, NFTA_PAYLOAD_BASE, htonl(priv->base)) ||
667             nla_put_be32(skb, NFTA_PAYLOAD_OFFSET, htonl(priv->offset)) ||
668             nla_put_be32(skb, NFTA_PAYLOAD_LEN, htonl(priv->len)) ||
669             nla_put_be32(skb, NFTA_PAYLOAD_CSUM_TYPE, htonl(priv->csum_type)) ||
670             nla_put_be32(skb, NFTA_PAYLOAD_CSUM_OFFSET,
671                          htonl(priv->csum_offset)) ||
672             nla_put_be32(skb, NFTA_PAYLOAD_CSUM_FLAGS, htonl(priv->csum_flags)))
673                 goto nla_put_failure;
674         return 0;
675
676 nla_put_failure:
677         return -1;
678 }
679
680 static const struct nft_expr_ops nft_payload_set_ops = {
681         .type           = &nft_payload_type,
682         .size           = NFT_EXPR_SIZE(sizeof(struct nft_payload_set)),
683         .eval           = nft_payload_set_eval,
684         .init           = nft_payload_set_init,
685         .dump           = nft_payload_set_dump,
686 };
687
688 static const struct nft_expr_ops *
689 nft_payload_select_ops(const struct nft_ctx *ctx,
690                        const struct nlattr * const tb[])
691 {
692         enum nft_payload_bases base;
693         unsigned int offset, len;
694
695         if (tb[NFTA_PAYLOAD_BASE] == NULL ||
696             tb[NFTA_PAYLOAD_OFFSET] == NULL ||
697             tb[NFTA_PAYLOAD_LEN] == NULL)
698                 return ERR_PTR(-EINVAL);
699
700         base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
701         switch (base) {
702         case NFT_PAYLOAD_LL_HEADER:
703         case NFT_PAYLOAD_NETWORK_HEADER:
704         case NFT_PAYLOAD_TRANSPORT_HEADER:
705                 break;
706         default:
707                 return ERR_PTR(-EOPNOTSUPP);
708         }
709
710         if (tb[NFTA_PAYLOAD_SREG] != NULL) {
711                 if (tb[NFTA_PAYLOAD_DREG] != NULL)
712                         return ERR_PTR(-EINVAL);
713                 return &nft_payload_set_ops;
714         }
715
716         if (tb[NFTA_PAYLOAD_DREG] == NULL)
717                 return ERR_PTR(-EINVAL);
718
719         offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
720         len    = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
721
722         if (len <= 4 && is_power_of_2(len) && IS_ALIGNED(offset, len) &&
723             base != NFT_PAYLOAD_LL_HEADER)
724                 return &nft_payload_fast_ops;
725         else
726                 return &nft_payload_ops;
727 }
728
729 struct nft_expr_type nft_payload_type __read_mostly = {
730         .name           = "payload",
731         .select_ops     = nft_payload_select_ops,
732         .policy         = nft_payload_policy,
733         .maxattr        = NFTA_PAYLOAD_MAX,
734         .owner          = THIS_MODULE,
735 };