chiark / gitweb /
305ded720ed677e84371a7f7c52403cee9f202f6
[elogind.git] / src / libsystemd-bus / kdbus.h
1 /*
2  * Copyright (C) 2013 Kay Sievers
3  * Copyright (C) 2013 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
4  * Copyright (C) 2013 Linux Foundation
5  *
6  * kdbus is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at
9  * your option) any later version.
10  */
11
12 #ifndef _KDBUS_H_
13 #define _KDBUS_H_
14
15 #ifndef __KERNEL__
16 #include <sys/ioctl.h>
17 #include <sys/types.h>
18 #include <linux/types.h>
19 #endif
20
21 #define KDBUS_IOC_MAGIC                 0x95
22
23 /* Message sent from kernel to userspace, when the owner or starter of
24  * a well-known name changes */
25 struct kdbus_manager_msg_name_change {
26         __u64 old_id;
27         __u64 new_id;
28         __u64 flags;            /* 0 or (possibly?) KDBUS_CMD_NAME_IN_QUEUE */
29         char name[0];
30 };
31
32 struct kdbus_manager_msg_id_change {
33         __u64 id;
34         __u64 flags; /* The kernel flags field from KDBUS_CMD_HELLO */
35 };
36
37 struct kdbus_creds {
38         __u64 uid;
39         __u64 gid;
40         __u64 pid;
41         __u64 tid;
42
43         /* The starttime of the process PID. This is useful to detect
44         PID overruns from the client side. i.e. if you use the PID to
45         look something up in /proc/$PID/ you can afterwards check the
46         starttime field of it to ensure you didn't run into a PID
47         ovretun. */
48         __u64 starttime;
49 };
50
51 struct kdbus_audit {
52         __u64 sessionid;
53         __u64 loginuid;
54 };
55
56 struct kdbus_timestamp {
57         __u64 monotonic_ns;
58         __u64 realtime_ns;
59 };
60
61 #define KDBUS_SRC_ID_KERNEL             (0)
62 #define KDBUS_DST_ID_WELL_KNOWN_NAME    (0)
63 #define KDBUS_MATCH_SRC_ID_ANY          (~0ULL)
64 #define KDBUS_DST_ID_BROADCAST          (~0ULL)
65
66 /* Message Item Types */
67 enum {
68         /* Filled in by userspace */
69         KDBUS_MSG_NULL,                 /* empty record */
70         KDBUS_MSG_PAYLOAD,              /* .data */
71         KDBUS_MSG_PAYLOAD_VEC,          /* .data_vec */
72         KDBUS_MSG_UNIX_FDS,             /* .data_fds of file descriptors */
73         KDBUS_MSG_BLOOM,                /* for broadcasts, carries bloom filter blob in .data */
74         KDBUS_MSG_DST_NAME,             /* destination's well-known name, in .str */
75
76         /* Filled in by kernelspace */
77         KDBUS_MSG_SRC_NAMES     = 0x200,/* NUL separated string list with well-known names of source */
78         KDBUS_MSG_TIMESTAMP,            /* .timestamp */
79         KDBUS_MSG_SRC_CREDS,            /* .creds */
80         KDBUS_MSG_SRC_PID_COMM,         /* optional, in .str */
81         KDBUS_MSG_SRC_TID_COMM,         /* optional, in .str */
82         KDBUS_MSG_SRC_EXE,              /* optional, in .str */
83         KDBUS_MSG_SRC_CMDLINE,          /* optional, in .str (a chain of NUL str) */
84         KDBUS_MSG_SRC_CGROUP,           /* optional, in .str */
85         KDBUS_MSG_SRC_CAPS,             /* caps data blob, in .data */
86         KDBUS_MSG_SRC_SECLABEL,         /* NUL terminated string, in .str */
87         KDBUS_MSG_SRC_AUDIT,            /* .audit */
88
89         /* Special messages from kernel, consisting of one and only one of these data blocks */
90         KDBUS_MSG_NAME_ADD      = 0x400,/* .name_change */
91         KDBUS_MSG_NAME_REMOVE,          /* .name_change */
92         KDBUS_MSG_NAME_CHANGE,          /* .name_change */
93         KDBUS_MSG_ID_ADD,               /* .id_change */
94         KDBUS_MSG_ID_REMOVE,            /* .id_change */
95         KDBUS_MSG_ID_CHANGE,            /* .id_change */
96         KDBUS_MSG_REPLY_TIMEOUT,        /* empty, but .reply_cookie in .kdbus_msg is filled in */
97         KDBUS_MSG_REPLY_DEAD,           /* dito */
98 };
99
100 struct kdbus_vec {
101         __u64 address;
102         __u64 size;
103 };
104
105 /**
106  * struct  kdbus_msg_item - chain of data blocks
107  *
108  * size: overall data record size
109  * type: kdbus_msg_item type of data
110  */
111 struct kdbus_msg_item {
112         __u64 size;
113         __u64 type;
114         union {
115                 /* inline data */
116                 __u8 data[0];
117                 __u32 data32[0];
118                 __u64 data64[0];
119                 char str[0];
120
121                 /* data vector */
122                 struct kdbus_vec vec;
123
124                 /* specific fields */
125                 int fds[0];                             /* int array of file descriptors */
126                 struct kdbus_creds creds;
127                 struct kdbus_audit audit;
128                 struct kdbus_timestamp timestamp;
129                 struct kdbus_manager_msg_name_change name_change;
130                 struct kdbus_manager_msg_id_change id_change;
131         };
132 };
133
134 enum {
135         KDBUS_MSG_FLAGS_EXPECT_REPLY    = 1,
136         KDBUS_MSG_FLAGS_NO_AUTO_START   = 2, /* possibly? */
137 };
138
139 enum {
140         KDBUS_PAYLOAD_NONE      = 0,
141         KDBUS_PAYLOAD_DBUS1     = 0x4442757356657231ULL, /* 'DBusVer1' */
142         KDBUS_PAYLOAD_GVARIANT  = 0x4756617269616e74ULL, /* 'GVariant' */
143 };
144
145 /**
146  * struct kdbus_msg
147  *
148  * set by userspace:
149  * dst_id: destination id
150  * flags: KDBUS_MSG_FLAGS_*
151  * items: data records
152  *
153  * set by kernel:
154  * src_id: who sent the message
155  */
156 struct kdbus_msg {
157         __u64 size;
158         __u64 flags;
159         __u64 dst_id;           /* connection, 0 == name in data, ~0 broadcast */
160         __u64 src_id;           /* connection, 0 == kernel */
161         __u64 payload_type;     /* 'DBusVer1', 'GVariant', ... */
162         __u64 cookie;           /* userspace-supplied cookie */
163         union {
164                 __u64 cookie_reply;     /* cookie we reply to */
165                 __u64 timeout_ns;       /* timespan to wait for reply */
166         };
167         struct kdbus_msg_item items[0];
168 };
169
170 enum {
171         KDBUS_POLICY_NAME,
172         KDBUS_POLICY_ACCESS,
173 };
174
175 enum {
176         KDBUS_POLICY_USER,
177         KDBUS_POLICY_GROUP,
178         KDBUS_POLICY_WORLD,
179 };
180
181 enum {
182         KDBUS_POLICY_RECV       = 4,
183         KDBUS_POLICY_SEND       = 2,
184         KDBUS_POLICY_OWN        = 1,
185 };
186
187 struct kdbus_policy {
188         __u64 size;
189         __u64 type; /* NAME or ACCESS */
190         union {
191                 char name[0];
192                 struct {
193                         __u32 type;     /* USER, GROUP, WORLD */
194                         __u32 bits;     /* RECV, SEND, OWN */
195                         __u64 id;       /* uid, gid, 0 */
196                 } access;
197         };
198 };
199
200 struct kdbus_cmd_policy {
201         __u64 size;
202         __u8 buffer[0]; /* a series of KDBUS_POLICY_NAME plus one or
203                          * more KDBUS_POLICY_ACCESS each. */
204 };
205
206 enum {
207         KDBUS_CMD_HELLO_STARTER         =  1 <<  0,
208         KDBUS_CMD_HELLO_ACCEPT_FD       =  1 <<  1,
209         KDBUS_CMD_HELLO_ACCEPT_MMAP     =  1 <<  2,
210
211         /* The following have an effect on directed messages only --
212          * not for broadcasts */
213         KDBUS_CMD_HELLO_ATTACH_COMM     =  1 << 10,
214         KDBUS_CMD_HELLO_ATTACH_EXE      =  1 << 11,
215         KDBUS_CMD_HELLO_ATTACH_CMDLINE  =  1 << 12,
216         KDBUS_CMD_HELLO_ATTACH_CGROUP   =  1 << 13,
217         KDBUS_CMD_HELLO_ATTACH_CAPS     =  1 << 14,
218         KDBUS_CMD_HELLO_ATTACH_SECLABEL =  1 << 15,
219         KDBUS_CMD_HELLO_ATTACH_AUDIT    =  1 << 16,
220 };
221
222 /* Flags for kdbus_cmd_bus_make, kdbus_cmd_ep_make and
223  * kdbus_cmd_ns_make */
224 enum {
225         KDBUS_ACCESS_GROUP      =  1,
226         KDBUS_ACCESS_WORLD      =  2,
227         KDBUS_POLICY_OPEN       =  4,
228 };
229
230 struct kdbus_cmd_hello {
231         /* userspace → kernel, kernel → userspace */
232         __u64 conn_flags;       /* userspace specifies its
233                                  * capabilities and more, kernel
234                                  * returns its capabilites and
235                                  * more. Kernel might refuse client's
236                                  * capabilities by returning an error
237                                  * from KDBUS_CMD_HELLO */
238
239         /* userspace → kernel */
240         __u64 pid;              /* To allow translator services which
241                                  * connect to the bus on behalf of
242                                  * somebody else, allow specifiying
243                                  * the PID of the client to connect on
244                                  * behalf on. Normal clients should
245                                  * pass this as 0 (i.e. to do things
246                                  * under their own PID). Priviliged
247                                  * clients can pass != 0, to operate
248                                  * on behalf of somebody else. */
249
250         /* kernel → userspace */
251         __u64 bus_flags;        /* this is .flags copied verbatim from
252                                  * from original KDBUS_CMD_BUS_MAKE
253                                  * ioctl. It's intended to be useful
254                                  * to do negotiation of features of
255                                  * the payload that is transfreted. */
256         __u64 id;               /* peer id */
257         __u64 bloom_size;       /* The bloom filter size chosen by the
258                                  * bus owner */
259 };
260
261 struct kdbus_cmd_bus_make {
262         __u64 size;
263         __u64 flags;            /* userspace → kernel, kernel → userspace
264                                  * When creating a bus feature
265                                  * kernel negotiation. */
266         __u64 bus_flags;        /* userspace → kernel
267                                  * When a bus is created this value is
268                                  * copied verbatim into the bus
269                                  * structure and returned from
270                                  * KDBUS_CMD_HELLO, later */
271         __u64 cgroup_id;        /* the cgroup hierarchy ID for which
272                                  * to attach cgroup membership paths
273                                  * to messages. 0 if no cgroup data
274                                  * shall be attached. */
275         __u64 bloom_size;       /* Size of the bloom filter for this bus. */
276         char name[0];
277 };
278
279 struct kdbus_cmd_ep_make {
280         __u64 size;
281         __u64 flags;            /* userspace → kernel, kernel → userspace
282                                  * When creating an entry point
283                                  * feature kernel negotiation done the
284                                  * same way as for
285                                  * KDBUS_CMD_BUS_MAKE. Unused for
286                                  * now. */
287         char name[0];
288 };
289
290 struct kdbus_cmd_ns_make {
291         __u64 size;
292         __u64 flags;            /* userspace → kernel, kernel → userspace
293                                  * When creating an entry point
294                                  * feature kernel negotiation done the
295                                  * same way as for
296                                  * KDBUS_CMD_BUS_MAKE. Unused for
297                                  * now. */
298         char name[0];
299 };
300
301 enum {
302         /* userspace → kernel */
303         KDBUS_CMD_NAME_REPLACE_EXISTING         =  1,
304         KDBUS_CMD_NAME_QUEUE                    =  2,
305         KDBUS_CMD_NAME_ALLOW_REPLACEMENT        =  4,
306
307         /* kernel → userspace */
308         KDBUS_CMD_NAME_IN_QUEUE = 0x200,
309 };
310
311 struct kdbus_cmd_name {
312         __u64 size;
313         __u64 name_flags;
314         __u64 id;               /* We allow registration/deregestration of names of other peers */
315         __u64 conn_flags;
316         char name[0];
317 };
318
319 struct kdbus_cmd_names {
320         __u64 size;
321         struct kdbus_cmd_name names[0];
322 };
323
324 enum {
325         KDBUS_CMD_NAME_INFO_ITEM_NAME,
326         KDBUS_CMD_NAME_INFO_ITEM_SECLABEL,
327         KDBUS_CMD_NAME_INFO_ITEM_AUDIT,
328 };
329
330 struct kdbus_cmd_name_info_item {
331         __u64 size;
332         __u64 type;
333         __u8 items[0];
334 };
335
336 struct kdbus_cmd_name_info {
337         __u64 size;                     /* overall size of info */
338         __u64 flags;
339         __u64 id;                       /* either ID, or 0 and _ITEM_NAME follows */
340         struct kdbus_creds creds;
341         struct kdbus_cmd_name_info_item item[0]; /* list of item records */
342 };
343
344 enum {
345         KDBUS_CMD_MATCH_BLOOM,          /* Matches a mask blob against KDBUS_MSG_BLOOM */
346         KDBUS_CMD_MATCH_SRC_NAME,       /* Matches a name string against KDBUS_MSG_SRC_NAMES */
347         KDBUS_CMD_MATCH_NAME_ADD,       /* Matches a name string against KDBUS_MSG_NAME_ADD */
348         KDBUS_CMD_MATCH_NAME_REMOVE,    /* Matches a name string against KDBUS_MSG_NAME_REMOVE */
349         KDBUS_CMD_MATCH_NAME_CHANGE,    /* Matches a name string against KDBUS_MSG_NAME_CHANGE */
350         KDBUS_CMD_MATCH_ID_ADD,         /* Matches an ID against KDBUS_MSG_ID_ADD */
351         KDBUS_CMD_MATCH_ID_REMOVE,      /* Matches an ID against KDBUS_MSG_ID_REMOVE */
352         KDBUS_CMD_MATCH_ID_CHANGE,      /* Matches an ID against KDBUS_MSG_ID_CHANGE */
353 };
354
355 struct kdbus_cmd_match_item {
356         __u64 size;
357         __u64 type;
358         union {
359                 __u64 id;
360                 __u8 data[0];
361                 char str[0];
362         };
363 };
364
365 struct kdbus_cmd_match {
366         __u64 size;
367         __u64 id;       /* We allow registration/deregestration of matches for other peers */
368         __u64 cookie;   /* userspace supplied cookie; when removing; kernel deletes everything with same cookie */
369         __u64 src_id;   /* ~0: any. other: exact unique match */
370         struct kdbus_cmd_match_item items[0];
371 };
372
373 struct kdbus_cmd_monitor {
374         __u64 id;               /* We allow setting the monitor flag of other peers */
375         int enabled;            /* A boolean to enable/disable monitoring */
376 };
377
378 /* FD states:
379  * control nodes: unset
380  *   bus owner  (via KDBUS_CMD_BUS_MAKE)
381  *   ns owner   (via KDBUS_CMD_NS_MAKE)
382  *
383  * ep nodes: unset
384  *   connected  (via KDBUS_CMD_HELLO)
385  *   starter    (via KDBUS_CMD_HELLO with KDBUS_CMD_HELLO_STARTER)
386  *   ep owner   (via KDBUS_CMD_EP_MAKE)
387  */
388 enum kdbus_cmd {
389         /* kdbus control node commands: require unset state */
390         KDBUS_CMD_BUS_MAKE =            _IOWR(KDBUS_IOC_MAGIC, 0x00, struct kdbus_cmd_bus_make),
391         KDBUS_CMD_NS_MAKE =             _IOWR(KDBUS_IOC_MAGIC, 0x10, struct kdbus_cmd_ns_make),
392
393         /* kdbus control node commands: require bus owner state */
394         KDBUS_CMD_BUS_POLICY_SET =      _IOWR(KDBUS_IOC_MAGIC, 0x20, struct kdbus_cmd_policy),
395
396         /* kdbus ep node commands: require unset state */
397         KDBUS_CMD_EP_MAKE =             _IOWR(KDBUS_IOC_MAGIC, 0x30, struct kdbus_cmd_ep_make),
398         KDBUS_CMD_HELLO =               _IOWR(KDBUS_IOC_MAGIC, 0x31, struct kdbus_cmd_hello),
399
400         /* kdbus ep node commands: require connected state */
401         KDBUS_CMD_MSG_SEND =            _IOWR(KDBUS_IOC_MAGIC, 0x40, struct kdbus_msg),
402         KDBUS_CMD_MSG_RECV =            _IOWR(KDBUS_IOC_MAGIC, 0x41, struct kdbus_msg),
403
404         KDBUS_CMD_NAME_ACQUIRE =        _IOWR(KDBUS_IOC_MAGIC, 0x50, struct kdbus_cmd_name),
405         KDBUS_CMD_NAME_RELEASE =        _IOWR(KDBUS_IOC_MAGIC, 0x51, struct kdbus_cmd_name),
406         KDBUS_CMD_NAME_LIST =           _IOWR(KDBUS_IOC_MAGIC, 0x52, struct kdbus_cmd_names),
407         KDBUS_CMD_NAME_QUERY =          _IOWR(KDBUS_IOC_MAGIC, 0x53, struct kdbus_cmd_name_info),
408
409         KDBUS_CMD_MATCH_ADD =           _IOWR(KDBUS_IOC_MAGIC, 0x60, struct kdbus_cmd_match),
410         KDBUS_CMD_MATCH_REMOVE =        _IOWR(KDBUS_IOC_MAGIC, 0x61, struct kdbus_cmd_match),
411         KDBUS_CMD_MONITOR =             _IOWR(KDBUS_IOC_MAGIC, 0x62, struct kdbus_cmd_monitor),
412
413         /* kdbus ep node commands: require ep owner state */
414         KDBUS_CMD_EP_POLICY_SET =       _IOWR(KDBUS_IOC_MAGIC, 0x70, struct kdbus_cmd_policy),
415 };
416 #endif