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