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