xref: /linux/net/openvswitch/flow.c (revision df23e9f642830f10c505c8a3d57772ad1238c701)
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>
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>
46ccb1352eSJesse Gross #include <net/ndisc.h>
47ccb1352eSJesse Gross 
48e6445719SPravin B Shelar u64 ovs_flow_used_time(unsigned long flow_jiffies)
4903f0d916SAndy Zhou {
50e6445719SPravin B Shelar 	struct timespec cur_ts;
51e6445719SPravin B Shelar 	u64 cur_ms, idle_ms;
5203f0d916SAndy Zhou 
53e6445719SPravin B Shelar 	ktime_get_ts(&cur_ts);
54e6445719SPravin B Shelar 	idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
55e6445719SPravin B Shelar 	cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
56e6445719SPravin B Shelar 		 cur_ts.tv_nsec / NSEC_PER_MSEC;
5703f0d916SAndy Zhou 
58e6445719SPravin B Shelar 	return cur_ms - idle_ms;
5903f0d916SAndy Zhou }
6003f0d916SAndy Zhou 
61*df23e9f6SJarno Rajahalme #define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF))
6203f0d916SAndy Zhou 
63e6445719SPravin B Shelar void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
645828cd9aSAndy Zhou {
65*df23e9f6SJarno Rajahalme 	__be16 tcp_flags = 0;
66e6445719SPravin B Shelar 
67e6445719SPravin B Shelar 	if ((flow->key.eth.type == htons(ETH_P_IP) ||
68e6445719SPravin B Shelar 	     flow->key.eth.type == htons(ETH_P_IPV6)) &&
69e6445719SPravin B Shelar 	    flow->key.ip.proto == IPPROTO_TCP &&
70e6445719SPravin B Shelar 	    likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
71*df23e9f6SJarno Rajahalme 		tcp_flags = TCP_FLAGS_BE16(tcp_hdr(skb));
725828cd9aSAndy Zhou 	}
735828cd9aSAndy Zhou 
74e6445719SPravin B Shelar 	spin_lock(&flow->lock);
75e6445719SPravin B Shelar 	flow->used = jiffies;
76e6445719SPravin B Shelar 	flow->packet_count++;
77e6445719SPravin B Shelar 	flow->byte_count += skb->len;
78e6445719SPravin B Shelar 	flow->tcp_flags |= tcp_flags;
79e6445719SPravin B Shelar 	spin_unlock(&flow->lock);
8003f0d916SAndy Zhou }
8103f0d916SAndy Zhou 
82ccb1352eSJesse Gross static int check_header(struct sk_buff *skb, int len)
83ccb1352eSJesse Gross {
84ccb1352eSJesse Gross 	if (unlikely(skb->len < len))
85ccb1352eSJesse Gross 		return -EINVAL;
86ccb1352eSJesse Gross 	if (unlikely(!pskb_may_pull(skb, len)))
87ccb1352eSJesse Gross 		return -ENOMEM;
88ccb1352eSJesse Gross 	return 0;
89ccb1352eSJesse Gross }
90ccb1352eSJesse Gross 
91ccb1352eSJesse Gross static bool arphdr_ok(struct sk_buff *skb)
92ccb1352eSJesse Gross {
93ccb1352eSJesse Gross 	return pskb_may_pull(skb, skb_network_offset(skb) +
94ccb1352eSJesse Gross 				  sizeof(struct arp_eth_header));
95ccb1352eSJesse Gross }
96ccb1352eSJesse Gross 
97ccb1352eSJesse Gross static int check_iphdr(struct sk_buff *skb)
98ccb1352eSJesse Gross {
99ccb1352eSJesse Gross 	unsigned int nh_ofs = skb_network_offset(skb);
100ccb1352eSJesse Gross 	unsigned int ip_len;
101ccb1352eSJesse Gross 	int err;
102ccb1352eSJesse Gross 
103ccb1352eSJesse Gross 	err = check_header(skb, nh_ofs + sizeof(struct iphdr));
104ccb1352eSJesse Gross 	if (unlikely(err))
105ccb1352eSJesse Gross 		return err;
106ccb1352eSJesse Gross 
107ccb1352eSJesse Gross 	ip_len = ip_hdrlen(skb);
108ccb1352eSJesse Gross 	if (unlikely(ip_len < sizeof(struct iphdr) ||
109ccb1352eSJesse Gross 		     skb->len < nh_ofs + ip_len))
110ccb1352eSJesse Gross 		return -EINVAL;
111ccb1352eSJesse Gross 
112ccb1352eSJesse Gross 	skb_set_transport_header(skb, nh_ofs + ip_len);
113ccb1352eSJesse Gross 	return 0;
114ccb1352eSJesse Gross }
115ccb1352eSJesse Gross 
116ccb1352eSJesse Gross static bool tcphdr_ok(struct sk_buff *skb)
117ccb1352eSJesse Gross {
118ccb1352eSJesse Gross 	int th_ofs = skb_transport_offset(skb);
119ccb1352eSJesse Gross 	int tcp_len;
120ccb1352eSJesse Gross 
121ccb1352eSJesse Gross 	if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
122ccb1352eSJesse Gross 		return false;
123ccb1352eSJesse Gross 
124ccb1352eSJesse Gross 	tcp_len = tcp_hdrlen(skb);
125ccb1352eSJesse Gross 	if (unlikely(tcp_len < sizeof(struct tcphdr) ||
126ccb1352eSJesse Gross 		     skb->len < th_ofs + tcp_len))
127ccb1352eSJesse Gross 		return false;
128ccb1352eSJesse Gross 
129ccb1352eSJesse Gross 	return true;
130ccb1352eSJesse Gross }
131ccb1352eSJesse Gross 
132ccb1352eSJesse Gross static bool udphdr_ok(struct sk_buff *skb)
133ccb1352eSJesse Gross {
134ccb1352eSJesse Gross 	return pskb_may_pull(skb, skb_transport_offset(skb) +
135ccb1352eSJesse Gross 				  sizeof(struct udphdr));
136ccb1352eSJesse Gross }
137ccb1352eSJesse Gross 
138a175a723SJoe Stringer static bool sctphdr_ok(struct sk_buff *skb)
139a175a723SJoe Stringer {
140a175a723SJoe Stringer 	return pskb_may_pull(skb, skb_transport_offset(skb) +
141a175a723SJoe Stringer 				  sizeof(struct sctphdr));
142a175a723SJoe Stringer }
143a175a723SJoe Stringer 
144ccb1352eSJesse Gross static bool icmphdr_ok(struct sk_buff *skb)
145ccb1352eSJesse Gross {
146ccb1352eSJesse Gross 	return pskb_may_pull(skb, skb_transport_offset(skb) +
147ccb1352eSJesse Gross 				  sizeof(struct icmphdr));
148ccb1352eSJesse Gross }
149ccb1352eSJesse Gross 
15003f0d916SAndy Zhou static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
151ccb1352eSJesse Gross {
152ccb1352eSJesse Gross 	unsigned int nh_ofs = skb_network_offset(skb);
153ccb1352eSJesse Gross 	unsigned int nh_len;
154ccb1352eSJesse Gross 	int payload_ofs;
155ccb1352eSJesse Gross 	struct ipv6hdr *nh;
156ccb1352eSJesse Gross 	uint8_t nexthdr;
157ccb1352eSJesse Gross 	__be16 frag_off;
158ccb1352eSJesse Gross 	int err;
159ccb1352eSJesse Gross 
160ccb1352eSJesse Gross 	err = check_header(skb, nh_ofs + sizeof(*nh));
161ccb1352eSJesse Gross 	if (unlikely(err))
162ccb1352eSJesse Gross 		return err;
163ccb1352eSJesse Gross 
164ccb1352eSJesse Gross 	nh = ipv6_hdr(skb);
165ccb1352eSJesse Gross 	nexthdr = nh->nexthdr;
166ccb1352eSJesse Gross 	payload_ofs = (u8 *)(nh + 1) - skb->data;
167ccb1352eSJesse Gross 
168ccb1352eSJesse Gross 	key->ip.proto = NEXTHDR_NONE;
169ccb1352eSJesse Gross 	key->ip.tos = ipv6_get_dsfield(nh);
170ccb1352eSJesse Gross 	key->ip.ttl = nh->hop_limit;
171ccb1352eSJesse Gross 	key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
172ccb1352eSJesse Gross 	key->ipv6.addr.src = nh->saddr;
173ccb1352eSJesse Gross 	key->ipv6.addr.dst = nh->daddr;
174ccb1352eSJesse Gross 
175ccb1352eSJesse Gross 	payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
176ccb1352eSJesse Gross 	if (unlikely(payload_ofs < 0))
177ccb1352eSJesse Gross 		return -EINVAL;
178ccb1352eSJesse Gross 
179ccb1352eSJesse Gross 	if (frag_off) {
180ccb1352eSJesse Gross 		if (frag_off & htons(~0x7))
181ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_LATER;
182ccb1352eSJesse Gross 		else
183ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_FIRST;
184ccb1352eSJesse Gross 	}
185ccb1352eSJesse Gross 
186ccb1352eSJesse Gross 	nh_len = payload_ofs - nh_ofs;
187ccb1352eSJesse Gross 	skb_set_transport_header(skb, nh_ofs + nh_len);
188ccb1352eSJesse Gross 	key->ip.proto = nexthdr;
189ccb1352eSJesse Gross 	return nh_len;
190ccb1352eSJesse Gross }
191ccb1352eSJesse Gross 
192ccb1352eSJesse Gross static bool icmp6hdr_ok(struct sk_buff *skb)
193ccb1352eSJesse Gross {
194ccb1352eSJesse Gross 	return pskb_may_pull(skb, skb_transport_offset(skb) +
195ccb1352eSJesse Gross 				  sizeof(struct icmp6hdr));
196ccb1352eSJesse Gross }
197ccb1352eSJesse Gross 
198ccb1352eSJesse Gross static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
199ccb1352eSJesse Gross {
200ccb1352eSJesse Gross 	struct qtag_prefix {
201ccb1352eSJesse Gross 		__be16 eth_type; /* ETH_P_8021Q */
202ccb1352eSJesse Gross 		__be16 tci;
203ccb1352eSJesse Gross 	};
204ccb1352eSJesse Gross 	struct qtag_prefix *qp;
205ccb1352eSJesse Gross 
206ccb1352eSJesse Gross 	if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
207ccb1352eSJesse Gross 		return 0;
208ccb1352eSJesse Gross 
209ccb1352eSJesse Gross 	if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
210ccb1352eSJesse Gross 					 sizeof(__be16))))
211ccb1352eSJesse Gross 		return -ENOMEM;
212ccb1352eSJesse Gross 
213ccb1352eSJesse Gross 	qp = (struct qtag_prefix *) skb->data;
214ccb1352eSJesse Gross 	key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
215ccb1352eSJesse Gross 	__skb_pull(skb, sizeof(struct qtag_prefix));
216ccb1352eSJesse Gross 
217ccb1352eSJesse Gross 	return 0;
218ccb1352eSJesse Gross }
219ccb1352eSJesse Gross 
220ccb1352eSJesse Gross static __be16 parse_ethertype(struct sk_buff *skb)
221ccb1352eSJesse Gross {
222ccb1352eSJesse Gross 	struct llc_snap_hdr {
223ccb1352eSJesse Gross 		u8  dsap;  /* Always 0xAA */
224ccb1352eSJesse Gross 		u8  ssap;  /* Always 0xAA */
225ccb1352eSJesse Gross 		u8  ctrl;
226ccb1352eSJesse Gross 		u8  oui[3];
227ccb1352eSJesse Gross 		__be16 ethertype;
228ccb1352eSJesse Gross 	};
229ccb1352eSJesse Gross 	struct llc_snap_hdr *llc;
230ccb1352eSJesse Gross 	__be16 proto;
231ccb1352eSJesse Gross 
232ccb1352eSJesse Gross 	proto = *(__be16 *) skb->data;
233ccb1352eSJesse Gross 	__skb_pull(skb, sizeof(__be16));
234ccb1352eSJesse Gross 
235e5c5d22eSSimon Horman 	if (ntohs(proto) >= ETH_P_802_3_MIN)
236ccb1352eSJesse Gross 		return proto;
237ccb1352eSJesse Gross 
238ccb1352eSJesse Gross 	if (skb->len < sizeof(struct llc_snap_hdr))
239ccb1352eSJesse Gross 		return htons(ETH_P_802_2);
240ccb1352eSJesse Gross 
241ccb1352eSJesse Gross 	if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
242ccb1352eSJesse Gross 		return htons(0);
243ccb1352eSJesse Gross 
244ccb1352eSJesse Gross 	llc = (struct llc_snap_hdr *) skb->data;
245ccb1352eSJesse Gross 	if (llc->dsap != LLC_SAP_SNAP ||
246ccb1352eSJesse Gross 	    llc->ssap != LLC_SAP_SNAP ||
247ccb1352eSJesse Gross 	    (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
248ccb1352eSJesse Gross 		return htons(ETH_P_802_2);
249ccb1352eSJesse Gross 
250ccb1352eSJesse Gross 	__skb_pull(skb, sizeof(struct llc_snap_hdr));
25117b682a0SRich Lane 
252e5c5d22eSSimon Horman 	if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
253ccb1352eSJesse Gross 		return llc->ethertype;
25417b682a0SRich Lane 
25517b682a0SRich Lane 	return htons(ETH_P_802_2);
256ccb1352eSJesse Gross }
257ccb1352eSJesse Gross 
258ccb1352eSJesse Gross static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
25903f0d916SAndy Zhou 			int nh_len)
260ccb1352eSJesse Gross {
261ccb1352eSJesse Gross 	struct icmp6hdr *icmp = icmp6_hdr(skb);
262ccb1352eSJesse Gross 
263ccb1352eSJesse Gross 	/* The ICMPv6 type and code fields use the 16-bit transport port
264ccb1352eSJesse Gross 	 * fields, so we need to store them in 16-bit network byte order.
265ccb1352eSJesse Gross 	 */
266ccb1352eSJesse Gross 	key->ipv6.tp.src = htons(icmp->icmp6_type);
267ccb1352eSJesse Gross 	key->ipv6.tp.dst = htons(icmp->icmp6_code);
268ccb1352eSJesse Gross 
269ccb1352eSJesse Gross 	if (icmp->icmp6_code == 0 &&
270ccb1352eSJesse Gross 	    (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
271ccb1352eSJesse Gross 	     icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
272ccb1352eSJesse Gross 		int icmp_len = skb->len - skb_transport_offset(skb);
273ccb1352eSJesse Gross 		struct nd_msg *nd;
274ccb1352eSJesse Gross 		int offset;
275ccb1352eSJesse Gross 
276ccb1352eSJesse Gross 		/* In order to process neighbor discovery options, we need the
277ccb1352eSJesse Gross 		 * entire packet.
278ccb1352eSJesse Gross 		 */
279ccb1352eSJesse Gross 		if (unlikely(icmp_len < sizeof(*nd)))
28003f0d916SAndy Zhou 			return 0;
28103f0d916SAndy Zhou 
28203f0d916SAndy Zhou 		if (unlikely(skb_linearize(skb)))
28303f0d916SAndy Zhou 			return -ENOMEM;
284ccb1352eSJesse Gross 
285ccb1352eSJesse Gross 		nd = (struct nd_msg *)skb_transport_header(skb);
286ccb1352eSJesse Gross 		key->ipv6.nd.target = nd->target;
287ccb1352eSJesse Gross 
288ccb1352eSJesse Gross 		icmp_len -= sizeof(*nd);
289ccb1352eSJesse Gross 		offset = 0;
290ccb1352eSJesse Gross 		while (icmp_len >= 8) {
291ccb1352eSJesse Gross 			struct nd_opt_hdr *nd_opt =
292ccb1352eSJesse Gross 				 (struct nd_opt_hdr *)(nd->opt + offset);
293ccb1352eSJesse Gross 			int opt_len = nd_opt->nd_opt_len * 8;
294ccb1352eSJesse Gross 
295ccb1352eSJesse Gross 			if (unlikely(!opt_len || opt_len > icmp_len))
29603f0d916SAndy Zhou 				return 0;
297ccb1352eSJesse Gross 
298ccb1352eSJesse Gross 			/* Store the link layer address if the appropriate
299ccb1352eSJesse Gross 			 * option is provided.  It is considered an error if
300ccb1352eSJesse Gross 			 * the same link layer option is specified twice.
301ccb1352eSJesse Gross 			 */
302ccb1352eSJesse Gross 			if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
303ccb1352eSJesse Gross 			    && opt_len == 8) {
304ccb1352eSJesse Gross 				if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
305ccb1352eSJesse Gross 					goto invalid;
306ccb1352eSJesse Gross 				memcpy(key->ipv6.nd.sll,
307ccb1352eSJesse Gross 				    &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
308ccb1352eSJesse Gross 			} else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
309ccb1352eSJesse Gross 				   && opt_len == 8) {
310ccb1352eSJesse Gross 				if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
311ccb1352eSJesse Gross 					goto invalid;
312ccb1352eSJesse Gross 				memcpy(key->ipv6.nd.tll,
313ccb1352eSJesse Gross 				    &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
314ccb1352eSJesse Gross 			}
315ccb1352eSJesse Gross 
316ccb1352eSJesse Gross 			icmp_len -= opt_len;
317ccb1352eSJesse Gross 			offset += opt_len;
318ccb1352eSJesse Gross 		}
319ccb1352eSJesse Gross 	}
320ccb1352eSJesse Gross 
32103f0d916SAndy Zhou 	return 0;
322ccb1352eSJesse Gross 
323ccb1352eSJesse Gross invalid:
324ccb1352eSJesse Gross 	memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
325ccb1352eSJesse Gross 	memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
326ccb1352eSJesse Gross 	memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
327ccb1352eSJesse Gross 
32803f0d916SAndy Zhou 	return 0;
329ccb1352eSJesse Gross }
330ccb1352eSJesse Gross 
331ccb1352eSJesse Gross /**
332ccb1352eSJesse Gross  * ovs_flow_extract - extracts a flow key from an Ethernet frame.
333ccb1352eSJesse Gross  * @skb: sk_buff that contains the frame, with skb->data pointing to the
334ccb1352eSJesse Gross  * Ethernet header
335ccb1352eSJesse Gross  * @in_port: port number on which @skb was received.
336ccb1352eSJesse Gross  * @key: output flow key
337ccb1352eSJesse Gross  *
338ccb1352eSJesse Gross  * The caller must ensure that skb->len >= ETH_HLEN.
339ccb1352eSJesse Gross  *
340ccb1352eSJesse Gross  * Returns 0 if successful, otherwise a negative errno value.
341ccb1352eSJesse Gross  *
342ccb1352eSJesse Gross  * Initializes @skb header pointers as follows:
343ccb1352eSJesse Gross  *
344ccb1352eSJesse Gross  *    - skb->mac_header: the Ethernet header.
345ccb1352eSJesse Gross  *
346ccb1352eSJesse Gross  *    - skb->network_header: just past the Ethernet header, or just past the
347ccb1352eSJesse Gross  *      VLAN header, to the first byte of the Ethernet payload.
348ccb1352eSJesse Gross  *
34934d94f21SLorand Jakab  *    - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
350ccb1352eSJesse Gross  *      on output, then just past the IP header, if one is present and
351ccb1352eSJesse Gross  *      of a correct length, otherwise the same as skb->network_header.
35234d94f21SLorand Jakab  *      For other key->eth.type values it is left untouched.
353ccb1352eSJesse Gross  */
35403f0d916SAndy Zhou int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
355ccb1352eSJesse Gross {
35603f0d916SAndy Zhou 	int error;
357ccb1352eSJesse Gross 	struct ethhdr *eth;
358ccb1352eSJesse Gross 
359ccb1352eSJesse Gross 	memset(key, 0, sizeof(*key));
360ccb1352eSJesse Gross 
361ccb1352eSJesse Gross 	key->phy.priority = skb->priority;
3627d5437c7SPravin B Shelar 	if (OVS_CB(skb)->tun_key)
3637d5437c7SPravin B Shelar 		memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
364ccb1352eSJesse Gross 	key->phy.in_port = in_port;
36539c7caebSAnsis Atteka 	key->phy.skb_mark = skb->mark;
366ccb1352eSJesse Gross 
367ccb1352eSJesse Gross 	skb_reset_mac_header(skb);
368ccb1352eSJesse Gross 
369ccb1352eSJesse Gross 	/* Link layer.  We are guaranteed to have at least the 14 byte Ethernet
370ccb1352eSJesse Gross 	 * header in the linear data area.
371ccb1352eSJesse Gross 	 */
372ccb1352eSJesse Gross 	eth = eth_hdr(skb);
373ccb1352eSJesse Gross 	memcpy(key->eth.src, eth->h_source, ETH_ALEN);
374ccb1352eSJesse Gross 	memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
375ccb1352eSJesse Gross 
376ccb1352eSJesse Gross 	__skb_pull(skb, 2 * ETH_ALEN);
377b34df5e8SPravin B Shelar 	/* We are going to push all headers that we pull, so no need to
378b34df5e8SPravin B Shelar 	 * update skb->csum here.
379b34df5e8SPravin B Shelar 	 */
380ccb1352eSJesse Gross 
381ccb1352eSJesse Gross 	if (vlan_tx_tag_present(skb))
382ccb1352eSJesse Gross 		key->eth.tci = htons(skb->vlan_tci);
383ccb1352eSJesse Gross 	else if (eth->h_proto == htons(ETH_P_8021Q))
384ccb1352eSJesse Gross 		if (unlikely(parse_vlan(skb, key)))
385ccb1352eSJesse Gross 			return -ENOMEM;
386ccb1352eSJesse Gross 
387ccb1352eSJesse Gross 	key->eth.type = parse_ethertype(skb);
388ccb1352eSJesse Gross 	if (unlikely(key->eth.type == htons(0)))
389ccb1352eSJesse Gross 		return -ENOMEM;
390ccb1352eSJesse Gross 
391ccb1352eSJesse Gross 	skb_reset_network_header(skb);
392ccb1352eSJesse Gross 	__skb_push(skb, skb->data - skb_mac_header(skb));
393ccb1352eSJesse Gross 
394ccb1352eSJesse Gross 	/* Network layer. */
395ccb1352eSJesse Gross 	if (key->eth.type == htons(ETH_P_IP)) {
396ccb1352eSJesse Gross 		struct iphdr *nh;
397ccb1352eSJesse Gross 		__be16 offset;
398ccb1352eSJesse Gross 
399ccb1352eSJesse Gross 		error = check_iphdr(skb);
400ccb1352eSJesse Gross 		if (unlikely(error)) {
401ccb1352eSJesse Gross 			if (error == -EINVAL) {
402ccb1352eSJesse Gross 				skb->transport_header = skb->network_header;
403ccb1352eSJesse Gross 				error = 0;
404ccb1352eSJesse Gross 			}
40503f0d916SAndy Zhou 			return error;
406ccb1352eSJesse Gross 		}
407ccb1352eSJesse Gross 
408ccb1352eSJesse Gross 		nh = ip_hdr(skb);
409ccb1352eSJesse Gross 		key->ipv4.addr.src = nh->saddr;
410ccb1352eSJesse Gross 		key->ipv4.addr.dst = nh->daddr;
411ccb1352eSJesse Gross 
412ccb1352eSJesse Gross 		key->ip.proto = nh->protocol;
413ccb1352eSJesse Gross 		key->ip.tos = nh->tos;
414ccb1352eSJesse Gross 		key->ip.ttl = nh->ttl;
415ccb1352eSJesse Gross 
416ccb1352eSJesse Gross 		offset = nh->frag_off & htons(IP_OFFSET);
417ccb1352eSJesse Gross 		if (offset) {
418ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_LATER;
41903f0d916SAndy Zhou 			return 0;
420ccb1352eSJesse Gross 		}
421ccb1352eSJesse Gross 		if (nh->frag_off & htons(IP_MF) ||
422ccb1352eSJesse Gross 			 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
423ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_FIRST;
424ccb1352eSJesse Gross 
425ccb1352eSJesse Gross 		/* Transport layer. */
426ccb1352eSJesse Gross 		if (key->ip.proto == IPPROTO_TCP) {
427ccb1352eSJesse Gross 			if (tcphdr_ok(skb)) {
428ccb1352eSJesse Gross 				struct tcphdr *tcp = tcp_hdr(skb);
429ccb1352eSJesse Gross 				key->ipv4.tp.src = tcp->source;
430ccb1352eSJesse Gross 				key->ipv4.tp.dst = tcp->dest;
431ccb1352eSJesse Gross 			}
432ccb1352eSJesse Gross 		} else if (key->ip.proto == IPPROTO_UDP) {
433ccb1352eSJesse Gross 			if (udphdr_ok(skb)) {
434ccb1352eSJesse Gross 				struct udphdr *udp = udp_hdr(skb);
435ccb1352eSJesse Gross 				key->ipv4.tp.src = udp->source;
436ccb1352eSJesse Gross 				key->ipv4.tp.dst = udp->dest;
437ccb1352eSJesse Gross 			}
438a175a723SJoe Stringer 		} else if (key->ip.proto == IPPROTO_SCTP) {
439a175a723SJoe Stringer 			if (sctphdr_ok(skb)) {
440a175a723SJoe Stringer 				struct sctphdr *sctp = sctp_hdr(skb);
441a175a723SJoe Stringer 				key->ipv4.tp.src = sctp->source;
442a175a723SJoe Stringer 				key->ipv4.tp.dst = sctp->dest;
443a175a723SJoe Stringer 			}
444ccb1352eSJesse Gross 		} else if (key->ip.proto == IPPROTO_ICMP) {
445ccb1352eSJesse Gross 			if (icmphdr_ok(skb)) {
446ccb1352eSJesse Gross 				struct icmphdr *icmp = icmp_hdr(skb);
447ccb1352eSJesse Gross 				/* The ICMP type and code fields use the 16-bit
448ccb1352eSJesse Gross 				 * transport port fields, so we need to store
449ccb1352eSJesse Gross 				 * them in 16-bit network byte order. */
450ccb1352eSJesse Gross 				key->ipv4.tp.src = htons(icmp->type);
451ccb1352eSJesse Gross 				key->ipv4.tp.dst = htons(icmp->code);
452ccb1352eSJesse Gross 			}
453ccb1352eSJesse Gross 		}
454ccb1352eSJesse Gross 
455c0618533SMehak Mahajan 	} else if ((key->eth.type == htons(ETH_P_ARP) ||
456c0618533SMehak Mahajan 		   key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
457ccb1352eSJesse Gross 		struct arp_eth_header *arp;
458ccb1352eSJesse Gross 
459ccb1352eSJesse Gross 		arp = (struct arp_eth_header *)skb_network_header(skb);
460ccb1352eSJesse Gross 
461ccb1352eSJesse Gross 		if (arp->ar_hrd == htons(ARPHRD_ETHER)
462ccb1352eSJesse Gross 				&& arp->ar_pro == htons(ETH_P_IP)
463ccb1352eSJesse Gross 				&& arp->ar_hln == ETH_ALEN
464ccb1352eSJesse Gross 				&& arp->ar_pln == 4) {
465ccb1352eSJesse Gross 
466ccb1352eSJesse Gross 			/* We only match on the lower 8 bits of the opcode. */
467ccb1352eSJesse Gross 			if (ntohs(arp->ar_op) <= 0xff)
468ccb1352eSJesse Gross 				key->ip.proto = ntohs(arp->ar_op);
469ccb1352eSJesse Gross 			memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
470ccb1352eSJesse Gross 			memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
471ccb1352eSJesse Gross 			memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
472ccb1352eSJesse Gross 			memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
473ccb1352eSJesse Gross 		}
474ccb1352eSJesse Gross 	} else if (key->eth.type == htons(ETH_P_IPV6)) {
475ccb1352eSJesse Gross 		int nh_len;             /* IPv6 Header + Extensions */
476ccb1352eSJesse Gross 
47703f0d916SAndy Zhou 		nh_len = parse_ipv6hdr(skb, key);
478ccb1352eSJesse Gross 		if (unlikely(nh_len < 0)) {
47903f0d916SAndy Zhou 			if (nh_len == -EINVAL) {
480ccb1352eSJesse Gross 				skb->transport_header = skb->network_header;
48103f0d916SAndy Zhou 				error = 0;
48203f0d916SAndy Zhou 			} else {
483ccb1352eSJesse Gross 				error = nh_len;
48403f0d916SAndy Zhou 			}
48503f0d916SAndy Zhou 			return error;
486ccb1352eSJesse Gross 		}
487ccb1352eSJesse Gross 
488ccb1352eSJesse Gross 		if (key->ip.frag == OVS_FRAG_TYPE_LATER)
48903f0d916SAndy Zhou 			return 0;
490ccb1352eSJesse Gross 		if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
491ccb1352eSJesse Gross 			key->ip.frag = OVS_FRAG_TYPE_FIRST;
492ccb1352eSJesse Gross 
493ccb1352eSJesse Gross 		/* Transport layer. */
494ccb1352eSJesse Gross 		if (key->ip.proto == NEXTHDR_TCP) {
495ccb1352eSJesse Gross 			if (tcphdr_ok(skb)) {
496ccb1352eSJesse Gross 				struct tcphdr *tcp = tcp_hdr(skb);
497ccb1352eSJesse Gross 				key->ipv6.tp.src = tcp->source;
498ccb1352eSJesse Gross 				key->ipv6.tp.dst = tcp->dest;
499ccb1352eSJesse Gross 			}
500ccb1352eSJesse Gross 		} else if (key->ip.proto == NEXTHDR_UDP) {
501ccb1352eSJesse Gross 			if (udphdr_ok(skb)) {
502ccb1352eSJesse Gross 				struct udphdr *udp = udp_hdr(skb);
503ccb1352eSJesse Gross 				key->ipv6.tp.src = udp->source;
504ccb1352eSJesse Gross 				key->ipv6.tp.dst = udp->dest;
505ccb1352eSJesse Gross 			}
506a175a723SJoe Stringer 		} else if (key->ip.proto == NEXTHDR_SCTP) {
507a175a723SJoe Stringer 			if (sctphdr_ok(skb)) {
508a175a723SJoe Stringer 				struct sctphdr *sctp = sctp_hdr(skb);
509a175a723SJoe Stringer 				key->ipv6.tp.src = sctp->source;
510a175a723SJoe Stringer 				key->ipv6.tp.dst = sctp->dest;
511a175a723SJoe Stringer 			}
512ccb1352eSJesse Gross 		} else if (key->ip.proto == NEXTHDR_ICMP) {
513ccb1352eSJesse Gross 			if (icmp6hdr_ok(skb)) {
51403f0d916SAndy Zhou 				error = parse_icmpv6(skb, key, nh_len);
51503f0d916SAndy Zhou 				if (error)
51603f0d916SAndy Zhou 					return error;
517ccb1352eSJesse Gross 			}
518ccb1352eSJesse Gross 		}
519ccb1352eSJesse Gross 	}
520ccb1352eSJesse Gross 
52103f0d916SAndy Zhou 	return 0;
522ccb1352eSJesse Gross }
523