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