blob: 4a1eb74fdfe78bb1cbd38e4b808019dbb364ee80 [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 <sys/socket.h>
31#include <arpa/inet.h>
32
33#include <osmocom/core/msgb.h>
34#include <osmocom/core/select.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020035#include <osmocom/netif/rtp.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020036#include <osmocom/mgcp/mgcp.h>
37#include <osmocom/mgcp/mgcp_internal.h>
38#include <osmocom/mgcp/mgcp_stat.h>
39#include <osmocom/mgcp/osmux.h>
40#include <osmocom/mgcp/mgcp_conn.h>
41#include <osmocom/mgcp/mgcp_ep.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020042
43#define RTP_SEQ_MOD (1 << 16)
44#define RTP_MAX_DROPOUT 3000
45#define RTP_MAX_MISORDER 100
46#define RTP_BUF_SIZE 4096
47
48enum {
49 MGCP_PROTO_RTP,
50 MGCP_PROTO_RTCP,
51};
52
Philipp Maier87bd9be2017-08-22 16:35:41 +020053/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020054 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +020055 * 1/codec_rate seconds. */
56static uint32_t get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020057{
58 struct timespec tp;
59 uint64_t ret;
60
Philipp Maier87bd9be2017-08-22 16:35:41 +020061 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020062 return 0;
63
64 memset(&tp, 0, sizeof(tp));
65 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +020066 LOGP(DLMGCP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020067
68 /* convert it to 1/unit seconds */
69 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +020070 ret *= codec_rate;
71 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020072
73 return ret;
74}
75
Philipp Maier87bd9be2017-08-22 16:35:41 +020076/*! send udp packet.
77 * \param[in] fd associated file descriptor
78 * \param[in] addr destination ip-address
79 * \param[in] port destination UDP port
80 * \param[in] buf buffer that holds the data to be send
81 * \param[in] len length of the data to be sent
82 * \returns bytes sent, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020083int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
84{
85 struct sockaddr_in out;
Philipp Maier87bd9be2017-08-22 16:35:41 +020086
87 LOGP(DLMGCP, LOGL_DEBUG,
88 "sending %i bytes length packet to %s:%u ...\n",
89 len, inet_ntoa(*addr), ntohs(port));
90
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020091 out.sin_family = AF_INET;
92 out.sin_port = port;
93 memcpy(&out.sin_addr, addr, sizeof(*addr));
94
95 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
96}
97
Philipp Maier87bd9be2017-08-22 16:35:41 +020098/*! send RTP dummy packet (to keep NAT connection open).
99 * \param[in] endp mcgp endpoint that holds the RTP connection
100 * \param[in] conn associated RTP connection
101 * \returns bytes sent, -1 on error */
102int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200103{
104 static char buf[] = { MGCP_DUMMY_LOAD };
105 int rc;
106 int was_rtcp = 0;
107
Philipp Maier87bd9be2017-08-22 16:35:41 +0200108 OSMO_ASSERT(endp);
109 OSMO_ASSERT(conn);
110
111 LOGP(DLMGCP, LOGL_DEBUG,
112 "endpoint:%x sending dummy packet...\n", ENDPOINT_NUMBER(endp));
113 LOGP(DLMGCP, LOGL_DEBUG, "endpoint:%x conn:%s\n",
114 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
115
116 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
117 conn->end.rtp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200118
119 if (rc == -1)
120 goto failed;
121
122 if (endp->tcfg->omit_rtcp)
123 return rc;
124
125 was_rtcp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200126 rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
127 conn->end.rtcp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200128
129 if (rc >= 0)
130 return rc;
131
132failed:
133 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200134 "endpoint:%x Failed to send dummy %s packet.\n",
135 ENDPOINT_NUMBER(endp), was_rtcp ? "RTCP" : "RTP");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200136
137 return -1;
138}
139
Philipp Maier87bd9be2017-08-22 16:35:41 +0200140/* Compute timestamp alignment error */
141static int32_t ts_alignment_error(struct mgcp_rtp_stream_state *sstate,
142 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200143{
144 int32_t timestamp_delta;
145
146 if (ptime == 0)
147 return 0;
148
149 /* Align according to: T - Tlast = k * Tptime */
150 timestamp_delta = timestamp - sstate->last_timestamp;
151
152 return timestamp_delta % ptime;
153}
154
Philipp Maier87bd9be2017-08-22 16:35:41 +0200155/* Check timestamp and sequence number for plausibility */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200156static int check_rtp_timestamp(struct mgcp_endpoint *endp,
157 struct mgcp_rtp_state *state,
158 struct mgcp_rtp_stream_state *sstate,
159 struct mgcp_rtp_end *rtp_end,
160 struct sockaddr_in *addr,
161 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200162 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200163{
164 int32_t tsdelta;
165 int32_t timestamp_error;
166
167 /* Not fully intialized, skip */
168 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
169 return 0;
170
171 if (seq == sstate->last_seq) {
172 if (timestamp != sstate->last_timestamp) {
173 sstate->err_ts_counter += 1;
174 LOGP(DLMGCP, LOGL_ERROR,
175 "The %s timestamp delta is != 0 but the sequence "
176 "number %d is the same, "
177 "TS offset: %d, SeqNo offset: %d "
178 "on 0x%x SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200179 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200180 text, seq,
181 state->timestamp_offset, state->seq_offset,
182 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200183 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200184 }
185 return 0;
186 }
187
188 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200189 (int32_t)(timestamp - sstate->last_timestamp) /
190 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200191
192 if (tsdelta == 0) {
193 /* Don't update *tsdelta_out */
194 LOGP(DLMGCP, LOGL_NOTICE,
195 "The %s timestamp delta is %d "
196 "on 0x%x SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200197 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200198 text, tsdelta,
199 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200200 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200201
202 return 0;
203 }
204
205 if (sstate->last_tsdelta != tsdelta) {
206 if (sstate->last_tsdelta) {
207 LOGP(DLMGCP, LOGL_INFO,
208 "The %s timestamp delta changes from %d to %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200209 "on 0x%x SSRC: %u timestamp: %u from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200210 text, sstate->last_tsdelta, tsdelta,
211 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200212 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200213 }
214 }
215
216 if (tsdelta_out)
217 *tsdelta_out = tsdelta;
218
219 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200220 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200221
222 if (timestamp_error) {
223 sstate->err_ts_counter += 1;
224 LOGP(DLMGCP, LOGL_NOTICE,
225 "The %s timestamp has an alignment error of %d "
226 "on 0x%x SSRC: %u "
227 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200228 "from %s:%d. ptime: %d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200229 text, timestamp_error,
230 ENDPOINT_NUMBER(endp), sstate->ssrc,
231 (int16_t)(seq - sstate->last_seq),
232 (int32_t)(timestamp - sstate->last_timestamp),
233 tsdelta,
234 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200235 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200236 }
237 return 1;
238}
239
240/* Set the timestamp offset according to the packet duration. */
241static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp,
242 struct mgcp_rtp_state *state,
243 struct mgcp_rtp_end *rtp_end,
244 struct sockaddr_in *addr,
245 int16_t delta_seq, uint32_t in_timestamp)
246{
247 int32_t tsdelta = state->packet_duration;
248 int timestamp_offset;
249 uint32_t out_timestamp;
250
251 if (tsdelta == 0) {
252 tsdelta = state->out_stream.last_tsdelta;
253 if (tsdelta != 0) {
254 LOGP(DLMGCP, LOGL_NOTICE,
255 "A fixed packet duration is not available on 0x%x, "
256 "using last output timestamp delta instead: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200257 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200258 ENDPOINT_NUMBER(endp), tsdelta,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200259 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200260 } else {
261 tsdelta = rtp_end->codec.rate * 20 / 1000;
262 LOGP(DLMGCP, LOGL_NOTICE,
263 "Fixed packet duration and last timestamp delta "
264 "are not available on 0x%x, "
265 "using fixed 20ms instead: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200266 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200267 ENDPOINT_NUMBER(endp), tsdelta,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200268 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200269 }
270 }
271
272 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
273 timestamp_offset = out_timestamp - in_timestamp;
274
275 if (state->timestamp_offset != timestamp_offset) {
276 state->timestamp_offset = timestamp_offset;
277
278 LOGP(DLMGCP, LOGL_NOTICE,
279 "Timestamp offset change on 0x%x SSRC: %u "
280 "SeqNo delta: %d, TS offset: %d, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200281 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200282 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
283 delta_seq, state->timestamp_offset,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200284 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200285 }
286
287 return timestamp_offset;
288}
289
290/* Set the timestamp offset according to the packet duration. */
291static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp,
292 struct mgcp_rtp_state *state,
293 struct mgcp_rtp_end *rtp_end,
294 struct sockaddr_in *addr,
295 uint32_t timestamp)
296{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200297 int ts_error = 0;
298 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200299 int ptime = state->packet_duration;
300
301 /* Align according to: T + Toffs - Tlast = k * Tptime */
302
Philipp Maier87bd9be2017-08-22 16:35:41 +0200303 ts_error = ts_alignment_error(&state->out_stream, ptime,
304 timestamp + state->timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200305
Philipp Maier87bd9be2017-08-22 16:35:41 +0200306 /* If there is an alignment error, we have to compensate it */
307 if (ts_error) {
308 state->timestamp_offset += ptime - ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200309
310 LOGP(DLMGCP, LOGL_NOTICE,
311 "Corrected timestamp alignment error of %d on 0x%x SSRC: %u "
312 "new TS offset: %d, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200313 "from %s:%d\n",
314 ts_error,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200315 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
316 state->timestamp_offset, inet_ntoa(addr->sin_addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200317 ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200318 }
319
Philipp Maier87bd9be2017-08-22 16:35:41 +0200320 /* Check we really managed to compensate the timestamp
321 * offset. There should not be any remaining error, failing
322 * here would point to a serous problem with the alingnment
323 * error computation fuction */
324 ts_check = ts_alignment_error(&state->out_stream, ptime,
325 timestamp + state->timestamp_offset);
326 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200327
Philipp Maier87bd9be2017-08-22 16:35:41 +0200328 /* Return alignment error before compensation */
329 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200330}
331
Philipp Maier87bd9be2017-08-22 16:35:41 +0200332/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
333 * \param[in] associated endpoint
334 * \param[in] destination RTP end
335 * \param[in,out] pointer to buffer with voice data
336 * \param[in] voice data length
337 * \param[in] maxmimum size of caller provided voice data buffer
338 * \returns ignores input parameters, return always 0 */
339int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
340 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200341 char *data, int *len, int buf_size)
342{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200343 LOGP(DLMGCP, LOGL_DEBUG, "endpoint:%x transcoding disabled\n",
344 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200345 return 0;
346}
347
Philipp Maier87bd9be2017-08-22 16:35:41 +0200348/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
349 * \param[in] associated endpoint
350 * \param[in] destination RTP end
351 * \param[in] source RTP end
352 * \returns ignores input parameters, return always 0 */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200353int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
354 struct mgcp_rtp_end *dst_end,
355 struct mgcp_rtp_end *src_end)
356{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200357 LOGP(DLMGCP, LOGL_DEBUG, "endpoint:%x transcoding disabled\n",
358 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200359 return 0;
360}
361
362void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
363 int *payload_type,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200364 const char **audio_name,
365 const char **fmtp_extra,
366 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200367{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200368 LOGP(DLMGCP, LOGL_DEBUG,
369 "endpoint:%x conn:%s using format defaults\n",
370 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
371
372 *payload_type = conn->end.codec.payload_type;
373 *audio_name = conn->end.codec.audio_name;
374 *fmtp_extra = conn->end.fmtp_extra;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200375}
376
Philipp Maier87bd9be2017-08-22 16:35:41 +0200377void mgcp_rtp_annex_count(struct mgcp_endpoint *endp,
378 struct mgcp_rtp_state *state, const uint16_t seq,
379 const int32_t transit, const uint32_t ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200380{
381 int32_t d;
382
383 /* initialize or re-initialize */
384 if (!state->stats_initialized || state->stats_ssrc != ssrc) {
385 state->stats_initialized = 1;
386 state->stats_base_seq = seq;
387 state->stats_max_seq = seq - 1;
388 state->stats_ssrc = ssrc;
389 state->stats_jitter = 0;
390 state->stats_transit = transit;
391 state->stats_cycles = 0;
392 } else {
393 uint16_t udelta;
394
Philipp Maier87bd9be2017-08-22 16:35:41 +0200395 /* The below takes the shape of the validation of
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200396 * Appendix A. Check if there is something weird with
397 * the sequence number, otherwise check for a wrap
398 * around in the sequence number.
399 * It can't wrap during the initialization so let's
400 * skip it here. The Appendix A probably doesn't have
Philipp Maier87bd9be2017-08-22 16:35:41 +0200401 * this issue because of the probation. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200402 udelta = seq - state->stats_max_seq;
403 if (udelta < RTP_MAX_DROPOUT) {
404 if (seq < state->stats_max_seq)
405 state->stats_cycles += RTP_SEQ_MOD;
406 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
407 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200408 "RTP seqno made a very large jump on 0x%x delta: %u\n",
409 ENDPOINT_NUMBER(endp), udelta);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200410 }
411 }
412
Philipp Maier87bd9be2017-08-22 16:35:41 +0200413 /* Calculate the jitter between the two packages. The TS should be
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200414 * taken closer to the read function. This was taken from the
415 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
Philipp Maier87bd9be2017-08-22 16:35:41 +0200416 * resolution. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200417 d = transit - state->stats_transit;
418 state->stats_transit = transit;
419 if (d < 0)
420 d = -d;
421 state->stats_jitter += d - ((state->stats_jitter + 8) >> 4);
422 state->stats_max_seq = seq;
423}
424
Philipp Maier87bd9be2017-08-22 16:35:41 +0200425/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200426 * some of the supported endpoints (e.g. the nanoBTS) can only handle
427 * one source and this code will patch RTP header to appear as if there
428 * is only one source.
429 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200430 * we receive will be seen as a switch in streams. */
431void mgcp_patch_and_count(struct mgcp_endpoint *endp,
432 struct mgcp_rtp_state *state,
433 struct mgcp_rtp_end *rtp_end,
434 struct sockaddr_in *addr, char *data, int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200435{
436 uint32_t arrival_time;
437 int32_t transit;
438 uint16_t seq;
439 uint32_t timestamp, ssrc;
440 struct rtp_hdr *rtp_hdr;
441 int payload = rtp_end->codec.payload_type;
442
443 if (len < sizeof(*rtp_hdr))
444 return;
445
Philipp Maier87bd9be2017-08-22 16:35:41 +0200446 rtp_hdr = (struct rtp_hdr *)data;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200447 seq = ntohs(rtp_hdr->sequence);
448 timestamp = ntohl(rtp_hdr->timestamp);
449 arrival_time = get_current_ts(rtp_end->codec.rate);
450 ssrc = ntohl(rtp_hdr->ssrc);
451 transit = arrival_time - timestamp;
452
453 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc);
454
455 if (!state->initialized) {
456 state->initialized = 1;
457 state->in_stream.last_seq = seq - 1;
458 state->in_stream.ssrc = state->orig_ssrc = ssrc;
459 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200460 state->packet_duration =
461 mgcp_rtp_packet_duration(endp, rtp_end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200462 state->out_stream = state->in_stream;
463 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200464 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200465 LOGP(DLMGCP, LOGL_INFO,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200466 "endpoint:%x initializing stream, SSRC: %u timestamp: %u "
467 "pkt-duration: %d, from %s:%d\n",
468 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
469 state->seq_offset, state->packet_duration,
470 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200471 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200472 state->packet_duration =
473 rtp_end->codec.rate * 20 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200474 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200475 "endpoint:%x fixed packet duration is not available, "
476 "using fixed 20ms instead: %d from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200477 ENDPOINT_NUMBER(endp), state->packet_duration,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200478 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200479 }
480 } else if (state->in_stream.ssrc != ssrc) {
481 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200482 "endpoint:%x SSRC changed: %u -> %u "
483 "from %s:%d\n",
484 ENDPOINT_NUMBER(endp),
485 state->in_stream.ssrc, rtp_hdr->ssrc,
486 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200487
488 state->in_stream.ssrc = ssrc;
489 if (rtp_end->force_constant_ssrc) {
490 int16_t delta_seq;
491
492 /* Always increment seqno by 1 */
493 state->seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200494 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200495
496 /* Estimate number of packets that would have been sent */
497 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200498 (arrival_time - state->in_stream.last_arrival_time
499 + state->packet_duration / 2) /
500 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200501
502 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
503 delta_seq, timestamp);
504
505 state->patch_ssrc = 1;
506 ssrc = state->orig_ssrc;
507 if (rtp_end->force_constant_ssrc != -1)
508 rtp_end->force_constant_ssrc -= 1;
509
510 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200511 "endpoint:%x SSRC patching enabled, SSRC: %u "
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200512 "SeqNo offset: %d, TS offset: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200513 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200514 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
515 state->seq_offset, state->timestamp_offset,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200516 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200517 }
518
519 state->in_stream.last_tsdelta = 0;
520 } else {
521 /* Compute current per-packet timestamp delta */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200522 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
523 addr, seq, timestamp, "input",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200524 &state->in_stream.last_tsdelta);
525
526 if (state->patch_ssrc)
527 ssrc = state->orig_ssrc;
528 }
529
530 /* Save before patching */
531 state->in_stream.last_timestamp = timestamp;
532 state->in_stream.last_seq = seq;
533 state->in_stream.last_arrival_time = arrival_time;
534
535 if (rtp_end->force_aligned_timing &&
536 state->out_stream.ssrc == ssrc && state->packet_duration)
537 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200538 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
539 timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200540
541 /* Store the updated SSRC back to the packet */
542 if (state->patch_ssrc)
543 rtp_hdr->ssrc = htonl(ssrc);
544
545 /* Apply the offset and store it back to the packet.
546 * This won't change anything if the offset is 0, so the conditional is
547 * omitted. */
548 seq += state->seq_offset;
549 rtp_hdr->sequence = htons(seq);
550 timestamp += state->timestamp_offset;
551 rtp_hdr->timestamp = htonl(timestamp);
552
553 /* Check again, whether the timestamps are still valid */
554 if (state->out_stream.ssrc == ssrc)
555 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
556 addr, seq, timestamp, "output",
557 &state->out_stream.last_tsdelta);
558
559 /* Save output values */
560 state->out_stream.last_seq = seq;
561 state->out_stream.last_timestamp = timestamp;
562 state->out_stream.ssrc = ssrc;
563
564 if (payload < 0)
565 return;
566
567#if 0
Philipp Maier87bd9be2017-08-22 16:35:41 +0200568 DEBUGP(DLMGCP,
569 "endpoint:%x payload hdr payload %u -> endp payload %u\n",
570 ENDPOINT_NUMBER(endp), rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200571 rtp_hdr->payload_type = payload;
572#endif
573}
574
Philipp Maier87bd9be2017-08-22 16:35:41 +0200575/* Forward data to a debug tap. This is debug function that is intended for
576 * debugging the voice traffic with tools like gstreamer */
577static int forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf,
578 int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200579{
580 if (!tap->enabled)
581 return 0;
582
583 return sendto(fd, buf, len, 0,
584 (struct sockaddr *)&tap->forward, sizeof(tap->forward));
585}
586
Philipp Maier87bd9be2017-08-22 16:35:41 +0200587/*! Send RTP/RTCP data to a specified destination connection.
588 * \param[in] endp associated endpoint (for configuration, logging)
589 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP
590 * \param[in] spoofed source address (set to NULL to disable)
591 * \param[in] buf buffer that contains the RTP/RTCP data
592 * \param[in] len length of the buffer that contains the RTP/RTCP data
593 * \param[in] conn_src associated source connection
594 * \param[in] conn_dst associated destination connection
595 * \returns 0 on success, -1 on ERROR */
596int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
597 char *buf, int len, struct mgcp_conn_rtp *conn_src,
598 struct mgcp_conn_rtp *conn_dst)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200599{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200600 /*! When no destination connection is available (e.g. when only one
601 * connection in loopback mode exists), then the source connection
602 * shall be specified as destination connection */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200603
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200604 struct mgcp_trunk_config *tcfg = endp->tcfg;
605 struct mgcp_rtp_end *rtp_end;
606 struct mgcp_rtp_state *rtp_state;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200607 char *dest_name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200608
Philipp Maier87bd9be2017-08-22 16:35:41 +0200609 OSMO_ASSERT(conn_src);
610 OSMO_ASSERT(conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200611
Philipp Maier87bd9be2017-08-22 16:35:41 +0200612 if (is_rtp) {
613 LOGP(DLMGCP, LOGL_DEBUG,
614 "endpoint:%x delivering RTP packet...\n",
615 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200616 } else {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200617 LOGP(DLMGCP, LOGL_DEBUG,
618 "endpoint:%x delivering RTCP packet...\n",
619 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200620 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200621
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200622 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200623 "endpoint:%x loop:%d, mode:%d ",
624 ENDPOINT_NUMBER(endp), tcfg->audio_loop, conn_src->conn->mode);
625 if (conn_src->conn->mode == MGCP_CONN_LOOPBACK)
626 LOGPC(DLMGCP, LOGL_DEBUG, "(loopback)\n");
627 else
628 LOGPC(DLMGCP, LOGL_DEBUG, "\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200629
Philipp Maier87bd9be2017-08-22 16:35:41 +0200630 /* Note: In case of loopback configuration, both, the source and the
631 * destination will point to the same connection. */
632 rtp_end = &conn_dst->end;
633 rtp_state = &conn_src->state;
634 dest_name = conn_dst->conn->name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200635
636 if (!rtp_end->output_enabled) {
637 rtp_end->dropped_packets += 1;
638 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200639 "endpoint:%x output disabled, drop to %s %s "
640 "rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200641 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200642 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200643 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200644 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200645 );
646 } else if (is_rtp) {
647 int cont;
648 int nbytes = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200649 int buflen = len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200650 do {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200651 /* Run transcoder */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200652 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200653 buf, &buflen,
654 RTP_BUF_SIZE);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200655 if (cont < 0)
656 break;
657
Philipp Maier87bd9be2017-08-22 16:35:41 +0200658 if (addr)
659 mgcp_patch_and_count(endp, rtp_state, rtp_end,
660 addr, buf, buflen);
661 LOGP(DLMGCP, LOGL_DEBUG,
662 "endpoint:%x process/send to %s %s "
663 "rtp_port:%u rtcp_port:%u\n",
664 ENDPOINT_NUMBER(endp), dest_name,
665 inet_ntoa(rtp_end->addr), ntohs(rtp_end->rtp_port),
666 ntohs(rtp_end->rtcp_port)
667 );
668
669 /* Forward a copy of the RTP data to a debug ip/port */
670 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
671 buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200672
673 /* FIXME: HACK HACK HACK. See OS#2459.
674 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
675 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
676 * cells (as long as we patch only the first RTP payload in each stream).
677 */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200678 if (!rtp_state->patched_first_rtp_payload) {
679 uint8_t *data = (uint8_t *) & buf[12];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200680 data[0] = 0xe4;
681 data[1] = 0x00;
682 rtp_state->patched_first_rtp_payload = true;
683 }
684
Philipp Maier87bd9be2017-08-22 16:35:41 +0200685 len = mgcp_udp_send(rtp_end->rtp.fd,
686 &rtp_end->addr,
687 rtp_end->rtp_port, buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200688
Philipp Maier87bd9be2017-08-22 16:35:41 +0200689 if (len <= 0)
690 return len;
691
692 conn_dst->end.packets_tx += 1;
693 conn_dst->end.octets_tx += len;
694
695 nbytes += len;
696 buflen = cont;
697 } while (buflen > 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200698 return nbytes;
699 } else if (!tcfg->omit_rtcp) {
700 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200701 "endpoint:%x send to %s %s rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200702 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200703 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200704 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200705 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200706 );
707
Philipp Maier87bd9be2017-08-22 16:35:41 +0200708 len = mgcp_udp_send(rtp_end->rtcp.fd,
709 &rtp_end->addr,
710 rtp_end->rtcp_port, buf, len);
711
712 conn_dst->end.packets_tx += 1;
713 conn_dst->end.octets_tx += len;
714
715 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200716 }
717
718 return 0;
719}
720
Philipp Maier87bd9be2017-08-22 16:35:41 +0200721/* Helper function for mgcp_recv(),
722 Receive one RTP Packet + Originating address from file descriptor */
723static int receive_from(struct mgcp_endpoint *endp, int fd,
724 struct sockaddr_in *addr, char *buf, int bufsize)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200725{
726 int rc;
727 socklen_t slen = sizeof(*addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200728 struct sockaddr_in addr_sink;
729 char buf_sink[RTP_BUF_SIZE];
730 bool tossed = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200731
Philipp Maier87bd9be2017-08-22 16:35:41 +0200732 if (!addr)
733 addr = &addr_sink;
734 if (!buf) {
735 tossed = true;
736 buf = buf_sink;
737 bufsize = sizeof(buf_sink);
738 }
739
740 rc = recvfrom(fd, buf, bufsize, 0, (struct sockaddr *)addr, &slen);
741
742 LOGP(DLMGCP, LOGL_DEBUG,
743 "receiving %u bytes length packet from %s:%u ...\n",
744 rc, inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
745
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200746 if (rc < 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200747 LOGP(DLMGCP, LOGL_ERROR,
748 "endpoint:%x failed to receive packet, errno: %d/%s\n",
749 ENDPOINT_NUMBER(endp), errno, strerror(errno));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200750 return -1;
751 }
752
Philipp Maier87bd9be2017-08-22 16:35:41 +0200753 if (tossed) {
754 LOGP(DLMGCP, LOGL_ERROR, "endpoint:%x packet tossed\n",
755 ENDPOINT_NUMBER(endp));
756 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200757
758 return rc;
759}
760
Philipp Maier87bd9be2017-08-22 16:35:41 +0200761/* Check if the origin (addr) matches the address/port data of the RTP
762 * connections. */
763static int check_rtp_origin(struct mgcp_conn_rtp *conn,
764 struct sockaddr_in *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200765{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200766 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200767 endp = conn->conn->endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200768
Philipp Maier87bd9be2017-08-22 16:35:41 +0200769 /* Note: Check if the inbound RTP data comes from the same host to
770 * which we send our outgoing RTP traffic. */
771 if (memcmp(&addr->sin_addr, &conn->end.addr, sizeof(addr->sin_addr))
772 != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200773 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200774 "endpoint:%x data from wrong address: %s, ",
775 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr));
776 LOGPC(DLMGCP, LOGL_ERROR, "expected: %s\n",
777 inet_ntoa(conn->end.addr));
778 LOGP(DLMGCP, LOGL_ERROR, "endpoint:%x packet tossed\n",
779 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200780 return -1;
781 }
782
Philipp Maier87bd9be2017-08-22 16:35:41 +0200783 /* Note: Usually the remote remote port of the data we receive will be
784 * the same as the remote port where we transmit outgoing RTP traffic
785 * to (set by MDCX). We use this to check the origin of the data for
786 * plausibility. */
787 if (conn->end.rtp_port != addr->sin_port &&
788 conn->end.rtcp_port != addr->sin_port) {
789 LOGP(DLMGCP, LOGL_ERROR,
790 "endpoint:%x data from wrong source port: %d, ",
791 ENDPOINT_NUMBER(endp), ntohs(addr->sin_port));
792 LOGPC(DLMGCP, LOGL_ERROR,
793 "expected: %d for RTP or %d for RTCP\n",
794 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
795 LOGP(DLMGCP, LOGL_ERROR, "endpoint:%x packet tossed\n",
796 ENDPOINT_NUMBER(endp));
797 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200798 }
799
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200800 return 0;
801}
802
Philipp Maier87bd9be2017-08-22 16:35:41 +0200803/* Check the if the destination address configuration of an RTP connection
804 * makes sense */
805static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200806{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200807 struct mgcp_endpoint *endp;
808 endp = conn->conn->endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200809
Philipp Maier87bd9be2017-08-22 16:35:41 +0200810 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0) {
811 LOGP(DLMGCP, LOGL_ERROR,
812 "endpoint:%x destination IP-address is invalid\n",
813 ENDPOINT_NUMBER(endp));
814 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200815 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200816
817 if (conn->end.rtp_port == 0) {
818 LOGP(DLMGCP, LOGL_ERROR,
819 "endpoint:%x destination rtp port is invalid\n",
820 ENDPOINT_NUMBER(endp));
821 return -1;
822 }
823
824 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200825}
826
Philipp Maier87bd9be2017-08-22 16:35:41 +0200827/* Receive RTP data from a specified source connection and dispatch it to a
828 * destination connection. */
829static int mgcp_recv(int *proto, struct sockaddr_in *addr, char *buf,
830 unsigned int buf_size, struct osmo_fd *fd)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200831{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200832 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200833 struct mgcp_conn_rtp *conn;
834 struct mgcp_trunk_config *tcfg;
835 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200836
Philipp Maier87bd9be2017-08-22 16:35:41 +0200837 conn = (struct mgcp_conn_rtp*) fd->data;
838 endp = conn->conn->endp;
839 tcfg = endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200840
Philipp Maier87bd9be2017-08-22 16:35:41 +0200841 LOGP(DLMGCP, LOGL_DEBUG, "endpoint:%x receiving RTP/RTCP packet...\n",
842 ENDPOINT_NUMBER(endp));
843
844 rc = receive_from(endp, fd->fd, addr, buf, buf_size);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200845 if (rc <= 0)
846 return -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200847 *proto = fd == &conn->end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200848
Philipp Maier87bd9be2017-08-22 16:35:41 +0200849 LOGP(DLMGCP, LOGL_DEBUG, "endpoint:%x ", ENDPOINT_NUMBER(endp));
850 LOGPC(DLMGCP, LOGL_DEBUG, "receiveing from %s %s %d\n",
851 conn->conn->name, inet_ntoa(addr->sin_addr),
852 ntohs(addr->sin_port));
853 LOGP(DLMGCP, LOGL_DEBUG, "endpoint:%x conn:%s\n", ENDPOINT_NUMBER(endp),
854 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200855
Philipp Maier87bd9be2017-08-22 16:35:41 +0200856 /* Check if the origin of the RTP packet seems plausible */
857 if (tcfg->rtp_accept_all == 0) {
858 if (check_rtp_origin(conn, addr) != 0)
859 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200860 }
861
Philipp Maier87bd9be2017-08-22 16:35:41 +0200862 /* Filter out dummy message */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200863 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200864 LOGP(DLMGCP, LOGL_NOTICE,
865 "endpoint:%x dummy message received\n",
866 ENDPOINT_NUMBER(endp));
867 LOGP(DLMGCP, LOGL_ERROR,
868 "endpoint:%x packet tossed\n", ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200869 return 0;
870 }
871
Philipp Maier87bd9be2017-08-22 16:35:41 +0200872 /* Increment RX statistics */
873 conn->end.packets_rx += 1;
874 conn->end.octets_rx += rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200875
Philipp Maier87bd9be2017-08-22 16:35:41 +0200876 /* Forward a copy of the RTP data to a debug ip/port */
877 forward_data(fd->fd, &conn->tap_in, buf, rc);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200878
Philipp Maier87bd9be2017-08-22 16:35:41 +0200879 return rc;
880}
881
882/* Send RTP data. Possible options are standard RTP packet
883 * transmission or trsmission via an osmux connection */
884static int mgcp_send_rtp(int proto, struct sockaddr_in *addr, char *buf,
885 unsigned int buf_size,
886 struct mgcp_conn_rtp *conn_src,
887 struct mgcp_conn_rtp *conn_dst)
888{
889 struct mgcp_endpoint *endp;
890 endp = conn_src->conn->endp;
891
892 LOGP(DLMGCP, LOGL_DEBUG, "endpoint:%x destin conn:%s\n",
893 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_dst->conn));
894
895 /* Before we try to deliver the packet, we check if the destination
896 * port and IP-Address make sense at all. If not, we will be unable
897 * to deliver the packet. */
898 if (check_rtp_destin(conn_dst) != 0)
899 return -1;
900
901 /* Depending on the RTP connection type, deliver the RTP packet to the
902 * destination connection. */
903 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200904 case MGCP_RTP_DEFAULT:
905 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200906 "endpoint:%x endpoint type is MGCP_RTP_DEFAULT, "
907 "using mgcp_send() to forward data directly\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200908 ENDPOINT_NUMBER(endp));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200909 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
910 addr, buf, buf_size, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200911 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200912 case MGCP_OSMUX_BSC:
913 LOGP(DLMGCP, LOGL_DEBUG,
914 "endpoint:%x endpoint type is MGCP_OSMUX_BSC_NAT, "
915 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n",
916 ENDPOINT_NUMBER(endp));
917 return osmux_xfrm_to_osmux(buf, buf_size, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200918 }
919
Philipp Maier87bd9be2017-08-22 16:35:41 +0200920 /* If the data has not been handled/forwarded until here, it will
921 * be discarded, this should not happen, normally the MGCP type
922 * should be properly set */
923 LOGP(DLMGCP, LOGL_ERROR,
924 "endpoint:%x bad MGCP type -- data discarded!\n",
925 ENDPOINT_NUMBER(endp));
926
927 return -1;
928}
929
930/*! dispatch incoming RTP packet to opposite RTP connection.
931 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP)
932 * \param[in] addr socket address where the RTP packet has been received from
933 * \param[in] buf buffer that hold the RTP payload
934 * \param[in] buf_size size data length of buf
935 * \param[in] conn originating connection
936 * \returns 0 on success, -1 on ERROR */
937int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf,
938 unsigned int buf_size, struct mgcp_conn *conn)
939{
940 struct mgcp_conn *conn_dst;
941 struct mgcp_endpoint *endp;
942 endp = conn->endp;
943
944 /*! NOTE: This callback function implements the endpoint specific
945 * dispatch bahviour of an rtp bridge/proxy endpoint. It is assumed
946 * that the endpoint will hold only two connections. This premise
947 * is used to determine the opposite connection (it is always the
948 * connection that is not the originating connection). Once the
949 * destination connection is known the RTP packet is sent via
950 * the destination connection. */
951
952 /* Find a destination connection. */
953 /* NOTE: This code path runs every time an RTP packet is received. The
954 * function mgcp_find_dst_conn() we use to determine the detination
955 * connection will iterate the connection list inside the endpoint.
956 * Since list iterations are quite costly, we will figure out the
957 * destination only once and use the optional private data pointer of
958 * the connection to cache the destination connection pointer. */
959 if (!conn->priv) {
960 conn_dst = mgcp_find_dst_conn(conn);
961 conn->priv = conn_dst;
962 } else {
963 conn_dst = (struct mgcp_conn *)conn->priv;
964 }
965
966 /* There is no destination conn, stop here */
967 if (!conn_dst) {
968 LOGP(DLMGCP, LOGL_ERROR,
969 "endpoint:%x unable to find destination conn\n",
970 ENDPOINT_NUMBER(endp));
971 return -1;
972 }
973
974 /* The destination conn is not an RTP connection */
975 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
976 LOGP(DLMGCP, LOGL_ERROR,
977 "endpoint:%x unable to find suitable destination conn\n",
978 ENDPOINT_NUMBER(endp));
979 return -1;
980 }
981
982 /* Dispatch RTP packet to destination RTP connection */
983 return mgcp_send_rtp(proto, addr, buf,
984 buf_size, &conn->u.rtp, &conn_dst->u.rtp);
985
986}
987
988/* Handle incoming RTP data from NET */
989static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
990{
991 /* NOTE: This is a generic implementation. RTP data is received. In
992 * case of loopback the data is just sent back to its origin. All
993 * other cases implement endpoint specific behaviour (e.g. how is the
994 * destination connection determined?). That specific behaviour is
995 * implemented by the callback function that is called at the end of
996 * the function */
997
998 struct mgcp_conn_rtp *conn_src;
999 struct mgcp_endpoint *endp;
1000 struct sockaddr_in addr;
1001
1002 char buf[RTP_BUF_SIZE];
1003 int proto;
1004 int rc;
1005
1006 conn_src = (struct mgcp_conn_rtp *)fd->data;
1007 OSMO_ASSERT(conn_src);
1008 endp = conn_src->conn->endp;
1009 OSMO_ASSERT(endp);
1010
1011 LOGP(DLMGCP, LOGL_DEBUG, "endpoint:%x source conn:%s\n",
1012 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_src->conn));
1013
1014 /* Receive packet */
1015 rc = mgcp_recv(&proto, &addr, buf, sizeof(buf), fd);
1016 if (rc < 0)
1017 return -1;
1018
1019 /* Check if the connection is in loopback mode, if yes, just send the
1020 * incoming data back to the origin */
1021 if (conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
1022 return mgcp_send_rtp(proto, &addr, buf,
1023 sizeof(buf), conn_src, conn_src);
1024 }
1025
1026 /* Execute endpoint specific implementation that handles the
1027 * dispatching of the RTP data */
1028 return endp->type->dispatch_rtp_cb(proto, &addr, buf, sizeof(buf),
1029 conn_src->conn);
1030}
1031
1032/*! set IP Type of Service parameter.
1033 * \param[in] fd associated file descriptor
1034 * \param[in] tos dscp value
1035 * \returns 0 on success, -1 on ERROR */
1036int mgcp_set_ip_tos(int fd, int tos)
1037{
1038 int ret;
1039 ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1040
1041 if (ret < 0)
1042 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001043 return 0;
1044}
1045
Philipp Maier87bd9be2017-08-22 16:35:41 +02001046/*! bind RTP port to osmo_fd.
1047 * \param[in] source_addr source (local) address to bind on
1048 * \param[in] fd associated file descriptor
1049 * \param[in] port to bind on
1050 * \returns 0 on success, -1 on ERROR */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001051int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
1052{
1053 struct sockaddr_in addr;
1054 int on = 1;
1055
1056 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
1057 if (fd->fd < 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001058 LOGP(DLMGCP, LOGL_ERROR, "failed to create UDP port (%s:%i).\n",
1059 source_addr, port);
1060 return -1;
1061 } else {
1062 LOGP(DLMGCP, LOGL_DEBUG,
1063 "created UDP port (%s:%i).\n", source_addr, port);
1064 }
1065
1066 if (setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) {
1067 LOGP(DLMGCP, LOGL_ERROR,
1068 "failed to set socket options (%s:%i).\n", source_addr,
1069 port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001070 return -1;
1071 }
1072
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001073 memset(&addr, 0, sizeof(addr));
1074 addr.sin_family = AF_INET;
1075 addr.sin_port = htons(port);
1076 inet_aton(source_addr, &addr.sin_addr);
1077
Philipp Maier87bd9be2017-08-22 16:35:41 +02001078 if (bind(fd->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001079 close(fd->fd);
1080 fd->fd = -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001081 LOGP(DLMGCP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
1082 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001083 return -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001084 } else {
1085 LOGP(DLMGCP, LOGL_DEBUG,
1086 "bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001087 }
1088
1089 return 0;
1090}
1091
Philipp Maier87bd9be2017-08-22 16:35:41 +02001092/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001093static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001094 struct mgcp_rtp_end *rtp_end, int endpno)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001095{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001096 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1097 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1098
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001099 if (mgcp_create_bind(source_addr, &rtp_end->rtp,
1100 rtp_end->local_port) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001101 LOGP(DLMGCP, LOGL_ERROR,
1102 "endpoint:%x failed to create RTP port: %s:%d\n", endpno,
1103 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001104 goto cleanup0;
1105 }
1106
1107 if (mgcp_create_bind(source_addr, &rtp_end->rtcp,
1108 rtp_end->local_port + 1) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001109 LOGP(DLMGCP, LOGL_ERROR,
1110 "endpoint:%x failed to create RTCP port: %s:%d\n", endpno,
1111 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001112 goto cleanup1;
1113 }
1114
Philipp Maier87bd9be2017-08-22 16:35:41 +02001115 /* Set Type of Service (DSCP-Value) as configured via VTY */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001116 mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
1117 mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
1118
1119 rtp_end->rtp.when = BSC_FD_READ;
1120 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001121 LOGP(DLMGCP, LOGL_ERROR,
1122 "endpoint:%x failed to register RTP port %d\n", endpno,
1123 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001124 goto cleanup2;
1125 }
1126
1127 rtp_end->rtcp.when = BSC_FD_READ;
1128 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001129 LOGP(DLMGCP, LOGL_ERROR,
1130 "endpoint:%x failed to register RTCP port %d\n", endpno,
1131 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001132 goto cleanup3;
1133 }
1134
1135 return 0;
1136
1137cleanup3:
1138 osmo_fd_unregister(&rtp_end->rtp);
1139cleanup2:
1140 close(rtp_end->rtcp.fd);
1141 rtp_end->rtcp.fd = -1;
1142cleanup1:
1143 close(rtp_end->rtp.fd);
1144 rtp_end->rtp.fd = -1;
1145cleanup0:
1146 return -1;
1147}
1148
Philipp Maier87bd9be2017-08-22 16:35:41 +02001149/*! bind RTP port to endpoint/connection.
1150 * \param[in] endp endpoint that holds the RTP connection
1151 * \param[in] rtp_port port number to bind on
1152 * \param[in] conn associated RTP connection
1153 * \returns 0 on success, -1 on ERROR */
1154int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1155 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001156{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001157 char name[512];
1158 struct mgcp_rtp_end *end;
1159
1160 snprintf(name, sizeof(name), "%s-%u", conn->conn->name, conn->conn->id);
1161 end = &conn->end;
1162
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001163 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001164 LOGP(DLMGCP, LOGL_ERROR,
1165 "endpoint:%x %u was already bound on conn:%s\n",
1166 ENDPOINT_NUMBER(endp), rtp_port,
1167 mgcp_conn_dump(conn->conn));
1168
1169 /* Double bindings should never occour! Since we always allocate
1170 * connections dynamically and free them when they are not
1171 * needed anymore, there must be no previous binding leftover.
1172 * Should there be a connection bound twice, we have a serious
1173 * problem and must exit immediately! */
1174 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001175 }
1176
1177 end->local_port = rtp_port;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001178 end->rtp.cb = rtp_data_net;
1179 end->rtp.data = conn;
1180 end->rtcp.data = conn;
1181 end->rtcp.cb = rtp_data_net;
1182
1183 return bind_rtp(endp->cfg, mgcp_net_src_addr(endp), end,
1184 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001185}
1186
Philipp Maier87bd9be2017-08-22 16:35:41 +02001187/*! free allocated RTP and RTCP ports.
1188 * \param[in] end RTP end */
1189void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001190{
1191 if (end->rtp.fd != -1) {
1192 close(end->rtp.fd);
1193 end->rtp.fd = -1;
1194 osmo_fd_unregister(&end->rtp);
1195 }
1196
1197 if (end->rtcp.fd != -1) {
1198 close(end->rtcp.fd);
1199 end->rtcp.fd = -1;
1200 osmo_fd_unregister(&end->rtcp);
1201 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001202}