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