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>
9 #include <linux/igmp.h>
10 #include <linux/icmp.h>
11 #include <linux/sctp.h>
12 #include <linux/dccp.h>
13 #include <linux/if_tunnel.h>
14 #include <linux/if_pppox.h>
15 #include <linux/ppp_defs.h>
16 #include <linux/stddef.h>
17 #include <linux/if_ether.h>
18 #include <linux/mpls.h>
19 #include <net/flow_dissector.h>
20 #include <scsi/fc/fc_fcoe.h>
22 static void dissector_set_key(struct flow_dissector
*flow_dissector
,
23 enum flow_dissector_key_id key_id
)
25 flow_dissector
->used_keys
|= (1 << key_id
);
28 void skb_flow_dissector_init(struct flow_dissector
*flow_dissector
,
29 const struct flow_dissector_key
*key
,
30 unsigned int key_count
)
34 memset(flow_dissector
, 0, sizeof(*flow_dissector
));
36 for (i
= 0; i
< key_count
; i
++, key
++) {
37 /* User should make sure that every key target offset is withing
38 * boundaries of unsigned short.
40 BUG_ON(key
->offset
> USHRT_MAX
);
41 BUG_ON(dissector_uses_key(flow_dissector
,
44 dissector_set_key(flow_dissector
, key
->key_id
);
45 flow_dissector
->offset
[key
->key_id
] = key
->offset
;
48 /* Ensure that the dissector always includes control and basic key.
49 * That way we are able to avoid handling lack of these in fast path.
51 BUG_ON(!dissector_uses_key(flow_dissector
,
52 FLOW_DISSECTOR_KEY_CONTROL
));
53 BUG_ON(!dissector_uses_key(flow_dissector
,
54 FLOW_DISSECTOR_KEY_BASIC
));
56 EXPORT_SYMBOL(skb_flow_dissector_init
);
59 * __skb_flow_get_ports - extract the upper layer ports and return them
60 * @skb: sk_buff to extract the ports from
61 * @thoff: transport header offset
62 * @ip_proto: protocol for which to get port offset
63 * @data: raw buffer pointer to the packet, if NULL use skb->data
64 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
66 * The function will try to retrieve the ports at offset thoff + poff where poff
67 * is the protocol port offset returned from proto_ports_offset
69 __be32
__skb_flow_get_ports(const struct sk_buff
*skb
, int thoff
, u8 ip_proto
,
72 int poff
= proto_ports_offset(ip_proto
);
76 hlen
= skb_headlen(skb
);
80 __be32
*ports
, _ports
;
82 ports
= __skb_header_pointer(skb
, thoff
+ poff
,
83 sizeof(_ports
), data
, hlen
, &_ports
);
90 EXPORT_SYMBOL(__skb_flow_get_ports
);
93 * __skb_flow_dissect - extract the flow_keys struct and return it
94 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
95 * @flow_dissector: list of keys to dissect
96 * @target_container: target structure to put dissected values into
97 * @data: raw buffer pointer to the packet, if NULL use skb->data
98 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
99 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
100 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
102 * The function will try to retrieve individual keys into target specified
103 * by flow_dissector from either the skbuff or a raw buffer specified by the
106 * Caller must take care of zeroing target container memory.
108 bool __skb_flow_dissect(const struct sk_buff
*skb
,
109 struct flow_dissector
*flow_dissector
,
110 void *target_container
,
111 void *data
, __be16 proto
, int nhoff
, int hlen
,
114 struct flow_dissector_key_control
*key_control
;
115 struct flow_dissector_key_basic
*key_basic
;
116 struct flow_dissector_key_addrs
*key_addrs
;
117 struct flow_dissector_key_ports
*key_ports
;
118 struct flow_dissector_key_tags
*key_tags
;
119 struct flow_dissector_key_keyid
*key_keyid
;
125 proto
= skb
->protocol
;
126 nhoff
= skb_network_offset(skb
);
127 hlen
= skb_headlen(skb
);
130 /* It is ensured by skb_flow_dissector_init() that control key will
133 key_control
= skb_flow_dissector_target(flow_dissector
,
134 FLOW_DISSECTOR_KEY_CONTROL
,
137 /* It is ensured by skb_flow_dissector_init() that basic key will
140 key_basic
= skb_flow_dissector_target(flow_dissector
,
141 FLOW_DISSECTOR_KEY_BASIC
,
144 if (dissector_uses_key(flow_dissector
,
145 FLOW_DISSECTOR_KEY_ETH_ADDRS
)) {
146 struct ethhdr
*eth
= eth_hdr(skb
);
147 struct flow_dissector_key_eth_addrs
*key_eth_addrs
;
149 key_eth_addrs
= skb_flow_dissector_target(flow_dissector
,
150 FLOW_DISSECTOR_KEY_ETH_ADDRS
,
152 memcpy(key_eth_addrs
, ð
->h_dest
, sizeof(*key_eth_addrs
));
157 case htons(ETH_P_IP
): {
158 const struct iphdr
*iph
;
161 iph
= __skb_header_pointer(skb
, nhoff
, sizeof(_iph
), data
, hlen
, &_iph
);
162 if (!iph
|| iph
->ihl
< 5)
164 nhoff
+= iph
->ihl
* 4;
166 ip_proto
= iph
->protocol
;
168 if (dissector_uses_key(flow_dissector
,
169 FLOW_DISSECTOR_KEY_IPV4_ADDRS
)) {
170 key_addrs
= skb_flow_dissector_target(flow_dissector
,
171 FLOW_DISSECTOR_KEY_IPV4_ADDRS
,
174 memcpy(&key_addrs
->v4addrs
, &iph
->saddr
,
175 sizeof(key_addrs
->v4addrs
));
176 key_control
->addr_type
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
;
179 if (ip_is_fragment(iph
)) {
180 key_control
->flags
|= FLOW_DIS_IS_FRAGMENT
;
182 if (iph
->frag_off
& htons(IP_OFFSET
)) {
185 key_control
->flags
|= FLOW_DIS_FIRST_FRAG
;
186 if (!(flags
& FLOW_DISSECTOR_F_PARSE_1ST_FRAG
))
191 if (flags
& FLOW_DISSECTOR_F_STOP_AT_L3
)
196 case htons(ETH_P_IPV6
): {
197 const struct ipv6hdr
*iph
;
201 iph
= __skb_header_pointer(skb
, nhoff
, sizeof(_iph
), data
, hlen
, &_iph
);
205 ip_proto
= iph
->nexthdr
;
206 nhoff
+= sizeof(struct ipv6hdr
);
208 if (dissector_uses_key(flow_dissector
,
209 FLOW_DISSECTOR_KEY_IPV6_ADDRS
)) {
210 key_addrs
= skb_flow_dissector_target(flow_dissector
,
211 FLOW_DISSECTOR_KEY_IPV6_ADDRS
,
214 memcpy(&key_addrs
->v6addrs
, &iph
->saddr
,
215 sizeof(key_addrs
->v6addrs
));
216 key_control
->addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
219 if ((dissector_uses_key(flow_dissector
,
220 FLOW_DISSECTOR_KEY_FLOW_LABEL
) ||
221 (flags
& FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
)) &&
222 ip6_flowlabel(iph
)) {
223 __be32 flow_label
= ip6_flowlabel(iph
);
225 if (dissector_uses_key(flow_dissector
,
226 FLOW_DISSECTOR_KEY_FLOW_LABEL
)) {
227 key_tags
= skb_flow_dissector_target(flow_dissector
,
228 FLOW_DISSECTOR_KEY_FLOW_LABEL
,
230 key_tags
->flow_label
= ntohl(flow_label
);
232 if (flags
& FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
)
236 if (flags
& FLOW_DISSECTOR_F_STOP_AT_L3
)
241 case htons(ETH_P_8021AD
):
242 case htons(ETH_P_8021Q
): {
243 const struct vlan_hdr
*vlan
;
244 struct vlan_hdr _vlan
;
246 vlan
= __skb_header_pointer(skb
, nhoff
, sizeof(_vlan
), data
, hlen
, &_vlan
);
250 if (dissector_uses_key(flow_dissector
,
251 FLOW_DISSECTOR_KEY_VLANID
)) {
252 key_tags
= skb_flow_dissector_target(flow_dissector
,
253 FLOW_DISSECTOR_KEY_VLANID
,
256 key_tags
->vlan_id
= skb_vlan_tag_get_id(skb
);
259 proto
= vlan
->h_vlan_encapsulated_proto
;
260 nhoff
+= sizeof(*vlan
);
263 case htons(ETH_P_PPP_SES
): {
265 struct pppoe_hdr hdr
;
268 hdr
= __skb_header_pointer(skb
, nhoff
, sizeof(_hdr
), data
, hlen
, &_hdr
);
272 nhoff
+= PPPOE_SES_HLEN
;
276 case htons(PPP_IPV6
):
282 case htons(ETH_P_TIPC
): {
287 hdr
= __skb_header_pointer(skb
, nhoff
, sizeof(_hdr
), data
, hlen
, &_hdr
);
291 if (dissector_uses_key(flow_dissector
,
292 FLOW_DISSECTOR_KEY_TIPC_ADDRS
)) {
293 key_addrs
= skb_flow_dissector_target(flow_dissector
,
294 FLOW_DISSECTOR_KEY_TIPC_ADDRS
,
296 key_addrs
->tipcaddrs
.srcnode
= hdr
->srcnode
;
297 key_control
->addr_type
= FLOW_DISSECTOR_KEY_TIPC_ADDRS
;
302 case htons(ETH_P_MPLS_UC
):
303 case htons(ETH_P_MPLS_MC
): {
304 struct mpls_label
*hdr
, _hdr
[2];
306 hdr
= __skb_header_pointer(skb
, nhoff
, sizeof(_hdr
), data
,
311 if ((ntohl(hdr
[0].entry
) & MPLS_LS_LABEL_MASK
) >>
312 MPLS_LS_LABEL_SHIFT
== MPLS_LABEL_ENTROPY
) {
313 if (dissector_uses_key(flow_dissector
,
314 FLOW_DISSECTOR_KEY_MPLS_ENTROPY
)) {
315 key_keyid
= skb_flow_dissector_target(flow_dissector
,
316 FLOW_DISSECTOR_KEY_MPLS_ENTROPY
,
318 key_keyid
->keyid
= hdr
[1].entry
&
319 htonl(MPLS_LS_LABEL_MASK
);
328 case htons(ETH_P_FCOE
):
329 if ((hlen
- nhoff
) < FCOE_HEADER_LEN
)
332 nhoff
+= FCOE_HEADER_LEN
;
346 hdr
= __skb_header_pointer(skb
, nhoff
, sizeof(_hdr
), data
, hlen
, &_hdr
);
350 * Only look inside GRE if version zero and no
353 if (hdr
->flags
& (GRE_VERSION
| GRE_ROUTING
))
358 if (hdr
->flags
& GRE_CSUM
)
360 if (hdr
->flags
& GRE_KEY
) {
364 keyid
= __skb_header_pointer(skb
, nhoff
, sizeof(_keyid
),
365 data
, hlen
, &_keyid
);
370 if (dissector_uses_key(flow_dissector
,
371 FLOW_DISSECTOR_KEY_GRE_KEYID
)) {
372 key_keyid
= skb_flow_dissector_target(flow_dissector
,
373 FLOW_DISSECTOR_KEY_GRE_KEYID
,
375 key_keyid
->keyid
= *keyid
;
379 if (hdr
->flags
& GRE_SEQ
)
381 if (proto
== htons(ETH_P_TEB
)) {
382 const struct ethhdr
*eth
;
385 eth
= __skb_header_pointer(skb
, nhoff
,
390 proto
= eth
->h_proto
;
391 nhoff
+= sizeof(*eth
);
393 /* Cap headers that we access via pointers at the
394 * end of the Ethernet header as our maximum alignment
395 * at that point is only 2 bytes.
401 key_control
->flags
|= FLOW_DIS_ENCAPSULATION
;
402 if (flags
& FLOW_DISSECTOR_F_STOP_AT_ENCAP
)
408 case NEXTHDR_ROUTING
:
410 u8 _opthdr
[2], *opthdr
;
412 if (proto
!= htons(ETH_P_IPV6
))
415 opthdr
= __skb_header_pointer(skb
, nhoff
, sizeof(_opthdr
),
416 data
, hlen
, &_opthdr
);
420 ip_proto
= opthdr
[0];
421 nhoff
+= (opthdr
[1] + 1) << 3;
425 case NEXTHDR_FRAGMENT
: {
426 struct frag_hdr _fh
, *fh
;
428 if (proto
!= htons(ETH_P_IPV6
))
431 fh
= __skb_header_pointer(skb
, nhoff
, sizeof(_fh
),
437 key_control
->flags
|= FLOW_DIS_IS_FRAGMENT
;
439 nhoff
+= sizeof(_fh
);
440 ip_proto
= fh
->nexthdr
;
442 if (!(fh
->frag_off
& htons(IP6_OFFSET
))) {
443 key_control
->flags
|= FLOW_DIS_FIRST_FRAG
;
444 if (flags
& FLOW_DISSECTOR_F_PARSE_1ST_FRAG
)
450 proto
= htons(ETH_P_IP
);
452 key_control
->flags
|= FLOW_DIS_ENCAPSULATION
;
453 if (flags
& FLOW_DISSECTOR_F_STOP_AT_ENCAP
)
458 proto
= htons(ETH_P_IPV6
);
460 key_control
->flags
|= FLOW_DIS_ENCAPSULATION
;
461 if (flags
& FLOW_DISSECTOR_F_STOP_AT_ENCAP
)
466 proto
= htons(ETH_P_MPLS_UC
);
472 if (dissector_uses_key(flow_dissector
,
473 FLOW_DISSECTOR_KEY_PORTS
)) {
474 key_ports
= skb_flow_dissector_target(flow_dissector
,
475 FLOW_DISSECTOR_KEY_PORTS
,
477 key_ports
->ports
= __skb_flow_get_ports(skb
, nhoff
, ip_proto
,
485 key_basic
->n_proto
= proto
;
486 key_basic
->ip_proto
= ip_proto
;
487 key_control
->thoff
= (u16
)nhoff
;
491 EXPORT_SYMBOL(__skb_flow_dissect
);
493 static u32 hashrnd __read_mostly
;
494 static __always_inline
void __flow_hash_secret_init(void)
496 net_get_random_once(&hashrnd
, sizeof(hashrnd
));
499 static __always_inline u32
__flow_hash_words(const u32
*words
, u32 length
,
502 return jhash2(words
, length
, keyval
);
505 static inline const u32
*flow_keys_hash_start(const struct flow_keys
*flow
)
507 const void *p
= flow
;
509 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET
% sizeof(u32
));
510 return (const u32
*)(p
+ FLOW_KEYS_HASH_OFFSET
);
513 static inline size_t flow_keys_hash_length(const struct flow_keys
*flow
)
515 size_t diff
= FLOW_KEYS_HASH_OFFSET
+ sizeof(flow
->addrs
);
516 BUILD_BUG_ON((sizeof(*flow
) - FLOW_KEYS_HASH_OFFSET
) % sizeof(u32
));
517 BUILD_BUG_ON(offsetof(typeof(*flow
), addrs
) !=
518 sizeof(*flow
) - sizeof(flow
->addrs
));
520 switch (flow
->control
.addr_type
) {
521 case FLOW_DISSECTOR_KEY_IPV4_ADDRS
:
522 diff
-= sizeof(flow
->addrs
.v4addrs
);
524 case FLOW_DISSECTOR_KEY_IPV6_ADDRS
:
525 diff
-= sizeof(flow
->addrs
.v6addrs
);
527 case FLOW_DISSECTOR_KEY_TIPC_ADDRS
:
528 diff
-= sizeof(flow
->addrs
.tipcaddrs
);
531 return (sizeof(*flow
) - diff
) / sizeof(u32
);
534 __be32
flow_get_u32_src(const struct flow_keys
*flow
)
536 switch (flow
->control
.addr_type
) {
537 case FLOW_DISSECTOR_KEY_IPV4_ADDRS
:
538 return flow
->addrs
.v4addrs
.src
;
539 case FLOW_DISSECTOR_KEY_IPV6_ADDRS
:
540 return (__force __be32
)ipv6_addr_hash(
541 &flow
->addrs
.v6addrs
.src
);
542 case FLOW_DISSECTOR_KEY_TIPC_ADDRS
:
543 return flow
->addrs
.tipcaddrs
.srcnode
;
548 EXPORT_SYMBOL(flow_get_u32_src
);
550 __be32
flow_get_u32_dst(const struct flow_keys
*flow
)
552 switch (flow
->control
.addr_type
) {
553 case FLOW_DISSECTOR_KEY_IPV4_ADDRS
:
554 return flow
->addrs
.v4addrs
.dst
;
555 case FLOW_DISSECTOR_KEY_IPV6_ADDRS
:
556 return (__force __be32
)ipv6_addr_hash(
557 &flow
->addrs
.v6addrs
.dst
);
562 EXPORT_SYMBOL(flow_get_u32_dst
);
564 static inline void __flow_hash_consistentify(struct flow_keys
*keys
)
568 switch (keys
->control
.addr_type
) {
569 case FLOW_DISSECTOR_KEY_IPV4_ADDRS
:
570 addr_diff
= (__force u32
)keys
->addrs
.v4addrs
.dst
-
571 (__force u32
)keys
->addrs
.v4addrs
.src
;
572 if ((addr_diff
< 0) ||
574 ((__force u16
)keys
->ports
.dst
<
575 (__force u16
)keys
->ports
.src
))) {
576 swap(keys
->addrs
.v4addrs
.src
, keys
->addrs
.v4addrs
.dst
);
577 swap(keys
->ports
.src
, keys
->ports
.dst
);
580 case FLOW_DISSECTOR_KEY_IPV6_ADDRS
:
581 addr_diff
= memcmp(&keys
->addrs
.v6addrs
.dst
,
582 &keys
->addrs
.v6addrs
.src
,
583 sizeof(keys
->addrs
.v6addrs
.dst
));
584 if ((addr_diff
< 0) ||
586 ((__force u16
)keys
->ports
.dst
<
587 (__force u16
)keys
->ports
.src
))) {
588 for (i
= 0; i
< 4; i
++)
589 swap(keys
->addrs
.v6addrs
.src
.s6_addr32
[i
],
590 keys
->addrs
.v6addrs
.dst
.s6_addr32
[i
]);
591 swap(keys
->ports
.src
, keys
->ports
.dst
);
597 static inline u32
__flow_hash_from_keys(struct flow_keys
*keys
, u32 keyval
)
601 __flow_hash_consistentify(keys
);
603 hash
= __flow_hash_words(flow_keys_hash_start(keys
),
604 flow_keys_hash_length(keys
), keyval
);
611 u32
flow_hash_from_keys(struct flow_keys
*keys
)
613 __flow_hash_secret_init();
614 return __flow_hash_from_keys(keys
, hashrnd
);
616 EXPORT_SYMBOL(flow_hash_from_keys
);
618 static inline u32
___skb_get_hash(const struct sk_buff
*skb
,
619 struct flow_keys
*keys
, u32 keyval
)
621 skb_flow_dissect_flow_keys(skb
, keys
,
622 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
);
624 return __flow_hash_from_keys(keys
, keyval
);
627 struct _flow_keys_digest_data
{
636 void make_flow_keys_digest(struct flow_keys_digest
*digest
,
637 const struct flow_keys
*flow
)
639 struct _flow_keys_digest_data
*data
=
640 (struct _flow_keys_digest_data
*)digest
;
642 BUILD_BUG_ON(sizeof(*data
) > sizeof(*digest
));
644 memset(digest
, 0, sizeof(*digest
));
646 data
->n_proto
= flow
->basic
.n_proto
;
647 data
->ip_proto
= flow
->basic
.ip_proto
;
648 data
->ports
= flow
->ports
.ports
;
649 data
->src
= flow
->addrs
.v4addrs
.src
;
650 data
->dst
= flow
->addrs
.v4addrs
.dst
;
652 EXPORT_SYMBOL(make_flow_keys_digest
);
655 * __skb_get_hash: calculate a flow hash
656 * @skb: sk_buff to calculate flow hash from
658 * This function calculates a flow hash based on src/dst addresses
659 * and src/dst port numbers. Sets hash in skb to non-zero hash value
660 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
661 * if hash is a canonical 4-tuple hash over transport ports.
663 void __skb_get_hash(struct sk_buff
*skb
)
665 struct flow_keys keys
;
667 __flow_hash_secret_init();
669 __skb_set_sw_hash(skb
, ___skb_get_hash(skb
, &keys
, hashrnd
),
670 flow_keys_have_l4(&keys
));
672 EXPORT_SYMBOL(__skb_get_hash
);
674 __u32
skb_get_hash_perturb(const struct sk_buff
*skb
, u32 perturb
)
676 struct flow_keys keys
;
678 return ___skb_get_hash(skb
, &keys
, perturb
);
680 EXPORT_SYMBOL(skb_get_hash_perturb
);
682 __u32
__skb_get_hash_flowi6(struct sk_buff
*skb
, const struct flowi6
*fl6
)
684 struct flow_keys keys
;
686 memset(&keys
, 0, sizeof(keys
));
688 memcpy(&keys
.addrs
.v6addrs
.src
, &fl6
->saddr
,
689 sizeof(keys
.addrs
.v6addrs
.src
));
690 memcpy(&keys
.addrs
.v6addrs
.dst
, &fl6
->daddr
,
691 sizeof(keys
.addrs
.v6addrs
.dst
));
692 keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
693 keys
.ports
.src
= fl6
->fl6_sport
;
694 keys
.ports
.dst
= fl6
->fl6_dport
;
695 keys
.keyid
.keyid
= fl6
->fl6_gre_key
;
696 keys
.tags
.flow_label
= (__force u32
)fl6
->flowlabel
;
697 keys
.basic
.ip_proto
= fl6
->flowi6_proto
;
699 __skb_set_sw_hash(skb
, flow_hash_from_keys(&keys
),
700 flow_keys_have_l4(&keys
));
704 EXPORT_SYMBOL(__skb_get_hash_flowi6
);
706 __u32
__skb_get_hash_flowi4(struct sk_buff
*skb
, const struct flowi4
*fl4
)
708 struct flow_keys keys
;
710 memset(&keys
, 0, sizeof(keys
));
712 keys
.addrs
.v4addrs
.src
= fl4
->saddr
;
713 keys
.addrs
.v4addrs
.dst
= fl4
->daddr
;
714 keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
;
715 keys
.ports
.src
= fl4
->fl4_sport
;
716 keys
.ports
.dst
= fl4
->fl4_dport
;
717 keys
.keyid
.keyid
= fl4
->fl4_gre_key
;
718 keys
.basic
.ip_proto
= fl4
->flowi4_proto
;
720 __skb_set_sw_hash(skb
, flow_hash_from_keys(&keys
),
721 flow_keys_have_l4(&keys
));
725 EXPORT_SYMBOL(__skb_get_hash_flowi4
);
727 u32
__skb_get_poff(const struct sk_buff
*skb
, void *data
,
728 const struct flow_keys
*keys
, int hlen
)
730 u32 poff
= keys
->control
.thoff
;
732 /* skip L4 headers for fragments after the first */
733 if ((keys
->control
.flags
& FLOW_DIS_IS_FRAGMENT
) &&
734 !(keys
->control
.flags
& FLOW_DIS_FIRST_FRAG
))
737 switch (keys
->basic
.ip_proto
) {
739 /* access doff as u8 to avoid unaligned access */
743 doff
= __skb_header_pointer(skb
, poff
+ 12, sizeof(_doff
),
748 poff
+= max_t(u32
, sizeof(struct tcphdr
), (*doff
& 0xF0) >> 2);
752 case IPPROTO_UDPLITE
:
753 poff
+= sizeof(struct udphdr
);
755 /* For the rest, we do not really care about header
756 * extensions at this point for now.
759 poff
+= sizeof(struct icmphdr
);
762 poff
+= sizeof(struct icmp6hdr
);
765 poff
+= sizeof(struct igmphdr
);
768 poff
+= sizeof(struct dccp_hdr
);
771 poff
+= sizeof(struct sctphdr
);
779 * skb_get_poff - get the offset to the payload
780 * @skb: sk_buff to get the payload offset from
782 * The function will get the offset to the payload as far as it could
783 * be dissected. The main user is currently BPF, so that we can dynamically
784 * truncate packets without needing to push actual payload to the user
785 * space and can analyze headers only, instead.
787 u32
skb_get_poff(const struct sk_buff
*skb
)
789 struct flow_keys keys
;
791 if (!skb_flow_dissect_flow_keys(skb
, &keys
, 0))
794 return __skb_get_poff(skb
, skb
->data
, &keys
, skb_headlen(skb
));
797 __u32
__get_hash_from_flowi6(const struct flowi6
*fl6
, struct flow_keys
*keys
)
799 memset(keys
, 0, sizeof(*keys
));
801 memcpy(&keys
->addrs
.v6addrs
.src
, &fl6
->saddr
,
802 sizeof(keys
->addrs
.v6addrs
.src
));
803 memcpy(&keys
->addrs
.v6addrs
.dst
, &fl6
->daddr
,
804 sizeof(keys
->addrs
.v6addrs
.dst
));
805 keys
->control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
806 keys
->ports
.src
= fl6
->fl6_sport
;
807 keys
->ports
.dst
= fl6
->fl6_dport
;
808 keys
->keyid
.keyid
= fl6
->fl6_gre_key
;
809 keys
->tags
.flow_label
= (__force u32
)fl6
->flowlabel
;
810 keys
->basic
.ip_proto
= fl6
->flowi6_proto
;
812 return flow_hash_from_keys(keys
);
814 EXPORT_SYMBOL(__get_hash_from_flowi6
);
816 __u32
__get_hash_from_flowi4(const struct flowi4
*fl4
, struct flow_keys
*keys
)
818 memset(keys
, 0, sizeof(*keys
));
820 keys
->addrs
.v4addrs
.src
= fl4
->saddr
;
821 keys
->addrs
.v4addrs
.dst
= fl4
->daddr
;
822 keys
->control
.addr_type
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
;
823 keys
->ports
.src
= fl4
->fl4_sport
;
824 keys
->ports
.dst
= fl4
->fl4_dport
;
825 keys
->keyid
.keyid
= fl4
->fl4_gre_key
;
826 keys
->basic
.ip_proto
= fl4
->flowi4_proto
;
828 return flow_hash_from_keys(keys
);
830 EXPORT_SYMBOL(__get_hash_from_flowi4
);
832 static const struct flow_dissector_key flow_keys_dissector_keys
[] = {
834 .key_id
= FLOW_DISSECTOR_KEY_CONTROL
,
835 .offset
= offsetof(struct flow_keys
, control
),
838 .key_id
= FLOW_DISSECTOR_KEY_BASIC
,
839 .offset
= offsetof(struct flow_keys
, basic
),
842 .key_id
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
,
843 .offset
= offsetof(struct flow_keys
, addrs
.v4addrs
),
846 .key_id
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
,
847 .offset
= offsetof(struct flow_keys
, addrs
.v6addrs
),
850 .key_id
= FLOW_DISSECTOR_KEY_TIPC_ADDRS
,
851 .offset
= offsetof(struct flow_keys
, addrs
.tipcaddrs
),
854 .key_id
= FLOW_DISSECTOR_KEY_PORTS
,
855 .offset
= offsetof(struct flow_keys
, ports
),
858 .key_id
= FLOW_DISSECTOR_KEY_VLANID
,
859 .offset
= offsetof(struct flow_keys
, tags
),
862 .key_id
= FLOW_DISSECTOR_KEY_FLOW_LABEL
,
863 .offset
= offsetof(struct flow_keys
, tags
),
866 .key_id
= FLOW_DISSECTOR_KEY_GRE_KEYID
,
867 .offset
= offsetof(struct flow_keys
, keyid
),
871 static const struct flow_dissector_key flow_keys_buf_dissector_keys
[] = {
873 .key_id
= FLOW_DISSECTOR_KEY_CONTROL
,
874 .offset
= offsetof(struct flow_keys
, control
),
877 .key_id
= FLOW_DISSECTOR_KEY_BASIC
,
878 .offset
= offsetof(struct flow_keys
, basic
),
882 struct flow_dissector flow_keys_dissector __read_mostly
;
883 EXPORT_SYMBOL(flow_keys_dissector
);
885 struct flow_dissector flow_keys_buf_dissector __read_mostly
;
887 static int __init
init_default_flow_dissectors(void)
889 skb_flow_dissector_init(&flow_keys_dissector
,
890 flow_keys_dissector_keys
,
891 ARRAY_SIZE(flow_keys_dissector_keys
));
892 skb_flow_dissector_init(&flow_keys_buf_dissector
,
893 flow_keys_buf_dissector_keys
,
894 ARRAY_SIZE(flow_keys_buf_dissector_keys
));
898 late_initcall_sync(init_default_flow_dissectors
);