Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 1 | /* RTP proxy handling for ip.access nanoBTS */ |
| 2 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 3 | /* (C) 2009-2013 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 7 | * 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 Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 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 |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 14 | * GNU Affero General Public License for more details. |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 15 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * 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 Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <unistd.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <netinet/in.h> |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 25 | #include <arpa/inet.h> |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 26 | #include <sys/time.h> /* gettimeofday() */ |
| 27 | #include <unistd.h> /* get..() */ |
| 28 | #include <time.h> /* clock() */ |
| 29 | #include <sys/utsname.h> /* uname() */ |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 30 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 31 | #include <osmocom/core/talloc.h> |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 32 | #include <openbsc/gsm_data.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 33 | #include <osmocom/core/msgb.h> |
| 34 | #include <osmocom/core/select.h> |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 35 | #include <openbsc/debug.h> |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 36 | #include <openbsc/rtp_proxy.h> |
Harald Welte | f142c97 | 2011-05-24 13:25:38 +0200 | [diff] [blame] | 37 | #include <openbsc/mncc.h> |
Alexander Huemer | 0a65461 | 2011-09-06 00:09:49 +0200 | [diff] [blame] | 38 | #include <openbsc/trau_upqueue.h> |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 39 | |
Holger Hans Peter Freyther | 3cb2879 | 2010-10-12 15:39:46 +0200 | [diff] [blame] | 40 | /* attempt to determine byte order */ |
Holger Hans Peter Freyther | 3cb2879 | 2010-10-12 15:39:46 +0200 | [diff] [blame] | 41 | #include <sys/param.h> |
| 42 | #include <limits.h> |
| 43 | |
| 44 | #ifndef __BYTE_ORDER |
Tobias Engel | aff2071 | 2012-10-24 17:53:50 +0200 | [diff] [blame] | 45 | # 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 Freyther | 3cb2879 | 2010-10-12 15:39:46 +0200 | [diff] [blame] | 52 | #endif |
| 53 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 54 | static LLIST_HEAD(rtp_sockets); |
| 55 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 56 | /* should we mangle the CNAME inside SDES of RTCP packets? We disable |
| 57 | * this by default, as it seems to be not needed */ |
| 58 | static int mangle_rtcp_cname = 0; |
| 59 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 60 | enum 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 Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 68 | /* according to RFC 1889 */ |
| 69 | struct rtcp_hdr { |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 70 | uint8_t byte0; |
| 71 | uint8_t type; |
| 72 | uint16_t length; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 73 | } __attribute__((packed)); |
| 74 | |
| 75 | #define RTCP_TYPE_SDES 202 |
| 76 | |
| 77 | #define RTCP_IE_CNAME 1 |
| 78 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 79 | /* according to RFC 3550 */ |
| 80 | struct rtp_hdr { |
Holger Hans Peter Freyther | 2890d10 | 2010-02-26 13:12:41 +0100 | [diff] [blame] | 81 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 82 | uint8_t csrc_count:4, |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 83 | extension:1, |
| 84 | padding:1, |
| 85 | version:2; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 86 | uint8_t payload_type:7, |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 87 | marker:1; |
Holger Hans Peter Freyther | 2890d10 | 2010-02-26 13:12:41 +0100 | [diff] [blame] | 88 | #elif __BYTE_ORDER == __BIG_ENDIAN |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 89 | uint8_t version:2, |
Holger Hans Peter Freyther | 2890d10 | 2010-02-26 13:12:41 +0100 | [diff] [blame] | 90 | padding:1, |
| 91 | extension:1, |
| 92 | csrc_count:4; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 93 | uint8_t marker:1, |
Holger Hans Peter Freyther | 2890d10 | 2010-02-26 13:12:41 +0100 | [diff] [blame] | 94 | payload_type:7; |
| 95 | #endif |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 96 | uint16_t sequence; |
| 97 | uint32_t timestamp; |
| 98 | uint32_t ssrc; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 99 | } __attribute__((packed)); |
| 100 | |
| 101 | struct rtp_x_hdr { |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 102 | uint16_t by_profile; |
| 103 | uint16_t length; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 104 | } __attribute__((packed)); |
| 105 | |
| 106 | #define RTP_VERSION 2 |
| 107 | |
Harald Welte | a87f8f9 | 2014-05-17 13:43:01 +0200 | [diff] [blame] | 108 | /* 33 for FR, all other codecs have smaller size */ |
| 109 | #define MAX_RTP_PAYLOAD_LEN 33 |
| 110 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 111 | /* decode an rtp frame and create a new buffer with payload */ |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 112 | static int rtp_decode(struct msgb *msg, uint32_t callref, struct msgb **data) |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 113 | { |
| 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 Welte | a87f8f9 | 2014-05-17 13:43:01 +0200 | [diff] [blame] | 118 | uint8_t *payload, *payload_out; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 119 | int payload_len; |
| 120 | int msg_type; |
| 121 | int x_len; |
| 122 | |
| 123 | if (msg->len < 12) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 124 | DEBUGPC(DLMUX, "received RTP frame too short (len = %d)\n", |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 125 | msg->len); |
| 126 | return -EINVAL; |
| 127 | } |
| 128 | if (rtph->version != RTP_VERSION) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 129 | DEBUGPC(DLMUX, "received RTP version %d not supported.\n", |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 130 | 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 Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 136 | DEBUGPC(DLMUX, "received RTP frame too short (len = %d, " |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 137 | "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 Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 142 | DEBUGPC(DLMUX, "received RTP frame too short for " |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 143 | "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 Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 151 | DEBUGPC(DLMUX, "received RTP frame too short, " |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 152 | "extension header exceeds frame length\n"); |
| 153 | return -EINVAL; |
| 154 | } |
| 155 | } |
| 156 | if (rtph->padding) { |
Jacob Erlbeck | d9e4039 | 2013-11-21 19:05:42 +0100 | [diff] [blame] | 157 | if (payload_len < 1) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 158 | DEBUGPC(DLMUX, "received RTP frame too short for " |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 159 | "padding length\n"); |
| 160 | return -EINVAL; |
| 161 | } |
| 162 | payload_len -= payload[payload_len - 1]; |
| 163 | if (payload_len < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 164 | DEBUGPC(DLMUX, "received RTP frame with padding " |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 165 | "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 Eversberg | 88012b6 | 2014-01-22 10:05:51 +0100 | [diff] [blame] | 173 | if (payload_len != RTP_LEN_GSM_FULL) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 174 | DEBUGPC(DLMUX, "received RTP full rate frame with " |
Andreas Eversberg | 88012b6 | 2014-01-22 10:05:51 +0100 | [diff] [blame] | 175 | "payload length != %d (len = %d)\n", |
| 176 | RTP_LEN_GSM_FULL, payload_len); |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 177 | return -EINVAL; |
| 178 | } |
| 179 | break; |
Harald Welte | aca8f15 | 2009-12-19 23:06:41 +0100 | [diff] [blame] | 180 | case RTP_PT_GSM_EFR: |
| 181 | msg_type = GSM_TCHF_FRAME_EFR; |
Andreas Eversberg | 88012b6 | 2014-01-22 10:05:51 +0100 | [diff] [blame] | 182 | 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 Welte | aca8f15 | 2009-12-19 23:06:41 +0100 | [diff] [blame] | 188 | break; |
Andreas Eversberg | 63bfdd8 | 2014-01-17 19:06:38 +0100 | [diff] [blame] | 189 | 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 Eversberg | d8967f7 | 2012-03-08 14:39:19 +0100 | [diff] [blame] | 198 | case RTP_PT_AMR: |
Harald Welte | ec75798 | 2014-05-18 18:32:05 +0200 | [diff] [blame] | 199 | msg_type = GSM_TCH_FRAME_AMR; |
Andreas Eversberg | d8967f7 | 2012-03-08 14:39:19 +0100 | [diff] [blame] | 200 | break; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 201 | default: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 202 | DEBUGPC(DLMUX, "received RTP frame with unknown payload " |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 203 | "type %d\n", rtph->payload_type); |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | |
Harald Welte | a87f8f9 | 2014-05-17 13:43:01 +0200 | [diff] [blame] | 207 | if (payload_len > MAX_RTP_PAYLOAD_LEN) { |
Andreas Eversberg | d8967f7 | 2012-03-08 14:39:19 +0100 | [diff] [blame] | 208 | DEBUGPC(DLMUX, "RTP payload too large (%d octets)\n", |
| 209 | payload_len); |
| 210 | return -EINVAL; |
| 211 | } |
| 212 | |
Harald Welte | a87f8f9 | 2014-05-17 13:43:01 +0200 | [diff] [blame] | 213 | /* 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 Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 218 | if (!new_msg) |
| 219 | return -ENOMEM; |
Harald Welte | a87f8f9 | 2014-05-17 13:43:01 +0200 | [diff] [blame] | 220 | frame = msgb_put(new_msg, sizeof(struct gsm_data_frame)); |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 221 | frame->msg_type = msg_type; |
| 222 | frame->callref = callref; |
Andreas Eversberg | d8967f7 | 2012-03-08 14:39:19 +0100 | [diff] [blame] | 223 | if (rtph->payload_type == RTP_PT_AMR) { |
Harald Welte | a87f8f9 | 2014-05-17 13:43:01 +0200 | [diff] [blame] | 224 | /* 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 Eversberg | d8967f7 | 2012-03-08 14:39:19 +0100 | [diff] [blame] | 229 | } |
Harald Welte | a87f8f9 | 2014-05-17 13:43:01 +0200 | [diff] [blame] | 230 | payload_out = msgb_put(new_msg, payload_len); |
| 231 | memcpy(payload_out, payload, payload_len); |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 232 | |
| 233 | *data = new_msg; |
| 234 | return 0; |
| 235 | } |
| 236 | |
Harald Welte | 392736d | 2009-12-20 13:16:14 +0100 | [diff] [blame] | 237 | /* "to - from" */ |
| 238 | static 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 Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 252 | /*! \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 Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 256 | int 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 Welte | a87f8f9 | 2014-05-17 13:43:01 +0200 | [diff] [blame] | 261 | uint8_t *payload; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 262 | 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 Eversberg | 88012b6 | 2014-01-22 10:05:51 +0100 | [diff] [blame] | 277 | payload_len = RTP_LEN_GSM_FULL; |
| 278 | duration = RTP_GSM_DURATION; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 279 | break; |
Harald Welte | aca8f15 | 2009-12-19 23:06:41 +0100 | [diff] [blame] | 280 | case GSM_TCHF_FRAME_EFR: |
| 281 | payload_type = RTP_PT_GSM_EFR; |
Andreas Eversberg | 88012b6 | 2014-01-22 10:05:51 +0100 | [diff] [blame] | 282 | payload_len = RTP_LEN_GSM_EFR; |
| 283 | duration = RTP_GSM_DURATION; |
Harald Welte | aca8f15 | 2009-12-19 23:06:41 +0100 | [diff] [blame] | 284 | break; |
Andreas Eversberg | 63bfdd8 | 2014-01-17 19:06:38 +0100 | [diff] [blame] | 285 | 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 Eversberg | d8967f7 | 2012-03-08 14:39:19 +0100 | [diff] [blame] | 290 | 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 Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 295 | default: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 296 | DEBUGPC(DLMUX, "unsupported message type %d\n", |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 297 | frame->msg_type); |
| 298 | return -EINVAL; |
| 299 | } |
| 300 | |
Harald Welte | 392736d | 2009-12-20 13:16:14 +0100 | [diff] [blame] | 301 | { |
| 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 Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 315 | LOGP(DLMUX, LOGL_NOTICE, |
Harald Welte (local) | 9fcf6d7 | 2009-12-27 17:01:40 +0100 | [diff] [blame] | 316 | "Correcting frame difference of %ld frames\n", frame_diff_excess); |
Harald Welte | 392736d | 2009-12-20 13:16:14 +0100 | [diff] [blame] | 317 | rs->transmit.sequence += frame_diff_excess; |
| 318 | rs->transmit.timestamp += frame_diff_excess * duration; |
| 319 | } |
| 320 | } |
| 321 | |
Harald Welte | a87f8f9 | 2014-05-17 13:43:01 +0200 | [diff] [blame] | 322 | if (payload_len > MAX_RTP_PAYLOAD_LEN) { |
Andreas Eversberg | d8967f7 | 2012-03-08 14:39:19 +0100 | [diff] [blame] | 323 | DEBUGPC(DLMUX, "RTP payload too large (%d octets)\n", |
| 324 | payload_len); |
| 325 | return -EINVAL; |
| 326 | } |
| 327 | |
Harald Welte | a87f8f9 | 2014-05-17 13:43:01 +0200 | [diff] [blame] | 328 | msg = msgb_alloc(sizeof(struct rtp_hdr) + payload_len, "RTP-GSM"); |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 329 | if (!msg) |
| 330 | return -ENOMEM; |
Harald Welte | a87f8f9 | 2014-05-17 13:43:01 +0200 | [diff] [blame] | 331 | rtph = (struct rtp_hdr *) msgb_put(msg, sizeof(struct rtp_hdr)); |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 332 | 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 Welte | a87f8f9 | 2014-05-17 13:43:01 +0200 | [diff] [blame] | 342 | |
| 343 | payload = msgb_put(msg, payload_len); |
Andreas Eversberg | d8967f7 | 2012-03-08 14:39:19 +0100 | [diff] [blame] | 344 | if (frame->msg_type == GSM_TCH_FRAME_AMR) |
Harald Welte | a87f8f9 | 2014-05-17 13:43:01 +0200 | [diff] [blame] | 345 | memcpy(payload, frame->data + 1, payload_len); |
Andreas Eversberg | d8967f7 | 2012-03-08 14:39:19 +0100 | [diff] [blame] | 346 | else |
Harald Welte | a87f8f9 | 2014-05-17 13:43:01 +0200 | [diff] [blame] | 347 | memcpy(payload, frame->data, payload_len); |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 348 | msgb_enqueue(&rss->tx_queue, msg); |
| 349 | rss->bfd.when |= BSC_FD_WRITE; |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
Holger Hans Peter Freyther | 89acf06 | 2009-07-29 06:46:26 +0200 | [diff] [blame] | 354 | /* 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] | 355 | * replace all of those with 'new_cname' */ |
| 356 | static int rtcp_sdes_cname_mangle(struct msgb *msg, struct rtcp_hdr *rh, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 357 | uint16_t *rtcp_len, const char *new_cname) |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 358 | { |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 359 | uint8_t *rtcp_end; |
| 360 | uint8_t *cur = (uint8_t *) rh; |
| 361 | uint8_t tag, len = 0; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 362 | |
| 363 | rtcp_end = cur + *rtcp_len; |
| 364 | /* move cur to end of RTP header */ |
Harald Welte | 198f3f5 | 2009-07-29 10:46:41 +0200 | [diff] [blame] | 365 | cur += sizeof(*rh); |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 366 | |
| 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 Willmann | 45fcb85 | 2014-05-21 15:46:43 +0200 | [diff] [blame] | 377 | while ((*cur++ == 0) && (cur < rtcp_end)) { } |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 378 | 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 | |
| 410 | static 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 Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 414 | uint16_t old_len; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 415 | int rc; |
| 416 | |
| 417 | if (!mangle_rtcp_cname) |
| 418 | return 0; |
| 419 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 420 | printf("RTCP\n"); |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 421 | /* iterate over list of RTCP messages */ |
| 422 | rtph = (struct rtcp_hdr *)msg->data; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 423 | while ((void *)rtph + sizeof(*rtph) <= (void *)msg->data + msg->len) { |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 424 | old_len = (ntohs(rtph->length) + 1) * 4; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 425 | if ((void *)rtph + old_len > (void *)msg->data + msg->len) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 426 | DEBUGPC(DLMUX, "received RTCP packet too short for " |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 427 | "length element\n"); |
| 428 | return -EINVAL; |
| 429 | } |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 430 | 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 Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 446 | /* read from incoming RTP/RTCP socket */ |
| 447 | static 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 Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 451 | struct msgb *new_msg; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 452 | 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 Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 467 | if (!rs->proxy.other_sock) { |
| 468 | rc = -EIO; |
| 469 | goto out_free; |
| 470 | } |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 471 | if (rss->bfd.priv_nr == RTP_PRIV_RTP) |
| 472 | other_rss = &rs->proxy.other_sock->rtp; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 473 | else if (rss->bfd.priv_nr == RTP_PRIV_RTCP) { |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 474 | other_rss = &rs->proxy.other_sock->rtcp; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 475 | /* 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 Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 482 | } |
| 483 | msgb_enqueue(&other_rss->tx_queue, msg); |
| 484 | other_rss->bfd.when |= BSC_FD_WRITE; |
| 485 | break; |
Holger Hans Peter Freyther | 1ddb356 | 2009-08-10 07:57:13 +0200 | [diff] [blame] | 486 | |
| 487 | case RTP_RECV_UPSTREAM: |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 488 | 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 Welte | 31c00f7 | 2011-03-03 23:29:05 +0100 | [diff] [blame] | 513 | trau_tx_to_mncc(rs->receive.net, new_msg); |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 514 | break; |
| 515 | |
| 516 | case RTP_NONE: /* if socket exists, but disabled by app */ |
| 517 | msgb_free(msg); |
Holger Hans Peter Freyther | 1ddb356 | 2009-08-10 07:57:13 +0200 | [diff] [blame] | 518 | break; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 519 | } |
| 520 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 521 | return 0; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 522 | |
| 523 | out_free: |
| 524 | msgb_free(msg); |
| 525 | return rc; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 526 | } |
| 527 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 528 | /* \brief write from tx_queue to RTP/RTCP socket */ |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 529 | static 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 Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 542 | LOGP(DLMIB, LOGL_ERROR, "short write"); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 543 | msgb_free(msg); |
| 544 | return -EIO; |
| 545 | } |
| 546 | |
| 547 | msgb_free(msg); |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 553 | /*! \brief callback for the select.c:bfd_* layer */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 554 | static int rtp_bfd_cb(struct osmo_fd *bfd, unsigned int flags) |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 555 | { |
| 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 Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 579 | /*! \brief initialize one rtp sub-socket */ |
Holger Hans Peter Freyther | acf8a0c | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 580 | static void init_rss(struct rtp_sub_socket *rss, |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 581 | 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 Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 590 | /*! \brief create a new RTP/RTCP socket and bind it */ |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 591 | struct rtp_socket *rtp_socket_create(void) |
| 592 | { |
| 593 | int rc; |
| 594 | struct rtp_socket *rs; |
| 595 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 596 | DEBUGP(DLMUX, "rtp_socket_create(): "); |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 597 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 598 | 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 Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 610 | rc = osmo_fd_register(&rs->rtp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 611 | 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 Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 619 | rc = osmo_fd_register(&rs->rtcp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 620 | if (rc < 0) |
| 621 | goto out_rtcp_socket; |
| 622 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 623 | DEBUGPC(DLMUX, "success\n"); |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 624 | |
| 625 | rc = rtp_socket_bind(rs, INADDR_ANY); |
| 626 | if (rc < 0) |
| 627 | goto out_rtcp_bfd; |
| 628 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 629 | return rs; |
| 630 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 631 | out_rtcp_bfd: |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 632 | osmo_fd_unregister(&rs->rtcp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 633 | out_rtcp_socket: |
| 634 | close(rs->rtcp.bfd.fd); |
| 635 | out_rtp_bfd: |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 636 | osmo_fd_unregister(&rs->rtp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 637 | out_rtp_socket: |
| 638 | close(rs->rtp.bfd.fd); |
| 639 | out_free: |
| 640 | talloc_free(rs); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 641 | DEBUGPC(DLMUX, "failed\n"); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 642 | return NULL; |
| 643 | } |
| 644 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 645 | static int rtp_sub_socket_bind(struct rtp_sub_socket *rss, uint32_t ip, |
| 646 | uint16_t port) |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 647 | { |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 648 | int rc; |
| 649 | socklen_t alen = sizeof(rss->sin_local); |
| 650 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 651 | 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 Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 656 | 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 Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | #define RTP_PORT_BASE 30000 |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 668 | static unsigned int next_udp_port = RTP_PORT_BASE; |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 669 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 670 | /*! \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 Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 674 | int rtp_socket_bind(struct rtp_socket *rs, uint32_t ip) |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 675 | { |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 676 | int rc = -EIO; |
| 677 | struct in_addr ia; |
| 678 | |
| 679 | ia.s_addr = htonl(ip); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 680 | DEBUGP(DLMUX, "rtp_socket_bind(rs=%p, IP=%s): ", rs, |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 681 | inet_ntoa(ia)); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 682 | |
| 683 | /* try to bind to a consecutive pair of ports */ |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 684 | for (next_udp_port = next_udp_port % 0xffff; |
| 685 | next_udp_port < 0xffff; next_udp_port += 2) { |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 686 | 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 Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 694 | if (rc < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 695 | DEBUGPC(DLMUX, "failed\n"); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 696 | return rc; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 697 | } |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 698 | |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 699 | ia.s_addr = rs->rtp.sin_local.sin_addr.s_addr; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 700 | DEBUGPC(DLMUX, "BOUND_IP=%s, BOUND_PORT=%u\n", |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 701 | inet_ntoa(ia), ntohs(rs->rtp.sin_local.sin_port)); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 702 | return ntohs(rs->rtp.sin_local.sin_port); |
| 703 | } |
| 704 | |
| 705 | static int rtp_sub_socket_connect(struct rtp_sub_socket *rss, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 706 | uint32_t ip, uint16_t port) |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 707 | { |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 708 | int rc; |
| 709 | socklen_t alen = sizeof(rss->sin_local); |
| 710 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 711 | 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 Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 715 | 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 Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 722 | } |
| 723 | |
Harald Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 724 | /*! \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 Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 729 | int rtp_socket_connect(struct rtp_socket *rs, uint32_t ip, uint16_t port) |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 730 | { |
| 731 | int rc; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 732 | struct in_addr ia; |
| 733 | |
| 734 | ia.s_addr = htonl(ip); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 735 | DEBUGP(DLMUX, "rtp_socket_connect(rs=%p, ip=%s, port=%u)\n", |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 736 | rs, inet_ntoa(ia), port); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 737 | |
| 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 Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 745 | /*! \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 Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 749 | int rtp_socket_proxy(struct rtp_socket *this, struct rtp_socket *other) |
| 750 | { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 751 | DEBUGP(DLMUX, "rtp_socket_proxy(this=%p, other=%p)\n", |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 752 | this, other); |
| 753 | |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 754 | 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 Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 763 | /*! \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 Welte | 9fb1f10 | 2009-12-20 17:07:23 +0100 | [diff] [blame] | 768 | int rtp_socket_upstream(struct rtp_socket *this, struct gsm_network *net, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 769 | uint32_t callref) |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 770 | { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 771 | DEBUGP(DLMUX, "rtp_socket_proxy(this=%p, callref=%u)\n", |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 772 | 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 Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 784 | static 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 Welte | 647db84 | 2013-02-03 12:06:58 +0100 | [diff] [blame] | 792 | /*! \brief Free/release a previously allocated RTP socket |
| 793 | * \param[in[] rs RTP/RTCP socket to be released |
| 794 | */ |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 795 | int rtp_socket_free(struct rtp_socket *rs) |
| 796 | { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 797 | DEBUGP(DLMUX, "rtp_socket_free(rs=%p)\n", rs); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 798 | |
| 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 Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 804 | osmo_fd_unregister(&rs->rtp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 805 | close(rs->rtp.bfd.fd); |
| 806 | free_tx_queue(&rs->rtp); |
| 807 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 808 | osmo_fd_unregister(&rs->rtcp.bfd); |
Harald Welte | ead7a7b | 2009-07-28 00:01:58 +0200 | [diff] [blame] | 809 | close(rs->rtcp.bfd.fd); |
| 810 | free_tx_queue(&rs->rtcp); |
| 811 | |
| 812 | talloc_free(rs); |
| 813 | |
| 814 | return 0; |
| 815 | } |