blob: a73209b83bba1853cb2df4c742f910b56050b886 [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 Hofmeyrf83ec562017-09-07 19:18:40 +020046
Philipp Maier6931f9a2018-07-26 09:29:31 +020047
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020048#define RTP_SEQ_MOD (1 << 16)
49#define RTP_MAX_DROPOUT 3000
50#define RTP_MAX_MISORDER 100
51#define RTP_BUF_SIZE 4096
52
53enum {
54 MGCP_PROTO_RTP,
55 MGCP_PROTO_RTCP,
56};
57
Philipp Maier1cb1e382017-11-02 17:16:04 +010058/*! Determine the local rtp bind IP-address.
59 * \param[out] addr caller provided memory to store the resulting IP-Address
60 * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters
61 *
62 * The local bind IP-address is automatically selected by probing the
63 * IP-Address of the interface that is pointing towards the remote IP-Address,
64 * if no remote IP-Address is known yet, the statically configured
65 * IP-Addresses are used as fallback. */
66void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn)
67{
68
69 struct mgcp_endpoint *endp;
70 int rc;
71 endp = conn->conn->endp;
72
73 /* Try probing the local IP-Address */
74 if (endp->cfg->net_ports.bind_addr_probe && conn->end.addr.s_addr != 0) {
75 rc = osmo_sock_local_ip(addr, inet_ntoa(conn->end.addr));
76 if (rc < 0)
77 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +010078 "endpoint:0x%x CI:%s local interface auto detection failed, using configured addresses...\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +010079 ENDPOINT_NUMBER(endp), conn->conn->id);
80 else {
81 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +010082 "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 +010083 ENDPOINT_NUMBER(endp), conn->conn->id, addr,
84 inet_ntoa(conn->end.addr));
85 return;
86 }
87 }
88
89 /* Select from preconfigured IP-Addresses */
90 if (endp->cfg->net_ports.bind_addr) {
91 /* Check there is a bind IP for the RTP traffic configured,
92 * if so, use that IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +010093 osmo_strlcpy(addr, endp->cfg->net_ports.bind_addr, INET_ADDRSTRLEN);
Philipp Maier1cb1e382017-11-02 17:16:04 +010094 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +010095 "endpoint:0x%x CI:%s using configured rtp bind ip as local bind ip %s\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +010096 ENDPOINT_NUMBER(endp), conn->conn->id, addr);
97 } else {
98 /* No specific bind IP is configured for the RTP traffic, so
99 * assume the IP where we listen for incoming MGCP messages
100 * as bind IP */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100101 osmo_strlcpy(addr, endp->cfg->source_addr, INET_ADDRSTRLEN);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100102 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100103 "endpoint:0x%x CI:%s using mgcp bind ip as local rtp bind ip: %s\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +0100104 ENDPOINT_NUMBER(endp), conn->conn->id, addr);
105 }
106}
107
Philipp Maier87bd9be2017-08-22 16:35:41 +0200108/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200109 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +0200110 * 1/codec_rate seconds. */
111static uint32_t get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200112{
113 struct timespec tp;
114 uint64_t ret;
115
Philipp Maier87bd9be2017-08-22 16:35:41 +0200116 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200117 return 0;
118
119 memset(&tp, 0, sizeof(tp));
120 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maierc3413882017-10-27 12:26:54 +0200121 LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200122
123 /* convert it to 1/unit seconds */
124 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200125 ret *= codec_rate;
126 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200127
128 return ret;
129}
130
Philipp Maier87bd9be2017-08-22 16:35:41 +0200131/*! send udp packet.
132 * \param[in] fd associated file descriptor
133 * \param[in] addr destination ip-address
134 * \param[in] port destination UDP port
135 * \param[in] buf buffer that holds the data to be send
136 * \param[in] len length of the data to be sent
137 * \returns bytes sent, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200138int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
139{
140 struct sockaddr_in out;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200141
Philipp Maierc3413882017-10-27 12:26:54 +0200142 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200143 "sending %i bytes length packet to %s:%u ...\n",
144 len, inet_ntoa(*addr), ntohs(port));
145
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200146 out.sin_family = AF_INET;
147 out.sin_port = port;
148 memcpy(&out.sin_addr, addr, sizeof(*addr));
149
150 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
151}
152
Philipp Maier87bd9be2017-08-22 16:35:41 +0200153/*! send RTP dummy packet (to keep NAT connection open).
154 * \param[in] endp mcgp endpoint that holds the RTP connection
155 * \param[in] conn associated RTP connection
156 * \returns bytes sent, -1 on error */
157int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200158{
159 static char buf[] = { MGCP_DUMMY_LOAD };
160 int rc;
161 int was_rtcp = 0;
162
Philipp Maier87bd9be2017-08-22 16:35:41 +0200163 OSMO_ASSERT(endp);
164 OSMO_ASSERT(conn);
165
Philipp Maierc3413882017-10-27 12:26:54 +0200166 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100167 "endpoint:0x%x sending dummy packet...\n", ENDPOINT_NUMBER(endp));
168 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200169 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
170
171 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
172 conn->end.rtp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200173
174 if (rc == -1)
175 goto failed;
176
177 if (endp->tcfg->omit_rtcp)
178 return rc;
179
180 was_rtcp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200181 rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
182 conn->end.rtcp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200183
184 if (rc >= 0)
185 return rc;
186
187failed:
Philipp Maierc3413882017-10-27 12:26:54 +0200188 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100189 "endpoint:0x%x Failed to send dummy %s packet.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200190 ENDPOINT_NUMBER(endp), was_rtcp ? "RTCP" : "RTP");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200191
192 return -1;
193}
194
Philipp Maier87bd9be2017-08-22 16:35:41 +0200195/* Compute timestamp alignment error */
196static int32_t ts_alignment_error(struct mgcp_rtp_stream_state *sstate,
197 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200198{
199 int32_t timestamp_delta;
200
201 if (ptime == 0)
202 return 0;
203
204 /* Align according to: T - Tlast = k * Tptime */
205 timestamp_delta = timestamp - sstate->last_timestamp;
206
207 return timestamp_delta % ptime;
208}
209
Philipp Maier87bd9be2017-08-22 16:35:41 +0200210/* Check timestamp and sequence number for plausibility */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200211static int check_rtp_timestamp(struct mgcp_endpoint *endp,
212 struct mgcp_rtp_state *state,
213 struct mgcp_rtp_stream_state *sstate,
214 struct mgcp_rtp_end *rtp_end,
215 struct sockaddr_in *addr,
216 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200217 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200218{
219 int32_t tsdelta;
220 int32_t timestamp_error;
221
222 /* Not fully intialized, skip */
223 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
224 return 0;
225
226 if (seq == sstate->last_seq) {
227 if (timestamp != sstate->last_timestamp) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200228 rate_ctr_inc(sstate->err_ts_ctr);
Philipp Maierc3413882017-10-27 12:26:54 +0200229 LOGP(DRTP, LOGL_ERROR,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200230 "The %s timestamp delta is != 0 but the sequence "
231 "number %d is the same, "
232 "TS offset: %d, SeqNo offset: %d "
233 "on 0x%x SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200234 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200235 text, seq,
Harald Welte33381352017-12-25 09:44:26 +0100236 state->patch.timestamp_offset, state->patch.seq_offset,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200237 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200238 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200239 }
240 return 0;
241 }
242
243 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200244 (int32_t)(timestamp - sstate->last_timestamp) /
245 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200246
247 if (tsdelta == 0) {
248 /* Don't update *tsdelta_out */
Philipp Maierc3413882017-10-27 12:26:54 +0200249 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200250 "The %s timestamp delta is %d "
251 "on 0x%x SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200252 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200253 text, tsdelta,
254 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200255 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200256
257 return 0;
258 }
259
260 if (sstate->last_tsdelta != tsdelta) {
261 if (sstate->last_tsdelta) {
Philipp Maierc3413882017-10-27 12:26:54 +0200262 LOGP(DRTP, LOGL_INFO,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200263 "The %s timestamp delta changes from %d to %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200264 "on 0x%x SSRC: %u timestamp: %u from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200265 text, sstate->last_tsdelta, tsdelta,
266 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200267 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200268 }
269 }
270
271 if (tsdelta_out)
272 *tsdelta_out = tsdelta;
273
274 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200275 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200276
277 if (timestamp_error) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200278 rate_ctr_inc(sstate->err_ts_ctr);
Philipp Maierc3413882017-10-27 12:26:54 +0200279 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200280 "The %s timestamp has an alignment error of %d "
281 "on 0x%x SSRC: %u "
282 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200283 "from %s:%d. ptime: %d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200284 text, timestamp_error,
285 ENDPOINT_NUMBER(endp), sstate->ssrc,
286 (int16_t)(seq - sstate->last_seq),
287 (int32_t)(timestamp - sstate->last_timestamp),
288 tsdelta,
289 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200290 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200291 }
292 return 1;
293}
294
295/* Set the timestamp offset according to the packet duration. */
296static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp,
297 struct mgcp_rtp_state *state,
298 struct mgcp_rtp_end *rtp_end,
299 struct sockaddr_in *addr,
300 int16_t delta_seq, uint32_t in_timestamp)
301{
302 int32_t tsdelta = state->packet_duration;
303 int timestamp_offset;
304 uint32_t out_timestamp;
305
306 if (tsdelta == 0) {
307 tsdelta = state->out_stream.last_tsdelta;
308 if (tsdelta != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200309 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200310 "A fixed packet duration is not available on 0x%x, "
311 "using last output timestamp delta instead: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200312 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200313 ENDPOINT_NUMBER(endp), tsdelta,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200314 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200315 } else {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200316 tsdelta = rtp_end->codec->rate * 20 / 1000;
Philipp Maierc3413882017-10-27 12:26:54 +0200317 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200318 "Fixed packet duration and last timestamp delta "
319 "are not available on 0x%x, "
320 "using fixed 20ms instead: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200321 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200322 ENDPOINT_NUMBER(endp), tsdelta,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200323 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200324 }
325 }
326
327 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
328 timestamp_offset = out_timestamp - in_timestamp;
329
Harald Welte33381352017-12-25 09:44:26 +0100330 if (state->patch.timestamp_offset != timestamp_offset) {
331 state->patch.timestamp_offset = timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200332
Philipp Maierc3413882017-10-27 12:26:54 +0200333 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200334 "Timestamp offset change on 0x%x SSRC: %u "
335 "SeqNo delta: %d, TS offset: %d, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200336 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200337 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100338 delta_seq, state->patch.timestamp_offset,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200339 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200340 }
341
342 return timestamp_offset;
343}
344
345/* Set the timestamp offset according to the packet duration. */
346static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp,
347 struct mgcp_rtp_state *state,
348 struct mgcp_rtp_end *rtp_end,
349 struct sockaddr_in *addr,
350 uint32_t timestamp)
351{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200352 int ts_error = 0;
353 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200354 int ptime = state->packet_duration;
355
356 /* Align according to: T + Toffs - Tlast = k * Tptime */
357
Philipp Maier87bd9be2017-08-22 16:35:41 +0200358 ts_error = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100359 timestamp + state->patch.timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200360
Philipp Maier87bd9be2017-08-22 16:35:41 +0200361 /* If there is an alignment error, we have to compensate it */
362 if (ts_error) {
Harald Welte33381352017-12-25 09:44:26 +0100363 state->patch.timestamp_offset += ptime - ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200364
Philipp Maierc3413882017-10-27 12:26:54 +0200365 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200366 "Corrected timestamp alignment error of %d on 0x%x SSRC: %u "
367 "new TS offset: %d, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200368 "from %s:%d\n",
369 ts_error,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200370 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100371 state->patch.timestamp_offset, inet_ntoa(addr->sin_addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200372 ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200373 }
374
Philipp Maier87bd9be2017-08-22 16:35:41 +0200375 /* Check we really managed to compensate the timestamp
376 * offset. There should not be any remaining error, failing
Harald Welte1d1b98f2017-12-25 10:03:40 +0100377 * here would point to a serous problem with the alignment
378 * error computation function */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200379 ts_check = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100380 timestamp + state->patch.timestamp_offset);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200381 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200382
Philipp Maier87bd9be2017-08-22 16:35:41 +0200383 /* Return alignment error before compensation */
384 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200385}
386
Philipp Maier87bd9be2017-08-22 16:35:41 +0200387/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
388 * \param[in] associated endpoint
389 * \param[in] destination RTP end
390 * \param[in,out] pointer to buffer with voice data
391 * \param[in] voice data length
Harald Welte1d1b98f2017-12-25 10:03:40 +0100392 * \param[in] maximum size of caller provided voice data buffer
Philipp Maier87bd9be2017-08-22 16:35:41 +0200393 * \returns ignores input parameters, return always 0 */
394int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
395 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200396 char *data, int *len, int buf_size)
397{
Philipp Maier230e4fc2017-11-28 09:38:45 +0100398 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x transcoding disabled\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200399 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200400 return 0;
401}
402
Philipp Maier87bd9be2017-08-22 16:35:41 +0200403/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
404 * \param[in] associated endpoint
Philipp Maieracc10352018-07-19 18:07:57 +0200405 * \param[in] destination RTP connnection
406 * \param[in] source RTP connection
Philipp Maier87bd9be2017-08-22 16:35:41 +0200407 * \returns ignores input parameters, return always 0 */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200408int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
Philipp Maieracc10352018-07-19 18:07:57 +0200409 struct mgcp_conn_rtp *conn_dst,
410 struct mgcp_conn_rtp *conn_src)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200411{
Philipp Maier230e4fc2017-11-28 09:38:45 +0100412 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x transcoding disabled\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200413 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200414 return 0;
415}
416
417void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
418 int *payload_type,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200419 const char **audio_name,
420 const char **fmtp_extra,
421 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200422{
Philipp Maierc3413882017-10-27 12:26:54 +0200423 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100424 "endpoint:0x%x conn:%s using format defaults\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200425 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
426
Philipp Maierbc0346e2018-06-07 09:52:16 +0200427 *payload_type = conn->end.codec->payload_type;
428 *audio_name = conn->end.codec->audio_name;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200429 *fmtp_extra = conn->end.fmtp_extra;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200430}
431
Philipp Maier87bd9be2017-08-22 16:35:41 +0200432void mgcp_rtp_annex_count(struct mgcp_endpoint *endp,
433 struct mgcp_rtp_state *state, const uint16_t seq,
434 const int32_t transit, const uint32_t ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200435{
436 int32_t d;
437
438 /* initialize or re-initialize */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100439 if (!state->stats.initialized || state->stats.ssrc != ssrc) {
440 state->stats.initialized = 1;
441 state->stats.base_seq = seq;
442 state->stats.max_seq = seq - 1;
443 state->stats.ssrc = ssrc;
444 state->stats.jitter = 0;
445 state->stats.transit = transit;
446 state->stats.cycles = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200447 } else {
448 uint16_t udelta;
449
Philipp Maier87bd9be2017-08-22 16:35:41 +0200450 /* The below takes the shape of the validation of
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200451 * Appendix A. Check if there is something weird with
452 * the sequence number, otherwise check for a wrap
453 * around in the sequence number.
454 * It can't wrap during the initialization so let's
455 * skip it here. The Appendix A probably doesn't have
Philipp Maier87bd9be2017-08-22 16:35:41 +0200456 * this issue because of the probation. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100457 udelta = seq - state->stats.max_seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200458 if (udelta < RTP_MAX_DROPOUT) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100459 if (seq < state->stats.max_seq)
460 state->stats.cycles += RTP_SEQ_MOD;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200461 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Philipp Maierc3413882017-10-27 12:26:54 +0200462 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200463 "RTP seqno made a very large jump on 0x%x delta: %u\n",
464 ENDPOINT_NUMBER(endp), udelta);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200465 }
466 }
467
Philipp Maier87bd9be2017-08-22 16:35:41 +0200468 /* Calculate the jitter between the two packages. The TS should be
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200469 * taken closer to the read function. This was taken from the
470 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
Philipp Maier87bd9be2017-08-22 16:35:41 +0200471 * resolution. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100472 d = transit - state->stats.transit;
473 state->stats.transit = transit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200474 if (d < 0)
475 d = -d;
Harald Welte49e3d5a2017-12-25 09:47:57 +0100476 state->stats.jitter += d - ((state->stats.jitter + 8) >> 4);
477 state->stats.max_seq = seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200478}
479
Philipp Maier6931f9a2018-07-26 09:29:31 +0200480/* There may be different payload type numbers negotiated for two connections.
481 * Patch the payload type of an RTP packet so that it uses the payload type
482 * that is valid for the destination connection (conn_dst) */
483static int mgcp_patch_pt(struct mgcp_conn_rtp *conn_src,
484 struct mgcp_conn_rtp *conn_dst, char *data, int len)
485{
486 struct rtp_hdr *rtp_hdr;
487 uint8_t pt_in;
488 int pt_out;
489
490 OSMO_ASSERT(len >= sizeof(struct rtp_hdr));
491 rtp_hdr = (struct rtp_hdr *)data;
492
493 pt_in = rtp_hdr->payload_type;
494 pt_out = mgcp_codec_pt_translate(conn_src, conn_dst, pt_in);
495 if (pt_out < 0)
496 return -EINVAL;
497
498 rtp_hdr->payload_type = (uint8_t) pt_out;
499 return 0;
500}
501
Philipp Maier87bd9be2017-08-22 16:35:41 +0200502/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200503 * some of the supported endpoints (e.g. the nanoBTS) can only handle
504 * one source and this code will patch RTP header to appear as if there
505 * is only one source.
506 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200507 * we receive will be seen as a switch in streams. */
508void mgcp_patch_and_count(struct mgcp_endpoint *endp,
509 struct mgcp_rtp_state *state,
510 struct mgcp_rtp_end *rtp_end,
511 struct sockaddr_in *addr, char *data, int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200512{
513 uint32_t arrival_time;
514 int32_t transit;
515 uint16_t seq;
516 uint32_t timestamp, ssrc;
517 struct rtp_hdr *rtp_hdr;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200518 int payload = rtp_end->codec->payload_type;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200519
520 if (len < sizeof(*rtp_hdr))
521 return;
522
Philipp Maier87bd9be2017-08-22 16:35:41 +0200523 rtp_hdr = (struct rtp_hdr *)data;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200524 seq = ntohs(rtp_hdr->sequence);
525 timestamp = ntohl(rtp_hdr->timestamp);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200526 arrival_time = get_current_ts(rtp_end->codec->rate);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200527 ssrc = ntohl(rtp_hdr->ssrc);
528 transit = arrival_time - timestamp;
529
530 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc);
531
532 if (!state->initialized) {
533 state->initialized = 1;
534 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100535 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200536 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200537 state->packet_duration =
538 mgcp_rtp_packet_duration(endp, rtp_end);
Philipp Maier0ec1d4e2018-05-16 11:09:42 +0200539 state->out_stream.last_seq = seq - 1;
540 state->out_stream.ssrc = state->patch.orig_ssrc = ssrc;
541 state->out_stream.last_tsdelta = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200542 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200543 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Philipp Maierc3413882017-10-27 12:26:54 +0200544 LOGP(DRTP, LOGL_INFO,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100545 "endpoint:0x%x initializing stream, SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200546 "pkt-duration: %d, from %s:%d\n",
547 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100548 state->patch.seq_offset, state->packet_duration,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200549 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200550 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200551 state->packet_duration =
Philipp Maierbc0346e2018-06-07 09:52:16 +0200552 rtp_end->codec->rate * 20 / 1000;
Philipp Maierc3413882017-10-27 12:26:54 +0200553 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100554 "endpoint:0x%x fixed packet duration is not available, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200555 "using fixed 20ms instead: %d from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200556 ENDPOINT_NUMBER(endp), state->packet_duration,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200557 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200558 }
559 } else if (state->in_stream.ssrc != ssrc) {
Philipp Maierc3413882017-10-27 12:26:54 +0200560 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100561 "endpoint:0x%x SSRC changed: %u -> %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200562 "from %s:%d\n",
563 ENDPOINT_NUMBER(endp),
564 state->in_stream.ssrc, rtp_hdr->ssrc,
565 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200566
567 state->in_stream.ssrc = ssrc;
568 if (rtp_end->force_constant_ssrc) {
569 int16_t delta_seq;
570
571 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100572 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200573 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200574
575 /* Estimate number of packets that would have been sent */
576 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200577 (arrival_time - state->in_stream.last_arrival_time
578 + state->packet_duration / 2) /
579 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200580
581 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
582 delta_seq, timestamp);
583
Harald Welte33381352017-12-25 09:44:26 +0100584 state->patch.patch_ssrc = 1;
585 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200586 if (rtp_end->force_constant_ssrc != -1)
587 rtp_end->force_constant_ssrc -= 1;
588
Philipp Maierc3413882017-10-27 12:26:54 +0200589 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100590 "endpoint:0x%x SSRC patching enabled, SSRC: %u "
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200591 "SeqNo offset: %d, TS offset: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200592 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200593 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100594 state->patch.seq_offset, state->patch.timestamp_offset,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200595 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200596 }
597
598 state->in_stream.last_tsdelta = 0;
599 } else {
600 /* Compute current per-packet timestamp delta */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200601 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
602 addr, seq, timestamp, "input",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200603 &state->in_stream.last_tsdelta);
604
Harald Welte33381352017-12-25 09:44:26 +0100605 if (state->patch.patch_ssrc)
606 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200607 }
608
609 /* Save before patching */
610 state->in_stream.last_timestamp = timestamp;
611 state->in_stream.last_seq = seq;
612 state->in_stream.last_arrival_time = arrival_time;
613
614 if (rtp_end->force_aligned_timing &&
615 state->out_stream.ssrc == ssrc && state->packet_duration)
616 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200617 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
618 timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200619
620 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100621 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200622 rtp_hdr->ssrc = htonl(ssrc);
623
624 /* Apply the offset and store it back to the packet.
625 * This won't change anything if the offset is 0, so the conditional is
626 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100627 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200628 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100629 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200630 rtp_hdr->timestamp = htonl(timestamp);
631
632 /* Check again, whether the timestamps are still valid */
633 if (state->out_stream.ssrc == ssrc)
634 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
635 addr, seq, timestamp, "output",
636 &state->out_stream.last_tsdelta);
637
638 /* Save output values */
639 state->out_stream.last_seq = seq;
640 state->out_stream.last_timestamp = timestamp;
641 state->out_stream.ssrc = ssrc;
642
643 if (payload < 0)
644 return;
645
646#if 0
Philipp Maierc3413882017-10-27 12:26:54 +0200647 DEBUGP(DRTP,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100648 "endpoint:0x%x payload hdr payload %u -> endp payload %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200649 ENDPOINT_NUMBER(endp), rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200650 rtp_hdr->payload_type = payload;
651#endif
652}
653
Philipp Maier87bd9be2017-08-22 16:35:41 +0200654/* Forward data to a debug tap. This is debug function that is intended for
655 * debugging the voice traffic with tools like gstreamer */
Philipp Maiere6f172d2017-11-07 12:00:01 +0100656static void forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf,
657 int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200658{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100659 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200660
Philipp Maiere6f172d2017-11-07 12:00:01 +0100661 if (!tap->enabled)
662 return;
663
664 rc = sendto(fd, buf, len, 0, (struct sockaddr *)&tap->forward,
665 sizeof(tap->forward));
666
667 if (rc < 0)
668 LOGP(DRTP, LOGL_ERROR,
669 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200670}
671
Philipp Maier87bd9be2017-08-22 16:35:41 +0200672/*! Send RTP/RTCP data to a specified destination connection.
673 * \param[in] endp associated endpoint (for configuration, logging)
674 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP
675 * \param[in] spoofed source address (set to NULL to disable)
676 * \param[in] buf buffer that contains the RTP/RTCP data
677 * \param[in] len length of the buffer that contains the RTP/RTCP data
678 * \param[in] conn_src associated source connection
679 * \param[in] conn_dst associated destination connection
680 * \returns 0 on success, -1 on ERROR */
681int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
682 char *buf, int len, struct mgcp_conn_rtp *conn_src,
683 struct mgcp_conn_rtp *conn_dst)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200684{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200685 /*! When no destination connection is available (e.g. when only one
686 * connection in loopback mode exists), then the source connection
687 * shall be specified as destination connection */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200688
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200689 struct mgcp_trunk_config *tcfg = endp->tcfg;
690 struct mgcp_rtp_end *rtp_end;
691 struct mgcp_rtp_state *rtp_state;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200692 char *dest_name;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200693 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200694
Philipp Maier87bd9be2017-08-22 16:35:41 +0200695 OSMO_ASSERT(conn_src);
696 OSMO_ASSERT(conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200697
Philipp Maier87bd9be2017-08-22 16:35:41 +0200698 if (is_rtp) {
Philipp Maierc3413882017-10-27 12:26:54 +0200699 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100700 "endpoint:0x%x delivering RTP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200701 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200702 } else {
Philipp Maierc3413882017-10-27 12:26:54 +0200703 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100704 "endpoint:0x%x delivering RTCP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200705 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200706 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200707
Philipp Maierc3413882017-10-27 12:26:54 +0200708 LOGP(DRTP, LOGL_DEBUG,
Neels Hofmeyr7066af82018-07-23 18:28:36 +0200709 "endpoint:0x%x loop:%d, mode:%d%s\n",
710 ENDPOINT_NUMBER(endp), tcfg->audio_loop, conn_src->conn->mode,
711 conn_src->conn->mode == MGCP_CONN_LOOPBACK ? " (loopback)" : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200712
Philipp Maier6931f9a2018-07-26 09:29:31 +0200713 /* FIXME: It is legal that the payload type on the egress connection is
714 * different from the payload type that has been negotiated on the
715 * ingress connection. Essentially the codecs are the same so we can
716 * match them and patch the payload type. However, if we can not find
717 * the codec pendant (everything ist equal except the PT), we are of
718 * course unable to patch the payload type. A situation like this
719 * should not occur if transcoding is consequently avoided. Until
720 * we have transcoding support in osmo-mgw we can not resolve this. */
Philipp Maierda895b12018-08-03 12:16:37 +0200721 if (is_rtp) {
722 rc = mgcp_patch_pt(conn_src, conn_dst, buf, len);
723 if (rc < 0) {
724 LOGP(DRTP, LOGL_ERROR,
725 "endpoint:0x%x can not patch PT because no suitable egress codec was found.\n",
726 ENDPOINT_NUMBER(endp));
727 }
Philipp Maier6931f9a2018-07-26 09:29:31 +0200728 }
729
Philipp Maier87bd9be2017-08-22 16:35:41 +0200730 /* Note: In case of loopback configuration, both, the source and the
731 * destination will point to the same connection. */
732 rtp_end = &conn_dst->end;
733 rtp_state = &conn_src->state;
734 dest_name = conn_dst->conn->name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200735
736 if (!rtp_end->output_enabled) {
Philipp Maiercede2a42018-07-03 14:14:21 +0200737 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_DROPPED_PACKETS_CTR]);
Philipp Maierc3413882017-10-27 12:26:54 +0200738 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100739 "endpoint:0x%x output disabled, drop to %s %s "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200740 "rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200741 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200742 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200743 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200744 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200745 );
746 } else if (is_rtp) {
747 int cont;
748 int nbytes = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200749 int buflen = len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200750 do {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200751 /* Run transcoder */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200752 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200753 buf, &buflen,
754 RTP_BUF_SIZE);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200755 if (cont < 0)
756 break;
757
Philipp Maier87bd9be2017-08-22 16:35:41 +0200758 if (addr)
759 mgcp_patch_and_count(endp, rtp_state, rtp_end,
760 addr, buf, buflen);
Philipp Maierc3413882017-10-27 12:26:54 +0200761 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100762 "endpoint:0x%x process/send to %s %s "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200763 "rtp_port:%u rtcp_port:%u\n",
764 ENDPOINT_NUMBER(endp), dest_name,
765 inet_ntoa(rtp_end->addr), ntohs(rtp_end->rtp_port),
766 ntohs(rtp_end->rtcp_port)
767 );
768
769 /* Forward a copy of the RTP data to a debug ip/port */
770 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
771 buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200772
773 /* FIXME: HACK HACK HACK. See OS#2459.
774 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
775 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
776 * cells (as long as we patch only the first RTP payload in each stream).
777 */
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200778 if (!rtp_state->patched_first_rtp_payload
779 && conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200780 uint8_t *data = (uint8_t *) & buf[12];
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200781 if (data[0] == 0xe0) {
782 data[0] = 0xe4;
783 data[1] = 0x00;
784 rtp_state->patched_first_rtp_payload = true;
785 LOGP(DRTP, LOGL_DEBUG,
786 "endpoint:0x%x Patching over first two bytes"
787 " to fake an IuUP Initialization Ack\n",
788 ENDPOINT_NUMBER(endp));
789 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200790 }
791
Philipp Maier87bd9be2017-08-22 16:35:41 +0200792 len = mgcp_udp_send(rtp_end->rtp.fd,
793 &rtp_end->addr,
794 rtp_end->rtp_port, buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200795
Philipp Maier87bd9be2017-08-22 16:35:41 +0200796 if (len <= 0)
797 return len;
798
Philipp Maiercede2a42018-07-03 14:14:21 +0200799 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
800 rate_ctr_add(&conn_dst->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], len);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200801
802 nbytes += len;
803 buflen = cont;
804 } while (buflen > 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200805 return nbytes;
806 } else if (!tcfg->omit_rtcp) {
Philipp Maierc3413882017-10-27 12:26:54 +0200807 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100808 "endpoint:0x%x send to %s %s rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200809 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200810 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200811 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200812 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200813 );
814
Philipp Maier87bd9be2017-08-22 16:35:41 +0200815 len = mgcp_udp_send(rtp_end->rtcp.fd,
816 &rtp_end->addr,
817 rtp_end->rtcp_port, buf, len);
818
Philipp Maiercede2a42018-07-03 14:14:21 +0200819 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
820 rate_ctr_add(&conn_dst->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], len);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200821
822 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200823 }
824
825 return 0;
826}
827
Philipp Maier87bd9be2017-08-22 16:35:41 +0200828/* Helper function for mgcp_recv(),
829 Receive one RTP Packet + Originating address from file descriptor */
830static int receive_from(struct mgcp_endpoint *endp, int fd,
831 struct sockaddr_in *addr, char *buf, int bufsize)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200832{
833 int rc;
834 socklen_t slen = sizeof(*addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200835 struct sockaddr_in addr_sink;
836 char buf_sink[RTP_BUF_SIZE];
837 bool tossed = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200838
Philipp Maier87bd9be2017-08-22 16:35:41 +0200839 if (!addr)
840 addr = &addr_sink;
841 if (!buf) {
842 tossed = true;
843 buf = buf_sink;
844 bufsize = sizeof(buf_sink);
845 }
846
847 rc = recvfrom(fd, buf, bufsize, 0, (struct sockaddr *)addr, &slen);
848
Philipp Maierc3413882017-10-27 12:26:54 +0200849 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200850 "receiving %u bytes length packet from %s:%u ...\n",
851 rc, inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
852
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200853 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200854 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100855 "endpoint:0x%x failed to receive packet, errno: %d/%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200856 ENDPOINT_NUMBER(endp), errno, strerror(errno));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200857 return -1;
858 }
859
Philipp Maier87bd9be2017-08-22 16:35:41 +0200860 if (tossed) {
Philipp Maier230e4fc2017-11-28 09:38:45 +0100861 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200862 ENDPOINT_NUMBER(endp));
863 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200864
865 return rc;
866}
867
Philipp Maier87bd9be2017-08-22 16:35:41 +0200868/* Check if the origin (addr) matches the address/port data of the RTP
869 * connections. */
870static int check_rtp_origin(struct mgcp_conn_rtp *conn,
871 struct sockaddr_in *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200872{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200873 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200874 endp = conn->conn->endp;
Neels Hofmeyr0063ca22018-07-23 18:12:16 +0200875 struct sockaddr_in zero_addr = {};
876
877 if (memcmp(&zero_addr, &conn->end.addr, sizeof(zero_addr)) == 0
878 && conn->conn->mode == MGCP_CONN_LOOPBACK) {
879 /* HACK: for IuUP, we want to reply with an IuUP Initialization ACK upon the first RTP
880 * message received. We currently hackishly accomplish that by putting the endpoint in
881 * loopback mode and patching over the looped back RTP message to make it look like an
882 * ack. We don't know the femto cell's IP address and port until the RAB Assignment
883 * Response is received, but the nano3G expects an IuUP Initialization Ack before it even
884 * sends the RAB Assignment Response. Hence, if the remote address is 0.0.0.0 and the
885 * MGCP port is in loopback mode, allow looping back the packet to any source. */
886 LOGP(DRTP, LOGL_ERROR,
887 "endpoint:0x%x In loopback mode and remote address not set: allowing data from address: %s\n",
888 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr));
889 return 0;
890 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200891
Philipp Maier87bd9be2017-08-22 16:35:41 +0200892 /* Note: Check if the inbound RTP data comes from the same host to
893 * which we send our outgoing RTP traffic. */
894 if (memcmp(&addr->sin_addr, &conn->end.addr, sizeof(addr->sin_addr))
895 != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200896 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100897 "endpoint:0x%x data from wrong address: %s, ",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200898 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr));
Philipp Maierc3413882017-10-27 12:26:54 +0200899 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200900 inet_ntoa(conn->end.addr));
Philipp Maier230e4fc2017-11-28 09:38:45 +0100901 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200902 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200903 return -1;
904 }
905
Philipp Maier87bd9be2017-08-22 16:35:41 +0200906 /* Note: Usually the remote remote port of the data we receive will be
907 * the same as the remote port where we transmit outgoing RTP traffic
908 * to (set by MDCX). We use this to check the origin of the data for
909 * plausibility. */
910 if (conn->end.rtp_port != addr->sin_port &&
911 conn->end.rtcp_port != addr->sin_port) {
Philipp Maierc3413882017-10-27 12:26:54 +0200912 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100913 "endpoint:0x%x data from wrong source port: %d, ",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200914 ENDPOINT_NUMBER(endp), ntohs(addr->sin_port));
Philipp Maierc3413882017-10-27 12:26:54 +0200915 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200916 "expected: %d for RTP or %d for RTCP\n",
917 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Philipp Maier230e4fc2017-11-28 09:38:45 +0100918 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200919 ENDPOINT_NUMBER(endp));
920 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200921 }
922
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200923 return 0;
924}
925
Philipp Maier87bd9be2017-08-22 16:35:41 +0200926/* Check the if the destination address configuration of an RTP connection
927 * makes sense */
928static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200929{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200930 struct mgcp_endpoint *endp;
931 endp = conn->conn->endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200932
Philipp Maiere6df0e42018-05-29 14:03:06 +0200933 /* Note: it is legal to create a connection but never setting a port
934 * and IP-address for outgoing data. */
935 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0 && conn->end.rtp_port == 0) {
936 LOGP(DRTP, LOGL_DEBUG,
937 "endpoint:0x%x destination IP-address and rtp port is (not yet) known\n",
938 ENDPOINT_NUMBER(endp));
939 return -1;
940 }
941
Philipp Maier87bd9be2017-08-22 16:35:41 +0200942 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200943 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100944 "endpoint:0x%x destination IP-address is invalid\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200945 ENDPOINT_NUMBER(endp));
946 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200947 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200948
949 if (conn->end.rtp_port == 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200950 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100951 "endpoint:0x%x destination rtp port is invalid\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200952 ENDPOINT_NUMBER(endp));
953 return -1;
954 }
955
956 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200957}
958
Philipp Maier4dba7692018-08-03 12:20:52 +0200959/* Do some basic checks to make sure that the RTCP packets we are going to
960 * process are not complete garbage */
961static int check_rtcp(char *buf, unsigned int buf_size)
962{
963 struct rtcp_hdr *hdr;
964 unsigned int len;
965 uint8_t type;
966
967 /* RTPC packets that are just a header without data do not make
968 * any sense. */
969 if (buf_size < sizeof(struct rtcp_hdr))
970 return -EINVAL;
971
972 /* Make sure that the length of the received packet does not exceed
973 * the available buffer size */
974 hdr = (struct rtcp_hdr *)buf;
975 len = (osmo_ntohs(hdr->length) + 1) * 4;
976 if (len > buf_size)
977 return -EINVAL;
978
979 /* Make sure we accept only packets that have a proper packet type set
980 * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
981 type = hdr->type;
982 if ((type < 192 || type > 195) && (type < 200 || type > 213))
983 return -EINVAL;
984
985 return 0;
986}
987
988/* Do some basic checks to make sure that the RTP packets we are going to
989 * process are not complete garbage */
990static int check_rtp(char *buf, unsigned int buf_size)
991{
992 /* RTP packets that are just a header without data do not make
993 * any sense. */
994 if (buf_size < sizeof(struct rtp_hdr))
995 return -EINVAL;
996
997 /* FIXME: Add more checks, the reason why we do not check more than
998 * the length is because we currently handle IUUP packets as RTP
999 * packets, so they must pass this check, if we weould be more
1000 * strict here, we would possibly break 3G. (see also FIXME note
1001 * below */
1002
1003 return 0;
1004}
1005
Philipp Maier87bd9be2017-08-22 16:35:41 +02001006/* Receive RTP data from a specified source connection and dispatch it to a
1007 * destination connection. */
1008static int mgcp_recv(int *proto, struct sockaddr_in *addr, char *buf,
1009 unsigned int buf_size, struct osmo_fd *fd)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001010{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001011 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001012 struct mgcp_conn_rtp *conn;
1013 struct mgcp_trunk_config *tcfg;
1014 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001015
Philipp Maier87bd9be2017-08-22 16:35:41 +02001016 conn = (struct mgcp_conn_rtp*) fd->data;
1017 endp = conn->conn->endp;
1018 tcfg = endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001019
Philipp Maier230e4fc2017-11-28 09:38:45 +01001020 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x receiving RTP/RTCP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001021 ENDPOINT_NUMBER(endp));
1022
1023 rc = receive_from(endp, fd->fd, addr, buf, buf_size);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001024 if (rc <= 0)
1025 return -1;
Philipp Maier4dba7692018-08-03 12:20:52 +02001026
1027 /* FIXME: The way how we detect the protocol looks odd. We should look
1028 * into the packet header. Also we should introduce a packet type
1029 * MGCP_PROTO_IUUP because currently we handle IUUP packets like RTP
1030 * packets which is problematic. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001031 *proto = fd == &conn->end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001032
Philipp Maier4dba7692018-08-03 12:20:52 +02001033 if (*proto == MGCP_PROTO_RTP) {
1034 if (check_rtp(buf, rc) < 0) {
1035 LOGP(DRTP, LOGL_ERROR,
1036 "endpoint:0x%x invalid RTP packet received -- packet tossed\n",
1037 ENDPOINT_NUMBER(endp));
1038 return -1;
1039 }
1040 } else if (*proto == MGCP_PROTO_RTCP) {
1041 if (check_rtcp(buf, rc) < 0) {
1042 LOGP(DRTP, LOGL_ERROR,
1043 "endpoint:0x%x invalid RTCP packet received -- packet tossed\n",
1044 ENDPOINT_NUMBER(endp));
1045 return -1;
1046 }
1047 }
1048
Philipp Maier230e4fc2017-11-28 09:38:45 +01001049 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x ", ENDPOINT_NUMBER(endp));
Neels Hofmeyr56725632018-01-15 15:15:07 +01001050 LOGPC(DRTP, LOGL_DEBUG, "receiving from %s %s %d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001051 conn->conn->name, inet_ntoa(addr->sin_addr),
1052 ntohs(addr->sin_port));
Philipp Maier230e4fc2017-11-28 09:38:45 +01001053 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x conn:%s\n", ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001054 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001055
Philipp Maier87bd9be2017-08-22 16:35:41 +02001056 /* Check if the origin of the RTP packet seems plausible */
1057 if (tcfg->rtp_accept_all == 0) {
1058 if (check_rtp_origin(conn, addr) != 0)
1059 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001060 }
1061
Philipp Maier87bd9be2017-08-22 16:35:41 +02001062 /* Filter out dummy message */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001063 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Philipp Maierc3413882017-10-27 12:26:54 +02001064 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001065 "endpoint:0x%x dummy message received\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001066 ENDPOINT_NUMBER(endp));
Philipp Maierc3413882017-10-27 12:26:54 +02001067 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001068 "endpoint:0x%x packet tossed\n", ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001069 return 0;
1070 }
1071
Philipp Maier87bd9be2017-08-22 16:35:41 +02001072 /* Increment RX statistics */
Philipp Maiercede2a42018-07-03 14:14:21 +02001073 rate_ctr_inc(&conn->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR]);
1074 rate_ctr_add(&conn->rate_ctr_group->ctr[RTP_OCTETS_RX_CTR], rc);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001075
Philipp Maier87bd9be2017-08-22 16:35:41 +02001076 /* Forward a copy of the RTP data to a debug ip/port */
1077 forward_data(fd->fd, &conn->tap_in, buf, rc);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001078
Philipp Maier87bd9be2017-08-22 16:35:41 +02001079 return rc;
1080}
1081
1082/* Send RTP data. Possible options are standard RTP packet
1083 * transmission or trsmission via an osmux connection */
1084static int mgcp_send_rtp(int proto, struct sockaddr_in *addr, char *buf,
1085 unsigned int buf_size,
1086 struct mgcp_conn_rtp *conn_src,
1087 struct mgcp_conn_rtp *conn_dst)
1088{
1089 struct mgcp_endpoint *endp;
1090 endp = conn_src->conn->endp;
1091
Philipp Maier230e4fc2017-11-28 09:38:45 +01001092 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x destin conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001093 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_dst->conn));
1094
1095 /* Before we try to deliver the packet, we check if the destination
1096 * port and IP-Address make sense at all. If not, we will be unable
1097 * to deliver the packet. */
1098 if (check_rtp_destin(conn_dst) != 0)
1099 return -1;
1100
1101 /* Depending on the RTP connection type, deliver the RTP packet to the
1102 * destination connection. */
1103 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001104 case MGCP_RTP_DEFAULT:
Philipp Maierc3413882017-10-27 12:26:54 +02001105 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001106 "endpoint:0x%x endpoint type is MGCP_RTP_DEFAULT, "
Philipp Maier87bd9be2017-08-22 16:35:41 +02001107 "using mgcp_send() to forward data directly\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001108 ENDPOINT_NUMBER(endp));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001109 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
1110 addr, buf, buf_size, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001111 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001112 case MGCP_OSMUX_BSC:
Philipp Maierc3413882017-10-27 12:26:54 +02001113 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001114 "endpoint:0x%x endpoint type is MGCP_OSMUX_BSC_NAT, "
Philipp Maier87bd9be2017-08-22 16:35:41 +02001115 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n",
1116 ENDPOINT_NUMBER(endp));
1117 return osmux_xfrm_to_osmux(buf, buf_size, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001118 }
1119
Philipp Maier87bd9be2017-08-22 16:35:41 +02001120 /* If the data has not been handled/forwarded until here, it will
1121 * be discarded, this should not happen, normally the MGCP type
1122 * should be properly set */
Philipp Maierc3413882017-10-27 12:26:54 +02001123 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001124 "endpoint:0x%x bad MGCP type -- data discarded!\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001125 ENDPOINT_NUMBER(endp));
1126
1127 return -1;
1128}
1129
1130/*! dispatch incoming RTP packet to opposite RTP connection.
1131 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP)
1132 * \param[in] addr socket address where the RTP packet has been received from
1133 * \param[in] buf buffer that hold the RTP payload
1134 * \param[in] buf_size size data length of buf
1135 * \param[in] conn originating connection
1136 * \returns 0 on success, -1 on ERROR */
1137int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf,
1138 unsigned int buf_size, struct mgcp_conn *conn)
1139{
1140 struct mgcp_conn *conn_dst;
1141 struct mgcp_endpoint *endp;
1142 endp = conn->endp;
1143
1144 /*! NOTE: This callback function implements the endpoint specific
1145 * dispatch bahviour of an rtp bridge/proxy endpoint. It is assumed
1146 * that the endpoint will hold only two connections. This premise
1147 * is used to determine the opposite connection (it is always the
1148 * connection that is not the originating connection). Once the
1149 * destination connection is known the RTP packet is sent via
1150 * the destination connection. */
1151
1152 /* Find a destination connection. */
1153 /* NOTE: This code path runs every time an RTP packet is received. The
1154 * function mgcp_find_dst_conn() we use to determine the detination
1155 * connection will iterate the connection list inside the endpoint.
1156 * Since list iterations are quite costly, we will figure out the
1157 * destination only once and use the optional private data pointer of
1158 * the connection to cache the destination connection pointer. */
1159 if (!conn->priv) {
1160 conn_dst = mgcp_find_dst_conn(conn);
1161 conn->priv = conn_dst;
1162 } else {
1163 conn_dst = (struct mgcp_conn *)conn->priv;
1164 }
1165
1166 /* There is no destination conn, stop here */
1167 if (!conn_dst) {
Philipp Maierc3413882017-10-27 12:26:54 +02001168 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001169 "endpoint:0x%x unable to find destination conn\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001170 ENDPOINT_NUMBER(endp));
1171 return -1;
1172 }
1173
1174 /* The destination conn is not an RTP connection */
1175 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Philipp Maierc3413882017-10-27 12:26:54 +02001176 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001177 "endpoint:0x%x unable to find suitable destination conn\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001178 ENDPOINT_NUMBER(endp));
1179 return -1;
1180 }
1181
1182 /* Dispatch RTP packet to destination RTP connection */
1183 return mgcp_send_rtp(proto, addr, buf,
1184 buf_size, &conn->u.rtp, &conn_dst->u.rtp);
1185
1186}
1187
Philipp Maierdf5d2192018-01-24 11:39:32 +01001188/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1189 * \param[in] endp Endpoint on which the connection resides.
1190 * \param[in] conn Connection that is about to be removed (ignored).
1191 * \returns 0 on success, -1 on ERROR. */
1192void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1193{
1194 struct mgcp_conn *conn_cleanup;
1195
1196 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1197 * pointer to the destination connection, so that we do not have
1198 * to go through the list every time an RTP packet arrives. To prevent
1199 * a use-after-free situation we invalidate this information for all
1200 * connections present when one connection is removed from the
1201 * endpoint. */
1202 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
1203 conn_cleanup->priv = NULL;
1204 }
1205}
1206
Philipp Maier87bd9be2017-08-22 16:35:41 +02001207/* Handle incoming RTP data from NET */
1208static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1209{
1210 /* NOTE: This is a generic implementation. RTP data is received. In
1211 * case of loopback the data is just sent back to its origin. All
1212 * other cases implement endpoint specific behaviour (e.g. how is the
1213 * destination connection determined?). That specific behaviour is
1214 * implemented by the callback function that is called at the end of
1215 * the function */
1216
1217 struct mgcp_conn_rtp *conn_src;
1218 struct mgcp_endpoint *endp;
1219 struct sockaddr_in addr;
1220
1221 char buf[RTP_BUF_SIZE];
1222 int proto;
Philipp Maierb9694552017-11-08 16:53:57 +01001223 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001224
1225 conn_src = (struct mgcp_conn_rtp *)fd->data;
1226 OSMO_ASSERT(conn_src);
1227 endp = conn_src->conn->endp;
1228 OSMO_ASSERT(endp);
1229
Philipp Maier230e4fc2017-11-28 09:38:45 +01001230 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x source conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001231 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_src->conn));
1232
1233 /* Receive packet */
Philipp Maierb9694552017-11-08 16:53:57 +01001234 len = mgcp_recv(&proto, &addr, buf, sizeof(buf), fd);
1235 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001236 return -1;
1237
1238 /* Check if the connection is in loopback mode, if yes, just send the
1239 * incoming data back to the origin */
1240 if (conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Philipp Maier5dbfc782017-12-12 16:20:25 +01001241 /* When we are in loopback mode, we loop back all incoming
1242 * packets back to their origin. We will use the originating
1243 * address data from the UDP packet header to patch the
1244 * outgoing address in connection on the fly */
1245 if (conn_src->end.rtp_port == 0) {
1246 conn_src->end.addr = addr.sin_addr;
1247 conn_src->end.rtp_port = addr.sin_port;
1248 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001249 return mgcp_send_rtp(proto, &addr, buf,
Philipp Maierb9694552017-11-08 16:53:57 +01001250 len, conn_src, conn_src);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001251 }
1252
1253 /* Execute endpoint specific implementation that handles the
1254 * dispatching of the RTP data */
Philipp Maierb9694552017-11-08 16:53:57 +01001255 return endp->type->dispatch_rtp_cb(proto, &addr, buf, len,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001256 conn_src->conn);
1257}
1258
1259/*! set IP Type of Service parameter.
1260 * \param[in] fd associated file descriptor
1261 * \param[in] tos dscp value
1262 * \returns 0 on success, -1 on ERROR */
1263int mgcp_set_ip_tos(int fd, int tos)
1264{
1265 int ret;
1266 ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1267
1268 if (ret < 0)
1269 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001270 return 0;
1271}
1272
Philipp Maier87bd9be2017-08-22 16:35:41 +02001273/*! bind RTP port to osmo_fd.
1274 * \param[in] source_addr source (local) address to bind on
1275 * \param[in] fd associated file descriptor
1276 * \param[in] port to bind on
1277 * \returns 0 on success, -1 on ERROR */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001278int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
1279{
Harald Welte8890dfa2017-11-17 15:09:30 +01001280 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001281
Harald Welte8890dfa2017-11-17 15:09:30 +01001282 rc = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
1283 NULL, 0, OSMO_SOCK_F_BIND);
1284 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001285 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001286 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001287 return -1;
1288 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001289 fd->fd = rc;
1290 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001291
1292 return 0;
1293}
1294
Philipp Maier87bd9be2017-08-22 16:35:41 +02001295/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001296static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001297 struct mgcp_rtp_end *rtp_end, int endpno)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001298{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001299 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1300 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1301
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001302 if (mgcp_create_bind(source_addr, &rtp_end->rtp,
1303 rtp_end->local_port) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001304 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001305 "endpoint:0x%x failed to create RTP port: %s:%d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001306 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001307 goto cleanup0;
1308 }
1309
1310 if (mgcp_create_bind(source_addr, &rtp_end->rtcp,
1311 rtp_end->local_port + 1) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001312 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001313 "endpoint:0x%x failed to create RTCP port: %s:%d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001314 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001315 goto cleanup1;
1316 }
1317
Philipp Maier87bd9be2017-08-22 16:35:41 +02001318 /* Set Type of Service (DSCP-Value) as configured via VTY */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001319 mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
1320 mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
1321
1322 rtp_end->rtp.when = BSC_FD_READ;
1323 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001324 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001325 "endpoint:0x%x failed to register RTP port %d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001326 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001327 goto cleanup2;
1328 }
1329
1330 rtp_end->rtcp.when = BSC_FD_READ;
1331 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001332 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001333 "endpoint:0x%x failed to register RTCP port %d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001334 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001335 goto cleanup3;
1336 }
1337
1338 return 0;
1339
1340cleanup3:
1341 osmo_fd_unregister(&rtp_end->rtp);
1342cleanup2:
1343 close(rtp_end->rtcp.fd);
1344 rtp_end->rtcp.fd = -1;
1345cleanup1:
1346 close(rtp_end->rtp.fd);
1347 rtp_end->rtp.fd = -1;
1348cleanup0:
1349 return -1;
1350}
1351
Philipp Maier87bd9be2017-08-22 16:35:41 +02001352/*! bind RTP port to endpoint/connection.
1353 * \param[in] endp endpoint that holds the RTP connection
1354 * \param[in] rtp_port port number to bind on
1355 * \param[in] conn associated RTP connection
1356 * \returns 0 on success, -1 on ERROR */
1357int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1358 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001359{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001360 char name[512];
1361 struct mgcp_rtp_end *end;
Philipp Maier1cb1e382017-11-02 17:16:04 +01001362 char local_ip_addr[INET_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001363
Philipp Maier01d24a32017-11-21 17:26:09 +01001364 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001365 end = &conn->end;
1366
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001367 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Philipp Maierc3413882017-10-27 12:26:54 +02001368 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001369 "endpoint:0x%x %u was already bound on conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001370 ENDPOINT_NUMBER(endp), rtp_port,
1371 mgcp_conn_dump(conn->conn));
1372
1373 /* Double bindings should never occour! Since we always allocate
1374 * connections dynamically and free them when they are not
1375 * needed anymore, there must be no previous binding leftover.
1376 * Should there be a connection bound twice, we have a serious
1377 * problem and must exit immediately! */
1378 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001379 }
1380
1381 end->local_port = rtp_port;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001382 end->rtp.cb = rtp_data_net;
1383 end->rtp.data = conn;
1384 end->rtcp.data = conn;
1385 end->rtcp.cb = rtp_data_net;
1386
Philipp Maier1cb1e382017-11-02 17:16:04 +01001387 mgcp_get_local_addr(local_ip_addr, conn);
1388
1389 return bind_rtp(endp->cfg, local_ip_addr, end,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001390 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001391}
1392
Philipp Maier87bd9be2017-08-22 16:35:41 +02001393/*! free allocated RTP and RTCP ports.
1394 * \param[in] end RTP end */
1395void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001396{
1397 if (end->rtp.fd != -1) {
1398 close(end->rtp.fd);
1399 end->rtp.fd = -1;
1400 osmo_fd_unregister(&end->rtp);
1401 }
1402
1403 if (end->rtcp.fd != -1) {
1404 close(end->rtcp.fd);
1405 end->rtcp.fd = -1;
1406 osmo_fd_unregister(&end->rtcp);
1407 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001408}