blob: 216652491e36bfd3cb6c655d6db33e7c3e7be364 [file] [log] [blame]
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001/* 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 Welte323d39d2017-11-13 01:09:21 +09009 * SPDX-License-Identifier: AGPL-3.0+
10 *
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020011 * 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 Ayusoeb434132011-07-07 19:19:46 +020031#include <stdbool.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020032#include <errno.h>
Daniel Willmann85980722014-01-09 14:30:55 +010033#include <netinet/tcp.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020034#include <string.h>
35#include <time.h>
Holger Hans Peter Freyther25474582017-01-23 19:49:07 +010036#include <fcntl.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020037#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 Weltef5efba42014-08-18 17:30:36 +020044#include <osmocom/core/macaddr.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020045#include <osmocom/core/logging.h>
Harald Welte71d87b22011-07-18 14:49:56 +020046#include <osmocom/core/talloc.h>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020047#include <osmocom/abis/e1_input.h>
48#include <osmocom/abis/ipaccess.h>
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +020049#include <osmocom/core/socket.h>
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020050#include <osmocom/abis/ipa.h>
Pablo Neira Ayusoef132692013-07-08 01:17:27 +020051#include <osmocom/core/backtrace.h>
Harald Welteb65f58f2014-08-20 22:04:11 +020052#include <osmocom/gsm/ipa.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020053
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +020054static void *tall_ipa_ctx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020055
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020056#define TS1_ALLOC_SIZE 900
57
Daniel Willmann85980722014-01-09 14:30:55 +010058#define DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT 30
59#define DEFAULT_TCP_KEEPALIVE_INTERVAL 3
60#define DEFAULT_TCP_KEEPALIVE_RETRY_COUNT 10
61
Harald Welte10b41302013-06-30 14:05:49 +020062static int ipaccess_drop(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020063{
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +020064 int ret = 1;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +020065 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 Ayusoc9c4fd32011-06-30 12:19:42 +020070
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020071 /* Error case: we did not see any ID_RESP yet for this socket. */
72 if (bfd->fd != -1) {
Harald Weltecc2241b2011-07-19 16:06:06 +020073 LOGP(DLINP, LOGL_ERROR, "Forcing socket shutdown with "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020074 "no signal link set\n");
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +020075 osmo_fd_unregister(bfd);
76 close(bfd->fd);
77 bfd->fd = -1;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020078 ret = -ENOENT;
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +020079 }
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +020080
Jacob Erlbeck98af3c32014-03-31 10:53:32 +020081 msgb_free(e1i_ts->pending_msg);
82 e1i_ts->pending_msg = NULL;
83
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +020084 /* e1inp_sign_link_destroy releases the socket descriptors for us. */
85 line->ops->sign_link_down(line);
86
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020087 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020088}
89
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +020090/* 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 Ayuso5a4b7c52011-06-07 14:07:48 +020092static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
93 struct osmo_fd *bfd)
94{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020095 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +020096 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020097 struct ipaccess_unit unit_data = {};
98 struct e1inp_sign_link *sign_link;
99 char *unitid;
100 int len, ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200101
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200102 /* Handle IPA PING, PONG and ID_ACK messages. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200103 ret = ipa_ccm_rcvmsg_base(msg, bfd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200104 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 Ayusoeb434132011-07-07 19:19:46 +0200110 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200111 case 0:
112 /* this is not an IPA control message, continue */
113 break;
114 default:
115 LOGP(DLINP, LOGL_ERROR, "Unexpected return from "
Harald Welteb65f58f2014-08-20 22:04:11 +0200116 "ipa_ccm_rcvmsg_base "
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200117 "(ret=%d)\n", ret);
118 goto err;
119 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200120
121 switch (msg_type) {
122 case IPAC_MSGT_ID_RESP:
Harald Weltecc2241b2011-07-19 16:06:06 +0200123 DEBUGP(DLMI, "ID_RESP\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200124 /* parse tags, search for Unit ID */
Harald Welte82eb55e2018-08-01 13:22:55 +0200125 ret = ipa_ccm_id_resp_parse(&tlvp, (const uint8_t *)msg->l2h+1, msgb_l2len(msg)-1);
Harald Weltecc2241b2011-07-19 16:06:06 +0200126 DEBUGP(DLMI, "\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200127 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200128 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200129 "with malformed TLVs\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200130 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200131 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200132 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200133 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200134 "without unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200135 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200136
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200137 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200138 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200139 if (len < 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200140 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200141 "with too small unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200142 goto err;
143 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200144 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
145 unitid[len - 1] = '\0';
Vadim Yanitskiy1c94f6a2019-12-02 02:16:07 +0700146 ret = ipa_parse_unitid(unitid, &unit_data);
147 if (ret) {
148 LOGP(DLINP, LOGL_ERROR, "Failed to parse unit ID '%s'\n", unitid);
149 goto err;
150 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200151
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200152 if (!line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200153 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200154 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200155 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200156 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200157 /* the BSC creates the new sign links at this stage. */
158 if (bfd->priv_nr == E1INP_SIGN_OML) {
159 sign_link =
160 line->ops->sign_link_up(&unit_data, line,
161 E1INP_SIGN_OML);
162 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200163 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200164 "Unable to set signal link, "
165 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200166 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200167 }
168 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200169 struct e1inp_ts *ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200170 struct osmo_fd *newbfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200171 struct e1inp_line *new_line;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200172
173 sign_link =
174 line->ops->sign_link_up(&unit_data, line,
175 E1INP_SIGN_RSL);
176 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200177 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200178 "Unable to set signal link, "
179 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200180 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200181 }
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200182 /* this is a bugtrap, the BSC should be using the
183 * virtual E1 line used by OML for this RSL link. */
184 if (sign_link->ts->line == line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200185 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200186 "Fix your BSC, you should use the "
187 "E1 line used by the OML link for "
188 "your RSL link.\n");
189 return 0;
190 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200191 /* Finally, we know which OML link is associated with
192 * this RSL link, attach it to this socket. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200193 bfd->data = new_line = sign_link->ts->line;
194 e1inp_line_get(new_line);
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200195 ts = e1inp_line_ipa_rsl_ts(new_line, unit_data.trx_id);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200196 newbfd = &ts->driver.ipaccess.fd;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200197
198 /* get rid of our old temporary bfd */
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200199 /* preserve 'newbfd->when' flags potentially set by sign_link_up() */
Pau Espin Pedrol67902bb2018-08-23 14:35:08 +0200200 osmo_fd_setup(newbfd, bfd->fd, newbfd->when | bfd->when, bfd->cb,
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200201 bfd->data, E1INP_SIGN_RSL + unit_data.trx_id);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200202 osmo_fd_unregister(bfd);
203 bfd->fd = -1;
Harald Welteae3a9932016-11-26 09:25:23 +0100204 ret = osmo_fd_register(newbfd);
205 if (ret < 0) {
206 LOGP(DLINP, LOGL_ERROR,
207 "could not register FD\n");
208 goto err;
209 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200210 /* now we can release the dummy RSL line. */
211 e1inp_line_put(line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200212 return 1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200213 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200214 break;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200215 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200216 LOGP(DLINP, LOGL_ERROR, "Unknown IPA message type\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200217 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200218 }
219 return 0;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200220err:
221 osmo_fd_unregister(bfd);
Harald Welteb4a7db02019-07-21 11:51:56 +0200222 if (bfd->fd != -1) {
223 close(bfd->fd);
224 bfd->fd = -1;
225 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200226 e1inp_line_put(line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200227 return -1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200228}
229
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200230/* Returns -EBADF if bfd cannot be used by the caller anymore after return. */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200231static int handle_ts1_read(struct osmo_fd *bfd)
232{
233 struct e1inp_line *line = bfd->data;
234 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200235 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200236 struct e1inp_sign_link *link;
237 struct ipaccess_head *hh;
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200238 struct msgb *msg = NULL;
Maxc9fa25e2017-01-09 13:23:15 +0100239 int ret, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200240
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200241 if (bfd->priv_nr == E1INP_SIGN_OML)
242 e1i_ts = e1inp_line_ipa_oml_ts(line);
243 else
244 e1i_ts = e1inp_line_ipa_rsl_ts(line, bfd->priv_nr - E1INP_SIGN_RSL);
245
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200246 ret = ipa_msg_recv_buffered(bfd->fd, &msg, &e1i_ts->pending_msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200247 if (ret < 0) {
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200248 if (ret == -EAGAIN)
249 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200250 LOGP(DLINP, LOGL_NOTICE, "Sign link problems, "
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200251 "closing socket. Reason: %s\n", strerror(-ret));
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200252 goto err;
253 } else if (ret == 0) {
254 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
255 goto err;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200256 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200257 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200258
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200259 hh = (struct ipaccess_head *) msg->data;
260 if (hh->proto == IPAC_PROTO_IPACCESS) {
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200261 ret = ipaccess_rcvmsg(line, msg, bfd);
262 /* BIG FAT WARNING: bfd might no longer exist here (ret != 0),
263 * since ipaccess_rcvmsg() might have free'd it !!! */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200264 msgb_free(msg);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200265 return ret != 0 ? -EBADF : 0;
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100266 } else if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
267 /* this sign link is not know yet.. complain. */
268 LOGP(DLINP, LOGL_ERROR, "Timeslot is not configured.\n");
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100269 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200270 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200271
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200272 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
273 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200274 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200275 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200276 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200277 }
278 msg->dst = link;
279
280 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200281 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200282 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200283 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200284 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200285 }
Maxc9fa25e2017-01-09 13:23:15 +0100286 rc = e1i_ts->line->ops->sign_link(msg);
287 if (rc < 0) {
Pablo Neira Ayusoa49c24d2012-10-16 11:24:08 +0200288 /* Don't close the signalling link if the upper layers report
289 * an error, that's too strict. BTW, the signalling layer is
290 * resposible for releasing the message.
291 */
Harald Weltecc2241b2011-07-19 16:06:06 +0200292 LOGP(DLINP, LOGL_ERROR, "Bad signalling message,"
Maxc9fa25e2017-01-09 13:23:15 +0100293 " sign_link returned error: %s.\n", strerror(-rc));
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200294 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200295
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200296 return rc;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200297err_msg:
298 msgb_free(msg);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200299err:
Harald Welte10b41302013-06-30 14:05:49 +0200300 ipaccess_drop(bfd, line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200301 return -EBADF;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200302}
303
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200304static int ts_want_write(struct e1inp_ts *e1i_ts)
305{
306 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
307
308 return 0;
309}
310
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200311static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200312{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200313 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200314 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100315 return e1inp_close_socket(e1i_ts, sign_link, bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200316}
317
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200318static void timeout_ts1_write(void *data)
319{
320 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
321
322 /* trigger write of ts1, due to tx delay timer */
323 ts_want_write(e1i_ts);
324}
325
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200326static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200327{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200328 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200329 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200330 struct e1inp_sign_link *sign_link;
331 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200332 int ret;
333
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200334 if (bfd->priv_nr == E1INP_SIGN_OML)
335 e1i_ts = e1inp_line_ipa_oml_ts(line);
336 else
337 e1i_ts = e1inp_line_ipa_rsl_ts(line, bfd->priv_nr - E1INP_SIGN_RSL);
338
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200339 bfd->when &= ~BSC_FD_WRITE;
340
341 /* get the next msg for this timeslot */
342 msg = e1inp_tx_ts(e1i_ts, &sign_link);
343 if (!msg) {
344 /* no message after tx delay timer */
345 return 0;
346 }
347
348 switch (sign_link->type) {
349 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200350 case E1INP_SIGN_RSL:
Harald Welte46fc7e22014-08-18 19:04:26 +0200351 case E1INP_SIGN_OSMO:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200352 break;
353 default:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200354 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200355 ret = -EINVAL;
356 goto out;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200357 }
358
359 msg->l2h = msg->data;
Harald Welteb65f58f2014-08-20 22:04:11 +0200360 ipa_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200361
Harald Weltecc2241b2011-07-19 16:06:06 +0200362 DEBUGP(DLMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200363
364 ret = send(bfd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200365 if (ret != msg->len) {
366 LOGP(DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling "
367 "message. Reason: %s\n", strerror(errno));
368 goto err;
369 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200370
371 /* set tx delay timer for next event */
Pablo Neira Ayusob26f2fd2017-06-07 18:32:13 +0200372 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200373
374 /* Reducing this might break the nanoBTS 900 init. */
375 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
376
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200377out:
378 msgb_free(msg);
379 return ret;
380err:
Harald Welte10b41302013-06-30 14:05:49 +0200381 ipaccess_drop(bfd, line);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200382 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200383 return ret;
384}
385
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200386static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200387{
388 struct e1inp_line *line = bfd->data;
389
390 return __handle_ts1_write(bfd, line);
391}
392
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200393static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200394{
395 struct e1inp_line *line = link->line;
396
397 return __handle_ts1_write(link->ofd, line);
398}
399
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200400/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200401int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200402{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200403 int rc = 0;
404
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200405 if (what & BSC_FD_READ)
406 rc = handle_ts1_read(bfd);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200407 if (rc != -EBADF && (what & BSC_FD_WRITE))
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200408 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200409
410 return rc;
411}
412
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200413static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200414
415struct e1inp_driver ipaccess_driver = {
416 .name = "ipa",
417 .want_write = ts_want_write,
418 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200419 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200420 .default_delay = 0,
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100421 .has_keepalive = 1,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200422};
423
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100424static void update_fd_settings(struct e1inp_line *line, int fd)
425{
426 int ret;
427 int val;
428
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100429 if (line->keepalive_num_probes) {
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100430 /* Enable TCP keepalive to find out if the connection is gone */
431 val = 1;
432 ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
433 if (ret < 0)
434 LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive: %s\n",
435 strerror(errno));
436 else
437 LOGP(DLINP, LOGL_NOTICE, "Keepalive is set: %i\n", ret);
438
439#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
440 /* The following options are not portable! */
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100441 val = line->keepalive_idle_timeout > 0 ?
442 line->keepalive_idle_timeout :
443 DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100444 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
445 &val, sizeof(val));
446 if (ret < 0)
447 LOGP(DLINP, LOGL_NOTICE,
448 "Failed to set keepalive idle time: %s\n",
449 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100450 val = line->keepalive_probe_interval > -1 ?
451 line->keepalive_probe_interval :
452 DEFAULT_TCP_KEEPALIVE_INTERVAL;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100453 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
454 &val, sizeof(val));
455 if (ret < 0)
456 LOGP(DLINP, LOGL_NOTICE,
457 "Failed to set keepalive interval: %s\n",
458 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100459 val = line->keepalive_num_probes > 0 ?
460 line->keepalive_num_probes :
461 DEFAULT_TCP_KEEPALIVE_RETRY_COUNT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100462 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
463 &val, sizeof(val));
464 if (ret < 0)
465 LOGP(DLINP, LOGL_NOTICE,
466 "Failed to set keepalive count: %s\n",
467 strerror(errno));
Eric Wild6eb186c2019-06-21 15:26:25 +0200468#if defined(TCP_USER_TIMEOUT)
469 val = 1000 * line->keepalive_num_probes *
470 line->keepalive_probe_interval +
471 line->keepalive_idle_timeout;
472 ret = setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
473 &val, sizeof(val));
474 if (ret < 0)
475 LOGP(DLINP, LOGL_NOTICE,
476 "Failed to set user timoeut: %s\n",
477 strerror(errno));
478#endif
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100479#endif
Holger Hans Peter Freytherf465a4c2014-02-03 09:34:02 +0100480 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100481}
482
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200483/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200484static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200485{
486 int ret;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100487 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200488 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200489 struct e1inp_ts *e1i_ts;
490 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200491
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200492 /* clone virtual E1 line for this new OML link. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200493 line = e1inp_line_clone(tall_ipa_ctx, link->line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200494 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200495 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200496 return -ENOMEM;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200497 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200498
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200499 /* create virrtual E1 timeslots for signalling */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200500 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200501
502 /* initialize the fds */
503 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
504 line->ts[i].driver.ipaccess.fd.fd = -1;
505
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200506 e1i_ts = e1inp_line_ipa_oml_ts(line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200507
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200508 bfd = &e1i_ts->driver.ipaccess.fd;
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200509 osmo_fd_setup(bfd, fd, BSC_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_OML);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200510 ret = osmo_fd_register(bfd);
511 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200512 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200513 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200514 }
515
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100516 update_fd_settings(line, bfd->fd);
Daniel Willmann85980722014-01-09 14:30:55 +0100517
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200518 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200519 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200520 if (ret < 0) {
521 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
522 strerror(errno));
523 goto err_socket;
524 }
525 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200526
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200527err_socket:
528 osmo_fd_unregister(bfd);
529err_line:
530 close(bfd->fd);
531 bfd->fd = -1;
532 e1inp_line_put(line);
533 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200534}
535
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200536static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200537{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200538 struct e1inp_line *line;
539 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200540 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200541 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200542
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200543 /* We don't know yet which OML link to associate it with. Thus, we
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200544 * allocate a temporary E1 line until we have received ID. */
545 line = e1inp_line_clone(tall_ipa_ctx, link->line);
546 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200547 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200548 return -ENOMEM;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200549 }
550 /* initialize the fds */
551 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
552 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200553
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200554 /* we need this to initialize this in case to avoid crashes in case
555 * that the socket is closed before we've seen an ID_RESP. */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200556 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200557
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200558 e1i_ts = e1inp_line_ipa_rsl_ts(line, 0);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200559
560 bfd = &e1i_ts->driver.ipaccess.fd;
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200561 osmo_fd_setup(bfd, fd, BSC_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_RSL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200562 ret = osmo_fd_register(bfd);
563 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200564 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200565 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200566 }
567 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200568 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200569 if (ret < 0) {
570 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
571 strerror(errno));
572 goto err_socket;
573 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100574 update_fd_settings(line, bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200575 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200576
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200577err_socket:
578 osmo_fd_unregister(bfd);
579err_line:
580 close(bfd->fd);
581 bfd->fd = -1;
582 e1inp_line_put(line);
583 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200584}
585
Harald Weltee416e2e2017-05-26 13:11:59 +0200586#define IPA_STRING_MAX 64
587
588static struct msgb *
Max71393522018-01-08 15:42:00 +0100589ipa_bts_id_resp(const struct ipaccess_unit *dev, uint8_t *data, int len, int trx_nr)
Harald Weltee416e2e2017-05-26 13:11:59 +0200590{
591 struct msgb *nmsg;
592 char str[IPA_STRING_MAX];
593 uint8_t *tag;
594
595 memset(str, 0, sizeof(str));
596
597 nmsg = ipa_msg_alloc(0);
598 if (!nmsg)
599 return NULL;
600
601 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
602 while (len) {
603 if (len < 2) {
604 LOGP(DLINP, LOGL_NOTICE,
605 "Short read of ipaccess tag\n");
606 msgb_free(nmsg);
607 return NULL;
608 }
609 switch (data[1]) {
610 case IPAC_IDTAG_UNIT:
611 snprintf(str, sizeof(str), "%u/%u/%u",
612 dev->site_id, dev->bts_id, trx_nr);
613 break;
614 case IPAC_IDTAG_MACADDR:
615 snprintf(str, sizeof(str),
616 "%02x:%02x:%02x:%02x:%02x:%02x",
617 dev->mac_addr[0], dev->mac_addr[1],
618 dev->mac_addr[2], dev->mac_addr[3],
619 dev->mac_addr[4], dev->mac_addr[5]);
620 break;
621 case IPAC_IDTAG_LOCATION1:
622 if (dev->location1)
Stefan Sperling961776a2018-12-06 13:09:48 +0100623 osmo_strlcpy(str, dev->location1, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200624 break;
625 case IPAC_IDTAG_LOCATION2:
626 if (dev->location2)
Stefan Sperling961776a2018-12-06 13:09:48 +0100627 osmo_strlcpy(str, dev->location2, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200628 break;
629 case IPAC_IDTAG_EQUIPVERS:
630 if (dev->equipvers)
Stefan Sperling961776a2018-12-06 13:09:48 +0100631 osmo_strlcpy(str, dev->equipvers, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200632 break;
633 case IPAC_IDTAG_SWVERSION:
634 if (dev->swversion)
Neels Hofmeyr4c57eef2018-07-26 17:28:24 +0200635 osmo_strlcpy(str, dev->swversion, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200636 break;
637 case IPAC_IDTAG_UNITNAME:
638 snprintf(str, sizeof(str),
639 "%s-%02x-%02x-%02x-%02x-%02x-%02x",
640 dev->unit_name,
641 dev->mac_addr[0], dev->mac_addr[1],
642 dev->mac_addr[2], dev->mac_addr[3],
643 dev->mac_addr[4], dev->mac_addr[5]);
644 break;
645 case IPAC_IDTAG_SERNR:
646 if (dev->serno)
Stefan Sperling961776a2018-12-06 13:09:48 +0100647 osmo_strlcpy(str, dev->serno, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200648 break;
649 default:
650 LOGP(DLINP, LOGL_NOTICE,
651 "Unknown ipaccess tag 0x%02x\n", *data);
652 msgb_free(nmsg);
653 return NULL;
654 }
Harald Weltee416e2e2017-05-26 13:11:59 +0200655
656 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
657 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
658 tag[0] = 0x00;
659 tag[1] = 1 + strlen(str) + 1;
660 tag[2] = data[1];
661 memcpy(tag + 3, str, strlen(str) + 1);
662 data += 2;
663 len -= 2;
664 }
665 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
666 return nmsg;
667}
668
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200669static struct msgb *ipa_bts_id_ack(void)
670{
671 struct msgb *nmsg2;
672
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200673 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200674 if (!nmsg2)
675 return NULL;
676
677 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200678 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200679
680 return nmsg2;
681}
682
Harald Welte51de9ca2013-06-30 20:13:16 +0200683static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up)
684{
685 struct e1inp_line *line = link->line;
686
Eric Wildb8242722019-06-21 13:52:06 +0200687 if (up) {
688 update_fd_settings(line, link->ofd->fd);
689 return;
690 }
Harald Welte51de9ca2013-06-30 20:13:16 +0200691
692 if (line->ops->sign_link_down)
693 line->ops->sign_link_down(line);
694}
695
Harald Welte783715b2014-08-17 18:24:51 +0200696/* handle incoming message to BTS, check if it is an IPA CCM, and if yes,
697 * handle it accordingly (PING/PONG/ID_REQ/ID_RESP/ID_ACK) */
698int ipaccess_bts_handle_ccm(struct ipa_client_conn *link,
699 struct ipaccess_unit *dev, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200700{
701 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200702 struct msgb *rmsg;
703 int ret = 0;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200704
705 /* special handling for IPA CCM. */
706 if (hh->proto == IPAC_PROTO_IPACCESS) {
707 uint8_t msg_type = *(msg->l2h);
708
709 /* ping, pong and acknowledgment cases. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200710 ret = ipa_ccm_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200711 if (ret < 0)
712 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200713
714 /* this is a request for identification from the BSC. */
715 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200716 uint8_t *data = msgb_l2(msg);
717 int len = msgb_l2len(msg);
Andreas Eversbergf422a752014-01-21 14:54:41 +0100718 int trx_nr = 0;
719
720 if (link->ofd->priv_nr >= E1INP_SIGN_RSL)
721 trx_nr = link->ofd->priv_nr - E1INP_SIGN_RSL;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200722
Vadim Yanitskiy69ae2382019-12-02 02:42:43 +0700723 LOGP(DLINP, LOGL_NOTICE, "received ID_GET for unit ID %u/%u/%u\n",
Pau Espin Pedroldd95eb62018-10-02 20:05:28 +0200724 dev->site_id, dev->bts_id, trx_nr);
Harald Weltee416e2e2017-05-26 13:11:59 +0200725 rmsg = ipa_bts_id_resp(dev, data + 1, len - 1, trx_nr);
Harald Welteb65f58f2014-08-20 22:04:11 +0200726 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200727 if (ret != rmsg->len) {
728 LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP "
729 "message. Reason: %s\n", strerror(errno));
730 goto err_rmsg;
731 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200732 msgb_free(rmsg);
733
734 /* send ID_ACK. */
735 rmsg = ipa_bts_id_ack();
Harald Welteb65f58f2014-08-20 22:04:11 +0200736 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200737 if (ret != rmsg->len) {
738 LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK "
739 "message. Reason: %s\n", strerror(errno));
740 goto err_rmsg;
741 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200742 msgb_free(rmsg);
Harald Welte783715b2014-08-17 18:24:51 +0200743 }
744 return 1;
745 }
Harald Welte6eddd472013-06-30 20:18:53 +0200746
Harald Welte783715b2014-08-17 18:24:51 +0200747 return 0;
748
749err_rmsg:
750 msgb_free(rmsg);
751err:
752 ipa_client_conn_close(link);
753 return -1;
754}
755
756static int ipaccess_bts_read_cb(struct ipa_client_conn *link, struct msgb *msg)
757{
758 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
759 struct e1inp_ts *e1i_ts = NULL;
760 struct e1inp_sign_link *sign_link;
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200761 uint8_t msg_type = *(msg->l2h);
Harald Welte783715b2014-08-17 18:24:51 +0200762 int ret = 0;
763
764 /* special handling for IPA CCM. */
765 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200766 /* this is a request for identification from the BSC. */
767 if (msg_type == IPAC_MSGT_ID_GET) {
768 if (!link->line->ops->sign_link_up) {
769 LOGP(DLINP, LOGL_ERROR,
770 "Unable to set signal link, "
771 "closing socket.\n");
Harald Welte783715b2014-08-17 18:24:51 +0200772 goto err;
773 }
774 }
775 }
776
777 /* core CCM handling */
778 ret = ipaccess_bts_handle_ccm(link, link->line->ops->cfg.ipa.dev, msg);
779 if (ret < 0)
780 goto err;
781
782 if (ret == 1 && hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200783 if (msg_type == IPAC_MSGT_ID_GET) {
Harald Welte6eddd472013-06-30 20:18:53 +0200784 sign_link = link->line->ops->sign_link_up(msg,
785 link->line,
786 link->ofd->priv_nr);
787 if (sign_link == NULL) {
788 LOGP(DLINP, LOGL_ERROR,
789 "Unable to set signal link, "
790 "closing socket.\n");
Harald Welte6eddd472013-06-30 20:18:53 +0200791 goto err;
792 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200793 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200794 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200795 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200796 } else if (link->port == IPA_TCP_PORT_OML)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200797 e1i_ts = e1inp_line_ipa_oml_ts(link->line);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200798 else if (link->port == IPA_TCP_PORT_RSL)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200799 e1i_ts = e1inp_line_ipa_rsl_ts(link->line, link->ofd->priv_nr - E1INP_SIGN_RSL);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200800
Pablo Neira Ayusob9487012013-07-05 14:38:43 +0200801 OSMO_ASSERT(e1i_ts != NULL);
802
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200803 if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
804 LOGP(DLINP, LOGL_ERROR, "Signalling link not initialized. Discarding."
805 " port=%u msg_type=%u\n", link->port, msg_type);
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200806 goto err;
807 }
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +0200808
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200809 /* look up for some existing signaling link. */
810 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
811 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200812 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200813 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200814 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200815 }
816 msg->dst = sign_link;
817
818 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200819 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200820 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200821 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200822 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200823 }
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200824 return link->line->ops->sign_link(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200825
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200826err:
Harald Welte783715b2014-08-17 18:24:51 +0200827 ipa_client_conn_close(link);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200828 msgb_free(msg);
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +0200829 return -EBADF;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200830}
831
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200832struct ipaccess_line {
833 int line_already_initialized;
834};
835
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200836static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200837{
838 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200839 struct ipaccess_line *il;
840
841 if (!line->driver_data)
842 line->driver_data = talloc_zero(line, struct ipaccess_line);
843
844 if (!line->driver_data) {
845 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
846 return -ENOMEM;
847 }
848 il = line->driver_data;
849
850 /* We only initialize this line once. */
851 if (il->line_already_initialized)
852 return 0;
853
854 il->line_already_initialized = 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200855
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200856 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200857 case E1INP_LINE_R_BSC: {
858 struct ipa_server_link *oml_link, *rsl_link;
Max4c4a1c22016-12-30 15:10:47 +0100859 const char *ipa = e1inp_ipa_get_bind_addr();
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200860
Max4c4a1c22016-12-30 15:10:47 +0100861 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode on %s "
862 "with OML %u and RSL %u TCP ports\n", ipa,
863 IPA_TCP_PORT_OML, IPA_TCP_PORT_RSL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200864
Max4c4a1c22016-12-30 15:10:47 +0100865 oml_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100866 IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200867 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200868 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200869 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200870 "BSC link: %s\n", strerror(errno));
871 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200872 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200873 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200874 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200875 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200876 ipa_server_link_destroy(oml_link);
877 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200878 }
Max4c4a1c22016-12-30 15:10:47 +0100879 rsl_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100880 IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200881 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200882 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200883 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200884 "BSC link: %s\n", strerror(errno));
885 return -ENOMEM;
886 }
887 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200888 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200889 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200890 ipa_server_link_destroy(rsl_link);
891 return -EIO;
892 }
893 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200894 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200895 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200896 case E1INP_LINE_R_BTS: {
Harald Welte84f67b22013-06-30 13:13:59 +0200897 struct ipa_client_conn *link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200898
Max4c4a1c22016-12-30 15:10:47 +0100899 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode, "
900 "OML connecting to %s:%u\n", line->ops->cfg.ipa.addr,
901 IPA_TCP_PORT_OML);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200902
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +0100903 link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200904 e1inp_line_ipa_oml_ts(line),
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +0200905 E1INP_SIGN_OML,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +0100906 NULL, 0,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200907 line->ops->cfg.ipa.addr,
908 IPA_TCP_PORT_OML,
Harald Welte51de9ca2013-06-30 20:13:16 +0200909 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +0200910 ipaccess_bts_read_cb,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200911 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +0200912 line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200913 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200914 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200915 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200916 return -ENOMEM;
917 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200918 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200919 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200920 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200921 ipa_client_conn_close(link);
922 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200923 return -EIO;
924 }
925 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200926 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200927 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200928 default:
929 break;
930 }
931 return ret;
932}
933
Andreas Eversbergf422a752014-01-21 14:54:41 +0100934
935/* backwards compatibility */
Harald Welte84f67b22013-06-30 13:13:59 +0200936int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line,
937 const char *rem_addr, uint16_t rem_port)
938{
Andreas Eversbergf422a752014-01-21 14:54:41 +0100939 return e1inp_ipa_bts_rsl_connect_n(line, rem_addr, rem_port, 0);
940}
941
942int e1inp_ipa_bts_rsl_connect_n(struct e1inp_line *line,
943 const char *rem_addr, uint16_t rem_port,
944 uint8_t trx_nr)
945{
Harald Welte84f67b22013-06-30 13:13:59 +0200946 struct ipa_client_conn *rsl_link;
947
Andreas Eversbergf422a752014-01-21 14:54:41 +0100948 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
949 LOGP(DLINP, LOGL_ERROR, "cannot create RSL BTS link: "
950 "trx_nr (%d) out of range\n", trx_nr);
951 return -EINVAL;
952 }
Andreas Eversbergf422a752014-01-21 14:54:41 +0100953
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +0100954 rsl_link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200955 e1inp_line_ipa_rsl_ts(line, trx_nr),
Andreas Eversbergf422a752014-01-21 14:54:41 +0100956 E1INP_SIGN_RSL+trx_nr,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +0100957 NULL, 0,
Harald Welte84f67b22013-06-30 13:13:59 +0200958 rem_addr, rem_port,
Harald Welte51de9ca2013-06-30 20:13:16 +0200959 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +0200960 ipaccess_bts_read_cb,
Harald Welte84f67b22013-06-30 13:13:59 +0200961 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +0200962 line);
Harald Welte84f67b22013-06-30 13:13:59 +0200963 if (rsl_link == NULL) {
964 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
965 "BTS link: %s\n", strerror(errno));
966 return -ENOMEM;
967 }
968 if (ipa_client_conn_open(rsl_link) < 0) {
969 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
970 strerror(errno));
971 ipa_client_conn_close(rsl_link);
972 ipa_client_conn_destroy(rsl_link);
973 return -EIO;
974 }
975 return 0;
976}
977
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200978void e1inp_ipaccess_init(void)
979{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200980 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200981 e1inp_driver_register(&ipaccess_driver);
982}
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100983
984void e1inp_ipa_set_bind_addr(const char *ip_bind_addr)
985{
986 talloc_free((char*)ipaccess_driver.bind_addr);
987 ipaccess_driver.bind_addr = NULL;
988
989 if (ip_bind_addr)
990 ipaccess_driver.bind_addr = talloc_strdup(tall_ipa_ctx,
991 ip_bind_addr);
992}
993
994const char *e1inp_ipa_get_bind_addr(void)
995{
996 return ipaccess_driver.bind_addr?
997 ipaccess_driver.bind_addr
998 : "0.0.0.0";
999}