Linux 4.19.133
[linux/fpc-iii.git] / net / core / flow_dissector.c
blob994dd1520f07a4ecc4f3ad8601b085d80b87c3ec
1 #include <linux/kernel.h>
2 #include <linux/skbuff.h>
3 #include <linux/export.h>
4 #include <linux/ip.h>
5 #include <linux/ipv6.h>
6 #include <linux/if_vlan.h>
7 #include <net/dsa.h>
8 #include <net/dst_metadata.h>
9 #include <net/ip.h>
10 #include <net/ipv6.h>
11 #include <net/gre.h>
12 #include <net/pptp.h>
13 #include <net/tipc.h>
14 #include <linux/igmp.h>
15 #include <linux/icmp.h>
16 #include <linux/sctp.h>
17 #include <linux/dccp.h>
18 #include <linux/if_tunnel.h>
19 #include <linux/if_pppox.h>
20 #include <linux/ppp_defs.h>
21 #include <linux/stddef.h>
22 #include <linux/if_ether.h>
23 #include <linux/mpls.h>
24 #include <linux/tcp.h>
25 #include <net/flow_dissector.h>
26 #include <scsi/fc/fc_fcoe.h>
27 #include <uapi/linux/batadv_packet.h>
29 static void dissector_set_key(struct flow_dissector *flow_dissector,
30 enum flow_dissector_key_id key_id)
32 flow_dissector->used_keys |= (1 << key_id);
35 void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
36 const struct flow_dissector_key *key,
37 unsigned int key_count)
39 unsigned int i;
41 memset(flow_dissector, 0, sizeof(*flow_dissector));
43 for (i = 0; i < key_count; i++, key++) {
44 /* User should make sure that every key target offset is withing
45 * boundaries of unsigned short.
47 BUG_ON(key->offset > USHRT_MAX);
48 BUG_ON(dissector_uses_key(flow_dissector,
49 key->key_id));
51 dissector_set_key(flow_dissector, key->key_id);
52 flow_dissector->offset[key->key_id] = key->offset;
55 /* Ensure that the dissector always includes control and basic key.
56 * That way we are able to avoid handling lack of these in fast path.
58 BUG_ON(!dissector_uses_key(flow_dissector,
59 FLOW_DISSECTOR_KEY_CONTROL));
60 BUG_ON(!dissector_uses_key(flow_dissector,
61 FLOW_DISSECTOR_KEY_BASIC));
63 EXPORT_SYMBOL(skb_flow_dissector_init);
65 /**
66 * skb_flow_get_be16 - extract be16 entity
67 * @skb: sk_buff to extract from
68 * @poff: offset to extract at
69 * @data: raw buffer pointer to the packet
70 * @hlen: packet header length
72 * The function will try to retrieve a be32 entity at
73 * offset poff
75 static __be16 skb_flow_get_be16(const struct sk_buff *skb, int poff,
76 void *data, int hlen)
78 __be16 *u, _u;
80 u = __skb_header_pointer(skb, poff, sizeof(_u), data, hlen, &_u);
81 if (u)
82 return *u;
84 return 0;
87 /**
88 * __skb_flow_get_ports - extract the upper layer ports and return them
89 * @skb: sk_buff to extract the ports from
90 * @thoff: transport header offset
91 * @ip_proto: protocol for which to get port offset
92 * @data: raw buffer pointer to the packet, if NULL use skb->data
93 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
95 * The function will try to retrieve the ports at offset thoff + poff where poff
96 * is the protocol port offset returned from proto_ports_offset
98 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
99 void *data, int hlen)
101 int poff = proto_ports_offset(ip_proto);
103 if (!data) {
104 data = skb->data;
105 hlen = skb_headlen(skb);
108 if (poff >= 0) {
109 __be32 *ports, _ports;
111 ports = __skb_header_pointer(skb, thoff + poff,
112 sizeof(_ports), data, hlen, &_ports);
113 if (ports)
114 return *ports;
117 return 0;
119 EXPORT_SYMBOL(__skb_flow_get_ports);
121 static void
122 skb_flow_dissect_set_enc_addr_type(enum flow_dissector_key_id type,
123 struct flow_dissector *flow_dissector,
124 void *target_container)
126 struct flow_dissector_key_control *ctrl;
128 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL))
129 return;
131 ctrl = skb_flow_dissector_target(flow_dissector,
132 FLOW_DISSECTOR_KEY_ENC_CONTROL,
133 target_container);
134 ctrl->addr_type = type;
137 void
138 skb_flow_dissect_tunnel_info(const struct sk_buff *skb,
139 struct flow_dissector *flow_dissector,
140 void *target_container)
142 struct ip_tunnel_info *info;
143 struct ip_tunnel_key *key;
145 /* A quick check to see if there might be something to do. */
146 if (!dissector_uses_key(flow_dissector,
147 FLOW_DISSECTOR_KEY_ENC_KEYID) &&
148 !dissector_uses_key(flow_dissector,
149 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) &&
150 !dissector_uses_key(flow_dissector,
151 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) &&
152 !dissector_uses_key(flow_dissector,
153 FLOW_DISSECTOR_KEY_ENC_CONTROL) &&
154 !dissector_uses_key(flow_dissector,
155 FLOW_DISSECTOR_KEY_ENC_PORTS) &&
156 !dissector_uses_key(flow_dissector,
157 FLOW_DISSECTOR_KEY_ENC_IP) &&
158 !dissector_uses_key(flow_dissector,
159 FLOW_DISSECTOR_KEY_ENC_OPTS))
160 return;
162 info = skb_tunnel_info(skb);
163 if (!info)
164 return;
166 key = &info->key;
168 switch (ip_tunnel_info_af(info)) {
169 case AF_INET:
170 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV4_ADDRS,
171 flow_dissector,
172 target_container);
173 if (dissector_uses_key(flow_dissector,
174 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) {
175 struct flow_dissector_key_ipv4_addrs *ipv4;
177 ipv4 = skb_flow_dissector_target(flow_dissector,
178 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
179 target_container);
180 ipv4->src = key->u.ipv4.src;
181 ipv4->dst = key->u.ipv4.dst;
183 break;
184 case AF_INET6:
185 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV6_ADDRS,
186 flow_dissector,
187 target_container);
188 if (dissector_uses_key(flow_dissector,
189 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS)) {
190 struct flow_dissector_key_ipv6_addrs *ipv6;
192 ipv6 = skb_flow_dissector_target(flow_dissector,
193 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
194 target_container);
195 ipv6->src = key->u.ipv6.src;
196 ipv6->dst = key->u.ipv6.dst;
198 break;
201 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
202 struct flow_dissector_key_keyid *keyid;
204 keyid = skb_flow_dissector_target(flow_dissector,
205 FLOW_DISSECTOR_KEY_ENC_KEYID,
206 target_container);
207 keyid->keyid = tunnel_id_to_key32(key->tun_id);
210 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
211 struct flow_dissector_key_ports *tp;
213 tp = skb_flow_dissector_target(flow_dissector,
214 FLOW_DISSECTOR_KEY_ENC_PORTS,
215 target_container);
216 tp->src = key->tp_src;
217 tp->dst = key->tp_dst;
220 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_IP)) {
221 struct flow_dissector_key_ip *ip;
223 ip = skb_flow_dissector_target(flow_dissector,
224 FLOW_DISSECTOR_KEY_ENC_IP,
225 target_container);
226 ip->tos = key->tos;
227 ip->ttl = key->ttl;
230 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_OPTS)) {
231 struct flow_dissector_key_enc_opts *enc_opt;
233 enc_opt = skb_flow_dissector_target(flow_dissector,
234 FLOW_DISSECTOR_KEY_ENC_OPTS,
235 target_container);
237 if (info->options_len) {
238 enc_opt->len = info->options_len;
239 ip_tunnel_info_opts_get(enc_opt->data, info);
240 enc_opt->dst_opt_type = info->key.tun_flags &
241 TUNNEL_OPTIONS_PRESENT;
245 EXPORT_SYMBOL(skb_flow_dissect_tunnel_info);
247 static enum flow_dissect_ret
248 __skb_flow_dissect_mpls(const struct sk_buff *skb,
249 struct flow_dissector *flow_dissector,
250 void *target_container, void *data, int nhoff, int hlen)
252 struct flow_dissector_key_keyid *key_keyid;
253 struct mpls_label *hdr, _hdr[2];
254 u32 entry, label;
256 if (!dissector_uses_key(flow_dissector,
257 FLOW_DISSECTOR_KEY_MPLS_ENTROPY) &&
258 !dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS))
259 return FLOW_DISSECT_RET_OUT_GOOD;
261 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
262 hlen, &_hdr);
263 if (!hdr)
264 return FLOW_DISSECT_RET_OUT_BAD;
266 entry = ntohl(hdr[0].entry);
267 label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
269 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) {
270 struct flow_dissector_key_mpls *key_mpls;
272 key_mpls = skb_flow_dissector_target(flow_dissector,
273 FLOW_DISSECTOR_KEY_MPLS,
274 target_container);
275 key_mpls->mpls_label = label;
276 key_mpls->mpls_ttl = (entry & MPLS_LS_TTL_MASK)
277 >> MPLS_LS_TTL_SHIFT;
278 key_mpls->mpls_tc = (entry & MPLS_LS_TC_MASK)
279 >> MPLS_LS_TC_SHIFT;
280 key_mpls->mpls_bos = (entry & MPLS_LS_S_MASK)
281 >> MPLS_LS_S_SHIFT;
284 if (label == MPLS_LABEL_ENTROPY) {
285 key_keyid = skb_flow_dissector_target(flow_dissector,
286 FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
287 target_container);
288 key_keyid->keyid = hdr[1].entry & htonl(MPLS_LS_LABEL_MASK);
290 return FLOW_DISSECT_RET_OUT_GOOD;
293 static enum flow_dissect_ret
294 __skb_flow_dissect_arp(const struct sk_buff *skb,
295 struct flow_dissector *flow_dissector,
296 void *target_container, void *data, int nhoff, int hlen)
298 struct flow_dissector_key_arp *key_arp;
299 struct {
300 unsigned char ar_sha[ETH_ALEN];
301 unsigned char ar_sip[4];
302 unsigned char ar_tha[ETH_ALEN];
303 unsigned char ar_tip[4];
304 } *arp_eth, _arp_eth;
305 const struct arphdr *arp;
306 struct arphdr _arp;
308 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ARP))
309 return FLOW_DISSECT_RET_OUT_GOOD;
311 arp = __skb_header_pointer(skb, nhoff, sizeof(_arp), data,
312 hlen, &_arp);
313 if (!arp)
314 return FLOW_DISSECT_RET_OUT_BAD;
316 if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
317 arp->ar_pro != htons(ETH_P_IP) ||
318 arp->ar_hln != ETH_ALEN ||
319 arp->ar_pln != 4 ||
320 (arp->ar_op != htons(ARPOP_REPLY) &&
321 arp->ar_op != htons(ARPOP_REQUEST)))
322 return FLOW_DISSECT_RET_OUT_BAD;
324 arp_eth = __skb_header_pointer(skb, nhoff + sizeof(_arp),
325 sizeof(_arp_eth), data,
326 hlen, &_arp_eth);
327 if (!arp_eth)
328 return FLOW_DISSECT_RET_OUT_BAD;
330 key_arp = skb_flow_dissector_target(flow_dissector,
331 FLOW_DISSECTOR_KEY_ARP,
332 target_container);
334 memcpy(&key_arp->sip, arp_eth->ar_sip, sizeof(key_arp->sip));
335 memcpy(&key_arp->tip, arp_eth->ar_tip, sizeof(key_arp->tip));
337 /* Only store the lower byte of the opcode;
338 * this covers ARPOP_REPLY and ARPOP_REQUEST.
340 key_arp->op = ntohs(arp->ar_op) & 0xff;
342 ether_addr_copy(key_arp->sha, arp_eth->ar_sha);
343 ether_addr_copy(key_arp->tha, arp_eth->ar_tha);
345 return FLOW_DISSECT_RET_OUT_GOOD;
348 static enum flow_dissect_ret
349 __skb_flow_dissect_gre(const struct sk_buff *skb,
350 struct flow_dissector_key_control *key_control,
351 struct flow_dissector *flow_dissector,
352 void *target_container, void *data,
353 __be16 *p_proto, int *p_nhoff, int *p_hlen,
354 unsigned int flags)
356 struct flow_dissector_key_keyid *key_keyid;
357 struct gre_base_hdr *hdr, _hdr;
358 int offset = 0;
359 u16 gre_ver;
361 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr),
362 data, *p_hlen, &_hdr);
363 if (!hdr)
364 return FLOW_DISSECT_RET_OUT_BAD;
366 /* Only look inside GRE without routing */
367 if (hdr->flags & GRE_ROUTING)
368 return FLOW_DISSECT_RET_OUT_GOOD;
370 /* Only look inside GRE for version 0 and 1 */
371 gre_ver = ntohs(hdr->flags & GRE_VERSION);
372 if (gre_ver > 1)
373 return FLOW_DISSECT_RET_OUT_GOOD;
375 *p_proto = hdr->protocol;
376 if (gre_ver) {
377 /* Version1 must be PPTP, and check the flags */
378 if (!(*p_proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY)))
379 return FLOW_DISSECT_RET_OUT_GOOD;
382 offset += sizeof(struct gre_base_hdr);
384 if (hdr->flags & GRE_CSUM)
385 offset += sizeof(((struct gre_full_hdr *) 0)->csum) +
386 sizeof(((struct gre_full_hdr *) 0)->reserved1);
388 if (hdr->flags & GRE_KEY) {
389 const __be32 *keyid;
390 __be32 _keyid;
392 keyid = __skb_header_pointer(skb, *p_nhoff + offset,
393 sizeof(_keyid),
394 data, *p_hlen, &_keyid);
395 if (!keyid)
396 return FLOW_DISSECT_RET_OUT_BAD;
398 if (dissector_uses_key(flow_dissector,
399 FLOW_DISSECTOR_KEY_GRE_KEYID)) {
400 key_keyid = skb_flow_dissector_target(flow_dissector,
401 FLOW_DISSECTOR_KEY_GRE_KEYID,
402 target_container);
403 if (gre_ver == 0)
404 key_keyid->keyid = *keyid;
405 else
406 key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK;
408 offset += sizeof(((struct gre_full_hdr *) 0)->key);
411 if (hdr->flags & GRE_SEQ)
412 offset += sizeof(((struct pptp_gre_header *) 0)->seq);
414 if (gre_ver == 0) {
415 if (*p_proto == htons(ETH_P_TEB)) {
416 const struct ethhdr *eth;
417 struct ethhdr _eth;
419 eth = __skb_header_pointer(skb, *p_nhoff + offset,
420 sizeof(_eth),
421 data, *p_hlen, &_eth);
422 if (!eth)
423 return FLOW_DISSECT_RET_OUT_BAD;
424 *p_proto = eth->h_proto;
425 offset += sizeof(*eth);
427 /* Cap headers that we access via pointers at the
428 * end of the Ethernet header as our maximum alignment
429 * at that point is only 2 bytes.
431 if (NET_IP_ALIGN)
432 *p_hlen = *p_nhoff + offset;
434 } else { /* version 1, must be PPTP */
435 u8 _ppp_hdr[PPP_HDRLEN];
436 u8 *ppp_hdr;
438 if (hdr->flags & GRE_ACK)
439 offset += sizeof(((struct pptp_gre_header *) 0)->ack);
441 ppp_hdr = __skb_header_pointer(skb, *p_nhoff + offset,
442 sizeof(_ppp_hdr),
443 data, *p_hlen, _ppp_hdr);
444 if (!ppp_hdr)
445 return FLOW_DISSECT_RET_OUT_BAD;
447 switch (PPP_PROTOCOL(ppp_hdr)) {
448 case PPP_IP:
449 *p_proto = htons(ETH_P_IP);
450 break;
451 case PPP_IPV6:
452 *p_proto = htons(ETH_P_IPV6);
453 break;
454 default:
455 /* Could probably catch some more like MPLS */
456 break;
459 offset += PPP_HDRLEN;
462 *p_nhoff += offset;
463 key_control->flags |= FLOW_DIS_ENCAPSULATION;
464 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
465 return FLOW_DISSECT_RET_OUT_GOOD;
467 return FLOW_DISSECT_RET_PROTO_AGAIN;
471 * __skb_flow_dissect_batadv() - dissect batman-adv header
472 * @skb: sk_buff to with the batman-adv header
473 * @key_control: flow dissectors control key
474 * @data: raw buffer pointer to the packet, if NULL use skb->data
475 * @p_proto: pointer used to update the protocol to process next
476 * @p_nhoff: pointer used to update inner network header offset
477 * @hlen: packet header length
478 * @flags: any combination of FLOW_DISSECTOR_F_*
480 * ETH_P_BATMAN packets are tried to be dissected. Only
481 * &struct batadv_unicast packets are actually processed because they contain an
482 * inner ethernet header and are usually followed by actual network header. This
483 * allows the flow dissector to continue processing the packet.
485 * Return: FLOW_DISSECT_RET_PROTO_AGAIN when &struct batadv_unicast was found,
486 * FLOW_DISSECT_RET_OUT_GOOD when dissector should stop after encapsulation,
487 * otherwise FLOW_DISSECT_RET_OUT_BAD
489 static enum flow_dissect_ret
490 __skb_flow_dissect_batadv(const struct sk_buff *skb,
491 struct flow_dissector_key_control *key_control,
492 void *data, __be16 *p_proto, int *p_nhoff, int hlen,
493 unsigned int flags)
495 struct {
496 struct batadv_unicast_packet batadv_unicast;
497 struct ethhdr eth;
498 } *hdr, _hdr;
500 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr), data, hlen,
501 &_hdr);
502 if (!hdr)
503 return FLOW_DISSECT_RET_OUT_BAD;
505 if (hdr->batadv_unicast.version != BATADV_COMPAT_VERSION)
506 return FLOW_DISSECT_RET_OUT_BAD;
508 if (hdr->batadv_unicast.packet_type != BATADV_UNICAST)
509 return FLOW_DISSECT_RET_OUT_BAD;
511 *p_proto = hdr->eth.h_proto;
512 *p_nhoff += sizeof(*hdr);
514 key_control->flags |= FLOW_DIS_ENCAPSULATION;
515 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
516 return FLOW_DISSECT_RET_OUT_GOOD;
518 return FLOW_DISSECT_RET_PROTO_AGAIN;
521 static void
522 __skb_flow_dissect_tcp(const struct sk_buff *skb,
523 struct flow_dissector *flow_dissector,
524 void *target_container, void *data, int thoff, int hlen)
526 struct flow_dissector_key_tcp *key_tcp;
527 struct tcphdr *th, _th;
529 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_TCP))
530 return;
532 th = __skb_header_pointer(skb, thoff, sizeof(_th), data, hlen, &_th);
533 if (!th)
534 return;
536 if (unlikely(__tcp_hdrlen(th) < sizeof(_th)))
537 return;
539 key_tcp = skb_flow_dissector_target(flow_dissector,
540 FLOW_DISSECTOR_KEY_TCP,
541 target_container);
542 key_tcp->flags = (*(__be16 *) &tcp_flag_word(th) & htons(0x0FFF));
545 static void
546 __skb_flow_dissect_ipv4(const struct sk_buff *skb,
547 struct flow_dissector *flow_dissector,
548 void *target_container, void *data, const struct iphdr *iph)
550 struct flow_dissector_key_ip *key_ip;
552 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
553 return;
555 key_ip = skb_flow_dissector_target(flow_dissector,
556 FLOW_DISSECTOR_KEY_IP,
557 target_container);
558 key_ip->tos = iph->tos;
559 key_ip->ttl = iph->ttl;
562 static void
563 __skb_flow_dissect_ipv6(const struct sk_buff *skb,
564 struct flow_dissector *flow_dissector,
565 void *target_container, void *data, const struct ipv6hdr *iph)
567 struct flow_dissector_key_ip *key_ip;
569 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
570 return;
572 key_ip = skb_flow_dissector_target(flow_dissector,
573 FLOW_DISSECTOR_KEY_IP,
574 target_container);
575 key_ip->tos = ipv6_get_dsfield(iph);
576 key_ip->ttl = iph->hop_limit;
579 /* Maximum number of protocol headers that can be parsed in
580 * __skb_flow_dissect
582 #define MAX_FLOW_DISSECT_HDRS 15
584 static bool skb_flow_dissect_allowed(int *num_hdrs)
586 ++*num_hdrs;
588 return (*num_hdrs <= MAX_FLOW_DISSECT_HDRS);
592 * __skb_flow_dissect - extract the flow_keys struct and return it
593 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
594 * @flow_dissector: list of keys to dissect
595 * @target_container: target structure to put dissected values into
596 * @data: raw buffer pointer to the packet, if NULL use skb->data
597 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
598 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
599 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
601 * The function will try to retrieve individual keys into target specified
602 * by flow_dissector from either the skbuff or a raw buffer specified by the
603 * rest parameters.
605 * Caller must take care of zeroing target container memory.
607 bool __skb_flow_dissect(const struct sk_buff *skb,
608 struct flow_dissector *flow_dissector,
609 void *target_container,
610 void *data, __be16 proto, int nhoff, int hlen,
611 unsigned int flags)
613 struct flow_dissector_key_control *key_control;
614 struct flow_dissector_key_basic *key_basic;
615 struct flow_dissector_key_addrs *key_addrs;
616 struct flow_dissector_key_ports *key_ports;
617 struct flow_dissector_key_icmp *key_icmp;
618 struct flow_dissector_key_tags *key_tags;
619 struct flow_dissector_key_vlan *key_vlan;
620 enum flow_dissect_ret fdret;
621 enum flow_dissector_key_id dissector_vlan = FLOW_DISSECTOR_KEY_MAX;
622 int num_hdrs = 0;
623 u8 ip_proto = 0;
624 bool ret;
626 if (!data) {
627 data = skb->data;
628 proto = skb_vlan_tag_present(skb) ?
629 skb->vlan_proto : skb->protocol;
630 nhoff = skb_network_offset(skb);
631 hlen = skb_headlen(skb);
632 #if IS_ENABLED(CONFIG_NET_DSA)
633 if (unlikely(skb->dev && netdev_uses_dsa(skb->dev) &&
634 proto == htons(ETH_P_XDSA))) {
635 const struct dsa_device_ops *ops;
636 int offset = 0;
638 ops = skb->dev->dsa_ptr->tag_ops;
639 if (ops->flow_dissect &&
640 !ops->flow_dissect(skb, &proto, &offset)) {
641 hlen -= offset;
642 nhoff += offset;
645 #endif
648 /* It is ensured by skb_flow_dissector_init() that control key will
649 * be always present.
651 key_control = skb_flow_dissector_target(flow_dissector,
652 FLOW_DISSECTOR_KEY_CONTROL,
653 target_container);
655 /* It is ensured by skb_flow_dissector_init() that basic key will
656 * be always present.
658 key_basic = skb_flow_dissector_target(flow_dissector,
659 FLOW_DISSECTOR_KEY_BASIC,
660 target_container);
662 if (dissector_uses_key(flow_dissector,
663 FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
664 struct ethhdr *eth = eth_hdr(skb);
665 struct flow_dissector_key_eth_addrs *key_eth_addrs;
667 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
668 FLOW_DISSECTOR_KEY_ETH_ADDRS,
669 target_container);
670 memcpy(key_eth_addrs, &eth->h_dest, sizeof(*key_eth_addrs));
673 proto_again:
674 fdret = FLOW_DISSECT_RET_CONTINUE;
676 switch (proto) {
677 case htons(ETH_P_IP): {
678 const struct iphdr *iph;
679 struct iphdr _iph;
681 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
682 if (!iph || iph->ihl < 5) {
683 fdret = FLOW_DISSECT_RET_OUT_BAD;
684 break;
687 nhoff += iph->ihl * 4;
689 ip_proto = iph->protocol;
691 if (dissector_uses_key(flow_dissector,
692 FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
693 key_addrs = skb_flow_dissector_target(flow_dissector,
694 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
695 target_container);
697 memcpy(&key_addrs->v4addrs, &iph->saddr,
698 sizeof(key_addrs->v4addrs));
699 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
702 if (ip_is_fragment(iph)) {
703 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
705 if (iph->frag_off & htons(IP_OFFSET)) {
706 fdret = FLOW_DISSECT_RET_OUT_GOOD;
707 break;
708 } else {
709 key_control->flags |= FLOW_DIS_FIRST_FRAG;
710 if (!(flags &
711 FLOW_DISSECTOR_F_PARSE_1ST_FRAG)) {
712 fdret = FLOW_DISSECT_RET_OUT_GOOD;
713 break;
718 __skb_flow_dissect_ipv4(skb, flow_dissector,
719 target_container, data, iph);
721 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3) {
722 fdret = FLOW_DISSECT_RET_OUT_GOOD;
723 break;
726 break;
728 case htons(ETH_P_IPV6): {
729 const struct ipv6hdr *iph;
730 struct ipv6hdr _iph;
732 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
733 if (!iph) {
734 fdret = FLOW_DISSECT_RET_OUT_BAD;
735 break;
738 ip_proto = iph->nexthdr;
739 nhoff += sizeof(struct ipv6hdr);
741 if (dissector_uses_key(flow_dissector,
742 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
743 key_addrs = skb_flow_dissector_target(flow_dissector,
744 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
745 target_container);
747 memcpy(&key_addrs->v6addrs, &iph->saddr,
748 sizeof(key_addrs->v6addrs));
749 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
752 if ((dissector_uses_key(flow_dissector,
753 FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
754 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
755 ip6_flowlabel(iph)) {
756 __be32 flow_label = ip6_flowlabel(iph);
758 if (dissector_uses_key(flow_dissector,
759 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
760 key_tags = skb_flow_dissector_target(flow_dissector,
761 FLOW_DISSECTOR_KEY_FLOW_LABEL,
762 target_container);
763 key_tags->flow_label = ntohl(flow_label);
765 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL) {
766 fdret = FLOW_DISSECT_RET_OUT_GOOD;
767 break;
771 __skb_flow_dissect_ipv6(skb, flow_dissector,
772 target_container, data, iph);
774 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
775 fdret = FLOW_DISSECT_RET_OUT_GOOD;
777 break;
779 case htons(ETH_P_8021AD):
780 case htons(ETH_P_8021Q): {
781 const struct vlan_hdr *vlan = NULL;
782 struct vlan_hdr _vlan;
783 __be16 saved_vlan_tpid = proto;
785 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX &&
786 skb && skb_vlan_tag_present(skb)) {
787 proto = skb->protocol;
788 } else {
789 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
790 data, hlen, &_vlan);
791 if (!vlan) {
792 fdret = FLOW_DISSECT_RET_OUT_BAD;
793 break;
796 proto = vlan->h_vlan_encapsulated_proto;
797 nhoff += sizeof(*vlan);
800 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX) {
801 dissector_vlan = FLOW_DISSECTOR_KEY_VLAN;
802 } else if (dissector_vlan == FLOW_DISSECTOR_KEY_VLAN) {
803 dissector_vlan = FLOW_DISSECTOR_KEY_CVLAN;
804 } else {
805 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
806 break;
809 if (dissector_uses_key(flow_dissector, dissector_vlan)) {
810 key_vlan = skb_flow_dissector_target(flow_dissector,
811 dissector_vlan,
812 target_container);
814 if (!vlan) {
815 key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
816 key_vlan->vlan_priority =
817 (skb_vlan_tag_get_prio(skb) >> VLAN_PRIO_SHIFT);
818 } else {
819 key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) &
820 VLAN_VID_MASK;
821 key_vlan->vlan_priority =
822 (ntohs(vlan->h_vlan_TCI) &
823 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
825 key_vlan->vlan_tpid = saved_vlan_tpid;
828 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
829 break;
831 case htons(ETH_P_PPP_SES): {
832 struct {
833 struct pppoe_hdr hdr;
834 __be16 proto;
835 } *hdr, _hdr;
836 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
837 if (!hdr) {
838 fdret = FLOW_DISSECT_RET_OUT_BAD;
839 break;
842 proto = hdr->proto;
843 nhoff += PPPOE_SES_HLEN;
844 switch (proto) {
845 case htons(PPP_IP):
846 proto = htons(ETH_P_IP);
847 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
848 break;
849 case htons(PPP_IPV6):
850 proto = htons(ETH_P_IPV6);
851 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
852 break;
853 default:
854 fdret = FLOW_DISSECT_RET_OUT_BAD;
855 break;
857 break;
859 case htons(ETH_P_TIPC): {
860 struct tipc_basic_hdr *hdr, _hdr;
862 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr),
863 data, hlen, &_hdr);
864 if (!hdr) {
865 fdret = FLOW_DISSECT_RET_OUT_BAD;
866 break;
869 if (dissector_uses_key(flow_dissector,
870 FLOW_DISSECTOR_KEY_TIPC)) {
871 key_addrs = skb_flow_dissector_target(flow_dissector,
872 FLOW_DISSECTOR_KEY_TIPC,
873 target_container);
874 key_addrs->tipckey.key = tipc_hdr_rps_key(hdr);
875 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC;
877 fdret = FLOW_DISSECT_RET_OUT_GOOD;
878 break;
881 case htons(ETH_P_MPLS_UC):
882 case htons(ETH_P_MPLS_MC):
883 fdret = __skb_flow_dissect_mpls(skb, flow_dissector,
884 target_container, data,
885 nhoff, hlen);
886 break;
887 case htons(ETH_P_FCOE):
888 if ((hlen - nhoff) < FCOE_HEADER_LEN) {
889 fdret = FLOW_DISSECT_RET_OUT_BAD;
890 break;
893 nhoff += FCOE_HEADER_LEN;
894 fdret = FLOW_DISSECT_RET_OUT_GOOD;
895 break;
897 case htons(ETH_P_ARP):
898 case htons(ETH_P_RARP):
899 fdret = __skb_flow_dissect_arp(skb, flow_dissector,
900 target_container, data,
901 nhoff, hlen);
902 break;
904 case htons(ETH_P_BATMAN):
905 fdret = __skb_flow_dissect_batadv(skb, key_control, data,
906 &proto, &nhoff, hlen, flags);
907 break;
909 default:
910 fdret = FLOW_DISSECT_RET_OUT_BAD;
911 break;
914 /* Process result of proto processing */
915 switch (fdret) {
916 case FLOW_DISSECT_RET_OUT_GOOD:
917 goto out_good;
918 case FLOW_DISSECT_RET_PROTO_AGAIN:
919 if (skb_flow_dissect_allowed(&num_hdrs))
920 goto proto_again;
921 goto out_good;
922 case FLOW_DISSECT_RET_CONTINUE:
923 case FLOW_DISSECT_RET_IPPROTO_AGAIN:
924 break;
925 case FLOW_DISSECT_RET_OUT_BAD:
926 default:
927 goto out_bad;
930 ip_proto_again:
931 fdret = FLOW_DISSECT_RET_CONTINUE;
933 switch (ip_proto) {
934 case IPPROTO_GRE:
935 fdret = __skb_flow_dissect_gre(skb, key_control, flow_dissector,
936 target_container, data,
937 &proto, &nhoff, &hlen, flags);
938 break;
940 case NEXTHDR_HOP:
941 case NEXTHDR_ROUTING:
942 case NEXTHDR_DEST: {
943 u8 _opthdr[2], *opthdr;
945 if (proto != htons(ETH_P_IPV6))
946 break;
948 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
949 data, hlen, &_opthdr);
950 if (!opthdr) {
951 fdret = FLOW_DISSECT_RET_OUT_BAD;
952 break;
955 ip_proto = opthdr[0];
956 nhoff += (opthdr[1] + 1) << 3;
958 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
959 break;
961 case NEXTHDR_FRAGMENT: {
962 struct frag_hdr _fh, *fh;
964 if (proto != htons(ETH_P_IPV6))
965 break;
967 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
968 data, hlen, &_fh);
970 if (!fh) {
971 fdret = FLOW_DISSECT_RET_OUT_BAD;
972 break;
975 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
977 nhoff += sizeof(_fh);
978 ip_proto = fh->nexthdr;
980 if (!(fh->frag_off & htons(IP6_OFFSET))) {
981 key_control->flags |= FLOW_DIS_FIRST_FRAG;
982 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) {
983 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
984 break;
988 fdret = FLOW_DISSECT_RET_OUT_GOOD;
989 break;
991 case IPPROTO_IPIP:
992 proto = htons(ETH_P_IP);
994 key_control->flags |= FLOW_DIS_ENCAPSULATION;
995 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
996 fdret = FLOW_DISSECT_RET_OUT_GOOD;
997 break;
1000 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1001 break;
1003 case IPPROTO_IPV6:
1004 proto = htons(ETH_P_IPV6);
1006 key_control->flags |= FLOW_DIS_ENCAPSULATION;
1007 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1008 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1009 break;
1012 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1013 break;
1016 case IPPROTO_MPLS:
1017 proto = htons(ETH_P_MPLS_UC);
1018 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1019 break;
1021 case IPPROTO_TCP:
1022 __skb_flow_dissect_tcp(skb, flow_dissector, target_container,
1023 data, nhoff, hlen);
1024 break;
1026 default:
1027 break;
1030 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS) &&
1031 !(key_control->flags & FLOW_DIS_IS_FRAGMENT)) {
1032 key_ports = skb_flow_dissector_target(flow_dissector,
1033 FLOW_DISSECTOR_KEY_PORTS,
1034 target_container);
1035 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
1036 data, hlen);
1039 if (dissector_uses_key(flow_dissector,
1040 FLOW_DISSECTOR_KEY_ICMP)) {
1041 key_icmp = skb_flow_dissector_target(flow_dissector,
1042 FLOW_DISSECTOR_KEY_ICMP,
1043 target_container);
1044 key_icmp->icmp = skb_flow_get_be16(skb, nhoff, data, hlen);
1047 /* Process result of IP proto processing */
1048 switch (fdret) {
1049 case FLOW_DISSECT_RET_PROTO_AGAIN:
1050 if (skb_flow_dissect_allowed(&num_hdrs))
1051 goto proto_again;
1052 break;
1053 case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1054 if (skb_flow_dissect_allowed(&num_hdrs))
1055 goto ip_proto_again;
1056 break;
1057 case FLOW_DISSECT_RET_OUT_GOOD:
1058 case FLOW_DISSECT_RET_CONTINUE:
1059 break;
1060 case FLOW_DISSECT_RET_OUT_BAD:
1061 default:
1062 goto out_bad;
1065 out_good:
1066 ret = true;
1068 out:
1069 key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
1070 key_basic->n_proto = proto;
1071 key_basic->ip_proto = ip_proto;
1073 return ret;
1075 out_bad:
1076 ret = false;
1077 goto out;
1079 EXPORT_SYMBOL(__skb_flow_dissect);
1081 static siphash_key_t hashrnd __read_mostly;
1082 static __always_inline void __flow_hash_secret_init(void)
1084 net_get_random_once(&hashrnd, sizeof(hashrnd));
1087 static const void *flow_keys_hash_start(const struct flow_keys *flow)
1089 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % SIPHASH_ALIGNMENT);
1090 return &flow->FLOW_KEYS_HASH_START_FIELD;
1093 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
1095 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
1096 BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
1097 sizeof(*flow) - sizeof(flow->addrs));
1099 switch (flow->control.addr_type) {
1100 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1101 diff -= sizeof(flow->addrs.v4addrs);
1102 break;
1103 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1104 diff -= sizeof(flow->addrs.v6addrs);
1105 break;
1106 case FLOW_DISSECTOR_KEY_TIPC:
1107 diff -= sizeof(flow->addrs.tipckey);
1108 break;
1110 return sizeof(*flow) - diff;
1113 __be32 flow_get_u32_src(const struct flow_keys *flow)
1115 switch (flow->control.addr_type) {
1116 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1117 return flow->addrs.v4addrs.src;
1118 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1119 return (__force __be32)ipv6_addr_hash(
1120 &flow->addrs.v6addrs.src);
1121 case FLOW_DISSECTOR_KEY_TIPC:
1122 return flow->addrs.tipckey.key;
1123 default:
1124 return 0;
1127 EXPORT_SYMBOL(flow_get_u32_src);
1129 __be32 flow_get_u32_dst(const struct flow_keys *flow)
1131 switch (flow->control.addr_type) {
1132 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1133 return flow->addrs.v4addrs.dst;
1134 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1135 return (__force __be32)ipv6_addr_hash(
1136 &flow->addrs.v6addrs.dst);
1137 default:
1138 return 0;
1141 EXPORT_SYMBOL(flow_get_u32_dst);
1143 static inline void __flow_hash_consistentify(struct flow_keys *keys)
1145 int addr_diff, i;
1147 switch (keys->control.addr_type) {
1148 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1149 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
1150 (__force u32)keys->addrs.v4addrs.src;
1151 if ((addr_diff < 0) ||
1152 (addr_diff == 0 &&
1153 ((__force u16)keys->ports.dst <
1154 (__force u16)keys->ports.src))) {
1155 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
1156 swap(keys->ports.src, keys->ports.dst);
1158 break;
1159 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1160 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
1161 &keys->addrs.v6addrs.src,
1162 sizeof(keys->addrs.v6addrs.dst));
1163 if ((addr_diff < 0) ||
1164 (addr_diff == 0 &&
1165 ((__force u16)keys->ports.dst <
1166 (__force u16)keys->ports.src))) {
1167 for (i = 0; i < 4; i++)
1168 swap(keys->addrs.v6addrs.src.s6_addr32[i],
1169 keys->addrs.v6addrs.dst.s6_addr32[i]);
1170 swap(keys->ports.src, keys->ports.dst);
1172 break;
1176 static inline u32 __flow_hash_from_keys(struct flow_keys *keys,
1177 const siphash_key_t *keyval)
1179 u32 hash;
1181 __flow_hash_consistentify(keys);
1183 hash = siphash(flow_keys_hash_start(keys),
1184 flow_keys_hash_length(keys), keyval);
1185 if (!hash)
1186 hash = 1;
1188 return hash;
1191 u32 flow_hash_from_keys(struct flow_keys *keys)
1193 __flow_hash_secret_init();
1194 return __flow_hash_from_keys(keys, &hashrnd);
1196 EXPORT_SYMBOL(flow_hash_from_keys);
1198 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
1199 struct flow_keys *keys,
1200 const siphash_key_t *keyval)
1202 skb_flow_dissect_flow_keys(skb, keys,
1203 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1205 return __flow_hash_from_keys(keys, keyval);
1208 struct _flow_keys_digest_data {
1209 __be16 n_proto;
1210 u8 ip_proto;
1211 u8 padding;
1212 __be32 ports;
1213 __be32 src;
1214 __be32 dst;
1217 void make_flow_keys_digest(struct flow_keys_digest *digest,
1218 const struct flow_keys *flow)
1220 struct _flow_keys_digest_data *data =
1221 (struct _flow_keys_digest_data *)digest;
1223 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
1225 memset(digest, 0, sizeof(*digest));
1227 data->n_proto = flow->basic.n_proto;
1228 data->ip_proto = flow->basic.ip_proto;
1229 data->ports = flow->ports.ports;
1230 data->src = flow->addrs.v4addrs.src;
1231 data->dst = flow->addrs.v4addrs.dst;
1233 EXPORT_SYMBOL(make_flow_keys_digest);
1235 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
1237 u32 __skb_get_hash_symmetric(const struct sk_buff *skb)
1239 struct flow_keys keys;
1241 __flow_hash_secret_init();
1243 memset(&keys, 0, sizeof(keys));
1244 __skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys,
1245 NULL, 0, 0, 0,
1246 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1248 return __flow_hash_from_keys(&keys, &hashrnd);
1250 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
1253 * __skb_get_hash: calculate a flow hash
1254 * @skb: sk_buff to calculate flow hash from
1256 * This function calculates a flow hash based on src/dst addresses
1257 * and src/dst port numbers. Sets hash in skb to non-zero hash value
1258 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
1259 * if hash is a canonical 4-tuple hash over transport ports.
1261 void __skb_get_hash(struct sk_buff *skb)
1263 struct flow_keys keys;
1264 u32 hash;
1266 __flow_hash_secret_init();
1268 hash = ___skb_get_hash(skb, &keys, &hashrnd);
1270 __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1272 EXPORT_SYMBOL(__skb_get_hash);
1274 __u32 skb_get_hash_perturb(const struct sk_buff *skb,
1275 const siphash_key_t *perturb)
1277 struct flow_keys keys;
1279 return ___skb_get_hash(skb, &keys, perturb);
1281 EXPORT_SYMBOL(skb_get_hash_perturb);
1283 u32 __skb_get_poff(const struct sk_buff *skb, void *data,
1284 const struct flow_keys_basic *keys, int hlen)
1286 u32 poff = keys->control.thoff;
1288 /* skip L4 headers for fragments after the first */
1289 if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
1290 !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
1291 return poff;
1293 switch (keys->basic.ip_proto) {
1294 case IPPROTO_TCP: {
1295 /* access doff as u8 to avoid unaligned access */
1296 const u8 *doff;
1297 u8 _doff;
1299 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
1300 data, hlen, &_doff);
1301 if (!doff)
1302 return poff;
1304 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
1305 break;
1307 case IPPROTO_UDP:
1308 case IPPROTO_UDPLITE:
1309 poff += sizeof(struct udphdr);
1310 break;
1311 /* For the rest, we do not really care about header
1312 * extensions at this point for now.
1314 case IPPROTO_ICMP:
1315 poff += sizeof(struct icmphdr);
1316 break;
1317 case IPPROTO_ICMPV6:
1318 poff += sizeof(struct icmp6hdr);
1319 break;
1320 case IPPROTO_IGMP:
1321 poff += sizeof(struct igmphdr);
1322 break;
1323 case IPPROTO_DCCP:
1324 poff += sizeof(struct dccp_hdr);
1325 break;
1326 case IPPROTO_SCTP:
1327 poff += sizeof(struct sctphdr);
1328 break;
1331 return poff;
1335 * skb_get_poff - get the offset to the payload
1336 * @skb: sk_buff to get the payload offset from
1338 * The function will get the offset to the payload as far as it could
1339 * be dissected. The main user is currently BPF, so that we can dynamically
1340 * truncate packets without needing to push actual payload to the user
1341 * space and can analyze headers only, instead.
1343 u32 skb_get_poff(const struct sk_buff *skb)
1345 struct flow_keys_basic keys;
1347 if (!skb_flow_dissect_flow_keys_basic(skb, &keys, NULL, 0, 0, 0, 0))
1348 return 0;
1350 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
1353 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
1355 memset(keys, 0, sizeof(*keys));
1357 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
1358 sizeof(keys->addrs.v6addrs.src));
1359 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
1360 sizeof(keys->addrs.v6addrs.dst));
1361 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1362 keys->ports.src = fl6->fl6_sport;
1363 keys->ports.dst = fl6->fl6_dport;
1364 keys->keyid.keyid = fl6->fl6_gre_key;
1365 keys->tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
1366 keys->basic.ip_proto = fl6->flowi6_proto;
1368 return flow_hash_from_keys(keys);
1370 EXPORT_SYMBOL(__get_hash_from_flowi6);
1372 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
1374 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1375 .offset = offsetof(struct flow_keys, control),
1378 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1379 .offset = offsetof(struct flow_keys, basic),
1382 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1383 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1386 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1387 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1390 .key_id = FLOW_DISSECTOR_KEY_TIPC,
1391 .offset = offsetof(struct flow_keys, addrs.tipckey),
1394 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1395 .offset = offsetof(struct flow_keys, ports),
1398 .key_id = FLOW_DISSECTOR_KEY_VLAN,
1399 .offset = offsetof(struct flow_keys, vlan),
1402 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
1403 .offset = offsetof(struct flow_keys, tags),
1406 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
1407 .offset = offsetof(struct flow_keys, keyid),
1411 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
1413 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1414 .offset = offsetof(struct flow_keys, control),
1417 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1418 .offset = offsetof(struct flow_keys, basic),
1421 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1422 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1425 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1426 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1429 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1430 .offset = offsetof(struct flow_keys, ports),
1434 static const struct flow_dissector_key flow_keys_basic_dissector_keys[] = {
1436 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1437 .offset = offsetof(struct flow_keys, control),
1440 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1441 .offset = offsetof(struct flow_keys, basic),
1445 struct flow_dissector flow_keys_dissector __read_mostly;
1446 EXPORT_SYMBOL(flow_keys_dissector);
1448 struct flow_dissector flow_keys_basic_dissector __read_mostly;
1449 EXPORT_SYMBOL(flow_keys_basic_dissector);
1451 static int __init init_default_flow_dissectors(void)
1453 skb_flow_dissector_init(&flow_keys_dissector,
1454 flow_keys_dissector_keys,
1455 ARRAY_SIZE(flow_keys_dissector_keys));
1456 skb_flow_dissector_init(&flow_keys_dissector_symmetric,
1457 flow_keys_dissector_symmetric_keys,
1458 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
1459 skb_flow_dissector_init(&flow_keys_basic_dissector,
1460 flow_keys_basic_dissector_keys,
1461 ARRAY_SIZE(flow_keys_basic_dissector_keys));
1462 return 0;
1465 core_initcall(init_default_flow_dissectors);