blob: b5e2cddf86b0888a38fa673b7943bfa1d3d682d2 [file] [log] [blame]
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +01001/* 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 Freytherb715d7f2010-05-14 02:14:09 +080026#include <stdlib.h>
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010027#include <unistd.h>
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010028#include <endian.h>
Holger Hans Peter Freyther575b8952010-04-07 12:55:40 +020029#include <errno.h>
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010030
31#include <sys/socket.h>
32#include <arpa/inet.h>
33
Holger Hans Peter Freyther1ebad742010-02-26 20:16:37 +010034#include <osmocore/msgb.h>
Holger Hans Peter Freyther1ebad742010-02-26 20:16:37 +010035#include <osmocore/select.h>
36
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010037#include <openbsc/debug.h>
38#include <openbsc/mgcp.h>
39#include <openbsc/mgcp_internal.h>
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010040
41#warning "Make use of the rtp proxy code"
42
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010043/* according to rtp_proxy.c RFC 3550 */
44struct rtp_hdr {
45#if __BYTE_ORDER == __LITTLE_ENDIAN
Holger Hans Peter Freyther7dece862010-07-23 18:56:26 +080046 uint8_t csrc_count:4,
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010047 extension:1,
48 padding:1,
49 version:2;
Holger Hans Peter Freyther7dece862010-07-23 18:56:26 +080050 uint8_t payload_type:7,
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010051 marker:1;
52#elif __BYTE_ORDER == __BIG_ENDIAN
Holger Hans Peter Freyther7dece862010-07-23 18:56:26 +080053 uint8_t version:2,
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010054 padding:1,
55 extension:1,
56 csrc_count:4;
Holger Hans Peter Freyther7dece862010-07-23 18:56:26 +080057 uint8_t marker:1,
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010058 payload_type:7;
59#endif
Holger Hans Peter Freytherd340cd32010-07-23 18:56:01 +080060 uint16_t sequence;
Holger Hans Peter Freytherd9b18f82010-07-23 18:55:38 +080061 uint32_t timestamp;
62 uint32_t ssrc;
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010063} __attribute__((packed));
64
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010065
66enum {
67 DEST_NETWORK = 0,
68 DEST_BTS = 1,
69};
70
71enum {
72 PROTO_RTP,
73 PROTO_RTCP,
74};
75
Holger Hans Peter Freytherb844b872010-04-21 21:25:13 +080076#define DUMMY_LOAD 0x23
77
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010078
79static 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 Freytherb844b872010-04-21 21:25:13 +080089int 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 Freyther31868922010-08-03 11:59:04 +000097static void patch_and_count(struct mgcp_rtp_state *state, int payload, char *data, int len)
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010098{
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +080099 uint16_t seq;
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000100 uint32_t timestamp;
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100101 struct rtp_hdr *rtp_hdr;
102
103 if (len < sizeof(*rtp_hdr))
104 return;
105
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800106 rtp_hdr = (struct rtp_hdr *) data;
107 seq = ntohs(rtp_hdr->sequence);
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000108 timestamp = ntohl(rtp_hdr->timestamp);
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800109
Holger Hans Peter Freyther31868922010-08-03 11:59:04 +0000110 if (!state->initialized) {
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000111 state->seq_no = seq - 1;
112 state->ssrc = state->orig_ssrc = rtp_hdr->ssrc;
Holger Hans Peter Freyther31868922010-08-03 11:59:04 +0000113 state->initialized = 1;
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000114 state->last_timestamp = timestamp;
115 } else if (state->ssrc != rtp_hdr->ssrc) {
116 state->ssrc = rtp_hdr->ssrc;
117 state->seq_offset = (state->seq_no + 1) - seq;
118 state->timestamp_offset = state->last_timestamp - timestamp;
119 LOGP(DMGCP, LOGL_NOTICE, "The SSRC changed... SSRC: %u offset: %d\n",
120 state->ssrc, state->seq_offset);
121 }
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800122
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000123 /* apply the offset and store it back to the packet */
124 seq += state->seq_offset;
125 rtp_hdr->sequence = htons(seq);
126 rtp_hdr->ssrc = state->orig_ssrc;
127
128 timestamp += state->timestamp_offset;
129 rtp_hdr->timestamp = htonl(timestamp);
130
131 /* seq changed, now compare if we have lost something */
132 if (state->seq_no + 1u != seq)
133 state->lost_no = abs(seq - (state->seq_no + 1));
Holger Hans Peter Freyther31868922010-08-03 11:59:04 +0000134 state->seq_no = seq;
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800135
Holger Hans Peter Freyther6aa882b2010-08-03 14:35:50 +0000136 state->last_timestamp = timestamp;
137
Holger Hans Peter Freyther7279d242010-04-06 11:15:50 +0200138 if (payload < 0)
139 return;
140
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100141 rtp_hdr->payload_type = payload;
142}
143
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100144/*
145 * There is data coming. We will have to figure out if it
146 * came from the BTS or the MediaGateway of the MSC. On top
147 * of that we need to figure out if it was RTP or RTCP.
148 *
149 * Currently we do not communicate with the BSC so we have
150 * no idea where the BTS is listening for RTP and need to
151 * do the classic routing trick. Wait for the first packet
152 * from the BTS and then go ahead.
153 */
154static int rtp_data_cb(struct bsc_fd *fd, unsigned int what)
155{
156 char buf[4096];
157 struct sockaddr_in addr;
158 socklen_t slen = sizeof(addr);
159 struct mgcp_endpoint *endp;
160 struct mgcp_config *cfg;
161 int rc, dest, proto;
162
163 endp = (struct mgcp_endpoint *) fd->data;
164 cfg = endp->cfg;
165
166 rc = recvfrom(fd->fd, &buf, sizeof(buf), 0,
167 (struct sockaddr *) &addr, &slen);
168 if (rc < 0) {
Holger Hans Peter Freyther575b8952010-04-07 12:55:40 +0200169 LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n",
170 ENDPOINT_NUMBER(endp), errno, strerror(errno));
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100171 return -1;
172 }
173
174 /* do not forward aynthing... maybe there is a packet from the bts */
Holger Hans Peter Freyther774f0722010-03-01 18:53:05 +0100175 if (endp->ci == CI_UNUSED)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100176 return -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100177
178 /*
179 * Figure out where to forward it to. This code assumes that we
180 * have received the Connection Modify and know who is a legitimate
181 * partner. According to the spec we could attempt to forward even
182 * after the Create Connection but we will not as we are not really
183 * able to tell if this is legitimate.
184 */
185 #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
186 dest = memcmp(&addr.sin_addr, &endp->remote, sizeof(addr.sin_addr)) == 0 &&
Holger Hans Peter Freythera5811362010-04-30 13:32:05 +0800187 (endp->net_rtp == addr.sin_port || endp->net_rtcp == addr.sin_port)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100188 ? DEST_BTS : DEST_NETWORK;
189 proto = fd == &endp->local_rtp ? PROTO_RTP : PROTO_RTCP;
190
191 /* We have no idea who called us, maybe it is the BTS. */
192 if (dest == DEST_NETWORK && (endp->bts_rtp == 0 || cfg->forward_ip)) {
193 /* it was the BTS... */
Holger Hans Peter Freyther8b120f02010-04-01 07:56:04 +0200194 if (!cfg->bts_ip
195 || memcmp(&addr.sin_addr, &cfg->bts_in, sizeof(cfg->bts_in)) == 0
196 || memcmp(&addr.sin_addr, &endp->bts, sizeof(endp->bts)) == 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100197 if (fd == &endp->local_rtp) {
198 endp->bts_rtp = addr.sin_port;
199 } else {
200 endp->bts_rtcp = addr.sin_port;
201 }
202
203 endp->bts = addr.sin_addr;
Holger Hans Peter Freytheraff596b2010-04-09 18:53:01 +0200204 LOGP(DMGCP, LOGL_NOTICE, "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n",
205 ENDPOINT_NUMBER(endp), ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp),
206 inet_ntoa(addr.sin_addr));
Holger Hans Peter Freytherb844b872010-04-21 21:25:13 +0800207
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100208 }
209 }
210
Holger Hans Peter Freytherb3c206a2010-05-14 02:18:59 +0800211 /* throw away the dummy message */
Holger Hans Peter Freytheraa9d3ce2010-04-22 12:14:51 +0800212 if (rc == 1 && buf[0] == DUMMY_LOAD) {
213 LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy on 0x%x\n",
214 ENDPOINT_NUMBER(endp));
215 return 0;
216 }
217
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +0200218 /* do this before the loop handling */
219 if (dest == DEST_NETWORK)
220 ++endp->in_bts;
221 else
222 ++endp->in_remote;
223
Holger Hans Peter Freytherb3c206a2010-05-14 02:18:59 +0800224 /* For loop toggle the destination and then dispatch. */
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100225 if (cfg->audio_loop)
226 dest = !dest;
227
Holger Hans Peter Freyther98a38772010-08-03 02:27:21 +0800228 /* Loop based on the conn_mode, maybe undoing the above */
229 if (endp->conn_mode == MGCP_CONN_LOOPBACK)
230 dest = !dest;
231
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100232 if (dest == DEST_NETWORK) {
Holger Hans Peter Freythera19bdab2010-05-22 22:06:13 +0800233 if (proto == PROTO_RTP)
Holger Hans Peter Freyther31868922010-08-03 11:59:04 +0000234 patch_and_count(&endp->bts_state,
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800235 endp->net_payload_type, buf, rc);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100236 return udp_send(fd->fd, &endp->remote,
237 proto == PROTO_RTP ? endp->net_rtp : endp->net_rtcp,
238 buf, rc);
239 } else {
Holger Hans Peter Freythera19bdab2010-05-22 22:06:13 +0800240 if (proto == PROTO_RTP)
Holger Hans Peter Freyther31868922010-08-03 11:59:04 +0000241 patch_and_count(&endp->net_state,
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +0800242 endp->bts_payload_type, buf, rc);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100243 return udp_send(fd->fd, &endp->bts,
244 proto == PROTO_RTP ? endp->bts_rtp : endp->bts_rtcp,
245 buf, rc);
246 }
247}
248
249static int create_bind(const char *source_addr, struct bsc_fd *fd, int port)
250{
251 struct sockaddr_in addr;
252 int on = 1;
253
254 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
255 if (fd->fd < 0) {
256 LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n");
257 return -1;
258 }
259
260 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
261 memset(&addr, 0, sizeof(addr));
262 addr.sin_family = AF_INET;
263 addr.sin_port = htons(port);
264 inet_aton(source_addr, &addr.sin_addr);
265
266 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
267 return -1;
268 }
269
270 return 0;
271}
272
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800273static int set_ip_tos(int fd, int tos)
274{
275 int ret;
276 ret = setsockopt(fd, IPPROTO_IP, IP_TOS,
277 &tos, sizeof(tos));
278 return ret != 0;
279}
280
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100281static int bind_rtp(struct mgcp_endpoint *endp)
282{
283 struct mgcp_config *cfg = endp->cfg;
284
285 if (create_bind(cfg->source_addr, &endp->local_rtp, endp->rtp_port) != 0) {
286 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n",
287 cfg->source_addr, endp->rtp_port, ENDPOINT_NUMBER(endp));
288 goto cleanup0;
289 }
290
291 if (create_bind(cfg->source_addr, &endp->local_rtcp, endp->rtp_port + 1) != 0) {
292 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n",
293 cfg->source_addr, endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
294 goto cleanup1;
295 }
296
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800297 set_ip_tos(endp->local_rtp.fd, cfg->endp_dscp);
298 set_ip_tos(endp->local_rtcp.fd, cfg->endp_dscp);
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800299
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100300 endp->local_rtp.cb = rtp_data_cb;
301 endp->local_rtp.data = endp;
302 endp->local_rtp.when = BSC_FD_READ;
303 if (bsc_register_fd(&endp->local_rtp) != 0) {
304 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n",
305 endp->rtp_port, ENDPOINT_NUMBER(endp));
306 goto cleanup2;
307 }
308
309 endp->local_rtcp.cb = rtp_data_cb;
310 endp->local_rtcp.data = endp;
311 endp->local_rtcp.when = BSC_FD_READ;
312 if (bsc_register_fd(&endp->local_rtcp) != 0) {
313 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n",
314 endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
315 goto cleanup3;
316 }
317
318 return 0;
319
320cleanup3:
321 bsc_unregister_fd(&endp->local_rtp);
322cleanup2:
323 close(endp->local_rtcp.fd);
324 endp->local_rtcp.fd = -1;
325cleanup1:
326 close(endp->local_rtp.fd);
327 endp->local_rtp.fd = -1;
328cleanup0:
329 return -1;
330}
331
332int mgcp_bind_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
333{
334 endp->rtp_port = rtp_port;
335 return bind_rtp(endp);
336}