datapath: Drop support for linux pre-2.6.32 kernel.
[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 "vport-internal_dev.h"
62 #include "vport-netdev.h"
63
64 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32) || \
65     LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
66 #error Kernels before 2.6.32 or after 3.10 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 = netdev_vport_priv(local)->dev->ifindex;
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          SET_PARALLEL_OPS
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 = ovs_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         case OVS_KEY_ATTR_SCTP:
730                 if (flow_key->ip.proto != IPPROTO_SCTP)
731                         return -EINVAL;
732
733                 return validate_tp_port(flow_key);
734
735         default:
736                 return -EINVAL;
737         }
738
739         return 0;
740 }
741
742 static int validate_userspace(const struct nlattr *attr)
743 {
744         static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] =   {
745                 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
746                 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
747         };
748         struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
749         int error;
750
751         error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
752                                  attr, userspace_policy);
753         if (error)
754                 return error;
755
756         if (!a[OVS_USERSPACE_ATTR_PID] ||
757             !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
758                 return -EINVAL;
759
760         return 0;
761 }
762
763 static int copy_action(const struct nlattr *from,
764                       struct sw_flow_actions **sfa)
765 {
766         int totlen = NLA_ALIGN(from->nla_len);
767         struct nlattr *to;
768
769         to = reserve_sfa_size(sfa, from->nla_len);
770         if (IS_ERR(to))
771                 return PTR_ERR(to);
772
773         memcpy(to, from, totlen);
774         return 0;
775 }
776
777 static int validate_and_copy_actions(const struct nlattr *attr,
778                                 const struct sw_flow_key *key,
779                                 int depth,
780                                 struct sw_flow_actions **sfa)
781 {
782         const struct nlattr *a;
783         int rem, err;
784
785         if (depth >= SAMPLE_ACTION_DEPTH)
786                 return -EOVERFLOW;
787
788         nla_for_each_nested(a, attr, rem) {
789                 /* Expected argument lengths, (u32)-1 for variable length. */
790                 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
791                         [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
792                         [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
793                         [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
794                         [OVS_ACTION_ATTR_POP_VLAN] = 0,
795                         [OVS_ACTION_ATTR_SET] = (u32)-1,
796                         [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
797                 };
798                 const struct ovs_action_push_vlan *vlan;
799                 int type = nla_type(a);
800                 bool skip_copy;
801
802                 if (type > OVS_ACTION_ATTR_MAX ||
803                     (action_lens[type] != nla_len(a) &&
804                      action_lens[type] != (u32)-1))
805                         return -EINVAL;
806
807                 skip_copy = false;
808                 switch (type) {
809                 case OVS_ACTION_ATTR_UNSPEC:
810                         return -EINVAL;
811
812                 case OVS_ACTION_ATTR_USERSPACE:
813                         err = validate_userspace(a);
814                         if (err)
815                                 return err;
816                         break;
817
818                 case OVS_ACTION_ATTR_OUTPUT:
819                         if (nla_get_u32(a) >= DP_MAX_PORTS)
820                                 return -EINVAL;
821                         break;
822
823
824                 case OVS_ACTION_ATTR_POP_VLAN:
825                         break;
826
827                 case OVS_ACTION_ATTR_PUSH_VLAN:
828                         vlan = nla_data(a);
829                         if (vlan->vlan_tpid != htons(ETH_P_8021Q))
830                                 return -EINVAL;
831                         if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
832                                 return -EINVAL;
833                         break;
834
835                 case OVS_ACTION_ATTR_SET:
836                         err = validate_set(a, key, sfa, &skip_copy);
837                         if (err)
838                                 return err;
839                         break;
840
841                 case OVS_ACTION_ATTR_SAMPLE:
842                         err = validate_and_copy_sample(a, key, depth, sfa);
843                         if (err)
844                                 return err;
845                         skip_copy = true;
846                         break;
847
848                 default:
849                         return -EINVAL;
850                 }
851                 if (!skip_copy) {
852                         err = copy_action(a, sfa);
853                         if (err)
854                                 return err;
855                 }
856         }
857
858         if (rem > 0)
859                 return -EINVAL;
860
861         return 0;
862 }
863
864 static void clear_stats(struct sw_flow *flow)
865 {
866         flow->used = 0;
867         flow->tcp_flags = 0;
868         flow->packet_count = 0;
869         flow->byte_count = 0;
870 }
871
872 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
873 {
874         struct ovs_header *ovs_header = info->userhdr;
875         struct nlattr **a = info->attrs;
876         struct sw_flow_actions *acts;
877         struct sk_buff *packet;
878         struct sw_flow *flow;
879         struct datapath *dp;
880         struct ethhdr *eth;
881         int len;
882         int err;
883
884         err = -EINVAL;
885         if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
886             !a[OVS_PACKET_ATTR_ACTIONS])
887                 goto err;
888
889         len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
890         packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
891         err = -ENOMEM;
892         if (!packet)
893                 goto err;
894         skb_reserve(packet, NET_IP_ALIGN);
895
896         nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
897
898         skb_reset_mac_header(packet);
899         eth = eth_hdr(packet);
900
901         /* Normally, setting the skb 'protocol' field would be handled by a
902          * call to eth_type_trans(), but it assumes there's a sending
903          * device, which we may not have. */
904         if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
905                 packet->protocol = eth->h_proto;
906         else
907                 packet->protocol = htons(ETH_P_802_2);
908
909         /* Build an sw_flow for sending this packet. */
910         flow = ovs_flow_alloc();
911         err = PTR_ERR(flow);
912         if (IS_ERR(flow))
913                 goto err_kfree_skb;
914
915         err = ovs_flow_extract(packet, -1, &flow->key);
916         if (err)
917                 goto err_flow_free;
918
919         err = ovs_flow_metadata_from_nlattrs(flow, a[OVS_PACKET_ATTR_KEY]);
920         if (err)
921                 goto err_flow_free;
922         acts = ovs_flow_actions_alloc(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
923         err = PTR_ERR(acts);
924         if (IS_ERR(acts))
925                 goto err_flow_free;
926
927         err = validate_and_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0, &acts);
928         rcu_assign_pointer(flow->sf_acts, acts);
929         if (err)
930                 goto err_flow_free;
931
932         OVS_CB(packet)->flow = flow;
933         OVS_CB(packet)->pkt_key = &flow->key;
934         packet->priority = flow->key.phy.priority;
935         skb_set_mark(packet, flow->key.phy.skb_mark);
936
937         rcu_read_lock();
938         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
939         err = -ENODEV;
940         if (!dp)
941                 goto err_unlock;
942
943         local_bh_disable();
944         err = ovs_execute_actions(dp, packet);
945         local_bh_enable();
946         rcu_read_unlock();
947
948         ovs_flow_free(flow, false);
949         return err;
950
951 err_unlock:
952         rcu_read_unlock();
953 err_flow_free:
954         ovs_flow_free(flow, false);
955 err_kfree_skb:
956         kfree_skb(packet);
957 err:
958         return err;
959 }
960
961 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
962 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
963         [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
964 #else
965         [OVS_PACKET_ATTR_PACKET] = { .minlen = ETH_HLEN },
966 #endif
967         [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
968         [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
969 };
970
971 static struct genl_ops dp_packet_genl_ops[] = {
972         { .cmd = OVS_PACKET_CMD_EXECUTE,
973           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
974           .policy = packet_policy,
975           .doit = ovs_packet_cmd_execute
976         }
977 };
978
979 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
980 {
981         struct flow_table *table;
982         int i;
983
984         table = rcu_dereference_check(dp->table, lockdep_ovsl_is_held());
985         stats->n_flows = ovs_flow_tbl_count(table);
986
987         stats->n_hit = stats->n_missed = stats->n_lost = 0;
988         for_each_possible_cpu(i) {
989                 const struct dp_stats_percpu *percpu_stats;
990                 struct dp_stats_percpu local_stats;
991                 unsigned int start;
992
993                 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
994
995                 do {
996                         start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
997                         local_stats = *percpu_stats;
998                 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
999
1000                 stats->n_hit += local_stats.n_hit;
1001                 stats->n_missed += local_stats.n_missed;
1002                 stats->n_lost += local_stats.n_lost;
1003         }
1004 }
1005
1006 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1007         [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1008         [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1009         [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1010 };
1011
1012 static struct genl_family dp_flow_genl_family = {
1013         .id = GENL_ID_GENERATE,
1014         .hdrsize = sizeof(struct ovs_header),
1015         .name = OVS_FLOW_FAMILY,
1016         .version = OVS_FLOW_VERSION,
1017         .maxattr = OVS_FLOW_ATTR_MAX,
1018          SET_NETNSOK
1019          SET_PARALLEL_OPS
1020 };
1021
1022 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
1023         .name = OVS_FLOW_MCGROUP
1024 };
1025
1026 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
1027 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1028 {
1029         const struct nlattr *a;
1030         struct nlattr *start;
1031         int err = 0, rem;
1032
1033         start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1034         if (!start)
1035                 return -EMSGSIZE;
1036
1037         nla_for_each_nested(a, attr, rem) {
1038                 int type = nla_type(a);
1039                 struct nlattr *st_sample;
1040
1041                 switch (type) {
1042                 case OVS_SAMPLE_ATTR_PROBABILITY:
1043                         if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
1044                                 return -EMSGSIZE;
1045                         break;
1046                 case OVS_SAMPLE_ATTR_ACTIONS:
1047                         st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1048                         if (!st_sample)
1049                                 return -EMSGSIZE;
1050                         err = actions_to_attr(nla_data(a), nla_len(a), skb);
1051                         if (err)
1052                                 return err;
1053                         nla_nest_end(skb, st_sample);
1054                         break;
1055                 }
1056         }
1057
1058         nla_nest_end(skb, start);
1059         return err;
1060 }
1061
1062 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1063 {
1064         const struct nlattr *ovs_key = nla_data(a);
1065         int key_type = nla_type(ovs_key);
1066         struct nlattr *start;
1067         int err;
1068
1069         switch (key_type) {
1070         case OVS_KEY_ATTR_IPV4_TUNNEL:
1071                 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1072                 if (!start)
1073                         return -EMSGSIZE;
1074
1075                 err = ovs_ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
1076                                              nla_data(ovs_key));
1077                 if (err)
1078                         return err;
1079                 nla_nest_end(skb, start);
1080                 break;
1081         default:
1082                 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1083                         return -EMSGSIZE;
1084                 break;
1085         }
1086
1087         return 0;
1088 }
1089
1090 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
1091 {
1092         const struct nlattr *a;
1093         int rem, err;
1094
1095         nla_for_each_attr(a, attr, len, rem) {
1096                 int type = nla_type(a);
1097
1098                 switch (type) {
1099                 case OVS_ACTION_ATTR_SET:
1100                         err = set_action_to_attr(a, skb);
1101                         if (err)
1102                                 return err;
1103                         break;
1104
1105                 case OVS_ACTION_ATTR_SAMPLE:
1106                         err = sample_action_to_attr(a, skb);
1107                         if (err)
1108                                 return err;
1109                         break;
1110                 default:
1111                         if (nla_put(skb, type, nla_len(a), nla_data(a)))
1112                                 return -EMSGSIZE;
1113                         break;
1114                 }
1115         }
1116
1117         return 0;
1118 }
1119
1120 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
1121 {
1122         return NLMSG_ALIGN(sizeof(struct ovs_header))
1123                 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
1124                 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
1125                 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
1126                 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
1127                 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
1128                 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
1129 }
1130
1131 /* Called with ovs_mutex. */
1132 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
1133                                   struct sk_buff *skb, u32 portid,
1134                                   u32 seq, u32 flags, u8 cmd)
1135 {
1136         const int skb_orig_len = skb->len;
1137         struct nlattr *start;
1138         struct ovs_flow_stats stats;
1139         struct ovs_header *ovs_header;
1140         struct nlattr *nla;
1141         unsigned long used;
1142         u8 tcp_flags;
1143         int err;
1144
1145         ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
1146         if (!ovs_header)
1147                 return -EMSGSIZE;
1148
1149         ovs_header->dp_ifindex = get_dpifindex(dp);
1150
1151         /* Fill flow key. */
1152         nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
1153         if (!nla)
1154                 goto nla_put_failure;
1155
1156         err = ovs_flow_to_nlattrs(&flow->unmasked_key,
1157                         &flow->unmasked_key, skb);
1158         if (err)
1159                 goto error;
1160         nla_nest_end(skb, nla);
1161
1162         nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
1163         if (!nla)
1164                 goto nla_put_failure;
1165
1166         err = ovs_flow_to_nlattrs(&flow->key, &flow->mask->key, skb);
1167         if (err)
1168                 goto error;
1169
1170         nla_nest_end(skb, nla);
1171
1172         spin_lock_bh(&flow->lock);
1173         used = flow->used;
1174         stats.n_packets = flow->packet_count;
1175         stats.n_bytes = flow->byte_count;
1176         tcp_flags = flow->tcp_flags;
1177         spin_unlock_bh(&flow->lock);
1178
1179         if (used &&
1180             nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
1181                 goto nla_put_failure;
1182
1183         if (stats.n_packets &&
1184             nla_put(skb, OVS_FLOW_ATTR_STATS,
1185                     sizeof(struct ovs_flow_stats), &stats))
1186                 goto nla_put_failure;
1187
1188         if (tcp_flags &&
1189             nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
1190                 goto nla_put_failure;
1191
1192         /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
1193          * this is the first flow to be dumped into 'skb'.  This is unusual for
1194          * Netlink but individual action lists can be longer than
1195          * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
1196          * The userspace caller can always fetch the actions separately if it
1197          * really wants them.  (Most userspace callers in fact don't care.)
1198          *
1199          * This can only fail for dump operations because the skb is always
1200          * properly sized for single flows.
1201          */
1202         start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
1203         if (start) {
1204                 const struct sw_flow_actions *sf_acts;
1205
1206                 sf_acts = rcu_dereference_check(flow->sf_acts,
1207                                                 lockdep_ovsl_is_held());
1208
1209                 err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
1210                 if (!err)
1211                         nla_nest_end(skb, start);
1212                 else {
1213                         if (skb_orig_len)
1214                                 goto error;
1215
1216                         nla_nest_cancel(skb, start);
1217                 }
1218         } else if (skb_orig_len)
1219                 goto nla_put_failure;
1220
1221         return genlmsg_end(skb, ovs_header);
1222
1223 nla_put_failure:
1224         err = -EMSGSIZE;
1225 error:
1226         genlmsg_cancel(skb, ovs_header);
1227         return err;
1228 }
1229
1230 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
1231 {
1232         const struct sw_flow_actions *sf_acts;
1233
1234         sf_acts = ovsl_dereference(flow->sf_acts);
1235
1236         return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
1237 }
1238
1239 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
1240                                                struct datapath *dp,
1241                                                u32 portid, u32 seq, u8 cmd)
1242 {
1243         struct sk_buff *skb;
1244         int retval;
1245
1246         skb = ovs_flow_cmd_alloc_info(flow);
1247         if (!skb)
1248                 return ERR_PTR(-ENOMEM);
1249
1250         retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
1251         BUG_ON(retval < 0);
1252         return skb;
1253 }
1254
1255 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1256 {
1257         struct nlattr **a = info->attrs;
1258         struct ovs_header *ovs_header = info->userhdr;
1259         struct sw_flow_key key, masked_key;
1260         struct sw_flow *flow = NULL;
1261         struct sw_flow_mask mask;
1262         struct sk_buff *reply;
1263         struct datapath *dp;
1264         struct flow_table *table;
1265         struct sw_flow_actions *acts = NULL;
1266         struct sw_flow_match match;
1267         int error;
1268
1269         /* Extract key. */
1270         error = -EINVAL;
1271         if (!a[OVS_FLOW_ATTR_KEY])
1272                 goto error;
1273
1274         ovs_match_init(&match, &key, &mask);
1275         error = ovs_match_from_nlattrs(&match,
1276                         a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
1277         if (error)
1278                 goto error;
1279
1280         /* Validate actions. */
1281         if (a[OVS_FLOW_ATTR_ACTIONS]) {
1282                 acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
1283                 error = PTR_ERR(acts);
1284                 if (IS_ERR(acts))
1285                         goto error;
1286
1287                 ovs_flow_key_mask(&masked_key, &key, &mask);
1288                 error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
1289                                                   &masked_key, 0, &acts);
1290                 if (error) {
1291                         OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
1292                         goto err_kfree;
1293                 }
1294         } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1295                 error = -EINVAL;
1296                 goto error;
1297         }
1298
1299         ovs_lock();
1300         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1301         error = -ENODEV;
1302         if (!dp)
1303                 goto err_unlock_ovs;
1304
1305         table = ovsl_dereference(dp->table);
1306
1307         /* Check if this is a duplicate flow */
1308         flow = ovs_flow_lookup(table, &key);
1309         if (!flow) {
1310                 struct sw_flow_mask *mask_p;
1311                 /* Bail out if we're not allowed to create a new flow. */
1312                 error = -ENOENT;
1313                 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1314                         goto err_unlock_ovs;
1315
1316                 /* Expand table, if necessary, to make room. */
1317                 if (ovs_flow_tbl_need_to_expand(table)) {
1318                         struct flow_table *new_table;
1319
1320                         new_table = ovs_flow_tbl_expand(table);
1321                         if (!IS_ERR(new_table)) {
1322                                 rcu_assign_pointer(dp->table, new_table);
1323                                 ovs_flow_tbl_destroy(table, true);
1324                                 table = ovsl_dereference(dp->table);
1325                         }
1326                 }
1327
1328                 /* Allocate flow. */
1329                 flow = ovs_flow_alloc();
1330                 if (IS_ERR(flow)) {
1331                         error = PTR_ERR(flow);
1332                         goto err_unlock_ovs;
1333                 }
1334                 clear_stats(flow);
1335
1336                 flow->key = masked_key;
1337                 flow->unmasked_key = key;
1338
1339                 /* Make sure mask is unique in the system */
1340                 mask_p = ovs_sw_flow_mask_find(table, &mask);
1341                 if (!mask_p) {
1342                         /* Allocate a new mask if none exsits. */
1343                         mask_p = ovs_sw_flow_mask_alloc();
1344                         if (!mask_p)
1345                                 goto err_flow_free;
1346                         mask_p->key = mask.key;
1347                         mask_p->range = mask.range;
1348                         ovs_sw_flow_mask_insert(table, mask_p);
1349                 }
1350
1351                 ovs_sw_flow_mask_add_ref(mask_p);
1352                 flow->mask = mask_p;
1353                 rcu_assign_pointer(flow->sf_acts, acts);
1354
1355                 /* Put flow in bucket. */
1356                 ovs_flow_insert(table, flow);
1357
1358                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1359                                                 info->snd_seq, OVS_FLOW_CMD_NEW);
1360         } else {
1361                 /* We found a matching flow. */
1362                 struct sw_flow_actions *old_acts;
1363
1364                 /* Bail out if we're not allowed to modify an existing flow.
1365                  * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1366                  * because Generic Netlink treats the latter as a dump
1367                  * request.  We also accept NLM_F_EXCL in case that bug ever
1368                  * gets fixed.
1369                  */
1370                 error = -EEXIST;
1371                 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1372                     info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1373                         goto err_unlock_ovs;
1374
1375                 /* The unmasked key has to be the same for flow updates. */
1376                 error = -EINVAL;
1377                 if (!ovs_flow_cmp_unmasked_key(flow, &key, match.range.end)) {
1378                         OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
1379                         goto err_unlock_ovs;
1380                 }
1381
1382                 /* Update actions. */
1383                 old_acts = ovsl_dereference(flow->sf_acts);
1384                 rcu_assign_pointer(flow->sf_acts, acts);
1385                 ovs_flow_deferred_free_acts(old_acts);
1386
1387                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1388                                                info->snd_seq, OVS_FLOW_CMD_NEW);
1389
1390                 /* Clear stats. */
1391                 if (a[OVS_FLOW_ATTR_CLEAR]) {
1392                         spin_lock_bh(&flow->lock);
1393                         clear_stats(flow);
1394                         spin_unlock_bh(&flow->lock);
1395                 }
1396         }
1397         ovs_unlock();
1398
1399         if (!IS_ERR(reply))
1400                 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1401         else
1402                 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1403                                 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1404         return 0;
1405
1406 err_flow_free:
1407         ovs_flow_free(flow, false);
1408 err_unlock_ovs:
1409         ovs_unlock();
1410 err_kfree:
1411         kfree(acts);
1412 error:
1413         return error;
1414 }
1415
1416 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1417 {
1418         struct nlattr **a = info->attrs;
1419         struct ovs_header *ovs_header = info->userhdr;
1420         struct sw_flow_key key;
1421         struct sk_buff *reply;
1422         struct sw_flow *flow;
1423         struct datapath *dp;
1424         struct flow_table *table;
1425         struct sw_flow_match match;
1426         int err;
1427
1428         if (!a[OVS_FLOW_ATTR_KEY]) {
1429                 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1430                 return -EINVAL;
1431         }
1432
1433         ovs_match_init(&match, &key, NULL);
1434         err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1435         if (err)
1436                 return err;
1437
1438         ovs_lock();
1439         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1440         if (!dp) {
1441                 err = -ENODEV;
1442                 goto unlock;
1443         }
1444
1445         table = ovsl_dereference(dp->table);
1446         flow = ovs_flow_lookup_unmasked_key(table, &match);
1447         if (!flow) {
1448                 err = -ENOENT;
1449                 goto unlock;
1450         }
1451
1452         reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1453                                         info->snd_seq, OVS_FLOW_CMD_NEW);
1454         if (IS_ERR(reply)) {
1455                 err = PTR_ERR(reply);
1456                 goto unlock;
1457         }
1458
1459         ovs_unlock();
1460         return genlmsg_reply(reply, info);
1461 unlock:
1462         ovs_unlock();
1463         return err;
1464 }
1465
1466 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1467 {
1468         struct nlattr **a = info->attrs;
1469         struct ovs_header *ovs_header = info->userhdr;
1470         struct sw_flow_key key;
1471         struct sk_buff *reply;
1472         struct sw_flow *flow;
1473         struct datapath *dp;
1474         struct flow_table *table;
1475         struct sw_flow_match match;
1476         int err;
1477
1478         ovs_lock();
1479         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1480         if (!dp) {
1481                 err = -ENODEV;
1482                 goto unlock;
1483         }
1484
1485         if (!a[OVS_FLOW_ATTR_KEY]) {
1486                 err = flush_flows(dp);
1487                 goto unlock;
1488         }
1489
1490         ovs_match_init(&match, &key, NULL);
1491         err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1492         if (err)
1493                 goto unlock;
1494
1495         table = ovsl_dereference(dp->table);
1496         flow = ovs_flow_lookup_unmasked_key(table, &match);
1497         if (!flow) {
1498                 err = -ENOENT;
1499                 goto unlock;
1500         }
1501
1502         reply = ovs_flow_cmd_alloc_info(flow);
1503         if (!reply) {
1504                 err = -ENOMEM;
1505                 goto unlock;
1506         }
1507
1508         ovs_flow_remove(table, flow);
1509
1510         err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
1511                                      info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1512         BUG_ON(err < 0);
1513
1514         ovs_flow_free(flow, true);
1515         ovs_unlock();
1516
1517         ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1518         return 0;
1519 unlock:
1520         ovs_unlock();
1521         return err;
1522 }
1523
1524 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1525 {
1526         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1527         struct datapath *dp;
1528         struct flow_table *table;
1529
1530         rcu_read_lock();
1531         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1532         if (!dp) {
1533                 rcu_read_unlock();
1534                 return -ENODEV;
1535         }
1536
1537         table = rcu_dereference(dp->table);
1538         for (;;) {
1539                 struct sw_flow *flow;
1540                 u32 bucket, obj;
1541
1542                 bucket = cb->args[0];
1543                 obj = cb->args[1];
1544                 flow = ovs_flow_dump_next(table, &bucket, &obj);
1545                 if (!flow)
1546                         break;
1547
1548                 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1549                                            NETLINK_CB(cb->skb).portid,
1550                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
1551                                            OVS_FLOW_CMD_NEW) < 0)
1552                         break;
1553
1554                 cb->args[0] = bucket;
1555                 cb->args[1] = obj;
1556         }
1557         rcu_read_unlock();
1558         return skb->len;
1559 }
1560
1561 static struct genl_ops dp_flow_genl_ops[] = {
1562         { .cmd = OVS_FLOW_CMD_NEW,
1563           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1564           .policy = flow_policy,
1565           .doit = ovs_flow_cmd_new_or_set
1566         },
1567         { .cmd = OVS_FLOW_CMD_DEL,
1568           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1569           .policy = flow_policy,
1570           .doit = ovs_flow_cmd_del
1571         },
1572         { .cmd = OVS_FLOW_CMD_GET,
1573           .flags = 0,               /* OK for unprivileged users. */
1574           .policy = flow_policy,
1575           .doit = ovs_flow_cmd_get,
1576           .dumpit = ovs_flow_cmd_dump
1577         },
1578         { .cmd = OVS_FLOW_CMD_SET,
1579           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1580           .policy = flow_policy,
1581           .doit = ovs_flow_cmd_new_or_set,
1582         },
1583 };
1584
1585 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1586 #ifdef HAVE_NLA_NUL_STRING
1587         [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1588 #endif
1589         [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1590 };
1591
1592 static struct genl_family dp_datapath_genl_family = {
1593         .id = GENL_ID_GENERATE,
1594         .hdrsize = sizeof(struct ovs_header),
1595         .name = OVS_DATAPATH_FAMILY,
1596         .version = OVS_DATAPATH_VERSION,
1597         .maxattr = OVS_DP_ATTR_MAX,
1598          SET_NETNSOK
1599          SET_PARALLEL_OPS
1600 };
1601
1602 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1603         .name = OVS_DATAPATH_MCGROUP
1604 };
1605
1606 static size_t ovs_dp_cmd_msg_size(void)
1607 {
1608         size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1609
1610         msgsize += nla_total_size(IFNAMSIZ);
1611         msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1612
1613         return msgsize;
1614 }
1615
1616 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1617                                 u32 portid, u32 seq, u32 flags, u8 cmd)
1618 {
1619         struct ovs_header *ovs_header;
1620         struct ovs_dp_stats dp_stats;
1621         int err;
1622
1623         ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1624                                    flags, cmd);
1625         if (!ovs_header)
1626                 goto error;
1627
1628         ovs_header->dp_ifindex = get_dpifindex(dp);
1629
1630         rcu_read_lock();
1631         err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1632         rcu_read_unlock();
1633         if (err)
1634                 goto nla_put_failure;
1635
1636         get_dp_stats(dp, &dp_stats);
1637         if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1638                 goto nla_put_failure;
1639
1640         return genlmsg_end(skb, ovs_header);
1641
1642 nla_put_failure:
1643         genlmsg_cancel(skb, ovs_header);
1644 error:
1645         return -EMSGSIZE;
1646 }
1647
1648 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
1649                                              u32 seq, u8 cmd)
1650 {
1651         struct sk_buff *skb;
1652         int retval;
1653
1654         skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1655         if (!skb)
1656                 return ERR_PTR(-ENOMEM);
1657
1658         retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
1659         if (retval < 0) {
1660                 kfree_skb(skb);
1661                 return ERR_PTR(retval);
1662         }
1663         return skb;
1664 }
1665
1666 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1667 {
1668         return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1669 }
1670
1671 /* Called with ovs_mutex. */
1672 static struct datapath *lookup_datapath(struct net *net,
1673                                         struct ovs_header *ovs_header,
1674                                         struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1675 {
1676         struct datapath *dp;
1677
1678         if (!a[OVS_DP_ATTR_NAME])
1679                 dp = get_dp(net, ovs_header->dp_ifindex);
1680         else {
1681                 struct vport *vport;
1682
1683                 rcu_read_lock();
1684                 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1685                 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1686                 rcu_read_unlock();
1687         }
1688         return dp ? dp : ERR_PTR(-ENODEV);
1689 }
1690
1691 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1692 {
1693         struct nlattr **a = info->attrs;
1694         struct vport_parms parms;
1695         struct sk_buff *reply;
1696         struct datapath *dp;
1697         struct vport *vport;
1698         struct ovs_net *ovs_net;
1699         int err, i;
1700
1701         err = -EINVAL;
1702         if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1703                 goto err;
1704
1705         err = ovs_dp_cmd_validate(a);
1706         if (err)
1707                 goto err;
1708
1709         ovs_lock();
1710
1711         err = -ENOMEM;
1712         dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1713         if (dp == NULL)
1714                 goto err_unlock_ovs;
1715
1716         ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1717
1718         /* Allocate table. */
1719         err = -ENOMEM;
1720         rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1721         if (!dp->table)
1722                 goto err_free_dp;
1723
1724         dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1725         if (!dp->stats_percpu) {
1726                 err = -ENOMEM;
1727                 goto err_destroy_table;
1728         }
1729
1730         dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1731                             GFP_KERNEL);
1732         if (!dp->ports) {
1733                 err = -ENOMEM;
1734                 goto err_destroy_percpu;
1735         }
1736
1737         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1738                 INIT_HLIST_HEAD(&dp->ports[i]);
1739
1740         /* Set up our datapath device. */
1741         parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1742         parms.type = OVS_VPORT_TYPE_INTERNAL;
1743         parms.options = NULL;
1744         parms.dp = dp;
1745         parms.port_no = OVSP_LOCAL;
1746         parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1747
1748         vport = new_vport(&parms);
1749         if (IS_ERR(vport)) {
1750                 err = PTR_ERR(vport);
1751                 if (err == -EBUSY)
1752                         err = -EEXIST;
1753
1754                 goto err_destroy_ports_array;
1755         }
1756
1757         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1758                                       info->snd_seq, OVS_DP_CMD_NEW);
1759         err = PTR_ERR(reply);
1760         if (IS_ERR(reply))
1761                 goto err_destroy_local_port;
1762
1763         ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1764         list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1765
1766         ovs_unlock();
1767
1768         ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1769         return 0;
1770
1771 err_destroy_local_port:
1772         ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1773 err_destroy_ports_array:
1774         kfree(dp->ports);
1775 err_destroy_percpu:
1776         free_percpu(dp->stats_percpu);
1777 err_destroy_table:
1778         ovs_flow_tbl_destroy(ovsl_dereference(dp->table), false);
1779 err_free_dp:
1780         release_net(ovs_dp_get_net(dp));
1781         kfree(dp);
1782 err_unlock_ovs:
1783         ovs_unlock();
1784 err:
1785         return err;
1786 }
1787
1788 /* Called with ovs_mutex. */
1789 static void __dp_destroy(struct datapath *dp)
1790 {
1791         int i;
1792
1793         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1794                 struct vport *vport;
1795                 struct hlist_node *n;
1796
1797                 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1798                         if (vport->port_no != OVSP_LOCAL)
1799                                 ovs_dp_detach_port(vport);
1800         }
1801
1802         list_del_rcu(&dp->list_node);
1803
1804         /* OVSP_LOCAL is datapath internal port. We need to make sure that
1805          * all port in datapath are destroyed first before freeing datapath.
1806          */
1807         ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1808
1809         call_rcu(&dp->rcu, destroy_dp_rcu);
1810 }
1811
1812 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1813 {
1814         struct sk_buff *reply;
1815         struct datapath *dp;
1816         int err;
1817
1818         err = ovs_dp_cmd_validate(info->attrs);
1819         if (err)
1820                 return err;
1821
1822         ovs_lock();
1823         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1824         err = PTR_ERR(dp);
1825         if (IS_ERR(dp))
1826                 goto unlock;
1827
1828         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1829                                       info->snd_seq, OVS_DP_CMD_DEL);
1830         err = PTR_ERR(reply);
1831         if (IS_ERR(reply))
1832                 goto unlock;
1833
1834         __dp_destroy(dp);
1835         ovs_unlock();
1836
1837         ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1838
1839         return 0;
1840 unlock:
1841         ovs_unlock();
1842         return err;
1843 }
1844
1845 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1846 {
1847         struct sk_buff *reply;
1848         struct datapath *dp;
1849         int err;
1850
1851         err = ovs_dp_cmd_validate(info->attrs);
1852         if (err)
1853                 return err;
1854
1855         ovs_lock();
1856         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1857         err = PTR_ERR(dp);
1858         if (IS_ERR(dp))
1859                 goto unlock;
1860
1861         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1862                                       info->snd_seq, OVS_DP_CMD_NEW);
1863         if (IS_ERR(reply)) {
1864                 err = PTR_ERR(reply);
1865                 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1866                                 ovs_dp_datapath_multicast_group.id, err);
1867                 err = 0;
1868                 goto unlock;
1869         }
1870
1871         ovs_unlock();
1872         ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1873
1874         return 0;
1875 unlock:
1876         ovs_unlock();
1877         return err;
1878 }
1879
1880 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1881 {
1882         struct sk_buff *reply;
1883         struct datapath *dp;
1884         int err;
1885
1886         err = ovs_dp_cmd_validate(info->attrs);
1887         if (err)
1888                 return err;
1889
1890         ovs_lock();
1891         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1892         if (IS_ERR(dp)) {
1893                 err = PTR_ERR(dp);
1894                 goto unlock;
1895         }
1896
1897         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1898                                       info->snd_seq, OVS_DP_CMD_NEW);
1899         if (IS_ERR(reply)) {
1900                 err = PTR_ERR(reply);
1901                 goto unlock;
1902         }
1903
1904         ovs_unlock();
1905         return genlmsg_reply(reply, info);
1906
1907 unlock:
1908         ovs_unlock();
1909         return err;
1910 }
1911
1912 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1913 {
1914         struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1915         struct datapath *dp;
1916         int skip = cb->args[0];
1917         int i = 0;
1918
1919         rcu_read_lock();
1920         list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1921                 if (i >= skip &&
1922                     ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1923                                          cb->nlh->nlmsg_seq, NLM_F_MULTI,
1924                                          OVS_DP_CMD_NEW) < 0)
1925                         break;
1926                 i++;
1927         }
1928         rcu_read_unlock();
1929
1930         cb->args[0] = i;
1931
1932         return skb->len;
1933 }
1934
1935 static struct genl_ops dp_datapath_genl_ops[] = {
1936         { .cmd = OVS_DP_CMD_NEW,
1937           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1938           .policy = datapath_policy,
1939           .doit = ovs_dp_cmd_new
1940         },
1941         { .cmd = OVS_DP_CMD_DEL,
1942           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1943           .policy = datapath_policy,
1944           .doit = ovs_dp_cmd_del
1945         },
1946         { .cmd = OVS_DP_CMD_GET,
1947           .flags = 0,               /* OK for unprivileged users. */
1948           .policy = datapath_policy,
1949           .doit = ovs_dp_cmd_get,
1950           .dumpit = ovs_dp_cmd_dump
1951         },
1952         { .cmd = OVS_DP_CMD_SET,
1953           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1954           .policy = datapath_policy,
1955           .doit = ovs_dp_cmd_set,
1956         },
1957 };
1958
1959 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1960 #ifdef HAVE_NLA_NUL_STRING
1961         [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1962         [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1963 #else
1964         [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1965 #endif
1966         [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1967         [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1968         [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1969         [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1970 };
1971
1972 static struct genl_family dp_vport_genl_family = {
1973         .id = GENL_ID_GENERATE,
1974         .hdrsize = sizeof(struct ovs_header),
1975         .name = OVS_VPORT_FAMILY,
1976         .version = OVS_VPORT_VERSION,
1977         .maxattr = OVS_VPORT_ATTR_MAX,
1978          SET_NETNSOK
1979          SET_PARALLEL_OPS
1980 };
1981
1982 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1983         .name = OVS_VPORT_MCGROUP
1984 };
1985
1986 /* Called with ovs_mutex or RCU read lock. */
1987 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1988                                    u32 portid, u32 seq, u32 flags, u8 cmd)
1989 {
1990         struct ovs_header *ovs_header;
1991         struct ovs_vport_stats vport_stats;
1992         int err;
1993
1994         ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1995                                  flags, cmd);
1996         if (!ovs_header)
1997                 return -EMSGSIZE;
1998
1999         ovs_header->dp_ifindex = get_dpifindex(vport->dp);
2000
2001         if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
2002             nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
2003             nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
2004             nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
2005                 goto nla_put_failure;
2006
2007         ovs_vport_get_stats(vport, &vport_stats);
2008         if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
2009                     &vport_stats))
2010                 goto nla_put_failure;
2011
2012         err = ovs_vport_get_options(vport, skb);
2013         if (err == -EMSGSIZE)
2014                 goto error;
2015
2016         return genlmsg_end(skb, ovs_header);
2017
2018 nla_put_failure:
2019         err = -EMSGSIZE;
2020 error:
2021         genlmsg_cancel(skb, ovs_header);
2022         return err;
2023 }
2024
2025 /* Called with ovs_mutex or RCU read lock. */
2026 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
2027                                          u32 seq, u8 cmd)
2028 {
2029         struct sk_buff *skb;
2030         int retval;
2031
2032         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2033         if (!skb)
2034                 return ERR_PTR(-ENOMEM);
2035
2036         retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
2037         BUG_ON(retval < 0);
2038
2039         return skb;
2040 }
2041
2042 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2043 {
2044         return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
2045 }
2046
2047 /* Called with ovs_mutex or RCU read lock. */
2048 static struct vport *lookup_vport(struct net *net,
2049                                   struct ovs_header *ovs_header,
2050                                   struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2051 {
2052         struct datapath *dp;
2053         struct vport *vport;
2054
2055         if (a[OVS_VPORT_ATTR_NAME]) {
2056                 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
2057                 if (!vport)
2058                         return ERR_PTR(-ENODEV);
2059                 if (ovs_header->dp_ifindex &&
2060                     ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2061                         return ERR_PTR(-ENODEV);
2062                 return vport;
2063         } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2064                 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2065
2066                 if (port_no >= DP_MAX_PORTS)
2067                         return ERR_PTR(-EFBIG);
2068
2069                 dp = get_dp(net, ovs_header->dp_ifindex);
2070                 if (!dp)
2071                         return ERR_PTR(-ENODEV);
2072
2073                 vport = ovs_vport_ovsl_rcu(dp, port_no);
2074                 if (!vport)
2075                         return ERR_PTR(-ENODEV);
2076                 return vport;
2077         } else
2078                 return ERR_PTR(-EINVAL);
2079 }
2080
2081 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2082 {
2083         struct nlattr **a = info->attrs;
2084         struct ovs_header *ovs_header = info->userhdr;
2085         struct vport_parms parms;
2086         struct sk_buff *reply;
2087         struct vport *vport;
2088         struct datapath *dp;
2089         u32 port_no;
2090         int err;
2091
2092         err = -EINVAL;
2093         if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2094             !a[OVS_VPORT_ATTR_UPCALL_PID])
2095                 goto exit;
2096
2097         err = ovs_vport_cmd_validate(a);
2098         if (err)
2099                 goto exit;
2100
2101         ovs_lock();
2102         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2103         err = -ENODEV;
2104         if (!dp)
2105                 goto exit_unlock;
2106
2107         if (a[OVS_VPORT_ATTR_PORT_NO]) {
2108                 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2109
2110                 err = -EFBIG;
2111                 if (port_no >= DP_MAX_PORTS)
2112                         goto exit_unlock;
2113
2114                 vport = ovs_vport_ovsl(dp, port_no);
2115                 err = -EBUSY;
2116                 if (vport)
2117                         goto exit_unlock;
2118         } else {
2119                 for (port_no = 1; ; port_no++) {
2120                         if (port_no >= DP_MAX_PORTS) {
2121                                 err = -EFBIG;
2122                                 goto exit_unlock;
2123                         }
2124                         vport = ovs_vport_ovsl(dp, port_no);
2125                         if (!vport)
2126                                 break;
2127                 }
2128         }
2129
2130         parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2131         parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2132         parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2133         parms.dp = dp;
2134         parms.port_no = port_no;
2135         parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2136
2137         vport = new_vport(&parms);
2138         err = PTR_ERR(vport);
2139         if (IS_ERR(vport))
2140                 goto exit_unlock;
2141
2142         err = 0;
2143         if (a[OVS_VPORT_ATTR_STATS])
2144                 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2145
2146         reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
2147                                          OVS_VPORT_CMD_NEW);
2148         if (IS_ERR(reply)) {
2149                 err = PTR_ERR(reply);
2150                 ovs_dp_detach_port(vport);
2151                 goto exit_unlock;
2152         }
2153
2154         ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2155
2156 exit_unlock:
2157         ovs_unlock();
2158 exit:
2159         return err;
2160 }
2161
2162 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2163 {
2164         struct nlattr **a = info->attrs;
2165         struct sk_buff *reply;
2166         struct vport *vport;
2167         int err;
2168
2169         err = ovs_vport_cmd_validate(a);
2170         if (err)
2171                 goto exit;
2172
2173         ovs_lock();
2174         vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2175         err = PTR_ERR(vport);
2176         if (IS_ERR(vport))
2177                 goto exit_unlock;
2178
2179         if (a[OVS_VPORT_ATTR_TYPE] &&
2180             nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2181                 err = -EINVAL;
2182                 goto exit_unlock;
2183         }
2184
2185         reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2186         if (!reply) {
2187                 err = -ENOMEM;
2188                 goto exit_unlock;
2189         }
2190
2191         if (a[OVS_VPORT_ATTR_OPTIONS]) {
2192                 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2193                 if (err)
2194                         goto exit_free;
2195         }
2196
2197         if (a[OVS_VPORT_ATTR_STATS])
2198                 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2199
2200         if (a[OVS_VPORT_ATTR_UPCALL_PID])
2201                 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2202
2203         err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2204                                       info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2205         BUG_ON(err < 0);
2206
2207         ovs_unlock();
2208         ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2209         return 0;
2210
2211 exit_free:
2212         kfree_skb(reply);
2213 exit_unlock:
2214         ovs_unlock();
2215 exit:
2216         return err;
2217 }
2218
2219 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2220 {
2221         struct nlattr **a = info->attrs;
2222         struct sk_buff *reply;
2223         struct vport *vport;
2224         int err;
2225
2226         err = ovs_vport_cmd_validate(a);
2227         if (err)
2228                 goto exit;
2229
2230         ovs_lock();
2231         vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2232         err = PTR_ERR(vport);
2233         if (IS_ERR(vport))
2234                 goto exit_unlock;
2235
2236         if (vport->port_no == OVSP_LOCAL) {
2237                 err = -EINVAL;
2238                 goto exit_unlock;
2239         }
2240
2241         reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2242                                          info->snd_seq, OVS_VPORT_CMD_DEL);
2243         err = PTR_ERR(reply);
2244         if (IS_ERR(reply))
2245                 goto exit_unlock;
2246
2247         err = 0;
2248         ovs_dp_detach_port(vport);
2249
2250         ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2251
2252 exit_unlock:
2253         ovs_unlock();
2254 exit:
2255         return err;
2256 }
2257
2258 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2259 {
2260         struct nlattr **a = info->attrs;
2261         struct ovs_header *ovs_header = info->userhdr;
2262         struct sk_buff *reply;
2263         struct vport *vport;
2264         int err;
2265
2266         err = ovs_vport_cmd_validate(a);
2267         if (err)
2268                 goto exit;
2269
2270         rcu_read_lock();
2271         vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2272         err = PTR_ERR(vport);
2273         if (IS_ERR(vport))
2274                 goto exit_unlock;
2275
2276         reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2277                                          info->snd_seq, OVS_VPORT_CMD_NEW);
2278         err = PTR_ERR(reply);
2279         if (IS_ERR(reply))
2280                 goto exit_unlock;
2281
2282         rcu_read_unlock();
2283
2284         return genlmsg_reply(reply, info);
2285
2286 exit_unlock:
2287         rcu_read_unlock();
2288 exit:
2289         return err;
2290 }
2291
2292 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2293 {
2294         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2295         struct datapath *dp;
2296         int bucket = cb->args[0], skip = cb->args[1];
2297         int i, j = 0;
2298
2299         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2300         if (!dp)
2301                 return -ENODEV;
2302
2303         rcu_read_lock();
2304         for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2305                 struct vport *vport;
2306
2307                 j = 0;
2308                 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2309                         if (j >= skip &&
2310                             ovs_vport_cmd_fill_info(vport, skb,
2311                                                     NETLINK_CB(cb->skb).portid,
2312                                                     cb->nlh->nlmsg_seq,
2313                                                     NLM_F_MULTI,
2314                                                     OVS_VPORT_CMD_NEW) < 0)
2315                                 goto out;
2316
2317                         j++;
2318                 }
2319                 skip = 0;
2320         }
2321 out:
2322         rcu_read_unlock();
2323
2324         cb->args[0] = i;
2325         cb->args[1] = j;
2326
2327         return skb->len;
2328 }
2329
2330 static struct genl_ops dp_vport_genl_ops[] = {
2331         { .cmd = OVS_VPORT_CMD_NEW,
2332           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2333           .policy = vport_policy,
2334           .doit = ovs_vport_cmd_new
2335         },
2336         { .cmd = OVS_VPORT_CMD_DEL,
2337           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2338           .policy = vport_policy,
2339           .doit = ovs_vport_cmd_del
2340         },
2341         { .cmd = OVS_VPORT_CMD_GET,
2342           .flags = 0,               /* OK for unprivileged users. */
2343           .policy = vport_policy,
2344           .doit = ovs_vport_cmd_get,
2345           .dumpit = ovs_vport_cmd_dump
2346         },
2347         { .cmd = OVS_VPORT_CMD_SET,
2348           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2349           .policy = vport_policy,
2350           .doit = ovs_vport_cmd_set,
2351         },
2352 };
2353
2354 struct genl_family_and_ops {
2355         struct genl_family *family;
2356         struct genl_ops *ops;
2357         int n_ops;
2358         struct genl_multicast_group *group;
2359 };
2360
2361 static const struct genl_family_and_ops dp_genl_families[] = {
2362         { &dp_datapath_genl_family,
2363           dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2364           &ovs_dp_datapath_multicast_group },
2365         { &dp_vport_genl_family,
2366           dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2367           &ovs_dp_vport_multicast_group },
2368         { &dp_flow_genl_family,
2369           dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2370           &ovs_dp_flow_multicast_group },
2371         { &dp_packet_genl_family,
2372           dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2373           NULL },
2374 };
2375
2376 static void dp_unregister_genl(int n_families)
2377 {
2378         int i;
2379
2380         for (i = 0; i < n_families; i++)
2381                 genl_unregister_family(dp_genl_families[i].family);
2382 }
2383
2384 static int dp_register_genl(void)
2385 {
2386         int n_registered;
2387         int err;
2388         int i;
2389
2390         n_registered = 0;
2391         for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2392                 const struct genl_family_and_ops *f = &dp_genl_families[i];
2393
2394                 err = genl_register_family_with_ops(f->family, f->ops,
2395                                                     f->n_ops);
2396                 if (err)
2397                         goto error;
2398                 n_registered++;
2399
2400                 if (f->group) {
2401                         err = genl_register_mc_group(f->family, f->group);
2402                         if (err)
2403                                 goto error;
2404                 }
2405         }
2406
2407         return 0;
2408
2409 error:
2410         dp_unregister_genl(n_registered);
2411         return err;
2412 }
2413
2414 static void rehash_flow_table(struct work_struct *work)
2415 {
2416         struct datapath *dp;
2417         struct net *net;
2418
2419         ovs_lock();
2420         rtnl_lock();
2421         for_each_net(net) {
2422                 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2423
2424                 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2425                         struct flow_table *old_table = ovsl_dereference(dp->table);
2426                         struct flow_table *new_table;
2427
2428                         new_table = ovs_flow_tbl_rehash(old_table);
2429                         if (!IS_ERR(new_table)) {
2430                                 rcu_assign_pointer(dp->table, new_table);
2431                                 ovs_flow_tbl_destroy(old_table, true);
2432                         }
2433                 }
2434         }
2435         rtnl_unlock();
2436         ovs_unlock();
2437         schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2438 }
2439
2440 static int __net_init ovs_init_net(struct net *net)
2441 {
2442         struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2443
2444         INIT_LIST_HEAD(&ovs_net->dps);
2445         INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2446         return 0;
2447 }
2448
2449 static void __net_exit ovs_exit_net(struct net *net)
2450 {
2451         struct datapath *dp, *dp_next;
2452         struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2453
2454         ovs_lock();
2455         list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2456                 __dp_destroy(dp);
2457         ovs_unlock();
2458
2459         cancel_work_sync(&ovs_net->dp_notify_work);
2460 }
2461
2462 static struct pernet_operations ovs_net_ops = {
2463         .init = ovs_init_net,
2464         .exit = ovs_exit_net,
2465         .id   = &ovs_net_id,
2466         .size = sizeof(struct ovs_net),
2467 };
2468
2469 DEFINE_COMPAT_PNET_REG_FUNC(device);
2470
2471 static int __init dp_init(void)
2472 {
2473         int err;
2474
2475         BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2476
2477         pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2478                 VERSION);
2479
2480         err = ovs_workqueues_init();
2481         if (err)
2482                 goto error;
2483
2484         err = ovs_flow_init();
2485         if (err)
2486                 goto error_wq;
2487
2488         err = ovs_vport_init();
2489         if (err)
2490                 goto error_flow_exit;
2491
2492         err = register_pernet_device(&ovs_net_ops);
2493         if (err)
2494                 goto error_vport_exit;
2495
2496         err = register_netdevice_notifier(&ovs_dp_device_notifier);
2497         if (err)
2498                 goto error_netns_exit;
2499
2500         err = dp_register_genl();
2501         if (err < 0)
2502                 goto error_unreg_notifier;
2503
2504         schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2505
2506         return 0;
2507
2508 error_unreg_notifier:
2509         unregister_netdevice_notifier(&ovs_dp_device_notifier);
2510 error_netns_exit:
2511         unregister_pernet_device(&ovs_net_ops);
2512 error_vport_exit:
2513         ovs_vport_exit();
2514 error_flow_exit:
2515         ovs_flow_exit();
2516 error_wq:
2517         ovs_workqueues_exit();
2518 error:
2519         return err;
2520 }
2521
2522 static void dp_cleanup(void)
2523 {
2524         cancel_delayed_work_sync(&rehash_flow_wq);
2525         dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2526         unregister_netdevice_notifier(&ovs_dp_device_notifier);
2527         unregister_pernet_device(&ovs_net_ops);
2528         rcu_barrier();
2529         ovs_vport_exit();
2530         ovs_flow_exit();
2531         ovs_workqueues_exit();
2532 }
2533
2534 module_init(dp_init);
2535 module_exit(dp_cleanup);
2536
2537 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2538 MODULE_LICENSE("GPL");
2539 MODULE_VERSION(VERSION);