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 ? ntohs(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 = READ_ONCE(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.src, &flow_keys->ipv6_src,
826 sizeof(key_addrs->v6addrs.src));
827 memcpy(&key_addrs->v6addrs.dst, &flow_keys->ipv6_dst,
828 sizeof(key_addrs->v6addrs.dst));
829 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
830 }
831
832 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS))
833 key_ports = skb_flow_dissector_target(flow_dissector,
834 FLOW_DISSECTOR_KEY_PORTS,
835 target_container);
836 else if (dissector_uses_key(flow_dissector,
837 FLOW_DISSECTOR_KEY_PORTS_RANGE))
838 key_ports = skb_flow_dissector_target(flow_dissector,
839 FLOW_DISSECTOR_KEY_PORTS_RANGE,
840 target_container);
841
842 if (key_ports) {
843 key_ports->src = flow_keys->sport;
844 key_ports->dst = flow_keys->dport;
845 }
846
847 if (dissector_uses_key(flow_dissector,
848 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
849 key_tags = skb_flow_dissector_target(flow_dissector,
850 FLOW_DISSECTOR_KEY_FLOW_LABEL,
851 target_container);
852 key_tags->flow_label = ntohl(flow_keys->flow_label);
853 }
854 }
855
bpf_flow_dissect(struct bpf_prog * prog,struct bpf_flow_dissector * ctx,__be16 proto,int nhoff,int hlen,unsigned int flags)856 bool bpf_flow_dissect(struct bpf_prog *prog, struct bpf_flow_dissector *ctx,
857 __be16 proto, int nhoff, int hlen, unsigned int flags)
858 {
859 struct bpf_flow_keys *flow_keys = ctx->flow_keys;
860 u32 result;
861
862 /* Pass parameters to the BPF program */
863 memset(flow_keys, 0, sizeof(*flow_keys));
864 flow_keys->n_proto = proto;
865 flow_keys->nhoff = nhoff;
866 flow_keys->thoff = flow_keys->nhoff;
867
868 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG !=
869 (int)FLOW_DISSECTOR_F_PARSE_1ST_FRAG);
870 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL !=
871 (int)FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
872 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP !=
873 (int)FLOW_DISSECTOR_F_STOP_AT_ENCAP);
874 flow_keys->flags = flags;
875
876 result = bpf_prog_run_pin_on_cpu(prog, ctx);
877
878 flow_keys->nhoff = clamp_t(u16, flow_keys->nhoff, nhoff, hlen);
879 flow_keys->thoff = clamp_t(u16, flow_keys->thoff,
880 flow_keys->nhoff, hlen);
881
882 return result == BPF_OK;
883 }
884
885 /**
886 * __skb_flow_dissect - extract the flow_keys struct and return it
887 * @net: associated network namespace, derived from @skb if NULL
888 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
889 * @flow_dissector: list of keys to dissect
890 * @target_container: target structure to put dissected values into
891 * @data: raw buffer pointer to the packet, if NULL use skb->data
892 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
893 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
894 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
895 * @flags: flags that control the dissection process, e.g.
896 * FLOW_DISSECTOR_F_STOP_AT_ENCAP.
897 *
898 * The function will try to retrieve individual keys into target specified
899 * by flow_dissector from either the skbuff or a raw buffer specified by the
900 * rest parameters.
901 *
902 * Caller must take care of zeroing target container memory.
903 */
__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)904 bool __skb_flow_dissect(const struct net *net,
905 const struct sk_buff *skb,
906 struct flow_dissector *flow_dissector,
907 void *target_container,
908 void *data, __be16 proto, int nhoff, int hlen,
909 unsigned int flags)
910 {
911 struct flow_dissector_key_control *key_control;
912 struct flow_dissector_key_basic *key_basic;
913 struct flow_dissector_key_addrs *key_addrs;
914 struct flow_dissector_key_tags *key_tags;
915 struct flow_dissector_key_vlan *key_vlan;
916 enum flow_dissect_ret fdret;
917 enum flow_dissector_key_id dissector_vlan = FLOW_DISSECTOR_KEY_MAX;
918 bool mpls_el = false;
919 int mpls_lse = 0;
920 int num_hdrs = 0;
921 u8 ip_proto = 0;
922 bool ret;
923
924 if (!data) {
925 data = skb->data;
926 proto = skb_vlan_tag_present(skb) ?
927 skb->vlan_proto : skb->protocol;
928 nhoff = skb_network_offset(skb);
929 hlen = skb_headlen(skb);
930 #if IS_ENABLED(CONFIG_NET_DSA)
931 if (unlikely(skb->dev && netdev_uses_dsa(skb->dev) &&
932 proto == htons(ETH_P_XDSA))) {
933 const struct dsa_device_ops *ops;
934 int offset = 0;
935
936 ops = skb->dev->dsa_ptr->tag_ops;
937 /* Tail taggers don't break flow dissection */
938 if (!ops->tail_tag) {
939 if (ops->flow_dissect)
940 ops->flow_dissect(skb, &proto, &offset);
941 else
942 dsa_tag_generic_flow_dissect(skb,
943 &proto,
944 &offset);
945 hlen -= offset;
946 nhoff += offset;
947 }
948 }
949 #endif
950 }
951
952 /* It is ensured by skb_flow_dissector_init() that control key will
953 * be always present.
954 */
955 key_control = skb_flow_dissector_target(flow_dissector,
956 FLOW_DISSECTOR_KEY_CONTROL,
957 target_container);
958
959 /* It is ensured by skb_flow_dissector_init() that basic key will
960 * be always present.
961 */
962 key_basic = skb_flow_dissector_target(flow_dissector,
963 FLOW_DISSECTOR_KEY_BASIC,
964 target_container);
965
966 if (skb) {
967 if (!net) {
968 if (skb->dev)
969 net = dev_net(skb->dev);
970 else if (skb->sk)
971 net = sock_net(skb->sk);
972 }
973 }
974
975 WARN_ON_ONCE(!net);
976 if (net) {
977 enum netns_bpf_attach_type type = NETNS_BPF_FLOW_DISSECTOR;
978 struct bpf_prog_array *run_array;
979
980 rcu_read_lock();
981 run_array = rcu_dereference(init_net.bpf.run_array[type]);
982 if (!run_array)
983 run_array = rcu_dereference(net->bpf.run_array[type]);
984
985 if (run_array) {
986 struct bpf_flow_keys flow_keys;
987 struct bpf_flow_dissector ctx = {
988 .flow_keys = &flow_keys,
989 .data = data,
990 .data_end = data + hlen,
991 };
992 __be16 n_proto = proto;
993 struct bpf_prog *prog;
994
995 if (skb) {
996 ctx.skb = skb;
997 /* we can't use 'proto' in the skb case
998 * because it might be set to skb->vlan_proto
999 * which has been pulled from the data
1000 */
1001 n_proto = skb->protocol;
1002 }
1003
1004 prog = READ_ONCE(run_array->items[0].prog);
1005 ret = bpf_flow_dissect(prog, &ctx, n_proto, nhoff,
1006 hlen, flags);
1007 __skb_flow_bpf_to_target(&flow_keys, flow_dissector,
1008 target_container);
1009 rcu_read_unlock();
1010 return ret;
1011 }
1012 rcu_read_unlock();
1013 }
1014
1015 if (dissector_uses_key(flow_dissector,
1016 FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1017 struct ethhdr *eth = eth_hdr(skb);
1018 struct flow_dissector_key_eth_addrs *key_eth_addrs;
1019
1020 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
1021 FLOW_DISSECTOR_KEY_ETH_ADDRS,
1022 target_container);
1023 memcpy(key_eth_addrs, ð->h_dest, sizeof(*key_eth_addrs));
1024 }
1025
1026 proto_again:
1027 fdret = FLOW_DISSECT_RET_CONTINUE;
1028
1029 switch (proto) {
1030 case htons(ETH_P_IP): {
1031 const struct iphdr *iph;
1032 struct iphdr _iph;
1033
1034 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
1035 if (!iph || iph->ihl < 5) {
1036 fdret = FLOW_DISSECT_RET_OUT_BAD;
1037 break;
1038 }
1039
1040 nhoff += iph->ihl * 4;
1041
1042 ip_proto = iph->protocol;
1043
1044 if (dissector_uses_key(flow_dissector,
1045 FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
1046 key_addrs = skb_flow_dissector_target(flow_dissector,
1047 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1048 target_container);
1049
1050 memcpy(&key_addrs->v4addrs.src, &iph->saddr,
1051 sizeof(key_addrs->v4addrs.src));
1052 memcpy(&key_addrs->v4addrs.dst, &iph->daddr,
1053 sizeof(key_addrs->v4addrs.dst));
1054 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1055 }
1056
1057 __skb_flow_dissect_ipv4(skb, flow_dissector,
1058 target_container, data, iph);
1059
1060 if (ip_is_fragment(iph)) {
1061 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
1062
1063 if (iph->frag_off & htons(IP_OFFSET)) {
1064 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1065 break;
1066 } else {
1067 key_control->flags |= FLOW_DIS_FIRST_FRAG;
1068 if (!(flags &
1069 FLOW_DISSECTOR_F_PARSE_1ST_FRAG)) {
1070 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1071 break;
1072 }
1073 }
1074 }
1075
1076 break;
1077 }
1078 case htons(ETH_P_IPV6): {
1079 const struct ipv6hdr *iph;
1080 struct ipv6hdr _iph;
1081
1082 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
1083 if (!iph) {
1084 fdret = FLOW_DISSECT_RET_OUT_BAD;
1085 break;
1086 }
1087
1088 ip_proto = iph->nexthdr;
1089 nhoff += sizeof(struct ipv6hdr);
1090
1091 if (dissector_uses_key(flow_dissector,
1092 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
1093 key_addrs = skb_flow_dissector_target(flow_dissector,
1094 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1095 target_container);
1096
1097 memcpy(&key_addrs->v6addrs.src, &iph->saddr,
1098 sizeof(key_addrs->v6addrs.src));
1099 memcpy(&key_addrs->v6addrs.dst, &iph->daddr,
1100 sizeof(key_addrs->v6addrs.dst));
1101 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1102 }
1103
1104 if ((dissector_uses_key(flow_dissector,
1105 FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
1106 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
1107 ip6_flowlabel(iph)) {
1108 __be32 flow_label = ip6_flowlabel(iph);
1109
1110 if (dissector_uses_key(flow_dissector,
1111 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
1112 key_tags = skb_flow_dissector_target(flow_dissector,
1113 FLOW_DISSECTOR_KEY_FLOW_LABEL,
1114 target_container);
1115 key_tags->flow_label = ntohl(flow_label);
1116 }
1117 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL) {
1118 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1119 break;
1120 }
1121 }
1122
1123 __skb_flow_dissect_ipv6(skb, flow_dissector,
1124 target_container, data, iph);
1125
1126 break;
1127 }
1128 case htons(ETH_P_8021AD):
1129 case htons(ETH_P_8021Q): {
1130 const struct vlan_hdr *vlan = NULL;
1131 struct vlan_hdr _vlan;
1132 __be16 saved_vlan_tpid = proto;
1133
1134 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX &&
1135 skb && skb_vlan_tag_present(skb)) {
1136 proto = skb->protocol;
1137 } else {
1138 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
1139 data, hlen, &_vlan);
1140 if (!vlan) {
1141 fdret = FLOW_DISSECT_RET_OUT_BAD;
1142 break;
1143 }
1144
1145 proto = vlan->h_vlan_encapsulated_proto;
1146 nhoff += sizeof(*vlan);
1147 }
1148
1149 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX) {
1150 dissector_vlan = FLOW_DISSECTOR_KEY_VLAN;
1151 } else if (dissector_vlan == FLOW_DISSECTOR_KEY_VLAN) {
1152 dissector_vlan = FLOW_DISSECTOR_KEY_CVLAN;
1153 } else {
1154 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1155 break;
1156 }
1157
1158 if (dissector_uses_key(flow_dissector, dissector_vlan)) {
1159 key_vlan = skb_flow_dissector_target(flow_dissector,
1160 dissector_vlan,
1161 target_container);
1162
1163 if (!vlan) {
1164 key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
1165 key_vlan->vlan_priority = skb_vlan_tag_get_prio(skb);
1166 } else {
1167 key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) &
1168 VLAN_VID_MASK;
1169 key_vlan->vlan_priority =
1170 (ntohs(vlan->h_vlan_TCI) &
1171 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
1172 }
1173 key_vlan->vlan_tpid = saved_vlan_tpid;
1174 key_vlan->vlan_eth_type = proto;
1175 }
1176
1177 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1178 break;
1179 }
1180 case htons(ETH_P_PPP_SES): {
1181 struct {
1182 struct pppoe_hdr hdr;
1183 __be16 proto;
1184 } *hdr, _hdr;
1185 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
1186 if (!hdr) {
1187 fdret = FLOW_DISSECT_RET_OUT_BAD;
1188 break;
1189 }
1190
1191 proto = hdr->proto;
1192 nhoff += PPPOE_SES_HLEN;
1193 switch (proto) {
1194 case htons(PPP_IP):
1195 proto = htons(ETH_P_IP);
1196 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1197 break;
1198 case htons(PPP_IPV6):
1199 proto = htons(ETH_P_IPV6);
1200 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1201 break;
1202 default:
1203 fdret = FLOW_DISSECT_RET_OUT_BAD;
1204 break;
1205 }
1206 break;
1207 }
1208 case htons(ETH_P_TIPC): {
1209 struct tipc_basic_hdr *hdr, _hdr;
1210
1211 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr),
1212 data, hlen, &_hdr);
1213 if (!hdr) {
1214 fdret = FLOW_DISSECT_RET_OUT_BAD;
1215 break;
1216 }
1217
1218 if (dissector_uses_key(flow_dissector,
1219 FLOW_DISSECTOR_KEY_TIPC)) {
1220 key_addrs = skb_flow_dissector_target(flow_dissector,
1221 FLOW_DISSECTOR_KEY_TIPC,
1222 target_container);
1223 key_addrs->tipckey.key = tipc_hdr_rps_key(hdr);
1224 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC;
1225 }
1226 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1227 break;
1228 }
1229
1230 case htons(ETH_P_MPLS_UC):
1231 case htons(ETH_P_MPLS_MC):
1232 fdret = __skb_flow_dissect_mpls(skb, flow_dissector,
1233 target_container, data,
1234 nhoff, hlen, mpls_lse,
1235 &mpls_el);
1236 nhoff += sizeof(struct mpls_label);
1237 mpls_lse++;
1238 break;
1239 case htons(ETH_P_FCOE):
1240 if ((hlen - nhoff) < FCOE_HEADER_LEN) {
1241 fdret = FLOW_DISSECT_RET_OUT_BAD;
1242 break;
1243 }
1244
1245 nhoff += FCOE_HEADER_LEN;
1246 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1247 break;
1248
1249 case htons(ETH_P_ARP):
1250 case htons(ETH_P_RARP):
1251 fdret = __skb_flow_dissect_arp(skb, flow_dissector,
1252 target_container, data,
1253 nhoff, hlen);
1254 break;
1255
1256 case htons(ETH_P_BATMAN):
1257 fdret = __skb_flow_dissect_batadv(skb, key_control, data,
1258 &proto, &nhoff, hlen, flags);
1259 break;
1260
1261 default:
1262 fdret = FLOW_DISSECT_RET_OUT_BAD;
1263 break;
1264 }
1265
1266 /* Process result of proto processing */
1267 switch (fdret) {
1268 case FLOW_DISSECT_RET_OUT_GOOD:
1269 goto out_good;
1270 case FLOW_DISSECT_RET_PROTO_AGAIN:
1271 if (skb_flow_dissect_allowed(&num_hdrs))
1272 goto proto_again;
1273 goto out_good;
1274 case FLOW_DISSECT_RET_CONTINUE:
1275 case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1276 break;
1277 case FLOW_DISSECT_RET_OUT_BAD:
1278 default:
1279 goto out_bad;
1280 }
1281
1282 ip_proto_again:
1283 fdret = FLOW_DISSECT_RET_CONTINUE;
1284
1285 switch (ip_proto) {
1286 case IPPROTO_GRE:
1287 fdret = __skb_flow_dissect_gre(skb, key_control, flow_dissector,
1288 target_container, data,
1289 &proto, &nhoff, &hlen, flags);
1290 break;
1291
1292 case NEXTHDR_HOP:
1293 case NEXTHDR_ROUTING:
1294 case NEXTHDR_DEST: {
1295 u8 _opthdr[2], *opthdr;
1296
1297 if (proto != htons(ETH_P_IPV6))
1298 break;
1299
1300 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
1301 data, hlen, &_opthdr);
1302 if (!opthdr) {
1303 fdret = FLOW_DISSECT_RET_OUT_BAD;
1304 break;
1305 }
1306
1307 ip_proto = opthdr[0];
1308 nhoff += (opthdr[1] + 1) << 3;
1309
1310 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1311 break;
1312 }
1313 case NEXTHDR_FRAGMENT: {
1314 struct frag_hdr _fh, *fh;
1315
1316 if (proto != htons(ETH_P_IPV6))
1317 break;
1318
1319 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
1320 data, hlen, &_fh);
1321
1322 if (!fh) {
1323 fdret = FLOW_DISSECT_RET_OUT_BAD;
1324 break;
1325 }
1326
1327 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
1328
1329 nhoff += sizeof(_fh);
1330 ip_proto = fh->nexthdr;
1331
1332 if (!(fh->frag_off & htons(IP6_OFFSET))) {
1333 key_control->flags |= FLOW_DIS_FIRST_FRAG;
1334 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) {
1335 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1336 break;
1337 }
1338 }
1339
1340 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1341 break;
1342 }
1343 case IPPROTO_IPIP:
1344 proto = htons(ETH_P_IP);
1345
1346 key_control->flags |= FLOW_DIS_ENCAPSULATION;
1347 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1348 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1349 break;
1350 }
1351
1352 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1353 break;
1354
1355 case IPPROTO_IPV6:
1356 proto = htons(ETH_P_IPV6);
1357
1358 key_control->flags |= FLOW_DIS_ENCAPSULATION;
1359 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1360 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1361 break;
1362 }
1363
1364 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1365 break;
1366
1367
1368 case IPPROTO_MPLS:
1369 proto = htons(ETH_P_MPLS_UC);
1370 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1371 break;
1372
1373 case IPPROTO_TCP:
1374 __skb_flow_dissect_tcp(skb, flow_dissector, target_container,
1375 data, nhoff, hlen);
1376 break;
1377
1378 case IPPROTO_ICMP:
1379 case IPPROTO_ICMPV6:
1380 __skb_flow_dissect_icmp(skb, flow_dissector, target_container,
1381 data, nhoff, hlen);
1382 break;
1383
1384 default:
1385 break;
1386 }
1387
1388 if (!(key_control->flags & FLOW_DIS_IS_FRAGMENT))
1389 __skb_flow_dissect_ports(skb, flow_dissector, target_container,
1390 data, nhoff, ip_proto, hlen);
1391
1392 /* Process result of IP proto processing */
1393 switch (fdret) {
1394 case FLOW_DISSECT_RET_PROTO_AGAIN:
1395 if (skb_flow_dissect_allowed(&num_hdrs))
1396 goto proto_again;
1397 break;
1398 case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1399 if (skb_flow_dissect_allowed(&num_hdrs))
1400 goto ip_proto_again;
1401 break;
1402 case FLOW_DISSECT_RET_OUT_GOOD:
1403 case FLOW_DISSECT_RET_CONTINUE:
1404 break;
1405 case FLOW_DISSECT_RET_OUT_BAD:
1406 default:
1407 goto out_bad;
1408 }
1409
1410 out_good:
1411 ret = true;
1412
1413 out:
1414 key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
1415 key_basic->n_proto = proto;
1416 key_basic->ip_proto = ip_proto;
1417
1418 return ret;
1419
1420 out_bad:
1421 ret = false;
1422 goto out;
1423 }
1424 EXPORT_SYMBOL(__skb_flow_dissect);
1425
1426 static siphash_key_t hashrnd __read_mostly;
__flow_hash_secret_init(void)1427 static __always_inline void __flow_hash_secret_init(void)
1428 {
1429 net_get_random_once(&hashrnd, sizeof(hashrnd));
1430 }
1431
flow_keys_hash_start(const struct flow_keys * flow)1432 static const void *flow_keys_hash_start(const struct flow_keys *flow)
1433 {
1434 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % SIPHASH_ALIGNMENT);
1435 return &flow->FLOW_KEYS_HASH_START_FIELD;
1436 }
1437
flow_keys_hash_length(const struct flow_keys * flow)1438 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
1439 {
1440 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
1441
1442 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
1443
1444 switch (flow->control.addr_type) {
1445 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1446 diff -= sizeof(flow->addrs.v4addrs);
1447 break;
1448 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1449 diff -= sizeof(flow->addrs.v6addrs);
1450 break;
1451 case FLOW_DISSECTOR_KEY_TIPC:
1452 diff -= sizeof(flow->addrs.tipckey);
1453 break;
1454 }
1455 return sizeof(*flow) - diff;
1456 }
1457
flow_get_u32_src(const struct flow_keys * flow)1458 __be32 flow_get_u32_src(const struct flow_keys *flow)
1459 {
1460 switch (flow->control.addr_type) {
1461 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1462 return flow->addrs.v4addrs.src;
1463 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1464 return (__force __be32)ipv6_addr_hash(
1465 &flow->addrs.v6addrs.src);
1466 case FLOW_DISSECTOR_KEY_TIPC:
1467 return flow->addrs.tipckey.key;
1468 default:
1469 return 0;
1470 }
1471 }
1472 EXPORT_SYMBOL(flow_get_u32_src);
1473
flow_get_u32_dst(const struct flow_keys * flow)1474 __be32 flow_get_u32_dst(const struct flow_keys *flow)
1475 {
1476 switch (flow->control.addr_type) {
1477 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1478 return flow->addrs.v4addrs.dst;
1479 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1480 return (__force __be32)ipv6_addr_hash(
1481 &flow->addrs.v6addrs.dst);
1482 default:
1483 return 0;
1484 }
1485 }
1486 EXPORT_SYMBOL(flow_get_u32_dst);
1487
1488 /* Sort the source and destination IP and the ports,
1489 * to have consistent hash within the two directions
1490 */
__flow_hash_consistentify(struct flow_keys * keys)1491 static inline void __flow_hash_consistentify(struct flow_keys *keys)
1492 {
1493 int addr_diff, i;
1494
1495 switch (keys->control.addr_type) {
1496 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1497 if ((__force u32)keys->addrs.v4addrs.dst <
1498 (__force u32)keys->addrs.v4addrs.src)
1499 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
1500
1501 if ((__force u16)keys->ports.dst <
1502 (__force u16)keys->ports.src) {
1503 swap(keys->ports.src, keys->ports.dst);
1504 }
1505 break;
1506 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1507 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
1508 &keys->addrs.v6addrs.src,
1509 sizeof(keys->addrs.v6addrs.dst));
1510 if (addr_diff < 0) {
1511 for (i = 0; i < 4; i++)
1512 swap(keys->addrs.v6addrs.src.s6_addr32[i],
1513 keys->addrs.v6addrs.dst.s6_addr32[i]);
1514 }
1515 if ((__force u16)keys->ports.dst <
1516 (__force u16)keys->ports.src) {
1517 swap(keys->ports.src, keys->ports.dst);
1518 }
1519 break;
1520 }
1521 }
1522
__flow_hash_from_keys(struct flow_keys * keys,const siphash_key_t * keyval)1523 static inline u32 __flow_hash_from_keys(struct flow_keys *keys,
1524 const siphash_key_t *keyval)
1525 {
1526 u32 hash;
1527
1528 __flow_hash_consistentify(keys);
1529
1530 hash = siphash(flow_keys_hash_start(keys),
1531 flow_keys_hash_length(keys), keyval);
1532 if (!hash)
1533 hash = 1;
1534
1535 return hash;
1536 }
1537
flow_hash_from_keys(struct flow_keys * keys)1538 u32 flow_hash_from_keys(struct flow_keys *keys)
1539 {
1540 __flow_hash_secret_init();
1541 return __flow_hash_from_keys(keys, &hashrnd);
1542 }
1543 EXPORT_SYMBOL(flow_hash_from_keys);
1544
___skb_get_hash(const struct sk_buff * skb,struct flow_keys * keys,const siphash_key_t * keyval)1545 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
1546 struct flow_keys *keys,
1547 const siphash_key_t *keyval)
1548 {
1549 skb_flow_dissect_flow_keys(skb, keys,
1550 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1551
1552 return __flow_hash_from_keys(keys, keyval);
1553 }
1554
1555 struct _flow_keys_digest_data {
1556 __be16 n_proto;
1557 u8 ip_proto;
1558 u8 padding;
1559 __be32 ports;
1560 __be32 src;
1561 __be32 dst;
1562 };
1563
make_flow_keys_digest(struct flow_keys_digest * digest,const struct flow_keys * flow)1564 void make_flow_keys_digest(struct flow_keys_digest *digest,
1565 const struct flow_keys *flow)
1566 {
1567 struct _flow_keys_digest_data *data =
1568 (struct _flow_keys_digest_data *)digest;
1569
1570 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
1571
1572 memset(digest, 0, sizeof(*digest));
1573
1574 data->n_proto = flow->basic.n_proto;
1575 data->ip_proto = flow->basic.ip_proto;
1576 data->ports = flow->ports.ports;
1577 data->src = flow->addrs.v4addrs.src;
1578 data->dst = flow->addrs.v4addrs.dst;
1579 }
1580 EXPORT_SYMBOL(make_flow_keys_digest);
1581
1582 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
1583
__skb_get_hash_symmetric(const struct sk_buff * skb)1584 u32 __skb_get_hash_symmetric(const struct sk_buff *skb)
1585 {
1586 struct flow_keys keys;
1587
1588 __flow_hash_secret_init();
1589
1590 memset(&keys, 0, sizeof(keys));
1591 __skb_flow_dissect(NULL, skb, &flow_keys_dissector_symmetric,
1592 &keys, NULL, 0, 0, 0, 0);
1593
1594 return __flow_hash_from_keys(&keys, &hashrnd);
1595 }
1596 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
1597
1598 /**
1599 * __skb_get_hash: calculate a flow hash
1600 * @skb: sk_buff to calculate flow hash from
1601 *
1602 * This function calculates a flow hash based on src/dst addresses
1603 * and src/dst port numbers. Sets hash in skb to non-zero hash value
1604 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
1605 * if hash is a canonical 4-tuple hash over transport ports.
1606 */
__skb_get_hash(struct sk_buff * skb)1607 void __skb_get_hash(struct sk_buff *skb)
1608 {
1609 struct flow_keys keys;
1610 u32 hash;
1611
1612 __flow_hash_secret_init();
1613
1614 hash = ___skb_get_hash(skb, &keys, &hashrnd);
1615
1616 __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1617 }
1618 EXPORT_SYMBOL(__skb_get_hash);
1619
skb_get_hash_perturb(const struct sk_buff * skb,const siphash_key_t * perturb)1620 __u32 skb_get_hash_perturb(const struct sk_buff *skb,
1621 const siphash_key_t *perturb)
1622 {
1623 struct flow_keys keys;
1624
1625 return ___skb_get_hash(skb, &keys, perturb);
1626 }
1627 EXPORT_SYMBOL(skb_get_hash_perturb);
1628
__skb_get_poff(const struct sk_buff * skb,void * data,const struct flow_keys_basic * keys,int hlen)1629 u32 __skb_get_poff(const struct sk_buff *skb, void *data,
1630 const struct flow_keys_basic *keys, int hlen)
1631 {
1632 u32 poff = keys->control.thoff;
1633
1634 /* skip L4 headers for fragments after the first */
1635 if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
1636 !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
1637 return poff;
1638
1639 switch (keys->basic.ip_proto) {
1640 case IPPROTO_TCP: {
1641 /* access doff as u8 to avoid unaligned access */
1642 const u8 *doff;
1643 u8 _doff;
1644
1645 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
1646 data, hlen, &_doff);
1647 if (!doff)
1648 return poff;
1649
1650 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
1651 break;
1652 }
1653 case IPPROTO_UDP:
1654 case IPPROTO_UDPLITE:
1655 poff += sizeof(struct udphdr);
1656 break;
1657 /* For the rest, we do not really care about header
1658 * extensions at this point for now.
1659 */
1660 case IPPROTO_ICMP:
1661 poff += sizeof(struct icmphdr);
1662 break;
1663 case IPPROTO_ICMPV6:
1664 poff += sizeof(struct icmp6hdr);
1665 break;
1666 case IPPROTO_IGMP:
1667 poff += sizeof(struct igmphdr);
1668 break;
1669 case IPPROTO_DCCP:
1670 poff += sizeof(struct dccp_hdr);
1671 break;
1672 case IPPROTO_SCTP:
1673 poff += sizeof(struct sctphdr);
1674 break;
1675 }
1676
1677 return poff;
1678 }
1679
1680 /**
1681 * skb_get_poff - get the offset to the payload
1682 * @skb: sk_buff to get the payload offset from
1683 *
1684 * The function will get the offset to the payload as far as it could
1685 * be dissected. The main user is currently BPF, so that we can dynamically
1686 * truncate packets without needing to push actual payload to the user
1687 * space and can analyze headers only, instead.
1688 */
skb_get_poff(const struct sk_buff * skb)1689 u32 skb_get_poff(const struct sk_buff *skb)
1690 {
1691 struct flow_keys_basic keys;
1692
1693 if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys,
1694 NULL, 0, 0, 0, 0))
1695 return 0;
1696
1697 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
1698 }
1699
__get_hash_from_flowi6(const struct flowi6 * fl6,struct flow_keys * keys)1700 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
1701 {
1702 memset(keys, 0, sizeof(*keys));
1703
1704 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
1705 sizeof(keys->addrs.v6addrs.src));
1706 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
1707 sizeof(keys->addrs.v6addrs.dst));
1708 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1709 keys->ports.src = fl6->fl6_sport;
1710 keys->ports.dst = fl6->fl6_dport;
1711 keys->keyid.keyid = fl6->fl6_gre_key;
1712 keys->tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
1713 keys->basic.ip_proto = fl6->flowi6_proto;
1714
1715 return flow_hash_from_keys(keys);
1716 }
1717 EXPORT_SYMBOL(__get_hash_from_flowi6);
1718
1719 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
1720 {
1721 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1722 .offset = offsetof(struct flow_keys, control),
1723 },
1724 {
1725 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1726 .offset = offsetof(struct flow_keys, basic),
1727 },
1728 {
1729 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1730 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1731 },
1732 {
1733 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1734 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1735 },
1736 {
1737 .key_id = FLOW_DISSECTOR_KEY_TIPC,
1738 .offset = offsetof(struct flow_keys, addrs.tipckey),
1739 },
1740 {
1741 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1742 .offset = offsetof(struct flow_keys, ports),
1743 },
1744 {
1745 .key_id = FLOW_DISSECTOR_KEY_VLAN,
1746 .offset = offsetof(struct flow_keys, vlan),
1747 },
1748 {
1749 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
1750 .offset = offsetof(struct flow_keys, tags),
1751 },
1752 {
1753 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
1754 .offset = offsetof(struct flow_keys, keyid),
1755 },
1756 };
1757
1758 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
1759 {
1760 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1761 .offset = offsetof(struct flow_keys, control),
1762 },
1763 {
1764 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1765 .offset = offsetof(struct flow_keys, basic),
1766 },
1767 {
1768 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1769 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1770 },
1771 {
1772 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1773 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1774 },
1775 {
1776 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1777 .offset = offsetof(struct flow_keys, ports),
1778 },
1779 };
1780
1781 static const struct flow_dissector_key flow_keys_basic_dissector_keys[] = {
1782 {
1783 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1784 .offset = offsetof(struct flow_keys, control),
1785 },
1786 {
1787 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1788 .offset = offsetof(struct flow_keys, basic),
1789 },
1790 };
1791
1792 struct flow_dissector flow_keys_dissector __read_mostly;
1793 EXPORT_SYMBOL(flow_keys_dissector);
1794
1795 struct flow_dissector flow_keys_basic_dissector __read_mostly;
1796 EXPORT_SYMBOL(flow_keys_basic_dissector);
1797
init_default_flow_dissectors(void)1798 static int __init init_default_flow_dissectors(void)
1799 {
1800 skb_flow_dissector_init(&flow_keys_dissector,
1801 flow_keys_dissector_keys,
1802 ARRAY_SIZE(flow_keys_dissector_keys));
1803 skb_flow_dissector_init(&flow_keys_dissector_symmetric,
1804 flow_keys_dissector_symmetric_keys,
1805 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
1806 skb_flow_dissector_init(&flow_keys_basic_dissector,
1807 flow_keys_basic_dissector_keys,
1808 ARRAY_SIZE(flow_keys_basic_dissector_keys));
1809 return 0;
1810 }
1811 core_initcall(init_default_flow_dissectors);
1812