blob: 94a5b2f572d2dd2c129647216d45f7cbf794717a [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>
Harald Welteead7a7b2009-07-28 00:01:58 +020039
Holger Hans Peter Freyther3cb28792010-10-12 15:39:46 +020040/* attempt to determine byte order */
Holger Hans Peter Freyther3cb28792010-10-12 15:39:46 +020041#include <sys/param.h>
42#include <limits.h>
43
44#ifndef __BYTE_ORDER
Tobias Engelaff20712012-10-24 17:53:50 +020045# ifdef __APPLE__
46# define __BYTE_ORDER __DARWIN_BYTE_ORDER
47# define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
48# define __BIG_ENDIAN __DARWIN_BIG_ENDIAN
49# else
50# error "__BYTE_ORDER should be defined by someone"
51# endif
Holger Hans Peter Freyther3cb28792010-10-12 15:39:46 +020052#endif
53
Harald Welteead7a7b2009-07-28 00:01:58 +020054static LLIST_HEAD(rtp_sockets);
55
Harald Welte805f6442009-07-28 18:25:29 +020056/* should we mangle the CNAME inside SDES of RTCP packets? We disable
57 * this by default, as it seems to be not needed */
58static int mangle_rtcp_cname = 0;
59
Harald Welteead7a7b2009-07-28 00:01:58 +020060enum rtp_bfd_priv {
61 RTP_PRIV_NONE,
62 RTP_PRIV_RTP,
63 RTP_PRIV_RTCP
64};
65
66#define RTP_ALLOC_SIZE 1500
67
Harald Welte805f6442009-07-28 18:25:29 +020068/* according to RFC 1889 */
69struct rtcp_hdr {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020070 uint8_t byte0;
71 uint8_t type;
72 uint16_t length;
Harald Welte805f6442009-07-28 18:25:29 +020073} __attribute__((packed));
74
75#define RTCP_TYPE_SDES 202
76
77#define RTCP_IE_CNAME 1
78
Harald Welteda7ab742009-12-19 22:23:05 +010079/* according to RFC 3550 */
80struct rtp_hdr {
Holger Hans Peter Freyther2890d102010-02-26 13:12:41 +010081#if __BYTE_ORDER == __LITTLE_ENDIAN
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020082 uint8_t csrc_count:4,
Harald Welteda7ab742009-12-19 22:23:05 +010083 extension:1,
84 padding:1,
85 version:2;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020086 uint8_t payload_type:7,
Harald Welteda7ab742009-12-19 22:23:05 +010087 marker:1;
Holger Hans Peter Freyther2890d102010-02-26 13:12:41 +010088#elif __BYTE_ORDER == __BIG_ENDIAN
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020089 uint8_t version:2,
Holger Hans Peter Freyther2890d102010-02-26 13:12:41 +010090 padding:1,
91 extension:1,
92 csrc_count:4;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020093 uint8_t marker:1,
Holger Hans Peter Freyther2890d102010-02-26 13:12:41 +010094 payload_type:7;
95#endif
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020096 uint16_t sequence;
97 uint32_t timestamp;
98 uint32_t ssrc;
Harald Welteda7ab742009-12-19 22:23:05 +010099} __attribute__((packed));
100
101struct rtp_x_hdr {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200102 uint16_t by_profile;
103 uint16_t length;
Harald Welteda7ab742009-12-19 22:23:05 +0100104} __attribute__((packed));
105
106#define RTP_VERSION 2
107
Harald Welteda7ab742009-12-19 22:23:05 +0100108/* decode an rtp frame and create a new buffer with payload */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200109static int rtp_decode(struct msgb *msg, uint32_t callref, struct msgb **data)
Harald Welteda7ab742009-12-19 22:23:05 +0100110{
111 struct msgb *new_msg;
112 struct gsm_data_frame *frame;
113 struct rtp_hdr *rtph = (struct rtp_hdr *)msg->data;
114 struct rtp_x_hdr *rtpxh;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200115 uint8_t *payload;
Harald Welteda7ab742009-12-19 22:23:05 +0100116 int payload_len;
117 int msg_type;
118 int x_len;
119
120 if (msg->len < 12) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200121 DEBUGPC(DLMUX, "received RTP frame too short (len = %d)\n",
Harald Welteda7ab742009-12-19 22:23:05 +0100122 msg->len);
123 return -EINVAL;
124 }
125 if (rtph->version != RTP_VERSION) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200126 DEBUGPC(DLMUX, "received RTP version %d not supported.\n",
Harald Welteda7ab742009-12-19 22:23:05 +0100127 rtph->version);
128 return -EINVAL;
129 }
130 payload = msg->data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
131 payload_len = msg->len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
132 if (payload_len < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200133 DEBUGPC(DLMUX, "received RTP frame too short (len = %d, "
Harald Welteda7ab742009-12-19 22:23:05 +0100134 "csrc count = %d)\n", msg->len, rtph->csrc_count);
135 return -EINVAL;
136 }
137 if (rtph->extension) {
138 if (payload_len < sizeof(struct rtp_x_hdr)) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200139 DEBUGPC(DLMUX, "received RTP frame too short for "
Harald Welteda7ab742009-12-19 22:23:05 +0100140 "extension header\n");
141 return -EINVAL;
142 }
143 rtpxh = (struct rtp_x_hdr *)payload;
144 x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
145 payload += x_len;
146 payload_len -= x_len;
147 if (payload_len < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200148 DEBUGPC(DLMUX, "received RTP frame too short, "
Harald Welteda7ab742009-12-19 22:23:05 +0100149 "extension header exceeds frame length\n");
150 return -EINVAL;
151 }
152 }
153 if (rtph->padding) {
Jacob Erlbeckd9e40392013-11-21 19:05:42 +0100154 if (payload_len < 1) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200155 DEBUGPC(DLMUX, "received RTP frame too short for "
Harald Welteda7ab742009-12-19 22:23:05 +0100156 "padding length\n");
157 return -EINVAL;
158 }
159 payload_len -= payload[payload_len - 1];
160 if (payload_len < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200161 DEBUGPC(DLMUX, "received RTP frame with padding "
Harald Welteda7ab742009-12-19 22:23:05 +0100162 "greater than payload\n");
163 return -EINVAL;
164 }
165 }
166
167 switch (rtph->payload_type) {
168 case RTP_PT_GSM_FULL:
169 msg_type = GSM_TCHF_FRAME;
Andreas Eversberg88012b62014-01-22 10:05:51 +0100170 if (payload_len != RTP_LEN_GSM_FULL) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200171 DEBUGPC(DLMUX, "received RTP full rate frame with "
Andreas Eversberg88012b62014-01-22 10:05:51 +0100172 "payload length != %d (len = %d)\n",
173 RTP_LEN_GSM_FULL, payload_len);
Harald Welteda7ab742009-12-19 22:23:05 +0100174 return -EINVAL;
175 }
176 break;
Harald Welteaca8f152009-12-19 23:06:41 +0100177 case RTP_PT_GSM_EFR:
178 msg_type = GSM_TCHF_FRAME_EFR;
Andreas Eversberg88012b62014-01-22 10:05:51 +0100179 if (payload_len != RTP_LEN_GSM_EFR) {
180 DEBUGPC(DLMUX, "received RTP extended full rate frame "
181 "with payload length != %d (len = %d)\n",
182 RTP_LEN_GSM_EFR, payload_len);
183 return -EINVAL;
184 }
Harald Welteaca8f152009-12-19 23:06:41 +0100185 break;
Harald Welteda7ab742009-12-19 22:23:05 +0100186 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200187 DEBUGPC(DLMUX, "received RTP frame with unknown payload "
Harald Welteda7ab742009-12-19 22:23:05 +0100188 "type %d\n", rtph->payload_type);
189 return -EINVAL;
190 }
191
192 new_msg = msgb_alloc(sizeof(struct gsm_data_frame) + payload_len,
193 "GSM-DATA");
194 if (!new_msg)
195 return -ENOMEM;
196 frame = (struct gsm_data_frame *)(new_msg->data);
197 frame->msg_type = msg_type;
198 frame->callref = callref;
199 memcpy(frame->data, payload, payload_len);
200 msgb_put(new_msg, sizeof(struct gsm_data_frame) + payload_len);
201
202 *data = new_msg;
203 return 0;
204}
205
Harald Welte392736d2009-12-20 13:16:14 +0100206/* "to - from" */
207static void tv_difference(struct timeval *diff, const struct timeval *from,
208 const struct timeval *__to)
209{
210 struct timeval _to = *__to, *to = &_to;
211
212 if (to->tv_usec < from->tv_usec) {
213 to->tv_sec -= 1;
214 to->tv_usec += 1000000;
215 }
216
217 diff->tv_usec = to->tv_usec - from->tv_usec;
218 diff->tv_sec = to->tv_sec - from->tv_sec;
219}
220
Harald Welte647db842013-02-03 12:06:58 +0100221/*! \brief encode and send a rtp frame
222 * \param[in] rs RTP socket through which we shall send
223 * \param[in] frame GSM RTP frame to be sent
224 */
Harald Welteda7ab742009-12-19 22:23:05 +0100225int rtp_send_frame(struct rtp_socket *rs, struct gsm_data_frame *frame)
226{
227 struct rtp_sub_socket *rss = &rs->rtp;
228 struct msgb *msg;
229 struct rtp_hdr *rtph;
230 int payload_type;
231 int payload_len;
232 int duration; /* in samples */
233
234 if (rs->tx_action != RTP_SEND_DOWNSTREAM) {
235 /* initialize sequences */
236 rs->tx_action = RTP_SEND_DOWNSTREAM;
237 rs->transmit.ssrc = rand();
238 rs->transmit.sequence = random();
239 rs->transmit.timestamp = random();
240 }
241
242 switch (frame->msg_type) {
243 case GSM_TCHF_FRAME:
244 payload_type = RTP_PT_GSM_FULL;
Andreas Eversberg88012b62014-01-22 10:05:51 +0100245 payload_len = RTP_LEN_GSM_FULL;
246 duration = RTP_GSM_DURATION;
Harald Welteda7ab742009-12-19 22:23:05 +0100247 break;
Harald Welteaca8f152009-12-19 23:06:41 +0100248 case GSM_TCHF_FRAME_EFR:
249 payload_type = RTP_PT_GSM_EFR;
Andreas Eversberg88012b62014-01-22 10:05:51 +0100250 payload_len = RTP_LEN_GSM_EFR;
251 duration = RTP_GSM_DURATION;
Harald Welteaca8f152009-12-19 23:06:41 +0100252 break;
Harald Welteda7ab742009-12-19 22:23:05 +0100253 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200254 DEBUGPC(DLMUX, "unsupported message type %d\n",
Harald Welteda7ab742009-12-19 22:23:05 +0100255 frame->msg_type);
256 return -EINVAL;
257 }
258
Harald Welte392736d2009-12-20 13:16:14 +0100259 {
260 struct timeval tv, tv_diff;
261 long int usec_diff, frame_diff;
262
263 gettimeofday(&tv, NULL);
264 tv_difference(&tv_diff, &rs->transmit.last_tv, &tv);
265 rs->transmit.last_tv = tv;
266
267 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
268 frame_diff = (usec_diff / 20000);
269
270 if (abs(frame_diff) > 1) {
271 long int frame_diff_excess = frame_diff - 1;
272
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200273 LOGP(DLMUX, LOGL_NOTICE,
Harald Welte (local)9fcf6d72009-12-27 17:01:40 +0100274 "Correcting frame difference of %ld frames\n", frame_diff_excess);
Harald Welte392736d2009-12-20 13:16:14 +0100275 rs->transmit.sequence += frame_diff_excess;
276 rs->transmit.timestamp += frame_diff_excess * duration;
277 }
278 }
279
Harald Welteda7ab742009-12-19 22:23:05 +0100280 msg = msgb_alloc(sizeof(struct rtp_hdr) + payload_len, "RTP-GSM-FULL");
281 if (!msg)
282 return -ENOMEM;
283 rtph = (struct rtp_hdr *)msg->data;
284 rtph->version = RTP_VERSION;
285 rtph->padding = 0;
286 rtph->extension = 0;
287 rtph->csrc_count = 0;
288 rtph->marker = 0;
289 rtph->payload_type = payload_type;
290 rtph->sequence = htons(rs->transmit.sequence++);
291 rtph->timestamp = htonl(rs->transmit.timestamp);
292 rs->transmit.timestamp += duration;
293 rtph->ssrc = htonl(rs->transmit.ssrc);
294 memcpy(msg->data + sizeof(struct rtp_hdr), frame->data, payload_len);
295 msgb_put(msg, sizeof(struct rtp_hdr) + payload_len);
296 msgb_enqueue(&rss->tx_queue, msg);
297 rss->bfd.when |= BSC_FD_WRITE;
298
299 return 0;
300}
301
Holger Hans Peter Freyther89acf062009-07-29 06:46:26 +0200302/* iterate over all chunks in one RTCP message, look for CNAME IEs and
Harald Welte805f6442009-07-28 18:25:29 +0200303 * replace all of those with 'new_cname' */
304static int rtcp_sdes_cname_mangle(struct msgb *msg, struct rtcp_hdr *rh,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200305 uint16_t *rtcp_len, const char *new_cname)
Harald Welte805f6442009-07-28 18:25:29 +0200306{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200307 uint8_t *rtcp_end;
308 uint8_t *cur = (uint8_t *) rh;
309 uint8_t tag, len = 0;
Harald Welte805f6442009-07-28 18:25:29 +0200310
311 rtcp_end = cur + *rtcp_len;
312 /* move cur to end of RTP header */
Harald Welte198f3f52009-07-29 10:46:41 +0200313 cur += sizeof(*rh);
Harald Welte805f6442009-07-28 18:25:29 +0200314
315 /* iterate over Chunks */
316 while (cur+4 < rtcp_end) {
317 /* skip four bytes SSRC/CSRC */
318 cur += 4;
319
320 /* iterate over IE's inside the chunk */
321 while (cur+1 < rtcp_end) {
322 tag = *cur++;
323 if (tag == 0) {
324 /* end of chunk, skip additional zero */
325 while (*cur++ == 0) { }
326 break;
327 }
328 len = *cur++;
329
330 if (tag == RTCP_IE_CNAME) {
331 /* we've found the CNAME, lets mangle it */
332 if (len < strlen(new_cname)) {
333 /* we need to make more space */
334 int increase = strlen(new_cname) - len;
335
336 msgb_push(msg, increase);
337 memmove(cur+len+increase, cur+len,
338 rtcp_end - (cur+len));
339 /* FIXME: we have to respect RTCP
340 * padding/alignment rules! */
341 len += increase;
342 *(cur-1) += increase;
343 rtcp_end += increase;
344 *rtcp_len += increase;
345 }
346 /* copy new CNAME into message */
347 memcpy(cur, new_cname, strlen(new_cname));
348 /* FIXME: zero the padding in case new CNAME
349 * is smaller than old one !!! */
350 }
351 cur += len;
352 }
353 }
354
355 return 0;
356}
357
358static int rtcp_mangle(struct msgb *msg, struct rtp_socket *rs)
359{
360 struct rtp_sub_socket *rss = &rs->rtcp;
361 struct rtcp_hdr *rtph;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200362 uint16_t old_len;
Harald Welte805f6442009-07-28 18:25:29 +0200363 int rc;
364
365 if (!mangle_rtcp_cname)
366 return 0;
367
Harald Welteda7ab742009-12-19 22:23:05 +0100368 printf("RTCP\n");
Harald Welte805f6442009-07-28 18:25:29 +0200369 /* iterate over list of RTCP messages */
370 rtph = (struct rtcp_hdr *)msg->data;
Harald Welteda7ab742009-12-19 22:23:05 +0100371 while ((void *)rtph + sizeof(*rtph) <= (void *)msg->data + msg->len) {
Harald Welte805f6442009-07-28 18:25:29 +0200372 old_len = (ntohs(rtph->length) + 1) * 4;
Harald Welteda7ab742009-12-19 22:23:05 +0100373 if ((void *)rtph + old_len > (void *)msg->data + msg->len) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200374 DEBUGPC(DLMUX, "received RTCP packet too short for "
Harald Welteda7ab742009-12-19 22:23:05 +0100375 "length element\n");
376 return -EINVAL;
377 }
Harald Welte805f6442009-07-28 18:25:29 +0200378 if (rtph->type == RTCP_TYPE_SDES) {
379 char new_cname[255];
380 strncpy(new_cname, inet_ntoa(rss->sin_local.sin_addr),
381 sizeof(new_cname));
382 new_cname[sizeof(new_cname)-1] = '\0';
383 rc = rtcp_sdes_cname_mangle(msg, rtph, &old_len,
384 new_cname);
385 if (rc < 0)
386 return rc;
387 }
388 rtph = (void *)rtph + old_len;
389 }
390
391 return 0;
392}
393
Harald Welteead7a7b2009-07-28 00:01:58 +0200394/* read from incoming RTP/RTCP socket */
395static int rtp_socket_read(struct rtp_socket *rs, struct rtp_sub_socket *rss)
396{
397 int rc;
398 struct msgb *msg = msgb_alloc(RTP_ALLOC_SIZE, "RTP/RTCP");
Harald Welteda7ab742009-12-19 22:23:05 +0100399 struct msgb *new_msg;
Harald Welteead7a7b2009-07-28 00:01:58 +0200400 struct rtp_sub_socket *other_rss;
401
402 if (!msg)
403 return -ENOMEM;
404
405 rc = read(rss->bfd.fd, msg->data, RTP_ALLOC_SIZE);
406 if (rc <= 0) {
407 rss->bfd.when &= ~BSC_FD_READ;
408 return rc;
409 }
410
411 msgb_put(msg, rc);
412
413 switch (rs->rx_action) {
414 case RTP_PROXY:
Harald Welte805f6442009-07-28 18:25:29 +0200415 if (!rs->proxy.other_sock) {
416 rc = -EIO;
417 goto out_free;
418 }
Harald Welteead7a7b2009-07-28 00:01:58 +0200419 if (rss->bfd.priv_nr == RTP_PRIV_RTP)
420 other_rss = &rs->proxy.other_sock->rtp;
Harald Welte805f6442009-07-28 18:25:29 +0200421 else if (rss->bfd.priv_nr == RTP_PRIV_RTCP) {
Harald Welteead7a7b2009-07-28 00:01:58 +0200422 other_rss = &rs->proxy.other_sock->rtcp;
Harald Welte805f6442009-07-28 18:25:29 +0200423 /* modify RTCP SDES CNAME */
424 rc = rtcp_mangle(msg, rs);
425 if (rc < 0)
426 goto out_free;
427 } else {
428 rc = -EINVAL;
429 goto out_free;
Harald Welteead7a7b2009-07-28 00:01:58 +0200430 }
431 msgb_enqueue(&other_rss->tx_queue, msg);
432 other_rss->bfd.when |= BSC_FD_WRITE;
433 break;
Holger Hans Peter Freyther1ddb3562009-08-10 07:57:13 +0200434
435 case RTP_RECV_UPSTREAM:
Harald Welteda7ab742009-12-19 22:23:05 +0100436 if (!rs->receive.callref || !rs->receive.net) {
437 rc = -EIO;
438 goto out_free;
439 }
440 if (rss->bfd.priv_nr == RTP_PRIV_RTCP) {
441 if (!mangle_rtcp_cname) {
442 msgb_free(msg);
443 break;
444 }
445 /* modify RTCP SDES CNAME */
446 rc = rtcp_mangle(msg, rs);
447 if (rc < 0)
448 goto out_free;
449 msgb_enqueue(&rss->tx_queue, msg);
450 rss->bfd.when |= BSC_FD_WRITE;
451 break;
452 }
453 if (rss->bfd.priv_nr != RTP_PRIV_RTP) {
454 rc = -EINVAL;
455 goto out_free;
456 }
457 rc = rtp_decode(msg, rs->receive.callref, &new_msg);
458 if (rc < 0)
459 goto out_free;
460 msgb_free(msg);
Harald Welte31c00f72011-03-03 23:29:05 +0100461 trau_tx_to_mncc(rs->receive.net, new_msg);
Harald Welteda7ab742009-12-19 22:23:05 +0100462 break;
463
464 case RTP_NONE: /* if socket exists, but disabled by app */
465 msgb_free(msg);
Holger Hans Peter Freyther1ddb3562009-08-10 07:57:13 +0200466 break;
Harald Welteead7a7b2009-07-28 00:01:58 +0200467 }
468
Harald Welteda7ab742009-12-19 22:23:05 +0100469 return 0;
Harald Welte805f6442009-07-28 18:25:29 +0200470
471out_free:
472 msgb_free(msg);
473 return rc;
Harald Welteead7a7b2009-07-28 00:01:58 +0200474}
475
Harald Welte647db842013-02-03 12:06:58 +0100476/* \brief write from tx_queue to RTP/RTCP socket */
Harald Welteead7a7b2009-07-28 00:01:58 +0200477static int rtp_socket_write(struct rtp_socket *rs, struct rtp_sub_socket *rss)
478{
479 struct msgb *msg;
480 int written;
481
482 msg = msgb_dequeue(&rss->tx_queue);
483 if (!msg) {
484 rss->bfd.when &= ~BSC_FD_WRITE;
485 return 0;
486 }
487
488 written = write(rss->bfd.fd, msg->data, msg->len);
489 if (written < msg->len) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200490 LOGP(DLMIB, LOGL_ERROR, "short write");
Harald Welteead7a7b2009-07-28 00:01:58 +0200491 msgb_free(msg);
492 return -EIO;
493 }
494
495 msgb_free(msg);
496
497 return 0;
498}
499
500
Harald Welte647db842013-02-03 12:06:58 +0100501/*! \brief callback for the select.c:bfd_* layer */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200502static int rtp_bfd_cb(struct osmo_fd *bfd, unsigned int flags)
Harald Welteead7a7b2009-07-28 00:01:58 +0200503{
504 struct rtp_socket *rs = bfd->data;
505 struct rtp_sub_socket *rss;
506
507 switch (bfd->priv_nr) {
508 case RTP_PRIV_RTP:
509 rss = &rs->rtp;
510 break;
511 case RTP_PRIV_RTCP:
512 rss = &rs->rtcp;
513 break;
514 default:
515 return -EINVAL;
516 }
517
518 if (flags & BSC_FD_READ)
519 rtp_socket_read(rs, rss);
520
521 if (flags & BSC_FD_WRITE)
522 rtp_socket_write(rs, rss);
523
524 return 0;
525}
526
Harald Welte647db842013-02-03 12:06:58 +0100527/*! \brief initialize one rtp sub-socket */
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200528static void init_rss(struct rtp_sub_socket *rss,
Harald Welteead7a7b2009-07-28 00:01:58 +0200529 struct rtp_socket *rs, int fd, int priv_nr)
530{
531 /* initialize bfd */
532 rss->bfd.fd = fd;
533 rss->bfd.data = rs;
534 rss->bfd.priv_nr = priv_nr;
535 rss->bfd.cb = rtp_bfd_cb;
536}
537
Harald Welte647db842013-02-03 12:06:58 +0100538/*! \brief create a new RTP/RTCP socket and bind it */
Harald Welteead7a7b2009-07-28 00:01:58 +0200539struct rtp_socket *rtp_socket_create(void)
540{
541 int rc;
542 struct rtp_socket *rs;
543
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200544 DEBUGP(DLMUX, "rtp_socket_create(): ");
Harald Welte805f6442009-07-28 18:25:29 +0200545
Harald Welteead7a7b2009-07-28 00:01:58 +0200546 rs = talloc_zero(tall_bsc_ctx, struct rtp_socket);
547 if (!rs)
548 return NULL;
549
550 INIT_LLIST_HEAD(&rs->rtp.tx_queue);
551 INIT_LLIST_HEAD(&rs->rtcp.tx_queue);
552
553 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
554 if (rc < 0)
555 goto out_free;
556
557 init_rss(&rs->rtp, rs, rc, RTP_PRIV_RTP);
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200558 rc = osmo_fd_register(&rs->rtp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200559 if (rc < 0)
560 goto out_rtp_socket;
561
562 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
563 if (rc < 0)
564 goto out_rtp_bfd;
565
566 init_rss(&rs->rtcp, rs, rc, RTP_PRIV_RTCP);
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200567 rc = osmo_fd_register(&rs->rtcp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200568 if (rc < 0)
569 goto out_rtcp_socket;
570
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200571 DEBUGPC(DLMUX, "success\n");
Harald Welte805f6442009-07-28 18:25:29 +0200572
573 rc = rtp_socket_bind(rs, INADDR_ANY);
574 if (rc < 0)
575 goto out_rtcp_bfd;
576
Harald Welteead7a7b2009-07-28 00:01:58 +0200577 return rs;
578
Harald Welte805f6442009-07-28 18:25:29 +0200579out_rtcp_bfd:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200580 osmo_fd_unregister(&rs->rtcp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200581out_rtcp_socket:
582 close(rs->rtcp.bfd.fd);
583out_rtp_bfd:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200584 osmo_fd_unregister(&rs->rtp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200585out_rtp_socket:
586 close(rs->rtp.bfd.fd);
587out_free:
588 talloc_free(rs);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200589 DEBUGPC(DLMUX, "failed\n");
Harald Welteead7a7b2009-07-28 00:01:58 +0200590 return NULL;
591}
592
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200593static int rtp_sub_socket_bind(struct rtp_sub_socket *rss, uint32_t ip,
594 uint16_t port)
Harald Welteead7a7b2009-07-28 00:01:58 +0200595{
Harald Welte805f6442009-07-28 18:25:29 +0200596 int rc;
597 socklen_t alen = sizeof(rss->sin_local);
598
Harald Welteead7a7b2009-07-28 00:01:58 +0200599 rss->sin_local.sin_family = AF_INET;
600 rss->sin_local.sin_addr.s_addr = htonl(ip);
601 rss->sin_local.sin_port = htons(port);
602 rss->bfd.when |= BSC_FD_READ;
603
Harald Welte805f6442009-07-28 18:25:29 +0200604 rc = bind(rss->bfd.fd, (struct sockaddr *)&rss->sin_local,
605 sizeof(rss->sin_local));
606 if (rc < 0)
607 return rc;
608
609 /* retrieve the address we actually bound to, in case we
610 * passed INADDR_ANY as IP address */
611 return getsockname(rss->bfd.fd, (struct sockaddr *)&rss->sin_local,
612 &alen);
Harald Welteead7a7b2009-07-28 00:01:58 +0200613}
614
615#define RTP_PORT_BASE 30000
Harald Welte805f6442009-07-28 18:25:29 +0200616static unsigned int next_udp_port = RTP_PORT_BASE;
Harald Welteead7a7b2009-07-28 00:01:58 +0200617
Harald Welte647db842013-02-03 12:06:58 +0100618/*! \brief bind a RTP socket to a specific local address
619 * \param[in] rs RTP socket to be bound
620 * \param[in] ip local IP address to which socket is to be bound
621 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200622int rtp_socket_bind(struct rtp_socket *rs, uint32_t ip)
Harald Welteead7a7b2009-07-28 00:01:58 +0200623{
Harald Welte805f6442009-07-28 18:25:29 +0200624 int rc = -EIO;
625 struct in_addr ia;
626
627 ia.s_addr = htonl(ip);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200628 DEBUGP(DLMUX, "rtp_socket_bind(rs=%p, IP=%s): ", rs,
Harald Welte805f6442009-07-28 18:25:29 +0200629 inet_ntoa(ia));
Harald Welteead7a7b2009-07-28 00:01:58 +0200630
631 /* try to bind to a consecutive pair of ports */
Harald Welte805f6442009-07-28 18:25:29 +0200632 for (next_udp_port = next_udp_port % 0xffff;
633 next_udp_port < 0xffff; next_udp_port += 2) {
Harald Welteead7a7b2009-07-28 00:01:58 +0200634 rc = rtp_sub_socket_bind(&rs->rtp, ip, next_udp_port);
635 if (rc != 0)
636 continue;
637
638 rc = rtp_sub_socket_bind(&rs->rtcp, ip, next_udp_port+1);
639 if (rc == 0)
640 break;
641 }
Harald Welte805f6442009-07-28 18:25:29 +0200642 if (rc < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200643 DEBUGPC(DLMUX, "failed\n");
Harald Welteead7a7b2009-07-28 00:01:58 +0200644 return rc;
Harald Welte805f6442009-07-28 18:25:29 +0200645 }
Harald Welteead7a7b2009-07-28 00:01:58 +0200646
Harald Welte805f6442009-07-28 18:25:29 +0200647 ia.s_addr = rs->rtp.sin_local.sin_addr.s_addr;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200648 DEBUGPC(DLMUX, "BOUND_IP=%s, BOUND_PORT=%u\n",
Harald Welte805f6442009-07-28 18:25:29 +0200649 inet_ntoa(ia), ntohs(rs->rtp.sin_local.sin_port));
Harald Welteead7a7b2009-07-28 00:01:58 +0200650 return ntohs(rs->rtp.sin_local.sin_port);
651}
652
653static int rtp_sub_socket_connect(struct rtp_sub_socket *rss,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200654 uint32_t ip, uint16_t port)
Harald Welteead7a7b2009-07-28 00:01:58 +0200655{
Harald Welte805f6442009-07-28 18:25:29 +0200656 int rc;
657 socklen_t alen = sizeof(rss->sin_local);
658
Harald Welteead7a7b2009-07-28 00:01:58 +0200659 rss->sin_remote.sin_family = AF_INET;
660 rss->sin_remote.sin_addr.s_addr = htonl(ip);
661 rss->sin_remote.sin_port = htons(port);
662
Harald Welte805f6442009-07-28 18:25:29 +0200663 rc = connect(rss->bfd.fd, (struct sockaddr *) &rss->sin_remote,
664 sizeof(rss->sin_remote));
665 if (rc < 0)
666 return rc;
667
668 return getsockname(rss->bfd.fd, (struct sockaddr *)&rss->sin_local,
669 &alen);
Harald Welteead7a7b2009-07-28 00:01:58 +0200670}
671
Harald Welte647db842013-02-03 12:06:58 +0100672/*! \brief 'connect' a RTP socket to a remote peer
673 * \param[in] rs RTP socket to be connected
674 * \param[in] ip remote IP address to which to connect
675 * \param[in] port remote UDP port number to which to connect
676 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200677int rtp_socket_connect(struct rtp_socket *rs, uint32_t ip, uint16_t port)
Harald Welteead7a7b2009-07-28 00:01:58 +0200678{
679 int rc;
Harald Welte805f6442009-07-28 18:25:29 +0200680 struct in_addr ia;
681
682 ia.s_addr = htonl(ip);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200683 DEBUGP(DLMUX, "rtp_socket_connect(rs=%p, ip=%s, port=%u)\n",
Harald Welte805f6442009-07-28 18:25:29 +0200684 rs, inet_ntoa(ia), port);
Harald Welteead7a7b2009-07-28 00:01:58 +0200685
686 rc = rtp_sub_socket_connect(&rs->rtp, ip, port);
687 if (rc < 0)
688 return rc;
689
690 return rtp_sub_socket_connect(&rs->rtcp, ip, port+1);
691}
692
Harald Welte647db842013-02-03 12:06:58 +0100693/*! \brief bind two RTP/RTCP sockets together in the proxy
694 * \param[in] this First RTP socket
695 * \param[in] other Second RTP socket
696 */
Harald Welteead7a7b2009-07-28 00:01:58 +0200697int rtp_socket_proxy(struct rtp_socket *this, struct rtp_socket *other)
698{
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200699 DEBUGP(DLMUX, "rtp_socket_proxy(this=%p, other=%p)\n",
Harald Welte805f6442009-07-28 18:25:29 +0200700 this, other);
701
Harald Welteead7a7b2009-07-28 00:01:58 +0200702 this->rx_action = RTP_PROXY;
703 this->proxy.other_sock = other;
704
705 other->rx_action = RTP_PROXY;
706 other->proxy.other_sock = this;
707
708 return 0;
709}
710
Harald Welte647db842013-02-03 12:06:58 +0100711/*! \brief bind RTP/RTCP socket to application, disabling proxy
712 * \param[in] this RTP socket
713 * \param[in] net gsm_network argument to trau_tx_to_mncc()
714 * \param[in] callref callref argument to trau_tx_to_mncc()
715 */
Harald Welte9fb1f102009-12-20 17:07:23 +0100716int rtp_socket_upstream(struct rtp_socket *this, struct gsm_network *net,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200717 uint32_t callref)
Harald Welteda7ab742009-12-19 22:23:05 +0100718{
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200719 DEBUGP(DLMUX, "rtp_socket_proxy(this=%p, callref=%u)\n",
Harald Welteda7ab742009-12-19 22:23:05 +0100720 this, callref);
721
722 if (callref) {
723 this->rx_action = RTP_RECV_UPSTREAM;
724 this->receive.net = net;
725 this->receive.callref = callref;
726 } else
727 this->rx_action = RTP_NONE;
728
729 return 0;
730}
731
Harald Welteead7a7b2009-07-28 00:01:58 +0200732static void free_tx_queue(struct rtp_sub_socket *rss)
733{
734 struct msgb *msg;
735
736 while ((msg = msgb_dequeue(&rss->tx_queue)))
737 msgb_free(msg);
738}
739
Harald Welte647db842013-02-03 12:06:58 +0100740/*! \brief Free/release a previously allocated RTP socket
741 * \param[in[] rs RTP/RTCP socket to be released
742 */
Harald Welteead7a7b2009-07-28 00:01:58 +0200743int rtp_socket_free(struct rtp_socket *rs)
744{
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200745 DEBUGP(DLMUX, "rtp_socket_free(rs=%p)\n", rs);
Harald Welteead7a7b2009-07-28 00:01:58 +0200746
747 /* make sure we don't leave references dangling to us */
748 if (rs->rx_action == RTP_PROXY &&
749 rs->proxy.other_sock)
750 rs->proxy.other_sock->proxy.other_sock = NULL;
751
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200752 osmo_fd_unregister(&rs->rtp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200753 close(rs->rtp.bfd.fd);
754 free_tx_queue(&rs->rtp);
755
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200756 osmo_fd_unregister(&rs->rtcp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200757 close(rs->rtcp.bfd.fd);
758 free_tx_queue(&rs->rtcp);
759
760 talloc_free(rs);
761
762 return 0;
763}