blob: f7c5a4fad339e730d23e6b29eff205e5068ba276 [file] [log] [blame]
Harald Welteead7a7b2009-07-28 00:01:58 +02001/* RTP proxy handling for ip.access nanoBTS */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
4 * 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 Welteda7ab742009-12-19 22:23:05 +0100215/* encode and send a rtp frame */
216int rtp_send_frame(struct rtp_socket *rs, struct gsm_data_frame *frame)
217{
218 struct rtp_sub_socket *rss = &rs->rtp;
219 struct msgb *msg;
220 struct rtp_hdr *rtph;
221 int payload_type;
222 int payload_len;
223 int duration; /* in samples */
224
225 if (rs->tx_action != RTP_SEND_DOWNSTREAM) {
226 /* initialize sequences */
227 rs->tx_action = RTP_SEND_DOWNSTREAM;
228 rs->transmit.ssrc = rand();
229 rs->transmit.sequence = random();
230 rs->transmit.timestamp = random();
231 }
232
233 switch (frame->msg_type) {
234 case GSM_TCHF_FRAME:
235 payload_type = RTP_PT_GSM_FULL;
236 payload_len = 33;
237 duration = 160;
238 break;
Harald Welteaca8f152009-12-19 23:06:41 +0100239 case GSM_TCHF_FRAME_EFR:
240 payload_type = RTP_PT_GSM_EFR;
241 payload_len = 31;
242 duration = 160;
243 break;
Harald Welteda7ab742009-12-19 22:23:05 +0100244 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200245 DEBUGPC(DLMUX, "unsupported message type %d\n",
Harald Welteda7ab742009-12-19 22:23:05 +0100246 frame->msg_type);
247 return -EINVAL;
248 }
249
Harald Welte392736d2009-12-20 13:16:14 +0100250 {
251 struct timeval tv, tv_diff;
252 long int usec_diff, frame_diff;
253
254 gettimeofday(&tv, NULL);
255 tv_difference(&tv_diff, &rs->transmit.last_tv, &tv);
256 rs->transmit.last_tv = tv;
257
258 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
259 frame_diff = (usec_diff / 20000);
260
261 if (abs(frame_diff) > 1) {
262 long int frame_diff_excess = frame_diff - 1;
263
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200264 LOGP(DLMUX, LOGL_NOTICE,
Harald Welte (local)9fcf6d72009-12-27 17:01:40 +0100265 "Correcting frame difference of %ld frames\n", frame_diff_excess);
Harald Welte392736d2009-12-20 13:16:14 +0100266 rs->transmit.sequence += frame_diff_excess;
267 rs->transmit.timestamp += frame_diff_excess * duration;
268 }
269 }
270
Harald Welteda7ab742009-12-19 22:23:05 +0100271 msg = msgb_alloc(sizeof(struct rtp_hdr) + payload_len, "RTP-GSM-FULL");
272 if (!msg)
273 return -ENOMEM;
274 rtph = (struct rtp_hdr *)msg->data;
275 rtph->version = RTP_VERSION;
276 rtph->padding = 0;
277 rtph->extension = 0;
278 rtph->csrc_count = 0;
279 rtph->marker = 0;
280 rtph->payload_type = payload_type;
281 rtph->sequence = htons(rs->transmit.sequence++);
282 rtph->timestamp = htonl(rs->transmit.timestamp);
283 rs->transmit.timestamp += duration;
284 rtph->ssrc = htonl(rs->transmit.ssrc);
285 memcpy(msg->data + sizeof(struct rtp_hdr), frame->data, payload_len);
286 msgb_put(msg, sizeof(struct rtp_hdr) + payload_len);
287 msgb_enqueue(&rss->tx_queue, msg);
288 rss->bfd.when |= BSC_FD_WRITE;
289
290 return 0;
291}
292
Holger Hans Peter Freyther89acf062009-07-29 06:46:26 +0200293/* iterate over all chunks in one RTCP message, look for CNAME IEs and
Harald Welte805f6442009-07-28 18:25:29 +0200294 * replace all of those with 'new_cname' */
295static int rtcp_sdes_cname_mangle(struct msgb *msg, struct rtcp_hdr *rh,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200296 uint16_t *rtcp_len, const char *new_cname)
Harald Welte805f6442009-07-28 18:25:29 +0200297{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200298 uint8_t *rtcp_end;
299 uint8_t *cur = (uint8_t *) rh;
300 uint8_t tag, len = 0;
Harald Welte805f6442009-07-28 18:25:29 +0200301
302 rtcp_end = cur + *rtcp_len;
303 /* move cur to end of RTP header */
Harald Welte198f3f52009-07-29 10:46:41 +0200304 cur += sizeof(*rh);
Harald Welte805f6442009-07-28 18:25:29 +0200305
306 /* iterate over Chunks */
307 while (cur+4 < rtcp_end) {
308 /* skip four bytes SSRC/CSRC */
309 cur += 4;
310
311 /* iterate over IE's inside the chunk */
312 while (cur+1 < rtcp_end) {
313 tag = *cur++;
314 if (tag == 0) {
315 /* end of chunk, skip additional zero */
316 while (*cur++ == 0) { }
317 break;
318 }
319 len = *cur++;
320
321 if (tag == RTCP_IE_CNAME) {
322 /* we've found the CNAME, lets mangle it */
323 if (len < strlen(new_cname)) {
324 /* we need to make more space */
325 int increase = strlen(new_cname) - len;
326
327 msgb_push(msg, increase);
328 memmove(cur+len+increase, cur+len,
329 rtcp_end - (cur+len));
330 /* FIXME: we have to respect RTCP
331 * padding/alignment rules! */
332 len += increase;
333 *(cur-1) += increase;
334 rtcp_end += increase;
335 *rtcp_len += increase;
336 }
337 /* copy new CNAME into message */
338 memcpy(cur, new_cname, strlen(new_cname));
339 /* FIXME: zero the padding in case new CNAME
340 * is smaller than old one !!! */
341 }
342 cur += len;
343 }
344 }
345
346 return 0;
347}
348
349static int rtcp_mangle(struct msgb *msg, struct rtp_socket *rs)
350{
351 struct rtp_sub_socket *rss = &rs->rtcp;
352 struct rtcp_hdr *rtph;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200353 uint16_t old_len;
Harald Welte805f6442009-07-28 18:25:29 +0200354 int rc;
355
356 if (!mangle_rtcp_cname)
357 return 0;
358
Harald Welteda7ab742009-12-19 22:23:05 +0100359 printf("RTCP\n");
Harald Welte805f6442009-07-28 18:25:29 +0200360 /* iterate over list of RTCP messages */
361 rtph = (struct rtcp_hdr *)msg->data;
Harald Welteda7ab742009-12-19 22:23:05 +0100362 while ((void *)rtph + sizeof(*rtph) <= (void *)msg->data + msg->len) {
Harald Welte805f6442009-07-28 18:25:29 +0200363 old_len = (ntohs(rtph->length) + 1) * 4;
Harald Welteda7ab742009-12-19 22:23:05 +0100364 if ((void *)rtph + old_len > (void *)msg->data + msg->len) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200365 DEBUGPC(DLMUX, "received RTCP packet too short for "
Harald Welteda7ab742009-12-19 22:23:05 +0100366 "length element\n");
367 return -EINVAL;
368 }
Harald Welte805f6442009-07-28 18:25:29 +0200369 if (rtph->type == RTCP_TYPE_SDES) {
370 char new_cname[255];
371 strncpy(new_cname, inet_ntoa(rss->sin_local.sin_addr),
372 sizeof(new_cname));
373 new_cname[sizeof(new_cname)-1] = '\0';
374 rc = rtcp_sdes_cname_mangle(msg, rtph, &old_len,
375 new_cname);
376 if (rc < 0)
377 return rc;
378 }
379 rtph = (void *)rtph + old_len;
380 }
381
382 return 0;
383}
384
Harald Welteead7a7b2009-07-28 00:01:58 +0200385/* read from incoming RTP/RTCP socket */
386static int rtp_socket_read(struct rtp_socket *rs, struct rtp_sub_socket *rss)
387{
388 int rc;
389 struct msgb *msg = msgb_alloc(RTP_ALLOC_SIZE, "RTP/RTCP");
Harald Welteda7ab742009-12-19 22:23:05 +0100390 struct msgb *new_msg;
Harald Welteead7a7b2009-07-28 00:01:58 +0200391 struct rtp_sub_socket *other_rss;
392
393 if (!msg)
394 return -ENOMEM;
395
396 rc = read(rss->bfd.fd, msg->data, RTP_ALLOC_SIZE);
397 if (rc <= 0) {
398 rss->bfd.when &= ~BSC_FD_READ;
399 return rc;
400 }
401
402 msgb_put(msg, rc);
403
404 switch (rs->rx_action) {
405 case RTP_PROXY:
Harald Welte805f6442009-07-28 18:25:29 +0200406 if (!rs->proxy.other_sock) {
407 rc = -EIO;
408 goto out_free;
409 }
Harald Welteead7a7b2009-07-28 00:01:58 +0200410 if (rss->bfd.priv_nr == RTP_PRIV_RTP)
411 other_rss = &rs->proxy.other_sock->rtp;
Harald Welte805f6442009-07-28 18:25:29 +0200412 else if (rss->bfd.priv_nr == RTP_PRIV_RTCP) {
Harald Welteead7a7b2009-07-28 00:01:58 +0200413 other_rss = &rs->proxy.other_sock->rtcp;
Harald Welte805f6442009-07-28 18:25:29 +0200414 /* modify RTCP SDES CNAME */
415 rc = rtcp_mangle(msg, rs);
416 if (rc < 0)
417 goto out_free;
418 } else {
419 rc = -EINVAL;
420 goto out_free;
Harald Welteead7a7b2009-07-28 00:01:58 +0200421 }
422 msgb_enqueue(&other_rss->tx_queue, msg);
423 other_rss->bfd.when |= BSC_FD_WRITE;
424 break;
Holger Hans Peter Freyther1ddb3562009-08-10 07:57:13 +0200425
426 case RTP_RECV_UPSTREAM:
Harald Welteda7ab742009-12-19 22:23:05 +0100427 if (!rs->receive.callref || !rs->receive.net) {
428 rc = -EIO;
429 goto out_free;
430 }
431 if (rss->bfd.priv_nr == RTP_PRIV_RTCP) {
432 if (!mangle_rtcp_cname) {
433 msgb_free(msg);
434 break;
435 }
436 /* modify RTCP SDES CNAME */
437 rc = rtcp_mangle(msg, rs);
438 if (rc < 0)
439 goto out_free;
440 msgb_enqueue(&rss->tx_queue, msg);
441 rss->bfd.when |= BSC_FD_WRITE;
442 break;
443 }
444 if (rss->bfd.priv_nr != RTP_PRIV_RTP) {
445 rc = -EINVAL;
446 goto out_free;
447 }
448 rc = rtp_decode(msg, rs->receive.callref, &new_msg);
449 if (rc < 0)
450 goto out_free;
451 msgb_free(msg);
Harald Welte31c00f72011-03-03 23:29:05 +0100452 trau_tx_to_mncc(rs->receive.net, new_msg);
Harald Welteda7ab742009-12-19 22:23:05 +0100453 break;
454
455 case RTP_NONE: /* if socket exists, but disabled by app */
456 msgb_free(msg);
Holger Hans Peter Freyther1ddb3562009-08-10 07:57:13 +0200457 break;
Harald Welteead7a7b2009-07-28 00:01:58 +0200458 }
459
Harald Welteda7ab742009-12-19 22:23:05 +0100460 return 0;
Harald Welte805f6442009-07-28 18:25:29 +0200461
462out_free:
463 msgb_free(msg);
464 return rc;
Harald Welteead7a7b2009-07-28 00:01:58 +0200465}
466
467/* write from tx_queue to RTP/RTCP socket */
468static int rtp_socket_write(struct rtp_socket *rs, struct rtp_sub_socket *rss)
469{
470 struct msgb *msg;
471 int written;
472
473 msg = msgb_dequeue(&rss->tx_queue);
474 if (!msg) {
475 rss->bfd.when &= ~BSC_FD_WRITE;
476 return 0;
477 }
478
479 written = write(rss->bfd.fd, msg->data, msg->len);
480 if (written < msg->len) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200481 LOGP(DLMIB, LOGL_ERROR, "short write");
Harald Welteead7a7b2009-07-28 00:01:58 +0200482 msgb_free(msg);
483 return -EIO;
484 }
485
486 msgb_free(msg);
487
488 return 0;
489}
490
491
492/* callback for the select.c:bfd_* layer */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200493static int rtp_bfd_cb(struct osmo_fd *bfd, unsigned int flags)
Harald Welteead7a7b2009-07-28 00:01:58 +0200494{
495 struct rtp_socket *rs = bfd->data;
496 struct rtp_sub_socket *rss;
497
498 switch (bfd->priv_nr) {
499 case RTP_PRIV_RTP:
500 rss = &rs->rtp;
501 break;
502 case RTP_PRIV_RTCP:
503 rss = &rs->rtcp;
504 break;
505 default:
506 return -EINVAL;
507 }
508
509 if (flags & BSC_FD_READ)
510 rtp_socket_read(rs, rss);
511
512 if (flags & BSC_FD_WRITE)
513 rtp_socket_write(rs, rss);
514
515 return 0;
516}
517
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200518static void init_rss(struct rtp_sub_socket *rss,
Harald Welteead7a7b2009-07-28 00:01:58 +0200519 struct rtp_socket *rs, int fd, int priv_nr)
520{
521 /* initialize bfd */
522 rss->bfd.fd = fd;
523 rss->bfd.data = rs;
524 rss->bfd.priv_nr = priv_nr;
525 rss->bfd.cb = rtp_bfd_cb;
526}
527
528struct rtp_socket *rtp_socket_create(void)
529{
530 int rc;
531 struct rtp_socket *rs;
532
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200533 DEBUGP(DLMUX, "rtp_socket_create(): ");
Harald Welte805f6442009-07-28 18:25:29 +0200534
Harald Welteead7a7b2009-07-28 00:01:58 +0200535 rs = talloc_zero(tall_bsc_ctx, struct rtp_socket);
536 if (!rs)
537 return NULL;
538
539 INIT_LLIST_HEAD(&rs->rtp.tx_queue);
540 INIT_LLIST_HEAD(&rs->rtcp.tx_queue);
541
542 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
543 if (rc < 0)
544 goto out_free;
545
546 init_rss(&rs->rtp, rs, rc, RTP_PRIV_RTP);
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200547 rc = osmo_fd_register(&rs->rtp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200548 if (rc < 0)
549 goto out_rtp_socket;
550
551 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
552 if (rc < 0)
553 goto out_rtp_bfd;
554
555 init_rss(&rs->rtcp, rs, rc, RTP_PRIV_RTCP);
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200556 rc = osmo_fd_register(&rs->rtcp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200557 if (rc < 0)
558 goto out_rtcp_socket;
559
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200560 DEBUGPC(DLMUX, "success\n");
Harald Welte805f6442009-07-28 18:25:29 +0200561
562 rc = rtp_socket_bind(rs, INADDR_ANY);
563 if (rc < 0)
564 goto out_rtcp_bfd;
565
Harald Welteead7a7b2009-07-28 00:01:58 +0200566 return rs;
567
Harald Welte805f6442009-07-28 18:25:29 +0200568out_rtcp_bfd:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200569 osmo_fd_unregister(&rs->rtcp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200570out_rtcp_socket:
571 close(rs->rtcp.bfd.fd);
572out_rtp_bfd:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200573 osmo_fd_unregister(&rs->rtp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200574out_rtp_socket:
575 close(rs->rtp.bfd.fd);
576out_free:
577 talloc_free(rs);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200578 DEBUGPC(DLMUX, "failed\n");
Harald Welteead7a7b2009-07-28 00:01:58 +0200579 return NULL;
580}
581
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200582static int rtp_sub_socket_bind(struct rtp_sub_socket *rss, uint32_t ip,
583 uint16_t port)
Harald Welteead7a7b2009-07-28 00:01:58 +0200584{
Harald Welte805f6442009-07-28 18:25:29 +0200585 int rc;
586 socklen_t alen = sizeof(rss->sin_local);
587
Harald Welteead7a7b2009-07-28 00:01:58 +0200588 rss->sin_local.sin_family = AF_INET;
589 rss->sin_local.sin_addr.s_addr = htonl(ip);
590 rss->sin_local.sin_port = htons(port);
591 rss->bfd.when |= BSC_FD_READ;
592
Harald Welte805f6442009-07-28 18:25:29 +0200593 rc = bind(rss->bfd.fd, (struct sockaddr *)&rss->sin_local,
594 sizeof(rss->sin_local));
595 if (rc < 0)
596 return rc;
597
598 /* retrieve the address we actually bound to, in case we
599 * passed INADDR_ANY as IP address */
600 return getsockname(rss->bfd.fd, (struct sockaddr *)&rss->sin_local,
601 &alen);
Harald Welteead7a7b2009-07-28 00:01:58 +0200602}
603
604#define RTP_PORT_BASE 30000
Harald Welte805f6442009-07-28 18:25:29 +0200605static unsigned int next_udp_port = RTP_PORT_BASE;
Harald Welteead7a7b2009-07-28 00:01:58 +0200606
607/* bind a RTP socket to a local address */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200608int rtp_socket_bind(struct rtp_socket *rs, uint32_t ip)
Harald Welteead7a7b2009-07-28 00:01:58 +0200609{
Harald Welte805f6442009-07-28 18:25:29 +0200610 int rc = -EIO;
611 struct in_addr ia;
612
613 ia.s_addr = htonl(ip);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200614 DEBUGP(DLMUX, "rtp_socket_bind(rs=%p, IP=%s): ", rs,
Harald Welte805f6442009-07-28 18:25:29 +0200615 inet_ntoa(ia));
Harald Welteead7a7b2009-07-28 00:01:58 +0200616
617 /* try to bind to a consecutive pair of ports */
Harald Welte805f6442009-07-28 18:25:29 +0200618 for (next_udp_port = next_udp_port % 0xffff;
619 next_udp_port < 0xffff; next_udp_port += 2) {
Harald Welteead7a7b2009-07-28 00:01:58 +0200620 rc = rtp_sub_socket_bind(&rs->rtp, ip, next_udp_port);
621 if (rc != 0)
622 continue;
623
624 rc = rtp_sub_socket_bind(&rs->rtcp, ip, next_udp_port+1);
625 if (rc == 0)
626 break;
627 }
Harald Welte805f6442009-07-28 18:25:29 +0200628 if (rc < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200629 DEBUGPC(DLMUX, "failed\n");
Harald Welteead7a7b2009-07-28 00:01:58 +0200630 return rc;
Harald Welte805f6442009-07-28 18:25:29 +0200631 }
Harald Welteead7a7b2009-07-28 00:01:58 +0200632
Harald Welte805f6442009-07-28 18:25:29 +0200633 ia.s_addr = rs->rtp.sin_local.sin_addr.s_addr;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200634 DEBUGPC(DLMUX, "BOUND_IP=%s, BOUND_PORT=%u\n",
Harald Welte805f6442009-07-28 18:25:29 +0200635 inet_ntoa(ia), ntohs(rs->rtp.sin_local.sin_port));
Harald Welteead7a7b2009-07-28 00:01:58 +0200636 return ntohs(rs->rtp.sin_local.sin_port);
637}
638
639static int rtp_sub_socket_connect(struct rtp_sub_socket *rss,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200640 uint32_t ip, uint16_t port)
Harald Welteead7a7b2009-07-28 00:01:58 +0200641{
Harald Welte805f6442009-07-28 18:25:29 +0200642 int rc;
643 socklen_t alen = sizeof(rss->sin_local);
644
Harald Welteead7a7b2009-07-28 00:01:58 +0200645 rss->sin_remote.sin_family = AF_INET;
646 rss->sin_remote.sin_addr.s_addr = htonl(ip);
647 rss->sin_remote.sin_port = htons(port);
648
Harald Welte805f6442009-07-28 18:25:29 +0200649 rc = connect(rss->bfd.fd, (struct sockaddr *) &rss->sin_remote,
650 sizeof(rss->sin_remote));
651 if (rc < 0)
652 return rc;
653
654 return getsockname(rss->bfd.fd, (struct sockaddr *)&rss->sin_local,
655 &alen);
Harald Welteead7a7b2009-07-28 00:01:58 +0200656}
657
658/* 'connect' a RTP socket to a remote peer */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200659int rtp_socket_connect(struct rtp_socket *rs, uint32_t ip, uint16_t port)
Harald Welteead7a7b2009-07-28 00:01:58 +0200660{
661 int rc;
Harald Welte805f6442009-07-28 18:25:29 +0200662 struct in_addr ia;
663
664 ia.s_addr = htonl(ip);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200665 DEBUGP(DLMUX, "rtp_socket_connect(rs=%p, ip=%s, port=%u)\n",
Harald Welte805f6442009-07-28 18:25:29 +0200666 rs, inet_ntoa(ia), port);
Harald Welteead7a7b2009-07-28 00:01:58 +0200667
668 rc = rtp_sub_socket_connect(&rs->rtp, ip, port);
669 if (rc < 0)
670 return rc;
671
672 return rtp_sub_socket_connect(&rs->rtcp, ip, port+1);
673}
674
675/* bind two RTP/RTCP sockets together */
676int rtp_socket_proxy(struct rtp_socket *this, struct rtp_socket *other)
677{
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200678 DEBUGP(DLMUX, "rtp_socket_proxy(this=%p, other=%p)\n",
Harald Welte805f6442009-07-28 18:25:29 +0200679 this, other);
680
Harald Welteead7a7b2009-07-28 00:01:58 +0200681 this->rx_action = RTP_PROXY;
682 this->proxy.other_sock = other;
683
684 other->rx_action = RTP_PROXY;
685 other->proxy.other_sock = this;
686
687 return 0;
688}
689
Harald Welteda7ab742009-12-19 22:23:05 +0100690/* bind RTP/RTCP socket to application */
Harald Welte9fb1f102009-12-20 17:07:23 +0100691int rtp_socket_upstream(struct rtp_socket *this, struct gsm_network *net,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200692 uint32_t callref)
Harald Welteda7ab742009-12-19 22:23:05 +0100693{
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200694 DEBUGP(DLMUX, "rtp_socket_proxy(this=%p, callref=%u)\n",
Harald Welteda7ab742009-12-19 22:23:05 +0100695 this, callref);
696
697 if (callref) {
698 this->rx_action = RTP_RECV_UPSTREAM;
699 this->receive.net = net;
700 this->receive.callref = callref;
701 } else
702 this->rx_action = RTP_NONE;
703
704 return 0;
705}
706
Harald Welteead7a7b2009-07-28 00:01:58 +0200707static void free_tx_queue(struct rtp_sub_socket *rss)
708{
709 struct msgb *msg;
710
711 while ((msg = msgb_dequeue(&rss->tx_queue)))
712 msgb_free(msg);
713}
714
715int rtp_socket_free(struct rtp_socket *rs)
716{
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200717 DEBUGP(DLMUX, "rtp_socket_free(rs=%p)\n", rs);
Harald Welteead7a7b2009-07-28 00:01:58 +0200718
719 /* make sure we don't leave references dangling to us */
720 if (rs->rx_action == RTP_PROXY &&
721 rs->proxy.other_sock)
722 rs->proxy.other_sock->proxy.other_sock = NULL;
723
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200724 osmo_fd_unregister(&rs->rtp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200725 close(rs->rtp.bfd.fd);
726 free_tx_queue(&rs->rtp);
727
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200728 osmo_fd_unregister(&rs->rtcp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200729 close(rs->rtcp.bfd.fd);
730 free_tx_queue(&rs->rtcp);
731
732 talloc_free(rs);
733
734 return 0;
735}