blob: 442319472d9c86082fb30d7a3d3c2205664448a2 [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,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200328 "Could not enable osmux for conn on %s: %s\n",
329 sending ? "sent" : "received",
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200330 mgcp_conn_dump(conn->conn));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200331 return -1;
332 }
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200333 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200334 "Osmux %s CID %u towards %s:%u is now enabled\n",
335 sending ? "sent" : "received",
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200336 conn->osmux.cid, inet_ntoa(conn->end.addr),
Pau Espin Pedrol295570c2019-05-13 17:32:47 +0200337 ntohs(conn->end.rtp_port));
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200338 return 0;
339 case OSMUX_STATE_ENABLED:
340 return 0;
341 default:
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200342 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
343 "Osmux %s in conn %s without full negotiation, state %d\n",
344 sending ? "sent" : "received",
345 mgcp_conn_dump(conn->conn), conn->osmux.state);
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200346 return -1;
347 }
348}
349
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200350static int osmux_legacy_dummy_parse_cid(struct sockaddr_in *addr, struct msgb *msg,
351 uint8_t *osmux_cid)
352{
Pau Espin Pedrolb5583cd2019-05-13 12:58:24 +0200353 if (msg->len < 1 + sizeof(*osmux_cid)) {
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200354 LOGP(DLMGCP, LOGL_ERROR,
Pau Espin Pedrolb5583cd2019-05-13 12:58:24 +0200355 "Discarding truncated Osmux dummy load: %s\n", osmo_hexdump(msg->data, msg->len));
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200356 return -1;
357 }
358
359 /* extract the osmux CID from the dummy message */
360 memcpy(osmux_cid, &msg->data[1], sizeof(*osmux_cid));
361 return 0;
362}
363
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200364#define osmux_chunk_length(msg, rem) (rem - msg->len);
365
366int osmux_read_from_bsc_nat_cb(struct osmo_fd *ofd, unsigned int what)
367{
368 struct msgb *msg;
369 struct osmux_hdr *osmuxh;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200370 struct sockaddr_in addr;
371 struct mgcp_config *cfg = ofd->data;
372 uint32_t rem;
Pau Espin Pedrol42199042018-05-17 14:26:02 +0200373 struct mgcp_conn_rtp *conn_bts = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200374
375 msg = osmux_recv(ofd, &addr);
376 if (!msg)
377 return -1;
378
Pau Espin Pedrold14163e2018-10-16 15:48:59 +0200379 if (!cfg->osmux) {
380 LOGP(DLMGCP, LOGL_ERROR,
381 "bsc-nat wants to use Osmux but bsc did not request it\n");
382 goto out;
383 }
384
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200385 /* not any further processing dummy messages */
386 if (msg->data[0] == MGCP_DUMMY_LOAD)
387 goto out;
388
389 rem = msg->len;
390 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
391 struct mgcp_endpoint *endp;
392
393 /* Yes, we use MGCP_DEST_NET to locate the origin */
394 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
395 &addr.sin_addr, MGCP_DEST_NET);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200396
397 /* FIXME: Get rid of CONN_ID_XXX! */
Pau Espin Pedrol42199042018-05-17 14:26:02 +0200398 conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS);
399 if (!conn_bts)
Pau Espin Pedrolff6606c2018-10-16 16:45:40 +0200400 continue;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200401
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200402 if (!endp) {
403 LOGP(DLMGCP, LOGL_ERROR,
404 "Cannot find an endpoint for circuit_id=%d\n",
405 osmuxh->circuit_id);
406 goto out;
407 }
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200408 if (endp_osmux_state_check(endp, conn_bts, false) == 0) {
409 conn_bts->osmux.stats.octets += osmux_chunk_length(msg, rem);
410 conn_bts->osmux.stats.chunks++;
411 osmux_xfrm_output_sched(&conn_bts->osmux.out, osmuxh);
412 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200413 rem = msg->len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200414 }
415out:
416 msgb_free(msg);
417 return 0;
418}
419
420/* This is called from the bsc-nat */
421static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr,
Pau Espin Pedrol852ba862018-10-16 15:55:56 +0200422 struct msgb *msg, int endp_type)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200423{
424 struct mgcp_endpoint *endp;
425 uint8_t osmux_cid;
Pau Espin Pedrol852ba862018-10-16 15:55:56 +0200426 struct mgcp_conn_rtp *conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200427
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200428 if (osmux_legacy_dummy_parse_cid(addr, msg, &osmux_cid) < 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200429 goto out;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200430
Pau Espin Pedrol852ba862018-10-16 15:55:56 +0200431 endp = endpoint_lookup(cfg, osmux_cid, &addr->sin_addr, endp_type);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200432 if (!endp) {
433 LOGP(DLMGCP, LOGL_ERROR,
434 "Cannot find endpoint for Osmux CID %d\n", osmux_cid);
435 goto out;
436 }
437
Pau Espin Pedrol852ba862018-10-16 15:55:56 +0200438 /* FIXME: Get rid of CONN_ID_XXX! */
439 conn = mgcp_conn_get_rtp(endp, endp_type == MGCP_DEST_BTS ? CONN_ID_NET : CONN_ID_BTS);
440 if (!conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200441 goto out;
442
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200443 endp_osmux_state_check(endp, conn, false);
444 /* Only needed to punch hole in firewall, it can be dropped */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200445out:
446 msgb_free(msg);
447 return 0;
448}
449
450int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what)
451{
452 struct msgb *msg;
453 struct osmux_hdr *osmuxh;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200454 struct sockaddr_in addr;
455 struct mgcp_config *cfg = ofd->data;
456 uint32_t rem;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200457 struct mgcp_conn_rtp *conn_net = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200458
459 msg = osmux_recv(ofd, &addr);
460 if (!msg)
461 return -1;
462
Pau Espin Pedrold14163e2018-10-16 15:48:59 +0200463 if (!cfg->osmux) {
464 LOGP(DLMGCP, LOGL_ERROR,
465 "bsc wants to use Osmux but bsc-nat did not request it\n");
466 goto out;
467 }
468
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200469 /* not any further processing dummy messages */
470 if (msg->data[0] == MGCP_DUMMY_LOAD)
Pau Espin Pedrol852ba862018-10-16 15:55:56 +0200471 return osmux_handle_dummy(cfg, &addr, msg, MGCP_DEST_BTS);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200472
473 rem = msg->len;
474 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
475 struct mgcp_endpoint *endp;
476
477 /* Yes, we use MGCP_DEST_BTS to locate the origin */
478 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
479 &addr.sin_addr, MGCP_DEST_BTS);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200480
481 /* FIXME: Get rid of CONN_ID_XXX! */
482 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
483 if (!conn_net)
Pau Espin Pedrolff6606c2018-10-16 16:45:40 +0200484 continue;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200485
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200486 if (!endp) {
487 LOGP(DLMGCP, LOGL_ERROR,
488 "Cannot find an endpoint for circuit_id=%d\n",
489 osmuxh->circuit_id);
490 goto out;
491 }
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200492 if (endp_osmux_state_check(endp, conn_net, false) == 0) {
493 conn_net->osmux.stats.octets += osmux_chunk_length(msg, rem);
494 conn_net->osmux.stats.chunks++;
495 osmux_xfrm_output_sched(&conn_net->osmux.out, osmuxh);
496 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200497 rem = msg->len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200498 }
499out:
500 msgb_free(msg);
501 return 0;
502}
503
504int osmux_init(int role, struct mgcp_config *cfg)
505{
506 int ret;
507
508 switch(role) {
509 case OSMUX_ROLE_BSC:
510 osmux_fd.cb = osmux_read_from_bsc_nat_cb;
511 break;
512 case OSMUX_ROLE_BSC_NAT:
513 osmux_fd.cb = osmux_read_from_bsc_cb;
514 break;
515 default:
516 LOGP(DLMGCP, LOGL_ERROR, "wrong role for OSMUX\n");
517 return -1;
518 }
519 osmux_fd.data = cfg;
520
521 ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port);
522 if (ret < 0) {
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200523 LOGP(DLMGCP, LOGL_ERROR, "cannot bind OSMUX socket to %s:%u\n",
524 cfg->osmux_addr, cfg->osmux_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200525 return ret;
526 }
527 mgcp_set_ip_tos(osmux_fd.fd, cfg->endp_dscp);
528 osmux_fd.when |= BSC_FD_READ;
529
530 ret = osmo_fd_register(&osmux_fd);
531 if (ret < 0) {
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200532 LOGP(DLMGCP, LOGL_ERROR, "cannot register OSMUX socket %s\n",
533 osmo_sock_get_name2(osmux_fd.fd));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200534 return ret;
535 }
536 cfg->osmux_init = 1;
537
Pau Espin Pedrolf027f172019-05-06 13:57:31 +0200538 LOGP(DLMGCP, LOGL_INFO, "OSMUX socket listening on %s\n",
539 osmo_sock_get_name2(osmux_fd.fd));
540
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200541 return 0;
542}
543
Philipp Maier87bd9be2017-08-22 16:35:41 +0200544/*! enable OSXMUX circuit for a specified connection.
545 * \param[in] endp mgcp endpoint (configuration)
546 * \param[in] conn connection to disable
547 * \param[in] addr IP address of remote OSMUX endpoint
Pau Espin Pedrol12056862019-05-13 17:30:21 +0200548 * \param[in] port portnumber of the remote OSMUX endpoint (in network byte order)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200549 * \returns 0 on success, -1 on ERROR */
550int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
551 struct in_addr *addr, uint16_t port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200552{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200553 /*! If osmux is enabled, initialize the output handler. This handler is
554 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
555 * allocated based on the circuit ID (conn_net->osmux.cid), which is unique
556 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200557 * SSRC space (2^32) by the OSMUX_CID_MAX + 1 possible circuit IDs, then randomly
Philipp Maier87bd9be2017-08-22 16:35:41 +0200558 * select one value from that window. Thus, we have no chance to have
559 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
560 * similarly, for flows traveling to the MSC.
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200561 */
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200562 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / (OSMUX_CID_MAX + 1);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200563 uint16_t osmux_dummy = endp->cfg->osmux_dummy;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200564
Philipp Maier87bd9be2017-08-22 16:35:41 +0200565 /* Check if osmux is enabled for the specified connection */
Pau Espin Pedrol48aff622018-10-16 16:03:57 +0200566 if (conn->osmux.state != OSMUX_STATE_ACTIVATING) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200567 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
568 "conn:%s didn't negotiate Osmux, state %d\n",
569 mgcp_conn_dump(conn->conn), conn->osmux.state);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200570 return -1;
571 }
572
Philipp Maier87bd9be2017-08-22 16:35:41 +0200573 conn->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
574 if (!conn->osmux.in) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200575 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
576 "Cannot allocate input osmux handle for conn:%s\n",
577 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200578 return -1;
579 }
Pau Espin Pedrolac772d82019-05-06 18:02:00 +0200580 if (osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.cid, osmux_dummy) < 0) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200581 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
582 "Cannot open osmux circuit %u for conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200583 conn->osmux.cid, mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200584 return -1;
585 }
586
Pau Espin Pedrol426a9d92018-10-16 15:31:45 +0200587 osmux_xfrm_output_init(&conn->osmux.out,
588 (conn->osmux.cid * rtp_ssrc_winlen) +
589 (random() % rtp_ssrc_winlen));
590
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200591 switch (endp->cfg->role) {
592 case MGCP_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200593 conn->type = MGCP_OSMUX_BSC_NAT;
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200594 osmux_xfrm_output_set_tx_cb(&conn->osmux.out,
595 scheduled_tx_net_cb, endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200596 break;
597 case MGCP_BSC:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200598 conn->type = MGCP_OSMUX_BSC;
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200599 osmux_xfrm_output_set_tx_cb(&conn->osmux.out,
600 scheduled_tx_bts_cb, endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200601 break;
602 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200603
604 conn->osmux.state = OSMUX_STATE_ENABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200605
606 return 0;
607}
608
Philipp Maier87bd9be2017-08-22 16:35:41 +0200609/*! disable OSXMUX circuit for a specified connection.
610 * \param[in] conn connection to disable */
611void osmux_disable_conn(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200612{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200613 if (!conn)
614 return;
615
616 if (conn->osmux.state != OSMUX_STATE_ENABLED)
617 return;
618
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200619 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO,
620 "Releasing connection %s using Osmux CID %u\n",
621 conn->conn->id, conn->osmux.cid);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200622
623 /* We are closing, we don't need pending RTP packets to be transmitted */
624 osmux_xfrm_output_set_tx_cb(&conn->osmux.out, NULL, NULL);
625 osmux_xfrm_output_flush(&conn->osmux.out);
626
Philipp Maier87bd9be2017-08-22 16:35:41 +0200627 osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.cid);
628 conn->osmux.state = OSMUX_STATE_DISABLED;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200629 conn_osmux_release_cid(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200630 osmux_handle_put(conn->osmux.in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200631}
632
Philipp Maier87bd9be2017-08-22 16:35:41 +0200633/*! relase OSXMUX cid, that had been allocated to this connection.
634 * \param[in] conn connection with OSMUX cid to release */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200635void conn_osmux_release_cid(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200636{
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200637 if (conn->osmux.cid_allocated)
638 osmux_cid_pool_put(conn->osmux.cid);
639 conn->osmux.cid = 0;
640 conn->osmux.cid_allocated = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200641}
642
Philipp Maier87bd9be2017-08-22 16:35:41 +0200643/*! allocate OSXMUX cid to connection.
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200644 * \param[in] conn connection for which we allocate the OSMUX cid
645 * \param[in] osmux_cid OSMUX cid to allocate. -1 Means take next available one.
646 * \returns Allocated OSMUX cid, -1 on error (no free cids avail, or selected one is already taken).
647 */
648int conn_osmux_allocate_cid(struct mgcp_conn_rtp *conn, int osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200649{
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200650 if (osmux_cid != -1 && osmux_cid_pool_allocated((uint8_t) osmux_cid)) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200651 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO,
652 "Osmux CID %d already allocated!\n",
653 osmux_cid);
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200654 return -1;
655 }
656
657 if (osmux_cid == -1) {
658 osmux_cid = osmux_cid_pool_get_next();
659 if (osmux_cid == -1) {
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200660 LOGPCONN(conn->conn, DLMGCP, LOGL_INFO,
661 "no available Osmux CID to allocate!\n");
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200662 return -1;
663 }
664 } else
665 osmux_cid_pool_get(osmux_cid);
666
667 conn->osmux.cid = (uint8_t) osmux_cid;
668 conn->osmux.cid_allocated = true;
Pau Espin Pedrolfa810e82019-05-06 18:54:10 +0200669 conn->type = MGCP_OSMUX_BSC;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200670 return osmux_cid;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200671}
672
Philipp Maier87bd9be2017-08-22 16:35:41 +0200673/*! send RTP dummy packet to OSMUX connection port.
674 * \param[in] endp mcgp endpoint that holds the RTP connection
675 * \param[in] conn associated RTP connection
676 * \returns bytes sent, -1 on error */
677int osmux_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200678{
679 char buf[1 + sizeof(uint8_t)];
680 struct in_addr addr_unset = {};
681
Philipp Maier87bd9be2017-08-22 16:35:41 +0200682 /*! The dummy packet will not be sent via the actual OSMUX connection,
683 * instead it is sent out of band to port where the remote OSMUX
684 * multplexer is listening. The goal is to ensure that the connection
685 * is kept open */
686
687 /*! We don't need to send the dummy load for osmux so often as another
688 * endpoint may have already punched the hole in the firewall. This
689 * approach is simple though. */
690
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200691 buf[0] = MGCP_DUMMY_LOAD;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200692 memcpy(&buf[1], &conn->osmux.cid, sizeof(conn->osmux.cid));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200693
694 /* Wait until we have the connection information from MDCX */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200695 if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200696 return 0;
697
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200698 if (endp_osmux_state_check(endp, conn, true) < 0)
699 return 0;
700
Pau Espin Pedrolc9a62802019-05-13 13:20:13 +0200701 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG,
Pau Espin Pedrol37525222019-05-13 17:34:14 +0200702 "sending OSMUX dummy load to %s:%u CID %u\n",
703 inet_ntoa(conn->end.addr), ntohs(conn->end.rtp_port), conn->osmux.cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200704
Philipp Maier87bd9be2017-08-22 16:35:41 +0200705 return mgcp_udp_send(osmux_fd.fd, &conn->end.addr,
Pau Espin Pedrol295570c2019-05-13 17:32:47 +0200706 conn->end.rtp_port, buf, sizeof(buf));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200707}
708
Pau Espin Pedrolbcd52e52018-09-17 12:41:28 +0200709/* bsc-nat allocates/releases the Osmux circuit ID. +7 to round up to 8 bit boundary. */
710static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1 + 7) / 8];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200711
Philipp Maier87bd9be2017-08-22 16:35:41 +0200712/*! count the number of taken OSMUX cids.
713 * \returns number of OSMUX cids in use */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200714int osmux_cid_pool_count_used(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200715{
716 int i, j, used = 0;
717
718 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
719 for (j = 0; j < 8; j++) {
720 if (osmux_cid_bitmap[i] & (1 << j))
721 used += 1;
722 }
723 }
724
725 return used;
726}
727
Philipp Maier87bd9be2017-08-22 16:35:41 +0200728/*! take a free OSMUX cid.
729 * \returns OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200730int osmux_cid_pool_get_next(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200731{
732 int i, j;
733
734 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
735 for (j = 0; j < 8; j++) {
736 if (osmux_cid_bitmap[i] & (1 << j))
737 continue;
738
739 osmux_cid_bitmap[i] |= (1 << j);
740 LOGP(DLMGCP, LOGL_DEBUG,
741 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
742 return (i * 8) + j;
743 }
744 }
745
746 LOGP(DLMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n");
747 return -1;
748}
749
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200750/*! take a specific OSMUX cid.
751 * \param[in] osmux_cid OSMUX cid */
752void osmux_cid_pool_get(uint8_t osmux_cid)
753{
754 LOGP(DLMGCP, LOGL_DEBUG, "Allocating Osmux CID %u from pool\n", osmux_cid);
755 osmux_cid_bitmap[osmux_cid / 8] |= (1 << (osmux_cid % 8));
756}
757
Philipp Maier87bd9be2017-08-22 16:35:41 +0200758/*! put back a no longer used OSMUX cid.
759 * \param[in] osmux_cid OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200760void osmux_cid_pool_put(uint8_t osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200761{
762 LOGP(DLMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
763 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
764}
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200765
766/*! check if OSMUX cid is already taken */
767bool osmux_cid_pool_allocated(uint8_t osmux_cid)
768{
769 return !!(osmux_cid_bitmap[osmux_cid / 8] & (1 << (osmux_cid % 8)));
770}