blob: 25e9abcd51f17415abf2d7007f03d469530fa2de [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Andy Zhou971427f32014-09-15 19:37:25 -07002 * Copyright (c) 2007-2014 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
Jesse Grossccb13522011-10-25 19:26:31 -070019#include <linux/uaccess.h>
20#include <linux/netdevice.h>
21#include <linux/etherdevice.h>
22#include <linux/if_ether.h>
23#include <linux/if_vlan.h>
24#include <net/llc_pdu.h>
25#include <linux/kernel.h>
26#include <linux/jhash.h>
27#include <linux/jiffies.h>
28#include <linux/llc.h>
29#include <linux/module.h>
30#include <linux/in.h>
31#include <linux/rcupdate.h>
32#include <linux/if_arp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070033#include <linux/ip.h>
34#include <linux/ipv6.h>
Simon Horman25cd9ba2014-10-06 05:05:13 -070035#include <linux/mpls.h>
Joe Stringera175a722013-08-22 12:30:48 -070036#include <linux/sctp.h>
Pravin B Shelare298e502013-10-29 17:22:21 -070037#include <linux/smp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070038#include <linux/tcp.h>
39#include <linux/udp.h>
40#include <linux/icmp.h>
41#include <linux/icmpv6.h>
42#include <linux/rculist.h>
43#include <net/ip.h>
Pravin B Shelar7d5437c2013-06-17 17:50:18 -070044#include <net/ip_tunnels.h>
Jesse Grossccb13522011-10-25 19:26:31 -070045#include <net/ipv6.h>
Simon Horman25cd9ba2014-10-06 05:05:13 -070046#include <net/mpls.h>
Jesse Grossccb13522011-10-25 19:26:31 -070047#include <net/ndisc.h>
48
Pravin B Shelar83c8df22014-09-15 19:20:31 -070049#include "datapath.h"
50#include "flow.h"
51#include "flow_netlink.h"
52
Pravin B Shelare6445712013-10-03 18:16:47 -070053u64 ovs_flow_used_time(unsigned long flow_jiffies)
Andy Zhou03f0d912013-08-07 20:01:00 -070054{
Pravin B Shelare6445712013-10-03 18:16:47 -070055 struct timespec cur_ts;
56 u64 cur_ms, idle_ms;
Andy Zhou03f0d912013-08-07 20:01:00 -070057
Pravin B Shelare6445712013-10-03 18:16:47 -070058 ktime_get_ts(&cur_ts);
59 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
60 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
61 cur_ts.tv_nsec / NSEC_PER_MSEC;
Andy Zhou03f0d912013-08-07 20:01:00 -070062
Pravin B Shelare6445712013-10-03 18:16:47 -070063 return cur_ms - idle_ms;
Andy Zhou03f0d912013-08-07 20:01:00 -070064}
65
Jarno Rajahalmedf23e9f2013-10-23 01:40:44 -070066#define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF))
Andy Zhou03f0d912013-08-07 20:01:00 -070067
Ben Pfaffad552002014-05-06 16:48:38 -070068void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags,
Thomas Graf12eb18f2014-11-06 06:58:52 -080069 const struct sk_buff *skb)
Andy Zhou5828cd92013-08-27 13:02:21 -070070{
Pravin B Shelare298e502013-10-29 17:22:21 -070071 struct flow_stats *stats;
Jarno Rajahalme63e79592014-03-27 12:42:54 -070072 int node = numa_node_id();
Andy Zhou5828cd92013-08-27 13:02:21 -070073
Jarno Rajahalme63e79592014-03-27 12:42:54 -070074 stats = rcu_dereference(flow->stats[node]);
Pravin B Shelare298e502013-10-29 17:22:21 -070075
Jarno Rajahalme63e79592014-03-27 12:42:54 -070076 /* Check if already have node-specific stats. */
77 if (likely(stats)) {
78 spin_lock(&stats->lock);
79 /* Mark if we write on the pre-allocated stats. */
80 if (node == 0 && unlikely(flow->stats_last_writer != node))
81 flow->stats_last_writer = node;
82 } else {
83 stats = rcu_dereference(flow->stats[0]); /* Pre-allocated. */
84 spin_lock(&stats->lock);
85
86 /* If the current NUMA-node is the only writer on the
87 * pre-allocated stats keep using them.
88 */
89 if (unlikely(flow->stats_last_writer != node)) {
90 /* A previous locker may have already allocated the
91 * stats, so we need to check again. If node-specific
92 * stats were already allocated, we update the pre-
93 * allocated stats as we have already locked them.
94 */
95 if (likely(flow->stats_last_writer != NUMA_NO_NODE)
Andreea-Cristina Bernat8c6b00c2014-08-17 16:29:43 +030096 && likely(!rcu_access_pointer(flow->stats[node]))) {
Jarno Rajahalme63e79592014-03-27 12:42:54 -070097 /* Try to allocate node-specific stats. */
98 struct flow_stats *new_stats;
99
100 new_stats =
101 kmem_cache_alloc_node(flow_stats_cache,
102 GFP_THISNODE |
103 __GFP_NOMEMALLOC,
104 node);
105 if (likely(new_stats)) {
106 new_stats->used = jiffies;
107 new_stats->packet_count = 1;
108 new_stats->byte_count = skb->len;
109 new_stats->tcp_flags = tcp_flags;
110 spin_lock_init(&new_stats->lock);
111
112 rcu_assign_pointer(flow->stats[node],
113 new_stats);
114 goto unlock;
115 }
116 }
117 flow->stats_last_writer = node;
118 }
119 }
120
Pravin B Shelare298e502013-10-29 17:22:21 -0700121 stats->used = jiffies;
122 stats->packet_count++;
123 stats->byte_count += skb->len;
124 stats->tcp_flags |= tcp_flags;
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700125unlock:
Pravin B Shelare298e502013-10-29 17:22:21 -0700126 spin_unlock(&stats->lock);
127}
128
Jarno Rajahalme86ec8db2014-05-05 14:17:28 -0700129/* Must be called with rcu_read_lock or ovs_mutex. */
130void ovs_flow_stats_get(const struct sw_flow *flow,
131 struct ovs_flow_stats *ovs_stats,
Pravin B Shelare298e502013-10-29 17:22:21 -0700132 unsigned long *used, __be16 *tcp_flags)
133{
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700134 int node;
Pravin B Shelare298e502013-10-29 17:22:21 -0700135
136 *used = 0;
137 *tcp_flags = 0;
138 memset(ovs_stats, 0, sizeof(*ovs_stats));
139
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700140 for_each_node(node) {
Jarno Rajahalme86ec8db2014-05-05 14:17:28 -0700141 struct flow_stats *stats = rcu_dereference_ovsl(flow->stats[node]);
Pravin B Shelare298e502013-10-29 17:22:21 -0700142
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700143 if (stats) {
144 /* Local CPU may write on non-local stats, so we must
145 * block bottom-halves here.
146 */
147 spin_lock_bh(&stats->lock);
148 if (!*used || time_after(stats->used, *used))
149 *used = stats->used;
150 *tcp_flags |= stats->tcp_flags;
151 ovs_stats->n_packets += stats->packet_count;
152 ovs_stats->n_bytes += stats->byte_count;
153 spin_unlock_bh(&stats->lock);
154 }
Pravin B Shelare298e502013-10-29 17:22:21 -0700155 }
Pravin B Shelare298e502013-10-29 17:22:21 -0700156}
157
Jarno Rajahalme86ec8db2014-05-05 14:17:28 -0700158/* Called with ovs_mutex. */
Pravin B Shelare298e502013-10-29 17:22:21 -0700159void ovs_flow_stats_clear(struct sw_flow *flow)
160{
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700161 int node;
Pravin B Shelare298e502013-10-29 17:22:21 -0700162
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700163 for_each_node(node) {
Jarno Rajahalme86ec8db2014-05-05 14:17:28 -0700164 struct flow_stats *stats = ovsl_dereference(flow->stats[node]);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700165
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700166 if (stats) {
167 spin_lock_bh(&stats->lock);
168 stats->used = 0;
169 stats->packet_count = 0;
170 stats->byte_count = 0;
171 stats->tcp_flags = 0;
172 spin_unlock_bh(&stats->lock);
173 }
174 }
Andy Zhou03f0d912013-08-07 20:01:00 -0700175}
176
Jesse Grossccb13522011-10-25 19:26:31 -0700177static int check_header(struct sk_buff *skb, int len)
178{
179 if (unlikely(skb->len < len))
180 return -EINVAL;
181 if (unlikely(!pskb_may_pull(skb, len)))
182 return -ENOMEM;
183 return 0;
184}
185
186static bool arphdr_ok(struct sk_buff *skb)
187{
188 return pskb_may_pull(skb, skb_network_offset(skb) +
189 sizeof(struct arp_eth_header));
190}
191
192static int check_iphdr(struct sk_buff *skb)
193{
194 unsigned int nh_ofs = skb_network_offset(skb);
195 unsigned int ip_len;
196 int err;
197
198 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
199 if (unlikely(err))
200 return err;
201
202 ip_len = ip_hdrlen(skb);
203 if (unlikely(ip_len < sizeof(struct iphdr) ||
204 skb->len < nh_ofs + ip_len))
205 return -EINVAL;
206
207 skb_set_transport_header(skb, nh_ofs + ip_len);
208 return 0;
209}
210
211static bool tcphdr_ok(struct sk_buff *skb)
212{
213 int th_ofs = skb_transport_offset(skb);
214 int tcp_len;
215
216 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
217 return false;
218
219 tcp_len = tcp_hdrlen(skb);
220 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
221 skb->len < th_ofs + tcp_len))
222 return false;
223
224 return true;
225}
226
227static bool udphdr_ok(struct sk_buff *skb)
228{
229 return pskb_may_pull(skb, skb_transport_offset(skb) +
230 sizeof(struct udphdr));
231}
232
Joe Stringera175a722013-08-22 12:30:48 -0700233static bool sctphdr_ok(struct sk_buff *skb)
234{
235 return pskb_may_pull(skb, skb_transport_offset(skb) +
236 sizeof(struct sctphdr));
237}
238
Jesse Grossccb13522011-10-25 19:26:31 -0700239static bool icmphdr_ok(struct sk_buff *skb)
240{
241 return pskb_may_pull(skb, skb_transport_offset(skb) +
242 sizeof(struct icmphdr));
243}
244
Andy Zhou03f0d912013-08-07 20:01:00 -0700245static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700246{
247 unsigned int nh_ofs = skb_network_offset(skb);
248 unsigned int nh_len;
249 int payload_ofs;
250 struct ipv6hdr *nh;
251 uint8_t nexthdr;
252 __be16 frag_off;
253 int err;
254
Jesse Grossccb13522011-10-25 19:26:31 -0700255 err = check_header(skb, nh_ofs + sizeof(*nh));
256 if (unlikely(err))
257 return err;
258
259 nh = ipv6_hdr(skb);
260 nexthdr = nh->nexthdr;
261 payload_ofs = (u8 *)(nh + 1) - skb->data;
262
263 key->ip.proto = NEXTHDR_NONE;
264 key->ip.tos = ipv6_get_dsfield(nh);
265 key->ip.ttl = nh->hop_limit;
266 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
267 key->ipv6.addr.src = nh->saddr;
268 key->ipv6.addr.dst = nh->daddr;
269
270 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
271 if (unlikely(payload_ofs < 0))
272 return -EINVAL;
273
274 if (frag_off) {
275 if (frag_off & htons(~0x7))
276 key->ip.frag = OVS_FRAG_TYPE_LATER;
277 else
278 key->ip.frag = OVS_FRAG_TYPE_FIRST;
Pravin B Shelar25ef1322014-10-17 13:56:31 -0700279 } else {
280 key->ip.frag = OVS_FRAG_TYPE_NONE;
Jesse Grossccb13522011-10-25 19:26:31 -0700281 }
282
283 nh_len = payload_ofs - nh_ofs;
284 skb_set_transport_header(skb, nh_ofs + nh_len);
285 key->ip.proto = nexthdr;
286 return nh_len;
287}
288
289static bool icmp6hdr_ok(struct sk_buff *skb)
290{
291 return pskb_may_pull(skb, skb_transport_offset(skb) +
292 sizeof(struct icmp6hdr));
293}
294
Jesse Grossccb13522011-10-25 19:26:31 -0700295static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
296{
297 struct qtag_prefix {
298 __be16 eth_type; /* ETH_P_8021Q */
299 __be16 tci;
300 };
301 struct qtag_prefix *qp;
302
303 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
304 return 0;
305
306 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
307 sizeof(__be16))))
308 return -ENOMEM;
309
310 qp = (struct qtag_prefix *) skb->data;
311 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
312 __skb_pull(skb, sizeof(struct qtag_prefix));
313
314 return 0;
315}
316
317static __be16 parse_ethertype(struct sk_buff *skb)
318{
319 struct llc_snap_hdr {
320 u8 dsap; /* Always 0xAA */
321 u8 ssap; /* Always 0xAA */
322 u8 ctrl;
323 u8 oui[3];
324 __be16 ethertype;
325 };
326 struct llc_snap_hdr *llc;
327 __be16 proto;
328
329 proto = *(__be16 *) skb->data;
330 __skb_pull(skb, sizeof(__be16));
331
Simon Hormane5c5d222013-03-28 13:38:25 +0900332 if (ntohs(proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700333 return proto;
334
335 if (skb->len < sizeof(struct llc_snap_hdr))
336 return htons(ETH_P_802_2);
337
338 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
339 return htons(0);
340
341 llc = (struct llc_snap_hdr *) skb->data;
342 if (llc->dsap != LLC_SAP_SNAP ||
343 llc->ssap != LLC_SAP_SNAP ||
344 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
345 return htons(ETH_P_802_2);
346
347 __skb_pull(skb, sizeof(struct llc_snap_hdr));
Rich Lane17b682a2013-02-19 11:10:30 -0800348
Simon Hormane5c5d222013-03-28 13:38:25 +0900349 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
Rich Lane17b682a2013-02-19 11:10:30 -0800350 return llc->ethertype;
351
352 return htons(ETH_P_802_2);
Jesse Grossccb13522011-10-25 19:26:31 -0700353}
354
355static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
Andy Zhou03f0d912013-08-07 20:01:00 -0700356 int nh_len)
Jesse Grossccb13522011-10-25 19:26:31 -0700357{
358 struct icmp6hdr *icmp = icmp6_hdr(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700359
360 /* The ICMPv6 type and code fields use the 16-bit transport port
361 * fields, so we need to store them in 16-bit network byte order.
362 */
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700363 key->tp.src = htons(icmp->icmp6_type);
364 key->tp.dst = htons(icmp->icmp6_code);
Pravin B Shelar25ef1322014-10-17 13:56:31 -0700365 memset(&key->ipv6.nd, 0, sizeof(key->ipv6.nd));
Jesse Grossccb13522011-10-25 19:26:31 -0700366
367 if (icmp->icmp6_code == 0 &&
368 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
369 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
370 int icmp_len = skb->len - skb_transport_offset(skb);
371 struct nd_msg *nd;
372 int offset;
373
Jesse Grossccb13522011-10-25 19:26:31 -0700374 /* In order to process neighbor discovery options, we need the
375 * entire packet.
376 */
377 if (unlikely(icmp_len < sizeof(*nd)))
Andy Zhou03f0d912013-08-07 20:01:00 -0700378 return 0;
379
380 if (unlikely(skb_linearize(skb)))
381 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -0700382
383 nd = (struct nd_msg *)skb_transport_header(skb);
384 key->ipv6.nd.target = nd->target;
Jesse Grossccb13522011-10-25 19:26:31 -0700385
386 icmp_len -= sizeof(*nd);
387 offset = 0;
388 while (icmp_len >= 8) {
389 struct nd_opt_hdr *nd_opt =
390 (struct nd_opt_hdr *)(nd->opt + offset);
391 int opt_len = nd_opt->nd_opt_len * 8;
392
393 if (unlikely(!opt_len || opt_len > icmp_len))
Andy Zhou03f0d912013-08-07 20:01:00 -0700394 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700395
396 /* Store the link layer address if the appropriate
397 * option is provided. It is considered an error if
398 * the same link layer option is specified twice.
399 */
400 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
401 && opt_len == 8) {
402 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
403 goto invalid;
Joe Perches8c63ff02014-02-18 11:15:45 -0800404 ether_addr_copy(key->ipv6.nd.sll,
405 &nd->opt[offset+sizeof(*nd_opt)]);
Jesse Grossccb13522011-10-25 19:26:31 -0700406 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
407 && opt_len == 8) {
408 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
409 goto invalid;
Joe Perches8c63ff02014-02-18 11:15:45 -0800410 ether_addr_copy(key->ipv6.nd.tll,
411 &nd->opt[offset+sizeof(*nd_opt)]);
Jesse Grossccb13522011-10-25 19:26:31 -0700412 }
413
414 icmp_len -= opt_len;
415 offset += opt_len;
416 }
417 }
418
Andy Zhou03f0d912013-08-07 20:01:00 -0700419 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700420
421invalid:
422 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
423 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
424 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
425
Andy Zhou03f0d912013-08-07 20:01:00 -0700426 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700427}
428
429/**
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700430 * key_extract - extracts a flow key from an Ethernet frame.
Jesse Grossccb13522011-10-25 19:26:31 -0700431 * @skb: sk_buff that contains the frame, with skb->data pointing to the
432 * Ethernet header
Jesse Grossccb13522011-10-25 19:26:31 -0700433 * @key: output flow key
Jesse Grossccb13522011-10-25 19:26:31 -0700434 *
435 * The caller must ensure that skb->len >= ETH_HLEN.
436 *
437 * Returns 0 if successful, otherwise a negative errno value.
438 *
439 * Initializes @skb header pointers as follows:
440 *
441 * - skb->mac_header: the Ethernet header.
442 *
443 * - skb->network_header: just past the Ethernet header, or just past the
444 * VLAN header, to the first byte of the Ethernet payload.
445 *
Lorand Jakab34d94f22013-06-03 10:01:14 -0700446 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
Jesse Grossccb13522011-10-25 19:26:31 -0700447 * on output, then just past the IP header, if one is present and
448 * of a correct length, otherwise the same as skb->network_header.
Lorand Jakab34d94f22013-06-03 10:01:14 -0700449 * For other key->eth.type values it is left untouched.
Jesse Grossccb13522011-10-25 19:26:31 -0700450 */
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700451static int key_extract(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700452{
Andy Zhou03f0d912013-08-07 20:01:00 -0700453 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700454 struct ethhdr *eth;
455
Jesse Grossf5796682014-10-03 15:35:33 -0700456 /* Flags are always used as part of stats */
457 key->tp.flags = 0;
458
Jesse Grossccb13522011-10-25 19:26:31 -0700459 skb_reset_mac_header(skb);
460
461 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
462 * header in the linear data area.
463 */
464 eth = eth_hdr(skb);
Joe Perches8c63ff02014-02-18 11:15:45 -0800465 ether_addr_copy(key->eth.src, eth->h_source);
466 ether_addr_copy(key->eth.dst, eth->h_dest);
Jesse Grossccb13522011-10-25 19:26:31 -0700467
468 __skb_pull(skb, 2 * ETH_ALEN);
Pravin B Shelarb34df5e2013-06-13 11:11:44 -0700469 /* We are going to push all headers that we pull, so no need to
470 * update skb->csum here.
471 */
Jesse Grossccb13522011-10-25 19:26:31 -0700472
Jesse Gross07148122014-10-03 15:35:29 -0700473 key->eth.tci = 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700474 if (vlan_tx_tag_present(skb))
475 key->eth.tci = htons(skb->vlan_tci);
476 else if (eth->h_proto == htons(ETH_P_8021Q))
477 if (unlikely(parse_vlan(skb, key)))
478 return -ENOMEM;
479
480 key->eth.type = parse_ethertype(skb);
481 if (unlikely(key->eth.type == htons(0)))
482 return -ENOMEM;
483
484 skb_reset_network_header(skb);
Simon Horman25cd9ba2014-10-06 05:05:13 -0700485 skb_reset_mac_len(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700486 __skb_push(skb, skb->data - skb_mac_header(skb));
487
488 /* Network layer. */
489 if (key->eth.type == htons(ETH_P_IP)) {
490 struct iphdr *nh;
491 __be16 offset;
492
Jesse Grossccb13522011-10-25 19:26:31 -0700493 error = check_iphdr(skb);
494 if (unlikely(error)) {
Jesse Gross07148122014-10-03 15:35:29 -0700495 memset(&key->ip, 0, sizeof(key->ip));
496 memset(&key->ipv4, 0, sizeof(key->ipv4));
Jesse Grossccb13522011-10-25 19:26:31 -0700497 if (error == -EINVAL) {
498 skb->transport_header = skb->network_header;
499 error = 0;
500 }
Andy Zhou03f0d912013-08-07 20:01:00 -0700501 return error;
Jesse Grossccb13522011-10-25 19:26:31 -0700502 }
503
504 nh = ip_hdr(skb);
505 key->ipv4.addr.src = nh->saddr;
506 key->ipv4.addr.dst = nh->daddr;
507
508 key->ip.proto = nh->protocol;
509 key->ip.tos = nh->tos;
510 key->ip.ttl = nh->ttl;
511
512 offset = nh->frag_off & htons(IP_OFFSET);
513 if (offset) {
514 key->ip.frag = OVS_FRAG_TYPE_LATER;
Andy Zhou03f0d912013-08-07 20:01:00 -0700515 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700516 }
517 if (nh->frag_off & htons(IP_MF) ||
Jesse Gross07148122014-10-03 15:35:29 -0700518 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
Jesse Grossccb13522011-10-25 19:26:31 -0700519 key->ip.frag = OVS_FRAG_TYPE_FIRST;
Jesse Gross07148122014-10-03 15:35:29 -0700520 else
521 key->ip.frag = OVS_FRAG_TYPE_NONE;
Jesse Grossccb13522011-10-25 19:26:31 -0700522
523 /* Transport layer. */
524 if (key->ip.proto == IPPROTO_TCP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700525 if (tcphdr_ok(skb)) {
526 struct tcphdr *tcp = tcp_hdr(skb);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700527 key->tp.src = tcp->source;
528 key->tp.dst = tcp->dest;
529 key->tp.flags = TCP_FLAGS_BE16(tcp);
Jesse Gross07148122014-10-03 15:35:29 -0700530 } else {
531 memset(&key->tp, 0, sizeof(key->tp));
Jesse Grossccb13522011-10-25 19:26:31 -0700532 }
Jesse Gross07148122014-10-03 15:35:29 -0700533
Jesse Grossccb13522011-10-25 19:26:31 -0700534 } else if (key->ip.proto == IPPROTO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700535 if (udphdr_ok(skb)) {
536 struct udphdr *udp = udp_hdr(skb);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700537 key->tp.src = udp->source;
538 key->tp.dst = udp->dest;
Jesse Gross07148122014-10-03 15:35:29 -0700539 } else {
540 memset(&key->tp, 0, sizeof(key->tp));
Jesse Grossccb13522011-10-25 19:26:31 -0700541 }
Joe Stringera175a722013-08-22 12:30:48 -0700542 } else if (key->ip.proto == IPPROTO_SCTP) {
543 if (sctphdr_ok(skb)) {
544 struct sctphdr *sctp = sctp_hdr(skb);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700545 key->tp.src = sctp->source;
546 key->tp.dst = sctp->dest;
Jesse Gross07148122014-10-03 15:35:29 -0700547 } else {
548 memset(&key->tp, 0, sizeof(key->tp));
Joe Stringera175a722013-08-22 12:30:48 -0700549 }
Jesse Grossccb13522011-10-25 19:26:31 -0700550 } else if (key->ip.proto == IPPROTO_ICMP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700551 if (icmphdr_ok(skb)) {
552 struct icmphdr *icmp = icmp_hdr(skb);
553 /* The ICMP type and code fields use the 16-bit
554 * transport port fields, so we need to store
555 * them in 16-bit network byte order. */
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700556 key->tp.src = htons(icmp->type);
557 key->tp.dst = htons(icmp->code);
Jesse Gross07148122014-10-03 15:35:29 -0700558 } else {
559 memset(&key->tp, 0, sizeof(key->tp));
Jesse Grossccb13522011-10-25 19:26:31 -0700560 }
561 }
562
Jesse Gross07148122014-10-03 15:35:29 -0700563 } else if (key->eth.type == htons(ETH_P_ARP) ||
564 key->eth.type == htons(ETH_P_RARP)) {
Jesse Grossccb13522011-10-25 19:26:31 -0700565 struct arp_eth_header *arp;
Li RongQing389f4892014-10-17 14:03:08 +0800566 bool arp_available = arphdr_ok(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700567
568 arp = (struct arp_eth_header *)skb_network_header(skb);
569
Li RongQing389f4892014-10-17 14:03:08 +0800570 if (arp_available &&
Jesse Gross07148122014-10-03 15:35:29 -0700571 arp->ar_hrd == htons(ARPHRD_ETHER) &&
572 arp->ar_pro == htons(ETH_P_IP) &&
573 arp->ar_hln == ETH_ALEN &&
574 arp->ar_pln == 4) {
Jesse Grossccb13522011-10-25 19:26:31 -0700575
576 /* We only match on the lower 8 bits of the opcode. */
577 if (ntohs(arp->ar_op) <= 0xff)
578 key->ip.proto = ntohs(arp->ar_op);
Jesse Gross07148122014-10-03 15:35:29 -0700579 else
580 key->ip.proto = 0;
581
Mehak Mahajand04d3822012-10-30 15:50:28 -0700582 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
583 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
Joe Perches8c63ff02014-02-18 11:15:45 -0800584 ether_addr_copy(key->ipv4.arp.sha, arp->ar_sha);
585 ether_addr_copy(key->ipv4.arp.tha, arp->ar_tha);
Jesse Gross07148122014-10-03 15:35:29 -0700586 } else {
587 memset(&key->ip, 0, sizeof(key->ip));
588 memset(&key->ipv4, 0, sizeof(key->ipv4));
Jesse Grossccb13522011-10-25 19:26:31 -0700589 }
Simon Horman25cd9ba2014-10-06 05:05:13 -0700590 } else if (eth_p_mpls(key->eth.type)) {
591 size_t stack_len = MPLS_HLEN;
592
593 /* In the presence of an MPLS label stack the end of the L2
594 * header and the beginning of the L3 header differ.
595 *
596 * Advance network_header to the beginning of the L3
597 * header. mac_len corresponds to the end of the L2 header.
598 */
599 while (1) {
600 __be32 lse;
601
602 error = check_header(skb, skb->mac_len + stack_len);
603 if (unlikely(error))
604 return 0;
605
606 memcpy(&lse, skb_network_header(skb), MPLS_HLEN);
607
608 if (stack_len == MPLS_HLEN)
609 memcpy(&key->mpls.top_lse, &lse, MPLS_HLEN);
610
611 skb_set_network_header(skb, skb->mac_len + stack_len);
612 if (lse & htonl(MPLS_LS_S_MASK))
613 break;
614
615 stack_len += MPLS_HLEN;
616 }
Jesse Grossccb13522011-10-25 19:26:31 -0700617 } else if (key->eth.type == htons(ETH_P_IPV6)) {
618 int nh_len; /* IPv6 Header + Extensions */
619
Andy Zhou03f0d912013-08-07 20:01:00 -0700620 nh_len = parse_ipv6hdr(skb, key);
Jesse Grossccb13522011-10-25 19:26:31 -0700621 if (unlikely(nh_len < 0)) {
Jesse Gross07148122014-10-03 15:35:29 -0700622 memset(&key->ip, 0, sizeof(key->ip));
623 memset(&key->ipv6.addr, 0, sizeof(key->ipv6.addr));
Andy Zhou03f0d912013-08-07 20:01:00 -0700624 if (nh_len == -EINVAL) {
Jesse Grossccb13522011-10-25 19:26:31 -0700625 skb->transport_header = skb->network_header;
Andy Zhou03f0d912013-08-07 20:01:00 -0700626 error = 0;
627 } else {
Jesse Grossccb13522011-10-25 19:26:31 -0700628 error = nh_len;
Andy Zhou03f0d912013-08-07 20:01:00 -0700629 }
630 return error;
Jesse Grossccb13522011-10-25 19:26:31 -0700631 }
632
633 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
Andy Zhou03f0d912013-08-07 20:01:00 -0700634 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700635 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
636 key->ip.frag = OVS_FRAG_TYPE_FIRST;
637
638 /* Transport layer. */
639 if (key->ip.proto == NEXTHDR_TCP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700640 if (tcphdr_ok(skb)) {
641 struct tcphdr *tcp = tcp_hdr(skb);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700642 key->tp.src = tcp->source;
643 key->tp.dst = tcp->dest;
644 key->tp.flags = TCP_FLAGS_BE16(tcp);
Jesse Gross07148122014-10-03 15:35:29 -0700645 } else {
646 memset(&key->tp, 0, sizeof(key->tp));
Jesse Grossccb13522011-10-25 19:26:31 -0700647 }
648 } else if (key->ip.proto == NEXTHDR_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700649 if (udphdr_ok(skb)) {
650 struct udphdr *udp = udp_hdr(skb);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700651 key->tp.src = udp->source;
652 key->tp.dst = udp->dest;
Jesse Gross07148122014-10-03 15:35:29 -0700653 } else {
654 memset(&key->tp, 0, sizeof(key->tp));
Jesse Grossccb13522011-10-25 19:26:31 -0700655 }
Joe Stringera175a722013-08-22 12:30:48 -0700656 } else if (key->ip.proto == NEXTHDR_SCTP) {
657 if (sctphdr_ok(skb)) {
658 struct sctphdr *sctp = sctp_hdr(skb);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700659 key->tp.src = sctp->source;
660 key->tp.dst = sctp->dest;
Jesse Gross07148122014-10-03 15:35:29 -0700661 } else {
662 memset(&key->tp, 0, sizeof(key->tp));
Joe Stringera175a722013-08-22 12:30:48 -0700663 }
Jesse Grossccb13522011-10-25 19:26:31 -0700664 } else if (key->ip.proto == NEXTHDR_ICMP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700665 if (icmp6hdr_ok(skb)) {
Andy Zhou03f0d912013-08-07 20:01:00 -0700666 error = parse_icmpv6(skb, key, nh_len);
667 if (error)
668 return error;
Jesse Gross07148122014-10-03 15:35:29 -0700669 } else {
670 memset(&key->tp, 0, sizeof(key->tp));
Jesse Grossccb13522011-10-25 19:26:31 -0700671 }
672 }
673 }
Andy Zhou03f0d912013-08-07 20:01:00 -0700674 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700675}
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700676
Andy Zhou971427f32014-09-15 19:37:25 -0700677int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key)
678{
679 return key_extract(skb, key);
680}
681
Thomas Graf12eb18f2014-11-06 06:58:52 -0800682int ovs_flow_key_extract(const struct ovs_tunnel_info *tun_info,
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700683 struct sk_buff *skb, struct sw_flow_key *key)
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700684{
685 /* Extract metadata from packet. */
Jesse Grossf5796682014-10-03 15:35:33 -0700686 if (tun_info) {
Jesse Grossf0b128c2014-10-03 15:35:31 -0700687 memcpy(&key->tun_key, &tun_info->tunnel, sizeof(key->tun_key));
Jesse Grossf5796682014-10-03 15:35:33 -0700688
689 if (tun_info->options) {
690 BUILD_BUG_ON((1 << (sizeof(tun_info->options_len) *
691 8)) - 1
692 > sizeof(key->tun_opts));
693 memcpy(GENEVE_OPTS(key, tun_info->options_len),
694 tun_info->options, tun_info->options_len);
695 key->tun_opts_len = tun_info->options_len;
696 } else {
697 key->tun_opts_len = 0;
698 }
699 } else {
700 key->tun_opts_len = 0;
Jesse Gross07148122014-10-03 15:35:29 -0700701 memset(&key->tun_key, 0, sizeof(key->tun_key));
Jesse Grossf5796682014-10-03 15:35:33 -0700702 }
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700703
704 key->phy.priority = skb->priority;
705 key->phy.in_port = OVS_CB(skb)->input_vport->port_no;
706 key->phy.skb_mark = skb->mark;
Jesse Gross07148122014-10-03 15:35:29 -0700707 key->ovs_flow_hash = 0;
708 key->recirc_id = 0;
709
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700710 return key_extract(skb, key);
711}
712
713int ovs_flow_key_extract_userspace(const struct nlattr *attr,
714 struct sk_buff *skb,
715 struct sw_flow_key *key)
716{
717 int err;
718
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700719 /* Extract metadata from netlink attributes. */
720 err = ovs_nla_get_flow_metadata(attr, key);
721 if (err)
722 return err;
723
724 return key_extract(skb, key);
725}