blob: a1121de8fb8fca1c0e1b69691966916689e4ac0e [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>
Pau Espin Pedrolf027f172019-05-06 13:57:31 +020018#include <osmocom/core/socket.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020019#include <osmocom/core/talloc.h>
20
21#include <osmocom/netif/osmux.h>
22#include <osmocom/netif/rtp.h>
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +020023#include <osmocom/netif/amr.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020024
Philipp Maier87bd9be2017-08-22 16:35:41 +020025#include <osmocom/mgcp/mgcp.h>
26#include <osmocom/mgcp/mgcp_internal.h>
27#include <osmocom/mgcp/osmux.h>
28#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010029#include <osmocom/mgcp/mgcp_endp.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020030
31static struct osmo_fd osmux_fd;
32
33static LLIST_HEAD(osmux_handle_list);
34
35struct osmux_handle {
36 struct llist_head head;
37 struct osmux_in_handle *in;
38 struct in_addr rem_addr;
Pau Espin Pedrol12056862019-05-13 17:30:21 +020039 int rem_port; /* network byte order */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020040 int refcnt;
41};
42
43static void *osmux;
44
Philipp Maier87bd9be2017-08-22 16:35:41 +020045/* Deliver OSMUX batch to the remote end */
46static void osmux_deliver_cb(struct msgb *batch_msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020047{
48 struct osmux_handle *handle = data;
49 struct sockaddr_in out = {
50 .sin_family = AF_INET,
51 .sin_port = handle->rem_port,
52 };
53
54 memcpy(&out.sin_addr, &handle->rem_addr, sizeof(handle->rem_addr));
55 sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0,
56 (struct sockaddr *)&out, sizeof(out));
57 msgb_free(batch_msg);
58}
59
Philipp Maier87bd9be2017-08-22 16:35:41 +020060/* Lookup existing OSMUX handle for specified destination address. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020061static struct osmux_handle *
62osmux_handle_find_get(struct in_addr *addr, int rem_port)
63{
64 struct osmux_handle *h;
65
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020066 llist_for_each_entry(h, &osmux_handle_list, head) {
67 if (memcmp(&h->rem_addr, addr, sizeof(struct in_addr)) == 0 &&
68 h->rem_port == rem_port) {
69 LOGP(DLMGCP, LOGL_DEBUG, "using existing OSMUX handle "
70 "for addr=%s:%d\n",
71 inet_ntoa(*addr), ntohs(rem_port));
72 h->refcnt++;
73 return h;
74 }
75 }
76
77 return NULL;
78}
79
Philipp Maier87bd9be2017-08-22 16:35:41 +020080/* Put down no longer needed OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020081static void osmux_handle_put(struct osmux_in_handle *in)
82{
83 struct osmux_handle *h;
84
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020085 llist_for_each_entry(h, &osmux_handle_list, head) {
86 if (h->in == in) {
87 if (--h->refcnt == 0) {
88 LOGP(DLMGCP, LOGL_INFO,
89 "Releasing unused osmux handle for %s:%d\n",
90 inet_ntoa(h->rem_addr),
91 ntohs(h->rem_port));
92 LOGP(DLMGCP, LOGL_INFO, "Stats: "
93 "input RTP msgs: %u bytes: %"PRIu64" "
94 "output osmux msgs: %u bytes: %"PRIu64"\n",
95 in->stats.input_rtp_msgs,
96 in->stats.input_rtp_bytes,
97 in->stats.output_osmux_msgs,
98 in->stats.output_osmux_bytes);
99 llist_del(&h->head);
100 osmux_xfrm_input_fini(h->in);
101 talloc_free(h);
102 }
103 return;
104 }
105 }
106 LOGP(DLMGCP, LOGL_ERROR, "cannot find Osmux input handle %p\n", in);
107}
108
Philipp Maier87bd9be2017-08-22 16:35:41 +0200109/* Allocate free OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200110static struct osmux_handle *
111osmux_handle_alloc(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
112{
113 struct osmux_handle *h;
114
115 h = talloc_zero(osmux, struct osmux_handle);
116 if (!h)
117 return NULL;
118 h->rem_addr = *addr;
119 h->rem_port = rem_port;
120 h->refcnt++;
121
122 h->in = talloc_zero(h, struct osmux_in_handle);
123 if (!h->in) {
124 talloc_free(h);
125 return NULL;
126 }
127
Philipp Maier87bd9be2017-08-22 16:35:41 +0200128 /* sequence number to start OSMUX message from */
129 h->in->osmux_seq = 0;
130
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200131 h->in->batch_factor = cfg->osmux_batch;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200132
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200133 /* If batch size is zero, the library defaults to 1470 bytes. */
134 h->in->batch_size = cfg->osmux_batch_size;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200135 h->in->deliver = osmux_deliver_cb;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200136 osmux_xfrm_input_init(h->in);
137 h->in->data = h;
138
139 llist_add(&h->head, &osmux_handle_list);
140
141 LOGP(DLMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s:%d\n",
142 inet_ntoa(*addr), ntohs(rem_port));
143
144 return h;
145}
146
Philipp Maier87bd9be2017-08-22 16:35:41 +0200147/* Lookup existing handle for a specified address, if the handle can not be
Harald Welte1d1b98f2017-12-25 10:03:40 +0100148 * found, the function will automatically allocate one */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200149static struct osmux_in_handle *
150osmux_handle_lookup(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
151{
152 struct osmux_handle *h;
153
154 h = osmux_handle_find_get(addr, rem_port);
155 if (h != NULL)
156 return h->in;
157
158 h = osmux_handle_alloc(cfg, addr, rem_port);
159 if (h == NULL)
160 return NULL;
161
162 return h->in;
163}
164
Philipp Maier87bd9be2017-08-22 16:35:41 +0200165/*! send RTP packet through OSMUX connection.
166 * \param[in] buf rtp data
167 * \param[in] buf_len length of rtp data
168 * \param[in] conn associated RTP connection
169 * \returns 0 on success, -1 on ERROR */
170int osmux_xfrm_to_osmux(char *buf, int buf_len, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200171{
172 int ret;
173 struct msgb *msg;
174
175 msg = msgb_alloc(4096, "RTP");
176 if (!msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200177 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200178
Philipp Maier87bd9be2017-08-22 16:35:41 +0200179 memcpy(msg->data, buf, buf_len);
180 msgb_put(msg, buf_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200181
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200182 if (conn->osmux.state != OSMUX_STATE_ENABLED) {
183 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO, "forwarding RTP to Osmux conn not yet enabled, dropping (cid=%d)\n",
184 conn->osmux.cid);
185 return -1;
186 }
187
Philipp Maier87bd9be2017-08-22 16:35:41 +0200188 while ((ret = osmux_xfrm_input(conn->osmux.in, msg, conn->osmux.cid)) > 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200189 /* batch full, build and deliver it */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200190 osmux_xfrm_input_deliver(conn->osmux.in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200191 }
192 return 0;
193}
194
Philipp Maier87bd9be2017-08-22 16:35:41 +0200195/* Lookup the endpoint that corresponds to the specified address (port) */
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200196static struct mgcp_conn_rtp*
197osmux_conn_lookup(struct mgcp_config *cfg, uint8_t cid,
198 struct in_addr *from_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200199{
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200200 struct mgcp_endpoint *endp;
201 struct mgcp_conn *conn = NULL;
202 struct mgcp_conn_rtp * conn_rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200203 int i;
204
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200205 for (i=0; i<cfg->trunk.number_endpoints; i++) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200206
Philipp Maier87bd9be2017-08-22 16:35:41 +0200207 endp = &cfg->trunk.endpoints[i];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200208
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200209 llist_for_each_entry(conn, &endp->conns, entry) {
210 if (conn->type != MGCP_CONN_TYPE_RTP)
211 continue;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200212
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200213 conn_rtp = &conn->u.rtp;
214 if (!mgcp_conn_rtp_is_osmux(conn_rtp))
215 continue;
216
217 if (conn_rtp->osmux.cid == cid)
218 return conn_rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200219 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200220 }
221
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200222 LOGP(DLMGCP, LOGL_ERROR, "Cannot find osmux conn with cid=%d\n", cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200223
224 return NULL;
225}
226
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200227/* FIXME: this is declared and used in mgcp_network.c, but documentation of mgcp_dispatch_rtp_bridge_cb() states another enum is to be used */
228enum {
229 MGCP_PROTO_RTP,
230 MGCP_PROTO_RTCP,
231};
232
233static void scheduled_from_osmux_tx_rtp_cb(struct msgb *msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200234{
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200235 struct mgcp_conn_rtp *conn = data;
236 struct mgcp_endpoint *endp = conn->conn->endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200237 struct sockaddr_in addr = {
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200238 .sin_addr = conn->end.addr,
239 .sin_port = conn->end.rtp_port,
240 }; /* FIXME: not set/used in cb */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200241
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200242
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200243 endp->type->dispatch_rtp_cb(MGCP_PROTO_RTP, &addr, (char *)msg->data, msg->len, conn->conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200244 msgb_free(msg);
245}
246
247static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr)
248{
249 struct msgb *msg;
250 socklen_t slen = sizeof(*addr);
251 int ret;
252
253 msg = msgb_alloc(4096, "OSMUX");
254 if (!msg) {
255 LOGP(DLMGCP, LOGL_ERROR, "cannot allocate message\n");
256 return NULL;
257 }
258 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0,
259 (struct sockaddr *)addr, &slen);
260 if (ret <= 0) {
261 msgb_free(msg);
262 LOGP(DLMGCP, LOGL_ERROR, "cannot receive message\n");
263 return NULL;
264 }
265 msgb_put(msg, ret);
266
267 return msg;
268}
269
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200270/* Updates endp osmux state and returns 0 if it can process messages, -1 otherwise */
271static int endp_osmux_state_check(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
272 bool sending)
273{
274 switch(conn->osmux.state) {
275 case OSMUX_STATE_ACTIVATING:
Pau Espin Pedrol295570c2019-05-13 17:32:47 +0200276 if (osmux_enable_conn(endp, conn, &conn->end.addr, conn->end.rtp_port) < 0) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200277 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200278 "Could not enable osmux for conn on %s: %s\n",
279 sending ? "sent" : "received",
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200280 mgcp_conn_dump(conn->conn));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200281 return -1;
282 }
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200283 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200284 "Osmux %s CID %u towards %s:%u is now enabled\n",
285 sending ? "sent" : "received",
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200286 conn->osmux.cid, inet_ntoa(conn->end.addr),
Pau Espin Pedrol295570c2019-05-13 17:32:47 +0200287 ntohs(conn->end.rtp_port));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200288 return 0;
289 case OSMUX_STATE_ENABLED:
290 return 0;
291 default:
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200292 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
293 "Osmux %s in conn %s without full negotiation, state %d\n",
294 sending ? "sent" : "received",
295 mgcp_conn_dump(conn->conn), conn->osmux.state);
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200296 return -1;
297 }
298}
299
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200300static int osmux_legacy_dummy_parse_cid(struct sockaddr_in *addr, struct msgb *msg,
301 uint8_t *osmux_cid)
302{
Pau Espin Pedrolb5583cd2019-05-13 12:58:24 +0200303 if (msg->len < 1 + sizeof(*osmux_cid)) {
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200304 LOGP(DLMGCP, LOGL_ERROR,
Pau Espin Pedrolb5583cd2019-05-13 12:58:24 +0200305 "Discarding truncated Osmux dummy load: %s\n", osmo_hexdump(msg->data, msg->len));
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200306 return -1;
307 }
308
309 /* extract the osmux CID from the dummy message */
310 memcpy(osmux_cid, &msg->data[1], sizeof(*osmux_cid));
311 return 0;
312}
313
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200314/* This is called from the bsc-nat */
315static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr,
316 struct msgb *msg)
317{
318 uint8_t osmux_cid;
319 struct mgcp_conn_rtp *conn;
320
321 if (osmux_legacy_dummy_parse_cid(addr, msg, &osmux_cid) < 0)
322 goto out;
323
324 conn = osmux_conn_lookup(cfg, osmux_cid, &addr->sin_addr);
325 if (!conn) {
326 LOGP(DLMGCP, LOGL_ERROR,
327 "Cannot find conn for Osmux CID %d\n", osmux_cid);
328 goto out;
329 }
330
331 endp_osmux_state_check(conn->conn->endp, conn, false);
332 /* Only needed to punch hole in firewall, it can be dropped */
333out:
334 msgb_free(msg);
335 return 0;
336}
337
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200338#define osmux_chunk_length(msg, rem) (rem - msg->len);
339
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200340static int osmux_read_fd_cb(struct osmo_fd *ofd, unsigned int what)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200341{
342 struct msgb *msg;
343 struct osmux_hdr *osmuxh;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200344 struct sockaddr_in addr;
345 struct mgcp_config *cfg = ofd->data;
346 uint32_t rem;
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200347 struct mgcp_conn_rtp *conn_src;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200348
349 msg = osmux_recv(ofd, &addr);
350 if (!msg)
351 return -1;
352
Pau Espin Pedrold14163e2018-10-16 15:48:59 +0200353 if (!cfg->osmux) {
354 LOGP(DLMGCP, LOGL_ERROR,
355 "bsc-nat wants to use Osmux but bsc did not request it\n");
356 goto out;
357 }
358
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200359 /* not any further processing dummy messages */
360 if (msg->data[0] == MGCP_DUMMY_LOAD)
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200361 return osmux_handle_dummy(cfg, &addr, msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200362
363 rem = msg->len;
364 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200365
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200366 conn_src = osmux_conn_lookup(cfg, osmuxh->circuit_id,
367 &addr.sin_addr);
368 if (!conn_src) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200369 LOGP(DLMGCP, LOGL_ERROR,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200370 "Cannot find a src conn for circuit_id=%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200371 osmuxh->circuit_id);
372 goto out;
373 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200374
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200375 /*conn_dst = mgcp_find_dst_conn(conn_src->conn);
376 if (!conn_dst) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200377 LOGP(DLMGCP, LOGL_ERROR,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200378 "Cannot find a dst conn for circuit_id=%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200379 osmuxh->circuit_id);
380 goto out;
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200381 }*/
382
383 if (endp_osmux_state_check(conn_src->conn->endp, conn_src, false) == 0) {
384 conn_src->osmux.stats.octets += osmux_chunk_length(msg, rem);
385 conn_src->osmux.stats.chunks++;
386 osmux_xfrm_output_sched(&conn_src->osmux.out, osmuxh);
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200387 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200388 rem = msg->len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200389 }
390out:
391 msgb_free(msg);
392 return 0;
393}
394
395int osmux_init(int role, struct mgcp_config *cfg)
396{
397 int ret;
398
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200399 osmux_fd.cb = osmux_read_fd_cb;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200400 osmux_fd.data = cfg;
401
402 ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port);
403 if (ret < 0) {
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200404 LOGP(DLMGCP, LOGL_ERROR, "cannot bind OSMUX socket to %s:%u\n",
405 cfg->osmux_addr, cfg->osmux_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200406 return ret;
407 }
408 mgcp_set_ip_tos(osmux_fd.fd, cfg->endp_dscp);
Pau Espin Pedrola7152e02020-05-09 19:15:50 +0200409 osmux_fd.when |= OSMO_FD_READ;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200410
411 ret = osmo_fd_register(&osmux_fd);
412 if (ret < 0) {
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200413 LOGP(DLMGCP, LOGL_ERROR, "cannot register OSMUX socket %s\n",
414 osmo_sock_get_name2(osmux_fd.fd));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200415 return ret;
416 }
417 cfg->osmux_init = 1;
418
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200419 LOGP(DLMGCP, LOGL_INFO, "OSMUX socket listening on %s\n",
420 osmo_sock_get_name2(osmux_fd.fd));
421
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200422 return 0;
423}
424
Philipp Maier87bd9be2017-08-22 16:35:41 +0200425/*! enable OSXMUX circuit for a specified connection.
426 * \param[in] endp mgcp endpoint (configuration)
427 * \param[in] conn connection to disable
428 * \param[in] addr IP address of remote OSMUX endpoint
Pau Espin Pedrol12056862019-05-13 17:30:21 +0200429 * \param[in] port portnumber of the remote OSMUX endpoint (in network byte order)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200430 * \returns 0 on success, -1 on ERROR */
431int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
432 struct in_addr *addr, uint16_t port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200433{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200434 /*! If osmux is enabled, initialize the output handler. This handler is
435 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
436 * allocated based on the circuit ID (conn_net->osmux.cid), which is unique
437 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200438 * SSRC space (2^32) by the OSMUX_CID_MAX + 1 possible circuit IDs, then randomly
Philipp Maier87bd9be2017-08-22 16:35:41 +0200439 * select one value from that window. Thus, we have no chance to have
440 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
441 * similarly, for flows traveling to the MSC.
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200442 */
Pau Espin Pedroldac2ca72019-05-13 17:34:51 +0200443 struct in_addr addr_unset = {};
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200444 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / (OSMUX_CID_MAX + 1);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200445 uint16_t osmux_dummy = endp->cfg->osmux_dummy;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200446
Philipp Maier87bd9be2017-08-22 16:35:41 +0200447 /* Check if osmux is enabled for the specified connection */
Pau Espin Pedrol48aff622018-10-16 16:03:57 +0200448 if (conn->osmux.state != OSMUX_STATE_ACTIVATING) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200449 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
450 "conn:%s didn't negotiate Osmux, state %d\n",
451 mgcp_conn_dump(conn->conn), conn->osmux.state);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200452 return -1;
453 }
454
Pau Espin Pedroldac2ca72019-05-13 17:34:51 +0200455 /* Wait until we have the connection information from MDCX */
456 if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0) {
457 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO,
458 "Osmux remote address/port still unknown\n");
459 return -1;
460 }
461
Philipp Maier87bd9be2017-08-22 16:35:41 +0200462 conn->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
463 if (!conn->osmux.in) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200464 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
465 "Cannot allocate input osmux handle for conn:%s\n",
466 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200467 return -1;
468 }
Pau Espin Pedrolac772d82019-05-06 18:02:00 +0200469 if (osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.cid, osmux_dummy) < 0) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200470 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
471 "Cannot open osmux circuit %u for conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200472 conn->osmux.cid, mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200473 return -1;
474 }
475
Pau Espin Pedroldf7d97e2019-05-17 17:14:06 +0200476 osmux_xfrm_output_init2(&conn->osmux.out,
Pau Espin Pedrol426a9d92018-10-16 15:31:45 +0200477 (conn->osmux.cid * rtp_ssrc_winlen) +
Pau Espin Pedroldf7d97e2019-05-17 17:14:06 +0200478 (random() % rtp_ssrc_winlen),
479 conn->end.codec->payload_type);
Pau Espin Pedrol426a9d92018-10-16 15:31:45 +0200480
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200481 osmux_xfrm_output_set_tx_cb(&conn->osmux.out,
482 scheduled_from_osmux_tx_rtp_cb, conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200483
484 conn->osmux.state = OSMUX_STATE_ENABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200485
486 return 0;
487}
488
Philipp Maier87bd9be2017-08-22 16:35:41 +0200489/*! disable OSXMUX circuit for a specified connection.
490 * \param[in] conn connection to disable */
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200491void conn_osmux_disable(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200492{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200493
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200494 OSMO_ASSERT(conn->osmux.state != OSMUX_STATE_DISABLED);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200495
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200496 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO,
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200497 "Releasing connection using Osmux CID %u\n", conn->osmux.cid);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200498
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200499 if (conn->osmux.state == OSMUX_STATE_ENABLED) {
500 /* We are closing, we don't need pending RTP packets to be transmitted */
501 osmux_xfrm_output_set_tx_cb(&conn->osmux.out, NULL, NULL);
502 osmux_xfrm_output_flush(&conn->osmux.out);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200503
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200504 osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.cid);
505 conn->osmux.state = OSMUX_STATE_DISABLED;
506 conn_osmux_release_cid(conn);
507 osmux_handle_put(conn->osmux.in);
508 }
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200509 conn_osmux_release_cid(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200510}
511
Philipp Maier87bd9be2017-08-22 16:35:41 +0200512/*! relase OSXMUX cid, that had been allocated to this connection.
513 * \param[in] conn connection with OSMUX cid to release */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200514void conn_osmux_release_cid(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200515{
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200516 if (conn->osmux.cid_allocated)
517 osmux_cid_pool_put(conn->osmux.cid);
518 conn->osmux.cid = 0;
519 conn->osmux.cid_allocated = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200520}
521
Philipp Maier87bd9be2017-08-22 16:35:41 +0200522/*! allocate OSXMUX cid to connection.
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200523 * \param[in] conn connection for which we allocate the OSMUX cid
524 * \param[in] osmux_cid OSMUX cid to allocate. -1 Means take next available one.
525 * \returns Allocated OSMUX cid, -1 on error (no free cids avail, or selected one is already taken).
526 */
527int conn_osmux_allocate_cid(struct mgcp_conn_rtp *conn, int osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200528{
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200529 if (osmux_cid != -1 && osmux_cid_pool_allocated((uint8_t) osmux_cid)) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200530 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO,
531 "Osmux CID %d already allocated!\n",
532 osmux_cid);
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200533 return -1;
534 }
535
536 if (osmux_cid == -1) {
537 osmux_cid = osmux_cid_pool_get_next();
538 if (osmux_cid == -1) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200539 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO,
540 "no available Osmux CID to allocate!\n");
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200541 return -1;
542 }
543 } else
544 osmux_cid_pool_get(osmux_cid);
545
546 conn->osmux.cid = (uint8_t) osmux_cid;
547 conn->osmux.cid_allocated = true;
Pau Espin Pedrolfa810e82019-05-06 18:54:10 +0200548 conn->type = MGCP_OSMUX_BSC;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200549 return osmux_cid;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200550}
551
Philipp Maier87bd9be2017-08-22 16:35:41 +0200552/*! send RTP dummy packet to OSMUX connection port.
553 * \param[in] endp mcgp endpoint that holds the RTP connection
554 * \param[in] conn associated RTP connection
555 * \returns bytes sent, -1 on error */
556int osmux_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200557{
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200558 struct osmux_hdr *osmuxh;
559 int buf_len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200560 struct in_addr addr_unset = {};
561
Philipp Maier87bd9be2017-08-22 16:35:41 +0200562 /*! The dummy packet will not be sent via the actual OSMUX connection,
563 * instead it is sent out of band to port where the remote OSMUX
564 * multplexer is listening. The goal is to ensure that the connection
565 * is kept open */
566
567 /*! We don't need to send the dummy load for osmux so often as another
568 * endpoint may have already punched the hole in the firewall. This
569 * approach is simple though. */
570
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200571 /* Wait until we have the connection information from MDCX */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200572 if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200573 return 0;
574
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200575 if (endp_osmux_state_check(endp, conn, true) < 0)
576 return 0;
577
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200578 buf_len = sizeof(struct osmux_hdr) + osmo_amr_bytes(AMR_FT_0);
579 osmuxh = (struct osmux_hdr *) alloca(buf_len);
580 memset(osmuxh, 0, buf_len);
581 osmuxh->ft = OSMUX_FT_DUMMY;
582 osmuxh->amr_ft = AMR_FT_0;
583 osmuxh->circuit_id = conn->osmux.cid;
584
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200585 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200586 "sending OSMUX dummy load to %s:%u CID %u\n",
587 inet_ntoa(conn->end.addr), ntohs(conn->end.rtp_port), conn->osmux.cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200588
Philipp Maier87bd9be2017-08-22 16:35:41 +0200589 return mgcp_udp_send(osmux_fd.fd, &conn->end.addr,
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200590 conn->end.rtp_port, (char*)osmuxh, buf_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200591}
592
Pau Espin Pedrolbcd52e52018-09-17 12:41:28 +0200593/* bsc-nat allocates/releases the Osmux circuit ID. +7 to round up to 8 bit boundary. */
594static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1 + 7) / 8];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200595
Philipp Maier87bd9be2017-08-22 16:35:41 +0200596/*! count the number of taken OSMUX cids.
597 * \returns number of OSMUX cids in use */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200598int osmux_cid_pool_count_used(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200599{
600 int i, j, used = 0;
601
602 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
603 for (j = 0; j < 8; j++) {
604 if (osmux_cid_bitmap[i] & (1 << j))
605 used += 1;
606 }
607 }
608
609 return used;
610}
611
Philipp Maier87bd9be2017-08-22 16:35:41 +0200612/*! take a free OSMUX cid.
613 * \returns OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200614int osmux_cid_pool_get_next(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200615{
616 int i, j;
617
618 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
619 for (j = 0; j < 8; j++) {
620 if (osmux_cid_bitmap[i] & (1 << j))
621 continue;
622
623 osmux_cid_bitmap[i] |= (1 << j);
624 LOGP(DLMGCP, LOGL_DEBUG,
625 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
626 return (i * 8) + j;
627 }
628 }
629
630 LOGP(DLMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n");
631 return -1;
632}
633
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200634/*! take a specific OSMUX cid.
635 * \param[in] osmux_cid OSMUX cid */
636void osmux_cid_pool_get(uint8_t osmux_cid)
637{
638 LOGP(DLMGCP, LOGL_DEBUG, "Allocating Osmux CID %u from pool\n", osmux_cid);
639 osmux_cid_bitmap[osmux_cid / 8] |= (1 << (osmux_cid % 8));
640}
641
Philipp Maier87bd9be2017-08-22 16:35:41 +0200642/*! put back a no longer used OSMUX cid.
643 * \param[in] osmux_cid OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200644void osmux_cid_pool_put(uint8_t osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200645{
646 LOGP(DLMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
647 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
648}
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200649
650/*! check if OSMUX cid is already taken */
651bool osmux_cid_pool_allocated(uint8_t osmux_cid)
652{
653 return !!(osmux_cid_bitmap[osmux_cid / 8] & (1 << (osmux_cid % 8)));
654}