6. The Usage of SCTP for Data Channels
6.1. SCTP Protocol Considerations
The DTLS encapsulation of SCTP packets as described in [RFC8261] MUST be used.
This SCTP stack and its upper layer MUST support the usage of multiple SCTP streams. A user message can be sent ordered or unordered and with partial or full reliability.
The following SCTP protocol extensions are required:
- The stream reconfiguration extension defined in [RFC6525] MUST be supported. It is used for closing channels.
- The dynamic address reconfiguration extension defined in [RFC5061] MUST be used to signal the support of the stream reset extension defined in [RFC6525]. Other features of [RFC5061] are OPTIONAL.
- The partial reliability extension defined in [RFC3758] MUST be supported. In addition to the timed reliability PR-SCTP policy defined in [RFC3758], the limited retransmission policy defined in [RFC7496] MUST be supported. Limiting the number of retransmissions to zero, combined with unordered delivery, provides a UDP-like service where each user message is sent exactly once and delivered in the order received.
The support for message interleaving as defined in [RFC8260] SHOULD be used.
6.2. SCTP Association Management
In the WebRTC context, the SCTP association will be set up when the two endpoints of the WebRTC PeerConnection agree on opening it, as negotiated by the JavaScript Session Establishment Protocol (JSEP), which is typically an exchange of the Session Description Protocol (SDP) [RFC8829]. It will use the DTLS connection selected via ICE, and typically this will be shared via BUNDLE or equivalent with DTLS connections used to key the SRTP media streams.
The number of streams negotiated during SCTP association setup SHOULD be 65535, which is the maximum number of streams that can be negotiated during the association setup.
SCTP supports two ways of terminating an SCTP association. The first method is a graceful one, where a procedure that ensures no messages are lost during the shutdown of the association is used. The second method is a non-graceful one, where one side can just abort the association.
Each SCTP endpoint continuously supervises the reachability of its peer by monitoring the number of retransmissions of user messages and test messages. In case of excessive retransmissions, the association is terminated in a non-graceful way.
If an SCTP association is closed in a graceful way, all of its data channels are closed. In case of a non-graceful teardown, all data channels are also closed, but an error indication SHOULD be provided if possible.
6.3. SCTP Streams
SCTP defines a stream as a unidirectional logical channel existing within an SCTP association to another SCTP endpoint. The streams are used to provide the notion of in-sequence delivery and for multiplexing. Each user message is sent on a particular stream, either ordered or unordered. Ordering is preserved only for ordered messages sent on the same stream.
6.4. Data Channel Definition
Data channels are defined such that their accompanying application-level API can closely mirror the API for WebSockets, which implies bidirectional streams of data and a textual field called 'label' used to identify the meaning of the data channel.
The realization of a data channel is a pair of one incoming stream and one outgoing SCTP stream having the same SCTP stream identifier. How these SCTP stream identifiers are selected is protocol and implementation dependent. This allows a bidirectional communication.
Additionally, each data channel has the following properties in each direction:
- reliable or unreliable message transmission: In case of unreliable transmissions, the same level of unreliability is used. Note that, in SCTP, this is a property of an SCTP user message and not of an SCTP stream.
- in-order or out-of-order message delivery for message sent: Note that, in SCTP, this is a property of an SCTP user message and not of an SCTP stream.
- a priority, which is a 2-byte unsigned integer: These priorities MUST be interpreted as weighted-fair-queuing scheduling priorities per the definition of the corresponding stream scheduler supporting interleaving in [RFC8260]. For use in WebRTC, the values used SHOULD be one of 128 ("below normal"), 256 ("normal"), 512 ("high"), or 1024 ("extra high").
- an optional label.
- an optional protocol.
Note that for a data channel being negotiated with the protocol specified in [RFC8832], all of the above properties are the same in both directions.
6.5. Opening a Data Channel
Data channels can be opened by using negotiation within the SCTP association (called in-band negotiation) or out-of-band negotiation. Out-of-band negotiation is defined as any method that results in an agreement as to the parameters of a channel and the creation thereof. The details are out of scope of this document. Applications using data channels need to use the negotiation methods consistently on both endpoints.
A simple protocol for in-band negotiation is specified in [RFC8832].
When one side wants to open a channel using out-of-band negotiation, it picks a stream. Unless otherwise defined or negotiated, the streams are picked based on the DTLS role (the client picks even stream identifiers, and the server picks odd stream identifiers). However, the application is responsible for avoiding collisions with existing streams. If it attempts to reuse a stream that is part of an existing data channel, the addition MUST fail. In addition to choosing a stream, the application SHOULD also determine the options to be used for sending messages. The application MUST ensure in an application-specific manner that the application at the peer will also know the selected stream to be used, as well as the options for sending data from that side.
6.6. Transferring User Data on a Data Channel
All data sent on a data channel in both directions MUST be sent over the underlying stream using the reliability defined when the data channel was opened, unless the options are changed or per-message options are specified by a higher level.
The message orientation of SCTP is used to preserve the message boundaries of user messages. Therefore, senders MUST NOT put more than one application message into an SCTP user message. Unless the deprecated PPID-based fragmentation and reassembly is used, the sender MUST include exactly one application message in each SCTP user message.
The SCTP Payload Protocol Identifiers (PPIDs) are used to signal the interpretation of the "payload data". The following PPIDs MUST be used (see Section 8):
- WebRTC String: to identify a non-empty JavaScript string encoded in UTF-8.
- WebRTC String Empty: to identify an empty JavaScript string encoded in UTF-8.
- WebRTC Binary: to identify non-empty JavaScript binary data (ArrayBuffer, ArrayBufferView, or Blob).
- WebRTC Binary Empty: to identify empty JavaScript binary data (ArrayBuffer, ArrayBufferView, or Blob).
SCTP does not support the sending of empty user messages. Therefore, if an empty message has to be sent, the appropriate PPID (WebRTC String Empty or WebRTC Binary Empty) is used, and the SCTP user message of one zero byte is sent. When receiving an SCTP user message with one of these PPIDs, the receiver MUST ignore the SCTP user message and process it as an empty message.
The usage of the PPIDs "WebRTC String Partial" and "WebRTC Binary Partial" is deprecated. They were used for a PPID-based fragmentation and reassembly of user messages belonging to reliable and ordered data channels.
If a message with an unsupported PPID is received or some error condition related to the received message is detected by the receiver (for example, illegal ordering), the receiver SHOULD close the corresponding data channel. This implies in particular that extensions using additional PPIDs can't be used without prior negotiation.
The SCTP base protocol specified in [RFC4960] does not support the interleaving of user messages. Therefore, sending a large user message can monopolize the SCTP association. To overcome this limitation, [RFC8260] defines an extension to support message interleaving, which SHOULD be used. As long as message interleaving is not supported, the sender SHOULD limit the maximum message size to 16 KB to avoid monopolization.
It is recommended that the message size be kept within certain size bounds, as applications will not be able to support arbitrarily large single messages. This limit has to be negotiated, for example, by using [RFC8841].
The sender SHOULD disable the Nagle algorithm (see [RFC1122]) to minimize the latency.
6.7. Closing a Data Channel
Closing of a data channel MUST be signaled by resetting the corresponding outgoing streams [RFC6525]. This means that if one side decides to close the data channel, it resets the corresponding outgoing stream. When the peer sees that an incoming stream was reset, it also resets its corresponding outgoing stream. Once this is completed, the data channel is closed. Resetting a stream sets the Stream Sequence Numbers (SSNs) of the stream back to 'zero' with a corresponding notification to the application layer that the reset has been performed. Streams are available for reuse after a reset has been performed.
[RFC6525] also guarantees that all the messages are delivered (or abandoned) before the stream is reset.