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 | |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 120 | /* base handling of the ip.access protocol */ |
| 121 | int ipaccess_rcvmsg_base(struct msgb *msg, |
| 122 | struct osmo_fd *bfd) |
| 123 | { |
| 124 | uint8_t msg_type = *(msg->l2h); |
| 125 | int ret = 0; |
| 126 | |
| 127 | switch (msg_type) { |
| 128 | case IPAC_MSGT_PING: |
| 129 | ret = ipaccess_send_pong(bfd->fd); |
| 130 | break; |
| 131 | case IPAC_MSGT_PONG: |
| 132 | DEBUGP(DMI, "PONG!\n"); |
| 133 | break; |
| 134 | case IPAC_MSGT_ID_ACK: |
| 135 | DEBUGP(DMI, "ID_ACK? -> ACK!\n"); |
| 136 | ret = ipaccess_send_id_ack(bfd->fd); |
| 137 | break; |
| 138 | } |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg, |
| 143 | struct osmo_fd *bfd) |
| 144 | { |
| 145 | uint8_t msg_type = *(msg->l2h); |
| 146 | |
| 147 | /* handle base messages */ |
| 148 | ipaccess_rcvmsg_base(msg, bfd); |
| 149 | |
| 150 | switch (msg_type) { |
| 151 | case IPAC_MSGT_ID_RESP: |
| 152 | DEBUGP(DMI, "ID_RESP\n"); |
| 153 | if (!line->ops.sign_link_up) { |
| 154 | LOGP(DINP, LOGL_ERROR, "Fix your application, " |
| 155 | "no action set if the signalling link " |
| 156 | "becomes ready\n"); |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | line->ops.sign_link_up(msg, line); |
| 160 | break; |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 165 | static int handle_ts1_read(struct osmo_fd *bfd) |
| 166 | { |
| 167 | struct e1inp_line *line = bfd->data; |
| 168 | unsigned int ts_nr = bfd->priv_nr; |
Pablo Neira Ayuso | 29465d3 | 2011-06-21 14:21:33 +0200 | [diff] [blame] | 169 | struct e1inp_ts *e1i_ts = NULL; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 170 | struct e1inp_sign_link *link; |
| 171 | struct ipaccess_head *hh; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 172 | struct msgb *msg; |
| 173 | int ret = 0, error; |
| 174 | |
Pablo Neira Ayuso | 7a24940 | 2011-06-21 14:15:46 +0200 | [diff] [blame] | 175 | error = ipa_msg_recv(bfd->fd, &msg); |
| 176 | if (error <= 0) { |
Pablo Neira Ayuso | 29465d3 | 2011-06-21 14:21:33 +0200 | [diff] [blame] | 177 | /* skip if RSL line is not set yet. */ |
| 178 | if (e1i_ts && e1i_ts->line->ops.error) |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 179 | e1i_ts->line->ops.error(NULL, error); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 180 | if (error == 0) { |
| 181 | osmo_fd_unregister(bfd); |
| 182 | close(bfd->fd); |
| 183 | bfd->fd = -1; |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame^] | 184 | talloc_free(bfd); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 185 | } |
| 186 | return error; |
| 187 | } |
| 188 | DEBUGP(DMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg))); |
| 189 | |
Pablo Neira Ayuso | 29465d3 | 2011-06-21 14:21:33 +0200 | [diff] [blame] | 190 | /* Now that we're sure that we've got a line for socket. */ |
| 191 | e1i_ts = &line->ts[ts_nr-1]; |
| 192 | |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 193 | hh = (struct ipaccess_head *) msg->data; |
| 194 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 195 | ipaccess_rcvmsg(line, msg, bfd); |
| 196 | msgb_free(msg); |
| 197 | return ret; |
| 198 | } |
| 199 | /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg() |
| 200 | * might have free'd it !!! */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 201 | |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 202 | link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0); |
| 203 | if (!link) { |
| 204 | LOGP(DINP, LOGL_ERROR, "no matching signalling link for " |
| 205 | "hh->proto=0x%02x\n", hh->proto); |
| 206 | msgb_free(msg); |
| 207 | return -EIO; |
| 208 | } |
| 209 | msg->dst = link; |
| 210 | |
| 211 | /* XXX better use e1inp_ts_rx? */ |
| 212 | if (!e1i_ts->line->ops.sign_link) { |
| 213 | LOGP(DINP, LOGL_ERROR, "Fix your application, " |
| 214 | "no action set for signalling messages.\n"); |
| 215 | return -ENOENT; |
| 216 | } |
| 217 | e1i_ts->line->ops.sign_link(msg, link); |
| 218 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | void ipaccess_prepend_header_ext(struct msgb *msg, int proto) |
| 223 | { |
| 224 | struct ipaccess_head_ext *hh_ext; |
| 225 | |
| 226 | /* prepend the osmo ip.access header extension */ |
| 227 | hh_ext = (struct ipaccess_head_ext *) msgb_push(msg, sizeof(*hh_ext)); |
| 228 | hh_ext->proto = proto; |
| 229 | } |
| 230 | |
| 231 | void ipaccess_prepend_header(struct msgb *msg, int proto) |
| 232 | { |
| 233 | struct ipaccess_head *hh; |
| 234 | |
| 235 | /* prepend the ip.access header */ |
| 236 | hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh)); |
| 237 | hh->len = htons(msg->len - sizeof(*hh)); |
| 238 | hh->proto = proto; |
| 239 | } |
| 240 | |
| 241 | static int ts_want_write(struct e1inp_ts *e1i_ts) |
| 242 | { |
| 243 | e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE; |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static void timeout_ts1_write(void *data) |
| 249 | { |
| 250 | struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data; |
| 251 | |
| 252 | /* trigger write of ts1, due to tx delay timer */ |
| 253 | ts_want_write(e1i_ts); |
| 254 | } |
| 255 | |
| 256 | static int handle_ts1_write(struct osmo_fd *bfd) |
| 257 | { |
| 258 | struct e1inp_line *line = bfd->data; |
| 259 | unsigned int ts_nr = bfd->priv_nr; |
| 260 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 261 | struct e1inp_sign_link *sign_link; |
| 262 | struct msgb *msg; |
| 263 | uint8_t proto; |
| 264 | int ret; |
| 265 | |
| 266 | bfd->when &= ~BSC_FD_WRITE; |
| 267 | |
| 268 | /* get the next msg for this timeslot */ |
| 269 | msg = e1inp_tx_ts(e1i_ts, &sign_link); |
| 270 | if (!msg) { |
| 271 | /* no message after tx delay timer */ |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | switch (sign_link->type) { |
| 276 | case E1INP_SIGN_OML: |
| 277 | proto = IPAC_PROTO_OML; |
| 278 | break; |
| 279 | case E1INP_SIGN_RSL: |
| 280 | proto = IPAC_PROTO_RSL; |
| 281 | break; |
| 282 | default: |
| 283 | msgb_free(msg); |
| 284 | bfd->when |= BSC_FD_WRITE; /* come back for more msg */ |
| 285 | return -EINVAL; |
| 286 | } |
| 287 | |
| 288 | msg->l2h = msg->data; |
| 289 | ipaccess_prepend_header(msg, sign_link->tei); |
| 290 | |
| 291 | DEBUGP(DMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg))); |
| 292 | |
| 293 | ret = send(bfd->fd, msg->data, msg->len, 0); |
| 294 | msgb_free(msg); |
| 295 | |
| 296 | /* set tx delay timer for next event */ |
| 297 | e1i_ts->sign.tx_timer.cb = timeout_ts1_write; |
| 298 | e1i_ts->sign.tx_timer.data = e1i_ts; |
| 299 | |
| 300 | /* Reducing this might break the nanoBTS 900 init. */ |
| 301 | osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay); |
| 302 | |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | /* callback from select.c in case one of the fd's can be read/written */ |
| 307 | static int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what) |
| 308 | { |
| 309 | struct e1inp_line *line = bfd->data; |
| 310 | unsigned int ts_nr = bfd->priv_nr; |
| 311 | unsigned int idx = ts_nr-1; |
| 312 | struct e1inp_ts *e1i_ts; |
| 313 | int rc = 0; |
| 314 | |
| 315 | /* In case of early RSL we might not yet have a line */ |
| 316 | |
| 317 | if (line) |
| 318 | e1i_ts = &line->ts[idx]; |
| 319 | |
| 320 | if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) { |
| 321 | if (what & BSC_FD_READ) |
| 322 | rc = handle_ts1_read(bfd); |
| 323 | if (what & BSC_FD_WRITE) |
| 324 | rc = handle_ts1_write(bfd); |
| 325 | } else |
| 326 | LOGP(DINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type); |
| 327 | |
| 328 | return rc; |
| 329 | } |
| 330 | |
Pablo Neira Ayuso | c00ee73 | 2011-06-21 12:22:49 +0200 | [diff] [blame] | 331 | static int ipaccess_line_update(struct e1inp_line *line, |
| 332 | enum e1inp_line_role role, const char *addr); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 333 | |
| 334 | struct e1inp_driver ipaccess_driver = { |
| 335 | .name = "ipa", |
| 336 | .want_write = ts_want_write, |
| 337 | .line_update = ipaccess_line_update, |
| 338 | .default_delay = 0, |
| 339 | }; |
| 340 | |
| 341 | /* callback of the OML listening filedescriptor */ |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame^] | 342 | static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 343 | { |
| 344 | int ret; |
| 345 | int idx = 0; |
| 346 | int i; |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame^] | 347 | struct e1inp_line *line = link->line; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 348 | struct e1inp_ts *e1i_ts; |
| 349 | struct osmo_fd *bfd; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 350 | |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame^] | 351 | bfd = talloc_zero(tall_ipa_ctx, struct osmo_fd); |
| 352 | if (!bfd) |
| 353 | return -ENOMEM; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 354 | |
| 355 | /* create virrtual E1 timeslots for signalling */ |
| 356 | e1inp_ts_config_sign(&line->ts[1-1], line); |
| 357 | |
| 358 | /* initialize the fds */ |
| 359 | for (i = 0; i < ARRAY_SIZE(line->ts); ++i) |
| 360 | line->ts[i].driver.ipaccess.fd.fd = -1; |
| 361 | |
| 362 | e1i_ts = &line->ts[idx]; |
| 363 | |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame^] | 364 | bfd->fd = fd; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 365 | bfd->data = line; |
| 366 | bfd->priv_nr = PRIV_OML; |
| 367 | bfd->cb = ipaccess_fd_cb; |
| 368 | bfd->when = BSC_FD_READ; |
| 369 | ret = osmo_fd_register(bfd); |
| 370 | if (ret < 0) { |
| 371 | LOGP(DINP, LOGL_ERROR, "could not register FD\n"); |
| 372 | close(bfd->fd); |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame^] | 373 | talloc_free(bfd); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */ |
| 378 | ret = ipaccess_send_id_req(bfd->fd); |
| 379 | |
| 380 | return ret; |
| 381 | } |
| 382 | |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame^] | 383 | static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 384 | { |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 385 | struct osmo_fd *bfd; |
| 386 | int ret; |
| 387 | |
Pablo Neira Ayuso | 54b4979 | 2011-06-07 12:15:26 +0200 | [diff] [blame] | 388 | bfd = talloc_zero(tall_ipa_ctx, struct osmo_fd); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 389 | if (!bfd) |
| 390 | return -ENOMEM; |
| 391 | |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame^] | 392 | bfd->fd = fd; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 393 | bfd->priv_nr = PRIV_RSL; |
| 394 | bfd->cb = ipaccess_fd_cb; |
| 395 | bfd->when = BSC_FD_READ; |
| 396 | ret = osmo_fd_register(bfd); |
| 397 | if (ret < 0) { |
| 398 | LOGP(DINP, LOGL_ERROR, "could not register FD\n"); |
| 399 | close(bfd->fd); |
| 400 | talloc_free(bfd); |
| 401 | return ret; |
| 402 | } |
| 403 | /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */ |
| 404 | ret = ipaccess_send_id_req(bfd->fd); |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 409 | static int ipaccess_bts_cb(struct ipa_client_link *link, struct msgb *msg) |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 410 | { |
| 411 | struct ipaccess_head *hh = (struct ipaccess_head *) msg->data; |
| 412 | struct e1inp_ts *e1i_ts = NULL; |
| 413 | struct e1inp_sign_link *sign_link; |
| 414 | |
| 415 | /* special handling for IPA CCM. */ |
| 416 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 417 | uint8_t msg_type = *(msg->l2h); |
| 418 | |
| 419 | /* ping, pong and acknowledgment cases. */ |
| 420 | ipaccess_rcvmsg_base(msg, &link->ofd); |
| 421 | |
| 422 | /* this is a request for identification from the BSC. */ |
| 423 | if (msg_type == IPAC_MSGT_ID_GET) { |
| 424 | LOGP(DINP, LOGL_NOTICE, "received ID get\n"); |
| 425 | if (!link->line->ops.sign_link_up) { |
| 426 | LOGP(DINP, LOGL_ERROR, "Fix your application, " |
| 427 | "no action set if the signalling link " |
| 428 | "becomes ready\n"); |
| 429 | return -EINVAL; |
| 430 | } |
| 431 | link->line->ops.sign_link_up(msg, link->line); |
| 432 | } |
| 433 | return 0; |
| 434 | } else if (link->port == IPA_TCP_PORT_OML) |
| 435 | e1i_ts = &link->line->ts[0]; |
| 436 | else if (link->port == IPA_TCP_PORT_RSL) |
| 437 | e1i_ts = &link->line->ts[1]; |
| 438 | |
| 439 | /* look up for some existing signaling link. */ |
| 440 | sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0); |
| 441 | if (sign_link == NULL) { |
| 442 | LOGP(DINP, LOGL_ERROR, "no matching signalling link for " |
| 443 | "hh->proto=0x%02x\n", hh->proto); |
| 444 | msgb_free(msg); |
| 445 | return -EIO; |
| 446 | } |
| 447 | msg->dst = sign_link; |
| 448 | |
| 449 | /* XXX better use e1inp_ts_rx? */ |
| 450 | if (!link->line->ops.sign_link) { |
| 451 | LOGP(DINP, LOGL_ERROR, "Fix your application, " |
| 452 | "no action set for signalling messages.\n"); |
| 453 | return -ENOENT; |
| 454 | } |
| 455 | link->line->ops.sign_link(msg, sign_link); |
| 456 | return 0; |
| 457 | } |
| 458 | |
Pablo Neira Ayuso | c00ee73 | 2011-06-21 12:22:49 +0200 | [diff] [blame] | 459 | static int ipaccess_line_update(struct e1inp_line *line, |
| 460 | enum e1inp_line_role role, const char *addr) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 461 | { |
| 462 | int ret = -ENOENT; |
| 463 | |
| 464 | switch(role) { |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame^] | 465 | case E1INP_LINE_R_BSC: { |
| 466 | struct ipa_server_link *oml_link, *rsl_link; |
| 467 | |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 468 | LOGP(DINP, LOGL_NOTICE, "enabling ipaccess BSC mode\n"); |
| 469 | |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame^] | 470 | oml_link = ipa_server_link_create(tall_ipa_ctx, line, |
| 471 | "0.0.0.0", IPA_TCP_PORT_OML, |
| 472 | ipaccess_bsc_oml_cb); |
| 473 | if (oml_link == NULL) { |
| 474 | LOGP(DINP, LOGL_ERROR, "cannot create OML " |
| 475 | "BSC link: %s\n", strerror(errno)); |
| 476 | return -ENOMEM; |
Pablo Neira Ayuso | f67471f | 2011-06-07 11:25:49 +0200 | [diff] [blame] | 477 | } |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame^] | 478 | if (ipa_server_link_open(oml_link) < 0) { |
| 479 | LOGP(DINP, LOGL_ERROR, "cannot open OML BSC link: %s\n", |
| 480 | strerror(errno)); |
| 481 | ipa_server_link_close(oml_link); |
| 482 | ipa_server_link_destroy(oml_link); |
| 483 | return -EIO; |
Pablo Neira Ayuso | f67471f | 2011-06-07 11:25:49 +0200 | [diff] [blame] | 484 | } |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame^] | 485 | rsl_link = ipa_server_link_create(tall_ipa_ctx, line, |
| 486 | "0.0.0.0", IPA_TCP_PORT_RSL, |
| 487 | ipaccess_bsc_rsl_cb); |
| 488 | if (rsl_link == NULL) { |
| 489 | LOGP(DINP, LOGL_ERROR, "cannot create RSL " |
| 490 | "BSC link: %s\n", strerror(errno)); |
| 491 | return -ENOMEM; |
| 492 | } |
| 493 | if (ipa_server_link_open(rsl_link) < 0) { |
| 494 | LOGP(DINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n", |
| 495 | strerror(errno)); |
| 496 | ipa_server_link_close(rsl_link); |
| 497 | ipa_server_link_destroy(rsl_link); |
| 498 | return -EIO; |
| 499 | } |
| 500 | ret = 0; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 501 | break; |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame^] | 502 | } |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +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, *rsl_link; |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 505 | |
| 506 | LOGP(DINP, LOGL_NOTICE, "enabling ipaccess BTS mode\n"); |
| 507 | |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 508 | link = ipa_client_link_create(tall_ipa_ctx, line, |
| 509 | addr, IPA_TCP_PORT_OML, |
| 510 | ipaccess_bts_cb); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 511 | if (link == NULL) { |
Pablo Neira Ayuso | 29465d3 | 2011-06-21 14:21:33 +0200 | [diff] [blame] | 512 | LOGP(DINP, LOGL_ERROR, "cannot create OML " |
| 513 | "BTS link: %s\n", strerror(errno)); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 514 | return -ENOMEM; |
| 515 | } |
| 516 | if (ipa_client_link_open(link) < 0) { |
Pablo Neira Ayuso | 29465d3 | 2011-06-21 14:21:33 +0200 | [diff] [blame] | 517 | LOGP(DINP, LOGL_ERROR, "cannot open OML BTS link: %s\n", |
| 518 | strerror(errno)); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 519 | ipa_client_link_close(link); |
| 520 | ipa_client_link_destroy(link); |
| 521 | return -EIO; |
| 522 | } |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 523 | rsl_link = ipa_client_link_create(tall_ipa_ctx, line, |
| 524 | addr, IPA_TCP_PORT_RSL, |
| 525 | ipaccess_bts_cb); |
Pablo Neira Ayuso | 29465d3 | 2011-06-21 14:21:33 +0200 | [diff] [blame] | 526 | if (rsl_link == NULL) { |
| 527 | LOGP(DINP, LOGL_ERROR, "cannot create RSL " |
| 528 | "BTS link: %s\n", strerror(errno)); |
| 529 | return -ENOMEM; |
| 530 | } |
| 531 | if (ipa_client_link_open(rsl_link) < 0) { |
| 532 | LOGP(DINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n", |
| 533 | strerror(errno)); |
| 534 | ipa_client_link_close(rsl_link); |
| 535 | ipa_client_link_destroy(rsl_link); |
| 536 | return -EIO; |
| 537 | } |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 538 | ret = 0; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 539 | break; |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 540 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 541 | default: |
| 542 | break; |
| 543 | } |
| 544 | return ret; |
| 545 | } |
| 546 | |
| 547 | /* Actively connect to a BTS. Currently used by ipaccess-config.c */ |
| 548 | int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa) |
| 549 | { |
| 550 | struct e1inp_ts *e1i_ts = &line->ts[0]; |
| 551 | struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd; |
| 552 | int ret, on = 1; |
| 553 | |
| 554 | bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 555 | bfd->cb = ipaccess_fd_cb; |
| 556 | bfd->when = BSC_FD_READ | BSC_FD_WRITE; |
| 557 | bfd->data = line; |
| 558 | bfd->priv_nr = PRIV_OML; |
| 559 | |
| 560 | if (bfd->fd < 0) { |
| 561 | LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n"); |
| 562 | return -EIO; |
| 563 | } |
| 564 | |
| 565 | setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 566 | |
| 567 | ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa)); |
| 568 | if (ret < 0) { |
| 569 | LOGP(DINP, LOGL_ERROR, "could not connect socket\n"); |
| 570 | close(bfd->fd); |
| 571 | return ret; |
| 572 | } |
| 573 | |
| 574 | ret = osmo_fd_register(bfd); |
| 575 | if (ret < 0) { |
| 576 | close(bfd->fd); |
| 577 | return ret; |
| 578 | } |
| 579 | |
| 580 | line->driver = &ipaccess_driver; |
| 581 | |
| 582 | return ret; |
| 583 | //return e1inp_line_register(line); |
| 584 | } |
| 585 | |
| 586 | int ipaccess_setup(struct gsm_network *gsmnet) |
| 587 | { |
| 588 | e1h->gsmnet = gsmnet; |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | void e1inp_ipaccess_init(void) |
| 593 | { |
Pablo Neira Ayuso | 54b4979 | 2011-06-07 12:15:26 +0200 | [diff] [blame] | 594 | tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa"); |
| 595 | |
| 596 | e1h = talloc_zero(tall_ipa_ctx, struct ia_e1_handle); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 597 | if (!e1h) |
| 598 | return; |
| 599 | |
| 600 | e1inp_driver_register(&ipaccess_driver); |
| 601 | } |