]> Git Repo - linux.git/blame - net/core/flow_dissector.c
sock_diag: fix autoloading of the raw_diag module
[linux.git] / net / core / flow_dissector.c
CommitLineData
fbff949e 1#include <linux/kernel.h>
0744dd00 2#include <linux/skbuff.h>
c452ed70 3#include <linux/export.h>
0744dd00
ED
4#include <linux/ip.h>
5#include <linux/ipv6.h>
6#include <linux/if_vlan.h>
43e66528 7#include <net/dsa.h>
a38402bc 8#include <net/dst_metadata.h>
0744dd00 9#include <net/ip.h>
ddbe5032 10#include <net/ipv6.h>
ab10dccb
GF
11#include <net/gre.h>
12#include <net/pptp.h>
8d6e79d3 13#include <net/tipc.h>
f77668dc
DB
14#include <linux/igmp.h>
15#include <linux/icmp.h>
16#include <linux/sctp.h>
17#include <linux/dccp.h>
0744dd00
ED
18#include <linux/if_tunnel.h>
19#include <linux/if_pppox.h>
20#include <linux/ppp_defs.h>
06635a35 21#include <linux/stddef.h>
67a900cc 22#include <linux/if_ether.h>
b3baa0fb 23#include <linux/mpls.h>
ac4bb5de 24#include <linux/tcp.h>
1bd758eb 25#include <net/flow_dissector.h>
56193d1b 26#include <scsi/fc/fc_fcoe.h>
5b0890a9 27#include <uapi/linux/batadv_packet.h>
d58e468b
PP
28#include <linux/bpf.h>
29
30static DEFINE_MUTEX(flow_dissector_mutex);
0744dd00 31
20a17bf6
DM
32static void dissector_set_key(struct flow_dissector *flow_dissector,
33 enum flow_dissector_key_id key_id)
fbff949e
JP
34{
35 flow_dissector->used_keys |= (1 << key_id);
36}
37
fbff949e
JP
38void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
39 const struct flow_dissector_key *key,
40 unsigned int key_count)
41{
42 unsigned int i;
43
44 memset(flow_dissector, 0, sizeof(*flow_dissector));
45
46 for (i = 0; i < key_count; i++, key++) {
47 /* User should make sure that every key target offset is withing
48 * boundaries of unsigned short.
49 */
50 BUG_ON(key->offset > USHRT_MAX);
20a17bf6
DM
51 BUG_ON(dissector_uses_key(flow_dissector,
52 key->key_id));
fbff949e 53
20a17bf6 54 dissector_set_key(flow_dissector, key->key_id);
fbff949e
JP
55 flow_dissector->offset[key->key_id] = key->offset;
56 }
57
42aecaa9
TH
58 /* Ensure that the dissector always includes control and basic key.
59 * That way we are able to avoid handling lack of these in fast path.
fbff949e 60 */
20a17bf6
DM
61 BUG_ON(!dissector_uses_key(flow_dissector,
62 FLOW_DISSECTOR_KEY_CONTROL));
63 BUG_ON(!dissector_uses_key(flow_dissector,
64 FLOW_DISSECTOR_KEY_BASIC));
fbff949e
JP
65}
66EXPORT_SYMBOL(skb_flow_dissector_init);
67
d58e468b
PP
68int skb_flow_dissector_bpf_prog_attach(const union bpf_attr *attr,
69 struct bpf_prog *prog)
70{
71 struct bpf_prog *attached;
72 struct net *net;
73
74 net = current->nsproxy->net_ns;
75 mutex_lock(&flow_dissector_mutex);
76 attached = rcu_dereference_protected(net->flow_dissector_prog,
77 lockdep_is_held(&flow_dissector_mutex));
78 if (attached) {
79 /* Only one BPF program can be attached at a time */
80 mutex_unlock(&flow_dissector_mutex);
81 return -EEXIST;
82 }
83 rcu_assign_pointer(net->flow_dissector_prog, prog);
84 mutex_unlock(&flow_dissector_mutex);
85 return 0;
86}
87
88int skb_flow_dissector_bpf_prog_detach(const union bpf_attr *attr)
89{
90 struct bpf_prog *attached;
91 struct net *net;
92
93 net = current->nsproxy->net_ns;
94 mutex_lock(&flow_dissector_mutex);
95 attached = rcu_dereference_protected(net->flow_dissector_prog,
96 lockdep_is_held(&flow_dissector_mutex));
97 if (!attached) {
98 mutex_unlock(&flow_dissector_mutex);
99 return -ENOENT;
100 }
101 bpf_prog_put(attached);
102 RCU_INIT_POINTER(net->flow_dissector_prog, NULL);
103 mutex_unlock(&flow_dissector_mutex);
104 return 0;
105}
972d3876
SH
106/**
107 * skb_flow_get_be16 - extract be16 entity
108 * @skb: sk_buff to extract from
109 * @poff: offset to extract at
110 * @data: raw buffer pointer to the packet
111 * @hlen: packet header length
112 *
113 * The function will try to retrieve a be32 entity at
114 * offset poff
115 */
d9584d8c
ED
116static __be16 skb_flow_get_be16(const struct sk_buff *skb, int poff,
117 void *data, int hlen)
972d3876
SH
118{
119 __be16 *u, _u;
120
121 u = __skb_header_pointer(skb, poff, sizeof(_u), data, hlen, &_u);
122 if (u)
123 return *u;
124
125 return 0;
126}
127
357afe9c 128/**
6451b3f5
WC
129 * __skb_flow_get_ports - extract the upper layer ports and return them
130 * @skb: sk_buff to extract the ports from
357afe9c
NA
131 * @thoff: transport header offset
132 * @ip_proto: protocol for which to get port offset
6451b3f5
WC
133 * @data: raw buffer pointer to the packet, if NULL use skb->data
134 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
357afe9c
NA
135 *
136 * The function will try to retrieve the ports at offset thoff + poff where poff
137 * is the protocol port offset returned from proto_ports_offset
138 */
690e36e7
DM
139__be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
140 void *data, int hlen)
357afe9c
NA
141{
142 int poff = proto_ports_offset(ip_proto);
143
690e36e7
DM
144 if (!data) {
145 data = skb->data;
146 hlen = skb_headlen(skb);
147 }
148
357afe9c
NA
149 if (poff >= 0) {
150 __be32 *ports, _ports;
151
690e36e7
DM
152 ports = __skb_header_pointer(skb, thoff + poff,
153 sizeof(_ports), data, hlen, &_ports);
357afe9c
NA
154 if (ports)
155 return *ports;
156 }
157
158 return 0;
159}
690e36e7 160EXPORT_SYMBOL(__skb_flow_get_ports);
357afe9c 161
a38402bc
SH
162static void
163skb_flow_dissect_set_enc_addr_type(enum flow_dissector_key_id type,
164 struct flow_dissector *flow_dissector,
165 void *target_container)
166{
167 struct flow_dissector_key_control *ctrl;
168
169 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL))
170 return;
171
172 ctrl = skb_flow_dissector_target(flow_dissector,
173 FLOW_DISSECTOR_KEY_ENC_CONTROL,
174 target_container);
175 ctrl->addr_type = type;
176}
177
62b32379
SH
178void
179skb_flow_dissect_tunnel_info(const struct sk_buff *skb,
180 struct flow_dissector *flow_dissector,
181 void *target_container)
a38402bc
SH
182{
183 struct ip_tunnel_info *info;
184 struct ip_tunnel_key *key;
185
186 /* A quick check to see if there might be something to do. */
187 if (!dissector_uses_key(flow_dissector,
188 FLOW_DISSECTOR_KEY_ENC_KEYID) &&
189 !dissector_uses_key(flow_dissector,
190 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) &&
191 !dissector_uses_key(flow_dissector,
192 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) &&
193 !dissector_uses_key(flow_dissector,
194 FLOW_DISSECTOR_KEY_ENC_CONTROL) &&
195 !dissector_uses_key(flow_dissector,
5544adb9
OG
196 FLOW_DISSECTOR_KEY_ENC_PORTS) &&
197 !dissector_uses_key(flow_dissector,
92e2c405
SH
198 FLOW_DISSECTOR_KEY_ENC_IP) &&
199 !dissector_uses_key(flow_dissector,
200 FLOW_DISSECTOR_KEY_ENC_OPTS))
a38402bc
SH
201 return;
202
203 info = skb_tunnel_info(skb);
204 if (!info)
205 return;
206
207 key = &info->key;
208
209 switch (ip_tunnel_info_af(info)) {
210 case AF_INET:
211 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV4_ADDRS,
212 flow_dissector,
213 target_container);
214 if (dissector_uses_key(flow_dissector,
215 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) {
216 struct flow_dissector_key_ipv4_addrs *ipv4;
217
218 ipv4 = skb_flow_dissector_target(flow_dissector,
219 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
220 target_container);
221 ipv4->src = key->u.ipv4.src;
222 ipv4->dst = key->u.ipv4.dst;
223 }
224 break;
225 case AF_INET6:
226 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV6_ADDRS,
227 flow_dissector,
228 target_container);
229 if (dissector_uses_key(flow_dissector,
230 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS)) {
231 struct flow_dissector_key_ipv6_addrs *ipv6;
232
233 ipv6 = skb_flow_dissector_target(flow_dissector,
234 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
235 target_container);
236 ipv6->src = key->u.ipv6.src;
237 ipv6->dst = key->u.ipv6.dst;
238 }
239 break;
240 }
241
242 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
243 struct flow_dissector_key_keyid *keyid;
244
245 keyid = skb_flow_dissector_target(flow_dissector,
246 FLOW_DISSECTOR_KEY_ENC_KEYID,
247 target_container);
248 keyid->keyid = tunnel_id_to_key32(key->tun_id);
249 }
250
251 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
252 struct flow_dissector_key_ports *tp;
253
254 tp = skb_flow_dissector_target(flow_dissector,
255 FLOW_DISSECTOR_KEY_ENC_PORTS,
256 target_container);
257 tp->src = key->tp_src;
258 tp->dst = key->tp_dst;
259 }
5544adb9
OG
260
261 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_IP)) {
262 struct flow_dissector_key_ip *ip;
263
264 ip = skb_flow_dissector_target(flow_dissector,
265 FLOW_DISSECTOR_KEY_ENC_IP,
266 target_container);
267 ip->tos = key->tos;
268 ip->ttl = key->ttl;
269 }
92e2c405
SH
270
271 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_OPTS)) {
272 struct flow_dissector_key_enc_opts *enc_opt;
273
274 enc_opt = skb_flow_dissector_target(flow_dissector,
275 FLOW_DISSECTOR_KEY_ENC_OPTS,
276 target_container);
277
278 if (info->options_len) {
279 enc_opt->len = info->options_len;
280 ip_tunnel_info_opts_get(enc_opt->data, info);
281 enc_opt->dst_opt_type = info->key.tun_flags &
282 TUNNEL_OPTIONS_PRESENT;
283 }
284 }
a38402bc 285}
62b32379 286EXPORT_SYMBOL(skb_flow_dissect_tunnel_info);
a38402bc 287
4a5d6c8b
JP
288static enum flow_dissect_ret
289__skb_flow_dissect_mpls(const struct sk_buff *skb,
290 struct flow_dissector *flow_dissector,
291 void *target_container, void *data, int nhoff, int hlen)
292{
293 struct flow_dissector_key_keyid *key_keyid;
294 struct mpls_label *hdr, _hdr[2];
029c1ecb 295 u32 entry, label;
4a5d6c8b
JP
296
297 if (!dissector_uses_key(flow_dissector,
029c1ecb
BL
298 FLOW_DISSECTOR_KEY_MPLS_ENTROPY) &&
299 !dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS))
4a5d6c8b
JP
300 return FLOW_DISSECT_RET_OUT_GOOD;
301
302 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
303 hlen, &_hdr);
304 if (!hdr)
305 return FLOW_DISSECT_RET_OUT_BAD;
306
029c1ecb
BL
307 entry = ntohl(hdr[0].entry);
308 label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
309
310 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) {
311 struct flow_dissector_key_mpls *key_mpls;
312
313 key_mpls = skb_flow_dissector_target(flow_dissector,
314 FLOW_DISSECTOR_KEY_MPLS,
315 target_container);
316 key_mpls->mpls_label = label;
317 key_mpls->mpls_ttl = (entry & MPLS_LS_TTL_MASK)
318 >> MPLS_LS_TTL_SHIFT;
319 key_mpls->mpls_tc = (entry & MPLS_LS_TC_MASK)
320 >> MPLS_LS_TC_SHIFT;
321 key_mpls->mpls_bos = (entry & MPLS_LS_S_MASK)
322 >> MPLS_LS_S_SHIFT;
323 }
324
325 if (label == MPLS_LABEL_ENTROPY) {
4a5d6c8b
JP
326 key_keyid = skb_flow_dissector_target(flow_dissector,
327 FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
328 target_container);
329 key_keyid->keyid = hdr[1].entry & htonl(MPLS_LS_LABEL_MASK);
330 }
331 return FLOW_DISSECT_RET_OUT_GOOD;
332}
333
9bf881ff
JP
334static enum flow_dissect_ret
335__skb_flow_dissect_arp(const struct sk_buff *skb,
336 struct flow_dissector *flow_dissector,
337 void *target_container, void *data, int nhoff, int hlen)
338{
339 struct flow_dissector_key_arp *key_arp;
340 struct {
341 unsigned char ar_sha[ETH_ALEN];
342 unsigned char ar_sip[4];
343 unsigned char ar_tha[ETH_ALEN];
344 unsigned char ar_tip[4];
345 } *arp_eth, _arp_eth;
346 const struct arphdr *arp;
6f14f443 347 struct arphdr _arp;
9bf881ff
JP
348
349 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ARP))
350 return FLOW_DISSECT_RET_OUT_GOOD;
351
352 arp = __skb_header_pointer(skb, nhoff, sizeof(_arp), data,
353 hlen, &_arp);
354 if (!arp)
355 return FLOW_DISSECT_RET_OUT_BAD;
356
357 if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
358 arp->ar_pro != htons(ETH_P_IP) ||
359 arp->ar_hln != ETH_ALEN ||
360 arp->ar_pln != 4 ||
361 (arp->ar_op != htons(ARPOP_REPLY) &&
362 arp->ar_op != htons(ARPOP_REQUEST)))
363 return FLOW_DISSECT_RET_OUT_BAD;
364
365 arp_eth = __skb_header_pointer(skb, nhoff + sizeof(_arp),
366 sizeof(_arp_eth), data,
367 hlen, &_arp_eth);
368 if (!arp_eth)
369 return FLOW_DISSECT_RET_OUT_BAD;
370
371 key_arp = skb_flow_dissector_target(flow_dissector,
372 FLOW_DISSECTOR_KEY_ARP,
373 target_container);
374
375 memcpy(&key_arp->sip, arp_eth->ar_sip, sizeof(key_arp->sip));
376 memcpy(&key_arp->tip, arp_eth->ar_tip, sizeof(key_arp->tip));
377
378 /* Only store the lower byte of the opcode;
379 * this covers ARPOP_REPLY and ARPOP_REQUEST.
380 */
381 key_arp->op = ntohs(arp->ar_op) & 0xff;
382
383 ether_addr_copy(key_arp->sha, arp_eth->ar_sha);
384 ether_addr_copy(key_arp->tha, arp_eth->ar_tha);
385
386 return FLOW_DISSECT_RET_OUT_GOOD;
387}
388
7c92de8e
JP
389static enum flow_dissect_ret
390__skb_flow_dissect_gre(const struct sk_buff *skb,
391 struct flow_dissector_key_control *key_control,
392 struct flow_dissector *flow_dissector,
393 void *target_container, void *data,
394 __be16 *p_proto, int *p_nhoff, int *p_hlen,
395 unsigned int flags)
396{
397 struct flow_dissector_key_keyid *key_keyid;
398 struct gre_base_hdr *hdr, _hdr;
399 int offset = 0;
400 u16 gre_ver;
401
402 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr),
403 data, *p_hlen, &_hdr);
404 if (!hdr)
405 return FLOW_DISSECT_RET_OUT_BAD;
406
407 /* Only look inside GRE without routing */
408 if (hdr->flags & GRE_ROUTING)
409 return FLOW_DISSECT_RET_OUT_GOOD;
410
411 /* Only look inside GRE for version 0 and 1 */
412 gre_ver = ntohs(hdr->flags & GRE_VERSION);
413 if (gre_ver > 1)
414 return FLOW_DISSECT_RET_OUT_GOOD;
415
416 *p_proto = hdr->protocol;
417 if (gre_ver) {
418 /* Version1 must be PPTP, and check the flags */
419 if (!(*p_proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY)))
420 return FLOW_DISSECT_RET_OUT_GOOD;
421 }
422
423 offset += sizeof(struct gre_base_hdr);
424
425 if (hdr->flags & GRE_CSUM)
f195efb4 426 offset += FIELD_SIZEOF(struct gre_full_hdr, csum) +
427 FIELD_SIZEOF(struct gre_full_hdr, reserved1);
7c92de8e
JP
428
429 if (hdr->flags & GRE_KEY) {
430 const __be32 *keyid;
431 __be32 _keyid;
432
433 keyid = __skb_header_pointer(skb, *p_nhoff + offset,
434 sizeof(_keyid),
435 data, *p_hlen, &_keyid);
436 if (!keyid)
437 return FLOW_DISSECT_RET_OUT_BAD;
438
439 if (dissector_uses_key(flow_dissector,
440 FLOW_DISSECTOR_KEY_GRE_KEYID)) {
441 key_keyid = skb_flow_dissector_target(flow_dissector,
442 FLOW_DISSECTOR_KEY_GRE_KEYID,
443 target_container);
444 if (gre_ver == 0)
445 key_keyid->keyid = *keyid;
446 else
447 key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK;
448 }
f195efb4 449 offset += FIELD_SIZEOF(struct gre_full_hdr, key);
7c92de8e
JP
450 }
451
452 if (hdr->flags & GRE_SEQ)
f195efb4 453 offset += FIELD_SIZEOF(struct pptp_gre_header, seq);
7c92de8e
JP
454
455 if (gre_ver == 0) {
456 if (*p_proto == htons(ETH_P_TEB)) {
457 const struct ethhdr *eth;
458 struct ethhdr _eth;
459
460 eth = __skb_header_pointer(skb, *p_nhoff + offset,
461 sizeof(_eth),
462 data, *p_hlen, &_eth);
463 if (!eth)
464 return FLOW_DISSECT_RET_OUT_BAD;
465 *p_proto = eth->h_proto;
466 offset += sizeof(*eth);
467
468 /* Cap headers that we access via pointers at the
469 * end of the Ethernet header as our maximum alignment
470 * at that point is only 2 bytes.
471 */
472 if (NET_IP_ALIGN)
473 *p_hlen = *p_nhoff + offset;
474 }
475 } else { /* version 1, must be PPTP */
476 u8 _ppp_hdr[PPP_HDRLEN];
477 u8 *ppp_hdr;
478
479 if (hdr->flags & GRE_ACK)
f195efb4 480 offset += FIELD_SIZEOF(struct pptp_gre_header, ack);
7c92de8e
JP
481
482 ppp_hdr = __skb_header_pointer(skb, *p_nhoff + offset,
483 sizeof(_ppp_hdr),
484 data, *p_hlen, _ppp_hdr);
485 if (!ppp_hdr)
486 return FLOW_DISSECT_RET_OUT_BAD;
487
488 switch (PPP_PROTOCOL(ppp_hdr)) {
489 case PPP_IP:
490 *p_proto = htons(ETH_P_IP);
491 break;
492 case PPP_IPV6:
493 *p_proto = htons(ETH_P_IPV6);
494 break;
495 default:
496 /* Could probably catch some more like MPLS */
497 break;
498 }
499
500 offset += PPP_HDRLEN;
501 }
502
503 *p_nhoff += offset;
504 key_control->flags |= FLOW_DIS_ENCAPSULATION;
505 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
506 return FLOW_DISSECT_RET_OUT_GOOD;
507
3a1214e8 508 return FLOW_DISSECT_RET_PROTO_AGAIN;
7c92de8e
JP
509}
510
5b0890a9
SE
511/**
512 * __skb_flow_dissect_batadv() - dissect batman-adv header
513 * @skb: sk_buff to with the batman-adv header
514 * @key_control: flow dissectors control key
515 * @data: raw buffer pointer to the packet, if NULL use skb->data
516 * @p_proto: pointer used to update the protocol to process next
517 * @p_nhoff: pointer used to update inner network header offset
518 * @hlen: packet header length
519 * @flags: any combination of FLOW_DISSECTOR_F_*
520 *
521 * ETH_P_BATMAN packets are tried to be dissected. Only
522 * &struct batadv_unicast packets are actually processed because they contain an
523 * inner ethernet header and are usually followed by actual network header. This
524 * allows the flow dissector to continue processing the packet.
525 *
526 * Return: FLOW_DISSECT_RET_PROTO_AGAIN when &struct batadv_unicast was found,
527 * FLOW_DISSECT_RET_OUT_GOOD when dissector should stop after encapsulation,
528 * otherwise FLOW_DISSECT_RET_OUT_BAD
529 */
530static enum flow_dissect_ret
531__skb_flow_dissect_batadv(const struct sk_buff *skb,
532 struct flow_dissector_key_control *key_control,
533 void *data, __be16 *p_proto, int *p_nhoff, int hlen,
534 unsigned int flags)
535{
536 struct {
537 struct batadv_unicast_packet batadv_unicast;
538 struct ethhdr eth;
539 } *hdr, _hdr;
540
541 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr), data, hlen,
542 &_hdr);
543 if (!hdr)
544 return FLOW_DISSECT_RET_OUT_BAD;
545
546 if (hdr->batadv_unicast.version != BATADV_COMPAT_VERSION)
547 return FLOW_DISSECT_RET_OUT_BAD;
548
549 if (hdr->batadv_unicast.packet_type != BATADV_UNICAST)
550 return FLOW_DISSECT_RET_OUT_BAD;
551
552 *p_proto = hdr->eth.h_proto;
553 *p_nhoff += sizeof(*hdr);
554
555 key_control->flags |= FLOW_DIS_ENCAPSULATION;
556 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
557 return FLOW_DISSECT_RET_OUT_GOOD;
558
559 return FLOW_DISSECT_RET_PROTO_AGAIN;
560}
561
ac4bb5de
JP
562static void
563__skb_flow_dissect_tcp(const struct sk_buff *skb,
564 struct flow_dissector *flow_dissector,
565 void *target_container, void *data, int thoff, int hlen)
566{
567 struct flow_dissector_key_tcp *key_tcp;
568 struct tcphdr *th, _th;
569
570 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_TCP))
571 return;
572
573 th = __skb_header_pointer(skb, thoff, sizeof(_th), data, hlen, &_th);
574 if (!th)
575 return;
576
577 if (unlikely(__tcp_hdrlen(th) < sizeof(_th)))
578 return;
579
580 key_tcp = skb_flow_dissector_target(flow_dissector,
581 FLOW_DISSECTOR_KEY_TCP,
582 target_container);
583 key_tcp->flags = (*(__be16 *) &tcp_flag_word(th) & htons(0x0FFF));
584}
585
518d8a2e
OG
586static void
587__skb_flow_dissect_ipv4(const struct sk_buff *skb,
588 struct flow_dissector *flow_dissector,
589 void *target_container, void *data, const struct iphdr *iph)
590{
591 struct flow_dissector_key_ip *key_ip;
592
593 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
594 return;
595
596 key_ip = skb_flow_dissector_target(flow_dissector,
597 FLOW_DISSECTOR_KEY_IP,
598 target_container);
599 key_ip->tos = iph->tos;
600 key_ip->ttl = iph->ttl;
601}
602
603static void
604__skb_flow_dissect_ipv6(const struct sk_buff *skb,
605 struct flow_dissector *flow_dissector,
606 void *target_container, void *data, const struct ipv6hdr *iph)
607{
608 struct flow_dissector_key_ip *key_ip;
609
610 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
611 return;
612
613 key_ip = skb_flow_dissector_target(flow_dissector,
614 FLOW_DISSECTOR_KEY_IP,
615 target_container);
616 key_ip->tos = ipv6_get_dsfield(iph);
617 key_ip->ttl = iph->hop_limit;
618}
619
1eed4dfb
TH
620/* Maximum number of protocol headers that can be parsed in
621 * __skb_flow_dissect
622 */
623#define MAX_FLOW_DISSECT_HDRS 15
624
625static bool skb_flow_dissect_allowed(int *num_hdrs)
626{
627 ++*num_hdrs;
628
629 return (*num_hdrs <= MAX_FLOW_DISSECT_HDRS);
630}
631
d58e468b
PP
632static void __skb_flow_bpf_to_target(const struct bpf_flow_keys *flow_keys,
633 struct flow_dissector *flow_dissector,
634 void *target_container)
635{
636 struct flow_dissector_key_control *key_control;
637 struct flow_dissector_key_basic *key_basic;
638 struct flow_dissector_key_addrs *key_addrs;
639 struct flow_dissector_key_ports *key_ports;
640
641 key_control = skb_flow_dissector_target(flow_dissector,
642 FLOW_DISSECTOR_KEY_CONTROL,
643 target_container);
644 key_control->thoff = flow_keys->thoff;
645 if (flow_keys->is_frag)
646 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
647 if (flow_keys->is_first_frag)
648 key_control->flags |= FLOW_DIS_FIRST_FRAG;
649 if (flow_keys->is_encap)
650 key_control->flags |= FLOW_DIS_ENCAPSULATION;
651
652 key_basic = skb_flow_dissector_target(flow_dissector,
653 FLOW_DISSECTOR_KEY_BASIC,
654 target_container);
655 key_basic->n_proto = flow_keys->n_proto;
656 key_basic->ip_proto = flow_keys->ip_proto;
657
658 if (flow_keys->addr_proto == ETH_P_IP &&
659 dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
660 key_addrs = skb_flow_dissector_target(flow_dissector,
661 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
662 target_container);
663 key_addrs->v4addrs.src = flow_keys->ipv4_src;
664 key_addrs->v4addrs.dst = flow_keys->ipv4_dst;
665 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
666 } else if (flow_keys->addr_proto == ETH_P_IPV6 &&
667 dissector_uses_key(flow_dissector,
668 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
669 key_addrs = skb_flow_dissector_target(flow_dissector,
670 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
671 target_container);
672 memcpy(&key_addrs->v6addrs, &flow_keys->ipv6_src,
673 sizeof(key_addrs->v6addrs));
674 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
675 }
676
677 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS)) {
678 key_ports = skb_flow_dissector_target(flow_dissector,
679 FLOW_DISSECTOR_KEY_PORTS,
680 target_container);
681 key_ports->src = flow_keys->sport;
682 key_ports->dst = flow_keys->dport;
683 }
684}
685
453a940e
WC
686/**
687 * __skb_flow_dissect - extract the flow_keys struct and return it
688 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
06635a35
JP
689 * @flow_dissector: list of keys to dissect
690 * @target_container: target structure to put dissected values into
453a940e
WC
691 * @data: raw buffer pointer to the packet, if NULL use skb->data
692 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
693 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
694 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
695 *
06635a35
JP
696 * The function will try to retrieve individual keys into target specified
697 * by flow_dissector from either the skbuff or a raw buffer specified by the
698 * rest parameters.
699 *
700 * Caller must take care of zeroing target container memory.
453a940e 701 */
06635a35
JP
702bool __skb_flow_dissect(const struct sk_buff *skb,
703 struct flow_dissector *flow_dissector,
704 void *target_container,
cd79a238
TH
705 void *data, __be16 proto, int nhoff, int hlen,
706 unsigned int flags)
0744dd00 707{
42aecaa9 708 struct flow_dissector_key_control *key_control;
06635a35
JP
709 struct flow_dissector_key_basic *key_basic;
710 struct flow_dissector_key_addrs *key_addrs;
711 struct flow_dissector_key_ports *key_ports;
972d3876 712 struct flow_dissector_key_icmp *key_icmp;
d34af823 713 struct flow_dissector_key_tags *key_tags;
f6a66927 714 struct flow_dissector_key_vlan *key_vlan;
3a1214e8 715 enum flow_dissect_ret fdret;
24c590e3 716 enum flow_dissector_key_id dissector_vlan = FLOW_DISSECTOR_KEY_MAX;
d0e13a14 717 struct bpf_prog *attached = NULL;
1eed4dfb 718 int num_hdrs = 0;
8e690ffd 719 u8 ip_proto = 0;
34fad54c 720 bool ret;
0744dd00 721
690e36e7
DM
722 if (!data) {
723 data = skb->data;
d5709f7a
HHZ
724 proto = skb_vlan_tag_present(skb) ?
725 skb->vlan_proto : skb->protocol;
453a940e 726 nhoff = skb_network_offset(skb);
690e36e7 727 hlen = skb_headlen(skb);
2d571645 728#if IS_ENABLED(CONFIG_NET_DSA)
7324157b 729 if (unlikely(skb->dev && netdev_uses_dsa(skb->dev))) {
43e66528
JC
730 const struct dsa_device_ops *ops;
731 int offset;
732
733 ops = skb->dev->dsa_ptr->tag_ops;
734 if (ops->flow_dissect &&
735 !ops->flow_dissect(skb, &proto, &offset)) {
736 hlen -= offset;
737 nhoff += offset;
738 }
739 }
2d571645 740#endif
690e36e7
DM
741 }
742
42aecaa9
TH
743 /* It is ensured by skb_flow_dissector_init() that control key will
744 * be always present.
745 */
746 key_control = skb_flow_dissector_target(flow_dissector,
747 FLOW_DISSECTOR_KEY_CONTROL,
748 target_container);
749
06635a35
JP
750 /* It is ensured by skb_flow_dissector_init() that basic key will
751 * be always present.
752 */
753 key_basic = skb_flow_dissector_target(flow_dissector,
754 FLOW_DISSECTOR_KEY_BASIC,
755 target_container);
0744dd00 756
d58e468b 757 rcu_read_lock();
d0e13a14
WB
758 if (skb) {
759 if (skb->dev)
760 attached = rcu_dereference(dev_net(skb->dev)->flow_dissector_prog);
761 else if (skb->sk)
762 attached = rcu_dereference(sock_net(skb->sk)->flow_dissector_prog);
763 else
764 WARN_ON_ONCE(1);
765 }
d58e468b
PP
766 if (attached) {
767 /* Note that even though the const qualifier is discarded
768 * throughout the execution of the BPF program, all changes(the
769 * control block) are reverted after the BPF program returns.
770 * Therefore, __skb_flow_dissect does not alter the skb.
771 */
772 struct bpf_flow_keys flow_keys = {};
773 struct bpf_skb_data_end cb_saved;
774 struct bpf_skb_data_end *cb;
775 u32 result;
776
777 cb = (struct bpf_skb_data_end *)skb->cb;
778
779 /* Save Control Block */
780 memcpy(&cb_saved, cb, sizeof(cb_saved));
781 memset(cb, 0, sizeof(cb_saved));
782
783 /* Pass parameters to the BPF program */
784 cb->qdisc_cb.flow_keys = &flow_keys;
785 flow_keys.nhoff = nhoff;
786
787 bpf_compute_data_pointers((struct sk_buff *)skb);
788 result = BPF_PROG_RUN(attached, skb);
789
790 /* Restore state */
791 memcpy(cb, &cb_saved, sizeof(cb_saved));
792
793 __skb_flow_bpf_to_target(&flow_keys, flow_dissector,
794 target_container);
795 key_control->thoff = min_t(u16, key_control->thoff, skb->len);
796 rcu_read_unlock();
797 return result == BPF_OK;
798 }
799 rcu_read_unlock();
800
20a17bf6
DM
801 if (dissector_uses_key(flow_dissector,
802 FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
67a900cc
JP
803 struct ethhdr *eth = eth_hdr(skb);
804 struct flow_dissector_key_eth_addrs *key_eth_addrs;
805
806 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
807 FLOW_DISSECTOR_KEY_ETH_ADDRS,
808 target_container);
809 memcpy(key_eth_addrs, &eth->h_dest, sizeof(*key_eth_addrs));
810 }
811
c5ef188e 812proto_again:
3a1214e8
TH
813 fdret = FLOW_DISSECT_RET_CONTINUE;
814
0744dd00 815 switch (proto) {
2b8837ae 816 case htons(ETH_P_IP): {
0744dd00
ED
817 const struct iphdr *iph;
818 struct iphdr _iph;
3a1214e8 819
690e36e7 820 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
3a1214e8
TH
821 if (!iph || iph->ihl < 5) {
822 fdret = FLOW_DISSECT_RET_OUT_BAD;
823 break;
824 }
825
3797d3e8 826 nhoff += iph->ihl * 4;
0744dd00 827
3797d3e8 828 ip_proto = iph->protocol;
3797d3e8 829
918c023f
AD
830 if (dissector_uses_key(flow_dissector,
831 FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
832 key_addrs = skb_flow_dissector_target(flow_dissector,
833 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
834 target_container);
835
836 memcpy(&key_addrs->v4addrs, &iph->saddr,
837 sizeof(key_addrs->v4addrs));
838 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
839 }
807e165d
TH
840
841 if (ip_is_fragment(iph)) {
4b36993d 842 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
807e165d
TH
843
844 if (iph->frag_off & htons(IP_OFFSET)) {
3a1214e8
TH
845 fdret = FLOW_DISSECT_RET_OUT_GOOD;
846 break;
807e165d 847 } else {
4b36993d 848 key_control->flags |= FLOW_DIS_FIRST_FRAG;
3a1214e8
TH
849 if (!(flags &
850 FLOW_DISSECTOR_F_PARSE_1ST_FRAG)) {
851 fdret = FLOW_DISSECT_RET_OUT_GOOD;
852 break;
853 }
807e165d
TH
854 }
855 }
856
518d8a2e
OG
857 __skb_flow_dissect_ipv4(skb, flow_dissector,
858 target_container, data, iph);
859
3a1214e8
TH
860 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3) {
861 fdret = FLOW_DISSECT_RET_OUT_GOOD;
862 break;
863 }
8306b688 864
0744dd00
ED
865 break;
866 }
2b8837ae 867 case htons(ETH_P_IPV6): {
0744dd00
ED
868 const struct ipv6hdr *iph;
869 struct ipv6hdr _iph;
19469a87 870
690e36e7 871 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
3a1214e8
TH
872 if (!iph) {
873 fdret = FLOW_DISSECT_RET_OUT_BAD;
874 break;
875 }
0744dd00
ED
876
877 ip_proto = iph->nexthdr;
0744dd00 878 nhoff += sizeof(struct ipv6hdr);
19469a87 879
20a17bf6
DM
880 if (dissector_uses_key(flow_dissector,
881 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
b3c3106c
AD
882 key_addrs = skb_flow_dissector_target(flow_dissector,
883 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
884 target_container);
5af7fb6e 885
b3c3106c
AD
886 memcpy(&key_addrs->v6addrs, &iph->saddr,
887 sizeof(key_addrs->v6addrs));
c3f83241 888 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
b924933c 889 }
87ee9e52 890
461547f3
AD
891 if ((dissector_uses_key(flow_dissector,
892 FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
893 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
894 ip6_flowlabel(iph)) {
895 __be32 flow_label = ip6_flowlabel(iph);
896
20a17bf6
DM
897 if (dissector_uses_key(flow_dissector,
898 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
87ee9e52
TH
899 key_tags = skb_flow_dissector_target(flow_dissector,
900 FLOW_DISSECTOR_KEY_FLOW_LABEL,
901 target_container);
902 key_tags->flow_label = ntohl(flow_label);
12c227ec 903 }
3a1214e8
TH
904 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL) {
905 fdret = FLOW_DISSECT_RET_OUT_GOOD;
906 break;
907 }
19469a87
TH
908 }
909
518d8a2e
OG
910 __skb_flow_dissect_ipv6(skb, flow_dissector,
911 target_container, data, iph);
912
8306b688 913 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
3a1214e8 914 fdret = FLOW_DISSECT_RET_OUT_GOOD;
8306b688 915
0744dd00
ED
916 break;
917 }
2b8837ae
JP
918 case htons(ETH_P_8021AD):
919 case htons(ETH_P_8021Q): {
24c590e3 920 const struct vlan_hdr *vlan = NULL;
bc72f3dd 921 struct vlan_hdr _vlan;
2064c3d4 922 __be16 saved_vlan_tpid = proto;
0744dd00 923
24c590e3
JL
924 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX &&
925 skb && skb_vlan_tag_present(skb)) {
d5709f7a 926 proto = skb->protocol;
24c590e3 927 } else {
d5709f7a
HHZ
928 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
929 data, hlen, &_vlan);
3a1214e8
TH
930 if (!vlan) {
931 fdret = FLOW_DISSECT_RET_OUT_BAD;
932 break;
933 }
934
d5709f7a
HHZ
935 proto = vlan->h_vlan_encapsulated_proto;
936 nhoff += sizeof(*vlan);
d5709f7a 937 }
0744dd00 938
24c590e3
JL
939 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX) {
940 dissector_vlan = FLOW_DISSECTOR_KEY_VLAN;
941 } else if (dissector_vlan == FLOW_DISSECTOR_KEY_VLAN) {
942 dissector_vlan = FLOW_DISSECTOR_KEY_CVLAN;
943 } else {
944 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
945 break;
946 }
947
948 if (dissector_uses_key(flow_dissector, dissector_vlan)) {
f6a66927 949 key_vlan = skb_flow_dissector_target(flow_dissector,
24c590e3 950 dissector_vlan,
d34af823
TH
951 target_container);
952
24c590e3 953 if (!vlan) {
f6a66927
HHZ
954 key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
955 key_vlan->vlan_priority =
956 (skb_vlan_tag_get_prio(skb) >> VLAN_PRIO_SHIFT);
957 } else {
958 key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) &
d5709f7a 959 VLAN_VID_MASK;
f6a66927
HHZ
960 key_vlan->vlan_priority =
961 (ntohs(vlan->h_vlan_TCI) &
962 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
963 }
2064c3d4 964 key_vlan->vlan_tpid = saved_vlan_tpid;
d34af823
TH
965 }
966
3a1214e8
TH
967 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
968 break;
0744dd00 969 }
2b8837ae 970 case htons(ETH_P_PPP_SES): {
0744dd00
ED
971 struct {
972 struct pppoe_hdr hdr;
973 __be16 proto;
974 } *hdr, _hdr;
690e36e7 975 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
3a1214e8
TH
976 if (!hdr) {
977 fdret = FLOW_DISSECT_RET_OUT_BAD;
978 break;
979 }
980
0744dd00
ED
981 proto = hdr->proto;
982 nhoff += PPPOE_SES_HLEN;
983 switch (proto) {
2b8837ae 984 case htons(PPP_IP):
3a1214e8
TH
985 proto = htons(ETH_P_IP);
986 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
987 break;
2b8837ae 988 case htons(PPP_IPV6):
3a1214e8
TH
989 proto = htons(ETH_P_IPV6);
990 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
991 break;
0744dd00 992 default:
3a1214e8
TH
993 fdret = FLOW_DISSECT_RET_OUT_BAD;
994 break;
0744dd00 995 }
3a1214e8 996 break;
0744dd00 997 }
08bfc9cb 998 case htons(ETH_P_TIPC): {
8d6e79d3
JM
999 struct tipc_basic_hdr *hdr, _hdr;
1000
1001 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr),
1002 data, hlen, &_hdr);
3a1214e8
TH
1003 if (!hdr) {
1004 fdret = FLOW_DISSECT_RET_OUT_BAD;
1005 break;
1006 }
06635a35 1007
20a17bf6 1008 if (dissector_uses_key(flow_dissector,
8d6e79d3 1009 FLOW_DISSECTOR_KEY_TIPC)) {
06635a35 1010 key_addrs = skb_flow_dissector_target(flow_dissector,
8d6e79d3 1011 FLOW_DISSECTOR_KEY_TIPC,
06635a35 1012 target_container);
8d6e79d3
JM
1013 key_addrs->tipckey.key = tipc_hdr_rps_key(hdr);
1014 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC;
06635a35 1015 }
3a1214e8
TH
1016 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1017 break;
08bfc9cb 1018 }
b3baa0fb
TH
1019
1020 case htons(ETH_P_MPLS_UC):
4a5d6c8b 1021 case htons(ETH_P_MPLS_MC):
3a1214e8 1022 fdret = __skb_flow_dissect_mpls(skb, flow_dissector,
4a5d6c8b 1023 target_container, data,
3a1214e8
TH
1024 nhoff, hlen);
1025 break;
56193d1b 1026 case htons(ETH_P_FCOE):
3a1214e8
TH
1027 if ((hlen - nhoff) < FCOE_HEADER_LEN) {
1028 fdret = FLOW_DISSECT_RET_OUT_BAD;
1029 break;
1030 }
224516b3
AD
1031
1032 nhoff += FCOE_HEADER_LEN;
3a1214e8
TH
1033 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1034 break;
55733350
SH
1035
1036 case htons(ETH_P_ARP):
9bf881ff 1037 case htons(ETH_P_RARP):
3a1214e8 1038 fdret = __skb_flow_dissect_arp(skb, flow_dissector,
9bf881ff 1039 target_container, data,
3a1214e8
TH
1040 nhoff, hlen);
1041 break;
1042
5b0890a9
SE
1043 case htons(ETH_P_BATMAN):
1044 fdret = __skb_flow_dissect_batadv(skb, key_control, data,
1045 &proto, &nhoff, hlen, flags);
1046 break;
1047
3a1214e8
TH
1048 default:
1049 fdret = FLOW_DISSECT_RET_OUT_BAD;
1050 break;
1051 }
1052
1053 /* Process result of proto processing */
1054 switch (fdret) {
1055 case FLOW_DISSECT_RET_OUT_GOOD:
1056 goto out_good;
1057 case FLOW_DISSECT_RET_PROTO_AGAIN:
1eed4dfb
TH
1058 if (skb_flow_dissect_allowed(&num_hdrs))
1059 goto proto_again;
1060 goto out_good;
3a1214e8
TH
1061 case FLOW_DISSECT_RET_CONTINUE:
1062 case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1063 break;
1064 case FLOW_DISSECT_RET_OUT_BAD:
0744dd00 1065 default:
a6e544b0 1066 goto out_bad;
0744dd00
ED
1067 }
1068
6a74fcf4 1069ip_proto_again:
3a1214e8
TH
1070 fdret = FLOW_DISSECT_RET_CONTINUE;
1071
0744dd00 1072 switch (ip_proto) {
7c92de8e 1073 case IPPROTO_GRE:
3a1214e8 1074 fdret = __skb_flow_dissect_gre(skb, key_control, flow_dissector,
7c92de8e 1075 target_container, data,
3a1214e8
TH
1076 &proto, &nhoff, &hlen, flags);
1077 break;
1078
6a74fcf4
TH
1079 case NEXTHDR_HOP:
1080 case NEXTHDR_ROUTING:
1081 case NEXTHDR_DEST: {
1082 u8 _opthdr[2], *opthdr;
1083
1084 if (proto != htons(ETH_P_IPV6))
1085 break;
1086
1087 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
1088 data, hlen, &_opthdr);
3a1214e8
TH
1089 if (!opthdr) {
1090 fdret = FLOW_DISSECT_RET_OUT_BAD;
1091 break;
1092 }
6a74fcf4 1093
1e98a0f0
ED
1094 ip_proto = opthdr[0];
1095 nhoff += (opthdr[1] + 1) << 3;
6a74fcf4 1096
3a1214e8
TH
1097 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1098 break;
6a74fcf4 1099 }
b840f28b
TH
1100 case NEXTHDR_FRAGMENT: {
1101 struct frag_hdr _fh, *fh;
1102
1103 if (proto != htons(ETH_P_IPV6))
1104 break;
1105
1106 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
1107 data, hlen, &_fh);
1108
3a1214e8
TH
1109 if (!fh) {
1110 fdret = FLOW_DISSECT_RET_OUT_BAD;
1111 break;
1112 }
b840f28b 1113
4b36993d 1114 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
b840f28b
TH
1115
1116 nhoff += sizeof(_fh);
43d2ccb3 1117 ip_proto = fh->nexthdr;
b840f28b
TH
1118
1119 if (!(fh->frag_off & htons(IP6_OFFSET))) {
4b36993d 1120 key_control->flags |= FLOW_DIS_FIRST_FRAG;
3a1214e8
TH
1121 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) {
1122 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1123 break;
1124 }
b840f28b 1125 }
3a1214e8
TH
1126
1127 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1128 break;
b840f28b 1129 }
0744dd00 1130 case IPPROTO_IPIP:
fca41895 1131 proto = htons(ETH_P_IP);
823b9693 1132
4b36993d 1133 key_control->flags |= FLOW_DIS_ENCAPSULATION;
3a1214e8
TH
1134 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1135 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1136 break;
1137 }
1138
1139 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1140 break;
823b9693 1141
b438f940
TH
1142 case IPPROTO_IPV6:
1143 proto = htons(ETH_P_IPV6);
823b9693 1144
4b36993d 1145 key_control->flags |= FLOW_DIS_ENCAPSULATION;
3a1214e8
TH
1146 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1147 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1148 break;
1149 }
1150
1151 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1152 break;
1153
823b9693 1154
b3baa0fb
TH
1155 case IPPROTO_MPLS:
1156 proto = htons(ETH_P_MPLS_UC);
3a1214e8
TH
1157 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1158 break;
1159
ac4bb5de
JP
1160 case IPPROTO_TCP:
1161 __skb_flow_dissect_tcp(skb, flow_dissector, target_container,
1162 data, nhoff, hlen);
1163 break;
3a1214e8 1164
0744dd00
ED
1165 default:
1166 break;
1167 }
1168
20a17bf6
DM
1169 if (dissector_uses_key(flow_dissector,
1170 FLOW_DISSECTOR_KEY_PORTS)) {
06635a35
JP
1171 key_ports = skb_flow_dissector_target(flow_dissector,
1172 FLOW_DISSECTOR_KEY_PORTS,
1173 target_container);
1174 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
1175 data, hlen);
1176 }
5af7fb6e 1177
972d3876
SH
1178 if (dissector_uses_key(flow_dissector,
1179 FLOW_DISSECTOR_KEY_ICMP)) {
1180 key_icmp = skb_flow_dissector_target(flow_dissector,
1181 FLOW_DISSECTOR_KEY_ICMP,
1182 target_container);
1183 key_icmp->icmp = skb_flow_get_be16(skb, nhoff, data, hlen);
1184 }
1185
3a1214e8
TH
1186 /* Process result of IP proto processing */
1187 switch (fdret) {
1188 case FLOW_DISSECT_RET_PROTO_AGAIN:
1eed4dfb
TH
1189 if (skb_flow_dissect_allowed(&num_hdrs))
1190 goto proto_again;
1191 break;
3a1214e8 1192 case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1eed4dfb
TH
1193 if (skb_flow_dissect_allowed(&num_hdrs))
1194 goto ip_proto_again;
1195 break;
3a1214e8
TH
1196 case FLOW_DISSECT_RET_OUT_GOOD:
1197 case FLOW_DISSECT_RET_CONTINUE:
1198 break;
1199 case FLOW_DISSECT_RET_OUT_BAD:
1200 default:
1201 goto out_bad;
1202 }
1203
a6e544b0
TH
1204out_good:
1205 ret = true;
1206
34fad54c 1207out:
d0c081b4 1208 key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
a6e544b0
TH
1209 key_basic->n_proto = proto;
1210 key_basic->ip_proto = ip_proto;
a6e544b0
TH
1211
1212 return ret;
34fad54c
ED
1213
1214out_bad:
1215 ret = false;
34fad54c 1216 goto out;
0744dd00 1217}
690e36e7 1218EXPORT_SYMBOL(__skb_flow_dissect);
441d9d32
CW
1219
1220static u32 hashrnd __read_mostly;
66415cf8
HFS
1221static __always_inline void __flow_hash_secret_init(void)
1222{
1223 net_get_random_once(&hashrnd, sizeof(hashrnd));
1224}
1225
20a17bf6
DM
1226static __always_inline u32 __flow_hash_words(const u32 *words, u32 length,
1227 u32 keyval)
42aecaa9
TH
1228{
1229 return jhash2(words, length, keyval);
1230}
1231
20a17bf6 1232static inline const u32 *flow_keys_hash_start(const struct flow_keys *flow)
66415cf8 1233{
20a17bf6
DM
1234 const void *p = flow;
1235
42aecaa9 1236 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32));
20a17bf6 1237 return (const u32 *)(p + FLOW_KEYS_HASH_OFFSET);
42aecaa9
TH
1238}
1239
20a17bf6 1240static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
42aecaa9 1241{
c3f83241 1242 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
42aecaa9 1243 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
c3f83241
TH
1244 BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
1245 sizeof(*flow) - sizeof(flow->addrs));
1246
1247 switch (flow->control.addr_type) {
1248 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1249 diff -= sizeof(flow->addrs.v4addrs);
1250 break;
1251 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1252 diff -= sizeof(flow->addrs.v6addrs);
1253 break;
8d6e79d3
JM
1254 case FLOW_DISSECTOR_KEY_TIPC:
1255 diff -= sizeof(flow->addrs.tipckey);
9f249089 1256 break;
c3f83241
TH
1257 }
1258 return (sizeof(*flow) - diff) / sizeof(u32);
1259}
1260
1261__be32 flow_get_u32_src(const struct flow_keys *flow)
1262{
1263 switch (flow->control.addr_type) {
1264 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1265 return flow->addrs.v4addrs.src;
1266 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1267 return (__force __be32)ipv6_addr_hash(
1268 &flow->addrs.v6addrs.src);
8d6e79d3
JM
1269 case FLOW_DISSECTOR_KEY_TIPC:
1270 return flow->addrs.tipckey.key;
c3f83241
TH
1271 default:
1272 return 0;
1273 }
1274}
1275EXPORT_SYMBOL(flow_get_u32_src);
1276
1277__be32 flow_get_u32_dst(const struct flow_keys *flow)
1278{
1279 switch (flow->control.addr_type) {
1280 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1281 return flow->addrs.v4addrs.dst;
1282 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1283 return (__force __be32)ipv6_addr_hash(
1284 &flow->addrs.v6addrs.dst);
1285 default:
1286 return 0;
1287 }
1288}
1289EXPORT_SYMBOL(flow_get_u32_dst);
1290
1291static inline void __flow_hash_consistentify(struct flow_keys *keys)
1292{
1293 int addr_diff, i;
1294
1295 switch (keys->control.addr_type) {
1296 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1297 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
1298 (__force u32)keys->addrs.v4addrs.src;
1299 if ((addr_diff < 0) ||
1300 (addr_diff == 0 &&
1301 ((__force u16)keys->ports.dst <
1302 (__force u16)keys->ports.src))) {
1303 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
1304 swap(keys->ports.src, keys->ports.dst);
1305 }
1306 break;
1307 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1308 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
1309 &keys->addrs.v6addrs.src,
1310 sizeof(keys->addrs.v6addrs.dst));
1311 if ((addr_diff < 0) ||
1312 (addr_diff == 0 &&
1313 ((__force u16)keys->ports.dst <
1314 (__force u16)keys->ports.src))) {
1315 for (i = 0; i < 4; i++)
1316 swap(keys->addrs.v6addrs.src.s6_addr32[i],
1317 keys->addrs.v6addrs.dst.s6_addr32[i]);
1318 swap(keys->ports.src, keys->ports.dst);
1319 }
1320 break;
1321 }
66415cf8
HFS
1322}
1323
50fb7992 1324static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
5ed20a68
TH
1325{
1326 u32 hash;
1327
c3f83241 1328 __flow_hash_consistentify(keys);
5ed20a68 1329
20a17bf6 1330 hash = __flow_hash_words(flow_keys_hash_start(keys),
42aecaa9 1331 flow_keys_hash_length(keys), keyval);
5ed20a68
TH
1332 if (!hash)
1333 hash = 1;
1334
1335 return hash;
1336}
1337
1338u32 flow_hash_from_keys(struct flow_keys *keys)
1339{
50fb7992
TH
1340 __flow_hash_secret_init();
1341 return __flow_hash_from_keys(keys, hashrnd);
5ed20a68
TH
1342}
1343EXPORT_SYMBOL(flow_hash_from_keys);
1344
50fb7992
TH
1345static inline u32 ___skb_get_hash(const struct sk_buff *skb,
1346 struct flow_keys *keys, u32 keyval)
1347{
6db61d79
TH
1348 skb_flow_dissect_flow_keys(skb, keys,
1349 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
50fb7992
TH
1350
1351 return __flow_hash_from_keys(keys, keyval);
1352}
1353
2f59e1eb
TH
1354struct _flow_keys_digest_data {
1355 __be16 n_proto;
1356 u8 ip_proto;
1357 u8 padding;
1358 __be32 ports;
1359 __be32 src;
1360 __be32 dst;
1361};
1362
1363void make_flow_keys_digest(struct flow_keys_digest *digest,
1364 const struct flow_keys *flow)
1365{
1366 struct _flow_keys_digest_data *data =
1367 (struct _flow_keys_digest_data *)digest;
1368
1369 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
1370
1371 memset(digest, 0, sizeof(*digest));
1372
06635a35
JP
1373 data->n_proto = flow->basic.n_proto;
1374 data->ip_proto = flow->basic.ip_proto;
1375 data->ports = flow->ports.ports;
c3f83241
TH
1376 data->src = flow->addrs.v4addrs.src;
1377 data->dst = flow->addrs.v4addrs.dst;
2f59e1eb
TH
1378}
1379EXPORT_SYMBOL(make_flow_keys_digest);
1380
eb70db87
DM
1381static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
1382
b917783c 1383u32 __skb_get_hash_symmetric(const struct sk_buff *skb)
eb70db87
DM
1384{
1385 struct flow_keys keys;
1386
1387 __flow_hash_secret_init();
1388
1389 memset(&keys, 0, sizeof(keys));
1390 __skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys,
1391 NULL, 0, 0, 0,
1392 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1393
1394 return __flow_hash_from_keys(&keys, hashrnd);
1395}
1396EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
1397
d4fd3275
JP
1398/**
1399 * __skb_get_hash: calculate a flow hash
1400 * @skb: sk_buff to calculate flow hash from
1401 *
1402 * This function calculates a flow hash based on src/dst addresses
61b905da
TH
1403 * and src/dst port numbers. Sets hash in skb to non-zero hash value
1404 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
441d9d32
CW
1405 * if hash is a canonical 4-tuple hash over transport ports.
1406 */
3958afa1 1407void __skb_get_hash(struct sk_buff *skb)
441d9d32
CW
1408{
1409 struct flow_keys keys;
635c223c 1410 u32 hash;
441d9d32 1411
50fb7992
TH
1412 __flow_hash_secret_init();
1413
635c223c
GF
1414 hash = ___skb_get_hash(skb, &keys, hashrnd);
1415
1416 __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
441d9d32 1417}
3958afa1 1418EXPORT_SYMBOL(__skb_get_hash);
441d9d32 1419
50fb7992
TH
1420__u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
1421{
1422 struct flow_keys keys;
1423
1424 return ___skb_get_hash(skb, &keys, perturb);
1425}
1426EXPORT_SYMBOL(skb_get_hash_perturb);
1427
56193d1b 1428u32 __skb_get_poff(const struct sk_buff *skb, void *data,
72a338bc 1429 const struct flow_keys_basic *keys, int hlen)
f77668dc 1430{
42aecaa9 1431 u32 poff = keys->control.thoff;
f77668dc 1432
43d2ccb3
AD
1433 /* skip L4 headers for fragments after the first */
1434 if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
1435 !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
1436 return poff;
1437
06635a35 1438 switch (keys->basic.ip_proto) {
f77668dc 1439 case IPPROTO_TCP: {
5af7fb6e
AD
1440 /* access doff as u8 to avoid unaligned access */
1441 const u8 *doff;
1442 u8 _doff;
f77668dc 1443
5af7fb6e
AD
1444 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
1445 data, hlen, &_doff);
1446 if (!doff)
f77668dc
DB
1447 return poff;
1448
5af7fb6e 1449 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
f77668dc
DB
1450 break;
1451 }
1452 case IPPROTO_UDP:
1453 case IPPROTO_UDPLITE:
1454 poff += sizeof(struct udphdr);
1455 break;
1456 /* For the rest, we do not really care about header
1457 * extensions at this point for now.
1458 */
1459 case IPPROTO_ICMP:
1460 poff += sizeof(struct icmphdr);
1461 break;
1462 case IPPROTO_ICMPV6:
1463 poff += sizeof(struct icmp6hdr);
1464 break;
1465 case IPPROTO_IGMP:
1466 poff += sizeof(struct igmphdr);
1467 break;
1468 case IPPROTO_DCCP:
1469 poff += sizeof(struct dccp_hdr);
1470 break;
1471 case IPPROTO_SCTP:
1472 poff += sizeof(struct sctphdr);
1473 break;
1474 }
1475
1476 return poff;
1477}
1478
0db89b8b
JP
1479/**
1480 * skb_get_poff - get the offset to the payload
1481 * @skb: sk_buff to get the payload offset from
1482 *
1483 * The function will get the offset to the payload as far as it could
1484 * be dissected. The main user is currently BPF, so that we can dynamically
56193d1b
AD
1485 * truncate packets without needing to push actual payload to the user
1486 * space and can analyze headers only, instead.
1487 */
1488u32 skb_get_poff(const struct sk_buff *skb)
1489{
72a338bc 1490 struct flow_keys_basic keys;
56193d1b 1491
d869dea6 1492 if (!skb_flow_dissect_flow_keys_basic(skb, &keys, NULL, 0, 0, 0, 0))
56193d1b
AD
1493 return 0;
1494
1495 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
1496}
06635a35 1497
20a17bf6 1498__u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
a17ace95
DM
1499{
1500 memset(keys, 0, sizeof(*keys));
1501
1502 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
1503 sizeof(keys->addrs.v6addrs.src));
1504 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
1505 sizeof(keys->addrs.v6addrs.dst));
1506 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1507 keys->ports.src = fl6->fl6_sport;
1508 keys->ports.dst = fl6->fl6_dport;
1509 keys->keyid.keyid = fl6->fl6_gre_key;
fa1be7e0 1510 keys->tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
a17ace95
DM
1511 keys->basic.ip_proto = fl6->flowi6_proto;
1512
1513 return flow_hash_from_keys(keys);
1514}
1515EXPORT_SYMBOL(__get_hash_from_flowi6);
1516
06635a35 1517static const struct flow_dissector_key flow_keys_dissector_keys[] = {
42aecaa9
TH
1518 {
1519 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1520 .offset = offsetof(struct flow_keys, control),
1521 },
06635a35
JP
1522 {
1523 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1524 .offset = offsetof(struct flow_keys, basic),
1525 },
1526 {
1527 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
c3f83241
TH
1528 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1529 },
1530 {
1531 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1532 .offset = offsetof(struct flow_keys, addrs.v6addrs),
06635a35 1533 },
9f249089 1534 {
8d6e79d3
JM
1535 .key_id = FLOW_DISSECTOR_KEY_TIPC,
1536 .offset = offsetof(struct flow_keys, addrs.tipckey),
9f249089 1537 },
06635a35
JP
1538 {
1539 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1540 .offset = offsetof(struct flow_keys, ports),
1541 },
d34af823 1542 {
f6a66927
HHZ
1543 .key_id = FLOW_DISSECTOR_KEY_VLAN,
1544 .offset = offsetof(struct flow_keys, vlan),
d34af823 1545 },
87ee9e52
TH
1546 {
1547 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
1548 .offset = offsetof(struct flow_keys, tags),
1549 },
1fdd512c
TH
1550 {
1551 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
1552 .offset = offsetof(struct flow_keys, keyid),
1553 },
06635a35
JP
1554};
1555
eb70db87
DM
1556static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
1557 {
1558 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1559 .offset = offsetof(struct flow_keys, control),
1560 },
1561 {
1562 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1563 .offset = offsetof(struct flow_keys, basic),
1564 },
1565 {
1566 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1567 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1568 },
1569 {
1570 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1571 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1572 },
1573 {
1574 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1575 .offset = offsetof(struct flow_keys, ports),
1576 },
1577};
1578
72a338bc 1579static const struct flow_dissector_key flow_keys_basic_dissector_keys[] = {
42aecaa9
TH
1580 {
1581 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1582 .offset = offsetof(struct flow_keys, control),
1583 },
06635a35
JP
1584 {
1585 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1586 .offset = offsetof(struct flow_keys, basic),
1587 },
1588};
1589
1590struct flow_dissector flow_keys_dissector __read_mostly;
1591EXPORT_SYMBOL(flow_keys_dissector);
1592
72a338bc
PA
1593struct flow_dissector flow_keys_basic_dissector __read_mostly;
1594EXPORT_SYMBOL(flow_keys_basic_dissector);
06635a35
JP
1595
1596static int __init init_default_flow_dissectors(void)
1597{
1598 skb_flow_dissector_init(&flow_keys_dissector,
1599 flow_keys_dissector_keys,
1600 ARRAY_SIZE(flow_keys_dissector_keys));
eb70db87
DM
1601 skb_flow_dissector_init(&flow_keys_dissector_symmetric,
1602 flow_keys_dissector_symmetric_keys,
1603 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
72a338bc
PA
1604 skb_flow_dissector_init(&flow_keys_basic_dissector,
1605 flow_keys_basic_dissector_keys,
1606 ARRAY_SIZE(flow_keys_basic_dissector_keys));
06635a35
JP
1607 return 0;
1608}
1609
c9b8af13 1610core_initcall(init_default_flow_dissectors);
This page took 0.647585 seconds and 4 git commands to generate.