chiark / gitweb /
8c643feceb947b35b7a5b350236565f45039ec87
[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  *
6  * kdbus is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at
9  * your option) any later version.
10  */
11
12 #ifndef _KDBUS_H_
13 #define _KDBUS_H_
14
15 #ifndef __KERNEL__
16 #include <sys/ioctl.h>
17 #include <sys/types.h>
18 #include <linux/types.h>
19 #endif
20
21 #define KDBUS_IOC_MAGIC                 0x95
22
23 /* Message sent from kernel to userspace, when the owner or starter of
24  * a well-known name changes */
25 struct kdbus_manager_msg_name_change {
26         __u64 old_id;
27         __u64 new_id;
28         __u64 flags;            /* 0 or (possibly?) KDBUS_CMD_NAME_IN_QUEUE */
29         char name[0];
30 };
31
32 struct kdbus_manager_msg_id_change {
33         __u64 id;
34         __u64 flags; /* The kernel flags field from KDBUS_CMD_HELLO */
35 };
36
37 struct kdbus_creds {
38         __u64 uid;
39         __u64 gid;
40         __u64 pid;
41         __u64 tid;
42
43         /* The starttime of the process PID. This is useful to detect
44         PID overruns from the client side. i.e. if you use the PID to
45         look something up in /proc/$PID/ you can afterwards check the
46         starttime field of it to ensure you didn't run into a PID
47         ovretun. */
48         __u64 starttime;
49 };
50
51 struct kdbus_audit {
52         __u64 sessionid;
53         __u64 loginuid;
54 };
55
56 #define KDBUS_SRC_ID_KERNEL             (0)
57 #define KDBUS_DST_ID_WELL_KNOWN_NAME    (0)
58 #define KDBUS_MATCH_SRC_ID_ANY          (~0ULL)
59 #define KDBUS_DST_ID_BROADCAST          (~0ULL)
60
61 /* Message Data Types */
62 enum {
63         /* Filled in by userspace */
64         KDBUS_MSG_NULL,                 /* empty record */
65         KDBUS_MSG_PAYLOAD,              /* .data */
66         KDBUS_MSG_PAYLOAD_VEC,          /* .data_vec */
67         KDBUS_MSG_UNIX_FDS,             /* .data_fds of file descriptors */
68         KDBUS_MSG_BLOOM,                /* for broadcasts, carries bloom filter blob in .data */
69         KDBUS_MSG_DST_NAME,             /* destination's well-known name, in .str */
70
71         /* Filled in by kernelspace */
72         KDBUS_MSG_SRC_NAMES     = 0x200,/* NUL separated string list with well-known names of source */
73         KDBUS_MSG_TIMESTAMP,            /* .ts_ns of CLOCK_MONOTONIC */
74         KDBUS_MSG_SRC_CREDS,            /* .creds */
75         KDBUS_MSG_SRC_PID_COMM,         /* optional, in .str */
76         KDBUS_MSG_SRC_TID_COMM,         /* optional, in .str */
77         KDBUS_MSG_SRC_EXE,              /* optional, in .str */
78         KDBUS_MSG_SRC_CMDLINE,          /* optional, in .str (a chain of NUL str) */
79         KDBUS_MSG_SRC_CGROUP,           /* optional, in .str */
80         KDBUS_MSG_SRC_CAPS,             /* caps data blob, in .data */
81         KDBUS_MSG_SRC_SECLABEL,         /* NUL terminated string, in .str */
82         KDBUS_MSG_SRC_AUDIT,            /* .audit */
83
84         /* Special messages from kernel, consisting of one and only one of these data blocks */
85         KDBUS_MSG_NAME_ADD      = 0x400,/* .name_change */
86         KDBUS_MSG_NAME_REMOVE,          /* .name_change */
87         KDBUS_MSG_NAME_CHANGE,          /* .name_change */
88         KDBUS_MSG_ID_ADD,               /* .id_change */
89         KDBUS_MSG_ID_REMOVE,            /* .id_change */
90         KDBUS_MSG_ID_CHANGE,            /* .id_change */
91         KDBUS_MSG_REPLY_TIMEOUT,        /* empty, but .reply_cookie in .kdbus_msg is filled in */
92         KDBUS_MSG_REPLY_DEAD,           /* dito */
93 };
94
95 struct kdbus_vec {
96         __u64 address;
97         __u64 size;
98 };
99
100 /**
101  * struct  kdbus_msg_data - chain of data blocks
102  *
103  * size: overall data record size
104  * type: kdbus_msg_data_type of data
105  */
106 struct kdbus_msg_data {
107         __u64 size;
108         __u64 type;
109         union {
110                 /* inline data */
111                 __u8 data[0];
112                 char str[0];
113                 __u32 data_u32[0];
114                 __u64 data_u64[0];
115
116                 /* data vector */
117                 struct kdbus_vec vec;
118
119                 /* specific fields */
120                 int fds[0];                             /* int array of file descriptors */
121                 __u64 ts_ns;                            /* timestamp in nanoseconds */
122                 struct kdbus_creds creds;
123                 struct kdbus_audit audit;
124                 struct kdbus_manager_msg_name_change name_change;
125                 struct kdbus_manager_msg_id_change id_change;
126         };
127 };
128
129 enum {
130         KDBUS_MSG_FLAGS_EXPECT_REPLY    = 1,
131         KDBUS_MSG_FLAGS_NO_AUTO_START   = 2, /* possibly? */
132 };
133
134 enum {
135         KDBUS_PAYLOAD_NONE      = 0,
136         KDBUS_PAYLOAD_DBUS1     = 0x4442757356657231ULL, /* 'DBusVer1' */
137         KDBUS_PAYLOAD_GVARIANT  = 0x4756617269616e74ULL, /* 'GVariant' */
138 };
139
140 /**
141  * struct kdbus_msg
142  *
143  * set by userspace:
144  * dst_id: destination id
145  * flags: KDBUS_MSG_FLAGS_*
146  * data_size: overall message size
147  * data: data records
148  *
149  * set by kernel:
150  * src_id: who sent the message
151  */
152 struct kdbus_msg {
153         __u64 size;
154         __u64 flags;
155         __u64 dst_id;           /* connection, 0 == name in data, ~0 broadcast */
156         __u64 src_id;           /* connection, 0 == kernel */
157         __u64 payload_type;     /* 'DBusVer1', 'GVariant', ... */
158         __u64 cookie;           /* userspace-supplied cookie */
159         union {
160                 __u64 cookie_reply;     /* cookie we reply to */
161                 __u64 timeout_ns;       /* timespan to wait for reply */
162         };
163         struct kdbus_msg_data data[0];
164 };
165
166 enum {
167         KDBUS_POLICY_NAME,
168         KDBUS_POLICY_ACCESS,
169 };
170
171 enum {
172         KDBUS_POLICY_USER,
173         KDBUS_POLICY_GROUP,
174         KDBUS_POLICY_WORLD,
175 };
176
177 enum {
178         KDBUS_POLICY_RECV       = 4,
179         KDBUS_POLICY_SEND       = 2,
180         KDBUS_POLICY_OWN        = 1,
181 };
182
183 struct kdbus_policy {
184         __u64 size;
185         __u64 type; /* NAME or ACCESS */
186         union {
187                 char name[0];
188                 struct {
189                         __u32 type;     /* USER, GROUP, WORLD */
190                         __u32 bits;     /* RECV, SEND, OWN */
191                         __u64 id;       /* uid, gid, 0 */
192                 } access;
193         };
194 };
195
196 struct kdbus_cmd_policy {
197         __u64 size;
198         __u8 buffer[0]; /* a series of KDBUS_POLICY_NAME plus one or
199                          * more KDBUS_POLICY_ACCESS each. */
200 };
201
202 enum {
203         KDBUS_CMD_HELLO_STARTER         =  1 <<  0,
204         KDBUS_CMD_HELLO_ACCEPT_FD       =  1 <<  1,
205         KDBUS_CMD_HELLO_ACCEPT_MMAP     =  1 <<  2,
206
207         /* The following have an effect on directed messages only --
208          * not for broadcasts */
209         KDBUS_CMD_HELLO_ATTACH_COMM     =  1 << 10,
210         KDBUS_CMD_HELLO_ATTACH_EXE      =  1 << 11,
211         KDBUS_CMD_HELLO_ATTACH_CMDLINE  =  1 << 12,
212         KDBUS_CMD_HELLO_ATTACH_CGROUP   =  1 << 13,
213         KDBUS_CMD_HELLO_ATTACH_CAPS     =  1 << 14,
214         KDBUS_CMD_HELLO_ATTACH_SECLABEL =  1 << 15,
215         KDBUS_CMD_HELLO_ATTACH_AUDIT    =  1 << 16,
216 };
217
218 /* Flags for kdbus_cmd_bus_make, kdbus_cmd_ep_make and
219  * kdbus_cmd_ns_make */
220 enum {
221         KDBUS_ACCESS_GROUP      =  1,
222         KDBUS_ACCESS_WORLD      =  2,
223         KDBUS_POLICY_OPEN       =  4,
224 };
225
226 struct kdbus_cmd_hello {
227         /* userspace → kernel, kernel → userspace */
228         __u64 conn_flags;       /* userspace specifies its
229                                  * capabilities and more, kernel
230                                  * returns its capabilites and
231                                  * more. Kernel might refuse client's
232                                  * capabilities by returning an error
233                                  * from KDBUS_CMD_HELLO */
234
235         /* userspace → kernel */
236         __u64 pid;              /* To allow translator services which
237                                  * connect to the bus on behalf of
238                                  * somebody else, allow specifiying
239                                  * the PID of the client to connect on
240                                  * behalf on. Normal clients should
241                                  * pass this as 0 (i.e. to do things
242                                  * under their own PID). Priviliged
243                                  * clients can pass != 0, to operate
244                                  * on behalf of somebody else. */
245
246         /* kernel → userspace */
247         __u64 bus_flags;        /* this is .flags copied verbatim from
248                                  * from original KDBUS_CMD_BUS_MAKE
249                                  * ioctl. It's intended to be useful
250                                  * to do negotiation of features of
251                                  * the payload that is transfreted. */
252         __u64 id;               /* peer id */
253         __u64 bloom_size;       /* The bloom filter size chosen by the
254                                  * bus owner */
255 };
256
257 struct kdbus_cmd_bus_make {
258         __u64 size;
259         __u64 flags;            /* userspace → kernel, kernel → userspace
260                                  * When creating a bus feature
261                                  * kernel negotiation. */
262         __u64 bus_flags;        /* userspace → kernel
263                                  * When a bus is created this value is
264                                  * copied verbatim into the bus
265                                  * structure and returned from
266                                  * KDBUS_CMD_HELLO, later */
267         __u64 cgroup_id;        /* the cgroup hierarchy ID for which
268                                  * to attach cgroup membership paths
269                                  * to messages. 0 if no cgroup data
270                                  * shall be attached. */
271         __u64 bloom_size;       /* Size of the bloom filter for this bus. */
272         char name[0];
273 };
274
275 struct kdbus_cmd_ep_make {
276         __u64 size;
277         __u64 flags;            /* userspace → kernel, kernel → userspace
278                                  * When creating an entry point
279                                  * feature kernel negotiation done the
280                                  * same way as for
281                                  * KDBUS_CMD_BUS_MAKE. Unused for
282                                  * now. */
283         char name[0];
284 };
285
286 struct kdbus_cmd_ns_make {
287         __u64 size;
288         __u64 flags;            /* userspace → kernel, kernel → userspace
289                                  * When creating an entry point
290                                  * feature kernel negotiation done the
291                                  * same way as for
292                                  * KDBUS_CMD_BUS_MAKE. Unused for
293                                  * now. */
294         char name[0];
295 };
296
297 enum {
298         /* userspace → kernel */
299         KDBUS_CMD_NAME_REPLACE_EXISTING         =  1,
300         KDBUS_CMD_NAME_QUEUE                    =  2,
301         KDBUS_CMD_NAME_ALLOW_REPLACEMENT        =  4,
302
303         /* kernel → userspace */
304         KDBUS_CMD_NAME_IN_QUEUE = 0x200,
305 };
306
307 struct kdbus_cmd_name {
308         __u64 size;
309         __u64 name_flags;
310         __u64 id;               /* We allow registration/deregestration of names of other peers */
311         __u64 conn_flags;
312         char name[0];
313 };
314
315 struct kdbus_cmd_names {
316         __u64 size;
317         struct kdbus_cmd_name names[0];
318 };
319
320 enum {
321         KDBUS_CMD_NAME_INFO_ITEM_NAME,
322         KDBUS_CMD_NAME_INFO_ITEM_SECLABEL,
323         KDBUS_CMD_NAME_INFO_ITEM_AUDIT,
324 };
325
326 struct kdbus_cmd_name_info_item {
327         __u64 size;
328         __u64 type;
329         __u8 data[0];
330 };
331
332 struct kdbus_cmd_name_info {
333         __u64 size;                     /* overall size of info */
334         __u64 flags;
335         __u64 id;                       /* either ID, or 0 and _ITEM_NAME follows */
336         struct kdbus_creds creds;
337         struct kdbus_cmd_name_info_item item[0]; /* list of item records */
338 };
339
340 enum {
341         KDBUS_CMD_MATCH_BLOOM,          /* Matches a mask blob against KDBUS_MSG_BLOOM */
342         KDBUS_CMD_MATCH_SRC_NAME,       /* Matches a name string against KDBUS_MSG_SRC_NAMES */
343         KDBUS_CMD_MATCH_NAME_ADD,       /* Matches a name string against KDBUS_MSG_NAME_ADD */
344         KDBUS_CMD_MATCH_NAME_REMOVE,    /* Matches a name string against KDBUS_MSG_NAME_REMOVE */
345         KDBUS_CMD_MATCH_NAME_CHANGE,    /* Matches a name string against KDBUS_MSG_NAME_CHANGE */
346         KDBUS_CMD_MATCH_ID_ADD,         /* Matches an ID against KDBUS_MSG_ID_ADD */
347         KDBUS_CMD_MATCH_ID_REMOVE,      /* Matches an ID against KDBUS_MSG_ID_REMOVE */
348         KDBUS_CMD_MATCH_ID_CHANGE,      /* Matches an ID against KDBUS_MSG_ID_CHANGE */
349 };
350
351 struct kdbus_cmd_match_item {
352         __u64 size;
353         __u64 type;
354         __u8 data[0];
355 };
356
357 struct kdbus_cmd_match {
358         __u64 size;
359         __u64 id;       /* We allow registration/deregestration of matches for other peers */
360         __u64 cookie;   /* userspace supplied cookie; when removing; kernel deletes everything with same cookie */
361         __u64 src_id;   /* ~0: any. other: exact unique match */
362         struct kdbus_cmd_match_item items[0];
363 };
364
365 struct kdbus_cmd_monitor {
366         __u64 id;               /* We allow setting the monitor flag of other peers */
367         int enabled;            /* A boolean to enable/disable monitoring */
368 };
369
370 /* FD states:
371  * control nodes: unset
372  *   bus owner  (via KDBUS_CMD_BUS_MAKE)
373  *   ns owner   (via KDBUS_CMD_NS_MAKE)
374  *
375  * ep nodes: unset
376  *   connected  (via KDBUS_CMD_HELLO)
377  *   starter    (via KDBUS_CMD_HELLO with KDBUS_CMD_HELLO_STARTER)
378  *   ep owner   (via KDBUS_CMD_EP_MAKE)
379  */
380 enum kdbus_cmd {
381         /* kdbus control node commands: require unset state */
382         KDBUS_CMD_BUS_MAKE =            _IOWR(KDBUS_IOC_MAGIC, 0x00, struct kdbus_cmd_bus_make),
383         KDBUS_CMD_NS_MAKE =             _IOWR(KDBUS_IOC_MAGIC, 0x10, struct kdbus_cmd_ns_make),
384
385         /* kdbus control node commands: require bus owner state */
386         KDBUS_CMD_BUS_POLICY_SET =      _IOWR(KDBUS_IOC_MAGIC, 0x20, struct kdbus_cmd_policy),
387
388         /* kdbus ep node commands: require unset state */
389         KDBUS_CMD_EP_MAKE =             _IOWR(KDBUS_IOC_MAGIC, 0x30, struct kdbus_cmd_ep_make),
390         KDBUS_CMD_HELLO =               _IOWR(KDBUS_IOC_MAGIC, 0x31, struct kdbus_cmd_hello),
391
392         /* kdbus ep node commands: require connected state */
393         KDBUS_CMD_MSG_SEND =            _IOWR(KDBUS_IOC_MAGIC, 0x40, struct kdbus_msg),
394         KDBUS_CMD_MSG_RECV =            _IOWR(KDBUS_IOC_MAGIC, 0x41, struct kdbus_msg),
395
396         KDBUS_CMD_NAME_ACQUIRE =        _IOWR(KDBUS_IOC_MAGIC, 0x50, struct kdbus_cmd_name),
397         KDBUS_CMD_NAME_RELEASE =        _IOWR(KDBUS_IOC_MAGIC, 0x51, struct kdbus_cmd_name),
398         KDBUS_CMD_NAME_LIST =           _IOWR(KDBUS_IOC_MAGIC, 0x52, struct kdbus_cmd_names),
399         KDBUS_CMD_NAME_QUERY =          _IOWR(KDBUS_IOC_MAGIC, 0x53, struct kdbus_cmd_name_info),
400
401         KDBUS_CMD_MATCH_ADD =           _IOWR(KDBUS_IOC_MAGIC, 0x60, struct kdbus_cmd_match),
402         KDBUS_CMD_MATCH_REMOVE =        _IOWR(KDBUS_IOC_MAGIC, 0x61, struct kdbus_cmd_match),
403         KDBUS_CMD_MONITOR =             _IOWR(KDBUS_IOC_MAGIC, 0x62, struct kdbus_cmd_monitor),
404
405         /* kdbus ep node commands: require ep owner state */
406         KDBUS_CMD_EP_POLICY_SET =       _IOWR(KDBUS_IOC_MAGIC, 0x70, struct kdbus_cmd_policy),
407 };
408 #endif