treewide: Use pr_fmt and pr_<level>
[sliver-openvswitch.git] / datapath / brcompat.c
1 /*
2  * Copyright (c) 2009 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/kernel.h>
12 #include <asm/uaccess.h>
13 #include <linux/completion.h>
14 #include <linux/etherdevice.h>
15 #include <linux/if_bridge.h>
16 #include <linux/netdevice.h>
17 #include <linux/rtnetlink.h>
18 #include <net/genetlink.h>
19
20 #include "compat.h"
21 #include "openvswitch/brcompat-netlink.h"
22 #include "brc_procfs.h"
23 #include "datapath.h"
24
25 static struct genl_family brc_genl_family;
26 static struct genl_multicast_group brc_mc_group;
27
28 /* Time to wait for ovs-vswitchd to respond to a datapath action, in
29  * jiffies. */
30 #define BRC_TIMEOUT (HZ * 5)
31
32 /* Mutex to serialize ovs-brcompatd callbacks.  (Some callbacks naturally hold
33  * br_ioctl_mutex, others hold rtnl_lock, but we can't take the former
34  * ourselves and we don't want to hold the latter over a potentially long
35  * period of time.) */
36 static DEFINE_MUTEX(brc_serial);
37
38 /* Userspace communication. */
39 static DEFINE_SPINLOCK(brc_lock);    /* Ensure atomic access to these vars. */
40 static DECLARE_COMPLETION(brc_done); /* Userspace signaled operation done? */
41 static struct sk_buff *brc_reply;    /* Reply from userspace. */
42 static u32 brc_seq;                  /* Sequence number for current op. */
43
44 static struct sk_buff *brc_send_command(struct sk_buff *, struct nlattr **attrs);
45 static int brc_send_simple_command(struct sk_buff *);
46
47 static struct sk_buff *brc_make_request(int op, const char *bridge,
48                                         const char *port)
49 {
50         struct sk_buff *skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
51         if (!skb)
52                 goto error;
53
54         genlmsg_put(skb, 0, 0, &brc_genl_family, 0, op);
55         if (bridge)
56                 NLA_PUT_STRING(skb, BRC_GENL_A_DP_NAME, bridge);
57         if (port)
58                 NLA_PUT_STRING(skb, BRC_GENL_A_PORT_NAME, port);
59         return skb;
60
61 nla_put_failure:
62         kfree_skb(skb);
63 error:
64         return NULL;
65 }
66
67 static int brc_send_simple_command(struct sk_buff *request)
68 {
69         struct nlattr *attrs[BRC_GENL_A_MAX + 1];
70         struct sk_buff *reply;
71         int error;
72
73         reply = brc_send_command(request, attrs);
74         if (IS_ERR(reply))
75                 return PTR_ERR(reply);
76
77         error = nla_get_u32(attrs[BRC_GENL_A_ERR_CODE]);
78         kfree_skb(reply);
79         return -error;
80 }
81
82 static int brc_add_del_bridge(char __user *uname, int add)
83 {
84         struct sk_buff *request;
85         char name[IFNAMSIZ];
86
87         if (copy_from_user(name, uname, IFNAMSIZ))
88                 return -EFAULT;
89
90         name[IFNAMSIZ - 1] = 0;
91         request = brc_make_request(add ? BRC_GENL_C_DP_ADD : BRC_GENL_C_DP_DEL,
92                                    name, NULL);
93         if (!request)
94                 return -ENOMEM;
95
96         return brc_send_simple_command(request);
97 }
98
99 static int brc_get_indices(int op, const char *br_name,
100                            int __user *uindices, int n)
101 {
102         struct nlattr *attrs[BRC_GENL_A_MAX + 1];
103         struct sk_buff *request, *reply;
104         int *indices;
105         int ret;
106         int len;
107
108         if (n < 0)
109                 return -EINVAL;
110         if (n >= 2048)
111                 return -ENOMEM;
112
113         request = brc_make_request(op, br_name, NULL);
114         if (!request)
115                 return -ENOMEM;
116
117         reply = brc_send_command(request, attrs);
118         ret = PTR_ERR(reply);
119         if (IS_ERR(reply))
120                 goto exit;
121
122         ret = -nla_get_u32(attrs[BRC_GENL_A_ERR_CODE]);
123         if (ret < 0)
124                 goto exit_free_skb;
125
126         ret = -EINVAL;
127         if (!attrs[BRC_GENL_A_IFINDEXES])
128                 goto exit_free_skb;
129
130         len = nla_len(attrs[BRC_GENL_A_IFINDEXES]);
131         indices = nla_data(attrs[BRC_GENL_A_IFINDEXES]);
132         if (len % sizeof(int))
133                 goto exit_free_skb;
134
135         n = min_t(int, n, len / sizeof(int));
136         ret = copy_to_user(uindices, indices, n * sizeof(int)) ? -EFAULT : n;
137
138 exit_free_skb:
139         kfree_skb(reply);
140 exit:
141         return ret;
142 }
143
144 /* Called with br_ioctl_mutex. */
145 static int brc_get_bridges(int __user *uindices, int n)
146 {
147         return brc_get_indices(BRC_GENL_C_GET_BRIDGES, NULL, uindices, n);
148 }
149
150 /* Legacy deviceless bridge ioctl's.  Called with br_ioctl_mutex. */
151 static int old_deviceless(void __user *uarg)
152 {
153         unsigned long args[3];
154
155         if (copy_from_user(args, uarg, sizeof(args)))
156                 return -EFAULT;
157
158         switch (args[0]) {
159         case BRCTL_GET_BRIDGES:
160                 return brc_get_bridges((int __user *)args[1], args[2]);
161
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);
166         }
167
168         return -EOPNOTSUPP;
169 }
170
171 /* Called with the br_ioctl_mutex. */
172 static int
173 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
174 brc_ioctl_deviceless_stub(unsigned int cmd, void __user *uarg)
175 #else
176 brc_ioctl_deviceless_stub(struct net *net, unsigned int cmd, void __user *uarg)
177 #endif
178 {
179         switch (cmd) {
180         case SIOCGIFBR:
181         case SIOCSIFBR:
182                 return old_deviceless(uarg);
183
184         case SIOCBRADDBR:
185                 return brc_add_del_bridge(uarg, 1);
186         case SIOCBRDELBR:
187                 return brc_add_del_bridge(uarg, 0);
188         }
189
190         return -EOPNOTSUPP;
191 }
192
193 static int brc_add_del_port(struct net_device *dev, int port_ifindex, int add)
194 {
195         struct sk_buff *request;
196         struct net_device *port;
197         int err;
198
199         port = __dev_get_by_index(&init_net, port_ifindex);
200         if (!port)
201                 return -EINVAL;
202
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);
207         if (!request)
208                 return -ENOMEM;
209
210         rtnl_unlock();
211         err = brc_send_simple_command(request);
212         rtnl_lock();
213
214         return err;
215 }
216
217 static int brc_get_bridge_info(struct net_device *dev,
218                                struct __bridge_info __user *ub)
219 {
220         struct __bridge_info b;
221         u64 id = 0;
222         int i;
223
224         memset(&b, 0, sizeof(struct __bridge_info));
225
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);
229         b.stp_enabled = 0;
230
231         if (copy_to_user(ub, &b, sizeof(struct __bridge_info)))
232                 return -EFAULT;
233
234         return 0;
235 }
236
237 static int brc_get_port_list(struct net_device *dev, int __user *uindices,
238                              int num)
239 {
240         int retval;
241
242         rtnl_unlock();
243         retval = brc_get_indices(BRC_GENL_C_GET_PORTS, dev->name,
244                                  uindices, num);
245         rtnl_lock();
246
247         return retval;
248 }
249
250 /*
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
256  */
257 static int brc_get_fdb_entries(struct net_device *dev, void __user *userbuf,
258                                unsigned long maxnum, unsigned long offset)
259 {
260         struct nlattr *attrs[BRC_GENL_A_MAX + 1];
261         struct sk_buff *request, *reply;
262         int retval;
263         int len;
264
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);
268
269         request = brc_make_request(BRC_GENL_C_FDB_QUERY, dev->name, NULL);
270         if (!request)
271                 return -ENOMEM;
272         NLA_PUT_U64(request, BRC_GENL_A_FDB_COUNT, maxnum);
273         NLA_PUT_U64(request, BRC_GENL_A_FDB_SKIP, offset);
274
275         rtnl_unlock();
276         reply = brc_send_command(request, attrs);
277         retval = PTR_ERR(reply);
278         if (IS_ERR(reply))
279                 goto exit;
280
281         retval = -nla_get_u32(attrs[BRC_GENL_A_ERR_CODE]);
282         if (retval < 0)
283                 goto exit_free_skb;
284
285         retval = -EINVAL;
286         if (!attrs[BRC_GENL_A_FDB_DATA])
287                 goto exit_free_skb;
288         len = nla_len(attrs[BRC_GENL_A_FDB_DATA]);
289         if (len % sizeof(struct __fdb_entry) ||
290             len / sizeof(struct __fdb_entry) > maxnum)
291                 goto exit_free_skb;
292
293         retval = len / sizeof(struct __fdb_entry);
294         if (copy_to_user(userbuf, nla_data(attrs[BRC_GENL_A_FDB_DATA]), len))
295                 retval = -EFAULT;
296
297 exit_free_skb:
298         kfree_skb(reply);
299 exit:
300         rtnl_lock();
301         return retval;
302
303 nla_put_failure:
304         kfree_skb(request);
305         return -ENOMEM;
306 }
307
308 /* Legacy ioctl's through SIOCDEVPRIVATE.  Called with rtnl_lock. */
309 static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
310 {
311         unsigned long args[4];
312
313         if (copy_from_user(args, rq->ifr_data, sizeof(args)))
314                 return -EFAULT;
315
316         switch (args[0]) {
317         case BRCTL_ADD_IF:
318                 return brc_add_del_port(dev, args[1], 1);
319         case BRCTL_DEL_IF:
320                 return brc_add_del_port(dev, args[1], 0);
321
322         case BRCTL_GET_BRIDGE_INFO:
323                 return brc_get_bridge_info(dev, (struct __bridge_info __user *)args[1]);
324
325         case BRCTL_GET_PORT_LIST:
326                 return brc_get_port_list(dev, (int __user *)args[1], args[2]);
327
328         case BRCTL_GET_FDB_ENTRIES:
329                 return brc_get_fdb_entries(dev, (void __user *)args[1],
330                                            args[2], args[3]);
331         }
332
333         return -EOPNOTSUPP;
334 }
335
336 /* Called with the rtnl_lock. */
337 static int brc_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
338 {
339         int err;
340
341         switch (cmd) {
342                 case SIOCDEVPRIVATE:
343                         err = old_dev_ioctl(dev, rq, cmd);
344                         break;
345
346                 case SIOCBRADDIF:
347                         return brc_add_del_port(dev, rq->ifr_ifindex, 1);
348                 case SIOCBRDELIF:
349                         return brc_add_del_port(dev, rq->ifr_ifindex, 0);
350
351                 default:
352                         err = -EOPNOTSUPP;
353                         break;
354         }
355
356         return err;
357 }
358
359
360 static struct genl_family brc_genl_family = {
361         .id = GENL_ID_GENERATE,
362         .hdrsize = 0,
363         .name = BRC_GENL_FAMILY_NAME,
364         .version = 1,
365         .maxattr = BRC_GENL_A_MAX,
366 };
367
368 static int brc_genl_query(struct sk_buff *skb, struct genl_info *info)
369 {
370         int err = -EINVAL;
371         struct sk_buff *ans_skb;
372         void *data;
373
374         ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
375         if (!ans_skb)
376                 return -ENOMEM;
377
378         data = genlmsg_put_reply(ans_skb, info, &brc_genl_family,
379                                  0, BRC_GENL_C_QUERY_MC);
380         if (data == NULL) {
381                 err = -ENOMEM;
382                 goto err;
383         }
384         NLA_PUT_U32(ans_skb, BRC_GENL_A_MC_GROUP, brc_mc_group.id);
385
386         genlmsg_end(ans_skb, data);
387         return genlmsg_reply(ans_skb, info);
388
389 err:
390 nla_put_failure:
391         kfree_skb(ans_skb);
392         return err;
393 }
394
395 static struct genl_ops brc_genl_ops_query_dp = {
396         .cmd = BRC_GENL_C_QUERY_MC,
397         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
398         .policy = NULL,
399         .doit = brc_genl_query,
400         .dumpit = NULL
401 };
402
403 /* Attribute policy: what each attribute may contain.  */
404 static struct nla_policy brc_genl_policy[BRC_GENL_A_MAX + 1] = {
405         [BRC_GENL_A_ERR_CODE] = { .type = NLA_U32 },
406
407         [BRC_GENL_A_PROC_DIR] = { .type = NLA_NUL_STRING },
408         [BRC_GENL_A_PROC_NAME] = { .type = NLA_NUL_STRING },
409         [BRC_GENL_A_PROC_DATA] = { .type = NLA_NUL_STRING },
410
411         [BRC_GENL_A_FDB_DATA] = { .type = NLA_UNSPEC },
412 };
413
414 static int brc_genl_dp_result(struct sk_buff *skb, struct genl_info *info)
415 {
416         unsigned long int flags;
417         int err;
418
419         if (!info->attrs[BRC_GENL_A_ERR_CODE])
420                 return -EINVAL;
421
422         skb = skb_clone(skb, GFP_KERNEL);
423         if (!skb)
424                 return -ENOMEM;
425
426         spin_lock_irqsave(&brc_lock, flags);
427         if (brc_seq == info->snd_seq) {
428                 brc_seq++;
429
430                 kfree_skb(brc_reply);
431                 brc_reply = skb;
432
433                 complete(&brc_done);
434                 err = 0;
435         } else {
436                 kfree_skb(skb);
437                 err = -ESTALE;
438         }
439         spin_unlock_irqrestore(&brc_lock, flags);
440
441         return err;
442 }
443
444 static struct genl_ops brc_genl_ops_dp_result = {
445         .cmd = BRC_GENL_C_DP_RESULT,
446         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
447         .policy = brc_genl_policy,
448         .doit = brc_genl_dp_result,
449         .dumpit = NULL
450 };
451
452 static struct genl_ops brc_genl_ops_set_proc = {
453         .cmd = BRC_GENL_C_SET_PROC,
454         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
455         .policy = brc_genl_policy,
456         .doit = brc_genl_set_proc,
457         .dumpit = NULL
458 };
459
460 static struct sk_buff *brc_send_command(struct sk_buff *request,
461                                         struct nlattr **attrs)
462 {
463         unsigned long int flags;
464         struct sk_buff *reply;
465         int error;
466
467         mutex_lock(&brc_serial);
468
469         /* Increment sequence number first, so that we ignore any replies
470          * to stale requests. */
471         spin_lock_irqsave(&brc_lock, flags);
472         nlmsg_hdr(request)->nlmsg_seq = ++brc_seq;
473         INIT_COMPLETION(brc_done);
474         spin_unlock_irqrestore(&brc_lock, flags);
475
476         nlmsg_end(request, nlmsg_hdr(request));
477
478         /* Send message. */
479         error = genlmsg_multicast(request, 0, brc_mc_group.id, GFP_KERNEL);
480         if (error < 0)
481                 goto error;
482
483         /* Wait for reply. */
484         error = -ETIMEDOUT;
485         if (!wait_for_completion_timeout(&brc_done, BRC_TIMEOUT)) {
486                 pr_warn("timed out waiting for userspace\n");
487                 goto error;
488     }
489
490         /* Grab reply. */
491         spin_lock_irqsave(&brc_lock, flags);
492         reply = brc_reply;
493         brc_reply = NULL;
494         spin_unlock_irqrestore(&brc_lock, flags);
495
496         mutex_unlock(&brc_serial);
497
498         /* Re-parse message.  Can't fail, since it parsed correctly once
499          * already. */
500         error = nlmsg_parse(nlmsg_hdr(reply), GENL_HDRLEN,
501                             attrs, BRC_GENL_A_MAX, brc_genl_policy);
502         WARN_ON(error);
503
504         return reply;
505
506 error:
507         mutex_unlock(&brc_serial);
508         return ERR_PTR(error);
509 }
510
511 static int __init brc_init(void)
512 {
513         int err;
514
515         printk("Open vSwitch Bridge Compatibility, built "__DATE__" "__TIME__"\n");
516
517         /* Set the bridge ioctl handler */
518         brioctl_set(brc_ioctl_deviceless_stub);
519
520         /* Set the openvswitch_mod device ioctl handler */
521         dp_ioctl_hook = brc_dev_ioctl;
522
523         /* Randomize the initial sequence number.  This is not a security
524          * feature; it only helps avoid crossed wires between userspace and
525          * the kernel when the module is unloaded and reloaded. */
526         brc_seq = net_random();
527
528         /* Register generic netlink family to communicate changes to
529          * userspace. */
530         err = genl_register_family(&brc_genl_family);
531         if (err)
532                 goto error;
533
534         err = genl_register_ops(&brc_genl_family, &brc_genl_ops_query_dp);
535         if (err != 0)
536                 goto err_unregister;
537
538         err = genl_register_ops(&brc_genl_family, &brc_genl_ops_dp_result);
539         if (err != 0)
540                 goto err_unregister;
541
542         err = genl_register_ops(&brc_genl_family, &brc_genl_ops_set_proc);
543         if (err != 0)
544                 goto err_unregister;
545
546         strcpy(brc_mc_group.name, "brcompat");
547         err = genl_register_mc_group(&brc_genl_family, &brc_mc_group);
548         if (err < 0)
549                 goto err_unregister;
550
551         return 0;
552
553 err_unregister:
554         genl_unregister_family(&brc_genl_family);
555 error:
556         pr_emerg("failed to install!\n");
557         return err;
558 }
559
560 static void brc_cleanup(void)
561 {
562         /* Unregister ioctl hooks */
563         dp_ioctl_hook = NULL;
564         brioctl_set(NULL);
565
566         genl_unregister_family(&brc_genl_family);
567         brc_procfs_exit();
568 }
569
570 module_init(brc_init);
571 module_exit(brc_cleanup);
572
573 MODULE_DESCRIPTION("Open vSwitch bridge compatibility");
574 MODULE_AUTHOR("Nicira Networks");
575 MODULE_LICENSE("GPL");