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