blob: 60ffe067e75f153aaff5af4313e59715d483f830 [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
145 * foud a 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);
210 this = &conn_net->end.addr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200211 break;
212 case MGCP_DEST_BTS:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200213 /* FIXME: Get rid of CONN_ID_XXX! */
214 conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS);
215 this = &conn_bts->end.addr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200216 break;
217 default:
218 /* Should not ever happen */
219 LOGP(DLMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type);
220 return NULL;
221 }
222
Philipp Maier87bd9be2017-08-22 16:35:41 +0200223 /* FIXME: Get rid of CONN_ID_XXX! */
224 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
225 if (conn_net->osmux.cid == cid && this->s_addr == from_addr->s_addr)
226 return endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200227 }
228
229 LOGP(DLMGCP, LOGL_ERROR, "Cannot find endpoint with cid=%d\n", cid);
230
231 return NULL;
232}
233
234static void scheduled_tx_net_cb(struct msgb *msg, void *data)
235{
236 struct mgcp_endpoint *endp = data;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200237 struct mgcp_conn_rtp *conn_net = NULL;
238 struct mgcp_conn_rtp *conn_bts = NULL;
239
240 /* FIXME: Get rid of CONN_ID_XXX! */
241 conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS);
242 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
243 if (!conn_bts || !conn_net)
244 return;
245
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200246 struct sockaddr_in addr = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200247 .sin_addr = conn_net->end.addr,
248 .sin_port = conn_net->end.rtp_port,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200249 };
250
Philipp Maier87bd9be2017-08-22 16:35:41 +0200251 conn_bts->end.octets_tx += msg->len;
252 conn_bts->end.packets_tx++;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200253
Philipp Maier87bd9be2017-08-22 16:35:41 +0200254 /* Send RTP data to NET */
255 /* FIXME: Get rid of conn_bts and conn_net! */
256 mgcp_send(endp, 1, &addr, (char *)msg->data, msg->len,
257 conn_bts, conn_net);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200258 msgb_free(msg);
259}
260
261static void scheduled_tx_bts_cb(struct msgb *msg, void *data)
262{
263 struct mgcp_endpoint *endp = data;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200264 struct mgcp_conn_rtp *conn_net = NULL;
265 struct mgcp_conn_rtp *conn_bts = NULL;
266
267 /* FIXME: Get rid of CONN_ID_XXX! */
268 conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS);
269 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
270 if (!conn_bts || !conn_net)
271 return;
272
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200273 struct sockaddr_in addr = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200274 .sin_addr = conn_bts->end.addr,
275 .sin_port = conn_bts->end.rtp_port,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200276 };
277
Philipp Maier87bd9be2017-08-22 16:35:41 +0200278 conn_net->end.octets_tx += msg->len;
279 conn_net->end.packets_tx++;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200280
Philipp Maier87bd9be2017-08-22 16:35:41 +0200281 /* Send RTP data to BTS */
282 /* FIXME: Get rid of conn_bts and conn_net! */
283 mgcp_send(endp, 1, &addr, (char *)msg->data, msg->len,
284 conn_net, conn_bts);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200285 msgb_free(msg);
286}
287
288static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr)
289{
290 struct msgb *msg;
291 socklen_t slen = sizeof(*addr);
292 int ret;
293
294 msg = msgb_alloc(4096, "OSMUX");
295 if (!msg) {
296 LOGP(DLMGCP, LOGL_ERROR, "cannot allocate message\n");
297 return NULL;
298 }
299 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0,
300 (struct sockaddr *)addr, &slen);
301 if (ret <= 0) {
302 msgb_free(msg);
303 LOGP(DLMGCP, LOGL_ERROR, "cannot receive message\n");
304 return NULL;
305 }
306 msgb_put(msg, ret);
307
308 return msg;
309}
310
311#define osmux_chunk_length(msg, rem) (rem - msg->len);
312
313int osmux_read_from_bsc_nat_cb(struct osmo_fd *ofd, unsigned int what)
314{
315 struct msgb *msg;
316 struct osmux_hdr *osmuxh;
317 struct llist_head list;
318 struct sockaddr_in addr;
319 struct mgcp_config *cfg = ofd->data;
320 uint32_t rem;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200321 struct mgcp_conn_rtp *conn_net = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200322
323 msg = osmux_recv(ofd, &addr);
324 if (!msg)
325 return -1;
326
327 /* not any further processing dummy messages */
328 if (msg->data[0] == MGCP_DUMMY_LOAD)
329 goto out;
330
331 rem = msg->len;
332 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
333 struct mgcp_endpoint *endp;
334
335 /* Yes, we use MGCP_DEST_NET to locate the origin */
336 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
337 &addr.sin_addr, MGCP_DEST_NET);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200338
339 /* FIXME: Get rid of CONN_ID_XXX! */
340 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
341 if (!conn_net)
342 goto out;
343
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200344 if (!endp) {
345 LOGP(DLMGCP, LOGL_ERROR,
346 "Cannot find an endpoint for circuit_id=%d\n",
347 osmuxh->circuit_id);
348 goto out;
349 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200350 conn_net->osmux.stats.octets += osmux_chunk_length(msg, rem);
351 conn_net->osmux.stats.chunks++;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200352 rem = msg->len;
353
Philipp Maier87bd9be2017-08-22 16:35:41 +0200354 osmux_xfrm_output(osmuxh, &conn_net->osmux.out, &list);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200355 osmux_tx_sched(&list, scheduled_tx_bts_cb, endp);
356 }
357out:
358 msgb_free(msg);
359 return 0;
360}
361
362/* This is called from the bsc-nat */
363static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr,
364 struct msgb *msg)
365{
366 struct mgcp_endpoint *endp;
367 uint8_t osmux_cid;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200368 struct mgcp_conn_rtp *conn_net = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200369
370 if (msg->len < 1 + sizeof(osmux_cid)) {
371 LOGP(DLMGCP, LOGL_ERROR,
372 "Discarding truncated Osmux dummy load\n");
373 goto out;
374 }
375
376 LOGP(DLMGCP, LOGL_DEBUG, "Received Osmux dummy load from %s\n",
377 inet_ntoa(addr->sin_addr));
378
379 if (!cfg->osmux) {
380 LOGP(DLMGCP, LOGL_ERROR,
381 "bsc wants to use Osmux but bsc-nat did not request it\n");
382 goto out;
383 }
384
385 /* extract the osmux CID from the dummy message */
386 memcpy(&osmux_cid, &msg->data[1], sizeof(osmux_cid));
387
388 endp = endpoint_lookup(cfg, osmux_cid, &addr->sin_addr, MGCP_DEST_BTS);
389 if (!endp) {
390 LOGP(DLMGCP, LOGL_ERROR,
391 "Cannot find endpoint for Osmux CID %d\n", osmux_cid);
392 goto out;
393 }
394
Philipp Maier87bd9be2017-08-22 16:35:41 +0200395 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
396 if (!conn_net)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200397 goto out;
398
Philipp Maier87bd9be2017-08-22 16:35:41 +0200399 if (conn_net->osmux.state == OSMUX_STATE_ENABLED)
400 goto out;
401
402 if (osmux_enable_conn(endp, conn_net, &addr->sin_addr, addr->sin_port) < 0 ) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200403 LOGP(DLMGCP, LOGL_ERROR,
404 "Could not enable osmux in endpoint %d\n",
405 ENDPOINT_NUMBER(endp));
406 goto out;
407 }
408
409 LOGP(DLMGCP, LOGL_INFO, "Enabling osmux in endpoint %d for %s:%u\n",
410 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr),
411 ntohs(addr->sin_port));
412out:
413 msgb_free(msg);
414 return 0;
415}
416
417int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what)
418{
419 struct msgb *msg;
420 struct osmux_hdr *osmuxh;
421 struct llist_head list;
422 struct sockaddr_in addr;
423 struct mgcp_config *cfg = ofd->data;
424 uint32_t rem;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200425 struct mgcp_conn_rtp *conn_net = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200426
427 msg = osmux_recv(ofd, &addr);
428 if (!msg)
429 return -1;
430
431 /* not any further processing dummy messages */
432 if (msg->data[0] == MGCP_DUMMY_LOAD)
433 return osmux_handle_dummy(cfg, &addr, msg);
434
435 rem = msg->len;
436 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
437 struct mgcp_endpoint *endp;
438
439 /* Yes, we use MGCP_DEST_BTS to locate the origin */
440 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
441 &addr.sin_addr, MGCP_DEST_BTS);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200442
443 /* FIXME: Get rid of CONN_ID_XXX! */
444 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
445 if (!conn_net)
446 goto out;
447
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200448 if (!endp) {
449 LOGP(DLMGCP, LOGL_ERROR,
450 "Cannot find an endpoint for circuit_id=%d\n",
451 osmuxh->circuit_id);
452 goto out;
453 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200454 conn_net->osmux.stats.octets += osmux_chunk_length(msg, rem);
455 conn_net->osmux.stats.chunks++;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200456 rem = msg->len;
457
Philipp Maier87bd9be2017-08-22 16:35:41 +0200458 osmux_xfrm_output(osmuxh, &conn_net->osmux.out, &list);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200459 osmux_tx_sched(&list, scheduled_tx_net_cb, endp);
460 }
461out:
462 msgb_free(msg);
463 return 0;
464}
465
466int osmux_init(int role, struct mgcp_config *cfg)
467{
468 int ret;
469
470 switch(role) {
471 case OSMUX_ROLE_BSC:
472 osmux_fd.cb = osmux_read_from_bsc_nat_cb;
473 break;
474 case OSMUX_ROLE_BSC_NAT:
475 osmux_fd.cb = osmux_read_from_bsc_cb;
476 break;
477 default:
478 LOGP(DLMGCP, LOGL_ERROR, "wrong role for OSMUX\n");
479 return -1;
480 }
481 osmux_fd.data = cfg;
482
483 ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port);
484 if (ret < 0) {
485 LOGP(DLMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n");
486 return ret;
487 }
488 mgcp_set_ip_tos(osmux_fd.fd, cfg->endp_dscp);
489 osmux_fd.when |= BSC_FD_READ;
490
491 ret = osmo_fd_register(&osmux_fd);
492 if (ret < 0) {
493 LOGP(DLMGCP, LOGL_ERROR, "cannot register OSMUX socket\n");
494 return ret;
495 }
496 cfg->osmux_init = 1;
497
498 return 0;
499}
500
Philipp Maier87bd9be2017-08-22 16:35:41 +0200501/*! enable OSXMUX circuit for a specified connection.
502 * \param[in] endp mgcp endpoint (configuration)
503 * \param[in] conn connection to disable
504 * \param[in] addr IP address of remote OSMUX endpoint
505 * \param[in] port portnumber of the remote OSMUX endpoint
506 * \returns 0 on success, -1 on ERROR */
507int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
508 struct in_addr *addr, uint16_t port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200509{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200510 /*! If osmux is enabled, initialize the output handler. This handler is
511 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
512 * allocated based on the circuit ID (conn_net->osmux.cid), which is unique
513 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
514 * SSRC space (2^32) by the 256 possible circuit IDs, then randomly
515 * select one value from that window. Thus, we have no chance to have
516 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
517 * similarly, for flows traveling to the MSC.
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200518 */
519 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200520 uint16_t osmux_dummy = endp->cfg->osmux_dummy;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200521
Philipp Maier87bd9be2017-08-22 16:35:41 +0200522 /* Check if osmux is enabled for the specified connection */
523 if (conn->osmux.state == OSMUX_STATE_DISABLED) {
524 LOGP(DLMGCP, LOGL_ERROR, "OSMUX not enabled for conn:%s\n",
525 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200526 return -1;
527 }
528
Philipp Maier87bd9be2017-08-22 16:35:41 +0200529 osmux_xfrm_output_init(&conn->osmux.out,
530 (conn->osmux.cid * rtp_ssrc_winlen) +
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200531 (random() % rtp_ssrc_winlen));
532
Philipp Maier87bd9be2017-08-22 16:35:41 +0200533 conn->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
534 if (!conn->osmux.in) {
535 LOGP(DLMGCP, LOGL_ERROR, "Cannot allocate input osmux handle for conn:%s\n",
536 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200537 return -1;
538 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200539 if (!osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.cid, osmux_dummy)) {
540 LOGP(DLMGCP, LOGL_ERROR, "Cannot open osmux circuit %u for conn:%s\n",
541 conn->osmux.cid, mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200542 return -1;
543 }
544
545 switch (endp->cfg->role) {
546 case MGCP_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200547 conn->type = MGCP_OSMUX_BSC_NAT;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200548 break;
549 case MGCP_BSC:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200550 conn->type = MGCP_OSMUX_BSC;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200551 break;
552 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200553
554 conn->osmux.state = OSMUX_STATE_ENABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200555
556 return 0;
557}
558
Philipp Maier87bd9be2017-08-22 16:35:41 +0200559/*! disable OSXMUX circuit for a specified connection.
560 * \param[in] conn connection to disable */
561void osmux_disable_conn(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200562{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200563 if (!conn)
564 return;
565
566 if (conn->osmux.state != OSMUX_STATE_ENABLED)
567 return;
568
569 LOGP(DLMGCP, LOGL_INFO, "Releasing connection %u using Osmux CID %u\n",
570 conn->conn->id, conn->osmux.cid);
571 osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.cid);
572 conn->osmux.state = OSMUX_STATE_DISABLED;
573 conn->osmux.cid = -1;
574 osmux_handle_put(conn->osmux.in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200575}
576
Philipp Maier87bd9be2017-08-22 16:35:41 +0200577/*! relase OSXMUX cid, that had been allocated to this connection.
578 * \param[in] conn connection with OSMUX cid to release */
579void osmux_release_cid(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200580{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200581 if (!conn)
582 return;
583
584 if (conn->osmux.state != OSMUX_STATE_ENABLED)
585 return;
586
587 if (conn->osmux.allocated_cid >= 0)
588 osmux_put_cid(conn->osmux.allocated_cid);
589 conn->osmux.allocated_cid = -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200590}
591
Philipp Maier87bd9be2017-08-22 16:35:41 +0200592/*! allocate OSXMUX cid to connection.
593 * \param[in] conn connection for which we allocate the OSMUX cid*/
594void osmux_allocate_cid(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200595{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200596 osmux_release_cid(conn);
597 conn->osmux.allocated_cid = osmux_get_cid();
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200598}
599
Philipp Maier87bd9be2017-08-22 16:35:41 +0200600/*! send RTP dummy packet to OSMUX connection port.
601 * \param[in] endp mcgp endpoint that holds the RTP connection
602 * \param[in] conn associated RTP connection
603 * \returns bytes sent, -1 on error */
604int osmux_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200605{
606 char buf[1 + sizeof(uint8_t)];
607 struct in_addr addr_unset = {};
608
Philipp Maier87bd9be2017-08-22 16:35:41 +0200609 /*! The dummy packet will not be sent via the actual OSMUX connection,
610 * instead it is sent out of band to port where the remote OSMUX
611 * multplexer is listening. The goal is to ensure that the connection
612 * is kept open */
613
614 /*! We don't need to send the dummy load for osmux so often as another
615 * endpoint may have already punched the hole in the firewall. This
616 * approach is simple though. */
617
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200618 buf[0] = MGCP_DUMMY_LOAD;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200619 memcpy(&buf[1], &conn->osmux.cid, sizeof(conn->osmux.cid));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200620
621 /* Wait until we have the connection information from MDCX */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200622 if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200623 return 0;
624
Philipp Maier87bd9be2017-08-22 16:35:41 +0200625 if (conn->osmux.state == OSMUX_STATE_ACTIVATING) {
626 if (osmux_enable_conn(endp, conn, &conn->end.addr,
627 htons(endp->cfg->osmux_port)) < 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200628 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200629 "Could not activate osmux for conn:%s\n",
630 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200631 }
632 LOGP(DLMGCP, LOGL_ERROR,
633 "Osmux CID %u for %s:%u is now enabled\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200634 conn->osmux.cid, inet_ntoa(conn->end.addr),
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200635 endp->cfg->osmux_port);
636 }
637 LOGP(DLMGCP, LOGL_DEBUG,
638 "sending OSMUX dummy load to %s CID %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200639 inet_ntoa(conn->end.addr), conn->osmux.cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200640
Philipp Maier87bd9be2017-08-22 16:35:41 +0200641 return mgcp_udp_send(osmux_fd.fd, &conn->end.addr,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200642 htons(endp->cfg->osmux_port), buf, sizeof(buf));
643}
644
Philipp Maier87bd9be2017-08-22 16:35:41 +0200645/*! bsc-nat allocates/releases the OSMUX cids (Circuit IDs). */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200646static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1) / 8];
647
Philipp Maier87bd9be2017-08-22 16:35:41 +0200648/*! count the number of taken OSMUX cids.
649 * \returns number of OSMUX cids in use */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200650int osmux_used_cid(void)
651{
652 int i, j, used = 0;
653
654 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
655 for (j = 0; j < 8; j++) {
656 if (osmux_cid_bitmap[i] & (1 << j))
657 used += 1;
658 }
659 }
660
661 return used;
662}
663
Philipp Maier87bd9be2017-08-22 16:35:41 +0200664/*! take a free OSMUX cid.
665 * \returns OSMUX cid */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200666int osmux_get_cid(void)
667{
668 int i, j;
669
670 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
671 for (j = 0; j < 8; j++) {
672 if (osmux_cid_bitmap[i] & (1 << j))
673 continue;
674
675 osmux_cid_bitmap[i] |= (1 << j);
676 LOGP(DLMGCP, LOGL_DEBUG,
677 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
678 return (i * 8) + j;
679 }
680 }
681
682 LOGP(DLMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n");
683 return -1;
684}
685
Philipp Maier87bd9be2017-08-22 16:35:41 +0200686/*! put back a no longer used OSMUX cid.
687 * \param[in] osmux_cid OSMUX cid */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200688void osmux_put_cid(uint8_t osmux_cid)
689{
690 LOGP(DLMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
691 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
692}