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