blob: 8ade27dcd1e802293245074562adcd7a177f1502 [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
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +020057enum rtp_proto {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020058 MGCP_PROTO_RTP,
59 MGCP_PROTO_RTCP,
60};
61
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +010062void rtpconn_rate_ctr_add(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp,
Harald Weltea48ff4a2020-03-08 14:45:08 +010063 int id, int inc)
64{
65 struct rate_ctr_group *conn_stats = conn_rtp->rate_ctr_group;
Philipp Maierc66ab2c2020-06-02 20:55:34 +020066 struct rate_ctr_group *mgw_stats = endp->trunk->ratectr.all_rtp_conn_stats;
Harald Weltea48ff4a2020-03-08 14:45:08 +010067
Philipp Maierc66ab2c2020-06-02 20:55:34 +020068 /* add to both the per-connection and the global stats */
Pau Espin Pedrol907744e2021-06-04 17:57:34 +020069 rate_ctr_add(rate_ctr_group_get_ctr(conn_stats, id), inc);
70 rate_ctr_add(rate_ctr_group_get_ctr(mgw_stats, id), inc);
Harald Weltea48ff4a2020-03-08 14:45:08 +010071}
72
73static void rtpconn_rate_ctr_inc(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp, int id)
74{
75 rtpconn_rate_ctr_add(conn_rtp, endp, id, 1);
76}
77
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +020078static int rx_rtp(struct msgb *msg);
79
Pau Espin Pedrol33347a42021-07-06 20:04:23 +020080static bool addr_is_any(const struct osmo_sockaddr *osa)
81{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +020082 if (osa->u.sa.sa_family == AF_INET6) {
83 struct in6_addr ip6_any = IN6ADDR_ANY_INIT;
84 return memcmp(&osa->u.sin6.sin6_addr,
85 &ip6_any, sizeof(ip6_any)) == 0;
86 } else {
87 return osa->u.sin.sin_addr.s_addr == 0;
88 }
89}
90
Pau Espin Pedrolca280a12021-07-06 18:15:35 +020091bool mgcp_rtp_end_remote_addr_available(const struct mgcp_rtp_end *rtp_end)
92{
93 return rtp_end->rtp_port && !addr_is_any(&rtp_end->addr);
94}
95
Philipp Maier1cb1e382017-11-02 17:16:04 +010096/*! Determine the local rtp bind IP-address.
Philipp Maier0b79d212020-06-18 12:02:49 +020097 * \param[out] addr caller provided memory to store the resulting IP-Address.
98 * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters.
Philipp Maier1cb1e382017-11-02 17:16:04 +010099 *
100 * The local bind IP-address is automatically selected by probing the
101 * IP-Address of the interface that is pointing towards the remote IP-Address,
102 * if no remote IP-Address is known yet, the statically configured
103 * IP-Addresses are used as fallback. */
104void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn)
105{
106
107 struct mgcp_endpoint *endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200108 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier1cb1e382017-11-02 17:16:04 +0100109 int rc;
110 endp = conn->conn->endp;
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200111 bool rem_addr_set = !addr_is_any(&conn->end.addr);
112 char *bind_addr;
Philipp Maier1cb1e382017-11-02 17:16:04 +0100113
114 /* Try probing the local IP-Address */
Ericfbf78d12021-08-23 22:31:39 +0200115 if (endp->trunk->cfg->net_ports.bind_addr_probe && rem_addr_set) {
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200116 rc = osmo_sock_local_ip(addr, osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Philipp Maier1cb1e382017-11-02 17:16:04 +0100117 if (rc < 0)
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200118 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
119 "local interface auto detection failed, using configured addresses...\n");
Philipp Maier1cb1e382017-11-02 17:16:04 +0100120 else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200121 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
122 "selected local rtp bind ip %s by probing using remote ip %s\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200123 addr, osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Philipp Maier1cb1e382017-11-02 17:16:04 +0100124 return;
125 }
126 }
127
Pau Espin Pedrola93c6e92019-05-06 15:23:57 +0200128 /* Select from preconfigured IP-Addresses. We don't have bind_addr for Osmux (yet?). */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200129 if (rem_addr_set) {
Philipp Maier1cb1e382017-11-02 17:16:04 +0100130 /* Check there is a bind IP for the RTP traffic configured,
131 * if so, use that IP-Address */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200132 bind_addr = conn->end.addr.u.sa.sa_family == AF_INET6 ?
Ericfbf78d12021-08-23 22:31:39 +0200133 endp->trunk->cfg->net_ports.bind_addr_v6 :
134 endp->trunk->cfg->net_ports.bind_addr_v4;
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200135 } else {
136 /* Choose any of the bind addresses, preferring v6 over v4 */
Ericfbf78d12021-08-23 22:31:39 +0200137 bind_addr = endp->trunk->cfg->net_ports.bind_addr_v6;
Eric2764bdb2021-08-23 22:11:47 +0200138 if (!strlen(bind_addr))
Ericfbf78d12021-08-23 22:31:39 +0200139 bind_addr = endp->trunk->cfg->net_ports.bind_addr_v4;
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200140 }
Eric2764bdb2021-08-23 22:11:47 +0200141 if (strlen(bind_addr)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200142 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
143 "using configured rtp bind ip as local bind ip %s\n",
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200144 bind_addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100145 } else {
146 /* No specific bind IP is configured for the RTP traffic, so
147 * assume the IP where we listen for incoming MGCP messages
148 * as bind IP */
Ericfbf78d12021-08-23 22:31:39 +0200149 bind_addr = endp->trunk->cfg->source_addr;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200150 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200151 "using mgcp bind ip as local rtp bind ip: %s\n", bind_addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100152 }
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200153 osmo_strlcpy(addr, bind_addr, INET6_ADDRSTRLEN);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100154}
155
Philipp Maier87bd9be2017-08-22 16:35:41 +0200156/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200157 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +0200158 * 1/codec_rate seconds. */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100159uint32_t mgcp_get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200160{
161 struct timespec tp;
162 uint64_t ret;
163
Philipp Maier87bd9be2017-08-22 16:35:41 +0200164 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200165 return 0;
166
167 memset(&tp, 0, sizeof(tp));
168 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maierc3413882017-10-27 12:26:54 +0200169 LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200170
171 /* convert it to 1/unit seconds */
172 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200173 ret *= codec_rate;
174 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200175
176 return ret;
177}
178
Philipp Maier87bd9be2017-08-22 16:35:41 +0200179/* Compute timestamp alignment error */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200180static int32_t ts_alignment_error(const struct mgcp_rtp_stream_state *sstate,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200181 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200182{
183 int32_t timestamp_delta;
184
185 if (ptime == 0)
186 return 0;
187
188 /* Align according to: T - Tlast = k * Tptime */
189 timestamp_delta = timestamp - sstate->last_timestamp;
190
191 return timestamp_delta % ptime;
192}
193
Philipp Maier87bd9be2017-08-22 16:35:41 +0200194/* Check timestamp and sequence number for plausibility */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200195static int check_rtp_timestamp(const struct mgcp_endpoint *endp,
196 const struct mgcp_rtp_state *state,
197 const struct mgcp_rtp_stream_state *sstate,
198 const struct mgcp_rtp_end *rtp_end,
199 const struct osmo_sockaddr *addr,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200200 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200201 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200202{
203 int32_t tsdelta;
204 int32_t timestamp_error;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200205 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200206
207 /* Not fully intialized, skip */
208 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
209 return 0;
210
211 if (seq == sstate->last_seq) {
212 if (timestamp != sstate->last_timestamp) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200213 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200214 LOGPENDP(endp, DRTP, LOGL_ERROR,
215 "The %s timestamp delta is != 0 but the sequence "
216 "number %d is the same, "
217 "TS offset: %d, SeqNo offset: %d "
218 "on SSRC: %u timestamp: %u "
219 "from %s:%d\n",
220 text, seq,
221 state->patch.timestamp_offset, state->patch.seq_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200222 sstate->ssrc, timestamp,
223 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
224 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200225 }
226 return 0;
227 }
228
229 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200230 (int32_t)(timestamp - sstate->last_timestamp) /
231 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200232
233 if (tsdelta == 0) {
234 /* Don't update *tsdelta_out */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200235 LOGPENDP(endp, DRTP, LOGL_NOTICE,
236 "The %s timestamp delta is %d "
237 "on SSRC: %u timestamp: %u "
238 "from %s:%d\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200239 text, tsdelta, sstate->ssrc, timestamp,
240 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
241 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200242
243 return 0;
244 }
245
246 if (sstate->last_tsdelta != tsdelta) {
247 if (sstate->last_tsdelta) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200248 LOGPENDP(endp, DRTP, LOGL_INFO,
249 "The %s timestamp delta changes from %d to %d "
250 "on SSRC: %u timestamp: %u from %s:%d\n",
251 text, sstate->last_tsdelta, tsdelta,
252 sstate->ssrc, timestamp,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200253 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
254 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200255 }
256 }
257
258 if (tsdelta_out)
259 *tsdelta_out = tsdelta;
260
261 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200262 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200263
264 if (timestamp_error) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200265 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200266 LOGPENDP(endp, DRTP, LOGL_NOTICE,
267 "The %s timestamp has an alignment error of %d "
268 "on SSRC: %u "
269 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
270 "from %s:%d. ptime: %d\n",
271 text, timestamp_error,
272 sstate->ssrc,
273 (int16_t)(seq - sstate->last_seq),
274 (int32_t)(timestamp - sstate->last_timestamp),
275 tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200276 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
277 osmo_sockaddr_port(&addr->u.sa),
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200278 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200279 }
280 return 1;
281}
282
283/* Set the timestamp offset according to the packet duration. */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200284static int adjust_rtp_timestamp_offset(const struct mgcp_endpoint *endp,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200285 struct mgcp_rtp_state *state,
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200286 const struct mgcp_rtp_end *rtp_end,
287 const struct osmo_sockaddr *addr,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200288 int16_t delta_seq, uint32_t in_timestamp,
289 bool marker_bit)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200290{
291 int32_t tsdelta = state->packet_duration;
292 int timestamp_offset;
293 uint32_t out_timestamp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200294 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200295
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200296 if (marker_bit) {
297 /* If RTP pkt contains marker bit, the timestamps are not longer
298 * in sync, so we can erase timestamp offset patching. */
299 state->patch.timestamp_offset = 0;
300 return 0;
301 }
302
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200303 if (tsdelta == 0) {
304 tsdelta = state->out_stream.last_tsdelta;
305 if (tsdelta != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200306 LOGPENDP(endp, DRTP, LOGL_NOTICE,
307 "A fixed packet duration is not available, "
308 "using last output timestamp delta instead: %d "
309 "from %s:%d\n", tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200310 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
311 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200312 } else {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200313 tsdelta = rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200314 LOGPENDP(endp, DRTP, LOGL_NOTICE,
315 "Fixed packet duration and last timestamp delta "
316 "are not available, "
317 "using fixed 20ms instead: %d "
318 "from %s:%d\n", tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200319 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
320 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200321 }
322 }
323
324 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
325 timestamp_offset = out_timestamp - in_timestamp;
326
Harald Welte33381352017-12-25 09:44:26 +0100327 if (state->patch.timestamp_offset != timestamp_offset) {
328 state->patch.timestamp_offset = timestamp_offset;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200329 LOGPENDP(endp, DRTP, LOGL_NOTICE,
330 "Timestamp offset change on SSRC: %u "
331 "SeqNo delta: %d, TS offset: %d, "
332 "from %s:%d\n", state->in_stream.ssrc,
333 delta_seq, state->patch.timestamp_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200334 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
335 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200336 }
337
338 return timestamp_offset;
339}
340
341/* Set the timestamp offset according to the packet duration. */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200342static int align_rtp_timestamp_offset(const struct mgcp_endpoint *endp,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200343 struct mgcp_rtp_state *state,
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200344 const struct mgcp_rtp_end *rtp_end,
345 const struct osmo_sockaddr *addr,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200346 uint32_t timestamp, bool marker_bit)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200347{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200348 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200349 int ts_error = 0;
350 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200351 int ptime = state->packet_duration;
352
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200353 if (marker_bit) {
354 /* If RTP pkt contains marker bit, the timestamps are not longer
355 * in sync, so no alignment is needed. */
356 return 0;
357 }
358
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200359 /* Align according to: T + Toffs - Tlast = k * Tptime */
360
Philipp Maier87bd9be2017-08-22 16:35:41 +0200361 ts_error = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100362 timestamp + state->patch.timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200363
Philipp Maier87bd9be2017-08-22 16:35:41 +0200364 /* If there is an alignment error, we have to compensate it */
365 if (ts_error) {
Harald Welte33381352017-12-25 09:44:26 +0100366 state->patch.timestamp_offset += ptime - ts_error;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200367 LOGPENDP(endp, DRTP, LOGL_NOTICE,
368 "Corrected timestamp alignment error of %d on SSRC: %u "
369 "new TS offset: %d, "
370 "from %s:%d\n",
371 ts_error, state->in_stream.ssrc,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200372 state->patch.timestamp_offset,
373 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
374 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200375 }
376
Philipp Maier87bd9be2017-08-22 16:35:41 +0200377 /* Check we really managed to compensate the timestamp
378 * offset. There should not be any remaining error, failing
Harald Welte1d1b98f2017-12-25 10:03:40 +0100379 * here would point to a serous problem with the alignment
380 * error computation function */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200381 ts_check = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100382 timestamp + state->patch.timestamp_offset);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200383 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200384
Philipp Maier87bd9be2017-08-22 16:35:41 +0200385 /* Return alignment error before compensation */
386 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200387}
388
Philipp Maier87bd9be2017-08-22 16:35:41 +0200389/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200390 * \param[in] associated endpoint.
391 * \param[in] destination RTP end.
392 * \param[in,out] pointer to buffer with voice data.
393 * \param[in] voice data length.
394 * \param[in] maximum size of caller provided voice data buffer.
395 * \returns ignores input parameters, return always 0. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200396int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
397 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200398 char *data, int *len, int buf_size)
399{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200400 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200401 return 0;
402}
403
Philipp Maier87bd9be2017-08-22 16:35:41 +0200404/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200405 * \param[in] associated endpoint.
406 * \param[in] destination RTP connnection.
407 * \param[in] source RTP connection.
408 * \returns ignores input parameters, return always 0. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200409int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
Philipp Maieracc10352018-07-19 18:07:57 +0200410 struct mgcp_conn_rtp *conn_dst,
411 struct mgcp_conn_rtp *conn_src)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200412{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200413 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200414 return 0;
415}
416
417void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
Philipp Maier58128252019-03-06 11:28:18 +0100418 const struct mgcp_rtp_codec **codec,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200419 const char **fmtp_extra,
420 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200421{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200422 LOGPENDP(endp, DRTP, LOGL_DEBUG, "conn:%s using format defaults\n",
423 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200424
Philipp Maier58128252019-03-06 11:28:18 +0100425 *codec = conn->end.codec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200426 *fmtp_extra = conn->end.fmtp_extra;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200427}
428
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200429void mgcp_rtp_annex_count(const struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200430 struct mgcp_rtp_state *state, const uint16_t seq,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200431 const int32_t transit, const uint32_t ssrc,
432 const bool marker_bit)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200433{
434 int32_t d;
435
436 /* initialize or re-initialize */
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200437 if (!state->stats.initialized || state->stats.ssrc != ssrc || marker_bit) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100438 state->stats.initialized = 1;
439 state->stats.base_seq = seq;
440 state->stats.max_seq = seq - 1;
441 state->stats.ssrc = ssrc;
442 state->stats.jitter = 0;
443 state->stats.transit = transit;
444 state->stats.cycles = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200445 } else {
446 uint16_t udelta;
447
Philipp Maier87bd9be2017-08-22 16:35:41 +0200448 /* The below takes the shape of the validation of
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200449 * Appendix A. Check if there is something weird with
450 * the sequence number, otherwise check for a wrap
451 * around in the sequence number.
452 * It can't wrap during the initialization so let's
453 * skip it here. The Appendix A probably doesn't have
Philipp Maier87bd9be2017-08-22 16:35:41 +0200454 * this issue because of the probation. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100455 udelta = seq - state->stats.max_seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200456 if (udelta < RTP_MAX_DROPOUT) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100457 if (seq < state->stats.max_seq)
458 state->stats.cycles += RTP_SEQ_MOD;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200459 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200460 LOGPENDP(endp, DRTP, LOGL_NOTICE,
461 "RTP seqno made a very large jump on delta: %u\n",
462 udelta);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200463 }
464 }
465
Philipp Maier87bd9be2017-08-22 16:35:41 +0200466 /* Calculate the jitter between the two packages. The TS should be
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200467 * taken closer to the read function. This was taken from the
468 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
Philipp Maier87bd9be2017-08-22 16:35:41 +0200469 * resolution. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100470 d = transit - state->stats.transit;
471 state->stats.transit = transit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200472 if (d < 0)
473 d = -d;
Harald Welte49e3d5a2017-12-25 09:47:57 +0100474 state->stats.jitter += d - ((state->stats.jitter + 8) >> 4);
475 state->stats.max_seq = seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200476}
477
Philipp Maier6931f9a2018-07-26 09:29:31 +0200478/* There may be different payload type numbers negotiated for two connections.
479 * Patch the payload type of an RTP packet so that it uses the payload type
480 * that is valid for the destination connection (conn_dst) */
481static int mgcp_patch_pt(struct mgcp_conn_rtp *conn_src,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200482 struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200483{
484 struct rtp_hdr *rtp_hdr;
485 uint8_t pt_in;
486 int pt_out;
487
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200488 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
489 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
490 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200491 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200492 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200493
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200494 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier6931f9a2018-07-26 09:29:31 +0200495
496 pt_in = rtp_hdr->payload_type;
497 pt_out = mgcp_codec_pt_translate(conn_src, conn_dst, pt_in);
498 if (pt_out < 0)
499 return -EINVAL;
500
501 rtp_hdr->payload_type = (uint8_t) pt_out;
502 return 0;
503}
504
Philipp Maier87bd9be2017-08-22 16:35:41 +0200505/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200506 * some of the supported endpoints (e.g. the nanoBTS) can only handle
507 * one source and this code will patch RTP header to appear as if there
508 * is only one source.
509 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200510 * we receive will be seen as a switch in streams. */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200511void mgcp_patch_and_count(const struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200512 struct mgcp_rtp_state *state,
513 struct mgcp_rtp_end *rtp_end,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200514 struct osmo_sockaddr *addr, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200515{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200516 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200517 uint32_t arrival_time;
518 int32_t transit;
519 uint16_t seq;
520 uint32_t timestamp, ssrc;
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200521 bool marker_bit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200522 struct rtp_hdr *rtp_hdr;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200523 int payload = rtp_end->codec->payload_type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200524 unsigned int len = msgb_length(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200525
526 if (len < sizeof(*rtp_hdr))
527 return;
528
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200529 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200530 seq = ntohs(rtp_hdr->sequence);
531 timestamp = ntohl(rtp_hdr->timestamp);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100532 arrival_time = mgcp_get_current_ts(rtp_end->codec->rate);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200533 ssrc = ntohl(rtp_hdr->ssrc);
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200534 marker_bit = !!rtp_hdr->marker;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200535 transit = arrival_time - timestamp;
536
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200537 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc, marker_bit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200538
539 if (!state->initialized) {
540 state->initialized = 1;
541 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100542 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200543 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200544 state->packet_duration =
545 mgcp_rtp_packet_duration(endp, rtp_end);
Philipp Maier0ec1d4e2018-05-16 11:09:42 +0200546 state->out_stream.last_seq = seq - 1;
547 state->out_stream.ssrc = state->patch.orig_ssrc = ssrc;
548 state->out_stream.last_tsdelta = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200549 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200550 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200551 LOGPENDP(endp, DRTP, LOGL_INFO,
552 "initializing stream, SSRC: %u timestamp: %u "
553 "pkt-duration: %d, from %s:%d\n",
554 state->in_stream.ssrc,
555 state->patch.seq_offset, state->packet_duration,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200556 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
557 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200558 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200559 state->packet_duration =
Philipp Maierbc0346e2018-06-07 09:52:16 +0200560 rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200561 LOGPENDP(endp, DRTP, LOGL_NOTICE,
562 "fixed packet duration is not available, "
563 "using fixed 20ms instead: %d from %s:%d\n",
564 state->packet_duration,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200565 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
566 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200567 }
568 } else if (state->in_stream.ssrc != ssrc) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200569 LOGPENDP(endp, DRTP, LOGL_NOTICE,
570 "SSRC changed: %u -> %u "
571 "from %s:%d\n",
572 state->in_stream.ssrc, rtp_hdr->ssrc,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200573 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
574 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200575
576 state->in_stream.ssrc = ssrc;
577 if (rtp_end->force_constant_ssrc) {
578 int16_t delta_seq;
579
580 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100581 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200582 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200583
584 /* Estimate number of packets that would have been sent */
585 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200586 (arrival_time - state->in_stream.last_arrival_time
587 + state->packet_duration / 2) /
588 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200589
590 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200591 delta_seq, timestamp, marker_bit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200592
Pau Espin Pedrol4c77e9b2021-07-07 12:12:58 +0200593 state->patch.patch_ssrc = true;
Harald Welte33381352017-12-25 09:44:26 +0100594 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200595 if (rtp_end->force_constant_ssrc != -1)
596 rtp_end->force_constant_ssrc -= 1;
597
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200598 LOGPENDP(endp, DRTP, LOGL_NOTICE,
599 "SSRC patching enabled, SSRC: %u "
600 "SeqNo offset: %d, TS offset: %d "
601 "from %s:%d\n", state->in_stream.ssrc,
602 state->patch.seq_offset, state->patch.timestamp_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200603 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
604 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200605 }
606
607 state->in_stream.last_tsdelta = 0;
608 } else {
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200609 if (!marker_bit) {
610 /* Compute current per-packet timestamp delta */
611 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
612 addr, seq, timestamp, "input",
613 &state->in_stream.last_tsdelta);
614 } else {
615 state->in_stream.last_tsdelta = 0;
616 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200617
Harald Welte33381352017-12-25 09:44:26 +0100618 if (state->patch.patch_ssrc)
619 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200620 }
621
622 /* Save before patching */
623 state->in_stream.last_timestamp = timestamp;
624 state->in_stream.last_seq = seq;
625 state->in_stream.last_arrival_time = arrival_time;
626
627 if (rtp_end->force_aligned_timing &&
628 state->out_stream.ssrc == ssrc && state->packet_duration)
629 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200630 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200631 timestamp, marker_bit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200632
633 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100634 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200635 rtp_hdr->ssrc = htonl(ssrc);
636
637 /* Apply the offset and store it back to the packet.
638 * This won't change anything if the offset is 0, so the conditional is
639 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100640 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200641 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100642 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200643 rtp_hdr->timestamp = htonl(timestamp);
644
645 /* Check again, whether the timestamps are still valid */
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200646 if (!marker_bit) {
647 if (state->out_stream.ssrc == ssrc)
648 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
649 addr, seq, timestamp, "output",
650 &state->out_stream.last_tsdelta);
651 } else {
652 state->out_stream.last_tsdelta = 0;
653 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200654
655 /* Save output values */
656 state->out_stream.last_seq = seq;
657 state->out_stream.last_timestamp = timestamp;
658 state->out_stream.ssrc = ssrc;
659
660 if (payload < 0)
661 return;
662
663#if 0
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200664 LOGPENDP(endp, DRTP, LOGL_DEBUG, "payload hdr payload %u -> endp payload %u\n",
665 rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200666 rtp_hdr->payload_type = payload;
667#endif
668}
669
Philipp Maier9fc8a022019-02-20 12:26:52 +0100670/* There are different dialects used to format and transfer voice data. When
671 * the receiving end expects GSM-HR data to be formated after RFC 5993, this
672 * function is used to convert between RFC 5993 and TS 101318, which we normally
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200673 * use.
674 * Return 0 on sucess, negative on errors like invalid data length. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200675static int rfc5993_hr_convert(struct mgcp_endpoint *endp, struct msgb *msg)
Philipp Maier9fc8a022019-02-20 12:26:52 +0100676{
Philipp Maier9fc8a022019-02-20 12:26:52 +0100677 struct rtp_hdr *rtp_hdr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200678 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200679 LOGPENDP(endp, DRTP, LOGL_ERROR, "AMR RTP packet too short (%d < %zu)\n",
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200680 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200681 return -EINVAL;
682 }
683
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200684 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100685
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200686 if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr)) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100687 /* TS 101318 encoding => RFC 5993 encoding */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200688 msgb_put(msg, 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100689 memmove(rtp_hdr->data + 1, rtp_hdr->data, GSM_HR_BYTES);
690 rtp_hdr->data[0] = 0x00;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200691 } else if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr) + 1) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100692 /* RFC 5993 encoding => TS 101318 encoding */
693 memmove(rtp_hdr->data, rtp_hdr->data + 1, GSM_HR_BYTES);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200694 msgb_trim(msg, msgb_length(msg) - 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100695 } else {
696 /* It is possible that multiple payloads occur in one RTP
697 * packet. This is not supported yet. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200698 LOGPENDP(endp, DRTP, LOGL_ERROR,
699 "cannot figure out how to convert RTP packet\n");
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200700 return -ENOTSUP;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100701 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200702 return 0;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100703}
704
Philipp Maier228e5912019-03-05 13:56:59 +0100705/* For AMR RTP two framing modes are defined RFC3267. There is a bandwith
706 * efficient encoding scheme where all fields are packed together one after
707 * another and an octet aligned mode where all fields are aligned to octet
708 * boundaries. This function is used to convert between the two modes */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200709static int amr_oa_bwe_convert(struct mgcp_endpoint *endp, struct msgb *msg,
Philipp Maier228e5912019-03-05 13:56:59 +0100710 bool target_is_oa)
711{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200712 /* NOTE: the msgb has an allocated length of RTP_BUF_SIZE, so there is
Philipp Maier228e5912019-03-05 13:56:59 +0100713 * plenty of space available to store the slightly larger, converted
714 * data */
Philipp Maier228e5912019-03-05 13:56:59 +0100715 struct rtp_hdr *rtp_hdr;
716 unsigned int payload_len;
717 int rc;
718
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200719 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
720 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 +0200721 return -EINVAL;
722 }
723
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200724 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier228e5912019-03-05 13:56:59 +0100725
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200726 payload_len = msgb_length(msg) - sizeof(struct rtp_hdr);
Philipp Maier228e5912019-03-05 13:56:59 +0100727
728 if (osmo_amr_is_oa(rtp_hdr->data, payload_len)) {
729 if (!target_is_oa)
730 /* Input data is oa an target format is bwe
731 * ==> convert */
732 rc = osmo_amr_oa_to_bwe(rtp_hdr->data, payload_len);
733 else
734 /* Input data is already bew, but we accept it anyway
735 * ==> no conversion needed */
736 rc = payload_len;
737 } else {
738 if (target_is_oa)
739 /* Input data is bwe an target format is oa
740 * ==> convert */
741 rc = osmo_amr_bwe_to_oa(rtp_hdr->data, payload_len,
742 RTP_BUF_SIZE);
743 else
744 /* Input data is already oa, but we accept it anyway
745 * ==> no conversion needed */
746 rc = payload_len;
747 }
748 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200749 LOGPENDP(endp, DRTP, LOGL_ERROR,
750 "AMR RTP packet conversion failed\n");
Philipp Maier228e5912019-03-05 13:56:59 +0100751 return -EINVAL;
752 }
753
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200754 return msgb_trim(msg, rc + sizeof(struct rtp_hdr));
Philipp Maier228e5912019-03-05 13:56:59 +0100755}
756
757/* Check if a conversion between octet-aligned and bandwith-efficient mode is
758 * indicated. */
759static bool amr_oa_bwe_convert_indicated(struct mgcp_rtp_codec *codec)
760{
761 if (codec->param_present == false)
762 return false;
763 if (!codec->param.amr_octet_aligned_present)
764 return false;
765 if (strcmp(codec->subtype_name, "AMR") != 0)
766 return false;
767 return true;
768}
769
770
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200771/* Return whether an RTP packet with AMR payload is in octet-aligned mode.
772 * Return 0 if in bandwidth-efficient mode, 1 for octet-aligned mode, and negative if the RTP data is invalid. */
773static int amr_oa_check(char *data, int len)
Philipp Maier228e5912019-03-05 13:56:59 +0100774{
775 struct rtp_hdr *rtp_hdr;
776 unsigned int payload_len;
777
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200778 if (len < sizeof(struct rtp_hdr))
779 return -EINVAL;
780
Philipp Maier228e5912019-03-05 13:56:59 +0100781 rtp_hdr = (struct rtp_hdr *)data;
782
783 payload_len = len - sizeof(struct rtp_hdr);
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200784 if (payload_len < sizeof(struct amr_hdr))
785 return -EINVAL;
Philipp Maier228e5912019-03-05 13:56:59 +0100786
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200787 return osmo_amr_is_oa(rtp_hdr->data, payload_len) ? 1 : 0;
Philipp Maier228e5912019-03-05 13:56:59 +0100788}
789
Philipp Maier87bd9be2017-08-22 16:35:41 +0200790/* Forward data to a debug tap. This is debug function that is intended for
791 * debugging the voice traffic with tools like gstreamer */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100792void forward_data_tap(int fd, struct mgcp_rtp_tap *tap, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200793{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100794 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200795
Philipp Maiere6f172d2017-11-07 12:00:01 +0100796 if (!tap->enabled)
797 return;
798
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200799 rc = sendto(fd, msgb_data(msg), msgb_length(msg), 0, (struct sockaddr *)&tap->forward,
Philipp Maiere6f172d2017-11-07 12:00:01 +0100800 sizeof(tap->forward));
801
802 if (rc < 0)
803 LOGP(DRTP, LOGL_ERROR,
804 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200805}
806
Philipp Maier889fe7f2020-07-06 17:44:12 +0200807/* Generate an RTP header if it is missing */
808static void gen_rtp_header(struct msgb *msg, struct mgcp_rtp_end *rtp_end,
809 struct mgcp_rtp_state *state)
810{
811 struct rtp_hdr *hdr = (struct rtp_hdr *)msgb_data(msg);
812
813 if (hdr->version > 0)
814 return;
815
816 hdr->version = 2;
817 hdr->payload_type = rtp_end->codec->payload_type;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100818 hdr->timestamp = osmo_htonl(mgcp_get_current_ts(rtp_end->codec->rate));
Philipp Maier889fe7f2020-07-06 17:44:12 +0200819 hdr->sequence = osmo_htons(state->alt_rtp_tx_sequence);
820 hdr->ssrc = state->alt_rtp_tx_ssrc;
821}
822
Philipp Maier87bd9be2017-08-22 16:35:41 +0200823/* Check if the origin (addr) matches the address/port data of the RTP
824 * connections. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200825static int check_rtp_origin(struct mgcp_conn_rtp *conn, struct osmo_sockaddr *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200826{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200827 char ipbuf[INET6_ADDRSTRLEN];
828
829 if (addr_is_any(&conn->end.addr)) {
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200830 switch (conn->conn->mode) {
831 case MGCP_CONN_LOOPBACK:
832 /* HACK: for IuUP, we want to reply with an IuUP Initialization ACK upon the first RTP
833 * message received. We currently hackishly accomplish that by putting the endpoint in
834 * loopback mode and patching over the looped back RTP message to make it look like an
835 * ack. We don't know the femto cell's IP address and port until the RAB Assignment
836 * Response is received, but the nano3G expects an IuUP Initialization Ack before it even
837 * sends the RAB Assignment Response. Hence, if the remote address is 0.0.0.0 and the
838 * MGCP port is in loopback mode, allow looping back the packet to any source. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200839 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
840 "In loopback mode and remote address not set:"
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200841 " allowing data from address: %s\n",
842 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200843 return 0;
844
845 default:
846 /* Receiving early media before the endpoint is configured. Instead of logging
847 * this as an error that occurs on every call, keep it more low profile to not
848 * confuse humans with expected errors. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200849 LOGPCONN(conn->conn, DRTP, LOGL_INFO,
850 "Rx RTP from %s, but remote address not set:"
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200851 " dropping early media\n",
852 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200853 return -1;
854 }
Neels Hofmeyr0063ca22018-07-23 18:12:16 +0200855 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200856
Philipp Maier87bd9be2017-08-22 16:35:41 +0200857 /* Note: Check if the inbound RTP data comes from the same host to
858 * which we send our outgoing RTP traffic. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200859 if (conn->end.addr.u.sa.sa_family != addr->u.sa.sa_family ||
860 (conn->end.addr.u.sa.sa_family == AF_INET &&
861 conn->end.addr.u.sin.sin_addr.s_addr != addr->u.sin.sin_addr.s_addr) ||
862 (conn->end.addr.u.sa.sa_family == AF_INET6 &&
863 memcmp(&conn->end.addr.u.sin6.sin6_addr, &addr->u.sin6.sin6_addr,
864 sizeof(struct in6_addr)))) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200865 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200866 "data from wrong address: %s, ",
867 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Philipp Maierc3413882017-10-27 12:26:54 +0200868 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200869 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200870 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200871 return -1;
872 }
873
Philipp Maier87bd9be2017-08-22 16:35:41 +0200874 /* Note: Usually the remote remote port of the data we receive will be
875 * the same as the remote port where we transmit outgoing RTP traffic
876 * to (set by MDCX). We use this to check the origin of the data for
877 * plausibility. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200878 if (ntohs(conn->end.rtp_port) != osmo_sockaddr_port(&addr->u.sa) &&
879 ntohs(conn->end.rtcp_port) != osmo_sockaddr_port(&addr->u.sa)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200880 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200881 "data from wrong source port: %d, ",
882 osmo_sockaddr_port(&addr->u.sa));
Philipp Maierc3413882017-10-27 12:26:54 +0200883 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200884 "expected: %d for RTP or %d for RTCP\n",
885 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200886 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200887 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200888 }
889
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200890 return 0;
891}
892
Philipp Maier87bd9be2017-08-22 16:35:41 +0200893/* Check the if the destination address configuration of an RTP connection
894 * makes sense */
895static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200896{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200897 char ipbuf[INET6_ADDRSTRLEN];
898 bool ip_is_any = addr_is_any(&conn->end.addr);
899
Philipp Maiere6df0e42018-05-29 14:03:06 +0200900 /* Note: it is legal to create a connection but never setting a port
901 * and IP-address for outgoing data. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200902 if (ip_is_any && conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200903 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
Neels Hofmeyra4b677c2021-07-07 23:38:23 +0200904 "destination IP-address and rtp port is not (yet) known (%s:%u)\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200905 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port);
Philipp Maiere6df0e42018-05-29 14:03:06 +0200906 return -1;
907 }
908
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200909 if (ip_is_any) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200910 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
911 "destination IP-address is invalid (%s:%u)\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200912 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200913 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200914 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200915
916 if (conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200917 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
918 "destination rtp port is invalid (%s:%u)\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200919 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200920 return -1;
921 }
922
923 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200924}
925
Philipp Maier4dba7692018-08-03 12:20:52 +0200926/* Do some basic checks to make sure that the RTCP packets we are going to
927 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200928static int check_rtcp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +0200929{
930 struct rtcp_hdr *hdr;
931 unsigned int len;
932 uint8_t type;
933
934 /* RTPC packets that are just a header without data do not make
935 * any sense. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200936 if (msgb_length(msg) < sizeof(struct rtcp_hdr)) {
937 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP packet too short (%u < %zu)\n",
938 msgb_length(msg), sizeof(struct rtcp_hdr));
Philipp Maier4dba7692018-08-03 12:20:52 +0200939 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200940 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200941
942 /* Make sure that the length of the received packet does not exceed
943 * the available buffer size */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200944 hdr = (struct rtcp_hdr *)msgb_data(msg);
Philipp Maier4dba7692018-08-03 12:20:52 +0200945 len = (osmo_ntohs(hdr->length) + 1) * 4;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200946 if (len > msgb_length(msg)) {
947 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header length exceeds packet size (%u > %u)\n",
948 len, msgb_length(msg));
Philipp Maier4dba7692018-08-03 12:20:52 +0200949 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200950 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200951
952 /* Make sure we accept only packets that have a proper packet type set
953 * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
954 type = hdr->type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200955 if ((type < 192 || type > 195) && (type < 200 || type > 213)) {
956 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header: invalid type: %u\n", type);
Philipp Maier4dba7692018-08-03 12:20:52 +0200957 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200958 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200959
960 return 0;
961}
962
963/* Do some basic checks to make sure that the RTP packets we are going to
964 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200965static int check_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +0200966{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200967 size_t min_size = sizeof(struct rtp_hdr);
968 if (msgb_length(msg) < min_size) {
969 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
970 msgb_length(msg), min_size);
971 return -1;
972 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200973
974 /* FIXME: Add more checks, the reason why we do not check more than
975 * the length is because we currently handle IUUP packets as RTP
976 * packets, so they must pass this check, if we weould be more
977 * strict here, we would possibly break 3G. (see also FIXME note
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100978 * below.*/
Philipp Maier4dba7692018-08-03 12:20:52 +0200979
980 return 0;
981}
982
Philipp Maier87bd9be2017-08-22 16:35:41 +0200983/* Send RTP data. Possible options are standard RTP packet
984 * transmission or trsmission via an osmux connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200985static int mgcp_send_rtp(struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200986{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200987 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
988 enum rtp_proto proto = mc->proto;
989 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200990 struct mgcp_endpoint *endp = conn_src->conn->endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200991
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200992 LOGPENDP(endp, DRTP, LOGL_DEBUG, "destin conn:%s\n",
993 mgcp_conn_dump(conn_dst->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200994
995 /* Before we try to deliver the packet, we check if the destination
996 * port and IP-Address make sense at all. If not, we will be unable
997 * to deliver the packet. */
998 if (check_rtp_destin(conn_dst) != 0)
999 return -1;
1000
1001 /* Depending on the RTP connection type, deliver the RTP packet to the
1002 * destination connection. */
1003 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001004 case MGCP_RTP_DEFAULT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001005 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1006 "endpoint type is MGCP_RTP_DEFAULT, "
1007 "using mgcp_send() to forward data directly\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001008 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001009 mc->from_addr, msg, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001010 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001011 case MGCP_OSMUX_BSC:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001012 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1013 "endpoint type is MGCP_OSMUX_BSC_NAT, "
1014 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n");
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001015 return osmux_xfrm_to_osmux((char*)msgb_data(msg), msgb_length(msg), conn_dst);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001016 case MGCP_RTP_IUUP:
1017 if (proto == MGCP_PROTO_RTP) {
1018 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1019 "endpoint type is MGCP_RTP_IUUP, "
1020 "using mgcp_conn_iuup_send_rtp() to forward data over IuUP\n");
1021 return mgcp_conn_iuup_send_rtp(conn_src, conn_dst, msg);
1022 }
1023 /* RTCP: we forward as usual for regular RTP connection */
1024 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1025 "endpoint type is MGCP_RTP_IUUP and proto!=MGCP_PROTO_RTP, "
1026 "using mgcp_send() to forward data directly\n");
1027 return mgcp_send(endp, false,
1028 mc->from_addr, msg, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001029 }
1030
Philipp Maier87bd9be2017-08-22 16:35:41 +02001031 /* If the data has not been handled/forwarded until here, it will
1032 * be discarded, this should not happen, normally the MGCP type
1033 * should be properly set */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001034 LOGPENDP(endp, DRTP, LOGL_ERROR, "bad MGCP type -- data discarded!\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001035
1036 return -1;
1037}
1038
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001039/*! send udp packet.
1040 * \param[in] fd associated file descriptor.
1041 * \param[in] addr destination ip-address.
1042 * \param[in] port destination UDP port (network byte order).
1043 * \param[in] buf buffer that holds the data to be send.
1044 * \param[in] len length of the data to be sent.
1045 * \returns bytes sent, -1 on error. */
1046int mgcp_udp_send(int fd, struct osmo_sockaddr *addr, int port, const char *buf, int len)
1047{
1048 char ipbuf[INET6_ADDRSTRLEN];
1049 size_t addr_len;
1050 bool is_ipv6 = addr->u.sa.sa_family == AF_INET6;
1051
1052 LOGP(DRTP, LOGL_DEBUG,
1053 "sending %i bytes length packet to %s:%u ...\n", len,
1054 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
1055 ntohs(port));
1056
1057 if (is_ipv6) {
1058 addr->u.sin6.sin6_port = port;
1059 addr_len = sizeof(addr->u.sin6);
1060 } else {
1061 addr->u.sin.sin_port = port;
1062 addr_len = sizeof(addr->u.sin);
1063 }
1064
1065 return sendto(fd, buf, len, 0, &addr->u.sa, addr_len);
1066}
1067
1068/*! send RTP dummy packet (to keep NAT connection open).
1069 * \param[in] endp mcgp endpoint that holds the RTP connection.
1070 * \param[in] conn associated RTP connection.
1071 * \returns bytes sent, -1 on error. */
1072int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
1073{
1074 int rc;
1075 int was_rtcp = 0;
1076
1077 OSMO_ASSERT(endp);
1078 OSMO_ASSERT(conn);
1079
1080 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG, "sending dummy packet... %s\n",
1081 mgcp_conn_dump(conn->conn));
1082
Pau Espin Pedrola24dcc62021-07-06 17:48:47 +02001083 /* Before we try to deliver the packet, we check if the destination
1084 * port and IP-Address make sense at all. If not, we will be unable
1085 * to deliver the packet. */
1086 if (check_rtp_destin(conn) != 0)
1087 goto failed;
1088
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001089 if (mgcp_conn_rtp_is_iuup(conn))
1090 rc = mgcp_conn_iuup_send_dummy(conn);
1091 else
1092 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr, conn->end.rtp_port,
1093 rtp_dummy_payload, sizeof(rtp_dummy_payload));
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001094
1095 if (rc == -1)
1096 goto failed;
1097
1098 if (endp->trunk->omit_rtcp)
1099 return rc;
1100
1101 was_rtcp = 1;
1102 rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
1103 conn->end.rtcp_port, rtp_dummy_payload, sizeof(rtp_dummy_payload));
1104
1105 if (rc >= 0)
1106 return rc;
1107
1108failed:
1109 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1110 "Failed to send dummy %s packet.\n",
1111 was_rtcp ? "RTCP" : "RTP");
1112
1113 return -1;
1114}
1115
1116/*! Send RTP/RTCP data to a specified destination connection.
1117 * \param[in] endp associated endpoint (for configuration, logging).
1118 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP.
1119 * \param[in] spoofed source address (set to NULL to disable).
1120 * \param[in] buf buffer that contains the RTP/RTCP data.
1121 * \param[in] len length of the buffer that contains the RTP/RTCP data.
1122 * \param[in] conn_src associated source connection.
1123 * \param[in] conn_dst associated destination connection.
1124 * \returns 0 on success, -1 on ERROR. */
1125int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct osmo_sockaddr *addr,
1126 struct msgb *msg, struct mgcp_conn_rtp *conn_src,
1127 struct mgcp_conn_rtp *conn_dst)
1128{
1129 /*! When no destination connection is available (e.g. when only one
1130 * connection in loopback mode exists), then the source connection
1131 * shall be specified as destination connection */
1132
1133 struct mgcp_trunk *trunk = endp->trunk;
1134 struct mgcp_rtp_end *rtp_end;
1135 struct mgcp_rtp_state *rtp_state;
1136 char ipbuf[INET6_ADDRSTRLEN];
1137 char *dest_name;
1138 int rc;
1139 int len;
1140
1141 OSMO_ASSERT(conn_src);
1142 OSMO_ASSERT(conn_dst);
1143
1144 if (is_rtp)
1145 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTP packet...\n");
1146 else
1147 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTCP packet...\n");
1148
1149 /* FIXME: It is legal that the payload type on the egress connection is
1150 * different from the payload type that has been negotiated on the
1151 * ingress connection. Essentially the codecs are the same so we can
1152 * match them and patch the payload type. However, if we can not find
1153 * the codec pendant (everything ist equal except the PT), we are of
1154 * course unable to patch the payload type. A situation like this
1155 * should not occur if transcoding is consequently avoided. Until
1156 * we have transcoding support in osmo-mgw we can not resolve this. */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001157 if (is_rtp && conn_dst->type != MGCP_RTP_IUUP) {
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001158 rc = mgcp_patch_pt(conn_src, conn_dst, msg);
1159 if (rc < 0) {
1160 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1161 "can not patch PT because no suitable egress codec was found.\n");
1162 }
1163 }
1164
1165 /* Note: In case of loopback configuration, both, the source and the
1166 * destination will point to the same connection. */
1167 rtp_end = &conn_dst->end;
1168 rtp_state = &conn_src->state;
1169 dest_name = conn_dst->conn->name;
1170
1171 /* Ensure we have an alternative SSRC in case we need it, see also
1172 * gen_rtp_header() */
1173 if (rtp_state->alt_rtp_tx_ssrc == 0)
1174 rtp_state->alt_rtp_tx_ssrc = rand();
1175
1176 if (!rtp_end->output_enabled) {
1177 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_DROPPED_PACKETS_CTR);
1178 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1179 "output disabled, drop to %s %s "
1180 "rtp_port:%u rtcp_port:%u\n",
1181 dest_name,
1182 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
1183 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
1184 );
1185 } else if (is_rtp) {
1186 int cont;
1187 int nbytes = 0;
1188 int buflen = msgb_length(msg);
1189
1190 /* Make sure we have a valid RTP header, in cases where no RTP
1191 * header is present, we will generate one. */
1192 gen_rtp_header(msg, rtp_end, rtp_state);
1193
1194 do {
1195 /* Run transcoder */
Ericfbf78d12021-08-23 22:31:39 +02001196 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 +02001197 if (cont < 0)
1198 break;
1199
1200 if (addr)
1201 mgcp_patch_and_count(endp, rtp_state, rtp_end,
1202 addr, msg);
1203
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001204 if (mgcp_conn_rtp_is_iuup(conn_dst) || mgcp_conn_rtp_is_iuup(conn_src)) {
1205 /* the iuup code will correctly transform to the correct AMR mode */
1206 } else if (amr_oa_bwe_convert_indicated(conn_dst->end.codec)) {
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001207 rc = amr_oa_bwe_convert(endp, msg,
1208 conn_dst->end.codec->param.amr_octet_aligned);
1209 if (rc < 0) {
1210 LOGPENDP(endp, DRTP, LOGL_ERROR,
1211 "Error in AMR octet-aligned <-> bandwidth-efficient mode conversion\n");
1212 break;
1213 }
1214 } else if (rtp_end->rfc5993_hr_convert &&
1215 strcmp(conn_src->end.codec->subtype_name, "GSM-HR-08") == 0) {
1216 rc = rfc5993_hr_convert(endp, msg);
1217 if (rc < 0) {
1218 LOGPENDP(endp, DRTP, LOGL_ERROR, "Error while converting to GSM-HR-08\n");
1219 break;
1220 }
1221 }
1222
1223 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1224 "process/send to %s %s "
1225 "rtp_port:%u rtcp_port:%u\n",
1226 dest_name,
1227 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
1228 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
1229 );
1230
1231 /* Forward a copy of the RTP data to a debug ip/port */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001232 forward_data_tap(rtp_end->rtp.fd, &conn_src->tap_out,
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001233 msg);
1234
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001235 len = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr, rtp_end->rtp_port,
1236 (char *)msgb_data(msg), msgb_length(msg));
1237
1238 if (len <= 0)
1239 return len;
1240
1241 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
1242 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
1243 rtp_state->alt_rtp_tx_sequence++;
1244
1245 nbytes += len;
1246 buflen = cont;
1247 } while (buflen > 0);
1248 return nbytes;
1249 } else if (!trunk->omit_rtcp) {
1250 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1251 "send to %s %s rtp_port:%u rtcp_port:%u\n",
1252 dest_name, osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
1253 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
1254 );
1255
1256 len = mgcp_udp_send(rtp_end->rtcp.fd,
1257 &rtp_end->addr,
1258 rtp_end->rtcp_port, (char *)msgb_data(msg), msgb_length(msg));
1259
1260 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
1261 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
1262 rtp_state->alt_rtp_tx_sequence++;
1263
1264 return len;
1265 }
1266
1267 return 0;
1268}
1269
Philipp Maier87bd9be2017-08-22 16:35:41 +02001270/*! dispatch incoming RTP packet to opposite RTP connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001271 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1272 * \param[in] addr socket address where the RTP packet has been received from.
1273 * \param[in] buf buffer that hold the RTP payload.
1274 * \param[in] buf_size size data length of buf.
1275 * \param[in] conn originating connection.
1276 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001277int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001278{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001279 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1280 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1281 struct mgcp_conn *conn = conn_src->conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001282 struct mgcp_conn *conn_dst;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001283 struct osmo_sockaddr *from_addr = mc->from_addr;
Philipp Maier97a93122021-05-07 22:50:31 +02001284 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001285
1286 /*! NOTE: This callback function implements the endpoint specific
Alexander Chemeris61cf9bb2020-05-11 18:13:53 +03001287 * dispatch behaviour of an rtp bridge/proxy endpoint. It is assumed
Philipp Maier87bd9be2017-08-22 16:35:41 +02001288 * that the endpoint will hold only two connections. This premise
1289 * is used to determine the opposite connection (it is always the
1290 * connection that is not the originating connection). Once the
1291 * destination connection is known the RTP packet is sent via
1292 * the destination connection. */
1293
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001294 /* If source is IuUP, we need to handle state, forward it through specific bridge path: */
1295 if (mgcp_conn_rtp_is_iuup(conn_src) && mc->proto == MGCP_PROTO_RTP)
1296 return mgcp_conn_iuup_dispatch_rtp(msg);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001297
1298 /* Check if the connection is in loopback mode, if yes, just send the
1299 * incoming data back to the origin */
1300 if (conn->mode == MGCP_CONN_LOOPBACK) {
1301 /* When we are in loopback mode, we loop back all incoming
1302 * packets back to their origin. We will use the originating
1303 * address data from the UDP packet header to patch the
1304 * outgoing address in connection on the fly */
1305 if (conn->u.rtp.end.rtp_port == 0) {
Philipp Maier97a93122021-05-07 22:50:31 +02001306 memcpy(&conn->u.rtp.end.addr, from_addr,
1307 sizeof(conn->u.rtp.end.addr));
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001308 switch (from_addr->u.sa.sa_family) {
1309 case AF_INET:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001310 conn->u.rtp.end.rtp_port = from_addr->u.sin.sin_port;
1311 break;
1312 case AF_INET6:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001313 conn->u.rtp.end.rtp_port = from_addr->u.sin6.sin6_port;
1314 break;
1315 default:
1316 OSMO_ASSERT(false);
1317 }
Philipp Maier97a93122021-05-07 22:50:31 +02001318 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1319 "loopback mode: implicitly using source address (%s:%u) as destination address\n",
1320 osmo_sockaddr_ntop(&from_addr->u.sa, ipbuf),
1321 conn->u.rtp.end.rtp_port);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001322 }
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001323 return mgcp_send_rtp(conn_src, msg);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001324 }
1325
Philipp Maier87bd9be2017-08-22 16:35:41 +02001326 /* Find a destination connection. */
1327 /* NOTE: This code path runs every time an RTP packet is received. The
1328 * function mgcp_find_dst_conn() we use to determine the detination
1329 * connection will iterate the connection list inside the endpoint.
1330 * Since list iterations are quite costly, we will figure out the
1331 * destination only once and use the optional private data pointer of
1332 * the connection to cache the destination connection pointer. */
1333 if (!conn->priv) {
1334 conn_dst = mgcp_find_dst_conn(conn);
1335 conn->priv = conn_dst;
1336 } else {
1337 conn_dst = (struct mgcp_conn *)conn->priv;
1338 }
1339
1340 /* There is no destination conn, stop here */
1341 if (!conn_dst) {
Alexander Chemerisebb9bf32020-05-11 18:14:31 +03001342 LOGPCONN(conn, DRTP, LOGL_DEBUG,
1343 "no connection to forward an incoming RTP packet to\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001344 return -1;
1345 }
1346
1347 /* The destination conn is not an RTP connection */
1348 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001349 LOGPCONN(conn, DRTP, LOGL_ERROR,
1350 "unable to find suitable destination conn\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001351 return -1;
1352 }
1353
1354 /* Dispatch RTP packet to destination RTP connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001355 return mgcp_send_rtp(&conn_dst->u.rtp, msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001356}
1357
Philipp Maier0996a1e2020-06-10 15:27:14 +02001358/*! dispatch incoming RTP packet to E1 subslot, handle RTCP packets locally.
1359 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1360 * \param[in] addr socket address where the RTP packet has been received from.
1361 * \param[in] buf buffer that hold the RTP payload.
1362 * \param[in] buf_size size data length of buf.
1363 * \param[in] conn originating connection.
1364 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001365int mgcp_dispatch_e1_bridge_cb(struct msgb *msg)
Philipp Maier0996a1e2020-06-10 15:27:14 +02001366{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001367 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1368 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1369 struct mgcp_conn *conn = conn_src->conn;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001370 struct osmo_sockaddr *from_addr = mc->from_addr;
Philipp Maier97a93122021-05-07 22:50:31 +02001371 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001372
Philipp Maier889fe7f2020-07-06 17:44:12 +02001373 /* Check if the connection is in loopback mode, if yes, just send the
1374 * incoming data back to the origin */
1375 if (conn->mode == MGCP_CONN_LOOPBACK) {
1376 /* When we are in loopback mode, we loop back all incoming
1377 * packets back to their origin. We will use the originating
1378 * address data from the UDP packet header to patch the
1379 * outgoing address in connection on the fly */
1380 if (conn->u.rtp.end.rtp_port == 0) {
Philipp Maier97a93122021-05-07 22:50:31 +02001381 memcpy(&conn->u.rtp.end.addr, from_addr,
1382 sizeof(conn->u.rtp.end.addr));
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001383 switch (from_addr->u.sa.sa_family) {
1384 case AF_INET:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001385 conn->u.rtp.end.rtp_port = from_addr->u.sin.sin_port;
1386 break;
1387 case AF_INET6:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001388 conn->u.rtp.end.rtp_port = from_addr->u.sin6.sin6_port;
1389 break;
1390 default:
1391 OSMO_ASSERT(false);
1392 }
Philipp Maier97a93122021-05-07 22:50:31 +02001393 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1394 "loopback mode: implicitly using source address (%s:%u) as destination address\n",
1395 osmo_sockaddr_ntop(&from_addr->u.sa, ipbuf),
1396 conn->u.rtp.end.rtp_port);
Philipp Maier889fe7f2020-07-06 17:44:12 +02001397 }
1398 return mgcp_send_rtp(conn_src, msg);
1399 }
1400
1401 /* Forward to E1 */
1402 return mgcp_e1_send_rtp(conn->endp, conn->u.rtp.end.codec, msg);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001403}
1404
Philipp Maierdf5d2192018-01-24 11:39:32 +01001405/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1406 * \param[in] endp Endpoint on which the connection resides.
Philipp Maier08eb9352020-06-18 11:55:35 +02001407 * \param[in] conn Connection that is about to be removed (ignored). */
Philipp Maierdf5d2192018-01-24 11:39:32 +01001408void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1409{
1410 struct mgcp_conn *conn_cleanup;
1411
1412 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1413 * pointer to the destination connection, so that we do not have
1414 * to go through the list every time an RTP packet arrives. To prevent
1415 * a use-after-free situation we invalidate this information for all
1416 * connections present when one connection is removed from the
1417 * endpoint. */
1418 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
Pau Espin Pedrol6d0a59a2020-09-08 16:50:22 +02001419 if (conn_cleanup->priv == conn)
1420 conn_cleanup->priv = NULL;
Philipp Maierdf5d2192018-01-24 11:39:32 +01001421 }
1422}
1423
Philipp Maier0996a1e2020-06-10 15:27:14 +02001424/*! cleanup an endpoint when a connection on an E1 endpoint is removed.
1425 * \param[in] endp Endpoint on which the connection resides.
1426 * \param[in] conn Connection that is about to be removed (ignored). */
1427void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1428{
Philipp Maier889fe7f2020-07-06 17:44:12 +02001429 /* Cleanup tasks for E1 are the same as for regular endpoint. The
1430 * shut down of the E1 part is handled separately. */
1431 mgcp_cleanup_rtp_bridge_cb(endp, conn);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001432}
1433
Philipp Maier87bd9be2017-08-22 16:35:41 +02001434/* Handle incoming RTP data from NET */
1435static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1436{
1437 /* NOTE: This is a generic implementation. RTP data is received. In
1438 * case of loopback the data is just sent back to its origin. All
1439 * other cases implement endpoint specific behaviour (e.g. how is the
1440 * destination connection determined?). That specific behaviour is
1441 * implemented by the callback function that is called at the end of
1442 * the function */
1443
1444 struct mgcp_conn_rtp *conn_src;
1445 struct mgcp_endpoint *endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001446 struct osmo_sockaddr addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001447 socklen_t slen = sizeof(addr);
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001448 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001449 int ret;
1450 enum rtp_proto proto;
1451 struct osmo_rtp_msg_ctx *mc;
Eric8f333032021-08-13 00:00:43 +02001452 struct msgb *msg;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001453 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001454
1455 conn_src = (struct mgcp_conn_rtp *)fd->data;
1456 OSMO_ASSERT(conn_src);
1457 endp = conn_src->conn->endp;
1458 OSMO_ASSERT(endp);
Eric8f333032021-08-13 00:00:43 +02001459 msg = msgb_alloc_c(endp->trunk, RTP_BUF_SIZE, "RTP-rx");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001460
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001461 proto = (fd == &conn_src->end.rtp)? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001462
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001463 ret = recvfrom(fd->fd, msgb_data(msg), msg->data_len, 0, (struct sockaddr *)&addr.u.sa, &slen);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001464
1465 if (ret <= 0) {
1466 LOG_CONN_RTP(conn_src, LOGL_ERROR, "recvfrom error: %s\n", strerror(errno));
1467 rc = -1;
1468 goto out;
1469 }
1470
1471 msgb_put(msg, ret);
1472
1473 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "%s: rx %u bytes from %s:%u\n",
1474 proto == MGCP_PROTO_RTP ? "RTP" : "RTPC",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001475 msgb_length(msg), osmo_sockaddr_ntop(&addr.u.sa, ipbuf),
1476 osmo_sockaddr_port(&addr.u.sa));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001477
1478 if ((proto == MGCP_PROTO_RTP && check_rtp(conn_src, msg))
1479 || (proto == MGCP_PROTO_RTCP && check_rtcp(conn_src, msg))) {
1480 /* Logging happened in the two check_ functions */
1481 rc = -1;
1482 goto out;
1483 }
1484
Philipp Maierb3d14eb2021-05-20 14:18:52 +02001485 if (mgcp_is_rtp_dummy_payload(msg)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001486 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx dummy packet (dropped)\n");
1487 rc = 0;
1488 goto out;
1489 }
1490
1491 /* Since the msgb remains owned and freed by this function, the msg ctx data struct can just be on the stack and
1492 * needs not be allocated with the msgb. */
1493 mc = OSMO_RTP_MSG_CTX(msg);
1494 *mc = (struct osmo_rtp_msg_ctx){
1495 .proto = proto,
1496 .conn_src = conn_src,
1497 .from_addr = &addr,
1498 };
1499 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "msg ctx: %d %p %s\n",
1500 mc->proto, mc->conn_src,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001501 osmo_hexdump((void*)mc->from_addr,
1502 mc->from_addr->u.sa.sa_family == AF_INET6 ?
1503 sizeof(struct sockaddr_in6) :
1504 sizeof(struct sockaddr_in)));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001505
1506 /* Increment RX statistics */
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001507 rate_ctr_inc(rate_ctr_group_get_ctr(conn_src->rate_ctr_group, RTP_PACKETS_RX_CTR));
1508 rate_ctr_add(rate_ctr_group_get_ctr(conn_src->rate_ctr_group, RTP_OCTETS_RX_CTR), msgb_length(msg));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001509 /* FIXME: count RTP and RTCP separately, also count IuUP payload-less separately */
1510
1511 /* Forward a copy of the RTP data to a debug ip/port */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001512 forward_data_tap(fd->fd, &conn_src->tap_in, msg);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001513
1514 rc = rx_rtp(msg);
1515
1516out:
1517 msgb_free(msg);
1518 return rc;
1519}
1520
Philipp Maiere1442752022-03-30 16:06:15 +02001521/* Note: This function is able to handle RTP and RTCP */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001522static int rx_rtp(struct msgb *msg)
1523{
1524 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1525 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001526 struct osmo_sockaddr *from_addr = mc->from_addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001527 struct mgcp_conn *conn = conn_src->conn;
1528 struct mgcp_trunk *trunk = conn->endp->trunk;
1529
1530 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx_rtp(%u bytes)\n", msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001531
Oliver Smithe36b7752019-01-22 16:31:36 +01001532 mgcp_conn_watchdog_kick(conn_src->conn);
1533
Philipp Maier228e5912019-03-05 13:56:59 +01001534 /* If AMR is configured for the ingress connection a conversion of the
1535 * framing mode (octet-aligned vs. bandwith-efficient is explicitly
1536 * define, then we check if the incoming payload matches that
1537 * expectation. */
Philipp Maiere1442752022-03-30 16:06:15 +02001538 if (mc->proto == MGCP_PROTO_RTP &&
1539 amr_oa_bwe_convert_indicated(conn_src->end.codec)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001540 int oa = amr_oa_check((char*)msgb_data(msg), msgb_length(msg));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +02001541 if (oa < 0)
1542 return -1;
1543 if (((bool)oa) != conn_src->end.codec->param.amr_octet_aligned)
Philipp Maier228e5912019-03-05 13:56:59 +01001544 return -1;
1545 }
1546
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001547 /* Check if the origin of the RTP packet seems plausible */
1548 if (!trunk->rtp_accept_all && check_rtp_origin(conn_src, from_addr))
1549 return -1;
1550
Philipp Maier87bd9be2017-08-22 16:35:41 +02001551 /* Execute endpoint specific implementation that handles the
1552 * dispatching of the RTP data */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001553 return conn->endp->type->dispatch_rtp_cb(msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001554}
1555
Philipp Maier87bd9be2017-08-22 16:35:41 +02001556/*! bind RTP port to osmo_fd.
Philipp Maier0b79d212020-06-18 12:02:49 +02001557 * \param[in] source_addr source (local) address to bind on.
1558 * \param[in] fd associated file descriptor.
1559 * \param[in] port to bind on.
Harald Welte55a92292021-04-28 19:06:34 +02001560 * \param[in] dscp IP DSCP value to use.
1561 * \param[in] prio socket priority to use.
Philipp Maier0b79d212020-06-18 12:02:49 +02001562 * \returns 0 on success, -1 on ERROR. */
Harald Welte55a92292021-04-28 19:06:34 +02001563int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port, uint8_t dscp,
1564 uint8_t prio)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001565{
Harald Welte8890dfa2017-11-17 15:09:30 +01001566 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001567
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001568 rc = osmo_sock_init2(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
Harald Welte55a92292021-04-28 19:06:34 +02001569 NULL, 0, OSMO_SOCK_F_BIND | OSMO_SOCK_F_DSCP(dscp) |
1570 OSMO_SOCK_F_PRIO(prio));
Harald Welte8890dfa2017-11-17 15:09:30 +01001571 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001572 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001573 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001574 return -1;
1575 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001576 fd->fd = rc;
1577 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001578
1579 return 0;
1580}
1581
Philipp Maier87bd9be2017-08-22 16:35:41 +02001582/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001583static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001584 struct mgcp_rtp_end *rtp_end, struct mgcp_endpoint *endp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001585{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001586 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1587 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1588
Harald Welte55a92292021-04-28 19:06:34 +02001589 if (mgcp_create_bind(source_addr, &rtp_end->rtp, rtp_end->local_port,
1590 cfg->endp_dscp, cfg->endp_priority) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001591 LOGPENDP(endp, DRTP, LOGL_ERROR,
1592 "failed to create RTP port: %s:%d\n",
1593 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001594 goto cleanup0;
1595 }
1596
Harald Welte55a92292021-04-28 19:06:34 +02001597 if (mgcp_create_bind(source_addr, &rtp_end->rtcp, rtp_end->local_port + 1,
1598 cfg->endp_dscp, cfg->endp_priority) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001599 LOGPENDP(endp, DRTP, LOGL_ERROR,
1600 "failed to create RTCP port: %s:%d\n",
1601 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001602 goto cleanup1;
1603 }
1604
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001605 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001606 LOGPENDP(endp, DRTP, LOGL_ERROR,
1607 "failed to register RTP port %d\n",
1608 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001609 goto cleanup2;
1610 }
1611
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001612 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001613 LOGPENDP(endp, DRTP, LOGL_ERROR,
1614 "failed to register RTCP port %d\n",
1615 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001616 goto cleanup3;
1617 }
1618
1619 return 0;
1620
1621cleanup3:
1622 osmo_fd_unregister(&rtp_end->rtp);
1623cleanup2:
1624 close(rtp_end->rtcp.fd);
1625 rtp_end->rtcp.fd = -1;
1626cleanup1:
1627 close(rtp_end->rtp.fd);
1628 rtp_end->rtp.fd = -1;
1629cleanup0:
1630 return -1;
1631}
1632
Philipp Maier87bd9be2017-08-22 16:35:41 +02001633/*! bind RTP port to endpoint/connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001634 * \param[in] endp endpoint that holds the RTP connection.
1635 * \param[in] rtp_port port number to bind on.
1636 * \param[in] conn associated RTP connection.
1637 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001638int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1639 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001640{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001641 char name[512];
1642 struct mgcp_rtp_end *end;
1643
Philipp Maier01d24a32017-11-21 17:26:09 +01001644 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001645 end = &conn->end;
1646
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001647 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001648 LOGPENDP(endp, DRTP, LOGL_ERROR, "%u was already bound on conn:%s\n",
1649 rtp_port, mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001650
1651 /* Double bindings should never occour! Since we always allocate
1652 * connections dynamically and free them when they are not
1653 * needed anymore, there must be no previous binding leftover.
1654 * Should there be a connection bound twice, we have a serious
1655 * problem and must exit immediately! */
1656 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001657 }
1658
1659 end->local_port = rtp_port;
Harald Weltec2a8f592020-10-19 13:25:41 +02001660 osmo_fd_setup(&end->rtp, -1, OSMO_FD_READ, rtp_data_net, conn, 0);
1661 osmo_fd_setup(&end->rtcp, -1, OSMO_FD_READ, rtp_data_net, conn, 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001662
Ericfbf78d12021-08-23 22:31:39 +02001663 return bind_rtp(endp->trunk->cfg, conn->end.local_addr, end, endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001664}
1665
Philipp Maier87bd9be2017-08-22 16:35:41 +02001666/*! free allocated RTP and RTCP ports.
1667 * \param[in] end RTP end */
1668void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001669{
1670 if (end->rtp.fd != -1) {
1671 close(end->rtp.fd);
1672 end->rtp.fd = -1;
1673 osmo_fd_unregister(&end->rtp);
1674 }
1675
1676 if (end->rtcp.fd != -1) {
1677 close(end->rtcp.fd);
1678 end->rtcp.fd = -1;
1679 osmo_fd_unregister(&end->rtcp);
1680 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001681}