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