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 | bd85b94 | 2010-01-13 09:06:46 +0100 | [diff] [blame^] | 167 | /* send the id ack */ |
| 168 | int ipaccess_send_id_ack(int fd) |
| 169 | { |
| 170 | return write(fd, id_ack, sizeof(id_ack)); |
| 171 | } |
| 172 | |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 173 | /* base handling of the ip.access protocol */ |
| 174 | int ipaccess_rcvmsg_base(struct msgb *msg, |
| 175 | struct bsc_fd *bfd) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 176 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 177 | u_int8_t msg_type = *(msg->l2h); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 178 | int ret = 0; |
| 179 | |
| 180 | switch (msg_type) { |
| 181 | case IPAC_MSGT_PING: |
| 182 | ret = write(bfd->fd, pong, sizeof(pong)); |
| 183 | break; |
| 184 | case IPAC_MSGT_PONG: |
| 185 | DEBUGP(DMI, "PONG!\n"); |
| 186 | break; |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 187 | case IPAC_MSGT_ID_ACK: |
| 188 | DEBUGP(DMI, "ID_ACK? -> ACK!\n"); |
Holger Hans Peter Freyther | bd85b94 | 2010-01-13 09:06:46 +0100 | [diff] [blame^] | 189 | ret = ipaccess_send_id_ack(bfd->fd); |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 190 | break; |
| 191 | } |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg, |
| 196 | struct bsc_fd *bfd) |
| 197 | { |
| 198 | struct tlv_parsed tlvp; |
| 199 | u_int8_t msg_type = *(msg->l2h); |
| 200 | u_int16_t site_id = 0, bts_id = 0, trx_id = 0; |
| 201 | struct gsm_bts *bts; |
| 202 | |
| 203 | /* handle base messages */ |
| 204 | ipaccess_rcvmsg_base(msg, bfd); |
| 205 | |
| 206 | switch (msg_type) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 207 | case IPAC_MSGT_ID_RESP: |
| 208 | DEBUGP(DMI, "ID_RESP "); |
| 209 | /* parse tags, search for Unit ID */ |
| 210 | ipac_idtag_parse(&tlvp, (u_int8_t *)msg->l2h + 2, |
| 211 | msgb_l2len(msg)-2); |
| 212 | DEBUGP(DMI, "\n"); |
| 213 | |
| 214 | if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) |
| 215 | break; |
| 216 | |
| 217 | /* lookup BTS, create sign_link, ... */ |
| 218 | parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT), |
| 219 | &site_id, &bts_id, &trx_id); |
| 220 | bts = find_bts_by_unitid(e1h->gsmnet, site_id, bts_id); |
| 221 | if (!bts) { |
Harald Welte | 5a13a45 | 2009-12-23 23:01:04 +0100 | [diff] [blame] | 222 | LOGP(DINP, LOGL_ERROR, "Unable to find BTS configuration for " |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 223 | " %u/%u/%u, disconnecting\n", site_id, bts_id, |
| 224 | trx_id); |
| 225 | return -EIO; |
| 226 | } |
| 227 | DEBUGP(DINP, "Identified BTS %u/%u/%u\n", site_id, bts_id, trx_id); |
| 228 | if (bfd->priv_nr == 1) { |
| 229 | bts->oml_link = e1inp_sign_link_create(&line->ts[1-1], |
| 230 | E1INP_SIGN_OML, bts->c0, |
Harald Welte | 2557287 | 2009-10-20 00:22:00 +0200 | [diff] [blame] | 231 | bts->oml_tei, 0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 232 | } else if (bfd->priv_nr == 2) { |
| 233 | struct e1inp_ts *e1i_ts; |
| 234 | struct bsc_fd *newbfd; |
Harald Welte | 2557287 | 2009-10-20 00:22:00 +0200 | [diff] [blame] | 235 | struct gsm_bts_trx *trx = gsm_bts_trx_num(bts, trx_id); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 236 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 237 | bfd->data = line = bts->oml_link->ts->line; |
Harald Welte | 2557287 | 2009-10-20 00:22:00 +0200 | [diff] [blame] | 238 | e1i_ts = &line->ts[2+trx_id - 1]; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 239 | newbfd = &e1i_ts->driver.ipaccess.fd; |
Harald Welte | 2557287 | 2009-10-20 00:22:00 +0200 | [diff] [blame] | 240 | e1inp_ts_config(e1i_ts, line, E1INP_TS_TYPE_SIGN); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 241 | |
Harald Welte | 2557287 | 2009-10-20 00:22:00 +0200 | [diff] [blame] | 242 | trx->rsl_link = e1inp_sign_link_create(e1i_ts, |
| 243 | E1INP_SIGN_RSL, trx, |
| 244 | trx->rsl_tei, 0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 245 | /* get rid of our old temporary bfd */ |
| 246 | memcpy(newbfd, bfd, sizeof(*newbfd)); |
Harald Welte | ca648f9 | 2009-12-21 23:01:33 +0100 | [diff] [blame] | 247 | newbfd->priv_nr = 2+trx_id; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 248 | bsc_unregister_fd(bfd); |
| 249 | bsc_register_fd(newbfd); |
Harald Welte | 49a8405 | 2009-06-22 01:36:25 +0200 | [diff] [blame] | 250 | talloc_free(bfd); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 251 | } |
| 252 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 253 | } |
| 254 | return 0; |
| 255 | } |
| 256 | |
Harald Welte | 5a989a9 | 2009-12-16 17:32:37 +0100 | [diff] [blame] | 257 | #define OML_UP 0x0001 |
| 258 | #define RSL_UP 0x0002 |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 259 | |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 260 | /* |
| 261 | * read one ipa message from the socket |
| 262 | * return NULL in case of error |
| 263 | */ |
| 264 | struct msgb *ipaccess_read_msg(struct bsc_fd *bfd, int *error) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 265 | { |
Harald Welte | 9cfc935 | 2009-06-26 19:39:35 +0200 | [diff] [blame] | 266 | struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "Abis/IP"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 267 | struct ipaccess_head *hh; |
Sylvain Munaut | 9da8a3a | 2009-10-29 16:33:59 +0100 | [diff] [blame] | 268 | int len, ret = 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 269 | |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 270 | if (!msg) { |
| 271 | *error = -ENOMEM; |
| 272 | return NULL; |
| 273 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 274 | |
| 275 | /* first read our 3-byte header */ |
| 276 | hh = (struct ipaccess_head *) msg->data; |
| 277 | ret = recv(bfd->fd, msg->data, 3, 0); |
| 278 | if (ret < 0) { |
Harald Welte | a2be686 | 2009-12-24 13:35:18 +0100 | [diff] [blame] | 279 | if (errno != EAGAIN) |
| 280 | LOGP(DINP, LOGL_ERROR, "recv error %d %s\n", ret, strerror(errno)); |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 281 | msgb_free(msg); |
| 282 | *error = ret; |
| 283 | return NULL; |
| 284 | } else if (ret == 0) { |
| 285 | msgb_free(msg); |
| 286 | *error = ret; |
| 287 | return NULL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 288 | } |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 289 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 290 | msgb_put(msg, ret); |
| 291 | |
| 292 | /* then read te length as specified in header */ |
| 293 | msg->l2h = msg->data + sizeof(*hh); |
Sylvain Munaut | 9da8a3a | 2009-10-29 16:33:59 +0100 | [diff] [blame] | 294 | len = ntohs(hh->len); |
| 295 | ret = recv(bfd->fd, msg->l2h, len, 0); |
| 296 | if (ret < len) { |
Harald Welte | 5a13a45 | 2009-12-23 23:01:04 +0100 | [diff] [blame] | 297 | LOGP(DINP, LOGL_ERROR, "short read!\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 298 | msgb_free(msg); |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 299 | *error = -EIO; |
| 300 | return NULL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 301 | } |
| 302 | msgb_put(msg, ret); |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 303 | |
| 304 | return msg; |
| 305 | } |
| 306 | |
| 307 | static int handle_ts1_read(struct bsc_fd *bfd) |
| 308 | { |
| 309 | struct e1inp_line *line = bfd->data; |
| 310 | unsigned int ts_nr = bfd->priv_nr; |
| 311 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 312 | struct e1inp_sign_link *link; |
| 313 | struct msgb *msg; |
| 314 | struct ipaccess_head *hh; |
Holger Hans Peter Freyther | b84a04a | 2009-10-27 10:08:38 +0100 | [diff] [blame] | 315 | int ret = 0, error; |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 316 | |
| 317 | msg = ipaccess_read_msg(bfd, &error); |
| 318 | if (!msg) { |
| 319 | if (error == 0) { |
Harald Welte (local) | 20bf3c6 | 2009-12-28 17:52:11 +0100 | [diff] [blame] | 320 | link = e1inp_lookup_sign_link(e1i_ts, IPAC_PROTO_OML, 0); |
| 321 | if (link) { |
| 322 | link->trx->bts->ip_access.flags = 0; |
| 323 | LOGP(DINP, LOGL_NOTICE, "BTS %u disappeared, dead socket\n", |
| 324 | link->trx->bts->nr); |
| 325 | } else |
| 326 | LOGP(DINP, LOGL_NOTICE, "unknown BTS disappeared, dead socket\n"); |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 327 | e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_RSL); |
| 328 | e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_OML); |
| 329 | bsc_unregister_fd(bfd); |
| 330 | close(bfd->fd); |
| 331 | bfd->fd = -1; |
| 332 | } |
| 333 | return error; |
| 334 | } |
| 335 | |
Sylvain Munaut | 71137ac | 2009-09-30 22:35:04 +0200 | [diff] [blame] | 336 | 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] | 337 | |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 338 | hh = (struct ipaccess_head *) msg->data; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 339 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 340 | ret = ipaccess_rcvmsg(line, msg, bfd); |
| 341 | if (ret < 0) { |
| 342 | e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_RSL); |
| 343 | e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_OML); |
| 344 | bsc_unregister_fd(bfd); |
| 345 | close(bfd->fd); |
| 346 | bfd->fd = -1; |
| 347 | } |
| 348 | msgb_free(msg); |
| 349 | return ret; |
| 350 | } |
| 351 | /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg() |
| 352 | * might have free'd it !!! */ |
| 353 | |
Harald Welte | 2557287 | 2009-10-20 00:22:00 +0200 | [diff] [blame] | 354 | link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 355 | if (!link) { |
Harald Welte | 5a13a45 | 2009-12-23 23:01:04 +0100 | [diff] [blame] | 356 | LOGP(DINP, LOGL_ERROR, "no matching signalling link for " |
| 357 | "hh->proto=0x%02x\n", hh->proto); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 358 | msgb_free(msg); |
| 359 | return -EIO; |
| 360 | } |
| 361 | msg->trx = link->trx; |
| 362 | |
Harald Welte | 2557287 | 2009-10-20 00:22:00 +0200 | [diff] [blame] | 363 | switch (link->type) { |
| 364 | case E1INP_SIGN_RSL: |
Harald Welte | ca648f9 | 2009-12-21 23:01:33 +0100 | [diff] [blame] | 365 | if (!(msg->trx->bts->ip_access.flags & (RSL_UP << msg->trx->nr))) { |
Harald Welte | 240ce76 | 2009-10-21 21:16:00 +0200 | [diff] [blame] | 366 | e1inp_event(e1i_ts, EVT_E1_TEI_UP, link->tei, link->sapi); |
Harald Welte | ca648f9 | 2009-12-21 23:01:33 +0100 | [diff] [blame] | 367 | msg->trx->bts->ip_access.flags |= (RSL_UP << msg->trx->nr); |
Harald Welte | 240ce76 | 2009-10-21 21:16:00 +0200 | [diff] [blame] | 368 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 369 | ret = abis_rsl_rcvmsg(msg); |
| 370 | break; |
Harald Welte | 2557287 | 2009-10-20 00:22:00 +0200 | [diff] [blame] | 371 | case E1INP_SIGN_OML: |
Harald Welte | 5a989a9 | 2009-12-16 17:32:37 +0100 | [diff] [blame] | 372 | if (!(msg->trx->bts->ip_access.flags & OML_UP)) { |
Harald Welte | 2557287 | 2009-10-20 00:22:00 +0200 | [diff] [blame] | 373 | e1inp_event(e1i_ts, EVT_E1_TEI_UP, link->tei, link->sapi); |
Harald Welte | 5a989a9 | 2009-12-16 17:32:37 +0100 | [diff] [blame] | 374 | msg->trx->bts->ip_access.flags |= OML_UP; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 375 | } |
| 376 | ret = abis_nm_rcvmsg(msg); |
| 377 | break; |
| 378 | default: |
Harald Welte | 5a13a45 | 2009-12-23 23:01:04 +0100 | [diff] [blame] | 379 | 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] | 380 | msgb_free(msg); |
| 381 | break; |
| 382 | } |
| 383 | return ret; |
| 384 | } |
| 385 | |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 386 | void ipaccess_prepend_header(struct msgb *msg, int proto) |
| 387 | { |
| 388 | struct ipaccess_head *hh; |
| 389 | |
| 390 | /* prepend the ip.access header */ |
| 391 | hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh)); |
Sylvain Munaut | 9da8a3a | 2009-10-29 16:33:59 +0100 | [diff] [blame] | 392 | hh->len = htons(msg->len - sizeof(*hh)); |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 393 | hh->proto = proto; |
| 394 | } |
| 395 | |
Andreas Eversberg | dd36b0b | 2009-10-08 12:53:16 +0200 | [diff] [blame] | 396 | static int ts_want_write(struct e1inp_ts *e1i_ts) |
| 397 | { |
| 398 | e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE; |
| 399 | |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | static void timeout_ts1_write(void *data) |
| 404 | { |
| 405 | struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data; |
| 406 | |
| 407 | /* trigger write of ts1, due to tx delay timer */ |
| 408 | ts_want_write(e1i_ts); |
| 409 | } |
| 410 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 411 | static int handle_ts1_write(struct bsc_fd *bfd) |
| 412 | { |
| 413 | struct e1inp_line *line = bfd->data; |
| 414 | unsigned int ts_nr = bfd->priv_nr; |
| 415 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 416 | struct e1inp_sign_link *sign_link; |
| 417 | struct msgb *msg; |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 418 | u_int8_t proto; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 419 | int ret; |
| 420 | |
Andreas Eversberg | dd36b0b | 2009-10-08 12:53:16 +0200 | [diff] [blame] | 421 | bfd->when &= ~BSC_FD_WRITE; |
| 422 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 423 | /* get the next msg for this timeslot */ |
| 424 | msg = e1inp_tx_ts(e1i_ts, &sign_link); |
| 425 | if (!msg) { |
Andreas Eversberg | dd36b0b | 2009-10-08 12:53:16 +0200 | [diff] [blame] | 426 | /* no message after tx delay timer */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 427 | return 0; |
| 428 | } |
| 429 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 430 | switch (sign_link->type) { |
| 431 | case E1INP_SIGN_OML: |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 432 | proto = IPAC_PROTO_OML; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 433 | break; |
| 434 | case E1INP_SIGN_RSL: |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 435 | proto = IPAC_PROTO_RSL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 436 | break; |
| 437 | default: |
| 438 | msgb_free(msg); |
Andreas Eversberg | dd36b0b | 2009-10-08 12:53:16 +0200 | [diff] [blame] | 439 | bfd->when |= BSC_FD_WRITE; /* come back for more msg */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 440 | return -EINVAL; |
| 441 | } |
| 442 | |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 443 | msg->l2h = msg->data; |
Harald Welte | 2557287 | 2009-10-20 00:22:00 +0200 | [diff] [blame] | 444 | ipaccess_prepend_header(msg, sign_link->tei); |
Holger Hans Peter Freyther | 7d3573e | 2009-08-18 10:05:45 +0200 | [diff] [blame] | 445 | |
| 446 | 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] | 447 | |
| 448 | ret = send(bfd->fd, msg->data, msg->len, 0); |
| 449 | msgb_free(msg); |
Andreas Eversberg | dd36b0b | 2009-10-08 12:53:16 +0200 | [diff] [blame] | 450 | |
| 451 | /* set tx delay timer for next event */ |
| 452 | e1i_ts->sign.tx_timer.cb = timeout_ts1_write; |
| 453 | e1i_ts->sign.tx_timer.data = e1i_ts; |
| 454 | bsc_schedule_timer(&e1i_ts->sign.tx_timer, 0, 100000); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 455 | |
| 456 | return ret; |
| 457 | } |
| 458 | |
| 459 | /* callback from select.c in case one of the fd's can be read/written */ |
| 460 | static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what) |
| 461 | { |
| 462 | struct e1inp_line *line = bfd->data; |
| 463 | unsigned int ts_nr = bfd->priv_nr; |
| 464 | unsigned int idx = ts_nr-1; |
| 465 | struct e1inp_ts *e1i_ts; |
| 466 | int rc = 0; |
| 467 | |
| 468 | /* In case of early RSL we might not yet have a line */ |
| 469 | |
| 470 | if (line) |
| 471 | e1i_ts = &line->ts[idx]; |
| 472 | |
| 473 | if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) { |
| 474 | if (what & BSC_FD_READ) |
| 475 | rc = handle_ts1_read(bfd); |
| 476 | if (what & BSC_FD_WRITE) |
| 477 | rc = handle_ts1_write(bfd); |
| 478 | } else |
Harald Welte | 5a13a45 | 2009-12-23 23:01:04 +0100 | [diff] [blame] | 479 | 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] | 480 | |
| 481 | return rc; |
| 482 | } |
| 483 | |
| 484 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 485 | struct e1inp_driver ipaccess_driver = { |
| 486 | .name = "ip.access", |
| 487 | .want_write = ts_want_write, |
| 488 | }; |
| 489 | |
| 490 | /* callback of the OML listening filedescriptor */ |
| 491 | static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what) |
| 492 | { |
| 493 | int ret; |
| 494 | int idx = 0; |
| 495 | struct e1inp_line *line; |
| 496 | struct e1inp_ts *e1i_ts; |
| 497 | struct bsc_fd *bfd; |
| 498 | struct sockaddr_in sa; |
| 499 | socklen_t sa_len = sizeof(sa); |
| 500 | |
| 501 | if (!(what & BSC_FD_READ)) |
| 502 | return 0; |
| 503 | |
| 504 | ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 505 | if (ret < 0) { |
| 506 | perror("accept"); |
| 507 | return ret; |
| 508 | } |
Harald Welte | 5a13a45 | 2009-12-23 23:01:04 +0100 | [diff] [blame] | 509 | LOGP(DINP, LOGL_NOTICE, "accept()ed new OML link from %s\n", |
| 510 | inet_ntoa(sa.sin_addr)); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 511 | |
Holger Hans Peter Freyther | e453348 | 2009-10-29 02:29:45 +0100 | [diff] [blame] | 512 | line = talloc_zero(tall_bsc_ctx, struct e1inp_line); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 513 | if (!line) { |
| 514 | close(ret); |
| 515 | return -ENOMEM; |
| 516 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 517 | line->driver = &ipaccess_driver; |
| 518 | //line->driver_data = e1h; |
| 519 | /* create virrtual E1 timeslots for signalling */ |
| 520 | e1inp_ts_config(&line->ts[1-1], line, E1INP_TS_TYPE_SIGN); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 521 | |
| 522 | e1i_ts = &line->ts[idx]; |
| 523 | |
| 524 | bfd = &e1i_ts->driver.ipaccess.fd; |
| 525 | bfd->fd = ret; |
| 526 | bfd->data = line; |
| 527 | bfd->priv_nr = 1; |
| 528 | bfd->cb = ipaccess_fd_cb; |
| 529 | bfd->when = BSC_FD_READ; |
| 530 | ret = bsc_register_fd(bfd); |
| 531 | if (ret < 0) { |
Harald Welte | 5a13a45 | 2009-12-23 23:01:04 +0100 | [diff] [blame] | 532 | LOGP(DINP, LOGL_ERROR, "could not register FD\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 533 | close(bfd->fd); |
Harald Welte | 49a8405 | 2009-06-22 01:36:25 +0200 | [diff] [blame] | 534 | talloc_free(line); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 535 | return ret; |
| 536 | } |
| 537 | |
| 538 | /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */ |
| 539 | ret = write(bfd->fd, id_req, sizeof(id_req)); |
| 540 | |
Holger Hans Peter Freyther | 182c745 | 2009-08-10 07:59:27 +0200 | [diff] [blame] | 541 | return ret; |
Harald Welte | 6286888 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 542 | //return e1inp_line_register(line); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | static int rsl_listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what) |
| 546 | { |
| 547 | struct sockaddr_in sa; |
| 548 | socklen_t sa_len = sizeof(sa); |
Harald Welte | 49a8405 | 2009-06-22 01:36:25 +0200 | [diff] [blame] | 549 | struct bsc_fd *bfd; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 550 | int ret; |
| 551 | |
| 552 | if (!(what & BSC_FD_READ)) |
| 553 | return 0; |
| 554 | |
Holger Hans Peter Freyther | e453348 | 2009-10-29 02:29:45 +0100 | [diff] [blame] | 555 | bfd = talloc_zero(tall_bsc_ctx, struct bsc_fd); |
Harald Welte | 49a8405 | 2009-06-22 01:36:25 +0200 | [diff] [blame] | 556 | if (!bfd) |
| 557 | return -ENOMEM; |
Harald Welte | 49a8405 | 2009-06-22 01:36:25 +0200 | [diff] [blame] | 558 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 559 | /* Some BTS has connected to us, but we don't know yet which line |
| 560 | * (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] | 561 | * allocate a temporary bfd until we have received ID from BTS */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 562 | |
| 563 | bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 564 | if (bfd->fd < 0) { |
| 565 | perror("accept"); |
| 566 | return bfd->fd; |
| 567 | } |
Harald Welte | 5a13a45 | 2009-12-23 23:01:04 +0100 | [diff] [blame] | 568 | 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] | 569 | bfd->priv_nr = 2; |
| 570 | bfd->cb = ipaccess_fd_cb; |
| 571 | bfd->when = BSC_FD_READ; |
| 572 | ret = bsc_register_fd(bfd); |
| 573 | if (ret < 0) { |
Harald Welte | 5a13a45 | 2009-12-23 23:01:04 +0100 | [diff] [blame] | 574 | LOGP(DINP, LOGL_ERROR, "could not register FD\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 575 | close(bfd->fd); |
Harald Welte | 49a8405 | 2009-06-22 01:36:25 +0200 | [diff] [blame] | 576 | talloc_free(bfd); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 577 | return ret; |
| 578 | } |
| 579 | /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */ |
| 580 | ret = write(bfd->fd, id_req, sizeof(id_req)); |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | static int make_sock(struct bsc_fd *bfd, u_int16_t port, |
| 586 | int (*cb)(struct bsc_fd *fd, unsigned int what)) |
| 587 | { |
| 588 | struct sockaddr_in addr; |
| 589 | int ret, on = 1; |
| 590 | |
| 591 | bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 592 | bfd->cb = cb; |
| 593 | bfd->when = BSC_FD_READ; |
| 594 | //bfd->data = line; |
| 595 | |
| 596 | memset(&addr, 0, sizeof(addr)); |
| 597 | addr.sin_family = AF_INET; |
| 598 | addr.sin_port = htons(port); |
| 599 | addr.sin_addr.s_addr = INADDR_ANY; |
| 600 | |
| 601 | setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 602 | |
| 603 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 604 | if (ret < 0) { |
Harald Welte | 5a13a45 | 2009-12-23 23:01:04 +0100 | [diff] [blame] | 605 | LOGP(DINP, LOGL_ERROR, "could not bind l2 socket %s\n", |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 606 | strerror(errno)); |
| 607 | return -EIO; |
| 608 | } |
| 609 | |
| 610 | ret = listen(bfd->fd, 1); |
| 611 | if (ret < 0) { |
| 612 | perror("listen"); |
| 613 | return ret; |
| 614 | } |
| 615 | |
| 616 | ret = bsc_register_fd(bfd); |
| 617 | if (ret < 0) { |
| 618 | perror("register_listen_fd"); |
| 619 | return ret; |
| 620 | } |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | /* Actively connect to a BTS. Currently used by ipaccess-config.c */ |
| 625 | int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa) |
| 626 | { |
| 627 | struct e1inp_ts *e1i_ts = &line->ts[0]; |
| 628 | struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd; |
| 629 | int ret, on = 1; |
| 630 | |
| 631 | bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 632 | bfd->cb = ipaccess_fd_cb; |
| 633 | bfd->when = BSC_FD_READ | BSC_FD_WRITE; |
| 634 | bfd->data = line; |
| 635 | bfd->priv_nr = 1; |
| 636 | |
| 637 | setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 638 | |
| 639 | ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa)); |
| 640 | if (ret < 0) { |
Harald Welte | 5a13a45 | 2009-12-23 23:01:04 +0100 | [diff] [blame] | 641 | LOGP(DINP, LOGL_ERROR, "could not connect socket\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 642 | close(bfd->fd); |
| 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | ret = bsc_register_fd(bfd); |
| 647 | if (ret < 0) { |
| 648 | close(bfd->fd); |
| 649 | return ret; |
| 650 | } |
| 651 | |
| 652 | line->driver = &ipaccess_driver; |
| 653 | |
Holger Hans Peter Freyther | 182c745 | 2009-08-10 07:59:27 +0200 | [diff] [blame] | 654 | return ret; |
Harald Welte | 6286888 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 655 | //return e1inp_line_register(line); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | int ipaccess_setup(struct gsm_network *gsmnet) |
| 659 | { |
| 660 | int ret; |
| 661 | |
| 662 | /* register the driver with the core */ |
| 663 | /* FIXME: do this in the plugin initializer function */ |
| 664 | ret = e1inp_driver_register(&ipaccess_driver); |
| 665 | if (ret) |
| 666 | return ret; |
| 667 | |
Holger Hans Peter Freyther | e453348 | 2009-10-29 02:29:45 +0100 | [diff] [blame] | 668 | e1h = talloc_zero(tall_bsc_ctx, struct ia_e1_handle); |
Harald Welte | 49a8405 | 2009-06-22 01:36:25 +0200 | [diff] [blame] | 669 | if (!e1h) |
| 670 | return -ENOMEM; |
Harald Welte | 49a8405 | 2009-06-22 01:36:25 +0200 | [diff] [blame] | 671 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 672 | e1h->gsmnet = gsmnet; |
| 673 | |
| 674 | /* Listen for OML connections */ |
Harald Welte | 9994b18 | 2009-12-23 22:47:53 +0100 | [diff] [blame] | 675 | ret = make_sock(&e1h->listen_fd, IPA_TCP_PORT_OML, listen_fd_cb); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 676 | if (ret < 0) |
| 677 | return ret; |
| 678 | |
| 679 | /* Listen for RSL connections */ |
Harald Welte | 9994b18 | 2009-12-23 22:47:53 +0100 | [diff] [blame] | 680 | ret = make_sock(&e1h->rsl_listen_fd, IPA_TCP_PORT_RSL, rsl_listen_fd_cb); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 681 | |
| 682 | return ret; |
| 683 | } |