Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 1 | /* 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 |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (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 |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <errno.h> |
| 23 | #include <unistd.h> |
| 24 | #include <sys/socket.h> |
| 25 | #include <netinet/in.h> |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 26 | #include <arpa/inet.h> |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 27 | #include <sys/time.h> /* gettimeofday() */ |
| 28 | #include <unistd.h> /* get..() */ |
| 29 | #include <time.h> /* clock() */ |
| 30 | #include <sys/utsname.h> /* uname() */ |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 31 | |
| 32 | #include <openbsc/talloc.h> |
| 33 | #include <openbsc/gsm_data.h> |
| 34 | #include <openbsc/msgb.h> |
| 35 | #include <openbsc/select.h> |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 36 | #include <openbsc/debug.h> |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 37 | #include <openbsc/rtp_proxy.h> |
| 38 | |
| 39 | static LLIST_HEAD(rtp_sockets); |
| 40 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 41 | /* should we mangle the CNAME inside SDES of RTCP packets? We disable |
| 42 | * this by default, as it seems to be not needed */ |
| 43 | static int mangle_rtcp_cname = 0; |
| 44 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 45 | enum rtp_bfd_priv { |
| 46 | RTP_PRIV_NONE, |
| 47 | RTP_PRIV_RTP, |
| 48 | RTP_PRIV_RTCP |
| 49 | }; |
| 50 | |
| 51 | #define RTP_ALLOC_SIZE 1500 |
| 52 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 53 | /* according to RFC 1889 */ |
| 54 | struct rtcp_hdr { |
| 55 | u_int8_t byte0; |
| 56 | u_int8_t type; |
| 57 | u_int16_t length; |
| 58 | } __attribute__((packed)); |
| 59 | |
| 60 | #define RTCP_TYPE_SDES 202 |
| 61 | |
| 62 | #define RTCP_IE_CNAME 1 |
| 63 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 64 | /* according to RFC 3550 */ |
| 65 | struct rtp_hdr { |
| 66 | u_int8_t csrc_count:4, |
| 67 | extension:1, |
| 68 | padding:1, |
| 69 | version:2; |
| 70 | u_int8_t payload_type:7, |
| 71 | marker:1; |
| 72 | u_int16_t sequence; |
| 73 | u_int32_t timestamp; |
| 74 | u_int32_t ssrc; |
| 75 | } __attribute__((packed)); |
| 76 | |
| 77 | struct rtp_x_hdr { |
| 78 | u_int16_t by_profile; |
| 79 | u_int16_t length; |
| 80 | } __attribute__((packed)); |
| 81 | |
| 82 | #define RTP_VERSION 2 |
| 83 | |
| 84 | #define RTP_PT_GSM_FULL 3 |
Harald Welte | aca8f15 | 2009-12-19 23:06:41 +0100 | [diff] [blame] | 85 | #define RTP_PT_GSM_EFR 97 |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 86 | |
| 87 | /* decode an rtp frame and create a new buffer with payload */ |
| 88 | static int rtp_decode(struct msgb *msg, u_int32_t callref, struct msgb **data) |
| 89 | { |
| 90 | struct msgb *new_msg; |
| 91 | struct gsm_data_frame *frame; |
| 92 | struct rtp_hdr *rtph = (struct rtp_hdr *)msg->data; |
| 93 | struct rtp_x_hdr *rtpxh; |
| 94 | u_int8_t *payload; |
| 95 | int payload_len; |
| 96 | int msg_type; |
| 97 | int x_len; |
| 98 | |
| 99 | if (msg->len < 12) { |
| 100 | DEBUGPC(DMUX, "received RTP frame too short (len = %d)\n", |
| 101 | msg->len); |
| 102 | return -EINVAL; |
| 103 | } |
| 104 | if (rtph->version != RTP_VERSION) { |
| 105 | DEBUGPC(DMUX, "received RTP version %d not supported.\n", |
| 106 | rtph->version); |
| 107 | return -EINVAL; |
| 108 | } |
| 109 | payload = msg->data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2); |
| 110 | payload_len = msg->len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2); |
| 111 | if (payload_len < 0) { |
| 112 | DEBUGPC(DMUX, "received RTP frame too short (len = %d, " |
| 113 | "csrc count = %d)\n", msg->len, rtph->csrc_count); |
| 114 | return -EINVAL; |
| 115 | } |
| 116 | if (rtph->extension) { |
| 117 | if (payload_len < sizeof(struct rtp_x_hdr)) { |
| 118 | DEBUGPC(DMUX, "received RTP frame too short for " |
| 119 | "extension header\n"); |
| 120 | return -EINVAL; |
| 121 | } |
| 122 | rtpxh = (struct rtp_x_hdr *)payload; |
| 123 | x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr); |
| 124 | payload += x_len; |
| 125 | payload_len -= x_len; |
| 126 | if (payload_len < 0) { |
| 127 | DEBUGPC(DMUX, "received RTP frame too short, " |
| 128 | "extension header exceeds frame length\n"); |
| 129 | return -EINVAL; |
| 130 | } |
| 131 | } |
| 132 | if (rtph->padding) { |
| 133 | if (payload_len < 0) { |
| 134 | DEBUGPC(DMUX, "received RTP frame too short for " |
| 135 | "padding length\n"); |
| 136 | return -EINVAL; |
| 137 | } |
| 138 | payload_len -= payload[payload_len - 1]; |
| 139 | if (payload_len < 0) { |
| 140 | DEBUGPC(DMUX, "received RTP frame with padding " |
| 141 | "greater than payload\n"); |
| 142 | return -EINVAL; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | switch (rtph->payload_type) { |
| 147 | case RTP_PT_GSM_FULL: |
| 148 | msg_type = GSM_TCHF_FRAME; |
| 149 | if (payload_len != 33) { |
| 150 | DEBUGPC(DMUX, "received RTP full rate frame with " |
| 151 | "payload length != 32 (len = %d)\n", |
| 152 | payload_len); |
| 153 | return -EINVAL; |
| 154 | } |
| 155 | break; |
Harald Welte | aca8f15 | 2009-12-19 23:06:41 +0100 | [diff] [blame] | 156 | case RTP_PT_GSM_EFR: |
| 157 | msg_type = GSM_TCHF_FRAME_EFR; |
| 158 | break; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 159 | default: |
| 160 | DEBUGPC(DMUX, "received RTP frame with unknown payload " |
| 161 | "type %d\n", rtph->payload_type); |
| 162 | return -EINVAL; |
| 163 | } |
| 164 | |
| 165 | new_msg = msgb_alloc(sizeof(struct gsm_data_frame) + payload_len, |
| 166 | "GSM-DATA"); |
| 167 | if (!new_msg) |
| 168 | return -ENOMEM; |
| 169 | frame = (struct gsm_data_frame *)(new_msg->data); |
| 170 | frame->msg_type = msg_type; |
| 171 | frame->callref = callref; |
| 172 | memcpy(frame->data, payload, payload_len); |
| 173 | msgb_put(new_msg, sizeof(struct gsm_data_frame) + payload_len); |
| 174 | |
| 175 | *data = new_msg; |
| 176 | return 0; |
| 177 | } |
| 178 | |
Harald Welte | 392736d | 2009-12-20 13:16:14 +0100 | [diff] [blame] | 179 | /* "to - from" */ |
| 180 | static void tv_difference(struct timeval *diff, const struct timeval *from, |
| 181 | const struct timeval *__to) |
| 182 | { |
| 183 | struct timeval _to = *__to, *to = &_to; |
| 184 | |
| 185 | if (to->tv_usec < from->tv_usec) { |
| 186 | to->tv_sec -= 1; |
| 187 | to->tv_usec += 1000000; |
| 188 | } |
| 189 | |
| 190 | diff->tv_usec = to->tv_usec - from->tv_usec; |
| 191 | diff->tv_sec = to->tv_sec - from->tv_sec; |
| 192 | } |
| 193 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 194 | /* encode and send a rtp frame */ |
| 195 | int rtp_send_frame(struct rtp_socket *rs, struct gsm_data_frame *frame) |
| 196 | { |
| 197 | struct rtp_sub_socket *rss = &rs->rtp; |
| 198 | struct msgb *msg; |
| 199 | struct rtp_hdr *rtph; |
| 200 | int payload_type; |
| 201 | int payload_len; |
| 202 | int duration; /* in samples */ |
| 203 | |
| 204 | if (rs->tx_action != RTP_SEND_DOWNSTREAM) { |
| 205 | /* initialize sequences */ |
| 206 | rs->tx_action = RTP_SEND_DOWNSTREAM; |
| 207 | rs->transmit.ssrc = rand(); |
| 208 | rs->transmit.sequence = random(); |
| 209 | rs->transmit.timestamp = random(); |
| 210 | } |
| 211 | |
| 212 | switch (frame->msg_type) { |
| 213 | case GSM_TCHF_FRAME: |
| 214 | payload_type = RTP_PT_GSM_FULL; |
| 215 | payload_len = 33; |
| 216 | duration = 160; |
| 217 | break; |
Harald Welte | aca8f15 | 2009-12-19 23:06:41 +0100 | [diff] [blame] | 218 | case GSM_TCHF_FRAME_EFR: |
| 219 | payload_type = RTP_PT_GSM_EFR; |
| 220 | payload_len = 31; |
| 221 | duration = 160; |
| 222 | break; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 223 | default: |
| 224 | DEBUGPC(DMUX, "unsupported message type %d\n", |
| 225 | frame->msg_type); |
| 226 | return -EINVAL; |
| 227 | } |
| 228 | |
Harald Welte | 392736d | 2009-12-20 13:16:14 +0100 | [diff] [blame] | 229 | { |
| 230 | struct timeval tv, tv_diff; |
| 231 | long int usec_diff, frame_diff; |
| 232 | |
| 233 | gettimeofday(&tv, NULL); |
| 234 | tv_difference(&tv_diff, &rs->transmit.last_tv, &tv); |
| 235 | rs->transmit.last_tv = tv; |
| 236 | |
| 237 | usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec; |
| 238 | frame_diff = (usec_diff / 20000); |
| 239 | |
| 240 | if (abs(frame_diff) > 1) { |
| 241 | long int frame_diff_excess = frame_diff - 1; |
| 242 | |
| 243 | DEBUGP(DMUX, "Correcting frame difference of %ld frames\n", frame_diff_excess); |
| 244 | rs->transmit.sequence += frame_diff_excess; |
| 245 | rs->transmit.timestamp += frame_diff_excess * duration; |
| 246 | } |
| 247 | } |
| 248 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 249 | msg = msgb_alloc(sizeof(struct rtp_hdr) + payload_len, "RTP-GSM-FULL"); |
| 250 | if (!msg) |
| 251 | return -ENOMEM; |
| 252 | rtph = (struct rtp_hdr *)msg->data; |
| 253 | rtph->version = RTP_VERSION; |
| 254 | rtph->padding = 0; |
| 255 | rtph->extension = 0; |
| 256 | rtph->csrc_count = 0; |
| 257 | rtph->marker = 0; |
| 258 | rtph->payload_type = payload_type; |
| 259 | rtph->sequence = htons(rs->transmit.sequence++); |
| 260 | rtph->timestamp = htonl(rs->transmit.timestamp); |
| 261 | rs->transmit.timestamp += duration; |
| 262 | rtph->ssrc = htonl(rs->transmit.ssrc); |
| 263 | memcpy(msg->data + sizeof(struct rtp_hdr), frame->data, payload_len); |
| 264 | msgb_put(msg, sizeof(struct rtp_hdr) + payload_len); |
| 265 | msgb_enqueue(&rss->tx_queue, msg); |
| 266 | rss->bfd.when |= BSC_FD_WRITE; |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
Holger Hans Peter Freyther | 89acf06 | 2009-07-29 06:46:26 +0200 | [diff] [blame] | 271 | /* iterate over all chunks in one RTCP message, look for CNAME IEs and |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 272 | * replace all of those with 'new_cname' */ |
| 273 | static int rtcp_sdes_cname_mangle(struct msgb *msg, struct rtcp_hdr *rh, |
| 274 | u_int16_t *rtcp_len, const char *new_cname) |
| 275 | { |
| 276 | u_int8_t *rtcp_end; |
| 277 | u_int8_t *cur = (u_int8_t *) rh; |
| 278 | u_int8_t tag, len = 0; |
| 279 | |
| 280 | rtcp_end = cur + *rtcp_len; |
| 281 | /* move cur to end of RTP header */ |
Harald Welte | 198f3f5 | 2009-07-29 10:46:41 +0200 | [diff] [blame] | 282 | cur += sizeof(*rh); |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 283 | |
| 284 | /* iterate over Chunks */ |
| 285 | while (cur+4 < rtcp_end) { |
| 286 | /* skip four bytes SSRC/CSRC */ |
| 287 | cur += 4; |
| 288 | |
| 289 | /* iterate over IE's inside the chunk */ |
| 290 | while (cur+1 < rtcp_end) { |
| 291 | tag = *cur++; |
| 292 | if (tag == 0) { |
| 293 | /* end of chunk, skip additional zero */ |
| 294 | while (*cur++ == 0) { } |
| 295 | break; |
| 296 | } |
| 297 | len = *cur++; |
| 298 | |
| 299 | if (tag == RTCP_IE_CNAME) { |
| 300 | /* we've found the CNAME, lets mangle it */ |
| 301 | if (len < strlen(new_cname)) { |
| 302 | /* we need to make more space */ |
| 303 | int increase = strlen(new_cname) - len; |
| 304 | |
| 305 | msgb_push(msg, increase); |
| 306 | memmove(cur+len+increase, cur+len, |
| 307 | rtcp_end - (cur+len)); |
| 308 | /* FIXME: we have to respect RTCP |
| 309 | * padding/alignment rules! */ |
| 310 | len += increase; |
| 311 | *(cur-1) += increase; |
| 312 | rtcp_end += increase; |
| 313 | *rtcp_len += increase; |
| 314 | } |
| 315 | /* copy new CNAME into message */ |
| 316 | memcpy(cur, new_cname, strlen(new_cname)); |
| 317 | /* FIXME: zero the padding in case new CNAME |
| 318 | * is smaller than old one !!! */ |
| 319 | } |
| 320 | cur += len; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | static int rtcp_mangle(struct msgb *msg, struct rtp_socket *rs) |
| 328 | { |
| 329 | struct rtp_sub_socket *rss = &rs->rtcp; |
| 330 | struct rtcp_hdr *rtph; |
| 331 | u_int16_t old_len; |
| 332 | int rc; |
| 333 | |
| 334 | if (!mangle_rtcp_cname) |
| 335 | return 0; |
| 336 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 337 | printf("RTCP\n"); |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 338 | /* iterate over list of RTCP messages */ |
| 339 | rtph = (struct rtcp_hdr *)msg->data; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 340 | while ((void *)rtph + sizeof(*rtph) <= (void *)msg->data + msg->len) { |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 341 | old_len = (ntohs(rtph->length) + 1) * 4; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 342 | if ((void *)rtph + old_len > (void *)msg->data + msg->len) { |
| 343 | DEBUGPC(DMUX, "received RTCP packet too short for " |
| 344 | "length element\n"); |
| 345 | return -EINVAL; |
| 346 | } |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 347 | if (rtph->type == RTCP_TYPE_SDES) { |
| 348 | char new_cname[255]; |
| 349 | strncpy(new_cname, inet_ntoa(rss->sin_local.sin_addr), |
| 350 | sizeof(new_cname)); |
| 351 | new_cname[sizeof(new_cname)-1] = '\0'; |
| 352 | rc = rtcp_sdes_cname_mangle(msg, rtph, &old_len, |
| 353 | new_cname); |
| 354 | if (rc < 0) |
| 355 | return rc; |
| 356 | } |
| 357 | rtph = (void *)rtph + old_len; |
| 358 | } |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 363 | /* read from incoming RTP/RTCP socket */ |
| 364 | static int rtp_socket_read(struct rtp_socket *rs, struct rtp_sub_socket *rss) |
| 365 | { |
| 366 | int rc; |
| 367 | struct msgb *msg = msgb_alloc(RTP_ALLOC_SIZE, "RTP/RTCP"); |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 368 | struct msgb *new_msg; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 369 | struct rtp_sub_socket *other_rss; |
| 370 | |
| 371 | if (!msg) |
| 372 | return -ENOMEM; |
| 373 | |
| 374 | rc = read(rss->bfd.fd, msg->data, RTP_ALLOC_SIZE); |
| 375 | if (rc <= 0) { |
| 376 | rss->bfd.when &= ~BSC_FD_READ; |
| 377 | return rc; |
| 378 | } |
| 379 | |
| 380 | msgb_put(msg, rc); |
| 381 | |
| 382 | switch (rs->rx_action) { |
| 383 | case RTP_PROXY: |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 384 | if (!rs->proxy.other_sock) { |
| 385 | rc = -EIO; |
| 386 | goto out_free; |
| 387 | } |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 388 | if (rss->bfd.priv_nr == RTP_PRIV_RTP) |
| 389 | other_rss = &rs->proxy.other_sock->rtp; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 390 | else if (rss->bfd.priv_nr == RTP_PRIV_RTCP) { |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 391 | other_rss = &rs->proxy.other_sock->rtcp; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 392 | /* modify RTCP SDES CNAME */ |
| 393 | rc = rtcp_mangle(msg, rs); |
| 394 | if (rc < 0) |
| 395 | goto out_free; |
| 396 | } else { |
| 397 | rc = -EINVAL; |
| 398 | goto out_free; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 399 | } |
| 400 | msgb_enqueue(&other_rss->tx_queue, msg); |
| 401 | other_rss->bfd.when |= BSC_FD_WRITE; |
| 402 | break; |
Holger Hans Peter Freyther | 1ddb356 | 2009-08-10 07:57:13 +0200 | [diff] [blame] | 403 | |
| 404 | case RTP_RECV_UPSTREAM: |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 405 | if (!rs->receive.callref || !rs->receive.net) { |
| 406 | rc = -EIO; |
| 407 | goto out_free; |
| 408 | } |
| 409 | if (rss->bfd.priv_nr == RTP_PRIV_RTCP) { |
| 410 | if (!mangle_rtcp_cname) { |
| 411 | msgb_free(msg); |
| 412 | break; |
| 413 | } |
| 414 | /* modify RTCP SDES CNAME */ |
| 415 | rc = rtcp_mangle(msg, rs); |
| 416 | if (rc < 0) |
| 417 | goto out_free; |
| 418 | msgb_enqueue(&rss->tx_queue, msg); |
| 419 | rss->bfd.when |= BSC_FD_WRITE; |
| 420 | break; |
| 421 | } |
| 422 | if (rss->bfd.priv_nr != RTP_PRIV_RTP) { |
| 423 | rc = -EINVAL; |
| 424 | goto out_free; |
| 425 | } |
| 426 | rc = rtp_decode(msg, rs->receive.callref, &new_msg); |
| 427 | if (rc < 0) |
| 428 | goto out_free; |
| 429 | msgb_free(msg); |
| 430 | msgb_enqueue(&rs->receive.net->upqueue, new_msg); |
| 431 | break; |
| 432 | |
| 433 | case RTP_NONE: /* if socket exists, but disabled by app */ |
| 434 | msgb_free(msg); |
Holger Hans Peter Freyther | 1ddb356 | 2009-08-10 07:57:13 +0200 | [diff] [blame] | 435 | break; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 438 | return 0; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 439 | |
| 440 | out_free: |
| 441 | msgb_free(msg); |
| 442 | return rc; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* write from tx_queue to RTP/RTCP socket */ |
| 446 | static int rtp_socket_write(struct rtp_socket *rs, struct rtp_sub_socket *rss) |
| 447 | { |
| 448 | struct msgb *msg; |
| 449 | int written; |
| 450 | |
| 451 | msg = msgb_dequeue(&rss->tx_queue); |
| 452 | if (!msg) { |
| 453 | rss->bfd.when &= ~BSC_FD_WRITE; |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | written = write(rss->bfd.fd, msg->data, msg->len); |
| 458 | if (written < msg->len) { |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 459 | LOGP(DMIB, LOGL_ERROR, "short write"); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 460 | msgb_free(msg); |
| 461 | return -EIO; |
| 462 | } |
| 463 | |
| 464 | msgb_free(msg); |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | |
| 470 | /* callback for the select.c:bfd_* layer */ |
| 471 | static int rtp_bfd_cb(struct bsc_fd *bfd, unsigned int flags) |
| 472 | { |
| 473 | struct rtp_socket *rs = bfd->data; |
| 474 | struct rtp_sub_socket *rss; |
| 475 | |
| 476 | switch (bfd->priv_nr) { |
| 477 | case RTP_PRIV_RTP: |
| 478 | rss = &rs->rtp; |
| 479 | break; |
| 480 | case RTP_PRIV_RTCP: |
| 481 | rss = &rs->rtcp; |
| 482 | break; |
| 483 | default: |
| 484 | return -EINVAL; |
| 485 | } |
| 486 | |
| 487 | if (flags & BSC_FD_READ) |
| 488 | rtp_socket_read(rs, rss); |
| 489 | |
| 490 | if (flags & BSC_FD_WRITE) |
| 491 | rtp_socket_write(rs, rss); |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static void init_rss(struct rtp_sub_socket *rss, |
| 497 | struct rtp_socket *rs, int fd, int priv_nr) |
| 498 | { |
| 499 | /* initialize bfd */ |
| 500 | rss->bfd.fd = fd; |
| 501 | rss->bfd.data = rs; |
| 502 | rss->bfd.priv_nr = priv_nr; |
| 503 | rss->bfd.cb = rtp_bfd_cb; |
| 504 | } |
| 505 | |
| 506 | struct rtp_socket *rtp_socket_create(void) |
| 507 | { |
| 508 | int rc; |
| 509 | struct rtp_socket *rs; |
| 510 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 511 | DEBUGP(DMUX, "rtp_socket_create(): "); |
| 512 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 513 | rs = talloc_zero(tall_bsc_ctx, struct rtp_socket); |
| 514 | if (!rs) |
| 515 | return NULL; |
| 516 | |
| 517 | INIT_LLIST_HEAD(&rs->rtp.tx_queue); |
| 518 | INIT_LLIST_HEAD(&rs->rtcp.tx_queue); |
| 519 | |
| 520 | rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 521 | if (rc < 0) |
| 522 | goto out_free; |
| 523 | |
| 524 | init_rss(&rs->rtp, rs, rc, RTP_PRIV_RTP); |
| 525 | rc = bsc_register_fd(&rs->rtp.bfd); |
| 526 | if (rc < 0) |
| 527 | goto out_rtp_socket; |
| 528 | |
| 529 | rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 530 | if (rc < 0) |
| 531 | goto out_rtp_bfd; |
| 532 | |
| 533 | init_rss(&rs->rtcp, rs, rc, RTP_PRIV_RTCP); |
| 534 | rc = bsc_register_fd(&rs->rtcp.bfd); |
| 535 | if (rc < 0) |
| 536 | goto out_rtcp_socket; |
| 537 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 538 | DEBUGPC(DMUX, "success\n"); |
| 539 | |
| 540 | rc = rtp_socket_bind(rs, INADDR_ANY); |
| 541 | if (rc < 0) |
| 542 | goto out_rtcp_bfd; |
| 543 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 544 | return rs; |
| 545 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 546 | out_rtcp_bfd: |
| 547 | bsc_unregister_fd(&rs->rtcp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 548 | out_rtcp_socket: |
| 549 | close(rs->rtcp.bfd.fd); |
| 550 | out_rtp_bfd: |
| 551 | bsc_unregister_fd(&rs->rtp.bfd); |
| 552 | out_rtp_socket: |
| 553 | close(rs->rtp.bfd.fd); |
| 554 | out_free: |
| 555 | talloc_free(rs); |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 556 | DEBUGPC(DMUX, "failed\n"); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 557 | return NULL; |
| 558 | } |
| 559 | |
| 560 | static int rtp_sub_socket_bind(struct rtp_sub_socket *rss, u_int32_t ip, |
| 561 | u_int16_t port) |
| 562 | { |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 563 | int rc; |
| 564 | socklen_t alen = sizeof(rss->sin_local); |
| 565 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 566 | rss->sin_local.sin_family = AF_INET; |
| 567 | rss->sin_local.sin_addr.s_addr = htonl(ip); |
| 568 | rss->sin_local.sin_port = htons(port); |
| 569 | rss->bfd.when |= BSC_FD_READ; |
| 570 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 571 | rc = bind(rss->bfd.fd, (struct sockaddr *)&rss->sin_local, |
| 572 | sizeof(rss->sin_local)); |
| 573 | if (rc < 0) |
| 574 | return rc; |
| 575 | |
| 576 | /* retrieve the address we actually bound to, in case we |
| 577 | * passed INADDR_ANY as IP address */ |
| 578 | return getsockname(rss->bfd.fd, (struct sockaddr *)&rss->sin_local, |
| 579 | &alen); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | #define RTP_PORT_BASE 30000 |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 583 | static unsigned int next_udp_port = RTP_PORT_BASE; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 584 | |
| 585 | /* bind a RTP socket to a local address */ |
| 586 | int rtp_socket_bind(struct rtp_socket *rs, u_int32_t ip) |
| 587 | { |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 588 | int rc = -EIO; |
| 589 | struct in_addr ia; |
| 590 | |
| 591 | ia.s_addr = htonl(ip); |
| 592 | DEBUGP(DMUX, "rtp_socket_bind(rs=%p, IP=%s): ", rs, |
| 593 | inet_ntoa(ia)); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 594 | |
| 595 | /* try to bind to a consecutive pair of ports */ |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 596 | for (next_udp_port = next_udp_port % 0xffff; |
| 597 | next_udp_port < 0xffff; next_udp_port += 2) { |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 598 | rc = rtp_sub_socket_bind(&rs->rtp, ip, next_udp_port); |
| 599 | if (rc != 0) |
| 600 | continue; |
| 601 | |
| 602 | rc = rtp_sub_socket_bind(&rs->rtcp, ip, next_udp_port+1); |
| 603 | if (rc == 0) |
| 604 | break; |
| 605 | } |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 606 | if (rc < 0) { |
| 607 | DEBUGPC(DMUX, "failed\n"); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 608 | return rc; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 609 | } |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 610 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 611 | ia.s_addr = rs->rtp.sin_local.sin_addr.s_addr; |
| 612 | DEBUGPC(DMUX, "BOUND_IP=%s, BOUND_PORT=%u\n", |
| 613 | inet_ntoa(ia), ntohs(rs->rtp.sin_local.sin_port)); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 614 | return ntohs(rs->rtp.sin_local.sin_port); |
| 615 | } |
| 616 | |
| 617 | static int rtp_sub_socket_connect(struct rtp_sub_socket *rss, |
| 618 | u_int32_t ip, u_int16_t port) |
| 619 | { |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 620 | int rc; |
| 621 | socklen_t alen = sizeof(rss->sin_local); |
| 622 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 623 | rss->sin_remote.sin_family = AF_INET; |
| 624 | rss->sin_remote.sin_addr.s_addr = htonl(ip); |
| 625 | rss->sin_remote.sin_port = htons(port); |
| 626 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 627 | rc = connect(rss->bfd.fd, (struct sockaddr *) &rss->sin_remote, |
| 628 | sizeof(rss->sin_remote)); |
| 629 | if (rc < 0) |
| 630 | return rc; |
| 631 | |
| 632 | return getsockname(rss->bfd.fd, (struct sockaddr *)&rss->sin_local, |
| 633 | &alen); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | /* 'connect' a RTP socket to a remote peer */ |
| 637 | int rtp_socket_connect(struct rtp_socket *rs, u_int32_t ip, u_int16_t port) |
| 638 | { |
| 639 | int rc; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 640 | struct in_addr ia; |
| 641 | |
| 642 | ia.s_addr = htonl(ip); |
| 643 | DEBUGP(DMUX, "rtp_socket_connect(rs=%p, ip=%s, port=%u)\n", |
| 644 | rs, inet_ntoa(ia), port); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 645 | |
| 646 | rc = rtp_sub_socket_connect(&rs->rtp, ip, port); |
| 647 | if (rc < 0) |
| 648 | return rc; |
| 649 | |
| 650 | return rtp_sub_socket_connect(&rs->rtcp, ip, port+1); |
| 651 | } |
| 652 | |
| 653 | /* bind two RTP/RTCP sockets together */ |
| 654 | int rtp_socket_proxy(struct rtp_socket *this, struct rtp_socket *other) |
| 655 | { |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 656 | DEBUGP(DMUX, "rtp_socket_proxy(this=%p, other=%p)\n", |
| 657 | this, other); |
| 658 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 659 | this->rx_action = RTP_PROXY; |
| 660 | this->proxy.other_sock = other; |
| 661 | |
| 662 | other->rx_action = RTP_PROXY; |
| 663 | other->proxy.other_sock = this; |
| 664 | |
| 665 | return 0; |
| 666 | } |
| 667 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 668 | /* bind RTP/RTCP socket to application */ |
Harald Welte | 9fb1f10 | 2009-12-20 17:07:23 +0100 | [diff] [blame] | 669 | int rtp_socket_upstream(struct rtp_socket *this, struct gsm_network *net, |
| 670 | u_int32_t callref) |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 671 | { |
Harald Welte | 9fb1f10 | 2009-12-20 17:07:23 +0100 | [diff] [blame] | 672 | DEBUGP(DMUX, "rtp_socket_proxy(this=%p, callref=%u)\n", |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 673 | this, callref); |
| 674 | |
| 675 | if (callref) { |
| 676 | this->rx_action = RTP_RECV_UPSTREAM; |
| 677 | this->receive.net = net; |
| 678 | this->receive.callref = callref; |
| 679 | } else |
| 680 | this->rx_action = RTP_NONE; |
| 681 | |
| 682 | return 0; |
| 683 | } |
| 684 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 685 | static void free_tx_queue(struct rtp_sub_socket *rss) |
| 686 | { |
| 687 | struct msgb *msg; |
| 688 | |
| 689 | while ((msg = msgb_dequeue(&rss->tx_queue))) |
| 690 | msgb_free(msg); |
| 691 | } |
| 692 | |
| 693 | int rtp_socket_free(struct rtp_socket *rs) |
| 694 | { |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 695 | DEBUGP(DMUX, "rtp_socket_free(rs=%p)\n", rs); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 696 | |
| 697 | /* make sure we don't leave references dangling to us */ |
| 698 | if (rs->rx_action == RTP_PROXY && |
| 699 | rs->proxy.other_sock) |
| 700 | rs->proxy.other_sock->proxy.other_sock = NULL; |
| 701 | |
| 702 | bsc_unregister_fd(&rs->rtp.bfd); |
| 703 | close(rs->rtp.bfd.fd); |
| 704 | free_tx_queue(&rs->rtp); |
| 705 | |
| 706 | bsc_unregister_fd(&rs->rtcp.bfd); |
| 707 | close(rs->rtcp.bfd.fd); |
| 708 | free_tx_queue(&rs->rtcp); |
| 709 | |
| 710 | talloc_free(rs); |
| 711 | |
| 712 | return 0; |
| 713 | } |