blob: 924e6fc44208020a29a89ad014a95651661cc6b3 [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 Freyther1b0ea972010-02-21 11:11:04 +010028
29#include <sys/socket.h>
30#include <arpa/inet.h>
31
Holger Hans Peter Freyther1ebad742010-02-26 20:16:37 +010032#include <osmocore/msgb.h>
33#include <osmocore/talloc.h>
34#include <osmocore/select.h>
35
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010036#include <openbsc/debug.h>
37#include <openbsc/mgcp.h>
38#include <openbsc/mgcp_internal.h>
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010039
40#warning "Make use of the rtp proxy code"
41
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010042/* according to rtp_proxy.c RFC 3550 */
43struct 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 Freyther1b0ea972010-02-21 11:11:04 +010064
65enum {
66 DEST_NETWORK = 0,
67 DEST_BTS = 1,
68};
69
70enum {
71 PROTO_RTP,
72 PROTO_RTCP,
73};
74
75
76static 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 Freyther36ed8cc2010-02-26 13:42:58 +010086static 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
93 rtp_hdr = (struct rtp_hdr *) data;
94 rtp_hdr->payload_type = payload;
95}
96
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010097/*
98 * There is data coming. We will have to figure out if it
99 * came from the BTS or the MediaGateway of the MSC. On top
100 * of that we need to figure out if it was RTP or RTCP.
101 *
102 * Currently we do not communicate with the BSC so we have
103 * no idea where the BTS is listening for RTP and need to
104 * do the classic routing trick. Wait for the first packet
105 * from the BTS and then go ahead.
106 */
107static int rtp_data_cb(struct bsc_fd *fd, unsigned int what)
108{
109 char buf[4096];
110 struct sockaddr_in addr;
111 socklen_t slen = sizeof(addr);
112 struct mgcp_endpoint *endp;
113 struct mgcp_config *cfg;
114 int rc, dest, proto;
115
116 endp = (struct mgcp_endpoint *) fd->data;
117 cfg = endp->cfg;
118
119 rc = recvfrom(fd->fd, &buf, sizeof(buf), 0,
120 (struct sockaddr *) &addr, &slen);
121 if (rc < 0) {
122 LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x\n",
123 ENDPOINT_NUMBER(endp));
124 return -1;
125 }
126
127 /* do not forward aynthing... maybe there is a packet from the bts */
Holger Hans Peter Freyther774f0722010-03-01 18:53:05 +0100128 if (endp->ci == CI_UNUSED)
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100129 return -1;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100130
131 /*
132 * Figure out where to forward it to. This code assumes that we
133 * have received the Connection Modify and know who is a legitimate
134 * partner. According to the spec we could attempt to forward even
135 * after the Create Connection but we will not as we are not really
136 * able to tell if this is legitimate.
137 */
138 #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
139 dest = memcmp(&addr.sin_addr, &endp->remote, sizeof(addr.sin_addr)) == 0 &&
140 (endp->net_rtp == addr.sin_port || endp->net_rtcp == addr.sin_port)
141 ? DEST_BTS : DEST_NETWORK;
142 proto = fd == &endp->local_rtp ? PROTO_RTP : PROTO_RTCP;
143
144 /* We have no idea who called us, maybe it is the BTS. */
145 if (dest == DEST_NETWORK && (endp->bts_rtp == 0 || cfg->forward_ip)) {
146 /* it was the BTS... */
147 if (!cfg->bts_ip || memcmp(&addr.sin_addr, &cfg->bts_in, sizeof(cfg->bts_in)) == 0) {
148 if (fd == &endp->local_rtp) {
149 endp->bts_rtp = addr.sin_port;
150 } else {
151 endp->bts_rtcp = addr.sin_port;
152 }
153
154 endp->bts = addr.sin_addr;
155 LOGP(DMGCP, LOGL_NOTICE, "Found BTS for endpoint: 0x%x on port: %d/%d\n",
156 ENDPOINT_NUMBER(endp), ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp));
157 }
158 }
159
160 /* dispatch */
161 if (cfg->audio_loop)
162 dest = !dest;
163
164 if (dest == DEST_NETWORK) {
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100165 patch_payload(endp->net_payload_type, buf, rc);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100166 return udp_send(fd->fd, &endp->remote,
167 proto == PROTO_RTP ? endp->net_rtp : endp->net_rtcp,
168 buf, rc);
169 } else {
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100170 patch_payload(endp->bts_payload_type, buf, rc);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100171 return udp_send(fd->fd, &endp->bts,
172 proto == PROTO_RTP ? endp->bts_rtp : endp->bts_rtcp,
173 buf, rc);
174 }
175}
176
177static int create_bind(const char *source_addr, struct bsc_fd *fd, int port)
178{
179 struct sockaddr_in addr;
180 int on = 1;
181
182 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
183 if (fd->fd < 0) {
184 LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n");
185 return -1;
186 }
187
188 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
189 memset(&addr, 0, sizeof(addr));
190 addr.sin_family = AF_INET;
191 addr.sin_port = htons(port);
192 inet_aton(source_addr, &addr.sin_addr);
193
194 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
195 return -1;
196 }
197
198 return 0;
199}
200
201static int bind_rtp(struct mgcp_endpoint *endp)
202{
203 struct mgcp_config *cfg = endp->cfg;
204
205 if (create_bind(cfg->source_addr, &endp->local_rtp, endp->rtp_port) != 0) {
206 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n",
207 cfg->source_addr, endp->rtp_port, ENDPOINT_NUMBER(endp));
208 goto cleanup0;
209 }
210
211 if (create_bind(cfg->source_addr, &endp->local_rtcp, endp->rtp_port + 1) != 0) {
212 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n",
213 cfg->source_addr, endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
214 goto cleanup1;
215 }
216
217 endp->local_rtp.cb = rtp_data_cb;
218 endp->local_rtp.data = endp;
219 endp->local_rtp.when = BSC_FD_READ;
220 if (bsc_register_fd(&endp->local_rtp) != 0) {
221 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n",
222 endp->rtp_port, ENDPOINT_NUMBER(endp));
223 goto cleanup2;
224 }
225
226 endp->local_rtcp.cb = rtp_data_cb;
227 endp->local_rtcp.data = endp;
228 endp->local_rtcp.when = BSC_FD_READ;
229 if (bsc_register_fd(&endp->local_rtcp) != 0) {
230 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n",
231 endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
232 goto cleanup3;
233 }
234
235 return 0;
236
237cleanup3:
238 bsc_unregister_fd(&endp->local_rtp);
239cleanup2:
240 close(endp->local_rtcp.fd);
241 endp->local_rtcp.fd = -1;
242cleanup1:
243 close(endp->local_rtp.fd);
244 endp->local_rtp.fd = -1;
245cleanup0:
246 return -1;
247}
248
249int mgcp_bind_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
250{
251 endp->rtp_port = rtp_port;
252 return bind_rtp(endp);
253}