2 * Copyright (c) 2009 Nicira Networks.
3 * Distributed under the terms of the GNU GPL version 2.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
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>
21 #include "openvswitch/brcompat-netlink.h"
22 #include "brc_procfs.h"
23 #include "brc_sysfs.h"
27 static struct genl_family brc_genl_family;
28 static struct genl_multicast_group brc_mc_group;
30 /* Time to wait for ovs-vswitchd to respond to a datapath action, in
32 #define BRC_TIMEOUT (HZ * 5)
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
38 static DEFINE_MUTEX(brc_serial);
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 struct sk_buff *brc_reply; /* Reply from userspace. */
44 static u32 brc_seq; /* Sequence number for current op. */
46 static struct sk_buff *brc_send_command(struct sk_buff *, struct nlattr **attrs);
47 static int brc_send_simple_command(struct sk_buff *);
50 get_dp_ifindices(int *indices, int num)
55 for (i=0; i < ODP_MAX && index < num; i++) {
56 struct datapath *dp = get_dp(i);
59 indices[index++] = dp->ports[ODPP_LOCAL]->dev->ifindex;
67 get_port_ifindices(struct datapath *dp, int *ifindices, int num)
69 struct net_bridge_port *p;
72 list_for_each_entry_rcu (p, &dp->port_list, node) {
74 ifindices[p->port_no] = p->dev->ifindex;
79 static struct sk_buff *
80 brc_make_request(int op, const char *bridge, const char *port)
82 struct sk_buff *skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
86 genlmsg_put(skb, 0, 0, &brc_genl_family, 0, op);
87 NLA_PUT_STRING(skb, BRC_GENL_A_DP_NAME, bridge);
89 NLA_PUT_STRING(skb, BRC_GENL_A_PORT_NAME, port);
98 static int brc_send_simple_command(struct sk_buff *request)
100 struct nlattr *attrs[BRC_GENL_A_MAX + 1];
101 struct sk_buff *reply;
104 reply = brc_send_command(request, attrs);
106 return PTR_ERR(reply);
108 error = nla_get_u32(attrs[BRC_GENL_A_ERR_CODE]);
113 static int brc_add_del_bridge(char __user *uname, int add)
115 struct sk_buff *request;
118 if (copy_from_user(name, uname, IFNAMSIZ))
121 name[IFNAMSIZ - 1] = 0;
122 request = brc_make_request(add ? BRC_GENL_C_DP_ADD : BRC_GENL_C_DP_DEL,
127 return brc_send_simple_command(request);
130 static int brc_get_bridges(int __user *uindices, int n)
138 indices = kcalloc(n, sizeof(int), GFP_KERNEL);
142 n = get_dp_ifindices(indices, n);
144 ret = copy_to_user(uindices, indices, n * sizeof(int)) ? -EFAULT : n;
150 /* Legacy deviceless bridge ioctl's. Called with br_ioctl_mutex. */
152 old_deviceless(void __user *uarg)
154 unsigned long args[3];
156 if (copy_from_user(args, uarg, sizeof(args)))
160 case BRCTL_GET_BRIDGES:
161 return brc_get_bridges((int __user *)args[1], args[2]);
163 case BRCTL_ADD_BRIDGE:
164 return brc_add_del_bridge((void __user *)args[1], 1);
165 case BRCTL_DEL_BRIDGE:
166 return brc_add_del_bridge((void __user *)args[1], 0);
172 /* Called with the br_ioctl_mutex. */
174 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
175 brc_ioctl_deviceless_stub(unsigned int cmd, void __user *uarg)
177 brc_ioctl_deviceless_stub(struct net *net, unsigned int cmd, void __user *uarg)
183 return old_deviceless(uarg);
186 return brc_add_del_bridge(uarg, 1);
188 return brc_add_del_bridge(uarg, 0);
195 brc_add_del_port(struct net_device *dev, int port_ifindex, int add)
197 struct sk_buff *request;
198 struct net_device *port;
201 port = __dev_get_by_index(&init_net, port_ifindex);
205 /* Save name of dev and port because there's a race between the
206 * rtnl_unlock() and the brc_send_simple_command(). */
207 request = brc_make_request(add ? BRC_GENL_C_PORT_ADD : BRC_GENL_C_PORT_DEL,
208 dev->name, port->name);
213 err = brc_send_simple_command(request);
220 brc_get_bridge_info(struct net_device *dev, struct __bridge_info __user *ub)
222 struct __bridge_info b;
226 memset(&b, 0, sizeof(struct __bridge_info));
228 for (i=0; i<ETH_ALEN; i++)
229 id |= (u64)dev->dev_addr[i] << (8*(ETH_ALEN-1 - i));
230 b.bridge_id = cpu_to_be64(id);
233 if (copy_to_user(ub, &b, sizeof(struct __bridge_info)))
240 brc_get_port_list(struct net_device *dev, int __user *uindices, int num)
242 struct dp_dev *dp_dev = netdev_priv(dev);
243 struct datapath *dp = dp_dev->dp;
250 if (num > DP_MAX_PORTS)
253 indices = kcalloc(num, sizeof(int), GFP_KERNEL);
257 get_port_ifindices(dp, indices, num);
258 if (copy_to_user(uindices, indices, num * sizeof(int)))
265 * Format up to a page worth of forwarding table entries
266 * userbuf -- where to copy result
267 * maxnum -- maximum number of entries desired
268 * (limited to a page for sanity)
269 * offset -- number of records to skip
271 static int brc_get_fdb_entries(struct net_device *dev, void __user *userbuf,
272 unsigned long maxnum, unsigned long offset)
274 struct nlattr *attrs[BRC_GENL_A_MAX + 1];
275 struct sk_buff *request, *reply;
279 /* Clamp size to PAGE_SIZE, test maxnum to avoid overflow */
280 if (maxnum > PAGE_SIZE/sizeof(struct __fdb_entry))
281 maxnum = PAGE_SIZE/sizeof(struct __fdb_entry);
283 request = brc_make_request(BRC_GENL_C_FDB_QUERY, dev->name, NULL);
286 NLA_PUT_U64(request, BRC_GENL_A_FDB_COUNT, maxnum);
287 NLA_PUT_U64(request, BRC_GENL_A_FDB_SKIP, offset);
290 reply = brc_send_command(request, attrs);
291 retval = PTR_ERR(reply);
295 retval = -nla_get_u32(attrs[BRC_GENL_A_ERR_CODE]);
300 if (!attrs[BRC_GENL_A_FDB_DATA])
302 len = nla_len(attrs[BRC_GENL_A_FDB_DATA]);
303 if (len % sizeof(struct __fdb_entry) ||
304 len / sizeof(struct __fdb_entry) > maxnum)
307 retval = len / sizeof(struct __fdb_entry);
308 if (copy_to_user(userbuf, nla_data(attrs[BRC_GENL_A_FDB_DATA]), len))
322 /* Legacy ioctl's through SIOCDEVPRIVATE. Called with rtnl_lock. */
324 old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
326 unsigned long args[4];
328 if (copy_from_user(args, rq->ifr_data, sizeof(args)))
333 return brc_add_del_port(dev, args[1], 1);
335 return brc_add_del_port(dev, args[1], 0);
337 case BRCTL_GET_BRIDGE_INFO:
338 return brc_get_bridge_info(dev, (struct __bridge_info __user *)args[1]);
340 case BRCTL_GET_PORT_LIST:
341 return brc_get_port_list(dev, (int __user *)args[1], args[2]);
343 case BRCTL_GET_FDB_ENTRIES:
344 return brc_get_fdb_entries(dev, (void __user *)args[1],
351 /* Called with the rtnl_lock. */
353 brc_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
359 err = old_dev_ioctl(dev, rq, cmd);
363 return brc_add_del_port(dev, rq->ifr_ifindex, 1);
365 return brc_add_del_port(dev, rq->ifr_ifindex, 0);
376 static struct genl_family brc_genl_family = {
377 .id = GENL_ID_GENERATE,
379 .name = BRC_GENL_FAMILY_NAME,
381 .maxattr = BRC_GENL_A_MAX,
384 static int brc_genl_query(struct sk_buff *skb, struct genl_info *info)
387 struct sk_buff *ans_skb;
390 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
394 data = genlmsg_put_reply(ans_skb, info, &brc_genl_family,
395 0, BRC_GENL_C_QUERY_MC);
400 NLA_PUT_U32(ans_skb, BRC_GENL_A_MC_GROUP, brc_mc_group.id);
402 genlmsg_end(ans_skb, data);
403 return genlmsg_reply(ans_skb, info);
411 static struct genl_ops brc_genl_ops_query_dp = {
412 .cmd = BRC_GENL_C_QUERY_MC,
413 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
415 .doit = brc_genl_query,
419 /* Attribute policy: what each attribute may contain. */
420 static struct nla_policy brc_genl_policy[BRC_GENL_A_MAX + 1] = {
421 [BRC_GENL_A_ERR_CODE] = { .type = NLA_U32 },
423 [BRC_GENL_A_PROC_DIR] = { .type = NLA_NUL_STRING },
424 [BRC_GENL_A_PROC_NAME] = { .type = NLA_NUL_STRING },
425 [BRC_GENL_A_PROC_DATA] = { .type = NLA_NUL_STRING },
427 [BRC_GENL_A_FDB_DATA] = { .type = NLA_UNSPEC },
431 brc_genl_dp_result(struct sk_buff *skb, struct genl_info *info)
433 unsigned long int flags;
436 if (!info->attrs[BRC_GENL_A_ERR_CODE])
439 skb = skb_clone(skb, GFP_KERNEL);
443 spin_lock_irqsave(&brc_lock, flags);
444 if (brc_seq == info->snd_seq) {
448 kfree_skb(brc_reply);
457 spin_unlock_irqrestore(&brc_lock, flags);
462 static struct genl_ops brc_genl_ops_dp_result = {
463 .cmd = BRC_GENL_C_DP_RESULT,
464 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
465 .policy = brc_genl_policy,
466 .doit = brc_genl_dp_result,
470 static struct genl_ops brc_genl_ops_set_proc = {
471 .cmd = BRC_GENL_C_SET_PROC,
472 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
473 .policy = brc_genl_policy,
474 .doit = brc_genl_set_proc,
478 static struct sk_buff *brc_send_command(struct sk_buff *request, struct nlattr **attrs)
480 unsigned long int flags;
481 struct sk_buff *reply;
484 mutex_lock(&brc_serial);
486 /* Increment sequence number first, so that we ignore any replies
487 * to stale requests. */
488 spin_lock_irqsave(&brc_lock, flags);
489 nlmsg_hdr(request)->nlmsg_seq = ++brc_seq;
490 INIT_COMPLETION(brc_done);
491 spin_unlock_irqrestore(&brc_lock, flags);
493 nlmsg_end(request, nlmsg_hdr(request));
496 error = genlmsg_multicast(request, 0, brc_mc_group.id, GFP_KERNEL);
500 /* Wait for reply. */
502 if (!wait_for_completion_timeout(&brc_done, BRC_TIMEOUT))
506 spin_lock_irqsave(&brc_lock, flags);
509 spin_unlock_irqrestore(&brc_lock, flags);
511 mutex_unlock(&brc_serial);
513 /* Re-parse message. Can't fail, since it parsed correctly once
515 error = nlmsg_parse(nlmsg_hdr(reply), GENL_HDRLEN,
516 attrs, BRC_GENL_A_MAX, brc_genl_policy);
522 mutex_unlock(&brc_serial);
523 return ERR_PTR(error);
526 int brc_add_dp(struct datapath *dp)
528 if (!try_module_get(THIS_MODULE))
531 brc_sysfs_add_dp(dp);
537 int brc_del_dp(struct datapath *dp)
540 brc_sysfs_del_dp(dp);
542 module_put(THIS_MODULE);
548 __init brc_init(void)
553 printk("Open vSwitch Bridge Compatibility, built "__DATE__" "__TIME__"\n");
556 for (i=0; i<ODP_MAX; i++) {
559 printk(KERN_EMERG "brcompat: no datapaths may exist!\n");
565 /* Set the bridge ioctl handler */
566 brioctl_set(brc_ioctl_deviceless_stub);
568 /* Set the openvswitch_mod device ioctl handler */
569 dp_ioctl_hook = brc_dev_ioctl;
571 /* Register hooks for datapath adds and deletes */
572 dp_add_dp_hook = brc_add_dp;
573 dp_del_dp_hook = brc_del_dp;
575 /* Register hooks for interface adds and deletes */
577 dp_add_if_hook = brc_sysfs_add_if;
578 dp_del_if_hook = brc_sysfs_del_if;
581 /* Randomize the initial sequence number. This is not a security
582 * feature; it only helps avoid crossed wires between userspace and
583 * the kernel when the module is unloaded and reloaded. */
584 brc_seq = net_random();
586 /* Register generic netlink family to communicate changes to
588 err = genl_register_family(&brc_genl_family);
592 err = genl_register_ops(&brc_genl_family, &brc_genl_ops_query_dp);
596 err = genl_register_ops(&brc_genl_family, &brc_genl_ops_dp_result);
600 err = genl_register_ops(&brc_genl_family, &brc_genl_ops_set_proc);
604 strcpy(brc_mc_group.name, "brcompat");
605 err = genl_register_mc_group(&brc_genl_family, &brc_mc_group);
612 genl_unregister_family(&brc_genl_family);
614 printk(KERN_EMERG "brcompat: failed to install!");
621 /* Unregister hooks for datapath adds and deletes */
622 dp_add_dp_hook = NULL;
623 dp_del_dp_hook = NULL;
625 /* Unregister hooks for interface adds and deletes */
626 dp_add_if_hook = NULL;
627 dp_del_if_hook = NULL;
629 /* Unregister ioctl hooks */
630 dp_ioctl_hook = NULL;
633 genl_unregister_family(&brc_genl_family);
637 module_init(brc_init);
638 module_exit(brc_cleanup);
640 MODULE_DESCRIPTION("Open vSwitch bridge compatibility");
641 MODULE_AUTHOR("Nicira Networks");
642 MODULE_LICENSE("GPL");