1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kernel.h>
3 #include <linux/skbuff.h>
4 #include <linux/export.h>
5 #include <linux/ip.h>
6 #include <linux/ipv6.h>
7 #include <linux/if_vlan.h>
8 #include <net/dsa.h>
9 #include <net/dst_metadata.h>
10 #include <net/ip.h>
11 #include <net/ipv6.h>
12 #include <net/gre.h>
13 #include <net/pptp.h>
14 #include <net/tipc.h>
15 #include <linux/igmp.h>
16 #include <linux/icmp.h>
17 #include <linux/sctp.h>
18 #include <linux/dccp.h>
19 #include <linux/if_tunnel.h>
20 #include <linux/if_pppox.h>
21 #include <linux/ppp_defs.h>
22 #include <linux/stddef.h>
23 #include <linux/if_ether.h>
24 #include <linux/mpls.h>
25 #include <linux/tcp.h>
26 #include <linux/ptp_classify.h>
27 #include <net/flow_dissector.h>
28 #include <scsi/fc/fc_fcoe.h>
29 #include <uapi/linux/batadv_packet.h>
30 #include <linux/bpf.h>
31 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
32 #include <net/netfilter/nf_conntrack_core.h>
33 #include <net/netfilter/nf_conntrack_labels.h>
34 #endif
35 #include <linux/bpf-netns.h>
36
dissector_set_key(struct flow_dissector * flow_dissector,enum flow_dissector_key_id key_id)37 static void dissector_set_key(struct flow_dissector *flow_dissector,
38 enum flow_dissector_key_id key_id)
39 {
40 flow_dissector->used_keys |= (1 << key_id);
41 }
42
skb_flow_dissector_init(struct flow_dissector * flow_dissector,const struct flow_dissector_key * key,unsigned int key_count)43 void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
44 const struct flow_dissector_key *key,
45 unsigned int key_count)
46 {
47 unsigned int i;
48
49 memset(flow_dissector, 0, sizeof(*flow_dissector));
50
51 for (i = 0; i < key_count; i++, key++) {
52 /* User should make sure that every key target offset is within
53 * boundaries of unsigned short.
54 */
55 BUG_ON(key->offset > USHRT_MAX);
56 BUG_ON(dissector_uses_key(flow_dissector,
57 key->key_id));
58
59 dissector_set_key(flow_dissector, key->key_id);
60 flow_dissector->offset[key->key_id] = key->offset;
61 }
62
63 /* Ensure that the dissector always includes control and basic key.
64 * That way we are able to avoid handling lack of these in fast path.
65 */
66 BUG_ON(!dissector_uses_key(flow_dissector,
67 FLOW_DISSECTOR_KEY_CONTROL));
68 BUG_ON(!dissector_uses_key(flow_dissector,
69 FLOW_DISSECTOR_KEY_BASIC));
70 }
71 EXPORT_SYMBOL(skb_flow_dissector_init);
72
73 #ifdef CONFIG_BPF_SYSCALL
flow_dissector_bpf_prog_attach_check(struct net * net,struct bpf_prog * prog)74 int flow_dissector_bpf_prog_attach_check(struct net *net,
75 struct bpf_prog *prog)
76 {
77 enum netns_bpf_attach_type type = NETNS_BPF_FLOW_DISSECTOR;
78
79 if (net == &init_net) {
80 /* BPF flow dissector in the root namespace overrides
81 * any per-net-namespace one. When attaching to root,
82 * make sure we don't have any BPF program attached
83 * to the non-root namespaces.
84 */
85 struct net *ns;
86
87 for_each_net(ns) {
88 if (ns == &init_net)
89 continue;
90 if (rcu_access_pointer(ns->bpf.run_array[type]))
91 return -EEXIST;
92 }
93 } else {
94 /* Make sure root flow dissector is not attached
95 * when attaching to the non-root namespace.
96 */
97 if (rcu_access_pointer(init_net.bpf.run_array[type]))
98 return -EEXIST;
99 }
100
101 return 0;
102 }
103 #endif /* CONFIG_BPF_SYSCALL */
104
105 /**
106 * __skb_flow_get_ports - extract the upper layer ports and return them
107 * @skb: sk_buff to extract the ports from
108 * @thoff: transport header offset
109 * @ip_proto: protocol for which to get port offset
110 * @data: raw buffer pointer to the packet, if NULL use skb->data
111 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
112 *
113 * The function will try to retrieve the ports at offset thoff + poff where poff
114 * is the protocol port offset returned from proto_ports_offset
115 */
__skb_flow_get_ports(const struct sk_buff * skb,int thoff,u8 ip_proto,const void * data,int hlen)116 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
117 const void *data, int hlen)
118 {
119 int poff = proto_ports_offset(ip_proto);
120
121 if (!data) {
122 data = skb->data;
123 hlen = skb_headlen(skb);
124 }
125
126 if (poff >= 0) {
127 __be32 *ports, _ports;
128
129 ports = __skb_header_pointer(skb, thoff + poff,
130 sizeof(_ports), data, hlen, &_ports);
131 if (ports)
132 return *ports;
133 }
134
135 return 0;
136 }
137 EXPORT_SYMBOL(__skb_flow_get_ports);
138
icmp_has_id(u8 type)139 static bool icmp_has_id(u8 type)
140 {
141 switch (type) {
142 case ICMP_ECHO:
143 case ICMP_ECHOREPLY:
144 case ICMP_TIMESTAMP:
145 case ICMP_TIMESTAMPREPLY:
146 case ICMPV6_ECHO_REQUEST:
147 case ICMPV6_ECHO_REPLY:
148 return true;
149 }
150
151 return false;
152 }
153
154 /**
155 * skb_flow_get_icmp_tci - extract ICMP(6) Type, Code and Identifier fields
156 * @skb: sk_buff to extract from
157 * @key_icmp: struct flow_dissector_key_icmp to fill
158 * @data: raw buffer pointer to the packet
159 * @thoff: offset to extract at
160 * @hlen: packet header length
161 */
skb_flow_get_icmp_tci(const struct sk_buff * skb,struct flow_dissector_key_icmp * key_icmp,const void * data,int thoff,int hlen)162 void skb_flow_get_icmp_tci(const struct sk_buff *skb,
163 struct flow_dissector_key_icmp *key_icmp,
164 const void *data, int thoff, int hlen)
165 {
166 struct icmphdr *ih, _ih;
167
168 ih = __skb_header_pointer(skb, thoff, sizeof(_ih), data, hlen, &_ih);
169 if (!ih)
170 return;
171
172 key_icmp->type = ih->type;
173 key_icmp->code = ih->code;
174
175 /* As we use 0 to signal that the Id field is not present,
176 * avoid confusion with packets without such field
177 */
178 if (icmp_has_id(ih->type))
179 key_icmp->id = ih->un.echo.id ? ntohs(ih->un.echo.id) : 1;
180 else
181 key_icmp->id = 0;
182 }
183 EXPORT_SYMBOL(skb_flow_get_icmp_tci);
184
185 /* If FLOW_DISSECTOR_KEY_ICMP is set, dissect an ICMP packet
186 * using skb_flow_get_icmp_tci().
187 */
__skb_flow_dissect_icmp(const struct sk_buff * skb,struct flow_dissector * flow_dissector,void * target_container,const void * data,int thoff,int hlen)188 static void __skb_flow_dissect_icmp(const struct sk_buff *skb,
189 struct flow_dissector *flow_dissector,
190 void *target_container, const void *data,
191 int thoff, int hlen)
192 {
193 struct flow_dissector_key_icmp *key_icmp;
194
195 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ICMP))
196 return;
197
198 key_icmp = skb_flow_dissector_target(flow_dissector,
199 FLOW_DISSECTOR_KEY_ICMP,
200 target_container);
201
202 skb_flow_get_icmp_tci(skb, key_icmp, data, thoff, hlen);
203 }
204
skb_flow_dissect_meta(const struct sk_buff * skb,struct flow_dissector * flow_dissector,void * target_container)205 void skb_flow_dissect_meta(const struct sk_buff *skb,
206 struct flow_dissector *flow_dissector,
207 void *target_container)
208 {
209 struct flow_dissector_key_meta *meta;
210
211 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_META))
212 return;
213
214 meta = skb_flow_dissector_target(flow_dissector,
215 FLOW_DISSECTOR_KEY_META,
216 target_container);
217 meta->ingress_ifindex = skb->skb_iif;
218 }
219 EXPORT_SYMBOL(skb_flow_dissect_meta);
220
221 static void
skb_flow_dissect_set_enc_addr_type(enum flow_dissector_key_id type,struct flow_dissector * flow_dissector,void * target_container)222 skb_flow_dissect_set_enc_addr_type(enum flow_dissector_key_id type,
223 struct flow_dissector *flow_dissector,
224 void *target_container)
225 {
226 struct flow_dissector_key_control *ctrl;
227
228 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL))
229 return;
230
231 ctrl = skb_flow_dissector_target(flow_dissector,
232 FLOW_DISSECTOR_KEY_ENC_CONTROL,
233 target_container);
234 ctrl->addr_type = type;
235 }
236
237 void
skb_flow_dissect_ct(const struct sk_buff * skb,struct flow_dissector * flow_dissector,void * target_container,u16 * ctinfo_map,size_t mapsize,bool post_ct,u16 zone)238 skb_flow_dissect_ct(const struct sk_buff *skb,
239 struct flow_dissector *flow_dissector,
240 void *target_container, u16 *ctinfo_map,
241 size_t mapsize, bool post_ct, u16 zone)
242 {
243 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
244 struct flow_dissector_key_ct *key;
245 enum ip_conntrack_info ctinfo;
246 struct nf_conn_labels *cl;
247 struct nf_conn *ct;
248
249 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_CT))
250 return;
251
252 ct = nf_ct_get(skb, &ctinfo);
253 if (!ct && !post_ct)
254 return;
255
256 key = skb_flow_dissector_target(flow_dissector,
257 FLOW_DISSECTOR_KEY_CT,
258 target_container);
259
260 if (!ct) {
261 key->ct_state = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
262 TCA_FLOWER_KEY_CT_FLAGS_INVALID;
263 key->ct_zone = zone;
264 return;
265 }
266
267 if (ctinfo < mapsize)
268 key->ct_state = ctinfo_map[ctinfo];
269 #if IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)
270 key->ct_zone = ct->zone.id;
271 #endif
272 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
273 key->ct_mark = ct->mark;
274 #endif
275
276 cl = nf_ct_labels_find(ct);
277 if (cl)
278 memcpy(key->ct_labels, cl->bits, sizeof(key->ct_labels));
279 #endif /* CONFIG_NF_CONNTRACK */
280 }
281 EXPORT_SYMBOL(skb_flow_dissect_ct);
282
283 void
skb_flow_dissect_tunnel_info(const struct sk_buff * skb,struct flow_dissector * flow_dissector,void * target_container)284 skb_flow_dissect_tunnel_info(const struct sk_buff *skb,
285 struct flow_dissector *flow_dissector,
286 void *target_container)
287 {
288 struct ip_tunnel_info *info;
289 struct ip_tunnel_key *key;
290
291 /* A quick check to see if there might be something to do. */
292 if (!dissector_uses_key(flow_dissector,
293 FLOW_DISSECTOR_KEY_ENC_KEYID) &&
294 !dissector_uses_key(flow_dissector,
295 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) &&
296 !dissector_uses_key(flow_dissector,
297 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) &&
298 !dissector_uses_key(flow_dissector,
299 FLOW_DISSECTOR_KEY_ENC_CONTROL) &&
300 !dissector_uses_key(flow_dissector,
301 FLOW_DISSECTOR_KEY_ENC_PORTS) &&
302 !dissector_uses_key(flow_dissector,
303 FLOW_DISSECTOR_KEY_ENC_IP) &&
304 !dissector_uses_key(flow_dissector,
305 FLOW_DISSECTOR_KEY_ENC_OPTS))
306 return;
307
308 info = skb_tunnel_info(skb);
309 if (!info)
310 return;
311
312 key = &info->key;
313
314 switch (ip_tunnel_info_af(info)) {
315 case AF_INET:
316 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV4_ADDRS,
317 flow_dissector,
318 target_container);
319 if (dissector_uses_key(flow_dissector,
320 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) {
321 struct flow_dissector_key_ipv4_addrs *ipv4;
322
323 ipv4 = skb_flow_dissector_target(flow_dissector,
324 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
325 target_container);
326 ipv4->src = key->u.ipv4.src;
327 ipv4->dst = key->u.ipv4.dst;
328 }
329 break;
330 case AF_INET6:
331 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV6_ADDRS,
332 flow_dissector,
333 target_container);
334 if (dissector_uses_key(flow_dissector,
335 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS)) {
336 struct flow_dissector_key_ipv6_addrs *ipv6;
337
338 ipv6 = skb_flow_dissector_target(flow_dissector,
339 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
340 target_container);
341 ipv6->src = key->u.ipv6.src;
342 ipv6->dst = key->u.ipv6.dst;
343 }
344 break;
345 }
346
347 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
348 struct flow_dissector_key_keyid *keyid;
349
350 keyid = skb_flow_dissector_target(flow_dissector,
351 FLOW_DISSECTOR_KEY_ENC_KEYID,
352 target_container);
353 keyid->keyid = tunnel_id_to_key32(key->tun_id);
354 }
355
356 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
357 struct flow_dissector_key_ports *tp;
358
359 tp = skb_flow_dissector_target(flow_dissector,
360 FLOW_DISSECTOR_KEY_ENC_PORTS,
361 target_container);
362 tp->src = key->tp_src;
363 tp->dst = key->tp_dst;
364 }
365
366 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_IP)) {
367 struct flow_dissector_key_ip *ip;
368
369 ip = skb_flow_dissector_target(flow_dissector,
370 FLOW_DISSECTOR_KEY_ENC_IP,
371 target_container);
372 ip->tos = key->tos;
373 ip->ttl = key->ttl;
374 }
375
376 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_OPTS)) {
377 struct flow_dissector_key_enc_opts *enc_opt;
378
379 enc_opt = skb_flow_dissector_target(flow_dissector,
380 FLOW_DISSECTOR_KEY_ENC_OPTS,
381 target_container);
382
383 if (info->options_len) {
384 enc_opt->len = info->options_len;
385 ip_tunnel_info_opts_get(enc_opt->data, info);
386 enc_opt->dst_opt_type = info->key.tun_flags &
387 TUNNEL_OPTIONS_PRESENT;
388 }
389 }
390 }
391 EXPORT_SYMBOL(skb_flow_dissect_tunnel_info);
392
skb_flow_dissect_hash(const struct sk_buff * skb,struct flow_dissector * flow_dissector,void * target_container)393 void skb_flow_dissect_hash(const struct sk_buff *skb,
394 struct flow_dissector *flow_dissector,
395 void *target_container)
396 {
397 struct flow_dissector_key_hash *key;
398
399 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_HASH))
400 return;
401
402 key = skb_flow_dissector_target(flow_dissector,
403 FLOW_DISSECTOR_KEY_HASH,
404 target_container);
405
406 key->hash = skb_get_hash_raw(skb);
407 }
408 EXPORT_SYMBOL(skb_flow_dissect_hash);
409
410 static enum flow_dissect_ret
__skb_flow_dissect_mpls(const struct sk_buff * skb,struct flow_dissector * flow_dissector,void * target_container,const void * data,int nhoff,int hlen,int lse_index,bool * entropy_label)411 __skb_flow_dissect_mpls(const struct sk_buff *skb,
412 struct flow_dissector *flow_dissector,
413 void *target_container, const void *data, int nhoff,
414 int hlen, int lse_index, bool *entropy_label)
415 {
416 struct mpls_label *hdr, _hdr;
417 u32 entry, label, bos;
418
419 if (!dissector_uses_key(flow_dissector,
420 FLOW_DISSECTOR_KEY_MPLS_ENTROPY) &&
421 !dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS))
422 return FLOW_DISSECT_RET_OUT_GOOD;
423
424 if (lse_index >= FLOW_DIS_MPLS_MAX)
425 return FLOW_DISSECT_RET_OUT_GOOD;
426
427 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
428 hlen, &_hdr);
429 if (!hdr)
430 return FLOW_DISSECT_RET_OUT_BAD;
431
432 entry = ntohl(hdr->entry);
433 label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
434 bos = (entry & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT;
435
436 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) {
437 struct flow_dissector_key_mpls *key_mpls;
438 struct flow_dissector_mpls_lse *lse;
439
440 key_mpls = skb_flow_dissector_target(flow_dissector,
441 FLOW_DISSECTOR_KEY_MPLS,
442 target_container);
443 lse = &key_mpls->ls[lse_index];
444
445 lse->mpls_ttl = (entry & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
446 lse->mpls_bos = bos;
447 lse->mpls_tc = (entry & MPLS_LS_TC_MASK) >> MPLS_LS_TC_SHIFT;
448 lse->mpls_label = label;
449 dissector_set_mpls_lse(key_mpls, lse_index);
450 }
451
452 if (*entropy_label &&
453 dissector_uses_key(flow_dissector,
454 FLOW_DISSECTOR_KEY_MPLS_ENTROPY)) {
455 struct flow_dissector_key_keyid *key_keyid;
456
457 key_keyid = skb_flow_dissector_target(flow_dissector,
458 FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
459 target_container);
460 key_keyid->keyid = cpu_to_be32(label);
461 }
462
463 *entropy_label = label == MPLS_LABEL_ENTROPY;
464
465 return bos ? FLOW_DISSECT_RET_OUT_GOOD : FLOW_DISSECT_RET_PROTO_AGAIN;
466 }
467
468 static enum flow_dissect_ret
__skb_flow_dissect_arp(const struct sk_buff * skb,struct flow_dissector * flow_dissector,void * target_container,const void * data,int nhoff,int hlen)469 __skb_flow_dissect_arp(const struct sk_buff *skb,
470 struct flow_dissector *flow_dissector,
471 void *target_container, const void *data,
472 int nhoff, int hlen)
473 {
474 struct flow_dissector_key_arp *key_arp;
475 struct {
476 unsigned char ar_sha[ETH_ALEN];
477 unsigned char ar_sip[4];
478 unsigned char ar_tha[ETH_ALEN];
479 unsigned char ar_tip[4];
480 } *arp_eth, _arp_eth;
481 const struct arphdr *arp;
482 struct arphdr _arp;
483
484 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ARP))
485 return FLOW_DISSECT_RET_OUT_GOOD;
486
487 arp = __skb_header_pointer(skb, nhoff, sizeof(_arp), data,
488 hlen, &_arp);
489 if (!arp)
490 return FLOW_DISSECT_RET_OUT_BAD;
491
492 if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
493 arp->ar_pro != htons(ETH_P_IP) ||
494 arp->ar_hln != ETH_ALEN ||
495 arp->ar_pln != 4 ||
496 (arp->ar_op != htons(ARPOP_REPLY) &&
497 arp->ar_op != htons(ARPOP_REQUEST)))
498 return FLOW_DISSECT_RET_OUT_BAD;
499
500 arp_eth = __skb_header_pointer(skb, nhoff + sizeof(_arp),
501 sizeof(_arp_eth), data,
502 hlen, &_arp_eth);
503 if (!arp_eth)
504 return FLOW_DISSECT_RET_OUT_BAD;
505
506 key_arp = skb_flow_dissector_target(flow_dissector,
507 FLOW_DISSECTOR_KEY_ARP,
508 target_container);
509
510 memcpy(&key_arp->sip, arp_eth->ar_sip, sizeof(key_arp->sip));
511 memcpy(&key_arp->tip, arp_eth->ar_tip, sizeof(key_arp->tip));
512
513 /* Only store the lower byte of the opcode;
514 * this covers ARPOP_REPLY and ARPOP_REQUEST.
515 */
516 key_arp->op = ntohs(arp->ar_op) & 0xff;
517
518 ether_addr_copy(key_arp->sha, arp_eth->ar_sha);
519 ether_addr_copy(key_arp->tha, arp_eth->ar_tha);
520
521 return FLOW_DISSECT_RET_OUT_GOOD;
522 }
523
524 static enum flow_dissect_ret
__skb_flow_dissect_gre(const struct sk_buff * skb,struct flow_dissector_key_control * key_control,struct flow_dissector * flow_dissector,void * target_container,const void * data,__be16 * p_proto,int * p_nhoff,int * p_hlen,unsigned int flags)525 __skb_flow_dissect_gre(const struct sk_buff *skb,
526 struct flow_dissector_key_control *key_control,
527 struct flow_dissector *flow_dissector,
528 void *target_container, const void *data,
529 __be16 *p_proto, int *p_nhoff, int *p_hlen,
530 unsigned int flags)
531 {
532 struct flow_dissector_key_keyid *key_keyid;
533 struct gre_base_hdr *hdr, _hdr;
534 int offset = 0;
535 u16 gre_ver;
536
537 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr),
538 data, *p_hlen, &_hdr);
539 if (!hdr)
540 return FLOW_DISSECT_RET_OUT_BAD;
541
542 /* Only look inside GRE without routing */
543 if (hdr->flags & GRE_ROUTING)
544 return FLOW_DISSECT_RET_OUT_GOOD;
545
546 /* Only look inside GRE for version 0 and 1 */
547 gre_ver = ntohs(hdr->flags & GRE_VERSION);
548 if (gre_ver > 1)
549 return FLOW_DISSECT_RET_OUT_GOOD;
550
551 *p_proto = hdr->protocol;
552 if (gre_ver) {
553 /* Version1 must be PPTP, and check the flags */
554 if (!(*p_proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY)))
555 return FLOW_DISSECT_RET_OUT_GOOD;
556 }
557
558 offset += sizeof(struct gre_base_hdr);
559
560 if (hdr->flags & GRE_CSUM)
561 offset += sizeof_field(struct gre_full_hdr, csum) +
562 sizeof_field(struct gre_full_hdr, reserved1);
563
564 if (hdr->flags & GRE_KEY) {
565 const __be32 *keyid;
566 __be32 _keyid;
567
568 keyid = __skb_header_pointer(skb, *p_nhoff + offset,
569 sizeof(_keyid),
570 data, *p_hlen, &_keyid);
571 if (!keyid)
572 return FLOW_DISSECT_RET_OUT_BAD;
573
574 if (dissector_uses_key(flow_dissector,
575 FLOW_DISSECTOR_KEY_GRE_KEYID)) {
576 key_keyid = skb_flow_dissector_target(flow_dissector,
577 FLOW_DISSECTOR_KEY_GRE_KEYID,
578 target_container);
579 if (gre_ver == 0)
580 key_keyid->keyid = *keyid;
581 else
582 key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK;
583 }
584 offset += sizeof_field(struct gre_full_hdr, key);
585 }
586
587 if (hdr->flags & GRE_SEQ)
588 offset += sizeof_field(struct pptp_gre_header, seq);
589
590 if (gre_ver == 0) {
591 if (*p_proto == htons(ETH_P_TEB)) {
592 const struct ethhdr *eth;
593 struct ethhdr _eth;
594
595 eth = __skb_header_pointer(skb, *p_nhoff + offset,
596 sizeof(_eth),
597 data, *p_hlen, &_eth);
598 if (!eth)
599 return FLOW_DISSECT_RET_OUT_BAD;
600 *p_proto = eth->h_proto;
601 offset += sizeof(*eth);
602
603 /* Cap headers that we access via pointers at the
604 * end of the Ethernet header as our maximum alignment
605 * at that point is only 2 bytes.
606 */
607 if (NET_IP_ALIGN)
608 *p_hlen = *p_nhoff + offset;
609 }
610 } else { /* version 1, must be PPTP */
611 u8 _ppp_hdr[PPP_HDRLEN];
612 u8 *ppp_hdr;
613
614 if (hdr->flags & GRE_ACK)
615 offset += sizeof_field(struct pptp_gre_header, ack);
616
617 ppp_hdr = __skb_header_pointer(skb, *p_nhoff + offset,
618 sizeof(_ppp_hdr),
619 data, *p_hlen, _ppp_hdr);
620 if (!ppp_hdr)
621 return FLOW_DISSECT_RET_OUT_BAD;
622
623 switch (PPP_PROTOCOL(ppp_hdr)) {
624 case PPP_IP:
625 *p_proto = htons(ETH_P_IP);
626 break;
627 case PPP_IPV6:
628 *p_proto = htons(ETH_P_IPV6);
629 break;
630 default:
631 /* Could probably catch some more like MPLS */
632 break;
633 }
634
635 offset += PPP_HDRLEN;
636 }
637
638 *p_nhoff += offset;
639 key_control->flags |= FLOW_DIS_ENCAPSULATION;
640 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
641 return FLOW_DISSECT_RET_OUT_GOOD;
642
643 return FLOW_DISSECT_RET_PROTO_AGAIN;
644 }
645
646 /**
647 * __skb_flow_dissect_batadv() - dissect batman-adv header
648 * @skb: sk_buff to with the batman-adv header
649 * @key_control: flow dissectors control key
650 * @data: raw buffer pointer to the packet, if NULL use skb->data
651 * @p_proto: pointer used to update the protocol to process next
652 * @p_nhoff: pointer used to update inner network header offset
653 * @hlen: packet header length
654 * @flags: any combination of FLOW_DISSECTOR_F_*
655 *
656 * ETH_P_BATMAN packets are tried to be dissected. Only
657 * &struct batadv_unicast packets are actually processed because they contain an
658 * inner ethernet header and are usually followed by actual network header. This
659 * allows the flow dissector to continue processing the packet.
660 *
661 * Return: FLOW_DISSECT_RET_PROTO_AGAIN when &struct batadv_unicast was found,
662 * FLOW_DISSECT_RET_OUT_GOOD when dissector should stop after encapsulation,
663 * otherwise FLOW_DISSECT_RET_OUT_BAD
664 */
665 static enum flow_dissect_ret
__skb_flow_dissect_batadv(const struct sk_buff * skb,struct flow_dissector_key_control * key_control,const void * data,__be16 * p_proto,int * p_nhoff,int hlen,unsigned int flags)666 __skb_flow_dissect_batadv(const struct sk_buff *skb,
667 struct flow_dissector_key_control *key_control,
668 const void *data, __be16 *p_proto, int *p_nhoff,
669 int hlen, unsigned int flags)
670 {
671 struct {
672 struct batadv_unicast_packet batadv_unicast;
673 struct ethhdr eth;
674 } *hdr, _hdr;
675
676 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr), data, hlen,
677 &_hdr);
678 if (!hdr)
679 return FLOW_DISSECT_RET_OUT_BAD;
680
681 if (hdr->batadv_unicast.version != BATADV_COMPAT_VERSION)
682 return FLOW_DISSECT_RET_OUT_BAD;
683
684 if (hdr->batadv_unicast.packet_type != BATADV_UNICAST)
685 return FLOW_DISSECT_RET_OUT_BAD;
686
687 *p_proto = hdr->eth.h_proto;
688 *p_nhoff += sizeof(*hdr);
689
690 key_control->flags |= FLOW_DIS_ENCAPSULATION;
691 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
692 return FLOW_DISSECT_RET_OUT_GOOD;
693
694 return FLOW_DISSECT_RET_PROTO_AGAIN;
695 }
696
697 static void
__skb_flow_dissect_tcp(const struct sk_buff * skb,struct flow_dissector * flow_dissector,void * target_container,const void * data,int thoff,int hlen)698 __skb_flow_dissect_tcp(const struct sk_buff *skb,
699 struct flow_dissector *flow_dissector,
700 void *target_container, const void *data,
701 int thoff, int hlen)
702 {
703 struct flow_dissector_key_tcp *key_tcp;
704 struct tcphdr *th, _th;
705
706 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_TCP))
707 return;
708
709 th = __skb_header_pointer(skb, thoff, sizeof(_th), data, hlen, &_th);
710 if (!th)
711 return;
712
713 if (unlikely(__tcp_hdrlen(th) < sizeof(_th)))
714 return;
715
716 key_tcp = skb_flow_dissector_target(flow_dissector,
717 FLOW_DISSECTOR_KEY_TCP,
718 target_container);
719 key_tcp->flags = (*(__be16 *) &tcp_flag_word(th) & htons(0x0FFF));
720 }
721
722 static void
__skb_flow_dissect_ports(const struct sk_buff * skb,struct flow_dissector * flow_dissector,void * target_container,const void * data,int nhoff,u8 ip_proto,int hlen)723 __skb_flow_dissect_ports(const struct sk_buff *skb,
724 struct flow_dissector *flow_dissector,
725 void *target_container, const void *data,
726 int nhoff, u8 ip_proto, int hlen)
727 {
728 enum flow_dissector_key_id dissector_ports = FLOW_DISSECTOR_KEY_MAX;
729 struct flow_dissector_key_ports *key_ports;
730
731 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS))
732 dissector_ports = FLOW_DISSECTOR_KEY_PORTS;
733 else if (dissector_uses_key(flow_dissector,
734 FLOW_DISSECTOR_KEY_PORTS_RANGE))
735 dissector_ports = FLOW_DISSECTOR_KEY_PORTS_RANGE;
736
737 if (dissector_ports == FLOW_DISSECTOR_KEY_MAX)
738 return;
739
740 key_ports = skb_flow_dissector_target(flow_dissector,
741 dissector_ports,
742 target_container);
743 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
744 data, hlen);
745 }
746
747 static void
__skb_flow_dissect_ipv4(const struct sk_buff * skb,struct flow_dissector * flow_dissector,void * target_container,const void * data,const struct iphdr * iph)748 __skb_flow_dissect_ipv4(const struct sk_buff *skb,
749 struct flow_dissector *flow_dissector,
750 void *target_container, const void *data,
751 const struct iphdr *iph)
752 {
753 struct flow_dissector_key_ip *key_ip;
754
755 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
756 return;
757
758 key_ip = skb_flow_dissector_target(flow_dissector,
759 FLOW_DISSECTOR_KEY_IP,
760 target_container);
761 key_ip->tos = iph->tos;
762 key_ip->ttl = iph->ttl;
763 }
764
765 static void
__skb_flow_dissect_ipv6(const struct sk_buff * skb,struct flow_dissector * flow_dissector,void * target_container,const void * data,const struct ipv6hdr * iph)766 __skb_flow_dissect_ipv6(const struct sk_buff *skb,
767 struct flow_dissector *flow_dissector,
768 void *target_container, const void *data,
769 const struct ipv6hdr *iph)
770 {
771 struct flow_dissector_key_ip *key_ip;
772
773 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
774 return;
775
776 key_ip = skb_flow_dissector_target(flow_dissector,
777 FLOW_DISSECTOR_KEY_IP,
778 target_container);
779 key_ip->tos = ipv6_get_dsfield(iph);
780 key_ip->ttl = iph->hop_limit;
781 }
782
783 /* Maximum number of protocol headers that can be parsed in
784 * __skb_flow_dissect
785 */
786 #define MAX_FLOW_DISSECT_HDRS 15
787
skb_flow_dissect_allowed(int * num_hdrs)788 static bool skb_flow_dissect_allowed(int *num_hdrs)
789 {
790 ++*num_hdrs;
791
792 return (*num_hdrs <= MAX_FLOW_DISSECT_HDRS);
793 }
794
__skb_flow_bpf_to_target(const struct bpf_flow_keys * flow_keys,struct flow_dissector * flow_dissector,void * target_container)795 static void __skb_flow_bpf_to_target(const struct bpf_flow_keys *flow_keys,
796 struct flow_dissector *flow_dissector,
797 void *target_container)
798 {
799 struct flow_dissector_key_ports *key_ports = NULL;
800 struct flow_dissector_key_control *key_control;
801 struct flow_dissector_key_basic *key_basic;
802 struct flow_dissector_key_addrs *key_addrs;
803 struct flow_dissector_key_tags *key_tags;
804
805 key_control = skb_flow_dissector_target(flow_dissector,
806 FLOW_DISSECTOR_KEY_CONTROL,
807 target_container);
808 key_control->thoff = flow_keys->thoff;
809 if (flow_keys->is_frag)
810 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
811 if (flow_keys->is_first_frag)
812 key_control->flags |= FLOW_DIS_FIRST_FRAG;
813 if (flow_keys->is_encap)
814 key_control->flags |= FLOW_DIS_ENCAPSULATION;
815
816 key_basic = skb_flow_dissector_target(flow_dissector,
817 FLOW_DISSECTOR_KEY_BASIC,
818 target_container);
819 key_basic->n_proto = flow_keys->n_proto;
820 key_basic->ip_proto = flow_keys->ip_proto;
821
822 if (flow_keys->addr_proto == ETH_P_IP &&
823 dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
824 key_addrs = skb_flow_dissector_target(flow_dissector,
825 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
826 target_container);
827 key_addrs->v4addrs.src = flow_keys->ipv4_src;
828 key_addrs->v4addrs.dst = flow_keys->ipv4_dst;
829 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
830 } else if (flow_keys->addr_proto == ETH_P_IPV6 &&
831 dissector_uses_key(flow_dissector,
832 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
833 key_addrs = skb_flow_dissector_target(flow_dissector,
834 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
835 target_container);
836 memcpy(&key_addrs->v6addrs.src, &flow_keys->ipv6_src,
837 sizeof(key_addrs->v6addrs.src));
838 memcpy(&key_addrs->v6addrs.dst, &flow_keys->ipv6_dst,
839 sizeof(key_addrs->v6addrs.dst));
840 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
841 }
842
843 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS))
844 key_ports = skb_flow_dissector_target(flow_dissector,
845 FLOW_DISSECTOR_KEY_PORTS,
846 target_container);
847 else if (dissector_uses_key(flow_dissector,
848 FLOW_DISSECTOR_KEY_PORTS_RANGE))
849 key_ports = skb_flow_dissector_target(flow_dissector,
850 FLOW_DISSECTOR_KEY_PORTS_RANGE,
851 target_container);
852
853 if (key_ports) {
854 key_ports->src = flow_keys->sport;
855 key_ports->dst = flow_keys->dport;
856 }
857
858 if (dissector_uses_key(flow_dissector,
859 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
860 key_tags = skb_flow_dissector_target(flow_dissector,
861 FLOW_DISSECTOR_KEY_FLOW_LABEL,
862 target_container);
863 key_tags->flow_label = ntohl(flow_keys->flow_label);
864 }
865 }
866
bpf_flow_dissect(struct bpf_prog * prog,struct bpf_flow_dissector * ctx,__be16 proto,int nhoff,int hlen,unsigned int flags)867 bool bpf_flow_dissect(struct bpf_prog *prog, struct bpf_flow_dissector *ctx,
868 __be16 proto, int nhoff, int hlen, unsigned int flags)
869 {
870 struct bpf_flow_keys *flow_keys = ctx->flow_keys;
871 u32 result;
872
873 /* Pass parameters to the BPF program */
874 memset(flow_keys, 0, sizeof(*flow_keys));
875 flow_keys->n_proto = proto;
876 flow_keys->nhoff = nhoff;
877 flow_keys->thoff = flow_keys->nhoff;
878
879 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG !=
880 (int)FLOW_DISSECTOR_F_PARSE_1ST_FRAG);
881 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL !=
882 (int)FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
883 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP !=
884 (int)FLOW_DISSECTOR_F_STOP_AT_ENCAP);
885 flow_keys->flags = flags;
886
887 result = bpf_prog_run_pin_on_cpu(prog, ctx);
888
889 flow_keys->nhoff = clamp_t(u16, flow_keys->nhoff, nhoff, hlen);
890 flow_keys->thoff = clamp_t(u16, flow_keys->thoff,
891 flow_keys->nhoff, hlen);
892
893 return result == BPF_OK;
894 }
895
896 /**
897 * __skb_flow_dissect - extract the flow_keys struct and return it
898 * @net: associated network namespace, derived from @skb if NULL
899 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
900 * @flow_dissector: list of keys to dissect
901 * @target_container: target structure to put dissected values into
902 * @data: raw buffer pointer to the packet, if NULL use skb->data
903 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
904 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
905 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
906 * @flags: flags that control the dissection process, e.g.
907 * FLOW_DISSECTOR_F_STOP_AT_ENCAP.
908 *
909 * The function will try to retrieve individual keys into target specified
910 * by flow_dissector from either the skbuff or a raw buffer specified by the
911 * rest parameters.
912 *
913 * Caller must take care of zeroing target container memory.
914 */
__skb_flow_dissect(const struct net * net,const struct sk_buff * skb,struct flow_dissector * flow_dissector,void * target_container,const void * data,__be16 proto,int nhoff,int hlen,unsigned int flags)915 bool __skb_flow_dissect(const struct net *net,
916 const struct sk_buff *skb,
917 struct flow_dissector *flow_dissector,
918 void *target_container, const void *data,
919 __be16 proto, int nhoff, int hlen, unsigned int flags)
920 {
921 struct flow_dissector_key_control *key_control;
922 struct flow_dissector_key_basic *key_basic;
923 struct flow_dissector_key_addrs *key_addrs;
924 struct flow_dissector_key_tags *key_tags;
925 struct flow_dissector_key_vlan *key_vlan;
926 enum flow_dissect_ret fdret;
927 enum flow_dissector_key_id dissector_vlan = FLOW_DISSECTOR_KEY_MAX;
928 bool mpls_el = false;
929 int mpls_lse = 0;
930 int num_hdrs = 0;
931 u8 ip_proto = 0;
932 bool ret;
933
934 if (!data) {
935 data = skb->data;
936 proto = skb_vlan_tag_present(skb) ?
937 skb->vlan_proto : skb->protocol;
938 nhoff = skb_network_offset(skb);
939 hlen = skb_headlen(skb);
940 #if IS_ENABLED(CONFIG_NET_DSA)
941 if (unlikely(skb->dev && netdev_uses_dsa(skb->dev) &&
942 proto == htons(ETH_P_XDSA))) {
943 const struct dsa_device_ops *ops;
944 int offset = 0;
945
946 ops = skb->dev->dsa_ptr->tag_ops;
947 /* Only DSA header taggers break flow dissection */
948 if (ops->needed_headroom) {
949 if (ops->flow_dissect)
950 ops->flow_dissect(skb, &proto, &offset);
951 else
952 dsa_tag_generic_flow_dissect(skb,
953 &proto,
954 &offset);
955 hlen -= offset;
956 nhoff += offset;
957 }
958 }
959 #endif
960 }
961
962 /* It is ensured by skb_flow_dissector_init() that control key will
963 * be always present.
964 */
965 key_control = skb_flow_dissector_target(flow_dissector,
966 FLOW_DISSECTOR_KEY_CONTROL,
967 target_container);
968
969 /* It is ensured by skb_flow_dissector_init() that basic key will
970 * be always present.
971 */
972 key_basic = skb_flow_dissector_target(flow_dissector,
973 FLOW_DISSECTOR_KEY_BASIC,
974 target_container);
975
976 if (skb) {
977 if (!net) {
978 if (skb->dev)
979 net = dev_net(skb->dev);
980 else if (skb->sk)
981 net = sock_net(skb->sk);
982 }
983 }
984
985 WARN_ON_ONCE(!net);
986 if (net) {
987 enum netns_bpf_attach_type type = NETNS_BPF_FLOW_DISSECTOR;
988 struct bpf_prog_array *run_array;
989
990 rcu_read_lock();
991 run_array = rcu_dereference(init_net.bpf.run_array[type]);
992 if (!run_array)
993 run_array = rcu_dereference(net->bpf.run_array[type]);
994
995 if (run_array) {
996 struct bpf_flow_keys flow_keys;
997 struct bpf_flow_dissector ctx = {
998 .flow_keys = &flow_keys,
999 .data = data,
1000 .data_end = data + hlen,
1001 };
1002 __be16 n_proto = proto;
1003 struct bpf_prog *prog;
1004
1005 if (skb) {
1006 ctx.skb = skb;
1007 /* we can't use 'proto' in the skb case
1008 * because it might be set to skb->vlan_proto
1009 * which has been pulled from the data
1010 */
1011 n_proto = skb->protocol;
1012 }
1013
1014 prog = READ_ONCE(run_array->items[0].prog);
1015 ret = bpf_flow_dissect(prog, &ctx, n_proto, nhoff,
1016 hlen, flags);
1017 __skb_flow_bpf_to_target(&flow_keys, flow_dissector,
1018 target_container);
1019 rcu_read_unlock();
1020 return ret;
1021 }
1022 rcu_read_unlock();
1023 }
1024
1025 if (dissector_uses_key(flow_dissector,
1026 FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1027 struct ethhdr *eth = eth_hdr(skb);
1028 struct flow_dissector_key_eth_addrs *key_eth_addrs;
1029
1030 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
1031 FLOW_DISSECTOR_KEY_ETH_ADDRS,
1032 target_container);
1033 memcpy(key_eth_addrs, ð->h_dest, sizeof(*key_eth_addrs));
1034 }
1035
1036 proto_again:
1037 fdret = FLOW_DISSECT_RET_CONTINUE;
1038
1039 switch (proto) {
1040 case htons(ETH_P_IP): {
1041 const struct iphdr *iph;
1042 struct iphdr _iph;
1043
1044 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
1045 if (!iph || iph->ihl < 5) {
1046 fdret = FLOW_DISSECT_RET_OUT_BAD;
1047 break;
1048 }
1049
1050 nhoff += iph->ihl * 4;
1051
1052 ip_proto = iph->protocol;
1053
1054 if (dissector_uses_key(flow_dissector,
1055 FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
1056 key_addrs = skb_flow_dissector_target(flow_dissector,
1057 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1058 target_container);
1059
1060 memcpy(&key_addrs->v4addrs.src, &iph->saddr,
1061 sizeof(key_addrs->v4addrs.src));
1062 memcpy(&key_addrs->v4addrs.dst, &iph->daddr,
1063 sizeof(key_addrs->v4addrs.dst));
1064 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1065 }
1066
1067 __skb_flow_dissect_ipv4(skb, flow_dissector,
1068 target_container, data, iph);
1069
1070 if (ip_is_fragment(iph)) {
1071 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
1072
1073 if (iph->frag_off & htons(IP_OFFSET)) {
1074 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1075 break;
1076 } else {
1077 key_control->flags |= FLOW_DIS_FIRST_FRAG;
1078 if (!(flags &
1079 FLOW_DISSECTOR_F_PARSE_1ST_FRAG)) {
1080 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1081 break;
1082 }
1083 }
1084 }
1085
1086 break;
1087 }
1088 case htons(ETH_P_IPV6): {
1089 const struct ipv6hdr *iph;
1090 struct ipv6hdr _iph;
1091
1092 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
1093 if (!iph) {
1094 fdret = FLOW_DISSECT_RET_OUT_BAD;
1095 break;
1096 }
1097
1098 ip_proto = iph->nexthdr;
1099 nhoff += sizeof(struct ipv6hdr);
1100
1101 if (dissector_uses_key(flow_dissector,
1102 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
1103 key_addrs = skb_flow_dissector_target(flow_dissector,
1104 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1105 target_container);
1106
1107 memcpy(&key_addrs->v6addrs.src, &iph->saddr,
1108 sizeof(key_addrs->v6addrs.src));
1109 memcpy(&key_addrs->v6addrs.dst, &iph->daddr,
1110 sizeof(key_addrs->v6addrs.dst));
1111 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1112 }
1113
1114 if ((dissector_uses_key(flow_dissector,
1115 FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
1116 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
1117 ip6_flowlabel(iph)) {
1118 __be32 flow_label = ip6_flowlabel(iph);
1119
1120 if (dissector_uses_key(flow_dissector,
1121 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
1122 key_tags = skb_flow_dissector_target(flow_dissector,
1123 FLOW_DISSECTOR_KEY_FLOW_LABEL,
1124 target_container);
1125 key_tags->flow_label = ntohl(flow_label);
1126 }
1127 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL) {
1128 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1129 break;
1130 }
1131 }
1132
1133 __skb_flow_dissect_ipv6(skb, flow_dissector,
1134 target_container, data, iph);
1135
1136 break;
1137 }
1138 case htons(ETH_P_8021AD):
1139 case htons(ETH_P_8021Q): {
1140 const struct vlan_hdr *vlan = NULL;
1141 struct vlan_hdr _vlan;
1142 __be16 saved_vlan_tpid = proto;
1143
1144 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX &&
1145 skb && skb_vlan_tag_present(skb)) {
1146 proto = skb->protocol;
1147 } else {
1148 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
1149 data, hlen, &_vlan);
1150 if (!vlan) {
1151 fdret = FLOW_DISSECT_RET_OUT_BAD;
1152 break;
1153 }
1154
1155 proto = vlan->h_vlan_encapsulated_proto;
1156 nhoff += sizeof(*vlan);
1157 }
1158
1159 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX) {
1160 dissector_vlan = FLOW_DISSECTOR_KEY_VLAN;
1161 } else if (dissector_vlan == FLOW_DISSECTOR_KEY_VLAN) {
1162 dissector_vlan = FLOW_DISSECTOR_KEY_CVLAN;
1163 } else {
1164 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1165 break;
1166 }
1167
1168 if (dissector_uses_key(flow_dissector, dissector_vlan)) {
1169 key_vlan = skb_flow_dissector_target(flow_dissector,
1170 dissector_vlan,
1171 target_container);
1172
1173 if (!vlan) {
1174 key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
1175 key_vlan->vlan_priority = skb_vlan_tag_get_prio(skb);
1176 } else {
1177 key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) &
1178 VLAN_VID_MASK;
1179 key_vlan->vlan_priority =
1180 (ntohs(vlan->h_vlan_TCI) &
1181 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
1182 }
1183 key_vlan->vlan_tpid = saved_vlan_tpid;
1184 }
1185
1186 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1187 break;
1188 }
1189 case htons(ETH_P_PPP_SES): {
1190 struct {
1191 struct pppoe_hdr hdr;
1192 __be16 proto;
1193 } *hdr, _hdr;
1194 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
1195 if (!hdr) {
1196 fdret = FLOW_DISSECT_RET_OUT_BAD;
1197 break;
1198 }
1199
1200 nhoff += PPPOE_SES_HLEN;
1201 switch (hdr->proto) {
1202 case htons(PPP_IP):
1203 proto = htons(ETH_P_IP);
1204 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1205 break;
1206 case htons(PPP_IPV6):
1207 proto = htons(ETH_P_IPV6);
1208 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1209 break;
1210 default:
1211 fdret = FLOW_DISSECT_RET_OUT_BAD;
1212 break;
1213 }
1214 break;
1215 }
1216 case htons(ETH_P_TIPC): {
1217 struct tipc_basic_hdr *hdr, _hdr;
1218
1219 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr),
1220 data, hlen, &_hdr);
1221 if (!hdr) {
1222 fdret = FLOW_DISSECT_RET_OUT_BAD;
1223 break;
1224 }
1225
1226 if (dissector_uses_key(flow_dissector,
1227 FLOW_DISSECTOR_KEY_TIPC)) {
1228 key_addrs = skb_flow_dissector_target(flow_dissector,
1229 FLOW_DISSECTOR_KEY_TIPC,
1230 target_container);
1231 key_addrs->tipckey.key = tipc_hdr_rps_key(hdr);
1232 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC;
1233 }
1234 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1235 break;
1236 }
1237
1238 case htons(ETH_P_MPLS_UC):
1239 case htons(ETH_P_MPLS_MC):
1240 fdret = __skb_flow_dissect_mpls(skb, flow_dissector,
1241 target_container, data,
1242 nhoff, hlen, mpls_lse,
1243 &mpls_el);
1244 nhoff += sizeof(struct mpls_label);
1245 mpls_lse++;
1246 break;
1247 case htons(ETH_P_FCOE):
1248 if ((hlen - nhoff) < FCOE_HEADER_LEN) {
1249 fdret = FLOW_DISSECT_RET_OUT_BAD;
1250 break;
1251 }
1252
1253 nhoff += FCOE_HEADER_LEN;
1254 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1255 break;
1256
1257 case htons(ETH_P_ARP):
1258 case htons(ETH_P_RARP):
1259 fdret = __skb_flow_dissect_arp(skb, flow_dissector,
1260 target_container, data,
1261 nhoff, hlen);
1262 break;
1263
1264 case htons(ETH_P_BATMAN):
1265 fdret = __skb_flow_dissect_batadv(skb, key_control, data,
1266 &proto, &nhoff, hlen, flags);
1267 break;
1268
1269 case htons(ETH_P_1588): {
1270 struct ptp_header *hdr, _hdr;
1271
1272 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
1273 hlen, &_hdr);
1274 if (!hdr) {
1275 fdret = FLOW_DISSECT_RET_OUT_BAD;
1276 break;
1277 }
1278
1279 nhoff += ntohs(hdr->message_length);
1280 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1281 break;
1282 }
1283
1284 default:
1285 fdret = FLOW_DISSECT_RET_OUT_BAD;
1286 break;
1287 }
1288
1289 /* Process result of proto processing */
1290 switch (fdret) {
1291 case FLOW_DISSECT_RET_OUT_GOOD:
1292 goto out_good;
1293 case FLOW_DISSECT_RET_PROTO_AGAIN:
1294 if (skb_flow_dissect_allowed(&num_hdrs))
1295 goto proto_again;
1296 goto out_good;
1297 case FLOW_DISSECT_RET_CONTINUE:
1298 case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1299 break;
1300 case FLOW_DISSECT_RET_OUT_BAD:
1301 default:
1302 goto out_bad;
1303 }
1304
1305 ip_proto_again:
1306 fdret = FLOW_DISSECT_RET_CONTINUE;
1307
1308 switch (ip_proto) {
1309 case IPPROTO_GRE:
1310 if (flags & FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP) {
1311 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1312 break;
1313 }
1314
1315 fdret = __skb_flow_dissect_gre(skb, key_control, flow_dissector,
1316 target_container, data,
1317 &proto, &nhoff, &hlen, flags);
1318 break;
1319
1320 case NEXTHDR_HOP:
1321 case NEXTHDR_ROUTING:
1322 case NEXTHDR_DEST: {
1323 u8 _opthdr[2], *opthdr;
1324
1325 if (proto != htons(ETH_P_IPV6))
1326 break;
1327
1328 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
1329 data, hlen, &_opthdr);
1330 if (!opthdr) {
1331 fdret = FLOW_DISSECT_RET_OUT_BAD;
1332 break;
1333 }
1334
1335 ip_proto = opthdr[0];
1336 nhoff += (opthdr[1] + 1) << 3;
1337
1338 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1339 break;
1340 }
1341 case NEXTHDR_FRAGMENT: {
1342 struct frag_hdr _fh, *fh;
1343
1344 if (proto != htons(ETH_P_IPV6))
1345 break;
1346
1347 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
1348 data, hlen, &_fh);
1349
1350 if (!fh) {
1351 fdret = FLOW_DISSECT_RET_OUT_BAD;
1352 break;
1353 }
1354
1355 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
1356
1357 nhoff += sizeof(_fh);
1358 ip_proto = fh->nexthdr;
1359
1360 if (!(fh->frag_off & htons(IP6_OFFSET))) {
1361 key_control->flags |= FLOW_DIS_FIRST_FRAG;
1362 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) {
1363 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1364 break;
1365 }
1366 }
1367
1368 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1369 break;
1370 }
1371 case IPPROTO_IPIP:
1372 if (flags & FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP) {
1373 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1374 break;
1375 }
1376
1377 proto = htons(ETH_P_IP);
1378
1379 key_control->flags |= FLOW_DIS_ENCAPSULATION;
1380 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1381 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1382 break;
1383 }
1384
1385 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1386 break;
1387
1388 case IPPROTO_IPV6:
1389 if (flags & FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP) {
1390 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1391 break;
1392 }
1393
1394 proto = htons(ETH_P_IPV6);
1395
1396 key_control->flags |= FLOW_DIS_ENCAPSULATION;
1397 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1398 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1399 break;
1400 }
1401
1402 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1403 break;
1404
1405
1406 case IPPROTO_MPLS:
1407 proto = htons(ETH_P_MPLS_UC);
1408 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1409 break;
1410
1411 case IPPROTO_TCP:
1412 __skb_flow_dissect_tcp(skb, flow_dissector, target_container,
1413 data, nhoff, hlen);
1414 break;
1415
1416 case IPPROTO_ICMP:
1417 case IPPROTO_ICMPV6:
1418 __skb_flow_dissect_icmp(skb, flow_dissector, target_container,
1419 data, nhoff, hlen);
1420 break;
1421
1422 default:
1423 break;
1424 }
1425
1426 if (!(key_control->flags & FLOW_DIS_IS_FRAGMENT))
1427 __skb_flow_dissect_ports(skb, flow_dissector, target_container,
1428 data, nhoff, ip_proto, hlen);
1429
1430 /* Process result of IP proto processing */
1431 switch (fdret) {
1432 case FLOW_DISSECT_RET_PROTO_AGAIN:
1433 if (skb_flow_dissect_allowed(&num_hdrs))
1434 goto proto_again;
1435 break;
1436 case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1437 if (skb_flow_dissect_allowed(&num_hdrs))
1438 goto ip_proto_again;
1439 break;
1440 case FLOW_DISSECT_RET_OUT_GOOD:
1441 case FLOW_DISSECT_RET_CONTINUE:
1442 break;
1443 case FLOW_DISSECT_RET_OUT_BAD:
1444 default:
1445 goto out_bad;
1446 }
1447
1448 out_good:
1449 ret = true;
1450
1451 out:
1452 key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
1453 key_basic->n_proto = proto;
1454 key_basic->ip_proto = ip_proto;
1455
1456 return ret;
1457
1458 out_bad:
1459 ret = false;
1460 goto out;
1461 }
1462 EXPORT_SYMBOL(__skb_flow_dissect);
1463
1464 static siphash_key_t hashrnd __read_mostly;
__flow_hash_secret_init(void)1465 static __always_inline void __flow_hash_secret_init(void)
1466 {
1467 net_get_random_once(&hashrnd, sizeof(hashrnd));
1468 }
1469
flow_keys_hash_start(const struct flow_keys * flow)1470 static const void *flow_keys_hash_start(const struct flow_keys *flow)
1471 {
1472 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % SIPHASH_ALIGNMENT);
1473 return &flow->FLOW_KEYS_HASH_START_FIELD;
1474 }
1475
flow_keys_hash_length(const struct flow_keys * flow)1476 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
1477 {
1478 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
1479
1480 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
1481
1482 switch (flow->control.addr_type) {
1483 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1484 diff -= sizeof(flow->addrs.v4addrs);
1485 break;
1486 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1487 diff -= sizeof(flow->addrs.v6addrs);
1488 break;
1489 case FLOW_DISSECTOR_KEY_TIPC:
1490 diff -= sizeof(flow->addrs.tipckey);
1491 break;
1492 }
1493 return sizeof(*flow) - diff;
1494 }
1495
flow_get_u32_src(const struct flow_keys * flow)1496 __be32 flow_get_u32_src(const struct flow_keys *flow)
1497 {
1498 switch (flow->control.addr_type) {
1499 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1500 return flow->addrs.v4addrs.src;
1501 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1502 return (__force __be32)ipv6_addr_hash(
1503 &flow->addrs.v6addrs.src);
1504 case FLOW_DISSECTOR_KEY_TIPC:
1505 return flow->addrs.tipckey.key;
1506 default:
1507 return 0;
1508 }
1509 }
1510 EXPORT_SYMBOL(flow_get_u32_src);
1511
flow_get_u32_dst(const struct flow_keys * flow)1512 __be32 flow_get_u32_dst(const struct flow_keys *flow)
1513 {
1514 switch (flow->control.addr_type) {
1515 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1516 return flow->addrs.v4addrs.dst;
1517 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1518 return (__force __be32)ipv6_addr_hash(
1519 &flow->addrs.v6addrs.dst);
1520 default:
1521 return 0;
1522 }
1523 }
1524 EXPORT_SYMBOL(flow_get_u32_dst);
1525
1526 /* Sort the source and destination IP and the ports,
1527 * to have consistent hash within the two directions
1528 */
__flow_hash_consistentify(struct flow_keys * keys)1529 static inline void __flow_hash_consistentify(struct flow_keys *keys)
1530 {
1531 int addr_diff, i;
1532
1533 switch (keys->control.addr_type) {
1534 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1535 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
1536 (__force u32)keys->addrs.v4addrs.src;
1537 if (addr_diff < 0)
1538 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
1539
1540 if ((__force u16)keys->ports.dst <
1541 (__force u16)keys->ports.src) {
1542 swap(keys->ports.src, keys->ports.dst);
1543 }
1544 break;
1545 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1546 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
1547 &keys->addrs.v6addrs.src,
1548 sizeof(keys->addrs.v6addrs.dst));
1549 if (addr_diff < 0) {
1550 for (i = 0; i < 4; i++)
1551 swap(keys->addrs.v6addrs.src.s6_addr32[i],
1552 keys->addrs.v6addrs.dst.s6_addr32[i]);
1553 }
1554 if ((__force u16)keys->ports.dst <
1555 (__force u16)keys->ports.src) {
1556 swap(keys->ports.src, keys->ports.dst);
1557 }
1558 break;
1559 }
1560 }
1561
__flow_hash_from_keys(struct flow_keys * keys,const siphash_key_t * keyval)1562 static inline u32 __flow_hash_from_keys(struct flow_keys *keys,
1563 const siphash_key_t *keyval)
1564 {
1565 u32 hash;
1566
1567 __flow_hash_consistentify(keys);
1568
1569 hash = siphash(flow_keys_hash_start(keys),
1570 flow_keys_hash_length(keys), keyval);
1571 if (!hash)
1572 hash = 1;
1573
1574 return hash;
1575 }
1576
flow_hash_from_keys(struct flow_keys * keys)1577 u32 flow_hash_from_keys(struct flow_keys *keys)
1578 {
1579 __flow_hash_secret_init();
1580 return __flow_hash_from_keys(keys, &hashrnd);
1581 }
1582 EXPORT_SYMBOL(flow_hash_from_keys);
1583
___skb_get_hash(const struct sk_buff * skb,struct flow_keys * keys,const siphash_key_t * keyval)1584 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
1585 struct flow_keys *keys,
1586 const siphash_key_t *keyval)
1587 {
1588 skb_flow_dissect_flow_keys(skb, keys,
1589 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1590
1591 return __flow_hash_from_keys(keys, keyval);
1592 }
1593
1594 struct _flow_keys_digest_data {
1595 __be16 n_proto;
1596 u8 ip_proto;
1597 u8 padding;
1598 __be32 ports;
1599 __be32 src;
1600 __be32 dst;
1601 };
1602
make_flow_keys_digest(struct flow_keys_digest * digest,const struct flow_keys * flow)1603 void make_flow_keys_digest(struct flow_keys_digest *digest,
1604 const struct flow_keys *flow)
1605 {
1606 struct _flow_keys_digest_data *data =
1607 (struct _flow_keys_digest_data *)digest;
1608
1609 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
1610
1611 memset(digest, 0, sizeof(*digest));
1612
1613 data->n_proto = flow->basic.n_proto;
1614 data->ip_proto = flow->basic.ip_proto;
1615 data->ports = flow->ports.ports;
1616 data->src = flow->addrs.v4addrs.src;
1617 data->dst = flow->addrs.v4addrs.dst;
1618 }
1619 EXPORT_SYMBOL(make_flow_keys_digest);
1620
1621 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
1622
__skb_get_hash_symmetric(const struct sk_buff * skb)1623 u32 __skb_get_hash_symmetric(const struct sk_buff *skb)
1624 {
1625 struct flow_keys keys;
1626
1627 __flow_hash_secret_init();
1628
1629 memset(&keys, 0, sizeof(keys));
1630 __skb_flow_dissect(NULL, skb, &flow_keys_dissector_symmetric,
1631 &keys, NULL, 0, 0, 0,
1632 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1633
1634 return __flow_hash_from_keys(&keys, &hashrnd);
1635 }
1636 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
1637
1638 /**
1639 * __skb_get_hash: calculate a flow hash
1640 * @skb: sk_buff to calculate flow hash from
1641 *
1642 * This function calculates a flow hash based on src/dst addresses
1643 * and src/dst port numbers. Sets hash in skb to non-zero hash value
1644 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
1645 * if hash is a canonical 4-tuple hash over transport ports.
1646 */
__skb_get_hash(struct sk_buff * skb)1647 void __skb_get_hash(struct sk_buff *skb)
1648 {
1649 struct flow_keys keys;
1650 u32 hash;
1651
1652 __flow_hash_secret_init();
1653
1654 hash = ___skb_get_hash(skb, &keys, &hashrnd);
1655
1656 __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1657 }
1658 EXPORT_SYMBOL(__skb_get_hash);
1659
skb_get_hash_perturb(const struct sk_buff * skb,const siphash_key_t * perturb)1660 __u32 skb_get_hash_perturb(const struct sk_buff *skb,
1661 const siphash_key_t *perturb)
1662 {
1663 struct flow_keys keys;
1664
1665 return ___skb_get_hash(skb, &keys, perturb);
1666 }
1667 EXPORT_SYMBOL(skb_get_hash_perturb);
1668
__skb_get_poff(const struct sk_buff * skb,const void * data,const struct flow_keys_basic * keys,int hlen)1669 u32 __skb_get_poff(const struct sk_buff *skb, const void *data,
1670 const struct flow_keys_basic *keys, int hlen)
1671 {
1672 u32 poff = keys->control.thoff;
1673
1674 /* skip L4 headers for fragments after the first */
1675 if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
1676 !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
1677 return poff;
1678
1679 switch (keys->basic.ip_proto) {
1680 case IPPROTO_TCP: {
1681 /* access doff as u8 to avoid unaligned access */
1682 const u8 *doff;
1683 u8 _doff;
1684
1685 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
1686 data, hlen, &_doff);
1687 if (!doff)
1688 return poff;
1689
1690 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
1691 break;
1692 }
1693 case IPPROTO_UDP:
1694 case IPPROTO_UDPLITE:
1695 poff += sizeof(struct udphdr);
1696 break;
1697 /* For the rest, we do not really care about header
1698 * extensions at this point for now.
1699 */
1700 case IPPROTO_ICMP:
1701 poff += sizeof(struct icmphdr);
1702 break;
1703 case IPPROTO_ICMPV6:
1704 poff += sizeof(struct icmp6hdr);
1705 break;
1706 case IPPROTO_IGMP:
1707 poff += sizeof(struct igmphdr);
1708 break;
1709 case IPPROTO_DCCP:
1710 poff += sizeof(struct dccp_hdr);
1711 break;
1712 case IPPROTO_SCTP:
1713 poff += sizeof(struct sctphdr);
1714 break;
1715 }
1716
1717 return poff;
1718 }
1719
1720 /**
1721 * skb_get_poff - get the offset to the payload
1722 * @skb: sk_buff to get the payload offset from
1723 *
1724 * The function will get the offset to the payload as far as it could
1725 * be dissected. The main user is currently BPF, so that we can dynamically
1726 * truncate packets without needing to push actual payload to the user
1727 * space and can analyze headers only, instead.
1728 */
skb_get_poff(const struct sk_buff * skb)1729 u32 skb_get_poff(const struct sk_buff *skb)
1730 {
1731 struct flow_keys_basic keys;
1732
1733 if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys,
1734 NULL, 0, 0, 0, 0))
1735 return 0;
1736
1737 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
1738 }
1739
__get_hash_from_flowi6(const struct flowi6 * fl6,struct flow_keys * keys)1740 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
1741 {
1742 memset(keys, 0, sizeof(*keys));
1743
1744 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
1745 sizeof(keys->addrs.v6addrs.src));
1746 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
1747 sizeof(keys->addrs.v6addrs.dst));
1748 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1749 keys->ports.src = fl6->fl6_sport;
1750 keys->ports.dst = fl6->fl6_dport;
1751 keys->keyid.keyid = fl6->fl6_gre_key;
1752 keys->tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
1753 keys->basic.ip_proto = fl6->flowi6_proto;
1754
1755 return flow_hash_from_keys(keys);
1756 }
1757 EXPORT_SYMBOL(__get_hash_from_flowi6);
1758
1759 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
1760 {
1761 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1762 .offset = offsetof(struct flow_keys, control),
1763 },
1764 {
1765 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1766 .offset = offsetof(struct flow_keys, basic),
1767 },
1768 {
1769 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1770 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1771 },
1772 {
1773 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1774 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1775 },
1776 {
1777 .key_id = FLOW_DISSECTOR_KEY_TIPC,
1778 .offset = offsetof(struct flow_keys, addrs.tipckey),
1779 },
1780 {
1781 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1782 .offset = offsetof(struct flow_keys, ports),
1783 },
1784 {
1785 .key_id = FLOW_DISSECTOR_KEY_VLAN,
1786 .offset = offsetof(struct flow_keys, vlan),
1787 },
1788 {
1789 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
1790 .offset = offsetof(struct flow_keys, tags),
1791 },
1792 {
1793 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
1794 .offset = offsetof(struct flow_keys, keyid),
1795 },
1796 };
1797
1798 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
1799 {
1800 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1801 .offset = offsetof(struct flow_keys, control),
1802 },
1803 {
1804 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1805 .offset = offsetof(struct flow_keys, basic),
1806 },
1807 {
1808 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1809 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1810 },
1811 {
1812 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1813 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1814 },
1815 {
1816 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1817 .offset = offsetof(struct flow_keys, ports),
1818 },
1819 };
1820
1821 static const struct flow_dissector_key flow_keys_basic_dissector_keys[] = {
1822 {
1823 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1824 .offset = offsetof(struct flow_keys, control),
1825 },
1826 {
1827 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1828 .offset = offsetof(struct flow_keys, basic),
1829 },
1830 };
1831
1832 struct flow_dissector flow_keys_dissector __read_mostly;
1833 EXPORT_SYMBOL(flow_keys_dissector);
1834
1835 struct flow_dissector flow_keys_basic_dissector __read_mostly;
1836 EXPORT_SYMBOL(flow_keys_basic_dissector);
1837
init_default_flow_dissectors(void)1838 static int __init init_default_flow_dissectors(void)
1839 {
1840 skb_flow_dissector_init(&flow_keys_dissector,
1841 flow_keys_dissector_keys,
1842 ARRAY_SIZE(flow_keys_dissector_keys));
1843 skb_flow_dissector_init(&flow_keys_dissector_symmetric,
1844 flow_keys_dissector_symmetric_keys,
1845 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
1846 skb_flow_dissector_init(&flow_keys_basic_dissector,
1847 flow_keys_basic_dissector_keys,
1848 ARRAY_SIZE(flow_keys_basic_dissector_keys));
1849 return 0;
1850 }
1851 core_initcall(init_default_flow_dissectors);
1852