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