chiark / gitweb /
bus: update kdbus.h
[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_notify_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_notify_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 size;
77         union {
78                 __u64 address;
79                 __u64 offset;
80         };
81 };
82
83 struct kdbus_memfd {
84         __u64 size;
85         int fd;
86         __u32 __pad;
87 };
88
89 /* Message Item Types */
90 enum {
91         _KDBUS_ITEM_NULL,
92
93         /* Filled in by userspace */
94         _KDBUS_ITEM_USER_BASE   = 1,
95         KDBUS_ITEM_PAYLOAD_VEC  = 1,    /* .data_vec, reference to memory area */
96         KDBUS_ITEM_PAYLOAD_OFF,         /* .data_vec, reference to memory area */
97         KDBUS_ITEM_PAYLOAD_MEMFD,       /* file descriptor of a special data file */
98         KDBUS_ITEM_FDS,                 /* .data_fds of file descriptors */
99         KDBUS_ITEM_BLOOM,               /* for broadcasts, carries bloom filter blob in .data */
100         KDBUS_ITEM_DST_NAME,            /* destination's well-known name, in .str */
101         KDBUS_ITEM_PRIORITY,            /* queue priority for message */
102
103         /* Filled in by kernelspace */
104         _KDBUS_ITEM_ATTACH_BASE = 0x400,
105         KDBUS_ITEM_NAMES        = 0x400,/* NUL separated string list with well-known names of source */
106         KDBUS_ITEM_TIMESTAMP,           /* .timestamp */
107
108         /* when appended to a message, the following items refer to the sender */
109         KDBUS_ITEM_CREDS,               /* .creds */
110         KDBUS_ITEM_PID_COMM,            /* optional, in .str */
111         KDBUS_ITEM_TID_COMM,            /* optional, in .str */
112         KDBUS_ITEM_EXE,                 /* optional, in .str */
113         KDBUS_ITEM_CMDLINE,             /* optional, in .str (a chain of NUL str) */
114         KDBUS_ITEM_CGROUP,              /* optional, in .str */
115         KDBUS_ITEM_CAPS,                /* caps data blob, in .data */
116         KDBUS_ITEM_SECLABEL,            /* NUL terminated string, in .str */
117         KDBUS_ITEM_AUDIT,               /* .audit */
118
119         /* Special messages from kernel, consisting of one and only one of these data blocks */
120         _KDBUS_ITEM_KERNEL_BASE = 0x800,
121         KDBUS_ITEM_NAME_ADD     = 0x800,/* .name_change */
122         KDBUS_ITEM_NAME_REMOVE,         /* .name_change */
123         KDBUS_ITEM_NAME_CHANGE,         /* .name_change */
124         KDBUS_ITEM_ID_ADD,              /* .id_change */
125         KDBUS_ITEM_ID_REMOVE,           /* .id_change */
126         KDBUS_ITEM_REPLY_TIMEOUT,       /* empty, but .reply_cookie in .kdbus_msg is filled in */
127         KDBUS_ITEM_REPLY_DEAD,          /* dito */
128 };
129
130 /**
131  * struct  kdbus_item - chain of data blocks
132  *
133  * size: overall data record size
134  * type: kdbus_item type of data
135  */
136 struct kdbus_item {
137         KDBUS_PART_HEADER;
138         union {
139                 /* inline data */
140                 __u8 data[0];
141                 __u32 data32[0];
142                 __u64 data64[0];
143                 char str[0];
144
145                 /* connection */
146                 __u64 id;
147
148                 /* data vector */
149                 struct kdbus_vec vec;
150
151                 /* process credentials and properties*/
152                 struct kdbus_creds creds;
153                 struct kdbus_audit audit;
154                 struct kdbus_timestamp timestamp;
155
156                 /* specific fields */
157                 struct kdbus_memfd memfd;
158                 int fds[0];
159                 struct kdbus_notify_name_change name_change;
160                 struct kdbus_notify_id_change id_change;
161         };
162 };
163
164 enum {
165         KDBUS_MSG_FLAGS_EXPECT_REPLY    = 1 << 0,
166         KDBUS_MSG_FLAGS_NO_AUTO_START   = 1 << 1,
167 };
168
169 enum {
170         KDBUS_PAYLOAD_KERNEL,
171         KDBUS_PAYLOAD_DBUS1     = 0x4442757356657231ULL, /* 'DBusVer1' */
172         KDBUS_PAYLOAD_GVARIANT  = 0x4756617269616e74ULL, /* 'GVariant' */
173 };
174
175 struct kdbus_msg {
176         __u64 size;
177         __u64 flags;
178         __u64 dst_id;                   /* connection, 0 == name in data, ~0 broadcast */
179         __u64 src_id;                   /* connection, 0 == kernel */
180         __u64 payload_type;             /* 'DBusVer1', 'GVariant', ... */
181         __u64 cookie;                   /* userspace-supplied cookie */
182         union {
183                 __u64 cookie_reply;     /* cookie we reply to */
184                 __u64 timeout_ns;       /* timespan to wait for reply */
185         };
186         struct kdbus_item items[0];
187 };
188
189 enum {
190         _KDBUS_POLICY_NULL,
191         KDBUS_POLICY_NAME,
192         KDBUS_POLICY_ACCESS,
193 };
194
195 enum {
196         _KDBUS_POLICY_ACCESS_NULL,
197         KDBUS_POLICY_ACCESS_USER,
198         KDBUS_POLICY_ACCESS_GROUP,
199         KDBUS_POLICY_ACCESS_WORLD,
200 };
201
202 enum {
203         KDBUS_POLICY_RECV               = 1 <<  2,
204         KDBUS_POLICY_SEND               = 1 <<  1,
205         KDBUS_POLICY_OWN                = 1 <<  0,
206 };
207
208 struct kdbus_policy_access {
209         __u64 type;                     /* USER, GROUP, WORLD */
210         __u64 bits;                     /* RECV, SEND, OWN */
211         __u64 id;                       /* uid, gid, 0 */
212 };
213
214 struct kdbus_policy {
215         KDBUS_PART_HEADER;
216         union {
217                 char name[0];
218                 struct kdbus_policy_access access;
219         };
220 };
221
222 /* A series of KDBUS_POLICY_NAME, plus one or more KDBUS_POLICY_ACCESS */
223 struct kdbus_cmd_policy {
224         __u64 size;
225         struct kdbus_policy policies[0];
226 };
227
228 /* Flags for struct kdbus_cmd_hello */
229 enum {
230         KDBUS_HELLO_STARTER             =  1 <<  0,
231         KDBUS_HELLO_ACCEPT_FD           =  1 <<  1,
232 };
233
234 /* Flags for message attachments */
235 enum {
236         KDBUS_ATTACH_TIMESTAMP          =  1 <<  0,
237         KDBUS_ATTACH_CREDS              =  1 <<  1,
238         KDBUS_ATTACH_NAMES              =  1 <<  2,
239         KDBUS_ATTACH_COMM               =  1 <<  3,
240         KDBUS_ATTACH_EXE                =  1 <<  4,
241         KDBUS_ATTACH_CMDLINE            =  1 <<  5,
242         KDBUS_ATTACH_CGROUP             =  1 <<  6,
243         KDBUS_ATTACH_CAPS               =  1 <<  7,
244         KDBUS_ATTACH_SECLABEL           =  1 <<  8,
245         KDBUS_ATTACH_AUDIT              =  1 <<  9,
246 };
247
248 /* KDBUS_CMD_HELLO */
249 struct kdbus_cmd_hello {
250         __u64 size;
251
252         /* userspace → kernel, kernel → userspace */
253         __u64 conn_flags;       /* userspace specifies its
254                                  * capabilities and more, kernel
255                                  * returns its capabilites and
256                                  * more. Kernel might refuse client's
257                                  * capabilities by returning an error
258                                  * from KDBUS_HELLO */
259         __u64 attach_flags;     /* userspace specifies the metadata
260                                  * attachments it wishes to receive with
261                                  * every message. */
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         __u64 pool_size;        /* maximum size of pool buffer */
273         __u8 id128[16];         /* the unique id of the bus */
274
275         struct kdbus_item items[0];
276 };
277
278 /* Flags for kdbus_cmd_{bus,ep,ns}_make */
279 enum {
280         KDBUS_MAKE_ACCESS_GROUP         = 1 <<  0,
281         KDBUS_MAKE_ACCESS_WORLD         = 1 <<  1,
282         KDBUS_MAKE_POLICY_OPEN          = 1 <<  2,
283 };
284
285 /* Items to append to kdbus_cmd_{bus,ep,ns}_make */
286 enum {
287         _KDBUS_MAKE_NULL,
288         KDBUS_MAKE_NAME,
289         KDBUS_MAKE_CRED,        /* allow translator services which connect
290                                  * to the bus on behalf of somebody else,
291                                  * allow specifying the credentials of the
292                                  * client to connect on behalf on. Needs
293                                  * privileges */
294 };
295
296 struct kdbus_cmd_bus_make {
297         __u64 size;
298         __u64 flags;            /* userspace → kernel, kernel → userspace
299                                  * When creating a bus feature
300                                  * kernel negotiation. */
301         __u64 bus_flags;        /* userspace → kernel
302                                  * When a bus is created this value is
303                                  * copied verbatim into the bus
304                                  * structure and returned from
305                                  * KDBUS_CMD_HELLO, later */
306         __u64 bloom_size;       /* size of the bloom filter for this bus */
307         struct kdbus_item items[0];
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 /* KDBUS_CMD_NAME_ACQUIRE */
343 struct kdbus_cmd_name {
344         __u64 size;
345         __u64 flags;
346         __u64 id;               /* we allow (de)registration of names of other peers */
347         __u64 conn_flags;
348         char name[0];
349 };
350
351 /* KDBUS_CMD_NAME_LIST */
352 enum {
353         KDBUS_NAME_LIST_UNIQUE_NAMES            = 1 <<  0,
354 };
355
356 struct kdbus_cmd_name_list {
357         __u64 flags;
358         __u64 offset;           /* returned offset in the caller's buffer */
359 };
360
361 struct kdbus_name_list {
362         __u64 size;
363         struct kdbus_cmd_name names[0];
364 };
365
366 /* KDBUS_CMD_NAME_INFO */
367 struct kdbus_cmd_name_info {
368         __u64 size;
369         __u64 flags;                    /* query flags */
370         __u64 attach_flags;             /* which meta data payload to attach */
371         __u64 id;                       /* either ID, or 0 and name follows */
372         __u64 offset;                   /* returned offset in the caller's buffer */
373         char name[0];
374 };
375
376 struct kdbus_name_info {
377         __u64 size;
378         __u64 id;
379         __u64 flags;                    /* connection flags */
380         struct kdbus_item items[0];     /* list of item records */
381 };
382
383 /* KDBUS_CMD_MATCH_ADD/REMOVE */
384 enum {
385         _KDBUS_MATCH_NULL,
386         KDBUS_MATCH_BLOOM,              /* Matches a mask blob against KDBUS_MSG_BLOOM */
387         KDBUS_MATCH_SRC_NAME,           /* Matches a name string against KDBUS_MSG_SRC_NAMES */
388         KDBUS_MATCH_NAME_ADD,           /* Matches a name string against KDBUS_MSG_NAME_ADD */
389         KDBUS_MATCH_NAME_REMOVE,        /* Matches a name string against KDBUS_MSG_NAME_REMOVE */
390         KDBUS_MATCH_NAME_CHANGE,        /* Matches a name string against KDBUS_MSG_NAME_CHANGE */
391         KDBUS_MATCH_ID_ADD,             /* Matches an ID against KDBUS_MSG_ID_ADD */
392         KDBUS_MATCH_ID_REMOVE,          /* Matches an ID against KDBUS_MSG_ID_REMOVE */
393 };
394
395 struct kdbus_cmd_match {
396         __u64 size;
397         __u64 id;       /* We allow registration/deregestration of matches for other peers */
398         __u64 cookie;   /* userspace supplied cookie; when removing; kernel deletes everything with same cookie */
399         __u64 src_id;   /* ~0: any. other: exact unique match */
400         struct kdbus_item items[0];
401 };
402
403 /* KDBUS_CMD_MONITOR */
404 struct kdbus_cmd_monitor {
405         __u64 id;               /* We allow setting the monitor flag of other peers */
406         unsigned int enable;    /* A boolean to enable/disable monitoring */
407         __u32 __pad;
408 };
409
410 enum {
411         /* kdbus control node commands: require unset state */
412         KDBUS_CMD_BUS_MAKE =            _IOW(KDBUS_IOC_MAGIC, 0x00, struct kdbus_cmd_bus_make),
413         KDBUS_CMD_NS_MAKE =             _IOR(KDBUS_IOC_MAGIC, 0x10, struct kdbus_cmd_ns_make),
414
415         /* kdbus ep node commands: require unset state */
416         KDBUS_CMD_EP_MAKE =             _IOW(KDBUS_IOC_MAGIC, 0x20, struct kdbus_cmd_ep_make),
417         KDBUS_CMD_HELLO =               _IOWR(KDBUS_IOC_MAGIC, 0x30, struct kdbus_cmd_hello),
418
419         /* kdbus ep node commands: require connected state */
420         KDBUS_CMD_MSG_SEND =            _IOW(KDBUS_IOC_MAGIC, 0x40, struct kdbus_msg),
421         KDBUS_CMD_MSG_RECV =            _IOR(KDBUS_IOC_MAGIC, 0x41, __u64 *),
422         KDBUS_CMD_FREE =                _IOW(KDBUS_IOC_MAGIC, 0x42, __u64 *),
423
424         KDBUS_CMD_NAME_ACQUIRE =        _IOWR(KDBUS_IOC_MAGIC, 0x50, struct kdbus_cmd_name),
425         KDBUS_CMD_NAME_RELEASE =        _IOW(KDBUS_IOC_MAGIC, 0x51, struct kdbus_cmd_name),
426         KDBUS_CMD_NAME_LIST =           _IOWR(KDBUS_IOC_MAGIC, 0x52, struct kdbus_cmd_name_list),
427         KDBUS_CMD_NAME_INFO =           _IOWR(KDBUS_IOC_MAGIC, 0x53, struct kdbus_cmd_name_info),
428
429         KDBUS_CMD_MATCH_ADD =           _IOW(KDBUS_IOC_MAGIC, 0x60, struct kdbus_cmd_match),
430         KDBUS_CMD_MATCH_REMOVE =        _IOW(KDBUS_IOC_MAGIC, 0x61, struct kdbus_cmd_match),
431         KDBUS_CMD_MONITOR =             _IOW(KDBUS_IOC_MAGIC, 0x62, struct kdbus_cmd_monitor),
432
433         /* kdbus ep node commands: require ep owner state */
434         KDBUS_CMD_EP_POLICY_SET =       _IOW(KDBUS_IOC_MAGIC, 0x70, struct kdbus_cmd_policy),
435
436         /* kdbus memfd commands: */
437         KDBUS_CMD_MEMFD_NEW =           _IOR(KDBUS_IOC_MAGIC, 0x80, int *),
438         KDBUS_CMD_MEMFD_SIZE_GET =      _IOR(KDBUS_IOC_MAGIC, 0x81, __u64 *),
439         KDBUS_CMD_MEMFD_SIZE_SET =      _IOW(KDBUS_IOC_MAGIC, 0x82, __u64 *),
440         KDBUS_CMD_MEMFD_SEAL_GET =      _IOR(KDBUS_IOC_MAGIC, 0x83, int *),
441         KDBUS_CMD_MEMFD_SEAL_SET =      _IO(KDBUS_IOC_MAGIC, 0x84),
442 };
443 #endif