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