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