blob: 86a63300f98f12d8dd7d5da0b4cb1e6c7f4b8d40 [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 Pedrol051eb4d2022-10-04 12:50:28 +020077 return rtp_end->rtp_port && (osmo_sockaddr_is_any(&rtp_end->addr) == 0);
Pau Espin Pedrolca280a12021-07-06 18:15:35 +020078}
79
Philipp Maier1cb1e382017-11-02 17:16:04 +010080/*! Determine the local rtp bind IP-address.
Philipp Maier0b79d212020-06-18 12:02:49 +020081 * \param[out] addr caller provided memory to store the resulting IP-Address.
82 * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters.
Philipp Maier1cb1e382017-11-02 17:16:04 +010083 *
84 * The local bind IP-address is automatically selected by probing the
85 * IP-Address of the interface that is pointing towards the remote IP-Address,
86 * if no remote IP-Address is known yet, the statically configured
87 * IP-Addresses are used as fallback. */
88void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn)
89{
90
91 struct mgcp_endpoint *endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +020092 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier1cb1e382017-11-02 17:16:04 +010093 int rc;
94 endp = conn->conn->endp;
Pau Espin Pedrol051eb4d2022-10-04 12:50:28 +020095 bool rem_addr_set = osmo_sockaddr_is_any(&conn->end.addr) == 0;
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +020096 char *bind_addr;
Philipp Maier1cb1e382017-11-02 17:16:04 +010097
Pau Espin Pedrol479cf762022-09-07 19:42:54 +020098 /* Osmux: No smart IP addresses allocation is supported yet. Simply
99 * return the one set in VTY config: */
100 if (mgcp_conn_rtp_is_osmux(conn)) {
101 bind_addr = conn->conn->endp->trunk->cfg->osmux_addr;
102 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
103 "using configured osmux bind ip as local bind ip %s\n",
104 bind_addr);
105 osmo_strlcpy(addr, bind_addr, INET6_ADDRSTRLEN);
106 return;
107 }
108
Philipp Maier1cb1e382017-11-02 17:16:04 +0100109 /* Try probing the local IP-Address */
Ericfbf78d12021-08-23 22:31:39 +0200110 if (endp->trunk->cfg->net_ports.bind_addr_probe && rem_addr_set) {
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200111 rc = osmo_sock_local_ip(addr, osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Philipp Maier1cb1e382017-11-02 17:16:04 +0100112 if (rc < 0)
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200113 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
114 "local interface auto detection failed, using configured addresses...\n");
Philipp Maier1cb1e382017-11-02 17:16:04 +0100115 else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200116 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
117 "selected local rtp bind ip %s by probing using remote ip %s\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200118 addr, osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Philipp Maier1cb1e382017-11-02 17:16:04 +0100119 return;
120 }
121 }
122
Pau Espin Pedrol479cf762022-09-07 19:42:54 +0200123 /* Select from preconfigured IP-Addresses. */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200124 if (rem_addr_set) {
Philipp Maier1cb1e382017-11-02 17:16:04 +0100125 /* Check there is a bind IP for the RTP traffic configured,
126 * if so, use that IP-Address */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200127 bind_addr = conn->end.addr.u.sa.sa_family == AF_INET6 ?
Ericfbf78d12021-08-23 22:31:39 +0200128 endp->trunk->cfg->net_ports.bind_addr_v6 :
129 endp->trunk->cfg->net_ports.bind_addr_v4;
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200130 } else {
131 /* Choose any of the bind addresses, preferring v6 over v4 */
Ericfbf78d12021-08-23 22:31:39 +0200132 bind_addr = endp->trunk->cfg->net_ports.bind_addr_v6;
Eric2764bdb2021-08-23 22:11:47 +0200133 if (!strlen(bind_addr))
Ericfbf78d12021-08-23 22:31:39 +0200134 bind_addr = endp->trunk->cfg->net_ports.bind_addr_v4;
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200135 }
Eric2764bdb2021-08-23 22:11:47 +0200136 if (strlen(bind_addr)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200137 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
138 "using configured rtp bind ip as local bind ip %s\n",
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200139 bind_addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100140 } else {
141 /* No specific bind IP is configured for the RTP traffic, so
142 * assume the IP where we listen for incoming MGCP messages
143 * as bind IP */
Ericfbf78d12021-08-23 22:31:39 +0200144 bind_addr = endp->trunk->cfg->source_addr;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200145 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200146 "using mgcp bind ip as local rtp bind ip: %s\n", bind_addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100147 }
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200148 osmo_strlcpy(addr, bind_addr, INET6_ADDRSTRLEN);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100149}
150
Philipp Maier87bd9be2017-08-22 16:35:41 +0200151/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200152 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +0200153 * 1/codec_rate seconds. */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100154uint32_t mgcp_get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200155{
156 struct timespec tp;
157 uint64_t ret;
158
Philipp Maier87bd9be2017-08-22 16:35:41 +0200159 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200160 return 0;
161
162 memset(&tp, 0, sizeof(tp));
163 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maierc3413882017-10-27 12:26:54 +0200164 LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200165
166 /* convert it to 1/unit seconds */
167 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200168 ret *= codec_rate;
169 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200170
171 return ret;
172}
173
Philipp Maier87bd9be2017-08-22 16:35:41 +0200174/* Compute timestamp alignment error */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200175static int32_t ts_alignment_error(const struct mgcp_rtp_stream_state *sstate,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200176 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200177{
178 int32_t timestamp_delta;
179
180 if (ptime == 0)
181 return 0;
182
183 /* Align according to: T - Tlast = k * Tptime */
184 timestamp_delta = timestamp - sstate->last_timestamp;
185
186 return timestamp_delta % ptime;
187}
188
Philipp Maier87bd9be2017-08-22 16:35:41 +0200189/* Check timestamp and sequence number for plausibility */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200190static int check_rtp_timestamp(const struct mgcp_endpoint *endp,
191 const struct mgcp_rtp_state *state,
192 const struct mgcp_rtp_stream_state *sstate,
193 const struct mgcp_rtp_end *rtp_end,
194 const struct osmo_sockaddr *addr,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200195 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200196 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200197{
198 int32_t tsdelta;
199 int32_t timestamp_error;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200200 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200201
202 /* Not fully intialized, skip */
203 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
204 return 0;
205
206 if (seq == sstate->last_seq) {
207 if (timestamp != sstate->last_timestamp) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200208 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200209 LOGPENDP(endp, DRTP, LOGL_ERROR,
210 "The %s timestamp delta is != 0 but the sequence "
211 "number %d is the same, "
212 "TS offset: %d, SeqNo offset: %d "
213 "on SSRC: %u timestamp: %u "
214 "from %s:%d\n",
215 text, seq,
216 state->patch.timestamp_offset, state->patch.seq_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200217 sstate->ssrc, timestamp,
218 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
219 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200220 }
221 return 0;
222 }
223
224 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200225 (int32_t)(timestamp - sstate->last_timestamp) /
226 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200227
228 if (tsdelta == 0) {
229 /* Don't update *tsdelta_out */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200230 LOGPENDP(endp, DRTP, LOGL_NOTICE,
231 "The %s timestamp delta is %d "
232 "on SSRC: %u timestamp: %u "
233 "from %s:%d\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200234 text, tsdelta, sstate->ssrc, timestamp,
235 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
236 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200237
238 return 0;
239 }
240
241 if (sstate->last_tsdelta != tsdelta) {
242 if (sstate->last_tsdelta) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200243 LOGPENDP(endp, DRTP, LOGL_INFO,
244 "The %s timestamp delta changes from %d to %d "
245 "on SSRC: %u timestamp: %u from %s:%d\n",
246 text, sstate->last_tsdelta, tsdelta,
247 sstate->ssrc, timestamp,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200248 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
249 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200250 }
251 }
252
253 if (tsdelta_out)
254 *tsdelta_out = tsdelta;
255
256 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200257 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200258
259 if (timestamp_error) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200260 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200261 LOGPENDP(endp, DRTP, LOGL_NOTICE,
262 "The %s timestamp has an alignment error of %d "
263 "on SSRC: %u "
264 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
265 "from %s:%d. ptime: %d\n",
266 text, timestamp_error,
267 sstate->ssrc,
268 (int16_t)(seq - sstate->last_seq),
269 (int32_t)(timestamp - sstate->last_timestamp),
270 tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200271 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
272 osmo_sockaddr_port(&addr->u.sa),
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200273 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200274 }
275 return 1;
276}
277
278/* Set the timestamp offset according to the packet duration. */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200279static int adjust_rtp_timestamp_offset(const struct mgcp_endpoint *endp,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200280 struct mgcp_rtp_state *state,
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200281 const struct mgcp_rtp_end *rtp_end,
282 const struct osmo_sockaddr *addr,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200283 int16_t delta_seq, uint32_t in_timestamp,
284 bool marker_bit)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200285{
286 int32_t tsdelta = state->packet_duration;
287 int timestamp_offset;
288 uint32_t out_timestamp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200289 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200290
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200291 if (marker_bit) {
292 /* If RTP pkt contains marker bit, the timestamps are not longer
293 * in sync, so we can erase timestamp offset patching. */
294 state->patch.timestamp_offset = 0;
295 return 0;
296 }
297
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200298 if (tsdelta == 0) {
299 tsdelta = state->out_stream.last_tsdelta;
300 if (tsdelta != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200301 LOGPENDP(endp, DRTP, LOGL_NOTICE,
302 "A fixed packet duration is not available, "
303 "using last output timestamp delta instead: %d "
304 "from %s:%d\n", tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200305 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
306 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200307 } else {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200308 tsdelta = rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200309 LOGPENDP(endp, DRTP, LOGL_NOTICE,
310 "Fixed packet duration and last timestamp delta "
311 "are not available, "
312 "using fixed 20ms instead: %d "
313 "from %s:%d\n", tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200314 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
315 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200316 }
317 }
318
319 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
320 timestamp_offset = out_timestamp - in_timestamp;
321
Harald Welte33381352017-12-25 09:44:26 +0100322 if (state->patch.timestamp_offset != timestamp_offset) {
323 state->patch.timestamp_offset = timestamp_offset;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200324 LOGPENDP(endp, DRTP, LOGL_NOTICE,
325 "Timestamp offset change on SSRC: %u "
326 "SeqNo delta: %d, TS offset: %d, "
327 "from %s:%d\n", state->in_stream.ssrc,
328 delta_seq, state->patch.timestamp_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200329 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
330 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200331 }
332
333 return timestamp_offset;
334}
335
336/* Set the timestamp offset according to the packet duration. */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200337static int align_rtp_timestamp_offset(const struct mgcp_endpoint *endp,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200338 struct mgcp_rtp_state *state,
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200339 const struct mgcp_rtp_end *rtp_end,
340 const struct osmo_sockaddr *addr,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200341 uint32_t timestamp, bool marker_bit)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200342{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200343 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200344 int ts_error = 0;
345 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200346 int ptime = state->packet_duration;
347
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200348 if (marker_bit) {
349 /* If RTP pkt contains marker bit, the timestamps are not longer
350 * in sync, so no alignment is needed. */
351 return 0;
352 }
353
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200354 /* Align according to: T + Toffs - Tlast = k * Tptime */
355
Philipp Maier87bd9be2017-08-22 16:35:41 +0200356 ts_error = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100357 timestamp + state->patch.timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200358
Philipp Maier87bd9be2017-08-22 16:35:41 +0200359 /* If there is an alignment error, we have to compensate it */
360 if (ts_error) {
Harald Welte33381352017-12-25 09:44:26 +0100361 state->patch.timestamp_offset += ptime - ts_error;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200362 LOGPENDP(endp, DRTP, LOGL_NOTICE,
363 "Corrected timestamp alignment error of %d on SSRC: %u "
364 "new TS offset: %d, "
365 "from %s:%d\n",
366 ts_error, state->in_stream.ssrc,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200367 state->patch.timestamp_offset,
368 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
369 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200370 }
371
Philipp Maier87bd9be2017-08-22 16:35:41 +0200372 /* Check we really managed to compensate the timestamp
373 * offset. There should not be any remaining error, failing
Harald Welte1d1b98f2017-12-25 10:03:40 +0100374 * here would point to a serous problem with the alignment
375 * error computation function */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200376 ts_check = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100377 timestamp + state->patch.timestamp_offset);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200378 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200379
Philipp Maier87bd9be2017-08-22 16:35:41 +0200380 /* Return alignment error before compensation */
381 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200382}
383
Philipp Maier87bd9be2017-08-22 16:35:41 +0200384/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200385 * \param[in] associated endpoint.
386 * \param[in] destination RTP end.
387 * \param[in,out] pointer to buffer with voice data.
388 * \param[in] voice data length.
389 * \param[in] maximum size of caller provided voice data buffer.
390 * \returns ignores input parameters, return always 0. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200391int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
392 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200393 char *data, int *len, int buf_size)
394{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200395 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200396 return 0;
397}
398
Philipp Maier87bd9be2017-08-22 16:35:41 +0200399/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200400 * \param[in] associated endpoint.
401 * \param[in] destination RTP connnection.
402 * \param[in] source RTP connection.
403 * \returns ignores input parameters, return always 0. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200404int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
Philipp Maieracc10352018-07-19 18:07:57 +0200405 struct mgcp_conn_rtp *conn_dst,
406 struct mgcp_conn_rtp *conn_src)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200407{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200408 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200409 return 0;
410}
411
412void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
Philipp Maier58128252019-03-06 11:28:18 +0100413 const struct mgcp_rtp_codec **codec,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200414 const char **fmtp_extra,
415 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200416{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200417 LOGPENDP(endp, DRTP, LOGL_DEBUG, "conn:%s using format defaults\n",
418 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200419
Philipp Maier58128252019-03-06 11:28:18 +0100420 *codec = conn->end.codec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200421 *fmtp_extra = conn->end.fmtp_extra;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200422}
423
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200424void mgcp_rtp_annex_count(const struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200425 struct mgcp_rtp_state *state, const uint16_t seq,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200426 const int32_t transit, const uint32_t ssrc,
427 const bool marker_bit)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200428{
429 int32_t d;
430
431 /* initialize or re-initialize */
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200432 if (!state->stats.initialized || state->stats.ssrc != ssrc || marker_bit) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100433 state->stats.initialized = 1;
434 state->stats.base_seq = seq;
435 state->stats.max_seq = seq - 1;
436 state->stats.ssrc = ssrc;
437 state->stats.jitter = 0;
438 state->stats.transit = transit;
439 state->stats.cycles = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200440 } else {
441 uint16_t udelta;
442
Philipp Maier87bd9be2017-08-22 16:35:41 +0200443 /* The below takes the shape of the validation of
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200444 * Appendix A. Check if there is something weird with
445 * the sequence number, otherwise check for a wrap
446 * around in the sequence number.
447 * It can't wrap during the initialization so let's
448 * skip it here. The Appendix A probably doesn't have
Philipp Maier87bd9be2017-08-22 16:35:41 +0200449 * this issue because of the probation. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100450 udelta = seq - state->stats.max_seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200451 if (udelta < RTP_MAX_DROPOUT) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100452 if (seq < state->stats.max_seq)
453 state->stats.cycles += RTP_SEQ_MOD;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200454 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200455 LOGPENDP(endp, DRTP, LOGL_NOTICE,
456 "RTP seqno made a very large jump on delta: %u\n",
457 udelta);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200458 }
459 }
460
Philipp Maier87bd9be2017-08-22 16:35:41 +0200461 /* Calculate the jitter between the two packages. The TS should be
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200462 * taken closer to the read function. This was taken from the
463 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
Philipp Maier87bd9be2017-08-22 16:35:41 +0200464 * resolution. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100465 d = transit - state->stats.transit;
466 state->stats.transit = transit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200467 if (d < 0)
468 d = -d;
Harald Welte49e3d5a2017-12-25 09:47:57 +0100469 state->stats.jitter += d - ((state->stats.jitter + 8) >> 4);
470 state->stats.max_seq = seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200471}
472
Philipp Maier6931f9a2018-07-26 09:29:31 +0200473/* There may be different payload type numbers negotiated for two connections.
474 * Patch the payload type of an RTP packet so that it uses the payload type
475 * that is valid for the destination connection (conn_dst) */
476static int mgcp_patch_pt(struct mgcp_conn_rtp *conn_src,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200477 struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200478{
479 struct rtp_hdr *rtp_hdr;
480 uint8_t pt_in;
481 int pt_out;
482
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200483 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
484 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
485 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200486 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200487 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200488
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200489 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier6931f9a2018-07-26 09:29:31 +0200490
491 pt_in = rtp_hdr->payload_type;
492 pt_out = mgcp_codec_pt_translate(conn_src, conn_dst, pt_in);
493 if (pt_out < 0)
494 return -EINVAL;
495
496 rtp_hdr->payload_type = (uint8_t) pt_out;
497 return 0;
498}
499
Philipp Maier87bd9be2017-08-22 16:35:41 +0200500/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200501 * some of the supported endpoints (e.g. the nanoBTS) can only handle
502 * one source and this code will patch RTP header to appear as if there
503 * is only one source.
504 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200505 * we receive will be seen as a switch in streams. */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200506void mgcp_patch_and_count(const struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200507 struct mgcp_rtp_state *state,
508 struct mgcp_rtp_end *rtp_end,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200509 struct osmo_sockaddr *addr, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200510{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200511 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200512 uint32_t arrival_time;
513 int32_t transit;
514 uint16_t seq;
515 uint32_t timestamp, ssrc;
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200516 bool marker_bit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200517 struct rtp_hdr *rtp_hdr;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200518 int payload = rtp_end->codec->payload_type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200519 unsigned int len = msgb_length(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200520
521 if (len < sizeof(*rtp_hdr))
522 return;
523
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200524 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200525 seq = ntohs(rtp_hdr->sequence);
526 timestamp = ntohl(rtp_hdr->timestamp);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100527 arrival_time = mgcp_get_current_ts(rtp_end->codec->rate);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200528 ssrc = ntohl(rtp_hdr->ssrc);
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200529 marker_bit = !!rtp_hdr->marker;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200530 transit = arrival_time - timestamp;
531
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200532 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc, marker_bit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200533
534 if (!state->initialized) {
535 state->initialized = 1;
536 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100537 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200538 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200539 state->packet_duration =
540 mgcp_rtp_packet_duration(endp, rtp_end);
Philipp Maier0ec1d4e2018-05-16 11:09:42 +0200541 state->out_stream.last_seq = seq - 1;
542 state->out_stream.ssrc = state->patch.orig_ssrc = ssrc;
543 state->out_stream.last_tsdelta = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200544 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200545 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200546 LOGPENDP(endp, DRTP, LOGL_INFO,
547 "initializing stream, SSRC: %u timestamp: %u "
548 "pkt-duration: %d, from %s:%d\n",
549 state->in_stream.ssrc,
550 state->patch.seq_offset, state->packet_duration,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200551 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
552 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200553 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200554 state->packet_duration =
Philipp Maierbc0346e2018-06-07 09:52:16 +0200555 rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200556 LOGPENDP(endp, DRTP, LOGL_NOTICE,
557 "fixed packet duration is not available, "
558 "using fixed 20ms instead: %d from %s:%d\n",
559 state->packet_duration,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200560 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
561 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200562 }
563 } else if (state->in_stream.ssrc != ssrc) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200564 LOGPENDP(endp, DRTP, LOGL_NOTICE,
565 "SSRC changed: %u -> %u "
566 "from %s:%d\n",
567 state->in_stream.ssrc, rtp_hdr->ssrc,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200568 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
569 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200570
571 state->in_stream.ssrc = ssrc;
572 if (rtp_end->force_constant_ssrc) {
573 int16_t delta_seq;
574
575 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100576 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200577 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200578
579 /* Estimate number of packets that would have been sent */
580 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200581 (arrival_time - state->in_stream.last_arrival_time
582 + state->packet_duration / 2) /
583 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200584
585 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200586 delta_seq, timestamp, marker_bit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200587
Pau Espin Pedrol4c77e9b2021-07-07 12:12:58 +0200588 state->patch.patch_ssrc = true;
Harald Welte33381352017-12-25 09:44:26 +0100589 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200590 if (rtp_end->force_constant_ssrc != -1)
591 rtp_end->force_constant_ssrc -= 1;
592
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200593 LOGPENDP(endp, DRTP, LOGL_NOTICE,
594 "SSRC patching enabled, SSRC: %u "
595 "SeqNo offset: %d, TS offset: %d "
596 "from %s:%d\n", state->in_stream.ssrc,
597 state->patch.seq_offset, state->patch.timestamp_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200598 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
599 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200600 }
601
602 state->in_stream.last_tsdelta = 0;
603 } else {
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200604 if (!marker_bit) {
605 /* Compute current per-packet timestamp delta */
606 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
607 addr, seq, timestamp, "input",
608 &state->in_stream.last_tsdelta);
609 } else {
610 state->in_stream.last_tsdelta = 0;
611 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200612
Harald Welte33381352017-12-25 09:44:26 +0100613 if (state->patch.patch_ssrc)
614 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200615 }
616
617 /* Save before patching */
618 state->in_stream.last_timestamp = timestamp;
619 state->in_stream.last_seq = seq;
620 state->in_stream.last_arrival_time = arrival_time;
621
622 if (rtp_end->force_aligned_timing &&
623 state->out_stream.ssrc == ssrc && state->packet_duration)
624 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200625 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200626 timestamp, marker_bit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200627
628 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100629 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200630 rtp_hdr->ssrc = htonl(ssrc);
631
632 /* Apply the offset and store it back to the packet.
633 * This won't change anything if the offset is 0, so the conditional is
634 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100635 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200636 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100637 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200638 rtp_hdr->timestamp = htonl(timestamp);
639
640 /* Check again, whether the timestamps are still valid */
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200641 if (!marker_bit) {
642 if (state->out_stream.ssrc == ssrc)
643 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
644 addr, seq, timestamp, "output",
645 &state->out_stream.last_tsdelta);
646 } else {
647 state->out_stream.last_tsdelta = 0;
648 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200649
650 /* Save output values */
651 state->out_stream.last_seq = seq;
652 state->out_stream.last_timestamp = timestamp;
653 state->out_stream.ssrc = ssrc;
654
655 if (payload < 0)
656 return;
657
658#if 0
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200659 LOGPENDP(endp, DRTP, LOGL_DEBUG, "payload hdr payload %u -> endp payload %u\n",
660 rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200661 rtp_hdr->payload_type = payload;
662#endif
663}
664
Philipp Maier9fc8a022019-02-20 12:26:52 +0100665/* There are different dialects used to format and transfer voice data. When
666 * the receiving end expects GSM-HR data to be formated after RFC 5993, this
667 * function is used to convert between RFC 5993 and TS 101318, which we normally
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200668 * use.
669 * Return 0 on sucess, negative on errors like invalid data length. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200670static int rfc5993_hr_convert(struct mgcp_endpoint *endp, struct msgb *msg)
Philipp Maier9fc8a022019-02-20 12:26:52 +0100671{
Philipp Maier9fc8a022019-02-20 12:26:52 +0100672 struct rtp_hdr *rtp_hdr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200673 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200674 LOGPENDP(endp, DRTP, LOGL_ERROR, "AMR RTP packet too short (%d < %zu)\n",
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200675 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200676 return -EINVAL;
677 }
678
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200679 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100680
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200681 if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr)) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100682 /* TS 101318 encoding => RFC 5993 encoding */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200683 msgb_put(msg, 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100684 memmove(rtp_hdr->data + 1, rtp_hdr->data, GSM_HR_BYTES);
685 rtp_hdr->data[0] = 0x00;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200686 } else if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr) + 1) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100687 /* RFC 5993 encoding => TS 101318 encoding */
688 memmove(rtp_hdr->data, rtp_hdr->data + 1, GSM_HR_BYTES);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200689 msgb_trim(msg, msgb_length(msg) - 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100690 } else {
691 /* It is possible that multiple payloads occur in one RTP
692 * packet. This is not supported yet. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200693 LOGPENDP(endp, DRTP, LOGL_ERROR,
694 "cannot figure out how to convert RTP packet\n");
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200695 return -ENOTSUP;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100696 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200697 return 0;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100698}
699
Philipp Maier228e5912019-03-05 13:56:59 +0100700/* For AMR RTP two framing modes are defined RFC3267. There is a bandwith
701 * efficient encoding scheme where all fields are packed together one after
702 * another and an octet aligned mode where all fields are aligned to octet
703 * boundaries. This function is used to convert between the two modes */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200704static int amr_oa_bwe_convert(struct mgcp_endpoint *endp, struct msgb *msg,
Philipp Maier228e5912019-03-05 13:56:59 +0100705 bool target_is_oa)
706{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200707 /* NOTE: the msgb has an allocated length of RTP_BUF_SIZE, so there is
Philipp Maier228e5912019-03-05 13:56:59 +0100708 * plenty of space available to store the slightly larger, converted
709 * data */
Philipp Maier228e5912019-03-05 13:56:59 +0100710 struct rtp_hdr *rtp_hdr;
711 unsigned int payload_len;
712 int rc;
713
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200714 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
715 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 +0200716 return -EINVAL;
717 }
718
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200719 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier228e5912019-03-05 13:56:59 +0100720
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200721 payload_len = msgb_length(msg) - sizeof(struct rtp_hdr);
Philipp Maier228e5912019-03-05 13:56:59 +0100722
723 if (osmo_amr_is_oa(rtp_hdr->data, payload_len)) {
724 if (!target_is_oa)
725 /* Input data is oa an target format is bwe
726 * ==> convert */
727 rc = osmo_amr_oa_to_bwe(rtp_hdr->data, payload_len);
728 else
729 /* Input data is already bew, but we accept it anyway
730 * ==> no conversion needed */
731 rc = payload_len;
732 } else {
733 if (target_is_oa)
734 /* Input data is bwe an target format is oa
735 * ==> convert */
736 rc = osmo_amr_bwe_to_oa(rtp_hdr->data, payload_len,
737 RTP_BUF_SIZE);
738 else
739 /* Input data is already oa, but we accept it anyway
740 * ==> no conversion needed */
741 rc = payload_len;
742 }
743 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200744 LOGPENDP(endp, DRTP, LOGL_ERROR,
745 "AMR RTP packet conversion failed\n");
Philipp Maier228e5912019-03-05 13:56:59 +0100746 return -EINVAL;
747 }
748
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200749 return msgb_trim(msg, rc + sizeof(struct rtp_hdr));
Philipp Maier228e5912019-03-05 13:56:59 +0100750}
751
752/* Check if a conversion between octet-aligned and bandwith-efficient mode is
753 * indicated. */
754static bool amr_oa_bwe_convert_indicated(struct mgcp_rtp_codec *codec)
755{
756 if (codec->param_present == false)
757 return false;
758 if (!codec->param.amr_octet_aligned_present)
759 return false;
760 if (strcmp(codec->subtype_name, "AMR") != 0)
761 return false;
762 return true;
763}
764
765
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200766/* Return whether an RTP packet with AMR payload is in octet-aligned mode.
767 * Return 0 if in bandwidth-efficient mode, 1 for octet-aligned mode, and negative if the RTP data is invalid. */
768static int amr_oa_check(char *data, int len)
Philipp Maier228e5912019-03-05 13:56:59 +0100769{
770 struct rtp_hdr *rtp_hdr;
771 unsigned int payload_len;
772
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200773 if (len < sizeof(struct rtp_hdr))
774 return -EINVAL;
775
Philipp Maier228e5912019-03-05 13:56:59 +0100776 rtp_hdr = (struct rtp_hdr *)data;
777
778 payload_len = len - sizeof(struct rtp_hdr);
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200779 if (payload_len < sizeof(struct amr_hdr))
780 return -EINVAL;
Philipp Maier228e5912019-03-05 13:56:59 +0100781
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200782 return osmo_amr_is_oa(rtp_hdr->data, payload_len) ? 1 : 0;
Philipp Maier228e5912019-03-05 13:56:59 +0100783}
784
Philipp Maier87bd9be2017-08-22 16:35:41 +0200785/* Forward data to a debug tap. This is debug function that is intended for
786 * debugging the voice traffic with tools like gstreamer */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100787void forward_data_tap(int fd, struct mgcp_rtp_tap *tap, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200788{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100789 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200790
Philipp Maiere6f172d2017-11-07 12:00:01 +0100791 if (!tap->enabled)
792 return;
793
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200794 rc = sendto(fd, msgb_data(msg), msgb_length(msg), 0, (struct sockaddr *)&tap->forward,
Philipp Maiere6f172d2017-11-07 12:00:01 +0100795 sizeof(tap->forward));
796
797 if (rc < 0)
798 LOGP(DRTP, LOGL_ERROR,
799 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200800}
801
Philipp Maier889fe7f2020-07-06 17:44:12 +0200802/* Generate an RTP header if it is missing */
803static void gen_rtp_header(struct msgb *msg, struct mgcp_rtp_end *rtp_end,
804 struct mgcp_rtp_state *state)
805{
806 struct rtp_hdr *hdr = (struct rtp_hdr *)msgb_data(msg);
807
808 if (hdr->version > 0)
809 return;
810
811 hdr->version = 2;
812 hdr->payload_type = rtp_end->codec->payload_type;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100813 hdr->timestamp = osmo_htonl(mgcp_get_current_ts(rtp_end->codec->rate));
Philipp Maier889fe7f2020-07-06 17:44:12 +0200814 hdr->sequence = osmo_htons(state->alt_rtp_tx_sequence);
815 hdr->ssrc = state->alt_rtp_tx_ssrc;
816}
817
Philipp Maier87bd9be2017-08-22 16:35:41 +0200818/* Check if the origin (addr) matches the address/port data of the RTP
819 * connections. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200820static int check_rtp_origin(struct mgcp_conn_rtp *conn, struct osmo_sockaddr *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200821{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200822 char ipbuf[INET6_ADDRSTRLEN];
823
Pau Espin Pedrol051eb4d2022-10-04 12:50:28 +0200824 if (osmo_sockaddr_is_any(&conn->end.addr) != 0) {
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200825 switch (conn->conn->mode) {
826 case MGCP_CONN_LOOPBACK:
827 /* HACK: for IuUP, we want to reply with an IuUP Initialization ACK upon the first RTP
828 * message received. We currently hackishly accomplish that by putting the endpoint in
829 * loopback mode and patching over the looped back RTP message to make it look like an
830 * ack. We don't know the femto cell's IP address and port until the RAB Assignment
831 * Response is received, but the nano3G expects an IuUP Initialization Ack before it even
832 * sends the RAB Assignment Response. Hence, if the remote address is 0.0.0.0 and the
833 * MGCP port is in loopback mode, allow looping back the packet to any source. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200834 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
835 "In loopback mode and remote address not set:"
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200836 " allowing data from address: %s\n",
837 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200838 return 0;
839
840 default:
841 /* Receiving early media before the endpoint is configured. Instead of logging
842 * this as an error that occurs on every call, keep it more low profile to not
843 * confuse humans with expected errors. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200844 LOGPCONN(conn->conn, DRTP, LOGL_INFO,
845 "Rx RTP from %s, but remote address not set:"
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200846 " dropping early media\n",
847 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200848 return -1;
849 }
Neels Hofmeyr0063ca22018-07-23 18:12:16 +0200850 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200851
Philipp Maier87bd9be2017-08-22 16:35:41 +0200852 /* Note: Check if the inbound RTP data comes from the same host to
853 * which we send our outgoing RTP traffic. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200854 if (conn->end.addr.u.sa.sa_family != addr->u.sa.sa_family ||
855 (conn->end.addr.u.sa.sa_family == AF_INET &&
856 conn->end.addr.u.sin.sin_addr.s_addr != addr->u.sin.sin_addr.s_addr) ||
857 (conn->end.addr.u.sa.sa_family == AF_INET6 &&
858 memcmp(&conn->end.addr.u.sin6.sin6_addr, &addr->u.sin6.sin6_addr,
859 sizeof(struct in6_addr)))) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200860 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200861 "data from wrong address: %s, ",
862 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Philipp Maierc3413882017-10-27 12:26:54 +0200863 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200864 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200865 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200866 return -1;
867 }
868
Philipp Maier87bd9be2017-08-22 16:35:41 +0200869 /* Note: Usually the remote remote port of the data we receive will be
870 * the same as the remote port where we transmit outgoing RTP traffic
871 * to (set by MDCX). We use this to check the origin of the data for
872 * plausibility. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200873 if (ntohs(conn->end.rtp_port) != osmo_sockaddr_port(&addr->u.sa) &&
874 ntohs(conn->end.rtcp_port) != osmo_sockaddr_port(&addr->u.sa)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200875 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200876 "data from wrong source port: %d, ",
877 osmo_sockaddr_port(&addr->u.sa));
Philipp Maierc3413882017-10-27 12:26:54 +0200878 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200879 "expected: %d for RTP or %d for RTCP\n",
880 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200881 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200882 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200883 }
884
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200885 return 0;
886}
887
Philipp Maier87bd9be2017-08-22 16:35:41 +0200888/* Check the if the destination address configuration of an RTP connection
889 * makes sense */
890static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200891{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200892 char ipbuf[INET6_ADDRSTRLEN];
Pau Espin Pedrol051eb4d2022-10-04 12:50:28 +0200893 bool ip_is_any = osmo_sockaddr_is_any(&conn->end.addr) != 0;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200894
Philipp Maiere6df0e42018-05-29 14:03:06 +0200895 /* Note: it is legal to create a connection but never setting a port
896 * and IP-address for outgoing data. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200897 if (ip_is_any && conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200898 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
Neels Hofmeyra4b677c2021-07-07 23:38:23 +0200899 "destination IP-address and rtp port is not (yet) known (%s:%u)\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200900 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port);
Philipp Maiere6df0e42018-05-29 14:03:06 +0200901 return -1;
902 }
903
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200904 if (ip_is_any) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200905 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
906 "destination IP-address is invalid (%s:%u)\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200907 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200908 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200909 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200910
911 if (conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200912 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
913 "destination rtp port is invalid (%s:%u)\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200914 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200915 return -1;
916 }
917
918 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200919}
920
Philipp Maier4dba7692018-08-03 12:20:52 +0200921/* Do some basic checks to make sure that the RTCP packets we are going to
922 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200923static int check_rtcp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +0200924{
925 struct rtcp_hdr *hdr;
926 unsigned int len;
927 uint8_t type;
928
929 /* RTPC packets that are just a header without data do not make
930 * any sense. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200931 if (msgb_length(msg) < sizeof(struct rtcp_hdr)) {
932 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP packet too short (%u < %zu)\n",
933 msgb_length(msg), sizeof(struct rtcp_hdr));
Philipp Maier4dba7692018-08-03 12:20:52 +0200934 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200935 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200936
937 /* Make sure that the length of the received packet does not exceed
938 * the available buffer size */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200939 hdr = (struct rtcp_hdr *)msgb_data(msg);
Philipp Maier4dba7692018-08-03 12:20:52 +0200940 len = (osmo_ntohs(hdr->length) + 1) * 4;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200941 if (len > msgb_length(msg)) {
942 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header length exceeds packet size (%u > %u)\n",
943 len, msgb_length(msg));
Philipp Maier4dba7692018-08-03 12:20:52 +0200944 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200945 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200946
947 /* Make sure we accept only packets that have a proper packet type set
948 * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
949 type = hdr->type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200950 if ((type < 192 || type > 195) && (type < 200 || type > 213)) {
951 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header: invalid type: %u\n", type);
Philipp Maier4dba7692018-08-03 12:20:52 +0200952 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200953 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200954
955 return 0;
956}
957
958/* Do some basic checks to make sure that the RTP packets we are going to
959 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200960static int check_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +0200961{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200962 size_t min_size = sizeof(struct rtp_hdr);
963 if (msgb_length(msg) < min_size) {
964 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
965 msgb_length(msg), min_size);
966 return -1;
967 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200968
969 /* FIXME: Add more checks, the reason why we do not check more than
970 * the length is because we currently handle IUUP packets as RTP
971 * packets, so they must pass this check, if we weould be more
972 * strict here, we would possibly break 3G. (see also FIXME note
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100973 * below.*/
Philipp Maier4dba7692018-08-03 12:20:52 +0200974
975 return 0;
976}
977
Philipp Maier87bd9be2017-08-22 16:35:41 +0200978/* Send RTP data. Possible options are standard RTP packet
979 * transmission or trsmission via an osmux connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200980static int mgcp_send_rtp(struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200981{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200982 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
983 enum rtp_proto proto = mc->proto;
984 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200985 struct mgcp_endpoint *endp = conn_src->conn->endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200986
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200987 LOGPENDP(endp, DRTP, LOGL_DEBUG, "destin conn:%s\n",
988 mgcp_conn_dump(conn_dst->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200989
990 /* Before we try to deliver the packet, we check if the destination
991 * port and IP-Address make sense at all. If not, we will be unable
992 * to deliver the packet. */
993 if (check_rtp_destin(conn_dst) != 0)
994 return -1;
995
996 /* Depending on the RTP connection type, deliver the RTP packet to the
997 * destination connection. */
998 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200999 case MGCP_RTP_DEFAULT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001000 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1001 "endpoint type is MGCP_RTP_DEFAULT, "
1002 "using mgcp_send() to forward data directly\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001003 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001004 mc->from_addr, msg, conn_src, conn_dst);
Pau Espin Pedrol9d939b62022-10-03 16:59:20 +02001005 case MGCP_RTP_OSMUX:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001006 LOGPENDP(endp, DRTP, LOGL_DEBUG,
Pau Espin Pedrol9d939b62022-10-03 16:59:20 +02001007 "endpoint type is MGCP_RTP_OSMUX, "
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001008 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n");
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001009 return osmux_xfrm_to_osmux((char*)msgb_data(msg), msgb_length(msg), conn_dst);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001010 case MGCP_RTP_IUUP:
1011 if (proto == MGCP_PROTO_RTP) {
1012 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1013 "endpoint type is MGCP_RTP_IUUP, "
1014 "using mgcp_conn_iuup_send_rtp() to forward data over IuUP\n");
1015 return mgcp_conn_iuup_send_rtp(conn_src, conn_dst, msg);
1016 }
1017 /* RTCP: we forward as usual for regular RTP connection */
1018 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1019 "endpoint type is MGCP_RTP_IUUP and proto!=MGCP_PROTO_RTP, "
1020 "using mgcp_send() to forward data directly\n");
1021 return mgcp_send(endp, false,
1022 mc->from_addr, msg, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001023 }
1024
Philipp Maier87bd9be2017-08-22 16:35:41 +02001025 /* If the data has not been handled/forwarded until here, it will
1026 * be discarded, this should not happen, normally the MGCP type
1027 * should be properly set */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001028 LOGPENDP(endp, DRTP, LOGL_ERROR, "bad MGCP type -- data discarded!\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001029
1030 return -1;
1031}
1032
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001033/*! send udp packet.
1034 * \param[in] fd associated file descriptor.
1035 * \param[in] addr destination ip-address.
1036 * \param[in] port destination UDP port (network byte order).
1037 * \param[in] buf buffer that holds the data to be send.
1038 * \param[in] len length of the data to be sent.
1039 * \returns bytes sent, -1 on error. */
1040int mgcp_udp_send(int fd, struct osmo_sockaddr *addr, int port, const char *buf, int len)
1041{
1042 char ipbuf[INET6_ADDRSTRLEN];
1043 size_t addr_len;
1044 bool is_ipv6 = addr->u.sa.sa_family == AF_INET6;
1045
1046 LOGP(DRTP, LOGL_DEBUG,
1047 "sending %i bytes length packet to %s:%u ...\n", len,
1048 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
1049 ntohs(port));
1050
1051 if (is_ipv6) {
1052 addr->u.sin6.sin6_port = port;
1053 addr_len = sizeof(addr->u.sin6);
1054 } else {
1055 addr->u.sin.sin_port = port;
1056 addr_len = sizeof(addr->u.sin);
1057 }
1058
1059 return sendto(fd, buf, len, 0, &addr->u.sa, addr_len);
1060}
1061
1062/*! send RTP dummy packet (to keep NAT connection open).
1063 * \param[in] endp mcgp endpoint that holds the RTP connection.
1064 * \param[in] conn associated RTP connection.
1065 * \returns bytes sent, -1 on error. */
1066int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
1067{
1068 int rc;
1069 int was_rtcp = 0;
1070
1071 OSMO_ASSERT(endp);
1072 OSMO_ASSERT(conn);
1073
1074 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG, "sending dummy packet... %s\n",
1075 mgcp_conn_dump(conn->conn));
1076
Pau Espin Pedrola24dcc62021-07-06 17:48:47 +02001077 /* Before we try to deliver the packet, we check if the destination
1078 * port and IP-Address make sense at all. If not, we will be unable
1079 * to deliver the packet. */
1080 if (check_rtp_destin(conn) != 0)
1081 goto failed;
1082
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001083 if (mgcp_conn_rtp_is_iuup(conn))
1084 rc = mgcp_conn_iuup_send_dummy(conn);
1085 else
1086 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr, conn->end.rtp_port,
1087 rtp_dummy_payload, sizeof(rtp_dummy_payload));
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001088
1089 if (rc == -1)
1090 goto failed;
1091
1092 if (endp->trunk->omit_rtcp)
1093 return rc;
1094
1095 was_rtcp = 1;
1096 rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
1097 conn->end.rtcp_port, rtp_dummy_payload, sizeof(rtp_dummy_payload));
1098
1099 if (rc >= 0)
1100 return rc;
1101
1102failed:
1103 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1104 "Failed to send dummy %s packet.\n",
1105 was_rtcp ? "RTCP" : "RTP");
1106
1107 return -1;
1108}
1109
1110/*! Send RTP/RTCP data to a specified destination connection.
1111 * \param[in] endp associated endpoint (for configuration, logging).
1112 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP.
1113 * \param[in] spoofed source address (set to NULL to disable).
1114 * \param[in] buf buffer that contains the RTP/RTCP data.
1115 * \param[in] len length of the buffer that contains the RTP/RTCP data.
1116 * \param[in] conn_src associated source connection.
1117 * \param[in] conn_dst associated destination connection.
1118 * \returns 0 on success, -1 on ERROR. */
1119int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct osmo_sockaddr *addr,
1120 struct msgb *msg, struct mgcp_conn_rtp *conn_src,
1121 struct mgcp_conn_rtp *conn_dst)
1122{
1123 /*! When no destination connection is available (e.g. when only one
1124 * connection in loopback mode exists), then the source connection
1125 * shall be specified as destination connection */
1126
1127 struct mgcp_trunk *trunk = endp->trunk;
1128 struct mgcp_rtp_end *rtp_end;
1129 struct mgcp_rtp_state *rtp_state;
1130 char ipbuf[INET6_ADDRSTRLEN];
1131 char *dest_name;
1132 int rc;
1133 int len;
1134
1135 OSMO_ASSERT(conn_src);
1136 OSMO_ASSERT(conn_dst);
1137
1138 if (is_rtp)
1139 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTP packet...\n");
1140 else
1141 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTCP packet...\n");
1142
1143 /* FIXME: It is legal that the payload type on the egress connection is
1144 * different from the payload type that has been negotiated on the
1145 * ingress connection. Essentially the codecs are the same so we can
1146 * match them and patch the payload type. However, if we can not find
1147 * the codec pendant (everything ist equal except the PT), we are of
1148 * course unable to patch the payload type. A situation like this
1149 * should not occur if transcoding is consequently avoided. Until
1150 * we have transcoding support in osmo-mgw we can not resolve this. */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001151 if (is_rtp && conn_dst->type != MGCP_RTP_IUUP) {
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001152 rc = mgcp_patch_pt(conn_src, conn_dst, msg);
1153 if (rc < 0) {
1154 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1155 "can not patch PT because no suitable egress codec was found.\n");
1156 }
1157 }
1158
1159 /* Note: In case of loopback configuration, both, the source and the
1160 * destination will point to the same connection. */
1161 rtp_end = &conn_dst->end;
1162 rtp_state = &conn_src->state;
1163 dest_name = conn_dst->conn->name;
1164
1165 /* Ensure we have an alternative SSRC in case we need it, see also
1166 * gen_rtp_header() */
1167 if (rtp_state->alt_rtp_tx_ssrc == 0)
1168 rtp_state->alt_rtp_tx_ssrc = rand();
1169
1170 if (!rtp_end->output_enabled) {
1171 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_DROPPED_PACKETS_CTR);
1172 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1173 "output disabled, drop to %s %s "
1174 "rtp_port:%u rtcp_port:%u\n",
1175 dest_name,
1176 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
1177 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
1178 );
1179 } else if (is_rtp) {
1180 int cont;
1181 int nbytes = 0;
1182 int buflen = msgb_length(msg);
1183
1184 /* Make sure we have a valid RTP header, in cases where no RTP
1185 * header is present, we will generate one. */
1186 gen_rtp_header(msg, rtp_end, rtp_state);
1187
1188 do {
1189 /* Run transcoder */
Ericfbf78d12021-08-23 22:31:39 +02001190 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 +02001191 if (cont < 0)
1192 break;
1193
1194 if (addr)
1195 mgcp_patch_and_count(endp, rtp_state, rtp_end,
1196 addr, msg);
1197
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001198 if (mgcp_conn_rtp_is_iuup(conn_dst) || mgcp_conn_rtp_is_iuup(conn_src)) {
1199 /* the iuup code will correctly transform to the correct AMR mode */
1200 } else if (amr_oa_bwe_convert_indicated(conn_dst->end.codec)) {
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001201 rc = amr_oa_bwe_convert(endp, msg,
1202 conn_dst->end.codec->param.amr_octet_aligned);
1203 if (rc < 0) {
1204 LOGPENDP(endp, DRTP, LOGL_ERROR,
1205 "Error in AMR octet-aligned <-> bandwidth-efficient mode conversion\n");
1206 break;
1207 }
1208 } else if (rtp_end->rfc5993_hr_convert &&
1209 strcmp(conn_src->end.codec->subtype_name, "GSM-HR-08") == 0) {
1210 rc = rfc5993_hr_convert(endp, msg);
1211 if (rc < 0) {
1212 LOGPENDP(endp, DRTP, LOGL_ERROR, "Error while converting to GSM-HR-08\n");
1213 break;
1214 }
1215 }
1216
1217 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1218 "process/send to %s %s "
1219 "rtp_port:%u rtcp_port:%u\n",
1220 dest_name,
1221 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
1222 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
1223 );
1224
1225 /* Forward a copy of the RTP data to a debug ip/port */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001226 forward_data_tap(rtp_end->rtp.fd, &conn_src->tap_out,
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001227 msg);
1228
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001229 len = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr, rtp_end->rtp_port,
1230 (char *)msgb_data(msg), msgb_length(msg));
1231
1232 if (len <= 0)
1233 return len;
1234
1235 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
1236 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
1237 rtp_state->alt_rtp_tx_sequence++;
1238
1239 nbytes += len;
1240 buflen = cont;
1241 } while (buflen > 0);
1242 return nbytes;
1243 } else if (!trunk->omit_rtcp) {
1244 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1245 "send to %s %s rtp_port:%u rtcp_port:%u\n",
1246 dest_name, osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
1247 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
1248 );
1249
1250 len = mgcp_udp_send(rtp_end->rtcp.fd,
1251 &rtp_end->addr,
1252 rtp_end->rtcp_port, (char *)msgb_data(msg), msgb_length(msg));
1253
1254 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
1255 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
1256 rtp_state->alt_rtp_tx_sequence++;
1257
1258 return len;
1259 }
1260
1261 return 0;
1262}
1263
Pau Espin Pedrolde805b62022-10-03 16:08:58 +02001264/*! Dispatch incoming RTP packet to opposite RTP connection.
1265 * \param[in] msg Message buffer to bridge, coming from source connection.
1266 * msg shall contain "struct osmo_rtp_msg_ctx *" attached in
1267 * "OSMO_RTP_MSG_CTX(msg)".
1268 * \returns 0 on success, -1 on ERROR.
1269 */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001270int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001271{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001272 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1273 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1274 struct mgcp_conn *conn = conn_src->conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001275 struct mgcp_conn *conn_dst;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001276 struct osmo_sockaddr *from_addr = mc->from_addr;
Philipp Maier97a93122021-05-07 22:50:31 +02001277 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001278
1279 /*! NOTE: This callback function implements the endpoint specific
Alexander Chemeris61cf9bb2020-05-11 18:13:53 +03001280 * dispatch behaviour of an rtp bridge/proxy endpoint. It is assumed
Philipp Maier87bd9be2017-08-22 16:35:41 +02001281 * that the endpoint will hold only two connections. This premise
1282 * is used to determine the opposite connection (it is always the
1283 * connection that is not the originating connection). Once the
1284 * destination connection is known the RTP packet is sent via
1285 * the destination connection. */
1286
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001287 /* If source is IuUP, we need to handle state, forward it through specific bridge path: */
1288 if (mgcp_conn_rtp_is_iuup(conn_src) && mc->proto == MGCP_PROTO_RTP)
1289 return mgcp_conn_iuup_dispatch_rtp(msg);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001290
1291 /* Check if the connection is in loopback mode, if yes, just send the
1292 * incoming data back to the origin */
1293 if (conn->mode == MGCP_CONN_LOOPBACK) {
1294 /* When we are in loopback mode, we loop back all incoming
1295 * packets back to their origin. We will use the originating
1296 * address data from the UDP packet header to patch the
1297 * outgoing address in connection on the fly */
1298 if (conn->u.rtp.end.rtp_port == 0) {
Philipp Maier97a93122021-05-07 22:50:31 +02001299 memcpy(&conn->u.rtp.end.addr, from_addr,
1300 sizeof(conn->u.rtp.end.addr));
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001301 switch (from_addr->u.sa.sa_family) {
1302 case AF_INET:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001303 conn->u.rtp.end.rtp_port = from_addr->u.sin.sin_port;
1304 break;
1305 case AF_INET6:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001306 conn->u.rtp.end.rtp_port = from_addr->u.sin6.sin6_port;
1307 break;
1308 default:
1309 OSMO_ASSERT(false);
1310 }
Philipp Maier97a93122021-05-07 22:50:31 +02001311 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1312 "loopback mode: implicitly using source address (%s:%u) as destination address\n",
1313 osmo_sockaddr_ntop(&from_addr->u.sa, ipbuf),
1314 conn->u.rtp.end.rtp_port);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001315 }
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001316 return mgcp_send_rtp(conn_src, msg);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001317 }
1318
Philipp Maier87bd9be2017-08-22 16:35:41 +02001319 /* Find a destination connection. */
1320 /* NOTE: This code path runs every time an RTP packet is received. The
1321 * function mgcp_find_dst_conn() we use to determine the detination
1322 * connection will iterate the connection list inside the endpoint.
1323 * Since list iterations are quite costly, we will figure out the
1324 * destination only once and use the optional private data pointer of
1325 * the connection to cache the destination connection pointer. */
1326 if (!conn->priv) {
1327 conn_dst = mgcp_find_dst_conn(conn);
1328 conn->priv = conn_dst;
1329 } else {
1330 conn_dst = (struct mgcp_conn *)conn->priv;
1331 }
1332
1333 /* There is no destination conn, stop here */
1334 if (!conn_dst) {
Alexander Chemerisebb9bf32020-05-11 18:14:31 +03001335 LOGPCONN(conn, DRTP, LOGL_DEBUG,
1336 "no connection to forward an incoming RTP packet to\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001337 return -1;
1338 }
1339
1340 /* The destination conn is not an RTP connection */
1341 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001342 LOGPCONN(conn, DRTP, LOGL_ERROR,
1343 "unable to find suitable destination conn\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001344 return -1;
1345 }
1346
1347 /* Dispatch RTP packet to destination RTP connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001348 return mgcp_send_rtp(&conn_dst->u.rtp, msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001349}
1350
Philipp Maier0996a1e2020-06-10 15:27:14 +02001351/*! dispatch incoming RTP packet to E1 subslot, handle RTCP packets locally.
1352 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1353 * \param[in] addr socket address where the RTP packet has been received from.
1354 * \param[in] buf buffer that hold the RTP payload.
1355 * \param[in] buf_size size data length of buf.
1356 * \param[in] conn originating connection.
1357 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001358int mgcp_dispatch_e1_bridge_cb(struct msgb *msg)
Philipp Maier0996a1e2020-06-10 15:27:14 +02001359{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001360 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1361 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1362 struct mgcp_conn *conn = conn_src->conn;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001363 struct osmo_sockaddr *from_addr = mc->from_addr;
Philipp Maier97a93122021-05-07 22:50:31 +02001364 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001365
Philipp Maier889fe7f2020-07-06 17:44:12 +02001366 /* Check if the connection is in loopback mode, if yes, just send the
1367 * incoming data back to the origin */
1368 if (conn->mode == MGCP_CONN_LOOPBACK) {
1369 /* When we are in loopback mode, we loop back all incoming
1370 * packets back to their origin. We will use the originating
1371 * address data from the UDP packet header to patch the
1372 * outgoing address in connection on the fly */
1373 if (conn->u.rtp.end.rtp_port == 0) {
Philipp Maier97a93122021-05-07 22:50:31 +02001374 memcpy(&conn->u.rtp.end.addr, from_addr,
1375 sizeof(conn->u.rtp.end.addr));
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001376 switch (from_addr->u.sa.sa_family) {
1377 case AF_INET:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001378 conn->u.rtp.end.rtp_port = from_addr->u.sin.sin_port;
1379 break;
1380 case AF_INET6:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001381 conn->u.rtp.end.rtp_port = from_addr->u.sin6.sin6_port;
1382 break;
1383 default:
1384 OSMO_ASSERT(false);
1385 }
Philipp Maier97a93122021-05-07 22:50:31 +02001386 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1387 "loopback mode: implicitly using source address (%s:%u) as destination address\n",
1388 osmo_sockaddr_ntop(&from_addr->u.sa, ipbuf),
1389 conn->u.rtp.end.rtp_port);
Philipp Maier889fe7f2020-07-06 17:44:12 +02001390 }
1391 return mgcp_send_rtp(conn_src, msg);
1392 }
1393
1394 /* Forward to E1 */
1395 return mgcp_e1_send_rtp(conn->endp, conn->u.rtp.end.codec, msg);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001396}
1397
Philipp Maierdf5d2192018-01-24 11:39:32 +01001398/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1399 * \param[in] endp Endpoint on which the connection resides.
Philipp Maier08eb9352020-06-18 11:55:35 +02001400 * \param[in] conn Connection that is about to be removed (ignored). */
Philipp Maierdf5d2192018-01-24 11:39:32 +01001401void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1402{
1403 struct mgcp_conn *conn_cleanup;
1404
1405 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1406 * pointer to the destination connection, so that we do not have
1407 * to go through the list every time an RTP packet arrives. To prevent
1408 * a use-after-free situation we invalidate this information for all
1409 * connections present when one connection is removed from the
1410 * endpoint. */
1411 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
Pau Espin Pedrol6d0a59a2020-09-08 16:50:22 +02001412 if (conn_cleanup->priv == conn)
1413 conn_cleanup->priv = NULL;
Philipp Maierdf5d2192018-01-24 11:39:32 +01001414 }
1415}
1416
Philipp Maier0996a1e2020-06-10 15:27:14 +02001417/*! cleanup an endpoint when a connection on an E1 endpoint is removed.
1418 * \param[in] endp Endpoint on which the connection resides.
1419 * \param[in] conn Connection that is about to be removed (ignored). */
1420void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1421{
Philipp Maier889fe7f2020-07-06 17:44:12 +02001422 /* Cleanup tasks for E1 are the same as for regular endpoint. The
1423 * shut down of the E1 part is handled separately. */
1424 mgcp_cleanup_rtp_bridge_cb(endp, conn);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001425}
1426
Philipp Maier87bd9be2017-08-22 16:35:41 +02001427/* Handle incoming RTP data from NET */
1428static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1429{
1430 /* NOTE: This is a generic implementation. RTP data is received. In
1431 * case of loopback the data is just sent back to its origin. All
1432 * other cases implement endpoint specific behaviour (e.g. how is the
1433 * destination connection determined?). That specific behaviour is
1434 * implemented by the callback function that is called at the end of
1435 * the function */
1436
1437 struct mgcp_conn_rtp *conn_src;
1438 struct mgcp_endpoint *endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001439 struct osmo_sockaddr addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001440 socklen_t slen = sizeof(addr);
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001441 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001442 int ret;
1443 enum rtp_proto proto;
1444 struct osmo_rtp_msg_ctx *mc;
Eric8f333032021-08-13 00:00:43 +02001445 struct msgb *msg;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001446 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001447
1448 conn_src = (struct mgcp_conn_rtp *)fd->data;
1449 OSMO_ASSERT(conn_src);
1450 endp = conn_src->conn->endp;
1451 OSMO_ASSERT(endp);
Eric8f333032021-08-13 00:00:43 +02001452 msg = msgb_alloc_c(endp->trunk, RTP_BUF_SIZE, "RTP-rx");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001453
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001454 proto = (fd == &conn_src->end.rtp)? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001455
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001456 ret = recvfrom(fd->fd, msgb_data(msg), msg->data_len, 0, (struct sockaddr *)&addr.u.sa, &slen);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001457
1458 if (ret <= 0) {
1459 LOG_CONN_RTP(conn_src, LOGL_ERROR, "recvfrom error: %s\n", strerror(errno));
1460 rc = -1;
1461 goto out;
1462 }
1463
1464 msgb_put(msg, ret);
1465
1466 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "%s: rx %u bytes from %s:%u\n",
Philipp Maier069dd162022-03-30 16:09:22 +02001467 proto == MGCP_PROTO_RTP ? "RTP" : "RTCP",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001468 msgb_length(msg), osmo_sockaddr_ntop(&addr.u.sa, ipbuf),
1469 osmo_sockaddr_port(&addr.u.sa));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001470
1471 if ((proto == MGCP_PROTO_RTP && check_rtp(conn_src, msg))
1472 || (proto == MGCP_PROTO_RTCP && check_rtcp(conn_src, msg))) {
1473 /* Logging happened in the two check_ functions */
1474 rc = -1;
1475 goto out;
1476 }
1477
Philipp Maierb3d14eb2021-05-20 14:18:52 +02001478 if (mgcp_is_rtp_dummy_payload(msg)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001479 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx dummy packet (dropped)\n");
1480 rc = 0;
1481 goto out;
1482 }
1483
1484 /* Since the msgb remains owned and freed by this function, the msg ctx data struct can just be on the stack and
1485 * needs not be allocated with the msgb. */
1486 mc = OSMO_RTP_MSG_CTX(msg);
1487 *mc = (struct osmo_rtp_msg_ctx){
1488 .proto = proto,
1489 .conn_src = conn_src,
1490 .from_addr = &addr,
1491 };
1492 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "msg ctx: %d %p %s\n",
1493 mc->proto, mc->conn_src,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001494 osmo_hexdump((void*)mc->from_addr,
1495 mc->from_addr->u.sa.sa_family == AF_INET6 ?
1496 sizeof(struct sockaddr_in6) :
1497 sizeof(struct sockaddr_in)));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001498
1499 /* Increment RX statistics */
Pau Espin Pedroldaf5bce2022-09-22 19:14:24 +02001500 rate_ctr_inc(rate_ctr_group_get_ctr(conn_src->ctrg, RTP_PACKETS_RX_CTR));
1501 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 +02001502 /* FIXME: count RTP and RTCP separately, also count IuUP payload-less separately */
1503
1504 /* Forward a copy of the RTP data to a debug ip/port */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001505 forward_data_tap(fd->fd, &conn_src->tap_in, msg);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001506
1507 rc = rx_rtp(msg);
1508
1509out:
1510 msgb_free(msg);
1511 return rc;
1512}
1513
Philipp Maiere1442752022-03-30 16:06:15 +02001514/* Note: This function is able to handle RTP and RTCP */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001515static int rx_rtp(struct msgb *msg)
1516{
1517 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1518 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001519 struct osmo_sockaddr *from_addr = mc->from_addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001520 struct mgcp_conn *conn = conn_src->conn;
1521 struct mgcp_trunk *trunk = conn->endp->trunk;
1522
1523 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx_rtp(%u bytes)\n", msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001524
Oliver Smithe36b7752019-01-22 16:31:36 +01001525 mgcp_conn_watchdog_kick(conn_src->conn);
1526
Philipp Maier228e5912019-03-05 13:56:59 +01001527 /* If AMR is configured for the ingress connection a conversion of the
1528 * framing mode (octet-aligned vs. bandwith-efficient is explicitly
1529 * define, then we check if the incoming payload matches that
1530 * expectation. */
Philipp Maiere1442752022-03-30 16:06:15 +02001531 if (mc->proto == MGCP_PROTO_RTP &&
1532 amr_oa_bwe_convert_indicated(conn_src->end.codec)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001533 int oa = amr_oa_check((char*)msgb_data(msg), msgb_length(msg));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +02001534 if (oa < 0)
1535 return -1;
1536 if (((bool)oa) != conn_src->end.codec->param.amr_octet_aligned)
Philipp Maier228e5912019-03-05 13:56:59 +01001537 return -1;
1538 }
1539
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001540 /* Check if the origin of the RTP packet seems plausible */
1541 if (!trunk->rtp_accept_all && check_rtp_origin(conn_src, from_addr))
1542 return -1;
1543
Philipp Maier87bd9be2017-08-22 16:35:41 +02001544 /* Execute endpoint specific implementation that handles the
1545 * dispatching of the RTP data */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001546 return conn->endp->type->dispatch_rtp_cb(msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001547}
1548
Philipp Maier87bd9be2017-08-22 16:35:41 +02001549/*! bind RTP port to osmo_fd.
Philipp Maier0b79d212020-06-18 12:02:49 +02001550 * \param[in] source_addr source (local) address to bind on.
1551 * \param[in] fd associated file descriptor.
1552 * \param[in] port to bind on.
Harald Welte55a92292021-04-28 19:06:34 +02001553 * \param[in] dscp IP DSCP value to use.
1554 * \param[in] prio socket priority to use.
Philipp Maier0b79d212020-06-18 12:02:49 +02001555 * \returns 0 on success, -1 on ERROR. */
Harald Welte55a92292021-04-28 19:06:34 +02001556int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port, uint8_t dscp,
1557 uint8_t prio)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001558{
Harald Welte8890dfa2017-11-17 15:09:30 +01001559 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001560
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001561 rc = osmo_sock_init2(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
Harald Welte55a92292021-04-28 19:06:34 +02001562 NULL, 0, OSMO_SOCK_F_BIND | OSMO_SOCK_F_DSCP(dscp) |
1563 OSMO_SOCK_F_PRIO(prio));
Harald Welte8890dfa2017-11-17 15:09:30 +01001564 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001565 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001566 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001567 return -1;
1568 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001569 fd->fd = rc;
1570 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001571
1572 return 0;
1573}
1574
Philipp Maier87bd9be2017-08-22 16:35:41 +02001575/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001576static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001577 struct mgcp_rtp_end *rtp_end, struct mgcp_endpoint *endp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001578{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001579 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1580 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1581
Harald Welte55a92292021-04-28 19:06:34 +02001582 if (mgcp_create_bind(source_addr, &rtp_end->rtp, rtp_end->local_port,
1583 cfg->endp_dscp, cfg->endp_priority) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001584 LOGPENDP(endp, DRTP, LOGL_ERROR,
1585 "failed to create RTP port: %s:%d\n",
1586 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001587 goto cleanup0;
1588 }
1589
Harald Welte55a92292021-04-28 19:06:34 +02001590 if (mgcp_create_bind(source_addr, &rtp_end->rtcp, rtp_end->local_port + 1,
1591 cfg->endp_dscp, cfg->endp_priority) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001592 LOGPENDP(endp, DRTP, LOGL_ERROR,
1593 "failed to create RTCP port: %s:%d\n",
1594 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001595 goto cleanup1;
1596 }
1597
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001598 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001599 LOGPENDP(endp, DRTP, LOGL_ERROR,
1600 "failed to register RTP port %d\n",
1601 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001602 goto cleanup2;
1603 }
1604
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001605 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001606 LOGPENDP(endp, DRTP, LOGL_ERROR,
1607 "failed to register RTCP port %d\n",
1608 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001609 goto cleanup3;
1610 }
1611
1612 return 0;
1613
1614cleanup3:
1615 osmo_fd_unregister(&rtp_end->rtp);
1616cleanup2:
1617 close(rtp_end->rtcp.fd);
1618 rtp_end->rtcp.fd = -1;
1619cleanup1:
1620 close(rtp_end->rtp.fd);
1621 rtp_end->rtp.fd = -1;
1622cleanup0:
1623 return -1;
1624}
1625
Philipp Maier87bd9be2017-08-22 16:35:41 +02001626/*! bind RTP port to endpoint/connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001627 * \param[in] endp endpoint that holds the RTP connection.
1628 * \param[in] rtp_port port number to bind on.
1629 * \param[in] conn associated RTP connection.
1630 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001631int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1632 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001633{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001634 char name[512];
1635 struct mgcp_rtp_end *end;
1636
Philipp Maier01d24a32017-11-21 17:26:09 +01001637 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001638 end = &conn->end;
1639
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001640 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001641 LOGPENDP(endp, DRTP, LOGL_ERROR, "%u was already bound on conn:%s\n",
1642 rtp_port, mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001643
1644 /* Double bindings should never occour! Since we always allocate
1645 * connections dynamically and free them when they are not
1646 * needed anymore, there must be no previous binding leftover.
1647 * Should there be a connection bound twice, we have a serious
1648 * problem and must exit immediately! */
1649 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001650 }
1651
1652 end->local_port = rtp_port;
Harald Weltec2a8f592020-10-19 13:25:41 +02001653 osmo_fd_setup(&end->rtp, -1, OSMO_FD_READ, rtp_data_net, conn, 0);
1654 osmo_fd_setup(&end->rtcp, -1, OSMO_FD_READ, rtp_data_net, conn, 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001655
Ericfbf78d12021-08-23 22:31:39 +02001656 return bind_rtp(endp->trunk->cfg, conn->end.local_addr, end, endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001657}
1658
Philipp Maier87bd9be2017-08-22 16:35:41 +02001659/*! free allocated RTP and RTCP ports.
1660 * \param[in] end RTP end */
1661void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001662{
1663 if (end->rtp.fd != -1) {
1664 close(end->rtp.fd);
1665 end->rtp.fd = -1;
1666 osmo_fd_unregister(&end->rtp);
1667 }
1668
1669 if (end->rtcp.fd != -1) {
1670 close(end->rtcp.fd);
1671 end->rtcp.fd = -1;
1672 osmo_fd_unregister(&end->rtcp);
1673 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001674}