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