blob: 0f8e2d5cf1b967a86f59ec8afc052b5ed8e52469 [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';
Harald Welteb65f58f2014-08-20 22:04:11 +0200146 ipa_parse_unitid(unitid, &unit_data);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200147
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200148 if (!line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200149 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200150 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200151 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200152 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200153 /* the BSC creates the new sign links at this stage. */
154 if (bfd->priv_nr == E1INP_SIGN_OML) {
155 sign_link =
156 line->ops->sign_link_up(&unit_data, line,
157 E1INP_SIGN_OML);
158 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200159 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200160 "Unable to set signal link, "
161 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200162 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200163 }
164 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200165 struct e1inp_ts *ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200166 struct osmo_fd *newbfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200167 struct e1inp_line *new_line;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200168
169 sign_link =
170 line->ops->sign_link_up(&unit_data, line,
171 E1INP_SIGN_RSL);
172 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200173 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200174 "Unable to set signal link, "
175 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200176 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200177 }
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200178 /* 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 Weltecc2241b2011-07-19 16:06:06 +0200181 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200182 "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 Ayusoc9c4fd32011-06-30 12:19:42 +0200187 /* Finally, we know which OML link is associated with
188 * this RSL link, attach it to this socket. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200189 bfd->data = new_line = sign_link->ts->line;
190 e1inp_line_get(new_line);
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200191 ts = e1inp_line_ipa_rsl_ts(new_line, unit_data.trx_id);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200192 newbfd = &ts->driver.ipaccess.fd;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200193
194 /* get rid of our old temporary bfd */
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200195 /* preserve 'newbfd->when' flags potentially set by sign_link_up() */
Pau Espin Pedrol67902bb2018-08-23 14:35:08 +0200196 osmo_fd_setup(newbfd, bfd->fd, newbfd->when | bfd->when, bfd->cb,
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200197 bfd->data, E1INP_SIGN_RSL + unit_data.trx_id);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200198 osmo_fd_unregister(bfd);
199 bfd->fd = -1;
Harald Welteae3a9932016-11-26 09:25:23 +0100200 ret = osmo_fd_register(newbfd);
201 if (ret < 0) {
202 LOGP(DLINP, LOGL_ERROR,
203 "could not register FD\n");
204 goto err;
205 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200206 /* now we can release the dummy RSL line. */
207 e1inp_line_put(line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200208 return 1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200209 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200210 break;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200211 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200212 LOGP(DLINP, LOGL_ERROR, "Unknown IPA message type\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200213 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200214 }
215 return 0;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200216err:
217 osmo_fd_unregister(bfd);
Harald Welteb4a7db02019-07-21 11:51:56 +0200218 if (bfd->fd != -1) {
219 close(bfd->fd);
220 bfd->fd = -1;
221 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200222 e1inp_line_put(line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200223 return -1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200224}
225
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200226/* Returns -EBADF if bfd cannot be used by the caller anymore after return. */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200227static int handle_ts1_read(struct osmo_fd *bfd)
228{
229 struct e1inp_line *line = bfd->data;
230 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200231 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200232 struct e1inp_sign_link *link;
233 struct ipaccess_head *hh;
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200234 struct msgb *msg = NULL;
Maxc9fa25e2017-01-09 13:23:15 +0100235 int ret, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200236
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200237 if (bfd->priv_nr == E1INP_SIGN_OML)
238 e1i_ts = e1inp_line_ipa_oml_ts(line);
239 else
240 e1i_ts = e1inp_line_ipa_rsl_ts(line, bfd->priv_nr - E1INP_SIGN_RSL);
241
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200242 ret = ipa_msg_recv_buffered(bfd->fd, &msg, &e1i_ts->pending_msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200243 if (ret < 0) {
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200244 if (ret == -EAGAIN)
245 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200246 LOGP(DLINP, LOGL_NOTICE, "Sign link problems, "
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200247 "closing socket. Reason: %s\n", strerror(-ret));
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200248 goto err;
249 } else if (ret == 0) {
250 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
251 goto err;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200252 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200253 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200254
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200255 hh = (struct ipaccess_head *) msg->data;
256 if (hh->proto == IPAC_PROTO_IPACCESS) {
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200257 ret = ipaccess_rcvmsg(line, msg, bfd);
258 /* BIG FAT WARNING: bfd might no longer exist here (ret != 0),
259 * since ipaccess_rcvmsg() might have free'd it !!! */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200260 msgb_free(msg);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200261 return ret != 0 ? -EBADF : 0;
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100262 } else if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
263 /* this sign link is not know yet.. complain. */
264 LOGP(DLINP, LOGL_ERROR, "Timeslot is not configured.\n");
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100265 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200266 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200267
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200268 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
269 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200270 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200271 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200272 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200273 }
274 msg->dst = link;
275
276 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200277 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200278 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200279 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200280 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200281 }
Maxc9fa25e2017-01-09 13:23:15 +0100282 rc = e1i_ts->line->ops->sign_link(msg);
283 if (rc < 0) {
Pablo Neira Ayusoa49c24d2012-10-16 11:24:08 +0200284 /* Don't close the signalling link if the upper layers report
285 * an error, that's too strict. BTW, the signalling layer is
286 * resposible for releasing the message.
287 */
Harald Weltecc2241b2011-07-19 16:06:06 +0200288 LOGP(DLINP, LOGL_ERROR, "Bad signalling message,"
Maxc9fa25e2017-01-09 13:23:15 +0100289 " sign_link returned error: %s.\n", strerror(-rc));
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200290 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200291
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200292 return rc;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200293err_msg:
294 msgb_free(msg);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200295err:
Harald Welte10b41302013-06-30 14:05:49 +0200296 ipaccess_drop(bfd, line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200297 return -EBADF;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200298}
299
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200300static int ts_want_write(struct e1inp_ts *e1i_ts)
301{
302 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
303
304 return 0;
305}
306
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200307static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200308{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200309 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200310 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100311 return e1inp_close_socket(e1i_ts, sign_link, bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200312}
313
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200314static void timeout_ts1_write(void *data)
315{
316 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
317
318 /* trigger write of ts1, due to tx delay timer */
319 ts_want_write(e1i_ts);
320}
321
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200322static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200323{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200324 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200325 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200326 struct e1inp_sign_link *sign_link;
327 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200328 int ret;
329
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200330 if (bfd->priv_nr == E1INP_SIGN_OML)
331 e1i_ts = e1inp_line_ipa_oml_ts(line);
332 else
333 e1i_ts = e1inp_line_ipa_rsl_ts(line, bfd->priv_nr - E1INP_SIGN_RSL);
334
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200335 bfd->when &= ~BSC_FD_WRITE;
336
337 /* get the next msg for this timeslot */
338 msg = e1inp_tx_ts(e1i_ts, &sign_link);
339 if (!msg) {
340 /* no message after tx delay timer */
341 return 0;
342 }
343
344 switch (sign_link->type) {
345 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200346 case E1INP_SIGN_RSL:
Harald Welte46fc7e22014-08-18 19:04:26 +0200347 case E1INP_SIGN_OSMO:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200348 break;
349 default:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200350 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200351 ret = -EINVAL;
352 goto out;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200353 }
354
355 msg->l2h = msg->data;
Harald Welteb65f58f2014-08-20 22:04:11 +0200356 ipa_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200357
Harald Weltecc2241b2011-07-19 16:06:06 +0200358 DEBUGP(DLMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200359
360 ret = send(bfd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200361 if (ret != msg->len) {
362 LOGP(DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling "
363 "message. Reason: %s\n", strerror(errno));
364 goto err;
365 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200366
367 /* set tx delay timer for next event */
Pablo Neira Ayusob26f2fd2017-06-07 18:32:13 +0200368 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200369
370 /* Reducing this might break the nanoBTS 900 init. */
371 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
372
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200373out:
374 msgb_free(msg);
375 return ret;
376err:
Harald Welte10b41302013-06-30 14:05:49 +0200377 ipaccess_drop(bfd, line);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200378 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200379 return ret;
380}
381
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200382static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200383{
384 struct e1inp_line *line = bfd->data;
385
386 return __handle_ts1_write(bfd, line);
387}
388
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200389static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200390{
391 struct e1inp_line *line = link->line;
392
393 return __handle_ts1_write(link->ofd, line);
394}
395
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200396/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200397int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200398{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200399 int rc = 0;
400
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200401 if (what & BSC_FD_READ)
402 rc = handle_ts1_read(bfd);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200403 if (rc != -EBADF && (what & BSC_FD_WRITE))
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200404 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200405
406 return rc;
407}
408
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200409static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200410
411struct e1inp_driver ipaccess_driver = {
412 .name = "ipa",
413 .want_write = ts_want_write,
414 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200415 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200416 .default_delay = 0,
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100417 .has_keepalive = 1,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200418};
419
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100420static void update_fd_settings(struct e1inp_line *line, int fd)
421{
422 int ret;
423 int val;
424
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100425 if (line->keepalive_num_probes) {
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100426 /* Enable TCP keepalive to find out if the connection is gone */
427 val = 1;
428 ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
429 if (ret < 0)
430 LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive: %s\n",
431 strerror(errno));
432 else
433 LOGP(DLINP, LOGL_NOTICE, "Keepalive is set: %i\n", ret);
434
435#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
436 /* The following options are not portable! */
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100437 val = line->keepalive_idle_timeout > 0 ?
438 line->keepalive_idle_timeout :
439 DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100440 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
441 &val, sizeof(val));
442 if (ret < 0)
443 LOGP(DLINP, LOGL_NOTICE,
444 "Failed to set keepalive idle time: %s\n",
445 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100446 val = line->keepalive_probe_interval > -1 ?
447 line->keepalive_probe_interval :
448 DEFAULT_TCP_KEEPALIVE_INTERVAL;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100449 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
450 &val, sizeof(val));
451 if (ret < 0)
452 LOGP(DLINP, LOGL_NOTICE,
453 "Failed to set keepalive interval: %s\n",
454 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100455 val = line->keepalive_num_probes > 0 ?
456 line->keepalive_num_probes :
457 DEFAULT_TCP_KEEPALIVE_RETRY_COUNT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100458 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
459 &val, sizeof(val));
460 if (ret < 0)
461 LOGP(DLINP, LOGL_NOTICE,
462 "Failed to set keepalive count: %s\n",
463 strerror(errno));
Eric Wild6eb186c2019-06-21 15:26:25 +0200464#if defined(TCP_USER_TIMEOUT)
465 val = 1000 * line->keepalive_num_probes *
466 line->keepalive_probe_interval +
467 line->keepalive_idle_timeout;
468 ret = setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
469 &val, sizeof(val));
470 if (ret < 0)
471 LOGP(DLINP, LOGL_NOTICE,
472 "Failed to set user timoeut: %s\n",
473 strerror(errno));
474#endif
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100475#endif
Holger Hans Peter Freytherf465a4c2014-02-03 09:34:02 +0100476 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100477}
478
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200479/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200480static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200481{
482 int ret;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100483 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200484 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200485 struct e1inp_ts *e1i_ts;
486 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200487
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200488 /* clone virtual E1 line for this new OML link. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200489 line = e1inp_line_clone(tall_ipa_ctx, link->line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200490 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200491 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200492 return -ENOMEM;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200493 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200494
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200495 /* create virrtual E1 timeslots for signalling */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200496 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200497
498 /* initialize the fds */
499 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
500 line->ts[i].driver.ipaccess.fd.fd = -1;
501
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200502 e1i_ts = e1inp_line_ipa_oml_ts(line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200503
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200504 bfd = &e1i_ts->driver.ipaccess.fd;
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200505 osmo_fd_setup(bfd, fd, BSC_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_OML);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200506 ret = osmo_fd_register(bfd);
507 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200508 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200509 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200510 }
511
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100512 update_fd_settings(line, bfd->fd);
Daniel Willmann85980722014-01-09 14:30:55 +0100513
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200514 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200515 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200516 if (ret < 0) {
517 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
518 strerror(errno));
519 goto err_socket;
520 }
521 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200522
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200523err_socket:
524 osmo_fd_unregister(bfd);
525err_line:
526 close(bfd->fd);
527 bfd->fd = -1;
528 e1inp_line_put(line);
529 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200530}
531
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200532static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200533{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200534 struct e1inp_line *line;
535 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200536 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200537 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200538
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200539 /* We don't know yet which OML link to associate it with. Thus, we
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200540 * allocate a temporary E1 line until we have received ID. */
541 line = e1inp_line_clone(tall_ipa_ctx, link->line);
542 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200543 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200544 return -ENOMEM;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200545 }
546 /* initialize the fds */
547 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
548 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200549
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200550 /* we need this to initialize this in case to avoid crashes in case
551 * that the socket is closed before we've seen an ID_RESP. */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200552 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200553
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200554 e1i_ts = e1inp_line_ipa_rsl_ts(line, 0);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200555
556 bfd = &e1i_ts->driver.ipaccess.fd;
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200557 osmo_fd_setup(bfd, fd, BSC_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_RSL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200558 ret = osmo_fd_register(bfd);
559 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200560 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200561 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200562 }
563 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200564 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200565 if (ret < 0) {
566 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
567 strerror(errno));
568 goto err_socket;
569 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100570 update_fd_settings(line, bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200571 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200572
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200573err_socket:
574 osmo_fd_unregister(bfd);
575err_line:
576 close(bfd->fd);
577 bfd->fd = -1;
578 e1inp_line_put(line);
579 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200580}
581
Harald Weltee416e2e2017-05-26 13:11:59 +0200582#define IPA_STRING_MAX 64
583
584static struct msgb *
Max71393522018-01-08 15:42:00 +0100585ipa_bts_id_resp(const struct ipaccess_unit *dev, uint8_t *data, int len, int trx_nr)
Harald Weltee416e2e2017-05-26 13:11:59 +0200586{
587 struct msgb *nmsg;
588 char str[IPA_STRING_MAX];
589 uint8_t *tag;
590
591 memset(str, 0, sizeof(str));
592
593 nmsg = ipa_msg_alloc(0);
594 if (!nmsg)
595 return NULL;
596
597 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
598 while (len) {
599 if (len < 2) {
600 LOGP(DLINP, LOGL_NOTICE,
601 "Short read of ipaccess tag\n");
602 msgb_free(nmsg);
603 return NULL;
604 }
605 switch (data[1]) {
606 case IPAC_IDTAG_UNIT:
607 snprintf(str, sizeof(str), "%u/%u/%u",
608 dev->site_id, dev->bts_id, trx_nr);
609 break;
610 case IPAC_IDTAG_MACADDR:
611 snprintf(str, sizeof(str),
612 "%02x:%02x:%02x:%02x:%02x:%02x",
613 dev->mac_addr[0], dev->mac_addr[1],
614 dev->mac_addr[2], dev->mac_addr[3],
615 dev->mac_addr[4], dev->mac_addr[5]);
616 break;
617 case IPAC_IDTAG_LOCATION1:
618 if (dev->location1)
Stefan Sperling961776a2018-12-06 13:09:48 +0100619 osmo_strlcpy(str, dev->location1, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200620 break;
621 case IPAC_IDTAG_LOCATION2:
622 if (dev->location2)
Stefan Sperling961776a2018-12-06 13:09:48 +0100623 osmo_strlcpy(str, dev->location2, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200624 break;
625 case IPAC_IDTAG_EQUIPVERS:
626 if (dev->equipvers)
Stefan Sperling961776a2018-12-06 13:09:48 +0100627 osmo_strlcpy(str, dev->equipvers, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200628 break;
629 case IPAC_IDTAG_SWVERSION:
630 if (dev->swversion)
Neels Hofmeyr4c57eef2018-07-26 17:28:24 +0200631 osmo_strlcpy(str, dev->swversion, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200632 break;
633 case IPAC_IDTAG_UNITNAME:
634 snprintf(str, sizeof(str),
635 "%s-%02x-%02x-%02x-%02x-%02x-%02x",
636 dev->unit_name,
637 dev->mac_addr[0], dev->mac_addr[1],
638 dev->mac_addr[2], dev->mac_addr[3],
639 dev->mac_addr[4], dev->mac_addr[5]);
640 break;
641 case IPAC_IDTAG_SERNR:
642 if (dev->serno)
Stefan Sperling961776a2018-12-06 13:09:48 +0100643 osmo_strlcpy(str, dev->serno, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200644 break;
645 default:
646 LOGP(DLINP, LOGL_NOTICE,
647 "Unknown ipaccess tag 0x%02x\n", *data);
648 msgb_free(nmsg);
649 return NULL;
650 }
Harald Weltee416e2e2017-05-26 13:11:59 +0200651
652 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
653 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
654 tag[0] = 0x00;
655 tag[1] = 1 + strlen(str) + 1;
656 tag[2] = data[1];
657 memcpy(tag + 3, str, strlen(str) + 1);
658 data += 2;
659 len -= 2;
660 }
661 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
662 return nmsg;
663}
664
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200665static struct msgb *ipa_bts_id_ack(void)
666{
667 struct msgb *nmsg2;
668
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200669 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200670 if (!nmsg2)
671 return NULL;
672
673 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200674 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200675
676 return nmsg2;
677}
678
Harald Welte51de9ca2013-06-30 20:13:16 +0200679static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up)
680{
681 struct e1inp_line *line = link->line;
682
Eric Wildb8242722019-06-21 13:52:06 +0200683 if (up) {
684 update_fd_settings(line, link->ofd->fd);
685 return;
686 }
Harald Welte51de9ca2013-06-30 20:13:16 +0200687
688 if (line->ops->sign_link_down)
689 line->ops->sign_link_down(line);
690}
691
Harald Welte783715b2014-08-17 18:24:51 +0200692/* handle incoming message to BTS, check if it is an IPA CCM, and if yes,
693 * handle it accordingly (PING/PONG/ID_REQ/ID_RESP/ID_ACK) */
694int ipaccess_bts_handle_ccm(struct ipa_client_conn *link,
695 struct ipaccess_unit *dev, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200696{
697 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200698 struct msgb *rmsg;
699 int ret = 0;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200700
701 /* special handling for IPA CCM. */
702 if (hh->proto == IPAC_PROTO_IPACCESS) {
703 uint8_t msg_type = *(msg->l2h);
704
705 /* ping, pong and acknowledgment cases. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200706 ret = ipa_ccm_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200707 if (ret < 0)
708 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200709
710 /* this is a request for identification from the BSC. */
711 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200712 uint8_t *data = msgb_l2(msg);
713 int len = msgb_l2len(msg);
Andreas Eversbergf422a752014-01-21 14:54:41 +0100714 int trx_nr = 0;
715
716 if (link->ofd->priv_nr >= E1INP_SIGN_RSL)
717 trx_nr = link->ofd->priv_nr - E1INP_SIGN_RSL;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200718
Max71393522018-01-08 15:42:00 +0100719 LOGP(DLINP, LOGL_NOTICE, "received ID get from %u/%u/%u\n",
Pau Espin Pedroldd95eb62018-10-02 20:05:28 +0200720 dev->site_id, dev->bts_id, trx_nr);
Harald Weltee416e2e2017-05-26 13:11:59 +0200721 rmsg = ipa_bts_id_resp(dev, data + 1, len - 1, trx_nr);
Harald Welteb65f58f2014-08-20 22:04:11 +0200722 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200723 if (ret != rmsg->len) {
724 LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP "
725 "message. Reason: %s\n", strerror(errno));
726 goto err_rmsg;
727 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200728 msgb_free(rmsg);
729
730 /* send ID_ACK. */
731 rmsg = ipa_bts_id_ack();
Harald Welteb65f58f2014-08-20 22:04:11 +0200732 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200733 if (ret != rmsg->len) {
734 LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK "
735 "message. Reason: %s\n", strerror(errno));
736 goto err_rmsg;
737 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200738 msgb_free(rmsg);
Harald Welte783715b2014-08-17 18:24:51 +0200739 }
740 return 1;
741 }
Harald Welte6eddd472013-06-30 20:18:53 +0200742
Harald Welte783715b2014-08-17 18:24:51 +0200743 return 0;
744
745err_rmsg:
746 msgb_free(rmsg);
747err:
748 ipa_client_conn_close(link);
749 return -1;
750}
751
752static int ipaccess_bts_read_cb(struct ipa_client_conn *link, struct msgb *msg)
753{
754 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
755 struct e1inp_ts *e1i_ts = NULL;
756 struct e1inp_sign_link *sign_link;
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200757 uint8_t msg_type = *(msg->l2h);
Harald Welte783715b2014-08-17 18:24:51 +0200758 int ret = 0;
759
760 /* special handling for IPA CCM. */
761 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200762 /* this is a request for identification from the BSC. */
763 if (msg_type == IPAC_MSGT_ID_GET) {
764 if (!link->line->ops->sign_link_up) {
765 LOGP(DLINP, LOGL_ERROR,
766 "Unable to set signal link, "
767 "closing socket.\n");
Harald Welte783715b2014-08-17 18:24:51 +0200768 goto err;
769 }
770 }
771 }
772
773 /* core CCM handling */
774 ret = ipaccess_bts_handle_ccm(link, link->line->ops->cfg.ipa.dev, msg);
775 if (ret < 0)
776 goto err;
777
778 if (ret == 1 && hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200779 if (msg_type == IPAC_MSGT_ID_GET) {
Harald Welte6eddd472013-06-30 20:18:53 +0200780 sign_link = link->line->ops->sign_link_up(msg,
781 link->line,
782 link->ofd->priv_nr);
783 if (sign_link == NULL) {
784 LOGP(DLINP, LOGL_ERROR,
785 "Unable to set signal link, "
786 "closing socket.\n");
Harald Welte6eddd472013-06-30 20:18:53 +0200787 goto err;
788 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200789 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200790 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200791 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200792 } else if (link->port == IPA_TCP_PORT_OML)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200793 e1i_ts = e1inp_line_ipa_oml_ts(link->line);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200794 else if (link->port == IPA_TCP_PORT_RSL)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200795 e1i_ts = e1inp_line_ipa_rsl_ts(link->line, link->ofd->priv_nr - E1INP_SIGN_RSL);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200796
Pablo Neira Ayusob9487012013-07-05 14:38:43 +0200797 OSMO_ASSERT(e1i_ts != NULL);
798
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200799 if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
800 LOGP(DLINP, LOGL_ERROR, "Signalling link not initialized. Discarding."
801 " port=%u msg_type=%u\n", link->port, msg_type);
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200802 goto err;
803 }
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +0200804
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200805 /* look up for some existing signaling link. */
806 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
807 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200808 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200809 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200810 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200811 }
812 msg->dst = sign_link;
813
814 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200815 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200816 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200817 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200818 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200819 }
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200820 return link->line->ops->sign_link(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200821
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200822err:
Harald Welte783715b2014-08-17 18:24:51 +0200823 ipa_client_conn_close(link);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200824 msgb_free(msg);
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +0200825 return -EBADF;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200826}
827
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200828struct ipaccess_line {
829 int line_already_initialized;
830};
831
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200832static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200833{
834 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200835 struct ipaccess_line *il;
836
837 if (!line->driver_data)
838 line->driver_data = talloc_zero(line, struct ipaccess_line);
839
840 if (!line->driver_data) {
841 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
842 return -ENOMEM;
843 }
844 il = line->driver_data;
845
846 /* We only initialize this line once. */
847 if (il->line_already_initialized)
848 return 0;
849
850 il->line_already_initialized = 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200851
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200852 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200853 case E1INP_LINE_R_BSC: {
854 struct ipa_server_link *oml_link, *rsl_link;
Max4c4a1c22016-12-30 15:10:47 +0100855 const char *ipa = e1inp_ipa_get_bind_addr();
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200856
Max4c4a1c22016-12-30 15:10:47 +0100857 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode on %s "
858 "with OML %u and RSL %u TCP ports\n", ipa,
859 IPA_TCP_PORT_OML, IPA_TCP_PORT_RSL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200860
Max4c4a1c22016-12-30 15:10:47 +0100861 oml_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100862 IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200863 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200864 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200865 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200866 "BSC link: %s\n", strerror(errno));
867 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200868 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200869 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200870 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200871 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200872 ipa_server_link_destroy(oml_link);
873 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200874 }
Max4c4a1c22016-12-30 15:10:47 +0100875 rsl_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100876 IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200877 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200878 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200879 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200880 "BSC link: %s\n", strerror(errno));
881 return -ENOMEM;
882 }
883 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200884 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200885 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200886 ipa_server_link_destroy(rsl_link);
887 return -EIO;
888 }
889 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200890 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200891 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200892 case E1INP_LINE_R_BTS: {
Harald Welte84f67b22013-06-30 13:13:59 +0200893 struct ipa_client_conn *link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200894
Max4c4a1c22016-12-30 15:10:47 +0100895 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode, "
896 "OML connecting to %s:%u\n", line->ops->cfg.ipa.addr,
897 IPA_TCP_PORT_OML);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200898
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +0100899 link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200900 e1inp_line_ipa_oml_ts(line),
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +0200901 E1INP_SIGN_OML,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +0100902 NULL, 0,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200903 line->ops->cfg.ipa.addr,
904 IPA_TCP_PORT_OML,
Harald Welte51de9ca2013-06-30 20:13:16 +0200905 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +0200906 ipaccess_bts_read_cb,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200907 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +0200908 line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200909 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200910 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200911 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200912 return -ENOMEM;
913 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200914 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200915 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200916 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200917 ipa_client_conn_close(link);
918 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200919 return -EIO;
920 }
921 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200922 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200923 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200924 default:
925 break;
926 }
927 return ret;
928}
929
Andreas Eversbergf422a752014-01-21 14:54:41 +0100930
931/* backwards compatibility */
Harald Welte84f67b22013-06-30 13:13:59 +0200932int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line,
933 const char *rem_addr, uint16_t rem_port)
934{
Andreas Eversbergf422a752014-01-21 14:54:41 +0100935 return e1inp_ipa_bts_rsl_connect_n(line, rem_addr, rem_port, 0);
936}
937
938int e1inp_ipa_bts_rsl_connect_n(struct e1inp_line *line,
939 const char *rem_addr, uint16_t rem_port,
940 uint8_t trx_nr)
941{
Harald Welte84f67b22013-06-30 13:13:59 +0200942 struct ipa_client_conn *rsl_link;
943
Andreas Eversbergf422a752014-01-21 14:54:41 +0100944 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
945 LOGP(DLINP, LOGL_ERROR, "cannot create RSL BTS link: "
946 "trx_nr (%d) out of range\n", trx_nr);
947 return -EINVAL;
948 }
Andreas Eversbergf422a752014-01-21 14:54:41 +0100949
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +0100950 rsl_link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200951 e1inp_line_ipa_rsl_ts(line, trx_nr),
Andreas Eversbergf422a752014-01-21 14:54:41 +0100952 E1INP_SIGN_RSL+trx_nr,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +0100953 NULL, 0,
Harald Welte84f67b22013-06-30 13:13:59 +0200954 rem_addr, rem_port,
Harald Welte51de9ca2013-06-30 20:13:16 +0200955 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +0200956 ipaccess_bts_read_cb,
Harald Welte84f67b22013-06-30 13:13:59 +0200957 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +0200958 line);
Harald Welte84f67b22013-06-30 13:13:59 +0200959 if (rsl_link == NULL) {
960 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
961 "BTS link: %s\n", strerror(errno));
962 return -ENOMEM;
963 }
964 if (ipa_client_conn_open(rsl_link) < 0) {
965 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
966 strerror(errno));
967 ipa_client_conn_close(rsl_link);
968 ipa_client_conn_destroy(rsl_link);
969 return -EIO;
970 }
971 return 0;
972}
973
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200974void e1inp_ipaccess_init(void)
975{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200976 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200977 e1inp_driver_register(&ipaccess_driver);
978}
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100979
980void e1inp_ipa_set_bind_addr(const char *ip_bind_addr)
981{
982 talloc_free((char*)ipaccess_driver.bind_addr);
983 ipaccess_driver.bind_addr = NULL;
984
985 if (ip_bind_addr)
986 ipaccess_driver.bind_addr = talloc_strdup(tall_ipa_ctx,
987 ip_bind_addr);
988}
989
990const char *e1inp_ipa_get_bind_addr(void)
991{
992 return ipaccess_driver.bind_addr?
993 ipaccess_driver.bind_addr
994 : "0.0.0.0";
995}