2 * Copyright (c) 2007-2012 Nicira Networks.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/uaccess.h>
24 #include <linux/completion.h>
25 #include <linux/etherdevice.h>
26 #include <linux/if_bridge.h>
27 #include <linux/netdevice.h>
28 #include <linux/rtnetlink.h>
29 #include <net/genetlink.h>
31 #include "openvswitch/brcompat-netlink.h"
34 static struct genl_family brc_genl_family;
35 static struct genl_multicast_group brc_mc_group;
37 /* Time to wait for ovs-vswitchd to respond to a datapath action, in
39 #define BRC_TIMEOUT (HZ * 5)
41 /* Mutex to serialize ovs-brcompatd callbacks. (Some callbacks naturally hold
42 * br_ioctl_mutex, others hold rtnl_lock, but we can't take the former
43 * ourselves and we don't want to hold the latter over a potentially long
45 static DEFINE_MUTEX(brc_serial);
47 /* Userspace communication. */
48 static DEFINE_SPINLOCK(brc_lock); /* Ensure atomic access to these vars. */
49 static DECLARE_COMPLETION(brc_done); /* Userspace signaled operation done? */
50 static struct sk_buff *brc_reply; /* Reply from userspace. */
51 static u32 brc_seq; /* Sequence number for current op. */
53 static struct sk_buff *brc_send_command(struct net *,
55 struct nlattr **attrs);
56 static int brc_send_simple_command(struct net *, struct sk_buff *);
58 static struct sk_buff *brc_make_request(int op, const char *bridge,
61 struct sk_buff *skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
65 genlmsg_put(skb, 0, 0, &brc_genl_family, 0, op);
67 NLA_PUT_STRING(skb, BRC_GENL_A_DP_NAME, bridge);
69 NLA_PUT_STRING(skb, BRC_GENL_A_PORT_NAME, port);
78 static int brc_send_simple_command(struct net *net, struct sk_buff *request)
80 struct nlattr *attrs[BRC_GENL_A_MAX + 1];
81 struct sk_buff *reply;
84 reply = brc_send_command(net, request, attrs);
86 return PTR_ERR(reply);
88 error = nla_get_u32(attrs[BRC_GENL_A_ERR_CODE]);
93 static int brc_add_del_bridge(struct net *net, char __user *uname, int add)
95 struct sk_buff *request;
98 if (!capable(CAP_NET_ADMIN))
101 if (copy_from_user(name, uname, IFNAMSIZ))
104 name[IFNAMSIZ - 1] = 0;
105 request = brc_make_request(add ? BRC_GENL_C_DP_ADD : BRC_GENL_C_DP_DEL,
110 return brc_send_simple_command(net, request);
113 static int brc_get_indices(struct net *net,
114 int op, const char *br_name,
115 int __user *uindices, int n)
117 struct nlattr *attrs[BRC_GENL_A_MAX + 1];
118 struct sk_buff *request, *reply;
128 request = brc_make_request(op, br_name, NULL);
132 reply = brc_send_command(net, request, attrs);
133 ret = PTR_ERR(reply);
137 ret = -nla_get_u32(attrs[BRC_GENL_A_ERR_CODE]);
142 if (!attrs[BRC_GENL_A_IFINDEXES])
145 len = nla_len(attrs[BRC_GENL_A_IFINDEXES]);
146 indices = nla_data(attrs[BRC_GENL_A_IFINDEXES]);
147 if (len % sizeof(int))
150 n = min_t(int, n, len / sizeof(int));
151 ret = copy_to_user(uindices, indices, n * sizeof(int)) ? -EFAULT : n;
159 /* Called with br_ioctl_mutex. */
160 static int brc_get_bridges(struct net *net, int __user *uindices, int n)
162 return brc_get_indices(net, BRC_GENL_C_GET_BRIDGES, NULL, uindices, n);
165 /* Legacy deviceless bridge ioctl's. Called with br_ioctl_mutex. */
166 static int old_deviceless(struct net *net, void __user *uarg)
168 unsigned long args[3];
170 if (copy_from_user(args, uarg, sizeof(args)))
174 case BRCTL_GET_BRIDGES:
175 return brc_get_bridges(net, (int __user *)args[1], args[2]);
177 case BRCTL_ADD_BRIDGE:
178 return brc_add_del_bridge(net, (void __user *)args[1], 1);
179 case BRCTL_DEL_BRIDGE:
180 return brc_add_del_bridge(net, (void __user *)args[1], 0);
186 /* Called with the br_ioctl_mutex. */
188 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
189 brc_ioctl_deviceless_stub(unsigned int cmd, void __user *uarg)
191 struct net *net = NULL;
193 brc_ioctl_deviceless_stub(struct net *net, unsigned int cmd, void __user *uarg)
199 return old_deviceless(net, uarg);
202 return brc_add_del_bridge(net, uarg, 1);
204 return brc_add_del_bridge(net, uarg, 0);
210 static int brc_add_del_port(struct net_device *dev, int port_ifindex, int add)
212 struct sk_buff *request;
213 struct net_device *port;
216 if (!capable(CAP_NET_ADMIN))
219 port = __dev_get_by_index(dev_net(dev), port_ifindex);
223 /* Save name of dev and port because there's a race between the
224 * rtnl_unlock() and the brc_send_simple_command(). */
225 request = brc_make_request(add ? BRC_GENL_C_PORT_ADD : BRC_GENL_C_PORT_DEL,
226 dev->name, port->name);
231 err = brc_send_simple_command(dev_net(dev), request);
237 static int brc_get_bridge_info(struct net_device *dev,
238 struct __bridge_info __user *ub)
240 struct __bridge_info b;
242 memset(&b, 0, sizeof(struct __bridge_info));
244 /* First two bytes are the priority, which we should skip. This comes
245 * from struct bridge_id in br_private.h, which is unavailable to us.
247 memcpy((u8 *)&b.bridge_id + 2, dev->dev_addr, ETH_ALEN);
250 if (copy_to_user(ub, &b, sizeof(struct __bridge_info)))
256 static int brc_get_port_list(struct net_device *dev, int __user *uindices,
262 retval = brc_get_indices(dev_net(dev), BRC_GENL_C_GET_PORTS, dev->name,
270 * Format up to a page worth of forwarding table entries
271 * userbuf -- where to copy result
272 * maxnum -- maximum number of entries desired
273 * (limited to a page for sanity)
274 * offset -- number of records to skip
276 static int brc_get_fdb_entries(struct net_device *dev, void __user *userbuf,
277 unsigned long maxnum, unsigned long offset)
279 struct nlattr *attrs[BRC_GENL_A_MAX + 1];
280 struct sk_buff *request, *reply;
284 /* Clamp size to PAGE_SIZE, test maxnum to avoid overflow */
285 if (maxnum > PAGE_SIZE/sizeof(struct __fdb_entry))
286 maxnum = PAGE_SIZE/sizeof(struct __fdb_entry);
288 request = brc_make_request(BRC_GENL_C_FDB_QUERY, dev->name, NULL);
291 NLA_PUT_U64(request, BRC_GENL_A_FDB_COUNT, maxnum);
292 NLA_PUT_U64(request, BRC_GENL_A_FDB_SKIP, offset);
295 reply = brc_send_command(dev_net(dev), request, attrs);
296 retval = PTR_ERR(reply);
300 retval = -nla_get_u32(attrs[BRC_GENL_A_ERR_CODE]);
305 if (!attrs[BRC_GENL_A_FDB_DATA])
307 len = nla_len(attrs[BRC_GENL_A_FDB_DATA]);
308 if (len % sizeof(struct __fdb_entry) ||
309 len / sizeof(struct __fdb_entry) > maxnum)
312 retval = len / sizeof(struct __fdb_entry);
313 if (copy_to_user(userbuf, nla_data(attrs[BRC_GENL_A_FDB_DATA]), len))
327 /* Legacy ioctl's through SIOCDEVPRIVATE. Called with rtnl_lock. */
328 static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
330 unsigned long args[4];
332 if (copy_from_user(args, rq->ifr_data, sizeof(args)))
337 return brc_add_del_port(dev, args[1], 1);
339 return brc_add_del_port(dev, args[1], 0);
341 case BRCTL_GET_BRIDGE_INFO:
342 return brc_get_bridge_info(dev, (struct __bridge_info __user *)args[1]);
344 case BRCTL_GET_PORT_LIST:
345 return brc_get_port_list(dev, (int __user *)args[1], args[2]);
347 case BRCTL_GET_FDB_ENTRIES:
348 return brc_get_fdb_entries(dev, (void __user *)args[1],
355 /* Called with the rtnl_lock. */
356 static int brc_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
362 err = old_dev_ioctl(dev, rq, cmd);
366 return brc_add_del_port(dev, rq->ifr_ifindex, 1);
368 return brc_add_del_port(dev, rq->ifr_ifindex, 0);
379 static struct genl_family brc_genl_family = {
380 .id = GENL_ID_GENERATE,
382 .name = BRC_GENL_FAMILY_NAME,
384 .maxattr = BRC_GENL_A_MAX,
388 static int brc_genl_query(struct sk_buff *skb, struct genl_info *info)
391 struct sk_buff *ans_skb;
394 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
398 data = genlmsg_put_reply(ans_skb, info, &brc_genl_family,
399 0, BRC_GENL_C_QUERY_MC);
404 NLA_PUT_U32(ans_skb, BRC_GENL_A_MC_GROUP, brc_mc_group.id);
406 genlmsg_end(ans_skb, data);
407 return genlmsg_reply(ans_skb, info);
415 /* Attribute policy: what each attribute may contain. */
416 static struct nla_policy brc_genl_policy[BRC_GENL_A_MAX + 1] = {
417 [BRC_GENL_A_ERR_CODE] = { .type = NLA_U32 },
418 [BRC_GENL_A_FDB_DATA] = { .type = NLA_UNSPEC },
421 static int brc_genl_dp_result(struct sk_buff *skb, struct genl_info *info)
423 unsigned long int flags;
426 if (!info->attrs[BRC_GENL_A_ERR_CODE])
429 skb = skb_clone(skb, GFP_KERNEL);
433 spin_lock_irqsave(&brc_lock, flags);
434 if (brc_seq == info->snd_seq) {
437 kfree_skb(brc_reply);
446 spin_unlock_irqrestore(&brc_lock, flags);
451 static struct genl_ops brc_genl_ops[] = {
452 { .cmd = BRC_GENL_C_QUERY_MC,
453 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
455 .doit = brc_genl_query,
457 { .cmd = BRC_GENL_C_DP_RESULT,
458 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
459 .policy = brc_genl_policy,
460 .doit = brc_genl_dp_result,
464 static struct sk_buff *brc_send_command(struct net *net,
465 struct sk_buff *request,
466 struct nlattr **attrs)
468 unsigned long int flags;
469 struct sk_buff *reply;
472 mutex_lock(&brc_serial);
474 /* Increment sequence number first, so that we ignore any replies
475 * to stale requests. */
476 spin_lock_irqsave(&brc_lock, flags);
477 nlmsg_hdr(request)->nlmsg_seq = ++brc_seq;
478 INIT_COMPLETION(brc_done);
479 spin_unlock_irqrestore(&brc_lock, flags);
481 nlmsg_end(request, nlmsg_hdr(request));
484 error = genlmsg_multicast_netns(net, request, 0,
485 brc_mc_group.id, GFP_KERNEL);
489 /* Wait for reply. */
491 if (!wait_for_completion_timeout(&brc_done, BRC_TIMEOUT)) {
492 pr_warn("timed out waiting for userspace\n");
497 spin_lock_irqsave(&brc_lock, flags);
500 spin_unlock_irqrestore(&brc_lock, flags);
502 mutex_unlock(&brc_serial);
504 /* Re-parse message. Can't fail, since it parsed correctly once
506 error = nlmsg_parse(nlmsg_hdr(reply), GENL_HDRLEN,
507 attrs, BRC_GENL_A_MAX, brc_genl_policy);
513 mutex_unlock(&brc_serial);
514 return ERR_PTR(error);
517 static int __init brc_init(void)
521 pr_info("Open vSwitch Bridge Compatibility, built "__DATE__" "__TIME__"\n");
523 /* Set the bridge ioctl handler */
524 brioctl_set(brc_ioctl_deviceless_stub);
526 /* Set the openvswitch_mod device ioctl handler */
527 ovs_dp_ioctl_hook = brc_dev_ioctl;
529 /* Randomize the initial sequence number. This is not a security
530 * feature; it only helps avoid crossed wires between userspace and
531 * the kernel when the module is unloaded and reloaded. */
532 brc_seq = net_random();
534 /* Register generic netlink family to communicate changes to
536 err = genl_register_family_with_ops(&brc_genl_family,
537 brc_genl_ops, ARRAY_SIZE(brc_genl_ops));
541 strcpy(brc_mc_group.name, "brcompat");
542 err = genl_register_mc_group(&brc_genl_family, &brc_mc_group);
549 genl_unregister_family(&brc_genl_family);
551 pr_emerg("failed to install!\n");
555 static void brc_cleanup(void)
557 /* Unregister ioctl hooks */
558 ovs_dp_ioctl_hook = NULL;
561 genl_unregister_family(&brc_genl_family);
564 module_init(brc_init);
565 module_exit(brc_cleanup);
567 MODULE_DESCRIPTION("Open vSwitch bridge compatibility");
568 MODULE_AUTHOR("Nicira Networks");
569 MODULE_LICENSE("GPL");
571 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
573 * In kernels 2.6.36 and later, Open vSwitch can safely coexist with
574 * the Linux bridge module, but it does not make sense to load both bridge and
575 * brcompat_mod, so this prevents it.
577 BRIDGE_MUTUAL_EXCLUSION;