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