| 1 | /* OpenBSC Abis input driver for ip.access */ |
|---|
| 2 | |
|---|
| 3 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
|---|
| 4 | * |
|---|
| 5 | * All Rights Reserved |
|---|
| 6 | * |
|---|
| 7 | * This program is free software; you can redistribute it and/or modify |
|---|
| 8 | * it under the terms of the GNU General Public License as published by |
|---|
| 9 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | * (at your option) any later version. |
|---|
| 11 | * |
|---|
| 12 | * This program is distributed in the hope that it will be useful, |
|---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | * GNU General Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License along |
|---|
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
|---|
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #include <stdio.h> |
|---|
| 24 | #include <unistd.h> |
|---|
| 25 | #include <stdlib.h> |
|---|
| 26 | #include <errno.h> |
|---|
| 27 | #include <string.h> |
|---|
| 28 | #include <time.h> |
|---|
| 29 | #include <sys/fcntl.h> |
|---|
| 30 | #include <sys/types.h> |
|---|
| 31 | #include <sys/socket.h> |
|---|
| 32 | #include <sys/ioctl.h> |
|---|
| 33 | #include <arpa/inet.h> |
|---|
| 34 | |
|---|
| 35 | #include <openbsc/select.h> |
|---|
| 36 | #include <openbsc/tlv.h> |
|---|
| 37 | #include <openbsc/msgb.h> |
|---|
| 38 | #include <openbsc/debug.h> |
|---|
| 39 | #include <openbsc/gsm_data.h> |
|---|
| 40 | #include <openbsc/abis_nm.h> |
|---|
| 41 | #include <openbsc/abis_rsl.h> |
|---|
| 42 | #include <openbsc/subchan_demux.h> |
|---|
| 43 | #include <openbsc/e1_input.h> |
|---|
| 44 | #include <openbsc/ipaccess.h> |
|---|
| 45 | #include <openbsc/talloc.h> |
|---|
| 46 | |
|---|
| 47 | /* data structure for one E1 interface with A-bis */ |
|---|
| 48 | struct ia_e1_handle { |
|---|
| 49 | struct bsc_fd listen_fd; |
|---|
| 50 | struct bsc_fd rsl_listen_fd; |
|---|
| 51 | struct gsm_network *gsmnet; |
|---|
| 52 | }; |
|---|
| 53 | |
|---|
| 54 | static struct ia_e1_handle *e1h; |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | #define TS1_ALLOC_SIZE 300 |
|---|
| 58 | |
|---|
| 59 | static const u_int8_t pong[] = { 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG }; |
|---|
| 60 | static const u_int8_t id_ack[] = { 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK }; |
|---|
| 61 | static const u_int8_t id_req[] = { 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET, |
|---|
| 62 | 0x01, IPAC_IDTAG_UNIT, |
|---|
| 63 | 0x01, IPAC_IDTAG_MACADDR, |
|---|
| 64 | 0x01, IPAC_IDTAG_LOCATION1, |
|---|
| 65 | 0x01, IPAC_IDTAG_LOCATION2, |
|---|
| 66 | 0x01, IPAC_IDTAG_EQUIPVERS, |
|---|
| 67 | 0x01, IPAC_IDTAG_SWVERSION, |
|---|
| 68 | 0x01, IPAC_IDTAG_UNITNAME, |
|---|
| 69 | 0x01, IPAC_IDTAG_SERNR, |
|---|
| 70 | }; |
|---|
| 71 | |
|---|
| 72 | static const char *idtag_names[] = { |
|---|
| 73 | [IPAC_IDTAG_SERNR] = "Serial_Number", |
|---|
| 74 | [IPAC_IDTAG_UNITNAME] = "Unit_Name", |
|---|
| 75 | [IPAC_IDTAG_LOCATION1] = "Location_1", |
|---|
| 76 | [IPAC_IDTAG_LOCATION2] = "Location_2", |
|---|
| 77 | [IPAC_IDTAG_EQUIPVERS] = "Equipment_Version", |
|---|
| 78 | [IPAC_IDTAG_SWVERSION] = "Software_Version", |
|---|
| 79 | [IPAC_IDTAG_IPADDR] = "IP_Address", |
|---|
| 80 | [IPAC_IDTAG_MACADDR] = "MAC_Address", |
|---|
| 81 | [IPAC_IDTAG_UNIT] = "Unit_ID", |
|---|
| 82 | }; |
|---|
| 83 | |
|---|
| 84 | static const char *ipac_idtag_name(int tag) |
|---|
| 85 | { |
|---|
| 86 | if (tag >= ARRAY_SIZE(idtag_names)) |
|---|
| 87 | return "unknown"; |
|---|
| 88 | |
|---|
| 89 | return idtag_names[tag]; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | int ipaccess_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len) |
|---|
| 93 | { |
|---|
| 94 | u_int8_t t_len; |
|---|
| 95 | u_int8_t t_tag; |
|---|
| 96 | u_int8_t *cur = buf; |
|---|
| 97 | |
|---|
| 98 | while (cur < buf + len) { |
|---|
| 99 | t_len = *cur++; |
|---|
| 100 | t_tag = *cur++; |
|---|
| 101 | |
|---|
| 102 | DEBUGPC(DMI, "%s='%s' ", ipac_idtag_name(t_tag), cur); |
|---|
| 103 | |
|---|
| 104 | dec->lv[t_tag].len = t_len; |
|---|
| 105 | dec->lv[t_tag].val = cur; |
|---|
| 106 | |
|---|
| 107 | cur += t_len; |
|---|
| 108 | } |
|---|
| 109 | return 0; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | struct gsm_bts *find_bts_by_unitid(struct gsm_network *net, |
|---|
| 113 | u_int16_t site_id, u_int16_t bts_id) |
|---|
| 114 | { |
|---|
| 115 | struct gsm_bts *bts; |
|---|
| 116 | |
|---|
| 117 | llist_for_each_entry(bts, &net->bts_list, list) { |
|---|
| 118 | |
|---|
| 119 | if (!is_ipaccess_bts(bts)) |
|---|
| 120 | continue; |
|---|
| 121 | |
|---|
| 122 | if (bts->ip_access.site_id == site_id && |
|---|
| 123 | bts->ip_access.bts_id == bts_id) |
|---|
| 124 | return bts; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | return NULL; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | static int parse_unitid(const char *str, u_int16_t *site_id, u_int16_t *bts_id, |
|---|
| 131 | u_int16_t *trx_id) |
|---|
| 132 | { |
|---|
| 133 | unsigned long ul; |
|---|
| 134 | char *endptr; |
|---|
| 135 | const char *nptr; |
|---|
| 136 | |
|---|
| 137 | nptr = str; |
|---|
| 138 | ul = strtoul(nptr, &endptr, 10); |
|---|
| 139 | if (endptr <= nptr) |
|---|
| 140 | return -EINVAL; |
|---|
| 141 | if (site_id) |
|---|
| 142 | *site_id = ul & 0xffff; |
|---|
| 143 | |
|---|
| 144 | if (*endptr++ != '/') |
|---|
| 145 | return -EINVAL; |
|---|
| 146 | |
|---|
| 147 | nptr = endptr; |
|---|
| 148 | ul = strtoul(nptr, &endptr, 10); |
|---|
| 149 | if (endptr <= nptr) |
|---|
| 150 | return -EINVAL; |
|---|
| 151 | if (bts_id) |
|---|
| 152 | *bts_id = ul & 0xffff; |
|---|
| 153 | |
|---|
| 154 | if (*endptr++ != '/') |
|---|
| 155 | return -EINVAL; |
|---|
| 156 | |
|---|
| 157 | nptr = endptr; |
|---|
| 158 | ul = strtoul(nptr, &endptr, 10); |
|---|
| 159 | if (endptr <= nptr) |
|---|
| 160 | return -EINVAL; |
|---|
| 161 | if (trx_id) |
|---|
| 162 | *trx_id = ul & 0xffff; |
|---|
| 163 | |
|---|
| 164 | return 0; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | /* send the id ack */ |
|---|
| 168 | int ipaccess_send_id_ack(int fd) |
|---|
| 169 | { |
|---|
| 170 | return write(fd, id_ack, sizeof(id_ack)); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | int ipaccess_send_id_req(int fd) |
|---|
| 174 | { |
|---|
| 175 | return write(fd, id_req, sizeof(id_req)); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | /* base handling of the ip.access protocol */ |
|---|
| 179 | int ipaccess_rcvmsg_base(struct msgb *msg, |
|---|
| 180 | struct bsc_fd *bfd) |
|---|
| 181 | { |
|---|
| 182 | u_int8_t msg_type = *(msg->l2h); |
|---|
| 183 | int ret = 0; |
|---|
| 184 | |
|---|
| 185 | switch (msg_type) { |
|---|
| 186 | case IPAC_MSGT_PING: |
|---|
| 187 | ret = write(bfd->fd, pong, sizeof(pong)); |
|---|
| 188 | break; |
|---|
| 189 | case IPAC_MSGT_PONG: |
|---|
| 190 | DEBUGP(DMI, "PONG!\n"); |
|---|
| 191 | break; |
|---|
| 192 | case IPAC_MSGT_ID_ACK: |
|---|
| 193 | DEBUGP(DMI, "ID_ACK? -> ACK!\n"); |
|---|
| 194 | ret = ipaccess_send_id_ack(bfd->fd); |
|---|
| 195 | break; |
|---|
| 196 | } |
|---|
| 197 | return 0; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg, |
|---|
| 201 | struct bsc_fd *bfd) |
|---|
| 202 | { |
|---|
| 203 | struct tlv_parsed tlvp; |
|---|
| 204 | u_int8_t msg_type = *(msg->l2h); |
|---|
| 205 | u_int16_t site_id = 0, bts_id = 0, trx_id = 0; |
|---|
| 206 | struct gsm_bts *bts; |
|---|
| 207 | |
|---|
| 208 | /* handle base messages */ |
|---|
| 209 | ipaccess_rcvmsg_base(msg, bfd); |
|---|
| 210 | |
|---|
| 211 | switch (msg_type) { |
|---|
| 212 | case IPAC_MSGT_ID_RESP: |
|---|
| 213 | DEBUGP(DMI, "ID_RESP "); |
|---|
| 214 | /* parse tags, search for Unit ID */ |
|---|
| 215 | ipaccess_idtag_parse(&tlvp, (u_int8_t *)msg->l2h + 2, |
|---|
| 216 | msgb_l2len(msg)-2); |
|---|
| 217 | DEBUGP(DMI, "\n"); |
|---|
| 218 | |
|---|
| 219 | if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) |
|---|
| 220 | break; |
|---|
| 221 | |
|---|
| 222 | /* lookup BTS, create sign_link, ... */ |
|---|
| 223 | parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT), |
|---|
| 224 | &site_id, &bts_id, &trx_id); |
|---|
| 225 | bts = find_bts_by_unitid(e1h->gsmnet, site_id, bts_id); |
|---|
| 226 | if (!bts) { |
|---|
| 227 | LOGP(DINP, LOGL_ERROR, "Unable to find BTS configuration for " |
|---|
| 228 | " %u/%u/%u, disconnecting\n", site_id, bts_id, |
|---|
| 229 | trx_id); |
|---|
| 230 | return -EIO; |
|---|
| 231 | } |
|---|
| 232 | DEBUGP(DINP, "Identified BTS %u/%u/%u\n", site_id, bts_id, trx_id); |
|---|
| 233 | if (bfd->priv_nr == 1) { |
|---|
| 234 | bts->oml_link = e1inp_sign_link_create(&line->ts[1-1], |
|---|
| 235 | E1INP_SIGN_OML, bts->c0, |
|---|
| 236 | bts->oml_tei, 0); |
|---|
| 237 | } else if (bfd->priv_nr == 2) { |
|---|
| 238 | struct e1inp_ts *e1i_ts; |
|---|
| 239 | struct bsc_fd *newbfd; |
|---|
| 240 | struct gsm_bts_trx *trx = gsm_bts_trx_num(bts, trx_id); |
|---|
| 241 | |
|---|
| 242 | bfd->data = line = bts->oml_link->ts->line; |
|---|
| 243 | e1i_ts = &line->ts[2+trx_id - 1]; |
|---|
| 244 | newbfd = &e1i_ts->driver.ipaccess.fd; |
|---|
| 245 | e1inp_ts_config(e1i_ts, line, E1INP_TS_TYPE_SIGN); |
|---|
| 246 | |
|---|
| 247 | trx->rsl_link = e1inp_sign_link_create(e1i_ts, |
|---|
| 248 | E1INP_SIGN_RSL, trx, |
|---|
| 249 | trx->rsl_tei, 0); |
|---|
| 250 | /* get rid of our old temporary bfd */ |
|---|
| 251 | memcpy(newbfd, bfd, sizeof(*newbfd)); |
|---|
| 252 | newbfd->priv_nr = 2+trx_id; |
|---|
| 253 | bsc_unregister_fd(bfd); |
|---|
| 254 | bsc_register_fd(newbfd); |
|---|
| 255 | talloc_free(bfd); |
|---|
| 256 | } |
|---|
| 257 | break; |
|---|
| 258 | } |
|---|
| 259 | return 0; |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | #define OML_UP 0x0001 |
|---|
| 263 | #define RSL_UP 0x0002 |
|---|
| 264 | |
|---|
| 265 | /* |
|---|
| 266 | * read one ipa message from the socket |
|---|
| 267 | * return NULL in case of error |
|---|
| 268 | */ |
|---|
| 269 | struct msgb *ipaccess_read_msg(struct bsc_fd *bfd, int *error) |
|---|
| 270 | { |
|---|
| 271 | struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "Abis/IP"); |
|---|
| 272 | struct ipaccess_head *hh; |
|---|
| 273 | int len, ret = 0; |
|---|
| 274 | |
|---|
| 275 | if (!msg) { |
|---|
| 276 | *error = -ENOMEM; |
|---|
| 277 | return NULL; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | /* first read our 3-byte header */ |
|---|
| 281 | hh = (struct ipaccess_head *) msg->data; |
|---|
| 282 | ret = recv(bfd->fd, msg->data, 3, 0); |
|---|
| 283 | if (ret < 0) { |
|---|
| 284 | if (errno != EAGAIN) |
|---|
| 285 | LOGP(DINP, LOGL_ERROR, "recv error %d %s\n", ret, strerror(errno)); |
|---|
| 286 | msgb_free(msg); |
|---|
| 287 | *error = ret; |
|---|
| 288 | return NULL; |
|---|
| 289 | } else if (ret == 0) { |
|---|
| 290 | msgb_free(msg); |
|---|
| 291 | *error = ret; |
|---|
| 292 | return NULL; |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | msgb_put(msg, ret); |
|---|
| 296 | |
|---|
| 297 | /* then read te length as specified in header */ |
|---|
| 298 | msg->l2h = msg->data + sizeof(*hh); |
|---|
| 299 | len = ntohs(hh->len); |
|---|
| 300 | ret = recv(bfd->fd, msg->l2h, len, 0); |
|---|
| 301 | if (ret < len) { |
|---|
| 302 | LOGP(DINP, LOGL_ERROR, "short read!\n"); |
|---|
| 303 | msgb_free(msg); |
|---|
| 304 | *error = -EIO; |
|---|
| 305 | return NULL; |
|---|
| 306 | } |
|---|
| 307 | msgb_put(msg, ret); |
|---|
| 308 | |
|---|
| 309 | return msg; |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | static int handle_ts1_read(struct bsc_fd *bfd) |
|---|
| 313 | { |
|---|
| 314 | struct e1inp_line *line = bfd->data; |
|---|
| 315 | unsigned int ts_nr = bfd->priv_nr; |
|---|
| 316 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
|---|
| 317 | struct e1inp_sign_link *link; |
|---|
| 318 | struct msgb *msg; |
|---|
| 319 | struct ipaccess_head *hh; |
|---|
| 320 | int ret = 0, error; |
|---|
| 321 | |
|---|
| 322 | msg = ipaccess_read_msg(bfd, &error); |
|---|
| 323 | if (!msg) { |
|---|
| 324 | if (error == 0) { |
|---|
| 325 | link = e1inp_lookup_sign_link(e1i_ts, IPAC_PROTO_OML, 0); |
|---|
| 326 | if (link) { |
|---|
| 327 | link->trx->bts->ip_access.flags = 0; |
|---|
| 328 | LOGP(DINP, LOGL_NOTICE, "BTS %u disappeared, dead socket\n", |
|---|
| 329 | link->trx->bts->nr); |
|---|
| 330 | } else |
|---|
| 331 | LOGP(DINP, LOGL_NOTICE, "unknown BTS disappeared, dead socket\n"); |
|---|
| 332 | e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_RSL); |
|---|
| 333 | e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_OML); |
|---|
| 334 | bsc_unregister_fd(bfd); |
|---|
| 335 | close(bfd->fd); |
|---|
| 336 | bfd->fd = -1; |
|---|
| 337 | } |
|---|
| 338 | return error; |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | DEBUGP(DMI, "RX %u: %s\n", ts_nr, hexdump(msgb_l2(msg), msgb_l2len(msg))); |
|---|
| 342 | |
|---|
| 343 | hh = (struct ipaccess_head *) msg->data; |
|---|
| 344 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
|---|
| 345 | ret = ipaccess_rcvmsg(line, msg, bfd); |
|---|
| 346 | if (ret < 0) { |
|---|
| 347 | e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_RSL); |
|---|
| 348 | e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_OML); |
|---|
| 349 | bsc_unregister_fd(bfd); |
|---|
| 350 | close(bfd->fd); |
|---|
| 351 | bfd->fd = -1; |
|---|
| 352 | } |
|---|
| 353 | msgb_free(msg); |
|---|
| 354 | return ret; |
|---|
| 355 | } |
|---|
| 356 | /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg() |
|---|
| 357 | * might have free'd it !!! */ |
|---|
| 358 | |
|---|
| 359 | link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0); |
|---|
| 360 | if (!link) { |
|---|
| 361 | LOGP(DINP, LOGL_ERROR, "no matching signalling link for " |
|---|
| 362 | "hh->proto=0x%02x\n", hh->proto); |
|---|
| 363 | msgb_free(msg); |
|---|
| 364 | return -EIO; |
|---|
| 365 | } |
|---|
| 366 | msg->trx = link->trx; |
|---|
| 367 | |
|---|
| 368 | switch (link->type) { |
|---|
| 369 | case E1INP_SIGN_RSL: |
|---|
| 370 | if (!(msg->trx->bts->ip_access.flags & (RSL_UP << msg->trx->nr))) { |
|---|
| 371 | e1inp_event(e1i_ts, EVT_E1_TEI_UP, link->tei, link->sapi); |
|---|
| 372 | msg->trx->bts->ip_access.flags |= (RSL_UP << msg->trx->nr); |
|---|
| 373 | } |
|---|
| 374 | ret = abis_rsl_rcvmsg(msg); |
|---|
| 375 | break; |
|---|
| 376 | case E1INP_SIGN_OML: |
|---|
| 377 | if (!(msg->trx->bts->ip_access.flags & OML_UP)) { |
|---|
| 378 | e1inp_event(e1i_ts, EVT_E1_TEI_UP, link->tei, link->sapi); |
|---|
| 379 | msg->trx->bts->ip_access.flags |= OML_UP; |
|---|
| 380 | } |
|---|
| 381 | ret = abis_nm_rcvmsg(msg); |
|---|
| 382 | break; |
|---|
| 383 | default: |
|---|
| 384 | LOGP(DINP, LOGL_NOTICE, "Unknown IP.access protocol proto=0x%02x\n", hh->proto); |
|---|
| 385 | msgb_free(msg); |
|---|
| 386 | break; |
|---|
| 387 | } |
|---|
| 388 | return ret; |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | void ipaccess_prepend_header(struct msgb *msg, int proto) |
|---|
| 392 | { |
|---|
| 393 | struct ipaccess_head *hh; |
|---|
| 394 | |
|---|
| 395 | /* prepend the ip.access header */ |
|---|
| 396 | hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh)); |
|---|
| 397 | hh->len = htons(msg->len - sizeof(*hh)); |
|---|
| 398 | hh->proto = proto; |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | static int ts_want_write(struct e1inp_ts *e1i_ts) |
|---|
| 402 | { |
|---|
| 403 | e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE; |
|---|
| 404 | |
|---|
| 405 | return 0; |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | static void timeout_ts1_write(void *data) |
|---|
| 409 | { |
|---|
| 410 | struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data; |
|---|
| 411 | |
|---|
| 412 | /* trigger write of ts1, due to tx delay timer */ |
|---|
| 413 | ts_want_write(e1i_ts); |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | static int handle_ts1_write(struct bsc_fd *bfd) |
|---|
| 417 | { |
|---|
| 418 | struct e1inp_line *line = bfd->data; |
|---|
| 419 | unsigned int ts_nr = bfd->priv_nr; |
|---|
| 420 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
|---|
| 421 | struct e1inp_sign_link *sign_link; |
|---|
| 422 | struct msgb *msg; |
|---|
| 423 | u_int8_t proto; |
|---|
| 424 | int ret; |
|---|
| 425 | |
|---|
| 426 | bfd->when &= ~BSC_FD_WRITE; |
|---|
| 427 | |
|---|
| 428 | /* get the next msg for this timeslot */ |
|---|
| 429 | msg = e1inp_tx_ts(e1i_ts, &sign_link); |
|---|
| 430 | if (!msg) { |
|---|
| 431 | /* no message after tx delay timer */ |
|---|
| 432 | return 0; |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | switch (sign_link->type) { |
|---|
| 436 | case E1INP_SIGN_OML: |
|---|
| 437 | proto = IPAC_PROTO_OML; |
|---|
| 438 | break; |
|---|
| 439 | case E1INP_SIGN_RSL: |
|---|
| 440 | proto = IPAC_PROTO_RSL; |
|---|
| 441 | break; |
|---|
| 442 | default: |
|---|
| 443 | msgb_free(msg); |
|---|
| 444 | bfd->when |= BSC_FD_WRITE; /* come back for more msg */ |
|---|
| 445 | return -EINVAL; |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | msg->l2h = msg->data; |
|---|
| 449 | ipaccess_prepend_header(msg, sign_link->tei); |
|---|
| 450 | |
|---|
| 451 | DEBUGP(DMI, "TX %u: %s\n", ts_nr, hexdump(msg->l2h, msgb_l2len(msg))); |
|---|
| 452 | |
|---|
| 453 | ret = send(bfd->fd, msg->data, msg->len, 0); |
|---|
| 454 | msgb_free(msg); |
|---|
| 455 | |
|---|
| 456 | /* set tx delay timer for next event */ |
|---|
| 457 | e1i_ts->sign.tx_timer.cb = timeout_ts1_write; |
|---|
| 458 | e1i_ts->sign.tx_timer.data = e1i_ts; |
|---|
| 459 | bsc_schedule_timer(&e1i_ts->sign.tx_timer, 0, 100000); |
|---|
| 460 | |
|---|
| 461 | return ret; |
|---|
| 462 | } |
|---|
| 463 | |
|---|
| 464 | /* callback from select.c in case one of the fd's can be read/written */ |
|---|
| 465 | static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what) |
|---|
| 466 | { |
|---|
| 467 | struct e1inp_line *line = bfd->data; |
|---|
| 468 | unsigned int ts_nr = bfd->priv_nr; |
|---|
| 469 | unsigned int idx = ts_nr-1; |
|---|
| 470 | struct e1inp_ts *e1i_ts; |
|---|
| 471 | int rc = 0; |
|---|
| 472 | |
|---|
| 473 | /* In case of early RSL we might not yet have a line */ |
|---|
| 474 | |
|---|
| 475 | if (line) |
|---|
| 476 | e1i_ts = &line->ts[idx]; |
|---|
| 477 | |
|---|
| 478 | if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) { |
|---|
| 479 | if (what & BSC_FD_READ) |
|---|
| 480 | rc = handle_ts1_read(bfd); |
|---|
| 481 | if (what & BSC_FD_WRITE) |
|---|
| 482 | rc = handle_ts1_write(bfd); |
|---|
| 483 | } else |
|---|
| 484 | LOGP(DINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type); |
|---|
| 485 | |
|---|
| 486 | return rc; |
|---|
| 487 | } |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | struct e1inp_driver ipaccess_driver = { |
|---|
| 491 | .name = "ip.access", |
|---|
| 492 | .want_write = ts_want_write, |
|---|
| 493 | }; |
|---|
| 494 | |
|---|
| 495 | /* callback of the OML listening filedescriptor */ |
|---|
| 496 | static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what) |
|---|
| 497 | { |
|---|
| 498 | int ret; |
|---|
| 499 | int idx = 0; |
|---|
| 500 | struct e1inp_line *line; |
|---|
| 501 | struct e1inp_ts *e1i_ts; |
|---|
| 502 | struct bsc_fd *bfd; |
|---|
| 503 | struct sockaddr_in sa; |
|---|
| 504 | socklen_t sa_len = sizeof(sa); |
|---|
| 505 | |
|---|
| 506 | if (!(what & BSC_FD_READ)) |
|---|
| 507 | return 0; |
|---|
| 508 | |
|---|
| 509 | ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len); |
|---|
| 510 | if (ret < 0) { |
|---|
| 511 | perror("accept"); |
|---|
| 512 | return ret; |
|---|
| 513 | } |
|---|
| 514 | LOGP(DINP, LOGL_NOTICE, "accept()ed new OML link from %s\n", |
|---|
| 515 | inet_ntoa(sa.sin_addr)); |
|---|
| 516 | |
|---|
| 517 | line = talloc_zero(tall_bsc_ctx, struct e1inp_line); |
|---|
| 518 | if (!line) { |
|---|
| 519 | close(ret); |
|---|
| 520 | return -ENOMEM; |
|---|
| 521 | } |
|---|
| 522 | line->driver = &ipaccess_driver; |
|---|
| 523 | //line->driver_data = e1h; |
|---|
| 524 | /* create virrtual E1 timeslots for signalling */ |
|---|
| 525 | e1inp_ts_config(&line->ts[1-1], line, E1INP_TS_TYPE_SIGN); |
|---|
| 526 | |
|---|
| 527 | e1i_ts = &line->ts[idx]; |
|---|
| 528 | |
|---|
| 529 | bfd = &e1i_ts->driver.ipaccess.fd; |
|---|
| 530 | bfd->fd = ret; |
|---|
| 531 | bfd->data = line; |
|---|
| 532 | bfd->priv_nr = 1; |
|---|
| 533 | bfd->cb = ipaccess_fd_cb; |
|---|
| 534 | bfd->when = BSC_FD_READ; |
|---|
| 535 | ret = bsc_register_fd(bfd); |
|---|
| 536 | if (ret < 0) { |
|---|
| 537 | LOGP(DINP, LOGL_ERROR, "could not register FD\n"); |
|---|
| 538 | close(bfd->fd); |
|---|
| 539 | talloc_free(line); |
|---|
| 540 | return ret; |
|---|
| 541 | } |
|---|
| 542 | |
|---|
| 543 | /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */ |
|---|
| 544 | ret = ipaccess_send_id_req(bfd->fd); |
|---|
| 545 | |
|---|
| 546 | return ret; |
|---|
| 547 | //return e1inp_line_register(line); |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | static int rsl_listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what) |
|---|
| 551 | { |
|---|
| 552 | struct sockaddr_in sa; |
|---|
| 553 | socklen_t sa_len = sizeof(sa); |
|---|
| 554 | struct bsc_fd *bfd; |
|---|
| 555 | int ret; |
|---|
| 556 | |
|---|
| 557 | if (!(what & BSC_FD_READ)) |
|---|
| 558 | return 0; |
|---|
| 559 | |
|---|
| 560 | bfd = talloc_zero(tall_bsc_ctx, struct bsc_fd); |
|---|
| 561 | if (!bfd) |
|---|
| 562 | return -ENOMEM; |
|---|
| 563 | |
|---|
| 564 | /* Some BTS has connected to us, but we don't know yet which line |
|---|
| 565 | * (as created by the OML link) to associate it with. Thus, we |
|---|
| 566 | * allocate a temporary bfd until we have received ID from BTS */ |
|---|
| 567 | |
|---|
| 568 | bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len); |
|---|
| 569 | if (bfd->fd < 0) { |
|---|
| 570 | perror("accept"); |
|---|
| 571 | return bfd->fd; |
|---|
| 572 | } |
|---|
| 573 | LOGP(DINP, LOGL_NOTICE, "accept()ed new RSL link from %s\n", inet_ntoa(sa.sin_addr)); |
|---|
| 574 | bfd->priv_nr = 2; |
|---|
| 575 | bfd->cb = ipaccess_fd_cb; |
|---|
| 576 | bfd->when = BSC_FD_READ; |
|---|
| 577 | ret = bsc_register_fd(bfd); |
|---|
| 578 | if (ret < 0) { |
|---|
| 579 | LOGP(DINP, LOGL_ERROR, "could not register FD\n"); |
|---|
| 580 | close(bfd->fd); |
|---|
| 581 | talloc_free(bfd); |
|---|
| 582 | return ret; |
|---|
| 583 | } |
|---|
| 584 | /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */ |
|---|
| 585 | ret = write(bfd->fd, id_req, sizeof(id_req)); |
|---|
| 586 | |
|---|
| 587 | return 0; |
|---|
| 588 | } |
|---|
| 589 | |
|---|
| 590 | static int make_sock(struct bsc_fd *bfd, u_int16_t port, |
|---|
| 591 | int (*cb)(struct bsc_fd *fd, unsigned int what)) |
|---|
| 592 | { |
|---|
| 593 | struct sockaddr_in addr; |
|---|
| 594 | int ret, on = 1; |
|---|
| 595 | |
|---|
| 596 | bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
|---|
| 597 | bfd->cb = cb; |
|---|
| 598 | bfd->when = BSC_FD_READ; |
|---|
| 599 | //bfd->data = line; |
|---|
| 600 | |
|---|
| 601 | if (bfd->fd < 0) { |
|---|
| 602 | LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n"); |
|---|
| 603 | return -EIO; |
|---|
| 604 | } |
|---|
| 605 | |
|---|
| 606 | memset(&addr, 0, sizeof(addr)); |
|---|
| 607 | addr.sin_family = AF_INET; |
|---|
| 608 | addr.sin_port = htons(port); |
|---|
| 609 | addr.sin_addr.s_addr = INADDR_ANY; |
|---|
| 610 | |
|---|
| 611 | setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
|---|
| 612 | |
|---|
| 613 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
|---|
| 614 | if (ret < 0) { |
|---|
| 615 | LOGP(DINP, LOGL_ERROR, "could not bind l2 socket %s\n", |
|---|
| 616 | strerror(errno)); |
|---|
| 617 | close(bfd->fd); |
|---|
| 618 | return -EIO; |
|---|
| 619 | } |
|---|
| 620 | |
|---|
| 621 | ret = listen(bfd->fd, 1); |
|---|
| 622 | if (ret < 0) { |
|---|
| 623 | perror("listen"); |
|---|
| 624 | close(bfd->fd); |
|---|
| 625 | return ret; |
|---|
| 626 | } |
|---|
| 627 | |
|---|
| 628 | ret = bsc_register_fd(bfd); |
|---|
| 629 | if (ret < 0) { |
|---|
| 630 | perror("register_listen_fd"); |
|---|
| 631 | close(bfd->fd); |
|---|
| 632 | return ret; |
|---|
| 633 | } |
|---|
| 634 | return 0; |
|---|
| 635 | } |
|---|
| 636 | |
|---|
| 637 | /* Actively connect to a BTS. Currently used by ipaccess-config.c */ |
|---|
| 638 | int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa) |
|---|
| 639 | { |
|---|
| 640 | struct e1inp_ts *e1i_ts = &line->ts[0]; |
|---|
| 641 | struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd; |
|---|
| 642 | int ret, on = 1; |
|---|
| 643 | |
|---|
| 644 | bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
|---|
| 645 | bfd->cb = ipaccess_fd_cb; |
|---|
| 646 | bfd->when = BSC_FD_READ | BSC_FD_WRITE; |
|---|
| 647 | bfd->data = line; |
|---|
| 648 | bfd->priv_nr = 1; |
|---|
| 649 | |
|---|
| 650 | if (bfd->fd < 0) { |
|---|
| 651 | LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n"); |
|---|
| 652 | return -EIO; |
|---|
| 653 | } |
|---|
| 654 | |
|---|
| 655 | setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
|---|
| 656 | |
|---|
| 657 | ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa)); |
|---|
| 658 | if (ret < 0) { |
|---|
| 659 | LOGP(DINP, LOGL_ERROR, "could not connect socket\n"); |
|---|
| 660 | close(bfd->fd); |
|---|
| 661 | return ret; |
|---|
| 662 | } |
|---|
| 663 | |
|---|
| 664 | ret = bsc_register_fd(bfd); |
|---|
| 665 | if (ret < 0) { |
|---|
| 666 | close(bfd->fd); |
|---|
| 667 | return ret; |
|---|
| 668 | } |
|---|
| 669 | |
|---|
| 670 | line->driver = &ipaccess_driver; |
|---|
| 671 | |
|---|
| 672 | return ret; |
|---|
| 673 | //return e1inp_line_register(line); |
|---|
| 674 | } |
|---|
| 675 | |
|---|
| 676 | int ipaccess_setup(struct gsm_network *gsmnet) |
|---|
| 677 | { |
|---|
| 678 | int ret; |
|---|
| 679 | |
|---|
| 680 | /* register the driver with the core */ |
|---|
| 681 | /* FIXME: do this in the plugin initializer function */ |
|---|
| 682 | ret = e1inp_driver_register(&ipaccess_driver); |
|---|
| 683 | if (ret) |
|---|
| 684 | return ret; |
|---|
| 685 | |
|---|
| 686 | e1h = talloc_zero(tall_bsc_ctx, struct ia_e1_handle); |
|---|
| 687 | if (!e1h) |
|---|
| 688 | return -ENOMEM; |
|---|
| 689 | |
|---|
| 690 | e1h->gsmnet = gsmnet; |
|---|
| 691 | |
|---|
| 692 | /* Listen for OML connections */ |
|---|
| 693 | ret = make_sock(&e1h->listen_fd, IPA_TCP_PORT_OML, listen_fd_cb); |
|---|
| 694 | if (ret < 0) |
|---|
| 695 | return ret; |
|---|
| 696 | |
|---|
| 697 | /* Listen for RSL connections */ |
|---|
| 698 | ret = make_sock(&e1h->rsl_listen_fd, IPA_TCP_PORT_RSL, rsl_listen_fd_cb); |
|---|
| 699 | |
|---|
| 700 | return ret; |
|---|
| 701 | } |
|---|