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/etherdevice.h>
13 #include <linux/if_bridge.h>
14 #include <linux/netdevice.h>
15 #include <linux/rtnetlink.h>
16 #include <net/genetlink.h>
19 #include "openvswitch/brcompat-netlink.h"
20 #include "brc_procfs.h"
23 static struct genl_family brc_genl_family;
24 static struct genl_multicast_group brc_mc_group;
26 /* Time to wait for ovs-vswitchd to respond to a datapath action, in
28 #define BRC_TIMEOUT (HZ * 5)
30 /* Mutex to serialize ovs-brcompatd callbacks. (Some callbacks naturally hold
31 * br_ioctl_mutex, others hold rtnl_lock, but we can't take the former
32 * ourselves and we don't want to hold the latter over a potentially long
34 static DEFINE_MUTEX(brc_serial);
36 /* Userspace communication. */
37 static DEFINE_SPINLOCK(brc_lock); /* Ensure atomic access to these vars. */
38 static DECLARE_COMPLETION(brc_done); /* Userspace signaled operation done? */
39 static struct sk_buff *brc_reply; /* Reply from userspace. */
40 static u32 brc_seq; /* Sequence number for current op. */
42 static struct sk_buff *brc_send_command(struct sk_buff *, struct nlattr **attrs);
43 static int brc_send_simple_command(struct sk_buff *);
45 static struct sk_buff *
46 brc_make_request(int op, const char *bridge, const char *port)
48 struct sk_buff *skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
52 genlmsg_put(skb, 0, 0, &brc_genl_family, 0, op);
54 NLA_PUT_STRING(skb, BRC_GENL_A_DP_NAME, bridge);
56 NLA_PUT_STRING(skb, BRC_GENL_A_PORT_NAME, port);
65 static int brc_send_simple_command(struct sk_buff *request)
67 struct nlattr *attrs[BRC_GENL_A_MAX + 1];
68 struct sk_buff *reply;
71 reply = brc_send_command(request, attrs);
73 return PTR_ERR(reply);
75 error = nla_get_u32(attrs[BRC_GENL_A_ERR_CODE]);
80 static int brc_add_del_bridge(char __user *uname, int add)
82 struct sk_buff *request;
85 if (copy_from_user(name, uname, IFNAMSIZ))
88 name[IFNAMSIZ - 1] = 0;
89 request = brc_make_request(add ? BRC_GENL_C_DP_ADD : BRC_GENL_C_DP_DEL,
94 return brc_send_simple_command(request);
97 static int brc_get_indices(int op, const char *br_name,
98 int __user *uindices, int n)
100 struct nlattr *attrs[BRC_GENL_A_MAX + 1];
101 struct sk_buff *request, *reply;
111 request = brc_make_request(op, br_name, NULL);
115 reply = brc_send_command(request, attrs);
116 ret = PTR_ERR(reply);
120 ret = -nla_get_u32(attrs[BRC_GENL_A_ERR_CODE]);
125 if (!attrs[BRC_GENL_A_IFINDEXES])
128 len = nla_len(attrs[BRC_GENL_A_IFINDEXES]);
129 indices = nla_data(attrs[BRC_GENL_A_IFINDEXES]);
130 if (len % sizeof(int))
133 n = min_t(int, n, len / sizeof(int));
134 ret = copy_to_user(uindices, indices, n * sizeof(int)) ? -EFAULT : n;
142 /* Called with br_ioctl_mutex. */
143 static int brc_get_bridges(int __user *uindices, int n)
145 return brc_get_indices(BRC_GENL_C_GET_BRIDGES, NULL, uindices, n);
148 /* Legacy deviceless bridge ioctl's. Called with br_ioctl_mutex. */
150 old_deviceless(void __user *uarg)
152 unsigned long args[3];
154 if (copy_from_user(args, uarg, sizeof(args)))
158 case BRCTL_GET_BRIDGES:
159 return brc_get_bridges((int __user *)args[1], args[2]);
161 case BRCTL_ADD_BRIDGE:
162 return brc_add_del_bridge((void __user *)args[1], 1);
163 case BRCTL_DEL_BRIDGE:
164 return brc_add_del_bridge((void __user *)args[1], 0);
170 /* Called with the br_ioctl_mutex. */
172 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
173 brc_ioctl_deviceless_stub(unsigned int cmd, void __user *uarg)
175 brc_ioctl_deviceless_stub(struct net *net, unsigned int cmd, void __user *uarg)
181 return old_deviceless(uarg);
184 return brc_add_del_bridge(uarg, 1);
186 return brc_add_del_bridge(uarg, 0);
193 brc_add_del_port(struct net_device *dev, int port_ifindex, int add)
195 struct sk_buff *request;
196 struct net_device *port;
199 port = __dev_get_by_index(&init_net, port_ifindex);
203 /* Save name of dev and port because there's a race between the
204 * rtnl_unlock() and the brc_send_simple_command(). */
205 request = brc_make_request(add ? BRC_GENL_C_PORT_ADD : BRC_GENL_C_PORT_DEL,
206 dev->name, port->name);
211 err = brc_send_simple_command(request);
218 brc_get_bridge_info(struct net_device *dev, struct __bridge_info __user *ub)
220 struct __bridge_info b;
224 memset(&b, 0, sizeof(struct __bridge_info));
226 for (i=0; i<ETH_ALEN; i++)
227 id |= (u64)dev->dev_addr[i] << (8*(ETH_ALEN-1 - i));
228 b.bridge_id = cpu_to_be64(id);
231 if (copy_to_user(ub, &b, sizeof(struct __bridge_info)))
238 brc_get_port_list(struct net_device *dev, int __user *uindices, int num)
243 retval = brc_get_indices(BRC_GENL_C_GET_PORTS, dev->name,
251 * Format up to a page worth of forwarding table entries
252 * userbuf -- where to copy result
253 * maxnum -- maximum number of entries desired
254 * (limited to a page for sanity)
255 * offset -- number of records to skip
257 static int brc_get_fdb_entries(struct net_device *dev, void __user *userbuf,
258 unsigned long maxnum, unsigned long offset)
260 struct nlattr *attrs[BRC_GENL_A_MAX + 1];
261 struct sk_buff *request, *reply;
265 /* Clamp size to PAGE_SIZE, test maxnum to avoid overflow */
266 if (maxnum > PAGE_SIZE/sizeof(struct __fdb_entry))
267 maxnum = PAGE_SIZE/sizeof(struct __fdb_entry);
269 request = brc_make_request(BRC_GENL_C_FDB_QUERY, dev->name, NULL);
272 NLA_PUT_U64(request, BRC_GENL_A_FDB_COUNT, maxnum);
273 NLA_PUT_U64(request, BRC_GENL_A_FDB_SKIP, offset);
276 reply = brc_send_command(request, attrs);
277 retval = PTR_ERR(reply);
281 retval = -nla_get_u32(attrs[BRC_GENL_A_ERR_CODE]);
286 if (!attrs[BRC_GENL_A_FDB_DATA])
288 len = nla_len(attrs[BRC_GENL_A_FDB_DATA]);
289 if (len % sizeof(struct __fdb_entry) ||
290 len / sizeof(struct __fdb_entry) > maxnum)
293 retval = len / sizeof(struct __fdb_entry);
294 if (copy_to_user(userbuf, nla_data(attrs[BRC_GENL_A_FDB_DATA]), len))
308 /* Legacy ioctl's through SIOCDEVPRIVATE. Called with rtnl_lock. */
310 old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
312 unsigned long args[4];
314 if (copy_from_user(args, rq->ifr_data, sizeof(args)))
319 return brc_add_del_port(dev, args[1], 1);
321 return brc_add_del_port(dev, args[1], 0);
323 case BRCTL_GET_BRIDGE_INFO:
324 return brc_get_bridge_info(dev, (struct __bridge_info __user *)args[1]);
326 case BRCTL_GET_PORT_LIST:
327 return brc_get_port_list(dev, (int __user *)args[1], args[2]);
329 case BRCTL_GET_FDB_ENTRIES:
330 return brc_get_fdb_entries(dev, (void __user *)args[1],
337 /* Called with the rtnl_lock. */
339 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 static struct genl_ops brc_genl_ops_query_dp = {
398 .cmd = BRC_GENL_C_QUERY_MC,
399 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
401 .doit = brc_genl_query,
405 /* Attribute policy: what each attribute may contain. */
406 static struct nla_policy brc_genl_policy[BRC_GENL_A_MAX + 1] = {
407 [BRC_GENL_A_ERR_CODE] = { .type = NLA_U32 },
409 [BRC_GENL_A_PROC_DIR] = { .type = NLA_NUL_STRING },
410 [BRC_GENL_A_PROC_NAME] = { .type = NLA_NUL_STRING },
411 [BRC_GENL_A_PROC_DATA] = { .type = NLA_NUL_STRING },
413 [BRC_GENL_A_FDB_DATA] = { .type = NLA_UNSPEC },
417 brc_genl_dp_result(struct sk_buff *skb, struct genl_info *info)
419 unsigned long int flags;
422 if (!info->attrs[BRC_GENL_A_ERR_CODE])
425 skb = skb_clone(skb, GFP_KERNEL);
429 spin_lock_irqsave(&brc_lock, flags);
430 if (brc_seq == info->snd_seq) {
434 kfree_skb(brc_reply);
443 spin_unlock_irqrestore(&brc_lock, flags);
448 static struct genl_ops brc_genl_ops_dp_result = {
449 .cmd = BRC_GENL_C_DP_RESULT,
450 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
451 .policy = brc_genl_policy,
452 .doit = brc_genl_dp_result,
456 static struct genl_ops brc_genl_ops_set_proc = {
457 .cmd = BRC_GENL_C_SET_PROC,
458 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
459 .policy = brc_genl_policy,
460 .doit = brc_genl_set_proc,
464 static struct sk_buff *brc_send_command(struct sk_buff *request, struct nlattr **attrs)
466 unsigned long int flags;
467 struct sk_buff *reply;
470 mutex_lock(&brc_serial);
472 /* Increment sequence number first, so that we ignore any replies
473 * to stale requests. */
474 spin_lock_irqsave(&brc_lock, flags);
475 nlmsg_hdr(request)->nlmsg_seq = ++brc_seq;
476 INIT_COMPLETION(brc_done);
477 spin_unlock_irqrestore(&brc_lock, flags);
479 nlmsg_end(request, nlmsg_hdr(request));
482 error = genlmsg_multicast(request, 0, brc_mc_group.id, GFP_KERNEL);
486 /* Wait for reply. */
488 if (!wait_for_completion_timeout(&brc_done, BRC_TIMEOUT))
492 spin_lock_irqsave(&brc_lock, flags);
495 spin_unlock_irqrestore(&brc_lock, flags);
497 mutex_unlock(&brc_serial);
499 /* Re-parse message. Can't fail, since it parsed correctly once
501 error = nlmsg_parse(nlmsg_hdr(reply), GENL_HDRLEN,
502 attrs, BRC_GENL_A_MAX, brc_genl_policy);
508 mutex_unlock(&brc_serial);
509 return ERR_PTR(error);
513 __init brc_init(void)
517 printk("Open vSwitch Bridge Compatibility, built "__DATE__" "__TIME__"\n");
519 /* Set the bridge ioctl handler */
520 brioctl_set(brc_ioctl_deviceless_stub);
522 /* Set the openvswitch_mod device ioctl handler */
523 dp_ioctl_hook = brc_dev_ioctl;
525 /* Randomize the initial sequence number. This is not a security
526 * feature; it only helps avoid crossed wires between userspace and
527 * the kernel when the module is unloaded and reloaded. */
528 brc_seq = net_random();
530 /* Register generic netlink family to communicate changes to
532 err = genl_register_family(&brc_genl_family);
536 err = genl_register_ops(&brc_genl_family, &brc_genl_ops_query_dp);
540 err = genl_register_ops(&brc_genl_family, &brc_genl_ops_dp_result);
544 err = genl_register_ops(&brc_genl_family, &brc_genl_ops_set_proc);
548 strcpy(brc_mc_group.name, "brcompat");
549 err = genl_register_mc_group(&brc_genl_family, &brc_mc_group);
556 genl_unregister_family(&brc_genl_family);
558 printk(KERN_EMERG "brcompat: failed to install!");
565 /* Unregister ioctl hooks */
566 dp_ioctl_hook = NULL;
569 genl_unregister_family(&brc_genl_family);
573 module_init(brc_init);
574 module_exit(brc_cleanup);
576 MODULE_DESCRIPTION("Open vSwitch bridge compatibility");
577 MODULE_AUTHOR("Nicira Networks");
578 MODULE_LICENSE("GPL");