blob: 888ec6eb7fc84ff4d98128b94f14433de829839a [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{
339 uint16_t udelta;
340 int32_t d;
341
342 /* initialize or re-initialize */
343 if (!state->stats_initialized || state->stats_ssrc != ssrc) {
344 state->stats_initialized = 1;
345 state->stats_base_seq = seq;
346 state->stats_max_seq = seq - 1;
347 state->stats_ssrc = ssrc;
348 state->stats_jitter = 0;
349 state->stats_transit = transit;
350 state->stats_cycles = 0;
351 }
352
353 /*
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 }
371
372 /*
373 * Calculate the jitter between the two packages. The TS should be
374 * taken closer to the read function. This was taken from the
375 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
376 * resolution.
377 */
378 d = transit - state->stats_transit;
379 state->stats_transit = transit;
380 if (d < 0)
381 d = -d;
382 state->stats_jitter += d - ((state->stats_jitter + 8) >> 4);
383 state->stats_max_seq = seq;
384}
385
386
387
Holger Hans Peter Freyther1e85af62012-10-22 17:15:31 +0200388/**
389 * The RFC 3550 Appendix A assumes there are multiple sources but
390 * some of the supported endpoints (e.g. the nanoBTS) can only handle
Holger Hans Peter Freyther2a7ab862014-10-06 17:16:25 +0200391 * one source and this code will patch RTP header to appear as if there
Holger Hans Peter Freyther1e85af62012-10-22 17:15:31 +0200392 * is only one source.
Holger Hans Peter Freyther2a7ab862014-10-06 17:16:25 +0200393 * There is also no probation period for new sources. Every RTP header
Holger Hans Peter Freyther1e85af62012-10-22 17:15:31 +0200394 * we receive will be seen as a switch in streams.
395 */
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100396void mgcp_patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
397 struct mgcp_rtp_end *rtp_end, struct sockaddr_in *addr,
398 char *data, int len)
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100399{
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200400 uint32_t arrival_time;
Holger Hans Peter Freythera5a59c92014-10-06 20:04:42 +0200401 int32_t transit;
402 uint16_t seq;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100403 uint32_t timestamp, ssrc;
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100404 struct rtp_hdr *rtp_hdr;
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200405 int payload = rtp_end->codec.payload_type;
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100406
407 if (len < sizeof(*rtp_hdr))
408 return;
409
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800410 rtp_hdr = (struct rtp_hdr *) data;
411 seq = ntohs(rtp_hdr->sequence);
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000412 timestamp = ntohl(rtp_hdr->timestamp);
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200413 arrival_time = get_current_ts(rtp_end->codec.rate);
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100414 ssrc = ntohl(rtp_hdr->ssrc);
Jacob Erlbeckb281e4e2014-01-30 21:01:36 +0100415 transit = arrival_time - timestamp;
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800416
Holger Hans Peter Freyther31868922010-08-03 11:59:04 +0000417 if (!state->initialized) {
Holger Hans Peter Freythera5a59c92014-10-06 20:04:42 +0200418 state->initialized = 1;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100419 state->in_stream.last_seq = seq - 1;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100420 state->in_stream.ssrc = state->orig_ssrc = ssrc;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100421 state->in_stream.last_tsdelta = 0;
Jacob Erlbeckf6ec0e92013-12-04 10:30:11 +0100422 state->packet_duration = mgcp_rtp_packet_duration(endp, rtp_end);
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100423 state->out_stream = state->in_stream;
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100424 state->out_stream.last_timestamp = timestamp;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100425 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100426 LOGP(DMGCP, LOGL_INFO,
427 "Initializing stream on 0x%x SSRC: %u timestamp: %u "
Jacob Erlbeck30ce4222013-12-05 12:02:15 +0100428 "pkt-duration: %d, from %s:%d in %d\n",
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100429 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Jacob Erlbeck30ce4222013-12-05 12:02:15 +0100430 state->seq_offset, state->packet_duration,
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100431 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
432 endp->conn_mode);
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100433 if (state->packet_duration == 0) {
Holger Hans Peter Freythercac24382014-09-01 10:35:55 +0200434 state->packet_duration = rtp_end->codec.rate * 20 / 1000;
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100435 LOGP(DMGCP, LOGL_NOTICE,
436 "Fixed packet duration is not available on 0x%x, "
437 "using fixed 20ms instead: %d from %s:%d in %d\n",
438 ENDPOINT_NUMBER(endp), state->packet_duration,
439 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
440 endp->conn_mode);
441 }
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100442 } else if (state->in_stream.ssrc != ssrc) {
Jacob Erlbeck5e9549e2013-12-05 12:20:05 +0100443 LOGP(DMGCP, LOGL_NOTICE,
444 "The SSRC changed on 0x%x: %u -> %u "
445 "from %s:%d in %d\n",
446 ENDPOINT_NUMBER(endp),
447 state->in_stream.ssrc, rtp_hdr->ssrc,
448 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
449 endp->conn_mode);
450
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100451 state->in_stream.ssrc = ssrc;
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100452 if (rtp_end->force_constant_ssrc) {
Jacob Erlbeckeacc9b92014-01-30 21:01:35 +0100453 int16_t delta_seq;
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100454
Jacob Erlbeckeacc9b92014-01-30 21:01:35 +0100455 /* Always increment seqno by 1 */
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100456 state->seq_offset =
Jacob Erlbeckeacc9b92014-01-30 21:01:35 +0100457 (state->out_stream.last_seq + 1) - seq;
458
459 /* Estimate number of packets that would have been sent */
460 delta_seq =
461 (arrival_time - state->in_stream.last_arrival_time
462 + state->packet_duration/2) /
463 state->packet_duration;
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100464
465 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
466 delta_seq, timestamp);
467
Jacob Erlbeck58340e52013-11-29 11:20:06 +0100468 state->patch_ssrc = 1;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100469 ssrc = state->orig_ssrc;
Jacob Erlbecke2292f32013-12-03 15:13:12 +0100470 if (rtp_end->force_constant_ssrc != -1)
471 rtp_end->force_constant_ssrc -= 1;
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100472
473 LOGP(DMGCP, LOGL_NOTICE,
474 "SSRC patching enabled on 0x%x SSRC: %u "
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100475 "SeqNo offset: %d, TS offset: %d "
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100476 "from %s:%d in %d\n",
477 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100478 state->seq_offset, state->timestamp_offset,
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100479 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
480 endp->conn_mode);
481 }
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100482
483 state->in_stream.last_tsdelta = 0;
484 } else {
485 /* Compute current per-packet timestamp delta */
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100486 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end, addr,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100487 seq, timestamp, "input",
488 &state->in_stream.last_tsdelta);
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100489
Jacob Erlbeck58340e52013-11-29 11:20:06 +0100490 if (state->patch_ssrc)
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100491 ssrc = state->orig_ssrc;
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000492 }
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800493
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100494 /* Save before patching */
495 state->in_stream.last_timestamp = timestamp;
496 state->in_stream.last_seq = seq;
Jacob Erlbeckeacc9b92014-01-30 21:01:35 +0100497 state->in_stream.last_arrival_time = arrival_time;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100498
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100499 if (rtp_end->force_aligned_timing &&
500 state->out_stream.ssrc == ssrc && state->packet_duration)
501 /* Align the timestamp offset */
502 align_rtp_timestamp_offset(endp, state, rtp_end, addr, timestamp);
Jacob Erlbeck58340e52013-11-29 11:20:06 +0100503
504 /* Store the updated SSRC back to the packet */
505 if (state->patch_ssrc)
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100506 rtp_hdr->ssrc = htonl(ssrc);
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000507
Jacob Erlbeck58340e52013-11-29 11:20:06 +0100508 /* Apply the offset and store it back to the packet.
509 * This won't change anything if the offset is 0, so the conditional is
510 * omitted. */
511 seq += state->seq_offset;
512 rtp_hdr->sequence = htons(seq);
513 timestamp += state->timestamp_offset;
514 rtp_hdr->timestamp = htonl(timestamp);
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000515
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100516 /* Check again, whether the timestamps are still valid */
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100517 if (state->out_stream.ssrc == ssrc)
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100518 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
519 addr, seq, timestamp, "output",
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100520 &state->out_stream.last_tsdelta);
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100521
Holger Hans Peter Freythera5a59c92014-10-06 20:04:42 +0200522 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc);
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200523
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100524 /* Save output values */
525 state->out_stream.last_seq = seq;
526 state->out_stream.last_timestamp = timestamp;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100527 state->out_stream.ssrc = ssrc;
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000528
Holger Hans Peter Freyther7279d242010-04-06 11:15:50 +0200529 if (payload < 0)
530 return;
531
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100532 rtp_hdr->payload_type = payload;
533}
534
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100535/*
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800536 * The below code is for dispatching. We have a dedicated port for
537 * the data coming from the net and one to discover the BTS.
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100538 */
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800539static int forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf, int len)
540{
541 if (!tap->enabled)
542 return 0;
543
544 return sendto(fd, buf, len, 0,
545 (struct sockaddr *)&tap->forward, sizeof(tap->forward));
546}
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800547
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200548static int mgcp_send_transcoder(struct mgcp_rtp_end *end,
549 struct mgcp_config *cfg, int is_rtp,
550 const char *buf, int len)
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800551{
552 int rc;
553 int port;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800554 struct sockaddr_in addr;
555
Holger Hans Peter Freyther8b19dee2010-11-01 22:38:25 +0100556 port = is_rtp ? end->rtp_port : end->rtcp_port;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800557
558 addr.sin_family = AF_INET;
559 addr.sin_addr = cfg->transcoder_in;
Holger Hans Peter Freyther5f2cd842010-11-01 21:53:39 +0100560 addr.sin_port = port;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800561
562 rc = sendto(is_rtp ?
Holger Hans Peter Freyther8b19dee2010-11-01 22:38:25 +0100563 end->rtp.fd :
564 end->rtcp.fd, buf, len, 0,
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800565 (struct sockaddr *) &addr, sizeof(addr));
566
567 if (rc != len)
568 LOGP(DMGCP, LOGL_ERROR,
569 "Failed to send data to the transcoder: %s\n",
570 strerror(errno));
571
572 return rc;
573}
574
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100575int mgcp_send(struct mgcp_endpoint *endp, int dest, int is_rtp,
576 struct sockaddr_in *addr, char *buf, int rc)
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800577{
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100578 struct mgcp_trunk_config *tcfg = endp->tcfg;
Jacob Erlbeckb8300082013-12-19 10:00:34 +0100579 struct mgcp_rtp_end *rtp_end;
580 struct mgcp_rtp_state *rtp_state;
581 int tap_idx;
582
Holger Hans Peter Freyther1fc43292010-08-05 06:31:58 +0800583 /* For loop toggle the destination and then dispatch. */
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100584 if (tcfg->audio_loop)
Holger Hans Peter Freyther1fc43292010-08-05 06:31:58 +0800585 dest = !dest;
586
587 /* Loop based on the conn_mode, maybe undoing the above */
588 if (endp->conn_mode == MGCP_CONN_LOOPBACK)
589 dest = !dest;
590
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200591 if (dest == MGCP_DEST_NET) {
Jacob Erlbeckb8300082013-12-19 10:00:34 +0100592 rtp_end = &endp->net_end;
593 rtp_state = &endp->bts_state;
594 tap_idx = MGCP_TAP_NET_OUT;
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800595 } else {
Jacob Erlbeckb8300082013-12-19 10:00:34 +0100596 rtp_end = &endp->bts_end;
597 rtp_state = &endp->net_state;
598 tap_idx = MGCP_TAP_BTS_OUT;
599 }
600
Jacob Erlbeck0970bab2013-12-19 12:13:32 +0100601 if (!rtp_end->output_enabled)
602 rtp_end->dropped_packets += 1;
603 else if (is_rtp) {
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200604 int cont;
605 int nbytes = 0;
606 int len = rc;
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200607 do {
608 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
609 buf, &len, RTP_BUF_SIZE);
610 if (cont < 0)
611 break;
612
Holger Hans Peter Freyther1fc1ed22014-07-02 21:54:14 +0200613 mgcp_patch_and_count(endp, rtp_state, rtp_end, addr, buf, len);
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200614 forward_data(rtp_end->rtp.fd, &endp->taps[tap_idx],
615 buf, len);
616 rc = mgcp_udp_send(rtp_end->rtp.fd,
617 &rtp_end->addr,
618 rtp_end->rtp_port, buf, len);
619
620 if (rc <= 0)
621 return rc;
622 nbytes += rc;
623 len = cont;
624 } while (len > 0);
625 return nbytes;
Jacob Erlbeckb8300082013-12-19 10:00:34 +0100626 } else if (!tcfg->omit_rtcp) {
627 return mgcp_udp_send(rtp_end->rtcp.fd,
628 &rtp_end->addr,
629 rtp_end->rtcp_port, buf, rc);
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800630 }
Holger Hans Peter Freyther8c3d0692012-09-11 12:31:25 +0200631
632 return 0;
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800633}
634
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200635static int receive_from(struct mgcp_endpoint *endp, int fd, struct sockaddr_in *addr,
636 char *buf, int bufsize)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100637{
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800638 int rc;
639 socklen_t slen = sizeof(*addr);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100640
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800641 rc = recvfrom(fd, buf, bufsize, 0,
642 (struct sockaddr *) addr, &slen);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100643 if (rc < 0) {
Holger Hans Peter Freyther575b8952010-04-07 12:55:40 +0200644 LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n",
645 ENDPOINT_NUMBER(endp), errno, strerror(errno));
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100646 return -1;
647 }
648
649 /* do not forward aynthing... maybe there is a packet from the bts */
Holger Hans Peter Freyther39a97e22010-08-06 18:03:11 +0800650 if (!endp->allocated)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100651 return -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100652
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800653 #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
654
655 return rc;
656}
657
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200658static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800659{
Jacob Erlbecka0d64ce2014-03-13 14:24:52 +0100660 char buf[RTP_BUF_SIZE];
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800661 struct sockaddr_in addr;
662 struct mgcp_endpoint *endp;
663 int rc, proto;
664
665 endp = (struct mgcp_endpoint *) fd->data;
666
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200667 rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf));
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800668 if (rc <= 0)
669 return -1;
670
671 if (memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) != 0) {
672 LOGP(DMGCP, LOGL_ERROR,
Holger Hans Peter Freythereedb4532012-11-12 10:55:29 +0100673 "Endpoint 0x%x data from wrong address %s vs. ",
674 ENDPOINT_NUMBER(endp), inet_ntoa(addr.sin_addr));
675 LOGPC(DMGCP, LOGL_ERROR,
676 "%s\n", inet_ntoa(endp->net_end.addr));
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800677 return -1;
678 }
679
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100680 switch(endp->type) {
681 case MGCP_RTP_DEFAULT:
682 case MGCP_RTP_TRANSCODED:
683 if (endp->net_end.rtp_port != addr.sin_port &&
684 endp->net_end.rtcp_port != addr.sin_port) {
685 LOGP(DMGCP, LOGL_ERROR,
686 "Data from wrong source port %d on 0x%x\n",
687 ntohs(addr.sin_port), ENDPOINT_NUMBER(endp));
688 return -1;
689 }
690 break;
691 case MGCP_OSMUX_BSC:
692 case MGCP_OSMUX_BSC_NAT:
693 break;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800694 }
695
696 /* throw away the dummy message */
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200697 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800698 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from network on 0x%x\n",
699 ENDPOINT_NUMBER(endp));
700 return 0;
701 }
702
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200703 proto = fd == &endp->net_end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800704 endp->net_end.packets += 1;
Holger Hans Peter Freyther952f7522012-09-12 11:38:37 +0200705 endp->net_end.octets += rc;
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800706
707 forward_data(fd->fd, &endp->taps[MGCP_TAP_NET_IN], buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200708
709 switch (endp->type) {
710 case MGCP_RTP_DEFAULT:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200711 return mgcp_send(endp, MGCP_DEST_BTS, proto == MGCP_PROTO_RTP,
712 &addr, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200713 case MGCP_RTP_TRANSCODED:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200714 return mgcp_send_transcoder(&endp->trans_net, endp->cfg,
715 proto == MGCP_PROTO_RTP, buf, rc);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100716 case MGCP_OSMUX_BSC_NAT:
717 return osmux_xfrm_to_osmux(MGCP_DEST_BTS, buf, rc, endp);
718 case MGCP_OSMUX_BSC: /* Should not happen */
719 break;
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200720 }
721
722 LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n",
723 endp->type, ENDPOINT_NUMBER(endp));
724 return 0;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800725}
726
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800727static void discover_bts(struct mgcp_endpoint *endp, int proto, struct sockaddr_in *addr)
728{
729 struct mgcp_config *cfg = endp->cfg;
730
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200731 if (proto == MGCP_PROTO_RTP && endp->bts_end.rtp_port == 0) {
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800732 if (!cfg->bts_ip ||
733 memcmp(&addr->sin_addr,
734 &cfg->bts_in, sizeof(cfg->bts_in)) == 0 ||
735 memcmp(&addr->sin_addr,
736 &endp->bts_end.addr, sizeof(endp->bts_end.addr)) == 0) {
737
738 endp->bts_end.rtp_port = addr->sin_port;
739 endp->bts_end.addr = addr->sin_addr;
740
741 LOGP(DMGCP, LOGL_NOTICE,
742 "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n",
743 ENDPOINT_NUMBER(endp), ntohs(endp->bts_end.rtp_port),
744 ntohs(endp->bts_end.rtcp_port), inet_ntoa(addr->sin_addr));
745 }
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200746 } else if (proto == MGCP_PROTO_RTCP && endp->bts_end.rtcp_port == 0) {
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800747 if (memcmp(&endp->bts_end.addr, &addr->sin_addr,
748 sizeof(endp->bts_end.addr)) == 0) {
749 endp->bts_end.rtcp_port = addr->sin_port;
750 }
751 }
752}
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800753
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200754static int rtp_data_bts(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800755{
Jacob Erlbecka0d64ce2014-03-13 14:24:52 +0100756 char buf[RTP_BUF_SIZE];
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800757 struct sockaddr_in addr;
758 struct mgcp_endpoint *endp;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800759 int rc, proto;
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800760
761 endp = (struct mgcp_endpoint *) fd->data;
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800762
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200763 rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf));
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800764 if (rc <= 0)
765 return -1;
766
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200767 proto = fd == &endp->bts_end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100768
769 /* We have no idea who called us, maybe it is the BTS. */
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800770 /* it was the BTS... */
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800771 discover_bts(endp, proto, &addr);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100772
Holger Hans Peter Freytherea97fbf2010-08-05 10:53:26 +0000773 if (memcmp(&endp->bts_end.addr, &addr.sin_addr, sizeof(addr.sin_addr)) != 0) {
774 LOGP(DMGCP, LOGL_ERROR,
775 "Data from wrong bts %s on 0x%x\n",
776 inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp));
777 return -1;
778 }
779
780 if (endp->bts_end.rtp_port != addr.sin_port &&
781 endp->bts_end.rtcp_port != addr.sin_port) {
782 LOGP(DMGCP, LOGL_ERROR,
783 "Data from wrong bts source port %d on 0x%x\n",
784 ntohs(addr.sin_port), ENDPOINT_NUMBER(endp));
785 return -1;
786 }
787
Holger Hans Peter Freytherb3c206a2010-05-14 02:18:59 +0800788 /* throw away the dummy message */
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200789 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800790 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from bts on 0x%x\n",
Holger Hans Peter Freytheraa9d3ce2010-04-22 12:14:51 +0800791 ENDPOINT_NUMBER(endp));
792 return 0;
793 }
794
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +0200795 /* do this before the loop handling */
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800796 endp->bts_end.packets += 1;
Holger Hans Peter Freyther952f7522012-09-12 11:38:37 +0200797 endp->bts_end.octets += rc;
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +0200798
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800799 forward_data(fd->fd, &endp->taps[MGCP_TAP_BTS_IN], buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200800
801 switch (endp->type) {
802 case MGCP_RTP_DEFAULT:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200803 return mgcp_send(endp, MGCP_DEST_NET, proto == MGCP_PROTO_RTP,
804 &addr, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200805 case MGCP_RTP_TRANSCODED:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200806 return mgcp_send_transcoder(&endp->trans_bts, endp->cfg,
807 proto == MGCP_PROTO_RTP, buf, rc);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100808 case MGCP_OSMUX_BSC:
809 /* OSMUX translation: BTS -> BSC */
810 return osmux_xfrm_to_osmux(MGCP_DEST_NET, buf, rc, endp);
811 case MGCP_OSMUX_BSC_NAT:
812 break; /* Should not happen */
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200813 }
814
815 LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n",
816 endp->type, ENDPOINT_NUMBER(endp));
817 return 0;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800818}
819
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100820static int rtp_data_transcoder(struct mgcp_rtp_end *end, struct mgcp_endpoint *_endp,
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200821 int dest, struct osmo_fd *fd)
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800822{
Jacob Erlbecka0d64ce2014-03-13 14:24:52 +0100823 char buf[RTP_BUF_SIZE];
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800824 struct sockaddr_in addr;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800825 struct mgcp_config *cfg;
826 int rc, proto;
827
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100828 cfg = _endp->cfg;
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200829 rc = receive_from(_endp, fd->fd, &addr, buf, sizeof(buf));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800830 if (rc <= 0)
831 return -1;
832
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200833 proto = fd == &end->rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800834
835 if (memcmp(&addr.sin_addr, &cfg->transcoder_in, sizeof(addr.sin_addr)) != 0) {
836 LOGP(DMGCP, LOGL_ERROR,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100837 "Data not coming from transcoder dest: %d %s on 0x%x\n",
838 dest, inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(_endp));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800839 return -1;
840 }
841
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100842 if (end->rtp_port != addr.sin_port &&
843 end->rtcp_port != addr.sin_port) {
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800844 LOGP(DMGCP, LOGL_ERROR,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100845 "Data from wrong transcoder dest %d source port %d on 0x%x\n",
846 dest, ntohs(addr.sin_port), ENDPOINT_NUMBER(_endp));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800847 return -1;
848 }
849
850 /* throw away the dummy message */
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200851 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100852 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from transcoder dest %d on 0x%x\n",
853 dest, ENDPOINT_NUMBER(_endp));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800854 return 0;
855 }
856
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100857 end->packets += 1;
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200858 return mgcp_send(_endp, dest, proto == MGCP_PROTO_RTP, &addr, buf, rc);
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100859}
860
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200861static int rtp_data_trans_net(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100862{
863 struct mgcp_endpoint *endp;
864 endp = (struct mgcp_endpoint *) fd->data;
865
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200866 return rtp_data_transcoder(&endp->trans_net, endp, MGCP_DEST_NET, fd);
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100867}
868
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200869static int rtp_data_trans_bts(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100870{
871 struct mgcp_endpoint *endp;
872 endp = (struct mgcp_endpoint *) fd->data;
873
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200874 return rtp_data_transcoder(&endp->trans_bts, endp, MGCP_DEST_BTS, fd);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100875}
876
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100877int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100878{
879 struct sockaddr_in addr;
880 int on = 1;
881
882 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
883 if (fd->fd < 0) {
884 LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n");
885 return -1;
886 }
887
888 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
889 memset(&addr, 0, sizeof(addr));
890 addr.sin_family = AF_INET;
891 addr.sin_port = htons(port);
892 inet_aton(source_addr, &addr.sin_addr);
893
894 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Holger Hans Peter Freyther6f36e922010-08-06 03:00:17 +0800895 close(fd->fd);
896 fd->fd = -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100897 return -1;
898 }
899
900 return 0;
901}
902
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800903static int set_ip_tos(int fd, int tos)
904{
905 int ret;
906 ret = setsockopt(fd, IPPROTO_IP, IP_TOS,
907 &tos, sizeof(tos));
908 return ret != 0;
909}
910
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800911static int bind_rtp(struct mgcp_config *cfg, struct mgcp_rtp_end *rtp_end, int endpno)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100912{
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200913 if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtp,
914 rtp_end->local_port) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100915 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800916 cfg->source_addr, rtp_end->local_port, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100917 goto cleanup0;
918 }
919
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200920 if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtcp,
921 rtp_end->local_port + 1) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100922 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800923 cfg->source_addr, rtp_end->local_port + 1, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100924 goto cleanup1;
925 }
926
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800927 set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
928 set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800929
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800930 rtp_end->rtp.when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200931 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100932 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800933 rtp_end->local_port, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100934 goto cleanup2;
935 }
936
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800937 rtp_end->rtcp.when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200938 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100939 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800940 rtp_end->local_port + 1, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100941 goto cleanup3;
942 }
943
944 return 0;
945
946cleanup3:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200947 osmo_fd_unregister(&rtp_end->rtp);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100948cleanup2:
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800949 close(rtp_end->rtcp.fd);
950 rtp_end->rtcp.fd = -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100951cleanup1:
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800952 close(rtp_end->rtp.fd);
953 rtp_end->rtp.fd = -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100954cleanup0:
955 return -1;
956}
957
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100958static int int_bind(const char *port,
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200959 struct mgcp_rtp_end *end, int (*cb)(struct osmo_fd *, unsigned),
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100960 struct mgcp_endpoint *_endp, int rtp_port)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100961{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100962 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
963 LOGP(DMGCP, LOGL_ERROR, "Previous %s was still bound on %d\n",
964 port, ENDPOINT_NUMBER(_endp));
965 mgcp_free_rtp_port(end);
Holger Hans Peter Freyther7fe2a3d2010-08-06 07:18:22 +0800966 }
967
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100968 end->local_port = rtp_port;
969 end->rtp.cb = cb;
970 end->rtp.data = _endp;
971 end->rtcp.data = _endp;
972 end->rtcp.cb = cb;
973 return bind_rtp(_endp->cfg, end, ENDPOINT_NUMBER(_endp));
974}
975
976
977int mgcp_bind_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
978{
979 return int_bind("bts-port", &endp->bts_end,
980 rtp_data_bts, endp, rtp_port);
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800981}
982
983int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
984{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100985 return int_bind("net-port", &endp->net_end,
986 rtp_data_net, endp, rtp_port);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100987}
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800988
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100989int mgcp_bind_trans_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800990{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100991 return int_bind("trans-net", &endp->trans_net,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100992 rtp_data_trans_net, endp, rtp_port);
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800993}
994
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100995int mgcp_bind_trans_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
996{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100997 return int_bind("trans-bts", &endp->trans_bts,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100998 rtp_data_trans_bts, endp, rtp_port);
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100999}
1000
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +08001001int mgcp_free_rtp_port(struct mgcp_rtp_end *end)
1002{
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +08001003 if (end->rtp.fd != -1) {
1004 close(end->rtp.fd);
1005 end->rtp.fd = -1;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001006 osmo_fd_unregister(&end->rtp);
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +08001007 }
1008
1009 if (end->rtcp.fd != -1) {
1010 close(end->rtcp.fd);
1011 end->rtcp.fd = -1;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001012 osmo_fd_unregister(&end->rtcp);
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +08001013 }
1014
1015 return 0;
1016}
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +02001017
1018
1019void mgcp_state_calc_loss(struct mgcp_rtp_state *state,
1020 struct mgcp_rtp_end *end, uint32_t *expected,
1021 int *loss)
1022{
Holger Hans Peter Freythera5a59c92014-10-06 20:04:42 +02001023 *expected = state->stats_cycles + state->stats_max_seq;
1024 *expected = *expected - state->stats_base_seq + 1;
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +02001025
Holger Hans Peter Freythera5a59c92014-10-06 20:04:42 +02001026 if (!state->stats_initialized) {
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +02001027 *expected = 0;
1028 *loss = 0;
1029 return;
1030 }
1031
1032 /*
1033 * Make sure the sign is correct and use the biggest
1034 * positive/negative number that fits.
1035 */
1036 *loss = *expected - end->packets;
1037 if (*expected < end->packets) {
1038 if (*loss > 0)
1039 *loss = INT_MIN;
1040 } else {
1041 if (*loss < 0)
1042 *loss = INT_MAX;
1043 }
1044}
Holger Hans Peter Freythercb306a62012-10-24 21:22:47 +02001045
1046uint32_t mgcp_state_calc_jitter(struct mgcp_rtp_state *state)
1047{
Holger Hans Peter Freythera5a59c92014-10-06 20:04:42 +02001048 if (!state->stats_initialized)
Holger Hans Peter Freythercb306a62012-10-24 21:22:47 +02001049 return 0;
Holger Hans Peter Freythera5a59c92014-10-06 20:04:42 +02001050 return state->stats_jitter >> 4;
Holger Hans Peter Freythercb306a62012-10-24 21:22:47 +02001051}