blob: 0074b4a04d59de3c2afcb18827b375baa675687e [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) {
154 if (payload_len < 0) {
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;
170 if (payload_len != 33) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200171 DEBUGPC(DLMUX, "received RTP full rate frame with "
Harald Welteda7ab742009-12-19 22:23:05 +0100172 "payload length != 32 (len = %d)\n",
173 payload_len);
174 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;
179 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
186 new_msg = msgb_alloc(sizeof(struct gsm_data_frame) + payload_len,
187 "GSM-DATA");
188 if (!new_msg)
189 return -ENOMEM;
190 frame = (struct gsm_data_frame *)(new_msg->data);
191 frame->msg_type = msg_type;
192 frame->callref = callref;
193 memcpy(frame->data, payload, payload_len);
194 msgb_put(new_msg, sizeof(struct gsm_data_frame) + payload_len);
195
196 *data = new_msg;
197 return 0;
198}
199
Harald Welte392736d2009-12-20 13:16:14 +0100200/* "to - from" */
201static void tv_difference(struct timeval *diff, const struct timeval *from,
202 const struct timeval *__to)
203{
204 struct timeval _to = *__to, *to = &_to;
205
206 if (to->tv_usec < from->tv_usec) {
207 to->tv_sec -= 1;
208 to->tv_usec += 1000000;
209 }
210
211 diff->tv_usec = to->tv_usec - from->tv_usec;
212 diff->tv_sec = to->tv_sec - from->tv_sec;
213}
214
Harald Welte647db842013-02-03 12:06:58 +0100215/*! \brief encode and send a rtp frame
216 * \param[in] rs RTP socket through which we shall send
217 * \param[in] frame GSM RTP frame to be sent
218 */
Harald Welteda7ab742009-12-19 22:23:05 +0100219int rtp_send_frame(struct rtp_socket *rs, struct gsm_data_frame *frame)
220{
221 struct rtp_sub_socket *rss = &rs->rtp;
222 struct msgb *msg;
223 struct rtp_hdr *rtph;
224 int payload_type;
225 int payload_len;
226 int duration; /* in samples */
227
228 if (rs->tx_action != RTP_SEND_DOWNSTREAM) {
229 /* initialize sequences */
230 rs->tx_action = RTP_SEND_DOWNSTREAM;
231 rs->transmit.ssrc = rand();
232 rs->transmit.sequence = random();
233 rs->transmit.timestamp = random();
234 }
235
236 switch (frame->msg_type) {
237 case GSM_TCHF_FRAME:
238 payload_type = RTP_PT_GSM_FULL;
239 payload_len = 33;
240 duration = 160;
241 break;
Harald Welteaca8f152009-12-19 23:06:41 +0100242 case GSM_TCHF_FRAME_EFR:
243 payload_type = RTP_PT_GSM_EFR;
244 payload_len = 31;
245 duration = 160;
246 break;
Harald Welteda7ab742009-12-19 22:23:05 +0100247 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200248 DEBUGPC(DLMUX, "unsupported message type %d\n",
Harald Welteda7ab742009-12-19 22:23:05 +0100249 frame->msg_type);
250 return -EINVAL;
251 }
252
Harald Welte392736d2009-12-20 13:16:14 +0100253 {
254 struct timeval tv, tv_diff;
255 long int usec_diff, frame_diff;
256
257 gettimeofday(&tv, NULL);
258 tv_difference(&tv_diff, &rs->transmit.last_tv, &tv);
259 rs->transmit.last_tv = tv;
260
261 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
262 frame_diff = (usec_diff / 20000);
263
264 if (abs(frame_diff) > 1) {
265 long int frame_diff_excess = frame_diff - 1;
266
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200267 LOGP(DLMUX, LOGL_NOTICE,
Harald Welte (local)9fcf6d72009-12-27 17:01:40 +0100268 "Correcting frame difference of %ld frames\n", frame_diff_excess);
Harald Welte392736d2009-12-20 13:16:14 +0100269 rs->transmit.sequence += frame_diff_excess;
270 rs->transmit.timestamp += frame_diff_excess * duration;
271 }
272 }
273
Harald Welteda7ab742009-12-19 22:23:05 +0100274 msg = msgb_alloc(sizeof(struct rtp_hdr) + payload_len, "RTP-GSM-FULL");
275 if (!msg)
276 return -ENOMEM;
277 rtph = (struct rtp_hdr *)msg->data;
278 rtph->version = RTP_VERSION;
279 rtph->padding = 0;
280 rtph->extension = 0;
281 rtph->csrc_count = 0;
282 rtph->marker = 0;
283 rtph->payload_type = payload_type;
284 rtph->sequence = htons(rs->transmit.sequence++);
285 rtph->timestamp = htonl(rs->transmit.timestamp);
286 rs->transmit.timestamp += duration;
287 rtph->ssrc = htonl(rs->transmit.ssrc);
288 memcpy(msg->data + sizeof(struct rtp_hdr), frame->data, payload_len);
289 msgb_put(msg, sizeof(struct rtp_hdr) + payload_len);
290 msgb_enqueue(&rss->tx_queue, msg);
291 rss->bfd.when |= BSC_FD_WRITE;
292
293 return 0;
294}
295
Holger Hans Peter Freyther89acf062009-07-29 06:46:26 +0200296/* iterate over all chunks in one RTCP message, look for CNAME IEs and
Harald Welte805f6442009-07-28 18:25:29 +0200297 * replace all of those with 'new_cname' */
298static int rtcp_sdes_cname_mangle(struct msgb *msg, struct rtcp_hdr *rh,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200299 uint16_t *rtcp_len, const char *new_cname)
Harald Welte805f6442009-07-28 18:25:29 +0200300{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200301 uint8_t *rtcp_end;
302 uint8_t *cur = (uint8_t *) rh;
303 uint8_t tag, len = 0;
Harald Welte805f6442009-07-28 18:25:29 +0200304
305 rtcp_end = cur + *rtcp_len;
306 /* move cur to end of RTP header */
Harald Welte198f3f52009-07-29 10:46:41 +0200307 cur += sizeof(*rh);
Harald Welte805f6442009-07-28 18:25:29 +0200308
309 /* iterate over Chunks */
310 while (cur+4 < rtcp_end) {
311 /* skip four bytes SSRC/CSRC */
312 cur += 4;
313
314 /* iterate over IE's inside the chunk */
315 while (cur+1 < rtcp_end) {
316 tag = *cur++;
317 if (tag == 0) {
318 /* end of chunk, skip additional zero */
319 while (*cur++ == 0) { }
320 break;
321 }
322 len = *cur++;
323
324 if (tag == RTCP_IE_CNAME) {
325 /* we've found the CNAME, lets mangle it */
326 if (len < strlen(new_cname)) {
327 /* we need to make more space */
328 int increase = strlen(new_cname) - len;
329
330 msgb_push(msg, increase);
331 memmove(cur+len+increase, cur+len,
332 rtcp_end - (cur+len));
333 /* FIXME: we have to respect RTCP
334 * padding/alignment rules! */
335 len += increase;
336 *(cur-1) += increase;
337 rtcp_end += increase;
338 *rtcp_len += increase;
339 }
340 /* copy new CNAME into message */
341 memcpy(cur, new_cname, strlen(new_cname));
342 /* FIXME: zero the padding in case new CNAME
343 * is smaller than old one !!! */
344 }
345 cur += len;
346 }
347 }
348
349 return 0;
350}
351
352static int rtcp_mangle(struct msgb *msg, struct rtp_socket *rs)
353{
354 struct rtp_sub_socket *rss = &rs->rtcp;
355 struct rtcp_hdr *rtph;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200356 uint16_t old_len;
Harald Welte805f6442009-07-28 18:25:29 +0200357 int rc;
358
359 if (!mangle_rtcp_cname)
360 return 0;
361
Harald Welteda7ab742009-12-19 22:23:05 +0100362 printf("RTCP\n");
Harald Welte805f6442009-07-28 18:25:29 +0200363 /* iterate over list of RTCP messages */
364 rtph = (struct rtcp_hdr *)msg->data;
Harald Welteda7ab742009-12-19 22:23:05 +0100365 while ((void *)rtph + sizeof(*rtph) <= (void *)msg->data + msg->len) {
Harald Welte805f6442009-07-28 18:25:29 +0200366 old_len = (ntohs(rtph->length) + 1) * 4;
Harald Welteda7ab742009-12-19 22:23:05 +0100367 if ((void *)rtph + old_len > (void *)msg->data + msg->len) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200368 DEBUGPC(DLMUX, "received RTCP packet too short for "
Harald Welteda7ab742009-12-19 22:23:05 +0100369 "length element\n");
370 return -EINVAL;
371 }
Harald Welte805f6442009-07-28 18:25:29 +0200372 if (rtph->type == RTCP_TYPE_SDES) {
373 char new_cname[255];
374 strncpy(new_cname, inet_ntoa(rss->sin_local.sin_addr),
375 sizeof(new_cname));
376 new_cname[sizeof(new_cname)-1] = '\0';
377 rc = rtcp_sdes_cname_mangle(msg, rtph, &old_len,
378 new_cname);
379 if (rc < 0)
380 return rc;
381 }
382 rtph = (void *)rtph + old_len;
383 }
384
385 return 0;
386}
387
Harald Welteead7a7b2009-07-28 00:01:58 +0200388/* read from incoming RTP/RTCP socket */
389static int rtp_socket_read(struct rtp_socket *rs, struct rtp_sub_socket *rss)
390{
391 int rc;
392 struct msgb *msg = msgb_alloc(RTP_ALLOC_SIZE, "RTP/RTCP");
Harald Welteda7ab742009-12-19 22:23:05 +0100393 struct msgb *new_msg;
Harald Welteead7a7b2009-07-28 00:01:58 +0200394 struct rtp_sub_socket *other_rss;
395
396 if (!msg)
397 return -ENOMEM;
398
399 rc = read(rss->bfd.fd, msg->data, RTP_ALLOC_SIZE);
400 if (rc <= 0) {
401 rss->bfd.when &= ~BSC_FD_READ;
402 return rc;
403 }
404
405 msgb_put(msg, rc);
406
407 switch (rs->rx_action) {
408 case RTP_PROXY:
Harald Welte805f6442009-07-28 18:25:29 +0200409 if (!rs->proxy.other_sock) {
410 rc = -EIO;
411 goto out_free;
412 }
Harald Welteead7a7b2009-07-28 00:01:58 +0200413 if (rss->bfd.priv_nr == RTP_PRIV_RTP)
414 other_rss = &rs->proxy.other_sock->rtp;
Harald Welte805f6442009-07-28 18:25:29 +0200415 else if (rss->bfd.priv_nr == RTP_PRIV_RTCP) {
Harald Welteead7a7b2009-07-28 00:01:58 +0200416 other_rss = &rs->proxy.other_sock->rtcp;
Harald Welte805f6442009-07-28 18:25:29 +0200417 /* modify RTCP SDES CNAME */
418 rc = rtcp_mangle(msg, rs);
419 if (rc < 0)
420 goto out_free;
421 } else {
422 rc = -EINVAL;
423 goto out_free;
Harald Welteead7a7b2009-07-28 00:01:58 +0200424 }
425 msgb_enqueue(&other_rss->tx_queue, msg);
426 other_rss->bfd.when |= BSC_FD_WRITE;
427 break;
Holger Hans Peter Freyther1ddb3562009-08-10 07:57:13 +0200428
429 case RTP_RECV_UPSTREAM:
Harald Welteda7ab742009-12-19 22:23:05 +0100430 if (!rs->receive.callref || !rs->receive.net) {
431 rc = -EIO;
432 goto out_free;
433 }
434 if (rss->bfd.priv_nr == RTP_PRIV_RTCP) {
435 if (!mangle_rtcp_cname) {
436 msgb_free(msg);
437 break;
438 }
439 /* modify RTCP SDES CNAME */
440 rc = rtcp_mangle(msg, rs);
441 if (rc < 0)
442 goto out_free;
443 msgb_enqueue(&rss->tx_queue, msg);
444 rss->bfd.when |= BSC_FD_WRITE;
445 break;
446 }
447 if (rss->bfd.priv_nr != RTP_PRIV_RTP) {
448 rc = -EINVAL;
449 goto out_free;
450 }
451 rc = rtp_decode(msg, rs->receive.callref, &new_msg);
452 if (rc < 0)
453 goto out_free;
454 msgb_free(msg);
Harald Welte31c00f72011-03-03 23:29:05 +0100455 trau_tx_to_mncc(rs->receive.net, new_msg);
Harald Welteda7ab742009-12-19 22:23:05 +0100456 break;
457
458 case RTP_NONE: /* if socket exists, but disabled by app */
459 msgb_free(msg);
Holger Hans Peter Freyther1ddb3562009-08-10 07:57:13 +0200460 break;
Harald Welteead7a7b2009-07-28 00:01:58 +0200461 }
462
Harald Welteda7ab742009-12-19 22:23:05 +0100463 return 0;
Harald Welte805f6442009-07-28 18:25:29 +0200464
465out_free:
466 msgb_free(msg);
467 return rc;
Harald Welteead7a7b2009-07-28 00:01:58 +0200468}
469
Harald Welte647db842013-02-03 12:06:58 +0100470/* \brief write from tx_queue to RTP/RTCP socket */
Harald Welteead7a7b2009-07-28 00:01:58 +0200471static int rtp_socket_write(struct rtp_socket *rs, struct rtp_sub_socket *rss)
472{
473 struct msgb *msg;
474 int written;
475
476 msg = msgb_dequeue(&rss->tx_queue);
477 if (!msg) {
478 rss->bfd.when &= ~BSC_FD_WRITE;
479 return 0;
480 }
481
482 written = write(rss->bfd.fd, msg->data, msg->len);
483 if (written < msg->len) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200484 LOGP(DLMIB, LOGL_ERROR, "short write");
Harald Welteead7a7b2009-07-28 00:01:58 +0200485 msgb_free(msg);
486 return -EIO;
487 }
488
489 msgb_free(msg);
490
491 return 0;
492}
493
494
Harald Welte647db842013-02-03 12:06:58 +0100495/*! \brief callback for the select.c:bfd_* layer */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200496static int rtp_bfd_cb(struct osmo_fd *bfd, unsigned int flags)
Harald Welteead7a7b2009-07-28 00:01:58 +0200497{
498 struct rtp_socket *rs = bfd->data;
499 struct rtp_sub_socket *rss;
500
501 switch (bfd->priv_nr) {
502 case RTP_PRIV_RTP:
503 rss = &rs->rtp;
504 break;
505 case RTP_PRIV_RTCP:
506 rss = &rs->rtcp;
507 break;
508 default:
509 return -EINVAL;
510 }
511
512 if (flags & BSC_FD_READ)
513 rtp_socket_read(rs, rss);
514
515 if (flags & BSC_FD_WRITE)
516 rtp_socket_write(rs, rss);
517
518 return 0;
519}
520
Harald Welte647db842013-02-03 12:06:58 +0100521/*! \brief initialize one rtp sub-socket */
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200522static void init_rss(struct rtp_sub_socket *rss,
Harald Welteead7a7b2009-07-28 00:01:58 +0200523 struct rtp_socket *rs, int fd, int priv_nr)
524{
525 /* initialize bfd */
526 rss->bfd.fd = fd;
527 rss->bfd.data = rs;
528 rss->bfd.priv_nr = priv_nr;
529 rss->bfd.cb = rtp_bfd_cb;
530}
531
Harald Welte647db842013-02-03 12:06:58 +0100532/*! \brief create a new RTP/RTCP socket and bind it */
Harald Welteead7a7b2009-07-28 00:01:58 +0200533struct rtp_socket *rtp_socket_create(void)
534{
535 int rc;
536 struct rtp_socket *rs;
537
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200538 DEBUGP(DLMUX, "rtp_socket_create(): ");
Harald Welte805f6442009-07-28 18:25:29 +0200539
Harald Welteead7a7b2009-07-28 00:01:58 +0200540 rs = talloc_zero(tall_bsc_ctx, struct rtp_socket);
541 if (!rs)
542 return NULL;
543
544 INIT_LLIST_HEAD(&rs->rtp.tx_queue);
545 INIT_LLIST_HEAD(&rs->rtcp.tx_queue);
546
547 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
548 if (rc < 0)
549 goto out_free;
550
551 init_rss(&rs->rtp, rs, rc, RTP_PRIV_RTP);
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200552 rc = osmo_fd_register(&rs->rtp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200553 if (rc < 0)
554 goto out_rtp_socket;
555
556 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
557 if (rc < 0)
558 goto out_rtp_bfd;
559
560 init_rss(&rs->rtcp, rs, rc, RTP_PRIV_RTCP);
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200561 rc = osmo_fd_register(&rs->rtcp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200562 if (rc < 0)
563 goto out_rtcp_socket;
564
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200565 DEBUGPC(DLMUX, "success\n");
Harald Welte805f6442009-07-28 18:25:29 +0200566
567 rc = rtp_socket_bind(rs, INADDR_ANY);
568 if (rc < 0)
569 goto out_rtcp_bfd;
570
Harald Welteead7a7b2009-07-28 00:01:58 +0200571 return rs;
572
Harald Welte805f6442009-07-28 18:25:29 +0200573out_rtcp_bfd:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200574 osmo_fd_unregister(&rs->rtcp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200575out_rtcp_socket:
576 close(rs->rtcp.bfd.fd);
577out_rtp_bfd:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200578 osmo_fd_unregister(&rs->rtp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200579out_rtp_socket:
580 close(rs->rtp.bfd.fd);
581out_free:
582 talloc_free(rs);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200583 DEBUGPC(DLMUX, "failed\n");
Harald Welteead7a7b2009-07-28 00:01:58 +0200584 return NULL;
585}
586
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200587static int rtp_sub_socket_bind(struct rtp_sub_socket *rss, uint32_t ip,
588 uint16_t port)
Harald Welteead7a7b2009-07-28 00:01:58 +0200589{
Harald Welte805f6442009-07-28 18:25:29 +0200590 int rc;
591 socklen_t alen = sizeof(rss->sin_local);
592
Harald Welteead7a7b2009-07-28 00:01:58 +0200593 rss->sin_local.sin_family = AF_INET;
594 rss->sin_local.sin_addr.s_addr = htonl(ip);
595 rss->sin_local.sin_port = htons(port);
596 rss->bfd.when |= BSC_FD_READ;
597
Harald Welte805f6442009-07-28 18:25:29 +0200598 rc = bind(rss->bfd.fd, (struct sockaddr *)&rss->sin_local,
599 sizeof(rss->sin_local));
600 if (rc < 0)
601 return rc;
602
603 /* retrieve the address we actually bound to, in case we
604 * passed INADDR_ANY as IP address */
605 return getsockname(rss->bfd.fd, (struct sockaddr *)&rss->sin_local,
606 &alen);
Harald Welteead7a7b2009-07-28 00:01:58 +0200607}
608
609#define RTP_PORT_BASE 30000
Harald Welte805f6442009-07-28 18:25:29 +0200610static unsigned int next_udp_port = RTP_PORT_BASE;
Harald Welteead7a7b2009-07-28 00:01:58 +0200611
Harald Welte647db842013-02-03 12:06:58 +0100612/*! \brief bind a RTP socket to a specific local address
613 * \param[in] rs RTP socket to be bound
614 * \param[in] ip local IP address to which socket is to be bound
615 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200616int rtp_socket_bind(struct rtp_socket *rs, uint32_t ip)
Harald Welteead7a7b2009-07-28 00:01:58 +0200617{
Harald Welte805f6442009-07-28 18:25:29 +0200618 int rc = -EIO;
619 struct in_addr ia;
620
621 ia.s_addr = htonl(ip);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200622 DEBUGP(DLMUX, "rtp_socket_bind(rs=%p, IP=%s): ", rs,
Harald Welte805f6442009-07-28 18:25:29 +0200623 inet_ntoa(ia));
Harald Welteead7a7b2009-07-28 00:01:58 +0200624
625 /* try to bind to a consecutive pair of ports */
Harald Welte805f6442009-07-28 18:25:29 +0200626 for (next_udp_port = next_udp_port % 0xffff;
627 next_udp_port < 0xffff; next_udp_port += 2) {
Harald Welteead7a7b2009-07-28 00:01:58 +0200628 rc = rtp_sub_socket_bind(&rs->rtp, ip, next_udp_port);
629 if (rc != 0)
630 continue;
631
632 rc = rtp_sub_socket_bind(&rs->rtcp, ip, next_udp_port+1);
633 if (rc == 0)
634 break;
635 }
Harald Welte805f6442009-07-28 18:25:29 +0200636 if (rc < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200637 DEBUGPC(DLMUX, "failed\n");
Harald Welteead7a7b2009-07-28 00:01:58 +0200638 return rc;
Harald Welte805f6442009-07-28 18:25:29 +0200639 }
Harald Welteead7a7b2009-07-28 00:01:58 +0200640
Harald Welte805f6442009-07-28 18:25:29 +0200641 ia.s_addr = rs->rtp.sin_local.sin_addr.s_addr;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200642 DEBUGPC(DLMUX, "BOUND_IP=%s, BOUND_PORT=%u\n",
Harald Welte805f6442009-07-28 18:25:29 +0200643 inet_ntoa(ia), ntohs(rs->rtp.sin_local.sin_port));
Harald Welteead7a7b2009-07-28 00:01:58 +0200644 return ntohs(rs->rtp.sin_local.sin_port);
645}
646
647static int rtp_sub_socket_connect(struct rtp_sub_socket *rss,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200648 uint32_t ip, uint16_t port)
Harald Welteead7a7b2009-07-28 00:01:58 +0200649{
Harald Welte805f6442009-07-28 18:25:29 +0200650 int rc;
651 socklen_t alen = sizeof(rss->sin_local);
652
Harald Welteead7a7b2009-07-28 00:01:58 +0200653 rss->sin_remote.sin_family = AF_INET;
654 rss->sin_remote.sin_addr.s_addr = htonl(ip);
655 rss->sin_remote.sin_port = htons(port);
656
Harald Welte805f6442009-07-28 18:25:29 +0200657 rc = connect(rss->bfd.fd, (struct sockaddr *) &rss->sin_remote,
658 sizeof(rss->sin_remote));
659 if (rc < 0)
660 return rc;
661
662 return getsockname(rss->bfd.fd, (struct sockaddr *)&rss->sin_local,
663 &alen);
Harald Welteead7a7b2009-07-28 00:01:58 +0200664}
665
Harald Welte647db842013-02-03 12:06:58 +0100666/*! \brief 'connect' a RTP socket to a remote peer
667 * \param[in] rs RTP socket to be connected
668 * \param[in] ip remote IP address to which to connect
669 * \param[in] port remote UDP port number to which to connect
670 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200671int rtp_socket_connect(struct rtp_socket *rs, uint32_t ip, uint16_t port)
Harald Welteead7a7b2009-07-28 00:01:58 +0200672{
673 int rc;
Harald Welte805f6442009-07-28 18:25:29 +0200674 struct in_addr ia;
675
676 ia.s_addr = htonl(ip);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200677 DEBUGP(DLMUX, "rtp_socket_connect(rs=%p, ip=%s, port=%u)\n",
Harald Welte805f6442009-07-28 18:25:29 +0200678 rs, inet_ntoa(ia), port);
Harald Welteead7a7b2009-07-28 00:01:58 +0200679
680 rc = rtp_sub_socket_connect(&rs->rtp, ip, port);
681 if (rc < 0)
682 return rc;
683
684 return rtp_sub_socket_connect(&rs->rtcp, ip, port+1);
685}
686
Harald Welte647db842013-02-03 12:06:58 +0100687/*! \brief bind two RTP/RTCP sockets together in the proxy
688 * \param[in] this First RTP socket
689 * \param[in] other Second RTP socket
690 */
Harald Welteead7a7b2009-07-28 00:01:58 +0200691int rtp_socket_proxy(struct rtp_socket *this, struct rtp_socket *other)
692{
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200693 DEBUGP(DLMUX, "rtp_socket_proxy(this=%p, other=%p)\n",
Harald Welte805f6442009-07-28 18:25:29 +0200694 this, other);
695
Harald Welteead7a7b2009-07-28 00:01:58 +0200696 this->rx_action = RTP_PROXY;
697 this->proxy.other_sock = other;
698
699 other->rx_action = RTP_PROXY;
700 other->proxy.other_sock = this;
701
702 return 0;
703}
704
Harald Welte647db842013-02-03 12:06:58 +0100705/*! \brief bind RTP/RTCP socket to application, disabling proxy
706 * \param[in] this RTP socket
707 * \param[in] net gsm_network argument to trau_tx_to_mncc()
708 * \param[in] callref callref argument to trau_tx_to_mncc()
709 */
Harald Welte9fb1f102009-12-20 17:07:23 +0100710int rtp_socket_upstream(struct rtp_socket *this, struct gsm_network *net,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200711 uint32_t callref)
Harald Welteda7ab742009-12-19 22:23:05 +0100712{
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200713 DEBUGP(DLMUX, "rtp_socket_proxy(this=%p, callref=%u)\n",
Harald Welteda7ab742009-12-19 22:23:05 +0100714 this, callref);
715
716 if (callref) {
717 this->rx_action = RTP_RECV_UPSTREAM;
718 this->receive.net = net;
719 this->receive.callref = callref;
720 } else
721 this->rx_action = RTP_NONE;
722
723 return 0;
724}
725
Harald Welteead7a7b2009-07-28 00:01:58 +0200726static void free_tx_queue(struct rtp_sub_socket *rss)
727{
728 struct msgb *msg;
729
730 while ((msg = msgb_dequeue(&rss->tx_queue)))
731 msgb_free(msg);
732}
733
Harald Welte647db842013-02-03 12:06:58 +0100734/*! \brief Free/release a previously allocated RTP socket
735 * \param[in[] rs RTP/RTCP socket to be released
736 */
Harald Welteead7a7b2009-07-28 00:01:58 +0200737int rtp_socket_free(struct rtp_socket *rs)
738{
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200739 DEBUGP(DLMUX, "rtp_socket_free(rs=%p)\n", rs);
Harald Welteead7a7b2009-07-28 00:01:58 +0200740
741 /* make sure we don't leave references dangling to us */
742 if (rs->rx_action == RTP_PROXY &&
743 rs->proxy.other_sock)
744 rs->proxy.other_sock->proxy.other_sock = NULL;
745
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200746 osmo_fd_unregister(&rs->rtp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200747 close(rs->rtp.bfd.fd);
748 free_tx_queue(&rs->rtp);
749
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200750 osmo_fd_unregister(&rs->rtcp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200751 close(rs->rtcp.bfd.fd);
752 free_tx_queue(&rs->rtcp);
753
754 talloc_free(rs);
755
756 return 0;
757}