Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1 | /* |
| 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 Pedrol | f027f17 | 2019-05-06 13:57:31 +0200 | [diff] [blame] | 18 | #include <osmocom/core/socket.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 19 | #include <osmocom/core/talloc.h> |
| 20 | |
| 21 | #include <osmocom/netif/osmux.h> |
| 22 | #include <osmocom/netif/rtp.h> |
Pau Espin Pedrol | c1ad553 | 2019-05-15 23:00:53 +0200 | [diff] [blame] | 23 | #include <osmocom/netif/amr.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 24 | |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 25 | #include <osmocom/mgcp/debug.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 26 | #include <osmocom/mgcp/mgcp.h> |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 27 | #include <osmocom/mgcp/mgcp_protocol.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 28 | #include <osmocom/mgcp/osmux.h> |
| 29 | #include <osmocom/mgcp/mgcp_conn.h> |
Philipp Maier | 37d11c8 | 2018-02-01 14:38:12 +0100 | [diff] [blame] | 30 | #include <osmocom/mgcp/mgcp_endp.h> |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 31 | #include <osmocom/mgcp/mgcp_trunk.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 32 | |
| 33 | static struct osmo_fd osmux_fd; |
| 34 | |
| 35 | static LLIST_HEAD(osmux_handle_list); |
| 36 | |
| 37 | struct osmux_handle { |
| 38 | struct llist_head head; |
| 39 | struct osmux_in_handle *in; |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 40 | struct osmo_sockaddr rem_addr; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 41 | int refcnt; |
| 42 | }; |
| 43 | |
Pau Espin Pedrol | ea7aaf2 | 2022-09-22 21:07:54 +0200 | [diff] [blame] | 44 | const 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 Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 51 | static 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 Pedrol | b4a5947 | 2022-09-23 18:08:53 +0200 | [diff] [blame] | 59 | static 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 | |
| 69 | static 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 Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 74 | /* Deliver OSMUX batch to the remote end */ |
| 75 | static void osmux_deliver_cb(struct msgb *batch_msg, void *data) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 76 | { |
| 77 | struct osmux_handle *handle = data; |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 78 | socklen_t dest_len; |
Pau Espin Pedrol | 4d1a52d | 2022-09-22 17:11:57 +0200 | [diff] [blame] | 79 | int rc; |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 80 | 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 Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 82 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 83 | 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 Pedrol | 4d1a52d | 2022-09-22 17:11:57 +0200 | [diff] [blame] | 92 | 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 Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 99 | 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 Pedrol | 4d1a52d | 2022-09-22 17:11:57 +0200 | [diff] [blame] | 102 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 103 | msgb_free(batch_msg); |
| 104 | } |
| 105 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 106 | /* Lookup existing OSMUX handle for specified destination address. */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 107 | static struct osmux_handle * |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 108 | osmux_handle_find_get(const struct osmo_sockaddr *rem_addr) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 109 | { |
| 110 | struct osmux_handle *h; |
| 111 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 112 | llist_for_each_entry(h, &osmux_handle_list, head) { |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 113 | if (osmo_sockaddr_cmp(&h->rem_addr, rem_addr) == 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 114 | h->refcnt++; |
Pau Espin Pedrol | c61664c | 2022-09-09 17:03:13 +0200 | [diff] [blame] | 115 | 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 Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 118 | return h; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return NULL; |
| 123 | } |
| 124 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 125 | /* Put down no longer needed OSMUX handle */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 126 | static void osmux_handle_put(struct osmux_in_handle *in) |
| 127 | { |
| 128 | struct osmux_handle *h; |
| 129 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 130 | llist_for_each_entry(h, &osmux_handle_list, head) { |
| 131 | if (h->in == in) { |
Pau Espin Pedrol | c61664c | 2022-09-09 17:03:13 +0200 | [diff] [blame] | 132 | 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 Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 135 | if (--h->refcnt == 0) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 136 | LOGP(DOSMUX, LOGL_INFO, |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 137 | "Releasing unused osmux handle for %s\n", |
| 138 | osmo_sockaddr_to_str(&h->rem_addr)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 139 | llist_del(&h->head); |
Pau Espin Pedrol | 72eff2c | 2022-09-29 14:26:00 +0200 | [diff] [blame] | 140 | TALLOC_FREE(h->in); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 141 | talloc_free(h); |
| 142 | } |
| 143 | return; |
| 144 | } |
| 145 | } |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 146 | LOGP(DOSMUX, LOGL_ERROR, "Cannot find Osmux input handle %p\n", in); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 149 | /* Allocate free OSMUX handle */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 150 | static struct osmux_handle * |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 151 | osmux_handle_alloc(struct mgcp_conn_rtp *conn, const struct osmo_sockaddr *rem_addr) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 152 | { |
| 153 | struct osmux_handle *h; |
Pau Espin Pedrol | 941e317 | 2022-09-22 21:16:56 +0200 | [diff] [blame] | 154 | struct mgcp_trunk *trunk = conn->conn->endp->trunk; |
| 155 | struct mgcp_config *cfg = trunk->cfg; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 156 | |
Pau Espin Pedrol | 941e317 | 2022-09-22 21:16:56 +0200 | [diff] [blame] | 157 | h = talloc_zero(trunk, struct osmux_handle); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 158 | if (!h) |
| 159 | return NULL; |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 160 | h->rem_addr = *rem_addr; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 161 | h->refcnt++; |
| 162 | |
Pau Espin Pedrol | 72eff2c | 2022-09-29 14:26:00 +0200 | [diff] [blame] | 163 | h->in = osmux_xfrm_input_alloc(h); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 164 | if (!h->in) { |
| 165 | talloc_free(h); |
| 166 | return NULL; |
| 167 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 168 | /* sequence number to start OSMUX message from */ |
Pau Espin Pedrol | 72eff2c | 2022-09-29 14:26:00 +0200 | [diff] [blame] | 169 | osmux_xfrm_input_set_initial_seqnum(h->in, 0); |
| 170 | osmux_xfrm_input_set_batch_factor(h->in, cfg->osmux_batch); |
| 171 | /* If batch size is zero, the library defaults to 1472 bytes. */ |
| 172 | osmux_xfrm_input_set_batch_size(h->in, cfg->osmux_batch_size); |
| 173 | osmux_xfrm_input_set_deliver_cb(h->in, osmux_deliver_cb, h); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 174 | |
| 175 | llist_add(&h->head, &osmux_handle_list); |
| 176 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 177 | LOGP(DOSMUX, LOGL_DEBUG, "Created new OSMUX handle for rem_addr=%s\n", |
| 178 | osmo_sockaddr_to_str(rem_addr)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 179 | |
| 180 | return h; |
| 181 | } |
| 182 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 183 | /* Lookup existing handle for a specified address, if the handle can not be |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 184 | * found, the function will automatically allocate one */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 185 | static struct osmux_in_handle * |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 186 | osmux_handle_find_or_create(struct mgcp_conn_rtp *conn, const struct osmo_sockaddr *rem_addr) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 187 | { |
| 188 | struct osmux_handle *h; |
| 189 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 190 | if (rem_addr->u.sa.sa_family != AF_INET) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 191 | LOGP(DOSMUX, LOGL_DEBUG, "IPv6 not supported in osmux yet!\n"); |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 192 | return NULL; |
| 193 | } |
| 194 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 195 | h = osmux_handle_find_get(rem_addr); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 196 | if (h != NULL) |
| 197 | return h->in; |
| 198 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 199 | h = osmux_handle_alloc(conn, rem_addr); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 200 | if (h == NULL) |
| 201 | return NULL; |
| 202 | |
| 203 | return h->in; |
| 204 | } |
| 205 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 206 | /*! send RTP packet through OSMUX connection. |
| 207 | * \param[in] buf rtp data |
| 208 | * \param[in] buf_len length of rtp data |
| 209 | * \param[in] conn associated RTP connection |
| 210 | * \returns 0 on success, -1 on ERROR */ |
| 211 | int osmux_xfrm_to_osmux(char *buf, int buf_len, struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 212 | { |
| 213 | int ret; |
| 214 | struct msgb *msg; |
| 215 | |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 216 | if (!conn->end.output_enabled) { |
Pau Espin Pedrol | 432ee9d | 2022-09-23 18:18:02 +0200 | [diff] [blame] | 217 | rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_DROPPED_CTR); |
Pau Espin Pedrol | e39ae87 | 2022-09-09 17:36:33 +0200 | [diff] [blame] | 218 | return -1; |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 219 | } |
Pau Espin Pedrol | e39ae87 | 2022-09-09 17:36:33 +0200 | [diff] [blame] | 220 | |
Pau Espin Pedrol | 8b0a614 | 2022-09-09 17:34:31 +0200 | [diff] [blame] | 221 | 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 Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 223 | conn->osmux.remote_cid); |
Pau Espin Pedrol | 432ee9d | 2022-09-23 18:18:02 +0200 | [diff] [blame] | 224 | rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_DROPPED_CTR); |
Pau Espin Pedrol | 8b0a614 | 2022-09-09 17:34:31 +0200 | [diff] [blame] | 225 | return -1; |
| 226 | } |
| 227 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 228 | msg = msgb_alloc(4096, "RTP"); |
| 229 | if (!msg) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 230 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 231 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 232 | memcpy(msg->data, buf, buf_len); |
| 233 | msgb_put(msg, buf_len); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 234 | |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 235 | while ((ret = osmux_xfrm_input(conn->osmux.in, msg, conn->osmux.remote_cid)) > 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 236 | /* batch full, build and deliver it */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 237 | osmux_xfrm_input_deliver(conn->osmux.in); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 238 | } |
Pau Espin Pedrol | 432ee9d | 2022-09-23 18:18:02 +0200 | [diff] [blame] | 239 | if (ret < 0) { |
| 240 | rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_DROPPED_CTR); |
| 241 | } else { |
| 242 | rtpconn_osmux_rate_ctr_inc(conn, OSMUX_RTP_PACKETS_TX_CTR); |
| 243 | rtpconn_osmux_rate_ctr_add(conn, OSMUX_AMR_OCTETS_TX_CTR, buf_len - sizeof(struct rtp_hdr)); |
| 244 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 248 | /* Lookup the endpoint that corresponds to the specified address (port) */ |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 249 | static struct mgcp_conn_rtp* |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 250 | osmux_conn_lookup(struct mgcp_trunk *trunk, uint8_t local_cid, const struct osmo_sockaddr *rem_addr) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 251 | { |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 252 | struct mgcp_endpoint *endp; |
| 253 | struct mgcp_conn *conn = NULL; |
Pau Espin Pedrol | b0ea797 | 2022-10-03 15:56:25 +0200 | [diff] [blame] | 254 | struct mgcp_conn_rtp *conn_rtp; |
| 255 | struct osmux_handle *h; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 256 | int i; |
| 257 | |
Philipp Maier | d19de2e | 2020-06-03 13:55:33 +0200 | [diff] [blame] | 258 | for (i = 0; i < trunk->number_endpoints; i++) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 259 | |
Philipp Maier | d19de2e | 2020-06-03 13:55:33 +0200 | [diff] [blame] | 260 | endp = trunk->endpoints[i]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 261 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 262 | llist_for_each_entry(conn, &endp->conns, entry) { |
| 263 | if (conn->type != MGCP_CONN_TYPE_RTP) |
| 264 | continue; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 265 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 266 | conn_rtp = &conn->u.rtp; |
| 267 | if (!mgcp_conn_rtp_is_osmux(conn_rtp)) |
| 268 | continue; |
| 269 | |
Pau Espin Pedrol | b0ea797 | 2022-10-03 15:56:25 +0200 | [diff] [blame] | 270 | /* Current implementation sets remote address & port for |
| 271 | * the conn based on src address received on the socket |
| 272 | * for the CID, in order to workaround NATs. |
| 273 | * Once the conn is fully established (remote address is |
| 274 | * known), validate the remote address doesn't change: */ |
| 275 | if (conn_rtp->osmux.state == OSMUX_STATE_ENABLED) { |
| 276 | h = osmux_xfrm_input_get_deliver_cb_data(conn_rtp->osmux.in); |
| 277 | if (osmo_sockaddr_cmp(&h->rem_addr, rem_addr) != 0) |
| 278 | continue; |
| 279 | } |
| 280 | /* else: select based on CID only, to learn rem addr in NAT-based scenarios. |
| 281 | * FIXME: This should be configurable, have some sort of "osmux nat (on|off)" */ |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 282 | |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 283 | if (conn_rtp->osmux.local_cid == local_cid) |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 284 | return conn_rtp; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 285 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 286 | } |
| 287 | |
Pau Espin Pedrol | 48fb176 | 2022-09-29 15:50:07 +0200 | [diff] [blame] | 288 | LOGP(DOSMUX, LOGL_DEBUG, "Cannot find osmux conn with rem_addr=%s local_cid=%d\n", |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 289 | osmo_sockaddr_to_str(rem_addr), local_cid); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 290 | |
| 291 | return NULL; |
| 292 | } |
| 293 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 294 | static void scheduled_from_osmux_tx_rtp_cb(struct msgb *msg, void *data) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 295 | { |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 296 | struct mgcp_conn_rtp *conn = data; |
| 297 | struct mgcp_endpoint *endp = conn->conn->endp; |
Pau Espin Pedrol | 69f15e4 | 2022-10-03 16:18:34 +0200 | [diff] [blame] | 298 | struct osmux_handle *h = osmux_xfrm_input_get_deliver_cb_data(conn->osmux.in); |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 299 | struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg); |
| 300 | *mc = (struct osmo_rtp_msg_ctx){ |
| 301 | .proto = MGCP_PROTO_RTP, |
| 302 | .conn_src = conn, |
Pau Espin Pedrol | 69f15e4 | 2022-10-03 16:18:34 +0200 | [diff] [blame] | 303 | .from_addr = &h->rem_addr, |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 304 | }; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 305 | |
Neels Hofmeyr | 51b42ff | 2020-06-19 01:34:42 +0200 | [diff] [blame] | 306 | endp->type->dispatch_rtp_cb(msg); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 307 | msgb_free(msg); |
| 308 | } |
| 309 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 310 | static struct msgb *osmux_recv(struct osmo_fd *ofd, struct osmo_sockaddr *addr) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 311 | { |
| 312 | struct msgb *msg; |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 313 | socklen_t slen = sizeof(addr->u.sas); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 314 | int ret; |
| 315 | |
| 316 | msg = msgb_alloc(4096, "OSMUX"); |
| 317 | if (!msg) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 318 | LOGP(DOSMUX, LOGL_ERROR, "cannot allocate message\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 319 | return NULL; |
| 320 | } |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 321 | ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0, &addr->u.sa, &slen); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 322 | if (ret <= 0) { |
| 323 | msgb_free(msg); |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 324 | LOGP(DOSMUX, LOGL_ERROR, "cannot receive message\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 325 | return NULL; |
| 326 | } |
| 327 | msgb_put(msg, ret); |
| 328 | |
| 329 | return msg; |
| 330 | } |
| 331 | |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 332 | /* Updates endp osmux state and returns 0 if it can process messages, -1 otherwise */ |
| 333 | static int endp_osmux_state_check(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn, |
| 334 | bool sending) |
| 335 | { |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 336 | struct osmo_sockaddr rem_addr; |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 337 | |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 338 | switch(conn->osmux.state) { |
| 339 | case OSMUX_STATE_ACTIVATING: |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 340 | rem_addr = conn->end.addr; |
| 341 | osmo_sockaddr_set_port(&rem_addr.u.sa, ntohs(conn->end.rtp_port)); |
| 342 | if (osmux_enable_conn(endp, conn, &rem_addr) < 0) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 343 | LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | 3752522 | 2019-05-13 17:34:14 +0200 | [diff] [blame] | 344 | "Could not enable osmux for conn on %s: %s\n", |
| 345 | sending ? "sent" : "received", |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 346 | mgcp_conn_dump(conn->conn)); |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 347 | return -1; |
| 348 | } |
Pau Espin Pedrol | c5bbb78 | 2022-09-19 16:46:02 +0200 | [diff] [blame] | 349 | LOGPCONN(conn->conn, DOSMUX, LOGL_INFO, |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 350 | "Osmux %s CID %u towards %s is now enabled\n", |
Pau Espin Pedrol | 3752522 | 2019-05-13 17:34:14 +0200 | [diff] [blame] | 351 | sending ? "sent" : "received", |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 352 | sending ? conn->osmux.remote_cid : conn->osmux.local_cid, |
| 353 | osmo_sockaddr_to_str(&rem_addr)); |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 354 | return 0; |
| 355 | case OSMUX_STATE_ENABLED: |
| 356 | return 0; |
| 357 | default: |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 358 | LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 359 | "Osmux %s in conn %s without full negotiation, state %d\n", |
| 360 | sending ? "sent" : "received", |
| 361 | mgcp_conn_dump(conn->conn), conn->osmux.state); |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 362 | return -1; |
| 363 | } |
| 364 | } |
| 365 | |
Pau Espin Pedrol | e740665 | 2022-10-04 11:58:56 +0200 | [diff] [blame] | 366 | /* Old versions of osmux used to send dummy packets [0x23 0x<CID>] to punch the |
| 367 | * hole in the NAT. Let's handle them speficially. */ |
| 368 | static int osmux_handle_legacy_dummy(struct mgcp_trunk *trunk, const struct osmo_sockaddr *rem_addr, |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 369 | struct msgb *msg) |
| 370 | { |
Pau Espin Pedrol | e740665 | 2022-10-04 11:58:56 +0200 | [diff] [blame] | 371 | uint8_t osmux_cid = msg->data[1]; |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 372 | struct mgcp_conn_rtp *conn; |
| 373 | |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 374 | conn = osmux_conn_lookup(trunk, osmux_cid, rem_addr); |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 375 | if (!conn) { |
Pau Espin Pedrol | 48fb176 | 2022-09-29 15:50:07 +0200 | [diff] [blame] | 376 | LOGP(DOSMUX, LOGL_DEBUG, |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 377 | "Cannot find conn for Osmux CID %d\n", osmux_cid); |
| 378 | goto out; |
| 379 | } |
| 380 | |
| 381 | endp_osmux_state_check(conn->conn->endp, conn, false); |
| 382 | /* Only needed to punch hole in firewall, it can be dropped */ |
| 383 | out: |
| 384 | msgb_free(msg); |
| 385 | return 0; |
| 386 | } |
| 387 | |
Pau Espin Pedrol | 3fbf035 | 2022-09-22 19:01:33 +0200 | [diff] [blame] | 388 | #define osmux_chunk_length(msg, rem) ((rem) - (msg)->len) |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 389 | static int osmux_read_fd_cb(struct osmo_fd *ofd, unsigned int what) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 390 | { |
| 391 | struct msgb *msg; |
| 392 | struct osmux_hdr *osmuxh; |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 393 | struct osmo_sockaddr rem_addr; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 394 | uint32_t rem; |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 395 | struct mgcp_trunk *trunk = ofd->data; |
| 396 | struct rate_ctr_group *all_rtp_stats = trunk->ratectr.all_osmux_conn_stats; |
Pau Espin Pedrol | 2dd2b33 | 2022-10-03 18:10:55 +0200 | [diff] [blame] | 397 | char addr_str[64]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 398 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 399 | msg = osmux_recv(ofd, &rem_addr); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 400 | if (!msg) |
| 401 | return -1; |
| 402 | |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 403 | rate_ctr_inc(rate_ctr_group_get_ctr(all_rtp_stats, OSMUX_PACKETS_RX_CTR)); |
Pau Espin Pedrol | 2dd2b33 | 2022-10-03 18:10:55 +0200 | [diff] [blame] | 404 | osmo_sockaddr_to_str_buf(addr_str, sizeof(addr_str), &rem_addr); |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 405 | |
Pau Espin Pedrol | 928a20b | 2022-09-23 15:38:24 +0200 | [diff] [blame] | 406 | if (trunk->cfg->osmux_use == OSMUX_USAGE_OFF) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 407 | LOGP(DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | 2dd2b33 | 2022-10-03 18:10:55 +0200 | [diff] [blame] | 408 | "Peer %s wants to use Osmux but MGCP Client did not request it\n", |
| 409 | addr_str); |
Pau Espin Pedrol | d14163e | 2018-10-16 15:48:59 +0200 | [diff] [blame] | 410 | goto out; |
| 411 | } |
| 412 | |
Pau Espin Pedrol | e740665 | 2022-10-04 11:58:56 +0200 | [diff] [blame] | 413 | /* Catch legacy dummy message and process them separately: */ |
| 414 | if (msg->len == 2 && msg->data[0] == MGCP_DUMMY_LOAD) |
| 415 | return osmux_handle_legacy_dummy(trunk, &rem_addr, msg); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 416 | |
| 417 | rem = msg->len; |
| 418 | while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) { |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 419 | struct mgcp_endpoint *endp; |
| 420 | struct mgcp_conn_rtp *conn_src; |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 421 | conn_src = osmux_conn_lookup(trunk, osmuxh->circuit_id, |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 422 | &rem_addr); |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 423 | if (!conn_src) { |
Pau Espin Pedrol | 48fb176 | 2022-09-29 15:50:07 +0200 | [diff] [blame] | 424 | LOGP(DOSMUX, LOGL_DEBUG, |
Pau Espin Pedrol | 2dd2b33 | 2022-10-03 18:10:55 +0200 | [diff] [blame] | 425 | "Cannot find a src conn for %s CID=%d\n", |
| 426 | addr_str, osmuxh->circuit_id); |
Pau Espin Pedrol | 1c5fcb0 | 2022-09-29 15:59:15 +0200 | [diff] [blame] | 427 | rem = msg->len; |
| 428 | continue; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 429 | } |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 430 | endp = conn_src->conn->endp; |
Pau Espin Pedrol | de13311 | 2020-09-08 17:51:36 +0200 | [diff] [blame] | 431 | mgcp_conn_watchdog_kick(conn_src->conn); |
| 432 | |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 433 | if (endp_osmux_state_check(endp, conn_src, false) == 0) { |
Pau Espin Pedrol | b4a5947 | 2022-09-23 18:08:53 +0200 | [diff] [blame] | 434 | rtpconn_osmux_rate_ctr_inc(conn_src, OSMUX_CHUNKS_RX_CTR); |
| 435 | rtpconn_osmux_rate_ctr_add(conn_src, OSMUX_OCTETS_RX_CTR, |
| 436 | osmux_chunk_length(msg, rem)); |
Pau Espin Pedrol | 3363916 | 2022-08-31 17:46:11 +0200 | [diff] [blame] | 437 | osmux_xfrm_output_sched(conn_src->osmux.out, osmuxh); |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 438 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 439 | rem = msg->len; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 440 | } |
| 441 | out: |
| 442 | msgb_free(msg); |
| 443 | return 0; |
| 444 | } |
| 445 | |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 446 | int osmux_init(int role, struct mgcp_trunk *trunk) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 447 | { |
| 448 | int ret; |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 449 | struct mgcp_config *cfg = trunk->cfg; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 450 | |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 451 | /* So far we only support running on one trunk: */ |
| 452 | OSMO_ASSERT(trunk == mgcp_trunk_by_num(cfg, MGCP_TRUNK_VIRTUAL, MGCP_VIRT_TRUNK_ID)); |
| 453 | |
| 454 | osmo_fd_setup(&osmux_fd, -1, OSMO_FD_READ, osmux_read_fd_cb, trunk, 0); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 455 | |
Harald Welte | 55a9229 | 2021-04-28 19:06:34 +0200 | [diff] [blame] | 456 | ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port, |
| 457 | cfg->endp_dscp, cfg->endp_priority); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 458 | if (ret < 0) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 459 | LOGP(DOSMUX, LOGL_ERROR, "cannot bind OSMUX socket to %s:%u\n", |
Pau Espin Pedrol | f027f17 | 2019-05-06 13:57:31 +0200 | [diff] [blame] | 460 | cfg->osmux_addr, cfg->osmux_port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 461 | return ret; |
| 462 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 463 | |
| 464 | ret = osmo_fd_register(&osmux_fd); |
| 465 | if (ret < 0) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 466 | LOGP(DOSMUX, LOGL_ERROR, "cannot register OSMUX socket %s\n", |
Pau Espin Pedrol | f027f17 | 2019-05-06 13:57:31 +0200 | [diff] [blame] | 467 | osmo_sock_get_name2(osmux_fd.fd)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 468 | return ret; |
| 469 | } |
Pau Espin Pedrol | b7f52b4 | 2022-09-25 00:14:03 +0200 | [diff] [blame] | 470 | cfg->osmux_initialized = true; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 471 | |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 472 | LOGP(DOSMUX, LOGL_INFO, "OSMUX socket listening on %s\n", |
Pau Espin Pedrol | f027f17 | 2019-05-06 13:57:31 +0200 | [diff] [blame] | 473 | osmo_sock_get_name2(osmux_fd.fd)); |
| 474 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 475 | return 0; |
| 476 | } |
| 477 | |
Pau Espin Pedrol | d48a811 | 2022-09-27 12:37:53 +0200 | [diff] [blame] | 478 | /*! Initialize Osmux bits of a conn. |
| 479 | * \param[in] conn Osmux connection to initialize |
| 480 | * \returns 0 on success, negative on ERROR */ |
| 481 | int osmux_init_conn(struct mgcp_conn_rtp *conn) |
| 482 | { |
| 483 | if (conn_osmux_allocate_local_cid(conn) == -1) |
| 484 | return -1; |
| 485 | conn->osmux.ctrg = rate_ctr_group_alloc(conn->conn, &rate_ctr_group_osmux_desc, conn->ctrg->idx); |
| 486 | |
| 487 | conn->osmux.state = OSMUX_STATE_ACTIVATING; |
| 488 | return 0; |
| 489 | } |
| 490 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 491 | /*! enable OSXMUX circuit for a specified connection. |
| 492 | * \param[in] endp mgcp endpoint (configuration) |
| 493 | * \param[in] conn connection to disable |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 494 | * \param[in] addr IP address and port of remote OSMUX endpoint |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 495 | * \returns 0 on success, -1 on ERROR */ |
| 496 | int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn, |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 497 | const struct osmo_sockaddr *rem_addr) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 498 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 499 | /*! If osmux is enabled, initialize the output handler. This handler is |
| 500 | * used to reconstruct the RTP flow from osmux. The RTP SSRC is |
| 501 | * allocated based on the circuit ID (conn_net->osmux.cid), which is unique |
| 502 | * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP |
Pau Espin Pedrol | 17bf603 | 2018-09-17 13:38:39 +0200 | [diff] [blame] | 503 | * SSRC space (2^32) by the OSMUX_CID_MAX + 1 possible circuit IDs, then randomly |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 504 | * select one value from that window. Thus, we have no chance to have |
| 505 | * overlapping RTP SSRC traveling to the BTSes behind the BSC, |
| 506 | * similarly, for flows traveling to the MSC. |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 507 | */ |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 508 | struct in6_addr addr_unset = {}; |
Pau Espin Pedrol | 17bf603 | 2018-09-17 13:38:39 +0200 | [diff] [blame] | 509 | static const uint32_t rtp_ssrc_winlen = UINT32_MAX / (OSMUX_CID_MAX + 1); |
Eric | fbf78d1 | 2021-08-23 22:31:39 +0200 | [diff] [blame] | 510 | uint16_t osmux_dummy = endp->trunk->cfg->osmux_dummy; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 511 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 512 | /* Check if osmux is enabled for the specified connection */ |
Pau Espin Pedrol | 48aff62 | 2018-10-16 16:03:57 +0200 | [diff] [blame] | 513 | if (conn->osmux.state != OSMUX_STATE_ACTIVATING) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 514 | LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 515 | "conn:%s didn't negotiate Osmux, state %d\n", |
| 516 | mgcp_conn_dump(conn->conn), conn->osmux.state); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 517 | return -1; |
| 518 | } |
| 519 | |
Pau Espin Pedrol | dac2ca7 | 2019-05-13 17:34:51 +0200 | [diff] [blame] | 520 | /* Wait until we have the connection information from MDCX */ |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 521 | if (memcmp(&conn->end.addr, &addr_unset, |
| 522 | conn->end.addr.u.sa.sa_family == AF_INET6 ? |
| 523 | sizeof(struct in6_addr) : |
| 524 | sizeof(struct in_addr)) == 0) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 525 | LOGPCONN(conn->conn, DOSMUX, LOGL_INFO, |
Pau Espin Pedrol | dac2ca7 | 2019-05-13 17:34:51 +0200 | [diff] [blame] | 526 | "Osmux remote address/port still unknown\n"); |
| 527 | return -1; |
| 528 | } |
| 529 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 530 | conn->osmux.in = osmux_handle_find_or_create(conn, rem_addr); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 531 | if (!conn->osmux.in) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 532 | LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 533 | "Cannot allocate input osmux handle for conn:%s\n", |
| 534 | mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 535 | return -1; |
| 536 | } |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 537 | if (osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.remote_cid, osmux_dummy) < 0) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 538 | LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 539 | "Cannot open osmux circuit %u for conn:%s\n", |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 540 | conn->osmux.remote_cid, mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 541 | return -1; |
| 542 | } |
| 543 | |
Pau Espin Pedrol | 941e317 | 2022-09-22 21:16:56 +0200 | [diff] [blame] | 544 | conn->osmux.out = osmux_xfrm_output_alloc(conn->conn); |
Pau Espin Pedrol | 3363916 | 2022-08-31 17:46:11 +0200 | [diff] [blame] | 545 | osmux_xfrm_output_set_rtp_ssrc(conn->osmux.out, |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 546 | (conn->osmux.remote_cid * rtp_ssrc_winlen) + |
Pau Espin Pedrol | 3363916 | 2022-08-31 17:46:11 +0200 | [diff] [blame] | 547 | (random() % rtp_ssrc_winlen)); |
| 548 | osmux_xfrm_output_set_rtp_pl_type(conn->osmux.out, conn->end.codec->payload_type); |
| 549 | osmux_xfrm_output_set_tx_cb(conn->osmux.out, |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 550 | scheduled_from_osmux_tx_rtp_cb, conn); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 551 | |
| 552 | conn->osmux.state = OSMUX_STATE_ENABLED; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 557 | /*! disable OSXMUX circuit for a specified connection. |
| 558 | * \param[in] conn connection to disable */ |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 559 | void conn_osmux_disable(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 560 | { |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 561 | OSMO_ASSERT(conn->osmux.state != OSMUX_STATE_DISABLED); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 562 | |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 563 | LOGPCONN(conn->conn, DOSMUX, LOGL_INFO, |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 564 | "Releasing connection using local Osmux CID %u\n", conn->osmux.local_cid); |
Pau Espin Pedrol | f2321b7 | 2018-05-17 13:55:14 +0200 | [diff] [blame] | 565 | |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 566 | struct rate_ctr_group *all_osmux_stats = conn->conn->endp->trunk->ratectr.all_osmux_conn_stats; |
| 567 | rate_ctr_inc(rate_ctr_group_get_ctr(all_osmux_stats, OSMUX_NUM_CONNECTIONS)); |
| 568 | |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 569 | if (conn->osmux.state == OSMUX_STATE_ENABLED) { |
| 570 | /* We are closing, we don't need pending RTP packets to be transmitted */ |
Pau Espin Pedrol | 3363916 | 2022-08-31 17:46:11 +0200 | [diff] [blame] | 571 | osmux_xfrm_output_set_tx_cb(conn->osmux.out, NULL, NULL); |
| 572 | TALLOC_FREE(conn->osmux.out); |
Pau Espin Pedrol | f2321b7 | 2018-05-17 13:55:14 +0200 | [diff] [blame] | 573 | |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 574 | osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.remote_cid); |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 575 | conn->osmux.state = OSMUX_STATE_DISABLED; |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 576 | osmux_handle_put(conn->osmux.in); |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 577 | conn->osmux.remote_cid = 0; |
| 578 | conn->osmux.remote_cid_present = false; |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 579 | } |
Pau Espin Pedrol | d48a811 | 2022-09-27 12:37:53 +0200 | [diff] [blame] | 580 | |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 581 | conn_osmux_release_local_cid(conn); |
Pau Espin Pedrol | d48a811 | 2022-09-27 12:37:53 +0200 | [diff] [blame] | 582 | |
| 583 | rate_ctr_group_free(conn->osmux.ctrg); |
| 584 | conn->osmux.ctrg = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 585 | } |
| 586 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 587 | /*! relase OSXMUX cid, that had been allocated to this connection. |
| 588 | * \param[in] conn connection with OSMUX cid to release */ |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 589 | void conn_osmux_release_local_cid(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 590 | { |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 591 | if (conn->osmux.local_cid_allocated) |
| 592 | osmux_cid_pool_put(conn->osmux.local_cid); |
| 593 | conn->osmux.local_cid = 0; |
| 594 | conn->osmux.local_cid_allocated = false; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 595 | } |
| 596 | |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 597 | /*! allocate local OSMUX cid to connection. |
| 598 | * \param[in] conn connection for which we allocate the local OSMUX cid |
| 599 | * \returns Allocated OSMUX cid, -1 on error (no free CIDs avail). |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 600 | */ |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 601 | int conn_osmux_allocate_local_cid(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 602 | { |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 603 | OSMO_ASSERT(conn->osmux.local_cid_allocated == false); |
| 604 | int osmux_cid = osmux_cid_pool_get_next(); |
| 605 | if (osmux_cid == -1) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 606 | LOGPCONN(conn->conn, DOSMUX, LOGL_INFO, |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 607 | "no available local Osmux CID to allocate!\n"); |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 608 | return -1; |
| 609 | } |
| 610 | |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 611 | conn->osmux.local_cid = (uint8_t) osmux_cid; |
| 612 | conn->osmux.local_cid_allocated = true; |
Pau Espin Pedrol | 9d939b6 | 2022-10-03 16:59:20 +0200 | [diff] [blame] | 613 | conn->type = MGCP_RTP_OSMUX; |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 614 | return osmux_cid; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 615 | } |
| 616 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 617 | /*! send RTP dummy packet to OSMUX connection port. |
| 618 | * \param[in] endp mcgp endpoint that holds the RTP connection |
| 619 | * \param[in] conn associated RTP connection |
| 620 | * \returns bytes sent, -1 on error */ |
| 621 | int osmux_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 622 | { |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 623 | char ipbuf[INET6_ADDRSTRLEN]; |
Pau Espin Pedrol | c1ad553 | 2019-05-15 23:00:53 +0200 | [diff] [blame] | 624 | struct osmux_hdr *osmuxh; |
| 625 | int buf_len; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 626 | struct in_addr addr_unset = {}; |
| 627 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 628 | /*! The dummy packet will not be sent via the actual OSMUX connection, |
| 629 | * instead it is sent out of band to port where the remote OSMUX |
| 630 | * multplexer is listening. The goal is to ensure that the connection |
| 631 | * is kept open */ |
| 632 | |
| 633 | /*! We don't need to send the dummy load for osmux so often as another |
| 634 | * endpoint may have already punched the hole in the firewall. This |
| 635 | * approach is simple though. */ |
| 636 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 637 | /* Wait until we have the connection information from MDCX */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 638 | if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 639 | return 0; |
| 640 | |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 641 | if (endp_osmux_state_check(endp, conn, true) < 0) |
| 642 | return 0; |
| 643 | |
Pau Espin Pedrol | c1ad553 | 2019-05-15 23:00:53 +0200 | [diff] [blame] | 644 | buf_len = sizeof(struct osmux_hdr) + osmo_amr_bytes(AMR_FT_0); |
| 645 | osmuxh = (struct osmux_hdr *) alloca(buf_len); |
| 646 | memset(osmuxh, 0, buf_len); |
| 647 | osmuxh->ft = OSMUX_FT_DUMMY; |
| 648 | osmuxh->amr_ft = AMR_FT_0; |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 649 | osmuxh->circuit_id = conn->osmux.remote_cid; |
Pau Espin Pedrol | c1ad553 | 2019-05-15 23:00:53 +0200 | [diff] [blame] | 650 | |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 651 | LOGPCONN(conn->conn, DOSMUX, LOGL_DEBUG, |
Pau Espin Pedrol | 3752522 | 2019-05-13 17:34:14 +0200 | [diff] [blame] | 652 | "sending OSMUX dummy load to %s:%u CID %u\n", |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 653 | osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 654 | ntohs(conn->end.rtp_port), conn->osmux.remote_cid); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 655 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 656 | return mgcp_udp_send(osmux_fd.fd, &conn->end.addr, |
Pau Espin Pedrol | c1ad553 | 2019-05-15 23:00:53 +0200 | [diff] [blame] | 657 | conn->end.rtp_port, (char*)osmuxh, buf_len); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 658 | } |
| 659 | |
Pau Espin Pedrol | bcd52e5 | 2018-09-17 12:41:28 +0200 | [diff] [blame] | 660 | /* bsc-nat allocates/releases the Osmux circuit ID. +7 to round up to 8 bit boundary. */ |
| 661 | static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1 + 7) / 8]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 662 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 663 | /*! count the number of taken OSMUX cids. |
| 664 | * \returns number of OSMUX cids in use */ |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 665 | int osmux_cid_pool_count_used(void) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 666 | { |
| 667 | int i, j, used = 0; |
| 668 | |
| 669 | for (i = 0; i < sizeof(osmux_cid_bitmap); i++) { |
| 670 | for (j = 0; j < 8; j++) { |
| 671 | if (osmux_cid_bitmap[i] & (1 << j)) |
| 672 | used += 1; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | return used; |
| 677 | } |
| 678 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 679 | /*! take a free OSMUX cid. |
| 680 | * \returns OSMUX cid */ |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 681 | int osmux_cid_pool_get_next(void) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 682 | { |
| 683 | int i, j; |
| 684 | |
| 685 | for (i = 0; i < sizeof(osmux_cid_bitmap); i++) { |
| 686 | for (j = 0; j < 8; j++) { |
| 687 | if (osmux_cid_bitmap[i] & (1 << j)) |
| 688 | continue; |
| 689 | |
| 690 | osmux_cid_bitmap[i] |= (1 << j); |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 691 | LOGP(DOSMUX, LOGL_DEBUG, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 692 | "Allocating Osmux CID %u from pool\n", (i * 8) + j); |
| 693 | return (i * 8) + j; |
| 694 | } |
| 695 | } |
| 696 | |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 697 | LOGP(DOSMUX, LOGL_ERROR, "All Osmux circuits are in use!\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 698 | return -1; |
| 699 | } |
| 700 | |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 701 | /*! take a specific OSMUX cid. |
| 702 | * \param[in] osmux_cid OSMUX cid */ |
| 703 | void osmux_cid_pool_get(uint8_t osmux_cid) |
| 704 | { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 705 | LOGP(DOSMUX, LOGL_DEBUG, "Allocating Osmux CID %u from pool\n", osmux_cid); |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 706 | osmux_cid_bitmap[osmux_cid / 8] |= (1 << (osmux_cid % 8)); |
| 707 | } |
| 708 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 709 | /*! put back a no longer used OSMUX cid. |
| 710 | * \param[in] osmux_cid OSMUX cid */ |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 711 | void osmux_cid_pool_put(uint8_t osmux_cid) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 712 | { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 713 | LOGP(DOSMUX, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 714 | osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8)); |
| 715 | } |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 716 | |
| 717 | /*! check if OSMUX cid is already taken */ |
| 718 | bool osmux_cid_pool_allocated(uint8_t osmux_cid) |
| 719 | { |
| 720 | return !!(osmux_cid_bitmap[osmux_cid / 8] & (1 << (osmux_cid % 8))); |
| 721 | } |