1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
4 * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
6 * Development of this code funded by Astaro AG (http://www.astaro.com/)
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>
24 #include <linux/ipv6.h>
25 #include <net/sctp/checksum.h>
27 static bool nft_payload_rebuild_vlan_hdr(const struct sk_buff *skb, int mac_off,
28 struct vlan_ethhdr *veth)
30 if (skb_copy_bits(skb, mac_off, veth, ETH_HLEN))
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;
40 /* add vlan header into the user buffer for if tag was removed by offloads */
42 nft_payload_copy_vlan(u32 *d, const struct sk_buff *skb, u8 offset, u8 len)
44 int mac_off = skb_mac_header(skb) - skb->data;
45 u8 *vlanh, *dst_u8 = (u8 *) d;
46 struct vlan_ethhdr veth;
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;
55 if (offset < VLAN_ETH_HLEN + vlan_hlen) {
59 skb_copy_bits(skb, mac_off, &veth, VLAN_ETH_HLEN) < 0)
61 else if (!nft_payload_rebuild_vlan_hdr(skb, mac_off, &veth))
64 if (offset + len > VLAN_ETH_HLEN + vlan_hlen)
65 ethlen -= offset + len - VLAN_ETH_HLEN + vlan_hlen;
67 memcpy(dst_u8, vlanh + offset - vlan_hlen, ethlen);
74 offset = ETH_HLEN + vlan_hlen;
76 offset -= VLAN_HLEN + vlan_hlen;
79 return skb_copy_bits(skb, offset + mac_off, dst_u8, len) == 0;
82 void nft_payload_eval(const struct nft_expr *expr,
83 struct nft_regs *regs,
84 const struct nft_pktinfo *pkt)
86 const struct nft_payload *priv = nft_expr_priv(expr);
87 const struct sk_buff *skb = pkt->skb;
88 u32 *dest = ®s->data[priv->dreg];
91 if (priv->len % NFT_REG32_SIZE)
92 dest[priv->len / NFT_REG32_SIZE] = 0;
95 case NFT_PAYLOAD_LL_HEADER:
96 if (!skb_mac_header_was_set(skb))
99 if (skb_vlan_tag_present(skb)) {
100 if (!nft_payload_copy_vlan(dest, skb,
101 priv->offset, priv->len))
105 offset = skb_mac_header(skb) - skb->data;
107 case NFT_PAYLOAD_NETWORK_HEADER:
108 offset = skb_network_offset(skb);
110 case NFT_PAYLOAD_TRANSPORT_HEADER:
113 offset = pkt->xt.thoff;
118 offset += priv->offset;
120 if (skb_copy_bits(skb, offset, dest, priv->len) < 0)
124 regs->verdict.code = NFT_BREAK;
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 },
138 static int nft_payload_init(const struct nft_ctx *ctx,
139 const struct nft_expr *expr,
140 const struct nlattr * const tb[])
142 struct nft_payload *priv = nft_expr_priv(expr);
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]);
149 return nft_validate_register_store(ctx, priv->dreg, NULL,
150 NFT_DATA_VALUE, priv->len);
153 static int nft_payload_dump(struct sk_buff *skb, const struct nft_expr *expr)
155 const struct nft_payload *priv = nft_expr_priv(expr);
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;
168 static int nft_payload_offload_ll(struct nft_offload_ctx *ctx,
169 struct nft_flow_rule *flow,
170 const struct nft_payload *priv)
172 struct nft_offload_reg *reg = &ctx->regs[priv->dreg];
174 switch (priv->offset) {
175 case offsetof(struct ethhdr, h_source):
176 if (priv->len != ETH_ALEN)
179 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_ETH_ADDRS, eth_addrs,
182 case offsetof(struct ethhdr, h_dest):
183 if (priv->len != ETH_ALEN)
186 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_ETH_ADDRS, eth_addrs,
189 case offsetof(struct ethhdr, h_proto):
190 if (priv->len != sizeof(__be16))
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);
197 case offsetof(struct vlan_ethhdr, h_vlan_TCI):
198 if (priv->len != sizeof(__be16))
201 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_VLAN, vlan,
202 vlan_tci, sizeof(__be16), reg);
204 case offsetof(struct vlan_ethhdr, h_vlan_encapsulated_proto):
205 if (priv->len != sizeof(__be16))
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);
212 case offsetof(struct vlan_ethhdr, h_vlan_TCI) + sizeof(struct vlan_hdr):
213 if (priv->len != sizeof(__be16))
216 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_CVLAN, vlan,
217 vlan_tci, sizeof(__be16), reg);
219 case offsetof(struct vlan_ethhdr, h_vlan_encapsulated_proto) +
220 sizeof(struct vlan_hdr):
221 if (priv->len != sizeof(__be16))
224 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_CVLAN, vlan,
225 vlan_tpid, sizeof(__be16), reg);
234 static int nft_payload_offload_ip(struct nft_offload_ctx *ctx,
235 struct nft_flow_rule *flow,
236 const struct nft_payload *priv)
238 struct nft_offload_reg *reg = &ctx->regs[priv->dreg];
240 switch (priv->offset) {
241 case offsetof(struct iphdr, saddr):
242 if (priv->len != sizeof(struct in_addr))
245 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4, src,
246 sizeof(struct in_addr), reg);
248 case offsetof(struct iphdr, daddr):
249 if (priv->len != sizeof(struct in_addr))
252 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4, dst,
253 sizeof(struct in_addr), reg);
255 case offsetof(struct iphdr, protocol):
256 if (priv->len != sizeof(__u8))
259 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_BASIC, basic, ip_proto,
261 nft_offload_set_dependency(ctx, NFT_OFFLOAD_DEP_TRANSPORT);
270 static int nft_payload_offload_ip6(struct nft_offload_ctx *ctx,
271 struct nft_flow_rule *flow,
272 const struct nft_payload *priv)
274 struct nft_offload_reg *reg = &ctx->regs[priv->dreg];
276 switch (priv->offset) {
277 case offsetof(struct ipv6hdr, saddr):
278 if (priv->len != sizeof(struct in6_addr))
281 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6, src,
282 sizeof(struct in6_addr), reg);
284 case offsetof(struct ipv6hdr, daddr):
285 if (priv->len != sizeof(struct in6_addr))
288 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6, dst,
289 sizeof(struct in6_addr), reg);
291 case offsetof(struct ipv6hdr, nexthdr):
292 if (priv->len != sizeof(__u8))
295 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_BASIC, basic, ip_proto,
297 nft_offload_set_dependency(ctx, NFT_OFFLOAD_DEP_TRANSPORT);
306 static int nft_payload_offload_nh(struct nft_offload_ctx *ctx,
307 struct nft_flow_rule *flow,
308 const struct nft_payload *priv)
312 switch (ctx->dep.l3num) {
313 case htons(ETH_P_IP):
314 err = nft_payload_offload_ip(ctx, flow, priv);
316 case htons(ETH_P_IPV6):
317 err = nft_payload_offload_ip6(ctx, flow, priv);
326 static int nft_payload_offload_tcp(struct nft_offload_ctx *ctx,
327 struct nft_flow_rule *flow,
328 const struct nft_payload *priv)
330 struct nft_offload_reg *reg = &ctx->regs[priv->dreg];
332 switch (priv->offset) {
333 case offsetof(struct tcphdr, source):
334 if (priv->len != sizeof(__be16))
337 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, src,
338 sizeof(__be16), reg);
340 case offsetof(struct tcphdr, dest):
341 if (priv->len != sizeof(__be16))
344 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, dst,
345 sizeof(__be16), reg);
354 static int nft_payload_offload_udp(struct nft_offload_ctx *ctx,
355 struct nft_flow_rule *flow,
356 const struct nft_payload *priv)
358 struct nft_offload_reg *reg = &ctx->regs[priv->dreg];
360 switch (priv->offset) {
361 case offsetof(struct udphdr, source):
362 if (priv->len != sizeof(__be16))
365 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, src,
366 sizeof(__be16), reg);
368 case offsetof(struct udphdr, dest):
369 if (priv->len != sizeof(__be16))
372 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_PORTS, tp, dst,
373 sizeof(__be16), reg);
382 static int nft_payload_offload_th(struct nft_offload_ctx *ctx,
383 struct nft_flow_rule *flow,
384 const struct nft_payload *priv)
388 switch (ctx->dep.protonum) {
390 err = nft_payload_offload_tcp(ctx, flow, priv);
393 err = nft_payload_offload_udp(ctx, flow, priv);
402 static int nft_payload_offload(struct nft_offload_ctx *ctx,
403 struct nft_flow_rule *flow,
404 const struct nft_expr *expr)
406 const struct nft_payload *priv = nft_expr_priv(expr);
409 switch (priv->base) {
410 case NFT_PAYLOAD_LL_HEADER:
411 err = nft_payload_offload_ll(ctx, flow, priv);
413 case NFT_PAYLOAD_NETWORK_HEADER:
414 err = nft_payload_offload_nh(ctx, flow, priv);
416 case NFT_PAYLOAD_TRANSPORT_HEADER:
417 err = nft_payload_offload_th(ctx, flow, priv);
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,
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,
444 static inline void nft_csum_replace(__sum16 *sum, __wsum fsum, __wsum tsum)
446 *sum = csum_fold(csum_add(csum_sub(~csum_unfold(*sum), fsum), tsum));
448 *sum = CSUM_MANGLED_0;
451 static bool nft_payload_udp_checksum(struct sk_buff *skb, unsigned int thoff)
453 struct udphdr *uh, _uh;
455 uh = skb_header_pointer(skb, thoff, sizeof(_uh), &_uh);
459 return (__force bool)uh->check;
462 static int nft_payload_l4csum_offset(const struct nft_pktinfo *pkt,
464 unsigned int *l4csum_offset)
466 switch (pkt->tprot) {
468 *l4csum_offset = offsetof(struct tcphdr, check);
471 if (!nft_payload_udp_checksum(skb, pkt->xt.thoff))
474 case IPPROTO_UDPLITE:
475 *l4csum_offset = offsetof(struct udphdr, check);
478 *l4csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
484 *l4csum_offset += pkt->xt.thoff;
488 static int nft_payload_csum_sctp(struct sk_buff *skb, int offset)
492 if (skb_ensure_writable(skb, offset + sizeof(*sh)))
495 sh = (struct sctphdr *)(skb->data + offset);
496 sh->checksum = sctp_compute_cksum(skb, offset);
497 skb->ip_summed = CHECKSUM_UNNECESSARY;
501 static int nft_payload_l4csum_update(const struct nft_pktinfo *pkt,
503 __wsum fsum, __wsum tsum)
508 /* If we cannot determine layer 4 checksum offset or this packet doesn't
509 * require layer 4 checksum recalculation, skip this packet.
511 if (nft_payload_l4csum_offset(pkt, skb, &l4csum_offset) < 0)
514 if (skb_copy_bits(skb, l4csum_offset, &sum, sizeof(sum)) < 0)
517 /* Checksum mangling for an arbitrary amount of bytes, based on
518 * inet_proto_csum_replace*() functions.
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),
527 sum = ~csum_fold(csum_add(csum_sub(csum_unfold(sum), fsum),
531 if (skb_ensure_writable(skb, l4csum_offset + sizeof(sum)) ||
532 skb_store_bits(skb, l4csum_offset, &sum, sizeof(sum)) < 0)
538 static int nft_payload_csum_inet(struct sk_buff *skb, const u32 *src,
539 __wsum fsum, __wsum tsum, int csum_offset)
543 if (skb_copy_bits(skb, csum_offset, &sum, sizeof(sum)) < 0)
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)
554 static void nft_payload_set_eval(const struct nft_expr *expr,
555 struct nft_regs *regs,
556 const struct nft_pktinfo *pkt)
558 const struct nft_payload_set *priv = nft_expr_priv(expr);
559 struct sk_buff *skb = pkt->skb;
560 const u32 *src = ®s->data[priv->sreg];
561 int offset, csum_offset;
564 switch (priv->base) {
565 case NFT_PAYLOAD_LL_HEADER:
566 if (!skb_mac_header_was_set(skb))
568 offset = skb_mac_header(skb) - skb->data;
570 case NFT_PAYLOAD_NETWORK_HEADER:
571 offset = skb_network_offset(skb);
573 case NFT_PAYLOAD_TRANSPORT_HEADER:
576 offset = pkt->xt.thoff;
582 csum_offset = offset + priv->csum_offset;
583 offset += priv->offset;
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);
591 if (priv->csum_type == NFT_PAYLOAD_CSUM_INET &&
592 nft_payload_csum_inet(skb, src, fsum, tsum, csum_offset))
595 if (priv->csum_flags &&
596 nft_payload_l4csum_update(pkt, skb, fsum, tsum) < 0)
600 if (skb_ensure_writable(skb, max(offset + priv->len, 0)) ||
601 skb_store_bits(skb, offset, src, priv->len) < 0)
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))
613 regs->verdict.code = NFT_BREAK;
616 static int nft_payload_set_init(const struct nft_ctx *ctx,
617 const struct nft_expr *expr,
618 const struct nlattr * const tb[])
620 struct nft_payload_set *priv = nft_expr_priv(expr);
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]);
627 if (tb[NFTA_PAYLOAD_CSUM_TYPE])
629 ntohl(nla_get_be32(tb[NFTA_PAYLOAD_CSUM_TYPE]));
630 if (tb[NFTA_PAYLOAD_CSUM_OFFSET])
632 ntohl(nla_get_be32(tb[NFTA_PAYLOAD_CSUM_OFFSET]));
633 if (tb[NFTA_PAYLOAD_CSUM_FLAGS]) {
636 flags = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_CSUM_FLAGS]));
637 if (flags & ~NFT_PAYLOAD_L4CSUM_PSEUDOHDR)
640 priv->csum_flags = flags;
643 switch (priv->csum_type) {
644 case NFT_PAYLOAD_CSUM_NONE:
645 case NFT_PAYLOAD_CSUM_INET:
647 case NFT_PAYLOAD_CSUM_SCTP:
648 if (priv->base != NFT_PAYLOAD_TRANSPORT_HEADER)
651 if (priv->csum_offset != offsetof(struct sctphdr, checksum))
658 return nft_validate_register_load(priv->sreg, priv->len);
661 static int nft_payload_set_dump(struct sk_buff *skb, const struct nft_expr *expr)
663 const struct nft_payload_set *priv = nft_expr_priv(expr);
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;
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,
688 static const struct nft_expr_ops *
689 nft_payload_select_ops(const struct nft_ctx *ctx,
690 const struct nlattr * const tb[])
692 enum nft_payload_bases base;
693 unsigned int offset, len;
695 if (tb[NFTA_PAYLOAD_BASE] == NULL ||
696 tb[NFTA_PAYLOAD_OFFSET] == NULL ||
697 tb[NFTA_PAYLOAD_LEN] == NULL)
698 return ERR_PTR(-EINVAL);
700 base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
702 case NFT_PAYLOAD_LL_HEADER:
703 case NFT_PAYLOAD_NETWORK_HEADER:
704 case NFT_PAYLOAD_TRANSPORT_HEADER:
707 return ERR_PTR(-EOPNOTSUPP);
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;
716 if (tb[NFTA_PAYLOAD_DREG] == NULL)
717 return ERR_PTR(-EINVAL);
719 offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
720 len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
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;
726 return &nft_payload_ops;
729 struct nft_expr_type nft_payload_type __read_mostly = {
731 .select_ops = nft_payload_select_ops,
732 .policy = nft_payload_policy,
733 .maxattr = NFTA_PAYLOAD_MAX,
734 .owner = THIS_MODULE,