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>
11 #include <linux/igmp.h>
12 #include <linux/icmp.h>
13 #include <linux/sctp.h>
14 #include <linux/dccp.h>
15 #include <linux/if_tunnel.h>
16 #include <linux/if_pppox.h>
17 #include <linux/ppp_defs.h>
18 #include <linux/stddef.h>
19 #include <linux/if_ether.h>
20 #include <linux/mpls.h>
21 #include <net/flow_dissector.h>
22 #include <scsi/fc/fc_fcoe.h>
24 static void dissector_set_key(struct flow_dissector
*flow_dissector
,
25 enum flow_dissector_key_id key_id
)
27 flow_dissector
->used_keys
|= (1 << key_id
);
30 void skb_flow_dissector_init(struct flow_dissector
*flow_dissector
,
31 const struct flow_dissector_key
*key
,
32 unsigned int key_count
)
36 memset(flow_dissector
, 0, sizeof(*flow_dissector
));
38 for (i
= 0; i
< key_count
; i
++, key
++) {
39 /* User should make sure that every key target offset is withing
40 * boundaries of unsigned short.
42 BUG_ON(key
->offset
> USHRT_MAX
);
43 BUG_ON(dissector_uses_key(flow_dissector
,
46 dissector_set_key(flow_dissector
, key
->key_id
);
47 flow_dissector
->offset
[key
->key_id
] = key
->offset
;
50 /* Ensure that the dissector always includes control and basic key.
51 * That way we are able to avoid handling lack of these in fast path.
53 BUG_ON(!dissector_uses_key(flow_dissector
,
54 FLOW_DISSECTOR_KEY_CONTROL
));
55 BUG_ON(!dissector_uses_key(flow_dissector
,
56 FLOW_DISSECTOR_KEY_BASIC
));
58 EXPORT_SYMBOL(skb_flow_dissector_init
);
61 * skb_flow_get_be16 - extract be16 entity
62 * @skb: sk_buff to extract from
63 * @poff: offset to extract at
64 * @data: raw buffer pointer to the packet
65 * @hlen: packet header length
67 * The function will try to retrieve a be32 entity at
70 static __be16
skb_flow_get_be16(const struct sk_buff
*skb
, int poff
,
75 u
= __skb_header_pointer(skb
, poff
, sizeof(_u
), data
, hlen
, &_u
);
83 * __skb_flow_get_ports - extract the upper layer ports and return them
84 * @skb: sk_buff to extract the ports from
85 * @thoff: transport header offset
86 * @ip_proto: protocol for which to get port offset
87 * @data: raw buffer pointer to the packet, if NULL use skb->data
88 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
90 * The function will try to retrieve the ports at offset thoff + poff where poff
91 * is the protocol port offset returned from proto_ports_offset
93 __be32
__skb_flow_get_ports(const struct sk_buff
*skb
, int thoff
, u8 ip_proto
,
96 int poff
= proto_ports_offset(ip_proto
);
100 hlen
= skb_headlen(skb
);
104 __be32
*ports
, _ports
;
106 ports
= __skb_header_pointer(skb
, thoff
+ poff
,
107 sizeof(_ports
), data
, hlen
, &_ports
);
114 EXPORT_SYMBOL(__skb_flow_get_ports
);
117 * __skb_flow_dissect - extract the flow_keys struct and return it
118 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
119 * @flow_dissector: list of keys to dissect
120 * @target_container: target structure to put dissected values into
121 * @data: raw buffer pointer to the packet, if NULL use skb->data
122 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
123 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
124 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
126 * The function will try to retrieve individual keys into target specified
127 * by flow_dissector from either the skbuff or a raw buffer specified by the
130 * Caller must take care of zeroing target container memory.
132 bool __skb_flow_dissect(const struct sk_buff
*skb
,
133 struct flow_dissector
*flow_dissector
,
134 void *target_container
,
135 void *data
, __be16 proto
, int nhoff
, int hlen
,
138 struct flow_dissector_key_control
*key_control
;
139 struct flow_dissector_key_basic
*key_basic
;
140 struct flow_dissector_key_addrs
*key_addrs
;
141 struct flow_dissector_key_arp
*key_arp
;
142 struct flow_dissector_key_ports
*key_ports
;
143 struct flow_dissector_key_icmp
*key_icmp
;
144 struct flow_dissector_key_tags
*key_tags
;
145 struct flow_dissector_key_vlan
*key_vlan
;
146 struct flow_dissector_key_keyid
*key_keyid
;
147 bool skip_vlan
= false;
153 proto
= skb_vlan_tag_present(skb
) ?
154 skb
->vlan_proto
: skb
->protocol
;
155 nhoff
= skb_network_offset(skb
);
156 hlen
= skb_headlen(skb
);
159 /* It is ensured by skb_flow_dissector_init() that control key will
162 key_control
= skb_flow_dissector_target(flow_dissector
,
163 FLOW_DISSECTOR_KEY_CONTROL
,
166 /* It is ensured by skb_flow_dissector_init() that basic key will
169 key_basic
= skb_flow_dissector_target(flow_dissector
,
170 FLOW_DISSECTOR_KEY_BASIC
,
173 if (dissector_uses_key(flow_dissector
,
174 FLOW_DISSECTOR_KEY_ETH_ADDRS
)) {
175 struct ethhdr
*eth
= eth_hdr(skb
);
176 struct flow_dissector_key_eth_addrs
*key_eth_addrs
;
178 key_eth_addrs
= skb_flow_dissector_target(flow_dissector
,
179 FLOW_DISSECTOR_KEY_ETH_ADDRS
,
181 memcpy(key_eth_addrs
, ð
->h_dest
, sizeof(*key_eth_addrs
));
186 case htons(ETH_P_IP
): {
187 const struct iphdr
*iph
;
190 iph
= __skb_header_pointer(skb
, nhoff
, sizeof(_iph
), data
, hlen
, &_iph
);
191 if (!iph
|| iph
->ihl
< 5)
193 nhoff
+= iph
->ihl
* 4;
195 ip_proto
= iph
->protocol
;
197 if (dissector_uses_key(flow_dissector
,
198 FLOW_DISSECTOR_KEY_IPV4_ADDRS
)) {
199 key_addrs
= skb_flow_dissector_target(flow_dissector
,
200 FLOW_DISSECTOR_KEY_IPV4_ADDRS
,
203 memcpy(&key_addrs
->v4addrs
, &iph
->saddr
,
204 sizeof(key_addrs
->v4addrs
));
205 key_control
->addr_type
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
;
208 if (ip_is_fragment(iph
)) {
209 key_control
->flags
|= FLOW_DIS_IS_FRAGMENT
;
211 if (iph
->frag_off
& htons(IP_OFFSET
)) {
214 key_control
->flags
|= FLOW_DIS_FIRST_FRAG
;
215 if (!(flags
& FLOW_DISSECTOR_F_PARSE_1ST_FRAG
))
220 if (flags
& FLOW_DISSECTOR_F_STOP_AT_L3
)
225 case htons(ETH_P_IPV6
): {
226 const struct ipv6hdr
*iph
;
230 iph
= __skb_header_pointer(skb
, nhoff
, sizeof(_iph
), data
, hlen
, &_iph
);
234 ip_proto
= iph
->nexthdr
;
235 nhoff
+= sizeof(struct ipv6hdr
);
237 if (dissector_uses_key(flow_dissector
,
238 FLOW_DISSECTOR_KEY_IPV6_ADDRS
)) {
239 key_addrs
= skb_flow_dissector_target(flow_dissector
,
240 FLOW_DISSECTOR_KEY_IPV6_ADDRS
,
243 memcpy(&key_addrs
->v6addrs
, &iph
->saddr
,
244 sizeof(key_addrs
->v6addrs
));
245 key_control
->addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
248 if ((dissector_uses_key(flow_dissector
,
249 FLOW_DISSECTOR_KEY_FLOW_LABEL
) ||
250 (flags
& FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
)) &&
251 ip6_flowlabel(iph
)) {
252 __be32 flow_label
= ip6_flowlabel(iph
);
254 if (dissector_uses_key(flow_dissector
,
255 FLOW_DISSECTOR_KEY_FLOW_LABEL
)) {
256 key_tags
= skb_flow_dissector_target(flow_dissector
,
257 FLOW_DISSECTOR_KEY_FLOW_LABEL
,
259 key_tags
->flow_label
= ntohl(flow_label
);
261 if (flags
& FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
)
265 if (flags
& FLOW_DISSECTOR_F_STOP_AT_L3
)
270 case htons(ETH_P_8021AD
):
271 case htons(ETH_P_8021Q
): {
272 const struct vlan_hdr
*vlan
;
273 struct vlan_hdr _vlan
;
274 bool vlan_tag_present
= skb
&& skb_vlan_tag_present(skb
);
276 if (vlan_tag_present
)
277 proto
= skb
->protocol
;
279 if (!vlan_tag_present
|| eth_type_vlan(skb
->protocol
)) {
280 vlan
= __skb_header_pointer(skb
, nhoff
, sizeof(_vlan
),
284 proto
= vlan
->h_vlan_encapsulated_proto
;
285 nhoff
+= sizeof(*vlan
);
291 if (dissector_uses_key(flow_dissector
,
292 FLOW_DISSECTOR_KEY_VLAN
)) {
293 key_vlan
= skb_flow_dissector_target(flow_dissector
,
294 FLOW_DISSECTOR_KEY_VLAN
,
297 if (vlan_tag_present
) {
298 key_vlan
->vlan_id
= skb_vlan_tag_get_id(skb
);
299 key_vlan
->vlan_priority
=
300 (skb_vlan_tag_get_prio(skb
) >> VLAN_PRIO_SHIFT
);
302 key_vlan
->vlan_id
= ntohs(vlan
->h_vlan_TCI
) &
304 key_vlan
->vlan_priority
=
305 (ntohs(vlan
->h_vlan_TCI
) &
306 VLAN_PRIO_MASK
) >> VLAN_PRIO_SHIFT
;
312 case htons(ETH_P_PPP_SES
): {
314 struct pppoe_hdr hdr
;
317 hdr
= __skb_header_pointer(skb
, nhoff
, sizeof(_hdr
), data
, hlen
, &_hdr
);
321 nhoff
+= PPPOE_SES_HLEN
;
325 case htons(PPP_IPV6
):
331 case htons(ETH_P_TIPC
): {
336 hdr
= __skb_header_pointer(skb
, nhoff
, sizeof(_hdr
), data
, hlen
, &_hdr
);
340 if (dissector_uses_key(flow_dissector
,
341 FLOW_DISSECTOR_KEY_TIPC_ADDRS
)) {
342 key_addrs
= skb_flow_dissector_target(flow_dissector
,
343 FLOW_DISSECTOR_KEY_TIPC_ADDRS
,
345 key_addrs
->tipcaddrs
.srcnode
= hdr
->srcnode
;
346 key_control
->addr_type
= FLOW_DISSECTOR_KEY_TIPC_ADDRS
;
351 case htons(ETH_P_MPLS_UC
):
352 case htons(ETH_P_MPLS_MC
): {
353 struct mpls_label
*hdr
, _hdr
[2];
355 hdr
= __skb_header_pointer(skb
, nhoff
, sizeof(_hdr
), data
,
360 if ((ntohl(hdr
[0].entry
) & MPLS_LS_LABEL_MASK
) >>
361 MPLS_LS_LABEL_SHIFT
== MPLS_LABEL_ENTROPY
) {
362 if (dissector_uses_key(flow_dissector
,
363 FLOW_DISSECTOR_KEY_MPLS_ENTROPY
)) {
364 key_keyid
= skb_flow_dissector_target(flow_dissector
,
365 FLOW_DISSECTOR_KEY_MPLS_ENTROPY
,
367 key_keyid
->keyid
= hdr
[1].entry
&
368 htonl(MPLS_LS_LABEL_MASK
);
377 case htons(ETH_P_FCOE
):
378 if ((hlen
- nhoff
) < FCOE_HEADER_LEN
)
381 nhoff
+= FCOE_HEADER_LEN
;
384 case htons(ETH_P_ARP
):
385 case htons(ETH_P_RARP
): {
387 unsigned char ar_sha
[ETH_ALEN
];
388 unsigned char ar_sip
[4];
389 unsigned char ar_tha
[ETH_ALEN
];
390 unsigned char ar_tip
[4];
391 } *arp_eth
, _arp_eth
;
392 const struct arphdr
*arp
;
395 arp
= __skb_header_pointer(skb
, nhoff
, sizeof(_arp
), data
,
400 if (arp
->ar_hrd
!= htons(ARPHRD_ETHER
) ||
401 arp
->ar_pro
!= htons(ETH_P_IP
) ||
402 arp
->ar_hln
!= ETH_ALEN
||
404 (arp
->ar_op
!= htons(ARPOP_REPLY
) &&
405 arp
->ar_op
!= htons(ARPOP_REQUEST
)))
408 arp_eth
= __skb_header_pointer(skb
, nhoff
+ sizeof(_arp
),
409 sizeof(_arp_eth
), data
,
415 if (dissector_uses_key(flow_dissector
,
416 FLOW_DISSECTOR_KEY_ARP
)) {
418 key_arp
= skb_flow_dissector_target(flow_dissector
,
419 FLOW_DISSECTOR_KEY_ARP
,
422 memcpy(&key_arp
->sip
, arp_eth
->ar_sip
,
423 sizeof(key_arp
->sip
));
424 memcpy(&key_arp
->tip
, arp_eth
->ar_tip
,
425 sizeof(key_arp
->tip
));
427 /* Only store the lower byte of the opcode;
428 * this covers ARPOP_REPLY and ARPOP_REQUEST.
430 key_arp
->op
= ntohs(arp
->ar_op
) & 0xff;
432 ether_addr_copy(key_arp
->sha
, arp_eth
->ar_sha
);
433 ether_addr_copy(key_arp
->tha
, arp_eth
->ar_tha
);
446 struct gre_base_hdr
*hdr
, _hdr
;
450 hdr
= __skb_header_pointer(skb
, nhoff
, sizeof(_hdr
), data
, hlen
, &_hdr
);
454 /* Only look inside GRE without routing */
455 if (hdr
->flags
& GRE_ROUTING
)
458 /* Only look inside GRE for version 0 and 1 */
459 gre_ver
= ntohs(hdr
->flags
& GRE_VERSION
);
463 proto
= hdr
->protocol
;
465 /* Version1 must be PPTP, and check the flags */
466 if (!(proto
== GRE_PROTO_PPP
&& (hdr
->flags
& GRE_KEY
)))
470 offset
+= sizeof(struct gre_base_hdr
);
472 if (hdr
->flags
& GRE_CSUM
)
473 offset
+= sizeof(((struct gre_full_hdr
*)0)->csum
) +
474 sizeof(((struct gre_full_hdr
*)0)->reserved1
);
476 if (hdr
->flags
& GRE_KEY
) {
480 keyid
= __skb_header_pointer(skb
, nhoff
+ offset
, sizeof(_keyid
),
481 data
, hlen
, &_keyid
);
485 if (dissector_uses_key(flow_dissector
,
486 FLOW_DISSECTOR_KEY_GRE_KEYID
)) {
487 key_keyid
= skb_flow_dissector_target(flow_dissector
,
488 FLOW_DISSECTOR_KEY_GRE_KEYID
,
491 key_keyid
->keyid
= *keyid
;
493 key_keyid
->keyid
= *keyid
& GRE_PPTP_KEY_MASK
;
495 offset
+= sizeof(((struct gre_full_hdr
*)0)->key
);
498 if (hdr
->flags
& GRE_SEQ
)
499 offset
+= sizeof(((struct pptp_gre_header
*)0)->seq
);
502 if (proto
== htons(ETH_P_TEB
)) {
503 const struct ethhdr
*eth
;
506 eth
= __skb_header_pointer(skb
, nhoff
+ offset
,
511 proto
= eth
->h_proto
;
512 offset
+= sizeof(*eth
);
514 /* Cap headers that we access via pointers at the
515 * end of the Ethernet header as our maximum alignment
516 * at that point is only 2 bytes.
519 hlen
= (nhoff
+ offset
);
521 } else { /* version 1, must be PPTP */
522 u8 _ppp_hdr
[PPP_HDRLEN
];
525 if (hdr
->flags
& GRE_ACK
)
526 offset
+= sizeof(((struct pptp_gre_header
*)0)->ack
);
528 ppp_hdr
= __skb_header_pointer(skb
, nhoff
+ offset
,
530 data
, hlen
, _ppp_hdr
);
534 switch (PPP_PROTOCOL(ppp_hdr
)) {
536 proto
= htons(ETH_P_IP
);
539 proto
= htons(ETH_P_IPV6
);
542 /* Could probably catch some more like MPLS */
546 offset
+= PPP_HDRLEN
;
550 key_control
->flags
|= FLOW_DIS_ENCAPSULATION
;
551 if (flags
& FLOW_DISSECTOR_F_STOP_AT_ENCAP
)
557 case NEXTHDR_ROUTING
:
559 u8 _opthdr
[2], *opthdr
;
561 if (proto
!= htons(ETH_P_IPV6
))
564 opthdr
= __skb_header_pointer(skb
, nhoff
, sizeof(_opthdr
),
565 data
, hlen
, &_opthdr
);
569 ip_proto
= opthdr
[0];
570 nhoff
+= (opthdr
[1] + 1) << 3;
574 case NEXTHDR_FRAGMENT
: {
575 struct frag_hdr _fh
, *fh
;
577 if (proto
!= htons(ETH_P_IPV6
))
580 fh
= __skb_header_pointer(skb
, nhoff
, sizeof(_fh
),
586 key_control
->flags
|= FLOW_DIS_IS_FRAGMENT
;
588 nhoff
+= sizeof(_fh
);
589 ip_proto
= fh
->nexthdr
;
591 if (!(fh
->frag_off
& htons(IP6_OFFSET
))) {
592 key_control
->flags
|= FLOW_DIS_FIRST_FRAG
;
593 if (flags
& FLOW_DISSECTOR_F_PARSE_1ST_FRAG
)
599 proto
= htons(ETH_P_IP
);
601 key_control
->flags
|= FLOW_DIS_ENCAPSULATION
;
602 if (flags
& FLOW_DISSECTOR_F_STOP_AT_ENCAP
)
607 proto
= htons(ETH_P_IPV6
);
609 key_control
->flags
|= FLOW_DIS_ENCAPSULATION
;
610 if (flags
& FLOW_DISSECTOR_F_STOP_AT_ENCAP
)
615 proto
= htons(ETH_P_MPLS_UC
);
621 if (dissector_uses_key(flow_dissector
,
622 FLOW_DISSECTOR_KEY_PORTS
)) {
623 key_ports
= skb_flow_dissector_target(flow_dissector
,
624 FLOW_DISSECTOR_KEY_PORTS
,
626 key_ports
->ports
= __skb_flow_get_ports(skb
, nhoff
, ip_proto
,
630 if (dissector_uses_key(flow_dissector
,
631 FLOW_DISSECTOR_KEY_ICMP
)) {
632 key_icmp
= skb_flow_dissector_target(flow_dissector
,
633 FLOW_DISSECTOR_KEY_ICMP
,
635 key_icmp
->icmp
= skb_flow_get_be16(skb
, nhoff
, data
, hlen
);
641 key_control
->thoff
= (u16
)nhoff
;
643 key_basic
->n_proto
= proto
;
644 key_basic
->ip_proto
= ip_proto
;
650 key_control
->thoff
= min_t(u16
, nhoff
, skb
? skb
->len
: hlen
);
653 EXPORT_SYMBOL(__skb_flow_dissect
);
655 static u32 hashrnd __read_mostly
;
656 static __always_inline
void __flow_hash_secret_init(void)
658 net_get_random_once(&hashrnd
, sizeof(hashrnd
));
661 static __always_inline u32
__flow_hash_words(const u32
*words
, u32 length
,
664 return jhash2(words
, length
, keyval
);
667 static inline const u32
*flow_keys_hash_start(const struct flow_keys
*flow
)
669 const void *p
= flow
;
671 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET
% sizeof(u32
));
672 return (const u32
*)(p
+ FLOW_KEYS_HASH_OFFSET
);
675 static inline size_t flow_keys_hash_length(const struct flow_keys
*flow
)
677 size_t diff
= FLOW_KEYS_HASH_OFFSET
+ sizeof(flow
->addrs
);
678 BUILD_BUG_ON((sizeof(*flow
) - FLOW_KEYS_HASH_OFFSET
) % sizeof(u32
));
679 BUILD_BUG_ON(offsetof(typeof(*flow
), addrs
) !=
680 sizeof(*flow
) - sizeof(flow
->addrs
));
682 switch (flow
->control
.addr_type
) {
683 case FLOW_DISSECTOR_KEY_IPV4_ADDRS
:
684 diff
-= sizeof(flow
->addrs
.v4addrs
);
686 case FLOW_DISSECTOR_KEY_IPV6_ADDRS
:
687 diff
-= sizeof(flow
->addrs
.v6addrs
);
689 case FLOW_DISSECTOR_KEY_TIPC_ADDRS
:
690 diff
-= sizeof(flow
->addrs
.tipcaddrs
);
693 return (sizeof(*flow
) - diff
) / sizeof(u32
);
696 __be32
flow_get_u32_src(const struct flow_keys
*flow
)
698 switch (flow
->control
.addr_type
) {
699 case FLOW_DISSECTOR_KEY_IPV4_ADDRS
:
700 return flow
->addrs
.v4addrs
.src
;
701 case FLOW_DISSECTOR_KEY_IPV6_ADDRS
:
702 return (__force __be32
)ipv6_addr_hash(
703 &flow
->addrs
.v6addrs
.src
);
704 case FLOW_DISSECTOR_KEY_TIPC_ADDRS
:
705 return flow
->addrs
.tipcaddrs
.srcnode
;
710 EXPORT_SYMBOL(flow_get_u32_src
);
712 __be32
flow_get_u32_dst(const struct flow_keys
*flow
)
714 switch (flow
->control
.addr_type
) {
715 case FLOW_DISSECTOR_KEY_IPV4_ADDRS
:
716 return flow
->addrs
.v4addrs
.dst
;
717 case FLOW_DISSECTOR_KEY_IPV6_ADDRS
:
718 return (__force __be32
)ipv6_addr_hash(
719 &flow
->addrs
.v6addrs
.dst
);
724 EXPORT_SYMBOL(flow_get_u32_dst
);
726 static inline void __flow_hash_consistentify(struct flow_keys
*keys
)
730 switch (keys
->control
.addr_type
) {
731 case FLOW_DISSECTOR_KEY_IPV4_ADDRS
:
732 addr_diff
= (__force u32
)keys
->addrs
.v4addrs
.dst
-
733 (__force u32
)keys
->addrs
.v4addrs
.src
;
734 if ((addr_diff
< 0) ||
736 ((__force u16
)keys
->ports
.dst
<
737 (__force u16
)keys
->ports
.src
))) {
738 swap(keys
->addrs
.v4addrs
.src
, keys
->addrs
.v4addrs
.dst
);
739 swap(keys
->ports
.src
, keys
->ports
.dst
);
742 case FLOW_DISSECTOR_KEY_IPV6_ADDRS
:
743 addr_diff
= memcmp(&keys
->addrs
.v6addrs
.dst
,
744 &keys
->addrs
.v6addrs
.src
,
745 sizeof(keys
->addrs
.v6addrs
.dst
));
746 if ((addr_diff
< 0) ||
748 ((__force u16
)keys
->ports
.dst
<
749 (__force u16
)keys
->ports
.src
))) {
750 for (i
= 0; i
< 4; i
++)
751 swap(keys
->addrs
.v6addrs
.src
.s6_addr32
[i
],
752 keys
->addrs
.v6addrs
.dst
.s6_addr32
[i
]);
753 swap(keys
->ports
.src
, keys
->ports
.dst
);
759 static inline u32
__flow_hash_from_keys(struct flow_keys
*keys
, u32 keyval
)
763 __flow_hash_consistentify(keys
);
765 hash
= __flow_hash_words(flow_keys_hash_start(keys
),
766 flow_keys_hash_length(keys
), keyval
);
773 u32
flow_hash_from_keys(struct flow_keys
*keys
)
775 __flow_hash_secret_init();
776 return __flow_hash_from_keys(keys
, hashrnd
);
778 EXPORT_SYMBOL(flow_hash_from_keys
);
780 static inline u32
___skb_get_hash(const struct sk_buff
*skb
,
781 struct flow_keys
*keys
, u32 keyval
)
783 skb_flow_dissect_flow_keys(skb
, keys
,
784 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
);
786 return __flow_hash_from_keys(keys
, keyval
);
789 struct _flow_keys_digest_data
{
798 void make_flow_keys_digest(struct flow_keys_digest
*digest
,
799 const struct flow_keys
*flow
)
801 struct _flow_keys_digest_data
*data
=
802 (struct _flow_keys_digest_data
*)digest
;
804 BUILD_BUG_ON(sizeof(*data
) > sizeof(*digest
));
806 memset(digest
, 0, sizeof(*digest
));
808 data
->n_proto
= flow
->basic
.n_proto
;
809 data
->ip_proto
= flow
->basic
.ip_proto
;
810 data
->ports
= flow
->ports
.ports
;
811 data
->src
= flow
->addrs
.v4addrs
.src
;
812 data
->dst
= flow
->addrs
.v4addrs
.dst
;
814 EXPORT_SYMBOL(make_flow_keys_digest
);
816 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly
;
818 u32
__skb_get_hash_symmetric(const struct sk_buff
*skb
)
820 struct flow_keys keys
;
822 __flow_hash_secret_init();
824 memset(&keys
, 0, sizeof(keys
));
825 __skb_flow_dissect(skb
, &flow_keys_dissector_symmetric
, &keys
,
827 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
);
829 return __flow_hash_from_keys(&keys
, hashrnd
);
831 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric
);
834 * __skb_get_hash: calculate a flow hash
835 * @skb: sk_buff to calculate flow hash from
837 * This function calculates a flow hash based on src/dst addresses
838 * and src/dst port numbers. Sets hash in skb to non-zero hash value
839 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
840 * if hash is a canonical 4-tuple hash over transport ports.
842 void __skb_get_hash(struct sk_buff
*skb
)
844 struct flow_keys keys
;
847 __flow_hash_secret_init();
849 hash
= ___skb_get_hash(skb
, &keys
, hashrnd
);
851 __skb_set_sw_hash(skb
, hash
, flow_keys_have_l4(&keys
));
853 EXPORT_SYMBOL(__skb_get_hash
);
855 __u32
skb_get_hash_perturb(const struct sk_buff
*skb
, u32 perturb
)
857 struct flow_keys keys
;
859 return ___skb_get_hash(skb
, &keys
, perturb
);
861 EXPORT_SYMBOL(skb_get_hash_perturb
);
863 __u32
__skb_get_hash_flowi6(struct sk_buff
*skb
, const struct flowi6
*fl6
)
865 struct flow_keys keys
;
867 memset(&keys
, 0, sizeof(keys
));
869 memcpy(&keys
.addrs
.v6addrs
.src
, &fl6
->saddr
,
870 sizeof(keys
.addrs
.v6addrs
.src
));
871 memcpy(&keys
.addrs
.v6addrs
.dst
, &fl6
->daddr
,
872 sizeof(keys
.addrs
.v6addrs
.dst
));
873 keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
874 keys
.ports
.src
= fl6
->fl6_sport
;
875 keys
.ports
.dst
= fl6
->fl6_dport
;
876 keys
.keyid
.keyid
= fl6
->fl6_gre_key
;
877 keys
.tags
.flow_label
= (__force u32
)fl6
->flowlabel
;
878 keys
.basic
.ip_proto
= fl6
->flowi6_proto
;
880 __skb_set_sw_hash(skb
, flow_hash_from_keys(&keys
),
881 flow_keys_have_l4(&keys
));
885 EXPORT_SYMBOL(__skb_get_hash_flowi6
);
887 __u32
__skb_get_hash_flowi4(struct sk_buff
*skb
, const struct flowi4
*fl4
)
889 struct flow_keys keys
;
891 memset(&keys
, 0, sizeof(keys
));
893 keys
.addrs
.v4addrs
.src
= fl4
->saddr
;
894 keys
.addrs
.v4addrs
.dst
= fl4
->daddr
;
895 keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
;
896 keys
.ports
.src
= fl4
->fl4_sport
;
897 keys
.ports
.dst
= fl4
->fl4_dport
;
898 keys
.keyid
.keyid
= fl4
->fl4_gre_key
;
899 keys
.basic
.ip_proto
= fl4
->flowi4_proto
;
901 __skb_set_sw_hash(skb
, flow_hash_from_keys(&keys
),
902 flow_keys_have_l4(&keys
));
906 EXPORT_SYMBOL(__skb_get_hash_flowi4
);
908 u32
__skb_get_poff(const struct sk_buff
*skb
, void *data
,
909 const struct flow_keys
*keys
, int hlen
)
911 u32 poff
= keys
->control
.thoff
;
913 /* skip L4 headers for fragments after the first */
914 if ((keys
->control
.flags
& FLOW_DIS_IS_FRAGMENT
) &&
915 !(keys
->control
.flags
& FLOW_DIS_FIRST_FRAG
))
918 switch (keys
->basic
.ip_proto
) {
920 /* access doff as u8 to avoid unaligned access */
924 doff
= __skb_header_pointer(skb
, poff
+ 12, sizeof(_doff
),
929 poff
+= max_t(u32
, sizeof(struct tcphdr
), (*doff
& 0xF0) >> 2);
933 case IPPROTO_UDPLITE
:
934 poff
+= sizeof(struct udphdr
);
936 /* For the rest, we do not really care about header
937 * extensions at this point for now.
940 poff
+= sizeof(struct icmphdr
);
943 poff
+= sizeof(struct icmp6hdr
);
946 poff
+= sizeof(struct igmphdr
);
949 poff
+= sizeof(struct dccp_hdr
);
952 poff
+= sizeof(struct sctphdr
);
960 * skb_get_poff - get the offset to the payload
961 * @skb: sk_buff to get the payload offset from
963 * The function will get the offset to the payload as far as it could
964 * be dissected. The main user is currently BPF, so that we can dynamically
965 * truncate packets without needing to push actual payload to the user
966 * space and can analyze headers only, instead.
968 u32
skb_get_poff(const struct sk_buff
*skb
)
970 struct flow_keys keys
;
972 if (!skb_flow_dissect_flow_keys(skb
, &keys
, 0))
975 return __skb_get_poff(skb
, skb
->data
, &keys
, skb_headlen(skb
));
978 __u32
__get_hash_from_flowi6(const struct flowi6
*fl6
, struct flow_keys
*keys
)
980 memset(keys
, 0, sizeof(*keys
));
982 memcpy(&keys
->addrs
.v6addrs
.src
, &fl6
->saddr
,
983 sizeof(keys
->addrs
.v6addrs
.src
));
984 memcpy(&keys
->addrs
.v6addrs
.dst
, &fl6
->daddr
,
985 sizeof(keys
->addrs
.v6addrs
.dst
));
986 keys
->control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
987 keys
->ports
.src
= fl6
->fl6_sport
;
988 keys
->ports
.dst
= fl6
->fl6_dport
;
989 keys
->keyid
.keyid
= fl6
->fl6_gre_key
;
990 keys
->tags
.flow_label
= (__force u32
)fl6
->flowlabel
;
991 keys
->basic
.ip_proto
= fl6
->flowi6_proto
;
993 return flow_hash_from_keys(keys
);
995 EXPORT_SYMBOL(__get_hash_from_flowi6
);
997 __u32
__get_hash_from_flowi4(const struct flowi4
*fl4
, struct flow_keys
*keys
)
999 memset(keys
, 0, sizeof(*keys
));
1001 keys
->addrs
.v4addrs
.src
= fl4
->saddr
;
1002 keys
->addrs
.v4addrs
.dst
= fl4
->daddr
;
1003 keys
->control
.addr_type
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
;
1004 keys
->ports
.src
= fl4
->fl4_sport
;
1005 keys
->ports
.dst
= fl4
->fl4_dport
;
1006 keys
->keyid
.keyid
= fl4
->fl4_gre_key
;
1007 keys
->basic
.ip_proto
= fl4
->flowi4_proto
;
1009 return flow_hash_from_keys(keys
);
1011 EXPORT_SYMBOL(__get_hash_from_flowi4
);
1013 static const struct flow_dissector_key flow_keys_dissector_keys
[] = {
1015 .key_id
= FLOW_DISSECTOR_KEY_CONTROL
,
1016 .offset
= offsetof(struct flow_keys
, control
),
1019 .key_id
= FLOW_DISSECTOR_KEY_BASIC
,
1020 .offset
= offsetof(struct flow_keys
, basic
),
1023 .key_id
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
,
1024 .offset
= offsetof(struct flow_keys
, addrs
.v4addrs
),
1027 .key_id
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
,
1028 .offset
= offsetof(struct flow_keys
, addrs
.v6addrs
),
1031 .key_id
= FLOW_DISSECTOR_KEY_TIPC_ADDRS
,
1032 .offset
= offsetof(struct flow_keys
, addrs
.tipcaddrs
),
1035 .key_id
= FLOW_DISSECTOR_KEY_PORTS
,
1036 .offset
= offsetof(struct flow_keys
, ports
),
1039 .key_id
= FLOW_DISSECTOR_KEY_VLAN
,
1040 .offset
= offsetof(struct flow_keys
, vlan
),
1043 .key_id
= FLOW_DISSECTOR_KEY_FLOW_LABEL
,
1044 .offset
= offsetof(struct flow_keys
, tags
),
1047 .key_id
= FLOW_DISSECTOR_KEY_GRE_KEYID
,
1048 .offset
= offsetof(struct flow_keys
, keyid
),
1052 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys
[] = {
1054 .key_id
= FLOW_DISSECTOR_KEY_CONTROL
,
1055 .offset
= offsetof(struct flow_keys
, control
),
1058 .key_id
= FLOW_DISSECTOR_KEY_BASIC
,
1059 .offset
= offsetof(struct flow_keys
, basic
),
1062 .key_id
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
,
1063 .offset
= offsetof(struct flow_keys
, addrs
.v4addrs
),
1066 .key_id
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
,
1067 .offset
= offsetof(struct flow_keys
, addrs
.v6addrs
),
1070 .key_id
= FLOW_DISSECTOR_KEY_PORTS
,
1071 .offset
= offsetof(struct flow_keys
, ports
),
1075 static const struct flow_dissector_key flow_keys_buf_dissector_keys
[] = {
1077 .key_id
= FLOW_DISSECTOR_KEY_CONTROL
,
1078 .offset
= offsetof(struct flow_keys
, control
),
1081 .key_id
= FLOW_DISSECTOR_KEY_BASIC
,
1082 .offset
= offsetof(struct flow_keys
, basic
),
1086 struct flow_dissector flow_keys_dissector __read_mostly
;
1087 EXPORT_SYMBOL(flow_keys_dissector
);
1089 struct flow_dissector flow_keys_buf_dissector __read_mostly
;
1091 static int __init
init_default_flow_dissectors(void)
1093 skb_flow_dissector_init(&flow_keys_dissector
,
1094 flow_keys_dissector_keys
,
1095 ARRAY_SIZE(flow_keys_dissector_keys
));
1096 skb_flow_dissector_init(&flow_keys_dissector_symmetric
,
1097 flow_keys_dissector_symmetric_keys
,
1098 ARRAY_SIZE(flow_keys_dissector_symmetric_keys
));
1099 skb_flow_dissector_init(&flow_keys_buf_dissector
,
1100 flow_keys_buf_dissector_keys
,
1101 ARRAY_SIZE(flow_keys_buf_dissector_keys
));
1105 core_initcall(init_default_flow_dissectors
);