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