xref: /linux/net/openvswitch/flow.c (revision 86ec8dbae27e5fa2b5d54f10f77286d9ef55732a)
1ccb1352eSJesse Gross /*
203f0d916SAndy Zhou  * Copyright (c) 2007-2013 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 "flow.h"
20ccb1352eSJesse Gross #include "datapath.h"
21ccb1352eSJesse Gross #include <linux/uaccess.h>
22ccb1352eSJesse Gross #include <linux/netdevice.h>
23ccb1352eSJesse Gross #include <linux/etherdevice.h>
24ccb1352eSJesse Gross #include <linux/if_ether.h>
25ccb1352eSJesse Gross #include <linux/if_vlan.h>
26ccb1352eSJesse Gross #include <net/llc_pdu.h>
27ccb1352eSJesse Gross #include <linux/kernel.h>
28ccb1352eSJesse Gross #include <linux/jhash.h>
29ccb1352eSJesse Gross #include <linux/jiffies.h>
30ccb1352eSJesse Gross #include <linux/llc.h>
31ccb1352eSJesse Gross #include <linux/module.h>
32ccb1352eSJesse Gross #include <linux/in.h>
33ccb1352eSJesse Gross #include <linux/rcupdate.h>
34ccb1352eSJesse Gross #include <linux/if_arp.h>
35ccb1352eSJesse Gross #include <linux/ip.h>
36ccb1352eSJesse Gross #include <linux/ipv6.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>
47ccb1352eSJesse Gross #include <net/ndisc.h>
48ccb1352eSJesse Gross 
49e6445719SPravin B Shelar u64 ovs_flow_used_time(unsigned long flow_jiffies)
5003f0d916SAndy Zhou {
51e6445719SPravin B Shelar 	struct timespec cur_ts;
52e6445719SPravin B Shelar 	u64 cur_ms, idle_ms;
5303f0d916SAndy Zhou 
54e6445719SPravin B Shelar 	ktime_get_ts(&cur_ts);
55e6445719SPravin B Shelar 	idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
56e6445719SPravin B Shelar 	cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
57e6445719SPravin B Shelar 		 cur_ts.tv_nsec / NSEC_PER_MSEC;
5803f0d916SAndy Zhou 
59e6445719SPravin B Shelar 	return cur_ms - idle_ms;
6003f0d916SAndy Zhou }
6103f0d916SAndy Zhou 
62df23e9f6SJarno Rajahalme #define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF))
6303f0d916SAndy Zhou 
64e298e505SPravin B Shelar void ovs_flow_stats_update(struct sw_flow *flow, struct sk_buff *skb)
655828cd9aSAndy Zhou {
66e298e505SPravin B Shelar 	struct flow_stats *stats;
671139e241SJarno Rajahalme 	__be16 tcp_flags = flow->key.tp.flags;
6863e7959cSJarno Rajahalme 	int node = numa_node_id();
69e6445719SPravin B Shelar 
7063e7959cSJarno Rajahalme 	stats = rcu_dereference(flow->stats[node]);
71e298e505SPravin B Shelar 
7263e7959cSJarno Rajahalme 	/* Check if already have node-specific stats. */
7363e7959cSJarno Rajahalme 	if (likely(stats)) {
74e298e505SPravin B Shelar 		spin_lock(&stats->lock);
7563e7959cSJarno Rajahalme 		/* Mark if we write on the pre-allocated stats. */
7663e7959cSJarno Rajahalme 		if (node == 0 && unlikely(flow->stats_last_writer != node))
7763e7959cSJarno Rajahalme 			flow->stats_last_writer = node;
7863e7959cSJarno Rajahalme 	} else {
7963e7959cSJarno Rajahalme 		stats = rcu_dereference(flow->stats[0]); /* Pre-allocated. */
8063e7959cSJarno Rajahalme 		spin_lock(&stats->lock);
8163e7959cSJarno Rajahalme 
8263e7959cSJarno Rajahalme 		/* If the current NUMA-node is the only writer on the
8363e7959cSJarno Rajahalme 		 * pre-allocated stats keep using them.
8463e7959cSJarno Rajahalme 		 */
8563e7959cSJarno Rajahalme 		if (unlikely(flow->stats_last_writer != node)) {
8663e7959cSJarno Rajahalme 			/* A previous locker may have already allocated the
8763e7959cSJarno Rajahalme 			 * stats, so we need to check again.  If node-specific
8863e7959cSJarno Rajahalme 			 * stats were already allocated, we update the pre-
8963e7959cSJarno Rajahalme 			 * allocated stats as we have already locked them.
9063e7959cSJarno Rajahalme 			 */
9163e7959cSJarno Rajahalme 			if (likely(flow->stats_last_writer != NUMA_NO_NODE)
9263e7959cSJarno Rajahalme 			    && likely(!rcu_dereference(flow->stats[node]))) {
9363e7959cSJarno Rajahalme 				/* Try to allocate node-specific stats. */
9463e7959cSJarno Rajahalme 				struct flow_stats *new_stats;
9563e7959cSJarno Rajahalme 
9663e7959cSJarno Rajahalme 				new_stats =
9763e7959cSJarno Rajahalme 					kmem_cache_alloc_node(flow_stats_cache,
9863e7959cSJarno Rajahalme 							      GFP_THISNODE |
9963e7959cSJarno Rajahalme 							      __GFP_NOMEMALLOC,
10063e7959cSJarno Rajahalme 							      node);
10163e7959cSJarno Rajahalme 				if (likely(new_stats)) {
10263e7959cSJarno Rajahalme 					new_stats->used = jiffies;
10363e7959cSJarno Rajahalme 					new_stats->packet_count = 1;
10463e7959cSJarno Rajahalme 					new_stats->byte_count = skb->len;
10563e7959cSJarno Rajahalme 					new_stats->tcp_flags = tcp_flags;
10663e7959cSJarno Rajahalme 					spin_lock_init(&new_stats->lock);
10763e7959cSJarno Rajahalme 
10863e7959cSJarno Rajahalme 					rcu_assign_pointer(flow->stats[node],
10963e7959cSJarno Rajahalme 							   new_stats);
11063e7959cSJarno Rajahalme 					goto unlock;
11163e7959cSJarno Rajahalme 				}
11263e7959cSJarno Rajahalme 			}
11363e7959cSJarno Rajahalme 			flow->stats_last_writer = node;
11463e7959cSJarno Rajahalme 		}
11563e7959cSJarno Rajahalme 	}
11663e7959cSJarno Rajahalme 
117e298e505SPravin B Shelar 	stats->used = jiffies;
118e298e505SPravin B Shelar 	stats->packet_count++;
119e298e505SPravin B Shelar 	stats->byte_count += skb->len;
120e298e505SPravin B Shelar 	stats->tcp_flags |= tcp_flags;
12163e7959cSJarno Rajahalme unlock:
122e298e505SPravin B Shelar 	spin_unlock(&stats->lock);
123e298e505SPravin B Shelar }
124e298e505SPravin B Shelar 
125*86ec8dbaSJarno Rajahalme /* Must be called with rcu_read_lock or ovs_mutex. */
126*86ec8dbaSJarno Rajahalme void ovs_flow_stats_get(const struct sw_flow *flow,
127*86ec8dbaSJarno Rajahalme 			struct ovs_flow_stats *ovs_stats,
128e298e505SPravin B Shelar 			unsigned long *used, __be16 *tcp_flags)
129e298e505SPravin B Shelar {
13063e7959cSJarno Rajahalme 	int node;
131e298e505SPravin B Shelar 
132e298e505SPravin B Shelar 	*used = 0;
133e298e505SPravin B Shelar 	*tcp_flags = 0;
134e298e505SPravin B Shelar 	memset(ovs_stats, 0, sizeof(*ovs_stats));
135e298e505SPravin B Shelar 
13663e7959cSJarno Rajahalme 	for_each_node(node) {
137*86ec8dbaSJarno Rajahalme 		struct flow_stats *stats = rcu_dereference_ovsl(flow->stats[node]);
13823dabf88SJarno Rajahalme 
13963e7959cSJarno Rajahalme 		if (stats) {
14063e7959cSJarno Rajahalme 			/* Local CPU may write on non-local stats, so we must
14163e7959cSJarno Rajahalme 			 * block bottom-halves here.
14263e7959cSJarno Rajahalme 			 */
14363e7959cSJarno Rajahalme 			spin_lock_bh(&stats->lock);
14463e7959cSJarno Rajahalme 			if (!*used || time_after(stats->used, *used))
14563e7959cSJarno Rajahalme 				*used = stats->used;
14663e7959cSJarno Rajahalme 			*tcp_flags |= stats->tcp_flags;
14763e7959cSJarno Rajahalme 			ovs_stats->n_packets += stats->packet_count;
14863e7959cSJarno Rajahalme 			ovs_stats->n_bytes += stats->byte_count;
14963e7959cSJarno Rajahalme 			spin_unlock_bh(&stats->lock);
1504f647e0aSFlavio Leitner 		}
151e298e505SPravin B Shelar 	}
152e298e505SPravin B Shelar }
153e298e505SPravin B Shelar 
154*86ec8dbaSJarno Rajahalme /* Called with ovs_mutex. */
155e298e505SPravin B Shelar void ovs_flow_stats_clear(struct sw_flow *flow)
156e298e505SPravin B Shelar {
15763e7959cSJarno Rajahalme 	int node;
158e298e505SPravin B Shelar 
15963e7959cSJarno Rajahalme 	for_each_node(node) {
160*86ec8dbaSJarno Rajahalme 		struct flow_stats *stats = ovsl_dereference(flow->stats[node]);
16123dabf88SJarno Rajahalme 
16263e7959cSJarno Rajahalme 		if (stats) {
16363e7959cSJarno Rajahalme 			spin_lock_bh(&stats->lock);
16463e7959cSJarno Rajahalme 			stats->used = 0;
16563e7959cSJarno Rajahalme 			stats->packet_count = 0;
16663e7959cSJarno Rajahalme 			stats->byte_count = 0;
16763e7959cSJarno Rajahalme 			stats->tcp_flags = 0;
16863e7959cSJarno Rajahalme 			spin_unlock_bh(&stats->lock);
16963e7959cSJarno Rajahalme 		}
17063e7959cSJarno Rajahalme 	}
171e298e505SPravin B Shelar }
17203f0d916SAndy Zhou 
173ccb1352eSJesse Gross static int check_header(struct sk_buff *skb, int len)
174ccb1352eSJesse Gross {
175ccb1352eSJesse Gross 	if (unlikely(skb->len < len))
176ccb1352eSJesse Gross 		return -EINVAL;
177ccb1352eSJesse Gross 	if (unlikely(!pskb_may_pull(skb, len)))
178ccb1352eSJesse Gross 		return -ENOMEM;
179ccb1352eSJesse Gross 	return 0;
180ccb1352eSJesse Gross }
181ccb1352eSJesse Gross 
182ccb1352eSJesse Gross static bool arphdr_ok(struct sk_buff *skb)
183ccb1352eSJesse Gross {
184ccb1352eSJesse Gross 	return pskb_may_pull(skb, skb_network_offset(skb) +
185ccb1352eSJesse Gross 				  sizeof(struct arp_eth_header));
186ccb1352eSJesse Gross }
187ccb1352eSJesse Gross 
188ccb1352eSJesse Gross static int check_iphdr(struct sk_buff *skb)
189ccb1352eSJesse Gross {
190ccb1352eSJesse Gross 	unsigned int nh_ofs = skb_network_offset(skb);
191ccb1352eSJesse Gross 	unsigned int ip_len;
192ccb1352eSJesse Gross 	int err;
193ccb1352eSJesse Gross 
194ccb1352eSJesse Gross 	err = check_header(skb, nh_ofs + sizeof(struct iphdr));
195ccb1352eSJesse Gross 	if (unlikely(err))
196ccb1352eSJesse Gross 		return err;
197ccb1352eSJesse Gross 
198ccb1352eSJesse Gross 	ip_len = ip_hdrlen(skb);
199ccb1352eSJesse Gross 	if (unlikely(ip_len < sizeof(struct iphdr) ||
200ccb1352eSJesse Gross 		     skb->len < nh_ofs + ip_len))
201ccb1352eSJesse Gross 		return -EINVAL;
202ccb1352eSJesse Gross 
203ccb1352eSJesse Gross 	skb_set_transport_header(skb, nh_ofs + ip_len);
204ccb1352eSJesse Gross 	return 0;
205ccb1352eSJesse Gross }
206ccb1352eSJesse Gross 
207ccb1352eSJesse Gross static bool tcphdr_ok(struct sk_buff *skb)
208ccb1352eSJesse Gross {
209ccb1352eSJesse Gross 	int th_ofs = skb_transport_offset(skb);
210ccb1352eSJesse Gross 	int tcp_len;
211ccb1352eSJesse Gross 
212ccb1352eSJesse Gross 	if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
213ccb1352eSJesse Gross 		return false;
214ccb1352eSJesse Gross 
215ccb1352eSJesse Gross 	tcp_len = tcp_hdrlen(skb);
216ccb1352eSJesse Gross 	if (unlikely(tcp_len < sizeof(struct tcphdr) ||
217ccb1352eSJesse Gross 		     skb->len < th_ofs + tcp_len))
218ccb1352eSJesse Gross 		return false;
219ccb1352eSJesse Gross 
220ccb1352eSJesse Gross 	return true;
221ccb1352eSJesse Gross }
222ccb1352eSJesse Gross 
223ccb1352eSJesse Gross static bool udphdr_ok(struct sk_buff *skb)
224ccb1352eSJesse Gross {
225ccb1352eSJesse Gross 	return pskb_may_pull(skb, skb_transport_offset(skb) +
226ccb1352eSJesse Gross 				  sizeof(struct udphdr));
227ccb1352eSJesse Gross }
228ccb1352eSJesse Gross 
229a175a723SJoe Stringer static bool sctphdr_ok(struct sk_buff *skb)
230a175a723SJoe Stringer {
231a175a723SJoe Stringer 	return pskb_may_pull(skb, skb_transport_offset(skb) +
232a175a723SJoe Stringer 				  sizeof(struct sctphdr));
233a175a723SJoe Stringer }
234a175a723SJoe Stringer 
235ccb1352eSJesse Gross static bool icmphdr_ok(struct sk_buff *skb)
236ccb1352eSJesse Gross {
237ccb1352eSJesse Gross 	return pskb_may_pull(skb, skb_transport_offset(skb) +
238ccb1352eSJesse Gross 				  sizeof(struct icmphdr));
239ccb1352eSJesse Gross }
240ccb1352eSJesse Gross 
24103f0d916SAndy Zhou static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
242ccb1352eSJesse Gross {
243ccb1352eSJesse Gross 	unsigned int nh_ofs = skb_network_offset(skb);
244ccb1352eSJesse Gross 	unsigned int nh_len;
245ccb1352eSJesse Gross 	int payload_ofs;
246ccb1352eSJesse Gross 	struct ipv6hdr *nh;
247ccb1352eSJesse Gross 	uint8_t nexthdr;
248ccb1352eSJesse Gross 	__be16 frag_off;
249ccb1352eSJesse Gross 	int err;
250ccb1352eSJesse Gross 
251ccb1352eSJesse Gross 	err = check_header(skb, nh_ofs + sizeof(*nh));
252ccb1352eSJesse Gross 	if (unlikely(err))
253ccb1352eSJesse Gross 		return err;
254ccb1352eSJesse Gross 
255ccb1352eSJesse Gross 	nh = ipv6_hdr(skb);
256ccb1352eSJesse Gross 	nexthdr = nh->nexthdr;
257ccb1352eSJesse Gross 	payload_ofs = (u8 *)(nh + 1) - skb->data;
258ccb1352eSJesse Gross 
259ccb1352eSJesse Gross 	key->ip.proto = NEXTHDR_NONE;
260ccb1352eSJesse Gross 	key->ip.tos = ipv6_get_dsfield(nh);
261ccb1352eSJesse Gross 	key->ip.ttl = nh->hop_limit;
262ccb1352eSJesse Gross 	key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
263ccb1352eSJesse Gross 	key->ipv6.addr.src = nh->saddr;
264ccb1352eSJesse Gross 	key->ipv6.addr.dst = nh->daddr;
265ccb1352eSJesse Gross 
266ccb1352eSJesse Gross 	payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
267ccb1352eSJesse Gross 	if (unlikely(payload_ofs < 0))
268ccb1352eSJesse Gross 		return -EINVAL;
269ccb1352eSJesse Gross 
270ccb1352eSJesse Gross 	if (frag_off) {
271ccb1352eSJesse Gross 		if (frag_off & htons(~0x7))
272ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_LATER;
273ccb1352eSJesse Gross 		else
274ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_FIRST;
275ccb1352eSJesse Gross 	}
276ccb1352eSJesse Gross 
277ccb1352eSJesse Gross 	nh_len = payload_ofs - nh_ofs;
278ccb1352eSJesse Gross 	skb_set_transport_header(skb, nh_ofs + nh_len);
279ccb1352eSJesse Gross 	key->ip.proto = nexthdr;
280ccb1352eSJesse Gross 	return nh_len;
281ccb1352eSJesse Gross }
282ccb1352eSJesse Gross 
283ccb1352eSJesse Gross static bool icmp6hdr_ok(struct sk_buff *skb)
284ccb1352eSJesse Gross {
285ccb1352eSJesse Gross 	return pskb_may_pull(skb, skb_transport_offset(skb) +
286ccb1352eSJesse Gross 				  sizeof(struct icmp6hdr));
287ccb1352eSJesse Gross }
288ccb1352eSJesse Gross 
289ccb1352eSJesse Gross static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
290ccb1352eSJesse Gross {
291ccb1352eSJesse Gross 	struct qtag_prefix {
292ccb1352eSJesse Gross 		__be16 eth_type; /* ETH_P_8021Q */
293ccb1352eSJesse Gross 		__be16 tci;
294ccb1352eSJesse Gross 	};
295ccb1352eSJesse Gross 	struct qtag_prefix *qp;
296ccb1352eSJesse Gross 
297ccb1352eSJesse Gross 	if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
298ccb1352eSJesse Gross 		return 0;
299ccb1352eSJesse Gross 
300ccb1352eSJesse Gross 	if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
301ccb1352eSJesse Gross 					 sizeof(__be16))))
302ccb1352eSJesse Gross 		return -ENOMEM;
303ccb1352eSJesse Gross 
304ccb1352eSJesse Gross 	qp = (struct qtag_prefix *) skb->data;
305ccb1352eSJesse Gross 	key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
306ccb1352eSJesse Gross 	__skb_pull(skb, sizeof(struct qtag_prefix));
307ccb1352eSJesse Gross 
308ccb1352eSJesse Gross 	return 0;
309ccb1352eSJesse Gross }
310ccb1352eSJesse Gross 
311ccb1352eSJesse Gross static __be16 parse_ethertype(struct sk_buff *skb)
312ccb1352eSJesse Gross {
313ccb1352eSJesse Gross 	struct llc_snap_hdr {
314ccb1352eSJesse Gross 		u8  dsap;  /* Always 0xAA */
315ccb1352eSJesse Gross 		u8  ssap;  /* Always 0xAA */
316ccb1352eSJesse Gross 		u8  ctrl;
317ccb1352eSJesse Gross 		u8  oui[3];
318ccb1352eSJesse Gross 		__be16 ethertype;
319ccb1352eSJesse Gross 	};
320ccb1352eSJesse Gross 	struct llc_snap_hdr *llc;
321ccb1352eSJesse Gross 	__be16 proto;
322ccb1352eSJesse Gross 
323ccb1352eSJesse Gross 	proto = *(__be16 *) skb->data;
324ccb1352eSJesse Gross 	__skb_pull(skb, sizeof(__be16));
325ccb1352eSJesse Gross 
326e5c5d22eSSimon Horman 	if (ntohs(proto) >= ETH_P_802_3_MIN)
327ccb1352eSJesse Gross 		return proto;
328ccb1352eSJesse Gross 
329ccb1352eSJesse Gross 	if (skb->len < sizeof(struct llc_snap_hdr))
330ccb1352eSJesse Gross 		return htons(ETH_P_802_2);
331ccb1352eSJesse Gross 
332ccb1352eSJesse Gross 	if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
333ccb1352eSJesse Gross 		return htons(0);
334ccb1352eSJesse Gross 
335ccb1352eSJesse Gross 	llc = (struct llc_snap_hdr *) skb->data;
336ccb1352eSJesse Gross 	if (llc->dsap != LLC_SAP_SNAP ||
337ccb1352eSJesse Gross 	    llc->ssap != LLC_SAP_SNAP ||
338ccb1352eSJesse Gross 	    (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
339ccb1352eSJesse Gross 		return htons(ETH_P_802_2);
340ccb1352eSJesse Gross 
341ccb1352eSJesse Gross 	__skb_pull(skb, sizeof(struct llc_snap_hdr));
34217b682a0SRich Lane 
343e5c5d22eSSimon Horman 	if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
344ccb1352eSJesse Gross 		return llc->ethertype;
34517b682a0SRich Lane 
34617b682a0SRich Lane 	return htons(ETH_P_802_2);
347ccb1352eSJesse Gross }
348ccb1352eSJesse Gross 
349ccb1352eSJesse Gross static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
35003f0d916SAndy Zhou 			int nh_len)
351ccb1352eSJesse Gross {
352ccb1352eSJesse Gross 	struct icmp6hdr *icmp = icmp6_hdr(skb);
353ccb1352eSJesse Gross 
354ccb1352eSJesse Gross 	/* The ICMPv6 type and code fields use the 16-bit transport port
355ccb1352eSJesse Gross 	 * fields, so we need to store them in 16-bit network byte order.
356ccb1352eSJesse Gross 	 */
3571139e241SJarno Rajahalme 	key->tp.src = htons(icmp->icmp6_type);
3581139e241SJarno Rajahalme 	key->tp.dst = htons(icmp->icmp6_code);
359ccb1352eSJesse Gross 
360ccb1352eSJesse Gross 	if (icmp->icmp6_code == 0 &&
361ccb1352eSJesse Gross 	    (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
362ccb1352eSJesse Gross 	     icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
363ccb1352eSJesse Gross 		int icmp_len = skb->len - skb_transport_offset(skb);
364ccb1352eSJesse Gross 		struct nd_msg *nd;
365ccb1352eSJesse Gross 		int offset;
366ccb1352eSJesse Gross 
367ccb1352eSJesse Gross 		/* In order to process neighbor discovery options, we need the
368ccb1352eSJesse Gross 		 * entire packet.
369ccb1352eSJesse Gross 		 */
370ccb1352eSJesse Gross 		if (unlikely(icmp_len < sizeof(*nd)))
37103f0d916SAndy Zhou 			return 0;
37203f0d916SAndy Zhou 
37303f0d916SAndy Zhou 		if (unlikely(skb_linearize(skb)))
37403f0d916SAndy Zhou 			return -ENOMEM;
375ccb1352eSJesse Gross 
376ccb1352eSJesse Gross 		nd = (struct nd_msg *)skb_transport_header(skb);
377ccb1352eSJesse Gross 		key->ipv6.nd.target = nd->target;
378ccb1352eSJesse Gross 
379ccb1352eSJesse Gross 		icmp_len -= sizeof(*nd);
380ccb1352eSJesse Gross 		offset = 0;
381ccb1352eSJesse Gross 		while (icmp_len >= 8) {
382ccb1352eSJesse Gross 			struct nd_opt_hdr *nd_opt =
383ccb1352eSJesse Gross 				 (struct nd_opt_hdr *)(nd->opt + offset);
384ccb1352eSJesse Gross 			int opt_len = nd_opt->nd_opt_len * 8;
385ccb1352eSJesse Gross 
386ccb1352eSJesse Gross 			if (unlikely(!opt_len || opt_len > icmp_len))
38703f0d916SAndy Zhou 				return 0;
388ccb1352eSJesse Gross 
389ccb1352eSJesse Gross 			/* Store the link layer address if the appropriate
390ccb1352eSJesse Gross 			 * option is provided.  It is considered an error if
391ccb1352eSJesse Gross 			 * the same link layer option is specified twice.
392ccb1352eSJesse Gross 			 */
393ccb1352eSJesse Gross 			if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
394ccb1352eSJesse Gross 			    && opt_len == 8) {
395ccb1352eSJesse Gross 				if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
396ccb1352eSJesse Gross 					goto invalid;
3978c63ff09SJoe Perches 				ether_addr_copy(key->ipv6.nd.sll,
3988c63ff09SJoe Perches 						&nd->opt[offset+sizeof(*nd_opt)]);
399ccb1352eSJesse Gross 			} else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
400ccb1352eSJesse Gross 				   && opt_len == 8) {
401ccb1352eSJesse Gross 				if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
402ccb1352eSJesse Gross 					goto invalid;
4038c63ff09SJoe Perches 				ether_addr_copy(key->ipv6.nd.tll,
4048c63ff09SJoe Perches 						&nd->opt[offset+sizeof(*nd_opt)]);
405ccb1352eSJesse Gross 			}
406ccb1352eSJesse Gross 
407ccb1352eSJesse Gross 			icmp_len -= opt_len;
408ccb1352eSJesse Gross 			offset += opt_len;
409ccb1352eSJesse Gross 		}
410ccb1352eSJesse Gross 	}
411ccb1352eSJesse Gross 
41203f0d916SAndy Zhou 	return 0;
413ccb1352eSJesse Gross 
414ccb1352eSJesse Gross invalid:
415ccb1352eSJesse Gross 	memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
416ccb1352eSJesse Gross 	memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
417ccb1352eSJesse Gross 	memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
418ccb1352eSJesse Gross 
41903f0d916SAndy Zhou 	return 0;
420ccb1352eSJesse Gross }
421ccb1352eSJesse Gross 
422ccb1352eSJesse Gross /**
423ccb1352eSJesse Gross  * ovs_flow_extract - extracts a flow key from an Ethernet frame.
424ccb1352eSJesse Gross  * @skb: sk_buff that contains the frame, with skb->data pointing to the
425ccb1352eSJesse Gross  * Ethernet header
426ccb1352eSJesse Gross  * @in_port: port number on which @skb was received.
427ccb1352eSJesse Gross  * @key: output flow key
428ccb1352eSJesse Gross  *
429ccb1352eSJesse Gross  * The caller must ensure that skb->len >= ETH_HLEN.
430ccb1352eSJesse Gross  *
431ccb1352eSJesse Gross  * Returns 0 if successful, otherwise a negative errno value.
432ccb1352eSJesse Gross  *
433ccb1352eSJesse Gross  * Initializes @skb header pointers as follows:
434ccb1352eSJesse Gross  *
435ccb1352eSJesse Gross  *    - skb->mac_header: the Ethernet header.
436ccb1352eSJesse Gross  *
437ccb1352eSJesse Gross  *    - skb->network_header: just past the Ethernet header, or just past the
438ccb1352eSJesse Gross  *      VLAN header, to the first byte of the Ethernet payload.
439ccb1352eSJesse Gross  *
44034d94f21SLorand Jakab  *    - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
441ccb1352eSJesse Gross  *      on output, then just past the IP header, if one is present and
442ccb1352eSJesse Gross  *      of a correct length, otherwise the same as skb->network_header.
44334d94f21SLorand Jakab  *      For other key->eth.type values it is left untouched.
444ccb1352eSJesse Gross  */
44503f0d916SAndy Zhou int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
446ccb1352eSJesse Gross {
44703f0d916SAndy Zhou 	int error;
448ccb1352eSJesse Gross 	struct ethhdr *eth;
449ccb1352eSJesse Gross 
450ccb1352eSJesse Gross 	memset(key, 0, sizeof(*key));
451ccb1352eSJesse Gross 
452ccb1352eSJesse Gross 	key->phy.priority = skb->priority;
4537d5437c7SPravin B Shelar 	if (OVS_CB(skb)->tun_key)
4547d5437c7SPravin B Shelar 		memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
455ccb1352eSJesse Gross 	key->phy.in_port = in_port;
45639c7caebSAnsis Atteka 	key->phy.skb_mark = skb->mark;
457ccb1352eSJesse Gross 
458ccb1352eSJesse Gross 	skb_reset_mac_header(skb);
459ccb1352eSJesse Gross 
460ccb1352eSJesse Gross 	/* Link layer.  We are guaranteed to have at least the 14 byte Ethernet
461ccb1352eSJesse Gross 	 * header in the linear data area.
462ccb1352eSJesse Gross 	 */
463ccb1352eSJesse Gross 	eth = eth_hdr(skb);
4648c63ff09SJoe Perches 	ether_addr_copy(key->eth.src, eth->h_source);
4658c63ff09SJoe Perches 	ether_addr_copy(key->eth.dst, eth->h_dest);
466ccb1352eSJesse Gross 
467ccb1352eSJesse Gross 	__skb_pull(skb, 2 * ETH_ALEN);
468b34df5e8SPravin B Shelar 	/* We are going to push all headers that we pull, so no need to
469b34df5e8SPravin B Shelar 	 * update skb->csum here.
470b34df5e8SPravin B Shelar 	 */
471ccb1352eSJesse Gross 
472ccb1352eSJesse Gross 	if (vlan_tx_tag_present(skb))
473ccb1352eSJesse Gross 		key->eth.tci = htons(skb->vlan_tci);
474ccb1352eSJesse Gross 	else if (eth->h_proto == htons(ETH_P_8021Q))
475ccb1352eSJesse Gross 		if (unlikely(parse_vlan(skb, key)))
476ccb1352eSJesse Gross 			return -ENOMEM;
477ccb1352eSJesse Gross 
478ccb1352eSJesse Gross 	key->eth.type = parse_ethertype(skb);
479ccb1352eSJesse Gross 	if (unlikely(key->eth.type == htons(0)))
480ccb1352eSJesse Gross 		return -ENOMEM;
481ccb1352eSJesse Gross 
482ccb1352eSJesse Gross 	skb_reset_network_header(skb);
483ccb1352eSJesse Gross 	__skb_push(skb, skb->data - skb_mac_header(skb));
484ccb1352eSJesse Gross 
485ccb1352eSJesse Gross 	/* Network layer. */
486ccb1352eSJesse Gross 	if (key->eth.type == htons(ETH_P_IP)) {
487ccb1352eSJesse Gross 		struct iphdr *nh;
488ccb1352eSJesse Gross 		__be16 offset;
489ccb1352eSJesse Gross 
490ccb1352eSJesse Gross 		error = check_iphdr(skb);
491ccb1352eSJesse Gross 		if (unlikely(error)) {
492ccb1352eSJesse Gross 			if (error == -EINVAL) {
493ccb1352eSJesse Gross 				skb->transport_header = skb->network_header;
494ccb1352eSJesse Gross 				error = 0;
495ccb1352eSJesse Gross 			}
49603f0d916SAndy Zhou 			return error;
497ccb1352eSJesse Gross 		}
498ccb1352eSJesse Gross 
499ccb1352eSJesse Gross 		nh = ip_hdr(skb);
500ccb1352eSJesse Gross 		key->ipv4.addr.src = nh->saddr;
501ccb1352eSJesse Gross 		key->ipv4.addr.dst = nh->daddr;
502ccb1352eSJesse Gross 
503ccb1352eSJesse Gross 		key->ip.proto = nh->protocol;
504ccb1352eSJesse Gross 		key->ip.tos = nh->tos;
505ccb1352eSJesse Gross 		key->ip.ttl = nh->ttl;
506ccb1352eSJesse Gross 
507ccb1352eSJesse Gross 		offset = nh->frag_off & htons(IP_OFFSET);
508ccb1352eSJesse Gross 		if (offset) {
509ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_LATER;
51003f0d916SAndy Zhou 			return 0;
511ccb1352eSJesse Gross 		}
512ccb1352eSJesse Gross 		if (nh->frag_off & htons(IP_MF) ||
513ccb1352eSJesse Gross 			 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
514ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_FIRST;
515ccb1352eSJesse Gross 
516ccb1352eSJesse Gross 		/* Transport layer. */
517ccb1352eSJesse Gross 		if (key->ip.proto == IPPROTO_TCP) {
518ccb1352eSJesse Gross 			if (tcphdr_ok(skb)) {
519ccb1352eSJesse Gross 				struct tcphdr *tcp = tcp_hdr(skb);
5201139e241SJarno Rajahalme 				key->tp.src = tcp->source;
5211139e241SJarno Rajahalme 				key->tp.dst = tcp->dest;
5221139e241SJarno Rajahalme 				key->tp.flags = TCP_FLAGS_BE16(tcp);
523ccb1352eSJesse Gross 			}
524ccb1352eSJesse Gross 		} else if (key->ip.proto == IPPROTO_UDP) {
525ccb1352eSJesse Gross 			if (udphdr_ok(skb)) {
526ccb1352eSJesse Gross 				struct udphdr *udp = udp_hdr(skb);
5271139e241SJarno Rajahalme 				key->tp.src = udp->source;
5281139e241SJarno Rajahalme 				key->tp.dst = udp->dest;
529ccb1352eSJesse Gross 			}
530a175a723SJoe Stringer 		} else if (key->ip.proto == IPPROTO_SCTP) {
531a175a723SJoe Stringer 			if (sctphdr_ok(skb)) {
532a175a723SJoe Stringer 				struct sctphdr *sctp = sctp_hdr(skb);
5331139e241SJarno Rajahalme 				key->tp.src = sctp->source;
5341139e241SJarno Rajahalme 				key->tp.dst = sctp->dest;
535a175a723SJoe Stringer 			}
536ccb1352eSJesse Gross 		} else if (key->ip.proto == IPPROTO_ICMP) {
537ccb1352eSJesse Gross 			if (icmphdr_ok(skb)) {
538ccb1352eSJesse Gross 				struct icmphdr *icmp = icmp_hdr(skb);
539ccb1352eSJesse Gross 				/* The ICMP type and code fields use the 16-bit
540ccb1352eSJesse Gross 				 * transport port fields, so we need to store
541ccb1352eSJesse Gross 				 * them in 16-bit network byte order. */
5421139e241SJarno Rajahalme 				key->tp.src = htons(icmp->type);
5431139e241SJarno Rajahalme 				key->tp.dst = htons(icmp->code);
544ccb1352eSJesse Gross 			}
545ccb1352eSJesse Gross 		}
546ccb1352eSJesse Gross 
547c0618533SMehak Mahajan 	} else if ((key->eth.type == htons(ETH_P_ARP) ||
548c0618533SMehak Mahajan 		   key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
549ccb1352eSJesse Gross 		struct arp_eth_header *arp;
550ccb1352eSJesse Gross 
551ccb1352eSJesse Gross 		arp = (struct arp_eth_header *)skb_network_header(skb);
552ccb1352eSJesse Gross 
553ccb1352eSJesse Gross 		if (arp->ar_hrd == htons(ARPHRD_ETHER)
554ccb1352eSJesse Gross 				&& arp->ar_pro == htons(ETH_P_IP)
555ccb1352eSJesse Gross 				&& arp->ar_hln == ETH_ALEN
556ccb1352eSJesse Gross 				&& arp->ar_pln == 4) {
557ccb1352eSJesse Gross 
558ccb1352eSJesse Gross 			/* We only match on the lower 8 bits of the opcode. */
559ccb1352eSJesse Gross 			if (ntohs(arp->ar_op) <= 0xff)
560ccb1352eSJesse Gross 				key->ip.proto = ntohs(arp->ar_op);
561ccb1352eSJesse Gross 			memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
562ccb1352eSJesse Gross 			memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
5638c63ff09SJoe Perches 			ether_addr_copy(key->ipv4.arp.sha, arp->ar_sha);
5648c63ff09SJoe Perches 			ether_addr_copy(key->ipv4.arp.tha, arp->ar_tha);
565ccb1352eSJesse Gross 		}
566ccb1352eSJesse Gross 	} else if (key->eth.type == htons(ETH_P_IPV6)) {
567ccb1352eSJesse Gross 		int nh_len;             /* IPv6 Header + Extensions */
568ccb1352eSJesse Gross 
56903f0d916SAndy Zhou 		nh_len = parse_ipv6hdr(skb, key);
570ccb1352eSJesse Gross 		if (unlikely(nh_len < 0)) {
57103f0d916SAndy Zhou 			if (nh_len == -EINVAL) {
572ccb1352eSJesse Gross 				skb->transport_header = skb->network_header;
57303f0d916SAndy Zhou 				error = 0;
57403f0d916SAndy Zhou 			} else {
575ccb1352eSJesse Gross 				error = nh_len;
57603f0d916SAndy Zhou 			}
57703f0d916SAndy Zhou 			return error;
578ccb1352eSJesse Gross 		}
579ccb1352eSJesse Gross 
580ccb1352eSJesse Gross 		if (key->ip.frag == OVS_FRAG_TYPE_LATER)
58103f0d916SAndy Zhou 			return 0;
582ccb1352eSJesse Gross 		if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
583ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_FIRST;
584ccb1352eSJesse Gross 
585ccb1352eSJesse Gross 		/* Transport layer. */
586ccb1352eSJesse Gross 		if (key->ip.proto == NEXTHDR_TCP) {
587ccb1352eSJesse Gross 			if (tcphdr_ok(skb)) {
588ccb1352eSJesse Gross 				struct tcphdr *tcp = tcp_hdr(skb);
5891139e241SJarno Rajahalme 				key->tp.src = tcp->source;
5901139e241SJarno Rajahalme 				key->tp.dst = tcp->dest;
5911139e241SJarno Rajahalme 				key->tp.flags = TCP_FLAGS_BE16(tcp);
592ccb1352eSJesse Gross 			}
593ccb1352eSJesse Gross 		} else if (key->ip.proto == NEXTHDR_UDP) {
594ccb1352eSJesse Gross 			if (udphdr_ok(skb)) {
595ccb1352eSJesse Gross 				struct udphdr *udp = udp_hdr(skb);
5961139e241SJarno Rajahalme 				key->tp.src = udp->source;
5971139e241SJarno Rajahalme 				key->tp.dst = udp->dest;
598ccb1352eSJesse Gross 			}
599a175a723SJoe Stringer 		} else if (key->ip.proto == NEXTHDR_SCTP) {
600a175a723SJoe Stringer 			if (sctphdr_ok(skb)) {
601a175a723SJoe Stringer 				struct sctphdr *sctp = sctp_hdr(skb);
6021139e241SJarno Rajahalme 				key->tp.src = sctp->source;
6031139e241SJarno Rajahalme 				key->tp.dst = sctp->dest;
604a175a723SJoe Stringer 			}
605ccb1352eSJesse Gross 		} else if (key->ip.proto == NEXTHDR_ICMP) {
606ccb1352eSJesse Gross 			if (icmp6hdr_ok(skb)) {
60703f0d916SAndy Zhou 				error = parse_icmpv6(skb, key, nh_len);
60803f0d916SAndy Zhou 				if (error)
60903f0d916SAndy Zhou 					return error;
610ccb1352eSJesse Gross 			}
611ccb1352eSJesse Gross 		}
612ccb1352eSJesse Gross 	}
613ccb1352eSJesse Gross 
61403f0d916SAndy Zhou 	return 0;
615ccb1352eSJesse Gross }
616