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