Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 1 | /* OpenBSC Abis input driver for HSL Femto */ |
| 2 | |
| 3 | /* (C) 2011 by Harald Welte <laforge@gnumonks.org> |
| 4 | * (C) 2011 by On-Waves |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU Affero General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | /* HSL uses a much more primitive/simplified version of the IPA multiplex. |
| 24 | * |
| 25 | * They have taken out the nice parts like the ID_GET / ID_RESP for resolving |
| 26 | * the UNIT ID, as well as the keepalive ping/pong messages. Furthermore, the |
| 27 | * Stream Identifiers are fixed on the BTS side (RSL always 0, OML always 0xff) |
| 28 | * and both OML+RSL share a single TCP connection. |
| 29 | * |
| 30 | * Other oddities include the encapsulation of BSSGP messages in the L3_INFO IE |
| 31 | * of RSL |
| 32 | */ |
| 33 | |
| 34 | #include "internal.h" |
| 35 | |
| 36 | #include <stdio.h> |
| 37 | #include <unistd.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <errno.h> |
| 40 | #include <string.h> |
| 41 | #include <time.h> |
| 42 | #include <sys/fcntl.h> |
| 43 | #include <sys/socket.h> |
| 44 | #include <sys/ioctl.h> |
| 45 | #include <arpa/inet.h> |
| 46 | |
| 47 | #include <osmocom/core/select.h> |
| 48 | #include <osmocom/gsm/tlv.h> |
| 49 | #include <osmocom/core/msgb.h> |
Pablo Neira Ayuso | 177094b | 2011-06-07 12:21:51 +0200 | [diff] [blame] | 50 | #include <osmocom/abis/e1_input.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 51 | #include <osmocom/core/logging.h> |
Pablo Neira Ayuso | e47a978 | 2011-06-07 18:03:48 +0200 | [diff] [blame] | 52 | #include <osmocom/abis/ipaccess.h> |
Pablo Neira Ayuso | f67471f | 2011-06-07 11:25:49 +0200 | [diff] [blame] | 53 | #include <osmocom/core/socket.h> |
Pablo Neira Ayuso | 0b099b2 | 2011-06-09 13:14:11 +0200 | [diff] [blame] | 54 | #include <osmocom/abis/logging.h> |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 55 | #include <osmocom/abis/ipa.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 56 | #include <talloc.h> |
| 57 | |
| 58 | #define HSL_TCP_PORT 2500 |
| 59 | #define HSL_PROTO_DEBUG 0xdd |
| 60 | |
| 61 | #define PRIV_OML 1 |
| 62 | #define PRIV_RSL 2 |
| 63 | |
Pablo Neira Ayuso | 54b4979 | 2011-06-07 12:15:26 +0200 | [diff] [blame] | 64 | static void *tall_hsl_ctx; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 65 | |
| 66 | /* data structure for one E1 interface with A-bis */ |
| 67 | struct hsl_e1_handle { |
| 68 | struct osmo_fd listen_fd; |
| 69 | struct gsm_network *gsmnet; |
| 70 | }; |
| 71 | |
| 72 | static struct hsl_e1_handle *e1h; |
| 73 | |
| 74 | |
| 75 | #define TS1_ALLOC_SIZE 900 |
| 76 | |
| 77 | static int handle_ts1_read(struct osmo_fd *bfd) |
| 78 | { |
| 79 | struct e1inp_line *line = bfd->data; |
| 80 | unsigned int ts_nr = bfd->priv_nr; |
| 81 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 82 | struct e1inp_sign_link *link; |
| 83 | struct ipaccess_head *hh; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 84 | struct msgb *msg; |
| 85 | int ret = 0, error; |
| 86 | |
Pablo Neira Ayuso | 7a24940 | 2011-06-21 14:15:46 +0200 | [diff] [blame] | 87 | error = ipa_msg_recv(bfd->fd, &msg); |
| 88 | if (error <= 0) { |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 89 | if (e1i_ts->line->ops.error) |
| 90 | e1i_ts->line->ops.error(NULL, error); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 91 | if (error == 0) { |
| 92 | osmo_fd_unregister(bfd); |
| 93 | close(bfd->fd); |
| 94 | bfd->fd = -1; |
| 95 | } |
| 96 | return error; |
| 97 | } |
| 98 | DEBUGP(DMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg))); |
| 99 | |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 100 | hh = (struct ipaccess_head *) msg->data; |
| 101 | if (hh->proto == HSL_PROTO_DEBUG) { |
| 102 | LOGP(DINP, LOGL_NOTICE, "HSL debug: %s\n", msg->data + sizeof(*hh)); |
| 103 | msgb_free(msg); |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | /* HSL proprietary RSL extension */ |
| 108 | if (hh->proto == 0 && (msg->l2h[0] == 0x81 || msg->l2h[0] == 0x80)) { |
| 109 | if (!line->ops.sign_link_up) { |
| 110 | LOGP(DINP, LOGL_ERROR, "Fix your application, no " |
| 111 | "action set if the signalling link " |
| 112 | "becomes ready.\n"); |
| 113 | return -EINVAL; |
| 114 | } |
| 115 | ret = line->ops.sign_link_up(msg, line); |
| 116 | if (ret < 0) { |
| 117 | /* FIXME: close connection */ |
| 118 | if (line->ops.error) |
| 119 | line->ops.error(msg, -EBADMSG); |
| 120 | return ret; |
| 121 | } else if (ret == 1) |
| 122 | return 0; |
| 123 | /* else: continue... */ |
| 124 | } |
| 125 | |
| 126 | #ifdef HSL_SR_1_0 |
| 127 | /* HSL for whatever reason chose to use 0x81 instead of 0x80 for FOM */ |
| 128 | if (hh->proto == 255 && msg->l2h[0] == (ABIS_OM_MDISC_FOM | 0x01)) |
| 129 | msg->l2h[0] = ABIS_OM_MDISC_FOM; |
| 130 | #endif |
| 131 | link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0); |
| 132 | if (!link) { |
| 133 | LOGP(DINP, LOGL_ERROR, "no matching signalling link for " |
| 134 | "hh->proto=0x%02x\n", hh->proto); |
| 135 | msgb_free(msg); |
| 136 | return -EIO; |
| 137 | } |
| 138 | msg->dst = link; |
| 139 | |
| 140 | /* XXX: better use e1inp_ts_rx? */ |
| 141 | if (!e1i_ts->line->ops.sign_link) { |
| 142 | LOGP(DINP, LOGL_ERROR, "Fix your application, " |
| 143 | "no action set for signalling messages.\n"); |
| 144 | return -ENOENT; |
| 145 | } |
| 146 | e1i_ts->line->ops.sign_link(msg, link); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | static int ts_want_write(struct e1inp_ts *e1i_ts) |
| 152 | { |
| 153 | e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE; |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static void timeout_ts1_write(void *data) |
| 159 | { |
| 160 | struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data; |
| 161 | |
| 162 | /* trigger write of ts1, due to tx delay timer */ |
| 163 | ts_want_write(e1i_ts); |
| 164 | } |
| 165 | |
| 166 | static int handle_ts1_write(struct osmo_fd *bfd) |
| 167 | { |
| 168 | struct e1inp_line *line = bfd->data; |
| 169 | unsigned int ts_nr = bfd->priv_nr; |
| 170 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 171 | struct e1inp_sign_link *sign_link; |
| 172 | struct msgb *msg; |
| 173 | uint8_t proto; |
| 174 | int ret; |
| 175 | |
| 176 | bfd->when &= ~BSC_FD_WRITE; |
| 177 | |
| 178 | /* get the next msg for this timeslot */ |
| 179 | msg = e1inp_tx_ts(e1i_ts, &sign_link); |
| 180 | if (!msg) { |
| 181 | /* no message after tx delay timer */ |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | switch (sign_link->type) { |
| 186 | case E1INP_SIGN_OML: |
| 187 | proto = IPAC_PROTO_OML; |
| 188 | #ifdef HSL_SR_1_0 |
| 189 | /* HSL uses 0x81 for FOM for some reason */ |
| 190 | if (msg->data[0] == ABIS_OM_MDISC_FOM) |
| 191 | msg->data[0] = ABIS_OM_MDISC_FOM | 0x01; |
| 192 | #endif |
| 193 | break; |
| 194 | case E1INP_SIGN_RSL: |
| 195 | proto = IPAC_PROTO_RSL; |
| 196 | break; |
| 197 | default: |
| 198 | msgb_free(msg); |
| 199 | bfd->when |= BSC_FD_WRITE; /* come back for more msg */ |
| 200 | return -EINVAL; |
| 201 | } |
| 202 | |
| 203 | msg->l2h = msg->data; |
| 204 | ipaccess_prepend_header(msg, sign_link->tei); |
| 205 | |
| 206 | DEBUGP(DMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg))); |
| 207 | |
| 208 | ret = send(bfd->fd, msg->data, msg->len, 0); |
| 209 | msgb_free(msg); |
| 210 | |
| 211 | /* set tx delay timer for next event */ |
| 212 | e1i_ts->sign.tx_timer.cb = timeout_ts1_write; |
| 213 | e1i_ts->sign.tx_timer.data = e1i_ts; |
| 214 | |
| 215 | /* Reducing this might break the nanoBTS 900 init. */ |
| 216 | osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay); |
| 217 | |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | /* callback from select.c in case one of the fd's can be read/written */ |
| 222 | static int hsl_fd_cb(struct osmo_fd *bfd, unsigned int what) |
| 223 | { |
| 224 | struct e1inp_line *line = bfd->data; |
| 225 | unsigned int ts_nr = bfd->priv_nr; |
| 226 | unsigned int idx = ts_nr-1; |
| 227 | struct e1inp_ts *e1i_ts; |
| 228 | int rc = 0; |
| 229 | |
| 230 | /* In case of early RSL we might not yet have a line */ |
| 231 | |
| 232 | if (line) |
| 233 | e1i_ts = &line->ts[idx]; |
| 234 | |
| 235 | if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) { |
| 236 | if (what & BSC_FD_READ) |
| 237 | rc = handle_ts1_read(bfd); |
| 238 | if (what & BSC_FD_WRITE) |
| 239 | rc = handle_ts1_write(bfd); |
| 240 | } else |
| 241 | LOGP(DINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type); |
| 242 | |
| 243 | return rc; |
| 244 | } |
| 245 | |
Pablo Neira Ayuso | c00ee73 | 2011-06-21 12:22:49 +0200 | [diff] [blame] | 246 | static int hsl_line_update(struct e1inp_line *line, |
| 247 | enum e1inp_line_role role, const char *addr); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 248 | |
| 249 | struct e1inp_driver hsl_driver = { |
| 250 | .name = "hsl", |
| 251 | .want_write = ts_want_write, |
| 252 | .line_update = hsl_line_update, |
| 253 | .default_delay = 0, |
| 254 | }; |
| 255 | |
| 256 | /* callback of the OML listening filedescriptor */ |
| 257 | static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what) |
| 258 | { |
| 259 | int ret; |
| 260 | int idx = 0; |
| 261 | int i; |
| 262 | struct e1inp_line *line = listen_bfd->data; |
| 263 | struct e1inp_ts *e1i_ts; |
| 264 | struct osmo_fd *bfd; |
| 265 | struct sockaddr_in sa; |
| 266 | socklen_t sa_len = sizeof(sa); |
| 267 | |
| 268 | if (!(what & BSC_FD_READ)) |
| 269 | return 0; |
| 270 | |
| 271 | ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 272 | if (ret < 0) { |
| 273 | perror("accept"); |
| 274 | return ret; |
| 275 | } |
| 276 | LOGP(DINP, LOGL_NOTICE, "accept()ed new HSL link from %s\n", |
| 277 | inet_ntoa(sa.sin_addr)); |
| 278 | |
| 279 | /* create virrtual E1 timeslots for signalling */ |
| 280 | e1inp_ts_config_sign(&line->ts[1-1], line); |
| 281 | |
| 282 | /* initialize the fds */ |
| 283 | for (i = 0; i < ARRAY_SIZE(line->ts); ++i) |
| 284 | line->ts[i].driver.ipaccess.fd.fd = -1; |
| 285 | |
| 286 | e1i_ts = &line->ts[idx]; |
| 287 | |
| 288 | bfd = &e1i_ts->driver.ipaccess.fd; |
| 289 | bfd->fd = ret; |
| 290 | bfd->data = line; |
| 291 | bfd->priv_nr = PRIV_OML; |
| 292 | bfd->cb = hsl_fd_cb; |
| 293 | bfd->when = BSC_FD_READ; |
| 294 | ret = osmo_fd_register(bfd); |
| 295 | if (ret < 0) { |
| 296 | LOGP(DINP, LOGL_ERROR, "could not register FD\n"); |
| 297 | close(bfd->fd); |
| 298 | talloc_free(line); |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | return ret; |
| 303 | } |
| 304 | |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 305 | static int hsl_bts_process(struct ipa_client_link *link, struct msgb *msg) |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 306 | { |
| 307 | /* XXX: not implemented yet. */ |
| 308 | return 0; |
| 309 | } |
| 310 | |
Pablo Neira Ayuso | c00ee73 | 2011-06-21 12:22:49 +0200 | [diff] [blame] | 311 | static int hsl_line_update(struct e1inp_line *line, |
| 312 | enum e1inp_line_role role, const char *addr) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 313 | { |
| 314 | int ret = -ENOENT; |
| 315 | |
| 316 | switch(role) { |
| 317 | case E1INP_LINE_R_BSC: |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 318 | LOGP(DINP, LOGL_NOTICE, "enabling hsl BSC mode\n"); |
| 319 | |
Pablo Neira Ayuso | f67471f | 2011-06-07 11:25:49 +0200 | [diff] [blame] | 320 | ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 321 | addr, HSL_TCP_PORT, OSMO_SOCK_F_BIND); |
Pablo Neira Ayuso | f67471f | 2011-06-07 11:25:49 +0200 | [diff] [blame] | 322 | if (ret < 0) |
| 323 | return ret; |
| 324 | |
| 325 | e1h->listen_fd.fd = ret; |
| 326 | e1h->listen_fd.when |= BSC_FD_READ; |
| 327 | e1h->listen_fd.cb = listen_fd_cb; |
| 328 | e1h->listen_fd.data = line; |
| 329 | |
| 330 | if (osmo_fd_register(&e1h->listen_fd) < 0) { |
| 331 | close(ret); |
| 332 | return ret; |
| 333 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 334 | break; |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 335 | case E1INP_LINE_R_BTS: { |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 336 | struct ipa_client_link *link; |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 337 | |
| 338 | LOGP(DINP, LOGL_NOTICE, "enabling hsl BTS mode\n"); |
| 339 | |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 340 | link = ipa_client_link_create(tall_hsl_ctx, line, addr, |
Pablo Neira Ayuso | e009f4a | 2011-06-23 13:36:34 +0200 | [diff] [blame] | 341 | HSL_TCP_PORT, hsl_bts_process, |
| 342 | NULL); |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 343 | if (link == NULL) { |
| 344 | LOGP(DINP, LOGL_ERROR, "cannot create BTS link: %s\n", |
| 345 | strerror(errno)); |
| 346 | return -ENOMEM; |
| 347 | } |
| 348 | if (ipa_client_link_open(link) < 0) { |
| 349 | LOGP(DINP, LOGL_ERROR, "cannot open BTS link: %s\n", |
| 350 | strerror(errno)); |
| 351 | ipa_client_link_close(link); |
| 352 | ipa_client_link_destroy(link); |
| 353 | return -EIO; |
| 354 | } |
| 355 | ret = 0; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 356 | break; |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 357 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 358 | default: |
| 359 | break; |
| 360 | } |
| 361 | return ret; |
| 362 | } |
| 363 | |
| 364 | int hsl_setup(struct gsm_network *gsmnet) |
| 365 | { |
| 366 | e1h->gsmnet = gsmnet; |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | void e1inp_hsl_init(void) |
| 371 | { |
Pablo Neira Ayuso | 54b4979 | 2011-06-07 12:15:26 +0200 | [diff] [blame] | 372 | tall_hsl_ctx = talloc_named_const(libosmo_abis_ctx, 1, "hsl"); |
| 373 | |
| 374 | e1h = talloc_zero(tall_hsl_ctx, struct hsl_e1_handle); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 375 | if (!e1h) |
| 376 | return; |
| 377 | |
| 378 | e1inp_driver_register(&hsl_driver); |
| 379 | } |