datapath: Support for Linux kernel 3.10
[sliver-openvswitch.git] / datapath / datapath.c
1 /*
2  * Copyright (c) 2007-2013 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_vlan.h>
25 #include <linux/in.h>
26 #include <linux/ip.h>
27 #include <linux/jhash.h>
28 #include <linux/delay.h>
29 #include <linux/time.h>
30 #include <linux/etherdevice.h>
31 #include <linux/genetlink.h>
32 #include <linux/kernel.h>
33 #include <linux/kthread.h>
34 #include <linux/mutex.h>
35 #include <linux/percpu.h>
36 #include <linux/rcupdate.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/version.h>
40 #include <linux/ethtool.h>
41 #include <linux/wait.h>
42 #include <asm/div64.h>
43 #include <linux/highmem.h>
44 #include <linux/netfilter_bridge.h>
45 #include <linux/netfilter_ipv4.h>
46 #include <linux/inetdevice.h>
47 #include <linux/list.h>
48 #include <linux/openvswitch.h>
49 #include <linux/rculist.h>
50 #include <linux/dmi.h>
51 #include <linux/genetlink.h>
52 #include <net/genetlink.h>
53 #include <net/genetlink.h>
54 #include <net/net_namespace.h>
55 #include <net/netns/generic.h>
56
57 #include "checksum.h"
58 #include "datapath.h"
59 #include "flow.h"
60 #include "vlan.h"
61 #include "tunnel.h"
62 #include "vport-internal_dev.h"
63 #include "vport-netdev.h"
64
65 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
66     LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
67 #error Kernels before 2.6.18 or after 3.9 are not supported by this version of Open vSwitch.
68 #endif
69
70 #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
71 static void rehash_flow_table(struct work_struct *work);
72 static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
73
74 int ovs_net_id __read_mostly;
75
76 static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
77                        struct genl_multicast_group *grp)
78 {
79         genl_notify(skb, genl_info_net(info), info->snd_portid,
80                     grp->id, info->nlhdr, GFP_KERNEL);
81 }
82
83 /**
84  * DOC: Locking:
85  *
86  * All writes e.g. Writes to device state (add/remove datapath, port, set
87  * operations on vports, etc.), Writes to other state (flow table
88  * modifications, set miscellaneous datapath parameters, etc.) are protected
89  * by ovs_lock.
90  *
91  * Reads are protected by RCU.
92  *
93  * There are a few special cases (mostly stats) that have their own
94  * synchronization but they nest under all of above and don't interact with
95  * each other.
96  *
97  * The RTNL lock nests inside ovs_mutex.
98  */
99
100 static DEFINE_MUTEX(ovs_mutex);
101
102 void ovs_lock(void)
103 {
104         mutex_lock(&ovs_mutex);
105 }
106
107 void ovs_unlock(void)
108 {
109         mutex_unlock(&ovs_mutex);
110 }
111
112 #ifdef CONFIG_LOCKDEP
113 int lockdep_ovsl_is_held(void)
114 {
115         if (debug_locks)
116                 return lockdep_is_held(&ovs_mutex);
117         else
118                 return 1;
119 }
120 #endif
121
122 static struct vport *new_vport(const struct vport_parms *);
123 static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
124                              const struct dp_upcall_info *);
125 static int queue_userspace_packet(struct net *, int dp_ifindex,
126                                   struct sk_buff *,
127                                   const struct dp_upcall_info *);
128
129 /* Must be called with rcu_read_lock or ovs_mutex. */
130 static struct datapath *get_dp(struct net *net, int dp_ifindex)
131 {
132         struct datapath *dp = NULL;
133         struct net_device *dev;
134
135         rcu_read_lock();
136         dev = dev_get_by_index_rcu(net, dp_ifindex);
137         if (dev) {
138                 struct vport *vport = ovs_internal_dev_get_vport(dev);
139                 if (vport)
140                         dp = vport->dp;
141         }
142         rcu_read_unlock();
143
144         return dp;
145 }
146
147 /* Must be called with rcu_read_lock or ovs_mutex. */
148 const char *ovs_dp_name(const struct datapath *dp)
149 {
150         struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
151         return vport->ops->get_name(vport);
152 }
153
154 static int get_dpifindex(struct datapath *dp)
155 {
156         struct vport *local;
157         int ifindex;
158
159         rcu_read_lock();
160
161         local = ovs_vport_rcu(dp, OVSP_LOCAL);
162         if (local)
163                 ifindex = netdev_vport_priv(local)->dev->ifindex;
164         else
165                 ifindex = 0;
166
167         rcu_read_unlock();
168
169         return ifindex;
170 }
171
172 static void destroy_dp_rcu(struct rcu_head *rcu)
173 {
174         struct datapath *dp = container_of(rcu, struct datapath, rcu);
175
176         ovs_flow_tbl_destroy((__force struct flow_table *)dp->table, false);
177         free_percpu(dp->stats_percpu);
178         release_net(ovs_dp_get_net(dp));
179         kfree(dp->ports);
180         kfree(dp);
181 }
182
183 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
184                                             u16 port_no)
185 {
186         return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
187 }
188
189 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
190 {
191         struct vport *vport;
192         struct hlist_head *head;
193
194         head = vport_hash_bucket(dp, port_no);
195         hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
196                 if (vport->port_no == port_no)
197                         return vport;
198         }
199         return NULL;
200 }
201
202 /* Called with ovs_mutex. */
203 static struct vport *new_vport(const struct vport_parms *parms)
204 {
205         struct vport *vport;
206
207         vport = ovs_vport_add(parms);
208         if (!IS_ERR(vport)) {
209                 struct datapath *dp = parms->dp;
210                 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
211
212                 hlist_add_head_rcu(&vport->dp_hash_node, head);
213         }
214         return vport;
215 }
216
217 void ovs_dp_detach_port(struct vport *p)
218 {
219         ASSERT_OVSL();
220
221         /* First drop references to device. */
222         hlist_del_rcu(&p->dp_hash_node);
223
224         /* Then destroy it. */
225         ovs_vport_del(p);
226 }
227
228 /* Must be called with rcu_read_lock. */
229 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
230 {
231         struct datapath *dp = p->dp;
232         struct sw_flow *flow;
233         struct dp_stats_percpu *stats;
234         struct sw_flow_key key;
235         u64 *stats_counter;
236         int error;
237
238         stats = this_cpu_ptr(dp->stats_percpu);
239
240         /* Extract flow from 'skb' into 'key'. */
241         error = ovs_flow_extract(skb, p->port_no, &key);
242         if (unlikely(error)) {
243                 kfree_skb(skb);
244                 return;
245         }
246
247         /* Look up flow. */
248         flow = ovs_flow_lookup(rcu_dereference(dp->table), &key);
249         if (unlikely(!flow)) {
250                 struct dp_upcall_info upcall;
251
252                 upcall.cmd = OVS_PACKET_CMD_MISS;
253                 upcall.key = &key;
254                 upcall.userdata = NULL;
255                 upcall.portid = p->upcall_portid;
256                 ovs_dp_upcall(dp, skb, &upcall);
257                 consume_skb(skb);
258                 stats_counter = &stats->n_missed;
259                 goto out;
260         }
261
262         OVS_CB(skb)->flow = flow;
263         OVS_CB(skb)->pkt_key = &key;
264
265         stats_counter = &stats->n_hit;
266         ovs_flow_used(OVS_CB(skb)->flow, skb);
267         ovs_execute_actions(dp, skb);
268
269 out:
270         /* Update datapath statistics. */
271         u64_stats_update_begin(&stats->sync);
272         (*stats_counter)++;
273         u64_stats_update_end(&stats->sync);
274 }
275
276 static struct genl_family dp_packet_genl_family = {
277         .id = GENL_ID_GENERATE,
278         .hdrsize = sizeof(struct ovs_header),
279         .name = OVS_PACKET_FAMILY,
280         .version = OVS_PACKET_VERSION,
281         .maxattr = OVS_PACKET_ATTR_MAX,
282          SET_NETNSOK
283 };
284
285 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
286                   const struct dp_upcall_info *upcall_info)
287 {
288         struct dp_stats_percpu *stats;
289         int dp_ifindex;
290         int err;
291
292         if (upcall_info->portid == 0) {
293                 err = -ENOTCONN;
294                 goto err;
295         }
296
297         dp_ifindex = get_dpifindex(dp);
298         if (!dp_ifindex) {
299                 err = -ENODEV;
300                 goto err;
301         }
302
303         forward_ip_summed(skb, true);
304
305         if (!skb_is_gso(skb))
306                 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
307         else
308                 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
309         if (err)
310                 goto err;
311
312         return 0;
313
314 err:
315         stats = this_cpu_ptr(dp->stats_percpu);
316
317         u64_stats_update_begin(&stats->sync);
318         stats->n_lost++;
319         u64_stats_update_end(&stats->sync);
320
321         return err;
322 }
323
324 static int queue_gso_packets(struct net *net, int dp_ifindex,
325                              struct sk_buff *skb,
326                              const struct dp_upcall_info *upcall_info)
327 {
328         unsigned short gso_type = skb_shinfo(skb)->gso_type;
329         struct dp_upcall_info later_info;
330         struct sw_flow_key later_key;
331         struct sk_buff *segs, *nskb;
332         int err;
333
334         segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
335         if (IS_ERR(segs))
336                 return PTR_ERR(segs);
337
338         /* Queue all of the segments. */
339         skb = segs;
340         do {
341                 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
342                 if (err)
343                         break;
344
345                 if (skb == segs && gso_type & SKB_GSO_UDP) {
346                         /* The initial flow key extracted by ovs_flow_extract()
347                          * in this case is for a first fragment, so we need to
348                          * properly mark later fragments.
349                          */
350                         later_key = *upcall_info->key;
351                         later_key.ip.frag = OVS_FRAG_TYPE_LATER;
352
353                         later_info = *upcall_info;
354                         later_info.key = &later_key;
355                         upcall_info = &later_info;
356                 }
357         } while ((skb = skb->next));
358
359         /* Free all of the segments. */
360         skb = segs;
361         do {
362                 nskb = skb->next;
363                 if (err)
364                         kfree_skb(skb);
365                 else
366                         consume_skb(skb);
367         } while ((skb = nskb));
368         return err;
369 }
370
371 static size_t key_attr_size(void)
372 {
373         return    nla_total_size(4)   /* OVS_KEY_ATTR_PRIORITY */
374                 + nla_total_size(0)   /* OVS_KEY_ATTR_TUNNEL */
375                   + nla_total_size(8)   /* OVS_TUNNEL_KEY_ATTR_ID */
376                   + nla_total_size(4)   /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
377                   + nla_total_size(4)   /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
378                   + nla_total_size(1)   /* OVS_TUNNEL_KEY_ATTR_TOS */
379                   + nla_total_size(1)   /* OVS_TUNNEL_KEY_ATTR_TTL */
380                   + nla_total_size(0)   /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
381                   + nla_total_size(0)   /* OVS_TUNNEL_KEY_ATTR_CSUM */
382                 + nla_total_size(4)   /* OVS_KEY_ATTR_IN_PORT */
383                 + nla_total_size(4)   /* OVS_KEY_ATTR_SKB_MARK */
384                 + nla_total_size(12)  /* OVS_KEY_ATTR_ETHERNET */
385                 + nla_total_size(2)   /* OVS_KEY_ATTR_ETHERTYPE */
386                 + nla_total_size(4)   /* OVS_KEY_ATTR_8021Q */
387                 + nla_total_size(0)   /* OVS_KEY_ATTR_ENCAP */
388                 + nla_total_size(2)   /* OVS_KEY_ATTR_ETHERTYPE */
389                 + nla_total_size(40)  /* OVS_KEY_ATTR_IPV6 */
390                 + nla_total_size(2)   /* OVS_KEY_ATTR_ICMPV6 */
391                 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
392 }
393
394 static size_t upcall_msg_size(const struct sk_buff *skb,
395                               const struct nlattr *userdata)
396 {
397         size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
398                 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
399                 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
400
401         /* OVS_PACKET_ATTR_USERDATA */
402         if (userdata)
403                 size += NLA_ALIGN(userdata->nla_len);
404
405         return size;
406 }
407
408 static int queue_userspace_packet(struct net *net, int dp_ifindex,
409                                   struct sk_buff *skb,
410                                   const struct dp_upcall_info *upcall_info)
411 {
412         struct ovs_header *upcall;
413         struct sk_buff *nskb = NULL;
414         struct sk_buff *user_skb; /* to be queued to userspace */
415         struct nlattr *nla;
416         int err;
417
418         if (vlan_tx_tag_present(skb)) {
419                 nskb = skb_clone(skb, GFP_ATOMIC);
420                 if (!nskb)
421                         return -ENOMEM;
422                 
423                 err = vlan_deaccel_tag(nskb);
424                 if (err)
425                         return err;
426
427                 skb = nskb;
428         }
429
430         if (nla_attr_size(skb->len) > USHRT_MAX) {
431                 err = -EFBIG;
432                 goto out;
433         }
434
435         user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
436         if (!user_skb) {
437                 err = -ENOMEM;
438                 goto out;
439         }
440
441         upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
442                              0, upcall_info->cmd);
443         upcall->dp_ifindex = dp_ifindex;
444
445         nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
446         ovs_flow_to_nlattrs(upcall_info->key, upcall_info->key, user_skb);
447         nla_nest_end(user_skb, nla);
448
449         if (upcall_info->userdata)
450                 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
451                           nla_len(upcall_info->userdata),
452                           nla_data(upcall_info->userdata));
453
454         nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
455
456         skb_copy_and_csum_dev(skb, nla_data(nla));
457
458         genlmsg_end(user_skb, upcall);
459         err = genlmsg_unicast(net, user_skb, upcall_info->portid);
460
461 out:
462         kfree_skb(nskb);
463         return err;
464 }
465
466 /* Called with ovs_mutex. */
467 static int flush_flows(struct datapath *dp)
468 {
469         struct flow_table *old_table;
470         struct flow_table *new_table;
471
472         old_table = ovsl_dereference(dp->table);
473         new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
474         if (!new_table)
475                 return -ENOMEM;
476
477         rcu_assign_pointer(dp->table, new_table);
478
479         ovs_flow_tbl_destroy(old_table, true);
480         return 0;
481 }
482
483 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, int attr_len)
484 {
485
486         struct sw_flow_actions *acts;
487         int new_acts_size;
488         int req_size = NLA_ALIGN(attr_len);
489         int next_offset = offsetof(struct sw_flow_actions, actions) +
490                                         (*sfa)->actions_len;
491
492         if (req_size <= (ksize(*sfa) - next_offset))
493                 goto out;
494
495         new_acts_size = ksize(*sfa) * 2;
496
497         if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
498                 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
499                         return ERR_PTR(-EMSGSIZE);
500                 new_acts_size = MAX_ACTIONS_BUFSIZE;
501         }
502
503         acts = ovs_flow_actions_alloc(new_acts_size);
504         if (IS_ERR(acts))
505                 return (void *)acts;
506
507         memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
508         acts->actions_len = (*sfa)->actions_len;
509         kfree(*sfa);
510         *sfa = acts;
511
512 out:
513         (*sfa)->actions_len += req_size;
514         return  (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
515 }
516
517 static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
518 {
519         struct nlattr *a;
520
521         a = reserve_sfa_size(sfa, nla_attr_size(len));
522         if (IS_ERR(a))
523                 return PTR_ERR(a);
524
525         a->nla_type = attrtype;
526         a->nla_len = nla_attr_size(len);
527
528         if (data)
529                 memcpy(nla_data(a), data, len);
530         memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
531
532         return 0;
533 }
534
535 static inline int add_nested_action_start(struct sw_flow_actions **sfa, int attrtype)
536 {
537         int used = (*sfa)->actions_len;
538         int err;
539
540         err = add_action(sfa, attrtype, NULL, 0);
541         if (err)
542                 return err;
543
544         return used;
545 }
546
547 static inline void add_nested_action_end(struct sw_flow_actions *sfa, int st_offset)
548 {
549         struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + st_offset);
550
551         a->nla_len = sfa->actions_len - st_offset;
552 }
553
554 static int validate_and_copy_actions(const struct nlattr *attr,
555                                 const struct sw_flow_key *key, int depth,
556                                 struct sw_flow_actions **sfa);
557
558 static int validate_and_copy_sample(const struct nlattr *attr,
559                            const struct sw_flow_key *key, int depth,
560                            struct sw_flow_actions **sfa)
561 {
562         const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
563         const struct nlattr *probability, *actions;
564         const struct nlattr *a;
565         int rem, start, err, st_acts;
566
567         memset(attrs, 0, sizeof(attrs));
568         nla_for_each_nested(a, attr, rem) {
569                 int type = nla_type(a);
570                 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
571                         return -EINVAL;
572                 attrs[type] = a;
573         }
574         if (rem)
575                 return -EINVAL;
576
577         probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
578         if (!probability || nla_len(probability) != sizeof(u32))
579                 return -EINVAL;
580
581         actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
582         if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
583                 return -EINVAL;
584
585         /* validation done, copy sample action. */
586         start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
587         if (start < 0)
588                 return start;
589         err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, nla_data(probability), sizeof(u32));
590         if (err)
591                 return err;
592         st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
593         if (st_acts < 0)
594                 return st_acts;
595
596         err = validate_and_copy_actions(actions, key, depth + 1, sfa);
597         if (err)
598                 return err;
599
600         add_nested_action_end(*sfa, st_acts);
601         add_nested_action_end(*sfa, start);
602
603         return 0;
604 }
605
606 static int validate_tp_port(const struct sw_flow_key *flow_key)
607 {
608         if (flow_key->eth.type == htons(ETH_P_IP)) {
609                 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
610                         return 0;
611         } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
612                 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
613                         return 0;
614         }
615
616         return -EINVAL;
617 }
618
619 static int validate_and_copy_set_tun(const struct nlattr *attr,
620                                      struct sw_flow_actions **sfa)
621 {
622         struct sw_flow_match match;
623         struct sw_flow_key key;
624         int err, start;
625
626         ovs_match_init(&match, &key, NULL);
627         err = ovs_ipv4_tun_from_nlattr(nla_data(attr), &match, false);
628         if (err)
629                 return err;
630
631         start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
632         if (start < 0)
633                 return start;
634
635         err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
636                         sizeof(match.key->tun_key));
637         add_nested_action_end(*sfa, start);
638
639         return err;
640 }
641
642 static int validate_set(const struct nlattr *a,
643                         const struct sw_flow_key *flow_key,
644                         struct sw_flow_actions **sfa,
645                         bool *set_tun)
646 {
647         const struct nlattr *ovs_key = nla_data(a);
648         int key_type = nla_type(ovs_key);
649
650         /* There can be only one key in a action */
651         if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
652                 return -EINVAL;
653
654         if (key_type > OVS_KEY_ATTR_MAX ||
655             (ovs_key_lens[key_type] != nla_len(ovs_key) &&
656              ovs_key_lens[key_type] != -1))
657                 return -EINVAL;
658
659         switch (key_type) {
660         const struct ovs_key_ipv4 *ipv4_key;
661         const struct ovs_key_ipv6 *ipv6_key;
662         int err;
663
664         case OVS_KEY_ATTR_PRIORITY:
665         case OVS_KEY_ATTR_ETHERNET:
666                 break;
667
668         case OVS_KEY_ATTR_SKB_MARK:
669 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
670                 if (nla_get_u32(ovs_key) != 0)
671                         return -EINVAL;
672 #endif
673                 break;
674
675         case OVS_KEY_ATTR_TUNNEL:
676                 *set_tun = true;
677                 err = validate_and_copy_set_tun(a, sfa);
678                 if (err)
679                         return err;
680                 break;
681
682         case OVS_KEY_ATTR_IPV4:
683                 if (flow_key->eth.type != htons(ETH_P_IP))
684                         return -EINVAL;
685
686                 if (!flow_key->ip.proto)
687                         return -EINVAL;
688
689                 ipv4_key = nla_data(ovs_key);
690                 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
691                         return -EINVAL;
692
693                 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
694                         return -EINVAL;
695
696                 break;
697
698         case OVS_KEY_ATTR_IPV6:
699                 if (flow_key->eth.type != htons(ETH_P_IPV6))
700                         return -EINVAL;
701
702                 if (!flow_key->ip.proto)
703                         return -EINVAL;
704
705                 ipv6_key = nla_data(ovs_key);
706                 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
707                         return -EINVAL;
708
709                 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
710                         return -EINVAL;
711
712                 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
713                         return -EINVAL;
714
715                 break;
716
717         case OVS_KEY_ATTR_TCP:
718                 if (flow_key->ip.proto != IPPROTO_TCP)
719                         return -EINVAL;
720
721                 return validate_tp_port(flow_key);
722
723         case OVS_KEY_ATTR_UDP:
724                 if (flow_key->ip.proto != IPPROTO_UDP)
725                         return -EINVAL;
726
727                 return validate_tp_port(flow_key);
728
729         default:
730                 return -EINVAL;
731         }
732
733         return 0;
734 }
735
736 static int validate_userspace(const struct nlattr *attr)
737 {
738         static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] =   {
739                 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
740                 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
741         };
742         struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
743         int error;
744
745         error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
746                                  attr, userspace_policy);
747         if (error)
748                 return error;
749
750         if (!a[OVS_USERSPACE_ATTR_PID] ||
751             !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
752                 return -EINVAL;
753
754         return 0;
755 }
756
757 static int copy_action(const struct nlattr *from,
758                       struct sw_flow_actions **sfa)
759 {
760         int totlen = NLA_ALIGN(from->nla_len);
761         struct nlattr *to;
762
763         to = reserve_sfa_size(sfa, from->nla_len);
764         if (IS_ERR(to))
765                 return PTR_ERR(to);
766
767         memcpy(to, from, totlen);
768         return 0;
769 }
770
771 static int validate_and_copy_actions(const struct nlattr *attr,
772                                 const struct sw_flow_key *key,
773                                 int depth,
774                                 struct sw_flow_actions **sfa)
775 {
776         const struct nlattr *a;
777         int rem, err;
778
779         if (depth >= SAMPLE_ACTION_DEPTH)
780                 return -EOVERFLOW;
781
782         nla_for_each_nested(a, attr, rem) {
783                 /* Expected argument lengths, (u32)-1 for variable length. */
784                 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
785                         [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
786                         [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
787                         [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
788                         [OVS_ACTION_ATTR_POP_VLAN] = 0,
789                         [OVS_ACTION_ATTR_SET] = (u32)-1,
790                         [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
791                 };
792                 const struct ovs_action_push_vlan *vlan;
793                 int type = nla_type(a);
794                 bool skip_copy;
795
796                 if (type > OVS_ACTION_ATTR_MAX ||
797                     (action_lens[type] != nla_len(a) &&
798                      action_lens[type] != (u32)-1))
799                         return -EINVAL;
800
801                 skip_copy = false;
802                 switch (type) {
803                 case OVS_ACTION_ATTR_UNSPEC:
804                         return -EINVAL;
805
806                 case OVS_ACTION_ATTR_USERSPACE:
807                         err = validate_userspace(a);
808                         if (err)
809                                 return err;
810                         break;
811
812                 case OVS_ACTION_ATTR_OUTPUT:
813                         if (nla_get_u32(a) >= DP_MAX_PORTS)
814                                 return -EINVAL;
815                         break;
816
817
818                 case OVS_ACTION_ATTR_POP_VLAN:
819                         break;
820
821                 case OVS_ACTION_ATTR_PUSH_VLAN:
822                         vlan = nla_data(a);
823                         if (vlan->vlan_tpid != htons(ETH_P_8021Q))
824                                 return -EINVAL;
825                         if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
826                                 return -EINVAL;
827                         break;
828
829                 case OVS_ACTION_ATTR_SET:
830                         err = validate_set(a, key, sfa, &skip_copy);
831                         if (err)
832                                 return err;
833                         break;
834
835                 case OVS_ACTION_ATTR_SAMPLE:
836                         err = validate_and_copy_sample(a, key, depth, sfa);
837                         if (err)
838                                 return err;
839                         skip_copy = true;
840                         break;
841
842                 default:
843                         return -EINVAL;
844                 }
845                 if (!skip_copy) {
846                         err = copy_action(a, sfa);
847                         if (err)
848                                 return err;
849                 }
850         }
851
852         if (rem > 0)
853                 return -EINVAL;
854
855         return 0;
856 }
857
858 static void clear_stats(struct sw_flow *flow)
859 {
860         flow->used = 0;
861         flow->tcp_flags = 0;
862         flow->packet_count = 0;
863         flow->byte_count = 0;
864 }
865
866 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
867 {
868         struct ovs_header *ovs_header = info->userhdr;
869         struct nlattr **a = info->attrs;
870         struct sw_flow_actions *acts;
871         struct sk_buff *packet;
872         struct sw_flow *flow;
873         struct datapath *dp;
874         struct ethhdr *eth;
875         int len;
876         int err;
877
878         err = -EINVAL;
879         if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
880             !a[OVS_PACKET_ATTR_ACTIONS])
881                 goto err;
882
883         len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
884         packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
885         err = -ENOMEM;
886         if (!packet)
887                 goto err;
888         skb_reserve(packet, NET_IP_ALIGN);
889
890         nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
891
892         skb_reset_mac_header(packet);
893         eth = eth_hdr(packet);
894
895         /* Normally, setting the skb 'protocol' field would be handled by a
896          * call to eth_type_trans(), but it assumes there's a sending
897          * device, which we may not have. */
898         if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
899                 packet->protocol = eth->h_proto;
900         else
901                 packet->protocol = htons(ETH_P_802_2);
902
903         /* Build an sw_flow for sending this packet. */
904         flow = ovs_flow_alloc();
905         err = PTR_ERR(flow);
906         if (IS_ERR(flow))
907                 goto err_kfree_skb;
908
909         err = ovs_flow_extract(packet, -1, &flow->key);
910         if (err)
911                 goto err_flow_free;
912
913         err = ovs_flow_metadata_from_nlattrs(flow, a[OVS_PACKET_ATTR_KEY]);
914         if (err)
915                 goto err_flow_free;
916         acts = ovs_flow_actions_alloc(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
917         err = PTR_ERR(acts);
918         if (IS_ERR(acts))
919                 goto err_flow_free;
920
921         err = validate_and_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0, &acts);
922         rcu_assign_pointer(flow->sf_acts, acts);
923         if (err)
924                 goto err_flow_free;
925
926         OVS_CB(packet)->flow = flow;
927         OVS_CB(packet)->pkt_key = &flow->key;
928         packet->priority = flow->key.phy.priority;
929         skb_set_mark(packet, flow->key.phy.skb_mark);
930
931         rcu_read_lock();
932         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
933         err = -ENODEV;
934         if (!dp)
935                 goto err_unlock;
936
937         local_bh_disable();
938         err = ovs_execute_actions(dp, packet);
939         local_bh_enable();
940         rcu_read_unlock();
941
942         ovs_flow_free(flow, false);
943         return err;
944
945 err_unlock:
946         rcu_read_unlock();
947 err_flow_free:
948         ovs_flow_free(flow, false);
949 err_kfree_skb:
950         kfree_skb(packet);
951 err:
952         return err;
953 }
954
955 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
956 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
957         [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
958 #else
959         [OVS_PACKET_ATTR_PACKET] = { .minlen = ETH_HLEN },
960 #endif
961         [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
962         [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
963 };
964
965 static struct genl_ops dp_packet_genl_ops[] = {
966         { .cmd = OVS_PACKET_CMD_EXECUTE,
967           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
968           .policy = packet_policy,
969           .doit = ovs_packet_cmd_execute
970         }
971 };
972
973 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
974 {
975         struct flow_table *table;
976         int i;
977
978         table = rcu_dereference_check(dp->table, lockdep_ovsl_is_held());
979         stats->n_flows = ovs_flow_tbl_count(table);
980
981         stats->n_hit = stats->n_missed = stats->n_lost = 0;
982         for_each_possible_cpu(i) {
983                 const struct dp_stats_percpu *percpu_stats;
984                 struct dp_stats_percpu local_stats;
985                 unsigned int start;
986
987                 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
988
989                 do {
990                         start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
991                         local_stats = *percpu_stats;
992                 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
993
994                 stats->n_hit += local_stats.n_hit;
995                 stats->n_missed += local_stats.n_missed;
996                 stats->n_lost += local_stats.n_lost;
997         }
998 }
999
1000 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1001         [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1002         [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1003         [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1004 };
1005
1006 static struct genl_family dp_flow_genl_family = {
1007         .id = GENL_ID_GENERATE,
1008         .hdrsize = sizeof(struct ovs_header),
1009         .name = OVS_FLOW_FAMILY,
1010         .version = OVS_FLOW_VERSION,
1011         .maxattr = OVS_FLOW_ATTR_MAX,
1012          SET_NETNSOK
1013 };
1014
1015 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
1016         .name = OVS_FLOW_MCGROUP
1017 };
1018
1019 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
1020 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1021 {
1022         const struct nlattr *a;
1023         struct nlattr *start;
1024         int err = 0, rem;
1025
1026         start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1027         if (!start)
1028                 return -EMSGSIZE;
1029
1030         nla_for_each_nested(a, attr, rem) {
1031                 int type = nla_type(a);
1032                 struct nlattr *st_sample;
1033
1034                 switch (type) {
1035                 case OVS_SAMPLE_ATTR_PROBABILITY:
1036                         if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
1037                                 return -EMSGSIZE;
1038                         break;
1039                 case OVS_SAMPLE_ATTR_ACTIONS:
1040                         st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1041                         if (!st_sample)
1042                                 return -EMSGSIZE;
1043                         err = actions_to_attr(nla_data(a), nla_len(a), skb);
1044                         if (err)
1045                                 return err;
1046                         nla_nest_end(skb, st_sample);
1047                         break;
1048                 }
1049         }
1050
1051         nla_nest_end(skb, start);
1052         return err;
1053 }
1054
1055 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1056 {
1057         const struct nlattr *ovs_key = nla_data(a);
1058         int key_type = nla_type(ovs_key);
1059         struct nlattr *start;
1060         int err;
1061
1062         switch (key_type) {
1063         case OVS_KEY_ATTR_IPV4_TUNNEL:
1064                 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1065                 if (!start)
1066                         return -EMSGSIZE;
1067
1068                 err = ovs_ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
1069                                              nla_data(ovs_key));
1070                 if (err)
1071                         return err;
1072                 nla_nest_end(skb, start);
1073                 break;
1074         default:
1075                 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1076                         return -EMSGSIZE;
1077                 break;
1078         }
1079
1080         return 0;
1081 }
1082
1083 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
1084 {
1085         const struct nlattr *a;
1086         int rem, err;
1087
1088         nla_for_each_attr(a, attr, len, rem) {
1089                 int type = nla_type(a);
1090
1091                 switch (type) {
1092                 case OVS_ACTION_ATTR_SET:
1093                         err = set_action_to_attr(a, skb);
1094                         if (err)
1095                                 return err;
1096                         break;
1097
1098                 case OVS_ACTION_ATTR_SAMPLE:
1099                         err = sample_action_to_attr(a, skb);
1100                         if (err)
1101                                 return err;
1102                         break;
1103                 default:
1104                         if (nla_put(skb, type, nla_len(a), nla_data(a)))
1105                                 return -EMSGSIZE;
1106                         break;
1107                 }
1108         }
1109
1110         return 0;
1111 }
1112
1113 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
1114 {
1115         return NLMSG_ALIGN(sizeof(struct ovs_header))
1116                 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
1117                 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
1118                 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
1119                 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
1120                 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
1121                 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
1122 }
1123
1124 /* Called with ovs_mutex. */
1125 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
1126                                   struct sk_buff *skb, u32 portid,
1127                                   u32 seq, u32 flags, u8 cmd)
1128 {
1129         const int skb_orig_len = skb->len;
1130         struct nlattr *start;
1131         struct ovs_flow_stats stats;
1132         struct ovs_header *ovs_header;
1133         struct nlattr *nla;
1134         unsigned long used;
1135         u8 tcp_flags;
1136         int err;
1137
1138         ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
1139         if (!ovs_header)
1140                 return -EMSGSIZE;
1141
1142         ovs_header->dp_ifindex = get_dpifindex(dp);
1143
1144         /* Fill flow key. */
1145         nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
1146         if (!nla)
1147                 goto nla_put_failure;
1148
1149         err = ovs_flow_to_nlattrs(&flow->unmasked_key,
1150                         &flow->unmasked_key, skb);
1151         if (err)
1152                 goto error;
1153         nla_nest_end(skb, nla);
1154
1155         nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
1156         if (!nla)
1157                 goto nla_put_failure;
1158
1159         err = ovs_flow_to_nlattrs(&flow->key, &flow->mask->key, skb);
1160         if (err)
1161                 goto error;
1162
1163         nla_nest_end(skb, nla);
1164
1165         spin_lock_bh(&flow->lock);
1166         used = flow->used;
1167         stats.n_packets = flow->packet_count;
1168         stats.n_bytes = flow->byte_count;
1169         tcp_flags = flow->tcp_flags;
1170         spin_unlock_bh(&flow->lock);
1171
1172         if (used &&
1173             nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
1174                 goto nla_put_failure;
1175
1176         if (stats.n_packets &&
1177             nla_put(skb, OVS_FLOW_ATTR_STATS,
1178                     sizeof(struct ovs_flow_stats), &stats))
1179                 goto nla_put_failure;
1180
1181         if (tcp_flags &&
1182             nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
1183                 goto nla_put_failure;
1184
1185         /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
1186          * this is the first flow to be dumped into 'skb'.  This is unusual for
1187          * Netlink but individual action lists can be longer than
1188          * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
1189          * The userspace caller can always fetch the actions separately if it
1190          * really wants them.  (Most userspace callers in fact don't care.)
1191          *
1192          * This can only fail for dump operations because the skb is always
1193          * properly sized for single flows.
1194          */
1195         start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
1196         if (start) {
1197                 const struct sw_flow_actions *sf_acts;
1198
1199                 sf_acts = rcu_dereference_check(flow->sf_acts,
1200                                                 lockdep_ovsl_is_held());
1201
1202                 err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
1203                 if (!err)
1204                         nla_nest_end(skb, start);
1205                 else {
1206                         if (skb_orig_len)
1207                                 goto error;
1208
1209                         nla_nest_cancel(skb, start);
1210                 }
1211         } else if (skb_orig_len)
1212                 goto nla_put_failure;
1213
1214         return genlmsg_end(skb, ovs_header);
1215
1216 nla_put_failure:
1217         err = -EMSGSIZE;
1218 error:
1219         genlmsg_cancel(skb, ovs_header);
1220         return err;
1221 }
1222
1223 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
1224 {
1225         const struct sw_flow_actions *sf_acts;
1226
1227         sf_acts = ovsl_dereference(flow->sf_acts);
1228
1229         return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
1230 }
1231
1232 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
1233                                                struct datapath *dp,
1234                                                u32 portid, u32 seq, u8 cmd)
1235 {
1236         struct sk_buff *skb;
1237         int retval;
1238
1239         skb = ovs_flow_cmd_alloc_info(flow);
1240         if (!skb)
1241                 return ERR_PTR(-ENOMEM);
1242
1243         retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
1244         BUG_ON(retval < 0);
1245         return skb;
1246 }
1247
1248 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1249 {
1250         struct nlattr **a = info->attrs;
1251         struct ovs_header *ovs_header = info->userhdr;
1252         struct sw_flow_key key, masked_key;
1253         struct sw_flow *flow = NULL;
1254         struct sw_flow_mask mask;
1255         struct sk_buff *reply;
1256         struct datapath *dp;
1257         struct flow_table *table;
1258         struct sw_flow_actions *acts = NULL;
1259         struct sw_flow_match match;
1260         int error;
1261
1262         /* Extract key. */
1263         error = -EINVAL;
1264         if (!a[OVS_FLOW_ATTR_KEY])
1265                 goto error;
1266
1267         ovs_match_init(&match, &key, &mask);
1268         error = ovs_match_from_nlattrs(&match,
1269                         a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
1270         if (error)
1271                 goto error;
1272
1273         /* Validate actions. */
1274         if (a[OVS_FLOW_ATTR_ACTIONS]) {
1275                 acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
1276                 error = PTR_ERR(acts);
1277                 if (IS_ERR(acts))
1278                         goto error;
1279
1280                 ovs_flow_key_mask(&masked_key, &key, &mask);
1281                 error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
1282                                                   &masked_key, 0, &acts);
1283                 if (error) {
1284                         OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
1285                         goto err_kfree;
1286                 }
1287         } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1288                 error = -EINVAL;
1289                 goto error;
1290         }
1291
1292         ovs_lock();
1293         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1294         error = -ENODEV;
1295         if (!dp)
1296                 goto err_unlock_ovs;
1297
1298         table = ovsl_dereference(dp->table);
1299
1300         /* Check if this is a duplicate flow */
1301         flow = ovs_flow_lookup(table, &key);
1302         if (!flow) {
1303                 struct sw_flow_mask *mask_p;
1304                 /* Bail out if we're not allowed to create a new flow. */
1305                 error = -ENOENT;
1306                 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1307                         goto err_unlock_ovs;
1308
1309                 /* Expand table, if necessary, to make room. */
1310                 if (ovs_flow_tbl_need_to_expand(table)) {
1311                         struct flow_table *new_table;
1312
1313                         new_table = ovs_flow_tbl_expand(table);
1314                         if (!IS_ERR(new_table)) {
1315                                 rcu_assign_pointer(dp->table, new_table);
1316                                 ovs_flow_tbl_destroy(table, true);
1317                                 table = ovsl_dereference(dp->table);
1318                         }
1319                 }
1320
1321                 /* Allocate flow. */
1322                 flow = ovs_flow_alloc();
1323                 if (IS_ERR(flow)) {
1324                         error = PTR_ERR(flow);
1325                         goto err_unlock_ovs;
1326                 }
1327                 clear_stats(flow);
1328
1329                 flow->key = masked_key;
1330                 flow->unmasked_key = key;
1331
1332                 /* Make sure mask is unique in the system */
1333                 mask_p = ovs_sw_flow_mask_find(table, &mask);
1334                 if (!mask_p) {
1335                         /* Allocate a new mask if none exsits. */
1336                         mask_p = ovs_sw_flow_mask_alloc();
1337                         if (!mask_p)
1338                                 goto err_flow_free;
1339                         mask_p->key = mask.key;
1340                         mask_p->range = mask.range;
1341                         ovs_sw_flow_mask_insert(table, mask_p);
1342                 }
1343
1344                 ovs_sw_flow_mask_add_ref(mask_p);
1345                 flow->mask = mask_p;
1346                 rcu_assign_pointer(flow->sf_acts, acts);
1347
1348                 /* Put flow in bucket. */
1349                 ovs_flow_insert(table, flow);
1350
1351                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1352                                                 info->snd_seq, OVS_FLOW_CMD_NEW);
1353         } else {
1354                 /* We found a matching flow. */
1355                 struct sw_flow_actions *old_acts;
1356
1357                 /* Bail out if we're not allowed to modify an existing flow.
1358                  * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1359                  * because Generic Netlink treats the latter as a dump
1360                  * request.  We also accept NLM_F_EXCL in case that bug ever
1361                  * gets fixed.
1362                  */
1363                 error = -EEXIST;
1364                 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1365                     info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1366                         goto err_unlock_ovs;
1367
1368                 /* The unmasked key has to be the same for flow updates. */
1369                 error = -EINVAL;
1370                 if (!ovs_flow_cmp_unmasked_key(flow, &key, match.range.end)) {
1371                         OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
1372                         goto err_unlock_ovs;
1373                 }
1374
1375                 /* Update actions. */
1376                 old_acts = ovsl_dereference(flow->sf_acts);
1377                 rcu_assign_pointer(flow->sf_acts, acts);
1378                 ovs_flow_deferred_free_acts(old_acts);
1379
1380                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1381                                                info->snd_seq, OVS_FLOW_CMD_NEW);
1382
1383                 /* Clear stats. */
1384                 if (a[OVS_FLOW_ATTR_CLEAR]) {
1385                         spin_lock_bh(&flow->lock);
1386                         clear_stats(flow);
1387                         spin_unlock_bh(&flow->lock);
1388                 }
1389         }
1390         ovs_unlock();
1391
1392         if (!IS_ERR(reply))
1393                 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1394         else
1395                 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1396                                 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1397         return 0;
1398
1399 err_flow_free:
1400         ovs_flow_free(flow, false);
1401 err_unlock_ovs:
1402         ovs_unlock();
1403 err_kfree:
1404         kfree(acts);
1405 error:
1406         return error;
1407 }
1408
1409 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1410 {
1411         struct nlattr **a = info->attrs;
1412         struct ovs_header *ovs_header = info->userhdr;
1413         struct sw_flow_key key;
1414         struct sk_buff *reply;
1415         struct sw_flow *flow;
1416         struct datapath *dp;
1417         struct flow_table *table;
1418         struct sw_flow_match match;
1419         int err;
1420
1421         if (!a[OVS_FLOW_ATTR_KEY]) {
1422                 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1423                 return -EINVAL;
1424         }
1425
1426         ovs_match_init(&match, &key, NULL);
1427         err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1428         if (err)
1429                 return err;
1430
1431         ovs_lock();
1432         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1433         if (!dp) {
1434                 err = -ENODEV;
1435                 goto unlock;
1436         }
1437
1438         table = ovsl_dereference(dp->table);
1439         flow = ovs_flow_lookup_unmasked_key(table, &match);
1440         if (!flow) {
1441                 err = -ENOENT;
1442                 goto unlock;
1443         }
1444
1445         reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1446                                         info->snd_seq, OVS_FLOW_CMD_NEW);
1447         if (IS_ERR(reply)) {
1448                 err = PTR_ERR(reply);
1449                 goto unlock;
1450         }
1451
1452         ovs_unlock();
1453         return genlmsg_reply(reply, info);
1454 unlock:
1455         ovs_unlock();
1456         return err;
1457 }
1458
1459 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1460 {
1461         struct nlattr **a = info->attrs;
1462         struct ovs_header *ovs_header = info->userhdr;
1463         struct sw_flow_key key;
1464         struct sk_buff *reply;
1465         struct sw_flow *flow;
1466         struct datapath *dp;
1467         struct flow_table *table;
1468         struct sw_flow_match match;
1469         int err;
1470
1471         ovs_lock();
1472         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1473         if (!dp) {
1474                 err = -ENODEV;
1475                 goto unlock;
1476         }
1477
1478         if (!a[OVS_FLOW_ATTR_KEY]) {
1479                 err = flush_flows(dp);
1480                 goto unlock;
1481         }
1482
1483         ovs_match_init(&match, &key, NULL);
1484         err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1485         if (err)
1486                 goto unlock;
1487
1488         table = ovsl_dereference(dp->table);
1489         flow = ovs_flow_lookup_unmasked_key(table, &match);
1490         if (!flow) {
1491                 err = -ENOENT;
1492                 goto unlock;
1493         }
1494
1495         reply = ovs_flow_cmd_alloc_info(flow);
1496         if (!reply) {
1497                 err = -ENOMEM;
1498                 goto unlock;
1499         }
1500
1501         ovs_flow_remove(table, flow);
1502
1503         err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
1504                                      info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1505         BUG_ON(err < 0);
1506
1507         ovs_flow_free(flow, true);
1508         ovs_unlock();
1509
1510         ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1511         return 0;
1512 unlock:
1513         ovs_unlock();
1514         return err;
1515 }
1516
1517 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1518 {
1519         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1520         struct datapath *dp;
1521         struct flow_table *table;
1522
1523         rcu_read_lock();
1524         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1525         if (!dp) {
1526                 rcu_read_unlock();
1527                 return -ENODEV;
1528         }
1529
1530         table = rcu_dereference(dp->table);
1531         for (;;) {
1532                 struct sw_flow *flow;
1533                 u32 bucket, obj;
1534
1535                 bucket = cb->args[0];
1536                 obj = cb->args[1];
1537                 flow = ovs_flow_dump_next(table, &bucket, &obj);
1538                 if (!flow)
1539                         break;
1540
1541                 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1542                                            NETLINK_CB(cb->skb).portid,
1543                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
1544                                            OVS_FLOW_CMD_NEW) < 0)
1545                         break;
1546
1547                 cb->args[0] = bucket;
1548                 cb->args[1] = obj;
1549         }
1550         rcu_read_unlock();
1551         return skb->len;
1552 }
1553
1554 static struct genl_ops dp_flow_genl_ops[] = {
1555         { .cmd = OVS_FLOW_CMD_NEW,
1556           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1557           .policy = flow_policy,
1558           .doit = ovs_flow_cmd_new_or_set
1559         },
1560         { .cmd = OVS_FLOW_CMD_DEL,
1561           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1562           .policy = flow_policy,
1563           .doit = ovs_flow_cmd_del
1564         },
1565         { .cmd = OVS_FLOW_CMD_GET,
1566           .flags = 0,               /* OK for unprivileged users. */
1567           .policy = flow_policy,
1568           .doit = ovs_flow_cmd_get,
1569           .dumpit = ovs_flow_cmd_dump
1570         },
1571         { .cmd = OVS_FLOW_CMD_SET,
1572           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1573           .policy = flow_policy,
1574           .doit = ovs_flow_cmd_new_or_set,
1575         },
1576 };
1577
1578 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1579 #ifdef HAVE_NLA_NUL_STRING
1580         [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1581 #endif
1582         [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1583 };
1584
1585 static struct genl_family dp_datapath_genl_family = {
1586         .id = GENL_ID_GENERATE,
1587         .hdrsize = sizeof(struct ovs_header),
1588         .name = OVS_DATAPATH_FAMILY,
1589         .version = OVS_DATAPATH_VERSION,
1590         .maxattr = OVS_DP_ATTR_MAX,
1591          SET_NETNSOK
1592 };
1593
1594 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1595         .name = OVS_DATAPATH_MCGROUP
1596 };
1597
1598 static size_t ovs_dp_cmd_msg_size(void)
1599 {
1600         size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1601
1602         msgsize += nla_total_size(IFNAMSIZ);
1603         msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1604
1605         return msgsize;
1606 }
1607
1608 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1609                                 u32 portid, u32 seq, u32 flags, u8 cmd)
1610 {
1611         struct ovs_header *ovs_header;
1612         struct ovs_dp_stats dp_stats;
1613         int err;
1614
1615         ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1616                                    flags, cmd);
1617         if (!ovs_header)
1618                 goto error;
1619
1620         ovs_header->dp_ifindex = get_dpifindex(dp);
1621
1622         rcu_read_lock();
1623         err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1624         rcu_read_unlock();
1625         if (err)
1626                 goto nla_put_failure;
1627
1628         get_dp_stats(dp, &dp_stats);
1629         if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1630                 goto nla_put_failure;
1631
1632         return genlmsg_end(skb, ovs_header);
1633
1634 nla_put_failure:
1635         genlmsg_cancel(skb, ovs_header);
1636 error:
1637         return -EMSGSIZE;
1638 }
1639
1640 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
1641                                              u32 seq, u8 cmd)
1642 {
1643         struct sk_buff *skb;
1644         int retval;
1645
1646         skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1647         if (!skb)
1648                 return ERR_PTR(-ENOMEM);
1649
1650         retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
1651         if (retval < 0) {
1652                 kfree_skb(skb);
1653                 return ERR_PTR(retval);
1654         }
1655         return skb;
1656 }
1657
1658 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1659 {
1660         return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1661 }
1662
1663 /* Called with ovs_mutex. */
1664 static struct datapath *lookup_datapath(struct net *net,
1665                                         struct ovs_header *ovs_header,
1666                                         struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1667 {
1668         struct datapath *dp;
1669
1670         if (!a[OVS_DP_ATTR_NAME])
1671                 dp = get_dp(net, ovs_header->dp_ifindex);
1672         else {
1673                 struct vport *vport;
1674
1675                 rcu_read_lock();
1676                 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1677                 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1678                 rcu_read_unlock();
1679         }
1680         return dp ? dp : ERR_PTR(-ENODEV);
1681 }
1682
1683 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1684 {
1685         struct nlattr **a = info->attrs;
1686         struct vport_parms parms;
1687         struct sk_buff *reply;
1688         struct datapath *dp;
1689         struct vport *vport;
1690         struct ovs_net *ovs_net;
1691         int err, i;
1692
1693         err = -EINVAL;
1694         if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1695                 goto err;
1696
1697         err = ovs_dp_cmd_validate(a);
1698         if (err)
1699                 goto err;
1700
1701         ovs_lock();
1702
1703         err = -ENOMEM;
1704         dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1705         if (dp == NULL)
1706                 goto err_unlock_ovs;
1707
1708         ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1709
1710         /* Allocate table. */
1711         err = -ENOMEM;
1712         rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1713         if (!dp->table)
1714                 goto err_free_dp;
1715
1716         dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1717         if (!dp->stats_percpu) {
1718                 err = -ENOMEM;
1719                 goto err_destroy_table;
1720         }
1721
1722         dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1723                             GFP_KERNEL);
1724         if (!dp->ports) {
1725                 err = -ENOMEM;
1726                 goto err_destroy_percpu;
1727         }
1728
1729         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1730                 INIT_HLIST_HEAD(&dp->ports[i]);
1731
1732         /* Set up our datapath device. */
1733         parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1734         parms.type = OVS_VPORT_TYPE_INTERNAL;
1735         parms.options = NULL;
1736         parms.dp = dp;
1737         parms.port_no = OVSP_LOCAL;
1738         parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1739
1740         vport = new_vport(&parms);
1741         if (IS_ERR(vport)) {
1742                 err = PTR_ERR(vport);
1743                 if (err == -EBUSY)
1744                         err = -EEXIST;
1745
1746                 goto err_destroy_ports_array;
1747         }
1748
1749         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1750                                       info->snd_seq, OVS_DP_CMD_NEW);
1751         err = PTR_ERR(reply);
1752         if (IS_ERR(reply))
1753                 goto err_destroy_local_port;
1754
1755         ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1756         list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1757
1758         ovs_unlock();
1759
1760         ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1761         return 0;
1762
1763 err_destroy_local_port:
1764         ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1765 err_destroy_ports_array:
1766         kfree(dp->ports);
1767 err_destroy_percpu:
1768         free_percpu(dp->stats_percpu);
1769 err_destroy_table:
1770         ovs_flow_tbl_destroy(ovsl_dereference(dp->table), false);
1771 err_free_dp:
1772         release_net(ovs_dp_get_net(dp));
1773         kfree(dp);
1774 err_unlock_ovs:
1775         ovs_unlock();
1776 err:
1777         return err;
1778 }
1779
1780 /* Called with ovs_mutex. */
1781 static void __dp_destroy(struct datapath *dp)
1782 {
1783         int i;
1784
1785         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1786                 struct vport *vport;
1787                 struct hlist_node *n;
1788
1789                 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1790                         if (vport->port_no != OVSP_LOCAL)
1791                                 ovs_dp_detach_port(vport);
1792         }
1793
1794         list_del_rcu(&dp->list_node);
1795
1796         /* OVSP_LOCAL is datapath internal port. We need to make sure that
1797          * all port in datapath are destroyed first before freeing datapath.
1798          */
1799         ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1800
1801         call_rcu(&dp->rcu, destroy_dp_rcu);
1802 }
1803
1804 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1805 {
1806         struct sk_buff *reply;
1807         struct datapath *dp;
1808         int err;
1809
1810         err = ovs_dp_cmd_validate(info->attrs);
1811         if (err)
1812                 return err;
1813
1814         ovs_lock();
1815         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1816         err = PTR_ERR(dp);
1817         if (IS_ERR(dp))
1818                 goto unlock;
1819
1820         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1821                                       info->snd_seq, OVS_DP_CMD_DEL);
1822         err = PTR_ERR(reply);
1823         if (IS_ERR(reply))
1824                 goto unlock;
1825
1826         __dp_destroy(dp);
1827         ovs_unlock();
1828
1829         ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1830
1831         return 0;
1832 unlock:
1833         ovs_unlock();
1834         return err;
1835 }
1836
1837 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1838 {
1839         struct sk_buff *reply;
1840         struct datapath *dp;
1841         int err;
1842
1843         err = ovs_dp_cmd_validate(info->attrs);
1844         if (err)
1845                 return err;
1846
1847         ovs_lock();
1848         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1849         err = PTR_ERR(dp);
1850         if (IS_ERR(dp))
1851                 goto unlock;
1852
1853         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1854                                       info->snd_seq, OVS_DP_CMD_NEW);
1855         if (IS_ERR(reply)) {
1856                 err = PTR_ERR(reply);
1857                 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1858                                 ovs_dp_datapath_multicast_group.id, err);
1859                 err = 0;
1860                 goto unlock;
1861         }
1862
1863         ovs_unlock();
1864         ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1865
1866         return 0;
1867 unlock:
1868         ovs_unlock();
1869         return err;
1870 }
1871
1872 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1873 {
1874         struct sk_buff *reply;
1875         struct datapath *dp;
1876         int err;
1877
1878         err = ovs_dp_cmd_validate(info->attrs);
1879         if (err)
1880                 return err;
1881
1882         ovs_lock();
1883         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1884         if (IS_ERR(dp)) {
1885                 err = PTR_ERR(dp);
1886                 goto unlock;
1887         }
1888
1889         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1890                                       info->snd_seq, OVS_DP_CMD_NEW);
1891         if (IS_ERR(reply)) {
1892                 err = PTR_ERR(reply);
1893                 goto unlock;
1894         }
1895
1896         ovs_unlock();
1897         return genlmsg_reply(reply, info);
1898
1899 unlock:
1900         ovs_unlock();
1901         return err;
1902 }
1903
1904 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1905 {
1906         struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1907         struct datapath *dp;
1908         int skip = cb->args[0];
1909         int i = 0;
1910
1911         rcu_read_lock();
1912         list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1913                 if (i >= skip &&
1914                     ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1915                                          cb->nlh->nlmsg_seq, NLM_F_MULTI,
1916                                          OVS_DP_CMD_NEW) < 0)
1917                         break;
1918                 i++;
1919         }
1920         rcu_read_unlock();
1921
1922         cb->args[0] = i;
1923
1924         return skb->len;
1925 }
1926
1927 static struct genl_ops dp_datapath_genl_ops[] = {
1928         { .cmd = OVS_DP_CMD_NEW,
1929           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1930           .policy = datapath_policy,
1931           .doit = ovs_dp_cmd_new
1932         },
1933         { .cmd = OVS_DP_CMD_DEL,
1934           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1935           .policy = datapath_policy,
1936           .doit = ovs_dp_cmd_del
1937         },
1938         { .cmd = OVS_DP_CMD_GET,
1939           .flags = 0,               /* OK for unprivileged users. */
1940           .policy = datapath_policy,
1941           .doit = ovs_dp_cmd_get,
1942           .dumpit = ovs_dp_cmd_dump
1943         },
1944         { .cmd = OVS_DP_CMD_SET,
1945           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1946           .policy = datapath_policy,
1947           .doit = ovs_dp_cmd_set,
1948         },
1949 };
1950
1951 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1952 #ifdef HAVE_NLA_NUL_STRING
1953         [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1954         [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1955 #else
1956         [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1957 #endif
1958         [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1959         [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1960         [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1961         [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1962 };
1963
1964 static struct genl_family dp_vport_genl_family = {
1965         .id = GENL_ID_GENERATE,
1966         .hdrsize = sizeof(struct ovs_header),
1967         .name = OVS_VPORT_FAMILY,
1968         .version = OVS_VPORT_VERSION,
1969         .maxattr = OVS_VPORT_ATTR_MAX,
1970          SET_NETNSOK
1971 };
1972
1973 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1974         .name = OVS_VPORT_MCGROUP
1975 };
1976
1977 /* Called with ovs_mutex or RCU read lock. */
1978 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1979                                    u32 portid, u32 seq, u32 flags, u8 cmd)
1980 {
1981         struct ovs_header *ovs_header;
1982         struct ovs_vport_stats vport_stats;
1983         int err;
1984
1985         ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1986                                  flags, cmd);
1987         if (!ovs_header)
1988                 return -EMSGSIZE;
1989
1990         ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1991
1992         if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1993             nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1994             nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1995             nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1996                 goto nla_put_failure;
1997
1998         ovs_vport_get_stats(vport, &vport_stats);
1999         if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
2000                     &vport_stats))
2001                 goto nla_put_failure;
2002
2003         err = ovs_vport_get_options(vport, skb);
2004         if (err == -EMSGSIZE)
2005                 goto error;
2006
2007         return genlmsg_end(skb, ovs_header);
2008
2009 nla_put_failure:
2010         err = -EMSGSIZE;
2011 error:
2012         genlmsg_cancel(skb, ovs_header);
2013         return err;
2014 }
2015
2016 /* Called with ovs_mutex or RCU read lock. */
2017 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
2018                                          u32 seq, u8 cmd)
2019 {
2020         struct sk_buff *skb;
2021         int retval;
2022
2023         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2024         if (!skb)
2025                 return ERR_PTR(-ENOMEM);
2026
2027         retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
2028         BUG_ON(retval < 0);
2029
2030         return skb;
2031 }
2032
2033 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2034 {
2035         return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
2036 }
2037
2038 /* Called with ovs_mutex or RCU read lock. */
2039 static struct vport *lookup_vport(struct net *net,
2040                                   struct ovs_header *ovs_header,
2041                                   struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2042 {
2043         struct datapath *dp;
2044         struct vport *vport;
2045
2046         if (a[OVS_VPORT_ATTR_NAME]) {
2047                 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
2048                 if (!vport)
2049                         return ERR_PTR(-ENODEV);
2050                 if (ovs_header->dp_ifindex &&
2051                     ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2052                         return ERR_PTR(-ENODEV);
2053                 return vport;
2054         } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2055                 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2056
2057                 if (port_no >= DP_MAX_PORTS)
2058                         return ERR_PTR(-EFBIG);
2059
2060                 dp = get_dp(net, ovs_header->dp_ifindex);
2061                 if (!dp)
2062                         return ERR_PTR(-ENODEV);
2063
2064                 vport = ovs_vport_ovsl_rcu(dp, port_no);
2065                 if (!vport)
2066                         return ERR_PTR(-ENODEV);
2067                 return vport;
2068         } else
2069                 return ERR_PTR(-EINVAL);
2070 }
2071
2072 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2073 {
2074         struct nlattr **a = info->attrs;
2075         struct ovs_header *ovs_header = info->userhdr;
2076         struct vport_parms parms;
2077         struct sk_buff *reply;
2078         struct vport *vport;
2079         struct datapath *dp;
2080         u32 port_no;
2081         int err;
2082
2083         err = -EINVAL;
2084         if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2085             !a[OVS_VPORT_ATTR_UPCALL_PID])
2086                 goto exit;
2087
2088         err = ovs_vport_cmd_validate(a);
2089         if (err)
2090                 goto exit;
2091
2092         ovs_lock();
2093         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2094         err = -ENODEV;
2095         if (!dp)
2096                 goto exit_unlock;
2097
2098         if (a[OVS_VPORT_ATTR_PORT_NO]) {
2099                 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2100
2101                 err = -EFBIG;
2102                 if (port_no >= DP_MAX_PORTS)
2103                         goto exit_unlock;
2104
2105                 vport = ovs_vport_ovsl(dp, port_no);
2106                 err = -EBUSY;
2107                 if (vport)
2108                         goto exit_unlock;
2109         } else {
2110                 for (port_no = 1; ; port_no++) {
2111                         if (port_no >= DP_MAX_PORTS) {
2112                                 err = -EFBIG;
2113                                 goto exit_unlock;
2114                         }
2115                         vport = ovs_vport_ovsl(dp, port_no);
2116                         if (!vport)
2117                                 break;
2118                 }
2119         }
2120
2121         parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2122         parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2123         parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2124         parms.dp = dp;
2125         parms.port_no = port_no;
2126         parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2127
2128         vport = new_vport(&parms);
2129         err = PTR_ERR(vport);
2130         if (IS_ERR(vport))
2131                 goto exit_unlock;
2132
2133         err = 0;
2134         if (a[OVS_VPORT_ATTR_STATS])
2135                 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2136
2137         reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
2138                                          OVS_VPORT_CMD_NEW);
2139         if (IS_ERR(reply)) {
2140                 err = PTR_ERR(reply);
2141                 ovs_dp_detach_port(vport);
2142                 goto exit_unlock;
2143         }
2144
2145         ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2146
2147 exit_unlock:
2148         ovs_unlock();
2149 exit:
2150         return err;
2151 }
2152
2153 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2154 {
2155         struct nlattr **a = info->attrs;
2156         struct sk_buff *reply;
2157         struct vport *vport;
2158         int err;
2159
2160         err = ovs_vport_cmd_validate(a);
2161         if (err)
2162                 goto exit;
2163
2164         ovs_lock();
2165         vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2166         err = PTR_ERR(vport);
2167         if (IS_ERR(vport))
2168                 goto exit_unlock;
2169
2170         if (a[OVS_VPORT_ATTR_TYPE] &&
2171             nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2172                 err = -EINVAL;
2173                 goto exit_unlock;
2174         }
2175
2176         reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2177         if (!reply) {
2178                 err = -ENOMEM;
2179                 goto exit_unlock;
2180         }
2181
2182         if (a[OVS_VPORT_ATTR_OPTIONS]) {
2183                 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2184                 if (err)
2185                         goto exit_free;
2186         }
2187
2188         if (a[OVS_VPORT_ATTR_STATS])
2189                 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2190
2191         if (a[OVS_VPORT_ATTR_UPCALL_PID])
2192                 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2193
2194         err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2195                                       info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2196         BUG_ON(err < 0);
2197
2198         ovs_unlock();
2199         ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2200         return 0;
2201
2202 exit_free:
2203         kfree_skb(reply);
2204 exit_unlock:
2205         ovs_unlock();
2206 exit:
2207         return err;
2208 }
2209
2210 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2211 {
2212         struct nlattr **a = info->attrs;
2213         struct sk_buff *reply;
2214         struct vport *vport;
2215         int err;
2216
2217         err = ovs_vport_cmd_validate(a);
2218         if (err)
2219                 goto exit;
2220
2221         ovs_lock();
2222         vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2223         err = PTR_ERR(vport);
2224         if (IS_ERR(vport))
2225                 goto exit_unlock;
2226
2227         if (vport->port_no == OVSP_LOCAL) {
2228                 err = -EINVAL;
2229                 goto exit_unlock;
2230         }
2231
2232         reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2233                                          info->snd_seq, OVS_VPORT_CMD_DEL);
2234         err = PTR_ERR(reply);
2235         if (IS_ERR(reply))
2236                 goto exit_unlock;
2237
2238         err = 0;
2239         ovs_dp_detach_port(vport);
2240
2241         ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2242
2243 exit_unlock:
2244         ovs_unlock();
2245 exit:
2246         return err;
2247 }
2248
2249 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2250 {
2251         struct nlattr **a = info->attrs;
2252         struct ovs_header *ovs_header = info->userhdr;
2253         struct sk_buff *reply;
2254         struct vport *vport;
2255         int err;
2256
2257         err = ovs_vport_cmd_validate(a);
2258         if (err)
2259                 goto exit;
2260
2261         rcu_read_lock();
2262         vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2263         err = PTR_ERR(vport);
2264         if (IS_ERR(vport))
2265                 goto exit_unlock;
2266
2267         reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2268                                          info->snd_seq, OVS_VPORT_CMD_NEW);
2269         err = PTR_ERR(reply);
2270         if (IS_ERR(reply))
2271                 goto exit_unlock;
2272
2273         rcu_read_unlock();
2274
2275         return genlmsg_reply(reply, info);
2276
2277 exit_unlock:
2278         rcu_read_unlock();
2279 exit:
2280         return err;
2281 }
2282
2283 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2284 {
2285         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2286         struct datapath *dp;
2287         int bucket = cb->args[0], skip = cb->args[1];
2288         int i, j = 0;
2289
2290         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2291         if (!dp)
2292                 return -ENODEV;
2293
2294         rcu_read_lock();
2295         for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2296                 struct vport *vport;
2297
2298                 j = 0;
2299                 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2300                         if (j >= skip &&
2301                             ovs_vport_cmd_fill_info(vport, skb,
2302                                                     NETLINK_CB(cb->skb).portid,
2303                                                     cb->nlh->nlmsg_seq,
2304                                                     NLM_F_MULTI,
2305                                                     OVS_VPORT_CMD_NEW) < 0)
2306                                 goto out;
2307
2308                         j++;
2309                 }
2310                 skip = 0;
2311         }
2312 out:
2313         rcu_read_unlock();
2314
2315         cb->args[0] = i;
2316         cb->args[1] = j;
2317
2318         return skb->len;
2319 }
2320
2321 static struct genl_ops dp_vport_genl_ops[] = {
2322         { .cmd = OVS_VPORT_CMD_NEW,
2323           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2324           .policy = vport_policy,
2325           .doit = ovs_vport_cmd_new
2326         },
2327         { .cmd = OVS_VPORT_CMD_DEL,
2328           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2329           .policy = vport_policy,
2330           .doit = ovs_vport_cmd_del
2331         },
2332         { .cmd = OVS_VPORT_CMD_GET,
2333           .flags = 0,               /* OK for unprivileged users. */
2334           .policy = vport_policy,
2335           .doit = ovs_vport_cmd_get,
2336           .dumpit = ovs_vport_cmd_dump
2337         },
2338         { .cmd = OVS_VPORT_CMD_SET,
2339           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2340           .policy = vport_policy,
2341           .doit = ovs_vport_cmd_set,
2342         },
2343 };
2344
2345 struct genl_family_and_ops {
2346         struct genl_family *family;
2347         struct genl_ops *ops;
2348         int n_ops;
2349         struct genl_multicast_group *group;
2350 };
2351
2352 static const struct genl_family_and_ops dp_genl_families[] = {
2353         { &dp_datapath_genl_family,
2354           dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2355           &ovs_dp_datapath_multicast_group },
2356         { &dp_vport_genl_family,
2357           dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2358           &ovs_dp_vport_multicast_group },
2359         { &dp_flow_genl_family,
2360           dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2361           &ovs_dp_flow_multicast_group },
2362         { &dp_packet_genl_family,
2363           dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2364           NULL },
2365 };
2366
2367 static void dp_unregister_genl(int n_families)
2368 {
2369         int i;
2370
2371         for (i = 0; i < n_families; i++)
2372                 genl_unregister_family(dp_genl_families[i].family);
2373 }
2374
2375 static int dp_register_genl(void)
2376 {
2377         int n_registered;
2378         int err;
2379         int i;
2380
2381         n_registered = 0;
2382         for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2383                 const struct genl_family_and_ops *f = &dp_genl_families[i];
2384
2385                 err = genl_register_family_with_ops(f->family, f->ops,
2386                                                     f->n_ops);
2387                 if (err)
2388                         goto error;
2389                 n_registered++;
2390
2391                 if (f->group) {
2392                         err = genl_register_mc_group(f->family, f->group);
2393                         if (err)
2394                                 goto error;
2395                 }
2396         }
2397
2398         return 0;
2399
2400 error:
2401         dp_unregister_genl(n_registered);
2402         return err;
2403 }
2404
2405 static void rehash_flow_table(struct work_struct *work)
2406 {
2407         struct datapath *dp;
2408         struct net *net;
2409
2410         ovs_lock();
2411         rtnl_lock();
2412         for_each_net(net) {
2413                 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2414
2415                 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2416                         struct flow_table *old_table = ovsl_dereference(dp->table);
2417                         struct flow_table *new_table;
2418
2419                         new_table = ovs_flow_tbl_rehash(old_table);
2420                         if (!IS_ERR(new_table)) {
2421                                 rcu_assign_pointer(dp->table, new_table);
2422                                 ovs_flow_tbl_destroy(old_table, true);
2423                         }
2424                 }
2425         }
2426         rtnl_unlock();
2427         ovs_unlock();
2428         schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2429 }
2430
2431 static int __net_init ovs_init_net(struct net *net)
2432 {
2433         struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2434
2435         INIT_LIST_HEAD(&ovs_net->dps);
2436         INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2437         return 0;
2438 }
2439
2440 static void __net_exit ovs_exit_net(struct net *net)
2441 {
2442         struct datapath *dp, *dp_next;
2443         struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2444
2445         ovs_lock();
2446         list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2447                 __dp_destroy(dp);
2448         ovs_unlock();
2449
2450         cancel_work_sync(&ovs_net->dp_notify_work);
2451 }
2452
2453 static struct pernet_operations ovs_net_ops = {
2454         .init = ovs_init_net,
2455         .exit = ovs_exit_net,
2456         .id   = &ovs_net_id,
2457         .size = sizeof(struct ovs_net),
2458 };
2459
2460 DEFINE_COMPAT_PNET_REG_FUNC(device);
2461
2462 static int __init dp_init(void)
2463 {
2464         int err;
2465
2466         BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2467
2468         pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2469                 VERSION);
2470
2471         err = ovs_workqueues_init();
2472         if (err)
2473                 goto error;
2474
2475         err = ovs_flow_init();
2476         if (err)
2477                 goto error_wq;
2478
2479         err = ovs_vport_init();
2480         if (err)
2481                 goto error_flow_exit;
2482
2483         err = register_pernet_device(&ovs_net_ops);
2484         if (err)
2485                 goto error_vport_exit;
2486
2487         err = register_netdevice_notifier(&ovs_dp_device_notifier);
2488         if (err)
2489                 goto error_netns_exit;
2490
2491         err = dp_register_genl();
2492         if (err < 0)
2493                 goto error_unreg_notifier;
2494
2495         schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2496
2497         return 0;
2498
2499 error_unreg_notifier:
2500         unregister_netdevice_notifier(&ovs_dp_device_notifier);
2501 error_netns_exit:
2502         unregister_pernet_device(&ovs_net_ops);
2503 error_vport_exit:
2504         ovs_vport_exit();
2505 error_flow_exit:
2506         ovs_flow_exit();
2507 error_wq:
2508         ovs_workqueues_exit();
2509 error:
2510         return err;
2511 }
2512
2513 static void dp_cleanup(void)
2514 {
2515         cancel_delayed_work_sync(&rehash_flow_wq);
2516         dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2517         unregister_netdevice_notifier(&ovs_dp_device_notifier);
2518         unregister_pernet_device(&ovs_net_ops);
2519         rcu_barrier();
2520         ovs_vport_exit();
2521         ovs_flow_exit();
2522         ovs_workqueues_exit();
2523 }
2524
2525 module_init(dp_init);
2526 module_exit(dp_cleanup);
2527
2528 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2529 MODULE_LICENSE("GPL");
2530 MODULE_VERSION(VERSION);