blob: 81de5fd2236c27e99e655c7e6fc500f2d5791a1b [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
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +020025#include <osmocom/mgcp/debug.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020026#include <osmocom/mgcp/mgcp.h>
Philipp Maier993ea6b2020-08-04 18:26:50 +020027#include <osmocom/mgcp/mgcp_protocol.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020028#include <osmocom/mgcp/osmux.h>
29#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010030#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maierc66ab2c2020-06-02 20:55:34 +020031#include <osmocom/mgcp/mgcp_trunk.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020032
33static struct osmo_fd osmux_fd;
34
35static LLIST_HEAD(osmux_handle_list);
36
37struct osmux_handle {
38 struct llist_head head;
39 struct osmux_in_handle *in;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +020040 struct osmo_sockaddr rem_addr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020041 int refcnt;
42};
43
44static void *osmux;
45
Philipp Maier87bd9be2017-08-22 16:35:41 +020046/* Deliver OSMUX batch to the remote end */
47static void osmux_deliver_cb(struct msgb *batch_msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020048{
49 struct osmux_handle *handle = data;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +020050 socklen_t dest_len;
Pau Espin Pedrol4d1a52d2022-09-22 17:11:57 +020051 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020052
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +020053 switch (handle->rem_addr.u.sa.sa_family) {
54 case AF_INET6:
55 dest_len = sizeof(handle->rem_addr.u.sin6);
56 break;
57 case AF_INET:
58 default:
59 dest_len = sizeof(handle->rem_addr.u.sin);
60 break;
61 }
Pau Espin Pedrol4d1a52d2022-09-22 17:11:57 +020062 rc = sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0,
63 (struct sockaddr *)&handle->rem_addr.u.sa, dest_len);
64 if (rc < 0) {
65 char errbuf[129];
66 strerror_r(errno, errbuf, sizeof(errbuf));
67 LOGP(DOSMUX, LOGL_NOTICE, "osmux sendto(%s) failed: %s\n",
68 osmo_sockaddr_to_str(&handle->rem_addr), errbuf);
69 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020070 msgb_free(batch_msg);
71}
72
Philipp Maier87bd9be2017-08-22 16:35:41 +020073/* Lookup existing OSMUX handle for specified destination address. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020074static struct osmux_handle *
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +020075osmux_handle_find_get(const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020076{
77 struct osmux_handle *h;
78
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020079 llist_for_each_entry(h, &osmux_handle_list, head) {
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +020080 if (osmo_sockaddr_cmp(&h->rem_addr, rem_addr) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020081 h->refcnt++;
Pau Espin Pedrolc61664c2022-09-09 17:03:13 +020082 LOGP(DOSMUX, LOGL_DEBUG,
83 "Using existing OSMUX handle for addr=%s (rfcnt=%u)\n",
84 osmo_sockaddr_to_str(rem_addr), h->refcnt);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020085 return h;
86 }
87 }
88
89 return NULL;
90}
91
Philipp Maier87bd9be2017-08-22 16:35:41 +020092/* Put down no longer needed OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020093static void osmux_handle_put(struct osmux_in_handle *in)
94{
95 struct osmux_handle *h;
96
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020097 llist_for_each_entry(h, &osmux_handle_list, head) {
98 if (h->in == in) {
Pau Espin Pedrolc61664c2022-09-09 17:03:13 +020099 LOGP(DOSMUX, LOGL_DEBUG,
100 "Putting existing OSMUX handle for addr=%s (rfcnt=%u)\n",
101 osmo_sockaddr_to_str(&h->rem_addr), h->refcnt);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200102 if (--h->refcnt == 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200103 LOGP(DOSMUX, LOGL_INFO,
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200104 "Releasing unused osmux handle for %s\n",
105 osmo_sockaddr_to_str(&h->rem_addr));
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200106 LOGP(DOSMUX, LOGL_INFO, "Stats: "
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200107 "input RTP msgs: %u bytes: %"PRIu64" "
108 "output osmux msgs: %u bytes: %"PRIu64"\n",
109 in->stats.input_rtp_msgs,
110 in->stats.input_rtp_bytes,
111 in->stats.output_osmux_msgs,
112 in->stats.output_osmux_bytes);
113 llist_del(&h->head);
114 osmux_xfrm_input_fini(h->in);
115 talloc_free(h);
116 }
117 return;
118 }
119 }
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200120 LOGP(DOSMUX, LOGL_ERROR, "Cannot find Osmux input handle %p\n", in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200121}
122
Philipp Maier87bd9be2017-08-22 16:35:41 +0200123/* Allocate free OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200124static struct osmux_handle *
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200125osmux_handle_alloc(struct mgcp_conn_rtp *conn, const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200126{
127 struct osmux_handle *h;
Ericfbf78d12021-08-23 22:31:39 +0200128 struct mgcp_config *cfg = conn->conn->endp->trunk->cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200129
130 h = talloc_zero(osmux, struct osmux_handle);
131 if (!h)
132 return NULL;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200133 h->rem_addr = *rem_addr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200134 h->refcnt++;
135
136 h->in = talloc_zero(h, struct osmux_in_handle);
137 if (!h->in) {
138 talloc_free(h);
139 return NULL;
140 }
141
Philipp Maier87bd9be2017-08-22 16:35:41 +0200142 /* sequence number to start OSMUX message from */
143 h->in->osmux_seq = 0;
144
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200145 h->in->batch_factor = cfg->osmux_batch;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200146
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200147 /* If batch size is zero, the library defaults to 1470 bytes. */
148 h->in->batch_size = cfg->osmux_batch_size;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200149 h->in->deliver = osmux_deliver_cb;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200150 osmux_xfrm_input_init(h->in);
151 h->in->data = h;
152
153 llist_add(&h->head, &osmux_handle_list);
154
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200155 LOGP(DOSMUX, LOGL_DEBUG, "Created new OSMUX handle for rem_addr=%s\n",
156 osmo_sockaddr_to_str(rem_addr));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200157
158 return h;
159}
160
Philipp Maier87bd9be2017-08-22 16:35:41 +0200161/* Lookup existing handle for a specified address, if the handle can not be
Harald Welte1d1b98f2017-12-25 10:03:40 +0100162 * found, the function will automatically allocate one */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200163static struct osmux_in_handle *
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200164osmux_handle_find_or_create(struct mgcp_conn_rtp *conn, const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200165{
166 struct osmux_handle *h;
167
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200168 if (rem_addr->u.sa.sa_family != AF_INET) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200169 LOGP(DOSMUX, LOGL_DEBUG, "IPv6 not supported in osmux yet!\n");
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200170 return NULL;
171 }
172
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200173 h = osmux_handle_find_get(rem_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200174 if (h != NULL)
175 return h->in;
176
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200177 h = osmux_handle_alloc(conn, rem_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200178 if (h == NULL)
179 return NULL;
180
181 return h->in;
182}
183
Philipp Maier87bd9be2017-08-22 16:35:41 +0200184/*! send RTP packet through OSMUX connection.
185 * \param[in] buf rtp data
186 * \param[in] buf_len length of rtp data
187 * \param[in] conn associated RTP connection
188 * \returns 0 on success, -1 on ERROR */
189int osmux_xfrm_to_osmux(char *buf, int buf_len, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200190{
191 int ret;
192 struct msgb *msg;
193
Pau Espin Pedrole39ae872022-09-09 17:36:33 +0200194 if (!conn->end.output_enabled)
195 return -1;
196
Pau Espin Pedrol8b0a6142022-09-09 17:34:31 +0200197 if (conn->osmux.state != OSMUX_STATE_ENABLED) {
198 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO, "forwarding RTP to Osmux conn not yet enabled, dropping (cid=%d)\n",
199 conn->osmux.cid);
200 return -1;
201 }
202
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200203 msg = msgb_alloc(4096, "RTP");
204 if (!msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200205 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200206
Philipp Maier87bd9be2017-08-22 16:35:41 +0200207 memcpy(msg->data, buf, buf_len);
208 msgb_put(msg, buf_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200209
Philipp Maier87bd9be2017-08-22 16:35:41 +0200210 while ((ret = osmux_xfrm_input(conn->osmux.in, msg, conn->osmux.cid)) > 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200211 /* batch full, build and deliver it */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200212 osmux_xfrm_input_deliver(conn->osmux.in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200213 }
214 return 0;
215}
216
Philipp Maier87bd9be2017-08-22 16:35:41 +0200217/* Lookup the endpoint that corresponds to the specified address (port) */
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200218static struct mgcp_conn_rtp*
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200219osmux_conn_lookup(struct mgcp_trunk *trunk, uint8_t cid, const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200220{
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200221 struct mgcp_endpoint *endp;
222 struct mgcp_conn *conn = NULL;
223 struct mgcp_conn_rtp * conn_rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200224 int i;
225
Philipp Maierd19de2e2020-06-03 13:55:33 +0200226 for (i = 0; i < trunk->number_endpoints; i++) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200227
Philipp Maierd19de2e2020-06-03 13:55:33 +0200228 endp = trunk->endpoints[i];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200229
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200230 llist_for_each_entry(conn, &endp->conns, entry) {
231 if (conn->type != MGCP_CONN_TYPE_RTP)
232 continue;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200233
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200234 conn_rtp = &conn->u.rtp;
235 if (!mgcp_conn_rtp_is_osmux(conn_rtp))
236 continue;
237
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200238 /* FIXME: Match remote address! */
239
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200240 if (conn_rtp->osmux.cid == cid)
241 return conn_rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200242 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200243 }
244
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200245 LOGP(DOSMUX, LOGL_ERROR, "Cannot find osmux conn with rem_addr=%s cid=%d\n",
246 osmo_sockaddr_to_str(rem_addr), cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200247
248 return NULL;
249}
250
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200251/* 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 */
252enum {
253 MGCP_PROTO_RTP,
254 MGCP_PROTO_RTCP,
255};
256
257static void scheduled_from_osmux_tx_rtp_cb(struct msgb *msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200258{
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200259 struct mgcp_conn_rtp *conn = data;
260 struct mgcp_endpoint *endp = conn->conn->endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200261 struct osmo_sockaddr addr = { /* FIXME: do we know the source address?? */ };
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200262 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
263 *mc = (struct osmo_rtp_msg_ctx){
264 .proto = MGCP_PROTO_RTP,
265 .conn_src = conn,
266 .from_addr = &addr,
267 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200268
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200269 endp->type->dispatch_rtp_cb(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200270 msgb_free(msg);
271}
272
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200273static struct msgb *osmux_recv(struct osmo_fd *ofd, struct osmo_sockaddr *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200274{
275 struct msgb *msg;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200276 socklen_t slen = sizeof(addr->u.sas);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200277 int ret;
278
279 msg = msgb_alloc(4096, "OSMUX");
280 if (!msg) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200281 LOGP(DOSMUX, LOGL_ERROR, "cannot allocate message\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200282 return NULL;
283 }
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200284 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0, &addr->u.sa, &slen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200285 if (ret <= 0) {
286 msgb_free(msg);
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200287 LOGP(DOSMUX, LOGL_ERROR, "cannot receive message\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200288 return NULL;
289 }
290 msgb_put(msg, ret);
291
292 return msg;
293}
294
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200295/* Updates endp osmux state and returns 0 if it can process messages, -1 otherwise */
296static int endp_osmux_state_check(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
297 bool sending)
298{
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200299 struct osmo_sockaddr rem_addr;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200300
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200301 switch(conn->osmux.state) {
302 case OSMUX_STATE_ACTIVATING:
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200303 rem_addr = conn->end.addr;
304 osmo_sockaddr_set_port(&rem_addr.u.sa, ntohs(conn->end.rtp_port));
305 if (osmux_enable_conn(endp, conn, &rem_addr) < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200306 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200307 "Could not enable osmux for conn on %s: %s\n",
308 sending ? "sent" : "received",
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200309 mgcp_conn_dump(conn->conn));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200310 return -1;
311 }
Pau Espin Pedrolc5bbb782022-09-19 16:46:02 +0200312 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200313 "Osmux %s CID %u towards %s is now enabled\n",
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200314 sending ? "sent" : "received",
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200315 conn->osmux.cid, osmo_sockaddr_to_str(&rem_addr));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200316 return 0;
317 case OSMUX_STATE_ENABLED:
318 return 0;
319 default:
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200320 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200321 "Osmux %s in conn %s without full negotiation, state %d\n",
322 sending ? "sent" : "received",
323 mgcp_conn_dump(conn->conn), conn->osmux.state);
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200324 return -1;
325 }
326}
327
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200328static int osmux_legacy_dummy_parse_cid(const struct osmo_sockaddr *rem_addr, struct msgb *msg,
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200329 uint8_t *osmux_cid)
330{
Pau Espin Pedrolb5583cd2019-05-13 12:58:24 +0200331 if (msg->len < 1 + sizeof(*osmux_cid)) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200332 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrolb5583cd2019-05-13 12:58:24 +0200333 "Discarding truncated Osmux dummy load: %s\n", osmo_hexdump(msg->data, msg->len));
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200334 return -1;
335 }
336
337 /* extract the osmux CID from the dummy message */
338 memcpy(osmux_cid, &msg->data[1], sizeof(*osmux_cid));
339 return 0;
340}
341
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200342/* This is called from the bsc-nat */
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200343static int osmux_handle_dummy(struct mgcp_trunk *trunk, const struct osmo_sockaddr *rem_addr,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200344 struct msgb *msg)
345{
346 uint8_t osmux_cid;
347 struct mgcp_conn_rtp *conn;
348
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200349 if (osmux_legacy_dummy_parse_cid(rem_addr, msg, &osmux_cid) < 0)
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200350 goto out;
351
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200352 conn = osmux_conn_lookup(trunk, osmux_cid, rem_addr);
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200353 if (!conn) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200354 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200355 "Cannot find conn for Osmux CID %d\n", osmux_cid);
356 goto out;
357 }
358
359 endp_osmux_state_check(conn->conn->endp, conn, false);
360 /* Only needed to punch hole in firewall, it can be dropped */
361out:
362 msgb_free(msg);
363 return 0;
364}
365
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200366#define osmux_chunk_length(msg, rem) (rem - msg->len);
367
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200368static int osmux_read_fd_cb(struct osmo_fd *ofd, unsigned int what)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200369{
370 struct msgb *msg;
371 struct osmux_hdr *osmuxh;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200372 struct osmo_sockaddr rem_addr;
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200373 struct mgcp_trunk *trunk = ofd->data;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200374 uint32_t rem;
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200375 struct mgcp_conn_rtp *conn_src;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200376
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200377 msg = osmux_recv(ofd, &rem_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200378 if (!msg)
379 return -1;
380
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200381 if (!trunk->cfg->osmux) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200382 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrold14163e2018-10-16 15:48:59 +0200383 "bsc-nat wants to use Osmux but bsc did not request it\n");
384 goto out;
385 }
386
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200387 /* not any further processing dummy messages */
Philipp Maierb3d14eb2021-05-20 14:18:52 +0200388 if (mgcp_is_rtp_dummy_payload(msg))
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200389 return osmux_handle_dummy(trunk, &rem_addr, msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200390
391 rem = msg->len;
392 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200393
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200394 conn_src = osmux_conn_lookup(trunk, osmuxh->circuit_id,
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200395 &rem_addr);
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200396 if (!conn_src) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200397 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200398 "Cannot find a src conn for circuit_id=%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200399 osmuxh->circuit_id);
400 goto out;
401 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200402
Pau Espin Pedrolde133112020-09-08 17:51:36 +0200403 mgcp_conn_watchdog_kick(conn_src->conn);
404
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200405 /*conn_dst = mgcp_find_dst_conn(conn_src->conn);
406 if (!conn_dst) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200407 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200408 "Cannot find a dst conn for circuit_id=%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200409 osmuxh->circuit_id);
410 goto out;
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200411 }*/
412
413 if (endp_osmux_state_check(conn_src->conn->endp, conn_src, false) == 0) {
414 conn_src->osmux.stats.octets += osmux_chunk_length(msg, rem);
415 conn_src->osmux.stats.chunks++;
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200416 osmux_xfrm_output_sched(conn_src->osmux.out, osmuxh);
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200417 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200418 rem = msg->len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200419 }
420out:
421 msgb_free(msg);
422 return 0;
423}
424
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200425int osmux_init(int role, struct mgcp_trunk *trunk)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200426{
427 int ret;
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200428 struct mgcp_config *cfg = trunk->cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200429
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200430 /* So far we only support running on one trunk: */
431 OSMO_ASSERT(trunk == mgcp_trunk_by_num(cfg, MGCP_TRUNK_VIRTUAL, MGCP_VIRT_TRUNK_ID));
432
433 osmo_fd_setup(&osmux_fd, -1, OSMO_FD_READ, osmux_read_fd_cb, trunk, 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200434
Harald Welte55a92292021-04-28 19:06:34 +0200435 ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port,
436 cfg->endp_dscp, cfg->endp_priority);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200437 if (ret < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200438 LOGP(DOSMUX, LOGL_ERROR, "cannot bind OSMUX socket to %s:%u\n",
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200439 cfg->osmux_addr, cfg->osmux_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200440 return ret;
441 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200442
443 ret = osmo_fd_register(&osmux_fd);
444 if (ret < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200445 LOGP(DOSMUX, LOGL_ERROR, "cannot register OSMUX socket %s\n",
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200446 osmo_sock_get_name2(osmux_fd.fd));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200447 return ret;
448 }
449 cfg->osmux_init = 1;
450
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200451 LOGP(DOSMUX, LOGL_INFO, "OSMUX socket listening on %s\n",
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200452 osmo_sock_get_name2(osmux_fd.fd));
453
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200454 return 0;
455}
456
Philipp Maier87bd9be2017-08-22 16:35:41 +0200457/*! enable OSXMUX circuit for a specified connection.
458 * \param[in] endp mgcp endpoint (configuration)
459 * \param[in] conn connection to disable
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200460 * \param[in] addr IP address and port of remote OSMUX endpoint
Philipp Maier87bd9be2017-08-22 16:35:41 +0200461 * \returns 0 on success, -1 on ERROR */
462int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200463 const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200464{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200465 /*! If osmux is enabled, initialize the output handler. This handler is
466 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
467 * allocated based on the circuit ID (conn_net->osmux.cid), which is unique
468 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200469 * SSRC space (2^32) by the OSMUX_CID_MAX + 1 possible circuit IDs, then randomly
Philipp Maier87bd9be2017-08-22 16:35:41 +0200470 * select one value from that window. Thus, we have no chance to have
471 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
472 * similarly, for flows traveling to the MSC.
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200473 */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200474 struct in6_addr addr_unset = {};
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200475 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / (OSMUX_CID_MAX + 1);
Ericfbf78d12021-08-23 22:31:39 +0200476 uint16_t osmux_dummy = endp->trunk->cfg->osmux_dummy;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200477
Philipp Maier87bd9be2017-08-22 16:35:41 +0200478 /* Check if osmux is enabled for the specified connection */
Pau Espin Pedrol48aff622018-10-16 16:03:57 +0200479 if (conn->osmux.state != OSMUX_STATE_ACTIVATING) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200480 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200481 "conn:%s didn't negotiate Osmux, state %d\n",
482 mgcp_conn_dump(conn->conn), conn->osmux.state);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200483 return -1;
484 }
485
Pau Espin Pedroldac2ca72019-05-13 17:34:51 +0200486 /* Wait until we have the connection information from MDCX */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200487 if (memcmp(&conn->end.addr, &addr_unset,
488 conn->end.addr.u.sa.sa_family == AF_INET6 ?
489 sizeof(struct in6_addr) :
490 sizeof(struct in_addr)) == 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200491 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedroldac2ca72019-05-13 17:34:51 +0200492 "Osmux remote address/port still unknown\n");
493 return -1;
494 }
495
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200496 conn->osmux.in = osmux_handle_find_or_create(conn, rem_addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200497 if (!conn->osmux.in) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200498 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200499 "Cannot allocate input osmux handle for conn:%s\n",
500 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200501 return -1;
502 }
Pau Espin Pedrolac772d82019-05-06 18:02:00 +0200503 if (osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.cid, osmux_dummy) < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200504 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200505 "Cannot open osmux circuit %u for conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200506 conn->osmux.cid, mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200507 return -1;
508 }
509
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200510 conn->osmux.out = osmux_xfrm_output_alloc(osmux);
511 osmux_xfrm_output_set_rtp_ssrc(conn->osmux.out,
512 (conn->osmux.cid * rtp_ssrc_winlen) +
513 (random() % rtp_ssrc_winlen));
514 osmux_xfrm_output_set_rtp_pl_type(conn->osmux.out, conn->end.codec->payload_type);
515 osmux_xfrm_output_set_tx_cb(conn->osmux.out,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200516 scheduled_from_osmux_tx_rtp_cb, conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200517
518 conn->osmux.state = OSMUX_STATE_ENABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200519
520 return 0;
521}
522
Philipp Maier87bd9be2017-08-22 16:35:41 +0200523/*! disable OSXMUX circuit for a specified connection.
524 * \param[in] conn connection to disable */
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200525void conn_osmux_disable(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200526{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200527
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200528 OSMO_ASSERT(conn->osmux.state != OSMUX_STATE_DISABLED);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200529
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200530 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200531 "Releasing connection using Osmux CID %u\n", conn->osmux.cid);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200532
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200533 if (conn->osmux.state == OSMUX_STATE_ENABLED) {
534 /* We are closing, we don't need pending RTP packets to be transmitted */
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200535 osmux_xfrm_output_set_tx_cb(conn->osmux.out, NULL, NULL);
536 TALLOC_FREE(conn->osmux.out);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200537
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200538 osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.cid);
539 conn->osmux.state = OSMUX_STATE_DISABLED;
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200540 osmux_handle_put(conn->osmux.in);
541 }
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200542 conn_osmux_release_cid(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200543}
544
Philipp Maier87bd9be2017-08-22 16:35:41 +0200545/*! relase OSXMUX cid, that had been allocated to this connection.
546 * \param[in] conn connection with OSMUX cid to release */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200547void conn_osmux_release_cid(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200548{
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200549 if (conn->osmux.cid_allocated)
550 osmux_cid_pool_put(conn->osmux.cid);
551 conn->osmux.cid = 0;
552 conn->osmux.cid_allocated = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200553}
554
Philipp Maier87bd9be2017-08-22 16:35:41 +0200555/*! allocate OSXMUX cid to connection.
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200556 * \param[in] conn connection for which we allocate the OSMUX cid
557 * \param[in] osmux_cid OSMUX cid to allocate. -1 Means take next available one.
558 * \returns Allocated OSMUX cid, -1 on error (no free cids avail, or selected one is already taken).
559 */
560int conn_osmux_allocate_cid(struct mgcp_conn_rtp *conn, int osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200561{
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200562 if (osmux_cid != -1 && osmux_cid_pool_allocated((uint8_t) osmux_cid)) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200563 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200564 "Osmux CID %d already allocated!\n",
565 osmux_cid);
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200566 return -1;
567 }
568
569 if (osmux_cid == -1) {
570 osmux_cid = osmux_cid_pool_get_next();
571 if (osmux_cid == -1) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200572 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200573 "no available Osmux CID to allocate!\n");
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200574 return -1;
575 }
576 } else
577 osmux_cid_pool_get(osmux_cid);
578
579 conn->osmux.cid = (uint8_t) osmux_cid;
580 conn->osmux.cid_allocated = true;
Pau Espin Pedrolfa810e82019-05-06 18:54:10 +0200581 conn->type = MGCP_OSMUX_BSC;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200582 return osmux_cid;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200583}
584
Philipp Maier87bd9be2017-08-22 16:35:41 +0200585/*! send RTP dummy packet to OSMUX connection port.
586 * \param[in] endp mcgp endpoint that holds the RTP connection
587 * \param[in] conn associated RTP connection
588 * \returns bytes sent, -1 on error */
589int osmux_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200590{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200591 char ipbuf[INET6_ADDRSTRLEN];
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200592 struct osmux_hdr *osmuxh;
593 int buf_len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200594 struct in_addr addr_unset = {};
595
Philipp Maier87bd9be2017-08-22 16:35:41 +0200596 /*! The dummy packet will not be sent via the actual OSMUX connection,
597 * instead it is sent out of band to port where the remote OSMUX
598 * multplexer is listening. The goal is to ensure that the connection
599 * is kept open */
600
601 /*! We don't need to send the dummy load for osmux so often as another
602 * endpoint may have already punched the hole in the firewall. This
603 * approach is simple though. */
604
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200605 /* Wait until we have the connection information from MDCX */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200606 if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200607 return 0;
608
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200609 if (endp_osmux_state_check(endp, conn, true) < 0)
610 return 0;
611
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200612 buf_len = sizeof(struct osmux_hdr) + osmo_amr_bytes(AMR_FT_0);
613 osmuxh = (struct osmux_hdr *) alloca(buf_len);
614 memset(osmuxh, 0, buf_len);
615 osmuxh->ft = OSMUX_FT_DUMMY;
616 osmuxh->amr_ft = AMR_FT_0;
617 osmuxh->circuit_id = conn->osmux.cid;
618
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200619 LOGPCONN(conn->conn, DOSMUX, LOGL_DEBUG,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200620 "sending OSMUX dummy load to %s:%u CID %u\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200621 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf),
622 ntohs(conn->end.rtp_port), conn->osmux.cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200623
Philipp Maier87bd9be2017-08-22 16:35:41 +0200624 return mgcp_udp_send(osmux_fd.fd, &conn->end.addr,
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200625 conn->end.rtp_port, (char*)osmuxh, buf_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200626}
627
Pau Espin Pedrolbcd52e52018-09-17 12:41:28 +0200628/* bsc-nat allocates/releases the Osmux circuit ID. +7 to round up to 8 bit boundary. */
629static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1 + 7) / 8];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200630
Philipp Maier87bd9be2017-08-22 16:35:41 +0200631/*! count the number of taken OSMUX cids.
632 * \returns number of OSMUX cids in use */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200633int osmux_cid_pool_count_used(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200634{
635 int i, j, used = 0;
636
637 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
638 for (j = 0; j < 8; j++) {
639 if (osmux_cid_bitmap[i] & (1 << j))
640 used += 1;
641 }
642 }
643
644 return used;
645}
646
Philipp Maier87bd9be2017-08-22 16:35:41 +0200647/*! take a free OSMUX cid.
648 * \returns OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200649int osmux_cid_pool_get_next(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200650{
651 int i, j;
652
653 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
654 for (j = 0; j < 8; j++) {
655 if (osmux_cid_bitmap[i] & (1 << j))
656 continue;
657
658 osmux_cid_bitmap[i] |= (1 << j);
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200659 LOGP(DOSMUX, LOGL_DEBUG,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200660 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
661 return (i * 8) + j;
662 }
663 }
664
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200665 LOGP(DOSMUX, LOGL_ERROR, "All Osmux circuits are in use!\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200666 return -1;
667}
668
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200669/*! take a specific OSMUX cid.
670 * \param[in] osmux_cid OSMUX cid */
671void osmux_cid_pool_get(uint8_t osmux_cid)
672{
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200673 LOGP(DOSMUX, LOGL_DEBUG, "Allocating Osmux CID %u from pool\n", osmux_cid);
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200674 osmux_cid_bitmap[osmux_cid / 8] |= (1 << (osmux_cid % 8));
675}
676
Philipp Maier87bd9be2017-08-22 16:35:41 +0200677/*! put back a no longer used OSMUX cid.
678 * \param[in] osmux_cid OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200679void osmux_cid_pool_put(uint8_t osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200680{
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200681 LOGP(DOSMUX, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200682 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
683}
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200684
685/*! check if OSMUX cid is already taken */
686bool osmux_cid_pool_allocated(uint8_t osmux_cid)
687{
688 return !!(osmux_cid_bitmap[osmux_cid / 8] & (1 << (osmux_cid % 8)));
689}