chiark / gitweb /
dfc64ffe2414e84d2ae42eaf4d81ed9a5f1724a1
[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  * Copyright (C) 2013 Lennart Poettering
6  * Copyright (C) 2013 Daniel Mack <daniel@zonque.org>
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_H_
15 #define _KDBUS_H_
16
17 #ifndef __KERNEL__
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <linux/types.h>
21 #endif
22
23 #define KDBUS_IOC_MAGIC                 0x95
24 #define KDBUS_SRC_ID_KERNEL             (0)
25 #define KDBUS_DST_ID_WELL_KNOWN_NAME    (0)
26 #define KDBUS_MATCH_SRC_ID_ANY          (~0ULL)
27 #define KDBUS_DST_ID_BROADCAST          (~0ULL)
28
29 /* Common first elements in a structure which are used to iterate over
30  * a list of elements. */
31 #define KDBUS_PART_HEADER \
32         struct {                                                        \
33                 __u64 size;                                             \
34                 __u64 type;                                             \
35         }
36
37 /* Message sent from kernel to userspace, when the owner or starter of
38  * a well-known name changes */
39 struct kdbus_manager_msg_name_change {
40         __u64 old_id;
41         __u64 new_id;
42         __u64 flags;                    /* 0 or (possibly?) KDBUS_NAME_IN_QUEUE */
43         char name[0];
44 };
45
46 struct kdbus_manager_msg_id_change {
47         __u64 id;
48         __u64 flags;                    /* The kernel flags field from KDBUS_HELLO */
49 };
50
51 struct kdbus_creds {
52         __u64 uid;
53         __u64 gid;
54         __u64 pid;
55         __u64 tid;
56
57         /* The starttime of the process PID. This is useful to detect
58         PID overruns from the client side. i.e. if you use the PID to
59         look something up in /proc/$PID/ you can afterwards check the
60         starttime field of it to ensure you didn't run into a PID
61         ovretun. */
62         __u64 starttime;
63 };
64
65 struct kdbus_audit {
66         __u64 sessionid;
67         __u64 loginuid;
68 };
69
70 struct kdbus_timestamp {
71         __u64 monotonic_ns;
72         __u64 realtime_ns;
73 };
74
75 struct kdbus_vec {
76         __u64 address;
77         __u64 size;
78 };
79
80 struct kdbus_memfd {
81         __u64 size;
82         int fd;
83 };
84
85 /* Message Item Types */
86 enum {
87         _KDBUS_MSG_NULL,
88
89         /* Filled in by userspace */
90         KDBUS_MSG_PAYLOAD_VEC,          /* .data_vec, reference to memory area */
91         KDBUS_MSG_PAYLOAD_MEMFD,        /* file descriptor of a special data file */
92         KDBUS_MSG_FDS,                  /* .data_fds of file descriptors */
93         KDBUS_MSG_BLOOM,                /* for broadcasts, carries bloom filter blob in .data */
94         KDBUS_MSG_DST_NAME,             /* destination's well-known name, in .str */
95         KDBUS_MSG_PRIORITY,             /* queue priority for message */
96
97         /* Filled in by kernelspace */
98         KDBUS_MSG_SRC_NAMES     = 0x400,/* NUL separated string list with well-known names of source */
99         KDBUS_MSG_TIMESTAMP,            /* .timestamp */
100         KDBUS_MSG_SRC_CREDS,            /* .creds */
101         KDBUS_MSG_SRC_PID_COMM,         /* optional, in .str */
102         KDBUS_MSG_SRC_TID_COMM,         /* optional, in .str */
103         KDBUS_MSG_SRC_EXE,              /* optional, in .str */
104         KDBUS_MSG_SRC_CMDLINE,          /* optional, in .str (a chain of NUL str) */
105         KDBUS_MSG_SRC_CGROUP,           /* optional, in .str */
106         KDBUS_MSG_SRC_CAPS,             /* caps data blob, in .data */
107         KDBUS_MSG_SRC_SECLABEL,         /* NUL terminated string, in .str */
108         KDBUS_MSG_SRC_AUDIT,            /* .audit */
109
110         /* Special messages from kernel, consisting of one and only one of these data blocks */
111         KDBUS_MSG_NAME_ADD      = 0x800,/* .name_change */
112         KDBUS_MSG_NAME_REMOVE,          /* .name_change */
113         KDBUS_MSG_NAME_CHANGE,          /* .name_change */
114         KDBUS_MSG_ID_ADD,               /* .id_change */
115         KDBUS_MSG_ID_REMOVE,            /* .id_change */
116         KDBUS_MSG_REPLY_TIMEOUT,        /* empty, but .reply_cookie in .kdbus_msg is filled in */
117         KDBUS_MSG_REPLY_DEAD,           /* dito */
118 };
119
120 /**
121  * struct  kdbus_item - chain of data blocks
122  *
123  * size: overall data record size
124  * type: kdbus_item type of data
125  */
126 struct kdbus_item {
127         KDBUS_PART_HEADER;
128         union {
129                 /* inline data */
130                 __u8 data[0];
131                 __u32 data32[0];
132                 __u64 data64[0];
133                 char str[0];
134
135                 /* connection */
136                 __u64 id;
137
138                 /* data vector */
139                 struct kdbus_vec vec;
140
141                 /* process credentials and properties*/
142                 struct kdbus_creds creds;
143                 struct kdbus_audit audit;
144                 struct kdbus_timestamp timestamp;
145
146                 /* specific fields */
147                 struct kdbus_memfd memfd;
148                 int fds[0];
149                 struct kdbus_manager_msg_name_change name_change;
150                 struct kdbus_manager_msg_id_change id_change;
151         };
152 };
153
154 enum {
155         KDBUS_MSG_FLAGS_EXPECT_REPLY    = 1 << 0,
156         KDBUS_MSG_FLAGS_NO_AUTO_START   = 1 << 1,
157 };
158
159 enum {
160         KDBUS_PAYLOAD_KERNEL,
161         KDBUS_PAYLOAD_DBUS1     = 0x4442757356657231ULL, /* 'DBusVer1' */
162         KDBUS_PAYLOAD_GVARIANT  = 0x4756617269616e74ULL, /* 'GVariant' */
163 };
164
165 /**
166  * struct kdbus_msg
167  *
168  * set by userspace:
169  * dst_id: destination id
170  * flags: KDBUS_MSG_FLAGS_*
171  * items: data records
172  *
173  * set by kernel:
174  * src_id: who sent the message
175  */
176 struct kdbus_msg {
177         __u64 size;
178         __u64 flags;
179         __u64 dst_id;                   /* connection, 0 == name in data, ~0 broadcast */
180         __u64 src_id;                   /* connection, 0 == kernel */
181         __u64 payload_type;             /* 'DBusVer1', 'GVariant', ... */
182         __u64 cookie;                   /* userspace-supplied cookie */
183         union {
184                 __u64 cookie_reply;     /* cookie we reply to */
185                 __u64 timeout_ns;       /* timespan to wait for reply */
186         };
187         struct kdbus_item items[0];
188 };
189
190 enum {
191         _KDBUS_POLICY_NULL,
192         KDBUS_POLICY_NAME,
193         KDBUS_POLICY_ACCESS,
194 };
195
196 enum {
197         _KDBUS_POLICY_ACCESS_NULL,
198         KDBUS_POLICY_ACCESS_USER,
199         KDBUS_POLICY_ACCESS_GROUP,
200         KDBUS_POLICY_ACCESS_WORLD,
201 };
202
203 enum {
204         KDBUS_POLICY_RECV               = 1 <<  2,
205         KDBUS_POLICY_SEND               = 1 <<  1,
206         KDBUS_POLICY_OWN                = 1 <<  0,
207 };
208
209 struct kdbus_policy_access {
210         __u64 type;             /* USER, GROUP, WORLD */
211         __u64 bits;             /* RECV, SEND, OWN */
212         __u64 id;               /* uid, gid, 0 */
213 };
214
215 struct kdbus_policy {
216         KDBUS_PART_HEADER;
217         union {
218                 char name[0];
219                 struct kdbus_policy_access access;
220         };
221 };
222
223 /* A series of KDBUS_POLICY_NAME, plus one or more KDBUS_POLICY_ACCESS */
224 struct kdbus_cmd_policy {
225         __u64 size;
226         struct kdbus_policy policies[0];
227 };
228
229 /* Flags for struct kdbus_cmd_hello */
230 enum {
231         KDBUS_HELLO_STARTER             =  1 <<  0,
232         KDBUS_HELLO_ACCEPT_FD           =  1 <<  1,
233
234         /* The following have an effect on directed messages only --
235          * not for broadcasts */
236         KDBUS_HELLO_ATTACH_COMM         =  1 << 10,
237         KDBUS_HELLO_ATTACH_EXE          =  1 << 11,
238         KDBUS_HELLO_ATTACH_CMDLINE      =  1 << 12,
239         KDBUS_HELLO_ATTACH_CGROUP       =  1 << 13,
240         KDBUS_HELLO_ATTACH_CAPS         =  1 << 14,
241         KDBUS_HELLO_ATTACH_SECLABEL     =  1 << 15,
242         KDBUS_HELLO_ATTACH_AUDIT        =  1 << 16,
243 };
244
245 /* Items to append to struct kdbus_cmd_hello */
246 enum {
247         _KDBUS_HELLO_NULL,
248         KDBUS_HELLO_POOL,       /* kdbus_vec, userspace supplied pool to
249                                  * place received messages */
250 };
251
252 struct kdbus_cmd_hello {
253         __u64 size;
254
255         /* userspace → kernel, kernel → userspace */
256         __u64 conn_flags;       /* userspace specifies its
257                                  * capabilities and more, kernel
258                                  * returns its capabilites and
259                                  * more. Kernel might refuse client's
260                                  * capabilities by returning an error
261                                  * from KDBUS_HELLO */
262
263         /* kernel → userspace */
264         __u64 bus_flags;        /* this is .flags copied verbatim from
265                                  * from original KDBUS_CMD_BUS_MAKE
266                                  * ioctl. It's intended to be useful
267                                  * to do negotiation of features of
268                                  * the payload that is transfreted. */
269         __u64 id;               /* id assigned to this connection */
270         __u64 bloom_size;       /* The bloom filter size chosen by the
271                                  * bus owner */
272         struct kdbus_item items[0];
273 };
274
275 /* Flags for kdbus_cmd_{bus,ep,ns}_make */
276 enum {
277         KDBUS_MAKE_ACCESS_GROUP         = 1 <<  0,
278         KDBUS_MAKE_ACCESS_WORLD         = 1 <<  1,
279         KDBUS_MAKE_POLICY_OPEN          = 1 <<  2,
280 };
281
282 /* Items to append to kdbus_cmd_{bus,ep,ns}_make */
283 enum {
284         _KDBUS_MAKE_NULL,
285         KDBUS_MAKE_NAME,
286         KDBUS_MAKE_CGROUP,      /* the cgroup hierarchy ID for which to attach
287                                  * cgroup membership paths * to messages. */
288         KDBUS_MAKE_CRED,        /* allow translator services which connect
289                                  * to the bus on behalf of somebody else,
290                                  * allow specifiying the credentials of the
291                                  * client to connect on behalf on. Needs
292                                  * privileges */
293 };
294
295 struct kdbus_cmd_bus_make {
296         __u64 size;
297         __u64 flags;            /* userspace → kernel, kernel → userspace
298                                  * When creating a bus feature
299                                  * kernel negotiation. */
300         __u64 bus_flags;        /* userspace → kernel
301                                  * When a bus is created this value is
302                                  * copied verbatim into the bus
303                                  * structure and returned from
304                                  * KDBUS_CMD_HELLO, later */
305         __u64 bloom_size;       /* size of the bloom filter for this bus */
306         struct kdbus_item items[0];
307
308 };
309
310 struct kdbus_cmd_ep_make {
311         __u64 size;
312         __u64 flags;            /* userspace → kernel, kernel → userspace
313                                  * When creating an entry point
314                                  * feature kernel negotiation done the
315                                  * same way as for
316                                  * KDBUS_CMD_BUS_MAKE. Unused for
317                                  * now. */
318         struct kdbus_item items[0];
319 };
320
321 struct kdbus_cmd_ns_make {
322         __u64 size;
323         __u64 flags;            /* userspace → kernel, kernel → userspace
324                                  * When creating an entry point
325                                  * feature kernel negotiation done the
326                                  * same way as for
327                                  * KDBUS_CMD_BUS_MAKE. Unused for
328                                  * now. */
329         struct kdbus_item items[0];
330 };
331
332 enum {
333         /* userspace → kernel */
334         KDBUS_NAME_REPLACE_EXISTING             = 1 <<  0,
335         KDBUS_NAME_QUEUE                        = 1 <<  1,
336         KDBUS_NAME_ALLOW_REPLACEMENT            = 1 <<  2,
337
338         /* kernel → userspace */
339         KDBUS_NAME_IN_QUEUE                     = 1 << 16,
340 };
341
342 /* We allow (de)regestration of names of other peers */
343 struct kdbus_cmd_name {
344         __u64 size;
345         __u64 flags;
346         __u64 id;
347         __u64 conn_flags;
348         char name[0];
349 };
350
351 struct kdbus_cmd_names {
352         __u64 size;
353         struct kdbus_cmd_name names[0];
354 };
355
356 enum {
357         _KDBUS_NAME_INFO_ITEM_NULL,
358         KDBUS_NAME_INFO_ITEM_NAME,      /* userspace → kernel */
359         KDBUS_NAME_INFO_ITEM_SECLABEL,  /* kernel → userspace */
360         KDBUS_NAME_INFO_ITEM_AUDIT,     /* kernel → userspace */
361 };
362
363 struct kdbus_cmd_name_info {
364         __u64 size;                     /* overall size of info */
365         __u64 flags;
366         __u64 id;                       /* either ID, or 0 and _ITEM_NAME follows */
367         struct kdbus_creds creds;
368         struct kdbus_item items[0];     /* list of item records */
369 };
370
371 enum {
372         _KDBUS_MATCH_NULL,
373         KDBUS_MATCH_BLOOM,              /* Matches a mask blob against KDBUS_MSG_BLOOM */
374         KDBUS_MATCH_SRC_NAME,           /* Matches a name string against KDBUS_MSG_SRC_NAMES */
375         KDBUS_MATCH_NAME_ADD,           /* Matches a name string against KDBUS_MSG_NAME_ADD */
376         KDBUS_MATCH_NAME_REMOVE,        /* Matches a name string against KDBUS_MSG_NAME_REMOVE */
377         KDBUS_MATCH_NAME_CHANGE,        /* Matches a name string against KDBUS_MSG_NAME_CHANGE */
378         KDBUS_MATCH_ID_ADD,             /* Matches an ID against KDBUS_MSG_ID_ADD */
379         KDBUS_MATCH_ID_REMOVE,          /* Matches an ID against KDBUS_MSG_ID_REMOVE */
380 };
381
382 struct kdbus_cmd_match {
383         __u64 size;
384         __u64 id;       /* We allow registration/deregestration of matches for other peers */
385         __u64 cookie;   /* userspace supplied cookie; when removing; kernel deletes everything with same cookie */
386         __u64 src_id;   /* ~0: any. other: exact unique match */
387         struct kdbus_item items[0];
388 };
389
390 struct kdbus_cmd_monitor {
391         __u64 id;               /* We allow setting the monitor flag of other peers */
392         unsigned int enable;    /* A boolean to enable/disable monitoring */
393 };
394
395 /* FD states:
396  * control nodes: unset
397  *   bus owner  (via KDBUS_CMD_BUS_MAKE)
398  *   ns owner   (via KDBUS_CMD_NS_MAKE)
399  *
400  * ep nodes: unset
401  *   connected  (via KDBUS_CMD_HELLO)
402  *   starter    (via KDBUS_CMD_HELLO with KDBUS_CMD_HELLO_STARTER)
403  *   ep owner   (via KDBUS_CMD_EP_MAKE)
404  */
405 enum {
406         /* kdbus control node commands: require unset state */
407         KDBUS_CMD_BUS_MAKE =            _IOWR(KDBUS_IOC_MAGIC, 0x00, struct kdbus_cmd_bus_make),
408         KDBUS_CMD_NS_MAKE =             _IOWR(KDBUS_IOC_MAGIC, 0x10, struct kdbus_cmd_ns_make),
409
410         /* kdbus ep node commands: require unset state */
411         KDBUS_CMD_EP_MAKE =             _IOWR(KDBUS_IOC_MAGIC, 0x20, struct kdbus_cmd_ep_make),
412         KDBUS_CMD_HELLO =               _IOWR(KDBUS_IOC_MAGIC, 0x30, struct kdbus_cmd_hello),
413
414         /* kdbus ep node commands: require connected state */
415         KDBUS_CMD_MSG_SEND =            _IOWR(KDBUS_IOC_MAGIC, 0x40, struct kdbus_msg),
416         KDBUS_CMD_MSG_RECV =            _IOWR(KDBUS_IOC_MAGIC, 0x41, __u64 *),
417         KDBUS_CMD_MSG_RELEASE =         _IOWR(KDBUS_IOC_MAGIC, 0x42, struct kdbus_msg),
418
419         KDBUS_CMD_NAME_ACQUIRE =        _IOWR(KDBUS_IOC_MAGIC, 0x50, struct kdbus_cmd_name),
420         KDBUS_CMD_NAME_RELEASE =        _IOWR(KDBUS_IOC_MAGIC, 0x51, struct kdbus_cmd_name),
421         KDBUS_CMD_NAME_LIST =           _IOWR(KDBUS_IOC_MAGIC, 0x52, struct kdbus_cmd_names),
422         KDBUS_CMD_NAME_QUERY =          _IOWR(KDBUS_IOC_MAGIC, 0x53, struct kdbus_cmd_name_info),
423
424         KDBUS_CMD_MATCH_ADD =           _IOWR(KDBUS_IOC_MAGIC, 0x60, struct kdbus_cmd_match),
425         KDBUS_CMD_MATCH_REMOVE =        _IOWR(KDBUS_IOC_MAGIC, 0x61, struct kdbus_cmd_match),
426         KDBUS_CMD_MONITOR =             _IOWR(KDBUS_IOC_MAGIC, 0x62, struct kdbus_cmd_monitor),
427
428         /* kdbus ep node commands: require ep owner state */
429         KDBUS_CMD_EP_POLICY_SET =       _IOWR(KDBUS_IOC_MAGIC, 0x70, struct kdbus_cmd_policy),
430
431         /* kdbus memfd commands: */
432         KDBUS_CMD_MEMFD_NEW =           _IOWR(KDBUS_IOC_MAGIC, 0x80, int *),
433         KDBUS_CMD_MEMFD_SIZE_GET =      _IOWR(KDBUS_IOC_MAGIC, 0x81, __u64 *),
434         KDBUS_CMD_MEMFD_SIZE_SET =      _IOWR(KDBUS_IOC_MAGIC, 0x82, __u64 *),
435         KDBUS_CMD_MEMFD_SEAL_GET =      _IOWR(KDBUS_IOC_MAGIC, 0x83, int *),
436         KDBUS_CMD_MEMFD_SEAL_SET =      _IOWR(KDBUS_IOC_MAGIC, 0x84, int),
437 };
438 #endif