1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kernel.h>
3 #include <linux/skbuff.h>
4 #include <linux/export.h>
6 #include <linux/ipv6.h>
7 #include <linux/if_vlan.h>
8 #include <linux/filter.h>
10 #include <net/dst_metadata.h>
16 #include <linux/igmp.h>
17 #include <linux/icmp.h>
18 #include <linux/sctp.h>
19 #include <linux/dccp.h>
20 #include <linux/if_tunnel.h>
21 #include <linux/if_pppox.h>
22 #include <linux/ppp_defs.h>
23 #include <linux/stddef.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_hsr.h>
26 #include <linux/mpls.h>
27 #include <linux/tcp.h>
28 #include <linux/ptp_classify.h>
29 #include <net/flow_dissector.h>
30 #include <net/pkt_cls.h>
31 #include <scsi/fc/fc_fcoe.h>
32 #include <uapi/linux/batadv_packet.h>
33 #include <linux/bpf.h>
34 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
35 #include <net/netfilter/nf_conntrack_core.h>
36 #include <net/netfilter/nf_conntrack_labels.h>
38 #include <linux/bpf-netns.h>
40 static void dissector_set_key(struct flow_dissector *flow_dissector,
41 enum flow_dissector_key_id key_id)
43 flow_dissector->used_keys |= (1 << key_id);
46 void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
47 const struct flow_dissector_key *key,
48 unsigned int key_count)
52 memset(flow_dissector, 0, sizeof(*flow_dissector));
54 for (i = 0; i < key_count; i++, key++) {
55 /* User should make sure that every key target offset is within
56 * boundaries of unsigned short.
58 BUG_ON(key->offset > USHRT_MAX);
59 BUG_ON(dissector_uses_key(flow_dissector,
62 dissector_set_key(flow_dissector, key->key_id);
63 flow_dissector->offset[key->key_id] = key->offset;
66 /* Ensure that the dissector always includes control and basic key.
67 * That way we are able to avoid handling lack of these in fast path.
69 BUG_ON(!dissector_uses_key(flow_dissector,
70 FLOW_DISSECTOR_KEY_CONTROL));
71 BUG_ON(!dissector_uses_key(flow_dissector,
72 FLOW_DISSECTOR_KEY_BASIC));
74 EXPORT_SYMBOL(skb_flow_dissector_init);
76 #ifdef CONFIG_BPF_SYSCALL
77 int flow_dissector_bpf_prog_attach_check(struct net *net,
78 struct bpf_prog *prog)
80 enum netns_bpf_attach_type type = NETNS_BPF_FLOW_DISSECTOR;
82 if (net == &init_net) {
83 /* BPF flow dissector in the root namespace overrides
84 * any per-net-namespace one. When attaching to root,
85 * make sure we don't have any BPF program attached
86 * to the non-root namespaces.
93 if (rcu_access_pointer(ns->bpf.run_array[type]))
97 /* Make sure root flow dissector is not attached
98 * when attaching to the non-root namespace.
100 if (rcu_access_pointer(init_net.bpf.run_array[type]))
106 #endif /* CONFIG_BPF_SYSCALL */
109 * __skb_flow_get_ports - extract the upper layer ports and return them
110 * @skb: sk_buff to extract the ports from
111 * @thoff: transport header offset
112 * @ip_proto: protocol for which to get port offset
113 * @data: raw buffer pointer to the packet, if NULL use skb->data
114 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
116 * The function will try to retrieve the ports at offset thoff + poff where poff
117 * is the protocol port offset returned from proto_ports_offset
119 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
120 const void *data, int hlen)
122 int poff = proto_ports_offset(ip_proto);
126 hlen = skb_headlen(skb);
130 __be32 *ports, _ports;
132 ports = __skb_header_pointer(skb, thoff + poff,
133 sizeof(_ports), data, hlen, &_ports);
140 EXPORT_SYMBOL(__skb_flow_get_ports);
142 static bool icmp_has_id(u8 type)
148 case ICMP_TIMESTAMPREPLY:
149 case ICMPV6_ECHO_REQUEST:
150 case ICMPV6_ECHO_REPLY:
158 * skb_flow_get_icmp_tci - extract ICMP(6) Type, Code and Identifier fields
159 * @skb: sk_buff to extract from
160 * @key_icmp: struct flow_dissector_key_icmp to fill
161 * @data: raw buffer pointer to the packet
162 * @thoff: offset to extract at
163 * @hlen: packet header length
165 void skb_flow_get_icmp_tci(const struct sk_buff *skb,
166 struct flow_dissector_key_icmp *key_icmp,
167 const void *data, int thoff, int hlen)
169 struct icmphdr *ih, _ih;
171 ih = __skb_header_pointer(skb, thoff, sizeof(_ih), data, hlen, &_ih);
175 key_icmp->type = ih->type;
176 key_icmp->code = ih->code;
178 /* As we use 0 to signal that the Id field is not present,
179 * avoid confusion with packets without such field
181 if (icmp_has_id(ih->type))
182 key_icmp->id = ih->un.echo.id ? ntohs(ih->un.echo.id) : 1;
186 EXPORT_SYMBOL(skb_flow_get_icmp_tci);
188 /* If FLOW_DISSECTOR_KEY_ICMP is set, dissect an ICMP packet
189 * using skb_flow_get_icmp_tci().
191 static void __skb_flow_dissect_icmp(const struct sk_buff *skb,
192 struct flow_dissector *flow_dissector,
193 void *target_container, const void *data,
196 struct flow_dissector_key_icmp *key_icmp;
198 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ICMP))
201 key_icmp = skb_flow_dissector_target(flow_dissector,
202 FLOW_DISSECTOR_KEY_ICMP,
205 skb_flow_get_icmp_tci(skb, key_icmp, data, thoff, hlen);
208 static void __skb_flow_dissect_l2tpv3(const struct sk_buff *skb,
209 struct flow_dissector *flow_dissector,
210 void *target_container, const void *data,
213 struct flow_dissector_key_l2tpv3 *key_l2tpv3;
218 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_L2TPV3))
221 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
225 key_l2tpv3 = skb_flow_dissector_target(flow_dissector,
226 FLOW_DISSECTOR_KEY_L2TPV3,
229 key_l2tpv3->session_id = hdr->session_id;
232 void skb_flow_dissect_meta(const struct sk_buff *skb,
233 struct flow_dissector *flow_dissector,
234 void *target_container)
236 struct flow_dissector_key_meta *meta;
238 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_META))
241 meta = skb_flow_dissector_target(flow_dissector,
242 FLOW_DISSECTOR_KEY_META,
244 meta->ingress_ifindex = skb->skb_iif;
245 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
246 if (tc_skb_ext_tc_enabled()) {
247 struct tc_skb_ext *ext;
249 ext = skb_ext_find(skb, TC_SKB_EXT);
251 meta->l2_miss = ext->l2_miss;
255 EXPORT_SYMBOL(skb_flow_dissect_meta);
258 skb_flow_dissect_set_enc_addr_type(enum flow_dissector_key_id type,
259 struct flow_dissector *flow_dissector,
260 void *target_container)
262 struct flow_dissector_key_control *ctrl;
264 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL))
267 ctrl = skb_flow_dissector_target(flow_dissector,
268 FLOW_DISSECTOR_KEY_ENC_CONTROL,
270 ctrl->addr_type = type;
274 skb_flow_dissect_ct(const struct sk_buff *skb,
275 struct flow_dissector *flow_dissector,
276 void *target_container, u16 *ctinfo_map,
277 size_t mapsize, bool post_ct, u16 zone)
279 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
280 struct flow_dissector_key_ct *key;
281 enum ip_conntrack_info ctinfo;
282 struct nf_conn_labels *cl;
285 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_CT))
288 ct = nf_ct_get(skb, &ctinfo);
292 key = skb_flow_dissector_target(flow_dissector,
293 FLOW_DISSECTOR_KEY_CT,
297 key->ct_state = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
298 TCA_FLOWER_KEY_CT_FLAGS_INVALID;
303 if (ctinfo < mapsize)
304 key->ct_state = ctinfo_map[ctinfo];
305 #if IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)
306 key->ct_zone = ct->zone.id;
308 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
309 key->ct_mark = READ_ONCE(ct->mark);
312 cl = nf_ct_labels_find(ct);
314 memcpy(key->ct_labels, cl->bits, sizeof(key->ct_labels));
315 #endif /* CONFIG_NF_CONNTRACK */
317 EXPORT_SYMBOL(skb_flow_dissect_ct);
320 skb_flow_dissect_tunnel_info(const struct sk_buff *skb,
321 struct flow_dissector *flow_dissector,
322 void *target_container)
324 struct ip_tunnel_info *info;
325 struct ip_tunnel_key *key;
327 /* A quick check to see if there might be something to do. */
328 if (!dissector_uses_key(flow_dissector,
329 FLOW_DISSECTOR_KEY_ENC_KEYID) &&
330 !dissector_uses_key(flow_dissector,
331 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) &&
332 !dissector_uses_key(flow_dissector,
333 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) &&
334 !dissector_uses_key(flow_dissector,
335 FLOW_DISSECTOR_KEY_ENC_CONTROL) &&
336 !dissector_uses_key(flow_dissector,
337 FLOW_DISSECTOR_KEY_ENC_PORTS) &&
338 !dissector_uses_key(flow_dissector,
339 FLOW_DISSECTOR_KEY_ENC_IP) &&
340 !dissector_uses_key(flow_dissector,
341 FLOW_DISSECTOR_KEY_ENC_OPTS))
344 info = skb_tunnel_info(skb);
350 switch (ip_tunnel_info_af(info)) {
352 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV4_ADDRS,
355 if (dissector_uses_key(flow_dissector,
356 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) {
357 struct flow_dissector_key_ipv4_addrs *ipv4;
359 ipv4 = skb_flow_dissector_target(flow_dissector,
360 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
362 ipv4->src = key->u.ipv4.src;
363 ipv4->dst = key->u.ipv4.dst;
367 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV6_ADDRS,
370 if (dissector_uses_key(flow_dissector,
371 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS)) {
372 struct flow_dissector_key_ipv6_addrs *ipv6;
374 ipv6 = skb_flow_dissector_target(flow_dissector,
375 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
377 ipv6->src = key->u.ipv6.src;
378 ipv6->dst = key->u.ipv6.dst;
383 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
384 struct flow_dissector_key_keyid *keyid;
386 keyid = skb_flow_dissector_target(flow_dissector,
387 FLOW_DISSECTOR_KEY_ENC_KEYID,
389 keyid->keyid = tunnel_id_to_key32(key->tun_id);
392 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
393 struct flow_dissector_key_ports *tp;
395 tp = skb_flow_dissector_target(flow_dissector,
396 FLOW_DISSECTOR_KEY_ENC_PORTS,
398 tp->src = key->tp_src;
399 tp->dst = key->tp_dst;
402 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_IP)) {
403 struct flow_dissector_key_ip *ip;
405 ip = skb_flow_dissector_target(flow_dissector,
406 FLOW_DISSECTOR_KEY_ENC_IP,
412 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_OPTS)) {
413 struct flow_dissector_key_enc_opts *enc_opt;
415 enc_opt = skb_flow_dissector_target(flow_dissector,
416 FLOW_DISSECTOR_KEY_ENC_OPTS,
419 if (info->options_len) {
420 enc_opt->len = info->options_len;
421 ip_tunnel_info_opts_get(enc_opt->data, info);
422 enc_opt->dst_opt_type = info->key.tun_flags &
423 TUNNEL_OPTIONS_PRESENT;
427 EXPORT_SYMBOL(skb_flow_dissect_tunnel_info);
429 void skb_flow_dissect_hash(const struct sk_buff *skb,
430 struct flow_dissector *flow_dissector,
431 void *target_container)
433 struct flow_dissector_key_hash *key;
435 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_HASH))
438 key = skb_flow_dissector_target(flow_dissector,
439 FLOW_DISSECTOR_KEY_HASH,
442 key->hash = skb_get_hash_raw(skb);
444 EXPORT_SYMBOL(skb_flow_dissect_hash);
446 static enum flow_dissect_ret
447 __skb_flow_dissect_mpls(const struct sk_buff *skb,
448 struct flow_dissector *flow_dissector,
449 void *target_container, const void *data, int nhoff,
450 int hlen, int lse_index, bool *entropy_label)
452 struct mpls_label *hdr, _hdr;
453 u32 entry, label, bos;
455 if (!dissector_uses_key(flow_dissector,
456 FLOW_DISSECTOR_KEY_MPLS_ENTROPY) &&
457 !dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS))
458 return FLOW_DISSECT_RET_OUT_GOOD;
460 if (lse_index >= FLOW_DIS_MPLS_MAX)
461 return FLOW_DISSECT_RET_OUT_GOOD;
463 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
466 return FLOW_DISSECT_RET_OUT_BAD;
468 entry = ntohl(hdr->entry);
469 label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
470 bos = (entry & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT;
472 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) {
473 struct flow_dissector_key_mpls *key_mpls;
474 struct flow_dissector_mpls_lse *lse;
476 key_mpls = skb_flow_dissector_target(flow_dissector,
477 FLOW_DISSECTOR_KEY_MPLS,
479 lse = &key_mpls->ls[lse_index];
481 lse->mpls_ttl = (entry & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
483 lse->mpls_tc = (entry & MPLS_LS_TC_MASK) >> MPLS_LS_TC_SHIFT;
484 lse->mpls_label = label;
485 dissector_set_mpls_lse(key_mpls, lse_index);
488 if (*entropy_label &&
489 dissector_uses_key(flow_dissector,
490 FLOW_DISSECTOR_KEY_MPLS_ENTROPY)) {
491 struct flow_dissector_key_keyid *key_keyid;
493 key_keyid = skb_flow_dissector_target(flow_dissector,
494 FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
496 key_keyid->keyid = cpu_to_be32(label);
499 *entropy_label = label == MPLS_LABEL_ENTROPY;
501 return bos ? FLOW_DISSECT_RET_OUT_GOOD : FLOW_DISSECT_RET_PROTO_AGAIN;
504 static enum flow_dissect_ret
505 __skb_flow_dissect_arp(const struct sk_buff *skb,
506 struct flow_dissector *flow_dissector,
507 void *target_container, const void *data,
510 struct flow_dissector_key_arp *key_arp;
512 unsigned char ar_sha[ETH_ALEN];
513 unsigned char ar_sip[4];
514 unsigned char ar_tha[ETH_ALEN];
515 unsigned char ar_tip[4];
516 } *arp_eth, _arp_eth;
517 const struct arphdr *arp;
520 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ARP))
521 return FLOW_DISSECT_RET_OUT_GOOD;
523 arp = __skb_header_pointer(skb, nhoff, sizeof(_arp), data,
526 return FLOW_DISSECT_RET_OUT_BAD;
528 if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
529 arp->ar_pro != htons(ETH_P_IP) ||
530 arp->ar_hln != ETH_ALEN ||
532 (arp->ar_op != htons(ARPOP_REPLY) &&
533 arp->ar_op != htons(ARPOP_REQUEST)))
534 return FLOW_DISSECT_RET_OUT_BAD;
536 arp_eth = __skb_header_pointer(skb, nhoff + sizeof(_arp),
537 sizeof(_arp_eth), data,
540 return FLOW_DISSECT_RET_OUT_BAD;
542 key_arp = skb_flow_dissector_target(flow_dissector,
543 FLOW_DISSECTOR_KEY_ARP,
546 memcpy(&key_arp->sip, arp_eth->ar_sip, sizeof(key_arp->sip));
547 memcpy(&key_arp->tip, arp_eth->ar_tip, sizeof(key_arp->tip));
549 /* Only store the lower byte of the opcode;
550 * this covers ARPOP_REPLY and ARPOP_REQUEST.
552 key_arp->op = ntohs(arp->ar_op) & 0xff;
554 ether_addr_copy(key_arp->sha, arp_eth->ar_sha);
555 ether_addr_copy(key_arp->tha, arp_eth->ar_tha);
557 return FLOW_DISSECT_RET_OUT_GOOD;
560 static enum flow_dissect_ret
561 __skb_flow_dissect_gre(const struct sk_buff *skb,
562 struct flow_dissector_key_control *key_control,
563 struct flow_dissector *flow_dissector,
564 void *target_container, const void *data,
565 __be16 *p_proto, int *p_nhoff, int *p_hlen,
568 struct flow_dissector_key_keyid *key_keyid;
569 struct gre_base_hdr *hdr, _hdr;
573 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr),
574 data, *p_hlen, &_hdr);
576 return FLOW_DISSECT_RET_OUT_BAD;
578 /* Only look inside GRE without routing */
579 if (hdr->flags & GRE_ROUTING)
580 return FLOW_DISSECT_RET_OUT_GOOD;
582 /* Only look inside GRE for version 0 and 1 */
583 gre_ver = ntohs(hdr->flags & GRE_VERSION);
585 return FLOW_DISSECT_RET_OUT_GOOD;
587 *p_proto = hdr->protocol;
589 /* Version1 must be PPTP, and check the flags */
590 if (!(*p_proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY)))
591 return FLOW_DISSECT_RET_OUT_GOOD;
594 offset += sizeof(struct gre_base_hdr);
596 if (hdr->flags & GRE_CSUM)
597 offset += sizeof_field(struct gre_full_hdr, csum) +
598 sizeof_field(struct gre_full_hdr, reserved1);
600 if (hdr->flags & GRE_KEY) {
604 keyid = __skb_header_pointer(skb, *p_nhoff + offset,
606 data, *p_hlen, &_keyid);
608 return FLOW_DISSECT_RET_OUT_BAD;
610 if (dissector_uses_key(flow_dissector,
611 FLOW_DISSECTOR_KEY_GRE_KEYID)) {
612 key_keyid = skb_flow_dissector_target(flow_dissector,
613 FLOW_DISSECTOR_KEY_GRE_KEYID,
616 key_keyid->keyid = *keyid;
618 key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK;
620 offset += sizeof_field(struct gre_full_hdr, key);
623 if (hdr->flags & GRE_SEQ)
624 offset += sizeof_field(struct pptp_gre_header, seq);
627 if (*p_proto == htons(ETH_P_TEB)) {
628 const struct ethhdr *eth;
631 eth = __skb_header_pointer(skb, *p_nhoff + offset,
633 data, *p_hlen, &_eth);
635 return FLOW_DISSECT_RET_OUT_BAD;
636 *p_proto = eth->h_proto;
637 offset += sizeof(*eth);
639 /* Cap headers that we access via pointers at the
640 * end of the Ethernet header as our maximum alignment
641 * at that point is only 2 bytes.
644 *p_hlen = *p_nhoff + offset;
646 } else { /* version 1, must be PPTP */
647 u8 _ppp_hdr[PPP_HDRLEN];
650 if (hdr->flags & GRE_ACK)
651 offset += sizeof_field(struct pptp_gre_header, ack);
653 ppp_hdr = __skb_header_pointer(skb, *p_nhoff + offset,
655 data, *p_hlen, _ppp_hdr);
657 return FLOW_DISSECT_RET_OUT_BAD;
659 switch (PPP_PROTOCOL(ppp_hdr)) {
661 *p_proto = htons(ETH_P_IP);
664 *p_proto = htons(ETH_P_IPV6);
667 /* Could probably catch some more like MPLS */
671 offset += PPP_HDRLEN;
675 key_control->flags |= FLOW_DIS_ENCAPSULATION;
676 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
677 return FLOW_DISSECT_RET_OUT_GOOD;
679 return FLOW_DISSECT_RET_PROTO_AGAIN;
683 * __skb_flow_dissect_batadv() - dissect batman-adv header
684 * @skb: sk_buff to with the batman-adv header
685 * @key_control: flow dissectors control key
686 * @data: raw buffer pointer to the packet, if NULL use skb->data
687 * @p_proto: pointer used to update the protocol to process next
688 * @p_nhoff: pointer used to update inner network header offset
689 * @hlen: packet header length
690 * @flags: any combination of FLOW_DISSECTOR_F_*
692 * ETH_P_BATMAN packets are tried to be dissected. Only
693 * &struct batadv_unicast packets are actually processed because they contain an
694 * inner ethernet header and are usually followed by actual network header. This
695 * allows the flow dissector to continue processing the packet.
697 * Return: FLOW_DISSECT_RET_PROTO_AGAIN when &struct batadv_unicast was found,
698 * FLOW_DISSECT_RET_OUT_GOOD when dissector should stop after encapsulation,
699 * otherwise FLOW_DISSECT_RET_OUT_BAD
701 static enum flow_dissect_ret
702 __skb_flow_dissect_batadv(const struct sk_buff *skb,
703 struct flow_dissector_key_control *key_control,
704 const void *data, __be16 *p_proto, int *p_nhoff,
705 int hlen, unsigned int flags)
708 struct batadv_unicast_packet batadv_unicast;
712 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr), data, hlen,
715 return FLOW_DISSECT_RET_OUT_BAD;
717 if (hdr->batadv_unicast.version != BATADV_COMPAT_VERSION)
718 return FLOW_DISSECT_RET_OUT_BAD;
720 if (hdr->batadv_unicast.packet_type != BATADV_UNICAST)
721 return FLOW_DISSECT_RET_OUT_BAD;
723 *p_proto = hdr->eth.h_proto;
724 *p_nhoff += sizeof(*hdr);
726 key_control->flags |= FLOW_DIS_ENCAPSULATION;
727 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
728 return FLOW_DISSECT_RET_OUT_GOOD;
730 return FLOW_DISSECT_RET_PROTO_AGAIN;
734 __skb_flow_dissect_tcp(const struct sk_buff *skb,
735 struct flow_dissector *flow_dissector,
736 void *target_container, const void *data,
739 struct flow_dissector_key_tcp *key_tcp;
740 struct tcphdr *th, _th;
742 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_TCP))
745 th = __skb_header_pointer(skb, thoff, sizeof(_th), data, hlen, &_th);
749 if (unlikely(__tcp_hdrlen(th) < sizeof(_th)))
752 key_tcp = skb_flow_dissector_target(flow_dissector,
753 FLOW_DISSECTOR_KEY_TCP,
755 key_tcp->flags = (*(__be16 *) &tcp_flag_word(th) & htons(0x0FFF));
759 __skb_flow_dissect_ports(const struct sk_buff *skb,
760 struct flow_dissector *flow_dissector,
761 void *target_container, const void *data,
762 int nhoff, u8 ip_proto, int hlen)
764 enum flow_dissector_key_id dissector_ports = FLOW_DISSECTOR_KEY_MAX;
765 struct flow_dissector_key_ports *key_ports;
767 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS))
768 dissector_ports = FLOW_DISSECTOR_KEY_PORTS;
769 else if (dissector_uses_key(flow_dissector,
770 FLOW_DISSECTOR_KEY_PORTS_RANGE))
771 dissector_ports = FLOW_DISSECTOR_KEY_PORTS_RANGE;
773 if (dissector_ports == FLOW_DISSECTOR_KEY_MAX)
776 key_ports = skb_flow_dissector_target(flow_dissector,
779 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
784 __skb_flow_dissect_ipv4(const struct sk_buff *skb,
785 struct flow_dissector *flow_dissector,
786 void *target_container, const void *data,
787 const struct iphdr *iph)
789 struct flow_dissector_key_ip *key_ip;
791 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
794 key_ip = skb_flow_dissector_target(flow_dissector,
795 FLOW_DISSECTOR_KEY_IP,
797 key_ip->tos = iph->tos;
798 key_ip->ttl = iph->ttl;
802 __skb_flow_dissect_ipv6(const struct sk_buff *skb,
803 struct flow_dissector *flow_dissector,
804 void *target_container, const void *data,
805 const struct ipv6hdr *iph)
807 struct flow_dissector_key_ip *key_ip;
809 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
812 key_ip = skb_flow_dissector_target(flow_dissector,
813 FLOW_DISSECTOR_KEY_IP,
815 key_ip->tos = ipv6_get_dsfield(iph);
816 key_ip->ttl = iph->hop_limit;
819 /* Maximum number of protocol headers that can be parsed in
822 #define MAX_FLOW_DISSECT_HDRS 15
824 static bool skb_flow_dissect_allowed(int *num_hdrs)
828 return (*num_hdrs <= MAX_FLOW_DISSECT_HDRS);
831 static void __skb_flow_bpf_to_target(const struct bpf_flow_keys *flow_keys,
832 struct flow_dissector *flow_dissector,
833 void *target_container)
835 struct flow_dissector_key_ports *key_ports = NULL;
836 struct flow_dissector_key_control *key_control;
837 struct flow_dissector_key_basic *key_basic;
838 struct flow_dissector_key_addrs *key_addrs;
839 struct flow_dissector_key_tags *key_tags;
841 key_control = skb_flow_dissector_target(flow_dissector,
842 FLOW_DISSECTOR_KEY_CONTROL,
844 key_control->thoff = flow_keys->thoff;
845 if (flow_keys->is_frag)
846 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
847 if (flow_keys->is_first_frag)
848 key_control->flags |= FLOW_DIS_FIRST_FRAG;
849 if (flow_keys->is_encap)
850 key_control->flags |= FLOW_DIS_ENCAPSULATION;
852 key_basic = skb_flow_dissector_target(flow_dissector,
853 FLOW_DISSECTOR_KEY_BASIC,
855 key_basic->n_proto = flow_keys->n_proto;
856 key_basic->ip_proto = flow_keys->ip_proto;
858 if (flow_keys->addr_proto == ETH_P_IP &&
859 dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
860 key_addrs = skb_flow_dissector_target(flow_dissector,
861 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
863 key_addrs->v4addrs.src = flow_keys->ipv4_src;
864 key_addrs->v4addrs.dst = flow_keys->ipv4_dst;
865 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
866 } else if (flow_keys->addr_proto == ETH_P_IPV6 &&
867 dissector_uses_key(flow_dissector,
868 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
869 key_addrs = skb_flow_dissector_target(flow_dissector,
870 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
872 memcpy(&key_addrs->v6addrs.src, &flow_keys->ipv6_src,
873 sizeof(key_addrs->v6addrs.src));
874 memcpy(&key_addrs->v6addrs.dst, &flow_keys->ipv6_dst,
875 sizeof(key_addrs->v6addrs.dst));
876 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
879 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS))
880 key_ports = skb_flow_dissector_target(flow_dissector,
881 FLOW_DISSECTOR_KEY_PORTS,
883 else if (dissector_uses_key(flow_dissector,
884 FLOW_DISSECTOR_KEY_PORTS_RANGE))
885 key_ports = skb_flow_dissector_target(flow_dissector,
886 FLOW_DISSECTOR_KEY_PORTS_RANGE,
890 key_ports->src = flow_keys->sport;
891 key_ports->dst = flow_keys->dport;
894 if (dissector_uses_key(flow_dissector,
895 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
896 key_tags = skb_flow_dissector_target(flow_dissector,
897 FLOW_DISSECTOR_KEY_FLOW_LABEL,
899 key_tags->flow_label = ntohl(flow_keys->flow_label);
903 u32 bpf_flow_dissect(struct bpf_prog *prog, struct bpf_flow_dissector *ctx,
904 __be16 proto, int nhoff, int hlen, unsigned int flags)
906 struct bpf_flow_keys *flow_keys = ctx->flow_keys;
909 /* Pass parameters to the BPF program */
910 memset(flow_keys, 0, sizeof(*flow_keys));
911 flow_keys->n_proto = proto;
912 flow_keys->nhoff = nhoff;
913 flow_keys->thoff = flow_keys->nhoff;
915 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG !=
916 (int)FLOW_DISSECTOR_F_PARSE_1ST_FRAG);
917 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL !=
918 (int)FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
919 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP !=
920 (int)FLOW_DISSECTOR_F_STOP_AT_ENCAP);
921 flow_keys->flags = flags;
923 result = bpf_prog_run_pin_on_cpu(prog, ctx);
925 flow_keys->nhoff = clamp_t(u16, flow_keys->nhoff, nhoff, hlen);
926 flow_keys->thoff = clamp_t(u16, flow_keys->thoff,
927 flow_keys->nhoff, hlen);
932 static bool is_pppoe_ses_hdr_valid(const struct pppoe_hdr *hdr)
934 return hdr->ver == 1 && hdr->type == 1 && hdr->code == 0;
938 * __skb_flow_dissect - extract the flow_keys struct and return it
939 * @net: associated network namespace, derived from @skb if NULL
940 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
941 * @flow_dissector: list of keys to dissect
942 * @target_container: target structure to put dissected values into
943 * @data: raw buffer pointer to the packet, if NULL use skb->data
944 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
945 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
946 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
947 * @flags: flags that control the dissection process, e.g.
948 * FLOW_DISSECTOR_F_STOP_AT_ENCAP.
950 * The function will try to retrieve individual keys into target specified
951 * by flow_dissector from either the skbuff or a raw buffer specified by the
954 * Caller must take care of zeroing target container memory.
956 bool __skb_flow_dissect(const struct net *net,
957 const struct sk_buff *skb,
958 struct flow_dissector *flow_dissector,
959 void *target_container, const void *data,
960 __be16 proto, int nhoff, int hlen, unsigned int flags)
962 struct flow_dissector_key_control *key_control;
963 struct flow_dissector_key_basic *key_basic;
964 struct flow_dissector_key_addrs *key_addrs;
965 struct flow_dissector_key_tags *key_tags;
966 struct flow_dissector_key_vlan *key_vlan;
967 enum flow_dissect_ret fdret;
968 enum flow_dissector_key_id dissector_vlan = FLOW_DISSECTOR_KEY_MAX;
969 bool mpls_el = false;
977 proto = skb_vlan_tag_present(skb) ?
978 skb->vlan_proto : skb->protocol;
979 nhoff = skb_network_offset(skb);
980 hlen = skb_headlen(skb);
981 #if IS_ENABLED(CONFIG_NET_DSA)
982 if (unlikely(skb->dev && netdev_uses_dsa(skb->dev) &&
983 proto == htons(ETH_P_XDSA))) {
984 struct metadata_dst *md_dst = skb_metadata_dst(skb);
985 const struct dsa_device_ops *ops;
988 ops = skb->dev->dsa_ptr->tag_ops;
989 /* Only DSA header taggers break flow dissection */
990 if (ops->needed_headroom &&
991 (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)) {
992 if (ops->flow_dissect)
993 ops->flow_dissect(skb, &proto, &offset);
995 dsa_tag_generic_flow_dissect(skb,
1005 /* It is ensured by skb_flow_dissector_init() that control key will
1006 * be always present.
1008 key_control = skb_flow_dissector_target(flow_dissector,
1009 FLOW_DISSECTOR_KEY_CONTROL,
1012 /* It is ensured by skb_flow_dissector_init() that basic key will
1013 * be always present.
1015 key_basic = skb_flow_dissector_target(flow_dissector,
1016 FLOW_DISSECTOR_KEY_BASIC,
1022 net = dev_net(skb->dev);
1024 net = sock_net(skb->sk);
1030 enum netns_bpf_attach_type type = NETNS_BPF_FLOW_DISSECTOR;
1031 struct bpf_prog_array *run_array;
1034 run_array = rcu_dereference(init_net.bpf.run_array[type]);
1036 run_array = rcu_dereference(net->bpf.run_array[type]);
1039 struct bpf_flow_keys flow_keys;
1040 struct bpf_flow_dissector ctx = {
1041 .flow_keys = &flow_keys,
1043 .data_end = data + hlen,
1045 __be16 n_proto = proto;
1046 struct bpf_prog *prog;
1051 /* we can't use 'proto' in the skb case
1052 * because it might be set to skb->vlan_proto
1053 * which has been pulled from the data
1055 n_proto = skb->protocol;
1058 prog = READ_ONCE(run_array->items[0].prog);
1059 result = bpf_flow_dissect(prog, &ctx, n_proto, nhoff,
1061 if (result == BPF_FLOW_DISSECTOR_CONTINUE)
1062 goto dissect_continue;
1063 __skb_flow_bpf_to_target(&flow_keys, flow_dissector,
1066 return result == BPF_OK;
1072 if (dissector_uses_key(flow_dissector,
1073 FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1074 struct ethhdr *eth = eth_hdr(skb);
1075 struct flow_dissector_key_eth_addrs *key_eth_addrs;
1077 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
1078 FLOW_DISSECTOR_KEY_ETH_ADDRS,
1080 memcpy(key_eth_addrs, eth, sizeof(*key_eth_addrs));
1083 if (dissector_uses_key(flow_dissector,
1084 FLOW_DISSECTOR_KEY_NUM_OF_VLANS)) {
1085 struct flow_dissector_key_num_of_vlans *key_num_of_vlans;
1087 key_num_of_vlans = skb_flow_dissector_target(flow_dissector,
1088 FLOW_DISSECTOR_KEY_NUM_OF_VLANS,
1090 key_num_of_vlans->num_of_vlans = 0;
1094 fdret = FLOW_DISSECT_RET_CONTINUE;
1097 case htons(ETH_P_IP): {
1098 const struct iphdr *iph;
1101 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
1102 if (!iph || iph->ihl < 5) {
1103 fdret = FLOW_DISSECT_RET_OUT_BAD;
1107 nhoff += iph->ihl * 4;
1109 ip_proto = iph->protocol;
1111 if (dissector_uses_key(flow_dissector,
1112 FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
1113 key_addrs = skb_flow_dissector_target(flow_dissector,
1114 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1117 memcpy(&key_addrs->v4addrs.src, &iph->saddr,
1118 sizeof(key_addrs->v4addrs.src));
1119 memcpy(&key_addrs->v4addrs.dst, &iph->daddr,
1120 sizeof(key_addrs->v4addrs.dst));
1121 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1124 __skb_flow_dissect_ipv4(skb, flow_dissector,
1125 target_container, data, iph);
1127 if (ip_is_fragment(iph)) {
1128 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
1130 if (iph->frag_off & htons(IP_OFFSET)) {
1131 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1134 key_control->flags |= FLOW_DIS_FIRST_FRAG;
1136 FLOW_DISSECTOR_F_PARSE_1ST_FRAG)) {
1137 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1145 case htons(ETH_P_IPV6): {
1146 const struct ipv6hdr *iph;
1147 struct ipv6hdr _iph;
1149 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
1151 fdret = FLOW_DISSECT_RET_OUT_BAD;
1155 ip_proto = iph->nexthdr;
1156 nhoff += sizeof(struct ipv6hdr);
1158 if (dissector_uses_key(flow_dissector,
1159 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
1160 key_addrs = skb_flow_dissector_target(flow_dissector,
1161 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1164 memcpy(&key_addrs->v6addrs.src, &iph->saddr,
1165 sizeof(key_addrs->v6addrs.src));
1166 memcpy(&key_addrs->v6addrs.dst, &iph->daddr,
1167 sizeof(key_addrs->v6addrs.dst));
1168 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1171 if ((dissector_uses_key(flow_dissector,
1172 FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
1173 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
1174 ip6_flowlabel(iph)) {
1175 __be32 flow_label = ip6_flowlabel(iph);
1177 if (dissector_uses_key(flow_dissector,
1178 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
1179 key_tags = skb_flow_dissector_target(flow_dissector,
1180 FLOW_DISSECTOR_KEY_FLOW_LABEL,
1182 key_tags->flow_label = ntohl(flow_label);
1184 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL) {
1185 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1190 __skb_flow_dissect_ipv6(skb, flow_dissector,
1191 target_container, data, iph);
1195 case htons(ETH_P_8021AD):
1196 case htons(ETH_P_8021Q): {
1197 const struct vlan_hdr *vlan = NULL;
1198 struct vlan_hdr _vlan;
1199 __be16 saved_vlan_tpid = proto;
1201 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX &&
1202 skb && skb_vlan_tag_present(skb)) {
1203 proto = skb->protocol;
1205 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
1206 data, hlen, &_vlan);
1208 fdret = FLOW_DISSECT_RET_OUT_BAD;
1212 proto = vlan->h_vlan_encapsulated_proto;
1213 nhoff += sizeof(*vlan);
1216 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_NUM_OF_VLANS) &&
1217 !(key_control->flags & FLOW_DIS_ENCAPSULATION)) {
1218 struct flow_dissector_key_num_of_vlans *key_nvs;
1220 key_nvs = skb_flow_dissector_target(flow_dissector,
1221 FLOW_DISSECTOR_KEY_NUM_OF_VLANS,
1223 key_nvs->num_of_vlans++;
1226 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX) {
1227 dissector_vlan = FLOW_DISSECTOR_KEY_VLAN;
1228 } else if (dissector_vlan == FLOW_DISSECTOR_KEY_VLAN) {
1229 dissector_vlan = FLOW_DISSECTOR_KEY_CVLAN;
1231 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1235 if (dissector_uses_key(flow_dissector, dissector_vlan)) {
1236 key_vlan = skb_flow_dissector_target(flow_dissector,
1241 key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
1242 key_vlan->vlan_priority = skb_vlan_tag_get_prio(skb);
1244 key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) &
1246 key_vlan->vlan_priority =
1247 (ntohs(vlan->h_vlan_TCI) &
1248 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
1250 key_vlan->vlan_tpid = saved_vlan_tpid;
1251 key_vlan->vlan_eth_type = proto;
1254 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1257 case htons(ETH_P_PPP_SES): {
1259 struct pppoe_hdr hdr;
1264 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
1266 fdret = FLOW_DISSECT_RET_OUT_BAD;
1270 if (!is_pppoe_ses_hdr_valid(&hdr->hdr)) {
1271 fdret = FLOW_DISSECT_RET_OUT_BAD;
1275 /* least significant bit of the most significant octet
1276 * indicates if protocol field was compressed
1278 ppp_proto = ntohs(hdr->proto);
1279 if (ppp_proto & 0x0100) {
1280 ppp_proto = ppp_proto >> 8;
1281 nhoff += PPPOE_SES_HLEN - 1;
1283 nhoff += PPPOE_SES_HLEN;
1286 if (ppp_proto == PPP_IP) {
1287 proto = htons(ETH_P_IP);
1288 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1289 } else if (ppp_proto == PPP_IPV6) {
1290 proto = htons(ETH_P_IPV6);
1291 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1292 } else if (ppp_proto == PPP_MPLS_UC) {
1293 proto = htons(ETH_P_MPLS_UC);
1294 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1295 } else if (ppp_proto == PPP_MPLS_MC) {
1296 proto = htons(ETH_P_MPLS_MC);
1297 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1298 } else if (ppp_proto_is_valid(ppp_proto)) {
1299 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1301 fdret = FLOW_DISSECT_RET_OUT_BAD;
1305 if (dissector_uses_key(flow_dissector,
1306 FLOW_DISSECTOR_KEY_PPPOE)) {
1307 struct flow_dissector_key_pppoe *key_pppoe;
1309 key_pppoe = skb_flow_dissector_target(flow_dissector,
1310 FLOW_DISSECTOR_KEY_PPPOE,
1312 key_pppoe->session_id = hdr->hdr.sid;
1313 key_pppoe->ppp_proto = htons(ppp_proto);
1314 key_pppoe->type = htons(ETH_P_PPP_SES);
1318 case htons(ETH_P_TIPC): {
1319 struct tipc_basic_hdr *hdr, _hdr;
1321 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr),
1324 fdret = FLOW_DISSECT_RET_OUT_BAD;
1328 if (dissector_uses_key(flow_dissector,
1329 FLOW_DISSECTOR_KEY_TIPC)) {
1330 key_addrs = skb_flow_dissector_target(flow_dissector,
1331 FLOW_DISSECTOR_KEY_TIPC,
1333 key_addrs->tipckey.key = tipc_hdr_rps_key(hdr);
1334 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC;
1336 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1340 case htons(ETH_P_MPLS_UC):
1341 case htons(ETH_P_MPLS_MC):
1342 fdret = __skb_flow_dissect_mpls(skb, flow_dissector,
1343 target_container, data,
1344 nhoff, hlen, mpls_lse,
1346 nhoff += sizeof(struct mpls_label);
1349 case htons(ETH_P_FCOE):
1350 if ((hlen - nhoff) < FCOE_HEADER_LEN) {
1351 fdret = FLOW_DISSECT_RET_OUT_BAD;
1355 nhoff += FCOE_HEADER_LEN;
1356 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1359 case htons(ETH_P_ARP):
1360 case htons(ETH_P_RARP):
1361 fdret = __skb_flow_dissect_arp(skb, flow_dissector,
1362 target_container, data,
1366 case htons(ETH_P_BATMAN):
1367 fdret = __skb_flow_dissect_batadv(skb, key_control, data,
1368 &proto, &nhoff, hlen, flags);
1371 case htons(ETH_P_1588): {
1372 struct ptp_header *hdr, _hdr;
1374 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
1377 fdret = FLOW_DISSECT_RET_OUT_BAD;
1381 nhoff += ntohs(hdr->message_length);
1382 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1386 case htons(ETH_P_PRP):
1387 case htons(ETH_P_HSR): {
1388 struct hsr_tag *hdr, _hdr;
1390 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen,
1393 fdret = FLOW_DISSECT_RET_OUT_BAD;
1397 proto = hdr->encap_proto;
1399 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1404 fdret = FLOW_DISSECT_RET_OUT_BAD;
1408 /* Process result of proto processing */
1410 case FLOW_DISSECT_RET_OUT_GOOD:
1412 case FLOW_DISSECT_RET_PROTO_AGAIN:
1413 if (skb_flow_dissect_allowed(&num_hdrs))
1416 case FLOW_DISSECT_RET_CONTINUE:
1417 case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1419 case FLOW_DISSECT_RET_OUT_BAD:
1425 fdret = FLOW_DISSECT_RET_CONTINUE;
1429 if (flags & FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP) {
1430 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1434 fdret = __skb_flow_dissect_gre(skb, key_control, flow_dissector,
1435 target_container, data,
1436 &proto, &nhoff, &hlen, flags);
1440 case NEXTHDR_ROUTING:
1441 case NEXTHDR_DEST: {
1442 u8 _opthdr[2], *opthdr;
1444 if (proto != htons(ETH_P_IPV6))
1447 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
1448 data, hlen, &_opthdr);
1450 fdret = FLOW_DISSECT_RET_OUT_BAD;
1454 ip_proto = opthdr[0];
1455 nhoff += (opthdr[1] + 1) << 3;
1457 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1460 case NEXTHDR_FRAGMENT: {
1461 struct frag_hdr _fh, *fh;
1463 if (proto != htons(ETH_P_IPV6))
1466 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
1470 fdret = FLOW_DISSECT_RET_OUT_BAD;
1474 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
1476 nhoff += sizeof(_fh);
1477 ip_proto = fh->nexthdr;
1479 if (!(fh->frag_off & htons(IP6_OFFSET))) {
1480 key_control->flags |= FLOW_DIS_FIRST_FRAG;
1481 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) {
1482 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1487 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1491 if (flags & FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP) {
1492 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1496 proto = htons(ETH_P_IP);
1498 key_control->flags |= FLOW_DIS_ENCAPSULATION;
1499 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1500 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1504 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1508 if (flags & FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP) {
1509 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1513 proto = htons(ETH_P_IPV6);
1515 key_control->flags |= FLOW_DIS_ENCAPSULATION;
1516 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1517 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1521 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1526 proto = htons(ETH_P_MPLS_UC);
1527 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1531 __skb_flow_dissect_tcp(skb, flow_dissector, target_container,
1536 case IPPROTO_ICMPV6:
1537 __skb_flow_dissect_icmp(skb, flow_dissector, target_container,
1541 __skb_flow_dissect_l2tpv3(skb, flow_dissector, target_container,
1549 if (!(key_control->flags & FLOW_DIS_IS_FRAGMENT))
1550 __skb_flow_dissect_ports(skb, flow_dissector, target_container,
1551 data, nhoff, ip_proto, hlen);
1553 /* Process result of IP proto processing */
1555 case FLOW_DISSECT_RET_PROTO_AGAIN:
1556 if (skb_flow_dissect_allowed(&num_hdrs))
1559 case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1560 if (skb_flow_dissect_allowed(&num_hdrs))
1561 goto ip_proto_again;
1563 case FLOW_DISSECT_RET_OUT_GOOD:
1564 case FLOW_DISSECT_RET_CONTINUE:
1566 case FLOW_DISSECT_RET_OUT_BAD:
1575 key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
1576 key_basic->n_proto = proto;
1577 key_basic->ip_proto = ip_proto;
1585 EXPORT_SYMBOL(__skb_flow_dissect);
1587 static siphash_aligned_key_t hashrnd;
1588 static __always_inline void __flow_hash_secret_init(void)
1590 net_get_random_once(&hashrnd, sizeof(hashrnd));
1593 static const void *flow_keys_hash_start(const struct flow_keys *flow)
1595 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % SIPHASH_ALIGNMENT);
1596 return &flow->FLOW_KEYS_HASH_START_FIELD;
1599 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
1601 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
1603 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
1605 switch (flow->control.addr_type) {
1606 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1607 diff -= sizeof(flow->addrs.v4addrs);
1609 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1610 diff -= sizeof(flow->addrs.v6addrs);
1612 case FLOW_DISSECTOR_KEY_TIPC:
1613 diff -= sizeof(flow->addrs.tipckey);
1616 return sizeof(*flow) - diff;
1619 __be32 flow_get_u32_src(const struct flow_keys *flow)
1621 switch (flow->control.addr_type) {
1622 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1623 return flow->addrs.v4addrs.src;
1624 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1625 return (__force __be32)ipv6_addr_hash(
1626 &flow->addrs.v6addrs.src);
1627 case FLOW_DISSECTOR_KEY_TIPC:
1628 return flow->addrs.tipckey.key;
1633 EXPORT_SYMBOL(flow_get_u32_src);
1635 __be32 flow_get_u32_dst(const struct flow_keys *flow)
1637 switch (flow->control.addr_type) {
1638 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1639 return flow->addrs.v4addrs.dst;
1640 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1641 return (__force __be32)ipv6_addr_hash(
1642 &flow->addrs.v6addrs.dst);
1647 EXPORT_SYMBOL(flow_get_u32_dst);
1649 /* Sort the source and destination IP and the ports,
1650 * to have consistent hash within the two directions
1652 static inline void __flow_hash_consistentify(struct flow_keys *keys)
1656 switch (keys->control.addr_type) {
1657 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1658 if ((__force u32)keys->addrs.v4addrs.dst <
1659 (__force u32)keys->addrs.v4addrs.src)
1660 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
1662 if ((__force u16)keys->ports.dst <
1663 (__force u16)keys->ports.src) {
1664 swap(keys->ports.src, keys->ports.dst);
1667 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1668 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
1669 &keys->addrs.v6addrs.src,
1670 sizeof(keys->addrs.v6addrs.dst));
1671 if (addr_diff < 0) {
1672 for (i = 0; i < 4; i++)
1673 swap(keys->addrs.v6addrs.src.s6_addr32[i],
1674 keys->addrs.v6addrs.dst.s6_addr32[i]);
1676 if ((__force u16)keys->ports.dst <
1677 (__force u16)keys->ports.src) {
1678 swap(keys->ports.src, keys->ports.dst);
1684 static inline u32 __flow_hash_from_keys(struct flow_keys *keys,
1685 const siphash_key_t *keyval)
1689 __flow_hash_consistentify(keys);
1691 hash = siphash(flow_keys_hash_start(keys),
1692 flow_keys_hash_length(keys), keyval);
1699 u32 flow_hash_from_keys(struct flow_keys *keys)
1701 __flow_hash_secret_init();
1702 return __flow_hash_from_keys(keys, &hashrnd);
1704 EXPORT_SYMBOL(flow_hash_from_keys);
1706 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
1707 struct flow_keys *keys,
1708 const siphash_key_t *keyval)
1710 skb_flow_dissect_flow_keys(skb, keys,
1711 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1713 return __flow_hash_from_keys(keys, keyval);
1716 struct _flow_keys_digest_data {
1725 void make_flow_keys_digest(struct flow_keys_digest *digest,
1726 const struct flow_keys *flow)
1728 struct _flow_keys_digest_data *data =
1729 (struct _flow_keys_digest_data *)digest;
1731 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
1733 memset(digest, 0, sizeof(*digest));
1735 data->n_proto = flow->basic.n_proto;
1736 data->ip_proto = flow->basic.ip_proto;
1737 data->ports = flow->ports.ports;
1738 data->src = flow->addrs.v4addrs.src;
1739 data->dst = flow->addrs.v4addrs.dst;
1741 EXPORT_SYMBOL(make_flow_keys_digest);
1743 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
1745 u32 __skb_get_hash_symmetric(const struct sk_buff *skb)
1747 struct flow_keys keys;
1749 __flow_hash_secret_init();
1751 memset(&keys, 0, sizeof(keys));
1752 __skb_flow_dissect(NULL, skb, &flow_keys_dissector_symmetric,
1753 &keys, NULL, 0, 0, 0,
1754 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1756 return __flow_hash_from_keys(&keys, &hashrnd);
1758 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
1761 * __skb_get_hash: calculate a flow hash
1762 * @skb: sk_buff to calculate flow hash from
1764 * This function calculates a flow hash based on src/dst addresses
1765 * and src/dst port numbers. Sets hash in skb to non-zero hash value
1766 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
1767 * if hash is a canonical 4-tuple hash over transport ports.
1769 void __skb_get_hash(struct sk_buff *skb)
1771 struct flow_keys keys;
1774 __flow_hash_secret_init();
1776 hash = ___skb_get_hash(skb, &keys, &hashrnd);
1778 __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1780 EXPORT_SYMBOL(__skb_get_hash);
1782 __u32 skb_get_hash_perturb(const struct sk_buff *skb,
1783 const siphash_key_t *perturb)
1785 struct flow_keys keys;
1787 return ___skb_get_hash(skb, &keys, perturb);
1789 EXPORT_SYMBOL(skb_get_hash_perturb);
1791 u32 __skb_get_poff(const struct sk_buff *skb, const void *data,
1792 const struct flow_keys_basic *keys, int hlen)
1794 u32 poff = keys->control.thoff;
1796 /* skip L4 headers for fragments after the first */
1797 if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
1798 !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
1801 switch (keys->basic.ip_proto) {
1803 /* access doff as u8 to avoid unaligned access */
1807 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
1808 data, hlen, &_doff);
1812 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
1816 case IPPROTO_UDPLITE:
1817 poff += sizeof(struct udphdr);
1819 /* For the rest, we do not really care about header
1820 * extensions at this point for now.
1823 poff += sizeof(struct icmphdr);
1825 case IPPROTO_ICMPV6:
1826 poff += sizeof(struct icmp6hdr);
1829 poff += sizeof(struct igmphdr);
1832 poff += sizeof(struct dccp_hdr);
1835 poff += sizeof(struct sctphdr);
1843 * skb_get_poff - get the offset to the payload
1844 * @skb: sk_buff to get the payload offset from
1846 * The function will get the offset to the payload as far as it could
1847 * be dissected. The main user is currently BPF, so that we can dynamically
1848 * truncate packets without needing to push actual payload to the user
1849 * space and can analyze headers only, instead.
1851 u32 skb_get_poff(const struct sk_buff *skb)
1853 struct flow_keys_basic keys;
1855 if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys,
1859 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
1862 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
1864 memset(keys, 0, sizeof(*keys));
1866 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
1867 sizeof(keys->addrs.v6addrs.src));
1868 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
1869 sizeof(keys->addrs.v6addrs.dst));
1870 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1871 keys->ports.src = fl6->fl6_sport;
1872 keys->ports.dst = fl6->fl6_dport;
1873 keys->keyid.keyid = fl6->fl6_gre_key;
1874 keys->tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
1875 keys->basic.ip_proto = fl6->flowi6_proto;
1877 return flow_hash_from_keys(keys);
1879 EXPORT_SYMBOL(__get_hash_from_flowi6);
1881 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
1883 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1884 .offset = offsetof(struct flow_keys, control),
1887 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1888 .offset = offsetof(struct flow_keys, basic),
1891 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1892 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1895 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1896 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1899 .key_id = FLOW_DISSECTOR_KEY_TIPC,
1900 .offset = offsetof(struct flow_keys, addrs.tipckey),
1903 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1904 .offset = offsetof(struct flow_keys, ports),
1907 .key_id = FLOW_DISSECTOR_KEY_VLAN,
1908 .offset = offsetof(struct flow_keys, vlan),
1911 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
1912 .offset = offsetof(struct flow_keys, tags),
1915 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
1916 .offset = offsetof(struct flow_keys, keyid),
1920 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
1922 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1923 .offset = offsetof(struct flow_keys, control),
1926 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1927 .offset = offsetof(struct flow_keys, basic),
1930 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1931 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1934 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1935 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1938 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1939 .offset = offsetof(struct flow_keys, ports),
1943 static const struct flow_dissector_key flow_keys_basic_dissector_keys[] = {
1945 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1946 .offset = offsetof(struct flow_keys, control),
1949 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1950 .offset = offsetof(struct flow_keys, basic),
1954 struct flow_dissector flow_keys_dissector __read_mostly;
1955 EXPORT_SYMBOL(flow_keys_dissector);
1957 struct flow_dissector flow_keys_basic_dissector __read_mostly;
1958 EXPORT_SYMBOL(flow_keys_basic_dissector);
1960 static int __init init_default_flow_dissectors(void)
1962 skb_flow_dissector_init(&flow_keys_dissector,
1963 flow_keys_dissector_keys,
1964 ARRAY_SIZE(flow_keys_dissector_keys));
1965 skb_flow_dissector_init(&flow_keys_dissector_symmetric,
1966 flow_keys_dissector_symmetric_keys,
1967 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
1968 skb_flow_dissector_init(&flow_keys_basic_dissector,
1969 flow_keys_basic_dissector_keys,
1970 ARRAY_SIZE(flow_keys_basic_dissector_keys));
1973 core_initcall(init_default_flow_dissectors);