xref: /linux/net/openvswitch/flow.c (revision 5108bbaddc37c1c8583f0cf2562d7d3463cd12cb)
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>
32db74a333SThadeu Lima de Souza Cascardo #include <linux/cpumask.h>
33ccb1352eSJesse Gross #include <linux/if_arp.h>
34ccb1352eSJesse Gross #include <linux/ip.h>
35ccb1352eSJesse Gross #include <linux/ipv6.h>
3625cd9ba0SSimon Horman #include <linux/mpls.h>
37a175a723SJoe Stringer #include <linux/sctp.h>
38e298e505SPravin B Shelar #include <linux/smp.h>
39ccb1352eSJesse Gross #include <linux/tcp.h>
40ccb1352eSJesse Gross #include <linux/udp.h>
41ccb1352eSJesse Gross #include <linux/icmp.h>
42ccb1352eSJesse Gross #include <linux/icmpv6.h>
43ccb1352eSJesse Gross #include <linux/rculist.h>
44ccb1352eSJesse Gross #include <net/ip.h>
457d5437c7SPravin B Shelar #include <net/ip_tunnels.h>
46ccb1352eSJesse Gross #include <net/ipv6.h>
4725cd9ba0SSimon Horman #include <net/mpls.h>
48ccb1352eSJesse Gross #include <net/ndisc.h>
49ccb1352eSJesse Gross 
50a581b96dSPravin B Shelar #include "conntrack.h"
5183c8df26SPravin B Shelar #include "datapath.h"
5283c8df26SPravin B Shelar #include "flow.h"
5383c8df26SPravin B Shelar #include "flow_netlink.h"
54a581b96dSPravin B Shelar #include "vport.h"
5583c8df26SPravin B Shelar 
56e6445719SPravin B Shelar u64 ovs_flow_used_time(unsigned long flow_jiffies)
5703f0d916SAndy Zhou {
58e6445719SPravin B Shelar 	struct timespec cur_ts;
59e6445719SPravin B Shelar 	u64 cur_ms, idle_ms;
6003f0d916SAndy Zhou 
61e6445719SPravin B Shelar 	ktime_get_ts(&cur_ts);
62e6445719SPravin B Shelar 	idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
63e6445719SPravin B Shelar 	cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
64e6445719SPravin B Shelar 		 cur_ts.tv_nsec / NSEC_PER_MSEC;
6503f0d916SAndy Zhou 
66e6445719SPravin B Shelar 	return cur_ms - idle_ms;
6703f0d916SAndy Zhou }
6803f0d916SAndy Zhou 
69df23e9f6SJarno Rajahalme #define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF))
7003f0d916SAndy Zhou 
71ad552007SBen Pfaff void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags,
7212eb18f7SThomas Graf 			   const struct sk_buff *skb)
735828cd9aSAndy Zhou {
74e298e505SPravin B Shelar 	struct flow_stats *stats;
7563e7959cSJarno Rajahalme 	int node = numa_node_id();
76db74a333SThadeu Lima de Souza Cascardo 	int cpu = smp_processor_id();
77df8a39deSJiri Pirko 	int len = skb->len + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
78e6445719SPravin B Shelar 
79db74a333SThadeu Lima de Souza Cascardo 	stats = rcu_dereference(flow->stats[cpu]);
80e298e505SPravin B Shelar 
81db74a333SThadeu Lima de Souza Cascardo 	/* Check if already have CPU-specific stats. */
8263e7959cSJarno Rajahalme 	if (likely(stats)) {
83e298e505SPravin B Shelar 		spin_lock(&stats->lock);
8463e7959cSJarno Rajahalme 		/* Mark if we write on the pre-allocated stats. */
85db74a333SThadeu Lima de Souza Cascardo 		if (cpu == 0 && unlikely(flow->stats_last_writer != cpu))
86db74a333SThadeu Lima de Souza Cascardo 			flow->stats_last_writer = cpu;
8763e7959cSJarno Rajahalme 	} else {
8863e7959cSJarno Rajahalme 		stats = rcu_dereference(flow->stats[0]); /* Pre-allocated. */
8963e7959cSJarno Rajahalme 		spin_lock(&stats->lock);
9063e7959cSJarno Rajahalme 
91db74a333SThadeu Lima de Souza Cascardo 		/* If the current CPU is the only writer on the
9263e7959cSJarno Rajahalme 		 * pre-allocated stats keep using them.
9363e7959cSJarno Rajahalme 		 */
94db74a333SThadeu Lima de Souza Cascardo 		if (unlikely(flow->stats_last_writer != cpu)) {
9563e7959cSJarno Rajahalme 			/* A previous locker may have already allocated the
96db74a333SThadeu Lima de Souza Cascardo 			 * stats, so we need to check again.  If CPU-specific
9763e7959cSJarno Rajahalme 			 * stats were already allocated, we update the pre-
9863e7959cSJarno Rajahalme 			 * allocated stats as we have already locked them.
9963e7959cSJarno Rajahalme 			 */
100db74a333SThadeu Lima de Souza Cascardo 			if (likely(flow->stats_last_writer != -1) &&
101db74a333SThadeu Lima de Souza Cascardo 			    likely(!rcu_access_pointer(flow->stats[cpu]))) {
102db74a333SThadeu Lima de Souza Cascardo 				/* Try to allocate CPU-specific stats. */
10363e7959cSJarno Rajahalme 				struct flow_stats *new_stats;
10463e7959cSJarno Rajahalme 
10563e7959cSJarno Rajahalme 				new_stats =
10663e7959cSJarno Rajahalme 					kmem_cache_alloc_node(flow_stats_cache,
1074167e9b2SDavid Rientjes 							      GFP_NOWAIT |
1084167e9b2SDavid Rientjes 							      __GFP_THISNODE |
1094167e9b2SDavid Rientjes 							      __GFP_NOWARN |
11063e7959cSJarno Rajahalme 							      __GFP_NOMEMALLOC,
11163e7959cSJarno Rajahalme 							      node);
11263e7959cSJarno Rajahalme 				if (likely(new_stats)) {
11363e7959cSJarno Rajahalme 					new_stats->used = jiffies;
11463e7959cSJarno Rajahalme 					new_stats->packet_count = 1;
11524cc59d1SBen Pfaff 					new_stats->byte_count = len;
11663e7959cSJarno Rajahalme 					new_stats->tcp_flags = tcp_flags;
11763e7959cSJarno Rajahalme 					spin_lock_init(&new_stats->lock);
11863e7959cSJarno Rajahalme 
119db74a333SThadeu Lima de Souza Cascardo 					rcu_assign_pointer(flow->stats[cpu],
12063e7959cSJarno Rajahalme 							   new_stats);
12163e7959cSJarno Rajahalme 					goto unlock;
12263e7959cSJarno Rajahalme 				}
12363e7959cSJarno Rajahalme 			}
124db74a333SThadeu Lima de Souza Cascardo 			flow->stats_last_writer = cpu;
12563e7959cSJarno Rajahalme 		}
12663e7959cSJarno Rajahalme 	}
12763e7959cSJarno Rajahalme 
128e298e505SPravin B Shelar 	stats->used = jiffies;
129e298e505SPravin B Shelar 	stats->packet_count++;
13024cc59d1SBen Pfaff 	stats->byte_count += len;
131e298e505SPravin B Shelar 	stats->tcp_flags |= tcp_flags;
13263e7959cSJarno Rajahalme unlock:
133e298e505SPravin B Shelar 	spin_unlock(&stats->lock);
134e298e505SPravin B Shelar }
135e298e505SPravin B Shelar 
13686ec8dbaSJarno Rajahalme /* Must be called with rcu_read_lock or ovs_mutex. */
13786ec8dbaSJarno Rajahalme void ovs_flow_stats_get(const struct sw_flow *flow,
13886ec8dbaSJarno Rajahalme 			struct ovs_flow_stats *ovs_stats,
139e298e505SPravin B Shelar 			unsigned long *used, __be16 *tcp_flags)
140e298e505SPravin B Shelar {
141db74a333SThadeu Lima de Souza Cascardo 	int cpu;
142e298e505SPravin B Shelar 
143e298e505SPravin B Shelar 	*used = 0;
144e298e505SPravin B Shelar 	*tcp_flags = 0;
145e298e505SPravin B Shelar 	memset(ovs_stats, 0, sizeof(*ovs_stats));
146e298e505SPravin B Shelar 
147db74a333SThadeu Lima de Souza Cascardo 	/* We open code this to make sure cpu 0 is always considered */
148db74a333SThadeu Lima de Souza Cascardo 	for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpu_possible_mask)) {
149db74a333SThadeu Lima de Souza Cascardo 		struct flow_stats *stats = rcu_dereference_ovsl(flow->stats[cpu]);
15023dabf88SJarno Rajahalme 
15163e7959cSJarno Rajahalme 		if (stats) {
15263e7959cSJarno Rajahalme 			/* Local CPU may write on non-local stats, so we must
15363e7959cSJarno Rajahalme 			 * block bottom-halves here.
15463e7959cSJarno Rajahalme 			 */
15563e7959cSJarno Rajahalme 			spin_lock_bh(&stats->lock);
15663e7959cSJarno Rajahalme 			if (!*used || time_after(stats->used, *used))
15763e7959cSJarno Rajahalme 				*used = stats->used;
15863e7959cSJarno Rajahalme 			*tcp_flags |= stats->tcp_flags;
15963e7959cSJarno Rajahalme 			ovs_stats->n_packets += stats->packet_count;
16063e7959cSJarno Rajahalme 			ovs_stats->n_bytes += stats->byte_count;
16163e7959cSJarno Rajahalme 			spin_unlock_bh(&stats->lock);
1624f647e0aSFlavio Leitner 		}
163e298e505SPravin B Shelar 	}
164e298e505SPravin B Shelar }
165e298e505SPravin B Shelar 
16686ec8dbaSJarno Rajahalme /* Called with ovs_mutex. */
167e298e505SPravin B Shelar void ovs_flow_stats_clear(struct sw_flow *flow)
168e298e505SPravin B Shelar {
169db74a333SThadeu Lima de Souza Cascardo 	int cpu;
170e298e505SPravin B Shelar 
171db74a333SThadeu Lima de Souza Cascardo 	/* We open code this to make sure cpu 0 is always considered */
172db74a333SThadeu Lima de Souza Cascardo 	for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpu_possible_mask)) {
173db74a333SThadeu Lima de Souza Cascardo 		struct flow_stats *stats = ovsl_dereference(flow->stats[cpu]);
17423dabf88SJarno Rajahalme 
17563e7959cSJarno Rajahalme 		if (stats) {
17663e7959cSJarno Rajahalme 			spin_lock_bh(&stats->lock);
17763e7959cSJarno Rajahalme 			stats->used = 0;
17863e7959cSJarno Rajahalme 			stats->packet_count = 0;
17963e7959cSJarno Rajahalme 			stats->byte_count = 0;
18063e7959cSJarno Rajahalme 			stats->tcp_flags = 0;
18163e7959cSJarno Rajahalme 			spin_unlock_bh(&stats->lock);
18263e7959cSJarno Rajahalme 		}
18363e7959cSJarno Rajahalme 	}
184e298e505SPravin B Shelar }
18503f0d916SAndy Zhou 
186ccb1352eSJesse Gross static int check_header(struct sk_buff *skb, int len)
187ccb1352eSJesse Gross {
188ccb1352eSJesse Gross 	if (unlikely(skb->len < len))
189ccb1352eSJesse Gross 		return -EINVAL;
190ccb1352eSJesse Gross 	if (unlikely(!pskb_may_pull(skb, len)))
191ccb1352eSJesse Gross 		return -ENOMEM;
192ccb1352eSJesse Gross 	return 0;
193ccb1352eSJesse Gross }
194ccb1352eSJesse Gross 
195ccb1352eSJesse Gross static bool arphdr_ok(struct sk_buff *skb)
196ccb1352eSJesse Gross {
197ccb1352eSJesse Gross 	return pskb_may_pull(skb, skb_network_offset(skb) +
198ccb1352eSJesse Gross 				  sizeof(struct arp_eth_header));
199ccb1352eSJesse Gross }
200ccb1352eSJesse Gross 
201ccb1352eSJesse Gross static int check_iphdr(struct sk_buff *skb)
202ccb1352eSJesse Gross {
203ccb1352eSJesse Gross 	unsigned int nh_ofs = skb_network_offset(skb);
204ccb1352eSJesse Gross 	unsigned int ip_len;
205ccb1352eSJesse Gross 	int err;
206ccb1352eSJesse Gross 
207ccb1352eSJesse Gross 	err = check_header(skb, nh_ofs + sizeof(struct iphdr));
208ccb1352eSJesse Gross 	if (unlikely(err))
209ccb1352eSJesse Gross 		return err;
210ccb1352eSJesse Gross 
211ccb1352eSJesse Gross 	ip_len = ip_hdrlen(skb);
212ccb1352eSJesse Gross 	if (unlikely(ip_len < sizeof(struct iphdr) ||
213ccb1352eSJesse Gross 		     skb->len < nh_ofs + ip_len))
214ccb1352eSJesse Gross 		return -EINVAL;
215ccb1352eSJesse Gross 
216ccb1352eSJesse Gross 	skb_set_transport_header(skb, nh_ofs + ip_len);
217ccb1352eSJesse Gross 	return 0;
218ccb1352eSJesse Gross }
219ccb1352eSJesse Gross 
220ccb1352eSJesse Gross static bool tcphdr_ok(struct sk_buff *skb)
221ccb1352eSJesse Gross {
222ccb1352eSJesse Gross 	int th_ofs = skb_transport_offset(skb);
223ccb1352eSJesse Gross 	int tcp_len;
224ccb1352eSJesse Gross 
225ccb1352eSJesse Gross 	if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
226ccb1352eSJesse Gross 		return false;
227ccb1352eSJesse Gross 
228ccb1352eSJesse Gross 	tcp_len = tcp_hdrlen(skb);
229ccb1352eSJesse Gross 	if (unlikely(tcp_len < sizeof(struct tcphdr) ||
230ccb1352eSJesse Gross 		     skb->len < th_ofs + tcp_len))
231ccb1352eSJesse Gross 		return false;
232ccb1352eSJesse Gross 
233ccb1352eSJesse Gross 	return true;
234ccb1352eSJesse Gross }
235ccb1352eSJesse Gross 
236ccb1352eSJesse Gross static bool udphdr_ok(struct sk_buff *skb)
237ccb1352eSJesse Gross {
238ccb1352eSJesse Gross 	return pskb_may_pull(skb, skb_transport_offset(skb) +
239ccb1352eSJesse Gross 				  sizeof(struct udphdr));
240ccb1352eSJesse Gross }
241ccb1352eSJesse Gross 
242a175a723SJoe Stringer static bool sctphdr_ok(struct sk_buff *skb)
243a175a723SJoe Stringer {
244a175a723SJoe Stringer 	return pskb_may_pull(skb, skb_transport_offset(skb) +
245a175a723SJoe Stringer 				  sizeof(struct sctphdr));
246a175a723SJoe Stringer }
247a175a723SJoe Stringer 
248ccb1352eSJesse Gross static bool icmphdr_ok(struct sk_buff *skb)
249ccb1352eSJesse Gross {
250ccb1352eSJesse Gross 	return pskb_may_pull(skb, skb_transport_offset(skb) +
251ccb1352eSJesse Gross 				  sizeof(struct icmphdr));
252ccb1352eSJesse Gross }
253ccb1352eSJesse Gross 
25403f0d916SAndy Zhou static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
255ccb1352eSJesse Gross {
256ccb1352eSJesse Gross 	unsigned int nh_ofs = skb_network_offset(skb);
257ccb1352eSJesse Gross 	unsigned int nh_len;
258ccb1352eSJesse Gross 	int payload_ofs;
259ccb1352eSJesse Gross 	struct ipv6hdr *nh;
260ccb1352eSJesse Gross 	uint8_t nexthdr;
261ccb1352eSJesse Gross 	__be16 frag_off;
262ccb1352eSJesse Gross 	int err;
263ccb1352eSJesse Gross 
264ccb1352eSJesse Gross 	err = check_header(skb, nh_ofs + sizeof(*nh));
265ccb1352eSJesse Gross 	if (unlikely(err))
266ccb1352eSJesse Gross 		return err;
267ccb1352eSJesse Gross 
268ccb1352eSJesse Gross 	nh = ipv6_hdr(skb);
269ccb1352eSJesse Gross 	nexthdr = nh->nexthdr;
270ccb1352eSJesse Gross 	payload_ofs = (u8 *)(nh + 1) - skb->data;
271ccb1352eSJesse Gross 
272ccb1352eSJesse Gross 	key->ip.proto = NEXTHDR_NONE;
273ccb1352eSJesse Gross 	key->ip.tos = ipv6_get_dsfield(nh);
274ccb1352eSJesse Gross 	key->ip.ttl = nh->hop_limit;
275ccb1352eSJesse Gross 	key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
276ccb1352eSJesse Gross 	key->ipv6.addr.src = nh->saddr;
277ccb1352eSJesse Gross 	key->ipv6.addr.dst = nh->daddr;
278ccb1352eSJesse Gross 
279ccb1352eSJesse Gross 	payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
280ccb1352eSJesse Gross 
281ccb1352eSJesse Gross 	if (frag_off) {
282ccb1352eSJesse Gross 		if (frag_off & htons(~0x7))
283ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_LATER;
284ccb1352eSJesse Gross 		else
285ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_FIRST;
28625ef1328SPravin B Shelar 	} else {
28725ef1328SPravin B Shelar 		key->ip.frag = OVS_FRAG_TYPE_NONE;
288ccb1352eSJesse Gross 	}
289ccb1352eSJesse Gross 
290c30da497SSimon Horman 	/* Delayed handling of error in ipv6_skip_exthdr() as it
291c30da497SSimon Horman 	 * always sets frag_off to a valid value which may be
292c30da497SSimon Horman 	 * used to set key->ip.frag above.
293c30da497SSimon Horman 	 */
294c30da497SSimon Horman 	if (unlikely(payload_ofs < 0))
295c30da497SSimon Horman 		return -EPROTO;
296c30da497SSimon Horman 
297ccb1352eSJesse Gross 	nh_len = payload_ofs - nh_ofs;
298ccb1352eSJesse Gross 	skb_set_transport_header(skb, nh_ofs + nh_len);
299ccb1352eSJesse Gross 	key->ip.proto = nexthdr;
300ccb1352eSJesse Gross 	return nh_len;
301ccb1352eSJesse Gross }
302ccb1352eSJesse Gross 
303ccb1352eSJesse Gross static bool icmp6hdr_ok(struct sk_buff *skb)
304ccb1352eSJesse Gross {
305ccb1352eSJesse Gross 	return pskb_may_pull(skb, skb_transport_offset(skb) +
306ccb1352eSJesse Gross 				  sizeof(struct icmp6hdr));
307ccb1352eSJesse Gross }
308ccb1352eSJesse Gross 
309018c1ddaSEric Garver /**
310018c1ddaSEric Garver  * Parse vlan tag from vlan header.
311018c1ddaSEric Garver  * Returns ERROR on memory error.
312018c1ddaSEric Garver  * Returns 0 if it encounters a non-vlan or incomplete packet.
313018c1ddaSEric Garver  * Returns 1 after successfully parsing vlan tag.
314018c1ddaSEric Garver  */
315018c1ddaSEric Garver static int parse_vlan_tag(struct sk_buff *skb, struct vlan_head *key_vh)
316ccb1352eSJesse Gross {
317018c1ddaSEric Garver 	struct vlan_head *vh = (struct vlan_head *)skb->data;
318ccb1352eSJesse Gross 
319018c1ddaSEric Garver 	if (likely(!eth_type_vlan(vh->tpid)))
320ccb1352eSJesse Gross 		return 0;
321ccb1352eSJesse Gross 
322018c1ddaSEric Garver 	if (unlikely(skb->len < sizeof(struct vlan_head) + sizeof(__be16)))
323018c1ddaSEric Garver 		return 0;
324018c1ddaSEric Garver 
325018c1ddaSEric Garver 	if (unlikely(!pskb_may_pull(skb, sizeof(struct vlan_head) +
326ccb1352eSJesse Gross 				 sizeof(__be16))))
327ccb1352eSJesse Gross 		return -ENOMEM;
328ccb1352eSJesse Gross 
329018c1ddaSEric Garver 	vh = (struct vlan_head *)skb->data;
330018c1ddaSEric Garver 	key_vh->tci = vh->tci | htons(VLAN_TAG_PRESENT);
331018c1ddaSEric Garver 	key_vh->tpid = vh->tpid;
332018c1ddaSEric Garver 
333018c1ddaSEric Garver 	__skb_pull(skb, sizeof(struct vlan_head));
334018c1ddaSEric Garver 	return 1;
335018c1ddaSEric Garver }
336018c1ddaSEric Garver 
337*5108bbadSJiri Benc static void clear_vlan(struct sw_flow_key *key)
338018c1ddaSEric Garver {
339018c1ddaSEric Garver 	key->eth.vlan.tci = 0;
340018c1ddaSEric Garver 	key->eth.vlan.tpid = 0;
341018c1ddaSEric Garver 	key->eth.cvlan.tci = 0;
342018c1ddaSEric Garver 	key->eth.cvlan.tpid = 0;
343*5108bbadSJiri Benc }
344*5108bbadSJiri Benc 
345*5108bbadSJiri Benc static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
346*5108bbadSJiri Benc {
347*5108bbadSJiri Benc 	int res;
348018c1ddaSEric Garver 
34920ecf1e4SJiri Benc 	if (skb_vlan_tag_present(skb)) {
350018c1ddaSEric Garver 		key->eth.vlan.tci = htons(skb->vlan_tci);
351018c1ddaSEric Garver 		key->eth.vlan.tpid = skb->vlan_proto;
352018c1ddaSEric Garver 	} else {
353018c1ddaSEric Garver 		/* Parse outer vlan tag in the non-accelerated case. */
354018c1ddaSEric Garver 		res = parse_vlan_tag(skb, &key->eth.vlan);
355018c1ddaSEric Garver 		if (res <= 0)
356018c1ddaSEric Garver 			return res;
357018c1ddaSEric Garver 	}
358018c1ddaSEric Garver 
359018c1ddaSEric Garver 	/* Parse inner vlan tag. */
360018c1ddaSEric Garver 	res = parse_vlan_tag(skb, &key->eth.cvlan);
361018c1ddaSEric Garver 	if (res <= 0)
362018c1ddaSEric Garver 		return res;
363ccb1352eSJesse Gross 
364ccb1352eSJesse Gross 	return 0;
365ccb1352eSJesse Gross }
366ccb1352eSJesse Gross 
367ccb1352eSJesse Gross static __be16 parse_ethertype(struct sk_buff *skb)
368ccb1352eSJesse Gross {
369ccb1352eSJesse Gross 	struct llc_snap_hdr {
370ccb1352eSJesse Gross 		u8  dsap;  /* Always 0xAA */
371ccb1352eSJesse Gross 		u8  ssap;  /* Always 0xAA */
372ccb1352eSJesse Gross 		u8  ctrl;
373ccb1352eSJesse Gross 		u8  oui[3];
374ccb1352eSJesse Gross 		__be16 ethertype;
375ccb1352eSJesse Gross 	};
376ccb1352eSJesse Gross 	struct llc_snap_hdr *llc;
377ccb1352eSJesse Gross 	__be16 proto;
378ccb1352eSJesse Gross 
379ccb1352eSJesse Gross 	proto = *(__be16 *) skb->data;
380ccb1352eSJesse Gross 	__skb_pull(skb, sizeof(__be16));
381ccb1352eSJesse Gross 
3826713fc9bSAlexander Duyck 	if (eth_proto_is_802_3(proto))
383ccb1352eSJesse Gross 		return proto;
384ccb1352eSJesse Gross 
385ccb1352eSJesse Gross 	if (skb->len < sizeof(struct llc_snap_hdr))
386ccb1352eSJesse Gross 		return htons(ETH_P_802_2);
387ccb1352eSJesse Gross 
388ccb1352eSJesse Gross 	if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
389ccb1352eSJesse Gross 		return htons(0);
390ccb1352eSJesse Gross 
391ccb1352eSJesse Gross 	llc = (struct llc_snap_hdr *) skb->data;
392ccb1352eSJesse Gross 	if (llc->dsap != LLC_SAP_SNAP ||
393ccb1352eSJesse Gross 	    llc->ssap != LLC_SAP_SNAP ||
394ccb1352eSJesse Gross 	    (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
395ccb1352eSJesse Gross 		return htons(ETH_P_802_2);
396ccb1352eSJesse Gross 
397ccb1352eSJesse Gross 	__skb_pull(skb, sizeof(struct llc_snap_hdr));
39817b682a0SRich Lane 
3996713fc9bSAlexander Duyck 	if (eth_proto_is_802_3(llc->ethertype))
400ccb1352eSJesse Gross 		return llc->ethertype;
40117b682a0SRich Lane 
40217b682a0SRich Lane 	return htons(ETH_P_802_2);
403ccb1352eSJesse Gross }
404ccb1352eSJesse Gross 
405ccb1352eSJesse Gross static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
40603f0d916SAndy Zhou 			int nh_len)
407ccb1352eSJesse Gross {
408ccb1352eSJesse Gross 	struct icmp6hdr *icmp = icmp6_hdr(skb);
409ccb1352eSJesse Gross 
410ccb1352eSJesse Gross 	/* The ICMPv6 type and code fields use the 16-bit transport port
411ccb1352eSJesse Gross 	 * fields, so we need to store them in 16-bit network byte order.
412ccb1352eSJesse Gross 	 */
4131139e241SJarno Rajahalme 	key->tp.src = htons(icmp->icmp6_type);
4141139e241SJarno Rajahalme 	key->tp.dst = htons(icmp->icmp6_code);
41525ef1328SPravin B Shelar 	memset(&key->ipv6.nd, 0, sizeof(key->ipv6.nd));
416ccb1352eSJesse Gross 
417ccb1352eSJesse Gross 	if (icmp->icmp6_code == 0 &&
418ccb1352eSJesse Gross 	    (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
419ccb1352eSJesse Gross 	     icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
420ccb1352eSJesse Gross 		int icmp_len = skb->len - skb_transport_offset(skb);
421ccb1352eSJesse Gross 		struct nd_msg *nd;
422ccb1352eSJesse Gross 		int offset;
423ccb1352eSJesse Gross 
424ccb1352eSJesse Gross 		/* In order to process neighbor discovery options, we need the
425ccb1352eSJesse Gross 		 * entire packet.
426ccb1352eSJesse Gross 		 */
427ccb1352eSJesse Gross 		if (unlikely(icmp_len < sizeof(*nd)))
42803f0d916SAndy Zhou 			return 0;
42903f0d916SAndy Zhou 
43003f0d916SAndy Zhou 		if (unlikely(skb_linearize(skb)))
43103f0d916SAndy Zhou 			return -ENOMEM;
432ccb1352eSJesse Gross 
433ccb1352eSJesse Gross 		nd = (struct nd_msg *)skb_transport_header(skb);
434ccb1352eSJesse Gross 		key->ipv6.nd.target = nd->target;
435ccb1352eSJesse Gross 
436ccb1352eSJesse Gross 		icmp_len -= sizeof(*nd);
437ccb1352eSJesse Gross 		offset = 0;
438ccb1352eSJesse Gross 		while (icmp_len >= 8) {
439ccb1352eSJesse Gross 			struct nd_opt_hdr *nd_opt =
440ccb1352eSJesse Gross 				 (struct nd_opt_hdr *)(nd->opt + offset);
441ccb1352eSJesse Gross 			int opt_len = nd_opt->nd_opt_len * 8;
442ccb1352eSJesse Gross 
443ccb1352eSJesse Gross 			if (unlikely(!opt_len || opt_len > icmp_len))
44403f0d916SAndy Zhou 				return 0;
445ccb1352eSJesse Gross 
446ccb1352eSJesse Gross 			/* Store the link layer address if the appropriate
447ccb1352eSJesse Gross 			 * option is provided.  It is considered an error if
448ccb1352eSJesse Gross 			 * the same link layer option is specified twice.
449ccb1352eSJesse Gross 			 */
450ccb1352eSJesse Gross 			if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
451ccb1352eSJesse Gross 			    && opt_len == 8) {
452ccb1352eSJesse Gross 				if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
453ccb1352eSJesse Gross 					goto invalid;
4548c63ff09SJoe Perches 				ether_addr_copy(key->ipv6.nd.sll,
4558c63ff09SJoe Perches 						&nd->opt[offset+sizeof(*nd_opt)]);
456ccb1352eSJesse Gross 			} else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
457ccb1352eSJesse Gross 				   && opt_len == 8) {
458ccb1352eSJesse Gross 				if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
459ccb1352eSJesse Gross 					goto invalid;
4608c63ff09SJoe Perches 				ether_addr_copy(key->ipv6.nd.tll,
4618c63ff09SJoe Perches 						&nd->opt[offset+sizeof(*nd_opt)]);
462ccb1352eSJesse Gross 			}
463ccb1352eSJesse Gross 
464ccb1352eSJesse Gross 			icmp_len -= opt_len;
465ccb1352eSJesse Gross 			offset += opt_len;
466ccb1352eSJesse Gross 		}
467ccb1352eSJesse Gross 	}
468ccb1352eSJesse Gross 
46903f0d916SAndy Zhou 	return 0;
470ccb1352eSJesse Gross 
471ccb1352eSJesse Gross invalid:
472ccb1352eSJesse Gross 	memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
473ccb1352eSJesse Gross 	memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
474ccb1352eSJesse Gross 	memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
475ccb1352eSJesse Gross 
47603f0d916SAndy Zhou 	return 0;
477ccb1352eSJesse Gross }
478ccb1352eSJesse Gross 
479ccb1352eSJesse Gross /**
48083c8df26SPravin B Shelar  * key_extract - extracts a flow key from an Ethernet frame.
481ccb1352eSJesse Gross  * @skb: sk_buff that contains the frame, with skb->data pointing to the
482ccb1352eSJesse Gross  * Ethernet header
483ccb1352eSJesse Gross  * @key: output flow key
484ccb1352eSJesse Gross  *
485ccb1352eSJesse Gross  * The caller must ensure that skb->len >= ETH_HLEN.
486ccb1352eSJesse Gross  *
487ccb1352eSJesse Gross  * Returns 0 if successful, otherwise a negative errno value.
488ccb1352eSJesse Gross  *
489*5108bbadSJiri Benc  * Initializes @skb header fields as follows:
490ccb1352eSJesse Gross  *
491*5108bbadSJiri Benc  *    - skb->mac_header: the L2 header.
492ccb1352eSJesse Gross  *
493*5108bbadSJiri Benc  *    - skb->network_header: just past the L2 header, or just past the
494*5108bbadSJiri Benc  *      VLAN header, to the first byte of the L2 payload.
495ccb1352eSJesse Gross  *
49634d94f21SLorand Jakab  *    - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
497ccb1352eSJesse Gross  *      on output, then just past the IP header, if one is present and
498ccb1352eSJesse Gross  *      of a correct length, otherwise the same as skb->network_header.
49934d94f21SLorand Jakab  *      For other key->eth.type values it is left untouched.
500*5108bbadSJiri Benc  *
501*5108bbadSJiri Benc  *    - skb->protocol: the type of the data starting at skb->network_header.
502*5108bbadSJiri Benc  *      Equals to key->eth.type.
503ccb1352eSJesse Gross  */
50483c8df26SPravin B Shelar static int key_extract(struct sk_buff *skb, struct sw_flow_key *key)
505ccb1352eSJesse Gross {
50603f0d916SAndy Zhou 	int error;
507ccb1352eSJesse Gross 	struct ethhdr *eth;
508ccb1352eSJesse Gross 
509f5796684SJesse Gross 	/* Flags are always used as part of stats */
510f5796684SJesse Gross 	key->tp.flags = 0;
511f5796684SJesse Gross 
512ccb1352eSJesse Gross 	skb_reset_mac_header(skb);
513ccb1352eSJesse Gross 
514*5108bbadSJiri Benc 	/* Link layer. */
515*5108bbadSJiri Benc 	clear_vlan(key);
516*5108bbadSJiri Benc 	if (key->mac_proto == MAC_PROTO_NONE) {
517*5108bbadSJiri Benc 		if (unlikely(eth_type_vlan(skb->protocol)))
518*5108bbadSJiri Benc 			return -EINVAL;
519*5108bbadSJiri Benc 
520*5108bbadSJiri Benc 		skb_reset_network_header(skb);
521*5108bbadSJiri Benc 	} else {
522ccb1352eSJesse Gross 		eth = eth_hdr(skb);
5238c63ff09SJoe Perches 		ether_addr_copy(key->eth.src, eth->h_source);
5248c63ff09SJoe Perches 		ether_addr_copy(key->eth.dst, eth->h_dest);
525ccb1352eSJesse Gross 
526ccb1352eSJesse Gross 		__skb_pull(skb, 2 * ETH_ALEN);
527b34df5e8SPravin B Shelar 		/* We are going to push all headers that we pull, so no need to
528b34df5e8SPravin B Shelar 		* update skb->csum here.
529b34df5e8SPravin B Shelar 		*/
530ccb1352eSJesse Gross 
531ccb1352eSJesse Gross 		if (unlikely(parse_vlan(skb, key)))
532ccb1352eSJesse Gross 			return -ENOMEM;
533ccb1352eSJesse Gross 
534*5108bbadSJiri Benc 		skb->protocol = parse_ethertype(skb);
535*5108bbadSJiri Benc 		if (unlikely(skb->protocol == htons(0)))
536ccb1352eSJesse Gross 			return -ENOMEM;
537ccb1352eSJesse Gross 
538ccb1352eSJesse Gross 		skb_reset_network_header(skb);
539ccb1352eSJesse Gross 		__skb_push(skb, skb->data - skb_mac_header(skb));
540*5108bbadSJiri Benc 	}
541*5108bbadSJiri Benc 	skb_reset_mac_len(skb);
542*5108bbadSJiri Benc 	key->eth.type = skb->protocol;
543ccb1352eSJesse Gross 
544ccb1352eSJesse Gross 	/* Network layer. */
545ccb1352eSJesse Gross 	if (key->eth.type == htons(ETH_P_IP)) {
546ccb1352eSJesse Gross 		struct iphdr *nh;
547ccb1352eSJesse Gross 		__be16 offset;
548ccb1352eSJesse Gross 
549ccb1352eSJesse Gross 		error = check_iphdr(skb);
550ccb1352eSJesse Gross 		if (unlikely(error)) {
55107148121SJesse Gross 			memset(&key->ip, 0, sizeof(key->ip));
55207148121SJesse Gross 			memset(&key->ipv4, 0, sizeof(key->ipv4));
553ccb1352eSJesse Gross 			if (error == -EINVAL) {
554ccb1352eSJesse Gross 				skb->transport_header = skb->network_header;
555ccb1352eSJesse Gross 				error = 0;
556ccb1352eSJesse Gross 			}
55703f0d916SAndy Zhou 			return error;
558ccb1352eSJesse Gross 		}
559ccb1352eSJesse Gross 
560ccb1352eSJesse Gross 		nh = ip_hdr(skb);
561ccb1352eSJesse Gross 		key->ipv4.addr.src = nh->saddr;
562ccb1352eSJesse Gross 		key->ipv4.addr.dst = nh->daddr;
563ccb1352eSJesse Gross 
564ccb1352eSJesse Gross 		key->ip.proto = nh->protocol;
565ccb1352eSJesse Gross 		key->ip.tos = nh->tos;
566ccb1352eSJesse Gross 		key->ip.ttl = nh->ttl;
567ccb1352eSJesse Gross 
568ccb1352eSJesse Gross 		offset = nh->frag_off & htons(IP_OFFSET);
569ccb1352eSJesse Gross 		if (offset) {
570ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_LATER;
57103f0d916SAndy Zhou 			return 0;
572ccb1352eSJesse Gross 		}
573ccb1352eSJesse Gross 		if (nh->frag_off & htons(IP_MF) ||
574ccb1352eSJesse Gross 			skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
575ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_FIRST;
57607148121SJesse Gross 		else
57707148121SJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_NONE;
578ccb1352eSJesse Gross 
579ccb1352eSJesse Gross 		/* Transport layer. */
580ccb1352eSJesse Gross 		if (key->ip.proto == IPPROTO_TCP) {
581ccb1352eSJesse Gross 			if (tcphdr_ok(skb)) {
582ccb1352eSJesse Gross 				struct tcphdr *tcp = tcp_hdr(skb);
5831139e241SJarno Rajahalme 				key->tp.src = tcp->source;
5841139e241SJarno Rajahalme 				key->tp.dst = tcp->dest;
5851139e241SJarno Rajahalme 				key->tp.flags = TCP_FLAGS_BE16(tcp);
58607148121SJesse Gross 			} else {
58707148121SJesse Gross 				memset(&key->tp, 0, sizeof(key->tp));
588ccb1352eSJesse Gross 			}
58907148121SJesse Gross 
590ccb1352eSJesse Gross 		} else if (key->ip.proto == IPPROTO_UDP) {
591ccb1352eSJesse Gross 			if (udphdr_ok(skb)) {
592ccb1352eSJesse Gross 				struct udphdr *udp = udp_hdr(skb);
5931139e241SJarno Rajahalme 				key->tp.src = udp->source;
5941139e241SJarno Rajahalme 				key->tp.dst = udp->dest;
59507148121SJesse Gross 			} else {
59607148121SJesse Gross 				memset(&key->tp, 0, sizeof(key->tp));
597ccb1352eSJesse Gross 			}
598a175a723SJoe Stringer 		} else if (key->ip.proto == IPPROTO_SCTP) {
599a175a723SJoe Stringer 			if (sctphdr_ok(skb)) {
600a175a723SJoe Stringer 				struct sctphdr *sctp = sctp_hdr(skb);
6011139e241SJarno Rajahalme 				key->tp.src = sctp->source;
6021139e241SJarno Rajahalme 				key->tp.dst = sctp->dest;
60307148121SJesse Gross 			} else {
60407148121SJesse Gross 				memset(&key->tp, 0, sizeof(key->tp));
605a175a723SJoe Stringer 			}
606ccb1352eSJesse Gross 		} else if (key->ip.proto == IPPROTO_ICMP) {
607ccb1352eSJesse Gross 			if (icmphdr_ok(skb)) {
608ccb1352eSJesse Gross 				struct icmphdr *icmp = icmp_hdr(skb);
609ccb1352eSJesse Gross 				/* The ICMP type and code fields use the 16-bit
610ccb1352eSJesse Gross 				 * transport port fields, so we need to store
611ccb1352eSJesse Gross 				 * them in 16-bit network byte order. */
6121139e241SJarno Rajahalme 				key->tp.src = htons(icmp->type);
6131139e241SJarno Rajahalme 				key->tp.dst = htons(icmp->code);
61407148121SJesse Gross 			} else {
61507148121SJesse Gross 				memset(&key->tp, 0, sizeof(key->tp));
616ccb1352eSJesse Gross 			}
617ccb1352eSJesse Gross 		}
618ccb1352eSJesse Gross 
61907148121SJesse Gross 	} else if (key->eth.type == htons(ETH_P_ARP) ||
62007148121SJesse Gross 		   key->eth.type == htons(ETH_P_RARP)) {
621ccb1352eSJesse Gross 		struct arp_eth_header *arp;
622389f4894SLi RongQing 		bool arp_available = arphdr_ok(skb);
623ccb1352eSJesse Gross 
624ccb1352eSJesse Gross 		arp = (struct arp_eth_header *)skb_network_header(skb);
625ccb1352eSJesse Gross 
626389f4894SLi RongQing 		if (arp_available &&
62707148121SJesse Gross 		    arp->ar_hrd == htons(ARPHRD_ETHER) &&
62807148121SJesse Gross 		    arp->ar_pro == htons(ETH_P_IP) &&
62907148121SJesse Gross 		    arp->ar_hln == ETH_ALEN &&
63007148121SJesse Gross 		    arp->ar_pln == 4) {
631ccb1352eSJesse Gross 
632ccb1352eSJesse Gross 			/* We only match on the lower 8 bits of the opcode. */
633ccb1352eSJesse Gross 			if (ntohs(arp->ar_op) <= 0xff)
634ccb1352eSJesse Gross 				key->ip.proto = ntohs(arp->ar_op);
63507148121SJesse Gross 			else
63607148121SJesse Gross 				key->ip.proto = 0;
63707148121SJesse Gross 
638ccb1352eSJesse Gross 			memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
639ccb1352eSJesse Gross 			memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
6408c63ff09SJoe Perches 			ether_addr_copy(key->ipv4.arp.sha, arp->ar_sha);
6418c63ff09SJoe Perches 			ether_addr_copy(key->ipv4.arp.tha, arp->ar_tha);
64207148121SJesse Gross 		} else {
64307148121SJesse Gross 			memset(&key->ip, 0, sizeof(key->ip));
64407148121SJesse Gross 			memset(&key->ipv4, 0, sizeof(key->ipv4));
645ccb1352eSJesse Gross 		}
64625cd9ba0SSimon Horman 	} else if (eth_p_mpls(key->eth.type)) {
64725cd9ba0SSimon Horman 		size_t stack_len = MPLS_HLEN;
64825cd9ba0SSimon Horman 
649f7d49bceSJiri Benc 		skb_set_inner_network_header(skb, skb->mac_len);
65025cd9ba0SSimon Horman 		while (1) {
65125cd9ba0SSimon Horman 			__be32 lse;
65225cd9ba0SSimon Horman 
65325cd9ba0SSimon Horman 			error = check_header(skb, skb->mac_len + stack_len);
65425cd9ba0SSimon Horman 			if (unlikely(error))
65525cd9ba0SSimon Horman 				return 0;
65625cd9ba0SSimon Horman 
657f7d49bceSJiri Benc 			memcpy(&lse, skb_inner_network_header(skb), MPLS_HLEN);
65825cd9ba0SSimon Horman 
65925cd9ba0SSimon Horman 			if (stack_len == MPLS_HLEN)
66025cd9ba0SSimon Horman 				memcpy(&key->mpls.top_lse, &lse, MPLS_HLEN);
66125cd9ba0SSimon Horman 
662f7d49bceSJiri Benc 			skb_set_inner_network_header(skb, skb->mac_len + stack_len);
66325cd9ba0SSimon Horman 			if (lse & htonl(MPLS_LS_S_MASK))
66425cd9ba0SSimon Horman 				break;
66525cd9ba0SSimon Horman 
66625cd9ba0SSimon Horman 			stack_len += MPLS_HLEN;
66725cd9ba0SSimon Horman 		}
668ccb1352eSJesse Gross 	} else if (key->eth.type == htons(ETH_P_IPV6)) {
669ccb1352eSJesse Gross 		int nh_len;             /* IPv6 Header + Extensions */
670ccb1352eSJesse Gross 
67103f0d916SAndy Zhou 		nh_len = parse_ipv6hdr(skb, key);
672ccb1352eSJesse Gross 		if (unlikely(nh_len < 0)) {
673c30da497SSimon Horman 			switch (nh_len) {
674c30da497SSimon Horman 			case -EINVAL:
67507148121SJesse Gross 				memset(&key->ip, 0, sizeof(key->ip));
67607148121SJesse Gross 				memset(&key->ipv6.addr, 0, sizeof(key->ipv6.addr));
677c30da497SSimon Horman 				/* fall-through */
678c30da497SSimon Horman 			case -EPROTO:
679ccb1352eSJesse Gross 				skb->transport_header = skb->network_header;
68003f0d916SAndy Zhou 				error = 0;
681c30da497SSimon Horman 				break;
682c30da497SSimon Horman 			default:
683ccb1352eSJesse Gross 				error = nh_len;
68403f0d916SAndy Zhou 			}
68503f0d916SAndy Zhou 			return error;
686ccb1352eSJesse Gross 		}
687ccb1352eSJesse Gross 
688ccb1352eSJesse Gross 		if (key->ip.frag == OVS_FRAG_TYPE_LATER)
68903f0d916SAndy Zhou 			return 0;
690ccb1352eSJesse Gross 		if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
691ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_FIRST;
692ccb1352eSJesse Gross 
693ccb1352eSJesse Gross 		/* Transport layer. */
694ccb1352eSJesse Gross 		if (key->ip.proto == NEXTHDR_TCP) {
695ccb1352eSJesse Gross 			if (tcphdr_ok(skb)) {
696ccb1352eSJesse Gross 				struct tcphdr *tcp = tcp_hdr(skb);
6971139e241SJarno Rajahalme 				key->tp.src = tcp->source;
6981139e241SJarno Rajahalme 				key->tp.dst = tcp->dest;
6991139e241SJarno Rajahalme 				key->tp.flags = TCP_FLAGS_BE16(tcp);
70007148121SJesse Gross 			} else {
70107148121SJesse Gross 				memset(&key->tp, 0, sizeof(key->tp));
702ccb1352eSJesse Gross 			}
703ccb1352eSJesse Gross 		} else if (key->ip.proto == NEXTHDR_UDP) {
704ccb1352eSJesse Gross 			if (udphdr_ok(skb)) {
705ccb1352eSJesse Gross 				struct udphdr *udp = udp_hdr(skb);
7061139e241SJarno Rajahalme 				key->tp.src = udp->source;
7071139e241SJarno Rajahalme 				key->tp.dst = udp->dest;
70807148121SJesse Gross 			} else {
70907148121SJesse Gross 				memset(&key->tp, 0, sizeof(key->tp));
710ccb1352eSJesse Gross 			}
711a175a723SJoe Stringer 		} else if (key->ip.proto == NEXTHDR_SCTP) {
712a175a723SJoe Stringer 			if (sctphdr_ok(skb)) {
713a175a723SJoe Stringer 				struct sctphdr *sctp = sctp_hdr(skb);
7141139e241SJarno Rajahalme 				key->tp.src = sctp->source;
7151139e241SJarno Rajahalme 				key->tp.dst = sctp->dest;
71607148121SJesse Gross 			} else {
71707148121SJesse Gross 				memset(&key->tp, 0, sizeof(key->tp));
718a175a723SJoe Stringer 			}
719ccb1352eSJesse Gross 		} else if (key->ip.proto == NEXTHDR_ICMP) {
720ccb1352eSJesse Gross 			if (icmp6hdr_ok(skb)) {
72103f0d916SAndy Zhou 				error = parse_icmpv6(skb, key, nh_len);
72203f0d916SAndy Zhou 				if (error)
72303f0d916SAndy Zhou 					return error;
72407148121SJesse Gross 			} else {
72507148121SJesse Gross 				memset(&key->tp, 0, sizeof(key->tp));
726ccb1352eSJesse Gross 			}
727ccb1352eSJesse Gross 		}
728ccb1352eSJesse Gross 	}
72903f0d916SAndy Zhou 	return 0;
730ccb1352eSJesse Gross }
73183c8df26SPravin B Shelar 
732971427f3SAndy Zhou int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key)
733971427f3SAndy Zhou {
734971427f3SAndy Zhou 	return key_extract(skb, key);
735971427f3SAndy Zhou }
736971427f3SAndy Zhou 
737*5108bbadSJiri Benc static int key_extract_mac_proto(struct sk_buff *skb)
738*5108bbadSJiri Benc {
739*5108bbadSJiri Benc 	switch (skb->dev->type) {
740*5108bbadSJiri Benc 	case ARPHRD_ETHER:
741*5108bbadSJiri Benc 		return MAC_PROTO_ETHERNET;
742*5108bbadSJiri Benc 	case ARPHRD_NONE:
743*5108bbadSJiri Benc 		if (skb->protocol == htons(ETH_P_TEB))
744*5108bbadSJiri Benc 			return MAC_PROTO_ETHERNET;
745*5108bbadSJiri Benc 		return MAC_PROTO_NONE;
746*5108bbadSJiri Benc 	}
747*5108bbadSJiri Benc 	WARN_ON_ONCE(1);
748*5108bbadSJiri Benc 	return -EINVAL;
749*5108bbadSJiri Benc }
750*5108bbadSJiri Benc 
7511d8fff90SThomas Graf int ovs_flow_key_extract(const struct ip_tunnel_info *tun_info,
7528c8b1b83SPravin B Shelar 			 struct sk_buff *skb, struct sw_flow_key *key)
75383c8df26SPravin B Shelar {
754*5108bbadSJiri Benc 	int res;
755*5108bbadSJiri Benc 
75683c8df26SPravin B Shelar 	/* Extract metadata from packet. */
757f5796684SJesse Gross 	if (tun_info) {
75800a93babSJiri Benc 		key->tun_proto = ip_tunnel_info_af(tun_info);
7591d8fff90SThomas Graf 		memcpy(&key->tun_key, &tun_info->key, sizeof(key->tun_key));
760f5796684SJesse Gross 
7614c222798SPravin B Shelar 		if (tun_info->options_len) {
762f5796684SJesse Gross 			BUILD_BUG_ON((1 << (sizeof(tun_info->options_len) *
763f5796684SJesse Gross 						   8)) - 1
764f5796684SJesse Gross 					> sizeof(key->tun_opts));
7654c222798SPravin B Shelar 
7664c222798SPravin B Shelar 			ip_tunnel_info_opts_get(TUN_METADATA_OPTS(key, tun_info->options_len),
7674c222798SPravin B Shelar 						tun_info);
768f5796684SJesse Gross 			key->tun_opts_len = tun_info->options_len;
769f5796684SJesse Gross 		} else {
770f5796684SJesse Gross 			key->tun_opts_len = 0;
771f5796684SJesse Gross 		}
772f5796684SJesse Gross 	} else  {
77300a93babSJiri Benc 		key->tun_proto = 0;
774f5796684SJesse Gross 		key->tun_opts_len = 0;
77507148121SJesse Gross 		memset(&key->tun_key, 0, sizeof(key->tun_key));
776f5796684SJesse Gross 	}
77783c8df26SPravin B Shelar 
77883c8df26SPravin B Shelar 	key->phy.priority = skb->priority;
77983c8df26SPravin B Shelar 	key->phy.in_port = OVS_CB(skb)->input_vport->port_no;
78083c8df26SPravin B Shelar 	key->phy.skb_mark = skb->mark;
7817f8a436eSJoe Stringer 	ovs_ct_fill_key(skb, key);
78207148121SJesse Gross 	key->ovs_flow_hash = 0;
783*5108bbadSJiri Benc 	res = key_extract_mac_proto(skb);
784*5108bbadSJiri Benc 	if (res < 0)
785*5108bbadSJiri Benc 		return res;
786*5108bbadSJiri Benc 	key->mac_proto = res;
78707148121SJesse Gross 	key->recirc_id = 0;
78807148121SJesse Gross 
78983c8df26SPravin B Shelar 	return key_extract(skb, key);
79083c8df26SPravin B Shelar }
79183c8df26SPravin B Shelar 
792c2ac6673SJoe Stringer int ovs_flow_key_extract_userspace(struct net *net, const struct nlattr *attr,
79383c8df26SPravin B Shelar 				   struct sk_buff *skb,
79405da5898SJarno Rajahalme 				   struct sw_flow_key *key, bool log)
79583c8df26SPravin B Shelar {
79683c8df26SPravin B Shelar 	int err;
79783c8df26SPravin B Shelar 
79883c8df26SPravin B Shelar 	/* Extract metadata from netlink attributes. */
799c2ac6673SJoe Stringer 	err = ovs_nla_get_flow_metadata(net, attr, key, log);
80083c8df26SPravin B Shelar 	if (err)
80183c8df26SPravin B Shelar 		return err;
80283c8df26SPravin B Shelar 
803*5108bbadSJiri Benc 	if (ovs_key_mac_proto(key) == MAC_PROTO_NONE) {
804*5108bbadSJiri Benc 		/* key_extract assumes that skb->protocol is set-up for
805*5108bbadSJiri Benc 		 * layer 3 packets which is the case for other callers,
806*5108bbadSJiri Benc 		 * in particular packets recieved from the network stack.
807*5108bbadSJiri Benc 		 * Here the correct value can be set from the metadata
808*5108bbadSJiri Benc 		 * extracted above.
809*5108bbadSJiri Benc 		 */
810*5108bbadSJiri Benc 		skb->protocol = key->eth.type;
811*5108bbadSJiri Benc 	} else {
812*5108bbadSJiri Benc 		struct ethhdr *eth;
813*5108bbadSJiri Benc 
814*5108bbadSJiri Benc 		skb_reset_mac_header(skb);
815*5108bbadSJiri Benc 		eth = eth_hdr(skb);
816*5108bbadSJiri Benc 
817*5108bbadSJiri Benc 		/* Normally, setting the skb 'protocol' field would be
818*5108bbadSJiri Benc 		 * handled by a call to eth_type_trans(), but it assumes
819*5108bbadSJiri Benc 		 * there's a sending device, which we may not have.
820*5108bbadSJiri Benc 		 */
821*5108bbadSJiri Benc 		if (eth_proto_is_802_3(eth->h_proto))
822*5108bbadSJiri Benc 			skb->protocol = eth->h_proto;
823*5108bbadSJiri Benc 		else
824*5108bbadSJiri Benc 			skb->protocol = htons(ETH_P_802_2);
825*5108bbadSJiri Benc 	}
826*5108bbadSJiri Benc 
82783c8df26SPravin B Shelar 	return key_extract(skb, key);
82883c8df26SPravin B Shelar }
829