blob: c3f43dd39bfed942edff3e6804d3688942053eea [file] [log] [blame]
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +01001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* The protocol implementation */
3
4/*
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +02005 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2012 by On-Waves
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +01007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * 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
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010012 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * 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/>.
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010021 *
22 */
23
24#include <string.h>
Holger Hans Peter Freytherb715d7f2010-05-14 02:14:09 +080025#include <stdlib.h>
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010026#include <unistd.h>
Holger Hans Peter Freyther575b8952010-04-07 12:55:40 +020027#include <errno.h>
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010028
29#include <sys/socket.h>
30#include <arpa/inet.h>
31
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010032#include <osmocom/core/msgb.h>
33#include <osmocom/core/select.h>
Holger Hans Peter Freyther1ebad742010-02-26 20:16:37 +010034
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010035#include <openbsc/mgcp.h>
36#include <openbsc/mgcp_internal.h>
Holger Hans Peter Freyther983c9912014-06-22 22:30:28 +020037#include <openbsc/rtp.h>
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010038
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010039#include <openbsc/osmux.h>
40
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010041#warning "Make use of the rtp proxy code"
42
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010043
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +020044#define RTP_SEQ_MOD (1 << 16)
45#define RTP_MAX_DROPOUT 3000
46#define RTP_MAX_MISORDER 100
Jacob Erlbecka0d64ce2014-03-13 14:24:52 +010047#define RTP_BUF_SIZE 4096
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +020048
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010049enum {
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +020050 MGCP_PROTO_RTP,
51 MGCP_PROTO_RTCP,
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010052};
53
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +020054/**
55 * This does not need to be a precision timestamp and
56 * is allowed to wrap quite fast. The returned value is
Jacob Erlbeck303b54a2014-01-30 21:01:34 +010057 * 1/unit seconds.
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +020058 */
Jacob Erlbeck303b54a2014-01-30 21:01:34 +010059static uint32_t get_current_ts(unsigned unit)
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +020060{
61 struct timespec tp;
62 uint64_t ret;
63
Jacob Erlbeck303b54a2014-01-30 21:01:34 +010064 if (!unit)
65 return 0;
66
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +020067 memset(&tp, 0, sizeof(tp));
68 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
69 LOGP(DMGCP, LOGL_NOTICE,
70 "Getting the clock failed.\n");
71
Jacob Erlbeck303b54a2014-01-30 21:01:34 +010072 /* convert it to 1/unit seconds */
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +020073 ret = tp.tv_sec;
Jacob Erlbeck303b54a2014-01-30 21:01:34 +010074 ret *= unit;
75 ret += (int64_t)tp.tv_nsec * unit / 1000 / 1000 / 1000;
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +020076
77 return ret;
78}
79
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010080int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010081{
82 struct sockaddr_in out;
83 out.sin_family = AF_INET;
84 out.sin_port = port;
85 memcpy(&out.sin_addr, addr, sizeof(*addr));
86
87 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
88}
89
Holger Hans Peter Freytherb844b872010-04-21 21:25:13 +080090int mgcp_send_dummy(struct mgcp_endpoint *endp)
91{
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +020092 static char buf[] = { MGCP_DUMMY_LOAD };
Jacob Erlbeck34bdc9f2013-12-19 18:25:54 +010093 int rc;
Holger Hans Peter Freytherb844b872010-04-21 21:25:13 +080094
Jacob Erlbeck34bdc9f2013-12-19 18:25:54 +010095 rc = mgcp_udp_send(endp->net_end.rtp.fd, &endp->net_end.addr,
96 endp->net_end.rtp_port, buf, 1);
97
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +010098 if (rc == -1)
99 goto failed;
100
101 if (endp->tcfg->omit_rtcp)
Jacob Erlbeck34bdc9f2013-12-19 18:25:54 +0100102 return rc;
103
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100104 rc = mgcp_udp_send(endp->net_end.rtcp.fd, &endp->net_end.addr,
105 endp->net_end.rtcp_port, buf, 1);
106
107 if (rc >= 0)
108 return rc;
109
110failed:
111 LOGP(DMGCP, LOGL_ERROR,
112 "Failed to send dummy packet: %s on: 0x%x to %s\n",
113 strerror(errno), ENDPOINT_NUMBER(endp), inet_ntoa(endp->net_end.addr));
114
115 return -1;
Holger Hans Peter Freytherb844b872010-04-21 21:25:13 +0800116}
117
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100118static int32_t compute_timestamp_aligment_error(struct mgcp_rtp_stream_state *sstate,
119 int ptime, uint32_t timestamp)
120{
121 int32_t timestamp_delta;
122
123 if (ptime == 0)
124 return 0;
125
126 /* Align according to: T - Tlast = k * Tptime */
127 timestamp_delta = timestamp - sstate->last_timestamp;
128
129 return timestamp_delta % ptime;
130}
131
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100132static int check_rtp_timestamp(struct mgcp_endpoint *endp,
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100133 struct mgcp_rtp_state *state,
134 struct mgcp_rtp_stream_state *sstate,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100135 struct mgcp_rtp_end *rtp_end,
136 struct sockaddr_in *addr,
137 uint16_t seq, uint32_t timestamp,
138 const char *text, int32_t *tsdelta_out)
139{
140 int32_t tsdelta;
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100141 int32_t timestamp_error;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100142
143 /* Not fully intialized, skip */
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100144 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100145 return 0;
146
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100147 if (seq == sstate->last_seq) {
148 if (timestamp != sstate->last_timestamp) {
149 sstate->err_ts_counter += 1;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100150 LOGP(DMGCP, LOGL_ERROR,
151 "The %s timestamp delta is != 0 but the sequence "
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100152 "number %d is the same, "
153 "TS offset: %d, SeqNo offset: %d "
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100154 "on 0x%x SSRC: %u timestamp: %u "
155 "from %s:%d in %d\n",
156 text, seq,
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100157 state->timestamp_offset, state->seq_offset,
158 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100159 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
160 endp->conn_mode);
161 }
162 return 0;
163 }
164
165 tsdelta =
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100166 (int32_t)(timestamp - sstate->last_timestamp) /
167 (int16_t)(seq - sstate->last_seq);
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100168
169 if (tsdelta == 0) {
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100170 /* Don't update *tsdelta_out */
171 LOGP(DMGCP, LOGL_NOTICE,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100172 "The %s timestamp delta is %d "
173 "on 0x%x SSRC: %u timestamp: %u "
174 "from %s:%d in %d\n",
175 text, tsdelta,
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100176 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100177 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
178 endp->conn_mode);
179
180 return 0;
181 }
182
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100183 if (sstate->last_tsdelta != tsdelta) {
184 if (sstate->last_tsdelta) {
185 LOGP(DMGCP, LOGL_INFO,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100186 "The %s timestamp delta changes from %d to %d "
187 "on 0x%x SSRC: %u timestamp: %u from %s:%d in %d\n",
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100188 text, sstate->last_tsdelta, tsdelta,
189 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100190 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
191 endp->conn_mode);
192 }
193 }
194
195 if (tsdelta_out)
196 *tsdelta_out = tsdelta;
197
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100198 timestamp_error =
199 compute_timestamp_aligment_error(sstate, state->packet_duration,
200 timestamp);
201
202 if (timestamp_error) {
203 sstate->err_ts_counter += 1;
204 LOGP(DMGCP, LOGL_NOTICE,
205 "The %s timestamp has an alignment error of %d "
206 "on 0x%x SSRC: %u "
207 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
Holger Hans Peter Freythera7992e02014-06-27 19:51:52 +0200208 "from %s:%d in mode %d. ptime: %d\n",
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100209 text, timestamp_error,
210 ENDPOINT_NUMBER(endp), sstate->ssrc,
211 (int16_t)(seq - sstate->last_seq),
212 (int32_t)(timestamp - sstate->last_timestamp),
213 tsdelta,
214 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
Holger Hans Peter Freythera7992e02014-06-27 19:51:52 +0200215 endp->conn_mode, state->packet_duration);
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100216 }
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100217 return 1;
218}
219
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100220/* Set the timestamp offset according to the packet duration. */
221static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp,
222 struct mgcp_rtp_state *state,
223 struct mgcp_rtp_end *rtp_end,
224 struct sockaddr_in *addr,
Jacob Erlbeck785e3c92013-12-19 17:50:27 +0100225 int16_t delta_seq, uint32_t in_timestamp)
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100226{
227 int32_t tsdelta = state->packet_duration;
228 int timestamp_offset;
Jacob Erlbeck785e3c92013-12-19 17:50:27 +0100229 uint32_t out_timestamp;
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100230
231 if (tsdelta == 0) {
232 tsdelta = state->out_stream.last_tsdelta;
233 if (tsdelta != 0) {
234 LOGP(DMGCP, LOGL_NOTICE,
235 "A fixed packet duration is not available on 0x%x, "
236 "using last output timestamp delta instead: %d "
237 "from %s:%d in %d\n",
238 ENDPOINT_NUMBER(endp), tsdelta,
239 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
240 endp->conn_mode);
241 } else {
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200242 tsdelta = rtp_end->codec.rate * 20 / 1000;
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100243 LOGP(DMGCP, LOGL_NOTICE,
244 "Fixed packet duration and last timestamp delta "
245 "are not available on 0x%x, "
246 "using fixed 20ms instead: %d "
247 "from %s:%d in %d\n",
248 ENDPOINT_NUMBER(endp), tsdelta,
249 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
250 endp->conn_mode);
251 }
252 }
253
Jacob Erlbeck785e3c92013-12-19 17:50:27 +0100254 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
255 timestamp_offset = out_timestamp - in_timestamp;
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100256
257 if (state->timestamp_offset != timestamp_offset) {
258 state->timestamp_offset = timestamp_offset;
259
260 LOGP(DMGCP, LOGL_NOTICE,
261 "Timestamp offset change on 0x%x SSRC: %u "
262 "SeqNo delta: %d, TS offset: %d, "
263 "from %s:%d in %d\n",
264 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
265 delta_seq, state->timestamp_offset,
266 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
267 endp->conn_mode);
268 }
269
270 return timestamp_offset;
271}
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100272
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100273/* Set the timestamp offset according to the packet duration. */
274static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp,
275 struct mgcp_rtp_state *state,
276 struct mgcp_rtp_end *rtp_end,
277 struct sockaddr_in *addr,
278 uint32_t timestamp)
279{
280 int timestamp_error = 0;
281 int ptime = state->packet_duration;
282
283 /* Align according to: T + Toffs - Tlast = k * Tptime */
284
285 timestamp_error = compute_timestamp_aligment_error(
286 &state->out_stream, ptime,
287 timestamp + state->timestamp_offset);
288
289 if (timestamp_error) {
290 state->timestamp_offset += ptime - timestamp_error;
291
292 LOGP(DMGCP, LOGL_NOTICE,
293 "Corrected timestamp alignment error of %d on 0x%x SSRC: %u "
294 "new TS offset: %d, "
295 "from %s:%d in %d\n",
296 timestamp_error,
297 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
298 state->timestamp_offset, inet_ntoa(addr->sin_addr),
299 ntohs(addr->sin_port), endp->conn_mode);
300 }
301
302 OSMO_ASSERT(compute_timestamp_aligment_error(&state->out_stream, ptime,
303 timestamp + state->timestamp_offset) == 0);
304
305 return timestamp_error;
306}
307
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200308int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, struct mgcp_rtp_end *dst_end,
Jacob Erlbecka0d64ce2014-03-13 14:24:52 +0100309 char *data, int *len, int buf_size)
310{
311 return 0;
312}
313
314int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
315 struct mgcp_rtp_end *dst_end,
316 struct mgcp_rtp_end *src_end)
317{
318 return 0;
319}
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100320
Jacob Erlbeck168ca002014-03-17 12:40:07 +0100321void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
322 int *payload_type,
323 const char**audio_name,
324 const char**fmtp_extra)
325{
326 /* Use the BTS side parameters when passing the SDP data (for
327 * downlink) to the net peer.
328 */
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200329 *payload_type = endp->bts_end.codec.payload_type;
330 *audio_name = endp->bts_end.codec.audio_name;
Jacob Erlbeck168ca002014-03-17 12:40:07 +0100331 *fmtp_extra = endp->bts_end.fmtp_extra;
332}
333
Holger Hans Peter Freythera5a59c92014-10-06 20:04:42 +0200334
335void mgcp_rtp_annex_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
336 const uint16_t seq, const int32_t transit,
337 const uint32_t ssrc)
338{
Holger Hans Peter Freythera5a59c92014-10-06 20:04:42 +0200339 int32_t d;
340
341 /* initialize or re-initialize */
342 if (!state->stats_initialized || state->stats_ssrc != ssrc) {
343 state->stats_initialized = 1;
344 state->stats_base_seq = seq;
345 state->stats_max_seq = seq - 1;
346 state->stats_ssrc = ssrc;
347 state->stats_jitter = 0;
348 state->stats_transit = transit;
349 state->stats_cycles = 0;
Holger Hans Peter Freyther05d481a2014-10-06 21:01:26 +0200350 } else {
351 uint16_t udelta;
Holger Hans Peter Freythera5a59c92014-10-06 20:04:42 +0200352
Holger Hans Peter Freyther05d481a2014-10-06 21:01:26 +0200353 /*
354 * The below takes the shape of the validation of
355 * Appendix A. Check if there is something weird with
356 * the sequence number, otherwise check for a wrap
357 * around in the sequence number.
358 * It can't wrap during the initialization so let's
359 * skip it here. The Appendix A probably doesn't have
360 * this issue because of the probation.
361 */
362 udelta = seq - state->stats_max_seq;
363 if (udelta < RTP_MAX_DROPOUT) {
364 if (seq < state->stats_max_seq)
365 state->stats_cycles += RTP_SEQ_MOD;
366 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
367 LOGP(DMGCP, LOGL_NOTICE,
368 "RTP seqno made a very large jump on 0x%x delta: %u\n",
369 ENDPOINT_NUMBER(endp), udelta);
370 }
Holger Hans Peter Freythera5a59c92014-10-06 20:04:42 +0200371 }
372
373 /*
374 * Calculate the jitter between the two packages. The TS should be
375 * taken closer to the read function. This was taken from the
376 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
377 * resolution.
378 */
379 d = transit - state->stats_transit;
380 state->stats_transit = transit;
381 if (d < 0)
382 d = -d;
383 state->stats_jitter += d - ((state->stats_jitter + 8) >> 4);
384 state->stats_max_seq = seq;
385}
386
387
388
Holger Hans Peter Freyther1e85af62012-10-22 17:15:31 +0200389/**
390 * The RFC 3550 Appendix A assumes there are multiple sources but
391 * some of the supported endpoints (e.g. the nanoBTS) can only handle
Holger Hans Peter Freyther2a7ab862014-10-06 17:16:25 +0200392 * one source and this code will patch RTP header to appear as if there
Holger Hans Peter Freyther1e85af62012-10-22 17:15:31 +0200393 * is only one source.
Holger Hans Peter Freyther2a7ab862014-10-06 17:16:25 +0200394 * There is also no probation period for new sources. Every RTP header
Holger Hans Peter Freyther1e85af62012-10-22 17:15:31 +0200395 * we receive will be seen as a switch in streams.
396 */
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100397void mgcp_patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
398 struct mgcp_rtp_end *rtp_end, struct sockaddr_in *addr,
399 char *data, int len)
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100400{
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200401 uint32_t arrival_time;
Holger Hans Peter Freythera5a59c92014-10-06 20:04:42 +0200402 int32_t transit;
403 uint16_t seq;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100404 uint32_t timestamp, ssrc;
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100405 struct rtp_hdr *rtp_hdr;
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200406 int payload = rtp_end->codec.payload_type;
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100407
408 if (len < sizeof(*rtp_hdr))
409 return;
410
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800411 rtp_hdr = (struct rtp_hdr *) data;
412 seq = ntohs(rtp_hdr->sequence);
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000413 timestamp = ntohl(rtp_hdr->timestamp);
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200414 arrival_time = get_current_ts(rtp_end->codec.rate);
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100415 ssrc = ntohl(rtp_hdr->ssrc);
Jacob Erlbeckb281e4e2014-01-30 21:01:36 +0100416 transit = arrival_time - timestamp;
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800417
Holger Hans Peter Freyther7b76f822014-10-06 21:04:40 +0200418 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc);
419
Holger Hans Peter Freyther31868922010-08-03 11:59:04 +0000420 if (!state->initialized) {
Holger Hans Peter Freythera5a59c92014-10-06 20:04:42 +0200421 state->initialized = 1;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100422 state->in_stream.last_seq = seq - 1;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100423 state->in_stream.ssrc = state->orig_ssrc = ssrc;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100424 state->in_stream.last_tsdelta = 0;
Jacob Erlbeckf6ec0e92013-12-04 10:30:11 +0100425 state->packet_duration = mgcp_rtp_packet_duration(endp, rtp_end);
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100426 state->out_stream = state->in_stream;
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100427 state->out_stream.last_timestamp = timestamp;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100428 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100429 LOGP(DMGCP, LOGL_INFO,
430 "Initializing stream on 0x%x SSRC: %u timestamp: %u "
Jacob Erlbeck30ce4222013-12-05 12:02:15 +0100431 "pkt-duration: %d, from %s:%d in %d\n",
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100432 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Jacob Erlbeck30ce4222013-12-05 12:02:15 +0100433 state->seq_offset, state->packet_duration,
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100434 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
435 endp->conn_mode);
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100436 if (state->packet_duration == 0) {
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200437 state->packet_duration = rtp_end->codec.rate * 20 / 1000;
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100438 LOGP(DMGCP, LOGL_NOTICE,
439 "Fixed packet duration is not available on 0x%x, "
440 "using fixed 20ms instead: %d from %s:%d in %d\n",
441 ENDPOINT_NUMBER(endp), state->packet_duration,
442 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
443 endp->conn_mode);
444 }
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100445 } else if (state->in_stream.ssrc != ssrc) {
Jacob Erlbeck5e9549e2013-12-05 12:20:05 +0100446 LOGP(DMGCP, LOGL_NOTICE,
447 "The SSRC changed on 0x%x: %u -> %u "
448 "from %s:%d in %d\n",
449 ENDPOINT_NUMBER(endp),
450 state->in_stream.ssrc, rtp_hdr->ssrc,
451 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
452 endp->conn_mode);
453
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100454 state->in_stream.ssrc = ssrc;
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100455 if (rtp_end->force_constant_ssrc) {
Jacob Erlbeckeacc9b92014-01-30 21:01:35 +0100456 int16_t delta_seq;
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100457
Jacob Erlbeckeacc9b92014-01-30 21:01:35 +0100458 /* Always increment seqno by 1 */
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100459 state->seq_offset =
Jacob Erlbeckeacc9b92014-01-30 21:01:35 +0100460 (state->out_stream.last_seq + 1) - seq;
461
462 /* Estimate number of packets that would have been sent */
463 delta_seq =
464 (arrival_time - state->in_stream.last_arrival_time
465 + state->packet_duration/2) /
466 state->packet_duration;
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100467
468 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
469 delta_seq, timestamp);
470
Jacob Erlbeck58340e52013-11-29 11:20:06 +0100471 state->patch_ssrc = 1;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100472 ssrc = state->orig_ssrc;
Jacob Erlbecke2292f32013-12-03 15:13:12 +0100473 if (rtp_end->force_constant_ssrc != -1)
474 rtp_end->force_constant_ssrc -= 1;
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100475
476 LOGP(DMGCP, LOGL_NOTICE,
477 "SSRC patching enabled on 0x%x SSRC: %u "
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100478 "SeqNo offset: %d, TS offset: %d "
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100479 "from %s:%d in %d\n",
480 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100481 state->seq_offset, state->timestamp_offset,
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100482 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
483 endp->conn_mode);
484 }
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100485
486 state->in_stream.last_tsdelta = 0;
487 } else {
488 /* Compute current per-packet timestamp delta */
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100489 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end, addr,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100490 seq, timestamp, "input",
491 &state->in_stream.last_tsdelta);
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100492
Jacob Erlbeck58340e52013-11-29 11:20:06 +0100493 if (state->patch_ssrc)
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100494 ssrc = state->orig_ssrc;
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000495 }
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800496
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100497 /* Save before patching */
498 state->in_stream.last_timestamp = timestamp;
499 state->in_stream.last_seq = seq;
Jacob Erlbeckeacc9b92014-01-30 21:01:35 +0100500 state->in_stream.last_arrival_time = arrival_time;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100501
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100502 if (rtp_end->force_aligned_timing &&
503 state->out_stream.ssrc == ssrc && state->packet_duration)
504 /* Align the timestamp offset */
505 align_rtp_timestamp_offset(endp, state, rtp_end, addr, timestamp);
Jacob Erlbeck58340e52013-11-29 11:20:06 +0100506
507 /* Store the updated SSRC back to the packet */
508 if (state->patch_ssrc)
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100509 rtp_hdr->ssrc = htonl(ssrc);
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000510
Jacob Erlbeck58340e52013-11-29 11:20:06 +0100511 /* Apply the offset and store it back to the packet.
512 * This won't change anything if the offset is 0, so the conditional is
513 * omitted. */
514 seq += state->seq_offset;
515 rtp_hdr->sequence = htons(seq);
516 timestamp += state->timestamp_offset;
517 rtp_hdr->timestamp = htonl(timestamp);
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000518
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100519 /* Check again, whether the timestamps are still valid */
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100520 if (state->out_stream.ssrc == ssrc)
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100521 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
522 addr, seq, timestamp, "output",
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100523 &state->out_stream.last_tsdelta);
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100524
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100525 /* Save output values */
526 state->out_stream.last_seq = seq;
527 state->out_stream.last_timestamp = timestamp;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100528 state->out_stream.ssrc = ssrc;
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000529
Holger Hans Peter Freyther7279d242010-04-06 11:15:50 +0200530 if (payload < 0)
531 return;
532
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100533 rtp_hdr->payload_type = payload;
534}
535
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100536/*
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800537 * The below code is for dispatching. We have a dedicated port for
538 * the data coming from the net and one to discover the BTS.
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100539 */
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800540static int forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf, int len)
541{
542 if (!tap->enabled)
543 return 0;
544
545 return sendto(fd, buf, len, 0,
546 (struct sockaddr *)&tap->forward, sizeof(tap->forward));
547}
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800548
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200549static int mgcp_send_transcoder(struct mgcp_rtp_end *end,
550 struct mgcp_config *cfg, int is_rtp,
551 const char *buf, int len)
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800552{
553 int rc;
554 int port;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800555 struct sockaddr_in addr;
556
Holger Hans Peter Freyther8b19dee2010-11-01 22:38:25 +0100557 port = is_rtp ? end->rtp_port : end->rtcp_port;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800558
559 addr.sin_family = AF_INET;
560 addr.sin_addr = cfg->transcoder_in;
Holger Hans Peter Freyther5f2cd842010-11-01 21:53:39 +0100561 addr.sin_port = port;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800562
563 rc = sendto(is_rtp ?
Holger Hans Peter Freyther8b19dee2010-11-01 22:38:25 +0100564 end->rtp.fd :
565 end->rtcp.fd, buf, len, 0,
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800566 (struct sockaddr *) &addr, sizeof(addr));
567
568 if (rc != len)
569 LOGP(DMGCP, LOGL_ERROR,
570 "Failed to send data to the transcoder: %s\n",
571 strerror(errno));
572
573 return rc;
574}
575
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100576int mgcp_send(struct mgcp_endpoint *endp, int dest, int is_rtp,
577 struct sockaddr_in *addr, char *buf, int rc)
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800578{
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100579 struct mgcp_trunk_config *tcfg = endp->tcfg;
Jacob Erlbeckb8300082013-12-19 10:00:34 +0100580 struct mgcp_rtp_end *rtp_end;
581 struct mgcp_rtp_state *rtp_state;
582 int tap_idx;
583
Holger Hans Peter Freyther1fc43292010-08-05 06:31:58 +0800584 /* For loop toggle the destination and then dispatch. */
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100585 if (tcfg->audio_loop)
Holger Hans Peter Freyther1fc43292010-08-05 06:31:58 +0800586 dest = !dest;
587
588 /* Loop based on the conn_mode, maybe undoing the above */
589 if (endp->conn_mode == MGCP_CONN_LOOPBACK)
590 dest = !dest;
591
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200592 if (dest == MGCP_DEST_NET) {
Jacob Erlbeckb8300082013-12-19 10:00:34 +0100593 rtp_end = &endp->net_end;
594 rtp_state = &endp->bts_state;
595 tap_idx = MGCP_TAP_NET_OUT;
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800596 } else {
Jacob Erlbeckb8300082013-12-19 10:00:34 +0100597 rtp_end = &endp->bts_end;
598 rtp_state = &endp->net_state;
599 tap_idx = MGCP_TAP_BTS_OUT;
600 }
601
Jacob Erlbeck0970bab2013-12-19 12:13:32 +0100602 if (!rtp_end->output_enabled)
603 rtp_end->dropped_packets += 1;
604 else if (is_rtp) {
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200605 int cont;
606 int nbytes = 0;
607 int len = rc;
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200608 do {
609 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
610 buf, &len, RTP_BUF_SIZE);
611 if (cont < 0)
612 break;
613
Holger Hans Peter Freyther1fc1ed22014-07-02 21:54:14 +0200614 mgcp_patch_and_count(endp, rtp_state, rtp_end, addr, buf, len);
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200615 forward_data(rtp_end->rtp.fd, &endp->taps[tap_idx],
616 buf, len);
617 rc = mgcp_udp_send(rtp_end->rtp.fd,
618 &rtp_end->addr,
619 rtp_end->rtp_port, buf, len);
620
621 if (rc <= 0)
622 return rc;
623 nbytes += rc;
624 len = cont;
625 } while (len > 0);
626 return nbytes;
Jacob Erlbeckb8300082013-12-19 10:00:34 +0100627 } else if (!tcfg->omit_rtcp) {
628 return mgcp_udp_send(rtp_end->rtcp.fd,
629 &rtp_end->addr,
630 rtp_end->rtcp_port, buf, rc);
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800631 }
Holger Hans Peter Freyther8c3d0692012-09-11 12:31:25 +0200632
633 return 0;
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800634}
635
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200636static int receive_from(struct mgcp_endpoint *endp, int fd, struct sockaddr_in *addr,
637 char *buf, int bufsize)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100638{
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800639 int rc;
640 socklen_t slen = sizeof(*addr);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100641
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800642 rc = recvfrom(fd, buf, bufsize, 0,
643 (struct sockaddr *) addr, &slen);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100644 if (rc < 0) {
Holger Hans Peter Freyther575b8952010-04-07 12:55:40 +0200645 LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n",
646 ENDPOINT_NUMBER(endp), errno, strerror(errno));
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100647 return -1;
648 }
649
650 /* do not forward aynthing... maybe there is a packet from the bts */
Holger Hans Peter Freyther39a97e22010-08-06 18:03:11 +0800651 if (!endp->allocated)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100652 return -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100653
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800654 #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
655
656 return rc;
657}
658
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200659static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800660{
Jacob Erlbecka0d64ce2014-03-13 14:24:52 +0100661 char buf[RTP_BUF_SIZE];
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800662 struct sockaddr_in addr;
663 struct mgcp_endpoint *endp;
664 int rc, proto;
665
666 endp = (struct mgcp_endpoint *) fd->data;
667
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200668 rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf));
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800669 if (rc <= 0)
670 return -1;
671
672 if (memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) != 0) {
673 LOGP(DMGCP, LOGL_ERROR,
Holger Hans Peter Freythereedb4532012-11-12 10:55:29 +0100674 "Endpoint 0x%x data from wrong address %s vs. ",
675 ENDPOINT_NUMBER(endp), inet_ntoa(addr.sin_addr));
676 LOGPC(DMGCP, LOGL_ERROR,
677 "%s\n", inet_ntoa(endp->net_end.addr));
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800678 return -1;
679 }
680
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100681 switch(endp->type) {
682 case MGCP_RTP_DEFAULT:
683 case MGCP_RTP_TRANSCODED:
684 if (endp->net_end.rtp_port != addr.sin_port &&
685 endp->net_end.rtcp_port != addr.sin_port) {
686 LOGP(DMGCP, LOGL_ERROR,
687 "Data from wrong source port %d on 0x%x\n",
688 ntohs(addr.sin_port), ENDPOINT_NUMBER(endp));
689 return -1;
690 }
691 break;
692 case MGCP_OSMUX_BSC:
693 case MGCP_OSMUX_BSC_NAT:
694 break;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800695 }
696
697 /* throw away the dummy message */
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200698 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800699 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from network on 0x%x\n",
700 ENDPOINT_NUMBER(endp));
701 return 0;
702 }
703
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200704 proto = fd == &endp->net_end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800705 endp->net_end.packets += 1;
Holger Hans Peter Freyther952f7522012-09-12 11:38:37 +0200706 endp->net_end.octets += rc;
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800707
708 forward_data(fd->fd, &endp->taps[MGCP_TAP_NET_IN], buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200709
710 switch (endp->type) {
711 case MGCP_RTP_DEFAULT:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200712 return mgcp_send(endp, MGCP_DEST_BTS, proto == MGCP_PROTO_RTP,
713 &addr, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200714 case MGCP_RTP_TRANSCODED:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200715 return mgcp_send_transcoder(&endp->trans_net, endp->cfg,
716 proto == MGCP_PROTO_RTP, buf, rc);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100717 case MGCP_OSMUX_BSC_NAT:
718 return osmux_xfrm_to_osmux(MGCP_DEST_BTS, buf, rc, endp);
719 case MGCP_OSMUX_BSC: /* Should not happen */
720 break;
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200721 }
722
723 LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n",
724 endp->type, ENDPOINT_NUMBER(endp));
725 return 0;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800726}
727
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800728static void discover_bts(struct mgcp_endpoint *endp, int proto, struct sockaddr_in *addr)
729{
730 struct mgcp_config *cfg = endp->cfg;
731
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200732 if (proto == MGCP_PROTO_RTP && endp->bts_end.rtp_port == 0) {
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800733 if (!cfg->bts_ip ||
734 memcmp(&addr->sin_addr,
735 &cfg->bts_in, sizeof(cfg->bts_in)) == 0 ||
736 memcmp(&addr->sin_addr,
737 &endp->bts_end.addr, sizeof(endp->bts_end.addr)) == 0) {
738
739 endp->bts_end.rtp_port = addr->sin_port;
740 endp->bts_end.addr = addr->sin_addr;
741
742 LOGP(DMGCP, LOGL_NOTICE,
743 "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n",
744 ENDPOINT_NUMBER(endp), ntohs(endp->bts_end.rtp_port),
745 ntohs(endp->bts_end.rtcp_port), inet_ntoa(addr->sin_addr));
746 }
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200747 } else if (proto == MGCP_PROTO_RTCP && endp->bts_end.rtcp_port == 0) {
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800748 if (memcmp(&endp->bts_end.addr, &addr->sin_addr,
749 sizeof(endp->bts_end.addr)) == 0) {
750 endp->bts_end.rtcp_port = addr->sin_port;
751 }
752 }
753}
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800754
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200755static int rtp_data_bts(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800756{
Jacob Erlbecka0d64ce2014-03-13 14:24:52 +0100757 char buf[RTP_BUF_SIZE];
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800758 struct sockaddr_in addr;
759 struct mgcp_endpoint *endp;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800760 int rc, proto;
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800761
762 endp = (struct mgcp_endpoint *) fd->data;
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800763
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200764 rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf));
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800765 if (rc <= 0)
766 return -1;
767
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200768 proto = fd == &endp->bts_end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100769
770 /* We have no idea who called us, maybe it is the BTS. */
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800771 /* it was the BTS... */
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800772 discover_bts(endp, proto, &addr);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100773
Holger Hans Peter Freytherea97fbf2010-08-05 10:53:26 +0000774 if (memcmp(&endp->bts_end.addr, &addr.sin_addr, sizeof(addr.sin_addr)) != 0) {
775 LOGP(DMGCP, LOGL_ERROR,
776 "Data from wrong bts %s on 0x%x\n",
777 inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp));
778 return -1;
779 }
780
781 if (endp->bts_end.rtp_port != addr.sin_port &&
782 endp->bts_end.rtcp_port != addr.sin_port) {
783 LOGP(DMGCP, LOGL_ERROR,
784 "Data from wrong bts source port %d on 0x%x\n",
785 ntohs(addr.sin_port), ENDPOINT_NUMBER(endp));
786 return -1;
787 }
788
Holger Hans Peter Freytherb3c206a2010-05-14 02:18:59 +0800789 /* throw away the dummy message */
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200790 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800791 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from bts on 0x%x\n",
Holger Hans Peter Freytheraa9d3ce2010-04-22 12:14:51 +0800792 ENDPOINT_NUMBER(endp));
793 return 0;
794 }
795
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +0200796 /* do this before the loop handling */
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800797 endp->bts_end.packets += 1;
Holger Hans Peter Freyther952f7522012-09-12 11:38:37 +0200798 endp->bts_end.octets += rc;
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +0200799
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800800 forward_data(fd->fd, &endp->taps[MGCP_TAP_BTS_IN], buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200801
802 switch (endp->type) {
803 case MGCP_RTP_DEFAULT:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200804 return mgcp_send(endp, MGCP_DEST_NET, proto == MGCP_PROTO_RTP,
805 &addr, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200806 case MGCP_RTP_TRANSCODED:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200807 return mgcp_send_transcoder(&endp->trans_bts, endp->cfg,
808 proto == MGCP_PROTO_RTP, buf, rc);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100809 case MGCP_OSMUX_BSC:
810 /* OSMUX translation: BTS -> BSC */
811 return osmux_xfrm_to_osmux(MGCP_DEST_NET, buf, rc, endp);
812 case MGCP_OSMUX_BSC_NAT:
813 break; /* Should not happen */
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200814 }
815
816 LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n",
817 endp->type, ENDPOINT_NUMBER(endp));
818 return 0;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800819}
820
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100821static int rtp_data_transcoder(struct mgcp_rtp_end *end, struct mgcp_endpoint *_endp,
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200822 int dest, struct osmo_fd *fd)
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800823{
Jacob Erlbecka0d64ce2014-03-13 14:24:52 +0100824 char buf[RTP_BUF_SIZE];
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800825 struct sockaddr_in addr;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800826 struct mgcp_config *cfg;
827 int rc, proto;
828
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100829 cfg = _endp->cfg;
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200830 rc = receive_from(_endp, fd->fd, &addr, buf, sizeof(buf));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800831 if (rc <= 0)
832 return -1;
833
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200834 proto = fd == &end->rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800835
836 if (memcmp(&addr.sin_addr, &cfg->transcoder_in, sizeof(addr.sin_addr)) != 0) {
837 LOGP(DMGCP, LOGL_ERROR,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100838 "Data not coming from transcoder dest: %d %s on 0x%x\n",
839 dest, inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(_endp));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800840 return -1;
841 }
842
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100843 if (end->rtp_port != addr.sin_port &&
844 end->rtcp_port != addr.sin_port) {
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800845 LOGP(DMGCP, LOGL_ERROR,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100846 "Data from wrong transcoder dest %d source port %d on 0x%x\n",
847 dest, ntohs(addr.sin_port), ENDPOINT_NUMBER(_endp));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800848 return -1;
849 }
850
851 /* throw away the dummy message */
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200852 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100853 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from transcoder dest %d on 0x%x\n",
854 dest, ENDPOINT_NUMBER(_endp));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800855 return 0;
856 }
857
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100858 end->packets += 1;
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200859 return mgcp_send(_endp, dest, proto == MGCP_PROTO_RTP, &addr, buf, rc);
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100860}
861
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200862static int rtp_data_trans_net(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100863{
864 struct mgcp_endpoint *endp;
865 endp = (struct mgcp_endpoint *) fd->data;
866
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200867 return rtp_data_transcoder(&endp->trans_net, endp, MGCP_DEST_NET, fd);
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100868}
869
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200870static int rtp_data_trans_bts(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100871{
872 struct mgcp_endpoint *endp;
873 endp = (struct mgcp_endpoint *) fd->data;
874
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200875 return rtp_data_transcoder(&endp->trans_bts, endp, MGCP_DEST_BTS, fd);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100876}
877
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100878int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100879{
880 struct sockaddr_in addr;
881 int on = 1;
882
883 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
884 if (fd->fd < 0) {
885 LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n");
886 return -1;
887 }
888
889 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
890 memset(&addr, 0, sizeof(addr));
891 addr.sin_family = AF_INET;
892 addr.sin_port = htons(port);
893 inet_aton(source_addr, &addr.sin_addr);
894
895 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Holger Hans Peter Freyther6f36e922010-08-06 03:00:17 +0800896 close(fd->fd);
897 fd->fd = -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100898 return -1;
899 }
900
901 return 0;
902}
903
Holger Hans Peter Freyther9be675e2015-01-21 11:39:47 +0100904int mgcp_set_ip_tos(int fd, int tos)
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800905{
906 int ret;
907 ret = setsockopt(fd, IPPROTO_IP, IP_TOS,
908 &tos, sizeof(tos));
909 return ret != 0;
910}
911
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800912static int bind_rtp(struct mgcp_config *cfg, struct mgcp_rtp_end *rtp_end, int endpno)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100913{
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200914 if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtp,
915 rtp_end->local_port) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100916 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800917 cfg->source_addr, rtp_end->local_port, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100918 goto cleanup0;
919 }
920
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200921 if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtcp,
922 rtp_end->local_port + 1) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100923 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800924 cfg->source_addr, rtp_end->local_port + 1, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100925 goto cleanup1;
926 }
927
Holger Hans Peter Freyther9be675e2015-01-21 11:39:47 +0100928 mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
929 mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800930
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800931 rtp_end->rtp.when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200932 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100933 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800934 rtp_end->local_port, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100935 goto cleanup2;
936 }
937
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800938 rtp_end->rtcp.when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200939 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100940 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800941 rtp_end->local_port + 1, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100942 goto cleanup3;
943 }
944
945 return 0;
946
947cleanup3:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200948 osmo_fd_unregister(&rtp_end->rtp);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100949cleanup2:
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800950 close(rtp_end->rtcp.fd);
951 rtp_end->rtcp.fd = -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100952cleanup1:
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800953 close(rtp_end->rtp.fd);
954 rtp_end->rtp.fd = -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100955cleanup0:
956 return -1;
957}
958
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100959static int int_bind(const char *port,
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200960 struct mgcp_rtp_end *end, int (*cb)(struct osmo_fd *, unsigned),
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100961 struct mgcp_endpoint *_endp, int rtp_port)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100962{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100963 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
964 LOGP(DMGCP, LOGL_ERROR, "Previous %s was still bound on %d\n",
965 port, ENDPOINT_NUMBER(_endp));
966 mgcp_free_rtp_port(end);
Holger Hans Peter Freyther7fe2a3d2010-08-06 07:18:22 +0800967 }
968
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100969 end->local_port = rtp_port;
970 end->rtp.cb = cb;
971 end->rtp.data = _endp;
972 end->rtcp.data = _endp;
973 end->rtcp.cb = cb;
974 return bind_rtp(_endp->cfg, end, ENDPOINT_NUMBER(_endp));
975}
976
977
978int mgcp_bind_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
979{
980 return int_bind("bts-port", &endp->bts_end,
981 rtp_data_bts, endp, rtp_port);
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800982}
983
984int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
985{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100986 return int_bind("net-port", &endp->net_end,
987 rtp_data_net, endp, rtp_port);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100988}
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800989
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100990int mgcp_bind_trans_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800991{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100992 return int_bind("trans-net", &endp->trans_net,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100993 rtp_data_trans_net, endp, rtp_port);
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800994}
995
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100996int mgcp_bind_trans_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
997{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100998 return int_bind("trans-bts", &endp->trans_bts,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100999 rtp_data_trans_bts, endp, rtp_port);
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +01001000}
1001
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +08001002int mgcp_free_rtp_port(struct mgcp_rtp_end *end)
1003{
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +08001004 if (end->rtp.fd != -1) {
1005 close(end->rtp.fd);
1006 end->rtp.fd = -1;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001007 osmo_fd_unregister(&end->rtp);
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +08001008 }
1009
1010 if (end->rtcp.fd != -1) {
1011 close(end->rtcp.fd);
1012 end->rtcp.fd = -1;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001013 osmo_fd_unregister(&end->rtcp);
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +08001014 }
1015
1016 return 0;
1017}
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +02001018
1019
1020void mgcp_state_calc_loss(struct mgcp_rtp_state *state,
1021 struct mgcp_rtp_end *end, uint32_t *expected,
1022 int *loss)
1023{
Holger Hans Peter Freythera5a59c92014-10-06 20:04:42 +02001024 *expected = state->stats_cycles + state->stats_max_seq;
1025 *expected = *expected - state->stats_base_seq + 1;
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +02001026
Holger Hans Peter Freythera5a59c92014-10-06 20:04:42 +02001027 if (!state->stats_initialized) {
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +02001028 *expected = 0;
1029 *loss = 0;
1030 return;
1031 }
1032
1033 /*
1034 * Make sure the sign is correct and use the biggest
1035 * positive/negative number that fits.
1036 */
1037 *loss = *expected - end->packets;
1038 if (*expected < end->packets) {
1039 if (*loss > 0)
1040 *loss = INT_MIN;
1041 } else {
1042 if (*loss < 0)
1043 *loss = INT_MAX;
1044 }
1045}
Holger Hans Peter Freythercb306a62012-10-24 21:22:47 +02001046
1047uint32_t mgcp_state_calc_jitter(struct mgcp_rtp_state *state)
1048{
Holger Hans Peter Freythera5a59c92014-10-06 20:04:42 +02001049 if (!state->stats_initialized)
Holger Hans Peter Freythercb306a62012-10-24 21:22:47 +02001050 return 0;
Holger Hans Peter Freythera5a59c92014-10-06 20:04:42 +02001051 return state->stats_jitter >> 4;
Holger Hans Peter Freythercb306a62012-10-24 21:22:47 +02001052}