blob: 743d3f9cc9735676075440d8324d0e5ed6455d01 [file] [log] [blame]
Neels Hofmeyre9920f22017-07-10 15:07:22 +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
23#include <osmocom/legacy_mgcp/mgcp.h>
24#include <osmocom/legacy_mgcp/mgcp_internal.h>
25#include <osmocom/legacy_mgcp/osmux.h>
26
27static 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;
34 struct in_addr rem_addr;
35 int rem_port;
36 int refcnt;
37};
38
39static void *osmux;
40
41static void osmux_deliver(struct msgb *batch_msg, void *data)
42{
43 struct osmux_handle *handle = data;
44 struct sockaddr_in out = {
45 .sin_family = AF_INET,
46 .sin_port = handle->rem_port,
47 };
48
49 memcpy(&out.sin_addr, &handle->rem_addr, sizeof(handle->rem_addr));
50 sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0,
51 (struct sockaddr *)&out, sizeof(out));
52 msgb_free(batch_msg);
53}
54
55static struct osmux_handle *
56osmux_handle_find_get(struct in_addr *addr, int rem_port)
57{
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) {
62 if (memcmp(&h->rem_addr, addr, sizeof(struct in_addr)) == 0 &&
63 h->rem_port == rem_port) {
64 LOGP(DLMGCP, LOGL_DEBUG, "using existing OSMUX handle "
65 "for addr=%s:%d\n",
66 inet_ntoa(*addr), ntohs(rem_port));
67 h->refcnt++;
68 return h;
69 }
70 }
71
72 return NULL;
73}
74
75static 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) {
83 LOGP(DLMGCP, LOGL_INFO,
84 "Releasing unused osmux handle for %s:%d\n",
85 inet_ntoa(h->rem_addr),
86 ntohs(h->rem_port));
87 LOGP(DLMGCP, 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);
94 llist_del(&h->head);
95 osmux_xfrm_input_fini(h->in);
96 talloc_free(h);
97 }
98 return;
99 }
100 }
101 LOGP(DLMGCP, LOGL_ERROR, "cannot find Osmux input handle %p\n", in);
102}
103
104static struct osmux_handle *
105osmux_handle_alloc(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
106{
107 struct osmux_handle *h;
108
109 h = talloc_zero(osmux, struct osmux_handle);
110 if (!h)
111 return NULL;
112 h->rem_addr = *addr;
113 h->rem_port = rem_port;
114 h->refcnt++;
115
116 h->in = talloc_zero(h, struct osmux_in_handle);
117 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;
124 /* If batch size is zero, the library defaults to 1470 bytes. */
125 h->in->batch_size = cfg->osmux_batch_size;
126 h->in->deliver = osmux_deliver;
127 osmux_xfrm_input_init(h->in);
128 h->in->data = h;
129
130 llist_add(&h->head, &osmux_handle_list);
131
132 LOGP(DLMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s:%d\n",
133 inet_ntoa(*addr), ntohs(rem_port));
134
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
151 return h->in;
152}
153
154int osmux_xfrm_to_osmux(int type, char *buf, int rc, struct mgcp_endpoint *endp)
155{
156 int ret;
157 struct msgb *msg;
158
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
166 while ((ret = osmux_xfrm_input(endp->osmux.in, msg, endp->osmux.cid)) > 0) {
167 /* batch full, build and deliver it */
168 osmux_xfrm_input_deliver(endp->osmux.in);
169 }
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(DLMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type);
199 return NULL;
200 }
201
202 if (tmp->osmux.cid == cid && this->s_addr == from_addr->s_addr)
203 return tmp;
204 }
205
206 LOGP(DLMGCP, LOGL_ERROR, "Cannot find endpoint with cid=%d\n", cid);
207
208 return NULL;
209}
210
211static void scheduled_tx_net_cb(struct msgb *msg, void *data)
212{
213 struct mgcp_endpoint *endp = data;
214 struct sockaddr_in addr = {
215 .sin_addr = endp->net_end.addr,
216 .sin_port = endp->net_end.rtp_port,
217 };
218
219 endp->bts_end.octets += msg->len;
220 endp->bts_end.packets++;
221
222 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;
229 struct sockaddr_in addr = {
230 .sin_addr = endp->bts_end.addr,
231 .sin_port = endp->bts_end.rtp_port,
232 };
233
234 endp->net_end.octets += msg->len;
235 endp->net_end.packets++;
236
237 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(DLMGCP, 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(DLMGCP, LOGL_ERROR, "cannot receive message\n");
257 return NULL;
258 }
259 msgb_put(msg, ret);
260
261 return msg;
262}
263
264#define osmux_chunk_length(msg, rem) (rem - msg->len);
265
266int 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;
273 uint32_t rem;
274
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
283 rem = msg->len;
284 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(DLMGCP, LOGL_ERROR,
292 "Cannot find an endpoint for circuit_id=%d\n",
293 osmuxh->circuit_id);
294 goto out;
295 }
296 endp->osmux.stats.octets += osmux_chunk_length(msg, rem);
297 endp->osmux.stats.chunks++;
298 rem = msg->len;
299
300 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
301 osmux_tx_sched(&list, scheduled_tx_bts_cb, endp);
302 }
303out:
304 msgb_free(msg);
305 return 0;
306}
307
308/* This is called from the bsc-nat */
309static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr,
310 struct msgb *msg)
311{
312 struct mgcp_endpoint *endp;
313 uint8_t osmux_cid;
314
315 if (msg->len < 1 + sizeof(osmux_cid)) {
316 LOGP(DLMGCP, LOGL_ERROR,
317 "Discarding truncated Osmux dummy load\n");
318 goto out;
319 }
320
321 LOGP(DLMGCP, LOGL_DEBUG, "Received Osmux dummy load from %s\n",
322 inet_ntoa(addr->sin_addr));
323
324 if (!cfg->osmux) {
325 LOGP(DLMGCP, 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);
334 if (!endp) {
335 LOGP(DLMGCP, LOGL_ERROR,
336 "Cannot find endpoint for Osmux CID %d\n", osmux_cid);
337 goto out;
338 }
339
340 if (endp->osmux.state == OSMUX_STATE_ENABLED)
341 goto out;
342
343 if (osmux_enable_endpoint(endp, &addr->sin_addr, addr->sin_port) < 0 ) {
344 LOGP(DLMGCP, LOGL_ERROR,
345 "Could not enable osmux in endpoint %d\n",
346 ENDPOINT_NUMBER(endp));
347 goto out;
348 }
349
350 LOGP(DLMGCP, LOGL_INFO, "Enabling osmux in endpoint %d for %s:%u\n",
351 ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr),
352 ntohs(addr->sin_port));
353out:
354 msgb_free(msg);
355 return 0;
356}
357
358int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what)
359{
360 struct msgb *msg;
361 struct osmux_hdr *osmuxh;
362 struct llist_head list;
363 struct sockaddr_in addr;
364 struct mgcp_config *cfg = ofd->data;
365 uint32_t rem;
366
367 msg = osmux_recv(ofd, &addr);
368 if (!msg)
369 return -1;
370
371 /* not any further processing dummy messages */
372 if (msg->data[0] == MGCP_DUMMY_LOAD)
373 return osmux_handle_dummy(cfg, &addr, msg);
374
375 rem = msg->len;
376 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
377 struct mgcp_endpoint *endp;
378
379 /* Yes, we use MGCP_DEST_BTS to locate the origin */
380 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
381 &addr.sin_addr, MGCP_DEST_BTS);
382 if (!endp) {
383 LOGP(DLMGCP, LOGL_ERROR,
384 "Cannot find an endpoint for circuit_id=%d\n",
385 osmuxh->circuit_id);
386 goto out;
387 }
388 endp->osmux.stats.octets += osmux_chunk_length(msg, rem);
389 endp->osmux.stats.chunks++;
390 rem = msg->len;
391
392 osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
393 osmux_tx_sched(&list, scheduled_tx_net_cb, endp);
394 }
395out:
396 msgb_free(msg);
397 return 0;
398}
399
400int osmux_init(int role, struct mgcp_config *cfg)
401{
402 int ret;
403
404 switch(role) {
405 case OSMUX_ROLE_BSC:
406 osmux_fd.cb = osmux_read_from_bsc_nat_cb;
407 break;
408 case OSMUX_ROLE_BSC_NAT:
409 osmux_fd.cb = osmux_read_from_bsc_cb;
410 break;
411 default:
412 LOGP(DLMGCP, LOGL_ERROR, "wrong role for OSMUX\n");
413 return -1;
414 }
415 osmux_fd.data = cfg;
416
417 ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port);
418 if (ret < 0) {
419 LOGP(DLMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n");
420 return ret;
421 }
422 mgcp_set_ip_tos(osmux_fd.fd, cfg->endp_dscp);
423 osmux_fd.when |= BSC_FD_READ;
424
425 ret = osmo_fd_register(&osmux_fd);
426 if (ret < 0) {
427 LOGP(DLMGCP, LOGL_ERROR, "cannot register OSMUX socket\n");
428 return ret;
429 }
430 cfg->osmux_init = 1;
431
432 return 0;
433}
434
435int osmux_enable_endpoint(struct mgcp_endpoint *endp, struct in_addr *addr, uint16_t port)
436{
437 /* If osmux is enabled, initialize the output handler. This handler is
438 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
439 * allocated based on the circuit ID (endp->osmux.cid), which is unique
440 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
441 * SSRC space (2^32) by the 256 possible circuit IDs, then randomly
442 * select one value from that window. Thus, we have no chance to have
443 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
444 * similarly, for flows traveling to the MSC.
445 */
446 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256;
447
448 if (endp->osmux.state == OSMUX_STATE_DISABLED) {
449 LOGP(DLMGCP, LOGL_ERROR, "Endpoint %u didn't request Osmux\n",
450 ENDPOINT_NUMBER(endp));
451 return -1;
452 }
453
454 osmux_xfrm_output_init(&endp->osmux.out,
455 (endp->osmux.cid * rtp_ssrc_winlen) +
456 (random() % rtp_ssrc_winlen));
457
458 endp->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
459 if (!endp->osmux.in) {
460 LOGP(DLMGCP, LOGL_ERROR, "Cannot allocate input osmux handle\n");
461 return -1;
462 }
463 if (!osmux_xfrm_input_open_circuit(endp->osmux.in, endp->osmux.cid,
464 endp->cfg->osmux_dummy)) {
465 LOGP(DLMGCP, LOGL_ERROR, "Cannot open osmux circuit %u\n",
466 endp->osmux.cid);
467 return -1;
468 }
469
470 switch (endp->cfg->role) {
471 case MGCP_BSC_NAT:
472 endp->type = MGCP_OSMUX_BSC_NAT;
473 break;
474 case MGCP_BSC:
475 endp->type = MGCP_OSMUX_BSC;
476 break;
477 }
478 endp->osmux.state = OSMUX_STATE_ENABLED;
479
480 return 0;
481}
482
483void osmux_disable_endpoint(struct mgcp_endpoint *endp)
484{
485 LOGP(DLMGCP, LOGL_INFO, "Releasing endpoint %u using Osmux CID %u\n",
486 ENDPOINT_NUMBER(endp), endp->osmux.cid);
487 osmux_xfrm_input_close_circuit(endp->osmux.in, endp->osmux.cid);
488 endp->osmux.state = OSMUX_STATE_DISABLED;
489 endp->osmux.cid = -1;
490 osmux_handle_put(endp->osmux.in);
491}
492
493void osmux_release_cid(struct mgcp_endpoint *endp)
494{
495 if (endp->osmux.allocated_cid >= 0)
496 osmux_put_cid(endp->osmux.allocated_cid);
497 endp->osmux.allocated_cid = -1;
498}
499
500void osmux_allocate_cid(struct mgcp_endpoint *endp)
501{
502 osmux_release_cid(endp);
503 endp->osmux.allocated_cid = osmux_get_cid();
504}
505
506/* We don't need to send the dummy load for osmux so often as another endpoint
507 * may have already punched the hole in the firewall. This approach is simple
508 * though.
509 */
510int osmux_send_dummy(struct mgcp_endpoint *endp)
511{
512 char buf[1 + sizeof(uint8_t)];
513 struct in_addr addr_unset = {};
514
515 buf[0] = MGCP_DUMMY_LOAD;
516 memcpy(&buf[1], &endp->osmux.cid, sizeof(endp->osmux.cid));
517
518 /* Wait until we have the connection information from MDCX */
519 if (memcmp(&endp->net_end.addr, &addr_unset, sizeof(addr_unset)) == 0)
520 return 0;
521
522 if (endp->osmux.state == OSMUX_STATE_ACTIVATING) {
523 if (osmux_enable_endpoint(endp, &endp->net_end.addr,
524 htons(endp->cfg->osmux_port)) < 0) {
525 LOGP(DLMGCP, LOGL_ERROR,
526 "Could not activate osmux in endpoint %d\n",
527 ENDPOINT_NUMBER(endp));
528 }
529 LOGP(DLMGCP, LOGL_ERROR,
530 "Osmux CID %u for %s:%u is now enabled\n",
531 endp->osmux.cid, inet_ntoa(endp->net_end.addr),
532 endp->cfg->osmux_port);
533 }
534 LOGP(DLMGCP, LOGL_DEBUG,
535 "sending OSMUX dummy load to %s CID %u\n",
536 inet_ntoa(endp->net_end.addr), endp->osmux.cid);
537
538 return mgcp_udp_send(osmux_fd.fd, &endp->net_end.addr,
539 htons(endp->cfg->osmux_port), buf, sizeof(buf));
540}
541
542/* bsc-nat allocates/releases the Osmux circuit ID */
543static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1) / 8];
544
545int osmux_used_cid(void)
546{
547 int i, j, used = 0;
548
549 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
550 for (j = 0; j < 8; j++) {
551 if (osmux_cid_bitmap[i] & (1 << j))
552 used += 1;
553 }
554 }
555
556 return used;
557}
558
559int osmux_get_cid(void)
560{
561 int i, j;
562
563 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
564 for (j = 0; j < 8; j++) {
565 if (osmux_cid_bitmap[i] & (1 << j))
566 continue;
567
568 osmux_cid_bitmap[i] |= (1 << j);
569 LOGP(DLMGCP, LOGL_DEBUG,
570 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
571 return (i * 8) + j;
572 }
573 }
574
575 LOGP(DLMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n");
576 return -1;
577}
578
579void osmux_put_cid(uint8_t osmux_cid)
580{
581 LOGP(DLMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
582 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
583}