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