blob: fa40f1f68f33040a42fc73ea5823456ada30c44f [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 */
Harald Weltea48ff4a2020-03-08 14:45:08 +010069 rate_ctr_add(&conn_stats->ctr[id], inc);
Philipp Maierc66ab2c2020-06-02 20:55:34 +020070 rate_ctr_add(&mgw_stats->ctr[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. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200180int mgcp_udp_send(int fd, struct osmo_sockaddr *addr, int port, 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{
208 static char buf[] = { MGCP_DUMMY_LOAD };
209 int rc;
210 int was_rtcp = 0;
211
Philipp Maier87bd9be2017-08-22 16:35:41 +0200212 OSMO_ASSERT(endp);
213 OSMO_ASSERT(conn);
214
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200215 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,"sending dummy packet... %s\n",
216 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200217
218 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
219 conn->end.rtp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200220
221 if (rc == -1)
222 goto failed;
223
Philipp Maier14b27a82020-06-02 20:15:30 +0200224 if (endp->trunk->omit_rtcp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200225 return rc;
226
227 was_rtcp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200228 rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
229 conn->end.rtcp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200230
231 if (rc >= 0)
232 return rc;
233
234failed:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200235 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
236 "Failed to send dummy %s packet.\n",
237 was_rtcp ? "RTCP" : "RTP");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200238
239 return -1;
240}
241
Philipp Maier87bd9be2017-08-22 16:35:41 +0200242/* Compute timestamp alignment error */
243static int32_t ts_alignment_error(struct mgcp_rtp_stream_state *sstate,
244 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200245{
246 int32_t timestamp_delta;
247
248 if (ptime == 0)
249 return 0;
250
251 /* Align according to: T - Tlast = k * Tptime */
252 timestamp_delta = timestamp - sstate->last_timestamp;
253
254 return timestamp_delta % ptime;
255}
256
Philipp Maier87bd9be2017-08-22 16:35:41 +0200257/* Check timestamp and sequence number for plausibility */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200258static int check_rtp_timestamp(struct mgcp_endpoint *endp,
259 struct mgcp_rtp_state *state,
260 struct mgcp_rtp_stream_state *sstate,
261 struct mgcp_rtp_end *rtp_end,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200262 struct osmo_sockaddr *addr,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200263 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200264 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200265{
266 int32_t tsdelta;
267 int32_t timestamp_error;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200268 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200269
270 /* Not fully intialized, skip */
271 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
272 return 0;
273
274 if (seq == sstate->last_seq) {
275 if (timestamp != sstate->last_timestamp) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200276 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200277 LOGPENDP(endp, DRTP, LOGL_ERROR,
278 "The %s timestamp delta is != 0 but the sequence "
279 "number %d is the same, "
280 "TS offset: %d, SeqNo offset: %d "
281 "on SSRC: %u timestamp: %u "
282 "from %s:%d\n",
283 text, seq,
284 state->patch.timestamp_offset, state->patch.seq_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200285 sstate->ssrc, timestamp,
286 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
287 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200288 }
289 return 0;
290 }
291
292 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200293 (int32_t)(timestamp - sstate->last_timestamp) /
294 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200295
296 if (tsdelta == 0) {
297 /* Don't update *tsdelta_out */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200298 LOGPENDP(endp, DRTP, LOGL_NOTICE,
299 "The %s timestamp delta is %d "
300 "on SSRC: %u timestamp: %u "
301 "from %s:%d\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200302 text, tsdelta, sstate->ssrc, timestamp,
303 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
304 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200305
306 return 0;
307 }
308
309 if (sstate->last_tsdelta != tsdelta) {
310 if (sstate->last_tsdelta) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200311 LOGPENDP(endp, DRTP, LOGL_INFO,
312 "The %s timestamp delta changes from %d to %d "
313 "on SSRC: %u timestamp: %u from %s:%d\n",
314 text, sstate->last_tsdelta, tsdelta,
315 sstate->ssrc, timestamp,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200316 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
317 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200318 }
319 }
320
321 if (tsdelta_out)
322 *tsdelta_out = tsdelta;
323
324 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200325 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200326
327 if (timestamp_error) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200328 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200329 LOGPENDP(endp, DRTP, LOGL_NOTICE,
330 "The %s timestamp has an alignment error of %d "
331 "on SSRC: %u "
332 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
333 "from %s:%d. ptime: %d\n",
334 text, timestamp_error,
335 sstate->ssrc,
336 (int16_t)(seq - sstate->last_seq),
337 (int32_t)(timestamp - sstate->last_timestamp),
338 tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200339 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
340 osmo_sockaddr_port(&addr->u.sa),
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200341 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200342 }
343 return 1;
344}
345
346/* Set the timestamp offset according to the packet duration. */
347static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp,
348 struct mgcp_rtp_state *state,
349 struct mgcp_rtp_end *rtp_end,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200350 struct osmo_sockaddr *addr,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200351 int16_t delta_seq, uint32_t in_timestamp)
352{
353 int32_t tsdelta = state->packet_duration;
354 int timestamp_offset;
355 uint32_t out_timestamp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200356 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200357
358 if (tsdelta == 0) {
359 tsdelta = state->out_stream.last_tsdelta;
360 if (tsdelta != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200361 LOGPENDP(endp, DRTP, LOGL_NOTICE,
362 "A fixed packet duration is not available, "
363 "using last output timestamp delta instead: %d "
364 "from %s:%d\n", tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200365 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
366 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200367 } else {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200368 tsdelta = rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200369 LOGPENDP(endp, DRTP, LOGL_NOTICE,
370 "Fixed packet duration and last timestamp delta "
371 "are not available, "
372 "using fixed 20ms instead: %d "
373 "from %s:%d\n", tsdelta,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200374 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
375 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200376 }
377 }
378
379 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
380 timestamp_offset = out_timestamp - in_timestamp;
381
Harald Welte33381352017-12-25 09:44:26 +0100382 if (state->patch.timestamp_offset != timestamp_offset) {
383 state->patch.timestamp_offset = timestamp_offset;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200384 LOGPENDP(endp, DRTP, LOGL_NOTICE,
385 "Timestamp offset change on SSRC: %u "
386 "SeqNo delta: %d, TS offset: %d, "
387 "from %s:%d\n", state->in_stream.ssrc,
388 delta_seq, state->patch.timestamp_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200389 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
390 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200391 }
392
393 return timestamp_offset;
394}
395
396/* Set the timestamp offset according to the packet duration. */
397static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp,
398 struct mgcp_rtp_state *state,
399 struct mgcp_rtp_end *rtp_end,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200400 struct osmo_sockaddr *addr,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200401 uint32_t timestamp)
402{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200403 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200404 int ts_error = 0;
405 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200406 int ptime = state->packet_duration;
407
408 /* Align according to: T + Toffs - Tlast = k * Tptime */
409
Philipp Maier87bd9be2017-08-22 16:35:41 +0200410 ts_error = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100411 timestamp + state->patch.timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200412
Philipp Maier87bd9be2017-08-22 16:35:41 +0200413 /* If there is an alignment error, we have to compensate it */
414 if (ts_error) {
Harald Welte33381352017-12-25 09:44:26 +0100415 state->patch.timestamp_offset += ptime - ts_error;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200416 LOGPENDP(endp, DRTP, LOGL_NOTICE,
417 "Corrected timestamp alignment error of %d on SSRC: %u "
418 "new TS offset: %d, "
419 "from %s:%d\n",
420 ts_error, state->in_stream.ssrc,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200421 state->patch.timestamp_offset,
422 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
423 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200424 }
425
Philipp Maier87bd9be2017-08-22 16:35:41 +0200426 /* Check we really managed to compensate the timestamp
427 * offset. There should not be any remaining error, failing
Harald Welte1d1b98f2017-12-25 10:03:40 +0100428 * here would point to a serous problem with the alignment
429 * error computation function */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200430 ts_check = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100431 timestamp + state->patch.timestamp_offset);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200432 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200433
Philipp Maier87bd9be2017-08-22 16:35:41 +0200434 /* Return alignment error before compensation */
435 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200436}
437
Philipp Maier87bd9be2017-08-22 16:35:41 +0200438/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200439 * \param[in] associated endpoint.
440 * \param[in] destination RTP end.
441 * \param[in,out] pointer to buffer with voice data.
442 * \param[in] voice data length.
443 * \param[in] maximum size of caller provided voice data buffer.
444 * \returns ignores input parameters, return always 0. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200445int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
446 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200447 char *data, int *len, int buf_size)
448{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200449 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200450 return 0;
451}
452
Philipp Maier87bd9be2017-08-22 16:35:41 +0200453/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200454 * \param[in] associated endpoint.
455 * \param[in] destination RTP connnection.
456 * \param[in] source RTP connection.
457 * \returns ignores input parameters, return always 0. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200458int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
Philipp Maieracc10352018-07-19 18:07:57 +0200459 struct mgcp_conn_rtp *conn_dst,
460 struct mgcp_conn_rtp *conn_src)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200461{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200462 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200463 return 0;
464}
465
466void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
Philipp Maier58128252019-03-06 11:28:18 +0100467 const struct mgcp_rtp_codec **codec,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200468 const char **fmtp_extra,
469 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200470{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200471 LOGPENDP(endp, DRTP, LOGL_DEBUG, "conn:%s using format defaults\n",
472 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200473
Philipp Maier58128252019-03-06 11:28:18 +0100474 *codec = conn->end.codec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200475 *fmtp_extra = conn->end.fmtp_extra;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200476}
477
Philipp Maier87bd9be2017-08-22 16:35:41 +0200478void mgcp_rtp_annex_count(struct mgcp_endpoint *endp,
479 struct mgcp_rtp_state *state, const uint16_t seq,
480 const int32_t transit, const uint32_t ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200481{
482 int32_t d;
483
484 /* initialize or re-initialize */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100485 if (!state->stats.initialized || state->stats.ssrc != ssrc) {
486 state->stats.initialized = 1;
487 state->stats.base_seq = seq;
488 state->stats.max_seq = seq - 1;
489 state->stats.ssrc = ssrc;
490 state->stats.jitter = 0;
491 state->stats.transit = transit;
492 state->stats.cycles = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200493 } else {
494 uint16_t udelta;
495
Philipp Maier87bd9be2017-08-22 16:35:41 +0200496 /* The below takes the shape of the validation of
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200497 * Appendix A. Check if there is something weird with
498 * the sequence number, otherwise check for a wrap
499 * around in the sequence number.
500 * It can't wrap during the initialization so let's
501 * skip it here. The Appendix A probably doesn't have
Philipp Maier87bd9be2017-08-22 16:35:41 +0200502 * this issue because of the probation. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100503 udelta = seq - state->stats.max_seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200504 if (udelta < RTP_MAX_DROPOUT) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100505 if (seq < state->stats.max_seq)
506 state->stats.cycles += RTP_SEQ_MOD;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200507 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200508 LOGPENDP(endp, DRTP, LOGL_NOTICE,
509 "RTP seqno made a very large jump on delta: %u\n",
510 udelta);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200511 }
512 }
513
Philipp Maier87bd9be2017-08-22 16:35:41 +0200514 /* Calculate the jitter between the two packages. The TS should be
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200515 * taken closer to the read function. This was taken from the
516 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
Philipp Maier87bd9be2017-08-22 16:35:41 +0200517 * resolution. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100518 d = transit - state->stats.transit;
519 state->stats.transit = transit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200520 if (d < 0)
521 d = -d;
Harald Welte49e3d5a2017-12-25 09:47:57 +0100522 state->stats.jitter += d - ((state->stats.jitter + 8) >> 4);
523 state->stats.max_seq = seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200524}
525
Philipp Maier6931f9a2018-07-26 09:29:31 +0200526/* There may be different payload type numbers negotiated for two connections.
527 * Patch the payload type of an RTP packet so that it uses the payload type
528 * that is valid for the destination connection (conn_dst) */
529static int mgcp_patch_pt(struct mgcp_conn_rtp *conn_src,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200530 struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200531{
532 struct rtp_hdr *rtp_hdr;
533 uint8_t pt_in;
534 int pt_out;
535
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200536 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
537 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
538 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200539 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200540 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200541
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200542 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier6931f9a2018-07-26 09:29:31 +0200543
544 pt_in = rtp_hdr->payload_type;
545 pt_out = mgcp_codec_pt_translate(conn_src, conn_dst, pt_in);
546 if (pt_out < 0)
547 return -EINVAL;
548
549 rtp_hdr->payload_type = (uint8_t) pt_out;
550 return 0;
551}
552
Philipp Maier87bd9be2017-08-22 16:35:41 +0200553/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200554 * some of the supported endpoints (e.g. the nanoBTS) can only handle
555 * one source and this code will patch RTP header to appear as if there
556 * is only one source.
557 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200558 * we receive will be seen as a switch in streams. */
559void mgcp_patch_and_count(struct mgcp_endpoint *endp,
560 struct mgcp_rtp_state *state,
561 struct mgcp_rtp_end *rtp_end,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200562 struct osmo_sockaddr *addr, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200563{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200564 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200565 uint32_t arrival_time;
566 int32_t transit;
567 uint16_t seq;
568 uint32_t timestamp, ssrc;
569 struct rtp_hdr *rtp_hdr;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200570 int payload = rtp_end->codec->payload_type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200571 unsigned int len = msgb_length(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200572
573 if (len < sizeof(*rtp_hdr))
574 return;
575
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200576 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200577 seq = ntohs(rtp_hdr->sequence);
578 timestamp = ntohl(rtp_hdr->timestamp);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200579 arrival_time = get_current_ts(rtp_end->codec->rate);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200580 ssrc = ntohl(rtp_hdr->ssrc);
581 transit = arrival_time - timestamp;
582
583 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc);
584
585 if (!state->initialized) {
586 state->initialized = 1;
587 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100588 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200589 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200590 state->packet_duration =
591 mgcp_rtp_packet_duration(endp, rtp_end);
Philipp Maier0ec1d4e2018-05-16 11:09:42 +0200592 state->out_stream.last_seq = seq - 1;
593 state->out_stream.ssrc = state->patch.orig_ssrc = ssrc;
594 state->out_stream.last_tsdelta = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200595 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200596 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200597 LOGPENDP(endp, DRTP, LOGL_INFO,
598 "initializing stream, SSRC: %u timestamp: %u "
599 "pkt-duration: %d, from %s:%d\n",
600 state->in_stream.ssrc,
601 state->patch.seq_offset, state->packet_duration,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200602 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
603 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200604 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200605 state->packet_duration =
Philipp Maierbc0346e2018-06-07 09:52:16 +0200606 rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200607 LOGPENDP(endp, DRTP, LOGL_NOTICE,
608 "fixed packet duration is not available, "
609 "using fixed 20ms instead: %d from %s:%d\n",
610 state->packet_duration,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200611 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
612 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200613 }
614 } else if (state->in_stream.ssrc != ssrc) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200615 LOGPENDP(endp, DRTP, LOGL_NOTICE,
616 "SSRC changed: %u -> %u "
617 "from %s:%d\n",
618 state->in_stream.ssrc, rtp_hdr->ssrc,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200619 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
620 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200621
622 state->in_stream.ssrc = ssrc;
623 if (rtp_end->force_constant_ssrc) {
624 int16_t delta_seq;
625
626 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100627 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200628 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200629
630 /* Estimate number of packets that would have been sent */
631 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200632 (arrival_time - state->in_stream.last_arrival_time
633 + state->packet_duration / 2) /
634 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200635
636 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
637 delta_seq, timestamp);
638
Harald Welte33381352017-12-25 09:44:26 +0100639 state->patch.patch_ssrc = 1;
640 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200641 if (rtp_end->force_constant_ssrc != -1)
642 rtp_end->force_constant_ssrc -= 1;
643
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200644 LOGPENDP(endp, DRTP, LOGL_NOTICE,
645 "SSRC patching enabled, SSRC: %u "
646 "SeqNo offset: %d, TS offset: %d "
647 "from %s:%d\n", state->in_stream.ssrc,
648 state->patch.seq_offset, state->patch.timestamp_offset,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200649 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
650 osmo_sockaddr_port(&addr->u.sa));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200651 }
652
653 state->in_stream.last_tsdelta = 0;
654 } else {
655 /* Compute current per-packet timestamp delta */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200656 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
657 addr, seq, timestamp, "input",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200658 &state->in_stream.last_tsdelta);
659
Harald Welte33381352017-12-25 09:44:26 +0100660 if (state->patch.patch_ssrc)
661 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200662 }
663
664 /* Save before patching */
665 state->in_stream.last_timestamp = timestamp;
666 state->in_stream.last_seq = seq;
667 state->in_stream.last_arrival_time = arrival_time;
668
669 if (rtp_end->force_aligned_timing &&
670 state->out_stream.ssrc == ssrc && state->packet_duration)
671 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200672 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
673 timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200674
675 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100676 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200677 rtp_hdr->ssrc = htonl(ssrc);
678
679 /* Apply the offset and store it back to the packet.
680 * This won't change anything if the offset is 0, so the conditional is
681 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100682 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200683 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100684 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200685 rtp_hdr->timestamp = htonl(timestamp);
686
687 /* Check again, whether the timestamps are still valid */
688 if (state->out_stream.ssrc == ssrc)
689 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
690 addr, seq, timestamp, "output",
691 &state->out_stream.last_tsdelta);
692
693 /* Save output values */
694 state->out_stream.last_seq = seq;
695 state->out_stream.last_timestamp = timestamp;
696 state->out_stream.ssrc = ssrc;
697
698 if (payload < 0)
699 return;
700
701#if 0
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200702 LOGPENDP(endp, DRTP, LOGL_DEBUG, "payload hdr payload %u -> endp payload %u\n",
703 rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200704 rtp_hdr->payload_type = payload;
705#endif
706}
707
Philipp Maier9fc8a022019-02-20 12:26:52 +0100708/* There are different dialects used to format and transfer voice data. When
709 * the receiving end expects GSM-HR data to be formated after RFC 5993, this
710 * function is used to convert between RFC 5993 and TS 101318, which we normally
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200711 * use.
712 * Return 0 on sucess, negative on errors like invalid data length. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200713static int rfc5993_hr_convert(struct mgcp_endpoint *endp, struct msgb *msg)
Philipp Maier9fc8a022019-02-20 12:26:52 +0100714{
Philipp Maier9fc8a022019-02-20 12:26:52 +0100715 struct rtp_hdr *rtp_hdr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200716 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200717 LOGPENDP(endp, DRTP, LOGL_ERROR, "AMR RTP packet too short (%d < %zu)\n",
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200718 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200719 return -EINVAL;
720 }
721
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200722 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100723
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200724 if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr)) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100725 /* TS 101318 encoding => RFC 5993 encoding */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200726 msgb_put(msg, 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100727 memmove(rtp_hdr->data + 1, rtp_hdr->data, GSM_HR_BYTES);
728 rtp_hdr->data[0] = 0x00;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200729 } else if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr) + 1) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100730 /* RFC 5993 encoding => TS 101318 encoding */
731 memmove(rtp_hdr->data, rtp_hdr->data + 1, GSM_HR_BYTES);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200732 msgb_trim(msg, msgb_length(msg) - 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100733 } else {
734 /* It is possible that multiple payloads occur in one RTP
735 * packet. This is not supported yet. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200736 LOGPENDP(endp, DRTP, LOGL_ERROR,
737 "cannot figure out how to convert RTP packet\n");
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200738 return -ENOTSUP;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100739 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200740 return 0;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100741}
742
Philipp Maier228e5912019-03-05 13:56:59 +0100743/* For AMR RTP two framing modes are defined RFC3267. There is a bandwith
744 * efficient encoding scheme where all fields are packed together one after
745 * another and an octet aligned mode where all fields are aligned to octet
746 * boundaries. This function is used to convert between the two modes */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200747static int amr_oa_bwe_convert(struct mgcp_endpoint *endp, struct msgb *msg,
Philipp Maier228e5912019-03-05 13:56:59 +0100748 bool target_is_oa)
749{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200750 /* NOTE: the msgb has an allocated length of RTP_BUF_SIZE, so there is
Philipp Maier228e5912019-03-05 13:56:59 +0100751 * plenty of space available to store the slightly larger, converted
752 * data */
Philipp Maier228e5912019-03-05 13:56:59 +0100753 struct rtp_hdr *rtp_hdr;
754 unsigned int payload_len;
755 int rc;
756
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200757 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
758 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 +0200759 return -EINVAL;
760 }
761
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200762 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier228e5912019-03-05 13:56:59 +0100763
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200764 payload_len = msgb_length(msg) - sizeof(struct rtp_hdr);
Philipp Maier228e5912019-03-05 13:56:59 +0100765
766 if (osmo_amr_is_oa(rtp_hdr->data, payload_len)) {
767 if (!target_is_oa)
768 /* Input data is oa an target format is bwe
769 * ==> convert */
770 rc = osmo_amr_oa_to_bwe(rtp_hdr->data, payload_len);
771 else
772 /* Input data is already bew, but we accept it anyway
773 * ==> no conversion needed */
774 rc = payload_len;
775 } else {
776 if (target_is_oa)
777 /* Input data is bwe an target format is oa
778 * ==> convert */
779 rc = osmo_amr_bwe_to_oa(rtp_hdr->data, payload_len,
780 RTP_BUF_SIZE);
781 else
782 /* Input data is already oa, but we accept it anyway
783 * ==> no conversion needed */
784 rc = payload_len;
785 }
786 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200787 LOGPENDP(endp, DRTP, LOGL_ERROR,
788 "AMR RTP packet conversion failed\n");
Philipp Maier228e5912019-03-05 13:56:59 +0100789 return -EINVAL;
790 }
791
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200792 return msgb_trim(msg, rc + sizeof(struct rtp_hdr));
Philipp Maier228e5912019-03-05 13:56:59 +0100793}
794
795/* Check if a conversion between octet-aligned and bandwith-efficient mode is
796 * indicated. */
797static bool amr_oa_bwe_convert_indicated(struct mgcp_rtp_codec *codec)
798{
799 if (codec->param_present == false)
800 return false;
801 if (!codec->param.amr_octet_aligned_present)
802 return false;
803 if (strcmp(codec->subtype_name, "AMR") != 0)
804 return false;
805 return true;
806}
807
808
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200809/* Return whether an RTP packet with AMR payload is in octet-aligned mode.
810 * Return 0 if in bandwidth-efficient mode, 1 for octet-aligned mode, and negative if the RTP data is invalid. */
811static int amr_oa_check(char *data, int len)
Philipp Maier228e5912019-03-05 13:56:59 +0100812{
813 struct rtp_hdr *rtp_hdr;
814 unsigned int payload_len;
815
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200816 if (len < sizeof(struct rtp_hdr))
817 return -EINVAL;
818
Philipp Maier228e5912019-03-05 13:56:59 +0100819 rtp_hdr = (struct rtp_hdr *)data;
820
821 payload_len = len - sizeof(struct rtp_hdr);
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200822 if (payload_len < sizeof(struct amr_hdr))
823 return -EINVAL;
Philipp Maier228e5912019-03-05 13:56:59 +0100824
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200825 return osmo_amr_is_oa(rtp_hdr->data, payload_len) ? 1 : 0;
Philipp Maier228e5912019-03-05 13:56:59 +0100826}
827
Philipp Maier87bd9be2017-08-22 16:35:41 +0200828/* Forward data to a debug tap. This is debug function that is intended for
829 * debugging the voice traffic with tools like gstreamer */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200830static void forward_data(int fd, struct mgcp_rtp_tap *tap, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200831{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100832 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200833
Philipp Maiere6f172d2017-11-07 12:00:01 +0100834 if (!tap->enabled)
835 return;
836
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200837 rc = sendto(fd, msgb_data(msg), msgb_length(msg), 0, (struct sockaddr *)&tap->forward,
Philipp Maiere6f172d2017-11-07 12:00:01 +0100838 sizeof(tap->forward));
839
840 if (rc < 0)
841 LOGP(DRTP, LOGL_ERROR,
842 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200843}
844
Philipp Maier889fe7f2020-07-06 17:44:12 +0200845/* Generate an RTP header if it is missing */
846static void gen_rtp_header(struct msgb *msg, struct mgcp_rtp_end *rtp_end,
847 struct mgcp_rtp_state *state)
848{
849 struct rtp_hdr *hdr = (struct rtp_hdr *)msgb_data(msg);
850
851 if (hdr->version > 0)
852 return;
853
854 hdr->version = 2;
855 hdr->payload_type = rtp_end->codec->payload_type;
856 hdr->timestamp = osmo_htonl(get_current_ts(rtp_end->codec->rate));
857 hdr->sequence = osmo_htons(state->alt_rtp_tx_sequence);
858 hdr->ssrc = state->alt_rtp_tx_ssrc;
859}
860
861
Philipp Maier87bd9be2017-08-22 16:35:41 +0200862/*! Send RTP/RTCP data to a specified destination connection.
Philipp Maier0b79d212020-06-18 12:02:49 +0200863 * \param[in] endp associated endpoint (for configuration, logging).
864 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP.
865 * \param[in] spoofed source address (set to NULL to disable).
866 * \param[in] buf buffer that contains the RTP/RTCP data.
867 * \param[in] len length of the buffer that contains the RTP/RTCP data.
868 * \param[in] conn_src associated source connection.
869 * \param[in] conn_dst associated destination connection.
870 * \returns 0 on success, -1 on ERROR. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200871int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct osmo_sockaddr *addr,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200872 struct msgb *msg, struct mgcp_conn_rtp *conn_src,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200873 struct mgcp_conn_rtp *conn_dst)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200874{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200875 /*! When no destination connection is available (e.g. when only one
876 * connection in loopback mode exists), then the source connection
877 * shall be specified as destination connection */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200878
Philipp Maier14b27a82020-06-02 20:15:30 +0200879 struct mgcp_trunk *trunk = endp->trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200880 struct mgcp_rtp_end *rtp_end;
881 struct mgcp_rtp_state *rtp_state;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200882 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200883 char *dest_name;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200884 int rc;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200885 int len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200886
Philipp Maier87bd9be2017-08-22 16:35:41 +0200887 OSMO_ASSERT(conn_src);
888 OSMO_ASSERT(conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200889
Philipp Maier87bd9be2017-08-22 16:35:41 +0200890 if (is_rtp) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200891 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTP packet...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200892 } else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200893 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTCP packet...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200894 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200895
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200896 LOGPENDP(endp, DRTP, LOGL_DEBUG, "loop:%d, mode:%d%s\n",
Philipp Maier14b27a82020-06-02 20:15:30 +0200897 trunk->audio_loop, conn_src->conn->mode,
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200898 conn_src->conn->mode == MGCP_CONN_LOOPBACK ? " (loopback)" : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200899
Philipp Maier6931f9a2018-07-26 09:29:31 +0200900 /* FIXME: It is legal that the payload type on the egress connection is
901 * different from the payload type that has been negotiated on the
902 * ingress connection. Essentially the codecs are the same so we can
903 * match them and patch the payload type. However, if we can not find
904 * the codec pendant (everything ist equal except the PT), we are of
905 * course unable to patch the payload type. A situation like this
906 * should not occur if transcoding is consequently avoided. Until
907 * we have transcoding support in osmo-mgw we can not resolve this. */
Philipp Maierda895b12018-08-03 12:16:37 +0200908 if (is_rtp) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200909 rc = mgcp_patch_pt(conn_src, conn_dst, msg);
Philipp Maierda895b12018-08-03 12:16:37 +0200910 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200911 LOGPENDP(endp, DRTP, LOGL_DEBUG,
912 "can not patch PT because no suitable egress codec was found.\n");
Philipp Maierda895b12018-08-03 12:16:37 +0200913 }
Philipp Maier6931f9a2018-07-26 09:29:31 +0200914 }
915
Philipp Maier87bd9be2017-08-22 16:35:41 +0200916 /* Note: In case of loopback configuration, both, the source and the
917 * destination will point to the same connection. */
918 rtp_end = &conn_dst->end;
919 rtp_state = &conn_src->state;
920 dest_name = conn_dst->conn->name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200921
Philipp Maier889fe7f2020-07-06 17:44:12 +0200922 /* Ensure we have an alternative SSRC in case we need it, see also
923 * gen_rtp_header() */
924 if (rtp_state->alt_rtp_tx_ssrc == 0)
925 rtp_state->alt_rtp_tx_ssrc = rand();
926
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200927 if (!rtp_end->output_enabled) {
Harald Weltea48ff4a2020-03-08 14:45:08 +0100928 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_DROPPED_PACKETS_CTR);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200929 LOGPENDP(endp, DRTP, LOGL_DEBUG,
930 "output disabled, drop to %s %s "
931 "rtp_port:%u rtcp_port:%u\n",
932 dest_name,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200933 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200934 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200935 );
936 } else if (is_rtp) {
937 int cont;
938 int nbytes = 0;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200939 int buflen = msgb_length(msg);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200940
941 /* Make sure we have a valid RTP header, in cases where no RTP
942 * header is present, we will generate one. */
943 gen_rtp_header(msg, rtp_end, rtp_state);
944
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200945 do {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200946 /* Run transcoder */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200947 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200948 (char*)msgb_data(msg), &buflen,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200949 RTP_BUF_SIZE);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200950 if (cont < 0)
951 break;
952
Philipp Maier87bd9be2017-08-22 16:35:41 +0200953 if (addr)
954 mgcp_patch_and_count(endp, rtp_state, rtp_end,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200955 addr, msg);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100956
Philipp Maier228e5912019-03-05 13:56:59 +0100957 if (amr_oa_bwe_convert_indicated(conn_dst->end.codec)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200958 rc = amr_oa_bwe_convert(endp, msg,
Neels Hofmeyr5a6220f2019-08-20 03:09:04 +0200959 conn_dst->end.codec->param.amr_octet_aligned);
960 if (rc < 0) {
961 LOGPENDP(endp, DRTP, LOGL_ERROR,
962 "Error in AMR octet-aligned <-> bandwidth-efficient mode conversion\n");
963 break;
964 }
Philipp Maier228e5912019-03-05 13:56:59 +0100965 }
966 else if (rtp_end->rfc5993_hr_convert
Philipp Maier9fc8a022019-02-20 12:26:52 +0100967 && strcmp(conn_src->end.codec->subtype_name,
Neels Hofmeyr5a6220f2019-08-20 03:09:04 +0200968 "GSM-HR-08") == 0) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200969 rc = rfc5993_hr_convert(endp, msg);
Neels Hofmeyr5a6220f2019-08-20 03:09:04 +0200970 if (rc < 0) {
971 LOGPENDP(endp, DRTP, LOGL_ERROR, "Error while converting to GSM-HR-08\n");
972 break;
973 }
974 }
Philipp Maier9fc8a022019-02-20 12:26:52 +0100975
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200976 LOGPENDP(endp, DRTP, LOGL_DEBUG,
977 "process/send to %s %s "
978 "rtp_port:%u rtcp_port:%u\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200979 dest_name,
980 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200981 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
982 );
Philipp Maier87bd9be2017-08-22 16:35:41 +0200983
984 /* Forward a copy of the RTP data to a debug ip/port */
985 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200986 msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200987
988 /* FIXME: HACK HACK HACK. See OS#2459.
989 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
990 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
991 * cells (as long as we patch only the first RTP payload in each stream).
992 */
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200993 if (!rtp_state->patched_first_rtp_payload
994 && conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200995 uint8_t *data = msgb_data(msg) + 12;
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200996 if (data[0] == 0xe0) {
997 data[0] = 0xe4;
998 data[1] = 0x00;
999 rtp_state->patched_first_rtp_payload = true;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001000 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1001 "Patching over first two bytes"
1002 " to fake an IuUP Initialization Ack\n");
Neels Hofmeyr35a38292018-07-23 18:29:04 +02001003 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001004 }
1005
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001006 len = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr, rtp_end->rtp_port,
1007 (char*)msgb_data(msg), msgb_length(msg));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001008
Philipp Maier87bd9be2017-08-22 16:35:41 +02001009 if (len <= 0)
1010 return len;
1011
Harald Weltea48ff4a2020-03-08 14:45:08 +01001012 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
1013 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
Philipp Maier889fe7f2020-07-06 17:44:12 +02001014 rtp_state->alt_rtp_tx_sequence++;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001015
1016 nbytes += len;
1017 buflen = cont;
1018 } while (buflen > 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001019 return nbytes;
Philipp Maier14b27a82020-06-02 20:15:30 +02001020 } else if (!trunk->omit_rtcp) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001021 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1022 "send to %s %s rtp_port:%u rtcp_port:%u\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001023 dest_name, osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001024 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
1025 );
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001026
Philipp Maier87bd9be2017-08-22 16:35:41 +02001027 len = mgcp_udp_send(rtp_end->rtcp.fd,
1028 &rtp_end->addr,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001029 rtp_end->rtcp_port, (char*)msgb_data(msg), msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001030
Harald Weltea48ff4a2020-03-08 14:45:08 +01001031 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
1032 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
Philipp Maier889fe7f2020-07-06 17:44:12 +02001033 rtp_state->alt_rtp_tx_sequence++;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001034
1035 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001036 }
1037
1038 return 0;
1039}
1040
Philipp Maier87bd9be2017-08-22 16:35:41 +02001041/* Check if the origin (addr) matches the address/port data of the RTP
1042 * connections. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001043static int check_rtp_origin(struct mgcp_conn_rtp *conn, struct osmo_sockaddr *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001044{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001045 char ipbuf[INET6_ADDRSTRLEN];
1046
1047 if (addr_is_any(&conn->end.addr)) {
Neels Hofmeyrefd645e2018-09-10 13:14:50 +02001048 switch (conn->conn->mode) {
1049 case MGCP_CONN_LOOPBACK:
1050 /* HACK: for IuUP, we want to reply with an IuUP Initialization ACK upon the first RTP
1051 * message received. We currently hackishly accomplish that by putting the endpoint in
1052 * loopback mode and patching over the looped back RTP message to make it look like an
1053 * ack. We don't know the femto cell's IP address and port until the RAB Assignment
1054 * Response is received, but the nano3G expects an IuUP Initialization Ack before it even
1055 * sends the RAB Assignment Response. Hence, if the remote address is 0.0.0.0 and the
1056 * MGCP port is in loopback mode, allow looping back the packet to any source. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001057 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1058 "In loopback mode and remote address not set:"
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001059 " allowing data from address: %s\n",
1060 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +02001061 return 0;
1062
1063 default:
1064 /* Receiving early media before the endpoint is configured. Instead of logging
1065 * this as an error that occurs on every call, keep it more low profile to not
1066 * confuse humans with expected errors. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001067 LOGPCONN(conn->conn, DRTP, LOGL_INFO,
1068 "Rx RTP from %s, but remote address not set:"
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001069 " dropping early media\n",
1070 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +02001071 return -1;
1072 }
Neels Hofmeyr0063ca22018-07-23 18:12:16 +02001073 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001074
Philipp Maier87bd9be2017-08-22 16:35:41 +02001075 /* Note: Check if the inbound RTP data comes from the same host to
1076 * which we send our outgoing RTP traffic. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001077 if (conn->end.addr.u.sa.sa_family != addr->u.sa.sa_family ||
1078 (conn->end.addr.u.sa.sa_family == AF_INET &&
1079 conn->end.addr.u.sin.sin_addr.s_addr != addr->u.sin.sin_addr.s_addr) ||
1080 (conn->end.addr.u.sa.sa_family == AF_INET6 &&
1081 memcmp(&conn->end.addr.u.sin6.sin6_addr, &addr->u.sin6.sin6_addr,
1082 sizeof(struct in6_addr)))) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001083 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001084 "data from wrong address: %s, ",
1085 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Philipp Maierc3413882017-10-27 12:26:54 +02001086 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001087 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001088 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001089 return -1;
1090 }
1091
Philipp Maier87bd9be2017-08-22 16:35:41 +02001092 /* Note: Usually the remote remote port of the data we receive will be
1093 * the same as the remote port where we transmit outgoing RTP traffic
1094 * to (set by MDCX). We use this to check the origin of the data for
1095 * plausibility. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001096 if (ntohs(conn->end.rtp_port) != osmo_sockaddr_port(&addr->u.sa) &&
1097 ntohs(conn->end.rtcp_port) != osmo_sockaddr_port(&addr->u.sa)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001098 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001099 "data from wrong source port: %d, ",
1100 osmo_sockaddr_port(&addr->u.sa));
Philipp Maierc3413882017-10-27 12:26:54 +02001101 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001102 "expected: %d for RTP or %d for RTCP\n",
1103 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001104 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001105 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001106 }
1107
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001108 return 0;
1109}
1110
Philipp Maier87bd9be2017-08-22 16:35:41 +02001111/* Check the if the destination address configuration of an RTP connection
1112 * makes sense */
1113static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001114{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001115 char ipbuf[INET6_ADDRSTRLEN];
1116 bool ip_is_any = addr_is_any(&conn->end.addr);
1117
Philipp Maiere6df0e42018-05-29 14:03:06 +02001118 /* Note: it is legal to create a connection but never setting a port
1119 * and IP-address for outgoing data. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001120 if (ip_is_any && conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001121 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
1122 "destination IP-address and rtp port is (not yet) known (%s:%u)\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001123 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port);
Philipp Maiere6df0e42018-05-29 14:03:06 +02001124 return -1;
1125 }
1126
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001127 if (ip_is_any) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001128 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1129 "destination IP-address is invalid (%s:%u)\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001130 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001131 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001132 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001133
1134 if (conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001135 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1136 "destination rtp port is invalid (%s:%u)\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001137 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001138 return -1;
1139 }
1140
1141 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001142}
1143
Philipp Maier4dba7692018-08-03 12:20:52 +02001144/* Do some basic checks to make sure that the RTCP packets we are going to
1145 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001146static int check_rtcp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +02001147{
1148 struct rtcp_hdr *hdr;
1149 unsigned int len;
1150 uint8_t type;
1151
1152 /* RTPC packets that are just a header without data do not make
1153 * any sense. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001154 if (msgb_length(msg) < sizeof(struct rtcp_hdr)) {
1155 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP packet too short (%u < %zu)\n",
1156 msgb_length(msg), sizeof(struct rtcp_hdr));
Philipp Maier4dba7692018-08-03 12:20:52 +02001157 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001158 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001159
1160 /* Make sure that the length of the received packet does not exceed
1161 * the available buffer size */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001162 hdr = (struct rtcp_hdr *)msgb_data(msg);
Philipp Maier4dba7692018-08-03 12:20:52 +02001163 len = (osmo_ntohs(hdr->length) + 1) * 4;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001164 if (len > msgb_length(msg)) {
1165 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header length exceeds packet size (%u > %u)\n",
1166 len, msgb_length(msg));
Philipp Maier4dba7692018-08-03 12:20:52 +02001167 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001168 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001169
1170 /* Make sure we accept only packets that have a proper packet type set
1171 * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
1172 type = hdr->type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001173 if ((type < 192 || type > 195) && (type < 200 || type > 213)) {
1174 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header: invalid type: %u\n", type);
Philipp Maier4dba7692018-08-03 12:20:52 +02001175 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001176 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001177
1178 return 0;
1179}
1180
1181/* Do some basic checks to make sure that the RTP packets we are going to
1182 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001183static int check_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +02001184{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001185 size_t min_size = sizeof(struct rtp_hdr);
1186 if (msgb_length(msg) < min_size) {
1187 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
1188 msgb_length(msg), min_size);
1189 return -1;
1190 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001191
1192 /* FIXME: Add more checks, the reason why we do not check more than
1193 * the length is because we currently handle IUUP packets as RTP
1194 * packets, so they must pass this check, if we weould be more
1195 * strict here, we would possibly break 3G. (see also FIXME note
1196 * below */
1197
1198 return 0;
1199}
1200
Philipp Maier87bd9be2017-08-22 16:35:41 +02001201/* Send RTP data. Possible options are standard RTP packet
1202 * transmission or trsmission via an osmux connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001203static int mgcp_send_rtp(struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001204{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001205 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1206 enum rtp_proto proto = mc->proto;
1207 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001208 struct mgcp_endpoint *endp = conn_src->conn->endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001209
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001210 LOGPENDP(endp, DRTP, LOGL_DEBUG, "destin conn:%s\n",
1211 mgcp_conn_dump(conn_dst->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001212
1213 /* Before we try to deliver the packet, we check if the destination
1214 * port and IP-Address make sense at all. If not, we will be unable
1215 * to deliver the packet. */
1216 if (check_rtp_destin(conn_dst) != 0)
1217 return -1;
1218
1219 /* Depending on the RTP connection type, deliver the RTP packet to the
1220 * destination connection. */
1221 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001222 case MGCP_RTP_DEFAULT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001223 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1224 "endpoint type is MGCP_RTP_DEFAULT, "
1225 "using mgcp_send() to forward data directly\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001226 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001227 mc->from_addr, msg, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001228 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001229 case MGCP_OSMUX_BSC:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001230 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1231 "endpoint type is MGCP_OSMUX_BSC_NAT, "
1232 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n");
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001233 return osmux_xfrm_to_osmux((char*)msgb_data(msg), msgb_length(msg), conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001234 }
1235
Philipp Maier87bd9be2017-08-22 16:35:41 +02001236 /* If the data has not been handled/forwarded until here, it will
1237 * be discarded, this should not happen, normally the MGCP type
1238 * should be properly set */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001239 LOGPENDP(endp, DRTP, LOGL_ERROR, "bad MGCP type -- data discarded!\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001240
1241 return -1;
1242}
1243
1244/*! dispatch incoming RTP packet to opposite RTP connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001245 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1246 * \param[in] addr socket address where the RTP packet has been received from.
1247 * \param[in] buf buffer that hold the RTP payload.
1248 * \param[in] buf_size size data length of buf.
1249 * \param[in] conn originating connection.
1250 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001251int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001252{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001253 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1254 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1255 struct mgcp_conn *conn = conn_src->conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001256 struct mgcp_conn *conn_dst;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001257 struct osmo_sockaddr *from_addr = mc->from_addr;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001258
1259 /*! NOTE: This callback function implements the endpoint specific
Alexander Chemeris61cf9bb2020-05-11 18:13:53 +03001260 * dispatch behaviour of an rtp bridge/proxy endpoint. It is assumed
Philipp Maier87bd9be2017-08-22 16:35:41 +02001261 * that the endpoint will hold only two connections. This premise
1262 * is used to determine the opposite connection (it is always the
1263 * connection that is not the originating connection). Once the
1264 * destination connection is known the RTP packet is sent via
1265 * the destination connection. */
1266
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001267
1268 /* Check if the connection is in loopback mode, if yes, just send the
1269 * incoming data back to the origin */
1270 if (conn->mode == MGCP_CONN_LOOPBACK) {
1271 /* When we are in loopback mode, we loop back all incoming
1272 * packets back to their origin. We will use the originating
1273 * address data from the UDP packet header to patch the
1274 * outgoing address in connection on the fly */
1275 if (conn->u.rtp.end.rtp_port == 0) {
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001276 OSMO_ASSERT(conn->u.rtp.end.addr.u.sa.sa_family == from_addr->u.sa.sa_family);
1277 switch (from_addr->u.sa.sa_family) {
1278 case AF_INET:
1279 conn->u.rtp.end.addr.u.sin.sin_addr = from_addr->u.sin.sin_addr;
1280 conn->u.rtp.end.rtp_port = from_addr->u.sin.sin_port;
1281 break;
1282 case AF_INET6:
1283 conn->u.rtp.end.addr.u.sin6.sin6_addr = from_addr->u.sin6.sin6_addr;
1284 conn->u.rtp.end.rtp_port = from_addr->u.sin6.sin6_port;
1285 break;
1286 default:
1287 OSMO_ASSERT(false);
1288 }
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001289 }
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001290 return mgcp_send_rtp(conn_src, msg);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001291 }
1292
Philipp Maier87bd9be2017-08-22 16:35:41 +02001293 /* Find a destination connection. */
1294 /* NOTE: This code path runs every time an RTP packet is received. The
1295 * function mgcp_find_dst_conn() we use to determine the detination
1296 * connection will iterate the connection list inside the endpoint.
1297 * Since list iterations are quite costly, we will figure out the
1298 * destination only once and use the optional private data pointer of
1299 * the connection to cache the destination connection pointer. */
1300 if (!conn->priv) {
1301 conn_dst = mgcp_find_dst_conn(conn);
1302 conn->priv = conn_dst;
1303 } else {
1304 conn_dst = (struct mgcp_conn *)conn->priv;
1305 }
1306
1307 /* There is no destination conn, stop here */
1308 if (!conn_dst) {
Alexander Chemerisebb9bf32020-05-11 18:14:31 +03001309 LOGPCONN(conn, DRTP, LOGL_DEBUG,
1310 "no connection to forward an incoming RTP packet to\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001311 return -1;
1312 }
1313
1314 /* The destination conn is not an RTP connection */
1315 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001316 LOGPCONN(conn, DRTP, LOGL_ERROR,
1317 "unable to find suitable destination conn\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001318 return -1;
1319 }
1320
1321 /* Dispatch RTP packet to destination RTP connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001322 return mgcp_send_rtp(&conn_dst->u.rtp, msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001323}
1324
Philipp Maier0996a1e2020-06-10 15:27:14 +02001325/*! dispatch incoming RTP packet to E1 subslot, handle RTCP packets locally.
1326 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1327 * \param[in] addr socket address where the RTP packet has been received from.
1328 * \param[in] buf buffer that hold the RTP payload.
1329 * \param[in] buf_size size data length of buf.
1330 * \param[in] conn originating connection.
1331 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001332int mgcp_dispatch_e1_bridge_cb(struct msgb *msg)
Philipp Maier0996a1e2020-06-10 15:27:14 +02001333{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001334 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1335 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1336 struct mgcp_conn *conn = conn_src->conn;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001337 struct osmo_sockaddr *from_addr = mc->from_addr;
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) {
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001347 OSMO_ASSERT(conn->u.rtp.end.addr.u.sa.sa_family == from_addr->u.sa.sa_family);
1348 switch (from_addr->u.sa.sa_family) {
1349 case AF_INET:
1350 conn->u.rtp.end.addr.u.sin.sin_addr = from_addr->u.sin.sin_addr;
1351 conn->u.rtp.end.rtp_port = from_addr->u.sin.sin_port;
1352 break;
1353 case AF_INET6:
1354 conn->u.rtp.end.addr.u.sin6.sin6_addr = from_addr->u.sin6.sin6_addr;
1355 conn->u.rtp.end.rtp_port = from_addr->u.sin6.sin6_port;
1356 break;
1357 default:
1358 OSMO_ASSERT(false);
1359 }
Philipp Maier889fe7f2020-07-06 17:44:12 +02001360 }
1361 return mgcp_send_rtp(conn_src, msg);
1362 }
1363
1364 /* Forward to E1 */
1365 return mgcp_e1_send_rtp(conn->endp, conn->u.rtp.end.codec, msg);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001366}
1367
Philipp Maierdf5d2192018-01-24 11:39:32 +01001368/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1369 * \param[in] endp Endpoint on which the connection resides.
Philipp Maier08eb9352020-06-18 11:55:35 +02001370 * \param[in] conn Connection that is about to be removed (ignored). */
Philipp Maierdf5d2192018-01-24 11:39:32 +01001371void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1372{
1373 struct mgcp_conn *conn_cleanup;
1374
1375 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1376 * pointer to the destination connection, so that we do not have
1377 * to go through the list every time an RTP packet arrives. To prevent
1378 * a use-after-free situation we invalidate this information for all
1379 * connections present when one connection is removed from the
1380 * endpoint. */
1381 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
Pau Espin Pedrol6d0a59a2020-09-08 16:50:22 +02001382 if (conn_cleanup->priv == conn)
1383 conn_cleanup->priv = NULL;
Philipp Maierdf5d2192018-01-24 11:39:32 +01001384 }
1385}
1386
Philipp Maier0996a1e2020-06-10 15:27:14 +02001387/*! cleanup an endpoint when a connection on an E1 endpoint is removed.
1388 * \param[in] endp Endpoint on which the connection resides.
1389 * \param[in] conn Connection that is about to be removed (ignored). */
1390void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1391{
Philipp Maier889fe7f2020-07-06 17:44:12 +02001392 /* Cleanup tasks for E1 are the same as for regular endpoint. The
1393 * shut down of the E1 part is handled separately. */
1394 mgcp_cleanup_rtp_bridge_cb(endp, conn);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001395}
1396
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001397static bool is_dummy_msg(enum rtp_proto proto, struct msgb *msg)
1398{
1399 return msgb_length(msg) == 1 && msgb_data(msg)[0] == MGCP_DUMMY_LOAD;
1400}
1401
Philipp Maier87bd9be2017-08-22 16:35:41 +02001402/* Handle incoming RTP data from NET */
1403static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1404{
1405 /* NOTE: This is a generic implementation. RTP data is received. In
1406 * case of loopback the data is just sent back to its origin. All
1407 * other cases implement endpoint specific behaviour (e.g. how is the
1408 * destination connection determined?). That specific behaviour is
1409 * implemented by the callback function that is called at the end of
1410 * the function */
1411
1412 struct mgcp_conn_rtp *conn_src;
1413 struct mgcp_endpoint *endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001414 struct osmo_sockaddr addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001415 socklen_t slen = sizeof(addr);
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001416 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001417 int ret;
1418 enum rtp_proto proto;
1419 struct osmo_rtp_msg_ctx *mc;
1420 struct msgb *msg = msgb_alloc(RTP_BUF_SIZE, "RTP-rx");
1421 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001422
1423 conn_src = (struct mgcp_conn_rtp *)fd->data;
1424 OSMO_ASSERT(conn_src);
1425 endp = conn_src->conn->endp;
1426 OSMO_ASSERT(endp);
1427
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001428 proto = (fd == &conn_src->end.rtp)? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001429
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001430 ret = recvfrom(fd->fd, msgb_data(msg), msg->data_len, 0, (struct sockaddr *)&addr.u.sa, &slen);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001431
1432 if (ret <= 0) {
1433 LOG_CONN_RTP(conn_src, LOGL_ERROR, "recvfrom error: %s\n", strerror(errno));
1434 rc = -1;
1435 goto out;
1436 }
1437
1438 msgb_put(msg, ret);
1439
1440 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "%s: rx %u bytes from %s:%u\n",
1441 proto == MGCP_PROTO_RTP ? "RTP" : "RTPC",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001442 msgb_length(msg), osmo_sockaddr_ntop(&addr.u.sa, ipbuf),
1443 osmo_sockaddr_port(&addr.u.sa));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001444
1445 if ((proto == MGCP_PROTO_RTP && check_rtp(conn_src, msg))
1446 || (proto == MGCP_PROTO_RTCP && check_rtcp(conn_src, msg))) {
1447 /* Logging happened in the two check_ functions */
1448 rc = -1;
1449 goto out;
1450 }
1451
1452 if (is_dummy_msg(proto, msg)) {
1453 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx dummy packet (dropped)\n");
1454 rc = 0;
1455 goto out;
1456 }
1457
1458 /* Since the msgb remains owned and freed by this function, the msg ctx data struct can just be on the stack and
1459 * needs not be allocated with the msgb. */
1460 mc = OSMO_RTP_MSG_CTX(msg);
1461 *mc = (struct osmo_rtp_msg_ctx){
1462 .proto = proto,
1463 .conn_src = conn_src,
1464 .from_addr = &addr,
1465 };
1466 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "msg ctx: %d %p %s\n",
1467 mc->proto, mc->conn_src,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001468 osmo_hexdump((void*)mc->from_addr,
1469 mc->from_addr->u.sa.sa_family == AF_INET6 ?
1470 sizeof(struct sockaddr_in6) :
1471 sizeof(struct sockaddr_in)));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001472
1473 /* Increment RX statistics */
1474 rate_ctr_inc(&conn_src->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR]);
1475 rate_ctr_add(&conn_src->rate_ctr_group->ctr[RTP_OCTETS_RX_CTR], msgb_length(msg));
1476 /* FIXME: count RTP and RTCP separately, also count IuUP payload-less separately */
1477
1478 /* Forward a copy of the RTP data to a debug ip/port */
1479 forward_data(fd->fd, &conn_src->tap_in, msg);
1480
1481 rc = rx_rtp(msg);
1482
1483out:
1484 msgb_free(msg);
1485 return rc;
1486}
1487
1488static int rx_rtp(struct msgb *msg)
1489{
1490 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1491 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001492 struct osmo_sockaddr *from_addr = mc->from_addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001493 struct mgcp_conn *conn = conn_src->conn;
1494 struct mgcp_trunk *trunk = conn->endp->trunk;
1495
1496 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx_rtp(%u bytes)\n", msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001497
Oliver Smithe36b7752019-01-22 16:31:36 +01001498 mgcp_conn_watchdog_kick(conn_src->conn);
1499
Philipp Maier228e5912019-03-05 13:56:59 +01001500 /* If AMR is configured for the ingress connection a conversion of the
1501 * framing mode (octet-aligned vs. bandwith-efficient is explicitly
1502 * define, then we check if the incoming payload matches that
1503 * expectation. */
1504 if (amr_oa_bwe_convert_indicated(conn_src->end.codec)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001505 int oa = amr_oa_check((char*)msgb_data(msg), msgb_length(msg));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +02001506 if (oa < 0)
1507 return -1;
1508 if (((bool)oa) != conn_src->end.codec->param.amr_octet_aligned)
Philipp Maier228e5912019-03-05 13:56:59 +01001509 return -1;
1510 }
1511
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001512 /* Check if the origin of the RTP packet seems plausible */
1513 if (!trunk->rtp_accept_all && check_rtp_origin(conn_src, from_addr))
1514 return -1;
1515
Philipp Maier87bd9be2017-08-22 16:35:41 +02001516 /* Execute endpoint specific implementation that handles the
1517 * dispatching of the RTP data */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001518 return conn->endp->type->dispatch_rtp_cb(msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001519}
1520
1521/*! set IP Type of Service parameter.
Philipp Maier0b79d212020-06-18 12:02:49 +02001522 * \param[in] fd associated file descriptor.
1523 * \param[in] tos dscp value.
1524 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001525int mgcp_set_ip_tos(int fd, int tos)
1526{
1527 int ret;
1528 ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1529
1530 if (ret < 0)
1531 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001532 return 0;
1533}
1534
Philipp Maier87bd9be2017-08-22 16:35:41 +02001535/*! bind RTP port to osmo_fd.
Philipp Maier0b79d212020-06-18 12:02:49 +02001536 * \param[in] source_addr source (local) address to bind on.
1537 * \param[in] fd associated file descriptor.
1538 * \param[in] port to bind on.
1539 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001540int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
1541{
Harald Welte8890dfa2017-11-17 15:09:30 +01001542 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001543
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001544 rc = osmo_sock_init2(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
Harald Welte8890dfa2017-11-17 15:09:30 +01001545 NULL, 0, OSMO_SOCK_F_BIND);
1546 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001547 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001548 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001549 return -1;
1550 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001551 fd->fd = rc;
1552 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001553
1554 return 0;
1555}
1556
Philipp Maier87bd9be2017-08-22 16:35:41 +02001557/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001558static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001559 struct mgcp_rtp_end *rtp_end, struct mgcp_endpoint *endp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001560{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001561 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1562 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1563
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001564 if (mgcp_create_bind(source_addr, &rtp_end->rtp,
1565 rtp_end->local_port) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001566 LOGPENDP(endp, DRTP, LOGL_ERROR,
1567 "failed to create RTP port: %s:%d\n",
1568 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001569 goto cleanup0;
1570 }
1571
1572 if (mgcp_create_bind(source_addr, &rtp_end->rtcp,
1573 rtp_end->local_port + 1) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001574 LOGPENDP(endp, DRTP, LOGL_ERROR,
1575 "failed to create RTCP port: %s:%d\n",
1576 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001577 goto cleanup1;
1578 }
1579
Philipp Maier87bd9be2017-08-22 16:35:41 +02001580 /* Set Type of Service (DSCP-Value) as configured via VTY */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001581 mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
1582 mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
1583
Pau Espin Pedrola7152e02020-05-09 19:15:50 +02001584 rtp_end->rtp.when = OSMO_FD_READ;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001585 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001586 LOGPENDP(endp, DRTP, LOGL_ERROR,
1587 "failed to register RTP port %d\n",
1588 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001589 goto cleanup2;
1590 }
1591
Pau Espin Pedrola7152e02020-05-09 19:15:50 +02001592 rtp_end->rtcp.when = OSMO_FD_READ;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001593 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001594 LOGPENDP(endp, DRTP, LOGL_ERROR,
1595 "failed to register RTCP port %d\n",
1596 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001597 goto cleanup3;
1598 }
1599
1600 return 0;
1601
1602cleanup3:
1603 osmo_fd_unregister(&rtp_end->rtp);
1604cleanup2:
1605 close(rtp_end->rtcp.fd);
1606 rtp_end->rtcp.fd = -1;
1607cleanup1:
1608 close(rtp_end->rtp.fd);
1609 rtp_end->rtp.fd = -1;
1610cleanup0:
1611 return -1;
1612}
1613
Philipp Maier87bd9be2017-08-22 16:35:41 +02001614/*! bind RTP port to endpoint/connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001615 * \param[in] endp endpoint that holds the RTP connection.
1616 * \param[in] rtp_port port number to bind on.
1617 * \param[in] conn associated RTP connection.
1618 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001619int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1620 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001621{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001622 char name[512];
1623 struct mgcp_rtp_end *end;
1624
Philipp Maier01d24a32017-11-21 17:26:09 +01001625 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001626 end = &conn->end;
1627
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001628 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001629 LOGPENDP(endp, DRTP, LOGL_ERROR, "%u was already bound on conn:%s\n",
1630 rtp_port, mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001631
1632 /* Double bindings should never occour! Since we always allocate
1633 * connections dynamically and free them when they are not
1634 * needed anymore, there must be no previous binding leftover.
1635 * Should there be a connection bound twice, we have a serious
1636 * problem and must exit immediately! */
1637 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001638 }
1639
1640 end->local_port = rtp_port;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001641 end->rtp.cb = rtp_data_net;
1642 end->rtp.data = conn;
1643 end->rtcp.data = conn;
1644 end->rtcp.cb = rtp_data_net;
1645
Pau Espin Pedrol71d42e72020-09-03 14:20:07 +02001646 return bind_rtp(endp->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) {
1654 close(end->rtp.fd);
1655 end->rtp.fd = -1;
1656 osmo_fd_unregister(&end->rtp);
1657 }
1658
1659 if (end->rtcp.fd != -1) {
1660 close(end->rtcp.fd);
1661 end->rtcp.fd = -1;
1662 osmo_fd_unregister(&end->rtcp);
1663 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001664}