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_vlan *key_vlan;
122 struct flow_dissector_key_keyid *key_keyid;
123 bool skip_vlan = false;
129 proto = skb_vlan_tag_present(skb) ?
130 skb->vlan_proto : skb->protocol;
131 nhoff = skb_network_offset(skb);
132 hlen = skb_headlen(skb);
135 /* It is ensured by skb_flow_dissector_init() that control key will
138 key_control = skb_flow_dissector_target(flow_dissector,
139 FLOW_DISSECTOR_KEY_CONTROL,
142 /* It is ensured by skb_flow_dissector_init() that basic key will
145 key_basic = skb_flow_dissector_target(flow_dissector,
146 FLOW_DISSECTOR_KEY_BASIC,
149 if (dissector_uses_key(flow_dissector,
150 FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
151 struct ethhdr *eth = eth_hdr(skb);
152 struct flow_dissector_key_eth_addrs *key_eth_addrs;
154 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
155 FLOW_DISSECTOR_KEY_ETH_ADDRS,
157 memcpy(key_eth_addrs, ð->h_dest, sizeof(*key_eth_addrs));
162 case htons(ETH_P_IP): {
163 const struct iphdr *iph;
166 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
167 if (!iph || iph->ihl < 5)
169 nhoff += iph->ihl * 4;
171 ip_proto = iph->protocol;
173 if (dissector_uses_key(flow_dissector,
174 FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
175 key_addrs = skb_flow_dissector_target(flow_dissector,
176 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
179 memcpy(&key_addrs->v4addrs, &iph->saddr,
180 sizeof(key_addrs->v4addrs));
181 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
184 if (ip_is_fragment(iph)) {
185 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
187 if (iph->frag_off & htons(IP_OFFSET)) {
190 key_control->flags |= FLOW_DIS_FIRST_FRAG;
191 if (!(flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG))
196 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
201 case htons(ETH_P_IPV6): {
202 const struct ipv6hdr *iph;
206 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
210 ip_proto = iph->nexthdr;
211 nhoff += sizeof(struct ipv6hdr);
213 if (dissector_uses_key(flow_dissector,
214 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
215 key_addrs = skb_flow_dissector_target(flow_dissector,
216 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
219 memcpy(&key_addrs->v6addrs, &iph->saddr,
220 sizeof(key_addrs->v6addrs));
221 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
224 if ((dissector_uses_key(flow_dissector,
225 FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
226 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
227 ip6_flowlabel(iph)) {
228 __be32 flow_label = ip6_flowlabel(iph);
230 if (dissector_uses_key(flow_dissector,
231 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
232 key_tags = skb_flow_dissector_target(flow_dissector,
233 FLOW_DISSECTOR_KEY_FLOW_LABEL,
235 key_tags->flow_label = ntohl(flow_label);
237 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)
241 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
246 case htons(ETH_P_8021AD):
247 case htons(ETH_P_8021Q): {
248 const struct vlan_hdr *vlan;
249 struct vlan_hdr _vlan;
250 bool vlan_tag_present = skb && skb_vlan_tag_present(skb);
252 if (vlan_tag_present)
253 proto = skb->protocol;
255 if (!vlan_tag_present || eth_type_vlan(skb->protocol)) {
256 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
260 proto = vlan->h_vlan_encapsulated_proto;
261 nhoff += sizeof(*vlan);
267 if (dissector_uses_key(flow_dissector,
268 FLOW_DISSECTOR_KEY_VLAN)) {
269 key_vlan = skb_flow_dissector_target(flow_dissector,
270 FLOW_DISSECTOR_KEY_VLAN,
273 if (vlan_tag_present) {
274 key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
275 key_vlan->vlan_priority =
276 (skb_vlan_tag_get_prio(skb) >> VLAN_PRIO_SHIFT);
278 key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) &
280 key_vlan->vlan_priority =
281 (ntohs(vlan->h_vlan_TCI) &
282 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
288 case htons(ETH_P_PPP_SES): {
290 struct pppoe_hdr hdr;
293 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
297 nhoff += PPPOE_SES_HLEN;
301 case htons(PPP_IPV6):
307 case htons(ETH_P_TIPC): {
312 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
316 if (dissector_uses_key(flow_dissector,
317 FLOW_DISSECTOR_KEY_TIPC_ADDRS)) {
318 key_addrs = skb_flow_dissector_target(flow_dissector,
319 FLOW_DISSECTOR_KEY_TIPC_ADDRS,
321 key_addrs->tipcaddrs.srcnode = hdr->srcnode;
322 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC_ADDRS;
327 case htons(ETH_P_MPLS_UC):
328 case htons(ETH_P_MPLS_MC): {
329 struct mpls_label *hdr, _hdr[2];
331 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
336 if ((ntohl(hdr[0].entry) & MPLS_LS_LABEL_MASK) >>
337 MPLS_LS_LABEL_SHIFT == MPLS_LABEL_ENTROPY) {
338 if (dissector_uses_key(flow_dissector,
339 FLOW_DISSECTOR_KEY_MPLS_ENTROPY)) {
340 key_keyid = skb_flow_dissector_target(flow_dissector,
341 FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
343 key_keyid->keyid = hdr[1].entry &
344 htonl(MPLS_LS_LABEL_MASK);
353 case htons(ETH_P_FCOE):
354 if ((hlen - nhoff) < FCOE_HEADER_LEN)
357 nhoff += FCOE_HEADER_LEN;
366 struct gre_base_hdr *hdr, _hdr;
370 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
374 /* Only look inside GRE without routing */
375 if (hdr->flags & GRE_ROUTING)
378 /* Only look inside GRE for version 0 and 1 */
379 gre_ver = ntohs(hdr->flags & GRE_VERSION);
383 proto = hdr->protocol;
385 /* Version1 must be PPTP, and check the flags */
386 if (!(proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY)))
390 offset += sizeof(struct gre_base_hdr);
392 if (hdr->flags & GRE_CSUM)
393 offset += sizeof(((struct gre_full_hdr *)0)->csum) +
394 sizeof(((struct gre_full_hdr *)0)->reserved1);
396 if (hdr->flags & GRE_KEY) {
400 keyid = __skb_header_pointer(skb, nhoff + offset, sizeof(_keyid),
401 data, hlen, &_keyid);
405 if (dissector_uses_key(flow_dissector,
406 FLOW_DISSECTOR_KEY_GRE_KEYID)) {
407 key_keyid = skb_flow_dissector_target(flow_dissector,
408 FLOW_DISSECTOR_KEY_GRE_KEYID,
411 key_keyid->keyid = *keyid;
413 key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK;
415 offset += sizeof(((struct gre_full_hdr *)0)->key);
418 if (hdr->flags & GRE_SEQ)
419 offset += sizeof(((struct pptp_gre_header *)0)->seq);
422 if (proto == htons(ETH_P_TEB)) {
423 const struct ethhdr *eth;
426 eth = __skb_header_pointer(skb, nhoff + offset,
431 proto = eth->h_proto;
432 offset += sizeof(*eth);
434 /* Cap headers that we access via pointers at the
435 * end of the Ethernet header as our maximum alignment
436 * at that point is only 2 bytes.
439 hlen = (nhoff + offset);
441 } else { /* version 1, must be PPTP */
442 u8 _ppp_hdr[PPP_HDRLEN];
445 if (hdr->flags & GRE_ACK)
446 offset += sizeof(((struct pptp_gre_header *)0)->ack);
448 ppp_hdr = skb_header_pointer(skb, nhoff + offset,
449 sizeof(_ppp_hdr), _ppp_hdr);
453 switch (PPP_PROTOCOL(ppp_hdr)) {
455 proto = htons(ETH_P_IP);
458 proto = htons(ETH_P_IPV6);
461 /* Could probably catch some more like MPLS */
465 offset += PPP_HDRLEN;
469 key_control->flags |= FLOW_DIS_ENCAPSULATION;
470 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
476 case NEXTHDR_ROUTING:
478 u8 _opthdr[2], *opthdr;
480 if (proto != htons(ETH_P_IPV6))
483 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
484 data, hlen, &_opthdr);
488 ip_proto = opthdr[0];
489 nhoff += (opthdr[1] + 1) << 3;
493 case NEXTHDR_FRAGMENT: {
494 struct frag_hdr _fh, *fh;
496 if (proto != htons(ETH_P_IPV6))
499 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
505 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
507 nhoff += sizeof(_fh);
508 ip_proto = fh->nexthdr;
510 if (!(fh->frag_off & htons(IP6_OFFSET))) {
511 key_control->flags |= FLOW_DIS_FIRST_FRAG;
512 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG)
518 proto = htons(ETH_P_IP);
520 key_control->flags |= FLOW_DIS_ENCAPSULATION;
521 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
526 proto = htons(ETH_P_IPV6);
528 key_control->flags |= FLOW_DIS_ENCAPSULATION;
529 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
534 proto = htons(ETH_P_MPLS_UC);
540 if (dissector_uses_key(flow_dissector,
541 FLOW_DISSECTOR_KEY_PORTS)) {
542 key_ports = skb_flow_dissector_target(flow_dissector,
543 FLOW_DISSECTOR_KEY_PORTS,
545 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
552 key_control->thoff = (u16)nhoff;
554 key_basic->n_proto = proto;
555 key_basic->ip_proto = ip_proto;
561 key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
564 EXPORT_SYMBOL(__skb_flow_dissect);
566 static u32 hashrnd __read_mostly;
567 static __always_inline void __flow_hash_secret_init(void)
569 net_get_random_once(&hashrnd, sizeof(hashrnd));
572 static __always_inline u32 __flow_hash_words(const u32 *words, u32 length,
575 return jhash2(words, length, keyval);
578 static inline const u32 *flow_keys_hash_start(const struct flow_keys *flow)
580 const void *p = flow;
582 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32));
583 return (const u32 *)(p + FLOW_KEYS_HASH_OFFSET);
586 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
588 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
589 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
590 BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
591 sizeof(*flow) - sizeof(flow->addrs));
593 switch (flow->control.addr_type) {
594 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
595 diff -= sizeof(flow->addrs.v4addrs);
597 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
598 diff -= sizeof(flow->addrs.v6addrs);
600 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
601 diff -= sizeof(flow->addrs.tipcaddrs);
604 return (sizeof(*flow) - diff) / sizeof(u32);
607 __be32 flow_get_u32_src(const struct flow_keys *flow)
609 switch (flow->control.addr_type) {
610 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
611 return flow->addrs.v4addrs.src;
612 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
613 return (__force __be32)ipv6_addr_hash(
614 &flow->addrs.v6addrs.src);
615 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
616 return flow->addrs.tipcaddrs.srcnode;
621 EXPORT_SYMBOL(flow_get_u32_src);
623 __be32 flow_get_u32_dst(const struct flow_keys *flow)
625 switch (flow->control.addr_type) {
626 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
627 return flow->addrs.v4addrs.dst;
628 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
629 return (__force __be32)ipv6_addr_hash(
630 &flow->addrs.v6addrs.dst);
635 EXPORT_SYMBOL(flow_get_u32_dst);
637 static inline void __flow_hash_consistentify(struct flow_keys *keys)
641 switch (keys->control.addr_type) {
642 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
643 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
644 (__force u32)keys->addrs.v4addrs.src;
645 if ((addr_diff < 0) ||
647 ((__force u16)keys->ports.dst <
648 (__force u16)keys->ports.src))) {
649 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
650 swap(keys->ports.src, keys->ports.dst);
653 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
654 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
655 &keys->addrs.v6addrs.src,
656 sizeof(keys->addrs.v6addrs.dst));
657 if ((addr_diff < 0) ||
659 ((__force u16)keys->ports.dst <
660 (__force u16)keys->ports.src))) {
661 for (i = 0; i < 4; i++)
662 swap(keys->addrs.v6addrs.src.s6_addr32[i],
663 keys->addrs.v6addrs.dst.s6_addr32[i]);
664 swap(keys->ports.src, keys->ports.dst);
670 static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
674 __flow_hash_consistentify(keys);
676 hash = __flow_hash_words(flow_keys_hash_start(keys),
677 flow_keys_hash_length(keys), keyval);
684 u32 flow_hash_from_keys(struct flow_keys *keys)
686 __flow_hash_secret_init();
687 return __flow_hash_from_keys(keys, hashrnd);
689 EXPORT_SYMBOL(flow_hash_from_keys);
691 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
692 struct flow_keys *keys, u32 keyval)
694 skb_flow_dissect_flow_keys(skb, keys,
695 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
697 return __flow_hash_from_keys(keys, keyval);
700 struct _flow_keys_digest_data {
709 void make_flow_keys_digest(struct flow_keys_digest *digest,
710 const struct flow_keys *flow)
712 struct _flow_keys_digest_data *data =
713 (struct _flow_keys_digest_data *)digest;
715 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
717 memset(digest, 0, sizeof(*digest));
719 data->n_proto = flow->basic.n_proto;
720 data->ip_proto = flow->basic.ip_proto;
721 data->ports = flow->ports.ports;
722 data->src = flow->addrs.v4addrs.src;
723 data->dst = flow->addrs.v4addrs.dst;
725 EXPORT_SYMBOL(make_flow_keys_digest);
727 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
729 u32 __skb_get_hash_symmetric(const struct sk_buff *skb)
731 struct flow_keys keys;
733 __flow_hash_secret_init();
735 memset(&keys, 0, sizeof(keys));
736 __skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys,
738 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
740 return __flow_hash_from_keys(&keys, hashrnd);
742 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
745 * __skb_get_hash: calculate a flow hash
746 * @skb: sk_buff to calculate flow hash from
748 * This function calculates a flow hash based on src/dst addresses
749 * and src/dst port numbers. Sets hash in skb to non-zero hash value
750 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
751 * if hash is a canonical 4-tuple hash over transport ports.
753 void __skb_get_hash(struct sk_buff *skb)
755 struct flow_keys keys;
758 __flow_hash_secret_init();
760 hash = ___skb_get_hash(skb, &keys, hashrnd);
762 __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
764 EXPORT_SYMBOL(__skb_get_hash);
766 __u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
768 struct flow_keys keys;
770 return ___skb_get_hash(skb, &keys, perturb);
772 EXPORT_SYMBOL(skb_get_hash_perturb);
774 __u32 __skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6)
776 struct flow_keys keys;
778 memset(&keys, 0, sizeof(keys));
780 memcpy(&keys.addrs.v6addrs.src, &fl6->saddr,
781 sizeof(keys.addrs.v6addrs.src));
782 memcpy(&keys.addrs.v6addrs.dst, &fl6->daddr,
783 sizeof(keys.addrs.v6addrs.dst));
784 keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
785 keys.ports.src = fl6->fl6_sport;
786 keys.ports.dst = fl6->fl6_dport;
787 keys.keyid.keyid = fl6->fl6_gre_key;
788 keys.tags.flow_label = (__force u32)fl6->flowlabel;
789 keys.basic.ip_proto = fl6->flowi6_proto;
791 __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
792 flow_keys_have_l4(&keys));
796 EXPORT_SYMBOL(__skb_get_hash_flowi6);
798 __u32 __skb_get_hash_flowi4(struct sk_buff *skb, const struct flowi4 *fl4)
800 struct flow_keys keys;
802 memset(&keys, 0, sizeof(keys));
804 keys.addrs.v4addrs.src = fl4->saddr;
805 keys.addrs.v4addrs.dst = fl4->daddr;
806 keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
807 keys.ports.src = fl4->fl4_sport;
808 keys.ports.dst = fl4->fl4_dport;
809 keys.keyid.keyid = fl4->fl4_gre_key;
810 keys.basic.ip_proto = fl4->flowi4_proto;
812 __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
813 flow_keys_have_l4(&keys));
817 EXPORT_SYMBOL(__skb_get_hash_flowi4);
819 u32 __skb_get_poff(const struct sk_buff *skb, void *data,
820 const struct flow_keys *keys, int hlen)
822 u32 poff = keys->control.thoff;
824 /* skip L4 headers for fragments after the first */
825 if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
826 !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
829 switch (keys->basic.ip_proto) {
831 /* access doff as u8 to avoid unaligned access */
835 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
840 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
844 case IPPROTO_UDPLITE:
845 poff += sizeof(struct udphdr);
847 /* For the rest, we do not really care about header
848 * extensions at this point for now.
851 poff += sizeof(struct icmphdr);
854 poff += sizeof(struct icmp6hdr);
857 poff += sizeof(struct igmphdr);
860 poff += sizeof(struct dccp_hdr);
863 poff += sizeof(struct sctphdr);
871 * skb_get_poff - get the offset to the payload
872 * @skb: sk_buff to get the payload offset from
874 * The function will get the offset to the payload as far as it could
875 * be dissected. The main user is currently BPF, so that we can dynamically
876 * truncate packets without needing to push actual payload to the user
877 * space and can analyze headers only, instead.
879 u32 skb_get_poff(const struct sk_buff *skb)
881 struct flow_keys keys;
883 if (!skb_flow_dissect_flow_keys(skb, &keys, 0))
886 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
889 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
891 memset(keys, 0, sizeof(*keys));
893 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
894 sizeof(keys->addrs.v6addrs.src));
895 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
896 sizeof(keys->addrs.v6addrs.dst));
897 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
898 keys->ports.src = fl6->fl6_sport;
899 keys->ports.dst = fl6->fl6_dport;
900 keys->keyid.keyid = fl6->fl6_gre_key;
901 keys->tags.flow_label = (__force u32)fl6->flowlabel;
902 keys->basic.ip_proto = fl6->flowi6_proto;
904 return flow_hash_from_keys(keys);
906 EXPORT_SYMBOL(__get_hash_from_flowi6);
908 __u32 __get_hash_from_flowi4(const struct flowi4 *fl4, struct flow_keys *keys)
910 memset(keys, 0, sizeof(*keys));
912 keys->addrs.v4addrs.src = fl4->saddr;
913 keys->addrs.v4addrs.dst = fl4->daddr;
914 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
915 keys->ports.src = fl4->fl4_sport;
916 keys->ports.dst = fl4->fl4_dport;
917 keys->keyid.keyid = fl4->fl4_gre_key;
918 keys->basic.ip_proto = fl4->flowi4_proto;
920 return flow_hash_from_keys(keys);
922 EXPORT_SYMBOL(__get_hash_from_flowi4);
924 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
926 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
927 .offset = offsetof(struct flow_keys, control),
930 .key_id = FLOW_DISSECTOR_KEY_BASIC,
931 .offset = offsetof(struct flow_keys, basic),
934 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
935 .offset = offsetof(struct flow_keys, addrs.v4addrs),
938 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
939 .offset = offsetof(struct flow_keys, addrs.v6addrs),
942 .key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS,
943 .offset = offsetof(struct flow_keys, addrs.tipcaddrs),
946 .key_id = FLOW_DISSECTOR_KEY_PORTS,
947 .offset = offsetof(struct flow_keys, ports),
950 .key_id = FLOW_DISSECTOR_KEY_VLAN,
951 .offset = offsetof(struct flow_keys, vlan),
954 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
955 .offset = offsetof(struct flow_keys, tags),
958 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
959 .offset = offsetof(struct flow_keys, keyid),
963 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
965 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
966 .offset = offsetof(struct flow_keys, control),
969 .key_id = FLOW_DISSECTOR_KEY_BASIC,
970 .offset = offsetof(struct flow_keys, basic),
973 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
974 .offset = offsetof(struct flow_keys, addrs.v4addrs),
977 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
978 .offset = offsetof(struct flow_keys, addrs.v6addrs),
981 .key_id = FLOW_DISSECTOR_KEY_PORTS,
982 .offset = offsetof(struct flow_keys, ports),
986 static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = {
988 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
989 .offset = offsetof(struct flow_keys, control),
992 .key_id = FLOW_DISSECTOR_KEY_BASIC,
993 .offset = offsetof(struct flow_keys, basic),
997 struct flow_dissector flow_keys_dissector __read_mostly;
998 EXPORT_SYMBOL(flow_keys_dissector);
1000 struct flow_dissector flow_keys_buf_dissector __read_mostly;
1002 static int __init init_default_flow_dissectors(void)
1004 skb_flow_dissector_init(&flow_keys_dissector,
1005 flow_keys_dissector_keys,
1006 ARRAY_SIZE(flow_keys_dissector_keys));
1007 skb_flow_dissector_init(&flow_keys_dissector_symmetric,
1008 flow_keys_dissector_symmetric_keys,
1009 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
1010 skb_flow_dissector_init(&flow_keys_buf_dissector,
1011 flow_keys_buf_dissector_keys,
1012 ARRAY_SIZE(flow_keys_buf_dissector_keys));
1016 core_initcall(init_default_flow_dissectors);