Debian Codegraph @

src/adv_ctrl.c

raw symbols SCIP
1 /*
2 * Copyright © 2024-2026 SipRadius LLC
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 *
7 * VSF TR-06-3:2024 — RIST Advanced Profile control message
8 * serialization and deserialization.
9 */
10
11 #include "proto/adv.h"
12 #include "rist-private.h"
13 #include "udp-private.h"
14 #include "transport-private.h"
15 #include "log-private.h"
16 #include "proto/rist_time.h"
17 #include "crypto/psk.h"
18 #include "cjson/cJSON.h"
19 #include <string.h>
20 #include <errno.h>
21
22 /* --------------------------------------------------------------------------
23 * All Advanced Profile control messages are sent as:
24 * Type = 4 (Control), E = 1 (Expedite), F = L = 1 (unfragmented),
25 * PSK = 0 (no encryption for now), LPC = 0 (no compression).
26 *
27 * The payload of a Type 4 packet is:
28 * | Control Index (16 bits) | Length (16 bits) |
29 * | ... body per CI ... |
30 * -------------------------------------------------------------------------- */
31
32 /* Build the outer Advanced Profile packet for a control message.
33 * ctrl_payload: serialized control body (CI + Length + data)
34 * ctrl_len: length of ctrl_payload
35 * Returns total packet length written into buf, or -1 on error. */
36 static int rist_adv_build_control(uint8_t *buf, uint32_t seq, uint32_t timestamp,
37 uint32_t ssrc, const uint8_t *ctrl_payload,
38 size_t ctrl_len)
39 {
40 struct rist_adv_params params;
41 memset(&params, 0, sizeof(params));
42 params.seq = seq;
43 params.timestamp = timestamp;
44 params.ssrc = ssrc;
45 params.enc_type = RIST_ADV_TYPE_CONTROL;
46 params.psk_mode = RIST_ADV_PSK_NONE;
47 params.lpc_mode = RIST_ADV_LPC_NONE;
48 params.first_frag = true;
49 params.last_frag = true;
50 params.expedite = true;
51 params.retransmit = false;
52
53 return rist_adv_build(buf, &params, ctrl_payload, ctrl_len);
54 }
55
56 /* --------------------------------------------------------------------------
57 * NACK Bitmask (CI=0x0000, Section 5.3.2)
58 *
59 * Body (repeatable 12-byte entries):
60 * | SSRC of Media Source (32) |
61 * | PSS — Packet Start Seq (32) |
62 * | BLP — Bitmask of Lost Packets (32) |
63 * -------------------------------------------------------------------------- */
64
65 int rist_adv_send_nack_bitmask(struct rist_peer *peer,
66 uint32_t media_ssrc,
67 uint32_t pss, uint32_t blp)
68 {
69 struct rist_common_ctx *ctx = get_cctx(peer);
70 uint8_t pkt[RIST_MAX_PACKET_SIZE];
71 uint8_t ctrl[4 + 12]; /* CI(2) + Len(2) + one 12-byte entry */
72 size_t off = 0;
73
74 /* Control header */
75 ctrl[off++] = (RIST_ADV_CI_NACK_BITMASK >> 8) & 0xFF;
76 ctrl[off++] = RIST_ADV_CI_NACK_BITMASK & 0xFF;
77 uint16_t body_len = 12;
78 ctrl[off++] = (body_len >> 8) & 0xFF;
79 ctrl[off++] = body_len & 0xFF;
80
81 /* SSRC of media source */
82 ctrl[off++] = (media_ssrc >> 24) & 0xFF;
83 ctrl[off++] = (media_ssrc >> 16) & 0xFF;
84 ctrl[off++] = (media_ssrc >> 8) & 0xFF;
85 ctrl[off++] = media_ssrc & 0xFF;
86
87 /* PSS */
88 ctrl[off++] = (pss >> 24) & 0xFF;
89 ctrl[off++] = (pss >> 16) & 0xFF;
90 ctrl[off++] = (pss >> 8) & 0xFF;
91 ctrl[off++] = pss & 0xFF;
92
93 /* BLP */
94 ctrl[off++] = (blp >> 24) & 0xFF;
95 ctrl[off++] = (blp >> 16) & 0xFF;
96 ctrl[off++] = (blp >> 8) & 0xFF;
97 ctrl[off++] = blp & 0xFF;
98
99 uint32_t seq = ctx->adv_seq_unprotected++;
100 uint32_t ts = (uint32_t)((timestampNTP_u64() * 1000000ULL) >> 16);
101 uint32_t ssrc = rist_adv_ssrc_unprotected(ctx->adv_ssrc_base);
102
103 int total = rist_adv_build_control(pkt, seq, ts, ssrc, ctrl, off);
104 if (total < 0)
105 return -1;
106
107 ssize_t ret = rist_transport_sendto(peer, pkt, (size_t)total, 0);
108 if (ret < 0)
109 _librist_log_send_error(peer, errno, (size_t)total, "adv-nack-bitmask sendto");
110
111 return (ret > 0) ? 0 : -1;
112 }
113
114 /* --------------------------------------------------------------------------
115 * NACK Range (CI=0x0001, Section 5.3.3)
116 *
117 * Body (repeatable 12-byte entries):
118 * | SSRC of Media Source (32) |
119 * | PSS — Packet Start Seq (32) |
120 * | NALP — Number of Additional Lost Packets (32) |
121 * -------------------------------------------------------------------------- */
122
123 int rist_adv_send_nack_range(struct rist_peer *peer,
124 uint32_t media_ssrc,
125 uint32_t pss, uint32_t nalp)
126 {
127 struct rist_common_ctx *ctx = get_cctx(peer);
128 uint8_t pkt[RIST_MAX_PACKET_SIZE];
129 uint8_t ctrl[4 + 12];
130 size_t off = 0;
131
132 ctrl[off++] = (RIST_ADV_CI_NACK_RANGE >> 8) & 0xFF;
133 ctrl[off++] = RIST_ADV_CI_NACK_RANGE & 0xFF;
134 uint16_t body_len = 12;
135 ctrl[off++] = (body_len >> 8) & 0xFF;
136 ctrl[off++] = body_len & 0xFF;
137
138 ctrl[off++] = (media_ssrc >> 24) & 0xFF;
139 ctrl[off++] = (media_ssrc >> 16) & 0xFF;
140 ctrl[off++] = (media_ssrc >> 8) & 0xFF;
141 ctrl[off++] = media_ssrc & 0xFF;
142
143 ctrl[off++] = (pss >> 24) & 0xFF;
144 ctrl[off++] = (pss >> 16) & 0xFF;
145 ctrl[off++] = (pss >> 8) & 0xFF;
146 ctrl[off++] = pss & 0xFF;
147
148 ctrl[off++] = (nalp >> 24) & 0xFF;
149 ctrl[off++] = (nalp >> 16) & 0xFF;
150 ctrl[off++] = (nalp >> 8) & 0xFF;
151 ctrl[off++] = nalp & 0xFF;
152
153 uint32_t seq = ctx->adv_seq_unprotected++;
154 uint32_t ts = (uint32_t)((timestampNTP_u64() * 1000000ULL) >> 16);
155 uint32_t ssrc = rist_adv_ssrc_unprotected(ctx->adv_ssrc_base);
156
157 int total = rist_adv_build_control(pkt, seq, ts, ssrc, ctrl, off);
158 if (total < 0)
159 return -1;
160
161 ssize_t ret = rist_transport_sendto(peer, pkt, (size_t)total, 0);
162 if (ret < 0)
163 _librist_log_send_error(peer, errno, (size_t)total, "adv-nack-range sendto");
164
165 return (ret > 0) ? 0 : -1;
166 }
167
168 /* --------------------------------------------------------------------------
169 * RTT Echo Request (CI=0x0010, Section 5.3.4)
170 *
171 * Body:
172 * | Requester SSRC (32) |
173 * | Timestamp MSW (32) |
174 * | Timestamp LSW (32) |
175 * | Processing Delay (32, microseconds) |
176 * | Padding (n * 32 bits) |
177 * -------------------------------------------------------------------------- */
178
179 int rist_adv_send_rtt_echo_request(struct rist_peer *peer)
180 {
181 struct rist_common_ctx *ctx = get_cctx(peer);
182 uint8_t pkt[RIST_MAX_PACKET_SIZE];
183 uint8_t ctrl[4 + 16]; /* CI(2) + Len(2) + 4 words */
184 size_t off = 0;
185
186 ctrl[off++] = (RIST_ADV_CI_RTT_ECHO_REQ >> 8) & 0xFF;
187 ctrl[off++] = RIST_ADV_CI_RTT_ECHO_REQ & 0xFF;
188 uint16_t body_len = 16;
189 ctrl[off++] = (body_len >> 8) & 0xFF;
190 ctrl[off++] = body_len & 0xFF;
191
192 uint32_t req_ssrc = rist_adv_ssrc_unprotected(ctx->adv_ssrc_base);
193 ctrl[off++] = (req_ssrc >> 24) & 0xFF;
194 ctrl[off++] = (req_ssrc >> 16) & 0xFF;
195 ctrl[off++] = (req_ssrc >> 8) & 0xFF;
196 ctrl[off++] = req_ssrc & 0xFF;
197
198 uint64_t ntp = timestampNTP_u64();
199 uint32_t msw = (uint32_t)(ntp >> 32);
200 uint32_t lsw = (uint32_t)(ntp & 0xFFFFFFFF);
201
202 ctrl[off++] = (msw >> 24) & 0xFF;
203 ctrl[off++] = (msw >> 16) & 0xFF;
204 ctrl[off++] = (msw >> 8) & 0xFF;
205 ctrl[off++] = msw & 0xFF;
206
207 ctrl[off++] = (lsw >> 24) & 0xFF;
208 ctrl[off++] = (lsw >> 16) & 0xFF;
209 ctrl[off++] = (lsw >> 8) & 0xFF;
210 ctrl[off++] = lsw & 0xFF;
211
212 /* Processing delay = 0 for request */
213 ctrl[off++] = 0; ctrl[off++] = 0; ctrl[off++] = 0; ctrl[off++] = 0;
214
215 uint32_t seq = ctx->adv_seq_unprotected++;
216 uint32_t ts = (uint32_t)((ntp * 1000000ULL) >> 16);
217
218 int total = rist_adv_build_control(pkt, seq, ts, req_ssrc, ctrl, off);
219 if (total < 0)
220 return -1;
221
222 ssize_t ret = rist_transport_sendto(peer, pkt, (size_t)total, 0);
223 if (ret < 0)
224 _librist_log_send_error(peer, errno, (size_t)total, "adv-rtt-echo-req sendto");
225
226 return (ret > 0) ? 0 : -1;
227 }
228
229 /* --------------------------------------------------------------------------
230 * RTT Echo Response (CI=0x0011, Section 5.3.4)
231 *
232 * Mirror the request body, fill in processing delay.
233 * -------------------------------------------------------------------------- */
234
235 int rist_adv_send_rtt_echo_response(struct rist_peer *peer,
236 uint32_t requester_ssrc,
237 uint32_t orig_msw, uint32_t orig_lsw,
238 uint32_t processing_delay_us)
239 {
240 struct rist_common_ctx *ctx = get_cctx(peer);
241 uint8_t pkt[RIST_MAX_PACKET_SIZE];
242 uint8_t ctrl[4 + 16];
243 size_t off = 0;
244
245 ctrl[off++] = (RIST_ADV_CI_RTT_ECHO_RESP >> 8) & 0xFF;
246 ctrl[off++] = RIST_ADV_CI_RTT_ECHO_RESP & 0xFF;
247 uint16_t body_len = 16;
248 ctrl[off++] = (body_len >> 8) & 0xFF;
249 ctrl[off++] = body_len & 0xFF;
250
251 ctrl[off++] = (requester_ssrc >> 24) & 0xFF;
252 ctrl[off++] = (requester_ssrc >> 16) & 0xFF;
253 ctrl[off++] = (requester_ssrc >> 8) & 0xFF;
254 ctrl[off++] = requester_ssrc & 0xFF;
255
256 ctrl[off++] = (orig_msw >> 24) & 0xFF;
257 ctrl[off++] = (orig_msw >> 16) & 0xFF;
258 ctrl[off++] = (orig_msw >> 8) & 0xFF;
259 ctrl[off++] = orig_msw & 0xFF;
260
261 ctrl[off++] = (orig_lsw >> 24) & 0xFF;
262 ctrl[off++] = (orig_lsw >> 16) & 0xFF;
263 ctrl[off++] = (orig_lsw >> 8) & 0xFF;
264 ctrl[off++] = orig_lsw & 0xFF;
265
266 ctrl[off++] = (processing_delay_us >> 24) & 0xFF;
267 ctrl[off++] = (processing_delay_us >> 16) & 0xFF;
268 ctrl[off++] = (processing_delay_us >> 8) & 0xFF;
269 ctrl[off++] = processing_delay_us & 0xFF;
270
271 uint32_t seq = ctx->adv_seq_unprotected++;
272 uint64_t ntp = timestampNTP_u64();
273 uint32_t ts = (uint32_t)((ntp * 1000000ULL) >> 16);
274 uint32_t ssrc = rist_adv_ssrc_unprotected(ctx->adv_ssrc_base);
275
276 int total = rist_adv_build_control(pkt, seq, ts, ssrc, ctrl, off);
277 if (total < 0)
278 return -1;
279
280 ssize_t ret = rist_transport_sendto(peer, pkt, (size_t)total, 0);
281 if (ret < 0)
282 _librist_log_send_error(peer, errno, (size_t)total, "adv-rtt-echo-resp sendto");
283
284 return (ret > 0) ? 0 : -1;
285 }
286
287 /* --------------------------------------------------------------------------
288 * Keep-Alive (CI=0x8000, Section 5.3.6)
289 *
290 * Wraps Main Profile keep-alive format with additional capability bits:
291 * C (bit 29): Compression capable
292 * G (bit 30): GRE key rotation capable
293 * I (bit 31): Advanced Profile capable
294 *
295 * For now we send a minimal keep-alive with the I bit set.
296 * -------------------------------------------------------------------------- */
297
298 #define RIST_ADV_KEEPALIVE_CAP_I (1u << 31)
299 #define RIST_ADV_KEEPALIVE_CAP_G (1u << 30)
300 #define RIST_ADV_KEEPALIVE_CAP_C (1u << 29)
301
302 int rist_adv_send_keepalive(struct rist_peer *peer)
303 {
304 struct rist_common_ctx *ctx = get_cctx(peer);
305 uint8_t pkt[RIST_MAX_PACKET_SIZE];
306
307 /*
308 * Keep-alive body layout (Section 5.3.6):
309 * | MAC Address (6 bytes) |
310 * | Capabilities (4 bytes, with C/G/I bits) |
311 * | JSON (optional, variable) |
312 *
313 * We encode it as CI(2) + Len(2) + MAC(6) + Capabilities(4) = 14 body bytes
314 */
315 uint8_t ctrl[4 + 6 + 4];
316 size_t off = 0;
317
318 ctrl[off++] = (RIST_ADV_CI_KEEPALIVE >> 8) & 0xFF;
319 ctrl[off++] = RIST_ADV_CI_KEEPALIVE & 0xFF;
320 uint16_t body_len = 10; /* MAC(6) + Capabilities(4) */
321 ctrl[off++] = (body_len >> 8) & 0xFF;
322 ctrl[off++] = body_len & 0xFF;
323
324 /* MAC address from the peer struct */
325 memcpy(&ctrl[off], peer->mac_addr, 6);
326 off += 6;
327
328 /* Capabilities with I bit set to signal Advanced Profile support */
329 uint32_t caps = RIST_ADV_KEEPALIVE_CAP_I;
330 ctrl[off++] = (caps >> 24) & 0xFF;
331 ctrl[off++] = (caps >> 16) & 0xFF;
332 ctrl[off++] = (caps >> 8) & 0xFF;
333 ctrl[off++] = caps & 0xFF;
334
335 uint32_t seq = ctx->adv_seq_unprotected++;
336 uint64_t ntp = timestampNTP_u64();
337 uint32_t ts = (uint32_t)((ntp * 1000000ULL) >> 16);
338 uint32_t ssrc = rist_adv_ssrc_unprotected(ctx->adv_ssrc_base);
339
340 int total = rist_adv_build_control(pkt, seq, ts, ssrc, ctrl, off);
341 if (total < 0)
342 return -1;
343
344 ssize_t ret = rist_transport_sendto(peer, pkt, (size_t)total, 0);
345 if (ret < 0)
346 _librist_log_send_error(peer, errno, (size_t)total, "adv-keepalive sendto");
347
348 return (ret > 0) ? 0 : -1;
349 }
350
351 /* --------------------------------------------------------------------------
352 * Control Message Unsupported Response (CI=0x8020, Section 5.3.10)
353 *
354 * Body:
355 * | Responder SSRC (32) |
356 * | Incoming CI (16) | Reserved (16) |
357 * | First 48 bits of incoming payload (48) | Padding (16) |
358 * -------------------------------------------------------------------------- */
359
360 int rist_adv_send_unsupported(struct rist_peer *peer,
361 uint16_t incoming_ci,
362 const uint8_t *incoming_payload,
363 size_t incoming_len)
364 {
365 struct rist_common_ctx *ctx = get_cctx(peer);
366 uint8_t pkt[RIST_MAX_PACKET_SIZE];
367 uint8_t ctrl[4 + 16];
368 size_t off = 0;
369
370 ctrl[off++] = (RIST_ADV_CI_UNSUPPORTED >> 8) & 0xFF;
371 ctrl[off++] = RIST_ADV_CI_UNSUPPORTED & 0xFF;
372 uint16_t body_len = 12;
373 ctrl[off++] = (body_len >> 8) & 0xFF;
374 ctrl[off++] = body_len & 0xFF;
375
376 uint32_t resp_ssrc = rist_adv_ssrc_unprotected(ctx->adv_ssrc_base);
377 ctrl[off++] = (resp_ssrc >> 24) & 0xFF;
378 ctrl[off++] = (resp_ssrc >> 16) & 0xFF;
379 ctrl[off++] = (resp_ssrc >> 8) & 0xFF;
380 ctrl[off++] = resp_ssrc & 0xFF;
381
382 ctrl[off++] = (incoming_ci >> 8) & 0xFF;
383 ctrl[off++] = incoming_ci & 0xFF;
384 ctrl[off++] = 0; /* Reserved */
385 ctrl[off++] = 0;
386
387 /* Copy first 6 bytes (48 bits) of incoming payload, or pad with zeros */
388 size_t copy_len = incoming_len < 6 ? incoming_len : 6;
389 if (incoming_payload && copy_len > 0)
390 memcpy(&ctrl[off], incoming_payload, copy_len);
391 if (copy_len < 6)
392 memset(&ctrl[off + copy_len], 0, 6 - copy_len);
393 off += 6;
394 /* Padding to 32-bit boundary */
395 ctrl[off++] = 0;
396 ctrl[off++] = 0;
397
398 uint32_t seq = ctx->adv_seq_unprotected++;
399 uint64_t ntp = timestampNTP_u64();
400 uint32_t ts = (uint32_t)((ntp * 1000000ULL) >> 16);
401
402 int total = rist_adv_build_control(pkt, seq, ts, resp_ssrc, ctrl, off);
403 if (total < 0)
404 return -1;
405
406 ssize_t ret = rist_transport_sendto(peer, pkt, (size_t)total, 0);
407 if (ret < 0)
408 _librist_log_send_error(peer, errno, (size_t)total, "adv-unsupported sendto");
409
410 return (ret > 0) ? 0 : -1;
411 }
412
413 /* --------------------------------------------------------------------------
414 * Type 8 — GRE-over-Advanced Profile (Section 5.2, Type=8)
415 *
416 * Wraps a complete Main Profile GRE packet inside an Advanced Profile
417 * envelope. The receiver strips the AP header and falls through to
418 * normal GRE processing. This enables rist2rist relay/bridge devices
419 * and is used during Section 9 transition.
420 * -------------------------------------------------------------------------- */
421
422 int rist_adv_send_type8(struct rist_peer *peer,
423 const uint8_t *gre_packet, size_t gre_len)
424 {
425 struct rist_common_ctx *ctx = get_cctx(peer);
426 uint8_t pkt[RIST_MAX_PACKET_SIZE + RIST_ADV_MAX_FIXED_HEADER];
427
428 if (gre_len > RIST_MAX_PACKET_SIZE)
429 return -1;
430
431 struct rist_adv_params params;
432 memset(&params, 0, sizeof(params));
433 params.seq = ctx->adv_seq_unprotected++;
434 params.timestamp = (uint32_t)((timestampNTP_u64() * 1000000ULL) >> 16);
435 params.ssrc = rist_adv_ssrc_unprotected(ctx->adv_ssrc_base);
436 params.enc_type = RIST_ADV_TYPE_GRE_MAIN;
437 params.psk_mode = RIST_ADV_PSK_NONE;
438 params.lpc_mode = RIST_ADV_LPC_NONE;
439 params.first_frag = true;
440 params.last_frag = true;
441 params.expedite = true;
442 params.retransmit = false;
443
444 int total = rist_adv_build(pkt, &params, gre_packet, gre_len);
445 if (total < 0)
446 return -1;
447
448 ssize_t ret = rist_transport_sendto(peer, pkt, (size_t)total, 0);
449 if (ret < 0)
450 _librist_log_send_error(peer, errno, (size_t)total, "adv-type8 sendto");
451
452 return (ret > 0) ? 0 : -1;
453 }
454
455 /* --------------------------------------------------------------------------
456 * PSK Future Nonce Announcement (CI=0x8011, Section 5.3.9)
457 *
458 * Body:
459 * | Future Nonce (4 bytes) |
460 * | Key Size (2 bytes) | Reserved (2 bytes) |
461 *
462 * Sent by the sender before rotating to a new nonce so the receiver
463 * can pre-derive the AES key (PBKDF2 is expensive) before the first
464 * data packet with the new nonce arrives.
465 * -------------------------------------------------------------------------- */
466
467 int rist_adv_send_psk_nonce(struct rist_peer *peer,
468 const uint8_t nonce[4], uint16_t key_size_bits)
469 {
470 struct rist_common_ctx *ctx = get_cctx(peer);
471 uint8_t pkt[RIST_MAX_PACKET_SIZE];
472 uint8_t ctrl[4 + 8]; /* CI(2) + Len(2) + Nonce(4) + KeySize(2) + Rsvd(2) */
473 size_t off = 0;
474
475 ctrl[off++] = (RIST_ADV_CI_PSK_NONCE >> 8) & 0xFF;
476 ctrl[off++] = RIST_ADV_CI_PSK_NONCE & 0xFF;
477 uint16_t body_len = 8;
478 ctrl[off++] = (body_len >> 8) & 0xFF;
479 ctrl[off++] = body_len & 0xFF;
480
481 memcpy(&ctrl[off], nonce, 4);
482 off += 4;
483
484 ctrl[off++] = (key_size_bits >> 8) & 0xFF;
485 ctrl[off++] = key_size_bits & 0xFF;
486 ctrl[off++] = 0; /* Reserved */
487 ctrl[off++] = 0;
488
489 uint32_t seq = ctx->adv_seq_unprotected++;
490 uint64_t ntp = timestampNTP_u64();
491 uint32_t ts = (uint32_t)((ntp * 1000000ULL) >> 16);
492 uint32_t ssrc = rist_adv_ssrc_unprotected(ctx->adv_ssrc_base);
493
494 int total = rist_adv_build_control(pkt, seq, ts, ssrc, ctrl, off);
495 if (total < 0)
496 return -1;
497
498 ssize_t ret = rist_transport_sendto(peer, pkt, (size_t)total, 0);
499 if (ret < 0)
500 _librist_log_send_error(peer, errno, (size_t)total, "adv-psk-nonce sendto");
501
502 return (ret > 0) ? 0 : -1;
503 }
504
505 /* --------------------------------------------------------------------------
506 * Flow Attribute (CI=0x8001, Section 5.3.7)
507 *
508 * Body: JSON object describing session and flow metadata.
509 * Sent periodically by the sender (~1 Hz).
510 *
511 * Minimal JSON schema:
512 * { "session": "<cname>",
513 * "flow_id": <outer>, "flow_inner": <inner>,
514 * "virt_dst_port": <port>, "profile": "advanced" }
515 * -------------------------------------------------------------------------- */
516
517 int rist_adv_send_flow_attr(struct rist_peer *peer)
518 {
519 struct rist_common_ctx *ctx = get_cctx(peer);
520 uint8_t pkt[RIST_MAX_PACKET_SIZE];
521
522 cJSON *root = cJSON_CreateObject();
523 if (!root)
524 return -1;
525
526 cJSON_AddStringToObject(root, "session", peer->cname);
527 cJSON_AddNumberToObject(root, "virt_dst_port", peer->config.virt_dst_port);
528 cJSON_AddStringToObject(root, "profile", "advanced");
529
530 uint16_t outer = peer->config.virt_dst_port;
531 uint16_t inner = 0;
532 cJSON_AddNumberToObject(root, "flow_id", outer);
533 cJSON_AddNumberToObject(root, "flow_inner", inner);
534
535 char *json = cJSON_PrintUnformatted(root);
536 cJSON_Delete(root);
537 if (!json)
538 return -1;
539
540 size_t json_len = strlen(json);
541 if (json_len > RIST_MAX_PACKET_SIZE - RIST_ADV_MAX_FIXED_HEADER - 4) {
542 free(json);
543 return -1;
544 }
545
546 uint8_t ctrl[4 + RIST_MAX_PACKET_SIZE];
547 size_t off = 0;
548
549 ctrl[off++] = (RIST_ADV_CI_FLOW_ATTR >> 8) & 0xFF;
550 ctrl[off++] = RIST_ADV_CI_FLOW_ATTR & 0xFF;
551 uint16_t body_len = (uint16_t)json_len;
552 ctrl[off++] = (body_len >> 8) & 0xFF;
553 ctrl[off++] = body_len & 0xFF;
554
555 memcpy(&ctrl[off], json, json_len);
556 off += json_len;
557 free(json);
558
559 uint32_t seq = ctx->adv_seq_unprotected++;
560 uint64_t ntp = timestampNTP_u64();
561 uint32_t ts = (uint32_t)((ntp * 1000000ULL) >> 16);
562 uint32_t ssrc = rist_adv_ssrc_unprotected(ctx->adv_ssrc_base);
563
564 int total = rist_adv_build_control(pkt, seq, ts, ssrc, ctrl, off);
565 if (total < 0)
566 return -1;
567
568 ssize_t ret = rist_transport_sendto(peer, pkt, (size_t)total, 0);
569 if (ret < 0)
570 _librist_log_send_error(peer, errno, (size_t)total, "adv-flow-attr sendto");
571
572 return (ret > 0) ? 0 : -1;
573 }
574
575 /* --------------------------------------------------------------------------
576 * Control message receive dispatcher
577 *
578 * Called from the Advanced Profile receive path when enc_type == CONTROL.
579 * Returns 0 on success, -1 on error/unknown CI.
580 * -------------------------------------------------------------------------- */
581
582 int rist_adv_recv_control(struct rist_peer *peer,
583 const uint8_t *ctrl_payload,
584 size_t ctrl_len)
585 {
586 struct rist_common_ctx *ctx = get_cctx(peer);
587
588 if (ctrl_len < RIST_ADV_CTRL_HDR_SIZE)
589 return -1;
590
591 uint16_t ci = (uint16_t)((ctrl_payload[0] << 8) | ctrl_payload[1]);
592 uint16_t body_len = (uint16_t)((ctrl_payload[2] << 8) | ctrl_payload[3]);
593
594 if ((size_t)4 + body_len > ctrl_len)
595 return -1;
596
597 const uint8_t *body = ctrl_payload + 4;
598
599 switch (ci) {
600 case RIST_ADV_CI_NACK_BITMASK: {
601 if (body_len < 12)
602 return -1;
603 uint32_t media_ssrc = (uint32_t)body[0] << 24 | body[1] << 16 | body[2] << 8 | body[3];
604 uint32_t pss = (uint32_t)body[4] << 24 | body[5] << 16 | body[6] << 8 | body[7];
605 uint32_t blp = (uint32_t)body[8] << 24 | body[9] << 16 | body[10] << 8 | body[11];
606 (void)media_ssrc;
607 rist_log_priv(ctx, RIST_LOG_DEBUG,
608 "Advanced NACK Bitmask received: PSS=%u BLP=0x%08x\n", pss, blp);
609
610 /* Enqueue retransmissions for pss and each bit set in blp */
611 if (peer->sender_ctx) {
612 struct rist_sender *sender = peer->sender_ctx;
613 rist_retry_enqueue(sender, pss, peer);
614 for (int i = 0; i < 32; i++) {
615 if (blp & (1u << i))
616 rist_retry_enqueue(sender, pss + 1 + (uint32_t)i, peer);
617 }
618 }
619 return 0;
620 }
621
622 case RIST_ADV_CI_NACK_RANGE: {
623 if (body_len < 12)
624 return -1;
625 uint32_t media_ssrc = (uint32_t)body[0] << 24 | body[1] << 16 | body[2] << 8 | body[3];
626 uint32_t pss = (uint32_t)body[4] << 24 | body[5] << 16 | body[6] << 8 | body[7];
627 uint32_t nalp = (uint32_t)body[8] << 24 | body[9] << 16 | body[10] << 8 | body[11];
628 (void)media_ssrc;
629 rist_log_priv(ctx, RIST_LOG_DEBUG,
630 "Advanced NACK Range received: PSS=%u NALP=%u\n", pss, nalp);
631
632 if (peer->sender_ctx) {
633 struct rist_sender *sender = peer->sender_ctx;
634 for (uint32_t i = 0; i <= nalp && i < 10000; i++)
635 rist_retry_enqueue(sender, pss + i, peer);
636 }
637 return 0;
638 }
639
640 case RIST_ADV_CI_RTT_ECHO_REQ: {
641 if (body_len < 16)
642 return -1;
643 uint32_t req_ssrc = (uint32_t)body[0] << 24 | body[1] << 16 | body[2] << 8 | body[3];
644 uint32_t orig_msw = (uint32_t)body[4] << 24 | body[5] << 16 | body[6] << 8 | body[7];
645 uint32_t orig_lsw = (uint32_t)body[8] << 24 | body[9] << 16 | body[10] << 8 | body[11];
646 /* processing_delay in the request is always 0 */
647 rist_log_priv(ctx, RIST_LOG_DEBUG, "Advanced RTT Echo Request from SSRC %u\n", req_ssrc);
648 return rist_adv_send_rtt_echo_response(peer, req_ssrc, orig_msw, orig_lsw, 0);
649 }
650
651 case RIST_ADV_CI_RTT_ECHO_RESP: {
652 if (body_len < 16)
653 return -1;
654 uint32_t req_ssrc = (uint32_t)body[0] << 24 | body[1] << 16 | body[2] << 8 | body[3];
655 uint32_t orig_msw = (uint32_t)body[4] << 24 | body[5] << 16 | body[6] << 8 | body[7];
656 uint32_t orig_lsw = (uint32_t)body[8] << 24 | body[9] << 16 | body[10] << 8 | body[11];
657 uint32_t proc_delay = (uint32_t)body[12] << 24 | body[13] << 16 | body[14] << 8 | body[15];
658 (void)req_ssrc;
659
660 uint64_t orig_ntp = ((uint64_t)orig_msw << 32) | orig_lsw;
661 peer->last_rtt = calculate_rtt_delay(orig_ntp, timestampNTP_u64(), proc_delay);
662 rist_log_priv(ctx, RIST_LOG_DEBUG,
663 "Advanced RTT Echo Response: RTT=%"PRIu64" us\n",
664 (peer->last_rtt * 1000000ULL) >> 32);
665 return 0;
666 }
667
668 case RIST_ADV_CI_KEEPALIVE: {
669 if (body_len < 10)
670 return -1;
671 /* Parse capabilities to detect I bit */
672 uint32_t caps = (uint32_t)body[6] << 24 | body[7] << 16 | body[8] << 8 | body[9];
673 peer->remote_supports_advanced = !!(caps & RIST_ADV_KEEPALIVE_CAP_I);
674 peer->last_pkt_received = timestampNTP_u64();
675 rist_log_priv(ctx, RIST_LOG_DEBUG,
676 "Advanced Keep-Alive: caps=0x%08x (I=%d)\n",
677 caps, peer->remote_supports_advanced);
678 return 0;
679 }
680
681 case RIST_ADV_CI_FLOW_ATTR: {
682 if (body_len == 0)
683 return 0;
684 rist_log_priv(ctx, RIST_LOG_DEBUG,
685 "Advanced Flow Attribute received (%u bytes)\n", body_len);
686 if (peer->receiver_ctx) {
687 struct rist_receiver *rcv = peer->receiver_ctx;
688 if (rcv->receiver_flow_attr_callback) {
689 char json_buf[RIST_MAX_PACKET_SIZE];
690 size_t copy_len = body_len < sizeof(json_buf) - 1 ? body_len : sizeof(json_buf) - 1;
691 memcpy(json_buf, body, copy_len);
692 json_buf[copy_len] = '\0';
693 rcv->receiver_flow_attr_callback(
694 rcv->receiver_flow_attr_callback_argument,
695 peer, json_buf, copy_len);
696 }
697 }
698 return 0;
699 }
700
701 case RIST_ADV_CI_PSK_NONCE: {
702 if (body_len < 8)
703 return -1;
704 uint8_t future_nonce[4];
705 memcpy(future_nonce, body, 4);
706 uint16_t key_bits = (uint16_t)((body[4] << 8) | body[5]);
707
708 bool odd = CHECK_BIT(future_nonce[0], 7);
709 struct rist_key *ak = odd ? &peer->key_rx_odd : &peer->key_rx;
710 if (ak->password_len > 0) {
711 pthread_mutex_lock(&peer->peer_lock);
712 _librist_crypto_psk_preannounce_nonce(ak, future_nonce, key_bits);
713 pthread_mutex_unlock(&peer->peer_lock);
714 }
715
716 rist_log_priv(ctx, RIST_LOG_DEBUG,
717 "Advanced PSK Future Nonce: nonce=0x%02x%02x%02x%02x key_bits=%u\n",
718 future_nonce[0], future_nonce[1], future_nonce[2], future_nonce[3],
719 key_bits);
720 return 0;
721 }
722
723 case RIST_ADV_CI_UNSUPPORTED: {
724 if (body_len < 8)
725 return -1;
726 uint16_t unsup_ci = (uint16_t)((body[4] << 8) | body[5]);
727 rist_log_priv(ctx, RIST_LOG_WARN,
728 "Remote does not support control message CI=0x%04x\n", unsup_ci);
729 return 0;
730 }
731
732 default:
733 rist_log_priv(ctx, RIST_LOG_DEBUG,
734 "Unknown Advanced Profile control CI=0x%04x, sending unsupported response\n", ci);
735 rist_adv_send_unsupported(peer, ci, body, body_len);
736 return -1;
737 }
738 }