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