blob: 7a817f809dd5de37868a52c55a0c4f141e2d0cb0 [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>
23
Philipp Maier87bd9be2017-08-22 16:35:41 +020024#include <osmocom/mgcp/mgcp.h>
25#include <osmocom/mgcp/mgcp_internal.h>
26#include <osmocom/mgcp/osmux.h>
27#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010028#include <osmocom/mgcp/mgcp_endp.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020029
30static struct osmo_fd osmux_fd;
31
32static LLIST_HEAD(osmux_handle_list);
33
34struct osmux_handle {
35 struct llist_head head;
36 struct osmux_in_handle *in;
37 struct in_addr rem_addr;
Pau Espin Pedrol12056862019-05-13 17:30:21 +020038 int rem_port; /* network byte order */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020039 int refcnt;
40};
41
42static void *osmux;
43
Philipp Maier87bd9be2017-08-22 16:35:41 +020044/* Deliver OSMUX batch to the remote end */
45static void osmux_deliver_cb(struct msgb *batch_msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020046{
47 struct osmux_handle *handle = data;
48 struct sockaddr_in out = {
49 .sin_family = AF_INET,
50 .sin_port = handle->rem_port,
51 };
52
53 memcpy(&out.sin_addr, &handle->rem_addr, sizeof(handle->rem_addr));
54 sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0,
55 (struct sockaddr *)&out, sizeof(out));
56 msgb_free(batch_msg);
57}
58
Philipp Maier87bd9be2017-08-22 16:35:41 +020059/* Lookup existing OSMUX handle for specified destination address. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020060static struct osmux_handle *
61osmux_handle_find_get(struct in_addr *addr, int rem_port)
62{
63 struct osmux_handle *h;
64
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020065 llist_for_each_entry(h, &osmux_handle_list, head) {
66 if (memcmp(&h->rem_addr, addr, sizeof(struct in_addr)) == 0 &&
67 h->rem_port == rem_port) {
68 LOGP(DLMGCP, LOGL_DEBUG, "using existing OSMUX handle "
69 "for addr=%s:%d\n",
70 inet_ntoa(*addr), ntohs(rem_port));
71 h->refcnt++;
72 return h;
73 }
74 }
75
76 return NULL;
77}
78
Philipp Maier87bd9be2017-08-22 16:35:41 +020079/* Put down no longer needed OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020080static void osmux_handle_put(struct osmux_in_handle *in)
81{
82 struct osmux_handle *h;
83
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020084 llist_for_each_entry(h, &osmux_handle_list, head) {
85 if (h->in == in) {
86 if (--h->refcnt == 0) {
87 LOGP(DLMGCP, LOGL_INFO,
88 "Releasing unused osmux handle for %s:%d\n",
89 inet_ntoa(h->rem_addr),
90 ntohs(h->rem_port));
91 LOGP(DLMGCP, LOGL_INFO, "Stats: "
92 "input RTP msgs: %u bytes: %"PRIu64" "
93 "output osmux msgs: %u bytes: %"PRIu64"\n",
94 in->stats.input_rtp_msgs,
95 in->stats.input_rtp_bytes,
96 in->stats.output_osmux_msgs,
97 in->stats.output_osmux_bytes);
98 llist_del(&h->head);
99 osmux_xfrm_input_fini(h->in);
100 talloc_free(h);
101 }
102 return;
103 }
104 }
105 LOGP(DLMGCP, LOGL_ERROR, "cannot find Osmux input handle %p\n", in);
106}
107
Philipp Maier87bd9be2017-08-22 16:35:41 +0200108/* Allocate free OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200109static struct osmux_handle *
110osmux_handle_alloc(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
111{
112 struct osmux_handle *h;
113
114 h = talloc_zero(osmux, struct osmux_handle);
115 if (!h)
116 return NULL;
117 h->rem_addr = *addr;
118 h->rem_port = rem_port;
119 h->refcnt++;
120
121 h->in = talloc_zero(h, struct osmux_in_handle);
122 if (!h->in) {
123 talloc_free(h);
124 return NULL;
125 }
126
Philipp Maier87bd9be2017-08-22 16:35:41 +0200127 /* sequence number to start OSMUX message from */
128 h->in->osmux_seq = 0;
129
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200130 h->in->batch_factor = cfg->osmux_batch;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200131
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200132 /* If batch size is zero, the library defaults to 1470 bytes. */
133 h->in->batch_size = cfg->osmux_batch_size;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200134 h->in->deliver = osmux_deliver_cb;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200135 osmux_xfrm_input_init(h->in);
136 h->in->data = h;
137
138 llist_add(&h->head, &osmux_handle_list);
139
140 LOGP(DLMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s:%d\n",
141 inet_ntoa(*addr), ntohs(rem_port));
142
143 return h;
144}
145
Philipp Maier87bd9be2017-08-22 16:35:41 +0200146/* Lookup existing handle for a specified address, if the handle can not be
Harald Welte1d1b98f2017-12-25 10:03:40 +0100147 * found, the function will automatically allocate one */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200148static struct osmux_in_handle *
149osmux_handle_lookup(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
150{
151 struct osmux_handle *h;
152
153 h = osmux_handle_find_get(addr, rem_port);
154 if (h != NULL)
155 return h->in;
156
157 h = osmux_handle_alloc(cfg, addr, rem_port);
158 if (h == NULL)
159 return NULL;
160
161 return h->in;
162}
163
Philipp Maier87bd9be2017-08-22 16:35:41 +0200164/*! send RTP packet through OSMUX connection.
165 * \param[in] buf rtp data
166 * \param[in] buf_len length of rtp data
167 * \param[in] conn associated RTP connection
168 * \returns 0 on success, -1 on ERROR */
169int osmux_xfrm_to_osmux(char *buf, int buf_len, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200170{
171 int ret;
172 struct msgb *msg;
173
174 msg = msgb_alloc(4096, "RTP");
175 if (!msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200176 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200177
Philipp Maier87bd9be2017-08-22 16:35:41 +0200178 memcpy(msg->data, buf, buf_len);
179 msgb_put(msg, buf_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200180
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200181 if (conn->osmux.state != OSMUX_STATE_ENABLED) {
182 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO, "forwarding RTP to Osmux conn not yet enabled, dropping (cid=%d)\n",
183 conn->osmux.cid);
184 return -1;
185 }
186
Philipp Maier87bd9be2017-08-22 16:35:41 +0200187 while ((ret = osmux_xfrm_input(conn->osmux.in, msg, conn->osmux.cid)) > 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200188 /* batch full, build and deliver it */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200189 osmux_xfrm_input_deliver(conn->osmux.in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200190 }
191 return 0;
192}
193
Philipp Maier87bd9be2017-08-22 16:35:41 +0200194/* Lookup the endpoint that corresponds to the specified address (port) */
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200195static struct mgcp_conn_rtp*
196osmux_conn_lookup(struct mgcp_config *cfg, uint8_t cid,
197 struct in_addr *from_addr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200198{
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200199 struct mgcp_endpoint *endp;
200 struct mgcp_conn *conn = NULL;
201 struct mgcp_conn_rtp * conn_rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200202 int i;
203
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200204 for (i=0; i<cfg->trunk.number_endpoints; i++) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200205
Philipp Maier87bd9be2017-08-22 16:35:41 +0200206 endp = &cfg->trunk.endpoints[i];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200207
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200208 llist_for_each_entry(conn, &endp->conns, entry) {
209 if (conn->type != MGCP_CONN_TYPE_RTP)
210 continue;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200211
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200212 conn_rtp = &conn->u.rtp;
213 if (!mgcp_conn_rtp_is_osmux(conn_rtp))
214 continue;
215
216 if (conn_rtp->osmux.cid == cid)
217 return conn_rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200218 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200219 }
220
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200221 LOGP(DLMGCP, LOGL_ERROR, "Cannot find osmux conn with cid=%d\n", cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200222
223 return NULL;
224}
225
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200226/* 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 */
227enum {
228 MGCP_PROTO_RTP,
229 MGCP_PROTO_RTCP,
230};
231
232static void scheduled_from_osmux_tx_rtp_cb(struct msgb *msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200233{
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200234 struct mgcp_conn_rtp *conn = data;
235 struct mgcp_endpoint *endp = conn->conn->endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200236 struct sockaddr_in addr = {
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200237 .sin_addr = conn->end.addr,
238 .sin_port = conn->end.rtp_port,
239 }; /* FIXME: not set/used in cb */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200240
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200241
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200242 endp->type->dispatch_rtp_cb(MGCP_PROTO_RTP, &addr, (char *)msg->data, msg->len, conn->conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200243 msgb_free(msg);
244}
245
246static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr)
247{
248 struct msgb *msg;
249 socklen_t slen = sizeof(*addr);
250 int ret;
251
252 msg = msgb_alloc(4096, "OSMUX");
253 if (!msg) {
254 LOGP(DLMGCP, LOGL_ERROR, "cannot allocate message\n");
255 return NULL;
256 }
257 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0,
258 (struct sockaddr *)addr, &slen);
259 if (ret <= 0) {
260 msgb_free(msg);
261 LOGP(DLMGCP, LOGL_ERROR, "cannot receive message\n");
262 return NULL;
263 }
264 msgb_put(msg, ret);
265
266 return msg;
267}
268
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200269/* Updates endp osmux state and returns 0 if it can process messages, -1 otherwise */
270static int endp_osmux_state_check(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
271 bool sending)
272{
273 switch(conn->osmux.state) {
274 case OSMUX_STATE_ACTIVATING:
Pau Espin Pedrol295570c2019-05-13 17:32:47 +0200275 if (osmux_enable_conn(endp, conn, &conn->end.addr, conn->end.rtp_port) < 0) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200276 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200277 "Could not enable osmux for conn on %s: %s\n",
278 sending ? "sent" : "received",
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200279 mgcp_conn_dump(conn->conn));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200280 return -1;
281 }
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200282 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200283 "Osmux %s CID %u towards %s:%u is now enabled\n",
284 sending ? "sent" : "received",
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200285 conn->osmux.cid, inet_ntoa(conn->end.addr),
Pau Espin Pedrol295570c2019-05-13 17:32:47 +0200286 ntohs(conn->end.rtp_port));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200287 return 0;
288 case OSMUX_STATE_ENABLED:
289 return 0;
290 default:
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200291 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
292 "Osmux %s in conn %s without full negotiation, state %d\n",
293 sending ? "sent" : "received",
294 mgcp_conn_dump(conn->conn), conn->osmux.state);
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200295 return -1;
296 }
297}
298
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200299static int osmux_legacy_dummy_parse_cid(struct sockaddr_in *addr, struct msgb *msg,
300 uint8_t *osmux_cid)
301{
Pau Espin Pedrolb5583cd2019-05-13 12:58:24 +0200302 if (msg->len < 1 + sizeof(*osmux_cid)) {
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200303 LOGP(DLMGCP, LOGL_ERROR,
Pau Espin Pedrolb5583cd2019-05-13 12:58:24 +0200304 "Discarding truncated Osmux dummy load: %s\n", osmo_hexdump(msg->data, msg->len));
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200305 return -1;
306 }
307
308 /* extract the osmux CID from the dummy message */
309 memcpy(osmux_cid, &msg->data[1], sizeof(*osmux_cid));
310 return 0;
311}
312
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200313/* This is called from the bsc-nat */
314static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr,
315 struct msgb *msg)
316{
317 uint8_t osmux_cid;
318 struct mgcp_conn_rtp *conn;
319
320 if (osmux_legacy_dummy_parse_cid(addr, msg, &osmux_cid) < 0)
321 goto out;
322
323 conn = osmux_conn_lookup(cfg, osmux_cid, &addr->sin_addr);
324 if (!conn) {
325 LOGP(DLMGCP, LOGL_ERROR,
326 "Cannot find conn for Osmux CID %d\n", osmux_cid);
327 goto out;
328 }
329
330 endp_osmux_state_check(conn->conn->endp, conn, false);
331 /* Only needed to punch hole in firewall, it can be dropped */
332out:
333 msgb_free(msg);
334 return 0;
335}
336
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200337#define osmux_chunk_length(msg, rem) (rem - msg->len);
338
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200339static int osmux_read_fd_cb(struct osmo_fd *ofd, unsigned int what)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200340{
341 struct msgb *msg;
342 struct osmux_hdr *osmuxh;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200343 struct sockaddr_in addr;
344 struct mgcp_config *cfg = ofd->data;
345 uint32_t rem;
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200346 struct mgcp_conn_rtp *conn_src;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200347
348 msg = osmux_recv(ofd, &addr);
349 if (!msg)
350 return -1;
351
Pau Espin Pedrold14163e2018-10-16 15:48:59 +0200352 if (!cfg->osmux) {
353 LOGP(DLMGCP, LOGL_ERROR,
354 "bsc-nat wants to use Osmux but bsc did not request it\n");
355 goto out;
356 }
357
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200358 /* not any further processing dummy messages */
359 if (msg->data[0] == MGCP_DUMMY_LOAD)
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200360 return osmux_handle_dummy(cfg, &addr, msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200361
362 rem = msg->len;
363 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200364
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200365 conn_src = osmux_conn_lookup(cfg, osmuxh->circuit_id,
366 &addr.sin_addr);
367 if (!conn_src) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200368 LOGP(DLMGCP, LOGL_ERROR,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200369 "Cannot find a src conn for circuit_id=%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200370 osmuxh->circuit_id);
371 goto out;
372 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200373
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200374 /*conn_dst = mgcp_find_dst_conn(conn_src->conn);
375 if (!conn_dst) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200376 LOGP(DLMGCP, LOGL_ERROR,
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200377 "Cannot find a dst conn for circuit_id=%d\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200378 osmuxh->circuit_id);
379 goto out;
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200380 }*/
381
382 if (endp_osmux_state_check(conn_src->conn->endp, conn_src, false) == 0) {
383 conn_src->osmux.stats.octets += osmux_chunk_length(msg, rem);
384 conn_src->osmux.stats.chunks++;
385 osmux_xfrm_output_sched(&conn_src->osmux.out, osmuxh);
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200386 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200387 rem = msg->len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200388 }
389out:
390 msgb_free(msg);
391 return 0;
392}
393
394int osmux_init(int role, struct mgcp_config *cfg)
395{
396 int ret;
397
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200398 osmux_fd.cb = osmux_read_fd_cb;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200399 osmux_fd.data = cfg;
400
401 ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port);
402 if (ret < 0) {
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200403 LOGP(DLMGCP, LOGL_ERROR, "cannot bind OSMUX socket to %s:%u\n",
404 cfg->osmux_addr, cfg->osmux_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200405 return ret;
406 }
407 mgcp_set_ip_tos(osmux_fd.fd, cfg->endp_dscp);
408 osmux_fd.when |= BSC_FD_READ;
409
410 ret = osmo_fd_register(&osmux_fd);
411 if (ret < 0) {
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200412 LOGP(DLMGCP, LOGL_ERROR, "cannot register OSMUX socket %s\n",
413 osmo_sock_get_name2(osmux_fd.fd));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200414 return ret;
415 }
416 cfg->osmux_init = 1;
417
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200418 LOGP(DLMGCP, LOGL_INFO, "OSMUX socket listening on %s\n",
419 osmo_sock_get_name2(osmux_fd.fd));
420
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200421 return 0;
422}
423
Philipp Maier87bd9be2017-08-22 16:35:41 +0200424/*! enable OSXMUX circuit for a specified connection.
425 * \param[in] endp mgcp endpoint (configuration)
426 * \param[in] conn connection to disable
427 * \param[in] addr IP address of remote OSMUX endpoint
Pau Espin Pedrol12056862019-05-13 17:30:21 +0200428 * \param[in] port portnumber of the remote OSMUX endpoint (in network byte order)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200429 * \returns 0 on success, -1 on ERROR */
430int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
431 struct in_addr *addr, uint16_t port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200432{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200433 /*! If osmux is enabled, initialize the output handler. This handler is
434 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
435 * allocated based on the circuit ID (conn_net->osmux.cid), which is unique
436 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200437 * SSRC space (2^32) by the OSMUX_CID_MAX + 1 possible circuit IDs, then randomly
Philipp Maier87bd9be2017-08-22 16:35:41 +0200438 * select one value from that window. Thus, we have no chance to have
439 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
440 * similarly, for flows traveling to the MSC.
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200441 */
Pau Espin Pedroldac2ca72019-05-13 17:34:51 +0200442 struct in_addr addr_unset = {};
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200443 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / (OSMUX_CID_MAX + 1);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200444 uint16_t osmux_dummy = endp->cfg->osmux_dummy;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200445
Philipp Maier87bd9be2017-08-22 16:35:41 +0200446 /* Check if osmux is enabled for the specified connection */
Pau Espin Pedrol48aff622018-10-16 16:03:57 +0200447 if (conn->osmux.state != OSMUX_STATE_ACTIVATING) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200448 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
449 "conn:%s didn't negotiate Osmux, state %d\n",
450 mgcp_conn_dump(conn->conn), conn->osmux.state);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200451 return -1;
452 }
453
Pau Espin Pedroldac2ca72019-05-13 17:34:51 +0200454 /* Wait until we have the connection information from MDCX */
455 if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0) {
456 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO,
457 "Osmux remote address/port still unknown\n");
458 return -1;
459 }
460
Philipp Maier87bd9be2017-08-22 16:35:41 +0200461 conn->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
462 if (!conn->osmux.in) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200463 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
464 "Cannot allocate input osmux handle for conn:%s\n",
465 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200466 return -1;
467 }
Pau Espin Pedrolac772d82019-05-06 18:02:00 +0200468 if (osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.cid, osmux_dummy) < 0) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200469 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
470 "Cannot open osmux circuit %u for conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200471 conn->osmux.cid, mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200472 return -1;
473 }
474
Pau Espin Pedrol426a9d92018-10-16 15:31:45 +0200475 osmux_xfrm_output_init(&conn->osmux.out,
476 (conn->osmux.cid * rtp_ssrc_winlen) +
477 (random() % rtp_ssrc_winlen));
478
Pau Espin Pedrol1442c5a2019-05-06 16:10:10 +0200479 osmux_xfrm_output_set_tx_cb(&conn->osmux.out,
480 scheduled_from_osmux_tx_rtp_cb, conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200481
482 conn->osmux.state = OSMUX_STATE_ENABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200483
484 return 0;
485}
486
Philipp Maier87bd9be2017-08-22 16:35:41 +0200487/*! disable OSXMUX circuit for a specified connection.
488 * \param[in] conn connection to disable */
489void osmux_disable_conn(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200490{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200491 if (!conn)
492 return;
493
494 if (conn->osmux.state != OSMUX_STATE_ENABLED)
495 return;
496
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200497 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO,
498 "Releasing connection %s using Osmux CID %u\n",
499 conn->conn->id, conn->osmux.cid);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200500
501 /* We are closing, we don't need pending RTP packets to be transmitted */
502 osmux_xfrm_output_set_tx_cb(&conn->osmux.out, NULL, NULL);
503 osmux_xfrm_output_flush(&conn->osmux.out);
504
Philipp Maier87bd9be2017-08-22 16:35:41 +0200505 osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.cid);
506 conn->osmux.state = OSMUX_STATE_DISABLED;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200507 conn_osmux_release_cid(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200508 osmux_handle_put(conn->osmux.in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200509}
510
Philipp Maier87bd9be2017-08-22 16:35:41 +0200511/*! relase OSXMUX cid, that had been allocated to this connection.
512 * \param[in] conn connection with OSMUX cid to release */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200513void conn_osmux_release_cid(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200514{
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200515 if (conn->osmux.cid_allocated)
516 osmux_cid_pool_put(conn->osmux.cid);
517 conn->osmux.cid = 0;
518 conn->osmux.cid_allocated = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200519}
520
Philipp Maier87bd9be2017-08-22 16:35:41 +0200521/*! allocate OSXMUX cid to connection.
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200522 * \param[in] conn connection for which we allocate the OSMUX cid
523 * \param[in] osmux_cid OSMUX cid to allocate. -1 Means take next available one.
524 * \returns Allocated OSMUX cid, -1 on error (no free cids avail, or selected one is already taken).
525 */
526int conn_osmux_allocate_cid(struct mgcp_conn_rtp *conn, int osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200527{
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200528 if (osmux_cid != -1 && osmux_cid_pool_allocated((uint8_t) osmux_cid)) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200529 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO,
530 "Osmux CID %d already allocated!\n",
531 osmux_cid);
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200532 return -1;
533 }
534
535 if (osmux_cid == -1) {
536 osmux_cid = osmux_cid_pool_get_next();
537 if (osmux_cid == -1) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200538 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO,
539 "no available Osmux CID to allocate!\n");
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200540 return -1;
541 }
542 } else
543 osmux_cid_pool_get(osmux_cid);
544
545 conn->osmux.cid = (uint8_t) osmux_cid;
546 conn->osmux.cid_allocated = true;
Pau Espin Pedrolfa810e82019-05-06 18:54:10 +0200547 conn->type = MGCP_OSMUX_BSC;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200548 return osmux_cid;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200549}
550
Philipp Maier87bd9be2017-08-22 16:35:41 +0200551/*! send RTP dummy packet to OSMUX connection port.
552 * \param[in] endp mcgp endpoint that holds the RTP connection
553 * \param[in] conn associated RTP connection
554 * \returns bytes sent, -1 on error */
555int osmux_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200556{
557 char buf[1 + sizeof(uint8_t)];
558 struct in_addr addr_unset = {};
559
Philipp Maier87bd9be2017-08-22 16:35:41 +0200560 /*! The dummy packet will not be sent via the actual OSMUX connection,
561 * instead it is sent out of band to port where the remote OSMUX
562 * multplexer is listening. The goal is to ensure that the connection
563 * is kept open */
564
565 /*! We don't need to send the dummy load for osmux so often as another
566 * endpoint may have already punched the hole in the firewall. This
567 * approach is simple though. */
568
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200569 buf[0] = MGCP_DUMMY_LOAD;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200570 memcpy(&buf[1], &conn->osmux.cid, sizeof(conn->osmux.cid));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200571
572 /* Wait until we have the connection information from MDCX */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200573 if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200574 return 0;
575
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200576 if (endp_osmux_state_check(endp, conn, true) < 0)
577 return 0;
578
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200579 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200580 "sending OSMUX dummy load to %s:%u CID %u\n",
581 inet_ntoa(conn->end.addr), ntohs(conn->end.rtp_port), conn->osmux.cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200582
Philipp Maier87bd9be2017-08-22 16:35:41 +0200583 return mgcp_udp_send(osmux_fd.fd, &conn->end.addr,
Pau Espin Pedrol295570c2019-05-13 17:32:47 +0200584 conn->end.rtp_port, buf, sizeof(buf));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200585}
586
Pau Espin Pedrolbcd52e52018-09-17 12:41:28 +0200587/* bsc-nat allocates/releases the Osmux circuit ID. +7 to round up to 8 bit boundary. */
588static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1 + 7) / 8];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200589
Philipp Maier87bd9be2017-08-22 16:35:41 +0200590/*! count the number of taken OSMUX cids.
591 * \returns number of OSMUX cids in use */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200592int osmux_cid_pool_count_used(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200593{
594 int i, j, used = 0;
595
596 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
597 for (j = 0; j < 8; j++) {
598 if (osmux_cid_bitmap[i] & (1 << j))
599 used += 1;
600 }
601 }
602
603 return used;
604}
605
Philipp Maier87bd9be2017-08-22 16:35:41 +0200606/*! take a free OSMUX cid.
607 * \returns OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200608int osmux_cid_pool_get_next(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200609{
610 int i, j;
611
612 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
613 for (j = 0; j < 8; j++) {
614 if (osmux_cid_bitmap[i] & (1 << j))
615 continue;
616
617 osmux_cid_bitmap[i] |= (1 << j);
618 LOGP(DLMGCP, LOGL_DEBUG,
619 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
620 return (i * 8) + j;
621 }
622 }
623
624 LOGP(DLMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n");
625 return -1;
626}
627
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200628/*! take a specific OSMUX cid.
629 * \param[in] osmux_cid OSMUX cid */
630void osmux_cid_pool_get(uint8_t osmux_cid)
631{
632 LOGP(DLMGCP, LOGL_DEBUG, "Allocating Osmux CID %u from pool\n", osmux_cid);
633 osmux_cid_bitmap[osmux_cid / 8] |= (1 << (osmux_cid % 8));
634}
635
Philipp Maier87bd9be2017-08-22 16:35:41 +0200636/*! put back a no longer used OSMUX cid.
637 * \param[in] osmux_cid OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200638void osmux_cid_pool_put(uint8_t osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200639{
640 LOGP(DLMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
641 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
642}
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200643
644/*! check if OSMUX cid is already taken */
645bool osmux_cid_pool_allocated(uint8_t osmux_cid)
646{
647 return !!(osmux_cid_bitmap[osmux_cid / 8] & (1 << (osmux_cid % 8)));
648}