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