blob: 976a8b766a6a6bc50d71df10b74b2d6f9850a5a0 [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Raju Subramaniancaf2ee12012-05-03 18:55:23 -07002 * Copyright (c) 2007-2011 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>
37#include <linux/tcp.h>
38#include <linux/udp.h>
39#include <linux/icmp.h>
40#include <linux/icmpv6.h>
41#include <linux/rculist.h>
42#include <net/ip.h>
Pravin B Shelar7d5437c2013-06-17 17:50:18 -070043#include <net/ip_tunnels.h>
Jesse Grossccb13522011-10-25 19:26:31 -070044#include <net/ipv6.h>
45#include <net/ndisc.h>
46
47static struct kmem_cache *flow_cache;
48
49static int check_header(struct sk_buff *skb, int len)
50{
51 if (unlikely(skb->len < len))
52 return -EINVAL;
53 if (unlikely(!pskb_may_pull(skb, len)))
54 return -ENOMEM;
55 return 0;
56}
57
58static bool arphdr_ok(struct sk_buff *skb)
59{
60 return pskb_may_pull(skb, skb_network_offset(skb) +
61 sizeof(struct arp_eth_header));
62}
63
64static int check_iphdr(struct sk_buff *skb)
65{
66 unsigned int nh_ofs = skb_network_offset(skb);
67 unsigned int ip_len;
68 int err;
69
70 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
71 if (unlikely(err))
72 return err;
73
74 ip_len = ip_hdrlen(skb);
75 if (unlikely(ip_len < sizeof(struct iphdr) ||
76 skb->len < nh_ofs + ip_len))
77 return -EINVAL;
78
79 skb_set_transport_header(skb, nh_ofs + ip_len);
80 return 0;
81}
82
83static bool tcphdr_ok(struct sk_buff *skb)
84{
85 int th_ofs = skb_transport_offset(skb);
86 int tcp_len;
87
88 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
89 return false;
90
91 tcp_len = tcp_hdrlen(skb);
92 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
93 skb->len < th_ofs + tcp_len))
94 return false;
95
96 return true;
97}
98
99static bool udphdr_ok(struct sk_buff *skb)
100{
101 return pskb_may_pull(skb, skb_transport_offset(skb) +
102 sizeof(struct udphdr));
103}
104
105static bool icmphdr_ok(struct sk_buff *skb)
106{
107 return pskb_may_pull(skb, skb_transport_offset(skb) +
108 sizeof(struct icmphdr));
109}
110
111u64 ovs_flow_used_time(unsigned long flow_jiffies)
112{
113 struct timespec cur_ts;
114 u64 cur_ms, idle_ms;
115
116 ktime_get_ts(&cur_ts);
117 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
118 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
119 cur_ts.tv_nsec / NSEC_PER_MSEC;
120
121 return cur_ms - idle_ms;
122}
123
124#define SW_FLOW_KEY_OFFSET(field) \
125 (offsetof(struct sw_flow_key, field) + \
126 FIELD_SIZEOF(struct sw_flow_key, field))
127
128static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key,
129 int *key_lenp)
130{
131 unsigned int nh_ofs = skb_network_offset(skb);
132 unsigned int nh_len;
133 int payload_ofs;
134 struct ipv6hdr *nh;
135 uint8_t nexthdr;
136 __be16 frag_off;
137 int err;
138
139 *key_lenp = SW_FLOW_KEY_OFFSET(ipv6.label);
140
141 err = check_header(skb, nh_ofs + sizeof(*nh));
142 if (unlikely(err))
143 return err;
144
145 nh = ipv6_hdr(skb);
146 nexthdr = nh->nexthdr;
147 payload_ofs = (u8 *)(nh + 1) - skb->data;
148
149 key->ip.proto = NEXTHDR_NONE;
150 key->ip.tos = ipv6_get_dsfield(nh);
151 key->ip.ttl = nh->hop_limit;
152 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
153 key->ipv6.addr.src = nh->saddr;
154 key->ipv6.addr.dst = nh->daddr;
155
156 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
157 if (unlikely(payload_ofs < 0))
158 return -EINVAL;
159
160 if (frag_off) {
161 if (frag_off & htons(~0x7))
162 key->ip.frag = OVS_FRAG_TYPE_LATER;
163 else
164 key->ip.frag = OVS_FRAG_TYPE_FIRST;
165 }
166
167 nh_len = payload_ofs - nh_ofs;
168 skb_set_transport_header(skb, nh_ofs + nh_len);
169 key->ip.proto = nexthdr;
170 return nh_len;
171}
172
173static bool icmp6hdr_ok(struct sk_buff *skb)
174{
175 return pskb_may_pull(skb, skb_transport_offset(skb) +
176 sizeof(struct icmp6hdr));
177}
178
179#define TCP_FLAGS_OFFSET 13
180#define TCP_FLAG_MASK 0x3f
181
182void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
183{
184 u8 tcp_flags = 0;
185
Jesse Grossc55177e2012-04-02 15:13:36 -0700186 if ((flow->key.eth.type == htons(ETH_P_IP) ||
187 flow->key.eth.type == htons(ETH_P_IPV6)) &&
Jesse Grossbf32fec2012-04-02 14:26:27 -0700188 flow->key.ip.proto == IPPROTO_TCP &&
189 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
Jesse Grossccb13522011-10-25 19:26:31 -0700190 u8 *tcp = (u8 *)tcp_hdr(skb);
191 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
192 }
193
194 spin_lock(&flow->lock);
195 flow->used = jiffies;
196 flow->packet_count++;
197 flow->byte_count += skb->len;
198 flow->tcp_flags |= tcp_flags;
199 spin_unlock(&flow->lock);
200}
201
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700202struct sw_flow_actions *ovs_flow_actions_alloc(int size)
Jesse Grossccb13522011-10-25 19:26:31 -0700203{
Jesse Grossccb13522011-10-25 19:26:31 -0700204 struct sw_flow_actions *sfa;
205
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700206 if (size > MAX_ACTIONS_BUFSIZE)
Jesse Grossccb13522011-10-25 19:26:31 -0700207 return ERR_PTR(-EINVAL);
208
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700209 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -0700210 if (!sfa)
211 return ERR_PTR(-ENOMEM);
212
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700213 sfa->actions_len = 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700214 return sfa;
215}
216
217struct sw_flow *ovs_flow_alloc(void)
218{
219 struct sw_flow *flow;
220
221 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
222 if (!flow)
223 return ERR_PTR(-ENOMEM);
224
225 spin_lock_init(&flow->lock);
226 flow->sf_acts = NULL;
227
228 return flow;
229}
230
231static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
232{
233 hash = jhash_1word(hash, table->hash_seed);
234 return flex_array_get(table->buckets,
235 (hash & (table->n_buckets - 1)));
236}
237
238static struct flex_array *alloc_buckets(unsigned int n_buckets)
239{
240 struct flex_array *buckets;
241 int i, err;
242
243 buckets = flex_array_alloc(sizeof(struct hlist_head *),
244 n_buckets, GFP_KERNEL);
245 if (!buckets)
246 return NULL;
247
248 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
249 if (err) {
250 flex_array_free(buckets);
251 return NULL;
252 }
253
254 for (i = 0; i < n_buckets; i++)
255 INIT_HLIST_HEAD((struct hlist_head *)
256 flex_array_get(buckets, i));
257
258 return buckets;
259}
260
261static void free_buckets(struct flex_array *buckets)
262{
263 flex_array_free(buckets);
264}
265
266struct flow_table *ovs_flow_tbl_alloc(int new_size)
267{
268 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
269
270 if (!table)
271 return NULL;
272
273 table->buckets = alloc_buckets(new_size);
274
275 if (!table->buckets) {
276 kfree(table);
277 return NULL;
278 }
279 table->n_buckets = new_size;
280 table->count = 0;
281 table->node_ver = 0;
282 table->keep_flows = false;
283 get_random_bytes(&table->hash_seed, sizeof(u32));
284
285 return table;
286}
287
288void ovs_flow_tbl_destroy(struct flow_table *table)
289{
290 int i;
291
292 if (!table)
293 return;
294
295 if (table->keep_flows)
296 goto skip_flows;
297
298 for (i = 0; i < table->n_buckets; i++) {
299 struct sw_flow *flow;
300 struct hlist_head *head = flex_array_get(table->buckets, i);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800301 struct hlist_node *n;
Jesse Grossccb13522011-10-25 19:26:31 -0700302 int ver = table->node_ver;
303
Sasha Levinb67bfe02013-02-27 17:06:00 -0800304 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
Jesse Grossccb13522011-10-25 19:26:31 -0700305 hlist_del_rcu(&flow->hash_node[ver]);
306 ovs_flow_free(flow);
307 }
308 }
309
310skip_flows:
311 free_buckets(table->buckets);
312 kfree(table);
313}
314
315static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
316{
317 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
318
319 ovs_flow_tbl_destroy(table);
320}
321
322void ovs_flow_tbl_deferred_destroy(struct flow_table *table)
323{
324 if (!table)
325 return;
326
327 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
328}
329
330struct sw_flow *ovs_flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *last)
331{
332 struct sw_flow *flow;
333 struct hlist_head *head;
Jesse Grossccb13522011-10-25 19:26:31 -0700334 int ver;
335 int i;
336
337 ver = table->node_ver;
338 while (*bucket < table->n_buckets) {
339 i = 0;
340 head = flex_array_get(table->buckets, *bucket);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800341 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
Jesse Grossccb13522011-10-25 19:26:31 -0700342 if (i < *last) {
343 i++;
344 continue;
345 }
346 *last = i + 1;
347 return flow;
348 }
349 (*bucket)++;
350 *last = 0;
351 }
352
353 return NULL;
354}
355
356static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
357{
358 int old_ver;
359 int i;
360
361 old_ver = old->node_ver;
362 new->node_ver = !old_ver;
363
364 /* Insert in new table. */
365 for (i = 0; i < old->n_buckets; i++) {
366 struct sw_flow *flow;
367 struct hlist_head *head;
Jesse Grossccb13522011-10-25 19:26:31 -0700368
369 head = flex_array_get(old->buckets, i);
370
Sasha Levinb67bfe02013-02-27 17:06:00 -0800371 hlist_for_each_entry(flow, head, hash_node[old_ver])
Jesse Grossccb13522011-10-25 19:26:31 -0700372 ovs_flow_tbl_insert(new, flow);
373 }
374 old->keep_flows = true;
375}
376
377static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
378{
379 struct flow_table *new_table;
380
381 new_table = ovs_flow_tbl_alloc(n_buckets);
382 if (!new_table)
383 return ERR_PTR(-ENOMEM);
384
385 flow_table_copy_flows(table, new_table);
386
387 return new_table;
388}
389
390struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
391{
392 return __flow_tbl_rehash(table, table->n_buckets);
393}
394
395struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
396{
397 return __flow_tbl_rehash(table, table->n_buckets * 2);
398}
399
400void ovs_flow_free(struct sw_flow *flow)
401{
402 if (unlikely(!flow))
403 return;
404
405 kfree((struct sf_flow_acts __force *)flow->sf_acts);
406 kmem_cache_free(flow_cache, flow);
407}
408
409/* RCU callback used by ovs_flow_deferred_free. */
410static void rcu_free_flow_callback(struct rcu_head *rcu)
411{
412 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
413
414 ovs_flow_free(flow);
415}
416
417/* Schedules 'flow' to be freed after the next RCU grace period.
418 * The caller must hold rcu_read_lock for this to be sensible. */
419void ovs_flow_deferred_free(struct sw_flow *flow)
420{
421 call_rcu(&flow->rcu, rcu_free_flow_callback);
422}
423
Jesse Grossccb13522011-10-25 19:26:31 -0700424/* Schedules 'sf_acts' to be freed after the next RCU grace period.
425 * The caller must hold rcu_read_lock for this to be sensible. */
426void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
427{
Wei Yongjun80f0fd82012-08-26 18:20:45 +0000428 kfree_rcu(sf_acts, rcu);
Jesse Grossccb13522011-10-25 19:26:31 -0700429}
430
431static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
432{
433 struct qtag_prefix {
434 __be16 eth_type; /* ETH_P_8021Q */
435 __be16 tci;
436 };
437 struct qtag_prefix *qp;
438
439 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
440 return 0;
441
442 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
443 sizeof(__be16))))
444 return -ENOMEM;
445
446 qp = (struct qtag_prefix *) skb->data;
447 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
448 __skb_pull(skb, sizeof(struct qtag_prefix));
449
450 return 0;
451}
452
453static __be16 parse_ethertype(struct sk_buff *skb)
454{
455 struct llc_snap_hdr {
456 u8 dsap; /* Always 0xAA */
457 u8 ssap; /* Always 0xAA */
458 u8 ctrl;
459 u8 oui[3];
460 __be16 ethertype;
461 };
462 struct llc_snap_hdr *llc;
463 __be16 proto;
464
465 proto = *(__be16 *) skb->data;
466 __skb_pull(skb, sizeof(__be16));
467
Simon Hormane5c5d222013-03-28 13:38:25 +0900468 if (ntohs(proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700469 return proto;
470
471 if (skb->len < sizeof(struct llc_snap_hdr))
472 return htons(ETH_P_802_2);
473
474 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
475 return htons(0);
476
477 llc = (struct llc_snap_hdr *) skb->data;
478 if (llc->dsap != LLC_SAP_SNAP ||
479 llc->ssap != LLC_SAP_SNAP ||
480 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
481 return htons(ETH_P_802_2);
482
483 __skb_pull(skb, sizeof(struct llc_snap_hdr));
Rich Lane17b682a2013-02-19 11:10:30 -0800484
Simon Hormane5c5d222013-03-28 13:38:25 +0900485 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
Rich Lane17b682a2013-02-19 11:10:30 -0800486 return llc->ethertype;
487
488 return htons(ETH_P_802_2);
Jesse Grossccb13522011-10-25 19:26:31 -0700489}
490
491static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
492 int *key_lenp, int nh_len)
493{
494 struct icmp6hdr *icmp = icmp6_hdr(skb);
495 int error = 0;
496 int key_len;
497
498 /* The ICMPv6 type and code fields use the 16-bit transport port
499 * fields, so we need to store them in 16-bit network byte order.
500 */
501 key->ipv6.tp.src = htons(icmp->icmp6_type);
502 key->ipv6.tp.dst = htons(icmp->icmp6_code);
503 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
504
505 if (icmp->icmp6_code == 0 &&
506 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
507 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
508 int icmp_len = skb->len - skb_transport_offset(skb);
509 struct nd_msg *nd;
510 int offset;
511
512 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
513
514 /* In order to process neighbor discovery options, we need the
515 * entire packet.
516 */
517 if (unlikely(icmp_len < sizeof(*nd)))
518 goto out;
519 if (unlikely(skb_linearize(skb))) {
520 error = -ENOMEM;
521 goto out;
522 }
523
524 nd = (struct nd_msg *)skb_transport_header(skb);
525 key->ipv6.nd.target = nd->target;
526 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
527
528 icmp_len -= sizeof(*nd);
529 offset = 0;
530 while (icmp_len >= 8) {
531 struct nd_opt_hdr *nd_opt =
532 (struct nd_opt_hdr *)(nd->opt + offset);
533 int opt_len = nd_opt->nd_opt_len * 8;
534
535 if (unlikely(!opt_len || opt_len > icmp_len))
536 goto invalid;
537
538 /* Store the link layer address if the appropriate
539 * option is provided. It is considered an error if
540 * the same link layer option is specified twice.
541 */
542 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
543 && opt_len == 8) {
544 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
545 goto invalid;
546 memcpy(key->ipv6.nd.sll,
547 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
548 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
549 && opt_len == 8) {
550 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
551 goto invalid;
552 memcpy(key->ipv6.nd.tll,
553 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
554 }
555
556 icmp_len -= opt_len;
557 offset += opt_len;
558 }
559 }
560
561 goto out;
562
563invalid:
564 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
565 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
566 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
567
568out:
569 *key_lenp = key_len;
570 return error;
571}
572
573/**
574 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
575 * @skb: sk_buff that contains the frame, with skb->data pointing to the
576 * Ethernet header
577 * @in_port: port number on which @skb was received.
578 * @key: output flow key
579 * @key_lenp: length of output flow key
580 *
581 * The caller must ensure that skb->len >= ETH_HLEN.
582 *
583 * Returns 0 if successful, otherwise a negative errno value.
584 *
585 * Initializes @skb header pointers as follows:
586 *
587 * - skb->mac_header: the Ethernet header.
588 *
589 * - skb->network_header: just past the Ethernet header, or just past the
590 * VLAN header, to the first byte of the Ethernet payload.
591 *
Lorand Jakab34d94f22013-06-03 10:01:14 -0700592 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
Jesse Grossccb13522011-10-25 19:26:31 -0700593 * on output, then just past the IP header, if one is present and
594 * of a correct length, otherwise the same as skb->network_header.
Lorand Jakab34d94f22013-06-03 10:01:14 -0700595 * For other key->eth.type values it is left untouched.
Jesse Grossccb13522011-10-25 19:26:31 -0700596 */
597int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
598 int *key_lenp)
599{
600 int error = 0;
601 int key_len = SW_FLOW_KEY_OFFSET(eth);
602 struct ethhdr *eth;
603
604 memset(key, 0, sizeof(*key));
605
606 key->phy.priority = skb->priority;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700607 if (OVS_CB(skb)->tun_key)
608 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
Jesse Grossccb13522011-10-25 19:26:31 -0700609 key->phy.in_port = in_port;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800610 key->phy.skb_mark = skb->mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700611
612 skb_reset_mac_header(skb);
613
614 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
615 * header in the linear data area.
616 */
617 eth = eth_hdr(skb);
618 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
619 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
620
621 __skb_pull(skb, 2 * ETH_ALEN);
Pravin B Shelarb34df5e2013-06-13 11:11:44 -0700622 /* We are going to push all headers that we pull, so no need to
623 * update skb->csum here.
624 */
Jesse Grossccb13522011-10-25 19:26:31 -0700625
626 if (vlan_tx_tag_present(skb))
627 key->eth.tci = htons(skb->vlan_tci);
628 else if (eth->h_proto == htons(ETH_P_8021Q))
629 if (unlikely(parse_vlan(skb, key)))
630 return -ENOMEM;
631
632 key->eth.type = parse_ethertype(skb);
633 if (unlikely(key->eth.type == htons(0)))
634 return -ENOMEM;
635
636 skb_reset_network_header(skb);
637 __skb_push(skb, skb->data - skb_mac_header(skb));
638
639 /* Network layer. */
640 if (key->eth.type == htons(ETH_P_IP)) {
641 struct iphdr *nh;
642 __be16 offset;
643
644 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
645
646 error = check_iphdr(skb);
647 if (unlikely(error)) {
648 if (error == -EINVAL) {
649 skb->transport_header = skb->network_header;
650 error = 0;
651 }
652 goto out;
653 }
654
655 nh = ip_hdr(skb);
656 key->ipv4.addr.src = nh->saddr;
657 key->ipv4.addr.dst = nh->daddr;
658
659 key->ip.proto = nh->protocol;
660 key->ip.tos = nh->tos;
661 key->ip.ttl = nh->ttl;
662
663 offset = nh->frag_off & htons(IP_OFFSET);
664 if (offset) {
665 key->ip.frag = OVS_FRAG_TYPE_LATER;
666 goto out;
667 }
668 if (nh->frag_off & htons(IP_MF) ||
669 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
670 key->ip.frag = OVS_FRAG_TYPE_FIRST;
671
672 /* Transport layer. */
673 if (key->ip.proto == IPPROTO_TCP) {
674 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
675 if (tcphdr_ok(skb)) {
676 struct tcphdr *tcp = tcp_hdr(skb);
677 key->ipv4.tp.src = tcp->source;
678 key->ipv4.tp.dst = tcp->dest;
679 }
680 } else if (key->ip.proto == IPPROTO_UDP) {
681 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
682 if (udphdr_ok(skb)) {
683 struct udphdr *udp = udp_hdr(skb);
684 key->ipv4.tp.src = udp->source;
685 key->ipv4.tp.dst = udp->dest;
686 }
687 } else if (key->ip.proto == IPPROTO_ICMP) {
688 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
689 if (icmphdr_ok(skb)) {
690 struct icmphdr *icmp = icmp_hdr(skb);
691 /* The ICMP type and code fields use the 16-bit
692 * transport port fields, so we need to store
693 * them in 16-bit network byte order. */
694 key->ipv4.tp.src = htons(icmp->type);
695 key->ipv4.tp.dst = htons(icmp->code);
696 }
697 }
698
Mehak Mahajanc0618532012-11-02 14:14:31 -0700699 } else if ((key->eth.type == htons(ETH_P_ARP) ||
700 key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
Jesse Grossccb13522011-10-25 19:26:31 -0700701 struct arp_eth_header *arp;
702
703 arp = (struct arp_eth_header *)skb_network_header(skb);
704
705 if (arp->ar_hrd == htons(ARPHRD_ETHER)
706 && arp->ar_pro == htons(ETH_P_IP)
707 && arp->ar_hln == ETH_ALEN
708 && arp->ar_pln == 4) {
709
710 /* We only match on the lower 8 bits of the opcode. */
711 if (ntohs(arp->ar_op) <= 0xff)
712 key->ip.proto = ntohs(arp->ar_op);
Mehak Mahajand04d3822012-10-30 15:50:28 -0700713 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
714 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
715 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
716 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
717 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
Jesse Grossccb13522011-10-25 19:26:31 -0700718 }
719 } else if (key->eth.type == htons(ETH_P_IPV6)) {
720 int nh_len; /* IPv6 Header + Extensions */
721
722 nh_len = parse_ipv6hdr(skb, key, &key_len);
723 if (unlikely(nh_len < 0)) {
724 if (nh_len == -EINVAL)
725 skb->transport_header = skb->network_header;
726 else
727 error = nh_len;
728 goto out;
729 }
730
731 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
732 goto out;
733 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
734 key->ip.frag = OVS_FRAG_TYPE_FIRST;
735
736 /* Transport layer. */
737 if (key->ip.proto == NEXTHDR_TCP) {
738 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
739 if (tcphdr_ok(skb)) {
740 struct tcphdr *tcp = tcp_hdr(skb);
741 key->ipv6.tp.src = tcp->source;
742 key->ipv6.tp.dst = tcp->dest;
743 }
744 } else if (key->ip.proto == NEXTHDR_UDP) {
745 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
746 if (udphdr_ok(skb)) {
747 struct udphdr *udp = udp_hdr(skb);
748 key->ipv6.tp.src = udp->source;
749 key->ipv6.tp.dst = udp->dest;
750 }
751 } else if (key->ip.proto == NEXTHDR_ICMP) {
752 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
753 if (icmp6hdr_ok(skb)) {
754 error = parse_icmpv6(skb, key, &key_len, nh_len);
755 if (error < 0)
756 goto out;
757 }
758 }
759 }
760
761out:
762 *key_lenp = key_len;
763 return error;
764}
765
766u32 ovs_flow_hash(const struct sw_flow_key *key, int key_len)
767{
768 return jhash2((u32 *)key, DIV_ROUND_UP(key_len, sizeof(u32)), 0);
769}
770
771struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table,
772 struct sw_flow_key *key, int key_len)
773{
774 struct sw_flow *flow;
Jesse Grossccb13522011-10-25 19:26:31 -0700775 struct hlist_head *head;
776 u32 hash;
777
778 hash = ovs_flow_hash(key, key_len);
779
780 head = find_bucket(table, hash);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800781 hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
Jesse Grossccb13522011-10-25 19:26:31 -0700782
783 if (flow->hash == hash &&
784 !memcmp(&flow->key, key, key_len)) {
785 return flow;
786 }
787 }
788 return NULL;
789}
790
791void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
792{
793 struct hlist_head *head;
794
795 head = find_bucket(table, flow->hash);
796 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
797 table->count++;
798}
799
800void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
801{
Hong Zhiguod3e11012013-03-27 20:41:17 +0800802 BUG_ON(table->count == 0);
Jesse Grossccb13522011-10-25 19:26:31 -0700803 hlist_del_rcu(&flow->hash_node[table->node_ver]);
804 table->count--;
Jesse Grossccb13522011-10-25 19:26:31 -0700805}
806
807/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
808const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
809 [OVS_KEY_ATTR_ENCAP] = -1,
810 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
811 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800812 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
Jesse Grossccb13522011-10-25 19:26:31 -0700813 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
814 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
815 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
816 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
817 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
818 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
819 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
820 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
821 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
822 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
823 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700824 [OVS_KEY_ATTR_TUNNEL] = -1,
Jesse Grossccb13522011-10-25 19:26:31 -0700825};
826
827static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
828 const struct nlattr *a[], u32 *attrs)
829{
830 const struct ovs_key_icmp *icmp_key;
831 const struct ovs_key_tcp *tcp_key;
832 const struct ovs_key_udp *udp_key;
833
834 switch (swkey->ip.proto) {
835 case IPPROTO_TCP:
836 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
837 return -EINVAL;
838 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
839
840 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
841 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
842 swkey->ipv4.tp.src = tcp_key->tcp_src;
843 swkey->ipv4.tp.dst = tcp_key->tcp_dst;
844 break;
845
846 case IPPROTO_UDP:
847 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
848 return -EINVAL;
849 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
850
851 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
852 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
853 swkey->ipv4.tp.src = udp_key->udp_src;
854 swkey->ipv4.tp.dst = udp_key->udp_dst;
855 break;
856
857 case IPPROTO_ICMP:
858 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
859 return -EINVAL;
860 *attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
861
862 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
863 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
864 swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
865 swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
866 break;
867 }
868
869 return 0;
870}
871
872static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
873 const struct nlattr *a[], u32 *attrs)
874{
875 const struct ovs_key_icmpv6 *icmpv6_key;
876 const struct ovs_key_tcp *tcp_key;
877 const struct ovs_key_udp *udp_key;
878
879 switch (swkey->ip.proto) {
880 case IPPROTO_TCP:
881 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
882 return -EINVAL;
883 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
884
885 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
886 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
887 swkey->ipv6.tp.src = tcp_key->tcp_src;
888 swkey->ipv6.tp.dst = tcp_key->tcp_dst;
889 break;
890
891 case IPPROTO_UDP:
892 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
893 return -EINVAL;
894 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
895
896 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
897 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
898 swkey->ipv6.tp.src = udp_key->udp_src;
899 swkey->ipv6.tp.dst = udp_key->udp_dst;
900 break;
901
902 case IPPROTO_ICMPV6:
903 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
904 return -EINVAL;
905 *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
906
907 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
908 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
909 swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
910 swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
911
912 if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
913 swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
914 const struct ovs_key_nd *nd_key;
915
916 if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
917 return -EINVAL;
918 *attrs &= ~(1 << OVS_KEY_ATTR_ND);
919
920 *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
921 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
922 memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
923 sizeof(swkey->ipv6.nd.target));
924 memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
925 memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
926 }
927 break;
928 }
929
930 return 0;
931}
932
933static int parse_flow_nlattrs(const struct nlattr *attr,
934 const struct nlattr *a[], u32 *attrsp)
935{
936 const struct nlattr *nla;
937 u32 attrs;
938 int rem;
939
940 attrs = 0;
941 nla_for_each_nested(nla, attr, rem) {
942 u16 type = nla_type(nla);
943 int expected_len;
944
945 if (type > OVS_KEY_ATTR_MAX || attrs & (1 << type))
946 return -EINVAL;
947
948 expected_len = ovs_key_lens[type];
949 if (nla_len(nla) != expected_len && expected_len != -1)
950 return -EINVAL;
951
952 attrs |= 1 << type;
953 a[type] = nla;
954 }
955 if (rem)
956 return -EINVAL;
957
958 *attrsp = attrs;
959 return 0;
960}
961
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700962int ovs_ipv4_tun_from_nlattr(const struct nlattr *attr,
963 struct ovs_key_ipv4_tunnel *tun_key)
964{
965 struct nlattr *a;
966 int rem;
967 bool ttl = false;
968
969 memset(tun_key, 0, sizeof(*tun_key));
970
971 nla_for_each_nested(a, attr, rem) {
972 int type = nla_type(a);
973 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
974 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
975 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
976 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
977 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
978 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
979 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
980 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
981 };
982
983 if (type > OVS_TUNNEL_KEY_ATTR_MAX ||
984 ovs_tunnel_key_lens[type] != nla_len(a))
985 return -EINVAL;
986
987 switch (type) {
988 case OVS_TUNNEL_KEY_ATTR_ID:
989 tun_key->tun_id = nla_get_be64(a);
990 tun_key->tun_flags |= TUNNEL_KEY;
991 break;
992 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
993 tun_key->ipv4_src = nla_get_be32(a);
994 break;
995 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
996 tun_key->ipv4_dst = nla_get_be32(a);
997 break;
998 case OVS_TUNNEL_KEY_ATTR_TOS:
999 tun_key->ipv4_tos = nla_get_u8(a);
1000 break;
1001 case OVS_TUNNEL_KEY_ATTR_TTL:
1002 tun_key->ipv4_ttl = nla_get_u8(a);
1003 ttl = true;
1004 break;
1005 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1006 tun_key->tun_flags |= TUNNEL_DONT_FRAGMENT;
1007 break;
1008 case OVS_TUNNEL_KEY_ATTR_CSUM:
1009 tun_key->tun_flags |= TUNNEL_CSUM;
1010 break;
1011 default:
1012 return -EINVAL;
1013
1014 }
1015 }
1016 if (rem > 0)
1017 return -EINVAL;
1018
1019 if (!tun_key->ipv4_dst)
1020 return -EINVAL;
1021
1022 if (!ttl)
1023 return -EINVAL;
1024
1025 return 0;
1026}
1027
1028int ovs_ipv4_tun_to_nlattr(struct sk_buff *skb,
1029 const struct ovs_key_ipv4_tunnel *tun_key)
1030{
1031 struct nlattr *nla;
1032
1033 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
1034 if (!nla)
1035 return -EMSGSIZE;
1036
1037 if (tun_key->tun_flags & TUNNEL_KEY &&
1038 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id))
1039 return -EMSGSIZE;
1040 if (tun_key->ipv4_src &&
1041 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ipv4_src))
1042 return -EMSGSIZE;
1043 if (nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ipv4_dst))
1044 return -EMSGSIZE;
1045 if (tun_key->ipv4_tos &&
1046 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ipv4_tos))
1047 return -EMSGSIZE;
1048 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ipv4_ttl))
1049 return -EMSGSIZE;
1050 if ((tun_key->tun_flags & TUNNEL_DONT_FRAGMENT) &&
1051 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
1052 return -EMSGSIZE;
1053 if ((tun_key->tun_flags & TUNNEL_CSUM) &&
1054 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
1055 return -EMSGSIZE;
1056
1057 nla_nest_end(skb, nla);
1058 return 0;
1059}
1060
Jesse Grossccb13522011-10-25 19:26:31 -07001061/**
1062 * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
1063 * @swkey: receives the extracted flow key.
1064 * @key_lenp: number of bytes used in @swkey.
1065 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1066 * sequence.
1067 */
1068int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
1069 const struct nlattr *attr)
1070{
1071 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1072 const struct ovs_key_ethernet *eth_key;
1073 int key_len;
1074 u32 attrs;
1075 int err;
1076
1077 memset(swkey, 0, sizeof(struct sw_flow_key));
1078 key_len = SW_FLOW_KEY_OFFSET(eth);
1079
1080 err = parse_flow_nlattrs(attr, a, &attrs);
1081 if (err)
1082 return err;
1083
1084 /* Metadata attributes. */
1085 if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1086 swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
1087 attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1088 }
1089 if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1090 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1091 if (in_port >= DP_MAX_PORTS)
1092 return -EINVAL;
1093 swkey->phy.in_port = in_port;
1094 attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1095 } else {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001096 swkey->phy.in_port = DP_MAX_PORTS;
Jesse Grossccb13522011-10-25 19:26:31 -07001097 }
Ansis Atteka39c7caeb2012-11-26 11:24:11 -08001098 if (attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1099 swkey->phy.skb_mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1100 attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1101 }
Jesse Grossccb13522011-10-25 19:26:31 -07001102
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001103 if (attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
1104 err = ovs_ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], &swkey->tun_key);
1105 if (err)
1106 return err;
1107
1108 attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
1109 }
1110
Jesse Grossccb13522011-10-25 19:26:31 -07001111 /* Data attributes. */
1112 if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
1113 return -EINVAL;
1114 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
1115
1116 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1117 memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
1118 memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
1119
1120 if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
1121 nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
1122 const struct nlattr *encap;
1123 __be16 tci;
1124
1125 if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
1126 (1 << OVS_KEY_ATTR_ETHERTYPE) |
1127 (1 << OVS_KEY_ATTR_ENCAP)))
1128 return -EINVAL;
1129
1130 encap = a[OVS_KEY_ATTR_ENCAP];
1131 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1132 if (tci & htons(VLAN_TAG_PRESENT)) {
1133 swkey->eth.tci = tci;
1134
1135 err = parse_flow_nlattrs(encap, a, &attrs);
1136 if (err)
1137 return err;
1138 } else if (!tci) {
1139 /* Corner case for truncated 802.1Q header. */
1140 if (nla_len(encap))
1141 return -EINVAL;
1142
1143 swkey->eth.type = htons(ETH_P_8021Q);
1144 *key_lenp = key_len;
1145 return 0;
1146 } else {
1147 return -EINVAL;
1148 }
1149 }
1150
1151 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1152 swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
Simon Hormane5c5d222013-03-28 13:38:25 +09001153 if (ntohs(swkey->eth.type) < ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -07001154 return -EINVAL;
1155 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1156 } else {
1157 swkey->eth.type = htons(ETH_P_802_2);
1158 }
1159
1160 if (swkey->eth.type == htons(ETH_P_IP)) {
1161 const struct ovs_key_ipv4 *ipv4_key;
1162
1163 if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
1164 return -EINVAL;
1165 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1166
1167 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
1168 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1169 if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
1170 return -EINVAL;
1171 swkey->ip.proto = ipv4_key->ipv4_proto;
1172 swkey->ip.tos = ipv4_key->ipv4_tos;
1173 swkey->ip.ttl = ipv4_key->ipv4_ttl;
1174 swkey->ip.frag = ipv4_key->ipv4_frag;
1175 swkey->ipv4.addr.src = ipv4_key->ipv4_src;
1176 swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
1177
1178 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1179 err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1180 if (err)
1181 return err;
1182 }
1183 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1184 const struct ovs_key_ipv6 *ipv6_key;
1185
1186 if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
1187 return -EINVAL;
1188 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1189
1190 key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
1191 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1192 if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
1193 return -EINVAL;
1194 swkey->ipv6.label = ipv6_key->ipv6_label;
1195 swkey->ip.proto = ipv6_key->ipv6_proto;
1196 swkey->ip.tos = ipv6_key->ipv6_tclass;
1197 swkey->ip.ttl = ipv6_key->ipv6_hlimit;
1198 swkey->ip.frag = ipv6_key->ipv6_frag;
1199 memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
1200 sizeof(swkey->ipv6.addr.src));
1201 memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
1202 sizeof(swkey->ipv6.addr.dst));
1203
1204 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1205 err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1206 if (err)
1207 return err;
1208 }
Mehak Mahajanc0618532012-11-02 14:14:31 -07001209 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1210 swkey->eth.type == htons(ETH_P_RARP)) {
Jesse Grossccb13522011-10-25 19:26:31 -07001211 const struct ovs_key_arp *arp_key;
1212
1213 if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
1214 return -EINVAL;
1215 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1216
1217 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
1218 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1219 swkey->ipv4.addr.src = arp_key->arp_sip;
1220 swkey->ipv4.addr.dst = arp_key->arp_tip;
1221 if (arp_key->arp_op & htons(0xff00))
1222 return -EINVAL;
1223 swkey->ip.proto = ntohs(arp_key->arp_op);
1224 memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
1225 memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
1226 }
1227
1228 if (attrs)
1229 return -EINVAL;
1230 *key_lenp = key_len;
1231
1232 return 0;
1233}
1234
1235/**
1236 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001237 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
1238 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
Jesse Grossccb13522011-10-25 19:26:31 -07001239 * sequence.
1240 *
1241 * This parses a series of Netlink attributes that form a flow key, which must
1242 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1243 * get the metadata, that is, the parts of the flow key that cannot be
1244 * extracted from the packet itself.
1245 */
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001246int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
1247 const struct nlattr *attr)
Jesse Grossccb13522011-10-25 19:26:31 -07001248{
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001249 struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
Jesse Grossccb13522011-10-25 19:26:31 -07001250 const struct nlattr *nla;
1251 int rem;
1252
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001253 flow->key.phy.in_port = DP_MAX_PORTS;
1254 flow->key.phy.priority = 0;
1255 flow->key.phy.skb_mark = 0;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001256 memset(tun_key, 0, sizeof(flow->key.tun_key));
Jesse Grossccb13522011-10-25 19:26:31 -07001257
1258 nla_for_each_nested(nla, attr, rem) {
1259 int type = nla_type(nla);
1260
1261 if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001262 int err;
1263
Jesse Grossccb13522011-10-25 19:26:31 -07001264 if (nla_len(nla) != ovs_key_lens[type])
1265 return -EINVAL;
1266
1267 switch (type) {
1268 case OVS_KEY_ATTR_PRIORITY:
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001269 flow->key.phy.priority = nla_get_u32(nla);
Jesse Grossccb13522011-10-25 19:26:31 -07001270 break;
1271
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001272 case OVS_KEY_ATTR_TUNNEL:
1273 err = ovs_ipv4_tun_from_nlattr(nla, tun_key);
1274 if (err)
1275 return err;
1276 break;
1277
Jesse Grossccb13522011-10-25 19:26:31 -07001278 case OVS_KEY_ATTR_IN_PORT:
1279 if (nla_get_u32(nla) >= DP_MAX_PORTS)
1280 return -EINVAL;
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001281 flow->key.phy.in_port = nla_get_u32(nla);
Jesse Grossccb13522011-10-25 19:26:31 -07001282 break;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -08001283
1284 case OVS_KEY_ATTR_SKB_MARK:
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001285 flow->key.phy.skb_mark = nla_get_u32(nla);
Ansis Atteka39c7caeb2012-11-26 11:24:11 -08001286 break;
Jesse Grossccb13522011-10-25 19:26:31 -07001287 }
1288 }
1289 }
1290 if (rem)
1291 return -EINVAL;
1292 return 0;
1293}
1294
1295int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
1296{
1297 struct ovs_key_ethernet *eth_key;
1298 struct nlattr *nla, *encap;
1299
David S. Miller028d6a62012-03-29 23:20:48 -04001300 if (swkey->phy.priority &&
1301 nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority))
1302 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001303
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001304 if (swkey->tun_key.ipv4_dst &&
1305 ovs_ipv4_tun_to_nlattr(skb, &swkey->tun_key))
1306 goto nla_put_failure;
1307
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001308 if (swkey->phy.in_port != DP_MAX_PORTS &&
David S. Miller028d6a62012-03-29 23:20:48 -04001309 nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port))
1310 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001311
Ansis Atteka39c7caeb2012-11-26 11:24:11 -08001312 if (swkey->phy.skb_mark &&
1313 nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, swkey->phy.skb_mark))
1314 goto nla_put_failure;
1315
Jesse Grossccb13522011-10-25 19:26:31 -07001316 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1317 if (!nla)
1318 goto nla_put_failure;
1319 eth_key = nla_data(nla);
1320 memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
1321 memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
1322
1323 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
David S. Miller028d6a62012-03-29 23:20:48 -04001324 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q)) ||
1325 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci))
1326 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001327 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1328 if (!swkey->eth.tci)
1329 goto unencap;
1330 } else {
1331 encap = NULL;
1332 }
1333
1334 if (swkey->eth.type == htons(ETH_P_802_2))
1335 goto unencap;
1336
David S. Miller028d6a62012-03-29 23:20:48 -04001337 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type))
1338 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001339
1340 if (swkey->eth.type == htons(ETH_P_IP)) {
1341 struct ovs_key_ipv4 *ipv4_key;
1342
1343 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1344 if (!nla)
1345 goto nla_put_failure;
1346 ipv4_key = nla_data(nla);
1347 ipv4_key->ipv4_src = swkey->ipv4.addr.src;
1348 ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
1349 ipv4_key->ipv4_proto = swkey->ip.proto;
1350 ipv4_key->ipv4_tos = swkey->ip.tos;
1351 ipv4_key->ipv4_ttl = swkey->ip.ttl;
1352 ipv4_key->ipv4_frag = swkey->ip.frag;
1353 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1354 struct ovs_key_ipv6 *ipv6_key;
1355
1356 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1357 if (!nla)
1358 goto nla_put_failure;
1359 ipv6_key = nla_data(nla);
1360 memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
1361 sizeof(ipv6_key->ipv6_src));
1362 memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
1363 sizeof(ipv6_key->ipv6_dst));
1364 ipv6_key->ipv6_label = swkey->ipv6.label;
1365 ipv6_key->ipv6_proto = swkey->ip.proto;
1366 ipv6_key->ipv6_tclass = swkey->ip.tos;
1367 ipv6_key->ipv6_hlimit = swkey->ip.ttl;
1368 ipv6_key->ipv6_frag = swkey->ip.frag;
Mehak Mahajanc0618532012-11-02 14:14:31 -07001369 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1370 swkey->eth.type == htons(ETH_P_RARP)) {
Jesse Grossccb13522011-10-25 19:26:31 -07001371 struct ovs_key_arp *arp_key;
1372
1373 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1374 if (!nla)
1375 goto nla_put_failure;
1376 arp_key = nla_data(nla);
1377 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1378 arp_key->arp_sip = swkey->ipv4.addr.src;
1379 arp_key->arp_tip = swkey->ipv4.addr.dst;
1380 arp_key->arp_op = htons(swkey->ip.proto);
1381 memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
1382 memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
1383 }
1384
1385 if ((swkey->eth.type == htons(ETH_P_IP) ||
1386 swkey->eth.type == htons(ETH_P_IPV6)) &&
1387 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1388
1389 if (swkey->ip.proto == IPPROTO_TCP) {
1390 struct ovs_key_tcp *tcp_key;
1391
1392 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1393 if (!nla)
1394 goto nla_put_failure;
1395 tcp_key = nla_data(nla);
1396 if (swkey->eth.type == htons(ETH_P_IP)) {
1397 tcp_key->tcp_src = swkey->ipv4.tp.src;
1398 tcp_key->tcp_dst = swkey->ipv4.tp.dst;
1399 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1400 tcp_key->tcp_src = swkey->ipv6.tp.src;
1401 tcp_key->tcp_dst = swkey->ipv6.tp.dst;
1402 }
1403 } else if (swkey->ip.proto == IPPROTO_UDP) {
1404 struct ovs_key_udp *udp_key;
1405
1406 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1407 if (!nla)
1408 goto nla_put_failure;
1409 udp_key = nla_data(nla);
1410 if (swkey->eth.type == htons(ETH_P_IP)) {
1411 udp_key->udp_src = swkey->ipv4.tp.src;
1412 udp_key->udp_dst = swkey->ipv4.tp.dst;
1413 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1414 udp_key->udp_src = swkey->ipv6.tp.src;
1415 udp_key->udp_dst = swkey->ipv6.tp.dst;
1416 }
1417 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1418 swkey->ip.proto == IPPROTO_ICMP) {
1419 struct ovs_key_icmp *icmp_key;
1420
1421 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1422 if (!nla)
1423 goto nla_put_failure;
1424 icmp_key = nla_data(nla);
1425 icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
1426 icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
1427 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1428 swkey->ip.proto == IPPROTO_ICMPV6) {
1429 struct ovs_key_icmpv6 *icmpv6_key;
1430
1431 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1432 sizeof(*icmpv6_key));
1433 if (!nla)
1434 goto nla_put_failure;
1435 icmpv6_key = nla_data(nla);
1436 icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
1437 icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
1438
1439 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1440 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1441 struct ovs_key_nd *nd_key;
1442
1443 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1444 if (!nla)
1445 goto nla_put_failure;
1446 nd_key = nla_data(nla);
1447 memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
1448 sizeof(nd_key->nd_target));
1449 memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
1450 memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
1451 }
1452 }
1453 }
1454
1455unencap:
1456 if (encap)
1457 nla_nest_end(skb, encap);
1458
1459 return 0;
1460
1461nla_put_failure:
1462 return -EMSGSIZE;
1463}
1464
1465/* Initializes the flow module.
1466 * Returns zero if successful or a negative error code. */
1467int ovs_flow_init(void)
1468{
1469 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1470 0, NULL);
1471 if (flow_cache == NULL)
1472 return -ENOMEM;
1473
1474 return 0;
1475}
1476
1477/* Uninitializes the flow module. */
1478void ovs_flow_exit(void)
1479{
1480 kmem_cache_destroy(flow_cache);
1481}