Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 28 | static struct osmo_fd osmux_fd; |
| 29 | |
Holger Hans Peter Freyther | d2c2701 | 2014-07-09 01:38:29 +0200 | [diff] [blame] | 30 | /* TODO: expire old handles.. */ |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 31 | static LLIST_HEAD(osmux_handle_list); |
| 32 | |
| 33 | struct osmux_handle { |
| 34 | struct llist_head head; |
| 35 | struct osmux_in_handle *in; |
| 36 | }; |
| 37 | |
| 38 | static void *osmux; |
| 39 | |
| 40 | static 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 | |
| 59 | static struct osmux_in_handle * |
| 60 | osmux_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 Freyther | a71794b | 2014-07-08 21:00:43 +0200 | [diff] [blame] | 93 | LOGP(DMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s\n", |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 94 | inet_ntoa(*addr)); |
| 95 | out: |
| 96 | return h->in; |
| 97 | } |
| 98 | |
| 99 | int 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 Freyther | 575599f | 2014-05-22 15:23:22 +0200 | [diff] [blame] | 123 | msgb_free(msg); |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 124 | 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 Freyther | 575599f | 2014-05-22 15:23:22 +0200 | [diff] [blame] | 131 | msgb_free(msg); |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 132 | 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 | |
| 145 | static struct mgcp_endpoint * |
| 146 | endpoint_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 Freyther | 3b8ab41 | 2014-07-09 00:28:02 +0200 | [diff] [blame^] | 174 | if ((tmp->ci & 0xFF) == cid && this->s_addr == from_addr->s_addr) |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 175 | return tmp; |
| 176 | } |
| 177 | |
Holger Hans Peter Freyther | 9350b30 | 2014-07-08 09:08:34 +0200 | [diff] [blame] | 178 | LOGP(DMGCP, LOGL_ERROR, "Cannot find endpoint with cid=%d\n", cid); |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 179 | |
| 180 | return NULL; |
| 181 | } |
| 182 | |
| 183 | static 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 | |
| 192 | static 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 | |
| 201 | static 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 | |
| 224 | int 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 | } |
| 265 | out: |
| 266 | msgb_free(msg); |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what) |
| 271 | { |
| 272 | struct msgb *msg; |
| 273 | struct osmux_hdr *osmuxh; |
| 274 | struct llist_head list; |
| 275 | struct sockaddr_in addr; |
| 276 | struct mgcp_config *cfg = ofd->data; |
| 277 | char buf[4096]; |
| 278 | |
| 279 | msg = osmux_recv(ofd, &addr); |
| 280 | if (!msg) |
| 281 | return -1; |
| 282 | |
| 283 | /* not any further processing dummy messages */ |
| 284 | if (msg->data[0] == MGCP_DUMMY_LOAD) |
| 285 | goto out; |
| 286 | |
| 287 | osmux_snprintf(buf, sizeof(buf), msg); |
Holger Hans Peter Freyther | f34a8dc | 2014-07-08 23:12:56 +0200 | [diff] [blame] | 288 | LOGP(DMGCP, LOGL_DEBUG, |
| 289 | "received OSMUX message from BSC(%s:%d) (len=%d) %s\n", |
| 290 | inet_ntoa(addr.sin_addr), ntohs(addr.sin_port), |
| 291 | msg->len, buf); |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 292 | |
| 293 | while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) { |
| 294 | struct mgcp_endpoint *endp; |
| 295 | |
| 296 | /* Yes, we use MGCP_DEST_BTS to locate the origin */ |
| 297 | endp = endpoint_lookup(cfg, osmuxh->circuit_id, |
| 298 | &addr.sin_addr, MGCP_DEST_BTS); |
| 299 | if (!endp) { |
| 300 | LOGP(DMGCP, LOGL_ERROR, |
| 301 | "Cannot find an endpoint for circuit_id=%d\n", |
| 302 | osmuxh->circuit_id); |
| 303 | goto out; |
| 304 | } |
| 305 | |
| 306 | LOGP(DMGCP, LOGL_DEBUG, |
| 307 | "sending extracted RTP from OSMUX to MSC via endpoint=%u " |
| 308 | "(allocated=%d)\n", ENDPOINT_NUMBER(endp), endp->allocated); |
| 309 | |
| 310 | osmux_xfrm_output(osmuxh, &endp->osmux_out, &list); |
| 311 | osmux_tx_sched(&list, scheduled_tx_net_cb, endp); |
| 312 | } |
| 313 | out: |
| 314 | msgb_free(msg); |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | int osmux_init(int role, struct mgcp_config *cfg) |
| 319 | { |
| 320 | int ret; |
| 321 | |
| 322 | switch(role) { |
| 323 | case OSMUX_ROLE_BSC: |
| 324 | osmux_fd.cb = osmux_read_from_bsc_nat_cb; |
| 325 | break; |
| 326 | case OSMUX_ROLE_BSC_NAT: |
| 327 | osmux_fd.cb = osmux_read_from_bsc_cb; |
| 328 | break; |
| 329 | default: |
| 330 | LOGP(DMGCP, LOGL_ERROR, "wrong role for OSMUX\n"); |
| 331 | return -1; |
| 332 | } |
| 333 | osmux_fd.data = cfg; |
| 334 | |
| 335 | ret = mgcp_create_bind("0.0.0.0", &osmux_fd, OSMUX_PORT); |
| 336 | if (ret < 0) { |
| 337 | LOGP(DMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n"); |
| 338 | return ret; |
| 339 | } |
| 340 | osmux_fd.when |= BSC_FD_READ; |
| 341 | |
| 342 | ret = osmo_fd_register(&osmux_fd); |
| 343 | if (ret < 0) { |
| 344 | LOGP(DMGCP, LOGL_ERROR, "cannot register OSMUX socket\n"); |
| 345 | return ret; |
| 346 | } |
| 347 | cfg->osmux_init = 1; |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | int osmux_enable_endpoint(struct mgcp_endpoint *endp, int role) |
| 353 | { |
| 354 | /* If osmux is enabled, initialize the output handler. This handler is |
| 355 | * used to reconstruct the RTP flow from osmux. The RTP SSRC is |
| 356 | * allocated based on the circuit ID (endp->ci), which is unique in the |
| 357 | * local scope to the BSC/BSC-NAT. We use it to divide the RTP SSRC |
| 358 | * space (2^32) by the 256 possible circuit IDs, then randomly select |
| 359 | * one value from that window. Thus, we have no chance to have |
| 360 | * overlapping RTP SSRC traveling to the BTSes behind the BSC, |
| 361 | * similarly, for flows traveling to the MSC. |
| 362 | */ |
| 363 | static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256; |
| 364 | |
| 365 | if (!endp->cfg->osmux_init) { |
| 366 | if (osmux_init(role, endp->cfg) < 0) { |
| 367 | LOGP(DMGCP, LOGL_ERROR, "Cannot init OSMUX\n"); |
| 368 | return -1; |
| 369 | } |
| 370 | LOGP(DMGCP, LOGL_NOTICE, "OSMUX requested, ENABLING.\n"); |
| 371 | } |
| 372 | |
| 373 | osmux_xfrm_output_init(&endp->osmux_out, |
| 374 | (endp->ci * rtp_ssrc_winlen) + |
| 375 | (random() % rtp_ssrc_winlen)); |
| 376 | |
| 377 | switch (endp->cfg->role) { |
| 378 | case MGCP_BSC_NAT: |
| 379 | endp->type = MGCP_OSMUX_BSC_NAT; |
| 380 | break; |
| 381 | case MGCP_BSC: |
| 382 | endp->type = MGCP_OSMUX_BSC; |
| 383 | break; |
| 384 | } |
| 385 | endp->osmux = 1; |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | /* We don't need to send the dummy load for osmux so often as another endpoint |
| 391 | * may have already punched the hole in the firewall. This approach is simple |
| 392 | * though. |
| 393 | */ |
| 394 | int osmux_send_dummy(struct mgcp_endpoint *endp) |
| 395 | { |
| 396 | static char buf[] = { MGCP_DUMMY_LOAD }; |
| 397 | |
| 398 | LOGP(DMGCP, LOGL_DEBUG, "sending OSMUX dummy load to %s\n", |
| 399 | inet_ntoa(endp->net_end.addr)); |
| 400 | |
| 401 | return mgcp_udp_send(osmux_fd.fd, &endp->net_end.addr, |
| 402 | htons(OSMUX_PORT), buf, 1); |
| 403 | } |