blob: 0fa7b45015354a1356073be18e87c902df0460be [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
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010027static struct osmo_fd osmux_fd;
28
29static LLIST_HEAD(osmux_handle_list);
30
31struct osmux_handle {
32 struct llist_head head;
33 struct osmux_in_handle *in;
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020034 struct in_addr rem_addr;
35 int rem_port;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +020036 int refcnt;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010037};
38
39static void *osmux;
40
41static void osmux_deliver(struct msgb *batch_msg, void *data)
42{
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020043 struct osmux_handle *handle = data;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010044 struct sockaddr_in out = {
45 .sin_family = AF_INET,
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020046 .sin_port = handle->rem_port,
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010047 };
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010048
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020049 memcpy(&out.sin_addr, &handle->rem_addr, sizeof(handle->rem_addr));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010050 sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0,
51 (struct sockaddr *)&out, sizeof(out));
Pablo Neira Ayuso4ef66b12014-08-28 20:13:59 +020052 msgb_free(batch_msg);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010053}
54
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020055static struct osmux_handle *
56osmux_handle_find_get(struct in_addr *addr, int rem_port)
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010057{
58 struct osmux_handle *h;
59
60 /* Lookup for existing OSMUX handle for this destination address. */
61 llist_for_each_entry(h, &osmux_handle_list, head) {
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020062 if (memcmp(&h->rem_addr, addr, sizeof(struct in_addr)) == 0 &&
63 h->rem_port == rem_port) {
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010064 LOGP(DMGCP, LOGL_DEBUG, "using existing OSMUX handle "
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +020065 "for addr=%s:%d\n",
66 inet_ntoa(*addr), ntohs(rem_port));
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +020067 h->refcnt++;
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020068 return h;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010069 }
70 }
71
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +020072 return NULL;
73}
74
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +020075static void osmux_handle_put(struct osmux_in_handle *in)
76{
77 struct osmux_handle *h;
78
79 /* Lookup for existing OSMUX handle for this destination address. */
80 llist_for_each_entry(h, &osmux_handle_list, head) {
81 if (h->in == in) {
82 if (--h->refcnt == 0) {
Pablo Neira Ayuso36a03bd2014-08-28 16:25:09 +020083 LOGP(DMGCP, LOGL_INFO,
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +020084 "Releasing unused osmux handle for %s:%d\n",
85 inet_ntoa(h->rem_addr),
86 ntohs(h->rem_port));
Pablo Neira Ayuso36a03bd2014-08-28 16:25:09 +020087 LOGP(DMGCP, LOGL_INFO, "Stats: "
88 "input RTP msgs: %u bytes: %"PRIu64" "
89 "output osmux msgs: %u bytes: %"PRIu64"\n",
90 in->stats.input_rtp_msgs,
91 in->stats.input_rtp_bytes,
92 in->stats.output_osmux_msgs,
93 in->stats.output_osmux_bytes);
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +020094 llist_del(&h->head);
Pablo Neira Ayuso72b187b2014-08-28 12:46:41 +020095 osmux_xfrm_input_fini(h->in);
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +020096 talloc_free(h);
97 }
98 return;
99 }
100 }
101 LOGP(DMGCP, LOGL_ERROR, "cannot find Osmux input handle %p\n", in);
102}
103
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +0200104static struct osmux_handle *
105osmux_handle_alloc(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
106{
107 struct osmux_handle *h;
108
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100109 h = talloc_zero(osmux, struct osmux_handle);
110 if (!h)
111 return NULL;
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200112 h->rem_addr = *addr;
113 h->rem_port = rem_port;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200114 h->refcnt++;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100115
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +0200116 h->in = talloc_zero(h, struct osmux_in_handle);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100117 if (!h->in) {
118 talloc_free(h);
119 return NULL;
120 }
121
122 h->in->osmux_seq = 0; /* sequence number to start OSmux message from */
123 h->in->batch_factor = cfg->osmux_batch;
Pablo Neira Ayuso03ab79a2014-08-29 12:30:38 +0200124 /* If batch size is zero, the library defaults to 1470 bytes. */
125 h->in->batch_size = cfg->osmux_batch_size;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100126 h->in->deliver = osmux_deliver;
127 osmux_xfrm_input_init(h->in);
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200128 h->in->data = h;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100129
130 llist_add(&h->head, &osmux_handle_list);
131
Holger Hans Peter Freytherea7ef382014-07-09 00:53:29 +0200132 LOGP(DMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s:%d\n",
133 inet_ntoa(*addr), ntohs(rem_port));
Pablo Neira Ayusofd1d9612014-08-26 18:45:46 +0200134
135 return h;
136}
137
138static struct osmux_in_handle *
139osmux_handle_lookup(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
140{
141 struct osmux_handle *h;
142
143 h = osmux_handle_find_get(addr, rem_port);
144 if (h != NULL)
145 return h->in;
146
147 h = osmux_handle_alloc(cfg, addr, rem_port);
148 if (h == NULL)
149 return NULL;
150
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100151 return h->in;
152}
153
154int osmux_xfrm_to_osmux(int type, char *buf, int rc, struct mgcp_endpoint *endp)
155{
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200156 int ret;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100157 struct msgb *msg;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100158
159 msg = msgb_alloc(4096, "RTP");
160 if (!msg)
161 return 0;
162
163 memcpy(msg->data, buf, rc);
164 msgb_put(msg, rc);
165
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;
Pablo Neira Ayusocaa0aac2014-08-28 19:50:01 +0200214 struct sockaddr_in addr = {
215 .sin_addr = endp->net_end.addr,
216 .sin_port = endp->net_end.rtp_port,
217 };
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100218
Pablo Neira Ayuso115e81e2014-09-04 17:25:16 +0200219 endp->bts_end.octets += msg->len;
220 endp->bts_end.packets++;
221
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100222 mgcp_send(endp, MGCP_DEST_NET, 1, &addr, (char *)msg->data, msg->len);
223 msgb_free(msg);
224}
225
226static void scheduled_tx_bts_cb(struct msgb *msg, void *data)
227{
228 struct mgcp_endpoint *endp = data;
Pablo Neira Ayusocaa0aac2014-08-28 19:50:01 +0200229 struct sockaddr_in addr = {
230 .sin_addr = endp->bts_end.addr,
231 .sin_port = endp->bts_end.rtp_port,
232 };
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100233
Pablo Neira Ayuso115e81e2014-09-04 17:25:16 +0200234 endp->net_end.octets += msg->len;
235 endp->net_end.packets++;
236
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100237 mgcp_send(endp, MGCP_DEST_BTS, 1, &addr, (char *)msg->data, msg->len);
238 msgb_free(msg);
239}
240
241static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr)
242{
243 struct msgb *msg;
244 socklen_t slen = sizeof(*addr);
245 int ret;
246
247 msg = msgb_alloc(4096, "OSMUX");
248 if (!msg) {
249 LOGP(DMGCP, LOGL_ERROR, "cannot allocate message\n");
250 return NULL;
251 }
252 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0,
253 (struct sockaddr *)addr, &slen);
254 if (ret <= 0) {
255 msgb_free(msg);
256 LOGP(DMGCP, LOGL_ERROR, "cannot receive message\n");
257 return NULL;
258 }
259 msgb_put(msg, ret);
260
261 return msg;
262}
263
264int osmux_read_from_bsc_nat_cb(struct osmo_fd *ofd, unsigned int what)
265{
266 struct msgb *msg;
267 struct osmux_hdr *osmuxh;
268 struct llist_head list;
269 struct sockaddr_in addr;
270 struct mgcp_config *cfg = ofd->data;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100271
272 msg = osmux_recv(ofd, &addr);
273 if (!msg)
274 return -1;
275
276 /* not any further processing dummy messages */
277 if (msg->data[0] == MGCP_DUMMY_LOAD)
278 goto out;
279
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100280 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
281 struct mgcp_endpoint *endp;
282
283 /* Yes, we use MGCP_DEST_NET to locate the origin */
284 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
285 &addr.sin_addr, MGCP_DEST_NET);
286 if (!endp) {
287 LOGP(DMGCP, LOGL_ERROR,
288 "Cannot find an endpoint for circuit_id=%d\n",
289 osmuxh->circuit_id);
290 goto out;
291 }
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200292 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100293 osmux_tx_sched(&list, scheduled_tx_bts_cb, endp);
294 }
295out:
296 msgb_free(msg);
297 return 0;
298}
299
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200300/* This is called from the bsc-nat */
301static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr,
302 struct msgb *msg)
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200303{
304 struct mgcp_endpoint *endp;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200305 uint8_t osmux_cid;
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200306
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200307 if (msg->len < 1 + sizeof(osmux_cid)) {
308 LOGP(DMGCP, LOGL_ERROR,
309 "Discarding truncated Osmux dummy load\n");
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200310 goto out;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200311 }
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200312
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200313 LOGP(DMGCP, LOGL_DEBUG, "Received Osmux dummy load from %s\n",
314 inet_ntoa(addr->sin_addr));
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200315
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200316 if (!cfg->osmux) {
317 LOGP(DMGCP, LOGL_ERROR,
318 "bsc wants to use Osmux but bsc-nat did not request it\n");
319 goto out;
320 }
321
322 /* extract the osmux CID from the dummy message */
323 memcpy(&osmux_cid, &msg->data[1], sizeof(osmux_cid));
324
325 endp = endpoint_lookup(cfg, osmux_cid, &addr->sin_addr, MGCP_DEST_BTS);
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200326 if (!endp) {
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200327 LOGP(DMGCP, LOGL_ERROR,
328 "Cannot find endpoint for Osmux CID %d\n", osmux_cid);
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200329 goto out;
330 }
331
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200332 if (endp->osmux.state == OSMUX_STATE_ENABLED)
333 goto out;
334
335 if (osmux_enable_endpoint(endp, OSMUX_ROLE_BSC_NAT,
336 &addr->sin_addr, addr->sin_port) < 0 ){
337 LOGP(DMGCP, LOGL_ERROR,
Pablo Neira Ayuso1c810452014-08-28 15:35:58 +0200338 "Could not enable osmux in endpoint %d\n",
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200339 ENDPOINT_NUMBER(endp));
Pablo Neira Ayuso1c810452014-08-28 15:35:58 +0200340 goto out;
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200341 }
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200342
343 LOGP(DMGCP, LOGL_INFO, "Enabling osmux in endpoint %d for %s:%u\n",
344 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr),
345 ntohs(addr->sin_port));
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200346out:
347 msgb_free(msg);
348 return 0;
349}
350
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100351int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what)
352{
353 struct msgb *msg;
354 struct osmux_hdr *osmuxh;
355 struct llist_head list;
356 struct sockaddr_in addr;
357 struct mgcp_config *cfg = ofd->data;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100358
359 msg = osmux_recv(ofd, &addr);
360 if (!msg)
361 return -1;
362
363 /* not any further processing dummy messages */
364 if (msg->data[0] == MGCP_DUMMY_LOAD)
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200365 return osmux_handle_dummy(cfg, &addr, msg);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100366
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100367 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
368 struct mgcp_endpoint *endp;
369
370 /* Yes, we use MGCP_DEST_BTS to locate the origin */
371 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
372 &addr.sin_addr, MGCP_DEST_BTS);
373 if (!endp) {
374 LOGP(DMGCP, LOGL_ERROR,
375 "Cannot find an endpoint for circuit_id=%d\n",
376 osmuxh->circuit_id);
377 goto out;
378 }
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200379 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100380 osmux_tx_sched(&list, scheduled_tx_net_cb, endp);
381 }
382out:
383 msgb_free(msg);
384 return 0;
385}
386
387int osmux_init(int role, struct mgcp_config *cfg)
388{
389 int ret;
390
391 switch(role) {
392 case OSMUX_ROLE_BSC:
393 osmux_fd.cb = osmux_read_from_bsc_nat_cb;
394 break;
395 case OSMUX_ROLE_BSC_NAT:
396 osmux_fd.cb = osmux_read_from_bsc_cb;
397 break;
398 default:
399 LOGP(DMGCP, LOGL_ERROR, "wrong role for OSMUX\n");
400 return -1;
401 }
402 osmux_fd.data = cfg;
403
Pablo Neira Ayuso0fe78d32014-08-28 16:43:38 +0200404 ret = mgcp_create_bind("0.0.0.0", &osmux_fd, cfg->osmux_port);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100405 if (ret < 0) {
406 LOGP(DMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n");
407 return ret;
408 }
409 osmux_fd.when |= BSC_FD_READ;
410
411 ret = osmo_fd_register(&osmux_fd);
412 if (ret < 0) {
413 LOGP(DMGCP, LOGL_ERROR, "cannot register OSMUX socket\n");
414 return ret;
415 }
416 cfg->osmux_init = 1;
417
418 return 0;
419}
420
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200421int osmux_enable_endpoint(struct mgcp_endpoint *endp, int role,
422 struct in_addr *addr, uint16_t port)
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100423{
424 /* If osmux is enabled, initialize the output handler. This handler is
425 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200426 * allocated based on the circuit ID (endp->osmux.cid), which is unique
427 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
428 * SSRC space (2^32) by the 256 possible circuit IDs, then randomly
429 * select one value from that window. Thus, we have no chance to have
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100430 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
431 * similarly, for flows traveling to the MSC.
432 */
433 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256;
434
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200435 if (endp->osmux.state == OSMUX_STATE_DISABLED) {
436 LOGP(DMGCP, LOGL_ERROR, "Endpoint %u didn't request Osmux\n",
437 ENDPOINT_NUMBER(endp));
438 return -1;
439 }
440
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200441 osmux_xfrm_output_init(&endp->osmux.out,
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200442 (endp->osmux.cid * rtp_ssrc_winlen) +
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100443 (random() % rtp_ssrc_winlen));
444
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200445 endp->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
446 if (!endp->osmux.in) {
447 LOGP(DMGCP, LOGL_ERROR, "Cannot allocate input osmux handle\n");
448 return -1;
449 }
450
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100451 switch (endp->cfg->role) {
452 case MGCP_BSC_NAT:
453 endp->type = MGCP_OSMUX_BSC_NAT;
454 break;
455 case MGCP_BSC:
456 endp->type = MGCP_OSMUX_BSC;
457 break;
458 }
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200459 endp->osmux.state = OSMUX_STATE_ENABLED;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100460
461 return 0;
462}
463
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200464void osmux_disable_endpoint(struct mgcp_endpoint *endp)
465{
466 LOGP(DMGCP, LOGL_INFO, "Releasing endpoint %u using Osmux CID %u\n",
467 ENDPOINT_NUMBER(endp), endp->osmux.cid);
468 endp->osmux.state = OSMUX_STATE_DISABLED;
469 endp->osmux.cid = -1;
470 osmux_handle_put(endp->osmux.in);
471}
472
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100473/* We don't need to send the dummy load for osmux so often as another endpoint
474 * may have already punched the hole in the firewall. This approach is simple
475 * though.
476 */
477int osmux_send_dummy(struct mgcp_endpoint *endp)
478{
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200479 char buf[1 + sizeof(uint8_t)];
480 struct in_addr addr_unset = {};
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200481
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200482 buf[0] = MGCP_DUMMY_LOAD;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200483 memcpy(&buf[1], &endp->osmux.cid, sizeof(endp->osmux.cid));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100484
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200485 /* Wait until we have the connection information from MDCX */
486 if (memcmp(&endp->net_end.addr, &addr_unset, sizeof(addr_unset)) == 0)
487 return 0;
488
489 if (endp->osmux.state == OSMUX_STATE_ACTIVATING) {
490 if (osmux_enable_endpoint(endp, OSMUX_ROLE_BSC,
491 &endp->net_end.addr,
Pablo Neira Ayusof8927632014-08-28 16:51:17 +0200492 htons(endp->cfg->osmux_port)) < 0) {
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200493 LOGP(DMGCP, LOGL_ERROR,
494 "Could not activate osmux in endpoint %d\n",
495 ENDPOINT_NUMBER(endp));
496 }
497 LOGP(DMGCP, LOGL_ERROR,
498 "Osmux CID %u for %s:%u is now enabled\n",
499 endp->osmux.cid, inet_ntoa(endp->net_end.addr),
Pablo Neira Ayusof8927632014-08-28 16:51:17 +0200500 endp->cfg->osmux_port);
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200501 }
502 LOGP(DMGCP, LOGL_DEBUG,
503 "sending OSMUX dummy load to %s CID %u\n",
504 inet_ntoa(endp->net_end.addr), endp->osmux.cid);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100505
506 return mgcp_udp_send(osmux_fd.fd, &endp->net_end.addr,
Pablo Neira Ayusof8927632014-08-28 16:51:17 +0200507 htons(endp->cfg->osmux_port), buf, sizeof(buf));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100508}
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200509
510/* bsc-nat allocates/releases the Osmux circuit ID */
511static uint8_t osmux_cid_bitmap[16];
512
513int osmux_get_cid(void)
514{
515 int i, j;
516
517 for (i = 0; i < sizeof(osmux_cid_bitmap) / 8; i++) {
518 for (j = 0; j < 8; j++) {
519 if (osmux_cid_bitmap[i] & (1 << j))
520 continue;
521
522 osmux_cid_bitmap[i] |= (1 << j);
523 LOGP(DMGCP, LOGL_DEBUG,
524 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
525 return (i * 8) + j;
526 }
527 }
528
529 LOGP(DMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n");
530 return -1;
531}
532
533void osmux_put_cid(uint8_t osmux_cid)
534{
535 LOGP(DMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
536 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
537}