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