blob: 737a24dc0f6eb4e1337d3e25bd4980b5d290d0e0 [file] [log] [blame]
Harald Welteead7a7b2009-07-28 00:01:58 +02001/* RTP proxy handling for ip.access nanoBTS */
2
Harald Welte647db842013-02-03 12:06:58 +01003/* (C) 2009-2013 by Harald Welte <laforge@gnumonks.org>
Harald Welteead7a7b2009-07-28 00:01:58 +02004 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01007 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
Harald Welteead7a7b2009-07-28 00:01:58 +02009 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010014 * GNU Affero General Public License for more details.
Harald Welteead7a7b2009-07-28 00:01:58 +020015 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welteead7a7b2009-07-28 00:01:58 +020018 *
19 */
20
21#include <errno.h>
22#include <unistd.h>
23#include <sys/socket.h>
24#include <netinet/in.h>
Harald Welte805f6442009-07-28 18:25:29 +020025#include <arpa/inet.h>
Harald Welteda7ab742009-12-19 22:23:05 +010026#include <sys/time.h> /* gettimeofday() */
27#include <unistd.h> /* get..() */
28#include <time.h> /* clock() */
29#include <sys/utsname.h> /* uname() */
Harald Welteead7a7b2009-07-28 00:01:58 +020030
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010031#include <osmocom/core/talloc.h>
Harald Welteead7a7b2009-07-28 00:01:58 +020032#include <openbsc/gsm_data.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010033#include <osmocom/core/msgb.h>
34#include <osmocom/core/select.h>
Harald Welte805f6442009-07-28 18:25:29 +020035#include <openbsc/debug.h>
Harald Welteead7a7b2009-07-28 00:01:58 +020036#include <openbsc/rtp_proxy.h>
Harald Weltef142c972011-05-24 13:25:38 +020037#include <openbsc/mncc.h>
Alexander Huemer0a654612011-09-06 00:09:49 +020038#include <openbsc/trau_upqueue.h>
Holger Hans Peter Freyther983c9912014-06-22 22:30:28 +020039#include <openbsc/rtp.h>
Harald Welteead7a7b2009-07-28 00:01:58 +020040
Holger Hans Peter Freyther3cb28792010-10-12 15:39:46 +020041/* attempt to determine byte order */
Holger Hans Peter Freyther3cb28792010-10-12 15:39:46 +020042#include <sys/param.h>
43#include <limits.h>
44
45#ifndef __BYTE_ORDER
Tobias Engelaff20712012-10-24 17:53:50 +020046# ifdef __APPLE__
47# define __BYTE_ORDER __DARWIN_BYTE_ORDER
48# define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
49# define __BIG_ENDIAN __DARWIN_BIG_ENDIAN
50# else
51# error "__BYTE_ORDER should be defined by someone"
52# endif
Holger Hans Peter Freyther3cb28792010-10-12 15:39:46 +020053#endif
54
Harald Welteead7a7b2009-07-28 00:01:58 +020055static LLIST_HEAD(rtp_sockets);
56
Harald Welte805f6442009-07-28 18:25:29 +020057/* should we mangle the CNAME inside SDES of RTCP packets? We disable
58 * this by default, as it seems to be not needed */
59static int mangle_rtcp_cname = 0;
60
Harald Welteead7a7b2009-07-28 00:01:58 +020061enum rtp_bfd_priv {
62 RTP_PRIV_NONE,
63 RTP_PRIV_RTP,
64 RTP_PRIV_RTCP
65};
66
67#define RTP_ALLOC_SIZE 1500
68
Harald Welte805f6442009-07-28 18:25:29 +020069/* according to RFC 1889 */
70struct rtcp_hdr {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020071 uint8_t byte0;
72 uint8_t type;
73 uint16_t length;
Harald Welte805f6442009-07-28 18:25:29 +020074} __attribute__((packed));
75
76#define RTCP_TYPE_SDES 202
77
78#define RTCP_IE_CNAME 1
79
Harald Welteda7ab742009-12-19 22:23:05 +010080struct rtp_x_hdr {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020081 uint16_t by_profile;
82 uint16_t length;
Harald Welteda7ab742009-12-19 22:23:05 +010083} __attribute__((packed));
84
85#define RTP_VERSION 2
86
Harald Weltea87f8f92014-05-17 13:43:01 +020087/* 33 for FR, all other codecs have smaller size */
88#define MAX_RTP_PAYLOAD_LEN 33
89
Harald Welteda7ab742009-12-19 22:23:05 +010090/* decode an rtp frame and create a new buffer with payload */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020091static int rtp_decode(struct msgb *msg, uint32_t callref, struct msgb **data)
Harald Welteda7ab742009-12-19 22:23:05 +010092{
93 struct msgb *new_msg;
94 struct gsm_data_frame *frame;
95 struct rtp_hdr *rtph = (struct rtp_hdr *)msg->data;
96 struct rtp_x_hdr *rtpxh;
Harald Weltea87f8f92014-05-17 13:43:01 +020097 uint8_t *payload, *payload_out;
Harald Welteda7ab742009-12-19 22:23:05 +010098 int payload_len;
99 int msg_type;
100 int x_len;
101
102 if (msg->len < 12) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200103 DEBUGPC(DLMUX, "received RTP frame too short (len = %d)\n",
Harald Welteda7ab742009-12-19 22:23:05 +0100104 msg->len);
105 return -EINVAL;
106 }
107 if (rtph->version != RTP_VERSION) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200108 DEBUGPC(DLMUX, "received RTP version %d not supported.\n",
Harald Welteda7ab742009-12-19 22:23:05 +0100109 rtph->version);
110 return -EINVAL;
111 }
112 payload = msg->data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
113 payload_len = msg->len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
114 if (payload_len < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200115 DEBUGPC(DLMUX, "received RTP frame too short (len = %d, "
Harald Welteda7ab742009-12-19 22:23:05 +0100116 "csrc count = %d)\n", msg->len, rtph->csrc_count);
117 return -EINVAL;
118 }
119 if (rtph->extension) {
120 if (payload_len < sizeof(struct rtp_x_hdr)) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200121 DEBUGPC(DLMUX, "received RTP frame too short for "
Harald Welteda7ab742009-12-19 22:23:05 +0100122 "extension header\n");
123 return -EINVAL;
124 }
125 rtpxh = (struct rtp_x_hdr *)payload;
126 x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
127 payload += x_len;
128 payload_len -= x_len;
129 if (payload_len < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200130 DEBUGPC(DLMUX, "received RTP frame too short, "
Harald Welteda7ab742009-12-19 22:23:05 +0100131 "extension header exceeds frame length\n");
132 return -EINVAL;
133 }
134 }
135 if (rtph->padding) {
Jacob Erlbeckd9e40392013-11-21 19:05:42 +0100136 if (payload_len < 1) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200137 DEBUGPC(DLMUX, "received RTP frame too short for "
Harald Welteda7ab742009-12-19 22:23:05 +0100138 "padding length\n");
139 return -EINVAL;
140 }
141 payload_len -= payload[payload_len - 1];
142 if (payload_len < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200143 DEBUGPC(DLMUX, "received RTP frame with padding "
Harald Welteda7ab742009-12-19 22:23:05 +0100144 "greater than payload\n");
145 return -EINVAL;
146 }
147 }
148
149 switch (rtph->payload_type) {
150 case RTP_PT_GSM_FULL:
151 msg_type = GSM_TCHF_FRAME;
Andreas Eversberg88012b62014-01-22 10:05:51 +0100152 if (payload_len != RTP_LEN_GSM_FULL) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200153 DEBUGPC(DLMUX, "received RTP full rate frame with "
Andreas Eversberg88012b62014-01-22 10:05:51 +0100154 "payload length != %d (len = %d)\n",
155 RTP_LEN_GSM_FULL, payload_len);
Harald Welteda7ab742009-12-19 22:23:05 +0100156 return -EINVAL;
157 }
158 break;
Harald Welteaca8f152009-12-19 23:06:41 +0100159 case RTP_PT_GSM_EFR:
160 msg_type = GSM_TCHF_FRAME_EFR;
Andreas Eversberg88012b62014-01-22 10:05:51 +0100161 if (payload_len != RTP_LEN_GSM_EFR) {
162 DEBUGPC(DLMUX, "received RTP extended full rate frame "
163 "with payload length != %d (len = %d)\n",
164 RTP_LEN_GSM_EFR, payload_len);
165 return -EINVAL;
166 }
Harald Welteaca8f152009-12-19 23:06:41 +0100167 break;
Andreas Eversberg63bfdd82014-01-17 19:06:38 +0100168 case RTP_PT_GSM_HALF:
169 msg_type = GSM_TCHH_FRAME;
170 if (payload_len != RTP_LEN_GSM_HALF) {
171 DEBUGPC(DLMUX, "received RTP half rate frame with "
172 "payload length != %d (len = %d)\n",
173 RTP_LEN_GSM_HALF, payload_len);
174 return -EINVAL;
175 }
176 break;
Andreas Eversbergd8967f72012-03-08 14:39:19 +0100177 case RTP_PT_AMR:
Harald Welteec757982014-05-18 18:32:05 +0200178 msg_type = GSM_TCH_FRAME_AMR;
Andreas Eversbergd8967f72012-03-08 14:39:19 +0100179 break;
Harald Welteda7ab742009-12-19 22:23:05 +0100180 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200181 DEBUGPC(DLMUX, "received RTP frame with unknown payload "
Harald Welteda7ab742009-12-19 22:23:05 +0100182 "type %d\n", rtph->payload_type);
183 return -EINVAL;
184 }
185
Harald Weltea87f8f92014-05-17 13:43:01 +0200186 if (payload_len > MAX_RTP_PAYLOAD_LEN) {
Andreas Eversbergd8967f72012-03-08 14:39:19 +0100187 DEBUGPC(DLMUX, "RTP payload too large (%d octets)\n",
188 payload_len);
189 return -EINVAL;
190 }
191
Harald Weltea87f8f92014-05-17 13:43:01 +0200192 /* always allocate for the maximum possible size to avoid
193 * fragmentation */
194 new_msg = msgb_alloc(sizeof(struct gsm_data_frame) +
195 MAX_RTP_PAYLOAD_LEN, "GSM-DATA (TCH)");
196
Harald Welteda7ab742009-12-19 22:23:05 +0100197 if (!new_msg)
198 return -ENOMEM;
Holger Hans Peter Freyther652cdb42014-07-24 21:09:31 +0200199 frame = (struct gsm_data_frame *) msgb_put(new_msg, sizeof(struct gsm_data_frame));
Harald Welteda7ab742009-12-19 22:23:05 +0100200 frame->msg_type = msg_type;
201 frame->callref = callref;
Andreas Eversbergd8967f72012-03-08 14:39:19 +0100202 if (rtph->payload_type == RTP_PT_AMR) {
Harald Weltea87f8f92014-05-17 13:43:01 +0200203 /* for FR/HR/EFR the length is implicit. In AMR, we
204 * need to make it explicit by using the first byte of
205 * the data[] buffer as length byte */
206 uint8_t *data0 = msgb_put(new_msg, 1);
207 *data0 = payload_len;
Andreas Eversbergd8967f72012-03-08 14:39:19 +0100208 }
Harald Weltea87f8f92014-05-17 13:43:01 +0200209 payload_out = msgb_put(new_msg, payload_len);
210 memcpy(payload_out, payload, payload_len);
Harald Welteda7ab742009-12-19 22:23:05 +0100211
212 *data = new_msg;
213 return 0;
214}
215
Harald Welte392736d2009-12-20 13:16:14 +0100216/* "to - from" */
217static void tv_difference(struct timeval *diff, const struct timeval *from,
218 const struct timeval *__to)
219{
220 struct timeval _to = *__to, *to = &_to;
221
222 if (to->tv_usec < from->tv_usec) {
223 to->tv_sec -= 1;
224 to->tv_usec += 1000000;
225 }
226
227 diff->tv_usec = to->tv_usec - from->tv_usec;
228 diff->tv_sec = to->tv_sec - from->tv_sec;
229}
230
Harald Welte647db842013-02-03 12:06:58 +0100231/*! \brief encode and send a rtp frame
232 * \param[in] rs RTP socket through which we shall send
233 * \param[in] frame GSM RTP frame to be sent
234 */
Harald Welteda7ab742009-12-19 22:23:05 +0100235int rtp_send_frame(struct rtp_socket *rs, struct gsm_data_frame *frame)
236{
237 struct rtp_sub_socket *rss = &rs->rtp;
238 struct msgb *msg;
239 struct rtp_hdr *rtph;
Harald Weltea87f8f92014-05-17 13:43:01 +0200240 uint8_t *payload;
Harald Welteda7ab742009-12-19 22:23:05 +0100241 int payload_type;
242 int payload_len;
243 int duration; /* in samples */
244
245 if (rs->tx_action != RTP_SEND_DOWNSTREAM) {
246 /* initialize sequences */
247 rs->tx_action = RTP_SEND_DOWNSTREAM;
248 rs->transmit.ssrc = rand();
249 rs->transmit.sequence = random();
250 rs->transmit.timestamp = random();
251 }
252
253 switch (frame->msg_type) {
254 case GSM_TCHF_FRAME:
255 payload_type = RTP_PT_GSM_FULL;
Andreas Eversberg88012b62014-01-22 10:05:51 +0100256 payload_len = RTP_LEN_GSM_FULL;
257 duration = RTP_GSM_DURATION;
Harald Welteda7ab742009-12-19 22:23:05 +0100258 break;
Harald Welteaca8f152009-12-19 23:06:41 +0100259 case GSM_TCHF_FRAME_EFR:
260 payload_type = RTP_PT_GSM_EFR;
Andreas Eversberg88012b62014-01-22 10:05:51 +0100261 payload_len = RTP_LEN_GSM_EFR;
262 duration = RTP_GSM_DURATION;
Harald Welteaca8f152009-12-19 23:06:41 +0100263 break;
Andreas Eversberg63bfdd82014-01-17 19:06:38 +0100264 case GSM_TCHH_FRAME:
265 payload_type = RTP_PT_GSM_HALF;
266 payload_len = RTP_LEN_GSM_HALF;
267 duration = RTP_GSM_DURATION;
268 break;
Andreas Eversbergd8967f72012-03-08 14:39:19 +0100269 case GSM_TCH_FRAME_AMR:
270 payload_type = RTP_PT_AMR;
271 payload_len = frame->data[0];
272 duration = RTP_GSM_DURATION;
273 break;
Harald Welteda7ab742009-12-19 22:23:05 +0100274 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200275 DEBUGPC(DLMUX, "unsupported message type %d\n",
Harald Welteda7ab742009-12-19 22:23:05 +0100276 frame->msg_type);
277 return -EINVAL;
278 }
279
Harald Weltea87f8f92014-05-17 13:43:01 +0200280 if (payload_len > MAX_RTP_PAYLOAD_LEN) {
Andreas Eversbergd8967f72012-03-08 14:39:19 +0100281 DEBUGPC(DLMUX, "RTP payload too large (%d octets)\n",
282 payload_len);
283 return -EINVAL;
284 }
285
Harald Weltea87f8f92014-05-17 13:43:01 +0200286 msg = msgb_alloc(sizeof(struct rtp_hdr) + payload_len, "RTP-GSM");
Harald Welteda7ab742009-12-19 22:23:05 +0100287 if (!msg)
288 return -ENOMEM;
Harald Weltea87f8f92014-05-17 13:43:01 +0200289 rtph = (struct rtp_hdr *) msgb_put(msg, sizeof(struct rtp_hdr));
Harald Welteda7ab742009-12-19 22:23:05 +0100290 rtph->version = RTP_VERSION;
291 rtph->padding = 0;
292 rtph->extension = 0;
293 rtph->csrc_count = 0;
294 rtph->marker = 0;
295 rtph->payload_type = payload_type;
296 rtph->sequence = htons(rs->transmit.sequence++);
297 rtph->timestamp = htonl(rs->transmit.timestamp);
298 rs->transmit.timestamp += duration;
299 rtph->ssrc = htonl(rs->transmit.ssrc);
Harald Weltea87f8f92014-05-17 13:43:01 +0200300
301 payload = msgb_put(msg, payload_len);
Andreas Eversbergd8967f72012-03-08 14:39:19 +0100302 if (frame->msg_type == GSM_TCH_FRAME_AMR)
Harald Weltea87f8f92014-05-17 13:43:01 +0200303 memcpy(payload, frame->data + 1, payload_len);
Andreas Eversbergd8967f72012-03-08 14:39:19 +0100304 else
Harald Weltea87f8f92014-05-17 13:43:01 +0200305 memcpy(payload, frame->data, payload_len);
Harald Welteda7ab742009-12-19 22:23:05 +0100306 msgb_enqueue(&rss->tx_queue, msg);
307 rss->bfd.when |= BSC_FD_WRITE;
308
309 return 0;
310}
311
Holger Hans Peter Freyther89acf062009-07-29 06:46:26 +0200312/* iterate over all chunks in one RTCP message, look for CNAME IEs and
Harald Welte805f6442009-07-28 18:25:29 +0200313 * replace all of those with 'new_cname' */
314static int rtcp_sdes_cname_mangle(struct msgb *msg, struct rtcp_hdr *rh,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200315 uint16_t *rtcp_len, const char *new_cname)
Harald Welte805f6442009-07-28 18:25:29 +0200316{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200317 uint8_t *rtcp_end;
318 uint8_t *cur = (uint8_t *) rh;
319 uint8_t tag, len = 0;
Harald Welte805f6442009-07-28 18:25:29 +0200320
321 rtcp_end = cur + *rtcp_len;
322 /* move cur to end of RTP header */
Harald Welte198f3f52009-07-29 10:46:41 +0200323 cur += sizeof(*rh);
Harald Welte805f6442009-07-28 18:25:29 +0200324
325 /* iterate over Chunks */
326 while (cur+4 < rtcp_end) {
327 /* skip four bytes SSRC/CSRC */
328 cur += 4;
329
330 /* iterate over IE's inside the chunk */
331 while (cur+1 < rtcp_end) {
332 tag = *cur++;
333 if (tag == 0) {
334 /* end of chunk, skip additional zero */
Daniel Willmann45fcb852014-05-21 15:46:43 +0200335 while ((*cur++ == 0) && (cur < rtcp_end)) { }
Harald Welte805f6442009-07-28 18:25:29 +0200336 break;
337 }
338 len = *cur++;
339
340 if (tag == RTCP_IE_CNAME) {
341 /* we've found the CNAME, lets mangle it */
342 if (len < strlen(new_cname)) {
343 /* we need to make more space */
344 int increase = strlen(new_cname) - len;
345
346 msgb_push(msg, increase);
347 memmove(cur+len+increase, cur+len,
348 rtcp_end - (cur+len));
349 /* FIXME: we have to respect RTCP
350 * padding/alignment rules! */
351 len += increase;
352 *(cur-1) += increase;
353 rtcp_end += increase;
354 *rtcp_len += increase;
355 }
356 /* copy new CNAME into message */
357 memcpy(cur, new_cname, strlen(new_cname));
358 /* FIXME: zero the padding in case new CNAME
359 * is smaller than old one !!! */
360 }
361 cur += len;
362 }
363 }
364
365 return 0;
366}
367
368static int rtcp_mangle(struct msgb *msg, struct rtp_socket *rs)
369{
370 struct rtp_sub_socket *rss = &rs->rtcp;
371 struct rtcp_hdr *rtph;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200372 uint16_t old_len;
Harald Welte805f6442009-07-28 18:25:29 +0200373 int rc;
374
375 if (!mangle_rtcp_cname)
376 return 0;
377
Harald Welteda7ab742009-12-19 22:23:05 +0100378 printf("RTCP\n");
Harald Welte805f6442009-07-28 18:25:29 +0200379 /* iterate over list of RTCP messages */
380 rtph = (struct rtcp_hdr *)msg->data;
Harald Welteda7ab742009-12-19 22:23:05 +0100381 while ((void *)rtph + sizeof(*rtph) <= (void *)msg->data + msg->len) {
Harald Welte805f6442009-07-28 18:25:29 +0200382 old_len = (ntohs(rtph->length) + 1) * 4;
Harald Welteda7ab742009-12-19 22:23:05 +0100383 if ((void *)rtph + old_len > (void *)msg->data + msg->len) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200384 DEBUGPC(DLMUX, "received RTCP packet too short for "
Harald Welteda7ab742009-12-19 22:23:05 +0100385 "length element\n");
386 return -EINVAL;
387 }
Harald Welte805f6442009-07-28 18:25:29 +0200388 if (rtph->type == RTCP_TYPE_SDES) {
389 char new_cname[255];
390 strncpy(new_cname, inet_ntoa(rss->sin_local.sin_addr),
391 sizeof(new_cname));
392 new_cname[sizeof(new_cname)-1] = '\0';
393 rc = rtcp_sdes_cname_mangle(msg, rtph, &old_len,
394 new_cname);
395 if (rc < 0)
396 return rc;
397 }
398 rtph = (void *)rtph + old_len;
399 }
400
401 return 0;
402}
403
Harald Welteead7a7b2009-07-28 00:01:58 +0200404/* read from incoming RTP/RTCP socket */
405static int rtp_socket_read(struct rtp_socket *rs, struct rtp_sub_socket *rss)
406{
407 int rc;
408 struct msgb *msg = msgb_alloc(RTP_ALLOC_SIZE, "RTP/RTCP");
Harald Welteda7ab742009-12-19 22:23:05 +0100409 struct msgb *new_msg;
Harald Welteead7a7b2009-07-28 00:01:58 +0200410 struct rtp_sub_socket *other_rss;
411
412 if (!msg)
413 return -ENOMEM;
414
415 rc = read(rss->bfd.fd, msg->data, RTP_ALLOC_SIZE);
416 if (rc <= 0) {
417 rss->bfd.when &= ~BSC_FD_READ;
418 return rc;
419 }
420
421 msgb_put(msg, rc);
422
423 switch (rs->rx_action) {
424 case RTP_PROXY:
Harald Welte805f6442009-07-28 18:25:29 +0200425 if (!rs->proxy.other_sock) {
426 rc = -EIO;
427 goto out_free;
428 }
Harald Welteead7a7b2009-07-28 00:01:58 +0200429 if (rss->bfd.priv_nr == RTP_PRIV_RTP)
430 other_rss = &rs->proxy.other_sock->rtp;
Harald Welte805f6442009-07-28 18:25:29 +0200431 else if (rss->bfd.priv_nr == RTP_PRIV_RTCP) {
Harald Welteead7a7b2009-07-28 00:01:58 +0200432 other_rss = &rs->proxy.other_sock->rtcp;
Harald Welte805f6442009-07-28 18:25:29 +0200433 /* modify RTCP SDES CNAME */
434 rc = rtcp_mangle(msg, rs);
435 if (rc < 0)
436 goto out_free;
437 } else {
438 rc = -EINVAL;
439 goto out_free;
Harald Welteead7a7b2009-07-28 00:01:58 +0200440 }
441 msgb_enqueue(&other_rss->tx_queue, msg);
442 other_rss->bfd.when |= BSC_FD_WRITE;
443 break;
Holger Hans Peter Freyther1ddb3562009-08-10 07:57:13 +0200444
445 case RTP_RECV_UPSTREAM:
Harald Welteda7ab742009-12-19 22:23:05 +0100446 if (!rs->receive.callref || !rs->receive.net) {
447 rc = -EIO;
448 goto out_free;
449 }
450 if (rss->bfd.priv_nr == RTP_PRIV_RTCP) {
451 if (!mangle_rtcp_cname) {
452 msgb_free(msg);
453 break;
454 }
455 /* modify RTCP SDES CNAME */
456 rc = rtcp_mangle(msg, rs);
457 if (rc < 0)
458 goto out_free;
459 msgb_enqueue(&rss->tx_queue, msg);
460 rss->bfd.when |= BSC_FD_WRITE;
461 break;
462 }
463 if (rss->bfd.priv_nr != RTP_PRIV_RTP) {
464 rc = -EINVAL;
465 goto out_free;
466 }
467 rc = rtp_decode(msg, rs->receive.callref, &new_msg);
468 if (rc < 0)
469 goto out_free;
470 msgb_free(msg);
Harald Welte31c00f72011-03-03 23:29:05 +0100471 trau_tx_to_mncc(rs->receive.net, new_msg);
Harald Welteda7ab742009-12-19 22:23:05 +0100472 break;
473
474 case RTP_NONE: /* if socket exists, but disabled by app */
475 msgb_free(msg);
Holger Hans Peter Freyther1ddb3562009-08-10 07:57:13 +0200476 break;
Harald Welteead7a7b2009-07-28 00:01:58 +0200477 }
478
Harald Welteda7ab742009-12-19 22:23:05 +0100479 return 0;
Harald Welte805f6442009-07-28 18:25:29 +0200480
481out_free:
482 msgb_free(msg);
483 return rc;
Harald Welteead7a7b2009-07-28 00:01:58 +0200484}
485
Harald Welte647db842013-02-03 12:06:58 +0100486/* \brief write from tx_queue to RTP/RTCP socket */
Harald Welteead7a7b2009-07-28 00:01:58 +0200487static int rtp_socket_write(struct rtp_socket *rs, struct rtp_sub_socket *rss)
488{
489 struct msgb *msg;
490 int written;
491
492 msg = msgb_dequeue(&rss->tx_queue);
493 if (!msg) {
494 rss->bfd.when &= ~BSC_FD_WRITE;
495 return 0;
496 }
497
498 written = write(rss->bfd.fd, msg->data, msg->len);
499 if (written < msg->len) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200500 LOGP(DLMIB, LOGL_ERROR, "short write");
Harald Welteead7a7b2009-07-28 00:01:58 +0200501 msgb_free(msg);
502 return -EIO;
503 }
504
505 msgb_free(msg);
506
507 return 0;
508}
509
510
Harald Welte647db842013-02-03 12:06:58 +0100511/*! \brief callback for the select.c:bfd_* layer */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200512static int rtp_bfd_cb(struct osmo_fd *bfd, unsigned int flags)
Harald Welteead7a7b2009-07-28 00:01:58 +0200513{
514 struct rtp_socket *rs = bfd->data;
515 struct rtp_sub_socket *rss;
516
517 switch (bfd->priv_nr) {
518 case RTP_PRIV_RTP:
519 rss = &rs->rtp;
520 break;
521 case RTP_PRIV_RTCP:
522 rss = &rs->rtcp;
523 break;
524 default:
525 return -EINVAL;
526 }
527
528 if (flags & BSC_FD_READ)
529 rtp_socket_read(rs, rss);
530
531 if (flags & BSC_FD_WRITE)
532 rtp_socket_write(rs, rss);
533
534 return 0;
535}
536
Harald Welte647db842013-02-03 12:06:58 +0100537/*! \brief initialize one rtp sub-socket */
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200538static void init_rss(struct rtp_sub_socket *rss,
Harald Welteead7a7b2009-07-28 00:01:58 +0200539 struct rtp_socket *rs, int fd, int priv_nr)
540{
541 /* initialize bfd */
542 rss->bfd.fd = fd;
543 rss->bfd.data = rs;
544 rss->bfd.priv_nr = priv_nr;
545 rss->bfd.cb = rtp_bfd_cb;
546}
547
Harald Welte647db842013-02-03 12:06:58 +0100548/*! \brief create a new RTP/RTCP socket and bind it */
Harald Welteead7a7b2009-07-28 00:01:58 +0200549struct rtp_socket *rtp_socket_create(void)
550{
551 int rc;
552 struct rtp_socket *rs;
553
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200554 DEBUGP(DLMUX, "rtp_socket_create(): ");
Harald Welte805f6442009-07-28 18:25:29 +0200555
Harald Welteead7a7b2009-07-28 00:01:58 +0200556 rs = talloc_zero(tall_bsc_ctx, struct rtp_socket);
557 if (!rs)
558 return NULL;
559
560 INIT_LLIST_HEAD(&rs->rtp.tx_queue);
561 INIT_LLIST_HEAD(&rs->rtcp.tx_queue);
562
563 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
564 if (rc < 0)
565 goto out_free;
566
567 init_rss(&rs->rtp, rs, rc, RTP_PRIV_RTP);
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200568 rc = osmo_fd_register(&rs->rtp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200569 if (rc < 0)
570 goto out_rtp_socket;
571
572 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
573 if (rc < 0)
574 goto out_rtp_bfd;
575
576 init_rss(&rs->rtcp, rs, rc, RTP_PRIV_RTCP);
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200577 rc = osmo_fd_register(&rs->rtcp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200578 if (rc < 0)
579 goto out_rtcp_socket;
580
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200581 DEBUGPC(DLMUX, "success\n");
Harald Welte805f6442009-07-28 18:25:29 +0200582
583 rc = rtp_socket_bind(rs, INADDR_ANY);
584 if (rc < 0)
585 goto out_rtcp_bfd;
586
Harald Welteead7a7b2009-07-28 00:01:58 +0200587 return rs;
588
Harald Welte805f6442009-07-28 18:25:29 +0200589out_rtcp_bfd:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200590 osmo_fd_unregister(&rs->rtcp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200591out_rtcp_socket:
592 close(rs->rtcp.bfd.fd);
593out_rtp_bfd:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200594 osmo_fd_unregister(&rs->rtp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200595out_rtp_socket:
596 close(rs->rtp.bfd.fd);
597out_free:
598 talloc_free(rs);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200599 DEBUGPC(DLMUX, "failed\n");
Harald Welteead7a7b2009-07-28 00:01:58 +0200600 return NULL;
601}
602
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200603static int rtp_sub_socket_bind(struct rtp_sub_socket *rss, uint32_t ip,
604 uint16_t port)
Harald Welteead7a7b2009-07-28 00:01:58 +0200605{
Harald Welte805f6442009-07-28 18:25:29 +0200606 int rc;
607 socklen_t alen = sizeof(rss->sin_local);
608
Harald Welteead7a7b2009-07-28 00:01:58 +0200609 rss->sin_local.sin_family = AF_INET;
610 rss->sin_local.sin_addr.s_addr = htonl(ip);
611 rss->sin_local.sin_port = htons(port);
612 rss->bfd.when |= BSC_FD_READ;
613
Harald Welte805f6442009-07-28 18:25:29 +0200614 rc = bind(rss->bfd.fd, (struct sockaddr *)&rss->sin_local,
615 sizeof(rss->sin_local));
616 if (rc < 0)
617 return rc;
618
619 /* retrieve the address we actually bound to, in case we
620 * passed INADDR_ANY as IP address */
621 return getsockname(rss->bfd.fd, (struct sockaddr *)&rss->sin_local,
622 &alen);
Harald Welteead7a7b2009-07-28 00:01:58 +0200623}
624
625#define RTP_PORT_BASE 30000
Harald Welte805f6442009-07-28 18:25:29 +0200626static unsigned int next_udp_port = RTP_PORT_BASE;
Harald Welteead7a7b2009-07-28 00:01:58 +0200627
Harald Welte647db842013-02-03 12:06:58 +0100628/*! \brief bind a RTP socket to a specific local address
629 * \param[in] rs RTP socket to be bound
630 * \param[in] ip local IP address to which socket is to be bound
631 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200632int rtp_socket_bind(struct rtp_socket *rs, uint32_t ip)
Harald Welteead7a7b2009-07-28 00:01:58 +0200633{
Harald Welte805f6442009-07-28 18:25:29 +0200634 int rc = -EIO;
635 struct in_addr ia;
636
637 ia.s_addr = htonl(ip);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200638 DEBUGP(DLMUX, "rtp_socket_bind(rs=%p, IP=%s): ", rs,
Harald Welte805f6442009-07-28 18:25:29 +0200639 inet_ntoa(ia));
Harald Welteead7a7b2009-07-28 00:01:58 +0200640
641 /* try to bind to a consecutive pair of ports */
Harald Welte805f6442009-07-28 18:25:29 +0200642 for (next_udp_port = next_udp_port % 0xffff;
643 next_udp_port < 0xffff; next_udp_port += 2) {
Harald Welteead7a7b2009-07-28 00:01:58 +0200644 rc = rtp_sub_socket_bind(&rs->rtp, ip, next_udp_port);
645 if (rc != 0)
646 continue;
647
648 rc = rtp_sub_socket_bind(&rs->rtcp, ip, next_udp_port+1);
649 if (rc == 0)
650 break;
651 }
Harald Welte805f6442009-07-28 18:25:29 +0200652 if (rc < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200653 DEBUGPC(DLMUX, "failed\n");
Harald Welteead7a7b2009-07-28 00:01:58 +0200654 return rc;
Harald Welte805f6442009-07-28 18:25:29 +0200655 }
Harald Welteead7a7b2009-07-28 00:01:58 +0200656
Harald Welte805f6442009-07-28 18:25:29 +0200657 ia.s_addr = rs->rtp.sin_local.sin_addr.s_addr;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200658 DEBUGPC(DLMUX, "BOUND_IP=%s, BOUND_PORT=%u\n",
Harald Welte805f6442009-07-28 18:25:29 +0200659 inet_ntoa(ia), ntohs(rs->rtp.sin_local.sin_port));
Harald Welteead7a7b2009-07-28 00:01:58 +0200660 return ntohs(rs->rtp.sin_local.sin_port);
661}
662
663static int rtp_sub_socket_connect(struct rtp_sub_socket *rss,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200664 uint32_t ip, uint16_t port)
Harald Welteead7a7b2009-07-28 00:01:58 +0200665{
Harald Welte805f6442009-07-28 18:25:29 +0200666 int rc;
667 socklen_t alen = sizeof(rss->sin_local);
668
Harald Welteead7a7b2009-07-28 00:01:58 +0200669 rss->sin_remote.sin_family = AF_INET;
670 rss->sin_remote.sin_addr.s_addr = htonl(ip);
671 rss->sin_remote.sin_port = htons(port);
672
Harald Welte805f6442009-07-28 18:25:29 +0200673 rc = connect(rss->bfd.fd, (struct sockaddr *) &rss->sin_remote,
674 sizeof(rss->sin_remote));
675 if (rc < 0)
676 return rc;
677
678 return getsockname(rss->bfd.fd, (struct sockaddr *)&rss->sin_local,
679 &alen);
Harald Welteead7a7b2009-07-28 00:01:58 +0200680}
681
Harald Welte647db842013-02-03 12:06:58 +0100682/*! \brief 'connect' a RTP socket to a remote peer
683 * \param[in] rs RTP socket to be connected
684 * \param[in] ip remote IP address to which to connect
685 * \param[in] port remote UDP port number to which to connect
686 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200687int rtp_socket_connect(struct rtp_socket *rs, uint32_t ip, uint16_t port)
Harald Welteead7a7b2009-07-28 00:01:58 +0200688{
689 int rc;
Harald Welte805f6442009-07-28 18:25:29 +0200690 struct in_addr ia;
691
692 ia.s_addr = htonl(ip);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200693 DEBUGP(DLMUX, "rtp_socket_connect(rs=%p, ip=%s, port=%u)\n",
Harald Welte805f6442009-07-28 18:25:29 +0200694 rs, inet_ntoa(ia), port);
Harald Welteead7a7b2009-07-28 00:01:58 +0200695
696 rc = rtp_sub_socket_connect(&rs->rtp, ip, port);
697 if (rc < 0)
698 return rc;
699
700 return rtp_sub_socket_connect(&rs->rtcp, ip, port+1);
701}
702
Harald Welte647db842013-02-03 12:06:58 +0100703/*! \brief bind two RTP/RTCP sockets together in the proxy
704 * \param[in] this First RTP socket
705 * \param[in] other Second RTP socket
706 */
Harald Welteead7a7b2009-07-28 00:01:58 +0200707int rtp_socket_proxy(struct rtp_socket *this, struct rtp_socket *other)
708{
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200709 DEBUGP(DLMUX, "rtp_socket_proxy(this=%p, other=%p)\n",
Harald Welte805f6442009-07-28 18:25:29 +0200710 this, other);
711
Harald Welteead7a7b2009-07-28 00:01:58 +0200712 this->rx_action = RTP_PROXY;
713 this->proxy.other_sock = other;
714
715 other->rx_action = RTP_PROXY;
716 other->proxy.other_sock = this;
717
718 return 0;
719}
720
Harald Welte647db842013-02-03 12:06:58 +0100721/*! \brief bind RTP/RTCP socket to application, disabling proxy
722 * \param[in] this RTP socket
723 * \param[in] net gsm_network argument to trau_tx_to_mncc()
724 * \param[in] callref callref argument to trau_tx_to_mncc()
725 */
Harald Welte9fb1f102009-12-20 17:07:23 +0100726int rtp_socket_upstream(struct rtp_socket *this, struct gsm_network *net,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200727 uint32_t callref)
Harald Welteda7ab742009-12-19 22:23:05 +0100728{
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200729 DEBUGP(DLMUX, "rtp_socket_proxy(this=%p, callref=%u)\n",
Harald Welteda7ab742009-12-19 22:23:05 +0100730 this, callref);
731
732 if (callref) {
733 this->rx_action = RTP_RECV_UPSTREAM;
734 this->receive.net = net;
735 this->receive.callref = callref;
736 } else
737 this->rx_action = RTP_NONE;
738
739 return 0;
740}
741
Harald Welteead7a7b2009-07-28 00:01:58 +0200742static void free_tx_queue(struct rtp_sub_socket *rss)
743{
744 struct msgb *msg;
745
746 while ((msg = msgb_dequeue(&rss->tx_queue)))
747 msgb_free(msg);
748}
749
Harald Welte647db842013-02-03 12:06:58 +0100750/*! \brief Free/release a previously allocated RTP socket
751 * \param[in[] rs RTP/RTCP socket to be released
752 */
Harald Welteead7a7b2009-07-28 00:01:58 +0200753int rtp_socket_free(struct rtp_socket *rs)
754{
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200755 DEBUGP(DLMUX, "rtp_socket_free(rs=%p)\n", rs);
Harald Welteead7a7b2009-07-28 00:01:58 +0200756
757 /* make sure we don't leave references dangling to us */
758 if (rs->rx_action == RTP_PROXY &&
759 rs->proxy.other_sock)
760 rs->proxy.other_sock->proxy.other_sock = NULL;
761
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200762 osmo_fd_unregister(&rs->rtp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200763 close(rs->rtp.bfd.fd);
764 free_tx_queue(&rs->rtp);
765
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200766 osmo_fd_unregister(&rs->rtcp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200767 close(rs->rtcp.bfd.fd);
768 free_tx_queue(&rs->rtcp);
769
770 talloc_free(rs);
771
772 return 0;
773}