blob: 2d275ec8d3da4000e0cc2d5d41ef89eae539c1e2 [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>
Philipp Maier6931f9a2018-07-26 09:29:31 +020051
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
Harald Weltea48ff4a2020-03-08 14:45:08 +010062static void rtpconn_rate_ctr_add(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp,
63 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. */
159static uint32_t 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);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200532 arrival_time = 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 */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200792static void forward_data(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;
818 hdr->timestamp = osmo_htonl(get_current_ts(rtp_end->codec->rate));
819 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
978 * below */
979
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);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001016 }
1017
Philipp Maier87bd9be2017-08-22 16:35:41 +02001018 /* If the data has not been handled/forwarded until here, it will
1019 * be discarded, this should not happen, normally the MGCP type
1020 * should be properly set */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001021 LOGPENDP(endp, DRTP, LOGL_ERROR, "bad MGCP type -- data discarded!\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001022
1023 return -1;
1024}
1025
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001026/*! send udp packet.
1027 * \param[in] fd associated file descriptor.
1028 * \param[in] addr destination ip-address.
1029 * \param[in] port destination UDP port (network byte order).
1030 * \param[in] buf buffer that holds the data to be send.
1031 * \param[in] len length of the data to be sent.
1032 * \returns bytes sent, -1 on error. */
1033int mgcp_udp_send(int fd, struct osmo_sockaddr *addr, int port, const char *buf, int len)
1034{
1035 char ipbuf[INET6_ADDRSTRLEN];
1036 size_t addr_len;
1037 bool is_ipv6 = addr->u.sa.sa_family == AF_INET6;
1038
1039 LOGP(DRTP, LOGL_DEBUG,
1040 "sending %i bytes length packet to %s:%u ...\n", len,
1041 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
1042 ntohs(port));
1043
1044 if (is_ipv6) {
1045 addr->u.sin6.sin6_port = port;
1046 addr_len = sizeof(addr->u.sin6);
1047 } else {
1048 addr->u.sin.sin_port = port;
1049 addr_len = sizeof(addr->u.sin);
1050 }
1051
1052 return sendto(fd, buf, len, 0, &addr->u.sa, addr_len);
1053}
1054
1055/*! send RTP dummy packet (to keep NAT connection open).
1056 * \param[in] endp mcgp endpoint that holds the RTP connection.
1057 * \param[in] conn associated RTP connection.
1058 * \returns bytes sent, -1 on error. */
1059int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
1060{
1061 int rc;
1062 int was_rtcp = 0;
1063
1064 OSMO_ASSERT(endp);
1065 OSMO_ASSERT(conn);
1066
1067 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG, "sending dummy packet... %s\n",
1068 mgcp_conn_dump(conn->conn));
1069
Pau Espin Pedrola24dcc62021-07-06 17:48:47 +02001070 /* Before we try to deliver the packet, we check if the destination
1071 * port and IP-Address make sense at all. If not, we will be unable
1072 * to deliver the packet. */
1073 if (check_rtp_destin(conn) != 0)
1074 goto failed;
1075
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001076 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
1077 conn->end.rtp_port, rtp_dummy_payload, sizeof(rtp_dummy_payload));
1078
1079 if (rc == -1)
1080 goto failed;
1081
1082 if (endp->trunk->omit_rtcp)
1083 return rc;
1084
1085 was_rtcp = 1;
1086 rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
1087 conn->end.rtcp_port, rtp_dummy_payload, sizeof(rtp_dummy_payload));
1088
1089 if (rc >= 0)
1090 return rc;
1091
1092failed:
1093 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1094 "Failed to send dummy %s packet.\n",
1095 was_rtcp ? "RTCP" : "RTP");
1096
1097 return -1;
1098}
1099
1100/*! Send RTP/RTCP data to a specified destination connection.
1101 * \param[in] endp associated endpoint (for configuration, logging).
1102 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP.
1103 * \param[in] spoofed source address (set to NULL to disable).
1104 * \param[in] buf buffer that contains the RTP/RTCP data.
1105 * \param[in] len length of the buffer that contains the RTP/RTCP data.
1106 * \param[in] conn_src associated source connection.
1107 * \param[in] conn_dst associated destination connection.
1108 * \returns 0 on success, -1 on ERROR. */
1109int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct osmo_sockaddr *addr,
1110 struct msgb *msg, struct mgcp_conn_rtp *conn_src,
1111 struct mgcp_conn_rtp *conn_dst)
1112{
1113 /*! When no destination connection is available (e.g. when only one
1114 * connection in loopback mode exists), then the source connection
1115 * shall be specified as destination connection */
1116
1117 struct mgcp_trunk *trunk = endp->trunk;
1118 struct mgcp_rtp_end *rtp_end;
1119 struct mgcp_rtp_state *rtp_state;
1120 char ipbuf[INET6_ADDRSTRLEN];
1121 char *dest_name;
1122 int rc;
1123 int len;
1124
1125 OSMO_ASSERT(conn_src);
1126 OSMO_ASSERT(conn_dst);
1127
1128 if (is_rtp)
1129 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTP packet...\n");
1130 else
1131 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTCP packet...\n");
1132
1133 /* FIXME: It is legal that the payload type on the egress connection is
1134 * different from the payload type that has been negotiated on the
1135 * ingress connection. Essentially the codecs are the same so we can
1136 * match them and patch the payload type. However, if we can not find
1137 * the codec pendant (everything ist equal except the PT), we are of
1138 * course unable to patch the payload type. A situation like this
1139 * should not occur if transcoding is consequently avoided. Until
1140 * we have transcoding support in osmo-mgw we can not resolve this. */
1141 if (is_rtp) {
1142 rc = mgcp_patch_pt(conn_src, conn_dst, msg);
1143 if (rc < 0) {
1144 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1145 "can not patch PT because no suitable egress codec was found.\n");
1146 }
1147 }
1148
1149 /* Note: In case of loopback configuration, both, the source and the
1150 * destination will point to the same connection. */
1151 rtp_end = &conn_dst->end;
1152 rtp_state = &conn_src->state;
1153 dest_name = conn_dst->conn->name;
1154
1155 /* Ensure we have an alternative SSRC in case we need it, see also
1156 * gen_rtp_header() */
1157 if (rtp_state->alt_rtp_tx_ssrc == 0)
1158 rtp_state->alt_rtp_tx_ssrc = rand();
1159
1160 if (!rtp_end->output_enabled) {
1161 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_DROPPED_PACKETS_CTR);
1162 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1163 "output disabled, drop to %s %s "
1164 "rtp_port:%u rtcp_port:%u\n",
1165 dest_name,
1166 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
1167 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
1168 );
1169 } else if (is_rtp) {
1170 int cont;
1171 int nbytes = 0;
1172 int buflen = msgb_length(msg);
1173
1174 /* Make sure we have a valid RTP header, in cases where no RTP
1175 * header is present, we will generate one. */
1176 gen_rtp_header(msg, rtp_end, rtp_state);
1177
1178 do {
1179 /* Run transcoder */
Ericfbf78d12021-08-23 22:31:39 +02001180 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 +02001181 if (cont < 0)
1182 break;
1183
1184 if (addr)
1185 mgcp_patch_and_count(endp, rtp_state, rtp_end,
1186 addr, msg);
1187
1188 if (amr_oa_bwe_convert_indicated(conn_dst->end.codec)) {
1189 rc = amr_oa_bwe_convert(endp, msg,
1190 conn_dst->end.codec->param.amr_octet_aligned);
1191 if (rc < 0) {
1192 LOGPENDP(endp, DRTP, LOGL_ERROR,
1193 "Error in AMR octet-aligned <-> bandwidth-efficient mode conversion\n");
1194 break;
1195 }
1196 } else if (rtp_end->rfc5993_hr_convert &&
1197 strcmp(conn_src->end.codec->subtype_name, "GSM-HR-08") == 0) {
1198 rc = rfc5993_hr_convert(endp, msg);
1199 if (rc < 0) {
1200 LOGPENDP(endp, DRTP, LOGL_ERROR, "Error while converting to GSM-HR-08\n");
1201 break;
1202 }
1203 }
1204
1205 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1206 "process/send to %s %s "
1207 "rtp_port:%u rtcp_port:%u\n",
1208 dest_name,
1209 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
1210 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
1211 );
1212
1213 /* Forward a copy of the RTP data to a debug ip/port */
1214 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
1215 msg);
1216
1217 /* FIXME: HACK HACK HACK. See OS#2459.
1218 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
1219 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
1220 * cells (as long as we patch only the first RTP payload in each stream).
1221 */
1222 if (!rtp_state->patched_first_rtp_payload
1223 && conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
1224 uint8_t *data = msgb_data(msg) + 12;
1225 if (data[0] == 0xe0) {
1226 data[0] = 0xe4;
1227 data[1] = 0x00;
1228 rtp_state->patched_first_rtp_payload = true;
1229 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1230 "Patching over first two bytes"
1231 " to fake an IuUP Initialization Ack\n");
1232 }
1233 }
1234
1235 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 Pedrol9aaaab62019-05-15 23:23:27 +02001294
1295 /* Check if the connection is in loopback mode, if yes, just send the
1296 * incoming data back to the origin */
1297 if (conn->mode == MGCP_CONN_LOOPBACK) {
1298 /* When we are in loopback mode, we loop back all incoming
1299 * packets back to their origin. We will use the originating
1300 * address data from the UDP packet header to patch the
1301 * outgoing address in connection on the fly */
1302 if (conn->u.rtp.end.rtp_port == 0) {
Philipp Maier97a93122021-05-07 22:50:31 +02001303 memcpy(&conn->u.rtp.end.addr, from_addr,
1304 sizeof(conn->u.rtp.end.addr));
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001305 switch (from_addr->u.sa.sa_family) {
1306 case AF_INET:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001307 conn->u.rtp.end.rtp_port = from_addr->u.sin.sin_port;
1308 break;
1309 case AF_INET6:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001310 conn->u.rtp.end.rtp_port = from_addr->u.sin6.sin6_port;
1311 break;
1312 default:
1313 OSMO_ASSERT(false);
1314 }
Philipp Maier97a93122021-05-07 22:50:31 +02001315 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1316 "loopback mode: implicitly using source address (%s:%u) as destination address\n",
1317 osmo_sockaddr_ntop(&from_addr->u.sa, ipbuf),
1318 conn->u.rtp.end.rtp_port);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001319 }
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001320 return mgcp_send_rtp(conn_src, msg);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001321 }
1322
Philipp Maier87bd9be2017-08-22 16:35:41 +02001323 /* Find a destination connection. */
1324 /* NOTE: This code path runs every time an RTP packet is received. The
1325 * function mgcp_find_dst_conn() we use to determine the detination
1326 * connection will iterate the connection list inside the endpoint.
1327 * Since list iterations are quite costly, we will figure out the
1328 * destination only once and use the optional private data pointer of
1329 * the connection to cache the destination connection pointer. */
1330 if (!conn->priv) {
1331 conn_dst = mgcp_find_dst_conn(conn);
1332 conn->priv = conn_dst;
1333 } else {
1334 conn_dst = (struct mgcp_conn *)conn->priv;
1335 }
1336
1337 /* There is no destination conn, stop here */
1338 if (!conn_dst) {
Alexander Chemerisebb9bf32020-05-11 18:14:31 +03001339 LOGPCONN(conn, DRTP, LOGL_DEBUG,
1340 "no connection to forward an incoming RTP packet to\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001341 return -1;
1342 }
1343
1344 /* The destination conn is not an RTP connection */
1345 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001346 LOGPCONN(conn, DRTP, LOGL_ERROR,
1347 "unable to find suitable destination conn\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001348 return -1;
1349 }
1350
1351 /* Dispatch RTP packet to destination RTP connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001352 return mgcp_send_rtp(&conn_dst->u.rtp, msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001353}
1354
Philipp Maier0996a1e2020-06-10 15:27:14 +02001355/*! dispatch incoming RTP packet to E1 subslot, handle RTCP packets locally.
1356 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1357 * \param[in] addr socket address where the RTP packet has been received from.
1358 * \param[in] buf buffer that hold the RTP payload.
1359 * \param[in] buf_size size data length of buf.
1360 * \param[in] conn originating connection.
1361 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001362int mgcp_dispatch_e1_bridge_cb(struct msgb *msg)
Philipp Maier0996a1e2020-06-10 15:27:14 +02001363{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001364 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1365 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1366 struct mgcp_conn *conn = conn_src->conn;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001367 struct osmo_sockaddr *from_addr = mc->from_addr;
Philipp Maier97a93122021-05-07 22:50:31 +02001368 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001369
Philipp Maier889fe7f2020-07-06 17:44:12 +02001370 /* Check if the connection is in loopback mode, if yes, just send the
1371 * incoming data back to the origin */
1372 if (conn->mode == MGCP_CONN_LOOPBACK) {
1373 /* When we are in loopback mode, we loop back all incoming
1374 * packets back to their origin. We will use the originating
1375 * address data from the UDP packet header to patch the
1376 * outgoing address in connection on the fly */
1377 if (conn->u.rtp.end.rtp_port == 0) {
Philipp Maier97a93122021-05-07 22:50:31 +02001378 memcpy(&conn->u.rtp.end.addr, from_addr,
1379 sizeof(conn->u.rtp.end.addr));
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001380 switch (from_addr->u.sa.sa_family) {
1381 case AF_INET:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001382 conn->u.rtp.end.rtp_port = from_addr->u.sin.sin_port;
1383 break;
1384 case AF_INET6:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001385 conn->u.rtp.end.rtp_port = from_addr->u.sin6.sin6_port;
1386 break;
1387 default:
1388 OSMO_ASSERT(false);
1389 }
Philipp Maier97a93122021-05-07 22:50:31 +02001390 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1391 "loopback mode: implicitly using source address (%s:%u) as destination address\n",
1392 osmo_sockaddr_ntop(&from_addr->u.sa, ipbuf),
1393 conn->u.rtp.end.rtp_port);
Philipp Maier889fe7f2020-07-06 17:44:12 +02001394 }
1395 return mgcp_send_rtp(conn_src, msg);
1396 }
1397
1398 /* Forward to E1 */
1399 return mgcp_e1_send_rtp(conn->endp, conn->u.rtp.end.codec, msg);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001400}
1401
Philipp Maierdf5d2192018-01-24 11:39:32 +01001402/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1403 * \param[in] endp Endpoint on which the connection resides.
Philipp Maier08eb9352020-06-18 11:55:35 +02001404 * \param[in] conn Connection that is about to be removed (ignored). */
Philipp Maierdf5d2192018-01-24 11:39:32 +01001405void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1406{
1407 struct mgcp_conn *conn_cleanup;
1408
1409 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1410 * pointer to the destination connection, so that we do not have
1411 * to go through the list every time an RTP packet arrives. To prevent
1412 * a use-after-free situation we invalidate this information for all
1413 * connections present when one connection is removed from the
1414 * endpoint. */
1415 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
Pau Espin Pedrol6d0a59a2020-09-08 16:50:22 +02001416 if (conn_cleanup->priv == conn)
1417 conn_cleanup->priv = NULL;
Philipp Maierdf5d2192018-01-24 11:39:32 +01001418 }
1419}
1420
Philipp Maier0996a1e2020-06-10 15:27:14 +02001421/*! cleanup an endpoint when a connection on an E1 endpoint is removed.
1422 * \param[in] endp Endpoint on which the connection resides.
1423 * \param[in] conn Connection that is about to be removed (ignored). */
1424void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1425{
Philipp Maier889fe7f2020-07-06 17:44:12 +02001426 /* Cleanup tasks for E1 are the same as for regular endpoint. The
1427 * shut down of the E1 part is handled separately. */
1428 mgcp_cleanup_rtp_bridge_cb(endp, conn);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001429}
1430
Philipp Maier87bd9be2017-08-22 16:35:41 +02001431/* Handle incoming RTP data from NET */
1432static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1433{
1434 /* NOTE: This is a generic implementation. RTP data is received. In
1435 * case of loopback the data is just sent back to its origin. All
1436 * other cases implement endpoint specific behaviour (e.g. how is the
1437 * destination connection determined?). That specific behaviour is
1438 * implemented by the callback function that is called at the end of
1439 * the function */
1440
1441 struct mgcp_conn_rtp *conn_src;
1442 struct mgcp_endpoint *endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001443 struct osmo_sockaddr addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001444 socklen_t slen = sizeof(addr);
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001445 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001446 int ret;
1447 enum rtp_proto proto;
1448 struct osmo_rtp_msg_ctx *mc;
Eric8f333032021-08-13 00:00:43 +02001449 struct msgb *msg;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001450 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001451
1452 conn_src = (struct mgcp_conn_rtp *)fd->data;
1453 OSMO_ASSERT(conn_src);
1454 endp = conn_src->conn->endp;
1455 OSMO_ASSERT(endp);
Eric8f333032021-08-13 00:00:43 +02001456 msg = msgb_alloc_c(endp->trunk, RTP_BUF_SIZE, "RTP-rx");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001457
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001458 proto = (fd == &conn_src->end.rtp)? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001459
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001460 ret = recvfrom(fd->fd, msgb_data(msg), msg->data_len, 0, (struct sockaddr *)&addr.u.sa, &slen);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001461
1462 if (ret <= 0) {
1463 LOG_CONN_RTP(conn_src, LOGL_ERROR, "recvfrom error: %s\n", strerror(errno));
1464 rc = -1;
1465 goto out;
1466 }
1467
1468 msgb_put(msg, ret);
1469
1470 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "%s: rx %u bytes from %s:%u\n",
1471 proto == MGCP_PROTO_RTP ? "RTP" : "RTPC",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001472 msgb_length(msg), osmo_sockaddr_ntop(&addr.u.sa, ipbuf),
1473 osmo_sockaddr_port(&addr.u.sa));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001474
1475 if ((proto == MGCP_PROTO_RTP && check_rtp(conn_src, msg))
1476 || (proto == MGCP_PROTO_RTCP && check_rtcp(conn_src, msg))) {
1477 /* Logging happened in the two check_ functions */
1478 rc = -1;
1479 goto out;
1480 }
1481
Philipp Maierb3d14eb2021-05-20 14:18:52 +02001482 if (mgcp_is_rtp_dummy_payload(msg)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001483 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx dummy packet (dropped)\n");
1484 rc = 0;
1485 goto out;
1486 }
1487
1488 /* Since the msgb remains owned and freed by this function, the msg ctx data struct can just be on the stack and
1489 * needs not be allocated with the msgb. */
1490 mc = OSMO_RTP_MSG_CTX(msg);
1491 *mc = (struct osmo_rtp_msg_ctx){
1492 .proto = proto,
1493 .conn_src = conn_src,
1494 .from_addr = &addr,
1495 };
1496 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "msg ctx: %d %p %s\n",
1497 mc->proto, mc->conn_src,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001498 osmo_hexdump((void*)mc->from_addr,
1499 mc->from_addr->u.sa.sa_family == AF_INET6 ?
1500 sizeof(struct sockaddr_in6) :
1501 sizeof(struct sockaddr_in)));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001502
1503 /* Increment RX statistics */
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001504 rate_ctr_inc(rate_ctr_group_get_ctr(conn_src->rate_ctr_group, RTP_PACKETS_RX_CTR));
1505 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 +02001506 /* FIXME: count RTP and RTCP separately, also count IuUP payload-less separately */
1507
1508 /* Forward a copy of the RTP data to a debug ip/port */
1509 forward_data(fd->fd, &conn_src->tap_in, msg);
1510
1511 rc = rx_rtp(msg);
1512
1513out:
1514 msgb_free(msg);
1515 return rc;
1516}
1517
1518static int rx_rtp(struct msgb *msg)
1519{
1520 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1521 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001522 struct osmo_sockaddr *from_addr = mc->from_addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001523 struct mgcp_conn *conn = conn_src->conn;
1524 struct mgcp_trunk *trunk = conn->endp->trunk;
1525
1526 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx_rtp(%u bytes)\n", msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001527
Oliver Smithe36b7752019-01-22 16:31:36 +01001528 mgcp_conn_watchdog_kick(conn_src->conn);
1529
Philipp Maier228e5912019-03-05 13:56:59 +01001530 /* If AMR is configured for the ingress connection a conversion of the
1531 * framing mode (octet-aligned vs. bandwith-efficient is explicitly
1532 * define, then we check if the incoming payload matches that
1533 * expectation. */
1534 if (amr_oa_bwe_convert_indicated(conn_src->end.codec)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001535 int oa = amr_oa_check((char*)msgb_data(msg), msgb_length(msg));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +02001536 if (oa < 0)
1537 return -1;
1538 if (((bool)oa) != conn_src->end.codec->param.amr_octet_aligned)
Philipp Maier228e5912019-03-05 13:56:59 +01001539 return -1;
1540 }
1541
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001542 /* Check if the origin of the RTP packet seems plausible */
1543 if (!trunk->rtp_accept_all && check_rtp_origin(conn_src, from_addr))
1544 return -1;
1545
Philipp Maier87bd9be2017-08-22 16:35:41 +02001546 /* Execute endpoint specific implementation that handles the
1547 * dispatching of the RTP data */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001548 return conn->endp->type->dispatch_rtp_cb(msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001549}
1550
Philipp Maier87bd9be2017-08-22 16:35:41 +02001551/*! bind RTP port to osmo_fd.
Philipp Maier0b79d212020-06-18 12:02:49 +02001552 * \param[in] source_addr source (local) address to bind on.
1553 * \param[in] fd associated file descriptor.
1554 * \param[in] port to bind on.
Harald Welte55a92292021-04-28 19:06:34 +02001555 * \param[in] dscp IP DSCP value to use.
1556 * \param[in] prio socket priority to use.
Philipp Maier0b79d212020-06-18 12:02:49 +02001557 * \returns 0 on success, -1 on ERROR. */
Harald Welte55a92292021-04-28 19:06:34 +02001558int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port, uint8_t dscp,
1559 uint8_t prio)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001560{
Harald Welte8890dfa2017-11-17 15:09:30 +01001561 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001562
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001563 rc = osmo_sock_init2(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
Harald Welte55a92292021-04-28 19:06:34 +02001564 NULL, 0, OSMO_SOCK_F_BIND | OSMO_SOCK_F_DSCP(dscp) |
1565 OSMO_SOCK_F_PRIO(prio));
Harald Welte8890dfa2017-11-17 15:09:30 +01001566 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001567 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001568 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001569 return -1;
1570 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001571 fd->fd = rc;
1572 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001573
1574 return 0;
1575}
1576
Philipp Maier87bd9be2017-08-22 16:35:41 +02001577/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001578static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001579 struct mgcp_rtp_end *rtp_end, struct mgcp_endpoint *endp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001580{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001581 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1582 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1583
Harald Welte55a92292021-04-28 19:06:34 +02001584 if (mgcp_create_bind(source_addr, &rtp_end->rtp, rtp_end->local_port,
1585 cfg->endp_dscp, cfg->endp_priority) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001586 LOGPENDP(endp, DRTP, LOGL_ERROR,
1587 "failed to create RTP port: %s:%d\n",
1588 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001589 goto cleanup0;
1590 }
1591
Harald Welte55a92292021-04-28 19:06:34 +02001592 if (mgcp_create_bind(source_addr, &rtp_end->rtcp, rtp_end->local_port + 1,
1593 cfg->endp_dscp, cfg->endp_priority) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001594 LOGPENDP(endp, DRTP, LOGL_ERROR,
1595 "failed to create RTCP port: %s:%d\n",
1596 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001597 goto cleanup1;
1598 }
1599
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001600 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001601 LOGPENDP(endp, DRTP, LOGL_ERROR,
1602 "failed to register RTP port %d\n",
1603 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001604 goto cleanup2;
1605 }
1606
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001607 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001608 LOGPENDP(endp, DRTP, LOGL_ERROR,
1609 "failed to register RTCP port %d\n",
1610 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001611 goto cleanup3;
1612 }
1613
1614 return 0;
1615
1616cleanup3:
1617 osmo_fd_unregister(&rtp_end->rtp);
1618cleanup2:
1619 close(rtp_end->rtcp.fd);
1620 rtp_end->rtcp.fd = -1;
1621cleanup1:
1622 close(rtp_end->rtp.fd);
1623 rtp_end->rtp.fd = -1;
1624cleanup0:
1625 return -1;
1626}
1627
Philipp Maier87bd9be2017-08-22 16:35:41 +02001628/*! bind RTP port to endpoint/connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001629 * \param[in] endp endpoint that holds the RTP connection.
1630 * \param[in] rtp_port port number to bind on.
1631 * \param[in] conn associated RTP connection.
1632 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001633int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1634 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001635{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001636 char name[512];
1637 struct mgcp_rtp_end *end;
1638
Philipp Maier01d24a32017-11-21 17:26:09 +01001639 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001640 end = &conn->end;
1641
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001642 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001643 LOGPENDP(endp, DRTP, LOGL_ERROR, "%u was already bound on conn:%s\n",
1644 rtp_port, mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001645
1646 /* Double bindings should never occour! Since we always allocate
1647 * connections dynamically and free them when they are not
1648 * needed anymore, there must be no previous binding leftover.
1649 * Should there be a connection bound twice, we have a serious
1650 * problem and must exit immediately! */
1651 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001652 }
1653
1654 end->local_port = rtp_port;
Harald Weltec2a8f592020-10-19 13:25:41 +02001655 osmo_fd_setup(&end->rtp, -1, OSMO_FD_READ, rtp_data_net, conn, 0);
1656 osmo_fd_setup(&end->rtcp, -1, OSMO_FD_READ, rtp_data_net, conn, 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001657
Ericfbf78d12021-08-23 22:31:39 +02001658 return bind_rtp(endp->trunk->cfg, conn->end.local_addr, end, endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001659}
1660
Philipp Maier87bd9be2017-08-22 16:35:41 +02001661/*! free allocated RTP and RTCP ports.
1662 * \param[in] end RTP end */
1663void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001664{
1665 if (end->rtp.fd != -1) {
1666 close(end->rtp.fd);
1667 end->rtp.fd = -1;
1668 osmo_fd_unregister(&end->rtp);
1669 }
1670
1671 if (end->rtcp.fd != -1) {
1672 close(end->rtcp.fd);
1673 end->rtcp.fd = -1;
1674 osmo_fd_unregister(&end->rtcp);
1675 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001676}