blob: ad9c2ee7118da9de48f1c54a3a4925458b3d2229 [file] [log] [blame]
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +01001/*
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 <netinet/in.h>
16#include <osmocom/core/msgb.h>
17#include <osmocom/core/talloc.h>
18
19#include <osmocom/netif/osmux.h>
20#include <osmocom/netif/rtp.h>
21
22#include <openbsc/mgcp.h>
23#include <openbsc/mgcp_internal.h>
24#include <openbsc/osmux.h>
25
26#define OSMUX_PORT 1984
27
28static struct osmo_fd osmux_fd;
29
Holger Hans Peter Freytherc6e90492014-07-09 01:38:29 +020030/* TODO: expire old handles.. */
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010031static LLIST_HEAD(osmux_handle_list);
32
33struct osmux_handle {
34 struct llist_head head;
35 struct osmux_in_handle *in;
36};
37
38static void *osmux;
39
40static void osmux_deliver(struct msgb *batch_msg, void *data)
41{
42 struct in_addr *addr = data;
43 struct sockaddr_in out = {
44 .sin_family = AF_INET,
45 .sin_port = htons(OSMUX_PORT),
46 };
47 char buf[4096];
48
49 memcpy(&out.sin_addr, addr, sizeof(*addr));
50
51 osmux_snprintf(buf, sizeof(buf), batch_msg);
52 LOGP(DMGCP, LOGL_DEBUG, "OSMUX delivering batch to addr=%s: %s\n",
53 inet_ntoa(out.sin_addr), buf);
54
55 sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0,
56 (struct sockaddr *)&out, sizeof(out));
57}
58
59static struct osmux_in_handle *
60osmux_handle_lookup(struct mgcp_config *cfg, struct in_addr *addr)
61{
62 struct osmux_handle *h;
63
64 /* Lookup for existing OSMUX handle for this destination address. */
65 llist_for_each_entry(h, &osmux_handle_list, head) {
66 if (memcmp(h->in->data, addr, sizeof(struct in_addr)) == 0) {
67 LOGP(DMGCP, LOGL_DEBUG, "using existing OSMUX handle "
68 "for addr=%s\n",
69 inet_ntoa(*addr));
70 goto out;
71 }
72 }
73
74 /* Does not exist, allocate it. */
75 h = talloc_zero(osmux, struct osmux_handle);
76 if (!h)
77 return NULL;
78
79 h->in = talloc_zero(osmux, struct osmux_in_handle);
80 if (!h->in) {
81 talloc_free(h);
82 return NULL;
83 }
84
85 h->in->osmux_seq = 0; /* sequence number to start OSmux message from */
86 h->in->batch_factor = cfg->osmux_batch;
87 h->in->deliver = osmux_deliver;
88 osmux_xfrm_input_init(h->in);
89 h->in->data = addr;
90
91 llist_add(&h->head, &osmux_handle_list);
92
Holger Hans Peter Freyther7239f572014-07-08 21:00:43 +020093 LOGP(DMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s\n",
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +010094 inet_ntoa(*addr));
95out:
96 return h->in;
97}
98
99int osmux_xfrm_to_osmux(int type, char *buf, int rc, struct mgcp_endpoint *endp)
100{
101 int ret;
102 struct msgb *msg;
103 struct in_addr *addr;
104 struct osmux_in_handle *in;
105
106 msg = msgb_alloc(4096, "RTP");
107 if (!msg)
108 return 0;
109
110 memcpy(msg->data, buf, rc);
111 msgb_put(msg, rc);
112
113 switch(type) {
114 case MGCP_DEST_NET:
115 addr = &endp->net_end.addr;
116 break;
117 case MGCP_DEST_BTS:
118 addr = &endp->bts_end.addr;
119 break;
120 default:
121 /* Should not ever happen */
122 LOGP(DMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type);
Holger Hans Peter Freyther4f200492014-05-22 15:23:22 +0200123 msgb_free(msg);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100124 return 0;
125 }
126
127 /* Lookup for osmux input handle that munches this RTP frame */
128 in = osmux_handle_lookup(endp->cfg, addr);
129 if (!in) {
130 LOGP(DMGCP, LOGL_ERROR, "No osmux handle, aborting\n");
Holger Hans Peter Freyther4f200492014-05-22 15:23:22 +0200131 msgb_free(msg);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100132 return 0;
133 }
134
135 LOGP(DMGCP, LOGL_DEBUG, "Osmux uses cid=%u from endpoint=%d (active=%d)\n",
136 endp->ci, ENDPOINT_NUMBER(endp), endp->allocated);
137
138 while ((ret = osmux_xfrm_input(in, msg, endp->ci)) > 0) {
139 /* batch full, build and deliver it */
140 osmux_xfrm_input_deliver(in);
141 }
142 return 0;
143}
144
145static struct mgcp_endpoint *
146endpoint_lookup(struct mgcp_config *cfg, int cid,
147 struct in_addr *from_addr, int type)
148{
149 struct mgcp_endpoint *tmp = NULL;
150 int i;
151
152 /* Lookup for the endpoint that corresponds to this port */
153 for (i=0; i<cfg->trunk.number_endpoints; i++) {
154 struct in_addr *this;
155
156 tmp = &cfg->trunk.endpoints[i];
157
158 if (!tmp->allocated)
159 continue;
160
161 switch(type) {
162 case MGCP_DEST_NET:
163 this = &tmp->net_end.addr;
164 break;
165 case MGCP_DEST_BTS:
166 this = &tmp->bts_end.addr;
167 break;
168 default:
169 /* Should not ever happen */
170 LOGP(DMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type);
171 return NULL;
172 }
173
Holger Hans Peter Freyther25a2db02014-07-09 00:28:02 +0200174 if ((tmp->ci & 0xFF) == cid && this->s_addr == from_addr->s_addr)
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100175 return tmp;
176 }
177
Holger Hans Peter Freyther891b0a82014-07-08 09:08:34 +0200178 LOGP(DMGCP, LOGL_ERROR, "Cannot find endpoint with cid=%d\n", cid);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100179
180 return NULL;
181}
182
183static void scheduled_tx_net_cb(struct msgb *msg, void *data)
184{
185 struct mgcp_endpoint *endp = data;
186 struct sockaddr_in addr;
187
188 mgcp_send(endp, MGCP_DEST_NET, 1, &addr, (char *)msg->data, msg->len);
189 msgb_free(msg);
190}
191
192static void scheduled_tx_bts_cb(struct msgb *msg, void *data)
193{
194 struct mgcp_endpoint *endp = data;
195 struct sockaddr_in addr;
196
197 mgcp_send(endp, MGCP_DEST_BTS, 1, &addr, (char *)msg->data, msg->len);
198 msgb_free(msg);
199}
200
201static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr)
202{
203 struct msgb *msg;
204 socklen_t slen = sizeof(*addr);
205 int ret;
206
207 msg = msgb_alloc(4096, "OSMUX");
208 if (!msg) {
209 LOGP(DMGCP, LOGL_ERROR, "cannot allocate message\n");
210 return NULL;
211 }
212 ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0,
213 (struct sockaddr *)addr, &slen);
214 if (ret <= 0) {
215 msgb_free(msg);
216 LOGP(DMGCP, LOGL_ERROR, "cannot receive message\n");
217 return NULL;
218 }
219 msgb_put(msg, ret);
220
221 return msg;
222}
223
224int osmux_read_from_bsc_nat_cb(struct osmo_fd *ofd, unsigned int what)
225{
226 struct msgb *msg;
227 struct osmux_hdr *osmuxh;
228 struct llist_head list;
229 struct sockaddr_in addr;
230 struct mgcp_config *cfg = ofd->data;
231 char buf[4096];
232
233 msg = osmux_recv(ofd, &addr);
234 if (!msg)
235 return -1;
236
237 /* not any further processing dummy messages */
238 if (msg->data[0] == MGCP_DUMMY_LOAD)
239 goto out;
240
241 osmux_snprintf(buf, sizeof(buf), msg);
242 LOGP(DMGCP, LOGL_DEBUG, "received OSMUX message from "
243 "BSC NAT (len=%d) %s\n", msg->len, buf);
244
245 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
246 struct mgcp_endpoint *endp;
247
248 /* Yes, we use MGCP_DEST_NET to locate the origin */
249 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
250 &addr.sin_addr, MGCP_DEST_NET);
251 if (!endp) {
252 LOGP(DMGCP, LOGL_ERROR,
253 "Cannot find an endpoint for circuit_id=%d\n",
254 osmuxh->circuit_id);
255 goto out;
256 }
257
258 LOGP(DMGCP, LOGL_DEBUG,
259 "sending extracted RTP from OSMUX to BSC via endpoint=%u "
260 "(allocated=%d)\n", ENDPOINT_NUMBER(endp), endp->allocated);
261
262 osmux_xfrm_output(osmuxh, &endp->osmux_out, &list);
263 osmux_tx_sched(&list, scheduled_tx_bts_cb, endp);
264 }
265out:
266 msgb_free(msg);
267 return 0;
268}
269
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200270/*
271 * Try to figure out where it came from and enter the rtp_port
272 */
273static int osmux_handle_dummy(struct mgcp_config *cfg,
274 struct sockaddr_in *addr, struct msgb *msg)
275{
276 struct mgcp_endpoint *endp;
277 uint32_t ci;
278
279 if (msg->len < 1 + sizeof(ci))
280 goto out;
281
282 /* extract the CI from the dummy message */
283 memcpy(&ci, &msg->data[1], sizeof(ci));
284 ci = ntohl(ci);
285
286 endp = endpoint_lookup(cfg, ci & 0xff, &addr->sin_addr, MGCP_DEST_BTS);
287 if (!endp) {
288 LOGP(DMGCP, LOGL_ERROR, "Can not find CI=%d\n", ci & 0xff);
289 goto out;
290 }
291
292 if (endp->bts_end.rtp_port == 0) {
293 endp->bts_end.rtp_port = addr->sin_port;
294 LOGP(DMGCP, LOGL_NOTICE, "0x%x found BTS on endpoint %s:%d\n",
295 ENDPOINT_NUMBER(endp),
296 inet_ntoa(addr->sin_addr), htons(addr->sin_port));
297 }
298out:
299 msgb_free(msg);
300 return 0;
301}
302
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100303int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what)
304{
305 struct msgb *msg;
306 struct osmux_hdr *osmuxh;
307 struct llist_head list;
308 struct sockaddr_in addr;
309 struct mgcp_config *cfg = ofd->data;
310 char buf[4096];
311
312 msg = osmux_recv(ofd, &addr);
313 if (!msg)
314 return -1;
315
316 /* not any further processing dummy messages */
317 if (msg->data[0] == MGCP_DUMMY_LOAD)
Holger Hans Peter Freyther48a071e2014-07-09 00:38:06 +0200318 return osmux_handle_dummy(cfg, &addr, msg);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100319
320 osmux_snprintf(buf, sizeof(buf), msg);
Holger Hans Peter Freyther9d43cee2014-07-08 23:12:56 +0200321 LOGP(DMGCP, LOGL_DEBUG,
322 "received OSMUX message from BSC(%s:%d) (len=%d) %s\n",
323 inet_ntoa(addr.sin_addr), ntohs(addr.sin_port),
324 msg->len, buf);
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100325
326 while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) {
327 struct mgcp_endpoint *endp;
328
329 /* Yes, we use MGCP_DEST_BTS to locate the origin */
330 endp = endpoint_lookup(cfg, osmuxh->circuit_id,
331 &addr.sin_addr, MGCP_DEST_BTS);
332 if (!endp) {
333 LOGP(DMGCP, LOGL_ERROR,
334 "Cannot find an endpoint for circuit_id=%d\n",
335 osmuxh->circuit_id);
336 goto out;
337 }
338
339 LOGP(DMGCP, LOGL_DEBUG,
340 "sending extracted RTP from OSMUX to MSC via endpoint=%u "
341 "(allocated=%d)\n", ENDPOINT_NUMBER(endp), endp->allocated);
342
343 osmux_xfrm_output(osmuxh, &endp->osmux_out, &list);
344 osmux_tx_sched(&list, scheduled_tx_net_cb, endp);
345 }
346out:
347 msgb_free(msg);
348 return 0;
349}
350
351int osmux_init(int role, struct mgcp_config *cfg)
352{
353 int ret;
354
355 switch(role) {
356 case OSMUX_ROLE_BSC:
357 osmux_fd.cb = osmux_read_from_bsc_nat_cb;
358 break;
359 case OSMUX_ROLE_BSC_NAT:
360 osmux_fd.cb = osmux_read_from_bsc_cb;
361 break;
362 default:
363 LOGP(DMGCP, LOGL_ERROR, "wrong role for OSMUX\n");
364 return -1;
365 }
366 osmux_fd.data = cfg;
367
368 ret = mgcp_create_bind("0.0.0.0", &osmux_fd, OSMUX_PORT);
369 if (ret < 0) {
370 LOGP(DMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n");
371 return ret;
372 }
373 osmux_fd.when |= BSC_FD_READ;
374
375 ret = osmo_fd_register(&osmux_fd);
376 if (ret < 0) {
377 LOGP(DMGCP, LOGL_ERROR, "cannot register OSMUX socket\n");
378 return ret;
379 }
380 cfg->osmux_init = 1;
381
382 return 0;
383}
384
385int osmux_enable_endpoint(struct mgcp_endpoint *endp, int role)
386{
387 /* If osmux is enabled, initialize the output handler. This handler is
388 * used to reconstruct the RTP flow from osmux. The RTP SSRC is
389 * allocated based on the circuit ID (endp->ci), which is unique in the
390 * local scope to the BSC/BSC-NAT. We use it to divide the RTP SSRC
391 * space (2^32) by the 256 possible circuit IDs, then randomly select
392 * one value from that window. Thus, we have no chance to have
393 * overlapping RTP SSRC traveling to the BTSes behind the BSC,
394 * similarly, for flows traveling to the MSC.
395 */
396 static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256;
397
398 if (!endp->cfg->osmux_init) {
399 if (osmux_init(role, endp->cfg) < 0) {
400 LOGP(DMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
401 return -1;
402 }
403 LOGP(DMGCP, LOGL_NOTICE, "OSMUX requested, ENABLING.\n");
404 }
405
406 osmux_xfrm_output_init(&endp->osmux_out,
407 (endp->ci * rtp_ssrc_winlen) +
408 (random() % rtp_ssrc_winlen));
409
410 switch (endp->cfg->role) {
411 case MGCP_BSC_NAT:
412 endp->type = MGCP_OSMUX_BSC_NAT;
413 break;
414 case MGCP_BSC:
415 endp->type = MGCP_OSMUX_BSC;
416 break;
417 }
418 endp->osmux = 1;
419
420 return 0;
421}
422
423/* We don't need to send the dummy load for osmux so often as another endpoint
424 * may have already punched the hole in the firewall. This approach is simple
425 * though.
426 */
427int osmux_send_dummy(struct mgcp_endpoint *endp)
428{
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200429 uint32_t ci_be;
430 char buf[1 + sizeof(uint32_t)];
431
432 ci_be = htonl(endp->ci);
433 buf[0] = MGCP_DUMMY_LOAD;
434 memcpy(&buf[1], &ci_be, sizeof(ci_be));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100435
436 LOGP(DMGCP, LOGL_DEBUG, "sending OSMUX dummy load to %s\n",
437 inet_ntoa(endp->net_end.addr));
438
439 return mgcp_udp_send(osmux_fd.fd, &endp->net_end.addr,
Holger Hans Peter Freyther07ec8ee2014-07-09 00:32:00 +0200440 htons(OSMUX_PORT), buf, sizeof(buf));
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100441}