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