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