blob: 994ce45ad34d02f7ab92ac8bb93aefc0a264dee4 [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
Philipp Maier4c4d2272023-04-26 15:45:17 +0200495 * of the codec that is set for the destination connection (conn_dst) */
496static int mgcp_patch_pt(struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200497{
498 struct rtp_hdr *rtp_hdr;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200499
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200500 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
Philipp Maier4c4d2272023-04-26 15:45:17 +0200501 LOG_CONN_RTP(conn_dst, LOGL_NOTICE, "RTP packet too short (%u < %zu)\n",
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200502 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200503 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200504 }
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200505 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier6931f9a2018-07-26 09:29:31 +0200506
Philipp Maier4c4d2272023-04-26 15:45:17 +0200507 if (!conn_dst->end.codec) {
508 LOG_CONN_RTP(conn_dst, LOGL_NOTICE, "no codec set on destination connection!\n");
Philipp Maier6931f9a2018-07-26 09:29:31 +0200509 return -EINVAL;
Philipp Maier4c4d2272023-04-26 15:45:17 +0200510 }
511 rtp_hdr->payload_type = (uint8_t) conn_dst->end.codec->payload_type;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200512
Philipp Maier6931f9a2018-07-26 09:29:31 +0200513 return 0;
514}
515
Philipp Maier87bd9be2017-08-22 16:35:41 +0200516/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200517 * some of the supported endpoints (e.g. the nanoBTS) can only handle
518 * one source and this code will patch RTP header to appear as if there
519 * is only one source.
520 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200521 * we receive will be seen as a switch in streams. */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200522void mgcp_patch_and_count(const struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200523 struct mgcp_rtp_state *state,
524 struct mgcp_rtp_end *rtp_end,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200525 struct osmo_sockaddr *addr, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200526{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200527 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200528 uint32_t arrival_time;
529 int32_t transit;
530 uint16_t seq;
531 uint32_t timestamp, ssrc;
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200532 bool marker_bit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200533 struct rtp_hdr *rtp_hdr;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200534 int payload = rtp_end->codec->payload_type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200535 unsigned int len = msgb_length(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200536
537 if (len < sizeof(*rtp_hdr))
538 return;
539
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200540 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200541 seq = ntohs(rtp_hdr->sequence);
542 timestamp = ntohl(rtp_hdr->timestamp);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100543 arrival_time = mgcp_get_current_ts(rtp_end->codec->rate);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200544 ssrc = ntohl(rtp_hdr->ssrc);
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200545 marker_bit = !!rtp_hdr->marker;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200546 transit = arrival_time - timestamp;
547
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200548 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc, marker_bit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200549
550 if (!state->initialized) {
551 state->initialized = 1;
552 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100553 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200554 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200555 state->packet_duration =
556 mgcp_rtp_packet_duration(endp, rtp_end);
Philipp Maier0ec1d4e2018-05-16 11:09:42 +0200557 state->out_stream.last_seq = seq - 1;
558 state->out_stream.ssrc = state->patch.orig_ssrc = ssrc;
559 state->out_stream.last_tsdelta = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200560 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200561 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200562 LOGPENDP(endp, DRTP, LOGL_INFO,
563 "initializing stream, SSRC: %u timestamp: %u "
564 "pkt-duration: %d, from %s:%d\n",
565 state->in_stream.ssrc,
566 state->patch.seq_offset, state->packet_duration,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200567 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
568 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200569 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200570 state->packet_duration =
Philipp Maierbc0346e2018-06-07 09:52:16 +0200571 rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200572 LOGPENDP(endp, DRTP, LOGL_NOTICE,
573 "fixed packet duration is not available, "
574 "using fixed 20ms instead: %d from %s:%d\n",
575 state->packet_duration,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200576 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
577 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200578 }
579 } else if (state->in_stream.ssrc != ssrc) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200580 LOGPENDP(endp, DRTP, LOGL_NOTICE,
581 "SSRC changed: %u -> %u "
582 "from %s:%d\n",
583 state->in_stream.ssrc, rtp_hdr->ssrc,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200584 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
585 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200586
587 state->in_stream.ssrc = ssrc;
588 if (rtp_end->force_constant_ssrc) {
589 int16_t delta_seq;
590
591 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100592 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200593 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200594
595 /* Estimate number of packets that would have been sent */
596 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200597 (arrival_time - state->in_stream.last_arrival_time
598 + state->packet_duration / 2) /
599 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200600
601 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200602 delta_seq, timestamp, marker_bit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200603
Pau Espin Pedrol4c77e9b2021-07-07 12:12:58 +0200604 state->patch.patch_ssrc = true;
Harald Welte33381352017-12-25 09:44:26 +0100605 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200606 if (rtp_end->force_constant_ssrc != -1)
607 rtp_end->force_constant_ssrc -= 1;
608
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200609 LOGPENDP(endp, DRTP, LOGL_NOTICE,
610 "SSRC patching enabled, SSRC: %u "
611 "SeqNo offset: %d, TS offset: %d "
612 "from %s:%d\n", state->in_stream.ssrc,
613 state->patch.seq_offset, state->patch.timestamp_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200614 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
615 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200616 }
617
618 state->in_stream.last_tsdelta = 0;
619 } else {
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200620 if (!marker_bit) {
621 /* Compute current per-packet timestamp delta */
622 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
623 addr, seq, timestamp, "input",
624 &state->in_stream.last_tsdelta);
625 } else {
626 state->in_stream.last_tsdelta = 0;
627 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200628
Harald Welte33381352017-12-25 09:44:26 +0100629 if (state->patch.patch_ssrc)
630 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200631 }
632
633 /* Save before patching */
634 state->in_stream.last_timestamp = timestamp;
635 state->in_stream.last_seq = seq;
636 state->in_stream.last_arrival_time = arrival_time;
637
638 if (rtp_end->force_aligned_timing &&
639 state->out_stream.ssrc == ssrc && state->packet_duration)
640 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200641 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200642 timestamp, marker_bit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200643
644 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100645 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200646 rtp_hdr->ssrc = htonl(ssrc);
647
648 /* Apply the offset and store it back to the packet.
649 * This won't change anything if the offset is 0, so the conditional is
650 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100651 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200652 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100653 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200654 rtp_hdr->timestamp = htonl(timestamp);
655
656 /* Check again, whether the timestamps are still valid */
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200657 if (!marker_bit) {
658 if (state->out_stream.ssrc == ssrc)
659 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
660 addr, seq, timestamp, "output",
661 &state->out_stream.last_tsdelta);
662 } else {
663 state->out_stream.last_tsdelta = 0;
664 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200665
666 /* Save output values */
667 state->out_stream.last_seq = seq;
668 state->out_stream.last_timestamp = timestamp;
669 state->out_stream.ssrc = ssrc;
670
671 if (payload < 0)
672 return;
673
674#if 0
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200675 LOGPENDP(endp, DRTP, LOGL_DEBUG, "payload hdr payload %u -> endp payload %u\n",
676 rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200677 rtp_hdr->payload_type = payload;
678#endif
679}
680
Philipp Maier9fc8a022019-02-20 12:26:52 +0100681/* There are different dialects used to format and transfer voice data. When
682 * the receiving end expects GSM-HR data to be formated after RFC 5993, this
683 * function is used to convert between RFC 5993 and TS 101318, which we normally
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200684 * use.
685 * Return 0 on sucess, negative on errors like invalid data length. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200686static int rfc5993_hr_convert(struct mgcp_endpoint *endp, struct msgb *msg)
Philipp Maier9fc8a022019-02-20 12:26:52 +0100687{
Philipp Maier9fc8a022019-02-20 12:26:52 +0100688 struct rtp_hdr *rtp_hdr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200689 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
Pau Espin Pedrol08077712022-10-19 19:06:45 +0200690 LOGPENDP(endp, DRTP, LOGL_ERROR, "RTP packet too short (%d < %zu)\n",
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200691 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200692 return -EINVAL;
693 }
694
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200695 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100696
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200697 if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr)) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100698 /* TS 101318 encoding => RFC 5993 encoding */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200699 msgb_put(msg, 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100700 memmove(rtp_hdr->data + 1, rtp_hdr->data, GSM_HR_BYTES);
701 rtp_hdr->data[0] = 0x00;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200702 } else if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr) + 1) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100703 /* RFC 5993 encoding => TS 101318 encoding */
704 memmove(rtp_hdr->data, rtp_hdr->data + 1, GSM_HR_BYTES);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200705 msgb_trim(msg, msgb_length(msg) - 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100706 } else {
707 /* It is possible that multiple payloads occur in one RTP
708 * packet. This is not supported yet. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200709 LOGPENDP(endp, DRTP, LOGL_ERROR,
710 "cannot figure out how to convert RTP packet\n");
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200711 return -ENOTSUP;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100712 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200713 return 0;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100714}
715
Pau Espin Pedrol377d3912022-10-25 13:52:19 +0200716/*! Convert msg to AMR RTP framing mode specified by target_is_oa.
717 * \param[in] endp MGCP Endpoint where this message belongs to (used for logging purposes)
718 * \param[in] msg Message containing an AMR RTP payload (in octet-aligned or bandwidth-efficient format).
719 * \param[in] target_is_oa the target framing mode that msg will contain after this function succeeds.
720 * \returns The size of the new RTP AMR content on success, negative on error.
721 *
722 * For AMR RTP two framing modes are defined RFC3267. There is a bandwidth
Philipp Maier228e5912019-03-05 13:56:59 +0100723 * efficient encoding scheme where all fields are packed together one after
724 * another and an octet aligned mode where all fields are aligned to octet
Pau Espin Pedrol377d3912022-10-25 13:52:19 +0200725 * boundaries. This function is used to convert between the two modes.
726 */
727int amr_oa_bwe_convert(struct mgcp_endpoint *endp, struct msgb *msg,
Philipp Maier228e5912019-03-05 13:56:59 +0100728 bool target_is_oa)
729{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200730 /* NOTE: the msgb has an allocated length of RTP_BUF_SIZE, so there is
Philipp Maier228e5912019-03-05 13:56:59 +0100731 * plenty of space available to store the slightly larger, converted
732 * data */
Philipp Maier228e5912019-03-05 13:56:59 +0100733 struct rtp_hdr *rtp_hdr;
734 unsigned int payload_len;
735 int rc;
Pau Espin Pedrol9ac9cb92022-10-26 12:36:48 +0200736 bool orig_is_oa;
Philipp Maier228e5912019-03-05 13:56:59 +0100737
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200738 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
739 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 +0200740 return -EINVAL;
741 }
742
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200743 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200744 payload_len = msgb_length(msg) - sizeof(struct rtp_hdr);
Pau Espin Pedrol9ac9cb92022-10-26 12:36:48 +0200745 orig_is_oa = osmo_amr_is_oa(rtp_hdr->data, payload_len);
Philipp Maier228e5912019-03-05 13:56:59 +0100746
Pau Espin Pedrol9ac9cb92022-10-26 12:36:48 +0200747 if (orig_is_oa) {
Philipp Maier228e5912019-03-05 13:56:59 +0100748 if (!target_is_oa)
749 /* Input data is oa an target format is bwe
750 * ==> convert */
751 rc = osmo_amr_oa_to_bwe(rtp_hdr->data, payload_len);
752 else
753 /* Input data is already bew, but we accept it anyway
754 * ==> no conversion needed */
755 rc = payload_len;
756 } else {
757 if (target_is_oa)
758 /* Input data is bwe an target format is oa
759 * ==> convert */
760 rc = osmo_amr_bwe_to_oa(rtp_hdr->data, payload_len,
761 RTP_BUF_SIZE);
762 else
763 /* Input data is already oa, but we accept it anyway
764 * ==> no conversion needed */
765 rc = payload_len;
766 }
767 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200768 LOGPENDP(endp, DRTP, LOGL_ERROR,
Pau Espin Pedrol9ac9cb92022-10-26 12:36:48 +0200769 "RTP AMR packet conversion %s->%s failed: %s\n",
770 orig_is_oa ? "OA" : "BWE",
771 target_is_oa ? "OA" : "BWE",
772 osmo_hexdump(rtp_hdr->data, payload_len));
Philipp Maier228e5912019-03-05 13:56:59 +0100773 return -EINVAL;
774 }
775
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200776 return msgb_trim(msg, rc + sizeof(struct rtp_hdr));
Philipp Maier228e5912019-03-05 13:56:59 +0100777}
778
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200779/* Return whether an RTP packet with AMR payload is in octet-aligned mode.
780 * Return 0 if in bandwidth-efficient mode, 1 for octet-aligned mode, and negative if the RTP data is invalid. */
781static int amr_oa_check(char *data, int len)
Philipp Maier228e5912019-03-05 13:56:59 +0100782{
783 struct rtp_hdr *rtp_hdr;
784 unsigned int payload_len;
785
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200786 if (len < sizeof(struct rtp_hdr))
787 return -EINVAL;
788
Philipp Maier228e5912019-03-05 13:56:59 +0100789 rtp_hdr = (struct rtp_hdr *)data;
790
791 payload_len = len - sizeof(struct rtp_hdr);
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200792 if (payload_len < sizeof(struct amr_hdr))
793 return -EINVAL;
Philipp Maier228e5912019-03-05 13:56:59 +0100794
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200795 return osmo_amr_is_oa(rtp_hdr->data, payload_len) ? 1 : 0;
Philipp Maier228e5912019-03-05 13:56:59 +0100796}
797
Philipp Maier87bd9be2017-08-22 16:35:41 +0200798/* Forward data to a debug tap. This is debug function that is intended for
799 * debugging the voice traffic with tools like gstreamer */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100800void forward_data_tap(int fd, struct mgcp_rtp_tap *tap, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200801{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100802 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200803
Philipp Maiere6f172d2017-11-07 12:00:01 +0100804 if (!tap->enabled)
805 return;
806
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200807 rc = sendto(fd, msgb_data(msg), msgb_length(msg), 0, (struct sockaddr *)&tap->forward,
Philipp Maiere6f172d2017-11-07 12:00:01 +0100808 sizeof(tap->forward));
809
810 if (rc < 0)
811 LOGP(DRTP, LOGL_ERROR,
812 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200813}
814
Philipp Maier889fe7f2020-07-06 17:44:12 +0200815/* Generate an RTP header if it is missing */
816static void gen_rtp_header(struct msgb *msg, struct mgcp_rtp_end *rtp_end,
817 struct mgcp_rtp_state *state)
818{
819 struct rtp_hdr *hdr = (struct rtp_hdr *)msgb_data(msg);
820
821 if (hdr->version > 0)
822 return;
823
824 hdr->version = 2;
825 hdr->payload_type = rtp_end->codec->payload_type;
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100826 hdr->timestamp = osmo_htonl(mgcp_get_current_ts(rtp_end->codec->rate));
Philipp Maier889fe7f2020-07-06 17:44:12 +0200827 hdr->sequence = osmo_htons(state->alt_rtp_tx_sequence);
828 hdr->ssrc = state->alt_rtp_tx_ssrc;
829}
830
Philipp Maier87bd9be2017-08-22 16:35:41 +0200831/* Check if the origin (addr) matches the address/port data of the RTP
832 * connections. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200833static int check_rtp_origin(struct mgcp_conn_rtp *conn, struct osmo_sockaddr *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200834{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200835 char ipbuf[INET6_ADDRSTRLEN];
836
Pau Espin Pedrol051eb4d2022-10-04 12:50:28 +0200837 if (osmo_sockaddr_is_any(&conn->end.addr) != 0) {
Neels Hofmeyr5e101c92023-12-04 03:54:43 +0100838 if (mgcp_conn_rtp_is_iuup(conn) && !conn->iuup.configured) {
839 /* Allow IuUP Initialization to get through even if we don't have a remote address set yet.
840 * This is needed because hNodeB doesn't announce its IuUP remote IP addr to the MGCP client
841 * (RAB Assignment Response at HNBGW) until it has gone through IuUP Initialization against
842 * this MGW here. Hence the MGW may not yet know the remote IuUP address and port at the time
843 * of receiving IuUP Initialization from the hNodeB.
844 */
845 LOGPCONN(conn->conn, DRTP, LOGL_INFO,
846 "Rx RTP from %s: allowing unknown src for IuUP Initialization\n",
847 osmo_sockaddr_to_str(addr));
848 return 0;
849 }
Neels Hofmeyrdd1ddf72023-12-01 01:52:43 +0100850 /* Receiving early media before the endpoint is configured. Instead of logging
851 * this as an error that occurs on every call, keep it more low profile to not
852 * confuse humans with expected errors. */
853 LOGPCONN(conn->conn, DRTP, LOGL_INFO,
854 "Rx RTP from %s, but remote address not set: dropping early media\n",
855 osmo_sockaddr_to_str(addr));
856 return -1;
Neels Hofmeyr0063ca22018-07-23 18:12:16 +0200857 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200858
Philipp Maier87bd9be2017-08-22 16:35:41 +0200859 /* Note: Check if the inbound RTP data comes from the same host to
860 * which we send our outgoing RTP traffic. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200861 if (conn->end.addr.u.sa.sa_family != addr->u.sa.sa_family ||
862 (conn->end.addr.u.sa.sa_family == AF_INET &&
863 conn->end.addr.u.sin.sin_addr.s_addr != addr->u.sin.sin_addr.s_addr) ||
864 (conn->end.addr.u.sa.sa_family == AF_INET6 &&
865 memcmp(&conn->end.addr.u.sin6.sin6_addr, &addr->u.sin6.sin6_addr,
866 sizeof(struct in6_addr)))) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200867 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
Pau Espin Pedrola7e6fbc2023-11-30 14:52:16 +0100868 "data from wrong src %s, expected IP Address %s. Packet tossed.\n",
869 osmo_sockaddr_to_str(addr), osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200870 return -1;
871 }
872
Philipp Maier87bd9be2017-08-22 16:35:41 +0200873 /* Note: Usually the remote remote port of the data we receive will be
874 * the same as the remote port where we transmit outgoing RTP traffic
875 * to (set by MDCX). We use this to check the origin of the data for
876 * plausibility. */
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200877 if (osmo_sockaddr_port(&conn->end.addr.u.sa) != osmo_sockaddr_port(&addr->u.sa) &&
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200878 ntohs(conn->end.rtcp_port) != osmo_sockaddr_port(&addr->u.sa)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200879 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
Pau Espin Pedrola7e6fbc2023-11-30 14:52:16 +0100880 "data from wrong src %s, expected port: %u for RTP or %u for RTCP. Packet tossed.\n",
881 osmo_sockaddr_to_str(addr), osmo_sockaddr_port(&conn->end.addr.u.sa),
882 ntohs(conn->end.rtcp_port));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200883 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200884 }
885
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200886 return 0;
887}
888
Philipp Maier87bd9be2017-08-22 16:35:41 +0200889/* Check the if the destination address configuration of an RTP connection
890 * makes sense */
891static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200892{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200893 char ipbuf[INET6_ADDRSTRLEN];
Pau Espin Pedrol051eb4d2022-10-04 12:50:28 +0200894 bool ip_is_any = osmo_sockaddr_is_any(&conn->end.addr) != 0;
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200895 uint16_t port = osmo_sockaddr_port(&conn->end.addr.u.sa);
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200896
Philipp Maiere6df0e42018-05-29 14:03:06 +0200897 /* Note: it is legal to create a connection but never setting a port
898 * and IP-address for outgoing data. */
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200899 if (ip_is_any && port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200900 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
Neels Hofmeyra4b677c2021-07-07 23:38:23 +0200901 "destination IP-address and rtp port is not (yet) known (%s:%u)\n",
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200902 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), port);
Philipp Maiere6df0e42018-05-29 14:03:06 +0200903 return -1;
904 }
905
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200906 if (ip_is_any) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200907 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
908 "destination IP-address is invalid (%s:%u)\n",
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200909 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), port);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200910 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200911 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200912
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200913 if (port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200914 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
915 "destination rtp port is invalid (%s:%u)\n",
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200916 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), port);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200917 return -1;
918 }
919
920 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200921}
922
Philipp Maier4dba7692018-08-03 12:20:52 +0200923/* Do some basic checks to make sure that the RTCP packets we are going to
924 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200925static int check_rtcp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +0200926{
927 struct rtcp_hdr *hdr;
928 unsigned int len;
929 uint8_t type;
930
931 /* RTPC packets that are just a header without data do not make
932 * any sense. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200933 if (msgb_length(msg) < sizeof(struct rtcp_hdr)) {
934 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP packet too short (%u < %zu)\n",
935 msgb_length(msg), sizeof(struct rtcp_hdr));
Philipp Maier4dba7692018-08-03 12:20:52 +0200936 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200937 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200938
939 /* Make sure that the length of the received packet does not exceed
940 * the available buffer size */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200941 hdr = (struct rtcp_hdr *)msgb_data(msg);
Philipp Maier4dba7692018-08-03 12:20:52 +0200942 len = (osmo_ntohs(hdr->length) + 1) * 4;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200943 if (len > msgb_length(msg)) {
944 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header length exceeds packet size (%u > %u)\n",
945 len, msgb_length(msg));
Philipp Maier4dba7692018-08-03 12:20:52 +0200946 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200947 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200948
949 /* Make sure we accept only packets that have a proper packet type set
950 * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
951 type = hdr->type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200952 if ((type < 192 || type > 195) && (type < 200 || type > 213)) {
953 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header: invalid type: %u\n", type);
Philipp Maier4dba7692018-08-03 12:20:52 +0200954 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200955 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200956
957 return 0;
958}
959
960/* Do some basic checks to make sure that the RTP packets we are going to
961 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200962static int check_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +0200963{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200964 size_t min_size = sizeof(struct rtp_hdr);
965 if (msgb_length(msg) < min_size) {
966 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
967 msgb_length(msg), min_size);
968 return -1;
969 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200970
971 /* FIXME: Add more checks, the reason why we do not check more than
972 * the length is because we currently handle IUUP packets as RTP
973 * packets, so they must pass this check, if we weould be more
974 * strict here, we would possibly break 3G. (see also FIXME note
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100975 * below.*/
Philipp Maier4dba7692018-08-03 12:20:52 +0200976
977 return 0;
978}
979
Pau Espin Pedrol6db3f7f2022-10-24 17:55:02 +0200980/*! Dispatch msg bridged from the sister conn in the endpoint.
981 * \param[in] conn_dst The destination conn that should handle and transmit the content to
982 * its peer outside MGW.
983 * \param[in] msg msgb containing an RTP pkt received by the sister conn in the endpoint,
984 * \returns bytes sent, -1 on error.
985 *
986 * Possible options are standard RTP packet transmission, transmission
987 * via IuUP or transmission via an osmux connection.
988 */
989static int mgcp_conn_rtp_dispatch_rtp(struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200990{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200991 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
992 enum rtp_proto proto = mc->proto;
993 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200994 struct mgcp_endpoint *endp = conn_src->conn->endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200995
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200996 LOGPENDP(endp, DRTP, LOGL_DEBUG, "destin conn:%s\n",
997 mgcp_conn_dump(conn_dst->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200998
999 /* Before we try to deliver the packet, we check if the destination
1000 * port and IP-Address make sense at all. If not, we will be unable
1001 * to deliver the packet. */
1002 if (check_rtp_destin(conn_dst) != 0)
1003 return -1;
1004
1005 /* Depending on the RTP connection type, deliver the RTP packet to the
1006 * destination connection. */
1007 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001008 case MGCP_RTP_DEFAULT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001009 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1010 "endpoint type is MGCP_RTP_DEFAULT, "
1011 "using mgcp_send() to forward data directly\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001012 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001013 mc->from_addr, msg, conn_src, conn_dst);
Pau Espin Pedrol9d939b62022-10-03 16:59:20 +02001014 case MGCP_RTP_OSMUX:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001015 LOGPENDP(endp, DRTP, LOGL_DEBUG,
Pau Espin Pedrol9d939b62022-10-03 16:59:20 +02001016 "endpoint type is MGCP_RTP_OSMUX, "
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001017 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n");
Pau Espin Pedrold1e94c72022-10-25 13:49:40 +02001018 return conn_osmux_send_rtp(conn_dst, msg);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001019 case MGCP_RTP_IUUP:
1020 if (proto == MGCP_PROTO_RTP) {
1021 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1022 "endpoint type is MGCP_RTP_IUUP, "
1023 "using mgcp_conn_iuup_send_rtp() to forward data over IuUP\n");
1024 return mgcp_conn_iuup_send_rtp(conn_src, conn_dst, msg);
1025 }
1026 /* RTCP: we forward as usual for regular RTP connection */
1027 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1028 "endpoint type is MGCP_RTP_IUUP and proto!=MGCP_PROTO_RTP, "
1029 "using mgcp_send() to forward data directly\n");
1030 return mgcp_send(endp, false,
1031 mc->from_addr, msg, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001032 }
1033
Philipp Maier87bd9be2017-08-22 16:35:41 +02001034 /* If the data has not been handled/forwarded until here, it will
1035 * be discarded, this should not happen, normally the MGCP type
1036 * should be properly set */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001037 LOGPENDP(endp, DRTP, LOGL_ERROR, "bad MGCP type -- data discarded!\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001038
1039 return -1;
1040}
1041
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001042/*! send udp packet.
1043 * \param[in] fd associated file descriptor.
1044 * \param[in] addr destination ip-address.
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001045 * \param[in] buf buffer that holds the data to be send.
1046 * \param[in] len length of the data to be sent.
1047 * \returns bytes sent, -1 on error. */
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001048int mgcp_udp_send(int fd, const struct osmo_sockaddr *addr, const char *buf, int len)
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001049{
1050 char ipbuf[INET6_ADDRSTRLEN];
1051 size_t addr_len;
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001052
1053 LOGP(DRTP, LOGL_DEBUG,
1054 "sending %i bytes length packet to %s:%u ...\n", len,
1055 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001056 osmo_sockaddr_port(&addr->u.sa));
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001057
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001058 if (addr->u.sa.sa_family == AF_INET6) {
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001059 addr_len = sizeof(addr->u.sin6);
1060 } else {
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001061 addr_len = sizeof(addr->u.sin);
1062 }
1063
1064 return sendto(fd, buf, len, 0, &addr->u.sa, addr_len);
1065}
1066
1067/*! send RTP dummy packet (to keep NAT connection open).
1068 * \param[in] endp mcgp endpoint that holds the RTP connection.
1069 * \param[in] conn associated RTP connection.
1070 * \returns bytes sent, -1 on error. */
1071int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
1072{
1073 int rc;
1074 int was_rtcp = 0;
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001075 struct osmo_sockaddr rtcp_addr;
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001076
1077 OSMO_ASSERT(endp);
1078 OSMO_ASSERT(conn);
1079
1080 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG, "sending dummy packet... %s\n",
1081 mgcp_conn_dump(conn->conn));
1082
Pau Espin Pedrola24dcc62021-07-06 17:48:47 +02001083 /* Before we try to deliver the packet, we check if the destination
1084 * port and IP-Address make sense at all. If not, we will be unable
1085 * to deliver the packet. */
1086 if (check_rtp_destin(conn) != 0)
1087 goto failed;
1088
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001089 if (mgcp_conn_rtp_is_iuup(conn))
1090 rc = mgcp_conn_iuup_send_dummy(conn);
1091 else
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001092 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001093 rtp_dummy_payload, sizeof(rtp_dummy_payload));
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001094
1095 if (rc == -1)
1096 goto failed;
1097
1098 if (endp->trunk->omit_rtcp)
1099 return rc;
1100
1101 was_rtcp = 1;
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001102 rtcp_addr = conn->end.addr;
1103 osmo_sockaddr_set_port(&rtcp_addr.u.sa, ntohs(conn->end.rtcp_port));
1104 rc = mgcp_udp_send(conn->end.rtcp.fd, &rtcp_addr,
1105 rtp_dummy_payload, sizeof(rtp_dummy_payload));
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001106
1107 if (rc >= 0)
1108 return rc;
1109
1110failed:
1111 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1112 "Failed to send dummy %s packet.\n",
1113 was_rtcp ? "RTCP" : "RTP");
1114
1115 return -1;
1116}
1117
1118/*! Send RTP/RTCP data to a specified destination connection.
1119 * \param[in] endp associated endpoint (for configuration, logging).
1120 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP.
Philipp Maiercca1d182023-04-06 15:13:39 +02001121 * \param[in] addr spoofed source address (set to NULL to disable).
1122 * \param[in] msg message buffer that contains the RTP/RTCP data.
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001123 * \param[in] conn_src associated source connection.
1124 * \param[in] conn_dst associated destination connection.
Philipp Maiercca1d182023-04-06 15:13:39 +02001125 * \returns 0 on success, negative on ERROR. */
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001126int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct osmo_sockaddr *addr,
1127 struct msgb *msg, struct mgcp_conn_rtp *conn_src,
1128 struct mgcp_conn_rtp *conn_dst)
1129{
1130 /*! When no destination connection is available (e.g. when only one
1131 * connection in loopback mode exists), then the source connection
1132 * shall be specified as destination connection */
1133
1134 struct mgcp_trunk *trunk = endp->trunk;
1135 struct mgcp_rtp_end *rtp_end;
1136 struct mgcp_rtp_state *rtp_state;
1137 char ipbuf[INET6_ADDRSTRLEN];
1138 char *dest_name;
1139 int rc;
1140 int len;
1141
1142 OSMO_ASSERT(conn_src);
1143 OSMO_ASSERT(conn_dst);
1144
1145 if (is_rtp)
1146 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTP packet...\n");
1147 else
1148 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTCP packet...\n");
1149
Neels Hofmeyrc1468ef2022-10-24 01:00:26 +02001150 /* Patch the payload type number: translate from conn_src to conn_dst.
1151 * Do not patch for IuUP, where the correct payload type number is already set in bridge_iuup_to_rtp_peer():
1152 * IuUP -> AMR: calls this function, skip patching if conn_src is IuUP.
1153 * {AMR or IuUP} -> IuUP: calls mgcp_udp_send() directly, skipping this function: No need to examine dst. */
1154 if (is_rtp && !mgcp_conn_rtp_is_iuup(conn_src)) {
Philipp Maier3d0676a2023-04-26 15:47:51 +02001155 if (mgcp_patch_pt(conn_dst, msg) < 0) {
1156 LOGPENDP(endp, DRTP, LOGL_NOTICE, "unable to patch payload type RTP packet, discarding...\n");
1157 return -EINVAL;
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001158 }
1159 }
1160
1161 /* Note: In case of loopback configuration, both, the source and the
1162 * destination will point to the same connection. */
1163 rtp_end = &conn_dst->end;
1164 rtp_state = &conn_src->state;
1165 dest_name = conn_dst->conn->name;
1166
1167 /* Ensure we have an alternative SSRC in case we need it, see also
1168 * gen_rtp_header() */
1169 if (rtp_state->alt_rtp_tx_ssrc == 0)
1170 rtp_state->alt_rtp_tx_ssrc = rand();
1171
1172 if (!rtp_end->output_enabled) {
1173 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_DROPPED_PACKETS_CTR);
1174 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1175 "output disabled, drop to %s %s "
1176 "rtp_port:%u rtcp_port:%u\n",
1177 dest_name,
1178 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001179 osmo_sockaddr_port(&rtp_end->addr.u.sa), ntohs(rtp_end->rtcp_port)
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001180 );
1181 } else if (is_rtp) {
1182 int cont;
1183 int nbytes = 0;
1184 int buflen = msgb_length(msg);
1185
1186 /* Make sure we have a valid RTP header, in cases where no RTP
1187 * header is present, we will generate one. */
1188 gen_rtp_header(msg, rtp_end, rtp_state);
1189
1190 do {
1191 /* Run transcoder */
Ericfbf78d12021-08-23 22:31:39 +02001192 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 +02001193 if (cont < 0)
1194 break;
1195
1196 if (addr)
1197 mgcp_patch_and_count(endp, rtp_state, rtp_end,
1198 addr, msg);
1199
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001200 if (mgcp_conn_rtp_is_iuup(conn_dst) || mgcp_conn_rtp_is_iuup(conn_src)) {
1201 /* the iuup code will correctly transform to the correct AMR mode */
Pau Espin Pedrolcca55242022-10-24 17:14:33 +02001202 } else if (mgcp_codec_amr_align_mode_is_indicated(conn_dst->end.codec)) {
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001203 rc = amr_oa_bwe_convert(endp, msg,
1204 conn_dst->end.codec->param.amr_octet_aligned);
1205 if (rc < 0) {
1206 LOGPENDP(endp, DRTP, LOGL_ERROR,
Pau Espin Pedrol9ac9cb92022-10-26 12:36:48 +02001207 "Error in AMR octet-aligned <-> bandwidth-efficient mode conversion (target=%s)\n",
1208 conn_dst->end.codec->param.amr_octet_aligned ? "octet-aligned" : "bandwidth-efficient");
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001209 break;
1210 }
1211 } else if (rtp_end->rfc5993_hr_convert &&
1212 strcmp(conn_src->end.codec->subtype_name, "GSM-HR-08") == 0) {
1213 rc = rfc5993_hr_convert(endp, msg);
1214 if (rc < 0) {
1215 LOGPENDP(endp, DRTP, LOGL_ERROR, "Error while converting to GSM-HR-08\n");
1216 break;
1217 }
1218 }
1219
1220 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1221 "process/send to %s %s "
1222 "rtp_port:%u rtcp_port:%u\n",
1223 dest_name,
1224 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001225 osmo_sockaddr_port(&rtp_end->addr.u.sa), ntohs(rtp_end->rtcp_port)
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001226 );
1227
1228 /* Forward a copy of the RTP data to a debug ip/port */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001229 forward_data_tap(rtp_end->rtp.fd, &conn_src->tap_out,
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001230 msg);
1231
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001232 len = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr,
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001233 (char *)msgb_data(msg), msgb_length(msg));
1234
1235 if (len <= 0)
1236 return len;
1237
1238 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
1239 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
1240 rtp_state->alt_rtp_tx_sequence++;
1241
1242 nbytes += len;
1243 buflen = cont;
1244 } while (buflen > 0);
1245 return nbytes;
1246 } else if (!trunk->omit_rtcp) {
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001247 struct osmo_sockaddr rtcp_addr = rtp_end->addr;
1248 osmo_sockaddr_set_port(&rtcp_addr.u.sa, rtp_end->rtcp_port);
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001249 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1250 "send to %s %s rtp_port:%u rtcp_port:%u\n",
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001251 dest_name, osmo_sockaddr_ntop(&rtcp_addr.u.sa, ipbuf),
1252 osmo_sockaddr_port(&rtp_end->addr.u.sa),
1253 osmo_sockaddr_port(&rtcp_addr.u.sa)
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001254 );
1255
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001256 len = mgcp_udp_send(rtp_end->rtcp.fd, &rtcp_addr,
1257 (char *)msgb_data(msg), msgb_length(msg));
Pau Espin Pedrol62d9df62021-07-06 16:39:09 +02001258
1259 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
1260 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
1261 rtp_state->alt_rtp_tx_sequence++;
1262
1263 return len;
1264 }
1265
1266 return 0;
1267}
1268
Pau Espin Pedrolde805b62022-10-03 16:08:58 +02001269/*! Dispatch incoming RTP packet to opposite RTP connection.
1270 * \param[in] msg Message buffer to bridge, coming from source connection.
1271 * msg shall contain "struct osmo_rtp_msg_ctx *" attached in
1272 * "OSMO_RTP_MSG_CTX(msg)".
1273 * \returns 0 on success, -1 on ERROR.
1274 */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001275int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001276{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001277 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1278 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1279 struct mgcp_conn *conn = conn_src->conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001280 struct mgcp_conn *conn_dst;
Andreas Eversbergaf671782023-07-05 12:51:40 +02001281 struct mgcp_endpoint *endp = conn->endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001282 struct osmo_sockaddr *from_addr = mc->from_addr;
Philipp Maier97a93122021-05-07 22:50:31 +02001283 char ipbuf[INET6_ADDRSTRLEN];
Andreas Eversbergaf671782023-07-05 12:51:40 +02001284 int rc = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001285
1286 /*! NOTE: This callback function implements the endpoint specific
Alexander Chemeris61cf9bb2020-05-11 18:13:53 +03001287 * dispatch behaviour of an rtp bridge/proxy endpoint. It is assumed
Philipp Maier87bd9be2017-08-22 16:35:41 +02001288 * that the endpoint will hold only two connections. This premise
1289 * is used to determine the opposite connection (it is always the
1290 * connection that is not the originating connection). Once the
1291 * destination connection is known the RTP packet is sent via
1292 * the destination connection. */
1293
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001294 /* If source is IuUP, we need to handle state, forward it through specific bridge path: */
1295 if (mgcp_conn_rtp_is_iuup(conn_src) && mc->proto == MGCP_PROTO_RTP)
1296 return mgcp_conn_iuup_dispatch_rtp(msg);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001297
1298 /* Check if the connection is in loopback mode, if yes, just send the
1299 * incoming data back to the origin */
1300 if (conn->mode == MGCP_CONN_LOOPBACK) {
1301 /* When we are in loopback mode, we loop back all incoming
1302 * packets back to their origin. We will use the originating
1303 * address data from the UDP packet header to patch the
1304 * outgoing address in connection on the fly */
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001305 if (osmo_sockaddr_port(&conn->u.rtp.end.addr.u.sa) == 0) {
Philipp Maier97a93122021-05-07 22:50:31 +02001306 memcpy(&conn->u.rtp.end.addr, from_addr,
1307 sizeof(conn->u.rtp.end.addr));
Philipp Maier97a93122021-05-07 22:50:31 +02001308 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1309 "loopback mode: implicitly using source address (%s:%u) as destination address\n",
1310 osmo_sockaddr_ntop(&from_addr->u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001311 osmo_sockaddr_port(&conn->u.rtp.end.addr.u.sa));
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001312 }
Pau Espin Pedrol6db3f7f2022-10-24 17:55:02 +02001313 return mgcp_conn_rtp_dispatch_rtp(conn_src, msg);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001314 }
1315
Andreas Eversbergaf671782023-07-05 12:51:40 +02001316 /* If the mode does not allow receiving RTP, we are done. */
1317 switch (conn->mode) {
1318 case MGCP_CONN_RECV_ONLY:
1319 case MGCP_CONN_RECV_SEND:
1320 case MGCP_CONN_CONFECHO:
1321 break;
1322 default:
1323 return rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001324 }
1325
Andreas Eversbergaf671782023-07-05 12:51:40 +02001326 /* If the mode is "confecho", send RTP back to the sender. */
1327 if (conn->mode == MGCP_CONN_CONFECHO)
1328 rc = mgcp_conn_rtp_dispatch_rtp(conn_src, msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001329
Andreas Eversbergaf671782023-07-05 12:51:40 +02001330 /* Dispatch RTP packet to all other connection(s) that send audio. */
1331 llist_for_each_entry(conn_dst, &endp->conns, entry) {
1332 if (conn_dst == conn)
1333 continue;
1334 switch (conn_dst->mode) {
1335 case MGCP_CONN_SEND_ONLY:
1336 case MGCP_CONN_RECV_SEND:
1337 case MGCP_CONN_CONFECHO:
1338 rc = mgcp_conn_rtp_dispatch_rtp(&conn_dst->u.rtp, msg);
1339 break;
1340 default:
1341 break;
1342 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001343 }
Andreas Eversbergaf671782023-07-05 12:51:40 +02001344 return rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001345}
1346
Philipp Maier0996a1e2020-06-10 15:27:14 +02001347/*! dispatch incoming RTP packet to E1 subslot, handle RTCP packets locally.
1348 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1349 * \param[in] addr socket address where the RTP packet has been received from.
1350 * \param[in] buf buffer that hold the RTP payload.
1351 * \param[in] buf_size size data length of buf.
1352 * \param[in] conn originating connection.
1353 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001354int mgcp_dispatch_e1_bridge_cb(struct msgb *msg)
Philipp Maier0996a1e2020-06-10 15:27:14 +02001355{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001356 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1357 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1358 struct mgcp_conn *conn = conn_src->conn;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001359 struct osmo_sockaddr *from_addr = mc->from_addr;
Philipp Maier97a93122021-05-07 22:50:31 +02001360 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001361
Philipp Maier889fe7f2020-07-06 17:44:12 +02001362 /* Check if the connection is in loopback mode, if yes, just send the
1363 * incoming data back to the origin */
1364 if (conn->mode == MGCP_CONN_LOOPBACK) {
1365 /* When we are in loopback mode, we loop back all incoming
1366 * packets back to their origin. We will use the originating
1367 * address data from the UDP packet header to patch the
1368 * outgoing address in connection on the fly */
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001369 if (osmo_sockaddr_port(&conn->u.rtp.end.addr.u.sa) == 0) {
Philipp Maier97a93122021-05-07 22:50:31 +02001370 memcpy(&conn->u.rtp.end.addr, from_addr,
1371 sizeof(conn->u.rtp.end.addr));
Philipp Maier97a93122021-05-07 22:50:31 +02001372 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1373 "loopback mode: implicitly using source address (%s:%u) as destination address\n",
1374 osmo_sockaddr_ntop(&from_addr->u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001375 osmo_sockaddr_port(&conn->u.rtp.end.addr.u.sa));
Philipp Maier889fe7f2020-07-06 17:44:12 +02001376 }
Pau Espin Pedrol6db3f7f2022-10-24 17:55:02 +02001377 return mgcp_conn_rtp_dispatch_rtp(conn_src, msg);
Philipp Maier889fe7f2020-07-06 17:44:12 +02001378 }
1379
1380 /* Forward to E1 */
1381 return mgcp_e1_send_rtp(conn->endp, conn->u.rtp.end.codec, msg);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001382}
1383
Philipp Maierdf5d2192018-01-24 11:39:32 +01001384/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1385 * \param[in] endp Endpoint on which the connection resides.
Philipp Maier08eb9352020-06-18 11:55:35 +02001386 * \param[in] conn Connection that is about to be removed (ignored). */
Philipp Maierdf5d2192018-01-24 11:39:32 +01001387void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1388{
1389 struct mgcp_conn *conn_cleanup;
1390
1391 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1392 * pointer to the destination connection, so that we do not have
1393 * to go through the list every time an RTP packet arrives. To prevent
1394 * a use-after-free situation we invalidate this information for all
1395 * connections present when one connection is removed from the
1396 * endpoint. */
1397 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
Pau Espin Pedrol6d0a59a2020-09-08 16:50:22 +02001398 if (conn_cleanup->priv == conn)
1399 conn_cleanup->priv = NULL;
Philipp Maierdf5d2192018-01-24 11:39:32 +01001400 }
1401}
1402
Philipp Maier0996a1e2020-06-10 15:27:14 +02001403/*! cleanup an endpoint when a connection on an E1 endpoint is removed.
1404 * \param[in] endp Endpoint on which the connection resides.
1405 * \param[in] conn Connection that is about to be removed (ignored). */
1406void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1407{
Philipp Maier889fe7f2020-07-06 17:44:12 +02001408 /* Cleanup tasks for E1 are the same as for regular endpoint. The
1409 * shut down of the E1 part is handled separately. */
1410 mgcp_cleanup_rtp_bridge_cb(endp, conn);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001411}
1412
Philipp Maier87bd9be2017-08-22 16:35:41 +02001413/* Handle incoming RTP data from NET */
1414static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1415{
1416 /* NOTE: This is a generic implementation. RTP data is received. In
1417 * case of loopback the data is just sent back to its origin. All
1418 * other cases implement endpoint specific behaviour (e.g. how is the
1419 * destination connection determined?). That specific behaviour is
1420 * implemented by the callback function that is called at the end of
1421 * the function */
1422
1423 struct mgcp_conn_rtp *conn_src;
1424 struct mgcp_endpoint *endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001425 struct osmo_sockaddr addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001426 socklen_t slen = sizeof(addr);
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001427 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001428 int ret;
1429 enum rtp_proto proto;
1430 struct osmo_rtp_msg_ctx *mc;
Eric8f333032021-08-13 00:00:43 +02001431 struct msgb *msg;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001432 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001433
1434 conn_src = (struct mgcp_conn_rtp *)fd->data;
1435 OSMO_ASSERT(conn_src);
1436 endp = conn_src->conn->endp;
1437 OSMO_ASSERT(endp);
Eric8f333032021-08-13 00:00:43 +02001438 msg = msgb_alloc_c(endp->trunk, RTP_BUF_SIZE, "RTP-rx");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001439
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001440 proto = (fd == &conn_src->end.rtp)? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001441
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001442 ret = recvfrom(fd->fd, msgb_data(msg), msg->data_len, 0, (struct sockaddr *)&addr.u.sa, &slen);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001443
1444 if (ret <= 0) {
1445 LOG_CONN_RTP(conn_src, LOGL_ERROR, "recvfrom error: %s\n", strerror(errno));
1446 rc = -1;
1447 goto out;
1448 }
1449
1450 msgb_put(msg, ret);
1451
1452 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "%s: rx %u bytes from %s:%u\n",
Philipp Maier069dd162022-03-30 16:09:22 +02001453 proto == MGCP_PROTO_RTP ? "RTP" : "RTCP",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001454 msgb_length(msg), osmo_sockaddr_ntop(&addr.u.sa, ipbuf),
1455 osmo_sockaddr_port(&addr.u.sa));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001456
1457 if ((proto == MGCP_PROTO_RTP && check_rtp(conn_src, msg))
1458 || (proto == MGCP_PROTO_RTCP && check_rtcp(conn_src, msg))) {
1459 /* Logging happened in the two check_ functions */
1460 rc = -1;
1461 goto out;
1462 }
1463
Philipp Maierb3d14eb2021-05-20 14:18:52 +02001464 if (mgcp_is_rtp_dummy_payload(msg)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001465 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx dummy packet (dropped)\n");
1466 rc = 0;
1467 goto out;
1468 }
1469
1470 /* Since the msgb remains owned and freed by this function, the msg ctx data struct can just be on the stack and
1471 * needs not be allocated with the msgb. */
1472 mc = OSMO_RTP_MSG_CTX(msg);
1473 *mc = (struct osmo_rtp_msg_ctx){
1474 .proto = proto,
1475 .conn_src = conn_src,
1476 .from_addr = &addr,
1477 };
1478 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "msg ctx: %d %p %s\n",
1479 mc->proto, mc->conn_src,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001480 osmo_hexdump((void*)mc->from_addr,
1481 mc->from_addr->u.sa.sa_family == AF_INET6 ?
1482 sizeof(struct sockaddr_in6) :
1483 sizeof(struct sockaddr_in)));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001484
1485 /* Increment RX statistics */
Pau Espin Pedroldaf5bce2022-09-22 19:14:24 +02001486 rate_ctr_inc(rate_ctr_group_get_ctr(conn_src->ctrg, RTP_PACKETS_RX_CTR));
1487 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 +02001488 /* FIXME: count RTP and RTCP separately, also count IuUP payload-less separately */
1489
1490 /* Forward a copy of the RTP data to a debug ip/port */
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001491 forward_data_tap(fd->fd, &conn_src->tap_in, msg);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001492
1493 rc = rx_rtp(msg);
1494
1495out:
1496 msgb_free(msg);
1497 return rc;
1498}
1499
Philipp Maiere1442752022-03-30 16:06:15 +02001500/* Note: This function is able to handle RTP and RTCP */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001501static int rx_rtp(struct msgb *msg)
1502{
1503 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1504 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001505 struct osmo_sockaddr *from_addr = mc->from_addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001506 struct mgcp_conn *conn = conn_src->conn;
1507 struct mgcp_trunk *trunk = conn->endp->trunk;
1508
1509 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx_rtp(%u bytes)\n", msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001510
Pau Espin Pedrol58c0b0a2022-10-24 17:36:13 +02001511 /* Check if the origin of the RTP packet seems plausible */
1512 if (!trunk->rtp_accept_all && check_rtp_origin(conn_src, from_addr))
1513 return -1;
Oliver Smithe36b7752019-01-22 16:31:36 +01001514
Philipp Maier82dfb502023-01-19 14:27:17 +01001515 /* Handle AMR frame format conversion (octet-aligned vs. bandwith-efficient) */
Philipp Maiere1442752022-03-30 16:06:15 +02001516 if (mc->proto == MGCP_PROTO_RTP &&
Pau Espin Pedrolcca55242022-10-24 17:14:33 +02001517 mgcp_codec_amr_align_mode_is_indicated(conn_src->end.codec)) {
Philipp Maier82dfb502023-01-19 14:27:17 +01001518 /* Make sure that the incoming AMR frame format matches the frame format that the call agent has
1519 * communicated via SDP when the connection was created/modfied. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001520 int oa = amr_oa_check((char*)msgb_data(msg), msgb_length(msg));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +02001521 if (oa < 0)
1522 return -1;
Pau Espin Pedrold266c372022-10-24 16:57:39 +02001523 if (((bool)oa) != conn_src->end.codec->param.amr_octet_aligned) {
1524 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1525 "rx_rtp(%u bytes): Expected RTP AMR octet-aligned=%u but got octet-aligned=%u."
1526 " check the config of your call-agent!\n",
1527 msgb_length(msg), conn_src->end.codec->param.amr_octet_aligned, oa);
Philipp Maier228e5912019-03-05 13:56:59 +01001528 return -1;
Pau Espin Pedrold266c372022-10-24 16:57:39 +02001529 }
Philipp Maier228e5912019-03-05 13:56:59 +01001530 }
1531
Pau Espin Pedrol58c0b0a2022-10-24 17:36:13 +02001532 mgcp_conn_watchdog_kick(conn_src->conn);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001533
Philipp Maier87bd9be2017-08-22 16:35:41 +02001534 /* Execute endpoint specific implementation that handles the
1535 * dispatching of the RTP data */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001536 return conn->endp->type->dispatch_rtp_cb(msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001537}
1538
Philipp Maier87bd9be2017-08-22 16:35:41 +02001539/*! bind RTP port to osmo_fd.
Philipp Maier0b79d212020-06-18 12:02:49 +02001540 * \param[in] source_addr source (local) address to bind on.
1541 * \param[in] fd associated file descriptor.
1542 * \param[in] port to bind on.
Harald Welte55a92292021-04-28 19:06:34 +02001543 * \param[in] dscp IP DSCP value to use.
1544 * \param[in] prio socket priority to use.
Philipp Maier0b79d212020-06-18 12:02:49 +02001545 * \returns 0 on success, -1 on ERROR. */
Harald Welte55a92292021-04-28 19:06:34 +02001546int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port, uint8_t dscp,
1547 uint8_t prio)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001548{
Harald Welte8890dfa2017-11-17 15:09:30 +01001549 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001550
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001551 rc = osmo_sock_init2(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
Harald Welte55a92292021-04-28 19:06:34 +02001552 NULL, 0, OSMO_SOCK_F_BIND | OSMO_SOCK_F_DSCP(dscp) |
1553 OSMO_SOCK_F_PRIO(prio));
Harald Welte8890dfa2017-11-17 15:09:30 +01001554 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001555 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001556 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001557 return -1;
1558 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001559 fd->fd = rc;
1560 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001561
1562 return 0;
1563}
1564
Philipp Maier87bd9be2017-08-22 16:35:41 +02001565/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001566static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001567 struct mgcp_rtp_end *rtp_end, struct mgcp_endpoint *endp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001568{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001569 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1570 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1571
Harald Welte55a92292021-04-28 19:06:34 +02001572 if (mgcp_create_bind(source_addr, &rtp_end->rtp, rtp_end->local_port,
1573 cfg->endp_dscp, cfg->endp_priority) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001574 LOGPENDP(endp, DRTP, LOGL_ERROR,
1575 "failed to create RTP port: %s:%d\n",
1576 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001577 goto cleanup0;
1578 }
1579
Harald Welte55a92292021-04-28 19:06:34 +02001580 if (mgcp_create_bind(source_addr, &rtp_end->rtcp, rtp_end->local_port + 1,
1581 cfg->endp_dscp, cfg->endp_priority) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001582 LOGPENDP(endp, DRTP, LOGL_ERROR,
1583 "failed to create RTCP port: %s:%d\n",
1584 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001585 goto cleanup1;
1586 }
1587
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001588 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001589 LOGPENDP(endp, DRTP, LOGL_ERROR,
1590 "failed to register RTP port %d\n",
1591 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001592 goto cleanup2;
1593 }
1594
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001595 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001596 LOGPENDP(endp, DRTP, LOGL_ERROR,
1597 "failed to register RTCP port %d\n",
1598 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001599 goto cleanup3;
1600 }
1601
1602 return 0;
1603
1604cleanup3:
1605 osmo_fd_unregister(&rtp_end->rtp);
1606cleanup2:
1607 close(rtp_end->rtcp.fd);
1608 rtp_end->rtcp.fd = -1;
1609cleanup1:
1610 close(rtp_end->rtp.fd);
1611 rtp_end->rtp.fd = -1;
1612cleanup0:
1613 return -1;
1614}
1615
Philipp Maier87bd9be2017-08-22 16:35:41 +02001616/*! bind RTP port to endpoint/connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001617 * \param[in] endp endpoint that holds the RTP connection.
1618 * \param[in] rtp_port port number to bind on.
1619 * \param[in] conn associated RTP connection.
1620 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001621int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1622 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001623{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001624 char name[512];
1625 struct mgcp_rtp_end *end;
1626
Philipp Maier01d24a32017-11-21 17:26:09 +01001627 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001628 end = &conn->end;
1629
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001630 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001631 LOGPENDP(endp, DRTP, LOGL_ERROR, "%u was already bound on conn:%s\n",
1632 rtp_port, mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001633
1634 /* Double bindings should never occour! Since we always allocate
1635 * connections dynamically and free them when they are not
1636 * needed anymore, there must be no previous binding leftover.
1637 * Should there be a connection bound twice, we have a serious
1638 * problem and must exit immediately! */
1639 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001640 }
1641
1642 end->local_port = rtp_port;
Harald Weltec2a8f592020-10-19 13:25:41 +02001643 osmo_fd_setup(&end->rtp, -1, OSMO_FD_READ, rtp_data_net, conn, 0);
1644 osmo_fd_setup(&end->rtcp, -1, OSMO_FD_READ, rtp_data_net, conn, 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001645
Ericfbf78d12021-08-23 22:31:39 +02001646 return bind_rtp(endp->trunk->cfg, conn->end.local_addr, end, endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001647}
1648
Philipp Maier87bd9be2017-08-22 16:35:41 +02001649/*! free allocated RTP and RTCP ports.
1650 * \param[in] end RTP end */
1651void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001652{
1653 if (end->rtp.fd != -1) {
Pau Espin Pedrol6c303662023-03-09 18:34:12 +01001654 osmo_fd_unregister(&end->rtp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001655 close(end->rtp.fd);
1656 end->rtp.fd = -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001657 }
1658
1659 if (end->rtcp.fd != -1) {
Pau Espin Pedrol6c303662023-03-09 18:34:12 +01001660 osmo_fd_unregister(&end->rtcp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001661 close(end->rtcp.fd);
1662 end->rtcp.fd = -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001663 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001664}