blob: 41e6ffce3d5ee440d90ef8f7e1c9c281cb29a2c0 [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* The protocol implementation */
3
4/*
5 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2012 by On-Waves
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <string.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <errno.h>
28#include <time.h>
29#include <limits.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020030#include <arpa/inet.h>
31
32#include <osmocom/core/msgb.h>
33#include <osmocom/core/select.h>
Philipp Maier1cb1e382017-11-02 17:16:04 +010034#include <osmocom/core/socket.h>
Philipp Maier4dba7692018-08-03 12:20:52 +020035#include <osmocom/core/byteswap.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020036#include <osmocom/netif/rtp.h>
Philipp Maier228e5912019-03-05 13:56:59 +010037#include <osmocom/netif/amr.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020038#include <osmocom/mgcp/mgcp.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020039#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier993ea6b2020-08-04 18:26:50 +020040#include <osmocom/mgcp/mgcp_network.h>
41#include <osmocom/mgcp/mgcp_protocol.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020042#include <osmocom/mgcp/mgcp_stat.h>
43#include <osmocom/mgcp/osmux.h>
44#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010045#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maierc66ab2c2020-06-02 20:55:34 +020046#include <osmocom/mgcp/mgcp_trunk.h>
Philipp Maier6931f9a2018-07-26 09:29:31 +020047#include <osmocom/mgcp/mgcp_codec.h>
Philipp Maierc3413882017-10-27 12:26:54 +020048#include <osmocom/mgcp/debug.h>
Philipp Maier9fc8a022019-02-20 12:26:52 +010049#include <osmocom/codec/codec.h>
Philipp Maier889fe7f2020-07-06 17:44:12 +020050#include <osmocom/mgcp/mgcp_e1.h>
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +010051#include <osmocom/mgcp/mgcp_iuup.h>
Philipp Maier993ea6b2020-08-04 18:26:50 +020052
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020053#define RTP_SEQ_MOD (1 << 16)
54#define RTP_MAX_DROPOUT 3000
55#define RTP_MAX_MISORDER 100
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020056
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +010057void rtpconn_rate_ctr_add(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp,
Harald Weltea48ff4a2020-03-08 14:45:08 +010058 int id, int inc)
59{
Pau Espin Pedroldaf5bce2022-09-22 19:14:24 +020060 struct rate_ctr_group *conn_stats = conn_rtp->ctrg;
Philipp Maierc66ab2c2020-06-02 20:55:34 +020061 struct rate_ctr_group *mgw_stats = endp->trunk->ratectr.all_rtp_conn_stats;
Harald Weltea48ff4a2020-03-08 14:45:08 +010062
Philipp Maierc66ab2c2020-06-02 20:55:34 +020063 /* add to both the per-connection and the global stats */
Pau Espin Pedrol907744e2021-06-04 17:57:34 +020064 rate_ctr_add(rate_ctr_group_get_ctr(conn_stats, id), inc);
65 rate_ctr_add(rate_ctr_group_get_ctr(mgw_stats, id), inc);
Harald Weltea48ff4a2020-03-08 14:45:08 +010066}
67
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +020068void rtpconn_rate_ctr_inc(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp, int id)
Harald Weltea48ff4a2020-03-08 14:45:08 +010069{
70 rtpconn_rate_ctr_add(conn_rtp, endp, id, 1);
71}
72
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +020073static int rx_rtp(struct msgb *msg);
74
Pau Espin Pedrolca280a12021-07-06 18:15:35 +020075bool mgcp_rtp_end_remote_addr_available(const struct mgcp_rtp_end *rtp_end)
76{
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +020077 return (osmo_sockaddr_port(&rtp_end->addr.u.sa) != 0) &&
78 (osmo_sockaddr_is_any(&rtp_end->addr) == 0);
Pau Espin Pedrolca280a12021-07-06 18:15:35 +020079}
80
Philipp Maier1cb1e382017-11-02 17:16:04 +010081/*! Determine the local rtp bind IP-address.
Philipp Maier0b79d212020-06-18 12:02:49 +020082 * \param[out] addr caller provided memory to store the resulting IP-Address.
83 * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters.
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +020084 * \ returns 0 on success, -1 if no local address could be provided.
Philipp Maier1cb1e382017-11-02 17:16:04 +010085 *
86 * The local bind IP-address is automatically selected by probing the
87 * IP-Address of the interface that is pointing towards the remote IP-Address,
88 * if no remote IP-Address is known yet, the statically configured
89 * IP-Addresses are used as fallback. */
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +020090int mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn)
Philipp Maier1cb1e382017-11-02 17:16:04 +010091{
92
93 struct mgcp_endpoint *endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +020094 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier1cb1e382017-11-02 17:16:04 +010095 int rc;
96 endp = conn->conn->endp;
Pau Espin Pedrol051eb4d2022-10-04 12:50:28 +020097 bool rem_addr_set = osmo_sockaddr_is_any(&conn->end.addr) == 0;
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +020098 char *bind_addr;
Philipp Maier1cb1e382017-11-02 17:16:04 +010099
Pau Espin Pedrol479cf762022-09-07 19:42:54 +0200100 /* Osmux: No smart IP addresses allocation is supported yet. Simply
101 * return the one set in VTY config: */
102 if (mgcp_conn_rtp_is_osmux(conn)) {
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200103 if (rem_addr_set) {
104 /* Match IP version with what was requested from remote: */
105 bind_addr = conn->end.addr.u.sa.sa_family == AF_INET6 ?
106 conn->conn->endp->trunk->cfg->osmux_addr_v6 :
107 conn->conn->endp->trunk->cfg->osmux_addr_v4;
108 } else {
109 /* Choose any of the bind addresses, preferring v6 over v4 if available: */
110 bind_addr = conn->conn->endp->trunk->cfg->osmux_addr_v6;
111 if (!bind_addr)
112 bind_addr = conn->conn->endp->trunk->cfg->osmux_addr_v4;
113 }
114 if (!bind_addr) {
115 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
116 "Unable to locate local Osmux address, check your configuration! v4=%u v6=%u remote_known=%s\n",
117 !!conn->conn->endp->trunk->cfg->osmux_addr_v4,
118 !!conn->conn->endp->trunk->cfg->osmux_addr_v6,
119 rem_addr_set ? osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf) : "no");
120 return -1;
121 }
122 LOGPCONN(conn->conn, DOSMUX, LOGL_DEBUG,
123 "Using configured osmux bind ip as local bind ip %s\n",
Pau Espin Pedrol479cf762022-09-07 19:42:54 +0200124 bind_addr);
125 osmo_strlcpy(addr, bind_addr, INET6_ADDRSTRLEN);
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200126 return 0;
Pau Espin Pedrol479cf762022-09-07 19:42:54 +0200127 }
128
Philipp Maier1cb1e382017-11-02 17:16:04 +0100129 /* Try probing the local IP-Address */
Ericfbf78d12021-08-23 22:31:39 +0200130 if (endp->trunk->cfg->net_ports.bind_addr_probe && rem_addr_set) {
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200131 rc = osmo_sock_local_ip(addr, osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Philipp Maier1cb1e382017-11-02 17:16:04 +0100132 if (rc < 0)
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200133 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
134 "local interface auto detection failed, using configured addresses...\n");
Philipp Maier1cb1e382017-11-02 17:16:04 +0100135 else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200136 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
137 "selected local rtp bind ip %s by probing using remote ip %s\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200138 addr, osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200139 return 0;
Philipp Maier1cb1e382017-11-02 17:16:04 +0100140 }
141 }
142
Pau Espin Pedrol479cf762022-09-07 19:42:54 +0200143 /* Select from preconfigured IP-Addresses. */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200144 if (rem_addr_set) {
Philipp Maier1cb1e382017-11-02 17:16:04 +0100145 /* Check there is a bind IP for the RTP traffic configured,
146 * if so, use that IP-Address */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200147 bind_addr = conn->end.addr.u.sa.sa_family == AF_INET6 ?
Ericfbf78d12021-08-23 22:31:39 +0200148 endp->trunk->cfg->net_ports.bind_addr_v6 :
149 endp->trunk->cfg->net_ports.bind_addr_v4;
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200150 } else {
151 /* Choose any of the bind addresses, preferring v6 over v4 */
Ericfbf78d12021-08-23 22:31:39 +0200152 bind_addr = endp->trunk->cfg->net_ports.bind_addr_v6;
Eric2764bdb2021-08-23 22:11:47 +0200153 if (!strlen(bind_addr))
Ericfbf78d12021-08-23 22:31:39 +0200154 bind_addr = endp->trunk->cfg->net_ports.bind_addr_v4;
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200155 }
Eric2764bdb2021-08-23 22:11:47 +0200156 if (strlen(bind_addr)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200157 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
158 "using configured rtp bind ip as local bind ip %s\n",
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200159 bind_addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100160 } else {
161 /* No specific bind IP is configured for the RTP traffic, so
162 * assume the IP where we listen for incoming MGCP messages
163 * as bind IP */
Ericfbf78d12021-08-23 22:31:39 +0200164 bind_addr = endp->trunk->cfg->source_addr;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200165 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200166 "using mgcp bind ip as local rtp bind ip: %s\n", bind_addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100167 }
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200168 osmo_strlcpy(addr, bind_addr, INET6_ADDRSTRLEN);
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200169 return 0;
Philipp Maier1cb1e382017-11-02 17:16:04 +0100170}
171
Philipp Maier87bd9be2017-08-22 16:35:41 +0200172/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200173 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +0200174 * 1/codec_rate seconds. */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100175uint32_t mgcp_get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200176{
177 struct timespec tp;
178 uint64_t ret;
179
Philipp Maier87bd9be2017-08-22 16:35:41 +0200180 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200181 return 0;
182
183 memset(&tp, 0, sizeof(tp));
184 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maierc3413882017-10-27 12:26:54 +0200185 LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200186
187 /* convert it to 1/unit seconds */
188 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200189 ret *= codec_rate;
190 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200191
192 return ret;
193}
194
Philipp Maier87bd9be2017-08-22 16:35:41 +0200195/* Compute timestamp alignment error */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200196static int32_t ts_alignment_error(const struct mgcp_rtp_stream_state *sstate,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200197 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200198{
199 int32_t timestamp_delta;
200
201 if (ptime == 0)
202 return 0;
203
204 /* Align according to: T - Tlast = k * Tptime */
205 timestamp_delta = timestamp - sstate->last_timestamp;
206
207 return timestamp_delta % ptime;
208}
209
Philipp Maier87bd9be2017-08-22 16:35:41 +0200210/* Check timestamp and sequence number for plausibility */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200211static int check_rtp_timestamp(const struct mgcp_endpoint *endp,
212 const struct mgcp_rtp_state *state,
213 const struct mgcp_rtp_stream_state *sstate,
214 const struct mgcp_rtp_end *rtp_end,
215 const struct osmo_sockaddr *addr,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200216 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200217 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200218{
219 int32_t tsdelta;
220 int32_t timestamp_error;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200221 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200222
223 /* Not fully intialized, skip */
224 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
225 return 0;
226
227 if (seq == sstate->last_seq) {
228 if (timestamp != sstate->last_timestamp) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200229 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200230 LOGPENDP(endp, DRTP, LOGL_ERROR,
231 "The %s timestamp delta is != 0 but the sequence "
232 "number %d is the same, "
233 "TS offset: %d, SeqNo offset: %d "
234 "on SSRC: %u timestamp: %u "
235 "from %s:%d\n",
236 text, seq,
237 state->patch.timestamp_offset, state->patch.seq_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200238 sstate->ssrc, timestamp,
239 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
240 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200241 }
242 return 0;
243 }
244
245 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200246 (int32_t)(timestamp - sstate->last_timestamp) /
247 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200248
249 if (tsdelta == 0) {
250 /* Don't update *tsdelta_out */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200251 LOGPENDP(endp, DRTP, LOGL_NOTICE,
252 "The %s timestamp delta is %d "
253 "on SSRC: %u timestamp: %u "
254 "from %s:%d\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200255 text, tsdelta, sstate->ssrc, timestamp,
256 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
257 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200258
259 return 0;
260 }
261
262 if (sstate->last_tsdelta != tsdelta) {
263 if (sstate->last_tsdelta) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200264 LOGPENDP(endp, DRTP, LOGL_INFO,
265 "The %s timestamp delta changes from %d to %d "
266 "on SSRC: %u timestamp: %u from %s:%d\n",
267 text, sstate->last_tsdelta, tsdelta,
268 sstate->ssrc, timestamp,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200269 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
270 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200271 }
272 }
273
274 if (tsdelta_out)
275 *tsdelta_out = tsdelta;
276
277 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200278 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200279
280 if (timestamp_error) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200281 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200282 LOGPENDP(endp, DRTP, LOGL_NOTICE,
283 "The %s timestamp has an alignment error of %d "
284 "on SSRC: %u "
285 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
286 "from %s:%d. ptime: %d\n",
287 text, timestamp_error,
288 sstate->ssrc,
289 (int16_t)(seq - sstate->last_seq),
290 (int32_t)(timestamp - sstate->last_timestamp),
291 tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200292 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
293 osmo_sockaddr_port(&addr->u.sa),
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200294 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200295 }
296 return 1;
297}
298
299/* Set the timestamp offset according to the packet duration. */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200300static int adjust_rtp_timestamp_offset(const struct mgcp_endpoint *endp,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200301 struct mgcp_rtp_state *state,
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200302 const struct mgcp_rtp_end *rtp_end,
303 const struct osmo_sockaddr *addr,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200304 int16_t delta_seq, uint32_t in_timestamp,
305 bool marker_bit)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200306{
307 int32_t tsdelta = state->packet_duration;
308 int timestamp_offset;
309 uint32_t out_timestamp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200310 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200311
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200312 if (marker_bit) {
313 /* If RTP pkt contains marker bit, the timestamps are not longer
314 * in sync, so we can erase timestamp offset patching. */
315 state->patch.timestamp_offset = 0;
316 return 0;
317 }
318
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200319 if (tsdelta == 0) {
320 tsdelta = state->out_stream.last_tsdelta;
321 if (tsdelta != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200322 LOGPENDP(endp, DRTP, LOGL_NOTICE,
323 "A fixed packet duration is not available, "
324 "using last output timestamp delta instead: %d "
325 "from %s:%d\n", tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200326 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
327 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200328 } else {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200329 tsdelta = rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200330 LOGPENDP(endp, DRTP, LOGL_NOTICE,
331 "Fixed packet duration and last timestamp delta "
332 "are not available, "
333 "using fixed 20ms instead: %d "
334 "from %s:%d\n", tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200335 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
336 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200337 }
338 }
339
340 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
341 timestamp_offset = out_timestamp - in_timestamp;
342
Harald Welte33381352017-12-25 09:44:26 +0100343 if (state->patch.timestamp_offset != timestamp_offset) {
344 state->patch.timestamp_offset = timestamp_offset;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200345 LOGPENDP(endp, DRTP, LOGL_NOTICE,
346 "Timestamp offset change on SSRC: %u "
347 "SeqNo delta: %d, TS offset: %d, "
348 "from %s:%d\n", state->in_stream.ssrc,
349 delta_seq, state->patch.timestamp_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200350 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
351 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200352 }
353
354 return timestamp_offset;
355}
356
357/* Set the timestamp offset according to the packet duration. */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200358static int align_rtp_timestamp_offset(const struct mgcp_endpoint *endp,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200359 struct mgcp_rtp_state *state,
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200360 const struct mgcp_rtp_end *rtp_end,
361 const struct osmo_sockaddr *addr,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200362 uint32_t timestamp, bool marker_bit)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200363{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200364 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200365 int ts_error = 0;
366 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200367 int ptime = state->packet_duration;
368
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200369 if (marker_bit) {
370 /* If RTP pkt contains marker bit, the timestamps are not longer
371 * in sync, so no alignment is needed. */
372 return 0;
373 }
374
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200375 /* Align according to: T + Toffs - Tlast = k * Tptime */
376
Philipp Maier87bd9be2017-08-22 16:35:41 +0200377 ts_error = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100378 timestamp + state->patch.timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200379
Philipp Maier87bd9be2017-08-22 16:35:41 +0200380 /* If there is an alignment error, we have to compensate it */
381 if (ts_error) {
Harald Welte33381352017-12-25 09:44:26 +0100382 state->patch.timestamp_offset += ptime - ts_error;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200383 LOGPENDP(endp, DRTP, LOGL_NOTICE,
384 "Corrected timestamp alignment error of %d on SSRC: %u "
385 "new TS offset: %d, "
386 "from %s:%d\n",
387 ts_error, state->in_stream.ssrc,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200388 state->patch.timestamp_offset,
389 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
390 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200391 }
392
Philipp Maier87bd9be2017-08-22 16:35:41 +0200393 /* Check we really managed to compensate the timestamp
394 * offset. There should not be any remaining error, failing
Harald Welte1d1b98f2017-12-25 10:03:40 +0100395 * here would point to a serous problem with the alignment
396 * error computation function */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200397 ts_check = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100398 timestamp + state->patch.timestamp_offset);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200399 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200400
Philipp Maier87bd9be2017-08-22 16:35:41 +0200401 /* Return alignment error before compensation */
402 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200403}
404
Philipp Maier87bd9be2017-08-22 16:35:41 +0200405/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200406 * \param[in] associated endpoint.
407 * \param[in] destination RTP end.
408 * \param[in,out] pointer to buffer with voice data.
409 * \param[in] voice data length.
410 * \param[in] maximum size of caller provided voice data buffer.
411 * \returns ignores input parameters, return always 0. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200412int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
413 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200414 char *data, int *len, int buf_size)
415{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200416 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200417 return 0;
418}
419
Philipp Maier87bd9be2017-08-22 16:35:41 +0200420/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200421 * \param[in] associated endpoint.
422 * \param[in] destination RTP connnection.
423 * \param[in] source RTP connection.
424 * \returns ignores input parameters, return always 0. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200425int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
Philipp Maieracc10352018-07-19 18:07:57 +0200426 struct mgcp_conn_rtp *conn_dst,
427 struct mgcp_conn_rtp *conn_src)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200428{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200429 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200430 return 0;
431}
432
433void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
Philipp Maier58128252019-03-06 11:28:18 +0100434 const struct mgcp_rtp_codec **codec,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200435 const char **fmtp_extra,
436 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200437{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200438 LOGPENDP(endp, DRTP, LOGL_DEBUG, "conn:%s using format defaults\n",
439 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200440
Philipp Maier58128252019-03-06 11:28:18 +0100441 *codec = conn->end.codec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200442 *fmtp_extra = conn->end.fmtp_extra;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200443}
444
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200445void mgcp_rtp_annex_count(const struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200446 struct mgcp_rtp_state *state, const uint16_t seq,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200447 const int32_t transit, const uint32_t ssrc,
448 const bool marker_bit)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200449{
450 int32_t d;
451
452 /* initialize or re-initialize */
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200453 if (!state->stats.initialized || state->stats.ssrc != ssrc || marker_bit) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100454 state->stats.initialized = 1;
455 state->stats.base_seq = seq;
456 state->stats.max_seq = seq - 1;
457 state->stats.ssrc = ssrc;
458 state->stats.jitter = 0;
459 state->stats.transit = transit;
460 state->stats.cycles = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200461 } else {
462 uint16_t udelta;
463
Philipp Maier87bd9be2017-08-22 16:35:41 +0200464 /* The below takes the shape of the validation of
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200465 * Appendix A. Check if there is something weird with
466 * the sequence number, otherwise check for a wrap
467 * around in the sequence number.
468 * It can't wrap during the initialization so let's
469 * skip it here. The Appendix A probably doesn't have
Philipp Maier87bd9be2017-08-22 16:35:41 +0200470 * this issue because of the probation. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100471 udelta = seq - state->stats.max_seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200472 if (udelta < RTP_MAX_DROPOUT) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100473 if (seq < state->stats.max_seq)
474 state->stats.cycles += RTP_SEQ_MOD;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200475 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200476 LOGPENDP(endp, DRTP, LOGL_NOTICE,
477 "RTP seqno made a very large jump on delta: %u\n",
478 udelta);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200479 }
480 }
481
Philipp Maier87bd9be2017-08-22 16:35:41 +0200482 /* Calculate the jitter between the two packages. The TS should be
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200483 * taken closer to the read function. This was taken from the
484 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
Philipp Maier87bd9be2017-08-22 16:35:41 +0200485 * resolution. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100486 d = transit - state->stats.transit;
487 state->stats.transit = transit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200488 if (d < 0)
489 d = -d;
Harald Welte49e3d5a2017-12-25 09:47:57 +0100490 state->stats.jitter += d - ((state->stats.jitter + 8) >> 4);
491 state->stats.max_seq = seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200492}
493
Philipp Maier6931f9a2018-07-26 09:29:31 +0200494/* There may be different payload type numbers negotiated for two connections.
495 * Patch the payload type of an RTP packet so that it uses the payload type
496 * that is valid for the destination connection (conn_dst) */
497static int mgcp_patch_pt(struct mgcp_conn_rtp *conn_src,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200498 struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200499{
500 struct rtp_hdr *rtp_hdr;
501 uint8_t pt_in;
502 int pt_out;
503
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200504 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
505 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
506 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200507 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200508 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200509
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200510 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier6931f9a2018-07-26 09:29:31 +0200511
512 pt_in = rtp_hdr->payload_type;
513 pt_out = mgcp_codec_pt_translate(conn_src, conn_dst, pt_in);
514 if (pt_out < 0)
515 return -EINVAL;
516
517 rtp_hdr->payload_type = (uint8_t) pt_out;
518 return 0;
519}
520
Philipp Maier87bd9be2017-08-22 16:35:41 +0200521/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200522 * some of the supported endpoints (e.g. the nanoBTS) can only handle
523 * one source and this code will patch RTP header to appear as if there
524 * is only one source.
525 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200526 * we receive will be seen as a switch in streams. */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200527void mgcp_patch_and_count(const struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200528 struct mgcp_rtp_state *state,
529 struct mgcp_rtp_end *rtp_end,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200530 struct osmo_sockaddr *addr, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200531{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200532 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200533 uint32_t arrival_time;
534 int32_t transit;
535 uint16_t seq;
536 uint32_t timestamp, ssrc;
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200537 bool marker_bit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200538 struct rtp_hdr *rtp_hdr;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200539 int payload = rtp_end->codec->payload_type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200540 unsigned int len = msgb_length(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200541
542 if (len < sizeof(*rtp_hdr))
543 return;
544
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200545 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200546 seq = ntohs(rtp_hdr->sequence);
547 timestamp = ntohl(rtp_hdr->timestamp);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100548 arrival_time = mgcp_get_current_ts(rtp_end->codec->rate);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200549 ssrc = ntohl(rtp_hdr->ssrc);
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200550 marker_bit = !!rtp_hdr->marker;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200551 transit = arrival_time - timestamp;
552
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200553 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc, marker_bit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200554
555 if (!state->initialized) {
556 state->initialized = 1;
557 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100558 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200559 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200560 state->packet_duration =
561 mgcp_rtp_packet_duration(endp, rtp_end);
Philipp Maier0ec1d4e2018-05-16 11:09:42 +0200562 state->out_stream.last_seq = seq - 1;
563 state->out_stream.ssrc = state->patch.orig_ssrc = ssrc;
564 state->out_stream.last_tsdelta = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200565 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200566 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200567 LOGPENDP(endp, DRTP, LOGL_INFO,
568 "initializing stream, SSRC: %u timestamp: %u "
569 "pkt-duration: %d, from %s:%d\n",
570 state->in_stream.ssrc,
571 state->patch.seq_offset, state->packet_duration,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200572 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
573 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200574 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200575 state->packet_duration =
Philipp Maierbc0346e2018-06-07 09:52:16 +0200576 rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200577 LOGPENDP(endp, DRTP, LOGL_NOTICE,
578 "fixed packet duration is not available, "
579 "using fixed 20ms instead: %d from %s:%d\n",
580 state->packet_duration,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200581 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
582 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200583 }
584 } else if (state->in_stream.ssrc != ssrc) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200585 LOGPENDP(endp, DRTP, LOGL_NOTICE,
586 "SSRC changed: %u -> %u "
587 "from %s:%d\n",
588 state->in_stream.ssrc, rtp_hdr->ssrc,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200589 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
590 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200591
592 state->in_stream.ssrc = ssrc;
593 if (rtp_end->force_constant_ssrc) {
594 int16_t delta_seq;
595
596 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100597 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200598 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200599
600 /* Estimate number of packets that would have been sent */
601 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200602 (arrival_time - state->in_stream.last_arrival_time
603 + state->packet_duration / 2) /
604 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200605
606 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200607 delta_seq, timestamp, marker_bit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200608
Pau Espin Pedrol4c77e9b2021-07-07 12:12:58 +0200609 state->patch.patch_ssrc = true;
Harald Welte33381352017-12-25 09:44:26 +0100610 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200611 if (rtp_end->force_constant_ssrc != -1)
612 rtp_end->force_constant_ssrc -= 1;
613
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200614 LOGPENDP(endp, DRTP, LOGL_NOTICE,
615 "SSRC patching enabled, SSRC: %u "
616 "SeqNo offset: %d, TS offset: %d "
617 "from %s:%d\n", state->in_stream.ssrc,
618 state->patch.seq_offset, state->patch.timestamp_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200619 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
620 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200621 }
622
623 state->in_stream.last_tsdelta = 0;
624 } else {
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200625 if (!marker_bit) {
626 /* Compute current per-packet timestamp delta */
627 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
628 addr, seq, timestamp, "input",
629 &state->in_stream.last_tsdelta);
630 } else {
631 state->in_stream.last_tsdelta = 0;
632 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200633
Harald Welte33381352017-12-25 09:44:26 +0100634 if (state->patch.patch_ssrc)
635 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200636 }
637
638 /* Save before patching */
639 state->in_stream.last_timestamp = timestamp;
640 state->in_stream.last_seq = seq;
641 state->in_stream.last_arrival_time = arrival_time;
642
643 if (rtp_end->force_aligned_timing &&
644 state->out_stream.ssrc == ssrc && state->packet_duration)
645 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200646 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200647 timestamp, marker_bit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200648
649 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100650 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200651 rtp_hdr->ssrc = htonl(ssrc);
652
653 /* Apply the offset and store it back to the packet.
654 * This won't change anything if the offset is 0, so the conditional is
655 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100656 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200657 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100658 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200659 rtp_hdr->timestamp = htonl(timestamp);
660
661 /* Check again, whether the timestamps are still valid */
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200662 if (!marker_bit) {
663 if (state->out_stream.ssrc == ssrc)
664 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
665 addr, seq, timestamp, "output",
666 &state->out_stream.last_tsdelta);
667 } else {
668 state->out_stream.last_tsdelta = 0;
669 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200670
671 /* Save output values */
672 state->out_stream.last_seq = seq;
673 state->out_stream.last_timestamp = timestamp;
674 state->out_stream.ssrc = ssrc;
675
676 if (payload < 0)
677 return;
678
679#if 0
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200680 LOGPENDP(endp, DRTP, LOGL_DEBUG, "payload hdr payload %u -> endp payload %u\n",
681 rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200682 rtp_hdr->payload_type = payload;
683#endif
684}
685
Philipp Maier9fc8a022019-02-20 12:26:52 +0100686/* There are different dialects used to format and transfer voice data. When
687 * the receiving end expects GSM-HR data to be formated after RFC 5993, this
688 * function is used to convert between RFC 5993 and TS 101318, which we normally
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200689 * use.
690 * Return 0 on sucess, negative on errors like invalid data length. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200691static int rfc5993_hr_convert(struct mgcp_endpoint *endp, struct msgb *msg)
Philipp Maier9fc8a022019-02-20 12:26:52 +0100692{
Philipp Maier9fc8a022019-02-20 12:26:52 +0100693 struct rtp_hdr *rtp_hdr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200694 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200695 LOGPENDP(endp, DRTP, LOGL_ERROR, "AMR RTP packet too short (%d < %zu)\n",
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200696 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200697 return -EINVAL;
698 }
699
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200700 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100701
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200702 if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr)) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100703 /* TS 101318 encoding => RFC 5993 encoding */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200704 msgb_put(msg, 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100705 memmove(rtp_hdr->data + 1, rtp_hdr->data, GSM_HR_BYTES);
706 rtp_hdr->data[0] = 0x00;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200707 } else if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr) + 1) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100708 /* RFC 5993 encoding => TS 101318 encoding */
709 memmove(rtp_hdr->data, rtp_hdr->data + 1, GSM_HR_BYTES);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200710 msgb_trim(msg, msgb_length(msg) - 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100711 } else {
712 /* It is possible that multiple payloads occur in one RTP
713 * packet. This is not supported yet. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200714 LOGPENDP(endp, DRTP, LOGL_ERROR,
715 "cannot figure out how to convert RTP packet\n");
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200716 return -ENOTSUP;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100717 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200718 return 0;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100719}
720
Philipp Maier228e5912019-03-05 13:56:59 +0100721/* For AMR RTP two framing modes are defined RFC3267. There is a bandwith
722 * efficient encoding scheme where all fields are packed together one after
723 * another and an octet aligned mode where all fields are aligned to octet
724 * boundaries. This function is used to convert between the two modes */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200725static int amr_oa_bwe_convert(struct mgcp_endpoint *endp, struct msgb *msg,
Philipp Maier228e5912019-03-05 13:56:59 +0100726 bool target_is_oa)
727{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200728 /* NOTE: the msgb has an allocated length of RTP_BUF_SIZE, so there is
Philipp Maier228e5912019-03-05 13:56:59 +0100729 * plenty of space available to store the slightly larger, converted
730 * data */
Philipp Maier228e5912019-03-05 13:56:59 +0100731 struct rtp_hdr *rtp_hdr;
732 unsigned int payload_len;
733 int rc;
734
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200735 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
736 LOGPENDP(endp, DRTP, LOGL_ERROR, "AMR RTP packet too short (%d < %zu)\n", msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200737 return -EINVAL;
738 }
739
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200740 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier228e5912019-03-05 13:56:59 +0100741
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200742 payload_len = msgb_length(msg) - sizeof(struct rtp_hdr);
Philipp Maier228e5912019-03-05 13:56:59 +0100743
744 if (osmo_amr_is_oa(rtp_hdr->data, payload_len)) {
745 if (!target_is_oa)
746 /* Input data is oa an target format is bwe
747 * ==> convert */
748 rc = osmo_amr_oa_to_bwe(rtp_hdr->data, payload_len);
749 else
750 /* Input data is already bew, but we accept it anyway
751 * ==> no conversion needed */
752 rc = payload_len;
753 } else {
754 if (target_is_oa)
755 /* Input data is bwe an target format is oa
756 * ==> convert */
757 rc = osmo_amr_bwe_to_oa(rtp_hdr->data, payload_len,
758 RTP_BUF_SIZE);
759 else
760 /* Input data is already oa, but we accept it anyway
761 * ==> no conversion needed */
762 rc = payload_len;
763 }
764 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200765 LOGPENDP(endp, DRTP, LOGL_ERROR,
766 "AMR RTP packet conversion failed\n");
Philipp Maier228e5912019-03-05 13:56:59 +0100767 return -EINVAL;
768 }
769
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200770 return msgb_trim(msg, rc + sizeof(struct rtp_hdr));
Philipp Maier228e5912019-03-05 13:56:59 +0100771}
772
773/* Check if a conversion between octet-aligned and bandwith-efficient mode is
774 * indicated. */
775static bool amr_oa_bwe_convert_indicated(struct mgcp_rtp_codec *codec)
776{
777 if (codec->param_present == false)
778 return false;
779 if (!codec->param.amr_octet_aligned_present)
780 return false;
781 if (strcmp(codec->subtype_name, "AMR") != 0)
782 return false;
783 return true;
784}
785
786
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200787/* Return whether an RTP packet with AMR payload is in octet-aligned mode.
788 * Return 0 if in bandwidth-efficient mode, 1 for octet-aligned mode, and negative if the RTP data is invalid. */
789static int amr_oa_check(char *data, int len)
Philipp Maier228e5912019-03-05 13:56:59 +0100790{
791 struct rtp_hdr *rtp_hdr;
792 unsigned int payload_len;
793
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200794 if (len < sizeof(struct rtp_hdr))
795 return -EINVAL;
796
Philipp Maier228e5912019-03-05 13:56:59 +0100797 rtp_hdr = (struct rtp_hdr *)data;
798
799 payload_len = len - sizeof(struct rtp_hdr);
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200800 if (payload_len < sizeof(struct amr_hdr))
801 return -EINVAL;
Philipp Maier228e5912019-03-05 13:56:59 +0100802
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200803 return osmo_amr_is_oa(rtp_hdr->data, payload_len) ? 1 : 0;
Philipp Maier228e5912019-03-05 13:56:59 +0100804}
805
Philipp Maier87bd9be2017-08-22 16:35:41 +0200806/* Forward data to a debug tap. This is debug function that is intended for
807 * debugging the voice traffic with tools like gstreamer */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100808void forward_data_tap(int fd, struct mgcp_rtp_tap *tap, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200809{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100810 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200811
Philipp Maiere6f172d2017-11-07 12:00:01 +0100812 if (!tap->enabled)
813 return;
814
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200815 rc = sendto(fd, msgb_data(msg), msgb_length(msg), 0, (struct sockaddr *)&tap->forward,
Philipp Maiere6f172d2017-11-07 12:00:01 +0100816 sizeof(tap->forward));
817
818 if (rc < 0)
819 LOGP(DRTP, LOGL_ERROR,
820 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200821}
822
Philipp Maier889fe7f2020-07-06 17:44:12 +0200823/* Generate an RTP header if it is missing */
824static void gen_rtp_header(struct msgb *msg, struct mgcp_rtp_end *rtp_end,
825 struct mgcp_rtp_state *state)
826{
827 struct rtp_hdr *hdr = (struct rtp_hdr *)msgb_data(msg);
828
829 if (hdr->version > 0)
830 return;
831
832 hdr->version = 2;
833 hdr->payload_type = rtp_end->codec->payload_type;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100834 hdr->timestamp = osmo_htonl(mgcp_get_current_ts(rtp_end->codec->rate));
Philipp Maier889fe7f2020-07-06 17:44:12 +0200835 hdr->sequence = osmo_htons(state->alt_rtp_tx_sequence);
836 hdr->ssrc = state->alt_rtp_tx_ssrc;
837}
838
Philipp Maier87bd9be2017-08-22 16:35:41 +0200839/* Check if the origin (addr) matches the address/port data of the RTP
840 * connections. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200841static int check_rtp_origin(struct mgcp_conn_rtp *conn, struct osmo_sockaddr *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200842{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200843 char ipbuf[INET6_ADDRSTRLEN];
844
Pau Espin Pedrol051eb4d2022-10-04 12:50:28 +0200845 if (osmo_sockaddr_is_any(&conn->end.addr) != 0) {
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200846 switch (conn->conn->mode) {
847 case MGCP_CONN_LOOPBACK:
848 /* HACK: for IuUP, we want to reply with an IuUP Initialization ACK upon the first RTP
849 * message received. We currently hackishly accomplish that by putting the endpoint in
850 * loopback mode and patching over the looped back RTP message to make it look like an
851 * ack. We don't know the femto cell's IP address and port until the RAB Assignment
852 * Response is received, but the nano3G expects an IuUP Initialization Ack before it even
853 * sends the RAB Assignment Response. Hence, if the remote address is 0.0.0.0 and the
854 * MGCP port is in loopback mode, allow looping back the packet to any source. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200855 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
856 "In loopback mode and remote address not set:"
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200857 " allowing data from address: %s\n",
858 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200859 return 0;
860
861 default:
862 /* Receiving early media before the endpoint is configured. Instead of logging
863 * this as an error that occurs on every call, keep it more low profile to not
864 * confuse humans with expected errors. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200865 LOGPCONN(conn->conn, DRTP, LOGL_INFO,
866 "Rx RTP from %s, but remote address not set:"
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200867 " dropping early media\n",
868 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200869 return -1;
870 }
Neels Hofmeyr0063ca22018-07-23 18:12:16 +0200871 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200872
Philipp Maier87bd9be2017-08-22 16:35:41 +0200873 /* Note: Check if the inbound RTP data comes from the same host to
874 * which we send our outgoing RTP traffic. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200875 if (conn->end.addr.u.sa.sa_family != addr->u.sa.sa_family ||
876 (conn->end.addr.u.sa.sa_family == AF_INET &&
877 conn->end.addr.u.sin.sin_addr.s_addr != addr->u.sin.sin_addr.s_addr) ||
878 (conn->end.addr.u.sa.sa_family == AF_INET6 &&
879 memcmp(&conn->end.addr.u.sin6.sin6_addr, &addr->u.sin6.sin6_addr,
880 sizeof(struct in6_addr)))) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200881 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200882 "data from wrong address: %s, ",
883 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Philipp Maierc3413882017-10-27 12:26:54 +0200884 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200885 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200886 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200887 return -1;
888 }
889
Philipp Maier87bd9be2017-08-22 16:35:41 +0200890 /* Note: Usually the remote remote port of the data we receive will be
891 * the same as the remote port where we transmit outgoing RTP traffic
892 * to (set by MDCX). We use this to check the origin of the data for
893 * plausibility. */
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200894 if (osmo_sockaddr_port(&conn->end.addr.u.sa) != osmo_sockaddr_port(&addr->u.sa) &&
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200895 ntohs(conn->end.rtcp_port) != osmo_sockaddr_port(&addr->u.sa)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200896 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200897 "data from wrong source port: %d, ",
898 osmo_sockaddr_port(&addr->u.sa));
Philipp Maierc3413882017-10-27 12:26:54 +0200899 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200900 "expected: %d for RTP or %d for RTCP\n",
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200901 osmo_sockaddr_port(&conn->end.addr.u.sa), ntohs(conn->end.rtcp_port));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200902 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200903 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200904 }
905
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200906 return 0;
907}
908
Philipp Maier87bd9be2017-08-22 16:35:41 +0200909/* Check the if the destination address configuration of an RTP connection
910 * makes sense */
911static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200912{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200913 char ipbuf[INET6_ADDRSTRLEN];
Pau Espin Pedrol051eb4d2022-10-04 12:50:28 +0200914 bool ip_is_any = osmo_sockaddr_is_any(&conn->end.addr) != 0;
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200915 uint16_t port = osmo_sockaddr_port(&conn->end.addr.u.sa);
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200916
Philipp Maiere6df0e42018-05-29 14:03:06 +0200917 /* Note: it is legal to create a connection but never setting a port
918 * and IP-address for outgoing data. */
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200919 if (ip_is_any && port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200920 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
Neels Hofmeyra4b677c2021-07-07 23:38:23 +0200921 "destination IP-address and rtp port is not (yet) known (%s:%u)\n",
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200922 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), port);
Philipp Maiere6df0e42018-05-29 14:03:06 +0200923 return -1;
924 }
925
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200926 if (ip_is_any) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200927 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
928 "destination IP-address is invalid (%s:%u)\n",
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200929 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), port);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200930 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200931 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200932
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200933 if (port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200934 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
935 "destination rtp port is invalid (%s:%u)\n",
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200936 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), port);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200937 return -1;
938 }
939
940 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200941}
942
Philipp Maier4dba7692018-08-03 12:20:52 +0200943/* Do some basic checks to make sure that the RTCP packets we are going to
944 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200945static int check_rtcp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +0200946{
947 struct rtcp_hdr *hdr;
948 unsigned int len;
949 uint8_t type;
950
951 /* RTPC packets that are just a header without data do not make
952 * any sense. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200953 if (msgb_length(msg) < sizeof(struct rtcp_hdr)) {
954 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP packet too short (%u < %zu)\n",
955 msgb_length(msg), sizeof(struct rtcp_hdr));
Philipp Maier4dba7692018-08-03 12:20:52 +0200956 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200957 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200958
959 /* Make sure that the length of the received packet does not exceed
960 * the available buffer size */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200961 hdr = (struct rtcp_hdr *)msgb_data(msg);
Philipp Maier4dba7692018-08-03 12:20:52 +0200962 len = (osmo_ntohs(hdr->length) + 1) * 4;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200963 if (len > msgb_length(msg)) {
964 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header length exceeds packet size (%u > %u)\n",
965 len, msgb_length(msg));
Philipp Maier4dba7692018-08-03 12:20:52 +0200966 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200967 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200968
969 /* Make sure we accept only packets that have a proper packet type set
970 * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
971 type = hdr->type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200972 if ((type < 192 || type > 195) && (type < 200 || type > 213)) {
973 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header: invalid type: %u\n", type);
Philipp Maier4dba7692018-08-03 12:20:52 +0200974 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200975 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200976
977 return 0;
978}
979
980/* Do some basic checks to make sure that the RTP packets we are going to
981 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200982static int check_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +0200983{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200984 size_t min_size = sizeof(struct rtp_hdr);
985 if (msgb_length(msg) < min_size) {
986 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
987 msgb_length(msg), min_size);
988 return -1;
989 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200990
991 /* FIXME: Add more checks, the reason why we do not check more than
992 * the length is because we currently handle IUUP packets as RTP
993 * packets, so they must pass this check, if we weould be more
994 * strict here, we would possibly break 3G. (see also FIXME note
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100995 * below.*/
Philipp Maier4dba7692018-08-03 12:20:52 +0200996
997 return 0;
998}
999
Philipp Maier87bd9be2017-08-22 16:35:41 +02001000/* Send RTP data. Possible options are standard RTP packet
1001 * transmission or trsmission via an osmux connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001002static int mgcp_send_rtp(struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001003{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001004 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1005 enum rtp_proto proto = mc->proto;
1006 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001007 struct mgcp_endpoint *endp = conn_src->conn->endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001008
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001009 LOGPENDP(endp, DRTP, LOGL_DEBUG, "destin conn:%s\n",
1010 mgcp_conn_dump(conn_dst->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001011
1012 /* Before we try to deliver the packet, we check if the destination
1013 * port and IP-Address make sense at all. If not, we will be unable
1014 * to deliver the packet. */
1015 if (check_rtp_destin(conn_dst) != 0)
1016 return -1;
1017
1018 /* Depending on the RTP connection type, deliver the RTP packet to the
1019 * destination connection. */
1020 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001021 case MGCP_RTP_DEFAULT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001022 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1023 "endpoint type is MGCP_RTP_DEFAULT, "
1024 "using mgcp_send() to forward data directly\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001025 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001026 mc->from_addr, msg, conn_src, conn_dst);
Pau Espin Pedrol9d939b62022-10-03 16:59:20 +02001027 case MGCP_RTP_OSMUX:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001028 LOGPENDP(endp, DRTP, LOGL_DEBUG,
Pau Espin Pedrol9d939b62022-10-03 16:59:20 +02001029 "endpoint type is MGCP_RTP_OSMUX, "
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001030 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n");
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001031 return osmux_xfrm_to_osmux((char*)msgb_data(msg), msgb_length(msg), conn_dst);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001032 case MGCP_RTP_IUUP:
1033 if (proto == MGCP_PROTO_RTP) {
1034 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1035 "endpoint type is MGCP_RTP_IUUP, "
1036 "using mgcp_conn_iuup_send_rtp() to forward data over IuUP\n");
1037 return mgcp_conn_iuup_send_rtp(conn_src, conn_dst, msg);
1038 }
1039 /* RTCP: we forward as usual for regular RTP connection */
1040 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1041 "endpoint type is MGCP_RTP_IUUP and proto!=MGCP_PROTO_RTP, "
1042 "using mgcp_send() to forward data directly\n");
1043 return mgcp_send(endp, false,
1044 mc->from_addr, msg, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001045 }
1046
Philipp Maier87bd9be2017-08-22 16:35:41 +02001047 /* If the data has not been handled/forwarded until here, it will
1048 * be discarded, this should not happen, normally the MGCP type
1049 * should be properly set */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001050 LOGPENDP(endp, DRTP, LOGL_ERROR, "bad MGCP type -- data discarded!\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001051
1052 return -1;
1053}
1054
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001055/*! send udp packet.
1056 * \param[in] fd associated file descriptor.
1057 * \param[in] addr destination ip-address.
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001058 * \param[in] buf buffer that holds the data to be send.
1059 * \param[in] len length of the data to be sent.
1060 * \returns bytes sent, -1 on error. */
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001061int mgcp_udp_send(int fd, const struct osmo_sockaddr *addr, const char *buf, int len)
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001062{
1063 char ipbuf[INET6_ADDRSTRLEN];
1064 size_t addr_len;
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001065
1066 LOGP(DRTP, LOGL_DEBUG,
1067 "sending %i bytes length packet to %s:%u ...\n", len,
1068 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001069 osmo_sockaddr_port(&addr->u.sa));
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001070
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001071 if (addr->u.sa.sa_family == AF_INET6) {
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001072 addr_len = sizeof(addr->u.sin6);
1073 } else {
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001074 addr_len = sizeof(addr->u.sin);
1075 }
1076
1077 return sendto(fd, buf, len, 0, &addr->u.sa, addr_len);
1078}
1079
1080/*! send RTP dummy packet (to keep NAT connection open).
1081 * \param[in] endp mcgp endpoint that holds the RTP connection.
1082 * \param[in] conn associated RTP connection.
1083 * \returns bytes sent, -1 on error. */
1084int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
1085{
1086 int rc;
1087 int was_rtcp = 0;
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001088 struct osmo_sockaddr rtcp_addr;
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001089
1090 OSMO_ASSERT(endp);
1091 OSMO_ASSERT(conn);
1092
1093 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG, "sending dummy packet... %s\n",
1094 mgcp_conn_dump(conn->conn));
1095
Pau Espin Pedrola24dcc62021-07-06 17:48:47 +02001096 /* Before we try to deliver the packet, we check if the destination
1097 * port and IP-Address make sense at all. If not, we will be unable
1098 * to deliver the packet. */
1099 if (check_rtp_destin(conn) != 0)
1100 goto failed;
1101
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001102 if (mgcp_conn_rtp_is_iuup(conn))
1103 rc = mgcp_conn_iuup_send_dummy(conn);
1104 else
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001105 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001106 rtp_dummy_payload, sizeof(rtp_dummy_payload));
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001107
1108 if (rc == -1)
1109 goto failed;
1110
1111 if (endp->trunk->omit_rtcp)
1112 return rc;
1113
1114 was_rtcp = 1;
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001115 rtcp_addr = conn->end.addr;
1116 osmo_sockaddr_set_port(&rtcp_addr.u.sa, ntohs(conn->end.rtcp_port));
1117 rc = mgcp_udp_send(conn->end.rtcp.fd, &rtcp_addr,
1118 rtp_dummy_payload, sizeof(rtp_dummy_payload));
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001119
1120 if (rc >= 0)
1121 return rc;
1122
1123failed:
1124 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1125 "Failed to send dummy %s packet.\n",
1126 was_rtcp ? "RTCP" : "RTP");
1127
1128 return -1;
1129}
1130
1131/*! Send RTP/RTCP data to a specified destination connection.
1132 * \param[in] endp associated endpoint (for configuration, logging).
1133 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP.
1134 * \param[in] spoofed source address (set to NULL to disable).
1135 * \param[in] buf buffer that contains the RTP/RTCP data.
1136 * \param[in] len length of the buffer that contains the RTP/RTCP data.
1137 * \param[in] conn_src associated source connection.
1138 * \param[in] conn_dst associated destination connection.
1139 * \returns 0 on success, -1 on ERROR. */
1140int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct osmo_sockaddr *addr,
1141 struct msgb *msg, struct mgcp_conn_rtp *conn_src,
1142 struct mgcp_conn_rtp *conn_dst)
1143{
1144 /*! When no destination connection is available (e.g. when only one
1145 * connection in loopback mode exists), then the source connection
1146 * shall be specified as destination connection */
1147
1148 struct mgcp_trunk *trunk = endp->trunk;
1149 struct mgcp_rtp_end *rtp_end;
1150 struct mgcp_rtp_state *rtp_state;
1151 char ipbuf[INET6_ADDRSTRLEN];
1152 char *dest_name;
1153 int rc;
1154 int len;
1155
1156 OSMO_ASSERT(conn_src);
1157 OSMO_ASSERT(conn_dst);
1158
1159 if (is_rtp)
1160 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTP packet...\n");
1161 else
1162 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTCP packet...\n");
1163
1164 /* FIXME: It is legal that the payload type on the egress connection is
1165 * different from the payload type that has been negotiated on the
1166 * ingress connection. Essentially the codecs are the same so we can
1167 * match them and patch the payload type. However, if we can not find
1168 * the codec pendant (everything ist equal except the PT), we are of
1169 * course unable to patch the payload type. A situation like this
1170 * should not occur if transcoding is consequently avoided. Until
1171 * we have transcoding support in osmo-mgw we can not resolve this. */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001172 if (is_rtp && conn_dst->type != MGCP_RTP_IUUP) {
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001173 rc = mgcp_patch_pt(conn_src, conn_dst, msg);
1174 if (rc < 0) {
1175 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1176 "can not patch PT because no suitable egress codec was found.\n");
1177 }
1178 }
1179
1180 /* Note: In case of loopback configuration, both, the source and the
1181 * destination will point to the same connection. */
1182 rtp_end = &conn_dst->end;
1183 rtp_state = &conn_src->state;
1184 dest_name = conn_dst->conn->name;
1185
1186 /* Ensure we have an alternative SSRC in case we need it, see also
1187 * gen_rtp_header() */
1188 if (rtp_state->alt_rtp_tx_ssrc == 0)
1189 rtp_state->alt_rtp_tx_ssrc = rand();
1190
1191 if (!rtp_end->output_enabled) {
1192 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_DROPPED_PACKETS_CTR);
1193 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1194 "output disabled, drop to %s %s "
1195 "rtp_port:%u rtcp_port:%u\n",
1196 dest_name,
1197 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001198 osmo_sockaddr_port(&rtp_end->addr.u.sa), ntohs(rtp_end->rtcp_port)
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001199 );
1200 } else if (is_rtp) {
1201 int cont;
1202 int nbytes = 0;
1203 int buflen = msgb_length(msg);
1204
1205 /* Make sure we have a valid RTP header, in cases where no RTP
1206 * header is present, we will generate one. */
1207 gen_rtp_header(msg, rtp_end, rtp_state);
1208
1209 do {
1210 /* Run transcoder */
Ericfbf78d12021-08-23 22:31:39 +02001211 cont = endp->trunk->cfg->rtp_processing_cb(endp, rtp_end, (char *)msgb_data(msg), &buflen, RTP_BUF_SIZE);
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001212 if (cont < 0)
1213 break;
1214
1215 if (addr)
1216 mgcp_patch_and_count(endp, rtp_state, rtp_end,
1217 addr, msg);
1218
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001219 if (mgcp_conn_rtp_is_iuup(conn_dst) || mgcp_conn_rtp_is_iuup(conn_src)) {
1220 /* the iuup code will correctly transform to the correct AMR mode */
1221 } else if (amr_oa_bwe_convert_indicated(conn_dst->end.codec)) {
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001222 rc = amr_oa_bwe_convert(endp, msg,
1223 conn_dst->end.codec->param.amr_octet_aligned);
1224 if (rc < 0) {
1225 LOGPENDP(endp, DRTP, LOGL_ERROR,
1226 "Error in AMR octet-aligned <-> bandwidth-efficient mode conversion\n");
1227 break;
1228 }
1229 } else if (rtp_end->rfc5993_hr_convert &&
1230 strcmp(conn_src->end.codec->subtype_name, "GSM-HR-08") == 0) {
1231 rc = rfc5993_hr_convert(endp, msg);
1232 if (rc < 0) {
1233 LOGPENDP(endp, DRTP, LOGL_ERROR, "Error while converting to GSM-HR-08\n");
1234 break;
1235 }
1236 }
1237
1238 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1239 "process/send to %s %s "
1240 "rtp_port:%u rtcp_port:%u\n",
1241 dest_name,
1242 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001243 osmo_sockaddr_port(&rtp_end->addr.u.sa), ntohs(rtp_end->rtcp_port)
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001244 );
1245
1246 /* Forward a copy of the RTP data to a debug ip/port */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001247 forward_data_tap(rtp_end->rtp.fd, &conn_src->tap_out,
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001248 msg);
1249
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001250 len = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr,
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001251 (char *)msgb_data(msg), msgb_length(msg));
1252
1253 if (len <= 0)
1254 return len;
1255
1256 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
1257 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
1258 rtp_state->alt_rtp_tx_sequence++;
1259
1260 nbytes += len;
1261 buflen = cont;
1262 } while (buflen > 0);
1263 return nbytes;
1264 } else if (!trunk->omit_rtcp) {
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001265 struct osmo_sockaddr rtcp_addr = rtp_end->addr;
1266 osmo_sockaddr_set_port(&rtcp_addr.u.sa, rtp_end->rtcp_port);
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001267 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1268 "send to %s %s rtp_port:%u rtcp_port:%u\n",
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001269 dest_name, osmo_sockaddr_ntop(&rtcp_addr.u.sa, ipbuf),
1270 osmo_sockaddr_port(&rtp_end->addr.u.sa),
1271 osmo_sockaddr_port(&rtcp_addr.u.sa)
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001272 );
1273
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001274 len = mgcp_udp_send(rtp_end->rtcp.fd, &rtcp_addr,
1275 (char *)msgb_data(msg), msgb_length(msg));
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001276
1277 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
1278 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
1279 rtp_state->alt_rtp_tx_sequence++;
1280
1281 return len;
1282 }
1283
1284 return 0;
1285}
1286
Pau Espin Pedrolde805b62022-10-03 16:08:58 +02001287/*! Dispatch incoming RTP packet to opposite RTP connection.
1288 * \param[in] msg Message buffer to bridge, coming from source connection.
1289 * msg shall contain "struct osmo_rtp_msg_ctx *" attached in
1290 * "OSMO_RTP_MSG_CTX(msg)".
1291 * \returns 0 on success, -1 on ERROR.
1292 */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001293int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001294{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001295 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1296 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1297 struct mgcp_conn *conn = conn_src->conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001298 struct mgcp_conn *conn_dst;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001299 struct osmo_sockaddr *from_addr = mc->from_addr;
Philipp Maier97a93122021-05-07 22:50:31 +02001300 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001301
1302 /*! NOTE: This callback function implements the endpoint specific
Alexander Chemeris61cf9bb2020-05-11 18:13:53 +03001303 * dispatch behaviour of an rtp bridge/proxy endpoint. It is assumed
Philipp Maier87bd9be2017-08-22 16:35:41 +02001304 * that the endpoint will hold only two connections. This premise
1305 * is used to determine the opposite connection (it is always the
1306 * connection that is not the originating connection). Once the
1307 * destination connection is known the RTP packet is sent via
1308 * the destination connection. */
1309
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001310 /* If source is IuUP, we need to handle state, forward it through specific bridge path: */
1311 if (mgcp_conn_rtp_is_iuup(conn_src) && mc->proto == MGCP_PROTO_RTP)
1312 return mgcp_conn_iuup_dispatch_rtp(msg);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001313
1314 /* Check if the connection is in loopback mode, if yes, just send the
1315 * incoming data back to the origin */
1316 if (conn->mode == MGCP_CONN_LOOPBACK) {
1317 /* When we are in loopback mode, we loop back all incoming
1318 * packets back to their origin. We will use the originating
1319 * address data from the UDP packet header to patch the
1320 * outgoing address in connection on the fly */
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001321 if (osmo_sockaddr_port(&conn->u.rtp.end.addr.u.sa) == 0) {
Philipp Maier97a93122021-05-07 22:50:31 +02001322 memcpy(&conn->u.rtp.end.addr, from_addr,
1323 sizeof(conn->u.rtp.end.addr));
Philipp Maier97a93122021-05-07 22:50:31 +02001324 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1325 "loopback mode: implicitly using source address (%s:%u) as destination address\n",
1326 osmo_sockaddr_ntop(&from_addr->u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001327 osmo_sockaddr_port(&conn->u.rtp.end.addr.u.sa));
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001328 }
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001329 return mgcp_send_rtp(conn_src, msg);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001330 }
1331
Philipp Maier87bd9be2017-08-22 16:35:41 +02001332 /* Find a destination connection. */
1333 /* NOTE: This code path runs every time an RTP packet is received. The
1334 * function mgcp_find_dst_conn() we use to determine the detination
1335 * connection will iterate the connection list inside the endpoint.
1336 * Since list iterations are quite costly, we will figure out the
1337 * destination only once and use the optional private data pointer of
1338 * the connection to cache the destination connection pointer. */
1339 if (!conn->priv) {
1340 conn_dst = mgcp_find_dst_conn(conn);
1341 conn->priv = conn_dst;
1342 } else {
1343 conn_dst = (struct mgcp_conn *)conn->priv;
1344 }
1345
1346 /* There is no destination conn, stop here */
1347 if (!conn_dst) {
Alexander Chemerisebb9bf32020-05-11 18:14:31 +03001348 LOGPCONN(conn, DRTP, LOGL_DEBUG,
1349 "no connection to forward an incoming RTP packet to\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001350 return -1;
1351 }
1352
1353 /* The destination conn is not an RTP connection */
1354 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001355 LOGPCONN(conn, DRTP, LOGL_ERROR,
1356 "unable to find suitable destination conn\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001357 return -1;
1358 }
1359
1360 /* Dispatch RTP packet to destination RTP connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001361 return mgcp_send_rtp(&conn_dst->u.rtp, msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001362}
1363
Philipp Maier0996a1e2020-06-10 15:27:14 +02001364/*! dispatch incoming RTP packet to E1 subslot, handle RTCP packets locally.
1365 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1366 * \param[in] addr socket address where the RTP packet has been received from.
1367 * \param[in] buf buffer that hold the RTP payload.
1368 * \param[in] buf_size size data length of buf.
1369 * \param[in] conn originating connection.
1370 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001371int mgcp_dispatch_e1_bridge_cb(struct msgb *msg)
Philipp Maier0996a1e2020-06-10 15:27:14 +02001372{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001373 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1374 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1375 struct mgcp_conn *conn = conn_src->conn;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001376 struct osmo_sockaddr *from_addr = mc->from_addr;
Philipp Maier97a93122021-05-07 22:50:31 +02001377 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001378
Philipp Maier889fe7f2020-07-06 17:44:12 +02001379 /* Check if the connection is in loopback mode, if yes, just send the
1380 * incoming data back to the origin */
1381 if (conn->mode == MGCP_CONN_LOOPBACK) {
1382 /* When we are in loopback mode, we loop back all incoming
1383 * packets back to their origin. We will use the originating
1384 * address data from the UDP packet header to patch the
1385 * outgoing address in connection on the fly */
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001386 if (osmo_sockaddr_port(&conn->u.rtp.end.addr.u.sa) == 0) {
Philipp Maier97a93122021-05-07 22:50:31 +02001387 memcpy(&conn->u.rtp.end.addr, from_addr,
1388 sizeof(conn->u.rtp.end.addr));
Philipp Maier97a93122021-05-07 22:50:31 +02001389 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1390 "loopback mode: implicitly using source address (%s:%u) as destination address\n",
1391 osmo_sockaddr_ntop(&from_addr->u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001392 osmo_sockaddr_port(&conn->u.rtp.end.addr.u.sa));
Philipp Maier889fe7f2020-07-06 17:44:12 +02001393 }
1394 return mgcp_send_rtp(conn_src, msg);
1395 }
1396
1397 /* Forward to E1 */
1398 return mgcp_e1_send_rtp(conn->endp, conn->u.rtp.end.codec, msg);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001399}
1400
Philipp Maierdf5d2192018-01-24 11:39:32 +01001401/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1402 * \param[in] endp Endpoint on which the connection resides.
Philipp Maier08eb9352020-06-18 11:55:35 +02001403 * \param[in] conn Connection that is about to be removed (ignored). */
Philipp Maierdf5d2192018-01-24 11:39:32 +01001404void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1405{
1406 struct mgcp_conn *conn_cleanup;
1407
1408 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1409 * pointer to the destination connection, so that we do not have
1410 * to go through the list every time an RTP packet arrives. To prevent
1411 * a use-after-free situation we invalidate this information for all
1412 * connections present when one connection is removed from the
1413 * endpoint. */
1414 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
Pau Espin Pedrol6d0a59a2020-09-08 16:50:22 +02001415 if (conn_cleanup->priv == conn)
1416 conn_cleanup->priv = NULL;
Philipp Maierdf5d2192018-01-24 11:39:32 +01001417 }
1418}
1419
Philipp Maier0996a1e2020-06-10 15:27:14 +02001420/*! cleanup an endpoint when a connection on an E1 endpoint is removed.
1421 * \param[in] endp Endpoint on which the connection resides.
1422 * \param[in] conn Connection that is about to be removed (ignored). */
1423void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1424{
Philipp Maier889fe7f2020-07-06 17:44:12 +02001425 /* Cleanup tasks for E1 are the same as for regular endpoint. The
1426 * shut down of the E1 part is handled separately. */
1427 mgcp_cleanup_rtp_bridge_cb(endp, conn);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001428}
1429
Philipp Maier87bd9be2017-08-22 16:35:41 +02001430/* Handle incoming RTP data from NET */
1431static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1432{
1433 /* NOTE: This is a generic implementation. RTP data is received. In
1434 * case of loopback the data is just sent back to its origin. All
1435 * other cases implement endpoint specific behaviour (e.g. how is the
1436 * destination connection determined?). That specific behaviour is
1437 * implemented by the callback function that is called at the end of
1438 * the function */
1439
1440 struct mgcp_conn_rtp *conn_src;
1441 struct mgcp_endpoint *endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001442 struct osmo_sockaddr addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001443 socklen_t slen = sizeof(addr);
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001444 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001445 int ret;
1446 enum rtp_proto proto;
1447 struct osmo_rtp_msg_ctx *mc;
Eric8f333032021-08-13 00:00:43 +02001448 struct msgb *msg;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001449 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001450
1451 conn_src = (struct mgcp_conn_rtp *)fd->data;
1452 OSMO_ASSERT(conn_src);
1453 endp = conn_src->conn->endp;
1454 OSMO_ASSERT(endp);
Eric8f333032021-08-13 00:00:43 +02001455 msg = msgb_alloc_c(endp->trunk, RTP_BUF_SIZE, "RTP-rx");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001456
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001457 proto = (fd == &conn_src->end.rtp)? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001458
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001459 ret = recvfrom(fd->fd, msgb_data(msg), msg->data_len, 0, (struct sockaddr *)&addr.u.sa, &slen);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001460
1461 if (ret <= 0) {
1462 LOG_CONN_RTP(conn_src, LOGL_ERROR, "recvfrom error: %s\n", strerror(errno));
1463 rc = -1;
1464 goto out;
1465 }
1466
1467 msgb_put(msg, ret);
1468
1469 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "%s: rx %u bytes from %s:%u\n",
Philipp Maier069dd162022-03-30 16:09:22 +02001470 proto == MGCP_PROTO_RTP ? "RTP" : "RTCP",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001471 msgb_length(msg), osmo_sockaddr_ntop(&addr.u.sa, ipbuf),
1472 osmo_sockaddr_port(&addr.u.sa));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001473
1474 if ((proto == MGCP_PROTO_RTP && check_rtp(conn_src, msg))
1475 || (proto == MGCP_PROTO_RTCP && check_rtcp(conn_src, msg))) {
1476 /* Logging happened in the two check_ functions */
1477 rc = -1;
1478 goto out;
1479 }
1480
Philipp Maierb3d14eb2021-05-20 14:18:52 +02001481 if (mgcp_is_rtp_dummy_payload(msg)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001482 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx dummy packet (dropped)\n");
1483 rc = 0;
1484 goto out;
1485 }
1486
1487 /* Since the msgb remains owned and freed by this function, the msg ctx data struct can just be on the stack and
1488 * needs not be allocated with the msgb. */
1489 mc = OSMO_RTP_MSG_CTX(msg);
1490 *mc = (struct osmo_rtp_msg_ctx){
1491 .proto = proto,
1492 .conn_src = conn_src,
1493 .from_addr = &addr,
1494 };
1495 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "msg ctx: %d %p %s\n",
1496 mc->proto, mc->conn_src,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001497 osmo_hexdump((void*)mc->from_addr,
1498 mc->from_addr->u.sa.sa_family == AF_INET6 ?
1499 sizeof(struct sockaddr_in6) :
1500 sizeof(struct sockaddr_in)));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001501
1502 /* Increment RX statistics */
Pau Espin Pedroldaf5bce2022-09-22 19:14:24 +02001503 rate_ctr_inc(rate_ctr_group_get_ctr(conn_src->ctrg, RTP_PACKETS_RX_CTR));
1504 rate_ctr_add(rate_ctr_group_get_ctr(conn_src->ctrg, RTP_OCTETS_RX_CTR), msgb_length(msg));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001505 /* FIXME: count RTP and RTCP separately, also count IuUP payload-less separately */
1506
1507 /* Forward a copy of the RTP data to a debug ip/port */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001508 forward_data_tap(fd->fd, &conn_src->tap_in, msg);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001509
1510 rc = rx_rtp(msg);
1511
1512out:
1513 msgb_free(msg);
1514 return rc;
1515}
1516
Philipp Maiere1442752022-03-30 16:06:15 +02001517/* Note: This function is able to handle RTP and RTCP */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001518static int rx_rtp(struct msgb *msg)
1519{
1520 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1521 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001522 struct osmo_sockaddr *from_addr = mc->from_addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001523 struct mgcp_conn *conn = conn_src->conn;
1524 struct mgcp_trunk *trunk = conn->endp->trunk;
1525
1526 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx_rtp(%u bytes)\n", msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001527
Oliver Smithe36b7752019-01-22 16:31:36 +01001528 mgcp_conn_watchdog_kick(conn_src->conn);
1529
Philipp Maier228e5912019-03-05 13:56:59 +01001530 /* If AMR is configured for the ingress connection a conversion of the
1531 * framing mode (octet-aligned vs. bandwith-efficient is explicitly
1532 * define, then we check if the incoming payload matches that
1533 * expectation. */
Philipp Maiere1442752022-03-30 16:06:15 +02001534 if (mc->proto == MGCP_PROTO_RTP &&
1535 amr_oa_bwe_convert_indicated(conn_src->end.codec)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001536 int oa = amr_oa_check((char*)msgb_data(msg), msgb_length(msg));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +02001537 if (oa < 0)
1538 return -1;
1539 if (((bool)oa) != conn_src->end.codec->param.amr_octet_aligned)
Philipp Maier228e5912019-03-05 13:56:59 +01001540 return -1;
1541 }
1542
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001543 /* Check if the origin of the RTP packet seems plausible */
1544 if (!trunk->rtp_accept_all && check_rtp_origin(conn_src, from_addr))
1545 return -1;
1546
Philipp Maier87bd9be2017-08-22 16:35:41 +02001547 /* Execute endpoint specific implementation that handles the
1548 * dispatching of the RTP data */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001549 return conn->endp->type->dispatch_rtp_cb(msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001550}
1551
Philipp Maier87bd9be2017-08-22 16:35:41 +02001552/*! bind RTP port to osmo_fd.
Philipp Maier0b79d212020-06-18 12:02:49 +02001553 * \param[in] source_addr source (local) address to bind on.
1554 * \param[in] fd associated file descriptor.
1555 * \param[in] port to bind on.
Harald Welte55a92292021-04-28 19:06:34 +02001556 * \param[in] dscp IP DSCP value to use.
1557 * \param[in] prio socket priority to use.
Philipp Maier0b79d212020-06-18 12:02:49 +02001558 * \returns 0 on success, -1 on ERROR. */
Harald Welte55a92292021-04-28 19:06:34 +02001559int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port, uint8_t dscp,
1560 uint8_t prio)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001561{
Harald Welte8890dfa2017-11-17 15:09:30 +01001562 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001563
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001564 rc = osmo_sock_init2(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
Harald Welte55a92292021-04-28 19:06:34 +02001565 NULL, 0, OSMO_SOCK_F_BIND | OSMO_SOCK_F_DSCP(dscp) |
1566 OSMO_SOCK_F_PRIO(prio));
Harald Welte8890dfa2017-11-17 15:09:30 +01001567 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001568 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001569 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001570 return -1;
1571 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001572 fd->fd = rc;
1573 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001574
1575 return 0;
1576}
1577
Philipp Maier87bd9be2017-08-22 16:35:41 +02001578/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001579static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001580 struct mgcp_rtp_end *rtp_end, struct mgcp_endpoint *endp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001581{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001582 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1583 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1584
Harald Welte55a92292021-04-28 19:06:34 +02001585 if (mgcp_create_bind(source_addr, &rtp_end->rtp, rtp_end->local_port,
1586 cfg->endp_dscp, cfg->endp_priority) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001587 LOGPENDP(endp, DRTP, LOGL_ERROR,
1588 "failed to create RTP port: %s:%d\n",
1589 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001590 goto cleanup0;
1591 }
1592
Harald Welte55a92292021-04-28 19:06:34 +02001593 if (mgcp_create_bind(source_addr, &rtp_end->rtcp, rtp_end->local_port + 1,
1594 cfg->endp_dscp, cfg->endp_priority) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001595 LOGPENDP(endp, DRTP, LOGL_ERROR,
1596 "failed to create RTCP port: %s:%d\n",
1597 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001598 goto cleanup1;
1599 }
1600
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001601 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001602 LOGPENDP(endp, DRTP, LOGL_ERROR,
1603 "failed to register RTP port %d\n",
1604 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001605 goto cleanup2;
1606 }
1607
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001608 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001609 LOGPENDP(endp, DRTP, LOGL_ERROR,
1610 "failed to register RTCP port %d\n",
1611 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001612 goto cleanup3;
1613 }
1614
1615 return 0;
1616
1617cleanup3:
1618 osmo_fd_unregister(&rtp_end->rtp);
1619cleanup2:
1620 close(rtp_end->rtcp.fd);
1621 rtp_end->rtcp.fd = -1;
1622cleanup1:
1623 close(rtp_end->rtp.fd);
1624 rtp_end->rtp.fd = -1;
1625cleanup0:
1626 return -1;
1627}
1628
Philipp Maier87bd9be2017-08-22 16:35:41 +02001629/*! bind RTP port to endpoint/connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001630 * \param[in] endp endpoint that holds the RTP connection.
1631 * \param[in] rtp_port port number to bind on.
1632 * \param[in] conn associated RTP connection.
1633 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001634int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1635 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001636{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001637 char name[512];
1638 struct mgcp_rtp_end *end;
1639
Philipp Maier01d24a32017-11-21 17:26:09 +01001640 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001641 end = &conn->end;
1642
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001643 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001644 LOGPENDP(endp, DRTP, LOGL_ERROR, "%u was already bound on conn:%s\n",
1645 rtp_port, mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001646
1647 /* Double bindings should never occour! Since we always allocate
1648 * connections dynamically and free them when they are not
1649 * needed anymore, there must be no previous binding leftover.
1650 * Should there be a connection bound twice, we have a serious
1651 * problem and must exit immediately! */
1652 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001653 }
1654
1655 end->local_port = rtp_port;
Harald Weltec2a8f592020-10-19 13:25:41 +02001656 osmo_fd_setup(&end->rtp, -1, OSMO_FD_READ, rtp_data_net, conn, 0);
1657 osmo_fd_setup(&end->rtcp, -1, OSMO_FD_READ, rtp_data_net, conn, 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001658
Ericfbf78d12021-08-23 22:31:39 +02001659 return bind_rtp(endp->trunk->cfg, conn->end.local_addr, end, endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001660}
1661
Philipp Maier87bd9be2017-08-22 16:35:41 +02001662/*! free allocated RTP and RTCP ports.
1663 * \param[in] end RTP end */
1664void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001665{
1666 if (end->rtp.fd != -1) {
1667 close(end->rtp.fd);
1668 end->rtp.fd = -1;
1669 osmo_fd_unregister(&end->rtp);
1670 }
1671
1672 if (end->rtcp.fd != -1) {
1673 close(end->rtcp.fd);
1674 end->rtcp.fd = -1;
1675 osmo_fd_unregister(&end->rtcp);
1676 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001677}