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