blob: 2c6c5719ba41923c5a19acc046492787ab846e41 [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 Maier228e5912019-03-05 13:56:59 +010037#include <osmocom/netif/amr.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020038#include <osmocom/mgcp/mgcp.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020039#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020040#include <osmocom/mgcp/mgcp_internal.h>
41#include <osmocom/mgcp/mgcp_stat.h>
42#include <osmocom/mgcp/osmux.h>
43#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010044#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maier6931f9a2018-07-26 09:29:31 +020045#include <osmocom/mgcp/mgcp_codec.h>
Philipp Maierc3413882017-10-27 12:26:54 +020046#include <osmocom/mgcp/debug.h>
Philipp Maier9fc8a022019-02-20 12:26:52 +010047#include <osmocom/codec/codec.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
55enum {
56 MGCP_PROTO_RTP,
57 MGCP_PROTO_RTCP,
58};
59
Philipp Maier1cb1e382017-11-02 17:16:04 +010060/*! Determine the local rtp bind IP-address.
61 * \param[out] addr caller provided memory to store the resulting IP-Address
62 * \param[in] endp mgcp endpoint, that holds a copy of the VTY parameters
63 *
64 * The local bind IP-address is automatically selected by probing the
65 * IP-Address of the interface that is pointing towards the remote IP-Address,
66 * if no remote IP-Address is known yet, the statically configured
67 * IP-Addresses are used as fallback. */
68void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn)
69{
70
71 struct mgcp_endpoint *endp;
72 int rc;
73 endp = conn->conn->endp;
74
75 /* Try probing the local IP-Address */
76 if (endp->cfg->net_ports.bind_addr_probe && conn->end.addr.s_addr != 0) {
77 rc = osmo_sock_local_ip(addr, inet_ntoa(conn->end.addr));
78 if (rc < 0)
79 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +010080 "endpoint:0x%x CI:%s local interface auto detection failed, using configured addresses...\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +010081 ENDPOINT_NUMBER(endp), conn->conn->id);
82 else {
83 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +010084 "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 +010085 ENDPOINT_NUMBER(endp), conn->conn->id, addr,
86 inet_ntoa(conn->end.addr));
87 return;
88 }
89 }
90
91 /* Select from preconfigured IP-Addresses */
92 if (endp->cfg->net_ports.bind_addr) {
93 /* Check there is a bind IP for the RTP traffic configured,
94 * if so, use that IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +010095 osmo_strlcpy(addr, endp->cfg->net_ports.bind_addr, INET_ADDRSTRLEN);
Philipp Maier1cb1e382017-11-02 17:16:04 +010096 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +010097 "endpoint:0x%x CI:%s using configured rtp bind ip as local bind ip %s\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +010098 ENDPOINT_NUMBER(endp), conn->conn->id, addr);
99 } else {
100 /* No specific bind IP is configured for the RTP traffic, so
101 * assume the IP where we listen for incoming MGCP messages
102 * as bind IP */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100103 osmo_strlcpy(addr, endp->cfg->source_addr, INET_ADDRSTRLEN);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100104 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100105 "endpoint:0x%x CI:%s using mgcp bind ip as local rtp bind ip: %s\n",
Philipp Maier1cb1e382017-11-02 17:16:04 +0100106 ENDPOINT_NUMBER(endp), conn->conn->id, addr);
107 }
108}
109
Philipp Maier87bd9be2017-08-22 16:35:41 +0200110/* This does not need to be a precision timestamp and
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200111 * is allowed to wrap quite fast. The returned value is
Philipp Maier87bd9be2017-08-22 16:35:41 +0200112 * 1/codec_rate seconds. */
113static uint32_t get_current_ts(unsigned codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200114{
115 struct timespec tp;
116 uint64_t ret;
117
Philipp Maier87bd9be2017-08-22 16:35:41 +0200118 if (!codec_rate)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200119 return 0;
120
121 memset(&tp, 0, sizeof(tp));
122 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
Philipp Maierc3413882017-10-27 12:26:54 +0200123 LOGP(DRTP, LOGL_NOTICE, "Getting the clock failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200124
125 /* convert it to 1/unit seconds */
126 ret = tp.tv_sec;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200127 ret *= codec_rate;
128 ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200129
130 return ret;
131}
132
Philipp Maier87bd9be2017-08-22 16:35:41 +0200133/*! send udp packet.
134 * \param[in] fd associated file descriptor
135 * \param[in] addr destination ip-address
136 * \param[in] port destination UDP port
137 * \param[in] buf buffer that holds the data to be send
138 * \param[in] len length of the data to be sent
139 * \returns bytes sent, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200140int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
141{
142 struct sockaddr_in out;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200143
Philipp Maierc3413882017-10-27 12:26:54 +0200144 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200145 "sending %i bytes length packet to %s:%u ...\n",
146 len, inet_ntoa(*addr), ntohs(port));
147
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200148 out.sin_family = AF_INET;
149 out.sin_port = port;
150 memcpy(&out.sin_addr, addr, sizeof(*addr));
151
152 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
153}
154
Philipp Maier87bd9be2017-08-22 16:35:41 +0200155/*! send RTP dummy packet (to keep NAT connection open).
156 * \param[in] endp mcgp endpoint that holds the RTP connection
157 * \param[in] conn associated RTP connection
158 * \returns bytes sent, -1 on error */
159int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200160{
161 static char buf[] = { MGCP_DUMMY_LOAD };
162 int rc;
163 int was_rtcp = 0;
164
Philipp Maier87bd9be2017-08-22 16:35:41 +0200165 OSMO_ASSERT(endp);
166 OSMO_ASSERT(conn);
167
Philipp Maierc3413882017-10-27 12:26:54 +0200168 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100169 "endpoint:0x%x sending dummy packet...\n", ENDPOINT_NUMBER(endp));
170 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200171 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
172
173 rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
174 conn->end.rtp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200175
176 if (rc == -1)
177 goto failed;
178
179 if (endp->tcfg->omit_rtcp)
180 return rc;
181
182 was_rtcp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200183 rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
184 conn->end.rtcp_port, buf, 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200185
186 if (rc >= 0)
187 return rc;
188
189failed:
Philipp Maierc3413882017-10-27 12:26:54 +0200190 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100191 "endpoint:0x%x Failed to send dummy %s packet.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200192 ENDPOINT_NUMBER(endp), was_rtcp ? "RTCP" : "RTP");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200193
194 return -1;
195}
196
Philipp Maier87bd9be2017-08-22 16:35:41 +0200197/* Compute timestamp alignment error */
198static int32_t ts_alignment_error(struct mgcp_rtp_stream_state *sstate,
199 int ptime, uint32_t timestamp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200200{
201 int32_t timestamp_delta;
202
203 if (ptime == 0)
204 return 0;
205
206 /* Align according to: T - Tlast = k * Tptime */
207 timestamp_delta = timestamp - sstate->last_timestamp;
208
209 return timestamp_delta % ptime;
210}
211
Philipp Maier87bd9be2017-08-22 16:35:41 +0200212/* Check timestamp and sequence number for plausibility */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200213static int check_rtp_timestamp(struct mgcp_endpoint *endp,
214 struct mgcp_rtp_state *state,
215 struct mgcp_rtp_stream_state *sstate,
216 struct mgcp_rtp_end *rtp_end,
217 struct sockaddr_in *addr,
218 uint16_t seq, uint32_t timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200219 const char *text, int32_t * tsdelta_out)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200220{
221 int32_t tsdelta;
222 int32_t timestamp_error;
223
224 /* Not fully intialized, skip */
225 if (sstate->last_tsdelta == 0 && timestamp == sstate->last_timestamp)
226 return 0;
227
228 if (seq == sstate->last_seq) {
229 if (timestamp != sstate->last_timestamp) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200230 rate_ctr_inc(sstate->err_ts_ctr);
Philipp Maierc3413882017-10-27 12:26:54 +0200231 LOGP(DRTP, LOGL_ERROR,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200232 "The %s timestamp delta is != 0 but the sequence "
233 "number %d is the same, "
234 "TS offset: %d, SeqNo offset: %d "
235 "on 0x%x SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200236 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200237 text, seq,
Harald Welte33381352017-12-25 09:44:26 +0100238 state->patch.timestamp_offset, state->patch.seq_offset,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200239 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200240 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200241 }
242 return 0;
243 }
244
245 tsdelta =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200246 (int32_t)(timestamp - sstate->last_timestamp) /
247 (int16_t)(seq - sstate->last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200248
249 if (tsdelta == 0) {
250 /* Don't update *tsdelta_out */
Philipp Maierc3413882017-10-27 12:26:54 +0200251 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200252 "The %s timestamp delta is %d "
253 "on 0x%x SSRC: %u timestamp: %u "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200254 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200255 text, tsdelta,
256 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200257 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200258
259 return 0;
260 }
261
262 if (sstate->last_tsdelta != tsdelta) {
263 if (sstate->last_tsdelta) {
Philipp Maierc3413882017-10-27 12:26:54 +0200264 LOGP(DRTP, LOGL_INFO,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200265 "The %s timestamp delta changes from %d to %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200266 "on 0x%x SSRC: %u timestamp: %u from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200267 text, sstate->last_tsdelta, tsdelta,
268 ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200269 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200270 }
271 }
272
273 if (tsdelta_out)
274 *tsdelta_out = tsdelta;
275
276 timestamp_error =
Philipp Maier87bd9be2017-08-22 16:35:41 +0200277 ts_alignment_error(sstate, state->packet_duration, timestamp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200278
279 if (timestamp_error) {
Philipp Maier9e1d1642018-05-09 16:26:34 +0200280 rate_ctr_inc(sstate->err_ts_ctr);
Philipp Maierc3413882017-10-27 12:26:54 +0200281 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200282 "The %s timestamp has an alignment error of %d "
283 "on 0x%x SSRC: %u "
284 "SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200285 "from %s:%d. ptime: %d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200286 text, timestamp_error,
287 ENDPOINT_NUMBER(endp), sstate->ssrc,
288 (int16_t)(seq - sstate->last_seq),
289 (int32_t)(timestamp - sstate->last_timestamp),
290 tsdelta,
291 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200292 state->packet_duration);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200293 }
294 return 1;
295}
296
297/* Set the timestamp offset according to the packet duration. */
298static int adjust_rtp_timestamp_offset(struct mgcp_endpoint *endp,
299 struct mgcp_rtp_state *state,
300 struct mgcp_rtp_end *rtp_end,
301 struct sockaddr_in *addr,
302 int16_t delta_seq, uint32_t in_timestamp)
303{
304 int32_t tsdelta = state->packet_duration;
305 int timestamp_offset;
306 uint32_t out_timestamp;
307
308 if (tsdelta == 0) {
309 tsdelta = state->out_stream.last_tsdelta;
310 if (tsdelta != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200311 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200312 "A fixed packet duration is not available on 0x%x, "
313 "using last output timestamp delta instead: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200314 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200315 ENDPOINT_NUMBER(endp), tsdelta,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200316 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200317 } else {
Philipp Maierbc0346e2018-06-07 09:52:16 +0200318 tsdelta = rtp_end->codec->rate * 20 / 1000;
Philipp Maierc3413882017-10-27 12:26:54 +0200319 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200320 "Fixed packet duration and last timestamp delta "
321 "are not available on 0x%x, "
322 "using fixed 20ms instead: %d "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200323 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200324 ENDPOINT_NUMBER(endp), tsdelta,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200325 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200326 }
327 }
328
329 out_timestamp = state->out_stream.last_timestamp + delta_seq * tsdelta;
330 timestamp_offset = out_timestamp - in_timestamp;
331
Harald Welte33381352017-12-25 09:44:26 +0100332 if (state->patch.timestamp_offset != timestamp_offset) {
333 state->patch.timestamp_offset = timestamp_offset;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200334
Philipp Maierc3413882017-10-27 12:26:54 +0200335 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200336 "Timestamp offset change on 0x%x SSRC: %u "
337 "SeqNo delta: %d, TS offset: %d, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200338 "from %s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200339 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100340 delta_seq, state->patch.timestamp_offset,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200341 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200342 }
343
344 return timestamp_offset;
345}
346
347/* Set the timestamp offset according to the packet duration. */
348static int align_rtp_timestamp_offset(struct mgcp_endpoint *endp,
349 struct mgcp_rtp_state *state,
350 struct mgcp_rtp_end *rtp_end,
351 struct sockaddr_in *addr,
352 uint32_t timestamp)
353{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200354 int ts_error = 0;
355 int ts_check = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200356 int ptime = state->packet_duration;
357
358 /* Align according to: T + Toffs - Tlast = k * Tptime */
359
Philipp Maier87bd9be2017-08-22 16:35:41 +0200360 ts_error = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100361 timestamp + state->patch.timestamp_offset);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200362
Philipp Maier87bd9be2017-08-22 16:35:41 +0200363 /* If there is an alignment error, we have to compensate it */
364 if (ts_error) {
Harald Welte33381352017-12-25 09:44:26 +0100365 state->patch.timestamp_offset += ptime - ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200366
Philipp Maierc3413882017-10-27 12:26:54 +0200367 LOGP(DRTP, LOGL_NOTICE,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200368 "Corrected timestamp alignment error of %d on 0x%x SSRC: %u "
369 "new TS offset: %d, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200370 "from %s:%d\n",
371 ts_error,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200372 ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
Harald Welte33381352017-12-25 09:44:26 +0100373 state->patch.timestamp_offset, inet_ntoa(addr->sin_addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200374 ntohs(addr->sin_port));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200375 }
376
Philipp Maier87bd9be2017-08-22 16:35:41 +0200377 /* Check we really managed to compensate the timestamp
378 * offset. There should not be any remaining error, failing
Harald Welte1d1b98f2017-12-25 10:03:40 +0100379 * here would point to a serous problem with the alignment
380 * error computation function */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200381 ts_check = ts_alignment_error(&state->out_stream, ptime,
Harald Welte33381352017-12-25 09:44:26 +0100382 timestamp + state->patch.timestamp_offset);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200383 OSMO_ASSERT(ts_check == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200384
Philipp Maier87bd9be2017-08-22 16:35:41 +0200385 /* Return alignment error before compensation */
386 return ts_error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200387}
388
Philipp Maier87bd9be2017-08-22 16:35:41 +0200389/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
390 * \param[in] associated endpoint
391 * \param[in] destination RTP end
392 * \param[in,out] pointer to buffer with voice data
393 * \param[in] voice data length
Harald Welte1d1b98f2017-12-25 10:03:40 +0100394 * \param[in] maximum size of caller provided voice data buffer
Philipp Maier87bd9be2017-08-22 16:35:41 +0200395 * \returns ignores input parameters, return always 0 */
396int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
397 struct mgcp_rtp_end *dst_end,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200398 char *data, int *len, int buf_size)
399{
Philipp Maier230e4fc2017-11-28 09:38:45 +0100400 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x transcoding disabled\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200401 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200402 return 0;
403}
404
Philipp Maier87bd9be2017-08-22 16:35:41 +0200405/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
406 * \param[in] associated endpoint
Philipp Maieracc10352018-07-19 18:07:57 +0200407 * \param[in] destination RTP connnection
408 * \param[in] source RTP connection
Philipp Maier87bd9be2017-08-22 16:35:41 +0200409 * \returns ignores input parameters, return always 0 */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200410int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
Philipp Maieracc10352018-07-19 18:07:57 +0200411 struct mgcp_conn_rtp *conn_dst,
412 struct mgcp_conn_rtp *conn_src)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200413{
Philipp Maier230e4fc2017-11-28 09:38:45 +0100414 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x transcoding disabled\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200415 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200416 return 0;
417}
418
419void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
Philipp Maier58128252019-03-06 11:28:18 +0100420 const struct mgcp_rtp_codec **codec,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200421 const char **fmtp_extra,
422 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200423{
Philipp Maierc3413882017-10-27 12:26:54 +0200424 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100425 "endpoint:0x%x conn:%s using format defaults\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200426 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
427
Philipp Maier58128252019-03-06 11:28:18 +0100428 *codec = conn->end.codec;
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 Maier9fc8a022019-02-20 12:26:52 +0100654/* There are different dialects used to format and transfer voice data. When
655 * the receiving end expects GSM-HR data to be formated after RFC 5993, this
656 * function is used to convert between RFC 5993 and TS 101318, which we normally
657 * use. */
658static void rfc5993_hr_convert(struct mgcp_endpoint *endp, char *data, int *len)
659{
660 /* NOTE: *data has an overall length of RTP_BUF_SIZE, so there is
661 * plenty of space available to store the slightly larger, converted
662 * data */
663
664 struct rtp_hdr *rtp_hdr;
665
666 OSMO_ASSERT(*len >= sizeof(struct rtp_hdr));
667 rtp_hdr = (struct rtp_hdr *)data;
668
669 if (*len == GSM_HR_BYTES + sizeof(struct rtp_hdr)) {
670 /* TS 101318 encoding => RFC 5993 encoding */
671 memmove(rtp_hdr->data + 1, rtp_hdr->data, GSM_HR_BYTES);
672 rtp_hdr->data[0] = 0x00;
673 (*len) += 1;
674
675 } else if (*len == GSM_HR_BYTES + sizeof(struct rtp_hdr) + 1) {
676 /* RFC 5993 encoding => TS 101318 encoding */
677 memmove(rtp_hdr->data, rtp_hdr->data + 1, GSM_HR_BYTES);
678 (*len) -= 1;
679 } else {
680 /* It is possible that multiple payloads occur in one RTP
681 * packet. This is not supported yet. */
682 LOGP(DRTP, LOGL_ERROR,
683 "endpoint:0x%x cannot figure out how to convert RTP packet\n",
684 ENDPOINT_NUMBER(endp));
685 }
686}
687
Philipp Maier228e5912019-03-05 13:56:59 +0100688/* For AMR RTP two framing modes are defined RFC3267. There is a bandwith
689 * efficient encoding scheme where all fields are packed together one after
690 * another and an octet aligned mode where all fields are aligned to octet
691 * boundaries. This function is used to convert between the two modes */
692static int amr_oa_bwe_convert(struct mgcp_endpoint *endp, char *data, int *len,
693 bool target_is_oa)
694{
695 /* NOTE: *data has an overall length of RTP_BUF_SIZE, so there is
696 * plenty of space available to store the slightly larger, converted
697 * data */
698
699 struct rtp_hdr *rtp_hdr;
700 unsigned int payload_len;
701 int rc;
702
703 OSMO_ASSERT(*len >= sizeof(struct rtp_hdr));
704 rtp_hdr = (struct rtp_hdr *)data;
705
706 payload_len = *len - sizeof(struct rtp_hdr);
707
708 if (osmo_amr_is_oa(rtp_hdr->data, payload_len)) {
709 if (!target_is_oa)
710 /* Input data is oa an target format is bwe
711 * ==> convert */
712 rc = osmo_amr_oa_to_bwe(rtp_hdr->data, payload_len);
713 else
714 /* Input data is already bew, but we accept it anyway
715 * ==> no conversion needed */
716 rc = payload_len;
717 } else {
718 if (target_is_oa)
719 /* Input data is bwe an target format is oa
720 * ==> convert */
721 rc = osmo_amr_bwe_to_oa(rtp_hdr->data, payload_len,
722 RTP_BUF_SIZE);
723 else
724 /* Input data is already oa, but we accept it anyway
725 * ==> no conversion needed */
726 rc = payload_len;
727 }
728 if (rc < 0) {
729 LOGP(DRTP, LOGL_ERROR,
730 "endpoint:0x%x AMR RTP packet conversion failed\n",
731 ENDPOINT_NUMBER(endp));
732 return -EINVAL;
733 }
734
735 *len = rc + sizeof(struct rtp_hdr);
736
737 return 0;
738}
739
740/* Check if a conversion between octet-aligned and bandwith-efficient mode is
741 * indicated. */
742static bool amr_oa_bwe_convert_indicated(struct mgcp_rtp_codec *codec)
743{
744 if (codec->param_present == false)
745 return false;
746 if (!codec->param.amr_octet_aligned_present)
747 return false;
748 if (strcmp(codec->subtype_name, "AMR") != 0)
749 return false;
750 return true;
751}
752
753
754/* Check if a given RTP with AMR payload for octet-aligned mode */
755static bool amr_oa_check(char *data, int len)
756{
757 struct rtp_hdr *rtp_hdr;
758 unsigned int payload_len;
759
760 OSMO_ASSERT(len >= sizeof(struct rtp_hdr));
761 rtp_hdr = (struct rtp_hdr *)data;
762
763 payload_len = len - sizeof(struct rtp_hdr);
764
765 return osmo_amr_is_oa(rtp_hdr->data, payload_len);
766}
767
Philipp Maier87bd9be2017-08-22 16:35:41 +0200768/* Forward data to a debug tap. This is debug function that is intended for
769 * debugging the voice traffic with tools like gstreamer */
Philipp Maiere6f172d2017-11-07 12:00:01 +0100770static void forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf,
771 int len)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200772{
Philipp Maiere6f172d2017-11-07 12:00:01 +0100773 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200774
Philipp Maiere6f172d2017-11-07 12:00:01 +0100775 if (!tap->enabled)
776 return;
777
778 rc = sendto(fd, buf, len, 0, (struct sockaddr *)&tap->forward,
779 sizeof(tap->forward));
780
781 if (rc < 0)
782 LOGP(DRTP, LOGL_ERROR,
783 "Forwarding tapped (debug) voice data failed.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200784}
785
Philipp Maier87bd9be2017-08-22 16:35:41 +0200786/*! Send RTP/RTCP data to a specified destination connection.
787 * \param[in] endp associated endpoint (for configuration, logging)
788 * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP
789 * \param[in] spoofed source address (set to NULL to disable)
790 * \param[in] buf buffer that contains the RTP/RTCP data
791 * \param[in] len length of the buffer that contains the RTP/RTCP data
792 * \param[in] conn_src associated source connection
793 * \param[in] conn_dst associated destination connection
794 * \returns 0 on success, -1 on ERROR */
795int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
796 char *buf, int len, struct mgcp_conn_rtp *conn_src,
797 struct mgcp_conn_rtp *conn_dst)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200798{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200799 /*! When no destination connection is available (e.g. when only one
800 * connection in loopback mode exists), then the source connection
801 * shall be specified as destination connection */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200802
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200803 struct mgcp_trunk_config *tcfg = endp->tcfg;
804 struct mgcp_rtp_end *rtp_end;
805 struct mgcp_rtp_state *rtp_state;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200806 char *dest_name;
Philipp Maier6931f9a2018-07-26 09:29:31 +0200807 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200808
Philipp Maier87bd9be2017-08-22 16:35:41 +0200809 OSMO_ASSERT(conn_src);
810 OSMO_ASSERT(conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200811
Philipp Maier87bd9be2017-08-22 16:35:41 +0200812 if (is_rtp) {
Philipp Maierc3413882017-10-27 12:26:54 +0200813 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100814 "endpoint:0x%x delivering RTP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200815 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200816 } else {
Philipp Maierc3413882017-10-27 12:26:54 +0200817 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100818 "endpoint:0x%x delivering RTCP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200819 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200820 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200821
Philipp Maierc3413882017-10-27 12:26:54 +0200822 LOGP(DRTP, LOGL_DEBUG,
Neels Hofmeyr7066af82018-07-23 18:28:36 +0200823 "endpoint:0x%x loop:%d, mode:%d%s\n",
824 ENDPOINT_NUMBER(endp), tcfg->audio_loop, conn_src->conn->mode,
825 conn_src->conn->mode == MGCP_CONN_LOOPBACK ? " (loopback)" : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200826
Philipp Maier6931f9a2018-07-26 09:29:31 +0200827 /* FIXME: It is legal that the payload type on the egress connection is
828 * different from the payload type that has been negotiated on the
829 * ingress connection. Essentially the codecs are the same so we can
830 * match them and patch the payload type. However, if we can not find
831 * the codec pendant (everything ist equal except the PT), we are of
832 * course unable to patch the payload type. A situation like this
833 * should not occur if transcoding is consequently avoided. Until
834 * we have transcoding support in osmo-mgw we can not resolve this. */
Philipp Maierda895b12018-08-03 12:16:37 +0200835 if (is_rtp) {
836 rc = mgcp_patch_pt(conn_src, conn_dst, buf, len);
837 if (rc < 0) {
Neels Hofmeyre3f8bca2019-03-04 22:18:32 +0100838 LOGP(DRTP, LOGL_DEBUG,
Philipp Maierda895b12018-08-03 12:16:37 +0200839 "endpoint:0x%x can not patch PT because no suitable egress codec was found.\n",
840 ENDPOINT_NUMBER(endp));
841 }
Philipp Maier6931f9a2018-07-26 09:29:31 +0200842 }
843
Philipp Maier87bd9be2017-08-22 16:35:41 +0200844 /* Note: In case of loopback configuration, both, the source and the
845 * destination will point to the same connection. */
846 rtp_end = &conn_dst->end;
847 rtp_state = &conn_src->state;
848 dest_name = conn_dst->conn->name;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200849
850 if (!rtp_end->output_enabled) {
Philipp Maiercede2a42018-07-03 14:14:21 +0200851 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_DROPPED_PACKETS_CTR]);
Philipp Maierc3413882017-10-27 12:26:54 +0200852 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100853 "endpoint:0x%x output disabled, drop to %s %s "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200854 "rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200855 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200856 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200857 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200858 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200859 );
860 } else if (is_rtp) {
861 int cont;
862 int nbytes = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200863 int buflen = len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200864 do {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200865 /* Run transcoder */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200866 cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200867 buf, &buflen,
868 RTP_BUF_SIZE);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200869 if (cont < 0)
870 break;
871
Philipp Maier87bd9be2017-08-22 16:35:41 +0200872 if (addr)
873 mgcp_patch_and_count(endp, rtp_state, rtp_end,
874 addr, buf, buflen);
Philipp Maier9fc8a022019-02-20 12:26:52 +0100875
Philipp Maier228e5912019-03-05 13:56:59 +0100876 if (amr_oa_bwe_convert_indicated(conn_dst->end.codec)) {
877 amr_oa_bwe_convert(endp, buf, &buflen,
878 conn_dst->end.codec->param.amr_octet_aligned);
879 }
880 else if (rtp_end->rfc5993_hr_convert
Philipp Maier9fc8a022019-02-20 12:26:52 +0100881 && strcmp(conn_src->end.codec->subtype_name,
882 "GSM-HR-08") == 0)
883 rfc5993_hr_convert(endp, buf, &buflen);
884
Philipp Maierc3413882017-10-27 12:26:54 +0200885 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100886 "endpoint:0x%x process/send to %s %s "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200887 "rtp_port:%u rtcp_port:%u\n",
888 ENDPOINT_NUMBER(endp), dest_name,
889 inet_ntoa(rtp_end->addr), ntohs(rtp_end->rtp_port),
890 ntohs(rtp_end->rtcp_port)
891 );
892
893 /* Forward a copy of the RTP data to a debug ip/port */
894 forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
895 buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200896
897 /* FIXME: HACK HACK HACK. See OS#2459.
898 * The ip.access nano3G needs the first RTP payload's first two bytes to read hex
899 * 'e400', or it will reject the RAB assignment. It seems to not harm other femto
900 * cells (as long as we patch only the first RTP payload in each stream).
901 */
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200902 if (!rtp_state->patched_first_rtp_payload
903 && conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200904 uint8_t *data = (uint8_t *) & buf[12];
Neels Hofmeyr35a38292018-07-23 18:29:04 +0200905 if (data[0] == 0xe0) {
906 data[0] = 0xe4;
907 data[1] = 0x00;
908 rtp_state->patched_first_rtp_payload = true;
909 LOGP(DRTP, LOGL_DEBUG,
910 "endpoint:0x%x Patching over first two bytes"
911 " to fake an IuUP Initialization Ack\n",
912 ENDPOINT_NUMBER(endp));
913 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200914 }
915
Philipp Maier87bd9be2017-08-22 16:35:41 +0200916 len = mgcp_udp_send(rtp_end->rtp.fd,
917 &rtp_end->addr,
918 rtp_end->rtp_port, buf, buflen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200919
Philipp Maier87bd9be2017-08-22 16:35:41 +0200920 if (len <= 0)
921 return len;
922
Philipp Maiercede2a42018-07-03 14:14:21 +0200923 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
924 rate_ctr_add(&conn_dst->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], len);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200925
926 nbytes += len;
927 buflen = cont;
928 } while (buflen > 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200929 return nbytes;
930 } else if (!tcfg->omit_rtcp) {
Philipp Maierc3413882017-10-27 12:26:54 +0200931 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100932 "endpoint:0x%x send to %s %s rtp_port:%u rtcp_port:%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200933 ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200934 dest_name,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200935 inet_ntoa(rtp_end->addr),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200936 ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200937 );
938
Philipp Maier87bd9be2017-08-22 16:35:41 +0200939 len = mgcp_udp_send(rtp_end->rtcp.fd,
940 &rtp_end->addr,
941 rtp_end->rtcp_port, buf, len);
942
Philipp Maiercede2a42018-07-03 14:14:21 +0200943 rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
944 rate_ctr_add(&conn_dst->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], len);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200945
946 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200947 }
948
949 return 0;
950}
951
Philipp Maier87bd9be2017-08-22 16:35:41 +0200952/* Helper function for mgcp_recv(),
953 Receive one RTP Packet + Originating address from file descriptor */
954static int receive_from(struct mgcp_endpoint *endp, int fd,
955 struct sockaddr_in *addr, char *buf, int bufsize)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200956{
957 int rc;
958 socklen_t slen = sizeof(*addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200959 struct sockaddr_in addr_sink;
960 char buf_sink[RTP_BUF_SIZE];
961 bool tossed = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200962
Philipp Maier87bd9be2017-08-22 16:35:41 +0200963 if (!addr)
964 addr = &addr_sink;
965 if (!buf) {
966 tossed = true;
967 buf = buf_sink;
968 bufsize = sizeof(buf_sink);
969 }
970
971 rc = recvfrom(fd, buf, bufsize, 0, (struct sockaddr *)addr, &slen);
972
Philipp Maierc3413882017-10-27 12:26:54 +0200973 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200974 "receiving %u bytes length packet from %s:%u ...\n",
975 rc, inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
976
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200977 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +0200978 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100979 "endpoint:0x%x failed to receive packet, errno: %d/%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200980 ENDPOINT_NUMBER(endp), errno, strerror(errno));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200981 return -1;
982 }
983
Philipp Maier87bd9be2017-08-22 16:35:41 +0200984 if (tossed) {
Philipp Maier230e4fc2017-11-28 09:38:45 +0100985 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200986 ENDPOINT_NUMBER(endp));
987 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200988
989 return rc;
990}
991
Philipp Maier87bd9be2017-08-22 16:35:41 +0200992/* Check if the origin (addr) matches the address/port data of the RTP
993 * connections. */
994static int check_rtp_origin(struct mgcp_conn_rtp *conn,
995 struct sockaddr_in *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200996{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200997 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200998 endp = conn->conn->endp;
Neels Hofmeyr0063ca22018-07-23 18:12:16 +0200999
Harald Weltec26b6652018-10-21 12:01:04 +02001000 if (conn->end.addr.s_addr == 0) {
Neels Hofmeyrefd645e2018-09-10 13:14:50 +02001001 switch (conn->conn->mode) {
1002 case MGCP_CONN_LOOPBACK:
1003 /* HACK: for IuUP, we want to reply with an IuUP Initialization ACK upon the first RTP
1004 * message received. We currently hackishly accomplish that by putting the endpoint in
1005 * loopback mode and patching over the looped back RTP message to make it look like an
1006 * ack. We don't know the femto cell's IP address and port until the RAB Assignment
1007 * Response is received, but the nano3G expects an IuUP Initialization Ack before it even
1008 * sends the RAB Assignment Response. Hence, if the remote address is 0.0.0.0 and the
1009 * MGCP port is in loopback mode, allow looping back the packet to any source. */
1010 LOGP(DRTP, LOGL_ERROR,
1011 "endpoint:0x%x In loopback mode and remote address not set:"
1012 " allowing data from address: %s\n",
1013 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr));
1014 return 0;
1015
1016 default:
1017 /* Receiving early media before the endpoint is configured. Instead of logging
1018 * this as an error that occurs on every call, keep it more low profile to not
1019 * confuse humans with expected errors. */
1020 LOGP(DRTP, LOGL_INFO,
1021 "endpoint:0x%x I:%s Rx RTP from %s, but remote address not set:"
1022 " dropping early media\n",
1023 ENDPOINT_NUMBER(endp), conn->conn->id, inet_ntoa(addr->sin_addr));
1024 return -1;
1025 }
Neels Hofmeyr0063ca22018-07-23 18:12:16 +02001026 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001027
Philipp Maier87bd9be2017-08-22 16:35:41 +02001028 /* Note: Check if the inbound RTP data comes from the same host to
1029 * which we send our outgoing RTP traffic. */
Harald Weltec26b6652018-10-21 12:01:04 +02001030 if (conn->end.addr.s_addr != addr->sin_addr.s_addr) {
Philipp Maierc3413882017-10-27 12:26:54 +02001031 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001032 "endpoint:0x%x data from wrong address: %s, ",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001033 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr));
Philipp Maierc3413882017-10-27 12:26:54 +02001034 LOGPC(DRTP, LOGL_ERROR, "expected: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001035 inet_ntoa(conn->end.addr));
Philipp Maier230e4fc2017-11-28 09:38:45 +01001036 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001037 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001038 return -1;
1039 }
1040
Philipp Maier87bd9be2017-08-22 16:35:41 +02001041 /* Note: Usually the remote remote port of the data we receive will be
1042 * the same as the remote port where we transmit outgoing RTP traffic
1043 * to (set by MDCX). We use this to check the origin of the data for
1044 * plausibility. */
1045 if (conn->end.rtp_port != addr->sin_port &&
1046 conn->end.rtcp_port != addr->sin_port) {
Philipp Maierc3413882017-10-27 12:26:54 +02001047 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001048 "endpoint:0x%x data from wrong source port: %d, ",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001049 ENDPOINT_NUMBER(endp), ntohs(addr->sin_port));
Philipp Maierc3413882017-10-27 12:26:54 +02001050 LOGPC(DRTP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001051 "expected: %d for RTP or %d for RTCP\n",
1052 ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
Philipp Maier230e4fc2017-11-28 09:38:45 +01001053 LOGP(DRTP, LOGL_ERROR, "endpoint:0x%x packet tossed\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001054 ENDPOINT_NUMBER(endp));
1055 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001056 }
1057
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001058 return 0;
1059}
1060
Philipp Maier87bd9be2017-08-22 16:35:41 +02001061/* Check the if the destination address configuration of an RTP connection
1062 * makes sense */
1063static int check_rtp_destin(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001064{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001065 struct mgcp_endpoint *endp;
1066 endp = conn->conn->endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001067
Philipp Maiere6df0e42018-05-29 14:03:06 +02001068 /* Note: it is legal to create a connection but never setting a port
1069 * and IP-address for outgoing data. */
1070 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0 && conn->end.rtp_port == 0) {
1071 LOGP(DRTP, LOGL_DEBUG,
Neels Hofmeyrad21a0e2018-12-12 01:16:42 +01001072 "endpoint:0x%x destination IP-address and rtp port is (not yet) known (%s:%u)\n",
1073 ENDPOINT_NUMBER(endp), inet_ntoa(conn->end.addr), conn->end.rtp_port);
Philipp Maiere6df0e42018-05-29 14:03:06 +02001074 return -1;
1075 }
1076
Philipp Maier87bd9be2017-08-22 16:35:41 +02001077 if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001078 LOGP(DRTP, LOGL_ERROR,
Neels Hofmeyrad21a0e2018-12-12 01:16:42 +01001079 "endpoint:0x%x destination IP-address is invalid (%s:%u)\n",
1080 ENDPOINT_NUMBER(endp), inet_ntoa(conn->end.addr), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001081 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001082 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001083
1084 if (conn->end.rtp_port == 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001085 LOGP(DRTP, LOGL_ERROR,
Neels Hofmeyrad21a0e2018-12-12 01:16:42 +01001086 "endpoint:0x%x destination rtp port is invalid (%s:%u)\n",
1087 ENDPOINT_NUMBER(endp), inet_ntoa(conn->end.addr), conn->end.rtp_port);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001088 return -1;
1089 }
1090
1091 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001092}
1093
Philipp Maier4dba7692018-08-03 12:20:52 +02001094/* Do some basic checks to make sure that the RTCP packets we are going to
1095 * process are not complete garbage */
1096static int check_rtcp(char *buf, unsigned int buf_size)
1097{
1098 struct rtcp_hdr *hdr;
1099 unsigned int len;
1100 uint8_t type;
1101
1102 /* RTPC packets that are just a header without data do not make
1103 * any sense. */
1104 if (buf_size < sizeof(struct rtcp_hdr))
1105 return -EINVAL;
1106
1107 /* Make sure that the length of the received packet does not exceed
1108 * the available buffer size */
1109 hdr = (struct rtcp_hdr *)buf;
1110 len = (osmo_ntohs(hdr->length) + 1) * 4;
1111 if (len > buf_size)
1112 return -EINVAL;
1113
1114 /* Make sure we accept only packets that have a proper packet type set
1115 * See also: http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
1116 type = hdr->type;
1117 if ((type < 192 || type > 195) && (type < 200 || type > 213))
1118 return -EINVAL;
1119
1120 return 0;
1121}
1122
1123/* Do some basic checks to make sure that the RTP packets we are going to
1124 * process are not complete garbage */
1125static int check_rtp(char *buf, unsigned int buf_size)
1126{
1127 /* RTP packets that are just a header without data do not make
1128 * any sense. */
1129 if (buf_size < sizeof(struct rtp_hdr))
1130 return -EINVAL;
1131
1132 /* FIXME: Add more checks, the reason why we do not check more than
1133 * the length is because we currently handle IUUP packets as RTP
1134 * packets, so they must pass this check, if we weould be more
1135 * strict here, we would possibly break 3G. (see also FIXME note
1136 * below */
1137
1138 return 0;
1139}
1140
Philipp Maier87bd9be2017-08-22 16:35:41 +02001141/* Receive RTP data from a specified source connection and dispatch it to a
1142 * destination connection. */
1143static int mgcp_recv(int *proto, struct sockaddr_in *addr, char *buf,
1144 unsigned int buf_size, struct osmo_fd *fd)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001145{
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001146 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001147 struct mgcp_conn_rtp *conn;
1148 struct mgcp_trunk_config *tcfg;
1149 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001150
Philipp Maier87bd9be2017-08-22 16:35:41 +02001151 conn = (struct mgcp_conn_rtp*) fd->data;
1152 endp = conn->conn->endp;
1153 tcfg = endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001154
Philipp Maier230e4fc2017-11-28 09:38:45 +01001155 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x receiving RTP/RTCP packet...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001156 ENDPOINT_NUMBER(endp));
1157
1158 rc = receive_from(endp, fd->fd, addr, buf, buf_size);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001159 if (rc <= 0)
1160 return -1;
Philipp Maier4dba7692018-08-03 12:20:52 +02001161
1162 /* FIXME: The way how we detect the protocol looks odd. We should look
1163 * into the packet header. Also we should introduce a packet type
1164 * MGCP_PROTO_IUUP because currently we handle IUUP packets like RTP
1165 * packets which is problematic. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001166 *proto = fd == &conn->end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001167
Philipp Maier4dba7692018-08-03 12:20:52 +02001168 if (*proto == MGCP_PROTO_RTP) {
1169 if (check_rtp(buf, rc) < 0) {
1170 LOGP(DRTP, LOGL_ERROR,
1171 "endpoint:0x%x invalid RTP packet received -- packet tossed\n",
1172 ENDPOINT_NUMBER(endp));
1173 return -1;
1174 }
1175 } else if (*proto == MGCP_PROTO_RTCP) {
1176 if (check_rtcp(buf, rc) < 0) {
1177 LOGP(DRTP, LOGL_ERROR,
1178 "endpoint:0x%x invalid RTCP packet received -- packet tossed\n",
1179 ENDPOINT_NUMBER(endp));
1180 return -1;
1181 }
1182 }
1183
Philipp Maier230e4fc2017-11-28 09:38:45 +01001184 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x ", ENDPOINT_NUMBER(endp));
Neels Hofmeyr56725632018-01-15 15:15:07 +01001185 LOGPC(DRTP, LOGL_DEBUG, "receiving from %s %s %d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001186 conn->conn->name, inet_ntoa(addr->sin_addr),
1187 ntohs(addr->sin_port));
Philipp Maier230e4fc2017-11-28 09:38:45 +01001188 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x conn:%s\n", ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001189 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001190
Philipp Maier87bd9be2017-08-22 16:35:41 +02001191 /* Check if the origin of the RTP packet seems plausible */
1192 if (tcfg->rtp_accept_all == 0) {
1193 if (check_rtp_origin(conn, addr) != 0)
1194 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001195 }
1196
Philipp Maier87bd9be2017-08-22 16:35:41 +02001197 /* Filter out dummy message */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001198 if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
Philipp Maierc3413882017-10-27 12:26:54 +02001199 LOGP(DRTP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001200 "endpoint:0x%x dummy message received\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001201 ENDPOINT_NUMBER(endp));
Philipp Maierc3413882017-10-27 12:26:54 +02001202 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001203 "endpoint:0x%x packet tossed\n", ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001204 return 0;
1205 }
1206
Philipp Maier87bd9be2017-08-22 16:35:41 +02001207 /* Increment RX statistics */
Philipp Maiercede2a42018-07-03 14:14:21 +02001208 rate_ctr_inc(&conn->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR]);
1209 rate_ctr_add(&conn->rate_ctr_group->ctr[RTP_OCTETS_RX_CTR], rc);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001210
Philipp Maier87bd9be2017-08-22 16:35:41 +02001211 /* Forward a copy of the RTP data to a debug ip/port */
1212 forward_data(fd->fd, &conn->tap_in, buf, rc);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001213
Philipp Maier87bd9be2017-08-22 16:35:41 +02001214 return rc;
1215}
1216
1217/* Send RTP data. Possible options are standard RTP packet
1218 * transmission or trsmission via an osmux connection */
1219static int mgcp_send_rtp(int proto, struct sockaddr_in *addr, char *buf,
1220 unsigned int buf_size,
1221 struct mgcp_conn_rtp *conn_src,
1222 struct mgcp_conn_rtp *conn_dst)
1223{
1224 struct mgcp_endpoint *endp;
1225 endp = conn_src->conn->endp;
1226
Philipp Maier230e4fc2017-11-28 09:38:45 +01001227 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x destin conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001228 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_dst->conn));
1229
1230 /* Before we try to deliver the packet, we check if the destination
1231 * port and IP-Address make sense at all. If not, we will be unable
1232 * to deliver the packet. */
1233 if (check_rtp_destin(conn_dst) != 0)
1234 return -1;
1235
1236 /* Depending on the RTP connection type, deliver the RTP packet to the
1237 * destination connection. */
1238 switch (conn_dst->type) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001239 case MGCP_RTP_DEFAULT:
Philipp Maierc3413882017-10-27 12:26:54 +02001240 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001241 "endpoint:0x%x endpoint type is MGCP_RTP_DEFAULT, "
Philipp Maier87bd9be2017-08-22 16:35:41 +02001242 "using mgcp_send() to forward data directly\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001243 ENDPOINT_NUMBER(endp));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001244 return mgcp_send(endp, proto == MGCP_PROTO_RTP,
1245 addr, buf, buf_size, conn_src, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001246 case MGCP_OSMUX_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001247 case MGCP_OSMUX_BSC:
Philipp Maierc3413882017-10-27 12:26:54 +02001248 LOGP(DRTP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001249 "endpoint:0x%x endpoint type is MGCP_OSMUX_BSC_NAT, "
Philipp Maier87bd9be2017-08-22 16:35:41 +02001250 "using osmux_xfrm_to_osmux() to forward data through OSMUX\n",
1251 ENDPOINT_NUMBER(endp));
1252 return osmux_xfrm_to_osmux(buf, buf_size, conn_dst);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001253 }
1254
Philipp Maier87bd9be2017-08-22 16:35:41 +02001255 /* If the data has not been handled/forwarded until here, it will
1256 * be discarded, this should not happen, normally the MGCP type
1257 * should be properly set */
Philipp Maierc3413882017-10-27 12:26:54 +02001258 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001259 "endpoint:0x%x bad MGCP type -- data discarded!\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001260 ENDPOINT_NUMBER(endp));
1261
1262 return -1;
1263}
1264
1265/*! dispatch incoming RTP packet to opposite RTP connection.
1266 * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP)
1267 * \param[in] addr socket address where the RTP packet has been received from
1268 * \param[in] buf buffer that hold the RTP payload
1269 * \param[in] buf_size size data length of buf
1270 * \param[in] conn originating connection
1271 * \returns 0 on success, -1 on ERROR */
1272int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf,
1273 unsigned int buf_size, struct mgcp_conn *conn)
1274{
1275 struct mgcp_conn *conn_dst;
1276 struct mgcp_endpoint *endp;
1277 endp = conn->endp;
1278
1279 /*! NOTE: This callback function implements the endpoint specific
1280 * dispatch bahviour of an rtp bridge/proxy endpoint. It is assumed
1281 * that the endpoint will hold only two connections. This premise
1282 * is used to determine the opposite connection (it is always the
1283 * connection that is not the originating connection). Once the
1284 * destination connection is known the RTP packet is sent via
1285 * the destination connection. */
1286
1287 /* Find a destination connection. */
1288 /* NOTE: This code path runs every time an RTP packet is received. The
1289 * function mgcp_find_dst_conn() we use to determine the detination
1290 * connection will iterate the connection list inside the endpoint.
1291 * Since list iterations are quite costly, we will figure out the
1292 * destination only once and use the optional private data pointer of
1293 * the connection to cache the destination connection pointer. */
1294 if (!conn->priv) {
1295 conn_dst = mgcp_find_dst_conn(conn);
1296 conn->priv = conn_dst;
1297 } else {
1298 conn_dst = (struct mgcp_conn *)conn->priv;
1299 }
1300
1301 /* There is no destination conn, stop here */
1302 if (!conn_dst) {
Philipp Maierc3413882017-10-27 12:26:54 +02001303 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001304 "endpoint:0x%x unable to find destination conn\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001305 ENDPOINT_NUMBER(endp));
1306 return -1;
1307 }
1308
1309 /* The destination conn is not an RTP connection */
1310 if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
Philipp Maierc3413882017-10-27 12:26:54 +02001311 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001312 "endpoint:0x%x unable to find suitable destination conn\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001313 ENDPOINT_NUMBER(endp));
1314 return -1;
1315 }
1316
1317 /* Dispatch RTP packet to destination RTP connection */
1318 return mgcp_send_rtp(proto, addr, buf,
1319 buf_size, &conn->u.rtp, &conn_dst->u.rtp);
1320
1321}
1322
Philipp Maierdf5d2192018-01-24 11:39:32 +01001323/*! cleanup an endpoint when a connection on an RTP bridge endpoint is removed.
1324 * \param[in] endp Endpoint on which the connection resides.
1325 * \param[in] conn Connection that is about to be removed (ignored).
1326 * \returns 0 on success, -1 on ERROR. */
1327void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
1328{
1329 struct mgcp_conn *conn_cleanup;
1330
1331 /* In mgcp_dispatch_rtp_bridge_cb() we use conn->priv to cache the
1332 * pointer to the destination connection, so that we do not have
1333 * to go through the list every time an RTP packet arrives. To prevent
1334 * a use-after-free situation we invalidate this information for all
1335 * connections present when one connection is removed from the
1336 * endpoint. */
1337 llist_for_each_entry(conn_cleanup, &endp->conns, entry) {
1338 conn_cleanup->priv = NULL;
1339 }
1340}
1341
Philipp Maier87bd9be2017-08-22 16:35:41 +02001342/* Handle incoming RTP data from NET */
1343static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
1344{
1345 /* NOTE: This is a generic implementation. RTP data is received. In
1346 * case of loopback the data is just sent back to its origin. All
1347 * other cases implement endpoint specific behaviour (e.g. how is the
1348 * destination connection determined?). That specific behaviour is
1349 * implemented by the callback function that is called at the end of
1350 * the function */
1351
1352 struct mgcp_conn_rtp *conn_src;
1353 struct mgcp_endpoint *endp;
1354 struct sockaddr_in addr;
1355
1356 char buf[RTP_BUF_SIZE];
1357 int proto;
Philipp Maierb9694552017-11-08 16:53:57 +01001358 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001359
1360 conn_src = (struct mgcp_conn_rtp *)fd->data;
1361 OSMO_ASSERT(conn_src);
1362 endp = conn_src->conn->endp;
1363 OSMO_ASSERT(endp);
1364
Philipp Maier230e4fc2017-11-28 09:38:45 +01001365 LOGP(DRTP, LOGL_DEBUG, "endpoint:0x%x source conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001366 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_src->conn));
1367
1368 /* Receive packet */
Philipp Maierb9694552017-11-08 16:53:57 +01001369 len = mgcp_recv(&proto, &addr, buf, sizeof(buf), fd);
1370 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001371 return -1;
1372
Oliver Smithe36b7752019-01-22 16:31:36 +01001373 mgcp_conn_watchdog_kick(conn_src->conn);
1374
Philipp Maier87bd9be2017-08-22 16:35:41 +02001375 /* Check if the connection is in loopback mode, if yes, just send the
1376 * incoming data back to the origin */
1377 if (conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
Philipp Maier5dbfc782017-12-12 16:20:25 +01001378 /* When we are in loopback mode, we loop back all incoming
1379 * packets back to their origin. We will use the originating
1380 * address data from the UDP packet header to patch the
1381 * outgoing address in connection on the fly */
1382 if (conn_src->end.rtp_port == 0) {
1383 conn_src->end.addr = addr.sin_addr;
1384 conn_src->end.rtp_port = addr.sin_port;
1385 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001386 return mgcp_send_rtp(proto, &addr, buf,
Philipp Maierb9694552017-11-08 16:53:57 +01001387 len, conn_src, conn_src);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001388 }
1389
Philipp Maier228e5912019-03-05 13:56:59 +01001390 /* If AMR is configured for the ingress connection a conversion of the
1391 * framing mode (octet-aligned vs. bandwith-efficient is explicitly
1392 * define, then we check if the incoming payload matches that
1393 * expectation. */
1394 if (amr_oa_bwe_convert_indicated(conn_src->end.codec)) {
1395 if (amr_oa_check(buf, len) != conn_src->end.codec->param.amr_octet_aligned)
1396 return -1;
1397 }
1398
Philipp Maier87bd9be2017-08-22 16:35:41 +02001399 /* Execute endpoint specific implementation that handles the
1400 * dispatching of the RTP data */
Philipp Maierb9694552017-11-08 16:53:57 +01001401 return endp->type->dispatch_rtp_cb(proto, &addr, buf, len,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001402 conn_src->conn);
1403}
1404
1405/*! set IP Type of Service parameter.
1406 * \param[in] fd associated file descriptor
1407 * \param[in] tos dscp value
1408 * \returns 0 on success, -1 on ERROR */
1409int mgcp_set_ip_tos(int fd, int tos)
1410{
1411 int ret;
1412 ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1413
1414 if (ret < 0)
1415 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001416 return 0;
1417}
1418
Philipp Maier87bd9be2017-08-22 16:35:41 +02001419/*! bind RTP port to osmo_fd.
1420 * \param[in] source_addr source (local) address to bind on
1421 * \param[in] fd associated file descriptor
1422 * \param[in] port to bind on
1423 * \returns 0 on success, -1 on ERROR */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001424int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
1425{
Harald Welte8890dfa2017-11-17 15:09:30 +01001426 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001427
Harald Welte8890dfa2017-11-17 15:09:30 +01001428 rc = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
1429 NULL, 0, OSMO_SOCK_F_BIND);
1430 if (rc < 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001431 LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001432 source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001433 return -1;
1434 }
Harald Welte8890dfa2017-11-17 15:09:30 +01001435 fd->fd = rc;
1436 LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001437
1438 return 0;
1439}
1440
Philipp Maier87bd9be2017-08-22 16:35:41 +02001441/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001442static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001443 struct mgcp_rtp_end *rtp_end, int endpno)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001444{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001445 /* NOTE: The port that is used for RTCP is the RTP port incremented by one
1446 * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
1447
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001448 if (mgcp_create_bind(source_addr, &rtp_end->rtp,
1449 rtp_end->local_port) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001450 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001451 "endpoint:0x%x failed to create RTP port: %s:%d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001452 source_addr, rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001453 goto cleanup0;
1454 }
1455
1456 if (mgcp_create_bind(source_addr, &rtp_end->rtcp,
1457 rtp_end->local_port + 1) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001458 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001459 "endpoint:0x%x failed to create RTCP port: %s:%d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001460 source_addr, rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001461 goto cleanup1;
1462 }
1463
Philipp Maier87bd9be2017-08-22 16:35:41 +02001464 /* Set Type of Service (DSCP-Value) as configured via VTY */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001465 mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
1466 mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
1467
1468 rtp_end->rtp.when = BSC_FD_READ;
1469 if (osmo_fd_register(&rtp_end->rtp) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001470 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001471 "endpoint:0x%x failed to register RTP port %d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001472 rtp_end->local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001473 goto cleanup2;
1474 }
1475
1476 rtp_end->rtcp.when = BSC_FD_READ;
1477 if (osmo_fd_register(&rtp_end->rtcp) != 0) {
Philipp Maierc3413882017-10-27 12:26:54 +02001478 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001479 "endpoint:0x%x failed to register RTCP port %d\n", endpno,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001480 rtp_end->local_port + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001481 goto cleanup3;
1482 }
1483
1484 return 0;
1485
1486cleanup3:
1487 osmo_fd_unregister(&rtp_end->rtp);
1488cleanup2:
1489 close(rtp_end->rtcp.fd);
1490 rtp_end->rtcp.fd = -1;
1491cleanup1:
1492 close(rtp_end->rtp.fd);
1493 rtp_end->rtp.fd = -1;
1494cleanup0:
1495 return -1;
1496}
1497
Philipp Maier87bd9be2017-08-22 16:35:41 +02001498/*! bind RTP port to endpoint/connection.
1499 * \param[in] endp endpoint that holds the RTP connection
1500 * \param[in] rtp_port port number to bind on
1501 * \param[in] conn associated RTP connection
1502 * \returns 0 on success, -1 on ERROR */
1503int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
1504 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001505{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001506 char name[512];
1507 struct mgcp_rtp_end *end;
Philipp Maier1cb1e382017-11-02 17:16:04 +01001508 char local_ip_addr[INET_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001509
Philipp Maier01d24a32017-11-21 17:26:09 +01001510 snprintf(name, sizeof(name), "%s-%s", conn->conn->name, conn->conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001511 end = &conn->end;
1512
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001513 if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
Philipp Maierc3413882017-10-27 12:26:54 +02001514 LOGP(DRTP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001515 "endpoint:0x%x %u was already bound on conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001516 ENDPOINT_NUMBER(endp), rtp_port,
1517 mgcp_conn_dump(conn->conn));
1518
1519 /* Double bindings should never occour! Since we always allocate
1520 * connections dynamically and free them when they are not
1521 * needed anymore, there must be no previous binding leftover.
1522 * Should there be a connection bound twice, we have a serious
1523 * problem and must exit immediately! */
1524 OSMO_ASSERT(false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001525 }
1526
1527 end->local_port = rtp_port;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001528 end->rtp.cb = rtp_data_net;
1529 end->rtp.data = conn;
1530 end->rtcp.data = conn;
1531 end->rtcp.cb = rtp_data_net;
1532
Philipp Maier1cb1e382017-11-02 17:16:04 +01001533 mgcp_get_local_addr(local_ip_addr, conn);
1534
1535 return bind_rtp(endp->cfg, local_ip_addr, end,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001536 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001537}
1538
Philipp Maier87bd9be2017-08-22 16:35:41 +02001539/*! free allocated RTP and RTCP ports.
1540 * \param[in] end RTP end */
1541void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001542{
1543 if (end->rtp.fd != -1) {
1544 close(end->rtp.fd);
1545 end->rtp.fd = -1;
1546 osmo_fd_unregister(&end->rtp);
1547 }
1548
1549 if (end->rtcp.fd != -1) {
1550 close(end->rtcp.fd);
1551 end->rtcp.fd = -1;
1552 osmo_fd_unregister(&end->rtcp);
1553 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001554}