blob: 15673234b2d170d67f5f4f833aa6c076c619c208 [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 Weltea87f8f92014-05-17 13:43:01 +0200108/* 33 for FR, all other codecs have smaller size */
109#define MAX_RTP_PAYLOAD_LEN 33
110
Harald Welteda7ab742009-12-19 22:23:05 +0100111/* decode an rtp frame and create a new buffer with payload */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200112static int rtp_decode(struct msgb *msg, uint32_t callref, struct msgb **data)
Harald Welteda7ab742009-12-19 22:23:05 +0100113{
114 struct msgb *new_msg;
115 struct gsm_data_frame *frame;
116 struct rtp_hdr *rtph = (struct rtp_hdr *)msg->data;
117 struct rtp_x_hdr *rtpxh;
Harald Weltea87f8f92014-05-17 13:43:01 +0200118 uint8_t *payload, *payload_out;
Harald Welteda7ab742009-12-19 22:23:05 +0100119 int payload_len;
120 int msg_type;
121 int x_len;
122
123 if (msg->len < 12) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200124 DEBUGPC(DLMUX, "received RTP frame too short (len = %d)\n",
Harald Welteda7ab742009-12-19 22:23:05 +0100125 msg->len);
126 return -EINVAL;
127 }
128 if (rtph->version != RTP_VERSION) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200129 DEBUGPC(DLMUX, "received RTP version %d not supported.\n",
Harald Welteda7ab742009-12-19 22:23:05 +0100130 rtph->version);
131 return -EINVAL;
132 }
133 payload = msg->data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
134 payload_len = msg->len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
135 if (payload_len < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200136 DEBUGPC(DLMUX, "received RTP frame too short (len = %d, "
Harald Welteda7ab742009-12-19 22:23:05 +0100137 "csrc count = %d)\n", msg->len, rtph->csrc_count);
138 return -EINVAL;
139 }
140 if (rtph->extension) {
141 if (payload_len < sizeof(struct rtp_x_hdr)) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200142 DEBUGPC(DLMUX, "received RTP frame too short for "
Harald Welteda7ab742009-12-19 22:23:05 +0100143 "extension header\n");
144 return -EINVAL;
145 }
146 rtpxh = (struct rtp_x_hdr *)payload;
147 x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
148 payload += x_len;
149 payload_len -= x_len;
150 if (payload_len < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200151 DEBUGPC(DLMUX, "received RTP frame too short, "
Harald Welteda7ab742009-12-19 22:23:05 +0100152 "extension header exceeds frame length\n");
153 return -EINVAL;
154 }
155 }
156 if (rtph->padding) {
Jacob Erlbeckd9e40392013-11-21 19:05:42 +0100157 if (payload_len < 1) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200158 DEBUGPC(DLMUX, "received RTP frame too short for "
Harald Welteda7ab742009-12-19 22:23:05 +0100159 "padding length\n");
160 return -EINVAL;
161 }
162 payload_len -= payload[payload_len - 1];
163 if (payload_len < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200164 DEBUGPC(DLMUX, "received RTP frame with padding "
Harald Welteda7ab742009-12-19 22:23:05 +0100165 "greater than payload\n");
166 return -EINVAL;
167 }
168 }
169
170 switch (rtph->payload_type) {
171 case RTP_PT_GSM_FULL:
172 msg_type = GSM_TCHF_FRAME;
Andreas Eversberg88012b62014-01-22 10:05:51 +0100173 if (payload_len != RTP_LEN_GSM_FULL) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200174 DEBUGPC(DLMUX, "received RTP full rate frame with "
Andreas Eversberg88012b62014-01-22 10:05:51 +0100175 "payload length != %d (len = %d)\n",
176 RTP_LEN_GSM_FULL, payload_len);
Harald Welteda7ab742009-12-19 22:23:05 +0100177 return -EINVAL;
178 }
179 break;
Harald Welteaca8f152009-12-19 23:06:41 +0100180 case RTP_PT_GSM_EFR:
181 msg_type = GSM_TCHF_FRAME_EFR;
Andreas Eversberg88012b62014-01-22 10:05:51 +0100182 if (payload_len != RTP_LEN_GSM_EFR) {
183 DEBUGPC(DLMUX, "received RTP extended full rate frame "
184 "with payload length != %d (len = %d)\n",
185 RTP_LEN_GSM_EFR, payload_len);
186 return -EINVAL;
187 }
Harald Welteaca8f152009-12-19 23:06:41 +0100188 break;
Andreas Eversberg63bfdd82014-01-17 19:06:38 +0100189 case RTP_PT_GSM_HALF:
190 msg_type = GSM_TCHH_FRAME;
191 if (payload_len != RTP_LEN_GSM_HALF) {
192 DEBUGPC(DLMUX, "received RTP half rate frame with "
193 "payload length != %d (len = %d)\n",
194 RTP_LEN_GSM_HALF, payload_len);
195 return -EINVAL;
196 }
197 break;
Andreas Eversbergd8967f72012-03-08 14:39:19 +0100198 case RTP_PT_AMR:
Harald Welteec757982014-05-18 18:32:05 +0200199 msg_type = GSM_TCH_FRAME_AMR;
Andreas Eversbergd8967f72012-03-08 14:39:19 +0100200 break;
Harald Welteda7ab742009-12-19 22:23:05 +0100201 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200202 DEBUGPC(DLMUX, "received RTP frame with unknown payload "
Harald Welteda7ab742009-12-19 22:23:05 +0100203 "type %d\n", rtph->payload_type);
204 return -EINVAL;
205 }
206
Harald Weltea87f8f92014-05-17 13:43:01 +0200207 if (payload_len > MAX_RTP_PAYLOAD_LEN) {
Andreas Eversbergd8967f72012-03-08 14:39:19 +0100208 DEBUGPC(DLMUX, "RTP payload too large (%d octets)\n",
209 payload_len);
210 return -EINVAL;
211 }
212
Harald Weltea87f8f92014-05-17 13:43:01 +0200213 /* always allocate for the maximum possible size to avoid
214 * fragmentation */
215 new_msg = msgb_alloc(sizeof(struct gsm_data_frame) +
216 MAX_RTP_PAYLOAD_LEN, "GSM-DATA (TCH)");
217
Harald Welteda7ab742009-12-19 22:23:05 +0100218 if (!new_msg)
219 return -ENOMEM;
Harald Weltea87f8f92014-05-17 13:43:01 +0200220 frame = msgb_put(new_msg, sizeof(struct gsm_data_frame));
Harald Welteda7ab742009-12-19 22:23:05 +0100221 frame->msg_type = msg_type;
222 frame->callref = callref;
Andreas Eversbergd8967f72012-03-08 14:39:19 +0100223 if (rtph->payload_type == RTP_PT_AMR) {
Harald Weltea87f8f92014-05-17 13:43:01 +0200224 /* for FR/HR/EFR the length is implicit. In AMR, we
225 * need to make it explicit by using the first byte of
226 * the data[] buffer as length byte */
227 uint8_t *data0 = msgb_put(new_msg, 1);
228 *data0 = payload_len;
Andreas Eversbergd8967f72012-03-08 14:39:19 +0100229 }
Harald Weltea87f8f92014-05-17 13:43:01 +0200230 payload_out = msgb_put(new_msg, payload_len);
231 memcpy(payload_out, payload, payload_len);
Harald Welteda7ab742009-12-19 22:23:05 +0100232
233 *data = new_msg;
234 return 0;
235}
236
Harald Welte392736d2009-12-20 13:16:14 +0100237/* "to - from" */
238static void tv_difference(struct timeval *diff, const struct timeval *from,
239 const struct timeval *__to)
240{
241 struct timeval _to = *__to, *to = &_to;
242
243 if (to->tv_usec < from->tv_usec) {
244 to->tv_sec -= 1;
245 to->tv_usec += 1000000;
246 }
247
248 diff->tv_usec = to->tv_usec - from->tv_usec;
249 diff->tv_sec = to->tv_sec - from->tv_sec;
250}
251
Harald Welte647db842013-02-03 12:06:58 +0100252/*! \brief encode and send a rtp frame
253 * \param[in] rs RTP socket through which we shall send
254 * \param[in] frame GSM RTP frame to be sent
255 */
Harald Welteda7ab742009-12-19 22:23:05 +0100256int rtp_send_frame(struct rtp_socket *rs, struct gsm_data_frame *frame)
257{
258 struct rtp_sub_socket *rss = &rs->rtp;
259 struct msgb *msg;
260 struct rtp_hdr *rtph;
Harald Weltea87f8f92014-05-17 13:43:01 +0200261 uint8_t *payload;
Harald Welteda7ab742009-12-19 22:23:05 +0100262 int payload_type;
263 int payload_len;
264 int duration; /* in samples */
265
266 if (rs->tx_action != RTP_SEND_DOWNSTREAM) {
267 /* initialize sequences */
268 rs->tx_action = RTP_SEND_DOWNSTREAM;
269 rs->transmit.ssrc = rand();
270 rs->transmit.sequence = random();
271 rs->transmit.timestamp = random();
272 }
273
274 switch (frame->msg_type) {
275 case GSM_TCHF_FRAME:
276 payload_type = RTP_PT_GSM_FULL;
Andreas Eversberg88012b62014-01-22 10:05:51 +0100277 payload_len = RTP_LEN_GSM_FULL;
278 duration = RTP_GSM_DURATION;
Harald Welteda7ab742009-12-19 22:23:05 +0100279 break;
Harald Welteaca8f152009-12-19 23:06:41 +0100280 case GSM_TCHF_FRAME_EFR:
281 payload_type = RTP_PT_GSM_EFR;
Andreas Eversberg88012b62014-01-22 10:05:51 +0100282 payload_len = RTP_LEN_GSM_EFR;
283 duration = RTP_GSM_DURATION;
Harald Welteaca8f152009-12-19 23:06:41 +0100284 break;
Andreas Eversberg63bfdd82014-01-17 19:06:38 +0100285 case GSM_TCHH_FRAME:
286 payload_type = RTP_PT_GSM_HALF;
287 payload_len = RTP_LEN_GSM_HALF;
288 duration = RTP_GSM_DURATION;
289 break;
Andreas Eversbergd8967f72012-03-08 14:39:19 +0100290 case GSM_TCH_FRAME_AMR:
291 payload_type = RTP_PT_AMR;
292 payload_len = frame->data[0];
293 duration = RTP_GSM_DURATION;
294 break;
Harald Welteda7ab742009-12-19 22:23:05 +0100295 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200296 DEBUGPC(DLMUX, "unsupported message type %d\n",
Harald Welteda7ab742009-12-19 22:23:05 +0100297 frame->msg_type);
298 return -EINVAL;
299 }
300
Harald Welte392736d2009-12-20 13:16:14 +0100301 {
302 struct timeval tv, tv_diff;
303 long int usec_diff, frame_diff;
304
305 gettimeofday(&tv, NULL);
306 tv_difference(&tv_diff, &rs->transmit.last_tv, &tv);
307 rs->transmit.last_tv = tv;
308
309 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
310 frame_diff = (usec_diff / 20000);
311
312 if (abs(frame_diff) > 1) {
313 long int frame_diff_excess = frame_diff - 1;
314
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200315 LOGP(DLMUX, LOGL_NOTICE,
Harald Welte (local)9fcf6d72009-12-27 17:01:40 +0100316 "Correcting frame difference of %ld frames\n", frame_diff_excess);
Harald Welte392736d2009-12-20 13:16:14 +0100317 rs->transmit.sequence += frame_diff_excess;
318 rs->transmit.timestamp += frame_diff_excess * duration;
319 }
320 }
321
Harald Weltea87f8f92014-05-17 13:43:01 +0200322 if (payload_len > MAX_RTP_PAYLOAD_LEN) {
Andreas Eversbergd8967f72012-03-08 14:39:19 +0100323 DEBUGPC(DLMUX, "RTP payload too large (%d octets)\n",
324 payload_len);
325 return -EINVAL;
326 }
327
Harald Weltea87f8f92014-05-17 13:43:01 +0200328 msg = msgb_alloc(sizeof(struct rtp_hdr) + payload_len, "RTP-GSM");
Harald Welteda7ab742009-12-19 22:23:05 +0100329 if (!msg)
330 return -ENOMEM;
Harald Weltea87f8f92014-05-17 13:43:01 +0200331 rtph = (struct rtp_hdr *) msgb_put(msg, sizeof(struct rtp_hdr));
Harald Welteda7ab742009-12-19 22:23:05 +0100332 rtph->version = RTP_VERSION;
333 rtph->padding = 0;
334 rtph->extension = 0;
335 rtph->csrc_count = 0;
336 rtph->marker = 0;
337 rtph->payload_type = payload_type;
338 rtph->sequence = htons(rs->transmit.sequence++);
339 rtph->timestamp = htonl(rs->transmit.timestamp);
340 rs->transmit.timestamp += duration;
341 rtph->ssrc = htonl(rs->transmit.ssrc);
Harald Weltea87f8f92014-05-17 13:43:01 +0200342
343 payload = msgb_put(msg, payload_len);
Andreas Eversbergd8967f72012-03-08 14:39:19 +0100344 if (frame->msg_type == GSM_TCH_FRAME_AMR)
Harald Weltea87f8f92014-05-17 13:43:01 +0200345 memcpy(payload, frame->data + 1, payload_len);
Andreas Eversbergd8967f72012-03-08 14:39:19 +0100346 else
Harald Weltea87f8f92014-05-17 13:43:01 +0200347 memcpy(payload, frame->data, payload_len);
Harald Welteda7ab742009-12-19 22:23:05 +0100348 msgb_enqueue(&rss->tx_queue, msg);
349 rss->bfd.when |= BSC_FD_WRITE;
350
351 return 0;
352}
353
Holger Hans Peter Freyther89acf062009-07-29 06:46:26 +0200354/* iterate over all chunks in one RTCP message, look for CNAME IEs and
Harald Welte805f6442009-07-28 18:25:29 +0200355 * replace all of those with 'new_cname' */
356static int rtcp_sdes_cname_mangle(struct msgb *msg, struct rtcp_hdr *rh,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200357 uint16_t *rtcp_len, const char *new_cname)
Harald Welte805f6442009-07-28 18:25:29 +0200358{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200359 uint8_t *rtcp_end;
360 uint8_t *cur = (uint8_t *) rh;
361 uint8_t tag, len = 0;
Harald Welte805f6442009-07-28 18:25:29 +0200362
363 rtcp_end = cur + *rtcp_len;
364 /* move cur to end of RTP header */
Harald Welte198f3f52009-07-29 10:46:41 +0200365 cur += sizeof(*rh);
Harald Welte805f6442009-07-28 18:25:29 +0200366
367 /* iterate over Chunks */
368 while (cur+4 < rtcp_end) {
369 /* skip four bytes SSRC/CSRC */
370 cur += 4;
371
372 /* iterate over IE's inside the chunk */
373 while (cur+1 < rtcp_end) {
374 tag = *cur++;
375 if (tag == 0) {
376 /* end of chunk, skip additional zero */
Daniel Willmann45fcb852014-05-21 15:46:43 +0200377 while ((*cur++ == 0) && (cur < rtcp_end)) { }
Harald Welte805f6442009-07-28 18:25:29 +0200378 break;
379 }
380 len = *cur++;
381
382 if (tag == RTCP_IE_CNAME) {
383 /* we've found the CNAME, lets mangle it */
384 if (len < strlen(new_cname)) {
385 /* we need to make more space */
386 int increase = strlen(new_cname) - len;
387
388 msgb_push(msg, increase);
389 memmove(cur+len+increase, cur+len,
390 rtcp_end - (cur+len));
391 /* FIXME: we have to respect RTCP
392 * padding/alignment rules! */
393 len += increase;
394 *(cur-1) += increase;
395 rtcp_end += increase;
396 *rtcp_len += increase;
397 }
398 /* copy new CNAME into message */
399 memcpy(cur, new_cname, strlen(new_cname));
400 /* FIXME: zero the padding in case new CNAME
401 * is smaller than old one !!! */
402 }
403 cur += len;
404 }
405 }
406
407 return 0;
408}
409
410static int rtcp_mangle(struct msgb *msg, struct rtp_socket *rs)
411{
412 struct rtp_sub_socket *rss = &rs->rtcp;
413 struct rtcp_hdr *rtph;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200414 uint16_t old_len;
Harald Welte805f6442009-07-28 18:25:29 +0200415 int rc;
416
417 if (!mangle_rtcp_cname)
418 return 0;
419
Harald Welteda7ab742009-12-19 22:23:05 +0100420 printf("RTCP\n");
Harald Welte805f6442009-07-28 18:25:29 +0200421 /* iterate over list of RTCP messages */
422 rtph = (struct rtcp_hdr *)msg->data;
Harald Welteda7ab742009-12-19 22:23:05 +0100423 while ((void *)rtph + sizeof(*rtph) <= (void *)msg->data + msg->len) {
Harald Welte805f6442009-07-28 18:25:29 +0200424 old_len = (ntohs(rtph->length) + 1) * 4;
Harald Welteda7ab742009-12-19 22:23:05 +0100425 if ((void *)rtph + old_len > (void *)msg->data + msg->len) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200426 DEBUGPC(DLMUX, "received RTCP packet too short for "
Harald Welteda7ab742009-12-19 22:23:05 +0100427 "length element\n");
428 return -EINVAL;
429 }
Harald Welte805f6442009-07-28 18:25:29 +0200430 if (rtph->type == RTCP_TYPE_SDES) {
431 char new_cname[255];
432 strncpy(new_cname, inet_ntoa(rss->sin_local.sin_addr),
433 sizeof(new_cname));
434 new_cname[sizeof(new_cname)-1] = '\0';
435 rc = rtcp_sdes_cname_mangle(msg, rtph, &old_len,
436 new_cname);
437 if (rc < 0)
438 return rc;
439 }
440 rtph = (void *)rtph + old_len;
441 }
442
443 return 0;
444}
445
Harald Welteead7a7b2009-07-28 00:01:58 +0200446/* read from incoming RTP/RTCP socket */
447static int rtp_socket_read(struct rtp_socket *rs, struct rtp_sub_socket *rss)
448{
449 int rc;
450 struct msgb *msg = msgb_alloc(RTP_ALLOC_SIZE, "RTP/RTCP");
Harald Welteda7ab742009-12-19 22:23:05 +0100451 struct msgb *new_msg;
Harald Welteead7a7b2009-07-28 00:01:58 +0200452 struct rtp_sub_socket *other_rss;
453
454 if (!msg)
455 return -ENOMEM;
456
457 rc = read(rss->bfd.fd, msg->data, RTP_ALLOC_SIZE);
458 if (rc <= 0) {
459 rss->bfd.when &= ~BSC_FD_READ;
460 return rc;
461 }
462
463 msgb_put(msg, rc);
464
465 switch (rs->rx_action) {
466 case RTP_PROXY:
Harald Welte805f6442009-07-28 18:25:29 +0200467 if (!rs->proxy.other_sock) {
468 rc = -EIO;
469 goto out_free;
470 }
Harald Welteead7a7b2009-07-28 00:01:58 +0200471 if (rss->bfd.priv_nr == RTP_PRIV_RTP)
472 other_rss = &rs->proxy.other_sock->rtp;
Harald Welte805f6442009-07-28 18:25:29 +0200473 else if (rss->bfd.priv_nr == RTP_PRIV_RTCP) {
Harald Welteead7a7b2009-07-28 00:01:58 +0200474 other_rss = &rs->proxy.other_sock->rtcp;
Harald Welte805f6442009-07-28 18:25:29 +0200475 /* modify RTCP SDES CNAME */
476 rc = rtcp_mangle(msg, rs);
477 if (rc < 0)
478 goto out_free;
479 } else {
480 rc = -EINVAL;
481 goto out_free;
Harald Welteead7a7b2009-07-28 00:01:58 +0200482 }
483 msgb_enqueue(&other_rss->tx_queue, msg);
484 other_rss->bfd.when |= BSC_FD_WRITE;
485 break;
Holger Hans Peter Freyther1ddb3562009-08-10 07:57:13 +0200486
487 case RTP_RECV_UPSTREAM:
Harald Welteda7ab742009-12-19 22:23:05 +0100488 if (!rs->receive.callref || !rs->receive.net) {
489 rc = -EIO;
490 goto out_free;
491 }
492 if (rss->bfd.priv_nr == RTP_PRIV_RTCP) {
493 if (!mangle_rtcp_cname) {
494 msgb_free(msg);
495 break;
496 }
497 /* modify RTCP SDES CNAME */
498 rc = rtcp_mangle(msg, rs);
499 if (rc < 0)
500 goto out_free;
501 msgb_enqueue(&rss->tx_queue, msg);
502 rss->bfd.when |= BSC_FD_WRITE;
503 break;
504 }
505 if (rss->bfd.priv_nr != RTP_PRIV_RTP) {
506 rc = -EINVAL;
507 goto out_free;
508 }
509 rc = rtp_decode(msg, rs->receive.callref, &new_msg);
510 if (rc < 0)
511 goto out_free;
512 msgb_free(msg);
Harald Welte31c00f72011-03-03 23:29:05 +0100513 trau_tx_to_mncc(rs->receive.net, new_msg);
Harald Welteda7ab742009-12-19 22:23:05 +0100514 break;
515
516 case RTP_NONE: /* if socket exists, but disabled by app */
517 msgb_free(msg);
Holger Hans Peter Freyther1ddb3562009-08-10 07:57:13 +0200518 break;
Harald Welteead7a7b2009-07-28 00:01:58 +0200519 }
520
Harald Welteda7ab742009-12-19 22:23:05 +0100521 return 0;
Harald Welte805f6442009-07-28 18:25:29 +0200522
523out_free:
524 msgb_free(msg);
525 return rc;
Harald Welteead7a7b2009-07-28 00:01:58 +0200526}
527
Harald Welte647db842013-02-03 12:06:58 +0100528/* \brief write from tx_queue to RTP/RTCP socket */
Harald Welteead7a7b2009-07-28 00:01:58 +0200529static int rtp_socket_write(struct rtp_socket *rs, struct rtp_sub_socket *rss)
530{
531 struct msgb *msg;
532 int written;
533
534 msg = msgb_dequeue(&rss->tx_queue);
535 if (!msg) {
536 rss->bfd.when &= ~BSC_FD_WRITE;
537 return 0;
538 }
539
540 written = write(rss->bfd.fd, msg->data, msg->len);
541 if (written < msg->len) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200542 LOGP(DLMIB, LOGL_ERROR, "short write");
Harald Welteead7a7b2009-07-28 00:01:58 +0200543 msgb_free(msg);
544 return -EIO;
545 }
546
547 msgb_free(msg);
548
549 return 0;
550}
551
552
Harald Welte647db842013-02-03 12:06:58 +0100553/*! \brief callback for the select.c:bfd_* layer */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200554static int rtp_bfd_cb(struct osmo_fd *bfd, unsigned int flags)
Harald Welteead7a7b2009-07-28 00:01:58 +0200555{
556 struct rtp_socket *rs = bfd->data;
557 struct rtp_sub_socket *rss;
558
559 switch (bfd->priv_nr) {
560 case RTP_PRIV_RTP:
561 rss = &rs->rtp;
562 break;
563 case RTP_PRIV_RTCP:
564 rss = &rs->rtcp;
565 break;
566 default:
567 return -EINVAL;
568 }
569
570 if (flags & BSC_FD_READ)
571 rtp_socket_read(rs, rss);
572
573 if (flags & BSC_FD_WRITE)
574 rtp_socket_write(rs, rss);
575
576 return 0;
577}
578
Harald Welte647db842013-02-03 12:06:58 +0100579/*! \brief initialize one rtp sub-socket */
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200580static void init_rss(struct rtp_sub_socket *rss,
Harald Welteead7a7b2009-07-28 00:01:58 +0200581 struct rtp_socket *rs, int fd, int priv_nr)
582{
583 /* initialize bfd */
584 rss->bfd.fd = fd;
585 rss->bfd.data = rs;
586 rss->bfd.priv_nr = priv_nr;
587 rss->bfd.cb = rtp_bfd_cb;
588}
589
Harald Welte647db842013-02-03 12:06:58 +0100590/*! \brief create a new RTP/RTCP socket and bind it */
Harald Welteead7a7b2009-07-28 00:01:58 +0200591struct rtp_socket *rtp_socket_create(void)
592{
593 int rc;
594 struct rtp_socket *rs;
595
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200596 DEBUGP(DLMUX, "rtp_socket_create(): ");
Harald Welte805f6442009-07-28 18:25:29 +0200597
Harald Welteead7a7b2009-07-28 00:01:58 +0200598 rs = talloc_zero(tall_bsc_ctx, struct rtp_socket);
599 if (!rs)
600 return NULL;
601
602 INIT_LLIST_HEAD(&rs->rtp.tx_queue);
603 INIT_LLIST_HEAD(&rs->rtcp.tx_queue);
604
605 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
606 if (rc < 0)
607 goto out_free;
608
609 init_rss(&rs->rtp, rs, rc, RTP_PRIV_RTP);
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200610 rc = osmo_fd_register(&rs->rtp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200611 if (rc < 0)
612 goto out_rtp_socket;
613
614 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
615 if (rc < 0)
616 goto out_rtp_bfd;
617
618 init_rss(&rs->rtcp, rs, rc, RTP_PRIV_RTCP);
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200619 rc = osmo_fd_register(&rs->rtcp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200620 if (rc < 0)
621 goto out_rtcp_socket;
622
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200623 DEBUGPC(DLMUX, "success\n");
Harald Welte805f6442009-07-28 18:25:29 +0200624
625 rc = rtp_socket_bind(rs, INADDR_ANY);
626 if (rc < 0)
627 goto out_rtcp_bfd;
628
Harald Welteead7a7b2009-07-28 00:01:58 +0200629 return rs;
630
Harald Welte805f6442009-07-28 18:25:29 +0200631out_rtcp_bfd:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200632 osmo_fd_unregister(&rs->rtcp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200633out_rtcp_socket:
634 close(rs->rtcp.bfd.fd);
635out_rtp_bfd:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200636 osmo_fd_unregister(&rs->rtp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200637out_rtp_socket:
638 close(rs->rtp.bfd.fd);
639out_free:
640 talloc_free(rs);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200641 DEBUGPC(DLMUX, "failed\n");
Harald Welteead7a7b2009-07-28 00:01:58 +0200642 return NULL;
643}
644
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200645static int rtp_sub_socket_bind(struct rtp_sub_socket *rss, uint32_t ip,
646 uint16_t port)
Harald Welteead7a7b2009-07-28 00:01:58 +0200647{
Harald Welte805f6442009-07-28 18:25:29 +0200648 int rc;
649 socklen_t alen = sizeof(rss->sin_local);
650
Harald Welteead7a7b2009-07-28 00:01:58 +0200651 rss->sin_local.sin_family = AF_INET;
652 rss->sin_local.sin_addr.s_addr = htonl(ip);
653 rss->sin_local.sin_port = htons(port);
654 rss->bfd.when |= BSC_FD_READ;
655
Harald Welte805f6442009-07-28 18:25:29 +0200656 rc = bind(rss->bfd.fd, (struct sockaddr *)&rss->sin_local,
657 sizeof(rss->sin_local));
658 if (rc < 0)
659 return rc;
660
661 /* retrieve the address we actually bound to, in case we
662 * passed INADDR_ANY as IP address */
663 return getsockname(rss->bfd.fd, (struct sockaddr *)&rss->sin_local,
664 &alen);
Harald Welteead7a7b2009-07-28 00:01:58 +0200665}
666
667#define RTP_PORT_BASE 30000
Harald Welte805f6442009-07-28 18:25:29 +0200668static unsigned int next_udp_port = RTP_PORT_BASE;
Harald Welteead7a7b2009-07-28 00:01:58 +0200669
Harald Welte647db842013-02-03 12:06:58 +0100670/*! \brief bind a RTP socket to a specific local address
671 * \param[in] rs RTP socket to be bound
672 * \param[in] ip local IP address to which socket is to be bound
673 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200674int rtp_socket_bind(struct rtp_socket *rs, uint32_t ip)
Harald Welteead7a7b2009-07-28 00:01:58 +0200675{
Harald Welte805f6442009-07-28 18:25:29 +0200676 int rc = -EIO;
677 struct in_addr ia;
678
679 ia.s_addr = htonl(ip);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200680 DEBUGP(DLMUX, "rtp_socket_bind(rs=%p, IP=%s): ", rs,
Harald Welte805f6442009-07-28 18:25:29 +0200681 inet_ntoa(ia));
Harald Welteead7a7b2009-07-28 00:01:58 +0200682
683 /* try to bind to a consecutive pair of ports */
Harald Welte805f6442009-07-28 18:25:29 +0200684 for (next_udp_port = next_udp_port % 0xffff;
685 next_udp_port < 0xffff; next_udp_port += 2) {
Harald Welteead7a7b2009-07-28 00:01:58 +0200686 rc = rtp_sub_socket_bind(&rs->rtp, ip, next_udp_port);
687 if (rc != 0)
688 continue;
689
690 rc = rtp_sub_socket_bind(&rs->rtcp, ip, next_udp_port+1);
691 if (rc == 0)
692 break;
693 }
Harald Welte805f6442009-07-28 18:25:29 +0200694 if (rc < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200695 DEBUGPC(DLMUX, "failed\n");
Harald Welteead7a7b2009-07-28 00:01:58 +0200696 return rc;
Harald Welte805f6442009-07-28 18:25:29 +0200697 }
Harald Welteead7a7b2009-07-28 00:01:58 +0200698
Harald Welte805f6442009-07-28 18:25:29 +0200699 ia.s_addr = rs->rtp.sin_local.sin_addr.s_addr;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200700 DEBUGPC(DLMUX, "BOUND_IP=%s, BOUND_PORT=%u\n",
Harald Welte805f6442009-07-28 18:25:29 +0200701 inet_ntoa(ia), ntohs(rs->rtp.sin_local.sin_port));
Harald Welteead7a7b2009-07-28 00:01:58 +0200702 return ntohs(rs->rtp.sin_local.sin_port);
703}
704
705static int rtp_sub_socket_connect(struct rtp_sub_socket *rss,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200706 uint32_t ip, uint16_t port)
Harald Welteead7a7b2009-07-28 00:01:58 +0200707{
Harald Welte805f6442009-07-28 18:25:29 +0200708 int rc;
709 socklen_t alen = sizeof(rss->sin_local);
710
Harald Welteead7a7b2009-07-28 00:01:58 +0200711 rss->sin_remote.sin_family = AF_INET;
712 rss->sin_remote.sin_addr.s_addr = htonl(ip);
713 rss->sin_remote.sin_port = htons(port);
714
Harald Welte805f6442009-07-28 18:25:29 +0200715 rc = connect(rss->bfd.fd, (struct sockaddr *) &rss->sin_remote,
716 sizeof(rss->sin_remote));
717 if (rc < 0)
718 return rc;
719
720 return getsockname(rss->bfd.fd, (struct sockaddr *)&rss->sin_local,
721 &alen);
Harald Welteead7a7b2009-07-28 00:01:58 +0200722}
723
Harald Welte647db842013-02-03 12:06:58 +0100724/*! \brief 'connect' a RTP socket to a remote peer
725 * \param[in] rs RTP socket to be connected
726 * \param[in] ip remote IP address to which to connect
727 * \param[in] port remote UDP port number to which to connect
728 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200729int rtp_socket_connect(struct rtp_socket *rs, uint32_t ip, uint16_t port)
Harald Welteead7a7b2009-07-28 00:01:58 +0200730{
731 int rc;
Harald Welte805f6442009-07-28 18:25:29 +0200732 struct in_addr ia;
733
734 ia.s_addr = htonl(ip);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200735 DEBUGP(DLMUX, "rtp_socket_connect(rs=%p, ip=%s, port=%u)\n",
Harald Welte805f6442009-07-28 18:25:29 +0200736 rs, inet_ntoa(ia), port);
Harald Welteead7a7b2009-07-28 00:01:58 +0200737
738 rc = rtp_sub_socket_connect(&rs->rtp, ip, port);
739 if (rc < 0)
740 return rc;
741
742 return rtp_sub_socket_connect(&rs->rtcp, ip, port+1);
743}
744
Harald Welte647db842013-02-03 12:06:58 +0100745/*! \brief bind two RTP/RTCP sockets together in the proxy
746 * \param[in] this First RTP socket
747 * \param[in] other Second RTP socket
748 */
Harald Welteead7a7b2009-07-28 00:01:58 +0200749int rtp_socket_proxy(struct rtp_socket *this, struct rtp_socket *other)
750{
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200751 DEBUGP(DLMUX, "rtp_socket_proxy(this=%p, other=%p)\n",
Harald Welte805f6442009-07-28 18:25:29 +0200752 this, other);
753
Harald Welteead7a7b2009-07-28 00:01:58 +0200754 this->rx_action = RTP_PROXY;
755 this->proxy.other_sock = other;
756
757 other->rx_action = RTP_PROXY;
758 other->proxy.other_sock = this;
759
760 return 0;
761}
762
Harald Welte647db842013-02-03 12:06:58 +0100763/*! \brief bind RTP/RTCP socket to application, disabling proxy
764 * \param[in] this RTP socket
765 * \param[in] net gsm_network argument to trau_tx_to_mncc()
766 * \param[in] callref callref argument to trau_tx_to_mncc()
767 */
Harald Welte9fb1f102009-12-20 17:07:23 +0100768int rtp_socket_upstream(struct rtp_socket *this, struct gsm_network *net,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200769 uint32_t callref)
Harald Welteda7ab742009-12-19 22:23:05 +0100770{
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200771 DEBUGP(DLMUX, "rtp_socket_proxy(this=%p, callref=%u)\n",
Harald Welteda7ab742009-12-19 22:23:05 +0100772 this, callref);
773
774 if (callref) {
775 this->rx_action = RTP_RECV_UPSTREAM;
776 this->receive.net = net;
777 this->receive.callref = callref;
778 } else
779 this->rx_action = RTP_NONE;
780
781 return 0;
782}
783
Harald Welteead7a7b2009-07-28 00:01:58 +0200784static void free_tx_queue(struct rtp_sub_socket *rss)
785{
786 struct msgb *msg;
787
788 while ((msg = msgb_dequeue(&rss->tx_queue)))
789 msgb_free(msg);
790}
791
Harald Welte647db842013-02-03 12:06:58 +0100792/*! \brief Free/release a previously allocated RTP socket
793 * \param[in[] rs RTP/RTCP socket to be released
794 */
Harald Welteead7a7b2009-07-28 00:01:58 +0200795int rtp_socket_free(struct rtp_socket *rs)
796{
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200797 DEBUGP(DLMUX, "rtp_socket_free(rs=%p)\n", rs);
Harald Welteead7a7b2009-07-28 00:01:58 +0200798
799 /* make sure we don't leave references dangling to us */
800 if (rs->rx_action == RTP_PROXY &&
801 rs->proxy.other_sock)
802 rs->proxy.other_sock->proxy.other_sock = NULL;
803
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200804 osmo_fd_unregister(&rs->rtp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200805 close(rs->rtp.bfd.fd);
806 free_tx_queue(&rs->rtp);
807
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200808 osmo_fd_unregister(&rs->rtcp.bfd);
Harald Welteead7a7b2009-07-28 00:01:58 +0200809 close(rs->rtcp.bfd.fd);
810 free_tx_queue(&rs->rtcp);
811
812 talloc_free(rs);
813
814 return 0;
815}