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