blob: e163d237b89a5a64aa61db091b70c3e8d5a0d5b9 [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
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200478 if (len < sizeof(struct rtp_hdr))
479 return -EINVAL;
480
Philipp Maier6931f9a2018-07-26 09:29:31 +0200481 rtp_hdr = (struct rtp_hdr *)data;
482
483 pt_in = rtp_hdr->payload_type;
484 pt_out = mgcp_codec_pt_translate(conn_src, conn_dst, pt_in);
485 if (pt_out < 0)
486 return -EINVAL;
487
488 rtp_hdr->payload_type = (uint8_t) pt_out;
489 return 0;
490}
491
Philipp Maier87bd9be2017-08-22 16:35:41 +0200492/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200493 * some of the supported endpoints (e.g. the nanoBTS) can only handle
494 * one source and this code will patch RTP header to appear as if there
495 * is only one source.
496 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200497 * we receive will be seen as a switch in streams. */
498void mgcp_patch_and_count(struct mgcp_endpoint *endp,
499 struct mgcp_rtp_state *state,
500 struct mgcp_rtp_end *rtp_end,
501 struct sockaddr_in *addr, char *data, int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200502{
503 uint32_t arrival_time;
504 int32_t transit;
505 uint16_t seq;
506 uint32_t timestamp, ssrc;
507 struct rtp_hdr *rtp_hdr;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200508 int payload = rtp_end->codec->payload_type;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200509
510 if (len < sizeof(*rtp_hdr))
511 return;
512
Philipp Maier87bd9be2017-08-22 16:35:41 +0200513 rtp_hdr = (struct rtp_hdr *)data;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200514 seq = ntohs(rtp_hdr->sequence);
515 timestamp = ntohl(rtp_hdr->timestamp);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200516 arrival_time = get_current_ts(rtp_end->codec->rate);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200517 ssrc = ntohl(rtp_hdr->ssrc);
518 transit = arrival_time - timestamp;
519
520 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc);
521
522 if (!state->initialized) {
523 state->initialized = 1;
524 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100525 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200526 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200527 state->packet_duration =
528 mgcp_rtp_packet_duration(endp, rtp_end);
Philipp Maier0ec1d4e2018-05-16 11:09:42 +0200529 state->out_stream.last_seq = seq - 1;
530 state->out_stream.ssrc = state->patch.orig_ssrc = ssrc;
531 state->out_stream.last_tsdelta = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200532 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200533 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200534 LOGPENDP(endp, DRTP, LOGL_INFO,
535 "initializing stream, SSRC: %u timestamp: %u "
536 "pkt-duration: %d, from %s:%d\n",
537 state->in_stream.ssrc,
538 state->patch.seq_offset, state->packet_duration,
539 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200540 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200541 state->packet_duration =
Philipp Maierbc0346e2018-06-07 09:52:16 +0200542 rtp_end->codec->rate * 20 / 1000;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200543 LOGPENDP(endp, DRTP, LOGL_NOTICE,
544 "fixed packet duration is not available, "
545 "using fixed 20ms instead: %d from %s:%d\n",
546 state->packet_duration,
547 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200548 }
549 } else if (state->in_stream.ssrc != ssrc) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200550 LOGPENDP(endp, DRTP, LOGL_NOTICE,
551 "SSRC changed: %u -> %u "
552 "from %s:%d\n",
553 state->in_stream.ssrc, rtp_hdr->ssrc,
554 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200555
556 state->in_stream.ssrc = ssrc;
557 if (rtp_end->force_constant_ssrc) {
558 int16_t delta_seq;
559
560 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100561 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200562 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200563
564 /* Estimate number of packets that would have been sent */
565 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200566 (arrival_time - state->in_stream.last_arrival_time
567 + state->packet_duration / 2) /
568 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200569
570 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
571 delta_seq, timestamp);
572
Harald Welte33381352017-12-25 09:44:26 +0100573 state->patch.patch_ssrc = 1;
574 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200575 if (rtp_end->force_constant_ssrc != -1)
576 rtp_end->force_constant_ssrc -= 1;
577
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200578 LOGPENDP(endp, DRTP, LOGL_NOTICE,
579 "SSRC patching enabled, SSRC: %u "
580 "SeqNo offset: %d, TS offset: %d "
581 "from %s:%d\n", state->in_stream.ssrc,
582 state->patch.seq_offset, state->patch.timestamp_offset,
583 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200584 }
585
586 state->in_stream.last_tsdelta = 0;
587 } else {
588 /* Compute current per-packet timestamp delta */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200589 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
590 addr, seq, timestamp, "input",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200591 &state->in_stream.last_tsdelta);
592
Harald Welte33381352017-12-25 09:44:26 +0100593 if (state->patch.patch_ssrc)
594 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200595 }
596
597 /* Save before patching */
598 state->in_stream.last_timestamp = timestamp;
599 state->in_stream.last_seq = seq;
600 state->in_stream.last_arrival_time = arrival_time;
601
602 if (rtp_end->force_aligned_timing &&
603 state->out_stream.ssrc == ssrc && state->packet_duration)
604 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200605 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
606 timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200607
608 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100609 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200610 rtp_hdr->ssrc = htonl(ssrc);
611
612 /* Apply the offset and store it back to the packet.
613 * This won't change anything if the offset is 0, so the conditional is
614 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100615 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200616 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100617 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200618 rtp_hdr->timestamp = htonl(timestamp);
619
620 /* Check again, whether the timestamps are still valid */
621 if (state->out_stream.ssrc == ssrc)
622 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
623 addr, seq, timestamp, "output",
624 &state->out_stream.last_tsdelta);
625
626 /* Save output values */
627 state->out_stream.last_seq = seq;
628 state->out_stream.last_timestamp = timestamp;
629 state->out_stream.ssrc = ssrc;
630
631 if (payload < 0)
632 return;
633
634#if 0
Philipp Maierc3413882017-10-27 12:26:54 +0200635 DEBUGP(DRTP,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100636 "endpoint:0x%x payload hdr payload %u -> endp payload %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200637 ENDPOINT_NUMBER(endp), rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200638 rtp_hdr->payload_type = payload;
639#endif
640}
641
Philipp Maier9fc8a022019-02-20 12:26:52 +0100642/* There are different dialects used to format and transfer voice data. When
643 * the receiving end expects GSM-HR data to be formated after RFC 5993, this
644 * function is used to convert between RFC 5993 and TS 101318, which we normally
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200645 * use.
646 * Return 0 on sucess, negative on errors like invalid data length. */
647static int rfc5993_hr_convert(struct mgcp_endpoint *endp, char *data, int *len)
Philipp Maier9fc8a022019-02-20 12:26:52 +0100648{
649 /* NOTE: *data has an overall length of RTP_BUF_SIZE, so there is
650 * plenty of space available to store the slightly larger, converted
651 * data */
652
653 struct rtp_hdr *rtp_hdr;
654
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200655 if (*len < sizeof(struct rtp_hdr)) {
656 LOGPENDP(endp, DRTP, LOGL_ERROR, "AMR RTP packet too short (%d < %zu)\n",
657 *len, sizeof(struct rtp_hdr));
658 return -EINVAL;
659 }
660
Philipp Maier9fc8a022019-02-20 12:26:52 +0100661 rtp_hdr = (struct rtp_hdr *)data;
662
663 if (*len == GSM_HR_BYTES + sizeof(struct rtp_hdr)) {
664 /* TS 101318 encoding => RFC 5993 encoding */
665 memmove(rtp_hdr->data + 1, rtp_hdr->data, GSM_HR_BYTES);
666 rtp_hdr->data[0] = 0x00;
667 (*len) += 1;
668
669 } else if (*len == GSM_HR_BYTES + sizeof(struct rtp_hdr) + 1) {
670 /* RFC 5993 encoding => TS 101318 encoding */
671 memmove(rtp_hdr->data, rtp_hdr->data + 1, GSM_HR_BYTES);
672 (*len) -= 1;
673 } else {
674 /* It is possible that multiple payloads occur in one RTP
675 * packet. This is not supported yet. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200676 LOGPENDP(endp, DRTP, LOGL_ERROR,
677 "cannot figure out how to convert RTP packet\n");
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200678 return -ENOTSUP;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100679 }
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200680 return 0;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100681}
682
Philipp Maier228e5912019-03-05 13:56:59 +0100683/* For AMR RTP two framing modes are defined RFC3267. There is a bandwith
684 * efficient encoding scheme where all fields are packed together one after
685 * another and an octet aligned mode where all fields are aligned to octet
686 * boundaries. This function is used to convert between the two modes */
687static int amr_oa_bwe_convert(struct mgcp_endpoint *endp, char *data, int *len,
688 bool target_is_oa)
689{
690 /* NOTE: *data has an overall length of RTP_BUF_SIZE, so there is
691 * plenty of space available to store the slightly larger, converted
692 * data */
693
694 struct rtp_hdr *rtp_hdr;
695 unsigned int payload_len;
696 int rc;
697
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200698 if (*len < sizeof(struct rtp_hdr)) {
699 LOGPENDP(endp, DRTP, LOGL_ERROR, "AMR RTP packet too short (%d < %zu)\n", *len, sizeof(struct rtp_hdr));
700 return -EINVAL;
701 }
702
Philipp Maier228e5912019-03-05 13:56:59 +0100703 rtp_hdr = (struct rtp_hdr *)data;
704
705 payload_len = *len - sizeof(struct rtp_hdr);
706
707 if (osmo_amr_is_oa(rtp_hdr->data, payload_len)) {
708 if (!target_is_oa)
709 /* Input data is oa an target format is bwe
710 * ==> convert */
711 rc = osmo_amr_oa_to_bwe(rtp_hdr->data, payload_len);
712 else
713 /* Input data is already bew, but we accept it anyway
714 * ==> no conversion needed */
715 rc = payload_len;
716 } else {
717 if (target_is_oa)
718 /* Input data is bwe an target format is oa
719 * ==> convert */
720 rc = osmo_amr_bwe_to_oa(rtp_hdr->data, payload_len,
721 RTP_BUF_SIZE);
722 else
723 /* Input data is already oa, but we accept it anyway
724 * ==> no conversion needed */
725 rc = payload_len;
726 }
727 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200728 LOGPENDP(endp, DRTP, LOGL_ERROR,
729 "AMR RTP packet conversion failed\n");
Philipp Maier228e5912019-03-05 13:56:59 +0100730 return -EINVAL;
731 }
732
733 *len = rc + sizeof(struct rtp_hdr);
734
735 return 0;
736}
737
738/* Check if a conversion between octet-aligned and bandwith-efficient mode is
739 * indicated. */
740static bool amr_oa_bwe_convert_indicated(struct mgcp_rtp_codec *codec)
741{
742 if (codec->param_present == false)
743 return false;
744 if (!codec->param.amr_octet_aligned_present)
745 return false;
746 if (strcmp(codec->subtype_name, "AMR") != 0)
747 return false;
748 return true;
749}
750
751
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200752/* Return whether an RTP packet with AMR payload is in octet-aligned mode.
753 * Return 0 if in bandwidth-efficient mode, 1 for octet-aligned mode, and negative if the RTP data is invalid. */
754static int amr_oa_check(char *data, int len)
Philipp Maier228e5912019-03-05 13:56:59 +0100755{
756 struct rtp_hdr *rtp_hdr;
757 unsigned int payload_len;
758
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200759 if (len < sizeof(struct rtp_hdr))
760 return -EINVAL;
761
Philipp Maier228e5912019-03-05 13:56:59 +0100762 rtp_hdr = (struct rtp_hdr *)data;
763
764 payload_len = len - sizeof(struct rtp_hdr);
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200765 if (payload_len < sizeof(struct amr_hdr))
766 return -EINVAL;
Philipp Maier228e5912019-03-05 13:56:59 +0100767
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +0200768 return osmo_amr_is_oa(rtp_hdr->data, payload_len) ? 1 : 0;
Philipp Maier228e5912019-03-05 13:56:59 +0100769}
770
Philipp Maier87bd9be2017-08-22 16:35:41 +0200771/* Forward data to a debug tap. This is debug function that is intended for
772 * debugging the voice traffic with tools like gstreamer */
Philipp Maiere6f172d2017-11-07 12:00:01 +0100773static void forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf,
774 int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200775{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100776 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200777
Philipp Maiere6f172d2017-11-07 12:00:01 +0100778 if (!tap->enabled)
779 return;
780
781 rc = sendto(fd, buf, len, 0, (struct sockaddr *)&tap->forward,
782 sizeof(tap->forward));
783
784 if (rc < 0)
785 LOGP(DRTP, LOGL_ERROR,
786 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200787}
788
Philipp Maier87bd9be2017-08-22 16:35:41 +0200789/*! Send RTP/RTCP data to a specified destination connection.
790 * \param[in] endp associated endpoint (for configuration, logging)
791 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP
792 * \param[in] spoofed source address (set to NULL to disable)
793 * \param[in] buf buffer that contains the RTP/RTCP data
794 * \param[in] len length of the buffer that contains the RTP/RTCP data
795 * \param[in] conn_src associated source connection
796 * \param[in] conn_dst associated destination connection
797 * \returns 0 on success, -1 on ERROR */
798int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
799 char *buf, int len, struct mgcp_conn_rtp *conn_src,
800 struct mgcp_conn_rtp *conn_dst)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200801{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200802 /*! When no destination connection is available (e.g. when only one
803 * connection in loopback mode exists), then the source connection
804 * shall be specified as destination connection */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200805
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200806 struct mgcp_trunk_config *tcfg = endp->tcfg;
807 struct mgcp_rtp_end *rtp_end;
808 struct mgcp_rtp_state *rtp_state;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200809 char *dest_name;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200810 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200811
Philipp Maier87bd9be2017-08-22 16:35:41 +0200812 OSMO_ASSERT(conn_src);
813 OSMO_ASSERT(conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200814
Philipp Maier87bd9be2017-08-22 16:35:41 +0200815 if (is_rtp) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200816 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTP packet...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200817 } else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200818 LOGPENDP(endp, DRTP, LOGL_DEBUG, "delivering RTCP packet...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200819 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200820
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200821 LOGPENDP(endp, DRTP, LOGL_DEBUG, "loop:%d, mode:%d%s\n",
822 tcfg->audio_loop, conn_src->conn->mode,
823 conn_src->conn->mode == MGCP_CONN_LOOPBACK ? " (loopback)" : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200824
Philipp Maier6931f9a2018-07-26 09:29:31 +0200825 /* FIXME: It is legal that the payload type on the egress connection is
826 * different from the payload type that has been negotiated on the
827 * ingress connection. Essentially the codecs are the same so we can
828 * match them and patch the payload type. However, if we can not find
829 * the codec pendant (everything ist equal except the PT), we are of
830 * course unable to patch the payload type. A situation like this
831 * should not occur if transcoding is consequently avoided. Until
832 * we have transcoding support in osmo-mgw we can not resolve this. */
Philipp Maierda895b12018-08-03 12:16:37 +0200833 if (is_rtp) {
834 rc = mgcp_patch_pt(conn_src, conn_dst, buf, len);
835 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200836 LOGPENDP(endp, DRTP, LOGL_DEBUG,
837 "can not patch PT because no suitable egress codec was found.\n");
Philipp Maierda895b12018-08-03 12:16:37 +0200838 }
Philipp Maier6931f9a2018-07-26 09:29:31 +0200839 }
840
Philipp Maier87bd9be2017-08-22 16:35:41 +0200841 /* Note: In case of loopback configuration, both, the source and the
842 * destination will point to the same connection. */
843 rtp_end = &conn_dst->end;
844 rtp_state = &conn_src->state;
845 dest_name = conn_dst->conn->name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200846
847 if (!rtp_end->output_enabled) {
Philipp Maiercede2a42018-07-03 14:14:21 +0200848 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_DROPPED_PACKETS_CTR]);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200849 LOGPENDP(endp, DRTP, LOGL_DEBUG,
850 "output disabled, drop to %s %s "
851 "rtp_port:%u rtcp_port:%u\n",
852 dest_name,
853 inet_ntoa(rtp_end->addr),
854 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200855 );
856 } else if (is_rtp) {
857 int cont;
858 int nbytes = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200859 int buflen = len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200860 do {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200861 /* Run transcoder */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200862 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200863 buf, &buflen,
864 RTP_BUF_SIZE);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200865 if (cont < 0)
866 break;
867
Philipp Maier87bd9be2017-08-22 16:35:41 +0200868 if (addr)
869 mgcp_patch_and_count(endp, rtp_state, rtp_end,
870 addr, buf, buflen);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100871
Philipp Maier228e5912019-03-05 13:56:59 +0100872 if (amr_oa_bwe_convert_indicated(conn_dst->end.codec)) {
873 amr_oa_bwe_convert(endp, buf, &buflen,
874 conn_dst->end.codec->param.amr_octet_aligned);
875 }
876 else if (rtp_end->rfc5993_hr_convert
Philipp Maier9fc8a022019-02-20 12:26:52 +0100877 && strcmp(conn_src->end.codec->subtype_name,
878 "GSM-HR-08") == 0)
879 rfc5993_hr_convert(endp, buf, &buflen);
880
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200881 LOGPENDP(endp, DRTP, LOGL_DEBUG,
882 "process/send to %s %s "
883 "rtp_port:%u rtcp_port:%u\n",
884 dest_name, inet_ntoa(rtp_end->addr),
885 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
886 );
Philipp Maier87bd9be2017-08-22 16:35:41 +0200887
888 /* Forward a copy of the RTP data to a debug ip/port */
889 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
890 buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200891
892 /* FIXME: HACK HACK HACK. See OS#2459.
893 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
894 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
895 * cells (as long as we patch only the first RTP payload in each stream).
896 */
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200897 if (!rtp_state->patched_first_rtp_payload
898 && conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200899 uint8_t *data = (uint8_t *) & buf[12];
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200900 if (data[0] == 0xe0) {
901 data[0] = 0xe4;
902 data[1] = 0x00;
903 rtp_state->patched_first_rtp_payload = true;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200904 LOGPENDP(endp, DRTP, LOGL_DEBUG,
905 "Patching over first two bytes"
906 " to fake an IuUP Initialization Ack\n");
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200907 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200908 }
909
Philipp Maier87bd9be2017-08-22 16:35:41 +0200910 len = mgcp_udp_send(rtp_end->rtp.fd,
911 &rtp_end->addr,
912 rtp_end->rtp_port, buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200913
Philipp Maier87bd9be2017-08-22 16:35:41 +0200914 if (len <= 0)
915 return len;
916
Philipp Maiercede2a42018-07-03 14:14:21 +0200917 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
918 rate_ctr_add(&conn_dst->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], len);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200919
920 nbytes += len;
921 buflen = cont;
922 } while (buflen > 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200923 return nbytes;
924 } else if (!tcfg->omit_rtcp) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200925 LOGPENDP(endp, DRTP, LOGL_DEBUG,
926 "send to %s %s rtp_port:%u rtcp_port:%u\n",
927 dest_name, inet_ntoa(rtp_end->addr),
928 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
929 );
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200930
Philipp Maier87bd9be2017-08-22 16:35:41 +0200931 len = mgcp_udp_send(rtp_end->rtcp.fd,
932 &rtp_end->addr,
933 rtp_end->rtcp_port, buf, len);
934
Philipp Maiercede2a42018-07-03 14:14:21 +0200935 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
936 rate_ctr_add(&conn_dst->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], len);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200937
938 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200939 }
940
941 return 0;
942}
943
Philipp Maier87bd9be2017-08-22 16:35:41 +0200944/* Helper function for mgcp_recv(),
945 Receive one RTP Packet + Originating address from file descriptor */
946static int receive_from(struct mgcp_endpoint *endp, int fd,
947 struct sockaddr_in *addr, char *buf, int bufsize)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200948{
949 int rc;
950 socklen_t slen = sizeof(*addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200951 struct sockaddr_in addr_sink;
952 char buf_sink[RTP_BUF_SIZE];
953 bool tossed = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200954
Philipp Maier87bd9be2017-08-22 16:35:41 +0200955 if (!addr)
956 addr = &addr_sink;
957 if (!buf) {
958 tossed = true;
959 buf = buf_sink;
960 bufsize = sizeof(buf_sink);
961 }
962
963 rc = recvfrom(fd, buf, bufsize, 0, (struct sockaddr *)addr, &slen);
964
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200965 LOGPENDP(endp, DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200966 "receiving %u bytes length packet from %s:%u ...\n",
967 rc, inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
968
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200969 if (rc < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200970 LOGPENDP(endp, DRTP, LOGL_ERROR,
971 "failed to receive packet, errno: %d/%s\n",
972 errno, strerror(errno));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200973 return -1;
974 }
975
Philipp Maier87bd9be2017-08-22 16:35:41 +0200976 if (tossed) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200977 LOGPENDP(endp, DRTP, LOGL_ERROR, "packet tossed\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200978 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200979
980 return rc;
981}
982
Philipp Maier87bd9be2017-08-22 16:35:41 +0200983/* Check if the origin (addr) matches the address/port data of the RTP
984 * connections. */
985static int check_rtp_origin(struct mgcp_conn_rtp *conn,
986 struct sockaddr_in *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200987{
Harald Weltec26b6652018-10-21 12:01:04 +0200988 if (conn->end.addr.s_addr == 0) {
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200989 switch (conn->conn->mode) {
990 case MGCP_CONN_LOOPBACK:
991 /* HACK: for IuUP, we want to reply with an IuUP Initialization ACK upon the first RTP
992 * message received. We currently hackishly accomplish that by putting the endpoint in
993 * loopback mode and patching over the looped back RTP message to make it look like an
994 * ack. We don't know the femto cell's IP address and port until the RAB Assignment
995 * Response is received, but the nano3G expects an IuUP Initialization Ack before it even
996 * sends the RAB Assignment Response. Hence, if the remote address is 0.0.0.0 and the
997 * MGCP port is in loopback mode, allow looping back the packet to any source. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200998 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
999 "In loopback mode and remote address not set:"
1000 " allowing data from address: %s\n", inet_ntoa(addr->sin_addr));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +02001001 return 0;
1002
1003 default:
1004 /* Receiving early media before the endpoint is configured. Instead of logging
1005 * this as an error that occurs on every call, keep it more low profile to not
1006 * confuse humans with expected errors. */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001007 LOGPCONN(conn->conn, DRTP, LOGL_INFO,
1008 "Rx RTP from %s, but remote address not set:"
1009 " dropping early media\n", inet_ntoa(addr->sin_addr));
Neels Hofmeyrefd645e2018-09-10 13:14:50 +02001010 return -1;
1011 }
Neels Hofmeyr0063ca22018-07-23 18:12:16 +02001012 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001013
Philipp Maier87bd9be2017-08-22 16:35:41 +02001014 /* Note: Check if the inbound RTP data comes from the same host to
1015 * which we send our outgoing RTP traffic. */
Harald Weltec26b6652018-10-21 12:01:04 +02001016 if (conn->end.addr.s_addr != addr->sin_addr.s_addr) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001017 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1018 "data from wrong address: %s, ", inet_ntoa(addr->sin_addr));
Philipp Maierc3413882017-10-27 12:26:54 +02001019 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001020 inet_ntoa(conn->end.addr));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001021 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001022 return -1;
1023 }
1024
Philipp Maier87bd9be2017-08-22 16:35:41 +02001025 /* Note: Usually the remote remote port of the data we receive will be
1026 * the same as the remote port where we transmit outgoing RTP traffic
1027 * to (set by MDCX). We use this to check the origin of the data for
1028 * plausibility. */
1029 if (conn->end.rtp_port != addr->sin_port &&
1030 conn->end.rtcp_port != addr->sin_port) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001031 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1032 "data from wrong source port: %d, ", ntohs(addr->sin_port));
Philipp Maierc3413882017-10-27 12:26:54 +02001033 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001034 "expected: %d for RTP or %d for RTCP\n",
1035 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001036 LOGPCONN(conn->conn, DRTP, LOGL_ERROR, "packet tossed\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001037 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001038 }
1039
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001040 return 0;
1041}
1042
Philipp Maier87bd9be2017-08-22 16:35:41 +02001043/* Check the if the destination address configuration of an RTP connection
1044 * makes sense */
1045static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001046{
Philipp Maiere6df0e42018-05-29 14:03:06 +02001047 /* Note: it is legal to create a connection but never setting a port
1048 * and IP-address for outgoing data. */
1049 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 +02001050 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
1051 "destination IP-address and rtp port is (not yet) known (%s:%u)\n",
1052 inet_ntoa(conn->end.addr), conn->end.rtp_port);
Philipp Maiere6df0e42018-05-29 14:03:06 +02001053 return -1;
1054 }
1055
Philipp Maier87bd9be2017-08-22 16:35:41 +02001056 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001057 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1058 "destination IP-address is invalid (%s:%u)\n",
1059 inet_ntoa(conn->end.addr), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001060 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001061 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001062
1063 if (conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001064 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1065 "destination rtp port is invalid (%s:%u)\n",
1066 inet_ntoa(conn->end.addr), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001067 return -1;
1068 }
1069
1070 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001071}
1072
Philipp Maier4dba7692018-08-03 12:20:52 +02001073/* Do some basic checks to make sure that the RTCP packets we are going to
1074 * process are not complete garbage */
1075static int check_rtcp(char *buf, unsigned int buf_size)
1076{
1077 struct rtcp_hdr *hdr;
1078 unsigned int len;
1079 uint8_t type;
1080
1081 /* RTPC packets that are just a header without data do not make
1082 * any sense. */
1083 if (buf_size < sizeof(struct rtcp_hdr))
1084 return -EINVAL;
1085
1086 /* Make sure that the length of the received packet does not exceed
1087 * the available buffer size */
1088 hdr = (struct rtcp_hdr *)buf;
1089 len = (osmo_ntohs(hdr->length) + 1) * 4;
1090 if (len > buf_size)
1091 return -EINVAL;
1092
1093 /* Make sure we accept only packets that have a proper packet type set
1094 * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
1095 type = hdr->type;
1096 if ((type < 192 || type > 195) && (type < 200 || type > 213))
1097 return -EINVAL;
1098
1099 return 0;
1100}
1101
1102/* Do some basic checks to make sure that the RTP packets we are going to
1103 * process are not complete garbage */
1104static int check_rtp(char *buf, unsigned int buf_size)
1105{
1106 /* RTP packets that are just a header without data do not make
1107 * any sense. */
1108 if (buf_size < sizeof(struct rtp_hdr))
1109 return -EINVAL;
1110
1111 /* FIXME: Add more checks, the reason why we do not check more than
1112 * the length is because we currently handle IUUP packets as RTP
1113 * packets, so they must pass this check, if we weould be more
1114 * strict here, we would possibly break 3G. (see also FIXME note
1115 * below */
1116
1117 return 0;
1118}
1119
Philipp Maier87bd9be2017-08-22 16:35:41 +02001120/* Receive RTP data from a specified source connection and dispatch it to a
1121 * destination connection. */
1122static int mgcp_recv(int *proto, struct sockaddr_in *addr, char *buf,
1123 unsigned int buf_size, struct osmo_fd *fd)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001124{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001125 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001126 struct mgcp_conn_rtp *conn;
1127 struct mgcp_trunk_config *tcfg;
1128 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001129
Philipp Maier87bd9be2017-08-22 16:35:41 +02001130 conn = (struct mgcp_conn_rtp*) fd->data;
1131 endp = conn->conn->endp;
1132 tcfg = endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001133
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001134 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG, "receiving RTP/RTCP packet...\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001135
1136 rc = receive_from(endp, fd->fd, addr, buf, buf_size);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001137 if (rc <= 0)
1138 return -1;
Philipp Maier4dba7692018-08-03 12:20:52 +02001139
1140 /* FIXME: The way how we detect the protocol looks odd. We should look
1141 * into the packet header. Also we should introduce a packet type
1142 * MGCP_PROTO_IUUP because currently we handle IUUP packets like RTP
1143 * packets which is problematic. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001144 *proto = fd == &conn->end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001145
Philipp Maier4dba7692018-08-03 12:20:52 +02001146 if (*proto == MGCP_PROTO_RTP) {
1147 if (check_rtp(buf, rc) < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001148 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1149 "invalid RTP packet received -- packet tossed\n");
Philipp Maier4dba7692018-08-03 12:20:52 +02001150 return -1;
1151 }
1152 } else if (*proto == MGCP_PROTO_RTCP) {
1153 if (check_rtcp(buf, rc) < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001154 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1155 "invalid RTCP packet received -- packet tossed\n");
Philipp Maier4dba7692018-08-03 12:20:52 +02001156 return -1;
1157 }
1158 }
1159
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001160 LOGPCONN(conn->conn, DRTP, LOGL_DEBUG, "");
Neels Hofmeyr56725632018-01-15 15:15:07 +01001161 LOGPC(DRTP, LOGL_DEBUG, "receiving from %s %s %d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001162 conn->conn->name, inet_ntoa(addr->sin_addr),
1163 ntohs(addr->sin_port));
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001164 LOGPENDP(endp, DRTP, LOGL_DEBUG, "conn:%s\n", mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001165
Philipp Maier87bd9be2017-08-22 16:35:41 +02001166 /* Check if the origin of the RTP packet seems plausible */
1167 if (tcfg->rtp_accept_all == 0) {
1168 if (check_rtp_origin(conn, addr) != 0)
1169 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001170 }
1171
Philipp Maier87bd9be2017-08-22 16:35:41 +02001172 /* Filter out dummy message */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001173 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001174 LOGPCONN(conn->conn, DRTP, LOGL_NOTICE,
1175 "dummy message received\n");
1176 LOGPCONN(conn->conn, DRTP, LOGL_ERROR,
1177 "packet tossed\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001178 return 0;
1179 }
1180
Philipp Maier87bd9be2017-08-22 16:35:41 +02001181 /* Increment RX statistics */
Philipp Maiercede2a42018-07-03 14:14:21 +02001182 rate_ctr_inc(&conn->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR]);
1183 rate_ctr_add(&conn->rate_ctr_group->ctr[RTP_OCTETS_RX_CTR], rc);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001184
Philipp Maier87bd9be2017-08-22 16:35:41 +02001185 /* Forward a copy of the RTP data to a debug ip/port */
1186 forward_data(fd->fd, &conn->tap_in, buf, rc);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001187
Philipp Maier87bd9be2017-08-22 16:35:41 +02001188 return rc;
1189}
1190
1191/* Send RTP data. Possible options are standard RTP packet
1192 * transmission or trsmission via an osmux connection */
1193static int mgcp_send_rtp(int proto, struct sockaddr_in *addr, char *buf,
1194 unsigned int buf_size,
1195 struct mgcp_conn_rtp *conn_src,
1196 struct mgcp_conn_rtp *conn_dst)
1197{
1198 struct mgcp_endpoint *endp;
1199 endp = conn_src->conn->endp;
1200
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001201 LOGPENDP(endp, DRTP, LOGL_DEBUG, "destin conn:%s\n",
1202 mgcp_conn_dump(conn_dst->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001203
1204 /* Before we try to deliver the packet, we check if the destination
1205 * port and IP-Address make sense at all. If not, we will be unable
1206 * to deliver the packet. */
1207 if (check_rtp_destin(conn_dst) != 0)
1208 return -1;
1209
1210 /* Depending on the RTP connection type, deliver the RTP packet to the
1211 * destination connection. */
1212 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001213 case MGCP_RTP_DEFAULT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001214 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1215 "endpoint type is MGCP_RTP_DEFAULT, "
1216 "using mgcp_send() to forward data directly\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001217 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
1218 addr, buf, buf_size, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001219 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001220 case MGCP_OSMUX_BSC:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001221 LOGPENDP(endp, DRTP, LOGL_DEBUG,
1222 "endpoint type is MGCP_OSMUX_BSC_NAT, "
1223 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001224 return osmux_xfrm_to_osmux(buf, buf_size, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001225 }
1226
Philipp Maier87bd9be2017-08-22 16:35:41 +02001227 /* If the data has not been handled/forwarded until here, it will
1228 * be discarded, this should not happen, normally the MGCP type
1229 * should be properly set */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001230 LOGPENDP(endp, DRTP, LOGL_ERROR, "bad MGCP type -- data discarded!\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001231
1232 return -1;
1233}
1234
1235/*! dispatch incoming RTP packet to opposite RTP connection.
1236 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP)
1237 * \param[in] addr socket address where the RTP packet has been received from
1238 * \param[in] buf buffer that hold the RTP payload
1239 * \param[in] buf_size size data length of buf
1240 * \param[in] conn originating connection
1241 * \returns 0 on success, -1 on ERROR */
1242int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf,
1243 unsigned int buf_size, struct mgcp_conn *conn)
1244{
1245 struct mgcp_conn *conn_dst;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001246
1247 /*! NOTE: This callback function implements the endpoint specific
1248 * dispatch bahviour of an rtp bridge/proxy endpoint. It is assumed
1249 * that the endpoint will hold only two connections. This premise
1250 * is used to determine the opposite connection (it is always the
1251 * connection that is not the originating connection). Once the
1252 * destination connection is known the RTP packet is sent via
1253 * the destination connection. */
1254
Pau Espin Pedrol9aaaab62019-05-15 23:23:27 +02001255
1256 /* Check if the connection is in loopback mode, if yes, just send the
1257 * incoming data back to the origin */
1258 if (conn->mode == MGCP_CONN_LOOPBACK) {
1259 /* When we are in loopback mode, we loop back all incoming
1260 * packets back to their origin. We will use the originating
1261 * address data from the UDP packet header to patch the
1262 * outgoing address in connection on the fly */
1263 if (conn->u.rtp.end.rtp_port == 0) {
1264 conn->u.rtp.end.addr = addr->sin_addr;
1265 conn->u.rtp.end.rtp_port = addr->sin_port;
1266 }
1267 return mgcp_send_rtp(proto, addr, buf,
1268 buf_size, &conn->u.rtp, &conn->u.rtp);
1269 }
1270
Philipp Maier87bd9be2017-08-22 16:35:41 +02001271 /* Find a destination connection. */
1272 /* NOTE: This code path runs every time an RTP packet is received. The
1273 * function mgcp_find_dst_conn() we use to determine the detination
1274 * connection will iterate the connection list inside the endpoint.
1275 * Since list iterations are quite costly, we will figure out the
1276 * destination only once and use the optional private data pointer of
1277 * the connection to cache the destination connection pointer. */
1278 if (!conn->priv) {
1279 conn_dst = mgcp_find_dst_conn(conn);
1280 conn->priv = conn_dst;
1281 } else {
1282 conn_dst = (struct mgcp_conn *)conn->priv;
1283 }
1284
1285 /* There is no destination conn, stop here */
1286 if (!conn_dst) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001287 LOGPCONN(conn, DRTP, LOGL_ERROR,
1288 "unable to find destination conn\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001289 return -1;
1290 }
1291
1292 /* The destination conn is not an RTP connection */
1293 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001294 LOGPCONN(conn, DRTP, LOGL_ERROR,
1295 "unable to find suitable destination conn\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001296 return -1;
1297 }
1298
1299 /* Dispatch RTP packet to destination RTP connection */
1300 return mgcp_send_rtp(proto, addr, buf,
1301 buf_size, &conn->u.rtp, &conn_dst->u.rtp);
1302
1303}
1304
Philipp Maierdf5d2192018-01-24 11:39:32 +01001305/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1306 * \param[in] endp Endpoint on which the connection resides.
1307 * \param[in] conn Connection that is about to be removed (ignored).
1308 * \returns 0 on success, -1 on ERROR. */
1309void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1310{
1311 struct mgcp_conn *conn_cleanup;
1312
1313 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1314 * pointer to the destination connection, so that we do not have
1315 * to go through the list every time an RTP packet arrives. To prevent
1316 * a use-after-free situation we invalidate this information for all
1317 * connections present when one connection is removed from the
1318 * endpoint. */
1319 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
1320 conn_cleanup->priv = NULL;
1321 }
1322}
1323
Philipp Maier87bd9be2017-08-22 16:35:41 +02001324/* Handle incoming RTP data from NET */
1325static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1326{
1327 /* NOTE: This is a generic implementation. RTP data is received. In
1328 * case of loopback the data is just sent back to its origin. All
1329 * other cases implement endpoint specific behaviour (e.g. how is the
1330 * destination connection determined?). That specific behaviour is
1331 * implemented by the callback function that is called at the end of
1332 * the function */
1333
1334 struct mgcp_conn_rtp *conn_src;
1335 struct mgcp_endpoint *endp;
1336 struct sockaddr_in addr;
1337
1338 char buf[RTP_BUF_SIZE];
1339 int proto;
Philipp Maierb9694552017-11-08 16:53:57 +01001340 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001341
1342 conn_src = (struct mgcp_conn_rtp *)fd->data;
1343 OSMO_ASSERT(conn_src);
1344 endp = conn_src->conn->endp;
1345 OSMO_ASSERT(endp);
1346
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001347 LOGPENDP(endp, DRTP, LOGL_DEBUG, "source conn:%s\n",
1348 mgcp_conn_dump(conn_src->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001349
1350 /* Receive packet */
Philipp Maierb9694552017-11-08 16:53:57 +01001351 len = mgcp_recv(&proto, &addr, buf, sizeof(buf), fd);
1352 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001353 return -1;
1354
Oliver Smithe36b7752019-01-22 16:31:36 +01001355 mgcp_conn_watchdog_kick(conn_src->conn);
1356
Philipp Maier228e5912019-03-05 13:56:59 +01001357 /* If AMR is configured for the ingress connection a conversion of the
1358 * framing mode (octet-aligned vs. bandwith-efficient is explicitly
1359 * define, then we check if the incoming payload matches that
1360 * expectation. */
1361 if (amr_oa_bwe_convert_indicated(conn_src->end.codec)) {
Neels Hofmeyr7c6dd3c2019-08-20 03:06:17 +02001362 int oa = amr_oa_check(buf, len);
1363 if (oa < 0)
1364 return -1;
1365 if (((bool)oa) != conn_src->end.codec->param.amr_octet_aligned)
Philipp Maier228e5912019-03-05 13:56:59 +01001366 return -1;
1367 }
1368
Philipp Maier87bd9be2017-08-22 16:35:41 +02001369 /* Execute endpoint specific implementation that handles the
1370 * dispatching of the RTP data */
Philipp Maierb9694552017-11-08 16:53:57 +01001371 return endp->type->dispatch_rtp_cb(proto, &addr, buf, len,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001372 conn_src->conn);
1373}
1374
1375/*! set IP Type of Service parameter.
1376 * \param[in] fd associated file descriptor
1377 * \param[in] tos dscp value
1378 * \returns 0 on success, -1 on ERROR */
1379int mgcp_set_ip_tos(int fd, int tos)
1380{
1381 int ret;
1382 ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1383
1384 if (ret < 0)
1385 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001386 return 0;
1387}
1388
Philipp Maier87bd9be2017-08-22 16:35:41 +02001389/*! bind RTP port to osmo_fd.
1390 * \param[in] source_addr source (local) address to bind on
1391 * \param[in] fd associated file descriptor
1392 * \param[in] port to bind on
1393 * \returns 0 on success, -1 on ERROR */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001394int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
1395{
Harald Welte8890dfa2017-11-17 15:09:30 +01001396 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001397
Harald Welte8890dfa2017-11-17 15:09:30 +01001398 rc = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
1399 NULL, 0, OSMO_SOCK_F_BIND);
1400 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001401 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001402 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001403 return -1;
1404 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001405 fd->fd = rc;
1406 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001407
1408 return 0;
1409}
1410
Philipp Maier87bd9be2017-08-22 16:35:41 +02001411/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001412static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001413 struct mgcp_rtp_end *rtp_end, int endpno)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001414{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001415 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1416 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001417 struct mgcp_endpoint *endp = &cfg->trunk.endpoints[endpno];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001418
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001419 if (mgcp_create_bind(source_addr, &rtp_end->rtp,
1420 rtp_end->local_port) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001421 LOGPENDP(endp, DRTP, LOGL_ERROR,
1422 "failed to create RTP port: %s:%d\n",
1423 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001424 goto cleanup0;
1425 }
1426
1427 if (mgcp_create_bind(source_addr, &rtp_end->rtcp,
1428 rtp_end->local_port + 1) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001429 LOGPENDP(endp, DRTP, LOGL_ERROR,
1430 "failed to create RTCP port: %s:%d\n",
1431 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001432 goto cleanup1;
1433 }
1434
Philipp Maier87bd9be2017-08-22 16:35:41 +02001435 /* Set Type of Service (DSCP-Value) as configured via VTY */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001436 mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
1437 mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
1438
1439 rtp_end->rtp.when = BSC_FD_READ;
1440 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001441 LOGPENDP(endp, DRTP, LOGL_ERROR,
1442 "failed to register RTP port %d\n",
1443 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001444 goto cleanup2;
1445 }
1446
1447 rtp_end->rtcp.when = BSC_FD_READ;
1448 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001449 LOGPENDP(endp, DRTP, LOGL_ERROR,
1450 "failed to register RTCP port %d\n",
1451 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001452 goto cleanup3;
1453 }
1454
1455 return 0;
1456
1457cleanup3:
1458 osmo_fd_unregister(&rtp_end->rtp);
1459cleanup2:
1460 close(rtp_end->rtcp.fd);
1461 rtp_end->rtcp.fd = -1;
1462cleanup1:
1463 close(rtp_end->rtp.fd);
1464 rtp_end->rtp.fd = -1;
1465cleanup0:
1466 return -1;
1467}
1468
Philipp Maier87bd9be2017-08-22 16:35:41 +02001469/*! bind RTP port to endpoint/connection.
1470 * \param[in] endp endpoint that holds the RTP connection
1471 * \param[in] rtp_port port number to bind on
1472 * \param[in] conn associated RTP connection
1473 * \returns 0 on success, -1 on ERROR */
1474int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1475 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001476{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001477 char name[512];
1478 struct mgcp_rtp_end *end;
Philipp Maier1cb1e382017-11-02 17:16:04 +01001479 char local_ip_addr[INET_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001480
Philipp Maier01d24a32017-11-21 17:26:09 +01001481 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001482 end = &conn->end;
1483
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001484 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001485 LOGPENDP(endp, DRTP, LOGL_ERROR, "%u was already bound on conn:%s\n",
1486 rtp_port, mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001487
1488 /* Double bindings should never occour! Since we always allocate
1489 * connections dynamically and free them when they are not
1490 * needed anymore, there must be no previous binding leftover.
1491 * Should there be a connection bound twice, we have a serious
1492 * problem and must exit immediately! */
1493 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001494 }
1495
1496 end->local_port = rtp_port;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001497 end->rtp.cb = rtp_data_net;
1498 end->rtp.data = conn;
1499 end->rtcp.data = conn;
1500 end->rtcp.cb = rtp_data_net;
1501
Philipp Maier1cb1e382017-11-02 17:16:04 +01001502 mgcp_get_local_addr(local_ip_addr, conn);
1503
1504 return bind_rtp(endp->cfg, local_ip_addr, end,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001505 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001506}
1507
Philipp Maier87bd9be2017-08-22 16:35:41 +02001508/*! free allocated RTP and RTCP ports.
1509 * \param[in] end RTP end */
1510void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001511{
1512 if (end->rtp.fd != -1) {
1513 close(end->rtp.fd);
1514 end->rtp.fd = -1;
1515 osmo_fd_unregister(&end->rtp);
1516 }
1517
1518 if (end->rtcp.fd != -1) {
1519 close(end->rtcp.fd);
1520 end->rtcp.fd = -1;
1521 osmo_fd_unregister(&end->rtcp);
1522 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001523}