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