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