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