2 * Copyright (c) 2007-2012 Nicira Networks.
4 * Significant portions of this file may be copied from parts of the Linux
5 * kernel, by Linus Torvalds and others.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/kernel.h>
11 #include <asm/uaccess.h>
12 #include <linux/completion.h>
13 #include <linux/etherdevice.h>
14 #include <linux/if_bridge.h>
15 #include <linux/netdevice.h>
16 #include <linux/rtnetlink.h>
17 #include <net/genetlink.h>
19 #include "openvswitch/brcompat-netlink.h"
22 static struct genl_family brc_genl_family;
23 static struct genl_multicast_group brc_mc_group;
25 /* Time to wait for ovs-vswitchd to respond to a datapath action, in
27 #define BRC_TIMEOUT (HZ * 5)
29 /* Mutex to serialize ovs-brcompatd callbacks. (Some callbacks naturally hold
30 * br_ioctl_mutex, others hold rtnl_lock, but we can't take the former
31 * ourselves and we don't want to hold the latter over a potentially long
33 static DEFINE_MUTEX(brc_serial);
35 /* Userspace communication. */
36 static DEFINE_SPINLOCK(brc_lock); /* Ensure atomic access to these vars. */
37 static DECLARE_COMPLETION(brc_done); /* Userspace signaled operation done? */
38 static struct sk_buff *brc_reply; /* Reply from userspace. */
39 static u32 brc_seq; /* Sequence number for current op. */
41 static struct sk_buff *brc_send_command(struct sk_buff *, struct nlattr **attrs);
42 static int brc_send_simple_command(struct sk_buff *);
44 static struct sk_buff *brc_make_request(int op, const char *bridge,
47 struct sk_buff *skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
51 genlmsg_put(skb, 0, 0, &brc_genl_family, 0, op);
53 NLA_PUT_STRING(skb, BRC_GENL_A_DP_NAME, bridge);
55 NLA_PUT_STRING(skb, BRC_GENL_A_PORT_NAME, port);
64 static int brc_send_simple_command(struct sk_buff *request)
66 struct nlattr *attrs[BRC_GENL_A_MAX + 1];
67 struct sk_buff *reply;
70 reply = brc_send_command(request, attrs);
72 return PTR_ERR(reply);
74 error = nla_get_u32(attrs[BRC_GENL_A_ERR_CODE]);
79 static int brc_add_del_bridge(char __user *uname, int add)
81 struct sk_buff *request;
84 if (!capable(CAP_NET_ADMIN))
87 if (copy_from_user(name, uname, IFNAMSIZ))
90 name[IFNAMSIZ - 1] = 0;
91 request = brc_make_request(add ? BRC_GENL_C_DP_ADD : BRC_GENL_C_DP_DEL,
96 return brc_send_simple_command(request);
99 static int brc_get_indices(int op, const char *br_name,
100 int __user *uindices, int n)
102 struct nlattr *attrs[BRC_GENL_A_MAX + 1];
103 struct sk_buff *request, *reply;
113 request = brc_make_request(op, br_name, NULL);
117 reply = brc_send_command(request, attrs);
118 ret = PTR_ERR(reply);
122 ret = -nla_get_u32(attrs[BRC_GENL_A_ERR_CODE]);
127 if (!attrs[BRC_GENL_A_IFINDEXES])
130 len = nla_len(attrs[BRC_GENL_A_IFINDEXES]);
131 indices = nla_data(attrs[BRC_GENL_A_IFINDEXES]);
132 if (len % sizeof(int))
135 n = min_t(int, n, len / sizeof(int));
136 ret = copy_to_user(uindices, indices, n * sizeof(int)) ? -EFAULT : n;
144 /* Called with br_ioctl_mutex. */
145 static int brc_get_bridges(int __user *uindices, int n)
147 return brc_get_indices(BRC_GENL_C_GET_BRIDGES, NULL, uindices, n);
150 /* Legacy deviceless bridge ioctl's. Called with br_ioctl_mutex. */
151 static int old_deviceless(void __user *uarg)
153 unsigned long args[3];
155 if (copy_from_user(args, uarg, sizeof(args)))
159 case BRCTL_GET_BRIDGES:
160 return brc_get_bridges((int __user *)args[1], args[2]);
162 case BRCTL_ADD_BRIDGE:
163 return brc_add_del_bridge((void __user *)args[1], 1);
164 case BRCTL_DEL_BRIDGE:
165 return brc_add_del_bridge((void __user *)args[1], 0);
171 /* Called with the br_ioctl_mutex. */
173 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
174 brc_ioctl_deviceless_stub(unsigned int cmd, void __user *uarg)
176 brc_ioctl_deviceless_stub(struct net *net, unsigned int cmd, void __user *uarg)
182 return old_deviceless(uarg);
185 return brc_add_del_bridge(uarg, 1);
187 return brc_add_del_bridge(uarg, 0);
193 static int brc_add_del_port(struct net_device *dev, int port_ifindex, int add)
195 struct sk_buff *request;
196 struct net_device *port;
199 if (!capable(CAP_NET_ADMIN))
202 port = __dev_get_by_index(&init_net, port_ifindex);
206 /* Save name of dev and port because there's a race between the
207 * rtnl_unlock() and the brc_send_simple_command(). */
208 request = brc_make_request(add ? BRC_GENL_C_PORT_ADD : BRC_GENL_C_PORT_DEL,
209 dev->name, port->name);
214 err = brc_send_simple_command(request);
220 static int brc_get_bridge_info(struct net_device *dev,
221 struct __bridge_info __user *ub)
223 struct __bridge_info b;
225 memset(&b, 0, sizeof(struct __bridge_info));
227 /* First two bytes are the priority, which we should skip. This comes
228 * from struct bridge_id in br_private.h, which is unavailable to us.
230 memcpy((u8 *)&b.bridge_id + 2, dev->dev_addr, ETH_ALEN);
233 if (copy_to_user(ub, &b, sizeof(struct __bridge_info)))
239 static int brc_get_port_list(struct net_device *dev, int __user *uindices,
245 retval = brc_get_indices(BRC_GENL_C_GET_PORTS, dev->name,
253 * Format up to a page worth of forwarding table entries
254 * userbuf -- where to copy result
255 * maxnum -- maximum number of entries desired
256 * (limited to a page for sanity)
257 * offset -- number of records to skip
259 static int brc_get_fdb_entries(struct net_device *dev, void __user *userbuf,
260 unsigned long maxnum, unsigned long offset)
262 struct nlattr *attrs[BRC_GENL_A_MAX + 1];
263 struct sk_buff *request, *reply;
267 /* Clamp size to PAGE_SIZE, test maxnum to avoid overflow */
268 if (maxnum > PAGE_SIZE/sizeof(struct __fdb_entry))
269 maxnum = PAGE_SIZE/sizeof(struct __fdb_entry);
271 request = brc_make_request(BRC_GENL_C_FDB_QUERY, dev->name, NULL);
274 NLA_PUT_U64(request, BRC_GENL_A_FDB_COUNT, maxnum);
275 NLA_PUT_U64(request, BRC_GENL_A_FDB_SKIP, offset);
278 reply = brc_send_command(request, attrs);
279 retval = PTR_ERR(reply);
283 retval = -nla_get_u32(attrs[BRC_GENL_A_ERR_CODE]);
288 if (!attrs[BRC_GENL_A_FDB_DATA])
290 len = nla_len(attrs[BRC_GENL_A_FDB_DATA]);
291 if (len % sizeof(struct __fdb_entry) ||
292 len / sizeof(struct __fdb_entry) > maxnum)
295 retval = len / sizeof(struct __fdb_entry);
296 if (copy_to_user(userbuf, nla_data(attrs[BRC_GENL_A_FDB_DATA]), len))
310 /* Legacy ioctl's through SIOCDEVPRIVATE. Called with rtnl_lock. */
311 static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
313 unsigned long args[4];
315 if (copy_from_user(args, rq->ifr_data, sizeof(args)))
320 return brc_add_del_port(dev, args[1], 1);
322 return brc_add_del_port(dev, args[1], 0);
324 case BRCTL_GET_BRIDGE_INFO:
325 return brc_get_bridge_info(dev, (struct __bridge_info __user *)args[1]);
327 case BRCTL_GET_PORT_LIST:
328 return brc_get_port_list(dev, (int __user *)args[1], args[2]);
330 case BRCTL_GET_FDB_ENTRIES:
331 return brc_get_fdb_entries(dev, (void __user *)args[1],
338 /* Called with the rtnl_lock. */
339 static int brc_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
345 err = old_dev_ioctl(dev, rq, cmd);
349 return brc_add_del_port(dev, rq->ifr_ifindex, 1);
351 return brc_add_del_port(dev, rq->ifr_ifindex, 0);
362 static struct genl_family brc_genl_family = {
363 .id = GENL_ID_GENERATE,
365 .name = BRC_GENL_FAMILY_NAME,
367 .maxattr = BRC_GENL_A_MAX,
370 static int brc_genl_query(struct sk_buff *skb, struct genl_info *info)
373 struct sk_buff *ans_skb;
376 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
380 data = genlmsg_put_reply(ans_skb, info, &brc_genl_family,
381 0, BRC_GENL_C_QUERY_MC);
386 NLA_PUT_U32(ans_skb, BRC_GENL_A_MC_GROUP, brc_mc_group.id);
388 genlmsg_end(ans_skb, data);
389 return genlmsg_reply(ans_skb, info);
397 /* Attribute policy: what each attribute may contain. */
398 static struct nla_policy brc_genl_policy[BRC_GENL_A_MAX + 1] = {
399 [BRC_GENL_A_ERR_CODE] = { .type = NLA_U32 },
400 [BRC_GENL_A_FDB_DATA] = { .type = NLA_UNSPEC },
403 static int brc_genl_dp_result(struct sk_buff *skb, struct genl_info *info)
405 unsigned long int flags;
408 if (!info->attrs[BRC_GENL_A_ERR_CODE])
411 skb = skb_clone(skb, GFP_KERNEL);
415 spin_lock_irqsave(&brc_lock, flags);
416 if (brc_seq == info->snd_seq) {
419 kfree_skb(brc_reply);
428 spin_unlock_irqrestore(&brc_lock, flags);
433 static struct genl_ops brc_genl_ops[] = {
434 { .cmd = BRC_GENL_C_QUERY_MC,
435 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
437 .doit = brc_genl_query,
439 { .cmd = BRC_GENL_C_DP_RESULT,
440 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
441 .policy = brc_genl_policy,
442 .doit = brc_genl_dp_result,
446 static struct sk_buff *brc_send_command(struct sk_buff *request,
447 struct nlattr **attrs)
449 unsigned long int flags;
450 struct sk_buff *reply;
453 mutex_lock(&brc_serial);
455 /* Increment sequence number first, so that we ignore any replies
456 * to stale requests. */
457 spin_lock_irqsave(&brc_lock, flags);
458 nlmsg_hdr(request)->nlmsg_seq = ++brc_seq;
459 INIT_COMPLETION(brc_done);
460 spin_unlock_irqrestore(&brc_lock, flags);
462 nlmsg_end(request, nlmsg_hdr(request));
465 error = genlmsg_multicast(request, 0, brc_mc_group.id, GFP_KERNEL);
469 /* Wait for reply. */
471 if (!wait_for_completion_timeout(&brc_done, BRC_TIMEOUT)) {
472 pr_warn("timed out waiting for userspace\n");
477 spin_lock_irqsave(&brc_lock, flags);
480 spin_unlock_irqrestore(&brc_lock, flags);
482 mutex_unlock(&brc_serial);
484 /* Re-parse message. Can't fail, since it parsed correctly once
486 error = nlmsg_parse(nlmsg_hdr(reply), GENL_HDRLEN,
487 attrs, BRC_GENL_A_MAX, brc_genl_policy);
493 mutex_unlock(&brc_serial);
494 return ERR_PTR(error);
497 static int __init brc_init(void)
501 printk("Open vSwitch Bridge Compatibility, built "__DATE__" "__TIME__"\n");
503 /* Set the bridge ioctl handler */
504 brioctl_set(brc_ioctl_deviceless_stub);
506 /* Set the openvswitch_mod device ioctl handler */
507 dp_ioctl_hook = brc_dev_ioctl;
509 /* Randomize the initial sequence number. This is not a security
510 * feature; it only helps avoid crossed wires between userspace and
511 * the kernel when the module is unloaded and reloaded. */
512 brc_seq = net_random();
514 /* Register generic netlink family to communicate changes to
516 err = genl_register_family_with_ops(&brc_genl_family,
517 brc_genl_ops, ARRAY_SIZE(brc_genl_ops));
521 strcpy(brc_mc_group.name, "brcompat");
522 err = genl_register_mc_group(&brc_genl_family, &brc_mc_group);
529 genl_unregister_family(&brc_genl_family);
531 pr_emerg("failed to install!\n");
535 static void brc_cleanup(void)
537 /* Unregister ioctl hooks */
538 dp_ioctl_hook = NULL;
541 genl_unregister_family(&brc_genl_family);
544 module_init(brc_init);
545 module_exit(brc_cleanup);
547 MODULE_DESCRIPTION("Open vSwitch bridge compatibility");
548 MODULE_AUTHOR("Nicira Networks");
549 MODULE_LICENSE("GPL");
551 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
553 * In kernels 2.6.36 and later, Open vSwitch can safely coexist with
554 * the Linux bridge module, but it does not make sense to load both bridge and
555 * brcompat_mod, so this prevents it.
557 BRIDGE_MUTUAL_EXCLUSION;