blob: e4ee8ad51b0c837831b8c4443fc579cfee7ac145 [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 Hofmeyr3243c7c2018-09-30 05:01:20 +020060static int rx_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *payload,
61 enum rtp_proto proto, struct sockaddr_in *from_addr);
62static int tx_rtp(struct mgcp_conn_rtp *conn_src, struct mgcp_conn_rtp *conn_dst,
63 enum rtp_proto proto, struct sockaddr_in *from_addr, struct msgb *payload);
64
Philipp Maier1cb1e382017-11-02 17:16:04 +010065/*! Determine the local rtp bind IP-address.
66 * \param[out] addr caller provided memory to store the resulting IP-Address
67 * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters
68 *
69 * The local bind IP-address is automatically selected by probing the
70 * IP-Address of the interface that is pointing towards the remote IP-Address,
71 * if no remote IP-Address is known yet, the statically configured
72 * IP-Addresses are used as fallback. */
73void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn)
74{
75
76 struct mgcp_endpoint *endp;
77 int rc;
78 endp = conn->conn->endp;
79
80 /* Try probing the local IP-Address */
81 if (endp->cfg->net_ports.bind_addr_probe && conn->end.addr.s_addr != 0) {
82 rc = osmo_sock_local_ip(addr, inet_ntoa(conn->end.addr));
83 if (rc < 0)
84 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +010085 "endpoint:0x%x CI:%s local interface auto detection failed, using configured addresses...\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +010086 ENDPOINT_NUMBER(endp), conn->conn->id);
87 else {
88 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +010089 "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 +010090 ENDPOINT_NUMBER(endp), conn->conn->id, addr,
91 inet_ntoa(conn->end.addr));
92 return;
93 }
94 }
95
96 /* Select from preconfigured IP-Addresses */
97 if (endp->cfg->net_ports.bind_addr) {
98 /* Check there is a bind IP for the RTP traffic configured,
99 * if so, use that IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100100 osmo_strlcpy(addr, endp->cfg->net_ports.bind_addr, INET_ADDRSTRLEN);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100101 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100102 "endpoint:0x%x CI:%s using configured rtp bind ip as local bind ip %s\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +0100103 ENDPOINT_NUMBER(endp), conn->conn->id, addr);
104 } else {
105 /* No specific bind IP is configured for the RTP traffic, so
106 * assume the IP where we listen for incoming MGCP messages
107 * as bind IP */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100108 osmo_strlcpy(addr, endp->cfg->source_addr, INET_ADDRSTRLEN);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100109 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100110 "endpoint:0x%x CI:%s using mgcp bind ip as local rtp bind ip: %s\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +0100111 ENDPOINT_NUMBER(endp), conn->conn->id, addr);
112 }
113}
114
Philipp Maier87bd9be2017-08-22 16:35:41 +0200115/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200116 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +0200117 * 1/codec_rate seconds. */
118static uint32_t get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200119{
120 struct timespec tp;
121 uint64_t ret;
122
Philipp Maier87bd9be2017-08-22 16:35:41 +0200123 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200124 return 0;
125
126 memset(&tp, 0, sizeof(tp));
127 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maierc3413882017-10-27 12:26:54 +0200128 LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200129
130 /* convert it to 1/unit seconds */
131 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200132 ret *= codec_rate;
133 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200134
135 return ret;
136}
137
Philipp Maier87bd9be2017-08-22 16:35:41 +0200138/*! send udp packet.
139 * \param[in] fd associated file descriptor
140 * \param[in] addr destination ip-address
141 * \param[in] port destination UDP port
142 * \param[in] buf buffer that holds the data to be send
143 * \param[in] len length of the data to be sent
144 * \returns bytes sent, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200145int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
146{
147 struct sockaddr_in out;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200148
Philipp Maierc3413882017-10-27 12:26:54 +0200149 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200150 "sending %i bytes length packet to %s:%u ...\n",
151 len, inet_ntoa(*addr), ntohs(port));
152
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200153 out.sin_family = AF_INET;
154 out.sin_port = port;
155 memcpy(&out.sin_addr, addr, sizeof(*addr));
156
157 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
158}
159
Philipp Maier87bd9be2017-08-22 16:35:41 +0200160/*! send RTP dummy packet (to keep NAT connection open).
161 * \param[in] endp mcgp endpoint that holds the RTP connection
162 * \param[in] conn associated RTP connection
163 * \returns bytes sent, -1 on error */
164int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200165{
166 static char buf[] = { MGCP_DUMMY_LOAD };
167 int rc;
168 int was_rtcp = 0;
169
Philipp Maier87bd9be2017-08-22 16:35:41 +0200170 OSMO_ASSERT(endp);
171 OSMO_ASSERT(conn);
172
Philipp Maierc3413882017-10-27 12:26:54 +0200173 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100174 "endpoint:0x%x sending dummy packet...\n", ENDPOINT_NUMBER(endp));
175 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200176 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
177
178 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
179 conn->end.rtp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200180
181 if (rc == -1)
182 goto failed;
183
184 if (endp->tcfg->omit_rtcp)
185 return rc;
186
187 was_rtcp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200188 rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
189 conn->end.rtcp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200190
191 if (rc >= 0)
192 return rc;
193
194failed:
Philipp Maierc3413882017-10-27 12:26:54 +0200195 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100196 "endpoint:0x%x Failed to send dummy %s packet.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200197 ENDPOINT_NUMBER(endp), was_rtcp ? "RTCP" : "RTP");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200198
199 return -1;
200}
201
Philipp Maier87bd9be2017-08-22 16:35:41 +0200202/* Compute timestamp alignment error */
203static int32_t ts_alignment_error(struct mgcp_rtp_stream_state *sstate,
204 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200205{
206 int32_t timestamp_delta;
207
208 if (ptime == 0)
209 return 0;
210
211 /* Align according to: T - Tlast = k * Tptime */
212 timestamp_delta = timestamp - sstate->last_timestamp;
213
214 return timestamp_delta % ptime;
215}
216
Philipp Maier87bd9be2017-08-22 16:35:41 +0200217/* Check timestamp and sequence number for plausibility */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200218static int check_rtp_timestamp(struct mgcp_endpoint *endp,
219 struct mgcp_rtp_state *state,
220 struct mgcp_rtp_stream_state *sstate,
221 struct mgcp_rtp_end *rtp_end,
222 struct sockaddr_in *addr,
223 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200224 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200225{
226 int32_t tsdelta;
227 int32_t timestamp_error;
228
229 /* Not fully intialized, skip */
230 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
231 return 0;
232
233 if (seq == sstate->last_seq) {
234 if (timestamp != sstate->last_timestamp) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200235 rate_ctr_inc(sstate->err_ts_ctr);
Philipp Maierc3413882017-10-27 12:26:54 +0200236 LOGP(DRTP, LOGL_ERROR,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200237 "The %s timestamp delta is != 0 but the sequence "
238 "number %d is the same, "
239 "TS offset: %d, SeqNo offset: %d "
240 "on 0x%x SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200241 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200242 text, seq,
Harald Welte33381352017-12-25 09:44:26 +0100243 state->patch.timestamp_offset, state->patch.seq_offset,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200244 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200245 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200246 }
247 return 0;
248 }
249
250 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200251 (int32_t)(timestamp - sstate->last_timestamp) /
252 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200253
254 if (tsdelta == 0) {
255 /* Don't update *tsdelta_out */
Philipp Maierc3413882017-10-27 12:26:54 +0200256 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200257 "The %s timestamp delta is %d "
258 "on 0x%x SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200259 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200260 text, tsdelta,
261 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200262 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200263
264 return 0;
265 }
266
267 if (sstate->last_tsdelta != tsdelta) {
268 if (sstate->last_tsdelta) {
Philipp Maierc3413882017-10-27 12:26:54 +0200269 LOGP(DRTP, LOGL_INFO,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200270 "The %s timestamp delta changes from %d to %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200271 "on 0x%x SSRC: %u timestamp: %u from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200272 text, sstate->last_tsdelta, tsdelta,
273 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200274 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200275 }
276 }
277
278 if (tsdelta_out)
279 *tsdelta_out = tsdelta;
280
281 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200282 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200283
284 if (timestamp_error) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200285 rate_ctr_inc(sstate->err_ts_ctr);
Philipp Maierc3413882017-10-27 12:26:54 +0200286 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200287 "The %s timestamp has an alignment error of %d "
288 "on 0x%x SSRC: %u "
289 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200290 "from %s:%d. ptime: %d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200291 text, timestamp_error,
292 ENDPOINT_NUMBER(endp), sstate->ssrc,
293 (int16_t)(seq - sstate->last_seq),
294 (int32_t)(timestamp - sstate->last_timestamp),
295 tsdelta,
296 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200297 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200298 }
299 return 1;
300}
301
302/* Set the timestamp offset according to the packet duration. */
303static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp,
304 struct mgcp_rtp_state *state,
305 struct mgcp_rtp_end *rtp_end,
306 struct sockaddr_in *addr,
307 int16_t delta_seq, uint32_t in_timestamp)
308{
309 int32_t tsdelta = state->packet_duration;
310 int timestamp_offset;
311 uint32_t out_timestamp;
312
313 if (tsdelta == 0) {
314 tsdelta = state->out_stream.last_tsdelta;
315 if (tsdelta != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200316 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200317 "A fixed packet duration is not available on 0x%x, "
318 "using last output timestamp delta instead: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200319 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200320 ENDPOINT_NUMBER(endp), tsdelta,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200321 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200322 } else {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200323 tsdelta = rtp_end->codec->rate * 20 / 1000;
Philipp Maierc3413882017-10-27 12:26:54 +0200324 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200325 "Fixed packet duration and last timestamp delta "
326 "are not available on 0x%x, "
327 "using fixed 20ms instead: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200328 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200329 ENDPOINT_NUMBER(endp), tsdelta,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200330 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200331 }
332 }
333
334 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
335 timestamp_offset = out_timestamp - in_timestamp;
336
Harald Welte33381352017-12-25 09:44:26 +0100337 if (state->patch.timestamp_offset != timestamp_offset) {
338 state->patch.timestamp_offset = timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200339
Philipp Maierc3413882017-10-27 12:26:54 +0200340 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200341 "Timestamp offset change on 0x%x SSRC: %u "
342 "SeqNo delta: %d, TS offset: %d, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200343 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200344 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100345 delta_seq, state->patch.timestamp_offset,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200346 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200347 }
348
349 return timestamp_offset;
350}
351
352/* Set the timestamp offset according to the packet duration. */
353static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp,
354 struct mgcp_rtp_state *state,
355 struct mgcp_rtp_end *rtp_end,
356 struct sockaddr_in *addr,
357 uint32_t timestamp)
358{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200359 int ts_error = 0;
360 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200361 int ptime = state->packet_duration;
362
363 /* Align according to: T + Toffs - Tlast = k * Tptime */
364
Philipp Maier87bd9be2017-08-22 16:35:41 +0200365 ts_error = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100366 timestamp + state->patch.timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200367
Philipp Maier87bd9be2017-08-22 16:35:41 +0200368 /* If there is an alignment error, we have to compensate it */
369 if (ts_error) {
Harald Welte33381352017-12-25 09:44:26 +0100370 state->patch.timestamp_offset += ptime - ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200371
Philipp Maierc3413882017-10-27 12:26:54 +0200372 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200373 "Corrected timestamp alignment error of %d on 0x%x SSRC: %u "
374 "new TS offset: %d, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200375 "from %s:%d\n",
376 ts_error,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200377 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100378 state->patch.timestamp_offset, inet_ntoa(addr->sin_addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200379 ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200380 }
381
Philipp Maier87bd9be2017-08-22 16:35:41 +0200382 /* Check we really managed to compensate the timestamp
383 * offset. There should not be any remaining error, failing
Harald Welte1d1b98f2017-12-25 10:03:40 +0100384 * here would point to a serous problem with the alignment
385 * error computation function */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200386 ts_check = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100387 timestamp + state->patch.timestamp_offset);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200388 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200389
Philipp Maier87bd9be2017-08-22 16:35:41 +0200390 /* Return alignment error before compensation */
391 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200392}
393
Philipp Maier87bd9be2017-08-22 16:35:41 +0200394/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
395 * \param[in] associated endpoint
396 * \param[in] destination RTP end
397 * \param[in,out] pointer to buffer with voice data
398 * \param[in] voice data length
Harald Welte1d1b98f2017-12-25 10:03:40 +0100399 * \param[in] maximum size of caller provided voice data buffer
Philipp Maier87bd9be2017-08-22 16:35:41 +0200400 * \returns ignores input parameters, return always 0 */
401int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
402 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200403 char *data, int *len, int buf_size)
404{
Philipp Maier230e4fc2017-11-28 09:38:45 +0100405 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x transcoding disabled\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200406 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200407 return 0;
408}
409
Philipp Maier87bd9be2017-08-22 16:35:41 +0200410/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
411 * \param[in] associated endpoint
Philipp Maieracc10352018-07-19 18:07:57 +0200412 * \param[in] destination RTP connnection
413 * \param[in] source RTP connection
Philipp Maier87bd9be2017-08-22 16:35:41 +0200414 * \returns ignores input parameters, return always 0 */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200415int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
Philipp Maieracc10352018-07-19 18:07:57 +0200416 struct mgcp_conn_rtp *conn_dst,
417 struct mgcp_conn_rtp *conn_src)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200418{
Philipp Maier230e4fc2017-11-28 09:38:45 +0100419 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x transcoding disabled\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200420 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200421 return 0;
422}
423
424void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
425 int *payload_type,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200426 const char **audio_name,
427 const char **fmtp_extra,
428 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200429{
Philipp Maierc3413882017-10-27 12:26:54 +0200430 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100431 "endpoint:0x%x conn:%s using format defaults\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200432 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
433
Philipp Maierbc0346e2018-06-07 09:52:16 +0200434 *payload_type = conn->end.codec->payload_type;
435 *audio_name = conn->end.codec->audio_name;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200436 *fmtp_extra = conn->end.fmtp_extra;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200437}
438
Philipp Maier87bd9be2017-08-22 16:35:41 +0200439void mgcp_rtp_annex_count(struct mgcp_endpoint *endp,
440 struct mgcp_rtp_state *state, const uint16_t seq,
441 const int32_t transit, const uint32_t ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200442{
443 int32_t d;
444
445 /* initialize or re-initialize */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100446 if (!state->stats.initialized || state->stats.ssrc != ssrc) {
447 state->stats.initialized = 1;
448 state->stats.base_seq = seq;
449 state->stats.max_seq = seq - 1;
450 state->stats.ssrc = ssrc;
451 state->stats.jitter = 0;
452 state->stats.transit = transit;
453 state->stats.cycles = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200454 } else {
455 uint16_t udelta;
456
Philipp Maier87bd9be2017-08-22 16:35:41 +0200457 /* The below takes the shape of the validation of
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200458 * Appendix A. Check if there is something weird with
459 * the sequence number, otherwise check for a wrap
460 * around in the sequence number.
461 * It can't wrap during the initialization so let's
462 * skip it here. The Appendix A probably doesn't have
Philipp Maier87bd9be2017-08-22 16:35:41 +0200463 * this issue because of the probation. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100464 udelta = seq - state->stats.max_seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200465 if (udelta < RTP_MAX_DROPOUT) {
Harald Welte49e3d5a2017-12-25 09:47:57 +0100466 if (seq < state->stats.max_seq)
467 state->stats.cycles += RTP_SEQ_MOD;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200468 } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
Philipp Maierc3413882017-10-27 12:26:54 +0200469 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200470 "RTP seqno made a very large jump on 0x%x delta: %u\n",
471 ENDPOINT_NUMBER(endp), udelta);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200472 }
473 }
474
Philipp Maier87bd9be2017-08-22 16:35:41 +0200475 /* Calculate the jitter between the two packages. The TS should be
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200476 * taken closer to the read function. This was taken from the
477 * Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
Philipp Maier87bd9be2017-08-22 16:35:41 +0200478 * resolution. */
Harald Welte49e3d5a2017-12-25 09:47:57 +0100479 d = transit - state->stats.transit;
480 state->stats.transit = transit;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200481 if (d < 0)
482 d = -d;
Harald Welte49e3d5a2017-12-25 09:47:57 +0100483 state->stats.jitter += d - ((state->stats.jitter + 8) >> 4);
484 state->stats.max_seq = seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200485}
486
Philipp Maier6931f9a2018-07-26 09:29:31 +0200487/* There may be different payload type numbers negotiated for two connections.
488 * Patch the payload type of an RTP packet so that it uses the payload type
489 * that is valid for the destination connection (conn_dst) */
490static int mgcp_patch_pt(struct mgcp_conn_rtp *conn_src,
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200491 struct mgcp_conn_rtp *conn_dst, struct msgb *msg)
Philipp Maier6931f9a2018-07-26 09:29:31 +0200492{
493 struct rtp_hdr *rtp_hdr;
494 uint8_t pt_in;
495 int pt_out;
496
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200497 OSMO_ASSERT(msgb_l3len(msg) >= sizeof(struct rtp_hdr));
498 rtp_hdr = (struct rtp_hdr *)msgb_l3(msg);
Philipp Maier6931f9a2018-07-26 09:29:31 +0200499
500 pt_in = rtp_hdr->payload_type;
501 pt_out = mgcp_codec_pt_translate(conn_src, conn_dst, pt_in);
502 if (pt_out < 0)
503 return -EINVAL;
504
505 rtp_hdr->payload_type = (uint8_t) pt_out;
506 return 0;
507}
508
Philipp Maier87bd9be2017-08-22 16:35:41 +0200509/* The RFC 3550 Appendix A assumes there are multiple sources but
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200510 * some of the supported endpoints (e.g. the nanoBTS) can only handle
511 * one source and this code will patch RTP header to appear as if there
512 * is only one source.
513 * There is also no probation period for new sources. Every RTP header
Philipp Maier87bd9be2017-08-22 16:35:41 +0200514 * we receive will be seen as a switch in streams. */
515void mgcp_patch_and_count(struct mgcp_endpoint *endp,
516 struct mgcp_rtp_state *state,
517 struct mgcp_rtp_end *rtp_end,
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200518 struct sockaddr_in *addr, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200519{
520 uint32_t arrival_time;
521 int32_t transit;
522 uint16_t seq;
523 uint32_t timestamp, ssrc;
524 struct rtp_hdr *rtp_hdr;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200525 int payload = rtp_end->codec->payload_type;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200526 unsigned int len = msgb_l3len(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200527
528 if (len < sizeof(*rtp_hdr))
529 return;
530
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200531 rtp_hdr = (struct rtp_hdr *)msgb_l3(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200532 seq = ntohs(rtp_hdr->sequence);
533 timestamp = ntohl(rtp_hdr->timestamp);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200534 arrival_time = get_current_ts(rtp_end->codec->rate);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200535 ssrc = ntohl(rtp_hdr->ssrc);
536 transit = arrival_time - timestamp;
537
538 mgcp_rtp_annex_count(endp, state, seq, transit, ssrc);
539
540 if (!state->initialized) {
541 state->initialized = 1;
542 state->in_stream.last_seq = seq - 1;
Harald Welte33381352017-12-25 09:44:26 +0100543 state->in_stream.ssrc = state->patch.orig_ssrc = ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200544 state->in_stream.last_tsdelta = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200545 state->packet_duration =
546 mgcp_rtp_packet_duration(endp, rtp_end);
Philipp Maier0ec1d4e2018-05-16 11:09:42 +0200547 state->out_stream.last_seq = seq - 1;
548 state->out_stream.ssrc = state->patch.orig_ssrc = ssrc;
549 state->out_stream.last_tsdelta = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200550 state->out_stream.last_timestamp = timestamp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200551 state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
Philipp Maierc3413882017-10-27 12:26:54 +0200552 LOGP(DRTP, LOGL_INFO,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100553 "endpoint:0x%x initializing stream, SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200554 "pkt-duration: %d, from %s:%d\n",
555 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100556 state->patch.seq_offset, 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 if (state->packet_duration == 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200559 state->packet_duration =
Philipp Maierbc0346e2018-06-07 09:52:16 +0200560 rtp_end->codec->rate * 20 / 1000;
Philipp Maierc3413882017-10-27 12:26:54 +0200561 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100562 "endpoint:0x%x fixed packet duration is not available, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200563 "using fixed 20ms instead: %d from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200564 ENDPOINT_NUMBER(endp), state->packet_duration,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200565 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200566 }
567 } else if (state->in_stream.ssrc != ssrc) {
Philipp Maierc3413882017-10-27 12:26:54 +0200568 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100569 "endpoint:0x%x SSRC changed: %u -> %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200570 "from %s:%d\n",
571 ENDPOINT_NUMBER(endp),
572 state->in_stream.ssrc, rtp_hdr->ssrc,
573 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200574
575 state->in_stream.ssrc = ssrc;
576 if (rtp_end->force_constant_ssrc) {
577 int16_t delta_seq;
578
579 /* Always increment seqno by 1 */
Harald Welte33381352017-12-25 09:44:26 +0100580 state->patch.seq_offset =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200581 (state->out_stream.last_seq + 1) - seq;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200582
583 /* Estimate number of packets that would have been sent */
584 delta_seq =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200585 (arrival_time - state->in_stream.last_arrival_time
586 + state->packet_duration / 2) /
587 state->packet_duration;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200588
589 adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
590 delta_seq, timestamp);
591
Harald Welte33381352017-12-25 09:44:26 +0100592 state->patch.patch_ssrc = 1;
593 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200594 if (rtp_end->force_constant_ssrc != -1)
595 rtp_end->force_constant_ssrc -= 1;
596
Philipp Maierc3413882017-10-27 12:26:54 +0200597 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100598 "endpoint:0x%x SSRC patching enabled, SSRC: %u "
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200599 "SeqNo offset: %d, TS offset: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200600 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200601 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100602 state->patch.seq_offset, state->patch.timestamp_offset,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200603 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200604 }
605
606 state->in_stream.last_tsdelta = 0;
607 } else {
608 /* Compute current per-packet timestamp delta */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200609 check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
610 addr, seq, timestamp, "input",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200611 &state->in_stream.last_tsdelta);
612
Harald Welte33381352017-12-25 09:44:26 +0100613 if (state->patch.patch_ssrc)
614 ssrc = state->patch.orig_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200615 }
616
617 /* Save before patching */
618 state->in_stream.last_timestamp = timestamp;
619 state->in_stream.last_seq = seq;
620 state->in_stream.last_arrival_time = arrival_time;
621
622 if (rtp_end->force_aligned_timing &&
623 state->out_stream.ssrc == ssrc && state->packet_duration)
624 /* Align the timestamp offset */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200625 align_rtp_timestamp_offset(endp, state, rtp_end, addr,
626 timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200627
628 /* Store the updated SSRC back to the packet */
Harald Welte33381352017-12-25 09:44:26 +0100629 if (state->patch.patch_ssrc)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200630 rtp_hdr->ssrc = htonl(ssrc);
631
632 /* Apply the offset and store it back to the packet.
633 * This won't change anything if the offset is 0, so the conditional is
634 * omitted. */
Harald Welte33381352017-12-25 09:44:26 +0100635 seq += state->patch.seq_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200636 rtp_hdr->sequence = htons(seq);
Harald Welte33381352017-12-25 09:44:26 +0100637 timestamp += state->patch.timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200638 rtp_hdr->timestamp = htonl(timestamp);
639
640 /* Check again, whether the timestamps are still valid */
641 if (state->out_stream.ssrc == ssrc)
642 check_rtp_timestamp(endp, state, &state->out_stream, rtp_end,
643 addr, seq, timestamp, "output",
644 &state->out_stream.last_tsdelta);
645
646 /* Save output values */
647 state->out_stream.last_seq = seq;
648 state->out_stream.last_timestamp = timestamp;
649 state->out_stream.ssrc = ssrc;
650
651 if (payload < 0)
652 return;
653
654#if 0
Philipp Maierc3413882017-10-27 12:26:54 +0200655 DEBUGP(DRTP,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100656 "endpoint:0x%x payload hdr payload %u -> endp payload %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200657 ENDPOINT_NUMBER(endp), rtp_hdr->payload_type, payload);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200658 rtp_hdr->payload_type = payload;
659#endif
660}
661
Philipp Maier87bd9be2017-08-22 16:35:41 +0200662/* Forward data to a debug tap. This is debug function that is intended for
663 * debugging the voice traffic with tools like gstreamer */
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200664static void forward_data(int fd, struct mgcp_rtp_tap *tap, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200665{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100666 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200667
Philipp Maiere6f172d2017-11-07 12:00:01 +0100668 if (!tap->enabled)
669 return;
670
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200671 rc = sendto(fd, msgb_l3(msg), msgb_l3len(msg), 0, (struct sockaddr *)&tap->forward,
Philipp Maiere6f172d2017-11-07 12:00:01 +0100672 sizeof(tap->forward));
673
674 if (rc < 0)
675 LOGP(DRTP, LOGL_ERROR,
676 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200677}
678
Philipp Maier87bd9be2017-08-22 16:35:41 +0200679/*! Send RTP/RTCP data to a specified destination connection.
680 * \param[in] endp associated endpoint (for configuration, logging)
681 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP
682 * \param[in] spoofed source address (set to NULL to disable)
683 * \param[in] buf buffer that contains the RTP/RTCP data
684 * \param[in] len length of the buffer that contains the RTP/RTCP data
685 * \param[in] conn_src associated source connection
686 * \param[in] conn_dst associated destination connection
687 * \returns 0 on success, -1 on ERROR */
688int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200689 struct msgb *msg, struct mgcp_conn_rtp *conn_src,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200690 struct mgcp_conn_rtp *conn_dst)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200691{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200692 /*! When no destination connection is available (e.g. when only one
693 * connection in loopback mode exists), then the source connection
694 * shall be specified as destination connection */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200695
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200696 struct mgcp_trunk_config *tcfg = endp->tcfg;
697 struct mgcp_rtp_end *rtp_end;
698 struct mgcp_rtp_state *rtp_state;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200699 char *dest_name;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200700 int rc;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200701 int len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200702
Philipp Maier87bd9be2017-08-22 16:35:41 +0200703 OSMO_ASSERT(conn_src);
704 OSMO_ASSERT(conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200705
Philipp Maier87bd9be2017-08-22 16:35:41 +0200706 if (is_rtp) {
Philipp Maierc3413882017-10-27 12:26:54 +0200707 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100708 "endpoint:0x%x delivering RTP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200709 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200710 } else {
Philipp Maierc3413882017-10-27 12:26:54 +0200711 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100712 "endpoint:0x%x delivering RTCP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200713 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200714 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200715
Philipp Maierc3413882017-10-27 12:26:54 +0200716 LOGP(DRTP, LOGL_DEBUG,
Neels Hofmeyr7066af82018-07-23 18:28:36 +0200717 "endpoint:0x%x loop:%d, mode:%d%s\n",
718 ENDPOINT_NUMBER(endp), tcfg->audio_loop, conn_src->conn->mode,
719 conn_src->conn->mode == MGCP_CONN_LOOPBACK ? " (loopback)" : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200720
Philipp Maier6931f9a2018-07-26 09:29:31 +0200721 /* FIXME: It is legal that the payload type on the egress connection is
722 * different from the payload type that has been negotiated on the
723 * ingress connection. Essentially the codecs are the same so we can
724 * match them and patch the payload type. However, if we can not find
725 * the codec pendant (everything ist equal except the PT), we are of
726 * course unable to patch the payload type. A situation like this
727 * should not occur if transcoding is consequently avoided. Until
728 * we have transcoding support in osmo-mgw we can not resolve this. */
Philipp Maierda895b12018-08-03 12:16:37 +0200729 if (is_rtp) {
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200730 rc = mgcp_patch_pt(conn_src, conn_dst, msg);
Philipp Maierda895b12018-08-03 12:16:37 +0200731 if (rc < 0) {
732 LOGP(DRTP, LOGL_ERROR,
733 "endpoint:0x%x can not patch PT because no suitable egress codec was found.\n",
734 ENDPOINT_NUMBER(endp));
735 }
Philipp Maier6931f9a2018-07-26 09:29:31 +0200736 }
737
Philipp Maier87bd9be2017-08-22 16:35:41 +0200738 /* Note: In case of loopback configuration, both, the source and the
739 * destination will point to the same connection. */
740 rtp_end = &conn_dst->end;
741 rtp_state = &conn_src->state;
742 dest_name = conn_dst->conn->name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200743
744 if (!rtp_end->output_enabled) {
Philipp Maiercede2a42018-07-03 14:14:21 +0200745 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_DROPPED_PACKETS_CTR]);
Philipp Maierc3413882017-10-27 12:26:54 +0200746 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100747 "endpoint:0x%x output disabled, drop to %s %s "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200748 "rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200749 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200750 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200751 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200752 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200753 );
754 } else if (is_rtp) {
755 int cont;
756 int nbytes = 0;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200757 int buflen = msgb_l3len(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200758 do {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200759 /* Run transcoder */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200760 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200761 msgb_l3(msg), &buflen,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200762 RTP_BUF_SIZE);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200763 if (cont < 0)
764 break;
765
Philipp Maier87bd9be2017-08-22 16:35:41 +0200766 if (addr)
767 mgcp_patch_and_count(endp, rtp_state, rtp_end,
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200768 addr, msg);
Philipp Maierc3413882017-10-27 12:26:54 +0200769 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100770 "endpoint:0x%x process/send to %s %s "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200771 "rtp_port:%u rtcp_port:%u\n",
772 ENDPOINT_NUMBER(endp), dest_name,
773 inet_ntoa(rtp_end->addr), ntohs(rtp_end->rtp_port),
774 ntohs(rtp_end->rtcp_port)
775 );
776
777 /* Forward a copy of the RTP data to a debug ip/port */
778 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200779 msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200780
781 /* FIXME: HACK HACK HACK. See OS#2459.
782 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
783 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
784 * cells (as long as we patch only the first RTP payload in each stream).
785 */
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200786 if (!rtp_state->patched_first_rtp_payload
787 && conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200788 uint8_t *data = ((uint8_t *)msgb_l3(msg)) + 12;
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200789 if (data[0] == 0xe0) {
790 data[0] = 0xe4;
791 data[1] = 0x00;
792 rtp_state->patched_first_rtp_payload = true;
793 LOGP(DRTP, LOGL_DEBUG,
794 "endpoint:0x%x Patching over first two bytes"
795 " to fake an IuUP Initialization Ack\n",
796 ENDPOINT_NUMBER(endp));
797 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200798 }
799
Philipp Maier87bd9be2017-08-22 16:35:41 +0200800 len = mgcp_udp_send(rtp_end->rtp.fd,
801 &rtp_end->addr,
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200802 rtp_end->rtp_port,
803 msgb_l3(msg), msgb_l3len(msg));
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 Hofmeyr3243c7c2018-09-30 05:01:20 +0200826 rtp_end->rtcp_port, msgb_l3(msg), msgb_l3len(msg));
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,
917 "endpoint:0x%x destination IP-address and rtp port is (not yet) known\n",
918 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;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200946 unsigned int buf_size = msgb_l3len(msg);
Philipp Maier4dba7692018-08-03 12:20:52 +0200947
948 /* RTPC packets that are just a header without data do not make
949 * any sense. */
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200950 if (buf_size < sizeof(struct rtcp_hdr)) {
951 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP packet too short (%u < %zu)\n",
952 buf_size, sizeof(struct rtcp_hdr));
Philipp Maier4dba7692018-08-03 12:20:52 +0200953 return -EINVAL;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200954 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200955
956 /* Make sure that the length of the received packet does not exceed
957 * the available buffer size */
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200958 hdr = (struct rtcp_hdr *)msgb_l3(msg);
Philipp Maier4dba7692018-08-03 12:20:52 +0200959 len = (osmo_ntohs(hdr->length) + 1) * 4;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200960 if (len > buf_size) {
961 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header length exceeds packet size (%u > %u)\n",
962 len, buf_size);
Philipp Maier4dba7692018-08-03 12:20:52 +0200963 return -EINVAL;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200964 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200965
966 /* Make sure we accept only packets that have a proper packet type set
967 * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
968 type = hdr->type;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200969 if ((type < 192 || type > 195) && (type < 200 || type > 213)) {
970 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTCP header: invalid type: %u\n", type);
Philipp Maier4dba7692018-08-03 12:20:52 +0200971 return -EINVAL;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200972 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200973
974 return 0;
975}
976
977/* Do some basic checks to make sure that the RTP packets we are going to
978 * process are not complete garbage */
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200979static int check_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *msg)
Philipp Maier4dba7692018-08-03 12:20:52 +0200980{
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +0200981 size_t min_size = sizeof(struct rtp_hdr);
982 if (conn_src->iuup)
983 min_size += sizeof(struct osmo_iuup_hdr_data);
984 if (msgb_l3len(msg) < min_size) {
985 LOG_CONN_RTP(conn_src, LOGL_ERROR, "RTP packet too short (%u < %zu)\n",
986 msgb_l3len(msg), min_size);
987 return -1;
988 }
Philipp Maier4dba7692018-08-03 12:20:52 +0200989
990 /* FIXME: Add more checks, the reason why we do not check more than
991 * the length is because we currently handle IUUP packets as RTP
992 * packets, so they must pass this check, if we weould be more
993 * strict here, we would possibly break 3G. (see also FIXME note
994 * below */
995
996 return 0;
997}
998
Philipp Maier87bd9be2017-08-22 16:35:41 +0200999/* Send RTP data. Possible options are standard RTP packet
1000 * transmission or trsmission via an osmux connection */
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001001static int mgcp_send_rtp(int proto, struct sockaddr_in *addr,
1002 struct msgb *payload,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001003 struct mgcp_conn_rtp *conn_src,
1004 struct mgcp_conn_rtp *conn_dst)
1005{
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001006 struct mgcp_endpoint *endp = conn_src->conn->endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001007
Philipp Maier230e4fc2017-11-28 09:38:45 +01001008 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x destin conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001009 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_dst->conn));
1010
1011 /* Before we try to deliver the packet, we check if the destination
1012 * port and IP-Address make sense at all. If not, we will be unable
1013 * to deliver the packet. */
1014 if (check_rtp_destin(conn_dst) != 0)
1015 return -1;
1016
1017 /* Depending on the RTP connection type, deliver the RTP packet to the
1018 * destination connection. */
1019 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001020 case MGCP_RTP_DEFAULT:
Philipp Maierc3413882017-10-27 12:26:54 +02001021 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001022 "endpoint:0x%x endpoint type is MGCP_RTP_DEFAULT, "
Philipp Maier87bd9be2017-08-22 16:35:41 +02001023 "using mgcp_send() to forward data directly\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001024 ENDPOINT_NUMBER(endp));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001025 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001026 addr, payload, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001027 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001028 case MGCP_OSMUX_BSC:
Philipp Maierc3413882017-10-27 12:26:54 +02001029 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001030 "endpoint:0x%x endpoint type is MGCP_OSMUX_BSC_NAT, "
Philipp Maier87bd9be2017-08-22 16:35:41 +02001031 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n",
1032 ENDPOINT_NUMBER(endp));
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001033 return osmux_xfrm_to_osmux(msgb_l3(payload), msgb_l3len(payload), conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001034 }
1035
Philipp Maier87bd9be2017-08-22 16:35:41 +02001036 /* If the data has not been handled/forwarded until here, it will
1037 * be discarded, this should not happen, normally the MGCP type
1038 * should be properly set */
Philipp Maierc3413882017-10-27 12:26:54 +02001039 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001040 "endpoint:0x%x bad MGCP type -- data discarded!\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001041 ENDPOINT_NUMBER(endp));
1042
1043 return -1;
1044}
1045
1046/*! dispatch incoming RTP packet to opposite RTP connection.
1047 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP)
1048 * \param[in] addr socket address where the RTP packet has been received from
1049 * \param[in] buf buffer that hold the RTP payload
1050 * \param[in] buf_size size data length of buf
1051 * \param[in] conn originating connection
1052 * \returns 0 on success, -1 on ERROR */
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001053int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr,
1054 struct msgb *payload, struct mgcp_conn *conn)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001055{
1056 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 Hofmeyr3243c7c2018-09-30 05:01:20 +02001099 return tx_rtp(&conn->u.rtp, &conn_dst->u.rtp, proto, addr, payload);
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{
1123 return msgb_l3len(msg) == 1 && ((char*)msgb_l3(msg))[0] == MGCP_DUMMY_LOAD;
1124}
1125
1126int rx_rtp_from_iuup(struct msgb *msg, void *node_priv, void *pdu_priv)
1127{
1128 struct mgcp_conn_rtp *conn_src = node_priv;
1129 struct sockaddr_in *from_addr = pdu_priv;
1130 return rx_rtp(conn_src, msg, MGCP_PROTO_RTP, from_addr);
1131}
1132
1133struct pdu_ctx {
1134 struct sockaddr_in *from_addr;
1135 struct mgcp_conn_rtp *conn_src;
1136};
1137
1138int tx_pdu_from_iuup(struct msgb *msg, void *node_priv, void *pdu_priv)
1139{
1140 struct mgcp_conn_rtp *conn_dst = node_priv;
1141 struct pdu_ctx *p = pdu_priv;
1142 return mgcp_send_rtp(MGCP_PROTO_RTP, p->from_addr, msg, p->conn_src, conn_dst);
1143}
1144
1145static void init_iuup(struct mgcp_conn_rtp *conn_src)
1146{
1147 struct osmo_iuup_cn_cfg cfg = {
1148 .node_priv = conn_src,
1149 .rx_payload = rx_rtp_from_iuup,
1150 .tx_msg = tx_pdu_from_iuup,
1151 };
1152
1153 osmo_iuup_cn_init(conn_src, &cfg, "%d@ I:%s",
1154 ENDPOINT_NUMBER(conn_src->conn->endp), conn_src->conn->id);
1155}
1156
Philipp Maier87bd9be2017-08-22 16:35:41 +02001157/* Handle incoming RTP data from NET */
1158static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1159{
1160 /* NOTE: This is a generic implementation. RTP data is received. In
1161 * case of loopback the data is just sent back to its origin. All
1162 * other cases implement endpoint specific behaviour (e.g. how is the
1163 * destination connection determined?). That specific behaviour is
1164 * implemented by the callback function that is called at the end of
1165 * the function */
1166
1167 struct mgcp_conn_rtp *conn_src;
1168 struct mgcp_endpoint *endp;
1169 struct sockaddr_in addr;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001170 socklen_t slen = sizeof(addr);
1171 int ret;
1172 enum rtp_proto proto;
1173 struct msgb *msg = msgb_alloc_headroom(RTP_BUF_SIZE + OSMO_IUUP_HEADROOM,
1174 OSMO_IUUP_HEADROOM, "RTP-rx");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001175
1176 conn_src = (struct mgcp_conn_rtp *)fd->data;
1177 OSMO_ASSERT(conn_src);
1178 endp = conn_src->conn->endp;
1179 OSMO_ASSERT(endp);
1180
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001181 proto = (fd == &conn_src->end.rtp)? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001182
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001183 ret = recvfrom(fd->fd, msg->data, msg->data_len, 0, (struct sockaddr *)&addr, &slen);
1184
1185 if (ret <= 0) {
1186 LOG_CONN_RTP(conn_src, LOGL_ERROR, "recvfrom error: %s\n", strerror(errno));
1187 msgb_free(msg);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001188 return -1;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001189 }
1190
1191 /* By default, indicate that RTP payload starts right from the buffer's beginning. */
1192 msg->l3h = msgb_put(msg, ret);
1193
1194 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "%s: rx %u bytes from %s:%u\n",
1195 proto == MGCP_PROTO_RTP ? "RTP" : "RTPC",
1196 msgb_l3len(msg), inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
1197
1198 if ((proto == MGCP_PROTO_RTP && check_rtp(conn_src, msg))
1199 || (proto == MGCP_PROTO_RTCP && check_rtcp(conn_src, msg))) {
1200 /* Logging happened in the two check_ functions */
1201 return -1;
1202 }
1203
1204 if (is_dummy_msg(proto, msg)) {
1205 LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx dummy packet (dropped)\n");
1206 return 0;
1207 }
1208
1209 /* Increment RX statistics */
1210 rate_ctr_inc(&conn_src->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR]);
1211 rate_ctr_add(&conn_src->rate_ctr_group->ctr[RTP_OCTETS_RX_CTR], msgb_l3len(msg));
1212 /* FIXME: count RTP and RTCP separately, also count IuUP payload-less separately */
1213
1214 /* Forward a copy of the RTP data to a debug ip/port */
1215 forward_data(fd->fd, &conn_src->tap_in, msg);
1216
1217 if (proto == MGCP_PROTO_RTP && osmo_iuup_is_init(msg))
1218 init_iuup(conn_src);
1219
1220 if (conn_src->iuup && proto == MGCP_PROTO_RTP)
1221 return osmo_iuup_cn_rx_pdu(conn_src->iuup, msg, &addr);
1222 else
1223 return rx_rtp(conn_src, msg, proto, &addr);
1224}
1225
1226static int rx_rtp(struct mgcp_conn_rtp *conn_src, struct msgb *payload,
1227 enum rtp_proto proto, struct sockaddr_in *from_addr)
1228{
1229 struct mgcp_endpoint *endp = conn_src->conn->endp;
1230 struct mgcp_trunk_config *tcfg = endp->tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001231
1232 /* Check if the connection is in loopback mode, if yes, just send the
1233 * incoming data back to the origin */
1234 if (conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Philipp Maier5dbfc782017-12-12 16:20:25 +01001235 /* When we are in loopback mode, we loop back all incoming
1236 * packets back to their origin. We will use the originating
1237 * address data from the UDP packet header to patch the
1238 * outgoing address in connection on the fly */
1239 if (conn_src->end.rtp_port == 0) {
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001240 conn_src->end.addr = from_addr->sin_addr;
1241 conn_src->end.rtp_port = from_addr->sin_port;
Philipp Maier5dbfc782017-12-12 16:20:25 +01001242 }
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001243 return tx_rtp(conn_src, conn_src, proto, from_addr, payload);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001244 }
1245
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001246 /* Check if the origin of the RTP packet seems plausible */
1247 if (!tcfg->rtp_accept_all && check_rtp_origin(conn_src, from_addr))
1248 return -1;
1249
Philipp Maier87bd9be2017-08-22 16:35:41 +02001250 /* Execute endpoint specific implementation that handles the
1251 * dispatching of the RTP data */
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001252 return conn_src->conn->endp->type->dispatch_rtp_cb(proto, from_addr, payload, conn_src->conn);
1253}
1254
1255static int tx_rtp(struct mgcp_conn_rtp *conn_src, struct mgcp_conn_rtp *conn_dst,
1256 enum rtp_proto proto, struct sockaddr_in *from_addr, struct msgb *payload)
1257{
1258 if (conn_dst->iuup && proto == MGCP_PROTO_RTP)
1259 return osmo_iuup_cn_tx_payload(conn_dst->iuup, payload, from_addr);
1260 else
1261 return mgcp_send_rtp(proto, from_addr, payload, conn_src, conn_dst);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001262}
1263
1264/*! set IP Type of Service parameter.
1265 * \param[in] fd associated file descriptor
1266 * \param[in] tos dscp value
1267 * \returns 0 on success, -1 on ERROR */
1268int mgcp_set_ip_tos(int fd, int tos)
1269{
1270 int ret;
1271 ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1272
1273 if (ret < 0)
1274 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001275 return 0;
1276}
1277
Philipp Maier87bd9be2017-08-22 16:35:41 +02001278/*! bind RTP port to osmo_fd.
1279 * \param[in] source_addr source (local) address to bind on
1280 * \param[in] fd associated file descriptor
1281 * \param[in] port to bind on
1282 * \returns 0 on success, -1 on ERROR */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001283int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
1284{
Harald Welte8890dfa2017-11-17 15:09:30 +01001285 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001286
Harald Welte8890dfa2017-11-17 15:09:30 +01001287 rc = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
1288 NULL, 0, OSMO_SOCK_F_BIND);
1289 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001290 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001291 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001292 return -1;
1293 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001294 fd->fd = rc;
1295 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001296
1297 return 0;
1298}
1299
Philipp Maier87bd9be2017-08-22 16:35:41 +02001300/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001301static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001302 struct mgcp_rtp_end *rtp_end, int endpno)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001303{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001304 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1305 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1306
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001307 if (mgcp_create_bind(source_addr, &rtp_end->rtp,
1308 rtp_end->local_port) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001309 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001310 "endpoint:0x%x failed to create RTP port: %s:%d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001311 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001312 goto cleanup0;
1313 }
1314
1315 if (mgcp_create_bind(source_addr, &rtp_end->rtcp,
1316 rtp_end->local_port + 1) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001317 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001318 "endpoint:0x%x failed to create RTCP port: %s:%d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001319 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001320 goto cleanup1;
1321 }
1322
Philipp Maier87bd9be2017-08-22 16:35:41 +02001323 /* Set Type of Service (DSCP-Value) as configured via VTY */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001324 mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
1325 mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
1326
1327 rtp_end->rtp.when = BSC_FD_READ;
1328 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001329 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001330 "endpoint:0x%x failed to register RTP port %d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001331 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001332 goto cleanup2;
1333 }
1334
1335 rtp_end->rtcp.when = BSC_FD_READ;
1336 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001337 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001338 "endpoint:0x%x failed to register RTCP port %d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001339 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001340 goto cleanup3;
1341 }
1342
1343 return 0;
1344
1345cleanup3:
1346 osmo_fd_unregister(&rtp_end->rtp);
1347cleanup2:
1348 close(rtp_end->rtcp.fd);
1349 rtp_end->rtcp.fd = -1;
1350cleanup1:
1351 close(rtp_end->rtp.fd);
1352 rtp_end->rtp.fd = -1;
1353cleanup0:
1354 return -1;
1355}
1356
Philipp Maier87bd9be2017-08-22 16:35:41 +02001357/*! bind RTP port to endpoint/connection.
1358 * \param[in] endp endpoint that holds the RTP connection
1359 * \param[in] rtp_port port number to bind on
1360 * \param[in] conn associated RTP connection
1361 * \returns 0 on success, -1 on ERROR */
1362int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1363 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001364{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001365 char name[512];
1366 struct mgcp_rtp_end *end;
Philipp Maier1cb1e382017-11-02 17:16:04 +01001367 char local_ip_addr[INET_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001368
Philipp Maier01d24a32017-11-21 17:26:09 +01001369 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001370 end = &conn->end;
1371
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001372 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Philipp Maierc3413882017-10-27 12:26:54 +02001373 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001374 "endpoint:0x%x %u was already bound on conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001375 ENDPOINT_NUMBER(endp), rtp_port,
1376 mgcp_conn_dump(conn->conn));
1377
1378 /* Double bindings should never occour! Since we always allocate
1379 * connections dynamically and free them when they are not
1380 * needed anymore, there must be no previous binding leftover.
1381 * Should there be a connection bound twice, we have a serious
1382 * problem and must exit immediately! */
1383 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001384 }
1385
1386 end->local_port = rtp_port;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001387 end->rtp.cb = rtp_data_net;
1388 end->rtp.data = conn;
1389 end->rtcp.data = conn;
1390 end->rtcp.cb = rtp_data_net;
1391
Philipp Maier1cb1e382017-11-02 17:16:04 +01001392 mgcp_get_local_addr(local_ip_addr, conn);
1393
1394 return bind_rtp(endp->cfg, local_ip_addr, end,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001395 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001396}
1397
Philipp Maier87bd9be2017-08-22 16:35:41 +02001398/*! free allocated RTP and RTCP ports.
1399 * \param[in] end RTP end */
1400void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001401{
1402 if (end->rtp.fd != -1) {
1403 close(end->rtp.fd);
1404 end->rtp.fd = -1;
1405 osmo_fd_unregister(&end->rtp);
1406 }
1407
1408 if (end->rtcp.fd != -1) {
1409 close(end->rtcp.fd);
1410 end->rtcp.fd = -1;
1411 osmo_fd_unregister(&end->rtcp);
1412 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001413}