blob: b46a80e7f81eecf26acad18b80d79e1861929484 [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
Pablo Neira Ayusoee11bc02014-09-17 12:33:09 +0200264#define osmux_chunk_length(msg, rem) (rem - msg->len);
265
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100266int osmux_read_from_bsc_nat_cb(struct osmo_fd *ofd, unsigned int what)
267{
268 struct msgb *msg;
269 struct osmux_hdr *osmuxh;
270 struct llist_head list;
271 struct sockaddr_in addr;
272 struct mgcp_config *cfg = ofd->data;
Pablo Neira Ayusoee11bc02014-09-17 12:33:09 +0200273 uint32_t rem;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100274
275 msg = osmux_recv(ofd, &addr);
276 if (!msg)
277 return -1;
278
279 /* not any further processing dummy messages */
280 if (msg->data[0] == MGCP_DUMMY_LOAD)
281 goto out;
282
Pablo Neira Ayusoee11bc02014-09-17 12:33:09 +0200283 rem = msg->len;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100284 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
285 struct mgcp_endpoint *endp;
286
287 /* Yes, we use MGCP_DEST_NET to locate the origin */
288 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
289 &addr.sin_addr, MGCP_DEST_NET);
290 if (!endp) {
291 LOGP(DMGCP, LOGL_ERROR,
292 "Cannot find an endpoint for circuit_id=%d\n",
293 osmuxh->circuit_id);
294 goto out;
295 }
Pablo Neira Ayusoee11bc02014-09-17 12:33:09 +0200296 endp->osmux.stats.octets += osmux_chunk_length(msg, rem);
297 endp->osmux.stats.chunks++;
298 rem = msg->len;
299
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200300 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100301 osmux_tx_sched(&list, scheduled_tx_bts_cb, endp);
302 }
303out:
304 msgb_free(msg);
305 return 0;
306}
307
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200308/* This is called from the bsc-nat */
309static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr,
310 struct msgb *msg)
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200311{
312 struct mgcp_endpoint *endp;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200313 uint8_t osmux_cid;
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200314
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200315 if (msg->len < 1 + sizeof(osmux_cid)) {
316 LOGP(DMGCP, LOGL_ERROR,
317 "Discarding truncated Osmux dummy load\n");
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200318 goto out;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200319 }
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200320
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200321 LOGP(DMGCP, LOGL_DEBUG, "Received Osmux dummy load from %s\n",
322 inet_ntoa(addr->sin_addr));
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200323
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200324 if (!cfg->osmux) {
325 LOGP(DMGCP, LOGL_ERROR,
326 "bsc wants to use Osmux but bsc-nat did not request it\n");
327 goto out;
328 }
329
330 /* extract the osmux CID from the dummy message */
331 memcpy(&osmux_cid, &msg->data[1], sizeof(osmux_cid));
332
333 endp = endpoint_lookup(cfg, osmux_cid, &addr->sin_addr, MGCP_DEST_BTS);
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200334 if (!endp) {
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200335 LOGP(DMGCP, LOGL_ERROR,
336 "Cannot find endpoint for Osmux CID %d\n", osmux_cid);
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200337 goto out;
338 }
339
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200340 if (endp->osmux.state == OSMUX_STATE_ENABLED)
341 goto out;
342
343 if (osmux_enable_endpoint(endp, OSMUX_ROLE_BSC_NAT,
344 &addr->sin_addr, addr->sin_port) < 0 ){
345 LOGP(DMGCP, LOGL_ERROR,
Pablo Neira Ayuso1c810452014-08-28 15:35:58 +0200346 "Could not enable osmux in endpoint %d\n",
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200347 ENDPOINT_NUMBER(endp));
Pablo Neira Ayuso1c810452014-08-28 15:35:58 +0200348 goto out;
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200349 }
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200350
351 LOGP(DMGCP, LOGL_INFO, "Enabling osmux in endpoint %d for %s:%u\n",
352 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr),
353 ntohs(addr->sin_port));
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200354out:
355 msgb_free(msg);
356 return 0;
357}
358
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100359int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what)
360{
361 struct msgb *msg;
362 struct osmux_hdr *osmuxh;
363 struct llist_head list;
364 struct sockaddr_in addr;
365 struct mgcp_config *cfg = ofd->data;
Pablo Neira Ayusoee11bc02014-09-17 12:33:09 +0200366 uint32_t rem;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100367
368 msg = osmux_recv(ofd, &addr);
369 if (!msg)
370 return -1;
371
372 /* not any further processing dummy messages */
373 if (msg->data[0] == MGCP_DUMMY_LOAD)
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200374 return osmux_handle_dummy(cfg, &addr, msg);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100375
Pablo Neira Ayusoee11bc02014-09-17 12:33:09 +0200376 rem = msg->len;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100377 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 }
Pablo Neira Ayusoee11bc02014-09-17 12:33:09 +0200389 endp->osmux.stats.octets += osmux_chunk_length(msg, rem);
390 endp->osmux.stats.chunks++;
391 rem = msg->len;
392
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200393 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100394 osmux_tx_sched(&list, scheduled_tx_net_cb, endp);
395 }
396out:
397 msgb_free(msg);
398 return 0;
399}
400
401int osmux_init(int role, struct mgcp_config *cfg)
402{
403 int ret;
404
405 switch(role) {
406 case OSMUX_ROLE_BSC:
407 osmux_fd.cb = osmux_read_from_bsc_nat_cb;
408 break;
409 case OSMUX_ROLE_BSC_NAT:
410 osmux_fd.cb = osmux_read_from_bsc_cb;
411 break;
412 default:
413 LOGP(DMGCP, LOGL_ERROR, "wrong role for OSMUX\n");
414 return -1;
415 }
416 osmux_fd.data = cfg;
417
Holger Hans Peter Freythera777c9e2015-10-12 20:06:16 +0200418 ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100419 if (ret < 0) {
420 LOGP(DMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n");
421 return ret;
422 }
Holger Hans Peter Freyther9be675e2015-01-21 11:39:47 +0100423 mgcp_set_ip_tos(osmux_fd.fd, cfg->endp_dscp);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100424 osmux_fd.when |= BSC_FD_READ;
425
426 ret = osmo_fd_register(&osmux_fd);
427 if (ret < 0) {
428 LOGP(DMGCP, LOGL_ERROR, "cannot register OSMUX socket\n");
429 return ret;
430 }
431 cfg->osmux_init = 1;
432
433 return 0;
434}
435
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200436int osmux_enable_endpoint(struct mgcp_endpoint *endp, int role,
437 struct in_addr *addr, uint16_t port)
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100438{
439 /* If osmux is enabled, initialize the output handler. This handler is
440 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200441 * allocated based on the circuit ID (endp->osmux.cid), which is unique
442 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
443 * SSRC space (2^32) by the 256 possible circuit IDs, then randomly
444 * select one value from that window. Thus, we have no chance to have
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100445 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
446 * similarly, for flows traveling to the MSC.
447 */
448 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256;
449
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200450 if (endp->osmux.state == OSMUX_STATE_DISABLED) {
451 LOGP(DMGCP, LOGL_ERROR, "Endpoint %u didn't request Osmux\n",
452 ENDPOINT_NUMBER(endp));
453 return -1;
454 }
455
Pablo Neira Ayuso63650bb2014-08-26 13:31:53 +0200456 osmux_xfrm_output_init(&endp->osmux.out,
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200457 (endp->osmux.cid * rtp_ssrc_winlen) +
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100458 (random() % rtp_ssrc_winlen));
459
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200460 endp->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
461 if (!endp->osmux.in) {
462 LOGP(DMGCP, LOGL_ERROR, "Cannot allocate input osmux handle\n");
463 return -1;
464 }
Pablo Neira Ayuso57e95a22015-07-17 21:56:23 +0200465 if (!osmux_xfrm_input_open_circuit(endp->osmux.in, endp->osmux.cid,
466 endp->cfg->osmux_dummy)) {
467 LOGP(DMGCP, LOGL_ERROR, "Cannot open osmux circuit %u\n",
468 endp->osmux.cid);
469 return -1;
470 }
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200471
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100472 switch (endp->cfg->role) {
473 case MGCP_BSC_NAT:
474 endp->type = MGCP_OSMUX_BSC_NAT;
475 break;
476 case MGCP_BSC:
477 endp->type = MGCP_OSMUX_BSC;
478 break;
479 }
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200480 endp->osmux.state = OSMUX_STATE_ENABLED;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100481
482 return 0;
483}
484
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200485void osmux_disable_endpoint(struct mgcp_endpoint *endp)
486{
487 LOGP(DMGCP, LOGL_INFO, "Releasing endpoint %u using Osmux CID %u\n",
488 ENDPOINT_NUMBER(endp), endp->osmux.cid);
Pablo Neira Ayuso57e95a22015-07-17 21:56:23 +0200489 osmux_xfrm_input_close_circuit(endp->osmux.in, endp->osmux.cid);
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200490 endp->osmux.state = OSMUX_STATE_DISABLED;
491 endp->osmux.cid = -1;
492 osmux_handle_put(endp->osmux.in);
493}
494
Holger Hans Peter Freyther1afe7c72015-10-04 11:11:11 +0200495void osmux_release_cid(struct mgcp_endpoint *endp)
496{
497 if (endp->osmux.allocated_cid >= 0)
498 osmux_put_cid(endp->osmux.allocated_cid);
499 endp->osmux.allocated_cid = -1;
500}
501
502void osmux_allocate_cid(struct mgcp_endpoint *endp)
503{
504 osmux_release_cid(endp);
505 endp->osmux.allocated_cid = osmux_get_cid();
506}
507
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100508/* We don't need to send the dummy load for osmux so often as another endpoint
509 * may have already punched the hole in the firewall. This approach is simple
510 * though.
511 */
512int osmux_send_dummy(struct mgcp_endpoint *endp)
513{
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200514 char buf[1 + sizeof(uint8_t)];
515 struct in_addr addr_unset = {};
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200516
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200517 buf[0] = MGCP_DUMMY_LOAD;
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200518 memcpy(&buf[1], &endp->osmux.cid, sizeof(endp->osmux.cid));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100519
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200520 /* Wait until we have the connection information from MDCX */
521 if (memcmp(&endp->net_end.addr, &addr_unset, sizeof(addr_unset)) == 0)
522 return 0;
523
524 if (endp->osmux.state == OSMUX_STATE_ACTIVATING) {
525 if (osmux_enable_endpoint(endp, OSMUX_ROLE_BSC,
526 &endp->net_end.addr,
Pablo Neira Ayusof8927632014-08-28 16:51:17 +0200527 htons(endp->cfg->osmux_port)) < 0) {
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200528 LOGP(DMGCP, LOGL_ERROR,
529 "Could not activate osmux in endpoint %d\n",
530 ENDPOINT_NUMBER(endp));
531 }
532 LOGP(DMGCP, LOGL_ERROR,
533 "Osmux CID %u for %s:%u is now enabled\n",
534 endp->osmux.cid, inet_ntoa(endp->net_end.addr),
Pablo Neira Ayusof8927632014-08-28 16:51:17 +0200535 endp->cfg->osmux_port);
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200536 }
537 LOGP(DMGCP, LOGL_DEBUG,
538 "sending OSMUX dummy load to %s CID %u\n",
539 inet_ntoa(endp->net_end.addr), endp->osmux.cid);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100540
541 return mgcp_udp_send(osmux_fd.fd, &endp->net_end.addr,
Pablo Neira Ayusof8927632014-08-28 16:51:17 +0200542 htons(endp->cfg->osmux_port), buf, sizeof(buf));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100543}
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200544
545/* bsc-nat allocates/releases the Osmux circuit ID */
Pablo Neira Ayuso11cb27e2015-10-15 07:57:29 +0200546static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1) / 8];
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200547
Holger Hans Peter Freyther641d3872015-10-02 15:56:35 +0200548int osmux_used_cid(void)
549{
550 int i, j, used = 0;
551
Holger Hans Peter Freytherb45e4d82015-10-02 16:11:15 +0200552 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
Holger Hans Peter Freyther641d3872015-10-02 15:56:35 +0200553 for (j = 0; j < 8; j++) {
554 if (osmux_cid_bitmap[i] & (1 << j))
555 used += 1;
556 }
557 }
558
559 return used;
560}
561
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200562int osmux_get_cid(void)
563{
564 int i, j;
565
Holger Hans Peter Freytherb45e4d82015-10-02 16:11:15 +0200566 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
Pablo Neira Ayusob769f3c2014-08-27 17:02:52 +0200567 for (j = 0; j < 8; j++) {
568 if (osmux_cid_bitmap[i] & (1 << j))
569 continue;
570
571 osmux_cid_bitmap[i] |= (1 << j);
572 LOGP(DMGCP, LOGL_DEBUG,
573 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
574 return (i * 8) + j;
575 }
576 }
577
578 LOGP(DMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n");
579 return -1;
580}
581
582void osmux_put_cid(uint8_t osmux_cid)
583{
584 LOGP(DMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
585 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
586}