blob: 3fdc652b08574a64747153b4248414f7c4e1f050 [file] [log] [blame]
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +01001/*
2 * (C) 2012-2013 by Pablo Neira Ayuso <pablo@gnumonks.org>
3 * (C) 2012-2013 by On Waves ehf <http://www.on-waves.com>
4 * All rights not specifically granted under this license are reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Affero General Public License as published by the
8 * Free Software Foundation; either version 3 of the License, or (at your
9 * option) any later version.
10 */
11
12#include <stdio.h> /* for printf */
13#include <string.h> /* for memcpy */
14#include <stdlib.h> /* for abs */
15#include <netinet/in.h>
16#include <osmocom/core/msgb.h>
17#include <osmocom/core/talloc.h>
18
19#include <osmocom/netif/osmux.h>
20#include <osmocom/netif/rtp.h>
21
22#include <openbsc/mgcp.h>
23#include <openbsc/mgcp_internal.h>
24#include <openbsc/osmux.h>
25
26#define OSMUX_PORT 1984
27
28static struct osmo_fd osmux_fd;
29
30static LLIST_HEAD(osmux_handle_list);
31
32struct osmux_handle {
33 struct llist_head head;
34 struct osmux_in_handle *in;
35};
36
37static void *osmux;
38
39static void osmux_deliver(struct msgb *batch_msg, void *data)
40{
41 struct in_addr *addr = data;
42 struct sockaddr_in out = {
43 .sin_family = AF_INET,
44 .sin_port = htons(OSMUX_PORT),
45 };
46 char buf[4096];
47
48 memcpy(&out.sin_addr, addr, sizeof(*addr));
49
50 osmux_snprintf(buf, sizeof(buf), batch_msg);
51 LOGP(DMGCP, LOGL_DEBUG, "OSMUX delivering batch to addr=%s: %s\n",
52 inet_ntoa(out.sin_addr), buf);
53
54 sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0,
55 (struct sockaddr *)&out, sizeof(out));
56}
57
58static struct osmux_in_handle *
59osmux_handle_lookup(struct mgcp_config *cfg, struct in_addr *addr)
60{
61 struct osmux_handle *h;
62
63 /* Lookup for existing OSMUX handle for this destination address. */
64 llist_for_each_entry(h, &osmux_handle_list, head) {
65 if (memcmp(h->in->data, addr, sizeof(struct in_addr)) == 0) {
66 LOGP(DMGCP, LOGL_DEBUG, "using existing OSMUX handle "
67 "for addr=%s\n",
68 inet_ntoa(*addr));
69 goto out;
70 }
71 }
72
73 /* Does not exist, allocate it. */
74 h = talloc_zero(osmux, struct osmux_handle);
75 if (!h)
76 return NULL;
77
78 h->in = talloc_zero(osmux, struct osmux_in_handle);
79 if (!h->in) {
80 talloc_free(h);
81 return NULL;
82 }
83
84 h->in->osmux_seq = 0; /* sequence number to start OSmux message from */
85 h->in->batch_factor = cfg->osmux_batch;
86 h->in->deliver = osmux_deliver;
87 osmux_xfrm_input_init(h->in);
88 h->in->data = addr;
89
90 llist_add(&h->head, &osmux_handle_list);
91
92 LOGP(DMGCP, LOGL_DEBUG, "creating new OSMUX handle for addr=%s\n",
93 inet_ntoa(*addr));
94out:
95 return h->in;
96}
97
98int osmux_xfrm_to_osmux(int type, char *buf, int rc, struct mgcp_endpoint *endp)
99{
100 int ret;
101 struct msgb *msg;
102 struct in_addr *addr;
103 struct osmux_in_handle *in;
104
105 msg = msgb_alloc(4096, "RTP");
106 if (!msg)
107 return 0;
108
109 memcpy(msg->data, buf, rc);
110 msgb_put(msg, rc);
111
112 switch(type) {
113 case MGCP_DEST_NET:
114 addr = &endp->net_end.addr;
115 break;
116 case MGCP_DEST_BTS:
117 addr = &endp->bts_end.addr;
118 break;
119 default:
120 /* Should not ever happen */
121 LOGP(DMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type);
122 return 0;
123 }
124
125 /* Lookup for osmux input handle that munches this RTP frame */
126 in = osmux_handle_lookup(endp->cfg, addr);
127 if (!in) {
128 LOGP(DMGCP, LOGL_ERROR, "No osmux handle, aborting\n");
129 return 0;
130 }
131
132 LOGP(DMGCP, LOGL_DEBUG, "Osmux uses cid=%u from endpoint=%d (active=%d)\n",
133 endp->ci, ENDPOINT_NUMBER(endp), endp->allocated);
134
135 while ((ret = osmux_xfrm_input(in, msg, endp->ci)) > 0) {
136 /* batch full, build and deliver it */
137 osmux_xfrm_input_deliver(in);
138 }
139 return 0;
140}
141
142static struct mgcp_endpoint *
143endpoint_lookup(struct mgcp_config *cfg, int cid,
144 struct in_addr *from_addr, int type)
145{
146 struct mgcp_endpoint *tmp = NULL;
147 int i;
148
149 /* Lookup for the endpoint that corresponds to this port */
150 for (i=0; i<cfg->trunk.number_endpoints; i++) {
151 struct in_addr *this;
152
153 tmp = &cfg->trunk.endpoints[i];
154
155 if (!tmp->allocated)
156 continue;
157
158 switch(type) {
159 case MGCP_DEST_NET:
160 this = &tmp->net_end.addr;
161 break;
162 case MGCP_DEST_BTS:
163 this = &tmp->bts_end.addr;
164 break;
165 default:
166 /* Should not ever happen */
167 LOGP(DMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type);
168 return NULL;
169 }
170
171 if (tmp->ci == cid && this->s_addr == from_addr->s_addr)
172 return tmp;
173 }
174
175 LOGP(DMGCP, LOGL_ERROR, "Cannot find endpoint with cid=%d\n!\n", cid);
176
177 return NULL;
178}
179
180static void scheduled_tx_net_cb(struct msgb *msg, void *data)
181{
182 struct mgcp_endpoint *endp = data;
183 struct sockaddr_in addr;
184
185 mgcp_send(endp, MGCP_DEST_NET, 1, &addr, (char *)msg->data, msg->len);
186 msgb_free(msg);
187}
188
189static void scheduled_tx_bts_cb(struct msgb *msg, void *data)
190{
191 struct mgcp_endpoint *endp = data;
192 struct sockaddr_in addr;
193
194 mgcp_send(endp, MGCP_DEST_BTS, 1, &addr, (char *)msg->data, msg->len);
195 msgb_free(msg);
196}
197
198static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr)
199{
200 struct msgb *msg;
201 socklen_t slen = sizeof(*addr);
202 int ret;
203
204 msg = msgb_alloc(4096, "OSMUX");
205 if (!msg) {
206 LOGP(DMGCP, LOGL_ERROR, "cannot allocate message\n");
207 return NULL;
208 }
209 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0,
210 (struct sockaddr *)addr, &slen);
211 if (ret <= 0) {
212 msgb_free(msg);
213 LOGP(DMGCP, LOGL_ERROR, "cannot receive message\n");
214 return NULL;
215 }
216 msgb_put(msg, ret);
217
218 return msg;
219}
220
221int osmux_read_from_bsc_nat_cb(struct osmo_fd *ofd, unsigned int what)
222{
223 struct msgb *msg;
224 struct osmux_hdr *osmuxh;
225 struct llist_head list;
226 struct sockaddr_in addr;
227 struct mgcp_config *cfg = ofd->data;
228 char buf[4096];
229
230 msg = osmux_recv(ofd, &addr);
231 if (!msg)
232 return -1;
233
234 /* not any further processing dummy messages */
235 if (msg->data[0] == MGCP_DUMMY_LOAD)
236 goto out;
237
238 osmux_snprintf(buf, sizeof(buf), msg);
239 LOGP(DMGCP, LOGL_DEBUG, "received OSMUX message from "
240 "BSC NAT (len=%d) %s\n", msg->len, buf);
241
242 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
243 struct mgcp_endpoint *endp;
244
245 /* Yes, we use MGCP_DEST_NET to locate the origin */
246 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
247 &addr.sin_addr, MGCP_DEST_NET);
248 if (!endp) {
249 LOGP(DMGCP, LOGL_ERROR,
250 "Cannot find an endpoint for circuit_id=%d\n",
251 osmuxh->circuit_id);
252 goto out;
253 }
254
255 LOGP(DMGCP, LOGL_DEBUG,
256 "sending extracted RTP from OSMUX to BSC via endpoint=%u "
257 "(allocated=%d)\n", ENDPOINT_NUMBER(endp), endp->allocated);
258
259 osmux_xfrm_output(osmuxh, &endp->osmux_out, &list);
260 osmux_tx_sched(&list, scheduled_tx_bts_cb, endp);
261 }
262out:
263 msgb_free(msg);
264 return 0;
265}
266
267int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what)
268{
269 struct msgb *msg;
270 struct osmux_hdr *osmuxh;
271 struct llist_head list;
272 struct sockaddr_in addr;
273 struct mgcp_config *cfg = ofd->data;
274 char buf[4096];
275
276 msg = osmux_recv(ofd, &addr);
277 if (!msg)
278 return -1;
279
280 /* not any further processing dummy messages */
281 if (msg->data[0] == MGCP_DUMMY_LOAD)
282 goto out;
283
284 osmux_snprintf(buf, sizeof(buf), msg);
285 LOGP(DMGCP, LOGL_DEBUG, "received OSMUX message "
286 "from BSC (len=%d) %s\n", msg->len, buf);
287
288 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
289 struct mgcp_endpoint *endp;
290
291 /* Yes, we use MGCP_DEST_BTS to locate the origin */
292 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
293 &addr.sin_addr, MGCP_DEST_BTS);
294 if (!endp) {
295 LOGP(DMGCP, LOGL_ERROR,
296 "Cannot find an endpoint for circuit_id=%d\n",
297 osmuxh->circuit_id);
298 goto out;
299 }
300
301 LOGP(DMGCP, LOGL_DEBUG,
302 "sending extracted RTP from OSMUX to MSC via endpoint=%u "
303 "(allocated=%d)\n", ENDPOINT_NUMBER(endp), endp->allocated);
304
305 osmux_xfrm_output(osmuxh, &endp->osmux_out, &list);
306 osmux_tx_sched(&list, scheduled_tx_net_cb, endp);
307 }
308out:
309 msgb_free(msg);
310 return 0;
311}
312
313int osmux_init(int role, struct mgcp_config *cfg)
314{
315 int ret;
316
317 switch(role) {
318 case OSMUX_ROLE_BSC:
319 osmux_fd.cb = osmux_read_from_bsc_nat_cb;
320 break;
321 case OSMUX_ROLE_BSC_NAT:
322 osmux_fd.cb = osmux_read_from_bsc_cb;
323 break;
324 default:
325 LOGP(DMGCP, LOGL_ERROR, "wrong role for OSMUX\n");
326 return -1;
327 }
328 osmux_fd.data = cfg;
329
330 ret = mgcp_create_bind("0.0.0.0", &osmux_fd, OSMUX_PORT);
331 if (ret < 0) {
332 LOGP(DMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n");
333 return ret;
334 }
335 osmux_fd.when |= BSC_FD_READ;
336
337 ret = osmo_fd_register(&osmux_fd);
338 if (ret < 0) {
339 LOGP(DMGCP, LOGL_ERROR, "cannot register OSMUX socket\n");
340 return ret;
341 }
342 cfg->osmux_init = 1;
343
344 return 0;
345}
346
347int osmux_enable_endpoint(struct mgcp_endpoint *endp, int role)
348{
349 /* If osmux is enabled, initialize the output handler. This handler is
350 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
351 * allocated based on the circuit ID (endp->ci), which is unique in the
352 * local scope to the BSC/BSC-NAT. We use it to divide the RTP SSRC
353 * space (2^32) by the 256 possible circuit IDs, then randomly select
354 * one value from that window. Thus, we have no chance to have
355 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
356 * similarly, for flows traveling to the MSC.
357 */
358 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256;
359
360 if (!endp->cfg->osmux_init) {
361 if (osmux_init(role, endp->cfg) < 0) {
362 LOGP(DMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
363 return -1;
364 }
365 LOGP(DMGCP, LOGL_NOTICE, "OSMUX requested, ENABLING.\n");
366 }
367
368 osmux_xfrm_output_init(&endp->osmux_out,
369 (endp->ci * rtp_ssrc_winlen) +
370 (random() % rtp_ssrc_winlen));
371
372 switch (endp->cfg->role) {
373 case MGCP_BSC_NAT:
374 endp->type = MGCP_OSMUX_BSC_NAT;
375 break;
376 case MGCP_BSC:
377 endp->type = MGCP_OSMUX_BSC;
378 break;
379 }
380 endp->osmux = 1;
381
382 return 0;
383}
384
385/* We don't need to send the dummy load for osmux so often as another endpoint
386 * may have already punched the hole in the firewall. This approach is simple
387 * though.
388 */
389int osmux_send_dummy(struct mgcp_endpoint *endp)
390{
391 static char buf[] = { MGCP_DUMMY_LOAD };
392
393 LOGP(DMGCP, LOGL_DEBUG, "sending OSMUX dummy load to %s\n",
394 inet_ntoa(endp->net_end.addr));
395
396 return mgcp_udp_send(osmux_fd.fd, &endp->net_end.addr,
397 htons(OSMUX_PORT), buf, 1);
398}