blob: c61f0a8581ba72598238c6c0aee0b3cd41ae00e1 [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
32#include <openbsc/debug.h>
33#include <openbsc/mgcp.h>
34#include <openbsc/mgcp_internal.h>
35#include <openbsc/msgb.h>
36#include <openbsc/talloc.h>
37#include <openbsc/select.h>
38
39#warning "Make use of the rtp proxy code"
40
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010041/* according to rtp_proxy.c RFC 3550 */
42struct rtp_hdr {
43#if __BYTE_ORDER == __LITTLE_ENDIAN
44 u_int8_t csrc_count:4,
45 extension:1,
46 padding:1,
47 version:2;
48 u_int8_t payload_type:7,
49 marker:1;
50#elif __BYTE_ORDER == __BIG_ENDIAN
51 u_int8_t version:2,
52 padding:1,
53 extension:1,
54 csrc_count:4;
55 u_int8_t marker:1,
56 payload_type:7;
57#endif
58 u_int16_t sequence;
59 u_int32_t timestamp;
60 u_int32_t ssrc;
61} __attribute__((packed));
62
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010063
64enum {
65 DEST_NETWORK = 0,
66 DEST_BTS = 1,
67};
68
69enum {
70 PROTO_RTP,
71 PROTO_RTCP,
72};
73
74
75static int udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
76{
77 struct sockaddr_in out;
78 out.sin_family = AF_INET;
79 out.sin_port = port;
80 memcpy(&out.sin_addr, addr, sizeof(*addr));
81
82 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
83}
84
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +010085static void patch_payload(int payload, char *data, int len)
86{
87 struct rtp_hdr *rtp_hdr;
88
89 if (len < sizeof(*rtp_hdr))
90 return;
91
92 rtp_hdr = (struct rtp_hdr *) data;
93 rtp_hdr->payload_type = payload;
94}
95
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +010096/*
97 * There is data coming. We will have to figure out if it
98 * came from the BTS or the MediaGateway of the MSC. On top
99 * of that we need to figure out if it was RTP or RTCP.
100 *
101 * Currently we do not communicate with the BSC so we have
102 * no idea where the BTS is listening for RTP and need to
103 * do the classic routing trick. Wait for the first packet
104 * from the BTS and then go ahead.
105 */
106static int rtp_data_cb(struct bsc_fd *fd, unsigned int what)
107{
108 char buf[4096];
109 struct sockaddr_in addr;
110 socklen_t slen = sizeof(addr);
111 struct mgcp_endpoint *endp;
112 struct mgcp_config *cfg;
113 int rc, dest, proto;
114
115 endp = (struct mgcp_endpoint *) fd->data;
116 cfg = endp->cfg;
117
118 rc = recvfrom(fd->fd, &buf, sizeof(buf), 0,
119 (struct sockaddr *) &addr, &slen);
120 if (rc < 0) {
121 LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x\n",
122 ENDPOINT_NUMBER(endp));
123 return -1;
124 }
125
126 /* do not forward aynthing... maybe there is a packet from the bts */
127 if (endp->ci == CI_UNUSED) {
128 LOGP(DMGCP, LOGL_ERROR, "Unknown message on endpoint: 0x%x\n", ENDPOINT_NUMBER(endp));
129 return -1;
130 }
131
132 /*
133 * Figure out where to forward it to. This code assumes that we
134 * have received the Connection Modify and know who is a legitimate
135 * partner. According to the spec we could attempt to forward even
136 * after the Create Connection but we will not as we are not really
137 * able to tell if this is legitimate.
138 */
139 #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
140 dest = memcmp(&addr.sin_addr, &endp->remote, sizeof(addr.sin_addr)) == 0 &&
141 (endp->net_rtp == addr.sin_port || endp->net_rtcp == addr.sin_port)
142 ? DEST_BTS : DEST_NETWORK;
143 proto = fd == &endp->local_rtp ? PROTO_RTP : PROTO_RTCP;
144
145 /* We have no idea who called us, maybe it is the BTS. */
146 if (dest == DEST_NETWORK && (endp->bts_rtp == 0 || cfg->forward_ip)) {
147 /* it was the BTS... */
148 if (!cfg->bts_ip || memcmp(&addr.sin_addr, &cfg->bts_in, sizeof(cfg->bts_in)) == 0) {
149 if (fd == &endp->local_rtp) {
150 endp->bts_rtp = addr.sin_port;
151 } else {
152 endp->bts_rtcp = addr.sin_port;
153 }
154
155 endp->bts = addr.sin_addr;
156 LOGP(DMGCP, LOGL_NOTICE, "Found BTS for endpoint: 0x%x on port: %d/%d\n",
157 ENDPOINT_NUMBER(endp), ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp));
158 }
159 }
160
161 /* dispatch */
162 if (cfg->audio_loop)
163 dest = !dest;
164
165 if (dest == DEST_NETWORK) {
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100166 patch_payload(endp->net_payload_type, buf, rc);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100167 return udp_send(fd->fd, &endp->remote,
168 proto == PROTO_RTP ? endp->net_rtp : endp->net_rtcp,
169 buf, rc);
170 } else {
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100171 patch_payload(endp->bts_payload_type, buf, rc);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100172 return udp_send(fd->fd, &endp->bts,
173 proto == PROTO_RTP ? endp->bts_rtp : endp->bts_rtcp,
174 buf, rc);
175 }
176}
177
178static int create_bind(const char *source_addr, struct bsc_fd *fd, int port)
179{
180 struct sockaddr_in addr;
181 int on = 1;
182
183 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
184 if (fd->fd < 0) {
185 LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n");
186 return -1;
187 }
188
189 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
190 memset(&addr, 0, sizeof(addr));
191 addr.sin_family = AF_INET;
192 addr.sin_port = htons(port);
193 inet_aton(source_addr, &addr.sin_addr);
194
195 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
196 return -1;
197 }
198
199 return 0;
200}
201
202static int bind_rtp(struct mgcp_endpoint *endp)
203{
204 struct mgcp_config *cfg = endp->cfg;
205
206 if (create_bind(cfg->source_addr, &endp->local_rtp, endp->rtp_port) != 0) {
207 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n",
208 cfg->source_addr, endp->rtp_port, ENDPOINT_NUMBER(endp));
209 goto cleanup0;
210 }
211
212 if (create_bind(cfg->source_addr, &endp->local_rtcp, endp->rtp_port + 1) != 0) {
213 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n",
214 cfg->source_addr, endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
215 goto cleanup1;
216 }
217
218 endp->local_rtp.cb = rtp_data_cb;
219 endp->local_rtp.data = endp;
220 endp->local_rtp.when = BSC_FD_READ;
221 if (bsc_register_fd(&endp->local_rtp) != 0) {
222 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n",
223 endp->rtp_port, ENDPOINT_NUMBER(endp));
224 goto cleanup2;
225 }
226
227 endp->local_rtcp.cb = rtp_data_cb;
228 endp->local_rtcp.data = endp;
229 endp->local_rtcp.when = BSC_FD_READ;
230 if (bsc_register_fd(&endp->local_rtcp) != 0) {
231 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n",
232 endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
233 goto cleanup3;
234 }
235
236 return 0;
237
238cleanup3:
239 bsc_unregister_fd(&endp->local_rtp);
240cleanup2:
241 close(endp->local_rtcp.fd);
242 endp->local_rtcp.fd = -1;
243cleanup1:
244 close(endp->local_rtp.fd);
245 endp->local_rtp.fd = -1;
246cleanup0:
247 return -1;
248}
249
250int mgcp_bind_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
251{
252 endp->rtp_port = rtp_port;
253 return bind_rtp(endp);
254}