Debian Codegraph @

src/proto/adv.h

raw symbols SCIP
1 /*
2 * Copyright © 2019-2020 SipRadius LLC
3 * Copyright © 2024-2026 SipRadius LLC
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-2-Clause
7 *
8 * VSF TR-06-3:2024 — RIST Advanced Profile wire format definitions.
9 */
10
11 #ifndef RIST_PROTO_ADV_H
12 #define RIST_PROTO_ADV_H
13
14 #include "common/attributes.h"
15 #include "proto/rtp.h"
16 #include <stdint.h>
17 #include <stdbool.h>
18 #include <stddef.h>
19
20 #ifdef _WIN32
21 #include <winsock2.h>
22 #else
23 #include <arpa/inet.h>
24 #endif
25
26 /*
27 * VSF TR-06-3 Advanced Profile top-level tunnel packet format (Figure 1).
28 *
29 * 0 1 2 3
30 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
31 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32 * |V=2|P|X| CC |M| PT=127 | Sequence Number (low 16) | RTP
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ (12 B)
34 * | Timestamp (1 MHz) |
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 * | SSRC identifier |
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 * : RTP Header Extension (if X=1) :
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 * | Seq Number Extension (16) |F|L|E|R|I|P|H|PSK |LPC| Type | Profile-
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ defined
42 * | Flow ID (if I=1, 4 bytes) | header
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * | PSK Hash (if present, 16 bytes) |
45 * | PSK Nonce (if present, 4 bytes) |
46 * | PSK IV (if present, 4 bytes) |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 * | Payload Compression (if LPC=11, 4 bytes) |
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 * | Payload Format Descriptor (if P=1, 4 bytes) |
51 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 * : RIST Header Extension (if H=1, variable) :
53 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 * | Payload ... |
55 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 */
57
58 /* --------------------------------------------------------------------------
59 * RTP constants for Advanced Profile
60 * -------------------------------------------------------------------------- */
61
62 /* PT=127 when SDP is not in use (Section 5.2.1) */
63 #define RIST_ADV_PT 127
64
65 /* Default timestamp frequency: 1 MHz (Section 5.2.1) */
66 #define RIST_ADV_CLOCK_HZ 1000000
67
68 /*
69 * Default RTP flags byte: V=2, P=0, X=0, CC=0 = 0x80.
70 * The profile-defined header (seq_ext + flags) is NOT an RFC 3550 header
71 * extension — it's part of the RTP "payload" interpreted by the Advanced
72 * Profile. X is only set when an actual RFC 3550 header extension is used.
73 */
74 #define RIST_ADV_RTP_FLAGS 0x80
75
76 /* --------------------------------------------------------------------------
77 * Profile-defined header: fixed extension (always present, 4 bytes)
78 * -------------------------------------------------------------------------- */
79
80 RIST_PACKED_STRUCT(rist_adv_ext, {
81 uint16_t seq_ext; /* Sequence Number Extension (high 16 bits) */
82 uint8_t flags; /* F|L|E|R|I|P|H|PSK[2] */
83 uint8_t params; /* PSK[1:0]|LPC[1:0]|Type[3:0] */
84 })
85
86 /* flags byte (byte 2 of the extension, bits 16-23 of line 5) */
87 #define RIST_ADV_FLAG_F 0x80 /* First fragment */
88 #define RIST_ADV_FLAG_L 0x40 /* Last fragment */
89 #define RIST_ADV_FLAG_E 0x20 /* Expedite */
90 #define RIST_ADV_FLAG_R 0x10 /* Retransmit */
91 #define RIST_ADV_FLAG_I 0x08 /* Flow ID present */
92 #define RIST_ADV_FLAG_P 0x04 /* Payload Format Descriptor present */
93 #define RIST_ADV_FLAG_H 0x02 /* RIST Header Extension present */
94 #define RIST_ADV_PSK2_MASK 0x01 /* PSK field bit 2 (MSB of 3-bit PSK) */
95
96 /* params byte (byte 3 of the extension, bits 24-31 of line 5) */
97 #define RIST_ADV_PSK10_SHIFT 6 /* PSK[1:0] in bits 7-6 */
98 #define RIST_ADV_PSK10_MASK 0xC0
99 #define RIST_ADV_LPC_SHIFT 4 /* LPC[1:0] in bits 5-4 */
100 #define RIST_ADV_LPC_MASK 0x30
101 #define RIST_ADV_TYPE_MASK 0x0F /* Type[3:0] in bits 3-0 */
102
103 /* --------------------------------------------------------------------------
104 * PSK mode extraction / insertion
105 *
106 * The 3-bit PSK field spans two bytes:
107 * PSK[2] = ext->flags bit 0
108 * PSK[1:0] = ext->params bits 7-6
109 * -------------------------------------------------------------------------- */
110
111 static inline uint8_t rist_adv_get_psk(const struct rist_adv_ext *ext)
112 {
113 return (uint8_t)(((ext->flags & RIST_ADV_PSK2_MASK) << 2) |
114 ((ext->params & RIST_ADV_PSK10_MASK) >> RIST_ADV_PSK10_SHIFT));
115 }
116
117 static inline void rist_adv_set_psk(struct rist_adv_ext *ext, uint8_t psk)
118 {
119 ext->flags = (ext->flags & ~RIST_ADV_PSK2_MASK) | ((psk >> 2) & 0x01);
120 ext->params = (ext->params & ~RIST_ADV_PSK10_MASK) |
121 (uint8_t)((psk & 0x03) << RIST_ADV_PSK10_SHIFT);
122 }
123
124 /* LPC extraction / insertion */
125 static inline uint8_t rist_adv_get_lpc(const struct rist_adv_ext *ext)
126 {
127 return (uint8_t)((ext->params & RIST_ADV_LPC_MASK) >> RIST_ADV_LPC_SHIFT);
128 }
129
130 static inline void rist_adv_set_lpc(struct rist_adv_ext *ext, uint8_t lpc)
131 {
132 ext->params = (ext->params & ~RIST_ADV_LPC_MASK) |
133 (uint8_t)((lpc & 0x03) << RIST_ADV_LPC_SHIFT);
134 }
135
136 /* Type extraction / insertion */
137 static inline uint8_t rist_adv_get_type(const struct rist_adv_ext *ext)
138 {
139 return ext->params & RIST_ADV_TYPE_MASK;
140 }
141
142 static inline void rist_adv_set_type(struct rist_adv_ext *ext, uint8_t type)
143 {
144 ext->params = (ext->params & ~RIST_ADV_TYPE_MASK) | (type & 0x0F);
145 }
146
147 /* --------------------------------------------------------------------------
148 * Encapsulation type constants (Type[3:0], Section 5.2.3)
149 * -------------------------------------------------------------------------- */
150 #define RIST_ADV_TYPE_RESERVED 0
151 #define RIST_ADV_TYPE_IPV4 1
152 #define RIST_ADV_TYPE_IPV6 2
153 #define RIST_ADV_TYPE_REDUCED_UDP 3
154 #define RIST_ADV_TYPE_CONTROL 4
155 #define RIST_ADV_TYPE_DIRECT 5
156 #define RIST_ADV_TYPE_LAYER2 6
157 #define RIST_ADV_TYPE_GRE_RFC2784 7
158 #define RIST_ADV_TYPE_GRE_MAIN 8
159
160 /* --------------------------------------------------------------------------
161 * PSK mode constants (PSK[2:0], Section 5.2.3)
162 * -------------------------------------------------------------------------- */
163 #define RIST_ADV_PSK_NONE 0 /* No Encryption */
164 #define RIST_ADV_PSK_AES_CTR 1 /* AES-CTR (Main Profile compat) */
165 #define RIST_ADV_PSK_HMAC_SHA256 2 /* HMAC-SHA256 (no encryption) */
166 #define RIST_ADV_PSK_AES_CTR_HMAC 3 /* AES-CTR-HMAC-SHA256 */
167 #define RIST_ADV_PSK_AES_GCM 4 /* AES-GCM */
168 #define RIST_ADV_PSK_CHACHA20_POLY1305 5 /* CHACHA20-POLY1305 */
169 #define RIST_ADV_PSK_USER_NOHASH 6 /* User-defined, no hash */
170 #define RIST_ADV_PSK_USER_HASH 7 /* User-defined, with hash */
171
172 /* --------------------------------------------------------------------------
173 * PSK header sizes per Table 1 (Section 5.2.3)
174 *
175 * Hash (16 B) + Nonce (4 B) + IV (4 B) — presence per mode.
176 * -------------------------------------------------------------------------- */
177
178 #define RIST_ADV_PSK_HASH_SIZE 16
179 #define RIST_ADV_PSK_NONCE_SIZE 4
180 #define RIST_ADV_PSK_IV_SIZE 4
181
182 static inline bool rist_adv_psk_has_hash(uint8_t psk)
183 {
184 return psk == 2 || psk == 3 || psk == 4 || psk == 5 || psk == 7;
185 }
186
187 static inline bool rist_adv_psk_has_nonce(uint8_t psk)
188 {
189 return psk >= 1;
190 }
191
192 static inline bool rist_adv_psk_has_iv(uint8_t psk)
193 {
194 return psk == 1 || psk >= 3;
195 }
196
197 static inline size_t rist_adv_psk_hdr_size(uint8_t psk)
198 {
199 size_t sz = 0;
200 if (rist_adv_psk_has_hash(psk)) sz += RIST_ADV_PSK_HASH_SIZE;
201 if (rist_adv_psk_has_nonce(psk)) sz += RIST_ADV_PSK_NONCE_SIZE;
202 if (rist_adv_psk_has_iv(psk)) sz += RIST_ADV_PSK_IV_SIZE;
203 return sz;
204 }
205
206 /* --------------------------------------------------------------------------
207 * LPC mode constants (LPC[1:0], Section 5.2.3)
208 * -------------------------------------------------------------------------- */
209 #define RIST_ADV_LPC_NONE 0 /* No Compression */
210 #define RIST_ADV_LPC_LZ4 1 /* LZ4 Compression */
211 #define RIST_ADV_LPC_RESERVED 2 /* Reserved */
212 #define RIST_ADV_LPC_FIELD_PRESENT 3 /* Payload Compression field present */
213
214 /* --------------------------------------------------------------------------
215 * Control Index values (Table 2, Section 5.3)
216 * -------------------------------------------------------------------------- */
217 #define RIST_ADV_CI_NACK_BITMASK 0x0000
218 #define RIST_ADV_CI_NACK_RANGE 0x0001
219 #define RIST_ADV_CI_RTT_ECHO_REQ 0x0010
220 #define RIST_ADV_CI_RTT_ECHO_RESP 0x0011
221 #define RIST_ADV_CI_FEC_2022_5_ROW 0x0020
222 #define RIST_ADV_CI_FEC_2022_5_COL 0x0021
223 #define RIST_ADV_CI_FEC_2022_1_ROW 0x0022
224 #define RIST_ADV_CI_FEC_2022_1_COL 0x0023
225 #define RIST_ADV_CI_KEEPALIVE 0x8000
226 #define RIST_ADV_CI_FLOW_ATTR 0x8001
227 #define RIST_ADV_CI_SRP_AUTH 0x8010
228 #define RIST_ADV_CI_PSK_NONCE 0x8011
229 #define RIST_ADV_CI_UNSUPPORTED 0x8020
230
231 /* --------------------------------------------------------------------------
232 * Flow ID field (4 bytes, Section 5.2.4, Figure 4)
233 * -------------------------------------------------------------------------- */
234
235 RIST_PACKED_STRUCT(rist_adv_flow_id, {
236 uint16_t outer; /* Outer Flow ID */
237 uint8_t inner_hi; /* Inner Flow ID bits [11:4] */
238 uint8_t inner_lo_sub; /* Inner Flow ID bits [3:0] | IFSID [3:0] */
239 })
240
241 static inline uint16_t rist_adv_flow_outer(const struct rist_adv_flow_id *f)
242 {
243 return ntohs(f->outer);
244 }
245
246 static inline uint16_t rist_adv_flow_inner(const struct rist_adv_flow_id *f)
247 {
248 return (uint16_t)((f->inner_hi << 4) | ((f->inner_lo_sub >> 4) & 0x0F));
249 }
250
251 static inline uint8_t rist_adv_flow_sub(const struct rist_adv_flow_id *f)
252 {
253 return f->inner_lo_sub & 0x0F;
254 }
255
256 /* --------------------------------------------------------------------------
257 * Payload Format Descriptor (4 bytes, Section 5.2.7, Figure 6)
258 * -------------------------------------------------------------------------- */
259
260 RIST_PACKED_STRUCT(rist_adv_pfd, {
261 uint32_t value; /* ID Type (4 bits) | ID Value (28 bits) */
262 })
263
264 static inline uint8_t rist_adv_pfd_id_type(const struct rist_adv_pfd *p)
265 {
266 return (uint8_t)(ntohl(p->value) >> 28);
267 }
268
269 static inline uint32_t rist_adv_pfd_id_value(const struct rist_adv_pfd *p)
270 {
271 return ntohl(p->value) & 0x0FFFFFFF;
272 }
273
274 /* --------------------------------------------------------------------------
275 * Header size constants
276 * -------------------------------------------------------------------------- */
277 #define RIST_ADV_RTP_SIZE 12 /* Standard RTP header */
278 #define RIST_ADV_EXT_SIZE 4 /* Profile-defined extension */
279 #define RIST_ADV_HEADER_MIN 16 /* RTP (12) + ext (4) */
280 #define RIST_ADV_FLOW_ID_SIZE 4
281 #define RIST_ADV_COMPRESSION_SIZE 4
282 #define RIST_ADV_PFD_SIZE 4
283
284 /* Maximum header: RTP(12) + ext(4) + flow_id(4) + hash(16) + nonce(4) + iv(4) + comp(4) + pfd(4) = 52
285 * (RIST Header Extension is variable-length on top of this) */
286 #define RIST_ADV_MAX_FIXED_HEADER 52
287
288 /* Control message sub-header */
289 #define RIST_ADV_CTRL_HDR_SIZE 4 /* Control Index (2) + Length (2) */
290
291 /* --------------------------------------------------------------------------
292 * 32-bit sequence number helpers
293 * -------------------------------------------------------------------------- */
294
295 static inline uint32_t rist_adv_seq32(const struct rist_rtp_hdr *rtp,
296 const struct rist_adv_ext *ext)
297 {
298 return ((uint32_t)ntohs(ext->seq_ext) << 16) | ntohs(rtp->seq);
299 }
300
301 static inline void rist_adv_set_seq32(struct rist_rtp_hdr *rtp,
302 struct rist_adv_ext *ext,
303 uint32_t seq)
304 {
305 rtp->seq = htons((uint16_t)(seq & 0xFFFF));
306 ext->seq_ext = htons((uint16_t)(seq >> 16));
307 }
308
309 /* --------------------------------------------------------------------------
310 * Fragment flag helpers
311 *
312 * F=1, L=1 -> unfragmented
313 * F=1, L=0 -> first fragment
314 * F=0, L=0 -> middle fragment
315 * F=0, L=1 -> last fragment
316 * -------------------------------------------------------------------------- */
317
318 static inline bool rist_adv_is_unfragmented(uint8_t flags)
319 {
320 return (flags & (RIST_ADV_FLAG_F | RIST_ADV_FLAG_L)) ==
321 (RIST_ADV_FLAG_F | RIST_ADV_FLAG_L);
322 }
323
324 static inline bool rist_adv_is_fragmented(uint8_t flags)
325 {
326 return !rist_adv_is_unfragmented(flags);
327 }
328
329 static inline bool rist_adv_is_first_fragment(uint8_t flags)
330 {
331 return (flags & RIST_ADV_FLAG_F) && !(flags & RIST_ADV_FLAG_L);
332 }
333
334 static inline bool rist_adv_is_last_fragment(uint8_t flags)
335 {
336 return !(flags & RIST_ADV_FLAG_F) && (flags & RIST_ADV_FLAG_L);
337 }
338
339 /* Sequence difference for wrap-around comparison */
340 static inline int32_t rist_adv_seq_diff(uint32_t a, uint32_t b)
341 {
342 return (int32_t)(a - b);
343 }
344
345 /* SSRC parity helpers (Section 5.2.1) */
346 static inline bool rist_adv_ssrc_is_protected(uint32_t ssrc)
347 {
348 return (ssrc & 1) == 0;
349 }
350
351 static inline uint32_t rist_adv_ssrc_protected(uint32_t ssrc_base)
352 {
353 return ssrc_base & ~(uint32_t)1;
354 }
355
356 static inline uint32_t rist_adv_ssrc_unprotected(uint32_t ssrc_base)
357 {
358 return ssrc_base | 1;
359 }
360
361 /* --------------------------------------------------------------------------
362 * Parse output structure
363 * -------------------------------------------------------------------------- */
364
365 struct rist_adv_parsed {
366 /* From RTP header */
367 uint32_t seq; /* Full 32-bit sequence number */
368 uint32_t timestamp; /* RTP timestamp (1 MHz clock) */
369 uint32_t ssrc;
370 bool rtp_ext_present; /* X bit in RTP header */
371 bool rtp_padding; /* P bit in RTP header */
372
373 /* From profile-defined extension */
374 bool first_frag; /* F bit */
375 bool last_frag; /* L bit */
376 bool expedite; /* E bit */
377 bool retransmit; /* R bit */
378 uint8_t psk_mode; /* PSK[2:0] (0-7) */
379 uint8_t lpc_mode; /* LPC[1:0] (0-3) */
380 uint8_t enc_type; /* Type[3:0] (0-15) */
381
382 /* Optional fields — pointers into original buffer (NULL if absent) */
383 bool has_flow_id; /* I flag */
384 const struct rist_adv_flow_id *flow_id;
385
386 bool has_psk; /* PSK mode > 0 */
387 const uint8_t *psk_hash; /* NULL if no hash for this mode */
388 const uint8_t *psk_nonce; /* NULL if no nonce */
389 const uint8_t *psk_iv; /* NULL if no IV */
390
391 bool has_compression; /* LPC == 3 */
392 const uint8_t *compression; /* 4-byte compression field */
393
394 bool has_pfd; /* P flag */
395 const struct rist_adv_pfd *pfd;
396
397 bool has_hdr_ext; /* H flag */
398 const uint8_t *hdr_ext; /* Points to RFC 3550-style extension header */
399 size_t hdr_ext_len; /* Total bytes of RIST header extension */
400
401 /* Payload */
402 const uint8_t *payload;
403 size_t payload_len;
404 };
405
406 /* --------------------------------------------------------------------------
407 * Build input structure
408 * -------------------------------------------------------------------------- */
409
410 struct rist_adv_params {
411 uint32_t seq;
412 uint32_t timestamp;
413 uint32_t ssrc;
414 uint8_t enc_type; /* Type[3:0] */
415 uint8_t psk_mode; /* PSK[2:0] */
416 uint8_t lpc_mode; /* LPC[1:0] */
417 bool first_frag;
418 bool last_frag;
419 bool expedite;
420 bool retransmit;
421
422 /* Optional fields — NULL/zero to omit */
423 const struct rist_adv_flow_id *flow_id;
424 const uint8_t *psk_hash;
425 const uint8_t *psk_nonce;
426 const uint8_t *psk_iv;
427 const uint8_t *compression; /* 4 bytes if LPC==3 */
428 const struct rist_adv_pfd *pfd;
429 const uint8_t *hdr_ext;
430 size_t hdr_ext_len;
431 };
432
433 /* --------------------------------------------------------------------------
434 * Function declarations
435 * -------------------------------------------------------------------------- */
436
437 /* Parse an Advanced Profile packet.
438 * buf: pointer to the start of the RTP header
439 * len: total packet length
440 * out: populated on success
441 * Returns 0 on success, -1 on malformed packet. */
442 RIST_PRIV int rist_adv_parse(const uint8_t *buf, size_t len,
443 struct rist_adv_parsed *out);
444
445 /* Build an Advanced Profile packet.
446 * buf: output buffer (caller ensures sufficient space)
447 * params: packet parameters
448 * payload: inner payload data (may be NULL if payload_len == 0)
449 * payload_len: inner payload length
450 * Returns total packet length on success, or -1 on error. */
451 RIST_PRIV int rist_adv_build(uint8_t *buf, const struct rist_adv_params *params,
452 const uint8_t *payload, size_t payload_len);
453
454 /* Compute the header size for given parameters (without payload). */
455 RIST_PRIV size_t rist_adv_header_size(const struct rist_adv_params *params);
456
457 /* --------------------------------------------------------------------------
458 * Control message API (adv_ctrl.c)
459 * -------------------------------------------------------------------------- */
460
461 /* Forward declaration for control message API */
462 struct rist_peer;
463
464 RIST_PRIV int rist_adv_send_nack_bitmask(struct rist_peer *peer,
465 uint32_t media_ssrc,
466 uint32_t pss, uint32_t blp);
467 RIST_PRIV int rist_adv_send_nack_range(struct rist_peer *peer,
468 uint32_t media_ssrc,
469 uint32_t pss, uint32_t nalp);
470 RIST_PRIV int rist_adv_send_rtt_echo_request(struct rist_peer *peer);
471 RIST_PRIV int rist_adv_send_rtt_echo_response(struct rist_peer *peer,
472 uint32_t requester_ssrc,
473 uint32_t orig_msw,
474 uint32_t orig_lsw,
475 uint32_t processing_delay_us);
476 RIST_PRIV int rist_adv_send_keepalive(struct rist_peer *peer);
477 RIST_PRIV int rist_adv_send_unsupported(struct rist_peer *peer,
478 uint16_t incoming_ci,
479 const uint8_t *incoming_payload,
480 size_t incoming_len);
481 RIST_PRIV int rist_adv_send_type8(struct rist_peer *peer,
482 const uint8_t *gre_packet,
483 size_t gre_len);
484 RIST_PRIV int rist_adv_send_psk_nonce(struct rist_peer *peer,
485 const uint8_t nonce[4],
486 uint16_t key_size_bits);
487 RIST_PRIV int rist_adv_send_flow_attr(struct rist_peer *peer);
488 RIST_PRIV int rist_adv_recv_control(struct rist_peer *peer,
489 const uint8_t *ctrl_payload,
490 size_t ctrl_len);
491
492 #endif /* RIST_PROTO_ADV_H */