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