Support matching and modifying IP ECN bits.
[sliver-openvswitch.git] / datapath / datapath.c
1 /*
2  * Copyright (c) 2007, 2008, 2009, 2010, 2011 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 /* Functions for managing the dp interface/device. */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/if_arp.h>
16 #include <linux/if_vlan.h>
17 #include <linux/in.h>
18 #include <linux/ip.h>
19 #include <linux/jhash.h>
20 #include <linux/delay.h>
21 #include <linux/time.h>
22 #include <linux/etherdevice.h>
23 #include <linux/genetlink.h>
24 #include <linux/kernel.h>
25 #include <linux/kthread.h>
26 #include <linux/mutex.h>
27 #include <linux/percpu.h>
28 #include <linux/rcupdate.h>
29 #include <linux/tcp.h>
30 #include <linux/udp.h>
31 #include <linux/version.h>
32 #include <linux/ethtool.h>
33 #include <linux/wait.h>
34 #include <asm/system.h>
35 #include <asm/div64.h>
36 #include <linux/highmem.h>
37 #include <linux/netfilter_bridge.h>
38 #include <linux/netfilter_ipv4.h>
39 #include <linux/inetdevice.h>
40 #include <linux/list.h>
41 #include <linux/openvswitch.h>
42 #include <linux/rculist.h>
43 #include <linux/dmi.h>
44 #include <net/genetlink.h>
45
46 #include "checksum.h"
47 #include "datapath.h"
48 #include "actions.h"
49 #include "flow.h"
50 #include "vlan.h"
51 #include "tunnel.h"
52 #include "vport-internal_dev.h"
53
54 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
55     LINUX_VERSION_CODE > KERNEL_VERSION(3,2,0)
56 #error Kernels before 2.6.18 or after 3.2 are not supported by this version of Open vSwitch.
57 #endif
58
59 int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
60 EXPORT_SYMBOL(dp_ioctl_hook);
61
62 /**
63  * DOC: Locking:
64  *
65  * Writes to device state (add/remove datapath, port, set operations on vports,
66  * etc.) are protected by RTNL.
67  *
68  * Writes to other state (flow table modifications, set miscellaneous datapath
69  * parameters, etc.) are protected by genl_mutex.  The RTNL lock nests inside
70  * genl_mutex.
71  *
72  * Reads are protected by RCU.
73  *
74  * There are a few special cases (mostly stats) that have their own
75  * synchronization but they nest under all of above and don't interact with
76  * each other.
77  */
78
79 /* Global list of datapaths to enable dumping them all out.
80  * Protected by genl_mutex.
81  */
82 static LIST_HEAD(dps);
83
84 static struct vport *new_vport(const struct vport_parms *);
85 static int queue_gso_packets(int dp_ifindex, struct sk_buff *,
86                              const struct dp_upcall_info *);
87 static int queue_userspace_packet(int dp_ifindex, struct sk_buff *,
88                                   const struct dp_upcall_info *);
89
90 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
91 struct datapath *get_dp(int dp_ifindex)
92 {
93         struct datapath *dp = NULL;
94         struct net_device *dev;
95
96         rcu_read_lock();
97         dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
98         if (dev) {
99                 struct vport *vport = internal_dev_get_vport(dev);
100                 if (vport)
101                         dp = vport->dp;
102         }
103         rcu_read_unlock();
104
105         return dp;
106 }
107 EXPORT_SYMBOL_GPL(get_dp);
108
109 /* Must be called with genl_mutex. */
110 static struct flow_table *get_table_protected(struct datapath *dp)
111 {
112         return rcu_dereference_protected(dp->table, lockdep_genl_is_held());
113 }
114
115 /* Must be called with rcu_read_lock or RTNL lock. */
116 static struct vport *get_vport_protected(struct datapath *dp, u16 port_no)
117 {
118         return rcu_dereference_rtnl(dp->ports[port_no]);
119 }
120
121 /* Must be called with rcu_read_lock or RTNL lock. */
122 const char *dp_name(const struct datapath *dp)
123 {
124         struct vport *vport = rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]);
125         return vport->ops->get_name(vport);
126 }
127
128 static int get_dpifindex(struct datapath *dp)
129 {
130         struct vport *local;
131         int ifindex;
132
133         rcu_read_lock();
134
135         local = get_vport_protected(dp, OVSP_LOCAL);
136         if (local)
137                 ifindex = local->ops->get_ifindex(local);
138         else
139                 ifindex = 0;
140
141         rcu_read_unlock();
142
143         return ifindex;
144 }
145
146 static size_t br_nlmsg_size(void)
147 {
148         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
149                + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
150                + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
151                + nla_total_size(4) /* IFLA_MASTER */
152                + nla_total_size(4) /* IFLA_MTU */
153                + nla_total_size(1); /* IFLA_OPERSTATE */
154 }
155
156 /* Caller must hold RTNL lock. */
157 static int dp_fill_ifinfo(struct sk_buff *skb,
158                           const struct vport *port,
159                           int event, unsigned int flags)
160 {
161         struct datapath *dp = port->dp;
162         struct ifinfomsg *hdr;
163         struct nlmsghdr *nlh;
164
165         if (!port->ops->get_ifindex)
166                 return -ENODEV;
167
168         nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
169         if (nlh == NULL)
170                 return -EMSGSIZE;
171
172         hdr = nlmsg_data(nlh);
173         hdr->ifi_family = AF_BRIDGE;
174         hdr->__ifi_pad = 0;
175         hdr->ifi_type = ARPHRD_ETHER;
176         hdr->ifi_index = port->ops->get_ifindex(port);
177         hdr->ifi_flags = port->ops->get_dev_flags(port);
178         hdr->ifi_change = 0;
179
180         NLA_PUT_STRING(skb, IFLA_IFNAME, port->ops->get_name(port));
181         NLA_PUT_U32(skb, IFLA_MASTER, get_dpifindex(dp));
182         NLA_PUT_U32(skb, IFLA_MTU, port->ops->get_mtu(port));
183 #ifdef IFLA_OPERSTATE
184         NLA_PUT_U8(skb, IFLA_OPERSTATE,
185                    port->ops->is_running(port)
186                         ? port->ops->get_operstate(port)
187                         : IF_OPER_DOWN);
188 #endif
189
190         NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN, port->ops->get_addr(port));
191
192         return nlmsg_end(skb, nlh);
193
194 nla_put_failure:
195         nlmsg_cancel(skb, nlh);
196         return -EMSGSIZE;
197 }
198
199 /* Caller must hold RTNL lock. */
200 static void dp_ifinfo_notify(int event, struct vport *port)
201 {
202         struct sk_buff *skb;
203         int err;
204
205         skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
206         if (!skb) {
207                 err = -ENOBUFS;
208                 goto err;
209         }
210
211         err = dp_fill_ifinfo(skb, port, event, 0);
212         if (err < 0) {
213                 if (err == -ENODEV) {
214                         goto out;
215                 } else {
216                         /* -EMSGSIZE implies BUG in br_nlmsg_size() */
217                         WARN_ON(err == -EMSGSIZE);
218                         goto err;
219                 }
220         }
221
222         rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
223
224         return;
225 err:
226         rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
227 out:
228         kfree_skb(skb);
229 }
230
231 static void release_dp(struct kobject *kobj)
232 {
233         struct datapath *dp = container_of(kobj, struct datapath, ifobj);
234         kfree(dp);
235 }
236
237 static struct kobj_type dp_ktype = {
238         .release = release_dp
239 };
240
241 static void destroy_dp_rcu(struct rcu_head *rcu)
242 {
243         struct datapath *dp = container_of(rcu, struct datapath, rcu);
244
245         flow_tbl_destroy(dp->table);
246         free_percpu(dp->stats_percpu);
247         kobject_put(&dp->ifobj);
248 }
249
250 /* Called with RTNL lock and genl_lock. */
251 static struct vport *new_vport(const struct vport_parms *parms)
252 {
253         struct vport *vport;
254
255         vport = vport_add(parms);
256         if (!IS_ERR(vport)) {
257                 struct datapath *dp = parms->dp;
258
259                 rcu_assign_pointer(dp->ports[parms->port_no], vport);
260                 list_add(&vport->node, &dp->port_list);
261
262                 dp_ifinfo_notify(RTM_NEWLINK, vport);
263         }
264
265         return vport;
266 }
267
268 /* Called with RTNL lock. */
269 void dp_detach_port(struct vport *p)
270 {
271         ASSERT_RTNL();
272
273         if (p->port_no != OVSP_LOCAL)
274                 dp_sysfs_del_if(p);
275         dp_ifinfo_notify(RTM_DELLINK, p);
276
277         /* First drop references to device. */
278         list_del(&p->node);
279         rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
280
281         /* Then destroy it. */
282         vport_del(p);
283 }
284
285 /* Must be called with rcu_read_lock. */
286 void dp_process_received_packet(struct vport *p, struct sk_buff *skb)
287 {
288         struct datapath *dp = p->dp;
289         struct sw_flow *flow;
290         struct dp_stats_percpu *stats;
291         u64 *stats_counter;
292         int error;
293
294         stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
295
296         if (!OVS_CB(skb)->flow) {
297                 struct sw_flow_key key;
298                 int key_len;
299
300                 /* Extract flow from 'skb' into 'key'. */
301                 error = flow_extract(skb, p->port_no, &key, &key_len);
302                 if (unlikely(error)) {
303                         kfree_skb(skb);
304                         return;
305                 }
306
307                 /* Look up flow. */
308                 flow = flow_tbl_lookup(rcu_dereference(dp->table),
309                                        &key, key_len);
310                 if (unlikely(!flow)) {
311                         struct dp_upcall_info upcall;
312
313                         upcall.cmd = OVS_PACKET_CMD_MISS;
314                         upcall.key = &key;
315                         upcall.userdata = NULL;
316                         upcall.pid = p->upcall_pid;
317                         dp_upcall(dp, skb, &upcall);
318                         consume_skb(skb);
319                         stats_counter = &stats->n_missed;
320                         goto out;
321                 }
322
323                 OVS_CB(skb)->flow = flow;
324         }
325
326         stats_counter = &stats->n_hit;
327         flow_used(OVS_CB(skb)->flow, skb);
328         execute_actions(dp, skb);
329
330 out:
331         /* Update datapath statistics. */
332
333         write_seqcount_begin(&stats->seqlock);
334         (*stats_counter)++;
335         write_seqcount_end(&stats->seqlock);
336 }
337
338 static void copy_and_csum_skb(struct sk_buff *skb, void *to)
339 {
340         u16 csum_start, csum_offset;
341         __wsum csum;
342
343         get_skb_csum_pointers(skb, &csum_start, &csum_offset);
344         csum_start -= skb_headroom(skb);
345
346         skb_copy_bits(skb, 0, to, csum_start);
347
348         csum = skb_copy_and_csum_bits(skb, csum_start, to + csum_start,
349                                       skb->len - csum_start, 0);
350         *(__sum16 *)(to + csum_start + csum_offset) = csum_fold(csum);
351 }
352
353 static struct genl_family dp_packet_genl_family = {
354         .id = GENL_ID_GENERATE,
355         .hdrsize = sizeof(struct ovs_header),
356         .name = OVS_PACKET_FAMILY,
357         .version = OVS_PACKET_VERSION,
358         .maxattr = OVS_PACKET_ATTR_MAX
359 };
360
361 int dp_upcall(struct datapath *dp, struct sk_buff *skb,
362               const struct dp_upcall_info *upcall_info)
363 {
364         struct dp_stats_percpu *stats;
365         int dp_ifindex;
366         int err;
367
368         if (upcall_info->pid == 0) {
369                 err = -ENOTCONN;
370                 goto err;
371         }
372
373         dp_ifindex = get_dpifindex(dp);
374         if (!dp_ifindex) {
375                 err = -ENODEV;
376                 goto err;
377         }
378
379         forward_ip_summed(skb, true);
380
381         if (!skb_is_gso(skb))
382                 err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
383         else
384                 err = queue_gso_packets(dp_ifindex, skb, upcall_info);
385         if (err)
386                 goto err;
387
388         return 0;
389
390 err:
391         stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
392
393         write_seqcount_begin(&stats->seqlock);
394         stats->n_lost++;
395         write_seqcount_end(&stats->seqlock);
396
397         return err;
398 }
399
400 static int queue_gso_packets(int dp_ifindex, struct sk_buff *skb,
401                              const struct dp_upcall_info *upcall_info)
402 {
403         struct dp_upcall_info later_info;
404         struct sw_flow_key later_key;
405         struct sk_buff *segs, *nskb;
406         int err;
407
408         segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
409         if (IS_ERR(skb))
410                 return PTR_ERR(skb);
411
412         /* Queue all of the segments. */
413         skb = segs;
414         do {
415                 err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
416                 if (err)
417                         break;
418
419                 if (skb == segs && skb_shinfo(skb)->gso_type & SKB_GSO_UDP) {
420                         /* The initial flow key extracted by flow_extract() in
421                          * this case is for a first fragment, so we need to
422                          * properly mark later fragments.
423                          */
424                         later_key = *upcall_info->key;
425                         later_key.ip.frag = OVS_FRAG_TYPE_LATER;
426
427                         later_info = *upcall_info;
428                         later_info.key = &later_key;
429                         upcall_info = &later_info;
430                 }
431         } while ((skb = skb->next));
432
433         /* Free all of the segments. */
434         skb = segs;
435         do {
436                 nskb = skb->next;
437                 if (err)
438                         kfree_skb(skb);
439                 else
440                         consume_skb(skb);
441         } while ((skb = nskb));
442         return err;
443 }
444
445 static int queue_userspace_packet(int dp_ifindex, struct sk_buff *skb,
446                                   const struct dp_upcall_info *upcall_info)
447 {
448         struct ovs_header *upcall;
449         struct sk_buff *user_skb; /* to be queued to userspace */
450         struct nlattr *nla;
451         unsigned int len;
452         int err;
453
454         err = vlan_deaccel_tag(skb);
455         if (unlikely(err))
456                 return err;
457
458         if (nla_attr_size(skb->len) > USHRT_MAX)
459                 return -EFBIG;
460
461         len = sizeof(struct ovs_header);
462         len += nla_total_size(skb->len);
463         len += nla_total_size(FLOW_BUFSIZE);
464         if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
465                 len += nla_total_size(8);
466
467         user_skb = genlmsg_new(len, GFP_ATOMIC);
468         if (!user_skb)
469                 return -ENOMEM;
470
471         upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
472                              0, upcall_info->cmd);
473         upcall->dp_ifindex = dp_ifindex;
474
475         nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
476         flow_to_nlattrs(upcall_info->key, user_skb);
477         nla_nest_end(user_skb, nla);
478
479         if (upcall_info->userdata)
480                 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
481                             nla_get_u64(upcall_info->userdata));
482
483         nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
484         if (skb->ip_summed == CHECKSUM_PARTIAL)
485                 copy_and_csum_skb(skb, nla_data(nla));
486         else
487                 skb_copy_bits(skb, 0, nla_data(nla), skb->len);
488
489         return genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
490 }
491
492 /* Called with genl_mutex. */
493 static int flush_flows(int dp_ifindex)
494 {
495         struct flow_table *old_table;
496         struct flow_table *new_table;
497         struct datapath *dp;
498
499         dp = get_dp(dp_ifindex);
500         if (!dp)
501                 return -ENODEV;
502
503         old_table = get_table_protected(dp);
504         new_table = flow_tbl_alloc(TBL_MIN_BUCKETS);
505         if (!new_table)
506                 return -ENOMEM;
507
508         rcu_assign_pointer(dp->table, new_table);
509
510         flow_tbl_deferred_destroy(old_table);
511         return 0;
512 }
513
514 static int validate_actions(const struct nlattr *attr,
515                                 const struct sw_flow_key *key, int depth);
516
517 static int validate_sample(const struct nlattr *attr,
518                                 const struct sw_flow_key *key, int depth)
519 {
520         const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
521         const struct nlattr *probability, *actions;
522         const struct nlattr *a;
523         int rem;
524
525         memset(attrs, 0, sizeof(attrs));
526         nla_for_each_nested(a, attr, rem) {
527                 int type = nla_type(a);
528                 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
529                         return -EINVAL;
530                 attrs[type] = a;
531         }
532         if (rem)
533                 return -EINVAL;
534
535         probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
536         if (!probability || nla_len(probability) != sizeof(u32))
537                 return -EINVAL;
538
539         actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
540         if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
541                 return -EINVAL;
542         return validate_actions(actions, key, depth + 1);
543 }
544
545 static int validate_action_key(const struct nlattr *a,
546                                 const struct sw_flow_key *flow_key)
547 {
548         int act_type = nla_type(a);
549         const struct nlattr *ovs_key = nla_data(a);
550         int key_type = nla_type(ovs_key);
551
552         /* There can be only one key in a action */
553         if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
554                 return -EINVAL;
555
556         if (key_type > OVS_KEY_ATTR_MAX ||
557             nla_len(ovs_key) != ovs_key_lens[key_type])
558                 return -EINVAL;
559
560 #define ACTION(act, key)        (((act) << 8) | (key))
561
562         switch (ACTION(act_type, key_type)) {
563         const struct ovs_key_ipv4 *ipv4_key;
564         const struct ovs_key_8021q *q_key;
565
566         case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_PRIORITY):
567         case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_TUN_ID):
568         case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_ETHERNET):
569                 break;
570
571         case ACTION(OVS_ACTION_ATTR_PUSH, OVS_KEY_ATTR_8021Q):
572                 q_key = nla_data(ovs_key);
573                 if (q_key->q_tpid != htons(ETH_P_8021Q))
574                         return -EINVAL;
575
576                 if (q_key->q_tci & htons(VLAN_TAG_PRESENT))
577                         return -EINVAL;
578                 break;
579
580         case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_IPV4):
581                 if (flow_key->eth.type != htons(ETH_P_IP))
582                         return -EINVAL;
583
584                 if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
585                         return -EINVAL;
586
587                 ipv4_key = nla_data(ovs_key);
588                 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
589                         return -EINVAL;
590
591                 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
592                         return -EINVAL;
593
594                 break;
595
596         case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_TCP):
597                 if (flow_key->ip.proto != IPPROTO_TCP)
598                         return -EINVAL;
599
600                 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
601                         return -EINVAL;
602
603                 break;
604
605         case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_UDP):
606                 if (flow_key->ip.proto != IPPROTO_UDP)
607                         return -EINVAL;
608
609                 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
610                         return -EINVAL;
611                 break;
612
613         default:
614                 return -EINVAL;
615         }
616 #undef ACTION
617         return 0;
618 }
619
620 static int validate_userspace(const struct nlattr *attr)
621 {
622         static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] =   {
623                 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
624                 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
625         };
626         struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
627         int error;
628
629         error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
630                                  attr, userspace_policy);
631         if (error)
632                 return error;
633
634         if (!a[OVS_USERSPACE_ATTR_PID] ||
635             !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
636                 return -EINVAL;
637
638         return 0;
639 }
640
641 static int validate_actions(const struct nlattr *attr,
642                                 const struct sw_flow_key *key,  int depth)
643 {
644         const struct nlattr *a;
645         int rem, err;
646
647         if (depth >= SAMPLE_ACTION_DEPTH)
648                 return -EOVERFLOW;
649
650         nla_for_each_nested(a, attr, rem) {
651                 /* Expected argument lengths, (u32)-1 for variable length. */
652                 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
653                         [OVS_ACTION_ATTR_OUTPUT] = 4,
654                         [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
655                         [OVS_ACTION_ATTR_PUSH] = (u32)-1,
656                         [OVS_ACTION_ATTR_POP] = 2,
657                         [OVS_ACTION_ATTR_SET] = (u32)-1,
658                         [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
659                 };
660                 int type = nla_type(a);
661
662                 if (type > OVS_ACTION_ATTR_MAX ||
663                     (action_lens[type] != nla_len(a) &&
664                      action_lens[type] != (u32)-1))
665                         return -EINVAL;
666
667                 switch (type) {
668                 case OVS_ACTION_ATTR_UNSPEC:
669                         return -EINVAL;
670
671                 case OVS_ACTION_ATTR_USERSPACE:
672                         err = validate_userspace(a);
673                         if (err)
674                                 return err;
675                         break;
676
677                 case OVS_ACTION_ATTR_OUTPUT:
678                         if (nla_get_u32(a) >= DP_MAX_PORTS)
679                                 return -EINVAL;
680                         break;
681
682
683                 case OVS_ACTION_ATTR_POP:
684                         if (nla_get_u16(a) != OVS_KEY_ATTR_8021Q)
685                                 return -EINVAL;
686                         break;
687
688                 case OVS_ACTION_ATTR_SET:
689                 case OVS_ACTION_ATTR_PUSH:
690                         err = validate_action_key(a, key);
691                         if (err)
692                                 return err;
693                         break;
694
695                 case OVS_ACTION_ATTR_SAMPLE:
696                         err = validate_sample(a, key, depth);
697                         if (err)
698                                 return err;
699                         break;
700
701                 default:
702                         return -EINVAL;
703                 }
704         }
705
706         if (rem > 0)
707                 return -EINVAL;
708
709         return 0;
710 }
711
712 static void clear_stats(struct sw_flow *flow)
713 {
714         flow->used = 0;
715         flow->tcp_flags = 0;
716         flow->packet_count = 0;
717         flow->byte_count = 0;
718 }
719
720 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
721 {
722         struct ovs_header *ovs_header = info->userhdr;
723         struct nlattr **a = info->attrs;
724         struct sw_flow_actions *acts;
725         struct sk_buff *packet;
726         struct sw_flow *flow;
727         struct datapath *dp;
728         struct ethhdr *eth;
729         int len;
730         int err;
731         int key_len;
732
733         err = -EINVAL;
734         if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
735             !a[OVS_PACKET_ATTR_ACTIONS] ||
736             nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
737                 goto err;
738
739         len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
740         packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
741         err = -ENOMEM;
742         if (!packet)
743                 goto err;
744         skb_reserve(packet, NET_IP_ALIGN);
745
746         memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
747
748         skb_reset_mac_header(packet);
749         eth = eth_hdr(packet);
750
751         /* Normally, setting the skb 'protocol' field would be handled by a
752          * call to eth_type_trans(), but it assumes there's a sending
753          * device, which we may not have. */
754         if (ntohs(eth->h_proto) >= 1536)
755                 packet->protocol = eth->h_proto;
756         else
757                 packet->protocol = htons(ETH_P_802_2);
758
759         /* Build an sw_flow for sending this packet. */
760         flow = flow_alloc();
761         err = PTR_ERR(flow);
762         if (IS_ERR(flow))
763                 goto err_kfree_skb;
764
765         err = flow_extract(packet, -1, &flow->key, &key_len);
766         if (err)
767                 goto err_flow_put;
768
769         err = flow_metadata_from_nlattrs(&flow->key.phy.priority,
770                                          &flow->key.phy.in_port,
771                                          &flow->key.phy.tun_id,
772                                          a[OVS_PACKET_ATTR_KEY]);
773         if (err)
774                 goto err_flow_put;
775
776         err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
777         if (err)
778                 goto err_flow_put;
779
780         flow->hash = flow_hash(&flow->key, key_len);
781
782         acts = flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
783         err = PTR_ERR(acts);
784         if (IS_ERR(acts))
785                 goto err_flow_put;
786         rcu_assign_pointer(flow->sf_acts, acts);
787
788         OVS_CB(packet)->flow = flow;
789         packet->priority = flow->key.phy.priority;
790
791         rcu_read_lock();
792         dp = get_dp(ovs_header->dp_ifindex);
793         err = -ENODEV;
794         if (!dp)
795                 goto err_unlock;
796
797         local_bh_disable();
798         err = execute_actions(dp, packet);
799         local_bh_enable();
800         rcu_read_unlock();
801
802         flow_put(flow);
803         return err;
804
805 err_unlock:
806         rcu_read_unlock();
807 err_flow_put:
808         flow_put(flow);
809 err_kfree_skb:
810         kfree_skb(packet);
811 err:
812         return err;
813 }
814
815 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
816         [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
817         [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
818         [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
819 };
820
821 static struct genl_ops dp_packet_genl_ops[] = {
822         { .cmd = OVS_PACKET_CMD_EXECUTE,
823           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
824           .policy = packet_policy,
825           .doit = ovs_packet_cmd_execute
826         }
827 };
828
829 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
830 {
831         int i;
832         struct flow_table *table = get_table_protected(dp);
833
834         stats->n_flows = flow_tbl_count(table);
835
836         stats->n_hit = stats->n_missed = stats->n_lost = 0;
837         for_each_possible_cpu(i) {
838                 const struct dp_stats_percpu *percpu_stats;
839                 struct dp_stats_percpu local_stats;
840                 unsigned seqcount;
841
842                 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
843
844                 do {
845                         seqcount = read_seqcount_begin(&percpu_stats->seqlock);
846                         local_stats = *percpu_stats;
847                 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
848
849                 stats->n_hit += local_stats.n_hit;
850                 stats->n_missed += local_stats.n_missed;
851                 stats->n_lost += local_stats.n_lost;
852         }
853 }
854
855 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
856         [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
857         [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
858         [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
859 };
860
861 static struct genl_family dp_flow_genl_family = {
862         .id = GENL_ID_GENERATE,
863         .hdrsize = sizeof(struct ovs_header),
864         .name = OVS_FLOW_FAMILY,
865         .version = OVS_FLOW_VERSION,
866         .maxattr = OVS_FLOW_ATTR_MAX
867 };
868
869 static struct genl_multicast_group dp_flow_multicast_group = {
870         .name = OVS_FLOW_MCGROUP
871 };
872
873 /* Called with genl_lock. */
874 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
875                                   struct sk_buff *skb, u32 pid,
876                                   u32 seq, u32 flags, u8 cmd)
877 {
878         const int skb_orig_len = skb->len;
879         const struct sw_flow_actions *sf_acts;
880         struct ovs_flow_stats stats;
881         struct ovs_header *ovs_header;
882         struct nlattr *nla;
883         unsigned long used;
884         u8 tcp_flags;
885         int err;
886
887         sf_acts = rcu_dereference_protected(flow->sf_acts,
888                                             lockdep_genl_is_held());
889
890         ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
891         if (!ovs_header)
892                 return -EMSGSIZE;
893
894         ovs_header->dp_ifindex = get_dpifindex(dp);
895
896         nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
897         if (!nla)
898                 goto nla_put_failure;
899         err = flow_to_nlattrs(&flow->key, skb);
900         if (err)
901                 goto error;
902         nla_nest_end(skb, nla);
903
904         spin_lock_bh(&flow->lock);
905         used = flow->used;
906         stats.n_packets = flow->packet_count;
907         stats.n_bytes = flow->byte_count;
908         tcp_flags = flow->tcp_flags;
909         spin_unlock_bh(&flow->lock);
910
911         if (used)
912                 NLA_PUT_U64(skb, OVS_FLOW_ATTR_USED, flow_used_time(used));
913
914         if (stats.n_packets)
915                 NLA_PUT(skb, OVS_FLOW_ATTR_STATS,
916                         sizeof(struct ovs_flow_stats), &stats);
917
918         if (tcp_flags)
919                 NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
920
921         /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
922          * this is the first flow to be dumped into 'skb'.  This is unusual for
923          * Netlink but individual action lists can be longer than
924          * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
925          * The userspace caller can always fetch the actions separately if it
926          * really wants them.  (Most userspace callers in fact don't care.)
927          *
928          * This can only fail for dump operations because the skb is always
929          * properly sized for single flows.
930          */
931         err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
932                       sf_acts->actions);
933         if (err < 0 && skb_orig_len)
934                 goto error;
935
936         return genlmsg_end(skb, ovs_header);
937
938 nla_put_failure:
939         err = -EMSGSIZE;
940 error:
941         genlmsg_cancel(skb, ovs_header);
942         return err;
943 }
944
945 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
946 {
947         const struct sw_flow_actions *sf_acts;
948         int len;
949
950         sf_acts = rcu_dereference_protected(flow->sf_acts,
951                                             lockdep_genl_is_held());
952
953         /* OVS_FLOW_ATTR_KEY */
954         len = nla_total_size(FLOW_BUFSIZE);
955         /* OVS_FLOW_ATTR_ACTIONS */
956         len += nla_total_size(sf_acts->actions_len);
957         /* OVS_FLOW_ATTR_STATS */
958         len += nla_total_size(sizeof(struct ovs_flow_stats));
959         /* OVS_FLOW_ATTR_TCP_FLAGS */
960         len += nla_total_size(1);
961         /* OVS_FLOW_ATTR_USED */
962         len += nla_total_size(8);
963
964         len += NLMSG_ALIGN(sizeof(struct ovs_header));
965
966         return genlmsg_new(len, GFP_KERNEL);
967 }
968
969 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
970                                                struct datapath *dp,
971                                                u32 pid, u32 seq, u8 cmd)
972 {
973         struct sk_buff *skb;
974         int retval;
975
976         skb = ovs_flow_cmd_alloc_info(flow);
977         if (!skb)
978                 return ERR_PTR(-ENOMEM);
979
980         retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
981         BUG_ON(retval < 0);
982         return skb;
983 }
984
985 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
986 {
987         struct nlattr **a = info->attrs;
988         struct ovs_header *ovs_header = info->userhdr;
989         struct sw_flow_key key;
990         struct sw_flow *flow;
991         struct sk_buff *reply;
992         struct datapath *dp;
993         struct flow_table *table;
994         int error;
995         int key_len;
996
997         /* Extract key. */
998         error = -EINVAL;
999         if (!a[OVS_FLOW_ATTR_KEY])
1000                 goto error;
1001         error = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1002         if (error)
1003                 goto error;
1004
1005         /* Validate actions. */
1006         if (a[OVS_FLOW_ATTR_ACTIONS]) {
1007                 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key,  0);
1008                 if (error)
1009                         goto error;
1010         } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1011                 error = -EINVAL;
1012                 goto error;
1013         }
1014
1015         dp = get_dp(ovs_header->dp_ifindex);
1016         error = -ENODEV;
1017         if (!dp)
1018                 goto error;
1019
1020         table = get_table_protected(dp);
1021         flow = flow_tbl_lookup(table, &key, key_len);
1022         if (!flow) {
1023                 struct sw_flow_actions *acts;
1024
1025                 /* Bail out if we're not allowed to create a new flow. */
1026                 error = -ENOENT;
1027                 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1028                         goto error;
1029
1030                 /* Expand table, if necessary, to make room. */
1031                 if (flow_tbl_need_to_expand(table)) {
1032                         struct flow_table *new_table;
1033
1034                         new_table = flow_tbl_expand(table);
1035                         if (!IS_ERR(new_table)) {
1036                                 rcu_assign_pointer(dp->table, new_table);
1037                                 flow_tbl_deferred_destroy(table);
1038                                 table = get_table_protected(dp);
1039                         }
1040                 }
1041
1042                 /* Allocate flow. */
1043                 flow = flow_alloc();
1044                 if (IS_ERR(flow)) {
1045                         error = PTR_ERR(flow);
1046                         goto error;
1047                 }
1048                 flow->key = key;
1049                 clear_stats(flow);
1050
1051                 /* Obtain actions. */
1052                 acts = flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1053                 error = PTR_ERR(acts);
1054                 if (IS_ERR(acts))
1055                         goto error_free_flow;
1056                 rcu_assign_pointer(flow->sf_acts, acts);
1057
1058                 /* Put flow in bucket. */
1059                 flow->hash = flow_hash(&key, key_len);
1060                 flow_tbl_insert(table, flow);
1061
1062                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1063                                                 info->snd_seq,
1064                                                 OVS_FLOW_CMD_NEW);
1065         } else {
1066                 /* We found a matching flow. */
1067                 struct sw_flow_actions *old_acts;
1068                 struct nlattr *acts_attrs;
1069
1070                 /* Bail out if we're not allowed to modify an existing flow.
1071                  * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1072                  * because Generic Netlink treats the latter as a dump
1073                  * request.  We also accept NLM_F_EXCL in case that bug ever
1074                  * gets fixed.
1075                  */
1076                 error = -EEXIST;
1077                 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1078                     info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1079                         goto error;
1080
1081                 /* Update actions. */
1082                 old_acts = rcu_dereference_protected(flow->sf_acts,
1083                                                      lockdep_genl_is_held());
1084                 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1085                 if (acts_attrs &&
1086                    (old_acts->actions_len != nla_len(acts_attrs) ||
1087                    memcmp(old_acts->actions, nla_data(acts_attrs),
1088                           old_acts->actions_len))) {
1089                         struct sw_flow_actions *new_acts;
1090
1091                         new_acts = flow_actions_alloc(acts_attrs);
1092                         error = PTR_ERR(new_acts);
1093                         if (IS_ERR(new_acts))
1094                                 goto error;
1095
1096                         rcu_assign_pointer(flow->sf_acts, new_acts);
1097                         flow_deferred_free_acts(old_acts);
1098                 }
1099
1100                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1101                                                info->snd_seq, OVS_FLOW_CMD_NEW);
1102
1103                 /* Clear stats. */
1104                 if (a[OVS_FLOW_ATTR_CLEAR]) {
1105                         spin_lock_bh(&flow->lock);
1106                         clear_stats(flow);
1107                         spin_unlock_bh(&flow->lock);
1108                 }
1109         }
1110
1111         if (!IS_ERR(reply))
1112                 genl_notify(reply, genl_info_net(info), info->snd_pid,
1113                            dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1114         else
1115                 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1116                                 dp_flow_multicast_group.id, PTR_ERR(reply));
1117         return 0;
1118
1119 error_free_flow:
1120         flow_put(flow);
1121 error:
1122         return error;
1123 }
1124
1125 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1126 {
1127         struct nlattr **a = info->attrs;
1128         struct ovs_header *ovs_header = info->userhdr;
1129         struct sw_flow_key key;
1130         struct sk_buff *reply;
1131         struct sw_flow *flow;
1132         struct datapath *dp;
1133         struct flow_table *table;
1134         int err;
1135         int key_len;
1136
1137         if (!a[OVS_FLOW_ATTR_KEY])
1138                 return -EINVAL;
1139         err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1140         if (err)
1141                 return err;
1142
1143         dp = get_dp(ovs_header->dp_ifindex);
1144         if (!dp)
1145                 return -ENODEV;
1146
1147         table = get_table_protected(dp);
1148         flow = flow_tbl_lookup(table, &key, key_len);
1149         if (!flow)
1150                 return -ENOENT;
1151
1152         reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1153                                         info->snd_seq, OVS_FLOW_CMD_NEW);
1154         if (IS_ERR(reply))
1155                 return PTR_ERR(reply);
1156
1157         return genlmsg_reply(reply, info);
1158 }
1159
1160 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1161 {
1162         struct nlattr **a = info->attrs;
1163         struct ovs_header *ovs_header = info->userhdr;
1164         struct sw_flow_key key;
1165         struct sk_buff *reply;
1166         struct sw_flow *flow;
1167         struct datapath *dp;
1168         struct flow_table *table;
1169         int err;
1170         int key_len;
1171
1172         if (!a[OVS_FLOW_ATTR_KEY])
1173                 return flush_flows(ovs_header->dp_ifindex);
1174         err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1175         if (err)
1176                 return err;
1177
1178         dp = get_dp(ovs_header->dp_ifindex);
1179         if (!dp)
1180                 return -ENODEV;
1181
1182         table = get_table_protected(dp);
1183         flow = flow_tbl_lookup(table, &key, key_len);
1184         if (!flow)
1185                 return -ENOENT;
1186
1187         reply = ovs_flow_cmd_alloc_info(flow);
1188         if (!reply)
1189                 return -ENOMEM;
1190
1191         flow_tbl_remove(table, flow);
1192
1193         err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1194                                      info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1195         BUG_ON(err < 0);
1196
1197         flow_deferred_free(flow);
1198
1199         genl_notify(reply, genl_info_net(info), info->snd_pid,
1200                     dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1201         return 0;
1202 }
1203
1204 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1205 {
1206         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1207         struct datapath *dp;
1208
1209         dp = get_dp(ovs_header->dp_ifindex);
1210         if (!dp)
1211                 return -ENODEV;
1212
1213         for (;;) {
1214                 struct sw_flow *flow;
1215                 u32 bucket, obj;
1216
1217                 bucket = cb->args[0];
1218                 obj = cb->args[1];
1219                 flow = flow_tbl_next(get_table_protected(dp), &bucket, &obj);
1220                 if (!flow)
1221                         break;
1222
1223                 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1224                                            NETLINK_CB(cb->skb).pid,
1225                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
1226                                            OVS_FLOW_CMD_NEW) < 0)
1227                         break;
1228
1229                 cb->args[0] = bucket;
1230                 cb->args[1] = obj;
1231         }
1232         return skb->len;
1233 }
1234
1235 static struct genl_ops dp_flow_genl_ops[] = {
1236         { .cmd = OVS_FLOW_CMD_NEW,
1237           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1238           .policy = flow_policy,
1239           .doit = ovs_flow_cmd_new_or_set
1240         },
1241         { .cmd = OVS_FLOW_CMD_DEL,
1242           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1243           .policy = flow_policy,
1244           .doit = ovs_flow_cmd_del
1245         },
1246         { .cmd = OVS_FLOW_CMD_GET,
1247           .flags = 0,               /* OK for unprivileged users. */
1248           .policy = flow_policy,
1249           .doit = ovs_flow_cmd_get,
1250           .dumpit = ovs_flow_cmd_dump
1251         },
1252         { .cmd = OVS_FLOW_CMD_SET,
1253           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1254           .policy = flow_policy,
1255           .doit = ovs_flow_cmd_new_or_set,
1256         },
1257 };
1258
1259 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1260 #ifdef HAVE_NLA_NUL_STRING
1261         [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1262 #endif
1263         [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1264 };
1265
1266 static struct genl_family dp_datapath_genl_family = {
1267         .id = GENL_ID_GENERATE,
1268         .hdrsize = sizeof(struct ovs_header),
1269         .name = OVS_DATAPATH_FAMILY,
1270         .version = OVS_DATAPATH_VERSION,
1271         .maxattr = OVS_DP_ATTR_MAX
1272 };
1273
1274 static struct genl_multicast_group dp_datapath_multicast_group = {
1275         .name = OVS_DATAPATH_MCGROUP
1276 };
1277
1278 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1279                                 u32 pid, u32 seq, u32 flags, u8 cmd)
1280 {
1281         struct ovs_header *ovs_header;
1282         struct nlattr *nla;
1283         int err;
1284
1285         ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1286                                    flags, cmd);
1287         if (!ovs_header)
1288                 goto error;
1289
1290         ovs_header->dp_ifindex = get_dpifindex(dp);
1291
1292         rcu_read_lock();
1293         err = nla_put_string(skb, OVS_DP_ATTR_NAME, dp_name(dp));
1294         rcu_read_unlock();
1295         if (err)
1296                 goto nla_put_failure;
1297
1298         nla = nla_reserve(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats));
1299         if (!nla)
1300                 goto nla_put_failure;
1301         get_dp_stats(dp, nla_data(nla));
1302
1303         return genlmsg_end(skb, ovs_header);
1304
1305 nla_put_failure:
1306         genlmsg_cancel(skb, ovs_header);
1307 error:
1308         return -EMSGSIZE;
1309 }
1310
1311 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1312                                              u32 seq, u8 cmd)
1313 {
1314         struct sk_buff *skb;
1315         int retval;
1316
1317         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1318         if (!skb)
1319                 return ERR_PTR(-ENOMEM);
1320
1321         retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1322         if (retval < 0) {
1323                 kfree_skb(skb);
1324                 return ERR_PTR(retval);
1325         }
1326         return skb;
1327 }
1328
1329 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1330 {
1331         return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1332 }
1333
1334 /* Called with genl_mutex and optionally with RTNL lock also. */
1335 static struct datapath *lookup_datapath(struct ovs_header *ovs_header,
1336                                         struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1337 {
1338         struct datapath *dp;
1339
1340         if (!a[OVS_DP_ATTR_NAME])
1341                 dp = get_dp(ovs_header->dp_ifindex);
1342         else {
1343                 struct vport *vport;
1344
1345                 rcu_read_lock();
1346                 vport = vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
1347                 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1348                 rcu_read_unlock();
1349         }
1350         return dp ? dp : ERR_PTR(-ENODEV);
1351 }
1352
1353 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1354 {
1355         struct nlattr **a = info->attrs;
1356         struct vport_parms parms;
1357         struct sk_buff *reply;
1358         struct datapath *dp;
1359         struct vport *vport;
1360         int err;
1361
1362         err = -EINVAL;
1363         if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1364                 goto err;
1365
1366         err = ovs_dp_cmd_validate(a);
1367         if (err)
1368                 goto err;
1369
1370         rtnl_lock();
1371         err = -ENODEV;
1372         if (!try_module_get(THIS_MODULE))
1373                 goto err_unlock_rtnl;
1374
1375         err = -ENOMEM;
1376         dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1377         if (dp == NULL)
1378                 goto err_put_module;
1379         INIT_LIST_HEAD(&dp->port_list);
1380
1381         /* Initialize kobject for bridge.  This will be added as
1382          * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1383         dp->ifobj.kset = NULL;
1384         kobject_init(&dp->ifobj, &dp_ktype);
1385
1386         /* Allocate table. */
1387         err = -ENOMEM;
1388         rcu_assign_pointer(dp->table, flow_tbl_alloc(TBL_MIN_BUCKETS));
1389         if (!dp->table)
1390                 goto err_free_dp;
1391
1392         dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1393         if (!dp->stats_percpu) {
1394                 err = -ENOMEM;
1395                 goto err_destroy_table;
1396         }
1397
1398         /* Set up our datapath device. */
1399         parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1400         parms.type = OVS_VPORT_TYPE_INTERNAL;
1401         parms.options = NULL;
1402         parms.dp = dp;
1403         parms.port_no = OVSP_LOCAL;
1404         parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1405
1406         vport = new_vport(&parms);
1407         if (IS_ERR(vport)) {
1408                 err = PTR_ERR(vport);
1409                 if (err == -EBUSY)
1410                         err = -EEXIST;
1411
1412                 goto err_destroy_percpu;
1413         }
1414
1415         reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1416                                       info->snd_seq, OVS_DP_CMD_NEW);
1417         err = PTR_ERR(reply);
1418         if (IS_ERR(reply))
1419                 goto err_destroy_local_port;
1420
1421         list_add_tail(&dp->list_node, &dps);
1422         dp_sysfs_add_dp(dp);
1423
1424         rtnl_unlock();
1425
1426         genl_notify(reply, genl_info_net(info), info->snd_pid,
1427                     dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1428         return 0;
1429
1430 err_destroy_local_port:
1431         dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1432 err_destroy_percpu:
1433         free_percpu(dp->stats_percpu);
1434 err_destroy_table:
1435         flow_tbl_destroy(get_table_protected(dp));
1436 err_free_dp:
1437         kfree(dp);
1438 err_put_module:
1439         module_put(THIS_MODULE);
1440 err_unlock_rtnl:
1441         rtnl_unlock();
1442 err:
1443         return err;
1444 }
1445
1446 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1447 {
1448         struct vport *vport, *next_vport;
1449         struct sk_buff *reply;
1450         struct datapath *dp;
1451         int err;
1452
1453         err = ovs_dp_cmd_validate(info->attrs);
1454         if (err)
1455                 goto exit;
1456
1457         rtnl_lock();
1458         dp = lookup_datapath(info->userhdr, info->attrs);
1459         err = PTR_ERR(dp);
1460         if (IS_ERR(dp))
1461                 goto exit_unlock;
1462
1463         reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1464                                       info->snd_seq, OVS_DP_CMD_DEL);
1465         err = PTR_ERR(reply);
1466         if (IS_ERR(reply))
1467                 goto exit_unlock;
1468
1469         list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
1470                 if (vport->port_no != OVSP_LOCAL)
1471                         dp_detach_port(vport);
1472
1473         dp_sysfs_del_dp(dp);
1474         list_del(&dp->list_node);
1475         dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1476
1477         /* rtnl_unlock() will wait until all the references to devices that
1478          * are pending unregistration have been dropped.  We do it here to
1479          * ensure that any internal devices (which contain DP pointers) are
1480          * fully destroyed before freeing the datapath.
1481          */
1482         rtnl_unlock();
1483
1484         call_rcu(&dp->rcu, destroy_dp_rcu);
1485         module_put(THIS_MODULE);
1486
1487         genl_notify(reply, genl_info_net(info), info->snd_pid,
1488                     dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1489
1490         return 0;
1491
1492 exit_unlock:
1493         rtnl_unlock();
1494 exit:
1495         return err;
1496 }
1497
1498 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1499 {
1500         struct sk_buff *reply;
1501         struct datapath *dp;
1502         int err;
1503
1504         err = ovs_dp_cmd_validate(info->attrs);
1505         if (err)
1506                 return err;
1507
1508         dp = lookup_datapath(info->userhdr, info->attrs);
1509         if (IS_ERR(dp))
1510                 return PTR_ERR(dp);
1511
1512         reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1513                                       info->snd_seq, OVS_DP_CMD_NEW);
1514         if (IS_ERR(reply)) {
1515                 err = PTR_ERR(reply);
1516                 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1517                                 dp_datapath_multicast_group.id, err);
1518                 return 0;
1519         }
1520
1521         genl_notify(reply, genl_info_net(info), info->snd_pid,
1522                     dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1523         return 0;
1524 }
1525
1526 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1527 {
1528         struct sk_buff *reply;
1529         struct datapath *dp;
1530         int err;
1531
1532         err = ovs_dp_cmd_validate(info->attrs);
1533         if (err)
1534                 return err;
1535
1536         dp = lookup_datapath(info->userhdr, info->attrs);
1537         if (IS_ERR(dp))
1538                 return PTR_ERR(dp);
1539
1540         reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1541                                       info->snd_seq, OVS_DP_CMD_NEW);
1542         if (IS_ERR(reply))
1543                 return PTR_ERR(reply);
1544
1545         return genlmsg_reply(reply, info);
1546 }
1547
1548 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1549 {
1550         struct datapath *dp;
1551         int skip = cb->args[0];
1552         int i = 0;
1553
1554         list_for_each_entry(dp, &dps, list_node) {
1555                 if (i < skip)
1556                         continue;
1557                 if (ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1558                                          cb->nlh->nlmsg_seq, NLM_F_MULTI,
1559                                          OVS_DP_CMD_NEW) < 0)
1560                         break;
1561                 i++;
1562         }
1563
1564         cb->args[0] = i;
1565
1566         return skb->len;
1567 }
1568
1569 static struct genl_ops dp_datapath_genl_ops[] = {
1570         { .cmd = OVS_DP_CMD_NEW,
1571           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1572           .policy = datapath_policy,
1573           .doit = ovs_dp_cmd_new
1574         },
1575         { .cmd = OVS_DP_CMD_DEL,
1576           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1577           .policy = datapath_policy,
1578           .doit = ovs_dp_cmd_del
1579         },
1580         { .cmd = OVS_DP_CMD_GET,
1581           .flags = 0,               /* OK for unprivileged users. */
1582           .policy = datapath_policy,
1583           .doit = ovs_dp_cmd_get,
1584           .dumpit = ovs_dp_cmd_dump
1585         },
1586         { .cmd = OVS_DP_CMD_SET,
1587           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1588           .policy = datapath_policy,
1589           .doit = ovs_dp_cmd_set,
1590         },
1591 };
1592
1593 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1594 #ifdef HAVE_NLA_NUL_STRING
1595         [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1596         [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1597         [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1598 #else
1599         [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1600         [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1601 #endif
1602         [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1603         [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1604         [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1605         [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1606 };
1607
1608 static struct genl_family dp_vport_genl_family = {
1609         .id = GENL_ID_GENERATE,
1610         .hdrsize = sizeof(struct ovs_header),
1611         .name = OVS_VPORT_FAMILY,
1612         .version = OVS_VPORT_VERSION,
1613         .maxattr = OVS_VPORT_ATTR_MAX
1614 };
1615
1616 struct genl_multicast_group dp_vport_multicast_group = {
1617         .name = OVS_VPORT_MCGROUP
1618 };
1619
1620 /* Called with RTNL lock or RCU read lock. */
1621 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1622                                    u32 pid, u32 seq, u32 flags, u8 cmd)
1623 {
1624         struct ovs_header *ovs_header;
1625         struct nlattr *nla;
1626         int err;
1627
1628         ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1629                                  flags, cmd);
1630         if (!ovs_header)
1631                 return -EMSGSIZE;
1632
1633         ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1634
1635         NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1636         NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type);
1637         NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport));
1638         NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
1639
1640         nla = nla_reserve(skb, OVS_VPORT_ATTR_STATS,
1641                           sizeof(struct ovs_vport_stats));
1642         if (!nla)
1643                 goto nla_put_failure;
1644
1645         vport_get_stats(vport, nla_data(nla));
1646
1647         NLA_PUT(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
1648                 vport->ops->get_addr(vport));
1649
1650         err = vport_get_options(vport, skb);
1651         if (err == -EMSGSIZE)
1652                 goto error;
1653
1654         return genlmsg_end(skb, ovs_header);
1655
1656 nla_put_failure:
1657         err = -EMSGSIZE;
1658 error:
1659         genlmsg_cancel(skb, ovs_header);
1660         return err;
1661 }
1662
1663 /* Called with RTNL lock or RCU read lock. */
1664 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1665                                          u32 seq, u8 cmd)
1666 {
1667         struct sk_buff *skb;
1668         int retval;
1669
1670         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1671         if (!skb)
1672                 return ERR_PTR(-ENOMEM);
1673
1674         retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1675         if (retval < 0) {
1676                 kfree_skb(skb);
1677                 return ERR_PTR(retval);
1678         }
1679         return skb;
1680 }
1681
1682 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1683 {
1684         return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1685 }
1686
1687 /* Called with RTNL lock or RCU read lock. */
1688 static struct vport *lookup_vport(struct ovs_header *ovs_header,
1689                                   struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1690 {
1691         struct datapath *dp;
1692         struct vport *vport;
1693
1694         if (a[OVS_VPORT_ATTR_NAME]) {
1695                 vport = vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
1696                 if (!vport)
1697                         return ERR_PTR(-ENODEV);
1698                 return vport;
1699         } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1700                 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1701
1702                 if (port_no >= DP_MAX_PORTS)
1703                         return ERR_PTR(-EFBIG);
1704
1705                 dp = get_dp(ovs_header->dp_ifindex);
1706                 if (!dp)
1707                         return ERR_PTR(-ENODEV);
1708
1709                 vport = get_vport_protected(dp, port_no);
1710                 if (!vport)
1711                         return ERR_PTR(-ENOENT);
1712                 return vport;
1713         } else
1714                 return ERR_PTR(-EINVAL);
1715 }
1716
1717 /* Called with RTNL lock. */
1718 static int change_vport(struct vport *vport,
1719                         struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1720 {
1721         int err = 0;
1722
1723         if (a[OVS_VPORT_ATTR_STATS])
1724                 vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1725
1726         if (a[OVS_VPORT_ATTR_ADDRESS])
1727                 err = vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
1728
1729         return err;
1730 }
1731
1732 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1733 {
1734         struct nlattr **a = info->attrs;
1735         struct ovs_header *ovs_header = info->userhdr;
1736         struct vport_parms parms;
1737         struct sk_buff *reply;
1738         struct vport *vport;
1739         struct datapath *dp;
1740         u32 port_no;
1741         int err;
1742
1743         err = -EINVAL;
1744         if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1745             !a[OVS_VPORT_ATTR_UPCALL_PID])
1746                 goto exit;
1747
1748         err = ovs_vport_cmd_validate(a);
1749         if (err)
1750                 goto exit;
1751
1752         rtnl_lock();
1753         dp = get_dp(ovs_header->dp_ifindex);
1754         err = -ENODEV;
1755         if (!dp)
1756                 goto exit_unlock;
1757
1758         if (a[OVS_VPORT_ATTR_PORT_NO]) {
1759                 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1760
1761                 err = -EFBIG;
1762                 if (port_no >= DP_MAX_PORTS)
1763                         goto exit_unlock;
1764
1765                 vport = get_vport_protected(dp, port_no);
1766                 err = -EBUSY;
1767                 if (vport)
1768                         goto exit_unlock;
1769         } else {
1770                 for (port_no = 1; ; port_no++) {
1771                         if (port_no >= DP_MAX_PORTS) {
1772                                 err = -EFBIG;
1773                                 goto exit_unlock;
1774                         }
1775                         vport = get_vport_protected(dp, port_no);
1776                         if (!vport)
1777                                 break;
1778                 }
1779         }
1780
1781         parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1782         parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1783         parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1784         parms.dp = dp;
1785         parms.port_no = port_no;
1786         parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1787
1788         vport = new_vport(&parms);
1789         err = PTR_ERR(vport);
1790         if (IS_ERR(vport))
1791                 goto exit_unlock;
1792
1793         dp_sysfs_add_if(vport);
1794
1795         err = change_vport(vport, a);
1796         if (!err) {
1797                 reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
1798                                                  info->snd_seq,
1799                                                  OVS_VPORT_CMD_NEW);
1800                 if (IS_ERR(reply))
1801                         err = PTR_ERR(reply);
1802         }
1803         if (err) {
1804                 dp_detach_port(vport);
1805                 goto exit_unlock;
1806         }
1807         genl_notify(reply, genl_info_net(info), info->snd_pid,
1808                     dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1809
1810
1811 exit_unlock:
1812         rtnl_unlock();
1813 exit:
1814         return err;
1815 }
1816
1817 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1818 {
1819         struct nlattr **a = info->attrs;
1820         struct sk_buff *reply;
1821         struct vport *vport;
1822         int err;
1823
1824         err = ovs_vport_cmd_validate(a);
1825         if (err)
1826                 goto exit;
1827
1828         rtnl_lock();
1829         vport = lookup_vport(info->userhdr, a);
1830         err = PTR_ERR(vport);
1831         if (IS_ERR(vport))
1832                 goto exit_unlock;
1833
1834         err = 0;
1835         if (a[OVS_VPORT_ATTR_TYPE] &&
1836             nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1837                 err = -EINVAL;
1838
1839         if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1840                 err = vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1841         if (!err)
1842                 err = change_vport(vport, a);
1843         if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1844                 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1845
1846         reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1847                                          OVS_VPORT_CMD_NEW);
1848         if (IS_ERR(reply)) {
1849                 err = PTR_ERR(reply);
1850                 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1851                                 dp_vport_multicast_group.id, err);
1852                 return 0;
1853         }
1854
1855         genl_notify(reply, genl_info_net(info), info->snd_pid,
1856                     dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1857
1858 exit_unlock:
1859         rtnl_unlock();
1860 exit:
1861         return err;
1862 }
1863
1864 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1865 {
1866         struct nlattr **a = info->attrs;
1867         struct sk_buff *reply;
1868         struct vport *vport;
1869         int err;
1870
1871         err = ovs_vport_cmd_validate(a);
1872         if (err)
1873                 goto exit;
1874
1875         rtnl_lock();
1876         vport = lookup_vport(info->userhdr, a);
1877         err = PTR_ERR(vport);
1878         if (IS_ERR(vport))
1879                 goto exit_unlock;
1880
1881         if (vport->port_no == OVSP_LOCAL) {
1882                 err = -EINVAL;
1883                 goto exit_unlock;
1884         }
1885
1886         reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1887                                          OVS_VPORT_CMD_DEL);
1888         err = PTR_ERR(reply);
1889         if (IS_ERR(reply))
1890                 goto exit_unlock;
1891
1892         dp_detach_port(vport);
1893
1894         genl_notify(reply, genl_info_net(info), info->snd_pid,
1895                     dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1896
1897 exit_unlock:
1898         rtnl_unlock();
1899 exit:
1900         return err;
1901 }
1902
1903 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1904 {
1905         struct nlattr **a = info->attrs;
1906         struct ovs_header *ovs_header = info->userhdr;
1907         struct sk_buff *reply;
1908         struct vport *vport;
1909         int err;
1910
1911         err = ovs_vport_cmd_validate(a);
1912         if (err)
1913                 goto exit;
1914
1915         rcu_read_lock();
1916         vport = lookup_vport(ovs_header, a);
1917         err = PTR_ERR(vport);
1918         if (IS_ERR(vport))
1919                 goto exit_unlock;
1920
1921         reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1922                                          OVS_VPORT_CMD_NEW);
1923         err = PTR_ERR(reply);
1924         if (IS_ERR(reply))
1925                 goto exit_unlock;
1926
1927         rcu_read_unlock();
1928
1929         return genlmsg_reply(reply, info);
1930
1931 exit_unlock:
1932         rcu_read_unlock();
1933 exit:
1934         return err;
1935 }
1936
1937 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1938 {
1939         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1940         struct datapath *dp;
1941         u32 port_no;
1942         int retval;
1943
1944         dp = get_dp(ovs_header->dp_ifindex);
1945         if (!dp)
1946                 return -ENODEV;
1947
1948         rcu_read_lock();
1949         for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1950                 struct vport *vport;
1951
1952                 vport = get_vport_protected(dp, port_no);
1953                 if (!vport)
1954                         continue;
1955
1956                 if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1957                                             cb->nlh->nlmsg_seq, NLM_F_MULTI,
1958                                             OVS_VPORT_CMD_NEW) < 0)
1959                         break;
1960         }
1961         rcu_read_unlock();
1962
1963         cb->args[0] = port_no;
1964         retval = skb->len;
1965
1966         return retval;
1967 }
1968
1969 static struct genl_ops dp_vport_genl_ops[] = {
1970         { .cmd = OVS_VPORT_CMD_NEW,
1971           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1972           .policy = vport_policy,
1973           .doit = ovs_vport_cmd_new
1974         },
1975         { .cmd = OVS_VPORT_CMD_DEL,
1976           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1977           .policy = vport_policy,
1978           .doit = ovs_vport_cmd_del
1979         },
1980         { .cmd = OVS_VPORT_CMD_GET,
1981           .flags = 0,               /* OK for unprivileged users. */
1982           .policy = vport_policy,
1983           .doit = ovs_vport_cmd_get,
1984           .dumpit = ovs_vport_cmd_dump
1985         },
1986         { .cmd = OVS_VPORT_CMD_SET,
1987           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1988           .policy = vport_policy,
1989           .doit = ovs_vport_cmd_set,
1990         },
1991 };
1992
1993 struct genl_family_and_ops {
1994         struct genl_family *family;
1995         struct genl_ops *ops;
1996         int n_ops;
1997         struct genl_multicast_group *group;
1998 };
1999
2000 static const struct genl_family_and_ops dp_genl_families[] = {
2001         { &dp_datapath_genl_family,
2002           dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2003           &dp_datapath_multicast_group },
2004         { &dp_vport_genl_family,
2005           dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2006           &dp_vport_multicast_group },
2007         { &dp_flow_genl_family,
2008           dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2009           &dp_flow_multicast_group },
2010         { &dp_packet_genl_family,
2011           dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2012           NULL },
2013 };
2014
2015 static void dp_unregister_genl(int n_families)
2016 {
2017         int i;
2018
2019         for (i = 0; i < n_families; i++)
2020                 genl_unregister_family(dp_genl_families[i].family);
2021 }
2022
2023 static int dp_register_genl(void)
2024 {
2025         int n_registered;
2026         int err;
2027         int i;
2028
2029         n_registered = 0;
2030         for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2031                 const struct genl_family_and_ops *f = &dp_genl_families[i];
2032
2033                 err = genl_register_family_with_ops(f->family, f->ops,
2034                                                     f->n_ops);
2035                 if (err)
2036                         goto error;
2037                 n_registered++;
2038
2039                 if (f->group) {
2040                         err = genl_register_mc_group(f->family, f->group);
2041                         if (err)
2042                                 goto error;
2043                 }
2044         }
2045
2046         return 0;
2047
2048 error:
2049         dp_unregister_genl(n_registered);
2050         return err;
2051 }
2052
2053 static int __init dp_init(void)
2054 {
2055         struct sk_buff *dummy_skb;
2056         int err;
2057
2058         BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2059
2060         pr_info("Open vSwitch %s, built "__DATE__" "__TIME__"\n",
2061                 VERSION BUILDNR);
2062
2063         err = tnl_init();
2064         if (err)
2065                 goto error;
2066
2067         err = flow_init();
2068         if (err)
2069                 goto error_tnl_exit;
2070
2071         err = vport_init();
2072         if (err)
2073                 goto error_flow_exit;
2074
2075         err = register_netdevice_notifier(&dp_device_notifier);
2076         if (err)
2077                 goto error_vport_exit;
2078
2079         err = dp_register_genl();
2080         if (err < 0)
2081                 goto error_unreg_notifier;
2082
2083         return 0;
2084
2085 error_unreg_notifier:
2086         unregister_netdevice_notifier(&dp_device_notifier);
2087 error_vport_exit:
2088         vport_exit();
2089 error_flow_exit:
2090         flow_exit();
2091 error_tnl_exit:
2092         tnl_exit();
2093 error:
2094         return err;
2095 }
2096
2097 static void dp_cleanup(void)
2098 {
2099         rcu_barrier();
2100         dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2101         unregister_netdevice_notifier(&dp_device_notifier);
2102         vport_exit();
2103         flow_exit();
2104         tnl_exit();
2105 }
2106
2107 module_init(dp_init);
2108 module_exit(dp_cleanup);
2109
2110 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2111 MODULE_LICENSE("GPL");