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