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 #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)
29 /* Common first elements in a structure which are used to iterate over
30 * a list of elements. */
31 #define KDBUS_PART_HEADER \
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 {
42 __u64 flags; /* 0 or (possibly?) KDBUS_NAME_IN_QUEUE */
46 struct kdbus_notify_id_change {
48 __u64 flags; /* The kernel flags field from KDBUS_HELLO */
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
66 * struct kdbus_audit - audit information
67 * @sessionid The audit session ID
68 * @loginuid The audit login uid
76 * struct kdbus_timestamp
77 * @monotonic_ns: Monotonic timestamp, in nanoseconds
78 * @realtime_ns: Realtime timestamp, in nanoseconds
80 struct kdbus_timestamp {
86 * struct kdbus_vec - I/O vector for kdbus payload items
87 * @size: The size of the vector
88 * @address Memory address for memory addresses
89 * @offset Offset in the in-message payload memory
100 * struct kdbus_memfd - a kdbus memfd
101 * @size: The memfd's size
102 * @fd: The file descriptor number
103 * @__pad: Padding to make the struct aligned
111 /* Message Item Types */
115 /* Filled in by userspace */
116 _KDBUS_ITEM_USER_BASE = 1,
117 KDBUS_ITEM_PAYLOAD_VEC = 1, /* .data_vec, reference to memory area */
118 KDBUS_ITEM_PAYLOAD_OFF, /* .data_vec, reference to memory area */
119 KDBUS_ITEM_PAYLOAD_MEMFD, /* file descriptor of a special data file */
120 KDBUS_ITEM_FDS, /* .data_fds of file descriptors */
121 KDBUS_ITEM_BLOOM, /* for broadcasts, carries bloom filter blob in .data */
122 KDBUS_ITEM_DST_NAME, /* destination's well-known name, in .str */
123 KDBUS_ITEM_PRIORITY, /* queue priority for message */
125 /* Filled in by kernelspace */
126 _KDBUS_ITEM_ATTACH_BASE = 0x400,
127 KDBUS_ITEM_NAMES = 0x400,/* NUL separated string list with well-known names of source */
128 KDBUS_ITEM_STARTER_NAME, /* Only used in HELLO for starter connection */
129 KDBUS_ITEM_TIMESTAMP, /* .timestamp */
131 /* when appended to a message, the following items refer to the sender */
132 KDBUS_ITEM_CREDS, /* .creds */
133 KDBUS_ITEM_PID_COMM, /* optional, in .str */
134 KDBUS_ITEM_TID_COMM, /* optional, in .str */
135 KDBUS_ITEM_EXE, /* optional, in .str */
136 KDBUS_ITEM_CMDLINE, /* optional, in .str (a chain of NUL str) */
137 KDBUS_ITEM_CGROUP, /* optional, in .str */
138 KDBUS_ITEM_CAPS, /* caps data blob, in .data */
139 KDBUS_ITEM_SECLABEL, /* NUL terminated string, in .str */
140 KDBUS_ITEM_AUDIT, /* .audit */
142 /* Special messages from kernel, consisting of one and only one of these data blocks */
143 _KDBUS_ITEM_KERNEL_BASE = 0x800,
144 KDBUS_ITEM_NAME_ADD = 0x800,/* .name_change */
145 KDBUS_ITEM_NAME_REMOVE, /* .name_change */
146 KDBUS_ITEM_NAME_CHANGE, /* .name_change */
147 KDBUS_ITEM_ID_ADD, /* .id_change */
148 KDBUS_ITEM_ID_REMOVE, /* .id_change */
149 KDBUS_ITEM_REPLY_TIMEOUT, /* empty, but .reply_cookie in .kdbus_msg is filled in */
150 KDBUS_ITEM_REPLY_DEAD, /* dito */
154 * struct kdbus_item - chain of data blocks
155 * @size: overall data record size
156 * @type: kdbus_item type of data
171 struct kdbus_vec vec;
173 /* process credentials and properties*/
174 struct kdbus_creds creds;
175 struct kdbus_audit audit;
176 struct kdbus_timestamp timestamp;
178 /* specific fields */
179 struct kdbus_memfd memfd;
181 struct kdbus_notify_name_change name_change;
182 struct kdbus_notify_id_change id_change;
187 KDBUS_MSG_FLAGS_EXPECT_REPLY = 1 << 0,
188 KDBUS_MSG_FLAGS_NO_AUTO_START = 1 << 1,
192 KDBUS_PAYLOAD_KERNEL,
193 KDBUS_PAYLOAD_DBUS1 = 0x4442757356657231ULL, /* 'DBusVer1' */
194 KDBUS_PAYLOAD_GVARIANT = 0x4756617269616e74ULL, /* 'GVariant' */
198 * struct kdbus_msg - the representation of a kdbus message
199 * @size: Total size of the message
200 * @flags: Message flags (KDBUS_MSG_FLAGS_*)
201 * @dst_id: 64-bit ID of the destination connection
202 * @src_id: 64-bit ID of the source connection
203 * @payload_type: Payload type (KDBUS_PAYLOAD_*)
204 * @cookie: Userspace-supplied cookie
205 * @cookie_reply: For kernel-generated messages, this is the cookie
206 * the message is a reply to
207 * @timeout_ns: For non-kernel-generated messages, this denotes the
208 * message timeout in nanoseconds
209 * @items: A list of kdbus_items containing the message payload
222 struct kdbus_item items[0];
232 _KDBUS_POLICY_ACCESS_NULL,
233 KDBUS_POLICY_ACCESS_USER,
234 KDBUS_POLICY_ACCESS_GROUP,
235 KDBUS_POLICY_ACCESS_WORLD,
239 KDBUS_POLICY_RECV = 1 << 2,
240 KDBUS_POLICY_SEND = 1 << 1,
241 KDBUS_POLICY_OWN = 1 << 0,
245 * struct kdbus_policy_access - policy access item
246 * @type: One of KDBUS_POLICY_ACCESS_* types
247 * @bits: Access to grant. One of KDBUS_POLICY_*
248 * @id: For KDBUS_POLICY_ACCESS_USER, the uid
249 * For KDBUS_POLICY_ACCESS_GROUP, the gid
251 struct kdbus_policy_access {
252 __u64 type; /* USER, GROUP, WORLD */
253 __u64 bits; /* RECV, SEND, OWN */
254 __u64 id; /* uid, gid, 0 */
258 * struct kdbus_policy - a policy to upload
259 * @size: The total size of the structure
260 * @type: KDBUS_POLICY_NAME or KDBUS_POLICY_ACCESS
261 * @name: The well-known name to grant access to,
262 * if @type is KDBUS_POLICY_NAME
263 * @access: The policy access details,
264 * if @type is KDBUS_POLICY_ACCESS
266 struct kdbus_policy {
270 struct kdbus_policy_access access;
275 * struct kdbus_cmd_policy - a series of policies to upload
276 * @size: The total size of the structure
277 * @policies: The policies to upload
279 * A KDBUS_POLICY_NAME must always preceed a KDBUS_POLICY_ACCESS entry.
280 * A new KDBUS_POLICY_NAME can be added after KDBUS_POLICY_ACCESS for
281 * chaining multiple policies together.
283 struct kdbus_cmd_policy {
285 struct kdbus_policy policies[0];
288 /* Flags for struct kdbus_cmd_hello */
290 KDBUS_HELLO_STARTER = 1 << 0,
291 KDBUS_HELLO_ACCEPT_FD = 1 << 1,
294 /* Flags for message attachments */
296 KDBUS_ATTACH_TIMESTAMP = 1 << 0,
297 KDBUS_ATTACH_CREDS = 1 << 1,
298 KDBUS_ATTACH_NAMES = 1 << 2,
299 KDBUS_ATTACH_COMM = 1 << 3,
300 KDBUS_ATTACH_EXE = 1 << 4,
301 KDBUS_ATTACH_CMDLINE = 1 << 5,
302 KDBUS_ATTACH_CGROUP = 1 << 6,
303 KDBUS_ATTACH_CAPS = 1 << 7,
304 KDBUS_ATTACH_SECLABEL = 1 << 8,
305 KDBUS_ATTACH_AUDIT = 1 << 9,
309 * struct kdbus_cmd_hello - struct to say hello to kdbus
310 * @size: The total size of the structure
311 * @conn_flags: Connection flags (KDBUS_HELLO_*). The kernel will
312 * return its capabilities in that field.
313 * @attach_flags: Mask of metdata to attach to each message sent
315 * @bus_flags: The flags field copied verbatim from the original
316 * KDBUS_CMD_BUS_MAKE ioctl. It's intended to be useful
317 * to do negotiation of features of the payload that is
318 * transferred (kernel → userspace)
319 * @id: The id of this connection (kernel → userspace)
320 * @bloom_size: The bloom filter size chosen by the owner
321 * (kernel → userspace)
322 * @pool_size: Maximum size of the pool buffer (kernel → userspace)
323 * @id128: Unique 128-bit ID of the bus (kernel → userspace)
324 * @items; A list of items
326 * This struct is used with the KDBUS_CMD_HELLO ioctl. See the ioctl
327 * documentation for more information.
329 struct kdbus_cmd_hello {
332 /* userspace → kernel, kernel → userspace */
336 /* kernel → userspace */
343 struct kdbus_item items[0];
346 /* Flags for KDBUS_CMD_{BUS,EP,NS}_MAKE */
348 KDBUS_MAKE_ACCESS_GROUP = 1 << 0,
349 KDBUS_MAKE_ACCESS_WORLD = 1 << 1,
350 KDBUS_MAKE_POLICY_OPEN = 1 << 2,
353 /* Items to append to KDBUS_CMD_{BUS,EP,NS}_MAKE */
357 KDBUS_MAKE_CRED, /* allow translator services which connect
358 * to the bus on behalf of somebody else,
359 * allow specifying the credentials of the
360 * client to connect on behalf on. Needs
365 * struct kdbus_cmd_bus_make - struct to make a bus
366 * @size: The total size of the struct
369 * @bloom_filter: Size of the bloom filter for this bus
370 * @items: Items describing details such as the name of the bus
372 * This structure is used with the KDBUS_CMD_BUS_MAKE ioctl. Refer to the
373 * documentation for more information.
375 struct kdbus_cmd_bus_make {
380 struct kdbus_item items[0];
384 * struct kdbus_cmd_ep_make - struct to make an endpoint
385 * @size: The total size of the struct
386 * @flags: Unused for now
387 * @items: Items describing details such as the
388 * name of the endpoint
390 * This structure is used with the KDBUS_CMD_EP_MAKE ioctl. Refer to the
391 * documentation for more information.
393 struct kdbus_cmd_ep_make {
396 struct kdbus_item items[0];
400 * struct kdbus_cmd_ns_make - struct to make a namespace
401 * @size: The total size of the struct
402 * @flags: Unused for now
403 * @items: Items describing details such as the
404 * name of the namespace
406 * This structure is used with the KDBUS_CMD_NS_MAKE ioctl. Refer to the
407 * documentation for more information.
409 struct kdbus_cmd_ns_make {
412 struct kdbus_item items[0];
416 /* userspace → kernel */
417 KDBUS_NAME_REPLACE_EXISTING = 1 << 0,
418 KDBUS_NAME_QUEUE = 1 << 1,
419 KDBUS_NAME_ALLOW_REPLACEMENT = 1 << 2,
421 /* kernel → userspace */
422 KDBUS_NAME_IN_QUEUE = 1 << 16,
426 * struct kdbus_cmd_name - struct to describe a well-known name
427 * @size: The total size of the struct
428 * @flags: Flags for a name entry (KDBUS_NAME_*)
429 * @id: Priviledged users may use this field to (de)register
430 * names on behalf of other peers.
431 * @conn_flags: The flags of the owning connection
432 * @name: The well-known name
434 * This structure is used with the KDBUS_CMD_NAME_ACQUIRE ioctl.
435 * Refer to the documentation for more information.
437 struct kdbus_cmd_name {
445 /* KDBUS_CMD_NAME_LIST */
447 KDBUS_NAME_LIST_UNIQUE_NAMES = 1 << 0,
448 KDBUS_NAME_LIST_STARTERS_ONLY = 1 << 1,
449 KDBUS_NAME_LIST_QUEUED_OWNERS = 1 << 2,
453 * struct kdbus_cmd_name_list - request a list of name entries
454 * @flags: Flags for the query (KDBUS_NAME_LIST_*)
455 * @offset: The returned offset in the caller's pool buffer.
456 * The user must use KDBUS_CMD_FREE to free the
458 * @name If KDBUS_NAME_LIST_QUEUED_OWNERS is set in @flags,
459 * a name must be provided.
461 * This structure is used with the KDBUS_CMD_NAME_LIST ioctl.
462 * Refer to the documentation for more information.
464 struct kdbus_cmd_name_list {
471 * struct kdbus_name_list - information returned by KDBUS_CMD_NAME_LIST
472 * @size: The total size of the structure
473 * @names: A list of names
475 * Note that the user is responsible for freeing the allocated memory with
476 * the KDBUS_CMD_FREE ioctl.
478 struct kdbus_name_list {
480 struct kdbus_cmd_name names[0];
483 /* KDBUS_CMD_CONN_INFO */
486 * struct kdbus_cmd_conn_info - struct used for KDBUS_CMD_CONN_INFO ioctl
487 * @size: The total size of the struct
488 * @flags: Query flags, currently unused
489 * @id: The 64-bit ID of the connection. If set to zero, passing
490 * @name is required. kdbus will look up the name to determine
491 * the ID in this case.
492 * @offset: Returned offset in the caller's pool buffer where the
493 * kdbus_name_info struct result is stored. The user must
494 * use KDBUS_CMD_FREE to free the allocated memory.
495 * @name: The optional well-known name to look up. Only needed in
498 * On success, the KDBUS_CMD_CONN_INFO ioctl will return 0 and @offset will
499 * tell the user the offset in the connection pool buffer at which to find the
500 * result in a struct kdbus_conn_info.
502 struct kdbus_cmd_conn_info {
504 __u64 flags; /* query flags */
505 __u64 id; /* either ID, or 0 and name follows */
506 __u64 offset; /* returned offset in the caller's buffer */
511 * struct kdbus_conn_info - information returned by KDBUS_CMD_CONN_INFO
512 * @size: The total size of the struct
513 * @id: The connection's 64-bit ID
514 * @flags: The connection's flags
515 * @items: A list of struct kdbus_item
517 * Note that the user is responsible for freeing the allocated memory with
518 * the KDBUS_CMD_FREE ioctl.
520 struct kdbus_conn_info {
523 __u64 flags; /* connection flags */
524 struct kdbus_item items[0]; /* list of item records */
527 /* KDBUS_CMD_MATCH_ADD/REMOVE */
530 KDBUS_MATCH_BLOOM, /* Matches a mask blob against KDBUS_MSG_BLOOM */
531 KDBUS_MATCH_SRC_NAME, /* Matches a name string against KDBUS_MSG_SRC_NAMES */
532 KDBUS_MATCH_NAME_ADD, /* Matches a name string against KDBUS_MSG_NAME_ADD */
533 KDBUS_MATCH_NAME_REMOVE, /* Matches a name string against KDBUS_MSG_NAME_REMOVE */
534 KDBUS_MATCH_NAME_CHANGE, /* Matches a name string against KDBUS_MSG_NAME_CHANGE */
535 KDBUS_MATCH_ID_ADD, /* Matches an ID against KDBUS_MSG_ID_ADD */
536 KDBUS_MATCH_ID_REMOVE, /* Matches an ID against KDBUS_MSG_ID_REMOVE */
540 * struct kdbus_cmd_match - struct to add or remove matches
541 * @size: The total size of the struct
542 * @id: Priviledged users may (de)register matches on behalf
544 * In other cases, set to 0.
545 * @cookie: Userspace supplied cookie. When removing, the cookie is
546 * suffices as information
547 * @src_id: The source ID to match against. Use KDBUS_MATCH_SRC_ID_ANY or
548 * any other value for a unique match.
549 * @items: A list of items for additional information
551 * This structure is used with the KDBUS_CMD_ADD_MATCH and
552 * KDBUS_CMD_REMOVE_MATCH ioctl. Refer to the documentation for more
555 struct kdbus_cmd_match {
557 __u64 id; /* We allow registration/deregestration of matches for other peers */
558 __u64 cookie; /* userspace supplied cookie; when removing; kernel deletes everything with same cookie */
559 __u64 src_id; /* ~0: any. other: exact unique match */
560 struct kdbus_item items[0];
563 /* KDBUS_CMD_MONITOR */
565 KDBUS_MONITOR_ENABLE = 1 << 0,
569 * struct kdbus_cmd_monitor - struct to enable or disable eavesdropping
570 * @id: Priviledged users may enable or disable the monitor feature
571 * on behalf of other peers
572 * @flags: Use KDBUS_MONITOR_ENABLE to enable eavesdropping
574 * This structure is used with the KDBUS_CMD_MONITOR ioctl.
575 * Refer to the documentation for more information.
577 struct kdbus_cmd_monitor {
583 /* kdbus control node commands: require unset state */
584 KDBUS_CMD_BUS_MAKE = _IOW(KDBUS_IOC_MAGIC, 0x00, struct kdbus_cmd_bus_make),
585 KDBUS_CMD_NS_MAKE = _IOR(KDBUS_IOC_MAGIC, 0x10, struct kdbus_cmd_ns_make),
587 /* kdbus ep node commands: require unset state */
588 KDBUS_CMD_EP_MAKE = _IOW(KDBUS_IOC_MAGIC, 0x20, struct kdbus_cmd_ep_make),
589 KDBUS_CMD_HELLO = _IOWR(KDBUS_IOC_MAGIC, 0x30, struct kdbus_cmd_hello),
591 /* kdbus ep node commands: require connected state */
592 KDBUS_CMD_MSG_SEND = _IOW(KDBUS_IOC_MAGIC, 0x40, struct kdbus_msg),
593 KDBUS_CMD_MSG_RECV = _IOR(KDBUS_IOC_MAGIC, 0x41, __u64 *),
594 KDBUS_CMD_FREE = _IOW(KDBUS_IOC_MAGIC, 0x42, __u64 *),
596 KDBUS_CMD_NAME_ACQUIRE = _IOWR(KDBUS_IOC_MAGIC, 0x50, struct kdbus_cmd_name),
597 KDBUS_CMD_NAME_RELEASE = _IOW(KDBUS_IOC_MAGIC, 0x51, struct kdbus_cmd_name),
598 KDBUS_CMD_NAME_LIST = _IOWR(KDBUS_IOC_MAGIC, 0x52, struct kdbus_cmd_name_list),
600 KDBUS_CMD_CONN_INFO = _IOWR(KDBUS_IOC_MAGIC, 0x60, struct kdbus_cmd_conn_info),
602 KDBUS_CMD_MATCH_ADD = _IOW(KDBUS_IOC_MAGIC, 0x70, struct kdbus_cmd_match),
603 KDBUS_CMD_MATCH_REMOVE = _IOW(KDBUS_IOC_MAGIC, 0x71, struct kdbus_cmd_match),
604 KDBUS_CMD_MONITOR = _IOW(KDBUS_IOC_MAGIC, 0x72, struct kdbus_cmd_monitor),
606 /* kdbus ep node commands: require ep owner state */
607 KDBUS_CMD_EP_POLICY_SET = _IOW(KDBUS_IOC_MAGIC, 0x80, struct kdbus_cmd_policy),
609 /* kdbus memfd commands: */
610 KDBUS_CMD_MEMFD_NEW = _IOR(KDBUS_IOC_MAGIC, 0x90, int *),
611 KDBUS_CMD_MEMFD_SIZE_GET = _IOR(KDBUS_IOC_MAGIC, 0x91, __u64 *),
612 KDBUS_CMD_MEMFD_SIZE_SET = _IOW(KDBUS_IOC_MAGIC, 0x92, __u64 *),
613 KDBUS_CMD_MEMFD_SEAL_GET = _IOR(KDBUS_IOC_MAGIC, 0x93, int *),
614 KDBUS_CMD_MEMFD_SEAL_SET = _IO(KDBUS_IOC_MAGIC, 0x94),