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