blob: 940d4b803ff52556855ce44864ac820897690cbb [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>
43#include <net/ipv6.h>
44#include <net/ndisc.h>
45
46static struct kmem_cache *flow_cache;
47
48static int check_header(struct sk_buff *skb, int len)
49{
50 if (unlikely(skb->len < len))
51 return -EINVAL;
52 if (unlikely(!pskb_may_pull(skb, len)))
53 return -ENOMEM;
54 return 0;
55}
56
57static bool arphdr_ok(struct sk_buff *skb)
58{
59 return pskb_may_pull(skb, skb_network_offset(skb) +
60 sizeof(struct arp_eth_header));
61}
62
63static int check_iphdr(struct sk_buff *skb)
64{
65 unsigned int nh_ofs = skb_network_offset(skb);
66 unsigned int ip_len;
67 int err;
68
69 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
70 if (unlikely(err))
71 return err;
72
73 ip_len = ip_hdrlen(skb);
74 if (unlikely(ip_len < sizeof(struct iphdr) ||
75 skb->len < nh_ofs + ip_len))
76 return -EINVAL;
77
78 skb_set_transport_header(skb, nh_ofs + ip_len);
79 return 0;
80}
81
82static bool tcphdr_ok(struct sk_buff *skb)
83{
84 int th_ofs = skb_transport_offset(skb);
85 int tcp_len;
86
87 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
88 return false;
89
90 tcp_len = tcp_hdrlen(skb);
91 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
92 skb->len < th_ofs + tcp_len))
93 return false;
94
95 return true;
96}
97
98static bool udphdr_ok(struct sk_buff *skb)
99{
100 return pskb_may_pull(skb, skb_transport_offset(skb) +
101 sizeof(struct udphdr));
102}
103
104static bool icmphdr_ok(struct sk_buff *skb)
105{
106 return pskb_may_pull(skb, skb_transport_offset(skb) +
107 sizeof(struct icmphdr));
108}
109
110u64 ovs_flow_used_time(unsigned long flow_jiffies)
111{
112 struct timespec cur_ts;
113 u64 cur_ms, idle_ms;
114
115 ktime_get_ts(&cur_ts);
116 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
117 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
118 cur_ts.tv_nsec / NSEC_PER_MSEC;
119
120 return cur_ms - idle_ms;
121}
122
123#define SW_FLOW_KEY_OFFSET(field) \
124 (offsetof(struct sw_flow_key, field) + \
125 FIELD_SIZEOF(struct sw_flow_key, field))
126
127static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key,
128 int *key_lenp)
129{
130 unsigned int nh_ofs = skb_network_offset(skb);
131 unsigned int nh_len;
132 int payload_ofs;
133 struct ipv6hdr *nh;
134 uint8_t nexthdr;
135 __be16 frag_off;
136 int err;
137
138 *key_lenp = SW_FLOW_KEY_OFFSET(ipv6.label);
139
140 err = check_header(skb, nh_ofs + sizeof(*nh));
141 if (unlikely(err))
142 return err;
143
144 nh = ipv6_hdr(skb);
145 nexthdr = nh->nexthdr;
146 payload_ofs = (u8 *)(nh + 1) - skb->data;
147
148 key->ip.proto = NEXTHDR_NONE;
149 key->ip.tos = ipv6_get_dsfield(nh);
150 key->ip.ttl = nh->hop_limit;
151 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
152 key->ipv6.addr.src = nh->saddr;
153 key->ipv6.addr.dst = nh->daddr;
154
155 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
156 if (unlikely(payload_ofs < 0))
157 return -EINVAL;
158
159 if (frag_off) {
160 if (frag_off & htons(~0x7))
161 key->ip.frag = OVS_FRAG_TYPE_LATER;
162 else
163 key->ip.frag = OVS_FRAG_TYPE_FIRST;
164 }
165
166 nh_len = payload_ofs - nh_ofs;
167 skb_set_transport_header(skb, nh_ofs + nh_len);
168 key->ip.proto = nexthdr;
169 return nh_len;
170}
171
172static bool icmp6hdr_ok(struct sk_buff *skb)
173{
174 return pskb_may_pull(skb, skb_transport_offset(skb) +
175 sizeof(struct icmp6hdr));
176}
177
178#define TCP_FLAGS_OFFSET 13
179#define TCP_FLAG_MASK 0x3f
180
181void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
182{
183 u8 tcp_flags = 0;
184
Jesse Grossc55177e2012-04-02 15:13:36 -0700185 if ((flow->key.eth.type == htons(ETH_P_IP) ||
186 flow->key.eth.type == htons(ETH_P_IPV6)) &&
Jesse Grossbf32fec2012-04-02 14:26:27 -0700187 flow->key.ip.proto == IPPROTO_TCP &&
188 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
Jesse Grossccb13522011-10-25 19:26:31 -0700189 u8 *tcp = (u8 *)tcp_hdr(skb);
190 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
191 }
192
193 spin_lock(&flow->lock);
194 flow->used = jiffies;
195 flow->packet_count++;
196 flow->byte_count += skb->len;
197 flow->tcp_flags |= tcp_flags;
198 spin_unlock(&flow->lock);
199}
200
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700201struct sw_flow_actions *ovs_flow_actions_alloc(int size)
Jesse Grossccb13522011-10-25 19:26:31 -0700202{
Jesse Grossccb13522011-10-25 19:26:31 -0700203 struct sw_flow_actions *sfa;
204
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700205 if (size > MAX_ACTIONS_BUFSIZE)
Jesse Grossccb13522011-10-25 19:26:31 -0700206 return ERR_PTR(-EINVAL);
207
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700208 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -0700209 if (!sfa)
210 return ERR_PTR(-ENOMEM);
211
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700212 sfa->actions_len = 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700213 return sfa;
214}
215
216struct sw_flow *ovs_flow_alloc(void)
217{
218 struct sw_flow *flow;
219
220 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
221 if (!flow)
222 return ERR_PTR(-ENOMEM);
223
224 spin_lock_init(&flow->lock);
225 flow->sf_acts = NULL;
226
227 return flow;
228}
229
230static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
231{
232 hash = jhash_1word(hash, table->hash_seed);
233 return flex_array_get(table->buckets,
234 (hash & (table->n_buckets - 1)));
235}
236
237static struct flex_array *alloc_buckets(unsigned int n_buckets)
238{
239 struct flex_array *buckets;
240 int i, err;
241
242 buckets = flex_array_alloc(sizeof(struct hlist_head *),
243 n_buckets, GFP_KERNEL);
244 if (!buckets)
245 return NULL;
246
247 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
248 if (err) {
249 flex_array_free(buckets);
250 return NULL;
251 }
252
253 for (i = 0; i < n_buckets; i++)
254 INIT_HLIST_HEAD((struct hlist_head *)
255 flex_array_get(buckets, i));
256
257 return buckets;
258}
259
260static void free_buckets(struct flex_array *buckets)
261{
262 flex_array_free(buckets);
263}
264
265struct flow_table *ovs_flow_tbl_alloc(int new_size)
266{
267 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
268
269 if (!table)
270 return NULL;
271
272 table->buckets = alloc_buckets(new_size);
273
274 if (!table->buckets) {
275 kfree(table);
276 return NULL;
277 }
278 table->n_buckets = new_size;
279 table->count = 0;
280 table->node_ver = 0;
281 table->keep_flows = false;
282 get_random_bytes(&table->hash_seed, sizeof(u32));
283
284 return table;
285}
286
287void ovs_flow_tbl_destroy(struct flow_table *table)
288{
289 int i;
290
291 if (!table)
292 return;
293
294 if (table->keep_flows)
295 goto skip_flows;
296
297 for (i = 0; i < table->n_buckets; i++) {
298 struct sw_flow *flow;
299 struct hlist_head *head = flex_array_get(table->buckets, i);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800300 struct hlist_node *n;
Jesse Grossccb13522011-10-25 19:26:31 -0700301 int ver = table->node_ver;
302
Sasha Levinb67bfe02013-02-27 17:06:00 -0800303 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
Jesse Grossccb13522011-10-25 19:26:31 -0700304 hlist_del_rcu(&flow->hash_node[ver]);
305 ovs_flow_free(flow);
306 }
307 }
308
309skip_flows:
310 free_buckets(table->buckets);
311 kfree(table);
312}
313
314static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
315{
316 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
317
318 ovs_flow_tbl_destroy(table);
319}
320
321void ovs_flow_tbl_deferred_destroy(struct flow_table *table)
322{
323 if (!table)
324 return;
325
326 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
327}
328
329struct sw_flow *ovs_flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *last)
330{
331 struct sw_flow *flow;
332 struct hlist_head *head;
Jesse Grossccb13522011-10-25 19:26:31 -0700333 int ver;
334 int i;
335
336 ver = table->node_ver;
337 while (*bucket < table->n_buckets) {
338 i = 0;
339 head = flex_array_get(table->buckets, *bucket);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800340 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
Jesse Grossccb13522011-10-25 19:26:31 -0700341 if (i < *last) {
342 i++;
343 continue;
344 }
345 *last = i + 1;
346 return flow;
347 }
348 (*bucket)++;
349 *last = 0;
350 }
351
352 return NULL;
353}
354
355static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
356{
357 int old_ver;
358 int i;
359
360 old_ver = old->node_ver;
361 new->node_ver = !old_ver;
362
363 /* Insert in new table. */
364 for (i = 0; i < old->n_buckets; i++) {
365 struct sw_flow *flow;
366 struct hlist_head *head;
Jesse Grossccb13522011-10-25 19:26:31 -0700367
368 head = flex_array_get(old->buckets, i);
369
Sasha Levinb67bfe02013-02-27 17:06:00 -0800370 hlist_for_each_entry(flow, head, hash_node[old_ver])
Jesse Grossccb13522011-10-25 19:26:31 -0700371 ovs_flow_tbl_insert(new, flow);
372 }
373 old->keep_flows = true;
374}
375
376static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
377{
378 struct flow_table *new_table;
379
380 new_table = ovs_flow_tbl_alloc(n_buckets);
381 if (!new_table)
382 return ERR_PTR(-ENOMEM);
383
384 flow_table_copy_flows(table, new_table);
385
386 return new_table;
387}
388
389struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
390{
391 return __flow_tbl_rehash(table, table->n_buckets);
392}
393
394struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
395{
396 return __flow_tbl_rehash(table, table->n_buckets * 2);
397}
398
399void ovs_flow_free(struct sw_flow *flow)
400{
401 if (unlikely(!flow))
402 return;
403
404 kfree((struct sf_flow_acts __force *)flow->sf_acts);
405 kmem_cache_free(flow_cache, flow);
406}
407
408/* RCU callback used by ovs_flow_deferred_free. */
409static void rcu_free_flow_callback(struct rcu_head *rcu)
410{
411 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
412
413 ovs_flow_free(flow);
414}
415
416/* Schedules 'flow' to be freed after the next RCU grace period.
417 * The caller must hold rcu_read_lock for this to be sensible. */
418void ovs_flow_deferred_free(struct sw_flow *flow)
419{
420 call_rcu(&flow->rcu, rcu_free_flow_callback);
421}
422
Jesse Grossccb13522011-10-25 19:26:31 -0700423/* Schedules 'sf_acts' to be freed after the next RCU grace period.
424 * The caller must hold rcu_read_lock for this to be sensible. */
425void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
426{
Wei Yongjun80f0fd82012-08-26 18:20:45 +0000427 kfree_rcu(sf_acts, rcu);
Jesse Grossccb13522011-10-25 19:26:31 -0700428}
429
430static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
431{
432 struct qtag_prefix {
433 __be16 eth_type; /* ETH_P_8021Q */
434 __be16 tci;
435 };
436 struct qtag_prefix *qp;
437
438 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
439 return 0;
440
441 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
442 sizeof(__be16))))
443 return -ENOMEM;
444
445 qp = (struct qtag_prefix *) skb->data;
446 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
447 __skb_pull(skb, sizeof(struct qtag_prefix));
448
449 return 0;
450}
451
452static __be16 parse_ethertype(struct sk_buff *skb)
453{
454 struct llc_snap_hdr {
455 u8 dsap; /* Always 0xAA */
456 u8 ssap; /* Always 0xAA */
457 u8 ctrl;
458 u8 oui[3];
459 __be16 ethertype;
460 };
461 struct llc_snap_hdr *llc;
462 __be16 proto;
463
464 proto = *(__be16 *) skb->data;
465 __skb_pull(skb, sizeof(__be16));
466
Simon Hormane5c5d222013-03-28 13:38:25 +0900467 if (ntohs(proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700468 return proto;
469
470 if (skb->len < sizeof(struct llc_snap_hdr))
471 return htons(ETH_P_802_2);
472
473 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
474 return htons(0);
475
476 llc = (struct llc_snap_hdr *) skb->data;
477 if (llc->dsap != LLC_SAP_SNAP ||
478 llc->ssap != LLC_SAP_SNAP ||
479 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
480 return htons(ETH_P_802_2);
481
482 __skb_pull(skb, sizeof(struct llc_snap_hdr));
Rich Lane17b682a2013-02-19 11:10:30 -0800483
Simon Hormane5c5d222013-03-28 13:38:25 +0900484 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
Rich Lane17b682a2013-02-19 11:10:30 -0800485 return llc->ethertype;
486
487 return htons(ETH_P_802_2);
Jesse Grossccb13522011-10-25 19:26:31 -0700488}
489
490static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
491 int *key_lenp, int nh_len)
492{
493 struct icmp6hdr *icmp = icmp6_hdr(skb);
494 int error = 0;
495 int key_len;
496
497 /* The ICMPv6 type and code fields use the 16-bit transport port
498 * fields, so we need to store them in 16-bit network byte order.
499 */
500 key->ipv6.tp.src = htons(icmp->icmp6_type);
501 key->ipv6.tp.dst = htons(icmp->icmp6_code);
502 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
503
504 if (icmp->icmp6_code == 0 &&
505 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
506 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
507 int icmp_len = skb->len - skb_transport_offset(skb);
508 struct nd_msg *nd;
509 int offset;
510
511 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
512
513 /* In order to process neighbor discovery options, we need the
514 * entire packet.
515 */
516 if (unlikely(icmp_len < sizeof(*nd)))
517 goto out;
518 if (unlikely(skb_linearize(skb))) {
519 error = -ENOMEM;
520 goto out;
521 }
522
523 nd = (struct nd_msg *)skb_transport_header(skb);
524 key->ipv6.nd.target = nd->target;
525 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
526
527 icmp_len -= sizeof(*nd);
528 offset = 0;
529 while (icmp_len >= 8) {
530 struct nd_opt_hdr *nd_opt =
531 (struct nd_opt_hdr *)(nd->opt + offset);
532 int opt_len = nd_opt->nd_opt_len * 8;
533
534 if (unlikely(!opt_len || opt_len > icmp_len))
535 goto invalid;
536
537 /* Store the link layer address if the appropriate
538 * option is provided. It is considered an error if
539 * the same link layer option is specified twice.
540 */
541 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
542 && opt_len == 8) {
543 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
544 goto invalid;
545 memcpy(key->ipv6.nd.sll,
546 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
547 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
548 && opt_len == 8) {
549 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
550 goto invalid;
551 memcpy(key->ipv6.nd.tll,
552 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
553 }
554
555 icmp_len -= opt_len;
556 offset += opt_len;
557 }
558 }
559
560 goto out;
561
562invalid:
563 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
564 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
565 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
566
567out:
568 *key_lenp = key_len;
569 return error;
570}
571
572/**
573 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
574 * @skb: sk_buff that contains the frame, with skb->data pointing to the
575 * Ethernet header
576 * @in_port: port number on which @skb was received.
577 * @key: output flow key
578 * @key_lenp: length of output flow key
579 *
580 * The caller must ensure that skb->len >= ETH_HLEN.
581 *
582 * Returns 0 if successful, otherwise a negative errno value.
583 *
584 * Initializes @skb header pointers as follows:
585 *
586 * - skb->mac_header: the Ethernet header.
587 *
588 * - skb->network_header: just past the Ethernet header, or just past the
589 * VLAN header, to the first byte of the Ethernet payload.
590 *
Lorand Jakab34d94f22013-06-03 10:01:14 -0700591 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
Jesse Grossccb13522011-10-25 19:26:31 -0700592 * on output, then just past the IP header, if one is present and
593 * of a correct length, otherwise the same as skb->network_header.
Lorand Jakab34d94f22013-06-03 10:01:14 -0700594 * For other key->eth.type values it is left untouched.
Jesse Grossccb13522011-10-25 19:26:31 -0700595 */
596int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
597 int *key_lenp)
598{
599 int error = 0;
600 int key_len = SW_FLOW_KEY_OFFSET(eth);
601 struct ethhdr *eth;
602
603 memset(key, 0, sizeof(*key));
604
605 key->phy.priority = skb->priority;
606 key->phy.in_port = in_port;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800607 key->phy.skb_mark = skb->mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700608
609 skb_reset_mac_header(skb);
610
611 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
612 * header in the linear data area.
613 */
614 eth = eth_hdr(skb);
615 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
616 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
617
618 __skb_pull(skb, 2 * ETH_ALEN);
Pravin B Shelarb34df5e2013-06-13 11:11:44 -0700619 /* We are going to push all headers that we pull, so no need to
620 * update skb->csum here.
621 */
Jesse Grossccb13522011-10-25 19:26:31 -0700622
623 if (vlan_tx_tag_present(skb))
624 key->eth.tci = htons(skb->vlan_tci);
625 else if (eth->h_proto == htons(ETH_P_8021Q))
626 if (unlikely(parse_vlan(skb, key)))
627 return -ENOMEM;
628
629 key->eth.type = parse_ethertype(skb);
630 if (unlikely(key->eth.type == htons(0)))
631 return -ENOMEM;
632
633 skb_reset_network_header(skb);
634 __skb_push(skb, skb->data - skb_mac_header(skb));
635
636 /* Network layer. */
637 if (key->eth.type == htons(ETH_P_IP)) {
638 struct iphdr *nh;
639 __be16 offset;
640
641 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
642
643 error = check_iphdr(skb);
644 if (unlikely(error)) {
645 if (error == -EINVAL) {
646 skb->transport_header = skb->network_header;
647 error = 0;
648 }
649 goto out;
650 }
651
652 nh = ip_hdr(skb);
653 key->ipv4.addr.src = nh->saddr;
654 key->ipv4.addr.dst = nh->daddr;
655
656 key->ip.proto = nh->protocol;
657 key->ip.tos = nh->tos;
658 key->ip.ttl = nh->ttl;
659
660 offset = nh->frag_off & htons(IP_OFFSET);
661 if (offset) {
662 key->ip.frag = OVS_FRAG_TYPE_LATER;
663 goto out;
664 }
665 if (nh->frag_off & htons(IP_MF) ||
666 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
667 key->ip.frag = OVS_FRAG_TYPE_FIRST;
668
669 /* Transport layer. */
670 if (key->ip.proto == IPPROTO_TCP) {
671 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
672 if (tcphdr_ok(skb)) {
673 struct tcphdr *tcp = tcp_hdr(skb);
674 key->ipv4.tp.src = tcp->source;
675 key->ipv4.tp.dst = tcp->dest;
676 }
677 } else if (key->ip.proto == IPPROTO_UDP) {
678 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
679 if (udphdr_ok(skb)) {
680 struct udphdr *udp = udp_hdr(skb);
681 key->ipv4.tp.src = udp->source;
682 key->ipv4.tp.dst = udp->dest;
683 }
684 } else if (key->ip.proto == IPPROTO_ICMP) {
685 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
686 if (icmphdr_ok(skb)) {
687 struct icmphdr *icmp = icmp_hdr(skb);
688 /* The ICMP type and code fields use the 16-bit
689 * transport port fields, so we need to store
690 * them in 16-bit network byte order. */
691 key->ipv4.tp.src = htons(icmp->type);
692 key->ipv4.tp.dst = htons(icmp->code);
693 }
694 }
695
Mehak Mahajanc0618532012-11-02 14:14:31 -0700696 } else if ((key->eth.type == htons(ETH_P_ARP) ||
697 key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
Jesse Grossccb13522011-10-25 19:26:31 -0700698 struct arp_eth_header *arp;
699
700 arp = (struct arp_eth_header *)skb_network_header(skb);
701
702 if (arp->ar_hrd == htons(ARPHRD_ETHER)
703 && arp->ar_pro == htons(ETH_P_IP)
704 && arp->ar_hln == ETH_ALEN
705 && arp->ar_pln == 4) {
706
707 /* We only match on the lower 8 bits of the opcode. */
708 if (ntohs(arp->ar_op) <= 0xff)
709 key->ip.proto = ntohs(arp->ar_op);
Mehak Mahajand04d3822012-10-30 15:50:28 -0700710 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
711 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
712 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
713 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
714 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
Jesse Grossccb13522011-10-25 19:26:31 -0700715 }
716 } else if (key->eth.type == htons(ETH_P_IPV6)) {
717 int nh_len; /* IPv6 Header + Extensions */
718
719 nh_len = parse_ipv6hdr(skb, key, &key_len);
720 if (unlikely(nh_len < 0)) {
721 if (nh_len == -EINVAL)
722 skb->transport_header = skb->network_header;
723 else
724 error = nh_len;
725 goto out;
726 }
727
728 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
729 goto out;
730 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
731 key->ip.frag = OVS_FRAG_TYPE_FIRST;
732
733 /* Transport layer. */
734 if (key->ip.proto == NEXTHDR_TCP) {
735 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
736 if (tcphdr_ok(skb)) {
737 struct tcphdr *tcp = tcp_hdr(skb);
738 key->ipv6.tp.src = tcp->source;
739 key->ipv6.tp.dst = tcp->dest;
740 }
741 } else if (key->ip.proto == NEXTHDR_UDP) {
742 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
743 if (udphdr_ok(skb)) {
744 struct udphdr *udp = udp_hdr(skb);
745 key->ipv6.tp.src = udp->source;
746 key->ipv6.tp.dst = udp->dest;
747 }
748 } else if (key->ip.proto == NEXTHDR_ICMP) {
749 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
750 if (icmp6hdr_ok(skb)) {
751 error = parse_icmpv6(skb, key, &key_len, nh_len);
752 if (error < 0)
753 goto out;
754 }
755 }
756 }
757
758out:
759 *key_lenp = key_len;
760 return error;
761}
762
763u32 ovs_flow_hash(const struct sw_flow_key *key, int key_len)
764{
765 return jhash2((u32 *)key, DIV_ROUND_UP(key_len, sizeof(u32)), 0);
766}
767
768struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table,
769 struct sw_flow_key *key, int key_len)
770{
771 struct sw_flow *flow;
Jesse Grossccb13522011-10-25 19:26:31 -0700772 struct hlist_head *head;
773 u32 hash;
774
775 hash = ovs_flow_hash(key, key_len);
776
777 head = find_bucket(table, hash);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800778 hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
Jesse Grossccb13522011-10-25 19:26:31 -0700779
780 if (flow->hash == hash &&
781 !memcmp(&flow->key, key, key_len)) {
782 return flow;
783 }
784 }
785 return NULL;
786}
787
788void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
789{
790 struct hlist_head *head;
791
792 head = find_bucket(table, flow->hash);
793 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
794 table->count++;
795}
796
797void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
798{
Hong Zhiguod3e11012013-03-27 20:41:17 +0800799 BUG_ON(table->count == 0);
Jesse Grossccb13522011-10-25 19:26:31 -0700800 hlist_del_rcu(&flow->hash_node[table->node_ver]);
801 table->count--;
Jesse Grossccb13522011-10-25 19:26:31 -0700802}
803
804/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
805const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
806 [OVS_KEY_ATTR_ENCAP] = -1,
807 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
808 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800809 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
Jesse Grossccb13522011-10-25 19:26:31 -0700810 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
811 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
812 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
813 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
814 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
815 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
816 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
817 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
818 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
819 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
820 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
821};
822
823static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
824 const struct nlattr *a[], u32 *attrs)
825{
826 const struct ovs_key_icmp *icmp_key;
827 const struct ovs_key_tcp *tcp_key;
828 const struct ovs_key_udp *udp_key;
829
830 switch (swkey->ip.proto) {
831 case IPPROTO_TCP:
832 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
833 return -EINVAL;
834 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
835
836 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
837 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
838 swkey->ipv4.tp.src = tcp_key->tcp_src;
839 swkey->ipv4.tp.dst = tcp_key->tcp_dst;
840 break;
841
842 case IPPROTO_UDP:
843 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
844 return -EINVAL;
845 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
846
847 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
848 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
849 swkey->ipv4.tp.src = udp_key->udp_src;
850 swkey->ipv4.tp.dst = udp_key->udp_dst;
851 break;
852
853 case IPPROTO_ICMP:
854 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
855 return -EINVAL;
856 *attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
857
858 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
859 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
860 swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
861 swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
862 break;
863 }
864
865 return 0;
866}
867
868static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
869 const struct nlattr *a[], u32 *attrs)
870{
871 const struct ovs_key_icmpv6 *icmpv6_key;
872 const struct ovs_key_tcp *tcp_key;
873 const struct ovs_key_udp *udp_key;
874
875 switch (swkey->ip.proto) {
876 case IPPROTO_TCP:
877 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
878 return -EINVAL;
879 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
880
881 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
882 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
883 swkey->ipv6.tp.src = tcp_key->tcp_src;
884 swkey->ipv6.tp.dst = tcp_key->tcp_dst;
885 break;
886
887 case IPPROTO_UDP:
888 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
889 return -EINVAL;
890 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
891
892 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
893 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
894 swkey->ipv6.tp.src = udp_key->udp_src;
895 swkey->ipv6.tp.dst = udp_key->udp_dst;
896 break;
897
898 case IPPROTO_ICMPV6:
899 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
900 return -EINVAL;
901 *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
902
903 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
904 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
905 swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
906 swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
907
908 if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
909 swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
910 const struct ovs_key_nd *nd_key;
911
912 if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
913 return -EINVAL;
914 *attrs &= ~(1 << OVS_KEY_ATTR_ND);
915
916 *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
917 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
918 memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
919 sizeof(swkey->ipv6.nd.target));
920 memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
921 memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
922 }
923 break;
924 }
925
926 return 0;
927}
928
929static int parse_flow_nlattrs(const struct nlattr *attr,
930 const struct nlattr *a[], u32 *attrsp)
931{
932 const struct nlattr *nla;
933 u32 attrs;
934 int rem;
935
936 attrs = 0;
937 nla_for_each_nested(nla, attr, rem) {
938 u16 type = nla_type(nla);
939 int expected_len;
940
941 if (type > OVS_KEY_ATTR_MAX || attrs & (1 << type))
942 return -EINVAL;
943
944 expected_len = ovs_key_lens[type];
945 if (nla_len(nla) != expected_len && expected_len != -1)
946 return -EINVAL;
947
948 attrs |= 1 << type;
949 a[type] = nla;
950 }
951 if (rem)
952 return -EINVAL;
953
954 *attrsp = attrs;
955 return 0;
956}
957
958/**
959 * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
960 * @swkey: receives the extracted flow key.
961 * @key_lenp: number of bytes used in @swkey.
962 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
963 * sequence.
964 */
965int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
966 const struct nlattr *attr)
967{
968 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
969 const struct ovs_key_ethernet *eth_key;
970 int key_len;
971 u32 attrs;
972 int err;
973
974 memset(swkey, 0, sizeof(struct sw_flow_key));
975 key_len = SW_FLOW_KEY_OFFSET(eth);
976
977 err = parse_flow_nlattrs(attr, a, &attrs);
978 if (err)
979 return err;
980
981 /* Metadata attributes. */
982 if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
983 swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
984 attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
985 }
986 if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
987 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
988 if (in_port >= DP_MAX_PORTS)
989 return -EINVAL;
990 swkey->phy.in_port = in_port;
991 attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
992 } else {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700993 swkey->phy.in_port = DP_MAX_PORTS;
Jesse Grossccb13522011-10-25 19:26:31 -0700994 }
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800995 if (attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
996 swkey->phy.skb_mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
997 attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
998 }
Jesse Grossccb13522011-10-25 19:26:31 -0700999
1000 /* Data attributes. */
1001 if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
1002 return -EINVAL;
1003 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
1004
1005 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1006 memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
1007 memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
1008
1009 if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
1010 nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
1011 const struct nlattr *encap;
1012 __be16 tci;
1013
1014 if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
1015 (1 << OVS_KEY_ATTR_ETHERTYPE) |
1016 (1 << OVS_KEY_ATTR_ENCAP)))
1017 return -EINVAL;
1018
1019 encap = a[OVS_KEY_ATTR_ENCAP];
1020 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1021 if (tci & htons(VLAN_TAG_PRESENT)) {
1022 swkey->eth.tci = tci;
1023
1024 err = parse_flow_nlattrs(encap, a, &attrs);
1025 if (err)
1026 return err;
1027 } else if (!tci) {
1028 /* Corner case for truncated 802.1Q header. */
1029 if (nla_len(encap))
1030 return -EINVAL;
1031
1032 swkey->eth.type = htons(ETH_P_8021Q);
1033 *key_lenp = key_len;
1034 return 0;
1035 } else {
1036 return -EINVAL;
1037 }
1038 }
1039
1040 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1041 swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
Simon Hormane5c5d222013-03-28 13:38:25 +09001042 if (ntohs(swkey->eth.type) < ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -07001043 return -EINVAL;
1044 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1045 } else {
1046 swkey->eth.type = htons(ETH_P_802_2);
1047 }
1048
1049 if (swkey->eth.type == htons(ETH_P_IP)) {
1050 const struct ovs_key_ipv4 *ipv4_key;
1051
1052 if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
1053 return -EINVAL;
1054 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1055
1056 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
1057 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1058 if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
1059 return -EINVAL;
1060 swkey->ip.proto = ipv4_key->ipv4_proto;
1061 swkey->ip.tos = ipv4_key->ipv4_tos;
1062 swkey->ip.ttl = ipv4_key->ipv4_ttl;
1063 swkey->ip.frag = ipv4_key->ipv4_frag;
1064 swkey->ipv4.addr.src = ipv4_key->ipv4_src;
1065 swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
1066
1067 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1068 err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1069 if (err)
1070 return err;
1071 }
1072 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1073 const struct ovs_key_ipv6 *ipv6_key;
1074
1075 if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
1076 return -EINVAL;
1077 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1078
1079 key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
1080 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1081 if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
1082 return -EINVAL;
1083 swkey->ipv6.label = ipv6_key->ipv6_label;
1084 swkey->ip.proto = ipv6_key->ipv6_proto;
1085 swkey->ip.tos = ipv6_key->ipv6_tclass;
1086 swkey->ip.ttl = ipv6_key->ipv6_hlimit;
1087 swkey->ip.frag = ipv6_key->ipv6_frag;
1088 memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
1089 sizeof(swkey->ipv6.addr.src));
1090 memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
1091 sizeof(swkey->ipv6.addr.dst));
1092
1093 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1094 err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1095 if (err)
1096 return err;
1097 }
Mehak Mahajanc0618532012-11-02 14:14:31 -07001098 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1099 swkey->eth.type == htons(ETH_P_RARP)) {
Jesse Grossccb13522011-10-25 19:26:31 -07001100 const struct ovs_key_arp *arp_key;
1101
1102 if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
1103 return -EINVAL;
1104 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1105
1106 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
1107 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1108 swkey->ipv4.addr.src = arp_key->arp_sip;
1109 swkey->ipv4.addr.dst = arp_key->arp_tip;
1110 if (arp_key->arp_op & htons(0xff00))
1111 return -EINVAL;
1112 swkey->ip.proto = ntohs(arp_key->arp_op);
1113 memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
1114 memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
1115 }
1116
1117 if (attrs)
1118 return -EINVAL;
1119 *key_lenp = key_len;
1120
1121 return 0;
1122}
1123
1124/**
1125 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001126 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
1127 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
Jesse Grossccb13522011-10-25 19:26:31 -07001128 * sequence.
1129 *
1130 * This parses a series of Netlink attributes that form a flow key, which must
1131 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1132 * get the metadata, that is, the parts of the flow key that cannot be
1133 * extracted from the packet itself.
1134 */
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001135int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
1136 const struct nlattr *attr)
Jesse Grossccb13522011-10-25 19:26:31 -07001137{
1138 const struct nlattr *nla;
1139 int rem;
1140
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001141 flow->key.phy.in_port = DP_MAX_PORTS;
1142 flow->key.phy.priority = 0;
1143 flow->key.phy.skb_mark = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001144
1145 nla_for_each_nested(nla, attr, rem) {
1146 int type = nla_type(nla);
1147
1148 if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
1149 if (nla_len(nla) != ovs_key_lens[type])
1150 return -EINVAL;
1151
1152 switch (type) {
1153 case OVS_KEY_ATTR_PRIORITY:
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001154 flow->key.phy.priority = nla_get_u32(nla);
Jesse Grossccb13522011-10-25 19:26:31 -07001155 break;
1156
1157 case OVS_KEY_ATTR_IN_PORT:
1158 if (nla_get_u32(nla) >= DP_MAX_PORTS)
1159 return -EINVAL;
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001160 flow->key.phy.in_port = nla_get_u32(nla);
Jesse Grossccb13522011-10-25 19:26:31 -07001161 break;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -08001162
1163 case OVS_KEY_ATTR_SKB_MARK:
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001164 flow->key.phy.skb_mark = nla_get_u32(nla);
Ansis Atteka39c7caeb2012-11-26 11:24:11 -08001165 break;
Jesse Grossccb13522011-10-25 19:26:31 -07001166 }
1167 }
1168 }
1169 if (rem)
1170 return -EINVAL;
1171 return 0;
1172}
1173
1174int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
1175{
1176 struct ovs_key_ethernet *eth_key;
1177 struct nlattr *nla, *encap;
1178
David S. Miller028d6a62012-03-29 23:20:48 -04001179 if (swkey->phy.priority &&
1180 nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority))
1181 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001182
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001183 if (swkey->phy.in_port != DP_MAX_PORTS &&
David S. Miller028d6a62012-03-29 23:20:48 -04001184 nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port))
1185 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001186
Ansis Atteka39c7caeb2012-11-26 11:24:11 -08001187 if (swkey->phy.skb_mark &&
1188 nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, swkey->phy.skb_mark))
1189 goto nla_put_failure;
1190
Jesse Grossccb13522011-10-25 19:26:31 -07001191 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1192 if (!nla)
1193 goto nla_put_failure;
1194 eth_key = nla_data(nla);
1195 memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
1196 memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
1197
1198 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
David S. Miller028d6a62012-03-29 23:20:48 -04001199 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q)) ||
1200 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci))
1201 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001202 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1203 if (!swkey->eth.tci)
1204 goto unencap;
1205 } else {
1206 encap = NULL;
1207 }
1208
1209 if (swkey->eth.type == htons(ETH_P_802_2))
1210 goto unencap;
1211
David S. Miller028d6a62012-03-29 23:20:48 -04001212 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type))
1213 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001214
1215 if (swkey->eth.type == htons(ETH_P_IP)) {
1216 struct ovs_key_ipv4 *ipv4_key;
1217
1218 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1219 if (!nla)
1220 goto nla_put_failure;
1221 ipv4_key = nla_data(nla);
1222 ipv4_key->ipv4_src = swkey->ipv4.addr.src;
1223 ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
1224 ipv4_key->ipv4_proto = swkey->ip.proto;
1225 ipv4_key->ipv4_tos = swkey->ip.tos;
1226 ipv4_key->ipv4_ttl = swkey->ip.ttl;
1227 ipv4_key->ipv4_frag = swkey->ip.frag;
1228 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1229 struct ovs_key_ipv6 *ipv6_key;
1230
1231 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1232 if (!nla)
1233 goto nla_put_failure;
1234 ipv6_key = nla_data(nla);
1235 memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
1236 sizeof(ipv6_key->ipv6_src));
1237 memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
1238 sizeof(ipv6_key->ipv6_dst));
1239 ipv6_key->ipv6_label = swkey->ipv6.label;
1240 ipv6_key->ipv6_proto = swkey->ip.proto;
1241 ipv6_key->ipv6_tclass = swkey->ip.tos;
1242 ipv6_key->ipv6_hlimit = swkey->ip.ttl;
1243 ipv6_key->ipv6_frag = swkey->ip.frag;
Mehak Mahajanc0618532012-11-02 14:14:31 -07001244 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1245 swkey->eth.type == htons(ETH_P_RARP)) {
Jesse Grossccb13522011-10-25 19:26:31 -07001246 struct ovs_key_arp *arp_key;
1247
1248 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1249 if (!nla)
1250 goto nla_put_failure;
1251 arp_key = nla_data(nla);
1252 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1253 arp_key->arp_sip = swkey->ipv4.addr.src;
1254 arp_key->arp_tip = swkey->ipv4.addr.dst;
1255 arp_key->arp_op = htons(swkey->ip.proto);
1256 memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
1257 memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
1258 }
1259
1260 if ((swkey->eth.type == htons(ETH_P_IP) ||
1261 swkey->eth.type == htons(ETH_P_IPV6)) &&
1262 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1263
1264 if (swkey->ip.proto == IPPROTO_TCP) {
1265 struct ovs_key_tcp *tcp_key;
1266
1267 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1268 if (!nla)
1269 goto nla_put_failure;
1270 tcp_key = nla_data(nla);
1271 if (swkey->eth.type == htons(ETH_P_IP)) {
1272 tcp_key->tcp_src = swkey->ipv4.tp.src;
1273 tcp_key->tcp_dst = swkey->ipv4.tp.dst;
1274 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1275 tcp_key->tcp_src = swkey->ipv6.tp.src;
1276 tcp_key->tcp_dst = swkey->ipv6.tp.dst;
1277 }
1278 } else if (swkey->ip.proto == IPPROTO_UDP) {
1279 struct ovs_key_udp *udp_key;
1280
1281 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1282 if (!nla)
1283 goto nla_put_failure;
1284 udp_key = nla_data(nla);
1285 if (swkey->eth.type == htons(ETH_P_IP)) {
1286 udp_key->udp_src = swkey->ipv4.tp.src;
1287 udp_key->udp_dst = swkey->ipv4.tp.dst;
1288 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1289 udp_key->udp_src = swkey->ipv6.tp.src;
1290 udp_key->udp_dst = swkey->ipv6.tp.dst;
1291 }
1292 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1293 swkey->ip.proto == IPPROTO_ICMP) {
1294 struct ovs_key_icmp *icmp_key;
1295
1296 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1297 if (!nla)
1298 goto nla_put_failure;
1299 icmp_key = nla_data(nla);
1300 icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
1301 icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
1302 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1303 swkey->ip.proto == IPPROTO_ICMPV6) {
1304 struct ovs_key_icmpv6 *icmpv6_key;
1305
1306 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1307 sizeof(*icmpv6_key));
1308 if (!nla)
1309 goto nla_put_failure;
1310 icmpv6_key = nla_data(nla);
1311 icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
1312 icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
1313
1314 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1315 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1316 struct ovs_key_nd *nd_key;
1317
1318 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1319 if (!nla)
1320 goto nla_put_failure;
1321 nd_key = nla_data(nla);
1322 memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
1323 sizeof(nd_key->nd_target));
1324 memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
1325 memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
1326 }
1327 }
1328 }
1329
1330unencap:
1331 if (encap)
1332 nla_nest_end(skb, encap);
1333
1334 return 0;
1335
1336nla_put_failure:
1337 return -EMSGSIZE;
1338}
1339
1340/* Initializes the flow module.
1341 * Returns zero if successful or a negative error code. */
1342int ovs_flow_init(void)
1343{
1344 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1345 0, NULL);
1346 if (flow_cache == NULL)
1347 return -ENOMEM;
1348
1349 return 0;
1350}
1351
1352/* Uninitializes the flow module. */
1353void ovs_flow_exit(void)
1354{
1355 kmem_cache_destroy(flow_cache);
1356}