Full name: Logical Link Control and Adaptation Protocol.
Definition
L2CAP (Logical Link Control and Adaptation Protocol) is the Bluetooth protocol layer that lets several higher protocols share one connection. Every L2CAP message starts with a 4-byte header that names a channel, and the receiver hands the message to the protocol or application that owns that channel. L2CAP also adapts sizes, carrying messages larger than one radio packet, and on some channels it paces the sender. In BLE §7.1 (Bluetooth Low Energy), L2CAP is part of the host, the protocol software above the radio controller.
The idea
A phone is connected to a medical sensor. Over that one connection the phone reads values with ATT §7.9 (Attribute Protocol), the sensor starts pairing §7.17 with the Security Manager Protocol §7.19, the sensor asks for a longer connection interval §7.7 to save power, and an application streams a 50 KiB log file. The radio delivers a single stream of packets. Something must tell the receiver which packet belongs to which conversation.
L2CAP does this much as port numbers do on the Internet. Each packet carries a channel identifier, and each protocol or application receives on its own channel. Some channels are fixed by the specification and exist as soon as the connection does. Others are opened on demand: the opening device names the service it wants, and the two devices agree on a pair of channel identifiers for the new conversation. The analogy needs one refinement. The number that names the service plays the part of a well-known port number, whereas a channel identifier names one end of one open channel, on one device, for as long as that channel exists.
How it works
The header. Every L2CAP message, which the specification calls a PDU (protocol data unit), begins with a 2-byte length, the number of bytes after the header, and a 2-byte CID (channel identifier), which names the channel at the receiver.
Fixed channels. On an LE connection three CIDs are fixed:
0x0004carries ATT;0x0005is the signaling channel, which carries L2CAP’s own control messages; and0x0006carries the Security Manager Protocol. These channels exist for the whole connection and need no setup.Naming a service. An application that wants a channel of its own registers a server on a PSM (protocol/service multiplexer), a number that names a service; on LE it is a 2-byte number called the SPSM (simplified PSM). Values from
0x0001to0x007Fare assigned to defined protocols by the Bluetooth SIG (Special Interest Group), the body that publishes the Bluetooth specifications. Values from0x0080to0x00FFare chosen by the device, and a client learns them from the server’s GATT §7.10 (Generic Attribute Profile) database.Opening a channel. The initiating device sends a connection request on the signaling channel. The request names the SPSM and the initiator’s own CID for the new channel, and it states three limits for traffic toward the initiator: the MTU §7.14 (maximum transmission unit), the largest message it accepts on the channel; the MPS (maximum PDU payload size), the largest payload of a single PDU it accepts; and its initial credits, the number of PDUs the other side may send before it must wait. The responder checks that a server is registered on the SPSM, that the link meets that server’s security requirement (for example, encryption with keys from authenticated pairing), and that it has resources to spare. It answers with its own CID, MTU, MPS and credits, and with a result: success, or a refusal such as “SPSM not supported”, “no resources available” or “insufficient encryption”.
Using the channel. After success, each side sends to the other side’s CID. Each device takes the CIDs of such channels from the range
0x0040to0x007F, so one device can hold at most 64 of them on one LE connection. Messages on the channel are split, paced and joined again as the LE credit-based flow control §7.16 entry describes.Closing. Either side may close the channel at any time with a disconnection request that names both CIDs, and the other side answers with a disconnection response. The request carries no reason. Both CIDs then become free for reuse.
Below the channels, an L2CAP message longer than one radio packet is split into several Link Layer §7.6 packets and joined again at the receiver, as the MTU and fragmentation entry describes. The signaling channel also carries a request with which the peripheral, the device that accepted the connection, asks the central, the device that initiated it, for new connection parameters.
Before an application reads a single byte from a channel, the stack, the protocol software of the device, has already spent memory on it: the channel’s state (its CIDs, limits and credit counters), buffers that hold received PDUs, and a buffer for a message that is still being joined. Every limit that protects that memory must be enforced where the memory is committed, inside the stack, not later in the application.
Worked example
Take a hypothetical stack that keeps 60 bytes of state for each open channel and reserves for it a 512-byte buffer, the channel’s MTU, in which to join one message. One channel costs bytes before the application reads anything.
If the stack accepts at most 4 channels per connection, the worst case is bytes. If it accepts every request until its CID range runs out, the worst case is bytes, which is of the RAM of a device with 64 KiB, for a single connection. A peer reaches that case without breaking any rule for a single message: it opens 64 channels and on each one sends only the first PDU of a message that announces 512 bytes, then nothing more. Every message is within the MTU; the total is what exhausts memory. The protection is a set of totals, each enforced when the resource is granted: a maximum number of channels per connection and per device, a shared buffer pool of fixed size, credits granted only for buffers that exist, and a time limit after which an incomplete message is discarded and its buffer freed.
Stated precisely
Let be the largest number of channels opened on demand that the stack will hold at once, the bytes of state per channel, and the largest number of received bytes one channel may hold before the application reads them. The memory these channels can take before any application reads is at most , plus the buffers the stack shares among all channels, including the fixed ones. A limit per message, such as the MTU, bounds only; must be bounded separately. If the receiver grants a credit only when it has a free buffer for one more PDU, and reserves such buffers for a channel, then is at most times the MPS.
The contract of a channel opened on demand is: no data may flow on it before a successful connection response; each side must keep every message within the other side’s MTU and every PDU payload within the other side’s MPS; and after a disconnection, neither CID refers to the old channel any more.
Limits and common mistakes
A channel’s security check concerns the link, not the authority of whoever wrote the data. A server that requires encryption with authenticated keys learns that the peer device completed pairing and that the link is encrypted. It does not learn who wrote the data or what that author may do, and opening a different channel does not change who the peer is. Authority over content needs its own checks, such as a message authentication code §5.7 over the content and an authorization §1.7 decision.
A later check does not bound earlier work. A message authentication code over a complete 512-byte message can be verified only after the stack has stored all 512 bytes, so size and count limits must come first, at admission.
Refusing after accepting looks different to the peer. A refusal at setup returns a reason in the connection response. Once the channel is open, the only refusal is to close it: the peer sees a channel that opened successfully, then a disconnection request without a reason.
Fixed channels cannot be refused. ATT and Security Manager Protocol messages may arrive at any time during a connection, so the stack must bound the memory and work they cause as well.
On LE connections, L2CAP does not resend lost data; it relies on the Link Layer for that. The L2CAP retransmission §7.36 modes exist only on classic Bluetooth, BR/EDR (basic rate and enhanced data rate). The same exhaustion pattern one level up, with connections instead of channels, is the subject of the Connection-slot exhaustion §7.35 entry.
Questions answered
What resource limits apply before an application consumes channel data?
Before an application reads data from an L2CAP (Logical Link Control and Adaptation Protocol) channel, the protocol software has already spent memory on the channel, and these limits bound that memory:
the number of channels it will open, per connection and in total, because each open channel holds state;
the channel’s MTU (maximum transmission unit), the largest message the channel accepts, which bounds the buffer for joining one message;
the MPS (maximum PDU payload size), the largest payload of a single L2CAP packet, which bounds each received piece;
the credits granted under credit-based flow control, which bound how many pieces the peer may send before the receiver allows more;
the size of the buffer pool shared by all channels;
a time limit after which an incomplete message is discarded and its buffer freed.
Each limit must be enforced where the resource is committed, inside the protocol software. A limit per message is not enough on its own: a peer that keeps every message within the MTU can still exhaust memory by opening many channels and leaving each message incomplete. Checks on content, such as verifying a message authentication code, come later and cannot give back memory already spent.
Related entries: LE credit-based flow control (§7.16), Message authentication code (MAC) (§5.7), Bounded queue (§9.31), MTU and fragmentation (§7.14), Connection-slot exhaustion (§7.35), ATT (§7.9). Further reading: [92].