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