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_ports - extract the upper layer ports and return them
62 * @skb: sk_buff to extract the ports from
63 * @thoff: transport header offset
64 * @ip_proto: protocol for which to get port offset
65 * @data: raw buffer pointer to the packet, if NULL use skb->data
66 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
68 * The function will try to retrieve the ports at offset thoff + poff where poff
69 * is the protocol port offset returned from proto_ports_offset
71 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
74 int poff = proto_ports_offset(ip_proto);
78 hlen = skb_headlen(skb);
82 __be32 *ports, _ports;
84 ports = __skb_header_pointer(skb, thoff + poff,
85 sizeof(_ports), data, hlen, &_ports);
92 EXPORT_SYMBOL(__skb_flow_get_ports);
95 * __skb_flow_dissect - extract the flow_keys struct and return it
96 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
97 * @flow_dissector: list of keys to dissect
98 * @target_container: target structure to put dissected values into
99 * @data: raw buffer pointer to the packet, if NULL use skb->data
100 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
101 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
102 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
104 * The function will try to retrieve individual keys into target specified
105 * by flow_dissector from either the skbuff or a raw buffer specified by the
108 * Caller must take care of zeroing target container memory.
110 bool __skb_flow_dissect(const struct sk_buff *skb,
111 struct flow_dissector *flow_dissector,
112 void *target_container,
113 void *data, __be16 proto, int nhoff, int hlen,
116 struct flow_dissector_key_control *key_control;
117 struct flow_dissector_key_basic *key_basic;
118 struct flow_dissector_key_addrs *key_addrs;
119 struct flow_dissector_key_ports *key_ports;
120 struct flow_dissector_key_tags *key_tags;
121 struct flow_dissector_key_keyid *key_keyid;
127 proto = skb->protocol;
128 nhoff = skb_network_offset(skb);
129 hlen = skb_headlen(skb);
132 /* It is ensured by skb_flow_dissector_init() that control key will
135 key_control = skb_flow_dissector_target(flow_dissector,
136 FLOW_DISSECTOR_KEY_CONTROL,
139 /* It is ensured by skb_flow_dissector_init() that basic key will
142 key_basic = skb_flow_dissector_target(flow_dissector,
143 FLOW_DISSECTOR_KEY_BASIC,
146 if (dissector_uses_key(flow_dissector,
147 FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
148 struct ethhdr *eth = eth_hdr(skb);
149 struct flow_dissector_key_eth_addrs *key_eth_addrs;
151 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
152 FLOW_DISSECTOR_KEY_ETH_ADDRS,
154 memcpy(key_eth_addrs, ð->h_dest, sizeof(*key_eth_addrs));
159 case htons(ETH_P_IP): {
160 const struct iphdr *iph;
163 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
164 if (!iph || iph->ihl < 5)
166 nhoff += iph->ihl * 4;
168 ip_proto = iph->protocol;
170 if (dissector_uses_key(flow_dissector,
171 FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
172 key_addrs = skb_flow_dissector_target(flow_dissector,
173 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
176 memcpy(&key_addrs->v4addrs, &iph->saddr,
177 sizeof(key_addrs->v4addrs));
178 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
181 if (ip_is_fragment(iph)) {
182 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
184 if (iph->frag_off & htons(IP_OFFSET)) {
187 key_control->flags |= FLOW_DIS_FIRST_FRAG;
188 if (!(flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG))
193 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
198 case htons(ETH_P_IPV6): {
199 const struct ipv6hdr *iph;
203 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
207 ip_proto = iph->nexthdr;
208 nhoff += sizeof(struct ipv6hdr);
210 if (dissector_uses_key(flow_dissector,
211 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
212 key_addrs = skb_flow_dissector_target(flow_dissector,
213 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
216 memcpy(&key_addrs->v6addrs, &iph->saddr,
217 sizeof(key_addrs->v6addrs));
218 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
221 if ((dissector_uses_key(flow_dissector,
222 FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
223 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
224 ip6_flowlabel(iph)) {
225 __be32 flow_label = ip6_flowlabel(iph);
227 if (dissector_uses_key(flow_dissector,
228 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
229 key_tags = skb_flow_dissector_target(flow_dissector,
230 FLOW_DISSECTOR_KEY_FLOW_LABEL,
232 key_tags->flow_label = ntohl(flow_label);
234 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)
238 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
243 case htons(ETH_P_8021AD):
244 case htons(ETH_P_8021Q): {
245 const struct vlan_hdr *vlan;
246 struct vlan_hdr _vlan;
248 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), data, hlen, &_vlan);
252 if (dissector_uses_key(flow_dissector,
253 FLOW_DISSECTOR_KEY_VLANID)) {
254 key_tags = skb_flow_dissector_target(flow_dissector,
255 FLOW_DISSECTOR_KEY_VLANID,
258 key_tags->vlan_id = skb_vlan_tag_get_id(skb);
261 proto = vlan->h_vlan_encapsulated_proto;
262 nhoff += sizeof(*vlan);
265 case htons(ETH_P_PPP_SES): {
267 struct pppoe_hdr hdr;
270 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
274 nhoff += PPPOE_SES_HLEN;
278 case htons(PPP_IPV6):
284 case htons(ETH_P_TIPC): {
289 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
293 if (dissector_uses_key(flow_dissector,
294 FLOW_DISSECTOR_KEY_TIPC_ADDRS)) {
295 key_addrs = skb_flow_dissector_target(flow_dissector,
296 FLOW_DISSECTOR_KEY_TIPC_ADDRS,
298 key_addrs->tipcaddrs.srcnode = hdr->srcnode;
299 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC_ADDRS;
304 case htons(ETH_P_MPLS_UC):
305 case htons(ETH_P_MPLS_MC): {
306 struct mpls_label *hdr, _hdr[2];
308 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
313 if ((ntohl(hdr[0].entry) & MPLS_LS_LABEL_MASK) >>
314 MPLS_LS_LABEL_SHIFT == MPLS_LABEL_ENTROPY) {
315 if (dissector_uses_key(flow_dissector,
316 FLOW_DISSECTOR_KEY_MPLS_ENTROPY)) {
317 key_keyid = skb_flow_dissector_target(flow_dissector,
318 FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
320 key_keyid->keyid = hdr[1].entry &
321 htonl(MPLS_LS_LABEL_MASK);
330 case htons(ETH_P_FCOE):
331 if ((hlen - nhoff) < FCOE_HEADER_LEN)
334 nhoff += FCOE_HEADER_LEN;
343 struct gre_base_hdr *hdr, _hdr;
347 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
351 /* Only look inside GRE without routing */
352 if (hdr->flags & GRE_ROUTING)
355 /* Only look inside GRE for version 0 and 1 */
356 gre_ver = ntohs(hdr->flags & GRE_VERSION);
360 proto = hdr->protocol;
362 /* Version1 must be PPTP, and check the flags */
363 if (!(proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY)))
367 offset += sizeof(struct gre_base_hdr);
369 if (hdr->flags & GRE_CSUM)
370 offset += sizeof(((struct gre_full_hdr *)0)->csum) +
371 sizeof(((struct gre_full_hdr *)0)->reserved1);
373 if (hdr->flags & GRE_KEY) {
377 keyid = __skb_header_pointer(skb, nhoff + offset, sizeof(_keyid),
378 data, hlen, &_keyid);
382 if (dissector_uses_key(flow_dissector,
383 FLOW_DISSECTOR_KEY_GRE_KEYID)) {
384 key_keyid = skb_flow_dissector_target(flow_dissector,
385 FLOW_DISSECTOR_KEY_GRE_KEYID,
388 key_keyid->keyid = *keyid;
390 key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK;
392 offset += sizeof(((struct gre_full_hdr *)0)->key);
395 if (hdr->flags & GRE_SEQ)
396 offset += sizeof(((struct pptp_gre_header *)0)->seq);
399 if (proto == htons(ETH_P_TEB)) {
400 const struct ethhdr *eth;
403 eth = __skb_header_pointer(skb, nhoff + offset,
408 proto = eth->h_proto;
409 offset += sizeof(*eth);
411 /* Cap headers that we access via pointers at the
412 * end of the Ethernet header as our maximum alignment
413 * at that point is only 2 bytes.
416 hlen = (nhoff + offset);
418 } else { /* version 1, must be PPTP */
419 u8 _ppp_hdr[PPP_HDRLEN];
422 if (hdr->flags & GRE_ACK)
423 offset += sizeof(((struct pptp_gre_header *)0)->ack);
425 ppp_hdr = skb_header_pointer(skb, nhoff + offset,
426 sizeof(_ppp_hdr), _ppp_hdr);
430 switch (PPP_PROTOCOL(ppp_hdr)) {
432 proto = htons(ETH_P_IP);
435 proto = htons(ETH_P_IPV6);
438 /* Could probably catch some more like MPLS */
442 offset += PPP_HDRLEN;
446 key_control->flags |= FLOW_DIS_ENCAPSULATION;
447 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
453 case NEXTHDR_ROUTING:
455 u8 _opthdr[2], *opthdr;
457 if (proto != htons(ETH_P_IPV6))
460 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
461 data, hlen, &_opthdr);
465 ip_proto = opthdr[0];
466 nhoff += (opthdr[1] + 1) << 3;
470 case NEXTHDR_FRAGMENT: {
471 struct frag_hdr _fh, *fh;
473 if (proto != htons(ETH_P_IPV6))
476 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
482 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
484 nhoff += sizeof(_fh);
485 ip_proto = fh->nexthdr;
487 if (!(fh->frag_off & htons(IP6_OFFSET))) {
488 key_control->flags |= FLOW_DIS_FIRST_FRAG;
489 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG)
495 proto = htons(ETH_P_IP);
497 key_control->flags |= FLOW_DIS_ENCAPSULATION;
498 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
503 proto = htons(ETH_P_IPV6);
505 key_control->flags |= FLOW_DIS_ENCAPSULATION;
506 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
511 proto = htons(ETH_P_MPLS_UC);
517 if (dissector_uses_key(flow_dissector,
518 FLOW_DISSECTOR_KEY_PORTS)) {
519 key_ports = skb_flow_dissector_target(flow_dissector,
520 FLOW_DISSECTOR_KEY_PORTS,
522 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
530 key_basic->n_proto = proto;
531 key_basic->ip_proto = ip_proto;
532 key_control->thoff = (u16)nhoff;
536 EXPORT_SYMBOL(__skb_flow_dissect);
538 static u32 hashrnd __read_mostly;
539 static __always_inline void __flow_hash_secret_init(void)
541 net_get_random_once(&hashrnd, sizeof(hashrnd));
544 static __always_inline u32 __flow_hash_words(const u32 *words, u32 length,
547 return jhash2(words, length, keyval);
550 static inline const u32 *flow_keys_hash_start(const struct flow_keys *flow)
552 const void *p = flow;
554 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32));
555 return (const u32 *)(p + FLOW_KEYS_HASH_OFFSET);
558 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
560 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
561 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
562 BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
563 sizeof(*flow) - sizeof(flow->addrs));
565 switch (flow->control.addr_type) {
566 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
567 diff -= sizeof(flow->addrs.v4addrs);
569 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
570 diff -= sizeof(flow->addrs.v6addrs);
572 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
573 diff -= sizeof(flow->addrs.tipcaddrs);
576 return (sizeof(*flow) - diff) / sizeof(u32);
579 __be32 flow_get_u32_src(const struct flow_keys *flow)
581 switch (flow->control.addr_type) {
582 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
583 return flow->addrs.v4addrs.src;
584 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
585 return (__force __be32)ipv6_addr_hash(
586 &flow->addrs.v6addrs.src);
587 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
588 return flow->addrs.tipcaddrs.srcnode;
593 EXPORT_SYMBOL(flow_get_u32_src);
595 __be32 flow_get_u32_dst(const struct flow_keys *flow)
597 switch (flow->control.addr_type) {
598 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
599 return flow->addrs.v4addrs.dst;
600 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
601 return (__force __be32)ipv6_addr_hash(
602 &flow->addrs.v6addrs.dst);
607 EXPORT_SYMBOL(flow_get_u32_dst);
609 static inline void __flow_hash_consistentify(struct flow_keys *keys)
613 switch (keys->control.addr_type) {
614 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
615 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
616 (__force u32)keys->addrs.v4addrs.src;
617 if ((addr_diff < 0) ||
619 ((__force u16)keys->ports.dst <
620 (__force u16)keys->ports.src))) {
621 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
622 swap(keys->ports.src, keys->ports.dst);
625 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
626 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
627 &keys->addrs.v6addrs.src,
628 sizeof(keys->addrs.v6addrs.dst));
629 if ((addr_diff < 0) ||
631 ((__force u16)keys->ports.dst <
632 (__force u16)keys->ports.src))) {
633 for (i = 0; i < 4; i++)
634 swap(keys->addrs.v6addrs.src.s6_addr32[i],
635 keys->addrs.v6addrs.dst.s6_addr32[i]);
636 swap(keys->ports.src, keys->ports.dst);
642 static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
646 __flow_hash_consistentify(keys);
648 hash = __flow_hash_words(flow_keys_hash_start(keys),
649 flow_keys_hash_length(keys), keyval);
656 u32 flow_hash_from_keys(struct flow_keys *keys)
658 __flow_hash_secret_init();
659 return __flow_hash_from_keys(keys, hashrnd);
661 EXPORT_SYMBOL(flow_hash_from_keys);
663 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
664 struct flow_keys *keys, u32 keyval)
666 skb_flow_dissect_flow_keys(skb, keys,
667 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
669 return __flow_hash_from_keys(keys, keyval);
672 struct _flow_keys_digest_data {
681 void make_flow_keys_digest(struct flow_keys_digest *digest,
682 const struct flow_keys *flow)
684 struct _flow_keys_digest_data *data =
685 (struct _flow_keys_digest_data *)digest;
687 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
689 memset(digest, 0, sizeof(*digest));
691 data->n_proto = flow->basic.n_proto;
692 data->ip_proto = flow->basic.ip_proto;
693 data->ports = flow->ports.ports;
694 data->src = flow->addrs.v4addrs.src;
695 data->dst = flow->addrs.v4addrs.dst;
697 EXPORT_SYMBOL(make_flow_keys_digest);
699 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
701 u32 __skb_get_hash_symmetric(struct sk_buff *skb)
703 struct flow_keys keys;
705 __flow_hash_secret_init();
707 memset(&keys, 0, sizeof(keys));
708 __skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys,
710 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
712 return __flow_hash_from_keys(&keys, hashrnd);
714 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
717 * __skb_get_hash: calculate a flow hash
718 * @skb: sk_buff to calculate flow hash from
720 * This function calculates a flow hash based on src/dst addresses
721 * and src/dst port numbers. Sets hash in skb to non-zero hash value
722 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
723 * if hash is a canonical 4-tuple hash over transport ports.
725 void __skb_get_hash(struct sk_buff *skb)
727 struct flow_keys keys;
729 __flow_hash_secret_init();
731 __skb_set_sw_hash(skb, ___skb_get_hash(skb, &keys, hashrnd),
732 flow_keys_have_l4(&keys));
734 EXPORT_SYMBOL(__skb_get_hash);
736 __u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
738 struct flow_keys keys;
740 return ___skb_get_hash(skb, &keys, perturb);
742 EXPORT_SYMBOL(skb_get_hash_perturb);
744 __u32 __skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6)
746 struct flow_keys keys;
748 memset(&keys, 0, sizeof(keys));
750 memcpy(&keys.addrs.v6addrs.src, &fl6->saddr,
751 sizeof(keys.addrs.v6addrs.src));
752 memcpy(&keys.addrs.v6addrs.dst, &fl6->daddr,
753 sizeof(keys.addrs.v6addrs.dst));
754 keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
755 keys.ports.src = fl6->fl6_sport;
756 keys.ports.dst = fl6->fl6_dport;
757 keys.keyid.keyid = fl6->fl6_gre_key;
758 keys.tags.flow_label = (__force u32)fl6->flowlabel;
759 keys.basic.ip_proto = fl6->flowi6_proto;
761 __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
762 flow_keys_have_l4(&keys));
766 EXPORT_SYMBOL(__skb_get_hash_flowi6);
768 __u32 __skb_get_hash_flowi4(struct sk_buff *skb, const struct flowi4 *fl4)
770 struct flow_keys keys;
772 memset(&keys, 0, sizeof(keys));
774 keys.addrs.v4addrs.src = fl4->saddr;
775 keys.addrs.v4addrs.dst = fl4->daddr;
776 keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
777 keys.ports.src = fl4->fl4_sport;
778 keys.ports.dst = fl4->fl4_dport;
779 keys.keyid.keyid = fl4->fl4_gre_key;
780 keys.basic.ip_proto = fl4->flowi4_proto;
782 __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
783 flow_keys_have_l4(&keys));
787 EXPORT_SYMBOL(__skb_get_hash_flowi4);
789 u32 __skb_get_poff(const struct sk_buff *skb, void *data,
790 const struct flow_keys *keys, int hlen)
792 u32 poff = keys->control.thoff;
794 /* skip L4 headers for fragments after the first */
795 if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
796 !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
799 switch (keys->basic.ip_proto) {
801 /* access doff as u8 to avoid unaligned access */
805 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
810 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
814 case IPPROTO_UDPLITE:
815 poff += sizeof(struct udphdr);
817 /* For the rest, we do not really care about header
818 * extensions at this point for now.
821 poff += sizeof(struct icmphdr);
824 poff += sizeof(struct icmp6hdr);
827 poff += sizeof(struct igmphdr);
830 poff += sizeof(struct dccp_hdr);
833 poff += sizeof(struct sctphdr);
841 * skb_get_poff - get the offset to the payload
842 * @skb: sk_buff to get the payload offset from
844 * The function will get the offset to the payload as far as it could
845 * be dissected. The main user is currently BPF, so that we can dynamically
846 * truncate packets without needing to push actual payload to the user
847 * space and can analyze headers only, instead.
849 u32 skb_get_poff(const struct sk_buff *skb)
851 struct flow_keys keys;
853 if (!skb_flow_dissect_flow_keys(skb, &keys, 0))
856 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
859 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
861 memset(keys, 0, sizeof(*keys));
863 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
864 sizeof(keys->addrs.v6addrs.src));
865 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
866 sizeof(keys->addrs.v6addrs.dst));
867 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
868 keys->ports.src = fl6->fl6_sport;
869 keys->ports.dst = fl6->fl6_dport;
870 keys->keyid.keyid = fl6->fl6_gre_key;
871 keys->tags.flow_label = (__force u32)fl6->flowlabel;
872 keys->basic.ip_proto = fl6->flowi6_proto;
874 return flow_hash_from_keys(keys);
876 EXPORT_SYMBOL(__get_hash_from_flowi6);
878 __u32 __get_hash_from_flowi4(const struct flowi4 *fl4, struct flow_keys *keys)
880 memset(keys, 0, sizeof(*keys));
882 keys->addrs.v4addrs.src = fl4->saddr;
883 keys->addrs.v4addrs.dst = fl4->daddr;
884 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
885 keys->ports.src = fl4->fl4_sport;
886 keys->ports.dst = fl4->fl4_dport;
887 keys->keyid.keyid = fl4->fl4_gre_key;
888 keys->basic.ip_proto = fl4->flowi4_proto;
890 return flow_hash_from_keys(keys);
892 EXPORT_SYMBOL(__get_hash_from_flowi4);
894 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
896 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
897 .offset = offsetof(struct flow_keys, control),
900 .key_id = FLOW_DISSECTOR_KEY_BASIC,
901 .offset = offsetof(struct flow_keys, basic),
904 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
905 .offset = offsetof(struct flow_keys, addrs.v4addrs),
908 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
909 .offset = offsetof(struct flow_keys, addrs.v6addrs),
912 .key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS,
913 .offset = offsetof(struct flow_keys, addrs.tipcaddrs),
916 .key_id = FLOW_DISSECTOR_KEY_PORTS,
917 .offset = offsetof(struct flow_keys, ports),
920 .key_id = FLOW_DISSECTOR_KEY_VLANID,
921 .offset = offsetof(struct flow_keys, tags),
924 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
925 .offset = offsetof(struct flow_keys, tags),
928 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
929 .offset = offsetof(struct flow_keys, keyid),
933 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
935 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
936 .offset = offsetof(struct flow_keys, control),
939 .key_id = FLOW_DISSECTOR_KEY_BASIC,
940 .offset = offsetof(struct flow_keys, basic),
943 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
944 .offset = offsetof(struct flow_keys, addrs.v4addrs),
947 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
948 .offset = offsetof(struct flow_keys, addrs.v6addrs),
951 .key_id = FLOW_DISSECTOR_KEY_PORTS,
952 .offset = offsetof(struct flow_keys, ports),
956 static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = {
958 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
959 .offset = offsetof(struct flow_keys, control),
962 .key_id = FLOW_DISSECTOR_KEY_BASIC,
963 .offset = offsetof(struct flow_keys, basic),
967 struct flow_dissector flow_keys_dissector __read_mostly;
968 EXPORT_SYMBOL(flow_keys_dissector);
970 struct flow_dissector flow_keys_buf_dissector __read_mostly;
972 static int __init init_default_flow_dissectors(void)
974 skb_flow_dissector_init(&flow_keys_dissector,
975 flow_keys_dissector_keys,
976 ARRAY_SIZE(flow_keys_dissector_keys));
977 skb_flow_dissector_init(&flow_keys_dissector_symmetric,
978 flow_keys_dissector_symmetric_keys,
979 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
980 skb_flow_dissector_init(&flow_keys_buf_dissector,
981 flow_keys_buf_dissector_keys,
982 ARRAY_SIZE(flow_keys_buf_dissector_keys));
986 late_initcall_sync(init_default_flow_dissectors);