Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [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 <inttypes.h> /* for PRIu64 */ |
| 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 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 23 | #include <osmocom/mgcp/mgcp.h> |
| 24 | #include <osmocom/mgcp/mgcp_internal.h> |
| 25 | #include <osmocom/mgcp/osmux.h> |
| 26 | #include <osmocom/mgcp/mgcp_conn.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 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 | struct in_addr rem_addr; |
| 36 | int rem_port; |
| 37 | int refcnt; |
| 38 | }; |
| 39 | |
| 40 | static void *osmux; |
| 41 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 42 | /* Deliver OSMUX batch to the remote end */ |
| 43 | static void osmux_deliver_cb(struct msgb *batch_msg, void *data) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 44 | { |
| 45 | struct osmux_handle *handle = data; |
| 46 | struct sockaddr_in out = { |
| 47 | .sin_family = AF_INET, |
| 48 | .sin_port = handle->rem_port, |
| 49 | }; |
| 50 | |
| 51 | memcpy(&out.sin_addr, &handle->rem_addr, sizeof(handle->rem_addr)); |
| 52 | sendto(osmux_fd.fd, batch_msg->data, batch_msg->len, 0, |
| 53 | (struct sockaddr *)&out, sizeof(out)); |
| 54 | msgb_free(batch_msg); |
| 55 | } |
| 56 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 57 | /* Lookup existing OSMUX handle for specified destination address. */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 58 | static struct osmux_handle * |
| 59 | osmux_handle_find_get(struct in_addr *addr, int rem_port) |
| 60 | { |
| 61 | struct osmux_handle *h; |
| 62 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 63 | llist_for_each_entry(h, &osmux_handle_list, head) { |
| 64 | if (memcmp(&h->rem_addr, addr, sizeof(struct in_addr)) == 0 && |
| 65 | h->rem_port == rem_port) { |
| 66 | LOGP(DLMGCP, LOGL_DEBUG, "using existing OSMUX handle " |
| 67 | "for addr=%s:%d\n", |
| 68 | inet_ntoa(*addr), ntohs(rem_port)); |
| 69 | h->refcnt++; |
| 70 | return h; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return NULL; |
| 75 | } |
| 76 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 77 | /* Put down no longer needed OSMUX handle */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 78 | static void osmux_handle_put(struct osmux_in_handle *in) |
| 79 | { |
| 80 | struct osmux_handle *h; |
| 81 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 82 | llist_for_each_entry(h, &osmux_handle_list, head) { |
| 83 | if (h->in == in) { |
| 84 | if (--h->refcnt == 0) { |
| 85 | LOGP(DLMGCP, LOGL_INFO, |
| 86 | "Releasing unused osmux handle for %s:%d\n", |
| 87 | inet_ntoa(h->rem_addr), |
| 88 | ntohs(h->rem_port)); |
| 89 | LOGP(DLMGCP, 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); |
| 96 | llist_del(&h->head); |
| 97 | osmux_xfrm_input_fini(h->in); |
| 98 | talloc_free(h); |
| 99 | } |
| 100 | return; |
| 101 | } |
| 102 | } |
| 103 | LOGP(DLMGCP, LOGL_ERROR, "cannot find Osmux input handle %p\n", in); |
| 104 | } |
| 105 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 106 | /* Allocate free OSMUX handle */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 107 | static struct osmux_handle * |
| 108 | osmux_handle_alloc(struct mgcp_config *cfg, struct in_addr *addr, int rem_port) |
| 109 | { |
| 110 | struct osmux_handle *h; |
| 111 | |
| 112 | h = talloc_zero(osmux, struct osmux_handle); |
| 113 | if (!h) |
| 114 | return NULL; |
| 115 | h->rem_addr = *addr; |
| 116 | h->rem_port = rem_port; |
| 117 | h->refcnt++; |
| 118 | |
| 119 | h->in = talloc_zero(h, struct osmux_in_handle); |
| 120 | if (!h->in) { |
| 121 | talloc_free(h); |
| 122 | return NULL; |
| 123 | } |
| 124 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 125 | /* sequence number to start OSMUX message from */ |
| 126 | h->in->osmux_seq = 0; |
| 127 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 128 | h->in->batch_factor = cfg->osmux_batch; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 129 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 130 | /* If batch size is zero, the library defaults to 1470 bytes. */ |
| 131 | h->in->batch_size = cfg->osmux_batch_size; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 132 | h->in->deliver = osmux_deliver_cb; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 133 | osmux_xfrm_input_init(h->in); |
| 134 | h->in->data = h; |
| 135 | |
| 136 | llist_add(&h->head, &osmux_handle_list); |
| 137 | |
| 138 | LOGP(DLMGCP, LOGL_DEBUG, "created new OSMUX handle for addr=%s:%d\n", |
| 139 | inet_ntoa(*addr), ntohs(rem_port)); |
| 140 | |
| 141 | return h; |
| 142 | } |
| 143 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 144 | /* Lookup existing handle for a specified address, if the handle can not be |
| 145 | * foud a the function will automatically allocate one */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 146 | static struct osmux_in_handle * |
| 147 | osmux_handle_lookup(struct mgcp_config *cfg, struct in_addr *addr, int rem_port) |
| 148 | { |
| 149 | struct osmux_handle *h; |
| 150 | |
| 151 | h = osmux_handle_find_get(addr, rem_port); |
| 152 | if (h != NULL) |
| 153 | return h->in; |
| 154 | |
| 155 | h = osmux_handle_alloc(cfg, addr, rem_port); |
| 156 | if (h == NULL) |
| 157 | return NULL; |
| 158 | |
| 159 | return h->in; |
| 160 | } |
| 161 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 162 | /*! send RTP packet through OSMUX connection. |
| 163 | * \param[in] buf rtp data |
| 164 | * \param[in] buf_len length of rtp data |
| 165 | * \param[in] conn associated RTP connection |
| 166 | * \returns 0 on success, -1 on ERROR */ |
| 167 | int osmux_xfrm_to_osmux(char *buf, int buf_len, struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 168 | { |
| 169 | int ret; |
| 170 | struct msgb *msg; |
| 171 | |
| 172 | msg = msgb_alloc(4096, "RTP"); |
| 173 | if (!msg) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 174 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 175 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 176 | memcpy(msg->data, buf, buf_len); |
| 177 | msgb_put(msg, buf_len); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 178 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 179 | while ((ret = osmux_xfrm_input(conn->osmux.in, msg, conn->osmux.cid)) > 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 180 | /* batch full, build and deliver it */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 181 | osmux_xfrm_input_deliver(conn->osmux.in); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 186 | /* Lookup the endpoint that corresponds to the specified address (port) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 187 | static struct mgcp_endpoint * |
| 188 | endpoint_lookup(struct mgcp_config *cfg, int cid, |
| 189 | struct in_addr *from_addr, int type) |
| 190 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 191 | struct mgcp_endpoint *endp = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 192 | int i; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 193 | struct mgcp_conn_rtp *conn_net = NULL; |
| 194 | struct mgcp_conn_rtp *conn_bts = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 195 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 196 | for (i=0; i<cfg->trunk.number_endpoints; i++) { |
| 197 | struct in_addr *this; |
| 198 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 199 | endp = &cfg->trunk.endpoints[i]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 200 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 201 | #if 0 |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 202 | if (!tmp->allocated) |
| 203 | continue; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 204 | #endif |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 205 | |
| 206 | switch(type) { |
| 207 | case MGCP_DEST_NET: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 208 | /* FIXME: Get rid of CONN_ID_XXX! */ |
| 209 | conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET); |
| 210 | this = &conn_net->end.addr; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 211 | break; |
| 212 | case MGCP_DEST_BTS: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 213 | /* FIXME: Get rid of CONN_ID_XXX! */ |
| 214 | conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS); |
| 215 | this = &conn_bts->end.addr; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 216 | break; |
| 217 | default: |
| 218 | /* Should not ever happen */ |
| 219 | LOGP(DLMGCP, LOGL_ERROR, "Bad type %d. Fix your code.\n", type); |
| 220 | return NULL; |
| 221 | } |
| 222 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 223 | /* FIXME: Get rid of CONN_ID_XXX! */ |
| 224 | conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET); |
| 225 | if (conn_net->osmux.cid == cid && this->s_addr == from_addr->s_addr) |
| 226 | return endp; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | LOGP(DLMGCP, LOGL_ERROR, "Cannot find endpoint with cid=%d\n", cid); |
| 230 | |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | static void scheduled_tx_net_cb(struct msgb *msg, void *data) |
| 235 | { |
| 236 | struct mgcp_endpoint *endp = data; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 237 | struct mgcp_conn_rtp *conn_net = NULL; |
| 238 | struct mgcp_conn_rtp *conn_bts = NULL; |
| 239 | |
| 240 | /* FIXME: Get rid of CONN_ID_XXX! */ |
| 241 | conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS); |
| 242 | conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET); |
| 243 | if (!conn_bts || !conn_net) |
| 244 | return; |
| 245 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 246 | struct sockaddr_in addr = { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 247 | .sin_addr = conn_net->end.addr, |
| 248 | .sin_port = conn_net->end.rtp_port, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 249 | }; |
| 250 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 251 | conn_bts->end.octets_tx += msg->len; |
| 252 | conn_bts->end.packets_tx++; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 253 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 254 | /* Send RTP data to NET */ |
| 255 | /* FIXME: Get rid of conn_bts and conn_net! */ |
| 256 | mgcp_send(endp, 1, &addr, (char *)msg->data, msg->len, |
| 257 | conn_bts, conn_net); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 258 | msgb_free(msg); |
| 259 | } |
| 260 | |
| 261 | static void scheduled_tx_bts_cb(struct msgb *msg, void *data) |
| 262 | { |
| 263 | struct mgcp_endpoint *endp = data; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 264 | struct mgcp_conn_rtp *conn_net = NULL; |
| 265 | struct mgcp_conn_rtp *conn_bts = NULL; |
| 266 | |
| 267 | /* FIXME: Get rid of CONN_ID_XXX! */ |
| 268 | conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS); |
| 269 | conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET); |
| 270 | if (!conn_bts || !conn_net) |
| 271 | return; |
| 272 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 273 | struct sockaddr_in addr = { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 274 | .sin_addr = conn_bts->end.addr, |
| 275 | .sin_port = conn_bts->end.rtp_port, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 276 | }; |
| 277 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 278 | conn_net->end.octets_tx += msg->len; |
| 279 | conn_net->end.packets_tx++; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 280 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 281 | /* Send RTP data to BTS */ |
| 282 | /* FIXME: Get rid of conn_bts and conn_net! */ |
| 283 | mgcp_send(endp, 1, &addr, (char *)msg->data, msg->len, |
| 284 | conn_net, conn_bts); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 285 | msgb_free(msg); |
| 286 | } |
| 287 | |
| 288 | static struct msgb *osmux_recv(struct osmo_fd *ofd, struct sockaddr_in *addr) |
| 289 | { |
| 290 | struct msgb *msg; |
| 291 | socklen_t slen = sizeof(*addr); |
| 292 | int ret; |
| 293 | |
| 294 | msg = msgb_alloc(4096, "OSMUX"); |
| 295 | if (!msg) { |
| 296 | LOGP(DLMGCP, LOGL_ERROR, "cannot allocate message\n"); |
| 297 | return NULL; |
| 298 | } |
| 299 | ret = recvfrom(ofd->fd, msg->data, msg->data_len, 0, |
| 300 | (struct sockaddr *)addr, &slen); |
| 301 | if (ret <= 0) { |
| 302 | msgb_free(msg); |
| 303 | LOGP(DLMGCP, LOGL_ERROR, "cannot receive message\n"); |
| 304 | return NULL; |
| 305 | } |
| 306 | msgb_put(msg, ret); |
| 307 | |
| 308 | return msg; |
| 309 | } |
| 310 | |
| 311 | #define osmux_chunk_length(msg, rem) (rem - msg->len); |
| 312 | |
| 313 | int osmux_read_from_bsc_nat_cb(struct osmo_fd *ofd, unsigned int what) |
| 314 | { |
| 315 | struct msgb *msg; |
| 316 | struct osmux_hdr *osmuxh; |
| 317 | struct llist_head list; |
| 318 | struct sockaddr_in addr; |
| 319 | struct mgcp_config *cfg = ofd->data; |
| 320 | uint32_t rem; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 321 | struct mgcp_conn_rtp *conn_net = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 322 | |
| 323 | msg = osmux_recv(ofd, &addr); |
| 324 | if (!msg) |
| 325 | return -1; |
| 326 | |
| 327 | /* not any further processing dummy messages */ |
| 328 | if (msg->data[0] == MGCP_DUMMY_LOAD) |
| 329 | goto out; |
| 330 | |
| 331 | rem = msg->len; |
| 332 | while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) { |
| 333 | struct mgcp_endpoint *endp; |
| 334 | |
| 335 | /* Yes, we use MGCP_DEST_NET to locate the origin */ |
| 336 | endp = endpoint_lookup(cfg, osmuxh->circuit_id, |
| 337 | &addr.sin_addr, MGCP_DEST_NET); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 338 | |
| 339 | /* FIXME: Get rid of CONN_ID_XXX! */ |
| 340 | conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET); |
| 341 | if (!conn_net) |
| 342 | goto out; |
| 343 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 344 | if (!endp) { |
| 345 | LOGP(DLMGCP, LOGL_ERROR, |
| 346 | "Cannot find an endpoint for circuit_id=%d\n", |
| 347 | osmuxh->circuit_id); |
| 348 | goto out; |
| 349 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 350 | conn_net->osmux.stats.octets += osmux_chunk_length(msg, rem); |
| 351 | conn_net->osmux.stats.chunks++; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 352 | rem = msg->len; |
| 353 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 354 | osmux_xfrm_output(osmuxh, &conn_net->osmux.out, &list); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 355 | osmux_tx_sched(&list, scheduled_tx_bts_cb, endp); |
| 356 | } |
| 357 | out: |
| 358 | msgb_free(msg); |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | /* This is called from the bsc-nat */ |
| 363 | static int osmux_handle_dummy(struct mgcp_config *cfg, struct sockaddr_in *addr, |
| 364 | struct msgb *msg) |
| 365 | { |
| 366 | struct mgcp_endpoint *endp; |
| 367 | uint8_t osmux_cid; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 368 | struct mgcp_conn_rtp *conn_net = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 369 | |
| 370 | if (msg->len < 1 + sizeof(osmux_cid)) { |
| 371 | LOGP(DLMGCP, LOGL_ERROR, |
| 372 | "Discarding truncated Osmux dummy load\n"); |
| 373 | goto out; |
| 374 | } |
| 375 | |
| 376 | LOGP(DLMGCP, LOGL_DEBUG, "Received Osmux dummy load from %s\n", |
| 377 | inet_ntoa(addr->sin_addr)); |
| 378 | |
| 379 | if (!cfg->osmux) { |
| 380 | LOGP(DLMGCP, LOGL_ERROR, |
| 381 | "bsc wants to use Osmux but bsc-nat did not request it\n"); |
| 382 | goto out; |
| 383 | } |
| 384 | |
| 385 | /* extract the osmux CID from the dummy message */ |
| 386 | memcpy(&osmux_cid, &msg->data[1], sizeof(osmux_cid)); |
| 387 | |
| 388 | endp = endpoint_lookup(cfg, osmux_cid, &addr->sin_addr, MGCP_DEST_BTS); |
| 389 | if (!endp) { |
| 390 | LOGP(DLMGCP, LOGL_ERROR, |
| 391 | "Cannot find endpoint for Osmux CID %d\n", osmux_cid); |
| 392 | goto out; |
| 393 | } |
| 394 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 395 | conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET); |
| 396 | if (!conn_net) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 397 | goto out; |
| 398 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 399 | if (conn_net->osmux.state == OSMUX_STATE_ENABLED) |
| 400 | goto out; |
| 401 | |
| 402 | if (osmux_enable_conn(endp, conn_net, &addr->sin_addr, addr->sin_port) < 0 ) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 403 | LOGP(DLMGCP, LOGL_ERROR, |
| 404 | "Could not enable osmux in endpoint %d\n", |
| 405 | ENDPOINT_NUMBER(endp)); |
| 406 | goto out; |
| 407 | } |
| 408 | |
| 409 | LOGP(DLMGCP, LOGL_INFO, "Enabling osmux in endpoint %d for %s:%u\n", |
| 410 | ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr), |
| 411 | ntohs(addr->sin_port)); |
| 412 | out: |
| 413 | msgb_free(msg); |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | int osmux_read_from_bsc_cb(struct osmo_fd *ofd, unsigned int what) |
| 418 | { |
| 419 | struct msgb *msg; |
| 420 | struct osmux_hdr *osmuxh; |
| 421 | struct llist_head list; |
| 422 | struct sockaddr_in addr; |
| 423 | struct mgcp_config *cfg = ofd->data; |
| 424 | uint32_t rem; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 425 | struct mgcp_conn_rtp *conn_net = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 426 | |
| 427 | msg = osmux_recv(ofd, &addr); |
| 428 | if (!msg) |
| 429 | return -1; |
| 430 | |
| 431 | /* not any further processing dummy messages */ |
| 432 | if (msg->data[0] == MGCP_DUMMY_LOAD) |
| 433 | return osmux_handle_dummy(cfg, &addr, msg); |
| 434 | |
| 435 | rem = msg->len; |
| 436 | while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) { |
| 437 | struct mgcp_endpoint *endp; |
| 438 | |
| 439 | /* Yes, we use MGCP_DEST_BTS to locate the origin */ |
| 440 | endp = endpoint_lookup(cfg, osmuxh->circuit_id, |
| 441 | &addr.sin_addr, MGCP_DEST_BTS); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 442 | |
| 443 | /* FIXME: Get rid of CONN_ID_XXX! */ |
| 444 | conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET); |
| 445 | if (!conn_net) |
| 446 | goto out; |
| 447 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 448 | if (!endp) { |
| 449 | LOGP(DLMGCP, LOGL_ERROR, |
| 450 | "Cannot find an endpoint for circuit_id=%d\n", |
| 451 | osmuxh->circuit_id); |
| 452 | goto out; |
| 453 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 454 | conn_net->osmux.stats.octets += osmux_chunk_length(msg, rem); |
| 455 | conn_net->osmux.stats.chunks++; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 456 | rem = msg->len; |
| 457 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 458 | osmux_xfrm_output(osmuxh, &conn_net->osmux.out, &list); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 459 | osmux_tx_sched(&list, scheduled_tx_net_cb, endp); |
| 460 | } |
| 461 | out: |
| 462 | msgb_free(msg); |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | int osmux_init(int role, struct mgcp_config *cfg) |
| 467 | { |
| 468 | int ret; |
| 469 | |
| 470 | switch(role) { |
| 471 | case OSMUX_ROLE_BSC: |
| 472 | osmux_fd.cb = osmux_read_from_bsc_nat_cb; |
| 473 | break; |
| 474 | case OSMUX_ROLE_BSC_NAT: |
| 475 | osmux_fd.cb = osmux_read_from_bsc_cb; |
| 476 | break; |
| 477 | default: |
| 478 | LOGP(DLMGCP, LOGL_ERROR, "wrong role for OSMUX\n"); |
| 479 | return -1; |
| 480 | } |
| 481 | osmux_fd.data = cfg; |
| 482 | |
| 483 | ret = mgcp_create_bind(cfg->osmux_addr, &osmux_fd, cfg->osmux_port); |
| 484 | if (ret < 0) { |
| 485 | LOGP(DLMGCP, LOGL_ERROR, "cannot bind OSMUX socket\n"); |
| 486 | return ret; |
| 487 | } |
| 488 | mgcp_set_ip_tos(osmux_fd.fd, cfg->endp_dscp); |
| 489 | osmux_fd.when |= BSC_FD_READ; |
| 490 | |
| 491 | ret = osmo_fd_register(&osmux_fd); |
| 492 | if (ret < 0) { |
| 493 | LOGP(DLMGCP, LOGL_ERROR, "cannot register OSMUX socket\n"); |
| 494 | return ret; |
| 495 | } |
| 496 | cfg->osmux_init = 1; |
| 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 501 | /*! enable OSXMUX circuit for a specified connection. |
| 502 | * \param[in] endp mgcp endpoint (configuration) |
| 503 | * \param[in] conn connection to disable |
| 504 | * \param[in] addr IP address of remote OSMUX endpoint |
| 505 | * \param[in] port portnumber of the remote OSMUX endpoint |
| 506 | * \returns 0 on success, -1 on ERROR */ |
| 507 | int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn, |
| 508 | struct in_addr *addr, uint16_t port) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 509 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 510 | /*! If osmux is enabled, initialize the output handler. This handler is |
| 511 | * used to reconstruct the RTP flow from osmux. The RTP SSRC is |
| 512 | * allocated based on the circuit ID (conn_net->osmux.cid), which is unique |
| 513 | * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP |
| 514 | * SSRC space (2^32) by the 256 possible circuit IDs, then randomly |
| 515 | * select one value from that window. Thus, we have no chance to have |
| 516 | * overlapping RTP SSRC traveling to the BTSes behind the BSC, |
| 517 | * similarly, for flows traveling to the MSC. |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 518 | */ |
| 519 | static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 520 | uint16_t osmux_dummy = endp->cfg->osmux_dummy; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 521 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 522 | /* Check if osmux is enabled for the specified connection */ |
| 523 | if (conn->osmux.state == OSMUX_STATE_DISABLED) { |
| 524 | LOGP(DLMGCP, LOGL_ERROR, "OSMUX not enabled for conn:%s\n", |
| 525 | mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 526 | return -1; |
| 527 | } |
| 528 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 529 | osmux_xfrm_output_init(&conn->osmux.out, |
| 530 | (conn->osmux.cid * rtp_ssrc_winlen) + |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 531 | (random() % rtp_ssrc_winlen)); |
| 532 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 533 | conn->osmux.in = osmux_handle_lookup(endp->cfg, addr, port); |
| 534 | if (!conn->osmux.in) { |
| 535 | LOGP(DLMGCP, LOGL_ERROR, "Cannot allocate input osmux handle for conn:%s\n", |
| 536 | mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 537 | return -1; |
| 538 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 539 | if (!osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.cid, osmux_dummy)) { |
| 540 | LOGP(DLMGCP, LOGL_ERROR, "Cannot open osmux circuit %u for conn:%s\n", |
| 541 | conn->osmux.cid, mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 542 | return -1; |
| 543 | } |
| 544 | |
| 545 | switch (endp->cfg->role) { |
| 546 | case MGCP_BSC_NAT: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 547 | conn->type = MGCP_OSMUX_BSC_NAT; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 548 | break; |
| 549 | case MGCP_BSC: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 550 | conn->type = MGCP_OSMUX_BSC; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 551 | break; |
| 552 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 553 | |
| 554 | conn->osmux.state = OSMUX_STATE_ENABLED; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 555 | |
| 556 | return 0; |
| 557 | } |
| 558 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 559 | /*! disable OSXMUX circuit for a specified connection. |
| 560 | * \param[in] conn connection to disable */ |
| 561 | void osmux_disable_conn(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 562 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 563 | if (!conn) |
| 564 | return; |
| 565 | |
| 566 | if (conn->osmux.state != OSMUX_STATE_ENABLED) |
| 567 | return; |
| 568 | |
| 569 | LOGP(DLMGCP, LOGL_INFO, "Releasing connection %u using Osmux CID %u\n", |
| 570 | conn->conn->id, conn->osmux.cid); |
| 571 | osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.cid); |
| 572 | conn->osmux.state = OSMUX_STATE_DISABLED; |
| 573 | conn->osmux.cid = -1; |
| 574 | osmux_handle_put(conn->osmux.in); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 575 | } |
| 576 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 577 | /*! relase OSXMUX cid, that had been allocated to this connection. |
| 578 | * \param[in] conn connection with OSMUX cid to release */ |
| 579 | void osmux_release_cid(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 580 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 581 | if (!conn) |
| 582 | return; |
| 583 | |
| 584 | if (conn->osmux.state != OSMUX_STATE_ENABLED) |
| 585 | return; |
| 586 | |
| 587 | if (conn->osmux.allocated_cid >= 0) |
| 588 | osmux_put_cid(conn->osmux.allocated_cid); |
| 589 | conn->osmux.allocated_cid = -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 590 | } |
| 591 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 592 | /*! allocate OSXMUX cid to connection. |
| 593 | * \param[in] conn connection for which we allocate the OSMUX cid*/ |
| 594 | void osmux_allocate_cid(struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 595 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 596 | osmux_release_cid(conn); |
| 597 | conn->osmux.allocated_cid = osmux_get_cid(); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 598 | } |
| 599 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 600 | /*! send RTP dummy packet to OSMUX connection port. |
| 601 | * \param[in] endp mcgp endpoint that holds the RTP connection |
| 602 | * \param[in] conn associated RTP connection |
| 603 | * \returns bytes sent, -1 on error */ |
| 604 | int osmux_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 605 | { |
| 606 | char buf[1 + sizeof(uint8_t)]; |
| 607 | struct in_addr addr_unset = {}; |
| 608 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 609 | /*! The dummy packet will not be sent via the actual OSMUX connection, |
| 610 | * instead it is sent out of band to port where the remote OSMUX |
| 611 | * multplexer is listening. The goal is to ensure that the connection |
| 612 | * is kept open */ |
| 613 | |
| 614 | /*! We don't need to send the dummy load for osmux so often as another |
| 615 | * endpoint may have already punched the hole in the firewall. This |
| 616 | * approach is simple though. */ |
| 617 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 618 | buf[0] = MGCP_DUMMY_LOAD; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 619 | memcpy(&buf[1], &conn->osmux.cid, sizeof(conn->osmux.cid)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 620 | |
| 621 | /* Wait until we have the connection information from MDCX */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 622 | if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 623 | return 0; |
| 624 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 625 | if (conn->osmux.state == OSMUX_STATE_ACTIVATING) { |
| 626 | if (osmux_enable_conn(endp, conn, &conn->end.addr, |
| 627 | htons(endp->cfg->osmux_port)) < 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 628 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 629 | "Could not activate osmux for conn:%s\n", |
| 630 | mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 631 | } |
| 632 | LOGP(DLMGCP, LOGL_ERROR, |
| 633 | "Osmux CID %u for %s:%u is now enabled\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 634 | conn->osmux.cid, inet_ntoa(conn->end.addr), |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 635 | endp->cfg->osmux_port); |
| 636 | } |
| 637 | LOGP(DLMGCP, LOGL_DEBUG, |
| 638 | "sending OSMUX dummy load to %s CID %u\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 639 | inet_ntoa(conn->end.addr), conn->osmux.cid); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 640 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 641 | return mgcp_udp_send(osmux_fd.fd, &conn->end.addr, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 642 | htons(endp->cfg->osmux_port), buf, sizeof(buf)); |
| 643 | } |
| 644 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 645 | /*! bsc-nat allocates/releases the OSMUX cids (Circuit IDs). */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 646 | static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1) / 8]; |
| 647 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 648 | /*! count the number of taken OSMUX cids. |
| 649 | * \returns number of OSMUX cids in use */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 650 | int osmux_used_cid(void) |
| 651 | { |
| 652 | int i, j, used = 0; |
| 653 | |
| 654 | for (i = 0; i < sizeof(osmux_cid_bitmap); i++) { |
| 655 | for (j = 0; j < 8; j++) { |
| 656 | if (osmux_cid_bitmap[i] & (1 << j)) |
| 657 | used += 1; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | return used; |
| 662 | } |
| 663 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 664 | /*! take a free OSMUX cid. |
| 665 | * \returns OSMUX cid */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 666 | int osmux_get_cid(void) |
| 667 | { |
| 668 | int i, j; |
| 669 | |
| 670 | for (i = 0; i < sizeof(osmux_cid_bitmap); i++) { |
| 671 | for (j = 0; j < 8; j++) { |
| 672 | if (osmux_cid_bitmap[i] & (1 << j)) |
| 673 | continue; |
| 674 | |
| 675 | osmux_cid_bitmap[i] |= (1 << j); |
| 676 | LOGP(DLMGCP, LOGL_DEBUG, |
| 677 | "Allocating Osmux CID %u from pool\n", (i * 8) + j); |
| 678 | return (i * 8) + j; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | LOGP(DLMGCP, LOGL_ERROR, "All Osmux circuits are in use!\n"); |
| 683 | return -1; |
| 684 | } |
| 685 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 686 | /*! put back a no longer used OSMUX cid. |
| 687 | * \param[in] osmux_cid OSMUX cid */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 688 | void osmux_put_cid(uint8_t osmux_cid) |
| 689 | { |
| 690 | LOGP(DLMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid); |
| 691 | osmux_cid_bitmap[osmux_cid / 8] &= ~(1 << (osmux_cid % 8)); |
| 692 | } |