blob: 08b34a4204dd388698c977f5e8b954eda07c0a80 [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
Eric Wildef1f3272019-07-10 18:10:31 +020062static inline struct e1inp_ts *ipaccess_line_ts(struct osmo_fd *bfd, struct e1inp_line *line)
63{
64 if (bfd->priv_nr == E1INP_SIGN_OML)
65 return e1inp_line_ipa_oml_ts(line);
66 else
67 return e1inp_line_ipa_rsl_ts(line, bfd->priv_nr - E1INP_SIGN_RSL);
68}
69
70static inline void ipaccess_keepalive_fsm_cleanup(struct e1inp_ts *e1i_ts)
71{
72 struct osmo_fsm_inst *ka_fsm;
73
74 ka_fsm = e1i_ts->driver.ipaccess.ka_fsm;
75 if (ka_fsm) {
76 ipa_keepalive_fsm_stop(ka_fsm);
77 e1i_ts->driver.ipaccess.ka_fsm = NULL;
78 }
79}
80
Harald Welte10b41302013-06-30 14:05:49 +020081static int ipaccess_drop(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020082{
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +020083 int ret = 1;
Eric Wildef1f3272019-07-10 18:10:31 +020084 struct e1inp_ts *e1i_ts = ipaccess_line_ts(bfd, line);
85
86 ipaccess_keepalive_fsm_cleanup(e1i_ts);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020087
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020088 /* Error case: we did not see any ID_RESP yet for this socket. */
89 if (bfd->fd != -1) {
Harald Weltecc2241b2011-07-19 16:06:06 +020090 LOGP(DLINP, LOGL_ERROR, "Forcing socket shutdown with "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020091 "no signal link set\n");
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +020092 osmo_fd_unregister(bfd);
93 close(bfd->fd);
94 bfd->fd = -1;
Pau Espin Pedrolb8ea0ff2020-07-14 13:28:27 +020095 /* This is BSC code, ipaccess_drop() is only called for
96 accepted() sockets, hence the bfd holds a reference to
97 e1inp_line in ->data that needs to be released */
98 OSMO_ASSERT(bfd->data == line);
99 bfd->data = NULL;
100 e1inp_line_put2(line, "ipa_bfd");
101
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200102 ret = -ENOENT;
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +0200103 }
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200104
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200105 msgb_free(e1i_ts->pending_msg);
106 e1i_ts->pending_msg = NULL;
107
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200108 /* e1inp_sign_link_destroy releases the socket descriptors for us. */
109 line->ops->sign_link_down(line);
110
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200111 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200112}
113
Eric Wildef1f3272019-07-10 18:10:31 +0200114static void ipa_bsc_keepalive_write_server_cb(struct osmo_fsm_inst *fi, void *conn, struct msgb *msg)
115{
116 struct osmo_fd *bfd = (struct osmo_fd *)conn;
117 write(bfd->fd, msg->data, msg->len);
118 msgb_free(msg);
119}
120
121static int ipa_bsc_keepalive_timeout_cb(struct osmo_fsm_inst *fi, void *data)
122{
123 struct osmo_fd *bfd = (struct osmo_fd *)data;
124
125 if (bfd->fd == -1)
126 return 1;
127
128 ipaccess_drop(bfd, (struct e1inp_line *)bfd->data);
129 return 1;
130}
131
132static void ipaccess_bsc_keepalive_fsm_alloc(struct e1inp_ts *e1i_ts, struct osmo_fd *bfd, const char *id)
133{
134 struct e1inp_line *line = e1i_ts->line;
135 struct osmo_fsm_inst *ka_fsm;
136
137 ipaccess_keepalive_fsm_cleanup(e1i_ts);
138 if (!line->ipa_kap)
139 return;
140
141 ka_fsm = ipa_generic_conn_alloc_keepalive_fsm(tall_ipa_ctx, bfd, line->ipa_kap, id);
142 e1i_ts->driver.ipaccess.ka_fsm = ka_fsm;
143 if (!ka_fsm)
144 return;
145
146 ipa_keepalive_fsm_set_timeout_cb(ka_fsm, ipa_bsc_keepalive_timeout_cb);
147 ipa_keepalive_fsm_set_send_cb(ka_fsm, ipa_bsc_keepalive_write_server_cb);
148 ipa_keepalive_fsm_start(ka_fsm);
149}
150
151static void ipa_bts_keepalive_write_client_cb(struct osmo_fsm_inst *fi, void *conn, struct msgb *msg) {
152 struct ipa_client_conn *link = (struct ipa_client_conn *)conn;
153 int ret = 0;
154
155 ret = ipa_send(link->ofd->fd, msg->data, msg->len);
156 if (ret != msg->len) {
157 LOGP(DLINP, LOGL_ERROR, "cannot send message. Reason: %s\n", strerror(errno));
158 }
159 msgb_free(msg);
160}
161
162static void update_fd_settings(struct e1inp_line *line, int fd);
163static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up);
164
165static int ipa_bts_keepalive_timeout_cb(struct osmo_fsm_inst *fi, void *conn) {
166 ipaccess_bts_updown_cb(conn, false);
167 return 1;
168}
169
170static void ipaccess_bts_keepalive_fsm_alloc(struct e1inp_ts *e1i_ts, struct ipa_client_conn *client, const char *id)
171{
172 struct e1inp_line *line = e1i_ts->line;
173 struct osmo_fsm_inst *ka_fsm;
174
175 ipaccess_keepalive_fsm_cleanup(e1i_ts);
176 if (!line->ipa_kap)
177 return;
178
179 ka_fsm = ipa_client_conn_alloc_keepalive_fsm(client, line->ipa_kap, id);
180 e1i_ts->driver.ipaccess.ka_fsm = ka_fsm;
181 if (!ka_fsm)
182 return;
183
184 ipa_keepalive_fsm_set_timeout_cb(ka_fsm, ipa_bts_keepalive_timeout_cb);
185 ipa_keepalive_fsm_set_send_cb(ka_fsm, ipa_bts_keepalive_write_client_cb);
186}
187
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200188/* Returns -1 on error, and 0 or 1 on success. If -1 or 1 is returned, line has
189 * been released and should not be used anymore by the caller. */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200190static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
191 struct osmo_fd *bfd)
192{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200193 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200194 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200195 struct ipaccess_unit unit_data = {};
196 struct e1inp_sign_link *sign_link;
197 char *unitid;
198 int len, ret;
Eric Wildef1f3272019-07-10 18:10:31 +0200199 struct e1inp_ts *e1i_ts;
200 struct osmo_fsm_inst *ka_fsm;
201
202 /* peek the pong for our keepalive fsm */
203 e1i_ts = ipaccess_line_ts(bfd, line);
204 ka_fsm = e1i_ts->driver.ipaccess.ka_fsm;
205 if (ka_fsm && msg_type == IPAC_MSGT_PONG)
206 ipa_keepalive_fsm_pong_received(ka_fsm);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200207
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200208 /* Handle IPA PING, PONG and ID_ACK messages. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200209 ret = ipa_ccm_rcvmsg_base(msg, bfd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200210 switch(ret) {
211 case -1:
212 /* error in IPA control message handling */
213 goto err;
214 case 1:
215 /* this is an IPA control message, skip further processing */
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200216 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200217 case 0:
218 /* this is not an IPA control message, continue */
219 break;
220 default:
221 LOGP(DLINP, LOGL_ERROR, "Unexpected return from "
Harald Welteb65f58f2014-08-20 22:04:11 +0200222 "ipa_ccm_rcvmsg_base "
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200223 "(ret=%d)\n", ret);
224 goto err;
225 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200226
227 switch (msg_type) {
228 case IPAC_MSGT_ID_RESP:
Pau Espin Pedrol30e80d52020-07-14 13:23:05 +0200229 DEBUGP(DLMI, "ID_RESP ");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200230 /* parse tags, search for Unit ID */
Harald Welte82eb55e2018-08-01 13:22:55 +0200231 ret = ipa_ccm_id_resp_parse(&tlvp, (const uint8_t *)msg->l2h+1, msgb_l2len(msg)-1);
Pau Espin Pedrol30e80d52020-07-14 13:23:05 +0200232 DEBUGPC(DLMI, "\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200233 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200234 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200235 "with malformed TLVs\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200236 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200237 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200238 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200239 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200240 "without unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200241 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200242
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200243 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200244 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200245 if (len < 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200246 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200247 "with too small unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200248 goto err;
249 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200250 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
251 unitid[len - 1] = '\0';
Vadim Yanitskiy1c94f6a2019-12-02 02:16:07 +0700252 ret = ipa_parse_unitid(unitid, &unit_data);
253 if (ret) {
254 LOGP(DLINP, LOGL_ERROR, "Failed to parse unit ID '%s'\n", unitid);
255 goto err;
256 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200257
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200258 if (!line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200259 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200260 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200261 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200262 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200263 /* the BSC creates the new sign links at this stage. */
264 if (bfd->priv_nr == E1INP_SIGN_OML) {
265 sign_link =
266 line->ops->sign_link_up(&unit_data, line,
267 E1INP_SIGN_OML);
268 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200269 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200270 "Unable to set signal link, "
271 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200272 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200273 }
Eric Wildef1f3272019-07-10 18:10:31 +0200274
275 ipaccess_bsc_keepalive_fsm_alloc(e1i_ts, bfd, "oml_bsc_to_bts");
276
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200277 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200278 struct e1inp_ts *ts;
Eric Wildef1f3272019-07-10 18:10:31 +0200279 struct osmo_fd *newbfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200280 struct e1inp_line *new_line;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200281
282 sign_link =
283 line->ops->sign_link_up(&unit_data, line,
284 E1INP_SIGN_RSL);
285 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200286 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200287 "Unable to set signal link, "
288 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200289 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200290 }
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200291 /* this is a bugtrap, the BSC should be using the
292 * virtual E1 line used by OML for this RSL link. */
293 if (sign_link->ts->line == line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200294 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200295 "Fix your BSC, you should use the "
296 "E1 line used by the OML link for "
297 "your RSL link.\n");
298 return 0;
299 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200300 /* Finally, we know which OML link is associated with
301 * this RSL link, attach it to this socket. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200302 bfd->data = new_line = sign_link->ts->line;
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200303 e1inp_line_get2(new_line, "ipa_bfd");
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200304 ts = e1inp_line_ipa_rsl_ts(new_line, unit_data.trx_id);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200305 newbfd = &ts->driver.ipaccess.fd;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200306
307 /* get rid of our old temporary bfd */
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200308 /* preserve 'newbfd->when' flags potentially set by sign_link_up() */
Pau Espin Pedrol67902bb2018-08-23 14:35:08 +0200309 osmo_fd_setup(newbfd, bfd->fd, newbfd->when | bfd->when, bfd->cb,
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200310 bfd->data, E1INP_SIGN_RSL + unit_data.trx_id);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200311 osmo_fd_unregister(bfd);
312 bfd->fd = -1;
Harald Welteae3a9932016-11-26 09:25:23 +0100313 ret = osmo_fd_register(newbfd);
314 if (ret < 0) {
315 LOGP(DLINP, LOGL_ERROR,
316 "could not register FD\n");
317 goto err;
318 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200319 /* now we can release the dummy RSL line. */
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200320 e1inp_line_put2(line, "ipa_bfd");
Eric Wildef1f3272019-07-10 18:10:31 +0200321
322 e1i_ts = ipaccess_line_ts(newbfd, new_line);
323 ipaccess_bsc_keepalive_fsm_alloc(e1i_ts, newbfd, "rsl_bsc_to_bts");
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200324 return 1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200325 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200326 break;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200327 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200328 LOGP(DLINP, LOGL_ERROR, "Unknown IPA message type\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200329 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200330 }
331 return 0;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200332err:
333 osmo_fd_unregister(bfd);
Harald Welteb4a7db02019-07-21 11:51:56 +0200334 if (bfd->fd != -1) {
335 close(bfd->fd);
336 bfd->fd = -1;
337 }
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200338 e1inp_line_put2(line, "ipa_bfd");
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200339 return -1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200340}
341
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200342/* Returns -EBADF if bfd cannot be used by the caller anymore after return. */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200343static int handle_ts1_read(struct osmo_fd *bfd)
344{
345 struct e1inp_line *line = bfd->data;
346 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200347 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200348 struct e1inp_sign_link *link;
349 struct ipaccess_head *hh;
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200350 struct msgb *msg = NULL;
Maxc9fa25e2017-01-09 13:23:15 +0100351 int ret, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200352
Eric Wildef1f3272019-07-10 18:10:31 +0200353 e1i_ts = ipaccess_line_ts(bfd, line);
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200354 ret = ipa_msg_recv_buffered(bfd->fd, &msg, &e1i_ts->pending_msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200355 if (ret < 0) {
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200356 if (ret == -EAGAIN)
357 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200358 LOGP(DLINP, LOGL_NOTICE, "Sign link problems, "
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200359 "closing socket. Reason: %s\n", strerror(-ret));
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200360 goto err;
361 } else if (ret == 0) {
362 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
363 goto err;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200364 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200365 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200366
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200367 hh = (struct ipaccess_head *) msg->data;
368 if (hh->proto == IPAC_PROTO_IPACCESS) {
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200369 ret = ipaccess_rcvmsg(line, msg, bfd);
370 /* BIG FAT WARNING: bfd might no longer exist here (ret != 0),
371 * since ipaccess_rcvmsg() might have free'd it !!! */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200372 msgb_free(msg);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200373 return ret != 0 ? -EBADF : 0;
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100374 } else if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
375 /* this sign link is not know yet.. complain. */
376 LOGP(DLINP, LOGL_ERROR, "Timeslot is not configured.\n");
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100377 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200378 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200379
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200380 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
381 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200382 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200383 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200384 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200385 }
386 msg->dst = link;
387
388 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200389 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200390 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200391 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200392 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200393 }
Maxc9fa25e2017-01-09 13:23:15 +0100394 rc = e1i_ts->line->ops->sign_link(msg);
395 if (rc < 0) {
Pablo Neira Ayusoa49c24d2012-10-16 11:24:08 +0200396 /* Don't close the signalling link if the upper layers report
397 * an error, that's too strict. BTW, the signalling layer is
398 * resposible for releasing the message.
399 */
Harald Weltecc2241b2011-07-19 16:06:06 +0200400 LOGP(DLINP, LOGL_ERROR, "Bad signalling message,"
Maxc9fa25e2017-01-09 13:23:15 +0100401 " sign_link returned error: %s.\n", strerror(-rc));
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200402 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200403
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200404 return rc;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200405err_msg:
406 msgb_free(msg);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200407err:
Harald Welte10b41302013-06-30 14:05:49 +0200408 ipaccess_drop(bfd, line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200409 return -EBADF;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200410}
411
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200412static int ts_want_write(struct e1inp_ts *e1i_ts)
413{
414 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
415
416 return 0;
417}
418
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200419static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200420{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200421 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200422 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Eric Wildef1f3272019-07-10 18:10:31 +0200423 struct e1inp_line *line = e1i_ts->line;
424
425 /* line might not exist if != bsc||bts */
426 if (line) {
427 /* depending on caller the fsm might be dead */
428 struct osmo_fsm_inst* ka_fsm = ipaccess_line_ts(bfd, line)->driver.ipaccess.ka_fsm;
429 if (ka_fsm)
430 ipa_keepalive_fsm_stop(ka_fsm);
431
432 }
433
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100434 return e1inp_close_socket(e1i_ts, sign_link, bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200435}
436
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200437static void timeout_ts1_write(void *data)
438{
439 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
440
441 /* trigger write of ts1, due to tx delay timer */
442 ts_want_write(e1i_ts);
443}
444
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200445static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200446{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200447 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200448 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200449 struct e1inp_sign_link *sign_link;
450 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200451 int ret;
452
Eric Wildef1f3272019-07-10 18:10:31 +0200453 e1i_ts = ipaccess_line_ts(bfd, line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200454 bfd->when &= ~BSC_FD_WRITE;
455
456 /* get the next msg for this timeslot */
457 msg = e1inp_tx_ts(e1i_ts, &sign_link);
458 if (!msg) {
459 /* no message after tx delay timer */
460 return 0;
461 }
462
463 switch (sign_link->type) {
464 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200465 case E1INP_SIGN_RSL:
Harald Welte46fc7e22014-08-18 19:04:26 +0200466 case E1INP_SIGN_OSMO:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200467 break;
468 default:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200469 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200470 ret = -EINVAL;
471 goto out;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200472 }
473
474 msg->l2h = msg->data;
Harald Welteb65f58f2014-08-20 22:04:11 +0200475 ipa_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200476
Harald Weltecc2241b2011-07-19 16:06:06 +0200477 DEBUGP(DLMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200478
479 ret = send(bfd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200480 if (ret != msg->len) {
481 LOGP(DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling "
482 "message. Reason: %s\n", strerror(errno));
483 goto err;
484 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200485
486 /* set tx delay timer for next event */
Pablo Neira Ayusob26f2fd2017-06-07 18:32:13 +0200487 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200488
489 /* Reducing this might break the nanoBTS 900 init. */
490 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
491
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200492out:
493 msgb_free(msg);
494 return ret;
495err:
Harald Welte10b41302013-06-30 14:05:49 +0200496 ipaccess_drop(bfd, line);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200497 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200498 return ret;
499}
500
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200501static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200502{
503 struct e1inp_line *line = bfd->data;
504
505 return __handle_ts1_write(bfd, line);
506}
507
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200508static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200509{
510 struct e1inp_line *line = link->line;
511
512 return __handle_ts1_write(link->ofd, line);
513}
514
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200515/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200516int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200517{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200518 int rc = 0;
519
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200520 if (what & BSC_FD_READ)
521 rc = handle_ts1_read(bfd);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200522 if (rc != -EBADF && (what & BSC_FD_WRITE))
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200523 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200524
525 return rc;
526}
527
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200528static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200529
530struct e1inp_driver ipaccess_driver = {
531 .name = "ipa",
532 .want_write = ts_want_write,
533 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200534 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200535 .default_delay = 0,
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100536 .has_keepalive = 1,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200537};
538
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100539static void update_fd_settings(struct e1inp_line *line, int fd)
540{
541 int ret;
542 int val;
543
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100544 if (line->keepalive_num_probes) {
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100545 /* Enable TCP keepalive to find out if the connection is gone */
546 val = 1;
547 ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
548 if (ret < 0)
549 LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive: %s\n",
550 strerror(errno));
551 else
552 LOGP(DLINP, LOGL_NOTICE, "Keepalive is set: %i\n", ret);
553
554#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
555 /* The following options are not portable! */
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100556 val = line->keepalive_idle_timeout > 0 ?
557 line->keepalive_idle_timeout :
558 DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100559 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
560 &val, sizeof(val));
561 if (ret < 0)
562 LOGP(DLINP, LOGL_NOTICE,
563 "Failed to set keepalive idle time: %s\n",
564 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100565 val = line->keepalive_probe_interval > -1 ?
566 line->keepalive_probe_interval :
567 DEFAULT_TCP_KEEPALIVE_INTERVAL;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100568 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
569 &val, sizeof(val));
570 if (ret < 0)
571 LOGP(DLINP, LOGL_NOTICE,
572 "Failed to set keepalive interval: %s\n",
573 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100574 val = line->keepalive_num_probes > 0 ?
575 line->keepalive_num_probes :
576 DEFAULT_TCP_KEEPALIVE_RETRY_COUNT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100577 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
578 &val, sizeof(val));
579 if (ret < 0)
580 LOGP(DLINP, LOGL_NOTICE,
581 "Failed to set keepalive count: %s\n",
582 strerror(errno));
Eric Wild6eb186c2019-06-21 15:26:25 +0200583#if defined(TCP_USER_TIMEOUT)
584 val = 1000 * line->keepalive_num_probes *
585 line->keepalive_probe_interval +
586 line->keepalive_idle_timeout;
587 ret = setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
588 &val, sizeof(val));
589 if (ret < 0)
590 LOGP(DLINP, LOGL_NOTICE,
591 "Failed to set user timoeut: %s\n",
592 strerror(errno));
593#endif
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100594#endif
Holger Hans Peter Freytherf465a4c2014-02-03 09:34:02 +0100595 }
Oliver Smith62725d02020-04-17 10:57:46 +0200596
597 val = 1;
598 ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
599 if (ret < 0)
600 LOGP(DLINP, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100601}
602
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200603/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200604static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200605{
606 int ret;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100607 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200608 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200609 struct e1inp_ts *e1i_ts;
610 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200611
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200612 /* clone virtual E1 line for this new OML link. */
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200613 line = e1inp_line_clone(tall_ipa_ctx, link->line, "ipa_bfd");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200614 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200615 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200616 return -ENOMEM;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200617 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200618
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200619 /* create virrtual E1 timeslots for signalling */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200620 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200621
622 /* initialize the fds */
623 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
624 line->ts[i].driver.ipaccess.fd.fd = -1;
625
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200626 e1i_ts = e1inp_line_ipa_oml_ts(line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200627
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200628 bfd = &e1i_ts->driver.ipaccess.fd;
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200629 osmo_fd_setup(bfd, fd, BSC_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_OML);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200630 ret = osmo_fd_register(bfd);
631 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200632 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200633 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200634 }
635
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100636 update_fd_settings(line, bfd->fd);
Daniel Willmann85980722014-01-09 14:30:55 +0100637
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200638 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200639 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200640 if (ret < 0) {
641 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
642 strerror(errno));
643 goto err_socket;
644 }
645 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200646
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200647err_socket:
648 osmo_fd_unregister(bfd);
649err_line:
650 close(bfd->fd);
651 bfd->fd = -1;
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200652 e1inp_line_put2(line, "ipa_bfd");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200653 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200654}
655
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200656static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200657{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200658 struct e1inp_line *line;
659 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200660 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200661 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200662
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200663 /* We don't know yet which OML link to associate it with. Thus, we
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200664 * allocate a temporary E1 line until we have received ID. */
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200665 line = e1inp_line_clone(tall_ipa_ctx, link->line, "ipa_bfd");
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200666 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200667 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200668 return -ENOMEM;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200669 }
670 /* initialize the fds */
671 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
672 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200673
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200674 /* we need this to initialize this in case to avoid crashes in case
675 * that the socket is closed before we've seen an ID_RESP. */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200676 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200677
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200678 e1i_ts = e1inp_line_ipa_rsl_ts(line, 0);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200679
680 bfd = &e1i_ts->driver.ipaccess.fd;
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200681 osmo_fd_setup(bfd, fd, BSC_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_RSL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200682 ret = osmo_fd_register(bfd);
683 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200684 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200685 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200686 }
687 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200688 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200689 if (ret < 0) {
690 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
691 strerror(errno));
692 goto err_socket;
693 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100694 update_fd_settings(line, bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200695 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200696
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200697err_socket:
698 osmo_fd_unregister(bfd);
699err_line:
700 close(bfd->fd);
701 bfd->fd = -1;
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200702 e1inp_line_put2(line, "ipa_bfd");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200703 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200704}
705
Harald Weltee416e2e2017-05-26 13:11:59 +0200706#define IPA_STRING_MAX 64
707
708static struct msgb *
Max71393522018-01-08 15:42:00 +0100709ipa_bts_id_resp(const struct ipaccess_unit *dev, uint8_t *data, int len, int trx_nr)
Harald Weltee416e2e2017-05-26 13:11:59 +0200710{
711 struct msgb *nmsg;
712 char str[IPA_STRING_MAX];
713 uint8_t *tag;
714
715 memset(str, 0, sizeof(str));
716
717 nmsg = ipa_msg_alloc(0);
718 if (!nmsg)
719 return NULL;
720
721 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
722 while (len) {
723 if (len < 2) {
724 LOGP(DLINP, LOGL_NOTICE,
725 "Short read of ipaccess tag\n");
726 msgb_free(nmsg);
727 return NULL;
728 }
729 switch (data[1]) {
730 case IPAC_IDTAG_UNIT:
731 snprintf(str, sizeof(str), "%u/%u/%u",
732 dev->site_id, dev->bts_id, trx_nr);
733 break;
734 case IPAC_IDTAG_MACADDR:
735 snprintf(str, sizeof(str),
736 "%02x:%02x:%02x:%02x:%02x:%02x",
737 dev->mac_addr[0], dev->mac_addr[1],
738 dev->mac_addr[2], dev->mac_addr[3],
739 dev->mac_addr[4], dev->mac_addr[5]);
740 break;
741 case IPAC_IDTAG_LOCATION1:
742 if (dev->location1)
Stefan Sperling961776a2018-12-06 13:09:48 +0100743 osmo_strlcpy(str, dev->location1, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200744 break;
745 case IPAC_IDTAG_LOCATION2:
746 if (dev->location2)
Stefan Sperling961776a2018-12-06 13:09:48 +0100747 osmo_strlcpy(str, dev->location2, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200748 break;
749 case IPAC_IDTAG_EQUIPVERS:
750 if (dev->equipvers)
Stefan Sperling961776a2018-12-06 13:09:48 +0100751 osmo_strlcpy(str, dev->equipvers, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200752 break;
753 case IPAC_IDTAG_SWVERSION:
754 if (dev->swversion)
Neels Hofmeyr4c57eef2018-07-26 17:28:24 +0200755 osmo_strlcpy(str, dev->swversion, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200756 break;
757 case IPAC_IDTAG_UNITNAME:
758 snprintf(str, sizeof(str),
759 "%s-%02x-%02x-%02x-%02x-%02x-%02x",
760 dev->unit_name,
761 dev->mac_addr[0], dev->mac_addr[1],
762 dev->mac_addr[2], dev->mac_addr[3],
763 dev->mac_addr[4], dev->mac_addr[5]);
764 break;
765 case IPAC_IDTAG_SERNR:
766 if (dev->serno)
Stefan Sperling961776a2018-12-06 13:09:48 +0100767 osmo_strlcpy(str, dev->serno, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200768 break;
769 default:
770 LOGP(DLINP, LOGL_NOTICE,
771 "Unknown ipaccess tag 0x%02x\n", *data);
772 msgb_free(nmsg);
773 return NULL;
774 }
Harald Weltee416e2e2017-05-26 13:11:59 +0200775
776 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
777 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
778 tag[0] = 0x00;
779 tag[1] = 1 + strlen(str) + 1;
780 tag[2] = data[1];
781 memcpy(tag + 3, str, strlen(str) + 1);
782 data += 2;
783 len -= 2;
784 }
785 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
786 return nmsg;
787}
788
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200789static struct msgb *ipa_bts_id_ack(void)
790{
791 struct msgb *nmsg2;
792
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200793 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200794 if (!nmsg2)
795 return NULL;
796
797 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200798 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200799
800 return nmsg2;
801}
802
Harald Welte51de9ca2013-06-30 20:13:16 +0200803static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up)
804{
805 struct e1inp_line *line = link->line;
806
Eric Wildef1f3272019-07-10 18:10:31 +0200807 if (up) {
808 struct osmo_fsm_inst *ka_fsm = ipaccess_line_ts(link->ofd, line)->driver.ipaccess.ka_fsm;
809
810 update_fd_settings(line, link->ofd->fd);
811 if (ka_fsm && line->ipa_kap)
812 ipa_keepalive_fsm_start(ka_fsm);
813 return;
814 }
Harald Welte51de9ca2013-06-30 20:13:16 +0200815
816 if (line->ops->sign_link_down)
817 line->ops->sign_link_down(line);
818}
819
Harald Welte783715b2014-08-17 18:24:51 +0200820/* handle incoming message to BTS, check if it is an IPA CCM, and if yes,
821 * handle it accordingly (PING/PONG/ID_REQ/ID_RESP/ID_ACK) */
822int ipaccess_bts_handle_ccm(struct ipa_client_conn *link,
823 struct ipaccess_unit *dev, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200824{
825 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200826 struct msgb *rmsg;
827 int ret = 0;
Eric Wildef1f3272019-07-10 18:10:31 +0200828 /* line might not exist if != bsc||bts */
829 struct e1inp_line *line = link->line;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200830
831 /* special handling for IPA CCM. */
832 if (hh->proto == IPAC_PROTO_IPACCESS) {
833 uint8_t msg_type = *(msg->l2h);
Eric Wildef1f3272019-07-10 18:10:31 +0200834 struct osmo_fsm_inst* ka_fsm = NULL;
835
836 /* peek the pong for our keepalive fsm */
837 if (line && msg_type == IPAC_MSGT_PONG) {
838 ka_fsm = ipaccess_line_ts(link->ofd, line)->driver.ipaccess.ka_fsm;
839 ipa_keepalive_fsm_pong_received(ka_fsm);
840 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200841
842 /* ping, pong and acknowledgment cases. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200843 ret = ipa_ccm_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200844 if (ret < 0)
845 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200846
847 /* this is a request for identification from the BSC. */
848 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200849 uint8_t *data = msgb_l2(msg);
850 int len = msgb_l2len(msg);
Andreas Eversbergf422a752014-01-21 14:54:41 +0100851 int trx_nr = 0;
852
853 if (link->ofd->priv_nr >= E1INP_SIGN_RSL)
854 trx_nr = link->ofd->priv_nr - E1INP_SIGN_RSL;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200855
Vadim Yanitskiy69ae2382019-12-02 02:42:43 +0700856 LOGP(DLINP, LOGL_NOTICE, "received ID_GET for unit ID %u/%u/%u\n",
Pau Espin Pedroldd95eb62018-10-02 20:05:28 +0200857 dev->site_id, dev->bts_id, trx_nr);
Harald Weltee416e2e2017-05-26 13:11:59 +0200858 rmsg = ipa_bts_id_resp(dev, data + 1, len - 1, trx_nr);
Harald Welteb65f58f2014-08-20 22:04:11 +0200859 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200860 if (ret != rmsg->len) {
861 LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP "
862 "message. Reason: %s\n", strerror(errno));
863 goto err_rmsg;
864 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200865 msgb_free(rmsg);
866
867 /* send ID_ACK. */
868 rmsg = ipa_bts_id_ack();
Harald Welteb65f58f2014-08-20 22:04:11 +0200869 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200870 if (ret != rmsg->len) {
871 LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK "
872 "message. Reason: %s\n", strerror(errno));
873 goto err_rmsg;
874 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200875 msgb_free(rmsg);
Harald Welte783715b2014-08-17 18:24:51 +0200876 }
877 return 1;
878 }
Harald Welte6eddd472013-06-30 20:18:53 +0200879
Harald Welte783715b2014-08-17 18:24:51 +0200880 return 0;
881
882err_rmsg:
883 msgb_free(rmsg);
884err:
885 ipa_client_conn_close(link);
886 return -1;
887}
888
889static int ipaccess_bts_read_cb(struct ipa_client_conn *link, struct msgb *msg)
890{
891 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
892 struct e1inp_ts *e1i_ts = NULL;
893 struct e1inp_sign_link *sign_link;
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200894 uint8_t msg_type = *(msg->l2h);
Harald Welte783715b2014-08-17 18:24:51 +0200895 int ret = 0;
896
897 /* special handling for IPA CCM. */
898 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200899 /* this is a request for identification from the BSC. */
900 if (msg_type == IPAC_MSGT_ID_GET) {
901 if (!link->line->ops->sign_link_up) {
902 LOGP(DLINP, LOGL_ERROR,
903 "Unable to set signal link, "
904 "closing socket.\n");
Harald Welte783715b2014-08-17 18:24:51 +0200905 goto err;
906 }
907 }
908 }
909
910 /* core CCM handling */
911 ret = ipaccess_bts_handle_ccm(link, link->line->ops->cfg.ipa.dev, msg);
912 if (ret < 0)
913 goto err;
914
915 if (ret == 1 && hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200916 if (msg_type == IPAC_MSGT_ID_GET) {
Harald Welte6eddd472013-06-30 20:18:53 +0200917 sign_link = link->line->ops->sign_link_up(msg,
918 link->line,
919 link->ofd->priv_nr);
920 if (sign_link == NULL) {
921 LOGP(DLINP, LOGL_ERROR,
922 "Unable to set signal link, "
923 "closing socket.\n");
Harald Welte6eddd472013-06-30 20:18:53 +0200924 goto err;
925 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200926 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200927 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200928 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200929 } else if (link->port == IPA_TCP_PORT_OML)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200930 e1i_ts = e1inp_line_ipa_oml_ts(link->line);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200931 else if (link->port == IPA_TCP_PORT_RSL)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200932 e1i_ts = e1inp_line_ipa_rsl_ts(link->line, link->ofd->priv_nr - E1INP_SIGN_RSL);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200933
Pablo Neira Ayusob9487012013-07-05 14:38:43 +0200934 OSMO_ASSERT(e1i_ts != NULL);
935
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200936 if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
937 LOGP(DLINP, LOGL_ERROR, "Signalling link not initialized. Discarding."
938 " port=%u msg_type=%u\n", link->port, msg_type);
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200939 goto err;
940 }
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +0200941
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200942 /* look up for some existing signaling link. */
943 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
944 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200945 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200946 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200947 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200948 }
949 msg->dst = sign_link;
950
951 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200952 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200953 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200954 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200955 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200956 }
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200957 return link->line->ops->sign_link(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200958
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200959err:
Harald Welte783715b2014-08-17 18:24:51 +0200960 ipa_client_conn_close(link);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200961 msgb_free(msg);
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +0200962 return -EBADF;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200963}
964
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200965struct ipaccess_line {
Oliver Smith65ae42c2020-01-22 11:24:52 +0100966 bool line_already_initialized;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200967};
968
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200969static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200970{
971 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200972 struct ipaccess_line *il;
973
974 if (!line->driver_data)
975 line->driver_data = talloc_zero(line, struct ipaccess_line);
976
977 if (!line->driver_data) {
978 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
979 return -ENOMEM;
980 }
981 il = line->driver_data;
982
983 /* We only initialize this line once. */
984 if (il->line_already_initialized)
985 return 0;
986
Oliver Smith65ae42c2020-01-22 11:24:52 +0100987 il->line_already_initialized = true;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200988
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200989 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200990 case E1INP_LINE_R_BSC: {
991 struct ipa_server_link *oml_link, *rsl_link;
Max4c4a1c22016-12-30 15:10:47 +0100992 const char *ipa = e1inp_ipa_get_bind_addr();
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200993
Max4c4a1c22016-12-30 15:10:47 +0100994 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode on %s "
995 "with OML %u and RSL %u TCP ports\n", ipa,
996 IPA_TCP_PORT_OML, IPA_TCP_PORT_RSL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200997
Max4c4a1c22016-12-30 15:10:47 +0100998 oml_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100999 IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +02001000 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001001 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001002 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001003 "BSC link: %s\n", strerror(errno));
1004 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +02001005 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001006 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001007 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001008 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001009 ipa_server_link_destroy(oml_link);
1010 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +02001011 }
Max4c4a1c22016-12-30 15:10:47 +01001012 rsl_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001013 IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +02001014 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001015 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001016 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001017 "BSC link: %s\n", strerror(errno));
1018 return -ENOMEM;
1019 }
1020 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001021 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001022 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001023 ipa_server_link_destroy(rsl_link);
1024 return -EIO;
1025 }
1026 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001027 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001028 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001029 case E1INP_LINE_R_BTS: {
Harald Welte84f67b22013-06-30 13:13:59 +02001030 struct ipa_client_conn *link;
Eric Wildef1f3272019-07-10 18:10:31 +02001031 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001032
Max4c4a1c22016-12-30 15:10:47 +01001033 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode, "
1034 "OML connecting to %s:%u\n", line->ops->cfg.ipa.addr,
1035 IPA_TCP_PORT_OML);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001036
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001037 link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +02001038 e1inp_line_ipa_oml_ts(line),
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +02001039 E1INP_SIGN_OML,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001040 NULL, 0,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001041 line->ops->cfg.ipa.addr,
1042 IPA_TCP_PORT_OML,
Harald Welte51de9ca2013-06-30 20:13:16 +02001043 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001044 ipaccess_bts_read_cb,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +02001045 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001046 line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001047 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001048 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001049 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001050 return -ENOMEM;
1051 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001052 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001053 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001054 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001055 ipa_client_conn_close(link);
1056 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001057 return -EIO;
1058 }
Eric Wildef1f3272019-07-10 18:10:31 +02001059
1060 e1i_ts = e1inp_line_ipa_oml_ts(line);
1061 ipaccess_bts_keepalive_fsm_alloc(e1i_ts, link, "oml_bts_to_bsc");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001062 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001063 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001064 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001065 default:
1066 break;
1067 }
1068 return ret;
1069}
1070
Andreas Eversbergf422a752014-01-21 14:54:41 +01001071
1072/* backwards compatibility */
Harald Welte84f67b22013-06-30 13:13:59 +02001073int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line,
1074 const char *rem_addr, uint16_t rem_port)
1075{
Andreas Eversbergf422a752014-01-21 14:54:41 +01001076 return e1inp_ipa_bts_rsl_connect_n(line, rem_addr, rem_port, 0);
1077}
1078
1079int e1inp_ipa_bts_rsl_connect_n(struct e1inp_line *line,
1080 const char *rem_addr, uint16_t rem_port,
1081 uint8_t trx_nr)
1082{
Harald Welte84f67b22013-06-30 13:13:59 +02001083 struct ipa_client_conn *rsl_link;
Eric Wildef1f3272019-07-10 18:10:31 +02001084 struct e1inp_ts *e1i_ts = e1inp_line_ipa_rsl_ts(line, trx_nr);
Harald Welte84f67b22013-06-30 13:13:59 +02001085
Andreas Eversbergf422a752014-01-21 14:54:41 +01001086 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
1087 LOGP(DLINP, LOGL_ERROR, "cannot create RSL BTS link: "
1088 "trx_nr (%d) out of range\n", trx_nr);
1089 return -EINVAL;
1090 }
Andreas Eversbergf422a752014-01-21 14:54:41 +01001091
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001092 rsl_link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +02001093 e1inp_line_ipa_rsl_ts(line, trx_nr),
Andreas Eversbergf422a752014-01-21 14:54:41 +01001094 E1INP_SIGN_RSL+trx_nr,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001095 NULL, 0,
Harald Welte84f67b22013-06-30 13:13:59 +02001096 rem_addr, rem_port,
Harald Welte51de9ca2013-06-30 20:13:16 +02001097 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001098 ipaccess_bts_read_cb,
Harald Welte84f67b22013-06-30 13:13:59 +02001099 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001100 line);
Harald Welte84f67b22013-06-30 13:13:59 +02001101 if (rsl_link == NULL) {
1102 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
1103 "BTS link: %s\n", strerror(errno));
1104 return -ENOMEM;
1105 }
1106 if (ipa_client_conn_open(rsl_link) < 0) {
1107 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
1108 strerror(errno));
1109 ipa_client_conn_close(rsl_link);
1110 ipa_client_conn_destroy(rsl_link);
1111 return -EIO;
1112 }
Eric Wildef1f3272019-07-10 18:10:31 +02001113
1114 ipaccess_bts_keepalive_fsm_alloc(e1i_ts, rsl_link, "rsl_bts_to_bsc");
Harald Welte84f67b22013-06-30 13:13:59 +02001115 return 0;
1116}
1117
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001118void e1inp_ipaccess_init(void)
1119{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +02001120 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001121 e1inp_driver_register(&ipaccess_driver);
1122}
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001123
1124void e1inp_ipa_set_bind_addr(const char *ip_bind_addr)
1125{
1126 talloc_free((char*)ipaccess_driver.bind_addr);
1127 ipaccess_driver.bind_addr = NULL;
1128
1129 if (ip_bind_addr)
1130 ipaccess_driver.bind_addr = talloc_strdup(tall_ipa_ctx,
1131 ip_bind_addr);
1132}
1133
1134const char *e1inp_ipa_get_bind_addr(void)
1135{
1136 return ipaccess_driver.bind_addr?
1137 ipaccess_driver.bind_addr
1138 : "0.0.0.0";
1139}