blob: c56e433e608abe87884f66a8540b285bf9d138aa [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 Maier7181cc12018-03-28 16:20:14 +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 Maier7181cc12018-03-28 16:20:14 +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 {
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) {
Philipp Maier7181cc12018-03-28 16:20:14 +0200508 /* FIXME: Move this initialization to mgcp.conn.c */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200509 state->initialized = 1;
510 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100511 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200512 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200513 state->packet_duration =
514 mgcp_rtp_packet_duration(endp, rtp_end);
Philipp Maier7181cc12018-03-28 16:20:14 +0200515 state->out_stream.last_seq = seq - 1;
516 state->out_stream.ssrc = state->patch.orig_ssrc = ssrc;
517 state->out_stream.last_tsdelta = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200518 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200519 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Philipp Maierc3413882017-10-27 12:26:54 +0200520 LOGP(DRTP, LOGL_INFO,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100521 "endpoint:0x%x initializing stream, SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200522 "pkt-duration: %d, from %s:%d\n",
523 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100524 state->patch.seq_offset, state->packet_duration,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200525 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200526 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200527 state->packet_duration =
528 rtp_end->codec.rate * 20 / 1000;
Philipp Maierc3413882017-10-27 12:26:54 +0200529 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100530 "endpoint:0x%x fixed packet duration is not available, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200531 "using fixed 20ms instead: %d from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200532 ENDPOINT_NUMBER(endp), state->packet_duration,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200533 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200534 }
535 } else if (state->in_stream.ssrc != ssrc) {
Philipp Maierc3413882017-10-27 12:26:54 +0200536 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100537 "endpoint:0x%x SSRC changed: %u -> %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200538 "from %s:%d\n",
539 ENDPOINT_NUMBER(endp),
540 state->in_stream.ssrc, rtp_hdr->ssrc,
541 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200542
543 state->in_stream.ssrc = ssrc;
544 if (rtp_end->force_constant_ssrc) {
545 int16_t delta_seq;
546
547 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100548 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200549 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200550
551 /* Estimate number of packets that would have been sent */
552 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200553 (arrival_time - state->in_stream.last_arrival_time
554 + state->packet_duration / 2) /
555 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200556
557 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
558 delta_seq, timestamp);
559
Harald Welte33381352017-12-25 09:44:26 +0100560 state->patch.patch_ssrc = 1;
561 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200562 if (rtp_end->force_constant_ssrc != -1)
563 rtp_end->force_constant_ssrc -= 1;
564
Philipp Maierc3413882017-10-27 12:26:54 +0200565 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100566 "endpoint:0x%x SSRC patching enabled, SSRC: %u "
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200567 "SeqNo offset: %d, TS offset: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200568 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200569 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100570 state->patch.seq_offset, state->patch.timestamp_offset,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200571 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200572 }
573
574 state->in_stream.last_tsdelta = 0;
575 } else {
576 /* Compute current per-packet timestamp delta */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200577 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
578 addr, seq, timestamp, "input",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200579 &state->in_stream.last_tsdelta);
580
Harald Welte33381352017-12-25 09:44:26 +0100581 if (state->patch.patch_ssrc)
582 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200583 }
584
585 /* Save before patching */
586 state->in_stream.last_timestamp = timestamp;
587 state->in_stream.last_seq = seq;
588 state->in_stream.last_arrival_time = arrival_time;
589
590 if (rtp_end->force_aligned_timing &&
591 state->out_stream.ssrc == ssrc && state->packet_duration)
592 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200593 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
594 timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200595
596 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100597 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200598 rtp_hdr->ssrc = htonl(ssrc);
599
600 /* Apply the offset and store it back to the packet.
601 * This won't change anything if the offset is 0, so the conditional is
602 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100603 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200604 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100605 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200606 rtp_hdr->timestamp = htonl(timestamp);
607
608 /* Check again, whether the timestamps are still valid */
609 if (state->out_stream.ssrc == ssrc)
610 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
611 addr, seq, timestamp, "output",
612 &state->out_stream.last_tsdelta);
613
614 /* Save output values */
615 state->out_stream.last_seq = seq;
616 state->out_stream.last_timestamp = timestamp;
617 state->out_stream.ssrc = ssrc;
618
619 if (payload < 0)
620 return;
621
622#if 0
Philipp Maierc3413882017-10-27 12:26:54 +0200623 DEBUGP(DRTP,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100624 "endpoint:0x%x payload hdr payload %u -> endp payload %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200625 ENDPOINT_NUMBER(endp), rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200626 rtp_hdr->payload_type = payload;
627#endif
628}
629
Philipp Maier87bd9be2017-08-22 16:35:41 +0200630/* Forward data to a debug tap. This is debug function that is intended for
631 * debugging the voice traffic with tools like gstreamer */
Philipp Maiere6f172d2017-11-07 12:00:01 +0100632static void forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf,
633 int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200634{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100635 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200636
Philipp Maiere6f172d2017-11-07 12:00:01 +0100637 if (!tap->enabled)
638 return;
639
640 rc = sendto(fd, buf, len, 0, (struct sockaddr *)&tap->forward,
641 sizeof(tap->forward));
642
643 if (rc < 0)
644 LOGP(DRTP, LOGL_ERROR,
645 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200646}
647
Philipp Maier87bd9be2017-08-22 16:35:41 +0200648/*! Send RTP/RTCP data to a specified destination connection.
649 * \param[in] endp associated endpoint (for configuration, logging)
650 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP
651 * \param[in] spoofed source address (set to NULL to disable)
652 * \param[in] buf buffer that contains the RTP/RTCP data
653 * \param[in] len length of the buffer that contains the RTP/RTCP data
654 * \param[in] conn_src associated source connection
655 * \param[in] conn_dst associated destination connection
656 * \returns 0 on success, -1 on ERROR */
657int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
658 char *buf, int len, struct mgcp_conn_rtp *conn_src,
659 struct mgcp_conn_rtp *conn_dst)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200660{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200661 /*! When no destination connection is available (e.g. when only one
662 * connection in loopback mode exists), then the source connection
663 * shall be specified as destination connection */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200664
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200665 struct mgcp_trunk_config *tcfg = endp->tcfg;
666 struct mgcp_rtp_end *rtp_end;
667 struct mgcp_rtp_state *rtp_state;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200668 char *dest_name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200669
Philipp Maier87bd9be2017-08-22 16:35:41 +0200670 OSMO_ASSERT(conn_src);
671 OSMO_ASSERT(conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200672
Philipp Maier87bd9be2017-08-22 16:35:41 +0200673 if (is_rtp) {
Philipp Maierc3413882017-10-27 12:26:54 +0200674 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100675 "endpoint:0x%x delivering RTP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200676 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200677 } else {
Philipp Maierc3413882017-10-27 12:26:54 +0200678 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100679 "endpoint:0x%x delivering RTCP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200680 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200681 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200682
Philipp Maierc3413882017-10-27 12:26:54 +0200683 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100684 "endpoint:0x%x loop:%d, mode:%d ",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200685 ENDPOINT_NUMBER(endp), tcfg->audio_loop, conn_src->conn->mode);
686 if (conn_src->conn->mode == MGCP_CONN_LOOPBACK)
Philipp Maierc3413882017-10-27 12:26:54 +0200687 LOGPC(DRTP, LOGL_DEBUG, "(loopback)\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200688 else
Philipp Maierc3413882017-10-27 12:26:54 +0200689 LOGPC(DRTP, LOGL_DEBUG, "\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200690
Philipp Maier87bd9be2017-08-22 16:35:41 +0200691 /* Note: In case of loopback configuration, both, the source and the
692 * destination will point to the same connection. */
693 rtp_end = &conn_dst->end;
694 rtp_state = &conn_src->state;
695 dest_name = conn_dst->conn->name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200696
697 if (!rtp_end->output_enabled) {
Harald Weltea0ac30f2017-12-25 09:52:30 +0100698 rtp_end->stats.dropped_packets += 1;
Philipp Maierc3413882017-10-27 12:26:54 +0200699 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100700 "endpoint:0x%x output disabled, drop to %s %s "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200701 "rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200702 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200703 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200704 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200705 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200706 );
707 } else if (is_rtp) {
708 int cont;
709 int nbytes = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200710 int buflen = len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200711 do {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200712 /* Run transcoder */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200713 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200714 buf, &buflen,
715 RTP_BUF_SIZE);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200716 if (cont < 0)
717 break;
718
Philipp Maier87bd9be2017-08-22 16:35:41 +0200719 if (addr)
720 mgcp_patch_and_count(endp, rtp_state, rtp_end,
721 addr, buf, buflen);
Philipp Maierc3413882017-10-27 12:26:54 +0200722 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100723 "endpoint:0x%x process/send to %s %s "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200724 "rtp_port:%u rtcp_port:%u\n",
725 ENDPOINT_NUMBER(endp), dest_name,
726 inet_ntoa(rtp_end->addr), ntohs(rtp_end->rtp_port),
727 ntohs(rtp_end->rtcp_port)
728 );
729
730 /* Forward a copy of the RTP data to a debug ip/port */
731 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
732 buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200733
734 /* FIXME: HACK HACK HACK. See OS#2459.
735 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
736 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
737 * cells (as long as we patch only the first RTP payload in each stream).
738 */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200739 if (!rtp_state->patched_first_rtp_payload) {
740 uint8_t *data = (uint8_t *) & buf[12];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200741 data[0] = 0xe4;
742 data[1] = 0x00;
743 rtp_state->patched_first_rtp_payload = true;
744 }
745
Philipp Maier87bd9be2017-08-22 16:35:41 +0200746 len = mgcp_udp_send(rtp_end->rtp.fd,
747 &rtp_end->addr,
748 rtp_end->rtp_port, buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200749
Philipp Maier87bd9be2017-08-22 16:35:41 +0200750 if (len <= 0)
751 return len;
752
Harald Weltea0ac30f2017-12-25 09:52:30 +0100753 conn_dst->end.stats.packets_tx += 1;
754 conn_dst->end.stats.octets_tx += len;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200755
756 nbytes += len;
757 buflen = cont;
758 } while (buflen > 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200759 return nbytes;
760 } else if (!tcfg->omit_rtcp) {
Philipp Maierc3413882017-10-27 12:26:54 +0200761 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100762 "endpoint:0x%x send to %s %s rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200763 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200764 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200765 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200766 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200767 );
768
Philipp Maier87bd9be2017-08-22 16:35:41 +0200769 len = mgcp_udp_send(rtp_end->rtcp.fd,
770 &rtp_end->addr,
771 rtp_end->rtcp_port, buf, len);
772
Harald Weltea0ac30f2017-12-25 09:52:30 +0100773 conn_dst->end.stats.packets_tx += 1;
774 conn_dst->end.stats.octets_tx += len;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200775
776 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200777 }
778
779 return 0;
780}
781
Philipp Maier87bd9be2017-08-22 16:35:41 +0200782/* Helper function for mgcp_recv(),
783 Receive one RTP Packet + Originating address from file descriptor */
784static int receive_from(struct mgcp_endpoint *endp, int fd,
785 struct sockaddr_in *addr, char *buf, int bufsize)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200786{
787 int rc;
788 socklen_t slen = sizeof(*addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200789 struct sockaddr_in addr_sink;
790 char buf_sink[RTP_BUF_SIZE];
791 bool tossed = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200792
Philipp Maier87bd9be2017-08-22 16:35:41 +0200793 if (!addr)
794 addr = &addr_sink;
795 if (!buf) {
796 tossed = true;
797 buf = buf_sink;
798 bufsize = sizeof(buf_sink);
799 }
800
801 rc = recvfrom(fd, buf, bufsize, 0, (struct sockaddr *)addr, &slen);
802
Philipp Maierc3413882017-10-27 12:26:54 +0200803 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200804 "receiving %u bytes length packet from %s:%u ...\n",
805 rc, inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
806
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200807 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200808 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100809 "endpoint:0x%x failed to receive packet, errno: %d/%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200810 ENDPOINT_NUMBER(endp), errno, strerror(errno));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200811 return -1;
812 }
813
Philipp Maier87bd9be2017-08-22 16:35:41 +0200814 if (tossed) {
Philipp Maier230e4fc2017-11-28 09:38:45 +0100815 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200816 ENDPOINT_NUMBER(endp));
817 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200818
819 return rc;
820}
821
Philipp Maier87bd9be2017-08-22 16:35:41 +0200822/* Check if the origin (addr) matches the address/port data of the RTP
823 * connections. */
824static int check_rtp_origin(struct mgcp_conn_rtp *conn,
825 struct sockaddr_in *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200826{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200827 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200828 endp = conn->conn->endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200829
Philipp Maier87bd9be2017-08-22 16:35:41 +0200830 /* Note: Check if the inbound RTP data comes from the same host to
831 * which we send our outgoing RTP traffic. */
832 if (memcmp(&addr->sin_addr, &conn->end.addr, sizeof(addr->sin_addr))
833 != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200834 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100835 "endpoint:0x%x data from wrong address: %s, ",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200836 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr));
Philipp Maierc3413882017-10-27 12:26:54 +0200837 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200838 inet_ntoa(conn->end.addr));
Philipp Maier230e4fc2017-11-28 09:38:45 +0100839 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200840 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200841 return -1;
842 }
843
Philipp Maier87bd9be2017-08-22 16:35:41 +0200844 /* Note: Usually the remote remote port of the data we receive will be
845 * the same as the remote port where we transmit outgoing RTP traffic
846 * to (set by MDCX). We use this to check the origin of the data for
847 * plausibility. */
848 if (conn->end.rtp_port != addr->sin_port &&
849 conn->end.rtcp_port != addr->sin_port) {
Philipp Maierc3413882017-10-27 12:26:54 +0200850 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100851 "endpoint:0x%x data from wrong source port: %d, ",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200852 ENDPOINT_NUMBER(endp), ntohs(addr->sin_port));
Philipp Maierc3413882017-10-27 12:26:54 +0200853 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200854 "expected: %d for RTP or %d for RTCP\n",
855 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Philipp Maier230e4fc2017-11-28 09:38:45 +0100856 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200857 ENDPOINT_NUMBER(endp));
858 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200859 }
860
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200861 return 0;
862}
863
Philipp Maier87bd9be2017-08-22 16:35:41 +0200864/* Check the if the destination address configuration of an RTP connection
865 * makes sense */
866static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200867{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200868 struct mgcp_endpoint *endp;
869 endp = conn->conn->endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200870
Philipp Maier87bd9be2017-08-22 16:35:41 +0200871 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200872 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100873 "endpoint:0x%x destination IP-address is invalid\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200874 ENDPOINT_NUMBER(endp));
875 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200876 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200877
878 if (conn->end.rtp_port == 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200879 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100880 "endpoint:0x%x destination rtp port is invalid\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200881 ENDPOINT_NUMBER(endp));
882 return -1;
883 }
884
885 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200886}
887
Philipp Maier87bd9be2017-08-22 16:35:41 +0200888/* Receive RTP data from a specified source connection and dispatch it to a
889 * destination connection. */
890static int mgcp_recv(int *proto, struct sockaddr_in *addr, char *buf,
891 unsigned int buf_size, struct osmo_fd *fd)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200892{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200893 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200894 struct mgcp_conn_rtp *conn;
895 struct mgcp_trunk_config *tcfg;
896 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200897
Philipp Maier87bd9be2017-08-22 16:35:41 +0200898 conn = (struct mgcp_conn_rtp*) fd->data;
899 endp = conn->conn->endp;
900 tcfg = endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200901
Philipp Maier230e4fc2017-11-28 09:38:45 +0100902 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x receiving RTP/RTCP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200903 ENDPOINT_NUMBER(endp));
904
905 rc = receive_from(endp, fd->fd, addr, buf, buf_size);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200906 if (rc <= 0)
907 return -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200908 *proto = fd == &conn->end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200909
Philipp Maier230e4fc2017-11-28 09:38:45 +0100910 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x ", ENDPOINT_NUMBER(endp));
Neels Hofmeyr56725632018-01-15 15:15:07 +0100911 LOGPC(DRTP, LOGL_DEBUG, "receiving from %s %s %d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200912 conn->conn->name, inet_ntoa(addr->sin_addr),
913 ntohs(addr->sin_port));
Philipp Maier230e4fc2017-11-28 09:38:45 +0100914 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x conn:%s\n", ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200915 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200916
Philipp Maier87bd9be2017-08-22 16:35:41 +0200917 /* Check if the origin of the RTP packet seems plausible */
918 if (tcfg->rtp_accept_all == 0) {
919 if (check_rtp_origin(conn, addr) != 0)
920 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200921 }
922
Philipp Maier87bd9be2017-08-22 16:35:41 +0200923 /* Filter out dummy message */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200924 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Philipp Maierc3413882017-10-27 12:26:54 +0200925 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100926 "endpoint:0x%x dummy message received\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200927 ENDPOINT_NUMBER(endp));
Philipp Maierc3413882017-10-27 12:26:54 +0200928 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100929 "endpoint:0x%x packet tossed\n", ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200930 return 0;
931 }
932
Philipp Maier87bd9be2017-08-22 16:35:41 +0200933 /* Increment RX statistics */
Harald Weltea0ac30f2017-12-25 09:52:30 +0100934 conn->end.stats.packets_rx += 1;
935 conn->end.stats.octets_rx += rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200936
Philipp Maier87bd9be2017-08-22 16:35:41 +0200937 /* Forward a copy of the RTP data to a debug ip/port */
938 forward_data(fd->fd, &conn->tap_in, buf, rc);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200939
Philipp Maier87bd9be2017-08-22 16:35:41 +0200940 return rc;
941}
942
943/* Send RTP data. Possible options are standard RTP packet
944 * transmission or trsmission via an osmux connection */
945static int mgcp_send_rtp(int proto, struct sockaddr_in *addr, char *buf,
946 unsigned int buf_size,
947 struct mgcp_conn_rtp *conn_src,
948 struct mgcp_conn_rtp *conn_dst)
949{
950 struct mgcp_endpoint *endp;
951 endp = conn_src->conn->endp;
952
Philipp Maier230e4fc2017-11-28 09:38:45 +0100953 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x destin conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200954 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_dst->conn));
955
956 /* Before we try to deliver the packet, we check if the destination
957 * port and IP-Address make sense at all. If not, we will be unable
958 * to deliver the packet. */
959 if (check_rtp_destin(conn_dst) != 0)
960 return -1;
961
962 /* Depending on the RTP connection type, deliver the RTP packet to the
963 * destination connection. */
964 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200965 case MGCP_RTP_DEFAULT:
Philipp Maierc3413882017-10-27 12:26:54 +0200966 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100967 "endpoint:0x%x endpoint type is MGCP_RTP_DEFAULT, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200968 "using mgcp_send() to forward data directly\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200969 ENDPOINT_NUMBER(endp));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200970 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
971 addr, buf, buf_size, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200972 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200973 case MGCP_OSMUX_BSC:
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_OSMUX_BSC_NAT, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200976 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n",
977 ENDPOINT_NUMBER(endp));
978 return osmux_xfrm_to_osmux(buf, buf_size, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200979 }
980
Philipp Maier87bd9be2017-08-22 16:35:41 +0200981 /* If the data has not been handled/forwarded until here, it will
982 * be discarded, this should not happen, normally the MGCP type
983 * should be properly set */
Philipp Maierc3413882017-10-27 12:26:54 +0200984 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100985 "endpoint:0x%x bad MGCP type -- data discarded!\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200986 ENDPOINT_NUMBER(endp));
987
988 return -1;
989}
990
991/*! dispatch incoming RTP packet to opposite RTP connection.
992 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP)
993 * \param[in] addr socket address where the RTP packet has been received from
994 * \param[in] buf buffer that hold the RTP payload
995 * \param[in] buf_size size data length of buf
996 * \param[in] conn originating connection
997 * \returns 0 on success, -1 on ERROR */
998int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf,
999 unsigned int buf_size, struct mgcp_conn *conn)
1000{
1001 struct mgcp_conn *conn_dst;
1002 struct mgcp_endpoint *endp;
1003 endp = conn->endp;
1004
1005 /*! NOTE: This callback function implements the endpoint specific
1006 * dispatch bahviour of an rtp bridge/proxy endpoint. It is assumed
1007 * that the endpoint will hold only two connections. This premise
1008 * is used to determine the opposite connection (it is always the
1009 * connection that is not the originating connection). Once the
1010 * destination connection is known the RTP packet is sent via
1011 * the destination connection. */
1012
1013 /* Find a destination connection. */
1014 /* NOTE: This code path runs every time an RTP packet is received. The
1015 * function mgcp_find_dst_conn() we use to determine the detination
1016 * connection will iterate the connection list inside the endpoint.
1017 * Since list iterations are quite costly, we will figure out the
1018 * destination only once and use the optional private data pointer of
1019 * the connection to cache the destination connection pointer. */
1020 if (!conn->priv) {
1021 conn_dst = mgcp_find_dst_conn(conn);
1022 conn->priv = conn_dst;
1023 } else {
1024 conn_dst = (struct mgcp_conn *)conn->priv;
1025 }
1026
1027 /* There is no destination conn, stop here */
1028 if (!conn_dst) {
Philipp Maierc3413882017-10-27 12:26:54 +02001029 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001030 "endpoint:0x%x unable to find destination conn\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001031 ENDPOINT_NUMBER(endp));
1032 return -1;
1033 }
1034
1035 /* The destination conn is not an RTP connection */
1036 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
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 suitable destination conn\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001039 ENDPOINT_NUMBER(endp));
1040 return -1;
1041 }
1042
1043 /* Dispatch RTP packet to destination RTP connection */
1044 return mgcp_send_rtp(proto, addr, buf,
1045 buf_size, &conn->u.rtp, &conn_dst->u.rtp);
1046
1047}
1048
Philipp Maierdf5d2192018-01-24 11:39:32 +01001049/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1050 * \param[in] endp Endpoint on which the connection resides.
1051 * \param[in] conn Connection that is about to be removed (ignored).
1052 * \returns 0 on success, -1 on ERROR. */
1053void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1054{
1055 struct mgcp_conn *conn_cleanup;
1056
1057 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1058 * pointer to the destination connection, so that we do not have
1059 * to go through the list every time an RTP packet arrives. To prevent
1060 * a use-after-free situation we invalidate this information for all
1061 * connections present when one connection is removed from the
1062 * endpoint. */
1063 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
1064 conn_cleanup->priv = NULL;
1065 }
1066}
1067
Philipp Maier87bd9be2017-08-22 16:35:41 +02001068/* Handle incoming RTP data from NET */
1069static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1070{
1071 /* NOTE: This is a generic implementation. RTP data is received. In
1072 * case of loopback the data is just sent back to its origin. All
1073 * other cases implement endpoint specific behaviour (e.g. how is the
1074 * destination connection determined?). That specific behaviour is
1075 * implemented by the callback function that is called at the end of
1076 * the function */
1077
1078 struct mgcp_conn_rtp *conn_src;
1079 struct mgcp_endpoint *endp;
1080 struct sockaddr_in addr;
1081
1082 char buf[RTP_BUF_SIZE];
1083 int proto;
Philipp Maierb9694552017-11-08 16:53:57 +01001084 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001085
1086 conn_src = (struct mgcp_conn_rtp *)fd->data;
1087 OSMO_ASSERT(conn_src);
1088 endp = conn_src->conn->endp;
1089 OSMO_ASSERT(endp);
1090
Philipp Maier230e4fc2017-11-28 09:38:45 +01001091 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x source conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001092 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_src->conn));
1093
1094 /* Receive packet */
Philipp Maierb9694552017-11-08 16:53:57 +01001095 len = mgcp_recv(&proto, &addr, buf, sizeof(buf), fd);
1096 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001097 return -1;
1098
1099 /* Check if the connection is in loopback mode, if yes, just send the
1100 * incoming data back to the origin */
Philipp Maier5dbfc782017-12-12 16:20:25 +01001101
Philipp Maier87bd9be2017-08-22 16:35:41 +02001102 if (conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Philipp Maier5dbfc782017-12-12 16:20:25 +01001103 /* When we are in loopback mode, we loop back all incoming
1104 * packets back to their origin. We will use the originating
1105 * address data from the UDP packet header to patch the
1106 * outgoing address in connection on the fly */
1107 if (conn_src->end.rtp_port == 0) {
1108 conn_src->end.addr = addr.sin_addr;
1109 conn_src->end.rtp_port = addr.sin_port;
1110 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001111 return mgcp_send_rtp(proto, &addr, buf,
Philipp Maierb9694552017-11-08 16:53:57 +01001112 len, conn_src, conn_src);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001113 }
1114
1115 /* Execute endpoint specific implementation that handles the
1116 * dispatching of the RTP data */
Philipp Maierb9694552017-11-08 16:53:57 +01001117 return endp->type->dispatch_rtp_cb(proto, &addr, buf, len,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001118 conn_src->conn);
1119}
1120
1121/*! set IP Type of Service parameter.
1122 * \param[in] fd associated file descriptor
1123 * \param[in] tos dscp value
1124 * \returns 0 on success, -1 on ERROR */
1125int mgcp_set_ip_tos(int fd, int tos)
1126{
1127 int ret;
1128 ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1129
1130 if (ret < 0)
1131 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001132 return 0;
1133}
1134
Philipp Maier87bd9be2017-08-22 16:35:41 +02001135/*! bind RTP port to osmo_fd.
1136 * \param[in] source_addr source (local) address to bind on
1137 * \param[in] fd associated file descriptor
1138 * \param[in] port to bind on
1139 * \returns 0 on success, -1 on ERROR */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001140int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
1141{
Harald Welte8890dfa2017-11-17 15:09:30 +01001142 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001143
Harald Welte8890dfa2017-11-17 15:09:30 +01001144 rc = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
1145 NULL, 0, OSMO_SOCK_F_BIND);
1146 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001147 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001148 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001149 return -1;
1150 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001151 fd->fd = rc;
1152 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001153
1154 return 0;
1155}
1156
Philipp Maier87bd9be2017-08-22 16:35:41 +02001157/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001158static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001159 struct mgcp_rtp_end *rtp_end, int endpno)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001160{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001161 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1162 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1163
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001164 if (mgcp_create_bind(source_addr, &rtp_end->rtp,
1165 rtp_end->local_port) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001166 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001167 "endpoint:0x%x failed to create RTP port: %s:%d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001168 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001169 goto cleanup0;
1170 }
1171
1172 if (mgcp_create_bind(source_addr, &rtp_end->rtcp,
1173 rtp_end->local_port + 1) != 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 RTCP port: %s:%d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001176 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001177 goto cleanup1;
1178 }
1179
Philipp Maier87bd9be2017-08-22 16:35:41 +02001180 /* Set Type of Service (DSCP-Value) as configured via VTY */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001181 mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
1182 mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
1183
1184 rtp_end->rtp.when = BSC_FD_READ;
1185 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001186 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001187 "endpoint:0x%x failed to register RTP port %d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001188 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001189 goto cleanup2;
1190 }
1191
1192 rtp_end->rtcp.when = BSC_FD_READ;
1193 if (osmo_fd_register(&rtp_end->rtcp) != 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 RTCP port %d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001196 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001197 goto cleanup3;
1198 }
1199
1200 return 0;
1201
1202cleanup3:
1203 osmo_fd_unregister(&rtp_end->rtp);
1204cleanup2:
1205 close(rtp_end->rtcp.fd);
1206 rtp_end->rtcp.fd = -1;
1207cleanup1:
1208 close(rtp_end->rtp.fd);
1209 rtp_end->rtp.fd = -1;
1210cleanup0:
1211 return -1;
1212}
1213
Philipp Maier87bd9be2017-08-22 16:35:41 +02001214/*! bind RTP port to endpoint/connection.
1215 * \param[in] endp endpoint that holds the RTP connection
1216 * \param[in] rtp_port port number to bind on
1217 * \param[in] conn associated RTP connection
1218 * \returns 0 on success, -1 on ERROR */
1219int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1220 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001221{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001222 char name[512];
1223 struct mgcp_rtp_end *end;
Philipp Maier1cb1e382017-11-02 17:16:04 +01001224 char local_ip_addr[INET_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001225
Philipp Maier01d24a32017-11-21 17:26:09 +01001226 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001227 end = &conn->end;
1228
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001229 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Philipp Maierc3413882017-10-27 12:26:54 +02001230 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001231 "endpoint:0x%x %u was already bound on conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001232 ENDPOINT_NUMBER(endp), rtp_port,
1233 mgcp_conn_dump(conn->conn));
1234
1235 /* Double bindings should never occour! Since we always allocate
1236 * connections dynamically and free them when they are not
1237 * needed anymore, there must be no previous binding leftover.
1238 * Should there be a connection bound twice, we have a serious
1239 * problem and must exit immediately! */
1240 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001241 }
1242
1243 end->local_port = rtp_port;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001244 end->rtp.cb = rtp_data_net;
1245 end->rtp.data = conn;
1246 end->rtcp.data = conn;
1247 end->rtcp.cb = rtp_data_net;
1248
Philipp Maier1cb1e382017-11-02 17:16:04 +01001249 mgcp_get_local_addr(local_ip_addr, conn);
1250
1251 return bind_rtp(endp->cfg, local_ip_addr, end,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001252 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001253}
1254
Philipp Maier87bd9be2017-08-22 16:35:41 +02001255/*! free allocated RTP and RTCP ports.
1256 * \param[in] end RTP end */
1257void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001258{
1259 if (end->rtp.fd != -1) {
1260 close(end->rtp.fd);
1261 end->rtp.fd = -1;
1262 osmo_fd_unregister(&end->rtp);
1263 }
1264
1265 if (end->rtcp.fd != -1) {
1266 close(end->rtcp.fd);
1267 end->rtcp.fd = -1;
1268 osmo_fd_unregister(&end->rtcp);
1269 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001270}