pespin has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-mgw/+/26823 )
Change subject: WIP: Initial IuUP support using proper FSMs ......................................................................
WIP: Initial IuUP support using proper FSMs
Related: OS#1937 Depends: libosmocore Change-Id I63ee780b4aa162ea097410b234e73984000c0965 Change-Id: I6694a21480b25ab8f35d375295be6601ce38e31d --- M include/osmocom/mgcp/Makefile.am M include/osmocom/mgcp/mgcp_conn.h A include/osmocom/mgcp/mgcp_iuup.h M include/osmocom/mgcp/mgcp_network.h M src/libosmo-mgcp/Makefile.am M src/libosmo-mgcp/mgcp_conn.c A src/libosmo-mgcp/mgcp_iuup.c M src/libosmo-mgcp/mgcp_network.c M src/libosmo-mgcp/mgcp_protocol.c 9 files changed, 671 insertions(+), 28 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-mgw refs/changes/23/26823/1
diff --git a/include/osmocom/mgcp/Makefile.am b/include/osmocom/mgcp/Makefile.am index b94cdcd..60f29c6 100644 --- a/include/osmocom/mgcp/Makefile.am +++ b/include/osmocom/mgcp/Makefile.am @@ -13,4 +13,5 @@ mgcp_e1.h \ mgcp_network.h \ mgcp_protocol.h \ + mgcp_iuup.h \ $(NULL) diff --git a/include/osmocom/mgcp/mgcp_conn.h b/include/osmocom/mgcp/mgcp_conn.h index 4f882e9..22e4d52 100644 --- a/include/osmocom/mgcp/mgcp_conn.h +++ b/include/osmocom/mgcp/mgcp_conn.h @@ -28,6 +28,7 @@ #include <osmocom/mgcp/osmux.h> #include <osmocom/core/linuxlist.h> #include <osmocom/core/rate_ctr.h> +#include <osmocom/gsm/iuup.h> #include <inttypes.h>
#define LOGPCONN(conn, cat, level, fmt, args...) \ @@ -47,6 +48,7 @@ MGCP_RTP_DEFAULT = 0, MGCP_OSMUX_BSC, MGCP_OSMUX_BSC_NAT, + MGCP_RTP_IUUP, };
/*! Connection type, specifies which member of the union "u" in mgcp_conn @@ -93,6 +95,14 @@ } stats; } osmux;
+ struct { + struct osmo_iuup_instance *iui; + bool active_init; /* true: Send IuUP Init */ + int rfci_idx_no_data; /* Index for RFCI NO_DATA (-1 if not available) */ + bool configured; + struct osmo_iuup_rnl_prim *init_ind; + } iuup; + struct rate_ctr_group *rate_ctr_group; };
@@ -176,6 +186,11 @@ return conn->type == MGCP_OSMUX_BSC || conn->type == MGCP_OSMUX_BSC_NAT; }
+/* Was conn configured to handle Osmux? */ +static inline bool mgcp_conn_rtp_is_iuup(const struct mgcp_conn_rtp *conn) { + return conn->type == MGCP_RTP_IUUP; +} + struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp, enum mgcp_conn_type type, char *name); struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id); diff --git a/include/osmocom/mgcp/mgcp_iuup.h b/include/osmocom/mgcp/mgcp_iuup.h new file mode 100644 index 0000000..e0708f5 --- /dev/null +++ b/include/osmocom/mgcp/mgcp_iuup.h @@ -0,0 +1,33 @@ +/* Message connection list handling */ + +/* + * (C) 2021 by sysmocom s.f.m.c. GmbH info@sysmocom.de + * All Rights Reserved + * + * Author: Pau Espin Pedrol + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + * + */ +#pragma once + +#include <osmocom/core/msgb.h> + +struct mgcp_conn_rtp; + +int mgcp_conn_iuup_init(struct mgcp_conn_rtp *conn_rtp); +void mgcp_conn_iuup_cleanup(struct mgcp_conn_rtp *conn_rtp); +int mgcp_conn_iuup_dispatch_rtp(struct msgb *msg); +int mgcp_conn_iuup_send_rtp(struct mgcp_conn_rtp *conn_src_rtp, struct mgcp_conn_rtp *conn_dest_rtp, struct msgb *msg); +int mgcp_conn_iuup_send_dummy(struct mgcp_conn_rtp *conn_rtp); diff --git a/include/osmocom/mgcp/mgcp_network.h b/include/osmocom/mgcp/mgcp_network.h index e3fa3b1..fa1c3e8 100644 --- a/include/osmocom/mgcp/mgcp_network.h +++ b/include/osmocom/mgcp/mgcp_network.h @@ -70,8 +70,6 @@ * data is just re-used) */ uint16_t alt_rtp_tx_sequence; uint32_t alt_rtp_tx_ssrc; - - bool patched_first_rtp_payload; /* FIXME: drop this, see OS#2459 */ };
struct mgcp_rtp_codec { diff --git a/src/libosmo-mgcp/Makefile.am b/src/libosmo-mgcp/Makefile.am index 91b2bf6..de42c0e 100644 --- a/src/libosmo-mgcp/Makefile.am +++ b/src/libosmo-mgcp/Makefile.am @@ -48,4 +48,5 @@ mgcp_ctrl.c \ mgcp_ratectr.c \ mgcp_e1.c \ + mgcp_iuup.c \ $(NULL) diff --git a/src/libosmo-mgcp/mgcp_conn.c b/src/libosmo-mgcp/mgcp_conn.c index 9c2fb0f..6dc0199 100644 --- a/src/libosmo-mgcp/mgcp_conn.c +++ b/src/libosmo-mgcp/mgcp_conn.c @@ -30,6 +30,8 @@ #include <osmocom/mgcp/mgcp_trunk.h> #include <osmocom/mgcp/mgcp_sdp.h> #include <osmocom/mgcp/mgcp_codec.h> +#include <osmocom/mgcp/mgcp_iuup.h> + #include <osmocom/gsm/gsm_utils.h> #include <osmocom/core/rate_ctr.h> #include <osmocom/core/timer.h> @@ -129,6 +131,8 @@ { if (mgcp_conn_rtp_is_osmux(conn_rtp)) conn_osmux_disable(conn_rtp); + if (mgcp_conn_rtp_is_iuup(conn_rtp)) + mgcp_conn_iuup_cleanup(conn_rtp); mgcp_free_rtp_port(&conn_rtp->end); rate_ctr_group_free(conn_rtp->rate_ctr_group); mgcp_codec_reset_all(conn_rtp); diff --git a/src/libosmo-mgcp/mgcp_iuup.c b/src/libosmo-mgcp/mgcp_iuup.c new file mode 100644 index 0000000..ab83f08 --- /dev/null +++ b/src/libosmo-mgcp/mgcp_iuup.c @@ -0,0 +1,584 @@ +/* + * (C) 2021 by sysmocom s.f.m.c. GmbH info@sysmocom.de + * All rights not specifically granted under this license are reserved. + * + * Author: Pau Espin Pedrol + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by the + * Free Software Foundation; either version 3 of the License, or (at your + * option) any later version. + */ + +#include <stdint.h> + +#include <osmocom/gsm/iuup.h> + +#include <osmocom/netif/rtp.h> +#include <osmocom/netif/amr.h> + +#include <osmocom/mgcp/mgcp_conn.h> +#include <osmocom/mgcp/mgcp_iuup.h> +#include <osmocom/mgcp/mgcp_endp.h> +#include <osmocom/mgcp/mgcp_codec.h> +#include <osmocom/mgcp/debug.h> + +#define MGW_IUUP_MSGB_SIZE 4096 + +static struct osmo_iuup_rnl_config def_configure_req = { + .transparent = false, + .active = true, + .supported_versions_mask = 0x0003, + .num_rfci = 0, + .num_subflows = 0, + /* .delivery_err_sdu = All set to 0 (YES) by default, */ + .IPTIs_present = false, + .t_init = { .t_ms = IUUP_TIMER_INIT_T_DEFAULT, .n_max = IUUP_TIMER_INIT_N_DEFAULT }, + .t_ta = { .t_ms = IUUP_TIMER_TA_T_DEFAULT, .n_max = IUUP_TIMER_TA_N_DEFAULT }, + .t_rc = { .t_ms = IUUP_TIMER_RC_T_DEFAULT, .n_max = IUUP_TIMER_RC_N_DEFAULT }, +}; + +static struct mgcp_conn *_find_dst_conn(struct mgcp_conn *conn) +{ + /* Find a destination connection. */ + /* NOTE: This code path runs every time an RTP packet is received. The + * function mgcp_find_dst_conn() we use to determine the detination + * connection will iterate the connection list inside the endpoint. + * Since list iterations are quite costly, we will figure out the + * destination only once and use the optional private data pointer of + * the connection to cache the destination connection pointer. */ + + struct mgcp_conn *conn_dst; + if (!conn->priv) { + conn_dst = mgcp_find_dst_conn(conn); + conn->priv = conn_dst; + } else { + conn_dst = (struct mgcp_conn *)conn->priv; + } + return conn_dst; +} + +/* Find RFCI containing all 0 sizes, -1 if not found. irp is an Initialization.ind prim */ +static int _find_rfci_no_data(struct osmo_iuup_rnl_prim *irp) +{ + int i; + /* Find RFCI containing NO_DATA: */ + for (i = 0; i < irp->u.status.u.initialization.num_rfci; i++) { + int j; + bool is_no_data = true; + for (j = 0; j < irp->u.status.u.initialization.num_subflows; j++) { + if (irp->u.status.u.initialization.subflow_sizes[i][j]) { + is_no_data = false; + break; + } + } + if (is_no_data) { + return i; + } + } + return -1; +} + +/* -1 if none found */ +static int8_t _conn_iuup_amr_ft_2_rfci(struct mgcp_conn_rtp *conn_rtp, uint8_t ft) +{ + int8_t i; + unsigned match_bytes = (unsigned)osmo_amr_bytes(ft); + struct osmo_iuup_rnl_prim *irp = conn_rtp->iuup.init_ind; + OSMO_ASSERT(irp); + + /* TODO: cache this somehow */ + for (i = 0; i < irp->u.status.u.initialization.num_rfci; i++) { + int j; + unsigned num_bits = 0; + for (j = 0; j < irp->u.status.u.initialization.num_subflows; j++) + num_bits += irp->u.status.u.initialization.subflow_sizes[i][j]; + if (match_bytes == (num_bits + 7)/8) + return i; + } + + return -1; +} + +static int _conn_iuup_configure_as_passive(struct mgcp_conn_rtp *conn_rtp, struct msgb *msg) +{ + struct osmo_iuup_rnl_prim *irp; + int rc; + + conn_rtp->iuup.active_init = false; + + /* Tx CONFIG.req */ + irp = osmo_iuup_rnl_prim_alloc(conn_rtp->conn, OSMO_IUUP_RNL_CONFIG, PRIM_OP_REQUEST, MGW_IUUP_MSGB_SIZE); + irp->u.config = def_configure_req; + irp->u.config.active = conn_rtp->iuup.active_init; + if ((rc = osmo_iuup_rnl_prim_down(conn_rtp->iuup.iui, irp)) == 0) + conn_rtp->iuup.configured = true; + else + LOG_CONN_RTP(conn_rtp, LOGL_ERROR, "Failed configuring IuUP layer\n"); + return rc; +} + +static int _conn_iuup_configure_as_active(struct mgcp_conn_rtp *conn_rtp, struct osmo_iuup_rnl_prim *init_ind) +{ + struct osmo_iuup_rnl_prim *irp = init_ind; + struct osmo_iuup_rnl_prim *irp2; + struct msgb *msg; + bool prev_output_enabled; + int rc; + + conn_rtp->iuup.active_init = true; + + /* Find RFCI containing NO_DATA: */ + conn_rtp->iuup.rfci_idx_no_data = _find_rfci_no_data(init_ind); + + /* Copy over the rfci_idx_no_data, since we reuse the same subflow set: */ + msg = msgb_copy_c(conn_rtp->conn, irp->oph.msg, "iuup-init-copy"); + conn_rtp->iuup.init_ind = (struct osmo_iuup_rnl_prim *)msgb_data(msg); + conn_rtp->iuup.init_ind->oph.msg = msg; + + /* Tx CONFIG.req */ + irp2 = osmo_iuup_rnl_prim_alloc(conn_rtp->conn, OSMO_IUUP_RNL_CONFIG, PRIM_OP_REQUEST, MGW_IUUP_MSGB_SIZE); + irp2->u.config.transparent = false; + irp2->u.config.active = conn_rtp->iuup.active_init; + irp2->u.config.data_pdu_type = irp->u.status.u.initialization.data_pdu_type; + irp2->u.config.supported_versions_mask = def_configure_req.supported_versions_mask; + irp2->u.config.num_rfci = irp->u.status.u.initialization.num_rfci; + irp2->u.config.num_subflows = irp->u.status.u.initialization.num_subflows; + memcpy(irp2->u.config.subflow_sizes, irp->u.status.u.initialization.subflow_sizes, + IUUP_MAX_RFCIS * IUUP_MAX_SUBFLOWS * sizeof(irp2->u.config.subflow_sizes[0][0])); + irp2->u.config.IPTIs_present = irp->u.status.u.initialization.IPTIs_present; + if (irp->u.status.u.initialization.IPTIs_present) + memcpy(irp2->u.config.IPTIs, irp->u.status.u.initialization.IPTIs, + IUUP_MAX_RFCIS * sizeof(irp2->u.config.IPTIs[0])); + irp2->u.config.t_init = def_configure_req.t_init; + irp2->u.config.t_ta = def_configure_req.t_ta; + irp2->u.config.t_rc = def_configure_req.t_rc; + + /* We need to force allowance of RTP containing Init-ACK back: */ + prev_output_enabled = conn_rtp->end.output_enabled; + conn_rtp->end.output_enabled = true; + + if ((rc = osmo_iuup_rnl_prim_down(conn_rtp->iuup.iui, irp2)) == 0) + conn_rtp->iuup.configured = true; + else + LOG_CONN_RTP(conn_rtp, LOGL_ERROR, "Failed configuring IuUP layer\n"); + + conn_rtp->end.output_enabled = prev_output_enabled; + return rc; +} + +static int _conn_iuup_rtp_pl_up(struct mgcp_conn_rtp *conn_rtp, struct msgb *msg) +{ + /* Send RTP payload (IuUP) up the stack: */ + struct osmo_iuup_tnl_prim *itp; + int rc; + + msg->l2h = msgb_data(msg) + sizeof(struct rtp_hdr); + + itp = osmo_iuup_tnl_prim_alloc(conn_rtp->conn, OSMO_IUUP_TNL_UNITDATA, PRIM_OP_INDICATION, MGW_IUUP_MSGB_SIZE); + itp->oph.msg->l2h = msgb_put(itp->oph.msg, msgb_l2len(msg)); + memcpy(itp->oph.msg->l2h, msgb_l2(msg), msgb_l2len(msg)); + if ((rc = osmo_iuup_tnl_prim_up(conn_rtp->iuup.iui, itp)) != 0) { + LOG_CONN_RTP(conn_rtp, LOGL_ERROR, "Failed passing IuUP-Init to IuUP layer\n"); + } + return rc; +} + +static int check_rtp_iuup(const struct mgcp_conn_rtp *conn_rtp, struct msgb *msg) +{ + size_t min_size = sizeof(struct rtp_hdr); + /* Check there's at least 2 bytes of RTP payload (IuUP header). This is + ** mainly to avoid 0-byte payload copy cases */ + if (msgb_length(msg) < sizeof(struct rtp_hdr) + 2) { + LOG_CONN_RTP(conn_rtp, LOGL_ERROR, "RTP-IuUP packet too short (%u < %zu)\n", + msgb_length(msg), min_size); + return -1; + } + return 0; +} + +/* owns the irp */ +static 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) +{ + int rc; + + /* If we are not configured and we received bridged data, it means + * conn_rtp_src is already configured and INITed, and we can infer + * conn_rtp_src is Init-passive (RNC side), so conn_rtp_dst needs to be + * configured as INIT-active: */ + if (!conn_rtp_dst->iuup.configured) { + OSMO_ASSERT(conn_rtp_src->iuup.init_ind); + rc = _conn_iuup_configure_as_active(conn_rtp_dst, conn_rtp_src->iuup.init_ind); + if (rc < 0) { + msgb_free(irp->oph.msg); + return rc; + } + } + + /* We simply forward the msg, without freeing it: */ + talloc_steal(conn_rtp_dst->conn, irp->oph.msg); + irp->oph.operation = PRIM_OP_REQUEST; + if ((rc = osmo_iuup_rnl_prim_down(conn_rtp_dst->iuup.iui, irp)) != 0) + LOG_CONN_RTP(conn_rtp_dst, LOGL_ERROR, "Failed Tx data down to IuUP layer\n"); + return rc; +} + +/* Owns the irp */ +static 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) +{ + /* FIXME: We probably need transcoding here?! Or at least look up AMR modes and translate to related RFCI */ + uint8_t frame_nr = irp->u.data.frame_nr; + uint8_t fqc = irp->u.data.fqc; + struct msgb *msg = irp->oph.msg; + ssize_t amr_length = 0; + int ft; + uint8_t *amr_data; + struct rtp_hdr *rtp_hdr; + struct amr_hdr *amr_hdr; + int rc; + + ft = osmo_amr_bytes_to_ft(msgb_l3len(msg)); + if (ft < 0) { + LOGPCONN(conn_rtp_src->conn, DRTP, LOGL_ERROR, + "Unknown AMR format for size %u\n", msgb_l3len(msg)); + msgb_free(msg); + return ft; + } + msgb_pull_to_l3(msg); + LOGP(DLMGCP, LOGL_DEBUG, "Convert Iuup -> AMR: ft %d, len %d\n", ft, msgb_l3len(msg)); + + if (mgcp_codec_amr_is_octet_aligned(conn_rtp_dst->end.codec)) { + amr_hdr = (struct amr_hdr *) msgb_push(msg, sizeof(struct amr_hdr)); + amr_hdr->cmr = 15; /* no change */ + amr_hdr->f = 0; + amr_hdr->q = !fqc; + amr_hdr->ft = ft & 0xff; + amr_hdr->pad1 = 0; + amr_hdr->pad2 = 0; + } else { + OSMO_ASSERT(msgb_tailroom(msg) >= 2); + msgb_put(msg, 2); + osmo_amr_iuup_to_bwe(msgb_data(msg), msgb_length(msg) - 2, msgb_length(msg) + 2); + /* fill bwe header */ + amr_data = msgb_data(msg); + /* CMR no change | follow bit | ft (3 of 4 bits) */ + amr_data[0] = 15 << 4 | (0 << 3) | (ft >> 1); + amr_data[1] |= ((ft & 0x1) << 7) | (((!fqc) & 0x1) << 6); + amr_length = (osmo_amr_bits(ft) + 10 + 7) / 8; + msgb_trim(msg, amr_length); + } + rtp_hdr = (struct rtp_hdr *) msgb_push(msg, sizeof(*rtp_hdr)); + *rtp_hdr = (struct rtp_hdr){ + .csrc_count = 0, + .extension = 0, + .padding = 0, + .version = 0, + .payload_type = conn_rtp_dst->end.codec->payload_type, + .marker = 0, + .sequence = frame_nr, + .timestamp = 0, + .ssrc = 0 + }; + + rc = mgcp_send(conn_rtp_dst->conn->endp, true, NULL, msg, conn_rtp_src, conn_rtp_dst); + msgb_free(msg); + return rc; +} + +static int _conn_iuup_rx_rnl_data(struct mgcp_conn_rtp *conn_rtp_src, struct osmo_iuup_rnl_prim *irp) +{ + struct mgcp_conn *conn_dst; + struct mgcp_conn_rtp *conn_rtp_dst; + int rc; + + conn_dst = _find_dst_conn(conn_rtp_src->conn); + + /* There is no destination conn, stop here */ + if (!conn_dst) { + LOGPCONN(conn_rtp_src->conn, DRTP, LOGL_DEBUG, + "no connection to forward an incoming IuUP payload to\n"); + rc= -1; + goto free_ret; + } + + /* The destination conn is not an RTP/IuUP connection */ + if (conn_dst->type != MGCP_CONN_TYPE_RTP) { + LOGPCONN(conn_rtp_src->conn, DRTP, LOGL_ERROR, + "unable to find suitable destination conn\n"); + rc= -1; + goto free_ret; + } + conn_rtp_dst = &conn_dst->u.rtp; + + switch (conn_rtp_dst->type) { + case MGCP_RTP_IUUP: + return bridge_iuup_to_iuup_peer(conn_rtp_src, conn_rtp_dst, irp); + case MGCP_RTP_DEFAULT: + return bridge_iuup_to_rtp_peer(conn_rtp_src, conn_rtp_dst, irp); + case MGCP_OSMUX_BSC: + case MGCP_OSMUX_BSC_NAT: + default: + LOGPCONN(conn_rtp_src->conn, DRTP, LOGL_ERROR, + "Forward of IuUP payload to RTP connection type %u not supported!\n", + conn_rtp_dst->type); + rc = 0; + } + +free_ret: + msgb_free(irp->oph.msg); + return rc; +} + +static int _conn_iuup_rx_rnl_status_init(struct mgcp_conn_rtp *conn_rtp_src, struct osmo_iuup_rnl_prim *irp) +{ + struct mgcp_conn *conn_dst; + struct mgcp_conn_rtp *conn_rtp_dst; + int rc = 0; + struct msgb *msg; + + /* Find RFCI containing NO_DATA: */ + conn_rtp_src->iuup.rfci_idx_no_data = _find_rfci_no_data(irp); + + msg = msgb_copy_c(conn_rtp_src->conn, irp->oph.msg, "iuup-init-copy"); + conn_rtp_src->iuup.init_ind = (struct osmo_iuup_rnl_prim *)msgb_data(msg); + conn_rtp_src->iuup.init_ind->oph.msg = msg; + + conn_dst = _find_dst_conn(conn_rtp_src->conn); + if (!conn_dst) + return 0; /* FIXME: that probably means we potentially need to Init the peer dst later! */ + conn_rtp_dst = &conn_dst->u.rtp; + if (!mgcp_conn_rtp_is_iuup(conn_rtp_dst)) + return 0; /* Nothing to do */ + + /* We received IuUP parameters on the peer (RNC), Init actively this conn (against CN): */ + if (!conn_rtp_dst->iuup.configured) + rc = _conn_iuup_configure_as_active(conn_rtp_dst, irp); + + return rc; +} + +static int _conn_iuup_rx_rnl_status(struct mgcp_conn_rtp *conn_rtp_src, struct osmo_iuup_rnl_prim *irp) +{ + int rc; + + switch (irp->u.status.procedure) { + case IUUP_PROC_INIT: + rc = _conn_iuup_rx_rnl_status_init(conn_rtp_src, irp); + break; + case IUUP_PROC_RATE_CTRL: + case IUUP_PROC_TIME_ALIGN: + case IUUP_PROC_ERR_EVENT: + default: + LOG_CONN_RTP(conn_rtp_src, LOGL_ERROR, + "Received IuUP RNL STATUS procedure type %u not handled\n", + irp->u.status.procedure); + rc = 0; + } + + return rc; +} +static int _conn_iuup_user_prim_cb(struct osmo_prim_hdr *oph, void *ctx) +{ + struct mgcp_conn_rtp *conn_rtp_src = ctx; + struct osmo_iuup_rnl_prim *irp = (struct osmo_iuup_rnl_prim *)oph; + struct msgb *msg = oph->msg; + int rc; + + switch (OSMO_PRIM_HDR(&irp->oph)) { + case OSMO_PRIM(OSMO_IUUP_RNL_DATA, PRIM_OP_INDICATION): + /* we pass ownsership of msg here: */ + rc = _conn_iuup_rx_rnl_data(conn_rtp_src, irp); + break; + case OSMO_PRIM(OSMO_IUUP_RNL_STATUS, PRIM_OP_INDICATION): + rc = _conn_iuup_rx_rnl_status(conn_rtp_src, irp); + msgb_free(msg); + break; + default: + msgb_free(msg); + OSMO_ASSERT(false); + } + + return rc; +} + +static int _conn_iuup_transport_prim_cb(struct osmo_prim_hdr *oph, void *ctx) +{ + struct mgcp_conn_rtp *conn_rtp_dst = ctx; + struct mgcp_conn *conn_dst = conn_rtp_dst->conn; + struct osmo_iuup_tnl_prim *itp = (struct osmo_iuup_tnl_prim *)oph; + struct mgcp_conn *conn_src; + struct msgb *msg; + struct rtp_hdr *rtph; + struct osmo_sockaddr from_addr = {0}; /* FIXME: what to do with this one? */ + + OSMO_ASSERT(OSMO_PRIM_HDR(&itp->oph) == OSMO_PRIM(OSMO_IUUP_TNL_UNITDATA, PRIM_OP_REQUEST)); + + msg = oph->msg; + talloc_steal(conn_rtp_dst->conn, msg); + + msgb_pull_to_l2(msg); + rtph = (struct rtp_hdr *)msgb_push(msg, sizeof(*rtph)); + /* TODO: fill rtph properly: */ + *rtph = (struct rtp_hdr){ + .csrc_count = 0, + .extension = 0, + .padding = 0, + .version = 0, + .payload_type = conn_rtp_dst->end.codec->payload_type, + .marker = 0, + .sequence = 0, + .timestamp = 0, + .ssrc = 0 + }; + + /* The destination of the destination conn is the source conn, right? */ + conn_src = _find_dst_conn(conn_dst); + if (!conn_src) { + LOG_CONN_RTP(conn_rtp_dst, LOGL_NOTICE, + "Couldn't find source conn for IuUP dst conn\n"); + //msgb_free(msg); + //return -ENOTCONN; + conn_src = conn_dst; + } + + /* FIXME: set from_addr = NULL */ + return mgcp_send(conn_dst->endp, 1, + &from_addr, msg, &conn_src->u.rtp, conn_rtp_dst); +} + +int mgcp_conn_iuup_init(struct mgcp_conn_rtp *conn_rtp) +{ + conn_rtp->type = MGCP_RTP_IUUP; + conn_rtp->iuup.iui = osmo_iuup_instance_alloc(conn_rtp->conn, conn_rtp->conn->id); + OSMO_ASSERT(conn_rtp->iuup.iui); + osmo_iuup_instance_set_user_prim_cb(conn_rtp->iuup.iui, _conn_iuup_user_prim_cb, conn_rtp); + osmo_iuup_instance_set_transport_prim_cb(conn_rtp->iuup.iui, _conn_iuup_transport_prim_cb, conn_rtp); + conn_rtp->iuup.rfci_idx_no_data = -1; + return 0; +} + +void mgcp_conn_iuup_cleanup(struct mgcp_conn_rtp *conn_rtp) +{ + osmo_iuup_instance_free(conn_rtp->iuup.iui); + conn_rtp->iuup.iui = NULL; +} + +int mgcp_conn_iuup_dispatch_rtp(struct msgb *msg) +{ + struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg); + struct mgcp_conn_rtp *conn_rtp_src = mc->conn_src; + int rc = 0; + bool force_output_enabled = false; + bool prev_output_enabled; + struct osmo_sockaddr prev_rem_addr; + uint16_t prev_rem_rtp_port; + + OSMO_ASSERT(mgcp_conn_rtp_is_iuup(conn_rtp_src)); + + if ((rc = check_rtp_iuup(conn_rtp_src, msg)) < 0) + goto free_ret; + + if (!conn_rtp_src->iuup.configured) { + /* We received the first message without sending any, the peer is the active side (RNC). */ + rc = _conn_iuup_configure_as_passive(conn_rtp_src, msg); + if (rc < 0) + goto free_ret; + /* We need to force allowance of RTP containing Init-ACK back: */ + prev_output_enabled = conn_rtp_src->end.output_enabled; + conn_rtp_src->end.output_enabled = true; + force_output_enabled = true; + /* Fill in the peer address so that we can send Init-ACK back: */ + prev_rem_addr = conn_rtp_src->end.addr; + prev_rem_rtp_port = conn_rtp_src->end.rtp_port; + conn_rtp_src->end.addr = *mc->from_addr; + conn_rtp_src->end.rtp_port = htons(osmo_sockaddr_port(&mc->from_addr->u.sa)); + } + + rc = _conn_iuup_rtp_pl_up(conn_rtp_src, msg); + + if (force_output_enabled) { + conn_rtp_src->end.output_enabled = prev_output_enabled; + conn_rtp_src->end.addr = prev_rem_addr; + conn_rtp_src->end.rtp_port = prev_rem_rtp_port; + } + + return rc; +free_ret: + msgb_free(msg); + return rc; +} + +/* Build IuUP RNL Data primitive from msg containing an incoming RTP pkt from + * peer and send it down the IuUP layer towards the destination as IuUP/RTP: */ +int mgcp_conn_iuup_send_rtp(struct mgcp_conn_rtp *conn_src_rtp, struct mgcp_conn_rtp *conn_dest_rtp, struct msgb *msg) +{ + struct osmo_iuup_rnl_prim *irp; + struct rtp_hdr *rtph; + struct amr_hdr *amr_hdr; + int rc = -1; + int iuup_length = 0; + + /* Tx RNL-DATA.req */ + rtph = (struct rtp_hdr *)msgb_data(msg); + msgb_pull(msg, sizeof(*rtph)); + + /* FIXME: validate amr packets */ + irp = osmo_iuup_rnl_prim_alloc(conn_dest_rtp->conn, OSMO_IUUP_RNL_DATA, PRIM_OP_REQUEST, MGW_IUUP_MSGB_SIZE); + irp->u.data.frame_nr = htons(rtph->sequence) % 16; + + /* FIXME: We probably need transcoding here?! Or at least look up AMR modes and translate to related RFCI */ + if (msgb_length(msg) < (sizeof(*amr_hdr))) { + return -1; + } + + amr_hdr = (struct amr_hdr *) msgb_data(msg); + if (mgcp_codec_amr_is_octet_aligned(conn_src_rtp->end.codec)) { + /* FIXME: CMR handling & multiple frames handling */ + irp->u.data.fqc = IUUP_FQC_FRAME_GOOD; + irp->u.data.rfci = _conn_iuup_amr_ft_2_rfci(conn_dest_rtp, amr_hdr->ft); + msgb_pull(msg, 2); + } else { + irp->u.data.fqc = amr_hdr->q; + irp->u.data.rfci = _conn_iuup_amr_ft_2_rfci(conn_dest_rtp, amr_hdr->ft); + rc = iuup_length = osmo_amr_bwe_to_iuup(msgb_data(msg), msgb_length(msg)); + if (rc < 0) { + LOG_CONN_RTP(conn_dest_rtp, LOGL_ERROR, "Failed convert the RTP/AMR to IuUP payload\n"); + return rc; + } + msgb_trim(msg, iuup_length); + } + + irp->oph.msg->l3h = msgb_put(irp->oph.msg, msgb_length(msg)); + memcpy(irp->oph.msg->l3h, msgb_data(msg), msgb_length(msg)); + if ((rc = osmo_iuup_rnl_prim_down(conn_dest_rtp->iuup.iui, irp)) != 0) { + LOG_CONN_RTP(conn_dest_rtp, LOGL_ERROR, "Failed Tx RTP payload down the IuUP layer\n"); + return rc; + } + + return rc; +} + +/* Build IuUP RNL Data primitive from msg containing dummy content and send it + * down the IuUP layer towards the destination as IuUP/RTP: */ +int mgcp_conn_iuup_send_dummy(struct mgcp_conn_rtp *conn_rtp) +{ + struct osmo_iuup_rnl_prim *irp; + int rc; + + if (conn_rtp->iuup.rfci_idx_no_data == -1) { + LOG_CONN_RTP(conn_rtp, LOGL_NOTICE, "No RFCI NO_DATA found, unable to send dummy packet\n"); + return -ENOTSUP; + } + + irp = osmo_iuup_rnl_prim_alloc(conn_rtp->conn, OSMO_IUUP_RNL_DATA, PRIM_OP_REQUEST, MGW_IUUP_MSGB_SIZE); + irp->u.data.frame_nr = 0; + irp->u.data.fqc = IUUP_FQC_FRAME_GOOD; + irp->u.data.rfci = conn_rtp->iuup.rfci_idx_no_data; + irp->oph.msg->l3h = irp->oph.msg->tail; + if ((rc = osmo_iuup_rnl_prim_down(conn_rtp->iuup.iui, irp)) != 0) { + LOG_CONN_RTP(conn_rtp, LOGL_ERROR, "Failed Tx RTP dummy payload down the IuUP layer\n"); + return -EINVAL; + } + + return 0; +} diff --git a/src/libosmo-mgcp/mgcp_network.c b/src/libosmo-mgcp/mgcp_network.c index f865dfe..ea57b26 100644 --- a/src/libosmo-mgcp/mgcp_network.c +++ b/src/libosmo-mgcp/mgcp_network.c @@ -48,7 +48,7 @@ #include <osmocom/mgcp/debug.h> #include <osmocom/codec/codec.h> #include <osmocom/mgcp/mgcp_e1.h> - +#include <osmocom/mgcp/mgcp_iuup.h>
#define RTP_SEQ_MOD (1 << 16) #define RTP_MAX_DROPOUT 3000 @@ -975,7 +975,8 @@ * the length is because we currently handle IUUP packets as RTP * packets, so they must pass this check, if we weould be more * strict here, we would possibly break 3G. (see also FIXME note - * below */ + * below. */ + /* FIXME UPDATE: we can now apply more checks based on mgcp_conn_rtp_is_iuup(conn_src) */
return 0; } @@ -1013,6 +1014,11 @@ "endpoint type is MGCP_OSMUX_BSC_NAT, " "using osmux_xfrm_to_osmux() to forward data through OSMUX\n"); return osmux_xfrm_to_osmux((char*)msgb_data(msg), msgb_length(msg), conn_dst); + case MGCP_RTP_IUUP: + LOGPENDP(endp, DRTP, LOGL_DEBUG, + "endpoint type is MGCP_RTP_IUUP, " + "using mgcp_conn_iuup_send_rtp() to forward data over IuUP\n"); + return mgcp_conn_iuup_send_rtp(conn_src, conn_dst, msg); }
/* If the data has not been handled/forwarded until here, it will @@ -1073,8 +1079,11 @@ if (check_rtp_destin(conn) != 0) goto failed;
- rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr, - conn->end.rtp_port, rtp_dummy_payload, sizeof(rtp_dummy_payload)); + if (mgcp_conn_rtp_is_iuup(conn)) + rc = mgcp_conn_iuup_send_dummy(conn); + else + rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr, conn->end.rtp_port, + rtp_dummy_payload, sizeof(rtp_dummy_payload));
if (rc == -1) goto failed; @@ -1138,7 +1147,7 @@ * course unable to patch the payload type. A situation like this * should not occur if transcoding is consequently avoided. Until * we have transcoding support in osmo-mgw we can not resolve this. */ - if (is_rtp) { + if (is_rtp && conn_dst->type != MGCP_RTP_IUUP) { rc = mgcp_patch_pt(conn_src, conn_dst, msg); if (rc < 0) { LOGPENDP(endp, DRTP, LOGL_DEBUG, @@ -1185,7 +1194,9 @@ mgcp_patch_and_count(endp, rtp_state, rtp_end, addr, msg);
- if (amr_oa_bwe_convert_indicated(conn_dst->end.codec)) { + if (mgcp_conn_rtp_is_iuup(conn_dst) || mgcp_conn_rtp_is_iuup(conn_src)) { + /* the iuup code will correctly transform to the correct AMR mode */ + } else if (amr_oa_bwe_convert_indicated(conn_dst->end.codec)) { rc = amr_oa_bwe_convert(endp, msg, conn_dst->end.codec->param.amr_octet_aligned); if (rc < 0) { @@ -1214,25 +1225,6 @@ forward_data(rtp_end->rtp.fd, &conn_src->tap_out, msg);
- /* FIXME: HACK HACK HACK. See OS#2459. - * The ip.access nano3G needs the first RTP payload's first two bytes to read hex - * 'e400', or it will reject the RAB assignment. It seems to not harm other femto - * cells (as long as we patch only the first RTP payload in each stream). - */ - if (!rtp_state->patched_first_rtp_payload - && conn_src->conn->mode == MGCP_CONN_LOOPBACK) { - uint8_t *data = msgb_data(msg) + 12; - if (data[0] == 0xe0) { - data[0] = 0xe4; - data[1] = 0x00; - data[2] = (0x09 << 2); /* Patch CRC Header to adapt to new header above */ - rtp_state->patched_first_rtp_payload = true; - LOGPENDP(endp, DRTP, LOGL_DEBUG, - "Patching over first two bytes" - " to fake an IuUP Initialization Ack\n"); - } - } - len = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr, rtp_end->rtp_port, (char *)msgb_data(msg), msgb_length(msg));
@@ -1292,6 +1284,9 @@ * destination connection is known the RTP packet is sent via * the destination connection. */
+ /* If source is IuUP, we need to handle state, forward it through specific bridge: */ + if (mgcp_conn_rtp_is_iuup(conn_src)) + return mgcp_conn_iuup_dispatch_rtp(msg);
/* Check if the connection is in loopback mode, if yes, just send the * incoming data back to the origin */ diff --git a/src/libosmo-mgcp/mgcp_protocol.c b/src/libosmo-mgcp/mgcp_protocol.c index 5b88c7a..b5a0c58 100644 --- a/src/libosmo-mgcp/mgcp_protocol.c +++ b/src/libosmo-mgcp/mgcp_protocol.c @@ -46,6 +46,7 @@ #include <osmocom/mgcp/mgcp_sdp.h> #include <osmocom/mgcp/mgcp_codec.h> #include <osmocom/mgcp/mgcp_conn.h> +#include <osmocom/mgcp/mgcp_iuup.h>
/* Contains the last successfully resolved endpoint name. This variable is used * for the unit-tests to verify that the endpoint was correctly resolved. */ @@ -147,7 +148,13 @@ struct mgcp_conn_rtp *conn_dst = conn; struct mgcp_conn *_conn;
- if (conn->type != MGCP_RTP_DEFAULT && !mgcp_conn_rtp_is_osmux(conn)) { + switch (conn->type) { + case MGCP_RTP_DEFAULT: + case MGCP_OSMUX_BSC: + case MGCP_OSMUX_BSC_NAT: + case MGCP_RTP_IUUP: + break; + default: LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "RTP-setup: Endpoint is not configured as RTP default, stopping here!\n"); return 0; @@ -1028,6 +1035,11 @@ rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_CODEC_NEGOTIATION)); goto error2; } + /* Upgrade the conn type RTP_DEFAULT->RTP_IUUP if needed based on requested codec: */ + /* TODO: "codec" probably needs to be moved from endp to conn */ + if (conn->type == MGCP_RTP_DEFAULT && strcmp(conn->end.codec->subtype_name, "VND.3GPP.IUFP") == 0) { + rc = mgcp_conn_iuup_init(conn); + }
conn->end.fmtp_extra = talloc_strdup(trunk->endpoints, trunk->audio_fmtp_extra);