Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | /* The protocol implementation */ |
| 3 | |
| 4 | /* |
| 5 | * (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2009-2010 by On-Waves |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <unistd.h> |
| 28 | #include <endian.h> |
| 29 | #include <errno.h> |
| 30 | |
| 31 | #include <sys/socket.h> |
| 32 | #include <arpa/inet.h> |
| 33 | |
| 34 | #include <mgcp/mgcp.h> |
| 35 | #include <mgcp/mgcp_internal.h> |
| 36 | |
Holger Hans Peter Freyther | cbf7d18 | 2010-07-31 05:25:35 +0800 | [diff] [blame] | 37 | #include <cellmgr_debug.h> |
| 38 | |
| 39 | #include <osmocore/msgb.h> |
| 40 | #include <osmocore/talloc.h> |
| 41 | #include <osmocore/select.h> |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 42 | |
| 43 | #warning "Make use of the rtp proxy code" |
| 44 | |
| 45 | /* according to rtp_proxy.c RFC 3550 */ |
| 46 | struct rtp_hdr { |
| 47 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
Holger Hans Peter Freyther | 5aa1701 | 2010-07-31 04:37:26 +0800 | [diff] [blame] | 48 | uint8_t csrc_count:4, |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 49 | extension:1, |
| 50 | padding:1, |
| 51 | version:2; |
Holger Hans Peter Freyther | 5aa1701 | 2010-07-31 04:37:26 +0800 | [diff] [blame] | 52 | uint8_t payload_type:7, |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 53 | marker:1; |
| 54 | #elif __BYTE_ORDER == __BIG_ENDIAN |
Holger Hans Peter Freyther | 5aa1701 | 2010-07-31 04:37:26 +0800 | [diff] [blame] | 55 | uint8_t version:2, |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 56 | padding:1, |
| 57 | extension:1, |
| 58 | csrc_count:4; |
Holger Hans Peter Freyther | 5aa1701 | 2010-07-31 04:37:26 +0800 | [diff] [blame] | 59 | uint8_t marker:1, |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 60 | payload_type:7; |
| 61 | #endif |
Holger Hans Peter Freyther | 585f3d9 | 2010-07-31 04:38:17 +0800 | [diff] [blame] | 62 | uint16_t sequence; |
Holger Hans Peter Freyther | 9ed3e1b | 2010-07-31 05:22:56 +0800 | [diff] [blame] | 63 | uint32_t timestamp; |
| 64 | uint32_t ssrc; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 65 | } __attribute__((packed)); |
| 66 | |
| 67 | |
| 68 | enum { |
| 69 | DEST_NETWORK = 0, |
| 70 | DEST_BTS = 1, |
| 71 | }; |
| 72 | |
| 73 | enum { |
| 74 | PROTO_RTP, |
| 75 | PROTO_RTCP, |
| 76 | }; |
| 77 | |
| 78 | #define DUMMY_LOAD 0x23 |
| 79 | |
| 80 | |
| 81 | static int udp_send(int fd, struct in_addr *addr, int port, char *buf, int len) |
| 82 | { |
| 83 | struct sockaddr_in out; |
| 84 | out.sin_family = AF_INET; |
| 85 | out.sin_port = port; |
| 86 | memcpy(&out.sin_addr, addr, sizeof(*addr)); |
| 87 | |
| 88 | return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out)); |
| 89 | } |
| 90 | |
| 91 | int mgcp_send_dummy(struct mgcp_endpoint *endp) |
| 92 | { |
| 93 | static char buf[] = { DUMMY_LOAD }; |
| 94 | |
| 95 | return udp_send(endp->local_rtp.fd, &endp->remote, |
| 96 | endp->net_rtp, buf, 1); |
| 97 | } |
| 98 | |
Holger Hans Peter Freyther | d86208d | 2010-08-04 06:11:27 +0800 | [diff] [blame^] | 99 | static void patch_and_count(struct mgcp_rtp_state *state, int payload, char *data, int len) |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 100 | { |
Holger Hans Peter Freyther | d86208d | 2010-08-04 06:11:27 +0800 | [diff] [blame^] | 101 | uint16_t seq; |
| 102 | uint32_t timestamp; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 103 | struct rtp_hdr *rtp_hdr; |
| 104 | |
| 105 | if (len < sizeof(*rtp_hdr)) |
| 106 | return; |
| 107 | |
Holger Hans Peter Freyther | d86208d | 2010-08-04 06:11:27 +0800 | [diff] [blame^] | 108 | rtp_hdr = (struct rtp_hdr *) data; |
| 109 | seq = ntohs(rtp_hdr->sequence); |
| 110 | timestamp = ntohl(rtp_hdr->timestamp); |
| 111 | |
| 112 | if (!state->initialized) { |
| 113 | state->seq_no = seq - 1; |
| 114 | state->ssrc = state->orig_ssrc = rtp_hdr->ssrc; |
| 115 | state->initialized = 1; |
| 116 | state->last_timestamp = timestamp; |
| 117 | } else if (state->ssrc != rtp_hdr->ssrc) { |
| 118 | state->ssrc = rtp_hdr->ssrc; |
| 119 | state->seq_offset = (state->seq_no + 1) - seq; |
| 120 | state->timestamp_offset = state->last_timestamp - timestamp; |
| 121 | state->patch = 1; |
| 122 | LOGP(DMGCP, LOGL_NOTICE, "The SSRC changed... SSRC: %u offset: %d\n", |
| 123 | state->ssrc, state->seq_offset); |
| 124 | } |
| 125 | |
| 126 | /* apply the offset and store it back to the packet */ |
| 127 | if (state->patch) { |
| 128 | seq += state->seq_offset; |
| 129 | rtp_hdr->sequence = htons(seq); |
| 130 | rtp_hdr->ssrc = state->orig_ssrc; |
| 131 | |
| 132 | timestamp += state->timestamp_offset; |
| 133 | rtp_hdr->timestamp = htonl(timestamp); |
| 134 | } |
| 135 | |
| 136 | /* seq changed, now compare if we have lost something */ |
| 137 | if (state->seq_no + 1u != seq) |
| 138 | state->lost_no = abs(seq - (state->seq_no + 1)); |
| 139 | state->seq_no = seq; |
| 140 | |
| 141 | state->last_timestamp = timestamp; |
| 142 | |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 143 | if (payload < 0) |
| 144 | return; |
| 145 | |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 146 | rtp_hdr->payload_type = payload; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * There is data coming. We will have to figure out if it |
| 151 | * came from the BTS or the MediaGateway of the MSC. On top |
| 152 | * of that we need to figure out if it was RTP or RTCP. |
| 153 | * |
| 154 | * Currently we do not communicate with the BSC so we have |
| 155 | * no idea where the BTS is listening for RTP and need to |
| 156 | * do the classic routing trick. Wait for the first packet |
| 157 | * from the BTS and then go ahead. |
| 158 | */ |
| 159 | static int rtp_data_cb(struct bsc_fd *fd, unsigned int what) |
| 160 | { |
| 161 | char buf[4096]; |
| 162 | struct sockaddr_in addr; |
| 163 | socklen_t slen = sizeof(addr); |
| 164 | struct mgcp_endpoint *endp; |
| 165 | struct mgcp_config *cfg; |
| 166 | int rc, dest, proto; |
| 167 | |
| 168 | endp = (struct mgcp_endpoint *) fd->data; |
| 169 | cfg = endp->cfg; |
| 170 | |
| 171 | rc = recvfrom(fd->fd, &buf, sizeof(buf), 0, |
| 172 | (struct sockaddr *) &addr, &slen); |
| 173 | if (rc < 0) { |
| 174 | LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n", |
| 175 | ENDPOINT_NUMBER(endp), errno, strerror(errno)); |
| 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | /* do not forward aynthing... maybe there is a packet from the bts */ |
Holger Hans Peter Freyther | d86208d | 2010-08-04 06:11:27 +0800 | [diff] [blame^] | 180 | if (endp->ci == CI_UNUSED) |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 181 | return -1; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 182 | |
| 183 | /* |
| 184 | * Figure out where to forward it to. This code assumes that we |
| 185 | * have received the Connection Modify and know who is a legitimate |
| 186 | * partner. According to the spec we could attempt to forward even |
| 187 | * after the Create Connection but we will not as we are not really |
| 188 | * able to tell if this is legitimate. |
| 189 | */ |
| 190 | #warning "Slight spec violation. With connection mode recvonly we should attempt to forward." |
| 191 | dest = memcmp(&addr.sin_addr, &endp->remote, sizeof(addr.sin_addr)) == 0 && |
Holger Hans Peter Freyther | d86208d | 2010-08-04 06:11:27 +0800 | [diff] [blame^] | 192 | (endp->net_rtp == addr.sin_port || endp->net_rtcp == addr.sin_port) |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 193 | ? DEST_BTS : DEST_NETWORK; |
| 194 | proto = fd == &endp->local_rtp ? PROTO_RTP : PROTO_RTCP; |
| 195 | |
| 196 | /* We have no idea who called us, maybe it is the BTS. */ |
| 197 | if (dest == DEST_NETWORK && (endp->bts_rtp == 0 || cfg->forward_ip)) { |
| 198 | /* it was the BTS... */ |
| 199 | if (!cfg->bts_ip |
| 200 | || memcmp(&addr.sin_addr, &cfg->bts_in, sizeof(cfg->bts_in)) == 0 |
| 201 | || memcmp(&addr.sin_addr, &endp->bts, sizeof(endp->bts)) == 0) { |
| 202 | if (fd == &endp->local_rtp) { |
| 203 | endp->bts_rtp = addr.sin_port; |
| 204 | } else { |
| 205 | endp->bts_rtcp = addr.sin_port; |
| 206 | } |
| 207 | |
| 208 | endp->bts = addr.sin_addr; |
| 209 | LOGP(DMGCP, LOGL_NOTICE, "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n", |
| 210 | ENDPOINT_NUMBER(endp), ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp), |
| 211 | inet_ntoa(addr.sin_addr)); |
| 212 | |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /* throw away the dummy message */ |
| 217 | if (rc == 1 && buf[0] == DUMMY_LOAD) { |
| 218 | LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy on 0x%x\n", |
| 219 | ENDPOINT_NUMBER(endp)); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* do this before the loop handling */ |
| 224 | if (dest == DEST_NETWORK) |
| 225 | ++endp->in_bts; |
| 226 | else |
| 227 | ++endp->in_remote; |
| 228 | |
| 229 | /* For loop toggle the destination and then dispatch. */ |
| 230 | if (cfg->audio_loop) |
| 231 | dest = !dest; |
| 232 | |
Holger Hans Peter Freyther | d86208d | 2010-08-04 06:11:27 +0800 | [diff] [blame^] | 233 | /* Loop based on the conn_mode, maybe undoing the above */ |
| 234 | if (endp->conn_mode == MGCP_CONN_LOOPBACK) |
| 235 | dest = !dest; |
| 236 | |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 237 | if (dest == DEST_NETWORK) { |
| 238 | if (proto == PROTO_RTP) |
Holger Hans Peter Freyther | d86208d | 2010-08-04 06:11:27 +0800 | [diff] [blame^] | 239 | patch_and_count(&endp->bts_state, |
| 240 | endp->net_payload_type, buf, rc); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 241 | return udp_send(fd->fd, &endp->remote, |
| 242 | proto == PROTO_RTP ? endp->net_rtp : endp->net_rtcp, |
| 243 | buf, rc); |
| 244 | } else { |
| 245 | if (proto == PROTO_RTP) |
Holger Hans Peter Freyther | d86208d | 2010-08-04 06:11:27 +0800 | [diff] [blame^] | 246 | patch_and_count(&endp->net_state, |
| 247 | endp->bts_payload_type, buf, rc); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 248 | return udp_send(fd->fd, &endp->bts, |
| 249 | proto == PROTO_RTP ? endp->bts_rtp : endp->bts_rtcp, |
| 250 | buf, rc); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | static int create_bind(const char *source_addr, struct bsc_fd *fd, int port) |
| 255 | { |
| 256 | struct sockaddr_in addr; |
| 257 | int on = 1; |
| 258 | |
| 259 | fd->fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 260 | if (fd->fd < 0) { |
| 261 | LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n"); |
| 262 | return -1; |
| 263 | } |
| 264 | |
| 265 | setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 266 | memset(&addr, 0, sizeof(addr)); |
| 267 | addr.sin_family = AF_INET; |
| 268 | addr.sin_port = htons(port); |
| 269 | inet_aton(source_addr, &addr.sin_addr); |
| 270 | |
| 271 | if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 272 | return -1; |
| 273 | } |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static int set_ip_tos(int fd, int tos) |
| 279 | { |
| 280 | int ret; |
| 281 | ret = setsockopt(fd, IPPROTO_IP, IP_TOS, |
| 282 | &tos, sizeof(tos)); |
| 283 | return ret != 0; |
| 284 | } |
| 285 | |
| 286 | static int bind_rtp(struct mgcp_endpoint *endp) |
| 287 | { |
| 288 | struct mgcp_config *cfg = endp->cfg; |
| 289 | |
| 290 | if (create_bind(cfg->source_addr, &endp->local_rtp, endp->rtp_port) != 0) { |
| 291 | LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n", |
| 292 | cfg->source_addr, endp->rtp_port, ENDPOINT_NUMBER(endp)); |
| 293 | goto cleanup0; |
| 294 | } |
| 295 | |
| 296 | if (create_bind(cfg->source_addr, &endp->local_rtcp, endp->rtp_port + 1) != 0) { |
| 297 | LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n", |
| 298 | cfg->source_addr, endp->rtp_port + 1, ENDPOINT_NUMBER(endp)); |
| 299 | goto cleanup1; |
| 300 | } |
| 301 | |
| 302 | set_ip_tos(endp->local_rtp.fd, cfg->endp_dscp); |
| 303 | set_ip_tos(endp->local_rtcp.fd, cfg->endp_dscp); |
| 304 | |
| 305 | endp->local_rtp.cb = rtp_data_cb; |
| 306 | endp->local_rtp.data = endp; |
| 307 | endp->local_rtp.when = BSC_FD_READ; |
| 308 | if (bsc_register_fd(&endp->local_rtp) != 0) { |
| 309 | LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n", |
| 310 | endp->rtp_port, ENDPOINT_NUMBER(endp)); |
| 311 | goto cleanup2; |
| 312 | } |
| 313 | |
| 314 | endp->local_rtcp.cb = rtp_data_cb; |
| 315 | endp->local_rtcp.data = endp; |
| 316 | endp->local_rtcp.when = BSC_FD_READ; |
| 317 | if (bsc_register_fd(&endp->local_rtcp) != 0) { |
| 318 | LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n", |
| 319 | endp->rtp_port + 1, ENDPOINT_NUMBER(endp)); |
| 320 | goto cleanup3; |
| 321 | } |
| 322 | |
| 323 | return 0; |
| 324 | |
| 325 | cleanup3: |
| 326 | bsc_unregister_fd(&endp->local_rtp); |
| 327 | cleanup2: |
| 328 | close(endp->local_rtcp.fd); |
| 329 | endp->local_rtcp.fd = -1; |
| 330 | cleanup1: |
| 331 | close(endp->local_rtp.fd); |
| 332 | endp->local_rtp.fd = -1; |
| 333 | cleanup0: |
| 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | int mgcp_bind_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 338 | { |
| 339 | endp->rtp_port = rtp_port; |
| 340 | return bind_rtp(endp); |
| 341 | } |