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