blob: 83c057007fdfabf8fe9b8eaef55424f8607adce4 [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>
Philipp Maier4dba7692018-08-03 12:20:52 +020035#include <osmocom/core/byteswap.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020036#include <osmocom/netif/rtp.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020037#include <osmocom/mgcp/mgcp.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020038#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020039#include <osmocom/mgcp/mgcp_internal.h>
40#include <osmocom/mgcp/mgcp_stat.h>
41#include <osmocom/mgcp/osmux.h>
42#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010043#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maier6931f9a2018-07-26 09:29:31 +020044#include <osmocom/mgcp/mgcp_codec.h>
Philipp Maierc3413882017-10-27 12:26:54 +020045#include <osmocom/mgcp/debug.h>
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +020046#include <osmocom/mgcp/iuup_cn_node.h>
47#include <osmocom/mgcp/iuup_protocol.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020048
Philipp Maier6931f9a2018-07-26 09:29:31 +020049
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020050#define RTP_SEQ_MOD (1 << 16)
51#define RTP_MAX_DROPOUT 3000
52#define RTP_MAX_MISORDER 100
53#define RTP_BUF_SIZE 4096
54
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +020055enum rtp_proto {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020056 MGCP_PROTO_RTP,
57 MGCP_PROTO_RTCP,
58};
59
Neels Hofmeyr85f94012018-10-08 03:36:45 +020060static int rx_rtp(struct msgb *msg);
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +020061
Philipp Maier1cb1e382017-11-02 17:16:04 +010062/*! Determine the local rtp bind IP-address.
63 * \param[out] addr caller provided memory to store the resulting IP-Address
64 * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters
65 *
66 * The local bind IP-address is automatically selected by probing the
67 * IP-Address of the interface that is pointing towards the remote IP-Address,
68 * if no remote IP-Address is known yet, the statically configured
69 * IP-Addresses are used as fallback. */
70void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn)
71{
72
73 struct mgcp_endpoint *endp;
74 int rc;
75 endp = conn->conn->endp;
76
77 /* Try probing the local IP-Address */
78 if (endp->cfg->net_ports.bind_addr_probe && conn->end.addr.s_addr != 0) {
79 rc = osmo_sock_local_ip(addr, inet_ntoa(conn->end.addr));
80 if (rc < 0)
81 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +010082 "endpoint:0x%x CI:%s local interface auto detection failed, using configured addresses...\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +010083 ENDPOINT_NUMBER(endp), conn->conn->id);
84 else {
85 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +010086 "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 +010087 ENDPOINT_NUMBER(endp), conn->conn->id, addr,
88 inet_ntoa(conn->end.addr));
89 return;
90 }
91 }
92
93 /* Select from preconfigured IP-Addresses */
94 if (endp->cfg->net_ports.bind_addr) {
95 /* Check there is a bind IP for the RTP traffic configured,
96 * if so, use that IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +010097 osmo_strlcpy(addr, endp->cfg->net_ports.bind_addr, INET_ADDRSTRLEN);
Philipp Maier1cb1e382017-11-02 17:16:04 +010098 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +010099 "endpoint:0x%x CI:%s using configured rtp bind ip as local bind ip %s\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +0100100 ENDPOINT_NUMBER(endp), conn->conn->id, addr);
101 } else {
102 /* No specific bind IP is configured for the RTP traffic, so
103 * assume the IP where we listen for incoming MGCP messages
104 * as bind IP */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100105 osmo_strlcpy(addr, endp->cfg->source_addr, INET_ADDRSTRLEN);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100106 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100107 "endpoint:0x%x CI:%s using mgcp bind ip as local rtp bind ip: %s\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +0100108 ENDPOINT_NUMBER(endp), conn->conn->id, addr);
109 }
110}
111
Philipp Maier87bd9be2017-08-22 16:35:41 +0200112/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200113 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +0200114 * 1/codec_rate seconds. */
115static uint32_t get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200116{
117 struct timespec tp;
118 uint64_t ret;
119
Philipp Maier87bd9be2017-08-22 16:35:41 +0200120 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200121 return 0;
122
123 memset(&tp, 0, sizeof(tp));
124 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maierc3413882017-10-27 12:26:54 +0200125 LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200126
127 /* convert it to 1/unit seconds */
128 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200129 ret *= codec_rate;
130 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200131
132 return ret;
133}
134
Philipp Maier87bd9be2017-08-22 16:35:41 +0200135/*! send udp packet.
136 * \param[in] fd associated file descriptor
137 * \param[in] addr destination ip-address
138 * \param[in] port destination UDP port
139 * \param[in] buf buffer that holds the data to be send
140 * \param[in] len length of the data to be sent
141 * \returns bytes sent, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200142int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
143{
144 struct sockaddr_in out;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200145
Philipp Maierc3413882017-10-27 12:26:54 +0200146 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200147 "sending %i bytes length packet to %s:%u ...\n",
148 len, inet_ntoa(*addr), ntohs(port));
149
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200150 out.sin_family = AF_INET;
151 out.sin_port = port;
152 memcpy(&out.sin_addr, addr, sizeof(*addr));
153
154 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
155}
156
Philipp Maier87bd9be2017-08-22 16:35:41 +0200157/*! send RTP dummy packet (to keep NAT connection open).
158 * \param[in] endp mcgp endpoint that holds the RTP connection
159 * \param[in] conn associated RTP connection
160 * \returns bytes sent, -1 on error */
161int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200162{
163 static char buf[] = { MGCP_DUMMY_LOAD };
164 int rc;
165 int was_rtcp = 0;
166
Philipp Maier87bd9be2017-08-22 16:35:41 +0200167 OSMO_ASSERT(endp);
168 OSMO_ASSERT(conn);
169
Philipp Maierc3413882017-10-27 12:26:54 +0200170 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100171 "endpoint:0x%x sending dummy packet...\n", ENDPOINT_NUMBER(endp));
172 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200173 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
174
175 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
176 conn->end.rtp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200177
178 if (rc == -1)
179 goto failed;
180
181 if (endp->tcfg->omit_rtcp)
182 return rc;
183
184 was_rtcp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200185 rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
186 conn->end.rtcp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200187
188 if (rc >= 0)
189 return rc;
190
191failed:
Philipp Maierc3413882017-10-27 12:26:54 +0200192 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100193 "endpoint:0x%x Failed to send dummy %s packet.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200194 ENDPOINT_NUMBER(endp), was_rtcp ? "RTCP" : "RTP");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200195
196 return -1;
197}
198
Philipp Maier87bd9be2017-08-22 16:35:41 +0200199/* Compute timestamp alignment error */
200static int32_t ts_alignment_error(struct mgcp_rtp_stream_state *sstate,
201 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200202{
203 int32_t timestamp_delta;
204
205 if (ptime == 0)
206 return 0;
207
208 /* Align according to: T - Tlast = k * Tptime */
209 timestamp_delta = timestamp - sstate->last_timestamp;
210
211 return timestamp_delta % ptime;
212}
213
Philipp Maier87bd9be2017-08-22 16:35:41 +0200214/* Check timestamp and sequence number for plausibility */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200215static int check_rtp_timestamp(struct mgcp_endpoint *endp,
216 struct mgcp_rtp_state *state,
217 struct mgcp_rtp_stream_state *sstate,
218 struct mgcp_rtp_end *rtp_end,
219 struct sockaddr_in *addr,
220 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200221 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200222{
223 int32_t tsdelta;
224 int32_t timestamp_error;
225
226 /* Not fully intialized, skip */
227 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
228 return 0;
229
230 if (seq == sstate->last_seq) {
231 if (timestamp != sstate->last_timestamp) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200232 rate_ctr_inc(sstate->err_ts_ctr);
Philipp Maierc3413882017-10-27 12:26:54 +0200233 LOGP(DRTP, LOGL_ERROR,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200234 "The %s timestamp delta is != 0 but the sequence "
235 "number %d is the same, "
236 "TS offset: %d, SeqNo offset: %d "
237 "on 0x%x SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200238 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200239 text, seq,
Harald Welte33381352017-12-25 09:44:26 +0100240 state->patch.timestamp_offset, state->patch.seq_offset,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200241 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200242 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200243 }
244 return 0;
245 }
246
247 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200248 (int32_t)(timestamp - sstate->last_timestamp) /
249 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200250
251 if (tsdelta == 0) {
252 /* Don't update *tsdelta_out */
Philipp Maierc3413882017-10-27 12:26:54 +0200253 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200254 "The %s timestamp delta is %d "
255 "on 0x%x SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200256 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200257 text, tsdelta,
258 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200259 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200260
261 return 0;
262 }
263
264 if (sstate->last_tsdelta != tsdelta) {
265 if (sstate->last_tsdelta) {
Philipp Maierc3413882017-10-27 12:26:54 +0200266 LOGP(DRTP, LOGL_INFO,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200267 "The %s timestamp delta changes from %d to %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200268 "on 0x%x SSRC: %u timestamp: %u from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200269 text, sstate->last_tsdelta, tsdelta,
270 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200271 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200272 }
273 }
274
275 if (tsdelta_out)
276 *tsdelta_out = tsdelta;
277
278 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200279 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200280
281 if (timestamp_error) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200282 rate_ctr_inc(sstate->err_ts_ctr);
Philipp Maierc3413882017-10-27 12:26:54 +0200283 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200284 "The %s timestamp has an alignment error of %d "
285 "on 0x%x SSRC: %u "
286 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200287 "from %s:%d. ptime: %d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200288 text, timestamp_error,
289 ENDPOINT_NUMBER(endp), sstate->ssrc,
290 (int16_t)(seq - sstate->last_seq),
291 (int32_t)(timestamp - sstate->last_timestamp),
292 tsdelta,
293 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200294 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200295 }
296 return 1;
297}
298
299/* Set the timestamp offset according to the packet duration. */
300static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp,
301 struct mgcp_rtp_state *state,
302 struct mgcp_rtp_end *rtp_end,
303 struct sockaddr_in *addr,
304 int16_t delta_seq, uint32_t in_timestamp)
305{
306 int32_t tsdelta = state->packet_duration;
307 int timestamp_offset;
308 uint32_t out_timestamp;
309
310 if (tsdelta == 0) {
311 tsdelta = state->out_stream.last_tsdelta;
312 if (tsdelta != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200313 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200314 "A fixed packet duration is not available on 0x%x, "
315 "using last output timestamp delta instead: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200316 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200317 ENDPOINT_NUMBER(endp), tsdelta,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200318 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200319 } else {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200320 tsdelta = rtp_end->codec->rate * 20 / 1000;
Philipp Maierc3413882017-10-27 12:26:54 +0200321 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200322 "Fixed packet duration and last timestamp delta "
323 "are not available on 0x%x, "
324 "using fixed 20ms instead: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200325 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200326 ENDPOINT_NUMBER(endp), tsdelta,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200327 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200328 }
329 }
330
331 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
332 timestamp_offset = out_timestamp - in_timestamp;
333
Harald Welte33381352017-12-25 09:44:26 +0100334 if (state->patch.timestamp_offset != timestamp_offset) {
335 state->patch.timestamp_offset = timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200336
Philipp Maierc3413882017-10-27 12:26:54 +0200337 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200338 "Timestamp offset change on 0x%x SSRC: %u "
339 "SeqNo delta: %d, TS offset: %d, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200340 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200341 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100342 delta_seq, state->patch.timestamp_offset,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200343 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200344 }
345
346 return timestamp_offset;
347}
348
349/* Set the timestamp offset according to the packet duration. */
350static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp,
351 struct mgcp_rtp_state *state,
352 struct mgcp_rtp_end *rtp_end,
353 struct sockaddr_in *addr,
354 uint32_t timestamp)
355{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200356 int ts_error = 0;
357 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200358 int ptime = state->packet_duration;
359
360 /* Align according to: T + Toffs - Tlast = k * Tptime */
361
Philipp Maier87bd9be2017-08-22 16:35:41 +0200362 ts_error = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100363 timestamp + state->patch.timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200364
Philipp Maier87bd9be2017-08-22 16:35:41 +0200365 /* If there is an alignment error, we have to compensate it */
366 if (ts_error) {
Harald Welte33381352017-12-25 09:44:26 +0100367 state->patch.timestamp_offset += ptime - ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200368
Philipp Maierc3413882017-10-27 12:26:54 +0200369 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200370 "Corrected timestamp alignment error of %d on 0x%x SSRC: %u "
371 "new TS offset: %d, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200372 "from %s:%d\n",
373 ts_error,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200374 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100375 state->patch.timestamp_offset, inet_ntoa(addr->sin_addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200376 ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200377 }
378
Philipp Maier87bd9be2017-08-22 16:35:41 +0200379 /* Check we really managed to compensate the timestamp
380 * offset. There should not be any remaining error, failing
Harald Welte1d1b98f2017-12-25 10:03:40 +0100381 * here would point to a serous problem with the alignment
382 * error computation function */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200383 ts_check = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100384 timestamp + state->patch.timestamp_offset);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200385 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200386
Philipp Maier87bd9be2017-08-22 16:35:41 +0200387 /* Return alignment error before compensation */
388 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200389}
390
Philipp Maier87bd9be2017-08-22 16:35:41 +0200391/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
392 * \param[in] associated endpoint
393 * \param[in] destination RTP end
394 * \param[in,out] pointer to buffer with voice data
395 * \param[in] voice data length
Harald Welte1d1b98f2017-12-25 10:03:40 +0100396 * \param[in] maximum size of caller provided voice data buffer
Philipp Maier87bd9be2017-08-22 16:35:41 +0200397 * \returns ignores input parameters, return always 0 */
398int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
399 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200400 char *data, int *len, int buf_size)
401{
Philipp Maier230e4fc2017-11-28 09:38:45 +0100402 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x transcoding disabled\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200403 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200404 return 0;
405}
406
Philipp Maier87bd9be2017-08-22 16:35:41 +0200407/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
408 * \param[in] associated endpoint
Philipp Maieracc10352018-07-19 18:07:57 +0200409 * \param[in] destination RTP connnection
410 * \param[in] source RTP connection
Philipp Maier87bd9be2017-08-22 16:35:41 +0200411 * \returns ignores input parameters, return always 0 */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200412int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
Philipp Maieracc10352018-07-19 18:07:57 +0200413 struct mgcp_conn_rtp *conn_dst,
414 struct mgcp_conn_rtp *conn_src)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200415{
Philipp Maier230e4fc2017-11-28 09:38:45 +0100416 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x transcoding disabled\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200417 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200418 return 0;
419}
420
421void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
422 int *payload_type,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200423 const char **audio_name,
424 const char **fmtp_extra,
425 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200426{
Philipp Maierc3413882017-10-27 12:26:54 +0200427 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100428 "endpoint:0x%x conn:%s using format defaults\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200429 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
430
Philipp Maierbc0346e2018-06-07 09:52:16 +0200431 *payload_type = conn->end.codec->payload_type;
432 *audio_name = conn->end.codec->audio_name;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200433 *fmtp_extra = conn->end.fmtp_extra;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200434}
435
Philipp Maier87bd9be2017-08-22 16:35:41 +0200436void mgcp_rtp_annex_count(struct mgcp_endpoint *endp,
437 struct mgcp_rtp_state *state, const uint16_t seq,
438 const int32_t transit, const uint32_t ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200439{
440 int32_t d;
441
442 /* initialize or re-initialize */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100443 if (!state->stats.initialized || state->stats.ssrc != ssrc) {
444 state->stats.initialized = 1;
445 state->stats.base_seq = seq;
446 state->stats.max_seq = seq - 1;
447 state->stats.ssrc = ssrc;
448 state->stats.jitter = 0;
449 state->stats.transit = transit;
450 state->stats.cycles = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200451 } else {
452 uint16_t udelta;
453
Philipp Maier87bd9be2017-08-22 16:35:41 +0200454 /* The below takes the shape of the validation of
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200455 * Appendix A. Check if there is something weird with
456 * the sequence number, otherwise check for a wrap
457 * around in the sequence number.
458 * It can't wrap during the initialization so let's
459 * skip it here. The Appendix A probably doesn't have
Philipp Maier87bd9be2017-08-22 16:35:41 +0200460 * this issue because of the probation. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100461 udelta = seq - state->stats.max_seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200462 if (udelta < RTP_MAX_DROPOUT) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100463 if (seq < state->stats.max_seq)
464 state->stats.cycles += RTP_SEQ_MOD;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200465 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Philipp Maierc3413882017-10-27 12:26:54 +0200466 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200467 "RTP seqno made a very large jump on 0x%x delta: %u\n",
468 ENDPOINT_NUMBER(endp), udelta);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200469 }
470 }
471
Philipp Maier87bd9be2017-08-22 16:35:41 +0200472 /* Calculate the jitter between the two packages. The TS should be
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200473 * taken closer to the read function. This was taken from the
474 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
Philipp Maier87bd9be2017-08-22 16:35:41 +0200475 * resolution. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100476 d = transit - state->stats.transit;
477 state->stats.transit = transit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200478 if (d < 0)
479 d = -d;
Harald Welte49e3d5a2017-12-25 09:47:57 +0100480 state->stats.jitter += d - ((state->stats.jitter + 8) >> 4);
481 state->stats.max_seq = seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200482}
483
Philipp Maier6931f9a2018-07-26 09:29:31 +0200484/* There may be different payload type numbers negotiated for two connections.
485 * Patch the payload type of an RTP packet so that it uses the payload type
486 * that is valid for the destination connection (conn_dst) */
487static int mgcp_patch_pt(struct mgcp_conn_rtp *conn_src,
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200488 struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200489{
490 struct rtp_hdr *rtp_hdr;
491 uint8_t pt_in;
492 int pt_out;
493
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200494 OSMO_ASSERT(msg->len >= sizeof(struct rtp_hdr));
495 rtp_hdr = (struct rtp_hdr *)msg->data;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200496
497 pt_in = rtp_hdr->payload_type;
498 pt_out = mgcp_codec_pt_translate(conn_src, conn_dst, pt_in);
499 if (pt_out < 0)
500 return -EINVAL;
501
502 rtp_hdr->payload_type = (uint8_t) pt_out;
503 return 0;
504}
505
Philipp Maier87bd9be2017-08-22 16:35:41 +0200506/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200507 * some of the supported endpoints (e.g. the nanoBTS) can only handle
508 * one source and this code will patch RTP header to appear as if there
509 * is only one source.
510 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200511 * we receive will be seen as a switch in streams. */
512void mgcp_patch_and_count(struct mgcp_endpoint *endp,
513 struct mgcp_rtp_state *state,
514 struct mgcp_rtp_end *rtp_end,
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200515 struct sockaddr_in *addr, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200516{
517 uint32_t arrival_time;
518 int32_t transit;
519 uint16_t seq;
520 uint32_t timestamp, ssrc;
521 struct rtp_hdr *rtp_hdr;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200522 int payload = rtp_end->codec->payload_type;
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200523 unsigned int len = msg->len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200524
525 if (len < sizeof(*rtp_hdr))
526 return;
527
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200528 rtp_hdr = (struct rtp_hdr *)msg->data;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200529 seq = ntohs(rtp_hdr->sequence);
530 timestamp = ntohl(rtp_hdr->timestamp);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200531 arrival_time = get_current_ts(rtp_end->codec->rate);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200532 ssrc = ntohl(rtp_hdr->ssrc);
533 transit = arrival_time - timestamp;
534
535 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc);
536
537 if (!state->initialized) {
538 state->initialized = 1;
539 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100540 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200541 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200542 state->packet_duration =
543 mgcp_rtp_packet_duration(endp, rtp_end);
Philipp Maier0ec1d4e2018-05-16 11:09:42 +0200544 state->out_stream.last_seq = seq - 1;
545 state->out_stream.ssrc = state->patch.orig_ssrc = ssrc;
546 state->out_stream.last_tsdelta = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200547 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200548 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Philipp Maierc3413882017-10-27 12:26:54 +0200549 LOGP(DRTP, LOGL_INFO,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100550 "endpoint:0x%x initializing stream, SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200551 "pkt-duration: %d, from %s:%d\n",
552 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100553 state->patch.seq_offset, state->packet_duration,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200554 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200555 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200556 state->packet_duration =
Philipp Maierbc0346e2018-06-07 09:52:16 +0200557 rtp_end->codec->rate * 20 / 1000;
Philipp Maierc3413882017-10-27 12:26:54 +0200558 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100559 "endpoint:0x%x fixed packet duration is not available, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200560 "using fixed 20ms instead: %d from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200561 ENDPOINT_NUMBER(endp), state->packet_duration,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200562 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200563 }
564 } else if (state->in_stream.ssrc != ssrc) {
Philipp Maierc3413882017-10-27 12:26:54 +0200565 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100566 "endpoint:0x%x SSRC changed: %u -> %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200567 "from %s:%d\n",
568 ENDPOINT_NUMBER(endp),
569 state->in_stream.ssrc, rtp_hdr->ssrc,
570 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200571
572 state->in_stream.ssrc = ssrc;
573 if (rtp_end->force_constant_ssrc) {
574 int16_t delta_seq;
575
576 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100577 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200578 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200579
580 /* Estimate number of packets that would have been sent */
581 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200582 (arrival_time - state->in_stream.last_arrival_time
583 + state->packet_duration / 2) /
584 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200585
586 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
587 delta_seq, timestamp);
588
Harald Welte33381352017-12-25 09:44:26 +0100589 state->patch.patch_ssrc = 1;
590 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200591 if (rtp_end->force_constant_ssrc != -1)
592 rtp_end->force_constant_ssrc -= 1;
593
Philipp Maierc3413882017-10-27 12:26:54 +0200594 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100595 "endpoint:0x%x SSRC patching enabled, SSRC: %u "
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200596 "SeqNo offset: %d, TS offset: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200597 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200598 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100599 state->patch.seq_offset, state->patch.timestamp_offset,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200600 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200601 }
602
603 state->in_stream.last_tsdelta = 0;
604 } else {
605 /* Compute current per-packet timestamp delta */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200606 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
607 addr, seq, timestamp, "input",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200608 &state->in_stream.last_tsdelta);
609
Harald Welte33381352017-12-25 09:44:26 +0100610 if (state->patch.patch_ssrc)
611 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200612 }
613
614 /* Save before patching */
615 state->in_stream.last_timestamp = timestamp;
616 state->in_stream.last_seq = seq;
617 state->in_stream.last_arrival_time = arrival_time;
618
619 if (rtp_end->force_aligned_timing &&
620 state->out_stream.ssrc == ssrc && state->packet_duration)
621 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200622 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
623 timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200624
625 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100626 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200627 rtp_hdr->ssrc = htonl(ssrc);
628
629 /* Apply the offset and store it back to the packet.
630 * This won't change anything if the offset is 0, so the conditional is
631 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100632 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200633 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100634 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200635 rtp_hdr->timestamp = htonl(timestamp);
636
637 /* Check again, whether the timestamps are still valid */
638 if (state->out_stream.ssrc == ssrc)
639 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
640 addr, seq, timestamp, "output",
641 &state->out_stream.last_tsdelta);
642
643 /* Save output values */
644 state->out_stream.last_seq = seq;
645 state->out_stream.last_timestamp = timestamp;
646 state->out_stream.ssrc = ssrc;
647
648 if (payload < 0)
649 return;
650
651#if 0
Philipp Maierc3413882017-10-27 12:26:54 +0200652 DEBUGP(DRTP,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100653 "endpoint:0x%x payload hdr payload %u -> endp payload %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200654 ENDPOINT_NUMBER(endp), rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200655 rtp_hdr->payload_type = payload;
656#endif
657}
658
Philipp Maier87bd9be2017-08-22 16:35:41 +0200659/* Forward data to a debug tap. This is debug function that is intended for
660 * debugging the voice traffic with tools like gstreamer */
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200661static void forward_data(int fd, struct mgcp_rtp_tap *tap, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200662{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100663 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200664
Philipp Maiere6f172d2017-11-07 12:00:01 +0100665 if (!tap->enabled)
666 return;
667
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200668 rc = sendto(fd, msg->data, msg->len, 0, (struct sockaddr *)&tap->forward,
Philipp Maiere6f172d2017-11-07 12:00:01 +0100669 sizeof(tap->forward));
670
671 if (rc < 0)
672 LOGP(DRTP, LOGL_ERROR,
673 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200674}
675
Philipp Maier87bd9be2017-08-22 16:35:41 +0200676/*! Send RTP/RTCP data to a specified destination connection.
677 * \param[in] endp associated endpoint (for configuration, logging)
678 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP
679 * \param[in] spoofed source address (set to NULL to disable)
680 * \param[in] buf buffer that contains the RTP/RTCP data
681 * \param[in] len length of the buffer that contains the RTP/RTCP data
682 * \param[in] conn_src associated source connection
683 * \param[in] conn_dst associated destination connection
684 * \returns 0 on success, -1 on ERROR */
685int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200686 struct msgb *msg, struct mgcp_conn_rtp *conn_src,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200687 struct mgcp_conn_rtp *conn_dst)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200688{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200689 /*! When no destination connection is available (e.g. when only one
690 * connection in loopback mode exists), then the source connection
691 * shall be specified as destination connection */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200692
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200693 struct mgcp_trunk_config *tcfg = endp->tcfg;
694 struct mgcp_rtp_end *rtp_end;
695 struct mgcp_rtp_state *rtp_state;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200696 char *dest_name;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200697 int rc;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200698 int len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200699
Philipp Maier87bd9be2017-08-22 16:35:41 +0200700 OSMO_ASSERT(conn_src);
701 OSMO_ASSERT(conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200702
Philipp Maier87bd9be2017-08-22 16:35:41 +0200703 if (is_rtp) {
Philipp Maierc3413882017-10-27 12:26:54 +0200704 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100705 "endpoint:0x%x delivering RTP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200706 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200707 } else {
Philipp Maierc3413882017-10-27 12:26:54 +0200708 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100709 "endpoint:0x%x delivering RTCP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200710 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200711 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200712
Philipp Maierc3413882017-10-27 12:26:54 +0200713 LOGP(DRTP, LOGL_DEBUG,
Neels Hofmeyr7066af82018-07-23 18:28:36 +0200714 "endpoint:0x%x loop:%d, mode:%d%s\n",
715 ENDPOINT_NUMBER(endp), tcfg->audio_loop, conn_src->conn->mode,
716 conn_src->conn->mode == MGCP_CONN_LOOPBACK ? " (loopback)" : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200717
Philipp Maier6931f9a2018-07-26 09:29:31 +0200718 /* FIXME: It is legal that the payload type on the egress connection is
719 * different from the payload type that has been negotiated on the
720 * ingress connection. Essentially the codecs are the same so we can
721 * match them and patch the payload type. However, if we can not find
722 * the codec pendant (everything ist equal except the PT), we are of
723 * course unable to patch the payload type. A situation like this
724 * should not occur if transcoding is consequently avoided. Until
725 * we have transcoding support in osmo-mgw we can not resolve this. */
Philipp Maierda895b12018-08-03 12:16:37 +0200726 if (is_rtp) {
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200727 rc = mgcp_patch_pt(conn_src, conn_dst, msg);
Philipp Maierda895b12018-08-03 12:16:37 +0200728 if (rc < 0) {
729 LOGP(DRTP, LOGL_ERROR,
730 "endpoint:0x%x can not patch PT because no suitable egress codec was found.\n",
731 ENDPOINT_NUMBER(endp));
732 }
Philipp Maier6931f9a2018-07-26 09:29:31 +0200733 }
734
Philipp Maier87bd9be2017-08-22 16:35:41 +0200735 /* Note: In case of loopback configuration, both, the source and the
736 * destination will point to the same connection. */
737 rtp_end = &conn_dst->end;
738 rtp_state = &conn_src->state;
739 dest_name = conn_dst->conn->name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200740
741 if (!rtp_end->output_enabled) {
Philipp Maiercede2a42018-07-03 14:14:21 +0200742 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_DROPPED_PACKETS_CTR]);
Philipp Maierc3413882017-10-27 12:26:54 +0200743 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100744 "endpoint:0x%x output disabled, drop to %s %s "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200745 "rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200746 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200747 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200748 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200749 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200750 );
751 } else if (is_rtp) {
752 int cont;
753 int nbytes = 0;
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200754 int buflen = msg->len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200755 do {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200756 /* Run transcoder */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200757 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200758 (char*)msg->data, &buflen,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200759 RTP_BUF_SIZE);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200760 if (cont < 0)
761 break;
762
Philipp Maier87bd9be2017-08-22 16:35:41 +0200763 if (addr)
764 mgcp_patch_and_count(endp, rtp_state, rtp_end,
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200765 addr, msg);
Philipp Maierc3413882017-10-27 12:26:54 +0200766 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100767 "endpoint:0x%x process/send to %s %s "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200768 "rtp_port:%u rtcp_port:%u\n",
769 ENDPOINT_NUMBER(endp), dest_name,
770 inet_ntoa(rtp_end->addr), ntohs(rtp_end->rtp_port),
771 ntohs(rtp_end->rtcp_port)
772 );
773
774 /* Forward a copy of the RTP data to a debug ip/port */
775 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200776 msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200777
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200778#if 0
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200779 /* FIXME: HACK HACK HACK. See OS#2459.
780 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
781 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
782 * cells (as long as we patch only the first RTP payload in each stream).
783 */
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200784 if (!rtp_state->patched_first_rtp_payload
785 && conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200786 uint8_t *data = msg->data + 12;
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200787 if (data[0] == 0xe0) {
788 data[0] = 0xe4;
789 data[1] = 0x00;
790 rtp_state->patched_first_rtp_payload = true;
791 LOGP(DRTP, LOGL_DEBUG,
792 "endpoint:0x%x Patching over first two bytes"
793 " to fake an IuUP Initialization Ack\n",
794 ENDPOINT_NUMBER(endp));
795 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200796 }
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200797#endif
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200798
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200799 if (conn_dst->iuup)
800 len = osmo_iuup_cn_tx_payload(conn_dst->iuup, msg);
801 else
802 len = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr, rtp_end->rtp_port,
803 (char*)msg->data, msg->len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200804
Philipp Maier87bd9be2017-08-22 16:35:41 +0200805 if (len <= 0)
806 return len;
807
Philipp Maiercede2a42018-07-03 14:14:21 +0200808 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
809 rate_ctr_add(&conn_dst->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], len);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200810
811 nbytes += len;
812 buflen = cont;
813 } while (buflen > 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200814 return nbytes;
815 } else if (!tcfg->omit_rtcp) {
Philipp Maierc3413882017-10-27 12:26:54 +0200816 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100817 "endpoint:0x%x send to %s %s rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200818 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200819 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200820 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200821 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200822 );
823
Philipp Maier87bd9be2017-08-22 16:35:41 +0200824 len = mgcp_udp_send(rtp_end->rtcp.fd,
825 &rtp_end->addr,
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200826 rtp_end->rtcp_port, (char*)msg->data, msg->len);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200827
Philipp Maiercede2a42018-07-03 14:14:21 +0200828 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
829 rate_ctr_add(&conn_dst->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], len);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200830
831 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200832 }
833
834 return 0;
835}
836
Philipp Maier87bd9be2017-08-22 16:35:41 +0200837/* Check if the origin (addr) matches the address/port data of the RTP
838 * connections. */
839static int check_rtp_origin(struct mgcp_conn_rtp *conn,
840 struct sockaddr_in *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200841{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200842 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200843 endp = conn->conn->endp;
Neels Hofmeyr0063ca22018-07-23 18:12:16 +0200844
Harald Weltec26b6652018-10-21 12:01:04 +0200845 if (conn->end.addr.s_addr == 0) {
Neels Hofmeyrefd645e2018-09-10 13:14:50 +0200846 switch (conn->conn->mode) {
847 case MGCP_CONN_LOOPBACK:
848 /* HACK: for IuUP, we want to reply with an IuUP Initialization ACK upon the first RTP
849 * message received. We currently hackishly accomplish that by putting the endpoint in
850 * loopback mode and patching over the looped back RTP message to make it look like an
851 * ack. We don't know the femto cell's IP address and port until the RAB Assignment
852 * Response is received, but the nano3G expects an IuUP Initialization Ack before it even
853 * sends the RAB Assignment Response. Hence, if the remote address is 0.0.0.0 and the
854 * MGCP port is in loopback mode, allow looping back the packet to any source. */
855 LOGP(DRTP, LOGL_ERROR,
856 "endpoint:0x%x In loopback mode and remote address not set:"
857 " allowing data from address: %s\n",
858 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr));
859 return 0;
860
861 default:
862 /* Receiving early media before the endpoint is configured. Instead of logging
863 * this as an error that occurs on every call, keep it more low profile to not
864 * confuse humans with expected errors. */
865 LOGP(DRTP, LOGL_INFO,
866 "endpoint:0x%x I:%s Rx RTP from %s, but remote address not set:"
867 " dropping early media\n",
868 ENDPOINT_NUMBER(endp), conn->conn->id, inet_ntoa(addr->sin_addr));
869 return -1;
870 }
Neels Hofmeyr0063ca22018-07-23 18:12:16 +0200871 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200872
Philipp Maier87bd9be2017-08-22 16:35:41 +0200873 /* Note: Check if the inbound RTP data comes from the same host to
874 * which we send our outgoing RTP traffic. */
Harald Weltec26b6652018-10-21 12:01:04 +0200875 if (conn->end.addr.s_addr != addr->sin_addr.s_addr) {
Philipp Maierc3413882017-10-27 12:26:54 +0200876 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100877 "endpoint:0x%x data from wrong address: %s, ",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200878 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr));
Philipp Maierc3413882017-10-27 12:26:54 +0200879 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200880 inet_ntoa(conn->end.addr));
Philipp Maier230e4fc2017-11-28 09:38:45 +0100881 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200882 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200883 return -1;
884 }
885
Philipp Maier87bd9be2017-08-22 16:35:41 +0200886 /* Note: Usually the remote remote port of the data we receive will be
887 * the same as the remote port where we transmit outgoing RTP traffic
888 * to (set by MDCX). We use this to check the origin of the data for
889 * plausibility. */
890 if (conn->end.rtp_port != addr->sin_port &&
891 conn->end.rtcp_port != addr->sin_port) {
Philipp Maierc3413882017-10-27 12:26:54 +0200892 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100893 "endpoint:0x%x data from wrong source port: %d, ",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200894 ENDPOINT_NUMBER(endp), ntohs(addr->sin_port));
Philipp Maierc3413882017-10-27 12:26:54 +0200895 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200896 "expected: %d for RTP or %d for RTCP\n",
897 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Philipp Maier230e4fc2017-11-28 09:38:45 +0100898 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200899 ENDPOINT_NUMBER(endp));
900 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200901 }
902
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200903 return 0;
904}
905
Philipp Maier87bd9be2017-08-22 16:35:41 +0200906/* Check the if the destination address configuration of an RTP connection
907 * makes sense */
908static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200909{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200910 struct mgcp_endpoint *endp;
911 endp = conn->conn->endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200912
Philipp Maiere6df0e42018-05-29 14:03:06 +0200913 /* Note: it is legal to create a connection but never setting a port
914 * and IP-address for outgoing data. */
915 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0 && conn->end.rtp_port == 0) {
916 LOGP(DRTP, LOGL_DEBUG,
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200917 "endpoint:0x%x destination IP-address and rtp port is not known (yet)\n",
Philipp Maiere6df0e42018-05-29 14:03:06 +0200918 ENDPOINT_NUMBER(endp));
919 return -1;
920 }
921
Philipp Maier87bd9be2017-08-22 16:35:41 +0200922 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200923 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100924 "endpoint:0x%x destination IP-address is invalid\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200925 ENDPOINT_NUMBER(endp));
926 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200927 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200928
929 if (conn->end.rtp_port == 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200930 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100931 "endpoint:0x%x destination rtp port is invalid\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200932 ENDPOINT_NUMBER(endp));
933 return -1;
934 }
935
936 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200937}
938
Philipp Maier4dba7692018-08-03 12:20:52 +0200939/* Do some basic checks to make sure that the RTCP packets we are going to
940 * process are not complete garbage */
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200941static int check_rtcp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +0200942{
943 struct rtcp_hdr *hdr;
944 unsigned int len;
945 uint8_t type;
946
947 /* RTPC packets that are just a header without data do not make
948 * any sense. */
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200949 if (msg->len < sizeof(struct rtcp_hdr)) {
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200950 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP packet too short (%u < %zu)\n",
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200951 msg->len, sizeof(struct rtcp_hdr));
Philipp Maier4dba7692018-08-03 12:20:52 +0200952 return -EINVAL;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200953 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200954
955 /* Make sure that the length of the received packet does not exceed
956 * the available buffer size */
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200957 hdr = (struct rtcp_hdr *)msg->data;
Philipp Maier4dba7692018-08-03 12:20:52 +0200958 len = (osmo_ntohs(hdr->length) + 1) * 4;
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200959 if (len > msg->len) {
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200960 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header length exceeds packet size (%u > %u)\n",
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200961 len, msg->len);
Philipp Maier4dba7692018-08-03 12:20:52 +0200962 return -EINVAL;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200963 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200964
965 /* Make sure we accept only packets that have a proper packet type set
966 * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
967 type = hdr->type;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200968 if ((type < 192 || type > 195) && (type < 200 || type > 213)) {
969 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header: invalid type: %u\n", type);
Philipp Maier4dba7692018-08-03 12:20:52 +0200970 return -EINVAL;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200971 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200972
973 return 0;
974}
975
976/* Do some basic checks to make sure that the RTP packets we are going to
977 * process are not complete garbage */
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200978static int check_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +0200979{
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200980 size_t min_size = sizeof(struct rtp_hdr);
981 if (conn_src->iuup)
982 min_size += sizeof(struct osmo_iuup_hdr_data);
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200983 if (msg->len < min_size) {
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200984 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
Neels Hofmeyr85f94012018-10-08 03:36:45 +0200985 msg->len, min_size);
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200986 return -1;
987 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200988
989 /* FIXME: Add more checks, the reason why we do not check more than
990 * the length is because we currently handle IUUP packets as RTP
991 * packets, so they must pass this check, if we weould be more
992 * strict here, we would possibly break 3G. (see also FIXME note
993 * below */
994
995 return 0;
996}
997
Philipp Maier87bd9be2017-08-22 16:35:41 +0200998/* Send RTP data. Possible options are standard RTP packet
999 * transmission or trsmission via an osmux connection */
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001000static int mgcp_send_rtp(struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001001{
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001002 enum rtp_proto proto = OSMO_RTP_MSG_CTX(msg)->proto;
1003 struct mgcp_conn_rtp *conn_src = OSMO_RTP_MSG_CTX(msg)->conn_src;
1004 struct sockaddr_in *from_addr = OSMO_RTP_MSG_CTX(msg)->from_addr;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001005 struct mgcp_endpoint *endp = conn_src->conn->endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001006
Philipp Maier230e4fc2017-11-28 09:38:45 +01001007 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x destin conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001008 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_dst->conn));
1009
1010 /* Before we try to deliver the packet, we check if the destination
1011 * port and IP-Address make sense at all. If not, we will be unable
1012 * to deliver the packet. */
1013 if (check_rtp_destin(conn_dst) != 0)
1014 return -1;
1015
1016 /* Depending on the RTP connection type, deliver the RTP packet to the
1017 * destination connection. */
1018 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001019 case MGCP_RTP_DEFAULT:
Philipp Maierc3413882017-10-27 12:26:54 +02001020 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001021 "endpoint:0x%x endpoint type is MGCP_RTP_DEFAULT, "
Philipp Maier87bd9be2017-08-22 16:35:41 +02001022 "using mgcp_send() to forward data directly\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001023 ENDPOINT_NUMBER(endp));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001024 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001025 from_addr, msg, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001026 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001027 case MGCP_OSMUX_BSC:
Philipp Maierc3413882017-10-27 12:26:54 +02001028 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001029 "endpoint:0x%x endpoint type is MGCP_OSMUX_BSC_NAT, "
Philipp Maier87bd9be2017-08-22 16:35:41 +02001030 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n",
1031 ENDPOINT_NUMBER(endp));
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001032 return osmux_xfrm_to_osmux((char*)msg->data, msg->len, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001033 }
1034
Philipp Maier87bd9be2017-08-22 16:35:41 +02001035 /* If the data has not been handled/forwarded until here, it will
1036 * be discarded, this should not happen, normally the MGCP type
1037 * should be properly set */
Philipp Maierc3413882017-10-27 12:26:54 +02001038 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001039 "endpoint:0x%x bad MGCP type -- data discarded!\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001040 ENDPOINT_NUMBER(endp));
1041
1042 return -1;
1043}
1044
1045/*! dispatch incoming RTP packet to opposite RTP connection.
1046 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP)
1047 * \param[in] addr socket address where the RTP packet has been received from
1048 * \param[in] buf buffer that hold the RTP payload
1049 * \param[in] buf_size size data length of buf
1050 * \param[in] conn originating connection
1051 * \returns 0 on success, -1 on ERROR */
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001052int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001053{
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001054 struct mgcp_conn_rtp *conn_src = OSMO_RTP_MSG_CTX(msg)->conn_src;
1055 struct mgcp_conn *conn = conn_src->conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001056 struct mgcp_conn *conn_dst;
1057 struct mgcp_endpoint *endp;
1058 endp = conn->endp;
1059
1060 /*! NOTE: This callback function implements the endpoint specific
1061 * dispatch bahviour of an rtp bridge/proxy endpoint. It is assumed
1062 * that the endpoint will hold only two connections. This premise
1063 * is used to determine the opposite connection (it is always the
1064 * connection that is not the originating connection). Once the
1065 * destination connection is known the RTP packet is sent via
1066 * the destination connection. */
1067
1068 /* Find a destination connection. */
1069 /* NOTE: This code path runs every time an RTP packet is received. The
1070 * function mgcp_find_dst_conn() we use to determine the detination
1071 * connection will iterate the connection list inside the endpoint.
1072 * Since list iterations are quite costly, we will figure out the
1073 * destination only once and use the optional private data pointer of
1074 * the connection to cache the destination connection pointer. */
1075 if (!conn->priv) {
1076 conn_dst = mgcp_find_dst_conn(conn);
1077 conn->priv = conn_dst;
1078 } else {
1079 conn_dst = (struct mgcp_conn *)conn->priv;
1080 }
1081
1082 /* There is no destination conn, stop here */
1083 if (!conn_dst) {
Philipp Maierc3413882017-10-27 12:26:54 +02001084 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001085 "endpoint:0x%x unable to find destination conn\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001086 ENDPOINT_NUMBER(endp));
1087 return -1;
1088 }
1089
1090 /* The destination conn is not an RTP connection */
1091 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Philipp Maierc3413882017-10-27 12:26:54 +02001092 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001093 "endpoint:0x%x unable to find suitable destination conn\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001094 ENDPOINT_NUMBER(endp));
1095 return -1;
1096 }
1097
1098 /* Dispatch RTP packet to destination RTP connection */
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001099 return mgcp_send_rtp(&conn_dst->u.rtp, msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001100}
1101
Philipp Maierdf5d2192018-01-24 11:39:32 +01001102/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1103 * \param[in] endp Endpoint on which the connection resides.
1104 * \param[in] conn Connection that is about to be removed (ignored).
1105 * \returns 0 on success, -1 on ERROR. */
1106void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1107{
1108 struct mgcp_conn *conn_cleanup;
1109
1110 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1111 * pointer to the destination connection, so that we do not have
1112 * to go through the list every time an RTP packet arrives. To prevent
1113 * a use-after-free situation we invalidate this information for all
1114 * connections present when one connection is removed from the
1115 * endpoint. */
1116 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
1117 conn_cleanup->priv = NULL;
1118 }
1119}
1120
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001121static bool is_dummy_msg(enum rtp_proto proto, struct msgb *msg)
1122{
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001123 return msg->len == 1 && msg->data[0] == MGCP_DUMMY_LOAD;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001124}
1125
1126struct pdu_ctx {
1127 struct sockaddr_in *from_addr;
1128 struct mgcp_conn_rtp *conn_src;
1129};
1130
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001131/* IuUP CN node has stripped an IuUP header and forwards RTP data to distribute to the peers. */
1132int iuup_rx_payload(struct msgb *msg, void *node_priv)
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001133{
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001134 struct mgcp_conn_rtp *conn_src = OSMO_RTP_MSG_CTX(msg)->conn_src;
1135 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "iuup_rx_payload(%u bytes)\n", msg->len);
1136 return rx_rtp(msg);
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001137}
1138
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001139/* IuUP CN node has composed a message that contains an IuUP header and asks us to send to the IuUP peer.
1140 */
1141int iuup_tx_msg(struct msgb *msg, void *node_priv)
1142{
1143 const struct in_addr zero_addr = {};
1144 struct mgcp_conn_rtp *conn_src = OSMO_RTP_MSG_CTX(msg)->conn_src;
1145 struct mgcp_conn_rtp *conn_dst = node_priv;
1146 struct sockaddr_in *from_addr = OSMO_RTP_MSG_CTX(msg)->from_addr;
1147 struct mgcp_rtp_end *rtp_end = &conn_dst->end;
1148 struct in_addr to_addr = rtp_end->addr;
1149 uint16_t to_port = rtp_end->rtp_port;
1150
1151 if (conn_src == conn_dst
1152 && !memcmp(&zero_addr, &to_addr, sizeof(zero_addr)) && !to_port) {
1153 LOG_CONN_RTP(conn_dst, LOGL_DEBUG, "iuup_tx_msg(): direct IuUP reply\n");
1154 /* IuUP wants to send a message back to the same peer that sent an RTP package, but there
1155 * is no address configured for that peer yet. It is probably an IuUP Initialization ACK
1156 * reply. Use the sender address to send the reply.
1157 *
1158 * During 3G RAB Assignment, a 3G cell might first probe the MGW and expect an IuUP
1159 * Initialization ACK before it replies to the MSC with a successful RAB Assignment; only
1160 * after that reply does MSC officially know which RTP address+port the 3G cell wants to
1161 * use and can tell this MGW about it, so this "loopback" is, for some 3G cells, the only
1162 * chance we have to get a successful RAB Assignment done (particularly the nano3G does
1163 * this). */
1164 to_addr = from_addr->sin_addr;
1165 to_port = from_addr->sin_port;
1166 }
1167
1168 LOG_CONN_RTP(conn_dst, LOGL_DEBUG, "iuup_tx_msg(%u bytes) to %s:%u\n", msg->len,
1169 inet_ntoa(to_addr), ntohs(to_port));
1170
1171 return mgcp_udp_send(rtp_end->rtp.fd, &to_addr, to_port, (char*)msg->data, msg->len);
1172}
1173
1174static void iuup_init(struct mgcp_conn_rtp *conn_src)
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001175{
1176 struct osmo_iuup_cn_cfg cfg = {
1177 .node_priv = conn_src,
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001178 .rx_payload = iuup_rx_payload,
1179 .tx_msg = iuup_tx_msg,
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001180 };
1181
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001182 if (conn_src->iuup) {
1183 LOG_CONN_RTP(conn_src, LOGL_NOTICE, "Rx IuUP init, but already initialized. Ignoring.\n");
1184 return;
1185 }
1186
1187 conn_src->iuup = osmo_iuup_cn_init(conn_src->conn, &cfg, "endp_%d_conn_%s",
1188 ENDPOINT_NUMBER(conn_src->conn->endp), conn_src->conn->id);
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001189}
1190
Philipp Maier87bd9be2017-08-22 16:35:41 +02001191/* Handle incoming RTP data from NET */
1192static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1193{
1194 /* NOTE: This is a generic implementation. RTP data is received. In
1195 * case of loopback the data is just sent back to its origin. All
1196 * other cases implement endpoint specific behaviour (e.g. how is the
1197 * destination connection determined?). That specific behaviour is
1198 * implemented by the callback function that is called at the end of
1199 * the function */
1200
1201 struct mgcp_conn_rtp *conn_src;
1202 struct mgcp_endpoint *endp;
1203 struct sockaddr_in addr;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001204 socklen_t slen = sizeof(addr);
1205 int ret;
1206 enum rtp_proto proto;
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001207 struct osmo_rtp_msg_ctx mc;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001208 struct msgb *msg = msgb_alloc_headroom(RTP_BUF_SIZE + OSMO_IUUP_HEADROOM,
1209 OSMO_IUUP_HEADROOM, "RTP-rx");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001210
1211 conn_src = (struct mgcp_conn_rtp *)fd->data;
1212 OSMO_ASSERT(conn_src);
1213 endp = conn_src->conn->endp;
1214 OSMO_ASSERT(endp);
1215
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001216 proto = (fd == &conn_src->end.rtp)? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001217
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001218 ret = recvfrom(fd->fd, msg->data, msg->data_len, 0, (struct sockaddr *)&addr, &slen);
1219
1220 if (ret <= 0) {
1221 LOG_CONN_RTP(conn_src, LOGL_ERROR, "recvfrom error: %s\n", strerror(errno));
1222 msgb_free(msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001223 return -1;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001224 }
1225
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001226 msgb_put(msg, ret);
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001227
1228 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "%s: rx %u bytes from %s:%u\n",
1229 proto == MGCP_PROTO_RTP ? "RTP" : "RTPC",
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001230 msg->len, inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001231
1232 if ((proto == MGCP_PROTO_RTP && check_rtp(conn_src, msg))
1233 || (proto == MGCP_PROTO_RTCP && check_rtcp(conn_src, msg))) {
1234 /* Logging happened in the two check_ functions */
1235 return -1;
1236 }
1237
1238 if (is_dummy_msg(proto, msg)) {
1239 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx dummy packet (dropped)\n");
1240 return 0;
1241 }
1242
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001243 mc = (struct osmo_rtp_msg_ctx){
1244 .proto = proto,
1245 .conn_src = conn_src,
1246 .from_addr = &addr,
1247 };
1248 OSMO_RTP_MSG_CTX(msg) = &mc;
1249 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "msg ctx: %d %p %s\n",
1250 OSMO_RTP_MSG_CTX(msg)->proto,
1251 OSMO_RTP_MSG_CTX(msg)->conn_src,
1252 osmo_hexdump((void*)OSMO_RTP_MSG_CTX(msg)->from_addr, sizeof(struct sockaddr_in)));
1253
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001254 /* Increment RX statistics */
1255 rate_ctr_inc(&conn_src->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR]);
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001256 rate_ctr_add(&conn_src->rate_ctr_group->ctr[RTP_OCTETS_RX_CTR], msg->len);
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001257 /* FIXME: count RTP and RTCP separately, also count IuUP payload-less separately */
1258
1259 /* Forward a copy of the RTP data to a debug ip/port */
1260 forward_data(fd->fd, &conn_src->tap_in, msg);
1261
1262 if (proto == MGCP_PROTO_RTP && osmo_iuup_is_init(msg))
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001263 iuup_init(conn_src);
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001264
1265 if (conn_src->iuup && proto == MGCP_PROTO_RTP)
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001266 return osmo_iuup_cn_rx_pdu(conn_src->iuup, msg);
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001267 else
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001268 return rx_rtp(msg);
1269 msgb_free(msg);
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001270}
1271
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001272static int rx_rtp(struct msgb *msg)
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001273{
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001274 struct mgcp_conn_rtp *conn_src = OSMO_RTP_MSG_CTX(msg)->conn_src;
1275 struct sockaddr_in *from_addr = OSMO_RTP_MSG_CTX(msg)->from_addr;
1276 struct mgcp_conn *conn = conn_src->conn;
1277 struct mgcp_trunk_config *tcfg = conn->endp->tcfg;
1278
1279 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx_rtp(%u bytes)\n", msg->len);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001280
1281 /* Check if the connection is in loopback mode, if yes, just send the
1282 * incoming data back to the origin */
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001283 if (conn->mode == MGCP_CONN_LOOPBACK) {
Philipp Maier5dbfc782017-12-12 16:20:25 +01001284 /* When we are in loopback mode, we loop back all incoming
1285 * packets back to their origin. We will use the originating
1286 * address data from the UDP packet header to patch the
1287 * outgoing address in connection on the fly */
1288 if (conn_src->end.rtp_port == 0) {
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001289 conn_src->end.addr = from_addr->sin_addr;
1290 conn_src->end.rtp_port = from_addr->sin_port;
Philipp Maier5dbfc782017-12-12 16:20:25 +01001291 }
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001292 return mgcp_send_rtp(conn_src, msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001293 }
1294
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001295 /* Check if the origin of the RTP packet seems plausible */
1296 if (!tcfg->rtp_accept_all && check_rtp_origin(conn_src, from_addr))
1297 return -1;
1298
Philipp Maier87bd9be2017-08-22 16:35:41 +02001299 /* Execute endpoint specific implementation that handles the
1300 * dispatching of the RTP data */
Neels Hofmeyr85f94012018-10-08 03:36:45 +02001301 return conn->endp->type->dispatch_rtp_cb(msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001302}
1303
1304/*! set IP Type of Service parameter.
1305 * \param[in] fd associated file descriptor
1306 * \param[in] tos dscp value
1307 * \returns 0 on success, -1 on ERROR */
1308int mgcp_set_ip_tos(int fd, int tos)
1309{
1310 int ret;
1311 ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1312
1313 if (ret < 0)
1314 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001315 return 0;
1316}
1317
Philipp Maier87bd9be2017-08-22 16:35:41 +02001318/*! bind RTP port to osmo_fd.
1319 * \param[in] source_addr source (local) address to bind on
1320 * \param[in] fd associated file descriptor
1321 * \param[in] port to bind on
1322 * \returns 0 on success, -1 on ERROR */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001323int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
1324{
Harald Welte8890dfa2017-11-17 15:09:30 +01001325 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001326
Harald Welte8890dfa2017-11-17 15:09:30 +01001327 rc = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
1328 NULL, 0, OSMO_SOCK_F_BIND);
1329 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001330 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001331 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001332 return -1;
1333 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001334 fd->fd = rc;
1335 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001336
1337 return 0;
1338}
1339
Philipp Maier87bd9be2017-08-22 16:35:41 +02001340/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001341static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001342 struct mgcp_rtp_end *rtp_end, int endpno)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001343{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001344 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1345 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1346
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001347 if (mgcp_create_bind(source_addr, &rtp_end->rtp,
1348 rtp_end->local_port) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001349 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001350 "endpoint:0x%x failed to create RTP port: %s:%d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001351 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001352 goto cleanup0;
1353 }
1354
1355 if (mgcp_create_bind(source_addr, &rtp_end->rtcp,
1356 rtp_end->local_port + 1) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001357 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001358 "endpoint:0x%x failed to create RTCP port: %s:%d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001359 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001360 goto cleanup1;
1361 }
1362
Philipp Maier87bd9be2017-08-22 16:35:41 +02001363 /* Set Type of Service (DSCP-Value) as configured via VTY */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001364 mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
1365 mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
1366
1367 rtp_end->rtp.when = BSC_FD_READ;
1368 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001369 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001370 "endpoint:0x%x failed to register RTP port %d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001371 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001372 goto cleanup2;
1373 }
1374
1375 rtp_end->rtcp.when = BSC_FD_READ;
1376 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001377 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001378 "endpoint:0x%x failed to register RTCP port %d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001379 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001380 goto cleanup3;
1381 }
1382
1383 return 0;
1384
1385cleanup3:
1386 osmo_fd_unregister(&rtp_end->rtp);
1387cleanup2:
1388 close(rtp_end->rtcp.fd);
1389 rtp_end->rtcp.fd = -1;
1390cleanup1:
1391 close(rtp_end->rtp.fd);
1392 rtp_end->rtp.fd = -1;
1393cleanup0:
1394 return -1;
1395}
1396
Philipp Maier87bd9be2017-08-22 16:35:41 +02001397/*! bind RTP port to endpoint/connection.
1398 * \param[in] endp endpoint that holds the RTP connection
1399 * \param[in] rtp_port port number to bind on
1400 * \param[in] conn associated RTP connection
1401 * \returns 0 on success, -1 on ERROR */
1402int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1403 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001404{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001405 char name[512];
1406 struct mgcp_rtp_end *end;
Philipp Maier1cb1e382017-11-02 17:16:04 +01001407 char local_ip_addr[INET_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001408
Philipp Maier01d24a32017-11-21 17:26:09 +01001409 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001410 end = &conn->end;
1411
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001412 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Philipp Maierc3413882017-10-27 12:26:54 +02001413 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001414 "endpoint:0x%x %u was already bound on conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001415 ENDPOINT_NUMBER(endp), rtp_port,
1416 mgcp_conn_dump(conn->conn));
1417
1418 /* Double bindings should never occour! Since we always allocate
1419 * connections dynamically and free them when they are not
1420 * needed anymore, there must be no previous binding leftover.
1421 * Should there be a connection bound twice, we have a serious
1422 * problem and must exit immediately! */
1423 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001424 }
1425
1426 end->local_port = rtp_port;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001427 end->rtp.cb = rtp_data_net;
1428 end->rtp.data = conn;
1429 end->rtcp.data = conn;
1430 end->rtcp.cb = rtp_data_net;
1431
Philipp Maier1cb1e382017-11-02 17:16:04 +01001432 mgcp_get_local_addr(local_ip_addr, conn);
1433
1434 return bind_rtp(endp->cfg, local_ip_addr, end,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001435 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001436}
1437
Philipp Maier87bd9be2017-08-22 16:35:41 +02001438/*! free allocated RTP and RTCP ports.
1439 * \param[in] end RTP end */
1440void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001441{
1442 if (end->rtp.fd != -1) {
1443 close(end->rtp.fd);
1444 end->rtp.fd = -1;
1445 osmo_fd_unregister(&end->rtp);
1446 }
1447
1448 if (end->rtcp.fd != -1) {
1449 close(end->rtcp.fd);
1450 end->rtcp.fd = -1;
1451 osmo_fd_unregister(&end->rtcp);
1452 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001453}