blob: 9bbc1b273cd69c21d09f630a0403f9716fcd2949 [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
Philipp Maier87bd9be2017-08-22 16:35:41 +0200181 while ((ret = osmux_xfrm_input(conn->osmux.in, msg, conn->osmux.cid)) > 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200182 /* batch full, build and deliver it */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200183 osmux_xfrm_input_deliver(conn->osmux.in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200184 }
185 return 0;
186}
187
Philipp Maier87bd9be2017-08-22 16:35:41 +0200188/* Lookup the endpoint that corresponds to the specified address (port) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200189static struct mgcp_endpoint *
190endpoint_lookup(struct mgcp_config *cfg, int cid,
191 struct in_addr *from_addr, int type)
192{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200193 struct mgcp_endpoint *endp = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200194 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200195 struct mgcp_conn_rtp *conn_net = NULL;
196 struct mgcp_conn_rtp *conn_bts = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200197
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200198 for (i=0; i<cfg->trunk.number_endpoints; i++) {
199 struct in_addr *this;
200
Philipp Maier87bd9be2017-08-22 16:35:41 +0200201 endp = &cfg->trunk.endpoints[i];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200202
Philipp Maier87bd9be2017-08-22 16:35:41 +0200203#if 0
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200204 if (!tmp->allocated)
205 continue;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200206#endif
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200207
208 switch(type) {
209 case MGCP_DEST_NET:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200210 /* FIXME: Get rid of CONN_ID_XXX! */
211 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
Philipp Maierddf1f9d2017-11-07 12:18:38 +0100212 if (conn_net)
213 this = &conn_net->end.addr;
214 else
215 this = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200216 break;
217 case MGCP_DEST_BTS:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200218 /* FIXME: Get rid of CONN_ID_XXX! */
219 conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS);
Philipp Maierddf1f9d2017-11-07 12:18:38 +0100220 if (conn_bts)
221 this = &conn_bts->end.addr;
222 else
223 this = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200224 break;
225 default:
226 /* Should not ever happen */
227 LOGP(DLMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type);
228 return NULL;
229 }
230
Philipp Maier87bd9be2017-08-22 16:35:41 +0200231 /* FIXME: Get rid of CONN_ID_XXX! */
232 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
Philipp Maierddf1f9d2017-11-07 12:18:38 +0100233 if (conn_net && this && conn_net->osmux.cid == cid
234 && this->s_addr == from_addr->s_addr)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200235 return endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200236 }
237
238 LOGP(DLMGCP, LOGL_ERROR, "Cannot find endpoint with cid=%d\n", cid);
239
240 return NULL;
241}
242
243static void scheduled_tx_net_cb(struct msgb *msg, void *data)
244{
245 struct mgcp_endpoint *endp = data;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200246 struct mgcp_conn_rtp *conn_net = NULL;
247 struct mgcp_conn_rtp *conn_bts = NULL;
248
249 /* FIXME: Get rid of CONN_ID_XXX! */
250 conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS);
251 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
252 if (!conn_bts || !conn_net)
253 return;
254
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200255 struct sockaddr_in addr = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200256 .sin_addr = conn_net->end.addr,
257 .sin_port = conn_net->end.rtp_port,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200258 };
259
Philipp Maiercede2a42018-07-03 14:14:21 +0200260 rate_ctr_inc(&conn_bts->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
261 rate_ctr_add(&conn_bts->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], msg->len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200262
Philipp Maier87bd9be2017-08-22 16:35:41 +0200263 /* Send RTP data to NET */
264 /* FIXME: Get rid of conn_bts and conn_net! */
265 mgcp_send(endp, 1, &addr, (char *)msg->data, msg->len,
266 conn_bts, conn_net);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200267 msgb_free(msg);
268}
269
270static void scheduled_tx_bts_cb(struct msgb *msg, void *data)
271{
272 struct mgcp_endpoint *endp = data;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200273 struct mgcp_conn_rtp *conn_net = NULL;
274 struct mgcp_conn_rtp *conn_bts = NULL;
275
276 /* FIXME: Get rid of CONN_ID_XXX! */
277 conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS);
278 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
279 if (!conn_bts || !conn_net)
280 return;
281
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200282 struct sockaddr_in addr = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200283 .sin_addr = conn_bts->end.addr,
284 .sin_port = conn_bts->end.rtp_port,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200285 };
286
Philipp Maiercede2a42018-07-03 14:14:21 +0200287 rate_ctr_inc(&conn_net->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
Pau Espin Pedrol426a9d92018-10-16 15:31:45 +0200288 rate_ctr_add(&conn_net->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], msg->len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200289
Philipp Maier87bd9be2017-08-22 16:35:41 +0200290 /* Send RTP data to BTS */
291 /* FIXME: Get rid of conn_bts and conn_net! */
292 mgcp_send(endp, 1, &addr, (char *)msg->data, msg->len,
293 conn_net, conn_bts);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200294 msgb_free(msg);
295}
296
297static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr)
298{
299 struct msgb *msg;
300 socklen_t slen = sizeof(*addr);
301 int ret;
302
303 msg = msgb_alloc(4096, "OSMUX");
304 if (!msg) {
305 LOGP(DLMGCP, LOGL_ERROR, "cannot allocate message\n");
306 return NULL;
307 }
308 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0,
309 (struct sockaddr *)addr, &slen);
310 if (ret <= 0) {
311 msgb_free(msg);
312 LOGP(DLMGCP, LOGL_ERROR, "cannot receive message\n");
313 return NULL;
314 }
315 msgb_put(msg, ret);
316
317 return msg;
318}
319
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200320/* Updates endp osmux state and returns 0 if it can process messages, -1 otherwise */
321static int endp_osmux_state_check(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
322 bool sending)
323{
324 switch(conn->osmux.state) {
325 case OSMUX_STATE_ACTIVATING:
Pau Espin Pedrol295570c2019-05-13 17:32:47 +0200326 if (osmux_enable_conn(endp, conn, &conn->end.addr, conn->end.rtp_port) < 0) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200327 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
328 "Could not enable osmux for conn:%s\n",
329 mgcp_conn_dump(conn->conn));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200330 return -1;
331 }
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200332 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
333 "Osmux CID %u for %s:%u is now enabled\n",
334 conn->osmux.cid, inet_ntoa(conn->end.addr),
Pau Espin Pedrol295570c2019-05-13 17:32:47 +0200335 ntohs(conn->end.rtp_port));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200336 return 0;
337 case OSMUX_STATE_ENABLED:
338 return 0;
339 default:
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200340 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
341 "Osmux %s in conn %s without full negotiation, state %d\n",
342 sending ? "sent" : "received",
343 mgcp_conn_dump(conn->conn), conn->osmux.state);
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200344 return -1;
345 }
346}
347
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200348static int osmux_legacy_dummy_parse_cid(struct sockaddr_in *addr, struct msgb *msg,
349 uint8_t *osmux_cid)
350{
Pau Espin Pedrolb5583cd2019-05-13 12:58:24 +0200351 if (msg->len < 1 + sizeof(*osmux_cid)) {
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200352 LOGP(DLMGCP, LOGL_ERROR,
Pau Espin Pedrolb5583cd2019-05-13 12:58:24 +0200353 "Discarding truncated Osmux dummy load: %s\n", osmo_hexdump(msg->data, msg->len));
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200354 return -1;
355 }
356
357 /* extract the osmux CID from the dummy message */
358 memcpy(osmux_cid, &msg->data[1], sizeof(*osmux_cid));
359 return 0;
360}
361
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200362#define osmux_chunk_length(msg, rem) (rem - msg->len);
363
364int osmux_read_from_bsc_nat_cb(struct osmo_fd *ofd, unsigned int what)
365{
366 struct msgb *msg;
367 struct osmux_hdr *osmuxh;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200368 struct sockaddr_in addr;
369 struct mgcp_config *cfg = ofd->data;
370 uint32_t rem;
Pau Espin Pedrol42199042018-05-17 14:26:02 +0200371 struct mgcp_conn_rtp *conn_bts = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200372
373 msg = osmux_recv(ofd, &addr);
374 if (!msg)
375 return -1;
376
Pau Espin Pedrold14163e2018-10-16 15:48:59 +0200377 if (!cfg->osmux) {
378 LOGP(DLMGCP, LOGL_ERROR,
379 "bsc-nat wants to use Osmux but bsc did not request it\n");
380 goto out;
381 }
382
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200383 /* not any further processing dummy messages */
384 if (msg->data[0] == MGCP_DUMMY_LOAD)
385 goto out;
386
387 rem = msg->len;
388 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
389 struct mgcp_endpoint *endp;
390
391 /* Yes, we use MGCP_DEST_NET to locate the origin */
392 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
393 &addr.sin_addr, MGCP_DEST_NET);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200394
395 /* FIXME: Get rid of CONN_ID_XXX! */
Pau Espin Pedrol42199042018-05-17 14:26:02 +0200396 conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS);
397 if (!conn_bts)
Pau Espin Pedrolff6606c2018-10-16 16:45:40 +0200398 continue;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200399
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200400 if (!endp) {
401 LOGP(DLMGCP, LOGL_ERROR,
402 "Cannot find an endpoint for circuit_id=%d\n",
403 osmuxh->circuit_id);
404 goto out;
405 }
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200406 if (endp_osmux_state_check(endp, conn_bts, false) == 0) {
407 conn_bts->osmux.stats.octets += osmux_chunk_length(msg, rem);
408 conn_bts->osmux.stats.chunks++;
409 osmux_xfrm_output_sched(&conn_bts->osmux.out, osmuxh);
410 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200411 rem = msg->len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200412 }
413out:
414 msgb_free(msg);
415 return 0;
416}
417
418/* This is called from the bsc-nat */
419static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr,
Pau Espin Pedrol852ba862018-10-16 15:55:56 +0200420 struct msgb *msg, int endp_type)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200421{
422 struct mgcp_endpoint *endp;
423 uint8_t osmux_cid;
Pau Espin Pedrol852ba862018-10-16 15:55:56 +0200424 struct mgcp_conn_rtp *conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200425
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200426 if (osmux_legacy_dummy_parse_cid(addr, msg, &osmux_cid) < 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200427 goto out;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200428
Pau Espin Pedrol852ba862018-10-16 15:55:56 +0200429 endp = endpoint_lookup(cfg, osmux_cid, &addr->sin_addr, endp_type);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200430 if (!endp) {
431 LOGP(DLMGCP, LOGL_ERROR,
432 "Cannot find endpoint for Osmux CID %d\n", osmux_cid);
433 goto out;
434 }
435
Pau Espin Pedrol852ba862018-10-16 15:55:56 +0200436 /* FIXME: Get rid of CONN_ID_XXX! */
437 conn = mgcp_conn_get_rtp(endp, endp_type == MGCP_DEST_BTS ? CONN_ID_NET : CONN_ID_BTS);
438 if (!conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200439 goto out;
440
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200441 endp_osmux_state_check(endp, conn, false);
442 /* Only needed to punch hole in firewall, it can be dropped */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200443out:
444 msgb_free(msg);
445 return 0;
446}
447
448int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what)
449{
450 struct msgb *msg;
451 struct osmux_hdr *osmuxh;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200452 struct sockaddr_in addr;
453 struct mgcp_config *cfg = ofd->data;
454 uint32_t rem;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200455 struct mgcp_conn_rtp *conn_net = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200456
457 msg = osmux_recv(ofd, &addr);
458 if (!msg)
459 return -1;
460
Pau Espin Pedrold14163e2018-10-16 15:48:59 +0200461 if (!cfg->osmux) {
462 LOGP(DLMGCP, LOGL_ERROR,
463 "bsc wants to use Osmux but bsc-nat did not request it\n");
464 goto out;
465 }
466
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200467 /* not any further processing dummy messages */
468 if (msg->data[0] == MGCP_DUMMY_LOAD)
Pau Espin Pedrol852ba862018-10-16 15:55:56 +0200469 return osmux_handle_dummy(cfg, &addr, msg, MGCP_DEST_BTS);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200470
471 rem = msg->len;
472 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
473 struct mgcp_endpoint *endp;
474
475 /* Yes, we use MGCP_DEST_BTS to locate the origin */
476 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
477 &addr.sin_addr, MGCP_DEST_BTS);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200478
479 /* FIXME: Get rid of CONN_ID_XXX! */
480 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
481 if (!conn_net)
Pau Espin Pedrolff6606c2018-10-16 16:45:40 +0200482 continue;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200483
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200484 if (!endp) {
485 LOGP(DLMGCP, LOGL_ERROR,
486 "Cannot find an endpoint for circuit_id=%d\n",
487 osmuxh->circuit_id);
488 goto out;
489 }
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200490 if (endp_osmux_state_check(endp, conn_net, false) == 0) {
491 conn_net->osmux.stats.octets += osmux_chunk_length(msg, rem);
492 conn_net->osmux.stats.chunks++;
493 osmux_xfrm_output_sched(&conn_net->osmux.out, osmuxh);
494 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200495 rem = msg->len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200496 }
497out:
498 msgb_free(msg);
499 return 0;
500}
501
502int osmux_init(int role, struct mgcp_config *cfg)
503{
504 int ret;
505
506 switch(role) {
507 case OSMUX_ROLE_BSC:
508 osmux_fd.cb = osmux_read_from_bsc_nat_cb;
509 break;
510 case OSMUX_ROLE_BSC_NAT:
511 osmux_fd.cb = osmux_read_from_bsc_cb;
512 break;
513 default:
514 LOGP(DLMGCP, LOGL_ERROR, "wrong role for OSMUX\n");
515 return -1;
516 }
517 osmux_fd.data = cfg;
518
519 ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port);
520 if (ret < 0) {
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200521 LOGP(DLMGCP, LOGL_ERROR, "cannot bind OSMUX socket to %s:%u\n",
522 cfg->osmux_addr, cfg->osmux_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200523 return ret;
524 }
525 mgcp_set_ip_tos(osmux_fd.fd, cfg->endp_dscp);
526 osmux_fd.when |= BSC_FD_READ;
527
528 ret = osmo_fd_register(&osmux_fd);
529 if (ret < 0) {
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200530 LOGP(DLMGCP, LOGL_ERROR, "cannot register OSMUX socket %s\n",
531 osmo_sock_get_name2(osmux_fd.fd));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200532 return ret;
533 }
534 cfg->osmux_init = 1;
535
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200536 LOGP(DLMGCP, LOGL_INFO, "OSMUX socket listening on %s\n",
537 osmo_sock_get_name2(osmux_fd.fd));
538
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200539 return 0;
540}
541
Philipp Maier87bd9be2017-08-22 16:35:41 +0200542/*! enable OSXMUX circuit for a specified connection.
543 * \param[in] endp mgcp endpoint (configuration)
544 * \param[in] conn connection to disable
545 * \param[in] addr IP address of remote OSMUX endpoint
Pau Espin Pedrol12056862019-05-13 17:30:21 +0200546 * \param[in] port portnumber of the remote OSMUX endpoint (in network byte order)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200547 * \returns 0 on success, -1 on ERROR */
548int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
549 struct in_addr *addr, uint16_t port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200550{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200551 /*! If osmux is enabled, initialize the output handler. This handler is
552 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
553 * allocated based on the circuit ID (conn_net->osmux.cid), which is unique
554 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200555 * SSRC space (2^32) by the OSMUX_CID_MAX + 1 possible circuit IDs, then randomly
Philipp Maier87bd9be2017-08-22 16:35:41 +0200556 * select one value from that window. Thus, we have no chance to have
557 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
558 * similarly, for flows traveling to the MSC.
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200559 */
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200560 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / (OSMUX_CID_MAX + 1);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200561 uint16_t osmux_dummy = endp->cfg->osmux_dummy;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200562
Philipp Maier87bd9be2017-08-22 16:35:41 +0200563 /* Check if osmux is enabled for the specified connection */
Pau Espin Pedrol48aff622018-10-16 16:03:57 +0200564 if (conn->osmux.state != OSMUX_STATE_ACTIVATING) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200565 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
566 "conn:%s didn't negotiate Osmux, state %d\n",
567 mgcp_conn_dump(conn->conn), conn->osmux.state);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200568 return -1;
569 }
570
Philipp Maier87bd9be2017-08-22 16:35:41 +0200571 conn->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
572 if (!conn->osmux.in) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200573 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
574 "Cannot allocate input osmux handle for conn:%s\n",
575 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200576 return -1;
577 }
Pau Espin Pedrolac772d82019-05-06 18:02:00 +0200578 if (osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.cid, osmux_dummy) < 0) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200579 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
580 "Cannot open osmux circuit %u for conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200581 conn->osmux.cid, mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200582 return -1;
583 }
584
Pau Espin Pedrol426a9d92018-10-16 15:31:45 +0200585 osmux_xfrm_output_init(&conn->osmux.out,
586 (conn->osmux.cid * rtp_ssrc_winlen) +
587 (random() % rtp_ssrc_winlen));
588
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200589 switch (endp->cfg->role) {
590 case MGCP_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200591 conn->type = MGCP_OSMUX_BSC_NAT;
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200592 osmux_xfrm_output_set_tx_cb(&conn->osmux.out,
593 scheduled_tx_net_cb, endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200594 break;
595 case MGCP_BSC:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200596 conn->type = MGCP_OSMUX_BSC;
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200597 osmux_xfrm_output_set_tx_cb(&conn->osmux.out,
598 scheduled_tx_bts_cb, endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200599 break;
600 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200601
602 conn->osmux.state = OSMUX_STATE_ENABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200603
604 return 0;
605}
606
Philipp Maier87bd9be2017-08-22 16:35:41 +0200607/*! disable OSXMUX circuit for a specified connection.
608 * \param[in] conn connection to disable */
609void osmux_disable_conn(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200610{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200611 if (!conn)
612 return;
613
614 if (conn->osmux.state != OSMUX_STATE_ENABLED)
615 return;
616
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200617 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO,
618 "Releasing connection %s using Osmux CID %u\n",
619 conn->conn->id, conn->osmux.cid);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200620
621 /* We are closing, we don't need pending RTP packets to be transmitted */
622 osmux_xfrm_output_set_tx_cb(&conn->osmux.out, NULL, NULL);
623 osmux_xfrm_output_flush(&conn->osmux.out);
624
Philipp Maier87bd9be2017-08-22 16:35:41 +0200625 osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.cid);
626 conn->osmux.state = OSMUX_STATE_DISABLED;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200627 conn_osmux_release_cid(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200628 osmux_handle_put(conn->osmux.in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200629}
630
Philipp Maier87bd9be2017-08-22 16:35:41 +0200631/*! relase OSXMUX cid, that had been allocated to this connection.
632 * \param[in] conn connection with OSMUX cid to release */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200633void conn_osmux_release_cid(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200634{
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200635 if (conn->osmux.cid_allocated)
636 osmux_cid_pool_put(conn->osmux.cid);
637 conn->osmux.cid = 0;
638 conn->osmux.cid_allocated = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200639}
640
Philipp Maier87bd9be2017-08-22 16:35:41 +0200641/*! allocate OSXMUX cid to connection.
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200642 * \param[in] conn connection for which we allocate the OSMUX cid
643 * \param[in] osmux_cid OSMUX cid to allocate. -1 Means take next available one.
644 * \returns Allocated OSMUX cid, -1 on error (no free cids avail, or selected one is already taken).
645 */
646int conn_osmux_allocate_cid(struct mgcp_conn_rtp *conn, int osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200647{
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200648 if (osmux_cid != -1 && osmux_cid_pool_allocated((uint8_t) osmux_cid)) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200649 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO,
650 "Osmux CID %d already allocated!\n",
651 osmux_cid);
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200652 return -1;
653 }
654
655 if (osmux_cid == -1) {
656 osmux_cid = osmux_cid_pool_get_next();
657 if (osmux_cid == -1) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200658 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO,
659 "no available Osmux CID to allocate!\n");
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200660 return -1;
661 }
662 } else
663 osmux_cid_pool_get(osmux_cid);
664
665 conn->osmux.cid = (uint8_t) osmux_cid;
666 conn->osmux.cid_allocated = true;
Pau Espin Pedrolfa810e82019-05-06 18:54:10 +0200667 conn->type = MGCP_OSMUX_BSC;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200668 return osmux_cid;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200669}
670
Philipp Maier87bd9be2017-08-22 16:35:41 +0200671/*! send RTP dummy packet to OSMUX connection port.
672 * \param[in] endp mcgp endpoint that holds the RTP connection
673 * \param[in] conn associated RTP connection
674 * \returns bytes sent, -1 on error */
675int osmux_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200676{
677 char buf[1 + sizeof(uint8_t)];
678 struct in_addr addr_unset = {};
679
Philipp Maier87bd9be2017-08-22 16:35:41 +0200680 /*! The dummy packet will not be sent via the actual OSMUX connection,
681 * instead it is sent out of band to port where the remote OSMUX
682 * multplexer is listening. The goal is to ensure that the connection
683 * is kept open */
684
685 /*! We don't need to send the dummy load for osmux so often as another
686 * endpoint may have already punched the hole in the firewall. This
687 * approach is simple though. */
688
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200689 buf[0] = MGCP_DUMMY_LOAD;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200690 memcpy(&buf[1], &conn->osmux.cid, sizeof(conn->osmux.cid));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200691
692 /* Wait until we have the connection information from MDCX */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200693 if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200694 return 0;
695
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200696 if (endp_osmux_state_check(endp, conn, true) < 0)
697 return 0;
698
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200699 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG,
700 "sending OSMUX dummy load to %s CID %u\n",
701 inet_ntoa(conn->end.addr), conn->osmux.cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200702
Philipp Maier87bd9be2017-08-22 16:35:41 +0200703 return mgcp_udp_send(osmux_fd.fd, &conn->end.addr,
Pau Espin Pedrol295570c2019-05-13 17:32:47 +0200704 conn->end.rtp_port, buf, sizeof(buf));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200705}
706
Pau Espin Pedrolbcd52e52018-09-17 12:41:28 +0200707/* bsc-nat allocates/releases the Osmux circuit ID. +7 to round up to 8 bit boundary. */
708static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1 + 7) / 8];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200709
Philipp Maier87bd9be2017-08-22 16:35:41 +0200710/*! count the number of taken OSMUX cids.
711 * \returns number of OSMUX cids in use */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200712int osmux_cid_pool_count_used(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200713{
714 int i, j, used = 0;
715
716 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
717 for (j = 0; j < 8; j++) {
718 if (osmux_cid_bitmap[i] & (1 << j))
719 used += 1;
720 }
721 }
722
723 return used;
724}
725
Philipp Maier87bd9be2017-08-22 16:35:41 +0200726/*! take a free OSMUX cid.
727 * \returns OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200728int osmux_cid_pool_get_next(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200729{
730 int i, j;
731
732 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
733 for (j = 0; j < 8; j++) {
734 if (osmux_cid_bitmap[i] & (1 << j))
735 continue;
736
737 osmux_cid_bitmap[i] |= (1 << j);
738 LOGP(DLMGCP, LOGL_DEBUG,
739 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
740 return (i * 8) + j;
741 }
742 }
743
744 LOGP(DLMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n");
745 return -1;
746}
747
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200748/*! take a specific OSMUX cid.
749 * \param[in] osmux_cid OSMUX cid */
750void osmux_cid_pool_get(uint8_t osmux_cid)
751{
752 LOGP(DLMGCP, LOGL_DEBUG, "Allocating Osmux CID %u from pool\n", osmux_cid);
753 osmux_cid_bitmap[osmux_cid / 8] |= (1 << (osmux_cid % 8));
754}
755
Philipp Maier87bd9be2017-08-22 16:35:41 +0200756/*! put back a no longer used OSMUX cid.
757 * \param[in] osmux_cid OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200758void osmux_cid_pool_put(uint8_t osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200759{
760 LOGP(DLMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
761 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
762}
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200763
764/*! check if OSMUX cid is already taken */
765bool osmux_cid_pool_allocated(uint8_t osmux_cid)
766{
767 return !!(osmux_cid_bitmap[osmux_cid / 8] & (1 << (osmux_cid % 8)));
768}