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