Definition
BlueZ is the official Bluetooth protocol stack of Linux: the layered software through which Linux uses a Bluetooth radio. Its kernel half, part of the Linux kernel, holds the device drivers §6.38 and the lower protocols. Its user-space half runs as ordinary programs outside the kernel; at its center is bluetoothd, a daemon (a background program) that runs the upper protocols and serves applications over D-Bus §12.22, the standard Linux message bus between programs. The radio chip and its firmware are not part of BlueZ. The name is not an abbreviation.
The idea
Reading one value from a Bluetooth Low Energy (BLE) §7.1 sensor takes work on three time scales: radio packets timed to within microseconds; routing, buffering and security checks for every packet; and slow decisions, such as which devices to trust, which keys to keep, and whether the user confirms a new pairing.
The Bluetooth Core Specification, the standard that defines Bluetooth, splits this work between a Bluetooth Controller §7.4, the radio chip with its firmware, which does the timing-critical radio work, and a Bluetooth Host, software on the main processor, which does everything above it. The two talk only through HCI §7.5 (Host Controller Interface), a standard set of commands, events and data packets. Linux splits the Host once more. Per-packet work and the keys that guard each connection sit in the kernel, which no ordinary program can bypass. Work that needs files, user interaction or slow decisions sits in bluetoothd. On the usual path, an application asks bluetoothd, bluetoothd asks the kernel, and the kernel commands the Controller.
How it works
The parts below are those of a current mainline Linux kernel, the version that its maintainers publish, with version 5 of BlueZ’s user-space half.
The Controller, outside BlueZ, runs the Link Layer §7.6, the lowest protocol above the radio: it times each connection, acknowledges and resends packets, and encrypts them with a key from the Host. It reaches the Host over USB or over a serial line driven by a UART §6.23 (universal asynchronous receiver-transmitter).
Kernel drivers, such as
btusbfor USB andhci_uartfor serial lines, move HCI packets to and from the Controller.The HCI core sends HCI commands, receives HCI events, tracks each connection by its connection handle, a number that the Controller assigns, and sends data only while the Controller reports a free buffer.
Kernel protocols. L2CAP §7.15 (Logical Link Control and Adaptation Protocol) carries several logical channels over one connection, each named by a channel identifier (CID). SMP (Security Manager Protocol §7.19) runs BLE pairing, in which two devices agree on keys, and then has the Controller encrypt the link. The kernel holds the keys in use and passes no data on a socket until the link meets the security level §7.33 that the socket requires. The kernel also runs the protocols of Bluetooth Classic, the older Bluetooth radio system.
Interfaces to user space. Programs exchange data through sockets of the address family
AF_BLUETOOTH.bluetoothdcontrols each adapter through the management interface, a channel of commands and events for powering the radio, discovering devices, pairing and loading stored keys.
User space holds the rest. bluetoothd runs ATT §7.9 (Attribute Protocol), with which one device reads and writes small numbered data items, called attributes, on another; it sends ATT messages through an L2CAP socket on the channel that BLE reserves for ATT. It runs GATT §7.10 (Generic Attribute Profile), which groups attributes into services and characteristics, and it implements the profiles, the rules for particular uses such as keyboards and other input devices. It stores the records of known devices and their keys under /var/lib/bluetooth and applies policy: which devices are trusted, blocked or reconnected automatically. It offers all of this on the D-Bus system bus under the name org.bluez, as one object per adapter and per device, such as /org/bluez/hci0 for the first adapter. An agent, such as a desktop dialog or the command-line client bluetoothctl, registers with bluetoothd to answer pairing questions for the user.
BLE pairing shows the parts working together.
The kernel’s SMP code exchanges pairing messages with the peer.
When the user must confirm a six-digit number, the kernel sends a management event to
bluetoothd, which calls the agent’s methodRequestConfirmationover D-Bus.The user’s answer travels back the same way. The kernel completes pairing and has the Controller encrypt the link.
The kernel reports the new keys to
bluetoothd, which stores them and loads them back into the kernel each time it starts.
Worked example
An application reads the battery level of a connected BLE sensor. The value sits at attribute handle 0x0012, a 16-bit number that the sensor assigned. Each layer adds its own bytes.
The application calls the method
ReadValueon the D-Bus object of the characteristic, whichbluetoothdowns.bluetoothdbuilds an ATT Read Request: the operation code §2.80x0A, then the handle with its least significant byte first. The bytes are0A 12 00, 3 in all.The kernel’s L2CAP code adds a 4-byte header: the length 3 as
03 00, and the CID0x0004, which BLE reserves for ATT, as04 00. Total: bytes.The HCI core adds a 4-byte header: the connection handle, here
0x0040, packed with two flag fields that are 0 here, as40 00, then the length 7 as07 00. Total: bytes. It sends them once the Controller reports a free buffer.A UART driver adds the byte
0x02, which marks an HCI data packet, so bytes cross the serial line. A USB driver adds no such byte, because USB carries HCI data on a separate endpoint, a numbered data channel of the device.The Controller takes the 7-byte L2CAP packet as its payload. It adds a 2-byte Link Layer header, a 4-byte message integrity §1.9 check (MIC) because the link is encrypted, and a 3-byte cyclic redundancy check §4.32 (CRC), which detects transmission errors. The radio sends a 1-byte preamble and a 4-byte access address first. On the air the packet has bytes, which is bits and takes 168 microseconds at the basic BLE rate of 1 megabit per second.
The sensor answers 0B 5A: the operation code 0x0B of a Read Response, then the value 0x5A, which is , meaning 90%. Each layer removes its header on the way up, and ReadValue returns one byte, 90. The tool btmon, which reads the kernel’s HCI layer, shows the 11 bytes of step 4 and the reply. It cannot show the Link Layer header, the MIC or any resending of step 6: those exist only inside the Controller and on the air, where only a radio sniffer, a separate receiver, records them.
Stated precisely
In the standard configuration of a mainline Linux kernel with BlueZ 5, the work is placed exactly as listed above: the Link Layer and the radio in the Controller; the drivers, HCI, L2CAP, SMP, the keys in use and the enforcement of each socket’s security level in the kernel; and ATT, GATT, the profiles, the stored keys, the trust policy, the pairing dialogue and the D-Bus interface in bluetoothd.
Each reported success covers only the work completed before it. A reply to ReadValue means that the peer’s ATT server answered. WriteValue takes an option named type. With type set to request, ATT sends a Write Request, and the reply follows the peer’s Write Response, which says that the peer’s ATT server accepted the bytes, not that the peer’s application acted on them. With type set to command, ATT sends a Write Command, which has no response, and the reply says only that bluetoothd accepted the bytes for sending.
Limits and common mistakes
The placement holds for the standard configuration, not for every system. A program with administrator rights can open an HCI user channel, which gives it sole, raw access to one Controller. That program then runs a complete Host of its own in user space, and neither the kernel’s L2CAP and SMP code nor bluetoothd takes part for that Controller.
Three kinds of authority are separate. The D-Bus policy and the kernel’s socket permissions decide which local programs may use Bluetooth. Pairing and encryption protect the radio link and show that the peer holds the agreed keys. Neither decides whether a value that a paired peer sends is one the application should accept; the application must make that third decision itself.
A decision in a higher layer comes after the work of the lower layers. By the time bluetoothd learns of a new connection, the Controller has completed it and the kernel has created state for it, and refusing the connection then cannot undo that use of resources.
Features depend on three versions at once: the kernel, BlueZ and the Controller firmware. Check the installed versions, not the newest documentation, and confirm the behavior with btmon.
Questions answered
Which work occurs in kernel protocol code versus userspace services?
In Linux, the kernel half of BlueZ does the protocol work that every packet passes through, and the user-space daemon bluetoothd does the higher protocols together with all decisions, storage and interfaces for applications. The kernel contains the drivers that move HCI (Host Controller Interface) packets to and from the Bluetooth radio chip, called the Controller; the HCI core, which sends commands, receives events and tracks the Controller’s free buffers; L2CAP (Logical Link Control and Adaptation Protocol), which carries several channels over one connection; and SMP (Security Manager Protocol), which runs pairing on Bluetooth Low Energy, holds the keys in use and enforces the security level that each socket requires. bluetoothd runs ATT (Attribute Protocol) and GATT (Generic Attribute Profile), the profiles, the store of known devices and their keys, the trust policy and the pairing dialogue, and offers these services to applications over D-Bus. The Controller does the radio and Link Layer work, including the encryption of packets.
To find the placement for one operation, run btmon, which shows the HCI packets and management messages in the kernel, and busctl monitor, which shows the D-Bus calls to bluetoothd. A program that opens an HCI user channel bypasses this split and runs its own Host in user space.
Related entries: D-Bus (§12.22), BLE Controller and Host (§7.4), Pairing (§7.17), Channel confidentiality versus object authenticity (§7.34), Host Controller Interface (HCI) (§7.5), L2CAP (§7.15), Security Manager Protocol (SMP) (§7.19), ATT (§7.9), GATT (§7.10). Further reading: [91, 92, 93, 123].