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