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