blob: eeafe394d8ba0caa3e56babdcd602e2a7f56d8b7 [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 };
50 char buf[4096];
51
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020052 memcpy(&out.sin_addr, &handle->rem_addr, sizeof(handle->rem_addr));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010053
54 osmux_snprintf(buf, sizeof(buf), batch_msg);
55 LOGP(DMGCP, LOGL_DEBUG, "OSMUX delivering batch to addr=%s: %s\n",
56 inet_ntoa(out.sin_addr), buf);
57
58 sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0,
59 (struct sockaddr *)&out, sizeof(out));
60}
61
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020062static struct osmux_handle *
63osmux_handle_find_get(struct in_addr *addr, int rem_port)
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010064{
65 struct osmux_handle *h;
66
67 /* Lookup for existing OSMUX handle for this destination address. */
68 llist_for_each_entry(h, &osmux_handle_list, head) {
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020069 if (memcmp(&h->rem_addr, addr, sizeof(struct in_addr)) == 0 &&
70 h->rem_port == rem_port) {
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010071 LOGP(DMGCP, LOGL_DEBUG, "using existing OSMUX handle "
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020072 "for addr=%s:%d\n",
73 inet_ntoa(*addr), ntohs(rem_port));
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +020074 h->refcnt++;
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020075 return h;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010076 }
77 }
78
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020079 return NULL;
80}
81
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +020082static void osmux_handle_put(struct osmux_in_handle *in)
83{
84 struct osmux_handle *h;
85
86 /* Lookup for existing OSMUX handle for this destination address. */
87 llist_for_each_entry(h, &osmux_handle_list, head) {
88 if (h->in == in) {
89 if (--h->refcnt == 0) {
Pablo Neira Ayuso36a03bd2014-08-28 16:25:09 +020090 LOGP(DMGCP, LOGL_INFO,
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +020091 "Releasing unused osmux handle for %s:%d\n",
92 inet_ntoa(h->rem_addr),
93 ntohs(h->rem_port));
Pablo Neira Ayuso36a03bd2014-08-28 16:25:09 +020094 LOGP(DMGCP, LOGL_INFO, "Stats: "
95 "input RTP msgs: %u bytes: %"PRIu64" "
96 "output osmux msgs: %u bytes: %"PRIu64"\n",
97 in->stats.input_rtp_msgs,
98 in->stats.input_rtp_bytes,
99 in->stats.output_osmux_msgs,
100 in->stats.output_osmux_bytes);
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200101 llist_del(&h->head);
Pablo Neira Ayuso72b187b2014-08-28 12:46:41 +0200102 osmux_xfrm_input_fini(h->in);
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200103 talloc_free(h);
104 }
105 return;
106 }
107 }
108 LOGP(DMGCP, LOGL_ERROR, "cannot find Osmux input handle %p\n", in);
109}
110
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +0200111static struct osmux_handle *
112osmux_handle_alloc(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
113{
114 struct osmux_handle *h;
115
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100116 h = talloc_zero(osmux, struct osmux_handle);
117 if (!h)
118 return NULL;
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200119 h->rem_addr = *addr;
120 h->rem_port = rem_port;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200121 h->refcnt++;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100122
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +0200123 h->in = talloc_zero(h, struct osmux_in_handle);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100124 if (!h->in) {
125 talloc_free(h);
126 return NULL;
127 }
128
129 h->in->osmux_seq = 0; /* sequence number to start OSmux message from */
130 h->in->batch_factor = cfg->osmux_batch;
131 h->in->deliver = osmux_deliver;
132 osmux_xfrm_input_init(h->in);
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200133 h->in->data = h;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100134
135 llist_add(&h->head, &osmux_handle_list);
136
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200137 LOGP(DMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s:%d\n",
138 inet_ntoa(*addr), ntohs(rem_port));
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +0200139
140 return h;
141}
142
143static struct osmux_in_handle *
144osmux_handle_lookup(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
145{
146 struct osmux_handle *h;
147
148 h = osmux_handle_find_get(addr, rem_port);
149 if (h != NULL)
150 return h->in;
151
152 h = osmux_handle_alloc(cfg, addr, rem_port);
153 if (h == NULL)
154 return NULL;
155
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100156 return h->in;
157}
158
159int osmux_xfrm_to_osmux(int type, char *buf, int rc, struct mgcp_endpoint *endp)
160{
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200161 int ret;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100162 struct msgb *msg;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100163
164 msg = msgb_alloc(4096, "RTP");
165 if (!msg)
166 return 0;
167
168 memcpy(msg->data, buf, rc);
169 msgb_put(msg, rc);
170
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200171 while ((ret = osmux_xfrm_input(endp->osmux.in, msg, endp->osmux.cid)) > 0) {
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100172 /* batch full, build and deliver it */
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200173 osmux_xfrm_input_deliver(endp->osmux.in);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100174 }
175 return 0;
176}
177
178static struct mgcp_endpoint *
179endpoint_lookup(struct mgcp_config *cfg, int cid,
180 struct in_addr *from_addr, int type)
181{
182 struct mgcp_endpoint *tmp = NULL;
183 int i;
184
185 /* Lookup for the endpoint that corresponds to this port */
186 for (i=0; i<cfg->trunk.number_endpoints; i++) {
187 struct in_addr *this;
188
189 tmp = &cfg->trunk.endpoints[i];
190
191 if (!tmp->allocated)
192 continue;
193
194 switch(type) {
195 case MGCP_DEST_NET:
196 this = &tmp->net_end.addr;
197 break;
198 case MGCP_DEST_BTS:
199 this = &tmp->bts_end.addr;
200 break;
201 default:
202 /* Should not ever happen */
203 LOGP(DMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type);
204 return NULL;
205 }
206
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200207 if (tmp->osmux.cid == cid && this->s_addr == from_addr->s_addr)
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100208 return tmp;
209 }
210
Holger Hans Peter Freyther891b0a82014-07-08 09:08:34 +0200211 LOGP(DMGCP, LOGL_ERROR, "Cannot find endpoint with cid=%d\n", cid);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100212
213 return NULL;
214}
215
216static void scheduled_tx_net_cb(struct msgb *msg, void *data)
217{
218 struct mgcp_endpoint *endp = data;
219 struct sockaddr_in addr;
220
221 mgcp_send(endp, MGCP_DEST_NET, 1, &addr, (char *)msg->data, msg->len);
222 msgb_free(msg);
223}
224
225static void scheduled_tx_bts_cb(struct msgb *msg, void *data)
226{
227 struct mgcp_endpoint *endp = data;
228 struct sockaddr_in addr;
229
230 mgcp_send(endp, MGCP_DEST_BTS, 1, &addr, (char *)msg->data, msg->len);
231 msgb_free(msg);
232}
233
234static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr)
235{
236 struct msgb *msg;
237 socklen_t slen = sizeof(*addr);
238 int ret;
239
240 msg = msgb_alloc(4096, "OSMUX");
241 if (!msg) {
242 LOGP(DMGCP, LOGL_ERROR, "cannot allocate message\n");
243 return NULL;
244 }
245 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0,
246 (struct sockaddr *)addr, &slen);
247 if (ret <= 0) {
248 msgb_free(msg);
249 LOGP(DMGCP, LOGL_ERROR, "cannot receive message\n");
250 return NULL;
251 }
252 msgb_put(msg, ret);
253
254 return msg;
255}
256
257int osmux_read_from_bsc_nat_cb(struct osmo_fd *ofd, unsigned int what)
258{
259 struct msgb *msg;
260 struct osmux_hdr *osmuxh;
261 struct llist_head list;
262 struct sockaddr_in addr;
263 struct mgcp_config *cfg = ofd->data;
264 char buf[4096];
265
266 msg = osmux_recv(ofd, &addr);
267 if (!msg)
268 return -1;
269
270 /* not any further processing dummy messages */
271 if (msg->data[0] == MGCP_DUMMY_LOAD)
272 goto out;
273
274 osmux_snprintf(buf, sizeof(buf), msg);
275 LOGP(DMGCP, LOGL_DEBUG, "received OSMUX message from "
276 "BSC NAT (len=%d) %s\n", msg->len, buf);
277
278 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
279 struct mgcp_endpoint *endp;
280
281 /* Yes, we use MGCP_DEST_NET to locate the origin */
282 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
283 &addr.sin_addr, MGCP_DEST_NET);
284 if (!endp) {
285 LOGP(DMGCP, LOGL_ERROR,
286 "Cannot find an endpoint for circuit_id=%d\n",
287 osmuxh->circuit_id);
288 goto out;
289 }
290
291 LOGP(DMGCP, LOGL_DEBUG,
292 "sending extracted RTP from OSMUX to BSC via endpoint=%u "
293 "(allocated=%d)\n", ENDPOINT_NUMBER(endp), endp->allocated);
294
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200295 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100296 osmux_tx_sched(&list, scheduled_tx_bts_cb, endp);
297 }
298out:
299 msgb_free(msg);
300 return 0;
301}
302
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200303/* This is called from the bsc-nat */
304static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr,
305 struct msgb *msg)
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200306{
307 struct mgcp_endpoint *endp;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200308 uint8_t osmux_cid;
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200309
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200310 if (msg->len < 1 + sizeof(osmux_cid)) {
311 LOGP(DMGCP, LOGL_ERROR,
312 "Discarding truncated Osmux dummy load\n");
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200313 goto out;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200314 }
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200315
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200316 LOGP(DMGCP, LOGL_DEBUG, "Received Osmux dummy load from %s\n",
317 inet_ntoa(addr->sin_addr));
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200318
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200319 if (!cfg->osmux) {
320 LOGP(DMGCP, LOGL_ERROR,
321 "bsc wants to use Osmux but bsc-nat did not request it\n");
322 goto out;
323 }
324
325 /* extract the osmux CID from the dummy message */
326 memcpy(&osmux_cid, &msg->data[1], sizeof(osmux_cid));
327
328 endp = endpoint_lookup(cfg, osmux_cid, &addr->sin_addr, MGCP_DEST_BTS);
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200329 if (!endp) {
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200330 LOGP(DMGCP, LOGL_ERROR,
331 "Cannot find endpoint for Osmux CID %d\n", osmux_cid);
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200332 goto out;
333 }
334
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200335 if (endp->osmux.state == OSMUX_STATE_ENABLED)
336 goto out;
337
338 if (osmux_enable_endpoint(endp, OSMUX_ROLE_BSC_NAT,
339 &addr->sin_addr, addr->sin_port) < 0 ){
340 LOGP(DMGCP, LOGL_ERROR,
Pablo Neira Ayuso1c810452014-08-28 15:35:58 +0200341 "Could not enable osmux in endpoint %d\n",
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200342 ENDPOINT_NUMBER(endp));
Pablo Neira Ayuso1c810452014-08-28 15:35:58 +0200343 goto out;
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200344 }
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200345
346 LOGP(DMGCP, LOGL_INFO, "Enabling osmux in endpoint %d for %s:%u\n",
347 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr),
348 ntohs(addr->sin_port));
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200349out:
350 msgb_free(msg);
351 return 0;
352}
353
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100354int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what)
355{
356 struct msgb *msg;
357 struct osmux_hdr *osmuxh;
358 struct llist_head list;
359 struct sockaddr_in addr;
360 struct mgcp_config *cfg = ofd->data;
361 char buf[4096];
362
363 msg = osmux_recv(ofd, &addr);
364 if (!msg)
365 return -1;
366
367 /* not any further processing dummy messages */
368 if (msg->data[0] == MGCP_DUMMY_LOAD)
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200369 return osmux_handle_dummy(cfg, &addr, msg);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100370
371 osmux_snprintf(buf, sizeof(buf), msg);
Holger Hans Peter Freyther9d43cee2014-07-08 23:12:56 +0200372 LOGP(DMGCP, LOGL_DEBUG,
373 "received OSMUX message from BSC(%s:%d) (len=%d) %s\n",
374 inet_ntoa(addr.sin_addr), ntohs(addr.sin_port),
375 msg->len, buf);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100376
377 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
378 struct mgcp_endpoint *endp;
379
380 /* Yes, we use MGCP_DEST_BTS to locate the origin */
381 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
382 &addr.sin_addr, MGCP_DEST_BTS);
383 if (!endp) {
384 LOGP(DMGCP, LOGL_ERROR,
385 "Cannot find an endpoint for circuit_id=%d\n",
386 osmuxh->circuit_id);
387 goto out;
388 }
389
390 LOGP(DMGCP, LOGL_DEBUG,
391 "sending extracted RTP from OSMUX to MSC via endpoint=%u "
392 "(allocated=%d)\n", ENDPOINT_NUMBER(endp), endp->allocated);
393
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200394 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100395 osmux_tx_sched(&list, scheduled_tx_net_cb, endp);
396 }
397out:
398 msgb_free(msg);
399 return 0;
400}
401
402int osmux_init(int role, struct mgcp_config *cfg)
403{
404 int ret;
405
406 switch(role) {
407 case OSMUX_ROLE_BSC:
408 osmux_fd.cb = osmux_read_from_bsc_nat_cb;
409 break;
410 case OSMUX_ROLE_BSC_NAT:
411 osmux_fd.cb = osmux_read_from_bsc_cb;
412 break;
413 default:
414 LOGP(DMGCP, LOGL_ERROR, "wrong role for OSMUX\n");
415 return -1;
416 }
417 osmux_fd.data = cfg;
418
Pablo Neira Ayuso0fe78d32014-08-28 16:43:38 +0200419 if (!cfg->osmux_port)
420 cfg->osmux_port = OSMUX_PORT;
421
422 ret = mgcp_create_bind("0.0.0.0", &osmux_fd, cfg->osmux_port);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100423 if (ret < 0) {
424 LOGP(DMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n");
425 return ret;
426 }
427 osmux_fd.when |= BSC_FD_READ;
428
429 ret = osmo_fd_register(&osmux_fd);
430 if (ret < 0) {
431 LOGP(DMGCP, LOGL_ERROR, "cannot register OSMUX socket\n");
432 return ret;
433 }
434 cfg->osmux_init = 1;
435
436 return 0;
437}
438
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200439int osmux_enable_endpoint(struct mgcp_endpoint *endp, int role,
440 struct in_addr *addr, uint16_t port)
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100441{
442 /* If osmux is enabled, initialize the output handler. This handler is
443 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200444 * allocated based on the circuit ID (endp->osmux.cid), which is unique
445 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
446 * SSRC space (2^32) by the 256 possible circuit IDs, then randomly
447 * select one value from that window. Thus, we have no chance to have
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100448 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
449 * similarly, for flows traveling to the MSC.
450 */
451 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256;
452
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200453 if (endp->osmux.state == OSMUX_STATE_DISABLED) {
454 LOGP(DMGCP, LOGL_ERROR, "Endpoint %u didn't request Osmux\n",
455 ENDPOINT_NUMBER(endp));
456 return -1;
457 }
458
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200459 osmux_xfrm_output_init(&endp->osmux.out,
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200460 (endp->osmux.cid * rtp_ssrc_winlen) +
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100461 (random() % rtp_ssrc_winlen));
462
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200463 endp->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
464 if (!endp->osmux.in) {
465 LOGP(DMGCP, LOGL_ERROR, "Cannot allocate input osmux handle\n");
466 return -1;
467 }
468
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100469 switch (endp->cfg->role) {
470 case MGCP_BSC_NAT:
471 endp->type = MGCP_OSMUX_BSC_NAT;
472 break;
473 case MGCP_BSC:
474 endp->type = MGCP_OSMUX_BSC;
475 break;
476 }
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200477 endp->osmux.state = OSMUX_STATE_ENABLED;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100478
479 return 0;
480}
481
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200482void osmux_disable_endpoint(struct mgcp_endpoint *endp)
483{
484 LOGP(DMGCP, LOGL_INFO, "Releasing endpoint %u using Osmux CID %u\n",
485 ENDPOINT_NUMBER(endp), endp->osmux.cid);
486 endp->osmux.state = OSMUX_STATE_DISABLED;
487 endp->osmux.cid = -1;
488 osmux_handle_put(endp->osmux.in);
489}
490
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100491/* We don't need to send the dummy load for osmux so often as another endpoint
492 * may have already punched the hole in the firewall. This approach is simple
493 * though.
494 */
495int osmux_send_dummy(struct mgcp_endpoint *endp)
496{
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200497 char buf[1 + sizeof(uint8_t)];
498 struct in_addr addr_unset = {};
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200499
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200500 buf[0] = MGCP_DUMMY_LOAD;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200501 memcpy(&buf[1], &endp->osmux.cid, sizeof(endp->osmux.cid));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100502
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200503 /* Wait until we have the connection information from MDCX */
504 if (memcmp(&endp->net_end.addr, &addr_unset, sizeof(addr_unset)) == 0)
505 return 0;
506
507 if (endp->osmux.state == OSMUX_STATE_ACTIVATING) {
508 if (osmux_enable_endpoint(endp, OSMUX_ROLE_BSC,
509 &endp->net_end.addr,
Pablo Neira Ayusof8927632014-08-28 16:51:17 +0200510 htons(endp->cfg->osmux_port)) < 0) {
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200511 LOGP(DMGCP, LOGL_ERROR,
512 "Could not activate osmux in endpoint %d\n",
513 ENDPOINT_NUMBER(endp));
514 }
515 LOGP(DMGCP, LOGL_ERROR,
516 "Osmux CID %u for %s:%u is now enabled\n",
517 endp->osmux.cid, inet_ntoa(endp->net_end.addr),
Pablo Neira Ayusof8927632014-08-28 16:51:17 +0200518 endp->cfg->osmux_port);
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200519 }
520 LOGP(DMGCP, LOGL_DEBUG,
521 "sending OSMUX dummy load to %s CID %u\n",
522 inet_ntoa(endp->net_end.addr), endp->osmux.cid);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100523
524 return mgcp_udp_send(osmux_fd.fd, &endp->net_end.addr,
Pablo Neira Ayusof8927632014-08-28 16:51:17 +0200525 htons(endp->cfg->osmux_port), buf, sizeof(buf));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100526}
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200527
528/* bsc-nat allocates/releases the Osmux circuit ID */
529static uint8_t osmux_cid_bitmap[16];
530
531int osmux_get_cid(void)
532{
533 int i, j;
534
535 for (i = 0; i < sizeof(osmux_cid_bitmap) / 8; i++) {
536 for (j = 0; j < 8; j++) {
537 if (osmux_cid_bitmap[i] & (1 << j))
538 continue;
539
540 osmux_cid_bitmap[i] |= (1 << j);
541 LOGP(DMGCP, LOGL_DEBUG,
542 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
543 return (i * 8) + j;
544 }
545 }
546
547 LOGP(DMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n");
548 return -1;
549}
550
551void osmux_put_cid(uint8_t osmux_cid)
552{
553 LOGP(DMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
554 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
555}