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