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