blob: 0a58a2edb70061e4720d1ffc67feb11de232b70e [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;
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020035 struct in_addr rem_addr;
36 int rem_port;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +020037 int refcnt;
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 Ayusob769f3c2014-08-27 17:02:52 +020073 h->refcnt++;
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020074 return h;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010075 }
76 }
77
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020078 return NULL;
79}
80
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +020081static void osmux_handle_put(struct osmux_in_handle *in)
82{
83 struct osmux_handle *h;
84
85 /* Lookup for existing OSMUX handle for this destination address. */
86 llist_for_each_entry(h, &osmux_handle_list, head) {
87 if (h->in == in) {
88 if (--h->refcnt == 0) {
89 LOGP(DMGCP, LOGL_DEBUG,
90 "Releasing unused osmux handle for %s:%d\n",
91 inet_ntoa(h->rem_addr),
92 ntohs(h->rem_port));
93 llist_del(&h->head);
94 talloc_free(h);
95 }
96 return;
97 }
98 }
99 LOGP(DMGCP, LOGL_ERROR, "cannot find Osmux input handle %p\n", in);
100}
101
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +0200102static struct osmux_handle *
103osmux_handle_alloc(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
104{
105 struct osmux_handle *h;
106
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100107 h = talloc_zero(osmux, struct osmux_handle);
108 if (!h)
109 return NULL;
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200110 h->rem_addr = *addr;
111 h->rem_port = rem_port;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200112 h->refcnt++;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100113
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +0200114 h->in = talloc_zero(h, struct osmux_in_handle);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100115 if (!h->in) {
116 talloc_free(h);
117 return NULL;
118 }
119
120 h->in->osmux_seq = 0; /* sequence number to start OSmux message from */
121 h->in->batch_factor = cfg->osmux_batch;
122 h->in->deliver = osmux_deliver;
123 osmux_xfrm_input_init(h->in);
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200124 h->in->data = h;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100125
126 llist_add(&h->head, &osmux_handle_list);
127
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200128 LOGP(DMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s:%d\n",
129 inet_ntoa(*addr), ntohs(rem_port));
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +0200130
131 return h;
132}
133
134static struct osmux_in_handle *
135osmux_handle_lookup(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
136{
137 struct osmux_handle *h;
138
139 h = osmux_handle_find_get(addr, rem_port);
140 if (h != NULL)
141 return h->in;
142
143 h = osmux_handle_alloc(cfg, addr, rem_port);
144 if (h == NULL)
145 return NULL;
146
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100147 return h->in;
148}
149
150int osmux_xfrm_to_osmux(int type, char *buf, int rc, struct mgcp_endpoint *endp)
151{
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200152 int ret;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100153 struct msgb *msg;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100154
155 msg = msgb_alloc(4096, "RTP");
156 if (!msg)
157 return 0;
158
159 memcpy(msg->data, buf, rc);
160 msgb_put(msg, rc);
161
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200162 LOGP(DMGCP, LOGL_DEBUG, "Osmux uses CID %u from endpoint=%d (active=%d)\n",
163 endp->osmux.cid, ENDPOINT_NUMBER(endp), endp->allocated);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100164
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200165 while ((ret = osmux_xfrm_input(endp->osmux.in, msg, endp->osmux.cid)) > 0) {
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100166 /* batch full, build and deliver it */
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200167 osmux_xfrm_input_deliver(endp->osmux.in);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100168 }
169 return 0;
170}
171
172static struct mgcp_endpoint *
173endpoint_lookup(struct mgcp_config *cfg, int cid,
174 struct in_addr *from_addr, int type)
175{
176 struct mgcp_endpoint *tmp = NULL;
177 int i;
178
179 /* Lookup for the endpoint that corresponds to this port */
180 for (i=0; i<cfg->trunk.number_endpoints; i++) {
181 struct in_addr *this;
182
183 tmp = &cfg->trunk.endpoints[i];
184
185 if (!tmp->allocated)
186 continue;
187
188 switch(type) {
189 case MGCP_DEST_NET:
190 this = &tmp->net_end.addr;
191 break;
192 case MGCP_DEST_BTS:
193 this = &tmp->bts_end.addr;
194 break;
195 default:
196 /* Should not ever happen */
197 LOGP(DMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type);
198 return NULL;
199 }
200
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200201 if (tmp->osmux.cid == cid && this->s_addr == from_addr->s_addr)
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100202 return tmp;
203 }
204
Holger Hans Peter Freyther891b0a82014-07-08 09:08:34 +0200205 LOGP(DMGCP, LOGL_ERROR, "Cannot find endpoint with cid=%d\n", cid);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100206
207 return NULL;
208}
209
210static void scheduled_tx_net_cb(struct msgb *msg, void *data)
211{
212 struct mgcp_endpoint *endp = data;
213 struct sockaddr_in addr;
214
215 mgcp_send(endp, MGCP_DEST_NET, 1, &addr, (char *)msg->data, msg->len);
216 msgb_free(msg);
217}
218
219static void scheduled_tx_bts_cb(struct msgb *msg, void *data)
220{
221 struct mgcp_endpoint *endp = data;
222 struct sockaddr_in addr;
223
224 mgcp_send(endp, MGCP_DEST_BTS, 1, &addr, (char *)msg->data, msg->len);
225 msgb_free(msg);
226}
227
228static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr)
229{
230 struct msgb *msg;
231 socklen_t slen = sizeof(*addr);
232 int ret;
233
234 msg = msgb_alloc(4096, "OSMUX");
235 if (!msg) {
236 LOGP(DMGCP, LOGL_ERROR, "cannot allocate message\n");
237 return NULL;
238 }
239 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0,
240 (struct sockaddr *)addr, &slen);
241 if (ret <= 0) {
242 msgb_free(msg);
243 LOGP(DMGCP, LOGL_ERROR, "cannot receive message\n");
244 return NULL;
245 }
246 msgb_put(msg, ret);
247
248 return msg;
249}
250
251int osmux_read_from_bsc_nat_cb(struct osmo_fd *ofd, unsigned int what)
252{
253 struct msgb *msg;
254 struct osmux_hdr *osmuxh;
255 struct llist_head list;
256 struct sockaddr_in addr;
257 struct mgcp_config *cfg = ofd->data;
258 char buf[4096];
259
260 msg = osmux_recv(ofd, &addr);
261 if (!msg)
262 return -1;
263
264 /* not any further processing dummy messages */
265 if (msg->data[0] == MGCP_DUMMY_LOAD)
266 goto out;
267
268 osmux_snprintf(buf, sizeof(buf), msg);
269 LOGP(DMGCP, LOGL_DEBUG, "received OSMUX message from "
270 "BSC NAT (len=%d) %s\n", msg->len, buf);
271
272 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
273 struct mgcp_endpoint *endp;
274
275 /* Yes, we use MGCP_DEST_NET to locate the origin */
276 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
277 &addr.sin_addr, MGCP_DEST_NET);
278 if (!endp) {
279 LOGP(DMGCP, LOGL_ERROR,
280 "Cannot find an endpoint for circuit_id=%d\n",
281 osmuxh->circuit_id);
282 goto out;
283 }
284
285 LOGP(DMGCP, LOGL_DEBUG,
286 "sending extracted RTP from OSMUX to BSC via endpoint=%u "
287 "(allocated=%d)\n", ENDPOINT_NUMBER(endp), endp->allocated);
288
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200289 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100290 osmux_tx_sched(&list, scheduled_tx_bts_cb, endp);
291 }
292out:
293 msgb_free(msg);
294 return 0;
295}
296
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200297/* This is called from the bsc-nat */
298static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr,
299 struct msgb *msg)
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200300{
301 struct mgcp_endpoint *endp;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200302 uint8_t osmux_cid;
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200303
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200304 if (msg->len < 1 + sizeof(osmux_cid)) {
305 LOGP(DMGCP, LOGL_ERROR,
306 "Discarding truncated Osmux dummy load\n");
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200307 goto out;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200308 }
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200309
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200310 LOGP(DMGCP, LOGL_DEBUG, "Received Osmux dummy load from %s\n",
311 inet_ntoa(addr->sin_addr));
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200312
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200313 if (!cfg->osmux) {
314 LOGP(DMGCP, LOGL_ERROR,
315 "bsc wants to use Osmux but bsc-nat did not request it\n");
316 goto out;
317 }
318
319 /* extract the osmux CID from the dummy message */
320 memcpy(&osmux_cid, &msg->data[1], sizeof(osmux_cid));
321
322 endp = endpoint_lookup(cfg, osmux_cid, &addr->sin_addr, MGCP_DEST_BTS);
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200323 if (!endp) {
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200324 LOGP(DMGCP, LOGL_ERROR,
325 "Cannot find endpoint for Osmux CID %d\n", osmux_cid);
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200326 goto out;
327 }
328
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200329 if (endp->osmux.state == OSMUX_STATE_ENABLED)
330 goto out;
331
332 if (osmux_enable_endpoint(endp, OSMUX_ROLE_BSC_NAT,
333 &addr->sin_addr, addr->sin_port) < 0 ){
334 LOGP(DMGCP, LOGL_ERROR,
335 "Could not update osmux in endpoint %d\n",
336 ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200337 }
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200338
339 LOGP(DMGCP, LOGL_INFO, "Enabling osmux in endpoint %d for %s:%u\n",
340 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr),
341 ntohs(addr->sin_port));
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200342out:
343 msgb_free(msg);
344 return 0;
345}
346
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100347int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what)
348{
349 struct msgb *msg;
350 struct osmux_hdr *osmuxh;
351 struct llist_head list;
352 struct sockaddr_in addr;
353 struct mgcp_config *cfg = ofd->data;
354 char buf[4096];
355
356 msg = osmux_recv(ofd, &addr);
357 if (!msg)
358 return -1;
359
360 /* not any further processing dummy messages */
361 if (msg->data[0] == MGCP_DUMMY_LOAD)
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200362 return osmux_handle_dummy(cfg, &addr, msg);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100363
364 osmux_snprintf(buf, sizeof(buf), msg);
Holger Hans Peter Freyther9d43cee2014-07-08 23:12:56 +0200365 LOGP(DMGCP, LOGL_DEBUG,
366 "received OSMUX message from BSC(%s:%d) (len=%d) %s\n",
367 inet_ntoa(addr.sin_addr), ntohs(addr.sin_port),
368 msg->len, buf);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100369
370 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
371 struct mgcp_endpoint *endp;
372
373 /* Yes, we use MGCP_DEST_BTS to locate the origin */
374 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
375 &addr.sin_addr, MGCP_DEST_BTS);
376 if (!endp) {
377 LOGP(DMGCP, LOGL_ERROR,
378 "Cannot find an endpoint for circuit_id=%d\n",
379 osmuxh->circuit_id);
380 goto out;
381 }
382
383 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
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200429int osmux_enable_endpoint(struct mgcp_endpoint *endp, int role,
430 struct in_addr *addr, uint16_t port)
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100431{
432 /* If osmux is enabled, initialize the output handler. This handler is
433 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200434 * allocated based on the circuit ID (endp->osmux.cid), which is unique
435 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
436 * SSRC space (2^32) by the 256 possible circuit IDs, then randomly
437 * select one value from that window. Thus, we have no chance to have
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100438 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
439 * similarly, for flows traveling to the MSC.
440 */
441 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256;
442
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200443 if (endp->osmux.state == OSMUX_STATE_DISABLED) {
444 LOGP(DMGCP, LOGL_ERROR, "Endpoint %u didn't request Osmux\n",
445 ENDPOINT_NUMBER(endp));
446 return -1;
447 }
448
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200449 osmux_xfrm_output_init(&endp->osmux.out,
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200450 (endp->osmux.cid * rtp_ssrc_winlen) +
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100451 (random() % rtp_ssrc_winlen));
452
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200453 endp->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
454 if (!endp->osmux.in) {
455 LOGP(DMGCP, LOGL_ERROR, "Cannot allocate input osmux handle\n");
456 return -1;
457 }
458
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100459 switch (endp->cfg->role) {
460 case MGCP_BSC_NAT:
461 endp->type = MGCP_OSMUX_BSC_NAT;
462 break;
463 case MGCP_BSC:
464 endp->type = MGCP_OSMUX_BSC;
465 break;
466 }
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200467 endp->osmux.state = OSMUX_STATE_ENABLED;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100468
469 return 0;
470}
471
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200472void osmux_disable_endpoint(struct mgcp_endpoint *endp)
473{
474 LOGP(DMGCP, LOGL_INFO, "Releasing endpoint %u using Osmux CID %u\n",
475 ENDPOINT_NUMBER(endp), endp->osmux.cid);
476 endp->osmux.state = OSMUX_STATE_DISABLED;
477 endp->osmux.cid = -1;
478 osmux_handle_put(endp->osmux.in);
479}
480
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100481/* We don't need to send the dummy load for osmux so often as another endpoint
482 * may have already punched the hole in the firewall. This approach is simple
483 * though.
484 */
485int osmux_send_dummy(struct mgcp_endpoint *endp)
486{
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200487 char buf[1 + sizeof(uint8_t)];
488 struct in_addr addr_unset = {};
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200489
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200490 buf[0] = MGCP_DUMMY_LOAD;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200491 memcpy(&buf[1], &endp->osmux.cid, sizeof(endp->osmux.cid));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100492
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200493 /* Wait until we have the connection information from MDCX */
494 if (memcmp(&endp->net_end.addr, &addr_unset, sizeof(addr_unset)) == 0)
495 return 0;
496
497 if (endp->osmux.state == OSMUX_STATE_ACTIVATING) {
498 if (osmux_enable_endpoint(endp, OSMUX_ROLE_BSC,
499 &endp->net_end.addr,
500 htons(OSMUX_PORT)) < 0) {
501 LOGP(DMGCP, LOGL_ERROR,
502 "Could not activate osmux in endpoint %d\n",
503 ENDPOINT_NUMBER(endp));
504 }
505 LOGP(DMGCP, LOGL_ERROR,
506 "Osmux CID %u for %s:%u is now enabled\n",
507 endp->osmux.cid, inet_ntoa(endp->net_end.addr),
508 OSMUX_PORT);
509 }
510 LOGP(DMGCP, LOGL_DEBUG,
511 "sending OSMUX dummy load to %s CID %u\n",
512 inet_ntoa(endp->net_end.addr), endp->osmux.cid);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100513
514 return mgcp_udp_send(osmux_fd.fd, &endp->net_end.addr,
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200515 htons(OSMUX_PORT), buf, sizeof(buf));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100516}
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200517
518/* bsc-nat allocates/releases the Osmux circuit ID */
519static uint8_t osmux_cid_bitmap[16];
520
521int osmux_get_cid(void)
522{
523 int i, j;
524
525 for (i = 0; i < sizeof(osmux_cid_bitmap) / 8; i++) {
526 for (j = 0; j < 8; j++) {
527 if (osmux_cid_bitmap[i] & (1 << j))
528 continue;
529
530 osmux_cid_bitmap[i] |= (1 << j);
531 LOGP(DMGCP, LOGL_DEBUG,
532 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
533 return (i * 8) + j;
534 }
535 }
536
537 LOGP(DMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n");
538 return -1;
539}
540
541void osmux_put_cid(uint8_t osmux_cid)
542{
543 LOGP(DMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
544 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
545}