chiark / gitweb /
dbus: fill in kdbus sender+destination from kdbus data
[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 #define KDBUS_SRC_ID_KERNEL             (0)
52 #define KDBUS_DST_ID_WELL_KNOWN_NAME    (0)
53 #define KDBUS_MATCH_SRC_ID_ANY          (~0ULL)
54 #define KDBUS_DST_ID_BROADCAST          (~0ULL)
55
56 /* Message Data Types */
57 enum {
58         /* Filled in by userspace */
59         KDBUS_MSG_NULL,                 /* empty record */
60         KDBUS_MSG_PAYLOAD,              /* .data */
61         KDBUS_MSG_PAYLOAD_VEC,          /* .data_vec, converted into _PAYLOAD at delivery */
62         KDBUS_MSG_MMAP,                 /* .data_vec */
63         KDBUS_MSG_MMAP_DONATE,          /* .data_vec, unmap the memory from the sender */
64         KDBUS_MSG_UNIX_FDS,             /* .data_fds of file descriptors */
65         KDBUS_MSG_BLOOM,                /* for broadcasts, carries bloom filter blob */
66         KDBUS_MSG_DST_NAME,             /* destination's well-known name */
67
68         /* Filled in by kernelspace */
69         KDBUS_MSG_SRC_NAMES     = 0x200,/* NUL separated string list with well-known names of source */
70         KDBUS_MSG_TIMESTAMP,            /* .ts_ns of CLOCK_MONOTONIC */
71         KDBUS_MSG_SRC_CREDS,            /* .creds */
72         KDBUS_MSG_SRC_COMM,             /* optional */
73         KDBUS_MSG_SRC_EXE,              /* optional */
74         KDBUS_MSG_SRC_CMDLINE,          /* optional */
75         KDBUS_MSG_SRC_CGROUP,           /* optional, specified which one */
76         KDBUS_MSG_SRC_CAPS,             /* caps data blob */
77         KDBUS_MSG_SRC_SECLABEL,         /* NUL terminated string */
78         KDBUS_MSG_SRC_AUDIT,            /* array of two uint64_t of audit loginuid + sessiond */
79
80         /* Special messages from kernel, consisting of one and only one of these data blocks */
81         KDBUS_MSG_NAME_ADD      = 0x400,/* .name_change */
82         KDBUS_MSG_NAME_REMOVE,          /* .name_change */
83         KDBUS_MSG_NAME_CHANGE,          /* .name_change */
84         KDBUS_MSG_ID_ADD,               /* .id_change */
85         KDBUS_MSG_ID_REMOVE,            /* .id_change */
86         KDBUS_MSG_ID_CHANGE,            /* .id_change */
87         KDBUS_MSG_REPLY_TIMEOUT,        /* empty, but .reply_cookie in .kdbus_msg is filled in */
88         KDBUS_MSG_REPLY_DEAD,           /* dito */
89 };
90
91 struct kdbus_vec {
92         __u64 address;
93         __u64 size;
94 };
95
96 /**
97  * struct  kdbus_msg_data - chain of data blocks
98  *
99  * size: overall data record size
100  * type: kdbus_msg_data_type of data
101  */
102 struct kdbus_msg_data {
103         __u64 size;
104         __u64 type;
105         union {
106                 /* inline data */
107                 __u8 data[0];
108                 char str[0];
109                 __u32 data_u32[0];
110                 __u64 data_u64[0];
111
112                 /* data vector */
113                 struct kdbus_vec vec;
114
115                 /* specific fields */
116                 int fds[0];                             /* int array of file descriptors */
117                 __u64 ts_ns;                            /* timestamp in nanoseconds */
118                 struct kdbus_creds creds;
119                 struct kdbus_manager_msg_name_change name_change;
120                 struct kdbus_manager_msg_id_change id_change;
121         };
122 };
123
124 enum {
125         KDBUS_MSG_FLAGS_EXPECT_REPLY    = 1,
126         KDBUS_MSG_FLAGS_NO_AUTO_START   = 2, /* possibly? */
127 };
128
129 enum {
130         KDBUS_PAYLOAD_NONE      = 0,
131         KDBUS_PAYLOAD_DBUS1     = 0x4442757356657231ULL, /* 'DBusVer1' */
132         KDBUS_PAYLOAD_GVARIANT  = 0x4756617269616e74ULL, /* 'GVariant' */
133 };
134
135 /**
136  * struct kdbus_msg
137  *
138  * set by userspace:
139  * dst_id: destination id
140  * flags: KDBUS_MSG_FLAGS_*
141  * data_size: overall message size
142  * data: data records
143  *
144  * set by kernel:
145  * src_id: who sent the message
146  */
147 struct kdbus_msg {
148         __u64 size;
149         __u64 flags;
150         __u64 dst_id;           /* connection, 0 == name in data, ~0 broadcast */
151         __u64 src_id;           /* connection, 0 == kernel */
152         __u64 payload_type;     /* 'DBusVer1', 'GVariant', ... */
153         __u64 cookie;           /* userspace-supplied cookie */
154         union {
155                 __u64 cookie_reply;     /* cookie we reply to */
156                 __u64 timeout_ns;       /* timespan to wait for reply */
157         };
158         struct kdbus_msg_data data[0];
159 };
160
161 enum {
162         KDBUS_POLICY_NAME,
163         KDBUS_POLICY_ACCESS,
164 };
165
166 enum {
167         KDBUS_POLICY_USER,
168         KDBUS_POLICY_GROUP,
169         KDBUS_POLICY_WORLD,
170 };
171
172 enum {
173         KDBUS_POLICY_RECV       = 4,
174         KDBUS_POLICY_SEND       = 2,
175         KDBUS_POLICY_OWN        = 1,
176 };
177
178 struct kdbus_policy {
179         __u64 size;
180         __u64 type; /* NAME or ACCESS */
181         union {
182                 char name[0];
183                 struct {
184                         __u32 type;     /* USER, GROUP, WORLD */
185                         __u32 bits;     /* RECV, SEND, OWN */
186                         __u64 id;       /* uid, gid, 0 */
187                 } access;
188         };
189 };
190
191 struct kdbus_cmd_policy {
192         __u64 size;
193         __u8 buffer[0]; /* a series of KDBUS_POLICY_NAME plus one or more KDBUS_POLICY_ACCESS each. */
194 };
195
196 enum {
197         KDBUS_CMD_HELLO_STARTER         =  1,
198         KDBUS_CMD_HELLO_ACCEPT_FD       =  2,
199         KDBUS_CMD_HELLO_ACCEPT_MMAP     =  4,
200 };
201
202 enum {
203         KDBUS_CMD_FNAME_ACCESS_GROUP    =  1,
204         KDBUS_CMD_FNAME_ACCESS_WORLD    =  2,
205         KDBUS_CMD_FNAME_POLICY_OPEN     =  4,
206 };
207
208 struct kdbus_cmd_hello {
209         /* userspace → kernel, kernel → userspace */
210         __u64 kernel_flags;     /* userspace specifies its
211                                  * capabilities and more, kernel
212                                  * returns its capabilites and
213                                  * more. Kernel might refuse client's
214                                  * capabilities by returning an error
215                                  * from KDBUS_CMD_HELLO */
216
217         /* userspace → kernel */
218         __u64 pid;              /* To allow translator services which
219                                  * connect to the bus on behalf of
220                                  * somebody else, allow specifiying
221                                  * the PID of the client to connect on
222                                  * behalf on. Normal clients should
223                                  * pass this as 0 (i.e. to do things
224                                  * under their own PID). Priviliged
225                                  * clients can pass != 0, to operate
226                                  * on behalf of somebody else. */
227
228         /* kernel → userspace */
229         __u64 bus_flags;        /* this is .flags copied verbatim from
230                                  * from original KDBUS_CMD_BUS_MAKE
231                                  * ioctl. It's intended to be useful
232                                  * to do negotiation of features of
233                                  * the payload that is transfreted. */
234         __u64 id;               /* peer id */
235 };
236
237 struct kdbus_cmd_fname {
238         __u64 size;
239         __u64 kernel_flags;     /* userspace → kernel, kernel → userspace
240                                  * When creating a bus/ns/ep feature
241                                  * kernel negotiation done the same
242                                  * way as for KDBUS_CMD_BUS_MAKE. */
243         __u64 user_flags;       /* userspace → kernel
244                                  * When a bus is created this value is
245                                  * copied verbatim into the bus
246                                  * structure and returned from
247                                  * KDBUS_CMD_HELLO, later */
248         char name[0];
249 };
250
251 enum {
252         /* userspace → kernel */
253         KDBUS_CMD_NAME_REPLACE_EXISTING         =  1,
254         KDBUS_CMD_NAME_QUEUE                    =  2,
255         KDBUS_CMD_NAME_ALLOW_REPLACEMENT        =  4,
256         KDBUS_CMD_NAME_STEAL_MESSAGES           =  8,
257
258         /* kernel → userspace */
259         KDBUS_CMD_NAME_IN_QUEUE = 0x200,
260 };
261
262 struct kdbus_cmd_name {
263         __u64 size;
264         __u64 flags;
265         __u64 id;               /* We allow registration/deregestration of names of other peers */
266         char name[0];
267 };
268
269 struct kdbus_cmd_names {
270         __u64 size;
271         struct kdbus_cmd_name names[0];
272 };
273
274 enum {
275         KDBUS_CMD_NAME_INFO_ITEM_NAME,
276         KDBUS_CMD_NAME_INFO_ITEM_SECLABEL,
277         KDBUS_CMD_NAME_INFO_ITEM_AUDIT,
278 };
279
280 struct kdbus_cmd_name_info_item {
281         __u64 size;
282         __u64 type;
283         __u8 data[0];
284 };
285
286 struct kdbus_cmd_name_info {
287         __u64 size;                     /* overall size of info */
288         __u64 flags;
289         __u64 id;                       /* either ID, or 0 and _ITEM_NAME follows */
290         struct kdbus_creds creds;
291         struct kdbus_cmd_name_info_item item[0]; /* list of item records */
292 };
293
294 enum {
295         KDBUS_CMD_MATCH_BLOOM,          /* Matches a mask blob against KDBUS_MSG_BLOOM */
296         KDBUS_CMD_MATCH_SRC_NAME,       /* Matches a name string against KDBUS_MSG_SRC_NAMES */
297         KDBUS_CMD_MATCH_NAME_ADD,       /* Matches a name string against KDBUS_MSG_NAME_ADD */
298         KDBUS_CMD_MATCH_NAME_REMOVE,    /* Matches a name string against KDBUS_MSG_NAME_REMOVE */
299         KDBUS_CMD_MATCH_NAME_CHANGE,    /* Matches a name string against KDBUS_MSG_NAME_CHANGE */
300         KDBUS_CMD_MATCH_ID_ADD,         /* Matches an ID against KDBUS_MSG_ID_ADD */
301         KDBUS_CMD_MATCH_ID_REMOVE,      /* Matches an ID against KDBUS_MSG_ID_REMOVE */
302         KDBUS_CMD_MATCH_ID_CHANGE,      /* Matches an ID against KDBUS_MSG_ID_CHANGE */
303 };
304
305 struct kdbus_cmd_match_item {
306         __u64 size;
307         __u64 type;
308         __u8 data[0];
309 };
310
311 struct kdbus_cmd_match {
312         __u64 size;
313         __u64 id;       /* We allow registration/deregestration of matches for other peers */
314         __u64 cookie;   /* userspace supplied cookie; when removing; kernel deletes everything with same cookie */
315         __u64 src_id;   /* ~0: any. other: exact unique match */
316         struct kdbus_cmd_match_item items[0];
317 };
318
319 struct kdbus_cmd_monitor {
320         __u64 id;               /* We allow setting the monitor flag of other peers */
321         int enabled;            /* A boolean to enable/disable monitoring */
322 };
323
324 /* FD states:
325  * control nodes: unset
326  *   bus owner  (via KDBUS_CMD_BUS_MAKE)
327  *   ns owner   (via KDBUS_CMD_NS_MAKE)
328  *
329  * ep nodes: unset
330  *   connected  (via KDBUS_CMD_HELLO)
331  *   starter    (via KDBUS_CMD_HELLO with KDBUS_CMD_HELLO_STARTER)
332  *   ep owner   (via KDBUS_CMD_EP_MAKE)
333  */
334 enum kdbus_cmd {
335         /* kdbus control node commands: require unset state */
336         KDBUS_CMD_BUS_MAKE =            _IOWR(KDBUS_IOC_MAGIC, 0x00, struct kdbus_cmd_fname),
337         KDBUS_CMD_NS_MAKE =             _IOWR(KDBUS_IOC_MAGIC, 0x10, struct kdbus_cmd_fname),
338
339         /* kdbus control node commands: require bus owner state */
340         KDBUS_CMD_BUS_POLICY_SET =      _IOWR(KDBUS_IOC_MAGIC, 0x20, struct kdbus_cmd_policy),
341
342         /* kdbus ep node commands: require unset state */
343         KDBUS_CMD_EP_MAKE =             _IOWR(KDBUS_IOC_MAGIC, 0x30, struct kdbus_cmd_fname),
344         KDBUS_CMD_HELLO =               _IOWR(KDBUS_IOC_MAGIC, 0x31, struct kdbus_cmd_hello),
345
346         /* kdbus ep node commands: require connected state */
347         KDBUS_CMD_MSG_SEND =            _IOWR(KDBUS_IOC_MAGIC, 0x40, struct kdbus_msg),
348         KDBUS_CMD_MSG_RECV =            _IOWR(KDBUS_IOC_MAGIC, 0x41, struct kdbus_msg),
349
350         KDBUS_CMD_NAME_ACQUIRE =        _IOWR(KDBUS_IOC_MAGIC, 0x50, struct kdbus_cmd_name),
351         KDBUS_CMD_NAME_RELEASE =        _IOWR(KDBUS_IOC_MAGIC, 0x51, struct kdbus_cmd_name),
352         KDBUS_CMD_NAME_LIST =           _IOWR(KDBUS_IOC_MAGIC, 0x52, struct kdbus_cmd_names),
353         KDBUS_CMD_NAME_QUERY =          _IOWR(KDBUS_IOC_MAGIC, 0x53, struct kdbus_cmd_name_info),
354
355         KDBUS_CMD_MATCH_ADD =           _IOWR(KDBUS_IOC_MAGIC, 0x60, struct kdbus_cmd_match),
356         KDBUS_CMD_MATCH_REMOVE =        _IOWR(KDBUS_IOC_MAGIC, 0x61, struct kdbus_cmd_match),
357         KDBUS_CMD_MONITOR =             _IOWR(KDBUS_IOC_MAGIC, 0x62, struct kdbus_cmd_monitor),
358
359         /* kdbus ep node commands: require ep owner state */
360         KDBUS_CMD_EP_POLICY_SET =       _IOWR(KDBUS_IOC_MAGIC, 0x70, struct kdbus_cmd_policy),
361 };
362 #endif