Merge remote-tracking branch 'net-next/master'
[deliverable/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>
7#include <net/ip.h>
ddbe5032 8#include <net/ipv6.h>
ab10dccb
GF
9#include <net/gre.h>
10#include <net/pptp.h>
f77668dc
DB
11#include <linux/igmp.h>
12#include <linux/icmp.h>
13#include <linux/sctp.h>
14#include <linux/dccp.h>
0744dd00
ED
15#include <linux/if_tunnel.h>
16#include <linux/if_pppox.h>
17#include <linux/ppp_defs.h>
06635a35 18#include <linux/stddef.h>
67a900cc 19#include <linux/if_ether.h>
b3baa0fb 20#include <linux/mpls.h>
1bd758eb 21#include <net/flow_dissector.h>
56193d1b 22#include <scsi/fc/fc_fcoe.h>
0744dd00 23
20a17bf6
DM
24static void dissector_set_key(struct flow_dissector *flow_dissector,
25 enum flow_dissector_key_id key_id)
fbff949e
JP
26{
27 flow_dissector->used_keys |= (1 << key_id);
28}
29
fbff949e
JP
30void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
31 const struct flow_dissector_key *key,
32 unsigned int key_count)
33{
34 unsigned int i;
35
36 memset(flow_dissector, 0, sizeof(*flow_dissector));
37
38 for (i = 0; i < key_count; i++, key++) {
39 /* User should make sure that every key target offset is withing
40 * boundaries of unsigned short.
41 */
42 BUG_ON(key->offset > USHRT_MAX);
20a17bf6
DM
43 BUG_ON(dissector_uses_key(flow_dissector,
44 key->key_id));
fbff949e 45
20a17bf6 46 dissector_set_key(flow_dissector, key->key_id);
fbff949e
JP
47 flow_dissector->offset[key->key_id] = key->offset;
48 }
49
42aecaa9
TH
50 /* Ensure that the dissector always includes control and basic key.
51 * That way we are able to avoid handling lack of these in fast path.
fbff949e 52 */
20a17bf6
DM
53 BUG_ON(!dissector_uses_key(flow_dissector,
54 FLOW_DISSECTOR_KEY_CONTROL));
55 BUG_ON(!dissector_uses_key(flow_dissector,
56 FLOW_DISSECTOR_KEY_BASIC));
fbff949e
JP
57}
58EXPORT_SYMBOL(skb_flow_dissector_init);
59
357afe9c 60/**
6451b3f5
WC
61 * __skb_flow_get_ports - extract the upper layer ports and return them
62 * @skb: sk_buff to extract the ports from
357afe9c
NA
63 * @thoff: transport header offset
64 * @ip_proto: protocol for which to get port offset
6451b3f5
WC
65 * @data: raw buffer pointer to the packet, if NULL use skb->data
66 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
357afe9c
NA
67 *
68 * The function will try to retrieve the ports at offset thoff + poff where poff
69 * is the protocol port offset returned from proto_ports_offset
70 */
690e36e7
DM
71__be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
72 void *data, int hlen)
357afe9c
NA
73{
74 int poff = proto_ports_offset(ip_proto);
75
690e36e7
DM
76 if (!data) {
77 data = skb->data;
78 hlen = skb_headlen(skb);
79 }
80
357afe9c
NA
81 if (poff >= 0) {
82 __be32 *ports, _ports;
83
690e36e7
DM
84 ports = __skb_header_pointer(skb, thoff + poff,
85 sizeof(_ports), data, hlen, &_ports);
357afe9c
NA
86 if (ports)
87 return *ports;
88 }
89
90 return 0;
91}
690e36e7 92EXPORT_SYMBOL(__skb_flow_get_ports);
357afe9c 93
453a940e
WC
94/**
95 * __skb_flow_dissect - extract the flow_keys struct and return it
96 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
06635a35
JP
97 * @flow_dissector: list of keys to dissect
98 * @target_container: target structure to put dissected values into
453a940e
WC
99 * @data: raw buffer pointer to the packet, if NULL use skb->data
100 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
101 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
102 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
103 *
06635a35
JP
104 * The function will try to retrieve individual keys into target specified
105 * by flow_dissector from either the skbuff or a raw buffer specified by the
106 * rest parameters.
107 *
108 * Caller must take care of zeroing target container memory.
453a940e 109 */
06635a35
JP
110bool __skb_flow_dissect(const struct sk_buff *skb,
111 struct flow_dissector *flow_dissector,
112 void *target_container,
cd79a238
TH
113 void *data, __be16 proto, int nhoff, int hlen,
114 unsigned int flags)
0744dd00 115{
42aecaa9 116 struct flow_dissector_key_control *key_control;
06635a35
JP
117 struct flow_dissector_key_basic *key_basic;
118 struct flow_dissector_key_addrs *key_addrs;
119 struct flow_dissector_key_ports *key_ports;
d34af823 120 struct flow_dissector_key_tags *key_tags;
f6a66927 121 struct flow_dissector_key_vlan *key_vlan;
1fdd512c 122 struct flow_dissector_key_keyid *key_keyid;
d5709f7a 123 bool skip_vlan = false;
8e690ffd 124 u8 ip_proto = 0;
a6e544b0 125 bool ret = false;
0744dd00 126
690e36e7
DM
127 if (!data) {
128 data = skb->data;
d5709f7a
HHZ
129 proto = skb_vlan_tag_present(skb) ?
130 skb->vlan_proto : skb->protocol;
453a940e 131 nhoff = skb_network_offset(skb);
690e36e7
DM
132 hlen = skb_headlen(skb);
133 }
134
42aecaa9
TH
135 /* It is ensured by skb_flow_dissector_init() that control key will
136 * be always present.
137 */
138 key_control = skb_flow_dissector_target(flow_dissector,
139 FLOW_DISSECTOR_KEY_CONTROL,
140 target_container);
141
06635a35
JP
142 /* It is ensured by skb_flow_dissector_init() that basic key will
143 * be always present.
144 */
145 key_basic = skb_flow_dissector_target(flow_dissector,
146 FLOW_DISSECTOR_KEY_BASIC,
147 target_container);
0744dd00 148
20a17bf6
DM
149 if (dissector_uses_key(flow_dissector,
150 FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
67a900cc
JP
151 struct ethhdr *eth = eth_hdr(skb);
152 struct flow_dissector_key_eth_addrs *key_eth_addrs;
153
154 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
155 FLOW_DISSECTOR_KEY_ETH_ADDRS,
156 target_container);
157 memcpy(key_eth_addrs, &eth->h_dest, sizeof(*key_eth_addrs));
158 }
159
0744dd00
ED
160again:
161 switch (proto) {
2b8837ae 162 case htons(ETH_P_IP): {
0744dd00
ED
163 const struct iphdr *iph;
164 struct iphdr _iph;
165ip:
690e36e7 166 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
6f092343 167 if (!iph || iph->ihl < 5)
a6e544b0 168 goto out_bad;
3797d3e8 169 nhoff += iph->ihl * 4;
0744dd00 170
3797d3e8 171 ip_proto = iph->protocol;
3797d3e8 172
918c023f
AD
173 if (dissector_uses_key(flow_dissector,
174 FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
175 key_addrs = skb_flow_dissector_target(flow_dissector,
176 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
177 target_container);
178
179 memcpy(&key_addrs->v4addrs, &iph->saddr,
180 sizeof(key_addrs->v4addrs));
181 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
182 }
807e165d
TH
183
184 if (ip_is_fragment(iph)) {
4b36993d 185 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
807e165d
TH
186
187 if (iph->frag_off & htons(IP_OFFSET)) {
188 goto out_good;
189 } else {
4b36993d 190 key_control->flags |= FLOW_DIS_FIRST_FRAG;
807e165d
TH
191 if (!(flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG))
192 goto out_good;
193 }
194 }
195
8306b688
TH
196 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
197 goto out_good;
198
0744dd00
ED
199 break;
200 }
2b8837ae 201 case htons(ETH_P_IPV6): {
0744dd00
ED
202 const struct ipv6hdr *iph;
203 struct ipv6hdr _iph;
19469a87 204
0744dd00 205ipv6:
690e36e7 206 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
0744dd00 207 if (!iph)
a6e544b0 208 goto out_bad;
0744dd00
ED
209
210 ip_proto = iph->nexthdr;
0744dd00 211 nhoff += sizeof(struct ipv6hdr);
19469a87 212
20a17bf6
DM
213 if (dissector_uses_key(flow_dissector,
214 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
b3c3106c
AD
215 key_addrs = skb_flow_dissector_target(flow_dissector,
216 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
217 target_container);
5af7fb6e 218
b3c3106c
AD
219 memcpy(&key_addrs->v6addrs, &iph->saddr,
220 sizeof(key_addrs->v6addrs));
c3f83241 221 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
b924933c 222 }
87ee9e52 223
461547f3
AD
224 if ((dissector_uses_key(flow_dissector,
225 FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
226 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
227 ip6_flowlabel(iph)) {
228 __be32 flow_label = ip6_flowlabel(iph);
229
20a17bf6
DM
230 if (dissector_uses_key(flow_dissector,
231 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
87ee9e52
TH
232 key_tags = skb_flow_dissector_target(flow_dissector,
233 FLOW_DISSECTOR_KEY_FLOW_LABEL,
234 target_container);
235 key_tags->flow_label = ntohl(flow_label);
12c227ec 236 }
872b1abb
TH
237 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)
238 goto out_good;
19469a87
TH
239 }
240
8306b688
TH
241 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
242 goto out_good;
243
0744dd00
ED
244 break;
245 }
2b8837ae
JP
246 case htons(ETH_P_8021AD):
247 case htons(ETH_P_8021Q): {
0744dd00 248 const struct vlan_hdr *vlan;
0744dd00 249
d5709f7a
HHZ
250 if (skb_vlan_tag_present(skb))
251 proto = skb->protocol;
252
253 if (!skb_vlan_tag_present(skb) ||
254 proto == cpu_to_be16(ETH_P_8021Q) ||
255 proto == cpu_to_be16(ETH_P_8021AD)) {
256 struct vlan_hdr _vlan;
257
258 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
259 data, hlen, &_vlan);
260 if (!vlan)
261 goto out_bad;
262 proto = vlan->h_vlan_encapsulated_proto;
263 nhoff += sizeof(*vlan);
264 if (skip_vlan)
265 goto again;
266 }
0744dd00 267
d5709f7a 268 skip_vlan = true;
20a17bf6 269 if (dissector_uses_key(flow_dissector,
f6a66927
HHZ
270 FLOW_DISSECTOR_KEY_VLAN)) {
271 key_vlan = skb_flow_dissector_target(flow_dissector,
272 FLOW_DISSECTOR_KEY_VLAN,
d34af823
TH
273 target_container);
274
f6a66927
HHZ
275 if (skb_vlan_tag_present(skb)) {
276 key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
277 key_vlan->vlan_priority =
278 (skb_vlan_tag_get_prio(skb) >> VLAN_PRIO_SHIFT);
279 } else {
280 key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) &
d5709f7a 281 VLAN_VID_MASK;
f6a66927
HHZ
282 key_vlan->vlan_priority =
283 (ntohs(vlan->h_vlan_TCI) &
284 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
285 }
d34af823
TH
286 }
287
0744dd00
ED
288 goto again;
289 }
2b8837ae 290 case htons(ETH_P_PPP_SES): {
0744dd00
ED
291 struct {
292 struct pppoe_hdr hdr;
293 __be16 proto;
294 } *hdr, _hdr;
690e36e7 295 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
0744dd00 296 if (!hdr)
a6e544b0 297 goto out_bad;
0744dd00
ED
298 proto = hdr->proto;
299 nhoff += PPPOE_SES_HLEN;
300 switch (proto) {
2b8837ae 301 case htons(PPP_IP):
0744dd00 302 goto ip;
2b8837ae 303 case htons(PPP_IPV6):
0744dd00
ED
304 goto ipv6;
305 default:
a6e544b0 306 goto out_bad;
0744dd00
ED
307 }
308 }
08bfc9cb
EH
309 case htons(ETH_P_TIPC): {
310 struct {
311 __be32 pre[3];
312 __be32 srcnode;
313 } *hdr, _hdr;
314 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
315 if (!hdr)
a6e544b0 316 goto out_bad;
06635a35 317
20a17bf6
DM
318 if (dissector_uses_key(flow_dissector,
319 FLOW_DISSECTOR_KEY_TIPC_ADDRS)) {
06635a35 320 key_addrs = skb_flow_dissector_target(flow_dissector,
9f249089 321 FLOW_DISSECTOR_KEY_TIPC_ADDRS,
06635a35 322 target_container);
9f249089
TH
323 key_addrs->tipcaddrs.srcnode = hdr->srcnode;
324 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC_ADDRS;
06635a35 325 }
a6e544b0 326 goto out_good;
08bfc9cb 327 }
b3baa0fb
TH
328
329 case htons(ETH_P_MPLS_UC):
330 case htons(ETH_P_MPLS_MC): {
331 struct mpls_label *hdr, _hdr[2];
332mpls:
333 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
334 hlen, &_hdr);
335 if (!hdr)
a6e544b0 336 goto out_bad;
b3baa0fb 337
611d23c5
TH
338 if ((ntohl(hdr[0].entry) & MPLS_LS_LABEL_MASK) >>
339 MPLS_LS_LABEL_SHIFT == MPLS_LABEL_ENTROPY) {
20a17bf6
DM
340 if (dissector_uses_key(flow_dissector,
341 FLOW_DISSECTOR_KEY_MPLS_ENTROPY)) {
b3baa0fb
TH
342 key_keyid = skb_flow_dissector_target(flow_dissector,
343 FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
344 target_container);
345 key_keyid->keyid = hdr[1].entry &
346 htonl(MPLS_LS_LABEL_MASK);
347 }
348
a6e544b0 349 goto out_good;
b3baa0fb
TH
350 }
351
a6e544b0 352 goto out_good;
b3baa0fb
TH
353 }
354
56193d1b 355 case htons(ETH_P_FCOE):
224516b3
AD
356 if ((hlen - nhoff) < FCOE_HEADER_LEN)
357 goto out_bad;
358
359 nhoff += FCOE_HEADER_LEN;
360 goto out_good;
0744dd00 361 default:
a6e544b0 362 goto out_bad;
0744dd00
ED
363 }
364
6a74fcf4 365ip_proto_again:
0744dd00
ED
366 switch (ip_proto) {
367 case IPPROTO_GRE: {
ab10dccb
GF
368 struct gre_base_hdr *hdr, _hdr;
369 u16 gre_ver;
370 int offset = 0;
0744dd00 371
690e36e7 372 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
0744dd00 373 if (!hdr)
a6e544b0 374 goto out_bad;
ab10dccb
GF
375
376 /* Only look inside GRE without routing */
377 if (hdr->flags & GRE_ROUTING)
ce3b5355
TH
378 break;
379
ab10dccb
GF
380 /* Only look inside GRE for version 0 and 1 */
381 gre_ver = ntohs(hdr->flags & GRE_VERSION);
382 if (gre_ver > 1)
383 break;
384
385 proto = hdr->protocol;
386 if (gre_ver) {
387 /* Version1 must be PPTP, and check the flags */
388 if (!(proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY)))
389 break;
390 }
391
392 offset += sizeof(struct gre_base_hdr);
393
ce3b5355 394 if (hdr->flags & GRE_CSUM)
ab10dccb
GF
395 offset += sizeof(((struct gre_full_hdr *)0)->csum) +
396 sizeof(((struct gre_full_hdr *)0)->reserved1);
397
1fdd512c
TH
398 if (hdr->flags & GRE_KEY) {
399 const __be32 *keyid;
400 __be32 _keyid;
401
ab10dccb 402 keyid = __skb_header_pointer(skb, nhoff + offset, sizeof(_keyid),
1fdd512c 403 data, hlen, &_keyid);
1fdd512c 404 if (!keyid)
a6e544b0 405 goto out_bad;
1fdd512c 406
20a17bf6
DM
407 if (dissector_uses_key(flow_dissector,
408 FLOW_DISSECTOR_KEY_GRE_KEYID)) {
1fdd512c
TH
409 key_keyid = skb_flow_dissector_target(flow_dissector,
410 FLOW_DISSECTOR_KEY_GRE_KEYID,
411 target_container);
ab10dccb
GF
412 if (gre_ver == 0)
413 key_keyid->keyid = *keyid;
414 else
415 key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK;
1fdd512c 416 }
ab10dccb 417 offset += sizeof(((struct gre_full_hdr *)0)->key);
1fdd512c 418 }
ab10dccb 419
ce3b5355 420 if (hdr->flags & GRE_SEQ)
ab10dccb
GF
421 offset += sizeof(((struct pptp_gre_header *)0)->seq);
422
423 if (gre_ver == 0) {
424 if (proto == htons(ETH_P_TEB)) {
425 const struct ethhdr *eth;
426 struct ethhdr _eth;
427
428 eth = __skb_header_pointer(skb, nhoff + offset,
429 sizeof(_eth),
430 data, hlen, &_eth);
431 if (!eth)
432 goto out_bad;
433 proto = eth->h_proto;
434 offset += sizeof(*eth);
435
436 /* Cap headers that we access via pointers at the
437 * end of the Ethernet header as our maximum alignment
438 * at that point is only 2 bytes.
439 */
440 if (NET_IP_ALIGN)
441 hlen = (nhoff + offset);
442 }
443 } else { /* version 1, must be PPTP */
444 u8 _ppp_hdr[PPP_HDRLEN];
445 u8 *ppp_hdr;
446
447 if (hdr->flags & GRE_ACK)
448 offset += sizeof(((struct pptp_gre_header *)0)->ack);
449
450 ppp_hdr = skb_header_pointer(skb, nhoff + offset,
451 sizeof(_ppp_hdr), _ppp_hdr);
452 if (!ppp_hdr)
a6e544b0 453 goto out_bad;
ab10dccb
GF
454
455 switch (PPP_PROTOCOL(ppp_hdr)) {
456 case PPP_IP:
457 proto = htons(ETH_P_IP);
458 break;
459 case PPP_IPV6:
460 proto = htons(ETH_P_IPV6);
461 break;
462 default:
463 /* Could probably catch some more like MPLS */
464 break;
465 }
466
467 offset += PPP_HDRLEN;
0744dd00 468 }
823b9693 469
ab10dccb 470 nhoff += offset;
4b36993d 471 key_control->flags |= FLOW_DIS_ENCAPSULATION;
823b9693
TH
472 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
473 goto out_good;
474
ce3b5355 475 goto again;
0744dd00 476 }
6a74fcf4
TH
477 case NEXTHDR_HOP:
478 case NEXTHDR_ROUTING:
479 case NEXTHDR_DEST: {
480 u8 _opthdr[2], *opthdr;
481
482 if (proto != htons(ETH_P_IPV6))
483 break;
484
485 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
486 data, hlen, &_opthdr);
1e98a0f0 487 if (!opthdr)
a6e544b0 488 goto out_bad;
6a74fcf4 489
1e98a0f0
ED
490 ip_proto = opthdr[0];
491 nhoff += (opthdr[1] + 1) << 3;
6a74fcf4
TH
492
493 goto ip_proto_again;
494 }
b840f28b
TH
495 case NEXTHDR_FRAGMENT: {
496 struct frag_hdr _fh, *fh;
497
498 if (proto != htons(ETH_P_IPV6))
499 break;
500
501 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
502 data, hlen, &_fh);
503
504 if (!fh)
505 goto out_bad;
506
4b36993d 507 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
b840f28b
TH
508
509 nhoff += sizeof(_fh);
43d2ccb3 510 ip_proto = fh->nexthdr;
b840f28b
TH
511
512 if (!(fh->frag_off & htons(IP6_OFFSET))) {
4b36993d 513 key_control->flags |= FLOW_DIS_FIRST_FRAG;
43d2ccb3 514 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG)
b840f28b 515 goto ip_proto_again;
b840f28b
TH
516 }
517 goto out_good;
518 }
0744dd00 519 case IPPROTO_IPIP:
fca41895 520 proto = htons(ETH_P_IP);
823b9693 521
4b36993d 522 key_control->flags |= FLOW_DIS_ENCAPSULATION;
823b9693
TH
523 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
524 goto out_good;
525
fca41895 526 goto ip;
b438f940
TH
527 case IPPROTO_IPV6:
528 proto = htons(ETH_P_IPV6);
823b9693 529
4b36993d 530 key_control->flags |= FLOW_DIS_ENCAPSULATION;
823b9693
TH
531 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
532 goto out_good;
533
b438f940 534 goto ipv6;
b3baa0fb
TH
535 case IPPROTO_MPLS:
536 proto = htons(ETH_P_MPLS_UC);
537 goto mpls;
0744dd00
ED
538 default:
539 break;
540 }
541
20a17bf6
DM
542 if (dissector_uses_key(flow_dissector,
543 FLOW_DISSECTOR_KEY_PORTS)) {
06635a35
JP
544 key_ports = skb_flow_dissector_target(flow_dissector,
545 FLOW_DISSECTOR_KEY_PORTS,
546 target_container);
547 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
548 data, hlen);
549 }
5af7fb6e 550
a6e544b0
TH
551out_good:
552 ret = true;
553
554out_bad:
555 key_basic->n_proto = proto;
556 key_basic->ip_proto = ip_proto;
557 key_control->thoff = (u16)nhoff;
558
559 return ret;
0744dd00 560}
690e36e7 561EXPORT_SYMBOL(__skb_flow_dissect);
441d9d32
CW
562
563static u32 hashrnd __read_mostly;
66415cf8
HFS
564static __always_inline void __flow_hash_secret_init(void)
565{
566 net_get_random_once(&hashrnd, sizeof(hashrnd));
567}
568
20a17bf6
DM
569static __always_inline u32 __flow_hash_words(const u32 *words, u32 length,
570 u32 keyval)
42aecaa9
TH
571{
572 return jhash2(words, length, keyval);
573}
574
20a17bf6 575static inline const u32 *flow_keys_hash_start(const struct flow_keys *flow)
66415cf8 576{
20a17bf6
DM
577 const void *p = flow;
578
42aecaa9 579 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32));
20a17bf6 580 return (const u32 *)(p + FLOW_KEYS_HASH_OFFSET);
42aecaa9
TH
581}
582
20a17bf6 583static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
42aecaa9 584{
c3f83241 585 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
42aecaa9 586 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
c3f83241
TH
587 BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
588 sizeof(*flow) - sizeof(flow->addrs));
589
590 switch (flow->control.addr_type) {
591 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
592 diff -= sizeof(flow->addrs.v4addrs);
593 break;
594 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
595 diff -= sizeof(flow->addrs.v6addrs);
596 break;
9f249089
TH
597 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
598 diff -= sizeof(flow->addrs.tipcaddrs);
599 break;
c3f83241
TH
600 }
601 return (sizeof(*flow) - diff) / sizeof(u32);
602}
603
604__be32 flow_get_u32_src(const struct flow_keys *flow)
605{
606 switch (flow->control.addr_type) {
607 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
608 return flow->addrs.v4addrs.src;
609 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
610 return (__force __be32)ipv6_addr_hash(
611 &flow->addrs.v6addrs.src);
9f249089
TH
612 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
613 return flow->addrs.tipcaddrs.srcnode;
c3f83241
TH
614 default:
615 return 0;
616 }
617}
618EXPORT_SYMBOL(flow_get_u32_src);
619
620__be32 flow_get_u32_dst(const struct flow_keys *flow)
621{
622 switch (flow->control.addr_type) {
623 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
624 return flow->addrs.v4addrs.dst;
625 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
626 return (__force __be32)ipv6_addr_hash(
627 &flow->addrs.v6addrs.dst);
628 default:
629 return 0;
630 }
631}
632EXPORT_SYMBOL(flow_get_u32_dst);
633
634static inline void __flow_hash_consistentify(struct flow_keys *keys)
635{
636 int addr_diff, i;
637
638 switch (keys->control.addr_type) {
639 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
640 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
641 (__force u32)keys->addrs.v4addrs.src;
642 if ((addr_diff < 0) ||
643 (addr_diff == 0 &&
644 ((__force u16)keys->ports.dst <
645 (__force u16)keys->ports.src))) {
646 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
647 swap(keys->ports.src, keys->ports.dst);
648 }
649 break;
650 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
651 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
652 &keys->addrs.v6addrs.src,
653 sizeof(keys->addrs.v6addrs.dst));
654 if ((addr_diff < 0) ||
655 (addr_diff == 0 &&
656 ((__force u16)keys->ports.dst <
657 (__force u16)keys->ports.src))) {
658 for (i = 0; i < 4; i++)
659 swap(keys->addrs.v6addrs.src.s6_addr32[i],
660 keys->addrs.v6addrs.dst.s6_addr32[i]);
661 swap(keys->ports.src, keys->ports.dst);
662 }
663 break;
664 }
66415cf8
HFS
665}
666
50fb7992 667static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
5ed20a68
TH
668{
669 u32 hash;
670
c3f83241 671 __flow_hash_consistentify(keys);
5ed20a68 672
20a17bf6 673 hash = __flow_hash_words(flow_keys_hash_start(keys),
42aecaa9 674 flow_keys_hash_length(keys), keyval);
5ed20a68
TH
675 if (!hash)
676 hash = 1;
677
678 return hash;
679}
680
681u32 flow_hash_from_keys(struct flow_keys *keys)
682{
50fb7992
TH
683 __flow_hash_secret_init();
684 return __flow_hash_from_keys(keys, hashrnd);
5ed20a68
TH
685}
686EXPORT_SYMBOL(flow_hash_from_keys);
687
50fb7992
TH
688static inline u32 ___skb_get_hash(const struct sk_buff *skb,
689 struct flow_keys *keys, u32 keyval)
690{
6db61d79
TH
691 skb_flow_dissect_flow_keys(skb, keys,
692 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
50fb7992
TH
693
694 return __flow_hash_from_keys(keys, keyval);
695}
696
2f59e1eb
TH
697struct _flow_keys_digest_data {
698 __be16 n_proto;
699 u8 ip_proto;
700 u8 padding;
701 __be32 ports;
702 __be32 src;
703 __be32 dst;
704};
705
706void make_flow_keys_digest(struct flow_keys_digest *digest,
707 const struct flow_keys *flow)
708{
709 struct _flow_keys_digest_data *data =
710 (struct _flow_keys_digest_data *)digest;
711
712 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
713
714 memset(digest, 0, sizeof(*digest));
715
06635a35
JP
716 data->n_proto = flow->basic.n_proto;
717 data->ip_proto = flow->basic.ip_proto;
718 data->ports = flow->ports.ports;
c3f83241
TH
719 data->src = flow->addrs.v4addrs.src;
720 data->dst = flow->addrs.v4addrs.dst;
2f59e1eb
TH
721}
722EXPORT_SYMBOL(make_flow_keys_digest);
723
eb70db87
DM
724static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
725
726u32 __skb_get_hash_symmetric(struct sk_buff *skb)
727{
728 struct flow_keys keys;
729
730 __flow_hash_secret_init();
731
732 memset(&keys, 0, sizeof(keys));
733 __skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys,
734 NULL, 0, 0, 0,
735 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
736
737 return __flow_hash_from_keys(&keys, hashrnd);
738}
739EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
740
d4fd3275
JP
741/**
742 * __skb_get_hash: calculate a flow hash
743 * @skb: sk_buff to calculate flow hash from
744 *
745 * This function calculates a flow hash based on src/dst addresses
61b905da
TH
746 * and src/dst port numbers. Sets hash in skb to non-zero hash value
747 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
441d9d32
CW
748 * if hash is a canonical 4-tuple hash over transport ports.
749 */
3958afa1 750void __skb_get_hash(struct sk_buff *skb)
441d9d32
CW
751{
752 struct flow_keys keys;
635c223c 753 u32 hash;
441d9d32 754
50fb7992
TH
755 __flow_hash_secret_init();
756
635c223c
GF
757 hash = ___skb_get_hash(skb, &keys, hashrnd);
758
759 __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
441d9d32 760}
3958afa1 761EXPORT_SYMBOL(__skb_get_hash);
441d9d32 762
50fb7992
TH
763__u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
764{
765 struct flow_keys keys;
766
767 return ___skb_get_hash(skb, &keys, perturb);
768}
769EXPORT_SYMBOL(skb_get_hash_perturb);
770
20a17bf6 771__u32 __skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6)
f70ea018
TH
772{
773 struct flow_keys keys;
774
775 memset(&keys, 0, sizeof(keys));
776
777 memcpy(&keys.addrs.v6addrs.src, &fl6->saddr,
778 sizeof(keys.addrs.v6addrs.src));
779 memcpy(&keys.addrs.v6addrs.dst, &fl6->daddr,
780 sizeof(keys.addrs.v6addrs.dst));
781 keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
782 keys.ports.src = fl6->fl6_sport;
783 keys.ports.dst = fl6->fl6_dport;
784 keys.keyid.keyid = fl6->fl6_gre_key;
785 keys.tags.flow_label = (__force u32)fl6->flowlabel;
786 keys.basic.ip_proto = fl6->flowi6_proto;
787
bcc83839
TH
788 __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
789 flow_keys_have_l4(&keys));
f70ea018
TH
790
791 return skb->hash;
792}
793EXPORT_SYMBOL(__skb_get_hash_flowi6);
794
20a17bf6 795__u32 __skb_get_hash_flowi4(struct sk_buff *skb, const struct flowi4 *fl4)
f70ea018
TH
796{
797 struct flow_keys keys;
798
799 memset(&keys, 0, sizeof(keys));
800
801 keys.addrs.v4addrs.src = fl4->saddr;
802 keys.addrs.v4addrs.dst = fl4->daddr;
803 keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
804 keys.ports.src = fl4->fl4_sport;
805 keys.ports.dst = fl4->fl4_dport;
806 keys.keyid.keyid = fl4->fl4_gre_key;
807 keys.basic.ip_proto = fl4->flowi4_proto;
808
bcc83839
TH
809 __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
810 flow_keys_have_l4(&keys));
f70ea018
TH
811
812 return skb->hash;
813}
814EXPORT_SYMBOL(__skb_get_hash_flowi4);
815
56193d1b
AD
816u32 __skb_get_poff(const struct sk_buff *skb, void *data,
817 const struct flow_keys *keys, int hlen)
f77668dc 818{
42aecaa9 819 u32 poff = keys->control.thoff;
f77668dc 820
43d2ccb3
AD
821 /* skip L4 headers for fragments after the first */
822 if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
823 !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
824 return poff;
825
06635a35 826 switch (keys->basic.ip_proto) {
f77668dc 827 case IPPROTO_TCP: {
5af7fb6e
AD
828 /* access doff as u8 to avoid unaligned access */
829 const u8 *doff;
830 u8 _doff;
f77668dc 831
5af7fb6e
AD
832 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
833 data, hlen, &_doff);
834 if (!doff)
f77668dc
DB
835 return poff;
836
5af7fb6e 837 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
f77668dc
DB
838 break;
839 }
840 case IPPROTO_UDP:
841 case IPPROTO_UDPLITE:
842 poff += sizeof(struct udphdr);
843 break;
844 /* For the rest, we do not really care about header
845 * extensions at this point for now.
846 */
847 case IPPROTO_ICMP:
848 poff += sizeof(struct icmphdr);
849 break;
850 case IPPROTO_ICMPV6:
851 poff += sizeof(struct icmp6hdr);
852 break;
853 case IPPROTO_IGMP:
854 poff += sizeof(struct igmphdr);
855 break;
856 case IPPROTO_DCCP:
857 poff += sizeof(struct dccp_hdr);
858 break;
859 case IPPROTO_SCTP:
860 poff += sizeof(struct sctphdr);
861 break;
862 }
863
864 return poff;
865}
866
0db89b8b
JP
867/**
868 * skb_get_poff - get the offset to the payload
869 * @skb: sk_buff to get the payload offset from
870 *
871 * The function will get the offset to the payload as far as it could
872 * be dissected. The main user is currently BPF, so that we can dynamically
56193d1b
AD
873 * truncate packets without needing to push actual payload to the user
874 * space and can analyze headers only, instead.
875 */
876u32 skb_get_poff(const struct sk_buff *skb)
877{
878 struct flow_keys keys;
879
cd79a238 880 if (!skb_flow_dissect_flow_keys(skb, &keys, 0))
56193d1b
AD
881 return 0;
882
883 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
884}
06635a35 885
20a17bf6 886__u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
a17ace95
DM
887{
888 memset(keys, 0, sizeof(*keys));
889
890 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
891 sizeof(keys->addrs.v6addrs.src));
892 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
893 sizeof(keys->addrs.v6addrs.dst));
894 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
895 keys->ports.src = fl6->fl6_sport;
896 keys->ports.dst = fl6->fl6_dport;
897 keys->keyid.keyid = fl6->fl6_gre_key;
898 keys->tags.flow_label = (__force u32)fl6->flowlabel;
899 keys->basic.ip_proto = fl6->flowi6_proto;
900
901 return flow_hash_from_keys(keys);
902}
903EXPORT_SYMBOL(__get_hash_from_flowi6);
904
20a17bf6 905__u32 __get_hash_from_flowi4(const struct flowi4 *fl4, struct flow_keys *keys)
a17ace95
DM
906{
907 memset(keys, 0, sizeof(*keys));
908
909 keys->addrs.v4addrs.src = fl4->saddr;
910 keys->addrs.v4addrs.dst = fl4->daddr;
911 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
912 keys->ports.src = fl4->fl4_sport;
913 keys->ports.dst = fl4->fl4_dport;
914 keys->keyid.keyid = fl4->fl4_gre_key;
915 keys->basic.ip_proto = fl4->flowi4_proto;
916
917 return flow_hash_from_keys(keys);
918}
919EXPORT_SYMBOL(__get_hash_from_flowi4);
920
06635a35 921static const struct flow_dissector_key flow_keys_dissector_keys[] = {
42aecaa9
TH
922 {
923 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
924 .offset = offsetof(struct flow_keys, control),
925 },
06635a35
JP
926 {
927 .key_id = FLOW_DISSECTOR_KEY_BASIC,
928 .offset = offsetof(struct flow_keys, basic),
929 },
930 {
931 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
c3f83241
TH
932 .offset = offsetof(struct flow_keys, addrs.v4addrs),
933 },
934 {
935 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
936 .offset = offsetof(struct flow_keys, addrs.v6addrs),
06635a35 937 },
9f249089
TH
938 {
939 .key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS,
940 .offset = offsetof(struct flow_keys, addrs.tipcaddrs),
941 },
06635a35
JP
942 {
943 .key_id = FLOW_DISSECTOR_KEY_PORTS,
944 .offset = offsetof(struct flow_keys, ports),
945 },
d34af823 946 {
f6a66927
HHZ
947 .key_id = FLOW_DISSECTOR_KEY_VLAN,
948 .offset = offsetof(struct flow_keys, vlan),
d34af823 949 },
87ee9e52
TH
950 {
951 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
952 .offset = offsetof(struct flow_keys, tags),
953 },
1fdd512c
TH
954 {
955 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
956 .offset = offsetof(struct flow_keys, keyid),
957 },
06635a35
JP
958};
959
eb70db87
DM
960static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
961 {
962 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
963 .offset = offsetof(struct flow_keys, control),
964 },
965 {
966 .key_id = FLOW_DISSECTOR_KEY_BASIC,
967 .offset = offsetof(struct flow_keys, basic),
968 },
969 {
970 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
971 .offset = offsetof(struct flow_keys, addrs.v4addrs),
972 },
973 {
974 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
975 .offset = offsetof(struct flow_keys, addrs.v6addrs),
976 },
977 {
978 .key_id = FLOW_DISSECTOR_KEY_PORTS,
979 .offset = offsetof(struct flow_keys, ports),
980 },
981};
982
06635a35 983static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = {
42aecaa9
TH
984 {
985 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
986 .offset = offsetof(struct flow_keys, control),
987 },
06635a35
JP
988 {
989 .key_id = FLOW_DISSECTOR_KEY_BASIC,
990 .offset = offsetof(struct flow_keys, basic),
991 },
992};
993
994struct flow_dissector flow_keys_dissector __read_mostly;
995EXPORT_SYMBOL(flow_keys_dissector);
996
997struct flow_dissector flow_keys_buf_dissector __read_mostly;
998
999static int __init init_default_flow_dissectors(void)
1000{
1001 skb_flow_dissector_init(&flow_keys_dissector,
1002 flow_keys_dissector_keys,
1003 ARRAY_SIZE(flow_keys_dissector_keys));
eb70db87
DM
1004 skb_flow_dissector_init(&flow_keys_dissector_symmetric,
1005 flow_keys_dissector_symmetric_keys,
1006 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
06635a35
JP
1007 skb_flow_dissector_init(&flow_keys_buf_dissector,
1008 flow_keys_buf_dissector_keys,
1009 ARRAY_SIZE(flow_keys_buf_dissector_keys));
1010 return 0;
1011}
1012
1013late_initcall_sync(init_default_flow_dissectors);
This page took 0.317286 seconds and 5 git commands to generate.