blob: 712c706ed959404b71700ac498e56071397ef98d [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
Philipp Maier1cb1e382017-11-02 17:16:04 +010080/*! Determine the local rtp bind IP-address.
Philipp Maier0b79d212020-06-18 12:02:49 +020081 * \param[out] addr caller provided memory to store the resulting IP-Address.
82 * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters.
Philipp Maier1cb1e382017-11-02 17:16:04 +010083 *
84 * The local bind IP-address is automatically selected by probing the
85 * IP-Address of the interface that is pointing towards the remote IP-Address,
86 * if no remote IP-Address is known yet, the statically configured
87 * IP-Addresses are used as fallback. */
88void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn)
89{
90
91 struct mgcp_endpoint *endp;
92 int rc;
93 endp = conn->conn->endp;
94
95 /* Try probing the local IP-Address */
96 if (endp->cfg->net_ports.bind_addr_probe && conn->end.addr.s_addr != 0) {
97 rc = osmo_sock_local_ip(addr, inet_ntoa(conn->end.addr));
98 if (rc < 0)
Pau Espin Pedrol3239f622019-04-24 18:47:46 +020099 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
100 "local interface auto detection failed, using configured addresses...\n");
Philipp Maier1cb1e382017-11-02 17:16:04 +0100101 else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200102 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
103 "selected local rtp bind ip %s by probing using remote ip %s\n",
104 addr, inet_ntoa(conn->end.addr));
Philipp Maier1cb1e382017-11-02 17:16:04 +0100105 return;
106 }
107 }
108
Pau Espin Pedrola93c6e92019-05-06 15:23:57 +0200109 /* Select from preconfigured IP-Addresses. We don't have bind_addr for Osmux (yet?). */
Philipp Maier1cb1e382017-11-02 17:16:04 +0100110 if (endp->cfg->net_ports.bind_addr) {
111 /* Check there is a bind IP for the RTP traffic configured,
112 * if so, use that IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100113 osmo_strlcpy(addr, endp->cfg->net_ports.bind_addr, INET_ADDRSTRLEN);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200114 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
115 "using configured rtp bind ip as local bind ip %s\n",
116 addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100117 } else {
118 /* No specific bind IP is configured for the RTP traffic, so
119 * assume the IP where we listen for incoming MGCP messages
120 * as bind IP */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100121 osmo_strlcpy(addr, endp->cfg->source_addr, INET_ADDRSTRLEN);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200122 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
123 "using mgcp bind ip as local rtp bind ip: %s\n", addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100124 }
125}
126
Philipp Maier87bd9be2017-08-22 16:35:41 +0200127/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200128 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +0200129 * 1/codec_rate seconds. */
130static uint32_t get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200131{
132 struct timespec tp;
133 uint64_t ret;
134
Philipp Maier87bd9be2017-08-22 16:35:41 +0200135 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200136 return 0;
137
138 memset(&tp, 0, sizeof(tp));
139 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maierc3413882017-10-27 12:26:54 +0200140 LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200141
142 /* convert it to 1/unit seconds */
143 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200144 ret *= codec_rate;
145 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200146
147 return ret;
148}
149
Philipp Maier87bd9be2017-08-22 16:35:41 +0200150/*! send udp packet.
Philipp Maier0b79d212020-06-18 12:02:49 +0200151 * \param[in] fd associated file descriptor.
152 * \param[in] addr destination ip-address.
153 * \param[in] port destination UDP port (network byte order).
154 * \param[in] buf buffer that holds the data to be send.
155 * \param[in] len length of the data to be sent.
156 * \returns bytes sent, -1 on error. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200157int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
158{
159 struct sockaddr_in out;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200160
Philipp Maierc3413882017-10-27 12:26:54 +0200161 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200162 "sending %i bytes length packet to %s:%u ...\n",
163 len, inet_ntoa(*addr), ntohs(port));
164
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200165 out.sin_family = AF_INET;
166 out.sin_port = port;
167 memcpy(&out.sin_addr, addr, sizeof(*addr));
168
169 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
170}
171
Philipp Maier87bd9be2017-08-22 16:35:41 +0200172/*! send RTP dummy packet (to keep NAT connection open).
Philipp Maier0b79d212020-06-18 12:02:49 +0200173 * \param[in] endp mcgp endpoint that holds the RTP connection.
174 * \param[in] conn associated RTP connection.
175 * \returns bytes sent, -1 on error. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200176int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200177{
178 static char buf[] = { MGCP_DUMMY_LOAD };
179 int rc;
180 int was_rtcp = 0;
181
Philipp Maier87bd9be2017-08-22 16:35:41 +0200182 OSMO_ASSERT(endp);
183 OSMO_ASSERT(conn);
184
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200185 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,"sending dummy packet... %s\n",
186 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200187
188 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
189 conn->end.rtp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200190
191 if (rc == -1)
192 goto failed;
193
Philipp Maier14b27a82020-06-02 20:15:30 +0200194 if (endp->trunk->omit_rtcp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200195 return rc;
196
197 was_rtcp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200198 rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
199 conn->end.rtcp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200200
201 if (rc >= 0)
202 return rc;
203
204failed:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200205 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
206 "Failed to send dummy %s packet.\n",
207 was_rtcp ? "RTCP" : "RTP");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200208
209 return -1;
210}
211
Philipp Maier87bd9be2017-08-22 16:35:41 +0200212/* Compute timestamp alignment error */
213static int32_t ts_alignment_error(struct mgcp_rtp_stream_state *sstate,
214 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200215{
216 int32_t timestamp_delta;
217
218 if (ptime == 0)
219 return 0;
220
221 /* Align according to: T - Tlast = k * Tptime */
222 timestamp_delta = timestamp - sstate->last_timestamp;
223
224 return timestamp_delta % ptime;
225}
226
Philipp Maier87bd9be2017-08-22 16:35:41 +0200227/* Check timestamp and sequence number for plausibility */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200228static int check_rtp_timestamp(struct mgcp_endpoint *endp,
229 struct mgcp_rtp_state *state,
230 struct mgcp_rtp_stream_state *sstate,
231 struct mgcp_rtp_end *rtp_end,
232 struct sockaddr_in *addr,
233 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200234 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200235{
236 int32_t tsdelta;
237 int32_t timestamp_error;
238
239 /* Not fully intialized, skip */
240 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
241 return 0;
242
243 if (seq == sstate->last_seq) {
244 if (timestamp != sstate->last_timestamp) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200245 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200246 LOGPENDP(endp, DRTP, LOGL_ERROR,
247 "The %s timestamp delta is != 0 but the sequence "
248 "number %d is the same, "
249 "TS offset: %d, SeqNo offset: %d "
250 "on SSRC: %u timestamp: %u "
251 "from %s:%d\n",
252 text, seq,
253 state->patch.timestamp_offset, state->patch.seq_offset,
254 sstate->ssrc, timestamp, inet_ntoa(addr->sin_addr),
255 ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200256 }
257 return 0;
258 }
259
260 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200261 (int32_t)(timestamp - sstate->last_timestamp) /
262 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200263
264 if (tsdelta == 0) {
265 /* Don't update *tsdelta_out */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200266 LOGPENDP(endp, DRTP, LOGL_NOTICE,
267 "The %s timestamp delta is %d "
268 "on SSRC: %u timestamp: %u "
269 "from %s:%d\n",
270 text, tsdelta,
271 sstate->ssrc, timestamp,
272 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200273
274 return 0;
275 }
276
277 if (sstate->last_tsdelta != tsdelta) {
278 if (sstate->last_tsdelta) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200279 LOGPENDP(endp, DRTP, LOGL_INFO,
280 "The %s timestamp delta changes from %d to %d "
281 "on SSRC: %u timestamp: %u from %s:%d\n",
282 text, sstate->last_tsdelta, tsdelta,
283 sstate->ssrc, timestamp,
284 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200285 }
286 }
287
288 if (tsdelta_out)
289 *tsdelta_out = tsdelta;
290
291 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200292 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200293
294 if (timestamp_error) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200295 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200296 LOGPENDP(endp, DRTP, LOGL_NOTICE,
297 "The %s timestamp has an alignment error of %d "
298 "on SSRC: %u "
299 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
300 "from %s:%d. ptime: %d\n",
301 text, timestamp_error,
302 sstate->ssrc,
303 (int16_t)(seq - sstate->last_seq),
304 (int32_t)(timestamp - sstate->last_timestamp),
305 tsdelta,
306 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
307 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200308 }
309 return 1;
310}
311
312/* Set the timestamp offset according to the packet duration. */
313static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp,
314 struct mgcp_rtp_state *state,
315 struct mgcp_rtp_end *rtp_end,
316 struct sockaddr_in *addr,
317 int16_t delta_seq, uint32_t in_timestamp)
318{
319 int32_t tsdelta = state->packet_duration;
320 int timestamp_offset;
321 uint32_t out_timestamp;
322
323 if (tsdelta == 0) {
324 tsdelta = state->out_stream.last_tsdelta;
325 if (tsdelta != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200326 LOGPENDP(endp, DRTP, LOGL_NOTICE,
327 "A fixed packet duration is not available, "
328 "using last output timestamp delta instead: %d "
329 "from %s:%d\n", tsdelta,
330 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200331 } else {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200332 tsdelta = rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200333 LOGPENDP(endp, DRTP, LOGL_NOTICE,
334 "Fixed packet duration and last timestamp delta "
335 "are not available, "
336 "using fixed 20ms instead: %d "
337 "from %s:%d\n", tsdelta,
338 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200339 }
340 }
341
342 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
343 timestamp_offset = out_timestamp - in_timestamp;
344
Harald Welte33381352017-12-25 09:44:26 +0100345 if (state->patch.timestamp_offset != timestamp_offset) {
346 state->patch.timestamp_offset = timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200347
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200348 LOGPENDP(endp, DRTP, LOGL_NOTICE,
349 "Timestamp offset change on SSRC: %u "
350 "SeqNo delta: %d, TS offset: %d, "
351 "from %s:%d\n", state->in_stream.ssrc,
352 delta_seq, state->patch.timestamp_offset,
353 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200354 }
355
356 return timestamp_offset;
357}
358
359/* Set the timestamp offset according to the packet duration. */
360static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp,
361 struct mgcp_rtp_state *state,
362 struct mgcp_rtp_end *rtp_end,
363 struct sockaddr_in *addr,
364 uint32_t timestamp)
365{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200366 int ts_error = 0;
367 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200368 int ptime = state->packet_duration;
369
370 /* Align according to: T + Toffs - Tlast = k * Tptime */
371
Philipp Maier87bd9be2017-08-22 16:35:41 +0200372 ts_error = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100373 timestamp + state->patch.timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200374
Philipp Maier87bd9be2017-08-22 16:35:41 +0200375 /* If there is an alignment error, we have to compensate it */
376 if (ts_error) {
Harald Welte33381352017-12-25 09:44:26 +0100377 state->patch.timestamp_offset += ptime - ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200378
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200379 LOGPENDP(endp, DRTP, LOGL_NOTICE,
380 "Corrected timestamp alignment error of %d on SSRC: %u "
381 "new TS offset: %d, "
382 "from %s:%d\n",
383 ts_error, state->in_stream.ssrc,
384 state->patch.timestamp_offset, inet_ntoa(addr->sin_addr),
385 ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200386 }
387
Philipp Maier87bd9be2017-08-22 16:35:41 +0200388 /* Check we really managed to compensate the timestamp
389 * offset. There should not be any remaining error, failing
Harald Welte1d1b98f2017-12-25 10:03:40 +0100390 * here would point to a serous problem with the alignment
391 * error computation function */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200392 ts_check = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100393 timestamp + state->patch.timestamp_offset);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200394 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200395
Philipp Maier87bd9be2017-08-22 16:35:41 +0200396 /* Return alignment error before compensation */
397 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200398}
399
Philipp Maier87bd9be2017-08-22 16:35:41 +0200400/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200401 * \param[in] associated endpoint.
402 * \param[in] destination RTP end.
403 * \param[in,out] pointer to buffer with voice data.
404 * \param[in] voice data length.
405 * \param[in] maximum size of caller provided voice data buffer.
406 * \returns ignores input parameters, return always 0. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200407int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
408 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200409 char *data, int *len, int buf_size)
410{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200411 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200412 return 0;
413}
414
Philipp Maier87bd9be2017-08-22 16:35:41 +0200415/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200416 * \param[in] associated endpoint.
417 * \param[in] destination RTP connnection.
418 * \param[in] source RTP connection.
419 * \returns ignores input parameters, return always 0. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200420int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
Philipp Maieracc10352018-07-19 18:07:57 +0200421 struct mgcp_conn_rtp *conn_dst,
422 struct mgcp_conn_rtp *conn_src)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200423{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200424 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200425 return 0;
426}
427
428void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
Philipp Maier58128252019-03-06 11:28:18 +0100429 const struct mgcp_rtp_codec **codec,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200430 const char **fmtp_extra,
431 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200432{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200433 LOGPENDP(endp, DRTP, LOGL_DEBUG, "conn:%s using format defaults\n",
434 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200435
Philipp Maier58128252019-03-06 11:28:18 +0100436 *codec = conn->end.codec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200437 *fmtp_extra = conn->end.fmtp_extra;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200438}
439
Philipp Maier87bd9be2017-08-22 16:35:41 +0200440void mgcp_rtp_annex_count(struct mgcp_endpoint *endp,
441 struct mgcp_rtp_state *state, const uint16_t seq,
442 const int32_t transit, const uint32_t ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200443{
444 int32_t d;
445
446 /* initialize or re-initialize */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100447 if (!state->stats.initialized || state->stats.ssrc != ssrc) {
448 state->stats.initialized = 1;
449 state->stats.base_seq = seq;
450 state->stats.max_seq = seq - 1;
451 state->stats.ssrc = ssrc;
452 state->stats.jitter = 0;
453 state->stats.transit = transit;
454 state->stats.cycles = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200455 } else {
456 uint16_t udelta;
457
Philipp Maier87bd9be2017-08-22 16:35:41 +0200458 /* The below takes the shape of the validation of
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200459 * Appendix A. Check if there is something weird with
460 * the sequence number, otherwise check for a wrap
461 * around in the sequence number.
462 * It can't wrap during the initialization so let's
463 * skip it here. The Appendix A probably doesn't have
Philipp Maier87bd9be2017-08-22 16:35:41 +0200464 * this issue because of the probation. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100465 udelta = seq - state->stats.max_seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200466 if (udelta < RTP_MAX_DROPOUT) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100467 if (seq < state->stats.max_seq)
468 state->stats.cycles += RTP_SEQ_MOD;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200469 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200470 LOGPENDP(endp, DRTP, LOGL_NOTICE,
471 "RTP seqno made a very large jump on delta: %u\n",
472 udelta);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200473 }
474 }
475
Philipp Maier87bd9be2017-08-22 16:35:41 +0200476 /* Calculate the jitter between the two packages. The TS should be
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200477 * taken closer to the read function. This was taken from the
478 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
Philipp Maier87bd9be2017-08-22 16:35:41 +0200479 * resolution. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100480 d = transit - state->stats.transit;
481 state->stats.transit = transit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200482 if (d < 0)
483 d = -d;
Harald Welte49e3d5a2017-12-25 09:47:57 +0100484 state->stats.jitter += d - ((state->stats.jitter + 8) >> 4);
485 state->stats.max_seq = seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200486}
487
Philipp Maier6931f9a2018-07-26 09:29:31 +0200488/* There may be different payload type numbers negotiated for two connections.
489 * Patch the payload type of an RTP packet so that it uses the payload type
490 * that is valid for the destination connection (conn_dst) */
491static int mgcp_patch_pt(struct mgcp_conn_rtp *conn_src,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200492 struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200493{
494 struct rtp_hdr *rtp_hdr;
495 uint8_t pt_in;
496 int pt_out;
497
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200498 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
499 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
500 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200501 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200502 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200503
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200504 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier6931f9a2018-07-26 09:29:31 +0200505
506 pt_in = rtp_hdr->payload_type;
507 pt_out = mgcp_codec_pt_translate(conn_src, conn_dst, pt_in);
508 if (pt_out < 0)
509 return -EINVAL;
510
511 rtp_hdr->payload_type = (uint8_t) pt_out;
512 return 0;
513}
514
Philipp Maier87bd9be2017-08-22 16:35:41 +0200515/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200516 * some of the supported endpoints (e.g. the nanoBTS) can only handle
517 * one source and this code will patch RTP header to appear as if there
518 * is only one source.
519 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200520 * we receive will be seen as a switch in streams. */
521void mgcp_patch_and_count(struct mgcp_endpoint *endp,
522 struct mgcp_rtp_state *state,
523 struct mgcp_rtp_end *rtp_end,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200524 struct sockaddr_in *addr, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200525{
526 uint32_t arrival_time;
527 int32_t transit;
528 uint16_t seq;
529 uint32_t timestamp, ssrc;
530 struct rtp_hdr *rtp_hdr;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200531 int payload = rtp_end->codec->payload_type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200532 unsigned int len = msgb_length(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200533
534 if (len < sizeof(*rtp_hdr))
535 return;
536
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200537 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200538 seq = ntohs(rtp_hdr->sequence);
539 timestamp = ntohl(rtp_hdr->timestamp);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200540 arrival_time = get_current_ts(rtp_end->codec->rate);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200541 ssrc = ntohl(rtp_hdr->ssrc);
542 transit = arrival_time - timestamp;
543
544 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc);
545
546 if (!state->initialized) {
547 state->initialized = 1;
548 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100549 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200550 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200551 state->packet_duration =
552 mgcp_rtp_packet_duration(endp, rtp_end);
Philipp Maier0ec1d4e2018-05-16 11:09:42 +0200553 state->out_stream.last_seq = seq - 1;
554 state->out_stream.ssrc = state->patch.orig_ssrc = ssrc;
555 state->out_stream.last_tsdelta = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200556 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200557 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200558 LOGPENDP(endp, DRTP, LOGL_INFO,
559 "initializing stream, SSRC: %u timestamp: %u "
560 "pkt-duration: %d, from %s:%d\n",
561 state->in_stream.ssrc,
562 state->patch.seq_offset, state->packet_duration,
563 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200564 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200565 state->packet_duration =
Philipp Maierbc0346e2018-06-07 09:52:16 +0200566 rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200567 LOGPENDP(endp, DRTP, LOGL_NOTICE,
568 "fixed packet duration is not available, "
569 "using fixed 20ms instead: %d from %s:%d\n",
570 state->packet_duration,
571 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200572 }
573 } else if (state->in_stream.ssrc != ssrc) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200574 LOGPENDP(endp, DRTP, LOGL_NOTICE,
575 "SSRC changed: %u -> %u "
576 "from %s:%d\n",
577 state->in_stream.ssrc, rtp_hdr->ssrc,
578 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200579
580 state->in_stream.ssrc = ssrc;
581 if (rtp_end->force_constant_ssrc) {
582 int16_t delta_seq;
583
584 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100585 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200586 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200587
588 /* Estimate number of packets that would have been sent */
589 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200590 (arrival_time - state->in_stream.last_arrival_time
591 + state->packet_duration / 2) /
592 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200593
594 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
595 delta_seq, timestamp);
596
Harald Welte33381352017-12-25 09:44:26 +0100597 state->patch.patch_ssrc = 1;
598 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200599 if (rtp_end->force_constant_ssrc != -1)
600 rtp_end->force_constant_ssrc -= 1;
601
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200602 LOGPENDP(endp, DRTP, LOGL_NOTICE,
603 "SSRC patching enabled, SSRC: %u "
604 "SeqNo offset: %d, TS offset: %d "
605 "from %s:%d\n", state->in_stream.ssrc,
606 state->patch.seq_offset, state->patch.timestamp_offset,
607 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200608 }
609
610 state->in_stream.last_tsdelta = 0;
611 } else {
612 /* Compute current per-packet timestamp delta */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200613 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
614 addr, seq, timestamp, "input",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200615 &state->in_stream.last_tsdelta);
616
Harald Welte33381352017-12-25 09:44:26 +0100617 if (state->patch.patch_ssrc)
618 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200619 }
620
621 /* Save before patching */
622 state->in_stream.last_timestamp = timestamp;
623 state->in_stream.last_seq = seq;
624 state->in_stream.last_arrival_time = arrival_time;
625
626 if (rtp_end->force_aligned_timing &&
627 state->out_stream.ssrc == ssrc && state->packet_duration)
628 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200629 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
630 timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200631
632 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100633 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200634 rtp_hdr->ssrc = htonl(ssrc);
635
636 /* Apply the offset and store it back to the packet.
637 * This won't change anything if the offset is 0, so the conditional is
638 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100639 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200640 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100641 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200642 rtp_hdr->timestamp = htonl(timestamp);
643
644 /* Check again, whether the timestamps are still valid */
645 if (state->out_stream.ssrc == ssrc)
646 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
647 addr, seq, timestamp, "output",
648 &state->out_stream.last_tsdelta);
649
650 /* Save output values */
651 state->out_stream.last_seq = seq;
652 state->out_stream.last_timestamp = timestamp;
653 state->out_stream.ssrc = ssrc;
654
655 if (payload < 0)
656 return;
657
658#if 0
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200659 LOGPENDP(endp, DRTP, LOGL_DEBUG, "payload hdr payload %u -> endp payload %u\n",
660 rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200661 rtp_hdr->payload_type = payload;
662#endif
663}
664
Philipp Maier9fc8a022019-02-20 12:26:52 +0100665/* There are different dialects used to format and transfer voice data. When
666 * the receiving end expects GSM-HR data to be formated after RFC 5993, this
667 * function is used to convert between RFC 5993 and TS 101318, which we normally
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200668 * use.
669 * Return 0 on sucess, negative on errors like invalid data length. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200670static int rfc5993_hr_convert(struct mgcp_endpoint *endp, struct msgb *msg)
Philipp Maier9fc8a022019-02-20 12:26:52 +0100671{
Philipp Maier9fc8a022019-02-20 12:26:52 +0100672 struct rtp_hdr *rtp_hdr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200673 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200674 LOGPENDP(endp, DRTP, LOGL_ERROR, "AMR RTP packet too short (%d < %zu)\n",
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200675 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200676 return -EINVAL;
677 }
678
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200679 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100680
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200681 if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr)) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100682 /* TS 101318 encoding => RFC 5993 encoding */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200683 msgb_put(msg, 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100684 memmove(rtp_hdr->data + 1, rtp_hdr->data, GSM_HR_BYTES);
685 rtp_hdr->data[0] = 0x00;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200686 } else if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr) + 1) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100687 /* RFC 5993 encoding => TS 101318 encoding */
688 memmove(rtp_hdr->data, rtp_hdr->data + 1, GSM_HR_BYTES);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200689 msgb_trim(msg, msgb_length(msg) - 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100690 } else {
691 /* It is possible that multiple payloads occur in one RTP
692 * packet. This is not supported yet. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200693 LOGPENDP(endp, DRTP, LOGL_ERROR,
694 "cannot figure out how to convert RTP packet\n");
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200695 return -ENOTSUP;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100696 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200697 return 0;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100698}
699
Philipp Maier228e5912019-03-05 13:56:59 +0100700/* For AMR RTP two framing modes are defined RFC3267. There is a bandwith
701 * efficient encoding scheme where all fields are packed together one after
702 * another and an octet aligned mode where all fields are aligned to octet
703 * boundaries. This function is used to convert between the two modes */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200704static int amr_oa_bwe_convert(struct mgcp_endpoint *endp, struct msgb *msg,
Philipp Maier228e5912019-03-05 13:56:59 +0100705 bool target_is_oa)
706{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200707 /* NOTE: the msgb has an allocated length of RTP_BUF_SIZE, so there is
Philipp Maier228e5912019-03-05 13:56:59 +0100708 * plenty of space available to store the slightly larger, converted
709 * data */
Philipp Maier228e5912019-03-05 13:56:59 +0100710 struct rtp_hdr *rtp_hdr;
711 unsigned int payload_len;
712 int rc;
713
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200714 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
715 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 +0200716 return -EINVAL;
717 }
718
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200719 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier228e5912019-03-05 13:56:59 +0100720
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200721 payload_len = msgb_length(msg) - sizeof(struct rtp_hdr);
Philipp Maier228e5912019-03-05 13:56:59 +0100722
723 if (osmo_amr_is_oa(rtp_hdr->data, payload_len)) {
724 if (!target_is_oa)
725 /* Input data is oa an target format is bwe
726 * ==> convert */
727 rc = osmo_amr_oa_to_bwe(rtp_hdr->data, payload_len);
728 else
729 /* Input data is already bew, but we accept it anyway
730 * ==> no conversion needed */
731 rc = payload_len;
732 } else {
733 if (target_is_oa)
734 /* Input data is bwe an target format is oa
735 * ==> convert */
736 rc = osmo_amr_bwe_to_oa(rtp_hdr->data, payload_len,
737 RTP_BUF_SIZE);
738 else
739 /* Input data is already oa, but we accept it anyway
740 * ==> no conversion needed */
741 rc = payload_len;
742 }
743 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200744 LOGPENDP(endp, DRTP, LOGL_ERROR,
745 "AMR RTP packet conversion failed\n");
Philipp Maier228e5912019-03-05 13:56:59 +0100746 return -EINVAL;
747 }
748
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200749 return msgb_trim(msg, rc + sizeof(struct rtp_hdr));
Philipp Maier228e5912019-03-05 13:56:59 +0100750}
751
752/* Check if a conversion between octet-aligned and bandwith-efficient mode is
753 * indicated. */
754static bool amr_oa_bwe_convert_indicated(struct mgcp_rtp_codec *codec)
755{
756 if (codec->param_present == false)
757 return false;
758 if (!codec->param.amr_octet_aligned_present)
759 return false;
760 if (strcmp(codec->subtype_name, "AMR") != 0)
761 return false;
762 return true;
763}
764
765
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200766/* Return whether an RTP packet with AMR payload is in octet-aligned mode.
767 * Return 0 if in bandwidth-efficient mode, 1 for octet-aligned mode, and negative if the RTP data is invalid. */
768static int amr_oa_check(char *data, int len)
Philipp Maier228e5912019-03-05 13:56:59 +0100769{
770 struct rtp_hdr *rtp_hdr;
771 unsigned int payload_len;
772
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200773 if (len < sizeof(struct rtp_hdr))
774 return -EINVAL;
775
Philipp Maier228e5912019-03-05 13:56:59 +0100776 rtp_hdr = (struct rtp_hdr *)data;
777
778 payload_len = len - sizeof(struct rtp_hdr);
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200779 if (payload_len < sizeof(struct amr_hdr))
780 return -EINVAL;
Philipp Maier228e5912019-03-05 13:56:59 +0100781
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200782 return osmo_amr_is_oa(rtp_hdr->data, payload_len) ? 1 : 0;
Philipp Maier228e5912019-03-05 13:56:59 +0100783}
784
Philipp Maier87bd9be2017-08-22 16:35:41 +0200785/* Forward data to a debug tap. This is debug function that is intended for
786 * debugging the voice traffic with tools like gstreamer */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200787static void forward_data(int fd, struct mgcp_rtp_tap *tap, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200788{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100789 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200790
Philipp Maiere6f172d2017-11-07 12:00:01 +0100791 if (!tap->enabled)
792 return;
793
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200794 rc = sendto(fd, msgb_data(msg), msgb_length(msg), 0, (struct sockaddr *)&tap->forward,
Philipp Maiere6f172d2017-11-07 12:00:01 +0100795 sizeof(tap->forward));
796
797 if (rc < 0)
798 LOGP(DRTP, LOGL_ERROR,
799 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200800}
801
Philipp Maier889fe7f2020-07-06 17:44:12 +0200802/* Generate an RTP header if it is missing */
803static void gen_rtp_header(struct msgb *msg, struct mgcp_rtp_end *rtp_end,
804 struct mgcp_rtp_state *state)
805{
806 struct rtp_hdr *hdr = (struct rtp_hdr *)msgb_data(msg);
807
808 if (hdr->version > 0)
809 return;
810
811 hdr->version = 2;
812 hdr->payload_type = rtp_end->codec->payload_type;
813 hdr->timestamp = osmo_htonl(get_current_ts(rtp_end->codec->rate));
814 hdr->sequence = osmo_htons(state->alt_rtp_tx_sequence);
815 hdr->ssrc = state->alt_rtp_tx_ssrc;
816}
817
818
Philipp Maier87bd9be2017-08-22 16:35:41 +0200819/*! Send RTP/RTCP data to a specified destination connection.
Philipp Maier0b79d212020-06-18 12:02:49 +0200820 * \param[in] endp associated endpoint (for configuration, logging).
821 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP.
822 * \param[in] spoofed source address (set to NULL to disable).
823 * \param[in] buf buffer that contains the RTP/RTCP data.
824 * \param[in] len length of the buffer that contains the RTP/RTCP data.
825 * \param[in] conn_src associated source connection.
826 * \param[in] conn_dst associated destination connection.
827 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200828int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200829 struct msgb *msg, struct mgcp_conn_rtp *conn_src,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200830 struct mgcp_conn_rtp *conn_dst)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200831{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200832 /*! When no destination connection is available (e.g. when only one
833 * connection in loopback mode exists), then the source connection
834 * shall be specified as destination connection */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200835
Philipp Maier14b27a82020-06-02 20:15:30 +0200836 struct mgcp_trunk *trunk = endp->trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200837 struct mgcp_rtp_end *rtp_end;
838 struct mgcp_rtp_state *rtp_state;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200839 char *dest_name;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200840 int rc;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200841 int len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200842
Philipp Maier87bd9be2017-08-22 16:35:41 +0200843 OSMO_ASSERT(conn_src);
844 OSMO_ASSERT(conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200845
Philipp Maier87bd9be2017-08-22 16:35:41 +0200846 if (is_rtp) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200847 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTP packet...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200848 } else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200849 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTCP packet...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200850 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200851
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200852 LOGPENDP(endp, DRTP, LOGL_DEBUG, "loop:%d, mode:%d%s\n",
Philipp Maier14b27a82020-06-02 20:15:30 +0200853 trunk->audio_loop, conn_src->conn->mode,
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200854 conn_src->conn->mode == MGCP_CONN_LOOPBACK ? " (loopback)" : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200855
Philipp Maier6931f9a2018-07-26 09:29:31 +0200856 /* FIXME: It is legal that the payload type on the egress connection is
857 * different from the payload type that has been negotiated on the
858 * ingress connection. Essentially the codecs are the same so we can
859 * match them and patch the payload type. However, if we can not find
860 * the codec pendant (everything ist equal except the PT), we are of
861 * course unable to patch the payload type. A situation like this
862 * should not occur if transcoding is consequently avoided. Until
863 * we have transcoding support in osmo-mgw we can not resolve this. */
Philipp Maierda895b12018-08-03 12:16:37 +0200864 if (is_rtp) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200865 rc = mgcp_patch_pt(conn_src, conn_dst, msg);
Philipp Maierda895b12018-08-03 12:16:37 +0200866 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200867 LOGPENDP(endp, DRTP, LOGL_DEBUG,
868 "can not patch PT because no suitable egress codec was found.\n");
Philipp Maierda895b12018-08-03 12:16:37 +0200869 }
Philipp Maier6931f9a2018-07-26 09:29:31 +0200870 }
871
Philipp Maier87bd9be2017-08-22 16:35:41 +0200872 /* Note: In case of loopback configuration, both, the source and the
873 * destination will point to the same connection. */
874 rtp_end = &conn_dst->end;
875 rtp_state = &conn_src->state;
876 dest_name = conn_dst->conn->name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200877
Philipp Maier889fe7f2020-07-06 17:44:12 +0200878 /* Ensure we have an alternative SSRC in case we need it, see also
879 * gen_rtp_header() */
880 if (rtp_state->alt_rtp_tx_ssrc == 0)
881 rtp_state->alt_rtp_tx_ssrc = rand();
882
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200883 if (!rtp_end->output_enabled) {
Harald Weltea48ff4a2020-03-08 14:45:08 +0100884 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_DROPPED_PACKETS_CTR);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200885 LOGPENDP(endp, DRTP, LOGL_DEBUG,
886 "output disabled, drop to %s %s "
887 "rtp_port:%u rtcp_port:%u\n",
888 dest_name,
889 inet_ntoa(rtp_end->addr),
890 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200891 );
892 } else if (is_rtp) {
893 int cont;
894 int nbytes = 0;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200895 int buflen = msgb_length(msg);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200896
897 /* Make sure we have a valid RTP header, in cases where no RTP
898 * header is present, we will generate one. */
899 gen_rtp_header(msg, rtp_end, rtp_state);
900
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200901 do {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200902 /* Run transcoder */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200903 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200904 (char*)msgb_data(msg), &buflen,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200905 RTP_BUF_SIZE);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200906 if (cont < 0)
907 break;
908
Philipp Maier87bd9be2017-08-22 16:35:41 +0200909 if (addr)
910 mgcp_patch_and_count(endp, rtp_state, rtp_end,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200911 addr, msg);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100912
Philipp Maier228e5912019-03-05 13:56:59 +0100913 if (amr_oa_bwe_convert_indicated(conn_dst->end.codec)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200914 rc = amr_oa_bwe_convert(endp, msg,
Neels Hofmeyr5a6220f2019-08-20 03:09:04 +0200915 conn_dst->end.codec->param.amr_octet_aligned);
916 if (rc < 0) {
917 LOGPENDP(endp, DRTP, LOGL_ERROR,
918 "Error in AMR octet-aligned <-> bandwidth-efficient mode conversion\n");
919 break;
920 }
Philipp Maier228e5912019-03-05 13:56:59 +0100921 }
922 else if (rtp_end->rfc5993_hr_convert
Philipp Maier9fc8a022019-02-20 12:26:52 +0100923 && strcmp(conn_src->end.codec->subtype_name,
Neels Hofmeyr5a6220f2019-08-20 03:09:04 +0200924 "GSM-HR-08") == 0) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200925 rc = rfc5993_hr_convert(endp, msg);
Neels Hofmeyr5a6220f2019-08-20 03:09:04 +0200926 if (rc < 0) {
927 LOGPENDP(endp, DRTP, LOGL_ERROR, "Error while converting to GSM-HR-08\n");
928 break;
929 }
930 }
Philipp Maier9fc8a022019-02-20 12:26:52 +0100931
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200932 LOGPENDP(endp, DRTP, LOGL_DEBUG,
933 "process/send to %s %s "
934 "rtp_port:%u rtcp_port:%u\n",
935 dest_name, inet_ntoa(rtp_end->addr),
936 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
937 );
Philipp Maier87bd9be2017-08-22 16:35:41 +0200938
939 /* Forward a copy of the RTP data to a debug ip/port */
940 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200941 msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200942
943 /* FIXME: HACK HACK HACK. See OS#2459.
944 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
945 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
946 * cells (as long as we patch only the first RTP payload in each stream).
947 */
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200948 if (!rtp_state->patched_first_rtp_payload
949 && conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200950 uint8_t *data = msgb_data(msg) + 12;
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200951 if (data[0] == 0xe0) {
952 data[0] = 0xe4;
953 data[1] = 0x00;
954 rtp_state->patched_first_rtp_payload = true;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200955 LOGPENDP(endp, DRTP, LOGL_DEBUG,
956 "Patching over first two bytes"
957 " to fake an IuUP Initialization Ack\n");
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200958 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200959 }
960
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200961 len = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr, rtp_end->rtp_port,
962 (char*)msgb_data(msg), msgb_length(msg));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200963
Philipp Maier87bd9be2017-08-22 16:35:41 +0200964 if (len <= 0)
965 return len;
966
Harald Weltea48ff4a2020-03-08 14:45:08 +0100967 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
968 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200969 rtp_state->alt_rtp_tx_sequence++;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200970
971 nbytes += len;
972 buflen = cont;
973 } while (buflen > 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200974 return nbytes;
Philipp Maier14b27a82020-06-02 20:15:30 +0200975 } else if (!trunk->omit_rtcp) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200976 LOGPENDP(endp, DRTP, LOGL_DEBUG,
977 "send to %s %s rtp_port:%u rtcp_port:%u\n",
978 dest_name, inet_ntoa(rtp_end->addr),
979 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
980 );
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200981
Philipp Maier87bd9be2017-08-22 16:35:41 +0200982 len = mgcp_udp_send(rtp_end->rtcp.fd,
983 &rtp_end->addr,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200984 rtp_end->rtcp_port, (char*)msgb_data(msg), msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200985
Harald Weltea48ff4a2020-03-08 14:45:08 +0100986 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
987 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200988 rtp_state->alt_rtp_tx_sequence++;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200989
990 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200991 }
992
993 return 0;
994}
995
Philipp Maier87bd9be2017-08-22 16:35:41 +0200996/* Check if the origin (addr) matches the address/port data of the RTP
997 * connections. */
998static int check_rtp_origin(struct mgcp_conn_rtp *conn,
999 struct sockaddr_in *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001000{
Harald Weltec26b6652018-10-21 12:01:04 +02001001 if (conn->end.addr.s_addr == 0) {
Neels Hofmeyrefd645e2018-09-10 13:14:50 +02001002 switch (conn->conn->mode) {
1003 case MGCP_CONN_LOOPBACK:
1004 /* HACK: for IuUP, we want to reply with an IuUP Initialization ACK upon the first RTP
1005 * message received. We currently hackishly accomplish that by putting the endpoint in
1006 * loopback mode and patching over the looped back RTP message to make it look like an
1007 * ack. We don't know the femto cell's IP address and port until the RAB Assignment
1008 * Response is received, but the nano3G expects an IuUP Initialization Ack before it even
1009 * sends the RAB Assignment Response. Hence, if the remote address is 0.0.0.0 and the
1010 * MGCP port is in loopback mode, allow looping back the packet to any source. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001011 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1012 "In loopback mode and remote address not set:"
1013 " allowing data from address: %s\n", inet_ntoa(addr->sin_addr));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +02001014 return 0;
1015
1016 default:
1017 /* Receiving early media before the endpoint is configured. Instead of logging
1018 * this as an error that occurs on every call, keep it more low profile to not
1019 * confuse humans with expected errors. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001020 LOGPCONN(conn->conn, DRTP, LOGL_INFO,
1021 "Rx RTP from %s, but remote address not set:"
1022 " dropping early media\n", inet_ntoa(addr->sin_addr));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +02001023 return -1;
1024 }
Neels Hofmeyr0063ca22018-07-23 18:12:16 +02001025 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001026
Philipp Maier87bd9be2017-08-22 16:35:41 +02001027 /* Note: Check if the inbound RTP data comes from the same host to
1028 * which we send our outgoing RTP traffic. */
Harald Weltec26b6652018-10-21 12:01:04 +02001029 if (conn->end.addr.s_addr != addr->sin_addr.s_addr) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001030 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1031 "data from wrong address: %s, ", inet_ntoa(addr->sin_addr));
Philipp Maierc3413882017-10-27 12:26:54 +02001032 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001033 inet_ntoa(conn->end.addr));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001034 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001035 return -1;
1036 }
1037
Philipp Maier87bd9be2017-08-22 16:35:41 +02001038 /* Note: Usually the remote remote port of the data we receive will be
1039 * the same as the remote port where we transmit outgoing RTP traffic
1040 * to (set by MDCX). We use this to check the origin of the data for
1041 * plausibility. */
1042 if (conn->end.rtp_port != addr->sin_port &&
1043 conn->end.rtcp_port != addr->sin_port) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001044 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1045 "data from wrong source port: %d, ", ntohs(addr->sin_port));
Philipp Maierc3413882017-10-27 12:26:54 +02001046 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001047 "expected: %d for RTP or %d for RTCP\n",
1048 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001049 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001050 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001051 }
1052
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001053 return 0;
1054}
1055
Philipp Maier87bd9be2017-08-22 16:35:41 +02001056/* Check the if the destination address configuration of an RTP connection
1057 * makes sense */
1058static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001059{
Philipp Maiere6df0e42018-05-29 14:03:06 +02001060 /* Note: it is legal to create a connection but never setting a port
1061 * and IP-address for outgoing data. */
1062 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0 && conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001063 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
1064 "destination IP-address and rtp port is (not yet) known (%s:%u)\n",
1065 inet_ntoa(conn->end.addr), conn->end.rtp_port);
Philipp Maiere6df0e42018-05-29 14:03:06 +02001066 return -1;
1067 }
1068
Philipp Maier87bd9be2017-08-22 16:35:41 +02001069 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001070 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1071 "destination IP-address is invalid (%s:%u)\n",
1072 inet_ntoa(conn->end.addr), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001073 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001074 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001075
1076 if (conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001077 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1078 "destination rtp port is invalid (%s:%u)\n",
1079 inet_ntoa(conn->end.addr), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001080 return -1;
1081 }
1082
1083 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001084}
1085
Philipp Maier4dba7692018-08-03 12:20:52 +02001086/* Do some basic checks to make sure that the RTCP packets we are going to
1087 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001088static int check_rtcp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +02001089{
1090 struct rtcp_hdr *hdr;
1091 unsigned int len;
1092 uint8_t type;
1093
1094 /* RTPC packets that are just a header without data do not make
1095 * any sense. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001096 if (msgb_length(msg) < sizeof(struct rtcp_hdr)) {
1097 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP packet too short (%u < %zu)\n",
1098 msgb_length(msg), sizeof(struct rtcp_hdr));
Philipp Maier4dba7692018-08-03 12:20:52 +02001099 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001100 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001101
1102 /* Make sure that the length of the received packet does not exceed
1103 * the available buffer size */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001104 hdr = (struct rtcp_hdr *)msgb_data(msg);
Philipp Maier4dba7692018-08-03 12:20:52 +02001105 len = (osmo_ntohs(hdr->length) + 1) * 4;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001106 if (len > msgb_length(msg)) {
1107 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header length exceeds packet size (%u > %u)\n",
1108 len, msgb_length(msg));
Philipp Maier4dba7692018-08-03 12:20:52 +02001109 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001110 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001111
1112 /* Make sure we accept only packets that have a proper packet type set
1113 * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
1114 type = hdr->type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001115 if ((type < 192 || type > 195) && (type < 200 || type > 213)) {
1116 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header: invalid type: %u\n", type);
Philipp Maier4dba7692018-08-03 12:20:52 +02001117 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001118 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001119
1120 return 0;
1121}
1122
1123/* Do some basic checks to make sure that the RTP packets we are going to
1124 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001125static int check_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +02001126{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001127 size_t min_size = sizeof(struct rtp_hdr);
1128 if (msgb_length(msg) < min_size) {
1129 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
1130 msgb_length(msg), min_size);
1131 return -1;
1132 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001133
1134 /* FIXME: Add more checks, the reason why we do not check more than
1135 * the length is because we currently handle IUUP packets as RTP
1136 * packets, so they must pass this check, if we weould be more
1137 * strict here, we would possibly break 3G. (see also FIXME note
1138 * below */
1139
1140 return 0;
1141}
1142
Philipp Maier87bd9be2017-08-22 16:35:41 +02001143/* Send RTP data. Possible options are standard RTP packet
1144 * transmission or trsmission via an osmux connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001145static int mgcp_send_rtp(struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001146{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001147 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1148 enum rtp_proto proto = mc->proto;
1149 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1150 struct sockaddr_in *from_addr = mc->from_addr;
1151 struct mgcp_endpoint *endp = conn_src->conn->endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001152
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001153 LOGPENDP(endp, DRTP, LOGL_DEBUG, "destin conn:%s\n",
1154 mgcp_conn_dump(conn_dst->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001155
1156 /* Before we try to deliver the packet, we check if the destination
1157 * port and IP-Address make sense at all. If not, we will be unable
1158 * to deliver the packet. */
1159 if (check_rtp_destin(conn_dst) != 0)
1160 return -1;
1161
1162 /* Depending on the RTP connection type, deliver the RTP packet to the
1163 * destination connection. */
1164 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001165 case MGCP_RTP_DEFAULT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001166 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1167 "endpoint type is MGCP_RTP_DEFAULT, "
1168 "using mgcp_send() to forward data directly\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001169 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001170 from_addr, msg, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001171 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001172 case MGCP_OSMUX_BSC:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001173 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1174 "endpoint type is MGCP_OSMUX_BSC_NAT, "
1175 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n");
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001176 return osmux_xfrm_to_osmux((char*)msgb_data(msg), msgb_length(msg), conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001177 }
1178
Philipp Maier87bd9be2017-08-22 16:35:41 +02001179 /* If the data has not been handled/forwarded until here, it will
1180 * be discarded, this should not happen, normally the MGCP type
1181 * should be properly set */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001182 LOGPENDP(endp, DRTP, LOGL_ERROR, "bad MGCP type -- data discarded!\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001183
1184 return -1;
1185}
1186
1187/*! dispatch incoming RTP packet to opposite RTP connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001188 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1189 * \param[in] addr socket address where the RTP packet has been received from.
1190 * \param[in] buf buffer that hold the RTP payload.
1191 * \param[in] buf_size size data length of buf.
1192 * \param[in] conn originating connection.
1193 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001194int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001195{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001196 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1197 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1198 struct mgcp_conn *conn = conn_src->conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001199 struct mgcp_conn *conn_dst;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001200 struct sockaddr_in *from_addr = mc->from_addr;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001201
1202 /*! NOTE: This callback function implements the endpoint specific
Alexander Chemeris61cf9bb2020-05-11 18:13:53 +03001203 * dispatch behaviour of an rtp bridge/proxy endpoint. It is assumed
Philipp Maier87bd9be2017-08-22 16:35:41 +02001204 * that the endpoint will hold only two connections. This premise
1205 * is used to determine the opposite connection (it is always the
1206 * connection that is not the originating connection). Once the
1207 * destination connection is known the RTP packet is sent via
1208 * the destination connection. */
1209
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001210
1211 /* Check if the connection is in loopback mode, if yes, just send the
1212 * incoming data back to the origin */
1213 if (conn->mode == MGCP_CONN_LOOPBACK) {
1214 /* When we are in loopback mode, we loop back all incoming
1215 * packets back to their origin. We will use the originating
1216 * address data from the UDP packet header to patch the
1217 * outgoing address in connection on the fly */
1218 if (conn->u.rtp.end.rtp_port == 0) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001219 conn->u.rtp.end.addr = from_addr->sin_addr;
1220 conn->u.rtp.end.rtp_port = from_addr->sin_port;
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001221 }
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001222 return mgcp_send_rtp(conn_src, msg);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001223 }
1224
Philipp Maier87bd9be2017-08-22 16:35:41 +02001225 /* Find a destination connection. */
1226 /* NOTE: This code path runs every time an RTP packet is received. The
1227 * function mgcp_find_dst_conn() we use to determine the detination
1228 * connection will iterate the connection list inside the endpoint.
1229 * Since list iterations are quite costly, we will figure out the
1230 * destination only once and use the optional private data pointer of
1231 * the connection to cache the destination connection pointer. */
1232 if (!conn->priv) {
1233 conn_dst = mgcp_find_dst_conn(conn);
1234 conn->priv = conn_dst;
1235 } else {
1236 conn_dst = (struct mgcp_conn *)conn->priv;
1237 }
1238
1239 /* There is no destination conn, stop here */
1240 if (!conn_dst) {
Alexander Chemerisebb9bf32020-05-11 18:14:31 +03001241 LOGPCONN(conn, DRTP, LOGL_DEBUG,
1242 "no connection to forward an incoming RTP packet to\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001243 return -1;
1244 }
1245
1246 /* The destination conn is not an RTP connection */
1247 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001248 LOGPCONN(conn, DRTP, LOGL_ERROR,
1249 "unable to find suitable destination conn\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001250 return -1;
1251 }
1252
1253 /* Dispatch RTP packet to destination RTP connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001254 return mgcp_send_rtp(&conn_dst->u.rtp, msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001255}
1256
Philipp Maier0996a1e2020-06-10 15:27:14 +02001257/*! dispatch incoming RTP packet to E1 subslot, handle RTCP packets locally.
1258 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1259 * \param[in] addr socket address where the RTP packet has been received from.
1260 * \param[in] buf buffer that hold the RTP payload.
1261 * \param[in] buf_size size data length of buf.
1262 * \param[in] conn originating connection.
1263 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001264int mgcp_dispatch_e1_bridge_cb(struct msgb *msg)
Philipp Maier0996a1e2020-06-10 15:27:14 +02001265{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001266 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1267 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1268 struct mgcp_conn *conn = conn_src->conn;
Philipp Maier889fe7f2020-07-06 17:44:12 +02001269 struct sockaddr_in *from_addr = mc->from_addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001270
Philipp Maier889fe7f2020-07-06 17:44:12 +02001271 /* Check if the connection is in loopback mode, if yes, just send the
1272 * incoming data back to the origin */
1273 if (conn->mode == MGCP_CONN_LOOPBACK) {
1274 /* When we are in loopback mode, we loop back all incoming
1275 * packets back to their origin. We will use the originating
1276 * address data from the UDP packet header to patch the
1277 * outgoing address in connection on the fly */
1278 if (conn->u.rtp.end.rtp_port == 0) {
1279 conn->u.rtp.end.addr = from_addr->sin_addr;
1280 conn->u.rtp.end.rtp_port = from_addr->sin_port;
1281 }
1282 return mgcp_send_rtp(conn_src, msg);
1283 }
1284
1285 /* Forward to E1 */
1286 return mgcp_e1_send_rtp(conn->endp, conn->u.rtp.end.codec, msg);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001287}
1288
Philipp Maierdf5d2192018-01-24 11:39:32 +01001289/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1290 * \param[in] endp Endpoint on which the connection resides.
Philipp Maier08eb9352020-06-18 11:55:35 +02001291 * \param[in] conn Connection that is about to be removed (ignored). */
Philipp Maierdf5d2192018-01-24 11:39:32 +01001292void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1293{
1294 struct mgcp_conn *conn_cleanup;
1295
1296 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1297 * pointer to the destination connection, so that we do not have
1298 * to go through the list every time an RTP packet arrives. To prevent
1299 * a use-after-free situation we invalidate this information for all
1300 * connections present when one connection is removed from the
1301 * endpoint. */
1302 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
1303 conn_cleanup->priv = NULL;
1304 }
1305}
1306
Philipp Maier0996a1e2020-06-10 15:27:14 +02001307/*! cleanup an endpoint when a connection on an E1 endpoint is removed.
1308 * \param[in] endp Endpoint on which the connection resides.
1309 * \param[in] conn Connection that is about to be removed (ignored). */
1310void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1311{
Philipp Maier889fe7f2020-07-06 17:44:12 +02001312 /* Cleanup tasks for E1 are the same as for regular endpoint. The
1313 * shut down of the E1 part is handled separately. */
1314 mgcp_cleanup_rtp_bridge_cb(endp, conn);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001315}
1316
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001317static bool is_dummy_msg(enum rtp_proto proto, struct msgb *msg)
1318{
1319 return msgb_length(msg) == 1 && msgb_data(msg)[0] == MGCP_DUMMY_LOAD;
1320}
1321
Philipp Maier87bd9be2017-08-22 16:35:41 +02001322/* Handle incoming RTP data from NET */
1323static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1324{
1325 /* NOTE: This is a generic implementation. RTP data is received. In
1326 * case of loopback the data is just sent back to its origin. All
1327 * other cases implement endpoint specific behaviour (e.g. how is the
1328 * destination connection determined?). That specific behaviour is
1329 * implemented by the callback function that is called at the end of
1330 * the function */
1331
1332 struct mgcp_conn_rtp *conn_src;
1333 struct mgcp_endpoint *endp;
1334 struct sockaddr_in addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001335 socklen_t slen = sizeof(addr);
1336 int ret;
1337 enum rtp_proto proto;
1338 struct osmo_rtp_msg_ctx *mc;
1339 struct msgb *msg = msgb_alloc(RTP_BUF_SIZE, "RTP-rx");
1340 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001341
1342 conn_src = (struct mgcp_conn_rtp *)fd->data;
1343 OSMO_ASSERT(conn_src);
1344 endp = conn_src->conn->endp;
1345 OSMO_ASSERT(endp);
1346
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001347 proto = (fd == &conn_src->end.rtp)? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001348
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001349 ret = recvfrom(fd->fd, msgb_data(msg), msg->data_len, 0, (struct sockaddr *)&addr, &slen);
1350
1351 if (ret <= 0) {
1352 LOG_CONN_RTP(conn_src, LOGL_ERROR, "recvfrom error: %s\n", strerror(errno));
1353 rc = -1;
1354 goto out;
1355 }
1356
1357 msgb_put(msg, ret);
1358
1359 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "%s: rx %u bytes from %s:%u\n",
1360 proto == MGCP_PROTO_RTP ? "RTP" : "RTPC",
1361 msgb_length(msg), inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
1362
1363 if ((proto == MGCP_PROTO_RTP && check_rtp(conn_src, msg))
1364 || (proto == MGCP_PROTO_RTCP && check_rtcp(conn_src, msg))) {
1365 /* Logging happened in the two check_ functions */
1366 rc = -1;
1367 goto out;
1368 }
1369
1370 if (is_dummy_msg(proto, msg)) {
1371 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx dummy packet (dropped)\n");
1372 rc = 0;
1373 goto out;
1374 }
1375
1376 /* Since the msgb remains owned and freed by this function, the msg ctx data struct can just be on the stack and
1377 * needs not be allocated with the msgb. */
1378 mc = OSMO_RTP_MSG_CTX(msg);
1379 *mc = (struct osmo_rtp_msg_ctx){
1380 .proto = proto,
1381 .conn_src = conn_src,
1382 .from_addr = &addr,
1383 };
1384 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "msg ctx: %d %p %s\n",
1385 mc->proto, mc->conn_src,
1386 osmo_hexdump((void*)mc->from_addr, sizeof(struct sockaddr_in)));
1387
1388 /* Increment RX statistics */
1389 rate_ctr_inc(&conn_src->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR]);
1390 rate_ctr_add(&conn_src->rate_ctr_group->ctr[RTP_OCTETS_RX_CTR], msgb_length(msg));
1391 /* FIXME: count RTP and RTCP separately, also count IuUP payload-less separately */
1392
1393 /* Forward a copy of the RTP data to a debug ip/port */
1394 forward_data(fd->fd, &conn_src->tap_in, msg);
1395
1396 rc = rx_rtp(msg);
1397
1398out:
1399 msgb_free(msg);
1400 return rc;
1401}
1402
1403static int rx_rtp(struct msgb *msg)
1404{
1405 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1406 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1407 struct sockaddr_in *from_addr = mc->from_addr;
1408 struct mgcp_conn *conn = conn_src->conn;
1409 struct mgcp_trunk *trunk = conn->endp->trunk;
1410
1411 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx_rtp(%u bytes)\n", msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001412
Oliver Smithe36b7752019-01-22 16:31:36 +01001413 mgcp_conn_watchdog_kick(conn_src->conn);
1414
Philipp Maier228e5912019-03-05 13:56:59 +01001415 /* If AMR is configured for the ingress connection a conversion of the
1416 * framing mode (octet-aligned vs. bandwith-efficient is explicitly
1417 * define, then we check if the incoming payload matches that
1418 * expectation. */
1419 if (amr_oa_bwe_convert_indicated(conn_src->end.codec)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001420 int oa = amr_oa_check((char*)msgb_data(msg), msgb_length(msg));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +02001421 if (oa < 0)
1422 return -1;
1423 if (((bool)oa) != conn_src->end.codec->param.amr_octet_aligned)
Philipp Maier228e5912019-03-05 13:56:59 +01001424 return -1;
1425 }
1426
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001427 /* Check if the origin of the RTP packet seems plausible */
1428 if (!trunk->rtp_accept_all && check_rtp_origin(conn_src, from_addr))
1429 return -1;
1430
Philipp Maier87bd9be2017-08-22 16:35:41 +02001431 /* Execute endpoint specific implementation that handles the
1432 * dispatching of the RTP data */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001433 return conn->endp->type->dispatch_rtp_cb(msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001434}
1435
1436/*! set IP Type of Service parameter.
Philipp Maier0b79d212020-06-18 12:02:49 +02001437 * \param[in] fd associated file descriptor.
1438 * \param[in] tos dscp value.
1439 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001440int mgcp_set_ip_tos(int fd, int tos)
1441{
1442 int ret;
1443 ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1444
1445 if (ret < 0)
1446 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001447 return 0;
1448}
1449
Philipp Maier87bd9be2017-08-22 16:35:41 +02001450/*! bind RTP port to osmo_fd.
Philipp Maier0b79d212020-06-18 12:02:49 +02001451 * \param[in] source_addr source (local) address to bind on.
1452 * \param[in] fd associated file descriptor.
1453 * \param[in] port to bind on.
1454 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001455int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
1456{
Harald Welte8890dfa2017-11-17 15:09:30 +01001457 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001458
Harald Welte8890dfa2017-11-17 15:09:30 +01001459 rc = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
1460 NULL, 0, OSMO_SOCK_F_BIND);
1461 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001462 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001463 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001464 return -1;
1465 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001466 fd->fd = rc;
1467 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001468
1469 return 0;
1470}
1471
Philipp Maier87bd9be2017-08-22 16:35:41 +02001472/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001473static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001474 struct mgcp_rtp_end *rtp_end, struct mgcp_endpoint *endp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001475{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001476 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1477 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1478
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001479 if (mgcp_create_bind(source_addr, &rtp_end->rtp,
1480 rtp_end->local_port) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001481 LOGPENDP(endp, DRTP, LOGL_ERROR,
1482 "failed to create RTP port: %s:%d\n",
1483 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001484 goto cleanup0;
1485 }
1486
1487 if (mgcp_create_bind(source_addr, &rtp_end->rtcp,
1488 rtp_end->local_port + 1) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001489 LOGPENDP(endp, DRTP, LOGL_ERROR,
1490 "failed to create RTCP port: %s:%d\n",
1491 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001492 goto cleanup1;
1493 }
1494
Philipp Maier87bd9be2017-08-22 16:35:41 +02001495 /* Set Type of Service (DSCP-Value) as configured via VTY */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001496 mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
1497 mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
1498
Pau Espin Pedrola7152e02020-05-09 19:15:50 +02001499 rtp_end->rtp.when = OSMO_FD_READ;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001500 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001501 LOGPENDP(endp, DRTP, LOGL_ERROR,
1502 "failed to register RTP port %d\n",
1503 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001504 goto cleanup2;
1505 }
1506
Pau Espin Pedrola7152e02020-05-09 19:15:50 +02001507 rtp_end->rtcp.when = OSMO_FD_READ;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001508 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001509 LOGPENDP(endp, DRTP, LOGL_ERROR,
1510 "failed to register RTCP port %d\n",
1511 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001512 goto cleanup3;
1513 }
1514
1515 return 0;
1516
1517cleanup3:
1518 osmo_fd_unregister(&rtp_end->rtp);
1519cleanup2:
1520 close(rtp_end->rtcp.fd);
1521 rtp_end->rtcp.fd = -1;
1522cleanup1:
1523 close(rtp_end->rtp.fd);
1524 rtp_end->rtp.fd = -1;
1525cleanup0:
1526 return -1;
1527}
1528
Philipp Maier87bd9be2017-08-22 16:35:41 +02001529/*! bind RTP port to endpoint/connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001530 * \param[in] endp endpoint that holds the RTP connection.
1531 * \param[in] rtp_port port number to bind on.
1532 * \param[in] conn associated RTP connection.
1533 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001534int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1535 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001536{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001537 char name[512];
1538 struct mgcp_rtp_end *end;
Philipp Maier1cb1e382017-11-02 17:16:04 +01001539 char local_ip_addr[INET_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001540
Philipp Maier01d24a32017-11-21 17:26:09 +01001541 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001542 end = &conn->end;
1543
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001544 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001545 LOGPENDP(endp, DRTP, LOGL_ERROR, "%u was already bound on conn:%s\n",
1546 rtp_port, mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001547
1548 /* Double bindings should never occour! Since we always allocate
1549 * connections dynamically and free them when they are not
1550 * needed anymore, there must be no previous binding leftover.
1551 * Should there be a connection bound twice, we have a serious
1552 * problem and must exit immediately! */
1553 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001554 }
1555
1556 end->local_port = rtp_port;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001557 end->rtp.cb = rtp_data_net;
1558 end->rtp.data = conn;
1559 end->rtcp.data = conn;
1560 end->rtcp.cb = rtp_data_net;
1561
Philipp Maier1cb1e382017-11-02 17:16:04 +01001562 mgcp_get_local_addr(local_ip_addr, conn);
1563
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001564 return bind_rtp(endp->cfg, local_ip_addr, end, endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001565}
1566
Philipp Maier87bd9be2017-08-22 16:35:41 +02001567/*! free allocated RTP and RTCP ports.
1568 * \param[in] end RTP end */
1569void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001570{
1571 if (end->rtp.fd != -1) {
1572 close(end->rtp.fd);
1573 end->rtp.fd = -1;
1574 osmo_fd_unregister(&end->rtp);
1575 }
1576
1577 if (end->rtcp.fd != -1) {
1578 close(end->rtcp.fd);
1579 end->rtcp.fd = -1;
1580 osmo_fd_unregister(&end->rtcp);
1581 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001582}