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