blob: a6deeaabb6c71206432fc6641b6ddcbb5131cd20 [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
Holger Hans Peter Freytherc6e90492014-07-09 01:38:29 +020030/* TODO: expire old handles.. */
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010031static LLIST_HEAD(osmux_handle_list);
32
33struct osmux_handle {
34 struct llist_head head;
35 struct osmux_in_handle *in;
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020036 struct in_addr rem_addr;
37 int rem_port;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010038};
39
40static void *osmux;
41
42static void osmux_deliver(struct msgb *batch_msg, void *data)
43{
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020044 struct osmux_handle *handle = data;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010045 struct sockaddr_in out = {
46 .sin_family = AF_INET,
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020047 .sin_port = handle->rem_port,
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010048 };
49 char buf[4096];
50
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020051 memcpy(&out.sin_addr, &handle->rem_addr, sizeof(handle->rem_addr));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010052
53 osmux_snprintf(buf, sizeof(buf), batch_msg);
54 LOGP(DMGCP, LOGL_DEBUG, "OSMUX delivering batch to addr=%s: %s\n",
55 inet_ntoa(out.sin_addr), buf);
56
57 sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0,
58 (struct sockaddr *)&out, sizeof(out));
59}
60
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020061static struct osmux_handle *
62osmux_handle_find_get(struct in_addr *addr, int rem_port)
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010063{
64 struct osmux_handle *h;
65
66 /* Lookup for existing OSMUX handle for this destination address. */
67 llist_for_each_entry(h, &osmux_handle_list, head) {
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020068 if (memcmp(&h->rem_addr, addr, sizeof(struct in_addr)) == 0 &&
69 h->rem_port == rem_port) {
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010070 LOGP(DMGCP, LOGL_DEBUG, "using existing OSMUX handle "
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020071 "for addr=%s:%d\n",
72 inet_ntoa(*addr), ntohs(rem_port));
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020073 return h;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010074 }
75 }
76
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020077 return NULL;
78}
79
80static struct osmux_handle *
81osmux_handle_alloc(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
82{
83 struct osmux_handle *h;
84
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010085 h = talloc_zero(osmux, struct osmux_handle);
86 if (!h)
87 return NULL;
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020088 h->rem_addr = *addr;
89 h->rem_port = rem_port;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010090
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020091 h->in = talloc_zero(h, struct osmux_in_handle);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010092 if (!h->in) {
93 talloc_free(h);
94 return NULL;
95 }
96
97 h->in->osmux_seq = 0; /* sequence number to start OSmux message from */
98 h->in->batch_factor = cfg->osmux_batch;
99 h->in->deliver = osmux_deliver;
100 osmux_xfrm_input_init(h->in);
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200101 h->in->data = h;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100102
103 llist_add(&h->head, &osmux_handle_list);
104
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200105 LOGP(DMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s:%d\n",
106 inet_ntoa(*addr), ntohs(rem_port));
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +0200107
108 return h;
109}
110
111static struct osmux_in_handle *
112osmux_handle_lookup(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
113{
114 struct osmux_handle *h;
115
116 h = osmux_handle_find_get(addr, rem_port);
117 if (h != NULL)
118 return h->in;
119
120 h = osmux_handle_alloc(cfg, addr, rem_port);
121 if (h == NULL)
122 return NULL;
123
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100124 return h->in;
125}
126
127int osmux_xfrm_to_osmux(int type, char *buf, int rc, struct mgcp_endpoint *endp)
128{
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200129 int ret, port;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100130 struct msgb *msg;
131 struct in_addr *addr;
132 struct osmux_in_handle *in;
133
134 msg = msgb_alloc(4096, "RTP");
135 if (!msg)
136 return 0;
137
138 memcpy(msg->data, buf, rc);
139 msgb_put(msg, rc);
140
141 switch(type) {
142 case MGCP_DEST_NET:
143 addr = &endp->net_end.addr;
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200144 port = htons(OSMUX_PORT);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100145 break;
146 case MGCP_DEST_BTS:
147 addr = &endp->bts_end.addr;
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200148 port = endp->bts_end.rtp_port;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100149 break;
150 default:
151 /* Should not ever happen */
152 LOGP(DMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type);
Holger Hans Peter Freyther4f200492014-05-22 15:23:22 +0200153 msgb_free(msg);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100154 return 0;
155 }
156
Holger Hans Peter Freythera5636412014-07-09 01:35:53 +0200157 if (port == 0) {
158 LOGP(DMGCP, LOGL_ERROR, "0x%x remote end not known yet.\n",
159 ENDPOINT_NUMBER(endp));
160 msgb_free(msg);
161 return 0;
162 }
163
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100164 /* Lookup for osmux input handle that munches this RTP frame */
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200165 in = osmux_handle_lookup(endp->cfg, addr, port);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100166 if (!in) {
167 LOGP(DMGCP, LOGL_ERROR, "No osmux handle, aborting\n");
Holger Hans Peter Freyther4f200492014-05-22 15:23:22 +0200168 msgb_free(msg);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100169 return 0;
170 }
171
172 LOGP(DMGCP, LOGL_DEBUG, "Osmux uses cid=%u from endpoint=%d (active=%d)\n",
173 endp->ci, ENDPOINT_NUMBER(endp), endp->allocated);
174
175 while ((ret = osmux_xfrm_input(in, msg, endp->ci)) > 0) {
176 /* batch full, build and deliver it */
177 osmux_xfrm_input_deliver(in);
178 }
179 return 0;
180}
181
182static struct mgcp_endpoint *
183endpoint_lookup(struct mgcp_config *cfg, int cid,
184 struct in_addr *from_addr, int type)
185{
186 struct mgcp_endpoint *tmp = NULL;
187 int i;
188
189 /* Lookup for the endpoint that corresponds to this port */
190 for (i=0; i<cfg->trunk.number_endpoints; i++) {
191 struct in_addr *this;
192
193 tmp = &cfg->trunk.endpoints[i];
194
195 if (!tmp->allocated)
196 continue;
197
198 switch(type) {
199 case MGCP_DEST_NET:
200 this = &tmp->net_end.addr;
201 break;
202 case MGCP_DEST_BTS:
203 this = &tmp->bts_end.addr;
204 break;
205 default:
206 /* Should not ever happen */
207 LOGP(DMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type);
208 return NULL;
209 }
210
Holger Hans Peter Freyther25a2db02014-07-09 00:28:02 +0200211 if ((tmp->ci & 0xFF) == cid && this->s_addr == from_addr->s_addr)
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100212 return tmp;
213 }
214
Holger Hans Peter Freyther891b0a82014-07-08 09:08:34 +0200215 LOGP(DMGCP, LOGL_ERROR, "Cannot find endpoint with cid=%d\n", cid);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100216
217 return NULL;
218}
219
220static void scheduled_tx_net_cb(struct msgb *msg, void *data)
221{
222 struct mgcp_endpoint *endp = data;
223 struct sockaddr_in addr;
224
225 mgcp_send(endp, MGCP_DEST_NET, 1, &addr, (char *)msg->data, msg->len);
226 msgb_free(msg);
227}
228
229static void scheduled_tx_bts_cb(struct msgb *msg, void *data)
230{
231 struct mgcp_endpoint *endp = data;
232 struct sockaddr_in addr;
233
234 mgcp_send(endp, MGCP_DEST_BTS, 1, &addr, (char *)msg->data, msg->len);
235 msgb_free(msg);
236}
237
238static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr)
239{
240 struct msgb *msg;
241 socklen_t slen = sizeof(*addr);
242 int ret;
243
244 msg = msgb_alloc(4096, "OSMUX");
245 if (!msg) {
246 LOGP(DMGCP, LOGL_ERROR, "cannot allocate message\n");
247 return NULL;
248 }
249 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0,
250 (struct sockaddr *)addr, &slen);
251 if (ret <= 0) {
252 msgb_free(msg);
253 LOGP(DMGCP, LOGL_ERROR, "cannot receive message\n");
254 return NULL;
255 }
256 msgb_put(msg, ret);
257
258 return msg;
259}
260
261int osmux_read_from_bsc_nat_cb(struct osmo_fd *ofd, unsigned int what)
262{
263 struct msgb *msg;
264 struct osmux_hdr *osmuxh;
265 struct llist_head list;
266 struct sockaddr_in addr;
267 struct mgcp_config *cfg = ofd->data;
268 char buf[4096];
269
270 msg = osmux_recv(ofd, &addr);
271 if (!msg)
272 return -1;
273
274 /* not any further processing dummy messages */
275 if (msg->data[0] == MGCP_DUMMY_LOAD)
276 goto out;
277
278 osmux_snprintf(buf, sizeof(buf), msg);
279 LOGP(DMGCP, LOGL_DEBUG, "received OSMUX message from "
280 "BSC NAT (len=%d) %s\n", msg->len, buf);
281
282 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
283 struct mgcp_endpoint *endp;
284
285 /* Yes, we use MGCP_DEST_NET to locate the origin */
286 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
287 &addr.sin_addr, MGCP_DEST_NET);
288 if (!endp) {
289 LOGP(DMGCP, LOGL_ERROR,
290 "Cannot find an endpoint for circuit_id=%d\n",
291 osmuxh->circuit_id);
292 goto out;
293 }
294
295 LOGP(DMGCP, LOGL_DEBUG,
296 "sending extracted RTP from OSMUX to BSC via endpoint=%u "
297 "(allocated=%d)\n", ENDPOINT_NUMBER(endp), endp->allocated);
298
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200299 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100300 osmux_tx_sched(&list, scheduled_tx_bts_cb, endp);
301 }
302out:
303 msgb_free(msg);
304 return 0;
305}
306
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200307/*
308 * Try to figure out where it came from and enter the rtp_port
309 */
310static int osmux_handle_dummy(struct mgcp_config *cfg,
311 struct sockaddr_in *addr, struct msgb *msg)
312{
313 struct mgcp_endpoint *endp;
314 uint32_t ci;
315
316 if (msg->len < 1 + sizeof(ci))
317 goto out;
318
319 /* extract the CI from the dummy message */
320 memcpy(&ci, &msg->data[1], sizeof(ci));
321 ci = ntohl(ci);
322
323 endp = endpoint_lookup(cfg, ci & 0xff, &addr->sin_addr, MGCP_DEST_BTS);
324 if (!endp) {
325 LOGP(DMGCP, LOGL_ERROR, "Can not find CI=%d\n", ci & 0xff);
326 goto out;
327 }
328
329 if (endp->bts_end.rtp_port == 0) {
330 endp->bts_end.rtp_port = addr->sin_port;
331 LOGP(DMGCP, LOGL_NOTICE, "0x%x found BTS on endpoint %s:%d\n",
332 ENDPOINT_NUMBER(endp),
333 inet_ntoa(addr->sin_addr), htons(addr->sin_port));
334 }
335out:
336 msgb_free(msg);
337 return 0;
338}
339
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100340int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what)
341{
342 struct msgb *msg;
343 struct osmux_hdr *osmuxh;
344 struct llist_head list;
345 struct sockaddr_in addr;
346 struct mgcp_config *cfg = ofd->data;
347 char buf[4096];
348
349 msg = osmux_recv(ofd, &addr);
350 if (!msg)
351 return -1;
352
353 /* not any further processing dummy messages */
354 if (msg->data[0] == MGCP_DUMMY_LOAD)
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200355 return osmux_handle_dummy(cfg, &addr, msg);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100356
357 osmux_snprintf(buf, sizeof(buf), msg);
Holger Hans Peter Freyther9d43cee2014-07-08 23:12:56 +0200358 LOGP(DMGCP, LOGL_DEBUG,
359 "received OSMUX message from BSC(%s:%d) (len=%d) %s\n",
360 inet_ntoa(addr.sin_addr), ntohs(addr.sin_port),
361 msg->len, buf);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100362
363 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
364 struct mgcp_endpoint *endp;
365
366 /* Yes, we use MGCP_DEST_BTS to locate the origin */
367 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
368 &addr.sin_addr, MGCP_DEST_BTS);
369 if (!endp) {
370 LOGP(DMGCP, LOGL_ERROR,
371 "Cannot find an endpoint for circuit_id=%d\n",
372 osmuxh->circuit_id);
373 goto out;
374 }
375
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200376 if (endp->bts_end.rtp_port == 0) {
377 endp->bts_end.rtp_port = addr.sin_port;
378 LOGP(DMGCP, LOGL_NOTICE, "0x%x found BTS on endpoint %s:%d\n",
379 ENDPOINT_NUMBER(endp),
380 inet_ntoa(addr.sin_addr), htons(addr.sin_port));
381 }
382
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100383 LOGP(DMGCP, LOGL_DEBUG,
384 "sending extracted RTP from OSMUX to MSC via endpoint=%u "
385 "(allocated=%d)\n", ENDPOINT_NUMBER(endp), endp->allocated);
386
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200387 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100388 osmux_tx_sched(&list, scheduled_tx_net_cb, endp);
389 }
390out:
391 msgb_free(msg);
392 return 0;
393}
394
395int osmux_init(int role, struct mgcp_config *cfg)
396{
397 int ret;
398
399 switch(role) {
400 case OSMUX_ROLE_BSC:
401 osmux_fd.cb = osmux_read_from_bsc_nat_cb;
402 break;
403 case OSMUX_ROLE_BSC_NAT:
404 osmux_fd.cb = osmux_read_from_bsc_cb;
405 break;
406 default:
407 LOGP(DMGCP, LOGL_ERROR, "wrong role for OSMUX\n");
408 return -1;
409 }
410 osmux_fd.data = cfg;
411
412 ret = mgcp_create_bind("0.0.0.0", &osmux_fd, OSMUX_PORT);
413 if (ret < 0) {
414 LOGP(DMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n");
415 return ret;
416 }
417 osmux_fd.when |= BSC_FD_READ;
418
419 ret = osmo_fd_register(&osmux_fd);
420 if (ret < 0) {
421 LOGP(DMGCP, LOGL_ERROR, "cannot register OSMUX socket\n");
422 return ret;
423 }
424 cfg->osmux_init = 1;
425
426 return 0;
427}
428
429int osmux_enable_endpoint(struct mgcp_endpoint *endp, int role)
430{
431 /* If osmux is enabled, initialize the output handler. This handler is
432 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
433 * allocated based on the circuit ID (endp->ci), which is unique in the
434 * local scope to the BSC/BSC-NAT. We use it to divide the RTP SSRC
435 * space (2^32) by the 256 possible circuit IDs, then randomly select
436 * one value from that window. Thus, we have no chance to have
437 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
438 * similarly, for flows traveling to the MSC.
439 */
440 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256;
441
442 if (!endp->cfg->osmux_init) {
443 if (osmux_init(role, endp->cfg) < 0) {
444 LOGP(DMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
445 return -1;
446 }
447 LOGP(DMGCP, LOGL_NOTICE, "OSMUX requested, ENABLING.\n");
448 }
449
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200450 osmux_xfrm_output_init(&endp->osmux.out,
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100451 (endp->ci * rtp_ssrc_winlen) +
452 (random() % rtp_ssrc_winlen));
453
454 switch (endp->cfg->role) {
455 case MGCP_BSC_NAT:
456 endp->type = MGCP_OSMUX_BSC_NAT;
457 break;
458 case MGCP_BSC:
459 endp->type = MGCP_OSMUX_BSC;
460 break;
461 }
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200462 endp->osmux.enable = 1;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100463
464 return 0;
465}
466
467/* We don't need to send the dummy load for osmux so often as another endpoint
468 * may have already punched the hole in the firewall. This approach is simple
469 * though.
470 */
471int osmux_send_dummy(struct mgcp_endpoint *endp)
472{
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200473 uint32_t ci_be;
474 char buf[1 + sizeof(uint32_t)];
475
476 ci_be = htonl(endp->ci);
477 buf[0] = MGCP_DUMMY_LOAD;
478 memcpy(&buf[1], &ci_be, sizeof(ci_be));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100479
480 LOGP(DMGCP, LOGL_DEBUG, "sending OSMUX dummy load to %s\n",
481 inet_ntoa(endp->net_end.addr));
482
483 return mgcp_udp_send(osmux_fd.fd, &endp->net_end.addr,
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200484 htons(OSMUX_PORT), buf, sizeof(buf));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100485}