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