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_NAME (0)
26 #define KDBUS_MATCH_SRC_ID_ANY (~0ULL)
27 #define KDBUS_DST_ID_BROADCAST (~0ULL)
30 * struct KDBUS_PART_HEADER - anonymous struct used as header
31 * @size: Size of element, excluding padding bytes
32 * @type Type of element
34 * Common first elements in a structure, used to specify the type
35 * and size of the data.
37 #define KDBUS_PART_HEADER \
44 * struct kdbus_notify_name_change - name registry change message
45 * @old_id Former owner of a name
46 * @new_id New owner of a name
47 * @flags flags from KDBUS_NAME_*
48 * @name Well-known name
52 * KDBUS_ITEM_NAME_REMOVE
53 * KDBUS_ITEM_NAME_CHANGE
55 * Sent from kernel to userspace when the owner or starter of
56 * a well-known name changes.
58 struct kdbus_notify_name_change {
66 * struct kdbus_notify_id_change - name registry change message
67 * @id New or former owner of the name
68 * @flags flags field from KDBUS_HELLO_*
72 * KDBUS_ITEM_ID_REMOVE
74 * Sent from kernel to userspace when the owner or starter of
75 * a well-known name changes.
77 struct kdbus_notify_id_change {
83 * struct kdbus_creds - process credentials
88 * @starttime Starttime of the process
90 * The starttime of the process PID. This is useful to detect PID overruns
91 * from the client side. i.e. if you use the PID to look something up in
92 * /proc/$PID/ you can afterwards check the starttime field of it, to ensure
93 * you didn't run into a PID overrun.
104 * struct kdbus_audit - audit information
105 * @sessionid The audit session ID
106 * @loginuid The audit login uid
114 * struct kdbus_timestamp
115 * @monotonic_ns: Monotonic timestamp, in nanoseconds
116 * @realtime_ns: Realtime timestamp, in nanoseconds
118 struct kdbus_timestamp {
124 * struct kdbus_vec - I/O vector for kdbus payload items
125 * @size: The size of the vector
126 * @address Memory address for memory addresses
127 * @offset Offset in the in-message payload memory
138 * struct kdbus_memfd - a kdbus memfd
139 * @size: The memfd's size
140 * @fd: The file descriptor number
141 * @__pad: Padding to make the struct aligned
150 * struct kdbus_name - a registered well-known name with its flags
151 * @flags flags from KDBUS_NAME_*
152 * @name well-known name
159 /* Message Item Types */
163 /* Filled in by userspace */
164 _KDBUS_ITEM_USER_BASE = 1,
165 KDBUS_ITEM_PAYLOAD_VEC = 1, /* .data_vec, reference to memory area */
166 KDBUS_ITEM_PAYLOAD_OFF, /* .data_vec, reference to memory area */
167 KDBUS_ITEM_PAYLOAD_MEMFD, /* file descriptor of a special data file */
168 KDBUS_ITEM_FDS, /* .data_fds of file descriptors */
169 KDBUS_ITEM_BLOOM, /* for broadcasts, carries bloom filter blob in .data */
170 KDBUS_ITEM_DST_NAME, /* destination's well-known name, in .str */
171 KDBUS_ITEM_PRIORITY, /* queue priority for message */
173 /* Filled in by kernelspace */
174 _KDBUS_ITEM_ATTACH_BASE = 0x400,
175 KDBUS_ITEM_NAME = 0x400,/* NUL separated string list with well-known names of source */
176 KDBUS_ITEM_STARTER_NAME, /* Only used in HELLO for starter connection */
177 KDBUS_ITEM_TIMESTAMP, /* .timestamp */
179 /* when appended to a message, the following items refer to the sender */
180 KDBUS_ITEM_CREDS, /* .creds */
181 KDBUS_ITEM_PID_COMM, /* optional, in .str */
182 KDBUS_ITEM_TID_COMM, /* optional, in .str */
183 KDBUS_ITEM_EXE, /* optional, in .str */
184 KDBUS_ITEM_CMDLINE, /* optional, in .str (a chain of NUL str) */
185 KDBUS_ITEM_CGROUP, /* optional, in .str */
186 KDBUS_ITEM_CAPS, /* caps data blob, in .data */
187 KDBUS_ITEM_SECLABEL, /* NUL terminated string, in .str */
188 KDBUS_ITEM_AUDIT, /* .audit */
190 /* Special messages from kernel, consisting of one and only one of these data blocks */
191 _KDBUS_ITEM_KERNEL_BASE = 0x800,
192 KDBUS_ITEM_NAME_ADD = 0x800,/* .name_change */
193 KDBUS_ITEM_NAME_REMOVE, /* .name_change */
194 KDBUS_ITEM_NAME_CHANGE, /* .name_change */
195 KDBUS_ITEM_ID_ADD, /* .id_change */
196 KDBUS_ITEM_ID_REMOVE, /* .id_change */
197 KDBUS_ITEM_REPLY_TIMEOUT, /* empty, but .reply_cookie in .kdbus_msg is filled in */
198 KDBUS_ITEM_REPLY_DEAD, /* dito */
202 * struct kdbus_item - chain of data blocks
203 * @size : overall data record size
204 * @type: kdbus_item type of data
219 struct kdbus_vec vec;
221 /* process credentials and properties*/
222 struct kdbus_creds creds;
223 struct kdbus_audit audit;
224 struct kdbus_timestamp timestamp;
225 struct kdbus_name name;
227 /* specific fields */
228 struct kdbus_memfd memfd;
230 struct kdbus_notify_name_change name_change;
231 struct kdbus_notify_id_change id_change;
236 KDBUS_MSG_FLAGS_EXPECT_REPLY = 1 << 0,
237 KDBUS_MSG_FLAGS_NO_AUTO_START = 1 << 1,
241 KDBUS_PAYLOAD_KERNEL,
242 KDBUS_PAYLOAD_DBUS1 = 0x4442757356657231ULL, /* 'DBusVer1' */
243 KDBUS_PAYLOAD_GVARIANT = 0x4756617269616e74ULL, /* 'GVariant' */
247 * struct kdbus_msg - the representation of a kdbus message
248 * @size: Total size of the message
249 * @flags: Message flags (KDBUS_MSG_FLAGS_*)
250 * @dst_id: 64-bit ID of the destination connection
251 * @src_id: 64-bit ID of the source connection
252 * @payload_type: Payload type (KDBUS_PAYLOAD_*)
253 * @cookie: Userspace-supplied cookie
254 * @cookie_reply: For kernel-generated messages, this is the cookie
255 * the message is a reply to
256 * @timeout_ns: For non-kernel-generated messages, this denotes the
257 * message timeout in nanoseconds
258 * @items: A list of kdbus_items containing the message payload
271 struct kdbus_item items[0];
281 _KDBUS_POLICY_ACCESS_NULL,
282 KDBUS_POLICY_ACCESS_USER,
283 KDBUS_POLICY_ACCESS_GROUP,
284 KDBUS_POLICY_ACCESS_WORLD,
288 KDBUS_POLICY_RECV = 1 << 2,
289 KDBUS_POLICY_SEND = 1 << 1,
290 KDBUS_POLICY_OWN = 1 << 0,
294 * struct kdbus_policy_access - policy access item
295 * @type: One of KDBUS_POLICY_ACCESS_* types
296 * @bits: Access to grant. One of KDBUS_POLICY_*
297 * @id: For KDBUS_POLICY_ACCESS_USER, the uid
298 * For KDBUS_POLICY_ACCESS_GROUP, the gid
300 struct kdbus_policy_access {
301 __u64 type; /* USER, GROUP, WORLD */
302 __u64 bits; /* RECV, SEND, OWN */
303 __u64 id; /* uid, gid, 0 */
307 * struct kdbus_policy - a policy to upload
308 * @size: The total size of the structure
309 * @type: KDBUS_POLICY_NAME or KDBUS_POLICY_ACCESS
310 * @name: The well-known name to grant access to,
311 * if @type is KDBUS_POLICY_NAME
312 * @access: The policy access details,
313 * if @type is KDBUS_POLICY_ACCESS
315 struct kdbus_policy {
319 struct kdbus_policy_access access;
324 * struct kdbus_cmd_policy - a series of policies to upload
325 * @size: The total size of the structure
326 * @policies: The policies to upload
328 * A KDBUS_POLICY_NAME must always preceed a KDBUS_POLICY_ACCESS entry.
329 * A new KDBUS_POLICY_NAME can be added after KDBUS_POLICY_ACCESS for
330 * chaining multiple policies together.
332 struct kdbus_cmd_policy {
334 struct kdbus_policy policies[0];
337 /* Flags for struct kdbus_cmd_hello */
339 KDBUS_HELLO_STARTER = 1 << 0,
340 KDBUS_HELLO_ACCEPT_FD = 1 << 1,
343 /* Flags for message attachments */
345 KDBUS_ATTACH_TIMESTAMP = 1 << 0,
346 KDBUS_ATTACH_CREDS = 1 << 1,
347 KDBUS_ATTACH_NAMES = 1 << 2,
348 KDBUS_ATTACH_COMM = 1 << 3,
349 KDBUS_ATTACH_EXE = 1 << 4,
350 KDBUS_ATTACH_CMDLINE = 1 << 5,
351 KDBUS_ATTACH_CGROUP = 1 << 6,
352 KDBUS_ATTACH_CAPS = 1 << 7,
353 KDBUS_ATTACH_SECLABEL = 1 << 8,
354 KDBUS_ATTACH_AUDIT = 1 << 9,
358 * struct kdbus_cmd_hello - struct to say hello to kdbus
359 * @size: The total size of the structure
360 * @conn_flags: Connection flags (KDBUS_HELLO_*). The kernel will
361 * return its capabilities in that field.
362 * @attach_flags: Mask of metdata to attach to each message sent
364 * @bus_flags: The flags field copied verbatim from the original
365 * KDBUS_CMD_BUS_MAKE ioctl. It's intended to be useful
366 * to do negotiation of features of the payload that is
367 * transferred (kernel → userspace)
368 * @id: The ID of this connection (kernel → userspace)
369 * @bloom_size: The bloom filter size chosen by the owner
370 * (kernel → userspace)
371 * @pool_size: Maximum size of the pool buffer (kernel → userspace)
372 * @id128: Unique 128-bit ID of the bus (kernel → userspace)
373 * @items; A list of items
375 * This struct is used with the KDBUS_CMD_HELLO ioctl. See the ioctl
376 * documentation for more information.
378 struct kdbus_cmd_hello {
381 /* userspace → kernel, kernel → userspace */
385 /* kernel → userspace */
392 struct kdbus_item items[0];
395 /* Flags for KDBUS_CMD_{BUS,EP,NS}_MAKE */
397 KDBUS_MAKE_ACCESS_GROUP = 1 << 0,
398 KDBUS_MAKE_ACCESS_WORLD = 1 << 1,
399 KDBUS_MAKE_POLICY_OPEN = 1 << 2,
402 /* Items to append to KDBUS_CMD_{BUS,EP,NS}_MAKE */
409 * struct kdbus_cmd_bus_make - struct to make a bus
410 * @size: The total size of the struct
411 * @flags: Properties for the bus to create
412 * @bloom_filter: Size of the bloom filter for this bus
413 * @items: Items describing details such as the name of the bus
415 * This structure is used with the KDBUS_CMD_BUS_MAKE ioctl. Refer to the
416 * documentation for more information.
418 struct kdbus_cmd_bus_make {
422 struct kdbus_item items[0];
426 * struct kdbus_cmd_ep_make - struct to make an endpoint
427 * @size: The total size of the struct
428 * @flags: Unused for now
429 * @items: Items describing details such as the
430 * name of the endpoint
432 * This structure is used with the KDBUS_CMD_EP_MAKE ioctl. Refer to the
433 * documentation for more information.
435 struct kdbus_cmd_ep_make {
438 struct kdbus_item items[0];
442 * struct kdbus_cmd_ns_make - struct to make a namespace
443 * @size: The total size of the struct
444 * @flags: Unused for now
445 * @items: Items describing details such as the
446 * name of the namespace
448 * This structure is used with the KDBUS_CMD_NS_MAKE ioctl. Refer to the
449 * documentation for more information.
451 struct kdbus_cmd_ns_make {
454 struct kdbus_item items[0];
458 /* userspace → kernel */
459 KDBUS_NAME_REPLACE_EXISTING = 1 << 0,
460 KDBUS_NAME_QUEUE = 1 << 1,
461 KDBUS_NAME_ALLOW_REPLACEMENT = 1 << 2,
463 /* kernel → userspace */
464 KDBUS_NAME_IN_QUEUE = 1 << 16,
468 * struct kdbus_cmd_name - struct to describe a well-known name
469 * @size: The total size of the struct
470 * @flags: Flags for a name entry (KDBUS_NAME_*)
471 * @id: Priviledged users may use this field to (de)register
472 * names on behalf of other peers.
473 * @conn_flags: The flags of the owning connection (KDBUS_HELLO_*)
474 * @name: The well-known name
476 * This structure is used with the KDBUS_CMD_NAME_ACQUIRE ioctl.
477 * Refer to the documentation for more information.
479 struct kdbus_cmd_name {
488 KDBUS_NAME_LIST_UNIQUE = 1 << 0,
489 KDBUS_NAME_LIST_NAMES = 1 << 1,
490 KDBUS_NAME_LIST_STARTERS = 1 << 2,
491 KDBUS_NAME_LIST_QUEUED = 1 << 3,
495 * struct kdbus_cmd_name_list - request a list of name entries
496 * @flags: Flags for the query (KDBUS_NAME_LIST_*)
497 * @offset: The returned offset in the caller's pool buffer.
498 * The user must use KDBUS_CMD_FREE to free the
501 * This structure is used with the KDBUS_CMD_NAME_LIST ioctl.
503 struct kdbus_cmd_name_list {
509 * struct kdbus_name_list - information returned by KDBUS_CMD_NAME_LIST
510 * @size: The total size of the structure
511 * @names: A list of names
513 * Note that the user is responsible for freeing the allocated memory with
514 * the KDBUS_CMD_FREE ioctl.
516 struct kdbus_name_list {
518 struct kdbus_cmd_name names[0];
522 * struct kdbus_cmd_conn_info - struct used for KDBUS_CMD_CONN_INFO ioctl
523 * @size: The total size of the struct
524 * @flags: KDBUS_ATTACH_* flags
525 * @id: The 64-bit ID of the connection. If set to zero, passing
526 * @name is required. kdbus will look up the name to determine
527 * the ID in this case.
528 * @offset: Returned offset in the caller's pool buffer where the
529 * kdbus_name_info struct result is stored. The user must
530 * use KDBUS_CMD_FREE to free the allocated memory.
531 * @name: The optional well-known name to look up. Only needed in
534 * On success, the KDBUS_CMD_CONN_INFO ioctl will return 0 and @offset will
535 * tell the user the offset in the connection pool buffer at which to find the
536 * result in a struct kdbus_conn_info.
538 struct kdbus_cmd_conn_info {
547 * struct kdbus_conn_info - information returned by KDBUS_CMD_CONN_INFO
548 * @size: The total size of the struct
549 * @id: The connection's 64-bit ID
550 * @flags: The connection's flags
551 * @items: A list of struct kdbus_item
553 * Note that the user is responsible for freeing the allocated memory with
554 * the KDBUS_CMD_FREE ioctl.
556 struct kdbus_conn_info {
560 struct kdbus_item items[0];
565 KDBUS_MATCH_BLOOM, /* Matches a mask blob against KDBUS_MSG_BLOOM */
566 KDBUS_MATCH_SRC_NAME, /* Matches a name string against KDBUS_ITEM_NAME */
567 KDBUS_MATCH_NAME_ADD, /* Matches a name string against KDBUS_ITEM_NAME_ADD */
568 KDBUS_MATCH_NAME_REMOVE, /* Matches a name string against KDBUS_ITEM_NAME_REMOVE */
569 KDBUS_MATCH_NAME_CHANGE, /* Matches a name string against KDBUS_ITEM_NAME_CHANGE */
570 KDBUS_MATCH_ID_ADD, /* Matches an ID against KDBUS_MSG_ID_ADD */
571 KDBUS_MATCH_ID_REMOVE, /* Matches an ID against KDBUS_MSG_ID_REMOVE */
575 * struct kdbus_cmd_match - struct to add or remove matches
576 * @size: The total size of the struct
577 * @id: Priviledged users may (de)register matches on behalf
579 * In other cases, set to 0.
580 * @cookie: Userspace supplied cookie. When removing, the cookie is
581 * suffices as information
582 * @src_id: The source ID to match against. Use KDBUS_MATCH_SRC_ID_ANY or
583 * any other value for a unique match.
584 * @items: A list of items for additional information
586 * This structure is used with the KDBUS_CMD_ADD_MATCH and
587 * KDBUS_CMD_REMOVE_MATCH ioctl. Refer to the documentation for more
590 struct kdbus_cmd_match {
595 struct kdbus_item items[0];
599 KDBUS_MONITOR_ENABLE = 1 << 0,
603 * struct kdbus_cmd_monitor - struct to enable or disable eavesdropping
604 * @id: Priviledged users may enable or disable the monitor feature
605 * on behalf of other peers
606 * @flags: Use KDBUS_MONITOR_ENABLE to enable eavesdropping
608 * This structure is used with the KDBUS_CMD_MONITOR ioctl.
609 * Refer to the documentation for more information.
611 struct kdbus_cmd_monitor {
617 /* kdbus control node commands: require unset state */
618 KDBUS_CMD_BUS_MAKE = _IOW(KDBUS_IOC_MAGIC, 0x00, struct kdbus_cmd_bus_make),
619 KDBUS_CMD_NS_MAKE = _IOR(KDBUS_IOC_MAGIC, 0x10, struct kdbus_cmd_ns_make),
621 /* kdbus ep node commands: require unset state */
622 KDBUS_CMD_EP_MAKE = _IOW(KDBUS_IOC_MAGIC, 0x20, struct kdbus_cmd_ep_make),
623 KDBUS_CMD_HELLO = _IOWR(KDBUS_IOC_MAGIC, 0x30, struct kdbus_cmd_hello),
625 /* kdbus ep node commands: require connected state */
626 KDBUS_CMD_MSG_SEND = _IOW(KDBUS_IOC_MAGIC, 0x40, struct kdbus_msg),
627 KDBUS_CMD_MSG_RECV = _IOR(KDBUS_IOC_MAGIC, 0x41, __u64 *),
628 KDBUS_CMD_FREE = _IOW(KDBUS_IOC_MAGIC, 0x42, __u64 *),
630 KDBUS_CMD_NAME_ACQUIRE = _IOWR(KDBUS_IOC_MAGIC, 0x50, struct kdbus_cmd_name),
631 KDBUS_CMD_NAME_RELEASE = _IOW(KDBUS_IOC_MAGIC, 0x51, struct kdbus_cmd_name),
632 KDBUS_CMD_NAME_LIST = _IOWR(KDBUS_IOC_MAGIC, 0x52, struct kdbus_cmd_name_list),
634 KDBUS_CMD_CONN_INFO = _IOWR(KDBUS_IOC_MAGIC, 0x60, struct kdbus_cmd_conn_info),
636 KDBUS_CMD_MATCH_ADD = _IOW(KDBUS_IOC_MAGIC, 0x70, struct kdbus_cmd_match),
637 KDBUS_CMD_MATCH_REMOVE = _IOW(KDBUS_IOC_MAGIC, 0x71, struct kdbus_cmd_match),
638 KDBUS_CMD_MONITOR = _IOW(KDBUS_IOC_MAGIC, 0x72, struct kdbus_cmd_monitor),
640 /* kdbus ep node commands: require ep owner state */
641 KDBUS_CMD_EP_POLICY_SET = _IOW(KDBUS_IOC_MAGIC, 0x80, struct kdbus_cmd_policy),
643 /* kdbus memfd commands: */
644 KDBUS_CMD_MEMFD_NEW = _IOR(KDBUS_IOC_MAGIC, 0x90, int *),
645 KDBUS_CMD_MEMFD_SIZE_GET = _IOR(KDBUS_IOC_MAGIC, 0x91, __u64 *),
646 KDBUS_CMD_MEMFD_SIZE_SET = _IOW(KDBUS_IOC_MAGIC, 0x92, __u64 *),
647 KDBUS_CMD_MEMFD_SEAL_GET = _IOR(KDBUS_IOC_MAGIC, 0x93, int *),
648 KDBUS_CMD_MEMFD_SEAL_SET = _IO(KDBUS_IOC_MAGIC, 0x94),