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