openvswitch: Fix parsing invalid LLC/SNAP ethertypes
[linux/fpc-iii.git] / net / openvswitch / flow.c
blob0c98d406124b8793c705d1a1271f45c8e6a80d05
1 /*
2 * Copyright (c) 2007-2011 Nicira, Inc.
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.
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.
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
19 #include "flow.h"
20 #include "datapath.h"
21 #include <linux/uaccess.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <net/llc_pdu.h>
27 #include <linux/kernel.h>
28 #include <linux/jhash.h>
29 #include <linux/jiffies.h>
30 #include <linux/llc.h>
31 #include <linux/module.h>
32 #include <linux/in.h>
33 #include <linux/rcupdate.h>
34 #include <linux/if_arp.h>
35 #include <linux/ip.h>
36 #include <linux/ipv6.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/icmp.h>
40 #include <linux/icmpv6.h>
41 #include <linux/rculist.h>
42 #include <net/ip.h>
43 #include <net/ipv6.h>
44 #include <net/ndisc.h>
46 static struct kmem_cache *flow_cache;
48 static int check_header(struct sk_buff *skb, int len)
50 if (unlikely(skb->len < len))
51 return -EINVAL;
52 if (unlikely(!pskb_may_pull(skb, len)))
53 return -ENOMEM;
54 return 0;
57 static bool arphdr_ok(struct sk_buff *skb)
59 return pskb_may_pull(skb, skb_network_offset(skb) +
60 sizeof(struct arp_eth_header));
63 static int check_iphdr(struct sk_buff *skb)
65 unsigned int nh_ofs = skb_network_offset(skb);
66 unsigned int ip_len;
67 int err;
69 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
70 if (unlikely(err))
71 return err;
73 ip_len = ip_hdrlen(skb);
74 if (unlikely(ip_len < sizeof(struct iphdr) ||
75 skb->len < nh_ofs + ip_len))
76 return -EINVAL;
78 skb_set_transport_header(skb, nh_ofs + ip_len);
79 return 0;
82 static bool tcphdr_ok(struct sk_buff *skb)
84 int th_ofs = skb_transport_offset(skb);
85 int tcp_len;
87 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
88 return false;
90 tcp_len = tcp_hdrlen(skb);
91 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
92 skb->len < th_ofs + tcp_len))
93 return false;
95 return true;
98 static bool udphdr_ok(struct sk_buff *skb)
100 return pskb_may_pull(skb, skb_transport_offset(skb) +
101 sizeof(struct udphdr));
104 static bool icmphdr_ok(struct sk_buff *skb)
106 return pskb_may_pull(skb, skb_transport_offset(skb) +
107 sizeof(struct icmphdr));
110 u64 ovs_flow_used_time(unsigned long flow_jiffies)
112 struct timespec cur_ts;
113 u64 cur_ms, idle_ms;
115 ktime_get_ts(&cur_ts);
116 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
117 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
118 cur_ts.tv_nsec / NSEC_PER_MSEC;
120 return cur_ms - idle_ms;
123 #define SW_FLOW_KEY_OFFSET(field) \
124 (offsetof(struct sw_flow_key, field) + \
125 FIELD_SIZEOF(struct sw_flow_key, field))
127 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key,
128 int *key_lenp)
130 unsigned int nh_ofs = skb_network_offset(skb);
131 unsigned int nh_len;
132 int payload_ofs;
133 struct ipv6hdr *nh;
134 uint8_t nexthdr;
135 __be16 frag_off;
136 int err;
138 *key_lenp = SW_FLOW_KEY_OFFSET(ipv6.label);
140 err = check_header(skb, nh_ofs + sizeof(*nh));
141 if (unlikely(err))
142 return err;
144 nh = ipv6_hdr(skb);
145 nexthdr = nh->nexthdr;
146 payload_ofs = (u8 *)(nh + 1) - skb->data;
148 key->ip.proto = NEXTHDR_NONE;
149 key->ip.tos = ipv6_get_dsfield(nh);
150 key->ip.ttl = nh->hop_limit;
151 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
152 key->ipv6.addr.src = nh->saddr;
153 key->ipv6.addr.dst = nh->daddr;
155 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
156 if (unlikely(payload_ofs < 0))
157 return -EINVAL;
159 if (frag_off) {
160 if (frag_off & htons(~0x7))
161 key->ip.frag = OVS_FRAG_TYPE_LATER;
162 else
163 key->ip.frag = OVS_FRAG_TYPE_FIRST;
166 nh_len = payload_ofs - nh_ofs;
167 skb_set_transport_header(skb, nh_ofs + nh_len);
168 key->ip.proto = nexthdr;
169 return nh_len;
172 static bool icmp6hdr_ok(struct sk_buff *skb)
174 return pskb_may_pull(skb, skb_transport_offset(skb) +
175 sizeof(struct icmp6hdr));
178 #define TCP_FLAGS_OFFSET 13
179 #define TCP_FLAG_MASK 0x3f
181 void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
183 u8 tcp_flags = 0;
185 if ((flow->key.eth.type == htons(ETH_P_IP) ||
186 flow->key.eth.type == htons(ETH_P_IPV6)) &&
187 flow->key.ip.proto == IPPROTO_TCP &&
188 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
189 u8 *tcp = (u8 *)tcp_hdr(skb);
190 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
193 spin_lock(&flow->lock);
194 flow->used = jiffies;
195 flow->packet_count++;
196 flow->byte_count += skb->len;
197 flow->tcp_flags |= tcp_flags;
198 spin_unlock(&flow->lock);
201 struct sw_flow_actions *ovs_flow_actions_alloc(const struct nlattr *actions)
203 int actions_len = nla_len(actions);
204 struct sw_flow_actions *sfa;
206 if (actions_len > MAX_ACTIONS_BUFSIZE)
207 return ERR_PTR(-EINVAL);
209 sfa = kmalloc(sizeof(*sfa) + actions_len, GFP_KERNEL);
210 if (!sfa)
211 return ERR_PTR(-ENOMEM);
213 sfa->actions_len = actions_len;
214 memcpy(sfa->actions, nla_data(actions), actions_len);
215 return sfa;
218 struct sw_flow *ovs_flow_alloc(void)
220 struct sw_flow *flow;
222 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
223 if (!flow)
224 return ERR_PTR(-ENOMEM);
226 spin_lock_init(&flow->lock);
227 flow->sf_acts = NULL;
229 return flow;
232 static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
234 hash = jhash_1word(hash, table->hash_seed);
235 return flex_array_get(table->buckets,
236 (hash & (table->n_buckets - 1)));
239 static struct flex_array *alloc_buckets(unsigned int n_buckets)
241 struct flex_array *buckets;
242 int i, err;
244 buckets = flex_array_alloc(sizeof(struct hlist_head *),
245 n_buckets, GFP_KERNEL);
246 if (!buckets)
247 return NULL;
249 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
250 if (err) {
251 flex_array_free(buckets);
252 return NULL;
255 for (i = 0; i < n_buckets; i++)
256 INIT_HLIST_HEAD((struct hlist_head *)
257 flex_array_get(buckets, i));
259 return buckets;
262 static void free_buckets(struct flex_array *buckets)
264 flex_array_free(buckets);
267 struct flow_table *ovs_flow_tbl_alloc(int new_size)
269 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
271 if (!table)
272 return NULL;
274 table->buckets = alloc_buckets(new_size);
276 if (!table->buckets) {
277 kfree(table);
278 return NULL;
280 table->n_buckets = new_size;
281 table->count = 0;
282 table->node_ver = 0;
283 table->keep_flows = false;
284 get_random_bytes(&table->hash_seed, sizeof(u32));
286 return table;
289 void ovs_flow_tbl_destroy(struct flow_table *table)
291 int i;
293 if (!table)
294 return;
296 if (table->keep_flows)
297 goto skip_flows;
299 for (i = 0; i < table->n_buckets; i++) {
300 struct sw_flow *flow;
301 struct hlist_head *head = flex_array_get(table->buckets, i);
302 struct hlist_node *node, *n;
303 int ver = table->node_ver;
305 hlist_for_each_entry_safe(flow, node, n, head, hash_node[ver]) {
306 hlist_del_rcu(&flow->hash_node[ver]);
307 ovs_flow_free(flow);
311 skip_flows:
312 free_buckets(table->buckets);
313 kfree(table);
316 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
318 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
320 ovs_flow_tbl_destroy(table);
323 void ovs_flow_tbl_deferred_destroy(struct flow_table *table)
325 if (!table)
326 return;
328 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
331 struct sw_flow *ovs_flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *last)
333 struct sw_flow *flow;
334 struct hlist_head *head;
335 struct hlist_node *n;
336 int ver;
337 int i;
339 ver = table->node_ver;
340 while (*bucket < table->n_buckets) {
341 i = 0;
342 head = flex_array_get(table->buckets, *bucket);
343 hlist_for_each_entry_rcu(flow, n, head, hash_node[ver]) {
344 if (i < *last) {
345 i++;
346 continue;
348 *last = i + 1;
349 return flow;
351 (*bucket)++;
352 *last = 0;
355 return NULL;
358 static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
360 int old_ver;
361 int i;
363 old_ver = old->node_ver;
364 new->node_ver = !old_ver;
366 /* Insert in new table. */
367 for (i = 0; i < old->n_buckets; i++) {
368 struct sw_flow *flow;
369 struct hlist_head *head;
370 struct hlist_node *n;
372 head = flex_array_get(old->buckets, i);
374 hlist_for_each_entry(flow, n, head, hash_node[old_ver])
375 ovs_flow_tbl_insert(new, flow);
377 old->keep_flows = true;
380 static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
382 struct flow_table *new_table;
384 new_table = ovs_flow_tbl_alloc(n_buckets);
385 if (!new_table)
386 return ERR_PTR(-ENOMEM);
388 flow_table_copy_flows(table, new_table);
390 return new_table;
393 struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
395 return __flow_tbl_rehash(table, table->n_buckets);
398 struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
400 return __flow_tbl_rehash(table, table->n_buckets * 2);
403 void ovs_flow_free(struct sw_flow *flow)
405 if (unlikely(!flow))
406 return;
408 kfree((struct sf_flow_acts __force *)flow->sf_acts);
409 kmem_cache_free(flow_cache, flow);
412 /* RCU callback used by ovs_flow_deferred_free. */
413 static void rcu_free_flow_callback(struct rcu_head *rcu)
415 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
417 ovs_flow_free(flow);
420 /* Schedules 'flow' to be freed after the next RCU grace period.
421 * The caller must hold rcu_read_lock for this to be sensible. */
422 void ovs_flow_deferred_free(struct sw_flow *flow)
424 call_rcu(&flow->rcu, rcu_free_flow_callback);
427 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
428 * The caller must hold rcu_read_lock for this to be sensible. */
429 void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
431 kfree_rcu(sf_acts, rcu);
434 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
436 struct qtag_prefix {
437 __be16 eth_type; /* ETH_P_8021Q */
438 __be16 tci;
440 struct qtag_prefix *qp;
442 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
443 return 0;
445 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
446 sizeof(__be16))))
447 return -ENOMEM;
449 qp = (struct qtag_prefix *) skb->data;
450 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
451 __skb_pull(skb, sizeof(struct qtag_prefix));
453 return 0;
456 static __be16 parse_ethertype(struct sk_buff *skb)
458 struct llc_snap_hdr {
459 u8 dsap; /* Always 0xAA */
460 u8 ssap; /* Always 0xAA */
461 u8 ctrl;
462 u8 oui[3];
463 __be16 ethertype;
465 struct llc_snap_hdr *llc;
466 __be16 proto;
468 proto = *(__be16 *) skb->data;
469 __skb_pull(skb, sizeof(__be16));
471 if (ntohs(proto) >= 1536)
472 return proto;
474 if (skb->len < sizeof(struct llc_snap_hdr))
475 return htons(ETH_P_802_2);
477 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
478 return htons(0);
480 llc = (struct llc_snap_hdr *) skb->data;
481 if (llc->dsap != LLC_SAP_SNAP ||
482 llc->ssap != LLC_SAP_SNAP ||
483 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
484 return htons(ETH_P_802_2);
486 __skb_pull(skb, sizeof(struct llc_snap_hdr));
488 if (ntohs(llc->ethertype) >= 1536)
489 return llc->ethertype;
491 return htons(ETH_P_802_2);
494 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
495 int *key_lenp, int nh_len)
497 struct icmp6hdr *icmp = icmp6_hdr(skb);
498 int error = 0;
499 int key_len;
501 /* The ICMPv6 type and code fields use the 16-bit transport port
502 * fields, so we need to store them in 16-bit network byte order.
504 key->ipv6.tp.src = htons(icmp->icmp6_type);
505 key->ipv6.tp.dst = htons(icmp->icmp6_code);
506 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
508 if (icmp->icmp6_code == 0 &&
509 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
510 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
511 int icmp_len = skb->len - skb_transport_offset(skb);
512 struct nd_msg *nd;
513 int offset;
515 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
517 /* In order to process neighbor discovery options, we need the
518 * entire packet.
520 if (unlikely(icmp_len < sizeof(*nd)))
521 goto out;
522 if (unlikely(skb_linearize(skb))) {
523 error = -ENOMEM;
524 goto out;
527 nd = (struct nd_msg *)skb_transport_header(skb);
528 key->ipv6.nd.target = nd->target;
529 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
531 icmp_len -= sizeof(*nd);
532 offset = 0;
533 while (icmp_len >= 8) {
534 struct nd_opt_hdr *nd_opt =
535 (struct nd_opt_hdr *)(nd->opt + offset);
536 int opt_len = nd_opt->nd_opt_len * 8;
538 if (unlikely(!opt_len || opt_len > icmp_len))
539 goto invalid;
541 /* Store the link layer address if the appropriate
542 * option is provided. It is considered an error if
543 * the same link layer option is specified twice.
545 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
546 && opt_len == 8) {
547 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
548 goto invalid;
549 memcpy(key->ipv6.nd.sll,
550 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
551 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
552 && opt_len == 8) {
553 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
554 goto invalid;
555 memcpy(key->ipv6.nd.tll,
556 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
559 icmp_len -= opt_len;
560 offset += opt_len;
564 goto out;
566 invalid:
567 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
568 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
569 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
571 out:
572 *key_lenp = key_len;
573 return error;
577 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
578 * @skb: sk_buff that contains the frame, with skb->data pointing to the
579 * Ethernet header
580 * @in_port: port number on which @skb was received.
581 * @key: output flow key
582 * @key_lenp: length of output flow key
584 * The caller must ensure that skb->len >= ETH_HLEN.
586 * Returns 0 if successful, otherwise a negative errno value.
588 * Initializes @skb header pointers as follows:
590 * - skb->mac_header: the Ethernet header.
592 * - skb->network_header: just past the Ethernet header, or just past the
593 * VLAN header, to the first byte of the Ethernet payload.
595 * - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6
596 * on output, then just past the IP header, if one is present and
597 * of a correct length, otherwise the same as skb->network_header.
598 * For other key->dl_type values it is left untouched.
600 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
601 int *key_lenp)
603 int error = 0;
604 int key_len = SW_FLOW_KEY_OFFSET(eth);
605 struct ethhdr *eth;
607 memset(key, 0, sizeof(*key));
609 key->phy.priority = skb->priority;
610 key->phy.in_port = in_port;
611 key->phy.skb_mark = skb->mark;
613 skb_reset_mac_header(skb);
615 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
616 * header in the linear data area.
618 eth = eth_hdr(skb);
619 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
620 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
622 __skb_pull(skb, 2 * ETH_ALEN);
624 if (vlan_tx_tag_present(skb))
625 key->eth.tci = htons(skb->vlan_tci);
626 else if (eth->h_proto == htons(ETH_P_8021Q))
627 if (unlikely(parse_vlan(skb, key)))
628 return -ENOMEM;
630 key->eth.type = parse_ethertype(skb);
631 if (unlikely(key->eth.type == htons(0)))
632 return -ENOMEM;
634 skb_reset_network_header(skb);
635 __skb_push(skb, skb->data - skb_mac_header(skb));
637 /* Network layer. */
638 if (key->eth.type == htons(ETH_P_IP)) {
639 struct iphdr *nh;
640 __be16 offset;
642 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
644 error = check_iphdr(skb);
645 if (unlikely(error)) {
646 if (error == -EINVAL) {
647 skb->transport_header = skb->network_header;
648 error = 0;
650 goto out;
653 nh = ip_hdr(skb);
654 key->ipv4.addr.src = nh->saddr;
655 key->ipv4.addr.dst = nh->daddr;
657 key->ip.proto = nh->protocol;
658 key->ip.tos = nh->tos;
659 key->ip.ttl = nh->ttl;
661 offset = nh->frag_off & htons(IP_OFFSET);
662 if (offset) {
663 key->ip.frag = OVS_FRAG_TYPE_LATER;
664 goto out;
666 if (nh->frag_off & htons(IP_MF) ||
667 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
668 key->ip.frag = OVS_FRAG_TYPE_FIRST;
670 /* Transport layer. */
671 if (key->ip.proto == IPPROTO_TCP) {
672 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
673 if (tcphdr_ok(skb)) {
674 struct tcphdr *tcp = tcp_hdr(skb);
675 key->ipv4.tp.src = tcp->source;
676 key->ipv4.tp.dst = tcp->dest;
678 } else if (key->ip.proto == IPPROTO_UDP) {
679 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
680 if (udphdr_ok(skb)) {
681 struct udphdr *udp = udp_hdr(skb);
682 key->ipv4.tp.src = udp->source;
683 key->ipv4.tp.dst = udp->dest;
685 } else if (key->ip.proto == IPPROTO_ICMP) {
686 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
687 if (icmphdr_ok(skb)) {
688 struct icmphdr *icmp = icmp_hdr(skb);
689 /* The ICMP type and code fields use the 16-bit
690 * transport port fields, so we need to store
691 * them in 16-bit network byte order. */
692 key->ipv4.tp.src = htons(icmp->type);
693 key->ipv4.tp.dst = htons(icmp->code);
697 } else if ((key->eth.type == htons(ETH_P_ARP) ||
698 key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
699 struct arp_eth_header *arp;
701 arp = (struct arp_eth_header *)skb_network_header(skb);
703 if (arp->ar_hrd == htons(ARPHRD_ETHER)
704 && arp->ar_pro == htons(ETH_P_IP)
705 && arp->ar_hln == ETH_ALEN
706 && arp->ar_pln == 4) {
708 /* We only match on the lower 8 bits of the opcode. */
709 if (ntohs(arp->ar_op) <= 0xff)
710 key->ip.proto = ntohs(arp->ar_op);
711 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
712 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
713 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
714 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
715 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
717 } else if (key->eth.type == htons(ETH_P_IPV6)) {
718 int nh_len; /* IPv6 Header + Extensions */
720 nh_len = parse_ipv6hdr(skb, key, &key_len);
721 if (unlikely(nh_len < 0)) {
722 if (nh_len == -EINVAL)
723 skb->transport_header = skb->network_header;
724 else
725 error = nh_len;
726 goto out;
729 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
730 goto out;
731 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
732 key->ip.frag = OVS_FRAG_TYPE_FIRST;
734 /* Transport layer. */
735 if (key->ip.proto == NEXTHDR_TCP) {
736 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
737 if (tcphdr_ok(skb)) {
738 struct tcphdr *tcp = tcp_hdr(skb);
739 key->ipv6.tp.src = tcp->source;
740 key->ipv6.tp.dst = tcp->dest;
742 } else if (key->ip.proto == NEXTHDR_UDP) {
743 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
744 if (udphdr_ok(skb)) {
745 struct udphdr *udp = udp_hdr(skb);
746 key->ipv6.tp.src = udp->source;
747 key->ipv6.tp.dst = udp->dest;
749 } else if (key->ip.proto == NEXTHDR_ICMP) {
750 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
751 if (icmp6hdr_ok(skb)) {
752 error = parse_icmpv6(skb, key, &key_len, nh_len);
753 if (error < 0)
754 goto out;
759 out:
760 *key_lenp = key_len;
761 return error;
764 u32 ovs_flow_hash(const struct sw_flow_key *key, int key_len)
766 return jhash2((u32 *)key, DIV_ROUND_UP(key_len, sizeof(u32)), 0);
769 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table,
770 struct sw_flow_key *key, int key_len)
772 struct sw_flow *flow;
773 struct hlist_node *n;
774 struct hlist_head *head;
775 u32 hash;
777 hash = ovs_flow_hash(key, key_len);
779 head = find_bucket(table, hash);
780 hlist_for_each_entry_rcu(flow, n, head, hash_node[table->node_ver]) {
782 if (flow->hash == hash &&
783 !memcmp(&flow->key, key, key_len)) {
784 return flow;
787 return NULL;
790 void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
792 struct hlist_head *head;
794 head = find_bucket(table, flow->hash);
795 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
796 table->count++;
799 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
801 hlist_del_rcu(&flow->hash_node[table->node_ver]);
802 table->count--;
803 BUG_ON(table->count < 0);
806 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
807 const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
808 [OVS_KEY_ATTR_ENCAP] = -1,
809 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
810 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
811 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
812 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
813 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
814 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
815 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
816 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
817 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
818 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
819 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
820 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
821 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
822 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
825 static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
826 const struct nlattr *a[], u32 *attrs)
828 const struct ovs_key_icmp *icmp_key;
829 const struct ovs_key_tcp *tcp_key;
830 const struct ovs_key_udp *udp_key;
832 switch (swkey->ip.proto) {
833 case IPPROTO_TCP:
834 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
835 return -EINVAL;
836 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
838 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
839 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
840 swkey->ipv4.tp.src = tcp_key->tcp_src;
841 swkey->ipv4.tp.dst = tcp_key->tcp_dst;
842 break;
844 case IPPROTO_UDP:
845 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
846 return -EINVAL;
847 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
849 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
850 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
851 swkey->ipv4.tp.src = udp_key->udp_src;
852 swkey->ipv4.tp.dst = udp_key->udp_dst;
853 break;
855 case IPPROTO_ICMP:
856 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
857 return -EINVAL;
858 *attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
860 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
861 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
862 swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
863 swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
864 break;
867 return 0;
870 static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
871 const struct nlattr *a[], u32 *attrs)
873 const struct ovs_key_icmpv6 *icmpv6_key;
874 const struct ovs_key_tcp *tcp_key;
875 const struct ovs_key_udp *udp_key;
877 switch (swkey->ip.proto) {
878 case IPPROTO_TCP:
879 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
880 return -EINVAL;
881 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
883 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
884 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
885 swkey->ipv6.tp.src = tcp_key->tcp_src;
886 swkey->ipv6.tp.dst = tcp_key->tcp_dst;
887 break;
889 case IPPROTO_UDP:
890 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
891 return -EINVAL;
892 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
894 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
895 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
896 swkey->ipv6.tp.src = udp_key->udp_src;
897 swkey->ipv6.tp.dst = udp_key->udp_dst;
898 break;
900 case IPPROTO_ICMPV6:
901 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
902 return -EINVAL;
903 *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
905 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
906 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
907 swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
908 swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
910 if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
911 swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
912 const struct ovs_key_nd *nd_key;
914 if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
915 return -EINVAL;
916 *attrs &= ~(1 << OVS_KEY_ATTR_ND);
918 *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
919 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
920 memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
921 sizeof(swkey->ipv6.nd.target));
922 memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
923 memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
925 break;
928 return 0;
931 static int parse_flow_nlattrs(const struct nlattr *attr,
932 const struct nlattr *a[], u32 *attrsp)
934 const struct nlattr *nla;
935 u32 attrs;
936 int rem;
938 attrs = 0;
939 nla_for_each_nested(nla, attr, rem) {
940 u16 type = nla_type(nla);
941 int expected_len;
943 if (type > OVS_KEY_ATTR_MAX || attrs & (1 << type))
944 return -EINVAL;
946 expected_len = ovs_key_lens[type];
947 if (nla_len(nla) != expected_len && expected_len != -1)
948 return -EINVAL;
950 attrs |= 1 << type;
951 a[type] = nla;
953 if (rem)
954 return -EINVAL;
956 *attrsp = attrs;
957 return 0;
961 * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
962 * @swkey: receives the extracted flow key.
963 * @key_lenp: number of bytes used in @swkey.
964 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
965 * sequence.
967 int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
968 const struct nlattr *attr)
970 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
971 const struct ovs_key_ethernet *eth_key;
972 int key_len;
973 u32 attrs;
974 int err;
976 memset(swkey, 0, sizeof(struct sw_flow_key));
977 key_len = SW_FLOW_KEY_OFFSET(eth);
979 err = parse_flow_nlattrs(attr, a, &attrs);
980 if (err)
981 return err;
983 /* Metadata attributes. */
984 if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
985 swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
986 attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
988 if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
989 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
990 if (in_port >= DP_MAX_PORTS)
991 return -EINVAL;
992 swkey->phy.in_port = in_port;
993 attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
994 } else {
995 swkey->phy.in_port = DP_MAX_PORTS;
997 if (attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
998 swkey->phy.skb_mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
999 attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1002 /* Data attributes. */
1003 if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
1004 return -EINVAL;
1005 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
1007 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1008 memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
1009 memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
1011 if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
1012 nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
1013 const struct nlattr *encap;
1014 __be16 tci;
1016 if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
1017 (1 << OVS_KEY_ATTR_ETHERTYPE) |
1018 (1 << OVS_KEY_ATTR_ENCAP)))
1019 return -EINVAL;
1021 encap = a[OVS_KEY_ATTR_ENCAP];
1022 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1023 if (tci & htons(VLAN_TAG_PRESENT)) {
1024 swkey->eth.tci = tci;
1026 err = parse_flow_nlattrs(encap, a, &attrs);
1027 if (err)
1028 return err;
1029 } else if (!tci) {
1030 /* Corner case for truncated 802.1Q header. */
1031 if (nla_len(encap))
1032 return -EINVAL;
1034 swkey->eth.type = htons(ETH_P_8021Q);
1035 *key_lenp = key_len;
1036 return 0;
1037 } else {
1038 return -EINVAL;
1042 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1043 swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1044 if (ntohs(swkey->eth.type) < 1536)
1045 return -EINVAL;
1046 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1047 } else {
1048 swkey->eth.type = htons(ETH_P_802_2);
1051 if (swkey->eth.type == htons(ETH_P_IP)) {
1052 const struct ovs_key_ipv4 *ipv4_key;
1054 if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
1055 return -EINVAL;
1056 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1058 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
1059 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1060 if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
1061 return -EINVAL;
1062 swkey->ip.proto = ipv4_key->ipv4_proto;
1063 swkey->ip.tos = ipv4_key->ipv4_tos;
1064 swkey->ip.ttl = ipv4_key->ipv4_ttl;
1065 swkey->ip.frag = ipv4_key->ipv4_frag;
1066 swkey->ipv4.addr.src = ipv4_key->ipv4_src;
1067 swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
1069 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1070 err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1071 if (err)
1072 return err;
1074 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1075 const struct ovs_key_ipv6 *ipv6_key;
1077 if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
1078 return -EINVAL;
1079 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1081 key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
1082 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1083 if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
1084 return -EINVAL;
1085 swkey->ipv6.label = ipv6_key->ipv6_label;
1086 swkey->ip.proto = ipv6_key->ipv6_proto;
1087 swkey->ip.tos = ipv6_key->ipv6_tclass;
1088 swkey->ip.ttl = ipv6_key->ipv6_hlimit;
1089 swkey->ip.frag = ipv6_key->ipv6_frag;
1090 memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
1091 sizeof(swkey->ipv6.addr.src));
1092 memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
1093 sizeof(swkey->ipv6.addr.dst));
1095 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1096 err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1097 if (err)
1098 return err;
1100 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1101 swkey->eth.type == htons(ETH_P_RARP)) {
1102 const struct ovs_key_arp *arp_key;
1104 if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
1105 return -EINVAL;
1106 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1108 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
1109 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1110 swkey->ipv4.addr.src = arp_key->arp_sip;
1111 swkey->ipv4.addr.dst = arp_key->arp_tip;
1112 if (arp_key->arp_op & htons(0xff00))
1113 return -EINVAL;
1114 swkey->ip.proto = ntohs(arp_key->arp_op);
1115 memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
1116 memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
1119 if (attrs)
1120 return -EINVAL;
1121 *key_lenp = key_len;
1123 return 0;
1127 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1128 * @priority: receives the skb priority
1129 * @mark: receives the skb mark
1130 * @in_port: receives the extracted input port.
1131 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1132 * sequence.
1134 * This parses a series of Netlink attributes that form a flow key, which must
1135 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1136 * get the metadata, that is, the parts of the flow key that cannot be
1137 * extracted from the packet itself.
1139 int ovs_flow_metadata_from_nlattrs(u32 *priority, u32 *mark, u16 *in_port,
1140 const struct nlattr *attr)
1142 const struct nlattr *nla;
1143 int rem;
1145 *in_port = DP_MAX_PORTS;
1146 *priority = 0;
1147 *mark = 0;
1149 nla_for_each_nested(nla, attr, rem) {
1150 int type = nla_type(nla);
1152 if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
1153 if (nla_len(nla) != ovs_key_lens[type])
1154 return -EINVAL;
1156 switch (type) {
1157 case OVS_KEY_ATTR_PRIORITY:
1158 *priority = nla_get_u32(nla);
1159 break;
1161 case OVS_KEY_ATTR_IN_PORT:
1162 if (nla_get_u32(nla) >= DP_MAX_PORTS)
1163 return -EINVAL;
1164 *in_port = nla_get_u32(nla);
1165 break;
1167 case OVS_KEY_ATTR_SKB_MARK:
1168 *mark = nla_get_u32(nla);
1169 break;
1173 if (rem)
1174 return -EINVAL;
1175 return 0;
1178 int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
1180 struct ovs_key_ethernet *eth_key;
1181 struct nlattr *nla, *encap;
1183 if (swkey->phy.priority &&
1184 nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority))
1185 goto nla_put_failure;
1187 if (swkey->phy.in_port != DP_MAX_PORTS &&
1188 nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port))
1189 goto nla_put_failure;
1191 if (swkey->phy.skb_mark &&
1192 nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, swkey->phy.skb_mark))
1193 goto nla_put_failure;
1195 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1196 if (!nla)
1197 goto nla_put_failure;
1198 eth_key = nla_data(nla);
1199 memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
1200 memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
1202 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1203 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q)) ||
1204 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci))
1205 goto nla_put_failure;
1206 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1207 if (!swkey->eth.tci)
1208 goto unencap;
1209 } else {
1210 encap = NULL;
1213 if (swkey->eth.type == htons(ETH_P_802_2))
1214 goto unencap;
1216 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type))
1217 goto nla_put_failure;
1219 if (swkey->eth.type == htons(ETH_P_IP)) {
1220 struct ovs_key_ipv4 *ipv4_key;
1222 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1223 if (!nla)
1224 goto nla_put_failure;
1225 ipv4_key = nla_data(nla);
1226 ipv4_key->ipv4_src = swkey->ipv4.addr.src;
1227 ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
1228 ipv4_key->ipv4_proto = swkey->ip.proto;
1229 ipv4_key->ipv4_tos = swkey->ip.tos;
1230 ipv4_key->ipv4_ttl = swkey->ip.ttl;
1231 ipv4_key->ipv4_frag = swkey->ip.frag;
1232 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1233 struct ovs_key_ipv6 *ipv6_key;
1235 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1236 if (!nla)
1237 goto nla_put_failure;
1238 ipv6_key = nla_data(nla);
1239 memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
1240 sizeof(ipv6_key->ipv6_src));
1241 memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
1242 sizeof(ipv6_key->ipv6_dst));
1243 ipv6_key->ipv6_label = swkey->ipv6.label;
1244 ipv6_key->ipv6_proto = swkey->ip.proto;
1245 ipv6_key->ipv6_tclass = swkey->ip.tos;
1246 ipv6_key->ipv6_hlimit = swkey->ip.ttl;
1247 ipv6_key->ipv6_frag = swkey->ip.frag;
1248 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1249 swkey->eth.type == htons(ETH_P_RARP)) {
1250 struct ovs_key_arp *arp_key;
1252 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1253 if (!nla)
1254 goto nla_put_failure;
1255 arp_key = nla_data(nla);
1256 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1257 arp_key->arp_sip = swkey->ipv4.addr.src;
1258 arp_key->arp_tip = swkey->ipv4.addr.dst;
1259 arp_key->arp_op = htons(swkey->ip.proto);
1260 memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
1261 memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
1264 if ((swkey->eth.type == htons(ETH_P_IP) ||
1265 swkey->eth.type == htons(ETH_P_IPV6)) &&
1266 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1268 if (swkey->ip.proto == IPPROTO_TCP) {
1269 struct ovs_key_tcp *tcp_key;
1271 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1272 if (!nla)
1273 goto nla_put_failure;
1274 tcp_key = nla_data(nla);
1275 if (swkey->eth.type == htons(ETH_P_IP)) {
1276 tcp_key->tcp_src = swkey->ipv4.tp.src;
1277 tcp_key->tcp_dst = swkey->ipv4.tp.dst;
1278 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1279 tcp_key->tcp_src = swkey->ipv6.tp.src;
1280 tcp_key->tcp_dst = swkey->ipv6.tp.dst;
1282 } else if (swkey->ip.proto == IPPROTO_UDP) {
1283 struct ovs_key_udp *udp_key;
1285 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1286 if (!nla)
1287 goto nla_put_failure;
1288 udp_key = nla_data(nla);
1289 if (swkey->eth.type == htons(ETH_P_IP)) {
1290 udp_key->udp_src = swkey->ipv4.tp.src;
1291 udp_key->udp_dst = swkey->ipv4.tp.dst;
1292 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1293 udp_key->udp_src = swkey->ipv6.tp.src;
1294 udp_key->udp_dst = swkey->ipv6.tp.dst;
1296 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1297 swkey->ip.proto == IPPROTO_ICMP) {
1298 struct ovs_key_icmp *icmp_key;
1300 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1301 if (!nla)
1302 goto nla_put_failure;
1303 icmp_key = nla_data(nla);
1304 icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
1305 icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
1306 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1307 swkey->ip.proto == IPPROTO_ICMPV6) {
1308 struct ovs_key_icmpv6 *icmpv6_key;
1310 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1311 sizeof(*icmpv6_key));
1312 if (!nla)
1313 goto nla_put_failure;
1314 icmpv6_key = nla_data(nla);
1315 icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
1316 icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
1318 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1319 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1320 struct ovs_key_nd *nd_key;
1322 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1323 if (!nla)
1324 goto nla_put_failure;
1325 nd_key = nla_data(nla);
1326 memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
1327 sizeof(nd_key->nd_target));
1328 memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
1329 memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
1334 unencap:
1335 if (encap)
1336 nla_nest_end(skb, encap);
1338 return 0;
1340 nla_put_failure:
1341 return -EMSGSIZE;
1344 /* Initializes the flow module.
1345 * Returns zero if successful or a negative error code. */
1346 int ovs_flow_init(void)
1348 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1349 0, NULL);
1350 if (flow_cache == NULL)
1351 return -ENOMEM;
1353 return 0;
1356 /* Uninitializes the flow module. */
1357 void ovs_flow_exit(void)
1359 kmem_cache_destroy(flow_cache);