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