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> |
| 18 | #include <osmocom/core/talloc.h> |
| 19 | |
| 20 | #include <osmocom/netif/osmux.h> |
| 21 | #include <osmocom/netif/rtp.h> |
| 22 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 23 | #include <osmocom/mgcp/mgcp.h> |
| 24 | #include <osmocom/mgcp/mgcp_internal.h> |
| 25 | #include <osmocom/mgcp/osmux.h> |
| 26 | #include <osmocom/mgcp/mgcp_conn.h> |
Philipp Maier | 37d11c8 | 2018-02-01 14:38:12 +0100 | [diff] [blame] | 27 | #include <osmocom/mgcp/mgcp_endp.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 28 | |
| 29 | static struct osmo_fd osmux_fd; |
| 30 | |
| 31 | static LLIST_HEAD(osmux_handle_list); |
| 32 | |
| 33 | struct osmux_handle { |
| 34 | struct llist_head head; |
| 35 | struct osmux_in_handle *in; |
| 36 | struct in_addr rem_addr; |
| 37 | int rem_port; |
| 38 | int refcnt; |
| 39 | }; |
| 40 | |
| 41 | static void *osmux; |
| 42 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 43 | /* Deliver OSMUX batch to the remote end */ |
| 44 | static void osmux_deliver_cb(struct msgb *batch_msg, void *data) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 45 | { |
| 46 | struct osmux_handle *handle = data; |
| 47 | struct sockaddr_in out = { |
| 48 | .sin_family = AF_INET, |
| 49 | .sin_port = handle->rem_port, |
| 50 | }; |
| 51 | |
| 52 | memcpy(&out.sin_addr, &handle->rem_addr, sizeof(handle->rem_addr)); |
| 53 | sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0, |
| 54 | (struct sockaddr *)&out, sizeof(out)); |
| 55 | msgb_free(batch_msg); |
| 56 | } |
| 57 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 58 | /* Lookup existing OSMUX handle for specified destination address. */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 59 | static struct osmux_handle * |
| 60 | osmux_handle_find_get(struct in_addr *addr, int rem_port) |
| 61 | { |
| 62 | struct osmux_handle *h; |
| 63 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 64 | llist_for_each_entry(h, &osmux_handle_list, head) { |
| 65 | if (memcmp(&h->rem_addr, addr, sizeof(struct in_addr)) == 0 && |
| 66 | h->rem_port == rem_port) { |
| 67 | LOGP(DLMGCP, LOGL_DEBUG, "using existing OSMUX handle " |
| 68 | "for addr=%s:%d\n", |
| 69 | inet_ntoa(*addr), ntohs(rem_port)); |
| 70 | h->refcnt++; |
| 71 | return h; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return NULL; |
| 76 | } |
| 77 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 78 | /* Put down no longer needed OSMUX handle */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 79 | static void osmux_handle_put(struct osmux_in_handle *in) |
| 80 | { |
| 81 | struct osmux_handle *h; |
| 82 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 83 | llist_for_each_entry(h, &osmux_handle_list, head) { |
| 84 | if (h->in == in) { |
| 85 | if (--h->refcnt == 0) { |
| 86 | LOGP(DLMGCP, LOGL_INFO, |
| 87 | "Releasing unused osmux handle for %s:%d\n", |
| 88 | inet_ntoa(h->rem_addr), |
| 89 | ntohs(h->rem_port)); |
| 90 | LOGP(DLMGCP, LOGL_INFO, "Stats: " |
| 91 | "input RTP msgs: %u bytes: %"PRIu64" " |
| 92 | "output osmux msgs: %u bytes: %"PRIu64"\n", |
| 93 | in->stats.input_rtp_msgs, |
| 94 | in->stats.input_rtp_bytes, |
| 95 | in->stats.output_osmux_msgs, |
| 96 | in->stats.output_osmux_bytes); |
| 97 | llist_del(&h->head); |
| 98 | osmux_xfrm_input_fini(h->in); |
| 99 | talloc_free(h); |
| 100 | } |
| 101 | return; |
| 102 | } |
| 103 | } |
| 104 | LOGP(DLMGCP, LOGL_ERROR, "cannot find Osmux input handle %p\n", in); |
| 105 | } |
| 106 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 107 | /* Allocate free OSMUX handle */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 108 | static struct osmux_handle * |
| 109 | osmux_handle_alloc(struct mgcp_config *cfg, struct in_addr *addr, int rem_port) |
| 110 | { |
| 111 | struct osmux_handle *h; |
| 112 | |
| 113 | h = talloc_zero(osmux, struct osmux_handle); |
| 114 | if (!h) |
| 115 | return NULL; |
| 116 | h->rem_addr = *addr; |
| 117 | h->rem_port = rem_port; |
| 118 | h->refcnt++; |
| 119 | |
| 120 | h->in = talloc_zero(h, struct osmux_in_handle); |
| 121 | if (!h->in) { |
| 122 | talloc_free(h); |
| 123 | return NULL; |
| 124 | } |
| 125 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 126 | /* sequence number to start OSMUX message from */ |
| 127 | h->in->osmux_seq = 0; |
| 128 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 129 | h->in->batch_factor = cfg->osmux_batch; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 130 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 131 | /* If batch size is zero, the library defaults to 1470 bytes. */ |
| 132 | h->in->batch_size = cfg->osmux_batch_size; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 133 | h->in->deliver = osmux_deliver_cb; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 134 | osmux_xfrm_input_init(h->in); |
| 135 | h->in->data = h; |
| 136 | |
| 137 | llist_add(&h->head, &osmux_handle_list); |
| 138 | |
| 139 | LOGP(DLMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s:%d\n", |
| 140 | inet_ntoa(*addr), ntohs(rem_port)); |
| 141 | |
| 142 | return h; |
| 143 | } |
| 144 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 145 | /* 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] | 146 | * found, the function will automatically allocate one */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 147 | static struct osmux_in_handle * |
| 148 | osmux_handle_lookup(struct mgcp_config *cfg, struct in_addr *addr, int rem_port) |
| 149 | { |
| 150 | struct osmux_handle *h; |
| 151 | |
| 152 | h = osmux_handle_find_get(addr, rem_port); |
| 153 | if (h != NULL) |
| 154 | return h->in; |
| 155 | |
| 156 | h = osmux_handle_alloc(cfg, addr, rem_port); |
| 157 | if (h == NULL) |
| 158 | return NULL; |
| 159 | |
| 160 | return h->in; |
| 161 | } |
| 162 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 163 | /*! send RTP packet through OSMUX connection. |
| 164 | * \param[in] buf rtp data |
| 165 | * \param[in] buf_len length of rtp data |
| 166 | * \param[in] conn associated RTP connection |
| 167 | * \returns 0 on success, -1 on ERROR */ |
| 168 | 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] | 169 | { |
| 170 | int ret; |
| 171 | struct msgb *msg; |
| 172 | |
| 173 | msg = msgb_alloc(4096, "RTP"); |
| 174 | if (!msg) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 175 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 176 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 177 | memcpy(msg->data, buf, buf_len); |
| 178 | msgb_put(msg, buf_len); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 179 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 180 | while ((ret = osmux_xfrm_input(conn->osmux.in, msg, conn->osmux.cid)) > 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 181 | /* batch full, build and deliver it */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 182 | osmux_xfrm_input_deliver(conn->osmux.in); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 183 | } |
| 184 | return 0; |
| 185 | } |
| 186 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 187 | /* Lookup the endpoint that corresponds to the specified address (port) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 188 | static struct mgcp_endpoint * |
| 189 | endpoint_lookup(struct mgcp_config *cfg, int cid, |
| 190 | struct in_addr *from_addr, int type) |
| 191 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 192 | struct mgcp_endpoint *endp = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 193 | int i; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 194 | struct mgcp_conn_rtp *conn_net = NULL; |
| 195 | struct mgcp_conn_rtp *conn_bts = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 196 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 197 | for (i=0; i<cfg->trunk.number_endpoints; i++) { |
| 198 | struct in_addr *this; |
| 199 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 200 | endp = &cfg->trunk.endpoints[i]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 201 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 202 | #if 0 |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 203 | if (!tmp->allocated) |
| 204 | continue; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 205 | #endif |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 206 | |
| 207 | switch(type) { |
| 208 | case MGCP_DEST_NET: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 209 | /* FIXME: Get rid of CONN_ID_XXX! */ |
| 210 | conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET); |
Philipp Maier | ddf1f9d | 2017-11-07 12:18:38 +0100 | [diff] [blame] | 211 | if (conn_net) |
| 212 | this = &conn_net->end.addr; |
| 213 | else |
| 214 | this = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 215 | break; |
| 216 | case MGCP_DEST_BTS: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 217 | /* FIXME: Get rid of CONN_ID_XXX! */ |
| 218 | conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS); |
Philipp Maier | ddf1f9d | 2017-11-07 12:18:38 +0100 | [diff] [blame] | 219 | if (conn_bts) |
| 220 | this = &conn_bts->end.addr; |
| 221 | else |
| 222 | this = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 223 | break; |
| 224 | default: |
| 225 | /* Should not ever happen */ |
| 226 | LOGP(DLMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type); |
| 227 | return NULL; |
| 228 | } |
| 229 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 230 | /* FIXME: Get rid of CONN_ID_XXX! */ |
| 231 | conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET); |
Philipp Maier | ddf1f9d | 2017-11-07 12:18:38 +0100 | [diff] [blame] | 232 | if (conn_net && this && conn_net->osmux.cid == cid |
| 233 | && this->s_addr == from_addr->s_addr) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 234 | return endp; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | LOGP(DLMGCP, LOGL_ERROR, "Cannot find endpoint with cid=%d\n", cid); |
| 238 | |
| 239 | return NULL; |
| 240 | } |
| 241 | |
| 242 | static void scheduled_tx_net_cb(struct msgb *msg, void *data) |
| 243 | { |
| 244 | struct mgcp_endpoint *endp = data; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 245 | struct mgcp_conn_rtp *conn_net = NULL; |
| 246 | struct mgcp_conn_rtp *conn_bts = NULL; |
| 247 | |
| 248 | /* FIXME: Get rid of CONN_ID_XXX! */ |
| 249 | conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS); |
| 250 | conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET); |
| 251 | if (!conn_bts || !conn_net) |
| 252 | return; |
| 253 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 254 | struct sockaddr_in addr = { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 255 | .sin_addr = conn_net->end.addr, |
| 256 | .sin_port = conn_net->end.rtp_port, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 257 | }; |
| 258 | |
Harald Welte | a0ac30f | 2017-12-25 09:52:30 +0100 | [diff] [blame] | 259 | conn_bts->end.stats.octets_tx += msg->len; |
| 260 | conn_bts->end.stats.packets_tx++; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 261 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 262 | /* Send RTP data to NET */ |
| 263 | /* FIXME: Get rid of conn_bts and conn_net! */ |
| 264 | mgcp_send(endp, 1, &addr, (char *)msg->data, msg->len, |
| 265 | conn_bts, conn_net); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 266 | msgb_free(msg); |
| 267 | } |
| 268 | |
| 269 | static void scheduled_tx_bts_cb(struct msgb *msg, void *data) |
| 270 | { |
| 271 | struct mgcp_endpoint *endp = data; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 272 | struct mgcp_conn_rtp *conn_net = NULL; |
| 273 | struct mgcp_conn_rtp *conn_bts = NULL; |
| 274 | |
| 275 | /* FIXME: Get rid of CONN_ID_XXX! */ |
| 276 | conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS); |
| 277 | conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET); |
| 278 | if (!conn_bts || !conn_net) |
| 279 | return; |
| 280 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 281 | struct sockaddr_in addr = { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 282 | .sin_addr = conn_bts->end.addr, |
| 283 | .sin_port = conn_bts->end.rtp_port, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 284 | }; |
| 285 | |
Harald Welte | a0ac30f | 2017-12-25 09:52:30 +0100 | [diff] [blame] | 286 | conn_net->end.stats.octets_tx += msg->len; |
| 287 | conn_net->end.stats.packets_tx++; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 288 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 289 | /* Send RTP data to BTS */ |
| 290 | /* FIXME: Get rid of conn_bts and conn_net! */ |
| 291 | mgcp_send(endp, 1, &addr, (char *)msg->data, msg->len, |
| 292 | conn_net, conn_bts); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 293 | msgb_free(msg); |
| 294 | } |
| 295 | |
| 296 | static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr) |
| 297 | { |
| 298 | struct msgb *msg; |
| 299 | socklen_t slen = sizeof(*addr); |
| 300 | int ret; |
| 301 | |
| 302 | msg = msgb_alloc(4096, "OSMUX"); |
| 303 | if (!msg) { |
| 304 | LOGP(DLMGCP, LOGL_ERROR, "cannot allocate message\n"); |
| 305 | return NULL; |
| 306 | } |
| 307 | ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0, |
| 308 | (struct sockaddr *)addr, &slen); |
| 309 | if (ret <= 0) { |
| 310 | msgb_free(msg); |
| 311 | LOGP(DLMGCP, LOGL_ERROR, "cannot receive message\n"); |
| 312 | return NULL; |
| 313 | } |
| 314 | msgb_put(msg, ret); |
| 315 | |
| 316 | return msg; |
| 317 | } |
| 318 | |
| 319 | #define osmux_chunk_length(msg, rem) (rem - msg->len); |
| 320 | |
| 321 | int osmux_read_from_bsc_nat_cb(struct osmo_fd *ofd, unsigned int what) |
| 322 | { |
| 323 | struct msgb *msg; |
| 324 | struct osmux_hdr *osmuxh; |
| 325 | struct llist_head list; |
| 326 | struct sockaddr_in addr; |
| 327 | struct mgcp_config *cfg = ofd->data; |
| 328 | uint32_t rem; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 329 | struct mgcp_conn_rtp *conn_net = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 330 | |
| 331 | msg = osmux_recv(ofd, &addr); |
| 332 | if (!msg) |
| 333 | return -1; |
| 334 | |
| 335 | /* not any further processing dummy messages */ |
| 336 | if (msg->data[0] == MGCP_DUMMY_LOAD) |
| 337 | goto out; |
| 338 | |
| 339 | rem = msg->len; |
| 340 | while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) { |
| 341 | struct mgcp_endpoint *endp; |
| 342 | |
| 343 | /* Yes, we use MGCP_DEST_NET to locate the origin */ |
| 344 | endp = endpoint_lookup(cfg, osmuxh->circuit_id, |
| 345 | &addr.sin_addr, MGCP_DEST_NET); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 346 | |
| 347 | /* FIXME: Get rid of CONN_ID_XXX! */ |
| 348 | conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET); |
| 349 | if (!conn_net) |
| 350 | goto out; |
| 351 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 352 | if (!endp) { |
| 353 | LOGP(DLMGCP, LOGL_ERROR, |
| 354 | "Cannot find an endpoint for circuit_id=%d\n", |
| 355 | osmuxh->circuit_id); |
| 356 | goto out; |
| 357 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 358 | conn_net->osmux.stats.octets += osmux_chunk_length(msg, rem); |
| 359 | conn_net->osmux.stats.chunks++; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 360 | rem = msg->len; |
| 361 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 362 | osmux_xfrm_output(osmuxh, &conn_net->osmux.out, &list); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 363 | osmux_tx_sched(&list, scheduled_tx_bts_cb, endp); |
| 364 | } |
| 365 | out: |
| 366 | msgb_free(msg); |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | /* This is called from the bsc-nat */ |
| 371 | static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr, |
| 372 | struct msgb *msg) |
| 373 | { |
| 374 | struct mgcp_endpoint *endp; |
| 375 | uint8_t osmux_cid; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 376 | struct mgcp_conn_rtp *conn_net = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 377 | |
| 378 | if (msg->len < 1 + sizeof(osmux_cid)) { |
| 379 | LOGP(DLMGCP, LOGL_ERROR, |
| 380 | "Discarding truncated Osmux dummy load\n"); |
| 381 | goto out; |
| 382 | } |
| 383 | |
| 384 | LOGP(DLMGCP, LOGL_DEBUG, "Received Osmux dummy load from %s\n", |
| 385 | inet_ntoa(addr->sin_addr)); |
| 386 | |
| 387 | if (!cfg->osmux) { |
| 388 | LOGP(DLMGCP, LOGL_ERROR, |
| 389 | "bsc wants to use Osmux but bsc-nat did not request it\n"); |
| 390 | goto out; |
| 391 | } |
| 392 | |
| 393 | /* extract the osmux CID from the dummy message */ |
| 394 | memcpy(&osmux_cid, &msg->data[1], sizeof(osmux_cid)); |
| 395 | |
| 396 | endp = endpoint_lookup(cfg, osmux_cid, &addr->sin_addr, MGCP_DEST_BTS); |
| 397 | if (!endp) { |
| 398 | LOGP(DLMGCP, LOGL_ERROR, |
| 399 | "Cannot find endpoint for Osmux CID %d\n", osmux_cid); |
| 400 | goto out; |
| 401 | } |
| 402 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 403 | conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET); |
| 404 | if (!conn_net) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 405 | goto out; |
| 406 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 407 | if (conn_net->osmux.state == OSMUX_STATE_ENABLED) |
| 408 | goto out; |
| 409 | |
| 410 | if (osmux_enable_conn(endp, conn_net, &addr->sin_addr, addr->sin_port) < 0 ) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 411 | LOGP(DLMGCP, LOGL_ERROR, |
| 412 | "Could not enable osmux in endpoint %d\n", |
| 413 | ENDPOINT_NUMBER(endp)); |
| 414 | goto out; |
| 415 | } |
| 416 | |
| 417 | LOGP(DLMGCP, LOGL_INFO, "Enabling osmux in endpoint %d for %s:%u\n", |
| 418 | ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr), |
| 419 | ntohs(addr->sin_port)); |
| 420 | out: |
| 421 | msgb_free(msg); |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what) |
| 426 | { |
| 427 | struct msgb *msg; |
| 428 | struct osmux_hdr *osmuxh; |
| 429 | struct llist_head list; |
| 430 | struct sockaddr_in addr; |
| 431 | struct mgcp_config *cfg = ofd->data; |
| 432 | uint32_t rem; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 433 | struct mgcp_conn_rtp *conn_net = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 434 | |
| 435 | msg = osmux_recv(ofd, &addr); |
| 436 | if (!msg) |
| 437 | return -1; |
| 438 | |
| 439 | /* not any further processing dummy messages */ |
| 440 | if (msg->data[0] == MGCP_DUMMY_LOAD) |
| 441 | return osmux_handle_dummy(cfg, &addr, msg); |
| 442 | |
| 443 | rem = msg->len; |
| 444 | while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) { |
| 445 | struct mgcp_endpoint *endp; |
| 446 | |
| 447 | /* Yes, we use MGCP_DEST_BTS to locate the origin */ |
| 448 | endp = endpoint_lookup(cfg, osmuxh->circuit_id, |
| 449 | &addr.sin_addr, MGCP_DEST_BTS); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 450 | |
| 451 | /* FIXME: Get rid of CONN_ID_XXX! */ |
| 452 | conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET); |
| 453 | if (!conn_net) |
| 454 | goto out; |
| 455 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 456 | if (!endp) { |
| 457 | LOGP(DLMGCP, LOGL_ERROR, |
| 458 | "Cannot find an endpoint for circuit_id=%d\n", |
| 459 | osmuxh->circuit_id); |
| 460 | goto out; |
| 461 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 462 | conn_net->osmux.stats.octets += osmux_chunk_length(msg, rem); |
| 463 | conn_net->osmux.stats.chunks++; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 464 | rem = msg->len; |
| 465 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 466 | osmux_xfrm_output(osmuxh, &conn_net->osmux.out, &list); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 467 | osmux_tx_sched(&list, scheduled_tx_net_cb, endp); |
| 468 | } |
| 469 | out: |
| 470 | msgb_free(msg); |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | int osmux_init(int role, struct mgcp_config *cfg) |
| 475 | { |
| 476 | int ret; |
| 477 | |
| 478 | switch(role) { |
| 479 | case OSMUX_ROLE_BSC: |
| 480 | osmux_fd.cb = osmux_read_from_bsc_nat_cb; |
| 481 | break; |
| 482 | case OSMUX_ROLE_BSC_NAT: |
| 483 | osmux_fd.cb = osmux_read_from_bsc_cb; |
| 484 | break; |
| 485 | default: |
| 486 | LOGP(DLMGCP, LOGL_ERROR, "wrong role for OSMUX\n"); |
| 487 | return -1; |
| 488 | } |
| 489 | osmux_fd.data = cfg; |
| 490 | |
| 491 | ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port); |
| 492 | if (ret < 0) { |
| 493 | LOGP(DLMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n"); |
| 494 | return ret; |
| 495 | } |
| 496 | mgcp_set_ip_tos(osmux_fd.fd, cfg->endp_dscp); |
| 497 | osmux_fd.when |= BSC_FD_READ; |
| 498 | |
| 499 | ret = osmo_fd_register(&osmux_fd); |
| 500 | if (ret < 0) { |
| 501 | LOGP(DLMGCP, LOGL_ERROR, "cannot register OSMUX socket\n"); |
| 502 | return ret; |
| 503 | } |
| 504 | cfg->osmux_init = 1; |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 509 | /*! enable OSXMUX circuit for a specified connection. |
| 510 | * \param[in] endp mgcp endpoint (configuration) |
| 511 | * \param[in] conn connection to disable |
| 512 | * \param[in] addr IP address of remote OSMUX endpoint |
| 513 | * \param[in] port portnumber of the remote OSMUX endpoint |
| 514 | * \returns 0 on success, -1 on ERROR */ |
| 515 | int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn, |
| 516 | struct in_addr *addr, uint16_t port) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 517 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 518 | /*! If osmux is enabled, initialize the output handler. This handler is |
| 519 | * used to reconstruct the RTP flow from osmux. The RTP SSRC is |
| 520 | * allocated based on the circuit ID (conn_net->osmux.cid), which is unique |
| 521 | * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP |
| 522 | * SSRC space (2^32) by the 256 possible circuit IDs, then randomly |
| 523 | * select one value from that window. Thus, we have no chance to have |
| 524 | * overlapping RTP SSRC traveling to the BTSes behind the BSC, |
| 525 | * similarly, for flows traveling to the MSC. |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 526 | */ |
| 527 | static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 528 | uint16_t osmux_dummy = endp->cfg->osmux_dummy; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 529 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 530 | /* Check if osmux is enabled for the specified connection */ |
| 531 | if (conn->osmux.state == OSMUX_STATE_DISABLED) { |
| 532 | LOGP(DLMGCP, LOGL_ERROR, "OSMUX not enabled for conn:%s\n", |
| 533 | mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 534 | return -1; |
| 535 | } |
| 536 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 537 | osmux_xfrm_output_init(&conn->osmux.out, |
| 538 | (conn->osmux.cid * rtp_ssrc_winlen) + |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 539 | (random() % rtp_ssrc_winlen)); |
| 540 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 541 | conn->osmux.in = osmux_handle_lookup(endp->cfg, addr, port); |
| 542 | if (!conn->osmux.in) { |
| 543 | LOGP(DLMGCP, LOGL_ERROR, "Cannot allocate input osmux handle for conn:%s\n", |
| 544 | mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 545 | return -1; |
| 546 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 547 | if (!osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.cid, osmux_dummy)) { |
| 548 | LOGP(DLMGCP, LOGL_ERROR, "Cannot open osmux circuit %u for conn:%s\n", |
| 549 | conn->osmux.cid, mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 550 | return -1; |
| 551 | } |
| 552 | |
| 553 | switch (endp->cfg->role) { |
| 554 | case MGCP_BSC_NAT: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 555 | conn->type = MGCP_OSMUX_BSC_NAT; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 556 | break; |
| 557 | case MGCP_BSC: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 558 | conn->type = MGCP_OSMUX_BSC; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 559 | break; |
| 560 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 561 | |
| 562 | conn->osmux.state = OSMUX_STATE_ENABLED; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 567 | /*! disable OSXMUX circuit for a specified connection. |
| 568 | * \param[in] conn connection to disable */ |
| 569 | void osmux_disable_conn(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 570 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 571 | if (!conn) |
| 572 | return; |
| 573 | |
| 574 | if (conn->osmux.state != OSMUX_STATE_ENABLED) |
| 575 | return; |
| 576 | |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 577 | LOGP(DLMGCP, LOGL_INFO, "Releasing connection %s using Osmux CID %u\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 578 | conn->conn->id, conn->osmux.cid); |
| 579 | osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.cid); |
| 580 | conn->osmux.state = OSMUX_STATE_DISABLED; |
| 581 | conn->osmux.cid = -1; |
| 582 | osmux_handle_put(conn->osmux.in); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 583 | } |
| 584 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 585 | /*! relase OSXMUX cid, that had been allocated to this connection. |
| 586 | * \param[in] conn connection with OSMUX cid to release */ |
| 587 | void osmux_release_cid(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 588 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 589 | if (!conn) |
| 590 | return; |
| 591 | |
| 592 | if (conn->osmux.state != OSMUX_STATE_ENABLED) |
| 593 | return; |
| 594 | |
| 595 | if (conn->osmux.allocated_cid >= 0) |
| 596 | osmux_put_cid(conn->osmux.allocated_cid); |
| 597 | conn->osmux.allocated_cid = -1; |
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 | /*! allocate OSXMUX cid to connection. |
| 601 | * \param[in] conn connection for which we allocate the OSMUX cid*/ |
| 602 | void osmux_allocate_cid(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 603 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 604 | osmux_release_cid(conn); |
| 605 | conn->osmux.allocated_cid = osmux_get_cid(); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 606 | } |
| 607 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 608 | /*! send RTP dummy packet to OSMUX connection port. |
| 609 | * \param[in] endp mcgp endpoint that holds the RTP connection |
| 610 | * \param[in] conn associated RTP connection |
| 611 | * \returns bytes sent, -1 on error */ |
| 612 | 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] | 613 | { |
| 614 | char buf[1 + sizeof(uint8_t)]; |
| 615 | struct in_addr addr_unset = {}; |
| 616 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 617 | /*! The dummy packet will not be sent via the actual OSMUX connection, |
| 618 | * instead it is sent out of band to port where the remote OSMUX |
| 619 | * multplexer is listening. The goal is to ensure that the connection |
| 620 | * is kept open */ |
| 621 | |
| 622 | /*! We don't need to send the dummy load for osmux so often as another |
| 623 | * endpoint may have already punched the hole in the firewall. This |
| 624 | * approach is simple though. */ |
| 625 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 626 | buf[0] = MGCP_DUMMY_LOAD; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 627 | memcpy(&buf[1], &conn->osmux.cid, sizeof(conn->osmux.cid)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 628 | |
| 629 | /* Wait until we have the connection information from MDCX */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 630 | if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 631 | return 0; |
| 632 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 633 | if (conn->osmux.state == OSMUX_STATE_ACTIVATING) { |
| 634 | if (osmux_enable_conn(endp, conn, &conn->end.addr, |
| 635 | htons(endp->cfg->osmux_port)) < 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 636 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 637 | "Could not activate osmux for conn:%s\n", |
| 638 | mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 639 | } |
| 640 | LOGP(DLMGCP, LOGL_ERROR, |
| 641 | "Osmux CID %u for %s:%u is now enabled\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 642 | conn->osmux.cid, inet_ntoa(conn->end.addr), |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 643 | endp->cfg->osmux_port); |
| 644 | } |
| 645 | LOGP(DLMGCP, LOGL_DEBUG, |
| 646 | "sending OSMUX dummy load to %s CID %u\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 647 | inet_ntoa(conn->end.addr), conn->osmux.cid); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 648 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 649 | return mgcp_udp_send(osmux_fd.fd, &conn->end.addr, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 650 | htons(endp->cfg->osmux_port), buf, sizeof(buf)); |
| 651 | } |
| 652 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 653 | /*! bsc-nat allocates/releases the OSMUX cids (Circuit IDs). */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 654 | static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1) / 8]; |
| 655 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 656 | /*! count the number of taken OSMUX cids. |
| 657 | * \returns number of OSMUX cids in use */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 658 | int osmux_used_cid(void) |
| 659 | { |
| 660 | int i, j, used = 0; |
| 661 | |
| 662 | for (i = 0; i < sizeof(osmux_cid_bitmap); i++) { |
| 663 | for (j = 0; j < 8; j++) { |
| 664 | if (osmux_cid_bitmap[i] & (1 << j)) |
| 665 | used += 1; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | return used; |
| 670 | } |
| 671 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 672 | /*! take a free OSMUX cid. |
| 673 | * \returns OSMUX cid */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 674 | int osmux_get_cid(void) |
| 675 | { |
| 676 | int i, j; |
| 677 | |
| 678 | for (i = 0; i < sizeof(osmux_cid_bitmap); i++) { |
| 679 | for (j = 0; j < 8; j++) { |
| 680 | if (osmux_cid_bitmap[i] & (1 << j)) |
| 681 | continue; |
| 682 | |
| 683 | osmux_cid_bitmap[i] |= (1 << j); |
| 684 | LOGP(DLMGCP, LOGL_DEBUG, |
| 685 | "Allocating Osmux CID %u from pool\n", (i * 8) + j); |
| 686 | return (i * 8) + j; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | LOGP(DLMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n"); |
| 691 | return -1; |
| 692 | } |
| 693 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 694 | /*! put back a no longer used OSMUX cid. |
| 695 | * \param[in] osmux_cid OSMUX cid */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 696 | void osmux_put_cid(uint8_t osmux_cid) |
| 697 | { |
| 698 | LOGP(DLMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid); |
| 699 | osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8)); |
| 700 | } |