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