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>
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.
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <linux/types.h>
23 /* todo #define KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE SZ_8M - maximum size of message header and items
24 * taken from internal.h of kdbus
25 * finally it should be placed higher - e.g. kdbus.h of kdbus kernel module
27 #define KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE 0x00800000 /* maximum size of message header and items */
29 /*todo must be te same as used to create bus in struct kdbus_bus.bloom_size
30 * maybe it should be placed in one common header file*/
31 #define KDBUS_BLOOM_SIZE_BYTES 64
34 #define KDBUS_IOC_MAGIC 0x95
35 #define KDBUS_SRC_ID_KERNEL (0)
36 #define KDBUS_DST_ID_WELL_KNOWN_NAME (0)
37 #define KDBUS_MATCH_SRC_ID_ANY (~0ULL)
38 #define KDBUS_DST_ID_BROADCAST (~0ULL)
40 /* Common first elements in a structure which are used to iterate over
41 * a list of elements. */
42 #define KDBUS_PART_HEADER \
48 /* Message sent from kernel to userspace, when the owner or starter of
49 * a well-known name changes */
50 struct kdbus_manager_msg_name_change {
53 __u64 flags; /* 0 or (possibly?) KDBUS_NAME_IN_QUEUE */
57 struct kdbus_manager_msg_id_change {
59 __u64 flags; /* The kernel flags field from KDBUS_HELLO */
68 /* The starttime of the process PID. This is useful to detect
69 PID overruns from the client side. i.e. if you use the PID to
70 look something up in /proc/$PID/ you can afterwards check the
71 starttime field of it to ensure you didn't run into a PID
81 struct kdbus_timestamp {
100 /* Message Item Types */
104 /* Filled in by userspace */
105 KDBUS_MSG_PAYLOAD_VEC, /* .data_vec, reference to memory area */
106 KDBUS_MSG_PAYLOAD_OFF, /* .data_vec, reference to memory area */
107 KDBUS_MSG_PAYLOAD_MEMFD, /* file descriptor of a special data file */
108 KDBUS_MSG_FDS, /* .data_fds of file descriptors */
109 KDBUS_MSG_BLOOM, /* for broadcasts, carries bloom filter blob in .data */
110 KDBUS_MSG_DST_NAME, /* destination's well-known name, in .str */
111 KDBUS_MSG_PRIORITY, /* queue priority for message */
113 /* Filled in by kernelspace */
114 KDBUS_MSG_SRC_NAMES = 0x400,/* NUL separated string list with well-known names of source */
115 KDBUS_MSG_TIMESTAMP, /* .timestamp */
116 KDBUS_MSG_SRC_CREDS, /* .creds */
117 KDBUS_MSG_SRC_PID_COMM, /* optional, in .str */
118 KDBUS_MSG_SRC_TID_COMM, /* optional, in .str */
119 KDBUS_MSG_SRC_EXE, /* optional, in .str */
120 KDBUS_MSG_SRC_CMDLINE, /* optional, in .str (a chain of NUL str) */
121 KDBUS_MSG_SRC_CGROUP, /* optional, in .str */
122 KDBUS_MSG_SRC_CAPS, /* caps data blob, in .data */
123 KDBUS_MSG_SRC_SECLABEL, /* NUL terminated string, in .str */
124 KDBUS_MSG_SRC_AUDIT, /* .audit */
126 /* Special messages from kernel, consisting of one and only one of these data blocks */
127 KDBUS_MSG_NAME_ADD = 0x800,/* .name_change */
128 KDBUS_MSG_NAME_REMOVE, /* .name_change */
129 KDBUS_MSG_NAME_CHANGE, /* .name_change */
130 KDBUS_MSG_ID_ADD, /* .id_change */
131 KDBUS_MSG_ID_REMOVE, /* .id_change */
132 KDBUS_MSG_REPLY_TIMEOUT, /* empty, but .reply_cookie in .kdbus_msg is filled in */
133 KDBUS_MSG_REPLY_DEAD, /* dito */
137 * struct kdbus_item - chain of data blocks
139 * size: overall data record size
140 * type: kdbus_item type of data
155 struct kdbus_vec vec;
157 /* process credentials and properties*/
158 struct kdbus_creds creds;
159 struct kdbus_audit audit;
160 struct kdbus_timestamp timestamp;
162 /* specific fields */
163 struct kdbus_memfd memfd;
165 struct kdbus_manager_msg_name_change name_change;
166 struct kdbus_manager_msg_id_change id_change;
171 KDBUS_MSG_FLAGS_EXPECT_REPLY = 1 << 0,
172 KDBUS_MSG_FLAGS_NO_AUTO_START = 1 << 1,
176 KDBUS_PAYLOAD_KERNEL,
177 KDBUS_PAYLOAD_DBUS1 = 0x4442757356657231ULL, /* 'DBusVer1' */
178 KDBUS_PAYLOAD_GVARIANT = 0x4756617269616e74ULL, /* 'GVariant' */
185 * dst_id: destination id
186 * flags: KDBUS_MSG_FLAGS_*
187 * items: data records
190 * src_id: who sent the message
195 __u64 dst_id; /* connection, 0 == name in data, ~0 broadcast */
196 __u64 src_id; /* connection, 0 == kernel */
197 __u64 payload_type; /* 'DBusVer1', 'GVariant', ... */
198 __u64 cookie; /* userspace-supplied cookie */
200 __u64 cookie_reply; /* cookie we reply to */
201 __u64 timeout_ns; /* timespan to wait for reply */
203 struct kdbus_item items[0];
213 _KDBUS_POLICY_ACCESS_NULL,
214 KDBUS_POLICY_ACCESS_USER,
215 KDBUS_POLICY_ACCESS_GROUP,
216 KDBUS_POLICY_ACCESS_WORLD,
220 KDBUS_POLICY_RECV = 1 << 2,
221 KDBUS_POLICY_SEND = 1 << 1,
222 KDBUS_POLICY_OWN = 1 << 0,
225 struct kdbus_policy_access {
226 __u64 type; /* USER, GROUP, WORLD */
227 __u64 bits; /* RECV, SEND, OWN */
228 __u64 id; /* uid, gid, 0 */
231 //FIXME: convert access to access[]
232 struct kdbus_policy {
236 struct kdbus_policy_access access;
240 /* A series of KDBUS_POLICY_NAME, plus one or more KDBUS_POLICY_ACCESS */
241 struct kdbus_cmd_policy {
243 struct kdbus_policy policies[0];
246 /* Flags for struct kdbus_cmd_hello */
248 KDBUS_HELLO_STARTER = 1 << 0,
249 KDBUS_HELLO_ACCEPT_FD = 1 << 1,
251 /* The following have an effect on directed messages only --
252 * not for broadcasts */
253 KDBUS_HELLO_ATTACH_COMM = 1 << 10,
254 KDBUS_HELLO_ATTACH_EXE = 1 << 11,
255 KDBUS_HELLO_ATTACH_CMDLINE = 1 << 12,
256 KDBUS_HELLO_ATTACH_CGROUP = 1 << 13,
257 KDBUS_HELLO_ATTACH_CAPS = 1 << 14,
258 KDBUS_HELLO_ATTACH_SECLABEL = 1 << 15,
259 KDBUS_HELLO_ATTACH_AUDIT = 1 << 16,
262 struct kdbus_cmd_hello {
265 /* userspace → kernel, kernel → userspace */
266 __u64 conn_flags; /* userspace specifies its
267 * capabilities and more, kernel
268 * returns its capabilites and
269 * more. Kernel might refuse client's
270 * capabilities by returning an error
271 * from KDBUS_HELLO */
273 /* kernel → userspace */
274 __u64 bus_flags; /* this is .flags copied verbatim from
275 * from original KDBUS_CMD_BUS_MAKE
276 * ioctl. It's intended to be useful
277 * to do negotiation of features of
278 * the payload that is transfreted. */
279 __u64 id; /* id assigned to this connection */
280 __u64 bloom_size; /* The bloom filter size chosen by the
282 __u64 pool_size; /* maximum size of pool buffer */
283 struct kdbus_item items[0];
286 /* Flags for kdbus_cmd_{bus,ep,ns}_make */
288 KDBUS_MAKE_ACCESS_GROUP = 1 << 0,
289 KDBUS_MAKE_ACCESS_WORLD = 1 << 1,
290 KDBUS_MAKE_POLICY_OPEN = 1 << 2,
293 /* Items to append to kdbus_cmd_{bus,ep,ns}_make */
297 KDBUS_MAKE_CGROUP, /* the cgroup hierarchy ID for which to attach
298 * cgroup membership paths to messages.
299 * FIXME: remove, use *the* hierarchy */
300 KDBUS_MAKE_CRED, /* allow translator services which connect
301 * to the bus on behalf of somebody else,
302 * allow specifiying the credentials of the
303 * client to connect on behalf on. Needs
307 struct kdbus_cmd_bus_make {
309 __u64 flags; /* userspace → kernel, kernel → userspace
310 * When creating a bus feature
311 * kernel negotiation. */
312 __u64 bus_flags; /* userspace → kernel
313 * When a bus is created this value is
314 * copied verbatim into the bus
315 * structure and returned from
316 * KDBUS_CMD_HELLO, later */
317 __u64 bloom_size; /* size of the bloom filter for this bus */
318 struct kdbus_item items[0];
321 struct kdbus_cmd_ep_make {
323 __u64 flags; /* userspace → kernel, kernel → userspace
324 * When creating an entry point
325 * feature kernel negotiation done the
327 * KDBUS_CMD_BUS_MAKE. Unused for
329 struct kdbus_item items[0];
332 struct kdbus_cmd_ns_make {
334 __u64 flags; /* userspace → kernel, kernel → userspace
335 * When creating an entry point
336 * feature kernel negotiation done the
338 * KDBUS_CMD_BUS_MAKE. Unused for
340 struct kdbus_item items[0];
344 /* userspace → kernel */
345 KDBUS_NAME_REPLACE_EXISTING = 1 << 0,
346 KDBUS_NAME_QUEUE = 1 << 1,
347 KDBUS_NAME_ALLOW_REPLACEMENT = 1 << 2,
349 /* kernel → userspace */
350 KDBUS_NAME_IN_QUEUE = 1 << 16,
353 /* We allow (de)regestration of names of other peers */
354 struct kdbus_cmd_name {
362 struct kdbus_cmd_names {
364 struct kdbus_cmd_name names[0];
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 */
374 struct kdbus_cmd_name_info {
375 __u64 size; /* overall size of info */
377 __u64 id; /* either ID, or 0 and _ITEM_NAME follows */
378 struct kdbus_creds creds;
379 struct kdbus_item items[0]; /* list of item records */
384 KDBUS_MATCH_BLOOM, /* Matches a mask blob against KDBUS_MSG_BLOOM */
385 KDBUS_MATCH_SRC_NAME, /* Matches a name string against KDBUS_MSG_SRC_NAMES */
386 KDBUS_MATCH_NAME_ADD, /* Matches a name string against KDBUS_MSG_NAME_ADD */
387 KDBUS_MATCH_NAME_REMOVE, /* Matches a name string against KDBUS_MSG_NAME_REMOVE */
388 KDBUS_MATCH_NAME_CHANGE, /* Matches a name string against KDBUS_MSG_NAME_CHANGE */
389 KDBUS_MATCH_ID_ADD, /* Matches an ID against KDBUS_MSG_ID_ADD */
390 KDBUS_MATCH_ID_REMOVE, /* Matches an ID against KDBUS_MSG_ID_REMOVE */
393 struct kdbus_cmd_match {
395 __u64 id; /* We allow registration/deregestration of matches for other peers */
396 __u64 cookie; /* userspace supplied cookie; when removing; kernel deletes everything with same cookie */
397 __u64 src_id; /* ~0: any. other: exact unique match */
398 struct kdbus_item items[0];
401 struct kdbus_cmd_monitor {
402 __u64 id; /* We allow setting the monitor flag of other peers */
403 unsigned int enable; /* A boolean to enable/disable monitoring */
408 * control nodes: unset
409 * bus owner (via KDBUS_CMD_BUS_MAKE)
410 * ns owner (via KDBUS_CMD_NS_MAKE)
413 * connected (via KDBUS_CMD_HELLO)
414 * starter (via KDBUS_CMD_HELLO with KDBUS_CMD_HELLO_STARTER)
415 * ep owner (via KDBUS_CMD_EP_MAKE)
418 /* kdbus control node commands: require unset state */
419 KDBUS_CMD_BUS_MAKE = _IOW(KDBUS_IOC_MAGIC, 0x00, struct kdbus_cmd_bus_make),
420 KDBUS_CMD_NS_MAKE = _IOR(KDBUS_IOC_MAGIC, 0x10, struct kdbus_cmd_ns_make),
422 /* kdbus ep node commands: require unset state */
423 KDBUS_CMD_EP_MAKE = _IOW(KDBUS_IOC_MAGIC, 0x20, struct kdbus_cmd_ep_make),
424 KDBUS_CMD_HELLO = _IOWR(KDBUS_IOC_MAGIC, 0x30, struct kdbus_cmd_hello),
426 /* kdbus ep node commands: require connected state */
427 KDBUS_CMD_MSG_SEND = _IOW(KDBUS_IOC_MAGIC, 0x40, struct kdbus_msg),
428 KDBUS_CMD_MSG_RECV = _IOR(KDBUS_IOC_MAGIC, 0x41, __u64 *),
429 KDBUS_CMD_MSG_RELEASE = _IOW(KDBUS_IOC_MAGIC, 0x42, __u64 *),
431 KDBUS_CMD_NAME_ACQUIRE = _IOWR(KDBUS_IOC_MAGIC, 0x50, struct kdbus_cmd_name),
432 KDBUS_CMD_NAME_RELEASE = _IOW(KDBUS_IOC_MAGIC, 0x51, struct kdbus_cmd_name),
433 KDBUS_CMD_NAME_LIST = _IOWR(KDBUS_IOC_MAGIC, 0x52, struct kdbus_cmd_names),
434 KDBUS_CMD_NAME_QUERY = _IOWR(KDBUS_IOC_MAGIC, 0x53, struct kdbus_cmd_name_info),
436 KDBUS_CMD_MATCH_ADD = _IOW(KDBUS_IOC_MAGIC, 0x60, struct kdbus_cmd_match),
437 KDBUS_CMD_MATCH_REMOVE = _IOW(KDBUS_IOC_MAGIC, 0x61, struct kdbus_cmd_match),
438 KDBUS_CMD_MONITOR = _IOW(KDBUS_IOC_MAGIC, 0x62, struct kdbus_cmd_monitor),
440 /* kdbus ep node commands: require ep owner state */
441 KDBUS_CMD_EP_POLICY_SET = _IOW(KDBUS_IOC_MAGIC, 0x70, struct kdbus_cmd_policy),
443 /* kdbus memfd commands: */
444 KDBUS_CMD_MEMFD_NEW = _IOR(KDBUS_IOC_MAGIC, 0x80, int *),
445 KDBUS_CMD_MEMFD_SIZE_GET = _IOR(KDBUS_IOC_MAGIC, 0x81, __u64 *),
446 KDBUS_CMD_MEMFD_SIZE_SET = _IOW(KDBUS_IOC_MAGIC, 0x82, __u64 *),
447 KDBUS_CMD_MEMFD_SEAL_GET = _IOR(KDBUS_IOC_MAGIC, 0x83, int *),
448 KDBUS_CMD_MEMFD_SEAL_SET = _IO(KDBUS_IOC_MAGIC, 0x84),