blob: c57df005bdb3b6d52adae5de8091baa19488cdce [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 */
Pablo Neira Ayuso36a03bd2014-08-28 16:25:09 +020015#include <inttypes.h> /* for PRIu64 */
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010016#include <netinet/in.h>
17#include <osmocom/core/msgb.h>
18#include <osmocom/core/talloc.h>
19
20#include <osmocom/netif/osmux.h>
21#include <osmocom/netif/rtp.h>
22
23#include <openbsc/mgcp.h>
24#include <openbsc/mgcp_internal.h>
25#include <openbsc/osmux.h>
26
27#define OSMUX_PORT 1984
28
29static struct osmo_fd osmux_fd;
30
31static 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 Ayusob769f3c2014-08-27 17:02:52 +020038 int refcnt;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010039};
40
41static void *osmux;
42
43static void osmux_deliver(struct msgb *batch_msg, void *data)
44{
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020045 struct osmux_handle *handle = data;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010046 struct sockaddr_in out = {
47 .sin_family = AF_INET,
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020048 .sin_port = handle->rem_port,
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010049 };
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010050
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 sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0,
53 (struct sockaddr *)&out, sizeof(out));
54}
55
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020056static struct osmux_handle *
57osmux_handle_find_get(struct in_addr *addr, int rem_port)
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010058{
59 struct osmux_handle *h;
60
61 /* Lookup for existing OSMUX handle for this destination address. */
62 llist_for_each_entry(h, &osmux_handle_list, head) {
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020063 if (memcmp(&h->rem_addr, addr, sizeof(struct in_addr)) == 0 &&
64 h->rem_port == rem_port) {
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010065 LOGP(DMGCP, LOGL_DEBUG, "using existing OSMUX handle "
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020066 "for addr=%s:%d\n",
67 inet_ntoa(*addr), ntohs(rem_port));
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +020068 h->refcnt++;
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020069 return h;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010070 }
71 }
72
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020073 return NULL;
74}
75
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +020076static void osmux_handle_put(struct osmux_in_handle *in)
77{
78 struct osmux_handle *h;
79
80 /* Lookup for existing OSMUX handle for this destination address. */
81 llist_for_each_entry(h, &osmux_handle_list, head) {
82 if (h->in == in) {
83 if (--h->refcnt == 0) {
Pablo Neira Ayuso36a03bd2014-08-28 16:25:09 +020084 LOGP(DMGCP, LOGL_INFO,
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +020085 "Releasing unused osmux handle for %s:%d\n",
86 inet_ntoa(h->rem_addr),
87 ntohs(h->rem_port));
Pablo Neira Ayuso36a03bd2014-08-28 16:25:09 +020088 LOGP(DMGCP, LOGL_INFO, "Stats: "
89 "input RTP msgs: %u bytes: %"PRIu64" "
90 "output osmux msgs: %u bytes: %"PRIu64"\n",
91 in->stats.input_rtp_msgs,
92 in->stats.input_rtp_bytes,
93 in->stats.output_osmux_msgs,
94 in->stats.output_osmux_bytes);
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +020095 llist_del(&h->head);
Pablo Neira Ayuso72b187b2014-08-28 12:46:41 +020096 osmux_xfrm_input_fini(h->in);
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +020097 talloc_free(h);
98 }
99 return;
100 }
101 }
102 LOGP(DMGCP, LOGL_ERROR, "cannot find Osmux input handle %p\n", in);
103}
104
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +0200105static struct osmux_handle *
106osmux_handle_alloc(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
107{
108 struct osmux_handle *h;
109
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100110 h = talloc_zero(osmux, struct osmux_handle);
111 if (!h)
112 return NULL;
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200113 h->rem_addr = *addr;
114 h->rem_port = rem_port;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200115 h->refcnt++;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100116
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +0200117 h->in = talloc_zero(h, struct osmux_in_handle);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100118 if (!h->in) {
119 talloc_free(h);
120 return NULL;
121 }
122
123 h->in->osmux_seq = 0; /* sequence number to start OSmux message from */
124 h->in->batch_factor = cfg->osmux_batch;
125 h->in->deliver = osmux_deliver;
126 osmux_xfrm_input_init(h->in);
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200127 h->in->data = h;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100128
129 llist_add(&h->head, &osmux_handle_list);
130
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200131 LOGP(DMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s:%d\n",
132 inet_ntoa(*addr), ntohs(rem_port));
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +0200133
134 return h;
135}
136
137static struct osmux_in_handle *
138osmux_handle_lookup(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
139{
140 struct osmux_handle *h;
141
142 h = osmux_handle_find_get(addr, rem_port);
143 if (h != NULL)
144 return h->in;
145
146 h = osmux_handle_alloc(cfg, addr, rem_port);
147 if (h == NULL)
148 return NULL;
149
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100150 return h->in;
151}
152
153int osmux_xfrm_to_osmux(int type, char *buf, int rc, struct mgcp_endpoint *endp)
154{
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200155 int ret;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100156 struct msgb *msg;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100157
158 msg = msgb_alloc(4096, "RTP");
159 if (!msg)
160 return 0;
161
162 memcpy(msg->data, buf, rc);
163 msgb_put(msg, rc);
164
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;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100258
259 msg = osmux_recv(ofd, &addr);
260 if (!msg)
261 return -1;
262
263 /* not any further processing dummy messages */
264 if (msg->data[0] == MGCP_DUMMY_LOAD)
265 goto out;
266
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100267 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
268 struct mgcp_endpoint *endp;
269
270 /* Yes, we use MGCP_DEST_NET to locate the origin */
271 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
272 &addr.sin_addr, MGCP_DEST_NET);
273 if (!endp) {
274 LOGP(DMGCP, LOGL_ERROR,
275 "Cannot find an endpoint for circuit_id=%d\n",
276 osmuxh->circuit_id);
277 goto out;
278 }
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200279 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100280 osmux_tx_sched(&list, scheduled_tx_bts_cb, endp);
281 }
282out:
283 msgb_free(msg);
284 return 0;
285}
286
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200287/* This is called from the bsc-nat */
288static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr,
289 struct msgb *msg)
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200290{
291 struct mgcp_endpoint *endp;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200292 uint8_t osmux_cid;
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200293
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200294 if (msg->len < 1 + sizeof(osmux_cid)) {
295 LOGP(DMGCP, LOGL_ERROR,
296 "Discarding truncated Osmux dummy load\n");
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200297 goto out;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200298 }
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200299
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200300 LOGP(DMGCP, LOGL_DEBUG, "Received Osmux dummy load from %s\n",
301 inet_ntoa(addr->sin_addr));
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200302
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200303 if (!cfg->osmux) {
304 LOGP(DMGCP, LOGL_ERROR,
305 "bsc wants to use Osmux but bsc-nat did not request it\n");
306 goto out;
307 }
308
309 /* extract the osmux CID from the dummy message */
310 memcpy(&osmux_cid, &msg->data[1], sizeof(osmux_cid));
311
312 endp = endpoint_lookup(cfg, osmux_cid, &addr->sin_addr, MGCP_DEST_BTS);
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200313 if (!endp) {
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200314 LOGP(DMGCP, LOGL_ERROR,
315 "Cannot find endpoint for Osmux CID %d\n", osmux_cid);
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200316 goto out;
317 }
318
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200319 if (endp->osmux.state == OSMUX_STATE_ENABLED)
320 goto out;
321
322 if (osmux_enable_endpoint(endp, OSMUX_ROLE_BSC_NAT,
323 &addr->sin_addr, addr->sin_port) < 0 ){
324 LOGP(DMGCP, LOGL_ERROR,
Pablo Neira Ayuso1c810452014-08-28 15:35:58 +0200325 "Could not enable osmux in endpoint %d\n",
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200326 ENDPOINT_NUMBER(endp));
Pablo Neira Ayuso1c810452014-08-28 15:35:58 +0200327 goto out;
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200328 }
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200329
330 LOGP(DMGCP, LOGL_INFO, "Enabling osmux in endpoint %d for %s:%u\n",
331 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr),
332 ntohs(addr->sin_port));
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200333out:
334 msgb_free(msg);
335 return 0;
336}
337
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100338int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what)
339{
340 struct msgb *msg;
341 struct osmux_hdr *osmuxh;
342 struct llist_head list;
343 struct sockaddr_in addr;
344 struct mgcp_config *cfg = ofd->data;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100345
346 msg = osmux_recv(ofd, &addr);
347 if (!msg)
348 return -1;
349
350 /* not any further processing dummy messages */
351 if (msg->data[0] == MGCP_DUMMY_LOAD)
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200352 return osmux_handle_dummy(cfg, &addr, msg);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100353
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100354 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
355 struct mgcp_endpoint *endp;
356
357 /* Yes, we use MGCP_DEST_BTS to locate the origin */
358 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
359 &addr.sin_addr, MGCP_DEST_BTS);
360 if (!endp) {
361 LOGP(DMGCP, LOGL_ERROR,
362 "Cannot find an endpoint for circuit_id=%d\n",
363 osmuxh->circuit_id);
364 goto out;
365 }
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200366 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100367 osmux_tx_sched(&list, scheduled_tx_net_cb, endp);
368 }
369out:
370 msgb_free(msg);
371 return 0;
372}
373
374int osmux_init(int role, struct mgcp_config *cfg)
375{
376 int ret;
377
378 switch(role) {
379 case OSMUX_ROLE_BSC:
380 osmux_fd.cb = osmux_read_from_bsc_nat_cb;
381 break;
382 case OSMUX_ROLE_BSC_NAT:
383 osmux_fd.cb = osmux_read_from_bsc_cb;
384 break;
385 default:
386 LOGP(DMGCP, LOGL_ERROR, "wrong role for OSMUX\n");
387 return -1;
388 }
389 osmux_fd.data = cfg;
390
Pablo Neira Ayuso0fe78d32014-08-28 16:43:38 +0200391 if (!cfg->osmux_port)
392 cfg->osmux_port = OSMUX_PORT;
393
394 ret = mgcp_create_bind("0.0.0.0", &osmux_fd, cfg->osmux_port);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100395 if (ret < 0) {
396 LOGP(DMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n");
397 return ret;
398 }
399 osmux_fd.when |= BSC_FD_READ;
400
401 ret = osmo_fd_register(&osmux_fd);
402 if (ret < 0) {
403 LOGP(DMGCP, LOGL_ERROR, "cannot register OSMUX socket\n");
404 return ret;
405 }
406 cfg->osmux_init = 1;
407
408 return 0;
409}
410
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200411int osmux_enable_endpoint(struct mgcp_endpoint *endp, int role,
412 struct in_addr *addr, uint16_t port)
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100413{
414 /* If osmux is enabled, initialize the output handler. This handler is
415 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200416 * allocated based on the circuit ID (endp->osmux.cid), which is unique
417 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
418 * SSRC space (2^32) by the 256 possible circuit IDs, then randomly
419 * select one value from that window. Thus, we have no chance to have
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100420 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
421 * similarly, for flows traveling to the MSC.
422 */
423 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256;
424
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200425 if (endp->osmux.state == OSMUX_STATE_DISABLED) {
426 LOGP(DMGCP, LOGL_ERROR, "Endpoint %u didn't request Osmux\n",
427 ENDPOINT_NUMBER(endp));
428 return -1;
429 }
430
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200431 osmux_xfrm_output_init(&endp->osmux.out,
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200432 (endp->osmux.cid * rtp_ssrc_winlen) +
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100433 (random() % rtp_ssrc_winlen));
434
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200435 endp->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
436 if (!endp->osmux.in) {
437 LOGP(DMGCP, LOGL_ERROR, "Cannot allocate input osmux handle\n");
438 return -1;
439 }
440
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100441 switch (endp->cfg->role) {
442 case MGCP_BSC_NAT:
443 endp->type = MGCP_OSMUX_BSC_NAT;
444 break;
445 case MGCP_BSC:
446 endp->type = MGCP_OSMUX_BSC;
447 break;
448 }
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200449 endp->osmux.state = OSMUX_STATE_ENABLED;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100450
451 return 0;
452}
453
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200454void osmux_disable_endpoint(struct mgcp_endpoint *endp)
455{
456 LOGP(DMGCP, LOGL_INFO, "Releasing endpoint %u using Osmux CID %u\n",
457 ENDPOINT_NUMBER(endp), endp->osmux.cid);
458 endp->osmux.state = OSMUX_STATE_DISABLED;
459 endp->osmux.cid = -1;
460 osmux_handle_put(endp->osmux.in);
461}
462
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100463/* We don't need to send the dummy load for osmux so often as another endpoint
464 * may have already punched the hole in the firewall. This approach is simple
465 * though.
466 */
467int osmux_send_dummy(struct mgcp_endpoint *endp)
468{
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200469 char buf[1 + sizeof(uint8_t)];
470 struct in_addr addr_unset = {};
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200471
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200472 buf[0] = MGCP_DUMMY_LOAD;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200473 memcpy(&buf[1], &endp->osmux.cid, sizeof(endp->osmux.cid));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100474
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200475 /* Wait until we have the connection information from MDCX */
476 if (memcmp(&endp->net_end.addr, &addr_unset, sizeof(addr_unset)) == 0)
477 return 0;
478
479 if (endp->osmux.state == OSMUX_STATE_ACTIVATING) {
480 if (osmux_enable_endpoint(endp, OSMUX_ROLE_BSC,
481 &endp->net_end.addr,
Pablo Neira Ayusof8927632014-08-28 16:51:17 +0200482 htons(endp->cfg->osmux_port)) < 0) {
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200483 LOGP(DMGCP, LOGL_ERROR,
484 "Could not activate osmux in endpoint %d\n",
485 ENDPOINT_NUMBER(endp));
486 }
487 LOGP(DMGCP, LOGL_ERROR,
488 "Osmux CID %u for %s:%u is now enabled\n",
489 endp->osmux.cid, inet_ntoa(endp->net_end.addr),
Pablo Neira Ayusof8927632014-08-28 16:51:17 +0200490 endp->cfg->osmux_port);
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200491 }
492 LOGP(DMGCP, LOGL_DEBUG,
493 "sending OSMUX dummy load to %s CID %u\n",
494 inet_ntoa(endp->net_end.addr), endp->osmux.cid);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100495
496 return mgcp_udp_send(osmux_fd.fd, &endp->net_end.addr,
Pablo Neira Ayusof8927632014-08-28 16:51:17 +0200497 htons(endp->cfg->osmux_port), buf, sizeof(buf));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100498}
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200499
500/* bsc-nat allocates/releases the Osmux circuit ID */
501static uint8_t osmux_cid_bitmap[16];
502
503int osmux_get_cid(void)
504{
505 int i, j;
506
507 for (i = 0; i < sizeof(osmux_cid_bitmap) / 8; i++) {
508 for (j = 0; j < 8; j++) {
509 if (osmux_cid_bitmap[i] & (1 << j))
510 continue;
511
512 osmux_cid_bitmap[i] |= (1 << j);
513 LOGP(DMGCP, LOGL_DEBUG,
514 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
515 return (i * 8) + j;
516 }
517 }
518
519 LOGP(DMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n");
520 return -1;
521}
522
523void osmux_put_cid(uint8_t osmux_cid)
524{
525 LOGP(DMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
526 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
527}