Section 7.36

Retransmission and duplicate delivery

Definition

Retransmission is sending data again because the sender has not learned that an earlier copy arrived. Duplicate delivery is what happens when the earlier copy did arrive: the receiver gets the same message more than once. A receiver that treats each copy as a new request performs the request twice. A retry is told apart from a new operation by an operation identifier, a value that the sender assigns once to each operation and repeats in every copy, and that the receiver remembers after performing the operation.

The idea

An updater sends a device the request “increase the stored counter by 1”. The device performs it and sends back an acknowledgment, a short reply that confirms the request was handled. The acknowledgment is lost, or the connection drops just before it arrives. To the updater, a lost request and a lost acknowledgment look the same: silence. No protocol removes this doubt, because an acknowledgment can be lost too, and so can an acknowledgment of the acknowledgment. The updater can only give up or send the request again. If the first copy had arrived, a device that does not recognize the second copy increases the counter twice.

The key is to count two things separately: deliveries, the number of copies that reach the receiver, and effects, the number of times the receiver performs the operation. Retransmission makes delivery reliable by allowing extra deliveries, which is called at-least-once delivery. The receiver must turn at-least-once delivery into an exactly-once effect §8.32: every operation it accepts is performed once, however many copies arrive. A numbered bank check is a fair picture. A bank pays each check number once. A second copy of the same check is refused, and a new check with a new number is paid even if the amount is the same. The picture holds only while the bank remembers which numbers it has paid. A bank that forgets after a fire may pay the same check twice, and a device that forgets after a restart may perform the same operation twice.

How it works

In BLE §7.1 (Bluetooth Low Energy), retransmission happens at more than one layer, and each layer detects duplicates only within its own scope.

Only the application knows what one operation is, so the application must tell retries from new operations. A common design has five steps.

  1. Assign an identifier. When the sender creates an operation, it assigns an operation identifier, for example the next value of a counter or a random 128-bit number, and stores the identifier with the operation. Every retry, including one sent after a reconnection or after the sender restarts, carries the same identifier and the same content. A new operation gets a new identifier.

  2. Protect it. The identifier is inside the authenticated part of the message, the bytes over which a message authentication code §5.7 (a short value that only a holder of the secret key can compute) is checked. An attacker therefore cannot change the identifier without detection.

  3. Keep a record. The receiver keeps a record of completed operations. Each entry holds the identifier, a digest of the content (a short fixed-length value computed from the content by a cryptographic hash function §5.1), and the result the receiver returned.

  4. Decide. For each arriving operation, the receiver looks up the identifier.

    • Not in the record: a new operation. Perform it, then record it.

    • In the record with the same digest: a retry. Do not perform it again. Send back the recorded result, so that the sender can finish.

    • In the record with a different digest: an error or an attack, never a retry. Reject it.

  5. Commit the effect and the record together. The effect and its record entry are written in one atomic step, a step that after a power loss has either happened completely or not at all. If the device could restart after performing the effect but before recording it, a retry after the restart would be performed again. The Exactly-once effect entry treats this step in detail.

A compact record is common. Each sender numbers its operations 1, 2, 3 and so on, and sends a new one only after the previous one is acknowledged. The receiver then stores only the highest completed number and its result. An arriving number equal to the stored one is a retry, a smaller one is old and is rejected, and a higher one is a new operation. This is the same state that a receiver keeps against a replay attack §1.26, in which an attacker records a genuine message and sends it again later. The receiver does not need to know whether a copy came from the honest sender or from an attacker: in both cases the operation is performed at most once.

A second approach makes the operation idempotent §8.31, meaning that performing it twice has the same effect as performing it once. “Set the counter to 6” is idempotent; “increase the counter by 1” is not. Idempotent operations make duplicates harmless without a record of identifiers. They still need ordering: a delayed copy of an old “set the counter to 6” that arrives after a newer “set the counter to 7” sets the counter back. A version number that may only increase prevents this.

Worked example

A device stores a counter with the value 5. The updater sends operation X, “increase the counter by 1”, with identifier 41; let hh be the digest of its content. The device performs X, so the counter becomes 5+1=65 + 1 = 6. It records the entry (41, hh, result 6) and replies. The reply is lost. The updater reconnects and sends X again with identifier 41.

Stated precisely

For an operation xx, let d(x)d(x) be the number of copies of xx that reach the receiver, and e(x)e(x) the number of times the receiver performs the effect of xx. Retransmission makes d(x)≥1d(x) \ge 1 as long as the sender keeps retrying and the network does not lose every copy, and it also allows d(x)>1d(x) > 1. The receiver’s goal is e(x)=1e(x) = 1 for every operation it accepts, whatever the value of d(x)≥1d(x) \ge 1, and e(x)=0e(x) = 0 for every operation it rejects.

Let ii be the identifier and hh the content digest of an arriving operation, and let RR be the receiver’s record, a set of entries of the form (identifier, digest, result). The receiver’s rule is:

The rule gives e(x)=1e(x) = 1 for every accepted operation when four assumptions hold.

  1. The sender never reuses an identifier for a different operation, including after its own restart.

  2. The identifier and the content are inside the authenticated part of the message.

  3. The record survives every failure that the effect survives: if the effect is stored in flash memory, so is the record entry, written in the same atomic step.

  4. The record keeps each identifier for as long as a retry of that operation can still arrive.

Limits and common mistakes

Reliable delivery at one layer does not give an exactly-once effect. A Link Layer acknowledgment says that the peer’s Link Layer received a packet. It does not say that the application performed anything.

A new connection does not mean a new operation, and a retry flag in the message does not mean a retry. A sender sets such a flag also when the first copy was lost, and an attacker can set it at will.

Identical bytes do not mean a duplicate, and a known identifier does not mean a duplicate either. Two genuine operations can have the same content, as identifiers 41 and 42 do in the example, and a reused identifier with new content is not a retry. The identifier and the content digest together decide.

Recording the identifier before performing the effect is wrong as well. If the effect then fails, every retry is answered “done” although nothing happened. Record the identifier together with the result, in the same atomic step as the effect.

The sender must store an operation’s identifier before sending it the first time. A sender that restarts and creates a new identifier for the same operation turns a retry into a new operation.

An effect outside the device, such as a motor movement or a payment made by another system, cannot be made atomic with a local record. An exactly-once effect there needs the other system’s cooperation, for example an identifier that the other system itself checks.

A record cannot grow without bound on a small device. A per-sender sequence number keeps it to one entry per sender, at the cost of allowing each sender only one unacknowledged operation at a time.

Questions answered

How is a retry distinguished from a new update operation?

A retry carries the same operation identifier and the same content as the original operation, and a new operation carries a new identifier. The sender assigns the identifier once, when it creates the operation, stores it, and repeats it in every retry, including retries after a reconnection or a restart. The receiver keeps a record of completed operations, each entry holding the identifier, a digest of the content and the result, written in the same atomic step as the operation’s effect. An arriving identifier that is not in the record marks a new operation, which the receiver performs. An identifier in the record with the same digest marks a retry: the receiver returns the stored result without performing the operation again. The same identifier with a different digest is rejected. The identifier must be inside the authenticated part of the message, so that no one can change it undetected.

Nothing below the application can make this distinction. Duplicate detection in the Link Layer, the part of a Bluetooth Low Energy chip that controls the radio and its packets, ends with the connection; a new connection does not mean a new operation; and two genuine operations can carry identical bytes. An idempotent operation, one whose repetition has the same effect as doing it once, such as “set the version to 7”, makes the distinction unnecessary for its effect, but it still needs ordering so that an old copy cannot undo a newer operation.

Related entries: Idempotency (§8.31), Exactly-once effect (§8.32), Replay attack (§1.26), Write request versus write command (§7.13), Protocol state machine (§7.37). Further reading: [91, 93].