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> |
Harald Welte | 3e1d84b | 2011-08-19 13:32:18 +0200 | [diff] [blame] | 42 | #include <inttypes.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 43 | #include <sys/fcntl.h> |
| 44 | #include <sys/socket.h> |
| 45 | #include <sys/ioctl.h> |
| 46 | #include <arpa/inet.h> |
| 47 | |
| 48 | #include <osmocom/core/select.h> |
| 49 | #include <osmocom/gsm/tlv.h> |
| 50 | #include <osmocom/core/msgb.h> |
Pablo Neira Ayuso | 177094b | 2011-06-07 12:21:51 +0200 | [diff] [blame] | 51 | #include <osmocom/abis/e1_input.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 52 | #include <osmocom/core/logging.h> |
Pablo Neira Ayuso | e47a978 | 2011-06-07 18:03:48 +0200 | [diff] [blame] | 53 | #include <osmocom/abis/ipaccess.h> |
Pablo Neira Ayuso | f67471f | 2011-06-07 11:25:49 +0200 | [diff] [blame] | 54 | #include <osmocom/core/socket.h> |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 55 | #include <osmocom/abis/ipa.h> |
Harald Welte | 71d87b2 | 2011-07-18 14:49:56 +0200 | [diff] [blame] | 56 | #include <osmocom/core/talloc.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 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 | |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 77 | static void hsl_drop(struct e1inp_line *line, struct osmo_fd *bfd) |
| 78 | { |
| 79 | line->ops->sign_link_down(line); |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 80 | |
| 81 | if (bfd->fd != -1) { |
| 82 | osmo_fd_unregister(bfd); |
| 83 | close(bfd->fd); |
| 84 | bfd->fd = -1; |
| 85 | } |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Pablo Neira Ayuso | cd8d2e5 | 2011-07-02 17:05:21 +0200 | [diff] [blame] | 88 | static int process_hsl_rsl(struct msgb *msg, struct e1inp_line *line, |
| 89 | struct osmo_fd *bfd) |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 90 | { |
| 91 | char serno_buf[16]; |
| 92 | uint8_t serno_len; |
Pablo Neira Ayuso | cd8d2e5 | 2011-07-02 17:05:21 +0200 | [diff] [blame] | 93 | struct e1inp_sign_link *sign_link; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 94 | struct hsl_unit unit_data; |
| 95 | |
| 96 | switch (msg->l2h[1]) { |
| 97 | case 0x80: |
| 98 | /*, contains Serial Number + SW version */ |
| 99 | if (msg->l2h[2] != 0xc0) |
| 100 | break; |
| 101 | serno_len = msg->l2h[3]; |
| 102 | if (serno_len > sizeof(serno_buf)-1) |
| 103 | serno_len = sizeof(serno_buf)-1; |
| 104 | memcpy(serno_buf, msg->l2h+4, serno_len); |
| 105 | serno_buf[serno_len] = '\0'; |
| 106 | unit_data.serno = strtoul(serno_buf, NULL, 10); |
| 107 | |
| 108 | if (!line->ops->sign_link_up) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 109 | LOGP(DLINP, LOGL_ERROR, |
Pablo Neira Ayuso | cd8d2e5 | 2011-07-02 17:05:21 +0200 | [diff] [blame] | 110 | "Unable to set signal link, closing socket.\n"); |
| 111 | osmo_fd_unregister(bfd); |
| 112 | close(bfd->fd); |
| 113 | bfd->fd = -1; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 114 | return -EINVAL; |
| 115 | } |
Pablo Neira Ayuso | cd8d2e5 | 2011-07-02 17:05:21 +0200 | [diff] [blame] | 116 | sign_link = line->ops->sign_link_up(&unit_data, |
| 117 | line, E1INP_SIGN_NONE); |
| 118 | if (sign_link == NULL) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 119 | LOGP(DLINP, LOGL_ERROR, |
Pablo Neira Ayuso | cd8d2e5 | 2011-07-02 17:05:21 +0200 | [diff] [blame] | 120 | "Unable to set signal link, closing socket.\n"); |
| 121 | osmo_fd_unregister(bfd); |
| 122 | close(bfd->fd); |
| 123 | bfd->fd = -1; |
| 124 | return -EINVAL; |
| 125 | } |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 126 | msgb_free(msg); |
| 127 | return 1; /* == we have taken over the msg */ |
| 128 | case 0x82: |
| 129 | /* FIXME: do something with BSSGP, i.e. forward it over |
| 130 | * NSIP to OsmoSGSN */ |
| 131 | msgb_free(msg); |
| 132 | return 1; |
| 133 | } |
| 134 | return 0; |
| 135 | } |
| 136 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 137 | static int handle_ts1_read(struct osmo_fd *bfd) |
| 138 | { |
| 139 | struct e1inp_line *line = bfd->data; |
| 140 | unsigned int ts_nr = bfd->priv_nr; |
| 141 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 142 | struct e1inp_sign_link *link; |
| 143 | struct ipaccess_head *hh; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 144 | struct msgb *msg; |
| 145 | int ret = 0, error; |
| 146 | |
Pablo Neira Ayuso | 7a24940 | 2011-06-21 14:15:46 +0200 | [diff] [blame] | 147 | error = ipa_msg_recv(bfd->fd, &msg); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 148 | if (error < 0) |
| 149 | return error; |
| 150 | else if (error == 0) { |
| 151 | hsl_drop(e1i_ts->line, bfd); |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 152 | LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n"); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 153 | return error; |
| 154 | } |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 155 | DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg))); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 156 | |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 157 | hh = (struct ipaccess_head *) msg->data; |
| 158 | if (hh->proto == HSL_PROTO_DEBUG) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 159 | LOGP(DLINP, LOGL_NOTICE, "HSL debug: %s\n", msg->data + sizeof(*hh)); |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 160 | msgb_free(msg); |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | /* HSL proprietary RSL extension */ |
| 165 | if (hh->proto == 0 && (msg->l2h[0] == 0x81 || msg->l2h[0] == 0x80)) { |
Pablo Neira Ayuso | cd8d2e5 | 2011-07-02 17:05:21 +0200 | [diff] [blame] | 166 | ret = process_hsl_rsl(msg, line, bfd); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 167 | if (ret < 0) { |
| 168 | hsl_drop(e1i_ts->line, bfd); |
| 169 | return ret; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 170 | } else if (ret == 1) |
| 171 | return 0; |
| 172 | /* else: continue... */ |
| 173 | } |
| 174 | |
| 175 | #ifdef HSL_SR_1_0 |
| 176 | /* HSL for whatever reason chose to use 0x81 instead of 0x80 for FOM */ |
| 177 | if (hh->proto == 255 && msg->l2h[0] == (ABIS_OM_MDISC_FOM | 0x01)) |
| 178 | msg->l2h[0] = ABIS_OM_MDISC_FOM; |
| 179 | #endif |
| 180 | link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0); |
| 181 | if (!link) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 182 | LOGP(DLINP, LOGL_ERROR, "no matching signalling link for " |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 183 | "hh->proto=0x%02x\n", hh->proto); |
| 184 | msgb_free(msg); |
| 185 | return -EIO; |
| 186 | } |
| 187 | msg->dst = link; |
| 188 | |
| 189 | /* XXX: better use e1inp_ts_rx? */ |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 190 | if (!e1i_ts->line->ops->sign_link) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 191 | LOGP(DLINP, LOGL_ERROR, "Fix your application, " |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 192 | "no action set for signalling messages.\n"); |
| 193 | return -ENOENT; |
| 194 | } |
Pablo Neira Ayuso | dbd82fb | 2011-07-05 15:29:23 +0200 | [diff] [blame] | 195 | e1i_ts->line->ops->sign_link(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 196 | |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | static int ts_want_write(struct e1inp_ts *e1i_ts) |
| 201 | { |
| 202 | e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE; |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static void timeout_ts1_write(void *data) |
| 208 | { |
| 209 | struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data; |
| 210 | |
| 211 | /* trigger write of ts1, due to tx delay timer */ |
| 212 | ts_want_write(e1i_ts); |
| 213 | } |
| 214 | |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 215 | static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 216 | { |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 217 | unsigned int ts_nr = bfd->priv_nr; |
| 218 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 219 | struct e1inp_sign_link *sign_link; |
| 220 | struct msgb *msg; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 221 | int ret; |
| 222 | |
| 223 | bfd->when &= ~BSC_FD_WRITE; |
| 224 | |
| 225 | /* get the next msg for this timeslot */ |
| 226 | msg = e1inp_tx_ts(e1i_ts, &sign_link); |
| 227 | if (!msg) { |
| 228 | /* no message after tx delay timer */ |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | switch (sign_link->type) { |
| 233 | case E1INP_SIGN_OML: |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 234 | #ifdef HSL_SR_1_0 |
| 235 | /* HSL uses 0x81 for FOM for some reason */ |
| 236 | if (msg->data[0] == ABIS_OM_MDISC_FOM) |
| 237 | msg->data[0] = ABIS_OM_MDISC_FOM | 0x01; |
| 238 | #endif |
| 239 | break; |
| 240 | case E1INP_SIGN_RSL: |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 241 | break; |
| 242 | default: |
| 243 | msgb_free(msg); |
| 244 | bfd->when |= BSC_FD_WRITE; /* come back for more msg */ |
| 245 | return -EINVAL; |
| 246 | } |
| 247 | |
| 248 | msg->l2h = msg->data; |
| 249 | ipaccess_prepend_header(msg, sign_link->tei); |
| 250 | |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 251 | DEBUGP(DLMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg))); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 252 | |
| 253 | ret = send(bfd->fd, msg->data, msg->len, 0); |
| 254 | msgb_free(msg); |
| 255 | |
| 256 | /* set tx delay timer for next event */ |
| 257 | e1i_ts->sign.tx_timer.cb = timeout_ts1_write; |
| 258 | e1i_ts->sign.tx_timer.data = e1i_ts; |
| 259 | |
| 260 | /* Reducing this might break the nanoBTS 900 init. */ |
| 261 | osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay); |
| 262 | |
| 263 | return ret; |
| 264 | } |
| 265 | |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 266 | static int handle_ts1_write(struct osmo_fd *bfd) |
| 267 | { |
| 268 | struct e1inp_line *line = bfd->data; |
| 269 | |
| 270 | return __handle_ts1_write(bfd, line); |
| 271 | } |
| 272 | |
Pablo Neira Ayuso | f099567 | 2011-09-08 12:58:38 +0200 | [diff] [blame] | 273 | int hsl_bts_write(struct ipa_client_conn *link) |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 274 | { |
| 275 | struct e1inp_line *line = link->line; |
| 276 | |
| 277 | return __handle_ts1_write(link->ofd, line); |
| 278 | } |
| 279 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 280 | /* callback from select.c in case one of the fd's can be read/written */ |
| 281 | static int hsl_fd_cb(struct osmo_fd *bfd, unsigned int what) |
| 282 | { |
| 283 | struct e1inp_line *line = bfd->data; |
| 284 | unsigned int ts_nr = bfd->priv_nr; |
| 285 | unsigned int idx = ts_nr-1; |
| 286 | struct e1inp_ts *e1i_ts; |
| 287 | int rc = 0; |
| 288 | |
| 289 | /* In case of early RSL we might not yet have a line */ |
| 290 | |
| 291 | if (line) |
| 292 | e1i_ts = &line->ts[idx]; |
| 293 | |
| 294 | if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) { |
| 295 | if (what & BSC_FD_READ) |
| 296 | rc = handle_ts1_read(bfd); |
| 297 | if (what & BSC_FD_WRITE) |
| 298 | rc = handle_ts1_write(bfd); |
| 299 | } else |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 300 | LOGP(DLINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 301 | |
| 302 | return rc; |
| 303 | } |
| 304 | |
Pablo Neira Ayuso | 34073fb | 2011-07-08 20:36:05 +0200 | [diff] [blame] | 305 | static void hsl_close(struct e1inp_sign_link *sign_link) |
| 306 | { |
Holger Hans Peter Freyther | 55467f0 | 2011-12-28 19:47:07 +0100 | [diff] [blame] | 307 | struct e1inp_ts *e1i_ts = sign_link->ts; |
| 308 | struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd; |
| 309 | return e1inp_close_socket(e1i_ts, sign_link, bfd); |
Pablo Neira Ayuso | 34073fb | 2011-07-08 20:36:05 +0200 | [diff] [blame] | 310 | } |
| 311 | |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 312 | static int hsl_line_update(struct e1inp_line *line); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 313 | |
| 314 | struct e1inp_driver hsl_driver = { |
| 315 | .name = "hsl", |
| 316 | .want_write = ts_want_write, |
| 317 | .line_update = hsl_line_update, |
Pablo Neira Ayuso | 34073fb | 2011-07-08 20:36:05 +0200 | [diff] [blame] | 318 | .close = hsl_close, |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 319 | .default_delay = 0, |
| 320 | }; |
| 321 | |
| 322 | /* callback of the OML listening filedescriptor */ |
| 323 | static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what) |
| 324 | { |
| 325 | int ret; |
| 326 | int idx = 0; |
| 327 | int i; |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 328 | struct e1inp_line *line; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 329 | struct e1inp_ts *e1i_ts; |
| 330 | struct osmo_fd *bfd; |
| 331 | struct sockaddr_in sa; |
| 332 | socklen_t sa_len = sizeof(sa); |
| 333 | |
| 334 | if (!(what & BSC_FD_READ)) |
| 335 | return 0; |
| 336 | |
| 337 | ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 338 | if (ret < 0) { |
| 339 | perror("accept"); |
| 340 | return ret; |
| 341 | } |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 342 | LOGP(DLINP, LOGL_NOTICE, "accept()ed new HSL link from %s\n", |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 343 | inet_ntoa(sa.sin_addr)); |
| 344 | |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 345 | /* clone virtual E1 line for this new signalling link. */ |
| 346 | line = e1inp_line_clone(tall_hsl_ctx, listen_bfd->data); |
| 347 | if (line == NULL) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 348 | LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n"); |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 349 | return -1; |
| 350 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 351 | /* create virrtual E1 timeslots for signalling */ |
| 352 | e1inp_ts_config_sign(&line->ts[1-1], line); |
| 353 | |
| 354 | /* initialize the fds */ |
| 355 | for (i = 0; i < ARRAY_SIZE(line->ts); ++i) |
| 356 | line->ts[i].driver.ipaccess.fd.fd = -1; |
| 357 | |
| 358 | e1i_ts = &line->ts[idx]; |
| 359 | |
| 360 | bfd = &e1i_ts->driver.ipaccess.fd; |
| 361 | bfd->fd = ret; |
| 362 | bfd->data = line; |
| 363 | bfd->priv_nr = PRIV_OML; |
| 364 | bfd->cb = hsl_fd_cb; |
| 365 | bfd->when = BSC_FD_READ; |
| 366 | ret = osmo_fd_register(bfd); |
| 367 | if (ret < 0) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 368 | LOGP(DLINP, LOGL_ERROR, "could not register FD\n"); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 369 | close(bfd->fd); |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 370 | e1inp_line_put(line); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | return ret; |
| 375 | } |
| 376 | |
Pablo Neira Ayuso | f099567 | 2011-09-08 12:58:38 +0200 | [diff] [blame] | 377 | static int hsl_bts_process(struct ipa_client_conn *link, struct msgb *msg) |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 378 | { |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 379 | struct ipaccess_head *hh; |
| 380 | struct e1inp_sign_link *sign_link; |
| 381 | struct e1inp_ts *e1i_ts = &link->line->ts[0]; |
| 382 | |
| 383 | hh = (struct ipaccess_head *) msg->data; |
| 384 | if (hh->proto == HSL_PROTO_DEBUG) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 385 | LOGP(DLINP, LOGL_NOTICE, "HSL debug: %s\n", |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 386 | msg->data + sizeof(*hh)); |
| 387 | msgb_free(msg); |
| 388 | return 0; |
| 389 | } |
| 390 | sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0); |
| 391 | if (!sign_link) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 392 | LOGP(DLINP, LOGL_ERROR, "no matching signalling link for " |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 393 | "hh->proto=0x%02x\n", hh->proto); |
| 394 | msgb_free(msg); |
| 395 | return -EIO; |
| 396 | } |
| 397 | msg->dst = sign_link; |
| 398 | |
| 399 | /* XXX better use e1inp_ts_rx? */ |
| 400 | if (!link->line->ops->sign_link) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 401 | LOGP(DLINP, LOGL_ERROR, "Fix your application, " |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 402 | "no action set for signalling messages.\n"); |
| 403 | return -ENOENT; |
| 404 | } |
| 405 | link->line->ops->sign_link(msg); |
| 406 | return 0; |
| 407 | } |
| 408 | |
Pablo Neira Ayuso | f099567 | 2011-09-08 12:58:38 +0200 | [diff] [blame] | 409 | static int hsl_bts_connect(struct ipa_client_conn *link) |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 410 | { |
| 411 | struct msgb *msg; |
| 412 | uint8_t *serno; |
| 413 | char serno_buf[16]; |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 414 | struct hsl_unit *unit = link->line->ops->cfg.ipa.dev; |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 415 | struct e1inp_sign_link *sign_link; |
| 416 | |
| 417 | /* send the minimal message to identify this BTS. */ |
| 418 | msg = ipa_msg_alloc(0); |
| 419 | if (!msg) |
| 420 | return -ENOMEM; |
| 421 | |
| 422 | *msgb_put(msg, 1) = 0x80; |
| 423 | *msgb_put(msg, 1) = 0x80; |
| 424 | *msgb_put(msg, 1) = unit->swversion; |
Harald Welte | 3e1d84b | 2011-08-19 13:32:18 +0200 | [diff] [blame] | 425 | snprintf(serno_buf, sizeof(serno_buf), "%"PRIx64, unit->serno); |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 426 | serno = msgb_put(msg, strlen(serno_buf)+1); |
| 427 | memcpy(serno, serno_buf, strlen(serno_buf)); |
| 428 | ipa_msg_push_header(msg, 0); |
| 429 | send(link->ofd->fd, msg->data, msg->len, 0); |
| 430 | msgb_free(msg); |
| 431 | |
| 432 | /* ... and enable the signalling link. */ |
| 433 | if (!link->line->ops->sign_link_up) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 434 | LOGP(DLINP, LOGL_ERROR, |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 435 | "Unable to set signal link, closing socket.\n"); |
Pablo Neira Ayuso | f099567 | 2011-09-08 12:58:38 +0200 | [diff] [blame] | 436 | ipa_client_conn_close(link); |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 437 | return -EINVAL; |
| 438 | } |
| 439 | sign_link = link->line->ops->sign_link_up(&unit, |
| 440 | link->line, E1INP_SIGN_NONE); |
| 441 | if (sign_link == NULL) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 442 | LOGP(DLINP, LOGL_ERROR, |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 443 | "Unable to set signal link, closing socket.\n"); |
Pablo Neira Ayuso | f099567 | 2011-09-08 12:58:38 +0200 | [diff] [blame] | 444 | ipa_client_conn_close(link); |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 445 | return -EINVAL; |
| 446 | } |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 447 | return 0; |
| 448 | } |
| 449 | |
Pablo Neira Ayuso | d621640 | 2011-08-31 16:47:44 +0200 | [diff] [blame] | 450 | struct hsl_line { |
| 451 | int line_already_initialized; |
| 452 | }; |
| 453 | |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 454 | static int hsl_line_update(struct e1inp_line *line) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 455 | { |
| 456 | int ret = -ENOENT; |
Pablo Neira Ayuso | d621640 | 2011-08-31 16:47:44 +0200 | [diff] [blame] | 457 | struct hsl_line *hl; |
| 458 | |
| 459 | if (!line->driver_data) |
| 460 | line->driver_data = talloc_zero(line, struct hsl_line); |
| 461 | |
| 462 | if (!line->driver_data) { |
| 463 | LOGP(DLINP, LOGL_NOTICE, "hsl: OOM in line update\n"); |
| 464 | return -ENOMEM; |
| 465 | } |
| 466 | hl = line->driver_data; |
| 467 | |
| 468 | /* We only initialize this line once. */ |
| 469 | if (hl->line_already_initialized) |
| 470 | return 0; |
| 471 | |
| 472 | hl->line_already_initialized = 1; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 473 | |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 474 | switch(line->ops->cfg.ipa.role) { |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 475 | case E1INP_LINE_R_BSC: |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 476 | LOGP(DLINP, LOGL_NOTICE, "enabling hsl BSC mode\n"); |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 477 | |
Pablo Neira Ayuso | f67471f | 2011-06-07 11:25:49 +0200 | [diff] [blame] | 478 | ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 479 | line->ops->cfg.ipa.addr, |
| 480 | HSL_TCP_PORT, OSMO_SOCK_F_BIND); |
Pablo Neira Ayuso | f67471f | 2011-06-07 11:25:49 +0200 | [diff] [blame] | 481 | if (ret < 0) |
| 482 | return ret; |
| 483 | |
| 484 | e1h->listen_fd.fd = ret; |
| 485 | e1h->listen_fd.when |= BSC_FD_READ; |
| 486 | e1h->listen_fd.cb = listen_fd_cb; |
| 487 | e1h->listen_fd.data = line; |
| 488 | |
| 489 | if (osmo_fd_register(&e1h->listen_fd) < 0) { |
| 490 | close(ret); |
| 491 | return ret; |
| 492 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 493 | break; |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 494 | case E1INP_LINE_R_BTS: { |
Pablo Neira Ayuso | f099567 | 2011-09-08 12:58:38 +0200 | [diff] [blame] | 495 | struct ipa_client_conn *link; |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 496 | |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 497 | LOGP(DLINP, LOGL_NOTICE, "enabling hsl BTS mode\n"); |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 498 | |
Pablo Neira Ayuso | f099567 | 2011-09-08 12:58:38 +0200 | [diff] [blame] | 499 | link = ipa_client_conn_create(tall_hsl_ctx, |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 500 | &line->ts[E1INP_SIGN_OML-1], |
Pablo Neira Ayuso | 00af772 | 2011-09-08 12:47:06 +0200 | [diff] [blame] | 501 | E1INP_SIGN_OML, |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 502 | line->ops->cfg.ipa.addr, |
| 503 | HSL_TCP_PORT, |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 504 | hsl_bts_connect, |
| 505 | hsl_bts_process, |
| 506 | hsl_bts_write, |
| 507 | NULL); |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 508 | if (link == NULL) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 509 | LOGP(DLINP, LOGL_ERROR, "cannot create BTS link: %s\n", |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 510 | strerror(errno)); |
| 511 | return -ENOMEM; |
| 512 | } |
Pablo Neira Ayuso | f099567 | 2011-09-08 12:58:38 +0200 | [diff] [blame] | 513 | if (ipa_client_conn_open(link) < 0) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 514 | LOGP(DLINP, LOGL_ERROR, "cannot open BTS link: %s\n", |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 515 | strerror(errno)); |
Pablo Neira Ayuso | f099567 | 2011-09-08 12:58:38 +0200 | [diff] [blame] | 516 | ipa_client_conn_close(link); |
| 517 | ipa_client_conn_destroy(link); |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 518 | return -EIO; |
| 519 | } |
| 520 | ret = 0; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 521 | break; |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 522 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 523 | default: |
| 524 | break; |
| 525 | } |
| 526 | return ret; |
| 527 | } |
| 528 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 529 | void e1inp_hsl_init(void) |
| 530 | { |
Pablo Neira Ayuso | 54b4979 | 2011-06-07 12:15:26 +0200 | [diff] [blame] | 531 | tall_hsl_ctx = talloc_named_const(libosmo_abis_ctx, 1, "hsl"); |
| 532 | |
| 533 | e1h = talloc_zero(tall_hsl_ctx, struct hsl_e1_handle); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 534 | if (!e1h) |
| 535 | return; |
| 536 | |
| 537 | e1inp_driver_register(&hsl_driver); |
| 538 | } |