Update primary code license to Apache 2.0.
[sliver-openvswitch.git] / datapath / brcompat.c
1 /*
2  * Copyright (c) 2009 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 #include <linux/kernel.h>
10 #include <asm/uaccess.h>
11 #include <linux/completion.h>
12 #include <linux/delay.h>
13 #include <linux/etherdevice.h>
14 #include <linux/if_bridge.h>
15 #include <linux/rculist.h>
16 #include <linux/netdevice.h>
17 #include <linux/rtnetlink.h>
18 #include <net/genetlink.h>
19
20 #include "compat.h"
21 #include "openvswitch/brcompat-netlink.h"
22 #include "brc_procfs.h"
23 #include "brc_sysfs.h"
24 #include "datapath.h"
25 #include "dp_dev.h"
26
27 static struct genl_family brc_genl_family;
28 static struct genl_multicast_group brc_mc_group;
29
30 /* Time to wait for ovs-vswitchd to respond to a datapath action, in
31  * jiffies. */
32 #define BRC_TIMEOUT (HZ * 5)
33
34 /* Mutex to serialize ovs-brcompatd callbacks.  (Some callbacks naturally hold
35  * br_ioctl_mutex, others hold rtnl_lock, but we can't take the former
36  * ourselves and we don't want to hold the latter over a potentially long
37  * period of time.) */
38 static DEFINE_MUTEX(brc_serial);
39
40 /* Userspace communication. */
41 static DEFINE_SPINLOCK(brc_lock);    /* Ensure atomic access to these vars. */
42 static DECLARE_COMPLETION(brc_done); /* Userspace signaled operation done? */
43 static int brc_err;                  /* Error code from userspace. */
44 static u32 brc_seq;                  /* Sequence number for current op. */
45
46 static int brc_send_command(const char *bridge, const char *port, int op);
47
48 static int
49 get_dp_ifindices(int *indices, int num)
50 {
51         int i, index = 0;
52
53         rcu_read_lock();
54         for (i=0; i < ODP_MAX && index < num; i++) {
55                 struct datapath *dp = get_dp(i);
56                 if (!dp)
57                         continue;
58                 indices[index++] = dp->ports[ODPP_LOCAL]->dev->ifindex;
59         }
60         rcu_read_unlock();
61
62         return index;
63 }
64
65 static void
66 get_port_ifindices(struct datapath *dp, int *ifindices, int num)
67 {
68         struct net_bridge_port *p;
69
70         rcu_read_lock();
71         list_for_each_entry_rcu (p, &dp->port_list, node) {
72                 if (p->port_no < num)
73                         ifindices[p->port_no] = p->dev->ifindex;
74         }
75         rcu_read_unlock();
76 }
77
78 static int brc_add_del_bridge(char __user *uname, int add)
79 {
80         char name[IFNAMSIZ];
81
82         if (copy_from_user(name, uname, IFNAMSIZ))
83                 return -EFAULT;
84
85         name[IFNAMSIZ - 1] = 0;
86         return brc_send_command(name, NULL,
87                                 add ? BRC_GENL_C_DP_ADD : BRC_GENL_C_DP_DEL);
88 }
89
90 static int brc_get_bridges(int __user *uindices, int n)
91 {
92         int *indices;
93         int ret;
94
95         if (n >= 2048)
96                 return -ENOMEM;
97
98         indices = kcalloc(n, sizeof(int), GFP_KERNEL);
99         if (indices == NULL)
100                 return -ENOMEM;
101
102         n = get_dp_ifindices(indices, n);
103
104         ret = copy_to_user(uindices, indices, n * sizeof(int)) ? -EFAULT : n;
105
106         kfree(indices);
107         return ret;
108 }
109
110 /* Legacy deviceless bridge ioctl's.  Called with br_ioctl_mutex. */
111 static int
112 old_deviceless(void __user *uarg)
113 {
114         unsigned long args[3];
115
116         if (copy_from_user(args, uarg, sizeof(args)))
117                 return -EFAULT;
118
119         switch (args[0]) {
120         case BRCTL_GET_BRIDGES:
121                 return brc_get_bridges((int __user *)args[1], args[2]);
122
123         case BRCTL_ADD_BRIDGE:
124                 return brc_add_del_bridge((void __user *)args[1], 1);
125         case BRCTL_DEL_BRIDGE:
126                 return brc_add_del_bridge((void __user *)args[1], 0);
127         }
128
129         return -EOPNOTSUPP;
130 }
131
132 /* Called with the br_ioctl_mutex. */
133 static int
134 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
135 brc_ioctl_deviceless_stub(unsigned int cmd, void __user *uarg)
136 #else
137 brc_ioctl_deviceless_stub(struct net *net, unsigned int cmd, void __user *uarg)
138 #endif
139 {
140         switch (cmd) {
141         case SIOCGIFBR:
142         case SIOCSIFBR:
143                 return old_deviceless(uarg);
144
145         case SIOCBRADDBR:
146                 return brc_add_del_bridge(uarg, 1);
147         case SIOCBRDELBR:
148                 return brc_add_del_bridge(uarg, 0);
149         }
150
151         return -EOPNOTSUPP;
152 }
153
154 static int
155 brc_add_del_port(struct net_device *dev, int port_ifindex, int add)
156 {
157         struct net_device *port;
158         char dev_name[IFNAMSIZ], port_name[IFNAMSIZ];
159         int err;
160
161         port = __dev_get_by_index(&init_net, port_ifindex);
162         if (!port)
163                 return -EINVAL;
164
165         /* Save name of dev and port because there's a race between the
166          * rtnl_unlock() and the brc_send_command(). */
167         strcpy(dev_name, dev->name);
168         strcpy(port_name, port->name);
169
170         rtnl_unlock();
171         err = brc_send_command(dev_name, port_name,
172                                add ? BRC_GENL_C_PORT_ADD : BRC_GENL_C_PORT_DEL);
173         rtnl_lock();
174
175         return err;
176 }
177
178 static int
179 brc_get_bridge_info(struct net_device *dev, struct __bridge_info __user *ub)
180 {
181         struct __bridge_info b;
182         u64 id = 0;
183         int i;
184
185         memset(&b, 0, sizeof(struct __bridge_info));
186
187         for (i=0; i<ETH_ALEN; i++)
188                 id |= (u64)dev->dev_addr[i] << (8*(ETH_ALEN-1 - i));
189         b.bridge_id = cpu_to_be64(id);
190         b.stp_enabled = 0;
191
192         if (copy_to_user(ub, &b, sizeof(struct __bridge_info)))
193                 return -EFAULT;
194
195         return 0;
196 }
197
198 static int
199 brc_get_port_list(struct net_device *dev, int __user *uindices, int num)
200 {
201         struct dp_dev *dp_dev = netdev_priv(dev);
202         struct datapath *dp = dp_dev->dp;
203         int *indices;
204
205         if (num < 0)
206                 return -EINVAL;
207         if (num == 0)
208                 num = 256;
209         if (num > DP_MAX_PORTS)
210                 num = DP_MAX_PORTS;
211
212         indices = kcalloc(num, sizeof(int), GFP_KERNEL);
213         if (indices == NULL)
214                 return -ENOMEM;
215
216         get_port_ifindices(dp, indices, num);
217         if (copy_to_user(uindices, indices, num * sizeof(int)))
218                 num = -EFAULT;
219         kfree(indices);
220         return num;
221 }
222
223 /* Legacy ioctl's through SIOCDEVPRIVATE.  Called with rtnl_lock. */
224 static int
225 old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
226 {
227         unsigned long args[4];
228
229         if (copy_from_user(args, rq->ifr_data, sizeof(args)))
230                 return -EFAULT;
231
232         switch (args[0]) {
233         case BRCTL_ADD_IF:
234                 return brc_add_del_port(dev, args[1], 1);
235         case BRCTL_DEL_IF:
236                 return brc_add_del_port(dev, args[1], 0);
237
238         case BRCTL_GET_BRIDGE_INFO:
239                 return brc_get_bridge_info(dev, (struct __bridge_info __user *)args[1]);
240
241         case BRCTL_GET_PORT_LIST:
242                 return brc_get_port_list(dev, (int __user *)args[1], args[2]);
243         }
244
245         return -EOPNOTSUPP;
246 }
247
248 /* Called with the rtnl_lock. */
249 static int
250 brc_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
251 {
252         int err;
253
254         switch (cmd) {
255                 case SIOCDEVPRIVATE:
256                         err = old_dev_ioctl(dev, rq, cmd);
257                         break;
258
259                 case SIOCBRADDIF:
260                         return brc_add_del_port(dev, rq->ifr_ifindex, 1);
261                 case SIOCBRDELIF:
262                         return brc_add_del_port(dev, rq->ifr_ifindex, 0);
263
264                 default:
265                         err = -EOPNOTSUPP;
266                         break;
267         }
268
269         return err;
270 }
271
272
273 static struct genl_family brc_genl_family = {
274         .id = GENL_ID_GENERATE,
275         .hdrsize = 0,
276         .name = BRC_GENL_FAMILY_NAME,
277         .version = 1,
278         .maxattr = BRC_GENL_A_MAX,
279 };
280
281 static int brc_genl_query(struct sk_buff *skb, struct genl_info *info)
282 {
283         int err = -EINVAL;
284         struct sk_buff *ans_skb;
285         void *data;
286
287         ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
288         if (!ans_skb) 
289                 return -ENOMEM;
290
291         data = genlmsg_put_reply(ans_skb, info, &brc_genl_family,
292                                  0, BRC_GENL_C_QUERY_MC);
293         if (data == NULL) {
294                 err = -ENOMEM;
295                 goto err;
296         }
297         NLA_PUT_U32(ans_skb, BRC_GENL_A_MC_GROUP, brc_mc_group.id);
298
299         genlmsg_end(ans_skb, data);
300         return genlmsg_reply(ans_skb, info);
301
302 err:
303 nla_put_failure:
304         kfree_skb(ans_skb);
305         return err;
306 }
307
308 static struct genl_ops brc_genl_ops_query_dp = {
309         .cmd = BRC_GENL_C_QUERY_MC,
310         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
311         .policy = NULL,
312         .doit = brc_genl_query,
313         .dumpit = NULL
314 };
315
316 /* Attribute policy: what each attribute may contain.  */
317 static struct nla_policy brc_genl_policy[BRC_GENL_A_MAX + 1] = {
318         [BRC_GENL_A_ERR_CODE] = { .type = NLA_U32 },
319         [BRC_GENL_A_PROC_DIR] = { .type = NLA_NUL_STRING },
320         [BRC_GENL_A_PROC_NAME] = { .type = NLA_NUL_STRING },
321         [BRC_GENL_A_PROC_DATA] = { .type = NLA_NUL_STRING },
322 };
323
324 static int
325 brc_genl_dp_result(struct sk_buff *skb, struct genl_info *info)
326 {
327         unsigned long int flags;
328         int err;
329
330         if (!info->attrs[BRC_GENL_A_ERR_CODE])
331                 return -EINVAL;
332
333         spin_lock_irqsave(&brc_lock, flags);
334         if (brc_seq == info->snd_seq) {
335                 brc_err = nla_get_u32(info->attrs[BRC_GENL_A_ERR_CODE]);
336                 complete(&brc_done);
337                 err = 0;
338         } else {
339                 err = -ESTALE;
340         }
341         spin_unlock_irqrestore(&brc_lock, flags);
342
343         return err;
344 }
345
346 static struct genl_ops brc_genl_ops_dp_result = {
347         .cmd = BRC_GENL_C_DP_RESULT,
348         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
349         .policy = brc_genl_policy,
350         .doit = brc_genl_dp_result,
351         .dumpit = NULL
352 };
353
354 static struct genl_ops brc_genl_ops_set_proc = {
355         .cmd = BRC_GENL_C_SET_PROC,
356         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
357         .policy = brc_genl_policy,
358         .doit = brc_genl_set_proc,
359         .dumpit = NULL
360 };
361
362 static int brc_send_command(const char *bridge, const char *port, int op)
363 {
364         unsigned long int flags;
365         struct sk_buff *skb;
366         void *data;
367         int error;
368
369         mutex_lock(&brc_serial);
370
371         /* Increment sequence number first, so that we ignore any replies
372          * to stale requests. */
373         spin_lock_irqsave(&brc_lock, flags);
374         brc_seq++;
375         INIT_COMPLETION(brc_done);
376         spin_unlock_irqrestore(&brc_lock, flags);
377
378         /* Compose message. */
379         skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
380         error = -ENOMEM;
381         if (skb == NULL)
382                 goto exit_unlock;
383         data = genlmsg_put(skb, 0, brc_seq, &brc_genl_family, 0, op);
384
385         NLA_PUT_STRING(skb, BRC_GENL_A_DP_NAME, bridge);
386         if (port)
387                 NLA_PUT_STRING(skb, BRC_GENL_A_PORT_NAME, port);
388
389         genlmsg_end(skb, data);
390
391         /* Send message. */
392         error = genlmsg_multicast(skb, 0, brc_mc_group.id, GFP_KERNEL);
393         if (error < 0)
394                 goto exit_unlock;
395
396         /* Wait for reply. */
397         error = -ETIMEDOUT;
398         if (!wait_for_completion_timeout(&brc_done, BRC_TIMEOUT))
399                 goto exit_unlock;
400
401         error = -brc_err;
402         goto exit_unlock;
403
404 nla_put_failure:
405         kfree_skb(skb);
406 exit_unlock:
407         mutex_unlock(&brc_serial);
408         return error;
409 }
410
411 int brc_add_dp(struct datapath *dp)
412 {
413         if (!try_module_get(THIS_MODULE))
414                 return -ENODEV;
415 #ifdef SUPPORT_SYSFS
416         brc_sysfs_add_dp(dp);
417 #endif
418
419         return 0;
420 }
421
422 int brc_del_dp(struct datapath *dp) 
423 {
424 #ifdef SUPPORT_SYSFS
425         brc_sysfs_del_dp(dp);
426 #endif
427         module_put(THIS_MODULE);
428
429         return 0;
430 }
431
432 static int 
433 __init brc_init(void)
434 {
435         int i;
436         int err;
437
438         printk("Open vSwitch Bridge Compatibility, built "__DATE__" "__TIME__"\n");
439
440         rcu_read_lock();
441         for (i=0; i<ODP_MAX; i++) {
442                 if (get_dp(i)) {
443                         rcu_read_unlock();
444                         printk(KERN_EMERG "brcompat: no datapaths may exist!\n");
445                         return -EEXIST;
446                 }
447         }
448         rcu_read_unlock();
449
450         /* Set the bridge ioctl handler */
451         brioctl_set(brc_ioctl_deviceless_stub);
452
453         /* Set the openvswitch_mod device ioctl handler */
454         dp_ioctl_hook = brc_dev_ioctl;
455
456         /* Register hooks for datapath adds and deletes */
457         dp_add_dp_hook = brc_add_dp;
458         dp_del_dp_hook = brc_del_dp;
459
460         /* Register hooks for interface adds and deletes */
461 #ifdef SUPPORT_SYSFS
462         dp_add_if_hook = brc_sysfs_add_if;
463         dp_del_if_hook = brc_sysfs_del_if;
464 #endif
465
466         /* Randomize the initial sequence number.  This is not a security
467          * feature; it only helps avoid crossed wires between userspace and
468          * the kernel when the module is unloaded and reloaded. */
469         brc_seq = net_random();
470
471         /* Register generic netlink family to communicate changes to
472          * userspace. */
473         err = genl_register_family(&brc_genl_family);
474         if (err)
475                 goto error;
476
477         err = genl_register_ops(&brc_genl_family, &brc_genl_ops_query_dp);
478         if (err != 0) 
479                 goto err_unregister;
480
481         err = genl_register_ops(&brc_genl_family, &brc_genl_ops_dp_result);
482         if (err != 0) 
483                 goto err_unregister;
484
485         err = genl_register_ops(&brc_genl_family, &brc_genl_ops_set_proc);
486         if (err != 0) 
487                 goto err_unregister;
488
489         strcpy(brc_mc_group.name, "brcompat");
490         err = genl_register_mc_group(&brc_genl_family, &brc_mc_group);
491         if (err < 0)
492                 goto err_unregister;
493
494         return 0;
495
496 err_unregister:
497         genl_unregister_family(&brc_genl_family);
498 error:
499         printk(KERN_EMERG "brcompat: failed to install!");
500         return err;
501 }
502
503 static void 
504 brc_cleanup(void)
505 {
506         /* Unregister hooks for datapath adds and deletes */
507         dp_add_dp_hook = NULL;
508         dp_del_dp_hook = NULL;
509         
510         /* Unregister hooks for interface adds and deletes */
511         dp_add_if_hook = NULL;
512         dp_del_if_hook = NULL;
513
514         /* Unregister ioctl hooks */
515         dp_ioctl_hook = NULL;
516         brioctl_set(NULL);
517
518         genl_unregister_family(&brc_genl_family);
519         brc_procfs_exit();
520 }
521
522 module_init(brc_init);
523 module_exit(brc_cleanup);
524
525 MODULE_DESCRIPTION("Open vSwitch bridge compatibility");
526 MODULE_AUTHOR("Nicira Networks");
527 MODULE_LICENSE("GPL");