blob: bb29d2bc14bc5953fc7f98a6975ca677025deaf8 [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 Maier6931f9a2018-07-26 09:29:31 +020045#include <osmocom/mgcp/mgcp_codec.h>
Philipp Maierc3413882017-10-27 12:26:54 +020046#include <osmocom/mgcp/debug.h>
Philipp Maier9fc8a022019-02-20 12:26:52 +010047#include <osmocom/codec/codec.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020048
Philipp Maier6931f9a2018-07-26 09:29:31 +020049
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020050#define RTP_SEQ_MOD (1 << 16)
51#define RTP_MAX_DROPOUT 3000
52#define RTP_MAX_MISORDER 100
53#define RTP_BUF_SIZE 4096
54
55enum {
56 MGCP_PROTO_RTP,
57 MGCP_PROTO_RTCP,
58};
59
Philipp Maier1cb1e382017-11-02 17:16:04 +010060/*! Determine the local rtp bind IP-address.
61 * \param[out] addr caller provided memory to store the resulting IP-Address
62 * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters
63 *
64 * The local bind IP-address is automatically selected by probing the
65 * IP-Address of the interface that is pointing towards the remote IP-Address,
66 * if no remote IP-Address is known yet, the statically configured
67 * IP-Addresses are used as fallback. */
68void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn)
69{
70
71 struct mgcp_endpoint *endp;
72 int rc;
73 endp = conn->conn->endp;
74
75 /* Try probing the local IP-Address */
76 if (endp->cfg->net_ports.bind_addr_probe && conn->end.addr.s_addr != 0) {
77 rc = osmo_sock_local_ip(addr, inet_ntoa(conn->end.addr));
78 if (rc < 0)
Pau Espin Pedrol3239f622019-04-24 18:47:46 +020079 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
80 "local interface auto detection failed, using configured addresses...\n");
Philipp Maier1cb1e382017-11-02 17:16:04 +010081 else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +020082 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
83 "selected local rtp bind ip %s by probing using remote ip %s\n",
84 addr, inet_ntoa(conn->end.addr));
Philipp Maier1cb1e382017-11-02 17:16:04 +010085 return;
86 }
87 }
88
Pau Espin Pedrola93c6e92019-05-06 15:23:57 +020089 /* Select from preconfigured IP-Addresses. We don't have bind_addr for Osmux (yet?). */
Philipp Maier1cb1e382017-11-02 17:16:04 +010090 if (endp->cfg->net_ports.bind_addr) {
91 /* Check there is a bind IP for the RTP traffic configured,
92 * if so, use that IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +010093 osmo_strlcpy(addr, endp->cfg->net_ports.bind_addr, INET_ADDRSTRLEN);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +020094 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
95 "using configured rtp bind ip as local bind ip %s\n",
96 addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +010097 } else {
98 /* No specific bind IP is configured for the RTP traffic, so
99 * assume the IP where we listen for incoming MGCP messages
100 * as bind IP */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100101 osmo_strlcpy(addr, endp->cfg->source_addr, INET_ADDRSTRLEN);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200102 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
103 "using mgcp bind ip as local rtp bind ip: %s\n", addr);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100104 }
105}
106
Philipp Maier87bd9be2017-08-22 16:35:41 +0200107/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200108 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +0200109 * 1/codec_rate seconds. */
110static uint32_t get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200111{
112 struct timespec tp;
113 uint64_t ret;
114
Philipp Maier87bd9be2017-08-22 16:35:41 +0200115 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200116 return 0;
117
118 memset(&tp, 0, sizeof(tp));
119 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maierc3413882017-10-27 12:26:54 +0200120 LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200121
122 /* convert it to 1/unit seconds */
123 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200124 ret *= codec_rate;
125 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200126
127 return ret;
128}
129
Philipp Maier87bd9be2017-08-22 16:35:41 +0200130/*! send udp packet.
131 * \param[in] fd associated file descriptor
132 * \param[in] addr destination ip-address
Pau Espin Pedrolf1d301a2019-05-13 16:35:53 +0200133 * \param[in] port destination UDP port (network byte order)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200134 * \param[in] buf buffer that holds the data to be send
135 * \param[in] len length of the data to be sent
136 * \returns bytes sent, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200137int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
138{
139 struct sockaddr_in out;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200140
Philipp Maierc3413882017-10-27 12:26:54 +0200141 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200142 "sending %i bytes length packet to %s:%u ...\n",
143 len, inet_ntoa(*addr), ntohs(port));
144
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200145 out.sin_family = AF_INET;
146 out.sin_port = port;
147 memcpy(&out.sin_addr, addr, sizeof(*addr));
148
149 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
150}
151
Philipp Maier87bd9be2017-08-22 16:35:41 +0200152/*! send RTP dummy packet (to keep NAT connection open).
153 * \param[in] endp mcgp endpoint that holds the RTP connection
154 * \param[in] conn associated RTP connection
155 * \returns bytes sent, -1 on error */
156int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200157{
158 static char buf[] = { MGCP_DUMMY_LOAD };
159 int rc;
160 int was_rtcp = 0;
161
Philipp Maier87bd9be2017-08-22 16:35:41 +0200162 OSMO_ASSERT(endp);
163 OSMO_ASSERT(conn);
164
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200165 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,"sending dummy packet... %s\n",
166 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200167
168 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
169 conn->end.rtp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200170
171 if (rc == -1)
172 goto failed;
173
174 if (endp->tcfg->omit_rtcp)
175 return rc;
176
177 was_rtcp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200178 rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
179 conn->end.rtcp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200180
181 if (rc >= 0)
182 return rc;
183
184failed:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200185 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
186 "Failed to send dummy %s packet.\n",
187 was_rtcp ? "RTCP" : "RTP");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200188
189 return -1;
190}
191
Philipp Maier87bd9be2017-08-22 16:35:41 +0200192/* Compute timestamp alignment error */
193static int32_t ts_alignment_error(struct mgcp_rtp_stream_state *sstate,
194 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200195{
196 int32_t timestamp_delta;
197
198 if (ptime == 0)
199 return 0;
200
201 /* Align according to: T - Tlast = k * Tptime */
202 timestamp_delta = timestamp - sstate->last_timestamp;
203
204 return timestamp_delta % ptime;
205}
206
Philipp Maier87bd9be2017-08-22 16:35:41 +0200207/* Check timestamp and sequence number for plausibility */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200208static int check_rtp_timestamp(struct mgcp_endpoint *endp,
209 struct mgcp_rtp_state *state,
210 struct mgcp_rtp_stream_state *sstate,
211 struct mgcp_rtp_end *rtp_end,
212 struct sockaddr_in *addr,
213 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200214 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200215{
216 int32_t tsdelta;
217 int32_t timestamp_error;
218
219 /* Not fully intialized, skip */
220 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
221 return 0;
222
223 if (seq == sstate->last_seq) {
224 if (timestamp != sstate->last_timestamp) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200225 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200226 LOGPENDP(endp, DRTP, LOGL_ERROR,
227 "The %s timestamp delta is != 0 but the sequence "
228 "number %d is the same, "
229 "TS offset: %d, SeqNo offset: %d "
230 "on SSRC: %u timestamp: %u "
231 "from %s:%d\n",
232 text, seq,
233 state->patch.timestamp_offset, state->patch.seq_offset,
234 sstate->ssrc, timestamp, inet_ntoa(addr->sin_addr),
235 ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200236 }
237 return 0;
238 }
239
240 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200241 (int32_t)(timestamp - sstate->last_timestamp) /
242 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200243
244 if (tsdelta == 0) {
245 /* Don't update *tsdelta_out */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200246 LOGPENDP(endp, DRTP, LOGL_NOTICE,
247 "The %s timestamp delta is %d "
248 "on SSRC: %u timestamp: %u "
249 "from %s:%d\n",
250 text, tsdelta,
251 sstate->ssrc, timestamp,
252 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200253
254 return 0;
255 }
256
257 if (sstate->last_tsdelta != tsdelta) {
258 if (sstate->last_tsdelta) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200259 LOGPENDP(endp, DRTP, LOGL_INFO,
260 "The %s timestamp delta changes from %d to %d "
261 "on SSRC: %u timestamp: %u from %s:%d\n",
262 text, sstate->last_tsdelta, tsdelta,
263 sstate->ssrc, timestamp,
264 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200265 }
266 }
267
268 if (tsdelta_out)
269 *tsdelta_out = tsdelta;
270
271 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200272 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200273
274 if (timestamp_error) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200275 rate_ctr_inc(sstate->err_ts_ctr);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200276 LOGPENDP(endp, DRTP, LOGL_NOTICE,
277 "The %s timestamp has an alignment error of %d "
278 "on SSRC: %u "
279 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
280 "from %s:%d. ptime: %d\n",
281 text, timestamp_error,
282 sstate->ssrc,
283 (int16_t)(seq - sstate->last_seq),
284 (int32_t)(timestamp - sstate->last_timestamp),
285 tsdelta,
286 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
287 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200288 }
289 return 1;
290}
291
292/* Set the timestamp offset according to the packet duration. */
293static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp,
294 struct mgcp_rtp_state *state,
295 struct mgcp_rtp_end *rtp_end,
296 struct sockaddr_in *addr,
297 int16_t delta_seq, uint32_t in_timestamp)
298{
299 int32_t tsdelta = state->packet_duration;
300 int timestamp_offset;
301 uint32_t out_timestamp;
302
303 if (tsdelta == 0) {
304 tsdelta = state->out_stream.last_tsdelta;
305 if (tsdelta != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200306 LOGPENDP(endp, DRTP, LOGL_NOTICE,
307 "A fixed packet duration is not available, "
308 "using last output timestamp delta instead: %d "
309 "from %s:%d\n", tsdelta,
310 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200311 } else {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200312 tsdelta = rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200313 LOGPENDP(endp, DRTP, LOGL_NOTICE,
314 "Fixed packet duration and last timestamp delta "
315 "are not available, "
316 "using fixed 20ms instead: %d "
317 "from %s:%d\n", tsdelta,
318 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200319 }
320 }
321
322 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
323 timestamp_offset = out_timestamp - in_timestamp;
324
Harald Welte33381352017-12-25 09:44:26 +0100325 if (state->patch.timestamp_offset != timestamp_offset) {
326 state->patch.timestamp_offset = timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200327
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200328 LOGPENDP(endp, DRTP, LOGL_NOTICE,
329 "Timestamp offset change on SSRC: %u "
330 "SeqNo delta: %d, TS offset: %d, "
331 "from %s:%d\n", state->in_stream.ssrc,
332 delta_seq, state->patch.timestamp_offset,
333 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200334 }
335
336 return timestamp_offset;
337}
338
339/* Set the timestamp offset according to the packet duration. */
340static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp,
341 struct mgcp_rtp_state *state,
342 struct mgcp_rtp_end *rtp_end,
343 struct sockaddr_in *addr,
344 uint32_t timestamp)
345{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200346 int ts_error = 0;
347 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200348 int ptime = state->packet_duration;
349
350 /* Align according to: T + Toffs - Tlast = k * Tptime */
351
Philipp Maier87bd9be2017-08-22 16:35:41 +0200352 ts_error = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100353 timestamp + state->patch.timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200354
Philipp Maier87bd9be2017-08-22 16:35:41 +0200355 /* If there is an alignment error, we have to compensate it */
356 if (ts_error) {
Harald Welte33381352017-12-25 09:44:26 +0100357 state->patch.timestamp_offset += ptime - ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200358
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200359 LOGPENDP(endp, DRTP, LOGL_NOTICE,
360 "Corrected timestamp alignment error of %d on SSRC: %u "
361 "new TS offset: %d, "
362 "from %s:%d\n",
363 ts_error, state->in_stream.ssrc,
364 state->patch.timestamp_offset, inet_ntoa(addr->sin_addr),
365 ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200366 }
367
Philipp Maier87bd9be2017-08-22 16:35:41 +0200368 /* Check we really managed to compensate the timestamp
369 * offset. There should not be any remaining error, failing
Harald Welte1d1b98f2017-12-25 10:03:40 +0100370 * here would point to a serous problem with the alignment
371 * error computation function */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200372 ts_check = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100373 timestamp + state->patch.timestamp_offset);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200374 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200375
Philipp Maier87bd9be2017-08-22 16:35:41 +0200376 /* Return alignment error before compensation */
377 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200378}
379
Philipp Maier87bd9be2017-08-22 16:35:41 +0200380/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
381 * \param[in] associated endpoint
382 * \param[in] destination RTP end
383 * \param[in,out] pointer to buffer with voice data
384 * \param[in] voice data length
Harald Welte1d1b98f2017-12-25 10:03:40 +0100385 * \param[in] maximum size of caller provided voice data buffer
Philipp Maier87bd9be2017-08-22 16:35:41 +0200386 * \returns ignores input parameters, return always 0 */
387int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
388 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200389 char *data, int *len, int buf_size)
390{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200391 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200392 return 0;
393}
394
Philipp Maier87bd9be2017-08-22 16:35:41 +0200395/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
396 * \param[in] associated endpoint
Philipp Maieracc10352018-07-19 18:07:57 +0200397 * \param[in] destination RTP connnection
398 * \param[in] source RTP connection
Philipp Maier87bd9be2017-08-22 16:35:41 +0200399 * \returns ignores input parameters, return always 0 */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200400int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
Philipp Maieracc10352018-07-19 18:07:57 +0200401 struct mgcp_conn_rtp *conn_dst,
402 struct mgcp_conn_rtp *conn_src)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200403{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200404 LOGPENDP(endp, DRTP, LOGL_DEBUG, "transcoding disabled\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200405 return 0;
406}
407
408void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
Philipp Maier58128252019-03-06 11:28:18 +0100409 const struct mgcp_rtp_codec **codec,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200410 const char **fmtp_extra,
411 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200412{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200413 LOGPENDP(endp, DRTP, LOGL_DEBUG, "conn:%s using format defaults\n",
414 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200415
Philipp Maier58128252019-03-06 11:28:18 +0100416 *codec = conn->end.codec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200417 *fmtp_extra = conn->end.fmtp_extra;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200418}
419
Philipp Maier87bd9be2017-08-22 16:35:41 +0200420void mgcp_rtp_annex_count(struct mgcp_endpoint *endp,
421 struct mgcp_rtp_state *state, const uint16_t seq,
422 const int32_t transit, const uint32_t ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200423{
424 int32_t d;
425
426 /* initialize or re-initialize */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100427 if (!state->stats.initialized || state->stats.ssrc != ssrc) {
428 state->stats.initialized = 1;
429 state->stats.base_seq = seq;
430 state->stats.max_seq = seq - 1;
431 state->stats.ssrc = ssrc;
432 state->stats.jitter = 0;
433 state->stats.transit = transit;
434 state->stats.cycles = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200435 } else {
436 uint16_t udelta;
437
Philipp Maier87bd9be2017-08-22 16:35:41 +0200438 /* The below takes the shape of the validation of
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200439 * Appendix A. Check if there is something weird with
440 * the sequence number, otherwise check for a wrap
441 * around in the sequence number.
442 * It can't wrap during the initialization so let's
443 * skip it here. The Appendix A probably doesn't have
Philipp Maier87bd9be2017-08-22 16:35:41 +0200444 * this issue because of the probation. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100445 udelta = seq - state->stats.max_seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200446 if (udelta < RTP_MAX_DROPOUT) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100447 if (seq < state->stats.max_seq)
448 state->stats.cycles += RTP_SEQ_MOD;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200449 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200450 LOGPENDP(endp, DRTP, LOGL_NOTICE,
451 "RTP seqno made a very large jump on delta: %u\n",
452 udelta);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200453 }
454 }
455
Philipp Maier87bd9be2017-08-22 16:35:41 +0200456 /* Calculate the jitter between the two packages. The TS should be
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200457 * taken closer to the read function. This was taken from the
458 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
Philipp Maier87bd9be2017-08-22 16:35:41 +0200459 * resolution. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100460 d = transit - state->stats.transit;
461 state->stats.transit = transit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200462 if (d < 0)
463 d = -d;
Harald Welte49e3d5a2017-12-25 09:47:57 +0100464 state->stats.jitter += d - ((state->stats.jitter + 8) >> 4);
465 state->stats.max_seq = seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200466}
467
Philipp Maier6931f9a2018-07-26 09:29:31 +0200468/* There may be different payload type numbers negotiated for two connections.
469 * Patch the payload type of an RTP packet so that it uses the payload type
470 * that is valid for the destination connection (conn_dst) */
471static int mgcp_patch_pt(struct mgcp_conn_rtp *conn_src,
472 struct mgcp_conn_rtp *conn_dst, char *data, int len)
473{
474 struct rtp_hdr *rtp_hdr;
475 uint8_t pt_in;
476 int pt_out;
477
478 OSMO_ASSERT(len >= sizeof(struct rtp_hdr));
479 rtp_hdr = (struct rtp_hdr *)data;
480
481 pt_in = rtp_hdr->payload_type;
482 pt_out = mgcp_codec_pt_translate(conn_src, conn_dst, pt_in);
483 if (pt_out < 0)
484 return -EINVAL;
485
486 rtp_hdr->payload_type = (uint8_t) pt_out;
487 return 0;
488}
489
Philipp Maier87bd9be2017-08-22 16:35:41 +0200490/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200491 * some of the supported endpoints (e.g. the nanoBTS) can only handle
492 * one source and this code will patch RTP header to appear as if there
493 * is only one source.
494 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200495 * we receive will be seen as a switch in streams. */
496void mgcp_patch_and_count(struct mgcp_endpoint *endp,
497 struct mgcp_rtp_state *state,
498 struct mgcp_rtp_end *rtp_end,
499 struct sockaddr_in *addr, char *data, int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200500{
501 uint32_t arrival_time;
502 int32_t transit;
503 uint16_t seq;
504 uint32_t timestamp, ssrc;
505 struct rtp_hdr *rtp_hdr;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200506 int payload = rtp_end->codec->payload_type;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200507
508 if (len < sizeof(*rtp_hdr))
509 return;
510
Philipp Maier87bd9be2017-08-22 16:35:41 +0200511 rtp_hdr = (struct rtp_hdr *)data;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200512 seq = ntohs(rtp_hdr->sequence);
513 timestamp = ntohl(rtp_hdr->timestamp);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200514 arrival_time = get_current_ts(rtp_end->codec->rate);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200515 ssrc = ntohl(rtp_hdr->ssrc);
516 transit = arrival_time - timestamp;
517
518 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc);
519
520 if (!state->initialized) {
521 state->initialized = 1;
522 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100523 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200524 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200525 state->packet_duration =
526 mgcp_rtp_packet_duration(endp, rtp_end);
Philipp Maier0ec1d4e2018-05-16 11:09:42 +0200527 state->out_stream.last_seq = seq - 1;
528 state->out_stream.ssrc = state->patch.orig_ssrc = ssrc;
529 state->out_stream.last_tsdelta = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200530 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200531 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200532 LOGPENDP(endp, DRTP, LOGL_INFO,
533 "initializing stream, SSRC: %u timestamp: %u "
534 "pkt-duration: %d, from %s:%d\n",
535 state->in_stream.ssrc,
536 state->patch.seq_offset, state->packet_duration,
537 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200538 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200539 state->packet_duration =
Philipp Maierbc0346e2018-06-07 09:52:16 +0200540 rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200541 LOGPENDP(endp, DRTP, LOGL_NOTICE,
542 "fixed packet duration is not available, "
543 "using fixed 20ms instead: %d from %s:%d\n",
544 state->packet_duration,
545 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200546 }
547 } else if (state->in_stream.ssrc != ssrc) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200548 LOGPENDP(endp, DRTP, LOGL_NOTICE,
549 "SSRC changed: %u -> %u "
550 "from %s:%d\n",
551 state->in_stream.ssrc, rtp_hdr->ssrc,
552 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200553
554 state->in_stream.ssrc = ssrc;
555 if (rtp_end->force_constant_ssrc) {
556 int16_t delta_seq;
557
558 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100559 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200560 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200561
562 /* Estimate number of packets that would have been sent */
563 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200564 (arrival_time - state->in_stream.last_arrival_time
565 + state->packet_duration / 2) /
566 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200567
568 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
569 delta_seq, timestamp);
570
Harald Welte33381352017-12-25 09:44:26 +0100571 state->patch.patch_ssrc = 1;
572 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200573 if (rtp_end->force_constant_ssrc != -1)
574 rtp_end->force_constant_ssrc -= 1;
575
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200576 LOGPENDP(endp, DRTP, LOGL_NOTICE,
577 "SSRC patching enabled, SSRC: %u "
578 "SeqNo offset: %d, TS offset: %d "
579 "from %s:%d\n", state->in_stream.ssrc,
580 state->patch.seq_offset, state->patch.timestamp_offset,
581 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200582 }
583
584 state->in_stream.last_tsdelta = 0;
585 } else {
586 /* Compute current per-packet timestamp delta */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200587 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
588 addr, seq, timestamp, "input",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200589 &state->in_stream.last_tsdelta);
590
Harald Welte33381352017-12-25 09:44:26 +0100591 if (state->patch.patch_ssrc)
592 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200593 }
594
595 /* Save before patching */
596 state->in_stream.last_timestamp = timestamp;
597 state->in_stream.last_seq = seq;
598 state->in_stream.last_arrival_time = arrival_time;
599
600 if (rtp_end->force_aligned_timing &&
601 state->out_stream.ssrc == ssrc && state->packet_duration)
602 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200603 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
604 timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200605
606 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100607 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200608 rtp_hdr->ssrc = htonl(ssrc);
609
610 /* Apply the offset and store it back to the packet.
611 * This won't change anything if the offset is 0, so the conditional is
612 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100613 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200614 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100615 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200616 rtp_hdr->timestamp = htonl(timestamp);
617
618 /* Check again, whether the timestamps are still valid */
619 if (state->out_stream.ssrc == ssrc)
620 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
621 addr, seq, timestamp, "output",
622 &state->out_stream.last_tsdelta);
623
624 /* Save output values */
625 state->out_stream.last_seq = seq;
626 state->out_stream.last_timestamp = timestamp;
627 state->out_stream.ssrc = ssrc;
628
629 if (payload < 0)
630 return;
631
632#if 0
Philipp Maierc3413882017-10-27 12:26:54 +0200633 DEBUGP(DRTP,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100634 "endpoint:0x%x payload hdr payload %u -> endp payload %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200635 ENDPOINT_NUMBER(endp), rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200636 rtp_hdr->payload_type = payload;
637#endif
638}
639
Philipp Maier9fc8a022019-02-20 12:26:52 +0100640/* There are different dialects used to format and transfer voice data. When
641 * the receiving end expects GSM-HR data to be formated after RFC 5993, this
642 * function is used to convert between RFC 5993 and TS 101318, which we normally
643 * use. */
644static void rfc5993_hr_convert(struct mgcp_endpoint *endp, char *data, int *len)
645{
646 /* NOTE: *data has an overall length of RTP_BUF_SIZE, so there is
647 * plenty of space available to store the slightly larger, converted
648 * data */
649
650 struct rtp_hdr *rtp_hdr;
651
652 OSMO_ASSERT(*len >= sizeof(struct rtp_hdr));
653 rtp_hdr = (struct rtp_hdr *)data;
654
655 if (*len == GSM_HR_BYTES + sizeof(struct rtp_hdr)) {
656 /* TS 101318 encoding => RFC 5993 encoding */
657 memmove(rtp_hdr->data + 1, rtp_hdr->data, GSM_HR_BYTES);
658 rtp_hdr->data[0] = 0x00;
659 (*len) += 1;
660
661 } else if (*len == GSM_HR_BYTES + sizeof(struct rtp_hdr) + 1) {
662 /* RFC 5993 encoding => TS 101318 encoding */
663 memmove(rtp_hdr->data, rtp_hdr->data + 1, GSM_HR_BYTES);
664 (*len) -= 1;
665 } else {
666 /* It is possible that multiple payloads occur in one RTP
667 * packet. This is not supported yet. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200668 LOGPENDP(endp, DRTP, LOGL_ERROR,
669 "cannot figure out how to convert RTP packet\n");
Philipp Maier9fc8a022019-02-20 12:26:52 +0100670 }
671}
672
Philipp Maier228e5912019-03-05 13:56:59 +0100673/* For AMR RTP two framing modes are defined RFC3267. There is a bandwith
674 * efficient encoding scheme where all fields are packed together one after
675 * another and an octet aligned mode where all fields are aligned to octet
676 * boundaries. This function is used to convert between the two modes */
677static int amr_oa_bwe_convert(struct mgcp_endpoint *endp, char *data, int *len,
678 bool target_is_oa)
679{
680 /* NOTE: *data has an overall length of RTP_BUF_SIZE, so there is
681 * plenty of space available to store the slightly larger, converted
682 * data */
683
684 struct rtp_hdr *rtp_hdr;
685 unsigned int payload_len;
686 int rc;
687
688 OSMO_ASSERT(*len >= sizeof(struct rtp_hdr));
689 rtp_hdr = (struct rtp_hdr *)data;
690
691 payload_len = *len - sizeof(struct rtp_hdr);
692
693 if (osmo_amr_is_oa(rtp_hdr->data, payload_len)) {
694 if (!target_is_oa)
695 /* Input data is oa an target format is bwe
696 * ==> convert */
697 rc = osmo_amr_oa_to_bwe(rtp_hdr->data, payload_len);
698 else
699 /* Input data is already bew, but we accept it anyway
700 * ==> no conversion needed */
701 rc = payload_len;
702 } else {
703 if (target_is_oa)
704 /* Input data is bwe an target format is oa
705 * ==> convert */
706 rc = osmo_amr_bwe_to_oa(rtp_hdr->data, payload_len,
707 RTP_BUF_SIZE);
708 else
709 /* Input data is already oa, but we accept it anyway
710 * ==> no conversion needed */
711 rc = payload_len;
712 }
713 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200714 LOGPENDP(endp, DRTP, LOGL_ERROR,
715 "AMR RTP packet conversion failed\n");
Philipp Maier228e5912019-03-05 13:56:59 +0100716 return -EINVAL;
717 }
718
719 *len = rc + sizeof(struct rtp_hdr);
720
721 return 0;
722}
723
724/* Check if a conversion between octet-aligned and bandwith-efficient mode is
725 * indicated. */
726static bool amr_oa_bwe_convert_indicated(struct mgcp_rtp_codec *codec)
727{
728 if (codec->param_present == false)
729 return false;
730 if (!codec->param.amr_octet_aligned_present)
731 return false;
732 if (strcmp(codec->subtype_name, "AMR") != 0)
733 return false;
734 return true;
735}
736
737
738/* Check if a given RTP with AMR payload for octet-aligned mode */
739static bool amr_oa_check(char *data, int len)
740{
741 struct rtp_hdr *rtp_hdr;
742 unsigned int payload_len;
743
744 OSMO_ASSERT(len >= sizeof(struct rtp_hdr));
745 rtp_hdr = (struct rtp_hdr *)data;
746
747 payload_len = len - sizeof(struct rtp_hdr);
748
749 return osmo_amr_is_oa(rtp_hdr->data, payload_len);
750}
751
Philipp Maier87bd9be2017-08-22 16:35:41 +0200752/* Forward data to a debug tap. This is debug function that is intended for
753 * debugging the voice traffic with tools like gstreamer */
Philipp Maiere6f172d2017-11-07 12:00:01 +0100754static void forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf,
755 int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200756{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100757 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200758
Philipp Maiere6f172d2017-11-07 12:00:01 +0100759 if (!tap->enabled)
760 return;
761
762 rc = sendto(fd, buf, len, 0, (struct sockaddr *)&tap->forward,
763 sizeof(tap->forward));
764
765 if (rc < 0)
766 LOGP(DRTP, LOGL_ERROR,
767 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200768}
769
Philipp Maier87bd9be2017-08-22 16:35:41 +0200770/*! Send RTP/RTCP data to a specified destination connection.
771 * \param[in] endp associated endpoint (for configuration, logging)
772 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP
773 * \param[in] spoofed source address (set to NULL to disable)
774 * \param[in] buf buffer that contains the RTP/RTCP data
775 * \param[in] len length of the buffer that contains the RTP/RTCP data
776 * \param[in] conn_src associated source connection
777 * \param[in] conn_dst associated destination connection
778 * \returns 0 on success, -1 on ERROR */
779int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
780 char *buf, int len, struct mgcp_conn_rtp *conn_src,
781 struct mgcp_conn_rtp *conn_dst)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200782{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200783 /*! When no destination connection is available (e.g. when only one
784 * connection in loopback mode exists), then the source connection
785 * shall be specified as destination connection */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200786
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200787 struct mgcp_trunk_config *tcfg = endp->tcfg;
788 struct mgcp_rtp_end *rtp_end;
789 struct mgcp_rtp_state *rtp_state;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200790 char *dest_name;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200791 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200792
Philipp Maier87bd9be2017-08-22 16:35:41 +0200793 OSMO_ASSERT(conn_src);
794 OSMO_ASSERT(conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200795
Philipp Maier87bd9be2017-08-22 16:35:41 +0200796 if (is_rtp) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200797 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTP packet...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200798 } else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200799 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTCP packet...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200800 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200801
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200802 LOGPENDP(endp, DRTP, LOGL_DEBUG, "loop:%d, mode:%d%s\n",
803 tcfg->audio_loop, conn_src->conn->mode,
804 conn_src->conn->mode == MGCP_CONN_LOOPBACK ? " (loopback)" : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200805
Philipp Maier6931f9a2018-07-26 09:29:31 +0200806 /* FIXME: It is legal that the payload type on the egress connection is
807 * different from the payload type that has been negotiated on the
808 * ingress connection. Essentially the codecs are the same so we can
809 * match them and patch the payload type. However, if we can not find
810 * the codec pendant (everything ist equal except the PT), we are of
811 * course unable to patch the payload type. A situation like this
812 * should not occur if transcoding is consequently avoided. Until
813 * we have transcoding support in osmo-mgw we can not resolve this. */
Philipp Maierda895b12018-08-03 12:16:37 +0200814 if (is_rtp) {
815 rc = mgcp_patch_pt(conn_src, conn_dst, buf, len);
816 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200817 LOGPENDP(endp, DRTP, LOGL_DEBUG,
818 "can not patch PT because no suitable egress codec was found.\n");
Philipp Maierda895b12018-08-03 12:16:37 +0200819 }
Philipp Maier6931f9a2018-07-26 09:29:31 +0200820 }
821
Philipp Maier87bd9be2017-08-22 16:35:41 +0200822 /* Note: In case of loopback configuration, both, the source and the
823 * destination will point to the same connection. */
824 rtp_end = &conn_dst->end;
825 rtp_state = &conn_src->state;
826 dest_name = conn_dst->conn->name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200827
828 if (!rtp_end->output_enabled) {
Philipp Maiercede2a42018-07-03 14:14:21 +0200829 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_DROPPED_PACKETS_CTR]);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200830 LOGPENDP(endp, DRTP, LOGL_DEBUG,
831 "output disabled, drop to %s %s "
832 "rtp_port:%u rtcp_port:%u\n",
833 dest_name,
834 inet_ntoa(rtp_end->addr),
835 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200836 );
837 } else if (is_rtp) {
838 int cont;
839 int nbytes = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200840 int buflen = len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200841 do {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200842 /* Run transcoder */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200843 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200844 buf, &buflen,
845 RTP_BUF_SIZE);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200846 if (cont < 0)
847 break;
848
Philipp Maier87bd9be2017-08-22 16:35:41 +0200849 if (addr)
850 mgcp_patch_and_count(endp, rtp_state, rtp_end,
851 addr, buf, buflen);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100852
Philipp Maier228e5912019-03-05 13:56:59 +0100853 if (amr_oa_bwe_convert_indicated(conn_dst->end.codec)) {
854 amr_oa_bwe_convert(endp, buf, &buflen,
855 conn_dst->end.codec->param.amr_octet_aligned);
856 }
857 else if (rtp_end->rfc5993_hr_convert
Philipp Maier9fc8a022019-02-20 12:26:52 +0100858 && strcmp(conn_src->end.codec->subtype_name,
859 "GSM-HR-08") == 0)
860 rfc5993_hr_convert(endp, buf, &buflen);
861
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200862 LOGPENDP(endp, DRTP, LOGL_DEBUG,
863 "process/send to %s %s "
864 "rtp_port:%u rtcp_port:%u\n",
865 dest_name, inet_ntoa(rtp_end->addr),
866 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
867 );
Philipp Maier87bd9be2017-08-22 16:35:41 +0200868
869 /* Forward a copy of the RTP data to a debug ip/port */
870 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
871 buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200872
873 /* FIXME: HACK HACK HACK. See OS#2459.
874 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
875 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
876 * cells (as long as we patch only the first RTP payload in each stream).
877 */
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200878 if (!rtp_state->patched_first_rtp_payload
879 && conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200880 uint8_t *data = (uint8_t *) & buf[12];
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200881 if (data[0] == 0xe0) {
882 data[0] = 0xe4;
883 data[1] = 0x00;
884 rtp_state->patched_first_rtp_payload = true;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200885 LOGPENDP(endp, DRTP, LOGL_DEBUG,
886 "Patching over first two bytes"
887 " to fake an IuUP Initialization Ack\n");
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200888 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200889 }
890
Philipp Maier87bd9be2017-08-22 16:35:41 +0200891 len = mgcp_udp_send(rtp_end->rtp.fd,
892 &rtp_end->addr,
893 rtp_end->rtp_port, buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200894
Philipp Maier87bd9be2017-08-22 16:35:41 +0200895 if (len <= 0)
896 return len;
897
Philipp Maiercede2a42018-07-03 14:14:21 +0200898 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
899 rate_ctr_add(&conn_dst->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], len);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200900
901 nbytes += len;
902 buflen = cont;
903 } while (buflen > 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200904 return nbytes;
905 } else if (!tcfg->omit_rtcp) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200906 LOGPENDP(endp, DRTP, LOGL_DEBUG,
907 "send to %s %s rtp_port:%u rtcp_port:%u\n",
908 dest_name, inet_ntoa(rtp_end->addr),
909 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
910 );
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200911
Philipp Maier87bd9be2017-08-22 16:35:41 +0200912 len = mgcp_udp_send(rtp_end->rtcp.fd,
913 &rtp_end->addr,
914 rtp_end->rtcp_port, buf, len);
915
Philipp Maiercede2a42018-07-03 14:14:21 +0200916 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
917 rate_ctr_add(&conn_dst->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], len);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200918
919 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200920 }
921
922 return 0;
923}
924
Philipp Maier87bd9be2017-08-22 16:35:41 +0200925/* Helper function for mgcp_recv(),
926 Receive one RTP Packet + Originating address from file descriptor */
927static int receive_from(struct mgcp_endpoint *endp, int fd,
928 struct sockaddr_in *addr, char *buf, int bufsize)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200929{
930 int rc;
931 socklen_t slen = sizeof(*addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200932 struct sockaddr_in addr_sink;
933 char buf_sink[RTP_BUF_SIZE];
934 bool tossed = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200935
Philipp Maier87bd9be2017-08-22 16:35:41 +0200936 if (!addr)
937 addr = &addr_sink;
938 if (!buf) {
939 tossed = true;
940 buf = buf_sink;
941 bufsize = sizeof(buf_sink);
942 }
943
944 rc = recvfrom(fd, buf, bufsize, 0, (struct sockaddr *)addr, &slen);
945
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200946 LOGPENDP(endp, DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200947 "receiving %u bytes length packet from %s:%u ...\n",
948 rc, inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
949
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200950 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200951 LOGPENDP(endp, DRTP, LOGL_ERROR,
952 "failed to receive packet, errno: %d/%s\n",
953 errno, strerror(errno));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200954 return -1;
955 }
956
Philipp Maier87bd9be2017-08-22 16:35:41 +0200957 if (tossed) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200958 LOGPENDP(endp, DRTP, LOGL_ERROR, "packet tossed\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200959 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200960
961 return rc;
962}
963
Philipp Maier87bd9be2017-08-22 16:35:41 +0200964/* Check if the origin (addr) matches the address/port data of the RTP
965 * connections. */
966static int check_rtp_origin(struct mgcp_conn_rtp *conn,
967 struct sockaddr_in *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200968{
Harald Weltec26b6652018-10-21 12:01:04 +0200969 if (conn->end.addr.s_addr == 0) {
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200970 switch (conn->conn->mode) {
971 case MGCP_CONN_LOOPBACK:
972 /* HACK: for IuUP, we want to reply with an IuUP Initialization ACK upon the first RTP
973 * message received. We currently hackishly accomplish that by putting the endpoint in
974 * loopback mode and patching over the looped back RTP message to make it look like an
975 * ack. We don't know the femto cell's IP address and port until the RAB Assignment
976 * Response is received, but the nano3G expects an IuUP Initialization Ack before it even
977 * sends the RAB Assignment Response. Hence, if the remote address is 0.0.0.0 and the
978 * MGCP port is in loopback mode, allow looping back the packet to any source. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200979 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
980 "In loopback mode and remote address not set:"
981 " allowing data from address: %s\n", inet_ntoa(addr->sin_addr));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200982 return 0;
983
984 default:
985 /* Receiving early media before the endpoint is configured. Instead of logging
986 * this as an error that occurs on every call, keep it more low profile to not
987 * confuse humans with expected errors. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200988 LOGPCONN(conn->conn, DRTP, LOGL_INFO,
989 "Rx RTP from %s, but remote address not set:"
990 " dropping early media\n", inet_ntoa(addr->sin_addr));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200991 return -1;
992 }
Neels Hofmeyr0063ca22018-07-23 18:12:16 +0200993 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200994
Philipp Maier87bd9be2017-08-22 16:35:41 +0200995 /* Note: Check if the inbound RTP data comes from the same host to
996 * which we send our outgoing RTP traffic. */
Harald Weltec26b6652018-10-21 12:01:04 +0200997 if (conn->end.addr.s_addr != addr->sin_addr.s_addr) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200998 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
999 "data from wrong address: %s, ", inet_ntoa(addr->sin_addr));
Philipp Maierc3413882017-10-27 12:26:54 +02001000 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001001 inet_ntoa(conn->end.addr));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001002 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001003 return -1;
1004 }
1005
Philipp Maier87bd9be2017-08-22 16:35:41 +02001006 /* Note: Usually the remote remote port of the data we receive will be
1007 * the same as the remote port where we transmit outgoing RTP traffic
1008 * to (set by MDCX). We use this to check the origin of the data for
1009 * plausibility. */
1010 if (conn->end.rtp_port != addr->sin_port &&
1011 conn->end.rtcp_port != addr->sin_port) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001012 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1013 "data from wrong source port: %d, ", ntohs(addr->sin_port));
Philipp Maierc3413882017-10-27 12:26:54 +02001014 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001015 "expected: %d for RTP or %d for RTCP\n",
1016 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001017 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001018 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001019 }
1020
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001021 return 0;
1022}
1023
Philipp Maier87bd9be2017-08-22 16:35:41 +02001024/* Check the if the destination address configuration of an RTP connection
1025 * makes sense */
1026static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001027{
Philipp Maiere6df0e42018-05-29 14:03:06 +02001028 /* Note: it is legal to create a connection but never setting a port
1029 * and IP-address for outgoing data. */
1030 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 +02001031 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
1032 "destination IP-address and rtp port is (not yet) known (%s:%u)\n",
1033 inet_ntoa(conn->end.addr), conn->end.rtp_port);
Philipp Maiere6df0e42018-05-29 14:03:06 +02001034 return -1;
1035 }
1036
Philipp Maier87bd9be2017-08-22 16:35:41 +02001037 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001038 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1039 "destination IP-address is invalid (%s:%u)\n",
1040 inet_ntoa(conn->end.addr), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001041 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001042 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001043
1044 if (conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001045 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1046 "destination rtp port is invalid (%s:%u)\n",
1047 inet_ntoa(conn->end.addr), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001048 return -1;
1049 }
1050
1051 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001052}
1053
Philipp Maier4dba7692018-08-03 12:20:52 +02001054/* Do some basic checks to make sure that the RTCP packets we are going to
1055 * process are not complete garbage */
1056static int check_rtcp(char *buf, unsigned int buf_size)
1057{
1058 struct rtcp_hdr *hdr;
1059 unsigned int len;
1060 uint8_t type;
1061
1062 /* RTPC packets that are just a header without data do not make
1063 * any sense. */
1064 if (buf_size < sizeof(struct rtcp_hdr))
1065 return -EINVAL;
1066
1067 /* Make sure that the length of the received packet does not exceed
1068 * the available buffer size */
1069 hdr = (struct rtcp_hdr *)buf;
1070 len = (osmo_ntohs(hdr->length) + 1) * 4;
1071 if (len > buf_size)
1072 return -EINVAL;
1073
1074 /* Make sure we accept only packets that have a proper packet type set
1075 * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
1076 type = hdr->type;
1077 if ((type < 192 || type > 195) && (type < 200 || type > 213))
1078 return -EINVAL;
1079
1080 return 0;
1081}
1082
1083/* Do some basic checks to make sure that the RTP packets we are going to
1084 * process are not complete garbage */
1085static int check_rtp(char *buf, unsigned int buf_size)
1086{
1087 /* RTP packets that are just a header without data do not make
1088 * any sense. */
1089 if (buf_size < sizeof(struct rtp_hdr))
1090 return -EINVAL;
1091
1092 /* FIXME: Add more checks, the reason why we do not check more than
1093 * the length is because we currently handle IUUP packets as RTP
1094 * packets, so they must pass this check, if we weould be more
1095 * strict here, we would possibly break 3G. (see also FIXME note
1096 * below */
1097
1098 return 0;
1099}
1100
Philipp Maier87bd9be2017-08-22 16:35:41 +02001101/* Receive RTP data from a specified source connection and dispatch it to a
1102 * destination connection. */
1103static int mgcp_recv(int *proto, struct sockaddr_in *addr, char *buf,
1104 unsigned int buf_size, struct osmo_fd *fd)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001105{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001106 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001107 struct mgcp_conn_rtp *conn;
1108 struct mgcp_trunk_config *tcfg;
1109 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001110
Philipp Maier87bd9be2017-08-22 16:35:41 +02001111 conn = (struct mgcp_conn_rtp*) fd->data;
1112 endp = conn->conn->endp;
1113 tcfg = endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001114
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001115 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG, "receiving RTP/RTCP packet...\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001116
1117 rc = receive_from(endp, fd->fd, addr, buf, buf_size);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001118 if (rc <= 0)
1119 return -1;
Philipp Maier4dba7692018-08-03 12:20:52 +02001120
1121 /* FIXME: The way how we detect the protocol looks odd. We should look
1122 * into the packet header. Also we should introduce a packet type
1123 * MGCP_PROTO_IUUP because currently we handle IUUP packets like RTP
1124 * packets which is problematic. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001125 *proto = fd == &conn->end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001126
Philipp Maier4dba7692018-08-03 12:20:52 +02001127 if (*proto == MGCP_PROTO_RTP) {
1128 if (check_rtp(buf, rc) < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001129 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1130 "invalid RTP packet received -- packet tossed\n");
Philipp Maier4dba7692018-08-03 12:20:52 +02001131 return -1;
1132 }
1133 } else if (*proto == MGCP_PROTO_RTCP) {
1134 if (check_rtcp(buf, rc) < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001135 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1136 "invalid RTCP packet received -- packet tossed\n");
Philipp Maier4dba7692018-08-03 12:20:52 +02001137 return -1;
1138 }
1139 }
1140
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001141 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG, "");
Neels Hofmeyr56725632018-01-15 15:15:07 +01001142 LOGPC(DRTP, LOGL_DEBUG, "receiving from %s %s %d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001143 conn->conn->name, inet_ntoa(addr->sin_addr),
1144 ntohs(addr->sin_port));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001145 LOGPENDP(endp, DRTP, LOGL_DEBUG, "conn:%s\n", mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001146
Philipp Maier87bd9be2017-08-22 16:35:41 +02001147 /* Check if the origin of the RTP packet seems plausible */
1148 if (tcfg->rtp_accept_all == 0) {
1149 if (check_rtp_origin(conn, addr) != 0)
1150 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001151 }
1152
Philipp Maier87bd9be2017-08-22 16:35:41 +02001153 /* Filter out dummy message */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001154 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001155 LOGPCONN(conn->conn, DRTP, LOGL_NOTICE,
1156 "dummy message received\n");
1157 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1158 "packet tossed\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001159 return 0;
1160 }
1161
Philipp Maier87bd9be2017-08-22 16:35:41 +02001162 /* Increment RX statistics */
Philipp Maiercede2a42018-07-03 14:14:21 +02001163 rate_ctr_inc(&conn->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR]);
1164 rate_ctr_add(&conn->rate_ctr_group->ctr[RTP_OCTETS_RX_CTR], rc);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001165
Philipp Maier87bd9be2017-08-22 16:35:41 +02001166 /* Forward a copy of the RTP data to a debug ip/port */
1167 forward_data(fd->fd, &conn->tap_in, buf, rc);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001168
Philipp Maier87bd9be2017-08-22 16:35:41 +02001169 return rc;
1170}
1171
1172/* Send RTP data. Possible options are standard RTP packet
1173 * transmission or trsmission via an osmux connection */
1174static int mgcp_send_rtp(int proto, struct sockaddr_in *addr, char *buf,
1175 unsigned int buf_size,
1176 struct mgcp_conn_rtp *conn_src,
1177 struct mgcp_conn_rtp *conn_dst)
1178{
1179 struct mgcp_endpoint *endp;
1180 endp = conn_src->conn->endp;
1181
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001182 LOGPENDP(endp, DRTP, LOGL_DEBUG, "destin conn:%s\n",
1183 mgcp_conn_dump(conn_dst->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001184
1185 /* Before we try to deliver the packet, we check if the destination
1186 * port and IP-Address make sense at all. If not, we will be unable
1187 * to deliver the packet. */
1188 if (check_rtp_destin(conn_dst) != 0)
1189 return -1;
1190
1191 /* Depending on the RTP connection type, deliver the RTP packet to the
1192 * destination connection. */
1193 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001194 case MGCP_RTP_DEFAULT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001195 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1196 "endpoint type is MGCP_RTP_DEFAULT, "
1197 "using mgcp_send() to forward data directly\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001198 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
1199 addr, buf, buf_size, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001200 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001201 case MGCP_OSMUX_BSC:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001202 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1203 "endpoint type is MGCP_OSMUX_BSC_NAT, "
1204 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001205 return osmux_xfrm_to_osmux(buf, buf_size, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001206 }
1207
Philipp Maier87bd9be2017-08-22 16:35:41 +02001208 /* If the data has not been handled/forwarded until here, it will
1209 * be discarded, this should not happen, normally the MGCP type
1210 * should be properly set */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001211 LOGPENDP(endp, DRTP, LOGL_ERROR, "bad MGCP type -- data discarded!\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001212
1213 return -1;
1214}
1215
1216/*! dispatch incoming RTP packet to opposite RTP connection.
1217 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP)
1218 * \param[in] addr socket address where the RTP packet has been received from
1219 * \param[in] buf buffer that hold the RTP payload
1220 * \param[in] buf_size size data length of buf
1221 * \param[in] conn originating connection
1222 * \returns 0 on success, -1 on ERROR */
1223int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf,
1224 unsigned int buf_size, struct mgcp_conn *conn)
1225{
1226 struct mgcp_conn *conn_dst;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001227
1228 /*! NOTE: This callback function implements the endpoint specific
1229 * dispatch bahviour of an rtp bridge/proxy endpoint. It is assumed
1230 * that the endpoint will hold only two connections. This premise
1231 * is used to determine the opposite connection (it is always the
1232 * connection that is not the originating connection). Once the
1233 * destination connection is known the RTP packet is sent via
1234 * the destination connection. */
1235
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001236
1237 /* Check if the connection is in loopback mode, if yes, just send the
1238 * incoming data back to the origin */
1239 if (conn->mode == MGCP_CONN_LOOPBACK) {
1240 /* When we are in loopback mode, we loop back all incoming
1241 * packets back to their origin. We will use the originating
1242 * address data from the UDP packet header to patch the
1243 * outgoing address in connection on the fly */
1244 if (conn->u.rtp.end.rtp_port == 0) {
1245 conn->u.rtp.end.addr = addr->sin_addr;
1246 conn->u.rtp.end.rtp_port = addr->sin_port;
1247 }
1248 return mgcp_send_rtp(proto, addr, buf,
1249 buf_size, &conn->u.rtp, &conn->u.rtp);
1250 }
1251
Philipp Maier87bd9be2017-08-22 16:35:41 +02001252 /* Find a destination connection. */
1253 /* NOTE: This code path runs every time an RTP packet is received. The
1254 * function mgcp_find_dst_conn() we use to determine the detination
1255 * connection will iterate the connection list inside the endpoint.
1256 * Since list iterations are quite costly, we will figure out the
1257 * destination only once and use the optional private data pointer of
1258 * the connection to cache the destination connection pointer. */
1259 if (!conn->priv) {
1260 conn_dst = mgcp_find_dst_conn(conn);
1261 conn->priv = conn_dst;
1262 } else {
1263 conn_dst = (struct mgcp_conn *)conn->priv;
1264 }
1265
1266 /* There is no destination conn, stop here */
1267 if (!conn_dst) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001268 LOGPCONN(conn, DRTP, LOGL_ERROR,
1269 "unable to find destination conn\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001270 return -1;
1271 }
1272
1273 /* The destination conn is not an RTP connection */
1274 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001275 LOGPCONN(conn, DRTP, LOGL_ERROR,
1276 "unable to find suitable destination conn\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001277 return -1;
1278 }
1279
1280 /* Dispatch RTP packet to destination RTP connection */
1281 return mgcp_send_rtp(proto, addr, buf,
1282 buf_size, &conn->u.rtp, &conn_dst->u.rtp);
1283
1284}
1285
Philipp Maierdf5d2192018-01-24 11:39:32 +01001286/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1287 * \param[in] endp Endpoint on which the connection resides.
1288 * \param[in] conn Connection that is about to be removed (ignored).
1289 * \returns 0 on success, -1 on ERROR. */
1290void 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 Maier87bd9be2017-08-22 16:35:41 +02001305/* Handle incoming RTP data from NET */
1306static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1307{
1308 /* NOTE: This is a generic implementation. RTP data is received. In
1309 * case of loopback the data is just sent back to its origin. All
1310 * other cases implement endpoint specific behaviour (e.g. how is the
1311 * destination connection determined?). That specific behaviour is
1312 * implemented by the callback function that is called at the end of
1313 * the function */
1314
1315 struct mgcp_conn_rtp *conn_src;
1316 struct mgcp_endpoint *endp;
1317 struct sockaddr_in addr;
1318
1319 char buf[RTP_BUF_SIZE];
1320 int proto;
Philipp Maierb9694552017-11-08 16:53:57 +01001321 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001322
1323 conn_src = (struct mgcp_conn_rtp *)fd->data;
1324 OSMO_ASSERT(conn_src);
1325 endp = conn_src->conn->endp;
1326 OSMO_ASSERT(endp);
1327
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001328 LOGPENDP(endp, DRTP, LOGL_DEBUG, "source conn:%s\n",
1329 mgcp_conn_dump(conn_src->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001330
1331 /* Receive packet */
Philipp Maierb9694552017-11-08 16:53:57 +01001332 len = mgcp_recv(&proto, &addr, buf, sizeof(buf), fd);
1333 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001334 return -1;
1335
Oliver Smithe36b7752019-01-22 16:31:36 +01001336 mgcp_conn_watchdog_kick(conn_src->conn);
1337
Philipp Maier228e5912019-03-05 13:56:59 +01001338 /* If AMR is configured for the ingress connection a conversion of the
1339 * framing mode (octet-aligned vs. bandwith-efficient is explicitly
1340 * define, then we check if the incoming payload matches that
1341 * expectation. */
1342 if (amr_oa_bwe_convert_indicated(conn_src->end.codec)) {
1343 if (amr_oa_check(buf, len) != conn_src->end.codec->param.amr_octet_aligned)
1344 return -1;
1345 }
1346
Philipp Maier87bd9be2017-08-22 16:35:41 +02001347 /* Execute endpoint specific implementation that handles the
1348 * dispatching of the RTP data */
Philipp Maierb9694552017-11-08 16:53:57 +01001349 return endp->type->dispatch_rtp_cb(proto, &addr, buf, len,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001350 conn_src->conn);
1351}
1352
1353/*! set IP Type of Service parameter.
1354 * \param[in] fd associated file descriptor
1355 * \param[in] tos dscp value
1356 * \returns 0 on success, -1 on ERROR */
1357int mgcp_set_ip_tos(int fd, int tos)
1358{
1359 int ret;
1360 ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1361
1362 if (ret < 0)
1363 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001364 return 0;
1365}
1366
Philipp Maier87bd9be2017-08-22 16:35:41 +02001367/*! bind RTP port to osmo_fd.
1368 * \param[in] source_addr source (local) address to bind on
1369 * \param[in] fd associated file descriptor
1370 * \param[in] port to bind on
1371 * \returns 0 on success, -1 on ERROR */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001372int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
1373{
Harald Welte8890dfa2017-11-17 15:09:30 +01001374 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001375
Harald Welte8890dfa2017-11-17 15:09:30 +01001376 rc = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
1377 NULL, 0, OSMO_SOCK_F_BIND);
1378 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001379 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001380 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001381 return -1;
1382 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001383 fd->fd = rc;
1384 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001385
1386 return 0;
1387}
1388
Philipp Maier87bd9be2017-08-22 16:35:41 +02001389/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001390static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001391 struct mgcp_rtp_end *rtp_end, int endpno)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001392{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001393 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1394 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001395 struct mgcp_endpoint *endp = &cfg->trunk.endpoints[endpno];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001396
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001397 if (mgcp_create_bind(source_addr, &rtp_end->rtp,
1398 rtp_end->local_port) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001399 LOGPENDP(endp, DRTP, LOGL_ERROR,
1400 "failed to create RTP port: %s:%d\n",
1401 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001402 goto cleanup0;
1403 }
1404
1405 if (mgcp_create_bind(source_addr, &rtp_end->rtcp,
1406 rtp_end->local_port + 1) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001407 LOGPENDP(endp, DRTP, LOGL_ERROR,
1408 "failed to create RTCP port: %s:%d\n",
1409 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001410 goto cleanup1;
1411 }
1412
Philipp Maier87bd9be2017-08-22 16:35:41 +02001413 /* Set Type of Service (DSCP-Value) as configured via VTY */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001414 mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
1415 mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
1416
1417 rtp_end->rtp.when = BSC_FD_READ;
1418 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001419 LOGPENDP(endp, DRTP, LOGL_ERROR,
1420 "failed to register RTP port %d\n",
1421 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001422 goto cleanup2;
1423 }
1424
1425 rtp_end->rtcp.when = BSC_FD_READ;
1426 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001427 LOGPENDP(endp, DRTP, LOGL_ERROR,
1428 "failed to register RTCP port %d\n",
1429 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001430 goto cleanup3;
1431 }
1432
1433 return 0;
1434
1435cleanup3:
1436 osmo_fd_unregister(&rtp_end->rtp);
1437cleanup2:
1438 close(rtp_end->rtcp.fd);
1439 rtp_end->rtcp.fd = -1;
1440cleanup1:
1441 close(rtp_end->rtp.fd);
1442 rtp_end->rtp.fd = -1;
1443cleanup0:
1444 return -1;
1445}
1446
Philipp Maier87bd9be2017-08-22 16:35:41 +02001447/*! bind RTP port to endpoint/connection.
1448 * \param[in] endp endpoint that holds the RTP connection
1449 * \param[in] rtp_port port number to bind on
1450 * \param[in] conn associated RTP connection
1451 * \returns 0 on success, -1 on ERROR */
1452int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1453 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001454{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001455 char name[512];
1456 struct mgcp_rtp_end *end;
Philipp Maier1cb1e382017-11-02 17:16:04 +01001457 char local_ip_addr[INET_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001458
Philipp Maier01d24a32017-11-21 17:26:09 +01001459 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001460 end = &conn->end;
1461
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001462 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001463 LOGPENDP(endp, DRTP, LOGL_ERROR, "%u was already bound on conn:%s\n",
1464 rtp_port, mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001465
1466 /* Double bindings should never occour! Since we always allocate
1467 * connections dynamically and free them when they are not
1468 * needed anymore, there must be no previous binding leftover.
1469 * Should there be a connection bound twice, we have a serious
1470 * problem and must exit immediately! */
1471 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001472 }
1473
1474 end->local_port = rtp_port;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001475 end->rtp.cb = rtp_data_net;
1476 end->rtp.data = conn;
1477 end->rtcp.data = conn;
1478 end->rtcp.cb = rtp_data_net;
1479
Philipp Maier1cb1e382017-11-02 17:16:04 +01001480 mgcp_get_local_addr(local_ip_addr, conn);
1481
1482 return bind_rtp(endp->cfg, local_ip_addr, end,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001483 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001484}
1485
Philipp Maier87bd9be2017-08-22 16:35:41 +02001486/*! free allocated RTP and RTCP ports.
1487 * \param[in] end RTP end */
1488void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001489{
1490 if (end->rtp.fd != -1) {
1491 close(end->rtp.fd);
1492 end->rtp.fd = -1;
1493 osmo_fd_unregister(&end->rtp);
1494 }
1495
1496 if (end->rtcp.fd != -1) {
1497 close(end->rtcp.fd);
1498 end->rtcp.fd = -1;
1499 osmo_fd_unregister(&end->rtcp);
1500 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001501}