blob: c64578bce497e2e11e67511ca2e4d8da596b515e [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 while ((ret = osmux_xfrm_input(endp->osmux.in, msg, endp->osmux.cid)) > 0) {
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100164 /* batch full, build and deliver it */
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200165 osmux_xfrm_input_deliver(endp->osmux.in);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100166 }
167 return 0;
168}
169
170static struct mgcp_endpoint *
171endpoint_lookup(struct mgcp_config *cfg, int cid,
172 struct in_addr *from_addr, int type)
173{
174 struct mgcp_endpoint *tmp = NULL;
175 int i;
176
177 /* Lookup for the endpoint that corresponds to this port */
178 for (i=0; i<cfg->trunk.number_endpoints; i++) {
179 struct in_addr *this;
180
181 tmp = &cfg->trunk.endpoints[i];
182
183 if (!tmp->allocated)
184 continue;
185
186 switch(type) {
187 case MGCP_DEST_NET:
188 this = &tmp->net_end.addr;
189 break;
190 case MGCP_DEST_BTS:
191 this = &tmp->bts_end.addr;
192 break;
193 default:
194 /* Should not ever happen */
195 LOGP(DMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type);
196 return NULL;
197 }
198
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200199 if (tmp->osmux.cid == cid && this->s_addr == from_addr->s_addr)
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100200 return tmp;
201 }
202
Holger Hans Peter Freyther891b0a82014-07-08 09:08:34 +0200203 LOGP(DMGCP, LOGL_ERROR, "Cannot find endpoint with cid=%d\n", cid);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100204
205 return NULL;
206}
207
208static void scheduled_tx_net_cb(struct msgb *msg, void *data)
209{
210 struct mgcp_endpoint *endp = data;
211 struct sockaddr_in addr;
212
213 mgcp_send(endp, MGCP_DEST_NET, 1, &addr, (char *)msg->data, msg->len);
214 msgb_free(msg);
215}
216
217static void scheduled_tx_bts_cb(struct msgb *msg, void *data)
218{
219 struct mgcp_endpoint *endp = data;
220 struct sockaddr_in addr;
221
222 mgcp_send(endp, MGCP_DEST_BTS, 1, &addr, (char *)msg->data, msg->len);
223 msgb_free(msg);
224}
225
226static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr)
227{
228 struct msgb *msg;
229 socklen_t slen = sizeof(*addr);
230 int ret;
231
232 msg = msgb_alloc(4096, "OSMUX");
233 if (!msg) {
234 LOGP(DMGCP, LOGL_ERROR, "cannot allocate message\n");
235 return NULL;
236 }
237 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0,
238 (struct sockaddr *)addr, &slen);
239 if (ret <= 0) {
240 msgb_free(msg);
241 LOGP(DMGCP, LOGL_ERROR, "cannot receive message\n");
242 return NULL;
243 }
244 msgb_put(msg, ret);
245
246 return msg;
247}
248
249int osmux_read_from_bsc_nat_cb(struct osmo_fd *ofd, unsigned int what)
250{
251 struct msgb *msg;
252 struct osmux_hdr *osmuxh;
253 struct llist_head list;
254 struct sockaddr_in addr;
255 struct mgcp_config *cfg = ofd->data;
256 char buf[4096];
257
258 msg = osmux_recv(ofd, &addr);
259 if (!msg)
260 return -1;
261
262 /* not any further processing dummy messages */
263 if (msg->data[0] == MGCP_DUMMY_LOAD)
264 goto out;
265
266 osmux_snprintf(buf, sizeof(buf), msg);
267 LOGP(DMGCP, LOGL_DEBUG, "received OSMUX message from "
268 "BSC NAT (len=%d) %s\n", msg->len, buf);
269
270 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
271 struct mgcp_endpoint *endp;
272
273 /* Yes, we use MGCP_DEST_NET to locate the origin */
274 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
275 &addr.sin_addr, MGCP_DEST_NET);
276 if (!endp) {
277 LOGP(DMGCP, LOGL_ERROR,
278 "Cannot find an endpoint for circuit_id=%d\n",
279 osmuxh->circuit_id);
280 goto out;
281 }
282
283 LOGP(DMGCP, LOGL_DEBUG,
284 "sending extracted RTP from OSMUX to BSC via endpoint=%u "
285 "(allocated=%d)\n", ENDPOINT_NUMBER(endp), endp->allocated);
286
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200287 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100288 osmux_tx_sched(&list, scheduled_tx_bts_cb, endp);
289 }
290out:
291 msgb_free(msg);
292 return 0;
293}
294
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200295/* This is called from the bsc-nat */
296static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr,
297 struct msgb *msg)
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200298{
299 struct mgcp_endpoint *endp;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200300 uint8_t osmux_cid;
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200301
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200302 if (msg->len < 1 + sizeof(osmux_cid)) {
303 LOGP(DMGCP, LOGL_ERROR,
304 "Discarding truncated Osmux dummy load\n");
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200305 goto out;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200306 }
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200307
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200308 LOGP(DMGCP, LOGL_DEBUG, "Received Osmux dummy load from %s\n",
309 inet_ntoa(addr->sin_addr));
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200310
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200311 if (!cfg->osmux) {
312 LOGP(DMGCP, LOGL_ERROR,
313 "bsc wants to use Osmux but bsc-nat did not request it\n");
314 goto out;
315 }
316
317 /* extract the osmux CID from the dummy message */
318 memcpy(&osmux_cid, &msg->data[1], sizeof(osmux_cid));
319
320 endp = endpoint_lookup(cfg, osmux_cid, &addr->sin_addr, MGCP_DEST_BTS);
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200321 if (!endp) {
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200322 LOGP(DMGCP, LOGL_ERROR,
323 "Cannot find endpoint for Osmux CID %d\n", osmux_cid);
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200324 goto out;
325 }
326
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200327 if (endp->osmux.state == OSMUX_STATE_ENABLED)
328 goto out;
329
330 if (osmux_enable_endpoint(endp, OSMUX_ROLE_BSC_NAT,
331 &addr->sin_addr, addr->sin_port) < 0 ){
332 LOGP(DMGCP, LOGL_ERROR,
Pablo Neira Ayuso1c810452014-08-28 15:35:58 +0200333 "Could not enable osmux in endpoint %d\n",
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200334 ENDPOINT_NUMBER(endp));
Pablo Neira Ayuso1c810452014-08-28 15:35:58 +0200335 goto out;
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200336 }
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200337
338 LOGP(DMGCP, LOGL_INFO, "Enabling osmux in endpoint %d for %s:%u\n",
339 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr),
340 ntohs(addr->sin_port));
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200341out:
342 msgb_free(msg);
343 return 0;
344}
345
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100346int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what)
347{
348 struct msgb *msg;
349 struct osmux_hdr *osmuxh;
350 struct llist_head list;
351 struct sockaddr_in addr;
352 struct mgcp_config *cfg = ofd->data;
353 char buf[4096];
354
355 msg = osmux_recv(ofd, &addr);
356 if (!msg)
357 return -1;
358
359 /* not any further processing dummy messages */
360 if (msg->data[0] == MGCP_DUMMY_LOAD)
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200361 return osmux_handle_dummy(cfg, &addr, msg);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100362
363 osmux_snprintf(buf, sizeof(buf), msg);
Holger Hans Peter Freyther9d43cee2014-07-08 23:12:56 +0200364 LOGP(DMGCP, LOGL_DEBUG,
365 "received OSMUX message from BSC(%s:%d) (len=%d) %s\n",
366 inet_ntoa(addr.sin_addr), ntohs(addr.sin_port),
367 msg->len, buf);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100368
369 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
370 struct mgcp_endpoint *endp;
371
372 /* Yes, we use MGCP_DEST_BTS to locate the origin */
373 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
374 &addr.sin_addr, MGCP_DEST_BTS);
375 if (!endp) {
376 LOGP(DMGCP, LOGL_ERROR,
377 "Cannot find an endpoint for circuit_id=%d\n",
378 osmuxh->circuit_id);
379 goto out;
380 }
381
382 LOGP(DMGCP, LOGL_DEBUG,
383 "sending extracted RTP from OSMUX to MSC via endpoint=%u "
384 "(allocated=%d)\n", ENDPOINT_NUMBER(endp), endp->allocated);
385
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200386 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100387 osmux_tx_sched(&list, scheduled_tx_net_cb, endp);
388 }
389out:
390 msgb_free(msg);
391 return 0;
392}
393
394int osmux_init(int role, struct mgcp_config *cfg)
395{
396 int ret;
397
398 switch(role) {
399 case OSMUX_ROLE_BSC:
400 osmux_fd.cb = osmux_read_from_bsc_nat_cb;
401 break;
402 case OSMUX_ROLE_BSC_NAT:
403 osmux_fd.cb = osmux_read_from_bsc_cb;
404 break;
405 default:
406 LOGP(DMGCP, LOGL_ERROR, "wrong role for OSMUX\n");
407 return -1;
408 }
409 osmux_fd.data = cfg;
410
411 ret = mgcp_create_bind("0.0.0.0", &osmux_fd, OSMUX_PORT);
412 if (ret < 0) {
413 LOGP(DMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n");
414 return ret;
415 }
416 osmux_fd.when |= BSC_FD_READ;
417
418 ret = osmo_fd_register(&osmux_fd);
419 if (ret < 0) {
420 LOGP(DMGCP, LOGL_ERROR, "cannot register OSMUX socket\n");
421 return ret;
422 }
423 cfg->osmux_init = 1;
424
425 return 0;
426}
427
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200428int osmux_enable_endpoint(struct mgcp_endpoint *endp, int role,
429 struct in_addr *addr, uint16_t port)
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100430{
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
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200433 * allocated based on the circuit ID (endp->osmux.cid), which is unique
434 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
435 * SSRC space (2^32) by the 256 possible circuit IDs, then randomly
436 * select one value from that window. Thus, we have no chance to have
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100437 * 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
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200442 if (endp->osmux.state == OSMUX_STATE_DISABLED) {
443 LOGP(DMGCP, LOGL_ERROR, "Endpoint %u didn't request Osmux\n",
444 ENDPOINT_NUMBER(endp));
445 return -1;
446 }
447
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200448 osmux_xfrm_output_init(&endp->osmux.out,
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200449 (endp->osmux.cid * rtp_ssrc_winlen) +
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100450 (random() % rtp_ssrc_winlen));
451
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200452 endp->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
453 if (!endp->osmux.in) {
454 LOGP(DMGCP, LOGL_ERROR, "Cannot allocate input osmux handle\n");
455 return -1;
456 }
457
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100458 switch (endp->cfg->role) {
459 case MGCP_BSC_NAT:
460 endp->type = MGCP_OSMUX_BSC_NAT;
461 break;
462 case MGCP_BSC:
463 endp->type = MGCP_OSMUX_BSC;
464 break;
465 }
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200466 endp->osmux.state = OSMUX_STATE_ENABLED;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100467
468 return 0;
469}
470
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200471void osmux_disable_endpoint(struct mgcp_endpoint *endp)
472{
473 LOGP(DMGCP, LOGL_INFO, "Releasing endpoint %u using Osmux CID %u\n",
474 ENDPOINT_NUMBER(endp), endp->osmux.cid);
475 endp->osmux.state = OSMUX_STATE_DISABLED;
476 endp->osmux.cid = -1;
477 osmux_handle_put(endp->osmux.in);
478}
479
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100480/* We don't need to send the dummy load for osmux so often as another endpoint
481 * may have already punched the hole in the firewall. This approach is simple
482 * though.
483 */
484int osmux_send_dummy(struct mgcp_endpoint *endp)
485{
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200486 char buf[1 + sizeof(uint8_t)];
487 struct in_addr addr_unset = {};
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200488
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200489 buf[0] = MGCP_DUMMY_LOAD;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200490 memcpy(&buf[1], &endp->osmux.cid, sizeof(endp->osmux.cid));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100491
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200492 /* Wait until we have the connection information from MDCX */
493 if (memcmp(&endp->net_end.addr, &addr_unset, sizeof(addr_unset)) == 0)
494 return 0;
495
496 if (endp->osmux.state == OSMUX_STATE_ACTIVATING) {
497 if (osmux_enable_endpoint(endp, OSMUX_ROLE_BSC,
498 &endp->net_end.addr,
499 htons(OSMUX_PORT)) < 0) {
500 LOGP(DMGCP, LOGL_ERROR,
501 "Could not activate osmux in endpoint %d\n",
502 ENDPOINT_NUMBER(endp));
503 }
504 LOGP(DMGCP, LOGL_ERROR,
505 "Osmux CID %u for %s:%u is now enabled\n",
506 endp->osmux.cid, inet_ntoa(endp->net_end.addr),
507 OSMUX_PORT);
508 }
509 LOGP(DMGCP, LOGL_DEBUG,
510 "sending OSMUX dummy load to %s CID %u\n",
511 inet_ntoa(endp->net_end.addr), endp->osmux.cid);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100512
513 return mgcp_udp_send(osmux_fd.fd, &endp->net_end.addr,
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200514 htons(OSMUX_PORT), buf, sizeof(buf));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100515}
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200516
517/* bsc-nat allocates/releases the Osmux circuit ID */
518static uint8_t osmux_cid_bitmap[16];
519
520int osmux_get_cid(void)
521{
522 int i, j;
523
524 for (i = 0; i < sizeof(osmux_cid_bitmap) / 8; i++) {
525 for (j = 0; j < 8; j++) {
526 if (osmux_cid_bitmap[i] & (1 << j))
527 continue;
528
529 osmux_cid_bitmap[i] |= (1 << j);
530 LOGP(DMGCP, LOGL_DEBUG,
531 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
532 return (i * 8) + j;
533 }
534 }
535
536 LOGP(DMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n");
537 return -1;
538}
539
540void osmux_put_cid(uint8_t osmux_cid)
541{
542 LOGP(DMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
543 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
544}