blob: 5a982608e8f71dbc81c23ac17937d055c68e2bc5 [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>
26#include <unistd.h>
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010027#include <endian.h>
Holger Hans Peter Freyther575b8952010-04-07 12:55:40 +020028#include <errno.h>
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010029
30#include <sys/socket.h>
31#include <arpa/inet.h>
32
Holger Hans Peter Freyther1ebad742010-02-26 20:16:37 +010033#include <osmocore/msgb.h>
34#include <osmocore/talloc.h>
35#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
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 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
76
77static int udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
78{
79 struct sockaddr_in out;
80 out.sin_family = AF_INET;
81 out.sin_port = port;
82 memcpy(&out.sin_addr, addr, sizeof(*addr));
83
84 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
85}
86
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010087static void patch_payload(int payload, char *data, int len)
88{
89 struct rtp_hdr *rtp_hdr;
90
91 if (len < sizeof(*rtp_hdr))
92 return;
93
Holger Hans Peter Freyther7279d242010-04-06 11:15:50 +020094 if (payload < 0)
95 return;
96
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010097 rtp_hdr = (struct rtp_hdr *) data;
98 rtp_hdr->payload_type = payload;
99}
100
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100101/*
102 * There is data coming. We will have to figure out if it
103 * came from the BTS or the MediaGateway of the MSC. On top
104 * of that we need to figure out if it was RTP or RTCP.
105 *
106 * Currently we do not communicate with the BSC so we have
107 * no idea where the BTS is listening for RTP and need to
108 * do the classic routing trick. Wait for the first packet
109 * from the BTS and then go ahead.
110 */
111static int rtp_data_cb(struct bsc_fd *fd, unsigned int what)
112{
113 char buf[4096];
114 struct sockaddr_in addr;
115 socklen_t slen = sizeof(addr);
116 struct mgcp_endpoint *endp;
117 struct mgcp_config *cfg;
118 int rc, dest, proto;
119
120 endp = (struct mgcp_endpoint *) fd->data;
121 cfg = endp->cfg;
122
123 rc = recvfrom(fd->fd, &buf, sizeof(buf), 0,
124 (struct sockaddr *) &addr, &slen);
125 if (rc < 0) {
Holger Hans Peter Freyther575b8952010-04-07 12:55:40 +0200126 LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n",
127 ENDPOINT_NUMBER(endp), errno, strerror(errno));
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100128 return -1;
129 }
130
131 /* do not forward aynthing... maybe there is a packet from the bts */
Holger Hans Peter Freyther774f0722010-03-01 18:53:05 +0100132 if (endp->ci == CI_UNUSED)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100133 return -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100134
135 /*
136 * Figure out where to forward it to. This code assumes that we
137 * have received the Connection Modify and know who is a legitimate
138 * partner. According to the spec we could attempt to forward even
139 * after the Create Connection but we will not as we are not really
140 * able to tell if this is legitimate.
141 */
142 #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
143 dest = memcmp(&addr.sin_addr, &endp->remote, sizeof(addr.sin_addr)) == 0 &&
144 (endp->net_rtp == addr.sin_port || endp->net_rtcp == addr.sin_port)
145 ? DEST_BTS : DEST_NETWORK;
146 proto = fd == &endp->local_rtp ? PROTO_RTP : PROTO_RTCP;
147
148 /* We have no idea who called us, maybe it is the BTS. */
149 if (dest == DEST_NETWORK && (endp->bts_rtp == 0 || cfg->forward_ip)) {
150 /* it was the BTS... */
Holger Hans Peter Freyther8b120f02010-04-01 07:56:04 +0200151 if (!cfg->bts_ip
152 || memcmp(&addr.sin_addr, &cfg->bts_in, sizeof(cfg->bts_in)) == 0
153 || memcmp(&addr.sin_addr, &endp->bts, sizeof(endp->bts)) == 0) {
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100154 if (fd == &endp->local_rtp) {
155 endp->bts_rtp = addr.sin_port;
156 } else {
157 endp->bts_rtcp = addr.sin_port;
158 }
159
160 endp->bts = addr.sin_addr;
Holger Hans Peter Freytheraff596b2010-04-09 18:53:01 +0200161 LOGP(DMGCP, LOGL_NOTICE, "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n",
162 ENDPOINT_NUMBER(endp), ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp),
163 inet_ntoa(addr.sin_addr));
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100164 }
165 }
166
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +0200167 /* do this before the loop handling */
168 if (dest == DEST_NETWORK)
169 ++endp->in_bts;
170 else
171 ++endp->in_remote;
172
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100173 /* dispatch */
174 if (cfg->audio_loop)
175 dest = !dest;
176
177 if (dest == DEST_NETWORK) {
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100178 patch_payload(endp->net_payload_type, buf, rc);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100179 return udp_send(fd->fd, &endp->remote,
180 proto == PROTO_RTP ? endp->net_rtp : endp->net_rtcp,
181 buf, rc);
182 } else {
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100183 patch_payload(endp->bts_payload_type, buf, rc);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100184 return udp_send(fd->fd, &endp->bts,
185 proto == PROTO_RTP ? endp->bts_rtp : endp->bts_rtcp,
186 buf, rc);
187 }
188}
189
190static int create_bind(const char *source_addr, struct bsc_fd *fd, int port)
191{
192 struct sockaddr_in addr;
193 int on = 1;
194
195 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
196 if (fd->fd < 0) {
197 LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n");
198 return -1;
199 }
200
201 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
202 memset(&addr, 0, sizeof(addr));
203 addr.sin_family = AF_INET;
204 addr.sin_port = htons(port);
205 inet_aton(source_addr, &addr.sin_addr);
206
207 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
208 return -1;
209 }
210
211 return 0;
212}
213
214static int bind_rtp(struct mgcp_endpoint *endp)
215{
216 struct mgcp_config *cfg = endp->cfg;
217
218 if (create_bind(cfg->source_addr, &endp->local_rtp, endp->rtp_port) != 0) {
219 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n",
220 cfg->source_addr, endp->rtp_port, ENDPOINT_NUMBER(endp));
221 goto cleanup0;
222 }
223
224 if (create_bind(cfg->source_addr, &endp->local_rtcp, endp->rtp_port + 1) != 0) {
225 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n",
226 cfg->source_addr, endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
227 goto cleanup1;
228 }
229
230 endp->local_rtp.cb = rtp_data_cb;
231 endp->local_rtp.data = endp;
232 endp->local_rtp.when = BSC_FD_READ;
233 if (bsc_register_fd(&endp->local_rtp) != 0) {
234 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n",
235 endp->rtp_port, ENDPOINT_NUMBER(endp));
236 goto cleanup2;
237 }
238
239 endp->local_rtcp.cb = rtp_data_cb;
240 endp->local_rtcp.data = endp;
241 endp->local_rtcp.when = BSC_FD_READ;
242 if (bsc_register_fd(&endp->local_rtcp) != 0) {
243 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n",
244 endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
245 goto cleanup3;
246 }
247
248 return 0;
249
250cleanup3:
251 bsc_unregister_fd(&endp->local_rtp);
252cleanup2:
253 close(endp->local_rtcp.fd);
254 endp->local_rtcp.fd = -1;
255cleanup1:
256 close(endp->local_rtp.fd);
257 endp->local_rtp.fd = -1;
258cleanup0:
259 return -1;
260}
261
262int mgcp_bind_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
263{
264 endp->rtp_port = rtp_port;
265 return bind_rtp(endp);
266}