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 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 25 | #include <osmocom/mgcp/mgcp.h> |
| 26 | #include <osmocom/mgcp/mgcp_internal.h> |
| 27 | #include <osmocom/mgcp/osmux.h> |
| 28 | #include <osmocom/mgcp/mgcp_conn.h> |
Philipp Maier | 37d11c8 | 2018-02-01 14:38:12 +0100 | [diff] [blame] | 29 | #include <osmocom/mgcp/mgcp_endp.h> |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 30 | #include <osmocom/mgcp/mgcp_trunk.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 31 | |
| 32 | static struct osmo_fd osmux_fd; |
| 33 | |
| 34 | static LLIST_HEAD(osmux_handle_list); |
| 35 | |
| 36 | struct osmux_handle { |
| 37 | struct llist_head head; |
| 38 | struct osmux_in_handle *in; |
| 39 | struct in_addr rem_addr; |
Pau Espin Pedrol | 1205686 | 2019-05-13 17:30:21 +0200 | [diff] [blame] | 40 | int rem_port; /* network byte order */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 41 | int refcnt; |
| 42 | }; |
| 43 | |
| 44 | static void *osmux; |
| 45 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 46 | /* Deliver OSMUX batch to the remote end */ |
| 47 | static void osmux_deliver_cb(struct msgb *batch_msg, void *data) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 48 | { |
| 49 | struct osmux_handle *handle = data; |
| 50 | struct sockaddr_in out = { |
| 51 | .sin_family = AF_INET, |
| 52 | .sin_port = handle->rem_port, |
| 53 | }; |
| 54 | |
| 55 | memcpy(&out.sin_addr, &handle->rem_addr, sizeof(handle->rem_addr)); |
| 56 | sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0, |
| 57 | (struct sockaddr *)&out, sizeof(out)); |
| 58 | msgb_free(batch_msg); |
| 59 | } |
| 60 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 61 | /* Lookup existing OSMUX handle for specified destination address. */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 62 | static struct osmux_handle * |
| 63 | osmux_handle_find_get(struct in_addr *addr, int rem_port) |
| 64 | { |
| 65 | struct osmux_handle *h; |
| 66 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 67 | llist_for_each_entry(h, &osmux_handle_list, head) { |
| 68 | if (memcmp(&h->rem_addr, addr, sizeof(struct in_addr)) == 0 && |
| 69 | h->rem_port == rem_port) { |
| 70 | LOGP(DLMGCP, LOGL_DEBUG, "using existing OSMUX handle " |
| 71 | "for addr=%s:%d\n", |
| 72 | inet_ntoa(*addr), ntohs(rem_port)); |
| 73 | h->refcnt++; |
| 74 | return h; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return NULL; |
| 79 | } |
| 80 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 81 | /* Put down no longer needed OSMUX handle */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 82 | static void osmux_handle_put(struct osmux_in_handle *in) |
| 83 | { |
| 84 | struct osmux_handle *h; |
| 85 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 86 | llist_for_each_entry(h, &osmux_handle_list, head) { |
| 87 | if (h->in == in) { |
| 88 | if (--h->refcnt == 0) { |
| 89 | LOGP(DLMGCP, LOGL_INFO, |
| 90 | "Releasing unused osmux handle for %s:%d\n", |
| 91 | inet_ntoa(h->rem_addr), |
| 92 | ntohs(h->rem_port)); |
| 93 | LOGP(DLMGCP, LOGL_INFO, "Stats: " |
| 94 | "input RTP msgs: %u bytes: %"PRIu64" " |
| 95 | "output osmux msgs: %u bytes: %"PRIu64"\n", |
| 96 | in->stats.input_rtp_msgs, |
| 97 | in->stats.input_rtp_bytes, |
| 98 | in->stats.output_osmux_msgs, |
| 99 | in->stats.output_osmux_bytes); |
| 100 | llist_del(&h->head); |
| 101 | osmux_xfrm_input_fini(h->in); |
| 102 | talloc_free(h); |
| 103 | } |
| 104 | return; |
| 105 | } |
| 106 | } |
| 107 | LOGP(DLMGCP, LOGL_ERROR, "cannot find Osmux input handle %p\n", in); |
| 108 | } |
| 109 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 110 | /* Allocate free OSMUX handle */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 111 | static struct osmux_handle * |
| 112 | osmux_handle_alloc(struct mgcp_config *cfg, struct in_addr *addr, int rem_port) |
| 113 | { |
| 114 | struct osmux_handle *h; |
| 115 | |
| 116 | h = talloc_zero(osmux, struct osmux_handle); |
| 117 | if (!h) |
| 118 | return NULL; |
| 119 | h->rem_addr = *addr; |
| 120 | h->rem_port = rem_port; |
| 121 | h->refcnt++; |
| 122 | |
| 123 | h->in = talloc_zero(h, struct osmux_in_handle); |
| 124 | if (!h->in) { |
| 125 | talloc_free(h); |
| 126 | return NULL; |
| 127 | } |
| 128 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 129 | /* sequence number to start OSMUX message from */ |
| 130 | h->in->osmux_seq = 0; |
| 131 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 132 | h->in->batch_factor = cfg->osmux_batch; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 133 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 134 | /* If batch size is zero, the library defaults to 1470 bytes. */ |
| 135 | h->in->batch_size = cfg->osmux_batch_size; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 136 | h->in->deliver = osmux_deliver_cb; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 137 | osmux_xfrm_input_init(h->in); |
| 138 | h->in->data = h; |
| 139 | |
| 140 | llist_add(&h->head, &osmux_handle_list); |
| 141 | |
| 142 | LOGP(DLMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s:%d\n", |
| 143 | inet_ntoa(*addr), ntohs(rem_port)); |
| 144 | |
| 145 | return h; |
| 146 | } |
| 147 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 148 | /* 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] | 149 | * found, the function will automatically allocate one */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 150 | static struct osmux_in_handle * |
| 151 | osmux_handle_lookup(struct mgcp_config *cfg, struct in_addr *addr, int rem_port) |
| 152 | { |
| 153 | struct osmux_handle *h; |
| 154 | |
| 155 | h = osmux_handle_find_get(addr, rem_port); |
| 156 | if (h != NULL) |
| 157 | return h->in; |
| 158 | |
| 159 | h = osmux_handle_alloc(cfg, addr, rem_port); |
| 160 | if (h == NULL) |
| 161 | return NULL; |
| 162 | |
| 163 | return h->in; |
| 164 | } |
| 165 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 166 | /*! send RTP packet through OSMUX connection. |
| 167 | * \param[in] buf rtp data |
| 168 | * \param[in] buf_len length of rtp data |
| 169 | * \param[in] conn associated RTP connection |
| 170 | * \returns 0 on success, -1 on ERROR */ |
| 171 | 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] | 172 | { |
| 173 | int ret; |
| 174 | struct msgb *msg; |
| 175 | |
| 176 | msg = msgb_alloc(4096, "RTP"); |
| 177 | if (!msg) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 178 | return -1; |
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 | memcpy(msg->data, buf, buf_len); |
| 181 | msgb_put(msg, buf_len); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 182 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 183 | if (conn->osmux.state != OSMUX_STATE_ENABLED) { |
| 184 | LOGPCONN(conn->conn, DLMGCP, LOGL_INFO, "forwarding RTP to Osmux conn not yet enabled, dropping (cid=%d)\n", |
| 185 | conn->osmux.cid); |
| 186 | return -1; |
| 187 | } |
| 188 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 189 | 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] | 190 | /* batch full, build and deliver it */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 191 | osmux_xfrm_input_deliver(conn->osmux.in); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 192 | } |
| 193 | return 0; |
| 194 | } |
| 195 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 196 | /* Lookup the endpoint that corresponds to the specified address (port) */ |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 197 | static struct mgcp_conn_rtp* |
| 198 | osmux_conn_lookup(struct mgcp_config *cfg, uint8_t cid, |
| 199 | struct in_addr *from_addr) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 200 | { |
Philipp Maier | d19de2e | 2020-06-03 13:55:33 +0200 | [diff] [blame] | 201 | struct mgcp_trunk *trunk = mgcp_trunk_by_num(cfg, MGCP_VIRT_TRUNK_ID); |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 202 | struct mgcp_endpoint *endp; |
| 203 | struct mgcp_conn *conn = NULL; |
| 204 | struct mgcp_conn_rtp * conn_rtp; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 205 | int i; |
| 206 | |
Philipp Maier | d19de2e | 2020-06-03 13:55:33 +0200 | [diff] [blame] | 207 | for (i = 0; i < trunk->number_endpoints; i++) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 208 | |
Philipp Maier | d19de2e | 2020-06-03 13:55:33 +0200 | [diff] [blame] | 209 | endp = trunk->endpoints[i]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 210 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 211 | llist_for_each_entry(conn, &endp->conns, entry) { |
| 212 | if (conn->type != MGCP_CONN_TYPE_RTP) |
| 213 | continue; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 214 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 215 | conn_rtp = &conn->u.rtp; |
| 216 | if (!mgcp_conn_rtp_is_osmux(conn_rtp)) |
| 217 | continue; |
| 218 | |
| 219 | if (conn_rtp->osmux.cid == cid) |
| 220 | return conn_rtp; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 221 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 222 | } |
| 223 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 224 | LOGP(DLMGCP, LOGL_ERROR, "Cannot find osmux conn with cid=%d\n", cid); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 225 | |
| 226 | return NULL; |
| 227 | } |
| 228 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 229 | /* FIXME: this is declared and used in mgcp_network.c, but documentation of mgcp_dispatch_rtp_bridge_cb() states another enum is to be used */ |
| 230 | enum { |
| 231 | MGCP_PROTO_RTP, |
| 232 | MGCP_PROTO_RTCP, |
| 233 | }; |
| 234 | |
| 235 | 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] | 236 | { |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 237 | struct mgcp_conn_rtp *conn = data; |
| 238 | struct mgcp_endpoint *endp = conn->conn->endp; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 239 | struct sockaddr_in addr = { |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 240 | .sin_addr = conn->end.addr, |
| 241 | .sin_port = conn->end.rtp_port, |
| 242 | }; /* FIXME: not set/used in cb */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 243 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 244 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 245 | endp->type->dispatch_rtp_cb(MGCP_PROTO_RTP, &addr, (char *)msg->data, msg->len, conn->conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 246 | msgb_free(msg); |
| 247 | } |
| 248 | |
| 249 | static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr) |
| 250 | { |
| 251 | struct msgb *msg; |
| 252 | socklen_t slen = sizeof(*addr); |
| 253 | int ret; |
| 254 | |
| 255 | msg = msgb_alloc(4096, "OSMUX"); |
| 256 | if (!msg) { |
| 257 | LOGP(DLMGCP, LOGL_ERROR, "cannot allocate message\n"); |
| 258 | return NULL; |
| 259 | } |
| 260 | ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0, |
| 261 | (struct sockaddr *)addr, &slen); |
| 262 | if (ret <= 0) { |
| 263 | msgb_free(msg); |
| 264 | LOGP(DLMGCP, LOGL_ERROR, "cannot receive message\n"); |
| 265 | return NULL; |
| 266 | } |
| 267 | msgb_put(msg, ret); |
| 268 | |
| 269 | return msg; |
| 270 | } |
| 271 | |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 272 | /* Updates endp osmux state and returns 0 if it can process messages, -1 otherwise */ |
| 273 | static int endp_osmux_state_check(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn, |
| 274 | bool sending) |
| 275 | { |
| 276 | switch(conn->osmux.state) { |
| 277 | case OSMUX_STATE_ACTIVATING: |
Pau Espin Pedrol | 295570c | 2019-05-13 17:32:47 +0200 | [diff] [blame] | 278 | if (osmux_enable_conn(endp, conn, &conn->end.addr, conn->end.rtp_port) < 0) { |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 279 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
Pau Espin Pedrol | 3752522 | 2019-05-13 17:34:14 +0200 | [diff] [blame] | 280 | "Could not enable osmux for conn on %s: %s\n", |
| 281 | sending ? "sent" : "received", |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 282 | mgcp_conn_dump(conn->conn)); |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 283 | return -1; |
| 284 | } |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 285 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
Pau Espin Pedrol | 3752522 | 2019-05-13 17:34:14 +0200 | [diff] [blame] | 286 | "Osmux %s CID %u towards %s:%u is now enabled\n", |
| 287 | sending ? "sent" : "received", |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 288 | conn->osmux.cid, inet_ntoa(conn->end.addr), |
Pau Espin Pedrol | 295570c | 2019-05-13 17:32:47 +0200 | [diff] [blame] | 289 | ntohs(conn->end.rtp_port)); |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 290 | return 0; |
| 291 | case OSMUX_STATE_ENABLED: |
| 292 | return 0; |
| 293 | default: |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 294 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 295 | "Osmux %s in conn %s without full negotiation, state %d\n", |
| 296 | sending ? "sent" : "received", |
| 297 | mgcp_conn_dump(conn->conn), conn->osmux.state); |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 298 | return -1; |
| 299 | } |
| 300 | } |
| 301 | |
Pau Espin Pedrol | 662fa42 | 2018-10-16 15:54:38 +0200 | [diff] [blame] | 302 | static int osmux_legacy_dummy_parse_cid(struct sockaddr_in *addr, struct msgb *msg, |
| 303 | uint8_t *osmux_cid) |
| 304 | { |
Pau Espin Pedrol | b5583cd | 2019-05-13 12:58:24 +0200 | [diff] [blame] | 305 | if (msg->len < 1 + sizeof(*osmux_cid)) { |
Pau Espin Pedrol | 662fa42 | 2018-10-16 15:54:38 +0200 | [diff] [blame] | 306 | LOGP(DLMGCP, LOGL_ERROR, |
Pau Espin Pedrol | b5583cd | 2019-05-13 12:58:24 +0200 | [diff] [blame] | 307 | "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] | 308 | return -1; |
| 309 | } |
| 310 | |
| 311 | /* extract the osmux CID from the dummy message */ |
| 312 | memcpy(osmux_cid, &msg->data[1], sizeof(*osmux_cid)); |
| 313 | return 0; |
| 314 | } |
| 315 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 316 | /* This is called from the bsc-nat */ |
| 317 | static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr, |
| 318 | struct msgb *msg) |
| 319 | { |
| 320 | uint8_t osmux_cid; |
| 321 | struct mgcp_conn_rtp *conn; |
| 322 | |
| 323 | if (osmux_legacy_dummy_parse_cid(addr, msg, &osmux_cid) < 0) |
| 324 | goto out; |
| 325 | |
| 326 | conn = osmux_conn_lookup(cfg, osmux_cid, &addr->sin_addr); |
| 327 | if (!conn) { |
| 328 | LOGP(DLMGCP, LOGL_ERROR, |
| 329 | "Cannot find conn for Osmux CID %d\n", osmux_cid); |
| 330 | goto out; |
| 331 | } |
| 332 | |
| 333 | endp_osmux_state_check(conn->conn->endp, conn, false); |
| 334 | /* Only needed to punch hole in firewall, it can be dropped */ |
| 335 | out: |
| 336 | msgb_free(msg); |
| 337 | return 0; |
| 338 | } |
| 339 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 340 | #define osmux_chunk_length(msg, rem) (rem - msg->len); |
| 341 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 342 | 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] | 343 | { |
| 344 | struct msgb *msg; |
| 345 | struct osmux_hdr *osmuxh; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 346 | struct sockaddr_in addr; |
| 347 | struct mgcp_config *cfg = ofd->data; |
| 348 | uint32_t rem; |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 349 | struct mgcp_conn_rtp *conn_src; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 350 | |
| 351 | msg = osmux_recv(ofd, &addr); |
| 352 | if (!msg) |
| 353 | return -1; |
| 354 | |
Pau Espin Pedrol | d14163e | 2018-10-16 15:48:59 +0200 | [diff] [blame] | 355 | if (!cfg->osmux) { |
| 356 | LOGP(DLMGCP, LOGL_ERROR, |
| 357 | "bsc-nat wants to use Osmux but bsc did not request it\n"); |
| 358 | goto out; |
| 359 | } |
| 360 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 361 | /* not any further processing dummy messages */ |
| 362 | if (msg->data[0] == MGCP_DUMMY_LOAD) |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 363 | return osmux_handle_dummy(cfg, &addr, msg); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 364 | |
| 365 | rem = msg->len; |
| 366 | while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 367 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 368 | conn_src = osmux_conn_lookup(cfg, osmuxh->circuit_id, |
| 369 | &addr.sin_addr); |
| 370 | if (!conn_src) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 371 | LOGP(DLMGCP, LOGL_ERROR, |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 372 | "Cannot find a src conn for circuit_id=%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 373 | osmuxh->circuit_id); |
| 374 | goto out; |
| 375 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 376 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 377 | /*conn_dst = mgcp_find_dst_conn(conn_src->conn); |
| 378 | if (!conn_dst) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 379 | LOGP(DLMGCP, LOGL_ERROR, |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 380 | "Cannot find a dst conn for circuit_id=%d\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 381 | osmuxh->circuit_id); |
| 382 | goto out; |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 383 | }*/ |
| 384 | |
| 385 | if (endp_osmux_state_check(conn_src->conn->endp, conn_src, false) == 0) { |
| 386 | conn_src->osmux.stats.octets += osmux_chunk_length(msg, rem); |
| 387 | conn_src->osmux.stats.chunks++; |
| 388 | osmux_xfrm_output_sched(&conn_src->osmux.out, osmuxh); |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 389 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 390 | rem = msg->len; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 391 | } |
| 392 | out: |
| 393 | msgb_free(msg); |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | int osmux_init(int role, struct mgcp_config *cfg) |
| 398 | { |
| 399 | int ret; |
| 400 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 401 | osmux_fd.cb = osmux_read_fd_cb; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 402 | osmux_fd.data = cfg; |
| 403 | |
| 404 | ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port); |
| 405 | if (ret < 0) { |
Pau Espin Pedrol | f027f17 | 2019-05-06 13:57:31 +0200 | [diff] [blame] | 406 | LOGP(DLMGCP, LOGL_ERROR, "cannot bind OSMUX socket to %s:%u\n", |
| 407 | cfg->osmux_addr, cfg->osmux_port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 408 | return ret; |
| 409 | } |
| 410 | mgcp_set_ip_tos(osmux_fd.fd, cfg->endp_dscp); |
Pau Espin Pedrol | a7152e0 | 2020-05-09 19:15:50 +0200 | [diff] [blame] | 411 | osmux_fd.when |= OSMO_FD_READ; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 412 | |
| 413 | ret = osmo_fd_register(&osmux_fd); |
| 414 | if (ret < 0) { |
Pau Espin Pedrol | f027f17 | 2019-05-06 13:57:31 +0200 | [diff] [blame] | 415 | LOGP(DLMGCP, LOGL_ERROR, "cannot register OSMUX socket %s\n", |
| 416 | osmo_sock_get_name2(osmux_fd.fd)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 417 | return ret; |
| 418 | } |
| 419 | cfg->osmux_init = 1; |
| 420 | |
Pau Espin Pedrol | f027f17 | 2019-05-06 13:57:31 +0200 | [diff] [blame] | 421 | LOGP(DLMGCP, LOGL_INFO, "OSMUX socket listening on %s\n", |
| 422 | osmo_sock_get_name2(osmux_fd.fd)); |
| 423 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 424 | return 0; |
| 425 | } |
| 426 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 427 | /*! enable OSXMUX circuit for a specified connection. |
| 428 | * \param[in] endp mgcp endpoint (configuration) |
| 429 | * \param[in] conn connection to disable |
| 430 | * \param[in] addr IP address of remote OSMUX endpoint |
Pau Espin Pedrol | 1205686 | 2019-05-13 17:30:21 +0200 | [diff] [blame] | 431 | * \param[in] port portnumber of the remote OSMUX endpoint (in network byte order) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 432 | * \returns 0 on success, -1 on ERROR */ |
| 433 | int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn, |
| 434 | struct in_addr *addr, uint16_t port) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 435 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 436 | /*! If osmux is enabled, initialize the output handler. This handler is |
| 437 | * used to reconstruct the RTP flow from osmux. The RTP SSRC is |
| 438 | * allocated based on the circuit ID (conn_net->osmux.cid), which is unique |
| 439 | * 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] | 440 | * 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] | 441 | * select one value from that window. Thus, we have no chance to have |
| 442 | * overlapping RTP SSRC traveling to the BTSes behind the BSC, |
| 443 | * similarly, for flows traveling to the MSC. |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 444 | */ |
Pau Espin Pedrol | dac2ca7 | 2019-05-13 17:34:51 +0200 | [diff] [blame] | 445 | struct in_addr addr_unset = {}; |
Pau Espin Pedrol | 17bf603 | 2018-09-17 13:38:39 +0200 | [diff] [blame] | 446 | static const uint32_t rtp_ssrc_winlen = UINT32_MAX / (OSMUX_CID_MAX + 1); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 447 | uint16_t osmux_dummy = endp->cfg->osmux_dummy; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 448 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 449 | /* Check if osmux is enabled for the specified connection */ |
Pau Espin Pedrol | 48aff62 | 2018-10-16 16:03:57 +0200 | [diff] [blame] | 450 | if (conn->osmux.state != OSMUX_STATE_ACTIVATING) { |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 451 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 452 | "conn:%s didn't negotiate Osmux, state %d\n", |
| 453 | mgcp_conn_dump(conn->conn), conn->osmux.state); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 454 | return -1; |
| 455 | } |
| 456 | |
Pau Espin Pedrol | dac2ca7 | 2019-05-13 17:34:51 +0200 | [diff] [blame] | 457 | /* Wait until we have the connection information from MDCX */ |
| 458 | if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0) { |
| 459 | LOGPCONN(conn->conn, DLMGCP, LOGL_INFO, |
| 460 | "Osmux remote address/port still unknown\n"); |
| 461 | return -1; |
| 462 | } |
| 463 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 464 | conn->osmux.in = osmux_handle_lookup(endp->cfg, addr, port); |
| 465 | if (!conn->osmux.in) { |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 466 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 467 | "Cannot allocate input osmux handle for conn:%s\n", |
| 468 | mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 469 | return -1; |
| 470 | } |
Pau Espin Pedrol | ac772d8 | 2019-05-06 18:02:00 +0200 | [diff] [blame] | 471 | if (osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.cid, osmux_dummy) < 0) { |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 472 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 473 | "Cannot open osmux circuit %u for conn:%s\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 474 | conn->osmux.cid, mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 475 | return -1; |
| 476 | } |
| 477 | |
Pau Espin Pedrol | df7d97e | 2019-05-17 17:14:06 +0200 | [diff] [blame] | 478 | osmux_xfrm_output_init2(&conn->osmux.out, |
Pau Espin Pedrol | 426a9d9 | 2018-10-16 15:31:45 +0200 | [diff] [blame] | 479 | (conn->osmux.cid * rtp_ssrc_winlen) + |
Pau Espin Pedrol | df7d97e | 2019-05-17 17:14:06 +0200 | [diff] [blame] | 480 | (random() % rtp_ssrc_winlen), |
| 481 | conn->end.codec->payload_type); |
Pau Espin Pedrol | 426a9d9 | 2018-10-16 15:31:45 +0200 | [diff] [blame] | 482 | |
Pau Espin Pedrol | 1442c5a | 2019-05-06 16:10:10 +0200 | [diff] [blame] | 483 | osmux_xfrm_output_set_tx_cb(&conn->osmux.out, |
| 484 | scheduled_from_osmux_tx_rtp_cb, conn); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 485 | |
| 486 | conn->osmux.state = OSMUX_STATE_ENABLED; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 491 | /*! disable OSXMUX circuit for a specified connection. |
| 492 | * \param[in] conn connection to disable */ |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 493 | void conn_osmux_disable(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 494 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 495 | |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 496 | OSMO_ASSERT(conn->osmux.state != OSMUX_STATE_DISABLED); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 497 | |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 498 | LOGPCONN(conn->conn, DLMGCP, LOGL_INFO, |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 499 | "Releasing connection using Osmux CID %u\n", conn->osmux.cid); |
Pau Espin Pedrol | f2321b7 | 2018-05-17 13:55:14 +0200 | [diff] [blame] | 500 | |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 501 | if (conn->osmux.state == OSMUX_STATE_ENABLED) { |
| 502 | /* We are closing, we don't need pending RTP packets to be transmitted */ |
| 503 | osmux_xfrm_output_set_tx_cb(&conn->osmux.out, NULL, NULL); |
| 504 | osmux_xfrm_output_flush(&conn->osmux.out); |
Pau Espin Pedrol | f2321b7 | 2018-05-17 13:55:14 +0200 | [diff] [blame] | 505 | |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 506 | osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.cid); |
| 507 | conn->osmux.state = OSMUX_STATE_DISABLED; |
| 508 | conn_osmux_release_cid(conn); |
| 509 | osmux_handle_put(conn->osmux.in); |
| 510 | } |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 511 | conn_osmux_release_cid(conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 512 | } |
| 513 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 514 | /*! relase OSXMUX cid, that had been allocated to this connection. |
| 515 | * \param[in] conn connection with OSMUX cid to release */ |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 516 | void conn_osmux_release_cid(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 517 | { |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 518 | if (conn->osmux.cid_allocated) |
| 519 | osmux_cid_pool_put(conn->osmux.cid); |
| 520 | conn->osmux.cid = 0; |
| 521 | conn->osmux.cid_allocated = false; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 522 | } |
| 523 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 524 | /*! allocate OSXMUX cid to connection. |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 525 | * \param[in] conn connection for which we allocate the OSMUX cid |
| 526 | * \param[in] osmux_cid OSMUX cid to allocate. -1 Means take next available one. |
| 527 | * \returns Allocated OSMUX cid, -1 on error (no free cids avail, or selected one is already taken). |
| 528 | */ |
| 529 | int conn_osmux_allocate_cid(struct mgcp_conn_rtp *conn, int osmux_cid) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 530 | { |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 531 | if (osmux_cid != -1 && osmux_cid_pool_allocated((uint8_t) osmux_cid)) { |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 532 | LOGPCONN(conn->conn, DLMGCP, LOGL_INFO, |
| 533 | "Osmux CID %d already allocated!\n", |
| 534 | osmux_cid); |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 535 | return -1; |
| 536 | } |
| 537 | |
| 538 | if (osmux_cid == -1) { |
| 539 | osmux_cid = osmux_cid_pool_get_next(); |
| 540 | if (osmux_cid == -1) { |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 541 | LOGPCONN(conn->conn, DLMGCP, LOGL_INFO, |
| 542 | "no available Osmux CID to allocate!\n"); |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 543 | return -1; |
| 544 | } |
| 545 | } else |
| 546 | osmux_cid_pool_get(osmux_cid); |
| 547 | |
| 548 | conn->osmux.cid = (uint8_t) osmux_cid; |
| 549 | conn->osmux.cid_allocated = true; |
Pau Espin Pedrol | fa810e8 | 2019-05-06 18:54:10 +0200 | [diff] [blame] | 550 | conn->type = MGCP_OSMUX_BSC; |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 551 | return osmux_cid; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 552 | } |
| 553 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 554 | /*! send RTP dummy packet to OSMUX connection port. |
| 555 | * \param[in] endp mcgp endpoint that holds the RTP connection |
| 556 | * \param[in] conn associated RTP connection |
| 557 | * \returns bytes sent, -1 on error */ |
| 558 | 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] | 559 | { |
Pau Espin Pedrol | c1ad553 | 2019-05-15 23:00:53 +0200 | [diff] [blame] | 560 | struct osmux_hdr *osmuxh; |
| 561 | int buf_len; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 562 | struct in_addr addr_unset = {}; |
| 563 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 564 | /*! The dummy packet will not be sent via the actual OSMUX connection, |
| 565 | * instead it is sent out of band to port where the remote OSMUX |
| 566 | * multplexer is listening. The goal is to ensure that the connection |
| 567 | * is kept open */ |
| 568 | |
| 569 | /*! We don't need to send the dummy load for osmux so often as another |
| 570 | * endpoint may have already punched the hole in the firewall. This |
| 571 | * approach is simple though. */ |
| 572 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 573 | /* Wait until we have the connection information from MDCX */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 574 | if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 575 | return 0; |
| 576 | |
Pau Espin Pedrol | de2a4d7 | 2018-10-16 16:37:43 +0200 | [diff] [blame] | 577 | if (endp_osmux_state_check(endp, conn, true) < 0) |
| 578 | return 0; |
| 579 | |
Pau Espin Pedrol | c1ad553 | 2019-05-15 23:00:53 +0200 | [diff] [blame] | 580 | buf_len = sizeof(struct osmux_hdr) + osmo_amr_bytes(AMR_FT_0); |
| 581 | osmuxh = (struct osmux_hdr *) alloca(buf_len); |
| 582 | memset(osmuxh, 0, buf_len); |
| 583 | osmuxh->ft = OSMUX_FT_DUMMY; |
| 584 | osmuxh->amr_ft = AMR_FT_0; |
| 585 | osmuxh->circuit_id = conn->osmux.cid; |
| 586 | |
Pau Espin Pedrol | c9a6280 | 2019-05-13 13:20:13 +0200 | [diff] [blame] | 587 | LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG, |
Pau Espin Pedrol | 3752522 | 2019-05-13 17:34:14 +0200 | [diff] [blame] | 588 | "sending OSMUX dummy load to %s:%u CID %u\n", |
| 589 | inet_ntoa(conn->end.addr), ntohs(conn->end.rtp_port), conn->osmux.cid); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 590 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 591 | return mgcp_udp_send(osmux_fd.fd, &conn->end.addr, |
Pau Espin Pedrol | c1ad553 | 2019-05-15 23:00:53 +0200 | [diff] [blame] | 592 | conn->end.rtp_port, (char*)osmuxh, buf_len); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 593 | } |
| 594 | |
Pau Espin Pedrol | bcd52e5 | 2018-09-17 12:41:28 +0200 | [diff] [blame] | 595 | /* bsc-nat allocates/releases the Osmux circuit ID. +7 to round up to 8 bit boundary. */ |
| 596 | static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1 + 7) / 8]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 597 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 598 | /*! count the number of taken OSMUX cids. |
| 599 | * \returns number of OSMUX cids in use */ |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 600 | int osmux_cid_pool_count_used(void) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 601 | { |
| 602 | int i, j, used = 0; |
| 603 | |
| 604 | for (i = 0; i < sizeof(osmux_cid_bitmap); i++) { |
| 605 | for (j = 0; j < 8; j++) { |
| 606 | if (osmux_cid_bitmap[i] & (1 << j)) |
| 607 | used += 1; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | return used; |
| 612 | } |
| 613 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 614 | /*! take a free OSMUX cid. |
| 615 | * \returns OSMUX cid */ |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 616 | int osmux_cid_pool_get_next(void) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 617 | { |
| 618 | int i, j; |
| 619 | |
| 620 | for (i = 0; i < sizeof(osmux_cid_bitmap); i++) { |
| 621 | for (j = 0; j < 8; j++) { |
| 622 | if (osmux_cid_bitmap[i] & (1 << j)) |
| 623 | continue; |
| 624 | |
| 625 | osmux_cid_bitmap[i] |= (1 << j); |
| 626 | LOGP(DLMGCP, LOGL_DEBUG, |
| 627 | "Allocating Osmux CID %u from pool\n", (i * 8) + j); |
| 628 | return (i * 8) + j; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | LOGP(DLMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n"); |
| 633 | return -1; |
| 634 | } |
| 635 | |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 636 | /*! take a specific OSMUX cid. |
| 637 | * \param[in] osmux_cid OSMUX cid */ |
| 638 | void osmux_cid_pool_get(uint8_t osmux_cid) |
| 639 | { |
| 640 | LOGP(DLMGCP, LOGL_DEBUG, "Allocating Osmux CID %u from pool\n", osmux_cid); |
| 641 | osmux_cid_bitmap[osmux_cid / 8] |= (1 << (osmux_cid % 8)); |
| 642 | } |
| 643 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 644 | /*! put back a no longer used OSMUX cid. |
| 645 | * \param[in] osmux_cid OSMUX cid */ |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 646 | void osmux_cid_pool_put(uint8_t osmux_cid) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 647 | { |
| 648 | LOGP(DLMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid); |
| 649 | osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8)); |
| 650 | } |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 651 | |
| 652 | /*! check if OSMUX cid is already taken */ |
| 653 | bool osmux_cid_pool_allocated(uint8_t osmux_cid) |
| 654 | { |
| 655 | return !!(osmux_cid_bitmap[osmux_cid / 8] & (1 << (osmux_cid % 8))); |
| 656 | } |