blob: 6d8d2da0a8ec7cf01de3e30e12efaf16e36988f6 [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Andy Zhou03f0d912013-08-07 20:01:00 -07002 * Copyright (c) 2007-2013 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19#include "flow.h"
20#include "datapath.h"
21#include <linux/uaccess.h>
22#include <linux/netdevice.h>
23#include <linux/etherdevice.h>
24#include <linux/if_ether.h>
25#include <linux/if_vlan.h>
26#include <net/llc_pdu.h>
27#include <linux/kernel.h>
28#include <linux/jhash.h>
29#include <linux/jiffies.h>
30#include <linux/llc.h>
31#include <linux/module.h>
32#include <linux/in.h>
33#include <linux/rcupdate.h>
34#include <linux/if_arp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070035#include <linux/ip.h>
36#include <linux/ipv6.h>
Joe Stringera175a722013-08-22 12:30:48 -070037#include <linux/sctp.h>
Pravin B Shelare298e502013-10-29 17:22:21 -070038#include <linux/smp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070039#include <linux/tcp.h>
40#include <linux/udp.h>
41#include <linux/icmp.h>
42#include <linux/icmpv6.h>
43#include <linux/rculist.h>
44#include <net/ip.h>
Pravin B Shelar7d5437c2013-06-17 17:50:18 -070045#include <net/ip_tunnels.h>
Jesse Grossccb13522011-10-25 19:26:31 -070046#include <net/ipv6.h>
47#include <net/ndisc.h>
48
Pravin B Shelare6445712013-10-03 18:16:47 -070049u64 ovs_flow_used_time(unsigned long flow_jiffies)
Andy Zhou03f0d912013-08-07 20:01:00 -070050{
Pravin B Shelare6445712013-10-03 18:16:47 -070051 struct timespec cur_ts;
52 u64 cur_ms, idle_ms;
Andy Zhou03f0d912013-08-07 20:01:00 -070053
Pravin B Shelare6445712013-10-03 18:16:47 -070054 ktime_get_ts(&cur_ts);
55 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
56 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
57 cur_ts.tv_nsec / NSEC_PER_MSEC;
Andy Zhou03f0d912013-08-07 20:01:00 -070058
Pravin B Shelare6445712013-10-03 18:16:47 -070059 return cur_ms - idle_ms;
Andy Zhou03f0d912013-08-07 20:01:00 -070060}
61
Jarno Rajahalmedf23e9f2013-10-23 01:40:44 -070062#define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF))
Andy Zhou03f0d912013-08-07 20:01:00 -070063
Pravin B Shelare298e502013-10-29 17:22:21 -070064void ovs_flow_stats_update(struct sw_flow *flow, struct sk_buff *skb)
Andy Zhou5828cd92013-08-27 13:02:21 -070065{
Pravin B Shelare298e502013-10-29 17:22:21 -070066 struct flow_stats *stats;
Jarno Rajahalme1139e242014-05-05 09:54:49 -070067 __be16 tcp_flags = flow->key.tp.flags;
Jarno Rajahalme63e79592014-03-27 12:42:54 -070068 int node = numa_node_id();
Andy Zhou5828cd92013-08-27 13:02:21 -070069
Jarno Rajahalme63e79592014-03-27 12:42:54 -070070 stats = rcu_dereference(flow->stats[node]);
Pravin B Shelare298e502013-10-29 17:22:21 -070071
Jarno Rajahalme63e79592014-03-27 12:42:54 -070072 /* Check if already have node-specific stats. */
73 if (likely(stats)) {
74 spin_lock(&stats->lock);
75 /* Mark if we write on the pre-allocated stats. */
76 if (node == 0 && unlikely(flow->stats_last_writer != node))
77 flow->stats_last_writer = node;
78 } else {
79 stats = rcu_dereference(flow->stats[0]); /* Pre-allocated. */
80 spin_lock(&stats->lock);
81
82 /* If the current NUMA-node is the only writer on the
83 * pre-allocated stats keep using them.
84 */
85 if (unlikely(flow->stats_last_writer != node)) {
86 /* A previous locker may have already allocated the
87 * stats, so we need to check again. If node-specific
88 * stats were already allocated, we update the pre-
89 * allocated stats as we have already locked them.
90 */
91 if (likely(flow->stats_last_writer != NUMA_NO_NODE)
92 && likely(!rcu_dereference(flow->stats[node]))) {
93 /* Try to allocate node-specific stats. */
94 struct flow_stats *new_stats;
95
96 new_stats =
97 kmem_cache_alloc_node(flow_stats_cache,
98 GFP_THISNODE |
99 __GFP_NOMEMALLOC,
100 node);
101 if (likely(new_stats)) {
102 new_stats->used = jiffies;
103 new_stats->packet_count = 1;
104 new_stats->byte_count = skb->len;
105 new_stats->tcp_flags = tcp_flags;
106 spin_lock_init(&new_stats->lock);
107
108 rcu_assign_pointer(flow->stats[node],
109 new_stats);
110 goto unlock;
111 }
112 }
113 flow->stats_last_writer = node;
114 }
115 }
116
Pravin B Shelare298e502013-10-29 17:22:21 -0700117 stats->used = jiffies;
118 stats->packet_count++;
119 stats->byte_count += skb->len;
120 stats->tcp_flags |= tcp_flags;
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700121unlock:
Pravin B Shelare298e502013-10-29 17:22:21 -0700122 spin_unlock(&stats->lock);
123}
124
125void ovs_flow_stats_get(struct sw_flow *flow, struct ovs_flow_stats *ovs_stats,
126 unsigned long *used, __be16 *tcp_flags)
127{
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700128 int node;
Pravin B Shelare298e502013-10-29 17:22:21 -0700129
130 *used = 0;
131 *tcp_flags = 0;
132 memset(ovs_stats, 0, sizeof(*ovs_stats));
133
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700134 for_each_node(node) {
135 struct flow_stats *stats = rcu_dereference(flow->stats[node]);
Pravin B Shelare298e502013-10-29 17:22:21 -0700136
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700137 if (stats) {
138 /* Local CPU may write on non-local stats, so we must
139 * block bottom-halves here.
140 */
141 spin_lock_bh(&stats->lock);
142 if (!*used || time_after(stats->used, *used))
143 *used = stats->used;
144 *tcp_flags |= stats->tcp_flags;
145 ovs_stats->n_packets += stats->packet_count;
146 ovs_stats->n_bytes += stats->byte_count;
147 spin_unlock_bh(&stats->lock);
148 }
Pravin B Shelare298e502013-10-29 17:22:21 -0700149 }
Pravin B Shelare298e502013-10-29 17:22:21 -0700150}
151
152void ovs_flow_stats_clear(struct sw_flow *flow)
153{
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700154 int node;
Pravin B Shelare298e502013-10-29 17:22:21 -0700155
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700156 for_each_node(node) {
157 struct flow_stats *stats = rcu_dereference(flow->stats[node]);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700158
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700159 if (stats) {
160 spin_lock_bh(&stats->lock);
161 stats->used = 0;
162 stats->packet_count = 0;
163 stats->byte_count = 0;
164 stats->tcp_flags = 0;
165 spin_unlock_bh(&stats->lock);
166 }
167 }
Andy Zhou03f0d912013-08-07 20:01:00 -0700168}
169
Jesse Grossccb13522011-10-25 19:26:31 -0700170static int check_header(struct sk_buff *skb, int len)
171{
172 if (unlikely(skb->len < len))
173 return -EINVAL;
174 if (unlikely(!pskb_may_pull(skb, len)))
175 return -ENOMEM;
176 return 0;
177}
178
179static bool arphdr_ok(struct sk_buff *skb)
180{
181 return pskb_may_pull(skb, skb_network_offset(skb) +
182 sizeof(struct arp_eth_header));
183}
184
185static int check_iphdr(struct sk_buff *skb)
186{
187 unsigned int nh_ofs = skb_network_offset(skb);
188 unsigned int ip_len;
189 int err;
190
191 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
192 if (unlikely(err))
193 return err;
194
195 ip_len = ip_hdrlen(skb);
196 if (unlikely(ip_len < sizeof(struct iphdr) ||
197 skb->len < nh_ofs + ip_len))
198 return -EINVAL;
199
200 skb_set_transport_header(skb, nh_ofs + ip_len);
201 return 0;
202}
203
204static bool tcphdr_ok(struct sk_buff *skb)
205{
206 int th_ofs = skb_transport_offset(skb);
207 int tcp_len;
208
209 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
210 return false;
211
212 tcp_len = tcp_hdrlen(skb);
213 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
214 skb->len < th_ofs + tcp_len))
215 return false;
216
217 return true;
218}
219
220static bool udphdr_ok(struct sk_buff *skb)
221{
222 return pskb_may_pull(skb, skb_transport_offset(skb) +
223 sizeof(struct udphdr));
224}
225
Joe Stringera175a722013-08-22 12:30:48 -0700226static bool sctphdr_ok(struct sk_buff *skb)
227{
228 return pskb_may_pull(skb, skb_transport_offset(skb) +
229 sizeof(struct sctphdr));
230}
231
Jesse Grossccb13522011-10-25 19:26:31 -0700232static bool icmphdr_ok(struct sk_buff *skb)
233{
234 return pskb_may_pull(skb, skb_transport_offset(skb) +
235 sizeof(struct icmphdr));
236}
237
Andy Zhou03f0d912013-08-07 20:01:00 -0700238static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700239{
240 unsigned int nh_ofs = skb_network_offset(skb);
241 unsigned int nh_len;
242 int payload_ofs;
243 struct ipv6hdr *nh;
244 uint8_t nexthdr;
245 __be16 frag_off;
246 int err;
247
Jesse Grossccb13522011-10-25 19:26:31 -0700248 err = check_header(skb, nh_ofs + sizeof(*nh));
249 if (unlikely(err))
250 return err;
251
252 nh = ipv6_hdr(skb);
253 nexthdr = nh->nexthdr;
254 payload_ofs = (u8 *)(nh + 1) - skb->data;
255
256 key->ip.proto = NEXTHDR_NONE;
257 key->ip.tos = ipv6_get_dsfield(nh);
258 key->ip.ttl = nh->hop_limit;
259 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
260 key->ipv6.addr.src = nh->saddr;
261 key->ipv6.addr.dst = nh->daddr;
262
263 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
264 if (unlikely(payload_ofs < 0))
265 return -EINVAL;
266
267 if (frag_off) {
268 if (frag_off & htons(~0x7))
269 key->ip.frag = OVS_FRAG_TYPE_LATER;
270 else
271 key->ip.frag = OVS_FRAG_TYPE_FIRST;
272 }
273
274 nh_len = payload_ofs - nh_ofs;
275 skb_set_transport_header(skb, nh_ofs + nh_len);
276 key->ip.proto = nexthdr;
277 return nh_len;
278}
279
280static bool icmp6hdr_ok(struct sk_buff *skb)
281{
282 return pskb_may_pull(skb, skb_transport_offset(skb) +
283 sizeof(struct icmp6hdr));
284}
285
Jesse Grossccb13522011-10-25 19:26:31 -0700286static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
287{
288 struct qtag_prefix {
289 __be16 eth_type; /* ETH_P_8021Q */
290 __be16 tci;
291 };
292 struct qtag_prefix *qp;
293
294 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
295 return 0;
296
297 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
298 sizeof(__be16))))
299 return -ENOMEM;
300
301 qp = (struct qtag_prefix *) skb->data;
302 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
303 __skb_pull(skb, sizeof(struct qtag_prefix));
304
305 return 0;
306}
307
308static __be16 parse_ethertype(struct sk_buff *skb)
309{
310 struct llc_snap_hdr {
311 u8 dsap; /* Always 0xAA */
312 u8 ssap; /* Always 0xAA */
313 u8 ctrl;
314 u8 oui[3];
315 __be16 ethertype;
316 };
317 struct llc_snap_hdr *llc;
318 __be16 proto;
319
320 proto = *(__be16 *) skb->data;
321 __skb_pull(skb, sizeof(__be16));
322
Simon Hormane5c5d222013-03-28 13:38:25 +0900323 if (ntohs(proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700324 return proto;
325
326 if (skb->len < sizeof(struct llc_snap_hdr))
327 return htons(ETH_P_802_2);
328
329 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
330 return htons(0);
331
332 llc = (struct llc_snap_hdr *) skb->data;
333 if (llc->dsap != LLC_SAP_SNAP ||
334 llc->ssap != LLC_SAP_SNAP ||
335 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
336 return htons(ETH_P_802_2);
337
338 __skb_pull(skb, sizeof(struct llc_snap_hdr));
Rich Lane17b682a2013-02-19 11:10:30 -0800339
Simon Hormane5c5d222013-03-28 13:38:25 +0900340 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
Rich Lane17b682a2013-02-19 11:10:30 -0800341 return llc->ethertype;
342
343 return htons(ETH_P_802_2);
Jesse Grossccb13522011-10-25 19:26:31 -0700344}
345
346static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
Andy Zhou03f0d912013-08-07 20:01:00 -0700347 int nh_len)
Jesse Grossccb13522011-10-25 19:26:31 -0700348{
349 struct icmp6hdr *icmp = icmp6_hdr(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700350
351 /* The ICMPv6 type and code fields use the 16-bit transport port
352 * fields, so we need to store them in 16-bit network byte order.
353 */
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700354 key->tp.src = htons(icmp->icmp6_type);
355 key->tp.dst = htons(icmp->icmp6_code);
Jesse Grossccb13522011-10-25 19:26:31 -0700356
357 if (icmp->icmp6_code == 0 &&
358 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
359 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
360 int icmp_len = skb->len - skb_transport_offset(skb);
361 struct nd_msg *nd;
362 int offset;
363
Jesse Grossccb13522011-10-25 19:26:31 -0700364 /* In order to process neighbor discovery options, we need the
365 * entire packet.
366 */
367 if (unlikely(icmp_len < sizeof(*nd)))
Andy Zhou03f0d912013-08-07 20:01:00 -0700368 return 0;
369
370 if (unlikely(skb_linearize(skb)))
371 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -0700372
373 nd = (struct nd_msg *)skb_transport_header(skb);
374 key->ipv6.nd.target = nd->target;
Jesse Grossccb13522011-10-25 19:26:31 -0700375
376 icmp_len -= sizeof(*nd);
377 offset = 0;
378 while (icmp_len >= 8) {
379 struct nd_opt_hdr *nd_opt =
380 (struct nd_opt_hdr *)(nd->opt + offset);
381 int opt_len = nd_opt->nd_opt_len * 8;
382
383 if (unlikely(!opt_len || opt_len > icmp_len))
Andy Zhou03f0d912013-08-07 20:01:00 -0700384 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700385
386 /* Store the link layer address if the appropriate
387 * option is provided. It is considered an error if
388 * the same link layer option is specified twice.
389 */
390 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
391 && opt_len == 8) {
392 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
393 goto invalid;
Joe Perches8c63ff02014-02-18 11:15:45 -0800394 ether_addr_copy(key->ipv6.nd.sll,
395 &nd->opt[offset+sizeof(*nd_opt)]);
Jesse Grossccb13522011-10-25 19:26:31 -0700396 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
397 && opt_len == 8) {
398 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
399 goto invalid;
Joe Perches8c63ff02014-02-18 11:15:45 -0800400 ether_addr_copy(key->ipv6.nd.tll,
401 &nd->opt[offset+sizeof(*nd_opt)]);
Jesse Grossccb13522011-10-25 19:26:31 -0700402 }
403
404 icmp_len -= opt_len;
405 offset += opt_len;
406 }
407 }
408
Andy Zhou03f0d912013-08-07 20:01:00 -0700409 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700410
411invalid:
412 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
413 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
414 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
415
Andy Zhou03f0d912013-08-07 20:01:00 -0700416 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700417}
418
419/**
420 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
421 * @skb: sk_buff that contains the frame, with skb->data pointing to the
422 * Ethernet header
423 * @in_port: port number on which @skb was received.
424 * @key: output flow key
Jesse Grossccb13522011-10-25 19:26:31 -0700425 *
426 * The caller must ensure that skb->len >= ETH_HLEN.
427 *
428 * Returns 0 if successful, otherwise a negative errno value.
429 *
430 * Initializes @skb header pointers as follows:
431 *
432 * - skb->mac_header: the Ethernet header.
433 *
434 * - skb->network_header: just past the Ethernet header, or just past the
435 * VLAN header, to the first byte of the Ethernet payload.
436 *
Lorand Jakab34d94f22013-06-03 10:01:14 -0700437 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
Jesse Grossccb13522011-10-25 19:26:31 -0700438 * on output, then just past the IP header, if one is present and
439 * of a correct length, otherwise the same as skb->network_header.
Lorand Jakab34d94f22013-06-03 10:01:14 -0700440 * For other key->eth.type values it is left untouched.
Jesse Grossccb13522011-10-25 19:26:31 -0700441 */
Andy Zhou03f0d912013-08-07 20:01:00 -0700442int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700443{
Andy Zhou03f0d912013-08-07 20:01:00 -0700444 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700445 struct ethhdr *eth;
446
447 memset(key, 0, sizeof(*key));
448
449 key->phy.priority = skb->priority;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700450 if (OVS_CB(skb)->tun_key)
451 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
Jesse Grossccb13522011-10-25 19:26:31 -0700452 key->phy.in_port = in_port;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800453 key->phy.skb_mark = skb->mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700454
455 skb_reset_mac_header(skb);
456
457 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
458 * header in the linear data area.
459 */
460 eth = eth_hdr(skb);
Joe Perches8c63ff02014-02-18 11:15:45 -0800461 ether_addr_copy(key->eth.src, eth->h_source);
462 ether_addr_copy(key->eth.dst, eth->h_dest);
Jesse Grossccb13522011-10-25 19:26:31 -0700463
464 __skb_pull(skb, 2 * ETH_ALEN);
Pravin B Shelarb34df5e2013-06-13 11:11:44 -0700465 /* We are going to push all headers that we pull, so no need to
466 * update skb->csum here.
467 */
Jesse Grossccb13522011-10-25 19:26:31 -0700468
469 if (vlan_tx_tag_present(skb))
470 key->eth.tci = htons(skb->vlan_tci);
471 else if (eth->h_proto == htons(ETH_P_8021Q))
472 if (unlikely(parse_vlan(skb, key)))
473 return -ENOMEM;
474
475 key->eth.type = parse_ethertype(skb);
476 if (unlikely(key->eth.type == htons(0)))
477 return -ENOMEM;
478
479 skb_reset_network_header(skb);
480 __skb_push(skb, skb->data - skb_mac_header(skb));
481
482 /* Network layer. */
483 if (key->eth.type == htons(ETH_P_IP)) {
484 struct iphdr *nh;
485 __be16 offset;
486
Jesse Grossccb13522011-10-25 19:26:31 -0700487 error = check_iphdr(skb);
488 if (unlikely(error)) {
489 if (error == -EINVAL) {
490 skb->transport_header = skb->network_header;
491 error = 0;
492 }
Andy Zhou03f0d912013-08-07 20:01:00 -0700493 return error;
Jesse Grossccb13522011-10-25 19:26:31 -0700494 }
495
496 nh = ip_hdr(skb);
497 key->ipv4.addr.src = nh->saddr;
498 key->ipv4.addr.dst = nh->daddr;
499
500 key->ip.proto = nh->protocol;
501 key->ip.tos = nh->tos;
502 key->ip.ttl = nh->ttl;
503
504 offset = nh->frag_off & htons(IP_OFFSET);
505 if (offset) {
506 key->ip.frag = OVS_FRAG_TYPE_LATER;
Andy Zhou03f0d912013-08-07 20:01:00 -0700507 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700508 }
509 if (nh->frag_off & htons(IP_MF) ||
510 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
511 key->ip.frag = OVS_FRAG_TYPE_FIRST;
512
513 /* Transport layer. */
514 if (key->ip.proto == IPPROTO_TCP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700515 if (tcphdr_ok(skb)) {
516 struct tcphdr *tcp = tcp_hdr(skb);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700517 key->tp.src = tcp->source;
518 key->tp.dst = tcp->dest;
519 key->tp.flags = TCP_FLAGS_BE16(tcp);
Jesse Grossccb13522011-10-25 19:26:31 -0700520 }
521 } else if (key->ip.proto == IPPROTO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700522 if (udphdr_ok(skb)) {
523 struct udphdr *udp = udp_hdr(skb);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700524 key->tp.src = udp->source;
525 key->tp.dst = udp->dest;
Jesse Grossccb13522011-10-25 19:26:31 -0700526 }
Joe Stringera175a722013-08-22 12:30:48 -0700527 } else if (key->ip.proto == IPPROTO_SCTP) {
528 if (sctphdr_ok(skb)) {
529 struct sctphdr *sctp = sctp_hdr(skb);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700530 key->tp.src = sctp->source;
531 key->tp.dst = sctp->dest;
Joe Stringera175a722013-08-22 12:30:48 -0700532 }
Jesse Grossccb13522011-10-25 19:26:31 -0700533 } else if (key->ip.proto == IPPROTO_ICMP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700534 if (icmphdr_ok(skb)) {
535 struct icmphdr *icmp = icmp_hdr(skb);
536 /* The ICMP type and code fields use the 16-bit
537 * transport port fields, so we need to store
538 * them in 16-bit network byte order. */
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700539 key->tp.src = htons(icmp->type);
540 key->tp.dst = htons(icmp->code);
Jesse Grossccb13522011-10-25 19:26:31 -0700541 }
542 }
543
Mehak Mahajanc0618532012-11-02 14:14:31 -0700544 } else if ((key->eth.type == htons(ETH_P_ARP) ||
545 key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
Jesse Grossccb13522011-10-25 19:26:31 -0700546 struct arp_eth_header *arp;
547
548 arp = (struct arp_eth_header *)skb_network_header(skb);
549
550 if (arp->ar_hrd == htons(ARPHRD_ETHER)
551 && arp->ar_pro == htons(ETH_P_IP)
552 && arp->ar_hln == ETH_ALEN
553 && arp->ar_pln == 4) {
554
555 /* We only match on the lower 8 bits of the opcode. */
556 if (ntohs(arp->ar_op) <= 0xff)
557 key->ip.proto = ntohs(arp->ar_op);
Mehak Mahajand04d3822012-10-30 15:50:28 -0700558 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
559 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
Joe Perches8c63ff02014-02-18 11:15:45 -0800560 ether_addr_copy(key->ipv4.arp.sha, arp->ar_sha);
561 ether_addr_copy(key->ipv4.arp.tha, arp->ar_tha);
Jesse Grossccb13522011-10-25 19:26:31 -0700562 }
563 } else if (key->eth.type == htons(ETH_P_IPV6)) {
564 int nh_len; /* IPv6 Header + Extensions */
565
Andy Zhou03f0d912013-08-07 20:01:00 -0700566 nh_len = parse_ipv6hdr(skb, key);
Jesse Grossccb13522011-10-25 19:26:31 -0700567 if (unlikely(nh_len < 0)) {
Andy Zhou03f0d912013-08-07 20:01:00 -0700568 if (nh_len == -EINVAL) {
Jesse Grossccb13522011-10-25 19:26:31 -0700569 skb->transport_header = skb->network_header;
Andy Zhou03f0d912013-08-07 20:01:00 -0700570 error = 0;
571 } else {
Jesse Grossccb13522011-10-25 19:26:31 -0700572 error = nh_len;
Andy Zhou03f0d912013-08-07 20:01:00 -0700573 }
574 return error;
Jesse Grossccb13522011-10-25 19:26:31 -0700575 }
576
577 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
Andy Zhou03f0d912013-08-07 20:01:00 -0700578 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700579 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
580 key->ip.frag = OVS_FRAG_TYPE_FIRST;
581
582 /* Transport layer. */
583 if (key->ip.proto == NEXTHDR_TCP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700584 if (tcphdr_ok(skb)) {
585 struct tcphdr *tcp = tcp_hdr(skb);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700586 key->tp.src = tcp->source;
587 key->tp.dst = tcp->dest;
588 key->tp.flags = TCP_FLAGS_BE16(tcp);
Jesse Grossccb13522011-10-25 19:26:31 -0700589 }
590 } else if (key->ip.proto == NEXTHDR_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700591 if (udphdr_ok(skb)) {
592 struct udphdr *udp = udp_hdr(skb);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700593 key->tp.src = udp->source;
594 key->tp.dst = udp->dest;
Jesse Grossccb13522011-10-25 19:26:31 -0700595 }
Joe Stringera175a722013-08-22 12:30:48 -0700596 } else if (key->ip.proto == NEXTHDR_SCTP) {
597 if (sctphdr_ok(skb)) {
598 struct sctphdr *sctp = sctp_hdr(skb);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700599 key->tp.src = sctp->source;
600 key->tp.dst = sctp->dest;
Joe Stringera175a722013-08-22 12:30:48 -0700601 }
Jesse Grossccb13522011-10-25 19:26:31 -0700602 } else if (key->ip.proto == NEXTHDR_ICMP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700603 if (icmp6hdr_ok(skb)) {
Andy Zhou03f0d912013-08-07 20:01:00 -0700604 error = parse_icmpv6(skb, key, nh_len);
605 if (error)
606 return error;
Jesse Grossccb13522011-10-25 19:26:31 -0700607 }
608 }
609 }
610
Andy Zhou03f0d912013-08-07 20:01:00 -0700611 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700612}