blob: b76ca4732284259e0aa0ae68bd42036cded1e245 [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 */
128 if (endp->ci == CI_UNUSED) {
129 LOGP(DMGCP, LOGL_ERROR, "Unknown message on endpoint: 0x%x\n", ENDPOINT_NUMBER(endp));
130 return -1;
131 }
132
133 /*
134 * Figure out where to forward it to. This code assumes that we
135 * have received the Connection Modify and know who is a legitimate
136 * partner. According to the spec we could attempt to forward even
137 * after the Create Connection but we will not as we are not really
138 * able to tell if this is legitimate.
139 */
140 #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
141 dest = memcmp(&addr.sin_addr, &endp->remote, sizeof(addr.sin_addr)) == 0 &&
142 (endp->net_rtp == addr.sin_port || endp->net_rtcp == addr.sin_port)
143 ? DEST_BTS : DEST_NETWORK;
144 proto = fd == &endp->local_rtp ? PROTO_RTP : PROTO_RTCP;
145
146 /* We have no idea who called us, maybe it is the BTS. */
147 if (dest == DEST_NETWORK && (endp->bts_rtp == 0 || cfg->forward_ip)) {
148 /* it was the BTS... */
149 if (!cfg->bts_ip || memcmp(&addr.sin_addr, &cfg->bts_in, sizeof(cfg->bts_in)) == 0) {
150 if (fd == &endp->local_rtp) {
151 endp->bts_rtp = addr.sin_port;
152 } else {
153 endp->bts_rtcp = addr.sin_port;
154 }
155
156 endp->bts = addr.sin_addr;
157 LOGP(DMGCP, LOGL_NOTICE, "Found BTS for endpoint: 0x%x on port: %d/%d\n",
158 ENDPOINT_NUMBER(endp), ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp));
159 }
160 }
161
162 /* dispatch */
163 if (cfg->audio_loop)
164 dest = !dest;
165
166 if (dest == DEST_NETWORK) {
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100167 patch_payload(endp->net_payload_type, buf, rc);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100168 return udp_send(fd->fd, &endp->remote,
169 proto == PROTO_RTP ? endp->net_rtp : endp->net_rtcp,
170 buf, rc);
171 } else {
Holger Hans Peter Freyther36ed8cc2010-02-26 13:42:58 +0100172 patch_payload(endp->bts_payload_type, buf, rc);
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100173 return udp_send(fd->fd, &endp->bts,
174 proto == PROTO_RTP ? endp->bts_rtp : endp->bts_rtcp,
175 buf, rc);
176 }
177}
178
179static int create_bind(const char *source_addr, struct bsc_fd *fd, int port)
180{
181 struct sockaddr_in addr;
182 int on = 1;
183
184 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
185 if (fd->fd < 0) {
186 LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n");
187 return -1;
188 }
189
190 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
191 memset(&addr, 0, sizeof(addr));
192 addr.sin_family = AF_INET;
193 addr.sin_port = htons(port);
194 inet_aton(source_addr, &addr.sin_addr);
195
196 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
197 return -1;
198 }
199
200 return 0;
201}
202
203static int bind_rtp(struct mgcp_endpoint *endp)
204{
205 struct mgcp_config *cfg = endp->cfg;
206
207 if (create_bind(cfg->source_addr, &endp->local_rtp, endp->rtp_port) != 0) {
208 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n",
209 cfg->source_addr, endp->rtp_port, ENDPOINT_NUMBER(endp));
210 goto cleanup0;
211 }
212
213 if (create_bind(cfg->source_addr, &endp->local_rtcp, endp->rtp_port + 1) != 0) {
214 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n",
215 cfg->source_addr, endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
216 goto cleanup1;
217 }
218
219 endp->local_rtp.cb = rtp_data_cb;
220 endp->local_rtp.data = endp;
221 endp->local_rtp.when = BSC_FD_READ;
222 if (bsc_register_fd(&endp->local_rtp) != 0) {
223 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n",
224 endp->rtp_port, ENDPOINT_NUMBER(endp));
225 goto cleanup2;
226 }
227
228 endp->local_rtcp.cb = rtp_data_cb;
229 endp->local_rtcp.data = endp;
230 endp->local_rtcp.when = BSC_FD_READ;
231 if (bsc_register_fd(&endp->local_rtcp) != 0) {
232 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n",
233 endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
234 goto cleanup3;
235 }
236
237 return 0;
238
239cleanup3:
240 bsc_unregister_fd(&endp->local_rtp);
241cleanup2:
242 close(endp->local_rtcp.fd);
243 endp->local_rtcp.fd = -1;
244cleanup1:
245 close(endp->local_rtp.fd);
246 endp->local_rtp.fd = -1;
247cleanup0:
248 return -1;
249}
250
251int mgcp_bind_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
252{
253 endp->rtp_port = rtp_port;
254 return bind_rtp(endp);
255}