blob: 6923b97f786e9ae663a1c8f79b330376bb92e556 [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 <arpa/inet.h>
31
32#include <osmocom/core/msgb.h>
33#include <osmocom/core/select.h>
Philipp Maier1cb1e382017-11-02 17:16:04 +010034#include <osmocom/core/socket.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020035#include <osmocom/netif/rtp.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020036#include <osmocom/mgcp/mgcp.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020037#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020038#include <osmocom/mgcp/mgcp_internal.h>
39#include <osmocom/mgcp/mgcp_stat.h>
40#include <osmocom/mgcp/osmux.h>
41#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010042#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maierc3413882017-10-27 12:26:54 +020043#include <osmocom/mgcp/debug.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020044
45#define RTP_SEQ_MOD (1 << 16)
46#define RTP_MAX_DROPOUT 3000
47#define RTP_MAX_MISORDER 100
48#define RTP_BUF_SIZE 4096
49
50enum {
51 MGCP_PROTO_RTP,
52 MGCP_PROTO_RTCP,
53};
54
Philipp Maier1cb1e382017-11-02 17:16:04 +010055/*! Determine the local rtp bind IP-address.
56 * \param[out] addr caller provided memory to store the resulting IP-Address
57 * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters
58 *
59 * The local bind IP-address is automatically selected by probing the
60 * IP-Address of the interface that is pointing towards the remote IP-Address,
61 * if no remote IP-Address is known yet, the statically configured
62 * IP-Addresses are used as fallback. */
63void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn)
64{
65
66 struct mgcp_endpoint *endp;
67 int rc;
68 endp = conn->conn->endp;
69
70 /* Try probing the local IP-Address */
71 if (endp->cfg->net_ports.bind_addr_probe && conn->end.addr.s_addr != 0) {
72 rc = osmo_sock_local_ip(addr, inet_ntoa(conn->end.addr));
73 if (rc < 0)
74 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +010075 "endpoint:0x%x CI:%s local interface auto detection failed, using configured addresses...\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +010076 ENDPOINT_NUMBER(endp), conn->conn->id);
77 else {
78 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +010079 "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 +010080 ENDPOINT_NUMBER(endp), conn->conn->id, addr,
81 inet_ntoa(conn->end.addr));
82 return;
83 }
84 }
85
86 /* Select from preconfigured IP-Addresses */
87 if (endp->cfg->net_ports.bind_addr) {
88 /* Check there is a bind IP for the RTP traffic configured,
89 * if so, use that IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +010090 osmo_strlcpy(addr, endp->cfg->net_ports.bind_addr, INET_ADDRSTRLEN);
Philipp Maier1cb1e382017-11-02 17:16:04 +010091 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +010092 "endpoint:0x%x CI:%s using configured rtp bind ip as local bind ip %s\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +010093 ENDPOINT_NUMBER(endp), conn->conn->id, addr);
94 } else {
95 /* No specific bind IP is configured for the RTP traffic, so
96 * assume the IP where we listen for incoming MGCP messages
97 * as bind IP */
Philipp Maierf8bfbe82017-11-23 19:32:31 +010098 osmo_strlcpy(addr, endp->cfg->source_addr, INET_ADDRSTRLEN);
Philipp Maier1cb1e382017-11-02 17:16:04 +010099 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100100 "endpoint:0x%x CI:%s using mgcp bind ip as local rtp bind ip: %s\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +0100101 ENDPOINT_NUMBER(endp), conn->conn->id, addr);
102 }
103}
104
Philipp Maier87bd9be2017-08-22 16:35:41 +0200105/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200106 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +0200107 * 1/codec_rate seconds. */
108static uint32_t get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200109{
110 struct timespec tp;
111 uint64_t ret;
112
Philipp Maier87bd9be2017-08-22 16:35:41 +0200113 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200114 return 0;
115
116 memset(&tp, 0, sizeof(tp));
117 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maierc3413882017-10-27 12:26:54 +0200118 LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200119
120 /* convert it to 1/unit seconds */
121 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200122 ret *= codec_rate;
123 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200124
125 return ret;
126}
127
Philipp Maier87bd9be2017-08-22 16:35:41 +0200128/*! send udp packet.
129 * \param[in] fd associated file descriptor
130 * \param[in] addr destination ip-address
131 * \param[in] port destination UDP port
132 * \param[in] buf buffer that holds the data to be send
133 * \param[in] len length of the data to be sent
134 * \returns bytes sent, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200135int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
136{
137 struct sockaddr_in out;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200138
Philipp Maierc3413882017-10-27 12:26:54 +0200139 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200140 "sending %i bytes length packet to %s:%u ...\n",
141 len, inet_ntoa(*addr), ntohs(port));
142
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200143 out.sin_family = AF_INET;
144 out.sin_port = port;
145 memcpy(&out.sin_addr, addr, sizeof(*addr));
146
147 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
148}
149
Philipp Maier87bd9be2017-08-22 16:35:41 +0200150/*! send RTP dummy packet (to keep NAT connection open).
151 * \param[in] endp mcgp endpoint that holds the RTP connection
152 * \param[in] conn associated RTP connection
153 * \returns bytes sent, -1 on error */
154int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200155{
156 static char buf[] = { MGCP_DUMMY_LOAD };
157 int rc;
158 int was_rtcp = 0;
159
Philipp Maier87bd9be2017-08-22 16:35:41 +0200160 OSMO_ASSERT(endp);
161 OSMO_ASSERT(conn);
162
Philipp Maierc3413882017-10-27 12:26:54 +0200163 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100164 "endpoint:0x%x sending dummy packet...\n", ENDPOINT_NUMBER(endp));
165 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200166 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
167
168 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
169 conn->end.rtp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200170
171 if (rc == -1)
172 goto failed;
173
174 if (endp->tcfg->omit_rtcp)
175 return rc;
176
177 was_rtcp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200178 rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
179 conn->end.rtcp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200180
181 if (rc >= 0)
182 return rc;
183
184failed:
Philipp Maierc3413882017-10-27 12:26:54 +0200185 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100186 "endpoint:0x%x Failed to send dummy %s packet.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200187 ENDPOINT_NUMBER(endp), was_rtcp ? "RTCP" : "RTP");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200188
189 return -1;
190}
191
Philipp Maier87bd9be2017-08-22 16:35:41 +0200192/* Compute timestamp alignment error */
193static int32_t ts_alignment_error(struct mgcp_rtp_stream_state *sstate,
194 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200195{
196 int32_t timestamp_delta;
197
198 if (ptime == 0)
199 return 0;
200
201 /* Align according to: T - Tlast = k * Tptime */
202 timestamp_delta = timestamp - sstate->last_timestamp;
203
204 return timestamp_delta % ptime;
205}
206
Philipp Maier87bd9be2017-08-22 16:35:41 +0200207/* Check timestamp and sequence number for plausibility */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200208static int check_rtp_timestamp(struct mgcp_endpoint *endp,
209 struct mgcp_rtp_state *state,
210 struct mgcp_rtp_stream_state *sstate,
211 struct mgcp_rtp_end *rtp_end,
212 struct sockaddr_in *addr,
213 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200214 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200215{
216 int32_t tsdelta;
217 int32_t timestamp_error;
218
219 /* Not fully intialized, skip */
220 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
221 return 0;
222
223 if (seq == sstate->last_seq) {
224 if (timestamp != sstate->last_timestamp) {
225 sstate->err_ts_counter += 1;
Philipp Maierc3413882017-10-27 12:26:54 +0200226 LOGP(DRTP, LOGL_ERROR,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200227 "The %s timestamp delta is != 0 but the sequence "
228 "number %d is the same, "
229 "TS offset: %d, SeqNo offset: %d "
230 "on 0x%x SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200231 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200232 text, seq,
Harald Welte33381352017-12-25 09:44:26 +0100233 state->patch.timestamp_offset, state->patch.seq_offset,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200234 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200235 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200236 }
237 return 0;
238 }
239
240 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200241 (int32_t)(timestamp - sstate->last_timestamp) /
242 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200243
244 if (tsdelta == 0) {
245 /* Don't update *tsdelta_out */
Philipp Maierc3413882017-10-27 12:26:54 +0200246 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200247 "The %s timestamp delta is %d "
248 "on 0x%x SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200249 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200250 text, tsdelta,
251 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200252 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200253
254 return 0;
255 }
256
257 if (sstate->last_tsdelta != tsdelta) {
258 if (sstate->last_tsdelta) {
Philipp Maierc3413882017-10-27 12:26:54 +0200259 LOGP(DRTP, LOGL_INFO,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200260 "The %s timestamp delta changes from %d to %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200261 "on 0x%x SSRC: %u timestamp: %u from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200262 text, sstate->last_tsdelta, tsdelta,
263 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200264 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200265 }
266 }
267
268 if (tsdelta_out)
269 *tsdelta_out = tsdelta;
270
271 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200272 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200273
274 if (timestamp_error) {
275 sstate->err_ts_counter += 1;
Philipp Maierc3413882017-10-27 12:26:54 +0200276 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200277 "The %s timestamp has an alignment error of %d "
278 "on 0x%x SSRC: %u "
279 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200280 "from %s:%d. ptime: %d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200281 text, timestamp_error,
282 ENDPOINT_NUMBER(endp), sstate->ssrc,
283 (int16_t)(seq - sstate->last_seq),
284 (int32_t)(timestamp - sstate->last_timestamp),
285 tsdelta,
286 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200287 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200288 }
289 return 1;
290}
291
292/* Set the timestamp offset according to the packet duration. */
293static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp,
294 struct mgcp_rtp_state *state,
295 struct mgcp_rtp_end *rtp_end,
296 struct sockaddr_in *addr,
297 int16_t delta_seq, uint32_t in_timestamp)
298{
299 int32_t tsdelta = state->packet_duration;
300 int timestamp_offset;
301 uint32_t out_timestamp;
302
303 if (tsdelta == 0) {
304 tsdelta = state->out_stream.last_tsdelta;
305 if (tsdelta != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200306 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200307 "A fixed packet duration is not available on 0x%x, "
308 "using last output timestamp delta instead: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200309 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200310 ENDPOINT_NUMBER(endp), tsdelta,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200311 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200312 } else {
313 tsdelta = rtp_end->codec.rate * 20 / 1000;
Philipp Maierc3413882017-10-27 12:26:54 +0200314 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200315 "Fixed packet duration and last timestamp delta "
316 "are not available on 0x%x, "
317 "using fixed 20ms instead: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200318 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200319 ENDPOINT_NUMBER(endp), tsdelta,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200320 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200321 }
322 }
323
324 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
325 timestamp_offset = out_timestamp - in_timestamp;
326
Harald Welte33381352017-12-25 09:44:26 +0100327 if (state->patch.timestamp_offset != timestamp_offset) {
328 state->patch.timestamp_offset = timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200329
Philipp Maierc3413882017-10-27 12:26:54 +0200330 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200331 "Timestamp offset change on 0x%x SSRC: %u "
332 "SeqNo delta: %d, TS offset: %d, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200333 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200334 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100335 delta_seq, state->patch.timestamp_offset,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200336 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200337 }
338
339 return timestamp_offset;
340}
341
342/* Set the timestamp offset according to the packet duration. */
343static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp,
344 struct mgcp_rtp_state *state,
345 struct mgcp_rtp_end *rtp_end,
346 struct sockaddr_in *addr,
347 uint32_t timestamp)
348{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200349 int ts_error = 0;
350 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200351 int ptime = state->packet_duration;
352
353 /* Align according to: T + Toffs - Tlast = k * Tptime */
354
Philipp Maier87bd9be2017-08-22 16:35:41 +0200355 ts_error = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100356 timestamp + state->patch.timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200357
Philipp Maier87bd9be2017-08-22 16:35:41 +0200358 /* If there is an alignment error, we have to compensate it */
359 if (ts_error) {
Harald Welte33381352017-12-25 09:44:26 +0100360 state->patch.timestamp_offset += ptime - ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200361
Philipp Maierc3413882017-10-27 12:26:54 +0200362 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200363 "Corrected timestamp alignment error of %d on 0x%x SSRC: %u "
364 "new TS offset: %d, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200365 "from %s:%d\n",
366 ts_error,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200367 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100368 state->patch.timestamp_offset, inet_ntoa(addr->sin_addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200369 ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200370 }
371
Philipp Maier87bd9be2017-08-22 16:35:41 +0200372 /* Check we really managed to compensate the timestamp
373 * offset. There should not be any remaining error, failing
Harald Welte1d1b98f2017-12-25 10:03:40 +0100374 * here would point to a serous problem with the alignment
375 * error computation function */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200376 ts_check = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100377 timestamp + state->patch.timestamp_offset);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200378 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200379
Philipp Maier87bd9be2017-08-22 16:35:41 +0200380 /* Return alignment error before compensation */
381 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200382}
383
Philipp Maier87bd9be2017-08-22 16:35:41 +0200384/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
385 * \param[in] associated endpoint
386 * \param[in] destination RTP end
387 * \param[in,out] pointer to buffer with voice data
388 * \param[in] voice data length
Harald Welte1d1b98f2017-12-25 10:03:40 +0100389 * \param[in] maximum size of caller provided voice data buffer
Philipp Maier87bd9be2017-08-22 16:35:41 +0200390 * \returns ignores input parameters, return always 0 */
391int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
392 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200393 char *data, int *len, int buf_size)
394{
Philipp Maier230e4fc2017-11-28 09:38:45 +0100395 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x transcoding disabled\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200396 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200397 return 0;
398}
399
Philipp Maier87bd9be2017-08-22 16:35:41 +0200400/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
401 * \param[in] associated endpoint
402 * \param[in] destination RTP end
403 * \param[in] source RTP end
404 * \returns ignores input parameters, return always 0 */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200405int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
406 struct mgcp_rtp_end *dst_end,
407 struct mgcp_rtp_end *src_end)
408{
Philipp Maier230e4fc2017-11-28 09:38:45 +0100409 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x transcoding disabled\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200410 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200411 return 0;
412}
413
414void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
415 int *payload_type,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200416 const char **audio_name,
417 const char **fmtp_extra,
418 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200419{
Philipp Maierc3413882017-10-27 12:26:54 +0200420 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100421 "endpoint:0x%x conn:%s using format defaults\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200422 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
423
424 *payload_type = conn->end.codec.payload_type;
425 *audio_name = conn->end.codec.audio_name;
426 *fmtp_extra = conn->end.fmtp_extra;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200427}
428
Philipp Maier87bd9be2017-08-22 16:35:41 +0200429void mgcp_rtp_annex_count(struct mgcp_endpoint *endp,
430 struct mgcp_rtp_state *state, const uint16_t seq,
431 const int32_t transit, const uint32_t ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200432{
433 int32_t d;
434
435 /* initialize or re-initialize */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100436 if (!state->stats.initialized || state->stats.ssrc != ssrc) {
437 state->stats.initialized = 1;
438 state->stats.base_seq = seq;
439 state->stats.max_seq = seq - 1;
440 state->stats.ssrc = ssrc;
441 state->stats.jitter = 0;
442 state->stats.transit = transit;
443 state->stats.cycles = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200444 } else {
445 uint16_t udelta;
446
Philipp Maier87bd9be2017-08-22 16:35:41 +0200447 /* The below takes the shape of the validation of
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200448 * Appendix A. Check if there is something weird with
449 * the sequence number, otherwise check for a wrap
450 * around in the sequence number.
451 * It can't wrap during the initialization so let's
452 * skip it here. The Appendix A probably doesn't have
Philipp Maier87bd9be2017-08-22 16:35:41 +0200453 * this issue because of the probation. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100454 udelta = seq - state->stats.max_seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200455 if (udelta < RTP_MAX_DROPOUT) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100456 if (seq < state->stats.max_seq)
457 state->stats.cycles += RTP_SEQ_MOD;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200458 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Philipp Maierc3413882017-10-27 12:26:54 +0200459 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200460 "RTP seqno made a very large jump on 0x%x delta: %u\n",
461 ENDPOINT_NUMBER(endp), udelta);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200462 }
463 }
464
Philipp Maier87bd9be2017-08-22 16:35:41 +0200465 /* Calculate the jitter between the two packages. The TS should be
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200466 * taken closer to the read function. This was taken from the
467 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
Philipp Maier87bd9be2017-08-22 16:35:41 +0200468 * resolution. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100469 d = transit - state->stats.transit;
470 state->stats.transit = transit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200471 if (d < 0)
472 d = -d;
Harald Welte49e3d5a2017-12-25 09:47:57 +0100473 state->stats.jitter += d - ((state->stats.jitter + 8) >> 4);
474 state->stats.max_seq = seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200475}
476
Philipp Maier87bd9be2017-08-22 16:35:41 +0200477/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200478 * some of the supported endpoints (e.g. the nanoBTS) can only handle
479 * one source and this code will patch RTP header to appear as if there
480 * is only one source.
481 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200482 * we receive will be seen as a switch in streams. */
483void mgcp_patch_and_count(struct mgcp_endpoint *endp,
484 struct mgcp_rtp_state *state,
485 struct mgcp_rtp_end *rtp_end,
486 struct sockaddr_in *addr, char *data, int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200487{
488 uint32_t arrival_time;
489 int32_t transit;
490 uint16_t seq;
491 uint32_t timestamp, ssrc;
492 struct rtp_hdr *rtp_hdr;
493 int payload = rtp_end->codec.payload_type;
494
495 if (len < sizeof(*rtp_hdr))
496 return;
497
Philipp Maier87bd9be2017-08-22 16:35:41 +0200498 rtp_hdr = (struct rtp_hdr *)data;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200499 seq = ntohs(rtp_hdr->sequence);
500 timestamp = ntohl(rtp_hdr->timestamp);
501 arrival_time = get_current_ts(rtp_end->codec.rate);
502 ssrc = ntohl(rtp_hdr->ssrc);
503 transit = arrival_time - timestamp;
504
505 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc);
506
507 if (!state->initialized) {
508 state->initialized = 1;
509 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100510 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200511 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200512 state->packet_duration =
513 mgcp_rtp_packet_duration(endp, rtp_end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200514 state->out_stream = state->in_stream;
515 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200516 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Philipp Maierc3413882017-10-27 12:26:54 +0200517 LOGP(DRTP, LOGL_INFO,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100518 "endpoint:0x%x initializing stream, SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200519 "pkt-duration: %d, from %s:%d\n",
520 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100521 state->patch.seq_offset, state->packet_duration,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200522 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200523 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200524 state->packet_duration =
525 rtp_end->codec.rate * 20 / 1000;
Philipp Maierc3413882017-10-27 12:26:54 +0200526 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100527 "endpoint:0x%x fixed packet duration is not available, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200528 "using fixed 20ms instead: %d from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200529 ENDPOINT_NUMBER(endp), state->packet_duration,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200530 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200531 }
532 } else if (state->in_stream.ssrc != ssrc) {
Philipp Maierc3413882017-10-27 12:26:54 +0200533 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100534 "endpoint:0x%x SSRC changed: %u -> %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200535 "from %s:%d\n",
536 ENDPOINT_NUMBER(endp),
537 state->in_stream.ssrc, rtp_hdr->ssrc,
538 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200539
540 state->in_stream.ssrc = ssrc;
541 if (rtp_end->force_constant_ssrc) {
542 int16_t delta_seq;
543
544 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100545 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200546 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200547
548 /* Estimate number of packets that would have been sent */
549 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200550 (arrival_time - state->in_stream.last_arrival_time
551 + state->packet_duration / 2) /
552 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200553
554 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
555 delta_seq, timestamp);
556
Harald Welte33381352017-12-25 09:44:26 +0100557 state->patch.patch_ssrc = 1;
558 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200559 if (rtp_end->force_constant_ssrc != -1)
560 rtp_end->force_constant_ssrc -= 1;
561
Philipp Maierc3413882017-10-27 12:26:54 +0200562 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100563 "endpoint:0x%x SSRC patching enabled, SSRC: %u "
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200564 "SeqNo offset: %d, TS offset: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200565 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200566 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100567 state->patch.seq_offset, state->patch.timestamp_offset,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200568 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200569 }
570
571 state->in_stream.last_tsdelta = 0;
572 } else {
573 /* Compute current per-packet timestamp delta */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200574 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
575 addr, seq, timestamp, "input",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200576 &state->in_stream.last_tsdelta);
577
Harald Welte33381352017-12-25 09:44:26 +0100578 if (state->patch.patch_ssrc)
579 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200580 }
581
582 /* Save before patching */
583 state->in_stream.last_timestamp = timestamp;
584 state->in_stream.last_seq = seq;
585 state->in_stream.last_arrival_time = arrival_time;
586
587 if (rtp_end->force_aligned_timing &&
588 state->out_stream.ssrc == ssrc && state->packet_duration)
589 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200590 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
591 timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200592
593 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100594 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200595 rtp_hdr->ssrc = htonl(ssrc);
596
597 /* Apply the offset and store it back to the packet.
598 * This won't change anything if the offset is 0, so the conditional is
599 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100600 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200601 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100602 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200603 rtp_hdr->timestamp = htonl(timestamp);
604
605 /* Check again, whether the timestamps are still valid */
606 if (state->out_stream.ssrc == ssrc)
607 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
608 addr, seq, timestamp, "output",
609 &state->out_stream.last_tsdelta);
610
611 /* Save output values */
612 state->out_stream.last_seq = seq;
613 state->out_stream.last_timestamp = timestamp;
614 state->out_stream.ssrc = ssrc;
615
616 if (payload < 0)
617 return;
618
619#if 0
Philipp Maierc3413882017-10-27 12:26:54 +0200620 DEBUGP(DRTP,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100621 "endpoint:0x%x payload hdr payload %u -> endp payload %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200622 ENDPOINT_NUMBER(endp), rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200623 rtp_hdr->payload_type = payload;
624#endif
625}
626
Philipp Maier87bd9be2017-08-22 16:35:41 +0200627/* Forward data to a debug tap. This is debug function that is intended for
628 * debugging the voice traffic with tools like gstreamer */
Philipp Maiere6f172d2017-11-07 12:00:01 +0100629static void forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf,
630 int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200631{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100632 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200633
Philipp Maiere6f172d2017-11-07 12:00:01 +0100634 if (!tap->enabled)
635 return;
636
637 rc = sendto(fd, buf, len, 0, (struct sockaddr *)&tap->forward,
638 sizeof(tap->forward));
639
640 if (rc < 0)
641 LOGP(DRTP, LOGL_ERROR,
642 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200643}
644
Philipp Maier87bd9be2017-08-22 16:35:41 +0200645/*! Send RTP/RTCP data to a specified destination connection.
646 * \param[in] endp associated endpoint (for configuration, logging)
647 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP
648 * \param[in] spoofed source address (set to NULL to disable)
649 * \param[in] buf buffer that contains the RTP/RTCP data
650 * \param[in] len length of the buffer that contains the RTP/RTCP data
651 * \param[in] conn_src associated source connection
652 * \param[in] conn_dst associated destination connection
653 * \returns 0 on success, -1 on ERROR */
654int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
655 char *buf, int len, struct mgcp_conn_rtp *conn_src,
656 struct mgcp_conn_rtp *conn_dst)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200657{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200658 /*! When no destination connection is available (e.g. when only one
659 * connection in loopback mode exists), then the source connection
660 * shall be specified as destination connection */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200661
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200662 struct mgcp_trunk_config *tcfg = endp->tcfg;
663 struct mgcp_rtp_end *rtp_end;
664 struct mgcp_rtp_state *rtp_state;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200665 char *dest_name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200666
Philipp Maier87bd9be2017-08-22 16:35:41 +0200667 OSMO_ASSERT(conn_src);
668 OSMO_ASSERT(conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200669
Philipp Maier87bd9be2017-08-22 16:35:41 +0200670 if (is_rtp) {
Philipp Maierc3413882017-10-27 12:26:54 +0200671 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100672 "endpoint:0x%x delivering RTP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200673 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200674 } else {
Philipp Maierc3413882017-10-27 12:26:54 +0200675 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100676 "endpoint:0x%x delivering RTCP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200677 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200678 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200679
Philipp Maierc3413882017-10-27 12:26:54 +0200680 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100681 "endpoint:0x%x loop:%d, mode:%d ",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200682 ENDPOINT_NUMBER(endp), tcfg->audio_loop, conn_src->conn->mode);
683 if (conn_src->conn->mode == MGCP_CONN_LOOPBACK)
Philipp Maierc3413882017-10-27 12:26:54 +0200684 LOGPC(DRTP, LOGL_DEBUG, "(loopback)\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200685 else
Philipp Maierc3413882017-10-27 12:26:54 +0200686 LOGPC(DRTP, LOGL_DEBUG, "\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200687
Philipp Maier87bd9be2017-08-22 16:35:41 +0200688 /* Note: In case of loopback configuration, both, the source and the
689 * destination will point to the same connection. */
690 rtp_end = &conn_dst->end;
691 rtp_state = &conn_src->state;
692 dest_name = conn_dst->conn->name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200693
694 if (!rtp_end->output_enabled) {
Harald Weltea0ac30f2017-12-25 09:52:30 +0100695 rtp_end->stats.dropped_packets += 1;
Philipp Maierc3413882017-10-27 12:26:54 +0200696 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100697 "endpoint:0x%x output disabled, drop to %s %s "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200698 "rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200699 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200700 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200701 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200702 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200703 );
704 } else if (is_rtp) {
705 int cont;
706 int nbytes = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200707 int buflen = len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200708 do {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200709 /* Run transcoder */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200710 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200711 buf, &buflen,
712 RTP_BUF_SIZE);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200713 if (cont < 0)
714 break;
715
Philipp Maier87bd9be2017-08-22 16:35:41 +0200716 if (addr)
717 mgcp_patch_and_count(endp, rtp_state, rtp_end,
718 addr, buf, buflen);
Philipp Maierc3413882017-10-27 12:26:54 +0200719 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100720 "endpoint:0x%x process/send to %s %s "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200721 "rtp_port:%u rtcp_port:%u\n",
722 ENDPOINT_NUMBER(endp), dest_name,
723 inet_ntoa(rtp_end->addr), ntohs(rtp_end->rtp_port),
724 ntohs(rtp_end->rtcp_port)
725 );
726
727 /* Forward a copy of the RTP data to a debug ip/port */
728 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
729 buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200730
731 /* FIXME: HACK HACK HACK. See OS#2459.
732 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
733 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
734 * cells (as long as we patch only the first RTP payload in each stream).
735 */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200736 if (!rtp_state->patched_first_rtp_payload) {
737 uint8_t *data = (uint8_t *) & buf[12];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200738 data[0] = 0xe4;
739 data[1] = 0x00;
740 rtp_state->patched_first_rtp_payload = true;
741 }
742
Philipp Maier87bd9be2017-08-22 16:35:41 +0200743 len = mgcp_udp_send(rtp_end->rtp.fd,
744 &rtp_end->addr,
745 rtp_end->rtp_port, buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200746
Philipp Maier87bd9be2017-08-22 16:35:41 +0200747 if (len <= 0)
748 return len;
749
Harald Weltea0ac30f2017-12-25 09:52:30 +0100750 conn_dst->end.stats.packets_tx += 1;
751 conn_dst->end.stats.octets_tx += len;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200752
753 nbytes += len;
754 buflen = cont;
755 } while (buflen > 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200756 return nbytes;
757 } else if (!tcfg->omit_rtcp) {
Philipp Maierc3413882017-10-27 12:26:54 +0200758 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100759 "endpoint:0x%x send to %s %s rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200760 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200761 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200762 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200763 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200764 );
765
Philipp Maier87bd9be2017-08-22 16:35:41 +0200766 len = mgcp_udp_send(rtp_end->rtcp.fd,
767 &rtp_end->addr,
768 rtp_end->rtcp_port, buf, len);
769
Harald Weltea0ac30f2017-12-25 09:52:30 +0100770 conn_dst->end.stats.packets_tx += 1;
771 conn_dst->end.stats.octets_tx += len;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200772
773 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200774 }
775
776 return 0;
777}
778
Philipp Maier87bd9be2017-08-22 16:35:41 +0200779/* Helper function for mgcp_recv(),
780 Receive one RTP Packet + Originating address from file descriptor */
781static int receive_from(struct mgcp_endpoint *endp, int fd,
782 struct sockaddr_in *addr, char *buf, int bufsize)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200783{
784 int rc;
785 socklen_t slen = sizeof(*addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200786 struct sockaddr_in addr_sink;
787 char buf_sink[RTP_BUF_SIZE];
788 bool tossed = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200789
Philipp Maier87bd9be2017-08-22 16:35:41 +0200790 if (!addr)
791 addr = &addr_sink;
792 if (!buf) {
793 tossed = true;
794 buf = buf_sink;
795 bufsize = sizeof(buf_sink);
796 }
797
798 rc = recvfrom(fd, buf, bufsize, 0, (struct sockaddr *)addr, &slen);
799
Philipp Maierc3413882017-10-27 12:26:54 +0200800 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200801 "receiving %u bytes length packet from %s:%u ...\n",
802 rc, inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
803
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200804 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200805 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100806 "endpoint:0x%x failed to receive packet, errno: %d/%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200807 ENDPOINT_NUMBER(endp), errno, strerror(errno));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200808 return -1;
809 }
810
Philipp Maier87bd9be2017-08-22 16:35:41 +0200811 if (tossed) {
Philipp Maier230e4fc2017-11-28 09:38:45 +0100812 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200813 ENDPOINT_NUMBER(endp));
814 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200815
816 return rc;
817}
818
Philipp Maier87bd9be2017-08-22 16:35:41 +0200819/* Check if the origin (addr) matches the address/port data of the RTP
820 * connections. */
821static int check_rtp_origin(struct mgcp_conn_rtp *conn,
822 struct sockaddr_in *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200823{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200824 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200825 endp = conn->conn->endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200826
Philipp Maier87bd9be2017-08-22 16:35:41 +0200827 /* Note: Check if the inbound RTP data comes from the same host to
828 * which we send our outgoing RTP traffic. */
829 if (memcmp(&addr->sin_addr, &conn->end.addr, sizeof(addr->sin_addr))
830 != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200831 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100832 "endpoint:0x%x data from wrong address: %s, ",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200833 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr));
Philipp Maierc3413882017-10-27 12:26:54 +0200834 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200835 inet_ntoa(conn->end.addr));
Philipp Maier230e4fc2017-11-28 09:38:45 +0100836 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200837 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200838 return -1;
839 }
840
Philipp Maier87bd9be2017-08-22 16:35:41 +0200841 /* Note: Usually the remote remote port of the data we receive will be
842 * the same as the remote port where we transmit outgoing RTP traffic
843 * to (set by MDCX). We use this to check the origin of the data for
844 * plausibility. */
845 if (conn->end.rtp_port != addr->sin_port &&
846 conn->end.rtcp_port != addr->sin_port) {
Philipp Maierc3413882017-10-27 12:26:54 +0200847 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100848 "endpoint:0x%x data from wrong source port: %d, ",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200849 ENDPOINT_NUMBER(endp), ntohs(addr->sin_port));
Philipp Maierc3413882017-10-27 12:26:54 +0200850 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200851 "expected: %d for RTP or %d for RTCP\n",
852 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Philipp Maier230e4fc2017-11-28 09:38:45 +0100853 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200854 ENDPOINT_NUMBER(endp));
855 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200856 }
857
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200858 return 0;
859}
860
Philipp Maier87bd9be2017-08-22 16:35:41 +0200861/* Check the if the destination address configuration of an RTP connection
862 * makes sense */
863static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200864{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200865 struct mgcp_endpoint *endp;
866 endp = conn->conn->endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200867
Philipp Maier87bd9be2017-08-22 16:35:41 +0200868 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200869 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100870 "endpoint:0x%x destination IP-address is invalid\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200871 ENDPOINT_NUMBER(endp));
872 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200873 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200874
875 if (conn->end.rtp_port == 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200876 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100877 "endpoint:0x%x destination rtp port is invalid\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200878 ENDPOINT_NUMBER(endp));
879 return -1;
880 }
881
882 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200883}
884
Philipp Maier87bd9be2017-08-22 16:35:41 +0200885/* Receive RTP data from a specified source connection and dispatch it to a
886 * destination connection. */
887static int mgcp_recv(int *proto, struct sockaddr_in *addr, char *buf,
888 unsigned int buf_size, struct osmo_fd *fd)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200889{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200890 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200891 struct mgcp_conn_rtp *conn;
892 struct mgcp_trunk_config *tcfg;
893 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200894
Philipp Maier87bd9be2017-08-22 16:35:41 +0200895 conn = (struct mgcp_conn_rtp*) fd->data;
896 endp = conn->conn->endp;
897 tcfg = endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200898
Philipp Maier230e4fc2017-11-28 09:38:45 +0100899 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x receiving RTP/RTCP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200900 ENDPOINT_NUMBER(endp));
901
902 rc = receive_from(endp, fd->fd, addr, buf, buf_size);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200903 if (rc <= 0)
904 return -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200905 *proto = fd == &conn->end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200906
Philipp Maier230e4fc2017-11-28 09:38:45 +0100907 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x ", ENDPOINT_NUMBER(endp));
Neels Hofmeyr56725632018-01-15 15:15:07 +0100908 LOGPC(DRTP, LOGL_DEBUG, "receiving from %s %s %d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200909 conn->conn->name, inet_ntoa(addr->sin_addr),
910 ntohs(addr->sin_port));
Philipp Maier230e4fc2017-11-28 09:38:45 +0100911 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x conn:%s\n", ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200912 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200913
Philipp Maier87bd9be2017-08-22 16:35:41 +0200914 /* Check if the origin of the RTP packet seems plausible */
915 if (tcfg->rtp_accept_all == 0) {
916 if (check_rtp_origin(conn, addr) != 0)
917 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200918 }
919
Philipp Maier87bd9be2017-08-22 16:35:41 +0200920 /* Filter out dummy message */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200921 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Philipp Maierc3413882017-10-27 12:26:54 +0200922 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100923 "endpoint:0x%x dummy message received\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200924 ENDPOINT_NUMBER(endp));
Philipp Maierc3413882017-10-27 12:26:54 +0200925 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100926 "endpoint:0x%x packet tossed\n", ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200927 return 0;
928 }
929
Philipp Maier87bd9be2017-08-22 16:35:41 +0200930 /* Increment RX statistics */
Harald Weltea0ac30f2017-12-25 09:52:30 +0100931 conn->end.stats.packets_rx += 1;
932 conn->end.stats.octets_rx += rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200933
Philipp Maier87bd9be2017-08-22 16:35:41 +0200934 /* Forward a copy of the RTP data to a debug ip/port */
935 forward_data(fd->fd, &conn->tap_in, buf, rc);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200936
Philipp Maier87bd9be2017-08-22 16:35:41 +0200937 return rc;
938}
939
940/* Send RTP data. Possible options are standard RTP packet
941 * transmission or trsmission via an osmux connection */
942static int mgcp_send_rtp(int proto, struct sockaddr_in *addr, char *buf,
943 unsigned int buf_size,
944 struct mgcp_conn_rtp *conn_src,
945 struct mgcp_conn_rtp *conn_dst)
946{
947 struct mgcp_endpoint *endp;
948 endp = conn_src->conn->endp;
949
Philipp Maier230e4fc2017-11-28 09:38:45 +0100950 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x destin conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200951 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_dst->conn));
952
953 /* Before we try to deliver the packet, we check if the destination
954 * port and IP-Address make sense at all. If not, we will be unable
955 * to deliver the packet. */
956 if (check_rtp_destin(conn_dst) != 0)
957 return -1;
958
959 /* Depending on the RTP connection type, deliver the RTP packet to the
960 * destination connection. */
961 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200962 case MGCP_RTP_DEFAULT:
Philipp Maierc3413882017-10-27 12:26:54 +0200963 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100964 "endpoint:0x%x endpoint type is MGCP_RTP_DEFAULT, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200965 "using mgcp_send() to forward data directly\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200966 ENDPOINT_NUMBER(endp));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200967 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
968 addr, buf, buf_size, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200969 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200970 case MGCP_OSMUX_BSC:
Philipp Maierc3413882017-10-27 12:26:54 +0200971 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100972 "endpoint:0x%x endpoint type is MGCP_OSMUX_BSC_NAT, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200973 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n",
974 ENDPOINT_NUMBER(endp));
975 return osmux_xfrm_to_osmux(buf, buf_size, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200976 }
977
Philipp Maier87bd9be2017-08-22 16:35:41 +0200978 /* If the data has not been handled/forwarded until here, it will
979 * be discarded, this should not happen, normally the MGCP type
980 * should be properly set */
Philipp Maierc3413882017-10-27 12:26:54 +0200981 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100982 "endpoint:0x%x bad MGCP type -- data discarded!\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200983 ENDPOINT_NUMBER(endp));
984
985 return -1;
986}
987
988/*! dispatch incoming RTP packet to opposite RTP connection.
989 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP)
990 * \param[in] addr socket address where the RTP packet has been received from
991 * \param[in] buf buffer that hold the RTP payload
992 * \param[in] buf_size size data length of buf
993 * \param[in] conn originating connection
994 * \returns 0 on success, -1 on ERROR */
995int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf,
996 unsigned int buf_size, struct mgcp_conn *conn)
997{
998 struct mgcp_conn *conn_dst;
999 struct mgcp_endpoint *endp;
1000 endp = conn->endp;
1001
1002 /*! NOTE: This callback function implements the endpoint specific
1003 * dispatch bahviour of an rtp bridge/proxy endpoint. It is assumed
1004 * that the endpoint will hold only two connections. This premise
1005 * is used to determine the opposite connection (it is always the
1006 * connection that is not the originating connection). Once the
1007 * destination connection is known the RTP packet is sent via
1008 * the destination connection. */
1009
1010 /* Find a destination connection. */
1011 /* NOTE: This code path runs every time an RTP packet is received. The
1012 * function mgcp_find_dst_conn() we use to determine the detination
1013 * connection will iterate the connection list inside the endpoint.
1014 * Since list iterations are quite costly, we will figure out the
1015 * destination only once and use the optional private data pointer of
1016 * the connection to cache the destination connection pointer. */
1017 if (!conn->priv) {
1018 conn_dst = mgcp_find_dst_conn(conn);
1019 conn->priv = conn_dst;
1020 } else {
1021 conn_dst = (struct mgcp_conn *)conn->priv;
1022 }
1023
1024 /* There is no destination conn, stop here */
1025 if (!conn_dst) {
Philipp Maierc3413882017-10-27 12:26:54 +02001026 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001027 "endpoint:0x%x unable to find destination conn\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001028 ENDPOINT_NUMBER(endp));
1029 return -1;
1030 }
1031
1032 /* The destination conn is not an RTP connection */
1033 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Philipp Maierc3413882017-10-27 12:26:54 +02001034 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001035 "endpoint:0x%x unable to find suitable destination conn\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001036 ENDPOINT_NUMBER(endp));
1037 return -1;
1038 }
1039
1040 /* Dispatch RTP packet to destination RTP connection */
1041 return mgcp_send_rtp(proto, addr, buf,
1042 buf_size, &conn->u.rtp, &conn_dst->u.rtp);
1043
1044}
1045
Philipp Maierdf5d2192018-01-24 11:39:32 +01001046/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1047 * \param[in] endp Endpoint on which the connection resides.
1048 * \param[in] conn Connection that is about to be removed (ignored).
1049 * \returns 0 on success, -1 on ERROR. */
1050void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1051{
1052 struct mgcp_conn *conn_cleanup;
1053
1054 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1055 * pointer to the destination connection, so that we do not have
1056 * to go through the list every time an RTP packet arrives. To prevent
1057 * a use-after-free situation we invalidate this information for all
1058 * connections present when one connection is removed from the
1059 * endpoint. */
1060 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
1061 conn_cleanup->priv = NULL;
1062 }
1063}
1064
Philipp Maier87bd9be2017-08-22 16:35:41 +02001065/* Handle incoming RTP data from NET */
1066static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1067{
1068 /* NOTE: This is a generic implementation. RTP data is received. In
1069 * case of loopback the data is just sent back to its origin. All
1070 * other cases implement endpoint specific behaviour (e.g. how is the
1071 * destination connection determined?). That specific behaviour is
1072 * implemented by the callback function that is called at the end of
1073 * the function */
1074
1075 struct mgcp_conn_rtp *conn_src;
1076 struct mgcp_endpoint *endp;
1077 struct sockaddr_in addr;
1078
1079 char buf[RTP_BUF_SIZE];
1080 int proto;
Philipp Maierb9694552017-11-08 16:53:57 +01001081 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001082
1083 conn_src = (struct mgcp_conn_rtp *)fd->data;
1084 OSMO_ASSERT(conn_src);
1085 endp = conn_src->conn->endp;
1086 OSMO_ASSERT(endp);
1087
Philipp Maier230e4fc2017-11-28 09:38:45 +01001088 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x source conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001089 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_src->conn));
1090
1091 /* Receive packet */
Philipp Maierb9694552017-11-08 16:53:57 +01001092 len = mgcp_recv(&proto, &addr, buf, sizeof(buf), fd);
1093 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001094 return -1;
1095
1096 /* Check if the connection is in loopback mode, if yes, just send the
1097 * incoming data back to the origin */
Philipp Maier5dbfc782017-12-12 16:20:25 +01001098
Philipp Maier87bd9be2017-08-22 16:35:41 +02001099 if (conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Philipp Maier5dbfc782017-12-12 16:20:25 +01001100 /* When we are in loopback mode, we loop back all incoming
1101 * packets back to their origin. We will use the originating
1102 * address data from the UDP packet header to patch the
1103 * outgoing address in connection on the fly */
1104 if (conn_src->end.rtp_port == 0) {
1105 conn_src->end.addr = addr.sin_addr;
1106 conn_src->end.rtp_port = addr.sin_port;
1107 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001108 return mgcp_send_rtp(proto, &addr, buf,
Philipp Maierb9694552017-11-08 16:53:57 +01001109 len, conn_src, conn_src);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001110 }
1111
1112 /* Execute endpoint specific implementation that handles the
1113 * dispatching of the RTP data */
Philipp Maierb9694552017-11-08 16:53:57 +01001114 return endp->type->dispatch_rtp_cb(proto, &addr, buf, len,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001115 conn_src->conn);
1116}
1117
1118/*! set IP Type of Service parameter.
1119 * \param[in] fd associated file descriptor
1120 * \param[in] tos dscp value
1121 * \returns 0 on success, -1 on ERROR */
1122int mgcp_set_ip_tos(int fd, int tos)
1123{
1124 int ret;
1125 ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1126
1127 if (ret < 0)
1128 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001129 return 0;
1130}
1131
Philipp Maier87bd9be2017-08-22 16:35:41 +02001132/*! bind RTP port to osmo_fd.
1133 * \param[in] source_addr source (local) address to bind on
1134 * \param[in] fd associated file descriptor
1135 * \param[in] port to bind on
1136 * \returns 0 on success, -1 on ERROR */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001137int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
1138{
Harald Welte8890dfa2017-11-17 15:09:30 +01001139 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001140
Harald Welte8890dfa2017-11-17 15:09:30 +01001141 rc = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
1142 NULL, 0, OSMO_SOCK_F_BIND);
1143 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001144 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001145 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001146 return -1;
1147 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001148 fd->fd = rc;
1149 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001150
1151 return 0;
1152}
1153
Philipp Maier87bd9be2017-08-22 16:35:41 +02001154/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001155static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001156 struct mgcp_rtp_end *rtp_end, int endpno)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001157{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001158 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1159 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1160
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001161 if (mgcp_create_bind(source_addr, &rtp_end->rtp,
1162 rtp_end->local_port) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001163 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001164 "endpoint:0x%x failed to create RTP port: %s:%d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001165 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001166 goto cleanup0;
1167 }
1168
1169 if (mgcp_create_bind(source_addr, &rtp_end->rtcp,
1170 rtp_end->local_port + 1) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001171 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001172 "endpoint:0x%x failed to create RTCP port: %s:%d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001173 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001174 goto cleanup1;
1175 }
1176
Philipp Maier87bd9be2017-08-22 16:35:41 +02001177 /* Set Type of Service (DSCP-Value) as configured via VTY */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001178 mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
1179 mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
1180
1181 rtp_end->rtp.when = BSC_FD_READ;
1182 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001183 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001184 "endpoint:0x%x failed to register RTP port %d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001185 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001186 goto cleanup2;
1187 }
1188
1189 rtp_end->rtcp.when = BSC_FD_READ;
1190 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001191 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001192 "endpoint:0x%x failed to register RTCP port %d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001193 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001194 goto cleanup3;
1195 }
1196
1197 return 0;
1198
1199cleanup3:
1200 osmo_fd_unregister(&rtp_end->rtp);
1201cleanup2:
1202 close(rtp_end->rtcp.fd);
1203 rtp_end->rtcp.fd = -1;
1204cleanup1:
1205 close(rtp_end->rtp.fd);
1206 rtp_end->rtp.fd = -1;
1207cleanup0:
1208 return -1;
1209}
1210
Philipp Maier87bd9be2017-08-22 16:35:41 +02001211/*! bind RTP port to endpoint/connection.
1212 * \param[in] endp endpoint that holds the RTP connection
1213 * \param[in] rtp_port port number to bind on
1214 * \param[in] conn associated RTP connection
1215 * \returns 0 on success, -1 on ERROR */
1216int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1217 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001218{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001219 char name[512];
1220 struct mgcp_rtp_end *end;
Philipp Maier1cb1e382017-11-02 17:16:04 +01001221 char local_ip_addr[INET_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001222
Philipp Maier01d24a32017-11-21 17:26:09 +01001223 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001224 end = &conn->end;
1225
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001226 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Philipp Maierc3413882017-10-27 12:26:54 +02001227 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001228 "endpoint:0x%x %u was already bound on conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001229 ENDPOINT_NUMBER(endp), rtp_port,
1230 mgcp_conn_dump(conn->conn));
1231
1232 /* Double bindings should never occour! Since we always allocate
1233 * connections dynamically and free them when they are not
1234 * needed anymore, there must be no previous binding leftover.
1235 * Should there be a connection bound twice, we have a serious
1236 * problem and must exit immediately! */
1237 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001238 }
1239
1240 end->local_port = rtp_port;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001241 end->rtp.cb = rtp_data_net;
1242 end->rtp.data = conn;
1243 end->rtcp.data = conn;
1244 end->rtcp.cb = rtp_data_net;
1245
Philipp Maier1cb1e382017-11-02 17:16:04 +01001246 mgcp_get_local_addr(local_ip_addr, conn);
1247
1248 return bind_rtp(endp->cfg, local_ip_addr, end,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001249 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001250}
1251
Philipp Maier87bd9be2017-08-22 16:35:41 +02001252/*! free allocated RTP and RTCP ports.
1253 * \param[in] end RTP end */
1254void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001255{
1256 if (end->rtp.fd != -1) {
1257 close(end->rtp.fd);
1258 end->rtp.fd = -1;
1259 osmo_fd_unregister(&end->rtp);
1260 }
1261
1262 if (end->rtcp.fd != -1) {
1263 close(end->rtcp.fd);
1264 end->rtcp.fd = -1;
1265 osmo_fd_unregister(&end->rtcp);
1266 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001267}