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