Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [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> |
Holger Hans Peter Freyther | b715d7f | 2010-05-14 02:14:09 +0800 | [diff] [blame] | 26 | #include <stdlib.h> |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 27 | #include <unistd.h> |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 28 | #include <endian.h> |
Holger Hans Peter Freyther | 575b895 | 2010-04-07 12:55:40 +0200 | [diff] [blame] | 29 | #include <errno.h> |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 30 | |
| 31 | #include <sys/socket.h> |
| 32 | #include <arpa/inet.h> |
| 33 | |
Holger Hans Peter Freyther | 1ebad74 | 2010-02-26 20:16:37 +0100 | [diff] [blame] | 34 | #include <osmocore/msgb.h> |
Holger Hans Peter Freyther | 1ebad74 | 2010-02-26 20:16:37 +0100 | [diff] [blame] | 35 | #include <osmocore/select.h> |
| 36 | |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 37 | #include <openbsc/debug.h> |
| 38 | #include <openbsc/mgcp.h> |
| 39 | #include <openbsc/mgcp_internal.h> |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 40 | |
| 41 | #warning "Make use of the rtp proxy code" |
| 42 | |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 43 | /* according to rtp_proxy.c RFC 3550 */ |
| 44 | struct rtp_hdr { |
| 45 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 46 | u_int8_t csrc_count:4, |
| 47 | extension:1, |
| 48 | padding:1, |
| 49 | version:2; |
| 50 | u_int8_t payload_type:7, |
| 51 | marker:1; |
| 52 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 53 | u_int8_t version:2, |
| 54 | padding:1, |
| 55 | extension:1, |
| 56 | csrc_count:4; |
| 57 | u_int8_t marker:1, |
| 58 | payload_type:7; |
| 59 | #endif |
| 60 | u_int16_t sequence; |
| 61 | u_int32_t timestamp; |
| 62 | u_int32_t ssrc; |
| 63 | } __attribute__((packed)); |
| 64 | |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 65 | |
| 66 | enum { |
| 67 | DEST_NETWORK = 0, |
| 68 | DEST_BTS = 1, |
| 69 | }; |
| 70 | |
| 71 | enum { |
| 72 | PROTO_RTP, |
| 73 | PROTO_RTCP, |
| 74 | }; |
| 75 | |
Holger Hans Peter Freyther | b844b87 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 76 | #define DUMMY_LOAD 0x23 |
| 77 | |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 78 | |
| 79 | static int udp_send(int fd, struct in_addr *addr, int port, char *buf, int len) |
| 80 | { |
| 81 | struct sockaddr_in out; |
| 82 | out.sin_family = AF_INET; |
| 83 | out.sin_port = port; |
| 84 | memcpy(&out.sin_addr, addr, sizeof(*addr)); |
| 85 | |
| 86 | return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out)); |
| 87 | } |
| 88 | |
Holger Hans Peter Freyther | b844b87 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 89 | int mgcp_send_dummy(struct mgcp_endpoint *endp) |
| 90 | { |
| 91 | static char buf[] = { DUMMY_LOAD }; |
| 92 | |
| 93 | return udp_send(endp->local_rtp.fd, &endp->remote, |
| 94 | endp->net_rtp, buf, 1); |
| 95 | } |
| 96 | |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 97 | static void patch_payload(int payload, char *data, int len) |
| 98 | { |
| 99 | struct rtp_hdr *rtp_hdr; |
| 100 | |
| 101 | if (len < sizeof(*rtp_hdr)) |
| 102 | return; |
| 103 | |
Holger Hans Peter Freyther | 7279d24 | 2010-04-06 11:15:50 +0200 | [diff] [blame] | 104 | if (payload < 0) |
| 105 | return; |
| 106 | |
Holger Hans Peter Freyther | 36ed8cc | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 107 | rtp_hdr = (struct rtp_hdr *) data; |
| 108 | rtp_hdr->payload_type = payload; |
| 109 | } |
| 110 | |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 111 | /* |
| 112 | * There is data coming. We will have to figure out if it |
| 113 | * came from the BTS or the MediaGateway of the MSC. On top |
| 114 | * of that we need to figure out if it was RTP or RTCP. |
| 115 | * |
| 116 | * Currently we do not communicate with the BSC so we have |
| 117 | * no idea where the BTS is listening for RTP and need to |
| 118 | * do the classic routing trick. Wait for the first packet |
| 119 | * from the BTS and then go ahead. |
| 120 | */ |
| 121 | static int rtp_data_cb(struct bsc_fd *fd, unsigned int what) |
| 122 | { |
| 123 | char buf[4096]; |
| 124 | struct sockaddr_in addr; |
| 125 | socklen_t slen = sizeof(addr); |
| 126 | struct mgcp_endpoint *endp; |
| 127 | struct mgcp_config *cfg; |
| 128 | int rc, dest, proto; |
| 129 | |
| 130 | endp = (struct mgcp_endpoint *) fd->data; |
| 131 | cfg = endp->cfg; |
| 132 | |
| 133 | rc = recvfrom(fd->fd, &buf, sizeof(buf), 0, |
| 134 | (struct sockaddr *) &addr, &slen); |
| 135 | if (rc < 0) { |
Holger Hans Peter Freyther | 575b895 | 2010-04-07 12:55:40 +0200 | [diff] [blame] | 136 | LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n", |
| 137 | ENDPOINT_NUMBER(endp), errno, strerror(errno)); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | /* do not forward aynthing... maybe there is a packet from the bts */ |
Holger Hans Peter Freyther | 774f072 | 2010-03-01 18:53:05 +0100 | [diff] [blame] | 142 | if (endp->ci == CI_UNUSED) |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 143 | return -1; |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * Figure out where to forward it to. This code assumes that we |
| 147 | * have received the Connection Modify and know who is a legitimate |
| 148 | * partner. According to the spec we could attempt to forward even |
| 149 | * after the Create Connection but we will not as we are not really |
| 150 | * able to tell if this is legitimate. |
| 151 | */ |
| 152 | #warning "Slight spec violation. With connection mode recvonly we should attempt to forward." |
| 153 | dest = memcmp(&addr.sin_addr, &endp->remote, sizeof(addr.sin_addr)) == 0 && |
Holger Hans Peter Freyther | a581136 | 2010-04-30 13:32:05 +0800 | [diff] [blame] | 154 | (endp->net_rtp == addr.sin_port || endp->net_rtcp == addr.sin_port) |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 155 | ? DEST_BTS : DEST_NETWORK; |
| 156 | proto = fd == &endp->local_rtp ? PROTO_RTP : PROTO_RTCP; |
| 157 | |
| 158 | /* We have no idea who called us, maybe it is the BTS. */ |
| 159 | if (dest == DEST_NETWORK && (endp->bts_rtp == 0 || cfg->forward_ip)) { |
| 160 | /* it was the BTS... */ |
Holger Hans Peter Freyther | 8b120f0 | 2010-04-01 07:56:04 +0200 | [diff] [blame] | 161 | if (!cfg->bts_ip |
| 162 | || memcmp(&addr.sin_addr, &cfg->bts_in, sizeof(cfg->bts_in)) == 0 |
| 163 | || memcmp(&addr.sin_addr, &endp->bts, sizeof(endp->bts)) == 0) { |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 164 | if (fd == &endp->local_rtp) { |
| 165 | endp->bts_rtp = addr.sin_port; |
| 166 | } else { |
| 167 | endp->bts_rtcp = addr.sin_port; |
| 168 | } |
| 169 | |
| 170 | endp->bts = addr.sin_addr; |
Holger Hans Peter Freyther | aff596b | 2010-04-09 18:53:01 +0200 | [diff] [blame] | 171 | LOGP(DMGCP, LOGL_NOTICE, "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n", |
| 172 | ENDPOINT_NUMBER(endp), ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp), |
| 173 | inet_ntoa(addr.sin_addr)); |
Holger Hans Peter Freyther | b844b87 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 174 | |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Holger Hans Peter Freyther | b3c206a | 2010-05-14 02:18:59 +0800 | [diff] [blame] | 178 | /* throw away the dummy message */ |
Holger Hans Peter Freyther | aa9d3ce | 2010-04-22 12:14:51 +0800 | [diff] [blame] | 179 | if (rc == 1 && buf[0] == DUMMY_LOAD) { |
| 180 | LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy on 0x%x\n", |
| 181 | ENDPOINT_NUMBER(endp)); |
| 182 | return 0; |
| 183 | } |
| 184 | |
Holger Hans Peter Freyther | b4b135e | 2010-04-07 09:37:17 +0200 | [diff] [blame] | 185 | /* do this before the loop handling */ |
| 186 | if (dest == DEST_NETWORK) |
| 187 | ++endp->in_bts; |
| 188 | else |
| 189 | ++endp->in_remote; |
| 190 | |
Holger Hans Peter Freyther | b3c206a | 2010-05-14 02:18:59 +0800 | [diff] [blame] | 191 | /* For loop toggle the destination and then dispatch. */ |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 192 | if (cfg->audio_loop) |
| 193 | dest = !dest; |
| 194 | |
| 195 | if (dest == DEST_NETWORK) { |
Holger Hans Peter Freyther | a19bdab | 2010-05-22 22:06:13 +0800 | [diff] [blame] | 196 | if (proto == PROTO_RTP) |
| 197 | patch_payload(endp->net_payload_type, buf, rc); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 198 | return udp_send(fd->fd, &endp->remote, |
| 199 | proto == PROTO_RTP ? endp->net_rtp : endp->net_rtcp, |
| 200 | buf, rc); |
| 201 | } else { |
Holger Hans Peter Freyther | a19bdab | 2010-05-22 22:06:13 +0800 | [diff] [blame] | 202 | if (proto == PROTO_RTP) |
| 203 | patch_payload(endp->bts_payload_type, buf, rc); |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 204 | return udp_send(fd->fd, &endp->bts, |
| 205 | proto == PROTO_RTP ? endp->bts_rtp : endp->bts_rtcp, |
| 206 | buf, rc); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | static int create_bind(const char *source_addr, struct bsc_fd *fd, int port) |
| 211 | { |
| 212 | struct sockaddr_in addr; |
| 213 | int on = 1; |
| 214 | |
| 215 | fd->fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 216 | if (fd->fd < 0) { |
| 217 | LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n"); |
| 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 222 | memset(&addr, 0, sizeof(addr)); |
| 223 | addr.sin_family = AF_INET; |
| 224 | addr.sin_port = htons(port); |
| 225 | inet_aton(source_addr, &addr.sin_addr); |
| 226 | |
| 227 | if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 228 | return -1; |
| 229 | } |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Holger Hans Peter Freyther | 75492e6 | 2010-05-31 10:22:00 +0800 | [diff] [blame] | 234 | static int set_ip_tos(int fd, int tos) |
| 235 | { |
| 236 | int ret; |
| 237 | ret = setsockopt(fd, IPPROTO_IP, IP_TOS, |
| 238 | &tos, sizeof(tos)); |
| 239 | return ret != 0; |
| 240 | } |
| 241 | |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 242 | static int bind_rtp(struct mgcp_endpoint *endp) |
| 243 | { |
| 244 | struct mgcp_config *cfg = endp->cfg; |
| 245 | |
| 246 | if (create_bind(cfg->source_addr, &endp->local_rtp, endp->rtp_port) != 0) { |
| 247 | LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n", |
| 248 | cfg->source_addr, endp->rtp_port, ENDPOINT_NUMBER(endp)); |
| 249 | goto cleanup0; |
| 250 | } |
| 251 | |
| 252 | if (create_bind(cfg->source_addr, &endp->local_rtcp, endp->rtp_port + 1) != 0) { |
| 253 | LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n", |
| 254 | cfg->source_addr, endp->rtp_port + 1, ENDPOINT_NUMBER(endp)); |
| 255 | goto cleanup1; |
| 256 | } |
| 257 | |
Holger Hans Peter Freyther | 75492e6 | 2010-05-31 10:22:00 +0800 | [diff] [blame] | 258 | set_ip_tos(endp->local_rtp.fd, cfg->endp_tos); |
| 259 | set_ip_tos(endp->local_rtcp.fd, cfg->endp_tos); |
| 260 | |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 261 | endp->local_rtp.cb = rtp_data_cb; |
| 262 | endp->local_rtp.data = endp; |
| 263 | endp->local_rtp.when = BSC_FD_READ; |
| 264 | if (bsc_register_fd(&endp->local_rtp) != 0) { |
| 265 | LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n", |
| 266 | endp->rtp_port, ENDPOINT_NUMBER(endp)); |
| 267 | goto cleanup2; |
| 268 | } |
| 269 | |
| 270 | endp->local_rtcp.cb = rtp_data_cb; |
| 271 | endp->local_rtcp.data = endp; |
| 272 | endp->local_rtcp.when = BSC_FD_READ; |
| 273 | if (bsc_register_fd(&endp->local_rtcp) != 0) { |
| 274 | LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n", |
| 275 | endp->rtp_port + 1, ENDPOINT_NUMBER(endp)); |
| 276 | goto cleanup3; |
| 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | |
| 281 | cleanup3: |
| 282 | bsc_unregister_fd(&endp->local_rtp); |
| 283 | cleanup2: |
| 284 | close(endp->local_rtcp.fd); |
| 285 | endp->local_rtcp.fd = -1; |
| 286 | cleanup1: |
| 287 | close(endp->local_rtp.fd); |
| 288 | endp->local_rtp.fd = -1; |
| 289 | cleanup0: |
| 290 | return -1; |
| 291 | } |
| 292 | |
| 293 | int mgcp_bind_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 294 | { |
| 295 | endp->rtp_port = rtp_port; |
| 296 | return bind_rtp(endp); |
| 297 | } |