blob: 90021f39dd79fd582c4699a87dcc1fa0548bb727 [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;
101 OSMO_ASSERT(irp);
102
103 /* TODO: cache this somehow */
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200104 for (i = 0; i < ARRAY_SIZE(irp->u.status.u.initialization.rfci); i++) {
105 struct osmo_iuup_rfci *rfci = &irp->u.status.u.initialization.rfci[i];
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100106 int j;
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200107 unsigned num_bits;
108 if (!rfci->used)
109 continue;
110 rfci_cnt++;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100111
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200112 num_bits = 0;
113 for (j = 0; j < irp->u.status.u.initialization.num_subflows; j++)
114 num_bits += rfci->subflow_sizes[j];
115 if (match_bytes == (num_bits + 7)/8)
116 return rfci->id;
117
118 /* early loop termination: */
119 if (rfci_cnt == irp->u.status.u.initialization.num_subflows)
120 break;
121 }
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100122 return -1;
123}
124
125/* Helper function to configure IuUP layer FSM as Init-Passive, based on default config */
126static int _conn_iuup_configure_as_passive(struct mgcp_conn_rtp *conn_rtp)
127{
128 struct osmo_iuup_rnl_prim *irp;
129 int rc;
130
131 conn_rtp->iuup.active_init = false;
132
133 /* Tx CONFIG.req */
134 irp = osmo_iuup_rnl_prim_alloc(conn_rtp->conn, OSMO_IUUP_RNL_CONFIG, PRIM_OP_REQUEST, MGW_IUUP_MSGB_SIZE);
135 irp->u.config = def_configure_req;
136 irp->u.config.active = conn_rtp->iuup.active_init;
137 if ((rc = osmo_iuup_rnl_prim_down(conn_rtp->iuup.iui, irp)) == 0)
138 conn_rtp->iuup.configured = true;
139 else
140 LOG_CONN_RTP(conn_rtp, LOGL_ERROR, "Failed configuring IuUP layer\n");
141 return rc;
142}
143
144/* Helper function to configure IuUP layer FSM as Init-Active, based on received
145 * RNL Status-Init primitive from the sister IuUP connection we will bridge to. */
146static int _conn_iuup_configure_as_active(struct mgcp_conn_rtp *conn_rtp, struct osmo_iuup_rnl_prim *init_ind)
147{
148 struct osmo_iuup_rnl_prim *irp = init_ind;
149 struct osmo_iuup_rnl_prim *irp2;
150 struct msgb *msg;
151 bool prev_output_enabled;
152 int rc;
153
154 conn_rtp->iuup.active_init = true;
155
156 /* Find RFCI containing NO_DATA: */
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200157 conn_rtp->iuup.rfci_id_no_data = _find_rfci_no_data(init_ind);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100158
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200159 /* Copy over the rfci_id_no_data, since we reuse the same subflow set: */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100160 msg = msgb_copy_c(conn_rtp->conn, irp->oph.msg, "iuup-init-copy");
161 conn_rtp->iuup.init_ind = (struct osmo_iuup_rnl_prim *)msgb_data(msg);
162 conn_rtp->iuup.init_ind->oph.msg = msg;
163
164 /* Tx CONFIG.req */
165 irp2 = osmo_iuup_rnl_prim_alloc(conn_rtp->conn, OSMO_IUUP_RNL_CONFIG, PRIM_OP_REQUEST, MGW_IUUP_MSGB_SIZE);
166 irp2->u.config.transparent = false;
167 irp2->u.config.active = conn_rtp->iuup.active_init;
168 irp2->u.config.data_pdu_type = irp->u.status.u.initialization.data_pdu_type;
169 irp2->u.config.supported_versions_mask = def_configure_req.supported_versions_mask;
170 irp2->u.config.num_rfci = irp->u.status.u.initialization.num_rfci;
171 irp2->u.config.num_subflows = irp->u.status.u.initialization.num_subflows;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100172 irp2->u.config.IPTIs_present = irp->u.status.u.initialization.IPTIs_present;
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200173 memcpy(irp2->u.config.rfci, irp->u.status.u.initialization.rfci, sizeof(irp2->u.config.rfci));
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100174 irp2->u.config.t_init = def_configure_req.t_init;
175 irp2->u.config.t_ta = def_configure_req.t_ta;
176 irp2->u.config.t_rc = def_configure_req.t_rc;
177
178 /* We need to force allowance of RTP containing Init-ACK back: */
179 prev_output_enabled = conn_rtp->end.output_enabled;
180 conn_rtp->end.output_enabled = true;
181
182 if ((rc = osmo_iuup_rnl_prim_down(conn_rtp->iuup.iui, irp2)) == 0)
183 conn_rtp->iuup.configured = true;
184 else
185 LOG_CONN_RTP(conn_rtp, LOGL_ERROR, "Failed configuring IuUP layer\n");
186
187 conn_rtp->end.output_enabled = prev_output_enabled;
188 return rc;
189}
190
191/* Helper function to push an RTP+IuUP pkt up to the IuUP layer FSM through the
192 * TNL primitive interface. */
193static int _conn_iuup_rtp_pl_up(struct mgcp_conn_rtp *conn_rtp, struct msgb *msg)
194{
195 /* Send RTP payload (IuUP) up the stack: */
196 struct osmo_iuup_tnl_prim *itp;
197 int rc;
198
199 msg->l2h = msgb_data(msg) + sizeof(struct rtp_hdr);
200
201 itp = osmo_iuup_tnl_prim_alloc(conn_rtp->conn, OSMO_IUUP_TNL_UNITDATA, PRIM_OP_INDICATION, MGW_IUUP_MSGB_SIZE);
202 itp->oph.msg->l2h = msgb_put(itp->oph.msg, msgb_l2len(msg));
203 memcpy(itp->oph.msg->l2h, msgb_l2(msg), msgb_l2len(msg));
204 if ((rc = osmo_iuup_tnl_prim_up(conn_rtp->iuup.iui, itp)) != 0) {
205 LOG_CONN_RTP(conn_rtp, LOGL_ERROR, "Failed passing IuUP-Init to IuUP layer\n");
206 }
207 return rc;
208}
209
210static int check_rtp_iuup(const struct mgcp_conn_rtp *conn_rtp, struct msgb *msg)
211{
212 size_t min_size = sizeof(struct rtp_hdr);
213 /* Check there's at least 2 bytes of RTP payload (IuUP header). This is
214 ** mainly to avoid 0-byte payload copy cases */
215 if (msgb_length(msg) < sizeof(struct rtp_hdr) + 2) {
216 LOG_CONN_RTP(conn_rtp, LOGL_ERROR, "RTP-IuUP packet too short (%u < %zu)\n",
217 msgb_length(msg), min_size);
218 return -1;
219 }
220 return 0;
221}
222
223/* Bridge received IuUP packet in conn_rtp_src to conn_rtp_dst, an IuUP sister
224 * conn in the endpoint. The function takes ownsership of the irp */
225static 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)
226{
227 int rc;
228
229 /* If we are not configured and we received bridged data, it means
230 * conn_rtp_src is already configured and INITed, and we can infer
231 * conn_rtp_src is Init-passive (RNC side), so conn_rtp_dst needs to be
232 * configured as INIT-active: */
233 if (!conn_rtp_dst->iuup.configured) {
234 OSMO_ASSERT(conn_rtp_src->iuup.init_ind);
235 rc = _conn_iuup_configure_as_active(conn_rtp_dst, conn_rtp_src->iuup.init_ind);
236 if (rc < 0) {
237 msgb_free(irp->oph.msg);
238 return rc;
239 }
240 }
241
242 /* We simply forward the msg, without freeing it: */
243 talloc_steal(conn_rtp_dst->conn, irp->oph.msg);
244 irp->oph.operation = PRIM_OP_REQUEST;
245 if ((rc = osmo_iuup_rnl_prim_down(conn_rtp_dst->iuup.iui, irp)) != 0)
246 LOG_CONN_RTP(conn_rtp_dst, LOGL_ERROR, "Failed Tx data down to IuUP layer\n");
247 return rc;
248}
249
250/* Bridge received IuUP packet in conn_rtp_src to conn_rtp_dst, an RTP (no IuUP)
251 * sister conn in the endpoint. The function takes ownsership of the irp */
252static 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)
253{
254 /* FIXME: We probably need transcoding here?! Or at least look up AMR modes and translate to related RFCI */
255 uint8_t frame_nr = irp->u.data.frame_nr;
256 uint8_t fqc = irp->u.data.fqc;
257 struct msgb *msg = irp->oph.msg;
258 ssize_t amr_length = 0;
259 int ft;
260 uint8_t *amr_data;
261 struct rtp_hdr *rtp_hdr;
262 struct amr_hdr *amr_hdr;
263 int rc;
264
265 ft = osmo_amr_bytes_to_ft(msgb_l3len(msg));
266 if (ft < 0) {
267 LOGPCONN(conn_rtp_src->conn, DRTP, LOGL_ERROR,
268 "Unknown AMR format for size %u\n", msgb_l3len(msg));
269 msgb_free(msg);
270 return ft;
271 }
272 msgb_pull_to_l3(msg);
Pau Espin Pedrolce055d52022-05-25 18:34:06 +0200273 LOGP(DLMGCP, LOGL_DEBUG, "Convert IuUP -> AMR: ft %d, len %d\n", ft, msgb_l3len(msg));
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100274
275 if (mgcp_codec_amr_is_octet_aligned(conn_rtp_dst->end.codec)) {
276 amr_hdr = (struct amr_hdr *) msgb_push(msg, sizeof(struct amr_hdr));
277 amr_hdr->cmr = 15; /* no change */
278 amr_hdr->f = 0;
279 amr_hdr->q = !fqc;
280 amr_hdr->ft = ft & 0xff;
281 amr_hdr->pad1 = 0;
282 amr_hdr->pad2 = 0;
283 } else {
284 OSMO_ASSERT(msgb_tailroom(msg) >= 2);
285 msgb_put(msg, 2);
286 osmo_amr_iuup_to_bwe(msgb_data(msg), msgb_length(msg) - 2, msgb_length(msg) + 2);
287 /* fill bwe header */
288 amr_data = msgb_data(msg);
289 /* CMR no change | follow bit | ft (3 of 4 bits) */
290 amr_data[0] = 15 << 4 | (0 << 3) | (ft >> 1);
291 amr_data[1] |= ((ft & 0x1) << 7) | (((!fqc) & 0x1) << 6);
292 amr_length = (osmo_amr_bits(ft) + 10 + 7) / 8;
293 msgb_trim(msg, amr_length);
294 }
295 rtp_hdr = (struct rtp_hdr *) msgb_push(msg, sizeof(*rtp_hdr));
296 *rtp_hdr = (struct rtp_hdr){
297 .csrc_count = 0,
298 .extension = 0,
299 .padding = 0,
300 .version = 0,
301 .payload_type = conn_rtp_dst->end.codec->payload_type,
302 .marker = 0,
303 .sequence = frame_nr,
304 .timestamp = 0,
305 .ssrc = 0
306 };
307
308 rc = mgcp_send(conn_rtp_dst->conn->endp, true, NULL, msg, conn_rtp_src, conn_rtp_dst);
309 msgb_free(msg);
310 return rc;
311}
312
313/* Handle RNL Data primitive received from the IuUP layer FSM: Bridge it to the
314 * sister connection in the endpoint: */
315static int _conn_iuup_rx_rnl_data(struct mgcp_conn_rtp *conn_rtp_src, struct osmo_iuup_rnl_prim *irp)
316{
317 struct mgcp_conn *conn_dst;
318 struct mgcp_conn_rtp *conn_rtp_dst;
319 int rc;
320
321 conn_dst = _find_dst_conn(conn_rtp_src->conn);
322
323 /* There is no destination conn, stop here */
324 if (!conn_dst) {
325 LOGPCONN(conn_rtp_src->conn, DRTP, LOGL_DEBUG,
326 "no connection to forward an incoming IuUP payload to\n");
327 rc = -1;
328 goto free_ret;
329 }
330
331 /* The destination conn is not an RTP/IuUP connection */
332 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
333 LOGPCONN(conn_rtp_src->conn, DRTP, LOGL_ERROR,
334 "unable to find suitable destination conn\n");
335 rc = -1;
336 goto free_ret;
337 }
338 conn_rtp_dst = &conn_dst->u.rtp;
339
340 switch (conn_rtp_dst->type) {
341 case MGCP_RTP_IUUP:
342 return bridge_iuup_to_iuup_peer(conn_rtp_src, conn_rtp_dst, irp);
343 case MGCP_RTP_DEFAULT:
344 return bridge_iuup_to_rtp_peer(conn_rtp_src, conn_rtp_dst, irp);
Pau Espin Pedrol9d939b62022-10-03 16:59:20 +0200345 case MGCP_RTP_OSMUX:
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100346 default:
347 LOGPCONN(conn_rtp_src->conn, DRTP, LOGL_ERROR,
348 "Forward of IuUP payload to RTP connection type %u not supported!\n",
349 conn_rtp_dst->type);
350 rc = 0;
351 }
352
353free_ret:
354 msgb_free(irp->oph.msg);
355 return rc;
356}
357
358/* Handle RNL Status-Init primitive received from the IuUP layer FSM.
359 * Potentially configure sister conn as IuUP Init-Active: */
360static int _conn_iuup_rx_rnl_status_init(struct mgcp_conn_rtp *conn_rtp_src, struct osmo_iuup_rnl_prim *irp)
361{
362 struct mgcp_conn *conn_dst;
363 struct mgcp_conn_rtp *conn_rtp_dst;
364 int rc = 0;
365 struct msgb *msg;
366
Pau Espin Pedrola02faff2022-06-13 14:07:22 +0200367 if (conn_rtp_src->iuup.init_ind) {
368 /* We received more than one IuUP Initialization. It's probably
369 * a retransmission, so simply ignore it (lower layers take care
370 * of ACKing it). */
371 LOGPCONN(conn_rtp_src->conn, DRTP, LOGL_INFO,
372 "Ignoring potential IuUP Initialization retrans\n");
373 return 0;
374 }
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100375
376 msg = msgb_copy_c(conn_rtp_src->conn, irp->oph.msg, "iuup-init-copy");
377 conn_rtp_src->iuup.init_ind = (struct osmo_iuup_rnl_prim *)msgb_data(msg);
378 conn_rtp_src->iuup.init_ind->oph.msg = msg;
379
Pau Espin Pedrola02faff2022-06-13 14:07:22 +0200380 /* Find RFCI containing NO_DATA: */
381 conn_rtp_src->iuup.rfci_id_no_data = _find_rfci_no_data(irp);
382
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100383 conn_dst = _find_dst_conn(conn_rtp_src->conn);
384 /* If not yet there, peer will potentially be IuUP-Initialized later
385 * when we attempt to bridge audio towards it. See bridge_iuup_to_iuup_peer() */
386 if (!conn_dst)
387 return 0;
388 conn_rtp_dst = &conn_dst->u.rtp;
389 if (!mgcp_conn_rtp_is_iuup(conn_rtp_dst))
390 return 0; /* Nothing to do */
391
392 /* We received IuUP parameters on the peer (RNC), Init actively this conn (against CN): */
393 if (!conn_rtp_dst->iuup.configured)
394 rc = _conn_iuup_configure_as_active(conn_rtp_dst, irp);
395
396 return rc;
397}
398
399/* Handle RNL Status primitives received from the IuUP layer FSM: */
400static int _conn_iuup_rx_rnl_status(struct mgcp_conn_rtp *conn_rtp_src, struct osmo_iuup_rnl_prim *irp)
401{
402 int rc;
403
404 switch (irp->u.status.procedure) {
405 case IUUP_PROC_INIT:
406 rc = _conn_iuup_rx_rnl_status_init(conn_rtp_src, irp);
407 break;
408 case IUUP_PROC_RATE_CTRL:
409 case IUUP_PROC_TIME_ALIGN:
410 case IUUP_PROC_ERR_EVENT:
411 default:
412 LOG_CONN_RTP(conn_rtp_src, LOGL_ERROR,
413 "Received IuUP RNL STATUS procedure type %u not handled\n",
414 irp->u.status.procedure);
415 rc = 0;
416 }
417
418 return rc;
419}
420
421/* Received RNL primitive from the IuUP layer FSM containing IuUP Status or
422 * data. Continue pushing it up the stack, either IuUP Status or Data: */
423static int _conn_iuup_user_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
424{
425 struct mgcp_conn_rtp *conn_rtp_src = ctx;
426 struct osmo_iuup_rnl_prim *irp = (struct osmo_iuup_rnl_prim *)oph;
427 struct msgb *msg = oph->msg;
428 int rc;
429
430 switch (OSMO_PRIM_HDR(&irp->oph)) {
431 case OSMO_PRIM(OSMO_IUUP_RNL_DATA, PRIM_OP_INDICATION):
432 /* we pass ownsership of msg here: */
433 rc = _conn_iuup_rx_rnl_data(conn_rtp_src, irp);
434 break;
435 case OSMO_PRIM(OSMO_IUUP_RNL_STATUS, PRIM_OP_INDICATION):
436 rc = _conn_iuup_rx_rnl_status(conn_rtp_src, irp);
437 msgb_free(msg);
438 break;
439 default:
440 msgb_free(msg);
441 OSMO_ASSERT(false);
442 }
443
444 return rc;
445}
446
447/*! Send |RTP+IuUP| data down the stack of the specified destination connection.
448 * \param[in] endp associated endpoint (for configuration, logging).
449 * \param[in] buf buffer that contains the |RTP+IuUP| data.
450 * \param[in] len length of the buffer that contains the |RTP+IuUP| data.
451 * \param[in] conn_src associated source connection.
452 * \param[in] conn_dst associated destination connection.
453 * \returns 0 on success, -1 on ERROR. */
454static int mgcp_send_iuup(struct mgcp_endpoint *endp, struct msgb *msg,
455 struct mgcp_conn_rtp *conn_src, struct mgcp_conn_rtp *conn_dst)
456{
457 /*! When no destination connection is available (e.g. when only one
458 * connection in loopback mode exists), then the source connection
459 * shall be specified as destination connection */
460
461 struct mgcp_rtp_end *rtp_end;
462 struct mgcp_rtp_state *rtp_state;
463 char ipbuf[INET6_ADDRSTRLEN];
464 struct rtp_hdr *hdr = (struct rtp_hdr *)msgb_data(msg);
465 int buflen = msgb_length(msg);
466 char *dest_name;
467 int len;
468
469 OSMO_ASSERT(conn_src);
470 OSMO_ASSERT(conn_dst);
471
472 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering IuUP packet...\n");
473
474 /* Note: In case of loopback configuration, both, the source and the
475 * destination will point to the same connection. */
476 rtp_end = &conn_dst->end;
477 rtp_state = &conn_src->state;
478 dest_name = conn_dst->conn->name;
479
480 /* Ensure we have an alternative SSRC in case we need it, see also
481 * gen_rtp_header() */
482 if (rtp_state->alt_rtp_tx_ssrc == 0)
483 rtp_state->alt_rtp_tx_ssrc = rand();
484
485 if (!rtp_end->output_enabled) {
486 rtpconn_rate_ctr_add(conn_dst, endp, RTP_DROPPED_PACKETS_CTR, 1);
487 LOGPENDP(endp, DRTP, LOGL_DEBUG,
488 "output disabled, drop to %s %s "
489 "rtp_port:%u rtcp_port:%u\n",
490 dest_name,
491 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200492 osmo_sockaddr_port(&rtp_end->addr.u.sa), ntohs(rtp_end->rtcp_port)
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100493 );
494 return 0;
495 }
496
497 /* Specs say, in IuUP, the RTP seqnum and timestamp should actually be
498 * ignored by the receiver, but still it's useful for debug purposes
499 * to set it. Moreover, it seems ip.access nano3g produces much worse
500 * audio output on the air side if timestamp is not set properly. */
501 hdr->timestamp = osmo_htonl(mgcp_get_current_ts(rtp_end->codec->rate));
502 hdr->sequence = osmo_htons(rtp_state->alt_rtp_tx_sequence);
503 hdr->ssrc = rtp_state->alt_rtp_tx_ssrc;
504
505 LOGPENDP(endp, DRTP, LOGL_DEBUG,
506 "process/send IuUP to %s %s rtp_port:%u rtcp_port:%u\n",
507 dest_name, osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200508 osmo_sockaddr_port(&rtp_end->addr.u.sa), ntohs(rtp_end->rtcp_port));
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100509
510 /* Forward a copy of the RTP data to a debug ip/port */
511 forward_data_tap(rtp_end->rtp.fd, &conn_src->tap_out,
512 msg);
513
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200514 len = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr, (char *)hdr, buflen);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100515
516 if (len <= 0)
517 return len;
518
519 rtpconn_rate_ctr_add(conn_dst, endp, RTP_PACKETS_TX_CTR, 1);
520 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
521 rtp_state->alt_rtp_tx_sequence++;
522
523 return len;
524}
525
526/* Received TNL primitive from IuUP layer FSM, transmit it further down to the
527 * socket towards destination peer. */
528static int _conn_iuup_transport_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
529{
530 struct mgcp_conn_rtp *conn_rtp_dst = ctx;
531 struct mgcp_conn *conn_dst = conn_rtp_dst->conn;
532 struct osmo_iuup_tnl_prim *itp = (struct osmo_iuup_tnl_prim *)oph;
533 struct mgcp_conn *conn_src;
534 struct msgb *msg;
535 struct rtp_hdr *rtph;
536
537 OSMO_ASSERT(OSMO_PRIM_HDR(&itp->oph) == OSMO_PRIM(OSMO_IUUP_TNL_UNITDATA, PRIM_OP_REQUEST));
538
539 msg = oph->msg;
540 talloc_steal(conn_rtp_dst->conn, msg);
541
542 msgb_pull_to_l2(msg);
543 rtph = (struct rtp_hdr *)msgb_push(msg, sizeof(*rtph));
544 /* TODO: fill rtph properly: */
545 *rtph = (struct rtp_hdr){
546 .csrc_count = 0,
547 .extension = 0,
548 .padding = 0,
549 .version = 2,
550 .payload_type = conn_rtp_dst->end.codec->payload_type,
551 .marker = 0,
552 .sequence = 0,
553 .timestamp = 0,
554 .ssrc = 0
555 };
556
557 /* The destination of the destination conn is the source conn, right? */
558 conn_src = _find_dst_conn(conn_dst);
559 if (!conn_src) {
560 LOG_CONN_RTP(conn_rtp_dst, LOGL_NOTICE,
561 "Couldn't find source conn for IuUP dst conn\n");
562 /* If there's no sister connection we are either still
563 * initializing (so we want to send back Init (ACK)), or we are
564 * probably in loopback mode anyway, so use dst as src. */
565 conn_src = conn_dst;
566 }
567
568 return mgcp_send_iuup(conn_dst->endp, msg, &conn_src->u.rtp, conn_rtp_dst);
569}
570
571/* Used to upgrade a regular RTP connection (MGCP_RTP_DEFAULT) to become a IuUP
572 * connection (MGCP_RTP_IUUP) */
573int mgcp_conn_iuup_init(struct mgcp_conn_rtp *conn_rtp)
574{
575 conn_rtp->type = MGCP_RTP_IUUP;
576 conn_rtp->iuup.iui = osmo_iuup_instance_alloc(conn_rtp->conn, conn_rtp->conn->id);
577 OSMO_ASSERT(conn_rtp->iuup.iui);
578 osmo_iuup_instance_set_user_prim_cb(conn_rtp->iuup.iui, _conn_iuup_user_prim_cb, conn_rtp);
579 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 +0200580 conn_rtp->iuup.rfci_id_no_data = -1;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100581 return 0;
582}
583
584/* Cleanup specific IuUP connection (MGCP_RTP_IUUP) state, allocated by mgcp_conn_iuup_init() */
585void mgcp_conn_iuup_cleanup(struct mgcp_conn_rtp *conn_rtp)
586{
587 osmo_iuup_instance_free(conn_rtp->iuup.iui);
588 conn_rtp->iuup.iui = NULL;
589}
590
591/* Received RTP+IuUP pkt from socket of conn_rtp_src, build a TNL primitive to
592 * push it further up the stack to the IuUP layer FSM to handle and/or bridge it */
593int mgcp_conn_iuup_dispatch_rtp(struct msgb *msg)
594{
595 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
596 struct mgcp_conn_rtp *conn_rtp_src = mc->conn_src;
597 int rc = 0;
598 bool force_output_enabled = false;
599 bool prev_output_enabled;
600 struct osmo_sockaddr prev_rem_addr;
601 uint16_t prev_rem_rtp_port;
602
603 OSMO_ASSERT(mgcp_conn_rtp_is_iuup(conn_rtp_src));
604
605 if ((rc = check_rtp_iuup(conn_rtp_src, msg)) < 0)
606 goto free_ret;
607
608 if (!conn_rtp_src->iuup.configured) {
609 /* We received the first message without sending any, the peer is the active side (RNC). */
610 rc = _conn_iuup_configure_as_passive(conn_rtp_src);
611 if (rc < 0)
612 goto free_ret;
613 /* We need to force allowance of RTP containing Init-ACK back: */
614 prev_output_enabled = conn_rtp_src->end.output_enabled;
615 conn_rtp_src->end.output_enabled = true;
616 force_output_enabled = true;
617 /* Fill in the peer address so that we can send Init-ACK back: */
618 prev_rem_addr = conn_rtp_src->end.addr;
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200619 prev_rem_rtp_port = osmo_sockaddr_port(&conn_rtp_src->end.addr.u.sa);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100620 conn_rtp_src->end.addr = *mc->from_addr;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100621 }
622
623 rc = _conn_iuup_rtp_pl_up(conn_rtp_src, msg);
624
625 if (force_output_enabled) {
626 conn_rtp_src->end.output_enabled = prev_output_enabled;
627 conn_rtp_src->end.addr = prev_rem_addr;
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200628 osmo_sockaddr_set_port(&conn_rtp_src->end.addr.u.sa, prev_rem_rtp_port);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100629 }
630
631 return rc;
632free_ret:
633 msgb_free(msg);
634 return rc;
635}
636
637/* Build IuUP RNL Data primitive from msg containing an incoming RTP pkt from
638 * peer and send it down the IuUP layer towards the destination as IuUP/RTP: */
639int mgcp_conn_iuup_send_rtp(struct mgcp_conn_rtp *conn_src_rtp, struct mgcp_conn_rtp *conn_dest_rtp, struct msgb *msg)
640{
641 struct osmo_iuup_rnl_prim *irp;
642 struct rtp_hdr *rtph;
643 int rc = -1;
644 int iuup_length = 0;
645 int8_t rfci;
646
647 /* Tx RNL-DATA.req */
648 rtph = (struct rtp_hdr *)msgb_data(msg);
649 msgb_pull(msg, sizeof(*rtph));
650
651 /* FIXME: validate amr packets */
652 irp = osmo_iuup_rnl_prim_alloc(conn_dest_rtp->conn, OSMO_IUUP_RNL_DATA, PRIM_OP_REQUEST, MGW_IUUP_MSGB_SIZE);
653 irp->u.data.frame_nr = htons(rtph->sequence) % 16;
654
655 /* TODO: CMR handling & multiple frames handling */
656
657 if (strcmp(conn_src_rtp->end.codec->subtype_name, "AMR") != 0) {
658 LOG_CONN_RTP(conn_src_rtp, LOGL_ERROR,
659 "Bridge RTP=>IuUP: Bridging src codec %s to IuUP AMR not supported\n",
660 conn_src_rtp->end.codec->subtype_name);
661 goto free_ret;
662 }
663 if (mgcp_codec_amr_is_octet_aligned(conn_src_rtp->end.codec)) {
664 struct amr_hdr *amr_hdr = (struct amr_hdr *) msgb_data(msg);
665 if (msgb_length(msg) < (sizeof(*amr_hdr))) {
666 LOG_CONN_RTP(conn_src_rtp, LOGL_NOTICE,
667 "Bridge RTP=>IuUP: too short for AMR OA hdr (%u)\n", msgb_length(msg));
668 goto free_ret;
669 }
670 if (amr_hdr->ft >= AMR_FT_MAX) {
671 LOG_CONN_RTP(conn_src_rtp, LOGL_NOTICE, "Bridge RTP=>IuUP: wrong AMR OA ft=%u\n", amr_hdr->ft);
672 goto free_ret;
673 }
674 if ((rfci = _conn_iuup_amr_ft_2_rfci(conn_dest_rtp, amr_hdr->ft)) < 0) {
675 LOG_CONN_RTP(conn_dest_rtp, LOGL_NOTICE, "Bridge RTP=>IuUP: No RFCI found for AMR OA ft=%u\n", amr_hdr->ft);
676 goto free_ret;
677 }
678 irp->u.data.fqc = amr_hdr->q;
679 irp->u.data.rfci = rfci;
680 msgb_pull(msg, 2);
681 } else {
682 uint8_t *amr_bwe_hdr = (uint8_t *) msgb_data(msg);
683 int8_t ft;
684 if (msgb_length(msg) < 2) {
685 LOG_CONN_RTP(conn_src_rtp, LOGL_NOTICE,
686 "Bridge RTP=>IuUP: too short for AMR BE hdr (%u)\n", msgb_length(msg));
687 goto free_ret;
688 }
689 ft = ((amr_bwe_hdr[0] & 0x07) << 1) | ((amr_bwe_hdr[1] & 0x80) >> 7);
690 if (ft >= AMR_FT_MAX) {
691 LOG_CONN_RTP(conn_src_rtp, LOGL_NOTICE, "Bridge RTP=>IuUP: wrong AMR BE ft=%u\n", ft);
692 goto free_ret;
693 }
694 if ((rfci = _conn_iuup_amr_ft_2_rfci(conn_dest_rtp, ft)) < 0) {
695 LOG_CONN_RTP(conn_dest_rtp, LOGL_NOTICE, "Bridge RTP=>IuUP: No RFCI found for AMR BE ft=%u\n", ft);
696 goto free_ret;
697 }
698 irp->u.data.fqc = ((amr_bwe_hdr[1] & 0x40) >> 6);
699 irp->u.data.rfci = rfci;
700 rc = iuup_length = osmo_amr_bwe_to_iuup(msgb_data(msg), msgb_length(msg));
701 if (rc < 0) {
702 LOG_CONN_RTP(conn_dest_rtp, LOGL_ERROR, "Bridge RTP=>IuUP: Failed convert the RTP/AMR to IuUP payload\n");
703 return rc;
704 }
705 msgb_trim(msg, iuup_length);
706 }
707
708 irp->oph.msg->l3h = msgb_put(irp->oph.msg, msgb_length(msg));
709 memcpy(irp->oph.msg->l3h, msgb_data(msg), msgb_length(msg));
710 if ((rc = osmo_iuup_rnl_prim_down(conn_dest_rtp->iuup.iui, irp)) != 0)
711 LOG_CONN_RTP(conn_dest_rtp, LOGL_ERROR, "Bridge RTP=>IuUP: Failed Tx RTP payload down the IuUP layer\n");
712 return rc;
713
714free_ret:
715 msgb_free(irp->oph.msg);
716 return -1;
717}
718
719/* Build IuUP RNL Data primitive from msg containing dummy content and send it
720 * down the IuUP layer towards the destination as IuUP/RTP: */
721int mgcp_conn_iuup_send_dummy(struct mgcp_conn_rtp *conn_rtp)
722{
723 struct osmo_iuup_rnl_prim *irp;
724 int rc;
725
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200726 if (conn_rtp->iuup.rfci_id_no_data == -1) {
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100727 LOG_CONN_RTP(conn_rtp, LOGL_NOTICE, "No RFCI NO_DATA found, unable to send dummy packet\n");
728 return -ENOTSUP;
729 }
730
731 irp = osmo_iuup_rnl_prim_alloc(conn_rtp->conn, OSMO_IUUP_RNL_DATA, PRIM_OP_REQUEST, MGW_IUUP_MSGB_SIZE);
732 irp->u.data.frame_nr = 0;
733 irp->u.data.fqc = IUUP_FQC_FRAME_GOOD;
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200734 irp->u.data.rfci = conn_rtp->iuup.rfci_id_no_data;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100735 irp->oph.msg->l3h = irp->oph.msg->tail;
736 if ((rc = osmo_iuup_rnl_prim_down(conn_rtp->iuup.iui, irp)) != 0) {
737 LOG_CONN_RTP(conn_rtp, LOGL_ERROR, "Failed Tx RTP dummy payload down the IuUP layer\n");
738 return -EINVAL;
739 }
740
741 return 0;
742}