blob: 5b3ea51c30d72dab0cb0e0f833578cb9027c35e8 [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
Philipp Maier1cb1e382017-11-02 17:16:04 +010091/*! Determine the local rtp bind IP-address.
Philipp Maier0b79d212020-06-18 12:02:49 +020092 * \param[out] addr caller provided memory to store the resulting IP-Address.
93 * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters.
Philipp Maier1cb1e382017-11-02 17:16:04 +010094 *
95 * The local bind IP-address is automatically selected by probing the
96 * IP-Address of the interface that is pointing towards the remote IP-Address,
97 * if no remote IP-Address is known yet, the statically configured
98 * IP-Addresses are used as fallback. */
99void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn)
100{
101
102 struct mgcp_endpoint *endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200103 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier1cb1e382017-11-02 17:16:04 +0100104 int rc;
105 endp = conn->conn->endp;
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200106 bool rem_addr_set = !addr_is_any(&conn->end.addr);
107 char *bind_addr;
Philipp Maier1cb1e382017-11-02 17:16:04 +0100108
109 /* Try probing the local IP-Address */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200110 if (endp->cfg->net_ports.bind_addr_probe && rem_addr_set) {
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200111 rc = osmo_sock_local_ip(addr, osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Philipp Maier1cb1e382017-11-02 17:16:04 +0100112 if (rc < 0)
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200113 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
114 "local interface auto detection failed, using configured addresses...\n");
Philipp Maier1cb1e382017-11-02 17:16:04 +0100115 else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200116 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
117 "selected local rtp bind ip %s by probing using remote ip %s\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200118 addr, osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Philipp Maier1cb1e382017-11-02 17:16:04 +0100119 return;
120 }
121 }
122
Pau Espin Pedrola93c6e92019-05-06 15:23:57 +0200123 /* Select from preconfigured IP-Addresses. We don't have bind_addr for Osmux (yet?). */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200124 if (rem_addr_set) {
Philipp Maier1cb1e382017-11-02 17:16:04 +0100125 /* Check there is a bind IP for the RTP traffic configured,
126 * if so, use that IP-Address */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200127 bind_addr = conn->end.addr.u.sa.sa_family == AF_INET6 ?
128 endp->cfg->net_ports.bind_addr_v6 :
129 endp->cfg->net_ports.bind_addr_v4;
130 } else {
131 /* Choose any of the bind addresses, preferring v6 over v4 */
132 bind_addr = endp->cfg->net_ports.bind_addr_v6;
133 if (!bind_addr)
134 bind_addr = endp->cfg->net_ports.bind_addr_v4;
135 }
136 if (bind_addr) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200137 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
138 "using configured rtp bind ip as local bind ip %s\n",
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200139 bind_addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100140 } else {
141 /* No specific bind IP is configured for the RTP traffic, so
142 * assume the IP where we listen for incoming MGCP messages
143 * as bind IP */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200144 bind_addr = endp->cfg->source_addr;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200145 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200146 "using mgcp bind ip as local rtp bind ip: %s\n", bind_addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100147 }
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200148 osmo_strlcpy(addr, bind_addr, INET6_ADDRSTRLEN);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100149}
150
Philipp Maier87bd9be2017-08-22 16:35:41 +0200151/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200152 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +0200153 * 1/codec_rate seconds. */
154static uint32_t get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200155{
156 struct timespec tp;
157 uint64_t ret;
158
Philipp Maier87bd9be2017-08-22 16:35:41 +0200159 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200160 return 0;
161
162 memset(&tp, 0, sizeof(tp));
163 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maierc3413882017-10-27 12:26:54 +0200164 LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200165
166 /* convert it to 1/unit seconds */
167 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200168 ret *= codec_rate;
169 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200170
171 return ret;
172}
173
Philipp Maier87bd9be2017-08-22 16:35:41 +0200174/* Compute timestamp alignment error */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200175static int32_t ts_alignment_error(const struct mgcp_rtp_stream_state *sstate,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200176 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200177{
178 int32_t timestamp_delta;
179
180 if (ptime == 0)
181 return 0;
182
183 /* Align according to: T - Tlast = k * Tptime */
184 timestamp_delta = timestamp - sstate->last_timestamp;
185
186 return timestamp_delta % ptime;
187}
188
Philipp Maier87bd9be2017-08-22 16:35:41 +0200189/* Check timestamp and sequence number for plausibility */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200190static int check_rtp_timestamp(const struct mgcp_endpoint *endp,
191 const struct mgcp_rtp_state *state,
192 const struct mgcp_rtp_stream_state *sstate,
193 const struct mgcp_rtp_end *rtp_end,
194 const struct osmo_sockaddr *addr,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200195 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200196 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200197{
198 int32_t tsdelta;
199 int32_t timestamp_error;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200200 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200201
202 /* Not fully intialized, skip */
203 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
204 return 0;
205
206 if (seq == sstate->last_seq) {
207 if (timestamp != sstate->last_timestamp) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200208 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200209 LOGPENDP(endp, DRTP, LOGL_ERROR,
210 "The %s timestamp delta is != 0 but the sequence "
211 "number %d is the same, "
212 "TS offset: %d, SeqNo offset: %d "
213 "on SSRC: %u timestamp: %u "
214 "from %s:%d\n",
215 text, seq,
216 state->patch.timestamp_offset, state->patch.seq_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200217 sstate->ssrc, timestamp,
218 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
219 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200220 }
221 return 0;
222 }
223
224 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200225 (int32_t)(timestamp - sstate->last_timestamp) /
226 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200227
228 if (tsdelta == 0) {
229 /* Don't update *tsdelta_out */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200230 LOGPENDP(endp, DRTP, LOGL_NOTICE,
231 "The %s timestamp delta is %d "
232 "on SSRC: %u timestamp: %u "
233 "from %s:%d\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200234 text, tsdelta, sstate->ssrc, timestamp,
235 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
236 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200237
238 return 0;
239 }
240
241 if (sstate->last_tsdelta != tsdelta) {
242 if (sstate->last_tsdelta) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200243 LOGPENDP(endp, DRTP, LOGL_INFO,
244 "The %s timestamp delta changes from %d to %d "
245 "on SSRC: %u timestamp: %u from %s:%d\n",
246 text, sstate->last_tsdelta, tsdelta,
247 sstate->ssrc, timestamp,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200248 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
249 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200250 }
251 }
252
253 if (tsdelta_out)
254 *tsdelta_out = tsdelta;
255
256 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200257 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200258
259 if (timestamp_error) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200260 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200261 LOGPENDP(endp, DRTP, LOGL_NOTICE,
262 "The %s timestamp has an alignment error of %d "
263 "on SSRC: %u "
264 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
265 "from %s:%d. ptime: %d\n",
266 text, timestamp_error,
267 sstate->ssrc,
268 (int16_t)(seq - sstate->last_seq),
269 (int32_t)(timestamp - sstate->last_timestamp),
270 tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200271 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
272 osmo_sockaddr_port(&addr->u.sa),
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200273 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200274 }
275 return 1;
276}
277
278/* Set the timestamp offset according to the packet duration. */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200279static int adjust_rtp_timestamp_offset(const struct mgcp_endpoint *endp,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200280 struct mgcp_rtp_state *state,
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200281 const struct mgcp_rtp_end *rtp_end,
282 const struct osmo_sockaddr *addr,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200283 int16_t delta_seq, uint32_t in_timestamp)
284{
285 int32_t tsdelta = state->packet_duration;
286 int timestamp_offset;
287 uint32_t out_timestamp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200288 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200289
290 if (tsdelta == 0) {
291 tsdelta = state->out_stream.last_tsdelta;
292 if (tsdelta != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200293 LOGPENDP(endp, DRTP, LOGL_NOTICE,
294 "A fixed packet duration is not available, "
295 "using last output timestamp delta instead: %d "
296 "from %s:%d\n", tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200297 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
298 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200299 } else {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200300 tsdelta = rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200301 LOGPENDP(endp, DRTP, LOGL_NOTICE,
302 "Fixed packet duration and last timestamp delta "
303 "are not available, "
304 "using fixed 20ms instead: %d "
305 "from %s:%d\n", tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200306 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
307 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200308 }
309 }
310
311 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
312 timestamp_offset = out_timestamp - in_timestamp;
313
Harald Welte33381352017-12-25 09:44:26 +0100314 if (state->patch.timestamp_offset != timestamp_offset) {
315 state->patch.timestamp_offset = timestamp_offset;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200316 LOGPENDP(endp, DRTP, LOGL_NOTICE,
317 "Timestamp offset change on SSRC: %u "
318 "SeqNo delta: %d, TS offset: %d, "
319 "from %s:%d\n", state->in_stream.ssrc,
320 delta_seq, state->patch.timestamp_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200321 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
322 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200323 }
324
325 return timestamp_offset;
326}
327
328/* Set the timestamp offset according to the packet duration. */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200329static int align_rtp_timestamp_offset(const struct mgcp_endpoint *endp,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200330 struct mgcp_rtp_state *state,
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200331 const struct mgcp_rtp_end *rtp_end,
332 const struct osmo_sockaddr *addr,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200333 uint32_t timestamp)
334{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200335 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200336 int ts_error = 0;
337 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200338 int ptime = state->packet_duration;
339
340 /* Align according to: T + Toffs - Tlast = k * Tptime */
341
Philipp Maier87bd9be2017-08-22 16:35:41 +0200342 ts_error = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100343 timestamp + state->patch.timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200344
Philipp Maier87bd9be2017-08-22 16:35:41 +0200345 /* If there is an alignment error, we have to compensate it */
346 if (ts_error) {
Harald Welte33381352017-12-25 09:44:26 +0100347 state->patch.timestamp_offset += ptime - ts_error;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200348 LOGPENDP(endp, DRTP, LOGL_NOTICE,
349 "Corrected timestamp alignment error of %d on SSRC: %u "
350 "new TS offset: %d, "
351 "from %s:%d\n",
352 ts_error, state->in_stream.ssrc,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200353 state->patch.timestamp_offset,
354 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
355 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200356 }
357
Philipp Maier87bd9be2017-08-22 16:35:41 +0200358 /* Check we really managed to compensate the timestamp
359 * offset. There should not be any remaining error, failing
Harald Welte1d1b98f2017-12-25 10:03:40 +0100360 * here would point to a serous problem with the alignment
361 * error computation function */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200362 ts_check = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100363 timestamp + state->patch.timestamp_offset);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200364 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200365
Philipp Maier87bd9be2017-08-22 16:35:41 +0200366 /* Return alignment error before compensation */
367 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200368}
369
Philipp Maier87bd9be2017-08-22 16:35:41 +0200370/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200371 * \param[in] associated endpoint.
372 * \param[in] destination RTP end.
373 * \param[in,out] pointer to buffer with voice data.
374 * \param[in] voice data length.
375 * \param[in] maximum size of caller provided voice data buffer.
376 * \returns ignores input parameters, return always 0. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200377int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
378 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200379 char *data, int *len, int buf_size)
380{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200381 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200382 return 0;
383}
384
Philipp Maier87bd9be2017-08-22 16:35:41 +0200385/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200386 * \param[in] associated endpoint.
387 * \param[in] destination RTP connnection.
388 * \param[in] source RTP connection.
389 * \returns ignores input parameters, return always 0. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200390int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
Philipp Maieracc10352018-07-19 18:07:57 +0200391 struct mgcp_conn_rtp *conn_dst,
392 struct mgcp_conn_rtp *conn_src)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200393{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200394 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200395 return 0;
396}
397
398void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
Philipp Maier58128252019-03-06 11:28:18 +0100399 const struct mgcp_rtp_codec **codec,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200400 const char **fmtp_extra,
401 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200402{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200403 LOGPENDP(endp, DRTP, LOGL_DEBUG, "conn:%s using format defaults\n",
404 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200405
Philipp Maier58128252019-03-06 11:28:18 +0100406 *codec = conn->end.codec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200407 *fmtp_extra = conn->end.fmtp_extra;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200408}
409
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200410void mgcp_rtp_annex_count(const struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200411 struct mgcp_rtp_state *state, const uint16_t seq,
412 const int32_t transit, const uint32_t ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200413{
414 int32_t d;
415
416 /* initialize or re-initialize */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100417 if (!state->stats.initialized || state->stats.ssrc != ssrc) {
418 state->stats.initialized = 1;
419 state->stats.base_seq = seq;
420 state->stats.max_seq = seq - 1;
421 state->stats.ssrc = ssrc;
422 state->stats.jitter = 0;
423 state->stats.transit = transit;
424 state->stats.cycles = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200425 } else {
426 uint16_t udelta;
427
Philipp Maier87bd9be2017-08-22 16:35:41 +0200428 /* The below takes the shape of the validation of
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200429 * Appendix A. Check if there is something weird with
430 * the sequence number, otherwise check for a wrap
431 * around in the sequence number.
432 * It can't wrap during the initialization so let's
433 * skip it here. The Appendix A probably doesn't have
Philipp Maier87bd9be2017-08-22 16:35:41 +0200434 * this issue because of the probation. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100435 udelta = seq - state->stats.max_seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200436 if (udelta < RTP_MAX_DROPOUT) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100437 if (seq < state->stats.max_seq)
438 state->stats.cycles += RTP_SEQ_MOD;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200439 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200440 LOGPENDP(endp, DRTP, LOGL_NOTICE,
441 "RTP seqno made a very large jump on delta: %u\n",
442 udelta);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200443 }
444 }
445
Philipp Maier87bd9be2017-08-22 16:35:41 +0200446 /* Calculate the jitter between the two packages. The TS should be
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200447 * taken closer to the read function. This was taken from the
448 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
Philipp Maier87bd9be2017-08-22 16:35:41 +0200449 * resolution. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100450 d = transit - state->stats.transit;
451 state->stats.transit = transit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200452 if (d < 0)
453 d = -d;
Harald Welte49e3d5a2017-12-25 09:47:57 +0100454 state->stats.jitter += d - ((state->stats.jitter + 8) >> 4);
455 state->stats.max_seq = seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200456}
457
Philipp Maier6931f9a2018-07-26 09:29:31 +0200458/* There may be different payload type numbers negotiated for two connections.
459 * Patch the payload type of an RTP packet so that it uses the payload type
460 * that is valid for the destination connection (conn_dst) */
461static int mgcp_patch_pt(struct mgcp_conn_rtp *conn_src,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200462 struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200463{
464 struct rtp_hdr *rtp_hdr;
465 uint8_t pt_in;
466 int pt_out;
467
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200468 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
469 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
470 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200471 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200472 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200473
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200474 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier6931f9a2018-07-26 09:29:31 +0200475
476 pt_in = rtp_hdr->payload_type;
477 pt_out = mgcp_codec_pt_translate(conn_src, conn_dst, pt_in);
478 if (pt_out < 0)
479 return -EINVAL;
480
481 rtp_hdr->payload_type = (uint8_t) pt_out;
482 return 0;
483}
484
Philipp Maier87bd9be2017-08-22 16:35:41 +0200485/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200486 * some of the supported endpoints (e.g. the nanoBTS) can only handle
487 * one source and this code will patch RTP header to appear as if there
488 * is only one source.
489 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200490 * we receive will be seen as a switch in streams. */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200491void mgcp_patch_and_count(const struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200492 struct mgcp_rtp_state *state,
493 struct mgcp_rtp_end *rtp_end,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200494 struct osmo_sockaddr *addr, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200495{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200496 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200497 uint32_t arrival_time;
498 int32_t transit;
499 uint16_t seq;
500 uint32_t timestamp, ssrc;
501 struct rtp_hdr *rtp_hdr;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200502 int payload = rtp_end->codec->payload_type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200503 unsigned int len = msgb_length(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200504
505 if (len < sizeof(*rtp_hdr))
506 return;
507
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200508 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200509 seq = ntohs(rtp_hdr->sequence);
510 timestamp = ntohl(rtp_hdr->timestamp);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200511 arrival_time = get_current_ts(rtp_end->codec->rate);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200512 ssrc = ntohl(rtp_hdr->ssrc);
513 transit = arrival_time - timestamp;
514
515 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc);
516
517 if (!state->initialized) {
518 state->initialized = 1;
519 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100520 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200521 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200522 state->packet_duration =
523 mgcp_rtp_packet_duration(endp, rtp_end);
Philipp Maier0ec1d4e2018-05-16 11:09:42 +0200524 state->out_stream.last_seq = seq - 1;
525 state->out_stream.ssrc = state->patch.orig_ssrc = ssrc;
526 state->out_stream.last_tsdelta = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200527 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200528 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200529 LOGPENDP(endp, DRTP, LOGL_INFO,
530 "initializing stream, SSRC: %u timestamp: %u "
531 "pkt-duration: %d, from %s:%d\n",
532 state->in_stream.ssrc,
533 state->patch.seq_offset, state->packet_duration,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200534 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
535 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200536 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200537 state->packet_duration =
Philipp Maierbc0346e2018-06-07 09:52:16 +0200538 rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200539 LOGPENDP(endp, DRTP, LOGL_NOTICE,
540 "fixed packet duration is not available, "
541 "using fixed 20ms instead: %d from %s:%d\n",
542 state->packet_duration,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200543 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
544 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200545 }
546 } else if (state->in_stream.ssrc != ssrc) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200547 LOGPENDP(endp, DRTP, LOGL_NOTICE,
548 "SSRC changed: %u -> %u "
549 "from %s:%d\n",
550 state->in_stream.ssrc, rtp_hdr->ssrc,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200551 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
552 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200553
554 state->in_stream.ssrc = ssrc;
555 if (rtp_end->force_constant_ssrc) {
556 int16_t delta_seq;
557
558 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100559 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200560 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200561
562 /* Estimate number of packets that would have been sent */
563 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200564 (arrival_time - state->in_stream.last_arrival_time
565 + state->packet_duration / 2) /
566 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200567
568 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
569 delta_seq, timestamp);
570
Pau Espin Pedrol4c77e9b2021-07-07 12:12:58 +0200571 state->patch.patch_ssrc = true;
Harald Welte33381352017-12-25 09:44:26 +0100572 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200573 if (rtp_end->force_constant_ssrc != -1)
574 rtp_end->force_constant_ssrc -= 1;
575
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200576 LOGPENDP(endp, DRTP, LOGL_NOTICE,
577 "SSRC patching enabled, SSRC: %u "
578 "SeqNo offset: %d, TS offset: %d "
579 "from %s:%d\n", state->in_stream.ssrc,
580 state->patch.seq_offset, state->patch.timestamp_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200581 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
582 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200583 }
584
585 state->in_stream.last_tsdelta = 0;
586 } else {
587 /* Compute current per-packet timestamp delta */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200588 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
589 addr, seq, timestamp, "input",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200590 &state->in_stream.last_tsdelta);
591
Harald Welte33381352017-12-25 09:44:26 +0100592 if (state->patch.patch_ssrc)
593 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200594 }
595
596 /* Save before patching */
597 state->in_stream.last_timestamp = timestamp;
598 state->in_stream.last_seq = seq;
599 state->in_stream.last_arrival_time = arrival_time;
600
601 if (rtp_end->force_aligned_timing &&
602 state->out_stream.ssrc == ssrc && state->packet_duration)
603 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200604 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
605 timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200606
607 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100608 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200609 rtp_hdr->ssrc = htonl(ssrc);
610
611 /* Apply the offset and store it back to the packet.
612 * This won't change anything if the offset is 0, so the conditional is
613 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100614 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200615 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100616 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200617 rtp_hdr->timestamp = htonl(timestamp);
618
619 /* Check again, whether the timestamps are still valid */
620 if (state->out_stream.ssrc == ssrc)
621 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
622 addr, seq, timestamp, "output",
623 &state->out_stream.last_tsdelta);
624
625 /* Save output values */
626 state->out_stream.last_seq = seq;
627 state->out_stream.last_timestamp = timestamp;
628 state->out_stream.ssrc = ssrc;
629
630 if (payload < 0)
631 return;
632
633#if 0
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200634 LOGPENDP(endp, DRTP, LOGL_DEBUG, "payload hdr payload %u -> endp payload %u\n",
635 rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200636 rtp_hdr->payload_type = payload;
637#endif
638}
639
Philipp Maier9fc8a022019-02-20 12:26:52 +0100640/* There are different dialects used to format and transfer voice data. When
641 * the receiving end expects GSM-HR data to be formated after RFC 5993, this
642 * function is used to convert between RFC 5993 and TS 101318, which we normally
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200643 * use.
644 * Return 0 on sucess, negative on errors like invalid data length. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200645static int rfc5993_hr_convert(struct mgcp_endpoint *endp, struct msgb *msg)
Philipp Maier9fc8a022019-02-20 12:26:52 +0100646{
Philipp Maier9fc8a022019-02-20 12:26:52 +0100647 struct rtp_hdr *rtp_hdr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200648 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200649 LOGPENDP(endp, DRTP, LOGL_ERROR, "AMR RTP packet too short (%d < %zu)\n",
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200650 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200651 return -EINVAL;
652 }
653
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200654 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100655
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200656 if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr)) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100657 /* TS 101318 encoding => RFC 5993 encoding */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200658 msgb_put(msg, 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100659 memmove(rtp_hdr->data + 1, rtp_hdr->data, GSM_HR_BYTES);
660 rtp_hdr->data[0] = 0x00;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200661 } else if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr) + 1) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100662 /* RFC 5993 encoding => TS 101318 encoding */
663 memmove(rtp_hdr->data, rtp_hdr->data + 1, GSM_HR_BYTES);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200664 msgb_trim(msg, msgb_length(msg) - 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100665 } else {
666 /* It is possible that multiple payloads occur in one RTP
667 * packet. This is not supported yet. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200668 LOGPENDP(endp, DRTP, LOGL_ERROR,
669 "cannot figure out how to convert RTP packet\n");
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200670 return -ENOTSUP;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100671 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200672 return 0;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100673}
674
Philipp Maier228e5912019-03-05 13:56:59 +0100675/* For AMR RTP two framing modes are defined RFC3267. There is a bandwith
676 * efficient encoding scheme where all fields are packed together one after
677 * another and an octet aligned mode where all fields are aligned to octet
678 * boundaries. This function is used to convert between the two modes */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200679static int amr_oa_bwe_convert(struct mgcp_endpoint *endp, struct msgb *msg,
Philipp Maier228e5912019-03-05 13:56:59 +0100680 bool target_is_oa)
681{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200682 /* NOTE: the msgb has an allocated length of RTP_BUF_SIZE, so there is
Philipp Maier228e5912019-03-05 13:56:59 +0100683 * plenty of space available to store the slightly larger, converted
684 * data */
Philipp Maier228e5912019-03-05 13:56:59 +0100685 struct rtp_hdr *rtp_hdr;
686 unsigned int payload_len;
687 int rc;
688
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200689 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
690 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 +0200691 return -EINVAL;
692 }
693
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200694 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier228e5912019-03-05 13:56:59 +0100695
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200696 payload_len = msgb_length(msg) - sizeof(struct rtp_hdr);
Philipp Maier228e5912019-03-05 13:56:59 +0100697
698 if (osmo_amr_is_oa(rtp_hdr->data, payload_len)) {
699 if (!target_is_oa)
700 /* Input data is oa an target format is bwe
701 * ==> convert */
702 rc = osmo_amr_oa_to_bwe(rtp_hdr->data, payload_len);
703 else
704 /* Input data is already bew, but we accept it anyway
705 * ==> no conversion needed */
706 rc = payload_len;
707 } else {
708 if (target_is_oa)
709 /* Input data is bwe an target format is oa
710 * ==> convert */
711 rc = osmo_amr_bwe_to_oa(rtp_hdr->data, payload_len,
712 RTP_BUF_SIZE);
713 else
714 /* Input data is already oa, but we accept it anyway
715 * ==> no conversion needed */
716 rc = payload_len;
717 }
718 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200719 LOGPENDP(endp, DRTP, LOGL_ERROR,
720 "AMR RTP packet conversion failed\n");
Philipp Maier228e5912019-03-05 13:56:59 +0100721 return -EINVAL;
722 }
723
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200724 return msgb_trim(msg, rc + sizeof(struct rtp_hdr));
Philipp Maier228e5912019-03-05 13:56:59 +0100725}
726
727/* Check if a conversion between octet-aligned and bandwith-efficient mode is
728 * indicated. */
729static bool amr_oa_bwe_convert_indicated(struct mgcp_rtp_codec *codec)
730{
731 if (codec->param_present == false)
732 return false;
733 if (!codec->param.amr_octet_aligned_present)
734 return false;
735 if (strcmp(codec->subtype_name, "AMR") != 0)
736 return false;
737 return true;
738}
739
740
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200741/* Return whether an RTP packet with AMR payload is in octet-aligned mode.
742 * Return 0 if in bandwidth-efficient mode, 1 for octet-aligned mode, and negative if the RTP data is invalid. */
743static int amr_oa_check(char *data, int len)
Philipp Maier228e5912019-03-05 13:56:59 +0100744{
745 struct rtp_hdr *rtp_hdr;
746 unsigned int payload_len;
747
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200748 if (len < sizeof(struct rtp_hdr))
749 return -EINVAL;
750
Philipp Maier228e5912019-03-05 13:56:59 +0100751 rtp_hdr = (struct rtp_hdr *)data;
752
753 payload_len = len - sizeof(struct rtp_hdr);
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200754 if (payload_len < sizeof(struct amr_hdr))
755 return -EINVAL;
Philipp Maier228e5912019-03-05 13:56:59 +0100756
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200757 return osmo_amr_is_oa(rtp_hdr->data, payload_len) ? 1 : 0;
Philipp Maier228e5912019-03-05 13:56:59 +0100758}
759
Philipp Maier87bd9be2017-08-22 16:35:41 +0200760/* Forward data to a debug tap. This is debug function that is intended for
761 * debugging the voice traffic with tools like gstreamer */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200762static void forward_data(int fd, struct mgcp_rtp_tap *tap, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200763{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100764 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200765
Philipp Maiere6f172d2017-11-07 12:00:01 +0100766 if (!tap->enabled)
767 return;
768
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200769 rc = sendto(fd, msgb_data(msg), msgb_length(msg), 0, (struct sockaddr *)&tap->forward,
Philipp Maiere6f172d2017-11-07 12:00:01 +0100770 sizeof(tap->forward));
771
772 if (rc < 0)
773 LOGP(DRTP, LOGL_ERROR,
774 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200775}
776
Philipp Maier889fe7f2020-07-06 17:44:12 +0200777/* Generate an RTP header if it is missing */
778static void gen_rtp_header(struct msgb *msg, struct mgcp_rtp_end *rtp_end,
779 struct mgcp_rtp_state *state)
780{
781 struct rtp_hdr *hdr = (struct rtp_hdr *)msgb_data(msg);
782
783 if (hdr->version > 0)
784 return;
785
786 hdr->version = 2;
787 hdr->payload_type = rtp_end->codec->payload_type;
788 hdr->timestamp = osmo_htonl(get_current_ts(rtp_end->codec->rate));
789 hdr->sequence = osmo_htons(state->alt_rtp_tx_sequence);
790 hdr->ssrc = state->alt_rtp_tx_ssrc;
791}
792
Philipp Maier87bd9be2017-08-22 16:35:41 +0200793/* Check if the origin (addr) matches the address/port data of the RTP
794 * connections. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200795static int check_rtp_origin(struct mgcp_conn_rtp *conn, struct osmo_sockaddr *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200796{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200797 char ipbuf[INET6_ADDRSTRLEN];
798
799 if (addr_is_any(&conn->end.addr)) {
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200800 switch (conn->conn->mode) {
801 case MGCP_CONN_LOOPBACK:
802 /* HACK: for IuUP, we want to reply with an IuUP Initialization ACK upon the first RTP
803 * message received. We currently hackishly accomplish that by putting the endpoint in
804 * loopback mode and patching over the looped back RTP message to make it look like an
805 * ack. We don't know the femto cell's IP address and port until the RAB Assignment
806 * Response is received, but the nano3G expects an IuUP Initialization Ack before it even
807 * sends the RAB Assignment Response. Hence, if the remote address is 0.0.0.0 and the
808 * MGCP port is in loopback mode, allow looping back the packet to any source. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200809 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
810 "In loopback mode and remote address not set:"
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200811 " allowing data from address: %s\n",
812 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200813 return 0;
814
815 default:
816 /* Receiving early media before the endpoint is configured. Instead of logging
817 * this as an error that occurs on every call, keep it more low profile to not
818 * confuse humans with expected errors. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200819 LOGPCONN(conn->conn, DRTP, LOGL_INFO,
820 "Rx RTP from %s, but remote address not set:"
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200821 " dropping early media\n",
822 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200823 return -1;
824 }
Neels Hofmeyr0063ca22018-07-23 18:12:16 +0200825 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200826
Philipp Maier87bd9be2017-08-22 16:35:41 +0200827 /* Note: Check if the inbound RTP data comes from the same host to
828 * which we send our outgoing RTP traffic. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200829 if (conn->end.addr.u.sa.sa_family != addr->u.sa.sa_family ||
830 (conn->end.addr.u.sa.sa_family == AF_INET &&
831 conn->end.addr.u.sin.sin_addr.s_addr != addr->u.sin.sin_addr.s_addr) ||
832 (conn->end.addr.u.sa.sa_family == AF_INET6 &&
833 memcmp(&conn->end.addr.u.sin6.sin6_addr, &addr->u.sin6.sin6_addr,
834 sizeof(struct in6_addr)))) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200835 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200836 "data from wrong address: %s, ",
837 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Philipp Maierc3413882017-10-27 12:26:54 +0200838 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200839 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200840 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200841 return -1;
842 }
843
Philipp Maier87bd9be2017-08-22 16:35:41 +0200844 /* Note: Usually the remote remote port of the data we receive will be
845 * the same as the remote port where we transmit outgoing RTP traffic
846 * to (set by MDCX). We use this to check the origin of the data for
847 * plausibility. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200848 if (ntohs(conn->end.rtp_port) != osmo_sockaddr_port(&addr->u.sa) &&
849 ntohs(conn->end.rtcp_port) != osmo_sockaddr_port(&addr->u.sa)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200850 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200851 "data from wrong source port: %d, ",
852 osmo_sockaddr_port(&addr->u.sa));
Philipp Maierc3413882017-10-27 12:26:54 +0200853 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200854 "expected: %d for RTP or %d for RTCP\n",
855 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200856 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200857 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200858 }
859
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200860 return 0;
861}
862
Philipp Maier87bd9be2017-08-22 16:35:41 +0200863/* Check the if the destination address configuration of an RTP connection
864 * makes sense */
865static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200866{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200867 char ipbuf[INET6_ADDRSTRLEN];
868 bool ip_is_any = addr_is_any(&conn->end.addr);
869
Philipp Maiere6df0e42018-05-29 14:03:06 +0200870 /* Note: it is legal to create a connection but never setting a port
871 * and IP-address for outgoing data. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200872 if (ip_is_any && conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200873 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
874 "destination IP-address and rtp port is (not yet) known (%s:%u)\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200875 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port);
Philipp Maiere6df0e42018-05-29 14:03:06 +0200876 return -1;
877 }
878
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200879 if (ip_is_any) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200880 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
881 "destination IP-address is invalid (%s:%u)\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200882 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200883 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200884 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200885
886 if (conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200887 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
888 "destination rtp port is invalid (%s:%u)\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200889 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200890 return -1;
891 }
892
893 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200894}
895
Philipp Maier4dba7692018-08-03 12:20:52 +0200896/* Do some basic checks to make sure that the RTCP packets we are going to
897 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200898static int check_rtcp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +0200899{
900 struct rtcp_hdr *hdr;
901 unsigned int len;
902 uint8_t type;
903
904 /* RTPC packets that are just a header without data do not make
905 * any sense. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200906 if (msgb_length(msg) < sizeof(struct rtcp_hdr)) {
907 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP packet too short (%u < %zu)\n",
908 msgb_length(msg), sizeof(struct rtcp_hdr));
Philipp Maier4dba7692018-08-03 12:20:52 +0200909 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200910 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200911
912 /* Make sure that the length of the received packet does not exceed
913 * the available buffer size */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200914 hdr = (struct rtcp_hdr *)msgb_data(msg);
Philipp Maier4dba7692018-08-03 12:20:52 +0200915 len = (osmo_ntohs(hdr->length) + 1) * 4;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200916 if (len > msgb_length(msg)) {
917 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header length exceeds packet size (%u > %u)\n",
918 len, msgb_length(msg));
Philipp Maier4dba7692018-08-03 12:20:52 +0200919 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200920 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200921
922 /* Make sure we accept only packets that have a proper packet type set
923 * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
924 type = hdr->type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200925 if ((type < 192 || type > 195) && (type < 200 || type > 213)) {
926 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header: invalid type: %u\n", type);
Philipp Maier4dba7692018-08-03 12:20:52 +0200927 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200928 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200929
930 return 0;
931}
932
933/* Do some basic checks to make sure that the RTP packets we are going to
934 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200935static int check_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +0200936{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200937 size_t min_size = sizeof(struct rtp_hdr);
938 if (msgb_length(msg) < min_size) {
939 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
940 msgb_length(msg), min_size);
941 return -1;
942 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200943
944 /* FIXME: Add more checks, the reason why we do not check more than
945 * the length is because we currently handle IUUP packets as RTP
946 * packets, so they must pass this check, if we weould be more
947 * strict here, we would possibly break 3G. (see also FIXME note
948 * below */
949
950 return 0;
951}
952
Philipp Maier87bd9be2017-08-22 16:35:41 +0200953/* Send RTP data. Possible options are standard RTP packet
954 * transmission or trsmission via an osmux connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200955static int mgcp_send_rtp(struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200956{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200957 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
958 enum rtp_proto proto = mc->proto;
959 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200960 struct mgcp_endpoint *endp = conn_src->conn->endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200961
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200962 LOGPENDP(endp, DRTP, LOGL_DEBUG, "destin conn:%s\n",
963 mgcp_conn_dump(conn_dst->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200964
965 /* Before we try to deliver the packet, we check if the destination
966 * port and IP-Address make sense at all. If not, we will be unable
967 * to deliver the packet. */
968 if (check_rtp_destin(conn_dst) != 0)
969 return -1;
970
971 /* Depending on the RTP connection type, deliver the RTP packet to the
972 * destination connection. */
973 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200974 case MGCP_RTP_DEFAULT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200975 LOGPENDP(endp, DRTP, LOGL_DEBUG,
976 "endpoint type is MGCP_RTP_DEFAULT, "
977 "using mgcp_send() to forward data directly\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200978 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200979 mc->from_addr, msg, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200980 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200981 case MGCP_OSMUX_BSC:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200982 LOGPENDP(endp, DRTP, LOGL_DEBUG,
983 "endpoint type is MGCP_OSMUX_BSC_NAT, "
984 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n");
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200985 return osmux_xfrm_to_osmux((char*)msgb_data(msg), msgb_length(msg), conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200986 }
987
Philipp Maier87bd9be2017-08-22 16:35:41 +0200988 /* If the data has not been handled/forwarded until here, it will
989 * be discarded, this should not happen, normally the MGCP type
990 * should be properly set */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200991 LOGPENDP(endp, DRTP, LOGL_ERROR, "bad MGCP type -- data discarded!\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200992
993 return -1;
994}
995
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +0200996/*! send udp packet.
997 * \param[in] fd associated file descriptor.
998 * \param[in] addr destination ip-address.
999 * \param[in] port destination UDP port (network byte order).
1000 * \param[in] buf buffer that holds the data to be send.
1001 * \param[in] len length of the data to be sent.
1002 * \returns bytes sent, -1 on error. */
1003int mgcp_udp_send(int fd, struct osmo_sockaddr *addr, int port, const char *buf, int len)
1004{
1005 char ipbuf[INET6_ADDRSTRLEN];
1006 size_t addr_len;
1007 bool is_ipv6 = addr->u.sa.sa_family == AF_INET6;
1008
1009 LOGP(DRTP, LOGL_DEBUG,
1010 "sending %i bytes length packet to %s:%u ...\n", len,
1011 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
1012 ntohs(port));
1013
1014 if (is_ipv6) {
1015 addr->u.sin6.sin6_port = port;
1016 addr_len = sizeof(addr->u.sin6);
1017 } else {
1018 addr->u.sin.sin_port = port;
1019 addr_len = sizeof(addr->u.sin);
1020 }
1021
1022 return sendto(fd, buf, len, 0, &addr->u.sa, addr_len);
1023}
1024
1025/*! send RTP dummy packet (to keep NAT connection open).
1026 * \param[in] endp mcgp endpoint that holds the RTP connection.
1027 * \param[in] conn associated RTP connection.
1028 * \returns bytes sent, -1 on error. */
1029int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
1030{
1031 int rc;
1032 int was_rtcp = 0;
1033
1034 OSMO_ASSERT(endp);
1035 OSMO_ASSERT(conn);
1036
1037 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG, "sending dummy packet... %s\n",
1038 mgcp_conn_dump(conn->conn));
1039
Pau Espin Pedrola24dcc62021-07-06 17:48:47 +02001040 /* Before we try to deliver the packet, we check if the destination
1041 * port and IP-Address make sense at all. If not, we will be unable
1042 * to deliver the packet. */
1043 if (check_rtp_destin(conn) != 0)
1044 goto failed;
1045
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001046 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
1047 conn->end.rtp_port, rtp_dummy_payload, sizeof(rtp_dummy_payload));
1048
1049 if (rc == -1)
1050 goto failed;
1051
1052 if (endp->trunk->omit_rtcp)
1053 return rc;
1054
1055 was_rtcp = 1;
1056 rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
1057 conn->end.rtcp_port, rtp_dummy_payload, sizeof(rtp_dummy_payload));
1058
1059 if (rc >= 0)
1060 return rc;
1061
1062failed:
1063 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1064 "Failed to send dummy %s packet.\n",
1065 was_rtcp ? "RTCP" : "RTP");
1066
1067 return -1;
1068}
1069
1070/*! Send RTP/RTCP data to a specified destination connection.
1071 * \param[in] endp associated endpoint (for configuration, logging).
1072 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP.
1073 * \param[in] spoofed source address (set to NULL to disable).
1074 * \param[in] buf buffer that contains the RTP/RTCP data.
1075 * \param[in] len length of the buffer that contains the RTP/RTCP data.
1076 * \param[in] conn_src associated source connection.
1077 * \param[in] conn_dst associated destination connection.
1078 * \returns 0 on success, -1 on ERROR. */
1079int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct osmo_sockaddr *addr,
1080 struct msgb *msg, struct mgcp_conn_rtp *conn_src,
1081 struct mgcp_conn_rtp *conn_dst)
1082{
1083 /*! When no destination connection is available (e.g. when only one
1084 * connection in loopback mode exists), then the source connection
1085 * shall be specified as destination connection */
1086
1087 struct mgcp_trunk *trunk = endp->trunk;
1088 struct mgcp_rtp_end *rtp_end;
1089 struct mgcp_rtp_state *rtp_state;
1090 char ipbuf[INET6_ADDRSTRLEN];
1091 char *dest_name;
1092 int rc;
1093 int len;
1094
1095 OSMO_ASSERT(conn_src);
1096 OSMO_ASSERT(conn_dst);
1097
1098 if (is_rtp)
1099 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTP packet...\n");
1100 else
1101 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTCP packet...\n");
1102
1103 /* FIXME: It is legal that the payload type on the egress connection is
1104 * different from the payload type that has been negotiated on the
1105 * ingress connection. Essentially the codecs are the same so we can
1106 * match them and patch the payload type. However, if we can not find
1107 * the codec pendant (everything ist equal except the PT), we are of
1108 * course unable to patch the payload type. A situation like this
1109 * should not occur if transcoding is consequently avoided. Until
1110 * we have transcoding support in osmo-mgw we can not resolve this. */
1111 if (is_rtp) {
1112 rc = mgcp_patch_pt(conn_src, conn_dst, msg);
1113 if (rc < 0) {
1114 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1115 "can not patch PT because no suitable egress codec was found.\n");
1116 }
1117 }
1118
1119 /* Note: In case of loopback configuration, both, the source and the
1120 * destination will point to the same connection. */
1121 rtp_end = &conn_dst->end;
1122 rtp_state = &conn_src->state;
1123 dest_name = conn_dst->conn->name;
1124
1125 /* Ensure we have an alternative SSRC in case we need it, see also
1126 * gen_rtp_header() */
1127 if (rtp_state->alt_rtp_tx_ssrc == 0)
1128 rtp_state->alt_rtp_tx_ssrc = rand();
1129
1130 if (!rtp_end->output_enabled) {
1131 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_DROPPED_PACKETS_CTR);
1132 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1133 "output disabled, drop to %s %s "
1134 "rtp_port:%u rtcp_port:%u\n",
1135 dest_name,
1136 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
1137 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
1138 );
1139 } else if (is_rtp) {
1140 int cont;
1141 int nbytes = 0;
1142 int buflen = msgb_length(msg);
1143
1144 /* Make sure we have a valid RTP header, in cases where no RTP
1145 * header is present, we will generate one. */
1146 gen_rtp_header(msg, rtp_end, rtp_state);
1147
1148 do {
1149 /* Run transcoder */
1150 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
1151 (char *)msgb_data(msg), &buflen,
1152 RTP_BUF_SIZE);
1153 if (cont < 0)
1154 break;
1155
1156 if (addr)
1157 mgcp_patch_and_count(endp, rtp_state, rtp_end,
1158 addr, msg);
1159
1160 if (amr_oa_bwe_convert_indicated(conn_dst->end.codec)) {
1161 rc = amr_oa_bwe_convert(endp, msg,
1162 conn_dst->end.codec->param.amr_octet_aligned);
1163 if (rc < 0) {
1164 LOGPENDP(endp, DRTP, LOGL_ERROR,
1165 "Error in AMR octet-aligned <-> bandwidth-efficient mode conversion\n");
1166 break;
1167 }
1168 } else if (rtp_end->rfc5993_hr_convert &&
1169 strcmp(conn_src->end.codec->subtype_name, "GSM-HR-08") == 0) {
1170 rc = rfc5993_hr_convert(endp, msg);
1171 if (rc < 0) {
1172 LOGPENDP(endp, DRTP, LOGL_ERROR, "Error while converting to GSM-HR-08\n");
1173 break;
1174 }
1175 }
1176
1177 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1178 "process/send to %s %s "
1179 "rtp_port:%u rtcp_port:%u\n",
1180 dest_name,
1181 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
1182 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
1183 );
1184
1185 /* Forward a copy of the RTP data to a debug ip/port */
1186 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
1187 msg);
1188
1189 /* FIXME: HACK HACK HACK. See OS#2459.
1190 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
1191 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
1192 * cells (as long as we patch only the first RTP payload in each stream).
1193 */
1194 if (!rtp_state->patched_first_rtp_payload
1195 && conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
1196 uint8_t *data = msgb_data(msg) + 12;
1197 if (data[0] == 0xe0) {
1198 data[0] = 0xe4;
1199 data[1] = 0x00;
1200 rtp_state->patched_first_rtp_payload = true;
1201 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1202 "Patching over first two bytes"
1203 " to fake an IuUP Initialization Ack\n");
1204 }
1205 }
1206
1207 len = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr, rtp_end->rtp_port,
1208 (char *)msgb_data(msg), msgb_length(msg));
1209
1210 if (len <= 0)
1211 return len;
1212
1213 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
1214 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
1215 rtp_state->alt_rtp_tx_sequence++;
1216
1217 nbytes += len;
1218 buflen = cont;
1219 } while (buflen > 0);
1220 return nbytes;
1221 } else if (!trunk->omit_rtcp) {
1222 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1223 "send to %s %s rtp_port:%u rtcp_port:%u\n",
1224 dest_name, osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
1225 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
1226 );
1227
1228 len = mgcp_udp_send(rtp_end->rtcp.fd,
1229 &rtp_end->addr,
1230 rtp_end->rtcp_port, (char *)msgb_data(msg), msgb_length(msg));
1231
1232 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
1233 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
1234 rtp_state->alt_rtp_tx_sequence++;
1235
1236 return len;
1237 }
1238
1239 return 0;
1240}
1241
Philipp Maier87bd9be2017-08-22 16:35:41 +02001242/*! dispatch incoming RTP packet to opposite RTP connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001243 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1244 * \param[in] addr socket address where the RTP packet has been received from.
1245 * \param[in] buf buffer that hold the RTP payload.
1246 * \param[in] buf_size size data length of buf.
1247 * \param[in] conn originating connection.
1248 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001249int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001250{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001251 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1252 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1253 struct mgcp_conn *conn = conn_src->conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001254 struct mgcp_conn *conn_dst;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001255 struct osmo_sockaddr *from_addr = mc->from_addr;
Philipp Maier97a93122021-05-07 22:50:31 +02001256 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001257
1258 /*! NOTE: This callback function implements the endpoint specific
Alexander Chemeris61cf9bb2020-05-11 18:13:53 +03001259 * dispatch behaviour of an rtp bridge/proxy endpoint. It is assumed
Philipp Maier87bd9be2017-08-22 16:35:41 +02001260 * that the endpoint will hold only two connections. This premise
1261 * is used to determine the opposite connection (it is always the
1262 * connection that is not the originating connection). Once the
1263 * destination connection is known the RTP packet is sent via
1264 * the destination connection. */
1265
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001266
1267 /* Check if the connection is in loopback mode, if yes, just send the
1268 * incoming data back to the origin */
1269 if (conn->mode == MGCP_CONN_LOOPBACK) {
1270 /* When we are in loopback mode, we loop back all incoming
1271 * packets back to their origin. We will use the originating
1272 * address data from the UDP packet header to patch the
1273 * outgoing address in connection on the fly */
1274 if (conn->u.rtp.end.rtp_port == 0) {
Philipp Maier97a93122021-05-07 22:50:31 +02001275 memcpy(&conn->u.rtp.end.addr, from_addr,
1276 sizeof(conn->u.rtp.end.addr));
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001277 switch (from_addr->u.sa.sa_family) {
1278 case AF_INET:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001279 conn->u.rtp.end.rtp_port = from_addr->u.sin.sin_port;
1280 break;
1281 case AF_INET6:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001282 conn->u.rtp.end.rtp_port = from_addr->u.sin6.sin6_port;
1283 break;
1284 default:
1285 OSMO_ASSERT(false);
1286 }
Philipp Maier97a93122021-05-07 22:50:31 +02001287 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1288 "loopback mode: implicitly using source address (%s:%u) as destination address\n",
1289 osmo_sockaddr_ntop(&from_addr->u.sa, ipbuf),
1290 conn->u.rtp.end.rtp_port);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001291 }
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001292 return mgcp_send_rtp(conn_src, msg);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001293 }
1294
Philipp Maier87bd9be2017-08-22 16:35:41 +02001295 /* Find a destination connection. */
1296 /* NOTE: This code path runs every time an RTP packet is received. The
1297 * function mgcp_find_dst_conn() we use to determine the detination
1298 * connection will iterate the connection list inside the endpoint.
1299 * Since list iterations are quite costly, we will figure out the
1300 * destination only once and use the optional private data pointer of
1301 * the connection to cache the destination connection pointer. */
1302 if (!conn->priv) {
1303 conn_dst = mgcp_find_dst_conn(conn);
1304 conn->priv = conn_dst;
1305 } else {
1306 conn_dst = (struct mgcp_conn *)conn->priv;
1307 }
1308
1309 /* There is no destination conn, stop here */
1310 if (!conn_dst) {
Alexander Chemerisebb9bf32020-05-11 18:14:31 +03001311 LOGPCONN(conn, DRTP, LOGL_DEBUG,
1312 "no connection to forward an incoming RTP packet to\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001313 return -1;
1314 }
1315
1316 /* The destination conn is not an RTP connection */
1317 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001318 LOGPCONN(conn, DRTP, LOGL_ERROR,
1319 "unable to find suitable destination conn\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001320 return -1;
1321 }
1322
1323 /* Dispatch RTP packet to destination RTP connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001324 return mgcp_send_rtp(&conn_dst->u.rtp, msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001325}
1326
Philipp Maier0996a1e2020-06-10 15:27:14 +02001327/*! dispatch incoming RTP packet to E1 subslot, handle RTCP packets locally.
1328 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1329 * \param[in] addr socket address where the RTP packet has been received from.
1330 * \param[in] buf buffer that hold the RTP payload.
1331 * \param[in] buf_size size data length of buf.
1332 * \param[in] conn originating connection.
1333 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001334int mgcp_dispatch_e1_bridge_cb(struct msgb *msg)
Philipp Maier0996a1e2020-06-10 15:27:14 +02001335{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001336 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1337 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1338 struct mgcp_conn *conn = conn_src->conn;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001339 struct osmo_sockaddr *from_addr = mc->from_addr;
Philipp Maier97a93122021-05-07 22:50:31 +02001340 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001341
Philipp Maier889fe7f2020-07-06 17:44:12 +02001342 /* Check if the connection is in loopback mode, if yes, just send the
1343 * incoming data back to the origin */
1344 if (conn->mode == MGCP_CONN_LOOPBACK) {
1345 /* When we are in loopback mode, we loop back all incoming
1346 * packets back to their origin. We will use the originating
1347 * address data from the UDP packet header to patch the
1348 * outgoing address in connection on the fly */
1349 if (conn->u.rtp.end.rtp_port == 0) {
Philipp Maier97a93122021-05-07 22:50:31 +02001350 memcpy(&conn->u.rtp.end.addr, from_addr,
1351 sizeof(conn->u.rtp.end.addr));
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001352 switch (from_addr->u.sa.sa_family) {
1353 case AF_INET:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001354 conn->u.rtp.end.rtp_port = from_addr->u.sin.sin_port;
1355 break;
1356 case AF_INET6:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001357 conn->u.rtp.end.rtp_port = from_addr->u.sin6.sin6_port;
1358 break;
1359 default:
1360 OSMO_ASSERT(false);
1361 }
Philipp Maier97a93122021-05-07 22:50:31 +02001362 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1363 "loopback mode: implicitly using source address (%s:%u) as destination address\n",
1364 osmo_sockaddr_ntop(&from_addr->u.sa, ipbuf),
1365 conn->u.rtp.end.rtp_port);
Philipp Maier889fe7f2020-07-06 17:44:12 +02001366 }
1367 return mgcp_send_rtp(conn_src, msg);
1368 }
1369
1370 /* Forward to E1 */
1371 return mgcp_e1_send_rtp(conn->endp, conn->u.rtp.end.codec, msg);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001372}
1373
Philipp Maierdf5d2192018-01-24 11:39:32 +01001374/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1375 * \param[in] endp Endpoint on which the connection resides.
Philipp Maier08eb9352020-06-18 11:55:35 +02001376 * \param[in] conn Connection that is about to be removed (ignored). */
Philipp Maierdf5d2192018-01-24 11:39:32 +01001377void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1378{
1379 struct mgcp_conn *conn_cleanup;
1380
1381 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1382 * pointer to the destination connection, so that we do not have
1383 * to go through the list every time an RTP packet arrives. To prevent
1384 * a use-after-free situation we invalidate this information for all
1385 * connections present when one connection is removed from the
1386 * endpoint. */
1387 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
Pau Espin Pedrol6d0a59a2020-09-08 16:50:22 +02001388 if (conn_cleanup->priv == conn)
1389 conn_cleanup->priv = NULL;
Philipp Maierdf5d2192018-01-24 11:39:32 +01001390 }
1391}
1392
Philipp Maier0996a1e2020-06-10 15:27:14 +02001393/*! cleanup an endpoint when a connection on an E1 endpoint is removed.
1394 * \param[in] endp Endpoint on which the connection resides.
1395 * \param[in] conn Connection that is about to be removed (ignored). */
1396void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1397{
Philipp Maier889fe7f2020-07-06 17:44:12 +02001398 /* Cleanup tasks for E1 are the same as for regular endpoint. The
1399 * shut down of the E1 part is handled separately. */
1400 mgcp_cleanup_rtp_bridge_cb(endp, conn);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001401}
1402
Philipp Maier87bd9be2017-08-22 16:35:41 +02001403/* Handle incoming RTP data from NET */
1404static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1405{
1406 /* NOTE: This is a generic implementation. RTP data is received. In
1407 * case of loopback the data is just sent back to its origin. All
1408 * other cases implement endpoint specific behaviour (e.g. how is the
1409 * destination connection determined?). That specific behaviour is
1410 * implemented by the callback function that is called at the end of
1411 * the function */
1412
1413 struct mgcp_conn_rtp *conn_src;
1414 struct mgcp_endpoint *endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001415 struct osmo_sockaddr addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001416 socklen_t slen = sizeof(addr);
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001417 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001418 int ret;
1419 enum rtp_proto proto;
1420 struct osmo_rtp_msg_ctx *mc;
1421 struct msgb *msg = msgb_alloc(RTP_BUF_SIZE, "RTP-rx");
1422 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001423
1424 conn_src = (struct mgcp_conn_rtp *)fd->data;
1425 OSMO_ASSERT(conn_src);
1426 endp = conn_src->conn->endp;
1427 OSMO_ASSERT(endp);
1428
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001429 proto = (fd == &conn_src->end.rtp)? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001430
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001431 ret = recvfrom(fd->fd, msgb_data(msg), msg->data_len, 0, (struct sockaddr *)&addr.u.sa, &slen);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001432
1433 if (ret <= 0) {
1434 LOG_CONN_RTP(conn_src, LOGL_ERROR, "recvfrom error: %s\n", strerror(errno));
1435 rc = -1;
1436 goto out;
1437 }
1438
1439 msgb_put(msg, ret);
1440
1441 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "%s: rx %u bytes from %s:%u\n",
1442 proto == MGCP_PROTO_RTP ? "RTP" : "RTPC",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001443 msgb_length(msg), osmo_sockaddr_ntop(&addr.u.sa, ipbuf),
1444 osmo_sockaddr_port(&addr.u.sa));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001445
1446 if ((proto == MGCP_PROTO_RTP && check_rtp(conn_src, msg))
1447 || (proto == MGCP_PROTO_RTCP && check_rtcp(conn_src, msg))) {
1448 /* Logging happened in the two check_ functions */
1449 rc = -1;
1450 goto out;
1451 }
1452
Philipp Maierb3d14eb2021-05-20 14:18:52 +02001453 if (mgcp_is_rtp_dummy_payload(msg)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001454 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx dummy packet (dropped)\n");
1455 rc = 0;
1456 goto out;
1457 }
1458
1459 /* Since the msgb remains owned and freed by this function, the msg ctx data struct can just be on the stack and
1460 * needs not be allocated with the msgb. */
1461 mc = OSMO_RTP_MSG_CTX(msg);
1462 *mc = (struct osmo_rtp_msg_ctx){
1463 .proto = proto,
1464 .conn_src = conn_src,
1465 .from_addr = &addr,
1466 };
1467 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "msg ctx: %d %p %s\n",
1468 mc->proto, mc->conn_src,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001469 osmo_hexdump((void*)mc->from_addr,
1470 mc->from_addr->u.sa.sa_family == AF_INET6 ?
1471 sizeof(struct sockaddr_in6) :
1472 sizeof(struct sockaddr_in)));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001473
1474 /* Increment RX statistics */
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001475 rate_ctr_inc(rate_ctr_group_get_ctr(conn_src->rate_ctr_group, RTP_PACKETS_RX_CTR));
1476 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 +02001477 /* FIXME: count RTP and RTCP separately, also count IuUP payload-less separately */
1478
1479 /* Forward a copy of the RTP data to a debug ip/port */
1480 forward_data(fd->fd, &conn_src->tap_in, msg);
1481
1482 rc = rx_rtp(msg);
1483
1484out:
1485 msgb_free(msg);
1486 return rc;
1487}
1488
1489static int rx_rtp(struct msgb *msg)
1490{
1491 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1492 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001493 struct osmo_sockaddr *from_addr = mc->from_addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001494 struct mgcp_conn *conn = conn_src->conn;
1495 struct mgcp_trunk *trunk = conn->endp->trunk;
1496
1497 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx_rtp(%u bytes)\n", msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001498
Oliver Smithe36b7752019-01-22 16:31:36 +01001499 mgcp_conn_watchdog_kick(conn_src->conn);
1500
Philipp Maier228e5912019-03-05 13:56:59 +01001501 /* If AMR is configured for the ingress connection a conversion of the
1502 * framing mode (octet-aligned vs. bandwith-efficient is explicitly
1503 * define, then we check if the incoming payload matches that
1504 * expectation. */
1505 if (amr_oa_bwe_convert_indicated(conn_src->end.codec)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001506 int oa = amr_oa_check((char*)msgb_data(msg), msgb_length(msg));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +02001507 if (oa < 0)
1508 return -1;
1509 if (((bool)oa) != conn_src->end.codec->param.amr_octet_aligned)
Philipp Maier228e5912019-03-05 13:56:59 +01001510 return -1;
1511 }
1512
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001513 /* Check if the origin of the RTP packet seems plausible */
1514 if (!trunk->rtp_accept_all && check_rtp_origin(conn_src, from_addr))
1515 return -1;
1516
Philipp Maier87bd9be2017-08-22 16:35:41 +02001517 /* Execute endpoint specific implementation that handles the
1518 * dispatching of the RTP data */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001519 return conn->endp->type->dispatch_rtp_cb(msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001520}
1521
Philipp Maier87bd9be2017-08-22 16:35:41 +02001522/*! bind RTP port to osmo_fd.
Philipp Maier0b79d212020-06-18 12:02:49 +02001523 * \param[in] source_addr source (local) address to bind on.
1524 * \param[in] fd associated file descriptor.
1525 * \param[in] port to bind on.
Harald Welte55a92292021-04-28 19:06:34 +02001526 * \param[in] dscp IP DSCP value to use.
1527 * \param[in] prio socket priority to use.
Philipp Maier0b79d212020-06-18 12:02:49 +02001528 * \returns 0 on success, -1 on ERROR. */
Harald Welte55a92292021-04-28 19:06:34 +02001529int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port, uint8_t dscp,
1530 uint8_t prio)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001531{
Harald Welte8890dfa2017-11-17 15:09:30 +01001532 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001533
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001534 rc = osmo_sock_init2(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
Harald Welte55a92292021-04-28 19:06:34 +02001535 NULL, 0, OSMO_SOCK_F_BIND | OSMO_SOCK_F_DSCP(dscp) |
1536 OSMO_SOCK_F_PRIO(prio));
Harald Welte8890dfa2017-11-17 15:09:30 +01001537 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001538 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001539 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001540 return -1;
1541 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001542 fd->fd = rc;
1543 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001544
1545 return 0;
1546}
1547
Philipp Maier87bd9be2017-08-22 16:35:41 +02001548/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001549static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001550 struct mgcp_rtp_end *rtp_end, struct mgcp_endpoint *endp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001551{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001552 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1553 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1554
Harald Welte55a92292021-04-28 19:06:34 +02001555 if (mgcp_create_bind(source_addr, &rtp_end->rtp, rtp_end->local_port,
1556 cfg->endp_dscp, cfg->endp_priority) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001557 LOGPENDP(endp, DRTP, LOGL_ERROR,
1558 "failed to create RTP port: %s:%d\n",
1559 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001560 goto cleanup0;
1561 }
1562
Harald Welte55a92292021-04-28 19:06:34 +02001563 if (mgcp_create_bind(source_addr, &rtp_end->rtcp, rtp_end->local_port + 1,
1564 cfg->endp_dscp, cfg->endp_priority) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001565 LOGPENDP(endp, DRTP, LOGL_ERROR,
1566 "failed to create RTCP port: %s:%d\n",
1567 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001568 goto cleanup1;
1569 }
1570
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001571 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001572 LOGPENDP(endp, DRTP, LOGL_ERROR,
1573 "failed to register RTP port %d\n",
1574 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001575 goto cleanup2;
1576 }
1577
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001578 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001579 LOGPENDP(endp, DRTP, LOGL_ERROR,
1580 "failed to register RTCP port %d\n",
1581 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001582 goto cleanup3;
1583 }
1584
1585 return 0;
1586
1587cleanup3:
1588 osmo_fd_unregister(&rtp_end->rtp);
1589cleanup2:
1590 close(rtp_end->rtcp.fd);
1591 rtp_end->rtcp.fd = -1;
1592cleanup1:
1593 close(rtp_end->rtp.fd);
1594 rtp_end->rtp.fd = -1;
1595cleanup0:
1596 return -1;
1597}
1598
Philipp Maier87bd9be2017-08-22 16:35:41 +02001599/*! bind RTP port to endpoint/connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001600 * \param[in] endp endpoint that holds the RTP connection.
1601 * \param[in] rtp_port port number to bind on.
1602 * \param[in] conn associated RTP connection.
1603 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001604int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1605 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001606{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001607 char name[512];
1608 struct mgcp_rtp_end *end;
1609
Philipp Maier01d24a32017-11-21 17:26:09 +01001610 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001611 end = &conn->end;
1612
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001613 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001614 LOGPENDP(endp, DRTP, LOGL_ERROR, "%u was already bound on conn:%s\n",
1615 rtp_port, mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001616
1617 /* Double bindings should never occour! Since we always allocate
1618 * connections dynamically and free them when they are not
1619 * needed anymore, there must be no previous binding leftover.
1620 * Should there be a connection bound twice, we have a serious
1621 * problem and must exit immediately! */
1622 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001623 }
1624
1625 end->local_port = rtp_port;
Harald Weltec2a8f592020-10-19 13:25:41 +02001626 osmo_fd_setup(&end->rtp, -1, OSMO_FD_READ, rtp_data_net, conn, 0);
1627 osmo_fd_setup(&end->rtcp, -1, OSMO_FD_READ, rtp_data_net, conn, 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001628
Pau Espin Pedrol71d42e72020-09-03 14:20:07 +02001629 return bind_rtp(endp->cfg, conn->end.local_addr, end, endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001630}
1631
Philipp Maier87bd9be2017-08-22 16:35:41 +02001632/*! free allocated RTP and RTCP ports.
1633 * \param[in] end RTP end */
1634void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001635{
1636 if (end->rtp.fd != -1) {
1637 close(end->rtp.fd);
1638 end->rtp.fd = -1;
1639 osmo_fd_unregister(&end->rtp);
1640 }
1641
1642 if (end->rtcp.fd != -1) {
1643 close(end->rtcp.fd);
1644 end->rtcp.fd = -1;
1645 osmo_fd_unregister(&end->rtcp);
1646 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001647}