]> git.proxmox.com Git - ovs.git/blame - datapath/flow.c
compat: Fix IPv6 frag expiry crash.
[ovs.git] / datapath / flow.c
CommitLineData
064af421 1/*
e23775f2 2 * Copyright (c) 2007-2015 Nicira, Inc.
a14bc59f 3 *
a9a29d22
JG
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
064af421
BP
17 */
18
6455100f 19#include <linux/uaccess.h>
064af421
BP
20#include <linux/netdevice.h>
21#include <linux/etherdevice.h>
22#include <linux/if_ether.h>
23#include <linux/if_vlan.h>
24#include <net/llc_pdu.h>
25#include <linux/kernel.h>
8d5ebd83 26#include <linux/jhash.h>
064af421
BP
27#include <linux/jiffies.h>
28#include <linux/llc.h>
29#include <linux/module.h>
30#include <linux/in.h>
31#include <linux/rcupdate.h>
a26ef517 32#include <linux/if_arp.h>
064af421 33#include <linux/ip.h>
d31f1109 34#include <linux/ipv6.h>
2baf0e0c 35#include <linux/mpls.h>
10f72e3d 36#include <linux/sctp.h>
b0b906cc 37#include <linux/smp.h>
064af421
BP
38#include <linux/tcp.h>
39#include <linux/udp.h>
40#include <linux/icmp.h>
d31f1109 41#include <linux/icmpv6.h>
3544358a 42#include <linux/rculist.h>
064af421 43#include <net/ip.h>
d31f1109 44#include <net/ipv6.h>
2baf0e0c 45#include <net/mpls.h>
685a51a5 46#include <net/ndisc.h>
064af421 47
c135bba1 48#include "datapath.h"
a94ebc39 49#include "conntrack.h"
c135bba1
PS
50#include "flow.h"
51#include "flow_netlink.h"
e23775f2 52#include "vport.h"
6ce39213 53
a097c0b2 54u64 ovs_flow_used_time(unsigned long flow_jiffies)
a1c564be 55{
a097c0b2
PS
56 struct timespec cur_ts;
57 u64 cur_ms, idle_ms;
a1c564be 58
a097c0b2
PS
59 ktime_get_ts(&cur_ts);
60 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
61 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
62 cur_ts.tv_nsec / NSEC_PER_MSEC;
a1c564be 63
a097c0b2 64 return cur_ms - idle_ms;
c2dd5e99
AZ
65}
66
a66733a8 67#define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF))
a1c564be 68
a5b8d49b 69void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags,
f1f60b85 70 const struct sk_buff *skb)
a1c564be 71{
b0f3a2fe 72 struct flow_stats *stats;
9ac56358 73 int node = numa_node_id();
efd8a18e 74 int len = skb->len + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
a1c564be 75
9ac56358 76 stats = rcu_dereference(flow->stats[node]);
b0f3a2fe 77
9ac56358
JR
78 /* Check if already have node-specific stats. */
79 if (likely(stats)) {
80 spin_lock(&stats->lock);
81 /* Mark if we write on the pre-allocated stats. */
82 if (node == 0 && unlikely(flow->stats_last_writer != node))
83 flow->stats_last_writer = node;
84 } else {
85 stats = rcu_dereference(flow->stats[0]); /* Pre-allocated. */
86 spin_lock(&stats->lock);
87
88 /* If the current NUMA-node is the only writer on the
89 * pre-allocated stats keep using them.
90 */
91 if (unlikely(flow->stats_last_writer != node)) {
92 /* A previous locker may have already allocated the
93 * stats, so we need to check again. If node-specific
94 * stats were already allocated, we update the pre-
95 * allocated stats as we have already locked them.
96 */
97 if (likely(flow->stats_last_writer != NUMA_NO_NODE)
bc36fcd3 98 && likely(!rcu_access_pointer(flow->stats[node]))) {
9ac56358
JR
99 /* Try to allocate node-specific stats. */
100 struct flow_stats *new_stats;
101
102 new_stats =
103 kmem_cache_alloc_node(flow_stats_cache,
c0cddcec
JS
104 GFP_NOWAIT |
105 __GFP_THISNODE |
106 __GFP_NOWARN |
9ac56358
JR
107 __GFP_NOMEMALLOC,
108 node);
109 if (likely(new_stats)) {
110 new_stats->used = jiffies;
111 new_stats->packet_count = 1;
9e917416 112 new_stats->byte_count = len;
9ac56358
JR
113 new_stats->tcp_flags = tcp_flags;
114 spin_lock_init(&new_stats->lock);
115
116 rcu_assign_pointer(flow->stats[node],
117 new_stats);
118 goto unlock;
119 }
120 }
121 flow->stats_last_writer = node;
122 }
123 }
124
b0b906cc
PS
125 stats->used = jiffies;
126 stats->packet_count++;
9e917416 127 stats->byte_count += len;
b0b906cc 128 stats->tcp_flags |= tcp_flags;
9ac56358 129unlock:
b0b906cc
PS
130 spin_unlock(&stats->lock);
131}
132
4bb90bea
JR
133/* Must be called with rcu_read_lock or ovs_mutex. */
134void ovs_flow_stats_get(const struct sw_flow *flow,
135 struct ovs_flow_stats *ovs_stats,
b0f3a2fe
PS
136 unsigned long *used, __be16 *tcp_flags)
137{
9ac56358 138 int node;
b0b906cc 139
b0f3a2fe
PS
140 *used = 0;
141 *tcp_flags = 0;
142 memset(ovs_stats, 0, sizeof(*ovs_stats));
b0b906cc 143
9ac56358 144 for_each_node(node) {
4bb90bea 145 struct flow_stats *stats = rcu_dereference_ovsl(flow->stats[node]);
9ac56358
JR
146
147 if (stats) {
148 /* Local CPU may write on non-local stats, so we must
149 * block bottom-halves here.
150 */
151 spin_lock_bh(&stats->lock);
4029c043 152 if (!*used || time_after(stats->used, *used))
9ac56358
JR
153 *used = stats->used;
154 *tcp_flags |= stats->tcp_flags;
155 ovs_stats->n_packets += stats->packet_count;
156 ovs_stats->n_bytes += stats->byte_count;
157 spin_unlock_bh(&stats->lock);
158 }
b0b906cc 159 }
b0b906cc
PS
160}
161
4bb90bea 162/* Called with ovs_mutex. */
b0b906cc
PS
163void ovs_flow_stats_clear(struct sw_flow *flow)
164{
9ac56358
JR
165 int node;
166
167 for_each_node(node) {
4bb90bea 168 struct flow_stats *stats = ovsl_dereference(flow->stats[node]);
9ac56358
JR
169
170 if (stats) {
171 spin_lock_bh(&stats->lock);
172 stats->used = 0;
173 stats->packet_count = 0;
174 stats->byte_count = 0;
175 stats->tcp_flags = 0;
176 spin_unlock_bh(&stats->lock);
177 }
b0b906cc 178 }
a1c564be
AZ
179}
180
2db65bf7
JG
181static int check_header(struct sk_buff *skb, int len)
182{
183 if (unlikely(skb->len < len))
184 return -EINVAL;
185 if (unlikely(!pskb_may_pull(skb, len)))
186 return -ENOMEM;
187 return 0;
188}
189
6455100f 190static bool arphdr_ok(struct sk_buff *skb)
a26ef517 191{
2db65bf7
JG
192 return pskb_may_pull(skb, skb_network_offset(skb) +
193 sizeof(struct arp_eth_header));
a26ef517
JP
194}
195
6455100f 196static int check_iphdr(struct sk_buff *skb)
064af421 197{
4c1ad233
BP
198 unsigned int nh_ofs = skb_network_offset(skb);
199 unsigned int ip_len;
2db65bf7 200 int err;
4c1ad233 201
2db65bf7
JG
202 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
203 if (unlikely(err))
204 return err;
4c1ad233
BP
205
206 ip_len = ip_hdrlen(skb);
2db65bf7
JG
207 if (unlikely(ip_len < sizeof(struct iphdr) ||
208 skb->len < nh_ofs + ip_len))
4c1ad233
BP
209 return -EINVAL;
210
4c1ad233
BP
211 skb_set_transport_header(skb, nh_ofs + ip_len);
212 return 0;
064af421
BP
213}
214
6455100f 215static bool tcphdr_ok(struct sk_buff *skb)
064af421
BP
216{
217 int th_ofs = skb_transport_offset(skb);
2db65bf7
JG
218 int tcp_len;
219
220 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
221 return false;
222
223 tcp_len = tcp_hdrlen(skb);
224 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
225 skb->len < th_ofs + tcp_len))
226 return false;
227
228 return true;
064af421
BP
229}
230
6455100f 231static bool udphdr_ok(struct sk_buff *skb)
064af421 232{
2db65bf7
JG
233 return pskb_may_pull(skb, skb_transport_offset(skb) +
234 sizeof(struct udphdr));
064af421
BP
235}
236
10f72e3d
JS
237static bool sctphdr_ok(struct sk_buff *skb)
238{
239 return pskb_may_pull(skb, skb_transport_offset(skb) +
240 sizeof(struct sctphdr));
241}
242
6455100f 243static bool icmphdr_ok(struct sk_buff *skb)
064af421 244{
2db65bf7
JG
245 return pskb_may_pull(skb, skb_transport_offset(skb) +
246 sizeof(struct icmphdr));
064af421
BP
247}
248
a1c564be 249static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
d31f1109
JP
250{
251 unsigned int nh_ofs = skb_network_offset(skb);
252 unsigned int nh_len;
253 int payload_ofs;
d31f1109
JP
254 struct ipv6hdr *nh;
255 uint8_t nexthdr;
0fd0d083 256 __be16 frag_off;
2db65bf7 257 int err;
d31f1109 258
2db65bf7
JG
259 err = check_header(skb, nh_ofs + sizeof(*nh));
260 if (unlikely(err))
261 return err;
d31f1109
JP
262
263 nh = ipv6_hdr(skb);
264 nexthdr = nh->nexthdr;
265 payload_ofs = (u8 *)(nh + 1) - skb->data;
d31f1109 266
28bad473 267 key->ip.proto = NEXTHDR_NONE;
530180fd 268 key->ip.tos = ipv6_get_dsfield(nh);
a61680c6 269 key->ip.ttl = nh->hop_limit;
fa8223b7 270 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
858d1026
JG
271 key->ipv6.addr.src = nh->saddr;
272 key->ipv6.addr.dst = nh->daddr;
d31f1109 273
0fd0d083 274 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
bfef4717 275
0fd0d083
JG
276 if (frag_off) {
277 if (frag_off & htons(~0x7))
278 key->ip.frag = OVS_FRAG_TYPE_LATER;
279 else
280 key->ip.frag = OVS_FRAG_TYPE_FIRST;
9cef26ac
JG
281 } else {
282 key->ip.frag = OVS_FRAG_TYPE_NONE;
0fd0d083
JG
283 }
284
b17b330b
PS
285 /* Delayed handling of error in ipv6_skip_exthdr() as it
286 * always sets frag_off to a valid value which may be
287 * used to set key->ip.frag above.
288 */
289 if (unlikely(payload_ofs < 0))
290 return -EPROTO;
291
d31f1109 292 nh_len = payload_ofs - nh_ofs;
d31f1109 293 skb_set_transport_header(skb, nh_ofs + nh_len);
28bad473 294 key->ip.proto = nexthdr;
d31f1109
JP
295 return nh_len;
296}
297
298static bool icmp6hdr_ok(struct sk_buff *skb)
299{
2db65bf7
JG
300 return pskb_may_pull(skb, skb_transport_offset(skb) +
301 sizeof(struct icmp6hdr));
d31f1109 302}
ec58547a 303
2db65bf7 304static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
064af421 305{
50f06e16
BP
306 struct qtag_prefix {
307 __be16 eth_type; /* ETH_P_8021Q */
308 __be16 tci;
309 };
310 struct qtag_prefix *qp;
311
8ddc056d
BP
312 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
313 return 0;
314
2db65bf7
JG
315 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
316 sizeof(__be16))))
317 return -ENOMEM;
50f06e16
BP
318
319 qp = (struct qtag_prefix *) skb->data;
76abe283 320 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
50f06e16 321 __skb_pull(skb, sizeof(struct qtag_prefix));
2db65bf7
JG
322
323 return 0;
50f06e16
BP
324}
325
326static __be16 parse_ethertype(struct sk_buff *skb)
064af421 327{
50f06e16
BP
328 struct llc_snap_hdr {
329 u8 dsap; /* Always 0xAA */
330 u8 ssap; /* Always 0xAA */
331 u8 ctrl;
332 u8 oui[3];
8dda8c9b 333 __be16 ethertype;
50f06e16
BP
334 };
335 struct llc_snap_hdr *llc;
336 __be16 proto;
337
338 proto = *(__be16 *) skb->data;
339 __skb_pull(skb, sizeof(__be16));
340
935fc582 341 if (eth_proto_is_802_3(proto))
50f06e16
BP
342 return proto;
343
2db65bf7 344 if (skb->len < sizeof(struct llc_snap_hdr))
36956a7d 345 return htons(ETH_P_802_2);
50f06e16 346
2db65bf7
JG
347 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
348 return htons(0);
349
50f06e16
BP
350 llc = (struct llc_snap_hdr *) skb->data;
351 if (llc->dsap != LLC_SAP_SNAP ||
352 llc->ssap != LLC_SAP_SNAP ||
353 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
36956a7d 354 return htons(ETH_P_802_2);
50f06e16
BP
355
356 __skb_pull(skb, sizeof(struct llc_snap_hdr));
9e69bc5f 357
935fc582 358 if (eth_proto_is_802_3(llc->ethertype))
9e69bc5f
RL
359 return llc->ethertype;
360
361 return htons(ETH_P_802_2);
064af421
BP
362}
363
685a51a5 364static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
a1c564be 365 int nh_len)
685a51a5 366{
685a51a5
JP
367 struct icmp6hdr *icmp = icmp6_hdr(skb);
368
369 /* The ICMPv6 type and code fields use the 16-bit transport port
bfef4717
JG
370 * fields, so we need to store them in 16-bit network byte order.
371 */
708fb4c5
JR
372 key->tp.src = htons(icmp->icmp6_type);
373 key->tp.dst = htons(icmp->icmp6_code);
9cef26ac 374 memset(&key->ipv6.nd, 0, sizeof(key->ipv6.nd));
685a51a5 375
bfef4717
JG
376 if (icmp->icmp6_code == 0 &&
377 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
378 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
e977ba19 379 int icmp_len = skb->len - skb_transport_offset(skb);
685a51a5
JP
380 struct nd_msg *nd;
381 int offset;
382
383 /* In order to process neighbor discovery options, we need the
bfef4717
JG
384 * entire packet.
385 */
386 if (unlikely(icmp_len < sizeof(*nd)))
a1c564be
AZ
387 return 0;
388
389 if (unlikely(skb_linearize(skb)))
390 return -ENOMEM;
685a51a5
JP
391
392 nd = (struct nd_msg *)skb_transport_header(skb);
858d1026 393 key->ipv6.nd.target = nd->target;
685a51a5
JP
394
395 icmp_len -= sizeof(*nd);
396 offset = 0;
397 while (icmp_len >= 8) {
6455100f
PS
398 struct nd_opt_hdr *nd_opt =
399 (struct nd_opt_hdr *)(nd->opt + offset);
685a51a5
JP
400 int opt_len = nd_opt->nd_opt_len * 8;
401
bfef4717 402 if (unlikely(!opt_len || opt_len > icmp_len))
a1c564be 403 return 0;
685a51a5 404
bfef4717
JG
405 /* Store the link layer address if the appropriate
406 * option is provided. It is considered an error if
407 * the same link layer option is specified twice.
408 */
685a51a5 409 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
bfef4717 410 && opt_len == 8) {
76abe283 411 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
685a51a5 412 goto invalid;
982a47ec 413 ether_addr_copy(key->ipv6.nd.sll,
7d16c847 414 &nd->opt[offset+sizeof(*nd_opt)]);
685a51a5 415 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
bfef4717 416 && opt_len == 8) {
76abe283 417 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
685a51a5 418 goto invalid;
982a47ec 419 ether_addr_copy(key->ipv6.nd.tll,
7d16c847 420 &nd->opt[offset+sizeof(*nd_opt)]);
685a51a5
JP
421 }
422
423 icmp_len -= opt_len;
424 offset += opt_len;
425 }
426 }
427
a1c564be 428 return 0;
685a51a5
JP
429
430invalid:
76abe283
AE
431 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
432 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
433 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
685a51a5 434
a1c564be 435 return 0;
685a51a5
JP
436}
437
a31e0e31 438/**
c135bba1 439 * key_extract - extracts a flow key from an Ethernet frame.
a31e0e31
BP
440 * @skb: sk_buff that contains the frame, with skb->data pointing to the
441 * Ethernet header
a31e0e31
BP
442 * @key: output flow key
443 *
444 * The caller must ensure that skb->len >= ETH_HLEN.
445 *
4c1ad233
BP
446 * Returns 0 if successful, otherwise a negative errno value.
447 *
59a18f80
BP
448 * Initializes @skb header pointers as follows:
449 *
450 * - skb->mac_header: the Ethernet header.
451 *
452 * - skb->network_header: just past the Ethernet header, or just past the
453 * VLAN header, to the first byte of the Ethernet payload.
454 *
ae9020cf 455 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
d31f1109
JP
456 * on output, then just past the IP header, if one is present and
457 * of a correct length, otherwise the same as skb->network_header.
ae9020cf 458 * For other key->eth.type values it is left untouched.
a31e0e31 459 */
c135bba1 460static int key_extract(struct sk_buff *skb, struct sw_flow_key *key)
064af421 461{
a1c564be 462 int error;
064af421 463 struct ethhdr *eth;
064af421 464
7d16c847 465 /* Flags are always used as part of stats */
9cef26ac 466 key->tp.flags = 0;
064af421 467
064af421 468 skb_reset_mac_header(skb);
064af421 469
2db65bf7
JG
470 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
471 * header in the linear data area.
472 */
50f06e16 473 eth = eth_hdr(skb);
982a47ec
JP
474 ether_addr_copy(key->eth.src, eth->h_source);
475 ether_addr_copy(key->eth.dst, eth->h_dest);
76abe283 476
50f06e16 477 __skb_pull(skb, 2 * ETH_ALEN);
3cfede14 478 /* We are going to push all headers that we pull, so no need to
af465b67
PS
479 * update skb->csum here.
480 */
6ce39213 481
9cef26ac 482 key->eth.tci = 0;
efd8a18e 483 if (skb_vlan_tag_present(skb))
8063e095 484 key->eth.tci = htons(skb->vlan_tci);
6ce39213 485 else if (eth->h_proto == htons(ETH_P_8021Q))
2db65bf7
JG
486 if (unlikely(parse_vlan(skb, key)))
487 return -ENOMEM;
6ce39213 488
76abe283
AE
489 key->eth.type = parse_ethertype(skb);
490 if (unlikely(key->eth.type == htons(0)))
2db65bf7
JG
491 return -ENOMEM;
492
50f06e16 493 skb_reset_network_header(skb);
ccf43786 494 skb_reset_mac_len(skb);
2db65bf7 495 __skb_push(skb, skb->data - skb_mac_header(skb));
064af421
BP
496
497 /* Network layer. */
76abe283 498 if (key->eth.type == htons(ETH_P_IP)) {
4c1ad233 499 struct iphdr *nh;
7257b535 500 __be16 offset;
76abe283 501
4c1ad233
BP
502 error = check_iphdr(skb);
503 if (unlikely(error)) {
9cef26ac
JG
504 memset(&key->ip, 0, sizeof(key->ip));
505 memset(&key->ipv4, 0, sizeof(key->ipv4));
4c1ad233
BP
506 if (error == -EINVAL) {
507 skb->transport_header = skb->network_header;
76abe283 508 error = 0;
4c1ad233 509 }
a1c564be 510 return error;
4c1ad233
BP
511 }
512
513 nh = ip_hdr(skb);
76abe283
AE
514 key->ipv4.addr.src = nh->saddr;
515 key->ipv4.addr.dst = nh->daddr;
7257b535 516
28bad473 517 key->ip.proto = nh->protocol;
530180fd 518 key->ip.tos = nh->tos;
a61680c6 519 key->ip.ttl = nh->ttl;
064af421 520
7257b535
BP
521 offset = nh->frag_off & htons(IP_OFFSET);
522 if (offset) {
9e44d715 523 key->ip.frag = OVS_FRAG_TYPE_LATER;
a1c564be 524 return 0;
7257b535
BP
525 }
526 if (nh->frag_off & htons(IP_MF) ||
7d16c847 527 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
9e44d715 528 key->ip.frag = OVS_FRAG_TYPE_FIRST;
9cef26ac
JG
529 else
530 key->ip.frag = OVS_FRAG_TYPE_NONE;
b7a31ec1 531
7257b535 532 /* Transport layer. */
28bad473 533 if (key->ip.proto == IPPROTO_TCP) {
7257b535 534 if (tcphdr_ok(skb)) {
5f4d087c 535 struct tcphdr *tcp = tcp_hdr(skb);
708fb4c5
JR
536 key->tp.src = tcp->source;
537 key->tp.dst = tcp->dest;
538 key->tp.flags = TCP_FLAGS_BE16(tcp);
9cef26ac
JG
539 } else {
540 memset(&key->tp, 0, sizeof(key->tp));
5f4d087c 541 }
7d16c847 542
28bad473 543 } else if (key->ip.proto == IPPROTO_UDP) {
7257b535 544 if (udphdr_ok(skb)) {
5f4d087c 545 struct udphdr *udp = udp_hdr(skb);
708fb4c5
JR
546 key->tp.src = udp->source;
547 key->tp.dst = udp->dest;
9cef26ac
JG
548 } else {
549 memset(&key->tp, 0, sizeof(key->tp));
5f4d087c 550 }
10f72e3d
JS
551 } else if (key->ip.proto == IPPROTO_SCTP) {
552 if (sctphdr_ok(skb)) {
553 struct sctphdr *sctp = sctp_hdr(skb);
708fb4c5
JR
554 key->tp.src = sctp->source;
555 key->tp.dst = sctp->dest;
9cef26ac
JG
556 } else {
557 memset(&key->tp, 0, sizeof(key->tp));
10f72e3d 558 }
28bad473 559 } else if (key->ip.proto == IPPROTO_ICMP) {
7257b535 560 if (icmphdr_ok(skb)) {
5f4d087c
JG
561 struct icmphdr *icmp = icmp_hdr(skb);
562 /* The ICMP type and code fields use the 16-bit
6455100f 563 * transport port fields, so we need to store
af465b67
PS
564 * them in 16-bit network byte order.
565 */
708fb4c5
JR
566 key->tp.src = htons(icmp->type);
567 key->tp.dst = htons(icmp->code);
9cef26ac
JG
568 } else {
569 memset(&key->tp, 0, sizeof(key->tp));
5f4d087c
JG
570 }
571 }
572
9cef26ac
JG
573 } else if (key->eth.type == htons(ETH_P_ARP) ||
574 key->eth.type == htons(ETH_P_RARP)) {
a26ef517 575 struct arp_eth_header *arp;
10173cea 576 bool arp_available = arphdr_ok(skb);
a26ef517
JP
577
578 arp = (struct arp_eth_header *)skb_network_header(skb);
579
10173cea
LR
580 if (arp_available &&
581 arp->ar_hrd == htons(ARPHRD_ETHER) &&
582 arp->ar_pro == htons(ETH_P_IP) &&
583 arp->ar_hln == ETH_ALEN &&
584 arp->ar_pln == 4) {
de3f65ea
JP
585
586 /* We only match on the lower 8 bits of the opcode. */
b7a31ec1 587 if (ntohs(arp->ar_op) <= 0xff)
28bad473 588 key->ip.proto = ntohs(arp->ar_op);
9cef26ac
JG
589 else
590 key->ip.proto = 0;
591
a3d3ad0c
MM
592 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
593 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
982a47ec
JP
594 ether_addr_copy(key->ipv4.arp.sha, arp->ar_sha);
595 ether_addr_copy(key->ipv4.arp.tha, arp->ar_tha);
9cef26ac
JG
596 } else {
597 memset(&key->ip, 0, sizeof(key->ip));
598 memset(&key->ipv4, 0, sizeof(key->ipv4));
de3f65ea 599 }
ccf43786
SH
600 } else if (eth_p_mpls(key->eth.type)) {
601 size_t stack_len = MPLS_HLEN;
602
603 /* In the presence of an MPLS label stack the end of the L2
604 * header and the beginning of the L3 header differ.
605 *
606 * Advance network_header to the beginning of the L3
607 * header. mac_len corresponds to the end of the L2 header.
608 */
609 while (1) {
610 __be32 lse;
611
612 error = check_header(skb, skb->mac_len + stack_len);
613 if (unlikely(error))
614 return 0;
615
616 memcpy(&lse, skb_network_header(skb), MPLS_HLEN);
617
618 if (stack_len == MPLS_HLEN)
619 memcpy(&key->mpls.top_lse, &lse, MPLS_HLEN);
620
621 skb_set_network_header(skb, skb->mac_len + stack_len);
2baf0e0c 622 if (lse & htonl(MPLS_LS_S_MASK))
ccf43786
SH
623 break;
624
625 stack_len += MPLS_HLEN;
626 }
76abe283 627 } else if (key->eth.type == htons(ETH_P_IPV6)) {
d31f1109
JP
628 int nh_len; /* IPv6 Header + Extensions */
629
a1c564be 630 nh_len = parse_ipv6hdr(skb, key);
d31f1109 631 if (unlikely(nh_len < 0)) {
b17b330b
PS
632 switch (nh_len) {
633 case -EINVAL:
634 memset(&key->ip, 0, sizeof(key->ip));
635 memset(&key->ipv6.addr, 0, sizeof(key->ipv6.addr));
636 /* fall-through */
637 case -EPROTO:
d31f1109 638 skb->transport_header = skb->network_header;
a1c564be 639 error = 0;
b17b330b
PS
640 break;
641 default:
76abe283 642 error = nh_len;
a1c564be
AZ
643 }
644 return error;
d31f1109
JP
645 }
646
9e44d715 647 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
a1c564be 648 return 0;
7257b535 649 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
9e44d715 650 key->ip.frag = OVS_FRAG_TYPE_FIRST;
7257b535 651
d31f1109 652 /* Transport layer. */
28bad473 653 if (key->ip.proto == NEXTHDR_TCP) {
d31f1109
JP
654 if (tcphdr_ok(skb)) {
655 struct tcphdr *tcp = tcp_hdr(skb);
708fb4c5
JR
656 key->tp.src = tcp->source;
657 key->tp.dst = tcp->dest;
658 key->tp.flags = TCP_FLAGS_BE16(tcp);
9cef26ac
JG
659 } else {
660 memset(&key->tp, 0, sizeof(key->tp));
d31f1109 661 }
28bad473 662 } else if (key->ip.proto == NEXTHDR_UDP) {
d31f1109
JP
663 if (udphdr_ok(skb)) {
664 struct udphdr *udp = udp_hdr(skb);
708fb4c5
JR
665 key->tp.src = udp->source;
666 key->tp.dst = udp->dest;
9cef26ac
JG
667 } else {
668 memset(&key->tp, 0, sizeof(key->tp));
d31f1109 669 }
10f72e3d
JS
670 } else if (key->ip.proto == NEXTHDR_SCTP) {
671 if (sctphdr_ok(skb)) {
672 struct sctphdr *sctp = sctp_hdr(skb);
708fb4c5
JR
673 key->tp.src = sctp->source;
674 key->tp.dst = sctp->dest;
9cef26ac
JG
675 } else {
676 memset(&key->tp, 0, sizeof(key->tp));
10f72e3d 677 }
28bad473 678 } else if (key->ip.proto == NEXTHDR_ICMP) {
d31f1109 679 if (icmp6hdr_ok(skb)) {
a1c564be
AZ
680 error = parse_icmpv6(skb, key, nh_len);
681 if (error)
682 return error;
9cef26ac
JG
683 } else {
684 memset(&key->tp, 0, sizeof(key->tp));
d31f1109
JP
685 }
686 }
064af421 687 }
a1c564be 688 return 0;
064af421 689}
c135bba1 690
e0a42ae2
AZ
691int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key)
692{
693 return key_extract(skb, key);
694}
695
e23775f2 696int ovs_flow_key_extract(const struct ip_tunnel_info *tun_info,
7d16c847 697 struct sk_buff *skb, struct sw_flow_key *key)
c135bba1
PS
698{
699 /* Extract metadata from packet. */
fb66fbd1 700 if (tun_info) {
8a2d4905 701 key->tun_proto = ip_tunnel_info_af(tun_info);
e23775f2 702 memcpy(&key->tun_key, &tun_info->key, sizeof(key->tun_key));
c135bba1
PS
703 BUILD_BUG_ON(((1 << (sizeof(tun_info->options_len) * 8)) - 1) >
704 sizeof(key->tun_opts));
705
e23775f2
PS
706 if (tun_info->options_len) {
707 ip_tunnel_info_opts_get(TUN_METADATA_OPTS(key, tun_info->options_len),
708 tun_info);
c135bba1
PS
709 key->tun_opts_len = tun_info->options_len;
710 } else {
711 key->tun_opts_len = 0;
712 }
8a2d4905
PS
713 } else {
714 key->tun_proto = 0;
c135bba1
PS
715 key->tun_opts_len = 0;
716 memset(&key->tun_key, 0, sizeof(key->tun_key));
717 }
718
719 key->phy.priority = skb->priority;
720 key->phy.in_port = OVS_CB(skb)->input_vport->port_no;
721 key->phy.skb_mark = skb->mark;
a94ebc39 722 ovs_ct_fill_key(skb, key);
c135bba1
PS
723 key->ovs_flow_hash = 0;
724 key->recirc_id = 0;
725
726 return key_extract(skb, key);
727}
728
038e34ab 729int ovs_flow_key_extract_userspace(struct net *net, const struct nlattr *attr,
c135bba1 730 struct sk_buff *skb,
9233cef7 731 struct sw_flow_key *key, bool log)
c135bba1
PS
732{
733 int err;
734
2250cb8b
PS
735 memset(key, 0, OVS_SW_FLOW_KEY_METADATA_SIZE);
736
c135bba1 737 /* Extract metadata from netlink attributes. */
038e34ab 738 err = ovs_nla_get_flow_metadata(net, attr, key, log);
c135bba1
PS
739 if (err)
740 return err;
741
742 return key_extract(skb, key);
743}