blob: 72152c721ee91ed351a1850a558a2f41a516d410 [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
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +020044static const struct rate_ctr_group_desc rate_ctr_group_osmux_desc = {
45 .group_name_prefix = "conn_osmux",
46 .group_description = "Osmux connection statistics",
47 .class_id = 1,
48 .num_ctr = ARRAY_SIZE(mgcp_conn_osmux_rate_ctr_desc),
49 .ctr_desc = mgcp_conn_osmux_rate_ctr_desc
50};
51
Philipp Maier87bd9be2017-08-22 16:35:41 +020052/* Deliver OSMUX batch to the remote end */
53static void osmux_deliver_cb(struct msgb *batch_msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020054{
55 struct osmux_handle *handle = data;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +020056 socklen_t dest_len;
Pau Espin Pedrol4d1a52d2022-09-22 17:11:57 +020057 int rc;
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +020058 struct mgcp_trunk *trunk = (struct mgcp_trunk *)osmux_fd.data;
59 struct rate_ctr_group *all_osmux_stats = trunk->ratectr.all_osmux_conn_stats;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020060
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +020061 switch (handle->rem_addr.u.sa.sa_family) {
62 case AF_INET6:
63 dest_len = sizeof(handle->rem_addr.u.sin6);
64 break;
65 case AF_INET:
66 default:
67 dest_len = sizeof(handle->rem_addr.u.sin);
68 break;
69 }
Pau Espin Pedrol4d1a52d2022-09-22 17:11:57 +020070 rc = sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0,
71 (struct sockaddr *)&handle->rem_addr.u.sa, dest_len);
72 if (rc < 0) {
73 char errbuf[129];
74 strerror_r(errno, errbuf, sizeof(errbuf));
75 LOGP(DOSMUX, LOGL_NOTICE, "osmux sendto(%s) failed: %s\n",
76 osmo_sockaddr_to_str(&handle->rem_addr), errbuf);
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +020077 rate_ctr_inc(rate_ctr_group_get_ctr(all_osmux_stats, OSMUX_DROPPED_PACKETS_CTR));
78 } else {
79 rate_ctr_inc(rate_ctr_group_get_ctr(all_osmux_stats, OSMUX_PACKETS_TX_CTR));
Pau Espin Pedrol4d1a52d2022-09-22 17:11:57 +020080 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020081 msgb_free(batch_msg);
82}
83
Philipp Maier87bd9be2017-08-22 16:35:41 +020084/* Lookup existing OSMUX handle for specified destination address. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020085static struct osmux_handle *
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +020086osmux_handle_find_get(const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020087{
88 struct osmux_handle *h;
89
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020090 llist_for_each_entry(h, &osmux_handle_list, head) {
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +020091 if (osmo_sockaddr_cmp(&h->rem_addr, rem_addr) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020092 h->refcnt++;
Pau Espin Pedrolc61664c2022-09-09 17:03:13 +020093 LOGP(DOSMUX, LOGL_DEBUG,
94 "Using existing OSMUX handle for addr=%s (rfcnt=%u)\n",
95 osmo_sockaddr_to_str(rem_addr), h->refcnt);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020096 return h;
97 }
98 }
99
100 return NULL;
101}
102
Philipp Maier87bd9be2017-08-22 16:35:41 +0200103/* Put down no longer needed OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200104static void osmux_handle_put(struct osmux_in_handle *in)
105{
106 struct osmux_handle *h;
107
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200108 llist_for_each_entry(h, &osmux_handle_list, head) {
109 if (h->in == in) {
Pau Espin Pedrolc61664c2022-09-09 17:03:13 +0200110 LOGP(DOSMUX, LOGL_DEBUG,
111 "Putting existing OSMUX handle for addr=%s (rfcnt=%u)\n",
112 osmo_sockaddr_to_str(&h->rem_addr), h->refcnt);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200113 if (--h->refcnt == 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200114 LOGP(DOSMUX, LOGL_INFO,
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200115 "Releasing unused osmux handle for %s\n",
116 osmo_sockaddr_to_str(&h->rem_addr));
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200117 LOGP(DOSMUX, LOGL_INFO, "Stats: "
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200118 "input RTP msgs: %u bytes: %"PRIu64" "
119 "output osmux msgs: %u bytes: %"PRIu64"\n",
120 in->stats.input_rtp_msgs,
121 in->stats.input_rtp_bytes,
122 in->stats.output_osmux_msgs,
123 in->stats.output_osmux_bytes);
124 llist_del(&h->head);
125 osmux_xfrm_input_fini(h->in);
126 talloc_free(h);
127 }
128 return;
129 }
130 }
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200131 LOGP(DOSMUX, LOGL_ERROR, "Cannot find Osmux input handle %p\n", in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200132}
133
Philipp Maier87bd9be2017-08-22 16:35:41 +0200134/* Allocate free OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200135static struct osmux_handle *
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200136osmux_handle_alloc(struct mgcp_conn_rtp *conn, const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200137{
138 struct osmux_handle *h;
Pau Espin Pedrol941e3172022-09-22 21:16:56 +0200139 struct mgcp_trunk *trunk = conn->conn->endp->trunk;
140 struct mgcp_config *cfg = trunk->cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200141
Pau Espin Pedrol941e3172022-09-22 21:16:56 +0200142 h = talloc_zero(trunk, struct osmux_handle);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200143 if (!h)
144 return NULL;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200145 h->rem_addr = *rem_addr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200146 h->refcnt++;
147
148 h->in = talloc_zero(h, struct osmux_in_handle);
149 if (!h->in) {
150 talloc_free(h);
151 return NULL;
152 }
153
Philipp Maier87bd9be2017-08-22 16:35:41 +0200154 /* sequence number to start OSMUX message from */
155 h->in->osmux_seq = 0;
156
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200157 h->in->batch_factor = cfg->osmux_batch;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200158
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200159 /* If batch size is zero, the library defaults to 1470 bytes. */
160 h->in->batch_size = cfg->osmux_batch_size;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200161 h->in->deliver = osmux_deliver_cb;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200162 osmux_xfrm_input_init(h->in);
163 h->in->data = h;
164
165 llist_add(&h->head, &osmux_handle_list);
166
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200167 LOGP(DOSMUX, LOGL_DEBUG, "Created new OSMUX handle for rem_addr=%s\n",
168 osmo_sockaddr_to_str(rem_addr));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200169
170 return h;
171}
172
Philipp Maier87bd9be2017-08-22 16:35:41 +0200173/* Lookup existing handle for a specified address, if the handle can not be
Harald Welte1d1b98f2017-12-25 10:03:40 +0100174 * found, the function will automatically allocate one */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200175static struct osmux_in_handle *
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200176osmux_handle_find_or_create(struct mgcp_conn_rtp *conn, const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200177{
178 struct osmux_handle *h;
179
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200180 if (rem_addr->u.sa.sa_family != AF_INET) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200181 LOGP(DOSMUX, LOGL_DEBUG, "IPv6 not supported in osmux yet!\n");
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200182 return NULL;
183 }
184
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200185 h = osmux_handle_find_get(rem_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200186 if (h != NULL)
187 return h->in;
188
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200189 h = osmux_handle_alloc(conn, rem_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200190 if (h == NULL)
191 return NULL;
192
193 return h->in;
194}
195
Philipp Maier87bd9be2017-08-22 16:35:41 +0200196/*! send RTP packet through OSMUX connection.
197 * \param[in] buf rtp data
198 * \param[in] buf_len length of rtp data
199 * \param[in] conn associated RTP connection
200 * \returns 0 on success, -1 on ERROR */
201int osmux_xfrm_to_osmux(char *buf, int buf_len, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200202{
203 int ret;
204 struct msgb *msg;
205
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200206 if (!conn->end.output_enabled) {
207 rtpconn_rate_ctr_inc(conn, conn->conn->endp, OSMUX_DROPPED_AMR_PAYLOADS_CTR);
Pau Espin Pedrole39ae872022-09-09 17:36:33 +0200208 return -1;
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200209 }
Pau Espin Pedrole39ae872022-09-09 17:36:33 +0200210
Pau Espin Pedrol8b0a6142022-09-09 17:34:31 +0200211 if (conn->osmux.state != OSMUX_STATE_ENABLED) {
212 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO, "forwarding RTP to Osmux conn not yet enabled, dropping (cid=%d)\n",
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200213 conn->osmux.remote_cid);
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200214 rtpconn_rate_ctr_inc(conn, conn->conn->endp, OSMUX_DROPPED_AMR_PAYLOADS_CTR);
Pau Espin Pedrol8b0a6142022-09-09 17:34:31 +0200215 return -1;
216 }
217
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200218 msg = msgb_alloc(4096, "RTP");
219 if (!msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200220 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200221
Philipp Maier87bd9be2017-08-22 16:35:41 +0200222 memcpy(msg->data, buf, buf_len);
223 msgb_put(msg, buf_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200224
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200225 while ((ret = osmux_xfrm_input(conn->osmux.in, msg, conn->osmux.remote_cid)) > 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200226 /* batch full, build and deliver it */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200227 osmux_xfrm_input_deliver(conn->osmux.in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200228 }
229 return 0;
230}
231
Philipp Maier87bd9be2017-08-22 16:35:41 +0200232/* Lookup the endpoint that corresponds to the specified address (port) */
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200233static struct mgcp_conn_rtp*
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200234osmux_conn_lookup(struct mgcp_trunk *trunk, uint8_t local_cid, const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200235{
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200236 struct mgcp_endpoint *endp;
237 struct mgcp_conn *conn = NULL;
238 struct mgcp_conn_rtp * conn_rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200239 int i;
240
Philipp Maierd19de2e2020-06-03 13:55:33 +0200241 for (i = 0; i < trunk->number_endpoints; i++) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200242
Philipp Maierd19de2e2020-06-03 13:55:33 +0200243 endp = trunk->endpoints[i];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200244
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200245 llist_for_each_entry(conn, &endp->conns, entry) {
246 if (conn->type != MGCP_CONN_TYPE_RTP)
247 continue;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200248
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200249 conn_rtp = &conn->u.rtp;
250 if (!mgcp_conn_rtp_is_osmux(conn_rtp))
251 continue;
252
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200253 /* FIXME: Match remote address! */
254
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200255 if (conn_rtp->osmux.local_cid == local_cid)
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200256 return conn_rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200257 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200258 }
259
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200260 LOGP(DOSMUX, LOGL_ERROR, "Cannot find osmux conn with rem_addr=%s local_cid=%d\n",
261 osmo_sockaddr_to_str(rem_addr), local_cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200262
263 return NULL;
264}
265
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200266/* 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 */
267enum {
268 MGCP_PROTO_RTP,
269 MGCP_PROTO_RTCP,
270};
271
272static void scheduled_from_osmux_tx_rtp_cb(struct msgb *msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200273{
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200274 struct mgcp_conn_rtp *conn = data;
275 struct mgcp_endpoint *endp = conn->conn->endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200276 struct osmo_sockaddr addr = { /* FIXME: do we know the source address?? */ };
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200277 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
278 *mc = (struct osmo_rtp_msg_ctx){
279 .proto = MGCP_PROTO_RTP,
280 .conn_src = conn,
281 .from_addr = &addr,
282 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200283
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200284 endp->type->dispatch_rtp_cb(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200285 msgb_free(msg);
286}
287
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200288static struct msgb *osmux_recv(struct osmo_fd *ofd, struct osmo_sockaddr *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200289{
290 struct msgb *msg;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200291 socklen_t slen = sizeof(addr->u.sas);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200292 int ret;
293
294 msg = msgb_alloc(4096, "OSMUX");
295 if (!msg) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200296 LOGP(DOSMUX, LOGL_ERROR, "cannot allocate message\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200297 return NULL;
298 }
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200299 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0, &addr->u.sa, &slen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200300 if (ret <= 0) {
301 msgb_free(msg);
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200302 LOGP(DOSMUX, LOGL_ERROR, "cannot receive message\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200303 return NULL;
304 }
305 msgb_put(msg, ret);
306
307 return msg;
308}
309
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200310/* Updates endp osmux state and returns 0 if it can process messages, -1 otherwise */
311static int endp_osmux_state_check(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
312 bool sending)
313{
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200314 struct osmo_sockaddr rem_addr;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200315
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200316 switch(conn->osmux.state) {
317 case OSMUX_STATE_ACTIVATING:
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200318 rem_addr = conn->end.addr;
319 osmo_sockaddr_set_port(&rem_addr.u.sa, ntohs(conn->end.rtp_port));
320 if (osmux_enable_conn(endp, conn, &rem_addr) < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200321 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200322 "Could not enable osmux for conn on %s: %s\n",
323 sending ? "sent" : "received",
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200324 mgcp_conn_dump(conn->conn));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200325 return -1;
326 }
Pau Espin Pedrolc5bbb782022-09-19 16:46:02 +0200327 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200328 "Osmux %s CID %u towards %s is now enabled\n",
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200329 sending ? "sent" : "received",
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200330 sending ? conn->osmux.remote_cid : conn->osmux.local_cid,
331 osmo_sockaddr_to_str(&rem_addr));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200332 return 0;
333 case OSMUX_STATE_ENABLED:
334 return 0;
335 default:
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200336 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200337 "Osmux %s in conn %s without full negotiation, state %d\n",
338 sending ? "sent" : "received",
339 mgcp_conn_dump(conn->conn), conn->osmux.state);
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200340 return -1;
341 }
342}
343
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200344static int osmux_legacy_dummy_parse_cid(const struct osmo_sockaddr *rem_addr, struct msgb *msg,
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200345 uint8_t *osmux_cid)
346{
Pau Espin Pedrolb5583cd2019-05-13 12:58:24 +0200347 if (msg->len < 1 + sizeof(*osmux_cid)) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200348 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrolb5583cd2019-05-13 12:58:24 +0200349 "Discarding truncated Osmux dummy load: %s\n", osmo_hexdump(msg->data, msg->len));
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200350 return -1;
351 }
352
353 /* extract the osmux CID from the dummy message */
354 memcpy(osmux_cid, &msg->data[1], sizeof(*osmux_cid));
355 return 0;
356}
357
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200358/* This is called from the bsc-nat */
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200359static int osmux_handle_dummy(struct mgcp_trunk *trunk, const struct osmo_sockaddr *rem_addr,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200360 struct msgb *msg)
361{
362 uint8_t osmux_cid;
363 struct mgcp_conn_rtp *conn;
364
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200365 if (osmux_legacy_dummy_parse_cid(rem_addr, msg, &osmux_cid) < 0)
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200366 goto out;
367
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200368 conn = osmux_conn_lookup(trunk, osmux_cid, rem_addr);
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200369 if (!conn) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200370 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200371 "Cannot find conn for Osmux CID %d\n", osmux_cid);
372 goto out;
373 }
374
375 endp_osmux_state_check(conn->conn->endp, conn, false);
376 /* Only needed to punch hole in firewall, it can be dropped */
377out:
378 msgb_free(msg);
379 return 0;
380}
381
Pau Espin Pedrol3fbf0352022-09-22 19:01:33 +0200382#define osmux_chunk_length(msg, rem) ((rem) - (msg)->len)
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200383static int osmux_read_fd_cb(struct osmo_fd *ofd, unsigned int what)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200384{
385 struct msgb *msg;
386 struct osmux_hdr *osmuxh;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200387 struct osmo_sockaddr rem_addr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200388 uint32_t rem;
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200389 struct mgcp_trunk *trunk = ofd->data;
390 struct rate_ctr_group *all_rtp_stats = trunk->ratectr.all_osmux_conn_stats;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200391
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200392 msg = osmux_recv(ofd, &rem_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200393 if (!msg)
394 return -1;
395
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200396 rate_ctr_inc(rate_ctr_group_get_ctr(all_rtp_stats, OSMUX_PACKETS_RX_CTR));
397
Pau Espin Pedrol928a20b2022-09-23 15:38:24 +0200398 if (trunk->cfg->osmux_use == OSMUX_USAGE_OFF) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200399 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrold14163e2018-10-16 15:48:59 +0200400 "bsc-nat wants to use Osmux but bsc did not request it\n");
401 goto out;
402 }
403
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200404 /* not any further processing dummy messages */
Philipp Maierb3d14eb2021-05-20 14:18:52 +0200405 if (mgcp_is_rtp_dummy_payload(msg))
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200406 return osmux_handle_dummy(trunk, &rem_addr, msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200407
408 rem = msg->len;
409 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200410 struct mgcp_endpoint *endp;
411 struct mgcp_conn_rtp *conn_src;
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200412 conn_src = osmux_conn_lookup(trunk, osmuxh->circuit_id,
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200413 &rem_addr);
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200414 if (!conn_src) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200415 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200416 "Cannot find a src conn for circuit_id=%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200417 osmuxh->circuit_id);
418 goto out;
419 }
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200420 endp = conn_src->conn->endp;
Pau Espin Pedrolde133112020-09-08 17:51:36 +0200421 mgcp_conn_watchdog_kick(conn_src->conn);
422
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200423 /*conn_dst = mgcp_find_dst_conn(conn_src->conn);
424 if (!conn_dst) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200425 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200426 "Cannot find a dst conn for circuit_id=%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200427 osmuxh->circuit_id);
428 goto out;
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200429 }*/
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200430 if (endp_osmux_state_check(endp, conn_src, false) == 0) {
431 rtpconn_rate_ctr_inc(conn_src, endp, OSMUX_CHUNKS_RX_CTR);
432 rtpconn_rate_ctr_add(conn_src, endp, OSMUX_OCTETS_RX_CTR,
433 osmux_chunk_length(msg, rem));
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200434 osmux_xfrm_output_sched(conn_src->osmux.out, osmuxh);
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200435 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200436 rem = msg->len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200437 }
438out:
439 msgb_free(msg);
440 return 0;
441}
442
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200443int osmux_init(int role, struct mgcp_trunk *trunk)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200444{
445 int ret;
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200446 struct mgcp_config *cfg = trunk->cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200447
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200448 /* So far we only support running on one trunk: */
449 OSMO_ASSERT(trunk == mgcp_trunk_by_num(cfg, MGCP_TRUNK_VIRTUAL, MGCP_VIRT_TRUNK_ID));
450
451 osmo_fd_setup(&osmux_fd, -1, OSMO_FD_READ, osmux_read_fd_cb, trunk, 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200452
Harald Welte55a92292021-04-28 19:06:34 +0200453 ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port,
454 cfg->endp_dscp, cfg->endp_priority);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200455 if (ret < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200456 LOGP(DOSMUX, LOGL_ERROR, "cannot bind OSMUX socket to %s:%u\n",
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200457 cfg->osmux_addr, cfg->osmux_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200458 return ret;
459 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200460
461 ret = osmo_fd_register(&osmux_fd);
462 if (ret < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200463 LOGP(DOSMUX, LOGL_ERROR, "cannot register OSMUX socket %s\n",
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200464 osmo_sock_get_name2(osmux_fd.fd));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200465 return ret;
466 }
Pau Espin Pedrol87f114a2022-09-23 15:40:11 +0200467 cfg->osmux_init = true;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200468
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200469 LOGP(DOSMUX, LOGL_INFO, "OSMUX socket listening on %s\n",
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200470 osmo_sock_get_name2(osmux_fd.fd));
471
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200472 return 0;
473}
474
Philipp Maier87bd9be2017-08-22 16:35:41 +0200475/*! enable OSXMUX circuit for a specified connection.
476 * \param[in] endp mgcp endpoint (configuration)
477 * \param[in] conn connection to disable
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200478 * \param[in] addr IP address and port of remote OSMUX endpoint
Philipp Maier87bd9be2017-08-22 16:35:41 +0200479 * \returns 0 on success, -1 on ERROR */
480int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200481 const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200482{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200483 /*! If osmux is enabled, initialize the output handler. This handler is
484 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
485 * allocated based on the circuit ID (conn_net->osmux.cid), which is unique
486 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200487 * SSRC space (2^32) by the OSMUX_CID_MAX + 1 possible circuit IDs, then randomly
Philipp Maier87bd9be2017-08-22 16:35:41 +0200488 * select one value from that window. Thus, we have no chance to have
489 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
490 * similarly, for flows traveling to the MSC.
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200491 */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200492 struct in6_addr addr_unset = {};
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200493 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / (OSMUX_CID_MAX + 1);
Ericfbf78d12021-08-23 22:31:39 +0200494 uint16_t osmux_dummy = endp->trunk->cfg->osmux_dummy;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200495
Philipp Maier87bd9be2017-08-22 16:35:41 +0200496 /* Check if osmux is enabled for the specified connection */
Pau Espin Pedrol48aff622018-10-16 16:03:57 +0200497 if (conn->osmux.state != OSMUX_STATE_ACTIVATING) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200498 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200499 "conn:%s didn't negotiate Osmux, state %d\n",
500 mgcp_conn_dump(conn->conn), conn->osmux.state);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200501 return -1;
502 }
503
Pau Espin Pedroldac2ca72019-05-13 17:34:51 +0200504 /* Wait until we have the connection information from MDCX */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200505 if (memcmp(&conn->end.addr, &addr_unset,
506 conn->end.addr.u.sa.sa_family == AF_INET6 ?
507 sizeof(struct in6_addr) :
508 sizeof(struct in_addr)) == 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200509 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedroldac2ca72019-05-13 17:34:51 +0200510 "Osmux remote address/port still unknown\n");
511 return -1;
512 }
513
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200514 conn->osmux.in = osmux_handle_find_or_create(conn, rem_addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200515 if (!conn->osmux.in) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200516 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200517 "Cannot allocate input osmux handle for conn:%s\n",
518 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200519 return -1;
520 }
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200521 if (osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.remote_cid, osmux_dummy) < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200522 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200523 "Cannot open osmux circuit %u for conn:%s\n",
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200524 conn->osmux.remote_cid, mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200525 return -1;
526 }
527
Pau Espin Pedrol941e3172022-09-22 21:16:56 +0200528 conn->osmux.out = osmux_xfrm_output_alloc(conn->conn);
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200529 osmux_xfrm_output_set_rtp_ssrc(conn->osmux.out,
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200530 (conn->osmux.remote_cid * rtp_ssrc_winlen) +
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200531 (random() % rtp_ssrc_winlen));
532 osmux_xfrm_output_set_rtp_pl_type(conn->osmux.out, conn->end.codec->payload_type);
533 osmux_xfrm_output_set_tx_cb(conn->osmux.out,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200534 scheduled_from_osmux_tx_rtp_cb, conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200535
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200536 conn->osmux.ctrg = rate_ctr_group_alloc(conn->conn, &rate_ctr_group_osmux_desc, conn->ctrg->idx);
537
Philipp Maier87bd9be2017-08-22 16:35:41 +0200538 conn->osmux.state = OSMUX_STATE_ENABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200539
540 return 0;
541}
542
Philipp Maier87bd9be2017-08-22 16:35:41 +0200543/*! disable OSXMUX circuit for a specified connection.
544 * \param[in] conn connection to disable */
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200545void conn_osmux_disable(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200546{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200547
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200548 OSMO_ASSERT(conn->osmux.state != OSMUX_STATE_DISABLED);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200549
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200550 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200551 "Releasing connection using local Osmux CID %u\n", conn->osmux.local_cid);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200552
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200553 struct rate_ctr_group *all_osmux_stats = conn->conn->endp->trunk->ratectr.all_osmux_conn_stats;
554 rate_ctr_inc(rate_ctr_group_get_ctr(all_osmux_stats, OSMUX_NUM_CONNECTIONS));
555
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200556 if (conn->osmux.state == OSMUX_STATE_ENABLED) {
557 /* We are closing, we don't need pending RTP packets to be transmitted */
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200558 osmux_xfrm_output_set_tx_cb(conn->osmux.out, NULL, NULL);
559 TALLOC_FREE(conn->osmux.out);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200560
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200561 osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.remote_cid);
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200562 conn->osmux.state = OSMUX_STATE_DISABLED;
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200563 osmux_handle_put(conn->osmux.in);
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200564 conn->osmux.remote_cid = 0;
565 conn->osmux.remote_cid_present = false;
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200566
567 rate_ctr_group_free(conn->osmux.ctrg);
568 conn->osmux.ctrg = NULL;
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200569 }
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200570 conn_osmux_release_local_cid(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200571}
572
Philipp Maier87bd9be2017-08-22 16:35:41 +0200573/*! relase OSXMUX cid, that had been allocated to this connection.
574 * \param[in] conn connection with OSMUX cid to release */
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200575void conn_osmux_release_local_cid(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200576{
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200577 if (conn->osmux.local_cid_allocated)
578 osmux_cid_pool_put(conn->osmux.local_cid);
579 conn->osmux.local_cid = 0;
580 conn->osmux.local_cid_allocated = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200581}
582
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200583/*! allocate local OSMUX cid to connection.
584 * \param[in] conn connection for which we allocate the local OSMUX cid
585 * \returns Allocated OSMUX cid, -1 on error (no free CIDs avail).
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200586 */
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200587int conn_osmux_allocate_local_cid(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200588{
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200589 OSMO_ASSERT(conn->osmux.local_cid_allocated == false);
590 int osmux_cid = osmux_cid_pool_get_next();
591 if (osmux_cid == -1) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200592 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200593 "no available local Osmux CID to allocate!\n");
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200594 return -1;
595 }
596
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200597 conn->osmux.local_cid = (uint8_t) osmux_cid;
598 conn->osmux.local_cid_allocated = true;
Pau Espin Pedrolfa810e82019-05-06 18:54:10 +0200599 conn->type = MGCP_OSMUX_BSC;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200600 return osmux_cid;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200601}
602
Philipp Maier87bd9be2017-08-22 16:35:41 +0200603/*! send RTP dummy packet to OSMUX connection port.
604 * \param[in] endp mcgp endpoint that holds the RTP connection
605 * \param[in] conn associated RTP connection
606 * \returns bytes sent, -1 on error */
607int osmux_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200608{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200609 char ipbuf[INET6_ADDRSTRLEN];
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200610 struct osmux_hdr *osmuxh;
611 int buf_len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200612 struct in_addr addr_unset = {};
613
Philipp Maier87bd9be2017-08-22 16:35:41 +0200614 /*! The dummy packet will not be sent via the actual OSMUX connection,
615 * instead it is sent out of band to port where the remote OSMUX
616 * multplexer is listening. The goal is to ensure that the connection
617 * is kept open */
618
619 /*! We don't need to send the dummy load for osmux so often as another
620 * endpoint may have already punched the hole in the firewall. This
621 * approach is simple though. */
622
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200623 /* Wait until we have the connection information from MDCX */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200624 if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200625 return 0;
626
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200627 if (endp_osmux_state_check(endp, conn, true) < 0)
628 return 0;
629
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200630 buf_len = sizeof(struct osmux_hdr) + osmo_amr_bytes(AMR_FT_0);
631 osmuxh = (struct osmux_hdr *) alloca(buf_len);
632 memset(osmuxh, 0, buf_len);
633 osmuxh->ft = OSMUX_FT_DUMMY;
634 osmuxh->amr_ft = AMR_FT_0;
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200635 osmuxh->circuit_id = conn->osmux.remote_cid;
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200636
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200637 LOGPCONN(conn->conn, DOSMUX, LOGL_DEBUG,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200638 "sending OSMUX dummy load to %s:%u CID %u\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200639 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf),
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200640 ntohs(conn->end.rtp_port), conn->osmux.remote_cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200641
Philipp Maier87bd9be2017-08-22 16:35:41 +0200642 return mgcp_udp_send(osmux_fd.fd, &conn->end.addr,
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200643 conn->end.rtp_port, (char*)osmuxh, buf_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200644}
645
Pau Espin Pedrolbcd52e52018-09-17 12:41:28 +0200646/* bsc-nat allocates/releases the Osmux circuit ID. +7 to round up to 8 bit boundary. */
647static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1 + 7) / 8];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200648
Philipp Maier87bd9be2017-08-22 16:35:41 +0200649/*! count the number of taken OSMUX cids.
650 * \returns number of OSMUX cids in use */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200651int osmux_cid_pool_count_used(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200652{
653 int i, j, used = 0;
654
655 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
656 for (j = 0; j < 8; j++) {
657 if (osmux_cid_bitmap[i] & (1 << j))
658 used += 1;
659 }
660 }
661
662 return used;
663}
664
Philipp Maier87bd9be2017-08-22 16:35:41 +0200665/*! take a free OSMUX cid.
666 * \returns OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200667int osmux_cid_pool_get_next(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200668{
669 int i, j;
670
671 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
672 for (j = 0; j < 8; j++) {
673 if (osmux_cid_bitmap[i] & (1 << j))
674 continue;
675
676 osmux_cid_bitmap[i] |= (1 << j);
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200677 LOGP(DOSMUX, LOGL_DEBUG,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200678 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
679 return (i * 8) + j;
680 }
681 }
682
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200683 LOGP(DOSMUX, LOGL_ERROR, "All Osmux circuits are in use!\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200684 return -1;
685}
686
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200687/*! take a specific OSMUX cid.
688 * \param[in] osmux_cid OSMUX cid */
689void osmux_cid_pool_get(uint8_t osmux_cid)
690{
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200691 LOGP(DOSMUX, LOGL_DEBUG, "Allocating Osmux CID %u from pool\n", osmux_cid);
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200692 osmux_cid_bitmap[osmux_cid / 8] |= (1 << (osmux_cid % 8));
693}
694
Philipp Maier87bd9be2017-08-22 16:35:41 +0200695/*! put back a no longer used OSMUX cid.
696 * \param[in] osmux_cid OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200697void osmux_cid_pool_put(uint8_t osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200698{
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200699 LOGP(DOSMUX, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200700 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
701}
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200702
703/*! check if OSMUX cid is already taken */
704bool osmux_cid_pool_allocated(uint8_t osmux_cid)
705{
706 return !!(osmux_cid_bitmap[osmux_cid / 8] & (1 << (osmux_cid % 8)));
707}