blob: 6ad62c8d8bb74ab00055f62ff154b9f6e34ad36a [file] [log] [blame]
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001/*
2 * (C) 2021 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
3 * All rights not specifically granted under this license are reserved.
4 *
5 * Author: Pau Espin Pedrol
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Affero General Public License as published by the
9 * Free Software Foundation; either version 3 of the License, or (at your
10 * option) any later version.
11 */
12
13#include <stdint.h>
14
15#include <osmocom/core/byteswap.h>
16
17#include <osmocom/gsm/iuup.h>
18
19#include <osmocom/netif/rtp.h>
20#include <osmocom/netif/amr.h>
21
22#include <osmocom/mgcp/mgcp_conn.h>
23#include <osmocom/mgcp/mgcp_iuup.h>
24#include <osmocom/mgcp/mgcp_endp.h>
25#include <osmocom/mgcp/mgcp_codec.h>
26#include <osmocom/mgcp/mgcp_network.h>
27#include <osmocom/mgcp/debug.h>
28
29#define MGW_IUUP_MSGB_SIZE 4096
30
31static const struct osmo_iuup_rnl_config def_configure_req = {
32 .transparent = false,
33 .active = true,
34 .supported_versions_mask = 0x0003,
35 .num_rfci = 0,
36 .num_subflows = 0,
37 .IPTIs_present = false,
38 .t_init = { .t_ms = IUUP_TIMER_INIT_T_DEFAULT, .n_max = IUUP_TIMER_INIT_N_DEFAULT },
39 .t_ta = { .t_ms = IUUP_TIMER_TA_T_DEFAULT, .n_max = IUUP_TIMER_TA_N_DEFAULT },
40 .t_rc = { .t_ms = IUUP_TIMER_RC_T_DEFAULT, .n_max = IUUP_TIMER_RC_N_DEFAULT },
41};
42
43/* Find a destination connection. */
44static struct mgcp_conn *_find_dst_conn(struct mgcp_conn *conn)
45{
46 /* NOTE: This code path runs every time an RTP packet is received. The
47 * function mgcp_find_dst_conn() we use to determine the detination
48 * connection will iterate the connection list inside the endpoint.
49 * Since list iterations are quite costly, we will figure out the
50 * destination only once and use the optional private data pointer of
51 * the connection to cache the destination connection pointer. */
52
53 struct mgcp_conn *conn_dst;
54 if (!conn->priv) {
55 conn_dst = mgcp_find_dst_conn(conn);
56 conn->priv = conn_dst;
57 } else {
58 conn_dst = (struct mgcp_conn *)conn->priv;
59 }
60 return conn_dst;
61}
62
63/* Find RFCI containing all 0 sizes, -1 if not found. irp is an Initialization.ind prim */
64static int _find_rfci_no_data(struct osmo_iuup_rnl_prim *irp)
65{
66 int i;
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +020067 uint8_t rfci_cnt = 0;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +010068 /* Find RFCI containing NO_DATA: */
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +020069 for (i = 0; i < ARRAY_SIZE(irp->u.status.u.initialization.rfci); i++) {
70 struct osmo_iuup_rfci *rfci = &irp->u.status.u.initialization.rfci[i];
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +010071 int j;
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +020072 bool is_no_data;
73 if (!rfci->used)
74 continue;
75 rfci_cnt++;
76
77 is_no_data = true;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +010078 for (j = 0; j < irp->u.status.u.initialization.num_subflows; j++) {
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +020079 if (rfci->subflow_sizes[j]) {
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +010080 is_no_data = false;
81 break;
82 }
83 }
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +020084 if (is_no_data)
85 return rfci->id;
86
87 /* early loop termination: */
88 if (rfci_cnt == irp->u.status.u.initialization.num_subflows)
89 break;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +010090 }
91 return -1;
92}
93
94/* Lookup RFCI to use for specific AMR codec type. -1 if none found */
95static int8_t _conn_iuup_amr_ft_2_rfci(struct mgcp_conn_rtp *conn_rtp, uint8_t ft)
96{
97 int8_t i;
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +020098 uint8_t rfci_cnt = 0;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +010099 unsigned match_bytes = (unsigned)osmo_amr_bytes(ft);
100 struct osmo_iuup_rnl_prim *irp = conn_rtp->iuup.init_ind;
Neels Hofmeyrd8c2f312022-10-24 00:40:56 +0200101 if (!irp) {
102 /* No IuUP Initialization has occured on the IuUP side yet. Return error and drop the RTP data, until
103 * the IuUP Initialization has configured the link. */
104 return -1;
105 }
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100106
107 /* TODO: cache this somehow */
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200108 for (i = 0; i < ARRAY_SIZE(irp->u.status.u.initialization.rfci); i++) {
109 struct osmo_iuup_rfci *rfci = &irp->u.status.u.initialization.rfci[i];
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100110 int j;
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200111 unsigned num_bits;
112 if (!rfci->used)
113 continue;
114 rfci_cnt++;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100115
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200116 num_bits = 0;
117 for (j = 0; j < irp->u.status.u.initialization.num_subflows; j++)
118 num_bits += rfci->subflow_sizes[j];
119 if (match_bytes == (num_bits + 7)/8)
120 return rfci->id;
121
122 /* early loop termination: */
123 if (rfci_cnt == irp->u.status.u.initialization.num_subflows)
124 break;
125 }
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100126 return -1;
127}
128
129/* Helper function to configure IuUP layer FSM as Init-Passive, based on default config */
130static int _conn_iuup_configure_as_passive(struct mgcp_conn_rtp *conn_rtp)
131{
132 struct osmo_iuup_rnl_prim *irp;
133 int rc;
134
135 conn_rtp->iuup.active_init = false;
136
137 /* Tx CONFIG.req */
138 irp = osmo_iuup_rnl_prim_alloc(conn_rtp->conn, OSMO_IUUP_RNL_CONFIG, PRIM_OP_REQUEST, MGW_IUUP_MSGB_SIZE);
139 irp->u.config = def_configure_req;
140 irp->u.config.active = conn_rtp->iuup.active_init;
141 if ((rc = osmo_iuup_rnl_prim_down(conn_rtp->iuup.iui, irp)) == 0)
142 conn_rtp->iuup.configured = true;
143 else
144 LOG_CONN_RTP(conn_rtp, LOGL_ERROR, "Failed configuring IuUP layer\n");
145 return rc;
146}
147
148/* Helper function to configure IuUP layer FSM as Init-Active, based on received
149 * RNL Status-Init primitive from the sister IuUP connection we will bridge to. */
150static int _conn_iuup_configure_as_active(struct mgcp_conn_rtp *conn_rtp, struct osmo_iuup_rnl_prim *init_ind)
151{
152 struct osmo_iuup_rnl_prim *irp = init_ind;
153 struct osmo_iuup_rnl_prim *irp2;
154 struct msgb *msg;
155 bool prev_output_enabled;
156 int rc;
157
158 conn_rtp->iuup.active_init = true;
159
160 /* Find RFCI containing NO_DATA: */
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200161 conn_rtp->iuup.rfci_id_no_data = _find_rfci_no_data(init_ind);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100162
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200163 /* Copy over the rfci_id_no_data, since we reuse the same subflow set: */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100164 msg = msgb_copy_c(conn_rtp->conn, irp->oph.msg, "iuup-init-copy");
165 conn_rtp->iuup.init_ind = (struct osmo_iuup_rnl_prim *)msgb_data(msg);
166 conn_rtp->iuup.init_ind->oph.msg = msg;
167
168 /* Tx CONFIG.req */
169 irp2 = osmo_iuup_rnl_prim_alloc(conn_rtp->conn, OSMO_IUUP_RNL_CONFIG, PRIM_OP_REQUEST, MGW_IUUP_MSGB_SIZE);
170 irp2->u.config.transparent = false;
171 irp2->u.config.active = conn_rtp->iuup.active_init;
172 irp2->u.config.data_pdu_type = irp->u.status.u.initialization.data_pdu_type;
173 irp2->u.config.supported_versions_mask = def_configure_req.supported_versions_mask;
174 irp2->u.config.num_rfci = irp->u.status.u.initialization.num_rfci;
175 irp2->u.config.num_subflows = irp->u.status.u.initialization.num_subflows;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100176 irp2->u.config.IPTIs_present = irp->u.status.u.initialization.IPTIs_present;
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200177 memcpy(irp2->u.config.rfci, irp->u.status.u.initialization.rfci, sizeof(irp2->u.config.rfci));
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100178 irp2->u.config.t_init = def_configure_req.t_init;
179 irp2->u.config.t_ta = def_configure_req.t_ta;
180 irp2->u.config.t_rc = def_configure_req.t_rc;
181
182 /* We need to force allowance of RTP containing Init-ACK back: */
183 prev_output_enabled = conn_rtp->end.output_enabled;
184 conn_rtp->end.output_enabled = true;
185
186 if ((rc = osmo_iuup_rnl_prim_down(conn_rtp->iuup.iui, irp2)) == 0)
187 conn_rtp->iuup.configured = true;
188 else
189 LOG_CONN_RTP(conn_rtp, LOGL_ERROR, "Failed configuring IuUP layer\n");
190
191 conn_rtp->end.output_enabled = prev_output_enabled;
192 return rc;
193}
194
195/* Helper function to push an RTP+IuUP pkt up to the IuUP layer FSM through the
196 * TNL primitive interface. */
197static int _conn_iuup_rtp_pl_up(struct mgcp_conn_rtp *conn_rtp, struct msgb *msg)
198{
199 /* Send RTP payload (IuUP) up the stack: */
200 struct osmo_iuup_tnl_prim *itp;
201 int rc;
202
203 msg->l2h = msgb_data(msg) + sizeof(struct rtp_hdr);
204
205 itp = osmo_iuup_tnl_prim_alloc(conn_rtp->conn, OSMO_IUUP_TNL_UNITDATA, PRIM_OP_INDICATION, MGW_IUUP_MSGB_SIZE);
206 itp->oph.msg->l2h = msgb_put(itp->oph.msg, msgb_l2len(msg));
207 memcpy(itp->oph.msg->l2h, msgb_l2(msg), msgb_l2len(msg));
208 if ((rc = osmo_iuup_tnl_prim_up(conn_rtp->iuup.iui, itp)) != 0) {
209 LOG_CONN_RTP(conn_rtp, LOGL_ERROR, "Failed passing IuUP-Init to IuUP layer\n");
210 }
211 return rc;
212}
213
214static int check_rtp_iuup(const struct mgcp_conn_rtp *conn_rtp, struct msgb *msg)
215{
216 size_t min_size = sizeof(struct rtp_hdr);
217 /* Check there's at least 2 bytes of RTP payload (IuUP header). This is
218 ** mainly to avoid 0-byte payload copy cases */
219 if (msgb_length(msg) < sizeof(struct rtp_hdr) + 2) {
220 LOG_CONN_RTP(conn_rtp, LOGL_ERROR, "RTP-IuUP packet too short (%u < %zu)\n",
221 msgb_length(msg), min_size);
222 return -1;
223 }
224 return 0;
225}
226
227/* Bridge received IuUP packet in conn_rtp_src to conn_rtp_dst, an IuUP sister
228 * conn in the endpoint. The function takes ownsership of the irp */
229static int bridge_iuup_to_iuup_peer(struct mgcp_conn_rtp *conn_rtp_src, struct mgcp_conn_rtp *conn_rtp_dst, struct osmo_iuup_rnl_prim *irp)
230{
231 int rc;
232
233 /* If we are not configured and we received bridged data, it means
234 * conn_rtp_src is already configured and INITed, and we can infer
235 * conn_rtp_src is Init-passive (RNC side), so conn_rtp_dst needs to be
236 * configured as INIT-active: */
237 if (!conn_rtp_dst->iuup.configured) {
238 OSMO_ASSERT(conn_rtp_src->iuup.init_ind);
239 rc = _conn_iuup_configure_as_active(conn_rtp_dst, conn_rtp_src->iuup.init_ind);
240 if (rc < 0) {
241 msgb_free(irp->oph.msg);
242 return rc;
243 }
244 }
245
246 /* We simply forward the msg, without freeing it: */
247 talloc_steal(conn_rtp_dst->conn, irp->oph.msg);
248 irp->oph.operation = PRIM_OP_REQUEST;
249 if ((rc = osmo_iuup_rnl_prim_down(conn_rtp_dst->iuup.iui, irp)) != 0)
250 LOG_CONN_RTP(conn_rtp_dst, LOGL_ERROR, "Failed Tx data down to IuUP layer\n");
251 return rc;
252}
253
254/* Bridge received IuUP packet in conn_rtp_src to conn_rtp_dst, an RTP (no IuUP)
255 * sister conn in the endpoint. The function takes ownsership of the irp */
256static int bridge_iuup_to_rtp_peer(struct mgcp_conn_rtp *conn_rtp_src, struct mgcp_conn_rtp *conn_rtp_dst, struct osmo_iuup_rnl_prim *irp)
257{
258 /* FIXME: We probably need transcoding here?! Or at least look up AMR modes and translate to related RFCI */
259 uint8_t frame_nr = irp->u.data.frame_nr;
260 uint8_t fqc = irp->u.data.fqc;
261 struct msgb *msg = irp->oph.msg;
262 ssize_t amr_length = 0;
263 int ft;
264 uint8_t *amr_data;
265 struct rtp_hdr *rtp_hdr;
266 struct amr_hdr *amr_hdr;
267 int rc;
268
269 ft = osmo_amr_bytes_to_ft(msgb_l3len(msg));
270 if (ft < 0) {
271 LOGPCONN(conn_rtp_src->conn, DRTP, LOGL_ERROR,
272 "Unknown AMR format for size %u\n", msgb_l3len(msg));
273 msgb_free(msg);
274 return ft;
275 }
276 msgb_pull_to_l3(msg);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100277
278 if (mgcp_codec_amr_is_octet_aligned(conn_rtp_dst->end.codec)) {
Neels Hofmeyr5e6661e2022-10-24 01:34:36 +0200279 LOGP(DLMGCP, LOGL_DEBUG, "Convert IuUP -> AMR OA: ft %d, len %d\n", ft, msgb_length(msg));
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100280 amr_hdr = (struct amr_hdr *) msgb_push(msg, sizeof(struct amr_hdr));
281 amr_hdr->cmr = 15; /* no change */
282 amr_hdr->f = 0;
283 amr_hdr->q = !fqc;
284 amr_hdr->ft = ft & 0xff;
285 amr_hdr->pad1 = 0;
286 amr_hdr->pad2 = 0;
287 } else {
288 OSMO_ASSERT(msgb_tailroom(msg) >= 2);
289 msgb_put(msg, 2);
290 osmo_amr_iuup_to_bwe(msgb_data(msg), msgb_length(msg) - 2, msgb_length(msg) + 2);
291 /* fill bwe header */
292 amr_data = msgb_data(msg);
293 /* CMR no change | follow bit | ft (3 of 4 bits) */
294 amr_data[0] = 15 << 4 | (0 << 3) | (ft >> 1);
295 amr_data[1] |= ((ft & 0x1) << 7) | (((!fqc) & 0x1) << 6);
296 amr_length = (osmo_amr_bits(ft) + 10 + 7) / 8;
297 msgb_trim(msg, amr_length);
Neels Hofmeyr5e6661e2022-10-24 01:34:36 +0200298 LOGP(DLMGCP, LOGL_DEBUG, "Convert IuUP -> AMR BE: ft %d, len %zd\n", ft, amr_length);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100299 }
300 rtp_hdr = (struct rtp_hdr *) msgb_push(msg, sizeof(*rtp_hdr));
301 *rtp_hdr = (struct rtp_hdr){
302 .csrc_count = 0,
303 .extension = 0,
304 .padding = 0,
305 .version = 0,
306 .payload_type = conn_rtp_dst->end.codec->payload_type,
307 .marker = 0,
308 .sequence = frame_nr,
309 .timestamp = 0,
310 .ssrc = 0
311 };
312
313 rc = mgcp_send(conn_rtp_dst->conn->endp, true, NULL, msg, conn_rtp_src, conn_rtp_dst);
314 msgb_free(msg);
315 return rc;
316}
317
318/* Handle RNL Data primitive received from the IuUP layer FSM: Bridge it to the
319 * sister connection in the endpoint: */
320static int _conn_iuup_rx_rnl_data(struct mgcp_conn_rtp *conn_rtp_src, struct osmo_iuup_rnl_prim *irp)
321{
322 struct mgcp_conn *conn_dst;
323 struct mgcp_conn_rtp *conn_rtp_dst;
324 int rc;
325
326 conn_dst = _find_dst_conn(conn_rtp_src->conn);
327
328 /* There is no destination conn, stop here */
329 if (!conn_dst) {
330 LOGPCONN(conn_rtp_src->conn, DRTP, LOGL_DEBUG,
331 "no connection to forward an incoming IuUP payload to\n");
332 rc = -1;
333 goto free_ret;
334 }
335
336 /* The destination conn is not an RTP/IuUP connection */
337 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
338 LOGPCONN(conn_rtp_src->conn, DRTP, LOGL_ERROR,
339 "unable to find suitable destination conn\n");
340 rc = -1;
341 goto free_ret;
342 }
343 conn_rtp_dst = &conn_dst->u.rtp;
344
345 switch (conn_rtp_dst->type) {
346 case MGCP_RTP_IUUP:
347 return bridge_iuup_to_iuup_peer(conn_rtp_src, conn_rtp_dst, irp);
348 case MGCP_RTP_DEFAULT:
349 return bridge_iuup_to_rtp_peer(conn_rtp_src, conn_rtp_dst, irp);
Pau Espin Pedrol9d939b62022-10-03 16:59:20 +0200350 case MGCP_RTP_OSMUX:
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100351 default:
352 LOGPCONN(conn_rtp_src->conn, DRTP, LOGL_ERROR,
353 "Forward of IuUP payload to RTP connection type %u not supported!\n",
354 conn_rtp_dst->type);
355 rc = 0;
356 }
357
358free_ret:
359 msgb_free(irp->oph.msg);
360 return rc;
361}
362
363/* Handle RNL Status-Init primitive received from the IuUP layer FSM.
364 * Potentially configure sister conn as IuUP Init-Active: */
365static int _conn_iuup_rx_rnl_status_init(struct mgcp_conn_rtp *conn_rtp_src, struct osmo_iuup_rnl_prim *irp)
366{
367 struct mgcp_conn *conn_dst;
368 struct mgcp_conn_rtp *conn_rtp_dst;
369 int rc = 0;
370 struct msgb *msg;
371
Pau Espin Pedrola02faff2022-06-13 14:07:22 +0200372 if (conn_rtp_src->iuup.init_ind) {
373 /* We received more than one IuUP Initialization. It's probably
374 * a retransmission, so simply ignore it (lower layers take care
375 * of ACKing it). */
376 LOGPCONN(conn_rtp_src->conn, DRTP, LOGL_INFO,
377 "Ignoring potential IuUP Initialization retrans\n");
378 return 0;
379 }
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100380
381 msg = msgb_copy_c(conn_rtp_src->conn, irp->oph.msg, "iuup-init-copy");
382 conn_rtp_src->iuup.init_ind = (struct osmo_iuup_rnl_prim *)msgb_data(msg);
383 conn_rtp_src->iuup.init_ind->oph.msg = msg;
384
Pau Espin Pedrola02faff2022-06-13 14:07:22 +0200385 /* Find RFCI containing NO_DATA: */
386 conn_rtp_src->iuup.rfci_id_no_data = _find_rfci_no_data(irp);
387
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100388 conn_dst = _find_dst_conn(conn_rtp_src->conn);
389 /* If not yet there, peer will potentially be IuUP-Initialized later
390 * when we attempt to bridge audio towards it. See bridge_iuup_to_iuup_peer() */
391 if (!conn_dst)
392 return 0;
393 conn_rtp_dst = &conn_dst->u.rtp;
394 if (!mgcp_conn_rtp_is_iuup(conn_rtp_dst))
395 return 0; /* Nothing to do */
396
397 /* We received IuUP parameters on the peer (RNC), Init actively this conn (against CN): */
398 if (!conn_rtp_dst->iuup.configured)
399 rc = _conn_iuup_configure_as_active(conn_rtp_dst, irp);
400
401 return rc;
402}
403
404/* Handle RNL Status primitives received from the IuUP layer FSM: */
405static int _conn_iuup_rx_rnl_status(struct mgcp_conn_rtp *conn_rtp_src, struct osmo_iuup_rnl_prim *irp)
406{
407 int rc;
408
409 switch (irp->u.status.procedure) {
410 case IUUP_PROC_INIT:
411 rc = _conn_iuup_rx_rnl_status_init(conn_rtp_src, irp);
412 break;
413 case IUUP_PROC_RATE_CTRL:
414 case IUUP_PROC_TIME_ALIGN:
415 case IUUP_PROC_ERR_EVENT:
416 default:
417 LOG_CONN_RTP(conn_rtp_src, LOGL_ERROR,
418 "Received IuUP RNL STATUS procedure type %u not handled\n",
419 irp->u.status.procedure);
420 rc = 0;
421 }
422
423 return rc;
424}
425
426/* Received RNL primitive from the IuUP layer FSM containing IuUP Status or
427 * data. Continue pushing it up the stack, either IuUP Status or Data: */
428static int _conn_iuup_user_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
429{
430 struct mgcp_conn_rtp *conn_rtp_src = ctx;
431 struct osmo_iuup_rnl_prim *irp = (struct osmo_iuup_rnl_prim *)oph;
432 struct msgb *msg = oph->msg;
433 int rc;
434
435 switch (OSMO_PRIM_HDR(&irp->oph)) {
436 case OSMO_PRIM(OSMO_IUUP_RNL_DATA, PRIM_OP_INDICATION):
437 /* we pass ownsership of msg here: */
438 rc = _conn_iuup_rx_rnl_data(conn_rtp_src, irp);
439 break;
440 case OSMO_PRIM(OSMO_IUUP_RNL_STATUS, PRIM_OP_INDICATION):
441 rc = _conn_iuup_rx_rnl_status(conn_rtp_src, irp);
442 msgb_free(msg);
443 break;
444 default:
445 msgb_free(msg);
446 OSMO_ASSERT(false);
447 }
448
449 return rc;
450}
451
452/*! Send |RTP+IuUP| data down the stack of the specified destination connection.
453 * \param[in] endp associated endpoint (for configuration, logging).
454 * \param[in] buf buffer that contains the |RTP+IuUP| data.
455 * \param[in] len length of the buffer that contains the |RTP+IuUP| data.
456 * \param[in] conn_src associated source connection.
457 * \param[in] conn_dst associated destination connection.
458 * \returns 0 on success, -1 on ERROR. */
459static int mgcp_send_iuup(struct mgcp_endpoint *endp, struct msgb *msg,
460 struct mgcp_conn_rtp *conn_src, struct mgcp_conn_rtp *conn_dst)
461{
462 /*! When no destination connection is available (e.g. when only one
463 * connection in loopback mode exists), then the source connection
464 * shall be specified as destination connection */
465
466 struct mgcp_rtp_end *rtp_end;
467 struct mgcp_rtp_state *rtp_state;
468 char ipbuf[INET6_ADDRSTRLEN];
469 struct rtp_hdr *hdr = (struct rtp_hdr *)msgb_data(msg);
470 int buflen = msgb_length(msg);
471 char *dest_name;
472 int len;
473
474 OSMO_ASSERT(conn_src);
475 OSMO_ASSERT(conn_dst);
476
477 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering IuUP packet...\n");
478
479 /* Note: In case of loopback configuration, both, the source and the
480 * destination will point to the same connection. */
481 rtp_end = &conn_dst->end;
482 rtp_state = &conn_src->state;
483 dest_name = conn_dst->conn->name;
484
485 /* Ensure we have an alternative SSRC in case we need it, see also
486 * gen_rtp_header() */
487 if (rtp_state->alt_rtp_tx_ssrc == 0)
488 rtp_state->alt_rtp_tx_ssrc = rand();
489
490 if (!rtp_end->output_enabled) {
491 rtpconn_rate_ctr_add(conn_dst, endp, RTP_DROPPED_PACKETS_CTR, 1);
492 LOGPENDP(endp, DRTP, LOGL_DEBUG,
493 "output disabled, drop to %s %s "
494 "rtp_port:%u rtcp_port:%u\n",
495 dest_name,
496 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200497 osmo_sockaddr_port(&rtp_end->addr.u.sa), ntohs(rtp_end->rtcp_port)
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100498 );
499 return 0;
500 }
501
502 /* Specs say, in IuUP, the RTP seqnum and timestamp should actually be
503 * ignored by the receiver, but still it's useful for debug purposes
504 * to set it. Moreover, it seems ip.access nano3g produces much worse
505 * audio output on the air side if timestamp is not set properly. */
506 hdr->timestamp = osmo_htonl(mgcp_get_current_ts(rtp_end->codec->rate));
507 hdr->sequence = osmo_htons(rtp_state->alt_rtp_tx_sequence);
508 hdr->ssrc = rtp_state->alt_rtp_tx_ssrc;
509
510 LOGPENDP(endp, DRTP, LOGL_DEBUG,
511 "process/send IuUP to %s %s rtp_port:%u rtcp_port:%u\n",
512 dest_name, osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200513 osmo_sockaddr_port(&rtp_end->addr.u.sa), ntohs(rtp_end->rtcp_port));
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100514
515 /* Forward a copy of the RTP data to a debug ip/port */
516 forward_data_tap(rtp_end->rtp.fd, &conn_src->tap_out,
517 msg);
518
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200519 len = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr, (char *)hdr, buflen);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100520
521 if (len <= 0)
522 return len;
523
524 rtpconn_rate_ctr_add(conn_dst, endp, RTP_PACKETS_TX_CTR, 1);
525 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
526 rtp_state->alt_rtp_tx_sequence++;
527
528 return len;
529}
530
531/* Received TNL primitive from IuUP layer FSM, transmit it further down to the
532 * socket towards destination peer. */
533static int _conn_iuup_transport_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
534{
535 struct mgcp_conn_rtp *conn_rtp_dst = ctx;
536 struct mgcp_conn *conn_dst = conn_rtp_dst->conn;
537 struct osmo_iuup_tnl_prim *itp = (struct osmo_iuup_tnl_prim *)oph;
538 struct mgcp_conn *conn_src;
539 struct msgb *msg;
540 struct rtp_hdr *rtph;
541
542 OSMO_ASSERT(OSMO_PRIM_HDR(&itp->oph) == OSMO_PRIM(OSMO_IUUP_TNL_UNITDATA, PRIM_OP_REQUEST));
543
544 msg = oph->msg;
545 talloc_steal(conn_rtp_dst->conn, msg);
546
547 msgb_pull_to_l2(msg);
548 rtph = (struct rtp_hdr *)msgb_push(msg, sizeof(*rtph));
549 /* TODO: fill rtph properly: */
550 *rtph = (struct rtp_hdr){
551 .csrc_count = 0,
552 .extension = 0,
553 .padding = 0,
554 .version = 2,
555 .payload_type = conn_rtp_dst->end.codec->payload_type,
556 .marker = 0,
557 .sequence = 0,
558 .timestamp = 0,
559 .ssrc = 0
560 };
561
562 /* The destination of the destination conn is the source conn, right? */
563 conn_src = _find_dst_conn(conn_dst);
564 if (!conn_src) {
565 LOG_CONN_RTP(conn_rtp_dst, LOGL_NOTICE,
566 "Couldn't find source conn for IuUP dst conn\n");
567 /* If there's no sister connection we are either still
568 * initializing (so we want to send back Init (ACK)), or we are
569 * probably in loopback mode anyway, so use dst as src. */
570 conn_src = conn_dst;
571 }
572
573 return mgcp_send_iuup(conn_dst->endp, msg, &conn_src->u.rtp, conn_rtp_dst);
574}
575
576/* Used to upgrade a regular RTP connection (MGCP_RTP_DEFAULT) to become a IuUP
577 * connection (MGCP_RTP_IUUP) */
578int mgcp_conn_iuup_init(struct mgcp_conn_rtp *conn_rtp)
579{
580 conn_rtp->type = MGCP_RTP_IUUP;
581 conn_rtp->iuup.iui = osmo_iuup_instance_alloc(conn_rtp->conn, conn_rtp->conn->id);
582 OSMO_ASSERT(conn_rtp->iuup.iui);
583 osmo_iuup_instance_set_user_prim_cb(conn_rtp->iuup.iui, _conn_iuup_user_prim_cb, conn_rtp);
584 osmo_iuup_instance_set_transport_prim_cb(conn_rtp->iuup.iui, _conn_iuup_transport_prim_cb, conn_rtp);
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200585 conn_rtp->iuup.rfci_id_no_data = -1;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100586 return 0;
587}
588
589/* Cleanup specific IuUP connection (MGCP_RTP_IUUP) state, allocated by mgcp_conn_iuup_init() */
590void mgcp_conn_iuup_cleanup(struct mgcp_conn_rtp *conn_rtp)
591{
592 osmo_iuup_instance_free(conn_rtp->iuup.iui);
593 conn_rtp->iuup.iui = NULL;
594}
595
596/* Received RTP+IuUP pkt from socket of conn_rtp_src, build a TNL primitive to
597 * push it further up the stack to the IuUP layer FSM to handle and/or bridge it */
598int mgcp_conn_iuup_dispatch_rtp(struct msgb *msg)
599{
600 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
601 struct mgcp_conn_rtp *conn_rtp_src = mc->conn_src;
602 int rc = 0;
603 bool force_output_enabled = false;
604 bool prev_output_enabled;
605 struct osmo_sockaddr prev_rem_addr;
606 uint16_t prev_rem_rtp_port;
607
608 OSMO_ASSERT(mgcp_conn_rtp_is_iuup(conn_rtp_src));
609
610 if ((rc = check_rtp_iuup(conn_rtp_src, msg)) < 0)
611 goto free_ret;
612
613 if (!conn_rtp_src->iuup.configured) {
614 /* We received the first message without sending any, the peer is the active side (RNC). */
615 rc = _conn_iuup_configure_as_passive(conn_rtp_src);
616 if (rc < 0)
617 goto free_ret;
618 /* We need to force allowance of RTP containing Init-ACK back: */
619 prev_output_enabled = conn_rtp_src->end.output_enabled;
620 conn_rtp_src->end.output_enabled = true;
621 force_output_enabled = true;
622 /* Fill in the peer address so that we can send Init-ACK back: */
623 prev_rem_addr = conn_rtp_src->end.addr;
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200624 prev_rem_rtp_port = osmo_sockaddr_port(&conn_rtp_src->end.addr.u.sa);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100625 conn_rtp_src->end.addr = *mc->from_addr;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100626 }
627
628 rc = _conn_iuup_rtp_pl_up(conn_rtp_src, msg);
629
630 if (force_output_enabled) {
631 conn_rtp_src->end.output_enabled = prev_output_enabled;
632 conn_rtp_src->end.addr = prev_rem_addr;
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200633 osmo_sockaddr_set_port(&conn_rtp_src->end.addr.u.sa, prev_rem_rtp_port);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100634 }
635
636 return rc;
637free_ret:
638 msgb_free(msg);
639 return rc;
640}
641
642/* Build IuUP RNL Data primitive from msg containing an incoming RTP pkt from
643 * peer and send it down the IuUP layer towards the destination as IuUP/RTP: */
644int mgcp_conn_iuup_send_rtp(struct mgcp_conn_rtp *conn_src_rtp, struct mgcp_conn_rtp *conn_dest_rtp, struct msgb *msg)
645{
646 struct osmo_iuup_rnl_prim *irp;
647 struct rtp_hdr *rtph;
648 int rc = -1;
649 int iuup_length = 0;
650 int8_t rfci;
651
652 /* Tx RNL-DATA.req */
653 rtph = (struct rtp_hdr *)msgb_data(msg);
654 msgb_pull(msg, sizeof(*rtph));
655
656 /* FIXME: validate amr packets */
657 irp = osmo_iuup_rnl_prim_alloc(conn_dest_rtp->conn, OSMO_IUUP_RNL_DATA, PRIM_OP_REQUEST, MGW_IUUP_MSGB_SIZE);
658 irp->u.data.frame_nr = htons(rtph->sequence) % 16;
659
660 /* TODO: CMR handling & multiple frames handling */
661
662 if (strcmp(conn_src_rtp->end.codec->subtype_name, "AMR") != 0) {
663 LOG_CONN_RTP(conn_src_rtp, LOGL_ERROR,
664 "Bridge RTP=>IuUP: Bridging src codec %s to IuUP AMR not supported\n",
665 conn_src_rtp->end.codec->subtype_name);
666 goto free_ret;
667 }
668 if (mgcp_codec_amr_is_octet_aligned(conn_src_rtp->end.codec)) {
669 struct amr_hdr *amr_hdr = (struct amr_hdr *) msgb_data(msg);
670 if (msgb_length(msg) < (sizeof(*amr_hdr))) {
671 LOG_CONN_RTP(conn_src_rtp, LOGL_NOTICE,
672 "Bridge RTP=>IuUP: too short for AMR OA hdr (%u)\n", msgb_length(msg));
673 goto free_ret;
674 }
Pau Espin Pedrolded02cf2022-12-19 13:24:02 +0100675 if (!osmo_amr_ft_valid(amr_hdr->ft)) {
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100676 LOG_CONN_RTP(conn_src_rtp, LOGL_NOTICE, "Bridge RTP=>IuUP: wrong AMR OA ft=%u\n", amr_hdr->ft);
677 goto free_ret;
678 }
679 if ((rfci = _conn_iuup_amr_ft_2_rfci(conn_dest_rtp, amr_hdr->ft)) < 0) {
680 LOG_CONN_RTP(conn_dest_rtp, LOGL_NOTICE, "Bridge RTP=>IuUP: No RFCI found for AMR OA ft=%u\n", amr_hdr->ft);
681 goto free_ret;
682 }
Neels Hofmeyr24763ea2022-10-24 00:54:40 +0200683 irp->u.data.fqc = amr_hdr->q ? IUUP_FQC_FRAME_GOOD : IUUP_FQC_FRAME_BAD;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100684 irp->u.data.rfci = rfci;
685 msgb_pull(msg, 2);
Neels Hofmeyr5fb814b2022-10-24 01:34:36 +0200686 LOGP(DLMGCP, LOGL_DEBUG, "Convert AMR OA -> IuUP: ft %d -> rfci %d len %d\n",
687 amr_hdr->ft, rfci, msgb_length(msg));
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100688 } else {
689 uint8_t *amr_bwe_hdr = (uint8_t *) msgb_data(msg);
690 int8_t ft;
Neels Hofmeyr24763ea2022-10-24 00:54:40 +0200691 uint8_t q;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100692 if (msgb_length(msg) < 2) {
693 LOG_CONN_RTP(conn_src_rtp, LOGL_NOTICE,
694 "Bridge RTP=>IuUP: too short for AMR BE hdr (%u)\n", msgb_length(msg));
695 goto free_ret;
696 }
697 ft = ((amr_bwe_hdr[0] & 0x07) << 1) | ((amr_bwe_hdr[1] & 0x80) >> 7);
Pau Espin Pedrolded02cf2022-12-19 13:24:02 +0100698 if (!osmo_amr_ft_valid(ft)) {
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100699 LOG_CONN_RTP(conn_src_rtp, LOGL_NOTICE, "Bridge RTP=>IuUP: wrong AMR BE ft=%u\n", ft);
700 goto free_ret;
701 }
702 if ((rfci = _conn_iuup_amr_ft_2_rfci(conn_dest_rtp, ft)) < 0) {
703 LOG_CONN_RTP(conn_dest_rtp, LOGL_NOTICE, "Bridge RTP=>IuUP: No RFCI found for AMR BE ft=%u\n", ft);
704 goto free_ret;
705 }
Neels Hofmeyr24763ea2022-10-24 00:54:40 +0200706 q = amr_bwe_hdr[1] & 0x40;
707 irp->u.data.fqc = q ? IUUP_FQC_FRAME_GOOD : IUUP_FQC_FRAME_BAD;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100708 irp->u.data.rfci = rfci;
709 rc = iuup_length = osmo_amr_bwe_to_iuup(msgb_data(msg), msgb_length(msg));
710 if (rc < 0) {
711 LOG_CONN_RTP(conn_dest_rtp, LOGL_ERROR, "Bridge RTP=>IuUP: Failed convert the RTP/AMR to IuUP payload\n");
712 return rc;
713 }
714 msgb_trim(msg, iuup_length);
Neels Hofmeyr5fb814b2022-10-24 01:34:36 +0200715 LOGP(DLMGCP, LOGL_DEBUG, "Convert AMR BE -> IuUP: ft %d -> rfci %d len %d\n",
716 ft, rfci, msgb_length(msg));
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100717 }
718
719 irp->oph.msg->l3h = msgb_put(irp->oph.msg, msgb_length(msg));
720 memcpy(irp->oph.msg->l3h, msgb_data(msg), msgb_length(msg));
721 if ((rc = osmo_iuup_rnl_prim_down(conn_dest_rtp->iuup.iui, irp)) != 0)
722 LOG_CONN_RTP(conn_dest_rtp, LOGL_ERROR, "Bridge RTP=>IuUP: Failed Tx RTP payload down the IuUP layer\n");
723 return rc;
724
725free_ret:
726 msgb_free(irp->oph.msg);
727 return -1;
728}
729
730/* Build IuUP RNL Data primitive from msg containing dummy content and send it
731 * down the IuUP layer towards the destination as IuUP/RTP: */
732int mgcp_conn_iuup_send_dummy(struct mgcp_conn_rtp *conn_rtp)
733{
734 struct osmo_iuup_rnl_prim *irp;
735 int rc;
736
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200737 if (conn_rtp->iuup.rfci_id_no_data == -1) {
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100738 LOG_CONN_RTP(conn_rtp, LOGL_NOTICE, "No RFCI NO_DATA found, unable to send dummy packet\n");
739 return -ENOTSUP;
740 }
741
742 irp = osmo_iuup_rnl_prim_alloc(conn_rtp->conn, OSMO_IUUP_RNL_DATA, PRIM_OP_REQUEST, MGW_IUUP_MSGB_SIZE);
743 irp->u.data.frame_nr = 0;
744 irp->u.data.fqc = IUUP_FQC_FRAME_GOOD;
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200745 irp->u.data.rfci = conn_rtp->iuup.rfci_id_no_data;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100746 irp->oph.msg->l3h = irp->oph.msg->tail;
747 if ((rc = osmo_iuup_rnl_prim_down(conn_rtp->iuup.iui, irp)) != 0) {
748 LOG_CONN_RTP(conn_rtp, LOGL_ERROR, "Failed Tx RTP dummy payload down the IuUP layer\n");
749 return -EINVAL;
750 }
751
752 return 0;
753}