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