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