1 #include <linux/kernel.h>
2 #include <linux/skbuff.h>
3 #include <linux/export.h>
5 #include <linux/ipv6.h>
6 #include <linux/if_vlan.h>
8 #include <net/dst_metadata.h>
14 #include <linux/igmp.h>
15 #include <linux/icmp.h>
16 #include <linux/sctp.h>
17 #include <linux/dccp.h>
18 #include <linux/if_tunnel.h>
19 #include <linux/if_pppox.h>
20 #include <linux/ppp_defs.h>
21 #include <linux/stddef.h>
22 #include <linux/if_ether.h>
23 #include <linux/mpls.h>
24 #include <linux/tcp.h>
25 #include <net/flow_dissector.h>
26 #include <scsi/fc/fc_fcoe.h>
28 static void dissector_set_key(struct flow_dissector
*flow_dissector
,
29 enum flow_dissector_key_id key_id
)
31 flow_dissector
->used_keys
|= (1 << key_id
);
34 void skb_flow_dissector_init(struct flow_dissector
*flow_dissector
,
35 const struct flow_dissector_key
*key
,
36 unsigned int key_count
)
40 memset(flow_dissector
, 0, sizeof(*flow_dissector
));
42 for (i
= 0; i
< key_count
; i
++, key
++) {
43 /* User should make sure that every key target offset is withing
44 * boundaries of unsigned short.
46 BUG_ON(key
->offset
> USHRT_MAX
);
47 BUG_ON(dissector_uses_key(flow_dissector
,
50 dissector_set_key(flow_dissector
, key
->key_id
);
51 flow_dissector
->offset
[key
->key_id
] = key
->offset
;
54 /* Ensure that the dissector always includes control and basic key.
55 * That way we are able to avoid handling lack of these in fast path.
57 BUG_ON(!dissector_uses_key(flow_dissector
,
58 FLOW_DISSECTOR_KEY_CONTROL
));
59 BUG_ON(!dissector_uses_key(flow_dissector
,
60 FLOW_DISSECTOR_KEY_BASIC
));
62 EXPORT_SYMBOL(skb_flow_dissector_init
);
65 * skb_flow_get_be16 - extract be16 entity
66 * @skb: sk_buff to extract from
67 * @poff: offset to extract at
68 * @data: raw buffer pointer to the packet
69 * @hlen: packet header length
71 * The function will try to retrieve a be32 entity at
74 static __be16
skb_flow_get_be16(const struct sk_buff
*skb
, int poff
,
79 u
= __skb_header_pointer(skb
, poff
, sizeof(_u
), data
, hlen
, &_u
);
87 * __skb_flow_get_ports - extract the upper layer ports and return them
88 * @skb: sk_buff to extract the ports from
89 * @thoff: transport header offset
90 * @ip_proto: protocol for which to get port offset
91 * @data: raw buffer pointer to the packet, if NULL use skb->data
92 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
94 * The function will try to retrieve the ports at offset thoff + poff where poff
95 * is the protocol port offset returned from proto_ports_offset
97 __be32
__skb_flow_get_ports(const struct sk_buff
*skb
, int thoff
, u8 ip_proto
,
100 int poff
= proto_ports_offset(ip_proto
);
104 hlen
= skb_headlen(skb
);
108 __be32
*ports
, _ports
;
110 ports
= __skb_header_pointer(skb
, thoff
+ poff
,
111 sizeof(_ports
), data
, hlen
, &_ports
);
118 EXPORT_SYMBOL(__skb_flow_get_ports
);
121 skb_flow_dissect_set_enc_addr_type(enum flow_dissector_key_id type
,
122 struct flow_dissector
*flow_dissector
,
123 void *target_container
)
125 struct flow_dissector_key_control
*ctrl
;
127 if (!dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_ENC_CONTROL
))
130 ctrl
= skb_flow_dissector_target(flow_dissector
,
131 FLOW_DISSECTOR_KEY_ENC_CONTROL
,
133 ctrl
->addr_type
= type
;
137 __skb_flow_dissect_tunnel_info(const struct sk_buff
*skb
,
138 struct flow_dissector
*flow_dissector
,
139 void *target_container
)
141 struct ip_tunnel_info
*info
;
142 struct ip_tunnel_key
*key
;
144 /* A quick check to see if there might be something to do. */
145 if (!dissector_uses_key(flow_dissector
,
146 FLOW_DISSECTOR_KEY_ENC_KEYID
) &&
147 !dissector_uses_key(flow_dissector
,
148 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS
) &&
149 !dissector_uses_key(flow_dissector
,
150 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS
) &&
151 !dissector_uses_key(flow_dissector
,
152 FLOW_DISSECTOR_KEY_ENC_CONTROL
) &&
153 !dissector_uses_key(flow_dissector
,
154 FLOW_DISSECTOR_KEY_ENC_PORTS
))
157 info
= skb_tunnel_info(skb
);
163 switch (ip_tunnel_info_af(info
)) {
165 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV4_ADDRS
,
168 if (dissector_uses_key(flow_dissector
,
169 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS
)) {
170 struct flow_dissector_key_ipv4_addrs
*ipv4
;
172 ipv4
= skb_flow_dissector_target(flow_dissector
,
173 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS
,
175 ipv4
->src
= key
->u
.ipv4
.src
;
176 ipv4
->dst
= key
->u
.ipv4
.dst
;
180 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV6_ADDRS
,
183 if (dissector_uses_key(flow_dissector
,
184 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS
)) {
185 struct flow_dissector_key_ipv6_addrs
*ipv6
;
187 ipv6
= skb_flow_dissector_target(flow_dissector
,
188 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS
,
190 ipv6
->src
= key
->u
.ipv6
.src
;
191 ipv6
->dst
= key
->u
.ipv6
.dst
;
196 if (dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_ENC_KEYID
)) {
197 struct flow_dissector_key_keyid
*keyid
;
199 keyid
= skb_flow_dissector_target(flow_dissector
,
200 FLOW_DISSECTOR_KEY_ENC_KEYID
,
202 keyid
->keyid
= tunnel_id_to_key32(key
->tun_id
);
205 if (dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_ENC_PORTS
)) {
206 struct flow_dissector_key_ports
*tp
;
208 tp
= skb_flow_dissector_target(flow_dissector
,
209 FLOW_DISSECTOR_KEY_ENC_PORTS
,
211 tp
->src
= key
->tp_src
;
212 tp
->dst
= key
->tp_dst
;
216 static enum flow_dissect_ret
217 __skb_flow_dissect_mpls(const struct sk_buff
*skb
,
218 struct flow_dissector
*flow_dissector
,
219 void *target_container
, void *data
, int nhoff
, int hlen
)
221 struct flow_dissector_key_keyid
*key_keyid
;
222 struct mpls_label
*hdr
, _hdr
[2];
225 if (!dissector_uses_key(flow_dissector
,
226 FLOW_DISSECTOR_KEY_MPLS_ENTROPY
) &&
227 !dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_MPLS
))
228 return FLOW_DISSECT_RET_OUT_GOOD
;
230 hdr
= __skb_header_pointer(skb
, nhoff
, sizeof(_hdr
), data
,
233 return FLOW_DISSECT_RET_OUT_BAD
;
235 entry
= ntohl(hdr
[0].entry
);
236 label
= (entry
& MPLS_LS_LABEL_MASK
) >> MPLS_LS_LABEL_SHIFT
;
238 if (dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_MPLS
)) {
239 struct flow_dissector_key_mpls
*key_mpls
;
241 key_mpls
= skb_flow_dissector_target(flow_dissector
,
242 FLOW_DISSECTOR_KEY_MPLS
,
244 key_mpls
->mpls_label
= label
;
245 key_mpls
->mpls_ttl
= (entry
& MPLS_LS_TTL_MASK
)
246 >> MPLS_LS_TTL_SHIFT
;
247 key_mpls
->mpls_tc
= (entry
& MPLS_LS_TC_MASK
)
249 key_mpls
->mpls_bos
= (entry
& MPLS_LS_S_MASK
)
253 if (label
== MPLS_LABEL_ENTROPY
) {
254 key_keyid
= skb_flow_dissector_target(flow_dissector
,
255 FLOW_DISSECTOR_KEY_MPLS_ENTROPY
,
257 key_keyid
->keyid
= hdr
[1].entry
& htonl(MPLS_LS_LABEL_MASK
);
259 return FLOW_DISSECT_RET_OUT_GOOD
;
262 static enum flow_dissect_ret
263 __skb_flow_dissect_arp(const struct sk_buff
*skb
,
264 struct flow_dissector
*flow_dissector
,
265 void *target_container
, void *data
, int nhoff
, int hlen
)
267 struct flow_dissector_key_arp
*key_arp
;
269 unsigned char ar_sha
[ETH_ALEN
];
270 unsigned char ar_sip
[4];
271 unsigned char ar_tha
[ETH_ALEN
];
272 unsigned char ar_tip
[4];
273 } *arp_eth
, _arp_eth
;
274 const struct arphdr
*arp
;
277 if (!dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_ARP
))
278 return FLOW_DISSECT_RET_OUT_GOOD
;
280 arp
= __skb_header_pointer(skb
, nhoff
, sizeof(_arp
), data
,
283 return FLOW_DISSECT_RET_OUT_BAD
;
285 if (arp
->ar_hrd
!= htons(ARPHRD_ETHER
) ||
286 arp
->ar_pro
!= htons(ETH_P_IP
) ||
287 arp
->ar_hln
!= ETH_ALEN
||
289 (arp
->ar_op
!= htons(ARPOP_REPLY
) &&
290 arp
->ar_op
!= htons(ARPOP_REQUEST
)))
291 return FLOW_DISSECT_RET_OUT_BAD
;
293 arp_eth
= __skb_header_pointer(skb
, nhoff
+ sizeof(_arp
),
294 sizeof(_arp_eth
), data
,
297 return FLOW_DISSECT_RET_OUT_BAD
;
299 key_arp
= skb_flow_dissector_target(flow_dissector
,
300 FLOW_DISSECTOR_KEY_ARP
,
303 memcpy(&key_arp
->sip
, arp_eth
->ar_sip
, sizeof(key_arp
->sip
));
304 memcpy(&key_arp
->tip
, arp_eth
->ar_tip
, sizeof(key_arp
->tip
));
306 /* Only store the lower byte of the opcode;
307 * this covers ARPOP_REPLY and ARPOP_REQUEST.
309 key_arp
->op
= ntohs(arp
->ar_op
) & 0xff;
311 ether_addr_copy(key_arp
->sha
, arp_eth
->ar_sha
);
312 ether_addr_copy(key_arp
->tha
, arp_eth
->ar_tha
);
314 return FLOW_DISSECT_RET_OUT_GOOD
;
317 static enum flow_dissect_ret
318 __skb_flow_dissect_gre(const struct sk_buff
*skb
,
319 struct flow_dissector_key_control
*key_control
,
320 struct flow_dissector
*flow_dissector
,
321 void *target_container
, void *data
,
322 __be16
*p_proto
, int *p_nhoff
, int *p_hlen
,
325 struct flow_dissector_key_keyid
*key_keyid
;
326 struct gre_base_hdr
*hdr
, _hdr
;
330 hdr
= __skb_header_pointer(skb
, *p_nhoff
, sizeof(_hdr
),
331 data
, *p_hlen
, &_hdr
);
333 return FLOW_DISSECT_RET_OUT_BAD
;
335 /* Only look inside GRE without routing */
336 if (hdr
->flags
& GRE_ROUTING
)
337 return FLOW_DISSECT_RET_OUT_GOOD
;
339 /* Only look inside GRE for version 0 and 1 */
340 gre_ver
= ntohs(hdr
->flags
& GRE_VERSION
);
342 return FLOW_DISSECT_RET_OUT_GOOD
;
344 *p_proto
= hdr
->protocol
;
346 /* Version1 must be PPTP, and check the flags */
347 if (!(*p_proto
== GRE_PROTO_PPP
&& (hdr
->flags
& GRE_KEY
)))
348 return FLOW_DISSECT_RET_OUT_GOOD
;
351 offset
+= sizeof(struct gre_base_hdr
);
353 if (hdr
->flags
& GRE_CSUM
)
354 offset
+= sizeof(((struct gre_full_hdr
*) 0)->csum
) +
355 sizeof(((struct gre_full_hdr
*) 0)->reserved1
);
357 if (hdr
->flags
& GRE_KEY
) {
361 keyid
= __skb_header_pointer(skb
, *p_nhoff
+ offset
,
363 data
, *p_hlen
, &_keyid
);
365 return FLOW_DISSECT_RET_OUT_BAD
;
367 if (dissector_uses_key(flow_dissector
,
368 FLOW_DISSECTOR_KEY_GRE_KEYID
)) {
369 key_keyid
= skb_flow_dissector_target(flow_dissector
,
370 FLOW_DISSECTOR_KEY_GRE_KEYID
,
373 key_keyid
->keyid
= *keyid
;
375 key_keyid
->keyid
= *keyid
& GRE_PPTP_KEY_MASK
;
377 offset
+= sizeof(((struct gre_full_hdr
*) 0)->key
);
380 if (hdr
->flags
& GRE_SEQ
)
381 offset
+= sizeof(((struct pptp_gre_header
*) 0)->seq
);
384 if (*p_proto
== htons(ETH_P_TEB
)) {
385 const struct ethhdr
*eth
;
388 eth
= __skb_header_pointer(skb
, *p_nhoff
+ offset
,
390 data
, *p_hlen
, &_eth
);
392 return FLOW_DISSECT_RET_OUT_BAD
;
393 *p_proto
= eth
->h_proto
;
394 offset
+= sizeof(*eth
);
396 /* Cap headers that we access via pointers at the
397 * end of the Ethernet header as our maximum alignment
398 * at that point is only 2 bytes.
401 *p_hlen
= *p_nhoff
+ offset
;
403 } else { /* version 1, must be PPTP */
404 u8 _ppp_hdr
[PPP_HDRLEN
];
407 if (hdr
->flags
& GRE_ACK
)
408 offset
+= sizeof(((struct pptp_gre_header
*) 0)->ack
);
410 ppp_hdr
= __skb_header_pointer(skb
, *p_nhoff
+ offset
,
412 data
, *p_hlen
, _ppp_hdr
);
414 return FLOW_DISSECT_RET_OUT_BAD
;
416 switch (PPP_PROTOCOL(ppp_hdr
)) {
418 *p_proto
= htons(ETH_P_IP
);
421 *p_proto
= htons(ETH_P_IPV6
);
424 /* Could probably catch some more like MPLS */
428 offset
+= PPP_HDRLEN
;
432 key_control
->flags
|= FLOW_DIS_ENCAPSULATION
;
433 if (flags
& FLOW_DISSECTOR_F_STOP_AT_ENCAP
)
434 return FLOW_DISSECT_RET_OUT_GOOD
;
436 return FLOW_DISSECT_RET_PROTO_AGAIN
;
440 __skb_flow_dissect_tcp(const struct sk_buff
*skb
,
441 struct flow_dissector
*flow_dissector
,
442 void *target_container
, void *data
, int thoff
, int hlen
)
444 struct flow_dissector_key_tcp
*key_tcp
;
445 struct tcphdr
*th
, _th
;
447 if (!dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_TCP
))
450 th
= __skb_header_pointer(skb
, thoff
, sizeof(_th
), data
, hlen
, &_th
);
454 if (unlikely(__tcp_hdrlen(th
) < sizeof(_th
)))
457 key_tcp
= skb_flow_dissector_target(flow_dissector
,
458 FLOW_DISSECTOR_KEY_TCP
,
460 key_tcp
->flags
= (*(__be16
*) &tcp_flag_word(th
) & htons(0x0FFF));
464 __skb_flow_dissect_ipv4(const struct sk_buff
*skb
,
465 struct flow_dissector
*flow_dissector
,
466 void *target_container
, void *data
, const struct iphdr
*iph
)
468 struct flow_dissector_key_ip
*key_ip
;
470 if (!dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_IP
))
473 key_ip
= skb_flow_dissector_target(flow_dissector
,
474 FLOW_DISSECTOR_KEY_IP
,
476 key_ip
->tos
= iph
->tos
;
477 key_ip
->ttl
= iph
->ttl
;
481 __skb_flow_dissect_ipv6(const struct sk_buff
*skb
,
482 struct flow_dissector
*flow_dissector
,
483 void *target_container
, void *data
, const struct ipv6hdr
*iph
)
485 struct flow_dissector_key_ip
*key_ip
;
487 if (!dissector_uses_key(flow_dissector
, FLOW_DISSECTOR_KEY_IP
))
490 key_ip
= skb_flow_dissector_target(flow_dissector
,
491 FLOW_DISSECTOR_KEY_IP
,
493 key_ip
->tos
= ipv6_get_dsfield(iph
);
494 key_ip
->ttl
= iph
->hop_limit
;
497 /* Maximum number of protocol headers that can be parsed in
500 #define MAX_FLOW_DISSECT_HDRS 15
502 static bool skb_flow_dissect_allowed(int *num_hdrs
)
506 return (*num_hdrs
<= MAX_FLOW_DISSECT_HDRS
);
510 * __skb_flow_dissect - extract the flow_keys struct and return it
511 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
512 * @flow_dissector: list of keys to dissect
513 * @target_container: target structure to put dissected values into
514 * @data: raw buffer pointer to the packet, if NULL use skb->data
515 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
516 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
517 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
519 * The function will try to retrieve individual keys into target specified
520 * by flow_dissector from either the skbuff or a raw buffer specified by the
523 * Caller must take care of zeroing target container memory.
525 bool __skb_flow_dissect(const struct sk_buff
*skb
,
526 struct flow_dissector
*flow_dissector
,
527 void *target_container
,
528 void *data
, __be16 proto
, int nhoff
, int hlen
,
531 struct flow_dissector_key_control
*key_control
;
532 struct flow_dissector_key_basic
*key_basic
;
533 struct flow_dissector_key_addrs
*key_addrs
;
534 struct flow_dissector_key_ports
*key_ports
;
535 struct flow_dissector_key_icmp
*key_icmp
;
536 struct flow_dissector_key_tags
*key_tags
;
537 struct flow_dissector_key_vlan
*key_vlan
;
538 enum flow_dissect_ret fdret
;
539 bool skip_vlan
= false;
546 proto
= skb_vlan_tag_present(skb
) ?
547 skb
->vlan_proto
: skb
->protocol
;
548 nhoff
= skb_network_offset(skb
);
549 hlen
= skb_headlen(skb
);
550 #if IS_ENABLED(CONFIG_NET_DSA)
551 if (unlikely(skb
->dev
&& netdev_uses_dsa(skb
->dev
))) {
552 const struct dsa_device_ops
*ops
;
555 ops
= skb
->dev
->dsa_ptr
->tag_ops
;
556 if (ops
->flow_dissect
&&
557 !ops
->flow_dissect(skb
, &proto
, &offset
)) {
565 /* It is ensured by skb_flow_dissector_init() that control key will
568 key_control
= skb_flow_dissector_target(flow_dissector
,
569 FLOW_DISSECTOR_KEY_CONTROL
,
572 /* It is ensured by skb_flow_dissector_init() that basic key will
575 key_basic
= skb_flow_dissector_target(flow_dissector
,
576 FLOW_DISSECTOR_KEY_BASIC
,
579 __skb_flow_dissect_tunnel_info(skb
, flow_dissector
,
582 if (dissector_uses_key(flow_dissector
,
583 FLOW_DISSECTOR_KEY_ETH_ADDRS
)) {
584 struct ethhdr
*eth
= eth_hdr(skb
);
585 struct flow_dissector_key_eth_addrs
*key_eth_addrs
;
587 key_eth_addrs
= skb_flow_dissector_target(flow_dissector
,
588 FLOW_DISSECTOR_KEY_ETH_ADDRS
,
590 memcpy(key_eth_addrs
, ð
->h_dest
, sizeof(*key_eth_addrs
));
594 fdret
= FLOW_DISSECT_RET_CONTINUE
;
597 case htons(ETH_P_IP
): {
598 const struct iphdr
*iph
;
601 iph
= __skb_header_pointer(skb
, nhoff
, sizeof(_iph
), data
, hlen
, &_iph
);
602 if (!iph
|| iph
->ihl
< 5) {
603 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
607 nhoff
+= iph
->ihl
* 4;
609 ip_proto
= iph
->protocol
;
611 if (dissector_uses_key(flow_dissector
,
612 FLOW_DISSECTOR_KEY_IPV4_ADDRS
)) {
613 key_addrs
= skb_flow_dissector_target(flow_dissector
,
614 FLOW_DISSECTOR_KEY_IPV4_ADDRS
,
617 memcpy(&key_addrs
->v4addrs
, &iph
->saddr
,
618 sizeof(key_addrs
->v4addrs
));
619 key_control
->addr_type
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
;
622 if (ip_is_fragment(iph
)) {
623 key_control
->flags
|= FLOW_DIS_IS_FRAGMENT
;
625 if (iph
->frag_off
& htons(IP_OFFSET
)) {
626 fdret
= FLOW_DISSECT_RET_OUT_GOOD
;
629 key_control
->flags
|= FLOW_DIS_FIRST_FRAG
;
631 FLOW_DISSECTOR_F_PARSE_1ST_FRAG
)) {
632 fdret
= FLOW_DISSECT_RET_OUT_GOOD
;
638 __skb_flow_dissect_ipv4(skb
, flow_dissector
,
639 target_container
, data
, iph
);
641 if (flags
& FLOW_DISSECTOR_F_STOP_AT_L3
) {
642 fdret
= FLOW_DISSECT_RET_OUT_GOOD
;
648 case htons(ETH_P_IPV6
): {
649 const struct ipv6hdr
*iph
;
652 iph
= __skb_header_pointer(skb
, nhoff
, sizeof(_iph
), data
, hlen
, &_iph
);
654 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
658 ip_proto
= iph
->nexthdr
;
659 nhoff
+= sizeof(struct ipv6hdr
);
661 if (dissector_uses_key(flow_dissector
,
662 FLOW_DISSECTOR_KEY_IPV6_ADDRS
)) {
663 key_addrs
= skb_flow_dissector_target(flow_dissector
,
664 FLOW_DISSECTOR_KEY_IPV6_ADDRS
,
667 memcpy(&key_addrs
->v6addrs
, &iph
->saddr
,
668 sizeof(key_addrs
->v6addrs
));
669 key_control
->addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
672 if ((dissector_uses_key(flow_dissector
,
673 FLOW_DISSECTOR_KEY_FLOW_LABEL
) ||
674 (flags
& FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
)) &&
675 ip6_flowlabel(iph
)) {
676 __be32 flow_label
= ip6_flowlabel(iph
);
678 if (dissector_uses_key(flow_dissector
,
679 FLOW_DISSECTOR_KEY_FLOW_LABEL
)) {
680 key_tags
= skb_flow_dissector_target(flow_dissector
,
681 FLOW_DISSECTOR_KEY_FLOW_LABEL
,
683 key_tags
->flow_label
= ntohl(flow_label
);
685 if (flags
& FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
) {
686 fdret
= FLOW_DISSECT_RET_OUT_GOOD
;
691 __skb_flow_dissect_ipv6(skb
, flow_dissector
,
692 target_container
, data
, iph
);
694 if (flags
& FLOW_DISSECTOR_F_STOP_AT_L3
)
695 fdret
= FLOW_DISSECT_RET_OUT_GOOD
;
699 case htons(ETH_P_8021AD
):
700 case htons(ETH_P_8021Q
): {
701 const struct vlan_hdr
*vlan
;
702 struct vlan_hdr _vlan
;
703 bool vlan_tag_present
= skb
&& skb_vlan_tag_present(skb
);
705 if (vlan_tag_present
)
706 proto
= skb
->protocol
;
708 if (!vlan_tag_present
|| eth_type_vlan(skb
->protocol
)) {
709 vlan
= __skb_header_pointer(skb
, nhoff
, sizeof(_vlan
),
712 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
716 proto
= vlan
->h_vlan_encapsulated_proto
;
717 nhoff
+= sizeof(*vlan
);
719 fdret
= FLOW_DISSECT_RET_PROTO_AGAIN
;
725 if (dissector_uses_key(flow_dissector
,
726 FLOW_DISSECTOR_KEY_VLAN
)) {
727 key_vlan
= skb_flow_dissector_target(flow_dissector
,
728 FLOW_DISSECTOR_KEY_VLAN
,
731 if (vlan_tag_present
) {
732 key_vlan
->vlan_id
= skb_vlan_tag_get_id(skb
);
733 key_vlan
->vlan_priority
=
734 (skb_vlan_tag_get_prio(skb
) >> VLAN_PRIO_SHIFT
);
736 key_vlan
->vlan_id
= ntohs(vlan
->h_vlan_TCI
) &
738 key_vlan
->vlan_priority
=
739 (ntohs(vlan
->h_vlan_TCI
) &
740 VLAN_PRIO_MASK
) >> VLAN_PRIO_SHIFT
;
744 fdret
= FLOW_DISSECT_RET_PROTO_AGAIN
;
747 case htons(ETH_P_PPP_SES
): {
749 struct pppoe_hdr hdr
;
752 hdr
= __skb_header_pointer(skb
, nhoff
, sizeof(_hdr
), data
, hlen
, &_hdr
);
754 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
759 nhoff
+= PPPOE_SES_HLEN
;
762 proto
= htons(ETH_P_IP
);
763 fdret
= FLOW_DISSECT_RET_PROTO_AGAIN
;
765 case htons(PPP_IPV6
):
766 proto
= htons(ETH_P_IPV6
);
767 fdret
= FLOW_DISSECT_RET_PROTO_AGAIN
;
770 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
775 case htons(ETH_P_TIPC
): {
776 struct tipc_basic_hdr
*hdr
, _hdr
;
778 hdr
= __skb_header_pointer(skb
, nhoff
, sizeof(_hdr
),
781 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
785 if (dissector_uses_key(flow_dissector
,
786 FLOW_DISSECTOR_KEY_TIPC
)) {
787 key_addrs
= skb_flow_dissector_target(flow_dissector
,
788 FLOW_DISSECTOR_KEY_TIPC
,
790 key_addrs
->tipckey
.key
= tipc_hdr_rps_key(hdr
);
791 key_control
->addr_type
= FLOW_DISSECTOR_KEY_TIPC
;
793 fdret
= FLOW_DISSECT_RET_OUT_GOOD
;
797 case htons(ETH_P_MPLS_UC
):
798 case htons(ETH_P_MPLS_MC
):
799 fdret
= __skb_flow_dissect_mpls(skb
, flow_dissector
,
800 target_container
, data
,
803 case htons(ETH_P_FCOE
):
804 if ((hlen
- nhoff
) < FCOE_HEADER_LEN
) {
805 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
809 nhoff
+= FCOE_HEADER_LEN
;
810 fdret
= FLOW_DISSECT_RET_OUT_GOOD
;
813 case htons(ETH_P_ARP
):
814 case htons(ETH_P_RARP
):
815 fdret
= __skb_flow_dissect_arp(skb
, flow_dissector
,
816 target_container
, data
,
821 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
825 /* Process result of proto processing */
827 case FLOW_DISSECT_RET_OUT_GOOD
:
829 case FLOW_DISSECT_RET_PROTO_AGAIN
:
830 if (skb_flow_dissect_allowed(&num_hdrs
))
833 case FLOW_DISSECT_RET_CONTINUE
:
834 case FLOW_DISSECT_RET_IPPROTO_AGAIN
:
836 case FLOW_DISSECT_RET_OUT_BAD
:
842 fdret
= FLOW_DISSECT_RET_CONTINUE
;
846 fdret
= __skb_flow_dissect_gre(skb
, key_control
, flow_dissector
,
847 target_container
, data
,
848 &proto
, &nhoff
, &hlen
, flags
);
852 case NEXTHDR_ROUTING
:
854 u8 _opthdr
[2], *opthdr
;
856 if (proto
!= htons(ETH_P_IPV6
))
859 opthdr
= __skb_header_pointer(skb
, nhoff
, sizeof(_opthdr
),
860 data
, hlen
, &_opthdr
);
862 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
866 ip_proto
= opthdr
[0];
867 nhoff
+= (opthdr
[1] + 1) << 3;
869 fdret
= FLOW_DISSECT_RET_IPPROTO_AGAIN
;
872 case NEXTHDR_FRAGMENT
: {
873 struct frag_hdr _fh
, *fh
;
875 if (proto
!= htons(ETH_P_IPV6
))
878 fh
= __skb_header_pointer(skb
, nhoff
, sizeof(_fh
),
882 fdret
= FLOW_DISSECT_RET_OUT_BAD
;
886 key_control
->flags
|= FLOW_DIS_IS_FRAGMENT
;
888 nhoff
+= sizeof(_fh
);
889 ip_proto
= fh
->nexthdr
;
891 if (!(fh
->frag_off
& htons(IP6_OFFSET
))) {
892 key_control
->flags
|= FLOW_DIS_FIRST_FRAG
;
893 if (flags
& FLOW_DISSECTOR_F_PARSE_1ST_FRAG
) {
894 fdret
= FLOW_DISSECT_RET_IPPROTO_AGAIN
;
899 fdret
= FLOW_DISSECT_RET_OUT_GOOD
;
903 proto
= htons(ETH_P_IP
);
905 key_control
->flags
|= FLOW_DIS_ENCAPSULATION
;
906 if (flags
& FLOW_DISSECTOR_F_STOP_AT_ENCAP
) {
907 fdret
= FLOW_DISSECT_RET_OUT_GOOD
;
911 fdret
= FLOW_DISSECT_RET_PROTO_AGAIN
;
915 proto
= htons(ETH_P_IPV6
);
917 key_control
->flags
|= FLOW_DIS_ENCAPSULATION
;
918 if (flags
& FLOW_DISSECTOR_F_STOP_AT_ENCAP
) {
919 fdret
= FLOW_DISSECT_RET_OUT_GOOD
;
923 fdret
= FLOW_DISSECT_RET_PROTO_AGAIN
;
928 proto
= htons(ETH_P_MPLS_UC
);
929 fdret
= FLOW_DISSECT_RET_PROTO_AGAIN
;
933 __skb_flow_dissect_tcp(skb
, flow_dissector
, target_container
,
941 if (dissector_uses_key(flow_dissector
,
942 FLOW_DISSECTOR_KEY_PORTS
)) {
943 key_ports
= skb_flow_dissector_target(flow_dissector
,
944 FLOW_DISSECTOR_KEY_PORTS
,
946 key_ports
->ports
= __skb_flow_get_ports(skb
, nhoff
, ip_proto
,
950 if (dissector_uses_key(flow_dissector
,
951 FLOW_DISSECTOR_KEY_ICMP
)) {
952 key_icmp
= skb_flow_dissector_target(flow_dissector
,
953 FLOW_DISSECTOR_KEY_ICMP
,
955 key_icmp
->icmp
= skb_flow_get_be16(skb
, nhoff
, data
, hlen
);
958 /* Process result of IP proto processing */
960 case FLOW_DISSECT_RET_PROTO_AGAIN
:
961 if (skb_flow_dissect_allowed(&num_hdrs
))
964 case FLOW_DISSECT_RET_IPPROTO_AGAIN
:
965 if (skb_flow_dissect_allowed(&num_hdrs
))
968 case FLOW_DISSECT_RET_OUT_GOOD
:
969 case FLOW_DISSECT_RET_CONTINUE
:
971 case FLOW_DISSECT_RET_OUT_BAD
:
980 key_control
->thoff
= min_t(u16
, nhoff
, skb
? skb
->len
: hlen
);
981 key_basic
->n_proto
= proto
;
982 key_basic
->ip_proto
= ip_proto
;
990 EXPORT_SYMBOL(__skb_flow_dissect
);
992 static u32 hashrnd __read_mostly
;
993 static __always_inline
void __flow_hash_secret_init(void)
995 net_get_random_once(&hashrnd
, sizeof(hashrnd
));
998 static __always_inline u32
__flow_hash_words(const u32
*words
, u32 length
,
1001 return jhash2(words
, length
, keyval
);
1004 static inline const u32
*flow_keys_hash_start(const struct flow_keys
*flow
)
1006 const void *p
= flow
;
1008 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET
% sizeof(u32
));
1009 return (const u32
*)(p
+ FLOW_KEYS_HASH_OFFSET
);
1012 static inline size_t flow_keys_hash_length(const struct flow_keys
*flow
)
1014 size_t diff
= FLOW_KEYS_HASH_OFFSET
+ sizeof(flow
->addrs
);
1015 BUILD_BUG_ON((sizeof(*flow
) - FLOW_KEYS_HASH_OFFSET
) % sizeof(u32
));
1016 BUILD_BUG_ON(offsetof(typeof(*flow
), addrs
) !=
1017 sizeof(*flow
) - sizeof(flow
->addrs
));
1019 switch (flow
->control
.addr_type
) {
1020 case FLOW_DISSECTOR_KEY_IPV4_ADDRS
:
1021 diff
-= sizeof(flow
->addrs
.v4addrs
);
1023 case FLOW_DISSECTOR_KEY_IPV6_ADDRS
:
1024 diff
-= sizeof(flow
->addrs
.v6addrs
);
1026 case FLOW_DISSECTOR_KEY_TIPC
:
1027 diff
-= sizeof(flow
->addrs
.tipckey
);
1030 return (sizeof(*flow
) - diff
) / sizeof(u32
);
1033 __be32
flow_get_u32_src(const struct flow_keys
*flow
)
1035 switch (flow
->control
.addr_type
) {
1036 case FLOW_DISSECTOR_KEY_IPV4_ADDRS
:
1037 return flow
->addrs
.v4addrs
.src
;
1038 case FLOW_DISSECTOR_KEY_IPV6_ADDRS
:
1039 return (__force __be32
)ipv6_addr_hash(
1040 &flow
->addrs
.v6addrs
.src
);
1041 case FLOW_DISSECTOR_KEY_TIPC
:
1042 return flow
->addrs
.tipckey
.key
;
1047 EXPORT_SYMBOL(flow_get_u32_src
);
1049 __be32
flow_get_u32_dst(const struct flow_keys
*flow
)
1051 switch (flow
->control
.addr_type
) {
1052 case FLOW_DISSECTOR_KEY_IPV4_ADDRS
:
1053 return flow
->addrs
.v4addrs
.dst
;
1054 case FLOW_DISSECTOR_KEY_IPV6_ADDRS
:
1055 return (__force __be32
)ipv6_addr_hash(
1056 &flow
->addrs
.v6addrs
.dst
);
1061 EXPORT_SYMBOL(flow_get_u32_dst
);
1063 static inline void __flow_hash_consistentify(struct flow_keys
*keys
)
1067 switch (keys
->control
.addr_type
) {
1068 case FLOW_DISSECTOR_KEY_IPV4_ADDRS
:
1069 addr_diff
= (__force u32
)keys
->addrs
.v4addrs
.dst
-
1070 (__force u32
)keys
->addrs
.v4addrs
.src
;
1071 if ((addr_diff
< 0) ||
1073 ((__force u16
)keys
->ports
.dst
<
1074 (__force u16
)keys
->ports
.src
))) {
1075 swap(keys
->addrs
.v4addrs
.src
, keys
->addrs
.v4addrs
.dst
);
1076 swap(keys
->ports
.src
, keys
->ports
.dst
);
1079 case FLOW_DISSECTOR_KEY_IPV6_ADDRS
:
1080 addr_diff
= memcmp(&keys
->addrs
.v6addrs
.dst
,
1081 &keys
->addrs
.v6addrs
.src
,
1082 sizeof(keys
->addrs
.v6addrs
.dst
));
1083 if ((addr_diff
< 0) ||
1085 ((__force u16
)keys
->ports
.dst
<
1086 (__force u16
)keys
->ports
.src
))) {
1087 for (i
= 0; i
< 4; i
++)
1088 swap(keys
->addrs
.v6addrs
.src
.s6_addr32
[i
],
1089 keys
->addrs
.v6addrs
.dst
.s6_addr32
[i
]);
1090 swap(keys
->ports
.src
, keys
->ports
.dst
);
1096 static inline u32
__flow_hash_from_keys(struct flow_keys
*keys
, u32 keyval
)
1100 __flow_hash_consistentify(keys
);
1102 hash
= __flow_hash_words(flow_keys_hash_start(keys
),
1103 flow_keys_hash_length(keys
), keyval
);
1110 u32
flow_hash_from_keys(struct flow_keys
*keys
)
1112 __flow_hash_secret_init();
1113 return __flow_hash_from_keys(keys
, hashrnd
);
1115 EXPORT_SYMBOL(flow_hash_from_keys
);
1117 static inline u32
___skb_get_hash(const struct sk_buff
*skb
,
1118 struct flow_keys
*keys
, u32 keyval
)
1120 skb_flow_dissect_flow_keys(skb
, keys
,
1121 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
);
1123 return __flow_hash_from_keys(keys
, keyval
);
1126 struct _flow_keys_digest_data
{
1135 void make_flow_keys_digest(struct flow_keys_digest
*digest
,
1136 const struct flow_keys
*flow
)
1138 struct _flow_keys_digest_data
*data
=
1139 (struct _flow_keys_digest_data
*)digest
;
1141 BUILD_BUG_ON(sizeof(*data
) > sizeof(*digest
));
1143 memset(digest
, 0, sizeof(*digest
));
1145 data
->n_proto
= flow
->basic
.n_proto
;
1146 data
->ip_proto
= flow
->basic
.ip_proto
;
1147 data
->ports
= flow
->ports
.ports
;
1148 data
->src
= flow
->addrs
.v4addrs
.src
;
1149 data
->dst
= flow
->addrs
.v4addrs
.dst
;
1151 EXPORT_SYMBOL(make_flow_keys_digest
);
1153 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly
;
1155 u32
__skb_get_hash_symmetric(const struct sk_buff
*skb
)
1157 struct flow_keys keys
;
1159 __flow_hash_secret_init();
1161 memset(&keys
, 0, sizeof(keys
));
1162 __skb_flow_dissect(skb
, &flow_keys_dissector_symmetric
, &keys
,
1164 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
);
1166 return __flow_hash_from_keys(&keys
, hashrnd
);
1168 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric
);
1171 * __skb_get_hash: calculate a flow hash
1172 * @skb: sk_buff to calculate flow hash from
1174 * This function calculates a flow hash based on src/dst addresses
1175 * and src/dst port numbers. Sets hash in skb to non-zero hash value
1176 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
1177 * if hash is a canonical 4-tuple hash over transport ports.
1179 void __skb_get_hash(struct sk_buff
*skb
)
1181 struct flow_keys keys
;
1184 __flow_hash_secret_init();
1186 hash
= ___skb_get_hash(skb
, &keys
, hashrnd
);
1188 __skb_set_sw_hash(skb
, hash
, flow_keys_have_l4(&keys
));
1190 EXPORT_SYMBOL(__skb_get_hash
);
1192 __u32
skb_get_hash_perturb(const struct sk_buff
*skb
, u32 perturb
)
1194 struct flow_keys keys
;
1196 return ___skb_get_hash(skb
, &keys
, perturb
);
1198 EXPORT_SYMBOL(skb_get_hash_perturb
);
1200 u32
__skb_get_poff(const struct sk_buff
*skb
, void *data
,
1201 const struct flow_keys
*keys
, int hlen
)
1203 u32 poff
= keys
->control
.thoff
;
1205 /* skip L4 headers for fragments after the first */
1206 if ((keys
->control
.flags
& FLOW_DIS_IS_FRAGMENT
) &&
1207 !(keys
->control
.flags
& FLOW_DIS_FIRST_FRAG
))
1210 switch (keys
->basic
.ip_proto
) {
1212 /* access doff as u8 to avoid unaligned access */
1216 doff
= __skb_header_pointer(skb
, poff
+ 12, sizeof(_doff
),
1217 data
, hlen
, &_doff
);
1221 poff
+= max_t(u32
, sizeof(struct tcphdr
), (*doff
& 0xF0) >> 2);
1225 case IPPROTO_UDPLITE
:
1226 poff
+= sizeof(struct udphdr
);
1228 /* For the rest, we do not really care about header
1229 * extensions at this point for now.
1232 poff
+= sizeof(struct icmphdr
);
1234 case IPPROTO_ICMPV6
:
1235 poff
+= sizeof(struct icmp6hdr
);
1238 poff
+= sizeof(struct igmphdr
);
1241 poff
+= sizeof(struct dccp_hdr
);
1244 poff
+= sizeof(struct sctphdr
);
1252 * skb_get_poff - get the offset to the payload
1253 * @skb: sk_buff to get the payload offset from
1255 * The function will get the offset to the payload as far as it could
1256 * be dissected. The main user is currently BPF, so that we can dynamically
1257 * truncate packets without needing to push actual payload to the user
1258 * space and can analyze headers only, instead.
1260 u32
skb_get_poff(const struct sk_buff
*skb
)
1262 struct flow_keys keys
;
1264 if (!skb_flow_dissect_flow_keys(skb
, &keys
, 0))
1267 return __skb_get_poff(skb
, skb
->data
, &keys
, skb_headlen(skb
));
1270 __u32
__get_hash_from_flowi6(const struct flowi6
*fl6
, struct flow_keys
*keys
)
1272 memset(keys
, 0, sizeof(*keys
));
1274 memcpy(&keys
->addrs
.v6addrs
.src
, &fl6
->saddr
,
1275 sizeof(keys
->addrs
.v6addrs
.src
));
1276 memcpy(&keys
->addrs
.v6addrs
.dst
, &fl6
->daddr
,
1277 sizeof(keys
->addrs
.v6addrs
.dst
));
1278 keys
->control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
1279 keys
->ports
.src
= fl6
->fl6_sport
;
1280 keys
->ports
.dst
= fl6
->fl6_dport
;
1281 keys
->keyid
.keyid
= fl6
->fl6_gre_key
;
1282 keys
->tags
.flow_label
= (__force u32
)fl6
->flowlabel
;
1283 keys
->basic
.ip_proto
= fl6
->flowi6_proto
;
1285 return flow_hash_from_keys(keys
);
1287 EXPORT_SYMBOL(__get_hash_from_flowi6
);
1289 __u32
__get_hash_from_flowi4(const struct flowi4
*fl4
, struct flow_keys
*keys
)
1291 memset(keys
, 0, sizeof(*keys
));
1293 keys
->addrs
.v4addrs
.src
= fl4
->saddr
;
1294 keys
->addrs
.v4addrs
.dst
= fl4
->daddr
;
1295 keys
->control
.addr_type
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
;
1296 keys
->ports
.src
= fl4
->fl4_sport
;
1297 keys
->ports
.dst
= fl4
->fl4_dport
;
1298 keys
->keyid
.keyid
= fl4
->fl4_gre_key
;
1299 keys
->basic
.ip_proto
= fl4
->flowi4_proto
;
1301 return flow_hash_from_keys(keys
);
1303 EXPORT_SYMBOL(__get_hash_from_flowi4
);
1305 static const struct flow_dissector_key flow_keys_dissector_keys
[] = {
1307 .key_id
= FLOW_DISSECTOR_KEY_CONTROL
,
1308 .offset
= offsetof(struct flow_keys
, control
),
1311 .key_id
= FLOW_DISSECTOR_KEY_BASIC
,
1312 .offset
= offsetof(struct flow_keys
, basic
),
1315 .key_id
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
,
1316 .offset
= offsetof(struct flow_keys
, addrs
.v4addrs
),
1319 .key_id
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
,
1320 .offset
= offsetof(struct flow_keys
, addrs
.v6addrs
),
1323 .key_id
= FLOW_DISSECTOR_KEY_TIPC
,
1324 .offset
= offsetof(struct flow_keys
, addrs
.tipckey
),
1327 .key_id
= FLOW_DISSECTOR_KEY_PORTS
,
1328 .offset
= offsetof(struct flow_keys
, ports
),
1331 .key_id
= FLOW_DISSECTOR_KEY_VLAN
,
1332 .offset
= offsetof(struct flow_keys
, vlan
),
1335 .key_id
= FLOW_DISSECTOR_KEY_FLOW_LABEL
,
1336 .offset
= offsetof(struct flow_keys
, tags
),
1339 .key_id
= FLOW_DISSECTOR_KEY_GRE_KEYID
,
1340 .offset
= offsetof(struct flow_keys
, keyid
),
1344 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys
[] = {
1346 .key_id
= FLOW_DISSECTOR_KEY_CONTROL
,
1347 .offset
= offsetof(struct flow_keys
, control
),
1350 .key_id
= FLOW_DISSECTOR_KEY_BASIC
,
1351 .offset
= offsetof(struct flow_keys
, basic
),
1354 .key_id
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
,
1355 .offset
= offsetof(struct flow_keys
, addrs
.v4addrs
),
1358 .key_id
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
,
1359 .offset
= offsetof(struct flow_keys
, addrs
.v6addrs
),
1362 .key_id
= FLOW_DISSECTOR_KEY_PORTS
,
1363 .offset
= offsetof(struct flow_keys
, ports
),
1367 static const struct flow_dissector_key flow_keys_buf_dissector_keys
[] = {
1369 .key_id
= FLOW_DISSECTOR_KEY_CONTROL
,
1370 .offset
= offsetof(struct flow_keys
, control
),
1373 .key_id
= FLOW_DISSECTOR_KEY_BASIC
,
1374 .offset
= offsetof(struct flow_keys
, basic
),
1378 struct flow_dissector flow_keys_dissector __read_mostly
;
1379 EXPORT_SYMBOL(flow_keys_dissector
);
1381 struct flow_dissector flow_keys_buf_dissector __read_mostly
;
1383 static int __init
init_default_flow_dissectors(void)
1385 skb_flow_dissector_init(&flow_keys_dissector
,
1386 flow_keys_dissector_keys
,
1387 ARRAY_SIZE(flow_keys_dissector_keys
));
1388 skb_flow_dissector_init(&flow_keys_dissector_symmetric
,
1389 flow_keys_dissector_symmetric_keys
,
1390 ARRAY_SIZE(flow_keys_dissector_symmetric_keys
));
1391 skb_flow_dissector_init(&flow_keys_buf_dissector
,
1392 flow_keys_buf_dissector_keys
,
1393 ARRAY_SIZE(flow_keys_buf_dissector_keys
));
1397 core_initcall(init_default_flow_dissectors
);