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 */ |
Pablo Neira Ayuso | 2e719f8 | 2014-08-28 16:25:09 +0200 | [diff] [blame] | 15 | #include <inttypes.h> /* for PRIu64 */ |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 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 | |
| 23 | #include <openbsc/mgcp.h> |
| 24 | #include <openbsc/mgcp_internal.h> |
| 25 | #include <openbsc/osmux.h> |
| 26 | |
| 27 | #define OSMUX_PORT 1984 |
| 28 | |
| 29 | static struct osmo_fd osmux_fd; |
| 30 | |
| 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 | c220c0b | 2014-07-09 00:53:29 +0200 | [diff] [blame] | 36 | struct in_addr rem_addr; |
| 37 | int rem_port; |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 38 | int refcnt; |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | static void *osmux; |
| 42 | |
| 43 | static void osmux_deliver(struct msgb *batch_msg, void *data) |
| 44 | { |
Holger Hans Peter Freyther | c220c0b | 2014-07-09 00:53:29 +0200 | [diff] [blame] | 45 | struct osmux_handle *handle = data; |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 46 | struct sockaddr_in out = { |
| 47 | .sin_family = AF_INET, |
Holger Hans Peter Freyther | c220c0b | 2014-07-09 00:53:29 +0200 | [diff] [blame] | 48 | .sin_port = handle->rem_port, |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 49 | }; |
| 50 | char buf[4096]; |
| 51 | |
Holger Hans Peter Freyther | c220c0b | 2014-07-09 00:53:29 +0200 | [diff] [blame] | 52 | memcpy(&out.sin_addr, &handle->rem_addr, sizeof(handle->rem_addr)); |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 53 | |
| 54 | osmux_snprintf(buf, sizeof(buf), batch_msg); |
| 55 | LOGP(DMGCP, LOGL_DEBUG, "OSMUX delivering batch to addr=%s: %s\n", |
| 56 | inet_ntoa(out.sin_addr), buf); |
| 57 | |
| 58 | sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0, |
| 59 | (struct sockaddr *)&out, sizeof(out)); |
| 60 | } |
| 61 | |
Pablo Neira Ayuso | e8f4760 | 2014-08-26 18:45:46 +0200 | [diff] [blame] | 62 | static struct osmux_handle * |
| 63 | osmux_handle_find_get(struct in_addr *addr, int rem_port) |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 64 | { |
| 65 | struct osmux_handle *h; |
| 66 | |
| 67 | /* Lookup for existing OSMUX handle for this destination address. */ |
| 68 | llist_for_each_entry(h, &osmux_handle_list, head) { |
Pablo Neira Ayuso | e8f4760 | 2014-08-26 18:45:46 +0200 | [diff] [blame] | 69 | if (memcmp(&h->rem_addr, addr, sizeof(struct in_addr)) == 0 && |
| 70 | h->rem_port == rem_port) { |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 71 | LOGP(DMGCP, LOGL_DEBUG, "using existing OSMUX handle " |
Holger Hans Peter Freyther | c220c0b | 2014-07-09 00:53:29 +0200 | [diff] [blame] | 72 | "for addr=%s:%d\n", |
| 73 | inet_ntoa(*addr), ntohs(rem_port)); |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 74 | h->refcnt++; |
Pablo Neira Ayuso | e8f4760 | 2014-08-26 18:45:46 +0200 | [diff] [blame] | 75 | return h; |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Pablo Neira Ayuso | e8f4760 | 2014-08-26 18:45:46 +0200 | [diff] [blame] | 79 | return NULL; |
| 80 | } |
| 81 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 82 | static void osmux_handle_put(struct osmux_in_handle *in) |
| 83 | { |
| 84 | struct osmux_handle *h; |
| 85 | |
| 86 | /* Lookup for existing OSMUX handle for this destination address. */ |
| 87 | llist_for_each_entry(h, &osmux_handle_list, head) { |
| 88 | if (h->in == in) { |
| 89 | if (--h->refcnt == 0) { |
Pablo Neira Ayuso | 2e719f8 | 2014-08-28 16:25:09 +0200 | [diff] [blame] | 90 | LOGP(DMGCP, LOGL_INFO, |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 91 | "Releasing unused osmux handle for %s:%d\n", |
| 92 | inet_ntoa(h->rem_addr), |
| 93 | ntohs(h->rem_port)); |
Pablo Neira Ayuso | 2e719f8 | 2014-08-28 16:25:09 +0200 | [diff] [blame] | 94 | LOGP(DMGCP, LOGL_INFO, "Stats: " |
| 95 | "input RTP msgs: %u bytes: %"PRIu64" " |
| 96 | "output osmux msgs: %u bytes: %"PRIu64"\n", |
| 97 | in->stats.input_rtp_msgs, |
| 98 | in->stats.input_rtp_bytes, |
| 99 | in->stats.output_osmux_msgs, |
| 100 | in->stats.output_osmux_bytes); |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 101 | llist_del(&h->head); |
Pablo Neira Ayuso | c41e77f | 2014-08-28 12:46:41 +0200 | [diff] [blame] | 102 | osmux_xfrm_input_fini(h->in); |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 103 | talloc_free(h); |
| 104 | } |
| 105 | return; |
| 106 | } |
| 107 | } |
| 108 | LOGP(DMGCP, LOGL_ERROR, "cannot find Osmux input handle %p\n", in); |
| 109 | } |
| 110 | |
Pablo Neira Ayuso | e8f4760 | 2014-08-26 18:45:46 +0200 | [diff] [blame] | 111 | static struct osmux_handle * |
| 112 | osmux_handle_alloc(struct mgcp_config *cfg, struct in_addr *addr, int rem_port) |
| 113 | { |
| 114 | struct osmux_handle *h; |
| 115 | |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 116 | h = talloc_zero(osmux, struct osmux_handle); |
| 117 | if (!h) |
| 118 | return NULL; |
Holger Hans Peter Freyther | c220c0b | 2014-07-09 00:53:29 +0200 | [diff] [blame] | 119 | h->rem_addr = *addr; |
| 120 | h->rem_port = rem_port; |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 121 | h->refcnt++; |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 122 | |
Pablo Neira Ayuso | e8f4760 | 2014-08-26 18:45:46 +0200 | [diff] [blame] | 123 | h->in = talloc_zero(h, struct osmux_in_handle); |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 124 | if (!h->in) { |
| 125 | talloc_free(h); |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | h->in->osmux_seq = 0; /* sequence number to start OSmux message from */ |
| 130 | h->in->batch_factor = cfg->osmux_batch; |
| 131 | h->in->deliver = osmux_deliver; |
| 132 | osmux_xfrm_input_init(h->in); |
Holger Hans Peter Freyther | c220c0b | 2014-07-09 00:53:29 +0200 | [diff] [blame] | 133 | h->in->data = h; |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 134 | |
| 135 | llist_add(&h->head, &osmux_handle_list); |
| 136 | |
Holger Hans Peter Freyther | c220c0b | 2014-07-09 00:53:29 +0200 | [diff] [blame] | 137 | LOGP(DMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s:%d\n", |
| 138 | inet_ntoa(*addr), ntohs(rem_port)); |
Pablo Neira Ayuso | e8f4760 | 2014-08-26 18:45:46 +0200 | [diff] [blame] | 139 | |
| 140 | return h; |
| 141 | } |
| 142 | |
| 143 | static struct osmux_in_handle * |
| 144 | osmux_handle_lookup(struct mgcp_config *cfg, struct in_addr *addr, int rem_port) |
| 145 | { |
| 146 | struct osmux_handle *h; |
| 147 | |
| 148 | h = osmux_handle_find_get(addr, rem_port); |
| 149 | if (h != NULL) |
| 150 | return h->in; |
| 151 | |
| 152 | h = osmux_handle_alloc(cfg, addr, rem_port); |
| 153 | if (h == NULL) |
| 154 | return NULL; |
| 155 | |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 156 | return h->in; |
| 157 | } |
| 158 | |
| 159 | int osmux_xfrm_to_osmux(int type, char *buf, int rc, struct mgcp_endpoint *endp) |
| 160 | { |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 161 | int ret; |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 162 | struct msgb *msg; |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 163 | |
| 164 | msg = msgb_alloc(4096, "RTP"); |
| 165 | if (!msg) |
| 166 | return 0; |
| 167 | |
| 168 | memcpy(msg->data, buf, rc); |
| 169 | msgb_put(msg, rc); |
| 170 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 171 | while ((ret = osmux_xfrm_input(endp->osmux.in, msg, endp->osmux.cid)) > 0) { |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 172 | /* batch full, build and deliver it */ |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 173 | osmux_xfrm_input_deliver(endp->osmux.in); |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 174 | } |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static struct mgcp_endpoint * |
| 179 | endpoint_lookup(struct mgcp_config *cfg, int cid, |
| 180 | struct in_addr *from_addr, int type) |
| 181 | { |
| 182 | struct mgcp_endpoint *tmp = NULL; |
| 183 | int i; |
| 184 | |
| 185 | /* Lookup for the endpoint that corresponds to this port */ |
| 186 | for (i=0; i<cfg->trunk.number_endpoints; i++) { |
| 187 | struct in_addr *this; |
| 188 | |
| 189 | tmp = &cfg->trunk.endpoints[i]; |
| 190 | |
| 191 | if (!tmp->allocated) |
| 192 | continue; |
| 193 | |
| 194 | switch(type) { |
| 195 | case MGCP_DEST_NET: |
| 196 | this = &tmp->net_end.addr; |
| 197 | break; |
| 198 | case MGCP_DEST_BTS: |
| 199 | this = &tmp->bts_end.addr; |
| 200 | break; |
| 201 | default: |
| 202 | /* Should not ever happen */ |
| 203 | LOGP(DMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type); |
| 204 | return NULL; |
| 205 | } |
| 206 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 207 | if (tmp->osmux.cid == cid && this->s_addr == from_addr->s_addr) |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 208 | return tmp; |
| 209 | } |
| 210 | |
Holger Hans Peter Freyther | 9350b30 | 2014-07-08 09:08:34 +0200 | [diff] [blame] | 211 | 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] | 212 | |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | static void scheduled_tx_net_cb(struct msgb *msg, void *data) |
| 217 | { |
| 218 | struct mgcp_endpoint *endp = data; |
| 219 | struct sockaddr_in addr; |
| 220 | |
| 221 | mgcp_send(endp, MGCP_DEST_NET, 1, &addr, (char *)msg->data, msg->len); |
| 222 | msgb_free(msg); |
| 223 | } |
| 224 | |
| 225 | static void scheduled_tx_bts_cb(struct msgb *msg, void *data) |
| 226 | { |
| 227 | struct mgcp_endpoint *endp = data; |
| 228 | struct sockaddr_in addr; |
| 229 | |
| 230 | mgcp_send(endp, MGCP_DEST_BTS, 1, &addr, (char *)msg->data, msg->len); |
| 231 | msgb_free(msg); |
| 232 | } |
| 233 | |
| 234 | static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr) |
| 235 | { |
| 236 | struct msgb *msg; |
| 237 | socklen_t slen = sizeof(*addr); |
| 238 | int ret; |
| 239 | |
| 240 | msg = msgb_alloc(4096, "OSMUX"); |
| 241 | if (!msg) { |
| 242 | LOGP(DMGCP, LOGL_ERROR, "cannot allocate message\n"); |
| 243 | return NULL; |
| 244 | } |
| 245 | ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0, |
| 246 | (struct sockaddr *)addr, &slen); |
| 247 | if (ret <= 0) { |
| 248 | msgb_free(msg); |
| 249 | LOGP(DMGCP, LOGL_ERROR, "cannot receive message\n"); |
| 250 | return NULL; |
| 251 | } |
| 252 | msgb_put(msg, ret); |
| 253 | |
| 254 | return msg; |
| 255 | } |
| 256 | |
| 257 | int osmux_read_from_bsc_nat_cb(struct osmo_fd *ofd, unsigned int what) |
| 258 | { |
| 259 | struct msgb *msg; |
| 260 | struct osmux_hdr *osmuxh; |
| 261 | struct llist_head list; |
| 262 | struct sockaddr_in addr; |
| 263 | struct mgcp_config *cfg = ofd->data; |
| 264 | char buf[4096]; |
| 265 | |
| 266 | msg = osmux_recv(ofd, &addr); |
| 267 | if (!msg) |
| 268 | return -1; |
| 269 | |
| 270 | /* not any further processing dummy messages */ |
| 271 | if (msg->data[0] == MGCP_DUMMY_LOAD) |
| 272 | goto out; |
| 273 | |
| 274 | osmux_snprintf(buf, sizeof(buf), msg); |
| 275 | LOGP(DMGCP, LOGL_DEBUG, "received OSMUX message from " |
| 276 | "BSC NAT (len=%d) %s\n", msg->len, buf); |
| 277 | |
| 278 | while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) { |
| 279 | struct mgcp_endpoint *endp; |
| 280 | |
| 281 | /* Yes, we use MGCP_DEST_NET to locate the origin */ |
| 282 | endp = endpoint_lookup(cfg, osmuxh->circuit_id, |
| 283 | &addr.sin_addr, MGCP_DEST_NET); |
| 284 | if (!endp) { |
| 285 | LOGP(DMGCP, LOGL_ERROR, |
| 286 | "Cannot find an endpoint for circuit_id=%d\n", |
| 287 | osmuxh->circuit_id); |
| 288 | goto out; |
| 289 | } |
| 290 | |
| 291 | LOGP(DMGCP, LOGL_DEBUG, |
| 292 | "sending extracted RTP from OSMUX to BSC via endpoint=%u " |
| 293 | "(allocated=%d)\n", ENDPOINT_NUMBER(endp), endp->allocated); |
| 294 | |
Pablo Neira Ayuso | 8dab748 | 2014-08-26 13:31:53 +0200 | [diff] [blame] | 295 | osmux_xfrm_output(osmuxh, &endp->osmux.out, &list); |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 296 | osmux_tx_sched(&list, scheduled_tx_bts_cb, endp); |
| 297 | } |
| 298 | out: |
| 299 | msgb_free(msg); |
| 300 | return 0; |
| 301 | } |
| 302 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 303 | /* This is called from the bsc-nat */ |
| 304 | static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr, |
| 305 | struct msgb *msg) |
Holger Hans Peter Freyther | fe0b579 | 2014-07-09 00:38:06 +0200 | [diff] [blame] | 306 | { |
| 307 | struct mgcp_endpoint *endp; |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 308 | uint8_t osmux_cid; |
Holger Hans Peter Freyther | fe0b579 | 2014-07-09 00:38:06 +0200 | [diff] [blame] | 309 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 310 | if (msg->len < 1 + sizeof(osmux_cid)) { |
| 311 | LOGP(DMGCP, LOGL_ERROR, |
| 312 | "Discarding truncated Osmux dummy load\n"); |
Holger Hans Peter Freyther | fe0b579 | 2014-07-09 00:38:06 +0200 | [diff] [blame] | 313 | goto out; |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 314 | } |
Holger Hans Peter Freyther | fe0b579 | 2014-07-09 00:38:06 +0200 | [diff] [blame] | 315 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 316 | LOGP(DMGCP, LOGL_DEBUG, "Received Osmux dummy load from %s\n", |
| 317 | inet_ntoa(addr->sin_addr)); |
Holger Hans Peter Freyther | fe0b579 | 2014-07-09 00:38:06 +0200 | [diff] [blame] | 318 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 319 | if (!cfg->osmux) { |
| 320 | LOGP(DMGCP, LOGL_ERROR, |
| 321 | "bsc wants to use Osmux but bsc-nat did not request it\n"); |
| 322 | goto out; |
| 323 | } |
| 324 | |
| 325 | /* extract the osmux CID from the dummy message */ |
| 326 | memcpy(&osmux_cid, &msg->data[1], sizeof(osmux_cid)); |
| 327 | |
| 328 | endp = endpoint_lookup(cfg, osmux_cid, &addr->sin_addr, MGCP_DEST_BTS); |
Holger Hans Peter Freyther | fe0b579 | 2014-07-09 00:38:06 +0200 | [diff] [blame] | 329 | if (!endp) { |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 330 | LOGP(DMGCP, LOGL_ERROR, |
| 331 | "Cannot find endpoint for Osmux CID %d\n", osmux_cid); |
Holger Hans Peter Freyther | fe0b579 | 2014-07-09 00:38:06 +0200 | [diff] [blame] | 332 | goto out; |
| 333 | } |
| 334 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 335 | if (endp->osmux.state == OSMUX_STATE_ENABLED) |
| 336 | goto out; |
| 337 | |
| 338 | if (osmux_enable_endpoint(endp, OSMUX_ROLE_BSC_NAT, |
| 339 | &addr->sin_addr, addr->sin_port) < 0 ){ |
| 340 | LOGP(DMGCP, LOGL_ERROR, |
Pablo Neira Ayuso | ba748b8 | 2014-08-28 15:35:58 +0200 | [diff] [blame] | 341 | "Could not enable osmux in endpoint %d\n", |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 342 | ENDPOINT_NUMBER(endp)); |
Pablo Neira Ayuso | ba748b8 | 2014-08-28 15:35:58 +0200 | [diff] [blame] | 343 | goto out; |
Holger Hans Peter Freyther | fe0b579 | 2014-07-09 00:38:06 +0200 | [diff] [blame] | 344 | } |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 345 | |
| 346 | LOGP(DMGCP, LOGL_INFO, "Enabling osmux in endpoint %d for %s:%u\n", |
| 347 | ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr), |
| 348 | ntohs(addr->sin_port)); |
Holger Hans Peter Freyther | fe0b579 | 2014-07-09 00:38:06 +0200 | [diff] [blame] | 349 | out: |
| 350 | msgb_free(msg); |
| 351 | return 0; |
| 352 | } |
| 353 | |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 354 | int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what) |
| 355 | { |
| 356 | struct msgb *msg; |
| 357 | struct osmux_hdr *osmuxh; |
| 358 | struct llist_head list; |
| 359 | struct sockaddr_in addr; |
| 360 | struct mgcp_config *cfg = ofd->data; |
| 361 | char buf[4096]; |
| 362 | |
| 363 | msg = osmux_recv(ofd, &addr); |
| 364 | if (!msg) |
| 365 | return -1; |
| 366 | |
| 367 | /* not any further processing dummy messages */ |
| 368 | if (msg->data[0] == MGCP_DUMMY_LOAD) |
Holger Hans Peter Freyther | fe0b579 | 2014-07-09 00:38:06 +0200 | [diff] [blame] | 369 | return osmux_handle_dummy(cfg, &addr, msg); |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 370 | |
| 371 | osmux_snprintf(buf, sizeof(buf), msg); |
Holger Hans Peter Freyther | f34a8dc | 2014-07-08 23:12:56 +0200 | [diff] [blame] | 372 | LOGP(DMGCP, LOGL_DEBUG, |
| 373 | "received OSMUX message from BSC(%s:%d) (len=%d) %s\n", |
| 374 | inet_ntoa(addr.sin_addr), ntohs(addr.sin_port), |
| 375 | msg->len, buf); |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 376 | |
| 377 | while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) { |
| 378 | struct mgcp_endpoint *endp; |
| 379 | |
| 380 | /* Yes, we use MGCP_DEST_BTS to locate the origin */ |
| 381 | endp = endpoint_lookup(cfg, osmuxh->circuit_id, |
| 382 | &addr.sin_addr, MGCP_DEST_BTS); |
| 383 | if (!endp) { |
| 384 | LOGP(DMGCP, LOGL_ERROR, |
| 385 | "Cannot find an endpoint for circuit_id=%d\n", |
| 386 | osmuxh->circuit_id); |
| 387 | goto out; |
| 388 | } |
| 389 | |
| 390 | LOGP(DMGCP, LOGL_DEBUG, |
| 391 | "sending extracted RTP from OSMUX to MSC via endpoint=%u " |
| 392 | "(allocated=%d)\n", ENDPOINT_NUMBER(endp), endp->allocated); |
| 393 | |
Pablo Neira Ayuso | 8dab748 | 2014-08-26 13:31:53 +0200 | [diff] [blame] | 394 | osmux_xfrm_output(osmuxh, &endp->osmux.out, &list); |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 395 | osmux_tx_sched(&list, scheduled_tx_net_cb, endp); |
| 396 | } |
| 397 | out: |
| 398 | msgb_free(msg); |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | int osmux_init(int role, struct mgcp_config *cfg) |
| 403 | { |
| 404 | int ret; |
| 405 | |
| 406 | switch(role) { |
| 407 | case OSMUX_ROLE_BSC: |
| 408 | osmux_fd.cb = osmux_read_from_bsc_nat_cb; |
| 409 | break; |
| 410 | case OSMUX_ROLE_BSC_NAT: |
| 411 | osmux_fd.cb = osmux_read_from_bsc_cb; |
| 412 | break; |
| 413 | default: |
| 414 | LOGP(DMGCP, LOGL_ERROR, "wrong role for OSMUX\n"); |
| 415 | return -1; |
| 416 | } |
| 417 | osmux_fd.data = cfg; |
| 418 | |
Pablo Neira Ayuso | 1b135d4 | 2014-08-28 16:43:38 +0200 | [diff] [blame] | 419 | if (!cfg->osmux_port) |
| 420 | cfg->osmux_port = OSMUX_PORT; |
| 421 | |
| 422 | ret = mgcp_create_bind("0.0.0.0", &osmux_fd, cfg->osmux_port); |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 423 | if (ret < 0) { |
| 424 | LOGP(DMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n"); |
| 425 | return ret; |
| 426 | } |
| 427 | osmux_fd.when |= BSC_FD_READ; |
| 428 | |
| 429 | ret = osmo_fd_register(&osmux_fd); |
| 430 | if (ret < 0) { |
| 431 | LOGP(DMGCP, LOGL_ERROR, "cannot register OSMUX socket\n"); |
| 432 | return ret; |
| 433 | } |
| 434 | cfg->osmux_init = 1; |
| 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 439 | int osmux_enable_endpoint(struct mgcp_endpoint *endp, int role, |
| 440 | struct in_addr *addr, uint16_t port) |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 441 | { |
| 442 | /* If osmux is enabled, initialize the output handler. This handler is |
| 443 | * used to reconstruct the RTP flow from osmux. The RTP SSRC is |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 444 | * allocated based on the circuit ID (endp->osmux.cid), which is unique |
| 445 | * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP |
| 446 | * SSRC space (2^32) by the 256 possible circuit IDs, then randomly |
| 447 | * select one value from that window. Thus, we have no chance to have |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 448 | * overlapping RTP SSRC traveling to the BTSes behind the BSC, |
| 449 | * similarly, for flows traveling to the MSC. |
| 450 | */ |
| 451 | static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256; |
| 452 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 453 | if (endp->osmux.state == OSMUX_STATE_DISABLED) { |
| 454 | LOGP(DMGCP, LOGL_ERROR, "Endpoint %u didn't request Osmux\n", |
| 455 | ENDPOINT_NUMBER(endp)); |
| 456 | return -1; |
| 457 | } |
| 458 | |
Pablo Neira Ayuso | 8dab748 | 2014-08-26 13:31:53 +0200 | [diff] [blame] | 459 | osmux_xfrm_output_init(&endp->osmux.out, |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 460 | (endp->osmux.cid * rtp_ssrc_winlen) + |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 461 | (random() % rtp_ssrc_winlen)); |
| 462 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 463 | endp->osmux.in = osmux_handle_lookup(endp->cfg, addr, port); |
| 464 | if (!endp->osmux.in) { |
| 465 | LOGP(DMGCP, LOGL_ERROR, "Cannot allocate input osmux handle\n"); |
| 466 | return -1; |
| 467 | } |
| 468 | |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 469 | switch (endp->cfg->role) { |
| 470 | case MGCP_BSC_NAT: |
| 471 | endp->type = MGCP_OSMUX_BSC_NAT; |
| 472 | break; |
| 473 | case MGCP_BSC: |
| 474 | endp->type = MGCP_OSMUX_BSC; |
| 475 | break; |
| 476 | } |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 477 | endp->osmux.state = OSMUX_STATE_ENABLED; |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 482 | void osmux_disable_endpoint(struct mgcp_endpoint *endp) |
| 483 | { |
| 484 | LOGP(DMGCP, LOGL_INFO, "Releasing endpoint %u using Osmux CID %u\n", |
| 485 | ENDPOINT_NUMBER(endp), endp->osmux.cid); |
| 486 | endp->osmux.state = OSMUX_STATE_DISABLED; |
| 487 | endp->osmux.cid = -1; |
| 488 | osmux_handle_put(endp->osmux.in); |
| 489 | } |
| 490 | |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 491 | /* We don't need to send the dummy load for osmux so often as another endpoint |
| 492 | * may have already punched the hole in the firewall. This approach is simple |
| 493 | * though. |
| 494 | */ |
| 495 | int osmux_send_dummy(struct mgcp_endpoint *endp) |
| 496 | { |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 497 | char buf[1 + sizeof(uint8_t)]; |
| 498 | struct in_addr addr_unset = {}; |
Holger Hans Peter Freyther | caf3dd0 | 2014-07-09 00:32:00 +0200 | [diff] [blame] | 499 | |
Holger Hans Peter Freyther | caf3dd0 | 2014-07-09 00:32:00 +0200 | [diff] [blame] | 500 | buf[0] = MGCP_DUMMY_LOAD; |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 501 | memcpy(&buf[1], &endp->osmux.cid, sizeof(endp->osmux.cid)); |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 502 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 503 | /* Wait until we have the connection information from MDCX */ |
| 504 | if (memcmp(&endp->net_end.addr, &addr_unset, sizeof(addr_unset)) == 0) |
| 505 | return 0; |
| 506 | |
| 507 | if (endp->osmux.state == OSMUX_STATE_ACTIVATING) { |
| 508 | if (osmux_enable_endpoint(endp, OSMUX_ROLE_BSC, |
| 509 | &endp->net_end.addr, |
| 510 | htons(OSMUX_PORT)) < 0) { |
| 511 | LOGP(DMGCP, LOGL_ERROR, |
| 512 | "Could not activate osmux in endpoint %d\n", |
| 513 | ENDPOINT_NUMBER(endp)); |
| 514 | } |
| 515 | LOGP(DMGCP, LOGL_ERROR, |
| 516 | "Osmux CID %u for %s:%u is now enabled\n", |
| 517 | endp->osmux.cid, inet_ntoa(endp->net_end.addr), |
| 518 | OSMUX_PORT); |
| 519 | } |
| 520 | LOGP(DMGCP, LOGL_DEBUG, |
| 521 | "sending OSMUX dummy load to %s CID %u\n", |
| 522 | inet_ntoa(endp->net_end.addr), endp->osmux.cid); |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 523 | |
| 524 | return mgcp_udp_send(osmux_fd.fd, &endp->net_end.addr, |
Holger Hans Peter Freyther | caf3dd0 | 2014-07-09 00:32:00 +0200 | [diff] [blame] | 525 | htons(OSMUX_PORT), buf, sizeof(buf)); |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 526 | } |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 527 | |
| 528 | /* bsc-nat allocates/releases the Osmux circuit ID */ |
| 529 | static uint8_t osmux_cid_bitmap[16]; |
| 530 | |
| 531 | int osmux_get_cid(void) |
| 532 | { |
| 533 | int i, j; |
| 534 | |
| 535 | for (i = 0; i < sizeof(osmux_cid_bitmap) / 8; i++) { |
| 536 | for (j = 0; j < 8; j++) { |
| 537 | if (osmux_cid_bitmap[i] & (1 << j)) |
| 538 | continue; |
| 539 | |
| 540 | osmux_cid_bitmap[i] |= (1 << j); |
| 541 | LOGP(DMGCP, LOGL_DEBUG, |
| 542 | "Allocating Osmux CID %u from pool\n", (i * 8) + j); |
| 543 | return (i * 8) + j; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | LOGP(DMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n"); |
| 548 | return -1; |
| 549 | } |
| 550 | |
| 551 | void osmux_put_cid(uint8_t osmux_cid) |
| 552 | { |
| 553 | LOGP(DMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid); |
| 554 | osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8)); |
| 555 | } |