blob: 980dbf0c830e2da9b5698fc6ea7d0fcfea2b0a39 [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001/*
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 Pedrolf027f172019-05-06 13:57:31 +020018#include <osmocom/core/socket.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020019#include <osmocom/core/talloc.h>
20
21#include <osmocom/netif/osmux.h>
22#include <osmocom/netif/rtp.h>
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +020023#include <osmocom/netif/amr.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020024
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +020025#include <osmocom/mgcp/debug.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020026#include <osmocom/mgcp/mgcp.h>
Philipp Maier993ea6b2020-08-04 18:26:50 +020027#include <osmocom/mgcp/mgcp_protocol.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020028#include <osmocom/mgcp/osmux.h>
29#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010030#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maierc66ab2c2020-06-02 20:55:34 +020031#include <osmocom/mgcp/mgcp_trunk.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020032
33static struct osmo_fd osmux_fd;
34
35static LLIST_HEAD(osmux_handle_list);
36
37struct osmux_handle {
38 struct llist_head head;
Pau Espin Pedrol24914012020-09-08 12:57:35 +020039 struct mgcp_conn_rtp *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020040 struct osmux_in_handle *in;
41 struct in_addr rem_addr;
Pau Espin Pedrol12056862019-05-13 17:30:21 +020042 int rem_port; /* network byte order */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020043 int refcnt;
44};
45
46static void *osmux;
47
Philipp Maier87bd9be2017-08-22 16:35:41 +020048/* Deliver OSMUX batch to the remote end */
49static void osmux_deliver_cb(struct msgb *batch_msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020050{
51 struct osmux_handle *handle = data;
Pau Espin Pedrol24914012020-09-08 12:57:35 +020052 struct mgcp_conn_rtp *conn = handle->conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020053
Pau Espin Pedrol24914012020-09-08 12:57:35 +020054 if (conn->end.output_enabled) {
55 struct sockaddr_in out = {
56 .sin_family = AF_INET,
57 .sin_port = handle->rem_port,
58 };
59 memcpy(&out.sin_addr, &handle->rem_addr, sizeof(handle->rem_addr));
60 sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0,
61 (struct sockaddr *)&out, sizeof(out));
62 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020063 msgb_free(batch_msg);
64}
65
Philipp Maier87bd9be2017-08-22 16:35:41 +020066/* Lookup existing OSMUX handle for specified destination address. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020067static struct osmux_handle *
68osmux_handle_find_get(struct in_addr *addr, int rem_port)
69{
70 struct osmux_handle *h;
71
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020072 llist_for_each_entry(h, &osmux_handle_list, head) {
73 if (memcmp(&h->rem_addr, addr, sizeof(struct in_addr)) == 0 &&
74 h->rem_port == rem_port) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +020075 LOGP(DOSMUX, LOGL_DEBUG, "using existing OSMUX handle "
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020076 "for addr=%s:%d\n",
77 inet_ntoa(*addr), ntohs(rem_port));
78 h->refcnt++;
79 return h;
80 }
81 }
82
83 return NULL;
84}
85
Philipp Maier87bd9be2017-08-22 16:35:41 +020086/* Put down no longer needed OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020087static void osmux_handle_put(struct osmux_in_handle *in)
88{
89 struct osmux_handle *h;
90
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020091 llist_for_each_entry(h, &osmux_handle_list, head) {
92 if (h->in == in) {
93 if (--h->refcnt == 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +020094 LOGP(DOSMUX, LOGL_INFO,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020095 "Releasing unused osmux handle for %s:%d\n",
96 inet_ntoa(h->rem_addr),
97 ntohs(h->rem_port));
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +020098 LOGP(DOSMUX, LOGL_INFO, "Stats: "
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020099 "input RTP msgs: %u bytes: %"PRIu64" "
100 "output osmux msgs: %u bytes: %"PRIu64"\n",
101 in->stats.input_rtp_msgs,
102 in->stats.input_rtp_bytes,
103 in->stats.output_osmux_msgs,
104 in->stats.output_osmux_bytes);
105 llist_del(&h->head);
106 osmux_xfrm_input_fini(h->in);
107 talloc_free(h);
108 }
109 return;
110 }
111 }
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200112 LOGP(DOSMUX, LOGL_ERROR, "cannot find Osmux input handle %p\n", in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200113}
114
Philipp Maier87bd9be2017-08-22 16:35:41 +0200115/* Allocate free OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200116static struct osmux_handle *
Pau Espin Pedrol24914012020-09-08 12:57:35 +0200117osmux_handle_alloc(struct mgcp_conn_rtp *conn, struct in_addr *addr, int rem_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200118{
119 struct osmux_handle *h;
Ericfbf78d12021-08-23 22:31:39 +0200120 struct mgcp_config *cfg = conn->conn->endp->trunk->cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200121
122 h = talloc_zero(osmux, struct osmux_handle);
123 if (!h)
124 return NULL;
Pau Espin Pedrol24914012020-09-08 12:57:35 +0200125 h->conn = conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200126 h->rem_addr = *addr;
127 h->rem_port = rem_port;
128 h->refcnt++;
129
130 h->in = talloc_zero(h, struct osmux_in_handle);
131 if (!h->in) {
132 talloc_free(h);
133 return NULL;
134 }
135
Philipp Maier87bd9be2017-08-22 16:35:41 +0200136 /* sequence number to start OSMUX message from */
137 h->in->osmux_seq = 0;
138
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200139 h->in->batch_factor = cfg->osmux_batch;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200140
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200141 /* If batch size is zero, the library defaults to 1470 bytes. */
142 h->in->batch_size = cfg->osmux_batch_size;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200143 h->in->deliver = osmux_deliver_cb;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200144 osmux_xfrm_input_init(h->in);
145 h->in->data = h;
146
147 llist_add(&h->head, &osmux_handle_list);
148
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200149 LOGP(DOSMUX, LOGL_DEBUG, "created new OSMUX handle for addr=%s:%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200150 inet_ntoa(*addr), ntohs(rem_port));
151
152 return h;
153}
154
Philipp Maier87bd9be2017-08-22 16:35:41 +0200155/* Lookup existing handle for a specified address, if the handle can not be
Harald Welte1d1b98f2017-12-25 10:03:40 +0100156 * found, the function will automatically allocate one */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200157static struct osmux_in_handle *
Pau Espin Pedrol24914012020-09-08 12:57:35 +0200158osmux_handle_lookup(struct mgcp_conn_rtp *conn, struct osmo_sockaddr *addr, int rem_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200159{
160 struct osmux_handle *h;
161
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200162 if (addr->u.sa.sa_family != AF_INET) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200163 LOGP(DOSMUX, LOGL_DEBUG, "IPv6 not supported in osmux yet!\n");
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200164 return NULL;
165 }
166
167 h = osmux_handle_find_get(&addr->u.sin.sin_addr, rem_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200168 if (h != NULL)
169 return h->in;
170
Pau Espin Pedrol24914012020-09-08 12:57:35 +0200171 h = osmux_handle_alloc(conn, &addr->u.sin.sin_addr, rem_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200172 if (h == NULL)
173 return NULL;
174
175 return h->in;
176}
177
Philipp Maier87bd9be2017-08-22 16:35:41 +0200178/*! send RTP packet through OSMUX connection.
179 * \param[in] buf rtp data
180 * \param[in] buf_len length of rtp data
181 * \param[in] conn associated RTP connection
182 * \returns 0 on success, -1 on ERROR */
183int osmux_xfrm_to_osmux(char *buf, int buf_len, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200184{
185 int ret;
186 struct msgb *msg;
187
188 msg = msgb_alloc(4096, "RTP");
189 if (!msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200190 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200191
Philipp Maier87bd9be2017-08-22 16:35:41 +0200192 memcpy(msg->data, buf, buf_len);
193 msgb_put(msg, buf_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200194
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200195 if (conn->osmux.state != OSMUX_STATE_ENABLED) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200196 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO, "forwarding RTP to Osmux conn not yet enabled, dropping (cid=%d)\n",
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200197 conn->osmux.cid);
198 return -1;
199 }
200
Philipp Maier87bd9be2017-08-22 16:35:41 +0200201 while ((ret = osmux_xfrm_input(conn->osmux.in, msg, conn->osmux.cid)) > 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200202 /* batch full, build and deliver it */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200203 osmux_xfrm_input_deliver(conn->osmux.in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200204 }
205 return 0;
206}
207
Philipp Maier87bd9be2017-08-22 16:35:41 +0200208/* Lookup the endpoint that corresponds to the specified address (port) */
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200209static struct mgcp_conn_rtp*
210osmux_conn_lookup(struct mgcp_config *cfg, uint8_t cid,
211 struct in_addr *from_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200212{
Philipp Maier6fbbeec2020-07-01 23:00:54 +0200213 struct mgcp_trunk *trunk = mgcp_trunk_by_num(cfg, MGCP_TRUNK_VIRTUAL, MGCP_VIRT_TRUNK_ID);
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200214 struct mgcp_endpoint *endp;
215 struct mgcp_conn *conn = NULL;
216 struct mgcp_conn_rtp * conn_rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200217 int i;
218
Philipp Maierd19de2e2020-06-03 13:55:33 +0200219 for (i = 0; i < trunk->number_endpoints; i++) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200220
Philipp Maierd19de2e2020-06-03 13:55:33 +0200221 endp = trunk->endpoints[i];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200222
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200223 llist_for_each_entry(conn, &endp->conns, entry) {
224 if (conn->type != MGCP_CONN_TYPE_RTP)
225 continue;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200226
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200227 conn_rtp = &conn->u.rtp;
228 if (!mgcp_conn_rtp_is_osmux(conn_rtp))
229 continue;
230
231 if (conn_rtp->osmux.cid == cid)
232 return conn_rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200233 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200234 }
235
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200236 LOGP(DOSMUX, LOGL_ERROR, "Cannot find osmux conn with cid=%d\n", cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200237
238 return NULL;
239}
240
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200241/* 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 */
242enum {
243 MGCP_PROTO_RTP,
244 MGCP_PROTO_RTCP,
245};
246
247static void scheduled_from_osmux_tx_rtp_cb(struct msgb *msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200248{
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200249 struct mgcp_conn_rtp *conn = data;
250 struct mgcp_endpoint *endp = conn->conn->endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200251 struct osmo_sockaddr addr = { /* FIXME: do we know the source address?? */ };
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200252 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
253 *mc = (struct osmo_rtp_msg_ctx){
254 .proto = MGCP_PROTO_RTP,
255 .conn_src = conn,
256 .from_addr = &addr,
257 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200258
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200259 endp->type->dispatch_rtp_cb(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200260 msgb_free(msg);
261}
262
263static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr)
264{
265 struct msgb *msg;
266 socklen_t slen = sizeof(*addr);
267 int ret;
268
269 msg = msgb_alloc(4096, "OSMUX");
270 if (!msg) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200271 LOGP(DOSMUX, LOGL_ERROR, "cannot allocate message\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200272 return NULL;
273 }
274 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0,
275 (struct sockaddr *)addr, &slen);
276 if (ret <= 0) {
277 msgb_free(msg);
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200278 LOGP(DOSMUX, LOGL_ERROR, "cannot receive message\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200279 return NULL;
280 }
281 msgb_put(msg, ret);
282
283 return msg;
284}
285
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200286/* Updates endp osmux state and returns 0 if it can process messages, -1 otherwise */
287static int endp_osmux_state_check(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
288 bool sending)
289{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200290 char ipbuf[INET6_ADDRSTRLEN];
291
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200292 switch(conn->osmux.state) {
293 case OSMUX_STATE_ACTIVATING:
Pau Espin Pedrola4f783b2022-09-07 20:04:20 +0200294 if (osmux_enable_conn(endp, conn, &conn->end.addr, conn->end.rtp_port) < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200295 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200296 "Could not enable osmux for conn on %s: %s\n",
297 sending ? "sent" : "received",
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200298 mgcp_conn_dump(conn->conn));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200299 return -1;
300 }
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200301 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200302 "Osmux %s CID %u towards %s:%u is now enabled\n",
303 sending ? "sent" : "received",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200304 conn->osmux.cid,
305 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf),
Pau Espin Pedrol295570c2019-05-13 17:32:47 +0200306 ntohs(conn->end.rtp_port));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200307 return 0;
308 case OSMUX_STATE_ENABLED:
309 return 0;
310 default:
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200311 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200312 "Osmux %s in conn %s without full negotiation, state %d\n",
313 sending ? "sent" : "received",
314 mgcp_conn_dump(conn->conn), conn->osmux.state);
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200315 return -1;
316 }
317}
318
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200319static int osmux_legacy_dummy_parse_cid(struct sockaddr_in *addr, struct msgb *msg,
320 uint8_t *osmux_cid)
321{
Pau Espin Pedrolb5583cd2019-05-13 12:58:24 +0200322 if (msg->len < 1 + sizeof(*osmux_cid)) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200323 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrolb5583cd2019-05-13 12:58:24 +0200324 "Discarding truncated Osmux dummy load: %s\n", osmo_hexdump(msg->data, msg->len));
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200325 return -1;
326 }
327
328 /* extract the osmux CID from the dummy message */
329 memcpy(osmux_cid, &msg->data[1], sizeof(*osmux_cid));
330 return 0;
331}
332
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200333/* This is called from the bsc-nat */
334static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr,
335 struct msgb *msg)
336{
337 uint8_t osmux_cid;
338 struct mgcp_conn_rtp *conn;
339
340 if (osmux_legacy_dummy_parse_cid(addr, msg, &osmux_cid) < 0)
341 goto out;
342
343 conn = osmux_conn_lookup(cfg, osmux_cid, &addr->sin_addr);
344 if (!conn) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200345 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200346 "Cannot find conn for Osmux CID %d\n", osmux_cid);
347 goto out;
348 }
349
350 endp_osmux_state_check(conn->conn->endp, conn, false);
351 /* Only needed to punch hole in firewall, it can be dropped */
352out:
353 msgb_free(msg);
354 return 0;
355}
356
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200357#define osmux_chunk_length(msg, rem) (rem - msg->len);
358
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200359static int osmux_read_fd_cb(struct osmo_fd *ofd, unsigned int what)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200360{
361 struct msgb *msg;
362 struct osmux_hdr *osmuxh;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200363 struct sockaddr_in addr;
364 struct mgcp_config *cfg = ofd->data;
365 uint32_t rem;
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200366 struct mgcp_conn_rtp *conn_src;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200367
368 msg = osmux_recv(ofd, &addr);
369 if (!msg)
370 return -1;
371
Pau Espin Pedrold14163e2018-10-16 15:48:59 +0200372 if (!cfg->osmux) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200373 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrold14163e2018-10-16 15:48:59 +0200374 "bsc-nat wants to use Osmux but bsc did not request it\n");
375 goto out;
376 }
377
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200378 /* not any further processing dummy messages */
Philipp Maierb3d14eb2021-05-20 14:18:52 +0200379 if (mgcp_is_rtp_dummy_payload(msg))
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200380 return osmux_handle_dummy(cfg, &addr, msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200381
382 rem = msg->len;
383 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200384
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200385 conn_src = osmux_conn_lookup(cfg, osmuxh->circuit_id,
386 &addr.sin_addr);
387 if (!conn_src) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200388 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200389 "Cannot find a src conn for circuit_id=%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200390 osmuxh->circuit_id);
391 goto out;
392 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200393
Pau Espin Pedrolde133112020-09-08 17:51:36 +0200394 mgcp_conn_watchdog_kick(conn_src->conn);
395
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200396 /*conn_dst = mgcp_find_dst_conn(conn_src->conn);
397 if (!conn_dst) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200398 LOGP(DOSMUX, LOGL_ERROR,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200399 "Cannot find a dst conn for circuit_id=%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200400 osmuxh->circuit_id);
401 goto out;
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200402 }*/
403
404 if (endp_osmux_state_check(conn_src->conn->endp, conn_src, false) == 0) {
405 conn_src->osmux.stats.octets += osmux_chunk_length(msg, rem);
406 conn_src->osmux.stats.chunks++;
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200407 osmux_xfrm_output_sched(conn_src->osmux.out, osmuxh);
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200408 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200409 rem = msg->len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200410 }
411out:
412 msgb_free(msg);
413 return 0;
414}
415
416int osmux_init(int role, struct mgcp_config *cfg)
417{
418 int ret;
419
Harald Weltec2a8f592020-10-19 13:25:41 +0200420 osmo_fd_setup(&osmux_fd, -1, OSMO_FD_READ, osmux_read_fd_cb, cfg, 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200421
Harald Welte55a92292021-04-28 19:06:34 +0200422 ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port,
423 cfg->endp_dscp, cfg->endp_priority);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200424 if (ret < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200425 LOGP(DOSMUX, LOGL_ERROR, "cannot bind OSMUX socket to %s:%u\n",
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200426 cfg->osmux_addr, cfg->osmux_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200427 return ret;
428 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200429
430 ret = osmo_fd_register(&osmux_fd);
431 if (ret < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200432 LOGP(DOSMUX, LOGL_ERROR, "cannot register OSMUX socket %s\n",
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200433 osmo_sock_get_name2(osmux_fd.fd));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200434 return ret;
435 }
436 cfg->osmux_init = 1;
437
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200438 LOGP(DOSMUX, LOGL_INFO, "OSMUX socket listening on %s\n",
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200439 osmo_sock_get_name2(osmux_fd.fd));
440
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200441 return 0;
442}
443
Philipp Maier87bd9be2017-08-22 16:35:41 +0200444/*! enable OSXMUX circuit for a specified connection.
445 * \param[in] endp mgcp endpoint (configuration)
446 * \param[in] conn connection to disable
447 * \param[in] addr IP address of remote OSMUX endpoint
Pau Espin Pedrol12056862019-05-13 17:30:21 +0200448 * \param[in] port portnumber of the remote OSMUX endpoint (in network byte order)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200449 * \returns 0 on success, -1 on ERROR */
450int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200451 struct osmo_sockaddr *addr, uint16_t port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200452{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200453 /*! If osmux is enabled, initialize the output handler. This handler is
454 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
455 * allocated based on the circuit ID (conn_net->osmux.cid), which is unique
456 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200457 * SSRC space (2^32) by the OSMUX_CID_MAX + 1 possible circuit IDs, then randomly
Philipp Maier87bd9be2017-08-22 16:35:41 +0200458 * select one value from that window. Thus, we have no chance to have
459 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
460 * similarly, for flows traveling to the MSC.
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200461 */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200462 struct in6_addr addr_unset = {};
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200463 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / (OSMUX_CID_MAX + 1);
Ericfbf78d12021-08-23 22:31:39 +0200464 uint16_t osmux_dummy = endp->trunk->cfg->osmux_dummy;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200465
Philipp Maier87bd9be2017-08-22 16:35:41 +0200466 /* Check if osmux is enabled for the specified connection */
Pau Espin Pedrol48aff622018-10-16 16:03:57 +0200467 if (conn->osmux.state != OSMUX_STATE_ACTIVATING) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200468 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200469 "conn:%s didn't negotiate Osmux, state %d\n",
470 mgcp_conn_dump(conn->conn), conn->osmux.state);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200471 return -1;
472 }
473
Pau Espin Pedroldac2ca72019-05-13 17:34:51 +0200474 /* Wait until we have the connection information from MDCX */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200475 if (memcmp(&conn->end.addr, &addr_unset,
476 conn->end.addr.u.sa.sa_family == AF_INET6 ?
477 sizeof(struct in6_addr) :
478 sizeof(struct in_addr)) == 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200479 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedroldac2ca72019-05-13 17:34:51 +0200480 "Osmux remote address/port still unknown\n");
481 return -1;
482 }
483
Pau Espin Pedrol24914012020-09-08 12:57:35 +0200484 conn->osmux.in = osmux_handle_lookup(conn, addr, port);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200485 if (!conn->osmux.in) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200486 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200487 "Cannot allocate input osmux handle for conn:%s\n",
488 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200489 return -1;
490 }
Pau Espin Pedrolac772d82019-05-06 18:02:00 +0200491 if (osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.cid, osmux_dummy) < 0) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200492 LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200493 "Cannot open osmux circuit %u for conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200494 conn->osmux.cid, mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200495 return -1;
496 }
497
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200498 conn->osmux.out = osmux_xfrm_output_alloc(osmux);
499 osmux_xfrm_output_set_rtp_ssrc(conn->osmux.out,
500 (conn->osmux.cid * rtp_ssrc_winlen) +
501 (random() % rtp_ssrc_winlen));
502 osmux_xfrm_output_set_rtp_pl_type(conn->osmux.out, conn->end.codec->payload_type);
503 osmux_xfrm_output_set_tx_cb(conn->osmux.out,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200504 scheduled_from_osmux_tx_rtp_cb, conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200505
506 conn->osmux.state = OSMUX_STATE_ENABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200507
508 return 0;
509}
510
Philipp Maier87bd9be2017-08-22 16:35:41 +0200511/*! disable OSXMUX circuit for a specified connection.
512 * \param[in] conn connection to disable */
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200513void conn_osmux_disable(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200514{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200515
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200516 OSMO_ASSERT(conn->osmux.state != OSMUX_STATE_DISABLED);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200517
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200518 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200519 "Releasing connection using Osmux CID %u\n", conn->osmux.cid);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200520
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200521 if (conn->osmux.state == OSMUX_STATE_ENABLED) {
522 /* We are closing, we don't need pending RTP packets to be transmitted */
Pau Espin Pedrol33639162022-08-31 17:46:11 +0200523 osmux_xfrm_output_set_tx_cb(conn->osmux.out, NULL, NULL);
524 TALLOC_FREE(conn->osmux.out);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200525
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200526 osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.cid);
527 conn->osmux.state = OSMUX_STATE_DISABLED;
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200528 osmux_handle_put(conn->osmux.in);
529 }
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200530 conn_osmux_release_cid(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200531}
532
Philipp Maier87bd9be2017-08-22 16:35:41 +0200533/*! relase OSXMUX cid, that had been allocated to this connection.
534 * \param[in] conn connection with OSMUX cid to release */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200535void conn_osmux_release_cid(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200536{
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200537 if (conn->osmux.cid_allocated)
538 osmux_cid_pool_put(conn->osmux.cid);
539 conn->osmux.cid = 0;
540 conn->osmux.cid_allocated = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200541}
542
Philipp Maier87bd9be2017-08-22 16:35:41 +0200543/*! allocate OSXMUX cid to connection.
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200544 * \param[in] conn connection for which we allocate the OSMUX cid
545 * \param[in] osmux_cid OSMUX cid to allocate. -1 Means take next available one.
546 * \returns Allocated OSMUX cid, -1 on error (no free cids avail, or selected one is already taken).
547 */
548int conn_osmux_allocate_cid(struct mgcp_conn_rtp *conn, int osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200549{
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200550 if (osmux_cid != -1 && osmux_cid_pool_allocated((uint8_t) osmux_cid)) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200551 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200552 "Osmux CID %d already allocated!\n",
553 osmux_cid);
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200554 return -1;
555 }
556
557 if (osmux_cid == -1) {
558 osmux_cid = osmux_cid_pool_get_next();
559 if (osmux_cid == -1) {
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200560 LOGPCONN(conn->conn, DOSMUX, LOGL_INFO,
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200561 "no available Osmux CID to allocate!\n");
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200562 return -1;
563 }
564 } else
565 osmux_cid_pool_get(osmux_cid);
566
567 conn->osmux.cid = (uint8_t) osmux_cid;
568 conn->osmux.cid_allocated = true;
Pau Espin Pedrolfa810e82019-05-06 18:54:10 +0200569 conn->type = MGCP_OSMUX_BSC;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200570 return osmux_cid;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200571}
572
Philipp Maier87bd9be2017-08-22 16:35:41 +0200573/*! send RTP dummy packet to OSMUX connection port.
574 * \param[in] endp mcgp endpoint that holds the RTP connection
575 * \param[in] conn associated RTP connection
576 * \returns bytes sent, -1 on error */
577int osmux_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200578{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200579 char ipbuf[INET6_ADDRSTRLEN];
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200580 struct osmux_hdr *osmuxh;
581 int buf_len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200582 struct in_addr addr_unset = {};
583
Philipp Maier87bd9be2017-08-22 16:35:41 +0200584 /*! The dummy packet will not be sent via the actual OSMUX connection,
585 * instead it is sent out of band to port where the remote OSMUX
586 * multplexer is listening. The goal is to ensure that the connection
587 * is kept open */
588
589 /*! We don't need to send the dummy load for osmux so often as another
590 * endpoint may have already punched the hole in the firewall. This
591 * approach is simple though. */
592
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200593 /* Wait until we have the connection information from MDCX */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200594 if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200595 return 0;
596
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200597 if (endp_osmux_state_check(endp, conn, true) < 0)
598 return 0;
599
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200600 buf_len = sizeof(struct osmux_hdr) + osmo_amr_bytes(AMR_FT_0);
601 osmuxh = (struct osmux_hdr *) alloca(buf_len);
602 memset(osmuxh, 0, buf_len);
603 osmuxh->ft = OSMUX_FT_DUMMY;
604 osmuxh->amr_ft = AMR_FT_0;
605 osmuxh->circuit_id = conn->osmux.cid;
606
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200607 LOGPCONN(conn->conn, DOSMUX, LOGL_DEBUG,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200608 "sending OSMUX dummy load to %s:%u CID %u\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200609 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf),
610 ntohs(conn->end.rtp_port), conn->osmux.cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200611
Philipp Maier87bd9be2017-08-22 16:35:41 +0200612 return mgcp_udp_send(osmux_fd.fd, &conn->end.addr,
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200613 conn->end.rtp_port, (char*)osmuxh, buf_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200614}
615
Pau Espin Pedrolbcd52e52018-09-17 12:41:28 +0200616/* bsc-nat allocates/releases the Osmux circuit ID. +7 to round up to 8 bit boundary. */
617static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1 + 7) / 8];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200618
Philipp Maier87bd9be2017-08-22 16:35:41 +0200619/*! count the number of taken OSMUX cids.
620 * \returns number of OSMUX cids in use */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200621int osmux_cid_pool_count_used(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200622{
623 int i, j, used = 0;
624
625 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
626 for (j = 0; j < 8; j++) {
627 if (osmux_cid_bitmap[i] & (1 << j))
628 used += 1;
629 }
630 }
631
632 return used;
633}
634
Philipp Maier87bd9be2017-08-22 16:35:41 +0200635/*! take a free OSMUX cid.
636 * \returns OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200637int osmux_cid_pool_get_next(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200638{
639 int i, j;
640
641 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
642 for (j = 0; j < 8; j++) {
643 if (osmux_cid_bitmap[i] & (1 << j))
644 continue;
645
646 osmux_cid_bitmap[i] |= (1 << j);
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200647 LOGP(DOSMUX, LOGL_DEBUG,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200648 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
649 return (i * 8) + j;
650 }
651 }
652
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200653 LOGP(DOSMUX, LOGL_ERROR, "All Osmux circuits are in use!\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200654 return -1;
655}
656
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200657/*! take a specific OSMUX cid.
658 * \param[in] osmux_cid OSMUX cid */
659void osmux_cid_pool_get(uint8_t osmux_cid)
660{
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200661 LOGP(DOSMUX, LOGL_DEBUG, "Allocating Osmux CID %u from pool\n", osmux_cid);
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200662 osmux_cid_bitmap[osmux_cid / 8] |= (1 << (osmux_cid % 8));
663}
664
Philipp Maier87bd9be2017-08-22 16:35:41 +0200665/*! put back a no longer used OSMUX cid.
666 * \param[in] osmux_cid OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200667void osmux_cid_pool_put(uint8_t osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200668{
Pau Espin Pedrolec5e85b2022-09-09 16:04:19 +0200669 LOGP(DOSMUX, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200670 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
671}
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200672
673/*! check if OSMUX cid is already taken */
674bool osmux_cid_pool_allocated(uint8_t osmux_cid)
675{
676 return !!(osmux_cid_bitmap[osmux_cid / 8] & (1 << (osmux_cid % 8)));
677}