blob: 155ed205039e311b6553a254d40d6cded38376b3 [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>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020049
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
54#define RTP_BUF_SIZE 4096
55
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +020056enum rtp_proto {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020057 MGCP_PROTO_RTP,
58 MGCP_PROTO_RTCP,
59};
60
Harald Weltea48ff4a2020-03-08 14:45:08 +010061static void rtpconn_rate_ctr_add(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp,
62 int id, int inc)
63{
64 struct rate_ctr_group *conn_stats = conn_rtp->rate_ctr_group;
Philipp Maierc66ab2c2020-06-02 20:55:34 +020065 struct rate_ctr_group *mgw_stats = endp->trunk->ratectr.all_rtp_conn_stats;
Harald Weltea48ff4a2020-03-08 14:45:08 +010066
Philipp Maierc66ab2c2020-06-02 20:55:34 +020067 /* add to both the per-connection and the global stats */
Harald Weltea48ff4a2020-03-08 14:45:08 +010068 rate_ctr_add(&conn_stats->ctr[id], inc);
Philipp Maierc66ab2c2020-06-02 20:55:34 +020069 rate_ctr_add(&mgw_stats->ctr[id], inc);
Harald Weltea48ff4a2020-03-08 14:45:08 +010070}
71
72static void rtpconn_rate_ctr_inc(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp, int id)
73{
74 rtpconn_rate_ctr_add(conn_rtp, endp, id, 1);
75}
76
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +020077static int rx_rtp(struct msgb *msg);
78
Philipp Maier1cb1e382017-11-02 17:16:04 +010079/*! Determine the local rtp bind IP-address.
Philipp Maier0b79d212020-06-18 12:02:49 +020080 * \param[out] addr caller provided memory to store the resulting IP-Address.
81 * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters.
Philipp Maier1cb1e382017-11-02 17:16:04 +010082 *
83 * The local bind IP-address is automatically selected by probing the
84 * IP-Address of the interface that is pointing towards the remote IP-Address,
85 * if no remote IP-Address is known yet, the statically configured
86 * IP-Addresses are used as fallback. */
87void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn)
88{
89
90 struct mgcp_endpoint *endp;
91 int rc;
92 endp = conn->conn->endp;
93
94 /* Try probing the local IP-Address */
95 if (endp->cfg->net_ports.bind_addr_probe && conn->end.addr.s_addr != 0) {
96 rc = osmo_sock_local_ip(addr, inet_ntoa(conn->end.addr));
97 if (rc < 0)
Pau Espin Pedrol3239f622019-04-24 18:47:46 +020098 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
99 "local interface auto detection failed, using configured addresses...\n");
Philipp Maier1cb1e382017-11-02 17:16:04 +0100100 else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200101 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
102 "selected local rtp bind ip %s by probing using remote ip %s\n",
103 addr, inet_ntoa(conn->end.addr));
Philipp Maier1cb1e382017-11-02 17:16:04 +0100104 return;
105 }
106 }
107
Pau Espin Pedrola93c6e92019-05-06 15:23:57 +0200108 /* Select from preconfigured IP-Addresses. We don't have bind_addr for Osmux (yet?). */
Philipp Maier1cb1e382017-11-02 17:16:04 +0100109 if (endp->cfg->net_ports.bind_addr) {
110 /* Check there is a bind IP for the RTP traffic configured,
111 * if so, use that IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100112 osmo_strlcpy(addr, endp->cfg->net_ports.bind_addr, INET_ADDRSTRLEN);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200113 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
114 "using configured rtp bind ip as local bind ip %s\n",
115 addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100116 } else {
117 /* No specific bind IP is configured for the RTP traffic, so
118 * assume the IP where we listen for incoming MGCP messages
119 * as bind IP */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100120 osmo_strlcpy(addr, endp->cfg->source_addr, INET_ADDRSTRLEN);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200121 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
122 "using mgcp bind ip as local rtp bind ip: %s\n", addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100123 }
124}
125
Philipp Maier87bd9be2017-08-22 16:35:41 +0200126/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200127 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +0200128 * 1/codec_rate seconds. */
129static uint32_t get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200130{
131 struct timespec tp;
132 uint64_t ret;
133
Philipp Maier87bd9be2017-08-22 16:35:41 +0200134 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200135 return 0;
136
137 memset(&tp, 0, sizeof(tp));
138 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maierc3413882017-10-27 12:26:54 +0200139 LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200140
141 /* convert it to 1/unit seconds */
142 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200143 ret *= codec_rate;
144 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200145
146 return ret;
147}
148
Philipp Maier87bd9be2017-08-22 16:35:41 +0200149/*! send udp packet.
Philipp Maier0b79d212020-06-18 12:02:49 +0200150 * \param[in] fd associated file descriptor.
151 * \param[in] addr destination ip-address.
152 * \param[in] port destination UDP port (network byte order).
153 * \param[in] buf buffer that holds the data to be send.
154 * \param[in] len length of the data to be sent.
155 * \returns bytes sent, -1 on error. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200156int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
157{
158 struct sockaddr_in out;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200159
Philipp Maierc3413882017-10-27 12:26:54 +0200160 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200161 "sending %i bytes length packet to %s:%u ...\n",
162 len, inet_ntoa(*addr), ntohs(port));
163
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200164 out.sin_family = AF_INET;
165 out.sin_port = port;
166 memcpy(&out.sin_addr, addr, sizeof(*addr));
167
168 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
169}
170
Philipp Maier87bd9be2017-08-22 16:35:41 +0200171/*! send RTP dummy packet (to keep NAT connection open).
Philipp Maier0b79d212020-06-18 12:02:49 +0200172 * \param[in] endp mcgp endpoint that holds the RTP connection.
173 * \param[in] conn associated RTP connection.
174 * \returns bytes sent, -1 on error. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200175int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200176{
177 static char buf[] = { MGCP_DUMMY_LOAD };
178 int rc;
179 int was_rtcp = 0;
180
Philipp Maier87bd9be2017-08-22 16:35:41 +0200181 OSMO_ASSERT(endp);
182 OSMO_ASSERT(conn);
183
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200184 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,"sending dummy packet... %s\n",
185 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200186
187 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
188 conn->end.rtp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200189
190 if (rc == -1)
191 goto failed;
192
Philipp Maier14b27a82020-06-02 20:15:30 +0200193 if (endp->trunk->omit_rtcp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200194 return rc;
195
196 was_rtcp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200197 rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
198 conn->end.rtcp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200199
200 if (rc >= 0)
201 return rc;
202
203failed:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200204 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
205 "Failed to send dummy %s packet.\n",
206 was_rtcp ? "RTCP" : "RTP");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200207
208 return -1;
209}
210
Philipp Maier87bd9be2017-08-22 16:35:41 +0200211/* Compute timestamp alignment error */
212static int32_t ts_alignment_error(struct mgcp_rtp_stream_state *sstate,
213 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200214{
215 int32_t timestamp_delta;
216
217 if (ptime == 0)
218 return 0;
219
220 /* Align according to: T - Tlast = k * Tptime */
221 timestamp_delta = timestamp - sstate->last_timestamp;
222
223 return timestamp_delta % ptime;
224}
225
Philipp Maier87bd9be2017-08-22 16:35:41 +0200226/* Check timestamp and sequence number for plausibility */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200227static int check_rtp_timestamp(struct mgcp_endpoint *endp,
228 struct mgcp_rtp_state *state,
229 struct mgcp_rtp_stream_state *sstate,
230 struct mgcp_rtp_end *rtp_end,
231 struct sockaddr_in *addr,
232 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200233 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200234{
235 int32_t tsdelta;
236 int32_t timestamp_error;
237
238 /* Not fully intialized, skip */
239 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
240 return 0;
241
242 if (seq == sstate->last_seq) {
243 if (timestamp != sstate->last_timestamp) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200244 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200245 LOGPENDP(endp, DRTP, LOGL_ERROR,
246 "The %s timestamp delta is != 0 but the sequence "
247 "number %d is the same, "
248 "TS offset: %d, SeqNo offset: %d "
249 "on SSRC: %u timestamp: %u "
250 "from %s:%d\n",
251 text, seq,
252 state->patch.timestamp_offset, state->patch.seq_offset,
253 sstate->ssrc, timestamp, inet_ntoa(addr->sin_addr),
254 ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200255 }
256 return 0;
257 }
258
259 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200260 (int32_t)(timestamp - sstate->last_timestamp) /
261 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200262
263 if (tsdelta == 0) {
264 /* Don't update *tsdelta_out */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200265 LOGPENDP(endp, DRTP, LOGL_NOTICE,
266 "The %s timestamp delta is %d "
267 "on SSRC: %u timestamp: %u "
268 "from %s:%d\n",
269 text, tsdelta,
270 sstate->ssrc, timestamp,
271 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200272
273 return 0;
274 }
275
276 if (sstate->last_tsdelta != tsdelta) {
277 if (sstate->last_tsdelta) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200278 LOGPENDP(endp, DRTP, LOGL_INFO,
279 "The %s timestamp delta changes from %d to %d "
280 "on SSRC: %u timestamp: %u from %s:%d\n",
281 text, sstate->last_tsdelta, tsdelta,
282 sstate->ssrc, timestamp,
283 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200284 }
285 }
286
287 if (tsdelta_out)
288 *tsdelta_out = tsdelta;
289
290 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200291 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200292
293 if (timestamp_error) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200294 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200295 LOGPENDP(endp, DRTP, LOGL_NOTICE,
296 "The %s timestamp has an alignment error of %d "
297 "on SSRC: %u "
298 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
299 "from %s:%d. ptime: %d\n",
300 text, timestamp_error,
301 sstate->ssrc,
302 (int16_t)(seq - sstate->last_seq),
303 (int32_t)(timestamp - sstate->last_timestamp),
304 tsdelta,
305 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
306 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200307 }
308 return 1;
309}
310
311/* Set the timestamp offset according to the packet duration. */
312static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp,
313 struct mgcp_rtp_state *state,
314 struct mgcp_rtp_end *rtp_end,
315 struct sockaddr_in *addr,
316 int16_t delta_seq, uint32_t in_timestamp)
317{
318 int32_t tsdelta = state->packet_duration;
319 int timestamp_offset;
320 uint32_t out_timestamp;
321
322 if (tsdelta == 0) {
323 tsdelta = state->out_stream.last_tsdelta;
324 if (tsdelta != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200325 LOGPENDP(endp, DRTP, LOGL_NOTICE,
326 "A fixed packet duration is not available, "
327 "using last output timestamp delta instead: %d "
328 "from %s:%d\n", tsdelta,
329 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200330 } else {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200331 tsdelta = rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200332 LOGPENDP(endp, DRTP, LOGL_NOTICE,
333 "Fixed packet duration and last timestamp delta "
334 "are not available, "
335 "using fixed 20ms instead: %d "
336 "from %s:%d\n", tsdelta,
337 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200338 }
339 }
340
341 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
342 timestamp_offset = out_timestamp - in_timestamp;
343
Harald Welte33381352017-12-25 09:44:26 +0100344 if (state->patch.timestamp_offset != timestamp_offset) {
345 state->patch.timestamp_offset = timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200346
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200347 LOGPENDP(endp, DRTP, LOGL_NOTICE,
348 "Timestamp offset change on SSRC: %u "
349 "SeqNo delta: %d, TS offset: %d, "
350 "from %s:%d\n", state->in_stream.ssrc,
351 delta_seq, state->patch.timestamp_offset,
352 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200353 }
354
355 return timestamp_offset;
356}
357
358/* Set the timestamp offset according to the packet duration. */
359static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp,
360 struct mgcp_rtp_state *state,
361 struct mgcp_rtp_end *rtp_end,
362 struct sockaddr_in *addr,
363 uint32_t timestamp)
364{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200365 int ts_error = 0;
366 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200367 int ptime = state->packet_duration;
368
369 /* Align according to: T + Toffs - Tlast = k * Tptime */
370
Philipp Maier87bd9be2017-08-22 16:35:41 +0200371 ts_error = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100372 timestamp + state->patch.timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200373
Philipp Maier87bd9be2017-08-22 16:35:41 +0200374 /* If there is an alignment error, we have to compensate it */
375 if (ts_error) {
Harald Welte33381352017-12-25 09:44:26 +0100376 state->patch.timestamp_offset += ptime - ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200377
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200378 LOGPENDP(endp, DRTP, LOGL_NOTICE,
379 "Corrected timestamp alignment error of %d on SSRC: %u "
380 "new TS offset: %d, "
381 "from %s:%d\n",
382 ts_error, state->in_stream.ssrc,
383 state->patch.timestamp_offset, inet_ntoa(addr->sin_addr),
384 ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200385 }
386
Philipp Maier87bd9be2017-08-22 16:35:41 +0200387 /* Check we really managed to compensate the timestamp
388 * offset. There should not be any remaining error, failing
Harald Welte1d1b98f2017-12-25 10:03:40 +0100389 * here would point to a serous problem with the alignment
390 * error computation function */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200391 ts_check = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100392 timestamp + state->patch.timestamp_offset);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200393 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200394
Philipp Maier87bd9be2017-08-22 16:35:41 +0200395 /* Return alignment error before compensation */
396 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200397}
398
Philipp Maier87bd9be2017-08-22 16:35:41 +0200399/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200400 * \param[in] associated endpoint.
401 * \param[in] destination RTP end.
402 * \param[in,out] pointer to buffer with voice data.
403 * \param[in] voice data length.
404 * \param[in] maximum size of caller provided voice data buffer.
405 * \returns ignores input parameters, return always 0. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200406int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
407 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200408 char *data, int *len, int buf_size)
409{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200410 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200411 return 0;
412}
413
Philipp Maier87bd9be2017-08-22 16:35:41 +0200414/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
Philipp Maier0b79d212020-06-18 12:02:49 +0200415 * \param[in] associated endpoint.
416 * \param[in] destination RTP connnection.
417 * \param[in] source RTP connection.
418 * \returns ignores input parameters, return always 0. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200419int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
Philipp Maieracc10352018-07-19 18:07:57 +0200420 struct mgcp_conn_rtp *conn_dst,
421 struct mgcp_conn_rtp *conn_src)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200422{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200423 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200424 return 0;
425}
426
427void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
Philipp Maier58128252019-03-06 11:28:18 +0100428 const struct mgcp_rtp_codec **codec,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200429 const char **fmtp_extra,
430 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200431{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200432 LOGPENDP(endp, DRTP, LOGL_DEBUG, "conn:%s using format defaults\n",
433 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200434
Philipp Maier58128252019-03-06 11:28:18 +0100435 *codec = conn->end.codec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200436 *fmtp_extra = conn->end.fmtp_extra;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200437}
438
Philipp Maier87bd9be2017-08-22 16:35:41 +0200439void mgcp_rtp_annex_count(struct mgcp_endpoint *endp,
440 struct mgcp_rtp_state *state, const uint16_t seq,
441 const int32_t transit, const uint32_t ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200442{
443 int32_t d;
444
445 /* initialize or re-initialize */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100446 if (!state->stats.initialized || state->stats.ssrc != ssrc) {
447 state->stats.initialized = 1;
448 state->stats.base_seq = seq;
449 state->stats.max_seq = seq - 1;
450 state->stats.ssrc = ssrc;
451 state->stats.jitter = 0;
452 state->stats.transit = transit;
453 state->stats.cycles = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200454 } else {
455 uint16_t udelta;
456
Philipp Maier87bd9be2017-08-22 16:35:41 +0200457 /* The below takes the shape of the validation of
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200458 * Appendix A. Check if there is something weird with
459 * the sequence number, otherwise check for a wrap
460 * around in the sequence number.
461 * It can't wrap during the initialization so let's
462 * skip it here. The Appendix A probably doesn't have
Philipp Maier87bd9be2017-08-22 16:35:41 +0200463 * this issue because of the probation. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100464 udelta = seq - state->stats.max_seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200465 if (udelta < RTP_MAX_DROPOUT) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100466 if (seq < state->stats.max_seq)
467 state->stats.cycles += RTP_SEQ_MOD;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200468 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200469 LOGPENDP(endp, DRTP, LOGL_NOTICE,
470 "RTP seqno made a very large jump on delta: %u\n",
471 udelta);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200472 }
473 }
474
Philipp Maier87bd9be2017-08-22 16:35:41 +0200475 /* Calculate the jitter between the two packages. The TS should be
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200476 * taken closer to the read function. This was taken from the
477 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
Philipp Maier87bd9be2017-08-22 16:35:41 +0200478 * resolution. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100479 d = transit - state->stats.transit;
480 state->stats.transit = transit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200481 if (d < 0)
482 d = -d;
Harald Welte49e3d5a2017-12-25 09:47:57 +0100483 state->stats.jitter += d - ((state->stats.jitter + 8) >> 4);
484 state->stats.max_seq = seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200485}
486
Philipp Maier6931f9a2018-07-26 09:29:31 +0200487/* There may be different payload type numbers negotiated for two connections.
488 * Patch the payload type of an RTP packet so that it uses the payload type
489 * that is valid for the destination connection (conn_dst) */
490static int mgcp_patch_pt(struct mgcp_conn_rtp *conn_src,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200491 struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200492{
493 struct rtp_hdr *rtp_hdr;
494 uint8_t pt_in;
495 int pt_out;
496
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200497 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
498 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
499 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200500 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200501 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200502
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200503 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier6931f9a2018-07-26 09:29:31 +0200504
505 pt_in = rtp_hdr->payload_type;
506 pt_out = mgcp_codec_pt_translate(conn_src, conn_dst, pt_in);
507 if (pt_out < 0)
508 return -EINVAL;
509
510 rtp_hdr->payload_type = (uint8_t) pt_out;
511 return 0;
512}
513
Philipp Maier87bd9be2017-08-22 16:35:41 +0200514/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200515 * some of the supported endpoints (e.g. the nanoBTS) can only handle
516 * one source and this code will patch RTP header to appear as if there
517 * is only one source.
518 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200519 * we receive will be seen as a switch in streams. */
520void mgcp_patch_and_count(struct mgcp_endpoint *endp,
521 struct mgcp_rtp_state *state,
522 struct mgcp_rtp_end *rtp_end,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200523 struct sockaddr_in *addr, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200524{
525 uint32_t arrival_time;
526 int32_t transit;
527 uint16_t seq;
528 uint32_t timestamp, ssrc;
529 struct rtp_hdr *rtp_hdr;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200530 int payload = rtp_end->codec->payload_type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200531 unsigned int len = msgb_length(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200532
533 if (len < sizeof(*rtp_hdr))
534 return;
535
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200536 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200537 seq = ntohs(rtp_hdr->sequence);
538 timestamp = ntohl(rtp_hdr->timestamp);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200539 arrival_time = get_current_ts(rtp_end->codec->rate);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200540 ssrc = ntohl(rtp_hdr->ssrc);
541 transit = arrival_time - timestamp;
542
543 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc);
544
545 if (!state->initialized) {
546 state->initialized = 1;
547 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100548 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200549 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200550 state->packet_duration =
551 mgcp_rtp_packet_duration(endp, rtp_end);
Philipp Maier0ec1d4e2018-05-16 11:09:42 +0200552 state->out_stream.last_seq = seq - 1;
553 state->out_stream.ssrc = state->patch.orig_ssrc = ssrc;
554 state->out_stream.last_tsdelta = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200555 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200556 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200557 LOGPENDP(endp, DRTP, LOGL_INFO,
558 "initializing stream, SSRC: %u timestamp: %u "
559 "pkt-duration: %d, from %s:%d\n",
560 state->in_stream.ssrc,
561 state->patch.seq_offset, state->packet_duration,
562 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200563 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200564 state->packet_duration =
Philipp Maierbc0346e2018-06-07 09:52:16 +0200565 rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200566 LOGPENDP(endp, DRTP, LOGL_NOTICE,
567 "fixed packet duration is not available, "
568 "using fixed 20ms instead: %d from %s:%d\n",
569 state->packet_duration,
570 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200571 }
572 } else if (state->in_stream.ssrc != ssrc) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200573 LOGPENDP(endp, DRTP, LOGL_NOTICE,
574 "SSRC changed: %u -> %u "
575 "from %s:%d\n",
576 state->in_stream.ssrc, rtp_hdr->ssrc,
577 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200578
579 state->in_stream.ssrc = ssrc;
580 if (rtp_end->force_constant_ssrc) {
581 int16_t delta_seq;
582
583 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100584 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200585 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200586
587 /* Estimate number of packets that would have been sent */
588 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200589 (arrival_time - state->in_stream.last_arrival_time
590 + state->packet_duration / 2) /
591 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200592
593 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
594 delta_seq, timestamp);
595
Harald Welte33381352017-12-25 09:44:26 +0100596 state->patch.patch_ssrc = 1;
597 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200598 if (rtp_end->force_constant_ssrc != -1)
599 rtp_end->force_constant_ssrc -= 1;
600
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200601 LOGPENDP(endp, DRTP, LOGL_NOTICE,
602 "SSRC patching enabled, SSRC: %u "
603 "SeqNo offset: %d, TS offset: %d "
604 "from %s:%d\n", state->in_stream.ssrc,
605 state->patch.seq_offset, state->patch.timestamp_offset,
606 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200607 }
608
609 state->in_stream.last_tsdelta = 0;
610 } else {
611 /* Compute current per-packet timestamp delta */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200612 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
613 addr, seq, timestamp, "input",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200614 &state->in_stream.last_tsdelta);
615
Harald Welte33381352017-12-25 09:44:26 +0100616 if (state->patch.patch_ssrc)
617 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200618 }
619
620 /* Save before patching */
621 state->in_stream.last_timestamp = timestamp;
622 state->in_stream.last_seq = seq;
623 state->in_stream.last_arrival_time = arrival_time;
624
625 if (rtp_end->force_aligned_timing &&
626 state->out_stream.ssrc == ssrc && state->packet_duration)
627 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200628 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
629 timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200630
631 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100632 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200633 rtp_hdr->ssrc = htonl(ssrc);
634
635 /* Apply the offset and store it back to the packet.
636 * This won't change anything if the offset is 0, so the conditional is
637 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100638 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200639 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100640 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200641 rtp_hdr->timestamp = htonl(timestamp);
642
643 /* Check again, whether the timestamps are still valid */
644 if (state->out_stream.ssrc == ssrc)
645 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
646 addr, seq, timestamp, "output",
647 &state->out_stream.last_tsdelta);
648
649 /* Save output values */
650 state->out_stream.last_seq = seq;
651 state->out_stream.last_timestamp = timestamp;
652 state->out_stream.ssrc = ssrc;
653
654 if (payload < 0)
655 return;
656
657#if 0
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200658 LOGPENDP(endp, DRTP, LOGL_DEBUG, "payload hdr payload %u -> endp payload %u\n",
659 rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200660 rtp_hdr->payload_type = payload;
661#endif
662}
663
Philipp Maier9fc8a022019-02-20 12:26:52 +0100664/* There are different dialects used to format and transfer voice data. When
665 * the receiving end expects GSM-HR data to be formated after RFC 5993, this
666 * function is used to convert between RFC 5993 and TS 101318, which we normally
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200667 * use.
668 * Return 0 on sucess, negative on errors like invalid data length. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200669static int rfc5993_hr_convert(struct mgcp_endpoint *endp, struct msgb *msg)
Philipp Maier9fc8a022019-02-20 12:26:52 +0100670{
Philipp Maier9fc8a022019-02-20 12:26:52 +0100671 struct rtp_hdr *rtp_hdr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200672 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200673 LOGPENDP(endp, DRTP, LOGL_ERROR, "AMR RTP packet too short (%d < %zu)\n",
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200674 msgb_length(msg), sizeof(struct rtp_hdr));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200675 return -EINVAL;
676 }
677
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200678 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100679
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200680 if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr)) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100681 /* TS 101318 encoding => RFC 5993 encoding */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200682 msgb_put(msg, 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100683 memmove(rtp_hdr->data + 1, rtp_hdr->data, GSM_HR_BYTES);
684 rtp_hdr->data[0] = 0x00;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200685 } else if (msgb_length(msg) == GSM_HR_BYTES + sizeof(struct rtp_hdr) + 1) {
Philipp Maier9fc8a022019-02-20 12:26:52 +0100686 /* RFC 5993 encoding => TS 101318 encoding */
687 memmove(rtp_hdr->data, rtp_hdr->data + 1, GSM_HR_BYTES);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200688 msgb_trim(msg, msgb_length(msg) - 1);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100689 } else {
690 /* It is possible that multiple payloads occur in one RTP
691 * packet. This is not supported yet. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200692 LOGPENDP(endp, DRTP, LOGL_ERROR,
693 "cannot figure out how to convert RTP packet\n");
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200694 return -ENOTSUP;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100695 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200696 return 0;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100697}
698
Philipp Maier228e5912019-03-05 13:56:59 +0100699/* For AMR RTP two framing modes are defined RFC3267. There is a bandwith
700 * efficient encoding scheme where all fields are packed together one after
701 * another and an octet aligned mode where all fields are aligned to octet
702 * boundaries. This function is used to convert between the two modes */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200703static int amr_oa_bwe_convert(struct mgcp_endpoint *endp, struct msgb *msg,
Philipp Maier228e5912019-03-05 13:56:59 +0100704 bool target_is_oa)
705{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200706 /* NOTE: the msgb has an allocated length of RTP_BUF_SIZE, so there is
Philipp Maier228e5912019-03-05 13:56:59 +0100707 * plenty of space available to store the slightly larger, converted
708 * data */
Philipp Maier228e5912019-03-05 13:56:59 +0100709 struct rtp_hdr *rtp_hdr;
710 unsigned int payload_len;
711 int rc;
712
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200713 if (msgb_length(msg) < sizeof(struct rtp_hdr)) {
714 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 +0200715 return -EINVAL;
716 }
717
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200718 rtp_hdr = (struct rtp_hdr *)msgb_data(msg);
Philipp Maier228e5912019-03-05 13:56:59 +0100719
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200720 payload_len = msgb_length(msg) - sizeof(struct rtp_hdr);
Philipp Maier228e5912019-03-05 13:56:59 +0100721
722 if (osmo_amr_is_oa(rtp_hdr->data, payload_len)) {
723 if (!target_is_oa)
724 /* Input data is oa an target format is bwe
725 * ==> convert */
726 rc = osmo_amr_oa_to_bwe(rtp_hdr->data, payload_len);
727 else
728 /* Input data is already bew, but we accept it anyway
729 * ==> no conversion needed */
730 rc = payload_len;
731 } else {
732 if (target_is_oa)
733 /* Input data is bwe an target format is oa
734 * ==> convert */
735 rc = osmo_amr_bwe_to_oa(rtp_hdr->data, payload_len,
736 RTP_BUF_SIZE);
737 else
738 /* Input data is already oa, but we accept it anyway
739 * ==> no conversion needed */
740 rc = payload_len;
741 }
742 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200743 LOGPENDP(endp, DRTP, LOGL_ERROR,
744 "AMR RTP packet conversion failed\n");
Philipp Maier228e5912019-03-05 13:56:59 +0100745 return -EINVAL;
746 }
747
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200748 return msgb_trim(msg, rc + sizeof(struct rtp_hdr));
Philipp Maier228e5912019-03-05 13:56:59 +0100749}
750
751/* Check if a conversion between octet-aligned and bandwith-efficient mode is
752 * indicated. */
753static bool amr_oa_bwe_convert_indicated(struct mgcp_rtp_codec *codec)
754{
755 if (codec->param_present == false)
756 return false;
757 if (!codec->param.amr_octet_aligned_present)
758 return false;
759 if (strcmp(codec->subtype_name, "AMR") != 0)
760 return false;
761 return true;
762}
763
764
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200765/* Return whether an RTP packet with AMR payload is in octet-aligned mode.
766 * Return 0 if in bandwidth-efficient mode, 1 for octet-aligned mode, and negative if the RTP data is invalid. */
767static int amr_oa_check(char *data, int len)
Philipp Maier228e5912019-03-05 13:56:59 +0100768{
769 struct rtp_hdr *rtp_hdr;
770 unsigned int payload_len;
771
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200772 if (len < sizeof(struct rtp_hdr))
773 return -EINVAL;
774
Philipp Maier228e5912019-03-05 13:56:59 +0100775 rtp_hdr = (struct rtp_hdr *)data;
776
777 payload_len = len - sizeof(struct rtp_hdr);
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200778 if (payload_len < sizeof(struct amr_hdr))
779 return -EINVAL;
Philipp Maier228e5912019-03-05 13:56:59 +0100780
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200781 return osmo_amr_is_oa(rtp_hdr->data, payload_len) ? 1 : 0;
Philipp Maier228e5912019-03-05 13:56:59 +0100782}
783
Philipp Maier87bd9be2017-08-22 16:35:41 +0200784/* Forward data to a debug tap. This is debug function that is intended for
785 * debugging the voice traffic with tools like gstreamer */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200786static void forward_data(int fd, struct mgcp_rtp_tap *tap, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200787{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100788 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200789
Philipp Maiere6f172d2017-11-07 12:00:01 +0100790 if (!tap->enabled)
791 return;
792
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200793 rc = sendto(fd, msgb_data(msg), msgb_length(msg), 0, (struct sockaddr *)&tap->forward,
Philipp Maiere6f172d2017-11-07 12:00:01 +0100794 sizeof(tap->forward));
795
796 if (rc < 0)
797 LOGP(DRTP, LOGL_ERROR,
798 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200799}
800
Philipp Maier87bd9be2017-08-22 16:35:41 +0200801/*! Send RTP/RTCP data to a specified destination connection.
Philipp Maier0b79d212020-06-18 12:02:49 +0200802 * \param[in] endp associated endpoint (for configuration, logging).
803 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP.
804 * \param[in] spoofed source address (set to NULL to disable).
805 * \param[in] buf buffer that contains the RTP/RTCP data.
806 * \param[in] len length of the buffer that contains the RTP/RTCP data.
807 * \param[in] conn_src associated source connection.
808 * \param[in] conn_dst associated destination connection.
809 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200810int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200811 struct msgb *msg, struct mgcp_conn_rtp *conn_src,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200812 struct mgcp_conn_rtp *conn_dst)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200813{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200814 /*! When no destination connection is available (e.g. when only one
815 * connection in loopback mode exists), then the source connection
816 * shall be specified as destination connection */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200817
Philipp Maier14b27a82020-06-02 20:15:30 +0200818 struct mgcp_trunk *trunk = endp->trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200819 struct mgcp_rtp_end *rtp_end;
820 struct mgcp_rtp_state *rtp_state;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200821 char *dest_name;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200822 int rc;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200823 int len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200824
Philipp Maier87bd9be2017-08-22 16:35:41 +0200825 OSMO_ASSERT(conn_src);
826 OSMO_ASSERT(conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200827
Philipp Maier87bd9be2017-08-22 16:35:41 +0200828 if (is_rtp) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200829 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTP packet...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200830 } else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200831 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTCP packet...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200832 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200833
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200834 LOGPENDP(endp, DRTP, LOGL_DEBUG, "loop:%d, mode:%d%s\n",
Philipp Maier14b27a82020-06-02 20:15:30 +0200835 trunk->audio_loop, conn_src->conn->mode,
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200836 conn_src->conn->mode == MGCP_CONN_LOOPBACK ? " (loopback)" : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200837
Philipp Maier6931f9a2018-07-26 09:29:31 +0200838 /* FIXME: It is legal that the payload type on the egress connection is
839 * different from the payload type that has been negotiated on the
840 * ingress connection. Essentially the codecs are the same so we can
841 * match them and patch the payload type. However, if we can not find
842 * the codec pendant (everything ist equal except the PT), we are of
843 * course unable to patch the payload type. A situation like this
844 * should not occur if transcoding is consequently avoided. Until
845 * we have transcoding support in osmo-mgw we can not resolve this. */
Philipp Maierda895b12018-08-03 12:16:37 +0200846 if (is_rtp) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200847 rc = mgcp_patch_pt(conn_src, conn_dst, msg);
Philipp Maierda895b12018-08-03 12:16:37 +0200848 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200849 LOGPENDP(endp, DRTP, LOGL_DEBUG,
850 "can not patch PT because no suitable egress codec was found.\n");
Philipp Maierda895b12018-08-03 12:16:37 +0200851 }
Philipp Maier6931f9a2018-07-26 09:29:31 +0200852 }
853
Philipp Maier87bd9be2017-08-22 16:35:41 +0200854 /* Note: In case of loopback configuration, both, the source and the
855 * destination will point to the same connection. */
856 rtp_end = &conn_dst->end;
857 rtp_state = &conn_src->state;
858 dest_name = conn_dst->conn->name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200859
860 if (!rtp_end->output_enabled) {
Harald Weltea48ff4a2020-03-08 14:45:08 +0100861 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_DROPPED_PACKETS_CTR);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200862 LOGPENDP(endp, DRTP, LOGL_DEBUG,
863 "output disabled, drop to %s %s "
864 "rtp_port:%u rtcp_port:%u\n",
865 dest_name,
866 inet_ntoa(rtp_end->addr),
867 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200868 );
869 } else if (is_rtp) {
870 int cont;
871 int nbytes = 0;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200872 int buflen = msgb_length(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200873 do {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200874 /* Run transcoder */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200875 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200876 (char*)msgb_data(msg), &buflen,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200877 RTP_BUF_SIZE);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200878 if (cont < 0)
879 break;
880
Philipp Maier87bd9be2017-08-22 16:35:41 +0200881 if (addr)
882 mgcp_patch_and_count(endp, rtp_state, rtp_end,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200883 addr, msg);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100884
Philipp Maier228e5912019-03-05 13:56:59 +0100885 if (amr_oa_bwe_convert_indicated(conn_dst->end.codec)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200886 rc = amr_oa_bwe_convert(endp, msg,
Neels Hofmeyr5a6220f2019-08-20 03:09:04 +0200887 conn_dst->end.codec->param.amr_octet_aligned);
888 if (rc < 0) {
889 LOGPENDP(endp, DRTP, LOGL_ERROR,
890 "Error in AMR octet-aligned <-> bandwidth-efficient mode conversion\n");
891 break;
892 }
Philipp Maier228e5912019-03-05 13:56:59 +0100893 }
894 else if (rtp_end->rfc5993_hr_convert
Philipp Maier9fc8a022019-02-20 12:26:52 +0100895 && strcmp(conn_src->end.codec->subtype_name,
Neels Hofmeyr5a6220f2019-08-20 03:09:04 +0200896 "GSM-HR-08") == 0) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200897 rc = rfc5993_hr_convert(endp, msg);
Neels Hofmeyr5a6220f2019-08-20 03:09:04 +0200898 if (rc < 0) {
899 LOGPENDP(endp, DRTP, LOGL_ERROR, "Error while converting to GSM-HR-08\n");
900 break;
901 }
902 }
Philipp Maier9fc8a022019-02-20 12:26:52 +0100903
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200904 LOGPENDP(endp, DRTP, LOGL_DEBUG,
905 "process/send to %s %s "
906 "rtp_port:%u rtcp_port:%u\n",
907 dest_name, inet_ntoa(rtp_end->addr),
908 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
909 );
Philipp Maier87bd9be2017-08-22 16:35:41 +0200910
911 /* Forward a copy of the RTP data to a debug ip/port */
912 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200913 msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200914
915 /* FIXME: HACK HACK HACK. See OS#2459.
916 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
917 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
918 * cells (as long as we patch only the first RTP payload in each stream).
919 */
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200920 if (!rtp_state->patched_first_rtp_payload
921 && conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200922 uint8_t *data = msgb_data(msg) + 12;
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200923 if (data[0] == 0xe0) {
924 data[0] = 0xe4;
925 data[1] = 0x00;
926 rtp_state->patched_first_rtp_payload = true;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200927 LOGPENDP(endp, DRTP, LOGL_DEBUG,
928 "Patching over first two bytes"
929 " to fake an IuUP Initialization Ack\n");
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200930 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200931 }
932
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200933 len = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr, rtp_end->rtp_port,
934 (char*)msgb_data(msg), msgb_length(msg));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200935
Philipp Maier87bd9be2017-08-22 16:35:41 +0200936 if (len <= 0)
937 return len;
938
Harald Weltea48ff4a2020-03-08 14:45:08 +0100939 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
940 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200941
942 nbytes += len;
943 buflen = cont;
944 } while (buflen > 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200945 return nbytes;
Philipp Maier14b27a82020-06-02 20:15:30 +0200946 } else if (!trunk->omit_rtcp) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200947 LOGPENDP(endp, DRTP, LOGL_DEBUG,
948 "send to %s %s rtp_port:%u rtcp_port:%u\n",
949 dest_name, inet_ntoa(rtp_end->addr),
950 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
951 );
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200952
Philipp Maier87bd9be2017-08-22 16:35:41 +0200953 len = mgcp_udp_send(rtp_end->rtcp.fd,
954 &rtp_end->addr,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200955 rtp_end->rtcp_port, (char*)msgb_data(msg), msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200956
Harald Weltea48ff4a2020-03-08 14:45:08 +0100957 rtpconn_rate_ctr_inc(conn_dst, endp, RTP_PACKETS_TX_CTR);
958 rtpconn_rate_ctr_add(conn_dst, endp, RTP_OCTETS_TX_CTR, len);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200959
960 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200961 }
962
963 return 0;
964}
965
Philipp Maier87bd9be2017-08-22 16:35:41 +0200966/* Check if the origin (addr) matches the address/port data of the RTP
967 * connections. */
968static int check_rtp_origin(struct mgcp_conn_rtp *conn,
969 struct sockaddr_in *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200970{
Harald Weltec26b6652018-10-21 12:01:04 +0200971 if (conn->end.addr.s_addr == 0) {
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200972 switch (conn->conn->mode) {
973 case MGCP_CONN_LOOPBACK:
974 /* HACK: for IuUP, we want to reply with an IuUP Initialization ACK upon the first RTP
975 * message received. We currently hackishly accomplish that by putting the endpoint in
976 * loopback mode and patching over the looped back RTP message to make it look like an
977 * ack. We don't know the femto cell's IP address and port until the RAB Assignment
978 * Response is received, but the nano3G expects an IuUP Initialization Ack before it even
979 * sends the RAB Assignment Response. Hence, if the remote address is 0.0.0.0 and the
980 * MGCP port is in loopback mode, allow looping back the packet to any source. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200981 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
982 "In loopback mode and remote address not set:"
983 " allowing data from address: %s\n", inet_ntoa(addr->sin_addr));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200984 return 0;
985
986 default:
987 /* Receiving early media before the endpoint is configured. Instead of logging
988 * this as an error that occurs on every call, keep it more low profile to not
989 * confuse humans with expected errors. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200990 LOGPCONN(conn->conn, DRTP, LOGL_INFO,
991 "Rx RTP from %s, but remote address not set:"
992 " dropping early media\n", inet_ntoa(addr->sin_addr));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200993 return -1;
994 }
Neels Hofmeyr0063ca22018-07-23 18:12:16 +0200995 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200996
Philipp Maier87bd9be2017-08-22 16:35:41 +0200997 /* Note: Check if the inbound RTP data comes from the same host to
998 * which we send our outgoing RTP traffic. */
Harald Weltec26b6652018-10-21 12:01:04 +0200999 if (conn->end.addr.s_addr != addr->sin_addr.s_addr) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001000 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1001 "data from wrong address: %s, ", inet_ntoa(addr->sin_addr));
Philipp Maierc3413882017-10-27 12:26:54 +02001002 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001003 inet_ntoa(conn->end.addr));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001004 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001005 return -1;
1006 }
1007
Philipp Maier87bd9be2017-08-22 16:35:41 +02001008 /* Note: Usually the remote remote port of the data we receive will be
1009 * the same as the remote port where we transmit outgoing RTP traffic
1010 * to (set by MDCX). We use this to check the origin of the data for
1011 * plausibility. */
1012 if (conn->end.rtp_port != addr->sin_port &&
1013 conn->end.rtcp_port != addr->sin_port) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001014 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1015 "data from wrong source port: %d, ", ntohs(addr->sin_port));
Philipp Maierc3413882017-10-27 12:26:54 +02001016 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001017 "expected: %d for RTP or %d for RTCP\n",
1018 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001019 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001020 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001021 }
1022
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001023 return 0;
1024}
1025
Philipp Maier87bd9be2017-08-22 16:35:41 +02001026/* Check the if the destination address configuration of an RTP connection
1027 * makes sense */
1028static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001029{
Philipp Maiere6df0e42018-05-29 14:03:06 +02001030 /* Note: it is legal to create a connection but never setting a port
1031 * and IP-address for outgoing data. */
1032 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 +02001033 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
1034 "destination IP-address and rtp port is (not yet) known (%s:%u)\n",
1035 inet_ntoa(conn->end.addr), conn->end.rtp_port);
Philipp Maiere6df0e42018-05-29 14:03:06 +02001036 return -1;
1037 }
1038
Philipp Maier87bd9be2017-08-22 16:35:41 +02001039 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001040 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1041 "destination IP-address is invalid (%s:%u)\n",
1042 inet_ntoa(conn->end.addr), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001043 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001044 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001045
1046 if (conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001047 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1048 "destination rtp port is invalid (%s:%u)\n",
1049 inet_ntoa(conn->end.addr), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001050 return -1;
1051 }
1052
1053 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001054}
1055
Philipp Maier4dba7692018-08-03 12:20:52 +02001056/* Do some basic checks to make sure that the RTCP packets we are going to
1057 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001058static int check_rtcp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +02001059{
1060 struct rtcp_hdr *hdr;
1061 unsigned int len;
1062 uint8_t type;
1063
1064 /* RTPC packets that are just a header without data do not make
1065 * any sense. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001066 if (msgb_length(msg) < sizeof(struct rtcp_hdr)) {
1067 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP packet too short (%u < %zu)\n",
1068 msgb_length(msg), sizeof(struct rtcp_hdr));
Philipp Maier4dba7692018-08-03 12:20:52 +02001069 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001070 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001071
1072 /* Make sure that the length of the received packet does not exceed
1073 * the available buffer size */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001074 hdr = (struct rtcp_hdr *)msgb_data(msg);
Philipp Maier4dba7692018-08-03 12:20:52 +02001075 len = (osmo_ntohs(hdr->length) + 1) * 4;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001076 if (len > msgb_length(msg)) {
1077 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header length exceeds packet size (%u > %u)\n",
1078 len, msgb_length(msg));
Philipp Maier4dba7692018-08-03 12:20:52 +02001079 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001080 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001081
1082 /* Make sure we accept only packets that have a proper packet type set
1083 * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
1084 type = hdr->type;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001085 if ((type < 192 || type > 195) && (type < 200 || type > 213)) {
1086 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header: invalid type: %u\n", type);
Philipp Maier4dba7692018-08-03 12:20:52 +02001087 return -EINVAL;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001088 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001089
1090 return 0;
1091}
1092
1093/* Do some basic checks to make sure that the RTP packets we are going to
1094 * process are not complete garbage */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001095static int check_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +02001096{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001097 size_t min_size = sizeof(struct rtp_hdr);
1098 if (msgb_length(msg) < min_size) {
1099 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
1100 msgb_length(msg), min_size);
1101 return -1;
1102 }
Philipp Maier4dba7692018-08-03 12:20:52 +02001103
1104 /* FIXME: Add more checks, the reason why we do not check more than
1105 * the length is because we currently handle IUUP packets as RTP
1106 * packets, so they must pass this check, if we weould be more
1107 * strict here, we would possibly break 3G. (see also FIXME note
1108 * below */
1109
1110 return 0;
1111}
1112
Philipp Maier87bd9be2017-08-22 16:35:41 +02001113/* Send RTP data. Possible options are standard RTP packet
1114 * transmission or trsmission via an osmux connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001115static int mgcp_send_rtp(struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001116{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001117 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1118 enum rtp_proto proto = mc->proto;
1119 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1120 struct sockaddr_in *from_addr = mc->from_addr;
1121 struct mgcp_endpoint *endp = conn_src->conn->endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001122
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001123 LOGPENDP(endp, DRTP, LOGL_DEBUG, "destin conn:%s\n",
1124 mgcp_conn_dump(conn_dst->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001125
1126 /* Before we try to deliver the packet, we check if the destination
1127 * port and IP-Address make sense at all. If not, we will be unable
1128 * to deliver the packet. */
1129 if (check_rtp_destin(conn_dst) != 0)
1130 return -1;
1131
1132 /* Depending on the RTP connection type, deliver the RTP packet to the
1133 * destination connection. */
1134 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001135 case MGCP_RTP_DEFAULT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001136 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1137 "endpoint type is MGCP_RTP_DEFAULT, "
1138 "using mgcp_send() to forward data directly\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001139 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001140 from_addr, msg, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001141 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001142 case MGCP_OSMUX_BSC:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001143 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1144 "endpoint type is MGCP_OSMUX_BSC_NAT, "
1145 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n");
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001146 return osmux_xfrm_to_osmux((char*)msgb_data(msg), msgb_length(msg), conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001147 }
1148
Philipp Maier87bd9be2017-08-22 16:35:41 +02001149 /* If the data has not been handled/forwarded until here, it will
1150 * be discarded, this should not happen, normally the MGCP type
1151 * should be properly set */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001152 LOGPENDP(endp, DRTP, LOGL_ERROR, "bad MGCP type -- data discarded!\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001153
1154 return -1;
1155}
1156
1157/*! dispatch incoming RTP packet to opposite RTP connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001158 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1159 * \param[in] addr socket address where the RTP packet has been received from.
1160 * \param[in] buf buffer that hold the RTP payload.
1161 * \param[in] buf_size size data length of buf.
1162 * \param[in] conn originating connection.
1163 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001164int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001165{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001166 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1167 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1168 struct mgcp_conn *conn = conn_src->conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001169 struct mgcp_conn *conn_dst;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001170 struct sockaddr_in *from_addr = mc->from_addr;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001171
1172 /*! NOTE: This callback function implements the endpoint specific
Alexander Chemeris61cf9bb2020-05-11 18:13:53 +03001173 * dispatch behaviour of an rtp bridge/proxy endpoint. It is assumed
Philipp Maier87bd9be2017-08-22 16:35:41 +02001174 * that the endpoint will hold only two connections. This premise
1175 * is used to determine the opposite connection (it is always the
1176 * connection that is not the originating connection). Once the
1177 * destination connection is known the RTP packet is sent via
1178 * the destination connection. */
1179
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001180
1181 /* Check if the connection is in loopback mode, if yes, just send the
1182 * incoming data back to the origin */
1183 if (conn->mode == MGCP_CONN_LOOPBACK) {
1184 /* When we are in loopback mode, we loop back all incoming
1185 * packets back to their origin. We will use the originating
1186 * address data from the UDP packet header to patch the
1187 * outgoing address in connection on the fly */
1188 if (conn->u.rtp.end.rtp_port == 0) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001189 conn->u.rtp.end.addr = from_addr->sin_addr;
1190 conn->u.rtp.end.rtp_port = from_addr->sin_port;
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001191 }
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001192 return mgcp_send_rtp(conn_src, msg);
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001193 }
1194
Philipp Maier87bd9be2017-08-22 16:35:41 +02001195 /* Find a destination connection. */
1196 /* NOTE: This code path runs every time an RTP packet is received. The
1197 * function mgcp_find_dst_conn() we use to determine the detination
1198 * connection will iterate the connection list inside the endpoint.
1199 * Since list iterations are quite costly, we will figure out the
1200 * destination only once and use the optional private data pointer of
1201 * the connection to cache the destination connection pointer. */
1202 if (!conn->priv) {
1203 conn_dst = mgcp_find_dst_conn(conn);
1204 conn->priv = conn_dst;
1205 } else {
1206 conn_dst = (struct mgcp_conn *)conn->priv;
1207 }
1208
1209 /* There is no destination conn, stop here */
1210 if (!conn_dst) {
Alexander Chemerisebb9bf32020-05-11 18:14:31 +03001211 LOGPCONN(conn, DRTP, LOGL_DEBUG,
1212 "no connection to forward an incoming RTP packet to\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001213 return -1;
1214 }
1215
1216 /* The destination conn is not an RTP connection */
1217 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001218 LOGPCONN(conn, DRTP, LOGL_ERROR,
1219 "unable to find suitable destination conn\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001220 return -1;
1221 }
1222
1223 /* Dispatch RTP packet to destination RTP connection */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001224 return mgcp_send_rtp(&conn_dst->u.rtp, msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001225}
1226
Philipp Maier0996a1e2020-06-10 15:27:14 +02001227/*! dispatch incoming RTP packet to E1 subslot, handle RTCP packets locally.
1228 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP).
1229 * \param[in] addr socket address where the RTP packet has been received from.
1230 * \param[in] buf buffer that hold the RTP payload.
1231 * \param[in] buf_size size data length of buf.
1232 * \param[in] conn originating connection.
1233 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001234int mgcp_dispatch_e1_bridge_cb(struct msgb *msg)
Philipp Maier0996a1e2020-06-10 15:27:14 +02001235{
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001236 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1237 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1238 struct mgcp_conn *conn = conn_src->conn;
1239
Philipp Maier0996a1e2020-06-10 15:27:14 +02001240 /* FIXME: integrate E1 support from libsomoabis, also implement
1241 * handling for RTCP packets, which can not converted to E1. */
1242 LOGPCONN(conn, DRTP, LOGL_FATAL,
1243 "cannot dispatch! E1 support is not implemented yet!\n");
1244 return -1;
1245}
1246
Philipp Maierdf5d2192018-01-24 11:39:32 +01001247/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1248 * \param[in] endp Endpoint on which the connection resides.
Philipp Maier08eb9352020-06-18 11:55:35 +02001249 * \param[in] conn Connection that is about to be removed (ignored). */
Philipp Maierdf5d2192018-01-24 11:39:32 +01001250void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1251{
1252 struct mgcp_conn *conn_cleanup;
1253
1254 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1255 * pointer to the destination connection, so that we do not have
1256 * to go through the list every time an RTP packet arrives. To prevent
1257 * a use-after-free situation we invalidate this information for all
1258 * connections present when one connection is removed from the
1259 * endpoint. */
1260 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
1261 conn_cleanup->priv = NULL;
1262 }
1263}
1264
Philipp Maier0996a1e2020-06-10 15:27:14 +02001265/*! cleanup an endpoint when a connection on an E1 endpoint is removed.
1266 * \param[in] endp Endpoint on which the connection resides.
1267 * \param[in] conn Connection that is about to be removed (ignored). */
1268void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1269{
1270 LOGPCONN(conn, DRTP, LOGL_FATAL,
1271 "cannot dispatch! E1 support is not implemented yet!\n");
1272}
1273
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001274static bool is_dummy_msg(enum rtp_proto proto, struct msgb *msg)
1275{
1276 return msgb_length(msg) == 1 && msgb_data(msg)[0] == MGCP_DUMMY_LOAD;
1277}
1278
Philipp Maier87bd9be2017-08-22 16:35:41 +02001279/* Handle incoming RTP data from NET */
1280static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1281{
1282 /* NOTE: This is a generic implementation. RTP data is received. In
1283 * case of loopback the data is just sent back to its origin. All
1284 * other cases implement endpoint specific behaviour (e.g. how is the
1285 * destination connection determined?). That specific behaviour is
1286 * implemented by the callback function that is called at the end of
1287 * the function */
1288
1289 struct mgcp_conn_rtp *conn_src;
1290 struct mgcp_endpoint *endp;
1291 struct sockaddr_in addr;
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001292 socklen_t slen = sizeof(addr);
1293 int ret;
1294 enum rtp_proto proto;
1295 struct osmo_rtp_msg_ctx *mc;
1296 struct msgb *msg = msgb_alloc(RTP_BUF_SIZE, "RTP-rx");
1297 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001298
1299 conn_src = (struct mgcp_conn_rtp *)fd->data;
1300 OSMO_ASSERT(conn_src);
1301 endp = conn_src->conn->endp;
1302 OSMO_ASSERT(endp);
1303
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001304 proto = (fd == &conn_src->end.rtp)? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001305
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001306 ret = recvfrom(fd->fd, msgb_data(msg), msg->data_len, 0, (struct sockaddr *)&addr, &slen);
1307
1308 if (ret <= 0) {
1309 LOG_CONN_RTP(conn_src, LOGL_ERROR, "recvfrom error: %s\n", strerror(errno));
1310 rc = -1;
1311 goto out;
1312 }
1313
1314 msgb_put(msg, ret);
1315
1316 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "%s: rx %u bytes from %s:%u\n",
1317 proto == MGCP_PROTO_RTP ? "RTP" : "RTPC",
1318 msgb_length(msg), inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
1319
1320 if ((proto == MGCP_PROTO_RTP && check_rtp(conn_src, msg))
1321 || (proto == MGCP_PROTO_RTCP && check_rtcp(conn_src, msg))) {
1322 /* Logging happened in the two check_ functions */
1323 rc = -1;
1324 goto out;
1325 }
1326
1327 if (is_dummy_msg(proto, msg)) {
1328 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx dummy packet (dropped)\n");
1329 rc = 0;
1330 goto out;
1331 }
1332
1333 /* Since the msgb remains owned and freed by this function, the msg ctx data struct can just be on the stack and
1334 * needs not be allocated with the msgb. */
1335 mc = OSMO_RTP_MSG_CTX(msg);
1336 *mc = (struct osmo_rtp_msg_ctx){
1337 .proto = proto,
1338 .conn_src = conn_src,
1339 .from_addr = &addr,
1340 };
1341 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "msg ctx: %d %p %s\n",
1342 mc->proto, mc->conn_src,
1343 osmo_hexdump((void*)mc->from_addr, sizeof(struct sockaddr_in)));
1344
1345 /* Increment RX statistics */
1346 rate_ctr_inc(&conn_src->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR]);
1347 rate_ctr_add(&conn_src->rate_ctr_group->ctr[RTP_OCTETS_RX_CTR], msgb_length(msg));
1348 /* FIXME: count RTP and RTCP separately, also count IuUP payload-less separately */
1349
1350 /* Forward a copy of the RTP data to a debug ip/port */
1351 forward_data(fd->fd, &conn_src->tap_in, msg);
1352
1353 rc = rx_rtp(msg);
1354
1355out:
1356 msgb_free(msg);
1357 return rc;
1358}
1359
1360static int rx_rtp(struct msgb *msg)
1361{
1362 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
1363 struct mgcp_conn_rtp *conn_src = mc->conn_src;
1364 struct sockaddr_in *from_addr = mc->from_addr;
1365 struct mgcp_conn *conn = conn_src->conn;
1366 struct mgcp_trunk *trunk = conn->endp->trunk;
1367
1368 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx_rtp(%u bytes)\n", msgb_length(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001369
Oliver Smithe36b7752019-01-22 16:31:36 +01001370 mgcp_conn_watchdog_kick(conn_src->conn);
1371
Philipp Maier228e5912019-03-05 13:56:59 +01001372 /* If AMR is configured for the ingress connection a conversion of the
1373 * framing mode (octet-aligned vs. bandwith-efficient is explicitly
1374 * define, then we check if the incoming payload matches that
1375 * expectation. */
1376 if (amr_oa_bwe_convert_indicated(conn_src->end.codec)) {
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001377 int oa = amr_oa_check((char*)msgb_data(msg), msgb_length(msg));
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +02001378 if (oa < 0)
1379 return -1;
1380 if (((bool)oa) != conn_src->end.codec->param.amr_octet_aligned)
Philipp Maier228e5912019-03-05 13:56:59 +01001381 return -1;
1382 }
1383
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001384 /* Check if the origin of the RTP packet seems plausible */
1385 if (!trunk->rtp_accept_all && check_rtp_origin(conn_src, from_addr))
1386 return -1;
1387
Philipp Maier87bd9be2017-08-22 16:35:41 +02001388 /* Execute endpoint specific implementation that handles the
1389 * dispatching of the RTP data */
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +02001390 return conn->endp->type->dispatch_rtp_cb(msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001391}
1392
1393/*! set IP Type of Service parameter.
Philipp Maier0b79d212020-06-18 12:02:49 +02001394 * \param[in] fd associated file descriptor.
1395 * \param[in] tos dscp value.
1396 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001397int mgcp_set_ip_tos(int fd, int tos)
1398{
1399 int ret;
1400 ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1401
1402 if (ret < 0)
1403 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001404 return 0;
1405}
1406
Philipp Maier87bd9be2017-08-22 16:35:41 +02001407/*! bind RTP port to osmo_fd.
Philipp Maier0b79d212020-06-18 12:02:49 +02001408 * \param[in] source_addr source (local) address to bind on.
1409 * \param[in] fd associated file descriptor.
1410 * \param[in] port to bind on.
1411 * \returns 0 on success, -1 on ERROR. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001412int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
1413{
Harald Welte8890dfa2017-11-17 15:09:30 +01001414 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001415
Harald Welte8890dfa2017-11-17 15:09:30 +01001416 rc = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
1417 NULL, 0, OSMO_SOCK_F_BIND);
1418 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001419 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001420 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001421 return -1;
1422 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001423 fd->fd = rc;
1424 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001425
1426 return 0;
1427}
1428
Philipp Maier87bd9be2017-08-22 16:35:41 +02001429/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001430static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001431 struct mgcp_rtp_end *rtp_end, struct mgcp_endpoint *endp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001432{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001433 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1434 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1435
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001436 if (mgcp_create_bind(source_addr, &rtp_end->rtp,
1437 rtp_end->local_port) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001438 LOGPENDP(endp, DRTP, LOGL_ERROR,
1439 "failed to create RTP port: %s:%d\n",
1440 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001441 goto cleanup0;
1442 }
1443
1444 if (mgcp_create_bind(source_addr, &rtp_end->rtcp,
1445 rtp_end->local_port + 1) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001446 LOGPENDP(endp, DRTP, LOGL_ERROR,
1447 "failed to create RTCP port: %s:%d\n",
1448 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001449 goto cleanup1;
1450 }
1451
Philipp Maier87bd9be2017-08-22 16:35:41 +02001452 /* Set Type of Service (DSCP-Value) as configured via VTY */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001453 mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
1454 mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
1455
Pau Espin Pedrola7152e02020-05-09 19:15:50 +02001456 rtp_end->rtp.when = OSMO_FD_READ;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001457 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001458 LOGPENDP(endp, DRTP, LOGL_ERROR,
1459 "failed to register RTP port %d\n",
1460 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001461 goto cleanup2;
1462 }
1463
Pau Espin Pedrola7152e02020-05-09 19:15:50 +02001464 rtp_end->rtcp.when = OSMO_FD_READ;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001465 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001466 LOGPENDP(endp, DRTP, LOGL_ERROR,
1467 "failed to register RTCP port %d\n",
1468 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001469 goto cleanup3;
1470 }
1471
1472 return 0;
1473
1474cleanup3:
1475 osmo_fd_unregister(&rtp_end->rtp);
1476cleanup2:
1477 close(rtp_end->rtcp.fd);
1478 rtp_end->rtcp.fd = -1;
1479cleanup1:
1480 close(rtp_end->rtp.fd);
1481 rtp_end->rtp.fd = -1;
1482cleanup0:
1483 return -1;
1484}
1485
Philipp Maier87bd9be2017-08-22 16:35:41 +02001486/*! bind RTP port to endpoint/connection.
Philipp Maier0b79d212020-06-18 12:02:49 +02001487 * \param[in] endp endpoint that holds the RTP connection.
1488 * \param[in] rtp_port port number to bind on.
1489 * \param[in] conn associated RTP connection.
1490 * \returns 0 on success, -1 on ERROR. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001491int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1492 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001493{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001494 char name[512];
1495 struct mgcp_rtp_end *end;
Philipp Maier1cb1e382017-11-02 17:16:04 +01001496 char local_ip_addr[INET_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001497
Philipp Maier01d24a32017-11-21 17:26:09 +01001498 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001499 end = &conn->end;
1500
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001501 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001502 LOGPENDP(endp, DRTP, LOGL_ERROR, "%u was already bound on conn:%s\n",
1503 rtp_port, mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001504
1505 /* Double bindings should never occour! Since we always allocate
1506 * connections dynamically and free them when they are not
1507 * needed anymore, there must be no previous binding leftover.
1508 * Should there be a connection bound twice, we have a serious
1509 * problem and must exit immediately! */
1510 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001511 }
1512
1513 end->local_port = rtp_port;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001514 end->rtp.cb = rtp_data_net;
1515 end->rtp.data = conn;
1516 end->rtcp.data = conn;
1517 end->rtcp.cb = rtp_data_net;
1518
Philipp Maier1cb1e382017-11-02 17:16:04 +01001519 mgcp_get_local_addr(local_ip_addr, conn);
1520
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001521 return bind_rtp(endp->cfg, local_ip_addr, end, endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001522}
1523
Philipp Maier87bd9be2017-08-22 16:35:41 +02001524/*! free allocated RTP and RTCP ports.
1525 * \param[in] end RTP end */
1526void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001527{
1528 if (end->rtp.fd != -1) {
1529 close(end->rtp.fd);
1530 end->rtp.fd = -1;
1531 osmo_fd_unregister(&end->rtp);
1532 }
1533
1534 if (end->rtcp.fd != -1) {
1535 close(end->rtcp.fd);
1536 end->rtcp.fd = -1;
1537 osmo_fd_unregister(&end->rtcp);
1538 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001539}