kdbus: the driver, original and non-working
[platform/kernel/linux-rpi.git] / ipc / kdbus / handle.h
1 /*
2  * Copyright (C) 2013-2015 Kay Sievers
3  * Copyright (C) 2013-2015 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
4  * Copyright (C) 2013-2015 Daniel Mack <daniel@zonque.org>
5  * Copyright (C) 2013-2015 David Herrmann <dh.herrmann@gmail.com>
6  * Copyright (C) 2013-2015 Linux Foundation
7  *
8  * kdbus is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the
10  * Free Software Foundation; either version 2.1 of the License, or (at
11  * your option) any later version.
12  */
13
14 #ifndef __KDBUS_HANDLE_H
15 #define __KDBUS_HANDLE_H
16
17 #include <linux/fs.h>
18 #include <uapi/linux/kdbus.h>
19
20 extern const struct file_operations kdbus_handle_ops;
21
22 /**
23  * kdbus_arg - information and state of a single ioctl command item
24  * @type:               item type
25  * @item:               set by the parser to the first found item of this type
26  * @multiple:           whether multiple items of this type are allowed
27  * @mandatory:          whether at least one item of this type is required
28  *
29  * This structure describes a single item in an ioctl command payload. The
30  * caller has to pre-fill the type and flags, the parser will then use this
31  * information to verify the ioctl payload. @item is set by the parser to point
32  * to the first occurrence of the item.
33  */
34 struct kdbus_arg {
35         u64 type;
36         struct kdbus_item *item;
37         bool multiple : 1;
38         bool mandatory : 1;
39 };
40
41 /**
42  * kdbus_args - information and state of ioctl command parser
43  * @allowed_flags:      set of flags this command supports
44  * @argc:               number of items in @argv
45  * @argv:               array of items this command supports
46  * @user:               set by parser to user-space location of current command
47  * @cmd:                set by parser to kernel copy of command payload
48  * @cmd_buf:            inline buf to avoid kmalloc() on small cmds
49  * @items:              points to item array in @cmd
50  * @items_size:         size of @items in bytes
51  * @is_cmd:             whether this is a command-payload or msg-payload
52  *
53  * This structure is used to parse ioctl command payloads on each invocation.
54  * The ioctl handler has to pre-fill the flags and allowed items before passing
55  * the object to kdbus_args_parse(). The parser will copy the command payload
56  * into kernel-space and verify the correctness of the data.
57  *
58  * We use a 256 bytes buffer for small command payloads, to be allocated on
59  * stack on syscall entrance.
60  */
61 struct kdbus_args {
62         u64 allowed_flags;
63         size_t argc;
64         struct kdbus_arg *argv;
65
66         struct kdbus_cmd __user *user;
67         struct kdbus_cmd *cmd;
68         u8 cmd_buf[256];
69
70         struct kdbus_item *items;
71         size_t items_size;
72         bool is_cmd : 1;
73 };
74
75 int __kdbus_args_parse(struct kdbus_args *args, bool is_cmd, void __user *argp,
76                        size_t type_size, size_t items_offset, void **out);
77 int kdbus_args_clear(struct kdbus_args *args, int ret);
78
79 #define kdbus_args_parse(_args, _argp, _v)                              \
80         ({                                                              \
81                 BUILD_BUG_ON(offsetof(typeof(**(_v)), size) !=          \
82                              offsetof(struct kdbus_cmd, size));         \
83                 BUILD_BUG_ON(offsetof(typeof(**(_v)), flags) !=         \
84                              offsetof(struct kdbus_cmd, flags));        \
85                 BUILD_BUG_ON(offsetof(typeof(**(_v)), return_flags) !=  \
86                              offsetof(struct kdbus_cmd, return_flags)); \
87                 __kdbus_args_parse((_args), 1, (_argp), sizeof(**(_v)), \
88                                    offsetof(typeof(**(_v)), items),     \
89                                    (void **)(_v));                      \
90         })
91
92 #define kdbus_args_parse_msg(_args, _argp, _v)                          \
93         ({                                                              \
94                 BUILD_BUG_ON(offsetof(typeof(**(_v)), size) !=          \
95                              offsetof(struct kdbus_cmd, size));         \
96                 BUILD_BUG_ON(offsetof(typeof(**(_v)), flags) !=         \
97                              offsetof(struct kdbus_cmd, flags));        \
98                 __kdbus_args_parse((_args), 0, (_argp), sizeof(**(_v)), \
99                                    offsetof(typeof(**(_v)), items),     \
100                                    (void **)(_v));                      \
101         })
102
103 #endif