Holger Hans Peter Freyther | 5ada53a | 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 | adb1992 | 2010-05-14 02:14:09 +0800 | [diff] [blame] | 26 | #include <stdlib.h> |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 27 | #include <unistd.h> |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 28 | #include <endian.h> |
Holger Hans Peter Freyther | 9d7bd81 | 2010-04-07 12:55:40 +0200 | [diff] [blame] | 29 | #include <errno.h> |
Holger Hans Peter Freyther | 5ada53a | 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 | f41fb1f | 2010-02-26 20:16:37 +0100 | [diff] [blame] | 34 | #include <osmocore/msgb.h> |
Holger Hans Peter Freyther | f41fb1f | 2010-02-26 20:16:37 +0100 | [diff] [blame] | 35 | #include <osmocore/select.h> |
| 36 | |
Holger Hans Peter Freyther | 5ada53a | 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 | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 40 | |
| 41 | #warning "Make use of the rtp proxy code" |
| 42 | |
Holger Hans Peter Freyther | f95e3ad | 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 |
Holger Hans Peter Freyther | 1979f79 | 2010-07-23 18:56:26 +0800 | [diff] [blame] | 46 | uint8_t csrc_count:4, |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 47 | extension:1, |
| 48 | padding:1, |
| 49 | version:2; |
Holger Hans Peter Freyther | 1979f79 | 2010-07-23 18:56:26 +0800 | [diff] [blame] | 50 | uint8_t payload_type:7, |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 51 | marker:1; |
| 52 | #elif __BYTE_ORDER == __BIG_ENDIAN |
Holger Hans Peter Freyther | 1979f79 | 2010-07-23 18:56:26 +0800 | [diff] [blame] | 53 | uint8_t version:2, |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 54 | padding:1, |
| 55 | extension:1, |
| 56 | csrc_count:4; |
Holger Hans Peter Freyther | 1979f79 | 2010-07-23 18:56:26 +0800 | [diff] [blame] | 57 | uint8_t marker:1, |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 58 | payload_type:7; |
| 59 | #endif |
Holger Hans Peter Freyther | 1884623 | 2010-07-23 18:56:01 +0800 | [diff] [blame] | 60 | uint16_t sequence; |
Holger Hans Peter Freyther | 142475b | 2010-07-23 18:55:38 +0800 | [diff] [blame] | 61 | uint32_t timestamp; |
| 62 | uint32_t ssrc; |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 63 | } __attribute__((packed)); |
| 64 | |
Holger Hans Peter Freyther | 5ada53a | 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 | b689426 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 76 | #define DUMMY_LOAD 0x23 |
| 77 | |
Holger Hans Peter Freyther | 5ada53a | 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 | b689426 | 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 | e758d95 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 97 | static void patch_and_count(uint16_t *last_seq, int *lost, int payload, char *data, int len) |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 98 | { |
Holger Hans Peter Freyther | e758d95 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 99 | uint16_t seq; |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 100 | struct rtp_hdr *rtp_hdr; |
| 101 | |
| 102 | if (len < sizeof(*rtp_hdr)) |
| 103 | return; |
| 104 | |
Holger Hans Peter Freyther | e758d95 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 105 | rtp_hdr = (struct rtp_hdr *) data; |
| 106 | seq = ntohs(rtp_hdr->sequence); |
| 107 | |
| 108 | /* 0 is assumed to be not set */ |
| 109 | if (*last_seq == 0) |
| 110 | *last_seq = seq; |
| 111 | else if (*last_seq + 1 != seq) |
Holger Hans Peter Freyther | 726f22c | 2010-07-29 03:01:55 +0800 | [diff] [blame] | 112 | *lost = abs(seq - (*last_seq + 1)); |
Holger Hans Peter Freyther | e758d95 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 113 | |
| 114 | *last_seq = seq; |
| 115 | |
| 116 | |
Holger Hans Peter Freyther | 0052955 | 2010-04-06 11:15:50 +0200 | [diff] [blame] | 117 | if (payload < 0) |
| 118 | return; |
| 119 | |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 120 | rtp_hdr->payload_type = payload; |
| 121 | } |
| 122 | |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 123 | /* |
| 124 | * There is data coming. We will have to figure out if it |
| 125 | * came from the BTS or the MediaGateway of the MSC. On top |
| 126 | * of that we need to figure out if it was RTP or RTCP. |
| 127 | * |
| 128 | * Currently we do not communicate with the BSC so we have |
| 129 | * no idea where the BTS is listening for RTP and need to |
| 130 | * do the classic routing trick. Wait for the first packet |
| 131 | * from the BTS and then go ahead. |
| 132 | */ |
| 133 | static int rtp_data_cb(struct bsc_fd *fd, unsigned int what) |
| 134 | { |
| 135 | char buf[4096]; |
| 136 | struct sockaddr_in addr; |
| 137 | socklen_t slen = sizeof(addr); |
| 138 | struct mgcp_endpoint *endp; |
| 139 | struct mgcp_config *cfg; |
| 140 | int rc, dest, proto; |
| 141 | |
| 142 | endp = (struct mgcp_endpoint *) fd->data; |
| 143 | cfg = endp->cfg; |
| 144 | |
| 145 | rc = recvfrom(fd->fd, &buf, sizeof(buf), 0, |
| 146 | (struct sockaddr *) &addr, &slen); |
| 147 | if (rc < 0) { |
Holger Hans Peter Freyther | 9d7bd81 | 2010-04-07 12:55:40 +0200 | [diff] [blame] | 148 | LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n", |
| 149 | ENDPOINT_NUMBER(endp), errno, strerror(errno)); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | /* do not forward aynthing... maybe there is a packet from the bts */ |
Holger Hans Peter Freyther | baefbf4 | 2010-03-01 18:53:05 +0100 | [diff] [blame] | 154 | if (endp->ci == CI_UNUSED) |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 155 | return -1; |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | * Figure out where to forward it to. This code assumes that we |
| 159 | * have received the Connection Modify and know who is a legitimate |
| 160 | * partner. According to the spec we could attempt to forward even |
| 161 | * after the Create Connection but we will not as we are not really |
| 162 | * able to tell if this is legitimate. |
| 163 | */ |
| 164 | #warning "Slight spec violation. With connection mode recvonly we should attempt to forward." |
| 165 | dest = memcmp(&addr.sin_addr, &endp->remote, sizeof(addr.sin_addr)) == 0 && |
Holger Hans Peter Freyther | 8c99434 | 2010-04-30 13:32:05 +0800 | [diff] [blame] | 166 | (endp->net_rtp == addr.sin_port || endp->net_rtcp == addr.sin_port) |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 167 | ? DEST_BTS : DEST_NETWORK; |
| 168 | proto = fd == &endp->local_rtp ? PROTO_RTP : PROTO_RTCP; |
| 169 | |
| 170 | /* We have no idea who called us, maybe it is the BTS. */ |
| 171 | if (dest == DEST_NETWORK && (endp->bts_rtp == 0 || cfg->forward_ip)) { |
| 172 | /* it was the BTS... */ |
Holger Hans Peter Freyther | 27c90f8 | 2010-04-01 07:56:04 +0200 | [diff] [blame] | 173 | if (!cfg->bts_ip |
| 174 | || memcmp(&addr.sin_addr, &cfg->bts_in, sizeof(cfg->bts_in)) == 0 |
| 175 | || memcmp(&addr.sin_addr, &endp->bts, sizeof(endp->bts)) == 0) { |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 176 | if (fd == &endp->local_rtp) { |
| 177 | endp->bts_rtp = addr.sin_port; |
| 178 | } else { |
| 179 | endp->bts_rtcp = addr.sin_port; |
| 180 | } |
| 181 | |
| 182 | endp->bts = addr.sin_addr; |
Holger Hans Peter Freyther | 9c3f576 | 2010-04-09 18:53:01 +0200 | [diff] [blame] | 183 | LOGP(DMGCP, LOGL_NOTICE, "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n", |
| 184 | ENDPOINT_NUMBER(endp), ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp), |
| 185 | inet_ntoa(addr.sin_addr)); |
Holger Hans Peter Freyther | b689426 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 186 | |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Holger Hans Peter Freyther | 906ed10 | 2010-05-14 02:18:59 +0800 | [diff] [blame] | 190 | /* throw away the dummy message */ |
Holger Hans Peter Freyther | 040c130 | 2010-04-22 12:14:51 +0800 | [diff] [blame] | 191 | if (rc == 1 && buf[0] == DUMMY_LOAD) { |
| 192 | LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy on 0x%x\n", |
| 193 | ENDPOINT_NUMBER(endp)); |
| 194 | return 0; |
| 195 | } |
| 196 | |
Holger Hans Peter Freyther | 6de5b11 | 2010-04-07 09:37:17 +0200 | [diff] [blame] | 197 | /* do this before the loop handling */ |
| 198 | if (dest == DEST_NETWORK) |
| 199 | ++endp->in_bts; |
| 200 | else |
| 201 | ++endp->in_remote; |
| 202 | |
Holger Hans Peter Freyther | 906ed10 | 2010-05-14 02:18:59 +0800 | [diff] [blame] | 203 | /* For loop toggle the destination and then dispatch. */ |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 204 | if (cfg->audio_loop) |
| 205 | dest = !dest; |
| 206 | |
| 207 | if (dest == DEST_NETWORK) { |
Holger Hans Peter Freyther | 6e9c55b | 2010-05-22 22:06:13 +0800 | [diff] [blame] | 208 | if (proto == PROTO_RTP) |
Holger Hans Peter Freyther | e758d95 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 209 | patch_and_count(&endp->bts_seq_no, &endp->bts_lost_no, |
| 210 | endp->net_payload_type, buf, rc); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 211 | return udp_send(fd->fd, &endp->remote, |
| 212 | proto == PROTO_RTP ? endp->net_rtp : endp->net_rtcp, |
| 213 | buf, rc); |
| 214 | } else { |
Holger Hans Peter Freyther | 6e9c55b | 2010-05-22 22:06:13 +0800 | [diff] [blame] | 215 | if (proto == PROTO_RTP) |
Holger Hans Peter Freyther | e758d95 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 216 | patch_and_count(&endp->net_seq_no, &endp->net_lost_no, |
| 217 | endp->bts_payload_type, buf, rc); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 218 | return udp_send(fd->fd, &endp->bts, |
| 219 | proto == PROTO_RTP ? endp->bts_rtp : endp->bts_rtcp, |
| 220 | buf, rc); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | static int create_bind(const char *source_addr, struct bsc_fd *fd, int port) |
| 225 | { |
| 226 | struct sockaddr_in addr; |
| 227 | int on = 1; |
| 228 | |
| 229 | fd->fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 230 | if (fd->fd < 0) { |
| 231 | LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n"); |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 236 | memset(&addr, 0, sizeof(addr)); |
| 237 | addr.sin_family = AF_INET; |
| 238 | addr.sin_port = htons(port); |
| 239 | inet_aton(source_addr, &addr.sin_addr); |
| 240 | |
| 241 | if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 242 | return -1; |
| 243 | } |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
Holger Hans Peter Freyther | 489d0a6 | 2010-05-31 10:22:00 +0800 | [diff] [blame] | 248 | static int set_ip_tos(int fd, int tos) |
| 249 | { |
| 250 | int ret; |
| 251 | ret = setsockopt(fd, IPPROTO_IP, IP_TOS, |
| 252 | &tos, sizeof(tos)); |
| 253 | return ret != 0; |
| 254 | } |
| 255 | |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 256 | static int bind_rtp(struct mgcp_endpoint *endp) |
| 257 | { |
| 258 | struct mgcp_config *cfg = endp->cfg; |
| 259 | |
| 260 | if (create_bind(cfg->source_addr, &endp->local_rtp, endp->rtp_port) != 0) { |
| 261 | LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n", |
| 262 | cfg->source_addr, endp->rtp_port, ENDPOINT_NUMBER(endp)); |
| 263 | goto cleanup0; |
| 264 | } |
| 265 | |
| 266 | if (create_bind(cfg->source_addr, &endp->local_rtcp, endp->rtp_port + 1) != 0) { |
| 267 | LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n", |
| 268 | cfg->source_addr, endp->rtp_port + 1, ENDPOINT_NUMBER(endp)); |
| 269 | goto cleanup1; |
| 270 | } |
| 271 | |
Holger Hans Peter Freyther | 3140c4f | 2010-07-27 20:34:45 +0800 | [diff] [blame] | 272 | set_ip_tos(endp->local_rtp.fd, cfg->endp_dscp); |
| 273 | set_ip_tos(endp->local_rtcp.fd, cfg->endp_dscp); |
Holger Hans Peter Freyther | 489d0a6 | 2010-05-31 10:22:00 +0800 | [diff] [blame] | 274 | |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 275 | endp->local_rtp.cb = rtp_data_cb; |
| 276 | endp->local_rtp.data = endp; |
| 277 | endp->local_rtp.when = BSC_FD_READ; |
| 278 | if (bsc_register_fd(&endp->local_rtp) != 0) { |
| 279 | LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n", |
| 280 | endp->rtp_port, ENDPOINT_NUMBER(endp)); |
| 281 | goto cleanup2; |
| 282 | } |
| 283 | |
| 284 | endp->local_rtcp.cb = rtp_data_cb; |
| 285 | endp->local_rtcp.data = endp; |
| 286 | endp->local_rtcp.when = BSC_FD_READ; |
| 287 | if (bsc_register_fd(&endp->local_rtcp) != 0) { |
| 288 | LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n", |
| 289 | endp->rtp_port + 1, ENDPOINT_NUMBER(endp)); |
| 290 | goto cleanup3; |
| 291 | } |
| 292 | |
| 293 | return 0; |
| 294 | |
| 295 | cleanup3: |
| 296 | bsc_unregister_fd(&endp->local_rtp); |
| 297 | cleanup2: |
| 298 | close(endp->local_rtcp.fd); |
| 299 | endp->local_rtcp.fd = -1; |
| 300 | cleanup1: |
| 301 | close(endp->local_rtp.fd); |
| 302 | endp->local_rtp.fd = -1; |
| 303 | cleanup0: |
| 304 | return -1; |
| 305 | } |
| 306 | |
| 307 | int mgcp_bind_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 308 | { |
| 309 | endp->rtp_port = rtp_port; |
| 310 | return bind_rtp(endp); |
| 311 | } |