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