blob: 494156d549dd0f250cae4c7aeb8322fb284c64a3 [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) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200225 rate_ctr_inc(sstate->err_ts_ctr);
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) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200275 rate_ctr_inc(sstate->err_ts_ctr);
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 {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200313 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
Philipp Maierbc0346e2018-06-07 09:52:16 +0200424 *payload_type = conn->end.codec->payload_type;
425 *audio_name = conn->end.codec->audio_name;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200426 *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;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200493 int payload = rtp_end->codec->payload_type;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200494
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);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200501 arrival_time = get_current_ts(rtp_end->codec->rate);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200502 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);
Philipp Maier0ec1d4e2018-05-16 11:09:42 +0200514 state->out_stream.last_seq = seq - 1;
515 state->out_stream.ssrc = state->patch.orig_ssrc = ssrc;
516 state->out_stream.last_tsdelta = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200517 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200518 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Philipp Maierc3413882017-10-27 12:26:54 +0200519 LOGP(DRTP, LOGL_INFO,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100520 "endpoint:0x%x initializing stream, SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200521 "pkt-duration: %d, from %s:%d\n",
522 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100523 state->patch.seq_offset, state->packet_duration,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200524 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200525 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200526 state->packet_duration =
Philipp Maierbc0346e2018-06-07 09:52:16 +0200527 rtp_end->codec->rate * 20 / 1000;
Philipp Maierc3413882017-10-27 12:26:54 +0200528 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100529 "endpoint:0x%x fixed packet duration is not available, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200530 "using fixed 20ms instead: %d from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200531 ENDPOINT_NUMBER(endp), state->packet_duration,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200532 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200533 }
534 } else if (state->in_stream.ssrc != ssrc) {
Philipp Maierc3413882017-10-27 12:26:54 +0200535 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100536 "endpoint:0x%x SSRC changed: %u -> %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200537 "from %s:%d\n",
538 ENDPOINT_NUMBER(endp),
539 state->in_stream.ssrc, rtp_hdr->ssrc,
540 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200541
542 state->in_stream.ssrc = ssrc;
543 if (rtp_end->force_constant_ssrc) {
544 int16_t delta_seq;
545
546 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100547 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200548 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200549
550 /* Estimate number of packets that would have been sent */
551 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200552 (arrival_time - state->in_stream.last_arrival_time
553 + state->packet_duration / 2) /
554 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200555
556 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
557 delta_seq, timestamp);
558
Harald Welte33381352017-12-25 09:44:26 +0100559 state->patch.patch_ssrc = 1;
560 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200561 if (rtp_end->force_constant_ssrc != -1)
562 rtp_end->force_constant_ssrc -= 1;
563
Philipp Maierc3413882017-10-27 12:26:54 +0200564 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100565 "endpoint:0x%x SSRC patching enabled, SSRC: %u "
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200566 "SeqNo offset: %d, TS offset: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200567 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200568 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100569 state->patch.seq_offset, state->patch.timestamp_offset,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200570 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200571 }
572
573 state->in_stream.last_tsdelta = 0;
574 } else {
575 /* Compute current per-packet timestamp delta */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200576 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
577 addr, seq, timestamp, "input",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200578 &state->in_stream.last_tsdelta);
579
Harald Welte33381352017-12-25 09:44:26 +0100580 if (state->patch.patch_ssrc)
581 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200582 }
583
584 /* Save before patching */
585 state->in_stream.last_timestamp = timestamp;
586 state->in_stream.last_seq = seq;
587 state->in_stream.last_arrival_time = arrival_time;
588
589 if (rtp_end->force_aligned_timing &&
590 state->out_stream.ssrc == ssrc && state->packet_duration)
591 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200592 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
593 timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200594
595 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100596 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200597 rtp_hdr->ssrc = htonl(ssrc);
598
599 /* Apply the offset and store it back to the packet.
600 * This won't change anything if the offset is 0, so the conditional is
601 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100602 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200603 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100604 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200605 rtp_hdr->timestamp = htonl(timestamp);
606
607 /* Check again, whether the timestamps are still valid */
608 if (state->out_stream.ssrc == ssrc)
609 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
610 addr, seq, timestamp, "output",
611 &state->out_stream.last_tsdelta);
612
613 /* Save output values */
614 state->out_stream.last_seq = seq;
615 state->out_stream.last_timestamp = timestamp;
616 state->out_stream.ssrc = ssrc;
617
618 if (payload < 0)
619 return;
620
621#if 0
Philipp Maierc3413882017-10-27 12:26:54 +0200622 DEBUGP(DRTP,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100623 "endpoint:0x%x payload hdr payload %u -> endp payload %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200624 ENDPOINT_NUMBER(endp), rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200625 rtp_hdr->payload_type = payload;
626#endif
627}
628
Philipp Maier87bd9be2017-08-22 16:35:41 +0200629/* Forward data to a debug tap. This is debug function that is intended for
630 * debugging the voice traffic with tools like gstreamer */
Philipp Maiere6f172d2017-11-07 12:00:01 +0100631static void forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf,
632 int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200633{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100634 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200635
Philipp Maiere6f172d2017-11-07 12:00:01 +0100636 if (!tap->enabled)
637 return;
638
639 rc = sendto(fd, buf, len, 0, (struct sockaddr *)&tap->forward,
640 sizeof(tap->forward));
641
642 if (rc < 0)
643 LOGP(DRTP, LOGL_ERROR,
644 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200645}
646
Philipp Maier87bd9be2017-08-22 16:35:41 +0200647/*! Send RTP/RTCP data to a specified destination connection.
648 * \param[in] endp associated endpoint (for configuration, logging)
649 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP
650 * \param[in] spoofed source address (set to NULL to disable)
651 * \param[in] buf buffer that contains the RTP/RTCP data
652 * \param[in] len length of the buffer that contains the RTP/RTCP data
653 * \param[in] conn_src associated source connection
654 * \param[in] conn_dst associated destination connection
655 * \returns 0 on success, -1 on ERROR */
656int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
657 char *buf, int len, struct mgcp_conn_rtp *conn_src,
658 struct mgcp_conn_rtp *conn_dst)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200659{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200660 /*! When no destination connection is available (e.g. when only one
661 * connection in loopback mode exists), then the source connection
662 * shall be specified as destination connection */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200663
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200664 struct mgcp_trunk_config *tcfg = endp->tcfg;
665 struct mgcp_rtp_end *rtp_end;
666 struct mgcp_rtp_state *rtp_state;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200667 char *dest_name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200668
Philipp Maier87bd9be2017-08-22 16:35:41 +0200669 OSMO_ASSERT(conn_src);
670 OSMO_ASSERT(conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200671
Philipp Maier87bd9be2017-08-22 16:35:41 +0200672 if (is_rtp) {
Philipp Maierc3413882017-10-27 12:26:54 +0200673 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100674 "endpoint:0x%x delivering RTP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200675 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200676 } else {
Philipp Maierc3413882017-10-27 12:26:54 +0200677 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100678 "endpoint:0x%x delivering RTCP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200679 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200680 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200681
Philipp Maierc3413882017-10-27 12:26:54 +0200682 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100683 "endpoint:0x%x loop:%d, mode:%d ",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200684 ENDPOINT_NUMBER(endp), tcfg->audio_loop, conn_src->conn->mode);
685 if (conn_src->conn->mode == MGCP_CONN_LOOPBACK)
Philipp Maierc3413882017-10-27 12:26:54 +0200686 LOGPC(DRTP, LOGL_DEBUG, "(loopback)\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200687 else
Philipp Maierc3413882017-10-27 12:26:54 +0200688 LOGPC(DRTP, LOGL_DEBUG, "\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200689
Philipp Maier87bd9be2017-08-22 16:35:41 +0200690 /* Note: In case of loopback configuration, both, the source and the
691 * destination will point to the same connection. */
692 rtp_end = &conn_dst->end;
693 rtp_state = &conn_src->state;
694 dest_name = conn_dst->conn->name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200695
696 if (!rtp_end->output_enabled) {
Philipp Maiercede2a42018-07-03 14:14:21 +0200697 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_DROPPED_PACKETS_CTR]);
Philipp Maierc3413882017-10-27 12:26:54 +0200698 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100699 "endpoint:0x%x output disabled, drop to %s %s "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200700 "rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200701 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200702 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200703 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200704 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200705 );
706 } else if (is_rtp) {
707 int cont;
708 int nbytes = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200709 int buflen = len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200710 do {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200711 /* Run transcoder */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200712 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200713 buf, &buflen,
714 RTP_BUF_SIZE);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200715 if (cont < 0)
716 break;
717
Philipp Maier87bd9be2017-08-22 16:35:41 +0200718 if (addr)
719 mgcp_patch_and_count(endp, rtp_state, rtp_end,
720 addr, buf, buflen);
Philipp Maierc3413882017-10-27 12:26:54 +0200721 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100722 "endpoint:0x%x process/send to %s %s "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200723 "rtp_port:%u rtcp_port:%u\n",
724 ENDPOINT_NUMBER(endp), dest_name,
725 inet_ntoa(rtp_end->addr), ntohs(rtp_end->rtp_port),
726 ntohs(rtp_end->rtcp_port)
727 );
728
729 /* Forward a copy of the RTP data to a debug ip/port */
730 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
731 buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200732
733 /* FIXME: HACK HACK HACK. See OS#2459.
734 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
735 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
736 * cells (as long as we patch only the first RTP payload in each stream).
737 */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200738 if (!rtp_state->patched_first_rtp_payload) {
739 uint8_t *data = (uint8_t *) & buf[12];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200740 data[0] = 0xe4;
741 data[1] = 0x00;
742 rtp_state->patched_first_rtp_payload = true;
743 }
744
Philipp Maier87bd9be2017-08-22 16:35:41 +0200745 len = mgcp_udp_send(rtp_end->rtp.fd,
746 &rtp_end->addr,
747 rtp_end->rtp_port, buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200748
Philipp Maier87bd9be2017-08-22 16:35:41 +0200749 if (len <= 0)
750 return len;
751
Philipp Maiercede2a42018-07-03 14:14:21 +0200752 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
753 rate_ctr_add(&conn_dst->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], len);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200754
755 nbytes += len;
756 buflen = cont;
757 } while (buflen > 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200758 return nbytes;
759 } else if (!tcfg->omit_rtcp) {
Philipp Maierc3413882017-10-27 12:26:54 +0200760 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100761 "endpoint:0x%x send to %s %s rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200762 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200763 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200764 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200765 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200766 );
767
Philipp Maier87bd9be2017-08-22 16:35:41 +0200768 len = mgcp_udp_send(rtp_end->rtcp.fd,
769 &rtp_end->addr,
770 rtp_end->rtcp_port, buf, len);
771
Philipp Maiercede2a42018-07-03 14:14:21 +0200772 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
773 rate_ctr_add(&conn_dst->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], len);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200774
775 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200776 }
777
778 return 0;
779}
780
Philipp Maier87bd9be2017-08-22 16:35:41 +0200781/* Helper function for mgcp_recv(),
782 Receive one RTP Packet + Originating address from file descriptor */
783static int receive_from(struct mgcp_endpoint *endp, int fd,
784 struct sockaddr_in *addr, char *buf, int bufsize)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200785{
786 int rc;
787 socklen_t slen = sizeof(*addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200788 struct sockaddr_in addr_sink;
789 char buf_sink[RTP_BUF_SIZE];
790 bool tossed = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200791
Philipp Maier87bd9be2017-08-22 16:35:41 +0200792 if (!addr)
793 addr = &addr_sink;
794 if (!buf) {
795 tossed = true;
796 buf = buf_sink;
797 bufsize = sizeof(buf_sink);
798 }
799
800 rc = recvfrom(fd, buf, bufsize, 0, (struct sockaddr *)addr, &slen);
801
Philipp Maierc3413882017-10-27 12:26:54 +0200802 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200803 "receiving %u bytes length packet from %s:%u ...\n",
804 rc, inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
805
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200806 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200807 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100808 "endpoint:0x%x failed to receive packet, errno: %d/%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200809 ENDPOINT_NUMBER(endp), errno, strerror(errno));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200810 return -1;
811 }
812
Philipp Maier87bd9be2017-08-22 16:35:41 +0200813 if (tossed) {
Philipp Maier230e4fc2017-11-28 09:38:45 +0100814 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200815 ENDPOINT_NUMBER(endp));
816 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200817
818 return rc;
819}
820
Philipp Maier87bd9be2017-08-22 16:35:41 +0200821/* Check if the origin (addr) matches the address/port data of the RTP
822 * connections. */
823static int check_rtp_origin(struct mgcp_conn_rtp *conn,
824 struct sockaddr_in *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200825{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200826 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200827 endp = conn->conn->endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200828
Philipp Maier87bd9be2017-08-22 16:35:41 +0200829 /* Note: Check if the inbound RTP data comes from the same host to
830 * which we send our outgoing RTP traffic. */
831 if (memcmp(&addr->sin_addr, &conn->end.addr, sizeof(addr->sin_addr))
832 != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200833 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100834 "endpoint:0x%x data from wrong address: %s, ",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200835 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr));
Philipp Maierc3413882017-10-27 12:26:54 +0200836 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200837 inet_ntoa(conn->end.addr));
Philipp Maier230e4fc2017-11-28 09:38:45 +0100838 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200839 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200840 return -1;
841 }
842
Philipp Maier87bd9be2017-08-22 16:35:41 +0200843 /* Note: Usually the remote remote port of the data we receive will be
844 * the same as the remote port where we transmit outgoing RTP traffic
845 * to (set by MDCX). We use this to check the origin of the data for
846 * plausibility. */
847 if (conn->end.rtp_port != addr->sin_port &&
848 conn->end.rtcp_port != addr->sin_port) {
Philipp Maierc3413882017-10-27 12:26:54 +0200849 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100850 "endpoint:0x%x data from wrong source port: %d, ",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200851 ENDPOINT_NUMBER(endp), ntohs(addr->sin_port));
Philipp Maierc3413882017-10-27 12:26:54 +0200852 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200853 "expected: %d for RTP or %d for RTCP\n",
854 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Philipp Maier230e4fc2017-11-28 09:38:45 +0100855 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200856 ENDPOINT_NUMBER(endp));
857 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200858 }
859
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200860 return 0;
861}
862
Philipp Maier87bd9be2017-08-22 16:35:41 +0200863/* Check the if the destination address configuration of an RTP connection
864 * makes sense */
865static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200866{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200867 struct mgcp_endpoint *endp;
868 endp = conn->conn->endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200869
Philipp Maiere6df0e42018-05-29 14:03:06 +0200870 /* Note: it is legal to create a connection but never setting a port
871 * and IP-address for outgoing data. */
872 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0 && conn->end.rtp_port == 0) {
873 LOGP(DRTP, LOGL_DEBUG,
874 "endpoint:0x%x destination IP-address and rtp port is (not yet) known\n",
875 ENDPOINT_NUMBER(endp));
876 return -1;
877 }
878
Philipp Maier87bd9be2017-08-22 16:35:41 +0200879 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200880 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100881 "endpoint:0x%x destination IP-address is invalid\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200882 ENDPOINT_NUMBER(endp));
883 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200884 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200885
886 if (conn->end.rtp_port == 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200887 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100888 "endpoint:0x%x destination rtp port is invalid\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200889 ENDPOINT_NUMBER(endp));
890 return -1;
891 }
892
893 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200894}
895
Philipp Maier87bd9be2017-08-22 16:35:41 +0200896/* Receive RTP data from a specified source connection and dispatch it to a
897 * destination connection. */
898static int mgcp_recv(int *proto, struct sockaddr_in *addr, char *buf,
899 unsigned int buf_size, struct osmo_fd *fd)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200900{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200901 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200902 struct mgcp_conn_rtp *conn;
903 struct mgcp_trunk_config *tcfg;
904 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200905
Philipp Maier87bd9be2017-08-22 16:35:41 +0200906 conn = (struct mgcp_conn_rtp*) fd->data;
907 endp = conn->conn->endp;
908 tcfg = endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200909
Philipp Maier230e4fc2017-11-28 09:38:45 +0100910 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x receiving RTP/RTCP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200911 ENDPOINT_NUMBER(endp));
912
913 rc = receive_from(endp, fd->fd, addr, buf, buf_size);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200914 if (rc <= 0)
915 return -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200916 *proto = fd == &conn->end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200917
Philipp Maier230e4fc2017-11-28 09:38:45 +0100918 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x ", ENDPOINT_NUMBER(endp));
Neels Hofmeyr56725632018-01-15 15:15:07 +0100919 LOGPC(DRTP, LOGL_DEBUG, "receiving from %s %s %d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200920 conn->conn->name, inet_ntoa(addr->sin_addr),
921 ntohs(addr->sin_port));
Philipp Maier230e4fc2017-11-28 09:38:45 +0100922 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x conn:%s\n", ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200923 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200924
Philipp Maier87bd9be2017-08-22 16:35:41 +0200925 /* Check if the origin of the RTP packet seems plausible */
926 if (tcfg->rtp_accept_all == 0) {
927 if (check_rtp_origin(conn, addr) != 0)
928 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200929 }
930
Philipp Maier87bd9be2017-08-22 16:35:41 +0200931 /* Filter out dummy message */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200932 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Philipp Maierc3413882017-10-27 12:26:54 +0200933 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100934 "endpoint:0x%x dummy message received\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200935 ENDPOINT_NUMBER(endp));
Philipp Maierc3413882017-10-27 12:26:54 +0200936 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100937 "endpoint:0x%x packet tossed\n", ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200938 return 0;
939 }
940
Philipp Maier87bd9be2017-08-22 16:35:41 +0200941 /* Increment RX statistics */
Philipp Maiercede2a42018-07-03 14:14:21 +0200942 rate_ctr_inc(&conn->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR]);
943 rate_ctr_add(&conn->rate_ctr_group->ctr[RTP_OCTETS_RX_CTR], rc);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200944
Philipp Maier87bd9be2017-08-22 16:35:41 +0200945 /* Forward a copy of the RTP data to a debug ip/port */
946 forward_data(fd->fd, &conn->tap_in, buf, rc);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200947
Philipp Maier87bd9be2017-08-22 16:35:41 +0200948 return rc;
949}
950
951/* Send RTP data. Possible options are standard RTP packet
952 * transmission or trsmission via an osmux connection */
953static int mgcp_send_rtp(int proto, struct sockaddr_in *addr, char *buf,
954 unsigned int buf_size,
955 struct mgcp_conn_rtp *conn_src,
956 struct mgcp_conn_rtp *conn_dst)
957{
958 struct mgcp_endpoint *endp;
959 endp = conn_src->conn->endp;
960
Philipp Maier230e4fc2017-11-28 09:38:45 +0100961 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x destin conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200962 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_dst->conn));
963
964 /* Before we try to deliver the packet, we check if the destination
965 * port and IP-Address make sense at all. If not, we will be unable
966 * to deliver the packet. */
967 if (check_rtp_destin(conn_dst) != 0)
968 return -1;
969
970 /* Depending on the RTP connection type, deliver the RTP packet to the
971 * destination connection. */
972 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200973 case MGCP_RTP_DEFAULT:
Philipp Maierc3413882017-10-27 12:26:54 +0200974 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100975 "endpoint:0x%x endpoint type is MGCP_RTP_DEFAULT, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200976 "using mgcp_send() to forward data directly\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200977 ENDPOINT_NUMBER(endp));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200978 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
979 addr, buf, buf_size, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200980 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200981 case MGCP_OSMUX_BSC:
Philipp Maierc3413882017-10-27 12:26:54 +0200982 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100983 "endpoint:0x%x endpoint type is MGCP_OSMUX_BSC_NAT, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200984 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n",
985 ENDPOINT_NUMBER(endp));
986 return osmux_xfrm_to_osmux(buf, buf_size, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200987 }
988
Philipp Maier87bd9be2017-08-22 16:35:41 +0200989 /* If the data has not been handled/forwarded until here, it will
990 * be discarded, this should not happen, normally the MGCP type
991 * should be properly set */
Philipp Maierc3413882017-10-27 12:26:54 +0200992 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100993 "endpoint:0x%x bad MGCP type -- data discarded!\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200994 ENDPOINT_NUMBER(endp));
995
996 return -1;
997}
998
999/*! dispatch incoming RTP packet to opposite RTP connection.
1000 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP)
1001 * \param[in] addr socket address where the RTP packet has been received from
1002 * \param[in] buf buffer that hold the RTP payload
1003 * \param[in] buf_size size data length of buf
1004 * \param[in] conn originating connection
1005 * \returns 0 on success, -1 on ERROR */
1006int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf,
1007 unsigned int buf_size, struct mgcp_conn *conn)
1008{
1009 struct mgcp_conn *conn_dst;
1010 struct mgcp_endpoint *endp;
1011 endp = conn->endp;
1012
1013 /*! NOTE: This callback function implements the endpoint specific
1014 * dispatch bahviour of an rtp bridge/proxy endpoint. It is assumed
1015 * that the endpoint will hold only two connections. This premise
1016 * is used to determine the opposite connection (it is always the
1017 * connection that is not the originating connection). Once the
1018 * destination connection is known the RTP packet is sent via
1019 * the destination connection. */
1020
1021 /* Find a destination connection. */
1022 /* NOTE: This code path runs every time an RTP packet is received. The
1023 * function mgcp_find_dst_conn() we use to determine the detination
1024 * connection will iterate the connection list inside the endpoint.
1025 * Since list iterations are quite costly, we will figure out the
1026 * destination only once and use the optional private data pointer of
1027 * the connection to cache the destination connection pointer. */
1028 if (!conn->priv) {
1029 conn_dst = mgcp_find_dst_conn(conn);
1030 conn->priv = conn_dst;
1031 } else {
1032 conn_dst = (struct mgcp_conn *)conn->priv;
1033 }
1034
1035 /* There is no destination conn, stop here */
1036 if (!conn_dst) {
Philipp Maierc3413882017-10-27 12:26:54 +02001037 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001038 "endpoint:0x%x unable to find destination conn\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001039 ENDPOINT_NUMBER(endp));
1040 return -1;
1041 }
1042
1043 /* The destination conn is not an RTP connection */
1044 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Philipp Maierc3413882017-10-27 12:26:54 +02001045 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001046 "endpoint:0x%x unable to find suitable destination conn\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001047 ENDPOINT_NUMBER(endp));
1048 return -1;
1049 }
1050
1051 /* Dispatch RTP packet to destination RTP connection */
1052 return mgcp_send_rtp(proto, addr, buf,
1053 buf_size, &conn->u.rtp, &conn_dst->u.rtp);
1054
1055}
1056
Philipp Maierdf5d2192018-01-24 11:39:32 +01001057/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1058 * \param[in] endp Endpoint on which the connection resides.
1059 * \param[in] conn Connection that is about to be removed (ignored).
1060 * \returns 0 on success, -1 on ERROR. */
1061void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1062{
1063 struct mgcp_conn *conn_cleanup;
1064
1065 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1066 * pointer to the destination connection, so that we do not have
1067 * to go through the list every time an RTP packet arrives. To prevent
1068 * a use-after-free situation we invalidate this information for all
1069 * connections present when one connection is removed from the
1070 * endpoint. */
1071 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
1072 conn_cleanup->priv = NULL;
1073 }
1074}
1075
Philipp Maier87bd9be2017-08-22 16:35:41 +02001076/* Handle incoming RTP data from NET */
1077static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1078{
1079 /* NOTE: This is a generic implementation. RTP data is received. In
1080 * case of loopback the data is just sent back to its origin. All
1081 * other cases implement endpoint specific behaviour (e.g. how is the
1082 * destination connection determined?). That specific behaviour is
1083 * implemented by the callback function that is called at the end of
1084 * the function */
1085
1086 struct mgcp_conn_rtp *conn_src;
1087 struct mgcp_endpoint *endp;
1088 struct sockaddr_in addr;
1089
1090 char buf[RTP_BUF_SIZE];
1091 int proto;
Philipp Maierb9694552017-11-08 16:53:57 +01001092 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001093
1094 conn_src = (struct mgcp_conn_rtp *)fd->data;
1095 OSMO_ASSERT(conn_src);
1096 endp = conn_src->conn->endp;
1097 OSMO_ASSERT(endp);
1098
Philipp Maier230e4fc2017-11-28 09:38:45 +01001099 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x source conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001100 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_src->conn));
1101
1102 /* Receive packet */
Philipp Maierb9694552017-11-08 16:53:57 +01001103 len = mgcp_recv(&proto, &addr, buf, sizeof(buf), fd);
1104 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001105 return -1;
1106
1107 /* Check if the connection is in loopback mode, if yes, just send the
1108 * incoming data back to the origin */
Philipp Maier5dbfc782017-12-12 16:20:25 +01001109
Philipp Maier87bd9be2017-08-22 16:35:41 +02001110 if (conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Philipp Maier5dbfc782017-12-12 16:20:25 +01001111 /* When we are in loopback mode, we loop back all incoming
1112 * packets back to their origin. We will use the originating
1113 * address data from the UDP packet header to patch the
1114 * outgoing address in connection on the fly */
1115 if (conn_src->end.rtp_port == 0) {
1116 conn_src->end.addr = addr.sin_addr;
1117 conn_src->end.rtp_port = addr.sin_port;
1118 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001119 return mgcp_send_rtp(proto, &addr, buf,
Philipp Maierb9694552017-11-08 16:53:57 +01001120 len, conn_src, conn_src);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001121 }
1122
1123 /* Execute endpoint specific implementation that handles the
1124 * dispatching of the RTP data */
Philipp Maierb9694552017-11-08 16:53:57 +01001125 return endp->type->dispatch_rtp_cb(proto, &addr, buf, len,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001126 conn_src->conn);
1127}
1128
1129/*! set IP Type of Service parameter.
1130 * \param[in] fd associated file descriptor
1131 * \param[in] tos dscp value
1132 * \returns 0 on success, -1 on ERROR */
1133int mgcp_set_ip_tos(int fd, int tos)
1134{
1135 int ret;
1136 ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1137
1138 if (ret < 0)
1139 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001140 return 0;
1141}
1142
Philipp Maier87bd9be2017-08-22 16:35:41 +02001143/*! bind RTP port to osmo_fd.
1144 * \param[in] source_addr source (local) address to bind on
1145 * \param[in] fd associated file descriptor
1146 * \param[in] port to bind on
1147 * \returns 0 on success, -1 on ERROR */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001148int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
1149{
Harald Welte8890dfa2017-11-17 15:09:30 +01001150 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001151
Harald Welte8890dfa2017-11-17 15:09:30 +01001152 rc = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
1153 NULL, 0, OSMO_SOCK_F_BIND);
1154 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001155 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001156 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001157 return -1;
1158 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001159 fd->fd = rc;
1160 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001161
1162 return 0;
1163}
1164
Philipp Maier87bd9be2017-08-22 16:35:41 +02001165/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001166static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001167 struct mgcp_rtp_end *rtp_end, int endpno)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001168{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001169 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1170 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1171
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001172 if (mgcp_create_bind(source_addr, &rtp_end->rtp,
1173 rtp_end->local_port) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001174 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001175 "endpoint:0x%x failed to create RTP port: %s:%d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001176 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001177 goto cleanup0;
1178 }
1179
1180 if (mgcp_create_bind(source_addr, &rtp_end->rtcp,
1181 rtp_end->local_port + 1) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001182 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001183 "endpoint:0x%x failed to create RTCP port: %s:%d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001184 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001185 goto cleanup1;
1186 }
1187
Philipp Maier87bd9be2017-08-22 16:35:41 +02001188 /* Set Type of Service (DSCP-Value) as configured via VTY */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001189 mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
1190 mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
1191
1192 rtp_end->rtp.when = BSC_FD_READ;
1193 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001194 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001195 "endpoint:0x%x failed to register RTP port %d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001196 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001197 goto cleanup2;
1198 }
1199
1200 rtp_end->rtcp.when = BSC_FD_READ;
1201 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001202 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001203 "endpoint:0x%x failed to register RTCP port %d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001204 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001205 goto cleanup3;
1206 }
1207
1208 return 0;
1209
1210cleanup3:
1211 osmo_fd_unregister(&rtp_end->rtp);
1212cleanup2:
1213 close(rtp_end->rtcp.fd);
1214 rtp_end->rtcp.fd = -1;
1215cleanup1:
1216 close(rtp_end->rtp.fd);
1217 rtp_end->rtp.fd = -1;
1218cleanup0:
1219 return -1;
1220}
1221
Philipp Maier87bd9be2017-08-22 16:35:41 +02001222/*! bind RTP port to endpoint/connection.
1223 * \param[in] endp endpoint that holds the RTP connection
1224 * \param[in] rtp_port port number to bind on
1225 * \param[in] conn associated RTP connection
1226 * \returns 0 on success, -1 on ERROR */
1227int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1228 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001229{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001230 char name[512];
1231 struct mgcp_rtp_end *end;
Philipp Maier1cb1e382017-11-02 17:16:04 +01001232 char local_ip_addr[INET_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001233
Philipp Maier01d24a32017-11-21 17:26:09 +01001234 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001235 end = &conn->end;
1236
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001237 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Philipp Maierc3413882017-10-27 12:26:54 +02001238 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001239 "endpoint:0x%x %u was already bound on conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001240 ENDPOINT_NUMBER(endp), rtp_port,
1241 mgcp_conn_dump(conn->conn));
1242
1243 /* Double bindings should never occour! Since we always allocate
1244 * connections dynamically and free them when they are not
1245 * needed anymore, there must be no previous binding leftover.
1246 * Should there be a connection bound twice, we have a serious
1247 * problem and must exit immediately! */
1248 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001249 }
1250
1251 end->local_port = rtp_port;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001252 end->rtp.cb = rtp_data_net;
1253 end->rtp.data = conn;
1254 end->rtcp.data = conn;
1255 end->rtcp.cb = rtp_data_net;
1256
Philipp Maier1cb1e382017-11-02 17:16:04 +01001257 mgcp_get_local_addr(local_ip_addr, conn);
1258
1259 return bind_rtp(endp->cfg, local_ip_addr, end,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001260 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001261}
1262
Philipp Maier87bd9be2017-08-22 16:35:41 +02001263/*! free allocated RTP and RTCP ports.
1264 * \param[in] end RTP end */
1265void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001266{
1267 if (end->rtp.fd != -1) {
1268 close(end->rtp.fd);
1269 end->rtp.fd = -1;
1270 osmo_fd_unregister(&end->rtp);
1271 }
1272
1273 if (end->rtcp.fd != -1) {
1274 close(end->rtcp.fd);
1275 end->rtcp.fd = -1;
1276 osmo_fd_unregister(&end->rtcp);
1277 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001278}