datapath: Return correct error code when dumping flow actions.
[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,9,0)
65 #error Kernels before 2.6.18 or after 3.8 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.portid = p->upcall_portid;
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->portid == 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->portid);
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 struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, int attr_len)
531 {
532
533         struct sw_flow_actions *acts;
534         int new_acts_size;
535         int req_size = NLA_ALIGN(attr_len);
536         int next_offset = offsetof(struct sw_flow_actions, actions) +
537                                         (*sfa)->actions_len;
538
539         if (req_size <= (ksize(*sfa) - next_offset))
540                 goto out;
541
542         new_acts_size = ksize(*sfa) * 2;
543
544         if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
545                 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
546                         return ERR_PTR(-EMSGSIZE);
547                 new_acts_size = MAX_ACTIONS_BUFSIZE;
548         }
549
550         acts = ovs_flow_actions_alloc(new_acts_size);
551         if (IS_ERR(acts))
552                 return (void *)acts;
553
554         memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
555         acts->actions_len = (*sfa)->actions_len;
556         kfree(*sfa);
557         *sfa = acts;
558
559 out:
560         (*sfa)->actions_len += req_size;
561         return  (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
562 }
563
564 static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
565 {
566         struct nlattr *a;
567
568         a = reserve_sfa_size(sfa, nla_attr_size(len));
569         if (IS_ERR(a))
570                 return PTR_ERR(a);
571
572         a->nla_type = attrtype;
573         a->nla_len = nla_attr_size(len);
574
575         if (data)
576                 memcpy(nla_data(a), data, len);
577         memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
578
579         return 0;
580 }
581
582 static inline int add_nested_action_start(struct sw_flow_actions **sfa, int attrtype)
583 {
584         int used = (*sfa)->actions_len;
585         int err;
586
587         err = add_action(sfa, attrtype, NULL, 0);
588         if (err)
589                 return err;
590
591         return used;
592 }
593
594 static inline void add_nested_action_end(struct sw_flow_actions *sfa, int st_offset)
595 {
596         struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + st_offset);
597
598         a->nla_len = sfa->actions_len - st_offset;
599 }
600
601 static int validate_and_copy_actions(const struct nlattr *attr,
602                                 const struct sw_flow_key *key, int depth,
603                                 struct sw_flow_actions **sfa);
604
605 static int validate_and_copy_sample(const struct nlattr *attr,
606                            const struct sw_flow_key *key, int depth,
607                            struct sw_flow_actions **sfa)
608 {
609         const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
610         const struct nlattr *probability, *actions;
611         const struct nlattr *a;
612         int rem, start, err, st_acts;
613
614         memset(attrs, 0, sizeof(attrs));
615         nla_for_each_nested(a, attr, rem) {
616                 int type = nla_type(a);
617                 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
618                         return -EINVAL;
619                 attrs[type] = a;
620         }
621         if (rem)
622                 return -EINVAL;
623
624         probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
625         if (!probability || nla_len(probability) != sizeof(u32))
626                 return -EINVAL;
627
628         actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
629         if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
630                 return -EINVAL;
631
632         /* validation done, copy sample action. */
633         start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
634         if (start < 0)
635                 return start;
636         err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, nla_data(probability), sizeof(u32));
637         if (err)
638                 return err;
639         st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
640         if (st_acts < 0)
641                 return st_acts;
642
643         err = validate_and_copy_actions(actions, key, depth + 1, sfa);
644         if (err)
645                 return err;
646
647         add_nested_action_end(*sfa, st_acts);
648         add_nested_action_end(*sfa, start);
649
650         return 0;
651 }
652
653 static int validate_tp_port(const struct sw_flow_key *flow_key)
654 {
655         if (flow_key->eth.type == htons(ETH_P_IP)) {
656                 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
657                         return 0;
658         } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
659                 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
660                         return 0;
661         }
662
663         return -EINVAL;
664 }
665
666 static int validate_and_copy_set_tun(const struct nlattr *attr,
667                                      struct sw_flow_actions **sfa)
668 {
669         struct ovs_key_ipv4_tunnel tun_key;
670         int err, start;
671
672         err = ipv4_tun_from_nlattr(nla_data(attr), &tun_key);
673         if (err)
674                 return err;
675
676         start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
677         if (start < 0)
678                 return start;
679
680         err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &tun_key, sizeof(tun_key));
681         add_nested_action_end(*sfa, start);
682
683         return err;
684 }
685
686 static int validate_set(const struct nlattr *a,
687                         const struct sw_flow_key *flow_key,
688                         struct sw_flow_actions **sfa,
689                         bool *set_tun)
690 {
691         const struct nlattr *ovs_key = nla_data(a);
692         int key_type = nla_type(ovs_key);
693
694         /* There can be only one key in a action */
695         if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
696                 return -EINVAL;
697
698         if (key_type > OVS_KEY_ATTR_MAX ||
699             (ovs_key_lens[key_type] != nla_len(ovs_key) &&
700              ovs_key_lens[key_type] != -1))
701                 return -EINVAL;
702
703         switch (key_type) {
704         const struct ovs_key_ipv4 *ipv4_key;
705         const struct ovs_key_ipv6 *ipv6_key;
706         int err;
707
708         case OVS_KEY_ATTR_PRIORITY:
709         case OVS_KEY_ATTR_TUN_ID:
710         case OVS_KEY_ATTR_ETHERNET:
711                 break;
712
713         case OVS_KEY_ATTR_SKB_MARK:
714 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
715                 if (nla_get_u32(ovs_key) != 0)
716                         return -EINVAL;
717 #endif
718                 break;
719
720         case OVS_KEY_ATTR_TUNNEL:
721                 *set_tun = true;
722                 err = validate_and_copy_set_tun(a, sfa);
723                 if (err)
724                         return err;
725                 break;
726
727         case OVS_KEY_ATTR_IPV4:
728                 if (flow_key->eth.type != htons(ETH_P_IP))
729                         return -EINVAL;
730
731                 if (!flow_key->ip.proto)
732                         return -EINVAL;
733
734                 ipv4_key = nla_data(ovs_key);
735                 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
736                         return -EINVAL;
737
738                 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
739                         return -EINVAL;
740
741                 break;
742
743         case OVS_KEY_ATTR_IPV6:
744                 if (flow_key->eth.type != htons(ETH_P_IPV6))
745                         return -EINVAL;
746
747                 if (!flow_key->ip.proto)
748                         return -EINVAL;
749
750                 ipv6_key = nla_data(ovs_key);
751                 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
752                         return -EINVAL;
753
754                 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
755                         return -EINVAL;
756
757                 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
758                         return -EINVAL;
759
760                 break;
761
762         case OVS_KEY_ATTR_TCP:
763                 if (flow_key->ip.proto != IPPROTO_TCP)
764                         return -EINVAL;
765
766                 return validate_tp_port(flow_key);
767
768         case OVS_KEY_ATTR_UDP:
769                 if (flow_key->ip.proto != IPPROTO_UDP)
770                         return -EINVAL;
771
772                 return validate_tp_port(flow_key);
773
774         default:
775                 return -EINVAL;
776         }
777
778         return 0;
779 }
780
781 static int validate_userspace(const struct nlattr *attr)
782 {
783         static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] =   {
784                 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
785                 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
786         };
787         struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
788         int error;
789
790         error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
791                                  attr, userspace_policy);
792         if (error)
793                 return error;
794
795         if (!a[OVS_USERSPACE_ATTR_PID] ||
796             !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
797                 return -EINVAL;
798
799         return 0;
800 }
801
802 static int copy_action(const struct nlattr *from,
803                       struct sw_flow_actions **sfa)
804 {
805         int totlen = NLA_ALIGN(from->nla_len);
806         struct nlattr *to;
807
808         to = reserve_sfa_size(sfa, from->nla_len);
809         if (IS_ERR(to))
810                 return PTR_ERR(to);
811
812         memcpy(to, from, totlen);
813         return 0;
814 }
815
816 static int validate_and_copy_actions(const struct nlattr *attr,
817                                 const struct sw_flow_key *key,
818                                 int depth,
819                                 struct sw_flow_actions **sfa)
820 {
821         const struct nlattr *a;
822         int rem, err;
823
824         if (depth >= SAMPLE_ACTION_DEPTH)
825                 return -EOVERFLOW;
826
827         nla_for_each_nested(a, attr, rem) {
828                 /* Expected argument lengths, (u32)-1 for variable length. */
829                 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
830                         [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
831                         [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
832                         [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
833                         [OVS_ACTION_ATTR_POP_VLAN] = 0,
834                         [OVS_ACTION_ATTR_SET] = (u32)-1,
835                         [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
836                 };
837                 const struct ovs_action_push_vlan *vlan;
838                 int type = nla_type(a);
839                 bool skip_copy;
840
841                 if (type > OVS_ACTION_ATTR_MAX ||
842                     (action_lens[type] != nla_len(a) &&
843                      action_lens[type] != (u32)-1))
844                         return -EINVAL;
845
846                 skip_copy = false;
847                 switch (type) {
848                 case OVS_ACTION_ATTR_UNSPEC:
849                         return -EINVAL;
850
851                 case OVS_ACTION_ATTR_USERSPACE:
852                         err = validate_userspace(a);
853                         if (err)
854                                 return err;
855                         break;
856
857                 case OVS_ACTION_ATTR_OUTPUT:
858                         if (nla_get_u32(a) >= DP_MAX_PORTS)
859                                 return -EINVAL;
860                         break;
861
862
863                 case OVS_ACTION_ATTR_POP_VLAN:
864                         break;
865
866                 case OVS_ACTION_ATTR_PUSH_VLAN:
867                         vlan = nla_data(a);
868                         if (vlan->vlan_tpid != htons(ETH_P_8021Q))
869                                 return -EINVAL;
870                         if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
871                                 return -EINVAL;
872                         break;
873
874                 case OVS_ACTION_ATTR_SET:
875                         err = validate_set(a, key, sfa, &skip_copy);
876                         if (err)
877                                 return err;
878                         break;
879
880                 case OVS_ACTION_ATTR_SAMPLE:
881                         err = validate_and_copy_sample(a, key, depth, sfa);
882                         if (err)
883                                 return err;
884                         skip_copy = true;
885                         break;
886
887                 default:
888                         return -EINVAL;
889                 }
890                 if (!skip_copy) {
891                         err = copy_action(a, sfa);
892                         if (err)
893                                 return err;
894                 }
895         }
896
897         if (rem > 0)
898                 return -EINVAL;
899
900         return 0;
901 }
902
903 static void clear_stats(struct sw_flow *flow)
904 {
905         flow->used = 0;
906         flow->tcp_flags = 0;
907         flow->packet_count = 0;
908         flow->byte_count = 0;
909 }
910
911 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
912 {
913         struct ovs_header *ovs_header = info->userhdr;
914         struct nlattr **a = info->attrs;
915         struct sw_flow_actions *acts;
916         struct sk_buff *packet;
917         struct sw_flow *flow;
918         struct datapath *dp;
919         struct ethhdr *eth;
920         int len;
921         int err;
922         int key_len;
923
924         err = -EINVAL;
925         if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
926             !a[OVS_PACKET_ATTR_ACTIONS] ||
927             nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
928                 goto err;
929
930         len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
931         packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
932         err = -ENOMEM;
933         if (!packet)
934                 goto err;
935         skb_reserve(packet, NET_IP_ALIGN);
936
937         memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
938
939         skb_reset_mac_header(packet);
940         eth = eth_hdr(packet);
941
942         /* Normally, setting the skb 'protocol' field would be handled by a
943          * call to eth_type_trans(), but it assumes there's a sending
944          * device, which we may not have. */
945         if (ntohs(eth->h_proto) >= 1536)
946                 packet->protocol = eth->h_proto;
947         else
948                 packet->protocol = htons(ETH_P_802_2);
949
950         /* Build an sw_flow for sending this packet. */
951         flow = ovs_flow_alloc();
952         err = PTR_ERR(flow);
953         if (IS_ERR(flow))
954                 goto err_kfree_skb;
955
956         err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
957         if (err)
958                 goto err_flow_free;
959
960         err = ovs_flow_metadata_from_nlattrs(flow, key_len, a[OVS_PACKET_ATTR_KEY]);
961         if (err)
962                 goto err_flow_free;
963         acts = ovs_flow_actions_alloc(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
964         err = PTR_ERR(acts);
965         if (IS_ERR(acts))
966                 goto err_flow_free;
967
968         err = validate_and_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0, &acts);
969         rcu_assign_pointer(flow->sf_acts, acts);
970         if (err)
971                 goto err_flow_free;
972
973         OVS_CB(packet)->flow = flow;
974         packet->priority = flow->key.phy.priority;
975         skb_set_mark(packet, flow->key.phy.skb_mark);
976
977         rcu_read_lock();
978         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
979         err = -ENODEV;
980         if (!dp)
981                 goto err_unlock;
982
983         local_bh_disable();
984         err = ovs_execute_actions(dp, packet);
985         local_bh_enable();
986         rcu_read_unlock();
987
988         ovs_flow_free(flow);
989         return err;
990
991 err_unlock:
992         rcu_read_unlock();
993 err_flow_free:
994         ovs_flow_free(flow);
995 err_kfree_skb:
996         kfree_skb(packet);
997 err:
998         return err;
999 }
1000
1001 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
1002         [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
1003         [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
1004         [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
1005 };
1006
1007 static struct genl_ops dp_packet_genl_ops[] = {
1008         { .cmd = OVS_PACKET_CMD_EXECUTE,
1009           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1010           .policy = packet_policy,
1011           .doit = ovs_packet_cmd_execute
1012         }
1013 };
1014
1015 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
1016 {
1017         int i;
1018         struct flow_table *table = genl_dereference(dp->table);
1019
1020         stats->n_flows = ovs_flow_tbl_count(table);
1021
1022         stats->n_hit = stats->n_missed = stats->n_lost = 0;
1023         for_each_possible_cpu(i) {
1024                 const struct dp_stats_percpu *percpu_stats;
1025                 struct dp_stats_percpu local_stats;
1026                 unsigned int start;
1027
1028                 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
1029
1030                 do {
1031                         start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
1032                         local_stats = *percpu_stats;
1033                 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
1034
1035                 stats->n_hit += local_stats.n_hit;
1036                 stats->n_missed += local_stats.n_missed;
1037                 stats->n_lost += local_stats.n_lost;
1038         }
1039 }
1040
1041 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1042         [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1043         [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1044         [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1045 };
1046
1047 static struct genl_family dp_flow_genl_family = {
1048         .id = GENL_ID_GENERATE,
1049         .hdrsize = sizeof(struct ovs_header),
1050         .name = OVS_FLOW_FAMILY,
1051         .version = OVS_FLOW_VERSION,
1052         .maxattr = OVS_FLOW_ATTR_MAX,
1053          SET_NETNSOK
1054 };
1055
1056 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
1057         .name = OVS_FLOW_MCGROUP
1058 };
1059
1060 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
1061 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1062 {
1063         const struct nlattr *a;
1064         struct nlattr *start;
1065         int err = 0, rem;
1066
1067         start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1068         if (!start)
1069                 return -EMSGSIZE;
1070
1071         nla_for_each_nested(a, attr, rem) {
1072                 int type = nla_type(a);
1073                 struct nlattr *st_sample;
1074
1075                 switch (type) {
1076                 case OVS_SAMPLE_ATTR_PROBABILITY:
1077                         if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
1078                                 return -EMSGSIZE;
1079                         break;
1080                 case OVS_SAMPLE_ATTR_ACTIONS:
1081                         st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1082                         if (!st_sample)
1083                                 return -EMSGSIZE;
1084                         err = actions_to_attr(nla_data(a), nla_len(a), skb);
1085                         if (err)
1086                                 return err;
1087                         nla_nest_end(skb, st_sample);
1088                         break;
1089                 }
1090         }
1091
1092         nla_nest_end(skb, start);
1093         return err;
1094 }
1095
1096 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1097 {
1098         const struct nlattr *ovs_key = nla_data(a);
1099         int key_type = nla_type(ovs_key);
1100         struct nlattr *start;
1101         int err;
1102
1103         switch (key_type) {
1104         case OVS_KEY_ATTR_IPV4_TUNNEL:
1105                 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1106                 if (!start)
1107                         return -EMSGSIZE;
1108
1109                 err = ipv4_tun_to_nlattr(skb, nla_data(ovs_key));
1110                 if (err)
1111                         return err;
1112                 nla_nest_end(skb, start);
1113                 break;
1114         default:
1115                 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1116                         return -EMSGSIZE;
1117                 break;
1118         }
1119
1120         return 0;
1121 }
1122
1123 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
1124 {
1125         const struct nlattr *a;
1126         int rem, err;
1127
1128         nla_for_each_attr(a, attr, len, rem) {
1129                 int type = nla_type(a);
1130
1131                 switch (type) {
1132                 case OVS_ACTION_ATTR_SET:
1133                         err = set_action_to_attr(a, skb);
1134                         if (err)
1135                                 return err;
1136                         break;
1137
1138                 case OVS_ACTION_ATTR_SAMPLE:
1139                         err = sample_action_to_attr(a, skb);
1140                         if (err)
1141                                 return err;
1142                         break;
1143                 default:
1144                         if (nla_put(skb, type, nla_len(a), nla_data(a)))
1145                                 return -EMSGSIZE;
1146                         break;
1147                 }
1148         }
1149
1150         return 0;
1151 }
1152
1153 /* Called with genl_lock. */
1154 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
1155                                   struct sk_buff *skb, u32 portid,
1156                                   u32 seq, u32 flags, u8 cmd)
1157 {
1158         const int skb_orig_len = skb->len;
1159         const struct sw_flow_actions *sf_acts;
1160         struct nlattr *start;
1161         struct ovs_flow_stats stats;
1162         struct ovs_header *ovs_header;
1163         struct nlattr *nla;
1164         unsigned long used;
1165         u8 tcp_flags;
1166         int err;
1167
1168         sf_acts = rcu_dereference_protected(flow->sf_acts,
1169                                             lockdep_genl_is_held());
1170
1171         ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
1172         if (!ovs_header)
1173                 return -EMSGSIZE;
1174
1175         ovs_header->dp_ifindex = get_dpifindex(dp);
1176
1177         nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
1178         if (!nla)
1179                 goto nla_put_failure;
1180         err = ovs_flow_to_nlattrs(&flow->key, skb);
1181         if (err)
1182                 goto error;
1183         nla_nest_end(skb, nla);
1184
1185         spin_lock_bh(&flow->lock);
1186         used = flow->used;
1187         stats.n_packets = flow->packet_count;
1188         stats.n_bytes = flow->byte_count;
1189         tcp_flags = flow->tcp_flags;
1190         spin_unlock_bh(&flow->lock);
1191
1192         if (used &&
1193             nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
1194                 goto nla_put_failure;
1195
1196         if (stats.n_packets &&
1197             nla_put(skb, OVS_FLOW_ATTR_STATS,
1198                     sizeof(struct ovs_flow_stats), &stats))
1199                 goto nla_put_failure;
1200
1201         if (tcp_flags &&
1202             nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
1203                 goto nla_put_failure;
1204
1205         /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
1206          * this is the first flow to be dumped into 'skb'.  This is unusual for
1207          * Netlink but individual action lists can be longer than
1208          * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
1209          * The userspace caller can always fetch the actions separately if it
1210          * really wants them.  (Most userspace callers in fact don't care.)
1211          *
1212          * This can only fail for dump operations because the skb is always
1213          * properly sized for single flows.
1214          */
1215         start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
1216         if (start) {
1217                 err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
1218                 if (!err)
1219                         nla_nest_end(skb, start);
1220                 else {
1221                         if (skb_orig_len)
1222                                 goto error;
1223
1224                         nla_nest_cancel(skb, start);
1225                 }
1226         } else if (skb_orig_len)
1227                 goto nla_put_failure;
1228
1229         return genlmsg_end(skb, ovs_header);
1230
1231 nla_put_failure:
1232         err = -EMSGSIZE;
1233 error:
1234         genlmsg_cancel(skb, ovs_header);
1235         return err;
1236 }
1237
1238 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
1239 {
1240         const struct sw_flow_actions *sf_acts;
1241         int len;
1242
1243         sf_acts = rcu_dereference_protected(flow->sf_acts,
1244                                             lockdep_genl_is_held());
1245
1246         /* OVS_FLOW_ATTR_KEY */
1247         len = nla_total_size(FLOW_BUFSIZE);
1248         /* OVS_FLOW_ATTR_ACTIONS */
1249         len += nla_total_size(sf_acts->actions_len);
1250         /* OVS_FLOW_ATTR_STATS */
1251         len += nla_total_size(sizeof(struct ovs_flow_stats));
1252         /* OVS_FLOW_ATTR_TCP_FLAGS */
1253         len += nla_total_size(1);
1254         /* OVS_FLOW_ATTR_USED */
1255         len += nla_total_size(8);
1256
1257         len += NLMSG_ALIGN(sizeof(struct ovs_header));
1258
1259         return genlmsg_new(len, GFP_KERNEL);
1260 }
1261
1262 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
1263                                                struct datapath *dp,
1264                                                u32 portid, u32 seq, u8 cmd)
1265 {
1266         struct sk_buff *skb;
1267         int retval;
1268
1269         skb = ovs_flow_cmd_alloc_info(flow);
1270         if (!skb)
1271                 return ERR_PTR(-ENOMEM);
1272
1273         retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
1274         BUG_ON(retval < 0);
1275         return skb;
1276 }
1277
1278 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1279 {
1280         struct nlattr **a = info->attrs;
1281         struct ovs_header *ovs_header = info->userhdr;
1282         struct sw_flow_key key;
1283         struct sw_flow *flow;
1284         struct sk_buff *reply;
1285         struct datapath *dp;
1286         struct flow_table *table;
1287         struct sw_flow_actions *acts = NULL;
1288         int error;
1289         int key_len;
1290
1291         /* Extract key. */
1292         error = -EINVAL;
1293         if (!a[OVS_FLOW_ATTR_KEY])
1294                 goto error;
1295         error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1296         if (error)
1297                 goto error;
1298
1299         /* Validate actions. */
1300         if (a[OVS_FLOW_ATTR_ACTIONS]) {
1301                 acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
1302                 error = PTR_ERR(acts);
1303                 if (IS_ERR(acts))
1304                         goto error;
1305
1306                 error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &key,  0, &acts);
1307                 if (error)
1308                         goto err_kfree;
1309         } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1310                 error = -EINVAL;
1311                 goto error;
1312         }
1313
1314         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1315         error = -ENODEV;
1316         if (!dp)
1317                 goto err_kfree;
1318
1319         table = genl_dereference(dp->table);
1320         flow = ovs_flow_tbl_lookup(table, &key, key_len);
1321         if (!flow) {
1322                 /* Bail out if we're not allowed to create a new flow. */
1323                 error = -ENOENT;
1324                 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1325                         goto err_kfree;
1326
1327                 /* Expand table, if necessary, to make room. */
1328                 if (ovs_flow_tbl_need_to_expand(table)) {
1329                         struct flow_table *new_table;
1330
1331                         new_table = ovs_flow_tbl_expand(table);
1332                         if (!IS_ERR(new_table)) {
1333                                 rcu_assign_pointer(dp->table, new_table);
1334                                 ovs_flow_tbl_deferred_destroy(table);
1335                                 table = genl_dereference(dp->table);
1336                         }
1337                 }
1338
1339                 /* Allocate flow. */
1340                 flow = ovs_flow_alloc();
1341                 if (IS_ERR(flow)) {
1342                         error = PTR_ERR(flow);
1343                         goto err_kfree;
1344                 }
1345                 clear_stats(flow);
1346
1347                 rcu_assign_pointer(flow->sf_acts, acts);
1348
1349                 /* Put flow in bucket. */
1350                 ovs_flow_tbl_insert(table, flow, &key, key_len);
1351
1352                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1353                                                 info->snd_seq,
1354                                                 OVS_FLOW_CMD_NEW);
1355         } else {
1356                 /* We found a matching flow. */
1357                 struct sw_flow_actions *old_acts;
1358
1359                 /* Bail out if we're not allowed to modify an existing flow.
1360                  * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1361                  * because Generic Netlink treats the latter as a dump
1362                  * request.  We also accept NLM_F_EXCL in case that bug ever
1363                  * gets fixed.
1364                  */
1365                 error = -EEXIST;
1366                 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1367                     info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1368                         goto err_kfree;
1369
1370                 /* Update actions. */
1371                 old_acts = rcu_dereference_protected(flow->sf_acts,
1372                                                      lockdep_genl_is_held());
1373                 rcu_assign_pointer(flow->sf_acts, acts);
1374                 ovs_flow_deferred_free_acts(old_acts);
1375
1376                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1377                                                info->snd_seq, OVS_FLOW_CMD_NEW);
1378
1379                 /* Clear stats. */
1380                 if (a[OVS_FLOW_ATTR_CLEAR]) {
1381                         spin_lock_bh(&flow->lock);
1382                         clear_stats(flow);
1383                         spin_unlock_bh(&flow->lock);
1384                 }
1385         }
1386
1387         if (!IS_ERR(reply))
1388                 genl_notify(reply, genl_info_net(info), info->snd_portid,
1389                            ovs_dp_flow_multicast_group.id, info->nlhdr,
1390                            GFP_KERNEL);
1391         else
1392                 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1393                                 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1394         return 0;
1395
1396 err_kfree:
1397         kfree(acts);
1398 error:
1399         return error;
1400 }
1401
1402 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1403 {
1404         struct nlattr **a = info->attrs;
1405         struct ovs_header *ovs_header = info->userhdr;
1406         struct sw_flow_key key;
1407         struct sk_buff *reply;
1408         struct sw_flow *flow;
1409         struct datapath *dp;
1410         struct flow_table *table;
1411         int err;
1412         int key_len;
1413
1414         if (!a[OVS_FLOW_ATTR_KEY])
1415                 return -EINVAL;
1416         err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1417         if (err)
1418                 return err;
1419
1420         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1421         if (!dp)
1422                 return -ENODEV;
1423
1424         table = genl_dereference(dp->table);
1425         flow = ovs_flow_tbl_lookup(table, &key, key_len);
1426         if (!flow)
1427                 return -ENOENT;
1428
1429         reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1430                                         info->snd_seq, OVS_FLOW_CMD_NEW);
1431         if (IS_ERR(reply))
1432                 return PTR_ERR(reply);
1433
1434         return genlmsg_reply(reply, info);
1435 }
1436
1437 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1438 {
1439         struct nlattr **a = info->attrs;
1440         struct ovs_header *ovs_header = info->userhdr;
1441         struct sw_flow_key key;
1442         struct sk_buff *reply;
1443         struct sw_flow *flow;
1444         struct datapath *dp;
1445         struct flow_table *table;
1446         int err;
1447         int key_len;
1448
1449         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1450         if (!dp)
1451                 return -ENODEV;
1452
1453         if (!a[OVS_FLOW_ATTR_KEY])
1454                 return flush_flows(dp);
1455
1456         err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1457         if (err)
1458                 return err;
1459
1460         table = genl_dereference(dp->table);
1461         flow = ovs_flow_tbl_lookup(table, &key, key_len);
1462         if (!flow)
1463                 return -ENOENT;
1464
1465         reply = ovs_flow_cmd_alloc_info(flow);
1466         if (!reply)
1467                 return -ENOMEM;
1468
1469         ovs_flow_tbl_remove(table, flow);
1470
1471         err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
1472                                      info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1473         BUG_ON(err < 0);
1474
1475         ovs_flow_deferred_free(flow);
1476
1477         genl_notify(reply, genl_info_net(info), info->snd_portid,
1478                     ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1479         return 0;
1480 }
1481
1482 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1483 {
1484         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1485         struct datapath *dp;
1486         struct flow_table *table;
1487
1488         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1489         if (!dp)
1490                 return -ENODEV;
1491
1492         table = genl_dereference(dp->table);
1493
1494         for (;;) {
1495                 struct sw_flow *flow;
1496                 u32 bucket, obj;
1497
1498                 bucket = cb->args[0];
1499                 obj = cb->args[1];
1500                 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1501                 if (!flow)
1502                         break;
1503
1504                 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1505                                            NETLINK_CB(cb->skb).portid,
1506                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
1507                                            OVS_FLOW_CMD_NEW) < 0)
1508                         break;
1509
1510                 cb->args[0] = bucket;
1511                 cb->args[1] = obj;
1512         }
1513         return skb->len;
1514 }
1515
1516 static struct genl_ops dp_flow_genl_ops[] = {
1517         { .cmd = OVS_FLOW_CMD_NEW,
1518           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1519           .policy = flow_policy,
1520           .doit = ovs_flow_cmd_new_or_set
1521         },
1522         { .cmd = OVS_FLOW_CMD_DEL,
1523           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1524           .policy = flow_policy,
1525           .doit = ovs_flow_cmd_del
1526         },
1527         { .cmd = OVS_FLOW_CMD_GET,
1528           .flags = 0,               /* OK for unprivileged users. */
1529           .policy = flow_policy,
1530           .doit = ovs_flow_cmd_get,
1531           .dumpit = ovs_flow_cmd_dump
1532         },
1533         { .cmd = OVS_FLOW_CMD_SET,
1534           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1535           .policy = flow_policy,
1536           .doit = ovs_flow_cmd_new_or_set,
1537         },
1538 };
1539
1540 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1541 #ifdef HAVE_NLA_NUL_STRING
1542         [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1543 #endif
1544         [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1545 };
1546
1547 static struct genl_family dp_datapath_genl_family = {
1548         .id = GENL_ID_GENERATE,
1549         .hdrsize = sizeof(struct ovs_header),
1550         .name = OVS_DATAPATH_FAMILY,
1551         .version = OVS_DATAPATH_VERSION,
1552         .maxattr = OVS_DP_ATTR_MAX,
1553          SET_NETNSOK
1554 };
1555
1556 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1557         .name = OVS_DATAPATH_MCGROUP
1558 };
1559
1560 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1561                                 u32 portid, u32 seq, u32 flags, u8 cmd)
1562 {
1563         struct ovs_header *ovs_header;
1564         struct ovs_dp_stats dp_stats;
1565         int err;
1566
1567         ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1568                                    flags, cmd);
1569         if (!ovs_header)
1570                 goto error;
1571
1572         ovs_header->dp_ifindex = get_dpifindex(dp);
1573
1574         rcu_read_lock();
1575         err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1576         rcu_read_unlock();
1577         if (err)
1578                 goto nla_put_failure;
1579
1580         get_dp_stats(dp, &dp_stats);
1581         if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1582                 goto nla_put_failure;
1583
1584         return genlmsg_end(skb, ovs_header);
1585
1586 nla_put_failure:
1587         genlmsg_cancel(skb, ovs_header);
1588 error:
1589         return -EMSGSIZE;
1590 }
1591
1592 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
1593                                              u32 seq, u8 cmd)
1594 {
1595         struct sk_buff *skb;
1596         int retval;
1597
1598         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1599         if (!skb)
1600                 return ERR_PTR(-ENOMEM);
1601
1602         retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
1603         if (retval < 0) {
1604                 kfree_skb(skb);
1605                 return ERR_PTR(retval);
1606         }
1607         return skb;
1608 }
1609
1610 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1611 {
1612         return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1613 }
1614
1615 /* Called with genl_mutex and optionally with RTNL lock also. */
1616 static struct datapath *lookup_datapath(struct net *net,
1617                                         struct ovs_header *ovs_header,
1618                                         struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1619 {
1620         struct datapath *dp;
1621
1622         if (!a[OVS_DP_ATTR_NAME])
1623                 dp = get_dp(net, ovs_header->dp_ifindex);
1624         else {
1625                 struct vport *vport;
1626
1627                 rcu_read_lock();
1628                 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1629                 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1630                 rcu_read_unlock();
1631         }
1632         return dp ? dp : ERR_PTR(-ENODEV);
1633 }
1634
1635 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1636 {
1637         struct nlattr **a = info->attrs;
1638         struct vport_parms parms;
1639         struct sk_buff *reply;
1640         struct datapath *dp;
1641         struct vport *vport;
1642         struct ovs_net *ovs_net;
1643         int err, i;
1644
1645         err = -EINVAL;
1646         if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1647                 goto err;
1648
1649         err = ovs_dp_cmd_validate(a);
1650         if (err)
1651                 goto err;
1652
1653         rtnl_lock();
1654
1655         err = -ENOMEM;
1656         dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1657         if (dp == NULL)
1658                 goto err_unlock_rtnl;
1659
1660         /* Initialize kobject for bridge.  This will be added as
1661          * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1662         dp->ifobj.kset = NULL;
1663         kobject_init(&dp->ifobj, &dp_ktype);
1664
1665         ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1666
1667         /* Allocate table. */
1668         err = -ENOMEM;
1669         rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1670         if (!dp->table)
1671                 goto err_free_dp;
1672
1673         dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1674         if (!dp->stats_percpu) {
1675                 err = -ENOMEM;
1676                 goto err_destroy_table;
1677         }
1678
1679         dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1680                             GFP_KERNEL);
1681         if (!dp->ports) {
1682                 err = -ENOMEM;
1683                 goto err_destroy_percpu;
1684         }
1685
1686         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1687                 INIT_HLIST_HEAD(&dp->ports[i]);
1688
1689         /* Set up our datapath device. */
1690         parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1691         parms.type = OVS_VPORT_TYPE_INTERNAL;
1692         parms.options = NULL;
1693         parms.dp = dp;
1694         parms.port_no = OVSP_LOCAL;
1695         parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1696
1697         vport = new_vport(&parms);
1698         if (IS_ERR(vport)) {
1699                 err = PTR_ERR(vport);
1700                 if (err == -EBUSY)
1701                         err = -EEXIST;
1702
1703                 goto err_destroy_ports_array;
1704         }
1705
1706         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1707                                       info->snd_seq, OVS_DP_CMD_NEW);
1708         err = PTR_ERR(reply);
1709         if (IS_ERR(reply))
1710                 goto err_destroy_local_port;
1711
1712         ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1713         list_add_tail(&dp->list_node, &ovs_net->dps);
1714         ovs_dp_sysfs_add_dp(dp);
1715
1716         rtnl_unlock();
1717
1718         genl_notify(reply, genl_info_net(info), info->snd_portid,
1719                     ovs_dp_datapath_multicast_group.id, info->nlhdr,
1720                     GFP_KERNEL);
1721         return 0;
1722
1723 err_destroy_local_port:
1724         ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1725 err_destroy_ports_array:
1726         kfree(dp->ports);
1727 err_destroy_percpu:
1728         free_percpu(dp->stats_percpu);
1729 err_destroy_table:
1730         ovs_flow_tbl_destroy(genl_dereference(dp->table));
1731 err_free_dp:
1732         release_net(ovs_dp_get_net(dp));
1733         kfree(dp);
1734 err_unlock_rtnl:
1735         rtnl_unlock();
1736 err:
1737         return err;
1738 }
1739
1740 /* Called with genl_mutex. */
1741 static void __dp_destroy(struct datapath *dp)
1742 {
1743         int i;
1744
1745         rtnl_lock();
1746
1747         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1748                 struct vport *vport;
1749                 struct hlist_node *node, *n;
1750
1751                 hlist_for_each_entry_safe(vport, node, n, &dp->ports[i], dp_hash_node)
1752                         if (vport->port_no != OVSP_LOCAL)
1753                                 ovs_dp_detach_port(vport);
1754         }
1755
1756         ovs_dp_sysfs_del_dp(dp);
1757         list_del(&dp->list_node);
1758         ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1759
1760         /* rtnl_unlock() will wait until all the references to devices that
1761          * are pending unregistration have been dropped.  We do it here to
1762          * ensure that any internal devices (which contain DP pointers) are
1763          * fully destroyed before freeing the datapath.
1764          */
1765         rtnl_unlock();
1766
1767         call_rcu(&dp->rcu, destroy_dp_rcu);
1768 }
1769
1770 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1771 {
1772         struct sk_buff *reply;
1773         struct datapath *dp;
1774         int err;
1775
1776         err = ovs_dp_cmd_validate(info->attrs);
1777         if (err)
1778                 return err;
1779
1780         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1781         err = PTR_ERR(dp);
1782         if (IS_ERR(dp))
1783                 return err;
1784
1785         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1786                                       info->snd_seq, OVS_DP_CMD_DEL);
1787         err = PTR_ERR(reply);
1788         if (IS_ERR(reply))
1789                 return err;
1790
1791         __dp_destroy(dp);
1792
1793         genl_notify(reply, genl_info_net(info), info->snd_portid,
1794                     ovs_dp_datapath_multicast_group.id, info->nlhdr,
1795                     GFP_KERNEL);
1796
1797         return 0;
1798 }
1799
1800 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1801 {
1802         struct sk_buff *reply;
1803         struct datapath *dp;
1804         int err;
1805
1806         err = ovs_dp_cmd_validate(info->attrs);
1807         if (err)
1808                 return err;
1809
1810         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1811         if (IS_ERR(dp))
1812                 return PTR_ERR(dp);
1813
1814         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1815                                       info->snd_seq, OVS_DP_CMD_NEW);
1816         if (IS_ERR(reply)) {
1817                 err = PTR_ERR(reply);
1818                 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1819                                 ovs_dp_datapath_multicast_group.id, err);
1820                 return 0;
1821         }
1822
1823         genl_notify(reply, genl_info_net(info), info->snd_portid,
1824                     ovs_dp_datapath_multicast_group.id, info->nlhdr,
1825                     GFP_KERNEL);
1826
1827         return 0;
1828 }
1829
1830 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1831 {
1832         struct sk_buff *reply;
1833         struct datapath *dp;
1834         int err;
1835
1836         err = ovs_dp_cmd_validate(info->attrs);
1837         if (err)
1838                 return err;
1839
1840         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1841         if (IS_ERR(dp))
1842                 return PTR_ERR(dp);
1843
1844         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1845                                       info->snd_seq, OVS_DP_CMD_NEW);
1846         if (IS_ERR(reply))
1847                 return PTR_ERR(reply);
1848
1849         return genlmsg_reply(reply, info);
1850 }
1851
1852 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1853 {
1854         struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1855         struct datapath *dp;
1856         int skip = cb->args[0];
1857         int i = 0;
1858
1859         list_for_each_entry(dp, &ovs_net->dps, list_node) {
1860                 if (i >= skip &&
1861                     ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1862                                          cb->nlh->nlmsg_seq, NLM_F_MULTI,
1863                                          OVS_DP_CMD_NEW) < 0)
1864                         break;
1865                 i++;
1866         }
1867
1868         cb->args[0] = i;
1869
1870         return skb->len;
1871 }
1872
1873 static struct genl_ops dp_datapath_genl_ops[] = {
1874         { .cmd = OVS_DP_CMD_NEW,
1875           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1876           .policy = datapath_policy,
1877           .doit = ovs_dp_cmd_new
1878         },
1879         { .cmd = OVS_DP_CMD_DEL,
1880           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1881           .policy = datapath_policy,
1882           .doit = ovs_dp_cmd_del
1883         },
1884         { .cmd = OVS_DP_CMD_GET,
1885           .flags = 0,               /* OK for unprivileged users. */
1886           .policy = datapath_policy,
1887           .doit = ovs_dp_cmd_get,
1888           .dumpit = ovs_dp_cmd_dump
1889         },
1890         { .cmd = OVS_DP_CMD_SET,
1891           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1892           .policy = datapath_policy,
1893           .doit = ovs_dp_cmd_set,
1894         },
1895 };
1896
1897 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1898 #ifdef HAVE_NLA_NUL_STRING
1899         [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1900         [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1901         [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1902 #else
1903         [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1904         [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1905 #endif
1906         [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1907         [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1908         [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1909         [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1910 };
1911
1912 static struct genl_family dp_vport_genl_family = {
1913         .id = GENL_ID_GENERATE,
1914         .hdrsize = sizeof(struct ovs_header),
1915         .name = OVS_VPORT_FAMILY,
1916         .version = OVS_VPORT_VERSION,
1917         .maxattr = OVS_VPORT_ATTR_MAX,
1918          SET_NETNSOK
1919 };
1920
1921 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1922         .name = OVS_VPORT_MCGROUP
1923 };
1924
1925 /* Called with RTNL lock or RCU read lock. */
1926 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1927                                    u32 portid, u32 seq, u32 flags, u8 cmd)
1928 {
1929         struct ovs_header *ovs_header;
1930         struct ovs_vport_stats vport_stats;
1931         int err;
1932
1933         ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1934                                  flags, cmd);
1935         if (!ovs_header)
1936                 return -EMSGSIZE;
1937
1938         ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1939
1940         if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1941             nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1942             nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1943             nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1944                 goto nla_put_failure;
1945
1946         ovs_vport_get_stats(vport, &vport_stats);
1947         if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1948                     &vport_stats))
1949                 goto nla_put_failure;
1950
1951         if (nla_put(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
1952                     vport->ops->get_addr(vport)))
1953                 goto nla_put_failure;
1954
1955         err = ovs_vport_get_options(vport, skb);
1956         if (err == -EMSGSIZE)
1957                 goto error;
1958
1959         return genlmsg_end(skb, ovs_header);
1960
1961 nla_put_failure:
1962         err = -EMSGSIZE;
1963 error:
1964         genlmsg_cancel(skb, ovs_header);
1965         return err;
1966 }
1967
1968 /* Called with RTNL lock or RCU read lock. */
1969 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1970                                          u32 seq, u8 cmd)
1971 {
1972         struct sk_buff *skb;
1973         int retval;
1974
1975         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1976         if (!skb)
1977                 return ERR_PTR(-ENOMEM);
1978
1979         retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1980         if (retval < 0) {
1981                 kfree_skb(skb);
1982                 return ERR_PTR(retval);
1983         }
1984         return skb;
1985 }
1986
1987 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1988 {
1989         return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1990 }
1991
1992 /* Called with RTNL lock or RCU read lock. */
1993 static struct vport *lookup_vport(struct net *net,
1994                                   struct ovs_header *ovs_header,
1995                                   struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1996 {
1997         struct datapath *dp;
1998         struct vport *vport;
1999
2000         if (a[OVS_VPORT_ATTR_NAME]) {
2001                 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
2002                 if (!vport)
2003                         return ERR_PTR(-ENODEV);
2004                 if (ovs_header->dp_ifindex &&
2005                     ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2006                         return ERR_PTR(-ENODEV);
2007                 return vport;
2008         } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2009                 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2010
2011                 if (port_no >= DP_MAX_PORTS)
2012                         return ERR_PTR(-EFBIG);
2013
2014                 dp = get_dp(net, ovs_header->dp_ifindex);
2015                 if (!dp)
2016                         return ERR_PTR(-ENODEV);
2017
2018                 vport = ovs_vport_rtnl_rcu(dp, port_no);
2019                 if (!vport)
2020                         return ERR_PTR(-ENOENT);
2021                 return vport;
2022         } else
2023                 return ERR_PTR(-EINVAL);
2024 }
2025
2026 /* Called with RTNL lock. */
2027 static int change_vport(struct vport *vport,
2028                         struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2029 {
2030         int err = 0;
2031
2032         if (a[OVS_VPORT_ATTR_STATS])
2033                 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2034
2035         if (a[OVS_VPORT_ATTR_ADDRESS])
2036                 err = ovs_vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
2037
2038         return err;
2039 }
2040
2041 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2042 {
2043         struct nlattr **a = info->attrs;
2044         struct ovs_header *ovs_header = info->userhdr;
2045         struct vport_parms parms;
2046         struct sk_buff *reply;
2047         struct vport *vport;
2048         struct datapath *dp;
2049         u32 port_no;
2050         int err;
2051
2052         err = -EINVAL;
2053         if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2054             !a[OVS_VPORT_ATTR_UPCALL_PID])
2055                 goto exit;
2056
2057         err = ovs_vport_cmd_validate(a);
2058         if (err)
2059                 goto exit;
2060
2061         rtnl_lock();
2062         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2063         err = -ENODEV;
2064         if (!dp)
2065                 goto exit_unlock;
2066
2067         if (a[OVS_VPORT_ATTR_PORT_NO]) {
2068                 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2069
2070                 err = -EFBIG;
2071                 if (port_no >= DP_MAX_PORTS)
2072                         goto exit_unlock;
2073
2074                 vport = ovs_vport_rtnl(dp, port_no);
2075                 err = -EBUSY;
2076                 if (vport)
2077                         goto exit_unlock;
2078         } else {
2079                 for (port_no = 1; ; port_no++) {
2080                         if (port_no >= DP_MAX_PORTS) {
2081                                 err = -EFBIG;
2082                                 goto exit_unlock;
2083                         }
2084                         vport = ovs_vport_rtnl(dp, port_no);
2085                         if (!vport)
2086                                 break;
2087                 }
2088         }
2089
2090         parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2091         parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2092         parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2093         parms.dp = dp;
2094         parms.port_no = port_no;
2095         parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2096
2097         vport = new_vport(&parms);
2098         err = PTR_ERR(vport);
2099         if (IS_ERR(vport))
2100                 goto exit_unlock;
2101
2102         ovs_dp_sysfs_add_if(vport);
2103
2104         err = change_vport(vport, a);
2105         if (!err) {
2106                 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2107                                                  info->snd_seq,
2108                                                  OVS_VPORT_CMD_NEW);
2109                 if (IS_ERR(reply))
2110                         err = PTR_ERR(reply);
2111         }
2112         if (err) {
2113                 ovs_dp_detach_port(vport);
2114                 goto exit_unlock;
2115         }
2116         genl_notify(reply, genl_info_net(info), info->snd_portid,
2117                     ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
2118
2119 exit_unlock:
2120         rtnl_unlock();
2121 exit:
2122         return err;
2123 }
2124
2125 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2126 {
2127         struct nlattr **a = info->attrs;
2128         struct sk_buff *reply;
2129         struct vport *vport;
2130         int err;
2131
2132         err = ovs_vport_cmd_validate(a);
2133         if (err)
2134                 goto exit;
2135
2136         rtnl_lock();
2137         vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2138         err = PTR_ERR(vport);
2139         if (IS_ERR(vport))
2140                 goto exit_unlock;
2141
2142         err = 0;
2143         if (a[OVS_VPORT_ATTR_TYPE] &&
2144             nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
2145                 err = -EINVAL;
2146
2147         if (!err && a[OVS_VPORT_ATTR_OPTIONS])
2148                 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2149         if (!err)
2150                 err = change_vport(vport, a);
2151         else
2152                 goto exit_unlock;
2153         if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
2154                 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2155
2156         reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2157                                          info->snd_seq, OVS_VPORT_CMD_NEW);
2158         if (IS_ERR(reply)) {
2159                 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
2160                                 ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
2161                 goto exit_unlock;
2162         }
2163
2164         genl_notify(reply, genl_info_net(info), info->snd_portid,
2165                     ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
2166
2167 exit_unlock:
2168         rtnl_unlock();
2169 exit:
2170         return err;
2171 }
2172
2173 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2174 {
2175         struct nlattr **a = info->attrs;
2176         struct sk_buff *reply;
2177         struct vport *vport;
2178         int err;
2179
2180         err = ovs_vport_cmd_validate(a);
2181         if (err)
2182                 goto exit;
2183
2184         rtnl_lock();
2185         vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2186         err = PTR_ERR(vport);
2187         if (IS_ERR(vport))
2188                 goto exit_unlock;
2189
2190         if (vport->port_no == OVSP_LOCAL) {
2191                 err = -EINVAL;
2192                 goto exit_unlock;
2193         }
2194
2195         reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2196                                          info->snd_seq, OVS_VPORT_CMD_DEL);
2197         err = PTR_ERR(reply);
2198         if (IS_ERR(reply))
2199                 goto exit_unlock;
2200
2201         ovs_dp_detach_port(vport);
2202
2203         genl_notify(reply, genl_info_net(info), info->snd_portid,
2204                     ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
2205
2206 exit_unlock:
2207         rtnl_unlock();
2208 exit:
2209         return err;
2210 }
2211
2212 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2213 {
2214         struct nlattr **a = info->attrs;
2215         struct ovs_header *ovs_header = info->userhdr;
2216         struct sk_buff *reply;
2217         struct vport *vport;
2218         int err;
2219
2220         err = ovs_vport_cmd_validate(a);
2221         if (err)
2222                 goto exit;
2223
2224         rcu_read_lock();
2225         vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2226         err = PTR_ERR(vport);
2227         if (IS_ERR(vport))
2228                 goto exit_unlock;
2229
2230         reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2231                                          info->snd_seq, OVS_VPORT_CMD_NEW);
2232         err = PTR_ERR(reply);
2233         if (IS_ERR(reply))
2234                 goto exit_unlock;
2235
2236         rcu_read_unlock();
2237
2238         return genlmsg_reply(reply, info);
2239
2240 exit_unlock:
2241         rcu_read_unlock();
2242 exit:
2243         return err;
2244 }
2245
2246 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2247 {
2248         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2249         struct datapath *dp;
2250         int bucket = cb->args[0], skip = cb->args[1];
2251         int i, j = 0;
2252
2253         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2254         if (!dp)
2255                 return -ENODEV;
2256
2257         rcu_read_lock();
2258         for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2259                 struct vport *vport;
2260                 struct hlist_node *n;
2261
2262                 j = 0;
2263                 hlist_for_each_entry_rcu(vport, n, &dp->ports[i], dp_hash_node) {
2264                         if (j >= skip &&
2265                             ovs_vport_cmd_fill_info(vport, skb,
2266                                                     NETLINK_CB(cb->skb).portid,
2267                                                     cb->nlh->nlmsg_seq,
2268                                                     NLM_F_MULTI,
2269                                                     OVS_VPORT_CMD_NEW) < 0)
2270                                 goto out;
2271
2272                         j++;
2273                 }
2274                 skip = 0;
2275         }
2276 out:
2277         rcu_read_unlock();
2278
2279         cb->args[0] = i;
2280         cb->args[1] = j;
2281
2282         return skb->len;
2283 }
2284
2285 static struct genl_ops dp_vport_genl_ops[] = {
2286         { .cmd = OVS_VPORT_CMD_NEW,
2287           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2288           .policy = vport_policy,
2289           .doit = ovs_vport_cmd_new
2290         },
2291         { .cmd = OVS_VPORT_CMD_DEL,
2292           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2293           .policy = vport_policy,
2294           .doit = ovs_vport_cmd_del
2295         },
2296         { .cmd = OVS_VPORT_CMD_GET,
2297           .flags = 0,               /* OK for unprivileged users. */
2298           .policy = vport_policy,
2299           .doit = ovs_vport_cmd_get,
2300           .dumpit = ovs_vport_cmd_dump
2301         },
2302         { .cmd = OVS_VPORT_CMD_SET,
2303           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2304           .policy = vport_policy,
2305           .doit = ovs_vport_cmd_set,
2306         },
2307 };
2308
2309 struct genl_family_and_ops {
2310         struct genl_family *family;
2311         struct genl_ops *ops;
2312         int n_ops;
2313         struct genl_multicast_group *group;
2314 };
2315
2316 static const struct genl_family_and_ops dp_genl_families[] = {
2317         { &dp_datapath_genl_family,
2318           dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2319           &ovs_dp_datapath_multicast_group },
2320         { &dp_vport_genl_family,
2321           dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2322           &ovs_dp_vport_multicast_group },
2323         { &dp_flow_genl_family,
2324           dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2325           &ovs_dp_flow_multicast_group },
2326         { &dp_packet_genl_family,
2327           dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2328           NULL },
2329 };
2330
2331 static void dp_unregister_genl(int n_families)
2332 {
2333         int i;
2334
2335         for (i = 0; i < n_families; i++)
2336                 genl_unregister_family(dp_genl_families[i].family);
2337 }
2338
2339 static int dp_register_genl(void)
2340 {
2341         int n_registered;
2342         int err;
2343         int i;
2344
2345         n_registered = 0;
2346         for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2347                 const struct genl_family_and_ops *f = &dp_genl_families[i];
2348
2349                 err = genl_register_family_with_ops(f->family, f->ops,
2350                                                     f->n_ops);
2351                 if (err)
2352                         goto error;
2353                 n_registered++;
2354
2355                 if (f->group) {
2356                         err = genl_register_mc_group(f->family, f->group);
2357                         if (err)
2358                                 goto error;
2359                 }
2360         }
2361
2362         return 0;
2363
2364 error:
2365         dp_unregister_genl(n_registered);
2366         return err;
2367 }
2368
2369 static int __rehash_flow_table(void *dummy)
2370 {
2371         struct datapath *dp;
2372         struct net *net;
2373
2374         rtnl_lock();
2375         for_each_net(net) {
2376                 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2377
2378                 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2379                         struct flow_table *old_table = genl_dereference(dp->table);
2380                         struct flow_table *new_table;
2381
2382                         new_table = ovs_flow_tbl_rehash(old_table);
2383                         if (!IS_ERR(new_table)) {
2384                                 rcu_assign_pointer(dp->table, new_table);
2385                                 ovs_flow_tbl_deferred_destroy(old_table);
2386                         }
2387                 }
2388         }
2389         rtnl_unlock();
2390         return 0;
2391 }
2392
2393 static void rehash_flow_table(struct work_struct *work)
2394 {
2395         genl_exec(__rehash_flow_table, NULL);
2396         schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2397 }
2398
2399 static int dp_destroy_all(void *data)
2400 {
2401         struct datapath *dp, *dp_next;
2402         struct ovs_net *ovs_net = data;
2403
2404         list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2405                 __dp_destroy(dp);
2406
2407         return 0;
2408 }
2409
2410 static int __net_init ovs_init_net(struct net *net)
2411 {
2412         struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2413
2414         INIT_LIST_HEAD(&ovs_net->dps);
2415         return 0;
2416 }
2417
2418 static void __net_exit ovs_exit_net(struct net *net)
2419 {
2420         struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2421
2422         genl_exec(dp_destroy_all, ovs_net);
2423 }
2424
2425 static struct pernet_operations ovs_net_ops = {
2426         .init = ovs_init_net,
2427         .exit = ovs_exit_net,
2428         .id   = &ovs_net_id,
2429         .size = sizeof(struct ovs_net),
2430 };
2431
2432 static int __init dp_init(void)
2433 {
2434         struct sk_buff *dummy_skb;
2435         int err;
2436
2437         BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2438
2439         pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2440                 VERSION);
2441
2442         err = genl_exec_init();
2443         if (err)
2444                 goto error;
2445
2446         err = ovs_workqueues_init();
2447         if (err)
2448                 goto error_genl_exec;
2449
2450         err = ovs_tnl_init();
2451         if (err)
2452                 goto error_wq;
2453
2454         err = ovs_flow_init();
2455         if (err)
2456                 goto error_tnl_exit;
2457
2458         err = ovs_vport_init();
2459         if (err)
2460                 goto error_flow_exit;
2461
2462         err = register_pernet_device(&ovs_net_ops);
2463         if (err)
2464                 goto error_vport_exit;
2465
2466         err = register_netdevice_notifier(&ovs_dp_device_notifier);
2467         if (err)
2468                 goto error_netns_exit;
2469
2470         err = dp_register_genl();
2471         if (err < 0)
2472                 goto error_unreg_notifier;
2473
2474         schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2475
2476         return 0;
2477
2478 error_unreg_notifier:
2479         unregister_netdevice_notifier(&ovs_dp_device_notifier);
2480 error_netns_exit:
2481         unregister_pernet_device(&ovs_net_ops);
2482 error_vport_exit:
2483         ovs_vport_exit();
2484 error_flow_exit:
2485         ovs_flow_exit();
2486 error_tnl_exit:
2487         ovs_tnl_exit();
2488 error_wq:
2489         ovs_workqueues_exit();
2490 error_genl_exec:
2491         genl_exec_exit();
2492 error:
2493         return err;
2494 }
2495
2496 static void dp_cleanup(void)
2497 {
2498         cancel_delayed_work_sync(&rehash_flow_wq);
2499         dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2500         unregister_netdevice_notifier(&ovs_dp_device_notifier);
2501         unregister_pernet_device(&ovs_net_ops);
2502         rcu_barrier();
2503         ovs_vport_exit();
2504         ovs_flow_exit();
2505         ovs_tnl_exit();
2506         ovs_workqueues_exit();
2507         genl_exec_exit();
2508 }
2509
2510 module_init(dp_init);
2511 module_exit(dp_cleanup);
2512
2513 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2514 MODULE_LICENSE("GPL");
2515 MODULE_VERSION(VERSION);