1 #include <linux/kernel.h>
2 #include <asm/uaccess.h>
3 #include <linux/completion.h>
4 #include <linux/delay.h>
5 #include <linux/etherdevice.h>
6 #include <linux/if_bridge.h>
7 #include <linux/rculist.h>
8 #include <linux/netdevice.h>
9 #include <linux/rtnetlink.h>
10 #include <net/genetlink.h>
13 #include "openvswitch/brcompat-netlink.h"
14 #include "brc_procfs.h"
15 #include "brc_sysfs.h"
19 static struct genl_family brc_genl_family;
20 static struct genl_multicast_group brc_mc_group;
22 /* Time to wait for ovs-vswitchd to respond to a datapath action, in
24 #define BRC_TIMEOUT (HZ * 5)
26 /* Mutex to serialize ovs-brcompatd callbacks. (Some callbacks naturally hold
27 * br_ioctl_mutex, others hold rtnl_lock, but we can't take the former
28 * ourselves and we don't want to hold the latter over a potentially long
30 static DEFINE_MUTEX(brc_serial);
32 /* Userspace communication. */
33 static DEFINE_SPINLOCK(brc_lock); /* Ensure atomic access to these vars. */
34 static DECLARE_COMPLETION(brc_done); /* Userspace signaled operation done? */
35 static int brc_err; /* Error code from userspace. */
36 static u32 brc_seq; /* Sequence number for current op. */
38 static int brc_send_command(const char *bridge, const char *port, int op);
41 get_dp_ifindices(int *indices, int num)
46 for (i=0; i < ODP_MAX && index < num; i++) {
47 struct datapath *dp = get_dp(i);
50 indices[index++] = dp->ports[ODPP_LOCAL]->dev->ifindex;
58 get_port_ifindices(struct datapath *dp, int *ifindices, int num)
60 struct net_bridge_port *p;
63 list_for_each_entry_rcu (p, &dp->port_list, node) {
65 ifindices[p->port_no] = p->dev->ifindex;
70 static int brc_add_del_bridge(char __user *uname, int add)
74 if (copy_from_user(name, uname, IFNAMSIZ))
77 name[IFNAMSIZ - 1] = 0;
78 return brc_send_command(name, NULL,
79 add ? BRC_GENL_C_DP_ADD : BRC_GENL_C_DP_DEL);
82 static int brc_get_bridges(int __user *uindices, int n)
90 indices = kcalloc(n, sizeof(int), GFP_KERNEL);
94 n = get_dp_ifindices(indices, n);
96 ret = copy_to_user(uindices, indices, n * sizeof(int)) ? -EFAULT : n;
102 /* Legacy deviceless bridge ioctl's. Called with br_ioctl_mutex. */
104 old_deviceless(void __user *uarg)
106 unsigned long args[3];
108 if (copy_from_user(args, uarg, sizeof(args)))
112 case BRCTL_GET_BRIDGES:
113 return brc_get_bridges((int __user *)args[1], args[2]);
115 case BRCTL_ADD_BRIDGE:
116 return brc_add_del_bridge((void __user *)args[1], 1);
117 case BRCTL_DEL_BRIDGE:
118 return brc_add_del_bridge((void __user *)args[1], 0);
124 /* Called with the br_ioctl_mutex. */
126 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
127 brc_ioctl_deviceless_stub(unsigned int cmd, void __user *uarg)
129 brc_ioctl_deviceless_stub(struct net *net, unsigned int cmd, void __user *uarg)
135 return old_deviceless(uarg);
138 return brc_add_del_bridge(uarg, 1);
140 return brc_add_del_bridge(uarg, 0);
147 brc_add_del_port(struct net_device *dev, int port_ifindex, int add)
149 struct net_device *port;
150 char dev_name[IFNAMSIZ], port_name[IFNAMSIZ];
153 port = __dev_get_by_index(&init_net, port_ifindex);
157 /* Save name of dev and port because there's a race between the
158 * rtnl_unlock() and the brc_send_command(). */
159 strcpy(dev_name, dev->name);
160 strcpy(port_name, port->name);
163 err = brc_send_command(dev_name, port_name,
164 add ? BRC_GENL_C_PORT_ADD : BRC_GENL_C_PORT_DEL);
171 brc_get_bridge_info(struct net_device *dev, struct __bridge_info __user *ub)
173 struct __bridge_info b;
177 memset(&b, 0, sizeof(struct __bridge_info));
179 for (i=0; i<ETH_ALEN; i++)
180 id |= (u64)dev->dev_addr[i] << (8*(ETH_ALEN-1 - i));
181 b.bridge_id = cpu_to_be64(id);
184 if (copy_to_user(ub, &b, sizeof(struct __bridge_info)))
191 brc_get_port_list(struct net_device *dev, int __user *uindices, int num)
193 struct dp_dev *dp_dev = netdev_priv(dev);
194 struct datapath *dp = dp_dev->dp;
201 if (num > DP_MAX_PORTS)
204 indices = kcalloc(num, sizeof(int), GFP_KERNEL);
208 get_port_ifindices(dp, indices, num);
209 if (copy_to_user(uindices, indices, num * sizeof(int)))
215 /* Legacy ioctl's through SIOCDEVPRIVATE. Called with rtnl_lock. */
217 old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
219 unsigned long args[4];
221 if (copy_from_user(args, rq->ifr_data, sizeof(args)))
226 return brc_add_del_port(dev, args[1], 1);
228 return brc_add_del_port(dev, args[1], 0);
230 case BRCTL_GET_BRIDGE_INFO:
231 return brc_get_bridge_info(dev, (struct __bridge_info __user *)args[1]);
233 case BRCTL_GET_PORT_LIST:
234 return brc_get_port_list(dev, (int __user *)args[1], args[2]);
240 /* Called with the rtnl_lock. */
242 brc_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
248 err = old_dev_ioctl(dev, rq, cmd);
252 return brc_add_del_port(dev, rq->ifr_ifindex, 1);
254 return brc_add_del_port(dev, rq->ifr_ifindex, 0);
265 static struct genl_family brc_genl_family = {
266 .id = GENL_ID_GENERATE,
268 .name = BRC_GENL_FAMILY_NAME,
270 .maxattr = BRC_GENL_A_MAX,
273 static int brc_genl_query(struct sk_buff *skb, struct genl_info *info)
276 struct sk_buff *ans_skb;
279 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
283 data = genlmsg_put_reply(ans_skb, info, &brc_genl_family,
284 0, BRC_GENL_C_QUERY_MC);
289 NLA_PUT_U32(ans_skb, BRC_GENL_A_MC_GROUP, brc_mc_group.id);
291 genlmsg_end(ans_skb, data);
292 return genlmsg_reply(ans_skb, info);
300 static struct genl_ops brc_genl_ops_query_dp = {
301 .cmd = BRC_GENL_C_QUERY_MC,
302 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
304 .doit = brc_genl_query,
308 /* Attribute policy: what each attribute may contain. */
309 static struct nla_policy brc_genl_policy[BRC_GENL_A_MAX + 1] = {
310 [BRC_GENL_A_ERR_CODE] = { .type = NLA_U32 },
311 [BRC_GENL_A_PROC_DIR] = { .type = NLA_NUL_STRING },
312 [BRC_GENL_A_PROC_NAME] = { .type = NLA_NUL_STRING },
313 [BRC_GENL_A_PROC_DATA] = { .type = NLA_NUL_STRING },
317 brc_genl_dp_result(struct sk_buff *skb, struct genl_info *info)
319 unsigned long int flags;
322 if (!info->attrs[BRC_GENL_A_ERR_CODE])
325 spin_lock_irqsave(&brc_lock, flags);
326 if (brc_seq == info->snd_seq) {
327 brc_err = nla_get_u32(info->attrs[BRC_GENL_A_ERR_CODE]);
333 spin_unlock_irqrestore(&brc_lock, flags);
338 static struct genl_ops brc_genl_ops_dp_result = {
339 .cmd = BRC_GENL_C_DP_RESULT,
340 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
341 .policy = brc_genl_policy,
342 .doit = brc_genl_dp_result,
346 static struct genl_ops brc_genl_ops_set_proc = {
347 .cmd = BRC_GENL_C_SET_PROC,
348 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
349 .policy = brc_genl_policy,
350 .doit = brc_genl_set_proc,
354 static int brc_send_command(const char *bridge, const char *port, int op)
356 unsigned long int flags;
361 mutex_lock(&brc_serial);
363 /* Increment sequence number first, so that we ignore any replies
364 * to stale requests. */
365 spin_lock_irqsave(&brc_lock, flags);
367 INIT_COMPLETION(brc_done);
368 spin_unlock_irqrestore(&brc_lock, flags);
370 /* Compose message. */
371 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
375 data = genlmsg_put(skb, 0, brc_seq, &brc_genl_family, 0, op);
377 NLA_PUT_STRING(skb, BRC_GENL_A_DP_NAME, bridge);
379 NLA_PUT_STRING(skb, BRC_GENL_A_PORT_NAME, port);
381 genlmsg_end(skb, data);
384 error = genlmsg_multicast(skb, 0, brc_mc_group.id, GFP_KERNEL);
388 /* Wait for reply. */
390 if (!wait_for_completion_timeout(&brc_done, BRC_TIMEOUT))
399 mutex_unlock(&brc_serial);
403 int brc_add_dp(struct datapath *dp)
405 if (!try_module_get(THIS_MODULE))
408 brc_sysfs_add_dp(dp);
414 int brc_del_dp(struct datapath *dp)
417 brc_sysfs_del_dp(dp);
419 module_put(THIS_MODULE);
425 __init brc_init(void)
430 printk("Open vSwitch Bridge Compatibility, built "__DATE__" "__TIME__"\n");
433 for (i=0; i<ODP_MAX; i++) {
436 printk(KERN_EMERG "brcompat: no datapaths may exist!\n");
442 /* Set the bridge ioctl handler */
443 brioctl_set(brc_ioctl_deviceless_stub);
445 /* Set the openvswitch_mod device ioctl handler */
446 dp_ioctl_hook = brc_dev_ioctl;
448 /* Register hooks for datapath adds and deletes */
449 dp_add_dp_hook = brc_add_dp;
450 dp_del_dp_hook = brc_del_dp;
452 /* Register hooks for interface adds and deletes */
454 dp_add_if_hook = brc_sysfs_add_if;
455 dp_del_if_hook = brc_sysfs_del_if;
458 /* Randomize the initial sequence number. This is not a security
459 * feature; it only helps avoid crossed wires between userspace and
460 * the kernel when the module is unloaded and reloaded. */
461 brc_seq = net_random();
463 /* Register generic netlink family to communicate changes to
465 err = genl_register_family(&brc_genl_family);
469 err = genl_register_ops(&brc_genl_family, &brc_genl_ops_query_dp);
473 err = genl_register_ops(&brc_genl_family, &brc_genl_ops_dp_result);
477 err = genl_register_ops(&brc_genl_family, &brc_genl_ops_set_proc);
481 strcpy(brc_mc_group.name, "brcompat");
482 err = genl_register_mc_group(&brc_genl_family, &brc_mc_group);
489 genl_unregister_family(&brc_genl_family);
491 printk(KERN_EMERG "brcompat: failed to install!");
498 /* Unregister hooks for datapath adds and deletes */
499 dp_add_dp_hook = NULL;
500 dp_del_dp_hook = NULL;
502 /* Unregister hooks for interface adds and deletes */
503 dp_add_if_hook = NULL;
504 dp_del_if_hook = NULL;
506 /* Unregister ioctl hooks */
507 dp_ioctl_hook = NULL;
510 genl_unregister_family(&brc_genl_family);
514 module_init(brc_init);
515 module_exit(brc_cleanup);
517 MODULE_DESCRIPTION("Open vSwitch bridge compatibility");
518 MODULE_AUTHOR("Nicira Networks");
519 MODULE_LICENSE("GPL");