blob: b7131833d3c161bb6adb602e38184de1822d87dc [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
61static struct osmux_in_handle *
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020062osmux_handle_lookup(struct mgcp_config *cfg, 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) {
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020068 if (memcmp(&h->rem_addr, addr, sizeof(struct in_addr)) == 0 && h->rem_port == rem_port) {
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010069 LOGP(DMGCP, LOGL_DEBUG, "using existing OSMUX handle "
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020070 "for addr=%s:%d\n",
71 inet_ntoa(*addr), ntohs(rem_port));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010072 goto out;
73 }
74 }
75
76 /* Does not exist, allocate it. */
77 h = talloc_zero(osmux, struct osmux_handle);
78 if (!h)
79 return NULL;
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020080 h->rem_addr = *addr;
81 h->rem_port = rem_port;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010082
83 h->in = talloc_zero(osmux, struct osmux_in_handle);
84 if (!h->in) {
85 talloc_free(h);
86 return NULL;
87 }
88
89 h->in->osmux_seq = 0; /* sequence number to start OSmux message from */
90 h->in->batch_factor = cfg->osmux_batch;
91 h->in->deliver = osmux_deliver;
92 osmux_xfrm_input_init(h->in);
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020093 h->in->data = h;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010094
95 llist_add(&h->head, &osmux_handle_list);
96
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020097 LOGP(DMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s:%d\n",
98 inet_ntoa(*addr), ntohs(rem_port));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010099out:
100 return h->in;
101}
102
103int osmux_xfrm_to_osmux(int type, char *buf, int rc, struct mgcp_endpoint *endp)
104{
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200105 int ret, port;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100106 struct msgb *msg;
107 struct in_addr *addr;
108 struct osmux_in_handle *in;
109
110 msg = msgb_alloc(4096, "RTP");
111 if (!msg)
112 return 0;
113
114 memcpy(msg->data, buf, rc);
115 msgb_put(msg, rc);
116
117 switch(type) {
118 case MGCP_DEST_NET:
119 addr = &endp->net_end.addr;
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200120 port = htons(OSMUX_PORT);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100121 break;
122 case MGCP_DEST_BTS:
123 addr = &endp->bts_end.addr;
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200124 port = endp->bts_end.rtp_port;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100125 break;
126 default:
127 /* Should not ever happen */
128 LOGP(DMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type);
Holger Hans Peter Freyther4f200492014-05-22 15:23:22 +0200129 msgb_free(msg);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100130 return 0;
131 }
132
133 /* Lookup for osmux input handle that munches this RTP frame */
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200134 in = osmux_handle_lookup(endp->cfg, addr, port);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100135 if (!in) {
136 LOGP(DMGCP, LOGL_ERROR, "No osmux handle, aborting\n");
Holger Hans Peter Freyther4f200492014-05-22 15:23:22 +0200137 msgb_free(msg);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100138 return 0;
139 }
140
141 LOGP(DMGCP, LOGL_DEBUG, "Osmux uses cid=%u from endpoint=%d (active=%d)\n",
142 endp->ci, ENDPOINT_NUMBER(endp), endp->allocated);
143
144 while ((ret = osmux_xfrm_input(in, msg, endp->ci)) > 0) {
145 /* batch full, build and deliver it */
146 osmux_xfrm_input_deliver(in);
147 }
148 return 0;
149}
150
151static struct mgcp_endpoint *
152endpoint_lookup(struct mgcp_config *cfg, int cid,
153 struct in_addr *from_addr, int type)
154{
155 struct mgcp_endpoint *tmp = NULL;
156 int i;
157
158 /* Lookup for the endpoint that corresponds to this port */
159 for (i=0; i<cfg->trunk.number_endpoints; i++) {
160 struct in_addr *this;
161
162 tmp = &cfg->trunk.endpoints[i];
163
164 if (!tmp->allocated)
165 continue;
166
167 switch(type) {
168 case MGCP_DEST_NET:
169 this = &tmp->net_end.addr;
170 break;
171 case MGCP_DEST_BTS:
172 this = &tmp->bts_end.addr;
173 break;
174 default:
175 /* Should not ever happen */
176 LOGP(DMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type);
177 return NULL;
178 }
179
Holger Hans Peter Freyther25a2db02014-07-09 00:28:02 +0200180 if ((tmp->ci & 0xFF) == cid && this->s_addr == from_addr->s_addr)
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100181 return tmp;
182 }
183
Holger Hans Peter Freyther891b0a82014-07-08 09:08:34 +0200184 LOGP(DMGCP, LOGL_ERROR, "Cannot find endpoint with cid=%d\n", cid);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100185
186 return NULL;
187}
188
189static void scheduled_tx_net_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_NET, 1, &addr, (char *)msg->data, msg->len);
195 msgb_free(msg);
196}
197
198static void scheduled_tx_bts_cb(struct msgb *msg, void *data)
199{
200 struct mgcp_endpoint *endp = data;
201 struct sockaddr_in addr;
202
203 mgcp_send(endp, MGCP_DEST_BTS, 1, &addr, (char *)msg->data, msg->len);
204 msgb_free(msg);
205}
206
207static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr)
208{
209 struct msgb *msg;
210 socklen_t slen = sizeof(*addr);
211 int ret;
212
213 msg = msgb_alloc(4096, "OSMUX");
214 if (!msg) {
215 LOGP(DMGCP, LOGL_ERROR, "cannot allocate message\n");
216 return NULL;
217 }
218 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0,
219 (struct sockaddr *)addr, &slen);
220 if (ret <= 0) {
221 msgb_free(msg);
222 LOGP(DMGCP, LOGL_ERROR, "cannot receive message\n");
223 return NULL;
224 }
225 msgb_put(msg, ret);
226
227 return msg;
228}
229
230int osmux_read_from_bsc_nat_cb(struct osmo_fd *ofd, unsigned int what)
231{
232 struct msgb *msg;
233 struct osmux_hdr *osmuxh;
234 struct llist_head list;
235 struct sockaddr_in addr;
236 struct mgcp_config *cfg = ofd->data;
237 char buf[4096];
238
239 msg = osmux_recv(ofd, &addr);
240 if (!msg)
241 return -1;
242
243 /* not any further processing dummy messages */
244 if (msg->data[0] == MGCP_DUMMY_LOAD)
245 goto out;
246
247 osmux_snprintf(buf, sizeof(buf), msg);
248 LOGP(DMGCP, LOGL_DEBUG, "received OSMUX message from "
249 "BSC NAT (len=%d) %s\n", msg->len, buf);
250
251 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
252 struct mgcp_endpoint *endp;
253
254 /* Yes, we use MGCP_DEST_NET to locate the origin */
255 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
256 &addr.sin_addr, MGCP_DEST_NET);
257 if (!endp) {
258 LOGP(DMGCP, LOGL_ERROR,
259 "Cannot find an endpoint for circuit_id=%d\n",
260 osmuxh->circuit_id);
261 goto out;
262 }
263
264 LOGP(DMGCP, LOGL_DEBUG,
265 "sending extracted RTP from OSMUX to BSC via endpoint=%u "
266 "(allocated=%d)\n", ENDPOINT_NUMBER(endp), endp->allocated);
267
268 osmux_xfrm_output(osmuxh, &endp->osmux_out, &list);
269 osmux_tx_sched(&list, scheduled_tx_bts_cb, endp);
270 }
271out:
272 msgb_free(msg);
273 return 0;
274}
275
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200276/*
277 * Try to figure out where it came from and enter the rtp_port
278 */
279static int osmux_handle_dummy(struct mgcp_config *cfg,
280 struct sockaddr_in *addr, struct msgb *msg)
281{
282 struct mgcp_endpoint *endp;
283 uint32_t ci;
284
285 if (msg->len < 1 + sizeof(ci))
286 goto out;
287
288 /* extract the CI from the dummy message */
289 memcpy(&ci, &msg->data[1], sizeof(ci));
290 ci = ntohl(ci);
291
292 endp = endpoint_lookup(cfg, ci & 0xff, &addr->sin_addr, MGCP_DEST_BTS);
293 if (!endp) {
294 LOGP(DMGCP, LOGL_ERROR, "Can not find CI=%d\n", ci & 0xff);
295 goto out;
296 }
297
298 if (endp->bts_end.rtp_port == 0) {
299 endp->bts_end.rtp_port = addr->sin_port;
300 LOGP(DMGCP, LOGL_NOTICE, "0x%x found BTS on endpoint %s:%d\n",
301 ENDPOINT_NUMBER(endp),
302 inet_ntoa(addr->sin_addr), htons(addr->sin_port));
303 }
304out:
305 msgb_free(msg);
306 return 0;
307}
308
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100309int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what)
310{
311 struct msgb *msg;
312 struct osmux_hdr *osmuxh;
313 struct llist_head list;
314 struct sockaddr_in addr;
315 struct mgcp_config *cfg = ofd->data;
316 char buf[4096];
317
318 msg = osmux_recv(ofd, &addr);
319 if (!msg)
320 return -1;
321
322 /* not any further processing dummy messages */
323 if (msg->data[0] == MGCP_DUMMY_LOAD)
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200324 return osmux_handle_dummy(cfg, &addr, msg);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100325
326 osmux_snprintf(buf, sizeof(buf), msg);
Holger Hans Peter Freyther9d43cee2014-07-08 23:12:56 +0200327 LOGP(DMGCP, LOGL_DEBUG,
328 "received OSMUX message from BSC(%s:%d) (len=%d) %s\n",
329 inet_ntoa(addr.sin_addr), ntohs(addr.sin_port),
330 msg->len, buf);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100331
332 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
333 struct mgcp_endpoint *endp;
334
335 /* Yes, we use MGCP_DEST_BTS to locate the origin */
336 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
337 &addr.sin_addr, MGCP_DEST_BTS);
338 if (!endp) {
339 LOGP(DMGCP, LOGL_ERROR,
340 "Cannot find an endpoint for circuit_id=%d\n",
341 osmuxh->circuit_id);
342 goto out;
343 }
344
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200345 if (endp->bts_end.rtp_port == 0) {
346 endp->bts_end.rtp_port = addr.sin_port;
347 LOGP(DMGCP, LOGL_NOTICE, "0x%x found BTS on endpoint %s:%d\n",
348 ENDPOINT_NUMBER(endp),
349 inet_ntoa(addr.sin_addr), htons(addr.sin_port));
350 }
351
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100352 LOGP(DMGCP, LOGL_DEBUG,
353 "sending extracted RTP from OSMUX to MSC via endpoint=%u "
354 "(allocated=%d)\n", ENDPOINT_NUMBER(endp), endp->allocated);
355
356 osmux_xfrm_output(osmuxh, &endp->osmux_out, &list);
357 osmux_tx_sched(&list, scheduled_tx_net_cb, endp);
358 }
359out:
360 msgb_free(msg);
361 return 0;
362}
363
364int osmux_init(int role, struct mgcp_config *cfg)
365{
366 int ret;
367
368 switch(role) {
369 case OSMUX_ROLE_BSC:
370 osmux_fd.cb = osmux_read_from_bsc_nat_cb;
371 break;
372 case OSMUX_ROLE_BSC_NAT:
373 osmux_fd.cb = osmux_read_from_bsc_cb;
374 break;
375 default:
376 LOGP(DMGCP, LOGL_ERROR, "wrong role for OSMUX\n");
377 return -1;
378 }
379 osmux_fd.data = cfg;
380
381 ret = mgcp_create_bind("0.0.0.0", &osmux_fd, OSMUX_PORT);
382 if (ret < 0) {
383 LOGP(DMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n");
384 return ret;
385 }
386 osmux_fd.when |= BSC_FD_READ;
387
388 ret = osmo_fd_register(&osmux_fd);
389 if (ret < 0) {
390 LOGP(DMGCP, LOGL_ERROR, "cannot register OSMUX socket\n");
391 return ret;
392 }
393 cfg->osmux_init = 1;
394
395 return 0;
396}
397
398int osmux_enable_endpoint(struct mgcp_endpoint *endp, int role)
399{
400 /* If osmux is enabled, initialize the output handler. This handler is
401 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
402 * allocated based on the circuit ID (endp->ci), which is unique in the
403 * local scope to the BSC/BSC-NAT. We use it to divide the RTP SSRC
404 * space (2^32) by the 256 possible circuit IDs, then randomly select
405 * one value from that window. Thus, we have no chance to have
406 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
407 * similarly, for flows traveling to the MSC.
408 */
409 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256;
410
411 if (!endp->cfg->osmux_init) {
412 if (osmux_init(role, endp->cfg) < 0) {
413 LOGP(DMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
414 return -1;
415 }
416 LOGP(DMGCP, LOGL_NOTICE, "OSMUX requested, ENABLING.\n");
417 }
418
419 osmux_xfrm_output_init(&endp->osmux_out,
420 (endp->ci * rtp_ssrc_winlen) +
421 (random() % rtp_ssrc_winlen));
422
423 switch (endp->cfg->role) {
424 case MGCP_BSC_NAT:
425 endp->type = MGCP_OSMUX_BSC_NAT;
426 break;
427 case MGCP_BSC:
428 endp->type = MGCP_OSMUX_BSC;
429 break;
430 }
431 endp->osmux = 1;
432
433 return 0;
434}
435
436/* We don't need to send the dummy load for osmux so often as another endpoint
437 * may have already punched the hole in the firewall. This approach is simple
438 * though.
439 */
440int osmux_send_dummy(struct mgcp_endpoint *endp)
441{
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200442 uint32_t ci_be;
443 char buf[1 + sizeof(uint32_t)];
444
445 ci_be = htonl(endp->ci);
446 buf[0] = MGCP_DUMMY_LOAD;
447 memcpy(&buf[1], &ci_be, sizeof(ci_be));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100448
449 LOGP(DMGCP, LOGL_DEBUG, "sending OSMUX dummy load to %s\n",
450 inet_ntoa(endp->net_end.addr));
451
452 return mgcp_udp_send(osmux_fd.fd, &endp->net_end.addr,
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200453 htons(OSMUX_PORT), buf, sizeof(buf));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100454}