Section 7.6

Link Layer

Definition

The Link Layer is the part of a Bluetooth Low Energy (BLE) §7.1 device that runs the radio protocol: it decides when the device transmits and listens, on which radio channel, and what happens to each packet that arrives. It sits in the Controller, the lower part of a BLE device that contains the radio, below the Host, the upper part that runs the higher protocols and the applications. It sets up connections, schedules every exchange, and checks, acknowledges, resends and encrypts packets. The Bluetooth Core Specification defines it.

The idea

Radio is unreliable: interference corrupts or destroys packets, a packet can arrive twice, and many other devices share the frequencies. A battery-powered device also cannot keep its receiver on, so the two ends of a link must agree exactly when they will next talk. During a connection, a device must start its reply a fixed time, normally 150 microseconds, after the other device’s packet ends, far too soon for application software to take part. The Link Layer does this work inside the Controller and offers the Host a simpler service: for each connection, intact data packets in their original order without duplicates, plus notices such as “connection lost”.

A mailroom is a fair picture. It signs for envelopes, throws away damaged ones and duplicate copies, and asks senders to send again; only intact, new envelopes go upstairs, and the office never learns what was thrown away. The picture fails in one respect: the mailroom’s signature, like a Link Layer acknowledgment, means only that the mailroom has the envelope, not that anyone upstairs has read it. So by the time any Host software, and therefore any application policy code, sees an event, the Link Layer has already made decisions about it and spent radio time, energy and memory on it.

How it works

Channels and states. BLE divides the 2.4-gigahertz band into 40 radio channels: 3 for advertising, the short broadcasts with which devices announce themselves and set up connections, and 37 for the data of connections. The specification describes the Link Layer as a state machine §8.33 whose main states are Standby (idle), Advertising (sending advertisements), Scanning (listening for them), Initiating (sending a connection request) and Connection. In a connection, the device that sent the request is the central and the other device is the peripheral.

The packet. On the 1-megabit and 2-megabit radio modes, a packet carries a preamble of 1 or 2 bytes respectively, which lets the receiver lock on to the signal; a 4-byte access address, which on a connection is a random value chosen for that connection; a 2-byte header; a payload of up to 251 bytes; on an encrypted connection, if the payload is not empty, a 4-byte MIC (message integrity §1.9 check), a tag computed with the session key that reveals a change to the payload; and a 3-byte CRC §4.32 (cyclic redundancy check), an error-detecting code computed over header, payload and MIC. The header holds, among other fields, a length field, which counts the bytes of the payload and of the MIC when one is present, a 1-bit sequence number SN, a 1-bit next expected sequence number NESN, a more-data bit, and a field LLID that marks the payload as a Link Layer control message or as Host data. The MIC also covers the payload length and LLID, but not SN, NESN or the more-data bit.

Decisions on each received packet, in this order, before any Host software sees it:

  1. Heard or not. Both devices compute the channel of each connection event, the burst of exchanges that starts once every connection interval §7.7, from a shared hopping rule. A packet sent at another time or on another channel is never received.

  2. Access address. A packet with another link’s access address is ignored.

  3. CRC. A packet whose CRC does not match is discarded and not acknowledged, so the sender sends it again.

  4. New or repeated. If SN is not the sequence number expected next, the packet is a repeat and is discarded. A new packet is accepted unless the receive buffer is full; then it is left unacknowledged and the sender must resend it later, which is the Link Layer’s flow control.

  5. Its own last packet. NESN tells the receiver whether its own last packet arrived; if not, the receiver sends that packet again.

  6. Decryption. On an encrypted connection, a packet that fails the MIC check makes the Link Layer consider the connection lost at once. Its Host learns only that the connection was terminated because of a MIC failure, and the peer, which is not told, notices only through its supervision timeout §7.8.

  7. Control or data. The Link Layer handles control messages itself: changes of connection interval, of the channels in use or of radio mode, the start of encryption, the exchange of supported features, and termination. Only data goes up to the Host.

  8. Supervision. Each valid packet restarts a timer; if none arrives for the supervision timeout, the Link Layer declares the connection lost.

Other decisions. Before a connection exists, the Host can tell an advertising device to drop scan and connection requests from addresses that are not on its Filter Accept List, the specification’s name for a list of allowed devices; the Host never hears of the dropped requests. A few decisions need the Host: when encryption starts, the peripheral’s Controller asks its Host, through the HCI §7.5 (Host Controller Interface), for the long-term key §7.28 from an earlier pairing §7.17, and the Host supplies it or refuses. Above the Link Layer, L2CAP §7.15 (Logical Link Control and Adaptation Protocol) joins the pieces of each message and checks its length, and ATT §7.9 (Attribute Protocol) checks that the addressed attribute exists and that the link’s security satisfies its permissions. Only then does GATT §7.10 (Generic Attribute Profile) call the application’s handler.

Worked example

A central C and a peripheral P have just connected. Each keeps two 1-bit counters, both 0 at the start: ss, the sequence number of the packet it is trying to deliver, and ee, the sequence number it expects next. Every packet carries its sender’s ss as SN and its sender’s ee as NESN. For a packet that passes the CRC check, the receiver applies two rules. If SN equals its own ee, the packet is new: accept it and flip ee. If NESN differs from its own ss, its own packet was delivered: flip ss and move on to a new packet; otherwise send the current packet (again).

  1. C sends packet A: SN 0, NESN 0. At P, SN 0 equals P’s e=0e = 0, so P accepts A and sets e=1e = 1. NESN 0 equals P’s s=0s = 0, so P sends its current packet, an empty one.

  2. P sends SN 0, NESN 1. At C, NESN 1 differs from C’s s=0s = 0: A was delivered, and C sets s=1s = 1. SN 0 equals C’s e=0e = 0, so C sets e=1e = 1.

  3. C sends packet B: SN 1, NESN 1. Interference corrupts it, P’s CRC check fails, and P discards B and changes nothing.

  4. P must still answer the central. Unable to read B’s NESN, P sends its empty packet again: SN 0, NESN 1. At C, NESN 1 equals C’s s=1s = 1, so B was not delivered. SN 0 differs from C’s e=1e = 1, so the empty packet is a repeat, and C discards it.

  5. C sends B again, unchanged. At P the CRC matches; SN 1 equals P’s e=1e = 1, so P accepts B and sets e=0e = 0; NESN 1 differs from P’s s=0s = 0, so P sets s=1s = 1.

P’s Host received A and B once each and never learned that B was corrupted. The radio paid for the repair. Suppose B is an unencrypted packet with a 27-byte payload, sent on the 1-megabit radio mode (1 million bits per second). It is 1+4+2+27+3=371 + 4 + 2 + 27 + 3 = 37 bytes, or 37×8=29637 \times 8 = 296 bits, which take 296 μ\mus. B cost 2×296=5922 \times 296 = 592 μ\mus of air time for one delivery, and the repeated empty packet of 1+4+2+3=101 + 4 + 2 + 3 = 10 bytes another 80 μ\mus, all invisible above the Link Layer.

Stated precisely

For one connection, the Link Layer gives the Host this contract, provided both Link Layers follow the specification:

Limits and common mistakes

A Link Layer acknowledgment is not an application acknowledgment. The peer’s Host or application may still discard or reject the packet, and the peer may lose power before acting on it.

Application policy code cannot see what the Link Layer dropped or handled itself: corrupted packets, repeats, filtered requests, and control procedures such as a change of connection interval. A policy enforced in an application handler covers only the operations that reach that handler.

Rejection is not free. A packet that the application rejects has already used air time, energy, buffers and processor time, so a peer whose every request is refused can still consume these resources, a form of denial of service §1.28.

The CRC is not a security check. The access address and the starting value of the CRC travel unencrypted in the connection request, so an eavesdropper on that request can forge packets that pass both checks. Only encryption with a MIC stops this, and only for packets with a payload: an empty packet carries no MIC, so a forged empty packet is still accepted.

The Link Layer does not decide who the peer is. Pairing, run by the Security Manager Protocol §7.19 in the Host, creates the keys, and the Link Layer uses whatever key the Host supplies. Nor can the Link Layer stop jamming, deliberate radio noise; resending and the supervision timeout only cope with it.

The exact split of decisions between Controller and Host software varies between products. The order given here follows the specification.

Questions answered

Which decisions happen before higher-layer policy code sees an event?

Before higher-layer policy code sees an event on a Bluetooth Low Energy (BLE) connection, the Link Layer in the Controller has already decided whether the packet was heard, whether it belongs to this connection, whether it arrived intact, whether it is new or a repeat, whether to acknowledge it or have it sent again, whether it decrypts with a valid integrity tag, and whether it is a control message that the Link Layer handles itself, such as a change of connection timing. Before the connection existed, the Link Layer decided whether to accept the connection request, and throughout the connection it decides when the link counts as lost.

Next, the Host’s L2CAP (Logical Link Control and Adaptation Protocol) layer reassembles and length-checks each message, and its ATT (Attribute Protocol) layer checks that the addressed attribute exists and that the link’s security meets the attribute’s permissions. Any of these steps can drop a packet, end the connection, or spend radio time, energy and memory without the policy code learning of it, so a policy enforced at the application layer covers only the operations that reach it.

Related entries: BLE Controller and Host (§7.4), Cross-layer security (§7.38), Complete mediation (§1.16), Supervision timeout (§7.8), Retransmission and duplicate delivery (§7.36). Further reading: [93].