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"
26 static struct genl_family brc_genl_family;
27 static struct genl_multicast_group brc_mc_group;
29 /* Time to wait for ovs-vswitchd to respond to a datapath action, in
31 #define BRC_TIMEOUT (HZ * 5)
33 /* Mutex to serialize ovs-brcompatd callbacks. (Some callbacks naturally hold
34 * br_ioctl_mutex, others hold rtnl_lock, but we can't take the former
35 * ourselves and we don't want to hold the latter over a potentially long
37 static DEFINE_MUTEX(brc_serial);
39 /* Userspace communication. */
40 static DEFINE_SPINLOCK(brc_lock); /* Ensure atomic access to these vars. */
41 static DECLARE_COMPLETION(brc_done); /* Userspace signaled operation done? */
42 static struct sk_buff *brc_reply; /* Reply from userspace. */
43 static u32 brc_seq; /* Sequence number for current op. */
45 static struct sk_buff *brc_send_command(struct sk_buff *, struct nlattr **attrs);
46 static int brc_send_simple_command(struct sk_buff *);
49 get_dp_ifindices(int *indices, int num)
54 for (i=0; i < ODP_MAX && index < num; i++) {
55 struct datapath *dp = get_dp(i);
58 indices[index++] = dp->ports[ODPP_LOCAL]->dev->ifindex;
66 get_port_ifindices(struct datapath *dp, int *ifindices, int num)
68 struct net_bridge_port *p;
71 list_for_each_entry_rcu (p, &dp->port_list, node) {
73 ifindices[p->port_no] = p->dev->ifindex;
78 static struct sk_buff *
79 brc_make_request(int op, const char *bridge, const char *port)
81 struct sk_buff *skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
85 genlmsg_put(skb, 0, 0, &brc_genl_family, 0, op);
86 NLA_PUT_STRING(skb, BRC_GENL_A_DP_NAME, bridge);
88 NLA_PUT_STRING(skb, BRC_GENL_A_PORT_NAME, port);
97 static int brc_send_simple_command(struct sk_buff *request)
99 struct nlattr *attrs[BRC_GENL_A_MAX + 1];
100 struct sk_buff *reply;
103 reply = brc_send_command(request, attrs);
105 return PTR_ERR(reply);
107 error = nla_get_u32(attrs[BRC_GENL_A_ERR_CODE]);
112 static int brc_add_del_bridge(char __user *uname, int add)
114 struct sk_buff *request;
117 if (copy_from_user(name, uname, IFNAMSIZ))
120 name[IFNAMSIZ - 1] = 0;
121 request = brc_make_request(add ? BRC_GENL_C_DP_ADD : BRC_GENL_C_DP_DEL,
126 return brc_send_simple_command(request);
129 static int brc_get_bridges(int __user *uindices, int n)
137 indices = kcalloc(n, sizeof(int), GFP_KERNEL);
141 n = get_dp_ifindices(indices, n);
143 ret = copy_to_user(uindices, indices, n * sizeof(int)) ? -EFAULT : n;
149 /* Legacy deviceless bridge ioctl's. Called with br_ioctl_mutex. */
151 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);
194 brc_add_del_port(struct net_device *dev, int port_ifindex, int add)
196 struct sk_buff *request;
197 struct net_device *port;
200 port = __dev_get_by_index(&init_net, port_ifindex);
204 /* Save name of dev and port because there's a race between the
205 * rtnl_unlock() and the brc_send_simple_command(). */
206 request = brc_make_request(add ? BRC_GENL_C_PORT_ADD : BRC_GENL_C_PORT_DEL,
207 dev->name, port->name);
212 err = brc_send_simple_command(request);
219 brc_get_bridge_info(struct net_device *dev, struct __bridge_info __user *ub)
221 struct __bridge_info b;
225 memset(&b, 0, sizeof(struct __bridge_info));
227 for (i=0; i<ETH_ALEN; i++)
228 id |= (u64)dev->dev_addr[i] << (8*(ETH_ALEN-1 - i));
229 b.bridge_id = cpu_to_be64(id);
232 if (copy_to_user(ub, &b, sizeof(struct __bridge_info)))
239 brc_get_port_list(struct net_device *dev, int __user *uindices, int num)
241 struct dp_dev *dp_dev = netdev_priv(dev);
242 struct datapath *dp = dp_dev->dp;
249 if (num > DP_MAX_PORTS)
252 indices = kcalloc(num, sizeof(int), GFP_KERNEL);
256 get_port_ifindices(dp, indices, num);
257 if (copy_to_user(uindices, indices, num * sizeof(int)))
264 * Format up to a page worth of forwarding table entries
265 * userbuf -- where to copy result
266 * maxnum -- maximum number of entries desired
267 * (limited to a page for sanity)
268 * offset -- number of records to skip
270 static int brc_get_fdb_entries(struct net_device *dev, void __user *userbuf,
271 unsigned long maxnum, unsigned long offset)
273 struct nlattr *attrs[BRC_GENL_A_MAX + 1];
274 struct sk_buff *request, *reply;
278 /* Clamp size to PAGE_SIZE, test maxnum to avoid overflow */
279 if (maxnum > PAGE_SIZE/sizeof(struct __fdb_entry))
280 maxnum = PAGE_SIZE/sizeof(struct __fdb_entry);
282 request = brc_make_request(BRC_GENL_C_FDB_QUERY, dev->name, NULL);
285 NLA_PUT_U64(request, BRC_GENL_A_FDB_COUNT, maxnum);
286 NLA_PUT_U64(request, BRC_GENL_A_FDB_SKIP, offset);
289 reply = brc_send_command(request, attrs);
290 retval = PTR_ERR(reply);
294 retval = -nla_get_u32(attrs[BRC_GENL_A_ERR_CODE]);
299 if (!attrs[BRC_GENL_A_FDB_DATA])
301 len = nla_len(attrs[BRC_GENL_A_FDB_DATA]);
302 if (len % sizeof(struct __fdb_entry) ||
303 len / sizeof(struct __fdb_entry) > maxnum)
306 retval = len / sizeof(struct __fdb_entry);
307 if (copy_to_user(userbuf, nla_data(attrs[BRC_GENL_A_FDB_DATA]), len))
321 /* Legacy ioctl's through SIOCDEVPRIVATE. Called with rtnl_lock. */
323 old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
325 unsigned long args[4];
327 if (copy_from_user(args, rq->ifr_data, sizeof(args)))
332 return brc_add_del_port(dev, args[1], 1);
334 return brc_add_del_port(dev, args[1], 0);
336 case BRCTL_GET_BRIDGE_INFO:
337 return brc_get_bridge_info(dev, (struct __bridge_info __user *)args[1]);
339 case BRCTL_GET_PORT_LIST:
340 return brc_get_port_list(dev, (int __user *)args[1], args[2]);
342 case BRCTL_GET_FDB_ENTRIES:
343 return brc_get_fdb_entries(dev, (void __user *)args[1],
350 /* Called with the rtnl_lock. */
352 brc_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
358 err = old_dev_ioctl(dev, rq, cmd);
362 return brc_add_del_port(dev, rq->ifr_ifindex, 1);
364 return brc_add_del_port(dev, rq->ifr_ifindex, 0);
375 static struct genl_family brc_genl_family = {
376 .id = GENL_ID_GENERATE,
378 .name = BRC_GENL_FAMILY_NAME,
380 .maxattr = BRC_GENL_A_MAX,
383 static int brc_genl_query(struct sk_buff *skb, struct genl_info *info)
386 struct sk_buff *ans_skb;
389 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
393 data = genlmsg_put_reply(ans_skb, info, &brc_genl_family,
394 0, BRC_GENL_C_QUERY_MC);
399 NLA_PUT_U32(ans_skb, BRC_GENL_A_MC_GROUP, brc_mc_group.id);
401 genlmsg_end(ans_skb, data);
402 return genlmsg_reply(ans_skb, info);
410 static struct genl_ops brc_genl_ops_query_dp = {
411 .cmd = BRC_GENL_C_QUERY_MC,
412 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
414 .doit = brc_genl_query,
418 /* Attribute policy: what each attribute may contain. */
419 static struct nla_policy brc_genl_policy[BRC_GENL_A_MAX + 1] = {
420 [BRC_GENL_A_ERR_CODE] = { .type = NLA_U32 },
422 [BRC_GENL_A_PROC_DIR] = { .type = NLA_NUL_STRING },
423 [BRC_GENL_A_PROC_NAME] = { .type = NLA_NUL_STRING },
424 [BRC_GENL_A_PROC_DATA] = { .type = NLA_NUL_STRING },
426 [BRC_GENL_A_FDB_DATA] = { .type = NLA_UNSPEC },
430 brc_genl_dp_result(struct sk_buff *skb, struct genl_info *info)
432 unsigned long int flags;
435 if (!info->attrs[BRC_GENL_A_ERR_CODE])
438 skb = skb_clone(skb, GFP_KERNEL);
442 spin_lock_irqsave(&brc_lock, flags);
443 if (brc_seq == info->snd_seq) {
447 kfree_skb(brc_reply);
456 spin_unlock_irqrestore(&brc_lock, flags);
461 static struct genl_ops brc_genl_ops_dp_result = {
462 .cmd = BRC_GENL_C_DP_RESULT,
463 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
464 .policy = brc_genl_policy,
465 .doit = brc_genl_dp_result,
469 static struct genl_ops brc_genl_ops_set_proc = {
470 .cmd = BRC_GENL_C_SET_PROC,
471 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
472 .policy = brc_genl_policy,
473 .doit = brc_genl_set_proc,
477 static struct sk_buff *brc_send_command(struct sk_buff *request, struct nlattr **attrs)
479 unsigned long int flags;
480 struct sk_buff *reply;
483 mutex_lock(&brc_serial);
485 /* Increment sequence number first, so that we ignore any replies
486 * to stale requests. */
487 spin_lock_irqsave(&brc_lock, flags);
488 nlmsg_hdr(request)->nlmsg_seq = ++brc_seq;
489 INIT_COMPLETION(brc_done);
490 spin_unlock_irqrestore(&brc_lock, flags);
492 nlmsg_end(request, nlmsg_hdr(request));
495 error = genlmsg_multicast(request, 0, brc_mc_group.id, GFP_KERNEL);
499 /* Wait for reply. */
501 if (!wait_for_completion_timeout(&brc_done, BRC_TIMEOUT))
505 spin_lock_irqsave(&brc_lock, flags);
508 spin_unlock_irqrestore(&brc_lock, flags);
510 mutex_unlock(&brc_serial);
512 /* Re-parse message. Can't fail, since it parsed correctly once
514 error = nlmsg_parse(nlmsg_hdr(reply), GENL_HDRLEN,
515 attrs, BRC_GENL_A_MAX, brc_genl_policy);
521 mutex_unlock(&brc_serial);
522 return ERR_PTR(error);
526 __init brc_init(void)
531 printk("Open vSwitch Bridge Compatibility, built "__DATE__" "__TIME__"\n");
534 for (i=0; i<ODP_MAX; i++) {
537 printk(KERN_EMERG "brcompat: no datapaths may exist!\n");
543 /* Set the bridge ioctl handler */
544 brioctl_set(brc_ioctl_deviceless_stub);
546 /* Set the openvswitch_mod device ioctl handler */
547 dp_ioctl_hook = brc_dev_ioctl;
549 /* Randomize the initial sequence number. This is not a security
550 * feature; it only helps avoid crossed wires between userspace and
551 * the kernel when the module is unloaded and reloaded. */
552 brc_seq = net_random();
554 /* Register generic netlink family to communicate changes to
556 err = genl_register_family(&brc_genl_family);
560 err = genl_register_ops(&brc_genl_family, &brc_genl_ops_query_dp);
564 err = genl_register_ops(&brc_genl_family, &brc_genl_ops_dp_result);
568 err = genl_register_ops(&brc_genl_family, &brc_genl_ops_set_proc);
572 strcpy(brc_mc_group.name, "brcompat");
573 err = genl_register_mc_group(&brc_genl_family, &brc_mc_group);
580 genl_unregister_family(&brc_genl_family);
582 printk(KERN_EMERG "brcompat: failed to install!");
589 /* Unregister ioctl hooks */
590 dp_ioctl_hook = NULL;
593 genl_unregister_family(&brc_genl_family);
597 module_init(brc_init);
598 module_exit(brc_cleanup);
600 MODULE_DESCRIPTION("Open vSwitch bridge compatibility");
601 MODULE_AUTHOR("Nicira Networks");
602 MODULE_LICENSE("GPL");