blob: 5c3ddbfea40ea1db85964908747b185272503c50 [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 Pedrolea7aaf22022-09-22 21:07:54 +020044const struct value_string osmux_state_strs[] = {
45 { OSMUX_STATE_DISABLED, "disabled" },
46 { OSMUX_STATE_ACTIVATING, "activating" },
47 { OSMUX_STATE_ENABLED, "enabled" },
48 { 0, NULL }
49};
50
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +020051static const struct rate_ctr_group_desc rate_ctr_group_osmux_desc = {
52 .group_name_prefix = "conn_osmux",
53 .group_description = "Osmux connection statistics",
54 .class_id = 1,
55 .num_ctr = ARRAY_SIZE(mgcp_conn_osmux_rate_ctr_desc),
56 .ctr_desc = mgcp_conn_osmux_rate_ctr_desc
57};
58
Pau Espin Pedrolb4a59472022-09-23 18:08:53 +020059static void rtpconn_osmux_rate_ctr_add(struct mgcp_conn_rtp *conn_rtp, int id, int inc)
60{
61 struct rate_ctr_group *conn_osmux_stats = conn_rtp->osmux.ctrg;
62 struct rate_ctr_group *trunk_osmux_stats = conn_rtp->conn->endp->trunk->ratectr.all_osmux_conn_stats;
63
64 /* add to both the per-connection and the global stats */
65 rate_ctr_add(rate_ctr_group_get_ctr(conn_osmux_stats, id), inc);
66 rate_ctr_add(rate_ctr_group_get_ctr(trunk_osmux_stats, id), inc);
67}
68
69static void rtpconn_osmux_rate_ctr_inc(struct mgcp_conn_rtp *conn_rtp, int id)
70{
71 rtpconn_osmux_rate_ctr_add(conn_rtp, id, 1);
72}
73
Philipp Maier87bd9be2017-08-22 16:35:41 +020074/* Deliver OSMUX batch to the remote end */
75static void osmux_deliver_cb(struct msgb *batch_msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020076{
77 struct osmux_handle *handle = data;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +020078 socklen_t dest_len;
Pau Espin Pedrol4d1a52d2022-09-22 17:11:57 +020079 int rc;
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +020080 struct mgcp_trunk *trunk = (struct mgcp_trunk *)osmux_fd.data;
81 struct rate_ctr_group *all_osmux_stats = trunk->ratectr.all_osmux_conn_stats;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020082
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +020083 switch (handle->rem_addr.u.sa.sa_family) {
84 case AF_INET6:
85 dest_len = sizeof(handle->rem_addr.u.sin6);
86 break;
87 case AF_INET:
88 default:
89 dest_len = sizeof(handle->rem_addr.u.sin);
90 break;
91 }
Pau Espin Pedrol4d1a52d2022-09-22 17:11:57 +020092 rc = sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0,
93 (struct sockaddr *)&handle->rem_addr.u.sa, dest_len);
94 if (rc < 0) {
95 char errbuf[129];
96 strerror_r(errno, errbuf, sizeof(errbuf));
97 LOGP(DOSMUX, LOGL_NOTICE, "osmux sendto(%s) failed: %s\n",
98 osmo_sockaddr_to_str(&handle->rem_addr), errbuf);
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +020099 rate_ctr_inc(rate_ctr_group_get_ctr(all_osmux_stats, OSMUX_DROPPED_PACKETS_CTR));
100 } else {
101 rate_ctr_inc(rate_ctr_group_get_ctr(all_osmux_stats, OSMUX_PACKETS_TX_CTR));
Pau Espin Pedrol4d1a52d2022-09-22 17:11:57 +0200102 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200103 msgb_free(batch_msg);
104}
105
Philipp Maier87bd9be2017-08-22 16:35:41 +0200106/* Lookup existing OSMUX handle for specified destination address. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200107static struct osmux_handle *
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200108osmux_handle_find_get(const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200109{
110 struct osmux_handle *h;
111
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200112 llist_for_each_entry(h, &osmux_handle_list, head) {
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200113 if (osmo_sockaddr_cmp(&h->rem_addr, rem_addr) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200114 h->refcnt++;
Pau Espin Pedrolc61664c2022-09-09 17:03:13 +0200115 LOGP(DOSMUX, LOGL_DEBUG,
116 "Using existing OSMUX handle for addr=%s (rfcnt=%u)\n",
117 osmo_sockaddr_to_str(rem_addr), h->refcnt);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200118 return h;
119 }
120 }
121
122 return NULL;
123}
124
Philipp Maier87bd9be2017-08-22 16:35:41 +0200125/* Put down no longer needed OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200126static void osmux_handle_put(struct osmux_in_handle *in)
127{
128 struct osmux_handle *h;
129
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200130 llist_for_each_entry(h, &osmux_handle_list, head) {
131 if (h->in == in) {
Pau Espin Pedrolc61664c2022-09-09 17:03:13 +0200132 LOGP(DOSMUX, LOGL_DEBUG,
133 "Putting existing OSMUX handle for addr=%s (rfcnt=%u)\n",
134 osmo_sockaddr_to_str(&h->rem_addr), h->refcnt);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200135 if (--h->refcnt == 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200136 LOGP(DOSMUX, LOGL_INFO,
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200137 "Releasing unused osmux handle for %s\n",
138 osmo_sockaddr_to_str(&h->rem_addr));
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200139 LOGP(DOSMUX, LOGL_INFO, "Stats: "
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200140 "input RTP msgs: %u bytes: %"PRIu64" "
141 "output osmux msgs: %u bytes: %"PRIu64"\n",
142 in->stats.input_rtp_msgs,
143 in->stats.input_rtp_bytes,
144 in->stats.output_osmux_msgs,
145 in->stats.output_osmux_bytes);
146 llist_del(&h->head);
Pau Espin Pedrol72eff2c2022-09-29 14:26:00 +0200147 TALLOC_FREE(h->in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200148 talloc_free(h);
149 }
150 return;
151 }
152 }
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200153 LOGP(DOSMUX, LOGL_ERROR, "Cannot find Osmux input handle %p\n", in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200154}
155
Philipp Maier87bd9be2017-08-22 16:35:41 +0200156/* Allocate free OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200157static struct osmux_handle *
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200158osmux_handle_alloc(struct mgcp_conn_rtp *conn, const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200159{
160 struct osmux_handle *h;
Pau Espin Pedrol941e3172022-09-22 21:16:56 +0200161 struct mgcp_trunk *trunk = conn->conn->endp->trunk;
162 struct mgcp_config *cfg = trunk->cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200163
Pau Espin Pedrol941e3172022-09-22 21:16:56 +0200164 h = talloc_zero(trunk, struct osmux_handle);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200165 if (!h)
166 return NULL;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200167 h->rem_addr = *rem_addr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200168 h->refcnt++;
169
Pau Espin Pedrol72eff2c2022-09-29 14:26:00 +0200170 h->in = osmux_xfrm_input_alloc(h);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200171 if (!h->in) {
172 talloc_free(h);
173 return NULL;
174 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200175 /* sequence number to start OSMUX message from */
Pau Espin Pedrol72eff2c2022-09-29 14:26:00 +0200176 osmux_xfrm_input_set_initial_seqnum(h->in, 0);
177 osmux_xfrm_input_set_batch_factor(h->in, cfg->osmux_batch);
178 /* If batch size is zero, the library defaults to 1472 bytes. */
179 osmux_xfrm_input_set_batch_size(h->in, cfg->osmux_batch_size);
180 osmux_xfrm_input_set_deliver_cb(h->in, osmux_deliver_cb, h);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200181
182 llist_add(&h->head, &osmux_handle_list);
183
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200184 LOGP(DOSMUX, LOGL_DEBUG, "Created new OSMUX handle for rem_addr=%s\n",
185 osmo_sockaddr_to_str(rem_addr));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200186
187 return h;
188}
189
Philipp Maier87bd9be2017-08-22 16:35:41 +0200190/* Lookup existing handle for a specified address, if the handle can not be
Harald Welte1d1b98f2017-12-25 10:03:40 +0100191 * found, the function will automatically allocate one */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200192static struct osmux_in_handle *
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200193osmux_handle_find_or_create(struct mgcp_conn_rtp *conn, const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200194{
195 struct osmux_handle *h;
196
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200197 if (rem_addr->u.sa.sa_family != AF_INET) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200198 LOGP(DOSMUX, LOGL_DEBUG, "IPv6 not supported in osmux yet!\n");
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200199 return NULL;
200 }
201
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200202 h = osmux_handle_find_get(rem_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200203 if (h != NULL)
204 return h->in;
205
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200206 h = osmux_handle_alloc(conn, rem_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200207 if (h == NULL)
208 return NULL;
209
210 return h->in;
211}
212
Philipp Maier87bd9be2017-08-22 16:35:41 +0200213/*! send RTP packet through OSMUX connection.
214 * \param[in] buf rtp data
215 * \param[in] buf_len length of rtp data
216 * \param[in] conn associated RTP connection
217 * \returns 0 on success, -1 on ERROR */
218int osmux_xfrm_to_osmux(char *buf, int buf_len, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200219{
220 int ret;
221 struct msgb *msg;
222
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200223 if (!conn->end.output_enabled) {
Pau Espin Pedrol432ee9d2022-09-23 18:18:02 +0200224 rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_DROPPED_CTR);
Pau Espin Pedrole39ae872022-09-09 17:36:33 +0200225 return -1;
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200226 }
Pau Espin Pedrole39ae872022-09-09 17:36:33 +0200227
Pau Espin Pedrol8b0a6142022-09-09 17:34:31 +0200228 if (conn->osmux.state != OSMUX_STATE_ENABLED) {
229 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 +0200230 conn->osmux.remote_cid);
Pau Espin Pedrol432ee9d2022-09-23 18:18:02 +0200231 rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_DROPPED_CTR);
Pau Espin Pedrol8b0a6142022-09-09 17:34:31 +0200232 return -1;
233 }
234
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200235 msg = msgb_alloc(4096, "RTP");
236 if (!msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200237 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200238
Philipp Maier87bd9be2017-08-22 16:35:41 +0200239 memcpy(msg->data, buf, buf_len);
240 msgb_put(msg, buf_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200241
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200242 while ((ret = osmux_xfrm_input(conn->osmux.in, msg, conn->osmux.remote_cid)) > 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200243 /* batch full, build and deliver it */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200244 osmux_xfrm_input_deliver(conn->osmux.in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200245 }
Pau Espin Pedrol432ee9d2022-09-23 18:18:02 +0200246 if (ret < 0) {
247 rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_DROPPED_CTR);
248 } else {
249 rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_CTR);
250 rtpconn_osmux_rate_ctr_add(conn, OSMUX_AMR_OCTETS_TX_CTR, buf_len - sizeof(struct rtp_hdr));
251 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200252 return 0;
253}
254
Philipp Maier87bd9be2017-08-22 16:35:41 +0200255/* Lookup the endpoint that corresponds to the specified address (port) */
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200256static struct mgcp_conn_rtp*
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200257osmux_conn_lookup(struct mgcp_trunk *trunk, uint8_t local_cid, const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200258{
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200259 struct mgcp_endpoint *endp;
260 struct mgcp_conn *conn = NULL;
261 struct mgcp_conn_rtp * conn_rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200262 int i;
263
Philipp Maierd19de2e2020-06-03 13:55:33 +0200264 for (i = 0; i < trunk->number_endpoints; i++) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200265
Philipp Maierd19de2e2020-06-03 13:55:33 +0200266 endp = trunk->endpoints[i];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200267
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200268 llist_for_each_entry(conn, &endp->conns, entry) {
269 if (conn->type != MGCP_CONN_TYPE_RTP)
270 continue;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200271
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200272 conn_rtp = &conn->u.rtp;
273 if (!mgcp_conn_rtp_is_osmux(conn_rtp))
274 continue;
275
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200276 /* FIXME: Match remote address! */
277
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200278 if (conn_rtp->osmux.local_cid == local_cid)
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200279 return conn_rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200280 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200281 }
282
Pau Espin Pedrol48fb1762022-09-29 15:50:07 +0200283 LOGP(DOSMUX, LOGL_DEBUG, "Cannot find osmux conn with rem_addr=%s local_cid=%d\n",
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200284 osmo_sockaddr_to_str(rem_addr), local_cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200285
286 return NULL;
287}
288
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200289/* 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 */
290enum {
291 MGCP_PROTO_RTP,
292 MGCP_PROTO_RTCP,
293};
294
295static void scheduled_from_osmux_tx_rtp_cb(struct msgb *msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200296{
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200297 struct mgcp_conn_rtp *conn = data;
298 struct mgcp_endpoint *endp = conn->conn->endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200299 struct osmo_sockaddr addr = { /* FIXME: do we know the source address?? */ };
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200300 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
301 *mc = (struct osmo_rtp_msg_ctx){
302 .proto = MGCP_PROTO_RTP,
303 .conn_src = conn,
304 .from_addr = &addr,
305 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200306
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200307 endp->type->dispatch_rtp_cb(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200308 msgb_free(msg);
309}
310
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200311static struct msgb *osmux_recv(struct osmo_fd *ofd, struct osmo_sockaddr *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200312{
313 struct msgb *msg;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200314 socklen_t slen = sizeof(addr->u.sas);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200315 int ret;
316
317 msg = msgb_alloc(4096, "OSMUX");
318 if (!msg) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200319 LOGP(DOSMUX, LOGL_ERROR, "cannot allocate message\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200320 return NULL;
321 }
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200322 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0, &addr->u.sa, &slen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200323 if (ret <= 0) {
324 msgb_free(msg);
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200325 LOGP(DOSMUX, LOGL_ERROR, "cannot receive message\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200326 return NULL;
327 }
328 msgb_put(msg, ret);
329
330 return msg;
331}
332
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200333/* Updates endp osmux state and returns 0 if it can process messages, -1 otherwise */
334static int endp_osmux_state_check(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
335 bool sending)
336{
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200337 struct osmo_sockaddr rem_addr;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200338
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200339 switch(conn->osmux.state) {
340 case OSMUX_STATE_ACTIVATING:
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200341 rem_addr = conn->end.addr;
342 osmo_sockaddr_set_port(&rem_addr.u.sa, ntohs(conn->end.rtp_port));
343 if (osmux_enable_conn(endp, conn, &rem_addr) < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200344 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200345 "Could not enable osmux for conn on %s: %s\n",
346 sending ? "sent" : "received",
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200347 mgcp_conn_dump(conn->conn));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200348 return -1;
349 }
Pau Espin Pedrolc5bbb782022-09-19 16:46:02 +0200350 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200351 "Osmux %s CID %u towards %s is now enabled\n",
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200352 sending ? "sent" : "received",
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200353 sending ? conn->osmux.remote_cid : conn->osmux.local_cid,
354 osmo_sockaddr_to_str(&rem_addr));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200355 return 0;
356 case OSMUX_STATE_ENABLED:
357 return 0;
358 default:
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200359 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200360 "Osmux %s in conn %s without full negotiation, state %d\n",
361 sending ? "sent" : "received",
362 mgcp_conn_dump(conn->conn), conn->osmux.state);
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200363 return -1;
364 }
365}
366
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200367static int osmux_legacy_dummy_parse_cid(const struct osmo_sockaddr *rem_addr, struct msgb *msg,
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200368 uint8_t *osmux_cid)
369{
Pau Espin Pedrolb5583cd2019-05-13 12:58:24 +0200370 if (msg->len < 1 + sizeof(*osmux_cid)) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200371 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrolb5583cd2019-05-13 12:58:24 +0200372 "Discarding truncated Osmux dummy load: %s\n", osmo_hexdump(msg->data, msg->len));
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200373 return -1;
374 }
375
376 /* extract the osmux CID from the dummy message */
377 memcpy(osmux_cid, &msg->data[1], sizeof(*osmux_cid));
378 return 0;
379}
380
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200381/* This is called from the bsc-nat */
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200382static int osmux_handle_dummy(struct mgcp_trunk *trunk, const struct osmo_sockaddr *rem_addr,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200383 struct msgb *msg)
384{
385 uint8_t osmux_cid;
386 struct mgcp_conn_rtp *conn;
387
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200388 if (osmux_legacy_dummy_parse_cid(rem_addr, msg, &osmux_cid) < 0)
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200389 goto out;
390
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200391 conn = osmux_conn_lookup(trunk, osmux_cid, rem_addr);
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200392 if (!conn) {
Pau Espin Pedrol48fb1762022-09-29 15:50:07 +0200393 LOGP(DOSMUX, LOGL_DEBUG,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200394 "Cannot find conn for Osmux CID %d\n", osmux_cid);
395 goto out;
396 }
397
398 endp_osmux_state_check(conn->conn->endp, conn, false);
399 /* Only needed to punch hole in firewall, it can be dropped */
400out:
401 msgb_free(msg);
402 return 0;
403}
404
Pau Espin Pedrol3fbf0352022-09-22 19:01:33 +0200405#define osmux_chunk_length(msg, rem) ((rem) - (msg)->len)
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200406static int osmux_read_fd_cb(struct osmo_fd *ofd, unsigned int what)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200407{
408 struct msgb *msg;
409 struct osmux_hdr *osmuxh;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200410 struct osmo_sockaddr rem_addr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200411 uint32_t rem;
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200412 struct mgcp_trunk *trunk = ofd->data;
413 struct rate_ctr_group *all_rtp_stats = trunk->ratectr.all_osmux_conn_stats;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200414
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200415 msg = osmux_recv(ofd, &rem_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200416 if (!msg)
417 return -1;
418
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200419 rate_ctr_inc(rate_ctr_group_get_ctr(all_rtp_stats, OSMUX_PACKETS_RX_CTR));
420
Pau Espin Pedrol928a20b2022-09-23 15:38:24 +0200421 if (trunk->cfg->osmux_use == OSMUX_USAGE_OFF) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200422 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrold14163e2018-10-16 15:48:59 +0200423 "bsc-nat wants to use Osmux but bsc did not request it\n");
424 goto out;
425 }
426
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200427 /* not any further processing dummy messages */
Philipp Maierb3d14eb2021-05-20 14:18:52 +0200428 if (mgcp_is_rtp_dummy_payload(msg))
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200429 return osmux_handle_dummy(trunk, &rem_addr, msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200430
431 rem = msg->len;
432 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200433 struct mgcp_endpoint *endp;
434 struct mgcp_conn_rtp *conn_src;
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200435 conn_src = osmux_conn_lookup(trunk, osmuxh->circuit_id,
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200436 &rem_addr);
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200437 if (!conn_src) {
Pau Espin Pedrol48fb1762022-09-29 15:50:07 +0200438 LOGP(DOSMUX, LOGL_DEBUG,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200439 "Cannot find a src conn for circuit_id=%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200440 osmuxh->circuit_id);
Pau Espin Pedrol1c5fcb02022-09-29 15:59:15 +0200441 rem = msg->len;
442 continue;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200443 }
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200444 endp = conn_src->conn->endp;
Pau Espin Pedrolde133112020-09-08 17:51:36 +0200445 mgcp_conn_watchdog_kick(conn_src->conn);
446
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200447 if (endp_osmux_state_check(endp, conn_src, false) == 0) {
Pau Espin Pedrolb4a59472022-09-23 18:08:53 +0200448 rtpconn_osmux_rate_ctr_inc(conn_src, OSMUX_CHUNKS_RX_CTR);
449 rtpconn_osmux_rate_ctr_add(conn_src, OSMUX_OCTETS_RX_CTR,
450 osmux_chunk_length(msg, rem));
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200451 osmux_xfrm_output_sched(conn_src->osmux.out, osmuxh);
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200452 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200453 rem = msg->len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200454 }
455out:
456 msgb_free(msg);
457 return 0;
458}
459
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200460int osmux_init(int role, struct mgcp_trunk *trunk)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200461{
462 int ret;
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200463 struct mgcp_config *cfg = trunk->cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200464
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200465 /* So far we only support running on one trunk: */
466 OSMO_ASSERT(trunk == mgcp_trunk_by_num(cfg, MGCP_TRUNK_VIRTUAL, MGCP_VIRT_TRUNK_ID));
467
468 osmo_fd_setup(&osmux_fd, -1, OSMO_FD_READ, osmux_read_fd_cb, trunk, 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200469
Harald Welte55a92292021-04-28 19:06:34 +0200470 ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port,
471 cfg->endp_dscp, cfg->endp_priority);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200472 if (ret < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200473 LOGP(DOSMUX, LOGL_ERROR, "cannot bind OSMUX socket to %s:%u\n",
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200474 cfg->osmux_addr, cfg->osmux_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200475 return ret;
476 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200477
478 ret = osmo_fd_register(&osmux_fd);
479 if (ret < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200480 LOGP(DOSMUX, LOGL_ERROR, "cannot register OSMUX socket %s\n",
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200481 osmo_sock_get_name2(osmux_fd.fd));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200482 return ret;
483 }
Pau Espin Pedrolb7f52b42022-09-25 00:14:03 +0200484 cfg->osmux_initialized = true;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200485
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200486 LOGP(DOSMUX, LOGL_INFO, "OSMUX socket listening on %s\n",
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200487 osmo_sock_get_name2(osmux_fd.fd));
488
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200489 return 0;
490}
491
Pau Espin Pedrold48a8112022-09-27 12:37:53 +0200492/*! Initialize Osmux bits of a conn.
493 * \param[in] conn Osmux connection to initialize
494 * \returns 0 on success, negative on ERROR */
495int osmux_init_conn(struct mgcp_conn_rtp *conn)
496{
497 if (conn_osmux_allocate_local_cid(conn) == -1)
498 return -1;
499 conn->osmux.ctrg = rate_ctr_group_alloc(conn->conn, &rate_ctr_group_osmux_desc, conn->ctrg->idx);
500
501 conn->osmux.state = OSMUX_STATE_ACTIVATING;
502 return 0;
503}
504
Philipp Maier87bd9be2017-08-22 16:35:41 +0200505/*! enable OSXMUX circuit for a specified connection.
506 * \param[in] endp mgcp endpoint (configuration)
507 * \param[in] conn connection to disable
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200508 * \param[in] addr IP address and port of remote OSMUX endpoint
Philipp Maier87bd9be2017-08-22 16:35:41 +0200509 * \returns 0 on success, -1 on ERROR */
510int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200511 const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200512{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200513 /*! If osmux is enabled, initialize the output handler. This handler is
514 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
515 * allocated based on the circuit ID (conn_net->osmux.cid), which is unique
516 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200517 * SSRC space (2^32) by the OSMUX_CID_MAX + 1 possible circuit IDs, then randomly
Philipp Maier87bd9be2017-08-22 16:35:41 +0200518 * select one value from that window. Thus, we have no chance to have
519 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
520 * similarly, for flows traveling to the MSC.
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200521 */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200522 struct in6_addr addr_unset = {};
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200523 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / (OSMUX_CID_MAX + 1);
Ericfbf78d12021-08-23 22:31:39 +0200524 uint16_t osmux_dummy = endp->trunk->cfg->osmux_dummy;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200525
Philipp Maier87bd9be2017-08-22 16:35:41 +0200526 /* Check if osmux is enabled for the specified connection */
Pau Espin Pedrol48aff622018-10-16 16:03:57 +0200527 if (conn->osmux.state != OSMUX_STATE_ACTIVATING) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200528 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200529 "conn:%s didn't negotiate Osmux, state %d\n",
530 mgcp_conn_dump(conn->conn), conn->osmux.state);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200531 return -1;
532 }
533
Pau Espin Pedroldac2ca72019-05-13 17:34:51 +0200534 /* Wait until we have the connection information from MDCX */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200535 if (memcmp(&conn->end.addr, &addr_unset,
536 conn->end.addr.u.sa.sa_family == AF_INET6 ?
537 sizeof(struct in6_addr) :
538 sizeof(struct in_addr)) == 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200539 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedroldac2ca72019-05-13 17:34:51 +0200540 "Osmux remote address/port still unknown\n");
541 return -1;
542 }
543
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200544 conn->osmux.in = osmux_handle_find_or_create(conn, rem_addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200545 if (!conn->osmux.in) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200546 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200547 "Cannot allocate input osmux handle for conn:%s\n",
548 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200549 return -1;
550 }
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200551 if (osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.remote_cid, osmux_dummy) < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200552 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200553 "Cannot open osmux circuit %u for conn:%s\n",
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200554 conn->osmux.remote_cid, mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200555 return -1;
556 }
557
Pau Espin Pedrol941e3172022-09-22 21:16:56 +0200558 conn->osmux.out = osmux_xfrm_output_alloc(conn->conn);
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200559 osmux_xfrm_output_set_rtp_ssrc(conn->osmux.out,
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200560 (conn->osmux.remote_cid * rtp_ssrc_winlen) +
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200561 (random() % rtp_ssrc_winlen));
562 osmux_xfrm_output_set_rtp_pl_type(conn->osmux.out, conn->end.codec->payload_type);
563 osmux_xfrm_output_set_tx_cb(conn->osmux.out,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200564 scheduled_from_osmux_tx_rtp_cb, conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200565
566 conn->osmux.state = OSMUX_STATE_ENABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200567
568 return 0;
569}
570
Philipp Maier87bd9be2017-08-22 16:35:41 +0200571/*! disable OSXMUX circuit for a specified connection.
572 * \param[in] conn connection to disable */
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200573void conn_osmux_disable(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200574{
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200575 OSMO_ASSERT(conn->osmux.state != OSMUX_STATE_DISABLED);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200576
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200577 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200578 "Releasing connection using local Osmux CID %u\n", conn->osmux.local_cid);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200579
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200580 struct rate_ctr_group *all_osmux_stats = conn->conn->endp->trunk->ratectr.all_osmux_conn_stats;
581 rate_ctr_inc(rate_ctr_group_get_ctr(all_osmux_stats, OSMUX_NUM_CONNECTIONS));
582
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200583 if (conn->osmux.state == OSMUX_STATE_ENABLED) {
584 /* We are closing, we don't need pending RTP packets to be transmitted */
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200585 osmux_xfrm_output_set_tx_cb(conn->osmux.out, NULL, NULL);
586 TALLOC_FREE(conn->osmux.out);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200587
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200588 osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.remote_cid);
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200589 conn->osmux.state = OSMUX_STATE_DISABLED;
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200590 osmux_handle_put(conn->osmux.in);
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200591 conn->osmux.remote_cid = 0;
592 conn->osmux.remote_cid_present = false;
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200593 }
Pau Espin Pedrold48a8112022-09-27 12:37:53 +0200594
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200595 conn_osmux_release_local_cid(conn);
Pau Espin Pedrold48a8112022-09-27 12:37:53 +0200596
597 rate_ctr_group_free(conn->osmux.ctrg);
598 conn->osmux.ctrg = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200599}
600
Philipp Maier87bd9be2017-08-22 16:35:41 +0200601/*! relase OSXMUX cid, that had been allocated to this connection.
602 * \param[in] conn connection with OSMUX cid to release */
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200603void conn_osmux_release_local_cid(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200604{
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200605 if (conn->osmux.local_cid_allocated)
606 osmux_cid_pool_put(conn->osmux.local_cid);
607 conn->osmux.local_cid = 0;
608 conn->osmux.local_cid_allocated = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200609}
610
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200611/*! allocate local OSMUX cid to connection.
612 * \param[in] conn connection for which we allocate the local OSMUX cid
613 * \returns Allocated OSMUX cid, -1 on error (no free CIDs avail).
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200614 */
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200615int conn_osmux_allocate_local_cid(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200616{
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200617 OSMO_ASSERT(conn->osmux.local_cid_allocated == false);
618 int osmux_cid = osmux_cid_pool_get_next();
619 if (osmux_cid == -1) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200620 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200621 "no available local Osmux CID to allocate!\n");
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200622 return -1;
623 }
624
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200625 conn->osmux.local_cid = (uint8_t) osmux_cid;
626 conn->osmux.local_cid_allocated = true;
Pau Espin Pedrolfa810e82019-05-06 18:54:10 +0200627 conn->type = MGCP_OSMUX_BSC;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200628 return osmux_cid;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200629}
630
Philipp Maier87bd9be2017-08-22 16:35:41 +0200631/*! send RTP dummy packet to OSMUX connection port.
632 * \param[in] endp mcgp endpoint that holds the RTP connection
633 * \param[in] conn associated RTP connection
634 * \returns bytes sent, -1 on error */
635int osmux_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200636{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200637 char ipbuf[INET6_ADDRSTRLEN];
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200638 struct osmux_hdr *osmuxh;
639 int buf_len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200640 struct in_addr addr_unset = {};
641
Philipp Maier87bd9be2017-08-22 16:35:41 +0200642 /*! The dummy packet will not be sent via the actual OSMUX connection,
643 * instead it is sent out of band to port where the remote OSMUX
644 * multplexer is listening. The goal is to ensure that the connection
645 * is kept open */
646
647 /*! We don't need to send the dummy load for osmux so often as another
648 * endpoint may have already punched the hole in the firewall. This
649 * approach is simple though. */
650
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200651 /* Wait until we have the connection information from MDCX */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200652 if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200653 return 0;
654
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200655 if (endp_osmux_state_check(endp, conn, true) < 0)
656 return 0;
657
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200658 buf_len = sizeof(struct osmux_hdr) + osmo_amr_bytes(AMR_FT_0);
659 osmuxh = (struct osmux_hdr *) alloca(buf_len);
660 memset(osmuxh, 0, buf_len);
661 osmuxh->ft = OSMUX_FT_DUMMY;
662 osmuxh->amr_ft = AMR_FT_0;
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200663 osmuxh->circuit_id = conn->osmux.remote_cid;
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200664
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200665 LOGPCONN(conn->conn, DOSMUX, LOGL_DEBUG,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200666 "sending OSMUX dummy load to %s:%u CID %u\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200667 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf),
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200668 ntohs(conn->end.rtp_port), conn->osmux.remote_cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200669
Philipp Maier87bd9be2017-08-22 16:35:41 +0200670 return mgcp_udp_send(osmux_fd.fd, &conn->end.addr,
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200671 conn->end.rtp_port, (char*)osmuxh, buf_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200672}
673
Pau Espin Pedrolbcd52e52018-09-17 12:41:28 +0200674/* bsc-nat allocates/releases the Osmux circuit ID. +7 to round up to 8 bit boundary. */
675static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1 + 7) / 8];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200676
Philipp Maier87bd9be2017-08-22 16:35:41 +0200677/*! count the number of taken OSMUX cids.
678 * \returns number of OSMUX cids in use */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200679int osmux_cid_pool_count_used(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200680{
681 int i, j, used = 0;
682
683 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
684 for (j = 0; j < 8; j++) {
685 if (osmux_cid_bitmap[i] & (1 << j))
686 used += 1;
687 }
688 }
689
690 return used;
691}
692
Philipp Maier87bd9be2017-08-22 16:35:41 +0200693/*! take a free OSMUX cid.
694 * \returns OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200695int osmux_cid_pool_get_next(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200696{
697 int i, j;
698
699 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
700 for (j = 0; j < 8; j++) {
701 if (osmux_cid_bitmap[i] & (1 << j))
702 continue;
703
704 osmux_cid_bitmap[i] |= (1 << j);
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200705 LOGP(DOSMUX, LOGL_DEBUG,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200706 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
707 return (i * 8) + j;
708 }
709 }
710
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200711 LOGP(DOSMUX, LOGL_ERROR, "All Osmux circuits are in use!\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200712 return -1;
713}
714
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200715/*! take a specific OSMUX cid.
716 * \param[in] osmux_cid OSMUX cid */
717void osmux_cid_pool_get(uint8_t osmux_cid)
718{
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200719 LOGP(DOSMUX, LOGL_DEBUG, "Allocating Osmux CID %u from pool\n", osmux_cid);
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200720 osmux_cid_bitmap[osmux_cid / 8] |= (1 << (osmux_cid % 8));
721}
722
Philipp Maier87bd9be2017-08-22 16:35:41 +0200723/*! put back a no longer used OSMUX cid.
724 * \param[in] osmux_cid OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200725void osmux_cid_pool_put(uint8_t osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200726{
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200727 LOGP(DOSMUX, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200728 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
729}
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200730
731/*! check if OSMUX cid is already taken */
732bool osmux_cid_pool_allocated(uint8_t osmux_cid)
733{
734 return !!(osmux_cid_bitmap[osmux_cid / 8] & (1 << (osmux_cid % 8)));
735}