blob: 15dd6133c3587ec223725a8af794d1f544ea1ed5 [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);
Pablo Neira Ayuso72b187b2014-08-28 12:46:41 +020094 osmux_xfrm_input_fini(h->in);
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +020095 talloc_free(h);
96 }
97 return;
98 }
99 }
100 LOGP(DMGCP, LOGL_ERROR, "cannot find Osmux input handle %p\n", in);
101}
102
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +0200103static struct osmux_handle *
104osmux_handle_alloc(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
105{
106 struct osmux_handle *h;
107
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100108 h = talloc_zero(osmux, struct osmux_handle);
109 if (!h)
110 return NULL;
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200111 h->rem_addr = *addr;
112 h->rem_port = rem_port;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200113 h->refcnt++;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100114
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +0200115 h->in = talloc_zero(h, struct osmux_in_handle);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100116 if (!h->in) {
117 talloc_free(h);
118 return NULL;
119 }
120
121 h->in->osmux_seq = 0; /* sequence number to start OSmux message from */
122 h->in->batch_factor = cfg->osmux_batch;
123 h->in->deliver = osmux_deliver;
124 osmux_xfrm_input_init(h->in);
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200125 h->in->data = h;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100126
127 llist_add(&h->head, &osmux_handle_list);
128
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200129 LOGP(DMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s:%d\n",
130 inet_ntoa(*addr), ntohs(rem_port));
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +0200131
132 return h;
133}
134
135static struct osmux_in_handle *
136osmux_handle_lookup(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
137{
138 struct osmux_handle *h;
139
140 h = osmux_handle_find_get(addr, rem_port);
141 if (h != NULL)
142 return h->in;
143
144 h = osmux_handle_alloc(cfg, addr, rem_port);
145 if (h == NULL)
146 return NULL;
147
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100148 return h->in;
149}
150
151int osmux_xfrm_to_osmux(int type, char *buf, int rc, struct mgcp_endpoint *endp)
152{
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200153 int ret;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100154 struct msgb *msg;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100155
156 msg = msgb_alloc(4096, "RTP");
157 if (!msg)
158 return 0;
159
160 memcpy(msg->data, buf, rc);
161 msgb_put(msg, rc);
162
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200163 LOGP(DMGCP, LOGL_DEBUG, "Osmux uses CID %u from endpoint=%d (active=%d)\n",
164 endp->osmux.cid, ENDPOINT_NUMBER(endp), endp->allocated);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100165
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200166 while ((ret = osmux_xfrm_input(endp->osmux.in, msg, endp->osmux.cid)) > 0) {
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100167 /* batch full, build and deliver it */
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200168 osmux_xfrm_input_deliver(endp->osmux.in);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100169 }
170 return 0;
171}
172
173static struct mgcp_endpoint *
174endpoint_lookup(struct mgcp_config *cfg, int cid,
175 struct in_addr *from_addr, int type)
176{
177 struct mgcp_endpoint *tmp = NULL;
178 int i;
179
180 /* Lookup for the endpoint that corresponds to this port */
181 for (i=0; i<cfg->trunk.number_endpoints; i++) {
182 struct in_addr *this;
183
184 tmp = &cfg->trunk.endpoints[i];
185
186 if (!tmp->allocated)
187 continue;
188
189 switch(type) {
190 case MGCP_DEST_NET:
191 this = &tmp->net_end.addr;
192 break;
193 case MGCP_DEST_BTS:
194 this = &tmp->bts_end.addr;
195 break;
196 default:
197 /* Should not ever happen */
198 LOGP(DMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type);
199 return NULL;
200 }
201
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200202 if (tmp->osmux.cid == cid && this->s_addr == from_addr->s_addr)
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100203 return tmp;
204 }
205
Holger Hans Peter Freyther891b0a82014-07-08 09:08:34 +0200206 LOGP(DMGCP, LOGL_ERROR, "Cannot find endpoint with cid=%d\n", cid);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100207
208 return NULL;
209}
210
211static void scheduled_tx_net_cb(struct msgb *msg, void *data)
212{
213 struct mgcp_endpoint *endp = data;
214 struct sockaddr_in addr;
215
216 mgcp_send(endp, MGCP_DEST_NET, 1, &addr, (char *)msg->data, msg->len);
217 msgb_free(msg);
218}
219
220static void scheduled_tx_bts_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_BTS, 1, &addr, (char *)msg->data, msg->len);
226 msgb_free(msg);
227}
228
229static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr)
230{
231 struct msgb *msg;
232 socklen_t slen = sizeof(*addr);
233 int ret;
234
235 msg = msgb_alloc(4096, "OSMUX");
236 if (!msg) {
237 LOGP(DMGCP, LOGL_ERROR, "cannot allocate message\n");
238 return NULL;
239 }
240 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0,
241 (struct sockaddr *)addr, &slen);
242 if (ret <= 0) {
243 msgb_free(msg);
244 LOGP(DMGCP, LOGL_ERROR, "cannot receive message\n");
245 return NULL;
246 }
247 msgb_put(msg, ret);
248
249 return msg;
250}
251
252int osmux_read_from_bsc_nat_cb(struct osmo_fd *ofd, unsigned int what)
253{
254 struct msgb *msg;
255 struct osmux_hdr *osmuxh;
256 struct llist_head list;
257 struct sockaddr_in addr;
258 struct mgcp_config *cfg = ofd->data;
259 char buf[4096];
260
261 msg = osmux_recv(ofd, &addr);
262 if (!msg)
263 return -1;
264
265 /* not any further processing dummy messages */
266 if (msg->data[0] == MGCP_DUMMY_LOAD)
267 goto out;
268
269 osmux_snprintf(buf, sizeof(buf), msg);
270 LOGP(DMGCP, LOGL_DEBUG, "received OSMUX message from "
271 "BSC NAT (len=%d) %s\n", msg->len, buf);
272
273 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
274 struct mgcp_endpoint *endp;
275
276 /* Yes, we use MGCP_DEST_NET to locate the origin */
277 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
278 &addr.sin_addr, MGCP_DEST_NET);
279 if (!endp) {
280 LOGP(DMGCP, LOGL_ERROR,
281 "Cannot find an endpoint for circuit_id=%d\n",
282 osmuxh->circuit_id);
283 goto out;
284 }
285
286 LOGP(DMGCP, LOGL_DEBUG,
287 "sending extracted RTP from OSMUX to BSC via endpoint=%u "
288 "(allocated=%d)\n", ENDPOINT_NUMBER(endp), endp->allocated);
289
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200290 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100291 osmux_tx_sched(&list, scheduled_tx_bts_cb, endp);
292 }
293out:
294 msgb_free(msg);
295 return 0;
296}
297
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200298/* This is called from the bsc-nat */
299static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr,
300 struct msgb *msg)
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200301{
302 struct mgcp_endpoint *endp;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200303 uint8_t osmux_cid;
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200304
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200305 if (msg->len < 1 + sizeof(osmux_cid)) {
306 LOGP(DMGCP, LOGL_ERROR,
307 "Discarding truncated Osmux dummy load\n");
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200308 goto out;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200309 }
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200310
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200311 LOGP(DMGCP, LOGL_DEBUG, "Received Osmux dummy load from %s\n",
312 inet_ntoa(addr->sin_addr));
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200313
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200314 if (!cfg->osmux) {
315 LOGP(DMGCP, LOGL_ERROR,
316 "bsc wants to use Osmux but bsc-nat did not request it\n");
317 goto out;
318 }
319
320 /* extract the osmux CID from the dummy message */
321 memcpy(&osmux_cid, &msg->data[1], sizeof(osmux_cid));
322
323 endp = endpoint_lookup(cfg, osmux_cid, &addr->sin_addr, MGCP_DEST_BTS);
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200324 if (!endp) {
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200325 LOGP(DMGCP, LOGL_ERROR,
326 "Cannot find endpoint for Osmux CID %d\n", osmux_cid);
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200327 goto out;
328 }
329
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200330 if (endp->osmux.state == OSMUX_STATE_ENABLED)
331 goto out;
332
333 if (osmux_enable_endpoint(endp, OSMUX_ROLE_BSC_NAT,
334 &addr->sin_addr, addr->sin_port) < 0 ){
335 LOGP(DMGCP, LOGL_ERROR,
336 "Could not update osmux in endpoint %d\n",
337 ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200338 }
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200339
340 LOGP(DMGCP, LOGL_INFO, "Enabling osmux in endpoint %d for %s:%u\n",
341 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr),
342 ntohs(addr->sin_port));
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200343out:
344 msgb_free(msg);
345 return 0;
346}
347
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100348int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what)
349{
350 struct msgb *msg;
351 struct osmux_hdr *osmuxh;
352 struct llist_head list;
353 struct sockaddr_in addr;
354 struct mgcp_config *cfg = ofd->data;
355 char buf[4096];
356
357 msg = osmux_recv(ofd, &addr);
358 if (!msg)
359 return -1;
360
361 /* not any further processing dummy messages */
362 if (msg->data[0] == MGCP_DUMMY_LOAD)
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200363 return osmux_handle_dummy(cfg, &addr, msg);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100364
365 osmux_snprintf(buf, sizeof(buf), msg);
Holger Hans Peter Freyther9d43cee2014-07-08 23:12:56 +0200366 LOGP(DMGCP, LOGL_DEBUG,
367 "received OSMUX message from BSC(%s:%d) (len=%d) %s\n",
368 inet_ntoa(addr.sin_addr), ntohs(addr.sin_port),
369 msg->len, buf);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100370
371 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
372 struct mgcp_endpoint *endp;
373
374 /* Yes, we use MGCP_DEST_BTS to locate the origin */
375 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
376 &addr.sin_addr, MGCP_DEST_BTS);
377 if (!endp) {
378 LOGP(DMGCP, LOGL_ERROR,
379 "Cannot find an endpoint for circuit_id=%d\n",
380 osmuxh->circuit_id);
381 goto out;
382 }
383
384 LOGP(DMGCP, LOGL_DEBUG,
385 "sending extracted RTP from OSMUX to MSC via endpoint=%u "
386 "(allocated=%d)\n", ENDPOINT_NUMBER(endp), endp->allocated);
387
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200388 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100389 osmux_tx_sched(&list, scheduled_tx_net_cb, endp);
390 }
391out:
392 msgb_free(msg);
393 return 0;
394}
395
396int osmux_init(int role, struct mgcp_config *cfg)
397{
398 int ret;
399
400 switch(role) {
401 case OSMUX_ROLE_BSC:
402 osmux_fd.cb = osmux_read_from_bsc_nat_cb;
403 break;
404 case OSMUX_ROLE_BSC_NAT:
405 osmux_fd.cb = osmux_read_from_bsc_cb;
406 break;
407 default:
408 LOGP(DMGCP, LOGL_ERROR, "wrong role for OSMUX\n");
409 return -1;
410 }
411 osmux_fd.data = cfg;
412
413 ret = mgcp_create_bind("0.0.0.0", &osmux_fd, OSMUX_PORT);
414 if (ret < 0) {
415 LOGP(DMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n");
416 return ret;
417 }
418 osmux_fd.when |= BSC_FD_READ;
419
420 ret = osmo_fd_register(&osmux_fd);
421 if (ret < 0) {
422 LOGP(DMGCP, LOGL_ERROR, "cannot register OSMUX socket\n");
423 return ret;
424 }
425 cfg->osmux_init = 1;
426
427 return 0;
428}
429
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200430int osmux_enable_endpoint(struct mgcp_endpoint *endp, int role,
431 struct in_addr *addr, uint16_t port)
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100432{
433 /* If osmux is enabled, initialize the output handler. This handler is
434 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200435 * allocated based on the circuit ID (endp->osmux.cid), which is unique
436 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
437 * SSRC space (2^32) by the 256 possible circuit IDs, then randomly
438 * select one value from that window. Thus, we have no chance to have
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100439 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
440 * similarly, for flows traveling to the MSC.
441 */
442 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256;
443
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200444 if (endp->osmux.state == OSMUX_STATE_DISABLED) {
445 LOGP(DMGCP, LOGL_ERROR, "Endpoint %u didn't request Osmux\n",
446 ENDPOINT_NUMBER(endp));
447 return -1;
448 }
449
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200450 osmux_xfrm_output_init(&endp->osmux.out,
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200451 (endp->osmux.cid * rtp_ssrc_winlen) +
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100452 (random() % rtp_ssrc_winlen));
453
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200454 endp->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
455 if (!endp->osmux.in) {
456 LOGP(DMGCP, LOGL_ERROR, "Cannot allocate input osmux handle\n");
457 return -1;
458 }
459
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100460 switch (endp->cfg->role) {
461 case MGCP_BSC_NAT:
462 endp->type = MGCP_OSMUX_BSC_NAT;
463 break;
464 case MGCP_BSC:
465 endp->type = MGCP_OSMUX_BSC;
466 break;
467 }
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200468 endp->osmux.state = OSMUX_STATE_ENABLED;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100469
470 return 0;
471}
472
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200473void osmux_disable_endpoint(struct mgcp_endpoint *endp)
474{
475 LOGP(DMGCP, LOGL_INFO, "Releasing endpoint %u using Osmux CID %u\n",
476 ENDPOINT_NUMBER(endp), endp->osmux.cid);
477 endp->osmux.state = OSMUX_STATE_DISABLED;
478 endp->osmux.cid = -1;
479 osmux_handle_put(endp->osmux.in);
480}
481
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100482/* We don't need to send the dummy load for osmux so often as another endpoint
483 * may have already punched the hole in the firewall. This approach is simple
484 * though.
485 */
486int osmux_send_dummy(struct mgcp_endpoint *endp)
487{
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200488 char buf[1 + sizeof(uint8_t)];
489 struct in_addr addr_unset = {};
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200490
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200491 buf[0] = MGCP_DUMMY_LOAD;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200492 memcpy(&buf[1], &endp->osmux.cid, sizeof(endp->osmux.cid));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100493
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200494 /* Wait until we have the connection information from MDCX */
495 if (memcmp(&endp->net_end.addr, &addr_unset, sizeof(addr_unset)) == 0)
496 return 0;
497
498 if (endp->osmux.state == OSMUX_STATE_ACTIVATING) {
499 if (osmux_enable_endpoint(endp, OSMUX_ROLE_BSC,
500 &endp->net_end.addr,
501 htons(OSMUX_PORT)) < 0) {
502 LOGP(DMGCP, LOGL_ERROR,
503 "Could not activate osmux in endpoint %d\n",
504 ENDPOINT_NUMBER(endp));
505 }
506 LOGP(DMGCP, LOGL_ERROR,
507 "Osmux CID %u for %s:%u is now enabled\n",
508 endp->osmux.cid, inet_ntoa(endp->net_end.addr),
509 OSMUX_PORT);
510 }
511 LOGP(DMGCP, LOGL_DEBUG,
512 "sending OSMUX dummy load to %s CID %u\n",
513 inet_ntoa(endp->net_end.addr), endp->osmux.cid);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100514
515 return mgcp_udp_send(osmux_fd.fd, &endp->net_end.addr,
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200516 htons(OSMUX_PORT), buf, sizeof(buf));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100517}
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200518
519/* bsc-nat allocates/releases the Osmux circuit ID */
520static uint8_t osmux_cid_bitmap[16];
521
522int osmux_get_cid(void)
523{
524 int i, j;
525
526 for (i = 0; i < sizeof(osmux_cid_bitmap) / 8; i++) {
527 for (j = 0; j < 8; j++) {
528 if (osmux_cid_bitmap[i] & (1 << j))
529 continue;
530
531 osmux_cid_bitmap[i] |= (1 << j);
532 LOGP(DMGCP, LOGL_DEBUG,
533 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
534 return (i * 8) + j;
535 }
536 }
537
538 LOGP(DMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n");
539 return -1;
540}
541
542void osmux_put_cid(uint8_t osmux_cid)
543{
544 LOGP(DMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
545 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
546}