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_notify_name_change - name registry change message
31 * @old_id: Former owner of a name
32 * @new_id: New owner of a name
33 * @flags: flags from KDBUS_NAME_*
34 * @name: Well-known name
38 * KDBUS_ITEM_NAME_REMOVE
39 * KDBUS_ITEM_NAME_CHANGE
41 * Sent from kernel to userspace when the owner or starter of
42 * a well-known name changes.
44 struct kdbus_notify_name_change {
52 * struct kdbus_notify_id_change - name registry change message
53 * @id: New or former owner of the name
54 * @flags: flags field from KDBUS_HELLO_*
58 * KDBUS_ITEM_ID_REMOVE
60 * Sent from kernel to userspace when the owner or starter of
61 * a well-known name changes.
63 struct kdbus_notify_id_change {
69 * struct kdbus_creds - process credentials
74 * @starttime: Starttime of the process
76 * The starttime of the process PID. This is useful to detect PID overruns
77 * from the client side. i.e. if you use the PID to look something up in
78 * /proc/$PID/ you can afterwards check the starttime field of it, to ensure
79 * you didn't run into a PID overrun.
90 * struct kdbus_audit - audit information
91 * @sessionid: The audit session ID
92 * @loginuid: The audit login uid
100 * struct kdbus_timestamp
101 * @monotonic_ns: Monotonic timestamp, in nanoseconds
102 * @realtime_ns: Realtime timestamp, in nanoseconds
104 struct kdbus_timestamp {
110 * struct kdbus_vec - I/O vector for kdbus payload items
111 * @size: The size of the vector
112 * @address: Memory address for memory addresses
113 * @offset: Offset in the in-message payload memory
124 * struct kdbus_memfd - a kdbus memfd
125 * @size: The memfd's size
126 * @fd: The file descriptor number
127 * @__pad: Padding to make the struct aligned
136 * struct kdbus_name - a registered well-known name with its flags
137 * @flags: flags from KDBUS_NAME_*
138 * @name: well-known name
146 * struct kdbus_policy_access - policy access item
147 * @type: One of KDBUS_POLICY_ACCESS_* types
148 * @bits: Access to grant. One of KDBUS_POLICY_*
149 * @id: For KDBUS_POLICY_ACCESS_USER, the uid
150 * For KDBUS_POLICY_ACCESS_GROUP, the gid
152 struct kdbus_policy_access {
153 __u64 type; /* USER, GROUP, WORLD */
154 __u64 bits; /* RECV, SEND, OWN */
155 __u64 id; /* uid, gid, 0 */
159 * struct kdbus_policy - a policy to upload
160 * @size: The total size of the structure
161 * @type: KDBUS_POLICY_NAME or KDBUS_POLICY_ACCESS
162 * @name: The well-known name to grant access to,
163 * if @type is KDBUS_POLICY_NAME
164 * @access: The policy access details,
165 * if @type is KDBUS_POLICY_ACCESS
167 struct kdbus_policy {
169 struct kdbus_policy_access access;
174 /* Message Item Types */
178 /* Filled in by userspace */
179 _KDBUS_ITEM_USER_BASE,
180 KDBUS_ITEM_PAYLOAD_VEC = _KDBUS_ITEM_USER_BASE,
181 KDBUS_ITEM_PAYLOAD_OFF, /* .data_vec, reference to memory area */
182 KDBUS_ITEM_PAYLOAD_MEMFD, /* file descriptor of a special data file */
183 KDBUS_ITEM_FDS, /* .data_fds of file descriptors */
184 KDBUS_ITEM_BLOOM, /* for broadcasts, carries bloom filter blob in .data */
185 KDBUS_ITEM_DST_NAME, /* destination's well-known name, in .str */
186 KDBUS_ITEM_PRIORITY, /* queue priority for message */
188 _KDBUS_ITEM_POLICY_BASE = 0x400,
189 KDBUS_ITEM_POLICY_NAME = _KDBUS_ITEM_POLICY_BASE,
190 KDBUS_ITEM_POLICY_ACCESS,
192 /* Filled in by kernelspace */
193 _KDBUS_ITEM_ATTACH_BASE = 0x600,
194 KDBUS_ITEM_NAME = _KDBUS_ITEM_ATTACH_BASE,
195 KDBUS_ITEM_STARTER_NAME, /* Only used in HELLO for starter connection */
196 KDBUS_ITEM_TIMESTAMP, /* .timestamp */
197 KDBUS_ITEM_CREDS, /* .creds */
198 KDBUS_ITEM_PID_COMM, /* optional, in .str */
199 KDBUS_ITEM_TID_COMM, /* optional, in .str */
200 KDBUS_ITEM_EXE, /* optional, in .str */
201 KDBUS_ITEM_CMDLINE, /* optional, in .str (a chain of NUL str) */
202 KDBUS_ITEM_CGROUP, /* optional, in .str */
203 KDBUS_ITEM_CAPS, /* caps data blob, in .data */
204 KDBUS_ITEM_SECLABEL, /* NUL terminated string, in .str */
205 KDBUS_ITEM_AUDIT, /* .audit */
207 /* Special messages from kernel, consisting of one and only one of these data blocks */
208 _KDBUS_ITEM_KERNEL_BASE = 0x800,
209 KDBUS_ITEM_NAME_ADD = _KDBUS_ITEM_KERNEL_BASE,
210 KDBUS_ITEM_NAME_REMOVE, /* .name_change */
211 KDBUS_ITEM_NAME_CHANGE, /* .name_change */
212 KDBUS_ITEM_ID_ADD, /* .id_change */
213 KDBUS_ITEM_ID_REMOVE, /* .id_change */
214 KDBUS_ITEM_REPLY_TIMEOUT, /* empty, but .reply_cookie in .kdbus_msg is filled in */
215 KDBUS_ITEM_REPLY_DEAD, /* dito */
219 * struct kdbus_item - chain of data blocks
220 * @size: : overall data record size
221 * @type: kdbus_item type of data
237 struct kdbus_vec vec;
239 /* process credentials and properties*/
240 struct kdbus_creds creds;
241 struct kdbus_audit audit;
242 struct kdbus_timestamp timestamp;
243 struct kdbus_name name;
245 /* specific fields */
246 struct kdbus_memfd memfd;
248 struct kdbus_notify_name_change name_change;
249 struct kdbus_notify_id_change id_change;
250 struct kdbus_policy policy;
255 KDBUS_MSG_FLAGS_EXPECT_REPLY = 1 << 0,
256 KDBUS_MSG_FLAGS_NO_AUTO_START = 1 << 1,
260 KDBUS_PAYLOAD_KERNEL,
261 KDBUS_PAYLOAD_DBUS1 = 0x4442757356657231ULL, /* 'DBusVer1' */
262 KDBUS_PAYLOAD_GVARIANT = 0x4756617269616e74ULL, /* 'GVariant' */
266 * struct kdbus_msg - the representation of a kdbus message
267 * @size: Total size of the message
268 * @flags: Message flags (KDBUS_MSG_FLAGS_*)
269 * @dst_id: 64-bit ID of the destination connection
270 * @src_id: 64-bit ID of the source connection
271 * @payload_type: Payload type (KDBUS_PAYLOAD_*)
272 * @cookie: Userspace-supplied cookie
273 * @cookie_reply: For kernel-generated messages, this is the cookie
274 * the message is a reply to
275 * @timeout_ns: For non-kernel-generated messages, this denotes the
276 * message timeout in nanoseconds
277 * @items: A list of kdbus_items containing the message payload
290 struct kdbus_item items[0];
294 _KDBUS_POLICY_ACCESS_NULL,
295 KDBUS_POLICY_ACCESS_USER,
296 KDBUS_POLICY_ACCESS_GROUP,
297 KDBUS_POLICY_ACCESS_WORLD,
301 KDBUS_POLICY_RECV = 1 << 2,
302 KDBUS_POLICY_SEND = 1 << 1,
303 KDBUS_POLICY_OWN = 1 << 0,
307 * struct kdbus_cmd_policy - a series of policies to upload
308 * @size: The total size of the structure
309 * @policies: The policies to upload
311 * A KDBUS_POLICY_NAME must always preceed a KDBUS_POLICY_ACCESS entry.
312 * A new KDBUS_POLICY_NAME can be added after KDBUS_POLICY_ACCESS for
313 * chaining multiple policies together.
315 struct kdbus_cmd_policy {
317 struct kdbus_item policies[0];
320 /* Flags for struct kdbus_cmd_hello */
322 KDBUS_HELLO_STARTER = 1 << 0,
323 KDBUS_HELLO_ACCEPT_FD = 1 << 1,
326 /* Flags for message attachments */
328 KDBUS_ATTACH_TIMESTAMP = 1 << 0,
329 KDBUS_ATTACH_CREDS = 1 << 1,
330 KDBUS_ATTACH_NAMES = 1 << 2,
331 KDBUS_ATTACH_COMM = 1 << 3,
332 KDBUS_ATTACH_EXE = 1 << 4,
333 KDBUS_ATTACH_CMDLINE = 1 << 5,
334 KDBUS_ATTACH_CGROUP = 1 << 6,
335 KDBUS_ATTACH_CAPS = 1 << 7,
336 KDBUS_ATTACH_SECLABEL = 1 << 8,
337 KDBUS_ATTACH_AUDIT = 1 << 9,
341 * struct kdbus_cmd_hello - struct to say hello to kdbus
342 * @size: The total size of the structure
343 * @conn_flags: Connection flags (KDBUS_HELLO_*). The kernel will
344 * return its capabilities in that field.
345 * @attach_flags: Mask of metdata to attach to each message sent
347 * @bus_flags: The flags field copied verbatim from the original
348 * KDBUS_CMD_BUS_MAKE ioctl. It's intended to be useful
349 * to do negotiation of features of the payload that is
350 * transferred (kernel → userspace)
351 * @id: The ID of this connection (kernel → userspace)
352 * @bloom_size: The bloom filter size chosen by the owner
353 * (kernel → userspace)
354 * @pool_size: Maximum size of the pool buffer (kernel → userspace)
355 * @id128: Unique 128-bit ID of the bus (kernel → userspace)
356 * @items: A list of items
358 * This struct is used with the KDBUS_CMD_HELLO ioctl. See the ioctl
359 * documentation for more information.
361 struct kdbus_cmd_hello {
364 /* userspace → kernel, kernel → userspace */
368 /* kernel → userspace */
375 struct kdbus_item items[0];
378 /* Flags for KDBUS_CMD_{BUS,EP,NS}_MAKE */
380 KDBUS_MAKE_ACCESS_GROUP = 1 << 0,
381 KDBUS_MAKE_ACCESS_WORLD = 1 << 1,
382 KDBUS_MAKE_POLICY_OPEN = 1 << 2,
385 /* Items to append to KDBUS_CMD_{BUS,EP,NS}_MAKE */
392 * struct kdbus_cmd_bus_make - struct to make a bus
393 * @size: The total size of the struct
394 * @flags: Properties for the bus to create
395 * @bloom_size: Size of the bloom filter for this bus
396 * @items: Items describing details such as the name of the bus
398 * This structure is used with the KDBUS_CMD_BUS_MAKE ioctl. Refer to the
399 * documentation for more information.
401 struct kdbus_cmd_bus_make {
405 struct kdbus_item items[0];
409 * struct kdbus_cmd_ep_make - struct to make an endpoint
410 * @size: The total size of the struct
411 * @flags: Unused for now
412 * @items: Items describing details such as the
413 * name of the endpoint
415 * This structure is used with the KDBUS_CMD_EP_MAKE ioctl. Refer to the
416 * documentation for more information.
418 struct kdbus_cmd_ep_make {
421 struct kdbus_item items[0];
425 * struct kdbus_cmd_ns_make - struct to make a namespace
426 * @size: The total size of the struct
427 * @flags: Unused for now
428 * @items: Items describing details such as the
429 * name of the namespace
431 * This structure is used with the KDBUS_CMD_NS_MAKE ioctl. Refer to the
432 * documentation for more information.
434 struct kdbus_cmd_ns_make {
437 struct kdbus_item items[0];
441 /* userspace → kernel */
442 KDBUS_NAME_REPLACE_EXISTING = 1 << 0,
443 KDBUS_NAME_QUEUE = 1 << 1,
444 KDBUS_NAME_ALLOW_REPLACEMENT = 1 << 2,
446 /* kernel → userspace */
447 KDBUS_NAME_IN_QUEUE = 1 << 16,
451 * struct kdbus_cmd_name - struct to describe a well-known name
452 * @size: The total size of the struct
453 * @flags: Flags for a name entry (KDBUS_NAME_*)
454 * @id: Priviledged users may use this field to (de)register
455 * names on behalf of other peers.
456 * @conn_flags: The flags of the owning connection (KDBUS_HELLO_*)
457 * @name: The well-known name
459 * This structure is used with the KDBUS_CMD_NAME_ACQUIRE ioctl.
460 * Refer to the documentation for more information.
462 struct kdbus_cmd_name {
471 KDBUS_NAME_LIST_UNIQUE = 1 << 0,
472 KDBUS_NAME_LIST_NAMES = 1 << 1,
473 KDBUS_NAME_LIST_STARTERS = 1 << 2,
474 KDBUS_NAME_LIST_QUEUED = 1 << 3,
478 * struct kdbus_cmd_name_list - request a list of name entries
479 * @flags: Flags for the query (KDBUS_NAME_LIST_*)
480 * @offset: The returned offset in the caller's pool buffer.
481 * The user must use KDBUS_CMD_FREE to free the
484 * This structure is used with the KDBUS_CMD_NAME_LIST ioctl.
486 struct kdbus_cmd_name_list {
492 * struct kdbus_name_list - information returned by KDBUS_CMD_NAME_LIST
493 * @size: The total size of the structure
494 * @names: A list of names
496 * Note that the user is responsible for freeing the allocated memory with
497 * the KDBUS_CMD_FREE ioctl.
499 struct kdbus_name_list {
501 struct kdbus_cmd_name names[0];
505 * struct kdbus_cmd_conn_info - struct used for KDBUS_CMD_CONN_INFO ioctl
506 * @size: The total size of the struct
507 * @flags: KDBUS_ATTACH_* flags
508 * @id: The 64-bit ID of the connection. If set to zero, passing
509 * @name is required. kdbus will look up the name to determine
510 * the ID in this case.
511 * @offset: Returned offset in the caller's pool buffer where the
512 * kdbus_name_info struct result is stored. The user must
513 * use KDBUS_CMD_FREE to free the allocated memory.
514 * @name: The optional well-known name to look up. Only needed in
517 * On success, the KDBUS_CMD_CONN_INFO ioctl will return 0 and @offset will
518 * tell the user the offset in the connection pool buffer at which to find the
519 * result in a struct kdbus_conn_info.
521 struct kdbus_cmd_conn_info {
530 * struct kdbus_conn_info - information returned by KDBUS_CMD_CONN_INFO
531 * @size: The total size of the struct
532 * @id: The connection's 64-bit ID
533 * @flags: The connection's flags
534 * @items: A list of struct kdbus_item
536 * Note that the user is responsible for freeing the allocated memory with
537 * the KDBUS_CMD_FREE ioctl.
539 struct kdbus_conn_info {
543 struct kdbus_item items[0];
548 KDBUS_MATCH_BLOOM, /* Matches a mask blob against KDBUS_MSG_BLOOM */
549 KDBUS_MATCH_SRC_NAME, /* Matches a name string against KDBUS_ITEM_NAME */
550 KDBUS_MATCH_NAME_ADD, /* Matches a name string against KDBUS_ITEM_NAME_ADD */
551 KDBUS_MATCH_NAME_REMOVE, /* Matches a name string against KDBUS_ITEM_NAME_REMOVE */
552 KDBUS_MATCH_NAME_CHANGE, /* Matches a name string against KDBUS_ITEM_NAME_CHANGE */
553 KDBUS_MATCH_ID_ADD, /* Matches an ID against KDBUS_MSG_ID_ADD */
554 KDBUS_MATCH_ID_REMOVE, /* Matches an ID against KDBUS_MSG_ID_REMOVE */
558 * struct kdbus_cmd_match - struct to add or remove matches
559 * @size: The total size of the struct
560 * @id: Priviledged users may (de)register matches on behalf
562 * In other cases, set to 0.
563 * @cookie: Userspace supplied cookie. When removing, the cookie is
564 * suffices as information
565 * @src_id: The source ID to match against. Use KDBUS_MATCH_SRC_ID_ANY or
566 * any other value for a unique match.
567 * @items: A list of items for additional information
569 * This structure is used with the KDBUS_CMD_ADD_MATCH and
570 * KDBUS_CMD_REMOVE_MATCH ioctl. Refer to the documentation for more
573 struct kdbus_cmd_match {
578 struct kdbus_item items[0];
582 KDBUS_MONITOR_ENABLE = 1 << 0,
586 * struct kdbus_cmd_monitor - struct to enable or disable eavesdropping
587 * @id: Priviledged users may enable or disable the monitor feature
588 * on behalf of other peers
589 * @flags: Use KDBUS_MONITOR_ENABLE to enable eavesdropping
591 * This structure is used with the KDBUS_CMD_MONITOR ioctl.
592 * Refer to the documentation for more information.
594 struct kdbus_cmd_monitor {
600 /* kdbus control node commands: require unset state */
601 KDBUS_CMD_BUS_MAKE = _IOW(KDBUS_IOC_MAGIC, 0x00, struct kdbus_cmd_bus_make),
602 KDBUS_CMD_NS_MAKE = _IOR(KDBUS_IOC_MAGIC, 0x10, struct kdbus_cmd_ns_make),
604 /* kdbus ep node commands: require unset state */
605 KDBUS_CMD_EP_MAKE = _IOW(KDBUS_IOC_MAGIC, 0x20, struct kdbus_cmd_ep_make),
606 KDBUS_CMD_HELLO = _IOWR(KDBUS_IOC_MAGIC, 0x30, struct kdbus_cmd_hello),
608 /* kdbus ep node commands: require connected state */
609 KDBUS_CMD_MSG_SEND = _IOW(KDBUS_IOC_MAGIC, 0x40, struct kdbus_msg),
610 KDBUS_CMD_MSG_RECV = _IOR(KDBUS_IOC_MAGIC, 0x41, __u64 *),
611 KDBUS_CMD_FREE = _IOW(KDBUS_IOC_MAGIC, 0x42, __u64 *),
613 KDBUS_CMD_NAME_ACQUIRE = _IOWR(KDBUS_IOC_MAGIC, 0x50, struct kdbus_cmd_name),
614 KDBUS_CMD_NAME_RELEASE = _IOW(KDBUS_IOC_MAGIC, 0x51, struct kdbus_cmd_name),
615 KDBUS_CMD_NAME_LIST = _IOWR(KDBUS_IOC_MAGIC, 0x52, struct kdbus_cmd_name_list),
617 KDBUS_CMD_CONN_INFO = _IOWR(KDBUS_IOC_MAGIC, 0x60, struct kdbus_cmd_conn_info),
619 KDBUS_CMD_MATCH_ADD = _IOW(KDBUS_IOC_MAGIC, 0x70, struct kdbus_cmd_match),
620 KDBUS_CMD_MATCH_REMOVE = _IOW(KDBUS_IOC_MAGIC, 0x71, struct kdbus_cmd_match),
621 KDBUS_CMD_MONITOR = _IOW(KDBUS_IOC_MAGIC, 0x72, struct kdbus_cmd_monitor),
623 /* kdbus ep node commands: require ep owner state */
624 KDBUS_CMD_EP_POLICY_SET = _IOW(KDBUS_IOC_MAGIC, 0x80, struct kdbus_cmd_policy),
626 /* kdbus memfd commands: */
627 KDBUS_CMD_MEMFD_NEW = _IOR(KDBUS_IOC_MAGIC, 0x90, int *),
628 KDBUS_CMD_MEMFD_SIZE_GET = _IOR(KDBUS_IOC_MAGIC, 0x91, __u64 *),
629 KDBUS_CMD_MEMFD_SIZE_SET = _IOW(KDBUS_IOC_MAGIC, 0x92, __u64 *),
630 KDBUS_CMD_MEMFD_SEAL_GET = _IOR(KDBUS_IOC_MAGIC, 0x93, int *),
631 KDBUS_CMD_MEMFD_SEAL_SET = _IO(KDBUS_IOC_MAGIC, 0x94),