blob: 5864c95f698480ceeb59414c42b992e49f9037a6 [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
Philipp Maier87bd9be2017-08-22 16:35:41 +020025#include <osmocom/mgcp/mgcp.h>
Philipp Maier993ea6b2020-08-04 18:26:50 +020026#include <osmocom/mgcp/mgcp_protocol.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020027#include <osmocom/mgcp/osmux.h>
28#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010029#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maierc66ab2c2020-06-02 20:55:34 +020030#include <osmocom/mgcp/mgcp_trunk.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020031
32static struct osmo_fd osmux_fd;
33
34static LLIST_HEAD(osmux_handle_list);
35
36struct osmux_handle {
37 struct llist_head head;
38 struct osmux_in_handle *in;
39 struct in_addr rem_addr;
Pau Espin Pedrol12056862019-05-13 17:30:21 +020040 int rem_port; /* network byte order */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020041 int refcnt;
42};
43
44static void *osmux;
45
Philipp Maier87bd9be2017-08-22 16:35:41 +020046/* Deliver OSMUX batch to the remote end */
47static void osmux_deliver_cb(struct msgb *batch_msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020048{
49 struct osmux_handle *handle = data;
50 struct sockaddr_in out = {
51 .sin_family = AF_INET,
52 .sin_port = handle->rem_port,
53 };
54
55 memcpy(&out.sin_addr, &handle->rem_addr, sizeof(handle->rem_addr));
56 sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0,
57 (struct sockaddr *)&out, sizeof(out));
58 msgb_free(batch_msg);
59}
60
Philipp Maier87bd9be2017-08-22 16:35:41 +020061/* Lookup existing OSMUX handle for specified destination address. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020062static struct osmux_handle *
63osmux_handle_find_get(struct in_addr *addr, int rem_port)
64{
65 struct osmux_handle *h;
66
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020067 llist_for_each_entry(h, &osmux_handle_list, head) {
68 if (memcmp(&h->rem_addr, addr, sizeof(struct in_addr)) == 0 &&
69 h->rem_port == rem_port) {
70 LOGP(DLMGCP, LOGL_DEBUG, "using existing OSMUX handle "
71 "for addr=%s:%d\n",
72 inet_ntoa(*addr), ntohs(rem_port));
73 h->refcnt++;
74 return h;
75 }
76 }
77
78 return NULL;
79}
80
Philipp Maier87bd9be2017-08-22 16:35:41 +020081/* Put down no longer needed OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020082static void osmux_handle_put(struct osmux_in_handle *in)
83{
84 struct osmux_handle *h;
85
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020086 llist_for_each_entry(h, &osmux_handle_list, head) {
87 if (h->in == in) {
88 if (--h->refcnt == 0) {
89 LOGP(DLMGCP, LOGL_INFO,
90 "Releasing unused osmux handle for %s:%d\n",
91 inet_ntoa(h->rem_addr),
92 ntohs(h->rem_port));
93 LOGP(DLMGCP, LOGL_INFO, "Stats: "
94 "input RTP msgs: %u bytes: %"PRIu64" "
95 "output osmux msgs: %u bytes: %"PRIu64"\n",
96 in->stats.input_rtp_msgs,
97 in->stats.input_rtp_bytes,
98 in->stats.output_osmux_msgs,
99 in->stats.output_osmux_bytes);
100 llist_del(&h->head);
101 osmux_xfrm_input_fini(h->in);
102 talloc_free(h);
103 }
104 return;
105 }
106 }
107 LOGP(DLMGCP, LOGL_ERROR, "cannot find Osmux input handle %p\n", in);
108}
109
Philipp Maier87bd9be2017-08-22 16:35:41 +0200110/* Allocate free OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200111static struct osmux_handle *
112osmux_handle_alloc(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
113{
114 struct osmux_handle *h;
115
116 h = talloc_zero(osmux, struct osmux_handle);
117 if (!h)
118 return NULL;
119 h->rem_addr = *addr;
120 h->rem_port = rem_port;
121 h->refcnt++;
122
123 h->in = talloc_zero(h, struct osmux_in_handle);
124 if (!h->in) {
125 talloc_free(h);
126 return NULL;
127 }
128
Philipp Maier87bd9be2017-08-22 16:35:41 +0200129 /* sequence number to start OSMUX message from */
130 h->in->osmux_seq = 0;
131
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200132 h->in->batch_factor = cfg->osmux_batch;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200133
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200134 /* If batch size is zero, the library defaults to 1470 bytes. */
135 h->in->batch_size = cfg->osmux_batch_size;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200136 h->in->deliver = osmux_deliver_cb;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200137 osmux_xfrm_input_init(h->in);
138 h->in->data = h;
139
140 llist_add(&h->head, &osmux_handle_list);
141
142 LOGP(DLMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s:%d\n",
143 inet_ntoa(*addr), ntohs(rem_port));
144
145 return h;
146}
147
Philipp Maier87bd9be2017-08-22 16:35:41 +0200148/* Lookup existing handle for a specified address, if the handle can not be
Harald Welte1d1b98f2017-12-25 10:03:40 +0100149 * found, the function will automatically allocate one */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200150static struct osmux_in_handle *
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200151osmux_handle_lookup(struct mgcp_config *cfg, struct osmo_sockaddr *addr, int rem_port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200152{
153 struct osmux_handle *h;
154
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200155 if (addr->u.sa.sa_family != AF_INET) {
156 LOGP(DLMGCP, LOGL_DEBUG, "IPv6 not supported in osmux yet!\n");
157 return NULL;
158 }
159
160 h = osmux_handle_find_get(&addr->u.sin.sin_addr, rem_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200161 if (h != NULL)
162 return h->in;
163
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200164 h = osmux_handle_alloc(cfg, &addr->u.sin.sin_addr, rem_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200165 if (h == NULL)
166 return NULL;
167
168 return h->in;
169}
170
Philipp Maier87bd9be2017-08-22 16:35:41 +0200171/*! send RTP packet through OSMUX connection.
172 * \param[in] buf rtp data
173 * \param[in] buf_len length of rtp data
174 * \param[in] conn associated RTP connection
175 * \returns 0 on success, -1 on ERROR */
176int osmux_xfrm_to_osmux(char *buf, int buf_len, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200177{
178 int ret;
179 struct msgb *msg;
180
181 msg = msgb_alloc(4096, "RTP");
182 if (!msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200183 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200184
Philipp Maier87bd9be2017-08-22 16:35:41 +0200185 memcpy(msg->data, buf, buf_len);
186 msgb_put(msg, buf_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200187
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200188 if (conn->osmux.state != OSMUX_STATE_ENABLED) {
189 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO, "forwarding RTP to Osmux conn not yet enabled, dropping (cid=%d)\n",
190 conn->osmux.cid);
191 return -1;
192 }
193
Philipp Maier87bd9be2017-08-22 16:35:41 +0200194 while ((ret = osmux_xfrm_input(conn->osmux.in, msg, conn->osmux.cid)) > 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200195 /* batch full, build and deliver it */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200196 osmux_xfrm_input_deliver(conn->osmux.in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200197 }
198 return 0;
199}
200
Philipp Maier87bd9be2017-08-22 16:35:41 +0200201/* Lookup the endpoint that corresponds to the specified address (port) */
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200202static struct mgcp_conn_rtp*
203osmux_conn_lookup(struct mgcp_config *cfg, uint8_t cid,
204 struct in_addr *from_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200205{
Philipp Maier6fbbeec2020-07-01 23:00:54 +0200206 struct mgcp_trunk *trunk = mgcp_trunk_by_num(cfg, MGCP_TRUNK_VIRTUAL, MGCP_VIRT_TRUNK_ID);
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200207 struct mgcp_endpoint *endp;
208 struct mgcp_conn *conn = NULL;
209 struct mgcp_conn_rtp * conn_rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200210 int i;
211
Philipp Maierd19de2e2020-06-03 13:55:33 +0200212 for (i = 0; i < trunk->number_endpoints; i++) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200213
Philipp Maierd19de2e2020-06-03 13:55:33 +0200214 endp = trunk->endpoints[i];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200215
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200216 llist_for_each_entry(conn, &endp->conns, entry) {
217 if (conn->type != MGCP_CONN_TYPE_RTP)
218 continue;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200219
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200220 conn_rtp = &conn->u.rtp;
221 if (!mgcp_conn_rtp_is_osmux(conn_rtp))
222 continue;
223
224 if (conn_rtp->osmux.cid == cid)
225 return conn_rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200226 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200227 }
228
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200229 LOGP(DLMGCP, LOGL_ERROR, "Cannot find osmux conn with cid=%d\n", cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200230
231 return NULL;
232}
233
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200234/* 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 */
235enum {
236 MGCP_PROTO_RTP,
237 MGCP_PROTO_RTCP,
238};
239
240static void scheduled_from_osmux_tx_rtp_cb(struct msgb *msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200241{
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200242 struct mgcp_conn_rtp *conn = data;
243 struct mgcp_endpoint *endp = conn->conn->endp;
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200244 struct osmo_sockaddr addr = { /* FIXME: do we know the source address?? */ };
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200245 struct osmo_rtp_msg_ctx *mc = OSMO_RTP_MSG_CTX(msg);
246 *mc = (struct osmo_rtp_msg_ctx){
247 .proto = MGCP_PROTO_RTP,
248 .conn_src = conn,
249 .from_addr = &addr,
250 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200251
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200252 endp->type->dispatch_rtp_cb(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200253 msgb_free(msg);
254}
255
256static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr)
257{
258 struct msgb *msg;
259 socklen_t slen = sizeof(*addr);
260 int ret;
261
262 msg = msgb_alloc(4096, "OSMUX");
263 if (!msg) {
264 LOGP(DLMGCP, LOGL_ERROR, "cannot allocate message\n");
265 return NULL;
266 }
267 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0,
268 (struct sockaddr *)addr, &slen);
269 if (ret <= 0) {
270 msgb_free(msg);
271 LOGP(DLMGCP, LOGL_ERROR, "cannot receive message\n");
272 return NULL;
273 }
274 msgb_put(msg, ret);
275
276 return msg;
277}
278
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200279/* Updates endp osmux state and returns 0 if it can process messages, -1 otherwise */
280static int endp_osmux_state_check(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
281 bool sending)
282{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200283 char ipbuf[INET6_ADDRSTRLEN];
284
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200285 switch(conn->osmux.state) {
286 case OSMUX_STATE_ACTIVATING:
Pau Espin Pedrol295570c2019-05-13 17:32:47 +0200287 if (osmux_enable_conn(endp, conn, &conn->end.addr, conn->end.rtp_port) < 0) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200288 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200289 "Could not enable osmux for conn on %s: %s\n",
290 sending ? "sent" : "received",
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200291 mgcp_conn_dump(conn->conn));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200292 return -1;
293 }
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200294 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200295 "Osmux %s CID %u towards %s:%u is now enabled\n",
296 sending ? "sent" : "received",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200297 conn->osmux.cid,
298 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf),
Pau Espin Pedrol295570c2019-05-13 17:32:47 +0200299 ntohs(conn->end.rtp_port));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200300 return 0;
301 case OSMUX_STATE_ENABLED:
302 return 0;
303 default:
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200304 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
305 "Osmux %s in conn %s without full negotiation, state %d\n",
306 sending ? "sent" : "received",
307 mgcp_conn_dump(conn->conn), conn->osmux.state);
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200308 return -1;
309 }
310}
311
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200312static int osmux_legacy_dummy_parse_cid(struct sockaddr_in *addr, struct msgb *msg,
313 uint8_t *osmux_cid)
314{
Pau Espin Pedrolb5583cd2019-05-13 12:58:24 +0200315 if (msg->len < 1 + sizeof(*osmux_cid)) {
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200316 LOGP(DLMGCP, LOGL_ERROR,
Pau Espin Pedrolb5583cd2019-05-13 12:58:24 +0200317 "Discarding truncated Osmux dummy load: %s\n", osmo_hexdump(msg->data, msg->len));
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200318 return -1;
319 }
320
321 /* extract the osmux CID from the dummy message */
322 memcpy(osmux_cid, &msg->data[1], sizeof(*osmux_cid));
323 return 0;
324}
325
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200326/* This is called from the bsc-nat */
327static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr,
328 struct msgb *msg)
329{
330 uint8_t osmux_cid;
331 struct mgcp_conn_rtp *conn;
332
333 if (osmux_legacy_dummy_parse_cid(addr, msg, &osmux_cid) < 0)
334 goto out;
335
336 conn = osmux_conn_lookup(cfg, osmux_cid, &addr->sin_addr);
337 if (!conn) {
338 LOGP(DLMGCP, LOGL_ERROR,
339 "Cannot find conn for Osmux CID %d\n", osmux_cid);
340 goto out;
341 }
342
343 endp_osmux_state_check(conn->conn->endp, conn, false);
344 /* Only needed to punch hole in firewall, it can be dropped */
345out:
346 msgb_free(msg);
347 return 0;
348}
349
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200350#define osmux_chunk_length(msg, rem) (rem - msg->len);
351
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200352static int osmux_read_fd_cb(struct osmo_fd *ofd, unsigned int what)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200353{
354 struct msgb *msg;
355 struct osmux_hdr *osmuxh;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200356 struct sockaddr_in addr;
357 struct mgcp_config *cfg = ofd->data;
358 uint32_t rem;
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200359 struct mgcp_conn_rtp *conn_src;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200360
361 msg = osmux_recv(ofd, &addr);
362 if (!msg)
363 return -1;
364
Pau Espin Pedrold14163e2018-10-16 15:48:59 +0200365 if (!cfg->osmux) {
366 LOGP(DLMGCP, LOGL_ERROR,
367 "bsc-nat wants to use Osmux but bsc did not request it\n");
368 goto out;
369 }
370
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200371 /* not any further processing dummy messages */
372 if (msg->data[0] == MGCP_DUMMY_LOAD)
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200373 return osmux_handle_dummy(cfg, &addr, msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200374
375 rem = msg->len;
376 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200377
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200378 conn_src = osmux_conn_lookup(cfg, osmuxh->circuit_id,
379 &addr.sin_addr);
380 if (!conn_src) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200381 LOGP(DLMGCP, LOGL_ERROR,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200382 "Cannot find a src conn for circuit_id=%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200383 osmuxh->circuit_id);
384 goto out;
385 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200386
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200387 /*conn_dst = mgcp_find_dst_conn(conn_src->conn);
388 if (!conn_dst) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200389 LOGP(DLMGCP, LOGL_ERROR,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200390 "Cannot find a dst conn for circuit_id=%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200391 osmuxh->circuit_id);
392 goto out;
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200393 }*/
394
395 if (endp_osmux_state_check(conn_src->conn->endp, conn_src, false) == 0) {
396 conn_src->osmux.stats.octets += osmux_chunk_length(msg, rem);
397 conn_src->osmux.stats.chunks++;
398 osmux_xfrm_output_sched(&conn_src->osmux.out, osmuxh);
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200399 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200400 rem = msg->len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200401 }
402out:
403 msgb_free(msg);
404 return 0;
405}
406
407int osmux_init(int role, struct mgcp_config *cfg)
408{
409 int ret;
410
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200411 osmux_fd.cb = osmux_read_fd_cb;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200412 osmux_fd.data = cfg;
413
414 ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port);
415 if (ret < 0) {
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200416 LOGP(DLMGCP, LOGL_ERROR, "cannot bind OSMUX socket to %s:%u\n",
417 cfg->osmux_addr, cfg->osmux_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200418 return ret;
419 }
420 mgcp_set_ip_tos(osmux_fd.fd, cfg->endp_dscp);
Pau Espin Pedrola7152e02020-05-09 19:15:50 +0200421 osmux_fd.when |= OSMO_FD_READ;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200422
423 ret = osmo_fd_register(&osmux_fd);
424 if (ret < 0) {
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200425 LOGP(DLMGCP, LOGL_ERROR, "cannot register OSMUX socket %s\n",
426 osmo_sock_get_name2(osmux_fd.fd));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200427 return ret;
428 }
429 cfg->osmux_init = 1;
430
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200431 LOGP(DLMGCP, LOGL_INFO, "OSMUX socket listening on %s\n",
432 osmo_sock_get_name2(osmux_fd.fd));
433
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200434 return 0;
435}
436
Philipp Maier87bd9be2017-08-22 16:35:41 +0200437/*! enable OSXMUX circuit for a specified connection.
438 * \param[in] endp mgcp endpoint (configuration)
439 * \param[in] conn connection to disable
440 * \param[in] addr IP address of remote OSMUX endpoint
Pau Espin Pedrol12056862019-05-13 17:30:21 +0200441 * \param[in] port portnumber of the remote OSMUX endpoint (in network byte order)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200442 * \returns 0 on success, -1 on ERROR */
443int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200444 struct osmo_sockaddr *addr, uint16_t port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200445{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200446 /*! If osmux is enabled, initialize the output handler. This handler is
447 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
448 * allocated based on the circuit ID (conn_net->osmux.cid), which is unique
449 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200450 * SSRC space (2^32) by the OSMUX_CID_MAX + 1 possible circuit IDs, then randomly
Philipp Maier87bd9be2017-08-22 16:35:41 +0200451 * select one value from that window. Thus, we have no chance to have
452 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
453 * similarly, for flows traveling to the MSC.
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200454 */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200455 struct in6_addr addr_unset = {};
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200456 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / (OSMUX_CID_MAX + 1);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200457 uint16_t osmux_dummy = endp->cfg->osmux_dummy;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200458
Philipp Maier87bd9be2017-08-22 16:35:41 +0200459 /* Check if osmux is enabled for the specified connection */
Pau Espin Pedrol48aff622018-10-16 16:03:57 +0200460 if (conn->osmux.state != OSMUX_STATE_ACTIVATING) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200461 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
462 "conn:%s didn't negotiate Osmux, state %d\n",
463 mgcp_conn_dump(conn->conn), conn->osmux.state);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200464 return -1;
465 }
466
Pau Espin Pedroldac2ca72019-05-13 17:34:51 +0200467 /* Wait until we have the connection information from MDCX */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200468 if (memcmp(&conn->end.addr, &addr_unset,
469 conn->end.addr.u.sa.sa_family == AF_INET6 ?
470 sizeof(struct in6_addr) :
471 sizeof(struct in_addr)) == 0) {
Pau Espin Pedroldac2ca72019-05-13 17:34:51 +0200472 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO,
473 "Osmux remote address/port still unknown\n");
474 return -1;
475 }
476
Philipp Maier87bd9be2017-08-22 16:35:41 +0200477 conn->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
478 if (!conn->osmux.in) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200479 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
480 "Cannot allocate input osmux handle for conn:%s\n",
481 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200482 return -1;
483 }
Pau Espin Pedrolac772d82019-05-06 18:02:00 +0200484 if (osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.cid, osmux_dummy) < 0) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200485 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
486 "Cannot open osmux circuit %u for conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200487 conn->osmux.cid, mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200488 return -1;
489 }
490
Pau Espin Pedroldf7d97e2019-05-17 17:14:06 +0200491 osmux_xfrm_output_init2(&conn->osmux.out,
Pau Espin Pedrol426a9d92018-10-16 15:31:45 +0200492 (conn->osmux.cid * rtp_ssrc_winlen) +
Pau Espin Pedroldf7d97e2019-05-17 17:14:06 +0200493 (random() % rtp_ssrc_winlen),
494 conn->end.codec->payload_type);
Pau Espin Pedrol426a9d92018-10-16 15:31:45 +0200495
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200496 osmux_xfrm_output_set_tx_cb(&conn->osmux.out,
497 scheduled_from_osmux_tx_rtp_cb, conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200498
499 conn->osmux.state = OSMUX_STATE_ENABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200500
501 return 0;
502}
503
Philipp Maier87bd9be2017-08-22 16:35:41 +0200504/*! disable OSXMUX circuit for a specified connection.
505 * \param[in] conn connection to disable */
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200506void conn_osmux_disable(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200507{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200508
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200509 OSMO_ASSERT(conn->osmux.state != OSMUX_STATE_DISABLED);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200510
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200511 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO,
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200512 "Releasing connection using Osmux CID %u\n", conn->osmux.cid);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200513
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200514 if (conn->osmux.state == OSMUX_STATE_ENABLED) {
515 /* We are closing, we don't need pending RTP packets to be transmitted */
516 osmux_xfrm_output_set_tx_cb(&conn->osmux.out, NULL, NULL);
517 osmux_xfrm_output_flush(&conn->osmux.out);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200518
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200519 osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.cid);
520 conn->osmux.state = OSMUX_STATE_DISABLED;
521 conn_osmux_release_cid(conn);
522 osmux_handle_put(conn->osmux.in);
523 }
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200524 conn_osmux_release_cid(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200525}
526
Philipp Maier87bd9be2017-08-22 16:35:41 +0200527/*! relase OSXMUX cid, that had been allocated to this connection.
528 * \param[in] conn connection with OSMUX cid to release */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200529void conn_osmux_release_cid(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200530{
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200531 if (conn->osmux.cid_allocated)
532 osmux_cid_pool_put(conn->osmux.cid);
533 conn->osmux.cid = 0;
534 conn->osmux.cid_allocated = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200535}
536
Philipp Maier87bd9be2017-08-22 16:35:41 +0200537/*! allocate OSXMUX cid to connection.
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200538 * \param[in] conn connection for which we allocate the OSMUX cid
539 * \param[in] osmux_cid OSMUX cid to allocate. -1 Means take next available one.
540 * \returns Allocated OSMUX cid, -1 on error (no free cids avail, or selected one is already taken).
541 */
542int conn_osmux_allocate_cid(struct mgcp_conn_rtp *conn, int osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200543{
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200544 if (osmux_cid != -1 && osmux_cid_pool_allocated((uint8_t) osmux_cid)) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200545 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO,
546 "Osmux CID %d already allocated!\n",
547 osmux_cid);
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200548 return -1;
549 }
550
551 if (osmux_cid == -1) {
552 osmux_cid = osmux_cid_pool_get_next();
553 if (osmux_cid == -1) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200554 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO,
555 "no available Osmux CID to allocate!\n");
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200556 return -1;
557 }
558 } else
559 osmux_cid_pool_get(osmux_cid);
560
561 conn->osmux.cid = (uint8_t) osmux_cid;
562 conn->osmux.cid_allocated = true;
Pau Espin Pedrolfa810e82019-05-06 18:54:10 +0200563 conn->type = MGCP_OSMUX_BSC;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200564 return osmux_cid;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200565}
566
Philipp Maier87bd9be2017-08-22 16:35:41 +0200567/*! send RTP dummy packet to OSMUX connection port.
568 * \param[in] endp mcgp endpoint that holds the RTP connection
569 * \param[in] conn associated RTP connection
570 * \returns bytes sent, -1 on error */
571int osmux_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200572{
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200573 char ipbuf[INET6_ADDRSTRLEN];
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200574 struct osmux_hdr *osmuxh;
575 int buf_len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200576 struct in_addr addr_unset = {};
577
Philipp Maier87bd9be2017-08-22 16:35:41 +0200578 /*! The dummy packet will not be sent via the actual OSMUX connection,
579 * instead it is sent out of band to port where the remote OSMUX
580 * multplexer is listening. The goal is to ensure that the connection
581 * is kept open */
582
583 /*! We don't need to send the dummy load for osmux so often as another
584 * endpoint may have already punched the hole in the firewall. This
585 * approach is simple though. */
586
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200587 /* Wait until we have the connection information from MDCX */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200588 if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200589 return 0;
590
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200591 if (endp_osmux_state_check(endp, conn, true) < 0)
592 return 0;
593
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200594 buf_len = sizeof(struct osmux_hdr) + osmo_amr_bytes(AMR_FT_0);
595 osmuxh = (struct osmux_hdr *) alloca(buf_len);
596 memset(osmuxh, 0, buf_len);
597 osmuxh->ft = OSMUX_FT_DUMMY;
598 osmuxh->amr_ft = AMR_FT_0;
599 osmuxh->circuit_id = conn->osmux.cid;
600
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200601 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200602 "sending OSMUX dummy load to %s:%u CID %u\n",
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200603 osmo_sockaddr_ntop(&conn->end.addr.u.sa, ipbuf),
604 ntohs(conn->end.rtp_port), conn->osmux.cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200605
Philipp Maier87bd9be2017-08-22 16:35:41 +0200606 return mgcp_udp_send(osmux_fd.fd, &conn->end.addr,
Pau Espin Pedrolc1ad5532019-05-15 23:00:53 +0200607 conn->end.rtp_port, (char*)osmuxh, buf_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200608}
609
Pau Espin Pedrolbcd52e52018-09-17 12:41:28 +0200610/* bsc-nat allocates/releases the Osmux circuit ID. +7 to round up to 8 bit boundary. */
611static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1 + 7) / 8];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200612
Philipp Maier87bd9be2017-08-22 16:35:41 +0200613/*! count the number of taken OSMUX cids.
614 * \returns number of OSMUX cids in use */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200615int osmux_cid_pool_count_used(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200616{
617 int i, j, used = 0;
618
619 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
620 for (j = 0; j < 8; j++) {
621 if (osmux_cid_bitmap[i] & (1 << j))
622 used += 1;
623 }
624 }
625
626 return used;
627}
628
Philipp Maier87bd9be2017-08-22 16:35:41 +0200629/*! take a free OSMUX cid.
630 * \returns OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200631int osmux_cid_pool_get_next(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200632{
633 int i, j;
634
635 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
636 for (j = 0; j < 8; j++) {
637 if (osmux_cid_bitmap[i] & (1 << j))
638 continue;
639
640 osmux_cid_bitmap[i] |= (1 << j);
641 LOGP(DLMGCP, LOGL_DEBUG,
642 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
643 return (i * 8) + j;
644 }
645 }
646
647 LOGP(DLMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n");
648 return -1;
649}
650
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200651/*! take a specific OSMUX cid.
652 * \param[in] osmux_cid OSMUX cid */
653void osmux_cid_pool_get(uint8_t osmux_cid)
654{
655 LOGP(DLMGCP, LOGL_DEBUG, "Allocating Osmux CID %u from pool\n", osmux_cid);
656 osmux_cid_bitmap[osmux_cid / 8] |= (1 << (osmux_cid % 8));
657}
658
Philipp Maier87bd9be2017-08-22 16:35:41 +0200659/*! put back a no longer used OSMUX cid.
660 * \param[in] osmux_cid OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200661void osmux_cid_pool_put(uint8_t osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200662{
663 LOGP(DLMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
664 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
665}
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200666
667/*! check if OSMUX cid is already taken */
668bool osmux_cid_pool_allocated(uint8_t osmux_cid)
669{
670 return !!(osmux_cid_bitmap[osmux_cid / 8] & (1 << (osmux_cid % 8)));
671}