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