Commit | Line | Data |
---|---|---|
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> |
f77668dc DB |
9 | #include <linux/igmp.h> |
10 | #include <linux/icmp.h> | |
11 | #include <linux/sctp.h> | |
12 | #include <linux/dccp.h> | |
0744dd00 ED |
13 | #include <linux/if_tunnel.h> |
14 | #include <linux/if_pppox.h> | |
15 | #include <linux/ppp_defs.h> | |
06635a35 | 16 | #include <linux/stddef.h> |
67a900cc | 17 | #include <linux/if_ether.h> |
1bd758eb | 18 | #include <net/flow_dissector.h> |
56193d1b | 19 | #include <scsi/fc/fc_fcoe.h> |
0744dd00 | 20 | |
fbff949e JP |
21 | static bool skb_flow_dissector_uses_key(struct flow_dissector *flow_dissector, |
22 | enum flow_dissector_key_id key_id) | |
23 | { | |
24 | return flow_dissector->used_keys & (1 << key_id); | |
25 | } | |
26 | ||
27 | static void skb_flow_dissector_set_key(struct flow_dissector *flow_dissector, | |
28 | enum flow_dissector_key_id key_id) | |
29 | { | |
30 | flow_dissector->used_keys |= (1 << key_id); | |
31 | } | |
32 | ||
33 | static void *skb_flow_dissector_target(struct flow_dissector *flow_dissector, | |
34 | enum flow_dissector_key_id key_id, | |
35 | void *target_container) | |
36 | { | |
37 | return ((char *) target_container) + flow_dissector->offset[key_id]; | |
38 | } | |
39 | ||
40 | void skb_flow_dissector_init(struct flow_dissector *flow_dissector, | |
41 | const struct flow_dissector_key *key, | |
42 | unsigned int key_count) | |
43 | { | |
44 | unsigned int i; | |
45 | ||
46 | memset(flow_dissector, 0, sizeof(*flow_dissector)); | |
47 | ||
48 | for (i = 0; i < key_count; i++, key++) { | |
49 | /* User should make sure that every key target offset is withing | |
50 | * boundaries of unsigned short. | |
51 | */ | |
52 | BUG_ON(key->offset > USHRT_MAX); | |
53 | BUG_ON(skb_flow_dissector_uses_key(flow_dissector, | |
54 | key->key_id)); | |
55 | ||
56 | skb_flow_dissector_set_key(flow_dissector, key->key_id); | |
57 | flow_dissector->offset[key->key_id] = key->offset; | |
58 | } | |
59 | ||
42aecaa9 TH |
60 | /* Ensure that the dissector always includes control and basic key. |
61 | * That way we are able to avoid handling lack of these in fast path. | |
fbff949e | 62 | */ |
42aecaa9 TH |
63 | BUG_ON(!skb_flow_dissector_uses_key(flow_dissector, |
64 | FLOW_DISSECTOR_KEY_CONTROL)); | |
fbff949e JP |
65 | BUG_ON(!skb_flow_dissector_uses_key(flow_dissector, |
66 | FLOW_DISSECTOR_KEY_BASIC)); | |
67 | } | |
68 | EXPORT_SYMBOL(skb_flow_dissector_init); | |
69 | ||
357afe9c | 70 | /** |
6451b3f5 WC |
71 | * __skb_flow_get_ports - extract the upper layer ports and return them |
72 | * @skb: sk_buff to extract the ports from | |
357afe9c NA |
73 | * @thoff: transport header offset |
74 | * @ip_proto: protocol for which to get port offset | |
6451b3f5 WC |
75 | * @data: raw buffer pointer to the packet, if NULL use skb->data |
76 | * @hlen: packet header length, if @data is NULL use skb_headlen(skb) | |
357afe9c NA |
77 | * |
78 | * The function will try to retrieve the ports at offset thoff + poff where poff | |
79 | * is the protocol port offset returned from proto_ports_offset | |
80 | */ | |
690e36e7 DM |
81 | __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto, |
82 | void *data, int hlen) | |
357afe9c NA |
83 | { |
84 | int poff = proto_ports_offset(ip_proto); | |
85 | ||
690e36e7 DM |
86 | if (!data) { |
87 | data = skb->data; | |
88 | hlen = skb_headlen(skb); | |
89 | } | |
90 | ||
357afe9c NA |
91 | if (poff >= 0) { |
92 | __be32 *ports, _ports; | |
93 | ||
690e36e7 DM |
94 | ports = __skb_header_pointer(skb, thoff + poff, |
95 | sizeof(_ports), data, hlen, &_ports); | |
357afe9c NA |
96 | if (ports) |
97 | return *ports; | |
98 | } | |
99 | ||
100 | return 0; | |
101 | } | |
690e36e7 | 102 | EXPORT_SYMBOL(__skb_flow_get_ports); |
357afe9c | 103 | |
453a940e WC |
104 | /** |
105 | * __skb_flow_dissect - extract the flow_keys struct and return it | |
106 | * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified | |
06635a35 JP |
107 | * @flow_dissector: list of keys to dissect |
108 | * @target_container: target structure to put dissected values into | |
453a940e WC |
109 | * @data: raw buffer pointer to the packet, if NULL use skb->data |
110 | * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol | |
111 | * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb) | |
112 | * @hlen: packet header length, if @data is NULL use skb_headlen(skb) | |
113 | * | |
06635a35 JP |
114 | * The function will try to retrieve individual keys into target specified |
115 | * by flow_dissector from either the skbuff or a raw buffer specified by the | |
116 | * rest parameters. | |
117 | * | |
118 | * Caller must take care of zeroing target container memory. | |
453a940e | 119 | */ |
06635a35 JP |
120 | bool __skb_flow_dissect(const struct sk_buff *skb, |
121 | struct flow_dissector *flow_dissector, | |
122 | void *target_container, | |
453a940e | 123 | void *data, __be16 proto, int nhoff, int hlen) |
0744dd00 | 124 | { |
42aecaa9 | 125 | struct flow_dissector_key_control *key_control; |
06635a35 JP |
126 | struct flow_dissector_key_basic *key_basic; |
127 | struct flow_dissector_key_addrs *key_addrs; | |
128 | struct flow_dissector_key_ports *key_ports; | |
d34af823 | 129 | struct flow_dissector_key_tags *key_tags; |
0744dd00 | 130 | u8 ip_proto; |
0744dd00 | 131 | |
690e36e7 DM |
132 | if (!data) { |
133 | data = skb->data; | |
453a940e WC |
134 | proto = skb->protocol; |
135 | nhoff = skb_network_offset(skb); | |
690e36e7 DM |
136 | hlen = skb_headlen(skb); |
137 | } | |
138 | ||
42aecaa9 TH |
139 | /* It is ensured by skb_flow_dissector_init() that control key will |
140 | * be always present. | |
141 | */ | |
142 | key_control = skb_flow_dissector_target(flow_dissector, | |
143 | FLOW_DISSECTOR_KEY_CONTROL, | |
144 | target_container); | |
145 | ||
06635a35 JP |
146 | /* It is ensured by skb_flow_dissector_init() that basic key will |
147 | * be always present. | |
148 | */ | |
149 | key_basic = skb_flow_dissector_target(flow_dissector, | |
150 | FLOW_DISSECTOR_KEY_BASIC, | |
151 | target_container); | |
0744dd00 | 152 | |
67a900cc JP |
153 | if (skb_flow_dissector_uses_key(flow_dissector, |
154 | FLOW_DISSECTOR_KEY_ETH_ADDRS)) { | |
155 | struct ethhdr *eth = eth_hdr(skb); | |
156 | struct flow_dissector_key_eth_addrs *key_eth_addrs; | |
157 | ||
158 | key_eth_addrs = skb_flow_dissector_target(flow_dissector, | |
159 | FLOW_DISSECTOR_KEY_ETH_ADDRS, | |
160 | target_container); | |
161 | memcpy(key_eth_addrs, ð->h_dest, sizeof(*key_eth_addrs)); | |
162 | } | |
163 | ||
0744dd00 ED |
164 | again: |
165 | switch (proto) { | |
2b8837ae | 166 | case htons(ETH_P_IP): { |
0744dd00 ED |
167 | const struct iphdr *iph; |
168 | struct iphdr _iph; | |
169 | ip: | |
690e36e7 | 170 | iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph); |
6f092343 | 171 | if (!iph || iph->ihl < 5) |
0744dd00 | 172 | return false; |
3797d3e8 | 173 | nhoff += iph->ihl * 4; |
0744dd00 | 174 | |
3797d3e8 | 175 | ip_proto = iph->protocol; |
0744dd00 ED |
176 | if (ip_is_fragment(iph)) |
177 | ip_proto = 0; | |
3797d3e8 | 178 | |
06635a35 JP |
179 | if (!skb_flow_dissector_uses_key(flow_dissector, |
180 | FLOW_DISSECTOR_KEY_IPV4_ADDRS)) | |
5af7fb6e | 181 | break; |
c3f83241 | 182 | |
06635a35 | 183 | key_addrs = skb_flow_dissector_target(flow_dissector, |
c3f83241 TH |
184 | FLOW_DISSECTOR_KEY_IPV4_ADDRS, target_container); |
185 | memcpy(&key_addrs->v4addrs, &iph->saddr, | |
186 | sizeof(key_addrs->v4addrs)); | |
187 | key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; | |
0744dd00 ED |
188 | break; |
189 | } | |
2b8837ae | 190 | case htons(ETH_P_IPV6): { |
0744dd00 ED |
191 | const struct ipv6hdr *iph; |
192 | struct ipv6hdr _iph; | |
19469a87 TH |
193 | __be32 flow_label; |
194 | ||
0744dd00 | 195 | ipv6: |
690e36e7 | 196 | iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph); |
0744dd00 ED |
197 | if (!iph) |
198 | return false; | |
199 | ||
200 | ip_proto = iph->nexthdr; | |
0744dd00 | 201 | nhoff += sizeof(struct ipv6hdr); |
19469a87 | 202 | |
b924933c JP |
203 | if (skb_flow_dissector_uses_key(flow_dissector, |
204 | FLOW_DISSECTOR_KEY_IPV6_ADDRS)) { | |
205 | struct flow_dissector_key_ipv6_addrs *key_ipv6_addrs; | |
206 | ||
207 | key_ipv6_addrs = skb_flow_dissector_target(flow_dissector, | |
208 | FLOW_DISSECTOR_KEY_IPV6_ADDRS, | |
209 | target_container); | |
5af7fb6e | 210 | |
b924933c | 211 | memcpy(key_ipv6_addrs, &iph->saddr, sizeof(*key_ipv6_addrs)); |
c3f83241 | 212 | key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
b924933c JP |
213 | goto flow_label; |
214 | } | |
215 | break; | |
216 | flow_label: | |
19469a87 TH |
217 | flow_label = ip6_flowlabel(iph); |
218 | if (flow_label) { | |
219 | /* Awesome, IPv6 packet has a flow label so we can | |
220 | * use that to represent the ports without any | |
221 | * further dissection. | |
222 | */ | |
06635a35 JP |
223 | |
224 | key_basic->n_proto = proto; | |
225 | key_basic->ip_proto = ip_proto; | |
42aecaa9 | 226 | key_control->thoff = (u16)nhoff; |
06635a35 | 227 | |
12c227ec JP |
228 | if (skb_flow_dissector_uses_key(flow_dissector, |
229 | FLOW_DISSECTOR_KEY_PORTS)) { | |
230 | key_ports = skb_flow_dissector_target(flow_dissector, | |
231 | FLOW_DISSECTOR_KEY_PORTS, | |
232 | target_container); | |
233 | key_ports->ports = flow_label; | |
234 | } | |
19469a87 TH |
235 | |
236 | return true; | |
237 | } | |
238 | ||
0744dd00 ED |
239 | break; |
240 | } | |
2b8837ae JP |
241 | case htons(ETH_P_8021AD): |
242 | case htons(ETH_P_8021Q): { | |
0744dd00 ED |
243 | const struct vlan_hdr *vlan; |
244 | struct vlan_hdr _vlan; | |
245 | ||
690e36e7 | 246 | vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), data, hlen, &_vlan); |
0744dd00 ED |
247 | if (!vlan) |
248 | return false; | |
249 | ||
d34af823 TH |
250 | if (skb_flow_dissector_uses_key(flow_dissector, |
251 | FLOW_DISSECTOR_KEY_VLANID)) { | |
252 | key_tags = skb_flow_dissector_target(flow_dissector, | |
253 | FLOW_DISSECTOR_KEY_VLANID, | |
254 | target_container); | |
255 | ||
256 | key_tags->vlan_id = skb_vlan_tag_get_id(skb); | |
257 | } | |
258 | ||
0744dd00 ED |
259 | proto = vlan->h_vlan_encapsulated_proto; |
260 | nhoff += sizeof(*vlan); | |
261 | goto again; | |
262 | } | |
2b8837ae | 263 | case htons(ETH_P_PPP_SES): { |
0744dd00 ED |
264 | struct { |
265 | struct pppoe_hdr hdr; | |
266 | __be16 proto; | |
267 | } *hdr, _hdr; | |
690e36e7 | 268 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); |
0744dd00 ED |
269 | if (!hdr) |
270 | return false; | |
271 | proto = hdr->proto; | |
272 | nhoff += PPPOE_SES_HLEN; | |
273 | switch (proto) { | |
2b8837ae | 274 | case htons(PPP_IP): |
0744dd00 | 275 | goto ip; |
2b8837ae | 276 | case htons(PPP_IPV6): |
0744dd00 ED |
277 | goto ipv6; |
278 | default: | |
279 | return false; | |
280 | } | |
281 | } | |
08bfc9cb EH |
282 | case htons(ETH_P_TIPC): { |
283 | struct { | |
284 | __be32 pre[3]; | |
285 | __be32 srcnode; | |
286 | } *hdr, _hdr; | |
287 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); | |
288 | if (!hdr) | |
289 | return false; | |
06635a35 | 290 | key_basic->n_proto = proto; |
42aecaa9 | 291 | key_control->thoff = (u16)nhoff; |
06635a35 JP |
292 | |
293 | if (skb_flow_dissector_uses_key(flow_dissector, | |
9f249089 | 294 | FLOW_DISSECTOR_KEY_TIPC_ADDRS)) { |
06635a35 | 295 | key_addrs = skb_flow_dissector_target(flow_dissector, |
9f249089 | 296 | FLOW_DISSECTOR_KEY_TIPC_ADDRS, |
06635a35 | 297 | target_container); |
9f249089 TH |
298 | key_addrs->tipcaddrs.srcnode = hdr->srcnode; |
299 | key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC_ADDRS; | |
06635a35 | 300 | } |
08bfc9cb EH |
301 | return true; |
302 | } | |
56193d1b | 303 | case htons(ETH_P_FCOE): |
42aecaa9 | 304 | key_control->thoff = (u16)(nhoff + FCOE_HEADER_LEN); |
56193d1b | 305 | /* fall through */ |
0744dd00 ED |
306 | default: |
307 | return false; | |
308 | } | |
309 | ||
310 | switch (ip_proto) { | |
311 | case IPPROTO_GRE: { | |
312 | struct gre_hdr { | |
313 | __be16 flags; | |
314 | __be16 proto; | |
315 | } *hdr, _hdr; | |
316 | ||
690e36e7 | 317 | hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); |
0744dd00 ED |
318 | if (!hdr) |
319 | return false; | |
320 | /* | |
321 | * Only look inside GRE if version zero and no | |
322 | * routing | |
323 | */ | |
ce3b5355 TH |
324 | if (hdr->flags & (GRE_VERSION | GRE_ROUTING)) |
325 | break; | |
326 | ||
327 | proto = hdr->proto; | |
328 | nhoff += 4; | |
329 | if (hdr->flags & GRE_CSUM) | |
0744dd00 | 330 | nhoff += 4; |
ce3b5355 TH |
331 | if (hdr->flags & GRE_KEY) |
332 | nhoff += 4; | |
333 | if (hdr->flags & GRE_SEQ) | |
334 | nhoff += 4; | |
335 | if (proto == htons(ETH_P_TEB)) { | |
336 | const struct ethhdr *eth; | |
337 | struct ethhdr _eth; | |
338 | ||
339 | eth = __skb_header_pointer(skb, nhoff, | |
340 | sizeof(_eth), | |
341 | data, hlen, &_eth); | |
342 | if (!eth) | |
343 | return false; | |
344 | proto = eth->h_proto; | |
345 | nhoff += sizeof(*eth); | |
0744dd00 | 346 | } |
ce3b5355 | 347 | goto again; |
0744dd00 ED |
348 | } |
349 | case IPPROTO_IPIP: | |
fca41895 TH |
350 | proto = htons(ETH_P_IP); |
351 | goto ip; | |
b438f940 TH |
352 | case IPPROTO_IPV6: |
353 | proto = htons(ETH_P_IPV6); | |
354 | goto ipv6; | |
0744dd00 ED |
355 | default: |
356 | break; | |
357 | } | |
358 | ||
06635a35 JP |
359 | key_basic->n_proto = proto; |
360 | key_basic->ip_proto = ip_proto; | |
42aecaa9 | 361 | key_control->thoff = (u16)nhoff; |
06635a35 JP |
362 | |
363 | if (skb_flow_dissector_uses_key(flow_dissector, | |
364 | FLOW_DISSECTOR_KEY_PORTS)) { | |
365 | key_ports = skb_flow_dissector_target(flow_dissector, | |
366 | FLOW_DISSECTOR_KEY_PORTS, | |
367 | target_container); | |
368 | key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto, | |
369 | data, hlen); | |
370 | } | |
5af7fb6e | 371 | |
0744dd00 ED |
372 | return true; |
373 | } | |
690e36e7 | 374 | EXPORT_SYMBOL(__skb_flow_dissect); |
441d9d32 CW |
375 | |
376 | static u32 hashrnd __read_mostly; | |
66415cf8 HFS |
377 | static __always_inline void __flow_hash_secret_init(void) |
378 | { | |
379 | net_get_random_once(&hashrnd, sizeof(hashrnd)); | |
380 | } | |
381 | ||
42aecaa9 TH |
382 | static __always_inline u32 __flow_hash_words(u32 *words, u32 length, u32 keyval) |
383 | { | |
384 | return jhash2(words, length, keyval); | |
385 | } | |
386 | ||
387 | static inline void *flow_keys_hash_start(struct flow_keys *flow) | |
66415cf8 | 388 | { |
42aecaa9 TH |
389 | BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32)); |
390 | return (void *)flow + FLOW_KEYS_HASH_OFFSET; | |
391 | } | |
392 | ||
393 | static inline size_t flow_keys_hash_length(struct flow_keys *flow) | |
394 | { | |
c3f83241 | 395 | size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs); |
42aecaa9 | 396 | BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32)); |
c3f83241 TH |
397 | BUILD_BUG_ON(offsetof(typeof(*flow), addrs) != |
398 | sizeof(*flow) - sizeof(flow->addrs)); | |
399 | ||
400 | switch (flow->control.addr_type) { | |
401 | case FLOW_DISSECTOR_KEY_IPV4_ADDRS: | |
402 | diff -= sizeof(flow->addrs.v4addrs); | |
403 | break; | |
404 | case FLOW_DISSECTOR_KEY_IPV6_ADDRS: | |
405 | diff -= sizeof(flow->addrs.v6addrs); | |
406 | break; | |
9f249089 TH |
407 | case FLOW_DISSECTOR_KEY_TIPC_ADDRS: |
408 | diff -= sizeof(flow->addrs.tipcaddrs); | |
409 | break; | |
c3f83241 TH |
410 | } |
411 | return (sizeof(*flow) - diff) / sizeof(u32); | |
412 | } | |
413 | ||
414 | __be32 flow_get_u32_src(const struct flow_keys *flow) | |
415 | { | |
416 | switch (flow->control.addr_type) { | |
417 | case FLOW_DISSECTOR_KEY_IPV4_ADDRS: | |
418 | return flow->addrs.v4addrs.src; | |
419 | case FLOW_DISSECTOR_KEY_IPV6_ADDRS: | |
420 | return (__force __be32)ipv6_addr_hash( | |
421 | &flow->addrs.v6addrs.src); | |
9f249089 TH |
422 | case FLOW_DISSECTOR_KEY_TIPC_ADDRS: |
423 | return flow->addrs.tipcaddrs.srcnode; | |
c3f83241 TH |
424 | default: |
425 | return 0; | |
426 | } | |
427 | } | |
428 | EXPORT_SYMBOL(flow_get_u32_src); | |
429 | ||
430 | __be32 flow_get_u32_dst(const struct flow_keys *flow) | |
431 | { | |
432 | switch (flow->control.addr_type) { | |
433 | case FLOW_DISSECTOR_KEY_IPV4_ADDRS: | |
434 | return flow->addrs.v4addrs.dst; | |
435 | case FLOW_DISSECTOR_KEY_IPV6_ADDRS: | |
436 | return (__force __be32)ipv6_addr_hash( | |
437 | &flow->addrs.v6addrs.dst); | |
438 | default: | |
439 | return 0; | |
440 | } | |
441 | } | |
442 | EXPORT_SYMBOL(flow_get_u32_dst); | |
443 | ||
444 | static inline void __flow_hash_consistentify(struct flow_keys *keys) | |
445 | { | |
446 | int addr_diff, i; | |
447 | ||
448 | switch (keys->control.addr_type) { | |
449 | case FLOW_DISSECTOR_KEY_IPV4_ADDRS: | |
450 | addr_diff = (__force u32)keys->addrs.v4addrs.dst - | |
451 | (__force u32)keys->addrs.v4addrs.src; | |
452 | if ((addr_diff < 0) || | |
453 | (addr_diff == 0 && | |
454 | ((__force u16)keys->ports.dst < | |
455 | (__force u16)keys->ports.src))) { | |
456 | swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst); | |
457 | swap(keys->ports.src, keys->ports.dst); | |
458 | } | |
459 | break; | |
460 | case FLOW_DISSECTOR_KEY_IPV6_ADDRS: | |
461 | addr_diff = memcmp(&keys->addrs.v6addrs.dst, | |
462 | &keys->addrs.v6addrs.src, | |
463 | sizeof(keys->addrs.v6addrs.dst)); | |
464 | if ((addr_diff < 0) || | |
465 | (addr_diff == 0 && | |
466 | ((__force u16)keys->ports.dst < | |
467 | (__force u16)keys->ports.src))) { | |
468 | for (i = 0; i < 4; i++) | |
469 | swap(keys->addrs.v6addrs.src.s6_addr32[i], | |
470 | keys->addrs.v6addrs.dst.s6_addr32[i]); | |
471 | swap(keys->ports.src, keys->ports.dst); | |
472 | } | |
473 | break; | |
474 | } | |
66415cf8 HFS |
475 | } |
476 | ||
50fb7992 | 477 | static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval) |
5ed20a68 TH |
478 | { |
479 | u32 hash; | |
480 | ||
c3f83241 | 481 | __flow_hash_consistentify(keys); |
5ed20a68 | 482 | |
42aecaa9 TH |
483 | hash = __flow_hash_words((u32 *)flow_keys_hash_start(keys), |
484 | flow_keys_hash_length(keys), keyval); | |
5ed20a68 TH |
485 | if (!hash) |
486 | hash = 1; | |
487 | ||
488 | return hash; | |
489 | } | |
490 | ||
491 | u32 flow_hash_from_keys(struct flow_keys *keys) | |
492 | { | |
50fb7992 TH |
493 | __flow_hash_secret_init(); |
494 | return __flow_hash_from_keys(keys, hashrnd); | |
5ed20a68 TH |
495 | } |
496 | EXPORT_SYMBOL(flow_hash_from_keys); | |
497 | ||
50fb7992 TH |
498 | static inline u32 ___skb_get_hash(const struct sk_buff *skb, |
499 | struct flow_keys *keys, u32 keyval) | |
500 | { | |
06635a35 | 501 | if (!skb_flow_dissect_flow_keys(skb, keys)) |
50fb7992 TH |
502 | return 0; |
503 | ||
504 | return __flow_hash_from_keys(keys, keyval); | |
505 | } | |
506 | ||
2f59e1eb TH |
507 | struct _flow_keys_digest_data { |
508 | __be16 n_proto; | |
509 | u8 ip_proto; | |
510 | u8 padding; | |
511 | __be32 ports; | |
512 | __be32 src; | |
513 | __be32 dst; | |
514 | }; | |
515 | ||
516 | void make_flow_keys_digest(struct flow_keys_digest *digest, | |
517 | const struct flow_keys *flow) | |
518 | { | |
519 | struct _flow_keys_digest_data *data = | |
520 | (struct _flow_keys_digest_data *)digest; | |
521 | ||
522 | BUILD_BUG_ON(sizeof(*data) > sizeof(*digest)); | |
523 | ||
524 | memset(digest, 0, sizeof(*digest)); | |
525 | ||
06635a35 JP |
526 | data->n_proto = flow->basic.n_proto; |
527 | data->ip_proto = flow->basic.ip_proto; | |
528 | data->ports = flow->ports.ports; | |
c3f83241 TH |
529 | data->src = flow->addrs.v4addrs.src; |
530 | data->dst = flow->addrs.v4addrs.dst; | |
2f59e1eb TH |
531 | } |
532 | EXPORT_SYMBOL(make_flow_keys_digest); | |
533 | ||
d4fd3275 JP |
534 | /** |
535 | * __skb_get_hash: calculate a flow hash | |
536 | * @skb: sk_buff to calculate flow hash from | |
537 | * | |
538 | * This function calculates a flow hash based on src/dst addresses | |
61b905da TH |
539 | * and src/dst port numbers. Sets hash in skb to non-zero hash value |
540 | * on success, zero indicates no valid hash. Also, sets l4_hash in skb | |
441d9d32 CW |
541 | * if hash is a canonical 4-tuple hash over transport ports. |
542 | */ | |
3958afa1 | 543 | void __skb_get_hash(struct sk_buff *skb) |
441d9d32 CW |
544 | { |
545 | struct flow_keys keys; | |
50fb7992 | 546 | u32 hash; |
441d9d32 | 547 | |
50fb7992 TH |
548 | __flow_hash_secret_init(); |
549 | ||
550 | hash = ___skb_get_hash(skb, &keys, hashrnd); | |
551 | if (!hash) | |
441d9d32 | 552 | return; |
06635a35 | 553 | if (keys.ports.ports) |
61b905da | 554 | skb->l4_hash = 1; |
a3b18ddb | 555 | skb->sw_hash = 1; |
50fb7992 | 556 | skb->hash = hash; |
441d9d32 | 557 | } |
3958afa1 | 558 | EXPORT_SYMBOL(__skb_get_hash); |
441d9d32 | 559 | |
50fb7992 TH |
560 | __u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb) |
561 | { | |
562 | struct flow_keys keys; | |
563 | ||
564 | return ___skb_get_hash(skb, &keys, perturb); | |
565 | } | |
566 | EXPORT_SYMBOL(skb_get_hash_perturb); | |
567 | ||
56193d1b AD |
568 | u32 __skb_get_poff(const struct sk_buff *skb, void *data, |
569 | const struct flow_keys *keys, int hlen) | |
f77668dc | 570 | { |
42aecaa9 | 571 | u32 poff = keys->control.thoff; |
f77668dc | 572 | |
06635a35 | 573 | switch (keys->basic.ip_proto) { |
f77668dc | 574 | case IPPROTO_TCP: { |
5af7fb6e AD |
575 | /* access doff as u8 to avoid unaligned access */ |
576 | const u8 *doff; | |
577 | u8 _doff; | |
f77668dc | 578 | |
5af7fb6e AD |
579 | doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff), |
580 | data, hlen, &_doff); | |
581 | if (!doff) | |
f77668dc DB |
582 | return poff; |
583 | ||
5af7fb6e | 584 | poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2); |
f77668dc DB |
585 | break; |
586 | } | |
587 | case IPPROTO_UDP: | |
588 | case IPPROTO_UDPLITE: | |
589 | poff += sizeof(struct udphdr); | |
590 | break; | |
591 | /* For the rest, we do not really care about header | |
592 | * extensions at this point for now. | |
593 | */ | |
594 | case IPPROTO_ICMP: | |
595 | poff += sizeof(struct icmphdr); | |
596 | break; | |
597 | case IPPROTO_ICMPV6: | |
598 | poff += sizeof(struct icmp6hdr); | |
599 | break; | |
600 | case IPPROTO_IGMP: | |
601 | poff += sizeof(struct igmphdr); | |
602 | break; | |
603 | case IPPROTO_DCCP: | |
604 | poff += sizeof(struct dccp_hdr); | |
605 | break; | |
606 | case IPPROTO_SCTP: | |
607 | poff += sizeof(struct sctphdr); | |
608 | break; | |
609 | } | |
610 | ||
611 | return poff; | |
612 | } | |
613 | ||
0db89b8b JP |
614 | /** |
615 | * skb_get_poff - get the offset to the payload | |
616 | * @skb: sk_buff to get the payload offset from | |
617 | * | |
618 | * The function will get the offset to the payload as far as it could | |
619 | * be dissected. The main user is currently BPF, so that we can dynamically | |
56193d1b AD |
620 | * truncate packets without needing to push actual payload to the user |
621 | * space and can analyze headers only, instead. | |
622 | */ | |
623 | u32 skb_get_poff(const struct sk_buff *skb) | |
624 | { | |
625 | struct flow_keys keys; | |
626 | ||
06635a35 | 627 | if (!skb_flow_dissect_flow_keys(skb, &keys)) |
56193d1b AD |
628 | return 0; |
629 | ||
630 | return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb)); | |
631 | } | |
06635a35 JP |
632 | |
633 | static const struct flow_dissector_key flow_keys_dissector_keys[] = { | |
42aecaa9 TH |
634 | { |
635 | .key_id = FLOW_DISSECTOR_KEY_CONTROL, | |
636 | .offset = offsetof(struct flow_keys, control), | |
637 | }, | |
06635a35 JP |
638 | { |
639 | .key_id = FLOW_DISSECTOR_KEY_BASIC, | |
640 | .offset = offsetof(struct flow_keys, basic), | |
641 | }, | |
642 | { | |
643 | .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS, | |
c3f83241 TH |
644 | .offset = offsetof(struct flow_keys, addrs.v4addrs), |
645 | }, | |
646 | { | |
647 | .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS, | |
648 | .offset = offsetof(struct flow_keys, addrs.v6addrs), | |
06635a35 | 649 | }, |
9f249089 TH |
650 | { |
651 | .key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS, | |
652 | .offset = offsetof(struct flow_keys, addrs.tipcaddrs), | |
653 | }, | |
06635a35 JP |
654 | { |
655 | .key_id = FLOW_DISSECTOR_KEY_PORTS, | |
656 | .offset = offsetof(struct flow_keys, ports), | |
657 | }, | |
d34af823 TH |
658 | { |
659 | .key_id = FLOW_DISSECTOR_KEY_VLANID, | |
660 | .offset = offsetof(struct flow_keys, tags), | |
661 | }, | |
06635a35 JP |
662 | }; |
663 | ||
664 | static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = { | |
42aecaa9 TH |
665 | { |
666 | .key_id = FLOW_DISSECTOR_KEY_CONTROL, | |
667 | .offset = offsetof(struct flow_keys, control), | |
668 | }, | |
06635a35 JP |
669 | { |
670 | .key_id = FLOW_DISSECTOR_KEY_BASIC, | |
671 | .offset = offsetof(struct flow_keys, basic), | |
672 | }, | |
673 | }; | |
674 | ||
675 | struct flow_dissector flow_keys_dissector __read_mostly; | |
676 | EXPORT_SYMBOL(flow_keys_dissector); | |
677 | ||
678 | struct flow_dissector flow_keys_buf_dissector __read_mostly; | |
679 | ||
680 | static int __init init_default_flow_dissectors(void) | |
681 | { | |
682 | skb_flow_dissector_init(&flow_keys_dissector, | |
683 | flow_keys_dissector_keys, | |
684 | ARRAY_SIZE(flow_keys_dissector_keys)); | |
685 | skb_flow_dissector_init(&flow_keys_buf_dissector, | |
686 | flow_keys_buf_dissector_keys, | |
687 | ARRAY_SIZE(flow_keys_buf_dissector_keys)); | |
688 | return 0; | |
689 | } | |
690 | ||
691 | late_initcall_sync(init_default_flow_dissectors); |