blob: 1014bf26170e46f21ce4b8ec9cfcd05d3f44868a [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* The protocol implementation */
3
4/*
5 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2012 by On-Waves
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <string.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <errno.h>
28#include <time.h>
29#include <limits.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020030#include <arpa/inet.h>
31
32#include <osmocom/core/msgb.h>
33#include <osmocom/core/select.h>
Philipp Maier1cb1e382017-11-02 17:16:04 +010034#include <osmocom/core/socket.h>
Philipp Maier4dba7692018-08-03 12:20:52 +020035#include <osmocom/core/byteswap.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020036#include <osmocom/netif/rtp.h>
Philipp Maier228e5912019-03-05 13:56:59 +010037#include <osmocom/netif/amr.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020038#include <osmocom/mgcp/mgcp.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020039#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier993ea6b2020-08-04 18:26:50 +020040#include <osmocom/mgcp/mgcp_network.h>
41#include <osmocom/mgcp/mgcp_protocol.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020042#include <osmocom/mgcp/mgcp_stat.h>
43#include <osmocom/mgcp/osmux.h>
44#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010045#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maierc66ab2c2020-06-02 20:55:34 +020046#include <osmocom/mgcp/mgcp_trunk.h>
Philipp Maier6931f9a2018-07-26 09:29:31 +020047#include <osmocom/mgcp/mgcp_codec.h>
Philipp Maierc3413882017-10-27 12:26:54 +020048#include <osmocom/mgcp/debug.h>
Philipp Maier9fc8a022019-02-20 12:26:52 +010049#include <osmocom/codec/codec.h>
Philipp Maier889fe7f2020-07-06 17:44:12 +020050#include <osmocom/mgcp/mgcp_e1.h>
Philipp Maier6931f9a2018-07-26 09:29:31 +020051
Philipp Maier993ea6b2020-08-04 18:26:50 +020052
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020053#define RTP_SEQ_MOD (1 << 16)
54#define RTP_MAX_DROPOUT 3000
55#define RTP_MAX_MISORDER 100
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020056
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +020057enum rtp_proto {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020058 MGCP_PROTO_RTP,
59 MGCP_PROTO_RTCP,
60};
61
Harald Weltea48ff4a2020-03-08 14:45:08 +010062static void rtpconn_rate_ctr_add(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp,
63 int id, int inc)
64{
65 struct rate_ctr_group *conn_stats = conn_rtp->rate_ctr_group;
Philipp Maierc66ab2c2020-06-02 20:55:34 +020066 struct rate_ctr_group *mgw_stats = endp->trunk->ratectr.all_rtp_conn_stats;
Harald Weltea48ff4a2020-03-08 14:45:08 +010067
Philipp Maierc66ab2c2020-06-02 20:55:34 +020068 /* add to both the per-connection and the global stats */
Pau Espin Pedrol907744e2021-06-04 17:57:34 +020069 rate_ctr_add(rate_ctr_group_get_ctr(conn_stats, id), inc);
70 rate_ctr_add(rate_ctr_group_get_ctr(mgw_stats, id), inc);
Harald Weltea48ff4a2020-03-08 14:45:08 +010071}
72
73static void rtpconn_rate_ctr_inc(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp, int id)
74{
75 rtpconn_rate_ctr_add(conn_rtp, endp, id, 1);
76}
77
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +020078static int rx_rtp(struct msgb *msg);
79
Pau Espin Pedrol33347a42021-07-06 20:04:23 +020080static bool addr_is_any(const struct osmo_sockaddr *osa)
81{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +020082 if (osa->u.sa.sa_family == AF_INET6) {
83 struct in6_addr ip6_any = IN6ADDR_ANY_INIT;
84 return memcmp(&osa->u.sin6.sin6_addr,
85 &ip6_any, sizeof(ip6_any)) == 0;
86 } else {
87 return osa->u.sin.sin_addr.s_addr == 0;
88 }
89}
90
Philipp Maier1cb1e382017-11-02 17:16:04 +010091/*! Determine the local rtp bind IP-address.
Philipp Maier0b79d212020-06-18 12:02:49 +020092 * \param[out] addr caller provided memory to store the resulting IP-Address.
93 * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters.
Philipp Maier1cb1e382017-11-02 17:16:04 +010094 *
95 * The local bind IP-address is automatically selected by probing the
96 * IP-Address of the interface that is pointing towards the remote IP-Address,
97 * if no remote IP-Address is known yet, the statically configured
98 * IP-Addresses are used as fallback. */
99void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn)
100{
101
102 struct mgcp_endpoint *endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200103 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier1cb1e382017-11-02 17:16:04 +0100104 int rc;
105 endp = conn->conn->endp;
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200106 bool rem_addr_set = !addr_is_any(&conn->end.addr);
107 char *bind_addr;
Philipp Maier1cb1e382017-11-02 17:16:04 +0100108
109 /* Try probing the local IP-Address */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200110 if (endp->cfg->net_ports.bind_addr_probe && rem_addr_set) {
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200111 rc = osmo_sock_local_ip(addr, osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Philipp Maier1cb1e382017-11-02 17:16:04 +0100112 if (rc < 0)
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200113 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
114 "local interface auto detection failed, using configured addresses...\n");
Philipp Maier1cb1e382017-11-02 17:16:04 +0100115 else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200116 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
117 "selected local rtp bind ip %s by probing using remote ip %s\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200118 addr, osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Philipp Maier1cb1e382017-11-02 17:16:04 +0100119 return;
120 }
121 }
122
Pau Espin Pedrola93c6e92019-05-06 15:23:57 +0200123 /* Select from preconfigured IP-Addresses. We don't have bind_addr for Osmux (yet?). */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200124 if (rem_addr_set) {
Philipp Maier1cb1e382017-11-02 17:16:04 +0100125 /* Check there is a bind IP for the RTP traffic configured,
126 * if so, use that IP-Address */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200127 bind_addr = conn->end.addr.u.sa.sa_family == AF_INET6 ?
128 endp->cfg->net_ports.bind_addr_v6 :
129 endp->cfg->net_ports.bind_addr_v4;
130 } else {
131 /* Choose any of the bind addresses, preferring v6 over v4 */
132 bind_addr = endp->cfg->net_ports.bind_addr_v6;
133 if (!bind_addr)
134 bind_addr = endp->cfg->net_ports.bind_addr_v4;
135 }
136 if (bind_addr) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200137 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
138 "using configured rtp bind ip as local bind ip %s\n",
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200139 bind_addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100140 } else {
141 /* No specific bind IP is configured for the RTP traffic, so
142 * assume the IP where we listen for incoming MGCP messages
143 * as bind IP */
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200144 bind_addr = endp->cfg->source_addr;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200145 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200146 "using mgcp bind ip as local rtp bind ip: %s\n", bind_addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100147 }
Pau Espin Pedrol8a2a1b22020-09-02 20:46:24 +0200148 osmo_strlcpy(addr, bind_addr, INET6_ADDRSTRLEN);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100149}
150
Philipp Maier87bd9be2017-08-22 16:35:41 +0200151/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200152 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +0200153 * 1/codec_rate seconds. */
154static uint32_t get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200155{
156 struct timespec tp;
157 uint64_t ret;
158
Philipp Maier87bd9be2017-08-22 16:35:41 +0200159 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200160 return 0;
161
162 memset(&tp, 0, sizeof(tp));
163 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maierc3413882017-10-27 12:26:54 +0200164 LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200165
166 /* convert it to 1/unit seconds */
167 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200168 ret *= codec_rate;
169 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200170
171 return ret;
172}
173
Philipp Maier87bd9be2017-08-22 16:35:41 +0200174/*! send udp packet.
Philipp Maier0b79d212020-06-18 12:02:49 +0200175 * \param[in] fd associated file descriptor.
176 * \param[in] addr destination ip-address.
177 * \param[in] port destination UDP port (network byte order).
178 * \param[in] buf buffer that holds the data to be send.
179 * \param[in] len length of the data to be sent.
180 * \returns bytes sent, -1 on error. */
Philipp Maier776846a2021-05-20 14:15:46 +0200181int mgcp_udp_send(int fd, struct osmo_sockaddr *addr, int port, const char *buf, int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200182{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200183 char ipbuf[INET6_ADDRSTRLEN];
184 size_t addr_len;
185 bool is_ipv6 = addr->u.sa.sa_family == AF_INET6;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200186
Philipp Maierc3413882017-10-27 12:26:54 +0200187 LOGP(DRTP, LOGL_DEBUG,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200188 "sending %i bytes length packet to %s:%u ...\n", len,
189 osmo_sockaddr_ntop(&addr->u.sa, ipbuf),
190 ntohs(port));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200191
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200192 if (is_ipv6) {
193 addr->u.sin6.sin6_port = port;
194 addr_len = sizeof(addr->u.sin6);
195 } else {
196 addr->u.sin.sin_port = port;
197 addr_len = sizeof(addr->u.sin);
198 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200199
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200200 return sendto(fd, buf, len, 0, &addr->u.sa, addr_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200201}
202
Philipp Maier87bd9be2017-08-22 16:35:41 +0200203/*! send RTP dummy packet (to keep NAT connection open).
Philipp Maier0b79d212020-06-18 12:02:49 +0200204 * \param[in] endp mcgp endpoint that holds the RTP connection.
205 * \param[in] conn associated RTP connection.
206 * \returns bytes sent, -1 on error. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200207int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200208{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200209 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,
Philipp Maierb3d14eb2021-05-20 14:18:52 +0200219 conn->end.rtp_port, rtp_dummy_payload, sizeof(rtp_dummy_payload));
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,
Philipp Maierb3d14eb2021-05-20 14:18:52 +0200229 conn->end.rtcp_port, rtp_dummy_payload, sizeof(rtp_dummy_payload));
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
Philipp Maier6931f9a2018-07-26 09:29:31 +0200896 /* FIXME: It is legal that the payload type on the egress connection is
897 * different from the payload type that has been negotiated on the
898 * ingress connection. Essentially the codecs are the same so we can
899 * match them and patch the payload type. However, if we can not find
900 * the codec pendant (everything ist equal except the PT), we are of
901 * course unable to patch the payload type. A situation like this
902 * should not occur if transcoding is consequently avoided. Until
903 * we have transcoding support in osmo-mgw we can not resolve this. */
Philipp Maierda895b12018-08-03 12:16:37 +0200904 if (is_rtp) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200905 rc = mgcp_patch_pt(conn_src, conn_dst, msg);
Philipp Maierda895b12018-08-03 12:16:37 +0200906 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200907 LOGPENDP(endp, DRTP, LOGL_DEBUG,
908 "can not patch PT because no suitable egress codec was found.\n");
Philipp Maierda895b12018-08-03 12:16:37 +0200909 }
Philipp Maier6931f9a2018-07-26 09:29:31 +0200910 }
911
Philipp Maier87bd9be2017-08-22 16:35:41 +0200912 /* Note: In case of loopback configuration, both, the source and the
913 * destination will point to the same connection. */
914 rtp_end = &conn_dst->end;
915 rtp_state = &conn_src->state;
916 dest_name = conn_dst->conn->name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200917
Philipp Maier889fe7f2020-07-06 17:44:12 +0200918 /* Ensure we have an alternative SSRC in case we need it, see also
919 * gen_rtp_header() */
920 if (rtp_state->alt_rtp_tx_ssrc == 0)
921 rtp_state->alt_rtp_tx_ssrc = rand();
922
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200923 if (!rtp_end->output_enabled) {
Harald Weltea48ff4a2020-03-08 14:45:08 +0100924 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_DROPPED_PACKETS_CTR);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200925 LOGPENDP(endp, DRTP, LOGL_DEBUG,
926 "output disabled, drop to %s %s "
927 "rtp_port:%u rtcp_port:%u\n",
928 dest_name,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200929 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200930 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200931 );
932 } else if (is_rtp) {
933 int cont;
934 int nbytes = 0;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200935 int buflen = msgb_length(msg);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200936
937 /* Make sure we have a valid RTP header, in cases where no RTP
938 * header is present, we will generate one. */
939 gen_rtp_header(msg, rtp_end, rtp_state);
940
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200941 do {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200942 /* Run transcoder */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200943 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200944 (char*)msgb_data(msg), &buflen,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200945 RTP_BUF_SIZE);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200946 if (cont < 0)
947 break;
948
Philipp Maier87bd9be2017-08-22 16:35:41 +0200949 if (addr)
950 mgcp_patch_and_count(endp, rtp_state, rtp_end,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200951 addr, msg);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100952
Philipp Maier228e5912019-03-05 13:56:59 +0100953 if (amr_oa_bwe_convert_indicated(conn_dst->end.codec)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200954 rc = amr_oa_bwe_convert(endp, msg,
Neels Hofmeyr5a6220f2019-08-20 03:09:04 +0200955 conn_dst->end.codec->param.amr_octet_aligned);
956 if (rc < 0) {
957 LOGPENDP(endp, DRTP, LOGL_ERROR,
958 "Error in AMR octet-aligned <-> bandwidth-efficient mode conversion\n");
959 break;
960 }
Philipp Maier228e5912019-03-05 13:56:59 +0100961 }
962 else if (rtp_end->rfc5993_hr_convert
Philipp Maier9fc8a022019-02-20 12:26:52 +0100963 && strcmp(conn_src->end.codec->subtype_name,
Neels Hofmeyr5a6220f2019-08-20 03:09:04 +0200964 "GSM-HR-08") == 0) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200965 rc = rfc5993_hr_convert(endp, msg);
Neels Hofmeyr5a6220f2019-08-20 03:09:04 +0200966 if (rc < 0) {
967 LOGPENDP(endp, DRTP, LOGL_ERROR, "Error while converting to GSM-HR-08\n");
968 break;
969 }
970 }
Philipp Maier9fc8a022019-02-20 12:26:52 +0100971
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200972 LOGPENDP(endp, DRTP, LOGL_DEBUG,
973 "process/send to %s %s "
974 "rtp_port:%u rtcp_port:%u\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200975 dest_name,
976 osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200977 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
978 );
Philipp Maier87bd9be2017-08-22 16:35:41 +0200979
980 /* Forward a copy of the RTP data to a debug ip/port */
981 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200982 msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200983
984 /* FIXME: HACK HACK HACK. See OS#2459.
985 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
986 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
987 * cells (as long as we patch only the first RTP payload in each stream).
988 */
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200989 if (!rtp_state->patched_first_rtp_payload
990 && conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200991 uint8_t *data = msgb_data(msg) + 12;
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200992 if (data[0] == 0xe0) {
993 data[0] = 0xe4;
994 data[1] = 0x00;
995 rtp_state->patched_first_rtp_payload = true;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200996 LOGPENDP(endp, DRTP, LOGL_DEBUG,
997 "Patching over first two bytes"
998 " to fake an IuUP Initialization Ack\n");
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200999 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001000 }
1001
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001002 len = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr, rtp_end->rtp_port,
1003 (char*)msgb_data(msg), msgb_length(msg));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001004
Philipp Maier87bd9be2017-08-22 16:35:41 +02001005 if (len <= 0)
1006 return len;
1007
Harald Weltea48ff4a2020-03-08 14:45:08 +01001008 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
1009 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
Philipp Maier889fe7f2020-07-06 17:44:12 +02001010 rtp_state->alt_rtp_tx_sequence++;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001011
1012 nbytes += len;
1013 buflen = cont;
1014 } while (buflen > 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001015 return nbytes;
Philipp Maier14b27a82020-06-02 20:15:30 +02001016 } else if (!trunk->omit_rtcp) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001017 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1018 "send to %s %s rtp_port:%u rtcp_port:%u\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001019 dest_name, osmo_sockaddr_ntop(&rtp_end->addr.u.sa, ipbuf),
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001020 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
1021 );
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001022
Philipp Maier87bd9be2017-08-22 16:35:41 +02001023 len = mgcp_udp_send(rtp_end->rtcp.fd,
1024 &rtp_end->addr,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001025 rtp_end->rtcp_port, (char*)msgb_data(msg), msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001026
Harald Weltea48ff4a2020-03-08 14:45:08 +01001027 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
1028 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
Philipp Maier889fe7f2020-07-06 17:44:12 +02001029 rtp_state->alt_rtp_tx_sequence++;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001030
1031 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001032 }
1033
1034 return 0;
1035}
1036
Philipp Maier87bd9be2017-08-22 16:35:41 +02001037/* Check if the origin (addr) matches the address/port data of the RTP
1038 * connections. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001039static int check_rtp_origin(struct mgcp_conn_rtp *conn, struct osmo_sockaddr *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001040{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001041 char ipbuf[INET6_ADDRSTRLEN];
1042
1043 if (addr_is_any(&conn->end.addr)) {
Neels Hofmeyrefd645e2018-09-10 13:14:50 +02001044 switch (conn->conn->mode) {
1045 case MGCP_CONN_LOOPBACK:
1046 /* HACK: for IuUP, we want to reply with an IuUP Initialization ACK upon the first RTP
1047 * message received. We currently hackishly accomplish that by putting the endpoint in
1048 * loopback mode and patching over the looped back RTP message to make it look like an
1049 * ack. We don't know the femto cell's IP address and port until the RAB Assignment
1050 * Response is received, but the nano3G expects an IuUP Initialization Ack before it even
1051 * sends the RAB Assignment Response. Hence, if the remote address is 0.0.0.0 and the
1052 * MGCP port is in loopback mode, allow looping back the packet to any source. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001053 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1054 "In loopback mode and remote address not set:"
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001055 " allowing data from address: %s\n",
1056 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +02001057 return 0;
1058
1059 default:
1060 /* Receiving early media before the endpoint is configured. Instead of logging
1061 * this as an error that occurs on every call, keep it more low profile to not
1062 * confuse humans with expected errors. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001063 LOGPCONN(conn->conn, DRTP, LOGL_INFO,
1064 "Rx RTP from %s, but remote address not set:"
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001065 " dropping early media\n",
1066 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +02001067 return -1;
1068 }
Neels Hofmeyr0063ca22018-07-23 18:12:16 +02001069 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001070
Philipp Maier87bd9be2017-08-22 16:35:41 +02001071 /* Note: Check if the inbound RTP data comes from the same host to
1072 * which we send our outgoing RTP traffic. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001073 if (conn->end.addr.u.sa.sa_family != addr->u.sa.sa_family ||
1074 (conn->end.addr.u.sa.sa_family == AF_INET &&
1075 conn->end.addr.u.sin.sin_addr.s_addr != addr->u.sin.sin_addr.s_addr) ||
1076 (conn->end.addr.u.sa.sa_family == AF_INET6 &&
1077 memcmp(&conn->end.addr.u.sin6.sin6_addr, &addr->u.sin6.sin6_addr,
1078 sizeof(struct in6_addr)))) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001079 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001080 "data from wrong address: %s, ",
1081 osmo_sockaddr_ntop(&addr->u.sa, ipbuf));
Philipp Maierc3413882017-10-27 12:26:54 +02001082 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001083 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001084 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001085 return -1;
1086 }
1087
Philipp Maier87bd9be2017-08-22 16:35:41 +02001088 /* Note: Usually the remote remote port of the data we receive will be
1089 * the same as the remote port where we transmit outgoing RTP traffic
1090 * to (set by MDCX). We use this to check the origin of the data for
1091 * plausibility. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001092 if (ntohs(conn->end.rtp_port) != osmo_sockaddr_port(&addr->u.sa) &&
1093 ntohs(conn->end.rtcp_port) != osmo_sockaddr_port(&addr->u.sa)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001094 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001095 "data from wrong source port: %d, ",
1096 osmo_sockaddr_port(&addr->u.sa));
Philipp Maierc3413882017-10-27 12:26:54 +02001097 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001098 "expected: %d for RTP or %d for RTCP\n",
1099 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001100 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001101 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001102 }
1103
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001104 return 0;
1105}
1106
Philipp Maier87bd9be2017-08-22 16:35:41 +02001107/* Check the if the destination address configuration of an RTP connection
1108 * makes sense */
1109static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001110{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001111 char ipbuf[INET6_ADDRSTRLEN];
1112 bool ip_is_any = addr_is_any(&conn->end.addr);
1113
Philipp Maiere6df0e42018-05-29 14:03:06 +02001114 /* Note: it is legal to create a connection but never setting a port
1115 * and IP-address for outgoing data. */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001116 if (ip_is_any && conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001117 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
1118 "destination IP-address and rtp port is (not yet) known (%s:%u)\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001119 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port);
Philipp Maiere6df0e42018-05-29 14:03:06 +02001120 return -1;
1121 }
1122
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001123 if (ip_is_any) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001124 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1125 "destination IP-address is invalid (%s:%u)\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001126 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001127 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001128 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001129
1130 if (conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001131 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1132 "destination rtp port is invalid (%s:%u)\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001133 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001134 return -1;
1135 }
1136
1137 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001138}
1139
Philipp Maier4dba7692018-08-03 12:20:52 +02001140/* Do some basic checks to make sure that the RTCP packets we are going to
1141 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001142static int check_rtcp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +02001143{
1144 struct rtcp_hdr *hdr;
1145 unsigned int len;
1146 uint8_t type;
1147
1148 /* RTPC packets that are just a header without data do not make
1149 * any sense. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001150 if (msgb_length(msg) < sizeof(struct rtcp_hdr)) {
1151 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP packet too short (%u < %zu)\n",
1152 msgb_length(msg), sizeof(struct rtcp_hdr));
Philipp Maier4dba7692018-08-03 12:20:52 +02001153 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001154 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001155
1156 /* Make sure that the length of the received packet does not exceed
1157 * the available buffer size */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001158 hdr = (struct rtcp_hdr *)msgb_data(msg);
Philipp Maier4dba7692018-08-03 12:20:52 +02001159 len = (osmo_ntohs(hdr->length) + 1) * 4;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001160 if (len > msgb_length(msg)) {
1161 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header length exceeds packet size (%u > %u)\n",
1162 len, msgb_length(msg));
Philipp Maier4dba7692018-08-03 12:20:52 +02001163 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001164 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001165
1166 /* Make sure we accept only packets that have a proper packet type set
1167 * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
1168 type = hdr->type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001169 if ((type < 192 || type > 195) && (type < 200 || type > 213)) {
1170 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header: invalid type: %u\n", type);
Philipp Maier4dba7692018-08-03 12:20:52 +02001171 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001172 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001173
1174 return 0;
1175}
1176
1177/* Do some basic checks to make sure that the RTP packets we are going to
1178 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001179static int check_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +02001180{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001181 size_t min_size = sizeof(struct rtp_hdr);
1182 if (msgb_length(msg) < min_size) {
1183 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
1184 msgb_length(msg), min_size);
1185 return -1;
1186 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001187
1188 /* FIXME: Add more checks, the reason why we do not check more than
1189 * the length is because we currently handle IUUP packets as RTP
1190 * packets, so they must pass this check, if we weould be more
1191 * strict here, we would possibly break 3G. (see also FIXME note
1192 * below */
1193
1194 return 0;
1195}
1196
Philipp Maier87bd9be2017-08-22 16:35:41 +02001197/* Send RTP data. Possible options are standard RTP packet
1198 * transmission or trsmission via an osmux connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001199static int mgcp_send_rtp(struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001200{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001201 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1202 enum rtp_proto proto = mc->proto;
1203 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001204 struct mgcp_endpoint *endp = conn_src->conn->endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001205
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001206 LOGPENDP(endp, DRTP, LOGL_DEBUG, "destin conn:%s\n",
1207 mgcp_conn_dump(conn_dst->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001208
1209 /* Before we try to deliver the packet, we check if the destination
1210 * port and IP-Address make sense at all. If not, we will be unable
1211 * to deliver the packet. */
1212 if (check_rtp_destin(conn_dst) != 0)
1213 return -1;
1214
1215 /* Depending on the RTP connection type, deliver the RTP packet to the
1216 * destination connection. */
1217 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001218 case MGCP_RTP_DEFAULT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001219 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1220 "endpoint type is MGCP_RTP_DEFAULT, "
1221 "using mgcp_send() to forward data directly\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001222 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001223 mc->from_addr, msg, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001224 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001225 case MGCP_OSMUX_BSC:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001226 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1227 "endpoint type is MGCP_OSMUX_BSC_NAT, "
1228 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n");
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001229 return osmux_xfrm_to_osmux((char*)msgb_data(msg), msgb_length(msg), conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001230 }
1231
Philipp Maier87bd9be2017-08-22 16:35:41 +02001232 /* If the data has not been handled/forwarded until here, it will
1233 * be discarded, this should not happen, normally the MGCP type
1234 * should be properly set */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001235 LOGPENDP(endp, DRTP, LOGL_ERROR, "bad MGCP type -- data discarded!\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001236
1237 return -1;
1238}
1239
1240/*! dispatch incoming RTP packet to opposite RTP connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001241 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1242 * \param[in] addr socket address where the RTP packet has been received from.
1243 * \param[in] buf buffer that hold the RTP payload.
1244 * \param[in] buf_size size data length of buf.
1245 * \param[in] conn originating connection.
1246 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001247int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001248{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001249 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1250 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1251 struct mgcp_conn *conn = conn_src->conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001252 struct mgcp_conn *conn_dst;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001253 struct osmo_sockaddr *from_addr = mc->from_addr;
Philipp Maier97a93122021-05-07 22:50:31 +02001254 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001255
1256 /*! NOTE: This callback function implements the endpoint specific
Alexander Chemeris61cf9bb2020-05-11 18:13:53 +03001257 * dispatch behaviour of an rtp bridge/proxy endpoint. It is assumed
Philipp Maier87bd9be2017-08-22 16:35:41 +02001258 * that the endpoint will hold only two connections. This premise
1259 * is used to determine the opposite connection (it is always the
1260 * connection that is not the originating connection). Once the
1261 * destination connection is known the RTP packet is sent via
1262 * the destination connection. */
1263
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001264
1265 /* Check if the connection is in loopback mode, if yes, just send the
1266 * incoming data back to the origin */
1267 if (conn->mode == MGCP_CONN_LOOPBACK) {
1268 /* When we are in loopback mode, we loop back all incoming
1269 * packets back to their origin. We will use the originating
1270 * address data from the UDP packet header to patch the
1271 * outgoing address in connection on the fly */
1272 if (conn->u.rtp.end.rtp_port == 0) {
Philipp Maier97a93122021-05-07 22:50:31 +02001273 memcpy(&conn->u.rtp.end.addr, from_addr,
1274 sizeof(conn->u.rtp.end.addr));
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001275 switch (from_addr->u.sa.sa_family) {
1276 case AF_INET:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001277 conn->u.rtp.end.rtp_port = from_addr->u.sin.sin_port;
1278 break;
1279 case AF_INET6:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001280 conn->u.rtp.end.rtp_port = from_addr->u.sin6.sin6_port;
1281 break;
1282 default:
1283 OSMO_ASSERT(false);
1284 }
Philipp Maier97a93122021-05-07 22:50:31 +02001285 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1286 "loopback mode: implicitly using source address (%s:%u) as destination address\n",
1287 osmo_sockaddr_ntop(&from_addr->u.sa, ipbuf),
1288 conn->u.rtp.end.rtp_port);
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;
Philipp Maier97a93122021-05-07 22:50:31 +02001338 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001339
Philipp Maier889fe7f2020-07-06 17:44:12 +02001340 /* Check if the connection is in loopback mode, if yes, just send the
1341 * incoming data back to the origin */
1342 if (conn->mode == MGCP_CONN_LOOPBACK) {
1343 /* When we are in loopback mode, we loop back all incoming
1344 * packets back to their origin. We will use the originating
1345 * address data from the UDP packet header to patch the
1346 * outgoing address in connection on the fly */
1347 if (conn->u.rtp.end.rtp_port == 0) {
Philipp Maier97a93122021-05-07 22:50:31 +02001348 memcpy(&conn->u.rtp.end.addr, from_addr,
1349 sizeof(conn->u.rtp.end.addr));
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001350 switch (from_addr->u.sa.sa_family) {
1351 case AF_INET:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001352 conn->u.rtp.end.rtp_port = from_addr->u.sin.sin_port;
1353 break;
1354 case AF_INET6:
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001355 conn->u.rtp.end.rtp_port = from_addr->u.sin6.sin6_port;
1356 break;
1357 default:
1358 OSMO_ASSERT(false);
1359 }
Philipp Maier97a93122021-05-07 22:50:31 +02001360 LOG_CONN_RTP(conn_src, LOGL_NOTICE,
1361 "loopback mode: implicitly using source address (%s:%u) as destination address\n",
1362 osmo_sockaddr_ntop(&from_addr->u.sa, ipbuf),
1363 conn->u.rtp.end.rtp_port);
Philipp Maier889fe7f2020-07-06 17:44:12 +02001364 }
1365 return mgcp_send_rtp(conn_src, msg);
1366 }
1367
1368 /* Forward to E1 */
1369 return mgcp_e1_send_rtp(conn->endp, conn->u.rtp.end.codec, msg);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001370}
1371
Philipp Maierdf5d2192018-01-24 11:39:32 +01001372/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1373 * \param[in] endp Endpoint on which the connection resides.
Philipp Maier08eb9352020-06-18 11:55:35 +02001374 * \param[in] conn Connection that is about to be removed (ignored). */
Philipp Maierdf5d2192018-01-24 11:39:32 +01001375void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1376{
1377 struct mgcp_conn *conn_cleanup;
1378
1379 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1380 * pointer to the destination connection, so that we do not have
1381 * to go through the list every time an RTP packet arrives. To prevent
1382 * a use-after-free situation we invalidate this information for all
1383 * connections present when one connection is removed from the
1384 * endpoint. */
1385 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
Pau Espin Pedrol6d0a59a2020-09-08 16:50:22 +02001386 if (conn_cleanup->priv == conn)
1387 conn_cleanup->priv = NULL;
Philipp Maierdf5d2192018-01-24 11:39:32 +01001388 }
1389}
1390
Philipp Maier0996a1e2020-06-10 15:27:14 +02001391/*! cleanup an endpoint when a connection on an E1 endpoint is removed.
1392 * \param[in] endp Endpoint on which the connection resides.
1393 * \param[in] conn Connection that is about to be removed (ignored). */
1394void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1395{
Philipp Maier889fe7f2020-07-06 17:44:12 +02001396 /* Cleanup tasks for E1 are the same as for regular endpoint. The
1397 * shut down of the E1 part is handled separately. */
1398 mgcp_cleanup_rtp_bridge_cb(endp, conn);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001399}
1400
Philipp Maier87bd9be2017-08-22 16:35:41 +02001401/* Handle incoming RTP data from NET */
1402static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1403{
1404 /* NOTE: This is a generic implementation. RTP data is received. In
1405 * case of loopback the data is just sent back to its origin. All
1406 * other cases implement endpoint specific behaviour (e.g. how is the
1407 * destination connection determined?). That specific behaviour is
1408 * implemented by the callback function that is called at the end of
1409 * the function */
1410
1411 struct mgcp_conn_rtp *conn_src;
1412 struct mgcp_endpoint *endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001413 struct osmo_sockaddr addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001414 socklen_t slen = sizeof(addr);
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001415 char ipbuf[INET6_ADDRSTRLEN];
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001416 int ret;
1417 enum rtp_proto proto;
1418 struct osmo_rtp_msg_ctx *mc;
1419 struct msgb *msg = msgb_alloc(RTP_BUF_SIZE, "RTP-rx");
1420 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001421
1422 conn_src = (struct mgcp_conn_rtp *)fd->data;
1423 OSMO_ASSERT(conn_src);
1424 endp = conn_src->conn->endp;
1425 OSMO_ASSERT(endp);
1426
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001427 proto = (fd == &conn_src->end.rtp)? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001428
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001429 ret = recvfrom(fd->fd, msgb_data(msg), msg->data_len, 0, (struct sockaddr *)&addr.u.sa, &slen);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001430
1431 if (ret <= 0) {
1432 LOG_CONN_RTP(conn_src, LOGL_ERROR, "recvfrom error: %s\n", strerror(errno));
1433 rc = -1;
1434 goto out;
1435 }
1436
1437 msgb_put(msg, ret);
1438
1439 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "%s: rx %u bytes from %s:%u\n",
1440 proto == MGCP_PROTO_RTP ? "RTP" : "RTPC",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001441 msgb_length(msg), osmo_sockaddr_ntop(&addr.u.sa, ipbuf),
1442 osmo_sockaddr_port(&addr.u.sa));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001443
1444 if ((proto == MGCP_PROTO_RTP && check_rtp(conn_src, msg))
1445 || (proto == MGCP_PROTO_RTCP && check_rtcp(conn_src, msg))) {
1446 /* Logging happened in the two check_ functions */
1447 rc = -1;
1448 goto out;
1449 }
1450
Philipp Maierb3d14eb2021-05-20 14:18:52 +02001451 if (mgcp_is_rtp_dummy_payload(msg)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001452 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx dummy packet (dropped)\n");
1453 rc = 0;
1454 goto out;
1455 }
1456
1457 /* Since the msgb remains owned and freed by this function, the msg ctx data struct can just be on the stack and
1458 * needs not be allocated with the msgb. */
1459 mc = OSMO_RTP_MSG_CTX(msg);
1460 *mc = (struct osmo_rtp_msg_ctx){
1461 .proto = proto,
1462 .conn_src = conn_src,
1463 .from_addr = &addr,
1464 };
1465 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "msg ctx: %d %p %s\n",
1466 mc->proto, mc->conn_src,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001467 osmo_hexdump((void*)mc->from_addr,
1468 mc->from_addr->u.sa.sa_family == AF_INET6 ?
1469 sizeof(struct sockaddr_in6) :
1470 sizeof(struct sockaddr_in)));
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001471
1472 /* Increment RX statistics */
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001473 rate_ctr_inc(rate_ctr_group_get_ctr(conn_src->rate_ctr_group, RTP_PACKETS_RX_CTR));
1474 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 +02001475 /* FIXME: count RTP and RTCP separately, also count IuUP payload-less separately */
1476
1477 /* Forward a copy of the RTP data to a debug ip/port */
1478 forward_data(fd->fd, &conn_src->tap_in, msg);
1479
1480 rc = rx_rtp(msg);
1481
1482out:
1483 msgb_free(msg);
1484 return rc;
1485}
1486
1487static int rx_rtp(struct msgb *msg)
1488{
1489 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1490 struct mgcp_conn_rtp *conn_src = mc->conn_src;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001491 struct osmo_sockaddr *from_addr = mc->from_addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001492 struct mgcp_conn *conn = conn_src->conn;
1493 struct mgcp_trunk *trunk = conn->endp->trunk;
1494
1495 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx_rtp(%u bytes)\n", msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001496
Oliver Smithe36b7752019-01-22 16:31:36 +01001497 mgcp_conn_watchdog_kick(conn_src->conn);
1498
Philipp Maier228e5912019-03-05 13:56:59 +01001499 /* If AMR is configured for the ingress connection a conversion of the
1500 * framing mode (octet-aligned vs. bandwith-efficient is explicitly
1501 * define, then we check if the incoming payload matches that
1502 * expectation. */
1503 if (amr_oa_bwe_convert_indicated(conn_src->end.codec)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001504 int oa = amr_oa_check((char*)msgb_data(msg), msgb_length(msg));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +02001505 if (oa < 0)
1506 return -1;
1507 if (((bool)oa) != conn_src->end.codec->param.amr_octet_aligned)
Philipp Maier228e5912019-03-05 13:56:59 +01001508 return -1;
1509 }
1510
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001511 /* Check if the origin of the RTP packet seems plausible */
1512 if (!trunk->rtp_accept_all && check_rtp_origin(conn_src, from_addr))
1513 return -1;
1514
Philipp Maier87bd9be2017-08-22 16:35:41 +02001515 /* Execute endpoint specific implementation that handles the
1516 * dispatching of the RTP data */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001517 return conn->endp->type->dispatch_rtp_cb(msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001518}
1519
Philipp Maier87bd9be2017-08-22 16:35:41 +02001520/*! bind RTP port to osmo_fd.
Philipp Maier0b79d212020-06-18 12:02:49 +02001521 * \param[in] source_addr source (local) address to bind on.
1522 * \param[in] fd associated file descriptor.
1523 * \param[in] port to bind on.
Harald Welte55a92292021-04-28 19:06:34 +02001524 * \param[in] dscp IP DSCP value to use.
1525 * \param[in] prio socket priority to use.
Philipp Maier0b79d212020-06-18 12:02:49 +02001526 * \returns 0 on success, -1 on ERROR. */
Harald Welte55a92292021-04-28 19:06:34 +02001527int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port, uint8_t dscp,
1528 uint8_t prio)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001529{
Harald Welte8890dfa2017-11-17 15:09:30 +01001530 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001531
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02001532 rc = osmo_sock_init2(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
Harald Welte55a92292021-04-28 19:06:34 +02001533 NULL, 0, OSMO_SOCK_F_BIND | OSMO_SOCK_F_DSCP(dscp) |
1534 OSMO_SOCK_F_PRIO(prio));
Harald Welte8890dfa2017-11-17 15:09:30 +01001535 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001536 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001537 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001538 return -1;
1539 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001540 fd->fd = rc;
1541 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001542
1543 return 0;
1544}
1545
Philipp Maier87bd9be2017-08-22 16:35:41 +02001546/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001547static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001548 struct mgcp_rtp_end *rtp_end, struct mgcp_endpoint *endp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001549{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001550 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1551 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1552
Harald Welte55a92292021-04-28 19:06:34 +02001553 if (mgcp_create_bind(source_addr, &rtp_end->rtp, rtp_end->local_port,
1554 cfg->endp_dscp, cfg->endp_priority) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001555 LOGPENDP(endp, DRTP, LOGL_ERROR,
1556 "failed to create RTP port: %s:%d\n",
1557 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001558 goto cleanup0;
1559 }
1560
Harald Welte55a92292021-04-28 19:06:34 +02001561 if (mgcp_create_bind(source_addr, &rtp_end->rtcp, rtp_end->local_port + 1,
1562 cfg->endp_dscp, cfg->endp_priority) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001563 LOGPENDP(endp, DRTP, LOGL_ERROR,
1564 "failed to create RTCP port: %s:%d\n",
1565 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001566 goto cleanup1;
1567 }
1568
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001569 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001570 LOGPENDP(endp, DRTP, LOGL_ERROR,
1571 "failed to register RTP port %d\n",
1572 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001573 goto cleanup2;
1574 }
1575
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001576 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001577 LOGPENDP(endp, DRTP, LOGL_ERROR,
1578 "failed to register RTCP port %d\n",
1579 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001580 goto cleanup3;
1581 }
1582
1583 return 0;
1584
1585cleanup3:
1586 osmo_fd_unregister(&rtp_end->rtp);
1587cleanup2:
1588 close(rtp_end->rtcp.fd);
1589 rtp_end->rtcp.fd = -1;
1590cleanup1:
1591 close(rtp_end->rtp.fd);
1592 rtp_end->rtp.fd = -1;
1593cleanup0:
1594 return -1;
1595}
1596
Philipp Maier87bd9be2017-08-22 16:35:41 +02001597/*! bind RTP port to endpoint/connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001598 * \param[in] endp endpoint that holds the RTP connection.
1599 * \param[in] rtp_port port number to bind on.
1600 * \param[in] conn associated RTP connection.
1601 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001602int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1603 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001604{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001605 char name[512];
1606 struct mgcp_rtp_end *end;
1607
Philipp Maier01d24a32017-11-21 17:26:09 +01001608 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001609 end = &conn->end;
1610
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001611 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001612 LOGPENDP(endp, DRTP, LOGL_ERROR, "%u was already bound on conn:%s\n",
1613 rtp_port, mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001614
1615 /* Double bindings should never occour! Since we always allocate
1616 * connections dynamically and free them when they are not
1617 * needed anymore, there must be no previous binding leftover.
1618 * Should there be a connection bound twice, we have a serious
1619 * problem and must exit immediately! */
1620 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001621 }
1622
1623 end->local_port = rtp_port;
Harald Weltec2a8f592020-10-19 13:25:41 +02001624 osmo_fd_setup(&end->rtp, -1, OSMO_FD_READ, rtp_data_net, conn, 0);
1625 osmo_fd_setup(&end->rtcp, -1, OSMO_FD_READ, rtp_data_net, conn, 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001626
Pau Espin Pedrol71d42e72020-09-03 14:20:07 +02001627 return bind_rtp(endp->cfg, conn->end.local_addr, end, endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001628}
1629
Philipp Maier87bd9be2017-08-22 16:35:41 +02001630/*! free allocated RTP and RTCP ports.
1631 * \param[in] end RTP end */
1632void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001633{
1634 if (end->rtp.fd != -1) {
1635 close(end->rtp.fd);
1636 end->rtp.fd = -1;
1637 osmo_fd_unregister(&end->rtp);
1638 }
1639
1640 if (end->rtcp.fd != -1) {
1641 close(end->rtcp.fd);
1642 end->rtcp.fd = -1;
1643 osmo_fd_unregister(&end->rtcp);
1644 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001645}