blob: 7261b9a969aad5782ac9cb45de974924fac785e2 [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* The protocol implementation */
3
4/*
5 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2012 by On-Waves
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <string.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <errno.h>
28#include <time.h>
29#include <limits.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020030#include <arpa/inet.h>
31
32#include <osmocom/core/msgb.h>
33#include <osmocom/core/select.h>
Philipp Maier1cb1e382017-11-02 17:16:04 +010034#include <osmocom/core/socket.h>
Philipp Maier4dba7692018-08-03 12:20:52 +020035#include <osmocom/core/byteswap.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020036#include <osmocom/netif/rtp.h>
Philipp Maier228e5912019-03-05 13:56:59 +010037#include <osmocom/netif/amr.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020038#include <osmocom/mgcp/mgcp.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020039#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier993ea6b2020-08-04 18:26:50 +020040#include <osmocom/mgcp/mgcp_network.h>
41#include <osmocom/mgcp/mgcp_protocol.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020042#include <osmocom/mgcp/mgcp_stat.h>
43#include <osmocom/mgcp/osmux.h>
44#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010045#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maierc66ab2c2020-06-02 20:55:34 +020046#include <osmocom/mgcp/mgcp_trunk.h>
Philipp Maier6931f9a2018-07-26 09:29:31 +020047#include <osmocom/mgcp/mgcp_codec.h>
Philipp Maierc3413882017-10-27 12:26:54 +020048#include <osmocom/mgcp/debug.h>
Philipp Maier9fc8a022019-02-20 12:26:52 +010049#include <osmocom/codec/codec.h>
Philipp Maier889fe7f2020-07-06 17:44:12 +020050#include <osmocom/mgcp/mgcp_e1.h>
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +010051#include <osmocom/mgcp/mgcp_iuup.h>
Philipp Maier993ea6b2020-08-04 18:26:50 +020052
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020053#define RTP_SEQ_MOD (1 << 16)
54#define RTP_MAX_DROPOUT 3000
55#define RTP_MAX_MISORDER 100
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020056
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +010057void rtpconn_rate_ctr_add(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp,
Harald Weltea48ff4a2020-03-08 14:45:08 +010058 int id, int inc)
59{
Pau Espin Pedroldaf5bce2022-09-22 19:14:24 +020060 struct rate_ctr_group *conn_stats = conn_rtp->ctrg;
Philipp Maierc66ab2c2020-06-02 20:55:34 +020061 struct rate_ctr_group *mgw_stats = endp->trunk->ratectr.all_rtp_conn_stats;
Harald Weltea48ff4a2020-03-08 14:45:08 +010062
Philipp Maierc66ab2c2020-06-02 20:55:34 +020063 /* add to both the per-connection and the global stats */
Pau Espin Pedrol907744e2021-06-04 17:57:34 +020064 rate_ctr_add(rate_ctr_group_get_ctr(conn_stats, id), inc);
65 rate_ctr_add(rate_ctr_group_get_ctr(mgw_stats, id), inc);
Harald Weltea48ff4a2020-03-08 14:45:08 +010066}
67
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +020068void rtpconn_rate_ctr_inc(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp, int id)
Harald Weltea48ff4a2020-03-08 14:45:08 +010069{
70 rtpconn_rate_ctr_add(conn_rtp, endp, id, 1);
71}
72
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +020073static int rx_rtp(struct msgb *msg);
74
Pau Espin Pedrolca280a12021-07-06 18:15:35 +020075bool mgcp_rtp_end_remote_addr_available(const struct mgcp_rtp_end *rtp_end)
76{
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +020077 return (osmo_sockaddr_port(&rtp_end->addr.u.sa) != 0) &&
78 (osmo_sockaddr_is_any(&rtp_end->addr) == 0);
Pau Espin Pedrolca280a12021-07-06 18:15:35 +020079}
80
Philipp Maier1cb1e382017-11-02 17:16:04 +010081/*! Determine the local rtp bind IP-address.
Philipp Maier0b79d212020-06-18 12:02:49 +020082 * \param[out] addr caller provided memory to store the resulting IP-Address.
83 * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters.
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +020084 * \ returns 0 on success, -1 if no local address could be provided.
Philipp Maier1cb1e382017-11-02 17:16:04 +010085 *
86 * The local bind IP-address is automatically selected by probing the
87 * IP-Address of the interface that is pointing towards the remote IP-Address,
88 * if no remote IP-Address is known yet, the statically configured
89 * IP-Addresses are used as fallback. */
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +020090int mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn)
Philipp Maier1cb1e382017-11-02 17:16:04 +010091{
Pau Espin Pedrol654b68d2022-10-05 10:37:49 +020092 const struct mgcp_endpoint *endp = conn->conn->endp;
93 const struct mgcp_config *cfg = endp->trunk->cfg;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +020094 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier1cb1e382017-11-02 17:16:04 +010095 int rc;
Pau Espin Pedrol051eb4d2022-10-04 12:50:28 +020096 bool rem_addr_set = osmo_sockaddr_is_any(&conn->end.addr) == 0;
Pau Espin Pedrol654b68d2022-10-05 10:37:49 +020097 const char *bind_addr;
Philipp Maier1cb1e382017-11-02 17:16:04 +010098
Pau Espin Pedrol479cf762022-09-07 19:42:54 +020099 /* Osmux: No smart IP addresses allocation is supported yet. Simply
100 * return the one set in VTY config: */
101 if (mgcp_conn_rtp_is_osmux(conn)) {
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200102 if (rem_addr_set) {
103 /* Match IP version with what was requested from remote: */
104 bind_addr = conn->end.addr.u.sa.sa_family == AF_INET6 ?
Pau Espin Pedrol36413c02022-10-12 17:58:17 +0200105 cfg->osmux.local_addr_v6 :
106 cfg->osmux.local_addr_v4;
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200107 } else {
108 /* Choose any of the bind addresses, preferring v6 over v4 if available: */
Pau Espin Pedrol36413c02022-10-12 17:58:17 +0200109 bind_addr = cfg->osmux.local_addr_v6;
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200110 if (!bind_addr)
Pau Espin Pedrol36413c02022-10-12 17:58:17 +0200111 bind_addr = cfg->osmux.local_addr_v4;
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200112 }
113 if (!bind_addr) {
114 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
115 "Unable to locate local Osmux address, check your configuration! v4=%u v6=%u remote_known=%s\n",
Pau Espin Pedrol36413c02022-10-12 17:58:17 +0200116 !!cfg->osmux.local_addr_v4,
117 !!cfg->osmux.local_addr_v6,
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200118 rem_addr_set ? osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf) : "no");
119 return -1;
120 }
121 LOGPCONN(conn->conn, DOSMUX, LOGL_DEBUG,
122 "Using configured osmux bind ip as local bind ip %s\n",
Pau Espin Pedrol479cf762022-09-07 19:42:54 +0200123 bind_addr);
124 osmo_strlcpy(addr, bind_addr, INET6_ADDRSTRLEN);
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200125 return 0;
Pau Espin Pedrol479cf762022-09-07 19:42:54 +0200126 }
127
Philipp Maier1cb1e382017-11-02 17:16:04 +0100128 /* Try probing the local IP-Address */
Pau Espin Pedrol654b68d2022-10-05 10:37:49 +0200129 if (cfg->net_ports.bind_addr_probe && rem_addr_set) {
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200130 rc = osmo_sock_local_ip(addr, osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Philipp Maier1cb1e382017-11-02 17:16:04 +0100131 if (rc < 0)
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200132 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
133 "local interface auto detection failed, using configured addresses...\n");
Philipp Maier1cb1e382017-11-02 17:16:04 +0100134 else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200135 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
136 "selected local rtp bind ip %s by probing using remote ip %s\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200137 addr, osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200138 return 0;
Philipp Maier1cb1e382017-11-02 17:16:04 +0100139 }
140 }
141
Pau Espin Pedrol479cf762022-09-07 19:42:54 +0200142 /* Select from preconfigured IP-Addresses. */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200143 if (rem_addr_set) {
Philipp Maier1cb1e382017-11-02 17:16:04 +0100144 /* Check there is a bind IP for the RTP traffic configured,
145 * if so, use that IP-Address */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200146 bind_addr = conn->end.addr.u.sa.sa_family == AF_INET6 ?
Pau Espin Pedrol654b68d2022-10-05 10:37:49 +0200147 cfg->net_ports.bind_addr_v6 :
148 cfg->net_ports.bind_addr_v4;
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200149 } else {
150 /* Choose any of the bind addresses, preferring v6 over v4 */
Pau Espin Pedrol654b68d2022-10-05 10:37:49 +0200151 bind_addr = cfg->net_ports.bind_addr_v6;
Eric2764bdb2021-08-23 22:11:47 +0200152 if (!strlen(bind_addr))
Pau Espin Pedrol654b68d2022-10-05 10:37:49 +0200153 bind_addr = cfg->net_ports.bind_addr_v4;
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200154 }
Eric2764bdb2021-08-23 22:11:47 +0200155 if (strlen(bind_addr)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200156 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
157 "using configured rtp bind ip as local bind ip %s\n",
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200158 bind_addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100159 } else {
160 /* No specific bind IP is configured for the RTP traffic, so
161 * assume the IP where we listen for incoming MGCP messages
162 * as bind IP */
Pau Espin Pedrol654b68d2022-10-05 10:37:49 +0200163 bind_addr = cfg->source_addr;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200164 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200165 "using mgcp bind ip as local rtp bind ip: %s\n", bind_addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100166 }
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200167 osmo_strlcpy(addr, bind_addr, INET6_ADDRSTRLEN);
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200168 return 0;
Philipp Maier1cb1e382017-11-02 17:16:04 +0100169}
170
Philipp Maier87bd9be2017-08-22 16:35:41 +0200171/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200172 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +0200173 * 1/codec_rate seconds. */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100174uint32_t mgcp_get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200175{
176 struct timespec tp;
177 uint64_t ret;
178
Philipp Maier87bd9be2017-08-22 16:35:41 +0200179 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200180 return 0;
181
182 memset(&tp, 0, sizeof(tp));
183 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maierc3413882017-10-27 12:26:54 +0200184 LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200185
186 /* convert it to 1/unit seconds */
187 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200188 ret *= codec_rate;
189 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200190
191 return ret;
192}
193
Philipp Maier87bd9be2017-08-22 16:35:41 +0200194/* Compute timestamp alignment error */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200195static int32_t ts_alignment_error(const struct mgcp_rtp_stream_state *sstate,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200196 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200197{
198 int32_t timestamp_delta;
199
200 if (ptime == 0)
201 return 0;
202
203 /* Align according to: T - Tlast = k * Tptime */
204 timestamp_delta = timestamp - sstate->last_timestamp;
205
206 return timestamp_delta % ptime;
207}
208
Philipp Maier87bd9be2017-08-22 16:35:41 +0200209/* Check timestamp and sequence number for plausibility */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200210static int check_rtp_timestamp(const struct mgcp_endpoint *endp,
211 const struct mgcp_rtp_state *state,
212 const struct mgcp_rtp_stream_state *sstate,
213 const struct mgcp_rtp_end *rtp_end,
214 const struct osmo_sockaddr *addr,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200215 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200216 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200217{
218 int32_t tsdelta;
219 int32_t timestamp_error;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200220 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200221
222 /* Not fully intialized, skip */
223 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
224 return 0;
225
226 if (seq == sstate->last_seq) {
227 if (timestamp != sstate->last_timestamp) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200228 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200229 LOGPENDP(endp, DRTP, LOGL_ERROR,
230 "The %s timestamp delta is != 0 but the sequence "
231 "number %d is the same, "
232 "TS offset: %d, SeqNo offset: %d "
233 "on SSRC: %u timestamp: %u "
234 "from %s:%d\n",
235 text, seq,
236 state->patch.timestamp_offset, state->patch.seq_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200237 sstate->ssrc, timestamp,
238 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
239 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200240 }
241 return 0;
242 }
243
244 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200245 (int32_t)(timestamp - sstate->last_timestamp) /
246 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200247
248 if (tsdelta == 0) {
249 /* Don't update *tsdelta_out */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200250 LOGPENDP(endp, DRTP, LOGL_NOTICE,
251 "The %s timestamp delta is %d "
252 "on SSRC: %u timestamp: %u "
253 "from %s:%d\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200254 text, tsdelta, sstate->ssrc, timestamp,
255 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
256 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200257
258 return 0;
259 }
260
261 if (sstate->last_tsdelta != tsdelta) {
262 if (sstate->last_tsdelta) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200263 LOGPENDP(endp, DRTP, LOGL_INFO,
264 "The %s timestamp delta changes from %d to %d "
265 "on SSRC: %u timestamp: %u from %s:%d\n",
266 text, sstate->last_tsdelta, tsdelta,
267 sstate->ssrc, timestamp,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200268 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
269 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200270 }
271 }
272
273 if (tsdelta_out)
274 *tsdelta_out = tsdelta;
275
276 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200277 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200278
279 if (timestamp_error) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200280 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200281 LOGPENDP(endp, DRTP, LOGL_NOTICE,
282 "The %s timestamp has an alignment error of %d "
283 "on SSRC: %u "
284 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
285 "from %s:%d. ptime: %d\n",
286 text, timestamp_error,
287 sstate->ssrc,
288 (int16_t)(seq - sstate->last_seq),
289 (int32_t)(timestamp - sstate->last_timestamp),
290 tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200291 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
292 osmo_sockaddr_port(&addr->u.sa),
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200293 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200294 }
295 return 1;
296}
297
298/* Set the timestamp offset according to the packet duration. */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200299static int adjust_rtp_timestamp_offset(const struct mgcp_endpoint *endp,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200300 struct mgcp_rtp_state *state,
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200301 const struct mgcp_rtp_end *rtp_end,
302 const struct osmo_sockaddr *addr,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200303 int16_t delta_seq, uint32_t in_timestamp,
304 bool marker_bit)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200305{
306 int32_t tsdelta = state->packet_duration;
307 int timestamp_offset;
308 uint32_t out_timestamp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200309 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200310
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200311 if (marker_bit) {
312 /* If RTP pkt contains marker bit, the timestamps are not longer
313 * in sync, so we can erase timestamp offset patching. */
314 state->patch.timestamp_offset = 0;
315 return 0;
316 }
317
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200318 if (tsdelta == 0) {
319 tsdelta = state->out_stream.last_tsdelta;
320 if (tsdelta != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200321 LOGPENDP(endp, DRTP, LOGL_NOTICE,
322 "A fixed packet duration is not available, "
323 "using last output timestamp delta instead: %d "
324 "from %s:%d\n", tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200325 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
326 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200327 } else {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200328 tsdelta = rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200329 LOGPENDP(endp, DRTP, LOGL_NOTICE,
330 "Fixed packet duration and last timestamp delta "
331 "are not available, "
332 "using fixed 20ms instead: %d "
333 "from %s:%d\n", tsdelta,
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
339 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
340 timestamp_offset = out_timestamp - in_timestamp;
341
Harald Welte33381352017-12-25 09:44:26 +0100342 if (state->patch.timestamp_offset != timestamp_offset) {
343 state->patch.timestamp_offset = timestamp_offset;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200344 LOGPENDP(endp, DRTP, LOGL_NOTICE,
345 "Timestamp offset change on SSRC: %u "
346 "SeqNo delta: %d, TS offset: %d, "
347 "from %s:%d\n", state->in_stream.ssrc,
348 delta_seq, state->patch.timestamp_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200349 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
350 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200351 }
352
353 return timestamp_offset;
354}
355
356/* Set the timestamp offset according to the packet duration. */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200357static int align_rtp_timestamp_offset(const struct mgcp_endpoint *endp,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200358 struct mgcp_rtp_state *state,
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200359 const struct mgcp_rtp_end *rtp_end,
360 const struct osmo_sockaddr *addr,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200361 uint32_t timestamp, bool marker_bit)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200362{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200363 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200364 int ts_error = 0;
365 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200366 int ptime = state->packet_duration;
367
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200368 if (marker_bit) {
369 /* If RTP pkt contains marker bit, the timestamps are not longer
370 * in sync, so no alignment is needed. */
371 return 0;
372 }
373
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200374 /* Align according to: T + Toffs - Tlast = k * Tptime */
375
Philipp Maier87bd9be2017-08-22 16:35:41 +0200376 ts_error = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100377 timestamp + state->patch.timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200378
Philipp Maier87bd9be2017-08-22 16:35:41 +0200379 /* If there is an alignment error, we have to compensate it */
380 if (ts_error) {
Harald Welte33381352017-12-25 09:44:26 +0100381 state->patch.timestamp_offset += ptime - ts_error;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200382 LOGPENDP(endp, DRTP, LOGL_NOTICE,
383 "Corrected timestamp alignment error of %d on SSRC: %u "
384 "new TS offset: %d, "
385 "from %s:%d\n",
386 ts_error, state->in_stream.ssrc,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200387 state->patch.timestamp_offset,
388 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
389 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200390 }
391
Philipp Maier87bd9be2017-08-22 16:35:41 +0200392 /* Check we really managed to compensate the timestamp
393 * offset. There should not be any remaining error, failing
Harald Welte1d1b98f2017-12-25 10:03:40 +0100394 * here would point to a serous problem with the alignment
395 * error computation function */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200396 ts_check = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100397 timestamp + state->patch.timestamp_offset);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200398 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200399
Philipp Maier87bd9be2017-08-22 16:35:41 +0200400 /* Return alignment error before compensation */
401 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200402}
403
Philipp Maier87bd9be2017-08-22 16:35:41 +0200404/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200405 * \param[in] associated endpoint.
406 * \param[in] destination RTP end.
407 * \param[in,out] pointer to buffer with voice data.
408 * \param[in] voice data length.
409 * \param[in] maximum size of caller provided voice data buffer.
410 * \returns ignores input parameters, return always 0. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200411int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
412 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200413 char *data, int *len, int buf_size)
414{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200415 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200416 return 0;
417}
418
Philipp Maier87bd9be2017-08-22 16:35:41 +0200419/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200420 * \param[in] associated endpoint.
421 * \param[in] destination RTP connnection.
422 * \param[in] source RTP connection.
423 * \returns ignores input parameters, return always 0. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200424int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
Philipp Maieracc10352018-07-19 18:07:57 +0200425 struct mgcp_conn_rtp *conn_dst,
426 struct mgcp_conn_rtp *conn_src)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200427{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200428 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200429 return 0;
430}
431
432void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
Philipp Maier58128252019-03-06 11:28:18 +0100433 const struct mgcp_rtp_codec **codec,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200434 const char **fmtp_extra,
435 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200436{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200437 LOGPENDP(endp, DRTP, LOGL_DEBUG, "conn:%s using format defaults\n",
438 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200439
Philipp Maier58128252019-03-06 11:28:18 +0100440 *codec = conn->end.codec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200441 *fmtp_extra = conn->end.fmtp_extra;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200442}
443
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200444void mgcp_rtp_annex_count(const struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200445 struct mgcp_rtp_state *state, const uint16_t seq,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200446 const int32_t transit, const uint32_t ssrc,
447 const bool marker_bit)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200448{
449 int32_t d;
450
451 /* initialize or re-initialize */
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200452 if (!state->stats.initialized || state->stats.ssrc != ssrc || marker_bit) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100453 state->stats.initialized = 1;
454 state->stats.base_seq = seq;
455 state->stats.max_seq = seq - 1;
456 state->stats.ssrc = ssrc;
457 state->stats.jitter = 0;
458 state->stats.transit = transit;
459 state->stats.cycles = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200460 } else {
461 uint16_t udelta;
462
Philipp Maier87bd9be2017-08-22 16:35:41 +0200463 /* The below takes the shape of the validation of
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200464 * Appendix A. Check if there is something weird with
465 * the sequence number, otherwise check for a wrap
466 * around in the sequence number.
467 * It can't wrap during the initialization so let's
468 * skip it here. The Appendix A probably doesn't have
Philipp Maier87bd9be2017-08-22 16:35:41 +0200469 * this issue because of the probation. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100470 udelta = seq - state->stats.max_seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200471 if (udelta < RTP_MAX_DROPOUT) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100472 if (seq < state->stats.max_seq)
473 state->stats.cycles += RTP_SEQ_MOD;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200474 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200475 LOGPENDP(endp, DRTP, LOGL_NOTICE,
476 "RTP seqno made a very large jump on delta: %u\n",
477 udelta);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200478 }
479 }
480
Philipp Maier87bd9be2017-08-22 16:35:41 +0200481 /* Calculate the jitter between the two packages. The TS should be
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200482 * taken closer to the read function. This was taken from the
483 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
Philipp Maier87bd9be2017-08-22 16:35:41 +0200484 * resolution. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100485 d = transit - state->stats.transit;
486 state->stats.transit = transit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200487 if (d < 0)
488 d = -d;
Harald Welte49e3d5a2017-12-25 09:47:57 +0100489 state->stats.jitter += d - ((state->stats.jitter + 8) >> 4);
490 state->stats.max_seq = seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200491}
492
Philipp Maier6931f9a2018-07-26 09:29:31 +0200493/* There may be different payload type numbers negotiated for two connections.
494 * Patch the payload type of an RTP packet so that it uses the payload type
495 * that is valid for the destination connection (conn_dst) */
496static int mgcp_patch_pt(struct mgcp_conn_rtp *conn_src,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200497 struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200498{
499 struct rtp_hdr *rtp_hdr;
500 uint8_t pt_in;
501 int pt_out;
502
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200503 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
504 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
505 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200506 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200507 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200508
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200509 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier6931f9a2018-07-26 09:29:31 +0200510
511 pt_in = rtp_hdr->payload_type;
512 pt_out = mgcp_codec_pt_translate(conn_src, conn_dst, pt_in);
513 if (pt_out < 0)
514 return -EINVAL;
515
516 rtp_hdr->payload_type = (uint8_t) pt_out;
517 return 0;
518}
519
Philipp Maier87bd9be2017-08-22 16:35:41 +0200520/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200521 * some of the supported endpoints (e.g. the nanoBTS) can only handle
522 * one source and this code will patch RTP header to appear as if there
523 * is only one source.
524 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200525 * we receive will be seen as a switch in streams. */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200526void mgcp_patch_and_count(const struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200527 struct mgcp_rtp_state *state,
528 struct mgcp_rtp_end *rtp_end,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200529 struct osmo_sockaddr *addr, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200530{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200531 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200532 uint32_t arrival_time;
533 int32_t transit;
534 uint16_t seq;
535 uint32_t timestamp, ssrc;
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200536 bool marker_bit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200537 struct rtp_hdr *rtp_hdr;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200538 int payload = rtp_end->codec->payload_type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200539 unsigned int len = msgb_length(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200540
541 if (len < sizeof(*rtp_hdr))
542 return;
543
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200544 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200545 seq = ntohs(rtp_hdr->sequence);
546 timestamp = ntohl(rtp_hdr->timestamp);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100547 arrival_time = mgcp_get_current_ts(rtp_end->codec->rate);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200548 ssrc = ntohl(rtp_hdr->ssrc);
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200549 marker_bit = !!rtp_hdr->marker;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200550 transit = arrival_time - timestamp;
551
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200552 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc, marker_bit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200553
554 if (!state->initialized) {
555 state->initialized = 1;
556 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100557 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200558 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200559 state->packet_duration =
560 mgcp_rtp_packet_duration(endp, rtp_end);
Philipp Maier0ec1d4e2018-05-16 11:09:42 +0200561 state->out_stream.last_seq = seq - 1;
562 state->out_stream.ssrc = state->patch.orig_ssrc = ssrc;
563 state->out_stream.last_tsdelta = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200564 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200565 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200566 LOGPENDP(endp, DRTP, LOGL_INFO,
567 "initializing stream, SSRC: %u timestamp: %u "
568 "pkt-duration: %d, from %s:%d\n",
569 state->in_stream.ssrc,
570 state->patch.seq_offset, state->packet_duration,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200571 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
572 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200573 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200574 state->packet_duration =
Philipp Maierbc0346e2018-06-07 09:52:16 +0200575 rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200576 LOGPENDP(endp, DRTP, LOGL_NOTICE,
577 "fixed packet duration is not available, "
578 "using fixed 20ms instead: %d from %s:%d\n",
579 state->packet_duration,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200580 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
581 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200582 }
583 } else if (state->in_stream.ssrc != ssrc) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200584 LOGPENDP(endp, DRTP, LOGL_NOTICE,
585 "SSRC changed: %u -> %u "
586 "from %s:%d\n",
587 state->in_stream.ssrc, rtp_hdr->ssrc,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200588 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
589 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200590
591 state->in_stream.ssrc = ssrc;
592 if (rtp_end->force_constant_ssrc) {
593 int16_t delta_seq;
594
595 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100596 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200597 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200598
599 /* Estimate number of packets that would have been sent */
600 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200601 (arrival_time - state->in_stream.last_arrival_time
602 + state->packet_duration / 2) /
603 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200604
605 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200606 delta_seq, timestamp, marker_bit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200607
Pau Espin Pedrol4c77e9b2021-07-07 12:12:58 +0200608 state->patch.patch_ssrc = true;
Harald Welte33381352017-12-25 09:44:26 +0100609 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200610 if (rtp_end->force_constant_ssrc != -1)
611 rtp_end->force_constant_ssrc -= 1;
612
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200613 LOGPENDP(endp, DRTP, LOGL_NOTICE,
614 "SSRC patching enabled, SSRC: %u "
615 "SeqNo offset: %d, TS offset: %d "
616 "from %s:%d\n", state->in_stream.ssrc,
617 state->patch.seq_offset, state->patch.timestamp_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200618 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
619 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200620 }
621
622 state->in_stream.last_tsdelta = 0;
623 } else {
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200624 if (!marker_bit) {
625 /* Compute current per-packet timestamp delta */
626 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
627 addr, seq, timestamp, "input",
628 &state->in_stream.last_tsdelta);
629 } else {
630 state->in_stream.last_tsdelta = 0;
631 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200632
Harald Welte33381352017-12-25 09:44:26 +0100633 if (state->patch.patch_ssrc)
634 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200635 }
636
637 /* Save before patching */
638 state->in_stream.last_timestamp = timestamp;
639 state->in_stream.last_seq = seq;
640 state->in_stream.last_arrival_time = arrival_time;
641
642 if (rtp_end->force_aligned_timing &&
643 state->out_stream.ssrc == ssrc && state->packet_duration)
644 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200645 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200646 timestamp, marker_bit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200647
648 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100649 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200650 rtp_hdr->ssrc = htonl(ssrc);
651
652 /* Apply the offset and store it back to the packet.
653 * This won't change anything if the offset is 0, so the conditional is
654 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100655 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200656 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100657 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200658 rtp_hdr->timestamp = htonl(timestamp);
659
660 /* Check again, whether the timestamps are still valid */
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200661 if (!marker_bit) {
662 if (state->out_stream.ssrc == ssrc)
663 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
664 addr, seq, timestamp, "output",
665 &state->out_stream.last_tsdelta);
666 } else {
667 state->out_stream.last_tsdelta = 0;
668 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200669
670 /* Save output values */
671 state->out_stream.last_seq = seq;
672 state->out_stream.last_timestamp = timestamp;
673 state->out_stream.ssrc = ssrc;
674
675 if (payload < 0)
676 return;
677
678#if 0
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200679 LOGPENDP(endp, DRTP, LOGL_DEBUG, "payload hdr payload %u -> endp payload %u\n",
680 rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200681 rtp_hdr->payload_type = payload;
682#endif
683}
684
Philipp Maier9fc8a022019-02-20 12:26:52 +0100685/* There are different dialects used to format and transfer voice data. When
686 * the receiving end expects GSM-HR data to be formated after RFC 5993, this
687 * function is used to convert between RFC 5993 and TS 101318, which we normally
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200688 * use.
689 * Return 0 on sucess, negative on errors like invalid data length. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200690static int rfc5993_hr_convert(struct mgcp_endpoint *endp, struct msgb *msg)
Philipp Maier9fc8a022019-02-20 12:26:52 +0100691{
Philipp Maier9fc8a022019-02-20 12:26:52 +0100692 struct rtp_hdr *rtp_hdr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200693 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
Pau Espin Pedrol08077712022-10-19 19:06:45 +0200694 LOGPENDP(endp, DRTP, LOGL_ERROR, "RTP packet too short (%d < %zu)\n",
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200695 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200696 return -EINVAL;
697 }
698
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200699 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100700
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200701 if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr)) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100702 /* TS 101318 encoding => RFC 5993 encoding */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200703 msgb_put(msg, 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100704 memmove(rtp_hdr->data + 1, rtp_hdr->data, GSM_HR_BYTES);
705 rtp_hdr->data[0] = 0x00;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200706 } else if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr) + 1) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100707 /* RFC 5993 encoding => TS 101318 encoding */
708 memmove(rtp_hdr->data, rtp_hdr->data + 1, GSM_HR_BYTES);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200709 msgb_trim(msg, msgb_length(msg) - 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100710 } else {
711 /* It is possible that multiple payloads occur in one RTP
712 * packet. This is not supported yet. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200713 LOGPENDP(endp, DRTP, LOGL_ERROR,
714 "cannot figure out how to convert RTP packet\n");
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200715 return -ENOTSUP;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100716 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200717 return 0;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100718}
719
Philipp Maier228e5912019-03-05 13:56:59 +0100720/* For AMR RTP two framing modes are defined RFC3267. There is a bandwith
721 * efficient encoding scheme where all fields are packed together one after
722 * another and an octet aligned mode where all fields are aligned to octet
723 * boundaries. This function is used to convert between the two modes */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200724static int amr_oa_bwe_convert(struct mgcp_endpoint *endp, struct msgb *msg,
Philipp Maier228e5912019-03-05 13:56:59 +0100725 bool target_is_oa)
726{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200727 /* NOTE: the msgb has an allocated length of RTP_BUF_SIZE, so there is
Philipp Maier228e5912019-03-05 13:56:59 +0100728 * plenty of space available to store the slightly larger, converted
729 * data */
Philipp Maier228e5912019-03-05 13:56:59 +0100730 struct rtp_hdr *rtp_hdr;
731 unsigned int payload_len;
732 int rc;
733
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200734 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
735 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 +0200736 return -EINVAL;
737 }
738
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200739 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier228e5912019-03-05 13:56:59 +0100740
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200741 payload_len = msgb_length(msg) - sizeof(struct rtp_hdr);
Philipp Maier228e5912019-03-05 13:56:59 +0100742
743 if (osmo_amr_is_oa(rtp_hdr->data, payload_len)) {
744 if (!target_is_oa)
745 /* Input data is oa an target format is bwe
746 * ==> convert */
747 rc = osmo_amr_oa_to_bwe(rtp_hdr->data, payload_len);
748 else
749 /* Input data is already bew, but we accept it anyway
750 * ==> no conversion needed */
751 rc = payload_len;
752 } else {
753 if (target_is_oa)
754 /* Input data is bwe an target format is oa
755 * ==> convert */
756 rc = osmo_amr_bwe_to_oa(rtp_hdr->data, payload_len,
757 RTP_BUF_SIZE);
758 else
759 /* Input data is already oa, but we accept it anyway
760 * ==> no conversion needed */
761 rc = payload_len;
762 }
763 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200764 LOGPENDP(endp, DRTP, LOGL_ERROR,
765 "AMR RTP packet conversion failed\n");
Philipp Maier228e5912019-03-05 13:56:59 +0100766 return -EINVAL;
767 }
768
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200769 return msgb_trim(msg, rc + sizeof(struct rtp_hdr));
Philipp Maier228e5912019-03-05 13:56:59 +0100770}
771
772/* Check if a conversion between octet-aligned and bandwith-efficient mode is
773 * indicated. */
774static bool amr_oa_bwe_convert_indicated(struct mgcp_rtp_codec *codec)
775{
776 if (codec->param_present == false)
777 return false;
778 if (!codec->param.amr_octet_aligned_present)
779 return false;
780 if (strcmp(codec->subtype_name, "AMR") != 0)
781 return false;
782 return true;
783}
784
785
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200786/* Return whether an RTP packet with AMR payload is in octet-aligned mode.
787 * Return 0 if in bandwidth-efficient mode, 1 for octet-aligned mode, and negative if the RTP data is invalid. */
788static int amr_oa_check(char *data, int len)
Philipp Maier228e5912019-03-05 13:56:59 +0100789{
790 struct rtp_hdr *rtp_hdr;
791 unsigned int payload_len;
792
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200793 if (len < sizeof(struct rtp_hdr))
794 return -EINVAL;
795
Philipp Maier228e5912019-03-05 13:56:59 +0100796 rtp_hdr = (struct rtp_hdr *)data;
797
798 payload_len = len - sizeof(struct rtp_hdr);
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200799 if (payload_len < sizeof(struct amr_hdr))
800 return -EINVAL;
Philipp Maier228e5912019-03-05 13:56:59 +0100801
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200802 return osmo_amr_is_oa(rtp_hdr->data, payload_len) ? 1 : 0;
Philipp Maier228e5912019-03-05 13:56:59 +0100803}
804
Philipp Maier87bd9be2017-08-22 16:35:41 +0200805/* Forward data to a debug tap. This is debug function that is intended for
806 * debugging the voice traffic with tools like gstreamer */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100807void forward_data_tap(int fd, struct mgcp_rtp_tap *tap, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200808{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100809 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200810
Philipp Maiere6f172d2017-11-07 12:00:01 +0100811 if (!tap->enabled)
812 return;
813
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200814 rc = sendto(fd, msgb_data(msg), msgb_length(msg), 0, (struct sockaddr *)&tap->forward,
Philipp Maiere6f172d2017-11-07 12:00:01 +0100815 sizeof(tap->forward));
816
817 if (rc < 0)
818 LOGP(DRTP, LOGL_ERROR,
819 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200820}
821
Philipp Maier889fe7f2020-07-06 17:44:12 +0200822/* Generate an RTP header if it is missing */
823static void gen_rtp_header(struct msgb *msg, struct mgcp_rtp_end *rtp_end,
824 struct mgcp_rtp_state *state)
825{
826 struct rtp_hdr *hdr = (struct rtp_hdr *)msgb_data(msg);
827
828 if (hdr->version > 0)
829 return;
830
831 hdr->version = 2;
832 hdr->payload_type = rtp_end->codec->payload_type;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100833 hdr->timestamp = osmo_htonl(mgcp_get_current_ts(rtp_end->codec->rate));
Philipp Maier889fe7f2020-07-06 17:44:12 +0200834 hdr->sequence = osmo_htons(state->alt_rtp_tx_sequence);
835 hdr->ssrc = state->alt_rtp_tx_ssrc;
836}
837
Philipp Maier87bd9be2017-08-22 16:35:41 +0200838/* Check if the origin (addr) matches the address/port data of the RTP
839 * connections. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200840static int check_rtp_origin(struct mgcp_conn_rtp *conn, struct osmo_sockaddr *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200841{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200842 char ipbuf[INET6_ADDRSTRLEN];
843
Pau Espin Pedrol051eb4d2022-10-04 12:50:28 +0200844 if (osmo_sockaddr_is_any(&conn->end.addr) != 0) {
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200845 switch (conn->conn->mode) {
846 case MGCP_CONN_LOOPBACK:
847 /* HACK: for IuUP, we want to reply with an IuUP Initialization ACK upon the first RTP
848 * message received. We currently hackishly accomplish that by putting the endpoint in
849 * loopback mode and patching over the looped back RTP message to make it look like an
850 * ack. We don't know the femto cell's IP address and port until the RAB Assignment
851 * Response is received, but the nano3G expects an IuUP Initialization Ack before it even
852 * sends the RAB Assignment Response. Hence, if the remote address is 0.0.0.0 and the
853 * MGCP port is in loopback mode, allow looping back the packet to any source. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200854 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
855 "In loopback mode and remote address not set:"
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200856 " allowing data from address: %s\n",
857 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200858 return 0;
859
860 default:
861 /* Receiving early media before the endpoint is configured. Instead of logging
862 * this as an error that occurs on every call, keep it more low profile to not
863 * confuse humans with expected errors. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200864 LOGPCONN(conn->conn, DRTP, LOGL_INFO,
865 "Rx RTP from %s, but remote address not set:"
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200866 " dropping early media\n",
867 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200868 return -1;
869 }
Neels Hofmeyr0063ca22018-07-23 18:12:16 +0200870 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200871
Philipp Maier87bd9be2017-08-22 16:35:41 +0200872 /* Note: Check if the inbound RTP data comes from the same host to
873 * which we send our outgoing RTP traffic. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200874 if (conn->end.addr.u.sa.sa_family != addr->u.sa.sa_family ||
875 (conn->end.addr.u.sa.sa_family == AF_INET &&
876 conn->end.addr.u.sin.sin_addr.s_addr != addr->u.sin.sin_addr.s_addr) ||
877 (conn->end.addr.u.sa.sa_family == AF_INET6 &&
878 memcmp(&conn->end.addr.u.sin6.sin6_addr, &addr->u.sin6.sin6_addr,
879 sizeof(struct in6_addr)))) {
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 address: %s, ",
882 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Philipp Maierc3413882017-10-27 12:26:54 +0200883 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200884 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200885 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200886 return -1;
887 }
888
Philipp Maier87bd9be2017-08-22 16:35:41 +0200889 /* Note: Usually the remote remote port of the data we receive will be
890 * the same as the remote port where we transmit outgoing RTP traffic
891 * to (set by MDCX). We use this to check the origin of the data for
892 * plausibility. */
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200893 if (osmo_sockaddr_port(&conn->end.addr.u.sa) != osmo_sockaddr_port(&addr->u.sa) &&
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200894 ntohs(conn->end.rtcp_port) != osmo_sockaddr_port(&addr->u.sa)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200895 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200896 "data from wrong source port: %d, ",
897 osmo_sockaddr_port(&addr->u.sa));
Philipp Maierc3413882017-10-27 12:26:54 +0200898 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200899 "expected: %d for RTP or %d for RTCP\n",
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200900 osmo_sockaddr_port(&conn->end.addr.u.sa), ntohs(conn->end.rtcp_port));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200901 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200902 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200903 }
904
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200905 return 0;
906}
907
Philipp Maier87bd9be2017-08-22 16:35:41 +0200908/* Check the if the destination address configuration of an RTP connection
909 * makes sense */
910static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200911{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200912 char ipbuf[INET6_ADDRSTRLEN];
Pau Espin Pedrol051eb4d2022-10-04 12:50:28 +0200913 bool ip_is_any = osmo_sockaddr_is_any(&conn->end.addr) != 0;
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200914 uint16_t port = osmo_sockaddr_port(&conn->end.addr.u.sa);
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200915
Philipp Maiere6df0e42018-05-29 14:03:06 +0200916 /* Note: it is legal to create a connection but never setting a port
917 * and IP-address for outgoing data. */
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200918 if (ip_is_any && port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200919 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
Neels Hofmeyra4b677c2021-07-07 23:38:23 +0200920 "destination IP-address and rtp port is not (yet) known (%s:%u)\n",
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200921 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), port);
Philipp Maiere6df0e42018-05-29 14:03:06 +0200922 return -1;
923 }
924
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200925 if (ip_is_any) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200926 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
927 "destination IP-address is invalid (%s:%u)\n",
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200928 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), port);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200929 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200930 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200931
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200932 if (port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200933 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
934 "destination rtp port is invalid (%s:%u)\n",
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200935 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), port);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200936 return -1;
937 }
938
939 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200940}
941
Philipp Maier4dba7692018-08-03 12:20:52 +0200942/* Do some basic checks to make sure that the RTCP packets we are going to
943 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200944static int check_rtcp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +0200945{
946 struct rtcp_hdr *hdr;
947 unsigned int len;
948 uint8_t type;
949
950 /* RTPC packets that are just a header without data do not make
951 * any sense. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200952 if (msgb_length(msg) < sizeof(struct rtcp_hdr)) {
953 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP packet too short (%u < %zu)\n",
954 msgb_length(msg), sizeof(struct rtcp_hdr));
Philipp Maier4dba7692018-08-03 12:20:52 +0200955 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200956 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200957
958 /* Make sure that the length of the received packet does not exceed
959 * the available buffer size */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200960 hdr = (struct rtcp_hdr *)msgb_data(msg);
Philipp Maier4dba7692018-08-03 12:20:52 +0200961 len = (osmo_ntohs(hdr->length) + 1) * 4;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200962 if (len > msgb_length(msg)) {
963 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header length exceeds packet size (%u > %u)\n",
964 len, msgb_length(msg));
Philipp Maier4dba7692018-08-03 12:20:52 +0200965 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200966 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200967
968 /* Make sure we accept only packets that have a proper packet type set
969 * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
970 type = hdr->type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200971 if ((type < 192 || type > 195) && (type < 200 || type > 213)) {
972 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header: invalid type: %u\n", type);
Philipp Maier4dba7692018-08-03 12:20:52 +0200973 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200974 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200975
976 return 0;
977}
978
979/* Do some basic checks to make sure that the RTP packets we are going to
980 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200981static int check_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +0200982{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200983 size_t min_size = sizeof(struct rtp_hdr);
984 if (msgb_length(msg) < min_size) {
985 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
986 msgb_length(msg), min_size);
987 return -1;
988 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200989
990 /* FIXME: Add more checks, the reason why we do not check more than
991 * the length is because we currently handle IUUP packets as RTP
992 * packets, so they must pass this check, if we weould be more
993 * strict here, we would possibly break 3G. (see also FIXME note
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100994 * below.*/
Philipp Maier4dba7692018-08-03 12:20:52 +0200995
996 return 0;
997}
998
Philipp Maier87bd9be2017-08-22 16:35:41 +0200999/* Send RTP data. Possible options are standard RTP packet
1000 * transmission or trsmission via an osmux connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001001static int mgcp_send_rtp(struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001002{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001003 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1004 enum rtp_proto proto = mc->proto;
1005 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001006 struct mgcp_endpoint *endp = conn_src->conn->endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001007
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001008 LOGPENDP(endp, DRTP, LOGL_DEBUG, "destin conn:%s\n",
1009 mgcp_conn_dump(conn_dst->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001010
1011 /* Before we try to deliver the packet, we check if the destination
1012 * port and IP-Address make sense at all. If not, we will be unable
1013 * to deliver the packet. */
1014 if (check_rtp_destin(conn_dst) != 0)
1015 return -1;
1016
1017 /* Depending on the RTP connection type, deliver the RTP packet to the
1018 * destination connection. */
1019 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001020 case MGCP_RTP_DEFAULT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001021 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1022 "endpoint type is MGCP_RTP_DEFAULT, "
1023 "using mgcp_send() to forward data directly\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001024 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001025 mc->from_addr, msg, conn_src, conn_dst);
Pau Espin Pedrol9d939b62022-10-03 16:59:20 +02001026 case MGCP_RTP_OSMUX:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001027 LOGPENDP(endp, DRTP, LOGL_DEBUG,
Pau Espin Pedrol9d939b62022-10-03 16:59:20 +02001028 "endpoint type is MGCP_RTP_OSMUX, "
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001029 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n");
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001030 return osmux_xfrm_to_osmux((char*)msgb_data(msg), msgb_length(msg), conn_dst);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001031 case MGCP_RTP_IUUP:
1032 if (proto == MGCP_PROTO_RTP) {
1033 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1034 "endpoint type is MGCP_RTP_IUUP, "
1035 "using mgcp_conn_iuup_send_rtp() to forward data over IuUP\n");
1036 return mgcp_conn_iuup_send_rtp(conn_src, conn_dst, msg);
1037 }
1038 /* RTCP: we forward as usual for regular RTP connection */
1039 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1040 "endpoint type is MGCP_RTP_IUUP and proto!=MGCP_PROTO_RTP, "
1041 "using mgcp_send() to forward data directly\n");
1042 return mgcp_send(endp, false,
1043 mc->from_addr, msg, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001044 }
1045
Philipp Maier87bd9be2017-08-22 16:35:41 +02001046 /* If the data has not been handled/forwarded until here, it will
1047 * be discarded, this should not happen, normally the MGCP type
1048 * should be properly set */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001049 LOGPENDP(endp, DRTP, LOGL_ERROR, "bad MGCP type -- data discarded!\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001050
1051 return -1;
1052}
1053
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001054/*! send udp packet.
1055 * \param[in] fd associated file descriptor.
1056 * \param[in] addr destination ip-address.
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001057 * \param[in] buf buffer that holds the data to be send.
1058 * \param[in] len length of the data to be sent.
1059 * \returns bytes sent, -1 on error. */
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001060int mgcp_udp_send(int fd, const struct osmo_sockaddr *addr, const char *buf, int len)
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001061{
1062 char ipbuf[INET6_ADDRSTRLEN];
1063 size_t addr_len;
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001064
1065 LOGP(DRTP, LOGL_DEBUG,
1066 "sending %i bytes length packet to %s:%u ...\n", len,
1067 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001068 osmo_sockaddr_port(&addr->u.sa));
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001069
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001070 if (addr->u.sa.sa_family == AF_INET6) {
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001071 addr_len = sizeof(addr->u.sin6);
1072 } else {
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001073 addr_len = sizeof(addr->u.sin);
1074 }
1075
1076 return sendto(fd, buf, len, 0, &addr->u.sa, addr_len);
1077}
1078
1079/*! send RTP dummy packet (to keep NAT connection open).
1080 * \param[in] endp mcgp endpoint that holds the RTP connection.
1081 * \param[in] conn associated RTP connection.
1082 * \returns bytes sent, -1 on error. */
1083int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
1084{
1085 int rc;
1086 int was_rtcp = 0;
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001087 struct osmo_sockaddr rtcp_addr;
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001088
1089 OSMO_ASSERT(endp);
1090 OSMO_ASSERT(conn);
1091
1092 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG, "sending dummy packet... %s\n",
1093 mgcp_conn_dump(conn->conn));
1094
Pau Espin Pedrola24dcc62021-07-06 17:48:47 +02001095 /* Before we try to deliver the packet, we check if the destination
1096 * port and IP-Address make sense at all. If not, we will be unable
1097 * to deliver the packet. */
1098 if (check_rtp_destin(conn) != 0)
1099 goto failed;
1100
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001101 if (mgcp_conn_rtp_is_iuup(conn))
1102 rc = mgcp_conn_iuup_send_dummy(conn);
1103 else
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001104 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001105 rtp_dummy_payload, sizeof(rtp_dummy_payload));
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001106
1107 if (rc == -1)
1108 goto failed;
1109
1110 if (endp->trunk->omit_rtcp)
1111 return rc;
1112
1113 was_rtcp = 1;
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001114 rtcp_addr = conn->end.addr;
1115 osmo_sockaddr_set_port(&rtcp_addr.u.sa, ntohs(conn->end.rtcp_port));
1116 rc = mgcp_udp_send(conn->end.rtcp.fd, &rtcp_addr,
1117 rtp_dummy_payload, sizeof(rtp_dummy_payload));
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001118
1119 if (rc >= 0)
1120 return rc;
1121
1122failed:
1123 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1124 "Failed to send dummy %s packet.\n",
1125 was_rtcp ? "RTCP" : "RTP");
1126
1127 return -1;
1128}
1129
1130/*! Send RTP/RTCP data to a specified destination connection.
1131 * \param[in] endp associated endpoint (for configuration, logging).
1132 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP.
1133 * \param[in] spoofed source address (set to NULL to disable).
1134 * \param[in] buf buffer that contains the RTP/RTCP data.
1135 * \param[in] len length of the buffer that contains the RTP/RTCP data.
1136 * \param[in] conn_src associated source connection.
1137 * \param[in] conn_dst associated destination connection.
1138 * \returns 0 on success, -1 on ERROR. */
1139int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct osmo_sockaddr *addr,
1140 struct msgb *msg, struct mgcp_conn_rtp *conn_src,
1141 struct mgcp_conn_rtp *conn_dst)
1142{
1143 /*! When no destination connection is available (e.g. when only one
1144 * connection in loopback mode exists), then the source connection
1145 * shall be specified as destination connection */
1146
1147 struct mgcp_trunk *trunk = endp->trunk;
1148 struct mgcp_rtp_end *rtp_end;
1149 struct mgcp_rtp_state *rtp_state;
1150 char ipbuf[INET6_ADDRSTRLEN];
1151 char *dest_name;
1152 int rc;
1153 int len;
1154
1155 OSMO_ASSERT(conn_src);
1156 OSMO_ASSERT(conn_dst);
1157
1158 if (is_rtp)
1159 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTP packet...\n");
1160 else
1161 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTCP packet...\n");
1162
1163 /* FIXME: It is legal that the payload type on the egress connection is
1164 * different from the payload type that has been negotiated on the
1165 * ingress connection. Essentially the codecs are the same so we can
1166 * match them and patch the payload type. However, if we can not find
1167 * the codec pendant (everything ist equal except the PT), we are of
1168 * course unable to patch the payload type. A situation like this
1169 * should not occur if transcoding is consequently avoided. Until
1170 * we have transcoding support in osmo-mgw we can not resolve this. */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001171 if (is_rtp && conn_dst->type != MGCP_RTP_IUUP) {
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001172 rc = mgcp_patch_pt(conn_src, conn_dst, msg);
1173 if (rc < 0) {
1174 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1175 "can not patch PT because no suitable egress codec was found.\n");
1176 }
1177 }
1178
1179 /* Note: In case of loopback configuration, both, the source and the
1180 * destination will point to the same connection. */
1181 rtp_end = &conn_dst->end;
1182 rtp_state = &conn_src->state;
1183 dest_name = conn_dst->conn->name;
1184
1185 /* Ensure we have an alternative SSRC in case we need it, see also
1186 * gen_rtp_header() */
1187 if (rtp_state->alt_rtp_tx_ssrc == 0)
1188 rtp_state->alt_rtp_tx_ssrc = rand();
1189
1190 if (!rtp_end->output_enabled) {
1191 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_DROPPED_PACKETS_CTR);
1192 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1193 "output disabled, drop to %s %s "
1194 "rtp_port:%u rtcp_port:%u\n",
1195 dest_name,
1196 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001197 osmo_sockaddr_port(&rtp_end->addr.u.sa), ntohs(rtp_end->rtcp_port)
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001198 );
1199 } else if (is_rtp) {
1200 int cont;
1201 int nbytes = 0;
1202 int buflen = msgb_length(msg);
1203
1204 /* Make sure we have a valid RTP header, in cases where no RTP
1205 * header is present, we will generate one. */
1206 gen_rtp_header(msg, rtp_end, rtp_state);
1207
1208 do {
1209 /* Run transcoder */
Ericfbf78d12021-08-23 22:31:39 +02001210 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 +02001211 if (cont < 0)
1212 break;
1213
1214 if (addr)
1215 mgcp_patch_and_count(endp, rtp_state, rtp_end,
1216 addr, msg);
1217
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001218 if (mgcp_conn_rtp_is_iuup(conn_dst) || mgcp_conn_rtp_is_iuup(conn_src)) {
1219 /* the iuup code will correctly transform to the correct AMR mode */
1220 } else if (amr_oa_bwe_convert_indicated(conn_dst->end.codec)) {
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001221 rc = amr_oa_bwe_convert(endp, msg,
1222 conn_dst->end.codec->param.amr_octet_aligned);
1223 if (rc < 0) {
1224 LOGPENDP(endp, DRTP, LOGL_ERROR,
1225 "Error in AMR octet-aligned <-> bandwidth-efficient mode conversion\n");
1226 break;
1227 }
1228 } else if (rtp_end->rfc5993_hr_convert &&
1229 strcmp(conn_src->end.codec->subtype_name, "GSM-HR-08") == 0) {
1230 rc = rfc5993_hr_convert(endp, msg);
1231 if (rc < 0) {
1232 LOGPENDP(endp, DRTP, LOGL_ERROR, "Error while converting to GSM-HR-08\n");
1233 break;
1234 }
1235 }
1236
1237 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1238 "process/send to %s %s "
1239 "rtp_port:%u rtcp_port:%u\n",
1240 dest_name,
1241 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001242 osmo_sockaddr_port(&rtp_end->addr.u.sa), ntohs(rtp_end->rtcp_port)
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001243 );
1244
1245 /* Forward a copy of the RTP data to a debug ip/port */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001246 forward_data_tap(rtp_end->rtp.fd, &conn_src->tap_out,
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001247 msg);
1248
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001249 len = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr,
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001250 (char *)msgb_data(msg), msgb_length(msg));
1251
1252 if (len <= 0)
1253 return len;
1254
1255 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
1256 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
1257 rtp_state->alt_rtp_tx_sequence++;
1258
1259 nbytes += len;
1260 buflen = cont;
1261 } while (buflen > 0);
1262 return nbytes;
1263 } else if (!trunk->omit_rtcp) {
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001264 struct osmo_sockaddr rtcp_addr = rtp_end->addr;
1265 osmo_sockaddr_set_port(&rtcp_addr.u.sa, rtp_end->rtcp_port);
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001266 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1267 "send to %s %s rtp_port:%u rtcp_port:%u\n",
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001268 dest_name, osmo_sockaddr_ntop(&rtcp_addr.u.sa, ipbuf),
1269 osmo_sockaddr_port(&rtp_end->addr.u.sa),
1270 osmo_sockaddr_port(&rtcp_addr.u.sa)
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001271 );
1272
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001273 len = mgcp_udp_send(rtp_end->rtcp.fd, &rtcp_addr,
1274 (char *)msgb_data(msg), msgb_length(msg));
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001275
1276 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
1277 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
1278 rtp_state->alt_rtp_tx_sequence++;
1279
1280 return len;
1281 }
1282
1283 return 0;
1284}
1285
Pau Espin Pedrolde805b62022-10-03 16:08:58 +02001286/*! Dispatch incoming RTP packet to opposite RTP connection.
1287 * \param[in] msg Message buffer to bridge, coming from source connection.
1288 * msg shall contain "struct osmo_rtp_msg_ctx *" attached in
1289 * "OSMO_RTP_MSG_CTX(msg)".
1290 * \returns 0 on success, -1 on ERROR.
1291 */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001292int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001293{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001294 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1295 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1296 struct mgcp_conn *conn = conn_src->conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001297 struct mgcp_conn *conn_dst;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001298 struct osmo_sockaddr *from_addr = mc->from_addr;
Philipp Maier97a93122021-05-07 22:50:31 +02001299 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001300
1301 /*! NOTE: This callback function implements the endpoint specific
Alexander Chemeris61cf9bb2020-05-11 18:13:53 +03001302 * dispatch behaviour of an rtp bridge/proxy endpoint. It is assumed
Philipp Maier87bd9be2017-08-22 16:35:41 +02001303 * that the endpoint will hold only two connections. This premise
1304 * is used to determine the opposite connection (it is always the
1305 * connection that is not the originating connection). Once the
1306 * destination connection is known the RTP packet is sent via
1307 * the destination connection. */
1308
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001309 /* If source is IuUP, we need to handle state, forward it through specific bridge path: */
1310 if (mgcp_conn_rtp_is_iuup(conn_src) && mc->proto == MGCP_PROTO_RTP)
1311 return mgcp_conn_iuup_dispatch_rtp(msg);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001312
1313 /* Check if the connection is in loopback mode, if yes, just send the
1314 * incoming data back to the origin */
1315 if (conn->mode == MGCP_CONN_LOOPBACK) {
1316 /* When we are in loopback mode, we loop back all incoming
1317 * packets back to their origin. We will use the originating
1318 * address data from the UDP packet header to patch the
1319 * outgoing address in connection on the fly */
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001320 if (osmo_sockaddr_port(&conn->u.rtp.end.addr.u.sa) == 0) {
Philipp Maier97a93122021-05-07 22:50:31 +02001321 memcpy(&conn->u.rtp.end.addr, from_addr,
1322 sizeof(conn->u.rtp.end.addr));
Philipp Maier97a93122021-05-07 22:50:31 +02001323 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1324 "loopback mode: implicitly using source address (%s:%u) as destination address\n",
1325 osmo_sockaddr_ntop(&from_addr->u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001326 osmo_sockaddr_port(&conn->u.rtp.end.addr.u.sa));
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001327 }
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001328 return mgcp_send_rtp(conn_src, msg);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001329 }
1330
Philipp Maier87bd9be2017-08-22 16:35:41 +02001331 /* Find a destination connection. */
1332 /* NOTE: This code path runs every time an RTP packet is received. The
1333 * function mgcp_find_dst_conn() we use to determine the detination
1334 * connection will iterate the connection list inside the endpoint.
1335 * Since list iterations are quite costly, we will figure out the
1336 * destination only once and use the optional private data pointer of
1337 * the connection to cache the destination connection pointer. */
1338 if (!conn->priv) {
1339 conn_dst = mgcp_find_dst_conn(conn);
1340 conn->priv = conn_dst;
1341 } else {
1342 conn_dst = (struct mgcp_conn *)conn->priv;
1343 }
1344
1345 /* There is no destination conn, stop here */
1346 if (!conn_dst) {
Alexander Chemerisebb9bf32020-05-11 18:14:31 +03001347 LOGPCONN(conn, DRTP, LOGL_DEBUG,
1348 "no connection to forward an incoming RTP packet to\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001349 return -1;
1350 }
1351
1352 /* The destination conn is not an RTP connection */
1353 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001354 LOGPCONN(conn, DRTP, LOGL_ERROR,
1355 "unable to find suitable destination conn\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001356 return -1;
1357 }
1358
1359 /* Dispatch RTP packet to destination RTP connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001360 return mgcp_send_rtp(&conn_dst->u.rtp, msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001361}
1362
Philipp Maier0996a1e2020-06-10 15:27:14 +02001363/*! dispatch incoming RTP packet to E1 subslot, handle RTCP packets locally.
1364 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1365 * \param[in] addr socket address where the RTP packet has been received from.
1366 * \param[in] buf buffer that hold the RTP payload.
1367 * \param[in] buf_size size data length of buf.
1368 * \param[in] conn originating connection.
1369 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001370int mgcp_dispatch_e1_bridge_cb(struct msgb *msg)
Philipp Maier0996a1e2020-06-10 15:27:14 +02001371{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001372 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1373 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1374 struct mgcp_conn *conn = conn_src->conn;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001375 struct osmo_sockaddr *from_addr = mc->from_addr;
Philipp Maier97a93122021-05-07 22:50:31 +02001376 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001377
Philipp Maier889fe7f2020-07-06 17:44:12 +02001378 /* Check if the connection is in loopback mode, if yes, just send the
1379 * incoming data back to the origin */
1380 if (conn->mode == MGCP_CONN_LOOPBACK) {
1381 /* When we are in loopback mode, we loop back all incoming
1382 * packets back to their origin. We will use the originating
1383 * address data from the UDP packet header to patch the
1384 * outgoing address in connection on the fly */
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001385 if (osmo_sockaddr_port(&conn->u.rtp.end.addr.u.sa) == 0) {
Philipp Maier97a93122021-05-07 22:50:31 +02001386 memcpy(&conn->u.rtp.end.addr, from_addr,
1387 sizeof(conn->u.rtp.end.addr));
Philipp Maier97a93122021-05-07 22:50:31 +02001388 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1389 "loopback mode: implicitly using source address (%s:%u) as destination address\n",
1390 osmo_sockaddr_ntop(&from_addr->u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001391 osmo_sockaddr_port(&conn->u.rtp.end.addr.u.sa));
Philipp Maier889fe7f2020-07-06 17:44:12 +02001392 }
1393 return mgcp_send_rtp(conn_src, msg);
1394 }
1395
1396 /* Forward to E1 */
1397 return mgcp_e1_send_rtp(conn->endp, conn->u.rtp.end.codec, msg);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001398}
1399
Philipp Maierdf5d2192018-01-24 11:39:32 +01001400/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1401 * \param[in] endp Endpoint on which the connection resides.
Philipp Maier08eb9352020-06-18 11:55:35 +02001402 * \param[in] conn Connection that is about to be removed (ignored). */
Philipp Maierdf5d2192018-01-24 11:39:32 +01001403void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1404{
1405 struct mgcp_conn *conn_cleanup;
1406
1407 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1408 * pointer to the destination connection, so that we do not have
1409 * to go through the list every time an RTP packet arrives. To prevent
1410 * a use-after-free situation we invalidate this information for all
1411 * connections present when one connection is removed from the
1412 * endpoint. */
1413 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
Pau Espin Pedrol6d0a59a2020-09-08 16:50:22 +02001414 if (conn_cleanup->priv == conn)
1415 conn_cleanup->priv = NULL;
Philipp Maierdf5d2192018-01-24 11:39:32 +01001416 }
1417}
1418
Philipp Maier0996a1e2020-06-10 15:27:14 +02001419/*! cleanup an endpoint when a connection on an E1 endpoint is removed.
1420 * \param[in] endp Endpoint on which the connection resides.
1421 * \param[in] conn Connection that is about to be removed (ignored). */
1422void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1423{
Philipp Maier889fe7f2020-07-06 17:44:12 +02001424 /* Cleanup tasks for E1 are the same as for regular endpoint. The
1425 * shut down of the E1 part is handled separately. */
1426 mgcp_cleanup_rtp_bridge_cb(endp, conn);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001427}
1428
Philipp Maier87bd9be2017-08-22 16:35:41 +02001429/* Handle incoming RTP data from NET */
1430static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1431{
1432 /* NOTE: This is a generic implementation. RTP data is received. In
1433 * case of loopback the data is just sent back to its origin. All
1434 * other cases implement endpoint specific behaviour (e.g. how is the
1435 * destination connection determined?). That specific behaviour is
1436 * implemented by the callback function that is called at the end of
1437 * the function */
1438
1439 struct mgcp_conn_rtp *conn_src;
1440 struct mgcp_endpoint *endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001441 struct osmo_sockaddr addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001442 socklen_t slen = sizeof(addr);
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001443 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001444 int ret;
1445 enum rtp_proto proto;
1446 struct osmo_rtp_msg_ctx *mc;
Eric8f333032021-08-13 00:00:43 +02001447 struct msgb *msg;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001448 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001449
1450 conn_src = (struct mgcp_conn_rtp *)fd->data;
1451 OSMO_ASSERT(conn_src);
1452 endp = conn_src->conn->endp;
1453 OSMO_ASSERT(endp);
Eric8f333032021-08-13 00:00:43 +02001454 msg = msgb_alloc_c(endp->trunk, RTP_BUF_SIZE, "RTP-rx");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001455
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001456 proto = (fd == &conn_src->end.rtp)? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001457
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001458 ret = recvfrom(fd->fd, msgb_data(msg), msg->data_len, 0, (struct sockaddr *)&addr.u.sa, &slen);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001459
1460 if (ret <= 0) {
1461 LOG_CONN_RTP(conn_src, LOGL_ERROR, "recvfrom error: %s\n", strerror(errno));
1462 rc = -1;
1463 goto out;
1464 }
1465
1466 msgb_put(msg, ret);
1467
1468 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "%s: rx %u bytes from %s:%u\n",
Philipp Maier069dd162022-03-30 16:09:22 +02001469 proto == MGCP_PROTO_RTP ? "RTP" : "RTCP",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001470 msgb_length(msg), osmo_sockaddr_ntop(&addr.u.sa, ipbuf),
1471 osmo_sockaddr_port(&addr.u.sa));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001472
1473 if ((proto == MGCP_PROTO_RTP && check_rtp(conn_src, msg))
1474 || (proto == MGCP_PROTO_RTCP && check_rtcp(conn_src, msg))) {
1475 /* Logging happened in the two check_ functions */
1476 rc = -1;
1477 goto out;
1478 }
1479
Philipp Maierb3d14eb2021-05-20 14:18:52 +02001480 if (mgcp_is_rtp_dummy_payload(msg)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001481 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx dummy packet (dropped)\n");
1482 rc = 0;
1483 goto out;
1484 }
1485
1486 /* Since the msgb remains owned and freed by this function, the msg ctx data struct can just be on the stack and
1487 * needs not be allocated with the msgb. */
1488 mc = OSMO_RTP_MSG_CTX(msg);
1489 *mc = (struct osmo_rtp_msg_ctx){
1490 .proto = proto,
1491 .conn_src = conn_src,
1492 .from_addr = &addr,
1493 };
1494 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "msg ctx: %d %p %s\n",
1495 mc->proto, mc->conn_src,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001496 osmo_hexdump((void*)mc->from_addr,
1497 mc->from_addr->u.sa.sa_family == AF_INET6 ?
1498 sizeof(struct sockaddr_in6) :
1499 sizeof(struct sockaddr_in)));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001500
1501 /* Increment RX statistics */
Pau Espin Pedroldaf5bce2022-09-22 19:14:24 +02001502 rate_ctr_inc(rate_ctr_group_get_ctr(conn_src->ctrg, RTP_PACKETS_RX_CTR));
1503 rate_ctr_add(rate_ctr_group_get_ctr(conn_src->ctrg, RTP_OCTETS_RX_CTR), msgb_length(msg));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001504 /* FIXME: count RTP and RTCP separately, also count IuUP payload-less separately */
1505
1506 /* Forward a copy of the RTP data to a debug ip/port */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001507 forward_data_tap(fd->fd, &conn_src->tap_in, msg);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001508
1509 rc = rx_rtp(msg);
1510
1511out:
1512 msgb_free(msg);
1513 return rc;
1514}
1515
Philipp Maiere1442752022-03-30 16:06:15 +02001516/* Note: This function is able to handle RTP and RTCP */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001517static int rx_rtp(struct msgb *msg)
1518{
1519 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1520 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001521 struct osmo_sockaddr *from_addr = mc->from_addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001522 struct mgcp_conn *conn = conn_src->conn;
1523 struct mgcp_trunk *trunk = conn->endp->trunk;
1524
1525 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx_rtp(%u bytes)\n", msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001526
Oliver Smithe36b7752019-01-22 16:31:36 +01001527 mgcp_conn_watchdog_kick(conn_src->conn);
1528
Philipp Maier228e5912019-03-05 13:56:59 +01001529 /* If AMR is configured for the ingress connection a conversion of the
1530 * framing mode (octet-aligned vs. bandwith-efficient is explicitly
1531 * define, then we check if the incoming payload matches that
1532 * expectation. */
Philipp Maiere1442752022-03-30 16:06:15 +02001533 if (mc->proto == MGCP_PROTO_RTP &&
1534 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}