Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 1 | /* OpenBSC Abis input driver for ip.access */ |
| 2 | |
| 3 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 4 | * (C) 2010 by Holger Hans Peter Freyther |
| 5 | * (C) 2010 by On-Waves |
| 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include "internal.h" |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <unistd.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <errno.h> |
| 30 | #include <string.h> |
| 31 | #include <time.h> |
| 32 | #include <sys/fcntl.h> |
| 33 | #include <sys/socket.h> |
| 34 | #include <sys/ioctl.h> |
| 35 | #include <arpa/inet.h> |
| 36 | |
| 37 | #include <osmocom/core/select.h> |
| 38 | #include <osmocom/gsm/tlv.h> |
| 39 | #include <osmocom/core/msgb.h> |
| 40 | #include <osmocom/core/logging.h> |
| 41 | #include <talloc.h> |
Pablo Neira Ayuso | 177094b | 2011-06-07 12:21:51 +0200 | [diff] [blame] | 42 | #include <osmocom/abis/e1_input.h> |
| 43 | #include <osmocom/abis/ipaccess.h> |
Pablo Neira Ayuso | f67471f | 2011-06-07 11:25:49 +0200 | [diff] [blame] | 44 | #include <osmocom/core/socket.h> |
Pablo Neira Ayuso | 0b099b2 | 2011-06-09 13:14:11 +0200 | [diff] [blame] | 45 | #include <osmocom/abis/logging.h> |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 46 | #include <osmocom/abis/ipa.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 47 | |
| 48 | #define PRIV_OML 1 |
| 49 | #define PRIV_RSL 2 |
| 50 | |
Pablo Neira Ayuso | 54b4979 | 2011-06-07 12:15:26 +0200 | [diff] [blame] | 51 | static void *tall_ipa_ctx; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 52 | |
| 53 | /* data structure for one E1 interface with A-bis */ |
| 54 | struct ia_e1_handle { |
| 55 | struct osmo_fd listen_fd; |
| 56 | struct osmo_fd rsl_listen_fd; |
| 57 | struct gsm_network *gsmnet; |
| 58 | }; |
| 59 | |
| 60 | static struct ia_e1_handle *e1h; |
| 61 | |
| 62 | |
| 63 | #define TS1_ALLOC_SIZE 900 |
| 64 | |
| 65 | /* |
| 66 | * Common propietary IPA messages: |
| 67 | * - PONG: in reply to PING. |
| 68 | * - ID_REQUEST: first messages once OML has been established. |
| 69 | * - ID_ACK: in reply to ID_ACK. |
| 70 | */ |
| 71 | const uint8_t ipa_pong_msg[] = { |
| 72 | 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG |
| 73 | }; |
| 74 | |
| 75 | const uint8_t ipa_id_ack_msg[] = { |
| 76 | 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK |
| 77 | }; |
| 78 | |
| 79 | const uint8_t ipa_id_req_msg[] = { |
| 80 | 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET, |
| 81 | 0x01, IPAC_IDTAG_UNIT, |
| 82 | 0x01, IPAC_IDTAG_MACADDR, |
| 83 | 0x01, IPAC_IDTAG_LOCATION1, |
| 84 | 0x01, IPAC_IDTAG_LOCATION2, |
| 85 | 0x01, IPAC_IDTAG_EQUIPVERS, |
| 86 | 0x01, IPAC_IDTAG_SWVERSION, |
| 87 | 0x01, IPAC_IDTAG_UNITNAME, |
| 88 | 0x01, IPAC_IDTAG_SERNR, |
| 89 | }; |
| 90 | |
| 91 | static int ipaccess_send(int fd, const void *msg, size_t msglen) |
| 92 | { |
| 93 | int ret; |
| 94 | |
| 95 | ret = write(fd, msg, msglen); |
| 96 | if (ret < 0) |
| 97 | return ret; |
| 98 | if (ret < msglen) { |
| 99 | LOGP(DINP, LOGL_ERROR, "ipaccess_send: short write\n"); |
| 100 | return -EIO; |
| 101 | } |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | int ipaccess_send_pong(int fd) |
| 106 | { |
| 107 | return ipaccess_send(fd, ipa_pong_msg, sizeof(ipa_pong_msg)); |
| 108 | } |
| 109 | |
| 110 | int ipaccess_send_id_ack(int fd) |
| 111 | { |
| 112 | return ipaccess_send(fd, ipa_id_ack_msg, sizeof(ipa_id_ack_msg)); |
| 113 | } |
| 114 | |
| 115 | int ipaccess_send_id_req(int fd) |
| 116 | { |
| 117 | return ipaccess_send(fd, ipa_id_req_msg, sizeof(ipa_id_req_msg)); |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * read one ipa message from the socket |
| 122 | * return NULL in case of error |
| 123 | */ |
| 124 | struct msgb *ipaccess_read_msg(struct osmo_fd *bfd, int *error) |
| 125 | { |
| 126 | struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "Abis/IP"); |
| 127 | struct ipaccess_head *hh; |
| 128 | int len, ret = 0; |
| 129 | |
| 130 | if (!msg) { |
| 131 | *error = -ENOMEM; |
| 132 | return NULL; |
| 133 | } |
| 134 | |
| 135 | /* first read our 3-byte header */ |
| 136 | hh = (struct ipaccess_head *) msg->data; |
| 137 | ret = recv(bfd->fd, msg->data, sizeof(*hh), 0); |
| 138 | if (ret == 0) { |
| 139 | msgb_free(msg); |
| 140 | *error = ret; |
| 141 | return NULL; |
| 142 | } else if (ret != sizeof(*hh)) { |
| 143 | if (errno != EAGAIN) |
| 144 | LOGP(DINP, LOGL_ERROR, "recv error %d %s\n", ret, strerror(errno)); |
| 145 | msgb_free(msg); |
| 146 | *error = ret; |
| 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | msgb_put(msg, ret); |
| 151 | |
| 152 | /* then read te length as specified in header */ |
| 153 | msg->l2h = msg->data + sizeof(*hh); |
| 154 | len = ntohs(hh->len); |
| 155 | |
| 156 | if (len < 0 || TS1_ALLOC_SIZE < len + sizeof(*hh)) { |
| 157 | LOGP(DINP, LOGL_ERROR, "Can not read this packet. %d avail\n", len); |
| 158 | msgb_free(msg); |
| 159 | *error = -EIO; |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | ret = recv(bfd->fd, msg->l2h, len, 0); |
| 164 | if (ret < len) { |
| 165 | LOGP(DINP, LOGL_ERROR, "short read! Got %d from %d\n", ret, len); |
| 166 | msgb_free(msg); |
| 167 | *error = -EIO; |
| 168 | return NULL; |
| 169 | } |
| 170 | msgb_put(msg, ret); |
| 171 | |
| 172 | return msg; |
| 173 | } |
| 174 | |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 175 | /* base handling of the ip.access protocol */ |
| 176 | int ipaccess_rcvmsg_base(struct msgb *msg, |
| 177 | struct osmo_fd *bfd) |
| 178 | { |
| 179 | uint8_t msg_type = *(msg->l2h); |
| 180 | int ret = 0; |
| 181 | |
| 182 | switch (msg_type) { |
| 183 | case IPAC_MSGT_PING: |
| 184 | ret = ipaccess_send_pong(bfd->fd); |
| 185 | break; |
| 186 | case IPAC_MSGT_PONG: |
| 187 | DEBUGP(DMI, "PONG!\n"); |
| 188 | break; |
| 189 | case IPAC_MSGT_ID_ACK: |
| 190 | DEBUGP(DMI, "ID_ACK? -> ACK!\n"); |
| 191 | ret = ipaccess_send_id_ack(bfd->fd); |
| 192 | break; |
| 193 | } |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg, |
| 198 | struct osmo_fd *bfd) |
| 199 | { |
| 200 | uint8_t msg_type = *(msg->l2h); |
| 201 | |
| 202 | /* handle base messages */ |
| 203 | ipaccess_rcvmsg_base(msg, bfd); |
| 204 | |
| 205 | switch (msg_type) { |
| 206 | case IPAC_MSGT_ID_RESP: |
| 207 | DEBUGP(DMI, "ID_RESP\n"); |
| 208 | if (!line->ops.sign_link_up) { |
| 209 | LOGP(DINP, LOGL_ERROR, "Fix your application, " |
| 210 | "no action set if the signalling link " |
| 211 | "becomes ready\n"); |
| 212 | return -EINVAL; |
| 213 | } |
| 214 | line->ops.sign_link_up(msg, line); |
| 215 | break; |
| 216 | } |
| 217 | return 0; |
| 218 | } |
| 219 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 220 | static int handle_ts1_read(struct osmo_fd *bfd) |
| 221 | { |
| 222 | struct e1inp_line *line = bfd->data; |
| 223 | unsigned int ts_nr = bfd->priv_nr; |
| 224 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 225 | struct e1inp_sign_link *link; |
| 226 | struct ipaccess_head *hh; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 227 | struct msgb *msg; |
| 228 | int ret = 0, error; |
| 229 | |
| 230 | msg = ipaccess_read_msg(bfd, &error); |
| 231 | if (!msg) { |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 232 | if (e1i_ts->line->ops.error) |
| 233 | e1i_ts->line->ops.error(NULL, error); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 234 | if (error == 0) { |
| 235 | osmo_fd_unregister(bfd); |
| 236 | close(bfd->fd); |
| 237 | bfd->fd = -1; |
| 238 | } |
| 239 | return error; |
| 240 | } |
| 241 | DEBUGP(DMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg))); |
| 242 | |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 243 | hh = (struct ipaccess_head *) msg->data; |
| 244 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 245 | ipaccess_rcvmsg(line, msg, bfd); |
| 246 | msgb_free(msg); |
| 247 | return ret; |
| 248 | } |
| 249 | /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg() |
| 250 | * might have free'd it !!! */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 251 | |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 252 | link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0); |
| 253 | if (!link) { |
| 254 | LOGP(DINP, LOGL_ERROR, "no matching signalling link for " |
| 255 | "hh->proto=0x%02x\n", hh->proto); |
| 256 | msgb_free(msg); |
| 257 | return -EIO; |
| 258 | } |
| 259 | msg->dst = link; |
| 260 | |
| 261 | /* XXX better use e1inp_ts_rx? */ |
| 262 | if (!e1i_ts->line->ops.sign_link) { |
| 263 | LOGP(DINP, LOGL_ERROR, "Fix your application, " |
| 264 | "no action set for signalling messages.\n"); |
| 265 | return -ENOENT; |
| 266 | } |
| 267 | e1i_ts->line->ops.sign_link(msg, link); |
| 268 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | void ipaccess_prepend_header_ext(struct msgb *msg, int proto) |
| 273 | { |
| 274 | struct ipaccess_head_ext *hh_ext; |
| 275 | |
| 276 | /* prepend the osmo ip.access header extension */ |
| 277 | hh_ext = (struct ipaccess_head_ext *) msgb_push(msg, sizeof(*hh_ext)); |
| 278 | hh_ext->proto = proto; |
| 279 | } |
| 280 | |
| 281 | void ipaccess_prepend_header(struct msgb *msg, int proto) |
| 282 | { |
| 283 | struct ipaccess_head *hh; |
| 284 | |
| 285 | /* prepend the ip.access header */ |
| 286 | hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh)); |
| 287 | hh->len = htons(msg->len - sizeof(*hh)); |
| 288 | hh->proto = proto; |
| 289 | } |
| 290 | |
| 291 | static int ts_want_write(struct e1inp_ts *e1i_ts) |
| 292 | { |
| 293 | e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE; |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static void timeout_ts1_write(void *data) |
| 299 | { |
| 300 | struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data; |
| 301 | |
| 302 | /* trigger write of ts1, due to tx delay timer */ |
| 303 | ts_want_write(e1i_ts); |
| 304 | } |
| 305 | |
| 306 | static int handle_ts1_write(struct osmo_fd *bfd) |
| 307 | { |
| 308 | struct e1inp_line *line = bfd->data; |
| 309 | unsigned int ts_nr = bfd->priv_nr; |
| 310 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 311 | struct e1inp_sign_link *sign_link; |
| 312 | struct msgb *msg; |
| 313 | uint8_t proto; |
| 314 | int ret; |
| 315 | |
| 316 | bfd->when &= ~BSC_FD_WRITE; |
| 317 | |
| 318 | /* get the next msg for this timeslot */ |
| 319 | msg = e1inp_tx_ts(e1i_ts, &sign_link); |
| 320 | if (!msg) { |
| 321 | /* no message after tx delay timer */ |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | switch (sign_link->type) { |
| 326 | case E1INP_SIGN_OML: |
| 327 | proto = IPAC_PROTO_OML; |
| 328 | break; |
| 329 | case E1INP_SIGN_RSL: |
| 330 | proto = IPAC_PROTO_RSL; |
| 331 | break; |
| 332 | default: |
| 333 | msgb_free(msg); |
| 334 | bfd->when |= BSC_FD_WRITE; /* come back for more msg */ |
| 335 | return -EINVAL; |
| 336 | } |
| 337 | |
| 338 | msg->l2h = msg->data; |
| 339 | ipaccess_prepend_header(msg, sign_link->tei); |
| 340 | |
| 341 | DEBUGP(DMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg))); |
| 342 | |
| 343 | ret = send(bfd->fd, msg->data, msg->len, 0); |
| 344 | msgb_free(msg); |
| 345 | |
| 346 | /* set tx delay timer for next event */ |
| 347 | e1i_ts->sign.tx_timer.cb = timeout_ts1_write; |
| 348 | e1i_ts->sign.tx_timer.data = e1i_ts; |
| 349 | |
| 350 | /* Reducing this might break the nanoBTS 900 init. */ |
| 351 | osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay); |
| 352 | |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | /* callback from select.c in case one of the fd's can be read/written */ |
| 357 | static int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what) |
| 358 | { |
| 359 | struct e1inp_line *line = bfd->data; |
| 360 | unsigned int ts_nr = bfd->priv_nr; |
| 361 | unsigned int idx = ts_nr-1; |
| 362 | struct e1inp_ts *e1i_ts; |
| 363 | int rc = 0; |
| 364 | |
| 365 | /* In case of early RSL we might not yet have a line */ |
| 366 | |
| 367 | if (line) |
| 368 | e1i_ts = &line->ts[idx]; |
| 369 | |
| 370 | if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) { |
| 371 | if (what & BSC_FD_READ) |
| 372 | rc = handle_ts1_read(bfd); |
| 373 | if (what & BSC_FD_WRITE) |
| 374 | rc = handle_ts1_write(bfd); |
| 375 | } else |
| 376 | LOGP(DINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type); |
| 377 | |
| 378 | return rc; |
| 379 | } |
| 380 | |
Pablo Neira Ayuso | c00ee73 | 2011-06-21 12:22:49 +0200 | [diff] [blame] | 381 | static int ipaccess_line_update(struct e1inp_line *line, |
| 382 | enum e1inp_line_role role, const char *addr); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 383 | |
| 384 | struct e1inp_driver ipaccess_driver = { |
| 385 | .name = "ipa", |
| 386 | .want_write = ts_want_write, |
| 387 | .line_update = ipaccess_line_update, |
| 388 | .default_delay = 0, |
| 389 | }; |
| 390 | |
| 391 | /* callback of the OML listening filedescriptor */ |
| 392 | static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what) |
| 393 | { |
| 394 | int ret; |
| 395 | int idx = 0; |
| 396 | int i; |
| 397 | struct e1inp_line *line = listen_bfd->data; |
| 398 | struct e1inp_ts *e1i_ts; |
| 399 | struct osmo_fd *bfd; |
| 400 | struct sockaddr_in sa; |
| 401 | socklen_t sa_len = sizeof(sa); |
| 402 | |
| 403 | if (!(what & BSC_FD_READ)) |
| 404 | return 0; |
| 405 | |
| 406 | ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 407 | if (ret < 0) { |
| 408 | perror("accept"); |
| 409 | return ret; |
| 410 | } |
| 411 | LOGP(DINP, LOGL_NOTICE, "accept()ed new OML link from %s\n", |
| 412 | inet_ntoa(sa.sin_addr)); |
| 413 | |
| 414 | /* create virrtual E1 timeslots for signalling */ |
| 415 | e1inp_ts_config_sign(&line->ts[1-1], line); |
| 416 | |
| 417 | /* initialize the fds */ |
| 418 | for (i = 0; i < ARRAY_SIZE(line->ts); ++i) |
| 419 | line->ts[i].driver.ipaccess.fd.fd = -1; |
| 420 | |
| 421 | e1i_ts = &line->ts[idx]; |
| 422 | |
| 423 | bfd = &e1i_ts->driver.ipaccess.fd; |
| 424 | bfd->fd = ret; |
| 425 | bfd->data = line; |
| 426 | bfd->priv_nr = PRIV_OML; |
| 427 | bfd->cb = ipaccess_fd_cb; |
| 428 | bfd->when = BSC_FD_READ; |
| 429 | ret = osmo_fd_register(bfd); |
| 430 | if (ret < 0) { |
| 431 | LOGP(DINP, LOGL_ERROR, "could not register FD\n"); |
| 432 | close(bfd->fd); |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */ |
| 437 | ret = ipaccess_send_id_req(bfd->fd); |
| 438 | |
| 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | static int rsl_listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what) |
| 443 | { |
| 444 | struct sockaddr_in sa; |
| 445 | socklen_t sa_len = sizeof(sa); |
| 446 | struct osmo_fd *bfd; |
| 447 | int ret; |
| 448 | |
| 449 | if (!(what & BSC_FD_READ)) |
| 450 | return 0; |
| 451 | |
Pablo Neira Ayuso | 54b4979 | 2011-06-07 12:15:26 +0200 | [diff] [blame] | 452 | bfd = talloc_zero(tall_ipa_ctx, struct osmo_fd); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 453 | if (!bfd) |
| 454 | return -ENOMEM; |
| 455 | |
| 456 | /* Some BTS has connected to us, but we don't know yet which line |
| 457 | * (as created by the OML link) to associate it with. Thus, we |
| 458 | * allocate a temporary bfd until we have received ID from BTS */ |
| 459 | |
| 460 | bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 461 | if (bfd->fd < 0) { |
| 462 | perror("accept"); |
| 463 | return bfd->fd; |
| 464 | } |
| 465 | LOGP(DINP, LOGL_NOTICE, "accept()ed new RSL link from %s\n", inet_ntoa(sa.sin_addr)); |
| 466 | bfd->priv_nr = PRIV_RSL; |
| 467 | bfd->cb = ipaccess_fd_cb; |
| 468 | bfd->when = BSC_FD_READ; |
| 469 | ret = osmo_fd_register(bfd); |
| 470 | if (ret < 0) { |
| 471 | LOGP(DINP, LOGL_ERROR, "could not register FD\n"); |
| 472 | close(bfd->fd); |
| 473 | talloc_free(bfd); |
| 474 | return ret; |
| 475 | } |
| 476 | /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */ |
| 477 | ret = ipaccess_send_id_req(bfd->fd); |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
Pablo Neira Ayuso | c00ee73 | 2011-06-21 12:22:49 +0200 | [diff] [blame] | 482 | static int ipaccess_line_update(struct e1inp_line *line, |
| 483 | enum e1inp_line_role role, const char *addr) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 484 | { |
| 485 | int ret = -ENOENT; |
| 486 | |
| 487 | switch(role) { |
| 488 | case E1INP_LINE_R_BSC: |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 489 | LOGP(DINP, LOGL_NOTICE, "enabling ipaccess BSC mode\n"); |
| 490 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 491 | /* Listen for OML connections */ |
Pablo Neira Ayuso | f67471f | 2011-06-07 11:25:49 +0200 | [diff] [blame] | 492 | ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, |
Pablo Neira Ayuso | c00ee73 | 2011-06-21 12:22:49 +0200 | [diff] [blame] | 493 | addr, IPA_TCP_PORT_OML, OSMO_SOCK_F_BIND); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 494 | if (ret < 0) |
| 495 | return ret; |
| 496 | |
Pablo Neira Ayuso | f67471f | 2011-06-07 11:25:49 +0200 | [diff] [blame] | 497 | e1h->listen_fd.fd = ret; |
| 498 | e1h->listen_fd.when |= BSC_FD_READ; |
| 499 | e1h->listen_fd.cb = listen_fd_cb; |
| 500 | e1h->listen_fd.data = line; |
| 501 | |
| 502 | if (osmo_fd_register(&e1h->listen_fd) < 0) { |
| 503 | close(ret); |
| 504 | return ret; |
| 505 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 506 | /* Listen for RSL connections */ |
Pablo Neira Ayuso | f67471f | 2011-06-07 11:25:49 +0200 | [diff] [blame] | 507 | ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, |
Pablo Neira Ayuso | c00ee73 | 2011-06-21 12:22:49 +0200 | [diff] [blame] | 508 | addr, IPA_TCP_PORT_RSL, OSMO_SOCK_F_BIND); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 509 | if (ret < 0) |
| 510 | return ret; |
Pablo Neira Ayuso | f67471f | 2011-06-07 11:25:49 +0200 | [diff] [blame] | 511 | |
| 512 | e1h->rsl_listen_fd.fd = ret; |
| 513 | e1h->rsl_listen_fd.when |= BSC_FD_READ; |
| 514 | e1h->rsl_listen_fd.cb = rsl_listen_fd_cb; |
| 515 | e1h->rsl_listen_fd.data = NULL; |
| 516 | |
| 517 | if (osmo_fd_register(&e1h->rsl_listen_fd) < 0) { |
| 518 | close(ret); |
| 519 | return ret; |
| 520 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 521 | break; |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 522 | case E1INP_LINE_R_BTS: { |
| 523 | struct ipa_link *link; |
| 524 | |
| 525 | LOGP(DINP, LOGL_NOTICE, "enabling ipaccess BTS mode\n"); |
| 526 | |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame^] | 527 | link = ipa_client_link_create(tall_ipa_ctx, |
| 528 | addr, IPA_TCP_PORT_OML); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 529 | if (link == NULL) { |
| 530 | perror("ipa_client_link_create: "); |
| 531 | return -ENOMEM; |
| 532 | } |
| 533 | if (ipa_client_link_open(link) < 0) { |
| 534 | perror("ipa_client_link_open: "); |
| 535 | ipa_client_link_close(link); |
| 536 | ipa_client_link_destroy(link); |
| 537 | return -EIO; |
| 538 | } |
| 539 | ret = 0; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 540 | break; |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 541 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 542 | default: |
| 543 | break; |
| 544 | } |
| 545 | return ret; |
| 546 | } |
| 547 | |
| 548 | /* Actively connect to a BTS. Currently used by ipaccess-config.c */ |
| 549 | int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa) |
| 550 | { |
| 551 | struct e1inp_ts *e1i_ts = &line->ts[0]; |
| 552 | struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd; |
| 553 | int ret, on = 1; |
| 554 | |
| 555 | bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 556 | bfd->cb = ipaccess_fd_cb; |
| 557 | bfd->when = BSC_FD_READ | BSC_FD_WRITE; |
| 558 | bfd->data = line; |
| 559 | bfd->priv_nr = PRIV_OML; |
| 560 | |
| 561 | if (bfd->fd < 0) { |
| 562 | LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n"); |
| 563 | return -EIO; |
| 564 | } |
| 565 | |
| 566 | setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 567 | |
| 568 | ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa)); |
| 569 | if (ret < 0) { |
| 570 | LOGP(DINP, LOGL_ERROR, "could not connect socket\n"); |
| 571 | close(bfd->fd); |
| 572 | return ret; |
| 573 | } |
| 574 | |
| 575 | ret = osmo_fd_register(bfd); |
| 576 | if (ret < 0) { |
| 577 | close(bfd->fd); |
| 578 | return ret; |
| 579 | } |
| 580 | |
| 581 | line->driver = &ipaccess_driver; |
| 582 | |
| 583 | return ret; |
| 584 | //return e1inp_line_register(line); |
| 585 | } |
| 586 | |
| 587 | int ipaccess_setup(struct gsm_network *gsmnet) |
| 588 | { |
| 589 | e1h->gsmnet = gsmnet; |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | void e1inp_ipaccess_init(void) |
| 594 | { |
Pablo Neira Ayuso | 54b4979 | 2011-06-07 12:15:26 +0200 | [diff] [blame] | 595 | tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa"); |
| 596 | |
| 597 | e1h = talloc_zero(tall_ipa_ctx, struct ia_e1_handle); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 598 | if (!e1h) |
| 599 | return; |
| 600 | |
| 601 | e1inp_driver_register(&ipaccess_driver); |
| 602 | } |