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>
9 #include <net/dst_metadata.h>
15 #include <linux/igmp.h>
16 #include <linux/icmp.h>
17 #include <linux/sctp.h>
18 #include <linux/dccp.h>
19 #include <linux/if_tunnel.h>
20 #include <linux/if_pppox.h>
21 #include <linux/ppp_defs.h>
22 #include <linux/stddef.h>
23 #include <linux/if_ether.h>
24 #include <linux/mpls.h>
25 #include <linux/tcp.h>
26 #include <net/flow_dissector.h>
27 #include <scsi/fc/fc_fcoe.h>
28 #include <uapi/linux/batadv_packet.h>
29 #include <linux/bpf.h>
30 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
31 #include <net/netfilter/nf_conntrack_core.h>
32 #include <net/netfilter/nf_conntrack_labels.h>
34 #include <linux/bpf-netns.h>
36 static void dissector_set_key(struct flow_dissector
*flow_dissector
,
37 enum flow_dissector_key_id key_id
)
39 flow_dissector
->used_keys
|= (1 << key_id
);
42 void skb_flow_dissector_init(struct flow_dissector
*flow_dissector
,
43 const struct flow_dissector_key
*key
,
44 unsigned int key_count
)
48 memset(flow_dissector
, 0, sizeof(*flow_dissector
));
50 for (i
= 0; i
< key_count
; i
++, key
++) {
51 /* User should make sure that every key target offset is within
52 * boundaries of unsigned short.
54 BUG_ON(key
->offset
> USHRT_MAX
);
55 BUG_ON(dissector_uses_key(flow_dissector
,
58 dissector_set_key(flow_dissector
, key
->key_id
);
59 flow_dissector
->offset
[key
->key_id
] = key
->offset
;
62 /* Ensure that the dissector always includes control and basic key.
63 * That way we are able to avoid handling lack of these in fast path.
65 BUG_ON(!dissector_uses_key(flow_dissector
,
66 FLOW_DISSECTOR_KEY_CONTROL
));
67 BUG_ON(!dissector_uses_key(flow_dissector
,
68 FLOW_DISSECTOR_KEY_BASIC
));
70 EXPORT_SYMBOL(skb_flow_dissector_init
);
72 #ifdef CONFIG_BPF_SYSCALL
73 int flow_dissector_bpf_prog_attach_check(struct net
*net
,
74 struct bpf_prog
*prog
)
76 enum netns_bpf_attach_type type
= NETNS_BPF_FLOW_DISSECTOR
;
78 if (net
== &init_net
) {
79 /* BPF flow dissector in the root namespace overrides
80 * any per-net-namespace one. When attaching to root,
81 * make sure we don't have any BPF program attached
82 * to the non-root namespaces.
89 if (rcu_access_pointer(ns
->bpf
.run_array
[type
]))
93 /* Make sure root flow dissector is not attached
94 * when attaching to the non-root namespace.
96 if (rcu_access_pointer(init_net
.bpf
.run_array
[type
]))
102 #endif /* CONFIG_BPF_SYSCALL */
105 * __skb_flow_get_ports - extract the upper layer ports and return them
106 * @skb: sk_buff to extract the ports from
107 * @thoff: transport header offset
108 * @ip_proto: protocol for which to get port offset
109 * @data: raw buffer pointer to the packet, if NULL use skb->data
110 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
112 * The function will try to retrieve the ports at offset thoff + poff where poff
113 * is the protocol port offset returned from proto_ports_offset
115 __be32
__skb_flow_get_ports(const struct sk_buff
*skb
, int thoff
, u8 ip_proto
,
116 void *data
, int hlen
)
118 int poff
= proto_ports_offset(ip_proto
);
122 hlen
= skb_headlen(skb
);
126 __be32
*ports
, _ports
;
128 ports
= __skb_header_pointer(skb
, thoff
+ poff
,
129 sizeof(_ports
), data
, hlen
, &_ports
);
136 EXPORT_SYMBOL(__skb_flow_get_ports
);
138 static bool icmp_has_id(u8 type
)
144 case ICMP_TIMESTAMPREPLY
:
145 case ICMPV6_ECHO_REQUEST
:
146 case ICMPV6_ECHO_REPLY
:
154 * skb_flow_get_icmp_tci - extract ICMP(6) Type, Code and Identifier fields
155 * @skb: sk_buff to extract from
156 * @key_icmp: struct flow_dissector_key_icmp to fill
157 * @data: raw buffer pointer to the packet
158 * @thoff: offset to extract at
159 * @hlen: packet header length
161 void skb_flow_get_icmp_tci(const struct sk_buff
*skb
,
162 struct flow_dissector_key_icmp
*key_icmp
,
163 void *data
, int thoff
, int hlen
)
165 struct icmphdr
*ih
, _ih
;
167 ih
= __skb_header_pointer(skb
, thoff
, sizeof(_ih
), data
, hlen
, &_ih
);
171 key_icmp
->type
= ih
->type
;
172 key_icmp
->code
= ih
->code
;
174 /* As we use 0 to signal that the Id field is not present,
175 * avoid confusion with packets without such field
177 if (icmp_has_id(ih
->type
))
178 key_icmp
->id
= ih
->un
.echo
.id
? : 1;
182 EXPORT_SYMBOL(skb_flow_get_icmp_tci
);
184 /* If FLOW_DISSECTOR_KEY_ICMP is set, dissect an ICMP packet
185 * using skb_flow_get_icmp_tci().
187 static void __skb_flow_dissect_icmp(const struct sk_buff
*skb
,
188 struct flow_dissector
*flow_dissector
,
189 void *target_container
,
190 void *data
, int thoff
, int hlen
)
192 struct flow_dissector_key_icmp
*key_icmp
;
194 if (!dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_ICMP
))
197 key_icmp
= skb_flow_dissector_target(flow_dissector
,
198 FLOW_DISSECTOR_KEY_ICMP
,
201 skb_flow_get_icmp_tci(skb
, key_icmp
, data
, thoff
, hlen
);
204 void skb_flow_dissect_meta(const struct sk_buff
*skb
,
205 struct flow_dissector
*flow_dissector
,
206 void *target_container
)
208 struct flow_dissector_key_meta
*meta
;
210 if (!dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_META
))
213 meta
= skb_flow_dissector_target(flow_dissector
,
214 FLOW_DISSECTOR_KEY_META
,
216 meta
->ingress_ifindex
= skb
->skb_iif
;
218 EXPORT_SYMBOL(skb_flow_dissect_meta
);
221 skb_flow_dissect_set_enc_addr_type(enum flow_dissector_key_id type
,
222 struct flow_dissector
*flow_dissector
,
223 void *target_container
)
225 struct flow_dissector_key_control
*ctrl
;
227 if (!dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_ENC_CONTROL
))
230 ctrl
= skb_flow_dissector_target(flow_dissector
,
231 FLOW_DISSECTOR_KEY_ENC_CONTROL
,
233 ctrl
->addr_type
= type
;
237 skb_flow_dissect_ct(const struct sk_buff
*skb
,
238 struct flow_dissector
*flow_dissector
,
239 void *target_container
,
243 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
244 struct flow_dissector_key_ct
*key
;
245 enum ip_conntrack_info ctinfo
;
246 struct nf_conn_labels
*cl
;
249 if (!dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_CT
))
252 ct
= nf_ct_get(skb
, &ctinfo
);
256 key
= skb_flow_dissector_target(flow_dissector
,
257 FLOW_DISSECTOR_KEY_CT
,
260 if (ctinfo
< mapsize
)
261 key
->ct_state
= ctinfo_map
[ctinfo
];
262 #if IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)
263 key
->ct_zone
= ct
->zone
.id
;
265 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
266 key
->ct_mark
= ct
->mark
;
269 cl
= nf_ct_labels_find(ct
);
271 memcpy(key
->ct_labels
, cl
->bits
, sizeof(key
->ct_labels
));
272 #endif /* CONFIG_NF_CONNTRACK */
274 EXPORT_SYMBOL(skb_flow_dissect_ct
);
277 skb_flow_dissect_tunnel_info(const struct sk_buff
*skb
,
278 struct flow_dissector
*flow_dissector
,
279 void *target_container
)
281 struct ip_tunnel_info
*info
;
282 struct ip_tunnel_key
*key
;
284 /* A quick check to see if there might be something to do. */
285 if (!dissector_uses_key(flow_dissector
,
286 FLOW_DISSECTOR_KEY_ENC_KEYID
) &&
287 !dissector_uses_key(flow_dissector
,
288 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS
) &&
289 !dissector_uses_key(flow_dissector
,
290 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS
) &&
291 !dissector_uses_key(flow_dissector
,
292 FLOW_DISSECTOR_KEY_ENC_CONTROL
) &&
293 !dissector_uses_key(flow_dissector
,
294 FLOW_DISSECTOR_KEY_ENC_PORTS
) &&
295 !dissector_uses_key(flow_dissector
,
296 FLOW_DISSECTOR_KEY_ENC_IP
) &&
297 !dissector_uses_key(flow_dissector
,
298 FLOW_DISSECTOR_KEY_ENC_OPTS
))
301 info
= skb_tunnel_info(skb
);
307 switch (ip_tunnel_info_af(info
)) {
309 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV4_ADDRS
,
312 if (dissector_uses_key(flow_dissector
,
313 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS
)) {
314 struct flow_dissector_key_ipv4_addrs
*ipv4
;
316 ipv4
= skb_flow_dissector_target(flow_dissector
,
317 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS
,
319 ipv4
->src
= key
->u
.ipv4
.src
;
320 ipv4
->dst
= key
->u
.ipv4
.dst
;
324 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV6_ADDRS
,
327 if (dissector_uses_key(flow_dissector
,
328 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS
)) {
329 struct flow_dissector_key_ipv6_addrs
*ipv6
;
331 ipv6
= skb_flow_dissector_target(flow_dissector
,
332 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS
,
334 ipv6
->src
= key
->u
.ipv6
.src
;
335 ipv6
->dst
= key
->u
.ipv6
.dst
;
340 if (dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_ENC_KEYID
)) {
341 struct flow_dissector_key_keyid
*keyid
;
343 keyid
= skb_flow_dissector_target(flow_dissector
,
344 FLOW_DISSECTOR_KEY_ENC_KEYID
,
346 keyid
->keyid
= tunnel_id_to_key32(key
->tun_id
);
349 if (dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_ENC_PORTS
)) {
350 struct flow_dissector_key_ports
*tp
;
352 tp
= skb_flow_dissector_target(flow_dissector
,
353 FLOW_DISSECTOR_KEY_ENC_PORTS
,
355 tp
->src
= key
->tp_src
;
356 tp
->dst
= key
->tp_dst
;
359 if (dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_ENC_IP
)) {
360 struct flow_dissector_key_ip
*ip
;
362 ip
= skb_flow_dissector_target(flow_dissector
,
363 FLOW_DISSECTOR_KEY_ENC_IP
,
369 if (dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_ENC_OPTS
)) {
370 struct flow_dissector_key_enc_opts
*enc_opt
;
372 enc_opt
= skb_flow_dissector_target(flow_dissector
,
373 FLOW_DISSECTOR_KEY_ENC_OPTS
,
376 if (info
->options_len
) {
377 enc_opt
->len
= info
->options_len
;
378 ip_tunnel_info_opts_get(enc_opt
->data
, info
);
379 enc_opt
->dst_opt_type
= info
->key
.tun_flags
&
380 TUNNEL_OPTIONS_PRESENT
;
384 EXPORT_SYMBOL(skb_flow_dissect_tunnel_info
);
386 void skb_flow_dissect_hash(const struct sk_buff
*skb
,
387 struct flow_dissector
*flow_dissector
,
388 void *target_container
)
390 struct flow_dissector_key_hash
*key
;
392 if (!dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_HASH
))
395 key
= skb_flow_dissector_target(flow_dissector
,
396 FLOW_DISSECTOR_KEY_HASH
,
399 key
->hash
= skb_get_hash_raw(skb
);
401 EXPORT_SYMBOL(skb_flow_dissect_hash
);
403 static enum flow_dissect_ret
404 __skb_flow_dissect_mpls(const struct sk_buff
*skb
,
405 struct flow_dissector
*flow_dissector
,
406 void *target_container
, void *data
, int nhoff
, int hlen
,
407 int lse_index
, bool *entropy_label
)
409 struct mpls_label
*hdr
, _hdr
;
410 u32 entry
, label
, bos
;
412 if (!dissector_uses_key(flow_dissector
,
413 FLOW_DISSECTOR_KEY_MPLS_ENTROPY
) &&
414 !dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_MPLS
))
415 return FLOW_DISSECT_RET_OUT_GOOD
;
417 if (lse_index
>= FLOW_DIS_MPLS_MAX
)
418 return FLOW_DISSECT_RET_OUT_GOOD
;
420 hdr
= __skb_header_pointer(skb
, nhoff
, sizeof(_hdr
), data
,
423 return FLOW_DISSECT_RET_OUT_BAD
;
425 entry
= ntohl(hdr
->entry
);
426 label
= (entry
& MPLS_LS_LABEL_MASK
) >> MPLS_LS_LABEL_SHIFT
;
427 bos
= (entry
& MPLS_LS_S_MASK
) >> MPLS_LS_S_SHIFT
;
429 if (dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_MPLS
)) {
430 struct flow_dissector_key_mpls
*key_mpls
;
431 struct flow_dissector_mpls_lse
*lse
;
433 key_mpls
= skb_flow_dissector_target(flow_dissector
,
434 FLOW_DISSECTOR_KEY_MPLS
,
436 lse
= &key_mpls
->ls
[lse_index
];
438 lse
->mpls_ttl
= (entry
& MPLS_LS_TTL_MASK
) >> MPLS_LS_TTL_SHIFT
;
440 lse
->mpls_tc
= (entry
& MPLS_LS_TC_MASK
) >> MPLS_LS_TC_SHIFT
;
441 lse
->mpls_label
= label
;
442 dissector_set_mpls_lse(key_mpls
, lse_index
);
445 if (*entropy_label
&&
446 dissector_uses_key(flow_dissector
,
447 FLOW_DISSECTOR_KEY_MPLS_ENTROPY
)) {
448 struct flow_dissector_key_keyid
*key_keyid
;
450 key_keyid
= skb_flow_dissector_target(flow_dissector
,
451 FLOW_DISSECTOR_KEY_MPLS_ENTROPY
,
453 key_keyid
->keyid
= cpu_to_be32(label
);
456 *entropy_label
= label
== MPLS_LABEL_ENTROPY
;
458 return bos
? FLOW_DISSECT_RET_OUT_GOOD
: FLOW_DISSECT_RET_PROTO_AGAIN
;
461 static enum flow_dissect_ret
462 __skb_flow_dissect_arp(const struct sk_buff
*skb
,
463 struct flow_dissector
*flow_dissector
,
464 void *target_container
, void *data
, int nhoff
, int hlen
)
466 struct flow_dissector_key_arp
*key_arp
;
468 unsigned char ar_sha
[ETH_ALEN
];
469 unsigned char ar_sip
[4];
470 unsigned char ar_tha
[ETH_ALEN
];
471 unsigned char ar_tip
[4];
472 } *arp_eth
, _arp_eth
;
473 const struct arphdr
*arp
;
476 if (!dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_ARP
))
477 return FLOW_DISSECT_RET_OUT_GOOD
;
479 arp
= __skb_header_pointer(skb
, nhoff
, sizeof(_arp
), data
,
482 return FLOW_DISSECT_RET_OUT_BAD
;
484 if (arp
->ar_hrd
!= htons(ARPHRD_ETHER
) ||
485 arp
->ar_pro
!= htons(ETH_P_IP
) ||
486 arp
->ar_hln
!= ETH_ALEN
||
488 (arp
->ar_op
!= htons(ARPOP_REPLY
) &&
489 arp
->ar_op
!= htons(ARPOP_REQUEST
)))
490 return FLOW_DISSECT_RET_OUT_BAD
;
492 arp_eth
= __skb_header_pointer(skb
, nhoff
+ sizeof(_arp
),
493 sizeof(_arp_eth
), data
,
496 return FLOW_DISSECT_RET_OUT_BAD
;
498 key_arp
= skb_flow_dissector_target(flow_dissector
,
499 FLOW_DISSECTOR_KEY_ARP
,
502 memcpy(&key_arp
->sip
, arp_eth
->ar_sip
, sizeof(key_arp
->sip
));
503 memcpy(&key_arp
->tip
, arp_eth
->ar_tip
, sizeof(key_arp
->tip
));
505 /* Only store the lower byte of the opcode;
506 * this covers ARPOP_REPLY and ARPOP_REQUEST.
508 key_arp
->op
= ntohs(arp
->ar_op
) & 0xff;
510 ether_addr_copy(key_arp
->sha
, arp_eth
->ar_sha
);
511 ether_addr_copy(key_arp
->tha
, arp_eth
->ar_tha
);
513 return FLOW_DISSECT_RET_OUT_GOOD
;
516 static enum flow_dissect_ret
517 __skb_flow_dissect_gre(const struct sk_buff
*skb
,
518 struct flow_dissector_key_control
*key_control
,
519 struct flow_dissector
*flow_dissector
,
520 void *target_container
, void *data
,
521 __be16
*p_proto
, int *p_nhoff
, int *p_hlen
,
524 struct flow_dissector_key_keyid
*key_keyid
;
525 struct gre_base_hdr
*hdr
, _hdr
;
529 hdr
= __skb_header_pointer(skb
, *p_nhoff
, sizeof(_hdr
),
530 data
, *p_hlen
, &_hdr
);
532 return FLOW_DISSECT_RET_OUT_BAD
;
534 /* Only look inside GRE without routing */
535 if (hdr
->flags
& GRE_ROUTING
)
536 return FLOW_DISSECT_RET_OUT_GOOD
;
538 /* Only look inside GRE for version 0 and 1 */
539 gre_ver
= ntohs(hdr
->flags
& GRE_VERSION
);
541 return FLOW_DISSECT_RET_OUT_GOOD
;
543 *p_proto
= hdr
->protocol
;
545 /* Version1 must be PPTP, and check the flags */
546 if (!(*p_proto
== GRE_PROTO_PPP
&& (hdr
->flags
& GRE_KEY
)))
547 return FLOW_DISSECT_RET_OUT_GOOD
;
550 offset
+= sizeof(struct gre_base_hdr
);
552 if (hdr
->flags
& GRE_CSUM
)
553 offset
+= sizeof_field(struct gre_full_hdr
, csum
) +
554 sizeof_field(struct gre_full_hdr
, reserved1
);
556 if (hdr
->flags
& GRE_KEY
) {
560 keyid
= __skb_header_pointer(skb
, *p_nhoff
+ offset
,
562 data
, *p_hlen
, &_keyid
);
564 return FLOW_DISSECT_RET_OUT_BAD
;
566 if (dissector_uses_key(flow_dissector
,
567 FLOW_DISSECTOR_KEY_GRE_KEYID
)) {
568 key_keyid
= skb_flow_dissector_target(flow_dissector
,
569 FLOW_DISSECTOR_KEY_GRE_KEYID
,
572 key_keyid
->keyid
= *keyid
;
574 key_keyid
->keyid
= *keyid
& GRE_PPTP_KEY_MASK
;
576 offset
+= sizeof_field(struct gre_full_hdr
, key
);
579 if (hdr
->flags
& GRE_SEQ
)
580 offset
+= sizeof_field(struct pptp_gre_header
, seq
);
583 if (*p_proto
== htons(ETH_P_TEB
)) {
584 const struct ethhdr
*eth
;
587 eth
= __skb_header_pointer(skb
, *p_nhoff
+ offset
,
589 data
, *p_hlen
, &_eth
);
591 return FLOW_DISSECT_RET_OUT_BAD
;
592 *p_proto
= eth
->h_proto
;
593 offset
+= sizeof(*eth
);
595 /* Cap headers that we access via pointers at the
596 * end of the Ethernet header as our maximum alignment
597 * at that point is only 2 bytes.
600 *p_hlen
= *p_nhoff
+ offset
;
602 } else { /* version 1, must be PPTP */
603 u8 _ppp_hdr
[PPP_HDRLEN
];
606 if (hdr
->flags
& GRE_ACK
)
607 offset
+= sizeof_field(struct pptp_gre_header
, ack
);
609 ppp_hdr
= __skb_header_pointer(skb
, *p_nhoff
+ offset
,
611 data
, *p_hlen
, _ppp_hdr
);
613 return FLOW_DISSECT_RET_OUT_BAD
;
615 switch (PPP_PROTOCOL(ppp_hdr
)) {
617 *p_proto
= htons(ETH_P_IP
);
620 *p_proto
= htons(ETH_P_IPV6
);
623 /* Could probably catch some more like MPLS */
627 offset
+= PPP_HDRLEN
;
631 key_control
->flags
|= FLOW_DIS_ENCAPSULATION
;
632 if (flags
& FLOW_DISSECTOR_F_STOP_AT_ENCAP
)
633 return FLOW_DISSECT_RET_OUT_GOOD
;
635 return FLOW_DISSECT_RET_PROTO_AGAIN
;
639 * __skb_flow_dissect_batadv() - dissect batman-adv header
640 * @skb: sk_buff to with the batman-adv header
641 * @key_control: flow dissectors control key
642 * @data: raw buffer pointer to the packet, if NULL use skb->data
643 * @p_proto: pointer used to update the protocol to process next
644 * @p_nhoff: pointer used to update inner network header offset
645 * @hlen: packet header length
646 * @flags: any combination of FLOW_DISSECTOR_F_*
648 * ETH_P_BATMAN packets are tried to be dissected. Only
649 * &struct batadv_unicast packets are actually processed because they contain an
650 * inner ethernet header and are usually followed by actual network header. This
651 * allows the flow dissector to continue processing the packet.
653 * Return: FLOW_DISSECT_RET_PROTO_AGAIN when &struct batadv_unicast was found,
654 * FLOW_DISSECT_RET_OUT_GOOD when dissector should stop after encapsulation,
655 * otherwise FLOW_DISSECT_RET_OUT_BAD
657 static enum flow_dissect_ret
658 __skb_flow_dissect_batadv(const struct sk_buff
*skb
,
659 struct flow_dissector_key_control
*key_control
,
660 void *data
, __be16
*p_proto
, int *p_nhoff
, int hlen
,
664 struct batadv_unicast_packet batadv_unicast
;
668 hdr
= __skb_header_pointer(skb
, *p_nhoff
, sizeof(_hdr
), data
, hlen
,
671 return FLOW_DISSECT_RET_OUT_BAD
;
673 if (hdr
->batadv_unicast
.version
!= BATADV_COMPAT_VERSION
)
674 return FLOW_DISSECT_RET_OUT_BAD
;
676 if (hdr
->batadv_unicast
.packet_type
!= BATADV_UNICAST
)
677 return FLOW_DISSECT_RET_OUT_BAD
;
679 *p_proto
= hdr
->eth
.h_proto
;
680 *p_nhoff
+= sizeof(*hdr
);
682 key_control
->flags
|= FLOW_DIS_ENCAPSULATION
;
683 if (flags
& FLOW_DISSECTOR_F_STOP_AT_ENCAP
)
684 return FLOW_DISSECT_RET_OUT_GOOD
;
686 return FLOW_DISSECT_RET_PROTO_AGAIN
;
690 __skb_flow_dissect_tcp(const struct sk_buff
*skb
,
691 struct flow_dissector
*flow_dissector
,
692 void *target_container
, void *data
, int thoff
, int hlen
)
694 struct flow_dissector_key_tcp
*key_tcp
;
695 struct tcphdr
*th
, _th
;
697 if (!dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_TCP
))
700 th
= __skb_header_pointer(skb
, thoff
, sizeof(_th
), data
, hlen
, &_th
);
704 if (unlikely(__tcp_hdrlen(th
) < sizeof(_th
)))
707 key_tcp
= skb_flow_dissector_target(flow_dissector
,
708 FLOW_DISSECTOR_KEY_TCP
,
710 key_tcp
->flags
= (*(__be16
*) &tcp_flag_word(th
) & htons(0x0FFF));
714 __skb_flow_dissect_ports(const struct sk_buff
*skb
,
715 struct flow_dissector
*flow_dissector
,
716 void *target_container
, void *data
, int nhoff
,
717 u8 ip_proto
, int hlen
)
719 enum flow_dissector_key_id dissector_ports
= FLOW_DISSECTOR_KEY_MAX
;
720 struct flow_dissector_key_ports
*key_ports
;
722 if (dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_PORTS
))
723 dissector_ports
= FLOW_DISSECTOR_KEY_PORTS
;
724 else if (dissector_uses_key(flow_dissector
,
725 FLOW_DISSECTOR_KEY_PORTS_RANGE
))
726 dissector_ports
= FLOW_DISSECTOR_KEY_PORTS_RANGE
;
728 if (dissector_ports
== FLOW_DISSECTOR_KEY_MAX
)
731 key_ports
= skb_flow_dissector_target(flow_dissector
,
734 key_ports
->ports
= __skb_flow_get_ports(skb
, nhoff
, ip_proto
,
739 __skb_flow_dissect_ipv4(const struct sk_buff
*skb
,
740 struct flow_dissector
*flow_dissector
,
741 void *target_container
, void *data
, const struct iphdr
*iph
)
743 struct flow_dissector_key_ip
*key_ip
;
745 if (!dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_IP
))
748 key_ip
= skb_flow_dissector_target(flow_dissector
,
749 FLOW_DISSECTOR_KEY_IP
,
751 key_ip
->tos
= iph
->tos
;
752 key_ip
->ttl
= iph
->ttl
;
756 __skb_flow_dissect_ipv6(const struct sk_buff
*skb
,
757 struct flow_dissector
*flow_dissector
,
758 void *target_container
, void *data
, const struct ipv6hdr
*iph
)
760 struct flow_dissector_key_ip
*key_ip
;
762 if (!dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_IP
))
765 key_ip
= skb_flow_dissector_target(flow_dissector
,
766 FLOW_DISSECTOR_KEY_IP
,
768 key_ip
->tos
= ipv6_get_dsfield(iph
);
769 key_ip
->ttl
= iph
->hop_limit
;
772 /* Maximum number of protocol headers that can be parsed in
775 #define MAX_FLOW_DISSECT_HDRS 15
777 static bool skb_flow_dissect_allowed(int *num_hdrs
)
781 return (*num_hdrs
<= MAX_FLOW_DISSECT_HDRS
);
784 static void __skb_flow_bpf_to_target(const struct bpf_flow_keys
*flow_keys
,
785 struct flow_dissector
*flow_dissector
,
786 void *target_container
)
788 struct flow_dissector_key_ports
*key_ports
= NULL
;
789 struct flow_dissector_key_control
*key_control
;
790 struct flow_dissector_key_basic
*key_basic
;
791 struct flow_dissector_key_addrs
*key_addrs
;
792 struct flow_dissector_key_tags
*key_tags
;
794 key_control
= skb_flow_dissector_target(flow_dissector
,
795 FLOW_DISSECTOR_KEY_CONTROL
,
797 key_control
->thoff
= flow_keys
->thoff
;
798 if (flow_keys
->is_frag
)
799 key_control
->flags
|= FLOW_DIS_IS_FRAGMENT
;
800 if (flow_keys
->is_first_frag
)
801 key_control
->flags
|= FLOW_DIS_FIRST_FRAG
;
802 if (flow_keys
->is_encap
)
803 key_control
->flags
|= FLOW_DIS_ENCAPSULATION
;
805 key_basic
= skb_flow_dissector_target(flow_dissector
,
806 FLOW_DISSECTOR_KEY_BASIC
,
808 key_basic
->n_proto
= flow_keys
->n_proto
;
809 key_basic
->ip_proto
= flow_keys
->ip_proto
;
811 if (flow_keys
->addr_proto
== ETH_P_IP
&&
812 dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_IPV4_ADDRS
)) {
813 key_addrs
= skb_flow_dissector_target(flow_dissector
,
814 FLOW_DISSECTOR_KEY_IPV4_ADDRS
,
816 key_addrs
->v4addrs
.src
= flow_keys
->ipv4_src
;
817 key_addrs
->v4addrs
.dst
= flow_keys
->ipv4_dst
;
818 key_control
->addr_type
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
;
819 } else if (flow_keys
->addr_proto
== ETH_P_IPV6
&&
820 dissector_uses_key(flow_dissector
,
821 FLOW_DISSECTOR_KEY_IPV6_ADDRS
)) {
822 key_addrs
= skb_flow_dissector_target(flow_dissector
,
823 FLOW_DISSECTOR_KEY_IPV6_ADDRS
,
825 memcpy(&key_addrs
->v6addrs
, &flow_keys
->ipv6_src
,
826 sizeof(key_addrs
->v6addrs
));
827 key_control
->addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
830 if (dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_PORTS
))
831 key_ports
= skb_flow_dissector_target(flow_dissector
,
832 FLOW_DISSECTOR_KEY_PORTS
,
834 else if (dissector_uses_key(flow_dissector
,
835 FLOW_DISSECTOR_KEY_PORTS_RANGE
))
836 key_ports
= skb_flow_dissector_target(flow_dissector
,
837 FLOW_DISSECTOR_KEY_PORTS_RANGE
,
841 key_ports
->src
= flow_keys
->sport
;
842 key_ports
->dst
= flow_keys
->dport
;
845 if (dissector_uses_key(flow_dissector
,
846 FLOW_DISSECTOR_KEY_FLOW_LABEL
)) {
847 key_tags
= skb_flow_dissector_target(flow_dissector
,
848 FLOW_DISSECTOR_KEY_FLOW_LABEL
,
850 key_tags
->flow_label
= ntohl(flow_keys
->flow_label
);
854 bool bpf_flow_dissect(struct bpf_prog
*prog
, struct bpf_flow_dissector
*ctx
,
855 __be16 proto
, int nhoff
, int hlen
, unsigned int flags
)
857 struct bpf_flow_keys
*flow_keys
= ctx
->flow_keys
;
860 /* Pass parameters to the BPF program */
861 memset(flow_keys
, 0, sizeof(*flow_keys
));
862 flow_keys
->n_proto
= proto
;
863 flow_keys
->nhoff
= nhoff
;
864 flow_keys
->thoff
= flow_keys
->nhoff
;
866 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG
!=
867 (int)FLOW_DISSECTOR_F_PARSE_1ST_FRAG
);
868 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
!=
869 (int)FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
);
870 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP
!=
871 (int)FLOW_DISSECTOR_F_STOP_AT_ENCAP
);
872 flow_keys
->flags
= flags
;
874 result
= bpf_prog_run_pin_on_cpu(prog
, ctx
);
876 flow_keys
->nhoff
= clamp_t(u16
, flow_keys
->nhoff
, nhoff
, hlen
);
877 flow_keys
->thoff
= clamp_t(u16
, flow_keys
->thoff
,
878 flow_keys
->nhoff
, hlen
);
880 return result
== BPF_OK
;
884 * __skb_flow_dissect - extract the flow_keys struct and return it
885 * @net: associated network namespace, derived from @skb if NULL
886 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
887 * @flow_dissector: list of keys to dissect
888 * @target_container: target structure to put dissected values into
889 * @data: raw buffer pointer to the packet, if NULL use skb->data
890 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
891 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
892 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
893 * @flags: flags that control the dissection process, e.g.
894 * FLOW_DISSECTOR_F_STOP_AT_ENCAP.
896 * The function will try to retrieve individual keys into target specified
897 * by flow_dissector from either the skbuff or a raw buffer specified by the
900 * Caller must take care of zeroing target container memory.
902 bool __skb_flow_dissect(const struct net
*net
,
903 const struct sk_buff
*skb
,
904 struct flow_dissector
*flow_dissector
,
905 void *target_container
,
906 void *data
, __be16 proto
, int nhoff
, int hlen
,
909 struct flow_dissector_key_control
*key_control
;
910 struct flow_dissector_key_basic
*key_basic
;
911 struct flow_dissector_key_addrs
*key_addrs
;
912 struct flow_dissector_key_tags
*key_tags
;
913 struct flow_dissector_key_vlan
*key_vlan
;
914 enum flow_dissect_ret fdret
;
915 enum flow_dissector_key_id dissector_vlan
= FLOW_DISSECTOR_KEY_MAX
;
916 bool mpls_el
= false;
924 proto
= skb_vlan_tag_present(skb
) ?
925 skb
->vlan_proto
: skb
->protocol
;
926 nhoff
= skb_network_offset(skb
);
927 hlen
= skb_headlen(skb
);
928 #if IS_ENABLED(CONFIG_NET_DSA)
929 if (unlikely(skb
->dev
&& netdev_uses_dsa(skb
->dev
) &&
930 proto
== htons(ETH_P_XDSA
))) {
931 const struct dsa_device_ops
*ops
;
934 ops
= skb
->dev
->dsa_ptr
->tag_ops
;
935 /* Tail taggers don't break flow dissection */
936 if (!ops
->tail_tag
) {
937 if (ops
->flow_dissect
)
938 ops
->flow_dissect(skb
, &proto
, &offset
);
940 dsa_tag_generic_flow_dissect(skb
,
950 /* It is ensured by skb_flow_dissector_init() that control key will
953 key_control
= skb_flow_dissector_target(flow_dissector
,
954 FLOW_DISSECTOR_KEY_CONTROL
,
957 /* It is ensured by skb_flow_dissector_init() that basic key will
960 key_basic
= skb_flow_dissector_target(flow_dissector
,
961 FLOW_DISSECTOR_KEY_BASIC
,
967 net
= dev_net(skb
->dev
);
969 net
= sock_net(skb
->sk
);
975 enum netns_bpf_attach_type type
= NETNS_BPF_FLOW_DISSECTOR
;
976 struct bpf_prog_array
*run_array
;
979 run_array
= rcu_dereference(init_net
.bpf
.run_array
[type
]);
981 run_array
= rcu_dereference(net
->bpf
.run_array
[type
]);
984 struct bpf_flow_keys flow_keys
;
985 struct bpf_flow_dissector ctx
= {
986 .flow_keys
= &flow_keys
,
988 .data_end
= data
+ hlen
,
990 __be16 n_proto
= proto
;
991 struct bpf_prog
*prog
;
995 /* we can't use 'proto' in the skb case
996 * because it might be set to skb->vlan_proto
997 * which has been pulled from the data
999 n_proto
= skb
->protocol
;
1002 prog
= READ_ONCE(run_array
->items
[0].prog
);
1003 ret
= bpf_flow_dissect(prog
, &ctx
, n_proto
, nhoff
,
1005 __skb_flow_bpf_to_target(&flow_keys
, flow_dissector
,
1013 if (dissector_uses_key(flow_dissector
,
1014 FLOW_DISSECTOR_KEY_ETH_ADDRS
)) {
1015 struct ethhdr
*eth
= eth_hdr(skb
);
1016 struct flow_dissector_key_eth_addrs
*key_eth_addrs
;
1018 key_eth_addrs
= skb_flow_dissector_target(flow_dissector
,
1019 FLOW_DISSECTOR_KEY_ETH_ADDRS
,
1021 memcpy(key_eth_addrs
, ð
->h_dest
, sizeof(*key_eth_addrs
));
1025 fdret
= FLOW_DISSECT_RET_CONTINUE
;
1028 case htons(ETH_P_IP
): {
1029 const struct iphdr
*iph
;
1032 iph
= __skb_header_pointer(skb
, nhoff
, sizeof(_iph
), data
, hlen
, &_iph
);
1033 if (!iph
|| iph
->ihl
< 5) {
1034 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
1038 nhoff
+= iph
->ihl
* 4;
1040 ip_proto
= iph
->protocol
;
1042 if (dissector_uses_key(flow_dissector
,
1043 FLOW_DISSECTOR_KEY_IPV4_ADDRS
)) {
1044 key_addrs
= skb_flow_dissector_target(flow_dissector
,
1045 FLOW_DISSECTOR_KEY_IPV4_ADDRS
,
1048 memcpy(&key_addrs
->v4addrs
, &iph
->saddr
,
1049 sizeof(key_addrs
->v4addrs
));
1050 key_control
->addr_type
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
;
1053 if (ip_is_fragment(iph
)) {
1054 key_control
->flags
|= FLOW_DIS_IS_FRAGMENT
;
1056 if (iph
->frag_off
& htons(IP_OFFSET
)) {
1057 fdret
= FLOW_DISSECT_RET_OUT_GOOD
;
1060 key_control
->flags
|= FLOW_DIS_FIRST_FRAG
;
1062 FLOW_DISSECTOR_F_PARSE_1ST_FRAG
)) {
1063 fdret
= FLOW_DISSECT_RET_OUT_GOOD
;
1069 __skb_flow_dissect_ipv4(skb
, flow_dissector
,
1070 target_container
, data
, iph
);
1074 case htons(ETH_P_IPV6
): {
1075 const struct ipv6hdr
*iph
;
1076 struct ipv6hdr _iph
;
1078 iph
= __skb_header_pointer(skb
, nhoff
, sizeof(_iph
), data
, hlen
, &_iph
);
1080 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
1084 ip_proto
= iph
->nexthdr
;
1085 nhoff
+= sizeof(struct ipv6hdr
);
1087 if (dissector_uses_key(flow_dissector
,
1088 FLOW_DISSECTOR_KEY_IPV6_ADDRS
)) {
1089 key_addrs
= skb_flow_dissector_target(flow_dissector
,
1090 FLOW_DISSECTOR_KEY_IPV6_ADDRS
,
1093 memcpy(&key_addrs
->v6addrs
, &iph
->saddr
,
1094 sizeof(key_addrs
->v6addrs
));
1095 key_control
->addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
1098 if ((dissector_uses_key(flow_dissector
,
1099 FLOW_DISSECTOR_KEY_FLOW_LABEL
) ||
1100 (flags
& FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
)) &&
1101 ip6_flowlabel(iph
)) {
1102 __be32 flow_label
= ip6_flowlabel(iph
);
1104 if (dissector_uses_key(flow_dissector
,
1105 FLOW_DISSECTOR_KEY_FLOW_LABEL
)) {
1106 key_tags
= skb_flow_dissector_target(flow_dissector
,
1107 FLOW_DISSECTOR_KEY_FLOW_LABEL
,
1109 key_tags
->flow_label
= ntohl(flow_label
);
1111 if (flags
& FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
) {
1112 fdret
= FLOW_DISSECT_RET_OUT_GOOD
;
1117 __skb_flow_dissect_ipv6(skb
, flow_dissector
,
1118 target_container
, data
, iph
);
1122 case htons(ETH_P_8021AD
):
1123 case htons(ETH_P_8021Q
): {
1124 const struct vlan_hdr
*vlan
= NULL
;
1125 struct vlan_hdr _vlan
;
1126 __be16 saved_vlan_tpid
= proto
;
1128 if (dissector_vlan
== FLOW_DISSECTOR_KEY_MAX
&&
1129 skb
&& skb_vlan_tag_present(skb
)) {
1130 proto
= skb
->protocol
;
1132 vlan
= __skb_header_pointer(skb
, nhoff
, sizeof(_vlan
),
1133 data
, hlen
, &_vlan
);
1135 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
1139 proto
= vlan
->h_vlan_encapsulated_proto
;
1140 nhoff
+= sizeof(*vlan
);
1143 if (dissector_vlan
== FLOW_DISSECTOR_KEY_MAX
) {
1144 dissector_vlan
= FLOW_DISSECTOR_KEY_VLAN
;
1145 } else if (dissector_vlan
== FLOW_DISSECTOR_KEY_VLAN
) {
1146 dissector_vlan
= FLOW_DISSECTOR_KEY_CVLAN
;
1148 fdret
= FLOW_DISSECT_RET_PROTO_AGAIN
;
1152 if (dissector_uses_key(flow_dissector
, dissector_vlan
)) {
1153 key_vlan
= skb_flow_dissector_target(flow_dissector
,
1158 key_vlan
->vlan_id
= skb_vlan_tag_get_id(skb
);
1159 key_vlan
->vlan_priority
= skb_vlan_tag_get_prio(skb
);
1161 key_vlan
->vlan_id
= ntohs(vlan
->h_vlan_TCI
) &
1163 key_vlan
->vlan_priority
=
1164 (ntohs(vlan
->h_vlan_TCI
) &
1165 VLAN_PRIO_MASK
) >> VLAN_PRIO_SHIFT
;
1167 key_vlan
->vlan_tpid
= saved_vlan_tpid
;
1170 fdret
= FLOW_DISSECT_RET_PROTO_AGAIN
;
1173 case htons(ETH_P_PPP_SES
): {
1175 struct pppoe_hdr hdr
;
1178 hdr
= __skb_header_pointer(skb
, nhoff
, sizeof(_hdr
), data
, hlen
, &_hdr
);
1180 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
1185 nhoff
+= PPPOE_SES_HLEN
;
1188 proto
= htons(ETH_P_IP
);
1189 fdret
= FLOW_DISSECT_RET_PROTO_AGAIN
;
1191 case htons(PPP_IPV6
):
1192 proto
= htons(ETH_P_IPV6
);
1193 fdret
= FLOW_DISSECT_RET_PROTO_AGAIN
;
1196 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
1201 case htons(ETH_P_TIPC
): {
1202 struct tipc_basic_hdr
*hdr
, _hdr
;
1204 hdr
= __skb_header_pointer(skb
, nhoff
, sizeof(_hdr
),
1207 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
1211 if (dissector_uses_key(flow_dissector
,
1212 FLOW_DISSECTOR_KEY_TIPC
)) {
1213 key_addrs
= skb_flow_dissector_target(flow_dissector
,
1214 FLOW_DISSECTOR_KEY_TIPC
,
1216 key_addrs
->tipckey
.key
= tipc_hdr_rps_key(hdr
);
1217 key_control
->addr_type
= FLOW_DISSECTOR_KEY_TIPC
;
1219 fdret
= FLOW_DISSECT_RET_OUT_GOOD
;
1223 case htons(ETH_P_MPLS_UC
):
1224 case htons(ETH_P_MPLS_MC
):
1225 fdret
= __skb_flow_dissect_mpls(skb
, flow_dissector
,
1226 target_container
, data
,
1227 nhoff
, hlen
, mpls_lse
,
1229 nhoff
+= sizeof(struct mpls_label
);
1232 case htons(ETH_P_FCOE
):
1233 if ((hlen
- nhoff
) < FCOE_HEADER_LEN
) {
1234 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
1238 nhoff
+= FCOE_HEADER_LEN
;
1239 fdret
= FLOW_DISSECT_RET_OUT_GOOD
;
1242 case htons(ETH_P_ARP
):
1243 case htons(ETH_P_RARP
):
1244 fdret
= __skb_flow_dissect_arp(skb
, flow_dissector
,
1245 target_container
, data
,
1249 case htons(ETH_P_BATMAN
):
1250 fdret
= __skb_flow_dissect_batadv(skb
, key_control
, data
,
1251 &proto
, &nhoff
, hlen
, flags
);
1255 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
1259 /* Process result of proto processing */
1261 case FLOW_DISSECT_RET_OUT_GOOD
:
1263 case FLOW_DISSECT_RET_PROTO_AGAIN
:
1264 if (skb_flow_dissect_allowed(&num_hdrs
))
1267 case FLOW_DISSECT_RET_CONTINUE
:
1268 case FLOW_DISSECT_RET_IPPROTO_AGAIN
:
1270 case FLOW_DISSECT_RET_OUT_BAD
:
1276 fdret
= FLOW_DISSECT_RET_CONTINUE
;
1280 fdret
= __skb_flow_dissect_gre(skb
, key_control
, flow_dissector
,
1281 target_container
, data
,
1282 &proto
, &nhoff
, &hlen
, flags
);
1286 case NEXTHDR_ROUTING
:
1287 case NEXTHDR_DEST
: {
1288 u8 _opthdr
[2], *opthdr
;
1290 if (proto
!= htons(ETH_P_IPV6
))
1293 opthdr
= __skb_header_pointer(skb
, nhoff
, sizeof(_opthdr
),
1294 data
, hlen
, &_opthdr
);
1296 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
1300 ip_proto
= opthdr
[0];
1301 nhoff
+= (opthdr
[1] + 1) << 3;
1303 fdret
= FLOW_DISSECT_RET_IPPROTO_AGAIN
;
1306 case NEXTHDR_FRAGMENT
: {
1307 struct frag_hdr _fh
, *fh
;
1309 if (proto
!= htons(ETH_P_IPV6
))
1312 fh
= __skb_header_pointer(skb
, nhoff
, sizeof(_fh
),
1316 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
1320 key_control
->flags
|= FLOW_DIS_IS_FRAGMENT
;
1322 nhoff
+= sizeof(_fh
);
1323 ip_proto
= fh
->nexthdr
;
1325 if (!(fh
->frag_off
& htons(IP6_OFFSET
))) {
1326 key_control
->flags
|= FLOW_DIS_FIRST_FRAG
;
1327 if (flags
& FLOW_DISSECTOR_F_PARSE_1ST_FRAG
) {
1328 fdret
= FLOW_DISSECT_RET_IPPROTO_AGAIN
;
1333 fdret
= FLOW_DISSECT_RET_OUT_GOOD
;
1337 proto
= htons(ETH_P_IP
);
1339 key_control
->flags
|= FLOW_DIS_ENCAPSULATION
;
1340 if (flags
& FLOW_DISSECTOR_F_STOP_AT_ENCAP
) {
1341 fdret
= FLOW_DISSECT_RET_OUT_GOOD
;
1345 fdret
= FLOW_DISSECT_RET_PROTO_AGAIN
;
1349 proto
= htons(ETH_P_IPV6
);
1351 key_control
->flags
|= FLOW_DIS_ENCAPSULATION
;
1352 if (flags
& FLOW_DISSECTOR_F_STOP_AT_ENCAP
) {
1353 fdret
= FLOW_DISSECT_RET_OUT_GOOD
;
1357 fdret
= FLOW_DISSECT_RET_PROTO_AGAIN
;
1362 proto
= htons(ETH_P_MPLS_UC
);
1363 fdret
= FLOW_DISSECT_RET_PROTO_AGAIN
;
1367 __skb_flow_dissect_tcp(skb
, flow_dissector
, target_container
,
1372 case IPPROTO_ICMPV6
:
1373 __skb_flow_dissect_icmp(skb
, flow_dissector
, target_container
,
1381 if (!(key_control
->flags
& FLOW_DIS_IS_FRAGMENT
))
1382 __skb_flow_dissect_ports(skb
, flow_dissector
, target_container
,
1383 data
, nhoff
, ip_proto
, hlen
);
1385 /* Process result of IP proto processing */
1387 case FLOW_DISSECT_RET_PROTO_AGAIN
:
1388 if (skb_flow_dissect_allowed(&num_hdrs
))
1391 case FLOW_DISSECT_RET_IPPROTO_AGAIN
:
1392 if (skb_flow_dissect_allowed(&num_hdrs
))
1393 goto ip_proto_again
;
1395 case FLOW_DISSECT_RET_OUT_GOOD
:
1396 case FLOW_DISSECT_RET_CONTINUE
:
1398 case FLOW_DISSECT_RET_OUT_BAD
:
1407 key_control
->thoff
= min_t(u16
, nhoff
, skb
? skb
->len
: hlen
);
1408 key_basic
->n_proto
= proto
;
1409 key_basic
->ip_proto
= ip_proto
;
1417 EXPORT_SYMBOL(__skb_flow_dissect
);
1419 static siphash_key_t hashrnd __read_mostly
;
1420 static __always_inline
void __flow_hash_secret_init(void)
1422 net_get_random_once(&hashrnd
, sizeof(hashrnd
));
1425 static const void *flow_keys_hash_start(const struct flow_keys
*flow
)
1427 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET
% SIPHASH_ALIGNMENT
);
1428 return &flow
->FLOW_KEYS_HASH_START_FIELD
;
1431 static inline size_t flow_keys_hash_length(const struct flow_keys
*flow
)
1433 size_t diff
= FLOW_KEYS_HASH_OFFSET
+ sizeof(flow
->addrs
);
1435 BUILD_BUG_ON((sizeof(*flow
) - FLOW_KEYS_HASH_OFFSET
) % sizeof(u32
));
1437 switch (flow
->control
.addr_type
) {
1438 case FLOW_DISSECTOR_KEY_IPV4_ADDRS
:
1439 diff
-= sizeof(flow
->addrs
.v4addrs
);
1441 case FLOW_DISSECTOR_KEY_IPV6_ADDRS
:
1442 diff
-= sizeof(flow
->addrs
.v6addrs
);
1444 case FLOW_DISSECTOR_KEY_TIPC
:
1445 diff
-= sizeof(flow
->addrs
.tipckey
);
1448 return sizeof(*flow
) - diff
;
1451 __be32
flow_get_u32_src(const struct flow_keys
*flow
)
1453 switch (flow
->control
.addr_type
) {
1454 case FLOW_DISSECTOR_KEY_IPV4_ADDRS
:
1455 return flow
->addrs
.v4addrs
.src
;
1456 case FLOW_DISSECTOR_KEY_IPV6_ADDRS
:
1457 return (__force __be32
)ipv6_addr_hash(
1458 &flow
->addrs
.v6addrs
.src
);
1459 case FLOW_DISSECTOR_KEY_TIPC
:
1460 return flow
->addrs
.tipckey
.key
;
1465 EXPORT_SYMBOL(flow_get_u32_src
);
1467 __be32
flow_get_u32_dst(const struct flow_keys
*flow
)
1469 switch (flow
->control
.addr_type
) {
1470 case FLOW_DISSECTOR_KEY_IPV4_ADDRS
:
1471 return flow
->addrs
.v4addrs
.dst
;
1472 case FLOW_DISSECTOR_KEY_IPV6_ADDRS
:
1473 return (__force __be32
)ipv6_addr_hash(
1474 &flow
->addrs
.v6addrs
.dst
);
1479 EXPORT_SYMBOL(flow_get_u32_dst
);
1481 /* Sort the source and destination IP (and the ports if the IP are the same),
1482 * to have consistent hash within the two directions
1484 static inline void __flow_hash_consistentify(struct flow_keys
*keys
)
1488 switch (keys
->control
.addr_type
) {
1489 case FLOW_DISSECTOR_KEY_IPV4_ADDRS
:
1490 addr_diff
= (__force u32
)keys
->addrs
.v4addrs
.dst
-
1491 (__force u32
)keys
->addrs
.v4addrs
.src
;
1492 if ((addr_diff
< 0) ||
1494 ((__force u16
)keys
->ports
.dst
<
1495 (__force u16
)keys
->ports
.src
))) {
1496 swap(keys
->addrs
.v4addrs
.src
, keys
->addrs
.v4addrs
.dst
);
1497 swap(keys
->ports
.src
, keys
->ports
.dst
);
1500 case FLOW_DISSECTOR_KEY_IPV6_ADDRS
:
1501 addr_diff
= memcmp(&keys
->addrs
.v6addrs
.dst
,
1502 &keys
->addrs
.v6addrs
.src
,
1503 sizeof(keys
->addrs
.v6addrs
.dst
));
1504 if ((addr_diff
< 0) ||
1506 ((__force u16
)keys
->ports
.dst
<
1507 (__force u16
)keys
->ports
.src
))) {
1508 for (i
= 0; i
< 4; i
++)
1509 swap(keys
->addrs
.v6addrs
.src
.s6_addr32
[i
],
1510 keys
->addrs
.v6addrs
.dst
.s6_addr32
[i
]);
1511 swap(keys
->ports
.src
, keys
->ports
.dst
);
1517 static inline u32
__flow_hash_from_keys(struct flow_keys
*keys
,
1518 const siphash_key_t
*keyval
)
1522 __flow_hash_consistentify(keys
);
1524 hash
= siphash(flow_keys_hash_start(keys
),
1525 flow_keys_hash_length(keys
), keyval
);
1532 u32
flow_hash_from_keys(struct flow_keys
*keys
)
1534 __flow_hash_secret_init();
1535 return __flow_hash_from_keys(keys
, &hashrnd
);
1537 EXPORT_SYMBOL(flow_hash_from_keys
);
1539 static inline u32
___skb_get_hash(const struct sk_buff
*skb
,
1540 struct flow_keys
*keys
,
1541 const siphash_key_t
*keyval
)
1543 skb_flow_dissect_flow_keys(skb
, keys
,
1544 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
);
1546 return __flow_hash_from_keys(keys
, keyval
);
1549 struct _flow_keys_digest_data
{
1558 void make_flow_keys_digest(struct flow_keys_digest
*digest
,
1559 const struct flow_keys
*flow
)
1561 struct _flow_keys_digest_data
*data
=
1562 (struct _flow_keys_digest_data
*)digest
;
1564 BUILD_BUG_ON(sizeof(*data
) > sizeof(*digest
));
1566 memset(digest
, 0, sizeof(*digest
));
1568 data
->n_proto
= flow
->basic
.n_proto
;
1569 data
->ip_proto
= flow
->basic
.ip_proto
;
1570 data
->ports
= flow
->ports
.ports
;
1571 data
->src
= flow
->addrs
.v4addrs
.src
;
1572 data
->dst
= flow
->addrs
.v4addrs
.dst
;
1574 EXPORT_SYMBOL(make_flow_keys_digest
);
1576 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly
;
1578 u32
__skb_get_hash_symmetric(const struct sk_buff
*skb
)
1580 struct flow_keys keys
;
1582 __flow_hash_secret_init();
1584 memset(&keys
, 0, sizeof(keys
));
1585 __skb_flow_dissect(NULL
, skb
, &flow_keys_dissector_symmetric
,
1586 &keys
, NULL
, 0, 0, 0,
1587 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
);
1589 return __flow_hash_from_keys(&keys
, &hashrnd
);
1591 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric
);
1594 * __skb_get_hash: calculate a flow hash
1595 * @skb: sk_buff to calculate flow hash from
1597 * This function calculates a flow hash based on src/dst addresses
1598 * and src/dst port numbers. Sets hash in skb to non-zero hash value
1599 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
1600 * if hash is a canonical 4-tuple hash over transport ports.
1602 void __skb_get_hash(struct sk_buff
*skb
)
1604 struct flow_keys keys
;
1607 __flow_hash_secret_init();
1609 hash
= ___skb_get_hash(skb
, &keys
, &hashrnd
);
1611 __skb_set_sw_hash(skb
, hash
, flow_keys_have_l4(&keys
));
1613 EXPORT_SYMBOL(__skb_get_hash
);
1615 __u32
skb_get_hash_perturb(const struct sk_buff
*skb
,
1616 const siphash_key_t
*perturb
)
1618 struct flow_keys keys
;
1620 return ___skb_get_hash(skb
, &keys
, perturb
);
1622 EXPORT_SYMBOL(skb_get_hash_perturb
);
1624 u32
__skb_get_poff(const struct sk_buff
*skb
, void *data
,
1625 const struct flow_keys_basic
*keys
, int hlen
)
1627 u32 poff
= keys
->control
.thoff
;
1629 /* skip L4 headers for fragments after the first */
1630 if ((keys
->control
.flags
& FLOW_DIS_IS_FRAGMENT
) &&
1631 !(keys
->control
.flags
& FLOW_DIS_FIRST_FRAG
))
1634 switch (keys
->basic
.ip_proto
) {
1636 /* access doff as u8 to avoid unaligned access */
1640 doff
= __skb_header_pointer(skb
, poff
+ 12, sizeof(_doff
),
1641 data
, hlen
, &_doff
);
1645 poff
+= max_t(u32
, sizeof(struct tcphdr
), (*doff
& 0xF0) >> 2);
1649 case IPPROTO_UDPLITE
:
1650 poff
+= sizeof(struct udphdr
);
1652 /* For the rest, we do not really care about header
1653 * extensions at this point for now.
1656 poff
+= sizeof(struct icmphdr
);
1658 case IPPROTO_ICMPV6
:
1659 poff
+= sizeof(struct icmp6hdr
);
1662 poff
+= sizeof(struct igmphdr
);
1665 poff
+= sizeof(struct dccp_hdr
);
1668 poff
+= sizeof(struct sctphdr
);
1676 * skb_get_poff - get the offset to the payload
1677 * @skb: sk_buff to get the payload offset from
1679 * The function will get the offset to the payload as far as it could
1680 * be dissected. The main user is currently BPF, so that we can dynamically
1681 * truncate packets without needing to push actual payload to the user
1682 * space and can analyze headers only, instead.
1684 u32
skb_get_poff(const struct sk_buff
*skb
)
1686 struct flow_keys_basic keys
;
1688 if (!skb_flow_dissect_flow_keys_basic(NULL
, skb
, &keys
,
1692 return __skb_get_poff(skb
, skb
->data
, &keys
, skb_headlen(skb
));
1695 __u32
__get_hash_from_flowi6(const struct flowi6
*fl6
, struct flow_keys
*keys
)
1697 memset(keys
, 0, sizeof(*keys
));
1699 memcpy(&keys
->addrs
.v6addrs
.src
, &fl6
->saddr
,
1700 sizeof(keys
->addrs
.v6addrs
.src
));
1701 memcpy(&keys
->addrs
.v6addrs
.dst
, &fl6
->daddr
,
1702 sizeof(keys
->addrs
.v6addrs
.dst
));
1703 keys
->control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
1704 keys
->ports
.src
= fl6
->fl6_sport
;
1705 keys
->ports
.dst
= fl6
->fl6_dport
;
1706 keys
->keyid
.keyid
= fl6
->fl6_gre_key
;
1707 keys
->tags
.flow_label
= (__force u32
)flowi6_get_flowlabel(fl6
);
1708 keys
->basic
.ip_proto
= fl6
->flowi6_proto
;
1710 return flow_hash_from_keys(keys
);
1712 EXPORT_SYMBOL(__get_hash_from_flowi6
);
1714 static const struct flow_dissector_key flow_keys_dissector_keys
[] = {
1716 .key_id
= FLOW_DISSECTOR_KEY_CONTROL
,
1717 .offset
= offsetof(struct flow_keys
, control
),
1720 .key_id
= FLOW_DISSECTOR_KEY_BASIC
,
1721 .offset
= offsetof(struct flow_keys
, basic
),
1724 .key_id
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
,
1725 .offset
= offsetof(struct flow_keys
, addrs
.v4addrs
),
1728 .key_id
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
,
1729 .offset
= offsetof(struct flow_keys
, addrs
.v6addrs
),
1732 .key_id
= FLOW_DISSECTOR_KEY_TIPC
,
1733 .offset
= offsetof(struct flow_keys
, addrs
.tipckey
),
1736 .key_id
= FLOW_DISSECTOR_KEY_PORTS
,
1737 .offset
= offsetof(struct flow_keys
, ports
),
1740 .key_id
= FLOW_DISSECTOR_KEY_VLAN
,
1741 .offset
= offsetof(struct flow_keys
, vlan
),
1744 .key_id
= FLOW_DISSECTOR_KEY_FLOW_LABEL
,
1745 .offset
= offsetof(struct flow_keys
, tags
),
1748 .key_id
= FLOW_DISSECTOR_KEY_GRE_KEYID
,
1749 .offset
= offsetof(struct flow_keys
, keyid
),
1753 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys
[] = {
1755 .key_id
= FLOW_DISSECTOR_KEY_CONTROL
,
1756 .offset
= offsetof(struct flow_keys
, control
),
1759 .key_id
= FLOW_DISSECTOR_KEY_BASIC
,
1760 .offset
= offsetof(struct flow_keys
, basic
),
1763 .key_id
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
,
1764 .offset
= offsetof(struct flow_keys
, addrs
.v4addrs
),
1767 .key_id
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
,
1768 .offset
= offsetof(struct flow_keys
, addrs
.v6addrs
),
1771 .key_id
= FLOW_DISSECTOR_KEY_PORTS
,
1772 .offset
= offsetof(struct flow_keys
, ports
),
1776 static const struct flow_dissector_key flow_keys_basic_dissector_keys
[] = {
1778 .key_id
= FLOW_DISSECTOR_KEY_CONTROL
,
1779 .offset
= offsetof(struct flow_keys
, control
),
1782 .key_id
= FLOW_DISSECTOR_KEY_BASIC
,
1783 .offset
= offsetof(struct flow_keys
, basic
),
1787 struct flow_dissector flow_keys_dissector __read_mostly
;
1788 EXPORT_SYMBOL(flow_keys_dissector
);
1790 struct flow_dissector flow_keys_basic_dissector __read_mostly
;
1791 EXPORT_SYMBOL(flow_keys_basic_dissector
);
1793 static int __init
init_default_flow_dissectors(void)
1795 skb_flow_dissector_init(&flow_keys_dissector
,
1796 flow_keys_dissector_keys
,
1797 ARRAY_SIZE(flow_keys_dissector_keys
));
1798 skb_flow_dissector_init(&flow_keys_dissector_symmetric
,
1799 flow_keys_dissector_symmetric_keys
,
1800 ARRAY_SIZE(flow_keys_dissector_symmetric_keys
));
1801 skb_flow_dissector_init(&flow_keys_basic_dissector
,
1802 flow_keys_basic_dissector_keys
,
1803 ARRAY_SIZE(flow_keys_basic_dissector_keys
));
1806 core_initcall(init_default_flow_dissectors
);