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