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