blob: 5363fb8de49236d5bf02bc083d6401958f59d6d6 [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 Freyther1b0ea972010-02-21 11:11:04 +010037
38#warning "Make use of the rtp proxy code"
39
Holger Hans Peter Freyther3cb28792010-10-12 15:39:46 +020040/* attempt to determine byte order */
Holger Hans Peter Freyther3cb28792010-10-12 15:39:46 +020041#include <sys/param.h>
42#include <limits.h>
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +020043#include <time.h>
Holger Hans Peter Freyther3cb28792010-10-12 15:39:46 +020044
45#ifndef __BYTE_ORDER
Tobias Engelaff20712012-10-24 17:53:50 +020046# ifdef __APPLE__
47# define __BYTE_ORDER __DARWIN_BYTE_ORDER
48# define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
49# define __BIG_ENDIAN __DARWIN_BIG_ENDIAN
50# else
51# error "__BYTE_ORDER should be defined by someone"
52# endif
Holger Hans Peter Freyther3cb28792010-10-12 15:39:46 +020053#endif
54
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010055/* according to rtp_proxy.c RFC 3550 */
56struct rtp_hdr {
57#if __BYTE_ORDER == __LITTLE_ENDIAN
Holger Hans Peter Freyther7dece862010-07-23 18:56:26 +080058 uint8_t csrc_count:4,
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010059 extension:1,
60 padding:1,
61 version:2;
Holger Hans Peter Freyther7dece862010-07-23 18:56:26 +080062 uint8_t payload_type:7,
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010063 marker:1;
64#elif __BYTE_ORDER == __BIG_ENDIAN
Holger Hans Peter Freyther7dece862010-07-23 18:56:26 +080065 uint8_t version:2,
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010066 padding:1,
67 extension:1,
68 csrc_count:4;
Holger Hans Peter Freyther7dece862010-07-23 18:56:26 +080069 uint8_t marker:1,
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010070 payload_type:7;
71#endif
Holger Hans Peter Freytherd340cd32010-07-23 18:56:01 +080072 uint16_t sequence;
Holger Hans Peter Freytherd9b18f82010-07-23 18:55:38 +080073 uint32_t timestamp;
74 uint32_t ssrc;
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010075} __attribute__((packed));
76
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +020077#define RTP_SEQ_MOD (1 << 16)
78#define RTP_MAX_DROPOUT 3000
79#define RTP_MAX_MISORDER 100
80
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010081
82enum {
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +020083 MGCP_DEST_NET = 0,
84 MGCP_DEST_BTS,
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010085};
86
87enum {
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +020088 MGCP_PROTO_RTP,
89 MGCP_PROTO_RTCP,
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010090};
91
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +020092#define MGCP_DUMMY_LOAD 0x23
Holger Hans Peter Freytherb844b872010-04-21 21:25:13 +080093
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010094
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +020095/**
96 * This does not need to be a precision timestamp and
97 * is allowed to wrap quite fast. The returned value is
Jacob Erlbeck303b54a2014-01-30 21:01:34 +010098 * 1/unit seconds.
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +020099 */
Jacob Erlbeck303b54a2014-01-30 21:01:34 +0100100static uint32_t get_current_ts(unsigned unit)
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200101{
102 struct timespec tp;
103 uint64_t ret;
104
Jacob Erlbeck303b54a2014-01-30 21:01:34 +0100105 if (!unit)
106 return 0;
107
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200108 memset(&tp, 0, sizeof(tp));
109 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
110 LOGP(DMGCP, LOGL_NOTICE,
111 "Getting the clock failed.\n");
112
Jacob Erlbeck303b54a2014-01-30 21:01:34 +0100113 /* convert it to 1/unit seconds */
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200114 ret = tp.tv_sec;
Jacob Erlbeck303b54a2014-01-30 21:01:34 +0100115 ret *= unit;
116 ret += (int64_t)tp.tv_nsec * unit / 1000 / 1000 / 1000;
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200117
118 return ret;
119}
120
Pablo Neira Ayusod81fec02013-08-25 17:39:38 +0200121static int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf,
122 int len)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100123{
124 struct sockaddr_in out;
125 out.sin_family = AF_INET;
126 out.sin_port = port;
127 memcpy(&out.sin_addr, addr, sizeof(*addr));
128
129 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
130}
131
Holger Hans Peter Freytherb844b872010-04-21 21:25:13 +0800132int mgcp_send_dummy(struct mgcp_endpoint *endp)
133{
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200134 static char buf[] = { MGCP_DUMMY_LOAD };
Jacob Erlbeck34bdc9f2013-12-19 18:25:54 +0100135 int rc;
Holger Hans Peter Freytherb844b872010-04-21 21:25:13 +0800136
Jacob Erlbeck34bdc9f2013-12-19 18:25:54 +0100137 rc = mgcp_udp_send(endp->net_end.rtp.fd, &endp->net_end.addr,
138 endp->net_end.rtp_port, buf, 1);
139
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100140 if (rc == -1)
141 goto failed;
142
143 if (endp->tcfg->omit_rtcp)
Jacob Erlbeck34bdc9f2013-12-19 18:25:54 +0100144 return rc;
145
Jacob Erlbeck1dc022c2014-01-17 09:22:57 +0100146 rc = mgcp_udp_send(endp->net_end.rtcp.fd, &endp->net_end.addr,
147 endp->net_end.rtcp_port, buf, 1);
148
149 if (rc >= 0)
150 return rc;
151
152failed:
153 LOGP(DMGCP, LOGL_ERROR,
154 "Failed to send dummy packet: %s on: 0x%x to %s\n",
155 strerror(errno), ENDPOINT_NUMBER(endp), inet_ntoa(endp->net_end.addr));
156
157 return -1;
Holger Hans Peter Freytherb844b872010-04-21 21:25:13 +0800158}
159
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100160static int32_t compute_timestamp_aligment_error(struct mgcp_rtp_stream_state *sstate,
161 int ptime, uint32_t timestamp)
162{
163 int32_t timestamp_delta;
164
165 if (ptime == 0)
166 return 0;
167
168 /* Align according to: T - Tlast = k * Tptime */
169 timestamp_delta = timestamp - sstate->last_timestamp;
170
171 return timestamp_delta % ptime;
172}
173
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100174static int check_rtp_timestamp(struct mgcp_endpoint *endp,
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100175 struct mgcp_rtp_state *state,
176 struct mgcp_rtp_stream_state *sstate,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100177 struct mgcp_rtp_end *rtp_end,
178 struct sockaddr_in *addr,
179 uint16_t seq, uint32_t timestamp,
180 const char *text, int32_t *tsdelta_out)
181{
182 int32_t tsdelta;
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100183 int32_t timestamp_error;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100184
185 /* Not fully intialized, skip */
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100186 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100187 return 0;
188
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100189 if (seq == sstate->last_seq) {
190 if (timestamp != sstate->last_timestamp) {
191 sstate->err_ts_counter += 1;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100192 LOGP(DMGCP, LOGL_ERROR,
193 "The %s timestamp delta is != 0 but the sequence "
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100194 "number %d is the same, "
195 "TS offset: %d, SeqNo offset: %d "
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100196 "on 0x%x SSRC: %u timestamp: %u "
197 "from %s:%d in %d\n",
198 text, seq,
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100199 state->timestamp_offset, state->seq_offset,
200 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100201 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
202 endp->conn_mode);
203 }
204 return 0;
205 }
206
207 tsdelta =
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100208 (int32_t)(timestamp - sstate->last_timestamp) /
209 (int16_t)(seq - sstate->last_seq);
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100210
211 if (tsdelta == 0) {
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100212 /* Don't update *tsdelta_out */
213 LOGP(DMGCP, LOGL_NOTICE,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100214 "The %s timestamp delta is %d "
215 "on 0x%x SSRC: %u timestamp: %u "
216 "from %s:%d in %d\n",
217 text, tsdelta,
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100218 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100219 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
220 endp->conn_mode);
221
222 return 0;
223 }
224
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100225 if (sstate->last_tsdelta != tsdelta) {
226 if (sstate->last_tsdelta) {
227 LOGP(DMGCP, LOGL_INFO,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100228 "The %s timestamp delta changes from %d to %d "
229 "on 0x%x SSRC: %u timestamp: %u from %s:%d in %d\n",
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100230 text, sstate->last_tsdelta, tsdelta,
231 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100232 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
233 endp->conn_mode);
234 }
235 }
236
237 if (tsdelta_out)
238 *tsdelta_out = tsdelta;
239
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100240 timestamp_error =
241 compute_timestamp_aligment_error(sstate, state->packet_duration,
242 timestamp);
243
244 if (timestamp_error) {
245 sstate->err_ts_counter += 1;
246 LOGP(DMGCP, LOGL_NOTICE,
247 "The %s timestamp has an alignment error of %d "
248 "on 0x%x SSRC: %u "
249 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
250 "from %s:%d in %d\n",
251 text, timestamp_error,
252 ENDPOINT_NUMBER(endp), sstate->ssrc,
253 (int16_t)(seq - sstate->last_seq),
254 (int32_t)(timestamp - sstate->last_timestamp),
255 tsdelta,
256 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
257 endp->conn_mode);
258 }
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100259 return 1;
260}
261
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100262/* Set the timestamp offset according to the packet duration. */
263static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp,
264 struct mgcp_rtp_state *state,
265 struct mgcp_rtp_end *rtp_end,
266 struct sockaddr_in *addr,
Jacob Erlbeck785e3c92013-12-19 17:50:27 +0100267 int16_t delta_seq, uint32_t in_timestamp)
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100268{
269 int32_t tsdelta = state->packet_duration;
270 int timestamp_offset;
Jacob Erlbeck785e3c92013-12-19 17:50:27 +0100271 uint32_t out_timestamp;
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100272
273 if (tsdelta == 0) {
274 tsdelta = state->out_stream.last_tsdelta;
275 if (tsdelta != 0) {
276 LOGP(DMGCP, LOGL_NOTICE,
277 "A fixed packet duration is not available on 0x%x, "
278 "using last output timestamp delta instead: %d "
279 "from %s:%d in %d\n",
280 ENDPOINT_NUMBER(endp), tsdelta,
281 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
282 endp->conn_mode);
283 } else {
284 tsdelta = rtp_end->rate * 20 / 1000;
285 LOGP(DMGCP, LOGL_NOTICE,
286 "Fixed packet duration and last timestamp delta "
287 "are not available on 0x%x, "
288 "using fixed 20ms instead: %d "
289 "from %s:%d in %d\n",
290 ENDPOINT_NUMBER(endp), tsdelta,
291 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
292 endp->conn_mode);
293 }
294 }
295
Jacob Erlbeck785e3c92013-12-19 17:50:27 +0100296 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
297 timestamp_offset = out_timestamp - in_timestamp;
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100298
299 if (state->timestamp_offset != timestamp_offset) {
300 state->timestamp_offset = timestamp_offset;
301
302 LOGP(DMGCP, LOGL_NOTICE,
303 "Timestamp offset change on 0x%x SSRC: %u "
304 "SeqNo delta: %d, TS offset: %d, "
305 "from %s:%d in %d\n",
306 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
307 delta_seq, state->timestamp_offset,
308 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
309 endp->conn_mode);
310 }
311
312 return timestamp_offset;
313}
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100314
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100315/* Set the timestamp offset according to the packet duration. */
316static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp,
317 struct mgcp_rtp_state *state,
318 struct mgcp_rtp_end *rtp_end,
319 struct sockaddr_in *addr,
320 uint32_t timestamp)
321{
322 int timestamp_error = 0;
323 int ptime = state->packet_duration;
324
325 /* Align according to: T + Toffs - Tlast = k * Tptime */
326
327 timestamp_error = compute_timestamp_aligment_error(
328 &state->out_stream, ptime,
329 timestamp + state->timestamp_offset);
330
331 if (timestamp_error) {
332 state->timestamp_offset += ptime - timestamp_error;
333
334 LOGP(DMGCP, LOGL_NOTICE,
335 "Corrected timestamp alignment error of %d on 0x%x SSRC: %u "
336 "new TS offset: %d, "
337 "from %s:%d in %d\n",
338 timestamp_error,
339 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
340 state->timestamp_offset, inet_ntoa(addr->sin_addr),
341 ntohs(addr->sin_port), endp->conn_mode);
342 }
343
344 OSMO_ASSERT(compute_timestamp_aligment_error(&state->out_stream, ptime,
345 timestamp + state->timestamp_offset) == 0);
346
347 return timestamp_error;
348}
349
350
Holger Hans Peter Freyther1e85af62012-10-22 17:15:31 +0200351/**
352 * The RFC 3550 Appendix A assumes there are multiple sources but
353 * some of the supported endpoints (e.g. the nanoBTS) can only handle
354 * one source and this code will patch packages to appear as if there
355 * is only one source.
356 * There is also no probation period for new sources. Every package
357 * we receive will be seen as a switch in streams.
358 */
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100359void mgcp_patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
360 struct mgcp_rtp_end *rtp_end, struct sockaddr_in *addr,
361 char *data, int len)
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100362{
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200363 uint32_t arrival_time;
364 int32_t transit, d;
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200365 uint16_t seq, udelta;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100366 uint32_t timestamp, ssrc;
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100367 struct rtp_hdr *rtp_hdr;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100368 int payload = rtp_end->payload_type;
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100369
370 if (len < sizeof(*rtp_hdr))
371 return;
372
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800373 rtp_hdr = (struct rtp_hdr *) data;
374 seq = ntohs(rtp_hdr->sequence);
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000375 timestamp = ntohl(rtp_hdr->timestamp);
Jacob Erlbeck303b54a2014-01-30 21:01:34 +0100376 arrival_time = get_current_ts(rtp_end->rate);
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100377 ssrc = ntohl(rtp_hdr->ssrc);
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800378
Holger Hans Peter Freyther31868922010-08-03 11:59:04 +0000379 if (!state->initialized) {
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100380 state->in_stream.last_seq = seq - 1;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100381 state->in_stream.ssrc = state->orig_ssrc = ssrc;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100382 state->in_stream.last_tsdelta = 0;
Holger Hans Peter Freythered3a6612012-10-22 17:08:48 +0200383 state->base_seq = seq;
Holger Hans Peter Freyther31868922010-08-03 11:59:04 +0000384 state->initialized = 1;
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200385 state->jitter = 0;
386 state->transit = arrival_time - timestamp;
Jacob Erlbeckf6ec0e92013-12-04 10:30:11 +0100387 state->packet_duration = mgcp_rtp_packet_duration(endp, rtp_end);
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100388 state->out_stream = state->in_stream;
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100389 state->out_stream.last_timestamp = timestamp;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100390 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100391 LOGP(DMGCP, LOGL_INFO,
392 "Initializing stream on 0x%x SSRC: %u timestamp: %u "
Jacob Erlbeck30ce4222013-12-05 12:02:15 +0100393 "pkt-duration: %d, from %s:%d in %d\n",
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100394 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Jacob Erlbeck30ce4222013-12-05 12:02:15 +0100395 state->seq_offset, state->packet_duration,
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100396 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
397 endp->conn_mode);
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100398 if (state->packet_duration == 0) {
399 state->packet_duration = rtp_end->rate * 20 / 1000;
400 LOGP(DMGCP, LOGL_NOTICE,
401 "Fixed packet duration is not available on 0x%x, "
402 "using fixed 20ms instead: %d from %s:%d in %d\n",
403 ENDPOINT_NUMBER(endp), state->packet_duration,
404 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
405 endp->conn_mode);
406 }
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100407 } else if (state->in_stream.ssrc != ssrc) {
Jacob Erlbeck5e9549e2013-12-05 12:20:05 +0100408 LOGP(DMGCP, LOGL_NOTICE,
409 "The SSRC changed on 0x%x: %u -> %u "
410 "from %s:%d in %d\n",
411 ENDPOINT_NUMBER(endp),
412 state->in_stream.ssrc, rtp_hdr->ssrc,
413 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
414 endp->conn_mode);
415
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100416 state->in_stream.ssrc = ssrc;
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100417 if (rtp_end->force_constant_ssrc) {
Jacob Erlbeckeacc9b92014-01-30 21:01:35 +0100418 int16_t delta_seq;
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100419
Jacob Erlbeckeacc9b92014-01-30 21:01:35 +0100420 /* Always increment seqno by 1 */
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100421 state->seq_offset =
Jacob Erlbeckeacc9b92014-01-30 21:01:35 +0100422 (state->out_stream.last_seq + 1) - seq;
423
424 /* Estimate number of packets that would have been sent */
425 delta_seq =
426 (arrival_time - state->in_stream.last_arrival_time
427 + state->packet_duration/2) /
428 state->packet_duration;
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100429
430 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
431 delta_seq, timestamp);
432
Jacob Erlbeck58340e52013-11-29 11:20:06 +0100433 state->patch_ssrc = 1;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100434 ssrc = state->orig_ssrc;
Jacob Erlbecke2292f32013-12-03 15:13:12 +0100435 if (rtp_end->force_constant_ssrc != -1)
436 rtp_end->force_constant_ssrc -= 1;
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100437
438 LOGP(DMGCP, LOGL_NOTICE,
439 "SSRC patching enabled on 0x%x SSRC: %u "
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100440 "SeqNo offset: %d, TS offset: %d "
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100441 "from %s:%d in %d\n",
442 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Jacob Erlbeck33f30092013-12-10 13:09:37 +0100443 state->seq_offset, state->timestamp_offset,
Jacob Erlbeckb35a7772013-12-05 11:33:20 +0100444 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
445 endp->conn_mode);
446 }
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100447
448 state->in_stream.last_tsdelta = 0;
449 } else {
450 /* Compute current per-packet timestamp delta */
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100451 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end, addr,
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100452 seq, timestamp, "input",
453 &state->in_stream.last_tsdelta);
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100454
Jacob Erlbeck58340e52013-11-29 11:20:06 +0100455 if (state->patch_ssrc)
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100456 ssrc = state->orig_ssrc;
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000457 }
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800458
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100459 /* Save before patching */
460 state->in_stream.last_timestamp = timestamp;
461 state->in_stream.last_seq = seq;
Jacob Erlbeckeacc9b92014-01-30 21:01:35 +0100462 state->in_stream.last_arrival_time = arrival_time;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100463
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100464 if (rtp_end->force_aligned_timing &&
465 state->out_stream.ssrc == ssrc && state->packet_duration)
466 /* Align the timestamp offset */
467 align_rtp_timestamp_offset(endp, state, rtp_end, addr, timestamp);
Jacob Erlbeck58340e52013-11-29 11:20:06 +0100468
469 /* Store the updated SSRC back to the packet */
470 if (state->patch_ssrc)
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100471 rtp_hdr->ssrc = htonl(ssrc);
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000472
Jacob Erlbeck58340e52013-11-29 11:20:06 +0100473 /* Apply the offset and store it back to the packet.
474 * This won't change anything if the offset is 0, so the conditional is
475 * omitted. */
476 seq += state->seq_offset;
477 rtp_hdr->sequence = htons(seq);
478 timestamp += state->timestamp_offset;
479 rtp_hdr->timestamp = htonl(timestamp);
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000480
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100481 /* Check again, whether the timestamps are still valid */
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100482 if (state->out_stream.ssrc == ssrc)
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100483 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
484 addr, seq, timestamp, "output",
Jacob Erlbeck55ba1402013-11-29 11:16:07 +0100485 &state->out_stream.last_tsdelta);
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100486
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200487 /*
488 * The below takes the shape of the validation from Appendix A. Check
489 * if there is something weird with the sequence number, otherwise check
490 * for a wrap around in the sequence number.
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100491 *
492 * Note that last_seq is used where the appendix mentions max_seq.
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200493 */
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100494 udelta = seq - state->out_stream.last_seq;
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200495 if (udelta < RTP_MAX_DROPOUT) {
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100496 if (seq < state->out_stream.last_seq)
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200497 state->cycles += RTP_SEQ_MOD;
Holger Hans Peter Freyther7e7ee5f2012-12-16 13:07:45 +0100498 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Holger Hans Peter Freyther769912c2012-10-22 17:30:12 +0200499 LOGP(DMGCP, LOGL_NOTICE,
500 "RTP seqno made a very large jump on 0x%x delta: %u\n",
501 ENDPOINT_NUMBER(endp), udelta);
502 }
503
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200504 /*
Jacob Erlbeck303b54a2014-01-30 21:01:34 +0100505 * Calculate the jitter between the two packages. The TS should be
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200506 * taken closer to the read function. This was taken from the
Jacob Erlbeck303b54a2014-01-30 21:01:34 +0100507 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
508 * resolution.
Holger Hans Peter Freyther30690ad2012-10-24 20:31:25 +0200509 */
510 transit = arrival_time - timestamp;
511 d = transit - state->transit;
512 state->transit = transit;
513 if (d < 0)
514 d = -d;
515 state->jitter += d - ((state->jitter + 8) >> 4);
516
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100517 /* Save output values */
518 state->out_stream.last_seq = seq;
519 state->out_stream.last_timestamp = timestamp;
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100520 state->out_stream.ssrc = ssrc;
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000521
Holger Hans Peter Freyther7279d242010-04-06 11:15:50 +0200522 if (payload < 0)
523 return;
524
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100525 rtp_hdr->payload_type = payload;
526}
527
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100528/*
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800529 * The below code is for dispatching. We have a dedicated port for
530 * the data coming from the net and one to discover the BTS.
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100531 */
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800532static int forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf, int len)
533{
534 if (!tap->enabled)
535 return 0;
536
537 return sendto(fd, buf, len, 0,
538 (struct sockaddr *)&tap->forward, sizeof(tap->forward));
539}
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800540
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200541static int mgcp_send_transcoder(struct mgcp_rtp_end *end,
542 struct mgcp_config *cfg, int is_rtp,
543 const char *buf, int len)
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800544{
545 int rc;
546 int port;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800547 struct sockaddr_in addr;
548
Holger Hans Peter Freyther8b19dee2010-11-01 22:38:25 +0100549 port = is_rtp ? end->rtp_port : end->rtcp_port;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800550
551 addr.sin_family = AF_INET;
552 addr.sin_addr = cfg->transcoder_in;
Holger Hans Peter Freyther5f2cd842010-11-01 21:53:39 +0100553 addr.sin_port = port;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800554
555 rc = sendto(is_rtp ?
Holger Hans Peter Freyther8b19dee2010-11-01 22:38:25 +0100556 end->rtp.fd :
557 end->rtcp.fd, buf, len, 0,
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800558 (struct sockaddr *) &addr, sizeof(addr));
559
560 if (rc != len)
561 LOGP(DMGCP, LOGL_ERROR,
562 "Failed to send data to the transcoder: %s\n",
563 strerror(errno));
564
565 return rc;
566}
567
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200568static int mgcp_send(struct mgcp_endpoint *endp, int dest, int is_rtp,
569 struct sockaddr_in *addr, char *buf, int rc)
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800570{
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100571 struct mgcp_trunk_config *tcfg = endp->tcfg;
Jacob Erlbeckb8300082013-12-19 10:00:34 +0100572 struct mgcp_rtp_end *rtp_end;
573 struct mgcp_rtp_state *rtp_state;
574 int tap_idx;
575
Holger Hans Peter Freyther1fc43292010-08-05 06:31:58 +0800576 /* For loop toggle the destination and then dispatch. */
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100577 if (tcfg->audio_loop)
Holger Hans Peter Freyther1fc43292010-08-05 06:31:58 +0800578 dest = !dest;
579
580 /* Loop based on the conn_mode, maybe undoing the above */
581 if (endp->conn_mode == MGCP_CONN_LOOPBACK)
582 dest = !dest;
583
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200584 if (dest == MGCP_DEST_NET) {
Jacob Erlbeckb8300082013-12-19 10:00:34 +0100585 rtp_end = &endp->net_end;
586 rtp_state = &endp->bts_state;
587 tap_idx = MGCP_TAP_NET_OUT;
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800588 } else {
Jacob Erlbeckb8300082013-12-19 10:00:34 +0100589 rtp_end = &endp->bts_end;
590 rtp_state = &endp->net_state;
591 tap_idx = MGCP_TAP_BTS_OUT;
592 }
593
Jacob Erlbeck0970bab2013-12-19 12:13:32 +0100594 if (!rtp_end->output_enabled)
595 rtp_end->dropped_packets += 1;
596 else if (is_rtp) {
Jacob Erlbeckb8300082013-12-19 10:00:34 +0100597 mgcp_patch_and_count(endp, rtp_state, rtp_end, addr, buf, rc);
598 forward_data(rtp_end->rtp.fd, &endp->taps[tap_idx], buf, rc);
599 return mgcp_udp_send(rtp_end->rtp.fd,
600 &rtp_end->addr,
601 rtp_end->rtp_port, buf, rc);
602 } else if (!tcfg->omit_rtcp) {
603 return mgcp_udp_send(rtp_end->rtcp.fd,
604 &rtp_end->addr,
605 rtp_end->rtcp_port, buf, rc);
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800606 }
Holger Hans Peter Freyther8c3d0692012-09-11 12:31:25 +0200607
608 return 0;
Holger Hans Peter Freyther84e1c472010-08-05 06:19:24 +0800609}
610
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200611static int receive_from(struct mgcp_endpoint *endp, int fd, struct sockaddr_in *addr,
612 char *buf, int bufsize)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100613{
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800614 int rc;
615 socklen_t slen = sizeof(*addr);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100616
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800617 rc = recvfrom(fd, buf, bufsize, 0,
618 (struct sockaddr *) addr, &slen);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100619 if (rc < 0) {
Holger Hans Peter Freyther575b8952010-04-07 12:55:40 +0200620 LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n",
621 ENDPOINT_NUMBER(endp), errno, strerror(errno));
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100622 return -1;
623 }
624
625 /* do not forward aynthing... maybe there is a packet from the bts */
Holger Hans Peter Freyther39a97e22010-08-06 18:03:11 +0800626 if (!endp->allocated)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100627 return -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100628
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800629 #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
630
631 return rc;
632}
633
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200634static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800635{
636 char buf[4096];
637 struct sockaddr_in addr;
638 struct mgcp_endpoint *endp;
639 int rc, proto;
640
641 endp = (struct mgcp_endpoint *) fd->data;
642
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200643 rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf));
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800644 if (rc <= 0)
645 return -1;
646
647 if (memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) != 0) {
648 LOGP(DMGCP, LOGL_ERROR,
Holger Hans Peter Freythereedb4532012-11-12 10:55:29 +0100649 "Endpoint 0x%x data from wrong address %s vs. ",
650 ENDPOINT_NUMBER(endp), inet_ntoa(addr.sin_addr));
651 LOGPC(DMGCP, LOGL_ERROR,
652 "%s\n", inet_ntoa(endp->net_end.addr));
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800653 return -1;
654 }
655
656 if (endp->net_end.rtp_port != addr.sin_port &&
657 endp->net_end.rtcp_port != addr.sin_port) {
658 LOGP(DMGCP, LOGL_ERROR,
659 "Data from wrong source port %d on 0x%x\n",
660 ntohs(addr.sin_port), ENDPOINT_NUMBER(endp));
661 return -1;
662 }
663
664 /* throw away the dummy message */
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200665 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800666 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from network on 0x%x\n",
667 ENDPOINT_NUMBER(endp));
668 return 0;
669 }
670
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200671 proto = fd == &endp->net_end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800672 endp->net_end.packets += 1;
Holger Hans Peter Freyther952f7522012-09-12 11:38:37 +0200673 endp->net_end.octets += rc;
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800674
675 forward_data(fd->fd, &endp->taps[MGCP_TAP_NET_IN], buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200676
677 switch (endp->type) {
678 case MGCP_RTP_DEFAULT:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200679 return mgcp_send(endp, MGCP_DEST_BTS, proto == MGCP_PROTO_RTP,
680 &addr, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200681 case MGCP_RTP_TRANSCODED:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200682 return mgcp_send_transcoder(&endp->trans_net, endp->cfg,
683 proto == MGCP_PROTO_RTP, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200684 }
685
686 LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n",
687 endp->type, ENDPOINT_NUMBER(endp));
688 return 0;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800689}
690
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800691static void discover_bts(struct mgcp_endpoint *endp, int proto, struct sockaddr_in *addr)
692{
693 struct mgcp_config *cfg = endp->cfg;
694
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200695 if (proto == MGCP_PROTO_RTP && endp->bts_end.rtp_port == 0) {
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800696 if (!cfg->bts_ip ||
697 memcmp(&addr->sin_addr,
698 &cfg->bts_in, sizeof(cfg->bts_in)) == 0 ||
699 memcmp(&addr->sin_addr,
700 &endp->bts_end.addr, sizeof(endp->bts_end.addr)) == 0) {
701
702 endp->bts_end.rtp_port = addr->sin_port;
703 endp->bts_end.addr = addr->sin_addr;
704
705 LOGP(DMGCP, LOGL_NOTICE,
706 "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n",
707 ENDPOINT_NUMBER(endp), ntohs(endp->bts_end.rtp_port),
708 ntohs(endp->bts_end.rtcp_port), inet_ntoa(addr->sin_addr));
709 }
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200710 } else if (proto == MGCP_PROTO_RTCP && endp->bts_end.rtcp_port == 0) {
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800711 if (memcmp(&endp->bts_end.addr, &addr->sin_addr,
712 sizeof(endp->bts_end.addr)) == 0) {
713 endp->bts_end.rtcp_port = addr->sin_port;
714 }
715 }
716}
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800717
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200718static int rtp_data_bts(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800719{
720 char buf[4096];
721 struct sockaddr_in addr;
722 struct mgcp_endpoint *endp;
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800723 int rc, proto;
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800724
725 endp = (struct mgcp_endpoint *) fd->data;
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800726
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200727 rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf));
Holger Hans Peter Freytherb6151642010-08-05 06:27:22 +0800728 if (rc <= 0)
729 return -1;
730
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200731 proto = fd == &endp->bts_end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100732
733 /* We have no idea who called us, maybe it is the BTS. */
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800734 /* it was the BTS... */
Holger Hans Peter Freyther52ccf6a2010-08-06 07:48:41 +0800735 discover_bts(endp, proto, &addr);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100736
Holger Hans Peter Freytherea97fbf2010-08-05 10:53:26 +0000737 if (memcmp(&endp->bts_end.addr, &addr.sin_addr, sizeof(addr.sin_addr)) != 0) {
738 LOGP(DMGCP, LOGL_ERROR,
739 "Data from wrong bts %s on 0x%x\n",
740 inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp));
741 return -1;
742 }
743
744 if (endp->bts_end.rtp_port != addr.sin_port &&
745 endp->bts_end.rtcp_port != addr.sin_port) {
746 LOGP(DMGCP, LOGL_ERROR,
747 "Data from wrong bts source port %d on 0x%x\n",
748 ntohs(addr.sin_port), ENDPOINT_NUMBER(endp));
749 return -1;
750 }
751
Holger Hans Peter Freytherb3c206a2010-05-14 02:18:59 +0800752 /* throw away the dummy message */
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200753 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800754 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from bts on 0x%x\n",
Holger Hans Peter Freytheraa9d3ce2010-04-22 12:14:51 +0800755 ENDPOINT_NUMBER(endp));
756 return 0;
757 }
758
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +0200759 /* do this before the loop handling */
Holger Hans Peter Freythere602cd62010-08-05 06:55:58 +0800760 endp->bts_end.packets += 1;
Holger Hans Peter Freyther952f7522012-09-12 11:38:37 +0200761 endp->bts_end.octets += rc;
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +0200762
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +0800763 forward_data(fd->fd, &endp->taps[MGCP_TAP_BTS_IN], buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200764
765 switch (endp->type) {
766 case MGCP_RTP_DEFAULT:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200767 return mgcp_send(endp, MGCP_DEST_NET, proto == MGCP_PROTO_RTP,
768 &addr, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200769 case MGCP_RTP_TRANSCODED:
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200770 return mgcp_send_transcoder(&endp->trans_bts, endp->cfg,
771 proto == MGCP_PROTO_RTP, buf, rc);
Pablo Neira Ayuso46bd4242013-07-08 05:09:46 +0200772 }
773
774 LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n",
775 endp->type, ENDPOINT_NUMBER(endp));
776 return 0;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800777}
778
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100779static int rtp_data_transcoder(struct mgcp_rtp_end *end, struct mgcp_endpoint *_endp,
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200780 int dest, struct osmo_fd *fd)
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800781{
782 char buf[4096];
783 struct sockaddr_in addr;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800784 struct mgcp_config *cfg;
785 int rc, proto;
786
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100787 cfg = _endp->cfg;
Holger Hans Peter Freyther9bdf5a92011-06-02 19:04:22 +0200788 rc = receive_from(_endp, fd->fd, &addr, buf, sizeof(buf));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800789 if (rc <= 0)
790 return -1;
791
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200792 proto = fd == &end->rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800793
794 if (memcmp(&addr.sin_addr, &cfg->transcoder_in, sizeof(addr.sin_addr)) != 0) {
795 LOGP(DMGCP, LOGL_ERROR,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100796 "Data not coming from transcoder dest: %d %s on 0x%x\n",
797 dest, inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(_endp));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800798 return -1;
799 }
800
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100801 if (end->rtp_port != addr.sin_port &&
802 end->rtcp_port != addr.sin_port) {
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800803 LOGP(DMGCP, LOGL_ERROR,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100804 "Data from wrong transcoder dest %d source port %d on 0x%x\n",
805 dest, ntohs(addr.sin_port), ENDPOINT_NUMBER(_endp));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800806 return -1;
807 }
808
809 /* throw away the dummy message */
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200810 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100811 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from transcoder dest %d on 0x%x\n",
812 dest, ENDPOINT_NUMBER(_endp));
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800813 return 0;
814 }
815
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100816 end->packets += 1;
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200817 return mgcp_send(_endp, dest, proto == MGCP_PROTO_RTP, &addr, buf, rc);
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100818}
819
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200820static int rtp_data_trans_net(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100821{
822 struct mgcp_endpoint *endp;
823 endp = (struct mgcp_endpoint *) fd->data;
824
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200825 return rtp_data_transcoder(&endp->trans_net, endp, MGCP_DEST_NET, fd);
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100826}
827
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200828static int rtp_data_trans_bts(struct osmo_fd *fd, unsigned int what)
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100829{
830 struct mgcp_endpoint *endp;
831 endp = (struct mgcp_endpoint *) fd->data;
832
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200833 return rtp_data_transcoder(&endp->trans_bts, endp, MGCP_DEST_BTS, fd);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100834}
835
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200836static int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd,
837 int port)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100838{
839 struct sockaddr_in addr;
840 int on = 1;
841
842 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
843 if (fd->fd < 0) {
844 LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n");
845 return -1;
846 }
847
848 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
849 memset(&addr, 0, sizeof(addr));
850 addr.sin_family = AF_INET;
851 addr.sin_port = htons(port);
852 inet_aton(source_addr, &addr.sin_addr);
853
854 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Holger Hans Peter Freyther6f36e922010-08-06 03:00:17 +0800855 close(fd->fd);
856 fd->fd = -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100857 return -1;
858 }
859
860 return 0;
861}
862
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800863static int set_ip_tos(int fd, int tos)
864{
865 int ret;
866 ret = setsockopt(fd, IPPROTO_IP, IP_TOS,
867 &tos, sizeof(tos));
868 return ret != 0;
869}
870
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800871static int bind_rtp(struct mgcp_config *cfg, struct mgcp_rtp_end *rtp_end, int endpno)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100872{
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200873 if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtp,
874 rtp_end->local_port) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100875 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800876 cfg->source_addr, rtp_end->local_port, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100877 goto cleanup0;
878 }
879
Pablo Neira Ayuso66b52c12013-08-14 15:55:45 +0200880 if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtcp,
881 rtp_end->local_port + 1) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100882 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800883 cfg->source_addr, rtp_end->local_port + 1, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100884 goto cleanup1;
885 }
886
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800887 set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
888 set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800889
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800890 rtp_end->rtp.when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200891 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100892 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800893 rtp_end->local_port, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100894 goto cleanup2;
895 }
896
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800897 rtp_end->rtcp.when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200898 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100899 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n",
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800900 rtp_end->local_port + 1, endpno);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100901 goto cleanup3;
902 }
903
904 return 0;
905
906cleanup3:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200907 osmo_fd_unregister(&rtp_end->rtp);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100908cleanup2:
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800909 close(rtp_end->rtcp.fd);
910 rtp_end->rtcp.fd = -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100911cleanup1:
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800912 close(rtp_end->rtp.fd);
913 rtp_end->rtp.fd = -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100914cleanup0:
915 return -1;
916}
917
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100918static int int_bind(const char *port,
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200919 struct mgcp_rtp_end *end, int (*cb)(struct osmo_fd *, unsigned),
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100920 struct mgcp_endpoint *_endp, int rtp_port)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100921{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100922 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
923 LOGP(DMGCP, LOGL_ERROR, "Previous %s was still bound on %d\n",
924 port, ENDPOINT_NUMBER(_endp));
925 mgcp_free_rtp_port(end);
Holger Hans Peter Freyther7fe2a3d2010-08-06 07:18:22 +0800926 }
927
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100928 end->local_port = rtp_port;
929 end->rtp.cb = cb;
930 end->rtp.data = _endp;
931 end->rtcp.data = _endp;
932 end->rtcp.cb = cb;
933 return bind_rtp(_endp->cfg, end, ENDPOINT_NUMBER(_endp));
934}
935
936
937int mgcp_bind_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
938{
939 return int_bind("bts-port", &endp->bts_end,
940 rtp_data_bts, endp, rtp_port);
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +0800941}
942
943int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
944{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100945 return int_bind("net-port", &endp->net_end,
946 rtp_data_net, endp, rtp_port);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100947}
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800948
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100949int mgcp_bind_trans_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800950{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100951 return int_bind("trans-net", &endp->trans_net,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100952 rtp_data_trans_net, endp, rtp_port);
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800953}
954
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100955int mgcp_bind_trans_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
956{
Holger Hans Peter Freyther386a9402010-11-01 21:15:22 +0100957 return int_bind("trans-bts", &endp->trans_bts,
Holger Hans Peter Freyther3f29cc82010-11-01 21:25:33 +0100958 rtp_data_trans_bts, endp, rtp_port);
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100959}
960
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800961int mgcp_free_rtp_port(struct mgcp_rtp_end *end)
962{
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800963 if (end->rtp.fd != -1) {
964 close(end->rtp.fd);
965 end->rtp.fd = -1;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200966 osmo_fd_unregister(&end->rtp);
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800967 }
968
969 if (end->rtcp.fd != -1) {
970 close(end->rtcp.fd);
971 end->rtcp.fd = -1;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200972 osmo_fd_unregister(&end->rtcp);
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800973 }
974
975 return 0;
976}
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +0200977
978
979void mgcp_state_calc_loss(struct mgcp_rtp_state *state,
980 struct mgcp_rtp_end *end, uint32_t *expected,
981 int *loss)
982{
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100983 *expected = state->cycles + state->out_stream.last_seq;
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +0200984 *expected = *expected - state->base_seq + 1;
985
986 if (!state->initialized) {
987 *expected = 0;
988 *loss = 0;
989 return;
990 }
991
992 /*
993 * Make sure the sign is correct and use the biggest
994 * positive/negative number that fits.
995 */
996 *loss = *expected - end->packets;
997 if (*expected < end->packets) {
998 if (*loss > 0)
999 *loss = INT_MIN;
1000 } else {
1001 if (*loss < 0)
1002 *loss = INT_MAX;
1003 }
1004}
Holger Hans Peter Freythercb306a62012-10-24 21:22:47 +02001005
1006uint32_t mgcp_state_calc_jitter(struct mgcp_rtp_state *state)
1007{
1008 if (!state->initialized)
1009 return 0;
1010 return state->jitter >> 4;
1011}