blob: 1b7c3bdbad5a0584a8983629a20362c8001012a9 [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* The protocol implementation */
3
4/*
5 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2012 by On-Waves
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <string.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <errno.h>
28#include <time.h>
29#include <limits.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020030#include <arpa/inet.h>
31
32#include <osmocom/core/msgb.h>
33#include <osmocom/core/select.h>
Philipp Maier1cb1e382017-11-02 17:16:04 +010034#include <osmocom/core/socket.h>
Philipp Maier4dba7692018-08-03 12:20:52 +020035#include <osmocom/core/byteswap.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020036#include <osmocom/netif/rtp.h>
Philipp Maier228e5912019-03-05 13:56:59 +010037#include <osmocom/netif/amr.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020038#include <osmocom/mgcp/mgcp.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020039#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier993ea6b2020-08-04 18:26:50 +020040#include <osmocom/mgcp/mgcp_network.h>
41#include <osmocom/mgcp/mgcp_protocol.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020042#include <osmocom/mgcp/mgcp_stat.h>
43#include <osmocom/mgcp/osmux.h>
44#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010045#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maierc66ab2c2020-06-02 20:55:34 +020046#include <osmocom/mgcp/mgcp_trunk.h>
Philipp Maier6931f9a2018-07-26 09:29:31 +020047#include <osmocom/mgcp/mgcp_codec.h>
Philipp Maierc3413882017-10-27 12:26:54 +020048#include <osmocom/mgcp/debug.h>
Philipp Maier9fc8a022019-02-20 12:26:52 +010049#include <osmocom/codec/codec.h>
Philipp Maier889fe7f2020-07-06 17:44:12 +020050#include <osmocom/mgcp/mgcp_e1.h>
Philipp Maier6931f9a2018-07-26 09:29:31 +020051
Philipp Maier993ea6b2020-08-04 18:26:50 +020052
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020053#define RTP_SEQ_MOD (1 << 16)
54#define RTP_MAX_DROPOUT 3000
55#define RTP_MAX_MISORDER 100
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020056
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +020057enum rtp_proto {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020058 MGCP_PROTO_RTP,
59 MGCP_PROTO_RTCP,
60};
61
Harald Weltea48ff4a2020-03-08 14:45:08 +010062static void rtpconn_rate_ctr_add(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp,
63 int id, int inc)
64{
65 struct rate_ctr_group *conn_stats = conn_rtp->rate_ctr_group;
Philipp Maierc66ab2c2020-06-02 20:55:34 +020066 struct rate_ctr_group *mgw_stats = endp->trunk->ratectr.all_rtp_conn_stats;
Harald Weltea48ff4a2020-03-08 14:45:08 +010067
Philipp Maierc66ab2c2020-06-02 20:55:34 +020068 /* add to both the per-connection and the global stats */
Pau Espin Pedrol907744e2021-06-04 17:57:34 +020069 rate_ctr_add(rate_ctr_group_get_ctr(conn_stats, id), inc);
70 rate_ctr_add(rate_ctr_group_get_ctr(mgw_stats, id), inc);
Harald Weltea48ff4a2020-03-08 14:45:08 +010071}
72
73static void rtpconn_rate_ctr_inc(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp, int id)
74{
75 rtpconn_rate_ctr_add(conn_rtp, endp, id, 1);
76}
77
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +020078static int rx_rtp(struct msgb *msg);
79
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +020080static bool addr_is_any(struct osmo_sockaddr *osa) {
81 if (osa->u.sa.sa_family == AF_INET6) {
82 struct in6_addr ip6_any = IN6ADDR_ANY_INIT;
83 return memcmp(&osa->u.sin6.sin6_addr,
84 &ip6_any, sizeof(ip6_any)) == 0;
85 } else {
86 return osa->u.sin.sin_addr.s_addr == 0;
87 }
88}
89
Philipp Maier1cb1e382017-11-02 17:16:04 +010090/*! Determine the local rtp bind IP-address.
Philipp Maier0b79d212020-06-18 12:02:49 +020091 * \param[out] addr caller provided memory to store the resulting IP-Address.
92 * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters.
Philipp Maier1cb1e382017-11-02 17:16:04 +010093 *
94 * The local bind IP-address is automatically selected by probing the
95 * IP-Address of the interface that is pointing towards the remote IP-Address,
96 * if no remote IP-Address is known yet, the statically configured
97 * IP-Addresses are used as fallback. */
98void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn)
99{
100
101 struct mgcp_endpoint *endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200102 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier1cb1e382017-11-02 17:16:04 +0100103 int rc;
104 endp = conn->conn->endp;
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200105 bool rem_addr_set = !addr_is_any(&conn->end.addr);
106 char *bind_addr;
Philipp Maier1cb1e382017-11-02 17:16:04 +0100107
108 /* Try probing the local IP-Address */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200109 if (endp->cfg->net_ports.bind_addr_probe && rem_addr_set) {
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200110 rc = osmo_sock_local_ip(addr, osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Philipp Maier1cb1e382017-11-02 17:16:04 +0100111 if (rc < 0)
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200112 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
113 "local interface auto detection failed, using configured addresses...\n");
Philipp Maier1cb1e382017-11-02 17:16:04 +0100114 else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200115 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
116 "selected local rtp bind ip %s by probing using remote ip %s\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200117 addr, osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Philipp Maier1cb1e382017-11-02 17:16:04 +0100118 return;
119 }
120 }
121
Pau Espin Pedrola93c6e92019-05-06 15:23:57 +0200122 /* Select from preconfigured IP-Addresses. We don't have bind_addr for Osmux (yet?). */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200123 if (rem_addr_set) {
Philipp Maier1cb1e382017-11-02 17:16:04 +0100124 /* Check there is a bind IP for the RTP traffic configured,
125 * if so, use that IP-Address */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200126 bind_addr = conn->end.addr.u.sa.sa_family == AF_INET6 ?
127 endp->cfg->net_ports.bind_addr_v6 :
128 endp->cfg->net_ports.bind_addr_v4;
129 } else {
130 /* Choose any of the bind addresses, preferring v6 over v4 */
131 bind_addr = endp->cfg->net_ports.bind_addr_v6;
132 if (!bind_addr)
133 bind_addr = endp->cfg->net_ports.bind_addr_v4;
134 }
135 if (bind_addr) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200136 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
137 "using configured rtp bind ip as local bind ip %s\n",
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200138 bind_addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100139 } else {
140 /* No specific bind IP is configured for the RTP traffic, so
141 * assume the IP where we listen for incoming MGCP messages
142 * as bind IP */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200143 bind_addr = endp->cfg->source_addr;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200144 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200145 "using mgcp bind ip as local rtp bind ip: %s\n", bind_addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100146 }
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200147 osmo_strlcpy(addr, bind_addr, INET6_ADDRSTRLEN);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100148}
149
Philipp Maier87bd9be2017-08-22 16:35:41 +0200150/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200151 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +0200152 * 1/codec_rate seconds. */
153static uint32_t get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200154{
155 struct timespec tp;
156 uint64_t ret;
157
Philipp Maier87bd9be2017-08-22 16:35:41 +0200158 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200159 return 0;
160
161 memset(&tp, 0, sizeof(tp));
162 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maierc3413882017-10-27 12:26:54 +0200163 LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200164
165 /* convert it to 1/unit seconds */
166 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200167 ret *= codec_rate;
168 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200169
170 return ret;
171}
172
Philipp Maier87bd9be2017-08-22 16:35:41 +0200173/*! send udp packet.
Philipp Maier0b79d212020-06-18 12:02:49 +0200174 * \param[in] fd associated file descriptor.
175 * \param[in] addr destination ip-address.
176 * \param[in] port destination UDP port (network byte order).
177 * \param[in] buf buffer that holds the data to be send.
178 * \param[in] len length of the data to be sent.
179 * \returns bytes sent, -1 on error. */
Philipp Maier776846a2021-05-20 14:15:46 +0200180int mgcp_udp_send(int fd, struct osmo_sockaddr *addr, int port, const char *buf, int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200181{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200182 char ipbuf[INET6_ADDRSTRLEN];
183 size_t addr_len;
184 bool is_ipv6 = addr->u.sa.sa_family == AF_INET6;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200185
Philipp Maierc3413882017-10-27 12:26:54 +0200186 LOGP(DRTP, LOGL_DEBUG,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200187 "sending %i bytes length packet to %s:%u ...\n", len,
188 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
189 ntohs(port));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200190
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200191 if (is_ipv6) {
192 addr->u.sin6.sin6_port = port;
193 addr_len = sizeof(addr->u.sin6);
194 } else {
195 addr->u.sin.sin_port = port;
196 addr_len = sizeof(addr->u.sin);
197 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200198
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200199 return sendto(fd, buf, len, 0, &addr->u.sa, addr_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200200}
201
Philipp Maier87bd9be2017-08-22 16:35:41 +0200202/*! send RTP dummy packet (to keep NAT connection open).
Philipp Maier0b79d212020-06-18 12:02:49 +0200203 * \param[in] endp mcgp endpoint that holds the RTP connection.
204 * \param[in] conn associated RTP connection.
205 * \returns bytes sent, -1 on error. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200206int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200207{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200208 int rc;
209 int was_rtcp = 0;
210
Philipp Maier87bd9be2017-08-22 16:35:41 +0200211 OSMO_ASSERT(endp);
212 OSMO_ASSERT(conn);
213
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200214 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,"sending dummy packet... %s\n",
215 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200216
217 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
Philipp Maierb3d14eb2021-05-20 14:18:52 +0200218 conn->end.rtp_port, rtp_dummy_payload, sizeof(rtp_dummy_payload));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200219
220 if (rc == -1)
221 goto failed;
222
Philipp Maier14b27a82020-06-02 20:15:30 +0200223 if (endp->trunk->omit_rtcp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200224 return rc;
225
226 was_rtcp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200227 rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
Philipp Maierb3d14eb2021-05-20 14:18:52 +0200228 conn->end.rtcp_port, rtp_dummy_payload, sizeof(rtp_dummy_payload));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200229
230 if (rc >= 0)
231 return rc;
232
233failed:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200234 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
235 "Failed to send dummy %s packet.\n",
236 was_rtcp ? "RTCP" : "RTP");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200237
238 return -1;
239}
240
Philipp Maier87bd9be2017-08-22 16:35:41 +0200241/* Compute timestamp alignment error */
242static int32_t ts_alignment_error(struct mgcp_rtp_stream_state *sstate,
243 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200244{
245 int32_t timestamp_delta;
246
247 if (ptime == 0)
248 return 0;
249
250 /* Align according to: T - Tlast = k * Tptime */
251 timestamp_delta = timestamp - sstate->last_timestamp;
252
253 return timestamp_delta % ptime;
254}
255
Philipp Maier87bd9be2017-08-22 16:35:41 +0200256/* Check timestamp and sequence number for plausibility */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200257static int check_rtp_timestamp(struct mgcp_endpoint *endp,
258 struct mgcp_rtp_state *state,
259 struct mgcp_rtp_stream_state *sstate,
260 struct mgcp_rtp_end *rtp_end,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200261 struct osmo_sockaddr *addr,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200262 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200263 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200264{
265 int32_t tsdelta;
266 int32_t timestamp_error;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200267 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200268
269 /* Not fully intialized, skip */
270 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
271 return 0;
272
273 if (seq == sstate->last_seq) {
274 if (timestamp != sstate->last_timestamp) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200275 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200276 LOGPENDP(endp, DRTP, LOGL_ERROR,
277 "The %s timestamp delta is != 0 but the sequence "
278 "number %d is the same, "
279 "TS offset: %d, SeqNo offset: %d "
280 "on SSRC: %u timestamp: %u "
281 "from %s:%d\n",
282 text, seq,
283 state->patch.timestamp_offset, state->patch.seq_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200284 sstate->ssrc, timestamp,
285 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
286 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200287 }
288 return 0;
289 }
290
291 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200292 (int32_t)(timestamp - sstate->last_timestamp) /
293 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200294
295 if (tsdelta == 0) {
296 /* Don't update *tsdelta_out */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200297 LOGPENDP(endp, DRTP, LOGL_NOTICE,
298 "The %s timestamp delta is %d "
299 "on SSRC: %u timestamp: %u "
300 "from %s:%d\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200301 text, tsdelta, sstate->ssrc, timestamp,
302 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
303 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200304
305 return 0;
306 }
307
308 if (sstate->last_tsdelta != tsdelta) {
309 if (sstate->last_tsdelta) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200310 LOGPENDP(endp, DRTP, LOGL_INFO,
311 "The %s timestamp delta changes from %d to %d "
312 "on SSRC: %u timestamp: %u from %s:%d\n",
313 text, sstate->last_tsdelta, tsdelta,
314 sstate->ssrc, timestamp,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200315 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
316 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200317 }
318 }
319
320 if (tsdelta_out)
321 *tsdelta_out = tsdelta;
322
323 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200324 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200325
326 if (timestamp_error) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200327 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200328 LOGPENDP(endp, DRTP, LOGL_NOTICE,
329 "The %s timestamp has an alignment error of %d "
330 "on SSRC: %u "
331 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
332 "from %s:%d. ptime: %d\n",
333 text, timestamp_error,
334 sstate->ssrc,
335 (int16_t)(seq - sstate->last_seq),
336 (int32_t)(timestamp - sstate->last_timestamp),
337 tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200338 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
339 osmo_sockaddr_port(&addr->u.sa),
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200340 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200341 }
342 return 1;
343}
344
345/* Set the timestamp offset according to the packet duration. */
346static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp,
347 struct mgcp_rtp_state *state,
348 struct mgcp_rtp_end *rtp_end,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200349 struct osmo_sockaddr *addr,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200350 int16_t delta_seq, uint32_t in_timestamp)
351{
352 int32_t tsdelta = state->packet_duration;
353 int timestamp_offset;
354 uint32_t out_timestamp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200355 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200356
357 if (tsdelta == 0) {
358 tsdelta = state->out_stream.last_tsdelta;
359 if (tsdelta != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200360 LOGPENDP(endp, DRTP, LOGL_NOTICE,
361 "A fixed packet duration is not available, "
362 "using last output timestamp delta instead: %d "
363 "from %s:%d\n", tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200364 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
365 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200366 } else {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200367 tsdelta = rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200368 LOGPENDP(endp, DRTP, LOGL_NOTICE,
369 "Fixed packet duration and last timestamp delta "
370 "are not available, "
371 "using fixed 20ms instead: %d "
372 "from %s:%d\n", tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200373 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
374 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200375 }
376 }
377
378 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
379 timestamp_offset = out_timestamp - in_timestamp;
380
Harald Welte33381352017-12-25 09:44:26 +0100381 if (state->patch.timestamp_offset != timestamp_offset) {
382 state->patch.timestamp_offset = timestamp_offset;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200383 LOGPENDP(endp, DRTP, LOGL_NOTICE,
384 "Timestamp offset change on SSRC: %u "
385 "SeqNo delta: %d, TS offset: %d, "
386 "from %s:%d\n", state->in_stream.ssrc,
387 delta_seq, state->patch.timestamp_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200388 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
389 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200390 }
391
392 return timestamp_offset;
393}
394
395/* Set the timestamp offset according to the packet duration. */
396static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp,
397 struct mgcp_rtp_state *state,
398 struct mgcp_rtp_end *rtp_end,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200399 struct osmo_sockaddr *addr,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200400 uint32_t timestamp)
401{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200402 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200403 int ts_error = 0;
404 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200405 int ptime = state->packet_duration;
406
407 /* Align according to: T + Toffs - Tlast = k * Tptime */
408
Philipp Maier87bd9be2017-08-22 16:35:41 +0200409 ts_error = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100410 timestamp + state->patch.timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200411
Philipp Maier87bd9be2017-08-22 16:35:41 +0200412 /* If there is an alignment error, we have to compensate it */
413 if (ts_error) {
Harald Welte33381352017-12-25 09:44:26 +0100414 state->patch.timestamp_offset += ptime - ts_error;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200415 LOGPENDP(endp, DRTP, LOGL_NOTICE,
416 "Corrected timestamp alignment error of %d on SSRC: %u "
417 "new TS offset: %d, "
418 "from %s:%d\n",
419 ts_error, state->in_stream.ssrc,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200420 state->patch.timestamp_offset,
421 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
422 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200423 }
424
Philipp Maier87bd9be2017-08-22 16:35:41 +0200425 /* Check we really managed to compensate the timestamp
426 * offset. There should not be any remaining error, failing
Harald Welte1d1b98f2017-12-25 10:03:40 +0100427 * here would point to a serous problem with the alignment
428 * error computation function */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200429 ts_check = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100430 timestamp + state->patch.timestamp_offset);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200431 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200432
Philipp Maier87bd9be2017-08-22 16:35:41 +0200433 /* Return alignment error before compensation */
434 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200435}
436
Philipp Maier87bd9be2017-08-22 16:35:41 +0200437/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200438 * \param[in] associated endpoint.
439 * \param[in] destination RTP end.
440 * \param[in,out] pointer to buffer with voice data.
441 * \param[in] voice data length.
442 * \param[in] maximum size of caller provided voice data buffer.
443 * \returns ignores input parameters, return always 0. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200444int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
445 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200446 char *data, int *len, int buf_size)
447{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200448 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200449 return 0;
450}
451
Philipp Maier87bd9be2017-08-22 16:35:41 +0200452/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200453 * \param[in] associated endpoint.
454 * \param[in] destination RTP connnection.
455 * \param[in] source RTP connection.
456 * \returns ignores input parameters, return always 0. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200457int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
Philipp Maieracc10352018-07-19 18:07:57 +0200458 struct mgcp_conn_rtp *conn_dst,
459 struct mgcp_conn_rtp *conn_src)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200460{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200461 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200462 return 0;
463}
464
465void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
Philipp Maier58128252019-03-06 11:28:18 +0100466 const struct mgcp_rtp_codec **codec,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200467 const char **fmtp_extra,
468 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200469{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200470 LOGPENDP(endp, DRTP, LOGL_DEBUG, "conn:%s using format defaults\n",
471 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200472
Philipp Maier58128252019-03-06 11:28:18 +0100473 *codec = conn->end.codec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200474 *fmtp_extra = conn->end.fmtp_extra;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200475}
476
Philipp Maier87bd9be2017-08-22 16:35:41 +0200477void mgcp_rtp_annex_count(struct mgcp_endpoint *endp,
478 struct mgcp_rtp_state *state, const uint16_t seq,
479 const int32_t transit, const uint32_t ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200480{
481 int32_t d;
482
483 /* initialize or re-initialize */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100484 if (!state->stats.initialized || state->stats.ssrc != ssrc) {
485 state->stats.initialized = 1;
486 state->stats.base_seq = seq;
487 state->stats.max_seq = seq - 1;
488 state->stats.ssrc = ssrc;
489 state->stats.jitter = 0;
490 state->stats.transit = transit;
491 state->stats.cycles = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200492 } else {
493 uint16_t udelta;
494
Philipp Maier87bd9be2017-08-22 16:35:41 +0200495 /* The below takes the shape of the validation of
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200496 * Appendix A. Check if there is something weird with
497 * the sequence number, otherwise check for a wrap
498 * around in the sequence number.
499 * It can't wrap during the initialization so let's
500 * skip it here. The Appendix A probably doesn't have
Philipp Maier87bd9be2017-08-22 16:35:41 +0200501 * this issue because of the probation. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100502 udelta = seq - state->stats.max_seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200503 if (udelta < RTP_MAX_DROPOUT) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100504 if (seq < state->stats.max_seq)
505 state->stats.cycles += RTP_SEQ_MOD;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200506 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200507 LOGPENDP(endp, DRTP, LOGL_NOTICE,
508 "RTP seqno made a very large jump on delta: %u\n",
509 udelta);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200510 }
511 }
512
Philipp Maier87bd9be2017-08-22 16:35:41 +0200513 /* Calculate the jitter between the two packages. The TS should be
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200514 * taken closer to the read function. This was taken from the
515 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
Philipp Maier87bd9be2017-08-22 16:35:41 +0200516 * resolution. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100517 d = transit - state->stats.transit;
518 state->stats.transit = transit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200519 if (d < 0)
520 d = -d;
Harald Welte49e3d5a2017-12-25 09:47:57 +0100521 state->stats.jitter += d - ((state->stats.jitter + 8) >> 4);
522 state->stats.max_seq = seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200523}
524
Philipp Maier6931f9a2018-07-26 09:29:31 +0200525/* There may be different payload type numbers negotiated for two connections.
526 * Patch the payload type of an RTP packet so that it uses the payload type
527 * that is valid for the destination connection (conn_dst) */
528static int mgcp_patch_pt(struct mgcp_conn_rtp *conn_src,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200529 struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200530{
531 struct rtp_hdr *rtp_hdr;
532 uint8_t pt_in;
533 int pt_out;
534
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200535 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
536 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
537 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200538 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200539 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200540
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200541 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier6931f9a2018-07-26 09:29:31 +0200542
543 pt_in = rtp_hdr->payload_type;
544 pt_out = mgcp_codec_pt_translate(conn_src, conn_dst, pt_in);
545 if (pt_out < 0)
546 return -EINVAL;
547
548 rtp_hdr->payload_type = (uint8_t) pt_out;
549 return 0;
550}
551
Philipp Maier87bd9be2017-08-22 16:35:41 +0200552/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200553 * some of the supported endpoints (e.g. the nanoBTS) can only handle
554 * one source and this code will patch RTP header to appear as if there
555 * is only one source.
556 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200557 * we receive will be seen as a switch in streams. */
558void mgcp_patch_and_count(struct mgcp_endpoint *endp,
559 struct mgcp_rtp_state *state,
560 struct mgcp_rtp_end *rtp_end,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200561 struct osmo_sockaddr *addr, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200562{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200563 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200564 uint32_t arrival_time;
565 int32_t transit;
566 uint16_t seq;
567 uint32_t timestamp, ssrc;
568 struct rtp_hdr *rtp_hdr;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200569 int payload = rtp_end->codec->payload_type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200570 unsigned int len = msgb_length(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200571
572 if (len < sizeof(*rtp_hdr))
573 return;
574
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200575 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200576 seq = ntohs(rtp_hdr->sequence);
577 timestamp = ntohl(rtp_hdr->timestamp);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200578 arrival_time = get_current_ts(rtp_end->codec->rate);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200579 ssrc = ntohl(rtp_hdr->ssrc);
580 transit = arrival_time - timestamp;
581
582 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc);
583
584 if (!state->initialized) {
585 state->initialized = 1;
586 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100587 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200588 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200589 state->packet_duration =
590 mgcp_rtp_packet_duration(endp, rtp_end);
Philipp Maier0ec1d4e2018-05-16 11:09:42 +0200591 state->out_stream.last_seq = seq - 1;
592 state->out_stream.ssrc = state->patch.orig_ssrc = ssrc;
593 state->out_stream.last_tsdelta = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200594 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200595 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200596 LOGPENDP(endp, DRTP, LOGL_INFO,
597 "initializing stream, SSRC: %u timestamp: %u "
598 "pkt-duration: %d, from %s:%d\n",
599 state->in_stream.ssrc,
600 state->patch.seq_offset, state->packet_duration,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200601 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
602 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200603 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200604 state->packet_duration =
Philipp Maierbc0346e2018-06-07 09:52:16 +0200605 rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200606 LOGPENDP(endp, DRTP, LOGL_NOTICE,
607 "fixed packet duration is not available, "
608 "using fixed 20ms instead: %d from %s:%d\n",
609 state->packet_duration,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200610 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
611 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200612 }
613 } else if (state->in_stream.ssrc != ssrc) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200614 LOGPENDP(endp, DRTP, LOGL_NOTICE,
615 "SSRC changed: %u -> %u "
616 "from %s:%d\n",
617 state->in_stream.ssrc, rtp_hdr->ssrc,
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 state->in_stream.ssrc = ssrc;
622 if (rtp_end->force_constant_ssrc) {
623 int16_t delta_seq;
624
625 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100626 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200627 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200628
629 /* Estimate number of packets that would have been sent */
630 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200631 (arrival_time - state->in_stream.last_arrival_time
632 + state->packet_duration / 2) /
633 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200634
635 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
636 delta_seq, timestamp);
637
Harald Welte33381352017-12-25 09:44:26 +0100638 state->patch.patch_ssrc = 1;
639 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200640 if (rtp_end->force_constant_ssrc != -1)
641 rtp_end->force_constant_ssrc -= 1;
642
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200643 LOGPENDP(endp, DRTP, LOGL_NOTICE,
644 "SSRC patching enabled, SSRC: %u "
645 "SeqNo offset: %d, TS offset: %d "
646 "from %s:%d\n", state->in_stream.ssrc,
647 state->patch.seq_offset, state->patch.timestamp_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200648 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
649 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200650 }
651
652 state->in_stream.last_tsdelta = 0;
653 } else {
654 /* Compute current per-packet timestamp delta */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200655 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
656 addr, seq, timestamp, "input",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200657 &state->in_stream.last_tsdelta);
658
Harald Welte33381352017-12-25 09:44:26 +0100659 if (state->patch.patch_ssrc)
660 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200661 }
662
663 /* Save before patching */
664 state->in_stream.last_timestamp = timestamp;
665 state->in_stream.last_seq = seq;
666 state->in_stream.last_arrival_time = arrival_time;
667
668 if (rtp_end->force_aligned_timing &&
669 state->out_stream.ssrc == ssrc && state->packet_duration)
670 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200671 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
672 timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200673
674 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100675 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200676 rtp_hdr->ssrc = htonl(ssrc);
677
678 /* Apply the offset and store it back to the packet.
679 * This won't change anything if the offset is 0, so the conditional is
680 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100681 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200682 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100683 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200684 rtp_hdr->timestamp = htonl(timestamp);
685
686 /* Check again, whether the timestamps are still valid */
687 if (state->out_stream.ssrc == ssrc)
688 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
689 addr, seq, timestamp, "output",
690 &state->out_stream.last_tsdelta);
691
692 /* Save output values */
693 state->out_stream.last_seq = seq;
694 state->out_stream.last_timestamp = timestamp;
695 state->out_stream.ssrc = ssrc;
696
697 if (payload < 0)
698 return;
699
700#if 0
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200701 LOGPENDP(endp, DRTP, LOGL_DEBUG, "payload hdr payload %u -> endp payload %u\n",
702 rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200703 rtp_hdr->payload_type = payload;
704#endif
705}
706
Philipp Maier9fc8a022019-02-20 12:26:52 +0100707/* There are different dialects used to format and transfer voice data. When
708 * the receiving end expects GSM-HR data to be formated after RFC 5993, this
709 * function is used to convert between RFC 5993 and TS 101318, which we normally
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200710 * use.
711 * Return 0 on sucess, negative on errors like invalid data length. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200712static int rfc5993_hr_convert(struct mgcp_endpoint *endp, struct msgb *msg)
Philipp Maier9fc8a022019-02-20 12:26:52 +0100713{
Philipp Maier9fc8a022019-02-20 12:26:52 +0100714 struct rtp_hdr *rtp_hdr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200715 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200716 LOGPENDP(endp, DRTP, LOGL_ERROR, "AMR RTP packet too short (%d < %zu)\n",
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200717 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200718 return -EINVAL;
719 }
720
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200721 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100722
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200723 if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr)) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100724 /* TS 101318 encoding => RFC 5993 encoding */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200725 msgb_put(msg, 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100726 memmove(rtp_hdr->data + 1, rtp_hdr->data, GSM_HR_BYTES);
727 rtp_hdr->data[0] = 0x00;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200728 } else if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr) + 1) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100729 /* RFC 5993 encoding => TS 101318 encoding */
730 memmove(rtp_hdr->data, rtp_hdr->data + 1, GSM_HR_BYTES);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200731 msgb_trim(msg, msgb_length(msg) - 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100732 } else {
733 /* It is possible that multiple payloads occur in one RTP
734 * packet. This is not supported yet. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200735 LOGPENDP(endp, DRTP, LOGL_ERROR,
736 "cannot figure out how to convert RTP packet\n");
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200737 return -ENOTSUP;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100738 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200739 return 0;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100740}
741
Philipp Maier228e5912019-03-05 13:56:59 +0100742/* For AMR RTP two framing modes are defined RFC3267. There is a bandwith
743 * efficient encoding scheme where all fields are packed together one after
744 * another and an octet aligned mode where all fields are aligned to octet
745 * boundaries. This function is used to convert between the two modes */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200746static int amr_oa_bwe_convert(struct mgcp_endpoint *endp, struct msgb *msg,
Philipp Maier228e5912019-03-05 13:56:59 +0100747 bool target_is_oa)
748{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200749 /* NOTE: the msgb has an allocated length of RTP_BUF_SIZE, so there is
Philipp Maier228e5912019-03-05 13:56:59 +0100750 * plenty of space available to store the slightly larger, converted
751 * data */
Philipp Maier228e5912019-03-05 13:56:59 +0100752 struct rtp_hdr *rtp_hdr;
753 unsigned int payload_len;
754 int rc;
755
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200756 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
757 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 +0200758 return -EINVAL;
759 }
760
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200761 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier228e5912019-03-05 13:56:59 +0100762
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200763 payload_len = msgb_length(msg) - sizeof(struct rtp_hdr);
Philipp Maier228e5912019-03-05 13:56:59 +0100764
765 if (osmo_amr_is_oa(rtp_hdr->data, payload_len)) {
766 if (!target_is_oa)
767 /* Input data is oa an target format is bwe
768 * ==> convert */
769 rc = osmo_amr_oa_to_bwe(rtp_hdr->data, payload_len);
770 else
771 /* Input data is already bew, but we accept it anyway
772 * ==> no conversion needed */
773 rc = payload_len;
774 } else {
775 if (target_is_oa)
776 /* Input data is bwe an target format is oa
777 * ==> convert */
778 rc = osmo_amr_bwe_to_oa(rtp_hdr->data, payload_len,
779 RTP_BUF_SIZE);
780 else
781 /* Input data is already oa, but we accept it anyway
782 * ==> no conversion needed */
783 rc = payload_len;
784 }
785 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200786 LOGPENDP(endp, DRTP, LOGL_ERROR,
787 "AMR RTP packet conversion failed\n");
Philipp Maier228e5912019-03-05 13:56:59 +0100788 return -EINVAL;
789 }
790
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200791 return msgb_trim(msg, rc + sizeof(struct rtp_hdr));
Philipp Maier228e5912019-03-05 13:56:59 +0100792}
793
794/* Check if a conversion between octet-aligned and bandwith-efficient mode is
795 * indicated. */
796static bool amr_oa_bwe_convert_indicated(struct mgcp_rtp_codec *codec)
797{
798 if (codec->param_present == false)
799 return false;
800 if (!codec->param.amr_octet_aligned_present)
801 return false;
802 if (strcmp(codec->subtype_name, "AMR") != 0)
803 return false;
804 return true;
805}
806
807
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200808/* Return whether an RTP packet with AMR payload is in octet-aligned mode.
809 * Return 0 if in bandwidth-efficient mode, 1 for octet-aligned mode, and negative if the RTP data is invalid. */
810static int amr_oa_check(char *data, int len)
Philipp Maier228e5912019-03-05 13:56:59 +0100811{
812 struct rtp_hdr *rtp_hdr;
813 unsigned int payload_len;
814
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200815 if (len < sizeof(struct rtp_hdr))
816 return -EINVAL;
817
Philipp Maier228e5912019-03-05 13:56:59 +0100818 rtp_hdr = (struct rtp_hdr *)data;
819
820 payload_len = len - sizeof(struct rtp_hdr);
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200821 if (payload_len < sizeof(struct amr_hdr))
822 return -EINVAL;
Philipp Maier228e5912019-03-05 13:56:59 +0100823
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200824 return osmo_amr_is_oa(rtp_hdr->data, payload_len) ? 1 : 0;
Philipp Maier228e5912019-03-05 13:56:59 +0100825}
826
Philipp Maier87bd9be2017-08-22 16:35:41 +0200827/* Forward data to a debug tap. This is debug function that is intended for
828 * debugging the voice traffic with tools like gstreamer */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200829static void forward_data(int fd, struct mgcp_rtp_tap *tap, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200830{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100831 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200832
Philipp Maiere6f172d2017-11-07 12:00:01 +0100833 if (!tap->enabled)
834 return;
835
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200836 rc = sendto(fd, msgb_data(msg), msgb_length(msg), 0, (struct sockaddr *)&tap->forward,
Philipp Maiere6f172d2017-11-07 12:00:01 +0100837 sizeof(tap->forward));
838
839 if (rc < 0)
840 LOGP(DRTP, LOGL_ERROR,
841 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200842}
843
Philipp Maier889fe7f2020-07-06 17:44:12 +0200844/* Generate an RTP header if it is missing */
845static void gen_rtp_header(struct msgb *msg, struct mgcp_rtp_end *rtp_end,
846 struct mgcp_rtp_state *state)
847{
848 struct rtp_hdr *hdr = (struct rtp_hdr *)msgb_data(msg);
849
850 if (hdr->version > 0)
851 return;
852
853 hdr->version = 2;
854 hdr->payload_type = rtp_end->codec->payload_type;
855 hdr->timestamp = osmo_htonl(get_current_ts(rtp_end->codec->rate));
856 hdr->sequence = osmo_htons(state->alt_rtp_tx_sequence);
857 hdr->ssrc = state->alt_rtp_tx_ssrc;
858}
859
860
Philipp Maier87bd9be2017-08-22 16:35:41 +0200861/*! Send RTP/RTCP data to a specified destination connection.
Philipp Maier0b79d212020-06-18 12:02:49 +0200862 * \param[in] endp associated endpoint (for configuration, logging).
863 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP.
864 * \param[in] spoofed source address (set to NULL to disable).
865 * \param[in] buf buffer that contains the RTP/RTCP data.
866 * \param[in] len length of the buffer that contains the RTP/RTCP data.
867 * \param[in] conn_src associated source connection.
868 * \param[in] conn_dst associated destination connection.
869 * \returns 0 on success, -1 on ERROR. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200870int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct osmo_sockaddr *addr,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200871 struct msgb *msg, struct mgcp_conn_rtp *conn_src,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200872 struct mgcp_conn_rtp *conn_dst)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200873{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200874 /*! When no destination connection is available (e.g. when only one
875 * connection in loopback mode exists), then the source connection
876 * shall be specified as destination connection */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200877
Philipp Maier14b27a82020-06-02 20:15:30 +0200878 struct mgcp_trunk *trunk = endp->trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200879 struct mgcp_rtp_end *rtp_end;
880 struct mgcp_rtp_state *rtp_state;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200881 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200882 char *dest_name;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200883 int rc;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200884 int len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200885
Philipp Maier87bd9be2017-08-22 16:35:41 +0200886 OSMO_ASSERT(conn_src);
887 OSMO_ASSERT(conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200888
Philipp Maier87bd9be2017-08-22 16:35:41 +0200889 if (is_rtp) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200890 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTP packet...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200891 } else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200892 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTCP packet...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200893 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200894
Philipp Maier6931f9a2018-07-26 09:29:31 +0200895 /* FIXME: It is legal that the payload type on the egress connection is
896 * different from the payload type that has been negotiated on the
897 * ingress connection. Essentially the codecs are the same so we can
898 * match them and patch the payload type. However, if we can not find
899 * the codec pendant (everything ist equal except the PT), we are of
900 * course unable to patch the payload type. A situation like this
901 * should not occur if transcoding is consequently avoided. Until
902 * we have transcoding support in osmo-mgw we can not resolve this. */
Philipp Maierda895b12018-08-03 12:16:37 +0200903 if (is_rtp) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200904 rc = mgcp_patch_pt(conn_src, conn_dst, msg);
Philipp Maierda895b12018-08-03 12:16:37 +0200905 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200906 LOGPENDP(endp, DRTP, LOGL_DEBUG,
907 "can not patch PT because no suitable egress codec was found.\n");
Philipp Maierda895b12018-08-03 12:16:37 +0200908 }
Philipp Maier6931f9a2018-07-26 09:29:31 +0200909 }
910
Philipp Maier87bd9be2017-08-22 16:35:41 +0200911 /* Note: In case of loopback configuration, both, the source and the
912 * destination will point to the same connection. */
913 rtp_end = &conn_dst->end;
914 rtp_state = &conn_src->state;
915 dest_name = conn_dst->conn->name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200916
Philipp Maier889fe7f2020-07-06 17:44:12 +0200917 /* Ensure we have an alternative SSRC in case we need it, see also
918 * gen_rtp_header() */
919 if (rtp_state->alt_rtp_tx_ssrc == 0)
920 rtp_state->alt_rtp_tx_ssrc = rand();
921
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200922 if (!rtp_end->output_enabled) {
Harald Weltea48ff4a2020-03-08 14:45:08 +0100923 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_DROPPED_PACKETS_CTR);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200924 LOGPENDP(endp, DRTP, LOGL_DEBUG,
925 "output disabled, drop to %s %s "
926 "rtp_port:%u rtcp_port:%u\n",
927 dest_name,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200928 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200929 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200930 );
931 } else if (is_rtp) {
932 int cont;
933 int nbytes = 0;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200934 int buflen = msgb_length(msg);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200935
936 /* Make sure we have a valid RTP header, in cases where no RTP
937 * header is present, we will generate one. */
938 gen_rtp_header(msg, rtp_end, rtp_state);
939
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200940 do {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200941 /* Run transcoder */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200942 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200943 (char*)msgb_data(msg), &buflen,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200944 RTP_BUF_SIZE);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200945 if (cont < 0)
946 break;
947
Philipp Maier87bd9be2017-08-22 16:35:41 +0200948 if (addr)
949 mgcp_patch_and_count(endp, rtp_state, rtp_end,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200950 addr, msg);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100951
Philipp Maier228e5912019-03-05 13:56:59 +0100952 if (amr_oa_bwe_convert_indicated(conn_dst->end.codec)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200953 rc = amr_oa_bwe_convert(endp, msg,
Neels Hofmeyr5a6220f2019-08-20 03:09:04 +0200954 conn_dst->end.codec->param.amr_octet_aligned);
955 if (rc < 0) {
956 LOGPENDP(endp, DRTP, LOGL_ERROR,
957 "Error in AMR octet-aligned <-> bandwidth-efficient mode conversion\n");
958 break;
959 }
Philipp Maier228e5912019-03-05 13:56:59 +0100960 }
961 else if (rtp_end->rfc5993_hr_convert
Philipp Maier9fc8a022019-02-20 12:26:52 +0100962 && strcmp(conn_src->end.codec->subtype_name,
Neels Hofmeyr5a6220f2019-08-20 03:09:04 +0200963 "GSM-HR-08") == 0) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200964 rc = rfc5993_hr_convert(endp, msg);
Neels Hofmeyr5a6220f2019-08-20 03:09:04 +0200965 if (rc < 0) {
966 LOGPENDP(endp, DRTP, LOGL_ERROR, "Error while converting to GSM-HR-08\n");
967 break;
968 }
969 }
Philipp Maier9fc8a022019-02-20 12:26:52 +0100970
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200971 LOGPENDP(endp, DRTP, LOGL_DEBUG,
972 "process/send to %s %s "
973 "rtp_port:%u rtcp_port:%u\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200974 dest_name,
975 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200976 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
977 );
Philipp Maier87bd9be2017-08-22 16:35:41 +0200978
979 /* Forward a copy of the RTP data to a debug ip/port */
980 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200981 msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200982
983 /* FIXME: HACK HACK HACK. See OS#2459.
984 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
985 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
986 * cells (as long as we patch only the first RTP payload in each stream).
987 */
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200988 if (!rtp_state->patched_first_rtp_payload
989 && conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200990 uint8_t *data = msgb_data(msg) + 12;
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200991 if (data[0] == 0xe0) {
992 data[0] = 0xe4;
993 data[1] = 0x00;
994 rtp_state->patched_first_rtp_payload = true;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200995 LOGPENDP(endp, DRTP, LOGL_DEBUG,
996 "Patching over first two bytes"
997 " to fake an IuUP Initialization Ack\n");
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200998 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200999 }
1000
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001001 len = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr, rtp_end->rtp_port,
1002 (char*)msgb_data(msg), msgb_length(msg));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001003
Philipp Maier87bd9be2017-08-22 16:35:41 +02001004 if (len <= 0)
1005 return len;
1006
Harald Weltea48ff4a2020-03-08 14:45:08 +01001007 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
1008 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
Philipp Maier889fe7f2020-07-06 17:44:12 +02001009 rtp_state->alt_rtp_tx_sequence++;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001010
1011 nbytes += len;
1012 buflen = cont;
1013 } while (buflen > 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001014 return nbytes;
Philipp Maier14b27a82020-06-02 20:15:30 +02001015 } else if (!trunk->omit_rtcp) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001016 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1017 "send to %s %s rtp_port:%u rtcp_port:%u\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001018 dest_name, osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001019 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
1020 );
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001021
Philipp Maier87bd9be2017-08-22 16:35:41 +02001022 len = mgcp_udp_send(rtp_end->rtcp.fd,
1023 &rtp_end->addr,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001024 rtp_end->rtcp_port, (char*)msgb_data(msg), msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001025
Harald Weltea48ff4a2020-03-08 14:45:08 +01001026 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
1027 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
Philipp Maier889fe7f2020-07-06 17:44:12 +02001028 rtp_state->alt_rtp_tx_sequence++;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001029
1030 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001031 }
1032
1033 return 0;
1034}
1035
Philipp Maier87bd9be2017-08-22 16:35:41 +02001036/* Check if the origin (addr) matches the address/port data of the RTP
1037 * connections. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001038static int check_rtp_origin(struct mgcp_conn_rtp *conn, struct osmo_sockaddr *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001039{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001040 char ipbuf[INET6_ADDRSTRLEN];
1041
1042 if (addr_is_any(&conn->end.addr)) {
Neels Hofmeyrefd645e2018-09-10 13:14:50 +02001043 switch (conn->conn->mode) {
1044 case MGCP_CONN_LOOPBACK:
1045 /* HACK: for IuUP, we want to reply with an IuUP Initialization ACK upon the first RTP
1046 * message received. We currently hackishly accomplish that by putting the endpoint in
1047 * loopback mode and patching over the looped back RTP message to make it look like an
1048 * ack. We don't know the femto cell's IP address and port until the RAB Assignment
1049 * Response is received, but the nano3G expects an IuUP Initialization Ack before it even
1050 * sends the RAB Assignment Response. Hence, if the remote address is 0.0.0.0 and the
1051 * MGCP port is in loopback mode, allow looping back the packet to any source. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001052 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1053 "In loopback mode and remote address not set:"
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001054 " allowing data from address: %s\n",
1055 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +02001056 return 0;
1057
1058 default:
1059 /* Receiving early media before the endpoint is configured. Instead of logging
1060 * this as an error that occurs on every call, keep it more low profile to not
1061 * confuse humans with expected errors. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001062 LOGPCONN(conn->conn, DRTP, LOGL_INFO,
1063 "Rx RTP from %s, but remote address not set:"
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001064 " dropping early media\n",
1065 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +02001066 return -1;
1067 }
Neels Hofmeyr0063ca22018-07-23 18:12:16 +02001068 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001069
Philipp Maier87bd9be2017-08-22 16:35:41 +02001070 /* Note: Check if the inbound RTP data comes from the same host to
1071 * which we send our outgoing RTP traffic. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001072 if (conn->end.addr.u.sa.sa_family != addr->u.sa.sa_family ||
1073 (conn->end.addr.u.sa.sa_family == AF_INET &&
1074 conn->end.addr.u.sin.sin_addr.s_addr != addr->u.sin.sin_addr.s_addr) ||
1075 (conn->end.addr.u.sa.sa_family == AF_INET6 &&
1076 memcmp(&conn->end.addr.u.sin6.sin6_addr, &addr->u.sin6.sin6_addr,
1077 sizeof(struct in6_addr)))) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001078 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001079 "data from wrong address: %s, ",
1080 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Philipp Maierc3413882017-10-27 12:26:54 +02001081 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001082 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001083 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001084 return -1;
1085 }
1086
Philipp Maier87bd9be2017-08-22 16:35:41 +02001087 /* Note: Usually the remote remote port of the data we receive will be
1088 * the same as the remote port where we transmit outgoing RTP traffic
1089 * to (set by MDCX). We use this to check the origin of the data for
1090 * plausibility. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001091 if (ntohs(conn->end.rtp_port) != osmo_sockaddr_port(&addr->u.sa) &&
1092 ntohs(conn->end.rtcp_port) != osmo_sockaddr_port(&addr->u.sa)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001093 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001094 "data from wrong source port: %d, ",
1095 osmo_sockaddr_port(&addr->u.sa));
Philipp Maierc3413882017-10-27 12:26:54 +02001096 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001097 "expected: %d for RTP or %d for RTCP\n",
1098 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001099 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001100 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001101 }
1102
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001103 return 0;
1104}
1105
Philipp Maier87bd9be2017-08-22 16:35:41 +02001106/* Check the if the destination address configuration of an RTP connection
1107 * makes sense */
1108static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001109{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001110 char ipbuf[INET6_ADDRSTRLEN];
1111 bool ip_is_any = addr_is_any(&conn->end.addr);
1112
Philipp Maiere6df0e42018-05-29 14:03:06 +02001113 /* Note: it is legal to create a connection but never setting a port
1114 * and IP-address for outgoing data. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001115 if (ip_is_any && conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001116 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
1117 "destination IP-address and rtp port is (not yet) known (%s:%u)\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001118 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port);
Philipp Maiere6df0e42018-05-29 14:03:06 +02001119 return -1;
1120 }
1121
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001122 if (ip_is_any) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001123 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1124 "destination IP-address is invalid (%s:%u)\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001125 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001126 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001127 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001128
1129 if (conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001130 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1131 "destination rtp port is invalid (%s:%u)\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001132 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001133 return -1;
1134 }
1135
1136 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001137}
1138
Philipp Maier4dba7692018-08-03 12:20:52 +02001139/* Do some basic checks to make sure that the RTCP packets we are going to
1140 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001141static int check_rtcp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +02001142{
1143 struct rtcp_hdr *hdr;
1144 unsigned int len;
1145 uint8_t type;
1146
1147 /* RTPC packets that are just a header without data do not make
1148 * any sense. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001149 if (msgb_length(msg) < sizeof(struct rtcp_hdr)) {
1150 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP packet too short (%u < %zu)\n",
1151 msgb_length(msg), sizeof(struct rtcp_hdr));
Philipp Maier4dba7692018-08-03 12:20:52 +02001152 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001153 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001154
1155 /* Make sure that the length of the received packet does not exceed
1156 * the available buffer size */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001157 hdr = (struct rtcp_hdr *)msgb_data(msg);
Philipp Maier4dba7692018-08-03 12:20:52 +02001158 len = (osmo_ntohs(hdr->length) + 1) * 4;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001159 if (len > msgb_length(msg)) {
1160 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header length exceeds packet size (%u > %u)\n",
1161 len, msgb_length(msg));
Philipp Maier4dba7692018-08-03 12:20:52 +02001162 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001163 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001164
1165 /* Make sure we accept only packets that have a proper packet type set
1166 * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
1167 type = hdr->type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001168 if ((type < 192 || type > 195) && (type < 200 || type > 213)) {
1169 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header: invalid type: %u\n", type);
Philipp Maier4dba7692018-08-03 12:20:52 +02001170 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001171 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001172
1173 return 0;
1174}
1175
1176/* Do some basic checks to make sure that the RTP packets we are going to
1177 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001178static int check_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +02001179{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001180 size_t min_size = sizeof(struct rtp_hdr);
1181 if (msgb_length(msg) < min_size) {
1182 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
1183 msgb_length(msg), min_size);
1184 return -1;
1185 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001186
1187 /* FIXME: Add more checks, the reason why we do not check more than
1188 * the length is because we currently handle IUUP packets as RTP
1189 * packets, so they must pass this check, if we weould be more
1190 * strict here, we would possibly break 3G. (see also FIXME note
1191 * below */
1192
1193 return 0;
1194}
1195
Philipp Maier87bd9be2017-08-22 16:35:41 +02001196/* Send RTP data. Possible options are standard RTP packet
1197 * transmission or trsmission via an osmux connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001198static int mgcp_send_rtp(struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001199{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001200 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1201 enum rtp_proto proto = mc->proto;
1202 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001203 struct mgcp_endpoint *endp = conn_src->conn->endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001204
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001205 LOGPENDP(endp, DRTP, LOGL_DEBUG, "destin conn:%s\n",
1206 mgcp_conn_dump(conn_dst->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001207
1208 /* Before we try to deliver the packet, we check if the destination
1209 * port and IP-Address make sense at all. If not, we will be unable
1210 * to deliver the packet. */
1211 if (check_rtp_destin(conn_dst) != 0)
1212 return -1;
1213
1214 /* Depending on the RTP connection type, deliver the RTP packet to the
1215 * destination connection. */
1216 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001217 case MGCP_RTP_DEFAULT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001218 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1219 "endpoint type is MGCP_RTP_DEFAULT, "
1220 "using mgcp_send() to forward data directly\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001221 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001222 mc->from_addr, msg, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001223 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001224 case MGCP_OSMUX_BSC:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001225 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1226 "endpoint type is MGCP_OSMUX_BSC_NAT, "
1227 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n");
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001228 return osmux_xfrm_to_osmux((char*)msgb_data(msg), msgb_length(msg), conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001229 }
1230
Philipp Maier87bd9be2017-08-22 16:35:41 +02001231 /* If the data has not been handled/forwarded until here, it will
1232 * be discarded, this should not happen, normally the MGCP type
1233 * should be properly set */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001234 LOGPENDP(endp, DRTP, LOGL_ERROR, "bad MGCP type -- data discarded!\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001235
1236 return -1;
1237}
1238
1239/*! dispatch incoming RTP packet to opposite RTP connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001240 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1241 * \param[in] addr socket address where the RTP packet has been received from.
1242 * \param[in] buf buffer that hold the RTP payload.
1243 * \param[in] buf_size size data length of buf.
1244 * \param[in] conn originating connection.
1245 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001246int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001247{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001248 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1249 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1250 struct mgcp_conn *conn = conn_src->conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001251 struct mgcp_conn *conn_dst;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001252 struct osmo_sockaddr *from_addr = mc->from_addr;
Philipp Maier97a93122021-05-07 22:50:31 +02001253 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001254
1255 /*! NOTE: This callback function implements the endpoint specific
Alexander Chemeris61cf9bb2020-05-11 18:13:53 +03001256 * dispatch behaviour of an rtp bridge/proxy endpoint. It is assumed
Philipp Maier87bd9be2017-08-22 16:35:41 +02001257 * that the endpoint will hold only two connections. This premise
1258 * is used to determine the opposite connection (it is always the
1259 * connection that is not the originating connection). Once the
1260 * destination connection is known the RTP packet is sent via
1261 * the destination connection. */
1262
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001263
1264 /* Check if the connection is in loopback mode, if yes, just send the
1265 * incoming data back to the origin */
1266 if (conn->mode == MGCP_CONN_LOOPBACK) {
1267 /* When we are in loopback mode, we loop back all incoming
1268 * packets back to their origin. We will use the originating
1269 * address data from the UDP packet header to patch the
1270 * outgoing address in connection on the fly */
1271 if (conn->u.rtp.end.rtp_port == 0) {
Philipp Maier97a93122021-05-07 22:50:31 +02001272 memcpy(&conn->u.rtp.end.addr, from_addr,
1273 sizeof(conn->u.rtp.end.addr));
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001274 switch (from_addr->u.sa.sa_family) {
1275 case AF_INET:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001276 conn->u.rtp.end.rtp_port = from_addr->u.sin.sin_port;
1277 break;
1278 case AF_INET6:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001279 conn->u.rtp.end.rtp_port = from_addr->u.sin6.sin6_port;
1280 break;
1281 default:
1282 OSMO_ASSERT(false);
1283 }
Philipp Maier97a93122021-05-07 22:50:31 +02001284 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1285 "loopback mode: implicitly using source address (%s:%u) as destination address\n",
1286 osmo_sockaddr_ntop(&from_addr->u.sa, ipbuf),
1287 conn->u.rtp.end.rtp_port);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001288 }
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001289 return mgcp_send_rtp(conn_src, msg);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001290 }
1291
Philipp Maier87bd9be2017-08-22 16:35:41 +02001292 /* Find a destination connection. */
1293 /* NOTE: This code path runs every time an RTP packet is received. The
1294 * function mgcp_find_dst_conn() we use to determine the detination
1295 * connection will iterate the connection list inside the endpoint.
1296 * Since list iterations are quite costly, we will figure out the
1297 * destination only once and use the optional private data pointer of
1298 * the connection to cache the destination connection pointer. */
1299 if (!conn->priv) {
1300 conn_dst = mgcp_find_dst_conn(conn);
1301 conn->priv = conn_dst;
1302 } else {
1303 conn_dst = (struct mgcp_conn *)conn->priv;
1304 }
1305
1306 /* There is no destination conn, stop here */
1307 if (!conn_dst) {
Alexander Chemerisebb9bf32020-05-11 18:14:31 +03001308 LOGPCONN(conn, DRTP, LOGL_DEBUG,
1309 "no connection to forward an incoming RTP packet to\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001310 return -1;
1311 }
1312
1313 /* The destination conn is not an RTP connection */
1314 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001315 LOGPCONN(conn, DRTP, LOGL_ERROR,
1316 "unable to find suitable destination conn\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001317 return -1;
1318 }
1319
1320 /* Dispatch RTP packet to destination RTP connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001321 return mgcp_send_rtp(&conn_dst->u.rtp, msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001322}
1323
Philipp Maier0996a1e2020-06-10 15:27:14 +02001324/*! dispatch incoming RTP packet to E1 subslot, handle RTCP packets locally.
1325 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1326 * \param[in] addr socket address where the RTP packet has been received from.
1327 * \param[in] buf buffer that hold the RTP payload.
1328 * \param[in] buf_size size data length of buf.
1329 * \param[in] conn originating connection.
1330 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001331int mgcp_dispatch_e1_bridge_cb(struct msgb *msg)
Philipp Maier0996a1e2020-06-10 15:27:14 +02001332{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001333 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1334 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1335 struct mgcp_conn *conn = conn_src->conn;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001336 struct osmo_sockaddr *from_addr = mc->from_addr;
Philipp Maier97a93122021-05-07 22:50:31 +02001337 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001338
Philipp Maier889fe7f2020-07-06 17:44:12 +02001339 /* Check if the connection is in loopback mode, if yes, just send the
1340 * incoming data back to the origin */
1341 if (conn->mode == MGCP_CONN_LOOPBACK) {
1342 /* When we are in loopback mode, we loop back all incoming
1343 * packets back to their origin. We will use the originating
1344 * address data from the UDP packet header to patch the
1345 * outgoing address in connection on the fly */
1346 if (conn->u.rtp.end.rtp_port == 0) {
Philipp Maier97a93122021-05-07 22:50:31 +02001347 memcpy(&conn->u.rtp.end.addr, from_addr,
1348 sizeof(conn->u.rtp.end.addr));
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001349 switch (from_addr->u.sa.sa_family) {
1350 case AF_INET:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001351 conn->u.rtp.end.rtp_port = from_addr->u.sin.sin_port;
1352 break;
1353 case AF_INET6:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001354 conn->u.rtp.end.rtp_port = from_addr->u.sin6.sin6_port;
1355 break;
1356 default:
1357 OSMO_ASSERT(false);
1358 }
Philipp Maier97a93122021-05-07 22:50:31 +02001359 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1360 "loopback mode: implicitly using source address (%s:%u) as destination address\n",
1361 osmo_sockaddr_ntop(&from_addr->u.sa, ipbuf),
1362 conn->u.rtp.end.rtp_port);
Philipp Maier889fe7f2020-07-06 17:44:12 +02001363 }
1364 return mgcp_send_rtp(conn_src, msg);
1365 }
1366
1367 /* Forward to E1 */
1368 return mgcp_e1_send_rtp(conn->endp, conn->u.rtp.end.codec, msg);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001369}
1370
Philipp Maierdf5d2192018-01-24 11:39:32 +01001371/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1372 * \param[in] endp Endpoint on which the connection resides.
Philipp Maier08eb9352020-06-18 11:55:35 +02001373 * \param[in] conn Connection that is about to be removed (ignored). */
Philipp Maierdf5d2192018-01-24 11:39:32 +01001374void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1375{
1376 struct mgcp_conn *conn_cleanup;
1377
1378 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1379 * pointer to the destination connection, so that we do not have
1380 * to go through the list every time an RTP packet arrives. To prevent
1381 * a use-after-free situation we invalidate this information for all
1382 * connections present when one connection is removed from the
1383 * endpoint. */
1384 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
Pau Espin Pedrol6d0a59a2020-09-08 16:50:22 +02001385 if (conn_cleanup->priv == conn)
1386 conn_cleanup->priv = NULL;
Philipp Maierdf5d2192018-01-24 11:39:32 +01001387 }
1388}
1389
Philipp Maier0996a1e2020-06-10 15:27:14 +02001390/*! cleanup an endpoint when a connection on an E1 endpoint is removed.
1391 * \param[in] endp Endpoint on which the connection resides.
1392 * \param[in] conn Connection that is about to be removed (ignored). */
1393void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1394{
Philipp Maier889fe7f2020-07-06 17:44:12 +02001395 /* Cleanup tasks for E1 are the same as for regular endpoint. The
1396 * shut down of the E1 part is handled separately. */
1397 mgcp_cleanup_rtp_bridge_cb(endp, conn);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001398}
1399
Philipp Maier87bd9be2017-08-22 16:35:41 +02001400/* Handle incoming RTP data from NET */
1401static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1402{
1403 /* NOTE: This is a generic implementation. RTP data is received. In
1404 * case of loopback the data is just sent back to its origin. All
1405 * other cases implement endpoint specific behaviour (e.g. how is the
1406 * destination connection determined?). That specific behaviour is
1407 * implemented by the callback function that is called at the end of
1408 * the function */
1409
1410 struct mgcp_conn_rtp *conn_src;
1411 struct mgcp_endpoint *endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001412 struct osmo_sockaddr addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001413 socklen_t slen = sizeof(addr);
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001414 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001415 int ret;
1416 enum rtp_proto proto;
1417 struct osmo_rtp_msg_ctx *mc;
1418 struct msgb *msg = msgb_alloc(RTP_BUF_SIZE, "RTP-rx");
1419 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001420
1421 conn_src = (struct mgcp_conn_rtp *)fd->data;
1422 OSMO_ASSERT(conn_src);
1423 endp = conn_src->conn->endp;
1424 OSMO_ASSERT(endp);
1425
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001426 proto = (fd == &conn_src->end.rtp)? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001427
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001428 ret = recvfrom(fd->fd, msgb_data(msg), msg->data_len, 0, (struct sockaddr *)&addr.u.sa, &slen);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001429
1430 if (ret <= 0) {
1431 LOG_CONN_RTP(conn_src, LOGL_ERROR, "recvfrom error: %s\n", strerror(errno));
1432 rc = -1;
1433 goto out;
1434 }
1435
1436 msgb_put(msg, ret);
1437
1438 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "%s: rx %u bytes from %s:%u\n",
1439 proto == MGCP_PROTO_RTP ? "RTP" : "RTPC",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001440 msgb_length(msg), osmo_sockaddr_ntop(&addr.u.sa, ipbuf),
1441 osmo_sockaddr_port(&addr.u.sa));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001442
1443 if ((proto == MGCP_PROTO_RTP && check_rtp(conn_src, msg))
1444 || (proto == MGCP_PROTO_RTCP && check_rtcp(conn_src, msg))) {
1445 /* Logging happened in the two check_ functions */
1446 rc = -1;
1447 goto out;
1448 }
1449
Philipp Maierb3d14eb2021-05-20 14:18:52 +02001450 if (mgcp_is_rtp_dummy_payload(msg)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001451 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx dummy packet (dropped)\n");
1452 rc = 0;
1453 goto out;
1454 }
1455
1456 /* Since the msgb remains owned and freed by this function, the msg ctx data struct can just be on the stack and
1457 * needs not be allocated with the msgb. */
1458 mc = OSMO_RTP_MSG_CTX(msg);
1459 *mc = (struct osmo_rtp_msg_ctx){
1460 .proto = proto,
1461 .conn_src = conn_src,
1462 .from_addr = &addr,
1463 };
1464 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "msg ctx: %d %p %s\n",
1465 mc->proto, mc->conn_src,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001466 osmo_hexdump((void*)mc->from_addr,
1467 mc->from_addr->u.sa.sa_family == AF_INET6 ?
1468 sizeof(struct sockaddr_in6) :
1469 sizeof(struct sockaddr_in)));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001470
1471 /* Increment RX statistics */
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001472 rate_ctr_inc(rate_ctr_group_get_ctr(conn_src->rate_ctr_group, RTP_PACKETS_RX_CTR));
1473 rate_ctr_add(rate_ctr_group_get_ctr(conn_src->rate_ctr_group, RTP_OCTETS_RX_CTR), msgb_length(msg));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001474 /* FIXME: count RTP and RTCP separately, also count IuUP payload-less separately */
1475
1476 /* Forward a copy of the RTP data to a debug ip/port */
1477 forward_data(fd->fd, &conn_src->tap_in, msg);
1478
1479 rc = rx_rtp(msg);
1480
1481out:
1482 msgb_free(msg);
1483 return rc;
1484}
1485
1486static int rx_rtp(struct msgb *msg)
1487{
1488 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1489 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001490 struct osmo_sockaddr *from_addr = mc->from_addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001491 struct mgcp_conn *conn = conn_src->conn;
1492 struct mgcp_trunk *trunk = conn->endp->trunk;
1493
1494 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx_rtp(%u bytes)\n", msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001495
Oliver Smithe36b7752019-01-22 16:31:36 +01001496 mgcp_conn_watchdog_kick(conn_src->conn);
1497
Philipp Maier228e5912019-03-05 13:56:59 +01001498 /* If AMR is configured for the ingress connection a conversion of the
1499 * framing mode (octet-aligned vs. bandwith-efficient is explicitly
1500 * define, then we check if the incoming payload matches that
1501 * expectation. */
1502 if (amr_oa_bwe_convert_indicated(conn_src->end.codec)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001503 int oa = amr_oa_check((char*)msgb_data(msg), msgb_length(msg));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +02001504 if (oa < 0)
1505 return -1;
1506 if (((bool)oa) != conn_src->end.codec->param.amr_octet_aligned)
Philipp Maier228e5912019-03-05 13:56:59 +01001507 return -1;
1508 }
1509
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001510 /* Check if the origin of the RTP packet seems plausible */
1511 if (!trunk->rtp_accept_all && check_rtp_origin(conn_src, from_addr))
1512 return -1;
1513
Philipp Maier87bd9be2017-08-22 16:35:41 +02001514 /* Execute endpoint specific implementation that handles the
1515 * dispatching of the RTP data */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001516 return conn->endp->type->dispatch_rtp_cb(msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001517}
1518
Philipp Maier87bd9be2017-08-22 16:35:41 +02001519/*! bind RTP port to osmo_fd.
Philipp Maier0b79d212020-06-18 12:02:49 +02001520 * \param[in] source_addr source (local) address to bind on.
1521 * \param[in] fd associated file descriptor.
1522 * \param[in] port to bind on.
Harald Welte55a92292021-04-28 19:06:34 +02001523 * \param[in] dscp IP DSCP value to use.
1524 * \param[in] prio socket priority to use.
Philipp Maier0b79d212020-06-18 12:02:49 +02001525 * \returns 0 on success, -1 on ERROR. */
Harald Welte55a92292021-04-28 19:06:34 +02001526int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port, uint8_t dscp,
1527 uint8_t prio)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001528{
Harald Welte8890dfa2017-11-17 15:09:30 +01001529 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001530
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001531 rc = osmo_sock_init2(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
Harald Welte55a92292021-04-28 19:06:34 +02001532 NULL, 0, OSMO_SOCK_F_BIND | OSMO_SOCK_F_DSCP(dscp) |
1533 OSMO_SOCK_F_PRIO(prio));
Harald Welte8890dfa2017-11-17 15:09:30 +01001534 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001535 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001536 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001537 return -1;
1538 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001539 fd->fd = rc;
1540 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001541
1542 return 0;
1543}
1544
Philipp Maier87bd9be2017-08-22 16:35:41 +02001545/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001546static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001547 struct mgcp_rtp_end *rtp_end, struct mgcp_endpoint *endp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001548{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001549 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1550 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1551
Harald Welte55a92292021-04-28 19:06:34 +02001552 if (mgcp_create_bind(source_addr, &rtp_end->rtp, rtp_end->local_port,
1553 cfg->endp_dscp, cfg->endp_priority) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001554 LOGPENDP(endp, DRTP, LOGL_ERROR,
1555 "failed to create RTP port: %s:%d\n",
1556 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001557 goto cleanup0;
1558 }
1559
Harald Welte55a92292021-04-28 19:06:34 +02001560 if (mgcp_create_bind(source_addr, &rtp_end->rtcp, rtp_end->local_port + 1,
1561 cfg->endp_dscp, cfg->endp_priority) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001562 LOGPENDP(endp, DRTP, LOGL_ERROR,
1563 "failed to create RTCP port: %s:%d\n",
1564 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001565 goto cleanup1;
1566 }
1567
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001568 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001569 LOGPENDP(endp, DRTP, LOGL_ERROR,
1570 "failed to register RTP port %d\n",
1571 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001572 goto cleanup2;
1573 }
1574
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001575 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001576 LOGPENDP(endp, DRTP, LOGL_ERROR,
1577 "failed to register RTCP port %d\n",
1578 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001579 goto cleanup3;
1580 }
1581
1582 return 0;
1583
1584cleanup3:
1585 osmo_fd_unregister(&rtp_end->rtp);
1586cleanup2:
1587 close(rtp_end->rtcp.fd);
1588 rtp_end->rtcp.fd = -1;
1589cleanup1:
1590 close(rtp_end->rtp.fd);
1591 rtp_end->rtp.fd = -1;
1592cleanup0:
1593 return -1;
1594}
1595
Philipp Maier87bd9be2017-08-22 16:35:41 +02001596/*! bind RTP port to endpoint/connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001597 * \param[in] endp endpoint that holds the RTP connection.
1598 * \param[in] rtp_port port number to bind on.
1599 * \param[in] conn associated RTP connection.
1600 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001601int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1602 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001603{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001604 char name[512];
1605 struct mgcp_rtp_end *end;
1606
Philipp Maier01d24a32017-11-21 17:26:09 +01001607 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001608 end = &conn->end;
1609
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001610 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001611 LOGPENDP(endp, DRTP, LOGL_ERROR, "%u was already bound on conn:%s\n",
1612 rtp_port, mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001613
1614 /* Double bindings should never occour! Since we always allocate
1615 * connections dynamically and free them when they are not
1616 * needed anymore, there must be no previous binding leftover.
1617 * Should there be a connection bound twice, we have a serious
1618 * problem and must exit immediately! */
1619 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001620 }
1621
1622 end->local_port = rtp_port;
Harald Weltec2a8f592020-10-19 13:25:41 +02001623 osmo_fd_setup(&end->rtp, -1, OSMO_FD_READ, rtp_data_net, conn, 0);
1624 osmo_fd_setup(&end->rtcp, -1, OSMO_FD_READ, rtp_data_net, conn, 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001625
Pau Espin Pedrol71d42e72020-09-03 14:20:07 +02001626 return bind_rtp(endp->cfg, conn->end.local_addr, end, endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001627}
1628
Philipp Maier87bd9be2017-08-22 16:35:41 +02001629/*! free allocated RTP and RTCP ports.
1630 * \param[in] end RTP end */
1631void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001632{
1633 if (end->rtp.fd != -1) {
1634 close(end->rtp.fd);
1635 end->rtp.fd = -1;
1636 osmo_fd_unregister(&end->rtp);
1637 }
1638
1639 if (end->rtcp.fd != -1) {
1640 close(end->rtcp.fd);
1641 end->rtcp.fd = -1;
1642 osmo_fd_unregister(&end->rtcp);
1643 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001644}