blob: 185aecd29501d40d1040d4a3f7f7679d017f5870 [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 Pedrol887c93e2022-10-06 12:36:03 +0200154osmux_handle_alloc(const struct mgcp_trunk *trunk, const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200155{
156 struct osmux_handle *h;
Pau Espin Pedrol887c93e2022-10-06 12:36:03 +0200157 const struct mgcp_config *cfg = trunk->cfg;
Pau Espin Pedrol0cbed362022-11-17 18:26:44 +0100158 char name[128] = "r=";
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 }
Pau Espin Pedrol0cbed362022-11-17 18:26:44 +0100171
172 osmo_sockaddr_to_str_buf(name + 2, sizeof(name) - 2, rem_addr);
173 osmux_xfrm_input_set_name(h->in, name);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200174 /* sequence number to start OSMUX message from */
Pau Espin Pedrol72eff2c2022-09-29 14:26:00 +0200175 osmux_xfrm_input_set_initial_seqnum(h->in, 0);
Pau Espin Pedrol36413c02022-10-12 17:58:17 +0200176 osmux_xfrm_input_set_batch_factor(h->in, cfg->osmux.batch_factor);
Pau Espin Pedrol72eff2c2022-09-29 14:26:00 +0200177 /* If batch size is zero, the library defaults to 1472 bytes. */
Pau Espin Pedrol36413c02022-10-12 17:58:17 +0200178 osmux_xfrm_input_set_batch_size(h->in, cfg->osmux.batch_size);
Pau Espin Pedrol72eff2c2022-09-29 14:26:00 +0200179 osmux_xfrm_input_set_deliver_cb(h->in, osmux_deliver_cb, h);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200180
181 llist_add(&h->head, &osmux_handle_list);
182
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200183 LOGP(DOSMUX, LOGL_DEBUG, "Created new OSMUX handle for rem_addr=%s\n",
184 osmo_sockaddr_to_str(rem_addr));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200185
186 return h;
187}
188
Philipp Maier87bd9be2017-08-22 16:35:41 +0200189/* Lookup existing handle for a specified address, if the handle can not be
Harald Welte1d1b98f2017-12-25 10:03:40 +0100190 * found, the function will automatically allocate one */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200191static struct osmux_in_handle *
Pau Espin Pedrol887c93e2022-10-06 12:36:03 +0200192osmux_handle_find_or_create(const struct mgcp_trunk *trunk, const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200193{
194 struct osmux_handle *h;
195
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200196 h = osmux_handle_find_get(rem_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200197 if (h != NULL)
198 return h->in;
199
Pau Espin Pedrol887c93e2022-10-06 12:36:03 +0200200 h = osmux_handle_alloc(trunk, rem_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200201 if (h == NULL)
202 return NULL;
203
204 return h->in;
205}
206
Philipp Maier87bd9be2017-08-22 16:35:41 +0200207/*! send RTP packet through OSMUX connection.
Philipp Maier87bd9be2017-08-22 16:35:41 +0200208 * \param[in] conn associated RTP connection
Pau Espin Pedrold1e94c72022-10-25 13:49:40 +0200209 * \param[in] msg msgb containing an RTP AMR packet
Philipp Maier87bd9be2017-08-22 16:35:41 +0200210 * \returns 0 on success, -1 on ERROR */
Pau Espin Pedrold1e94c72022-10-25 13:49:40 +0200211int conn_osmux_send_rtp(struct mgcp_conn_rtp *conn, struct msgb *msg)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200212{
213 int ret;
Pau Espin Pedrold1e94c72022-10-25 13:49:40 +0200214 struct msgb *msg2;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200215
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200216 if (!conn->end.output_enabled) {
Pau Espin Pedrol432ee9d2022-09-23 18:18:02 +0200217 rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_DROPPED_CTR);
Pau Espin Pedrole39ae872022-09-09 17:36:33 +0200218 return -1;
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200219 }
Pau Espin Pedrole39ae872022-09-09 17:36:33 +0200220
Pau Espin Pedrol8b0a6142022-09-09 17:34:31 +0200221 if (conn->osmux.state != OSMUX_STATE_ENABLED) {
222 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 +0200223 conn->osmux.remote_cid);
Pau Espin Pedrol432ee9d2022-09-23 18:18:02 +0200224 rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_DROPPED_CTR);
Pau Espin Pedrol8b0a6142022-09-09 17:34:31 +0200225 return -1;
226 }
227
Pau Espin Pedrold1e94c72022-10-25 13:49:40 +0200228 /* msg is not owned by us and will be freed by the caller stack upon return: */
229 msg2 = msgb_copy_c(conn->conn, msg, "osmux-rtp-send");
230 if (!msg2)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200231 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200232
Pau Espin Pedrol377d3912022-10-25 13:52:19 +0200233 /* Osmux implementation works with AMR OA only, make sure we convert to it if needed: */
234 if (amr_oa_bwe_convert(conn->conn->endp, msg2, true) < 0) {
235 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
236 "Error converting to AMR octet-aligned mode\n");
237 return -1;
238 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200239
Pau Espin Pedrold1e94c72022-10-25 13:49:40 +0200240 while ((ret = osmux_xfrm_input(conn->osmux.in, msg2, conn->osmux.remote_cid)) > 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200241 /* batch full, build and deliver it */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200242 osmux_xfrm_input_deliver(conn->osmux.in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200243 }
Pau Espin Pedrol432ee9d2022-09-23 18:18:02 +0200244 if (ret < 0) {
245 rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_DROPPED_CTR);
246 } else {
247 rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_CTR);
Pau Espin Pedrold1e94c72022-10-25 13:49:40 +0200248 rtpconn_osmux_rate_ctr_add(conn, OSMUX_AMR_OCTETS_TX_CTR, msgb_length(msg2) - sizeof(struct rtp_hdr));
Pau Espin Pedrol432ee9d2022-09-23 18:18:02 +0200249 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200250 return 0;
251}
252
Philipp Maier87bd9be2017-08-22 16:35:41 +0200253/* Lookup the endpoint that corresponds to the specified address (port) */
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200254static struct mgcp_conn_rtp*
Pau Espin Pedrol887c93e2022-10-06 12:36:03 +0200255osmux_conn_lookup(const struct mgcp_trunk *trunk, uint8_t local_cid, const struct osmo_sockaddr *rem_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200256{
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200257 struct mgcp_endpoint *endp;
258 struct mgcp_conn *conn = NULL;
Pau Espin Pedrolb0ea7972022-10-03 15:56:25 +0200259 struct mgcp_conn_rtp *conn_rtp;
260 struct osmux_handle *h;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200261 int i;
262
Philipp Maierd19de2e2020-06-03 13:55:33 +0200263 for (i = 0; i < trunk->number_endpoints; i++) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200264
Philipp Maierd19de2e2020-06-03 13:55:33 +0200265 endp = trunk->endpoints[i];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200266
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200267 llist_for_each_entry(conn, &endp->conns, entry) {
268 if (conn->type != MGCP_CONN_TYPE_RTP)
269 continue;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200270
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200271 conn_rtp = &conn->u.rtp;
272 if (!mgcp_conn_rtp_is_osmux(conn_rtp))
273 continue;
274
Pau Espin Pedrol833281d2022-10-06 17:41:22 +0200275 /* If Osmux peer is behind NAT:
276 * + state OSMUX_STATE_ACTIVATING: we cannot validate the remote address & port for
277 * the conn since we actually set conn's remote IP address from the info we received
278 * from this source address. NOTE: This means if Osmux peer is behind NAT we cannot
279 * have more than 1 osmux trunk since it's not possible to differentiate based on
280 * remote address.
281 * + state OSMUX_STATE_ENABLED: the conn is fully established (remote address is known),
282 * it is now posssible to validate the remote address doesn't change.
283 * If Osmux peer is not behind NAT:
284 * We can always validate the remote IP address is matching the one we received in
285 * CRCX/MDCX, we can support several parallel Osmux trunks since we can differentiate same CID
286 * based on remote IP addr+port. However, if Osmux peer is not behind NAT it means it will go
287 * into OSMUX_STATE_ENABLED as soon as the remote address is known, meaning if the conn is not
288 * in that state we cannot (nor want) validate the IP address, we want to skip it until it is
289 * ready (to avoid 3rd party data injections).
290 * TLDR: When in OSMUX_STATE_ENABLED we can always validate remote address+port.
291 * When in OSMUX_STATE_ACTIVATING, in case peer behind NAT we select it,
292 * in case peer NOT behind NAT we skip it.
293 */
Pau Espin Pedrolb0ea7972022-10-03 15:56:25 +0200294 if (conn_rtp->osmux.state == OSMUX_STATE_ENABLED) {
295 h = osmux_xfrm_input_get_deliver_cb_data(conn_rtp->osmux.in);
296 if (osmo_sockaddr_cmp(&h->rem_addr, rem_addr) != 0)
297 continue;
Pau Espin Pedrol36413c02022-10-12 17:58:17 +0200298 } else if (!trunk->cfg->osmux.peer_behind_nat) {
Pau Espin Pedrol833281d2022-10-06 17:41:22 +0200299 LOGPCONN(conn, DOSMUX, LOGL_DEBUG, "osmux_conn_lookup(rem_addr=%s local_cid=%d): Skipping because not (yet) ENABLED\n",
300 osmo_sockaddr_to_str(rem_addr), local_cid);
301 continue; /* skip, read above */
302 } /* else: continue CID validation below: */
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200303
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200304 if (conn_rtp->osmux.local_cid == local_cid)
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200305 return conn_rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200306 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200307 }
308
Pau Espin Pedrol48fb1762022-09-29 15:50:07 +0200309 LOGP(DOSMUX, LOGL_DEBUG, "Cannot find osmux conn with rem_addr=%s local_cid=%d\n",
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200310 osmo_sockaddr_to_str(rem_addr), local_cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200311
312 return NULL;
313}
314
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200315static void scheduled_from_osmux_tx_rtp_cb(struct msgb *msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200316{
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200317 struct mgcp_conn_rtp *conn = data;
Pau Espin Pedrol887c93e2022-10-06 12:36:03 +0200318 const struct mgcp_endpoint *endp = conn->conn->endp;
Pau Espin Pedrol69f15e42022-10-03 16:18:34 +0200319 struct osmux_handle *h = osmux_xfrm_input_get_deliver_cb_data(conn->osmux.in);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200320 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
321 *mc = (struct osmo_rtp_msg_ctx){
322 .proto = MGCP_PROTO_RTP,
323 .conn_src = conn,
Pau Espin Pedrol69f15e42022-10-03 16:18:34 +0200324 .from_addr = &h->rem_addr,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200325 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200326
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200327 endp->type->dispatch_rtp_cb(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200328 msgb_free(msg);
329}
330
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200331static struct msgb *osmux_recv(struct osmo_fd *ofd, struct osmo_sockaddr *addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200332{
333 struct msgb *msg;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200334 socklen_t slen = sizeof(addr->u.sas);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200335 int ret;
336
337 msg = msgb_alloc(4096, "OSMUX");
338 if (!msg) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200339 LOGP(DOSMUX, LOGL_ERROR, "cannot allocate message\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200340 return NULL;
341 }
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200342 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0, &addr->u.sa, &slen);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200343 if (ret <= 0) {
344 msgb_free(msg);
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200345 LOGP(DOSMUX, LOGL_ERROR, "cannot receive message\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200346 return NULL;
347 }
348 msgb_put(msg, ret);
349
350 return msg;
351}
352
Pau Espin Pedrol833281d2022-10-06 17:41:22 +0200353/* To be called every time some AMR data is received on a connection
354 * returns: 0 if conn can process data, negative if an error ocurred and data should not be further processed */
355static int conn_osmux_event_data_received(struct mgcp_conn_rtp *conn, const struct osmo_sockaddr *rem_addr)
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200356{
Pau Espin Pedrol833281d2022-10-06 17:41:22 +0200357 const struct mgcp_config *cfg;
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200358 switch(conn->osmux.state) {
359 case OSMUX_STATE_ACTIVATING:
Pau Espin Pedrol833281d2022-10-06 17:41:22 +0200360 /* If peer is not behind NAT, transition to OSMUX_STATE_ENABLED is done through
361 * conn_osmux_rx_mdcx() whenever a CRCX/MDCX with the remote address is received.
362 */
363 cfg = conn->conn->endp->trunk->cfg;
Pau Espin Pedrol36413c02022-10-12 17:58:17 +0200364 if (!cfg->osmux.peer_behind_nat) {
Pau Espin Pedrol833281d2022-10-06 17:41:22 +0200365 /* osmux_conn_lookup() should never provide us with an
366 * ACTIVATING conn without NAT in first place. This should never happen. */
367 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
368 "Unexpected rx osmux data for conn in ACTIVATING state without NAT\n");
369 return -1;
370 }
371 /* We have to wait until we received the remote CID from CRCX/MDCX. */
372 if (!conn->osmux.remote_cid_present)
373 return -1;
374
375 /* Update remote address with the src address of the package we received */
376 conn->end.addr = *rem_addr;
377 /* mgcp_rtp_end_remote_addr_available() is now true and we can enable the conn: */
Pau Espin Pedrol887c93e2022-10-06 12:36:03 +0200378 if (conn_osmux_enable(conn) < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200379 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrol833281d2022-10-06 17:41:22 +0200380 "Could not enable osmux conn: %s\n",
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200381 mgcp_conn_dump(conn->conn));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200382 return -1;
383 }
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200384 return 0;
385 case OSMUX_STATE_ENABLED:
386 return 0;
387 default:
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200388 return -1;
389 }
390}
391
Pau Espin Pedrol833281d2022-10-06 17:41:22 +0200392/* To be called every time an CRCX/MDCX is received.
393 * returns: 0 if conn can continue, negative if an error ocurred during setup */
394int conn_osmux_event_rx_crcx_mdcx(struct mgcp_conn_rtp *conn)
395{
396 struct mgcp_config *cfg;
397
398 switch (conn->osmux.state) {
399 case OSMUX_STATE_ACTIVATING:
400 /* If peer is behind NAT, we have to wait until 1st osmux frame is received
401 * to discover peer's real remote address */
402 cfg = conn->conn->endp->trunk->cfg;
Pau Espin Pedrol36413c02022-10-12 17:58:17 +0200403 if (cfg->osmux.peer_behind_nat)
Pau Espin Pedrol833281d2022-10-06 17:41:22 +0200404 return 0;
405 /* Keep waiting to receive remote CID through CRCX/MDCX */
406 if (!conn->osmux.remote_cid_present)
407 return 0;
408 /* keep waiting to receive remote address through CRCX/MDCX */
409 if (!mgcp_rtp_end_remote_addr_available(&conn->end))
410 return 0;
411 /* Since the peer is not behind NAT, we can enable the conn right away since the proper
412 * remote address is now known: */
413 if (conn_osmux_enable(conn) < 0)
414 return -1;
415 /* We have already transitioned to OSMUX_STATE_ENABLED here */
416 return 0;
417 case OSMUX_STATE_ENABLED:
418 return 0;
419 default:
420 OSMO_ASSERT(NULL);
421 }
422 return 0;
423}
424
Pau Espin Pedrole7406652022-10-04 11:58:56 +0200425/* Old versions of osmux used to send dummy packets [0x23 0x<CID>] to punch the
426 * hole in the NAT. Let's handle them speficially. */
Pau Espin Pedrol887c93e2022-10-06 12:36:03 +0200427static int osmux_handle_legacy_dummy(const struct mgcp_trunk *trunk, const struct osmo_sockaddr *rem_addr,
428 struct msgb *msg)
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200429{
Pau Espin Pedrole7406652022-10-04 11:58:56 +0200430 uint8_t osmux_cid = msg->data[1];
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200431 struct mgcp_conn_rtp *conn;
432
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200433 conn = osmux_conn_lookup(trunk, osmux_cid, rem_addr);
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200434 if (!conn) {
Pau Espin Pedrol48fb1762022-09-29 15:50:07 +0200435 LOGP(DOSMUX, LOGL_DEBUG,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200436 "Cannot find conn for Osmux CID %d\n", osmux_cid);
437 goto out;
438 }
439
Pau Espin Pedrol833281d2022-10-06 17:41:22 +0200440 conn_osmux_event_data_received(conn, rem_addr);
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200441 /* Only needed to punch hole in firewall, it can be dropped */
442out:
443 msgb_free(msg);
444 return 0;
445}
446
Pau Espin Pedrol3fbf0352022-09-22 19:01:33 +0200447#define osmux_chunk_length(msg, rem) ((rem) - (msg)->len)
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200448static int osmux_read_fd_cb(struct osmo_fd *ofd, unsigned int what)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200449{
450 struct msgb *msg;
451 struct osmux_hdr *osmuxh;
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200452 struct osmo_sockaddr rem_addr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200453 uint32_t rem;
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200454 struct mgcp_trunk *trunk = ofd->data;
455 struct rate_ctr_group *all_rtp_stats = trunk->ratectr.all_osmux_conn_stats;
Pau Espin Pedrol2dd2b332022-10-03 18:10:55 +0200456 char addr_str[64];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200457
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200458 msg = osmux_recv(ofd, &rem_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200459 if (!msg)
460 return -1;
461
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200462 rate_ctr_inc(rate_ctr_group_get_ctr(all_rtp_stats, OSMUX_PACKETS_RX_CTR));
Pau Espin Pedrol2dd2b332022-10-03 18:10:55 +0200463 osmo_sockaddr_to_str_buf(addr_str, sizeof(addr_str), &rem_addr);
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200464
Pau Espin Pedrol36413c02022-10-12 17:58:17 +0200465 if (trunk->cfg->osmux.usage == OSMUX_USAGE_OFF) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200466 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrol2dd2b332022-10-03 18:10:55 +0200467 "Peer %s wants to use Osmux but MGCP Client did not request it\n",
468 addr_str);
Pau Espin Pedrold14163e2018-10-16 15:48:59 +0200469 goto out;
470 }
471
Pau Espin Pedrole7406652022-10-04 11:58:56 +0200472 /* Catch legacy dummy message and process them separately: */
473 if (msg->len == 2 && msg->data[0] == MGCP_DUMMY_LOAD)
474 return osmux_handle_legacy_dummy(trunk, &rem_addr, msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200475
476 rem = msg->len;
477 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200478 struct mgcp_conn_rtp *conn_src;
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200479 conn_src = osmux_conn_lookup(trunk, osmuxh->circuit_id,
Pau Espin Pedrol15e7e4f2022-09-09 16:52:41 +0200480 &rem_addr);
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200481 if (!conn_src) {
Pau Espin Pedrol48fb1762022-09-29 15:50:07 +0200482 LOGP(DOSMUX, LOGL_DEBUG,
Pau Espin Pedrol2dd2b332022-10-03 18:10:55 +0200483 "Cannot find a src conn for %s CID=%d\n",
484 addr_str, osmuxh->circuit_id);
Pau Espin Pedrol833281d2022-10-06 17:41:22 +0200485 goto next;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200486 }
Pau Espin Pedrol833281d2022-10-06 17:41:22 +0200487
488 if (conn_osmux_event_data_received(conn_src, &rem_addr) < 0)
489 goto next;
490
Pau Espin Pedrolde133112020-09-08 17:51:36 +0200491 mgcp_conn_watchdog_kick(conn_src->conn);
492
Pau Espin Pedrol833281d2022-10-06 17:41:22 +0200493 rtpconn_osmux_rate_ctr_inc(conn_src, OSMUX_CHUNKS_RX_CTR);
494 rtpconn_osmux_rate_ctr_add(conn_src, OSMUX_OCTETS_RX_CTR,
495 osmux_chunk_length(msg, rem));
496 osmux_xfrm_output_sched(conn_src->osmux.out, osmuxh);
497next:
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200498 rem = msg->len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200499 }
500out:
501 msgb_free(msg);
502 return 0;
503}
504
Pau Espin Pedrolc1ad7fd2022-10-06 10:32:07 +0200505int osmux_init(struct mgcp_trunk *trunk)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200506{
507 int ret;
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200508 struct mgcp_config *cfg = trunk->cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200509
Pau Espin Pedrol21bcf6a2022-09-22 18:23:04 +0200510 /* So far we only support running on one trunk: */
511 OSMO_ASSERT(trunk == mgcp_trunk_by_num(cfg, MGCP_TRUNK_VIRTUAL, MGCP_VIRT_TRUNK_ID));
512
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200513 osmo_fd_setup(&osmux_fd_v4, -1, OSMO_FD_READ, osmux_read_fd_cb, trunk, 0);
514 osmo_fd_setup(&osmux_fd_v6, -1, OSMO_FD_READ, osmux_read_fd_cb, trunk, 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200515
Pau Espin Pedrol36413c02022-10-12 17:58:17 +0200516 if (cfg->osmux.local_addr_v4) {
517 ret = mgcp_create_bind(cfg->osmux.local_addr_v4, &osmux_fd_v4, cfg->osmux.local_port,
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200518 cfg->endp_dscp, cfg->endp_priority);
519 if (ret < 0) {
520 LOGP(DOSMUX, LOGL_ERROR, "Cannot bind OSMUX IPv4 socket to %s:%u\n",
Pau Espin Pedrol36413c02022-10-12 17:58:17 +0200521 cfg->osmux.local_addr_v4, cfg->osmux.local_port);
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200522 return ret;
523 }
524
525 ret = osmo_fd_register(&osmux_fd_v4);
526 if (ret < 0) {
527 LOGP(DOSMUX, LOGL_ERROR, "Cannot register OSMUX IPv4 socket %s\n",
528 osmo_sock_get_name2(osmux_fd_v4.fd));
529 return ret;
530 }
531 LOGP(DOSMUX, LOGL_INFO, "OSMUX IPv4 socket listening on %s\n",
532 osmo_sock_get_name2(osmux_fd_v4.fd));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200533 }
Pau Espin Pedrol36413c02022-10-12 17:58:17 +0200534 if (cfg->osmux.local_addr_v6) {
535 ret = mgcp_create_bind(cfg->osmux.local_addr_v6, &osmux_fd_v6, cfg->osmux.local_port,
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200536 cfg->endp_dscp, cfg->endp_priority);
537 if (ret < 0) {
Pau Espin Pedrol37799192022-10-06 10:29:47 +0200538 LOGP(DOSMUX, LOGL_ERROR, "Cannot bind OSMUX IPv6 socket to [%s]:%u\n",
Pau Espin Pedrol36413c02022-10-12 17:58:17 +0200539 cfg->osmux.local_addr_v6, cfg->osmux.local_port);
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200540 return ret;
541 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200542
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200543 ret = osmo_fd_register(&osmux_fd_v6);
544 if (ret < 0) {
545 LOGP(DOSMUX, LOGL_ERROR, "Cannot register OSMUX IPv6 socket %s\n",
546 osmo_sock_get_name2(osmux_fd_v6.fd));
547 return ret;
548 }
549 LOGP(DOSMUX, LOGL_INFO, "OSMUX IPv6 socket listening on %s\n",
550 osmo_sock_get_name2(osmux_fd_v6.fd));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200551 }
Pau Espin Pedrol36413c02022-10-12 17:58:17 +0200552 cfg->osmux.initialized = true;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200553 return 0;
554}
555
Pau Espin Pedrol705565d2022-10-06 14:37:31 +0200556/*! relase OSXMUX cid, that had been allocated to this connection.
557 * \param[in] conn connection with OSMUX cid to release */
558static void conn_osmux_release_local_cid(struct mgcp_conn_rtp *conn)
559{
560 if (conn->osmux.local_cid_allocated)
561 osmux_cid_pool_put(conn->osmux.local_cid);
562 conn->osmux.local_cid = 0;
563 conn->osmux.local_cid_allocated = false;
564}
565
566/*! allocate local OSMUX cid to connection.
567 * \param[in] conn connection for which we allocate the local OSMUX cid
568 * \returns Allocated OSMUX cid, -1 on error (no free CIDs avail).
569 */
570static int conn_osmux_allocate_local_cid(struct mgcp_conn_rtp *conn)
571{
572 OSMO_ASSERT(conn->osmux.local_cid_allocated == false);
573 int osmux_cid = osmux_cid_pool_get_next();
574 if (osmux_cid == -1) {
575 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
576 "no available local Osmux CID to allocate!\n");
577 return -1;
578 }
579
580 conn->osmux.local_cid = (uint8_t) osmux_cid;
581 conn->osmux.local_cid_allocated = true;
582 return osmux_cid;
583}
584
Pau Espin Pedrold48a8112022-09-27 12:37:53 +0200585/*! Initialize Osmux bits of a conn.
586 * \param[in] conn Osmux connection to initialize
587 * \returns 0 on success, negative on ERROR */
588int osmux_init_conn(struct mgcp_conn_rtp *conn)
589{
590 if (conn_osmux_allocate_local_cid(conn) == -1)
591 return -1;
592 conn->osmux.ctrg = rate_ctr_group_alloc(conn->conn, &rate_ctr_group_osmux_desc, conn->ctrg->idx);
593
Pau Espin Pedrol63088e02022-10-06 14:32:56 +0200594 conn->type = MGCP_RTP_OSMUX;
Pau Espin Pedrolc0e1b1a2022-10-06 16:21:37 +0200595 /* Annotate Osmux circuit ID and set it to negotiating state until this
596 * is fully set up from the dummy load. */
Pau Espin Pedrold48a8112022-09-27 12:37:53 +0200597 conn->osmux.state = OSMUX_STATE_ACTIVATING;
598 return 0;
599}
600
Philipp Maier87bd9be2017-08-22 16:35:41 +0200601/*! enable OSXMUX circuit for a specified connection.
Pau Espin Pedrol887c93e2022-10-06 12:36:03 +0200602 * \param[in] conn connection to enable
Philipp Maier87bd9be2017-08-22 16:35:41 +0200603 * \returns 0 on success, -1 on ERROR */
Pau Espin Pedrol887c93e2022-10-06 12:36:03 +0200604int conn_osmux_enable(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200605{
Pau Espin Pedrol833281d2022-10-06 17:41:22 +0200606 OSMO_ASSERT(conn->osmux.state == OSMUX_STATE_ACTIVATING);
607 OSMO_ASSERT(conn->osmux.remote_cid_present == true);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200608 /*! If osmux is enabled, initialize the output handler. This handler is
609 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
610 * allocated based on the circuit ID (conn_net->osmux.cid), which is unique
611 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200612 * SSRC space (2^32) by the OSMUX_CID_MAX + 1 possible circuit IDs, then randomly
Philipp Maier87bd9be2017-08-22 16:35:41 +0200613 * select one value from that window. Thus, we have no chance to have
614 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
615 * similarly, for flows traveling to the MSC.
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200616 */
Pau Espin Pedrol887c93e2022-10-06 12:36:03 +0200617 const struct mgcp_trunk *trunk = conn->conn->endp->trunk;
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200618 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / (OSMUX_CID_MAX + 1);
Pau Espin Pedrol36413c02022-10-12 17:58:17 +0200619 bool osmux_dummy = trunk->cfg->osmux.dummy_padding;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200620
Pau Espin Pedrol833281d2022-10-06 17:41:22 +0200621 /* Wait until we have the remote connection information, be it from MDCX (peer not behind NAT)
622 * or later learned from first received remote osmux packet (peer behind NAT) */
Pau Espin Pedrole56ec4a2022-10-04 14:35:00 +0200623 if (!mgcp_rtp_end_remote_addr_available(&conn->end)) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200624 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrole7de1372022-10-06 10:36:32 +0200625 "Osmux remote address/port still unknown\n");
Pau Espin Pedroldac2ca72019-05-13 17:34:51 +0200626 return -1;
627 }
628
Pau Espin Pedrol887c93e2022-10-06 12:36:03 +0200629 conn->osmux.in = osmux_handle_find_or_create(trunk, &conn->end.addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200630 if (!conn->osmux.in) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200631 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrole7de1372022-10-06 10:36:32 +0200632 "Cannot allocate input osmux handle for conn:%s\n",
633 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200634 return -1;
635 }
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200636 if (osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.remote_cid, osmux_dummy) < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200637 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrole7de1372022-10-06 10:36:32 +0200638 "Cannot open osmux circuit %u for conn:%s\n",
639 conn->osmux.remote_cid, mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200640 return -1;
641 }
642
Pau Espin Pedrol941e3172022-09-22 21:16:56 +0200643 conn->osmux.out = osmux_xfrm_output_alloc(conn->conn);
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200644 osmux_xfrm_output_set_rtp_ssrc(conn->osmux.out,
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200645 (conn->osmux.remote_cid * rtp_ssrc_winlen) +
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200646 (random() % rtp_ssrc_winlen));
647 osmux_xfrm_output_set_rtp_pl_type(conn->osmux.out, conn->end.codec->payload_type);
648 osmux_xfrm_output_set_tx_cb(conn->osmux.out,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200649 scheduled_from_osmux_tx_rtp_cb, conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200650
651 conn->osmux.state = OSMUX_STATE_ENABLED;
Pau Espin Pedrol833281d2022-10-06 17:41:22 +0200652 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
653 "Osmux CID %u towards %s is now enabled\n",
654 conn->osmux.remote_cid,
655 osmo_sockaddr_to_str(&conn->end.addr));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200656 return 0;
657}
658
Philipp Maier87bd9be2017-08-22 16:35:41 +0200659/*! disable OSXMUX circuit for a specified connection.
660 * \param[in] conn connection to disable */
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200661void conn_osmux_disable(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200662{
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200663 OSMO_ASSERT(conn->osmux.state != OSMUX_STATE_DISABLED);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200664
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200665 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200666 "Releasing connection using local Osmux CID %u\n", conn->osmux.local_cid);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200667
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200668 struct rate_ctr_group *all_osmux_stats = conn->conn->endp->trunk->ratectr.all_osmux_conn_stats;
669 rate_ctr_inc(rate_ctr_group_get_ctr(all_osmux_stats, OSMUX_NUM_CONNECTIONS));
670
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200671 if (conn->osmux.state == OSMUX_STATE_ENABLED) {
672 /* We are closing, we don't need pending RTP packets to be transmitted */
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200673 osmux_xfrm_output_set_tx_cb(conn->osmux.out, NULL, NULL);
674 TALLOC_FREE(conn->osmux.out);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200675
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200676 osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.remote_cid);
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200677 conn->osmux.state = OSMUX_STATE_DISABLED;
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200678 osmux_handle_put(conn->osmux.in);
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200679 conn->osmux.remote_cid = 0;
680 conn->osmux.remote_cid_present = false;
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200681 }
Pau Espin Pedrold48a8112022-09-27 12:37:53 +0200682
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200683 conn_osmux_release_local_cid(conn);
Pau Espin Pedrold48a8112022-09-27 12:37:53 +0200684
685 rate_ctr_group_free(conn->osmux.ctrg);
686 conn->osmux.ctrg = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200687}
688
Philipp Maier87bd9be2017-08-22 16:35:41 +0200689/*! send RTP dummy packet to OSMUX connection port.
Philipp Maier87bd9be2017-08-22 16:35:41 +0200690 * \param[in] conn associated RTP connection
691 * \returns bytes sent, -1 on error */
Pau Espin Pedrol887c93e2022-10-06 12:36:03 +0200692int osmux_send_dummy(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200693{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200694 char ipbuf[INET6_ADDRSTRLEN];
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200695 struct osmux_hdr *osmuxh;
696 int buf_len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200697
Philipp Maier87bd9be2017-08-22 16:35:41 +0200698 /*! The dummy packet will not be sent via the actual OSMUX connection,
699 * instead it is sent out of band to port where the remote OSMUX
700 * multplexer is listening. The goal is to ensure that the connection
701 * is kept open */
702
703 /*! We don't need to send the dummy load for osmux so often as another
704 * endpoint may have already punched the hole in the firewall. This
705 * approach is simple though. */
706
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200707 buf_len = sizeof(struct osmux_hdr) + osmo_amr_bytes(AMR_FT_0);
708 osmuxh = (struct osmux_hdr *) alloca(buf_len);
709 memset(osmuxh, 0, buf_len);
710 osmuxh->ft = OSMUX_FT_DUMMY;
711 osmuxh->amr_ft = AMR_FT_0;
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200712 osmuxh->circuit_id = conn->osmux.remote_cid;
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200713
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200714 LOGPCONN(conn->conn, DOSMUX, LOGL_DEBUG,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200715 "sending OSMUX dummy load to %s:%u CID %u\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200716 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf),
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200717 osmo_sockaddr_port(&conn->end.addr.u.sa), conn->osmux.remote_cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200718
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200719 return mgcp_udp_send(osmux_fd_v4.fd, &conn->end.addr, (char *)osmuxh, buf_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200720}
721
Pau Espin Pedrolc7c8e642022-10-06 18:24:21 +0200722/* Keeps track of locally allocated Osmux circuit ID. +7 to round up to 8 bit boundary. */
Pau Espin Pedrolbcd52e52018-09-17 12:41:28 +0200723static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1 + 7) / 8];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200724
Philipp Maier87bd9be2017-08-22 16:35:41 +0200725/*! count the number of taken OSMUX cids.
726 * \returns number of OSMUX cids in use */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200727int osmux_cid_pool_count_used(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200728{
729 int i, j, used = 0;
730
731 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
732 for (j = 0; j < 8; j++) {
733 if (osmux_cid_bitmap[i] & (1 << j))
734 used += 1;
735 }
736 }
737
738 return used;
739}
740
Pau Espin Pedrol310e41c2022-11-15 13:23:38 +0100741/*! Find and reserve a free OSMUX cid. Keep state of last allocated CID to
742 * rotate allocated CIDs over time. This helps in letting CIDs unused for some
743 * time after last use.
Philipp Maier87bd9be2017-08-22 16:35:41 +0200744 * \returns OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200745int osmux_cid_pool_get_next(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200746{
Pau Espin Pedrol310e41c2022-11-15 13:23:38 +0100747 static uint8_t next_free_osmux_cid_lookup = 0;
748 uint8_t start_i, start_j;
749 uint8_t i, j, cid;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200750
Pau Espin Pedrol310e41c2022-11-15 13:23:38 +0100751 /* i = octet index, j = bit index inside ith octet */
752 start_i = next_free_osmux_cid_lookup >> 3;
753 start_j = next_free_osmux_cid_lookup & 0x07;
754
755 for (i = start_i; i < sizeof(osmux_cid_bitmap); i++) {
756 for (j = start_j; j < 8; j++) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200757 if (osmux_cid_bitmap[i] & (1 << j))
758 continue;
Pau Espin Pedrol310e41c2022-11-15 13:23:38 +0100759 goto found;
760 }
761 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200762
Pau Espin Pedrol310e41c2022-11-15 13:23:38 +0100763 for (i = 0; i <= start_i; i++) {
764 for (j = 0; j < start_j; j++) {
765 if (osmux_cid_bitmap[i] & (1 << j))
766 continue;
767 goto found;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200768 }
769 }
770
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200771 LOGP(DOSMUX, LOGL_ERROR, "All Osmux circuits are in use!\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200772 return -1;
Pau Espin Pedrol310e41c2022-11-15 13:23:38 +0100773
774found:
775 osmux_cid_bitmap[i] |= (1 << j);
776 cid = (i << 3) | j;
777 next_free_osmux_cid_lookup = (cid + 1) & 0xff;
778 LOGP(DOSMUX, LOGL_DEBUG,
779 "Allocating Osmux CID %u from pool\n", cid);
780 return cid;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200781}
782
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200783/*! take a specific OSMUX cid.
784 * \param[in] osmux_cid OSMUX cid */
785void osmux_cid_pool_get(uint8_t osmux_cid)
786{
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200787 LOGP(DOSMUX, LOGL_DEBUG, "Allocating Osmux CID %u from pool\n", osmux_cid);
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200788 osmux_cid_bitmap[osmux_cid / 8] |= (1 << (osmux_cid % 8));
789}
790
Philipp Maier87bd9be2017-08-22 16:35:41 +0200791/*! put back a no longer used OSMUX cid.
792 * \param[in] osmux_cid OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200793void osmux_cid_pool_put(uint8_t osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200794{
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200795 LOGP(DOSMUX, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200796 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
797}
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200798
799/*! check if OSMUX cid is already taken */
800bool osmux_cid_pool_allocated(uint8_t osmux_cid)
801{
802 return !!(osmux_cid_bitmap[osmux_cid / 8] & (1 << (osmux_cid % 8)));
803}