Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 1 | /* OpenBSC Abis input driver for ip.access */ |
| 2 | |
| 3 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 4 | * (C) 2010 by Holger Hans Peter Freyther |
| 5 | * (C) 2010 by On-Waves |
| 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 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 |
| 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 |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 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/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include "internal.h" |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <unistd.h> |
| 28 | #include <stdlib.h> |
Pablo Neira Ayuso | eb43413 | 2011-07-07 19:19:46 +0200 | [diff] [blame] | 29 | #include <stdbool.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 30 | #include <errno.h> |
Daniel Willmann | 8598072 | 2014-01-09 14:30:55 +0100 | [diff] [blame^] | 31 | #include <netinet/tcp.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 32 | #include <string.h> |
| 33 | #include <time.h> |
| 34 | #include <sys/fcntl.h> |
| 35 | #include <sys/socket.h> |
| 36 | #include <sys/ioctl.h> |
| 37 | #include <arpa/inet.h> |
| 38 | |
| 39 | #include <osmocom/core/select.h> |
| 40 | #include <osmocom/gsm/tlv.h> |
| 41 | #include <osmocom/core/msgb.h> |
| 42 | #include <osmocom/core/logging.h> |
Harald Welte | 71d87b2 | 2011-07-18 14:49:56 +0200 | [diff] [blame] | 43 | #include <osmocom/core/talloc.h> |
Pablo Neira Ayuso | 177094b | 2011-06-07 12:21:51 +0200 | [diff] [blame] | 44 | #include <osmocom/abis/e1_input.h> |
| 45 | #include <osmocom/abis/ipaccess.h> |
Pablo Neira Ayuso | f67471f | 2011-06-07 11:25:49 +0200 | [diff] [blame] | 46 | #include <osmocom/core/socket.h> |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 47 | #include <osmocom/abis/ipa.h> |
Pablo Neira Ayuso | ef13269 | 2013-07-08 01:17:27 +0200 | [diff] [blame] | 48 | #include <osmocom/core/backtrace.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 49 | |
Pablo Neira Ayuso | 54b4979 | 2011-06-07 12:15:26 +0200 | [diff] [blame] | 50 | static void *tall_ipa_ctx; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 51 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 52 | #define TS1_ALLOC_SIZE 900 |
| 53 | |
Daniel Willmann | 8598072 | 2014-01-09 14:30:55 +0100 | [diff] [blame^] | 54 | #define DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT 30 |
| 55 | #define DEFAULT_TCP_KEEPALIVE_INTERVAL 3 |
| 56 | #define DEFAULT_TCP_KEEPALIVE_RETRY_COUNT 10 |
| 57 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 58 | /* |
| 59 | * Common propietary IPA messages: |
| 60 | * - PONG: in reply to PING. |
| 61 | * - ID_REQUEST: first messages once OML has been established. |
| 62 | * - ID_ACK: in reply to ID_ACK. |
| 63 | */ |
| 64 | const uint8_t ipa_pong_msg[] = { |
| 65 | 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG |
| 66 | }; |
| 67 | |
| 68 | const uint8_t ipa_id_ack_msg[] = { |
| 69 | 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK |
| 70 | }; |
| 71 | |
| 72 | const uint8_t ipa_id_req_msg[] = { |
| 73 | 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET, |
| 74 | 0x01, IPAC_IDTAG_UNIT, |
| 75 | 0x01, IPAC_IDTAG_MACADDR, |
| 76 | 0x01, IPAC_IDTAG_LOCATION1, |
| 77 | 0x01, IPAC_IDTAG_LOCATION2, |
| 78 | 0x01, IPAC_IDTAG_EQUIPVERS, |
| 79 | 0x01, IPAC_IDTAG_SWVERSION, |
| 80 | 0x01, IPAC_IDTAG_UNITNAME, |
| 81 | 0x01, IPAC_IDTAG_SERNR, |
| 82 | }; |
| 83 | |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 84 | static const char *idtag_names[] = { |
| 85 | [IPAC_IDTAG_SERNR] = "Serial_Number", |
| 86 | [IPAC_IDTAG_UNITNAME] = "Unit_Name", |
| 87 | [IPAC_IDTAG_LOCATION1] = "Location_1", |
| 88 | [IPAC_IDTAG_LOCATION2] = "Location_2", |
| 89 | [IPAC_IDTAG_EQUIPVERS] = "Equipment_Version", |
| 90 | [IPAC_IDTAG_SWVERSION] = "Software_Version", |
| 91 | [IPAC_IDTAG_IPADDR] = "IP_Address", |
| 92 | [IPAC_IDTAG_MACADDR] = "MAC_Address", |
| 93 | [IPAC_IDTAG_UNIT] = "Unit_ID", |
| 94 | }; |
| 95 | |
| 96 | const char *ipaccess_idtag_name(uint8_t tag) |
| 97 | { |
| 98 | if (tag >= ARRAY_SIZE(idtag_names)) |
| 99 | return "unknown"; |
| 100 | |
| 101 | return idtag_names[tag]; |
| 102 | } |
| 103 | |
| 104 | int ipaccess_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len) |
| 105 | { |
| 106 | uint8_t t_len; |
| 107 | uint8_t t_tag; |
| 108 | uint8_t *cur = buf; |
| 109 | |
| 110 | memset(dec, 0, sizeof(*dec)); |
| 111 | |
| 112 | while (len >= 2) { |
| 113 | len -= 2; |
| 114 | t_len = *cur++; |
| 115 | t_tag = *cur++; |
| 116 | |
| 117 | if (t_len > len + 1) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 118 | LOGP(DLMI, LOGL_ERROR, "The tag does not fit: %d\n", t_len); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 119 | return -EINVAL; |
| 120 | } |
| 121 | |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 122 | DEBUGPC(DLMI, "%s='%s' ", ipaccess_idtag_name(t_tag), cur); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 123 | |
| 124 | dec->lv[t_tag].len = t_len; |
| 125 | dec->lv[t_tag].val = cur; |
| 126 | |
| 127 | cur += t_len; |
| 128 | len -= t_len; |
| 129 | } |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | int ipaccess_parse_unitid(const char *str, struct ipaccess_unit *unit_data) |
| 134 | { |
| 135 | unsigned long ul; |
| 136 | char *endptr; |
| 137 | const char *nptr; |
| 138 | |
| 139 | nptr = str; |
| 140 | ul = strtoul(nptr, &endptr, 10); |
| 141 | if (endptr <= nptr) |
| 142 | return -EINVAL; |
Pablo Neira Ayuso | 62d345a | 2011-07-05 16:37:37 +0200 | [diff] [blame] | 143 | unit_data->site_id = ul & 0xffff; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 144 | |
| 145 | if (*endptr++ != '/') |
| 146 | return -EINVAL; |
| 147 | |
| 148 | nptr = endptr; |
| 149 | ul = strtoul(nptr, &endptr, 10); |
| 150 | if (endptr <= nptr) |
| 151 | return -EINVAL; |
Pablo Neira Ayuso | 62d345a | 2011-07-05 16:37:37 +0200 | [diff] [blame] | 152 | unit_data->bts_id = ul & 0xffff; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 153 | |
| 154 | if (*endptr++ != '/') |
| 155 | return -EINVAL; |
| 156 | |
| 157 | nptr = endptr; |
| 158 | ul = strtoul(nptr, &endptr, 10); |
| 159 | if (endptr <= nptr) |
| 160 | return -EINVAL; |
Pablo Neira Ayuso | 62d345a | 2011-07-05 16:37:37 +0200 | [diff] [blame] | 161 | unit_data->trx_id = ul & 0xffff; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 166 | static int ipaccess_send(int fd, const void *msg, size_t msglen) |
| 167 | { |
| 168 | int ret; |
| 169 | |
| 170 | ret = write(fd, msg, msglen); |
| 171 | if (ret < 0) |
| 172 | return ret; |
| 173 | if (ret < msglen) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 174 | LOGP(DLINP, LOGL_ERROR, "ipaccess_send: short write\n"); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 175 | return -EIO; |
| 176 | } |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | int ipaccess_send_pong(int fd) |
| 181 | { |
| 182 | return ipaccess_send(fd, ipa_pong_msg, sizeof(ipa_pong_msg)); |
| 183 | } |
| 184 | |
| 185 | int ipaccess_send_id_ack(int fd) |
| 186 | { |
| 187 | return ipaccess_send(fd, ipa_id_ack_msg, sizeof(ipa_id_ack_msg)); |
| 188 | } |
| 189 | |
| 190 | int ipaccess_send_id_req(int fd) |
| 191 | { |
| 192 | return ipaccess_send(fd, ipa_id_req_msg, sizeof(ipa_id_req_msg)); |
| 193 | } |
| 194 | |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 195 | /* base handling of the ip.access protocol */ |
Harald Welte | bc25bca | 2011-08-19 22:32:38 +0200 | [diff] [blame] | 196 | int ipaccess_rcvmsg_base(struct msgb *msg, struct osmo_fd *bfd) |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 197 | { |
| 198 | uint8_t msg_type = *(msg->l2h); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 199 | int ret; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 200 | |
| 201 | switch (msg_type) { |
| 202 | case IPAC_MSGT_PING: |
| 203 | ret = ipaccess_send_pong(bfd->fd); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 204 | if (ret < 0) { |
| 205 | LOGP(DLINP, LOGL_ERROR, "Cannot send PING " |
| 206 | "message. Reason: %s\n", strerror(errno)); |
| 207 | break; |
| 208 | } |
| 209 | ret = 1; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 210 | break; |
| 211 | case IPAC_MSGT_PONG: |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 212 | DEBUGP(DLMI, "PONG!\n"); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 213 | ret = 1; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 214 | break; |
| 215 | case IPAC_MSGT_ID_ACK: |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 216 | DEBUGP(DLMI, "ID_ACK? -> ACK!\n"); |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 217 | ret = ipaccess_send_id_ack(bfd->fd); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 218 | if (ret < 0) { |
| 219 | LOGP(DLINP, LOGL_ERROR, "Cannot send ID_ACK " |
| 220 | "message. Reason: %s\n", strerror(errno)); |
| 221 | break; |
| 222 | } |
| 223 | ret = 1; |
| 224 | break; |
| 225 | default: |
| 226 | /* This is not an IPA PING, PONG or ID_ACK message */ |
| 227 | ret = 0; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 228 | break; |
| 229 | } |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 230 | return ret; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 233 | /* base handling of the ip.access protocol */ |
| 234 | int ipaccess_rcvmsg_bts_base(struct msgb *msg, |
| 235 | struct osmo_fd *bfd) |
| 236 | { |
| 237 | uint8_t msg_type = *(msg->l2h); |
| 238 | int ret = 0; |
| 239 | |
| 240 | switch (msg_type) { |
| 241 | case IPAC_MSGT_PING: |
| 242 | ret = ipaccess_send_pong(bfd->fd); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 243 | if (ret < 0) { |
| 244 | LOGP(DLINP, LOGL_ERROR, "Cannot send PONG " |
| 245 | "message. Reason: %s\n", strerror(errno)); |
| 246 | } |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 247 | break; |
| 248 | case IPAC_MSGT_PONG: |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 249 | DEBUGP(DLMI, "PONG!\n"); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 250 | break; |
| 251 | case IPAC_MSGT_ID_ACK: |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 252 | DEBUGP(DLMI, "ID_ACK\n"); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 253 | break; |
| 254 | } |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 255 | return ret; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Harald Welte | 10b4130 | 2013-06-30 14:05:49 +0200 | [diff] [blame] | 258 | static int ipaccess_drop(struct osmo_fd *bfd, struct e1inp_line *line) |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 259 | { |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 260 | int ret = 1; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 261 | |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 262 | /* Error case: we did not see any ID_RESP yet for this socket. */ |
| 263 | if (bfd->fd != -1) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 264 | LOGP(DLINP, LOGL_ERROR, "Forcing socket shutdown with " |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 265 | "no signal link set\n"); |
Pablo Neira Ayuso | 9621b41 | 2011-07-07 16:19:21 +0200 | [diff] [blame] | 266 | osmo_fd_unregister(bfd); |
| 267 | close(bfd->fd); |
| 268 | bfd->fd = -1; |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 269 | ret = -ENOENT; |
Pablo Neira Ayuso | 9621b41 | 2011-07-07 16:19:21 +0200 | [diff] [blame] | 270 | } |
Pablo Neira Ayuso | 6cc3f92 | 2012-08-22 13:57:58 +0200 | [diff] [blame] | 271 | |
| 272 | /* e1inp_sign_link_destroy releases the socket descriptors for us. */ |
| 273 | line->ops->sign_link_down(line); |
| 274 | |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 275 | return ret; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 278 | static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg, |
| 279 | struct osmo_fd *bfd) |
| 280 | { |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 281 | struct tlv_parsed tlvp; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 282 | uint8_t msg_type = *(msg->l2h); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 283 | struct ipaccess_unit unit_data = {}; |
| 284 | struct e1inp_sign_link *sign_link; |
| 285 | char *unitid; |
| 286 | int len, ret; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 287 | |
Pablo Neira Ayuso | eb43413 | 2011-07-07 19:19:46 +0200 | [diff] [blame] | 288 | /* Handle IPA PING, PONG and ID_ACK messages. */ |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 289 | ret = ipaccess_rcvmsg_base(msg, bfd); |
| 290 | switch(ret) { |
| 291 | case -1: |
| 292 | /* error in IPA control message handling */ |
| 293 | goto err; |
| 294 | case 1: |
| 295 | /* this is an IPA control message, skip further processing */ |
Pablo Neira Ayuso | eb43413 | 2011-07-07 19:19:46 +0200 | [diff] [blame] | 296 | return 0; |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 297 | case 0: |
| 298 | /* this is not an IPA control message, continue */ |
| 299 | break; |
| 300 | default: |
| 301 | LOGP(DLINP, LOGL_ERROR, "Unexpected return from " |
| 302 | "ipaccess_rcvmsg_base " |
| 303 | "(ret=%d)\n", ret); |
| 304 | goto err; |
| 305 | } |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 306 | |
| 307 | switch (msg_type) { |
| 308 | case IPAC_MSGT_ID_RESP: |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 309 | DEBUGP(DLMI, "ID_RESP\n"); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 310 | /* parse tags, search for Unit ID */ |
| 311 | ret = ipaccess_idtag_parse(&tlvp, (uint8_t *)msg->l2h + 2, |
| 312 | msgb_l2len(msg)-2); |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 313 | DEBUGP(DLMI, "\n"); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 314 | if (ret < 0) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 315 | LOGP(DLINP, LOGL_ERROR, "IPA response message " |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 316 | "with malformed TLVs\n"); |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 317 | ret = -EINVAL; |
| 318 | goto err; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 319 | } |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 320 | if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 321 | LOGP(DLINP, LOGL_ERROR, "IPA response message " |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 322 | "without unit ID\n"); |
| 323 | ret = -EINVAL; |
| 324 | goto err; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 325 | |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 326 | } |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 327 | len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT); |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 328 | if (len < 1) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 329 | LOGP(DLINP, LOGL_ERROR, "IPA response message " |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 330 | "with too small unit ID\n"); |
| 331 | ret = -EINVAL; |
| 332 | goto err; |
| 333 | } |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 334 | unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT); |
| 335 | unitid[len - 1] = '\0'; |
| 336 | ipaccess_parse_unitid(unitid, &unit_data); |
| 337 | |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 338 | if (!line->ops->sign_link_up) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 339 | LOGP(DLINP, LOGL_ERROR, |
Pablo Neira Ayuso | cd8d2e5 | 2011-07-02 17:05:21 +0200 | [diff] [blame] | 340 | "Unable to set signal link, closing socket.\n"); |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 341 | ret = -EINVAL; |
| 342 | goto err; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 343 | } |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 344 | /* the BSC creates the new sign links at this stage. */ |
| 345 | if (bfd->priv_nr == E1INP_SIGN_OML) { |
| 346 | sign_link = |
| 347 | line->ops->sign_link_up(&unit_data, line, |
| 348 | E1INP_SIGN_OML); |
| 349 | if (sign_link == NULL) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 350 | LOGP(DLINP, LOGL_ERROR, |
Pablo Neira Ayuso | cd8d2e5 | 2011-07-02 17:05:21 +0200 | [diff] [blame] | 351 | "Unable to set signal link, " |
| 352 | "closing socket.\n"); |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 353 | ret = -EINVAL; |
| 354 | goto err; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 355 | } |
| 356 | } else if (bfd->priv_nr == E1INP_SIGN_RSL) { |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 357 | struct e1inp_ts *ts; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 358 | struct osmo_fd *newbfd; |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 359 | struct e1inp_line *new_line; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 360 | |
| 361 | sign_link = |
| 362 | line->ops->sign_link_up(&unit_data, line, |
| 363 | E1INP_SIGN_RSL); |
| 364 | if (sign_link == NULL) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 365 | LOGP(DLINP, LOGL_ERROR, |
Pablo Neira Ayuso | cd8d2e5 | 2011-07-02 17:05:21 +0200 | [diff] [blame] | 366 | "Unable to set signal link, " |
| 367 | "closing socket.\n"); |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 368 | ret = -EINVAL; |
| 369 | goto err; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 370 | } |
Pablo Neira Ayuso | db1c8a7 | 2011-07-08 15:12:03 +0200 | [diff] [blame] | 371 | /* this is a bugtrap, the BSC should be using the |
| 372 | * virtual E1 line used by OML for this RSL link. */ |
| 373 | if (sign_link->ts->line == line) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 374 | LOGP(DLINP, LOGL_ERROR, |
Pablo Neira Ayuso | db1c8a7 | 2011-07-08 15:12:03 +0200 | [diff] [blame] | 375 | "Fix your BSC, you should use the " |
| 376 | "E1 line used by the OML link for " |
| 377 | "your RSL link.\n"); |
| 378 | return 0; |
| 379 | } |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 380 | /* Finally, we know which OML link is associated with |
| 381 | * this RSL link, attach it to this socket. */ |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 382 | bfd->data = new_line = sign_link->ts->line; |
| 383 | e1inp_line_get(new_line); |
| 384 | ts = &new_line->ts[E1INP_SIGN_RSL+unit_data.trx_id-1]; |
| 385 | newbfd = &ts->driver.ipaccess.fd; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 386 | |
| 387 | /* get rid of our old temporary bfd */ |
| 388 | memcpy(newbfd, bfd, sizeof(*newbfd)); |
| 389 | newbfd->priv_nr = E1INP_SIGN_RSL + unit_data.trx_id; |
| 390 | osmo_fd_unregister(bfd); |
| 391 | bfd->fd = -1; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 392 | osmo_fd_register(newbfd); |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 393 | /* now we can release the dummy RSL line. */ |
| 394 | e1inp_line_put(line); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 395 | } |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 396 | break; |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 397 | default: |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 398 | LOGP(DLINP, LOGL_ERROR, "Unknown IPA message type\n"); |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 399 | ret = -EINVAL; |
| 400 | goto err; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 401 | } |
| 402 | return 0; |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 403 | err: |
| 404 | osmo_fd_unregister(bfd); |
| 405 | close(bfd->fd); |
| 406 | bfd->fd = -1; |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 407 | e1inp_line_put(line); |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 408 | return ret; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 411 | static int handle_ts1_read(struct osmo_fd *bfd) |
| 412 | { |
| 413 | struct e1inp_line *line = bfd->data; |
| 414 | unsigned int ts_nr = bfd->priv_nr; |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 415 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 416 | struct e1inp_sign_link *link; |
| 417 | struct ipaccess_head *hh; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 418 | struct msgb *msg; |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 419 | int ret; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 420 | |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 421 | ret = ipa_msg_recv(bfd->fd, &msg); |
| 422 | if (ret < 0) { |
| 423 | LOGP(DLINP, LOGL_NOTICE, "Sign link problems, " |
| 424 | "closing socket. Reason: %s\n", strerror(errno)); |
| 425 | goto err; |
| 426 | } else if (ret == 0) { |
| 427 | LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n"); |
| 428 | goto err; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 429 | } |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 430 | DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg))); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 431 | |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 432 | hh = (struct ipaccess_head *) msg->data; |
| 433 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 434 | ipaccess_rcvmsg(line, msg, bfd); |
| 435 | msgb_free(msg); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 436 | return 0; |
Holger Hans Peter Freyther | 63ddf46 | 2013-12-28 21:19:19 +0100 | [diff] [blame] | 437 | } else if (e1i_ts->type == E1INP_TS_TYPE_NONE) { |
| 438 | /* this sign link is not know yet.. complain. */ |
| 439 | LOGP(DLINP, LOGL_ERROR, "Timeslot is not configured.\n"); |
| 440 | ret = -EINVAL; |
| 441 | goto err_msg; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 442 | } |
| 443 | /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg() |
| 444 | * might have free'd it !!! */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 445 | |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 446 | link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0); |
| 447 | if (!link) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 448 | LOGP(DLINP, LOGL_ERROR, "no matching signalling link for " |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 449 | "hh->proto=0x%02x\n", hh->proto); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 450 | ret = -EINVAL; |
| 451 | goto err_msg; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 452 | } |
| 453 | msg->dst = link; |
| 454 | |
| 455 | /* XXX better use e1inp_ts_rx? */ |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 456 | if (!e1i_ts->line->ops->sign_link) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 457 | LOGP(DLINP, LOGL_ERROR, "Fix your application, " |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 458 | "no action set for signalling messages.\n"); |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 459 | ret = -EINVAL; |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 460 | goto err_msg; |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 461 | } |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 462 | if (e1i_ts->line->ops->sign_link(msg) < 0) { |
Pablo Neira Ayuso | a49c24d | 2012-10-16 11:24:08 +0200 | [diff] [blame] | 463 | /* Don't close the signalling link if the upper layers report |
| 464 | * an error, that's too strict. BTW, the signalling layer is |
| 465 | * resposible for releasing the message. |
| 466 | */ |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 467 | LOGP(DLINP, LOGL_ERROR, "Bad signalling message," |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 468 | "sign_link returned error\n"); |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 469 | } |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 470 | |
| 471 | return 0; |
| 472 | err_msg: |
| 473 | msgb_free(msg); |
Pablo Neira Ayuso | 466c546 | 2011-07-07 19:17:20 +0200 | [diff] [blame] | 474 | err: |
Harald Welte | 10b4130 | 2013-06-30 14:05:49 +0200 | [diff] [blame] | 475 | ipaccess_drop(bfd, line); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | void ipaccess_prepend_header_ext(struct msgb *msg, int proto) |
| 480 | { |
| 481 | struct ipaccess_head_ext *hh_ext; |
| 482 | |
| 483 | /* prepend the osmo ip.access header extension */ |
| 484 | hh_ext = (struct ipaccess_head_ext *) msgb_push(msg, sizeof(*hh_ext)); |
| 485 | hh_ext->proto = proto; |
| 486 | } |
| 487 | |
| 488 | void ipaccess_prepend_header(struct msgb *msg, int proto) |
| 489 | { |
| 490 | struct ipaccess_head *hh; |
| 491 | |
| 492 | /* prepend the ip.access header */ |
| 493 | hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh)); |
| 494 | hh->len = htons(msg->len - sizeof(*hh)); |
| 495 | hh->proto = proto; |
| 496 | } |
| 497 | |
| 498 | static int ts_want_write(struct e1inp_ts *e1i_ts) |
| 499 | { |
| 500 | e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE; |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
Pablo Neira Ayuso | add3ec8 | 2011-07-05 14:45:46 +0200 | [diff] [blame] | 505 | static void ipaccess_close(struct e1inp_sign_link *sign_link) |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 506 | { |
Pablo Neira Ayuso | add3ec8 | 2011-07-05 14:45:46 +0200 | [diff] [blame] | 507 | struct e1inp_ts *e1i_ts = sign_link->ts; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 508 | struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd; |
Holger Hans Peter Freyther | 55467f0 | 2011-12-28 19:47:07 +0100 | [diff] [blame] | 509 | return e1inp_close_socket(e1i_ts, sign_link, bfd); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 510 | } |
| 511 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 512 | static void timeout_ts1_write(void *data) |
| 513 | { |
| 514 | struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data; |
| 515 | |
| 516 | /* trigger write of ts1, due to tx delay timer */ |
| 517 | ts_want_write(e1i_ts); |
| 518 | } |
| 519 | |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 520 | static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 521 | { |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 522 | unsigned int ts_nr = bfd->priv_nr; |
| 523 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 524 | struct e1inp_sign_link *sign_link; |
| 525 | struct msgb *msg; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 526 | int ret; |
| 527 | |
| 528 | bfd->when &= ~BSC_FD_WRITE; |
| 529 | |
| 530 | /* get the next msg for this timeslot */ |
| 531 | msg = e1inp_tx_ts(e1i_ts, &sign_link); |
| 532 | if (!msg) { |
| 533 | /* no message after tx delay timer */ |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | switch (sign_link->type) { |
| 538 | case E1INP_SIGN_OML: |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 539 | break; |
| 540 | case E1INP_SIGN_RSL: |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 541 | break; |
| 542 | default: |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 543 | bfd->when |= BSC_FD_WRITE; /* come back for more msg */ |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 544 | ret = -EINVAL; |
| 545 | goto out; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | msg->l2h = msg->data; |
Pablo Neira Ayuso | b9ed7e3 | 2011-07-05 18:31:59 +0200 | [diff] [blame] | 549 | ipaccess_prepend_header(msg, sign_link->tei); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 550 | |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 551 | DEBUGP(DLMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg))); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 552 | |
| 553 | ret = send(bfd->fd, msg->data, msg->len, 0); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 554 | if (ret != msg->len) { |
| 555 | LOGP(DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling " |
| 556 | "message. Reason: %s\n", strerror(errno)); |
| 557 | goto err; |
| 558 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 559 | |
| 560 | /* set tx delay timer for next event */ |
| 561 | e1i_ts->sign.tx_timer.cb = timeout_ts1_write; |
| 562 | e1i_ts->sign.tx_timer.data = e1i_ts; |
| 563 | |
| 564 | /* Reducing this might break the nanoBTS 900 init. */ |
| 565 | osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay); |
| 566 | |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 567 | out: |
| 568 | msgb_free(msg); |
| 569 | return ret; |
| 570 | err: |
Harald Welte | 10b4130 | 2013-06-30 14:05:49 +0200 | [diff] [blame] | 571 | ipaccess_drop(bfd, line); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 572 | msgb_free(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 573 | return ret; |
| 574 | } |
| 575 | |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 576 | static int handle_ts1_write(struct osmo_fd *bfd) |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 577 | { |
| 578 | struct e1inp_line *line = bfd->data; |
| 579 | |
| 580 | return __handle_ts1_write(bfd, line); |
| 581 | } |
| 582 | |
Pablo Neira Ayuso | f099567 | 2011-09-08 12:58:38 +0200 | [diff] [blame] | 583 | static int ipaccess_bts_write_cb(struct ipa_client_conn *link) |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 584 | { |
| 585 | struct e1inp_line *line = link->line; |
| 586 | |
| 587 | return __handle_ts1_write(link->ofd, line); |
| 588 | } |
| 589 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 590 | /* callback from select.c in case one of the fd's can be read/written */ |
Pablo Neira Ayuso | 495ddb6 | 2011-07-08 21:04:11 +0200 | [diff] [blame] | 591 | int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 592 | { |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 593 | int rc = 0; |
| 594 | |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 595 | if (what & BSC_FD_READ) |
| 596 | rc = handle_ts1_read(bfd); |
| 597 | if (what & BSC_FD_WRITE) |
| 598 | rc = handle_ts1_write(bfd); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 599 | |
| 600 | return rc; |
| 601 | } |
| 602 | |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 603 | static int ipaccess_line_update(struct e1inp_line *line); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 604 | |
| 605 | struct e1inp_driver ipaccess_driver = { |
| 606 | .name = "ipa", |
| 607 | .want_write = ts_want_write, |
| 608 | .line_update = ipaccess_line_update, |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 609 | .close = ipaccess_close, |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 610 | .default_delay = 0, |
| 611 | }; |
| 612 | |
| 613 | /* callback of the OML listening filedescriptor */ |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 614 | static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 615 | { |
| 616 | int ret; |
| 617 | int idx = 0; |
Daniel Willmann | 8598072 | 2014-01-09 14:30:55 +0100 | [diff] [blame^] | 618 | int i, val; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 619 | struct e1inp_line *line; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 620 | struct e1inp_ts *e1i_ts; |
| 621 | struct osmo_fd *bfd; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 622 | |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 623 | /* clone virtual E1 line for this new OML link. */ |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 624 | line = e1inp_line_clone(tall_ipa_ctx, link->line); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 625 | if (line == NULL) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 626 | LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n"); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 627 | return -ENOMEM; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 628 | } |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 629 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 630 | /* create virrtual E1 timeslots for signalling */ |
Pablo Neira Ayuso | 5930185 | 2011-06-27 15:44:23 +0200 | [diff] [blame] | 631 | e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 632 | |
| 633 | /* initialize the fds */ |
| 634 | for (i = 0; i < ARRAY_SIZE(line->ts); ++i) |
| 635 | line->ts[i].driver.ipaccess.fd.fd = -1; |
| 636 | |
| 637 | e1i_ts = &line->ts[idx]; |
| 638 | |
Pablo Neira Ayuso | 93c6201 | 2011-06-26 19:12:47 +0200 | [diff] [blame] | 639 | bfd = &e1i_ts->driver.ipaccess.fd; |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 640 | bfd->fd = fd; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 641 | bfd->data = line; |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 642 | bfd->priv_nr = E1INP_SIGN_OML; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 643 | bfd->cb = ipaccess_fd_cb; |
| 644 | bfd->when = BSC_FD_READ; |
| 645 | ret = osmo_fd_register(bfd); |
| 646 | if (ret < 0) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 647 | LOGP(DLINP, LOGL_ERROR, "could not register FD\n"); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 648 | goto err_line; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 649 | } |
| 650 | |
Daniel Willmann | 8598072 | 2014-01-09 14:30:55 +0100 | [diff] [blame^] | 651 | /* Enable TCP keepalive to find out if the connection is gone */ |
| 652 | val = 1; |
| 653 | ret = setsockopt(bfd->fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)); |
| 654 | if (ret < 0) |
| 655 | LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive: %s\n", |
| 656 | strerror(errno)); |
| 657 | else |
| 658 | LOGP(DLINP, LOGL_NOTICE, "Keepalive is set: %i\n", ret); |
| 659 | |
| 660 | #if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT) |
| 661 | /* The following options are not portable! */ |
| 662 | val = DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT; |
| 663 | ret = setsockopt(bfd->fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val)); |
| 664 | if (ret < 0) |
| 665 | LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive idle time: %s\n", |
| 666 | strerror(errno)); |
| 667 | val = DEFAULT_TCP_KEEPALIVE_INTERVAL; |
| 668 | ret = setsockopt(bfd->fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)); |
| 669 | if (ret < 0) |
| 670 | LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive interval: %s\n", |
| 671 | strerror(errno)); |
| 672 | val = DEFAULT_TCP_KEEPALIVE_RETRY_COUNT; |
| 673 | ret = setsockopt(bfd->fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val)); |
| 674 | if (ret < 0) |
| 675 | LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive count: %s\n", |
| 676 | strerror(errno)); |
| 677 | #endif |
| 678 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 679 | /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */ |
| 680 | ret = ipaccess_send_id_req(bfd->fd); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 681 | if (ret < 0) { |
| 682 | LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n", |
| 683 | strerror(errno)); |
| 684 | goto err_socket; |
| 685 | } |
| 686 | return ret; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 687 | |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 688 | err_socket: |
| 689 | osmo_fd_unregister(bfd); |
| 690 | err_line: |
| 691 | close(bfd->fd); |
| 692 | bfd->fd = -1; |
| 693 | e1inp_line_put(line); |
| 694 | return ret; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 695 | } |
| 696 | |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 697 | static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 698 | { |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 699 | struct e1inp_line *line; |
| 700 | struct e1inp_ts *e1i_ts; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 701 | struct osmo_fd *bfd; |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 702 | int i, ret; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 703 | |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 704 | /* We don't know yet which OML link to associate it with. Thus, we |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 705 | * allocate a temporary E1 line until we have received ID. */ |
| 706 | line = e1inp_line_clone(tall_ipa_ctx, link->line); |
| 707 | if (line == NULL) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 708 | LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n"); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 709 | return -ENOMEM; |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 710 | } |
| 711 | /* initialize the fds */ |
| 712 | for (i = 0; i < ARRAY_SIZE(line->ts); ++i) |
| 713 | line->ts[i].driver.ipaccess.fd.fd = -1; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 714 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 715 | /* we need this to initialize this in case to avoid crashes in case |
| 716 | * that the socket is closed before we've seen an ID_RESP. */ |
| 717 | e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line); |
| 718 | |
| 719 | e1i_ts = &line->ts[E1INP_SIGN_RSL-1]; |
| 720 | |
| 721 | bfd = &e1i_ts->driver.ipaccess.fd; |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 722 | bfd->fd = fd; |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 723 | bfd->data = line; |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 724 | bfd->priv_nr = E1INP_SIGN_RSL; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 725 | bfd->cb = ipaccess_fd_cb; |
| 726 | bfd->when = BSC_FD_READ; |
| 727 | ret = osmo_fd_register(bfd); |
| 728 | if (ret < 0) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 729 | LOGP(DLINP, LOGL_ERROR, "could not register FD\n"); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 730 | goto err_line; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 731 | } |
| 732 | /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */ |
| 733 | ret = ipaccess_send_id_req(bfd->fd); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 734 | if (ret < 0) { |
| 735 | LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n", |
| 736 | strerror(errno)); |
| 737 | goto err_socket; |
| 738 | } |
| 739 | return ret; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 740 | |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 741 | err_socket: |
| 742 | osmo_fd_unregister(bfd); |
| 743 | err_line: |
| 744 | close(bfd->fd); |
| 745 | bfd->fd = -1; |
| 746 | e1inp_line_put(line); |
| 747 | return ret; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 748 | } |
| 749 | |
Pablo Neira Ayuso | 4bab3bf | 2013-07-05 15:00:13 +0200 | [diff] [blame] | 750 | #define IPA_STRING_MAX 64 |
| 751 | |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 752 | static struct msgb * |
| 753 | ipa_bts_id_resp(struct ipaccess_unit *dev, uint8_t *data, int len) |
| 754 | { |
| 755 | struct msgb *nmsg; |
Pablo Neira Ayuso | 4bab3bf | 2013-07-05 15:00:13 +0200 | [diff] [blame] | 756 | char str[IPA_STRING_MAX]; |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 757 | uint8_t *tag; |
| 758 | |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 759 | nmsg = ipa_msg_alloc(0); |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 760 | if (!nmsg) |
| 761 | return NULL; |
| 762 | |
| 763 | *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP; |
| 764 | while (len) { |
| 765 | if (len < 2) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 766 | LOGP(DLINP, LOGL_NOTICE, |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 767 | "Short read of ipaccess tag\n"); |
| 768 | msgb_free(nmsg); |
| 769 | return NULL; |
| 770 | } |
| 771 | switch (data[1]) { |
| 772 | case IPAC_IDTAG_UNIT: |
Pablo Neira Ayuso | 4bab3bf | 2013-07-05 15:00:13 +0200 | [diff] [blame] | 773 | snprintf(str, sizeof(str), "%u/%u/%u", |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 774 | dev->site_id, dev->bts_id, dev->trx_id); |
| 775 | break; |
| 776 | case IPAC_IDTAG_MACADDR: |
Pablo Neira Ayuso | 4bab3bf | 2013-07-05 15:00:13 +0200 | [diff] [blame] | 777 | snprintf(str, sizeof(str), |
| 778 | "%02x:%02x:%02x:%02x:%02x:%02x", |
| 779 | dev->mac_addr[0], dev->mac_addr[1], |
| 780 | dev->mac_addr[2], dev->mac_addr[3], |
| 781 | dev->mac_addr[4], dev->mac_addr[5]); |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 782 | break; |
| 783 | case IPAC_IDTAG_LOCATION1: |
Pablo Neira Ayuso | 4bab3bf | 2013-07-05 15:00:13 +0200 | [diff] [blame] | 784 | strncpy(str, dev->location1, IPA_STRING_MAX); |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 785 | break; |
| 786 | case IPAC_IDTAG_LOCATION2: |
Pablo Neira Ayuso | 4bab3bf | 2013-07-05 15:00:13 +0200 | [diff] [blame] | 787 | strncpy(str, dev->location2, IPA_STRING_MAX); |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 788 | break; |
| 789 | case IPAC_IDTAG_EQUIPVERS: |
Pablo Neira Ayuso | 4bab3bf | 2013-07-05 15:00:13 +0200 | [diff] [blame] | 790 | strncpy(str, dev->equipvers, IPA_STRING_MAX); |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 791 | break; |
| 792 | case IPAC_IDTAG_SWVERSION: |
Pablo Neira Ayuso | 4bab3bf | 2013-07-05 15:00:13 +0200 | [diff] [blame] | 793 | strncpy(str, dev->swversion, IPA_STRING_MAX); |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 794 | break; |
| 795 | case IPAC_IDTAG_UNITNAME: |
Pablo Neira Ayuso | 4bab3bf | 2013-07-05 15:00:13 +0200 | [diff] [blame] | 796 | snprintf(str, sizeof(str), |
| 797 | "%s-%02x-%02x-%02x-%02x-%02x-%02x", |
| 798 | dev->unit_name, |
| 799 | dev->mac_addr[0], dev->mac_addr[1], |
| 800 | dev->mac_addr[2], dev->mac_addr[3], |
| 801 | dev->mac_addr[4], dev->mac_addr[5]); |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 802 | break; |
| 803 | case IPAC_IDTAG_SERNR: |
Pablo Neira Ayuso | 4bab3bf | 2013-07-05 15:00:13 +0200 | [diff] [blame] | 804 | strncpy(str, dev->serno, IPA_STRING_MAX); |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 805 | break; |
| 806 | default: |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 807 | LOGP(DLINP, LOGL_NOTICE, |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 808 | "Unknown ipaccess tag 0x%02x\n", *data); |
| 809 | msgb_free(nmsg); |
| 810 | return NULL; |
| 811 | } |
Pablo Neira Ayuso | 4bab3bf | 2013-07-05 15:00:13 +0200 | [diff] [blame] | 812 | str[IPA_STRING_MAX-1] = '\0'; |
| 813 | |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 814 | LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str); |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 815 | tag = msgb_put(nmsg, 3 + strlen(str) + 1); |
| 816 | tag[0] = 0x00; |
| 817 | tag[1] = 1 + strlen(str) + 1; |
| 818 | tag[2] = data[1]; |
| 819 | memcpy(tag + 3, str, strlen(str) + 1); |
| 820 | data += 2; |
| 821 | len -= 2; |
| 822 | } |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 823 | ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS); |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 824 | return nmsg; |
| 825 | } |
| 826 | |
| 827 | static struct msgb *ipa_bts_id_ack(void) |
| 828 | { |
| 829 | struct msgb *nmsg2; |
| 830 | |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 831 | nmsg2 = ipa_msg_alloc(0); |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 832 | if (!nmsg2) |
| 833 | return NULL; |
| 834 | |
| 835 | *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK; |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 836 | ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS); |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 837 | |
| 838 | return nmsg2; |
| 839 | } |
| 840 | |
Harald Welte | 51de9ca | 2013-06-30 20:13:16 +0200 | [diff] [blame] | 841 | static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up) |
| 842 | { |
| 843 | struct e1inp_line *line = link->line; |
| 844 | |
| 845 | if (up) |
| 846 | return; |
| 847 | |
| 848 | if (line->ops->sign_link_down) |
| 849 | line->ops->sign_link_down(line); |
| 850 | } |
| 851 | |
Harald Welte | a3e9dd5 | 2013-06-30 14:25:07 +0200 | [diff] [blame] | 852 | static int ipaccess_bts_read_cb(struct ipa_client_conn *link, struct msgb *msg) |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 853 | { |
| 854 | struct ipaccess_head *hh = (struct ipaccess_head *) msg->data; |
| 855 | struct e1inp_ts *e1i_ts = NULL; |
| 856 | struct e1inp_sign_link *sign_link; |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 857 | struct msgb *rmsg; |
| 858 | int ret = 0; |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 859 | |
| 860 | /* special handling for IPA CCM. */ |
| 861 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 862 | uint8_t msg_type = *(msg->l2h); |
| 863 | |
| 864 | /* ping, pong and acknowledgment cases. */ |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 865 | ret = ipaccess_rcvmsg_bts_base(msg, link->ofd); |
| 866 | if (ret < 0) |
| 867 | goto err; |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 868 | |
| 869 | /* this is a request for identification from the BSC. */ |
| 870 | if (msg_type == IPAC_MSGT_ID_GET) { |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 871 | struct e1inp_sign_link *sign_link; |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 872 | uint8_t *data = msgb_l2(msg); |
| 873 | int len = msgb_l2len(msg); |
| 874 | |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 875 | LOGP(DLINP, LOGL_NOTICE, "received ID get\n"); |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 876 | if (!link->line->ops->sign_link_up) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 877 | LOGP(DLINP, LOGL_ERROR, |
Pablo Neira Ayuso | cd8d2e5 | 2011-07-02 17:05:21 +0200 | [diff] [blame] | 878 | "Unable to set signal link, " |
| 879 | "closing socket.\n"); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 880 | ret = -EINVAL; |
| 881 | goto err; |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 882 | } |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 883 | rmsg = ipa_bts_id_resp(link->line->ops->cfg.ipa.dev, |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 884 | data + 1, len - 1); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 885 | ret = ipaccess_send(link->ofd->fd, rmsg->data, |
| 886 | rmsg->len); |
| 887 | if (ret != rmsg->len) { |
| 888 | LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP " |
| 889 | "message. Reason: %s\n", strerror(errno)); |
| 890 | goto err_rmsg; |
| 891 | } |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 892 | msgb_free(rmsg); |
| 893 | |
| 894 | /* send ID_ACK. */ |
| 895 | rmsg = ipa_bts_id_ack(); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 896 | ret = ipaccess_send(link->ofd->fd, rmsg->data, |
| 897 | rmsg->len); |
| 898 | if (ret != rmsg->len) { |
| 899 | LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK " |
| 900 | "message. Reason: %s\n", strerror(errno)); |
| 901 | goto err_rmsg; |
| 902 | } |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 903 | msgb_free(rmsg); |
Harald Welte | 6eddd47 | 2013-06-30 20:18:53 +0200 | [diff] [blame] | 904 | |
| 905 | sign_link = link->line->ops->sign_link_up(msg, |
| 906 | link->line, |
| 907 | link->ofd->priv_nr); |
| 908 | if (sign_link == NULL) { |
| 909 | LOGP(DLINP, LOGL_ERROR, |
| 910 | "Unable to set signal link, " |
| 911 | "closing socket.\n"); |
| 912 | ret = -EINVAL; |
| 913 | goto err; |
| 914 | } |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 915 | } |
Pablo Neira Ayuso | 84e5cb9 | 2012-08-23 23:41:54 +0200 | [diff] [blame] | 916 | msgb_free(msg); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 917 | return ret; |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 918 | } else if (link->port == IPA_TCP_PORT_OML) |
| 919 | e1i_ts = &link->line->ts[0]; |
| 920 | else if (link->port == IPA_TCP_PORT_RSL) |
| 921 | e1i_ts = &link->line->ts[1]; |
| 922 | |
Pablo Neira Ayuso | b948701 | 2013-07-05 14:38:43 +0200 | [diff] [blame] | 923 | OSMO_ASSERT(e1i_ts != NULL); |
| 924 | |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 925 | /* look up for some existing signaling link. */ |
| 926 | sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0); |
| 927 | if (sign_link == NULL) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 928 | LOGP(DLINP, LOGL_ERROR, "no matching signalling link for " |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 929 | "hh->proto=0x%02x\n", hh->proto); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 930 | ret = -EIO; |
| 931 | goto err; |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 932 | } |
| 933 | msg->dst = sign_link; |
| 934 | |
| 935 | /* XXX better use e1inp_ts_rx? */ |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 936 | if (!link->line->ops->sign_link) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 937 | LOGP(DLINP, LOGL_ERROR, "Fix your application, " |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 938 | "no action set for signalling messages.\n"); |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 939 | ret = -ENOENT; |
| 940 | goto err; |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 941 | } |
Pablo Neira Ayuso | dbd82fb | 2011-07-05 15:29:23 +0200 | [diff] [blame] | 942 | link->line->ops->sign_link(msg); |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 943 | return 0; |
Pablo Neira Ayuso | 1e40194 | 2012-08-22 14:16:24 +0200 | [diff] [blame] | 944 | |
| 945 | err_rmsg: |
| 946 | msgb_free(rmsg); |
| 947 | err: |
| 948 | osmo_fd_unregister(link->ofd); |
| 949 | close(link->ofd->fd); |
| 950 | link->ofd->fd = -1; |
| 951 | msgb_free(msg); |
| 952 | return ret; |
Pablo Neira Ayuso | 591ddad | 2011-06-21 18:16:42 +0200 | [diff] [blame] | 953 | } |
| 954 | |
Pablo Neira Ayuso | d621640 | 2011-08-31 16:47:44 +0200 | [diff] [blame] | 955 | struct ipaccess_line { |
| 956 | int line_already_initialized; |
| 957 | }; |
| 958 | |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 959 | static int ipaccess_line_update(struct e1inp_line *line) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 960 | { |
| 961 | int ret = -ENOENT; |
Pablo Neira Ayuso | d621640 | 2011-08-31 16:47:44 +0200 | [diff] [blame] | 962 | struct ipaccess_line *il; |
| 963 | |
| 964 | if (!line->driver_data) |
| 965 | line->driver_data = talloc_zero(line, struct ipaccess_line); |
| 966 | |
| 967 | if (!line->driver_data) { |
| 968 | LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n"); |
| 969 | return -ENOMEM; |
| 970 | } |
| 971 | il = line->driver_data; |
| 972 | |
| 973 | /* We only initialize this line once. */ |
| 974 | if (il->line_already_initialized) |
| 975 | return 0; |
| 976 | |
| 977 | il->line_already_initialized = 1; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 978 | |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 979 | switch(line->ops->cfg.ipa.role) { |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 980 | case E1INP_LINE_R_BSC: { |
| 981 | struct ipa_server_link *oml_link, *rsl_link; |
| 982 | |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 983 | LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode\n"); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 984 | |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 985 | oml_link = ipa_server_link_create(tall_ipa_ctx, line, |
| 986 | "0.0.0.0", IPA_TCP_PORT_OML, |
Pablo Neira Ayuso | e009f4a | 2011-06-23 13:36:34 +0200 | [diff] [blame] | 987 | ipaccess_bsc_oml_cb, NULL); |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 988 | if (oml_link == NULL) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 989 | LOGP(DLINP, LOGL_ERROR, "cannot create OML " |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 990 | "BSC link: %s\n", strerror(errno)); |
| 991 | return -ENOMEM; |
Pablo Neira Ayuso | f67471f | 2011-06-07 11:25:49 +0200 | [diff] [blame] | 992 | } |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 993 | if (ipa_server_link_open(oml_link) < 0) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 994 | LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n", |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 995 | strerror(errno)); |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 996 | ipa_server_link_destroy(oml_link); |
| 997 | return -EIO; |
Pablo Neira Ayuso | f67471f | 2011-06-07 11:25:49 +0200 | [diff] [blame] | 998 | } |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 999 | rsl_link = ipa_server_link_create(tall_ipa_ctx, line, |
| 1000 | "0.0.0.0", IPA_TCP_PORT_RSL, |
Pablo Neira Ayuso | e009f4a | 2011-06-23 13:36:34 +0200 | [diff] [blame] | 1001 | ipaccess_bsc_rsl_cb, NULL); |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 1002 | if (rsl_link == NULL) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 1003 | LOGP(DLINP, LOGL_ERROR, "cannot create RSL " |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 1004 | "BSC link: %s\n", strerror(errno)); |
| 1005 | return -ENOMEM; |
| 1006 | } |
| 1007 | if (ipa_server_link_open(rsl_link) < 0) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 1008 | LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n", |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 1009 | strerror(errno)); |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 1010 | ipa_server_link_destroy(rsl_link); |
| 1011 | return -EIO; |
| 1012 | } |
| 1013 | ret = 0; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 1014 | break; |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 1015 | } |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 1016 | case E1INP_LINE_R_BTS: { |
Harald Welte | 84f67b2 | 2013-06-30 13:13:59 +0200 | [diff] [blame] | 1017 | struct ipa_client_conn *link; |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 1018 | |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 1019 | LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode\n"); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 1020 | |
Pablo Neira Ayuso | f099567 | 2011-09-08 12:58:38 +0200 | [diff] [blame] | 1021 | link = ipa_client_conn_create(tall_ipa_ctx, |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 1022 | &line->ts[E1INP_SIGN_OML-1], |
Pablo Neira Ayuso | 00af772 | 2011-09-08 12:47:06 +0200 | [diff] [blame] | 1023 | E1INP_SIGN_OML, |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 1024 | line->ops->cfg.ipa.addr, |
| 1025 | IPA_TCP_PORT_OML, |
Harald Welte | 51de9ca | 2013-06-30 20:13:16 +0200 | [diff] [blame] | 1026 | ipaccess_bts_updown_cb, |
Harald Welte | a3e9dd5 | 2013-06-30 14:25:07 +0200 | [diff] [blame] | 1027 | ipaccess_bts_read_cb, |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 1028 | ipaccess_bts_write_cb, |
Harald Welte | 10b4130 | 2013-06-30 14:05:49 +0200 | [diff] [blame] | 1029 | line); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 1030 | if (link == NULL) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 1031 | LOGP(DLINP, LOGL_ERROR, "cannot create OML " |
Pablo Neira Ayuso | 29465d3 | 2011-06-21 14:21:33 +0200 | [diff] [blame] | 1032 | "BTS link: %s\n", strerror(errno)); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 1033 | return -ENOMEM; |
| 1034 | } |
Pablo Neira Ayuso | f099567 | 2011-09-08 12:58:38 +0200 | [diff] [blame] | 1035 | if (ipa_client_conn_open(link) < 0) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 1036 | LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n", |
Pablo Neira Ayuso | 29465d3 | 2011-06-21 14:21:33 +0200 | [diff] [blame] | 1037 | strerror(errno)); |
Pablo Neira Ayuso | f099567 | 2011-09-08 12:58:38 +0200 | [diff] [blame] | 1038 | ipa_client_conn_close(link); |
| 1039 | ipa_client_conn_destroy(link); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 1040 | return -EIO; |
| 1041 | } |
| 1042 | ret = 0; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 1043 | break; |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 1044 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 1045 | default: |
| 1046 | break; |
| 1047 | } |
| 1048 | return ret; |
| 1049 | } |
| 1050 | |
Harald Welte | 84f67b2 | 2013-06-30 13:13:59 +0200 | [diff] [blame] | 1051 | int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line, |
| 1052 | const char *rem_addr, uint16_t rem_port) |
| 1053 | { |
| 1054 | struct ipa_client_conn *rsl_link; |
| 1055 | |
| 1056 | rsl_link = ipa_client_conn_create(tall_ipa_ctx, |
| 1057 | &line->ts[E1INP_SIGN_RSL-1], |
| 1058 | E1INP_SIGN_RSL, |
| 1059 | rem_addr, rem_port, |
Harald Welte | 51de9ca | 2013-06-30 20:13:16 +0200 | [diff] [blame] | 1060 | ipaccess_bts_updown_cb, |
Harald Welte | a3e9dd5 | 2013-06-30 14:25:07 +0200 | [diff] [blame] | 1061 | ipaccess_bts_read_cb, |
Harald Welte | 84f67b2 | 2013-06-30 13:13:59 +0200 | [diff] [blame] | 1062 | ipaccess_bts_write_cb, |
Harald Welte | 10b4130 | 2013-06-30 14:05:49 +0200 | [diff] [blame] | 1063 | line); |
Harald Welte | 84f67b2 | 2013-06-30 13:13:59 +0200 | [diff] [blame] | 1064 | if (rsl_link == NULL) { |
| 1065 | LOGP(DLINP, LOGL_ERROR, "cannot create RSL " |
| 1066 | "BTS link: %s\n", strerror(errno)); |
| 1067 | return -ENOMEM; |
| 1068 | } |
| 1069 | if (ipa_client_conn_open(rsl_link) < 0) { |
| 1070 | LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n", |
| 1071 | strerror(errno)); |
| 1072 | ipa_client_conn_close(rsl_link); |
| 1073 | ipa_client_conn_destroy(rsl_link); |
| 1074 | return -EIO; |
| 1075 | } |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 1079 | void e1inp_ipaccess_init(void) |
| 1080 | { |
Pablo Neira Ayuso | 54b4979 | 2011-06-07 12:15:26 +0200 | [diff] [blame] | 1081 | tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa"); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 1082 | e1inp_driver_register(&ipaccess_driver); |
| 1083 | } |