blob: d9a10a9c3fd422f64b29bca09fad2727660c67f5 [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
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +020033static struct osmo_fd osmux_fd_v4;
34static struct osmo_fd osmux_fd_v6;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020035
36static LLIST_HEAD(osmux_handle_list);
37
38struct osmux_handle {
39 struct llist_head head;
40 struct osmux_in_handle *in;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +020041 struct osmo_sockaddr rem_addr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020042 int refcnt;
43};
44
Pau Espin Pedrolea7aaf22022-09-22 21:07:54 +020045const struct value_string osmux_state_strs[] = {
46 { OSMUX_STATE_DISABLED, "disabled" },
47 { OSMUX_STATE_ACTIVATING, "activating" },
48 { OSMUX_STATE_ENABLED, "enabled" },
49 { 0, NULL }
50};
51
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +020052static const struct rate_ctr_group_desc rate_ctr_group_osmux_desc = {
53 .group_name_prefix = "conn_osmux",
54 .group_description = "Osmux connection statistics",
55 .class_id = 1,
56 .num_ctr = ARRAY_SIZE(mgcp_conn_osmux_rate_ctr_desc),
57 .ctr_desc = mgcp_conn_osmux_rate_ctr_desc
58};
59
Pau Espin Pedrolb4a59472022-09-23 18:08:53 +020060static void rtpconn_osmux_rate_ctr_add(struct mgcp_conn_rtp *conn_rtp, int id, int inc)
61{
62 struct rate_ctr_group *conn_osmux_stats = conn_rtp->osmux.ctrg;
63 struct rate_ctr_group *trunk_osmux_stats = conn_rtp->conn->endp->trunk->ratectr.all_osmux_conn_stats;
64
65 /* add to both the per-connection and the global stats */
66 rate_ctr_add(rate_ctr_group_get_ctr(conn_osmux_stats, id), inc);
67 rate_ctr_add(rate_ctr_group_get_ctr(trunk_osmux_stats, id), inc);
68}
69
70static void rtpconn_osmux_rate_ctr_inc(struct mgcp_conn_rtp *conn_rtp, int id)
71{
72 rtpconn_osmux_rate_ctr_add(conn_rtp, id, 1);
73}
74
Philipp Maier87bd9be2017-08-22 16:35:41 +020075/* Deliver OSMUX batch to the remote end */
76static void osmux_deliver_cb(struct msgb *batch_msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020077{
78 struct osmux_handle *handle = data;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +020079 socklen_t dest_len;
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +020080 int rc, fd;
81 struct mgcp_trunk *trunk = (struct mgcp_trunk *)osmux_fd_v4.data;
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +020082 struct rate_ctr_group *all_osmux_stats = trunk->ratectr.all_osmux_conn_stats;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020083
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +020084 switch (handle->rem_addr.u.sa.sa_family) {
85 case AF_INET6:
86 dest_len = sizeof(handle->rem_addr.u.sin6);
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +020087 fd = osmux_fd_v6.fd;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +020088 break;
89 case AF_INET:
90 default:
91 dest_len = sizeof(handle->rem_addr.u.sin);
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +020092 fd = osmux_fd_v4.fd;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +020093 break;
94 }
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +020095 rc = sendto(fd, batch_msg->data, batch_msg->len, 0,
Pau Espin Pedrol4d1a52d2022-09-22 17:11:57 +020096 (struct sockaddr *)&handle->rem_addr.u.sa, dest_len);
97 if (rc < 0) {
98 char errbuf[129];
99 strerror_r(errno, errbuf, sizeof(errbuf));
100 LOGP(DOSMUX, LOGL_NOTICE, "osmux sendto(%s) failed: %s\n",
101 osmo_sockaddr_to_str(&handle->rem_addr), errbuf);
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200102 rate_ctr_inc(rate_ctr_group_get_ctr(all_osmux_stats, OSMUX_DROPPED_PACKETS_CTR));
103 } else {
104 rate_ctr_inc(rate_ctr_group_get_ctr(all_osmux_stats, OSMUX_PACKETS_TX_CTR));
Pau Espin Pedrol4d1a52d2022-09-22 17:11:57 +0200105 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200106 msgb_free(batch_msg);
107}
108
Philipp Maier87bd9be2017-08-22 16:35:41 +0200109/* Lookup existing OSMUX handle for specified destination address. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200110static struct osmux_handle *
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200111osmux_handle_find_get(const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200112{
113 struct osmux_handle *h;
114
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200115 llist_for_each_entry(h, &osmux_handle_list, head) {
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200116 if (osmo_sockaddr_cmp(&h->rem_addr, rem_addr) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200117 h->refcnt++;
Pau Espin Pedrolc61664c2022-09-09 17:03:13 +0200118 LOGP(DOSMUX, LOGL_DEBUG,
119 "Using existing OSMUX handle for addr=%s (rfcnt=%u)\n",
120 osmo_sockaddr_to_str(rem_addr), h->refcnt);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200121 return h;
122 }
123 }
124
125 return NULL;
126}
127
Philipp Maier87bd9be2017-08-22 16:35:41 +0200128/* Put down no longer needed OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200129static void osmux_handle_put(struct osmux_in_handle *in)
130{
131 struct osmux_handle *h;
132
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200133 llist_for_each_entry(h, &osmux_handle_list, head) {
134 if (h->in == in) {
Pau Espin Pedrolc61664c2022-09-09 17:03:13 +0200135 LOGP(DOSMUX, LOGL_DEBUG,
136 "Putting existing OSMUX handle for addr=%s (rfcnt=%u)\n",
137 osmo_sockaddr_to_str(&h->rem_addr), h->refcnt);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200138 if (--h->refcnt == 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200139 LOGP(DOSMUX, LOGL_INFO,
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200140 "Releasing unused osmux handle for %s\n",
141 osmo_sockaddr_to_str(&h->rem_addr));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200142 llist_del(&h->head);
Pau Espin Pedrol72eff2c2022-09-29 14:26:00 +0200143 TALLOC_FREE(h->in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200144 talloc_free(h);
145 }
146 return;
147 }
148 }
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200149 LOGP(DOSMUX, LOGL_ERROR, "Cannot find Osmux input handle %p\n", in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200150}
151
Philipp Maier87bd9be2017-08-22 16:35:41 +0200152/* Allocate free OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200153static struct osmux_handle *
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200154osmux_handle_alloc(struct mgcp_conn_rtp *conn, const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200155{
156 struct osmux_handle *h;
Pau Espin Pedrol941e3172022-09-22 21:16:56 +0200157 struct mgcp_trunk *trunk = conn->conn->endp->trunk;
158 struct mgcp_config *cfg = trunk->cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200159
Pau Espin Pedrol941e3172022-09-22 21:16:56 +0200160 h = talloc_zero(trunk, struct osmux_handle);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200161 if (!h)
162 return NULL;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200163 h->rem_addr = *rem_addr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200164 h->refcnt++;
165
Pau Espin Pedrol72eff2c2022-09-29 14:26:00 +0200166 h->in = osmux_xfrm_input_alloc(h);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200167 if (!h->in) {
168 talloc_free(h);
169 return NULL;
170 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200171 /* sequence number to start OSMUX message from */
Pau Espin Pedrol72eff2c2022-09-29 14:26:00 +0200172 osmux_xfrm_input_set_initial_seqnum(h->in, 0);
173 osmux_xfrm_input_set_batch_factor(h->in, cfg->osmux_batch);
174 /* If batch size is zero, the library defaults to 1472 bytes. */
175 osmux_xfrm_input_set_batch_size(h->in, cfg->osmux_batch_size);
176 osmux_xfrm_input_set_deliver_cb(h->in, osmux_deliver_cb, h);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200177
178 llist_add(&h->head, &osmux_handle_list);
179
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200180 LOGP(DOSMUX, LOGL_DEBUG, "Created new OSMUX handle for rem_addr=%s\n",
181 osmo_sockaddr_to_str(rem_addr));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200182
183 return h;
184}
185
Philipp Maier87bd9be2017-08-22 16:35:41 +0200186/* Lookup existing handle for a specified address, if the handle can not be
Harald Welte1d1b98f2017-12-25 10:03:40 +0100187 * found, the function will automatically allocate one */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200188static struct osmux_in_handle *
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200189osmux_handle_find_or_create(struct mgcp_conn_rtp *conn, const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200190{
191 struct osmux_handle *h;
192
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200193 h = osmux_handle_find_get(rem_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200194 if (h != NULL)
195 return h->in;
196
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200197 h = osmux_handle_alloc(conn, rem_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200198 if (h == NULL)
199 return NULL;
200
201 return h->in;
202}
203
Philipp Maier87bd9be2017-08-22 16:35:41 +0200204/*! send RTP packet through OSMUX connection.
205 * \param[in] buf rtp data
206 * \param[in] buf_len length of rtp data
207 * \param[in] conn associated RTP connection
208 * \returns 0 on success, -1 on ERROR */
209int osmux_xfrm_to_osmux(char *buf, int buf_len, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200210{
211 int ret;
212 struct msgb *msg;
213
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200214 if (!conn->end.output_enabled) {
Pau Espin Pedrol432ee9d2022-09-23 18:18:02 +0200215 rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_DROPPED_CTR);
Pau Espin Pedrole39ae872022-09-09 17:36:33 +0200216 return -1;
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200217 }
Pau Espin Pedrole39ae872022-09-09 17:36:33 +0200218
Pau Espin Pedrol8b0a6142022-09-09 17:34:31 +0200219 if (conn->osmux.state != OSMUX_STATE_ENABLED) {
220 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 +0200221 conn->osmux.remote_cid);
Pau Espin Pedrol432ee9d2022-09-23 18:18:02 +0200222 rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_DROPPED_CTR);
Pau Espin Pedrol8b0a6142022-09-09 17:34:31 +0200223 return -1;
224 }
225
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200226 msg = msgb_alloc(4096, "RTP");
227 if (!msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200228 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200229
Philipp Maier87bd9be2017-08-22 16:35:41 +0200230 memcpy(msg->data, buf, buf_len);
231 msgb_put(msg, buf_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200232
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200233 while ((ret = osmux_xfrm_input(conn->osmux.in, msg, conn->osmux.remote_cid)) > 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200234 /* batch full, build and deliver it */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200235 osmux_xfrm_input_deliver(conn->osmux.in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200236 }
Pau Espin Pedrol432ee9d2022-09-23 18:18:02 +0200237 if (ret < 0) {
238 rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_DROPPED_CTR);
239 } else {
240 rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_CTR);
241 rtpconn_osmux_rate_ctr_add(conn, OSMUX_AMR_OCTETS_TX_CTR, buf_len - sizeof(struct rtp_hdr));
242 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200243 return 0;
244}
245
Philipp Maier87bd9be2017-08-22 16:35:41 +0200246/* Lookup the endpoint that corresponds to the specified address (port) */
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200247static struct mgcp_conn_rtp*
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200248osmux_conn_lookup(struct mgcp_trunk *trunk, uint8_t local_cid, const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200249{
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200250 struct mgcp_endpoint *endp;
251 struct mgcp_conn *conn = NULL;
Pau Espin Pedrolb0ea7972022-10-03 15:56:25 +0200252 struct mgcp_conn_rtp *conn_rtp;
253 struct osmux_handle *h;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200254 int i;
255
Philipp Maierd19de2e2020-06-03 13:55:33 +0200256 for (i = 0; i < trunk->number_endpoints; i++) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200257
Philipp Maierd19de2e2020-06-03 13:55:33 +0200258 endp = trunk->endpoints[i];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200259
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200260 llist_for_each_entry(conn, &endp->conns, entry) {
261 if (conn->type != MGCP_CONN_TYPE_RTP)
262 continue;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200263
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200264 conn_rtp = &conn->u.rtp;
265 if (!mgcp_conn_rtp_is_osmux(conn_rtp))
266 continue;
267
Pau Espin Pedrolb0ea7972022-10-03 15:56:25 +0200268 /* Current implementation sets remote address & port for
269 * the conn based on src address received on the socket
270 * for the CID, in order to workaround NATs.
271 * Once the conn is fully established (remote address is
272 * known), validate the remote address doesn't change: */
273 if (conn_rtp->osmux.state == OSMUX_STATE_ENABLED) {
274 h = osmux_xfrm_input_get_deliver_cb_data(conn_rtp->osmux.in);
275 if (osmo_sockaddr_cmp(&h->rem_addr, rem_addr) != 0)
276 continue;
277 }
278 /* else: select based on CID only, to learn rem addr in NAT-based scenarios.
279 * FIXME: This should be configurable, have some sort of "osmux nat (on|off)" */
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200280
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200281 if (conn_rtp->osmux.local_cid == local_cid)
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200282 return conn_rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200283 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200284 }
285
Pau Espin Pedrol48fb1762022-09-29 15:50:07 +0200286 LOGP(DOSMUX, LOGL_DEBUG, "Cannot find osmux conn with rem_addr=%s local_cid=%d\n",
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200287 osmo_sockaddr_to_str(rem_addr), local_cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200288
289 return NULL;
290}
291
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200292static void scheduled_from_osmux_tx_rtp_cb(struct msgb *msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200293{
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200294 struct mgcp_conn_rtp *conn = data;
295 struct mgcp_endpoint *endp = conn->conn->endp;
Pau Espin Pedrol69f15e42022-10-03 16:18:34 +0200296 struct osmux_handle *h = osmux_xfrm_input_get_deliver_cb_data(conn->osmux.in);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200297 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
298 *mc = (struct osmo_rtp_msg_ctx){
299 .proto = MGCP_PROTO_RTP,
300 .conn_src = conn,
Pau Espin Pedrol69f15e42022-10-03 16:18:34 +0200301 .from_addr = &h->rem_addr,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200302 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200303
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200304 endp->type->dispatch_rtp_cb(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200305 msgb_free(msg);
306}
307
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200308static struct msgb *osmux_recv(struct osmo_fd *ofd, struct osmo_sockaddr *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200309{
310 struct msgb *msg;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200311 socklen_t slen = sizeof(addr->u.sas);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200312 int ret;
313
314 msg = msgb_alloc(4096, "OSMUX");
315 if (!msg) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200316 LOGP(DOSMUX, LOGL_ERROR, "cannot allocate message\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200317 return NULL;
318 }
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200319 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0, &addr->u.sa, &slen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200320 if (ret <= 0) {
321 msgb_free(msg);
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200322 LOGP(DOSMUX, LOGL_ERROR, "cannot receive message\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200323 return NULL;
324 }
325 msgb_put(msg, ret);
326
327 return msg;
328}
329
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200330/* Updates endp osmux state and returns 0 if it can process messages, -1 otherwise */
331static int endp_osmux_state_check(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
332 bool sending)
333{
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200334 struct osmo_sockaddr rem_addr;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200335
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200336 switch(conn->osmux.state) {
337 case OSMUX_STATE_ACTIVATING:
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200338 rem_addr = conn->end.addr;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200339 if (osmux_enable_conn(endp, conn, &rem_addr) < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200340 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200341 "Could not enable osmux for conn on %s: %s\n",
342 sending ? "sent" : "received",
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200343 mgcp_conn_dump(conn->conn));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200344 return -1;
345 }
Pau Espin Pedrolc5bbb782022-09-19 16:46:02 +0200346 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200347 "Osmux %s CID %u towards %s is now enabled\n",
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200348 sending ? "sent" : "received",
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200349 sending ? conn->osmux.remote_cid : conn->osmux.local_cid,
350 osmo_sockaddr_to_str(&rem_addr));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200351 return 0;
352 case OSMUX_STATE_ENABLED:
353 return 0;
354 default:
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200355 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200356 "Osmux %s in conn %s without full negotiation, state %d\n",
357 sending ? "sent" : "received",
358 mgcp_conn_dump(conn->conn), conn->osmux.state);
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200359 return -1;
360 }
361}
362
Pau Espin Pedrole7406652022-10-04 11:58:56 +0200363/* Old versions of osmux used to send dummy packets [0x23 0x<CID>] to punch the
364 * hole in the NAT. Let's handle them speficially. */
365static int osmux_handle_legacy_dummy(struct mgcp_trunk *trunk, const struct osmo_sockaddr *rem_addr,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200366 struct msgb *msg)
367{
Pau Espin Pedrole7406652022-10-04 11:58:56 +0200368 uint8_t osmux_cid = msg->data[1];
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200369 struct mgcp_conn_rtp *conn;
370
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200371 conn = osmux_conn_lookup(trunk, osmux_cid, rem_addr);
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200372 if (!conn) {
Pau Espin Pedrol48fb1762022-09-29 15:50:07 +0200373 LOGP(DOSMUX, LOGL_DEBUG,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200374 "Cannot find conn for Osmux CID %d\n", osmux_cid);
375 goto out;
376 }
377
378 endp_osmux_state_check(conn->conn->endp, conn, false);
379 /* Only needed to punch hole in firewall, it can be dropped */
380out:
381 msgb_free(msg);
382 return 0;
383}
384
Pau Espin Pedrol3fbf0352022-09-22 19:01:33 +0200385#define osmux_chunk_length(msg, rem) ((rem) - (msg)->len)
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200386static int osmux_read_fd_cb(struct osmo_fd *ofd, unsigned int what)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200387{
388 struct msgb *msg;
389 struct osmux_hdr *osmuxh;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200390 struct osmo_sockaddr rem_addr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200391 uint32_t rem;
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200392 struct mgcp_trunk *trunk = ofd->data;
393 struct rate_ctr_group *all_rtp_stats = trunk->ratectr.all_osmux_conn_stats;
Pau Espin Pedrol2dd2b332022-10-03 18:10:55 +0200394 char addr_str[64];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200395
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200396 msg = osmux_recv(ofd, &rem_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200397 if (!msg)
398 return -1;
399
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200400 rate_ctr_inc(rate_ctr_group_get_ctr(all_rtp_stats, OSMUX_PACKETS_RX_CTR));
Pau Espin Pedrol2dd2b332022-10-03 18:10:55 +0200401 osmo_sockaddr_to_str_buf(addr_str, sizeof(addr_str), &rem_addr);
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200402
Pau Espin Pedrol928a20b2022-09-23 15:38:24 +0200403 if (trunk->cfg->osmux_use == OSMUX_USAGE_OFF) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200404 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrol2dd2b332022-10-03 18:10:55 +0200405 "Peer %s wants to use Osmux but MGCP Client did not request it\n",
406 addr_str);
Pau Espin Pedrold14163e2018-10-16 15:48:59 +0200407 goto out;
408 }
409
Pau Espin Pedrole7406652022-10-04 11:58:56 +0200410 /* Catch legacy dummy message and process them separately: */
411 if (msg->len == 2 && msg->data[0] == MGCP_DUMMY_LOAD)
412 return osmux_handle_legacy_dummy(trunk, &rem_addr, msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200413
414 rem = msg->len;
415 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200416 struct mgcp_endpoint *endp;
417 struct mgcp_conn_rtp *conn_src;
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200418 conn_src = osmux_conn_lookup(trunk, osmuxh->circuit_id,
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200419 &rem_addr);
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200420 if (!conn_src) {
Pau Espin Pedrol48fb1762022-09-29 15:50:07 +0200421 LOGP(DOSMUX, LOGL_DEBUG,
Pau Espin Pedrol2dd2b332022-10-03 18:10:55 +0200422 "Cannot find a src conn for %s CID=%d\n",
423 addr_str, osmuxh->circuit_id);
Pau Espin Pedrol1c5fcb02022-09-29 15:59:15 +0200424 rem = msg->len;
425 continue;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200426 }
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200427 endp = conn_src->conn->endp;
Pau Espin Pedrolde133112020-09-08 17:51:36 +0200428 mgcp_conn_watchdog_kick(conn_src->conn);
429
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200430 if (endp_osmux_state_check(endp, conn_src, false) == 0) {
Pau Espin Pedrolb4a59472022-09-23 18:08:53 +0200431 rtpconn_osmux_rate_ctr_inc(conn_src, OSMUX_CHUNKS_RX_CTR);
432 rtpconn_osmux_rate_ctr_add(conn_src, 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 Pedrolc1ad7fd2022-10-06 10:32:07 +0200443int osmux_init(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
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200451 osmo_fd_setup(&osmux_fd_v4, -1, OSMO_FD_READ, osmux_read_fd_cb, trunk, 0);
452 osmo_fd_setup(&osmux_fd_v6, -1, OSMO_FD_READ, osmux_read_fd_cb, trunk, 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200453
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200454 if (cfg->osmux_addr_v4) {
455 ret = mgcp_create_bind(cfg->osmux_addr_v4, &osmux_fd_v4, cfg->osmux_port,
456 cfg->endp_dscp, cfg->endp_priority);
457 if (ret < 0) {
458 LOGP(DOSMUX, LOGL_ERROR, "Cannot bind OSMUX IPv4 socket to %s:%u\n",
459 cfg->osmux_addr_v4, cfg->osmux_port);
460 return ret;
461 }
462
463 ret = osmo_fd_register(&osmux_fd_v4);
464 if (ret < 0) {
465 LOGP(DOSMUX, LOGL_ERROR, "Cannot register OSMUX IPv4 socket %s\n",
466 osmo_sock_get_name2(osmux_fd_v4.fd));
467 return ret;
468 }
469 LOGP(DOSMUX, LOGL_INFO, "OSMUX IPv4 socket listening on %s\n",
470 osmo_sock_get_name2(osmux_fd_v4.fd));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200471 }
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200472 if (cfg->osmux_addr_v6) {
473 ret = mgcp_create_bind(cfg->osmux_addr_v6, &osmux_fd_v6, cfg->osmux_port,
474 cfg->endp_dscp, cfg->endp_priority);
475 if (ret < 0) {
Pau Espin Pedrol37799192022-10-06 10:29:47 +0200476 LOGP(DOSMUX, LOGL_ERROR, "Cannot bind OSMUX IPv6 socket to [%s]:%u\n",
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200477 cfg->osmux_addr_v6, cfg->osmux_port);
478 return ret;
479 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200480
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200481 ret = osmo_fd_register(&osmux_fd_v6);
482 if (ret < 0) {
483 LOGP(DOSMUX, LOGL_ERROR, "Cannot register OSMUX IPv6 socket %s\n",
484 osmo_sock_get_name2(osmux_fd_v6.fd));
485 return ret;
486 }
487 LOGP(DOSMUX, LOGL_INFO, "OSMUX IPv6 socket listening on %s\n",
488 osmo_sock_get_name2(osmux_fd_v6.fd));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200489 }
Pau Espin Pedrolb7f52b42022-09-25 00:14:03 +0200490 cfg->osmux_initialized = true;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200491 return 0;
492}
493
Pau Espin Pedrold48a8112022-09-27 12:37:53 +0200494/*! Initialize Osmux bits of a conn.
495 * \param[in] conn Osmux connection to initialize
496 * \returns 0 on success, negative on ERROR */
497int osmux_init_conn(struct mgcp_conn_rtp *conn)
498{
499 if (conn_osmux_allocate_local_cid(conn) == -1)
500 return -1;
501 conn->osmux.ctrg = rate_ctr_group_alloc(conn->conn, &rate_ctr_group_osmux_desc, conn->ctrg->idx);
502
503 conn->osmux.state = OSMUX_STATE_ACTIVATING;
504 return 0;
505}
506
Philipp Maier87bd9be2017-08-22 16:35:41 +0200507/*! enable OSXMUX circuit for a specified connection.
508 * \param[in] endp mgcp endpoint (configuration)
509 * \param[in] conn connection to disable
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200510 * \param[in] addr IP address and port of remote OSMUX endpoint
Philipp Maier87bd9be2017-08-22 16:35:41 +0200511 * \returns 0 on success, -1 on ERROR */
512int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200513 const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200514{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200515 /*! If osmux is enabled, initialize the output handler. This handler is
516 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
517 * allocated based on the circuit ID (conn_net->osmux.cid), which is unique
518 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200519 * SSRC space (2^32) by the OSMUX_CID_MAX + 1 possible circuit IDs, then randomly
Philipp Maier87bd9be2017-08-22 16:35:41 +0200520 * select one value from that window. Thus, we have no chance to have
521 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
522 * similarly, for flows traveling to the MSC.
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200523 */
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200524 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / (OSMUX_CID_MAX + 1);
Ericfbf78d12021-08-23 22:31:39 +0200525 uint16_t osmux_dummy = endp->trunk->cfg->osmux_dummy;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200526
Philipp Maier87bd9be2017-08-22 16:35:41 +0200527 /* Check if osmux is enabled for the specified connection */
Pau Espin Pedrol48aff622018-10-16 16:03:57 +0200528 if (conn->osmux.state != OSMUX_STATE_ACTIVATING) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200529 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200530 "conn:%s didn't negotiate Osmux, state %d\n",
531 mgcp_conn_dump(conn->conn), conn->osmux.state);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200532 return -1;
533 }
534
Pau Espin Pedroldac2ca72019-05-13 17:34:51 +0200535 /* Wait until we have the connection information from MDCX */
Pau Espin Pedrole56ec4a2022-10-04 14:35:00 +0200536 if (!mgcp_rtp_end_remote_addr_available(&conn->end)) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200537 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrole7de1372022-10-06 10:36:32 +0200538 "Osmux remote address/port still unknown\n");
Pau Espin Pedroldac2ca72019-05-13 17:34:51 +0200539 return -1;
540 }
541
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200542 conn->osmux.in = osmux_handle_find_or_create(conn, rem_addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200543 if (!conn->osmux.in) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200544 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrole7de1372022-10-06 10:36:32 +0200545 "Cannot allocate input osmux handle for conn:%s\n",
546 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200547 return -1;
548 }
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200549 if (osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.remote_cid, osmux_dummy) < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200550 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrole7de1372022-10-06 10:36:32 +0200551 "Cannot open osmux circuit %u for conn:%s\n",
552 conn->osmux.remote_cid, mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200553 return -1;
554 }
555
Pau Espin Pedrol941e3172022-09-22 21:16:56 +0200556 conn->osmux.out = osmux_xfrm_output_alloc(conn->conn);
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200557 osmux_xfrm_output_set_rtp_ssrc(conn->osmux.out,
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200558 (conn->osmux.remote_cid * rtp_ssrc_winlen) +
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200559 (random() % rtp_ssrc_winlen));
560 osmux_xfrm_output_set_rtp_pl_type(conn->osmux.out, conn->end.codec->payload_type);
561 osmux_xfrm_output_set_tx_cb(conn->osmux.out,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200562 scheduled_from_osmux_tx_rtp_cb, conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200563
564 conn->osmux.state = OSMUX_STATE_ENABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200565
566 return 0;
567}
568
Philipp Maier87bd9be2017-08-22 16:35:41 +0200569/*! disable OSXMUX circuit for a specified connection.
570 * \param[in] conn connection to disable */
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200571void conn_osmux_disable(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200572{
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200573 OSMO_ASSERT(conn->osmux.state != OSMUX_STATE_DISABLED);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200574
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200575 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200576 "Releasing connection using local Osmux CID %u\n", conn->osmux.local_cid);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200577
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200578 struct rate_ctr_group *all_osmux_stats = conn->conn->endp->trunk->ratectr.all_osmux_conn_stats;
579 rate_ctr_inc(rate_ctr_group_get_ctr(all_osmux_stats, OSMUX_NUM_CONNECTIONS));
580
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200581 if (conn->osmux.state == OSMUX_STATE_ENABLED) {
582 /* We are closing, we don't need pending RTP packets to be transmitted */
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200583 osmux_xfrm_output_set_tx_cb(conn->osmux.out, NULL, NULL);
584 TALLOC_FREE(conn->osmux.out);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200585
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200586 osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.remote_cid);
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200587 conn->osmux.state = OSMUX_STATE_DISABLED;
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200588 osmux_handle_put(conn->osmux.in);
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200589 conn->osmux.remote_cid = 0;
590 conn->osmux.remote_cid_present = false;
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200591 }
Pau Espin Pedrold48a8112022-09-27 12:37:53 +0200592
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200593 conn_osmux_release_local_cid(conn);
Pau Espin Pedrold48a8112022-09-27 12:37:53 +0200594
595 rate_ctr_group_free(conn->osmux.ctrg);
596 conn->osmux.ctrg = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200597}
598
Philipp Maier87bd9be2017-08-22 16:35:41 +0200599/*! relase OSXMUX cid, that had been allocated to this connection.
600 * \param[in] conn connection with OSMUX cid to release */
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200601void conn_osmux_release_local_cid(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200602{
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200603 if (conn->osmux.local_cid_allocated)
604 osmux_cid_pool_put(conn->osmux.local_cid);
605 conn->osmux.local_cid = 0;
606 conn->osmux.local_cid_allocated = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200607}
608
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200609/*! allocate local OSMUX cid to connection.
610 * \param[in] conn connection for which we allocate the local OSMUX cid
611 * \returns Allocated OSMUX cid, -1 on error (no free CIDs avail).
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200612 */
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200613int conn_osmux_allocate_local_cid(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200614{
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200615 OSMO_ASSERT(conn->osmux.local_cid_allocated == false);
616 int osmux_cid = osmux_cid_pool_get_next();
617 if (osmux_cid == -1) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200618 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200619 "no available local Osmux CID to allocate!\n");
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200620 return -1;
621 }
622
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200623 conn->osmux.local_cid = (uint8_t) osmux_cid;
624 conn->osmux.local_cid_allocated = true;
Pau Espin Pedrol9d939b62022-10-03 16:59:20 +0200625 conn->type = MGCP_RTP_OSMUX;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200626 return osmux_cid;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200627}
628
Philipp Maier87bd9be2017-08-22 16:35:41 +0200629/*! send RTP dummy packet to OSMUX connection port.
630 * \param[in] endp mcgp endpoint that holds the RTP connection
631 * \param[in] conn associated RTP connection
632 * \returns bytes sent, -1 on error */
633int osmux_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200634{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200635 char ipbuf[INET6_ADDRSTRLEN];
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200636 struct osmux_hdr *osmuxh;
637 int buf_len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200638
Philipp Maier87bd9be2017-08-22 16:35:41 +0200639 /*! The dummy packet will not be sent via the actual OSMUX connection,
640 * instead it is sent out of band to port where the remote OSMUX
641 * multplexer is listening. The goal is to ensure that the connection
642 * is kept open */
643
644 /*! We don't need to send the dummy load for osmux so often as another
645 * endpoint may have already punched the hole in the firewall. This
646 * approach is simple though. */
647
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200648 /* Wait until we have the connection information from MDCX */
Pau Espin Pedrole56ec4a2022-10-04 14:35:00 +0200649 if (!mgcp_rtp_end_remote_addr_available(&conn->end))
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200650 return 0;
651
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200652 if (endp_osmux_state_check(endp, conn, true) < 0)
653 return 0;
654
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200655 buf_len = sizeof(struct osmux_hdr) + osmo_amr_bytes(AMR_FT_0);
656 osmuxh = (struct osmux_hdr *) alloca(buf_len);
657 memset(osmuxh, 0, buf_len);
658 osmuxh->ft = OSMUX_FT_DUMMY;
659 osmuxh->amr_ft = AMR_FT_0;
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200660 osmuxh->circuit_id = conn->osmux.remote_cid;
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200661
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200662 LOGPCONN(conn->conn, DOSMUX, LOGL_DEBUG,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200663 "sending OSMUX dummy load to %s:%u CID %u\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200664 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200665 osmo_sockaddr_port(&conn->end.addr.u.sa), conn->osmux.remote_cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200666
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200667 return mgcp_udp_send(osmux_fd_v4.fd, &conn->end.addr, (char *)osmuxh, buf_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200668}
669
Pau Espin Pedrolbcd52e52018-09-17 12:41:28 +0200670/* bsc-nat allocates/releases the Osmux circuit ID. +7 to round up to 8 bit boundary. */
671static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1 + 7) / 8];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200672
Philipp Maier87bd9be2017-08-22 16:35:41 +0200673/*! count the number of taken OSMUX cids.
674 * \returns number of OSMUX cids in use */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200675int osmux_cid_pool_count_used(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200676{
677 int i, j, used = 0;
678
679 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
680 for (j = 0; j < 8; j++) {
681 if (osmux_cid_bitmap[i] & (1 << j))
682 used += 1;
683 }
684 }
685
686 return used;
687}
688
Philipp Maier87bd9be2017-08-22 16:35:41 +0200689/*! take a free OSMUX cid.
690 * \returns OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200691int osmux_cid_pool_get_next(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200692{
693 int i, j;
694
695 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
696 for (j = 0; j < 8; j++) {
697 if (osmux_cid_bitmap[i] & (1 << j))
698 continue;
699
700 osmux_cid_bitmap[i] |= (1 << j);
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200701 LOGP(DOSMUX, LOGL_DEBUG,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200702 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
703 return (i * 8) + j;
704 }
705 }
706
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200707 LOGP(DOSMUX, LOGL_ERROR, "All Osmux circuits are in use!\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200708 return -1;
709}
710
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200711/*! take a specific OSMUX cid.
712 * \param[in] osmux_cid OSMUX cid */
713void osmux_cid_pool_get(uint8_t osmux_cid)
714{
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200715 LOGP(DOSMUX, LOGL_DEBUG, "Allocating Osmux CID %u from pool\n", osmux_cid);
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200716 osmux_cid_bitmap[osmux_cid / 8] |= (1 << (osmux_cid % 8));
717}
718
Philipp Maier87bd9be2017-08-22 16:35:41 +0200719/*! put back a no longer used OSMUX cid.
720 * \param[in] osmux_cid OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200721void osmux_cid_pool_put(uint8_t osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200722{
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200723 LOGP(DOSMUX, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200724 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
725}
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200726
727/*! check if OSMUX cid is already taken */
728bool osmux_cid_pool_allocated(uint8_t osmux_cid)
729{
730 return !!(osmux_cid_bitmap[osmux_cid / 8] & (1 << (osmux_cid % 8)));
731}