blob: 3bd765e9c9f8346b75015376539f1caff505e27e [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>
18#include <osmocom/core/talloc.h>
19
20#include <osmocom/netif/osmux.h>
21#include <osmocom/netif/rtp.h>
22
Philipp Maier87bd9be2017-08-22 16:35:41 +020023#include <osmocom/mgcp/mgcp.h>
24#include <osmocom/mgcp/mgcp_internal.h>
25#include <osmocom/mgcp/osmux.h>
26#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010027#include <osmocom/mgcp/mgcp_endp.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020028
29static struct osmo_fd osmux_fd;
30
31static LLIST_HEAD(osmux_handle_list);
32
33struct osmux_handle {
34 struct llist_head head;
35 struct osmux_in_handle *in;
36 struct in_addr rem_addr;
37 int rem_port;
38 int refcnt;
39};
40
41static void *osmux;
42
Philipp Maier87bd9be2017-08-22 16:35:41 +020043/* Deliver OSMUX batch to the remote end */
44static void osmux_deliver_cb(struct msgb *batch_msg, void *data)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020045{
46 struct osmux_handle *handle = data;
47 struct sockaddr_in out = {
48 .sin_family = AF_INET,
49 .sin_port = handle->rem_port,
50 };
51
52 memcpy(&out.sin_addr, &handle->rem_addr, sizeof(handle->rem_addr));
53 sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0,
54 (struct sockaddr *)&out, sizeof(out));
55 msgb_free(batch_msg);
56}
57
Philipp Maier87bd9be2017-08-22 16:35:41 +020058/* Lookup existing OSMUX handle for specified destination address. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020059static struct osmux_handle *
60osmux_handle_find_get(struct in_addr *addr, int rem_port)
61{
62 struct osmux_handle *h;
63
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020064 llist_for_each_entry(h, &osmux_handle_list, head) {
65 if (memcmp(&h->rem_addr, addr, sizeof(struct in_addr)) == 0 &&
66 h->rem_port == rem_port) {
67 LOGP(DLMGCP, LOGL_DEBUG, "using existing OSMUX handle "
68 "for addr=%s:%d\n",
69 inet_ntoa(*addr), ntohs(rem_port));
70 h->refcnt++;
71 return h;
72 }
73 }
74
75 return NULL;
76}
77
Philipp Maier87bd9be2017-08-22 16:35:41 +020078/* Put down no longer needed OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020079static void osmux_handle_put(struct osmux_in_handle *in)
80{
81 struct osmux_handle *h;
82
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020083 llist_for_each_entry(h, &osmux_handle_list, head) {
84 if (h->in == in) {
85 if (--h->refcnt == 0) {
86 LOGP(DLMGCP, LOGL_INFO,
87 "Releasing unused osmux handle for %s:%d\n",
88 inet_ntoa(h->rem_addr),
89 ntohs(h->rem_port));
90 LOGP(DLMGCP, LOGL_INFO, "Stats: "
91 "input RTP msgs: %u bytes: %"PRIu64" "
92 "output osmux msgs: %u bytes: %"PRIu64"\n",
93 in->stats.input_rtp_msgs,
94 in->stats.input_rtp_bytes,
95 in->stats.output_osmux_msgs,
96 in->stats.output_osmux_bytes);
97 llist_del(&h->head);
98 osmux_xfrm_input_fini(h->in);
99 talloc_free(h);
100 }
101 return;
102 }
103 }
104 LOGP(DLMGCP, LOGL_ERROR, "cannot find Osmux input handle %p\n", in);
105}
106
Philipp Maier87bd9be2017-08-22 16:35:41 +0200107/* Allocate free OSMUX handle */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200108static struct osmux_handle *
109osmux_handle_alloc(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
110{
111 struct osmux_handle *h;
112
113 h = talloc_zero(osmux, struct osmux_handle);
114 if (!h)
115 return NULL;
116 h->rem_addr = *addr;
117 h->rem_port = rem_port;
118 h->refcnt++;
119
120 h->in = talloc_zero(h, struct osmux_in_handle);
121 if (!h->in) {
122 talloc_free(h);
123 return NULL;
124 }
125
Philipp Maier87bd9be2017-08-22 16:35:41 +0200126 /* sequence number to start OSMUX message from */
127 h->in->osmux_seq = 0;
128
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200129 h->in->batch_factor = cfg->osmux_batch;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200130
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200131 /* If batch size is zero, the library defaults to 1470 bytes. */
132 h->in->batch_size = cfg->osmux_batch_size;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200133 h->in->deliver = osmux_deliver_cb;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200134 osmux_xfrm_input_init(h->in);
135 h->in->data = h;
136
137 llist_add(&h->head, &osmux_handle_list);
138
139 LOGP(DLMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s:%d\n",
140 inet_ntoa(*addr), ntohs(rem_port));
141
142 return h;
143}
144
Philipp Maier87bd9be2017-08-22 16:35:41 +0200145/* Lookup existing handle for a specified address, if the handle can not be
Harald Welte1d1b98f2017-12-25 10:03:40 +0100146 * found, the function will automatically allocate one */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200147static struct osmux_in_handle *
148osmux_handle_lookup(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
149{
150 struct osmux_handle *h;
151
152 h = osmux_handle_find_get(addr, rem_port);
153 if (h != NULL)
154 return h->in;
155
156 h = osmux_handle_alloc(cfg, addr, rem_port);
157 if (h == NULL)
158 return NULL;
159
160 return h->in;
161}
162
Philipp Maier87bd9be2017-08-22 16:35:41 +0200163/*! send RTP packet through OSMUX connection.
164 * \param[in] buf rtp data
165 * \param[in] buf_len length of rtp data
166 * \param[in] conn associated RTP connection
167 * \returns 0 on success, -1 on ERROR */
168int osmux_xfrm_to_osmux(char *buf, int buf_len, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200169{
170 int ret;
171 struct msgb *msg;
172
173 msg = msgb_alloc(4096, "RTP");
174 if (!msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200175 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200176
Philipp Maier87bd9be2017-08-22 16:35:41 +0200177 memcpy(msg->data, buf, buf_len);
178 msgb_put(msg, buf_len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200179
Philipp Maier87bd9be2017-08-22 16:35:41 +0200180 while ((ret = osmux_xfrm_input(conn->osmux.in, msg, conn->osmux.cid)) > 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200181 /* batch full, build and deliver it */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200182 osmux_xfrm_input_deliver(conn->osmux.in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200183 }
184 return 0;
185}
186
Philipp Maier87bd9be2017-08-22 16:35:41 +0200187/* Lookup the endpoint that corresponds to the specified address (port) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200188static struct mgcp_endpoint *
189endpoint_lookup(struct mgcp_config *cfg, int cid,
190 struct in_addr *from_addr, int type)
191{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200192 struct mgcp_endpoint *endp = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200193 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200194 struct mgcp_conn_rtp *conn_net = NULL;
195 struct mgcp_conn_rtp *conn_bts = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200196
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200197 for (i=0; i<cfg->trunk.number_endpoints; i++) {
198 struct in_addr *this;
199
Philipp Maier87bd9be2017-08-22 16:35:41 +0200200 endp = &cfg->trunk.endpoints[i];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200201
Philipp Maier87bd9be2017-08-22 16:35:41 +0200202#if 0
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200203 if (!tmp->allocated)
204 continue;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200205#endif
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200206
207 switch(type) {
208 case MGCP_DEST_NET:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200209 /* FIXME: Get rid of CONN_ID_XXX! */
210 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
Philipp Maierddf1f9d2017-11-07 12:18:38 +0100211 if (conn_net)
212 this = &conn_net->end.addr;
213 else
214 this = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200215 break;
216 case MGCP_DEST_BTS:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200217 /* FIXME: Get rid of CONN_ID_XXX! */
218 conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS);
Philipp Maierddf1f9d2017-11-07 12:18:38 +0100219 if (conn_bts)
220 this = &conn_bts->end.addr;
221 else
222 this = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200223 break;
224 default:
225 /* Should not ever happen */
226 LOGP(DLMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type);
227 return NULL;
228 }
229
Philipp Maier87bd9be2017-08-22 16:35:41 +0200230 /* FIXME: Get rid of CONN_ID_XXX! */
231 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
Philipp Maierddf1f9d2017-11-07 12:18:38 +0100232 if (conn_net && this && conn_net->osmux.cid == cid
233 && this->s_addr == from_addr->s_addr)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200234 return endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200235 }
236
237 LOGP(DLMGCP, LOGL_ERROR, "Cannot find endpoint with cid=%d\n", cid);
238
239 return NULL;
240}
241
242static void scheduled_tx_net_cb(struct msgb *msg, void *data)
243{
244 struct mgcp_endpoint *endp = data;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200245 struct mgcp_conn_rtp *conn_net = NULL;
246 struct mgcp_conn_rtp *conn_bts = NULL;
247
248 /* FIXME: Get rid of CONN_ID_XXX! */
249 conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS);
250 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
251 if (!conn_bts || !conn_net)
252 return;
253
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200254 struct sockaddr_in addr = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200255 .sin_addr = conn_net->end.addr,
256 .sin_port = conn_net->end.rtp_port,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200257 };
258
Philipp Maiercede2a42018-07-03 14:14:21 +0200259 rate_ctr_inc(&conn_bts->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
260 rate_ctr_add(&conn_bts->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], msg->len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200261
Philipp Maier87bd9be2017-08-22 16:35:41 +0200262 /* Send RTP data to NET */
263 /* FIXME: Get rid of conn_bts and conn_net! */
264 mgcp_send(endp, 1, &addr, (char *)msg->data, msg->len,
265 conn_bts, conn_net);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200266 msgb_free(msg);
267}
268
269static void scheduled_tx_bts_cb(struct msgb *msg, void *data)
270{
271 struct mgcp_endpoint *endp = data;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200272 struct mgcp_conn_rtp *conn_net = NULL;
273 struct mgcp_conn_rtp *conn_bts = NULL;
274
275 /* FIXME: Get rid of CONN_ID_XXX! */
276 conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS);
277 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
278 if (!conn_bts || !conn_net)
279 return;
280
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200281 struct sockaddr_in addr = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200282 .sin_addr = conn_bts->end.addr,
283 .sin_port = conn_bts->end.rtp_port,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200284 };
285
Philipp Maiercede2a42018-07-03 14:14:21 +0200286 rate_ctr_inc(&conn_net->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
Pau Espin Pedrol426a9d92018-10-16 15:31:45 +0200287 rate_ctr_add(&conn_net->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], msg->len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200288
Philipp Maier87bd9be2017-08-22 16:35:41 +0200289 /* Send RTP data to BTS */
290 /* FIXME: Get rid of conn_bts and conn_net! */
291 mgcp_send(endp, 1, &addr, (char *)msg->data, msg->len,
292 conn_net, conn_bts);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200293 msgb_free(msg);
294}
295
296static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr)
297{
298 struct msgb *msg;
299 socklen_t slen = sizeof(*addr);
300 int ret;
301
302 msg = msgb_alloc(4096, "OSMUX");
303 if (!msg) {
304 LOGP(DLMGCP, LOGL_ERROR, "cannot allocate message\n");
305 return NULL;
306 }
307 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0,
308 (struct sockaddr *)addr, &slen);
309 if (ret <= 0) {
310 msgb_free(msg);
311 LOGP(DLMGCP, LOGL_ERROR, "cannot receive message\n");
312 return NULL;
313 }
314 msgb_put(msg, ret);
315
316 return msg;
317}
318
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200319/* Updates endp osmux state and returns 0 if it can process messages, -1 otherwise */
320static int endp_osmux_state_check(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
321 bool sending)
322{
323 switch(conn->osmux.state) {
324 case OSMUX_STATE_ACTIVATING:
325 if (osmux_enable_conn(endp, conn, &conn->end.addr, htons(endp->cfg->osmux_port)) < 0) {
326 LOGP(DLMGCP, LOGL_ERROR,
327 "Could not enable osmux for conn:%s\n",
328 mgcp_conn_dump(conn->conn));
329 return -1;
330 }
331 LOGP(DLMGCP, LOGL_ERROR,
332 "Osmux CID %u for %s:%u is now enabled\n",
333 conn->osmux.cid, inet_ntoa(conn->end.addr),
334 endp->cfg->osmux_port);
335 return 0;
336 case OSMUX_STATE_ENABLED:
337 return 0;
338 default:
339 LOGP(DLMGCP, LOGL_ERROR,
340 "Osmux %s in conn %s without full negotiation, state %d\n",
341 sending ? "sent" : "received",
342 mgcp_conn_dump(conn->conn), conn->osmux.state);
343 return -1;
344 }
345}
346
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200347static int osmux_legacy_dummy_parse_cid(struct sockaddr_in *addr, struct msgb *msg,
348 uint8_t *osmux_cid)
349{
350 if (msg->len < 1 + sizeof(osmux_cid)) {
351 LOGP(DLMGCP, LOGL_ERROR,
352 "Discarding truncated Osmux dummy load\n");
353 return -1;
354 }
355
356 /* extract the osmux CID from the dummy message */
357 memcpy(osmux_cid, &msg->data[1], sizeof(*osmux_cid));
358 return 0;
359}
360
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200361#define osmux_chunk_length(msg, rem) (rem - msg->len);
362
363int osmux_read_from_bsc_nat_cb(struct osmo_fd *ofd, unsigned int what)
364{
365 struct msgb *msg;
366 struct osmux_hdr *osmuxh;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200367 struct sockaddr_in addr;
368 struct mgcp_config *cfg = ofd->data;
369 uint32_t rem;
Pau Espin Pedrol42199042018-05-17 14:26:02 +0200370 struct mgcp_conn_rtp *conn_bts = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200371
372 msg = osmux_recv(ofd, &addr);
373 if (!msg)
374 return -1;
375
Pau Espin Pedrold14163e2018-10-16 15:48:59 +0200376 if (!cfg->osmux) {
377 LOGP(DLMGCP, LOGL_ERROR,
378 "bsc-nat wants to use Osmux but bsc did not request it\n");
379 goto out;
380 }
381
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200382 /* not any further processing dummy messages */
383 if (msg->data[0] == MGCP_DUMMY_LOAD)
384 goto out;
385
386 rem = msg->len;
387 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
388 struct mgcp_endpoint *endp;
389
390 /* Yes, we use MGCP_DEST_NET to locate the origin */
391 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
392 &addr.sin_addr, MGCP_DEST_NET);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200393
394 /* FIXME: Get rid of CONN_ID_XXX! */
Pau Espin Pedrol42199042018-05-17 14:26:02 +0200395 conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS);
396 if (!conn_bts)
Pau Espin Pedrolff6606c2018-10-16 16:45:40 +0200397 continue;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200398
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200399 if (!endp) {
400 LOGP(DLMGCP, LOGL_ERROR,
401 "Cannot find an endpoint for circuit_id=%d\n",
402 osmuxh->circuit_id);
403 goto out;
404 }
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200405 if (endp_osmux_state_check(endp, conn_bts, false) == 0) {
406 conn_bts->osmux.stats.octets += osmux_chunk_length(msg, rem);
407 conn_bts->osmux.stats.chunks++;
408 osmux_xfrm_output_sched(&conn_bts->osmux.out, osmuxh);
409 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200410 rem = msg->len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200411 }
412out:
413 msgb_free(msg);
414 return 0;
415}
416
417/* This is called from the bsc-nat */
418static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr,
Pau Espin Pedrol852ba862018-10-16 15:55:56 +0200419 struct msgb *msg, int endp_type)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200420{
421 struct mgcp_endpoint *endp;
422 uint8_t osmux_cid;
Pau Espin Pedrol852ba862018-10-16 15:55:56 +0200423 struct mgcp_conn_rtp *conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200424
Pau Espin Pedrol662fa422018-10-16 15:54:38 +0200425 if (osmux_legacy_dummy_parse_cid(addr, msg, &osmux_cid) < 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200426 goto out;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200427
Pau Espin Pedrol852ba862018-10-16 15:55:56 +0200428 endp = endpoint_lookup(cfg, osmux_cid, &addr->sin_addr, endp_type);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200429 if (!endp) {
430 LOGP(DLMGCP, LOGL_ERROR,
431 "Cannot find endpoint for Osmux CID %d\n", osmux_cid);
432 goto out;
433 }
434
Pau Espin Pedrol852ba862018-10-16 15:55:56 +0200435 /* FIXME: Get rid of CONN_ID_XXX! */
436 conn = mgcp_conn_get_rtp(endp, endp_type == MGCP_DEST_BTS ? CONN_ID_NET : CONN_ID_BTS);
437 if (!conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200438 goto out;
439
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200440 endp_osmux_state_check(endp, conn, false);
441 /* Only needed to punch hole in firewall, it can be dropped */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200442out:
443 msgb_free(msg);
444 return 0;
445}
446
447int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what)
448{
449 struct msgb *msg;
450 struct osmux_hdr *osmuxh;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200451 struct sockaddr_in addr;
452 struct mgcp_config *cfg = ofd->data;
453 uint32_t rem;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200454 struct mgcp_conn_rtp *conn_net = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200455
456 msg = osmux_recv(ofd, &addr);
457 if (!msg)
458 return -1;
459
Pau Espin Pedrold14163e2018-10-16 15:48:59 +0200460 if (!cfg->osmux) {
461 LOGP(DLMGCP, LOGL_ERROR,
462 "bsc wants to use Osmux but bsc-nat did not request it\n");
463 goto out;
464 }
465
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200466 /* not any further processing dummy messages */
467 if (msg->data[0] == MGCP_DUMMY_LOAD)
Pau Espin Pedrol852ba862018-10-16 15:55:56 +0200468 return osmux_handle_dummy(cfg, &addr, msg, MGCP_DEST_BTS);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200469
470 rem = msg->len;
471 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
472 struct mgcp_endpoint *endp;
473
474 /* Yes, we use MGCP_DEST_BTS to locate the origin */
475 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
476 &addr.sin_addr, MGCP_DEST_BTS);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200477
478 /* FIXME: Get rid of CONN_ID_XXX! */
479 conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
480 if (!conn_net)
Pau Espin Pedrolff6606c2018-10-16 16:45:40 +0200481 continue;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200482
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200483 if (!endp) {
484 LOGP(DLMGCP, LOGL_ERROR,
485 "Cannot find an endpoint for circuit_id=%d\n",
486 osmuxh->circuit_id);
487 goto out;
488 }
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200489 if (endp_osmux_state_check(endp, conn_net, false) == 0) {
490 conn_net->osmux.stats.octets += osmux_chunk_length(msg, rem);
491 conn_net->osmux.stats.chunks++;
492 osmux_xfrm_output_sched(&conn_net->osmux.out, osmuxh);
493 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200494 rem = msg->len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200495 }
496out:
497 msgb_free(msg);
498 return 0;
499}
500
501int osmux_init(int role, struct mgcp_config *cfg)
502{
503 int ret;
504
505 switch(role) {
506 case OSMUX_ROLE_BSC:
507 osmux_fd.cb = osmux_read_from_bsc_nat_cb;
508 break;
509 case OSMUX_ROLE_BSC_NAT:
510 osmux_fd.cb = osmux_read_from_bsc_cb;
511 break;
512 default:
513 LOGP(DLMGCP, LOGL_ERROR, "wrong role for OSMUX\n");
514 return -1;
515 }
516 osmux_fd.data = cfg;
517
518 ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port);
519 if (ret < 0) {
520 LOGP(DLMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n");
521 return ret;
522 }
523 mgcp_set_ip_tos(osmux_fd.fd, cfg->endp_dscp);
524 osmux_fd.when |= BSC_FD_READ;
525
526 ret = osmo_fd_register(&osmux_fd);
527 if (ret < 0) {
528 LOGP(DLMGCP, LOGL_ERROR, "cannot register OSMUX socket\n");
529 return ret;
530 }
531 cfg->osmux_init = 1;
532
533 return 0;
534}
535
Philipp Maier87bd9be2017-08-22 16:35:41 +0200536/*! enable OSXMUX circuit for a specified connection.
537 * \param[in] endp mgcp endpoint (configuration)
538 * \param[in] conn connection to disable
539 * \param[in] addr IP address of remote OSMUX endpoint
540 * \param[in] port portnumber of the remote OSMUX endpoint
541 * \returns 0 on success, -1 on ERROR */
542int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
543 struct in_addr *addr, uint16_t port)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200544{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200545 /*! If osmux is enabled, initialize the output handler. This handler is
546 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
547 * allocated based on the circuit ID (conn_net->osmux.cid), which is unique
548 * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200549 * SSRC space (2^32) by the OSMUX_CID_MAX + 1 possible circuit IDs, then randomly
Philipp Maier87bd9be2017-08-22 16:35:41 +0200550 * select one value from that window. Thus, we have no chance to have
551 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
552 * similarly, for flows traveling to the MSC.
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200553 */
Pau Espin Pedrol17bf6032018-09-17 13:38:39 +0200554 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / (OSMUX_CID_MAX + 1);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200555 uint16_t osmux_dummy = endp->cfg->osmux_dummy;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200556
Philipp Maier87bd9be2017-08-22 16:35:41 +0200557 /* Check if osmux is enabled for the specified connection */
Pau Espin Pedrol48aff622018-10-16 16:03:57 +0200558 if (conn->osmux.state != OSMUX_STATE_ACTIVATING) {
559 LOGP(DLMGCP, LOGL_ERROR, "conn:%s didn't negotiate Osmux, state %d\n",
560 mgcp_conn_dump(conn->conn), conn->osmux.state);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200561 return -1;
562 }
563
Philipp Maier87bd9be2017-08-22 16:35:41 +0200564 conn->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
565 if (!conn->osmux.in) {
566 LOGP(DLMGCP, LOGL_ERROR, "Cannot allocate input osmux handle for conn:%s\n",
567 mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200568 return -1;
569 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200570 if (!osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.cid, osmux_dummy)) {
571 LOGP(DLMGCP, LOGL_ERROR, "Cannot open osmux circuit %u for conn:%s\n",
572 conn->osmux.cid, mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200573 return -1;
574 }
575
Pau Espin Pedrol426a9d92018-10-16 15:31:45 +0200576 osmux_xfrm_output_init(&conn->osmux.out,
577 (conn->osmux.cid * rtp_ssrc_winlen) +
578 (random() % rtp_ssrc_winlen));
579
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200580 switch (endp->cfg->role) {
581 case MGCP_BSC_NAT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200582 conn->type = MGCP_OSMUX_BSC_NAT;
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200583 osmux_xfrm_output_set_tx_cb(&conn->osmux.out,
584 scheduled_tx_net_cb, endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200585 break;
586 case MGCP_BSC:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200587 conn->type = MGCP_OSMUX_BSC;
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200588 osmux_xfrm_output_set_tx_cb(&conn->osmux.out,
589 scheduled_tx_bts_cb, endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200590 break;
591 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200592
593 conn->osmux.state = OSMUX_STATE_ENABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200594
595 return 0;
596}
597
Philipp Maier87bd9be2017-08-22 16:35:41 +0200598/*! disable OSXMUX circuit for a specified connection.
599 * \param[in] conn connection to disable */
600void osmux_disable_conn(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200601{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200602 if (!conn)
603 return;
604
605 if (conn->osmux.state != OSMUX_STATE_ENABLED)
606 return;
607
Philipp Maier01d24a32017-11-21 17:26:09 +0100608 LOGP(DLMGCP, LOGL_INFO, "Releasing connection %s using Osmux CID %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200609 conn->conn->id, conn->osmux.cid);
Pau Espin Pedrolf2321b72018-05-17 13:55:14 +0200610
611 /* We are closing, we don't need pending RTP packets to be transmitted */
612 osmux_xfrm_output_set_tx_cb(&conn->osmux.out, NULL, NULL);
613 osmux_xfrm_output_flush(&conn->osmux.out);
614
Philipp Maier87bd9be2017-08-22 16:35:41 +0200615 osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.cid);
616 conn->osmux.state = OSMUX_STATE_DISABLED;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200617 conn_osmux_release_cid(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200618 osmux_handle_put(conn->osmux.in);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200619}
620
Philipp Maier87bd9be2017-08-22 16:35:41 +0200621/*! relase OSXMUX cid, that had been allocated to this connection.
622 * \param[in] conn connection with OSMUX cid to release */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200623void conn_osmux_release_cid(struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200624{
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200625 if (conn->osmux.cid_allocated)
626 osmux_cid_pool_put(conn->osmux.cid);
627 conn->osmux.cid = 0;
628 conn->osmux.cid_allocated = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200629}
630
Philipp Maier87bd9be2017-08-22 16:35:41 +0200631/*! allocate OSXMUX cid to connection.
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200632 * \param[in] conn connection for which we allocate the OSMUX cid
633 * \param[in] osmux_cid OSMUX cid to allocate. -1 Means take next available one.
634 * \returns Allocated OSMUX cid, -1 on error (no free cids avail, or selected one is already taken).
635 */
636int conn_osmux_allocate_cid(struct mgcp_conn_rtp *conn, int osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200637{
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200638 if (osmux_cid != -1 && osmux_cid_pool_allocated((uint8_t) osmux_cid)) {
639 LOGP(DLMGCP, LOGL_INFO, "Conn %s: Osmux CID %d already allocated!\n",
640 conn->conn->id, osmux_cid);
641 return -1;
642 }
643
644 if (osmux_cid == -1) {
645 osmux_cid = osmux_cid_pool_get_next();
646 if (osmux_cid == -1) {
647 LOGP(DLMGCP, LOGL_INFO, "Conn %s: no available Osmux CID to allocate!\n",
648 conn->conn->id);
649 return -1;
650 }
651 } else
652 osmux_cid_pool_get(osmux_cid);
653
654 conn->osmux.cid = (uint8_t) osmux_cid;
655 conn->osmux.cid_allocated = true;
656 return osmux_cid;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200657}
658
Philipp Maier87bd9be2017-08-22 16:35:41 +0200659/*! send RTP dummy packet to OSMUX connection port.
660 * \param[in] endp mcgp endpoint that holds the RTP connection
661 * \param[in] conn associated RTP connection
662 * \returns bytes sent, -1 on error */
663int osmux_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200664{
665 char buf[1 + sizeof(uint8_t)];
666 struct in_addr addr_unset = {};
667
Philipp Maier87bd9be2017-08-22 16:35:41 +0200668 /*! The dummy packet will not be sent via the actual OSMUX connection,
669 * instead it is sent out of band to port where the remote OSMUX
670 * multplexer is listening. The goal is to ensure that the connection
671 * is kept open */
672
673 /*! We don't need to send the dummy load for osmux so often as another
674 * endpoint may have already punched the hole in the firewall. This
675 * approach is simple though. */
676
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200677 buf[0] = MGCP_DUMMY_LOAD;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200678 memcpy(&buf[1], &conn->osmux.cid, sizeof(conn->osmux.cid));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200679
680 /* Wait until we have the connection information from MDCX */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200681 if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200682 return 0;
683
Pau Espin Pedrolde2a4d72018-10-16 16:37:43 +0200684 if (endp_osmux_state_check(endp, conn, true) < 0)
685 return 0;
686
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200687 LOGP(DLMGCP, LOGL_DEBUG,
688 "sending OSMUX dummy load to %s CID %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200689 inet_ntoa(conn->end.addr), conn->osmux.cid);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200690
Philipp Maier87bd9be2017-08-22 16:35:41 +0200691 return mgcp_udp_send(osmux_fd.fd, &conn->end.addr,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200692 htons(endp->cfg->osmux_port), buf, sizeof(buf));
693}
694
Pau Espin Pedrolbcd52e52018-09-17 12:41:28 +0200695/* bsc-nat allocates/releases the Osmux circuit ID. +7 to round up to 8 bit boundary. */
696static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1 + 7) / 8];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200697
Philipp Maier87bd9be2017-08-22 16:35:41 +0200698/*! count the number of taken OSMUX cids.
699 * \returns number of OSMUX cids in use */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200700int osmux_cid_pool_count_used(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200701{
702 int i, j, used = 0;
703
704 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
705 for (j = 0; j < 8; j++) {
706 if (osmux_cid_bitmap[i] & (1 << j))
707 used += 1;
708 }
709 }
710
711 return used;
712}
713
Philipp Maier87bd9be2017-08-22 16:35:41 +0200714/*! take a free OSMUX cid.
715 * \returns OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200716int osmux_cid_pool_get_next(void)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200717{
718 int i, j;
719
720 for (i = 0; i < sizeof(osmux_cid_bitmap); i++) {
721 for (j = 0; j < 8; j++) {
722 if (osmux_cid_bitmap[i] & (1 << j))
723 continue;
724
725 osmux_cid_bitmap[i] |= (1 << j);
726 LOGP(DLMGCP, LOGL_DEBUG,
727 "Allocating Osmux CID %u from pool\n", (i * 8) + j);
728 return (i * 8) + j;
729 }
730 }
731
732 LOGP(DLMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n");
733 return -1;
734}
735
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200736/*! take a specific OSMUX cid.
737 * \param[in] osmux_cid OSMUX cid */
738void osmux_cid_pool_get(uint8_t osmux_cid)
739{
740 LOGP(DLMGCP, LOGL_DEBUG, "Allocating Osmux CID %u from pool\n", osmux_cid);
741 osmux_cid_bitmap[osmux_cid / 8] |= (1 << (osmux_cid % 8));
742}
743
Philipp Maier87bd9be2017-08-22 16:35:41 +0200744/*! put back a no longer used OSMUX cid.
745 * \param[in] osmux_cid OSMUX cid */
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200746void osmux_cid_pool_put(uint8_t osmux_cid)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200747{
748 LOGP(DLMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
749 osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8));
750}
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200751
752/*! check if OSMUX cid is already taken */
753bool osmux_cid_pool_allocated(uint8_t osmux_cid)
754{
755 return !!(osmux_cid_bitmap[osmux_cid / 8] & (1 << (osmux_cid % 8)));
756}