xref: /openbmc/linux/net/openvswitch/flow.c (revision 1f9f6a78)
1 /*
2  * Copyright (c) 2007-2014 Nicira, Inc.
3  *
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
17  */
18 
19 #include <linux/uaccess.h>
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>
26 #include <linux/jhash.h>
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>
32 #include <linux/if_arp.h>
33 #include <linux/ip.h>
34 #include <linux/ipv6.h>
35 #include <linux/mpls.h>
36 #include <linux/sctp.h>
37 #include <linux/smp.h>
38 #include <linux/tcp.h>
39 #include <linux/udp.h>
40 #include <linux/icmp.h>
41 #include <linux/icmpv6.h>
42 #include <linux/rculist.h>
43 #include <net/ip.h>
44 #include <net/ip_tunnels.h>
45 #include <net/ipv6.h>
46 #include <net/mpls.h>
47 #include <net/ndisc.h>
48 
49 #include "datapath.h"
50 #include "flow.h"
51 #include "flow_netlink.h"
52 
53 u64 ovs_flow_used_time(unsigned long flow_jiffies)
54 {
55 	struct timespec cur_ts;
56 	u64 cur_ms, idle_ms;
57 
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;
62 
63 	return cur_ms - idle_ms;
64 }
65 
66 #define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF))
67 
68 void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags,
69 			   const struct sk_buff *skb)
70 {
71 	struct flow_stats *stats;
72 	int node = numa_node_id();
73 
74 	stats = rcu_dereference(flow->stats[node]);
75 
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)
96 			    && likely(!rcu_access_pointer(flow->stats[node]))) {
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 
121 	stats->used = jiffies;
122 	stats->packet_count++;
123 	stats->byte_count += skb->len;
124 	stats->tcp_flags |= tcp_flags;
125 unlock:
126 	spin_unlock(&stats->lock);
127 }
128 
129 /* Must be called with rcu_read_lock or ovs_mutex. */
130 void ovs_flow_stats_get(const struct sw_flow *flow,
131 			struct ovs_flow_stats *ovs_stats,
132 			unsigned long *used, __be16 *tcp_flags)
133 {
134 	int node;
135 
136 	*used = 0;
137 	*tcp_flags = 0;
138 	memset(ovs_stats, 0, sizeof(*ovs_stats));
139 
140 	for_each_node(node) {
141 		struct flow_stats *stats = rcu_dereference_ovsl(flow->stats[node]);
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);
148 			if (!*used || time_after(stats->used, *used))
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 		}
155 	}
156 }
157 
158 /* Called with ovs_mutex. */
159 void ovs_flow_stats_clear(struct sw_flow *flow)
160 {
161 	int node;
162 
163 	for_each_node(node) {
164 		struct flow_stats *stats = ovsl_dereference(flow->stats[node]);
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 		}
174 	}
175 }
176 
177 static 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 
186 static bool arphdr_ok(struct sk_buff *skb)
187 {
188 	return pskb_may_pull(skb, skb_network_offset(skb) +
189 				  sizeof(struct arp_eth_header));
190 }
191 
192 static int check_iphdr(struct sk_buff *skb)
193 {
194 	unsigned int nh_ofs = skb_network_offset(skb);
195 	unsigned int ip_len;
196 	int err;
197 
198 	err = check_header(skb, nh_ofs + sizeof(struct iphdr));
199 	if (unlikely(err))
200 		return err;
201 
202 	ip_len = ip_hdrlen(skb);
203 	if (unlikely(ip_len < sizeof(struct iphdr) ||
204 		     skb->len < nh_ofs + ip_len))
205 		return -EINVAL;
206 
207 	skb_set_transport_header(skb, nh_ofs + ip_len);
208 	return 0;
209 }
210 
211 static bool tcphdr_ok(struct sk_buff *skb)
212 {
213 	int th_ofs = skb_transport_offset(skb);
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;
225 }
226 
227 static bool udphdr_ok(struct sk_buff *skb)
228 {
229 	return pskb_may_pull(skb, skb_transport_offset(skb) +
230 				  sizeof(struct udphdr));
231 }
232 
233 static bool sctphdr_ok(struct sk_buff *skb)
234 {
235 	return pskb_may_pull(skb, skb_transport_offset(skb) +
236 				  sizeof(struct sctphdr));
237 }
238 
239 static bool icmphdr_ok(struct sk_buff *skb)
240 {
241 	return pskb_may_pull(skb, skb_transport_offset(skb) +
242 				  sizeof(struct icmphdr));
243 }
244 
245 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
246 {
247 	unsigned int nh_ofs = skb_network_offset(skb);
248 	unsigned int nh_len;
249 	int payload_ofs;
250 	struct ipv6hdr *nh;
251 	uint8_t nexthdr;
252 	__be16 frag_off;
253 	int err;
254 
255 	err = check_header(skb, nh_ofs + sizeof(*nh));
256 	if (unlikely(err))
257 		return err;
258 
259 	nh = ipv6_hdr(skb);
260 	nexthdr = nh->nexthdr;
261 	payload_ofs = (u8 *)(nh + 1) - skb->data;
262 
263 	key->ip.proto = NEXTHDR_NONE;
264 	key->ip.tos = ipv6_get_dsfield(nh);
265 	key->ip.ttl = nh->hop_limit;
266 	key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
267 	key->ipv6.addr.src = nh->saddr;
268 	key->ipv6.addr.dst = nh->daddr;
269 
270 	payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
271 	if (unlikely(payload_ofs < 0))
272 		return -EINVAL;
273 
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;
279 	} else {
280 		key->ip.frag = OVS_FRAG_TYPE_NONE;
281 	}
282 
283 	nh_len = payload_ofs - nh_ofs;
284 	skb_set_transport_header(skb, nh_ofs + nh_len);
285 	key->ip.proto = nexthdr;
286 	return nh_len;
287 }
288 
289 static bool icmp6hdr_ok(struct sk_buff *skb)
290 {
291 	return pskb_may_pull(skb, skb_transport_offset(skb) +
292 				  sizeof(struct icmp6hdr));
293 }
294 
295 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
296 {
297 	struct qtag_prefix {
298 		__be16 eth_type; /* ETH_P_8021Q */
299 		__be16 tci;
300 	};
301 	struct qtag_prefix *qp;
302 
303 	if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
304 		return 0;
305 
306 	if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
307 					 sizeof(__be16))))
308 		return -ENOMEM;
309 
310 	qp = (struct qtag_prefix *) skb->data;
311 	key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
312 	__skb_pull(skb, sizeof(struct qtag_prefix));
313 
314 	return 0;
315 }
316 
317 static __be16 parse_ethertype(struct sk_buff *skb)
318 {
319 	struct llc_snap_hdr {
320 		u8  dsap;  /* Always 0xAA */
321 		u8  ssap;  /* Always 0xAA */
322 		u8  ctrl;
323 		u8  oui[3];
324 		__be16 ethertype;
325 	};
326 	struct llc_snap_hdr *llc;
327 	__be16 proto;
328 
329 	proto = *(__be16 *) skb->data;
330 	__skb_pull(skb, sizeof(__be16));
331 
332 	if (ntohs(proto) >= ETH_P_802_3_MIN)
333 		return proto;
334 
335 	if (skb->len < sizeof(struct llc_snap_hdr))
336 		return htons(ETH_P_802_2);
337 
338 	if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
339 		return htons(0);
340 
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)
345 		return htons(ETH_P_802_2);
346 
347 	__skb_pull(skb, sizeof(struct llc_snap_hdr));
348 
349 	if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
350 		return llc->ethertype;
351 
352 	return htons(ETH_P_802_2);
353 }
354 
355 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
356 			int nh_len)
357 {
358 	struct icmp6hdr *icmp = icmp6_hdr(skb);
359 
360 	/* The ICMPv6 type and code fields use the 16-bit transport port
361 	 * fields, so we need to store them in 16-bit network byte order.
362 	 */
363 	key->tp.src = htons(icmp->icmp6_type);
364 	key->tp.dst = htons(icmp->icmp6_code);
365 	memset(&key->ipv6.nd, 0, sizeof(key->ipv6.nd));
366 
367 	if (icmp->icmp6_code == 0 &&
368 	    (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
369 	     icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
370 		int icmp_len = skb->len - skb_transport_offset(skb);
371 		struct nd_msg *nd;
372 		int offset;
373 
374 		/* In order to process neighbor discovery options, we need the
375 		 * entire packet.
376 		 */
377 		if (unlikely(icmp_len < sizeof(*nd)))
378 			return 0;
379 
380 		if (unlikely(skb_linearize(skb)))
381 			return -ENOMEM;
382 
383 		nd = (struct nd_msg *)skb_transport_header(skb);
384 		key->ipv6.nd.target = nd->target;
385 
386 		icmp_len -= sizeof(*nd);
387 		offset = 0;
388 		while (icmp_len >= 8) {
389 			struct nd_opt_hdr *nd_opt =
390 				 (struct nd_opt_hdr *)(nd->opt + offset);
391 			int opt_len = nd_opt->nd_opt_len * 8;
392 
393 			if (unlikely(!opt_len || opt_len > icmp_len))
394 				return 0;
395 
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 			 */
400 			if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
401 			    && opt_len == 8) {
402 				if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
403 					goto invalid;
404 				ether_addr_copy(key->ipv6.nd.sll,
405 						&nd->opt[offset+sizeof(*nd_opt)]);
406 			} else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
407 				   && opt_len == 8) {
408 				if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
409 					goto invalid;
410 				ether_addr_copy(key->ipv6.nd.tll,
411 						&nd->opt[offset+sizeof(*nd_opt)]);
412 			}
413 
414 			icmp_len -= opt_len;
415 			offset += opt_len;
416 		}
417 	}
418 
419 	return 0;
420 
421 invalid:
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));
425 
426 	return 0;
427 }
428 
429 /**
430  * key_extract - extracts a flow key from an Ethernet frame.
431  * @skb: sk_buff that contains the frame, with skb->data pointing to the
432  * Ethernet header
433  * @key: output flow key
434  *
435  * The caller must ensure that skb->len >= ETH_HLEN.
436  *
437  * Returns 0 if successful, otherwise a negative errno value.
438  *
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  *
446  *    - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
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.
449  *      For other key->eth.type values it is left untouched.
450  */
451 static int key_extract(struct sk_buff *skb, struct sw_flow_key *key)
452 {
453 	int error;
454 	struct ethhdr *eth;
455 
456 	/* Flags are always used as part of stats */
457 	key->tp.flags = 0;
458 
459 	skb_reset_mac_header(skb);
460 
461 	/* Link layer.  We are guaranteed to have at least the 14 byte Ethernet
462 	 * header in the linear data area.
463 	 */
464 	eth = eth_hdr(skb);
465 	ether_addr_copy(key->eth.src, eth->h_source);
466 	ether_addr_copy(key->eth.dst, eth->h_dest);
467 
468 	__skb_pull(skb, 2 * ETH_ALEN);
469 	/* We are going to push all headers that we pull, so no need to
470 	 * update skb->csum here.
471 	 */
472 
473 	key->eth.tci = 0;
474 	if (vlan_tx_tag_present(skb))
475 		key->eth.tci = htons(skb->vlan_tci);
476 	else if (eth->h_proto == htons(ETH_P_8021Q))
477 		if (unlikely(parse_vlan(skb, key)))
478 			return -ENOMEM;
479 
480 	key->eth.type = parse_ethertype(skb);
481 	if (unlikely(key->eth.type == htons(0)))
482 		return -ENOMEM;
483 
484 	skb_reset_network_header(skb);
485 	skb_reset_mac_len(skb);
486 	__skb_push(skb, skb->data - skb_mac_header(skb));
487 
488 	/* Network layer. */
489 	if (key->eth.type == htons(ETH_P_IP)) {
490 		struct iphdr *nh;
491 		__be16 offset;
492 
493 		error = check_iphdr(skb);
494 		if (unlikely(error)) {
495 			memset(&key->ip, 0, sizeof(key->ip));
496 			memset(&key->ipv4, 0, sizeof(key->ipv4));
497 			if (error == -EINVAL) {
498 				skb->transport_header = skb->network_header;
499 				error = 0;
500 			}
501 			return error;
502 		}
503 
504 		nh = ip_hdr(skb);
505 		key->ipv4.addr.src = nh->saddr;
506 		key->ipv4.addr.dst = nh->daddr;
507 
508 		key->ip.proto = nh->protocol;
509 		key->ip.tos = nh->tos;
510 		key->ip.ttl = nh->ttl;
511 
512 		offset = nh->frag_off & htons(IP_OFFSET);
513 		if (offset) {
514 			key->ip.frag = OVS_FRAG_TYPE_LATER;
515 			return 0;
516 		}
517 		if (nh->frag_off & htons(IP_MF) ||
518 			skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
519 			key->ip.frag = OVS_FRAG_TYPE_FIRST;
520 		else
521 			key->ip.frag = OVS_FRAG_TYPE_NONE;
522 
523 		/* Transport layer. */
524 		if (key->ip.proto == IPPROTO_TCP) {
525 			if (tcphdr_ok(skb)) {
526 				struct tcphdr *tcp = tcp_hdr(skb);
527 				key->tp.src = tcp->source;
528 				key->tp.dst = tcp->dest;
529 				key->tp.flags = TCP_FLAGS_BE16(tcp);
530 			} else {
531 				memset(&key->tp, 0, sizeof(key->tp));
532 			}
533 
534 		} else if (key->ip.proto == IPPROTO_UDP) {
535 			if (udphdr_ok(skb)) {
536 				struct udphdr *udp = udp_hdr(skb);
537 				key->tp.src = udp->source;
538 				key->tp.dst = udp->dest;
539 			} else {
540 				memset(&key->tp, 0, sizeof(key->tp));
541 			}
542 		} else if (key->ip.proto == IPPROTO_SCTP) {
543 			if (sctphdr_ok(skb)) {
544 				struct sctphdr *sctp = sctp_hdr(skb);
545 				key->tp.src = sctp->source;
546 				key->tp.dst = sctp->dest;
547 			} else {
548 				memset(&key->tp, 0, sizeof(key->tp));
549 			}
550 		} else if (key->ip.proto == IPPROTO_ICMP) {
551 			if (icmphdr_ok(skb)) {
552 				struct icmphdr *icmp = icmp_hdr(skb);
553 				/* The ICMP type and code fields use the 16-bit
554 				 * transport port fields, so we need to store
555 				 * them in 16-bit network byte order. */
556 				key->tp.src = htons(icmp->type);
557 				key->tp.dst = htons(icmp->code);
558 			} else {
559 				memset(&key->tp, 0, sizeof(key->tp));
560 			}
561 		}
562 
563 	} else if (key->eth.type == htons(ETH_P_ARP) ||
564 		   key->eth.type == htons(ETH_P_RARP)) {
565 		struct arp_eth_header *arp;
566 		bool arp_available = arphdr_ok(skb);
567 
568 		arp = (struct arp_eth_header *)skb_network_header(skb);
569 
570 		if (arp_available &&
571 		    arp->ar_hrd == htons(ARPHRD_ETHER) &&
572 		    arp->ar_pro == htons(ETH_P_IP) &&
573 		    arp->ar_hln == ETH_ALEN &&
574 		    arp->ar_pln == 4) {
575 
576 			/* We only match on the lower 8 bits of the opcode. */
577 			if (ntohs(arp->ar_op) <= 0xff)
578 				key->ip.proto = ntohs(arp->ar_op);
579 			else
580 				key->ip.proto = 0;
581 
582 			memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
583 			memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
584 			ether_addr_copy(key->ipv4.arp.sha, arp->ar_sha);
585 			ether_addr_copy(key->ipv4.arp.tha, arp->ar_tha);
586 		} else {
587 			memset(&key->ip, 0, sizeof(key->ip));
588 			memset(&key->ipv4, 0, sizeof(key->ipv4));
589 		}
590 	} else if (eth_p_mpls(key->eth.type)) {
591 		size_t stack_len = MPLS_HLEN;
592 
593 		/* In the presence of an MPLS label stack the end of the L2
594 		 * header and the beginning of the L3 header differ.
595 		 *
596 		 * Advance network_header to the beginning of the L3
597 		 * header. mac_len corresponds to the end of the L2 header.
598 		 */
599 		while (1) {
600 			__be32 lse;
601 
602 			error = check_header(skb, skb->mac_len + stack_len);
603 			if (unlikely(error))
604 				return 0;
605 
606 			memcpy(&lse, skb_network_header(skb), MPLS_HLEN);
607 
608 			if (stack_len == MPLS_HLEN)
609 				memcpy(&key->mpls.top_lse, &lse, MPLS_HLEN);
610 
611 			skb_set_network_header(skb, skb->mac_len + stack_len);
612 			if (lse & htonl(MPLS_LS_S_MASK))
613 				break;
614 
615 			stack_len += MPLS_HLEN;
616 		}
617 	} else if (key->eth.type == htons(ETH_P_IPV6)) {
618 		int nh_len;             /* IPv6 Header + Extensions */
619 
620 		nh_len = parse_ipv6hdr(skb, key);
621 		if (unlikely(nh_len < 0)) {
622 			memset(&key->ip, 0, sizeof(key->ip));
623 			memset(&key->ipv6.addr, 0, sizeof(key->ipv6.addr));
624 			if (nh_len == -EINVAL) {
625 				skb->transport_header = skb->network_header;
626 				error = 0;
627 			} else {
628 				error = nh_len;
629 			}
630 			return error;
631 		}
632 
633 		if (key->ip.frag == OVS_FRAG_TYPE_LATER)
634 			return 0;
635 		if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
636 			key->ip.frag = OVS_FRAG_TYPE_FIRST;
637 
638 		/* Transport layer. */
639 		if (key->ip.proto == NEXTHDR_TCP) {
640 			if (tcphdr_ok(skb)) {
641 				struct tcphdr *tcp = tcp_hdr(skb);
642 				key->tp.src = tcp->source;
643 				key->tp.dst = tcp->dest;
644 				key->tp.flags = TCP_FLAGS_BE16(tcp);
645 			} else {
646 				memset(&key->tp, 0, sizeof(key->tp));
647 			}
648 		} else if (key->ip.proto == NEXTHDR_UDP) {
649 			if (udphdr_ok(skb)) {
650 				struct udphdr *udp = udp_hdr(skb);
651 				key->tp.src = udp->source;
652 				key->tp.dst = udp->dest;
653 			} else {
654 				memset(&key->tp, 0, sizeof(key->tp));
655 			}
656 		} else if (key->ip.proto == NEXTHDR_SCTP) {
657 			if (sctphdr_ok(skb)) {
658 				struct sctphdr *sctp = sctp_hdr(skb);
659 				key->tp.src = sctp->source;
660 				key->tp.dst = sctp->dest;
661 			} else {
662 				memset(&key->tp, 0, sizeof(key->tp));
663 			}
664 		} else if (key->ip.proto == NEXTHDR_ICMP) {
665 			if (icmp6hdr_ok(skb)) {
666 				error = parse_icmpv6(skb, key, nh_len);
667 				if (error)
668 					return error;
669 			} else {
670 				memset(&key->tp, 0, sizeof(key->tp));
671 			}
672 		}
673 	}
674 	return 0;
675 }
676 
677 int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key)
678 {
679 	return key_extract(skb, key);
680 }
681 
682 int ovs_flow_key_extract(const struct ovs_tunnel_info *tun_info,
683 			 struct sk_buff *skb, struct sw_flow_key *key)
684 {
685 	/* Extract metadata from packet. */
686 	if (tun_info) {
687 		memcpy(&key->tun_key, &tun_info->tunnel, sizeof(key->tun_key));
688 
689 		if (tun_info->options) {
690 			BUILD_BUG_ON((1 << (sizeof(tun_info->options_len) *
691 						   8)) - 1
692 					> sizeof(key->tun_opts));
693 			memcpy(GENEVE_OPTS(key, tun_info->options_len),
694 			       tun_info->options, tun_info->options_len);
695 			key->tun_opts_len = tun_info->options_len;
696 		} else {
697 			key->tun_opts_len = 0;
698 		}
699 	} else  {
700 		key->tun_opts_len = 0;
701 		memset(&key->tun_key, 0, sizeof(key->tun_key));
702 	}
703 
704 	key->phy.priority = skb->priority;
705 	key->phy.in_port = OVS_CB(skb)->input_vport->port_no;
706 	key->phy.skb_mark = skb->mark;
707 	key->ovs_flow_hash = 0;
708 	key->recirc_id = 0;
709 
710 	return key_extract(skb, key);
711 }
712 
713 int ovs_flow_key_extract_userspace(const struct nlattr *attr,
714 				   struct sk_buff *skb,
715 				   struct sw_flow_key *key, bool log)
716 {
717 	int err;
718 
719 	/* Extract metadata from netlink attributes. */
720 	err = ovs_nla_get_flow_metadata(attr, key, log);
721 	if (err)
722 		return err;
723 
724 	return key_extract(skb, key);
725 }
726