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