blob: d0141f31623ed3ad20cefe81fb9db1ebeb890a38 [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001/*
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 */
15#include <inttypes.h> /* for PRIu64 */
16#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
Philipp Maier87bd9be2017-08-22 16:35:41 +020023#include <osmocom/mgcp/mgcp.h>
24#include <osmocom/mgcp/mgcp_internal.h>
25#include <osmocom/mgcp/osmux.h>
26#include <osmocom/mgcp/mgcp_conn.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020027
28static struct osmo_fd osmux_fd;
29
30static LLIST_HEAD(osmux_handle_list);
31
32struct osmux_handle {
33 struct llist_head head;
34 struct osmux_in_handle *in;
35 struct in_addr rem_addr;
36 int rem_port;
37 int refcnt;
38};
39
40static void *osmux;
41
Philipp Maier87bd9be2017-08-22 16:35:41 +020042/* Deliver OSMUX batch to the remote end */
43static void osmux_deliver_cb(struct msgb *batch_msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020044{
45 struct osmux_handle *handle = data;
46 struct sockaddr_in out = {
47 .sin_family = AF_INET,
48 .sin_port = handle->rem_port,
49 };
50
51 memcpy(&out.sin_addr, &handle->rem_addr, sizeof(handle->rem_addr));
52 sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0,
53 (struct sockaddr *)&out, sizeof(out));
54 msgb_free(batch_msg);
55}
56
Philipp Maier87bd9be2017-08-22 16:35:41 +020057/* Lookup existing OSMUX handle for specified destination address. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020058static struct osmux_handle *
59osmux_handle_find_get(struct in_addr *addr, int rem_port)
60{
61 struct osmux_handle *h;
62
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020063 llist_for_each_entry(h, &osmux_handle_list, head) {
64 if (memcmp(&h->rem_addr, addr, sizeof(struct in_addr)) == 0 &&
65 h->rem_port == rem_port) {
66 LOGP(DLMGCP, LOGL_DEBUG, "using existing OSMUX handle "
67 "for addr=%s:%d\n",
68 inet_ntoa(*addr), ntohs(rem_port));
69 h->refcnt++;
70 return h;
71 }
72 }
73
74 return NULL;
75}
76
Philipp Maier87bd9be2017-08-22 16:35:41 +020077/* Put down no longer needed OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020078static void osmux_handle_put(struct osmux_in_handle *in)
79{
80 struct osmux_handle *h;
81
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020082 llist_for_each_entry(h, &osmux_handle_list, head) {
83 if (h->in == in) {
84 if (--h->refcnt == 0) {
85 LOGP(DLMGCP, LOGL_INFO,
86 "Releasing unused osmux handle for %s:%d\n",
87 inet_ntoa(h->rem_addr),
88 ntohs(h->rem_port));
89 LOGP(DLMGCP, LOGL_INFO, "Stats: "
90 "input RTP msgs: %u bytes: %"PRIu64" "
91 "output osmux msgs: %u bytes: %"PRIu64"\n",
92 in->stats.input_rtp_msgs,
93 in->stats.input_rtp_bytes,
94 in->stats.output_osmux_msgs,
95 in->stats.output_osmux_bytes);
96 llist_del(&h->head);
97 osmux_xfrm_input_fini(h->in);
98 talloc_free(h);
99 }
100 return;
101 }
102 }
103 LOGP(DLMGCP, LOGL_ERROR, "cannot find Osmux input handle %p\n", in);
104}
105
Philipp Maier87bd9be2017-08-22 16:35:41 +0200106/* Allocate free OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200107static struct osmux_handle *
108osmux_handle_alloc(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
109{
110 struct osmux_handle *h;
111
112 h = talloc_zero(osmux, struct osmux_handle);
113 if (!h)
114 return NULL;
115 h->rem_addr = *addr;
116 h->rem_port = rem_port;
117 h->refcnt++;
118
119 h->in = talloc_zero(h, struct osmux_in_handle);
120 if (!h->in) {
121 talloc_free(h);
122 return NULL;
123 }
124
Philipp Maier87bd9be2017-08-22 16:35:41 +0200125 /* sequence number to start OSMUX message from */
126 h->in->osmux_seq = 0;
127
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200128 h->in->batch_factor = cfg->osmux_batch;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200129
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200130 /* If batch size is zero, the library defaults to 1470 bytes. */
131 h->in->batch_size = cfg->osmux_batch_size;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200132 h->in->deliver = osmux_deliver_cb;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200133 osmux_xfrm_input_init(h->in);
134 h->in->data = h;
135
136 llist_add(&h->head, &osmux_handle_list);
137
138 LOGP(DLMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s:%d\n",
139 inet_ntoa(*addr), ntohs(rem_port));
140
141 return h;
142}
143
Philipp Maier87bd9be2017-08-22 16:35:41 +0200144/* Lookup existing handle for a specified address, if the handle can not be
Harald Welte1d1b98f2017-12-25 10:03:40 +0100145 * found, the function will automatically allocate one */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200146static struct osmux_in_handle *
147osmux_handle_lookup(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
148{
149 struct osmux_handle *h;
150
151 h = osmux_handle_find_get(addr, rem_port);
152 if (h != NULL)
153 return h->in;
154
155 h = osmux_handle_alloc(cfg, addr, rem_port);
156 if (h == NULL)
157 return NULL;
158
159 return h->in;
160}
161
Philipp Maier87bd9be2017-08-22 16:35:41 +0200162/*! send RTP packet through OSMUX connection.
163 * \param[in] buf rtp data
164 * \param[in] buf_len length of rtp data
165 * \param[in] conn associated RTP connection
166 * \returns 0 on success, -1 on ERROR */
167int osmux_xfrm_to_osmux(char *buf, int buf_len, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200168{
169 int ret;
170 struct msgb *msg;
171
172 msg = msgb_alloc(4096, "RTP");
173 if (!msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200174 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200175
Philipp Maier87bd9be2017-08-22 16:35:41 +0200176 memcpy(msg->data, buf, buf_len);
177 msgb_put(msg, buf_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200178
Philipp Maier87bd9be2017-08-22 16:35:41 +0200179 while ((ret = osmux_xfrm_input(conn->osmux.in, msg, conn->osmux.cid)) > 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200180 /* batch full, build and deliver it */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200181 osmux_xfrm_input_deliver(conn->osmux.in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200182 }
183 return 0;
184}
185
Philipp Maier87bd9be2017-08-22 16:35:41 +0200186/* Lookup the endpoint that corresponds to the specified address (port) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200187static struct mgcp_endpoint *
188endpoint_lookup(struct mgcp_config *cfg, int cid,
189 struct in_addr *from_addr, int type)
190{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200191 struct mgcp_endpoint *endp = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200192 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200193 struct mgcp_conn_rtp *conn_net = NULL;
194 struct mgcp_conn_rtp *conn_bts = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200195
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200196 for (i=0; i<cfg->trunk.number_endpoints; i++) {
197 struct in_addr *this;
198
Philipp Maier87bd9be2017-08-22 16:35:41 +0200199 endp = &cfg->trunk.endpoints[i];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200200
Philipp Maier87bd9be2017-08-22 16:35:41 +0200201#if 0
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200202 if (!tmp->allocated)
203 continue;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200204#endif
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200205
206 switch(type) {
207 case MGCP_DEST_NET:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200208 /* FIXME: Get rid of CONN_ID_XXX! */
209 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
Philipp Maierddf1f9d2017-11-07 12:18:38 +0100210 if (conn_net)
211 this = &conn_net->end.addr;
212 else
213 this = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200214 break;
215 case MGCP_DEST_BTS:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200216 /* FIXME: Get rid of CONN_ID_XXX! */
217 conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS);
Philipp Maierddf1f9d2017-11-07 12:18:38 +0100218 if (conn_bts)
219 this = &conn_bts->end.addr;
220 else
221 this = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200222 break;
223 default:
224 /* Should not ever happen */
225 LOGP(DLMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type);
226 return NULL;
227 }
228
Philipp Maier87bd9be2017-08-22 16:35:41 +0200229 /* FIXME: Get rid of CONN_ID_XXX! */
230 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
Philipp Maierddf1f9d2017-11-07 12:18:38 +0100231 if (conn_net && this && conn_net->osmux.cid == cid
232 && this->s_addr == from_addr->s_addr)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200233 return endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200234 }
235
236 LOGP(DLMGCP, LOGL_ERROR, "Cannot find endpoint with cid=%d\n", cid);
237
238 return NULL;
239}
240
241static void scheduled_tx_net_cb(struct msgb *msg, void *data)
242{
243 struct mgcp_endpoint *endp = data;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200244 struct mgcp_conn_rtp *conn_net = NULL;
245 struct mgcp_conn_rtp *conn_bts = NULL;
246
247 /* FIXME: Get rid of CONN_ID_XXX! */
248 conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS);
249 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
250 if (!conn_bts || !conn_net)
251 return;
252
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200253 struct sockaddr_in addr = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200254 .sin_addr = conn_net->end.addr,
255 .sin_port = conn_net->end.rtp_port,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200256 };
257
Harald Weltea0ac30f2017-12-25 09:52:30 +0100258 conn_bts->end.stats.octets_tx += msg->len;
259 conn_bts->end.stats.packets_tx++;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200260
Philipp Maier87bd9be2017-08-22 16:35:41 +0200261 /* Send RTP data to NET */
262 /* FIXME: Get rid of conn_bts and conn_net! */
263 mgcp_send(endp, 1, &addr, (char *)msg->data, msg->len,
264 conn_bts, conn_net);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200265 msgb_free(msg);
266}
267
268static void scheduled_tx_bts_cb(struct msgb *msg, void *data)
269{
270 struct mgcp_endpoint *endp = data;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200271 struct mgcp_conn_rtp *conn_net = NULL;
272 struct mgcp_conn_rtp *conn_bts = NULL;
273
274 /* FIXME: Get rid of CONN_ID_XXX! */
275 conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS);
276 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
277 if (!conn_bts || !conn_net)
278 return;
279
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200280 struct sockaddr_in addr = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200281 .sin_addr = conn_bts->end.addr,
282 .sin_port = conn_bts->end.rtp_port,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200283 };
284
Harald Weltea0ac30f2017-12-25 09:52:30 +0100285 conn_net->end.stats.octets_tx += msg->len;
286 conn_net->end.stats.packets_tx++;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200287
Philipp Maier87bd9be2017-08-22 16:35:41 +0200288 /* Send RTP data to BTS */
289 /* FIXME: Get rid of conn_bts and conn_net! */
290 mgcp_send(endp, 1, &addr, (char *)msg->data, msg->len,
291 conn_net, conn_bts);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200292 msgb_free(msg);
293}
294
295static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr)
296{
297 struct msgb *msg;
298 socklen_t slen = sizeof(*addr);
299 int ret;
300
301 msg = msgb_alloc(4096, "OSMUX");
302 if (!msg) {
303 LOGP(DLMGCP, LOGL_ERROR, "cannot allocate message\n");
304 return NULL;
305 }
306 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0,
307 (struct sockaddr *)addr, &slen);
308 if (ret <= 0) {
309 msgb_free(msg);
310 LOGP(DLMGCP, LOGL_ERROR, "cannot receive message\n");
311 return NULL;
312 }
313 msgb_put(msg, ret);
314
315 return msg;
316}
317
318#define osmux_chunk_length(msg, rem) (rem - msg->len);
319
320int osmux_read_from_bsc_nat_cb(struct osmo_fd *ofd, unsigned int what)
321{
322 struct msgb *msg;
323 struct osmux_hdr *osmuxh;
324 struct llist_head list;
325 struct sockaddr_in addr;
326 struct mgcp_config *cfg = ofd->data;
327 uint32_t rem;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200328 struct mgcp_conn_rtp *conn_net = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200329
330 msg = osmux_recv(ofd, &addr);
331 if (!msg)
332 return -1;
333
334 /* not any further processing dummy messages */
335 if (msg->data[0] == MGCP_DUMMY_LOAD)
336 goto out;
337
338 rem = msg->len;
339 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
340 struct mgcp_endpoint *endp;
341
342 /* Yes, we use MGCP_DEST_NET to locate the origin */
343 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
344 &addr.sin_addr, MGCP_DEST_NET);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200345
346 /* FIXME: Get rid of CONN_ID_XXX! */
347 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
348 if (!conn_net)
349 goto out;
350
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200351 if (!endp) {
352 LOGP(DLMGCP, LOGL_ERROR,
353 "Cannot find an endpoint for circuit_id=%d\n",
354 osmuxh->circuit_id);
355 goto out;
356 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200357 conn_net->osmux.stats.octets += osmux_chunk_length(msg, rem);
358 conn_net->osmux.stats.chunks++;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200359 rem = msg->len;
360
Philipp Maier87bd9be2017-08-22 16:35:41 +0200361 osmux_xfrm_output(osmuxh, &conn_net->osmux.out, &list);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200362 osmux_tx_sched(&list, scheduled_tx_bts_cb, endp);
363 }
364out:
365 msgb_free(msg);
366 return 0;
367}
368
369/* This is called from the bsc-nat */
370static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr,
371 struct msgb *msg)
372{
373 struct mgcp_endpoint *endp;
374 uint8_t osmux_cid;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200375 struct mgcp_conn_rtp *conn_net = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200376
377 if (msg->len < 1 + sizeof(osmux_cid)) {
378 LOGP(DLMGCP, LOGL_ERROR,
379 "Discarding truncated Osmux dummy load\n");
380 goto out;
381 }
382
383 LOGP(DLMGCP, LOGL_DEBUG, "Received Osmux dummy load from %s\n",
384 inet_ntoa(addr->sin_addr));
385
386 if (!cfg->osmux) {
387 LOGP(DLMGCP, LOGL_ERROR,
388 "bsc wants to use Osmux but bsc-nat did not request it\n");
389 goto out;
390 }
391
392 /* extract the osmux CID from the dummy message */
393 memcpy(&osmux_cid, &msg->data[1], sizeof(osmux_cid));
394
395 endp = endpoint_lookup(cfg, osmux_cid, &addr->sin_addr, MGCP_DEST_BTS);
396 if (!endp) {
397 LOGP(DLMGCP, LOGL_ERROR,
398 "Cannot find endpoint for Osmux CID %d\n", osmux_cid);
399 goto out;
400 }
401
Philipp Maier87bd9be2017-08-22 16:35:41 +0200402 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
403 if (!conn_net)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200404 goto out;
405
Philipp Maier87bd9be2017-08-22 16:35:41 +0200406 if (conn_net->osmux.state == OSMUX_STATE_ENABLED)
407 goto out;
408
409 if (osmux_enable_conn(endp, conn_net, &addr->sin_addr, addr->sin_port) < 0 ) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200410 LOGP(DLMGCP, LOGL_ERROR,
411 "Could not enable osmux in endpoint %d\n",
412 ENDPOINT_NUMBER(endp));
413 goto out;
414 }
415
416 LOGP(DLMGCP, LOGL_INFO, "Enabling osmux in endpoint %d for %s:%u\n",
417 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr),
418 ntohs(addr->sin_port));
419out:
420 msgb_free(msg);
421 return 0;
422}
423
424int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what)
425{
426 struct msgb *msg;
427 struct osmux_hdr *osmuxh;
428 struct llist_head list;
429 struct sockaddr_in addr;
430 struct mgcp_config *cfg = ofd->data;
431 uint32_t rem;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200432 struct mgcp_conn_rtp *conn_net = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200433
434 msg = osmux_recv(ofd, &addr);
435 if (!msg)
436 return -1;
437
438 /* not any further processing dummy messages */
439 if (msg->data[0] == MGCP_DUMMY_LOAD)
440 return osmux_handle_dummy(cfg, &addr, msg);
441
442 rem = msg->len;
443 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
444 struct mgcp_endpoint *endp;
445
446 /* Yes, we use MGCP_DEST_BTS to locate the origin */
447 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
448 &addr.sin_addr, MGCP_DEST_BTS);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200449
450 /* FIXME: Get rid of CONN_ID_XXX! */
451 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
452 if (!conn_net)
453 goto out;
454
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200455 if (!endp) {
456 LOGP(DLMGCP, LOGL_ERROR,
457 "Cannot find an endpoint for circuit_id=%d\n",
458 osmuxh->circuit_id);
459 goto out;
460 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200461 conn_net->osmux.stats.octets += osmux_chunk_length(msg, rem);
462 conn_net->osmux.stats.chunks++;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200463 rem = msg->len;
464
Philipp Maier87bd9be2017-08-22 16:35:41 +0200465 osmux_xfrm_output(osmuxh, &conn_net->osmux.out, &list);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200466 osmux_tx_sched(&list, scheduled_tx_net_cb, endp);
467 }
468out:
469 msgb_free(msg);
470 return 0;
471}
472
473int osmux_init(int role, struct mgcp_config *cfg)
474{
475 int ret;
476
477 switch(role) {
478 case OSMUX_ROLE_BSC:
479 osmux_fd.cb = osmux_read_from_bsc_nat_cb;
480 break;
481 case OSMUX_ROLE_BSC_NAT:
482 osmux_fd.cb = osmux_read_from_bsc_cb;
483 break;
484 default:
485 LOGP(DLMGCP, LOGL_ERROR, "wrong role for OSMUX\n");
486 return -1;
487 }
488 osmux_fd.data = cfg;
489
490 ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port);
491 if (ret < 0) {
492 LOGP(DLMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n");
493 return ret;
494 }
495 mgcp_set_ip_tos(osmux_fd.fd, cfg->endp_dscp);
496 osmux_fd.when |= BSC_FD_READ;
497
498 ret = osmo_fd_register(&osmux_fd);
499 if (ret < 0) {
500 LOGP(DLMGCP, LOGL_ERROR, "cannot register OSMUX socket\n");
501 return ret;
502 }
503 cfg->osmux_init = 1;
504
505 return 0;
506}
507
Philipp Maier87bd9be2017-08-22 16:35:41 +0200508/*! enable OSXMUX circuit for a specified connection.
509 * \param[in] endp mgcp endpoint (configuration)
510 * \param[in] conn connection to disable
511 * \param[in] addr IP address of remote OSMUX endpoint
512 * \param[in] port portnumber of the remote OSMUX endpoint
513 * \returns 0 on success, -1 on ERROR */
514int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
515 struct in_addr *addr, uint16_t port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200516{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200517 /*! If osmux is enabled, initialize the output handler. This handler is
518 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
519 * allocated based on the circuit ID (conn_net->osmux.cid), which is unique
520 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
521 * SSRC space (2^32) by the 256 possible circuit IDs, then randomly
522 * select one value from that window. Thus, we have no chance to have
523 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
524 * similarly, for flows traveling to the MSC.
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200525 */
526 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200527 uint16_t osmux_dummy = endp->cfg->osmux_dummy;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200528
Philipp Maier87bd9be2017-08-22 16:35:41 +0200529 /* Check if osmux is enabled for the specified connection */
530 if (conn->osmux.state == OSMUX_STATE_DISABLED) {
531 LOGP(DLMGCP, LOGL_ERROR, "OSMUX not enabled for conn:%s\n",
532 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200533 return -1;
534 }
535
Philipp Maier87bd9be2017-08-22 16:35:41 +0200536 osmux_xfrm_output_init(&conn->osmux.out,
537 (conn->osmux.cid * rtp_ssrc_winlen) +
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200538 (random() % rtp_ssrc_winlen));
539
Philipp Maier87bd9be2017-08-22 16:35:41 +0200540 conn->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
541 if (!conn->osmux.in) {
542 LOGP(DLMGCP, LOGL_ERROR, "Cannot allocate input osmux handle for conn:%s\n",
543 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200544 return -1;
545 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200546 if (!osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.cid, osmux_dummy)) {
547 LOGP(DLMGCP, LOGL_ERROR, "Cannot open osmux circuit %u for conn:%s\n",
548 conn->osmux.cid, mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200549 return -1;
550 }
551
552 switch (endp->cfg->role) {
553 case MGCP_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200554 conn->type = MGCP_OSMUX_BSC_NAT;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200555 break;
556 case MGCP_BSC:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200557 conn->type = MGCP_OSMUX_BSC;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200558 break;
559 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200560
561 conn->osmux.state = OSMUX_STATE_ENABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200562
563 return 0;
564}
565
Philipp Maier87bd9be2017-08-22 16:35:41 +0200566/*! disable OSXMUX circuit for a specified connection.
567 * \param[in] conn connection to disable */
568void osmux_disable_conn(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200569{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200570 if (!conn)
571 return;
572
573 if (conn->osmux.state != OSMUX_STATE_ENABLED)
574 return;
575
Philipp Maier01d24a32017-11-21 17:26:09 +0100576 LOGP(DLMGCP, LOGL_INFO, "Releasing connection %s using Osmux CID %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200577 conn->conn->id, conn->osmux.cid);
578 osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.cid);
579 conn->osmux.state = OSMUX_STATE_DISABLED;
580 conn->osmux.cid = -1;
581 osmux_handle_put(conn->osmux.in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200582}
583
Philipp Maier87bd9be2017-08-22 16:35:41 +0200584/*! relase OSXMUX cid, that had been allocated to this connection.
585 * \param[in] conn connection with OSMUX cid to release */
586void osmux_release_cid(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200587{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200588 if (!conn)
589 return;
590
591 if (conn->osmux.state != OSMUX_STATE_ENABLED)
592 return;
593
594 if (conn->osmux.allocated_cid >= 0)
595 osmux_put_cid(conn->osmux.allocated_cid);
596 conn->osmux.allocated_cid = -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200597}
598
Philipp Maier87bd9be2017-08-22 16:35:41 +0200599/*! allocate OSXMUX cid to connection.
600 * \param[in] conn connection for which we allocate the OSMUX cid*/
601void osmux_allocate_cid(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200602{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200603 osmux_release_cid(conn);
604 conn->osmux.allocated_cid = osmux_get_cid();
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200605}
606
Philipp Maier87bd9be2017-08-22 16:35:41 +0200607/*! send RTP dummy packet to OSMUX connection port.
608 * \param[in] endp mcgp endpoint that holds the RTP connection
609 * \param[in] conn associated RTP connection
610 * \returns bytes sent, -1 on error */
611int osmux_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200612{
613 char buf[1 + sizeof(uint8_t)];
614 struct in_addr addr_unset = {};
615
Philipp Maier87bd9be2017-08-22 16:35:41 +0200616 /*! The dummy packet will not be sent via the actual OSMUX connection,
617 * instead it is sent out of band to port where the remote OSMUX
618 * multplexer is listening. The goal is to ensure that the connection
619 * is kept open */
620
621 /*! We don't need to send the dummy load for osmux so often as another
622 * endpoint may have already punched the hole in the firewall. This
623 * approach is simple though. */
624
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200625 buf[0] = MGCP_DUMMY_LOAD;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200626 memcpy(&buf[1], &conn->osmux.cid, sizeof(conn->osmux.cid));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200627
628 /* Wait until we have the connection information from MDCX */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200629 if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200630 return 0;
631
Philipp Maier87bd9be2017-08-22 16:35:41 +0200632 if (conn->osmux.state == OSMUX_STATE_ACTIVATING) {
633 if (osmux_enable_conn(endp, conn, &conn->end.addr,
634 htons(endp->cfg->osmux_port)) < 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200635 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200636 "Could not activate osmux for conn:%s\n",
637 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200638 }
639 LOGP(DLMGCP, LOGL_ERROR,
640 "Osmux CID %u for %s:%u is now enabled\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200641 conn->osmux.cid, inet_ntoa(conn->end.addr),
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200642 endp->cfg->osmux_port);
643 }
644 LOGP(DLMGCP, LOGL_DEBUG,
645 "sending OSMUX dummy load to %s CID %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200646 inet_ntoa(conn->end.addr), conn->osmux.cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200647
Philipp Maier87bd9be2017-08-22 16:35:41 +0200648 return mgcp_udp_send(osmux_fd.fd, &conn->end.addr,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200649 htons(endp->cfg->osmux_port), buf, sizeof(buf));
650}
651
Philipp Maier87bd9be2017-08-22 16:35:41 +0200652/*! bsc-nat allocates/releases the OSMUX cids (Circuit IDs). */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200653static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1) / 8];
654
Philipp Maier87bd9be2017-08-22 16:35:41 +0200655/*! count the number of taken OSMUX cids.
656 * \returns number of OSMUX cids in use */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200657int osmux_used_cid(void)
658{
659 int i, j, used = 0;
660
661 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
662 for (j = 0; j < 8; j++) {
663 if (osmux_cid_bitmap[i] & (1 << j))
664 used += 1;
665 }
666 }
667
668 return used;
669}
670
Philipp Maier87bd9be2017-08-22 16:35:41 +0200671/*! take a free OSMUX cid.
672 * \returns OSMUX cid */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200673int osmux_get_cid(void)
674{
675 int i, j;
676
677 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
678 for (j = 0; j < 8; j++) {
679 if (osmux_cid_bitmap[i] & (1 << j))
680 continue;
681
682 osmux_cid_bitmap[i] |= (1 << j);
683 LOGP(DLMGCP, LOGL_DEBUG,
684 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
685 return (i * 8) + j;
686 }
687 }
688
689 LOGP(DLMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n");
690 return -1;
691}
692
Philipp Maier87bd9be2017-08-22 16:35:41 +0200693/*! put back a no longer used OSMUX cid.
694 * \param[in] osmux_cid OSMUX cid */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200695void osmux_put_cid(uint8_t osmux_cid)
696{
697 LOGP(DLMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
698 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
699}