chiark / gitweb /
c69c1feec79e691c659c9466d8917849486718ce
[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 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_MSG_NULL,
92
93         /* Filled in by userspace */
94         KDBUS_MSG_PAYLOAD_VEC,          /* .data_vec, reference to memory area */
95         KDBUS_MSG_PAYLOAD_OFF,          /* .data_vec, reference to memory area */
96         KDBUS_MSG_PAYLOAD_MEMFD,        /* file descriptor of a special data file */
97         KDBUS_MSG_FDS,                  /* .data_fds of file descriptors */
98         KDBUS_MSG_BLOOM,                /* for broadcasts, carries bloom filter blob in .data */
99         KDBUS_MSG_DST_NAME,             /* destination's well-known name, in .str */
100         KDBUS_MSG_PRIORITY,             /* queue priority for message */
101
102         /* Filled in by kernelspace */
103         KDBUS_MSG_SRC_NAMES     = 0x400,/* NUL separated string list with well-known names of source */
104         KDBUS_MSG_TIMESTAMP,            /* .timestamp */
105         KDBUS_MSG_SRC_CREDS,            /* .creds */
106         KDBUS_MSG_SRC_PID_COMM,         /* optional, in .str */
107         KDBUS_MSG_SRC_TID_COMM,         /* optional, in .str */
108         KDBUS_MSG_SRC_EXE,              /* optional, in .str */
109         KDBUS_MSG_SRC_CMDLINE,          /* optional, in .str (a chain of NUL str) */
110         KDBUS_MSG_SRC_CGROUP,           /* optional, in .str */
111         KDBUS_MSG_SRC_CAPS,             /* caps data blob, in .data */
112         KDBUS_MSG_SRC_SECLABEL,         /* NUL terminated string, in .str */
113         KDBUS_MSG_SRC_AUDIT,            /* .audit */
114
115         /* Special messages from kernel, consisting of one and only one of these data blocks */
116         KDBUS_MSG_NAME_ADD      = 0x800,/* .name_change */
117         KDBUS_MSG_NAME_REMOVE,          /* .name_change */
118         KDBUS_MSG_NAME_CHANGE,          /* .name_change */
119         KDBUS_MSG_ID_ADD,               /* .id_change */
120         KDBUS_MSG_ID_REMOVE,            /* .id_change */
121         KDBUS_MSG_REPLY_TIMEOUT,        /* empty, but .reply_cookie in .kdbus_msg is filled in */
122         KDBUS_MSG_REPLY_DEAD,           /* dito */
123 };
124
125 /**
126  * struct  kdbus_item - chain of data blocks
127  *
128  * size: overall data record size
129  * type: kdbus_item type of data
130  */
131 struct kdbus_item {
132         KDBUS_PART_HEADER;
133         union {
134                 /* inline data */
135                 __u8 data[0];
136                 __u32 data32[0];
137                 __u64 data64[0];
138                 char str[0];
139
140                 /* connection */
141                 __u64 id;
142
143                 /* data vector */
144                 struct kdbus_vec vec;
145
146                 /* process credentials and properties*/
147                 struct kdbus_creds creds;
148                 struct kdbus_audit audit;
149                 struct kdbus_timestamp timestamp;
150
151                 /* specific fields */
152                 struct kdbus_memfd memfd;
153                 int fds[0];
154                 struct kdbus_manager_msg_name_change name_change;
155                 struct kdbus_manager_msg_id_change id_change;
156         };
157 };
158
159 enum {
160         KDBUS_MSG_FLAGS_EXPECT_REPLY    = 1 << 0,
161         KDBUS_MSG_FLAGS_NO_AUTO_START   = 1 << 1,
162 };
163
164 enum {
165         KDBUS_PAYLOAD_KERNEL,
166         KDBUS_PAYLOAD_DBUS1     = 0x4442757356657231ULL, /* 'DBusVer1' */
167         KDBUS_PAYLOAD_GVARIANT  = 0x4756617269616e74ULL, /* 'GVariant' */
168 };
169
170 /**
171  * struct kdbus_msg
172  *
173  * set by userspace:
174  * dst_id: destination id
175  * flags: KDBUS_MSG_FLAGS_*
176  * items: data records
177  *
178  * set by kernel:
179  * src_id: who sent the message
180  */
181 struct kdbus_msg {
182         __u64 size;
183         __u64 flags;
184         __u64 dst_id;                   /* connection, 0 == name in data, ~0 broadcast */
185         __u64 src_id;                   /* connection, 0 == kernel */
186         __u64 payload_type;             /* 'DBusVer1', 'GVariant', ... */
187         __u64 cookie;                   /* userspace-supplied cookie */
188         union {
189                 __u64 cookie_reply;     /* cookie we reply to */
190                 __u64 timeout_ns;       /* timespan to wait for reply */
191         };
192         struct kdbus_item items[0];
193 };
194
195 enum {
196         _KDBUS_POLICY_NULL,
197         KDBUS_POLICY_NAME,
198         KDBUS_POLICY_ACCESS,
199 };
200
201 enum {
202         _KDBUS_POLICY_ACCESS_NULL,
203         KDBUS_POLICY_ACCESS_USER,
204         KDBUS_POLICY_ACCESS_GROUP,
205         KDBUS_POLICY_ACCESS_WORLD,
206 };
207
208 enum {
209         KDBUS_POLICY_RECV               = 1 <<  2,
210         KDBUS_POLICY_SEND               = 1 <<  1,
211         KDBUS_POLICY_OWN                = 1 <<  0,
212 };
213
214 struct kdbus_policy_access {
215         __u64 type;             /* USER, GROUP, WORLD */
216         __u64 bits;             /* RECV, SEND, OWN */
217         __u64 id;               /* uid, gid, 0 */
218 };
219
220 //FIXME: convert access to access[]
221 struct kdbus_policy {
222         KDBUS_PART_HEADER;
223         union {
224                 char name[0];
225                 struct kdbus_policy_access access;
226         };
227 };
228
229 /* A series of KDBUS_POLICY_NAME, plus one or more KDBUS_POLICY_ACCESS */
230 struct kdbus_cmd_policy {
231         __u64 size;
232         struct kdbus_policy policies[0];
233 };
234
235 /* Flags for struct kdbus_cmd_hello */
236 enum {
237         KDBUS_HELLO_STARTER             =  1 <<  0,
238         KDBUS_HELLO_ACCEPT_FD           =  1 <<  1,
239 };
240
241 /* Flags for message attachments */
242 enum {
243         KDBUS_ATTACH_TIMESTAMP          =  1 <<  0,
244         KDBUS_ATTACH_CREDS              =  1 <<  1,
245         KDBUS_ATTACH_NAMES              =  1 <<  2,
246         KDBUS_ATTACH_COMM               =  1 <<  3,
247         KDBUS_ATTACH_EXE                =  1 <<  4,
248         KDBUS_ATTACH_CMDLINE            =  1 <<  5,
249         KDBUS_ATTACH_CGROUP             =  1 <<  6,
250         KDBUS_ATTACH_CAPS               =  1 <<  7,
251         KDBUS_ATTACH_SECLABEL           =  1 <<  8,
252         KDBUS_ATTACH_AUDIT              =  1 <<  9,
253 };
254
255 struct kdbus_cmd_hello {
256         __u64 size;
257
258         /* userspace → kernel, kernel → userspace */
259         __u64 conn_flags;       /* userspace specifies its
260                                  * capabilities and more, kernel
261                                  * returns its capabilites and
262                                  * more. Kernel might refuse client's
263                                  * capabilities by returning an error
264                                  * from KDBUS_HELLO */
265         __u64 attach_flags;     /* userspace specifies the metadata
266                                  * attachments it wishes to receive with
267                                  * every message. */
268
269         /* kernel → userspace */
270         __u64 bus_flags;        /* this is .flags copied verbatim from
271                                  * from original KDBUS_CMD_BUS_MAKE
272                                  * ioctl. It's intended to be useful
273                                  * to do negotiation of features of
274                                  * the payload that is transfreted. */
275         __u64 id;               /* id assigned to this connection */
276         __u64 bloom_size;       /* The bloom filter size chosen by the
277                                  * bus owner */
278         __u64 pool_size;        /* maximum size of pool buffer */
279         __u8 id128[16];         /* the unique id of the bus */
280
281         struct kdbus_item items[0];
282 };
283
284 /* Flags for kdbus_cmd_{bus,ep,ns}_make */
285 enum {
286         KDBUS_MAKE_ACCESS_GROUP         = 1 <<  0,
287         KDBUS_MAKE_ACCESS_WORLD         = 1 <<  1,
288         KDBUS_MAKE_POLICY_OPEN          = 1 <<  2,
289 };
290
291 /* Items to append to kdbus_cmd_{bus,ep,ns}_make */
292 enum {
293         _KDBUS_MAKE_NULL,
294         KDBUS_MAKE_NAME,
295         KDBUS_MAKE_CRED,        /* allow translator services which connect
296                                  * to the bus on behalf of somebody else,
297                                  * allow specifying the credentials of the
298                                  * client to connect on behalf on. Needs
299                                  * privileges */
300 };
301
302 struct kdbus_cmd_bus_make {
303         __u64 size;
304         __u64 flags;            /* userspace → kernel, kernel → userspace
305                                  * When creating a bus feature
306                                  * kernel negotiation. */
307         __u64 bus_flags;        /* userspace → kernel
308                                  * When a bus is created this value is
309                                  * copied verbatim into the bus
310                                  * structure and returned from
311                                  * KDBUS_CMD_HELLO, later */
312         __u64 bloom_size;       /* size of the bloom filter for this bus */
313         struct kdbus_item items[0];
314 };
315
316 struct kdbus_cmd_ep_make {
317         __u64 size;
318         __u64 flags;            /* userspace → kernel, kernel → userspace
319                                  * When creating an entry point
320                                  * feature kernel negotiation done the
321                                  * same way as for
322                                  * KDBUS_CMD_BUS_MAKE. Unused for
323                                  * now. */
324         struct kdbus_item items[0];
325 };
326
327 struct kdbus_cmd_ns_make {
328         __u64 size;
329         __u64 flags;            /* userspace → kernel, kernel → userspace
330                                  * When creating an entry point
331                                  * feature kernel negotiation done the
332                                  * same way as for
333                                  * KDBUS_CMD_BUS_MAKE. Unused for
334                                  * now. */
335         struct kdbus_item items[0];
336 };
337
338 enum {
339         /* userspace → kernel */
340         KDBUS_NAME_REPLACE_EXISTING             = 1 <<  0,
341         KDBUS_NAME_QUEUE                        = 1 <<  1,
342         KDBUS_NAME_ALLOW_REPLACEMENT            = 1 <<  2,
343
344         /* kernel → userspace */
345         KDBUS_NAME_IN_QUEUE                     = 1 << 16,
346 };
347
348 /* We allow (de)regestration of names of other peers */
349 struct kdbus_cmd_name {
350         __u64 size;
351         __u64 flags;
352         __u64 id;
353         __u64 conn_flags;
354         char name[0];
355 };
356
357 enum {
358         KDBUS_NAME_LIST_UNIQUE_NAMES            = 1 <<  0,
359 };
360
361 struct kdbus_cmd_names {
362         __u64 size;
363         __u64 flags;
364         struct kdbus_cmd_name names[0];
365 };
366
367 enum {
368         _KDBUS_NAME_INFO_ITEM_NULL,
369         KDBUS_NAME_INFO_ITEM_NAME,      /* userspace → kernel */
370         KDBUS_NAME_INFO_ITEM_SECLABEL,  /* kernel → userspace */
371         KDBUS_NAME_INFO_ITEM_AUDIT,     /* kernel → userspace */
372 };
373
374 struct kdbus_cmd_name_info {
375         __u64 size;                     /* overall size of info */
376         __u64 flags;                    /* query flags */
377         __u64 attach_flags;             /* which meta data payload to attach */
378         __u64 id;                       /* either ID, or 0 and _ITEM_NAME follows */
379         struct kdbus_creds creds;
380         struct kdbus_item items[0];     /* list of item records */
381 };
382
383 enum {
384         _KDBUS_MATCH_NULL,
385         KDBUS_MATCH_BLOOM,              /* Matches a mask blob against KDBUS_MSG_BLOOM */
386         KDBUS_MATCH_SRC_NAME,           /* Matches a name string against KDBUS_MSG_SRC_NAMES */
387         KDBUS_MATCH_NAME_ADD,           /* Matches a name string against KDBUS_MSG_NAME_ADD */
388         KDBUS_MATCH_NAME_REMOVE,        /* Matches a name string against KDBUS_MSG_NAME_REMOVE */
389         KDBUS_MATCH_NAME_CHANGE,        /* Matches a name string against KDBUS_MSG_NAME_CHANGE */
390         KDBUS_MATCH_ID_ADD,             /* Matches an ID against KDBUS_MSG_ID_ADD */
391         KDBUS_MATCH_ID_REMOVE,          /* Matches an ID against KDBUS_MSG_ID_REMOVE */
392 };
393
394 struct kdbus_cmd_match {
395         __u64 size;
396         __u64 id;       /* We allow registration/deregestration of matches for other peers */
397         __u64 cookie;   /* userspace supplied cookie; when removing; kernel deletes everything with same cookie */
398         __u64 src_id;   /* ~0: any. other: exact unique match */
399         struct kdbus_item items[0];
400 };
401
402 struct kdbus_cmd_monitor {
403         __u64 id;               /* We allow setting the monitor flag of other peers */
404         unsigned int enable;    /* A boolean to enable/disable monitoring */
405         __u32 __pad;
406 };
407
408 /* FD states:
409  * control nodes: unset
410  *   bus owner  (via KDBUS_CMD_BUS_MAKE)
411  *   ns owner   (via KDBUS_CMD_NS_MAKE)
412  *
413  * ep nodes: unset
414  *   connected  (via KDBUS_CMD_HELLO)
415  *   starter    (via KDBUS_CMD_HELLO with KDBUS_CMD_HELLO_STARTER)
416  *   ep owner   (via KDBUS_CMD_EP_MAKE)
417  */
418 enum {
419         /* kdbus control node commands: require unset state */
420         KDBUS_CMD_BUS_MAKE =            _IOW(KDBUS_IOC_MAGIC, 0x00, struct kdbus_cmd_bus_make),
421         KDBUS_CMD_NS_MAKE =             _IOR(KDBUS_IOC_MAGIC, 0x10, struct kdbus_cmd_ns_make),
422
423         /* kdbus ep node commands: require unset state */
424         KDBUS_CMD_EP_MAKE =             _IOW(KDBUS_IOC_MAGIC, 0x20, struct kdbus_cmd_ep_make),
425         KDBUS_CMD_HELLO =               _IOWR(KDBUS_IOC_MAGIC, 0x30, struct kdbus_cmd_hello),
426
427         /* kdbus ep node commands: require connected state */
428         KDBUS_CMD_MSG_SEND =            _IOW(KDBUS_IOC_MAGIC, 0x40, struct kdbus_msg),
429         KDBUS_CMD_MSG_RECV =            _IOR(KDBUS_IOC_MAGIC, 0x41, __u64 *),
430         KDBUS_CMD_MSG_RELEASE =         _IOW(KDBUS_IOC_MAGIC, 0x42, __u64 *),
431
432         KDBUS_CMD_NAME_ACQUIRE =        _IOWR(KDBUS_IOC_MAGIC, 0x50, struct kdbus_cmd_name),
433         KDBUS_CMD_NAME_RELEASE =        _IOW(KDBUS_IOC_MAGIC, 0x51, struct kdbus_cmd_name),
434         KDBUS_CMD_NAME_LIST =           _IOWR(KDBUS_IOC_MAGIC, 0x52, struct kdbus_cmd_names),
435         KDBUS_CMD_NAME_QUERY =          _IOWR(KDBUS_IOC_MAGIC, 0x53, struct kdbus_cmd_name_info),
436
437         KDBUS_CMD_MATCH_ADD =           _IOW(KDBUS_IOC_MAGIC, 0x60, struct kdbus_cmd_match),
438         KDBUS_CMD_MATCH_REMOVE =        _IOW(KDBUS_IOC_MAGIC, 0x61, struct kdbus_cmd_match),
439         KDBUS_CMD_MONITOR =             _IOW(KDBUS_IOC_MAGIC, 0x62, struct kdbus_cmd_monitor),
440
441         /* kdbus ep node commands: require ep owner state */
442         KDBUS_CMD_EP_POLICY_SET =       _IOW(KDBUS_IOC_MAGIC, 0x70, struct kdbus_cmd_policy),
443
444         /* kdbus memfd commands: */
445         KDBUS_CMD_MEMFD_NEW =           _IOR(KDBUS_IOC_MAGIC, 0x80, int *),
446         KDBUS_CMD_MEMFD_SIZE_GET =      _IOR(KDBUS_IOC_MAGIC, 0x81, __u64 *),
447         KDBUS_CMD_MEMFD_SIZE_SET =      _IOW(KDBUS_IOC_MAGIC, 0x82, __u64 *),
448         KDBUS_CMD_MEMFD_SEAL_GET =      _IOR(KDBUS_IOC_MAGIC, 0x83, int *),
449         KDBUS_CMD_MEMFD_SEAL_SET =      _IO(KDBUS_IOC_MAGIC, 0x84),
450 };
451 #endif