blob: e0aa42e15eab6be47b15c73a6da132e87f012ec7 [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 Maier87bd9be2017-08-22 16:35:41 +020040#include <osmocom/mgcp/mgcp_internal.h>
41#include <osmocom/mgcp/mgcp_stat.h>
42#include <osmocom/mgcp/osmux.h>
43#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010044#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maierc66ab2c2020-06-02 20:55:34 +020045#include <osmocom/mgcp/mgcp_trunk.h>
Philipp Maier6931f9a2018-07-26 09:29:31 +020046#include <osmocom/mgcp/mgcp_codec.h>
Philipp Maierc3413882017-10-27 12:26:54 +020047#include <osmocom/mgcp/debug.h>
Philipp Maier9fc8a022019-02-20 12:26:52 +010048#include <osmocom/codec/codec.h>
Philipp Maier889fe7f2020-07-06 17:44:12 +020049#include <osmocom/mgcp/mgcp_e1.h>
Philipp Maier6931f9a2018-07-26 09:29:31 +020050
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020051#define RTP_SEQ_MOD (1 << 16)
52#define RTP_MAX_DROPOUT 3000
53#define RTP_MAX_MISORDER 100
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020054
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +020055enum rtp_proto {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020056 MGCP_PROTO_RTP,
57 MGCP_PROTO_RTCP,
58};
59
Harald Weltea48ff4a2020-03-08 14:45:08 +010060static void rtpconn_rate_ctr_add(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp,
61 int id, int inc)
62{
63 struct rate_ctr_group *conn_stats = conn_rtp->rate_ctr_group;
Philipp Maierc66ab2c2020-06-02 20:55:34 +020064 struct rate_ctr_group *mgw_stats = endp->trunk->ratectr.all_rtp_conn_stats;
Harald Weltea48ff4a2020-03-08 14:45:08 +010065
Philipp Maierc66ab2c2020-06-02 20:55:34 +020066 /* add to both the per-connection and the global stats */
Harald Weltea48ff4a2020-03-08 14:45:08 +010067 rate_ctr_add(&conn_stats->ctr[id], inc);
Philipp Maierc66ab2c2020-06-02 20:55:34 +020068 rate_ctr_add(&mgw_stats->ctr[id], inc);
Harald Weltea48ff4a2020-03-08 14:45:08 +010069}
70
71static void rtpconn_rate_ctr_inc(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp, int id)
72{
73 rtpconn_rate_ctr_add(conn_rtp, endp, id, 1);
74}
75
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +020076static int rx_rtp(struct msgb *msg);
77
Philipp Maier1cb1e382017-11-02 17:16:04 +010078/*! Determine the local rtp bind IP-address.
Philipp Maier0b79d212020-06-18 12:02:49 +020079 * \param[out] addr caller provided memory to store the resulting IP-Address.
80 * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters.
Philipp Maier1cb1e382017-11-02 17:16:04 +010081 *
82 * The local bind IP-address is automatically selected by probing the
83 * IP-Address of the interface that is pointing towards the remote IP-Address,
84 * if no remote IP-Address is known yet, the statically configured
85 * IP-Addresses are used as fallback. */
86void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn)
87{
88
89 struct mgcp_endpoint *endp;
90 int rc;
91 endp = conn->conn->endp;
92
93 /* Try probing the local IP-Address */
94 if (endp->cfg->net_ports.bind_addr_probe && conn->end.addr.s_addr != 0) {
95 rc = osmo_sock_local_ip(addr, inet_ntoa(conn->end.addr));
96 if (rc < 0)
Pau Espin Pedrol3239f622019-04-24 18:47:46 +020097 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
98 "local interface auto detection failed, using configured addresses...\n");
Philipp Maier1cb1e382017-11-02 17:16:04 +010099 else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200100 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
101 "selected local rtp bind ip %s by probing using remote ip %s\n",
102 addr, inet_ntoa(conn->end.addr));
Philipp Maier1cb1e382017-11-02 17:16:04 +0100103 return;
104 }
105 }
106
Pau Espin Pedrola93c6e92019-05-06 15:23:57 +0200107 /* Select from preconfigured IP-Addresses. We don't have bind_addr for Osmux (yet?). */
Philipp Maier1cb1e382017-11-02 17:16:04 +0100108 if (endp->cfg->net_ports.bind_addr) {
109 /* Check there is a bind IP for the RTP traffic configured,
110 * if so, use that IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100111 osmo_strlcpy(addr, endp->cfg->net_ports.bind_addr, INET_ADDRSTRLEN);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200112 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
113 "using configured rtp bind ip as local bind ip %s\n",
114 addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100115 } else {
116 /* No specific bind IP is configured for the RTP traffic, so
117 * assume the IP where we listen for incoming MGCP messages
118 * as bind IP */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100119 osmo_strlcpy(addr, endp->cfg->source_addr, INET_ADDRSTRLEN);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200120 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
121 "using mgcp bind ip as local rtp bind ip: %s\n", addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100122 }
123}
124
Philipp Maier87bd9be2017-08-22 16:35:41 +0200125/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200126 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +0200127 * 1/codec_rate seconds. */
128static uint32_t get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200129{
130 struct timespec tp;
131 uint64_t ret;
132
Philipp Maier87bd9be2017-08-22 16:35:41 +0200133 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200134 return 0;
135
136 memset(&tp, 0, sizeof(tp));
137 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maierc3413882017-10-27 12:26:54 +0200138 LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200139
140 /* convert it to 1/unit seconds */
141 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200142 ret *= codec_rate;
143 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200144
145 return ret;
146}
147
Philipp Maier87bd9be2017-08-22 16:35:41 +0200148/*! send udp packet.
Philipp Maier0b79d212020-06-18 12:02:49 +0200149 * \param[in] fd associated file descriptor.
150 * \param[in] addr destination ip-address.
151 * \param[in] port destination UDP port (network byte order).
152 * \param[in] buf buffer that holds the data to be send.
153 * \param[in] len length of the data to be sent.
154 * \returns bytes sent, -1 on error. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200155int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
156{
157 struct sockaddr_in out;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200158
Philipp Maierc3413882017-10-27 12:26:54 +0200159 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200160 "sending %i bytes length packet to %s:%u ...\n",
161 len, inet_ntoa(*addr), ntohs(port));
162
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200163 out.sin_family = AF_INET;
164 out.sin_port = port;
165 memcpy(&out.sin_addr, addr, sizeof(*addr));
166
167 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
168}
169
Philipp Maier87bd9be2017-08-22 16:35:41 +0200170/*! send RTP dummy packet (to keep NAT connection open).
Philipp Maier0b79d212020-06-18 12:02:49 +0200171 * \param[in] endp mcgp endpoint that holds the RTP connection.
172 * \param[in] conn associated RTP connection.
173 * \returns bytes sent, -1 on error. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200174int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200175{
176 static char buf[] = { MGCP_DUMMY_LOAD };
177 int rc;
178 int was_rtcp = 0;
179
Philipp Maier87bd9be2017-08-22 16:35:41 +0200180 OSMO_ASSERT(endp);
181 OSMO_ASSERT(conn);
182
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200183 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,"sending dummy packet... %s\n",
184 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200185
186 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
187 conn->end.rtp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200188
189 if (rc == -1)
190 goto failed;
191
Philipp Maier14b27a82020-06-02 20:15:30 +0200192 if (endp->trunk->omit_rtcp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200193 return rc;
194
195 was_rtcp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200196 rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
197 conn->end.rtcp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200198
199 if (rc >= 0)
200 return rc;
201
202failed:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200203 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
204 "Failed to send dummy %s packet.\n",
205 was_rtcp ? "RTCP" : "RTP");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200206
207 return -1;
208}
209
Philipp Maier87bd9be2017-08-22 16:35:41 +0200210/* Compute timestamp alignment error */
211static int32_t ts_alignment_error(struct mgcp_rtp_stream_state *sstate,
212 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200213{
214 int32_t timestamp_delta;
215
216 if (ptime == 0)
217 return 0;
218
219 /* Align according to: T - Tlast = k * Tptime */
220 timestamp_delta = timestamp - sstate->last_timestamp;
221
222 return timestamp_delta % ptime;
223}
224
Philipp Maier87bd9be2017-08-22 16:35:41 +0200225/* Check timestamp and sequence number for plausibility */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200226static int check_rtp_timestamp(struct mgcp_endpoint *endp,
227 struct mgcp_rtp_state *state,
228 struct mgcp_rtp_stream_state *sstate,
229 struct mgcp_rtp_end *rtp_end,
230 struct sockaddr_in *addr,
231 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200232 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200233{
234 int32_t tsdelta;
235 int32_t timestamp_error;
236
237 /* Not fully intialized, skip */
238 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
239 return 0;
240
241 if (seq == sstate->last_seq) {
242 if (timestamp != sstate->last_timestamp) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200243 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200244 LOGPENDP(endp, DRTP, LOGL_ERROR,
245 "The %s timestamp delta is != 0 but the sequence "
246 "number %d is the same, "
247 "TS offset: %d, SeqNo offset: %d "
248 "on SSRC: %u timestamp: %u "
249 "from %s:%d\n",
250 text, seq,
251 state->patch.timestamp_offset, state->patch.seq_offset,
252 sstate->ssrc, timestamp, inet_ntoa(addr->sin_addr),
253 ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200254 }
255 return 0;
256 }
257
258 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200259 (int32_t)(timestamp - sstate->last_timestamp) /
260 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200261
262 if (tsdelta == 0) {
263 /* Don't update *tsdelta_out */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200264 LOGPENDP(endp, DRTP, LOGL_NOTICE,
265 "The %s timestamp delta is %d "
266 "on SSRC: %u timestamp: %u "
267 "from %s:%d\n",
268 text, tsdelta,
269 sstate->ssrc, timestamp,
270 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200271
272 return 0;
273 }
274
275 if (sstate->last_tsdelta != tsdelta) {
276 if (sstate->last_tsdelta) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200277 LOGPENDP(endp, DRTP, LOGL_INFO,
278 "The %s timestamp delta changes from %d to %d "
279 "on SSRC: %u timestamp: %u from %s:%d\n",
280 text, sstate->last_tsdelta, tsdelta,
281 sstate->ssrc, timestamp,
282 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200283 }
284 }
285
286 if (tsdelta_out)
287 *tsdelta_out = tsdelta;
288
289 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200290 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200291
292 if (timestamp_error) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200293 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200294 LOGPENDP(endp, DRTP, LOGL_NOTICE,
295 "The %s timestamp has an alignment error of %d "
296 "on SSRC: %u "
297 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
298 "from %s:%d. ptime: %d\n",
299 text, timestamp_error,
300 sstate->ssrc,
301 (int16_t)(seq - sstate->last_seq),
302 (int32_t)(timestamp - sstate->last_timestamp),
303 tsdelta,
304 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
305 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200306 }
307 return 1;
308}
309
310/* Set the timestamp offset according to the packet duration. */
311static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp,
312 struct mgcp_rtp_state *state,
313 struct mgcp_rtp_end *rtp_end,
314 struct sockaddr_in *addr,
315 int16_t delta_seq, uint32_t in_timestamp)
316{
317 int32_t tsdelta = state->packet_duration;
318 int timestamp_offset;
319 uint32_t out_timestamp;
320
321 if (tsdelta == 0) {
322 tsdelta = state->out_stream.last_tsdelta;
323 if (tsdelta != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200324 LOGPENDP(endp, DRTP, LOGL_NOTICE,
325 "A fixed packet duration is not available, "
326 "using last output timestamp delta instead: %d "
327 "from %s:%d\n", tsdelta,
328 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200329 } else {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200330 tsdelta = rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200331 LOGPENDP(endp, DRTP, LOGL_NOTICE,
332 "Fixed packet duration and last timestamp delta "
333 "are not available, "
334 "using fixed 20ms instead: %d "
335 "from %s:%d\n", tsdelta,
336 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200337 }
338 }
339
340 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
341 timestamp_offset = out_timestamp - in_timestamp;
342
Harald Welte33381352017-12-25 09:44:26 +0100343 if (state->patch.timestamp_offset != timestamp_offset) {
344 state->patch.timestamp_offset = timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200345
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200346 LOGPENDP(endp, DRTP, LOGL_NOTICE,
347 "Timestamp offset change on SSRC: %u "
348 "SeqNo delta: %d, TS offset: %d, "
349 "from %s:%d\n", state->in_stream.ssrc,
350 delta_seq, state->patch.timestamp_offset,
351 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200352 }
353
354 return timestamp_offset;
355}
356
357/* Set the timestamp offset according to the packet duration. */
358static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp,
359 struct mgcp_rtp_state *state,
360 struct mgcp_rtp_end *rtp_end,
361 struct sockaddr_in *addr,
362 uint32_t timestamp)
363{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200364 int ts_error = 0;
365 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200366 int ptime = state->packet_duration;
367
368 /* Align according to: T + Toffs - Tlast = k * Tptime */
369
Philipp Maier87bd9be2017-08-22 16:35:41 +0200370 ts_error = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100371 timestamp + state->patch.timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200372
Philipp Maier87bd9be2017-08-22 16:35:41 +0200373 /* If there is an alignment error, we have to compensate it */
374 if (ts_error) {
Harald Welte33381352017-12-25 09:44:26 +0100375 state->patch.timestamp_offset += ptime - ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200376
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200377 LOGPENDP(endp, DRTP, LOGL_NOTICE,
378 "Corrected timestamp alignment error of %d on SSRC: %u "
379 "new TS offset: %d, "
380 "from %s:%d\n",
381 ts_error, state->in_stream.ssrc,
382 state->patch.timestamp_offset, inet_ntoa(addr->sin_addr),
383 ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200384 }
385
Philipp Maier87bd9be2017-08-22 16:35:41 +0200386 /* Check we really managed to compensate the timestamp
387 * offset. There should not be any remaining error, failing
Harald Welte1d1b98f2017-12-25 10:03:40 +0100388 * here would point to a serous problem with the alignment
389 * error computation function */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200390 ts_check = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100391 timestamp + state->patch.timestamp_offset);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200392 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200393
Philipp Maier87bd9be2017-08-22 16:35:41 +0200394 /* Return alignment error before compensation */
395 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200396}
397
Philipp Maier87bd9be2017-08-22 16:35:41 +0200398/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200399 * \param[in] associated endpoint.
400 * \param[in] destination RTP end.
401 * \param[in,out] pointer to buffer with voice data.
402 * \param[in] voice data length.
403 * \param[in] maximum size of caller provided voice data buffer.
404 * \returns ignores input parameters, return always 0. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200405int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
406 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200407 char *data, int *len, int buf_size)
408{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200409 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200410 return 0;
411}
412
Philipp Maier87bd9be2017-08-22 16:35:41 +0200413/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200414 * \param[in] associated endpoint.
415 * \param[in] destination RTP connnection.
416 * \param[in] source RTP connection.
417 * \returns ignores input parameters, return always 0. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200418int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
Philipp Maieracc10352018-07-19 18:07:57 +0200419 struct mgcp_conn_rtp *conn_dst,
420 struct mgcp_conn_rtp *conn_src)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200421{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200422 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200423 return 0;
424}
425
426void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
Philipp Maier58128252019-03-06 11:28:18 +0100427 const struct mgcp_rtp_codec **codec,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200428 const char **fmtp_extra,
429 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200430{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200431 LOGPENDP(endp, DRTP, LOGL_DEBUG, "conn:%s using format defaults\n",
432 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200433
Philipp Maier58128252019-03-06 11:28:18 +0100434 *codec = conn->end.codec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200435 *fmtp_extra = conn->end.fmtp_extra;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200436}
437
Philipp Maier87bd9be2017-08-22 16:35:41 +0200438void mgcp_rtp_annex_count(struct mgcp_endpoint *endp,
439 struct mgcp_rtp_state *state, const uint16_t seq,
440 const int32_t transit, const uint32_t ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200441{
442 int32_t d;
443
444 /* initialize or re-initialize */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100445 if (!state->stats.initialized || state->stats.ssrc != ssrc) {
446 state->stats.initialized = 1;
447 state->stats.base_seq = seq;
448 state->stats.max_seq = seq - 1;
449 state->stats.ssrc = ssrc;
450 state->stats.jitter = 0;
451 state->stats.transit = transit;
452 state->stats.cycles = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200453 } else {
454 uint16_t udelta;
455
Philipp Maier87bd9be2017-08-22 16:35:41 +0200456 /* The below takes the shape of the validation of
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200457 * Appendix A. Check if there is something weird with
458 * the sequence number, otherwise check for a wrap
459 * around in the sequence number.
460 * It can't wrap during the initialization so let's
461 * skip it here. The Appendix A probably doesn't have
Philipp Maier87bd9be2017-08-22 16:35:41 +0200462 * this issue because of the probation. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100463 udelta = seq - state->stats.max_seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200464 if (udelta < RTP_MAX_DROPOUT) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100465 if (seq < state->stats.max_seq)
466 state->stats.cycles += RTP_SEQ_MOD;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200467 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200468 LOGPENDP(endp, DRTP, LOGL_NOTICE,
469 "RTP seqno made a very large jump on delta: %u\n",
470 udelta);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200471 }
472 }
473
Philipp Maier87bd9be2017-08-22 16:35:41 +0200474 /* Calculate the jitter between the two packages. The TS should be
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200475 * taken closer to the read function. This was taken from the
476 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
Philipp Maier87bd9be2017-08-22 16:35:41 +0200477 * resolution. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100478 d = transit - state->stats.transit;
479 state->stats.transit = transit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200480 if (d < 0)
481 d = -d;
Harald Welte49e3d5a2017-12-25 09:47:57 +0100482 state->stats.jitter += d - ((state->stats.jitter + 8) >> 4);
483 state->stats.max_seq = seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200484}
485
Philipp Maier6931f9a2018-07-26 09:29:31 +0200486/* There may be different payload type numbers negotiated for two connections.
487 * Patch the payload type of an RTP packet so that it uses the payload type
488 * that is valid for the destination connection (conn_dst) */
489static int mgcp_patch_pt(struct mgcp_conn_rtp *conn_src,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200490 struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200491{
492 struct rtp_hdr *rtp_hdr;
493 uint8_t pt_in;
494 int pt_out;
495
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200496 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
497 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
498 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200499 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200500 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200501
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200502 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier6931f9a2018-07-26 09:29:31 +0200503
504 pt_in = rtp_hdr->payload_type;
505 pt_out = mgcp_codec_pt_translate(conn_src, conn_dst, pt_in);
506 if (pt_out < 0)
507 return -EINVAL;
508
509 rtp_hdr->payload_type = (uint8_t) pt_out;
510 return 0;
511}
512
Philipp Maier87bd9be2017-08-22 16:35:41 +0200513/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200514 * some of the supported endpoints (e.g. the nanoBTS) can only handle
515 * one source and this code will patch RTP header to appear as if there
516 * is only one source.
517 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200518 * we receive will be seen as a switch in streams. */
519void mgcp_patch_and_count(struct mgcp_endpoint *endp,
520 struct mgcp_rtp_state *state,
521 struct mgcp_rtp_end *rtp_end,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200522 struct sockaddr_in *addr, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200523{
524 uint32_t arrival_time;
525 int32_t transit;
526 uint16_t seq;
527 uint32_t timestamp, ssrc;
528 struct rtp_hdr *rtp_hdr;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200529 int payload = rtp_end->codec->payload_type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200530 unsigned int len = msgb_length(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200531
532 if (len < sizeof(*rtp_hdr))
533 return;
534
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200535 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200536 seq = ntohs(rtp_hdr->sequence);
537 timestamp = ntohl(rtp_hdr->timestamp);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200538 arrival_time = get_current_ts(rtp_end->codec->rate);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200539 ssrc = ntohl(rtp_hdr->ssrc);
540 transit = arrival_time - timestamp;
541
542 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc);
543
544 if (!state->initialized) {
545 state->initialized = 1;
546 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100547 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200548 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200549 state->packet_duration =
550 mgcp_rtp_packet_duration(endp, rtp_end);
Philipp Maier0ec1d4e2018-05-16 11:09:42 +0200551 state->out_stream.last_seq = seq - 1;
552 state->out_stream.ssrc = state->patch.orig_ssrc = ssrc;
553 state->out_stream.last_tsdelta = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200554 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200555 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200556 LOGPENDP(endp, DRTP, LOGL_INFO,
557 "initializing stream, SSRC: %u timestamp: %u "
558 "pkt-duration: %d, from %s:%d\n",
559 state->in_stream.ssrc,
560 state->patch.seq_offset, state->packet_duration,
561 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200562 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200563 state->packet_duration =
Philipp Maierbc0346e2018-06-07 09:52:16 +0200564 rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200565 LOGPENDP(endp, DRTP, LOGL_NOTICE,
566 "fixed packet duration is not available, "
567 "using fixed 20ms instead: %d from %s:%d\n",
568 state->packet_duration,
569 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200570 }
571 } else if (state->in_stream.ssrc != ssrc) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200572 LOGPENDP(endp, DRTP, LOGL_NOTICE,
573 "SSRC changed: %u -> %u "
574 "from %s:%d\n",
575 state->in_stream.ssrc, rtp_hdr->ssrc,
576 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200577
578 state->in_stream.ssrc = ssrc;
579 if (rtp_end->force_constant_ssrc) {
580 int16_t delta_seq;
581
582 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100583 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200584 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200585
586 /* Estimate number of packets that would have been sent */
587 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200588 (arrival_time - state->in_stream.last_arrival_time
589 + state->packet_duration / 2) /
590 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200591
592 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
593 delta_seq, timestamp);
594
Harald Welte33381352017-12-25 09:44:26 +0100595 state->patch.patch_ssrc = 1;
596 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200597 if (rtp_end->force_constant_ssrc != -1)
598 rtp_end->force_constant_ssrc -= 1;
599
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200600 LOGPENDP(endp, DRTP, LOGL_NOTICE,
601 "SSRC patching enabled, SSRC: %u "
602 "SeqNo offset: %d, TS offset: %d "
603 "from %s:%d\n", state->in_stream.ssrc,
604 state->patch.seq_offset, state->patch.timestamp_offset,
605 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200606 }
607
608 state->in_stream.last_tsdelta = 0;
609 } else {
610 /* Compute current per-packet timestamp delta */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200611 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
612 addr, seq, timestamp, "input",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200613 &state->in_stream.last_tsdelta);
614
Harald Welte33381352017-12-25 09:44:26 +0100615 if (state->patch.patch_ssrc)
616 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200617 }
618
619 /* Save before patching */
620 state->in_stream.last_timestamp = timestamp;
621 state->in_stream.last_seq = seq;
622 state->in_stream.last_arrival_time = arrival_time;
623
624 if (rtp_end->force_aligned_timing &&
625 state->out_stream.ssrc == ssrc && state->packet_duration)
626 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200627 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
628 timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200629
630 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100631 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200632 rtp_hdr->ssrc = htonl(ssrc);
633
634 /* Apply the offset and store it back to the packet.
635 * This won't change anything if the offset is 0, so the conditional is
636 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100637 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200638 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100639 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200640 rtp_hdr->timestamp = htonl(timestamp);
641
642 /* Check again, whether the timestamps are still valid */
643 if (state->out_stream.ssrc == ssrc)
644 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
645 addr, seq, timestamp, "output",
646 &state->out_stream.last_tsdelta);
647
648 /* Save output values */
649 state->out_stream.last_seq = seq;
650 state->out_stream.last_timestamp = timestamp;
651 state->out_stream.ssrc = ssrc;
652
653 if (payload < 0)
654 return;
655
656#if 0
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200657 LOGPENDP(endp, DRTP, LOGL_DEBUG, "payload hdr payload %u -> endp payload %u\n",
658 rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200659 rtp_hdr->payload_type = payload;
660#endif
661}
662
Philipp Maier9fc8a022019-02-20 12:26:52 +0100663/* There are different dialects used to format and transfer voice data. When
664 * the receiving end expects GSM-HR data to be formated after RFC 5993, this
665 * function is used to convert between RFC 5993 and TS 101318, which we normally
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200666 * use.
667 * Return 0 on sucess, negative on errors like invalid data length. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200668static int rfc5993_hr_convert(struct mgcp_endpoint *endp, struct msgb *msg)
Philipp Maier9fc8a022019-02-20 12:26:52 +0100669{
Philipp Maier9fc8a022019-02-20 12:26:52 +0100670 struct rtp_hdr *rtp_hdr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200671 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200672 LOGPENDP(endp, DRTP, LOGL_ERROR, "AMR RTP packet too short (%d < %zu)\n",
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200673 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200674 return -EINVAL;
675 }
676
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200677 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100678
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200679 if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr)) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100680 /* TS 101318 encoding => RFC 5993 encoding */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200681 msgb_put(msg, 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100682 memmove(rtp_hdr->data + 1, rtp_hdr->data, GSM_HR_BYTES);
683 rtp_hdr->data[0] = 0x00;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200684 } else if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr) + 1) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100685 /* RFC 5993 encoding => TS 101318 encoding */
686 memmove(rtp_hdr->data, rtp_hdr->data + 1, GSM_HR_BYTES);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200687 msgb_trim(msg, msgb_length(msg) - 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100688 } else {
689 /* It is possible that multiple payloads occur in one RTP
690 * packet. This is not supported yet. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200691 LOGPENDP(endp, DRTP, LOGL_ERROR,
692 "cannot figure out how to convert RTP packet\n");
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200693 return -ENOTSUP;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100694 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200695 return 0;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100696}
697
Philipp Maier228e5912019-03-05 13:56:59 +0100698/* For AMR RTP two framing modes are defined RFC3267. There is a bandwith
699 * efficient encoding scheme where all fields are packed together one after
700 * another and an octet aligned mode where all fields are aligned to octet
701 * boundaries. This function is used to convert between the two modes */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200702static int amr_oa_bwe_convert(struct mgcp_endpoint *endp, struct msgb *msg,
Philipp Maier228e5912019-03-05 13:56:59 +0100703 bool target_is_oa)
704{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200705 /* NOTE: the msgb has an allocated length of RTP_BUF_SIZE, so there is
Philipp Maier228e5912019-03-05 13:56:59 +0100706 * plenty of space available to store the slightly larger, converted
707 * data */
Philipp Maier228e5912019-03-05 13:56:59 +0100708 struct rtp_hdr *rtp_hdr;
709 unsigned int payload_len;
710 int rc;
711
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200712 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
713 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 +0200714 return -EINVAL;
715 }
716
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200717 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier228e5912019-03-05 13:56:59 +0100718
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200719 payload_len = msgb_length(msg) - sizeof(struct rtp_hdr);
Philipp Maier228e5912019-03-05 13:56:59 +0100720
721 if (osmo_amr_is_oa(rtp_hdr->data, payload_len)) {
722 if (!target_is_oa)
723 /* Input data is oa an target format is bwe
724 * ==> convert */
725 rc = osmo_amr_oa_to_bwe(rtp_hdr->data, payload_len);
726 else
727 /* Input data is already bew, but we accept it anyway
728 * ==> no conversion needed */
729 rc = payload_len;
730 } else {
731 if (target_is_oa)
732 /* Input data is bwe an target format is oa
733 * ==> convert */
734 rc = osmo_amr_bwe_to_oa(rtp_hdr->data, payload_len,
735 RTP_BUF_SIZE);
736 else
737 /* Input data is already oa, but we accept it anyway
738 * ==> no conversion needed */
739 rc = payload_len;
740 }
741 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200742 LOGPENDP(endp, DRTP, LOGL_ERROR,
743 "AMR RTP packet conversion failed\n");
Philipp Maier228e5912019-03-05 13:56:59 +0100744 return -EINVAL;
745 }
746
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200747 return msgb_trim(msg, rc + sizeof(struct rtp_hdr));
Philipp Maier228e5912019-03-05 13:56:59 +0100748}
749
750/* Check if a conversion between octet-aligned and bandwith-efficient mode is
751 * indicated. */
752static bool amr_oa_bwe_convert_indicated(struct mgcp_rtp_codec *codec)
753{
754 if (codec->param_present == false)
755 return false;
756 if (!codec->param.amr_octet_aligned_present)
757 return false;
758 if (strcmp(codec->subtype_name, "AMR") != 0)
759 return false;
760 return true;
761}
762
763
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200764/* Return whether an RTP packet with AMR payload is in octet-aligned mode.
765 * Return 0 if in bandwidth-efficient mode, 1 for octet-aligned mode, and negative if the RTP data is invalid. */
766static int amr_oa_check(char *data, int len)
Philipp Maier228e5912019-03-05 13:56:59 +0100767{
768 struct rtp_hdr *rtp_hdr;
769 unsigned int payload_len;
770
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200771 if (len < sizeof(struct rtp_hdr))
772 return -EINVAL;
773
Philipp Maier228e5912019-03-05 13:56:59 +0100774 rtp_hdr = (struct rtp_hdr *)data;
775
776 payload_len = len - sizeof(struct rtp_hdr);
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200777 if (payload_len < sizeof(struct amr_hdr))
778 return -EINVAL;
Philipp Maier228e5912019-03-05 13:56:59 +0100779
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200780 return osmo_amr_is_oa(rtp_hdr->data, payload_len) ? 1 : 0;
Philipp Maier228e5912019-03-05 13:56:59 +0100781}
782
Philipp Maier87bd9be2017-08-22 16:35:41 +0200783/* Forward data to a debug tap. This is debug function that is intended for
784 * debugging the voice traffic with tools like gstreamer */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200785static void forward_data(int fd, struct mgcp_rtp_tap *tap, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200786{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100787 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200788
Philipp Maiere6f172d2017-11-07 12:00:01 +0100789 if (!tap->enabled)
790 return;
791
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200792 rc = sendto(fd, msgb_data(msg), msgb_length(msg), 0, (struct sockaddr *)&tap->forward,
Philipp Maiere6f172d2017-11-07 12:00:01 +0100793 sizeof(tap->forward));
794
795 if (rc < 0)
796 LOGP(DRTP, LOGL_ERROR,
797 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200798}
799
Philipp Maier889fe7f2020-07-06 17:44:12 +0200800/* Generate an RTP header if it is missing */
801static void gen_rtp_header(struct msgb *msg, struct mgcp_rtp_end *rtp_end,
802 struct mgcp_rtp_state *state)
803{
804 struct rtp_hdr *hdr = (struct rtp_hdr *)msgb_data(msg);
805
806 if (hdr->version > 0)
807 return;
808
809 hdr->version = 2;
810 hdr->payload_type = rtp_end->codec->payload_type;
811 hdr->timestamp = osmo_htonl(get_current_ts(rtp_end->codec->rate));
812 hdr->sequence = osmo_htons(state->alt_rtp_tx_sequence);
813 hdr->ssrc = state->alt_rtp_tx_ssrc;
814}
815
816
Philipp Maier87bd9be2017-08-22 16:35:41 +0200817/*! Send RTP/RTCP data to a specified destination connection.
Philipp Maier0b79d212020-06-18 12:02:49 +0200818 * \param[in] endp associated endpoint (for configuration, logging).
819 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP.
820 * \param[in] spoofed source address (set to NULL to disable).
821 * \param[in] buf buffer that contains the RTP/RTCP data.
822 * \param[in] len length of the buffer that contains the RTP/RTCP data.
823 * \param[in] conn_src associated source connection.
824 * \param[in] conn_dst associated destination connection.
825 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200826int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200827 struct msgb *msg, struct mgcp_conn_rtp *conn_src,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200828 struct mgcp_conn_rtp *conn_dst)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200829{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200830 /*! When no destination connection is available (e.g. when only one
831 * connection in loopback mode exists), then the source connection
832 * shall be specified as destination connection */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200833
Philipp Maier14b27a82020-06-02 20:15:30 +0200834 struct mgcp_trunk *trunk = endp->trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200835 struct mgcp_rtp_end *rtp_end;
836 struct mgcp_rtp_state *rtp_state;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200837 char *dest_name;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200838 int rc;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200839 int len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200840
Philipp Maier87bd9be2017-08-22 16:35:41 +0200841 OSMO_ASSERT(conn_src);
842 OSMO_ASSERT(conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200843
Philipp Maier87bd9be2017-08-22 16:35:41 +0200844 if (is_rtp) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200845 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTP packet...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200846 } else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200847 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTCP packet...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200848 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200849
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200850 LOGPENDP(endp, DRTP, LOGL_DEBUG, "loop:%d, mode:%d%s\n",
Philipp Maier14b27a82020-06-02 20:15:30 +0200851 trunk->audio_loop, conn_src->conn->mode,
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200852 conn_src->conn->mode == MGCP_CONN_LOOPBACK ? " (loopback)" : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200853
Philipp Maier6931f9a2018-07-26 09:29:31 +0200854 /* FIXME: It is legal that the payload type on the egress connection is
855 * different from the payload type that has been negotiated on the
856 * ingress connection. Essentially the codecs are the same so we can
857 * match them and patch the payload type. However, if we can not find
858 * the codec pendant (everything ist equal except the PT), we are of
859 * course unable to patch the payload type. A situation like this
860 * should not occur if transcoding is consequently avoided. Until
861 * we have transcoding support in osmo-mgw we can not resolve this. */
Philipp Maierda895b12018-08-03 12:16:37 +0200862 if (is_rtp) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200863 rc = mgcp_patch_pt(conn_src, conn_dst, msg);
Philipp Maierda895b12018-08-03 12:16:37 +0200864 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200865 LOGPENDP(endp, DRTP, LOGL_DEBUG,
866 "can not patch PT because no suitable egress codec was found.\n");
Philipp Maierda895b12018-08-03 12:16:37 +0200867 }
Philipp Maier6931f9a2018-07-26 09:29:31 +0200868 }
869
Philipp Maier87bd9be2017-08-22 16:35:41 +0200870 /* Note: In case of loopback configuration, both, the source and the
871 * destination will point to the same connection. */
872 rtp_end = &conn_dst->end;
873 rtp_state = &conn_src->state;
874 dest_name = conn_dst->conn->name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200875
Philipp Maier889fe7f2020-07-06 17:44:12 +0200876 /* Ensure we have an alternative SSRC in case we need it, see also
877 * gen_rtp_header() */
878 if (rtp_state->alt_rtp_tx_ssrc == 0)
879 rtp_state->alt_rtp_tx_ssrc = rand();
880
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200881 if (!rtp_end->output_enabled) {
Harald Weltea48ff4a2020-03-08 14:45:08 +0100882 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_DROPPED_PACKETS_CTR);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200883 LOGPENDP(endp, DRTP, LOGL_DEBUG,
884 "output disabled, drop to %s %s "
885 "rtp_port:%u rtcp_port:%u\n",
886 dest_name,
887 inet_ntoa(rtp_end->addr),
888 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200889 );
890 } else if (is_rtp) {
891 int cont;
892 int nbytes = 0;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200893 int buflen = msgb_length(msg);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200894
895 /* Make sure we have a valid RTP header, in cases where no RTP
896 * header is present, we will generate one. */
897 gen_rtp_header(msg, rtp_end, rtp_state);
898
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200899 do {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200900 /* Run transcoder */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200901 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200902 (char*)msgb_data(msg), &buflen,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200903 RTP_BUF_SIZE);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200904 if (cont < 0)
905 break;
906
Philipp Maier87bd9be2017-08-22 16:35:41 +0200907 if (addr)
908 mgcp_patch_and_count(endp, rtp_state, rtp_end,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200909 addr, msg);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100910
Philipp Maier228e5912019-03-05 13:56:59 +0100911 if (amr_oa_bwe_convert_indicated(conn_dst->end.codec)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200912 rc = amr_oa_bwe_convert(endp, msg,
Neels Hofmeyr5a6220f2019-08-20 03:09:04 +0200913 conn_dst->end.codec->param.amr_octet_aligned);
914 if (rc < 0) {
915 LOGPENDP(endp, DRTP, LOGL_ERROR,
916 "Error in AMR octet-aligned <-> bandwidth-efficient mode conversion\n");
917 break;
918 }
Philipp Maier228e5912019-03-05 13:56:59 +0100919 }
920 else if (rtp_end->rfc5993_hr_convert
Philipp Maier9fc8a022019-02-20 12:26:52 +0100921 && strcmp(conn_src->end.codec->subtype_name,
Neels Hofmeyr5a6220f2019-08-20 03:09:04 +0200922 "GSM-HR-08") == 0) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200923 rc = rfc5993_hr_convert(endp, msg);
Neels Hofmeyr5a6220f2019-08-20 03:09:04 +0200924 if (rc < 0) {
925 LOGPENDP(endp, DRTP, LOGL_ERROR, "Error while converting to GSM-HR-08\n");
926 break;
927 }
928 }
Philipp Maier9fc8a022019-02-20 12:26:52 +0100929
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200930 LOGPENDP(endp, DRTP, LOGL_DEBUG,
931 "process/send to %s %s "
932 "rtp_port:%u rtcp_port:%u\n",
933 dest_name, inet_ntoa(rtp_end->addr),
934 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
935 );
Philipp Maier87bd9be2017-08-22 16:35:41 +0200936
937 /* Forward a copy of the RTP data to a debug ip/port */
938 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200939 msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200940
941 /* FIXME: HACK HACK HACK. See OS#2459.
942 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
943 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
944 * cells (as long as we patch only the first RTP payload in each stream).
945 */
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200946 if (!rtp_state->patched_first_rtp_payload
947 && conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200948 uint8_t *data = msgb_data(msg) + 12;
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200949 if (data[0] == 0xe0) {
950 data[0] = 0xe4;
951 data[1] = 0x00;
952 rtp_state->patched_first_rtp_payload = true;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200953 LOGPENDP(endp, DRTP, LOGL_DEBUG,
954 "Patching over first two bytes"
955 " to fake an IuUP Initialization Ack\n");
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200956 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200957 }
958
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200959 len = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr, rtp_end->rtp_port,
960 (char*)msgb_data(msg), msgb_length(msg));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200961
Philipp Maier87bd9be2017-08-22 16:35:41 +0200962 if (len <= 0)
963 return len;
964
Harald Weltea48ff4a2020-03-08 14:45:08 +0100965 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
966 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200967 rtp_state->alt_rtp_tx_sequence++;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200968
969 nbytes += len;
970 buflen = cont;
971 } while (buflen > 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200972 return nbytes;
Philipp Maier14b27a82020-06-02 20:15:30 +0200973 } else if (!trunk->omit_rtcp) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200974 LOGPENDP(endp, DRTP, LOGL_DEBUG,
975 "send to %s %s rtp_port:%u rtcp_port:%u\n",
976 dest_name, inet_ntoa(rtp_end->addr),
977 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
978 );
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200979
Philipp Maier87bd9be2017-08-22 16:35:41 +0200980 len = mgcp_udp_send(rtp_end->rtcp.fd,
981 &rtp_end->addr,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200982 rtp_end->rtcp_port, (char*)msgb_data(msg), msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200983
Harald Weltea48ff4a2020-03-08 14:45:08 +0100984 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
985 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200986 rtp_state->alt_rtp_tx_sequence++;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200987
988 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200989 }
990
991 return 0;
992}
993
Philipp Maier87bd9be2017-08-22 16:35:41 +0200994/* Check if the origin (addr) matches the address/port data of the RTP
995 * connections. */
996static int check_rtp_origin(struct mgcp_conn_rtp *conn,
997 struct sockaddr_in *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200998{
Harald Weltec26b6652018-10-21 12:01:04 +0200999 if (conn->end.addr.s_addr == 0) {
Neels Hofmeyrefd645e2018-09-10 13:14:50 +02001000 switch (conn->conn->mode) {
1001 case MGCP_CONN_LOOPBACK:
1002 /* HACK: for IuUP, we want to reply with an IuUP Initialization ACK upon the first RTP
1003 * message received. We currently hackishly accomplish that by putting the endpoint in
1004 * loopback mode and patching over the looped back RTP message to make it look like an
1005 * ack. We don't know the femto cell's IP address and port until the RAB Assignment
1006 * Response is received, but the nano3G expects an IuUP Initialization Ack before it even
1007 * sends the RAB Assignment Response. Hence, if the remote address is 0.0.0.0 and the
1008 * MGCP port is in loopback mode, allow looping back the packet to any source. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001009 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1010 "In loopback mode and remote address not set:"
1011 " allowing data from address: %s\n", inet_ntoa(addr->sin_addr));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +02001012 return 0;
1013
1014 default:
1015 /* Receiving early media before the endpoint is configured. Instead of logging
1016 * this as an error that occurs on every call, keep it more low profile to not
1017 * confuse humans with expected errors. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001018 LOGPCONN(conn->conn, DRTP, LOGL_INFO,
1019 "Rx RTP from %s, but remote address not set:"
1020 " dropping early media\n", inet_ntoa(addr->sin_addr));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +02001021 return -1;
1022 }
Neels Hofmeyr0063ca22018-07-23 18:12:16 +02001023 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001024
Philipp Maier87bd9be2017-08-22 16:35:41 +02001025 /* Note: Check if the inbound RTP data comes from the same host to
1026 * which we send our outgoing RTP traffic. */
Harald Weltec26b6652018-10-21 12:01:04 +02001027 if (conn->end.addr.s_addr != addr->sin_addr.s_addr) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001028 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1029 "data from wrong address: %s, ", inet_ntoa(addr->sin_addr));
Philipp Maierc3413882017-10-27 12:26:54 +02001030 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001031 inet_ntoa(conn->end.addr));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001032 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001033 return -1;
1034 }
1035
Philipp Maier87bd9be2017-08-22 16:35:41 +02001036 /* Note: Usually the remote remote port of the data we receive will be
1037 * the same as the remote port where we transmit outgoing RTP traffic
1038 * to (set by MDCX). We use this to check the origin of the data for
1039 * plausibility. */
1040 if (conn->end.rtp_port != addr->sin_port &&
1041 conn->end.rtcp_port != addr->sin_port) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001042 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1043 "data from wrong source port: %d, ", ntohs(addr->sin_port));
Philipp Maierc3413882017-10-27 12:26:54 +02001044 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001045 "expected: %d for RTP or %d for RTCP\n",
1046 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001047 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001048 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001049 }
1050
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001051 return 0;
1052}
1053
Philipp Maier87bd9be2017-08-22 16:35:41 +02001054/* Check the if the destination address configuration of an RTP connection
1055 * makes sense */
1056static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001057{
Philipp Maiere6df0e42018-05-29 14:03:06 +02001058 /* Note: it is legal to create a connection but never setting a port
1059 * and IP-address for outgoing data. */
1060 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 +02001061 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
1062 "destination IP-address and rtp port is (not yet) known (%s:%u)\n",
1063 inet_ntoa(conn->end.addr), conn->end.rtp_port);
Philipp Maiere6df0e42018-05-29 14:03:06 +02001064 return -1;
1065 }
1066
Philipp Maier87bd9be2017-08-22 16:35:41 +02001067 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001068 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1069 "destination IP-address is invalid (%s:%u)\n",
1070 inet_ntoa(conn->end.addr), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001071 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001072 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001073
1074 if (conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001075 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1076 "destination rtp port is invalid (%s:%u)\n",
1077 inet_ntoa(conn->end.addr), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001078 return -1;
1079 }
1080
1081 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001082}
1083
Philipp Maier4dba7692018-08-03 12:20:52 +02001084/* Do some basic checks to make sure that the RTCP packets we are going to
1085 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001086static int check_rtcp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +02001087{
1088 struct rtcp_hdr *hdr;
1089 unsigned int len;
1090 uint8_t type;
1091
1092 /* RTPC packets that are just a header without data do not make
1093 * any sense. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001094 if (msgb_length(msg) < sizeof(struct rtcp_hdr)) {
1095 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP packet too short (%u < %zu)\n",
1096 msgb_length(msg), sizeof(struct rtcp_hdr));
Philipp Maier4dba7692018-08-03 12:20:52 +02001097 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001098 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001099
1100 /* Make sure that the length of the received packet does not exceed
1101 * the available buffer size */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001102 hdr = (struct rtcp_hdr *)msgb_data(msg);
Philipp Maier4dba7692018-08-03 12:20:52 +02001103 len = (osmo_ntohs(hdr->length) + 1) * 4;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001104 if (len > msgb_length(msg)) {
1105 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header length exceeds packet size (%u > %u)\n",
1106 len, msgb_length(msg));
Philipp Maier4dba7692018-08-03 12:20:52 +02001107 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001108 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001109
1110 /* Make sure we accept only packets that have a proper packet type set
1111 * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
1112 type = hdr->type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001113 if ((type < 192 || type > 195) && (type < 200 || type > 213)) {
1114 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header: invalid type: %u\n", type);
Philipp Maier4dba7692018-08-03 12:20:52 +02001115 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001116 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001117
1118 return 0;
1119}
1120
1121/* Do some basic checks to make sure that the RTP packets we are going to
1122 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001123static int check_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +02001124{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001125 size_t min_size = sizeof(struct rtp_hdr);
1126 if (msgb_length(msg) < min_size) {
1127 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
1128 msgb_length(msg), min_size);
1129 return -1;
1130 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001131
1132 /* FIXME: Add more checks, the reason why we do not check more than
1133 * the length is because we currently handle IUUP packets as RTP
1134 * packets, so they must pass this check, if we weould be more
1135 * strict here, we would possibly break 3G. (see also FIXME note
1136 * below */
1137
1138 return 0;
1139}
1140
Philipp Maier87bd9be2017-08-22 16:35:41 +02001141/* Send RTP data. Possible options are standard RTP packet
1142 * transmission or trsmission via an osmux connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001143static int mgcp_send_rtp(struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001144{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001145 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1146 enum rtp_proto proto = mc->proto;
1147 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1148 struct sockaddr_in *from_addr = mc->from_addr;
1149 struct mgcp_endpoint *endp = conn_src->conn->endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001150
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001151 LOGPENDP(endp, DRTP, LOGL_DEBUG, "destin conn:%s\n",
1152 mgcp_conn_dump(conn_dst->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001153
1154 /* Before we try to deliver the packet, we check if the destination
1155 * port and IP-Address make sense at all. If not, we will be unable
1156 * to deliver the packet. */
1157 if (check_rtp_destin(conn_dst) != 0)
1158 return -1;
1159
1160 /* Depending on the RTP connection type, deliver the RTP packet to the
1161 * destination connection. */
1162 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001163 case MGCP_RTP_DEFAULT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001164 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1165 "endpoint type is MGCP_RTP_DEFAULT, "
1166 "using mgcp_send() to forward data directly\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001167 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001168 from_addr, msg, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001169 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001170 case MGCP_OSMUX_BSC:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001171 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1172 "endpoint type is MGCP_OSMUX_BSC_NAT, "
1173 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n");
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001174 return osmux_xfrm_to_osmux((char*)msgb_data(msg), msgb_length(msg), conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001175 }
1176
Philipp Maier87bd9be2017-08-22 16:35:41 +02001177 /* If the data has not been handled/forwarded until here, it will
1178 * be discarded, this should not happen, normally the MGCP type
1179 * should be properly set */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001180 LOGPENDP(endp, DRTP, LOGL_ERROR, "bad MGCP type -- data discarded!\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001181
1182 return -1;
1183}
1184
1185/*! dispatch incoming RTP packet to opposite RTP connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001186 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1187 * \param[in] addr socket address where the RTP packet has been received from.
1188 * \param[in] buf buffer that hold the RTP payload.
1189 * \param[in] buf_size size data length of buf.
1190 * \param[in] conn originating connection.
1191 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001192int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001193{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001194 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1195 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1196 struct mgcp_conn *conn = conn_src->conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001197 struct mgcp_conn *conn_dst;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001198 struct sockaddr_in *from_addr = mc->from_addr;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001199
1200 /*! NOTE: This callback function implements the endpoint specific
Alexander Chemeris61cf9bb2020-05-11 18:13:53 +03001201 * dispatch behaviour of an rtp bridge/proxy endpoint. It is assumed
Philipp Maier87bd9be2017-08-22 16:35:41 +02001202 * that the endpoint will hold only two connections. This premise
1203 * is used to determine the opposite connection (it is always the
1204 * connection that is not the originating connection). Once the
1205 * destination connection is known the RTP packet is sent via
1206 * the destination connection. */
1207
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001208
1209 /* Check if the connection is in loopback mode, if yes, just send the
1210 * incoming data back to the origin */
1211 if (conn->mode == MGCP_CONN_LOOPBACK) {
1212 /* When we are in loopback mode, we loop back all incoming
1213 * packets back to their origin. We will use the originating
1214 * address data from the UDP packet header to patch the
1215 * outgoing address in connection on the fly */
1216 if (conn->u.rtp.end.rtp_port == 0) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001217 conn->u.rtp.end.addr = from_addr->sin_addr;
1218 conn->u.rtp.end.rtp_port = from_addr->sin_port;
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001219 }
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001220 return mgcp_send_rtp(conn_src, msg);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001221 }
1222
Philipp Maier87bd9be2017-08-22 16:35:41 +02001223 /* Find a destination connection. */
1224 /* NOTE: This code path runs every time an RTP packet is received. The
1225 * function mgcp_find_dst_conn() we use to determine the detination
1226 * connection will iterate the connection list inside the endpoint.
1227 * Since list iterations are quite costly, we will figure out the
1228 * destination only once and use the optional private data pointer of
1229 * the connection to cache the destination connection pointer. */
1230 if (!conn->priv) {
1231 conn_dst = mgcp_find_dst_conn(conn);
1232 conn->priv = conn_dst;
1233 } else {
1234 conn_dst = (struct mgcp_conn *)conn->priv;
1235 }
1236
1237 /* There is no destination conn, stop here */
1238 if (!conn_dst) {
Alexander Chemerisebb9bf32020-05-11 18:14:31 +03001239 LOGPCONN(conn, DRTP, LOGL_DEBUG,
1240 "no connection to forward an incoming RTP packet to\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001241 return -1;
1242 }
1243
1244 /* The destination conn is not an RTP connection */
1245 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001246 LOGPCONN(conn, DRTP, LOGL_ERROR,
1247 "unable to find suitable destination conn\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001248 return -1;
1249 }
1250
1251 /* Dispatch RTP packet to destination RTP connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001252 return mgcp_send_rtp(&conn_dst->u.rtp, msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001253}
1254
Philipp Maier0996a1e2020-06-10 15:27:14 +02001255/*! dispatch incoming RTP packet to E1 subslot, handle RTCP packets locally.
1256 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1257 * \param[in] addr socket address where the RTP packet has been received from.
1258 * \param[in] buf buffer that hold the RTP payload.
1259 * \param[in] buf_size size data length of buf.
1260 * \param[in] conn originating connection.
1261 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001262int mgcp_dispatch_e1_bridge_cb(struct msgb *msg)
Philipp Maier0996a1e2020-06-10 15:27:14 +02001263{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001264 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1265 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1266 struct mgcp_conn *conn = conn_src->conn;
Philipp Maier889fe7f2020-07-06 17:44:12 +02001267 struct sockaddr_in *from_addr = mc->from_addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001268
Philipp Maier889fe7f2020-07-06 17:44:12 +02001269 /* Check if the connection is in loopback mode, if yes, just send the
1270 * incoming data back to the origin */
1271 if (conn->mode == MGCP_CONN_LOOPBACK) {
1272 /* When we are in loopback mode, we loop back all incoming
1273 * packets back to their origin. We will use the originating
1274 * address data from the UDP packet header to patch the
1275 * outgoing address in connection on the fly */
1276 if (conn->u.rtp.end.rtp_port == 0) {
1277 conn->u.rtp.end.addr = from_addr->sin_addr;
1278 conn->u.rtp.end.rtp_port = from_addr->sin_port;
1279 }
1280 return mgcp_send_rtp(conn_src, msg);
1281 }
1282
1283 /* Forward to E1 */
1284 return mgcp_e1_send_rtp(conn->endp, conn->u.rtp.end.codec, msg);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001285}
1286
Philipp Maierdf5d2192018-01-24 11:39:32 +01001287/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1288 * \param[in] endp Endpoint on which the connection resides.
Philipp Maier08eb9352020-06-18 11:55:35 +02001289 * \param[in] conn Connection that is about to be removed (ignored). */
Philipp Maierdf5d2192018-01-24 11:39:32 +01001290void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1291{
1292 struct mgcp_conn *conn_cleanup;
1293
1294 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1295 * pointer to the destination connection, so that we do not have
1296 * to go through the list every time an RTP packet arrives. To prevent
1297 * a use-after-free situation we invalidate this information for all
1298 * connections present when one connection is removed from the
1299 * endpoint. */
1300 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
1301 conn_cleanup->priv = NULL;
1302 }
1303}
1304
Philipp Maier0996a1e2020-06-10 15:27:14 +02001305/*! cleanup an endpoint when a connection on an E1 endpoint is removed.
1306 * \param[in] endp Endpoint on which the connection resides.
1307 * \param[in] conn Connection that is about to be removed (ignored). */
1308void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1309{
Philipp Maier889fe7f2020-07-06 17:44:12 +02001310 /* Cleanup tasks for E1 are the same as for regular endpoint. The
1311 * shut down of the E1 part is handled separately. */
1312 mgcp_cleanup_rtp_bridge_cb(endp, conn);
Philipp Maier0996a1e2020-06-10 15:27:14 +02001313}
1314
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001315static bool is_dummy_msg(enum rtp_proto proto, struct msgb *msg)
1316{
1317 return msgb_length(msg) == 1 && msgb_data(msg)[0] == MGCP_DUMMY_LOAD;
1318}
1319
Philipp Maier87bd9be2017-08-22 16:35:41 +02001320/* Handle incoming RTP data from NET */
1321static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1322{
1323 /* NOTE: This is a generic implementation. RTP data is received. In
1324 * case of loopback the data is just sent back to its origin. All
1325 * other cases implement endpoint specific behaviour (e.g. how is the
1326 * destination connection determined?). That specific behaviour is
1327 * implemented by the callback function that is called at the end of
1328 * the function */
1329
1330 struct mgcp_conn_rtp *conn_src;
1331 struct mgcp_endpoint *endp;
1332 struct sockaddr_in addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001333 socklen_t slen = sizeof(addr);
1334 int ret;
1335 enum rtp_proto proto;
1336 struct osmo_rtp_msg_ctx *mc;
1337 struct msgb *msg = msgb_alloc(RTP_BUF_SIZE, "RTP-rx");
1338 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001339
1340 conn_src = (struct mgcp_conn_rtp *)fd->data;
1341 OSMO_ASSERT(conn_src);
1342 endp = conn_src->conn->endp;
1343 OSMO_ASSERT(endp);
1344
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001345 proto = (fd == &conn_src->end.rtp)? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001346
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001347 ret = recvfrom(fd->fd, msgb_data(msg), msg->data_len, 0, (struct sockaddr *)&addr, &slen);
1348
1349 if (ret <= 0) {
1350 LOG_CONN_RTP(conn_src, LOGL_ERROR, "recvfrom error: %s\n", strerror(errno));
1351 rc = -1;
1352 goto out;
1353 }
1354
1355 msgb_put(msg, ret);
1356
1357 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "%s: rx %u bytes from %s:%u\n",
1358 proto == MGCP_PROTO_RTP ? "RTP" : "RTPC",
1359 msgb_length(msg), inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
1360
1361 if ((proto == MGCP_PROTO_RTP && check_rtp(conn_src, msg))
1362 || (proto == MGCP_PROTO_RTCP && check_rtcp(conn_src, msg))) {
1363 /* Logging happened in the two check_ functions */
1364 rc = -1;
1365 goto out;
1366 }
1367
1368 if (is_dummy_msg(proto, msg)) {
1369 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx dummy packet (dropped)\n");
1370 rc = 0;
1371 goto out;
1372 }
1373
1374 /* Since the msgb remains owned and freed by this function, the msg ctx data struct can just be on the stack and
1375 * needs not be allocated with the msgb. */
1376 mc = OSMO_RTP_MSG_CTX(msg);
1377 *mc = (struct osmo_rtp_msg_ctx){
1378 .proto = proto,
1379 .conn_src = conn_src,
1380 .from_addr = &addr,
1381 };
1382 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "msg ctx: %d %p %s\n",
1383 mc->proto, mc->conn_src,
1384 osmo_hexdump((void*)mc->from_addr, sizeof(struct sockaddr_in)));
1385
1386 /* Increment RX statistics */
1387 rate_ctr_inc(&conn_src->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR]);
1388 rate_ctr_add(&conn_src->rate_ctr_group->ctr[RTP_OCTETS_RX_CTR], msgb_length(msg));
1389 /* FIXME: count RTP and RTCP separately, also count IuUP payload-less separately */
1390
1391 /* Forward a copy of the RTP data to a debug ip/port */
1392 forward_data(fd->fd, &conn_src->tap_in, msg);
1393
1394 rc = rx_rtp(msg);
1395
1396out:
1397 msgb_free(msg);
1398 return rc;
1399}
1400
1401static int rx_rtp(struct msgb *msg)
1402{
1403 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1404 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1405 struct sockaddr_in *from_addr = mc->from_addr;
1406 struct mgcp_conn *conn = conn_src->conn;
1407 struct mgcp_trunk *trunk = conn->endp->trunk;
1408
1409 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx_rtp(%u bytes)\n", msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001410
Oliver Smithe36b7752019-01-22 16:31:36 +01001411 mgcp_conn_watchdog_kick(conn_src->conn);
1412
Philipp Maier228e5912019-03-05 13:56:59 +01001413 /* If AMR is configured for the ingress connection a conversion of the
1414 * framing mode (octet-aligned vs. bandwith-efficient is explicitly
1415 * define, then we check if the incoming payload matches that
1416 * expectation. */
1417 if (amr_oa_bwe_convert_indicated(conn_src->end.codec)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001418 int oa = amr_oa_check((char*)msgb_data(msg), msgb_length(msg));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +02001419 if (oa < 0)
1420 return -1;
1421 if (((bool)oa) != conn_src->end.codec->param.amr_octet_aligned)
Philipp Maier228e5912019-03-05 13:56:59 +01001422 return -1;
1423 }
1424
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001425 /* Check if the origin of the RTP packet seems plausible */
1426 if (!trunk->rtp_accept_all && check_rtp_origin(conn_src, from_addr))
1427 return -1;
1428
Philipp Maier87bd9be2017-08-22 16:35:41 +02001429 /* Execute endpoint specific implementation that handles the
1430 * dispatching of the RTP data */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001431 return conn->endp->type->dispatch_rtp_cb(msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001432}
1433
1434/*! set IP Type of Service parameter.
Philipp Maier0b79d212020-06-18 12:02:49 +02001435 * \param[in] fd associated file descriptor.
1436 * \param[in] tos dscp value.
1437 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001438int mgcp_set_ip_tos(int fd, int tos)
1439{
1440 int ret;
1441 ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1442
1443 if (ret < 0)
1444 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001445 return 0;
1446}
1447
Philipp Maier87bd9be2017-08-22 16:35:41 +02001448/*! bind RTP port to osmo_fd.
Philipp Maier0b79d212020-06-18 12:02:49 +02001449 * \param[in] source_addr source (local) address to bind on.
1450 * \param[in] fd associated file descriptor.
1451 * \param[in] port to bind on.
1452 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001453int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
1454{
Harald Welte8890dfa2017-11-17 15:09:30 +01001455 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001456
Harald Welte8890dfa2017-11-17 15:09:30 +01001457 rc = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
1458 NULL, 0, OSMO_SOCK_F_BIND);
1459 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001460 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001461 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001462 return -1;
1463 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001464 fd->fd = rc;
1465 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001466
1467 return 0;
1468}
1469
Philipp Maier87bd9be2017-08-22 16:35:41 +02001470/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001471static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001472 struct mgcp_rtp_end *rtp_end, struct mgcp_endpoint *endp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001473{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001474 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1475 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1476
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001477 if (mgcp_create_bind(source_addr, &rtp_end->rtp,
1478 rtp_end->local_port) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001479 LOGPENDP(endp, DRTP, LOGL_ERROR,
1480 "failed to create RTP port: %s:%d\n",
1481 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001482 goto cleanup0;
1483 }
1484
1485 if (mgcp_create_bind(source_addr, &rtp_end->rtcp,
1486 rtp_end->local_port + 1) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001487 LOGPENDP(endp, DRTP, LOGL_ERROR,
1488 "failed to create RTCP port: %s:%d\n",
1489 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001490 goto cleanup1;
1491 }
1492
Philipp Maier87bd9be2017-08-22 16:35:41 +02001493 /* Set Type of Service (DSCP-Value) as configured via VTY */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001494 mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
1495 mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
1496
Pau Espin Pedrola7152e02020-05-09 19:15:50 +02001497 rtp_end->rtp.when = OSMO_FD_READ;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001498 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001499 LOGPENDP(endp, DRTP, LOGL_ERROR,
1500 "failed to register RTP port %d\n",
1501 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001502 goto cleanup2;
1503 }
1504
Pau Espin Pedrola7152e02020-05-09 19:15:50 +02001505 rtp_end->rtcp.when = OSMO_FD_READ;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001506 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001507 LOGPENDP(endp, DRTP, LOGL_ERROR,
1508 "failed to register RTCP port %d\n",
1509 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001510 goto cleanup3;
1511 }
1512
1513 return 0;
1514
1515cleanup3:
1516 osmo_fd_unregister(&rtp_end->rtp);
1517cleanup2:
1518 close(rtp_end->rtcp.fd);
1519 rtp_end->rtcp.fd = -1;
1520cleanup1:
1521 close(rtp_end->rtp.fd);
1522 rtp_end->rtp.fd = -1;
1523cleanup0:
1524 return -1;
1525}
1526
Philipp Maier87bd9be2017-08-22 16:35:41 +02001527/*! bind RTP port to endpoint/connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001528 * \param[in] endp endpoint that holds the RTP connection.
1529 * \param[in] rtp_port port number to bind on.
1530 * \param[in] conn associated RTP connection.
1531 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001532int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1533 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001534{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001535 char name[512];
1536 struct mgcp_rtp_end *end;
Philipp Maier1cb1e382017-11-02 17:16:04 +01001537 char local_ip_addr[INET_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001538
Philipp Maier01d24a32017-11-21 17:26:09 +01001539 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001540 end = &conn->end;
1541
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001542 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001543 LOGPENDP(endp, DRTP, LOGL_ERROR, "%u was already bound on conn:%s\n",
1544 rtp_port, mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001545
1546 /* Double bindings should never occour! Since we always allocate
1547 * connections dynamically and free them when they are not
1548 * needed anymore, there must be no previous binding leftover.
1549 * Should there be a connection bound twice, we have a serious
1550 * problem and must exit immediately! */
1551 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001552 }
1553
1554 end->local_port = rtp_port;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001555 end->rtp.cb = rtp_data_net;
1556 end->rtp.data = conn;
1557 end->rtcp.data = conn;
1558 end->rtcp.cb = rtp_data_net;
1559
Philipp Maier1cb1e382017-11-02 17:16:04 +01001560 mgcp_get_local_addr(local_ip_addr, conn);
1561
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001562 return bind_rtp(endp->cfg, local_ip_addr, end, endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001563}
1564
Philipp Maier87bd9be2017-08-22 16:35:41 +02001565/*! free allocated RTP and RTCP ports.
1566 * \param[in] end RTP end */
1567void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001568{
1569 if (end->rtp.fd != -1) {
1570 close(end->rtp.fd);
1571 end->rtp.fd = -1;
1572 osmo_fd_unregister(&end->rtp);
1573 }
1574
1575 if (end->rtcp.fd != -1) {
1576 close(end->rtcp.fd);
1577 end->rtcp.fd = -1;
1578 osmo_fd_unregister(&end->rtcp);
1579 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001580}