blob: 15d674d554a8ec6a2e4f39cee74238e851110052 [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 Pedrolce055d52022-05-25 18:34:06 +0200277 LOGP(DLMGCP, LOGL_DEBUG, "Convert IuUP -> AMR: ft %d, len %d\n", ft, msgb_l3len(msg));
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100278
279 if (mgcp_codec_amr_is_octet_aligned(conn_rtp_dst->end.codec)) {
280 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);
298 }
299 rtp_hdr = (struct rtp_hdr *) msgb_push(msg, sizeof(*rtp_hdr));
300 *rtp_hdr = (struct rtp_hdr){
301 .csrc_count = 0,
302 .extension = 0,
303 .padding = 0,
304 .version = 0,
305 .payload_type = conn_rtp_dst->end.codec->payload_type,
306 .marker = 0,
307 .sequence = frame_nr,
308 .timestamp = 0,
309 .ssrc = 0
310 };
311
312 rc = mgcp_send(conn_rtp_dst->conn->endp, true, NULL, msg, conn_rtp_src, conn_rtp_dst);
313 msgb_free(msg);
314 return rc;
315}
316
317/* Handle RNL Data primitive received from the IuUP layer FSM: Bridge it to the
318 * sister connection in the endpoint: */
319static int _conn_iuup_rx_rnl_data(struct mgcp_conn_rtp *conn_rtp_src, struct osmo_iuup_rnl_prim *irp)
320{
321 struct mgcp_conn *conn_dst;
322 struct mgcp_conn_rtp *conn_rtp_dst;
323 int rc;
324
325 conn_dst = _find_dst_conn(conn_rtp_src->conn);
326
327 /* There is no destination conn, stop here */
328 if (!conn_dst) {
329 LOGPCONN(conn_rtp_src->conn, DRTP, LOGL_DEBUG,
330 "no connection to forward an incoming IuUP payload to\n");
331 rc = -1;
332 goto free_ret;
333 }
334
335 /* The destination conn is not an RTP/IuUP connection */
336 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
337 LOGPCONN(conn_rtp_src->conn, DRTP, LOGL_ERROR,
338 "unable to find suitable destination conn\n");
339 rc = -1;
340 goto free_ret;
341 }
342 conn_rtp_dst = &conn_dst->u.rtp;
343
344 switch (conn_rtp_dst->type) {
345 case MGCP_RTP_IUUP:
346 return bridge_iuup_to_iuup_peer(conn_rtp_src, conn_rtp_dst, irp);
347 case MGCP_RTP_DEFAULT:
348 return bridge_iuup_to_rtp_peer(conn_rtp_src, conn_rtp_dst, irp);
Pau Espin Pedrol9d939b62022-10-03 16:59:20 +0200349 case MGCP_RTP_OSMUX:
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100350 default:
351 LOGPCONN(conn_rtp_src->conn, DRTP, LOGL_ERROR,
352 "Forward of IuUP payload to RTP connection type %u not supported!\n",
353 conn_rtp_dst->type);
354 rc = 0;
355 }
356
357free_ret:
358 msgb_free(irp->oph.msg);
359 return rc;
360}
361
362/* Handle RNL Status-Init primitive received from the IuUP layer FSM.
363 * Potentially configure sister conn as IuUP Init-Active: */
364static int _conn_iuup_rx_rnl_status_init(struct mgcp_conn_rtp *conn_rtp_src, struct osmo_iuup_rnl_prim *irp)
365{
366 struct mgcp_conn *conn_dst;
367 struct mgcp_conn_rtp *conn_rtp_dst;
368 int rc = 0;
369 struct msgb *msg;
370
Pau Espin Pedrola02faff2022-06-13 14:07:22 +0200371 if (conn_rtp_src->iuup.init_ind) {
372 /* We received more than one IuUP Initialization. It's probably
373 * a retransmission, so simply ignore it (lower layers take care
374 * of ACKing it). */
375 LOGPCONN(conn_rtp_src->conn, DRTP, LOGL_INFO,
376 "Ignoring potential IuUP Initialization retrans\n");
377 return 0;
378 }
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100379
380 msg = msgb_copy_c(conn_rtp_src->conn, irp->oph.msg, "iuup-init-copy");
381 conn_rtp_src->iuup.init_ind = (struct osmo_iuup_rnl_prim *)msgb_data(msg);
382 conn_rtp_src->iuup.init_ind->oph.msg = msg;
383
Pau Espin Pedrola02faff2022-06-13 14:07:22 +0200384 /* Find RFCI containing NO_DATA: */
385 conn_rtp_src->iuup.rfci_id_no_data = _find_rfci_no_data(irp);
386
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100387 conn_dst = _find_dst_conn(conn_rtp_src->conn);
388 /* If not yet there, peer will potentially be IuUP-Initialized later
389 * when we attempt to bridge audio towards it. See bridge_iuup_to_iuup_peer() */
390 if (!conn_dst)
391 return 0;
392 conn_rtp_dst = &conn_dst->u.rtp;
393 if (!mgcp_conn_rtp_is_iuup(conn_rtp_dst))
394 return 0; /* Nothing to do */
395
396 /* We received IuUP parameters on the peer (RNC), Init actively this conn (against CN): */
397 if (!conn_rtp_dst->iuup.configured)
398 rc = _conn_iuup_configure_as_active(conn_rtp_dst, irp);
399
400 return rc;
401}
402
403/* Handle RNL Status primitives received from the IuUP layer FSM: */
404static int _conn_iuup_rx_rnl_status(struct mgcp_conn_rtp *conn_rtp_src, struct osmo_iuup_rnl_prim *irp)
405{
406 int rc;
407
408 switch (irp->u.status.procedure) {
409 case IUUP_PROC_INIT:
410 rc = _conn_iuup_rx_rnl_status_init(conn_rtp_src, irp);
411 break;
412 case IUUP_PROC_RATE_CTRL:
413 case IUUP_PROC_TIME_ALIGN:
414 case IUUP_PROC_ERR_EVENT:
415 default:
416 LOG_CONN_RTP(conn_rtp_src, LOGL_ERROR,
417 "Received IuUP RNL STATUS procedure type %u not handled\n",
418 irp->u.status.procedure);
419 rc = 0;
420 }
421
422 return rc;
423}
424
425/* Received RNL primitive from the IuUP layer FSM containing IuUP Status or
426 * data. Continue pushing it up the stack, either IuUP Status or Data: */
427static int _conn_iuup_user_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
428{
429 struct mgcp_conn_rtp *conn_rtp_src = ctx;
430 struct osmo_iuup_rnl_prim *irp = (struct osmo_iuup_rnl_prim *)oph;
431 struct msgb *msg = oph->msg;
432 int rc;
433
434 switch (OSMO_PRIM_HDR(&irp->oph)) {
435 case OSMO_PRIM(OSMO_IUUP_RNL_DATA, PRIM_OP_INDICATION):
436 /* we pass ownsership of msg here: */
437 rc = _conn_iuup_rx_rnl_data(conn_rtp_src, irp);
438 break;
439 case OSMO_PRIM(OSMO_IUUP_RNL_STATUS, PRIM_OP_INDICATION):
440 rc = _conn_iuup_rx_rnl_status(conn_rtp_src, irp);
441 msgb_free(msg);
442 break;
443 default:
444 msgb_free(msg);
445 OSMO_ASSERT(false);
446 }
447
448 return rc;
449}
450
451/*! Send |RTP+IuUP| data down the stack of the specified destination connection.
452 * \param[in] endp associated endpoint (for configuration, logging).
453 * \param[in] buf buffer that contains the |RTP+IuUP| data.
454 * \param[in] len length of the buffer that contains the |RTP+IuUP| data.
455 * \param[in] conn_src associated source connection.
456 * \param[in] conn_dst associated destination connection.
457 * \returns 0 on success, -1 on ERROR. */
458static int mgcp_send_iuup(struct mgcp_endpoint *endp, struct msgb *msg,
459 struct mgcp_conn_rtp *conn_src, struct mgcp_conn_rtp *conn_dst)
460{
461 /*! When no destination connection is available (e.g. when only one
462 * connection in loopback mode exists), then the source connection
463 * shall be specified as destination connection */
464
465 struct mgcp_rtp_end *rtp_end;
466 struct mgcp_rtp_state *rtp_state;
467 char ipbuf[INET6_ADDRSTRLEN];
468 struct rtp_hdr *hdr = (struct rtp_hdr *)msgb_data(msg);
469 int buflen = msgb_length(msg);
470 char *dest_name;
471 int len;
472
473 OSMO_ASSERT(conn_src);
474 OSMO_ASSERT(conn_dst);
475
476 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering IuUP packet...\n");
477
478 /* Note: In case of loopback configuration, both, the source and the
479 * destination will point to the same connection. */
480 rtp_end = &conn_dst->end;
481 rtp_state = &conn_src->state;
482 dest_name = conn_dst->conn->name;
483
484 /* Ensure we have an alternative SSRC in case we need it, see also
485 * gen_rtp_header() */
486 if (rtp_state->alt_rtp_tx_ssrc == 0)
487 rtp_state->alt_rtp_tx_ssrc = rand();
488
489 if (!rtp_end->output_enabled) {
490 rtpconn_rate_ctr_add(conn_dst, endp, RTP_DROPPED_PACKETS_CTR, 1);
491 LOGPENDP(endp, DRTP, LOGL_DEBUG,
492 "output disabled, drop to %s %s "
493 "rtp_port:%u rtcp_port:%u\n",
494 dest_name,
495 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200496 osmo_sockaddr_port(&rtp_end->addr.u.sa), ntohs(rtp_end->rtcp_port)
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100497 );
498 return 0;
499 }
500
501 /* Specs say, in IuUP, the RTP seqnum and timestamp should actually be
502 * ignored by the receiver, but still it's useful for debug purposes
503 * to set it. Moreover, it seems ip.access nano3g produces much worse
504 * audio output on the air side if timestamp is not set properly. */
505 hdr->timestamp = osmo_htonl(mgcp_get_current_ts(rtp_end->codec->rate));
506 hdr->sequence = osmo_htons(rtp_state->alt_rtp_tx_sequence);
507 hdr->ssrc = rtp_state->alt_rtp_tx_ssrc;
508
509 LOGPENDP(endp, DRTP, LOGL_DEBUG,
510 "process/send IuUP to %s %s rtp_port:%u rtcp_port:%u\n",
511 dest_name, osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200512 osmo_sockaddr_port(&rtp_end->addr.u.sa), ntohs(rtp_end->rtcp_port));
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100513
514 /* Forward a copy of the RTP data to a debug ip/port */
515 forward_data_tap(rtp_end->rtp.fd, &conn_src->tap_out,
516 msg);
517
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200518 len = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr, (char *)hdr, buflen);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100519
520 if (len <= 0)
521 return len;
522
523 rtpconn_rate_ctr_add(conn_dst, endp, RTP_PACKETS_TX_CTR, 1);
524 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
525 rtp_state->alt_rtp_tx_sequence++;
526
527 return len;
528}
529
530/* Received TNL primitive from IuUP layer FSM, transmit it further down to the
531 * socket towards destination peer. */
532static int _conn_iuup_transport_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
533{
534 struct mgcp_conn_rtp *conn_rtp_dst = ctx;
535 struct mgcp_conn *conn_dst = conn_rtp_dst->conn;
536 struct osmo_iuup_tnl_prim *itp = (struct osmo_iuup_tnl_prim *)oph;
537 struct mgcp_conn *conn_src;
538 struct msgb *msg;
539 struct rtp_hdr *rtph;
540
541 OSMO_ASSERT(OSMO_PRIM_HDR(&itp->oph) == OSMO_PRIM(OSMO_IUUP_TNL_UNITDATA, PRIM_OP_REQUEST));
542
543 msg = oph->msg;
544 talloc_steal(conn_rtp_dst->conn, msg);
545
546 msgb_pull_to_l2(msg);
547 rtph = (struct rtp_hdr *)msgb_push(msg, sizeof(*rtph));
548 /* TODO: fill rtph properly: */
549 *rtph = (struct rtp_hdr){
550 .csrc_count = 0,
551 .extension = 0,
552 .padding = 0,
553 .version = 2,
554 .payload_type = conn_rtp_dst->end.codec->payload_type,
555 .marker = 0,
556 .sequence = 0,
557 .timestamp = 0,
558 .ssrc = 0
559 };
560
561 /* The destination of the destination conn is the source conn, right? */
562 conn_src = _find_dst_conn(conn_dst);
563 if (!conn_src) {
564 LOG_CONN_RTP(conn_rtp_dst, LOGL_NOTICE,
565 "Couldn't find source conn for IuUP dst conn\n");
566 /* If there's no sister connection we are either still
567 * initializing (so we want to send back Init (ACK)), or we are
568 * probably in loopback mode anyway, so use dst as src. */
569 conn_src = conn_dst;
570 }
571
572 return mgcp_send_iuup(conn_dst->endp, msg, &conn_src->u.rtp, conn_rtp_dst);
573}
574
575/* Used to upgrade a regular RTP connection (MGCP_RTP_DEFAULT) to become a IuUP
576 * connection (MGCP_RTP_IUUP) */
577int mgcp_conn_iuup_init(struct mgcp_conn_rtp *conn_rtp)
578{
579 conn_rtp->type = MGCP_RTP_IUUP;
580 conn_rtp->iuup.iui = osmo_iuup_instance_alloc(conn_rtp->conn, conn_rtp->conn->id);
581 OSMO_ASSERT(conn_rtp->iuup.iui);
582 osmo_iuup_instance_set_user_prim_cb(conn_rtp->iuup.iui, _conn_iuup_user_prim_cb, conn_rtp);
583 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 +0200584 conn_rtp->iuup.rfci_id_no_data = -1;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100585 return 0;
586}
587
588/* Cleanup specific IuUP connection (MGCP_RTP_IUUP) state, allocated by mgcp_conn_iuup_init() */
589void mgcp_conn_iuup_cleanup(struct mgcp_conn_rtp *conn_rtp)
590{
591 osmo_iuup_instance_free(conn_rtp->iuup.iui);
592 conn_rtp->iuup.iui = NULL;
593}
594
595/* Received RTP+IuUP pkt from socket of conn_rtp_src, build a TNL primitive to
596 * push it further up the stack to the IuUP layer FSM to handle and/or bridge it */
597int mgcp_conn_iuup_dispatch_rtp(struct msgb *msg)
598{
599 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
600 struct mgcp_conn_rtp *conn_rtp_src = mc->conn_src;
601 int rc = 0;
602 bool force_output_enabled = false;
603 bool prev_output_enabled;
604 struct osmo_sockaddr prev_rem_addr;
605 uint16_t prev_rem_rtp_port;
606
607 OSMO_ASSERT(mgcp_conn_rtp_is_iuup(conn_rtp_src));
608
609 if ((rc = check_rtp_iuup(conn_rtp_src, msg)) < 0)
610 goto free_ret;
611
612 if (!conn_rtp_src->iuup.configured) {
613 /* We received the first message without sending any, the peer is the active side (RNC). */
614 rc = _conn_iuup_configure_as_passive(conn_rtp_src);
615 if (rc < 0)
616 goto free_ret;
617 /* We need to force allowance of RTP containing Init-ACK back: */
618 prev_output_enabled = conn_rtp_src->end.output_enabled;
619 conn_rtp_src->end.output_enabled = true;
620 force_output_enabled = true;
621 /* Fill in the peer address so that we can send Init-ACK back: */
622 prev_rem_addr = conn_rtp_src->end.addr;
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200623 prev_rem_rtp_port = osmo_sockaddr_port(&conn_rtp_src->end.addr.u.sa);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100624 conn_rtp_src->end.addr = *mc->from_addr;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100625 }
626
627 rc = _conn_iuup_rtp_pl_up(conn_rtp_src, msg);
628
629 if (force_output_enabled) {
630 conn_rtp_src->end.output_enabled = prev_output_enabled;
631 conn_rtp_src->end.addr = prev_rem_addr;
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200632 osmo_sockaddr_set_port(&conn_rtp_src->end.addr.u.sa, prev_rem_rtp_port);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100633 }
634
635 return rc;
636free_ret:
637 msgb_free(msg);
638 return rc;
639}
640
641/* Build IuUP RNL Data primitive from msg containing an incoming RTP pkt from
642 * peer and send it down the IuUP layer towards the destination as IuUP/RTP: */
643int mgcp_conn_iuup_send_rtp(struct mgcp_conn_rtp *conn_src_rtp, struct mgcp_conn_rtp *conn_dest_rtp, struct msgb *msg)
644{
645 struct osmo_iuup_rnl_prim *irp;
646 struct rtp_hdr *rtph;
647 int rc = -1;
648 int iuup_length = 0;
649 int8_t rfci;
650
651 /* Tx RNL-DATA.req */
652 rtph = (struct rtp_hdr *)msgb_data(msg);
653 msgb_pull(msg, sizeof(*rtph));
654
655 /* FIXME: validate amr packets */
656 irp = osmo_iuup_rnl_prim_alloc(conn_dest_rtp->conn, OSMO_IUUP_RNL_DATA, PRIM_OP_REQUEST, MGW_IUUP_MSGB_SIZE);
657 irp->u.data.frame_nr = htons(rtph->sequence) % 16;
658
659 /* TODO: CMR handling & multiple frames handling */
660
661 if (strcmp(conn_src_rtp->end.codec->subtype_name, "AMR") != 0) {
662 LOG_CONN_RTP(conn_src_rtp, LOGL_ERROR,
663 "Bridge RTP=>IuUP: Bridging src codec %s to IuUP AMR not supported\n",
664 conn_src_rtp->end.codec->subtype_name);
665 goto free_ret;
666 }
667 if (mgcp_codec_amr_is_octet_aligned(conn_src_rtp->end.codec)) {
668 struct amr_hdr *amr_hdr = (struct amr_hdr *) msgb_data(msg);
669 if (msgb_length(msg) < (sizeof(*amr_hdr))) {
670 LOG_CONN_RTP(conn_src_rtp, LOGL_NOTICE,
671 "Bridge RTP=>IuUP: too short for AMR OA hdr (%u)\n", msgb_length(msg));
672 goto free_ret;
673 }
674 if (amr_hdr->ft >= AMR_FT_MAX) {
675 LOG_CONN_RTP(conn_src_rtp, LOGL_NOTICE, "Bridge RTP=>IuUP: wrong AMR OA ft=%u\n", amr_hdr->ft);
676 goto free_ret;
677 }
678 if ((rfci = _conn_iuup_amr_ft_2_rfci(conn_dest_rtp, amr_hdr->ft)) < 0) {
679 LOG_CONN_RTP(conn_dest_rtp, LOGL_NOTICE, "Bridge RTP=>IuUP: No RFCI found for AMR OA ft=%u\n", amr_hdr->ft);
680 goto free_ret;
681 }
682 irp->u.data.fqc = amr_hdr->q;
683 irp->u.data.rfci = rfci;
684 msgb_pull(msg, 2);
685 } else {
686 uint8_t *amr_bwe_hdr = (uint8_t *) msgb_data(msg);
687 int8_t ft;
688 if (msgb_length(msg) < 2) {
689 LOG_CONN_RTP(conn_src_rtp, LOGL_NOTICE,
690 "Bridge RTP=>IuUP: too short for AMR BE hdr (%u)\n", msgb_length(msg));
691 goto free_ret;
692 }
693 ft = ((amr_bwe_hdr[0] & 0x07) << 1) | ((amr_bwe_hdr[1] & 0x80) >> 7);
694 if (ft >= AMR_FT_MAX) {
695 LOG_CONN_RTP(conn_src_rtp, LOGL_NOTICE, "Bridge RTP=>IuUP: wrong AMR BE ft=%u\n", ft);
696 goto free_ret;
697 }
698 if ((rfci = _conn_iuup_amr_ft_2_rfci(conn_dest_rtp, ft)) < 0) {
699 LOG_CONN_RTP(conn_dest_rtp, LOGL_NOTICE, "Bridge RTP=>IuUP: No RFCI found for AMR BE ft=%u\n", ft);
700 goto free_ret;
701 }
702 irp->u.data.fqc = ((amr_bwe_hdr[1] & 0x40) >> 6);
703 irp->u.data.rfci = rfci;
704 rc = iuup_length = osmo_amr_bwe_to_iuup(msgb_data(msg), msgb_length(msg));
705 if (rc < 0) {
706 LOG_CONN_RTP(conn_dest_rtp, LOGL_ERROR, "Bridge RTP=>IuUP: Failed convert the RTP/AMR to IuUP payload\n");
707 return rc;
708 }
709 msgb_trim(msg, iuup_length);
710 }
711
712 irp->oph.msg->l3h = msgb_put(irp->oph.msg, msgb_length(msg));
713 memcpy(irp->oph.msg->l3h, msgb_data(msg), msgb_length(msg));
714 if ((rc = osmo_iuup_rnl_prim_down(conn_dest_rtp->iuup.iui, irp)) != 0)
715 LOG_CONN_RTP(conn_dest_rtp, LOGL_ERROR, "Bridge RTP=>IuUP: Failed Tx RTP payload down the IuUP layer\n");
716 return rc;
717
718free_ret:
719 msgb_free(irp->oph.msg);
720 return -1;
721}
722
723/* Build IuUP RNL Data primitive from msg containing dummy content and send it
724 * down the IuUP layer towards the destination as IuUP/RTP: */
725int mgcp_conn_iuup_send_dummy(struct mgcp_conn_rtp *conn_rtp)
726{
727 struct osmo_iuup_rnl_prim *irp;
728 int rc;
729
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200730 if (conn_rtp->iuup.rfci_id_no_data == -1) {
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100731 LOG_CONN_RTP(conn_rtp, LOGL_NOTICE, "No RFCI NO_DATA found, unable to send dummy packet\n");
732 return -ENOTSUP;
733 }
734
735 irp = osmo_iuup_rnl_prim_alloc(conn_rtp->conn, OSMO_IUUP_RNL_DATA, PRIM_OP_REQUEST, MGW_IUUP_MSGB_SIZE);
736 irp->u.data.frame_nr = 0;
737 irp->u.data.fqc = IUUP_FQC_FRAME_GOOD;
Pau Espin Pedrol452f2ba2022-05-24 19:15:54 +0200738 irp->u.data.rfci = conn_rtp->iuup.rfci_id_no_data;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100739 irp->oph.msg->l3h = irp->oph.msg->tail;
740 if ((rc = osmo_iuup_rnl_prim_down(conn_rtp->iuup.iui, irp)) != 0) {
741 LOG_CONN_RTP(conn_rtp, LOGL_ERROR, "Failed Tx RTP dummy payload down the IuUP layer\n");
742 return -EINVAL;
743 }
744
745 return 0;
746}