]> git.proxmox.com Git - mirror_ovs.git/blame - datapath/flow.c
datapath: Fix Netlink error message header.
[mirror_ovs.git] / datapath / flow.c
CommitLineData
064af421 1/*
a1c564be 2 * Copyright (c) 2007-2013 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
a1c564be
AZ
50static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
51 struct sw_flow_key_range *range, u8 val);
52
53static void update_range__(struct sw_flow_match *match,
54 size_t offset, size_t size, bool is_mask)
55{
56 struct sw_flow_key_range *range = NULL;
57 size_t start = offset;
58 size_t end = offset + size;
59
60 if (!is_mask)
61 range = &match->range;
62 else if (match->mask)
63 range = &match->mask->range;
64
65 if (!range)
66 return;
67
68 if (range->start == range->end) {
69 range->start = start;
70 range->end = end;
71 return;
72 }
73
74 if (range->start > start)
75 range->start = start;
76
77 if (range->end < end)
78 range->end = end;
79}
80
81#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
82 do { \
83 update_range__(match, offsetof(struct sw_flow_key, field), \
84 sizeof((match)->key->field), is_mask); \
85 if (is_mask && match->mask != NULL) { \
86 (match)->mask->key.field = value; \
87 } else { \
88 (match)->key->field = value; \
89 } \
90 } while (0)
91
92#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
93 do { \
94 update_range__(match, offsetof(struct sw_flow_key, field), \
95 len, is_mask); \
96 if (is_mask && match->mask != NULL) { \
97 memcpy(&(match)->mask->key.field, value_p, len); \
98 } else { \
99 memcpy(&(match)->key->field, value_p, len); \
100 } \
101 } while (0)
102
103void ovs_match_init(struct sw_flow_match *match,
104 struct sw_flow_key *key,
105 struct sw_flow_mask *mask)
106{
107 memset(match, 0, sizeof(*match));
108 match->key = key;
109 match->mask = mask;
110
111 memset(key, 0, sizeof(*key));
112
113 if (mask) {
114 memset(&mask->key, 0, sizeof(mask->key));
115 mask->range.start = mask->range.end = 0;
116 }
117}
118
119static bool ovs_match_validate(const struct sw_flow_match *match,
120 u64 key_attrs, u64 mask_attrs)
121{
122 u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
123 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
124
125 /* The following mask attributes allowed only if they
126 * pass the validation tests. */
127 mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
128 | (1ULL << OVS_KEY_ATTR_IPV6)
129 | (1ULL << OVS_KEY_ATTR_TCP)
130 | (1ULL << OVS_KEY_ATTR_UDP)
131 | (1ULL << OVS_KEY_ATTR_ICMP)
132 | (1ULL << OVS_KEY_ATTR_ICMPV6)
133 | (1ULL << OVS_KEY_ATTR_ARP)
134 | (1ULL << OVS_KEY_ATTR_ND));
135
861687d4
JG
136 if (match->key->phy.in_port == DP_MAX_PORTS &&
137 match->mask && (match->mask->key.phy.in_port == 0xffff))
138 mask_allowed |= (1ULL << OVS_KEY_ATTR_IN_PORT);
139
a1c564be
AZ
140 if (match->key->eth.type == htons(ETH_P_802_2) &&
141 match->mask && (match->mask->key.eth.type == htons(0xffff)))
142 mask_allowed |= (1ULL << OVS_KEY_ATTR_ETHERTYPE);
143
144 /* Check key attributes. */
145 if (match->key->eth.type == htons(ETH_P_ARP)
146 || match->key->eth.type == htons(ETH_P_RARP)) {
147 key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
148 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
149 mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
150 }
151
152 if (match->key->eth.type == htons(ETH_P_IP)) {
153 key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
154 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
155 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
156
157 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
158 if (match->key->ip.proto == IPPROTO_UDP) {
159 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
160 if (match->mask && (match->mask->key.ip.proto == 0xff))
161 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
162 }
163
164 if (match->key->ip.proto == IPPROTO_TCP) {
165 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
166 if (match->mask && (match->mask->key.ip.proto == 0xff))
167 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
168 }
169
170 if (match->key->ip.proto == IPPROTO_ICMP) {
171 key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
172 if (match->mask && (match->mask->key.ip.proto == 0xff))
173 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
174 }
175 }
176 }
177
178 if (match->key->eth.type == htons(ETH_P_IPV6)) {
179 key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
180 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
181 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
182
183 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
184 if (match->key->ip.proto == IPPROTO_UDP) {
185 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
186 if (match->mask && (match->mask->key.ip.proto == 0xff))
187 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
188 }
189
190 if (match->key->ip.proto == IPPROTO_TCP) {
191 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
192 if (match->mask && (match->mask->key.ip.proto == 0xff))
193 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
194 }
195
196 if (match->key->ip.proto == IPPROTO_ICMPV6) {
197 key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
198 if (match->mask && (match->mask->key.ip.proto == 0xff))
199 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
200
201 if (match->key->ipv6.tp.src ==
202 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
203 match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
204 key_expected |= 1ULL << OVS_KEY_ATTR_ND;
205 if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
206 mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
207 }
208 }
209 }
210 }
211
1b936472 212 if ((key_attrs & key_expected) != key_expected) {
a1c564be 213 /* Key attributes check failed. */
1b936472
AZ
214 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
215 key_attrs, key_expected);
a1c564be 216 return false;
1b936472 217 }
a1c564be 218
1b936472 219 if ((mask_attrs & mask_allowed) != mask_attrs) {
a1c564be 220 /* Mask attributes check failed. */
1b936472
AZ
221 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
222 mask_attrs, mask_allowed);
a1c564be 223 return false;
1b936472 224 }
a1c564be
AZ
225
226 return true;
227}
228
2db65bf7
JG
229static int check_header(struct sk_buff *skb, int len)
230{
231 if (unlikely(skb->len < len))
232 return -EINVAL;
233 if (unlikely(!pskb_may_pull(skb, len)))
234 return -ENOMEM;
235 return 0;
236}
237
6455100f 238static bool arphdr_ok(struct sk_buff *skb)
a26ef517 239{
2db65bf7
JG
240 return pskb_may_pull(skb, skb_network_offset(skb) +
241 sizeof(struct arp_eth_header));
a26ef517
JP
242}
243
6455100f 244static int check_iphdr(struct sk_buff *skb)
064af421 245{
4c1ad233
BP
246 unsigned int nh_ofs = skb_network_offset(skb);
247 unsigned int ip_len;
2db65bf7 248 int err;
4c1ad233 249
2db65bf7
JG
250 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
251 if (unlikely(err))
252 return err;
4c1ad233
BP
253
254 ip_len = ip_hdrlen(skb);
2db65bf7
JG
255 if (unlikely(ip_len < sizeof(struct iphdr) ||
256 skb->len < nh_ofs + ip_len))
4c1ad233
BP
257 return -EINVAL;
258
4c1ad233
BP
259 skb_set_transport_header(skb, nh_ofs + ip_len);
260 return 0;
064af421
BP
261}
262
6455100f 263static bool tcphdr_ok(struct sk_buff *skb)
064af421
BP
264{
265 int th_ofs = skb_transport_offset(skb);
2db65bf7
JG
266 int tcp_len;
267
268 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
269 return false;
270
271 tcp_len = tcp_hdrlen(skb);
272 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
273 skb->len < th_ofs + tcp_len))
274 return false;
275
276 return true;
064af421
BP
277}
278
6455100f 279static bool udphdr_ok(struct sk_buff *skb)
064af421 280{
2db65bf7
JG
281 return pskb_may_pull(skb, skb_transport_offset(skb) +
282 sizeof(struct udphdr));
064af421
BP
283}
284
6455100f 285static bool icmphdr_ok(struct sk_buff *skb)
064af421 286{
2db65bf7
JG
287 return pskb_may_pull(skb, skb_transport_offset(skb) +
288 sizeof(struct icmphdr));
064af421
BP
289}
290
850b6b3b 291u64 ovs_flow_used_time(unsigned long flow_jiffies)
ec58547a
JG
292{
293 struct timespec cur_ts;
294 u64 cur_ms, idle_ms;
295
296 ktime_get_ts(&cur_ts);
297 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
298 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
299 cur_ts.tv_nsec / NSEC_PER_MSEC;
300
301 return cur_ms - idle_ms;
302}
303
a1c564be 304static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
d31f1109
JP
305{
306 unsigned int nh_ofs = skb_network_offset(skb);
307 unsigned int nh_len;
308 int payload_ofs;
d31f1109
JP
309 struct ipv6hdr *nh;
310 uint8_t nexthdr;
0fd0d083 311 __be16 frag_off;
2db65bf7 312 int err;
d31f1109 313
2db65bf7
JG
314 err = check_header(skb, nh_ofs + sizeof(*nh));
315 if (unlikely(err))
316 return err;
d31f1109
JP
317
318 nh = ipv6_hdr(skb);
319 nexthdr = nh->nexthdr;
320 payload_ofs = (u8 *)(nh + 1) - skb->data;
d31f1109 321
28bad473 322 key->ip.proto = NEXTHDR_NONE;
530180fd 323 key->ip.tos = ipv6_get_dsfield(nh);
a61680c6 324 key->ip.ttl = nh->hop_limit;
fa8223b7 325 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
858d1026
JG
326 key->ipv6.addr.src = nh->saddr;
327 key->ipv6.addr.dst = nh->daddr;
d31f1109 328
0fd0d083 329 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
bfef4717 330 if (unlikely(payload_ofs < 0))
d31f1109 331 return -EINVAL;
bfef4717 332
0fd0d083
JG
333 if (frag_off) {
334 if (frag_off & htons(~0x7))
335 key->ip.frag = OVS_FRAG_TYPE_LATER;
336 else
337 key->ip.frag = OVS_FRAG_TYPE_FIRST;
338 }
339
d31f1109 340 nh_len = payload_ofs - nh_ofs;
d31f1109 341 skb_set_transport_header(skb, nh_ofs + nh_len);
28bad473 342 key->ip.proto = nexthdr;
d31f1109
JP
343 return nh_len;
344}
345
346static bool icmp6hdr_ok(struct sk_buff *skb)
347{
2db65bf7
JG
348 return pskb_may_pull(skb, skb_transport_offset(skb) +
349 sizeof(struct icmp6hdr));
d31f1109 350}
ec58547a 351
a1c564be
AZ
352static void flow_key_mask(struct sw_flow_key *dst,
353 const struct sw_flow_key *src,
354 const struct sw_flow_mask *mask)
355{
356 u8 *m = (u8 *)&mask->key + mask->range.start;
357 u8 *s = (u8 *)src + mask->range.start;
358 u8 *d = (u8 *)dst + mask->range.start;
359 int i;
360
361 memset(dst, 0, sizeof(*dst));
362 for (i = 0; i < ovs_sw_flow_mask_size_roundup(mask); i++) {
363 *d = *s & *m;
364 d++, s++, m++;
365 }
366}
367
064af421
BP
368#define TCP_FLAGS_OFFSET 13
369#define TCP_FLAG_MASK 0x3f
370
850b6b3b 371void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
064af421 372{
064af421
BP
373 u8 tcp_flags = 0;
374
11460e23
JG
375 if ((flow->key.eth.type == htons(ETH_P_IP) ||
376 flow->key.eth.type == htons(ETH_P_IPV6)) &&
9c47b45a
JG
377 flow->key.ip.proto == IPPROTO_TCP &&
378 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
abfec865
BP
379 u8 *tcp = (u8 *)tcp_hdr(skb);
380 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
064af421
BP
381 }
382
e9141eec 383 spin_lock(&flow->lock);
6bfafa55 384 flow->used = jiffies;
064af421
BP
385 flow->packet_count++;
386 flow->byte_count += skb->len;
387 flow->tcp_flags |= tcp_flags;
e9141eec 388 spin_unlock(&flow->lock);
064af421
BP
389}
390
9b405f1a 391struct sw_flow_actions *ovs_flow_actions_alloc(int size)
064af421
BP
392{
393 struct sw_flow_actions *sfa;
394
9b405f1a 395 if (size > MAX_ACTIONS_BUFSIZE)
064af421
BP
396 return ERR_PTR(-EINVAL);
397
ba400435 398 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
064af421
BP
399 if (!sfa)
400 return ERR_PTR(-ENOMEM);
401
9b405f1a 402 sfa->actions_len = 0;
064af421
BP
403 return sfa;
404}
405
850b6b3b 406struct sw_flow *ovs_flow_alloc(void)
560e8022
JG
407{
408 struct sw_flow *flow;
409
410 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
411 if (!flow)
412 return ERR_PTR(-ENOMEM);
413
414 spin_lock_init(&flow->lock);
6f04e94a 415 flow->sf_acts = NULL;
a1c564be 416 flow->mask = NULL;
064af421 417
560e8022
JG
418 return flow;
419}
420
be2ba156 421static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
8d5ebd83 422{
acd051f1 423 hash = jhash_1word(hash, table->hash_seed);
3544358a
PS
424 return flex_array_get(table->buckets,
425 (hash & (table->n_buckets - 1)));
426}
427
be2ba156 428static struct flex_array *alloc_buckets(unsigned int n_buckets)
3544358a 429{
be2ba156 430 struct flex_array *buckets;
3544358a
PS
431 int i, err;
432
433 buckets = flex_array_alloc(sizeof(struct hlist_head *),
434 n_buckets, GFP_KERNEL);
435 if (!buckets)
436 return NULL;
437
438 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
439 if (err) {
440 flex_array_free(buckets);
441 return NULL;
442 }
443
444 for (i = 0; i < n_buckets; i++)
445 INIT_HLIST_HEAD((struct hlist_head *)
446 flex_array_get(buckets, i));
447
448 return buckets;
449}
450
6455100f 451static void free_buckets(struct flex_array *buckets)
3544358a
PS
452{
453 flex_array_free(buckets);
454}
455
ede77a46 456static struct flow_table *__flow_tbl_alloc(int new_size)
3544358a
PS
457{
458 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
459
460 if (!table)
461 return NULL;
462
463 table->buckets = alloc_buckets(new_size);
464
465 if (!table->buckets) {
466 kfree(table);
467 return NULL;
468 }
469 table->n_buckets = new_size;
470 table->count = 0;
acd051f1
PS
471 table->node_ver = 0;
472 table->keep_flows = false;
473 get_random_bytes(&table->hash_seed, sizeof(u32));
ede77a46 474 table->mask_list = NULL;
3544358a
PS
475
476 return table;
477}
fb8c9347 478
a1c564be 479static void __flow_tbl_destroy(struct flow_table *table)
3544358a
PS
480{
481 int i;
482
acd051f1
PS
483 if (table->keep_flows)
484 goto skip_flows;
485
3544358a
PS
486 for (i = 0; i < table->n_buckets; i++) {
487 struct sw_flow *flow;
488 struct hlist_head *head = flex_array_get(table->buckets, i);
f8dfbcb7 489 struct hlist_node *n;
acd051f1 490 int ver = table->node_ver;
3544358a 491
f8dfbcb7 492 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
acd051f1 493 hlist_del_rcu(&flow->hash_node[ver]);
a1c564be 494 ovs_flow_free(flow, false);
3544358a
PS
495 }
496 }
497
ede77a46
AZ
498 BUG_ON(!list_empty(table->mask_list));
499 kfree(table->mask_list);
500
acd051f1 501skip_flows:
3544358a
PS
502 free_buckets(table->buckets);
503 kfree(table);
504}
505
ede77a46
AZ
506struct flow_table *ovs_flow_tbl_alloc(int new_size)
507{
508 struct flow_table *table = __flow_tbl_alloc(new_size);
509
510 if (!table)
511 return NULL;
512
513 table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
514 if (!table->mask_list) {
515 table->keep_flows = true;
516 __flow_tbl_destroy(table);
517 return NULL;
518 }
519 INIT_LIST_HEAD(table->mask_list);
520
521 return table;
522}
523
3544358a
PS
524static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
525{
526 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
527
a1c564be 528 __flow_tbl_destroy(table);
3544358a
PS
529}
530
a1c564be 531void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
3544358a 532{
6455100f
PS
533 if (!table)
534 return;
3544358a 535
a1c564be
AZ
536 if (deferred)
537 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
538 else
539 __flow_tbl_destroy(table);
3544358a
PS
540}
541
a1c564be 542struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *last)
3544358a
PS
543{
544 struct sw_flow *flow;
545 struct hlist_head *head;
acd051f1 546 int ver;
3544358a
PS
547 int i;
548
acd051f1 549 ver = table->node_ver;
3544358a
PS
550 while (*bucket < table->n_buckets) {
551 i = 0;
552 head = flex_array_get(table->buckets, *bucket);
f8dfbcb7 553 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
3544358a
PS
554 if (i < *last) {
555 i++;
556 continue;
557 }
558 *last = i + 1;
559 return flow;
560 }
561 (*bucket)++;
562 *last = 0;
563 }
564
565 return NULL;
566}
567
a1c564be 568static void __tbl_insert(struct flow_table *table, struct sw_flow *flow)
13e24889
PS
569{
570 struct hlist_head *head;
a1c564be 571
13e24889
PS
572 head = find_bucket(table, flow->hash);
573 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
a1c564be 574
13e24889
PS
575 table->count++;
576}
577
acd051f1 578static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
3544358a 579{
acd051f1 580 int old_ver;
3544358a
PS
581 int i;
582
acd051f1
PS
583 old_ver = old->node_ver;
584 new->node_ver = !old_ver;
3544358a 585
acd051f1
PS
586 /* Insert in new table. */
587 for (i = 0; i < old->n_buckets; i++) {
3544358a
PS
588 struct sw_flow *flow;
589 struct hlist_head *head;
3544358a 590
acd051f1 591 head = flex_array_get(old->buckets, i);
3544358a 592
f8dfbcb7 593 hlist_for_each_entry(flow, head, hash_node[old_ver])
a1c564be 594 __tbl_insert(new, flow);
3544358a 595 }
a1c564be
AZ
596
597 new->mask_list = old->mask_list;
acd051f1
PS
598 old->keep_flows = true;
599}
600
601static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
602{
603 struct flow_table *new_table;
604
ede77a46 605 new_table = __flow_tbl_alloc(n_buckets);
acd051f1
PS
606 if (!new_table)
607 return ERR_PTR(-ENOMEM);
608
609 flow_table_copy_flows(table, new_table);
3544358a
PS
610
611 return new_table;
612}
613
acd051f1
PS
614struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
615{
616 return __flow_tbl_rehash(table, table->n_buckets);
617}
618
619struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
620{
621 return __flow_tbl_rehash(table, table->n_buckets * 2);
622}
623
a1c564be 624static void __flow_free(struct sw_flow *flow)
9321954a 625{
9321954a
JG
626 kfree((struct sf_flow_acts __force *)flow->sf_acts);
627 kmem_cache_free(flow_cache, flow);
628}
629
064af421
BP
630static void rcu_free_flow_callback(struct rcu_head *rcu)
631{
632 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
fb8c9347 633
a1c564be 634 __flow_free(flow);
064af421
BP
635}
636
a1c564be 637void ovs_flow_free(struct sw_flow *flow, bool deferred)
064af421 638{
a1c564be
AZ
639 if (!flow)
640 return;
641
642 ovs_sw_flow_mask_del_ref((struct sw_flow_mask __force *)flow->mask,
643 deferred);
644
645 if (deferred)
646 call_rcu(&flow->rcu, rcu_free_flow_callback);
647 else
648 __flow_free(flow);
064af421
BP
649}
650
850b6b3b 651/* RCU callback used by ovs_flow_deferred_free_acts. */
064af421
BP
652static void rcu_free_acts_callback(struct rcu_head *rcu)
653{
d295e8e9 654 struct sw_flow_actions *sf_acts = container_of(rcu,
064af421 655 struct sw_flow_actions, rcu);
ba400435 656 kfree(sf_acts);
064af421
BP
657}
658
659/* Schedules 'sf_acts' to be freed after the next RCU grace period.
660 * The caller must hold rcu_read_lock for this to be sensible. */
850b6b3b 661void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
064af421
BP
662{
663 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
664}
665
2db65bf7 666static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
064af421 667{
50f06e16
BP
668 struct qtag_prefix {
669 __be16 eth_type; /* ETH_P_8021Q */
670 __be16 tci;
671 };
672 struct qtag_prefix *qp;
673
8ddc056d
BP
674 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
675 return 0;
676
2db65bf7
JG
677 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
678 sizeof(__be16))))
679 return -ENOMEM;
50f06e16
BP
680
681 qp = (struct qtag_prefix *) skb->data;
76abe283 682 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
50f06e16 683 __skb_pull(skb, sizeof(struct qtag_prefix));
2db65bf7
JG
684
685 return 0;
50f06e16
BP
686}
687
688static __be16 parse_ethertype(struct sk_buff *skb)
064af421 689{
50f06e16
BP
690 struct llc_snap_hdr {
691 u8 dsap; /* Always 0xAA */
692 u8 ssap; /* Always 0xAA */
693 u8 ctrl;
694 u8 oui[3];
8dda8c9b 695 __be16 ethertype;
50f06e16
BP
696 };
697 struct llc_snap_hdr *llc;
698 __be16 proto;
699
700 proto = *(__be16 *) skb->data;
701 __skb_pull(skb, sizeof(__be16));
702
7cd46155 703 if (ntohs(proto) >= ETH_P_802_3_MIN)
50f06e16
BP
704 return proto;
705
2db65bf7 706 if (skb->len < sizeof(struct llc_snap_hdr))
36956a7d 707 return htons(ETH_P_802_2);
50f06e16 708
2db65bf7
JG
709 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
710 return htons(0);
711
50f06e16
BP
712 llc = (struct llc_snap_hdr *) skb->data;
713 if (llc->dsap != LLC_SAP_SNAP ||
714 llc->ssap != LLC_SAP_SNAP ||
715 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
36956a7d 716 return htons(ETH_P_802_2);
50f06e16
BP
717
718 __skb_pull(skb, sizeof(struct llc_snap_hdr));
9e69bc5f 719
7cd46155 720 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
9e69bc5f
RL
721 return llc->ethertype;
722
723 return htons(ETH_P_802_2);
064af421
BP
724}
725
685a51a5 726static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
a1c564be 727 int nh_len)
685a51a5 728{
685a51a5
JP
729 struct icmp6hdr *icmp = icmp6_hdr(skb);
730
731 /* The ICMPv6 type and code fields use the 16-bit transport port
bfef4717
JG
732 * fields, so we need to store them in 16-bit network byte order.
733 */
76abe283
AE
734 key->ipv6.tp.src = htons(icmp->icmp6_type);
735 key->ipv6.tp.dst = htons(icmp->icmp6_code);
685a51a5 736
bfef4717
JG
737 if (icmp->icmp6_code == 0 &&
738 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
739 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
e977ba19 740 int icmp_len = skb->len - skb_transport_offset(skb);
685a51a5
JP
741 struct nd_msg *nd;
742 int offset;
743
744 /* In order to process neighbor discovery options, we need the
bfef4717
JG
745 * entire packet.
746 */
747 if (unlikely(icmp_len < sizeof(*nd)))
a1c564be
AZ
748 return 0;
749
750 if (unlikely(skb_linearize(skb)))
751 return -ENOMEM;
685a51a5
JP
752
753 nd = (struct nd_msg *)skb_transport_header(skb);
858d1026 754 key->ipv6.nd.target = nd->target;
685a51a5
JP
755
756 icmp_len -= sizeof(*nd);
757 offset = 0;
758 while (icmp_len >= 8) {
6455100f
PS
759 struct nd_opt_hdr *nd_opt =
760 (struct nd_opt_hdr *)(nd->opt + offset);
685a51a5
JP
761 int opt_len = nd_opt->nd_opt_len * 8;
762
bfef4717 763 if (unlikely(!opt_len || opt_len > icmp_len))
a1c564be 764 return 0;
685a51a5 765
bfef4717
JG
766 /* Store the link layer address if the appropriate
767 * option is provided. It is considered an error if
768 * the same link layer option is specified twice.
769 */
685a51a5 770 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
bfef4717 771 && opt_len == 8) {
76abe283 772 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
685a51a5 773 goto invalid;
76abe283 774 memcpy(key->ipv6.nd.sll,
bfef4717 775 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
685a51a5 776 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
bfef4717 777 && opt_len == 8) {
76abe283 778 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
685a51a5 779 goto invalid;
76abe283 780 memcpy(key->ipv6.nd.tll,
bfef4717 781 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
685a51a5
JP
782 }
783
784 icmp_len -= opt_len;
785 offset += opt_len;
786 }
787 }
788
a1c564be 789 return 0;
685a51a5
JP
790
791invalid:
76abe283
AE
792 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
793 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
794 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
685a51a5 795
a1c564be 796 return 0;
685a51a5
JP
797}
798
a31e0e31 799/**
850b6b3b 800 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
a31e0e31
BP
801 * @skb: sk_buff that contains the frame, with skb->data pointing to the
802 * Ethernet header
803 * @in_port: port number on which @skb was received.
804 * @key: output flow key
76abe283 805 * @key_lenp: length of output flow key
a31e0e31
BP
806 *
807 * The caller must ensure that skb->len >= ETH_HLEN.
808 *
4c1ad233
BP
809 * Returns 0 if successful, otherwise a negative errno value.
810 *
59a18f80
BP
811 * Initializes @skb header pointers as follows:
812 *
813 * - skb->mac_header: the Ethernet header.
814 *
815 * - skb->network_header: just past the Ethernet header, or just past the
816 * VLAN header, to the first byte of the Ethernet payload.
817 *
ae9020cf 818 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
d31f1109
JP
819 * on output, then just past the IP header, if one is present and
820 * of a correct length, otherwise the same as skb->network_header.
ae9020cf 821 * For other key->eth.type values it is left untouched.
a31e0e31 822 */
a1c564be 823int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
064af421 824{
a1c564be 825 int error;
064af421 826 struct ethhdr *eth;
064af421 827
84c17d98 828 memset(key, 0, sizeof(*key));
abff858b
PS
829
830 key->phy.priority = skb->priority;
356af50b 831 if (OVS_CB(skb)->tun_key)
6694e498 832 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
abff858b 833 key->phy.in_port = in_port;
72e8bf28 834 key->phy.skb_mark = skb_get_mark(skb);
064af421 835
064af421 836 skb_reset_mac_header(skb);
064af421 837
2db65bf7
JG
838 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
839 * header in the linear data area.
840 */
50f06e16 841 eth = eth_hdr(skb);
76abe283
AE
842 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
843 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
844
50f06e16 845 __skb_pull(skb, 2 * ETH_ALEN);
3cfede14
PS
846 /* We are going to push all headers that we pull, so no need to
847 * update skb->csum here. */
6ce39213
JG
848
849 if (vlan_tx_tag_present(skb))
76abe283 850 key->eth.tci = htons(vlan_get_tci(skb));
6ce39213 851 else if (eth->h_proto == htons(ETH_P_8021Q))
2db65bf7
JG
852 if (unlikely(parse_vlan(skb, key)))
853 return -ENOMEM;
6ce39213 854
76abe283
AE
855 key->eth.type = parse_ethertype(skb);
856 if (unlikely(key->eth.type == htons(0)))
2db65bf7
JG
857 return -ENOMEM;
858
50f06e16 859 skb_reset_network_header(skb);
2db65bf7 860 __skb_push(skb, skb->data - skb_mac_header(skb));
064af421
BP
861
862 /* Network layer. */
76abe283 863 if (key->eth.type == htons(ETH_P_IP)) {
4c1ad233 864 struct iphdr *nh;
7257b535 865 __be16 offset;
76abe283 866
4c1ad233
BP
867 error = check_iphdr(skb);
868 if (unlikely(error)) {
869 if (error == -EINVAL) {
870 skb->transport_header = skb->network_header;
76abe283 871 error = 0;
4c1ad233 872 }
a1c564be 873 return error;
4c1ad233
BP
874 }
875
876 nh = ip_hdr(skb);
76abe283
AE
877 key->ipv4.addr.src = nh->saddr;
878 key->ipv4.addr.dst = nh->daddr;
7257b535 879
28bad473 880 key->ip.proto = nh->protocol;
530180fd 881 key->ip.tos = nh->tos;
a61680c6 882 key->ip.ttl = nh->ttl;
064af421 883
7257b535
BP
884 offset = nh->frag_off & htons(IP_OFFSET);
885 if (offset) {
9e44d715 886 key->ip.frag = OVS_FRAG_TYPE_LATER;
a1c564be 887 return 0;
7257b535
BP
888 }
889 if (nh->frag_off & htons(IP_MF) ||
890 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
9e44d715 891 key->ip.frag = OVS_FRAG_TYPE_FIRST;
b7a31ec1 892
7257b535 893 /* Transport layer. */
28bad473 894 if (key->ip.proto == IPPROTO_TCP) {
7257b535 895 if (tcphdr_ok(skb)) {
5f4d087c
JG
896 struct tcphdr *tcp = tcp_hdr(skb);
897 key->ipv4.tp.src = tcp->source;
898 key->ipv4.tp.dst = tcp->dest;
899 }
28bad473 900 } else if (key->ip.proto == IPPROTO_UDP) {
7257b535 901 if (udphdr_ok(skb)) {
5f4d087c
JG
902 struct udphdr *udp = udp_hdr(skb);
903 key->ipv4.tp.src = udp->source;
904 key->ipv4.tp.dst = udp->dest;
905 }
28bad473 906 } else if (key->ip.proto == IPPROTO_ICMP) {
7257b535 907 if (icmphdr_ok(skb)) {
5f4d087c
JG
908 struct icmphdr *icmp = icmp_hdr(skb);
909 /* The ICMP type and code fields use the 16-bit
6455100f
PS
910 * transport port fields, so we need to store
911 * them in 16-bit network byte order. */
5f4d087c
JG
912 key->ipv4.tp.src = htons(icmp->type);
913 key->ipv4.tp.dst = htons(icmp->code);
914 }
915 }
916
8087f5ff
MM
917 } else if ((key->eth.type == htons(ETH_P_ARP) ||
918 key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
a26ef517
JP
919 struct arp_eth_header *arp;
920
921 arp = (struct arp_eth_header *)skb_network_header(skb);
922
f5e86186 923 if (arp->ar_hrd == htons(ARPHRD_ETHER)
de3f65ea
JP
924 && arp->ar_pro == htons(ETH_P_IP)
925 && arp->ar_hln == ETH_ALEN
926 && arp->ar_pln == 4) {
927
928 /* We only match on the lower 8 bits of the opcode. */
b7a31ec1 929 if (ntohs(arp->ar_op) <= 0xff)
28bad473 930 key->ip.proto = ntohs(arp->ar_op);
a3d3ad0c
MM
931 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
932 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
933 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
934 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
de3f65ea 935 }
76abe283 936 } else if (key->eth.type == htons(ETH_P_IPV6)) {
d31f1109
JP
937 int nh_len; /* IPv6 Header + Extensions */
938
a1c564be 939 nh_len = parse_ipv6hdr(skb, key);
d31f1109 940 if (unlikely(nh_len < 0)) {
a1c564be 941 if (nh_len == -EINVAL) {
d31f1109 942 skb->transport_header = skb->network_header;
a1c564be
AZ
943 error = 0;
944 } else {
76abe283 945 error = nh_len;
a1c564be
AZ
946 }
947 return error;
d31f1109
JP
948 }
949
9e44d715 950 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
a1c564be 951 return 0;
7257b535 952 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
9e44d715 953 key->ip.frag = OVS_FRAG_TYPE_FIRST;
7257b535 954
d31f1109 955 /* Transport layer. */
28bad473 956 if (key->ip.proto == NEXTHDR_TCP) {
d31f1109
JP
957 if (tcphdr_ok(skb)) {
958 struct tcphdr *tcp = tcp_hdr(skb);
76abe283
AE
959 key->ipv6.tp.src = tcp->source;
960 key->ipv6.tp.dst = tcp->dest;
d31f1109 961 }
28bad473 962 } else if (key->ip.proto == NEXTHDR_UDP) {
d31f1109
JP
963 if (udphdr_ok(skb)) {
964 struct udphdr *udp = udp_hdr(skb);
76abe283
AE
965 key->ipv6.tp.src = udp->source;
966 key->ipv6.tp.dst = udp->dest;
d31f1109 967 }
28bad473 968 } else if (key->ip.proto == NEXTHDR_ICMP) {
d31f1109 969 if (icmp6hdr_ok(skb)) {
a1c564be
AZ
970 error = parse_icmpv6(skb, key, nh_len);
971 if (error)
972 return error;
d31f1109
JP
973 }
974 }
064af421 975 }
76abe283 976
a1c564be 977 return 0;
064af421
BP
978}
979
13e24889 980static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start, int key_len)
8d5ebd83 981{
13e24889
PS
982 return jhash2((u32 *)((u8 *)key + key_start),
983 DIV_ROUND_UP(key_len - key_start, sizeof(u32)), 0);
984}
985
a1c564be 986static int flow_key_start(const struct sw_flow_key *key)
13e24889 987{
6694e498 988 if (key->tun_key.ipv4_dst)
13e24889
PS
989 return 0;
990 else
6694e498 991 return offsetof(struct sw_flow_key, phy);
8d5ebd83
JG
992}
993
a1c564be
AZ
994static bool __cmp_key(const struct sw_flow_key *key1,
995 const struct sw_flow_key *key2, int key_start, int key_len)
996{
997 return !memcmp((u8 *)key1 + key_start,
998 (u8 *)key2 + key_start, (key_len - key_start));
999}
1000
1001static bool __flow_cmp_key(const struct sw_flow *flow,
1002 const struct sw_flow_key *key, int key_start, int key_len)
1003{
1004 return __cmp_key(&flow->key, key, key_start, key_len);
1005}
1006
1007static bool __flow_cmp_unmasked_key(const struct sw_flow *flow,
1008 const struct sw_flow_key *key, int key_start, int key_len)
1009{
1010 return __cmp_key(&flow->unmasked_key, key, key_start, key_len);
1011}
1012
1013bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
1014 const struct sw_flow_key *key, int key_len)
1015{
1016 int key_start;
1017 key_start = flow_key_start(key);
1018
1019 return __flow_cmp_unmasked_key(flow, key, key_start, key_len);
1020
1021}
1022
1023struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
1024 struct sw_flow_match *match)
1025{
1026 struct sw_flow_key *unmasked = match->key;
1027 int key_len = match->range.end;
1028 struct sw_flow *flow;
1029
1030 flow = ovs_flow_lookup(table, unmasked);
1031 if (flow && (!ovs_flow_cmp_unmasked_key(flow, unmasked, key_len)))
1032 flow = NULL;
1033
1034 return flow;
1035}
1036
1037static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table,
1038 const struct sw_flow_key *flow_key,
1039 struct sw_flow_mask *mask)
8d5ebd83 1040{
3544358a 1041 struct sw_flow *flow;
3544358a 1042 struct hlist_head *head;
a1c564be
AZ
1043 int key_start = mask->range.start;
1044 int key_len = mask->range.end;
3544358a 1045 u32 hash;
a1c564be 1046 struct sw_flow_key masked_key;
8d5ebd83 1047
a1c564be
AZ
1048 flow_key_mask(&masked_key, flow_key, mask);
1049 hash = ovs_flow_hash(&masked_key, key_start, key_len);
3544358a 1050 head = find_bucket(table, hash);
f8dfbcb7 1051 hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
a1c564be 1052 if (__flow_cmp_key(flow, &masked_key, key_start, key_len))
3544358a 1053 return flow;
3544358a
PS
1054 }
1055 return NULL;
1056}
1057
a1c564be
AZ
1058struct sw_flow *ovs_flow_lookup(struct flow_table *tbl,
1059 const struct sw_flow_key *key)
3544358a 1060{
a1c564be
AZ
1061 struct sw_flow *flow = NULL;
1062 struct sw_flow_mask *mask;
1063
ede77a46 1064 list_for_each_entry_rcu(mask, tbl->mask_list, list) {
a1c564be
AZ
1065 flow = ovs_masked_flow_lookup(tbl, key, mask);
1066 if (flow) /* Found */
1067 break;
1068 }
1069
1070 return flow;
3544358a
PS
1071}
1072
a1c564be
AZ
1073
1074void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow,
1075 const struct sw_flow_key *key, int key_len)
1076{
1077 flow->unmasked_key = *key;
1078 flow_key_mask(&flow->key, &flow->unmasked_key, ovsl_dereference(flow->mask));
1079 flow->hash = ovs_flow_hash(&flow->key,
1080 ovsl_dereference(flow->mask)->range.start,
1081 ovsl_dereference(flow->mask)->range.end);
1082 __tbl_insert(table, flow);
1083}
1084
1085void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow)
3544358a 1086{
49c7f80c 1087 BUG_ON(table->count == 0);
acd051f1
PS
1088 hlist_del_rcu(&flow->hash_node[table->node_ver]);
1089 table->count--;
36956a7d
BP
1090}
1091
df2c07f4 1092/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
4094406f
BP
1093const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
1094 [OVS_KEY_ATTR_ENCAP] = -1,
a1bf209f
BP
1095 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
1096 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
72e8bf28 1097 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
df2c07f4 1098 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
fea393b1 1099 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
a1bf209f 1100 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
df2c07f4
JP
1101 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
1102 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
1103 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
1104 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
1105 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
1106 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
1107 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
1108 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
9b405f1a 1109 [OVS_KEY_ATTR_TUNNEL] = -1,
80e5eed9
BP
1110};
1111
a1c564be 1112static bool is_all_zero(const u8 *fp, size_t size)
34118cae 1113{
a1c564be 1114 int i;
34118cae 1115
a1c564be
AZ
1116 if (!fp)
1117 return false;
34118cae 1118
a1c564be
AZ
1119 for (i = 0; i < size; i++)
1120 if (fp[i])
1121 return false;
34118cae 1122
a1c564be 1123 return true;
34118cae
BP
1124}
1125
a1c564be
AZ
1126static int __parse_flow_nlattrs(const struct nlattr *attr,
1127 const struct nlattr *a[],
1128 u64 *attrsp, bool nz)
fea393b1
BP
1129{
1130 const struct nlattr *nla;
1131 u64 attrs;
1132 int rem;
1133
a1c564be 1134 attrs = *attrsp;
fea393b1
BP
1135 nla_for_each_nested(nla, attr, rem) {
1136 u16 type = nla_type(nla);
4094406f 1137 int expected_len;
fea393b1 1138
1b936472
AZ
1139 if (type > OVS_KEY_ATTR_MAX) {
1140 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
1141 type, OVS_KEY_ATTR_MAX);
1142 }
4094406f 1143
1b936472
AZ
1144 if (attrs & (1ULL << type)) {
1145 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
4094406f 1146 return -EINVAL;
1b936472 1147 }
4094406f 1148
1b936472
AZ
1149 expected_len = ovs_key_lens[type];
1150 if (nla_len(nla) != expected_len && expected_len != -1) {
1151 OVS_NLERR("Key attribute has unexpected length (type=%d"
1152 ", length=%d, expected=%d).\n", type,
1153 nla_len(nla), expected_len);
a1c564be 1154 return -EINVAL;
1b936472 1155 }
a1c564be
AZ
1156
1157 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
1158 attrs |= 1ULL << type;
1159 a[type] = nla;
1160 }
fea393b1 1161 }
1b936472
AZ
1162 if (rem) {
1163 OVS_NLERR("Message has %d unknown bytes.\n", rem);
fea393b1 1164 return -EINVAL;
1b936472 1165 }
fea393b1
BP
1166
1167 *attrsp = attrs;
1168 return 0;
1169}
1170
a1c564be
AZ
1171static int parse_flow_mask_nlattrs(const struct nlattr *attr,
1172 const struct nlattr *a[], u64 *attrsp)
1173{
1174 return __parse_flow_nlattrs(attr, a, attrsp, true);
1175}
1176
1177static int parse_flow_nlattrs(const struct nlattr *attr,
1178 const struct nlattr *a[], u64 *attrsp)
1179{
1180 return __parse_flow_nlattrs(attr, a, attrsp, false);
1181}
1182
9b405f1a 1183int ipv4_tun_from_nlattr(const struct nlattr *attr,
a1c564be 1184 struct sw_flow_match *match, bool is_mask)
9b405f1a
PS
1185{
1186 struct nlattr *a;
1187 int rem;
1188 bool ttl = false;
5ebaf571 1189 __be16 tun_flags = 0;
9b405f1a
PS
1190
1191 nla_for_each_nested(a, attr, rem) {
1192 int type = nla_type(a);
1193 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1194 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
1195 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
1196 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
1197 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
1198 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
1199 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
1200 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
1201 };
1202
1b936472 1203 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
17ce1965 1204 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
1b936472
AZ
1205 type, OVS_TUNNEL_KEY_ATTR_MAX);
1206 return -EINVAL;
1207 }
1208
1209 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
1210 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
17ce1965 1211 " legnth (type=%d, length=%d, expected=%d).\n",
1b936472 1212 type, nla_len(a), ovs_tunnel_key_lens[type]);
9b405f1a 1213 return -EINVAL;
1b936472 1214 }
9b405f1a
PS
1215
1216 switch (type) {
1217 case OVS_TUNNEL_KEY_ATTR_ID:
a1c564be
AZ
1218 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
1219 nla_get_be64(a), is_mask);
5ebaf571 1220 tun_flags |= TUNNEL_KEY;
9b405f1a
PS
1221 break;
1222 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
a1c564be
AZ
1223 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
1224 nla_get_be32(a), is_mask);
9b405f1a
PS
1225 break;
1226 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
a1c564be
AZ
1227 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
1228 nla_get_be32(a), is_mask);
9b405f1a
PS
1229 break;
1230 case OVS_TUNNEL_KEY_ATTR_TOS:
a1c564be
AZ
1231 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
1232 nla_get_u8(a), is_mask);
9b405f1a
PS
1233 break;
1234 case OVS_TUNNEL_KEY_ATTR_TTL:
a1c564be
AZ
1235 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
1236 nla_get_u8(a), is_mask);
9b405f1a
PS
1237 ttl = true;
1238 break;
1239 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
5ebaf571 1240 tun_flags |= TUNNEL_DONT_FRAGMENT;
9b405f1a
PS
1241 break;
1242 case OVS_TUNNEL_KEY_ATTR_CSUM:
5ebaf571 1243 tun_flags |= TUNNEL_CSUM;
9b405f1a
PS
1244 break;
1245 default:
1246 return -EINVAL;
9b405f1a
PS
1247 }
1248 }
a1c564be
AZ
1249
1250 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
1251
1b936472
AZ
1252 if (rem > 0) {
1253 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
9b405f1a 1254 return -EINVAL;
1b936472 1255 }
9b405f1a 1256
1b936472
AZ
1257 if (!match->key->tun_key.ipv4_dst) {
1258 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
9b405f1a 1259 return -EINVAL;
1b936472 1260 }
9b405f1a 1261
1b936472
AZ
1262 if (!ttl) {
1263 OVS_NLERR("IPv4 tunnel TTL is zero.\n");
9b405f1a 1264 return -EINVAL;
1b936472 1265 }
9b405f1a
PS
1266
1267 return 0;
1268}
1269
1270int ipv4_tun_to_nlattr(struct sk_buff *skb,
a1c564be
AZ
1271 const struct ovs_key_ipv4_tunnel *tun_key,
1272 const struct ovs_key_ipv4_tunnel *output)
9b405f1a
PS
1273{
1274 struct nlattr *nla;
1275
1276 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
1277 if (!nla)
1278 return -EMSGSIZE;
1279
5ebaf571 1280 if (tun_key->tun_flags & TUNNEL_KEY &&
a1c564be 1281 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
9b405f1a
PS
1282 return -EMSGSIZE;
1283 if (tun_key->ipv4_src &&
a1c564be 1284 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
9b405f1a 1285 return -EMSGSIZE;
a1c564be 1286 if (nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
9b405f1a
PS
1287 return -EMSGSIZE;
1288 if (tun_key->ipv4_tos &&
a1c564be 1289 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
9b405f1a 1290 return -EMSGSIZE;
a1c564be 1291 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
9b405f1a 1292 return -EMSGSIZE;
5ebaf571 1293 if ((tun_key->tun_flags & TUNNEL_DONT_FRAGMENT) &&
9b405f1a
PS
1294 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
1295 return -EMSGSIZE;
5ebaf571 1296 if ((tun_key->tun_flags & TUNNEL_CSUM) &&
9b405f1a
PS
1297 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
1298 return -EMSGSIZE;
1299
1300 nla_nest_end(skb, nla);
1301 return 0;
1302}
1303
36956a7d 1304
a1c564be
AZ
1305static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
1306 const struct nlattr **a, bool is_mask)
1307{
1308 if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
1309 SW_FLOW_KEY_PUT(match, phy.priority,
1310 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1311 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
34118cae 1312 }
a1c564be
AZ
1313
1314 if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
34118cae 1315 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
a1c564be
AZ
1316
1317 if (!is_mask && in_port >= DP_MAX_PORTS)
34118cae 1318 return -EINVAL;
a1c564be
AZ
1319 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1320 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
861687d4
JG
1321 } else if (!is_mask) {
1322 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
34118cae 1323 }
a1c564be
AZ
1324
1325 if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
72e8bf28
AA
1326 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1327#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
1b936472
AZ
1328 if (!is_mask && mark != 0) {
1329 OVS_NLERR("skb->mark must be zero on this kernel (mark=%d).\n", mark);
72e8bf28 1330 return -EINVAL;
1b936472 1331 }
72e8bf28 1332#endif
a1c564be
AZ
1333 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1334 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
1335 }
1336 if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
1337 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1338 is_mask))
1339 return -EINVAL;
1340 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
72e8bf28 1341 }
a1c564be
AZ
1342 return 0;
1343}
36956a7d 1344
a1c564be
AZ
1345static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
1346 const struct nlattr **a, bool is_mask)
1347{
1348 int err;
828f5f64 1349 u64 orig_attrs = attrs;
5c7f5883 1350
a1c564be
AZ
1351 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
1352 if (err)
1353 return err;
76abe283 1354
a1c564be
AZ
1355 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
1356 const struct ovs_key_ethernet *eth_key;
36956a7d 1357
a1c564be
AZ
1358 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1359 SW_FLOW_KEY_MEMCPY(match, eth.src,
1360 eth_key->eth_src, ETH_ALEN, is_mask);
1361 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1362 eth_key->eth_dst, ETH_ALEN, is_mask);
1363 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
1364 }
76abe283 1365
a1c564be 1366 if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
8ddc056d
BP
1367 __be16 tci;
1368
a1c564be 1369 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
9bbd9da0 1370 if (!is_mask)
1b936472
AZ
1371 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1372 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
9bbd9da0 1373 return -EINVAL;
1b936472 1374 }
36956a7d 1375
a1c564be
AZ
1376 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
1377 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
1378 }
8ddc056d 1379
a1c564be
AZ
1380 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
1381 __be16 eth_type;
8ddc056d 1382
a1c564be 1383 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1b936472
AZ
1384 if (!is_mask && ntohs(eth_type) < ETH_P_802_3_MIN) {
1385 OVS_NLERR("EtherType is less than mimimum (type=%x, min=%x).\n",
1386 ntohs(eth_type), ETH_P_802_3_MIN);
8ddc056d 1387 return -EINVAL;
1b936472 1388 }
a1c564be
AZ
1389
1390 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1391 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1392 } else if (!is_mask) {
1393 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
34118cae
BP
1394 }
1395
a1c564be
AZ
1396 if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1397 const struct ovs_key_ipv4 *ipv4_key;
1398
1399 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1b936472
AZ
1400 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
1401 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
1402 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
34118cae 1403 return -EINVAL;
1b936472 1404 }
a1c564be
AZ
1405 SW_FLOW_KEY_PUT(match, ip.proto,
1406 ipv4_key->ipv4_proto, is_mask);
1407 SW_FLOW_KEY_PUT(match, ip.tos,
1408 ipv4_key->ipv4_tos, is_mask);
1409 SW_FLOW_KEY_PUT(match, ip.ttl,
1410 ipv4_key->ipv4_ttl, is_mask);
1411 SW_FLOW_KEY_PUT(match, ip.frag,
1412 ipv4_key->ipv4_frag, is_mask);
1413 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1414 ipv4_key->ipv4_src, is_mask);
1415 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1416 ipv4_key->ipv4_dst, is_mask);
1417 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4);
34118cae
BP
1418 }
1419
a1c564be
AZ
1420 if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
1421 const struct ovs_key_ipv6 *ipv6_key;
685a51a5 1422
a1c564be 1423 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1b936472
AZ
1424 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
1425 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
1426 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
34118cae 1427 return -EINVAL;
1b936472 1428 }
a1c564be
AZ
1429 SW_FLOW_KEY_PUT(match, ipv6.label,
1430 ipv6_key->ipv6_label, is_mask);
1431 SW_FLOW_KEY_PUT(match, ip.proto,
1432 ipv6_key->ipv6_proto, is_mask);
1433 SW_FLOW_KEY_PUT(match, ip.tos,
1434 ipv6_key->ipv6_tclass, is_mask);
1435 SW_FLOW_KEY_PUT(match, ip.ttl,
1436 ipv6_key->ipv6_hlimit, is_mask);
1437 SW_FLOW_KEY_PUT(match, ip.frag,
1438 ipv6_key->ipv6_frag, is_mask);
1439 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1440 ipv6_key->ipv6_src,
1441 sizeof(match->key->ipv6.addr.src),
1442 is_mask);
1443 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1444 ipv6_key->ipv6_dst,
1445 sizeof(match->key->ipv6.addr.dst),
1446 is_mask);
1447
1448 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
1449 }
34118cae 1450
a1c564be
AZ
1451 if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
1452 const struct ovs_key_arp *arp_key;
1453
1454 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1b936472
AZ
1455 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
1456 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
1457 arp_key->arp_op);
34118cae 1458 return -EINVAL;
1b936472 1459 }
a1c564be
AZ
1460
1461 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1462 arp_key->arp_sip, is_mask);
1463 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1464 arp_key->arp_tip, is_mask);
1465 SW_FLOW_KEY_PUT(match, ip.proto,
1466 ntohs(arp_key->arp_op), is_mask);
1467 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1468 arp_key->arp_sha, ETH_ALEN, is_mask);
1469 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1470 arp_key->arp_tha, ETH_ALEN, is_mask);
1471
1472 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
1473 }
1474
1475 if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
1476 const struct ovs_key_tcp *tcp_key;
1477
1478 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
828f5f64
JP
1479 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1480 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1481 tcp_key->tcp_src, is_mask);
1482 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1483 tcp_key->tcp_dst, is_mask);
1484 } else {
1485 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1486 tcp_key->tcp_src, is_mask);
1487 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1488 tcp_key->tcp_dst, is_mask);
1489 }
a1c564be
AZ
1490 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
1491 }
1492
1493 if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
1494 const struct ovs_key_udp *udp_key;
1495
1496 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
828f5f64
JP
1497 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1498 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1499 udp_key->udp_src, is_mask);
1500 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1501 udp_key->udp_dst, is_mask);
1502 } else {
1503 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1504 udp_key->udp_src, is_mask);
1505 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1506 udp_key->udp_dst, is_mask);
1507 }
a1c564be
AZ
1508 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
1509 }
1510
1511 if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
1512 const struct ovs_key_icmp *icmp_key;
1513
1514 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1515 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1516 htons(icmp_key->icmp_type), is_mask);
1517 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1518 htons(icmp_key->icmp_code), is_mask);
1519 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
1520 }
1521
1522 if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
1523 const struct ovs_key_icmpv6 *icmpv6_key;
1524
1525 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1526 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1527 htons(icmpv6_key->icmpv6_type), is_mask);
1528 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1529 htons(icmpv6_key->icmpv6_code), is_mask);
1530 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
1531 }
1532
1533 if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
1534 const struct ovs_key_nd *nd_key;
1535
1536 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1537 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1538 nd_key->nd_target,
1539 sizeof(match->key->ipv6.nd.target),
1540 is_mask);
1541 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1542 nd_key->nd_sll, ETH_ALEN, is_mask);
1543 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1544 nd_key->nd_tll, ETH_ALEN, is_mask);
1545 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
1546 }
1547
1548 if (attrs != 0)
1549 return -EINVAL;
1550
1551 return 0;
1552}
1553
1554/**
1555 * ovs_match_from_nlattrs - parses Netlink attributes into a flow key and
1556 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1557 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1558 * does not include any don't care bit.
1559 * @match: receives the extracted flow match information.
1560 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1561 * sequence. The fields should of the packet that triggered the creation
1562 * of this flow.
1563 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1564 * attribute specifies the mask field of the wildcarded flow.
1565 */
1566int ovs_match_from_nlattrs(struct sw_flow_match *match,
1567 const struct nlattr *key,
1568 const struct nlattr *mask)
1569{
1570 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
a1c564be
AZ
1571 const struct nlattr *encap;
1572 u64 key_attrs = 0;
1573 u64 mask_attrs = 0;
1574 bool encap_valid = false;
1575 int err;
1576
1577 err = parse_flow_nlattrs(key, a, &key_attrs);
1578 if (err)
1579 return err;
1580
1581 if (key_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
1582 encap = a[OVS_KEY_ATTR_ENCAP];
1583 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1584 if (nla_len(encap)) {
1585 __be16 eth_type = 0; /* ETH_P_8021Q */
1586
1587 if (a[OVS_KEY_ATTR_ETHERTYPE])
1588 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1589
1590 if ((eth_type == htons(ETH_P_8021Q)) && (a[OVS_KEY_ATTR_VLAN])) {
1591 encap_valid = true;
1592 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1593 err = parse_flow_nlattrs(encap, a, &key_attrs);
1b936472
AZ
1594 } else {
1595 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
a1c564be 1596 err = -EINVAL;
1b936472 1597 }
a1c564be 1598
34118cae
BP
1599 if (err)
1600 return err;
36956a7d 1601 }
a1c564be 1602 }
36956a7d 1603
a1c564be
AZ
1604 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
1605 if (err)
1606 return err;
1607
1608 if (mask) {
1a02b768 1609 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
a1c564be
AZ
1610 if (err)
1611 return err;
1612
1613 if ((mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) && encap_valid) {
1614 __be16 eth_type = 0;
1615
063236f1 1616 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1a02b768
JG
1617 if (a[OVS_KEY_ATTR_ETHERTYPE])
1618 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
a1c564be
AZ
1619 if (eth_type == htons(0xffff)) {
1620 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1a02b768
JG
1621 encap = a[OVS_KEY_ATTR_ENCAP];
1622 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
1b936472
AZ
1623 } else {
1624 OVS_NLERR("VLAN frames must have an exact match"
1625 " on the TPID (mask=%x).\n",
1626 ntohs(eth_type));
a1c564be 1627 err = -EINVAL;
1b936472 1628 }
34118cae 1629
34118cae
BP
1630 if (err)
1631 return err;
1632 }
36956a7d 1633
1a02b768 1634 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
a1c564be
AZ
1635 if (err)
1636 return err;
1637 } else {
1638 /* Populate exact match flow's key mask. */
1639 if (match->mask)
1640 ovs_sw_flow_mask_set(match->mask, &match->range, 0xff);
34118cae 1641 }
76abe283 1642
a1c564be 1643 if (!ovs_match_validate(match, key_attrs, mask_attrs))
34118cae 1644 return -EINVAL;
34118cae
BP
1645
1646 return 0;
36956a7d
BP
1647}
1648
80e5eed9 1649/**
850b6b3b 1650 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
c3b36027 1651 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
c3b36027 1652 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
80e5eed9
BP
1653 * sequence.
1654 *
1655 * This parses a series of Netlink attributes that form a flow key, which must
1656 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1657 * get the metadata, that is, the parts of the flow key that cannot be
1658 * extracted from the packet itself.
1659 */
13e24889 1660
a1c564be
AZ
1661int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
1662 const struct nlattr *attr)
80e5eed9 1663{
6694e498 1664 struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
a1c564be
AZ
1665 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1666 u64 attrs = 0;
1667 int err;
1668 struct sw_flow_match match;
80e5eed9 1669
13e24889
PS
1670 flow->key.phy.in_port = DP_MAX_PORTS;
1671 flow->key.phy.priority = 0;
72e8bf28 1672 flow->key.phy.skb_mark = 0;
6694e498 1673 memset(tun_key, 0, sizeof(flow->key.tun_key));
80e5eed9 1674
a1c564be
AZ
1675 err = parse_flow_nlattrs(attr, a, &attrs);
1676 if (err)
80e5eed9 1677 return -EINVAL;
13e24889 1678
46d4c97f
JG
1679 memset(&match, 0, sizeof(match));
1680 match.key = &flow->key;
a1c564be
AZ
1681
1682 err = metadata_from_nlattrs(&match, &attrs, a, false);
1683 if (err)
1684 return err;
13e24889 1685
80e5eed9
BP
1686 return 0;
1687}
1688
a1c564be
AZ
1689int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey,
1690 const struct sw_flow_key *output, struct sk_buff *skb)
36956a7d 1691{
df2c07f4 1692 struct ovs_key_ethernet *eth_key;
fea393b1 1693 struct nlattr *nla, *encap;
36956a7d 1694
c3cc8c03 1695 if (swkey->phy.priority &&
a1c564be 1696 nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
c3cc8c03 1697 goto nla_put_failure;
18c43631 1698
9b405f1a 1699 if (swkey->tun_key.ipv4_dst &&
a1c564be 1700 ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
9b405f1a
PS
1701 goto nla_put_failure;
1702
a1c564be
AZ
1703 if (swkey->phy.in_port != DP_MAX_PORTS) {
1704 /* Exact match upper 16 bits. */
1705 u16 upper_u16;
1706 upper_u16 = (swkey == output) ? 0 : 0xffff;
1707
1708 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1709 (upper_u16 << 16) | output->phy.in_port))
1710 goto nla_put_failure;
1711 }
36956a7d 1712
72e8bf28 1713 if (swkey->phy.skb_mark &&
a1c564be 1714 nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
72e8bf28
AA
1715 goto nla_put_failure;
1716
df2c07f4 1717 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
d6569377
BP
1718 if (!nla)
1719 goto nla_put_failure;
a1c564be 1720
d6569377 1721 eth_key = nla_data(nla);
a1c564be
AZ
1722 memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN);
1723 memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN);
36956a7d 1724
8ddc056d 1725 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
a1c564be
AZ
1726 __be16 eth_type;
1727 eth_type = (swkey == output) ? htons(ETH_P_8021Q) : htons(0xffff) ;
1728 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1729 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
c3cc8c03 1730 goto nla_put_failure;
fea393b1 1731 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
8ddc056d
BP
1732 if (!swkey->eth.tci)
1733 goto unencap;
a1c564be 1734 } else
fea393b1 1735 encap = NULL;
36956a7d 1736
a1c564be 1737 if ((swkey == output) && (swkey->eth.type == htons(ETH_P_802_2)))
fea393b1 1738 goto unencap;
36956a7d 1739
a1c564be
AZ
1740 if (output->eth.type != 0)
1741 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1742 goto nla_put_failure;
36956a7d 1743
76abe283 1744 if (swkey->eth.type == htons(ETH_P_IP)) {
df2c07f4 1745 struct ovs_key_ipv4 *ipv4_key;
36956a7d 1746
df2c07f4 1747 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
d6569377
BP
1748 if (!nla)
1749 goto nla_put_failure;
1750 ipv4_key = nla_data(nla);
a1c564be
AZ
1751 ipv4_key->ipv4_src = output->ipv4.addr.src;
1752 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1753 ipv4_key->ipv4_proto = output->ip.proto;
1754 ipv4_key->ipv4_tos = output->ip.tos;
1755 ipv4_key->ipv4_ttl = output->ip.ttl;
1756 ipv4_key->ipv4_frag = output->ip.frag;
76abe283 1757 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
df2c07f4 1758 struct ovs_key_ipv6 *ipv6_key;
d31f1109 1759
df2c07f4 1760 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
d31f1109
JP
1761 if (!nla)
1762 goto nla_put_failure;
1763 ipv6_key = nla_data(nla);
a1c564be 1764 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
d31f1109 1765 sizeof(ipv6_key->ipv6_src));
a1c564be 1766 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
d31f1109 1767 sizeof(ipv6_key->ipv6_dst));
a1c564be
AZ
1768 ipv6_key->ipv6_label = output->ipv6.label;
1769 ipv6_key->ipv6_proto = output->ip.proto;
1770 ipv6_key->ipv6_tclass = output->ip.tos;
1771 ipv6_key->ipv6_hlimit = output->ip.ttl;
1772 ipv6_key->ipv6_frag = output->ip.frag;
8087f5ff
MM
1773 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1774 swkey->eth.type == htons(ETH_P_RARP)) {
df2c07f4 1775 struct ovs_key_arp *arp_key;
d31f1109 1776
df2c07f4 1777 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
d31f1109
JP
1778 if (!nla)
1779 goto nla_put_failure;
1780 arp_key = nla_data(nla);
df2c07f4 1781 memset(arp_key, 0, sizeof(struct ovs_key_arp));
a1c564be
AZ
1782 arp_key->arp_sip = output->ipv4.addr.src;
1783 arp_key->arp_tip = output->ipv4.addr.dst;
1784 arp_key->arp_op = htons(output->ip.proto);
1785 memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN);
1786 memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN);
d31f1109
JP
1787 }
1788
7257b535
BP
1789 if ((swkey->eth.type == htons(ETH_P_IP) ||
1790 swkey->eth.type == htons(ETH_P_IPV6)) &&
9e44d715 1791 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
36956a7d 1792
28bad473 1793 if (swkey->ip.proto == IPPROTO_TCP) {
df2c07f4 1794 struct ovs_key_tcp *tcp_key;
36956a7d 1795
df2c07f4 1796 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
d6569377
BP
1797 if (!nla)
1798 goto nla_put_failure;
1799 tcp_key = nla_data(nla);
76abe283 1800 if (swkey->eth.type == htons(ETH_P_IP)) {
a1c564be
AZ
1801 tcp_key->tcp_src = output->ipv4.tp.src;
1802 tcp_key->tcp_dst = output->ipv4.tp.dst;
76abe283 1803 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
a1c564be
AZ
1804 tcp_key->tcp_src = output->ipv6.tp.src;
1805 tcp_key->tcp_dst = output->ipv6.tp.dst;
76abe283 1806 }
28bad473 1807 } else if (swkey->ip.proto == IPPROTO_UDP) {
df2c07f4 1808 struct ovs_key_udp *udp_key;
36956a7d 1809
df2c07f4 1810 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
d6569377
BP
1811 if (!nla)
1812 goto nla_put_failure;
1813 udp_key = nla_data(nla);
76abe283 1814 if (swkey->eth.type == htons(ETH_P_IP)) {
a1c564be
AZ
1815 udp_key->udp_src = output->ipv4.tp.src;
1816 udp_key->udp_dst = output->ipv4.tp.dst;
76abe283 1817 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
a1c564be
AZ
1818 udp_key->udp_src = output->ipv6.tp.src;
1819 udp_key->udp_dst = output->ipv6.tp.dst;
76abe283
AE
1820 }
1821 } else if (swkey->eth.type == htons(ETH_P_IP) &&
28bad473 1822 swkey->ip.proto == IPPROTO_ICMP) {
df2c07f4 1823 struct ovs_key_icmp *icmp_key;
36956a7d 1824
df2c07f4 1825 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
d6569377
BP
1826 if (!nla)
1827 goto nla_put_failure;
1828 icmp_key = nla_data(nla);
a1c564be
AZ
1829 icmp_key->icmp_type = ntohs(output->ipv4.tp.src);
1830 icmp_key->icmp_code = ntohs(output->ipv4.tp.dst);
76abe283 1831 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
28bad473 1832 swkey->ip.proto == IPPROTO_ICMPV6) {
df2c07f4 1833 struct ovs_key_icmpv6 *icmpv6_key;
36956a7d 1834
df2c07f4 1835 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
bfef4717 1836 sizeof(*icmpv6_key));
d31f1109
JP
1837 if (!nla)
1838 goto nla_put_failure;
1839 icmpv6_key = nla_data(nla);
a1c564be
AZ
1840 icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src);
1841 icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst);
685a51a5 1842
bfef4717
JG
1843 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1844 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
df2c07f4 1845 struct ovs_key_nd *nd_key;
685a51a5 1846
df2c07f4 1847 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
685a51a5
JP
1848 if (!nla)
1849 goto nla_put_failure;
1850 nd_key = nla_data(nla);
a1c564be 1851 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
685a51a5 1852 sizeof(nd_key->nd_target));
a1c564be
AZ
1853 memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN);
1854 memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN);
685a51a5 1855 }
d31f1109 1856 }
36956a7d
BP
1857 }
1858
fea393b1
BP
1859unencap:
1860 if (encap)
1861 nla_nest_end(skb, encap);
1862
d6569377 1863 return 0;
36956a7d 1864
d6569377
BP
1865nla_put_failure:
1866 return -EMSGSIZE;
8d5ebd83
JG
1867}
1868
064af421
BP
1869/* Initializes the flow module.
1870 * Returns zero if successful or a negative error code. */
850b6b3b 1871int ovs_flow_init(void)
064af421
BP
1872{
1873 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1874 0, NULL);
1875 if (flow_cache == NULL)
1876 return -ENOMEM;
1877
1878 return 0;
1879}
1880
1881/* Uninitializes the flow module. */
850b6b3b 1882void ovs_flow_exit(void)
064af421
BP
1883{
1884 kmem_cache_destroy(flow_cache);
1885}
a1c564be
AZ
1886
1887struct sw_flow_mask *ovs_sw_flow_mask_alloc(void)
1888{
1889 struct sw_flow_mask *mask;
1890
1891 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
1892 if (mask)
1893 mask->ref_count = 0;
1894
1895 return mask;
1896}
1897
1898void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask)
1899{
1900 mask->ref_count++;
1901}
1902
1903static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu)
1904{
1905 struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu);
1906
1907 kfree(mask);
1908}
1909
1910void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
1911{
1912 if (!mask)
1913 return;
1914
1915 BUG_ON(!mask->ref_count);
1916 mask->ref_count--;
1917
1918 if (!mask->ref_count) {
1919 list_del_rcu(&mask->list);
1920 if (deferred)
1921 call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb);
1922 else
1923 kfree(mask);
1924 }
1925}
1926
1927static bool ovs_sw_flow_mask_equal(const struct sw_flow_mask *a,
1928 const struct sw_flow_mask *b)
1929{
1930 u8 *a_ = (u8 *)&a->key + a->range.start;
1931 u8 *b_ = (u8 *)&b->key + b->range.start;
1932
1933 return (a->range.end == b->range.end)
1934 && (a->range.start == b->range.start)
1935 && (memcmp(a_, b_, ovs_sw_flow_mask_actual_size(a)) == 0);
1936}
1937
1938struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
1939 const struct sw_flow_mask *mask)
1940{
1941 struct list_head *ml;
1942
ede77a46 1943 list_for_each(ml, tbl->mask_list) {
a1c564be
AZ
1944 struct sw_flow_mask *m;
1945 m = container_of(ml, struct sw_flow_mask, list);
1946 if (ovs_sw_flow_mask_equal(mask, m))
1947 return m;
1948 }
1949
1950 return NULL;
1951}
1952
1953/**
1954 * add a new mask into the mask list.
1955 * The caller needs to make sure that 'mask' is not the same
1956 * as any masks that are already on the list.
1957 */
1958void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask)
1959{
ede77a46 1960 list_add_rcu(&mask->list, tbl->mask_list);
a1c564be
AZ
1961}
1962
1963/**
1964 * Set 'range' fields in the mask to the value of 'val'.
1965 */
1966static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
1967 struct sw_flow_key_range *range, u8 val)
1968{
1969 u8 *m = (u8 *)&mask->key + range->start;
1970
1971 mask->range = *range;
1972 memset(m, val, ovs_sw_flow_mask_size_roundup(mask));
1973}