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 | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 298 | struct osmo_sockaddr addr = { /* FIXME: do we know the source address?? */ }; |
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, |
| 303 | .from_addr = &addr, |
| 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 | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 366 | static int osmux_legacy_dummy_parse_cid(const struct osmo_sockaddr *rem_addr, struct msgb *msg, |
Pau Espin Pedrol | 662fa42 | 2018-10-16 15:54:38 +0200 | [diff] [blame] | 367 | uint8_t *osmux_cid) |
| 368 | { |
Pau Espin Pedrol | b5583cd | 2019-05-13 12:58:24 +0200 | [diff] [blame] | 369 | if (msg->len < 1 + sizeof(*osmux_cid)) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 370 | LOGP(DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | b5583cd | 2019-05-13 12:58:24 +0200 | [diff] [blame] | 371 | "Discarding truncated Osmux dummy load: %s\n", osmo_hexdump(msg->data, msg->len)); |
Pau Espin Pedrol | 662fa42 | 2018-10-16 15:54:38 +0200 | [diff] [blame] | 372 | return -1; |
| 373 | } |
| 374 | |
| 375 | /* extract the osmux CID from the dummy message */ |
| 376 | memcpy(osmux_cid, &msg->data[1], sizeof(*osmux_cid)); |
| 377 | return 0; |
| 378 | } |
| 379 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 380 | /* This is called from the bsc-nat */ |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 381 | static int osmux_handle_dummy(struct mgcp_trunk *trunk, const struct osmo_sockaddr *rem_addr, |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 382 | struct msgb *msg) |
| 383 | { |
| 384 | uint8_t osmux_cid; |
| 385 | struct mgcp_conn_rtp *conn; |
| 386 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 387 | if (osmux_legacy_dummy_parse_cid(rem_addr, msg, &osmux_cid) < 0) |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 388 | goto out; |
| 389 | |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 390 | conn = osmux_conn_lookup(trunk, osmux_cid, rem_addr); |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 391 | if (!conn) { |
Pau Espin Pedrol | 48fb176 | 2022-09-29 15:50:07 +0200 | [diff] [blame] | 392 | LOGP(DOSMUX, LOGL_DEBUG, |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 393 | "Cannot find conn for Osmux CID %d\n", osmux_cid); |
| 394 | goto out; |
| 395 | } |
| 396 | |
| 397 | endp_osmux_state_check(conn->conn->endp, conn, false); |
| 398 | /* Only needed to punch hole in firewall, it can be dropped */ |
| 399 | out: |
| 400 | msgb_free(msg); |
| 401 | return 0; |
| 402 | } |
| 403 | |
Pau Espin Pedrol | 3fbf035 | 2022-09-22 19:01:33 +0200 | [diff] [blame] | 404 | #define osmux_chunk_length(msg, rem) ((rem) - (msg)->len) |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 405 | 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] | 406 | { |
| 407 | struct msgb *msg; |
| 408 | struct osmux_hdr *osmuxh; |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 409 | struct osmo_sockaddr rem_addr; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 410 | uint32_t rem; |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 411 | struct mgcp_trunk *trunk = ofd->data; |
| 412 | struct rate_ctr_group *all_rtp_stats = trunk->ratectr.all_osmux_conn_stats; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 413 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 414 | msg = osmux_recv(ofd, &rem_addr); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 415 | if (!msg) |
| 416 | return -1; |
| 417 | |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 418 | rate_ctr_inc(rate_ctr_group_get_ctr(all_rtp_stats, OSMUX_PACKETS_RX_CTR)); |
| 419 | |
Pau Espin Pedrol | 928a20b | 2022-09-23 15:38:24 +0200 | [diff] [blame] | 420 | if (trunk->cfg->osmux_use == OSMUX_USAGE_OFF) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 421 | LOGP(DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | d14163e | 2018-10-16 15:48:59 +0200 | [diff] [blame] | 422 | "bsc-nat wants to use Osmux but bsc did not request it\n"); |
| 423 | goto out; |
| 424 | } |
| 425 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 426 | /* not any further processing dummy messages */ |
Philipp Maier | b3d14eb | 2021-05-20 14:18:52 +0200 | [diff] [blame] | 427 | if (mgcp_is_rtp_dummy_payload(msg)) |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 428 | return osmux_handle_dummy(trunk, &rem_addr, msg); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 429 | |
| 430 | rem = msg->len; |
| 431 | while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) { |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 432 | struct mgcp_endpoint *endp; |
| 433 | struct mgcp_conn_rtp *conn_src; |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 434 | conn_src = osmux_conn_lookup(trunk, osmuxh->circuit_id, |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 435 | &rem_addr); |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 436 | if (!conn_src) { |
Pau Espin Pedrol | 48fb176 | 2022-09-29 15:50:07 +0200 | [diff] [blame] | 437 | LOGP(DOSMUX, LOGL_DEBUG, |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 438 | "Cannot find a src conn for circuit_id=%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 439 | osmuxh->circuit_id); |
Pau Espin Pedrol | 1c5fcb0 | 2022-09-29 15:59:15 +0200 | [diff] [blame] | 440 | rem = msg->len; |
| 441 | continue; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 442 | } |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 443 | endp = conn_src->conn->endp; |
Pau Espin Pedrol | de13311 | 2020-09-08 17:51:36 +0200 | [diff] [blame] | 444 | mgcp_conn_watchdog_kick(conn_src->conn); |
| 445 | |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 446 | if (endp_osmux_state_check(endp, conn_src, false) == 0) { |
Pau Espin Pedrol | b4a5947 | 2022-09-23 18:08:53 +0200 | [diff] [blame] | 447 | rtpconn_osmux_rate_ctr_inc(conn_src, OSMUX_CHUNKS_RX_CTR); |
| 448 | rtpconn_osmux_rate_ctr_add(conn_src, OSMUX_OCTETS_RX_CTR, |
| 449 | osmux_chunk_length(msg, rem)); |
Pau Espin Pedrol | 3363916 | 2022-08-31 17:46:11 +0200 | [diff] [blame] | 450 | osmux_xfrm_output_sched(conn_src->osmux.out, osmuxh); |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 451 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 452 | rem = msg->len; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 453 | } |
| 454 | out: |
| 455 | msgb_free(msg); |
| 456 | return 0; |
| 457 | } |
| 458 | |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 459 | int osmux_init(int role, struct mgcp_trunk *trunk) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 460 | { |
| 461 | int ret; |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 462 | struct mgcp_config *cfg = trunk->cfg; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 463 | |
Pau Espin Pedrol | 21bcf6a | 2022-09-22 18:23:04 +0200 | [diff] [blame] | 464 | /* So far we only support running on one trunk: */ |
| 465 | OSMO_ASSERT(trunk == mgcp_trunk_by_num(cfg, MGCP_TRUNK_VIRTUAL, MGCP_VIRT_TRUNK_ID)); |
| 466 | |
| 467 | 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] | 468 | |
Harald Welte | 55a9229 | 2021-04-28 19:06:34 +0200 | [diff] [blame] | 469 | ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port, |
| 470 | cfg->endp_dscp, cfg->endp_priority); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 471 | if (ret < 0) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 472 | 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] | 473 | cfg->osmux_addr, cfg->osmux_port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 474 | return ret; |
| 475 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 476 | |
| 477 | ret = osmo_fd_register(&osmux_fd); |
| 478 | if (ret < 0) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 479 | LOGP(DOSMUX, LOGL_ERROR, "cannot register OSMUX socket %s\n", |
Pau Espin Pedrol | f027f17 | 2019-05-06 13:57:31 +0200 | [diff] [blame] | 480 | osmo_sock_get_name2(osmux_fd.fd)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 481 | return ret; |
| 482 | } |
Pau Espin Pedrol | b7f52b4 | 2022-09-25 00:14:03 +0200 | [diff] [blame] | 483 | cfg->osmux_initialized = true; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 484 | |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 485 | LOGP(DOSMUX, LOGL_INFO, "OSMUX socket listening on %s\n", |
Pau Espin Pedrol | f027f17 | 2019-05-06 13:57:31 +0200 | [diff] [blame] | 486 | osmo_sock_get_name2(osmux_fd.fd)); |
| 487 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 488 | return 0; |
| 489 | } |
| 490 | |
Pau Espin Pedrol | d48a811 | 2022-09-27 12:37:53 +0200 | [diff] [blame] | 491 | /*! Initialize Osmux bits of a conn. |
| 492 | * \param[in] conn Osmux connection to initialize |
| 493 | * \returns 0 on success, negative on ERROR */ |
| 494 | int osmux_init_conn(struct mgcp_conn_rtp *conn) |
| 495 | { |
| 496 | if (conn_osmux_allocate_local_cid(conn) == -1) |
| 497 | return -1; |
| 498 | conn->osmux.ctrg = rate_ctr_group_alloc(conn->conn, &rate_ctr_group_osmux_desc, conn->ctrg->idx); |
| 499 | |
| 500 | conn->osmux.state = OSMUX_STATE_ACTIVATING; |
| 501 | return 0; |
| 502 | } |
| 503 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 504 | /*! enable OSXMUX circuit for a specified connection. |
| 505 | * \param[in] endp mgcp endpoint (configuration) |
| 506 | * \param[in] conn connection to disable |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 507 | * \param[in] addr IP address and port of remote OSMUX endpoint |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 508 | * \returns 0 on success, -1 on ERROR */ |
| 509 | 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] | 510 | const struct osmo_sockaddr *rem_addr) |
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 | /*! If osmux is enabled, initialize the output handler. This handler is |
| 513 | * used to reconstruct the RTP flow from osmux. The RTP SSRC is |
| 514 | * allocated based on the circuit ID (conn_net->osmux.cid), which is unique |
| 515 | * 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] | 516 | * 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] | 517 | * select one value from that window. Thus, we have no chance to have |
| 518 | * overlapping RTP SSRC traveling to the BTSes behind the BSC, |
| 519 | * similarly, for flows traveling to the MSC. |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 520 | */ |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 521 | struct in6_addr addr_unset = {}; |
Pau Espin Pedrol | 17bf603 | 2018-09-17 13:38:39 +0200 | [diff] [blame] | 522 | static const uint32_t rtp_ssrc_winlen = UINT32_MAX / (OSMUX_CID_MAX + 1); |
Eric | fbf78d1 | 2021-08-23 22:31:39 +0200 | [diff] [blame] | 523 | uint16_t osmux_dummy = endp->trunk->cfg->osmux_dummy; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 524 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 525 | /* Check if osmux is enabled for the specified connection */ |
Pau Espin Pedrol | 48aff62 | 2018-10-16 16:03:57 +0200 | [diff] [blame] | 526 | if (conn->osmux.state != OSMUX_STATE_ACTIVATING) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 527 | LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 528 | "conn:%s didn't negotiate Osmux, state %d\n", |
| 529 | mgcp_conn_dump(conn->conn), conn->osmux.state); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 530 | return -1; |
| 531 | } |
| 532 | |
Pau Espin Pedrol | dac2ca7 | 2019-05-13 17:34:51 +0200 | [diff] [blame] | 533 | /* Wait until we have the connection information from MDCX */ |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 534 | if (memcmp(&conn->end.addr, &addr_unset, |
| 535 | conn->end.addr.u.sa.sa_family == AF_INET6 ? |
| 536 | sizeof(struct in6_addr) : |
| 537 | sizeof(struct in_addr)) == 0) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 538 | LOGPCONN(conn->conn, DOSMUX, LOGL_INFO, |
Pau Espin Pedrol | dac2ca7 | 2019-05-13 17:34:51 +0200 | [diff] [blame] | 539 | "Osmux remote address/port still unknown\n"); |
| 540 | return -1; |
| 541 | } |
| 542 | |
Pau Espin Pedrol | 15e7e4f | 2022-09-09 16:52:41 +0200 | [diff] [blame] | 543 | conn->osmux.in = osmux_handle_find_or_create(conn, rem_addr); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 544 | if (!conn->osmux.in) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 545 | LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 546 | "Cannot allocate input osmux handle for conn:%s\n", |
| 547 | mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 548 | return -1; |
| 549 | } |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 550 | 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] | 551 | LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR, |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 552 | "Cannot open osmux circuit %u for conn:%s\n", |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 553 | conn->osmux.remote_cid, mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 554 | return -1; |
| 555 | } |
| 556 | |
Pau Espin Pedrol | 941e317 | 2022-09-22 21:16:56 +0200 | [diff] [blame] | 557 | conn->osmux.out = osmux_xfrm_output_alloc(conn->conn); |
Pau Espin Pedrol | 3363916 | 2022-08-31 17:46:11 +0200 | [diff] [blame] | 558 | osmux_xfrm_output_set_rtp_ssrc(conn->osmux.out, |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 559 | (conn->osmux.remote_cid * rtp_ssrc_winlen) + |
Pau Espin Pedrol | 3363916 | 2022-08-31 17:46:11 +0200 | [diff] [blame] | 560 | (random() % rtp_ssrc_winlen)); |
| 561 | osmux_xfrm_output_set_rtp_pl_type(conn->osmux.out, conn->end.codec->payload_type); |
| 562 | osmux_xfrm_output_set_tx_cb(conn->osmux.out, |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 563 | scheduled_from_osmux_tx_rtp_cb, conn); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 564 | |
| 565 | conn->osmux.state = OSMUX_STATE_ENABLED; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 570 | /*! disable OSXMUX circuit for a specified connection. |
| 571 | * \param[in] conn connection to disable */ |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 572 | void conn_osmux_disable(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 573 | { |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 574 | OSMO_ASSERT(conn->osmux.state != OSMUX_STATE_DISABLED); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 575 | |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 576 | LOGPCONN(conn->conn, DOSMUX, LOGL_INFO, |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 577 | "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] | 578 | |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 579 | struct rate_ctr_group *all_osmux_stats = conn->conn->endp->trunk->ratectr.all_osmux_conn_stats; |
| 580 | rate_ctr_inc(rate_ctr_group_get_ctr(all_osmux_stats, OSMUX_NUM_CONNECTIONS)); |
| 581 | |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 582 | if (conn->osmux.state == OSMUX_STATE_ENABLED) { |
| 583 | /* 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] | 584 | osmux_xfrm_output_set_tx_cb(conn->osmux.out, NULL, NULL); |
| 585 | TALLOC_FREE(conn->osmux.out); |
Pau Espin Pedrol | f2321b7 | 2018-05-17 13:55:14 +0200 | [diff] [blame] | 586 | |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 587 | 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] | 588 | conn->osmux.state = OSMUX_STATE_DISABLED; |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 589 | osmux_handle_put(conn->osmux.in); |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 590 | conn->osmux.remote_cid = 0; |
| 591 | conn->osmux.remote_cid_present = false; |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 592 | } |
Pau Espin Pedrol | d48a811 | 2022-09-27 12:37:53 +0200 | [diff] [blame] | 593 | |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 594 | conn_osmux_release_local_cid(conn); |
Pau Espin Pedrol | d48a811 | 2022-09-27 12:37:53 +0200 | [diff] [blame] | 595 | |
| 596 | rate_ctr_group_free(conn->osmux.ctrg); |
| 597 | conn->osmux.ctrg = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 598 | } |
| 599 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 600 | /*! relase OSXMUX cid, that had been allocated to this connection. |
| 601 | * \param[in] conn connection with OSMUX cid to release */ |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 602 | void conn_osmux_release_local_cid(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 603 | { |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 604 | if (conn->osmux.local_cid_allocated) |
| 605 | osmux_cid_pool_put(conn->osmux.local_cid); |
| 606 | conn->osmux.local_cid = 0; |
| 607 | conn->osmux.local_cid_allocated = false; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 608 | } |
| 609 | |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 610 | /*! allocate local OSMUX cid to connection. |
| 611 | * \param[in] conn connection for which we allocate the local OSMUX cid |
| 612 | * \returns Allocated OSMUX cid, -1 on error (no free CIDs avail). |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 613 | */ |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 614 | int conn_osmux_allocate_local_cid(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 615 | { |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 616 | OSMO_ASSERT(conn->osmux.local_cid_allocated == false); |
| 617 | int osmux_cid = osmux_cid_pool_get_next(); |
| 618 | if (osmux_cid == -1) { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 619 | LOGPCONN(conn->conn, DOSMUX, LOGL_INFO, |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 620 | "no available local Osmux CID to allocate!\n"); |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 621 | return -1; |
| 622 | } |
| 623 | |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 624 | conn->osmux.local_cid = (uint8_t) osmux_cid; |
| 625 | conn->osmux.local_cid_allocated = true; |
Pau Espin Pedrol | 9d939b6 | 2022-10-03 16:59:20 +0200 | [diff] [blame] | 626 | conn->type = MGCP_RTP_OSMUX; |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 627 | return osmux_cid; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 628 | } |
| 629 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 630 | /*! send RTP dummy packet to OSMUX connection port. |
| 631 | * \param[in] endp mcgp endpoint that holds the RTP connection |
| 632 | * \param[in] conn associated RTP connection |
| 633 | * \returns bytes sent, -1 on error */ |
| 634 | 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] | 635 | { |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 636 | char ipbuf[INET6_ADDRSTRLEN]; |
Pau Espin Pedrol | c1ad553 | 2019-05-15 23:00:53 +0200 | [diff] [blame] | 637 | struct osmux_hdr *osmuxh; |
| 638 | int buf_len; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 639 | struct in_addr addr_unset = {}; |
| 640 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 641 | /*! The dummy packet will not be sent via the actual OSMUX connection, |
| 642 | * instead it is sent out of band to port where the remote OSMUX |
| 643 | * multplexer is listening. The goal is to ensure that the connection |
| 644 | * is kept open */ |
| 645 | |
| 646 | /*! We don't need to send the dummy load for osmux so often as another |
| 647 | * endpoint may have already punched the hole in the firewall. This |
| 648 | * approach is simple though. */ |
| 649 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 650 | /* Wait until we have the connection information from MDCX */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 651 | if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 652 | return 0; |
| 653 | |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 654 | if (endp_osmux_state_check(endp, conn, true) < 0) |
| 655 | return 0; |
| 656 | |
Pau Espin Pedrol | c1ad553 | 2019-05-15 23:00:53 +0200 | [diff] [blame] | 657 | buf_len = sizeof(struct osmux_hdr) + osmo_amr_bytes(AMR_FT_0); |
| 658 | osmuxh = (struct osmux_hdr *) alloca(buf_len); |
| 659 | memset(osmuxh, 0, buf_len); |
| 660 | osmuxh->ft = OSMUX_FT_DUMMY; |
| 661 | osmuxh->amr_ft = AMR_FT_0; |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 662 | osmuxh->circuit_id = conn->osmux.remote_cid; |
Pau Espin Pedrol | c1ad553 | 2019-05-15 23:00:53 +0200 | [diff] [blame] | 663 | |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 664 | LOGPCONN(conn->conn, DOSMUX, LOGL_DEBUG, |
Pau Espin Pedrol | 3752522 | 2019-05-13 17:34:14 +0200 | [diff] [blame] | 665 | "sending OSMUX dummy load to %s:%u CID %u\n", |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 666 | osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf), |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 667 | ntohs(conn->end.rtp_port), conn->osmux.remote_cid); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 668 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 669 | return mgcp_udp_send(osmux_fd.fd, &conn->end.addr, |
Pau Espin Pedrol | c1ad553 | 2019-05-15 23:00:53 +0200 | [diff] [blame] | 670 | conn->end.rtp_port, (char*)osmuxh, buf_len); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 671 | } |
| 672 | |
Pau Espin Pedrol | bcd52e5 | 2018-09-17 12:41:28 +0200 | [diff] [blame] | 673 | /* bsc-nat allocates/releases the Osmux circuit ID. +7 to round up to 8 bit boundary. */ |
| 674 | static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1 + 7) / 8]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 675 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 676 | /*! count the number of taken OSMUX cids. |
| 677 | * \returns number of OSMUX cids in use */ |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 678 | int osmux_cid_pool_count_used(void) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 679 | { |
| 680 | int i, j, used = 0; |
| 681 | |
| 682 | for (i = 0; i < sizeof(osmux_cid_bitmap); i++) { |
| 683 | for (j = 0; j < 8; j++) { |
| 684 | if (osmux_cid_bitmap[i] & (1 << j)) |
| 685 | used += 1; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | return used; |
| 690 | } |
| 691 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 692 | /*! take a free OSMUX cid. |
| 693 | * \returns OSMUX cid */ |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 694 | int osmux_cid_pool_get_next(void) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 695 | { |
| 696 | int i, j; |
| 697 | |
| 698 | for (i = 0; i < sizeof(osmux_cid_bitmap); i++) { |
| 699 | for (j = 0; j < 8; j++) { |
| 700 | if (osmux_cid_bitmap[i] & (1 << j)) |
| 701 | continue; |
| 702 | |
| 703 | osmux_cid_bitmap[i] |= (1 << j); |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 704 | LOGP(DOSMUX, LOGL_DEBUG, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 705 | "Allocating Osmux CID %u from pool\n", (i * 8) + j); |
| 706 | return (i * 8) + j; |
| 707 | } |
| 708 | } |
| 709 | |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 710 | LOGP(DOSMUX, LOGL_ERROR, "All Osmux circuits are in use!\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 711 | return -1; |
| 712 | } |
| 713 | |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 714 | /*! take a specific OSMUX cid. |
| 715 | * \param[in] osmux_cid OSMUX cid */ |
| 716 | void osmux_cid_pool_get(uint8_t osmux_cid) |
| 717 | { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 718 | 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] | 719 | osmux_cid_bitmap[osmux_cid / 8] |= (1 << (osmux_cid % 8)); |
| 720 | } |
| 721 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 722 | /*! put back a no longer used OSMUX cid. |
| 723 | * \param[in] osmux_cid OSMUX cid */ |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 724 | void osmux_cid_pool_put(uint8_t osmux_cid) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 725 | { |
Pau Espin Pedrol | ec5e85b | 2022-09-09 16:04:19 +0200 | [diff] [blame] | 726 | 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] | 727 | osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8)); |
| 728 | } |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 729 | |
| 730 | /*! check if OSMUX cid is already taken */ |
| 731 | bool osmux_cid_pool_allocated(uint8_t osmux_cid) |
| 732 | { |
| 733 | return !!(osmux_cid_bitmap[osmux_cid / 8] & (1 << (osmux_cid % 8))); |
| 734 | } |