xref: /linux/net/openvswitch/flow.c (revision 00a93babd06aaad31d23384cda576ede0f586a8c)
1ccb1352eSJesse Gross /*
2971427f3SAndy Zhou  * Copyright (c) 2007-2014 Nicira, Inc.
3ccb1352eSJesse Gross  *
4ccb1352eSJesse Gross  * This program is free software; you can redistribute it and/or
5ccb1352eSJesse Gross  * modify it under the terms of version 2 of the GNU General Public
6ccb1352eSJesse Gross  * License as published by the Free Software Foundation.
7ccb1352eSJesse Gross  *
8ccb1352eSJesse Gross  * This program is distributed in the hope that it will be useful, but
9ccb1352eSJesse Gross  * WITHOUT ANY WARRANTY; without even the implied warranty of
10ccb1352eSJesse Gross  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11ccb1352eSJesse Gross  * General Public License for more details.
12ccb1352eSJesse Gross  *
13ccb1352eSJesse Gross  * You should have received a copy of the GNU General Public License
14ccb1352eSJesse Gross  * along with this program; if not, write to the Free Software
15ccb1352eSJesse Gross  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16ccb1352eSJesse Gross  * 02110-1301, USA
17ccb1352eSJesse Gross  */
18ccb1352eSJesse Gross 
19ccb1352eSJesse Gross #include <linux/uaccess.h>
20ccb1352eSJesse Gross #include <linux/netdevice.h>
21ccb1352eSJesse Gross #include <linux/etherdevice.h>
22ccb1352eSJesse Gross #include <linux/if_ether.h>
23ccb1352eSJesse Gross #include <linux/if_vlan.h>
24ccb1352eSJesse Gross #include <net/llc_pdu.h>
25ccb1352eSJesse Gross #include <linux/kernel.h>
26ccb1352eSJesse Gross #include <linux/jhash.h>
27ccb1352eSJesse Gross #include <linux/jiffies.h>
28ccb1352eSJesse Gross #include <linux/llc.h>
29ccb1352eSJesse Gross #include <linux/module.h>
30ccb1352eSJesse Gross #include <linux/in.h>
31ccb1352eSJesse Gross #include <linux/rcupdate.h>
32ccb1352eSJesse Gross #include <linux/if_arp.h>
33ccb1352eSJesse Gross #include <linux/ip.h>
34ccb1352eSJesse Gross #include <linux/ipv6.h>
3525cd9ba0SSimon Horman #include <linux/mpls.h>
36a175a723SJoe Stringer #include <linux/sctp.h>
37e298e505SPravin B Shelar #include <linux/smp.h>
38ccb1352eSJesse Gross #include <linux/tcp.h>
39ccb1352eSJesse Gross #include <linux/udp.h>
40ccb1352eSJesse Gross #include <linux/icmp.h>
41ccb1352eSJesse Gross #include <linux/icmpv6.h>
42ccb1352eSJesse Gross #include <linux/rculist.h>
43ccb1352eSJesse Gross #include <net/ip.h>
447d5437c7SPravin B Shelar #include <net/ip_tunnels.h>
45ccb1352eSJesse Gross #include <net/ipv6.h>
4625cd9ba0SSimon Horman #include <net/mpls.h>
47ccb1352eSJesse Gross #include <net/ndisc.h>
48ccb1352eSJesse Gross 
49a581b96dSPravin B Shelar #include "conntrack.h"
5083c8df26SPravin B Shelar #include "datapath.h"
5183c8df26SPravin B Shelar #include "flow.h"
5283c8df26SPravin B Shelar #include "flow_netlink.h"
53a581b96dSPravin B Shelar #include "vport.h"
5483c8df26SPravin B Shelar 
55e6445719SPravin B Shelar u64 ovs_flow_used_time(unsigned long flow_jiffies)
5603f0d916SAndy Zhou {
57e6445719SPravin B Shelar 	struct timespec cur_ts;
58e6445719SPravin B Shelar 	u64 cur_ms, idle_ms;
5903f0d916SAndy Zhou 
60e6445719SPravin B Shelar 	ktime_get_ts(&cur_ts);
61e6445719SPravin B Shelar 	idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
62e6445719SPravin B Shelar 	cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
63e6445719SPravin B Shelar 		 cur_ts.tv_nsec / NSEC_PER_MSEC;
6403f0d916SAndy Zhou 
65e6445719SPravin B Shelar 	return cur_ms - idle_ms;
6603f0d916SAndy Zhou }
6703f0d916SAndy Zhou 
68df23e9f6SJarno Rajahalme #define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF))
6903f0d916SAndy Zhou 
70ad552007SBen Pfaff void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags,
7112eb18f7SThomas Graf 			   const struct sk_buff *skb)
725828cd9aSAndy Zhou {
73e298e505SPravin B Shelar 	struct flow_stats *stats;
7463e7959cSJarno Rajahalme 	int node = numa_node_id();
75df8a39deSJiri Pirko 	int len = skb->len + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
76e6445719SPravin B Shelar 
7763e7959cSJarno Rajahalme 	stats = rcu_dereference(flow->stats[node]);
78e298e505SPravin B Shelar 
7963e7959cSJarno Rajahalme 	/* Check if already have node-specific stats. */
8063e7959cSJarno Rajahalme 	if (likely(stats)) {
81e298e505SPravin B Shelar 		spin_lock(&stats->lock);
8263e7959cSJarno Rajahalme 		/* Mark if we write on the pre-allocated stats. */
8363e7959cSJarno Rajahalme 		if (node == 0 && unlikely(flow->stats_last_writer != node))
8463e7959cSJarno Rajahalme 			flow->stats_last_writer = node;
8563e7959cSJarno Rajahalme 	} else {
8663e7959cSJarno Rajahalme 		stats = rcu_dereference(flow->stats[0]); /* Pre-allocated. */
8763e7959cSJarno Rajahalme 		spin_lock(&stats->lock);
8863e7959cSJarno Rajahalme 
8963e7959cSJarno Rajahalme 		/* If the current NUMA-node is the only writer on the
9063e7959cSJarno Rajahalme 		 * pre-allocated stats keep using them.
9163e7959cSJarno Rajahalme 		 */
9263e7959cSJarno Rajahalme 		if (unlikely(flow->stats_last_writer != node)) {
9363e7959cSJarno Rajahalme 			/* A previous locker may have already allocated the
9463e7959cSJarno Rajahalme 			 * stats, so we need to check again.  If node-specific
9563e7959cSJarno Rajahalme 			 * stats were already allocated, we update the pre-
9663e7959cSJarno Rajahalme 			 * allocated stats as we have already locked them.
9763e7959cSJarno Rajahalme 			 */
9863e7959cSJarno Rajahalme 			if (likely(flow->stats_last_writer != NUMA_NO_NODE)
998c6b00c8SAndreea-Cristina Bernat 			    && likely(!rcu_access_pointer(flow->stats[node]))) {
10063e7959cSJarno Rajahalme 				/* Try to allocate node-specific stats. */
10163e7959cSJarno Rajahalme 				struct flow_stats *new_stats;
10263e7959cSJarno Rajahalme 
10363e7959cSJarno Rajahalme 				new_stats =
10463e7959cSJarno Rajahalme 					kmem_cache_alloc_node(flow_stats_cache,
1054167e9b2SDavid Rientjes 							      GFP_NOWAIT |
1064167e9b2SDavid Rientjes 							      __GFP_THISNODE |
1074167e9b2SDavid Rientjes 							      __GFP_NOWARN |
10863e7959cSJarno Rajahalme 							      __GFP_NOMEMALLOC,
10963e7959cSJarno Rajahalme 							      node);
11063e7959cSJarno Rajahalme 				if (likely(new_stats)) {
11163e7959cSJarno Rajahalme 					new_stats->used = jiffies;
11263e7959cSJarno Rajahalme 					new_stats->packet_count = 1;
11324cc59d1SBen Pfaff 					new_stats->byte_count = len;
11463e7959cSJarno Rajahalme 					new_stats->tcp_flags = tcp_flags;
11563e7959cSJarno Rajahalme 					spin_lock_init(&new_stats->lock);
11663e7959cSJarno Rajahalme 
11763e7959cSJarno Rajahalme 					rcu_assign_pointer(flow->stats[node],
11863e7959cSJarno Rajahalme 							   new_stats);
11963e7959cSJarno Rajahalme 					goto unlock;
12063e7959cSJarno Rajahalme 				}
12163e7959cSJarno Rajahalme 			}
12263e7959cSJarno Rajahalme 			flow->stats_last_writer = node;
12363e7959cSJarno Rajahalme 		}
12463e7959cSJarno Rajahalme 	}
12563e7959cSJarno Rajahalme 
126e298e505SPravin B Shelar 	stats->used = jiffies;
127e298e505SPravin B Shelar 	stats->packet_count++;
12824cc59d1SBen Pfaff 	stats->byte_count += len;
129e298e505SPravin B Shelar 	stats->tcp_flags |= tcp_flags;
13063e7959cSJarno Rajahalme unlock:
131e298e505SPravin B Shelar 	spin_unlock(&stats->lock);
132e298e505SPravin B Shelar }
133e298e505SPravin B Shelar 
13486ec8dbaSJarno Rajahalme /* Must be called with rcu_read_lock or ovs_mutex. */
13586ec8dbaSJarno Rajahalme void ovs_flow_stats_get(const struct sw_flow *flow,
13686ec8dbaSJarno Rajahalme 			struct ovs_flow_stats *ovs_stats,
137e298e505SPravin B Shelar 			unsigned long *used, __be16 *tcp_flags)
138e298e505SPravin B Shelar {
13963e7959cSJarno Rajahalme 	int node;
140e298e505SPravin B Shelar 
141e298e505SPravin B Shelar 	*used = 0;
142e298e505SPravin B Shelar 	*tcp_flags = 0;
143e298e505SPravin B Shelar 	memset(ovs_stats, 0, sizeof(*ovs_stats));
144e298e505SPravin B Shelar 
14563e7959cSJarno Rajahalme 	for_each_node(node) {
14686ec8dbaSJarno Rajahalme 		struct flow_stats *stats = rcu_dereference_ovsl(flow->stats[node]);
14723dabf88SJarno Rajahalme 
14863e7959cSJarno Rajahalme 		if (stats) {
14963e7959cSJarno Rajahalme 			/* Local CPU may write on non-local stats, so we must
15063e7959cSJarno Rajahalme 			 * block bottom-halves here.
15163e7959cSJarno Rajahalme 			 */
15263e7959cSJarno Rajahalme 			spin_lock_bh(&stats->lock);
15363e7959cSJarno Rajahalme 			if (!*used || time_after(stats->used, *used))
15463e7959cSJarno Rajahalme 				*used = stats->used;
15563e7959cSJarno Rajahalme 			*tcp_flags |= stats->tcp_flags;
15663e7959cSJarno Rajahalme 			ovs_stats->n_packets += stats->packet_count;
15763e7959cSJarno Rajahalme 			ovs_stats->n_bytes += stats->byte_count;
15863e7959cSJarno Rajahalme 			spin_unlock_bh(&stats->lock);
1594f647e0aSFlavio Leitner 		}
160e298e505SPravin B Shelar 	}
161e298e505SPravin B Shelar }
162e298e505SPravin B Shelar 
16386ec8dbaSJarno Rajahalme /* Called with ovs_mutex. */
164e298e505SPravin B Shelar void ovs_flow_stats_clear(struct sw_flow *flow)
165e298e505SPravin B Shelar {
16663e7959cSJarno Rajahalme 	int node;
167e298e505SPravin B Shelar 
16863e7959cSJarno Rajahalme 	for_each_node(node) {
16986ec8dbaSJarno Rajahalme 		struct flow_stats *stats = ovsl_dereference(flow->stats[node]);
17023dabf88SJarno Rajahalme 
17163e7959cSJarno Rajahalme 		if (stats) {
17263e7959cSJarno Rajahalme 			spin_lock_bh(&stats->lock);
17363e7959cSJarno Rajahalme 			stats->used = 0;
17463e7959cSJarno Rajahalme 			stats->packet_count = 0;
17563e7959cSJarno Rajahalme 			stats->byte_count = 0;
17663e7959cSJarno Rajahalme 			stats->tcp_flags = 0;
17763e7959cSJarno Rajahalme 			spin_unlock_bh(&stats->lock);
17863e7959cSJarno Rajahalme 		}
17963e7959cSJarno Rajahalme 	}
180e298e505SPravin B Shelar }
18103f0d916SAndy Zhou 
182ccb1352eSJesse Gross static int check_header(struct sk_buff *skb, int len)
183ccb1352eSJesse Gross {
184ccb1352eSJesse Gross 	if (unlikely(skb->len < len))
185ccb1352eSJesse Gross 		return -EINVAL;
186ccb1352eSJesse Gross 	if (unlikely(!pskb_may_pull(skb, len)))
187ccb1352eSJesse Gross 		return -ENOMEM;
188ccb1352eSJesse Gross 	return 0;
189ccb1352eSJesse Gross }
190ccb1352eSJesse Gross 
191ccb1352eSJesse Gross static bool arphdr_ok(struct sk_buff *skb)
192ccb1352eSJesse Gross {
193ccb1352eSJesse Gross 	return pskb_may_pull(skb, skb_network_offset(skb) +
194ccb1352eSJesse Gross 				  sizeof(struct arp_eth_header));
195ccb1352eSJesse Gross }
196ccb1352eSJesse Gross 
197ccb1352eSJesse Gross static int check_iphdr(struct sk_buff *skb)
198ccb1352eSJesse Gross {
199ccb1352eSJesse Gross 	unsigned int nh_ofs = skb_network_offset(skb);
200ccb1352eSJesse Gross 	unsigned int ip_len;
201ccb1352eSJesse Gross 	int err;
202ccb1352eSJesse Gross 
203ccb1352eSJesse Gross 	err = check_header(skb, nh_ofs + sizeof(struct iphdr));
204ccb1352eSJesse Gross 	if (unlikely(err))
205ccb1352eSJesse Gross 		return err;
206ccb1352eSJesse Gross 
207ccb1352eSJesse Gross 	ip_len = ip_hdrlen(skb);
208ccb1352eSJesse Gross 	if (unlikely(ip_len < sizeof(struct iphdr) ||
209ccb1352eSJesse Gross 		     skb->len < nh_ofs + ip_len))
210ccb1352eSJesse Gross 		return -EINVAL;
211ccb1352eSJesse Gross 
212ccb1352eSJesse Gross 	skb_set_transport_header(skb, nh_ofs + ip_len);
213ccb1352eSJesse Gross 	return 0;
214ccb1352eSJesse Gross }
215ccb1352eSJesse Gross 
216ccb1352eSJesse Gross static bool tcphdr_ok(struct sk_buff *skb)
217ccb1352eSJesse Gross {
218ccb1352eSJesse Gross 	int th_ofs = skb_transport_offset(skb);
219ccb1352eSJesse Gross 	int tcp_len;
220ccb1352eSJesse Gross 
221ccb1352eSJesse Gross 	if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
222ccb1352eSJesse Gross 		return false;
223ccb1352eSJesse Gross 
224ccb1352eSJesse Gross 	tcp_len = tcp_hdrlen(skb);
225ccb1352eSJesse Gross 	if (unlikely(tcp_len < sizeof(struct tcphdr) ||
226ccb1352eSJesse Gross 		     skb->len < th_ofs + tcp_len))
227ccb1352eSJesse Gross 		return false;
228ccb1352eSJesse Gross 
229ccb1352eSJesse Gross 	return true;
230ccb1352eSJesse Gross }
231ccb1352eSJesse Gross 
232ccb1352eSJesse Gross static bool udphdr_ok(struct sk_buff *skb)
233ccb1352eSJesse Gross {
234ccb1352eSJesse Gross 	return pskb_may_pull(skb, skb_transport_offset(skb) +
235ccb1352eSJesse Gross 				  sizeof(struct udphdr));
236ccb1352eSJesse Gross }
237ccb1352eSJesse Gross 
238a175a723SJoe Stringer static bool sctphdr_ok(struct sk_buff *skb)
239a175a723SJoe Stringer {
240a175a723SJoe Stringer 	return pskb_may_pull(skb, skb_transport_offset(skb) +
241a175a723SJoe Stringer 				  sizeof(struct sctphdr));
242a175a723SJoe Stringer }
243a175a723SJoe Stringer 
244ccb1352eSJesse Gross static bool icmphdr_ok(struct sk_buff *skb)
245ccb1352eSJesse Gross {
246ccb1352eSJesse Gross 	return pskb_may_pull(skb, skb_transport_offset(skb) +
247ccb1352eSJesse Gross 				  sizeof(struct icmphdr));
248ccb1352eSJesse Gross }
249ccb1352eSJesse Gross 
25003f0d916SAndy Zhou static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
251ccb1352eSJesse Gross {
252ccb1352eSJesse Gross 	unsigned int nh_ofs = skb_network_offset(skb);
253ccb1352eSJesse Gross 	unsigned int nh_len;
254ccb1352eSJesse Gross 	int payload_ofs;
255ccb1352eSJesse Gross 	struct ipv6hdr *nh;
256ccb1352eSJesse Gross 	uint8_t nexthdr;
257ccb1352eSJesse Gross 	__be16 frag_off;
258ccb1352eSJesse Gross 	int err;
259ccb1352eSJesse Gross 
260ccb1352eSJesse Gross 	err = check_header(skb, nh_ofs + sizeof(*nh));
261ccb1352eSJesse Gross 	if (unlikely(err))
262ccb1352eSJesse Gross 		return err;
263ccb1352eSJesse Gross 
264ccb1352eSJesse Gross 	nh = ipv6_hdr(skb);
265ccb1352eSJesse Gross 	nexthdr = nh->nexthdr;
266ccb1352eSJesse Gross 	payload_ofs = (u8 *)(nh + 1) - skb->data;
267ccb1352eSJesse Gross 
268ccb1352eSJesse Gross 	key->ip.proto = NEXTHDR_NONE;
269ccb1352eSJesse Gross 	key->ip.tos = ipv6_get_dsfield(nh);
270ccb1352eSJesse Gross 	key->ip.ttl = nh->hop_limit;
271ccb1352eSJesse Gross 	key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
272ccb1352eSJesse Gross 	key->ipv6.addr.src = nh->saddr;
273ccb1352eSJesse Gross 	key->ipv6.addr.dst = nh->daddr;
274ccb1352eSJesse Gross 
275ccb1352eSJesse Gross 	payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
276ccb1352eSJesse Gross 
277ccb1352eSJesse Gross 	if (frag_off) {
278ccb1352eSJesse Gross 		if (frag_off & htons(~0x7))
279ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_LATER;
280ccb1352eSJesse Gross 		else
281ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_FIRST;
28225ef1328SPravin B Shelar 	} else {
28325ef1328SPravin B Shelar 		key->ip.frag = OVS_FRAG_TYPE_NONE;
284ccb1352eSJesse Gross 	}
285ccb1352eSJesse Gross 
286c30da497SSimon Horman 	/* Delayed handling of error in ipv6_skip_exthdr() as it
287c30da497SSimon Horman 	 * always sets frag_off to a valid value which may be
288c30da497SSimon Horman 	 * used to set key->ip.frag above.
289c30da497SSimon Horman 	 */
290c30da497SSimon Horman 	if (unlikely(payload_ofs < 0))
291c30da497SSimon Horman 		return -EPROTO;
292c30da497SSimon Horman 
293ccb1352eSJesse Gross 	nh_len = payload_ofs - nh_ofs;
294ccb1352eSJesse Gross 	skb_set_transport_header(skb, nh_ofs + nh_len);
295ccb1352eSJesse Gross 	key->ip.proto = nexthdr;
296ccb1352eSJesse Gross 	return nh_len;
297ccb1352eSJesse Gross }
298ccb1352eSJesse Gross 
299ccb1352eSJesse Gross static bool icmp6hdr_ok(struct sk_buff *skb)
300ccb1352eSJesse Gross {
301ccb1352eSJesse Gross 	return pskb_may_pull(skb, skb_transport_offset(skb) +
302ccb1352eSJesse Gross 				  sizeof(struct icmp6hdr));
303ccb1352eSJesse Gross }
304ccb1352eSJesse Gross 
305ccb1352eSJesse Gross static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
306ccb1352eSJesse Gross {
307ccb1352eSJesse Gross 	struct qtag_prefix {
308ccb1352eSJesse Gross 		__be16 eth_type; /* ETH_P_8021Q */
309ccb1352eSJesse Gross 		__be16 tci;
310ccb1352eSJesse Gross 	};
311ccb1352eSJesse Gross 	struct qtag_prefix *qp;
312ccb1352eSJesse Gross 
313ccb1352eSJesse Gross 	if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
314ccb1352eSJesse Gross 		return 0;
315ccb1352eSJesse Gross 
316ccb1352eSJesse Gross 	if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
317ccb1352eSJesse Gross 					 sizeof(__be16))))
318ccb1352eSJesse Gross 		return -ENOMEM;
319ccb1352eSJesse Gross 
320ccb1352eSJesse Gross 	qp = (struct qtag_prefix *) skb->data;
321ccb1352eSJesse Gross 	key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
322ccb1352eSJesse Gross 	__skb_pull(skb, sizeof(struct qtag_prefix));
323ccb1352eSJesse Gross 
324ccb1352eSJesse Gross 	return 0;
325ccb1352eSJesse Gross }
326ccb1352eSJesse Gross 
327ccb1352eSJesse Gross static __be16 parse_ethertype(struct sk_buff *skb)
328ccb1352eSJesse Gross {
329ccb1352eSJesse Gross 	struct llc_snap_hdr {
330ccb1352eSJesse Gross 		u8  dsap;  /* Always 0xAA */
331ccb1352eSJesse Gross 		u8  ssap;  /* Always 0xAA */
332ccb1352eSJesse Gross 		u8  ctrl;
333ccb1352eSJesse Gross 		u8  oui[3];
334ccb1352eSJesse Gross 		__be16 ethertype;
335ccb1352eSJesse Gross 	};
336ccb1352eSJesse Gross 	struct llc_snap_hdr *llc;
337ccb1352eSJesse Gross 	__be16 proto;
338ccb1352eSJesse Gross 
339ccb1352eSJesse Gross 	proto = *(__be16 *) skb->data;
340ccb1352eSJesse Gross 	__skb_pull(skb, sizeof(__be16));
341ccb1352eSJesse Gross 
3426713fc9bSAlexander Duyck 	if (eth_proto_is_802_3(proto))
343ccb1352eSJesse Gross 		return proto;
344ccb1352eSJesse Gross 
345ccb1352eSJesse Gross 	if (skb->len < sizeof(struct llc_snap_hdr))
346ccb1352eSJesse Gross 		return htons(ETH_P_802_2);
347ccb1352eSJesse Gross 
348ccb1352eSJesse Gross 	if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
349ccb1352eSJesse Gross 		return htons(0);
350ccb1352eSJesse Gross 
351ccb1352eSJesse Gross 	llc = (struct llc_snap_hdr *) skb->data;
352ccb1352eSJesse Gross 	if (llc->dsap != LLC_SAP_SNAP ||
353ccb1352eSJesse Gross 	    llc->ssap != LLC_SAP_SNAP ||
354ccb1352eSJesse Gross 	    (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
355ccb1352eSJesse Gross 		return htons(ETH_P_802_2);
356ccb1352eSJesse Gross 
357ccb1352eSJesse Gross 	__skb_pull(skb, sizeof(struct llc_snap_hdr));
35817b682a0SRich Lane 
3596713fc9bSAlexander Duyck 	if (eth_proto_is_802_3(llc->ethertype))
360ccb1352eSJesse Gross 		return llc->ethertype;
36117b682a0SRich Lane 
36217b682a0SRich Lane 	return htons(ETH_P_802_2);
363ccb1352eSJesse Gross }
364ccb1352eSJesse Gross 
365ccb1352eSJesse Gross static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
36603f0d916SAndy Zhou 			int nh_len)
367ccb1352eSJesse Gross {
368ccb1352eSJesse Gross 	struct icmp6hdr *icmp = icmp6_hdr(skb);
369ccb1352eSJesse Gross 
370ccb1352eSJesse Gross 	/* The ICMPv6 type and code fields use the 16-bit transport port
371ccb1352eSJesse Gross 	 * fields, so we need to store them in 16-bit network byte order.
372ccb1352eSJesse Gross 	 */
3731139e241SJarno Rajahalme 	key->tp.src = htons(icmp->icmp6_type);
3741139e241SJarno Rajahalme 	key->tp.dst = htons(icmp->icmp6_code);
37525ef1328SPravin B Shelar 	memset(&key->ipv6.nd, 0, sizeof(key->ipv6.nd));
376ccb1352eSJesse Gross 
377ccb1352eSJesse Gross 	if (icmp->icmp6_code == 0 &&
378ccb1352eSJesse Gross 	    (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
379ccb1352eSJesse Gross 	     icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
380ccb1352eSJesse Gross 		int icmp_len = skb->len - skb_transport_offset(skb);
381ccb1352eSJesse Gross 		struct nd_msg *nd;
382ccb1352eSJesse Gross 		int offset;
383ccb1352eSJesse Gross 
384ccb1352eSJesse Gross 		/* In order to process neighbor discovery options, we need the
385ccb1352eSJesse Gross 		 * entire packet.
386ccb1352eSJesse Gross 		 */
387ccb1352eSJesse Gross 		if (unlikely(icmp_len < sizeof(*nd)))
38803f0d916SAndy Zhou 			return 0;
38903f0d916SAndy Zhou 
39003f0d916SAndy Zhou 		if (unlikely(skb_linearize(skb)))
39103f0d916SAndy Zhou 			return -ENOMEM;
392ccb1352eSJesse Gross 
393ccb1352eSJesse Gross 		nd = (struct nd_msg *)skb_transport_header(skb);
394ccb1352eSJesse Gross 		key->ipv6.nd.target = nd->target;
395ccb1352eSJesse Gross 
396ccb1352eSJesse Gross 		icmp_len -= sizeof(*nd);
397ccb1352eSJesse Gross 		offset = 0;
398ccb1352eSJesse Gross 		while (icmp_len >= 8) {
399ccb1352eSJesse Gross 			struct nd_opt_hdr *nd_opt =
400ccb1352eSJesse Gross 				 (struct nd_opt_hdr *)(nd->opt + offset);
401ccb1352eSJesse Gross 			int opt_len = nd_opt->nd_opt_len * 8;
402ccb1352eSJesse Gross 
403ccb1352eSJesse Gross 			if (unlikely(!opt_len || opt_len > icmp_len))
40403f0d916SAndy Zhou 				return 0;
405ccb1352eSJesse Gross 
406ccb1352eSJesse Gross 			/* Store the link layer address if the appropriate
407ccb1352eSJesse Gross 			 * option is provided.  It is considered an error if
408ccb1352eSJesse Gross 			 * the same link layer option is specified twice.
409ccb1352eSJesse Gross 			 */
410ccb1352eSJesse Gross 			if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
411ccb1352eSJesse Gross 			    && opt_len == 8) {
412ccb1352eSJesse Gross 				if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
413ccb1352eSJesse Gross 					goto invalid;
4148c63ff09SJoe Perches 				ether_addr_copy(key->ipv6.nd.sll,
4158c63ff09SJoe Perches 						&nd->opt[offset+sizeof(*nd_opt)]);
416ccb1352eSJesse Gross 			} else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
417ccb1352eSJesse Gross 				   && opt_len == 8) {
418ccb1352eSJesse Gross 				if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
419ccb1352eSJesse Gross 					goto invalid;
4208c63ff09SJoe Perches 				ether_addr_copy(key->ipv6.nd.tll,
4218c63ff09SJoe Perches 						&nd->opt[offset+sizeof(*nd_opt)]);
422ccb1352eSJesse Gross 			}
423ccb1352eSJesse Gross 
424ccb1352eSJesse Gross 			icmp_len -= opt_len;
425ccb1352eSJesse Gross 			offset += opt_len;
426ccb1352eSJesse Gross 		}
427ccb1352eSJesse Gross 	}
428ccb1352eSJesse Gross 
42903f0d916SAndy Zhou 	return 0;
430ccb1352eSJesse Gross 
431ccb1352eSJesse Gross invalid:
432ccb1352eSJesse Gross 	memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
433ccb1352eSJesse Gross 	memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
434ccb1352eSJesse Gross 	memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
435ccb1352eSJesse Gross 
43603f0d916SAndy Zhou 	return 0;
437ccb1352eSJesse Gross }
438ccb1352eSJesse Gross 
439ccb1352eSJesse Gross /**
44083c8df26SPravin B Shelar  * key_extract - extracts a flow key from an Ethernet frame.
441ccb1352eSJesse Gross  * @skb: sk_buff that contains the frame, with skb->data pointing to the
442ccb1352eSJesse Gross  * Ethernet header
443ccb1352eSJesse Gross  * @key: output flow key
444ccb1352eSJesse Gross  *
445ccb1352eSJesse Gross  * The caller must ensure that skb->len >= ETH_HLEN.
446ccb1352eSJesse Gross  *
447ccb1352eSJesse Gross  * Returns 0 if successful, otherwise a negative errno value.
448ccb1352eSJesse Gross  *
449ccb1352eSJesse Gross  * Initializes @skb header pointers as follows:
450ccb1352eSJesse Gross  *
451ccb1352eSJesse Gross  *    - skb->mac_header: the Ethernet header.
452ccb1352eSJesse Gross  *
453ccb1352eSJesse Gross  *    - skb->network_header: just past the Ethernet header, or just past the
454ccb1352eSJesse Gross  *      VLAN header, to the first byte of the Ethernet payload.
455ccb1352eSJesse Gross  *
45634d94f21SLorand Jakab  *    - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
457ccb1352eSJesse Gross  *      on output, then just past the IP header, if one is present and
458ccb1352eSJesse Gross  *      of a correct length, otherwise the same as skb->network_header.
45934d94f21SLorand Jakab  *      For other key->eth.type values it is left untouched.
460ccb1352eSJesse Gross  */
46183c8df26SPravin B Shelar static int key_extract(struct sk_buff *skb, struct sw_flow_key *key)
462ccb1352eSJesse Gross {
46303f0d916SAndy Zhou 	int error;
464ccb1352eSJesse Gross 	struct ethhdr *eth;
465ccb1352eSJesse Gross 
466f5796684SJesse Gross 	/* Flags are always used as part of stats */
467f5796684SJesse Gross 	key->tp.flags = 0;
468f5796684SJesse Gross 
469ccb1352eSJesse Gross 	skb_reset_mac_header(skb);
470ccb1352eSJesse Gross 
471ccb1352eSJesse Gross 	/* Link layer.  We are guaranteed to have at least the 14 byte Ethernet
472ccb1352eSJesse Gross 	 * header in the linear data area.
473ccb1352eSJesse Gross 	 */
474ccb1352eSJesse Gross 	eth = eth_hdr(skb);
4758c63ff09SJoe Perches 	ether_addr_copy(key->eth.src, eth->h_source);
4768c63ff09SJoe Perches 	ether_addr_copy(key->eth.dst, eth->h_dest);
477ccb1352eSJesse Gross 
478ccb1352eSJesse Gross 	__skb_pull(skb, 2 * ETH_ALEN);
479b34df5e8SPravin B Shelar 	/* We are going to push all headers that we pull, so no need to
480b34df5e8SPravin B Shelar 	 * update skb->csum here.
481b34df5e8SPravin B Shelar 	 */
482ccb1352eSJesse Gross 
48307148121SJesse Gross 	key->eth.tci = 0;
484df8a39deSJiri Pirko 	if (skb_vlan_tag_present(skb))
485ccb1352eSJesse Gross 		key->eth.tci = htons(skb->vlan_tci);
486ccb1352eSJesse Gross 	else if (eth->h_proto == htons(ETH_P_8021Q))
487ccb1352eSJesse Gross 		if (unlikely(parse_vlan(skb, key)))
488ccb1352eSJesse Gross 			return -ENOMEM;
489ccb1352eSJesse Gross 
490ccb1352eSJesse Gross 	key->eth.type = parse_ethertype(skb);
491ccb1352eSJesse Gross 	if (unlikely(key->eth.type == htons(0)))
492ccb1352eSJesse Gross 		return -ENOMEM;
493ccb1352eSJesse Gross 
494ccb1352eSJesse Gross 	skb_reset_network_header(skb);
49525cd9ba0SSimon Horman 	skb_reset_mac_len(skb);
496ccb1352eSJesse Gross 	__skb_push(skb, skb->data - skb_mac_header(skb));
497ccb1352eSJesse Gross 
498ccb1352eSJesse Gross 	/* Network layer. */
499ccb1352eSJesse Gross 	if (key->eth.type == htons(ETH_P_IP)) {
500ccb1352eSJesse Gross 		struct iphdr *nh;
501ccb1352eSJesse Gross 		__be16 offset;
502ccb1352eSJesse Gross 
503ccb1352eSJesse Gross 		error = check_iphdr(skb);
504ccb1352eSJesse Gross 		if (unlikely(error)) {
50507148121SJesse Gross 			memset(&key->ip, 0, sizeof(key->ip));
50607148121SJesse Gross 			memset(&key->ipv4, 0, sizeof(key->ipv4));
507ccb1352eSJesse Gross 			if (error == -EINVAL) {
508ccb1352eSJesse Gross 				skb->transport_header = skb->network_header;
509ccb1352eSJesse Gross 				error = 0;
510ccb1352eSJesse Gross 			}
51103f0d916SAndy Zhou 			return error;
512ccb1352eSJesse Gross 		}
513ccb1352eSJesse Gross 
514ccb1352eSJesse Gross 		nh = ip_hdr(skb);
515ccb1352eSJesse Gross 		key->ipv4.addr.src = nh->saddr;
516ccb1352eSJesse Gross 		key->ipv4.addr.dst = nh->daddr;
517ccb1352eSJesse Gross 
518ccb1352eSJesse Gross 		key->ip.proto = nh->protocol;
519ccb1352eSJesse Gross 		key->ip.tos = nh->tos;
520ccb1352eSJesse Gross 		key->ip.ttl = nh->ttl;
521ccb1352eSJesse Gross 
522ccb1352eSJesse Gross 		offset = nh->frag_off & htons(IP_OFFSET);
523ccb1352eSJesse Gross 		if (offset) {
524ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_LATER;
52503f0d916SAndy Zhou 			return 0;
526ccb1352eSJesse Gross 		}
527ccb1352eSJesse Gross 		if (nh->frag_off & htons(IP_MF) ||
528ccb1352eSJesse Gross 			skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
529ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_FIRST;
53007148121SJesse Gross 		else
53107148121SJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_NONE;
532ccb1352eSJesse Gross 
533ccb1352eSJesse Gross 		/* Transport layer. */
534ccb1352eSJesse Gross 		if (key->ip.proto == IPPROTO_TCP) {
535ccb1352eSJesse Gross 			if (tcphdr_ok(skb)) {
536ccb1352eSJesse Gross 				struct tcphdr *tcp = tcp_hdr(skb);
5371139e241SJarno Rajahalme 				key->tp.src = tcp->source;
5381139e241SJarno Rajahalme 				key->tp.dst = tcp->dest;
5391139e241SJarno Rajahalme 				key->tp.flags = TCP_FLAGS_BE16(tcp);
54007148121SJesse Gross 			} else {
54107148121SJesse Gross 				memset(&key->tp, 0, sizeof(key->tp));
542ccb1352eSJesse Gross 			}
54307148121SJesse Gross 
544ccb1352eSJesse Gross 		} else if (key->ip.proto == IPPROTO_UDP) {
545ccb1352eSJesse Gross 			if (udphdr_ok(skb)) {
546ccb1352eSJesse Gross 				struct udphdr *udp = udp_hdr(skb);
5471139e241SJarno Rajahalme 				key->tp.src = udp->source;
5481139e241SJarno Rajahalme 				key->tp.dst = udp->dest;
54907148121SJesse Gross 			} else {
55007148121SJesse Gross 				memset(&key->tp, 0, sizeof(key->tp));
551ccb1352eSJesse Gross 			}
552a175a723SJoe Stringer 		} else if (key->ip.proto == IPPROTO_SCTP) {
553a175a723SJoe Stringer 			if (sctphdr_ok(skb)) {
554a175a723SJoe Stringer 				struct sctphdr *sctp = sctp_hdr(skb);
5551139e241SJarno Rajahalme 				key->tp.src = sctp->source;
5561139e241SJarno Rajahalme 				key->tp.dst = sctp->dest;
55707148121SJesse Gross 			} else {
55807148121SJesse Gross 				memset(&key->tp, 0, sizeof(key->tp));
559a175a723SJoe Stringer 			}
560ccb1352eSJesse Gross 		} else if (key->ip.proto == IPPROTO_ICMP) {
561ccb1352eSJesse Gross 			if (icmphdr_ok(skb)) {
562ccb1352eSJesse Gross 				struct icmphdr *icmp = icmp_hdr(skb);
563ccb1352eSJesse Gross 				/* The ICMP type and code fields use the 16-bit
564ccb1352eSJesse Gross 				 * transport port fields, so we need to store
565ccb1352eSJesse Gross 				 * them in 16-bit network byte order. */
5661139e241SJarno Rajahalme 				key->tp.src = htons(icmp->type);
5671139e241SJarno Rajahalme 				key->tp.dst = htons(icmp->code);
56807148121SJesse Gross 			} else {
56907148121SJesse Gross 				memset(&key->tp, 0, sizeof(key->tp));
570ccb1352eSJesse Gross 			}
571ccb1352eSJesse Gross 		}
572ccb1352eSJesse Gross 
57307148121SJesse Gross 	} else if (key->eth.type == htons(ETH_P_ARP) ||
57407148121SJesse Gross 		   key->eth.type == htons(ETH_P_RARP)) {
575ccb1352eSJesse Gross 		struct arp_eth_header *arp;
576389f4894SLi RongQing 		bool arp_available = arphdr_ok(skb);
577ccb1352eSJesse Gross 
578ccb1352eSJesse Gross 		arp = (struct arp_eth_header *)skb_network_header(skb);
579ccb1352eSJesse Gross 
580389f4894SLi RongQing 		if (arp_available &&
58107148121SJesse Gross 		    arp->ar_hrd == htons(ARPHRD_ETHER) &&
58207148121SJesse Gross 		    arp->ar_pro == htons(ETH_P_IP) &&
58307148121SJesse Gross 		    arp->ar_hln == ETH_ALEN &&
58407148121SJesse Gross 		    arp->ar_pln == 4) {
585ccb1352eSJesse Gross 
586ccb1352eSJesse Gross 			/* We only match on the lower 8 bits of the opcode. */
587ccb1352eSJesse Gross 			if (ntohs(arp->ar_op) <= 0xff)
588ccb1352eSJesse Gross 				key->ip.proto = ntohs(arp->ar_op);
58907148121SJesse Gross 			else
59007148121SJesse Gross 				key->ip.proto = 0;
59107148121SJesse Gross 
592ccb1352eSJesse Gross 			memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
593ccb1352eSJesse Gross 			memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
5948c63ff09SJoe Perches 			ether_addr_copy(key->ipv4.arp.sha, arp->ar_sha);
5958c63ff09SJoe Perches 			ether_addr_copy(key->ipv4.arp.tha, arp->ar_tha);
59607148121SJesse Gross 		} else {
59707148121SJesse Gross 			memset(&key->ip, 0, sizeof(key->ip));
59807148121SJesse Gross 			memset(&key->ipv4, 0, sizeof(key->ipv4));
599ccb1352eSJesse Gross 		}
60025cd9ba0SSimon Horman 	} else if (eth_p_mpls(key->eth.type)) {
60125cd9ba0SSimon Horman 		size_t stack_len = MPLS_HLEN;
60225cd9ba0SSimon Horman 
60325cd9ba0SSimon Horman 		/* In the presence of an MPLS label stack the end of the L2
60425cd9ba0SSimon Horman 		 * header and the beginning of the L3 header differ.
60525cd9ba0SSimon Horman 		 *
60625cd9ba0SSimon Horman 		 * Advance network_header to the beginning of the L3
60725cd9ba0SSimon Horman 		 * header. mac_len corresponds to the end of the L2 header.
60825cd9ba0SSimon Horman 		 */
60925cd9ba0SSimon Horman 		while (1) {
61025cd9ba0SSimon Horman 			__be32 lse;
61125cd9ba0SSimon Horman 
61225cd9ba0SSimon Horman 			error = check_header(skb, skb->mac_len + stack_len);
61325cd9ba0SSimon Horman 			if (unlikely(error))
61425cd9ba0SSimon Horman 				return 0;
61525cd9ba0SSimon Horman 
61625cd9ba0SSimon Horman 			memcpy(&lse, skb_network_header(skb), MPLS_HLEN);
61725cd9ba0SSimon Horman 
61825cd9ba0SSimon Horman 			if (stack_len == MPLS_HLEN)
61925cd9ba0SSimon Horman 				memcpy(&key->mpls.top_lse, &lse, MPLS_HLEN);
62025cd9ba0SSimon Horman 
62125cd9ba0SSimon Horman 			skb_set_network_header(skb, skb->mac_len + stack_len);
62225cd9ba0SSimon Horman 			if (lse & htonl(MPLS_LS_S_MASK))
62325cd9ba0SSimon Horman 				break;
62425cd9ba0SSimon Horman 
62525cd9ba0SSimon Horman 			stack_len += MPLS_HLEN;
62625cd9ba0SSimon Horman 		}
627ccb1352eSJesse Gross 	} else if (key->eth.type == htons(ETH_P_IPV6)) {
628ccb1352eSJesse Gross 		int nh_len;             /* IPv6 Header + Extensions */
629ccb1352eSJesse Gross 
63003f0d916SAndy Zhou 		nh_len = parse_ipv6hdr(skb, key);
631ccb1352eSJesse Gross 		if (unlikely(nh_len < 0)) {
632c30da497SSimon Horman 			switch (nh_len) {
633c30da497SSimon Horman 			case -EINVAL:
63407148121SJesse Gross 				memset(&key->ip, 0, sizeof(key->ip));
63507148121SJesse Gross 				memset(&key->ipv6.addr, 0, sizeof(key->ipv6.addr));
636c30da497SSimon Horman 				/* fall-through */
637c30da497SSimon Horman 			case -EPROTO:
638ccb1352eSJesse Gross 				skb->transport_header = skb->network_header;
63903f0d916SAndy Zhou 				error = 0;
640c30da497SSimon Horman 				break;
641c30da497SSimon Horman 			default:
642ccb1352eSJesse Gross 				error = nh_len;
64303f0d916SAndy Zhou 			}
64403f0d916SAndy Zhou 			return error;
645ccb1352eSJesse Gross 		}
646ccb1352eSJesse Gross 
647ccb1352eSJesse Gross 		if (key->ip.frag == OVS_FRAG_TYPE_LATER)
64803f0d916SAndy Zhou 			return 0;
649ccb1352eSJesse Gross 		if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
650ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_FIRST;
651ccb1352eSJesse Gross 
652ccb1352eSJesse Gross 		/* Transport layer. */
653ccb1352eSJesse Gross 		if (key->ip.proto == NEXTHDR_TCP) {
654ccb1352eSJesse Gross 			if (tcphdr_ok(skb)) {
655ccb1352eSJesse Gross 				struct tcphdr *tcp = tcp_hdr(skb);
6561139e241SJarno Rajahalme 				key->tp.src = tcp->source;
6571139e241SJarno Rajahalme 				key->tp.dst = tcp->dest;
6581139e241SJarno Rajahalme 				key->tp.flags = TCP_FLAGS_BE16(tcp);
65907148121SJesse Gross 			} else {
66007148121SJesse Gross 				memset(&key->tp, 0, sizeof(key->tp));
661ccb1352eSJesse Gross 			}
662ccb1352eSJesse Gross 		} else if (key->ip.proto == NEXTHDR_UDP) {
663ccb1352eSJesse Gross 			if (udphdr_ok(skb)) {
664ccb1352eSJesse Gross 				struct udphdr *udp = udp_hdr(skb);
6651139e241SJarno Rajahalme 				key->tp.src = udp->source;
6661139e241SJarno Rajahalme 				key->tp.dst = udp->dest;
66707148121SJesse Gross 			} else {
66807148121SJesse Gross 				memset(&key->tp, 0, sizeof(key->tp));
669ccb1352eSJesse Gross 			}
670a175a723SJoe Stringer 		} else if (key->ip.proto == NEXTHDR_SCTP) {
671a175a723SJoe Stringer 			if (sctphdr_ok(skb)) {
672a175a723SJoe Stringer 				struct sctphdr *sctp = sctp_hdr(skb);
6731139e241SJarno Rajahalme 				key->tp.src = sctp->source;
6741139e241SJarno Rajahalme 				key->tp.dst = sctp->dest;
67507148121SJesse Gross 			} else {
67607148121SJesse Gross 				memset(&key->tp, 0, sizeof(key->tp));
677a175a723SJoe Stringer 			}
678ccb1352eSJesse Gross 		} else if (key->ip.proto == NEXTHDR_ICMP) {
679ccb1352eSJesse Gross 			if (icmp6hdr_ok(skb)) {
68003f0d916SAndy Zhou 				error = parse_icmpv6(skb, key, nh_len);
68103f0d916SAndy Zhou 				if (error)
68203f0d916SAndy Zhou 					return error;
68307148121SJesse Gross 			} else {
68407148121SJesse Gross 				memset(&key->tp, 0, sizeof(key->tp));
685ccb1352eSJesse Gross 			}
686ccb1352eSJesse Gross 		}
687ccb1352eSJesse Gross 	}
68803f0d916SAndy Zhou 	return 0;
689ccb1352eSJesse Gross }
69083c8df26SPravin B Shelar 
691971427f3SAndy Zhou int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key)
692971427f3SAndy Zhou {
693971427f3SAndy Zhou 	return key_extract(skb, key);
694971427f3SAndy Zhou }
695971427f3SAndy Zhou 
6961d8fff90SThomas Graf int ovs_flow_key_extract(const struct ip_tunnel_info *tun_info,
6978c8b1b83SPravin B Shelar 			 struct sk_buff *skb, struct sw_flow_key *key)
69883c8df26SPravin B Shelar {
69983c8df26SPravin B Shelar 	/* Extract metadata from packet. */
700f5796684SJesse Gross 	if (tun_info) {
701*00a93babSJiri Benc 		key->tun_proto = ip_tunnel_info_af(tun_info);
7021d8fff90SThomas Graf 		memcpy(&key->tun_key, &tun_info->key, sizeof(key->tun_key));
703f5796684SJesse Gross 
7044c222798SPravin B Shelar 		if (tun_info->options_len) {
705f5796684SJesse Gross 			BUILD_BUG_ON((1 << (sizeof(tun_info->options_len) *
706f5796684SJesse Gross 						   8)) - 1
707f5796684SJesse Gross 					> sizeof(key->tun_opts));
7084c222798SPravin B Shelar 
7094c222798SPravin B Shelar 			ip_tunnel_info_opts_get(TUN_METADATA_OPTS(key, tun_info->options_len),
7104c222798SPravin B Shelar 						tun_info);
711f5796684SJesse Gross 			key->tun_opts_len = tun_info->options_len;
712f5796684SJesse Gross 		} else {
713f5796684SJesse Gross 			key->tun_opts_len = 0;
714f5796684SJesse Gross 		}
715f5796684SJesse Gross 	} else  {
716*00a93babSJiri Benc 		key->tun_proto = 0;
717f5796684SJesse Gross 		key->tun_opts_len = 0;
71807148121SJesse Gross 		memset(&key->tun_key, 0, sizeof(key->tun_key));
719f5796684SJesse Gross 	}
72083c8df26SPravin B Shelar 
72183c8df26SPravin B Shelar 	key->phy.priority = skb->priority;
72283c8df26SPravin B Shelar 	key->phy.in_port = OVS_CB(skb)->input_vport->port_no;
72383c8df26SPravin B Shelar 	key->phy.skb_mark = skb->mark;
7247f8a436eSJoe Stringer 	ovs_ct_fill_key(skb, key);
72507148121SJesse Gross 	key->ovs_flow_hash = 0;
72607148121SJesse Gross 	key->recirc_id = 0;
72707148121SJesse Gross 
72883c8df26SPravin B Shelar 	return key_extract(skb, key);
72983c8df26SPravin B Shelar }
73083c8df26SPravin B Shelar 
731c2ac6673SJoe Stringer int ovs_flow_key_extract_userspace(struct net *net, const struct nlattr *attr,
73283c8df26SPravin B Shelar 				   struct sk_buff *skb,
73305da5898SJarno Rajahalme 				   struct sw_flow_key *key, bool log)
73483c8df26SPravin B Shelar {
73583c8df26SPravin B Shelar 	int err;
73683c8df26SPravin B Shelar 
737b35725a2SPravin B Shelar 	memset(key, 0, OVS_SW_FLOW_KEY_METADATA_SIZE);
738b35725a2SPravin B Shelar 
73983c8df26SPravin B Shelar 	/* Extract metadata from netlink attributes. */
740c2ac6673SJoe Stringer 	err = ovs_nla_get_flow_metadata(net, attr, key, log);
74183c8df26SPravin B Shelar 	if (err)
74283c8df26SPravin B Shelar 		return err;
74383c8df26SPravin B Shelar 
74483c8df26SPravin B Shelar 	return key_extract(skb, key);
74583c8df26SPravin B Shelar }
746