blob: 2056a8e47d3084c339289ac434a67b3788419118 [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* The protocol implementation */
3
4/*
5 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2012 by On-Waves
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <string.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <errno.h>
28#include <time.h>
29#include <limits.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020030#include <sys/socket.h>
31#include <arpa/inet.h>
32
33#include <osmocom/core/msgb.h>
34#include <osmocom/core/select.h>
Philipp Maier1cb1e382017-11-02 17:16:04 +010035#include <osmocom/core/socket.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020036#include <osmocom/netif/rtp.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020037#include <osmocom/mgcp/mgcp.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020038#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020039#include <osmocom/mgcp/mgcp_internal.h>
40#include <osmocom/mgcp/mgcp_stat.h>
41#include <osmocom/mgcp/osmux.h>
42#include <osmocom/mgcp/mgcp_conn.h>
43#include <osmocom/mgcp/mgcp_ep.h>
Philipp Maierc3413882017-10-27 12:26:54 +020044#include <osmocom/mgcp/debug.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020045
46#define RTP_SEQ_MOD (1 << 16)
47#define RTP_MAX_DROPOUT 3000
48#define RTP_MAX_MISORDER 100
49#define RTP_BUF_SIZE 4096
50
51enum {
52 MGCP_PROTO_RTP,
53 MGCP_PROTO_RTCP,
54};
55
Philipp Maier1cb1e382017-11-02 17:16:04 +010056/*! Determine the local rtp bind IP-address.
57 * \param[out] addr caller provided memory to store the resulting IP-Address
58 * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters
59 *
60 * The local bind IP-address is automatically selected by probing the
61 * IP-Address of the interface that is pointing towards the remote IP-Address,
62 * if no remote IP-Address is known yet, the statically configured
63 * IP-Addresses are used as fallback. */
64void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn)
65{
66
67 struct mgcp_endpoint *endp;
68 int rc;
69 endp = conn->conn->endp;
70
71 /* Try probing the local IP-Address */
72 if (endp->cfg->net_ports.bind_addr_probe && conn->end.addr.s_addr != 0) {
73 rc = osmo_sock_local_ip(addr, inet_ntoa(conn->end.addr));
74 if (rc < 0)
75 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +010076 "endpoint:0x%x CI:%s local interface auto detection failed, using configured addresses...\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +010077 ENDPOINT_NUMBER(endp), conn->conn->id);
78 else {
79 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +010080 "endpoint:0x%x CI:%s selected local rtp bind ip %s by probing using remote ip %s\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +010081 ENDPOINT_NUMBER(endp), conn->conn->id, addr,
82 inet_ntoa(conn->end.addr));
83 return;
84 }
85 }
86
87 /* Select from preconfigured IP-Addresses */
88 if (endp->cfg->net_ports.bind_addr) {
89 /* Check there is a bind IP for the RTP traffic configured,
90 * if so, use that IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +010091 osmo_strlcpy(addr, endp->cfg->net_ports.bind_addr, INET_ADDRSTRLEN);
Philipp Maier1cb1e382017-11-02 17:16:04 +010092 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +010093 "endpoint:0x%x CI:%s using configured rtp bind ip as local bind ip %s\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +010094 ENDPOINT_NUMBER(endp), conn->conn->id, addr);
95 } else {
96 /* No specific bind IP is configured for the RTP traffic, so
97 * assume the IP where we listen for incoming MGCP messages
98 * as bind IP */
Philipp Maierf8bfbe82017-11-23 19:32:31 +010099 osmo_strlcpy(addr, endp->cfg->source_addr, INET_ADDRSTRLEN);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100100 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100101 "endpoint:0x%x CI:%s using mgcp bind ip as local rtp bind ip: %s\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +0100102 ENDPOINT_NUMBER(endp), conn->conn->id, addr);
103 }
104}
105
Philipp Maier87bd9be2017-08-22 16:35:41 +0200106/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200107 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +0200108 * 1/codec_rate seconds. */
109static uint32_t get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200110{
111 struct timespec tp;
112 uint64_t ret;
113
Philipp Maier87bd9be2017-08-22 16:35:41 +0200114 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200115 return 0;
116
117 memset(&tp, 0, sizeof(tp));
118 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maierc3413882017-10-27 12:26:54 +0200119 LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200120
121 /* convert it to 1/unit seconds */
122 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200123 ret *= codec_rate;
124 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200125
126 return ret;
127}
128
Philipp Maier87bd9be2017-08-22 16:35:41 +0200129/*! send udp packet.
130 * \param[in] fd associated file descriptor
131 * \param[in] addr destination ip-address
132 * \param[in] port destination UDP port
133 * \param[in] buf buffer that holds the data to be send
134 * \param[in] len length of the data to be sent
135 * \returns bytes sent, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200136int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
137{
138 struct sockaddr_in out;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200139
Philipp Maierc3413882017-10-27 12:26:54 +0200140 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200141 "sending %i bytes length packet to %s:%u ...\n",
142 len, inet_ntoa(*addr), ntohs(port));
143
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200144 out.sin_family = AF_INET;
145 out.sin_port = port;
146 memcpy(&out.sin_addr, addr, sizeof(*addr));
147
148 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
149}
150
Philipp Maier87bd9be2017-08-22 16:35:41 +0200151/*! send RTP dummy packet (to keep NAT connection open).
152 * \param[in] endp mcgp endpoint that holds the RTP connection
153 * \param[in] conn associated RTP connection
154 * \returns bytes sent, -1 on error */
155int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200156{
157 static char buf[] = { MGCP_DUMMY_LOAD };
158 int rc;
159 int was_rtcp = 0;
160
Philipp Maier87bd9be2017-08-22 16:35:41 +0200161 OSMO_ASSERT(endp);
162 OSMO_ASSERT(conn);
163
Philipp Maierc3413882017-10-27 12:26:54 +0200164 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100165 "endpoint:0x%x sending dummy packet...\n", ENDPOINT_NUMBER(endp));
166 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200167 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
168
169 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
170 conn->end.rtp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200171
172 if (rc == -1)
173 goto failed;
174
175 if (endp->tcfg->omit_rtcp)
176 return rc;
177
178 was_rtcp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200179 rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
180 conn->end.rtcp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200181
182 if (rc >= 0)
183 return rc;
184
185failed:
Philipp Maierc3413882017-10-27 12:26:54 +0200186 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100187 "endpoint:0x%x Failed to send dummy %s packet.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200188 ENDPOINT_NUMBER(endp), was_rtcp ? "RTCP" : "RTP");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200189
190 return -1;
191}
192
Philipp Maier87bd9be2017-08-22 16:35:41 +0200193/* Compute timestamp alignment error */
194static int32_t ts_alignment_error(struct mgcp_rtp_stream_state *sstate,
195 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200196{
197 int32_t timestamp_delta;
198
199 if (ptime == 0)
200 return 0;
201
202 /* Align according to: T - Tlast = k * Tptime */
203 timestamp_delta = timestamp - sstate->last_timestamp;
204
205 return timestamp_delta % ptime;
206}
207
Philipp Maier87bd9be2017-08-22 16:35:41 +0200208/* Check timestamp and sequence number for plausibility */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200209static int check_rtp_timestamp(struct mgcp_endpoint *endp,
210 struct mgcp_rtp_state *state,
211 struct mgcp_rtp_stream_state *sstate,
212 struct mgcp_rtp_end *rtp_end,
213 struct sockaddr_in *addr,
214 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200215 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200216{
217 int32_t tsdelta;
218 int32_t timestamp_error;
219
220 /* Not fully intialized, skip */
221 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
222 return 0;
223
224 if (seq == sstate->last_seq) {
225 if (timestamp != sstate->last_timestamp) {
226 sstate->err_ts_counter += 1;
Philipp Maierc3413882017-10-27 12:26:54 +0200227 LOGP(DRTP, LOGL_ERROR,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200228 "The %s timestamp delta is != 0 but the sequence "
229 "number %d is the same, "
230 "TS offset: %d, SeqNo offset: %d "
231 "on 0x%x SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200232 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200233 text, seq,
Harald Welte33381352017-12-25 09:44:26 +0100234 state->patch.timestamp_offset, state->patch.seq_offset,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200235 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200236 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200237 }
238 return 0;
239 }
240
241 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200242 (int32_t)(timestamp - sstate->last_timestamp) /
243 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200244
245 if (tsdelta == 0) {
246 /* Don't update *tsdelta_out */
Philipp Maierc3413882017-10-27 12:26:54 +0200247 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200248 "The %s timestamp delta is %d "
249 "on 0x%x SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200250 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200251 text, tsdelta,
252 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200253 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200254
255 return 0;
256 }
257
258 if (sstate->last_tsdelta != tsdelta) {
259 if (sstate->last_tsdelta) {
Philipp Maierc3413882017-10-27 12:26:54 +0200260 LOGP(DRTP, LOGL_INFO,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200261 "The %s timestamp delta changes from %d to %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200262 "on 0x%x SSRC: %u timestamp: %u from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200263 text, sstate->last_tsdelta, tsdelta,
264 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200265 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200266 }
267 }
268
269 if (tsdelta_out)
270 *tsdelta_out = tsdelta;
271
272 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200273 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200274
275 if (timestamp_error) {
276 sstate->err_ts_counter += 1;
Philipp Maierc3413882017-10-27 12:26:54 +0200277 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200278 "The %s timestamp has an alignment error of %d "
279 "on 0x%x SSRC: %u "
280 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200281 "from %s:%d. ptime: %d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200282 text, timestamp_error,
283 ENDPOINT_NUMBER(endp), sstate->ssrc,
284 (int16_t)(seq - sstate->last_seq),
285 (int32_t)(timestamp - sstate->last_timestamp),
286 tsdelta,
287 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200288 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200289 }
290 return 1;
291}
292
293/* Set the timestamp offset according to the packet duration. */
294static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp,
295 struct mgcp_rtp_state *state,
296 struct mgcp_rtp_end *rtp_end,
297 struct sockaddr_in *addr,
298 int16_t delta_seq, uint32_t in_timestamp)
299{
300 int32_t tsdelta = state->packet_duration;
301 int timestamp_offset;
302 uint32_t out_timestamp;
303
304 if (tsdelta == 0) {
305 tsdelta = state->out_stream.last_tsdelta;
306 if (tsdelta != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200307 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200308 "A fixed packet duration is not available on 0x%x, "
309 "using last output timestamp delta instead: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200310 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200311 ENDPOINT_NUMBER(endp), tsdelta,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200312 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200313 } else {
314 tsdelta = rtp_end->codec.rate * 20 / 1000;
Philipp Maierc3413882017-10-27 12:26:54 +0200315 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200316 "Fixed packet duration and last timestamp delta "
317 "are not available on 0x%x, "
318 "using fixed 20ms instead: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200319 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200320 ENDPOINT_NUMBER(endp), tsdelta,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200321 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200322 }
323 }
324
325 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
326 timestamp_offset = out_timestamp - in_timestamp;
327
Harald Welte33381352017-12-25 09:44:26 +0100328 if (state->patch.timestamp_offset != timestamp_offset) {
329 state->patch.timestamp_offset = timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200330
Philipp Maierc3413882017-10-27 12:26:54 +0200331 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200332 "Timestamp offset change on 0x%x SSRC: %u "
333 "SeqNo delta: %d, TS offset: %d, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200334 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200335 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100336 delta_seq, state->patch.timestamp_offset,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200337 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200338 }
339
340 return timestamp_offset;
341}
342
343/* Set the timestamp offset according to the packet duration. */
344static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp,
345 struct mgcp_rtp_state *state,
346 struct mgcp_rtp_end *rtp_end,
347 struct sockaddr_in *addr,
348 uint32_t timestamp)
349{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200350 int ts_error = 0;
351 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200352 int ptime = state->packet_duration;
353
354 /* Align according to: T + Toffs - Tlast = k * Tptime */
355
Philipp Maier87bd9be2017-08-22 16:35:41 +0200356 ts_error = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100357 timestamp + state->patch.timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200358
Philipp Maier87bd9be2017-08-22 16:35:41 +0200359 /* If there is an alignment error, we have to compensate it */
360 if (ts_error) {
Harald Welte33381352017-12-25 09:44:26 +0100361 state->patch.timestamp_offset += ptime - ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200362
Philipp Maierc3413882017-10-27 12:26:54 +0200363 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200364 "Corrected timestamp alignment error of %d on 0x%x SSRC: %u "
365 "new TS offset: %d, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200366 "from %s:%d\n",
367 ts_error,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200368 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100369 state->patch.timestamp_offset, inet_ntoa(addr->sin_addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200370 ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200371 }
372
Philipp Maier87bd9be2017-08-22 16:35:41 +0200373 /* Check we really managed to compensate the timestamp
374 * offset. There should not be any remaining error, failing
375 * here would point to a serous problem with the alingnment
376 * error computation fuction */
377 ts_check = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100378 timestamp + state->patch.timestamp_offset);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200379 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200380
Philipp Maier87bd9be2017-08-22 16:35:41 +0200381 /* Return alignment error before compensation */
382 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200383}
384
Philipp Maier87bd9be2017-08-22 16:35:41 +0200385/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
386 * \param[in] associated endpoint
387 * \param[in] destination RTP end
388 * \param[in,out] pointer to buffer with voice data
389 * \param[in] voice data length
390 * \param[in] maxmimum size of caller provided voice data buffer
391 * \returns ignores input parameters, return always 0 */
392int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
393 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200394 char *data, int *len, int buf_size)
395{
Philipp Maier230e4fc2017-11-28 09:38:45 +0100396 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x transcoding disabled\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200397 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200398 return 0;
399}
400
Philipp Maier87bd9be2017-08-22 16:35:41 +0200401/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
402 * \param[in] associated endpoint
403 * \param[in] destination RTP end
404 * \param[in] source RTP end
405 * \returns ignores input parameters, return always 0 */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200406int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
407 struct mgcp_rtp_end *dst_end,
408 struct mgcp_rtp_end *src_end)
409{
Philipp Maier230e4fc2017-11-28 09:38:45 +0100410 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x transcoding disabled\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200411 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200412 return 0;
413}
414
415void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
416 int *payload_type,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200417 const char **audio_name,
418 const char **fmtp_extra,
419 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200420{
Philipp Maierc3413882017-10-27 12:26:54 +0200421 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100422 "endpoint:0x%x conn:%s using format defaults\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200423 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
424
425 *payload_type = conn->end.codec.payload_type;
426 *audio_name = conn->end.codec.audio_name;
427 *fmtp_extra = conn->end.fmtp_extra;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200428}
429
Philipp Maier87bd9be2017-08-22 16:35:41 +0200430void mgcp_rtp_annex_count(struct mgcp_endpoint *endp,
431 struct mgcp_rtp_state *state, const uint16_t seq,
432 const int32_t transit, const uint32_t ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200433{
434 int32_t d;
435
436 /* initialize or re-initialize */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100437 if (!state->stats.initialized || state->stats.ssrc != ssrc) {
438 state->stats.initialized = 1;
439 state->stats.base_seq = seq;
440 state->stats.max_seq = seq - 1;
441 state->stats.ssrc = ssrc;
442 state->stats.jitter = 0;
443 state->stats.transit = transit;
444 state->stats.cycles = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200445 } else {
446 uint16_t udelta;
447
Philipp Maier87bd9be2017-08-22 16:35:41 +0200448 /* The below takes the shape of the validation of
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200449 * Appendix A. Check if there is something weird with
450 * the sequence number, otherwise check for a wrap
451 * around in the sequence number.
452 * It can't wrap during the initialization so let's
453 * skip it here. The Appendix A probably doesn't have
Philipp Maier87bd9be2017-08-22 16:35:41 +0200454 * this issue because of the probation. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100455 udelta = seq - state->stats.max_seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200456 if (udelta < RTP_MAX_DROPOUT) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100457 if (seq < state->stats.max_seq)
458 state->stats.cycles += RTP_SEQ_MOD;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200459 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Philipp Maierc3413882017-10-27 12:26:54 +0200460 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200461 "RTP seqno made a very large jump on 0x%x delta: %u\n",
462 ENDPOINT_NUMBER(endp), udelta);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200463 }
464 }
465
Philipp Maier87bd9be2017-08-22 16:35:41 +0200466 /* Calculate the jitter between the two packages. The TS should be
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200467 * taken closer to the read function. This was taken from the
468 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
Philipp Maier87bd9be2017-08-22 16:35:41 +0200469 * resolution. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100470 d = transit - state->stats.transit;
471 state->stats.transit = transit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200472 if (d < 0)
473 d = -d;
Harald Welte49e3d5a2017-12-25 09:47:57 +0100474 state->stats.jitter += d - ((state->stats.jitter + 8) >> 4);
475 state->stats.max_seq = seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200476}
477
Philipp Maier87bd9be2017-08-22 16:35:41 +0200478/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200479 * some of the supported endpoints (e.g. the nanoBTS) can only handle
480 * one source and this code will patch RTP header to appear as if there
481 * is only one source.
482 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200483 * we receive will be seen as a switch in streams. */
484void mgcp_patch_and_count(struct mgcp_endpoint *endp,
485 struct mgcp_rtp_state *state,
486 struct mgcp_rtp_end *rtp_end,
487 struct sockaddr_in *addr, char *data, int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200488{
489 uint32_t arrival_time;
490 int32_t transit;
491 uint16_t seq;
492 uint32_t timestamp, ssrc;
493 struct rtp_hdr *rtp_hdr;
494 int payload = rtp_end->codec.payload_type;
495
496 if (len < sizeof(*rtp_hdr))
497 return;
498
Philipp Maier87bd9be2017-08-22 16:35:41 +0200499 rtp_hdr = (struct rtp_hdr *)data;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200500 seq = ntohs(rtp_hdr->sequence);
501 timestamp = ntohl(rtp_hdr->timestamp);
502 arrival_time = get_current_ts(rtp_end->codec.rate);
503 ssrc = ntohl(rtp_hdr->ssrc);
504 transit = arrival_time - timestamp;
505
506 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc);
507
508 if (!state->initialized) {
509 state->initialized = 1;
510 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100511 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200512 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200513 state->packet_duration =
514 mgcp_rtp_packet_duration(endp, rtp_end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200515 state->out_stream = state->in_stream;
516 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200517 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Philipp Maierc3413882017-10-27 12:26:54 +0200518 LOGP(DRTP, LOGL_INFO,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100519 "endpoint:0x%x initializing stream, SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200520 "pkt-duration: %d, from %s:%d\n",
521 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100522 state->patch.seq_offset, state->packet_duration,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200523 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200524 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200525 state->packet_duration =
526 rtp_end->codec.rate * 20 / 1000;
Philipp Maierc3413882017-10-27 12:26:54 +0200527 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100528 "endpoint:0x%x fixed packet duration is not available, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200529 "using fixed 20ms instead: %d from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200530 ENDPOINT_NUMBER(endp), state->packet_duration,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200531 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200532 }
533 } else if (state->in_stream.ssrc != ssrc) {
Philipp Maierc3413882017-10-27 12:26:54 +0200534 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100535 "endpoint:0x%x SSRC changed: %u -> %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200536 "from %s:%d\n",
537 ENDPOINT_NUMBER(endp),
538 state->in_stream.ssrc, rtp_hdr->ssrc,
539 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200540
541 state->in_stream.ssrc = ssrc;
542 if (rtp_end->force_constant_ssrc) {
543 int16_t delta_seq;
544
545 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100546 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200547 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200548
549 /* Estimate number of packets that would have been sent */
550 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200551 (arrival_time - state->in_stream.last_arrival_time
552 + state->packet_duration / 2) /
553 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200554
555 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
556 delta_seq, timestamp);
557
Harald Welte33381352017-12-25 09:44:26 +0100558 state->patch.patch_ssrc = 1;
559 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200560 if (rtp_end->force_constant_ssrc != -1)
561 rtp_end->force_constant_ssrc -= 1;
562
Philipp Maierc3413882017-10-27 12:26:54 +0200563 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100564 "endpoint:0x%x SSRC patching enabled, SSRC: %u "
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200565 "SeqNo offset: %d, TS offset: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200566 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200567 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100568 state->patch.seq_offset, state->patch.timestamp_offset,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200569 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200570 }
571
572 state->in_stream.last_tsdelta = 0;
573 } else {
574 /* Compute current per-packet timestamp delta */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200575 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
576 addr, seq, timestamp, "input",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200577 &state->in_stream.last_tsdelta);
578
Harald Welte33381352017-12-25 09:44:26 +0100579 if (state->patch.patch_ssrc)
580 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200581 }
582
583 /* Save before patching */
584 state->in_stream.last_timestamp = timestamp;
585 state->in_stream.last_seq = seq;
586 state->in_stream.last_arrival_time = arrival_time;
587
588 if (rtp_end->force_aligned_timing &&
589 state->out_stream.ssrc == ssrc && state->packet_duration)
590 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200591 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
592 timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200593
594 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100595 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200596 rtp_hdr->ssrc = htonl(ssrc);
597
598 /* Apply the offset and store it back to the packet.
599 * This won't change anything if the offset is 0, so the conditional is
600 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100601 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200602 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100603 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200604 rtp_hdr->timestamp = htonl(timestamp);
605
606 /* Check again, whether the timestamps are still valid */
607 if (state->out_stream.ssrc == ssrc)
608 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
609 addr, seq, timestamp, "output",
610 &state->out_stream.last_tsdelta);
611
612 /* Save output values */
613 state->out_stream.last_seq = seq;
614 state->out_stream.last_timestamp = timestamp;
615 state->out_stream.ssrc = ssrc;
616
617 if (payload < 0)
618 return;
619
620#if 0
Philipp Maierc3413882017-10-27 12:26:54 +0200621 DEBUGP(DRTP,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100622 "endpoint:0x%x payload hdr payload %u -> endp payload %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200623 ENDPOINT_NUMBER(endp), rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200624 rtp_hdr->payload_type = payload;
625#endif
626}
627
Philipp Maier87bd9be2017-08-22 16:35:41 +0200628/* Forward data to a debug tap. This is debug function that is intended for
629 * debugging the voice traffic with tools like gstreamer */
Philipp Maiere6f172d2017-11-07 12:00:01 +0100630static void forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf,
631 int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200632{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100633 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200634
Philipp Maiere6f172d2017-11-07 12:00:01 +0100635 if (!tap->enabled)
636 return;
637
638 rc = sendto(fd, buf, len, 0, (struct sockaddr *)&tap->forward,
639 sizeof(tap->forward));
640
641 if (rc < 0)
642 LOGP(DRTP, LOGL_ERROR,
643 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200644}
645
Philipp Maier87bd9be2017-08-22 16:35:41 +0200646/*! Send RTP/RTCP data to a specified destination connection.
647 * \param[in] endp associated endpoint (for configuration, logging)
648 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP
649 * \param[in] spoofed source address (set to NULL to disable)
650 * \param[in] buf buffer that contains the RTP/RTCP data
651 * \param[in] len length of the buffer that contains the RTP/RTCP data
652 * \param[in] conn_src associated source connection
653 * \param[in] conn_dst associated destination connection
654 * \returns 0 on success, -1 on ERROR */
655int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
656 char *buf, int len, struct mgcp_conn_rtp *conn_src,
657 struct mgcp_conn_rtp *conn_dst)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200658{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200659 /*! When no destination connection is available (e.g. when only one
660 * connection in loopback mode exists), then the source connection
661 * shall be specified as destination connection */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200662
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200663 struct mgcp_trunk_config *tcfg = endp->tcfg;
664 struct mgcp_rtp_end *rtp_end;
665 struct mgcp_rtp_state *rtp_state;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200666 char *dest_name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200667
Philipp Maier87bd9be2017-08-22 16:35:41 +0200668 OSMO_ASSERT(conn_src);
669 OSMO_ASSERT(conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200670
Philipp Maier87bd9be2017-08-22 16:35:41 +0200671 if (is_rtp) {
Philipp Maierc3413882017-10-27 12:26:54 +0200672 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100673 "endpoint:0x%x delivering RTP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200674 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200675 } else {
Philipp Maierc3413882017-10-27 12:26:54 +0200676 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100677 "endpoint:0x%x delivering RTCP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200678 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200679 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200680
Philipp Maierc3413882017-10-27 12:26:54 +0200681 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100682 "endpoint:0x%x loop:%d, mode:%d ",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200683 ENDPOINT_NUMBER(endp), tcfg->audio_loop, conn_src->conn->mode);
684 if (conn_src->conn->mode == MGCP_CONN_LOOPBACK)
Philipp Maierc3413882017-10-27 12:26:54 +0200685 LOGPC(DRTP, LOGL_DEBUG, "(loopback)\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200686 else
Philipp Maierc3413882017-10-27 12:26:54 +0200687 LOGPC(DRTP, LOGL_DEBUG, "\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200688
Philipp Maier87bd9be2017-08-22 16:35:41 +0200689 /* Note: In case of loopback configuration, both, the source and the
690 * destination will point to the same connection. */
691 rtp_end = &conn_dst->end;
692 rtp_state = &conn_src->state;
693 dest_name = conn_dst->conn->name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200694
695 if (!rtp_end->output_enabled) {
696 rtp_end->dropped_packets += 1;
Philipp Maierc3413882017-10-27 12:26:54 +0200697 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100698 "endpoint:0x%x output disabled, drop to %s %s "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200699 "rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200700 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200701 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200702 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200703 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200704 );
705 } else if (is_rtp) {
706 int cont;
707 int nbytes = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200708 int buflen = len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200709 do {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200710 /* Run transcoder */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200711 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200712 buf, &buflen,
713 RTP_BUF_SIZE);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200714 if (cont < 0)
715 break;
716
Philipp Maier87bd9be2017-08-22 16:35:41 +0200717 if (addr)
718 mgcp_patch_and_count(endp, rtp_state, rtp_end,
719 addr, buf, buflen);
Philipp Maierc3413882017-10-27 12:26:54 +0200720 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100721 "endpoint:0x%x process/send to %s %s "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200722 "rtp_port:%u rtcp_port:%u\n",
723 ENDPOINT_NUMBER(endp), dest_name,
724 inet_ntoa(rtp_end->addr), ntohs(rtp_end->rtp_port),
725 ntohs(rtp_end->rtcp_port)
726 );
727
728 /* Forward a copy of the RTP data to a debug ip/port */
729 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
730 buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200731
732 /* FIXME: HACK HACK HACK. See OS#2459.
733 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
734 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
735 * cells (as long as we patch only the first RTP payload in each stream).
736 */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200737 if (!rtp_state->patched_first_rtp_payload) {
738 uint8_t *data = (uint8_t *) & buf[12];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200739 data[0] = 0xe4;
740 data[1] = 0x00;
741 rtp_state->patched_first_rtp_payload = true;
742 }
743
Philipp Maier87bd9be2017-08-22 16:35:41 +0200744 len = mgcp_udp_send(rtp_end->rtp.fd,
745 &rtp_end->addr,
746 rtp_end->rtp_port, buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200747
Philipp Maier87bd9be2017-08-22 16:35:41 +0200748 if (len <= 0)
749 return len;
750
751 conn_dst->end.packets_tx += 1;
752 conn_dst->end.octets_tx += len;
753
754 nbytes += len;
755 buflen = cont;
756 } while (buflen > 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200757 return nbytes;
758 } else if (!tcfg->omit_rtcp) {
Philipp Maierc3413882017-10-27 12:26:54 +0200759 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100760 "endpoint:0x%x send to %s %s rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200761 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200762 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200763 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200764 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200765 );
766
Philipp Maier87bd9be2017-08-22 16:35:41 +0200767 len = mgcp_udp_send(rtp_end->rtcp.fd,
768 &rtp_end->addr,
769 rtp_end->rtcp_port, buf, len);
770
771 conn_dst->end.packets_tx += 1;
772 conn_dst->end.octets_tx += len;
773
774 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200775 }
776
777 return 0;
778}
779
Philipp Maier87bd9be2017-08-22 16:35:41 +0200780/* Helper function for mgcp_recv(),
781 Receive one RTP Packet + Originating address from file descriptor */
782static int receive_from(struct mgcp_endpoint *endp, int fd,
783 struct sockaddr_in *addr, char *buf, int bufsize)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200784{
785 int rc;
786 socklen_t slen = sizeof(*addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200787 struct sockaddr_in addr_sink;
788 char buf_sink[RTP_BUF_SIZE];
789 bool tossed = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200790
Philipp Maier87bd9be2017-08-22 16:35:41 +0200791 if (!addr)
792 addr = &addr_sink;
793 if (!buf) {
794 tossed = true;
795 buf = buf_sink;
796 bufsize = sizeof(buf_sink);
797 }
798
799 rc = recvfrom(fd, buf, bufsize, 0, (struct sockaddr *)addr, &slen);
800
Philipp Maierc3413882017-10-27 12:26:54 +0200801 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200802 "receiving %u bytes length packet from %s:%u ...\n",
803 rc, inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
804
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200805 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200806 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100807 "endpoint:0x%x failed to receive packet, errno: %d/%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200808 ENDPOINT_NUMBER(endp), errno, strerror(errno));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200809 return -1;
810 }
811
Philipp Maier87bd9be2017-08-22 16:35:41 +0200812 if (tossed) {
Philipp Maier230e4fc2017-11-28 09:38:45 +0100813 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200814 ENDPOINT_NUMBER(endp));
815 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200816
817 return rc;
818}
819
Philipp Maier87bd9be2017-08-22 16:35:41 +0200820/* Check if the origin (addr) matches the address/port data of the RTP
821 * connections. */
822static int check_rtp_origin(struct mgcp_conn_rtp *conn,
823 struct sockaddr_in *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200824{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200825 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200826 endp = conn->conn->endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200827
Philipp Maier87bd9be2017-08-22 16:35:41 +0200828 /* Note: Check if the inbound RTP data comes from the same host to
829 * which we send our outgoing RTP traffic. */
830 if (memcmp(&addr->sin_addr, &conn->end.addr, sizeof(addr->sin_addr))
831 != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200832 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100833 "endpoint:0x%x data from wrong address: %s, ",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200834 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr));
Philipp Maierc3413882017-10-27 12:26:54 +0200835 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200836 inet_ntoa(conn->end.addr));
Philipp Maier230e4fc2017-11-28 09:38:45 +0100837 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200838 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200839 return -1;
840 }
841
Philipp Maier87bd9be2017-08-22 16:35:41 +0200842 /* Note: Usually the remote remote port of the data we receive will be
843 * the same as the remote port where we transmit outgoing RTP traffic
844 * to (set by MDCX). We use this to check the origin of the data for
845 * plausibility. */
846 if (conn->end.rtp_port != addr->sin_port &&
847 conn->end.rtcp_port != addr->sin_port) {
Philipp Maierc3413882017-10-27 12:26:54 +0200848 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100849 "endpoint:0x%x data from wrong source port: %d, ",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200850 ENDPOINT_NUMBER(endp), ntohs(addr->sin_port));
Philipp Maierc3413882017-10-27 12:26:54 +0200851 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200852 "expected: %d for RTP or %d for RTCP\n",
853 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Philipp Maier230e4fc2017-11-28 09:38:45 +0100854 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200855 ENDPOINT_NUMBER(endp));
856 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200857 }
858
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200859 return 0;
860}
861
Philipp Maier87bd9be2017-08-22 16:35:41 +0200862/* Check the if the destination address configuration of an RTP connection
863 * makes sense */
864static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200865{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200866 struct mgcp_endpoint *endp;
867 endp = conn->conn->endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200868
Philipp Maier87bd9be2017-08-22 16:35:41 +0200869 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200870 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100871 "endpoint:0x%x destination IP-address is invalid\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200872 ENDPOINT_NUMBER(endp));
873 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200874 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200875
876 if (conn->end.rtp_port == 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200877 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100878 "endpoint:0x%x destination rtp port is invalid\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200879 ENDPOINT_NUMBER(endp));
880 return -1;
881 }
882
883 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200884}
885
Philipp Maier87bd9be2017-08-22 16:35:41 +0200886/* Receive RTP data from a specified source connection and dispatch it to a
887 * destination connection. */
888static int mgcp_recv(int *proto, struct sockaddr_in *addr, char *buf,
889 unsigned int buf_size, struct osmo_fd *fd)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200890{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200891 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200892 struct mgcp_conn_rtp *conn;
893 struct mgcp_trunk_config *tcfg;
894 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200895
Philipp Maier87bd9be2017-08-22 16:35:41 +0200896 conn = (struct mgcp_conn_rtp*) fd->data;
897 endp = conn->conn->endp;
898 tcfg = endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200899
Philipp Maier230e4fc2017-11-28 09:38:45 +0100900 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x receiving RTP/RTCP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200901 ENDPOINT_NUMBER(endp));
902
903 rc = receive_from(endp, fd->fd, addr, buf, buf_size);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200904 if (rc <= 0)
905 return -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200906 *proto = fd == &conn->end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200907
Philipp Maier230e4fc2017-11-28 09:38:45 +0100908 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x ", ENDPOINT_NUMBER(endp));
Philipp Maierc3413882017-10-27 12:26:54 +0200909 LOGPC(DRTP, LOGL_DEBUG, "receiveing from %s %s %d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200910 conn->conn->name, inet_ntoa(addr->sin_addr),
911 ntohs(addr->sin_port));
Philipp Maier230e4fc2017-11-28 09:38:45 +0100912 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x conn:%s\n", ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200913 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200914
Philipp Maier87bd9be2017-08-22 16:35:41 +0200915 /* Check if the origin of the RTP packet seems plausible */
916 if (tcfg->rtp_accept_all == 0) {
917 if (check_rtp_origin(conn, addr) != 0)
918 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200919 }
920
Philipp Maier87bd9be2017-08-22 16:35:41 +0200921 /* Filter out dummy message */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200922 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Philipp Maierc3413882017-10-27 12:26:54 +0200923 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100924 "endpoint:0x%x dummy message received\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200925 ENDPOINT_NUMBER(endp));
Philipp Maierc3413882017-10-27 12:26:54 +0200926 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100927 "endpoint:0x%x packet tossed\n", ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200928 return 0;
929 }
930
Philipp Maier87bd9be2017-08-22 16:35:41 +0200931 /* Increment RX statistics */
932 conn->end.packets_rx += 1;
933 conn->end.octets_rx += rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200934
Philipp Maier87bd9be2017-08-22 16:35:41 +0200935 /* Forward a copy of the RTP data to a debug ip/port */
936 forward_data(fd->fd, &conn->tap_in, buf, rc);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200937
Philipp Maier87bd9be2017-08-22 16:35:41 +0200938 return rc;
939}
940
941/* Send RTP data. Possible options are standard RTP packet
942 * transmission or trsmission via an osmux connection */
943static int mgcp_send_rtp(int proto, struct sockaddr_in *addr, char *buf,
944 unsigned int buf_size,
945 struct mgcp_conn_rtp *conn_src,
946 struct mgcp_conn_rtp *conn_dst)
947{
948 struct mgcp_endpoint *endp;
949 endp = conn_src->conn->endp;
950
Philipp Maier230e4fc2017-11-28 09:38:45 +0100951 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x destin conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200952 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_dst->conn));
953
954 /* Before we try to deliver the packet, we check if the destination
955 * port and IP-Address make sense at all. If not, we will be unable
956 * to deliver the packet. */
957 if (check_rtp_destin(conn_dst) != 0)
958 return -1;
959
960 /* Depending on the RTP connection type, deliver the RTP packet to the
961 * destination connection. */
962 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200963 case MGCP_RTP_DEFAULT:
Philipp Maierc3413882017-10-27 12:26:54 +0200964 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100965 "endpoint:0x%x endpoint type is MGCP_RTP_DEFAULT, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200966 "using mgcp_send() to forward data directly\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200967 ENDPOINT_NUMBER(endp));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200968 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
969 addr, buf, buf_size, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200970 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200971 case MGCP_OSMUX_BSC:
Philipp Maierc3413882017-10-27 12:26:54 +0200972 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100973 "endpoint:0x%x endpoint type is MGCP_OSMUX_BSC_NAT, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200974 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n",
975 ENDPOINT_NUMBER(endp));
976 return osmux_xfrm_to_osmux(buf, buf_size, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200977 }
978
Philipp Maier87bd9be2017-08-22 16:35:41 +0200979 /* If the data has not been handled/forwarded until here, it will
980 * be discarded, this should not happen, normally the MGCP type
981 * should be properly set */
Philipp Maierc3413882017-10-27 12:26:54 +0200982 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100983 "endpoint:0x%x bad MGCP type -- data discarded!\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200984 ENDPOINT_NUMBER(endp));
985
986 return -1;
987}
988
989/*! dispatch incoming RTP packet to opposite RTP connection.
990 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP)
991 * \param[in] addr socket address where the RTP packet has been received from
992 * \param[in] buf buffer that hold the RTP payload
993 * \param[in] buf_size size data length of buf
994 * \param[in] conn originating connection
995 * \returns 0 on success, -1 on ERROR */
996int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf,
997 unsigned int buf_size, struct mgcp_conn *conn)
998{
999 struct mgcp_conn *conn_dst;
1000 struct mgcp_endpoint *endp;
1001 endp = conn->endp;
1002
1003 /*! NOTE: This callback function implements the endpoint specific
1004 * dispatch bahviour of an rtp bridge/proxy endpoint. It is assumed
1005 * that the endpoint will hold only two connections. This premise
1006 * is used to determine the opposite connection (it is always the
1007 * connection that is not the originating connection). Once the
1008 * destination connection is known the RTP packet is sent via
1009 * the destination connection. */
1010
1011 /* Find a destination connection. */
1012 /* NOTE: This code path runs every time an RTP packet is received. The
1013 * function mgcp_find_dst_conn() we use to determine the detination
1014 * connection will iterate the connection list inside the endpoint.
1015 * Since list iterations are quite costly, we will figure out the
1016 * destination only once and use the optional private data pointer of
1017 * the connection to cache the destination connection pointer. */
1018 if (!conn->priv) {
1019 conn_dst = mgcp_find_dst_conn(conn);
1020 conn->priv = conn_dst;
1021 } else {
1022 conn_dst = (struct mgcp_conn *)conn->priv;
1023 }
1024
1025 /* There is no destination conn, stop here */
1026 if (!conn_dst) {
Philipp Maierc3413882017-10-27 12:26:54 +02001027 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001028 "endpoint:0x%x unable to find destination conn\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001029 ENDPOINT_NUMBER(endp));
1030 return -1;
1031 }
1032
1033 /* The destination conn is not an RTP connection */
1034 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Philipp Maierc3413882017-10-27 12:26:54 +02001035 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001036 "endpoint:0x%x unable to find suitable destination conn\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001037 ENDPOINT_NUMBER(endp));
1038 return -1;
1039 }
1040
1041 /* Dispatch RTP packet to destination RTP connection */
1042 return mgcp_send_rtp(proto, addr, buf,
1043 buf_size, &conn->u.rtp, &conn_dst->u.rtp);
1044
1045}
1046
1047/* Handle incoming RTP data from NET */
1048static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1049{
1050 /* NOTE: This is a generic implementation. RTP data is received. In
1051 * case of loopback the data is just sent back to its origin. All
1052 * other cases implement endpoint specific behaviour (e.g. how is the
1053 * destination connection determined?). That specific behaviour is
1054 * implemented by the callback function that is called at the end of
1055 * the function */
1056
1057 struct mgcp_conn_rtp *conn_src;
1058 struct mgcp_endpoint *endp;
1059 struct sockaddr_in addr;
1060
1061 char buf[RTP_BUF_SIZE];
1062 int proto;
Philipp Maierb9694552017-11-08 16:53:57 +01001063 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001064
1065 conn_src = (struct mgcp_conn_rtp *)fd->data;
1066 OSMO_ASSERT(conn_src);
1067 endp = conn_src->conn->endp;
1068 OSMO_ASSERT(endp);
1069
Philipp Maier230e4fc2017-11-28 09:38:45 +01001070 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x source conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001071 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_src->conn));
1072
1073 /* Receive packet */
Philipp Maierb9694552017-11-08 16:53:57 +01001074 len = mgcp_recv(&proto, &addr, buf, sizeof(buf), fd);
1075 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001076 return -1;
1077
1078 /* Check if the connection is in loopback mode, if yes, just send the
1079 * incoming data back to the origin */
Philipp Maier5dbfc782017-12-12 16:20:25 +01001080
Philipp Maier87bd9be2017-08-22 16:35:41 +02001081 if (conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Philipp Maier5dbfc782017-12-12 16:20:25 +01001082 /* When we are in loopback mode, we loop back all incoming
1083 * packets back to their origin. We will use the originating
1084 * address data from the UDP packet header to patch the
1085 * outgoing address in connection on the fly */
1086 if (conn_src->end.rtp_port == 0) {
1087 conn_src->end.addr = addr.sin_addr;
1088 conn_src->end.rtp_port = addr.sin_port;
1089 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001090 return mgcp_send_rtp(proto, &addr, buf,
Philipp Maierb9694552017-11-08 16:53:57 +01001091 len, conn_src, conn_src);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001092 }
1093
1094 /* Execute endpoint specific implementation that handles the
1095 * dispatching of the RTP data */
Philipp Maierb9694552017-11-08 16:53:57 +01001096 return endp->type->dispatch_rtp_cb(proto, &addr, buf, len,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001097 conn_src->conn);
1098}
1099
1100/*! set IP Type of Service parameter.
1101 * \param[in] fd associated file descriptor
1102 * \param[in] tos dscp value
1103 * \returns 0 on success, -1 on ERROR */
1104int mgcp_set_ip_tos(int fd, int tos)
1105{
1106 int ret;
1107 ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1108
1109 if (ret < 0)
1110 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001111 return 0;
1112}
1113
Philipp Maier87bd9be2017-08-22 16:35:41 +02001114/*! bind RTP port to osmo_fd.
1115 * \param[in] source_addr source (local) address to bind on
1116 * \param[in] fd associated file descriptor
1117 * \param[in] port to bind on
1118 * \returns 0 on success, -1 on ERROR */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001119int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
1120{
1121 struct sockaddr_in addr;
1122 int on = 1;
1123
1124 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
1125 if (fd->fd < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001126 LOGP(DRTP, LOGL_ERROR, "failed to create UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001127 source_addr, port);
1128 return -1;
1129 } else {
Philipp Maierc3413882017-10-27 12:26:54 +02001130 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001131 "created UDP port (%s:%i).\n", source_addr, port);
1132 }
1133
1134 if (setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001135 LOGP(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001136 "failed to set socket options (%s:%i).\n", source_addr,
1137 port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001138 return -1;
1139 }
1140
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001141 memset(&addr, 0, sizeof(addr));
1142 addr.sin_family = AF_INET;
1143 addr.sin_port = htons(port);
1144 inet_aton(source_addr, &addr.sin_addr);
1145
Philipp Maier87bd9be2017-08-22 16:35:41 +02001146 if (bind(fd->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001147 close(fd->fd);
1148 fd->fd = -1;
Philipp Maierc3413882017-10-27 12:26:54 +02001149 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001150 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001151 return -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001152 } else {
Philipp Maierc3413882017-10-27 12:26:54 +02001153 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001154 "bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001155 }
1156
1157 return 0;
1158}
1159
Philipp Maier87bd9be2017-08-22 16:35:41 +02001160/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001161static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001162 struct mgcp_rtp_end *rtp_end, int endpno)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001163{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001164 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1165 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1166
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001167 if (mgcp_create_bind(source_addr, &rtp_end->rtp,
1168 rtp_end->local_port) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001169 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001170 "endpoint:0x%x failed to create RTP port: %s:%d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001171 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001172 goto cleanup0;
1173 }
1174
1175 if (mgcp_create_bind(source_addr, &rtp_end->rtcp,
1176 rtp_end->local_port + 1) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001177 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001178 "endpoint:0x%x failed to create RTCP port: %s:%d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001179 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001180 goto cleanup1;
1181 }
1182
Philipp Maier87bd9be2017-08-22 16:35:41 +02001183 /* Set Type of Service (DSCP-Value) as configured via VTY */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001184 mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
1185 mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
1186
1187 rtp_end->rtp.when = BSC_FD_READ;
1188 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001189 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001190 "endpoint:0x%x failed to register RTP port %d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001191 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001192 goto cleanup2;
1193 }
1194
1195 rtp_end->rtcp.when = BSC_FD_READ;
1196 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001197 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001198 "endpoint:0x%x failed to register RTCP port %d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001199 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001200 goto cleanup3;
1201 }
1202
1203 return 0;
1204
1205cleanup3:
1206 osmo_fd_unregister(&rtp_end->rtp);
1207cleanup2:
1208 close(rtp_end->rtcp.fd);
1209 rtp_end->rtcp.fd = -1;
1210cleanup1:
1211 close(rtp_end->rtp.fd);
1212 rtp_end->rtp.fd = -1;
1213cleanup0:
1214 return -1;
1215}
1216
Philipp Maier87bd9be2017-08-22 16:35:41 +02001217/*! bind RTP port to endpoint/connection.
1218 * \param[in] endp endpoint that holds the RTP connection
1219 * \param[in] rtp_port port number to bind on
1220 * \param[in] conn associated RTP connection
1221 * \returns 0 on success, -1 on ERROR */
1222int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1223 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001224{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001225 char name[512];
1226 struct mgcp_rtp_end *end;
Philipp Maier1cb1e382017-11-02 17:16:04 +01001227 char local_ip_addr[INET_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001228
Philipp Maier01d24a32017-11-21 17:26:09 +01001229 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001230 end = &conn->end;
1231
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001232 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Philipp Maierc3413882017-10-27 12:26:54 +02001233 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001234 "endpoint:0x%x %u was already bound on conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001235 ENDPOINT_NUMBER(endp), rtp_port,
1236 mgcp_conn_dump(conn->conn));
1237
1238 /* Double bindings should never occour! Since we always allocate
1239 * connections dynamically and free them when they are not
1240 * needed anymore, there must be no previous binding leftover.
1241 * Should there be a connection bound twice, we have a serious
1242 * problem and must exit immediately! */
1243 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001244 }
1245
1246 end->local_port = rtp_port;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001247 end->rtp.cb = rtp_data_net;
1248 end->rtp.data = conn;
1249 end->rtcp.data = conn;
1250 end->rtcp.cb = rtp_data_net;
1251
Philipp Maier1cb1e382017-11-02 17:16:04 +01001252 mgcp_get_local_addr(local_ip_addr, conn);
1253
1254 return bind_rtp(endp->cfg, local_ip_addr, end,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001255 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001256}
1257
Philipp Maier87bd9be2017-08-22 16:35:41 +02001258/*! free allocated RTP and RTCP ports.
1259 * \param[in] end RTP end */
1260void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001261{
1262 if (end->rtp.fd != -1) {
1263 close(end->rtp.fd);
1264 end->rtp.fd = -1;
1265 osmo_fd_unregister(&end->rtp);
1266 }
1267
1268 if (end->rtcp.fd != -1) {
1269 close(end->rtcp.fd);
1270 end->rtcp.fd = -1;
1271 osmo_fd_unregister(&end->rtcp);
1272 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001273}