blob: b554b35b0042866d2d8a9572d83396c327b431a9 [file] [log] [blame]
Pablo Neira Ayuso4a2f6592014-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 Ayuso2e719f82014-08-28 16:25:09 +020015#include <inttypes.h> /* for PRIu64 */
Pablo Neira Ayuso4a2f6592014-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 Freytherc220c0b2014-07-09 00:53:29 +020036 struct in_addr rem_addr;
37 int rem_port;
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +020038 int refcnt;
Pablo Neira Ayuso4a2f6592014-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 Freytherc220c0b2014-07-09 00:53:29 +020045 struct osmux_handle *handle = data;
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +010046 struct sockaddr_in out = {
47 .sin_family = AF_INET,
Holger Hans Peter Freytherc220c0b2014-07-09 00:53:29 +020048 .sin_port = handle->rem_port,
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +010049 };
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +010050
Holger Hans Peter Freytherc220c0b2014-07-09 00:53:29 +020051 memcpy(&out.sin_addr, &handle->rem_addr, sizeof(handle->rem_addr));
Pablo Neira Ayuso4a2f6592014-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 Ayusoe8f47602014-08-26 18:45:46 +020056static struct osmux_handle *
57osmux_handle_find_get(struct in_addr *addr, int rem_port)
Pablo Neira Ayuso4a2f6592014-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 Ayusoe8f47602014-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 Ayuso4a2f6592014-02-05 18:56:17 +010065 LOGP(DMGCP, LOGL_DEBUG, "using existing OSMUX handle "
Holger Hans Peter Freytherc220c0b2014-07-09 00:53:29 +020066 "for addr=%s:%d\n",
67 inet_ntoa(*addr), ntohs(rem_port));
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +020068 h->refcnt++;
Pablo Neira Ayusoe8f47602014-08-26 18:45:46 +020069 return h;
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +010070 }
71 }
72
Pablo Neira Ayusoe8f47602014-08-26 18:45:46 +020073 return NULL;
74}
75
Pablo Neira Ayusoa8c68ad2014-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 Ayuso2e719f82014-08-28 16:25:09 +020084 LOGP(DMGCP, LOGL_INFO,
Pablo Neira Ayusoa8c68ad2014-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 Ayuso2e719f82014-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 Ayusoa8c68ad2014-08-27 17:02:52 +020095 llist_del(&h->head);
Pablo Neira Ayusoc41e77f2014-08-28 12:46:41 +020096 osmux_xfrm_input_fini(h->in);
Pablo Neira Ayusoa8c68ad2014-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 Ayusoe8f47602014-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 Ayuso4a2f6592014-02-05 18:56:17 +0100110 h = talloc_zero(osmux, struct osmux_handle);
111 if (!h)
112 return NULL;
Holger Hans Peter Freytherc220c0b2014-07-09 00:53:29 +0200113 h->rem_addr = *addr;
114 h->rem_port = rem_port;
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200115 h->refcnt++;
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100116
Pablo Neira Ayusoe8f47602014-08-26 18:45:46 +0200117 h->in = talloc_zero(h, struct osmux_in_handle);
Pablo Neira Ayuso4a2f6592014-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 Freytherc220c0b2014-07-09 00:53:29 +0200127 h->in->data = h;
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100128
129 llist_add(&h->head, &osmux_handle_list);
130
Holger Hans Peter Freytherc220c0b2014-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 Ayusoe8f47602014-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 Ayuso4a2f6592014-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 Ayusoa8c68ad2014-08-27 17:02:52 +0200155 int ret;
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100156 struct msgb *msg;
Pablo Neira Ayuso4a2f6592014-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 Ayusoa8c68ad2014-08-27 17:02:52 +0200165 while ((ret = osmux_xfrm_input(endp->osmux.in, msg, endp->osmux.cid)) > 0) {
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100166 /* batch full, build and deliver it */
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200167 osmux_xfrm_input_deliver(endp->osmux.in);
Pablo Neira Ayuso4a2f6592014-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 Ayusoa8c68ad2014-08-27 17:02:52 +0200201 if (tmp->osmux.cid == cid && this->s_addr == from_addr->s_addr)
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100202 return tmp;
203 }
204
Holger Hans Peter Freyther9350b302014-07-08 09:08:34 +0200205 LOGP(DMGCP, LOGL_ERROR, "Cannot find endpoint with cid=%d\n", cid);
Pablo Neira Ayuso4a2f6592014-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;
Pablo Neira Ayuso7ccbef12014-08-28 19:50:01 +0200213 struct sockaddr_in addr = {
214 .sin_addr = endp->net_end.addr,
215 .sin_port = endp->net_end.rtp_port,
216 };
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100217
218 mgcp_send(endp, MGCP_DEST_NET, 1, &addr, (char *)msg->data, msg->len);
219 msgb_free(msg);
220}
221
222static void scheduled_tx_bts_cb(struct msgb *msg, void *data)
223{
224 struct mgcp_endpoint *endp = data;
Pablo Neira Ayuso7ccbef12014-08-28 19:50:01 +0200225 struct sockaddr_in addr = {
226 .sin_addr = endp->bts_end.addr,
227 .sin_port = endp->bts_end.rtp_port,
228 };
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100229
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;
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100264
265 msg = osmux_recv(ofd, &addr);
266 if (!msg)
267 return -1;
268
269 /* not any further processing dummy messages */
270 if (msg->data[0] == MGCP_DUMMY_LOAD)
271 goto out;
272
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100273 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 }
Pablo Neira Ayuso8dab7482014-08-26 13:31:53 +0200285 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100286 osmux_tx_sched(&list, scheduled_tx_bts_cb, endp);
287 }
288out:
289 msgb_free(msg);
290 return 0;
291}
292
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200293/* This is called from the bsc-nat */
294static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr,
295 struct msgb *msg)
Holger Hans Peter Freytherfe0b5792014-07-09 00:38:06 +0200296{
297 struct mgcp_endpoint *endp;
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200298 uint8_t osmux_cid;
Holger Hans Peter Freytherfe0b5792014-07-09 00:38:06 +0200299
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200300 if (msg->len < 1 + sizeof(osmux_cid)) {
301 LOGP(DMGCP, LOGL_ERROR,
302 "Discarding truncated Osmux dummy load\n");
Holger Hans Peter Freytherfe0b5792014-07-09 00:38:06 +0200303 goto out;
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200304 }
Holger Hans Peter Freytherfe0b5792014-07-09 00:38:06 +0200305
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200306 LOGP(DMGCP, LOGL_DEBUG, "Received Osmux dummy load from %s\n",
307 inet_ntoa(addr->sin_addr));
Holger Hans Peter Freytherfe0b5792014-07-09 00:38:06 +0200308
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200309 if (!cfg->osmux) {
310 LOGP(DMGCP, LOGL_ERROR,
311 "bsc wants to use Osmux but bsc-nat did not request it\n");
312 goto out;
313 }
314
315 /* extract the osmux CID from the dummy message */
316 memcpy(&osmux_cid, &msg->data[1], sizeof(osmux_cid));
317
318 endp = endpoint_lookup(cfg, osmux_cid, &addr->sin_addr, MGCP_DEST_BTS);
Holger Hans Peter Freytherfe0b5792014-07-09 00:38:06 +0200319 if (!endp) {
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200320 LOGP(DMGCP, LOGL_ERROR,
321 "Cannot find endpoint for Osmux CID %d\n", osmux_cid);
Holger Hans Peter Freytherfe0b5792014-07-09 00:38:06 +0200322 goto out;
323 }
324
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200325 if (endp->osmux.state == OSMUX_STATE_ENABLED)
326 goto out;
327
328 if (osmux_enable_endpoint(endp, OSMUX_ROLE_BSC_NAT,
329 &addr->sin_addr, addr->sin_port) < 0 ){
330 LOGP(DMGCP, LOGL_ERROR,
Pablo Neira Ayusoba748b82014-08-28 15:35:58 +0200331 "Could not enable osmux in endpoint %d\n",
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200332 ENDPOINT_NUMBER(endp));
Pablo Neira Ayusoba748b82014-08-28 15:35:58 +0200333 goto out;
Holger Hans Peter Freytherfe0b5792014-07-09 00:38:06 +0200334 }
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200335
336 LOGP(DMGCP, LOGL_INFO, "Enabling osmux in endpoint %d for %s:%u\n",
337 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr),
338 ntohs(addr->sin_port));
Holger Hans Peter Freytherfe0b5792014-07-09 00:38:06 +0200339out:
340 msgb_free(msg);
341 return 0;
342}
343
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100344int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what)
345{
346 struct msgb *msg;
347 struct osmux_hdr *osmuxh;
348 struct llist_head list;
349 struct sockaddr_in addr;
350 struct mgcp_config *cfg = ofd->data;
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100351
352 msg = osmux_recv(ofd, &addr);
353 if (!msg)
354 return -1;
355
356 /* not any further processing dummy messages */
357 if (msg->data[0] == MGCP_DUMMY_LOAD)
Holger Hans Peter Freytherfe0b5792014-07-09 00:38:06 +0200358 return osmux_handle_dummy(cfg, &addr, msg);
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100359
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100360 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
361 struct mgcp_endpoint *endp;
362
363 /* Yes, we use MGCP_DEST_BTS to locate the origin */
364 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
365 &addr.sin_addr, MGCP_DEST_BTS);
366 if (!endp) {
367 LOGP(DMGCP, LOGL_ERROR,
368 "Cannot find an endpoint for circuit_id=%d\n",
369 osmuxh->circuit_id);
370 goto out;
371 }
Pablo Neira Ayuso8dab7482014-08-26 13:31:53 +0200372 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100373 osmux_tx_sched(&list, scheduled_tx_net_cb, endp);
374 }
375out:
376 msgb_free(msg);
377 return 0;
378}
379
380int osmux_init(int role, struct mgcp_config *cfg)
381{
382 int ret;
383
384 switch(role) {
385 case OSMUX_ROLE_BSC:
386 osmux_fd.cb = osmux_read_from_bsc_nat_cb;
387 break;
388 case OSMUX_ROLE_BSC_NAT:
389 osmux_fd.cb = osmux_read_from_bsc_cb;
390 break;
391 default:
392 LOGP(DMGCP, LOGL_ERROR, "wrong role for OSMUX\n");
393 return -1;
394 }
395 osmux_fd.data = cfg;
396
Pablo Neira Ayuso1b135d42014-08-28 16:43:38 +0200397 if (!cfg->osmux_port)
398 cfg->osmux_port = OSMUX_PORT;
399
400 ret = mgcp_create_bind("0.0.0.0", &osmux_fd, cfg->osmux_port);
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100401 if (ret < 0) {
402 LOGP(DMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n");
403 return ret;
404 }
405 osmux_fd.when |= BSC_FD_READ;
406
407 ret = osmo_fd_register(&osmux_fd);
408 if (ret < 0) {
409 LOGP(DMGCP, LOGL_ERROR, "cannot register OSMUX socket\n");
410 return ret;
411 }
412 cfg->osmux_init = 1;
413
414 return 0;
415}
416
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200417int osmux_enable_endpoint(struct mgcp_endpoint *endp, int role,
418 struct in_addr *addr, uint16_t port)
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100419{
420 /* If osmux is enabled, initialize the output handler. This handler is
421 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200422 * allocated based on the circuit ID (endp->osmux.cid), which is unique
423 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
424 * SSRC space (2^32) by the 256 possible circuit IDs, then randomly
425 * select one value from that window. Thus, we have no chance to have
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100426 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
427 * similarly, for flows traveling to the MSC.
428 */
429 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256;
430
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200431 if (endp->osmux.state == OSMUX_STATE_DISABLED) {
432 LOGP(DMGCP, LOGL_ERROR, "Endpoint %u didn't request Osmux\n",
433 ENDPOINT_NUMBER(endp));
434 return -1;
435 }
436
Pablo Neira Ayuso8dab7482014-08-26 13:31:53 +0200437 osmux_xfrm_output_init(&endp->osmux.out,
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200438 (endp->osmux.cid * rtp_ssrc_winlen) +
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100439 (random() % rtp_ssrc_winlen));
440
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200441 endp->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
442 if (!endp->osmux.in) {
443 LOGP(DMGCP, LOGL_ERROR, "Cannot allocate input osmux handle\n");
444 return -1;
445 }
446
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100447 switch (endp->cfg->role) {
448 case MGCP_BSC_NAT:
449 endp->type = MGCP_OSMUX_BSC_NAT;
450 break;
451 case MGCP_BSC:
452 endp->type = MGCP_OSMUX_BSC;
453 break;
454 }
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200455 endp->osmux.state = OSMUX_STATE_ENABLED;
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100456
457 return 0;
458}
459
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200460void osmux_disable_endpoint(struct mgcp_endpoint *endp)
461{
462 LOGP(DMGCP, LOGL_INFO, "Releasing endpoint %u using Osmux CID %u\n",
463 ENDPOINT_NUMBER(endp), endp->osmux.cid);
464 endp->osmux.state = OSMUX_STATE_DISABLED;
465 endp->osmux.cid = -1;
466 osmux_handle_put(endp->osmux.in);
467}
468
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100469/* We don't need to send the dummy load for osmux so often as another endpoint
470 * may have already punched the hole in the firewall. This approach is simple
471 * though.
472 */
473int osmux_send_dummy(struct mgcp_endpoint *endp)
474{
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200475 char buf[1 + sizeof(uint8_t)];
476 struct in_addr addr_unset = {};
Holger Hans Peter Freythercaf3dd02014-07-09 00:32:00 +0200477
Holger Hans Peter Freythercaf3dd02014-07-09 00:32:00 +0200478 buf[0] = MGCP_DUMMY_LOAD;
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200479 memcpy(&buf[1], &endp->osmux.cid, sizeof(endp->osmux.cid));
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100480
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200481 /* Wait until we have the connection information from MDCX */
482 if (memcmp(&endp->net_end.addr, &addr_unset, sizeof(addr_unset)) == 0)
483 return 0;
484
485 if (endp->osmux.state == OSMUX_STATE_ACTIVATING) {
486 if (osmux_enable_endpoint(endp, OSMUX_ROLE_BSC,
487 &endp->net_end.addr,
Pablo Neira Ayuso7e821732014-08-28 16:51:17 +0200488 htons(endp->cfg->osmux_port)) < 0) {
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200489 LOGP(DMGCP, LOGL_ERROR,
490 "Could not activate osmux in endpoint %d\n",
491 ENDPOINT_NUMBER(endp));
492 }
493 LOGP(DMGCP, LOGL_ERROR,
494 "Osmux CID %u for %s:%u is now enabled\n",
495 endp->osmux.cid, inet_ntoa(endp->net_end.addr),
Pablo Neira Ayuso7e821732014-08-28 16:51:17 +0200496 endp->cfg->osmux_port);
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200497 }
498 LOGP(DMGCP, LOGL_DEBUG,
499 "sending OSMUX dummy load to %s CID %u\n",
500 inet_ntoa(endp->net_end.addr), endp->osmux.cid);
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100501
502 return mgcp_udp_send(osmux_fd.fd, &endp->net_end.addr,
Pablo Neira Ayuso7e821732014-08-28 16:51:17 +0200503 htons(endp->cfg->osmux_port), buf, sizeof(buf));
Pablo Neira Ayuso4a2f6592014-02-05 18:56:17 +0100504}
Pablo Neira Ayusoa8c68ad2014-08-27 17:02:52 +0200505
506/* bsc-nat allocates/releases the Osmux circuit ID */
507static uint8_t osmux_cid_bitmap[16];
508
509int osmux_get_cid(void)
510{
511 int i, j;
512
513 for (i = 0; i < sizeof(osmux_cid_bitmap) / 8; i++) {
514 for (j = 0; j < 8; j++) {
515 if (osmux_cid_bitmap[i] & (1 << j))
516 continue;
517
518 osmux_cid_bitmap[i] |= (1 << j);
519 LOGP(DMGCP, LOGL_DEBUG,
520 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
521 return (i * 8) + j;
522 }
523 }
524
525 LOGP(DMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n");
526 return -1;
527}
528
529void osmux_put_cid(uint8_t osmux_cid)
530{
531 LOGP(DMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
532 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
533}