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