blob: 418606e6bf7f48fae4e681549d6914b501878dab [file] [log] [blame]
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001/* OpenBSC Abis input driver for ip.access */
2
Harald Welte41547552021-04-28 18:23:21 +02003/* (C) 2009-2021 by Harald Welte <laforge@gnumonks.org>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02004 * (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>
Philipp Maierf5f31d32021-12-06 16:46:40 +010053#include <osmocom/core/stats_tcp.h>
Daniel Willmann8e380942022-12-13 19:15:15 +010054#include <osmocom/core/fsm.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020055
Harald Welte41547552021-04-28 18:23:21 +020056/* global parameters of IPA input driver */
57struct ipa_pars g_e1inp_ipaccess_pars;
58
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +020059static void *tall_ipa_ctx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020060
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020061#define TS1_ALLOC_SIZE 900
62
Daniel Willmann85980722014-01-09 14:30:55 +010063#define DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT 30
64#define DEFAULT_TCP_KEEPALIVE_INTERVAL 3
65#define DEFAULT_TCP_KEEPALIVE_RETRY_COUNT 10
66
Eric Wildef1f3272019-07-10 18:10:31 +020067static inline struct e1inp_ts *ipaccess_line_ts(struct osmo_fd *bfd, struct e1inp_line *line)
68{
69 if (bfd->priv_nr == E1INP_SIGN_OML)
70 return e1inp_line_ipa_oml_ts(line);
71 else
72 return e1inp_line_ipa_rsl_ts(line, bfd->priv_nr - E1INP_SIGN_RSL);
73}
74
75static inline void ipaccess_keepalive_fsm_cleanup(struct e1inp_ts *e1i_ts)
76{
77 struct osmo_fsm_inst *ka_fsm;
78
79 ka_fsm = e1i_ts->driver.ipaccess.ka_fsm;
80 if (ka_fsm) {
Daniel Willmann8e380942022-12-13 19:15:15 +010081 osmo_fsm_inst_term(ka_fsm, OSMO_FSM_TERM_REQUEST, NULL);
Eric Wildef1f3272019-07-10 18:10:31 +020082 e1i_ts->driver.ipaccess.ka_fsm = NULL;
83 }
84}
85
Harald Welte10b41302013-06-30 14:05:49 +020086static int ipaccess_drop(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020087{
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +020088 int ret = 1;
Eric Wildef1f3272019-07-10 18:10:31 +020089 struct e1inp_ts *e1i_ts = ipaccess_line_ts(bfd, line);
Pau Espin Pedroldeb5c4f2020-07-31 12:55:01 +020090 e1inp_line_get2(line, __func__);
Eric Wildef1f3272019-07-10 18:10:31 +020091
92 ipaccess_keepalive_fsm_cleanup(e1i_ts);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020093
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020094 /* Error case: we did not see any ID_RESP yet for this socket. */
95 if (bfd->fd != -1) {
Pau Espin Pedrol91314c12020-11-23 14:45:03 +010096 LOGPITS(e1i_ts, DLINP, LOGL_NOTICE, "Forcing socket shutdown\n");
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +020097 osmo_fd_unregister(bfd);
98 close(bfd->fd);
99 bfd->fd = -1;
Pau Espin Pedrol9af069d2020-11-23 14:54:00 +0100100 switch (line->ops->cfg.ipa.role) {
101 case E1INP_LINE_R_BSC:
102 /* This is BSC code, ipaccess_drop() is only called for
103 accepted() sockets, hence the bfd holds a reference to
104 e1inp_line in ->data that needs to be released */
105 OSMO_ASSERT(bfd->data == line);
106 bfd->data = NULL;
107 e1inp_line_put2(line, "ipa_bfd");
108 break;
109 case E1INP_LINE_R_BTS:
110 /* BTS code: bfd->data contains pointer to struct
111 * ipa_client_conn. Leave it alive so it reconnects.
112 */
113 break;
114 default:
115 break;
116 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200117 ret = -ENOENT;
Pau Espin Pedrol91314c12020-11-23 14:45:03 +0100118 } else {
119 LOGPITS(e1i_ts, DLINP, LOGL_ERROR,
120 "Forcing socket shutdown with no signal link set\n");
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +0200121 }
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200122
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200123 msgb_free(e1i_ts->pending_msg);
124 e1i_ts->pending_msg = NULL;
125
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200126 /* e1inp_sign_link_destroy releases the socket descriptors for us. */
Pau Espin Pedrolc4c3a852022-09-16 19:24:07 +0200127 if (line->ops->sign_link_down)
128 line->ops->sign_link_down(line);
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200129
Pau Espin Pedroldeb5c4f2020-07-31 12:55:01 +0200130 e1inp_line_put2(line, __func__);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200131 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200132}
133
Eric Wildef1f3272019-07-10 18:10:31 +0200134static void ipa_bsc_keepalive_write_server_cb(struct osmo_fsm_inst *fi, void *conn, struct msgb *msg)
135{
136 struct osmo_fd *bfd = (struct osmo_fd *)conn;
137 write(bfd->fd, msg->data, msg->len);
138 msgb_free(msg);
139}
140
141static int ipa_bsc_keepalive_timeout_cb(struct osmo_fsm_inst *fi, void *data)
142{
143 struct osmo_fd *bfd = (struct osmo_fd *)data;
144
145 if (bfd->fd == -1)
146 return 1;
147
148 ipaccess_drop(bfd, (struct e1inp_line *)bfd->data);
149 return 1;
150}
151
152static void ipaccess_bsc_keepalive_fsm_alloc(struct e1inp_ts *e1i_ts, struct osmo_fd *bfd, const char *id)
153{
154 struct e1inp_line *line = e1i_ts->line;
155 struct osmo_fsm_inst *ka_fsm;
156
157 ipaccess_keepalive_fsm_cleanup(e1i_ts);
158 if (!line->ipa_kap)
159 return;
160
161 ka_fsm = ipa_generic_conn_alloc_keepalive_fsm(tall_ipa_ctx, bfd, line->ipa_kap, id);
162 e1i_ts->driver.ipaccess.ka_fsm = ka_fsm;
163 if (!ka_fsm)
164 return;
165
166 ipa_keepalive_fsm_set_timeout_cb(ka_fsm, ipa_bsc_keepalive_timeout_cb);
167 ipa_keepalive_fsm_set_send_cb(ka_fsm, ipa_bsc_keepalive_write_server_cb);
168 ipa_keepalive_fsm_start(ka_fsm);
169}
170
171static void ipa_bts_keepalive_write_client_cb(struct osmo_fsm_inst *fi, void *conn, struct msgb *msg) {
172 struct ipa_client_conn *link = (struct ipa_client_conn *)conn;
173 int ret = 0;
174
175 ret = ipa_send(link->ofd->fd, msg->data, msg->len);
176 if (ret != msg->len) {
177 LOGP(DLINP, LOGL_ERROR, "cannot send message. Reason: %s\n", strerror(errno));
178 }
179 msgb_free(msg);
180}
181
182static void update_fd_settings(struct e1inp_line *line, int fd);
183static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up);
184
185static int ipa_bts_keepalive_timeout_cb(struct osmo_fsm_inst *fi, void *conn) {
186 ipaccess_bts_updown_cb(conn, false);
187 return 1;
188}
189
190static void ipaccess_bts_keepalive_fsm_alloc(struct e1inp_ts *e1i_ts, struct ipa_client_conn *client, const char *id)
191{
192 struct e1inp_line *line = e1i_ts->line;
193 struct osmo_fsm_inst *ka_fsm;
194
Vadim Yanitskiyc2729a52023-05-05 22:28:46 +0700195 OSMO_ASSERT(e1i_ts->driver.ipaccess.ka_fsm == NULL);
Eric Wildef1f3272019-07-10 18:10:31 +0200196 if (!line->ipa_kap)
197 return;
198
199 ka_fsm = ipa_client_conn_alloc_keepalive_fsm(client, line->ipa_kap, id);
200 e1i_ts->driver.ipaccess.ka_fsm = ka_fsm;
201 if (!ka_fsm)
202 return;
203
204 ipa_keepalive_fsm_set_timeout_cb(ka_fsm, ipa_bts_keepalive_timeout_cb);
205 ipa_keepalive_fsm_set_send_cb(ka_fsm, ipa_bts_keepalive_write_client_cb);
206}
207
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200208/* Returns -1 on error, and 0 or 1 on success. If -1 or 1 is returned, line has
209 * been released and should not be used anymore by the caller. */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200210static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
211 struct osmo_fd *bfd)
212{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200213 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200214 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200215 struct ipaccess_unit unit_data = {};
216 struct e1inp_sign_link *sign_link;
217 char *unitid;
218 int len, ret;
Eric Wildef1f3272019-07-10 18:10:31 +0200219 struct e1inp_ts *e1i_ts;
220 struct osmo_fsm_inst *ka_fsm;
221
222 /* peek the pong for our keepalive fsm */
223 e1i_ts = ipaccess_line_ts(bfd, line);
224 ka_fsm = e1i_ts->driver.ipaccess.ka_fsm;
225 if (ka_fsm && msg_type == IPAC_MSGT_PONG)
226 ipa_keepalive_fsm_pong_received(ka_fsm);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200227
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200228 /* Handle IPA PING, PONG and ID_ACK messages. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200229 ret = ipa_ccm_rcvmsg_base(msg, bfd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200230 switch(ret) {
231 case -1:
232 /* error in IPA control message handling */
233 goto err;
234 case 1:
235 /* this is an IPA control message, skip further processing */
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200236 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200237 case 0:
238 /* this is not an IPA control message, continue */
239 break;
240 default:
241 LOGP(DLINP, LOGL_ERROR, "Unexpected return from "
Harald Welteb65f58f2014-08-20 22:04:11 +0200242 "ipa_ccm_rcvmsg_base "
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200243 "(ret=%d)\n", ret);
244 goto err;
245 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200246
247 switch (msg_type) {
248 case IPAC_MSGT_ID_RESP:
Pau Espin Pedrol30e80d52020-07-14 13:23:05 +0200249 DEBUGP(DLMI, "ID_RESP ");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200250 /* parse tags, search for Unit ID */
Harald Welte82eb55e2018-08-01 13:22:55 +0200251 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 +0200252 DEBUGPC(DLMI, "\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200253 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200254 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200255 "with malformed TLVs\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200256 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200257 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200258 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200259 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200260 "without unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200261 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200262
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200263 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200264 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200265 if (len < 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200266 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200267 "with too small unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200268 goto err;
269 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200270 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
271 unitid[len - 1] = '\0';
Vadim Yanitskiy1c94f6a2019-12-02 02:16:07 +0700272 ret = ipa_parse_unitid(unitid, &unit_data);
273 if (ret) {
274 LOGP(DLINP, LOGL_ERROR, "Failed to parse unit ID '%s'\n", unitid);
275 goto err;
276 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200277
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200278 if (!line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200279 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200280 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200281 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200282 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200283 /* the BSC creates the new sign links at this stage. */
284 if (bfd->priv_nr == E1INP_SIGN_OML) {
285 sign_link =
286 line->ops->sign_link_up(&unit_data, line,
287 E1INP_SIGN_OML);
288 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200289 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200290 "Unable to set signal link, "
291 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200292 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200293 }
Eric Wildef1f3272019-07-10 18:10:31 +0200294
295 ipaccess_bsc_keepalive_fsm_alloc(e1i_ts, bfd, "oml_bsc_to_bts");
296
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200297 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200298 struct e1inp_ts *ts;
Eric Wildef1f3272019-07-10 18:10:31 +0200299 struct osmo_fd *newbfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200300 struct e1inp_line *new_line;
Michael Iedemaee2589e2022-05-03 07:27:38 -0700301 char tcp_stat_name[64];
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200302
303 sign_link =
304 line->ops->sign_link_up(&unit_data, line,
305 E1INP_SIGN_RSL);
306 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200307 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200308 "Unable to set signal link, "
309 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200310 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200311 }
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200312 /* Finally, we know which OML link is associated with
313 * this RSL link, attach it to this socket. */
314 new_line = sign_link->ts->line;
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200315 /* this is a bugtrap, the BSC should be using the
316 * virtual E1 line used by OML for this RSL link. */
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200317 if (new_line == line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200318 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200319 "Fix your BSC, you should use the "
320 "E1 line used by the OML link for "
321 "your RSL link.\n");
322 return 0;
323 }
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200324 e1inp_line_get2(new_line, "ipa_bfd");
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200325 ts = e1inp_line_ipa_rsl_ts(new_line, unit_data.trx_id);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200326 newbfd = &ts->driver.ipaccess.fd;
Pau Espin Pedrol815117c2020-07-14 21:19:16 +0200327 OSMO_ASSERT(newbfd != bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200328
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200329 /* preserve 'newbfd->when' flags potentially set by sign_link_up() */
Pau Espin Pedrol67902bb2018-08-23 14:35:08 +0200330 osmo_fd_setup(newbfd, bfd->fd, newbfd->when | bfd->when, bfd->cb,
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200331 new_line, E1INP_SIGN_RSL + unit_data.trx_id);
332
333
334 /* now we can release the dummy RSL line (old temporary bfd). */
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200335 osmo_fd_unregister(bfd);
336 bfd->fd = -1;
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200337 /* bfd->data holds a reference to line, drop it */
338 OSMO_ASSERT(bfd->data == line);
339 bfd->data = NULL;
340 e1inp_line_put2(line, "ipa_bfd");
341
Harald Welteae3a9932016-11-26 09:25:23 +0100342 ret = osmo_fd_register(newbfd);
343 if (ret < 0) {
344 LOGP(DLINP, LOGL_ERROR,
345 "could not register FD\n");
346 goto err;
347 }
Michael Iedemaee2589e2022-05-03 07:27:38 -0700348 snprintf(tcp_stat_name, sizeof(tcp_stat_name), "site.%u.bts.%u.ipa-rsl.%u",
349 unit_data.site_id, unit_data.bts_id, unit_data.trx_id);
Michael Iedema29667862022-02-24 09:25:20 -0800350 osmo_stats_tcp_osmo_fd_register(newbfd, tcp_stat_name);
Eric Wildef1f3272019-07-10 18:10:31 +0200351
352 e1i_ts = ipaccess_line_ts(newbfd, new_line);
353 ipaccess_bsc_keepalive_fsm_alloc(e1i_ts, newbfd, "rsl_bsc_to_bts");
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200354 return 1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200355 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200356 break;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200357 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200358 LOGP(DLINP, LOGL_ERROR, "Unknown IPA message type\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200359 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200360 }
361 return 0;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200362err:
Harald Welteb4a7db02019-07-21 11:51:56 +0200363 if (bfd->fd != -1) {
Pau Espin Pedroldc55a5f2020-07-14 21:14:34 +0200364 osmo_fd_unregister(bfd);
Harald Welteb4a7db02019-07-21 11:51:56 +0200365 close(bfd->fd);
366 bfd->fd = -1;
Pau Espin Pedroldc55a5f2020-07-14 21:14:34 +0200367 /* This is a BSC accepted socket, bfd->data holds a reference to line, drop it */
368 OSMO_ASSERT(bfd->data == line);
369 bfd->data = NULL;
370 e1inp_line_put2(line, "ipa_bfd");
Harald Welteb4a7db02019-07-21 11:51:56 +0200371 }
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200372 return -1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200373}
374
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200375/* Returns -EBADF if bfd cannot be used by the caller anymore after return. */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200376static int handle_ts1_read(struct osmo_fd *bfd)
377{
378 struct e1inp_line *line = bfd->data;
379 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200380 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200381 struct e1inp_sign_link *link;
382 struct ipaccess_head *hh;
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200383 struct msgb *msg = NULL;
Maxc9fa25e2017-01-09 13:23:15 +0100384 int ret, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200385
Eric Wildef1f3272019-07-10 18:10:31 +0200386 e1i_ts = ipaccess_line_ts(bfd, line);
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200387 ret = ipa_msg_recv_buffered(bfd->fd, &msg, &e1i_ts->pending_msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200388 if (ret < 0) {
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200389 if (ret == -EAGAIN)
390 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200391 LOGP(DLINP, LOGL_NOTICE, "Sign link problems, "
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200392 "closing socket. Reason: %s\n", strerror(-ret));
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200393 goto err;
394 } else if (ret == 0) {
395 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
396 goto err;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200397 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200398 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200399
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200400 hh = (struct ipaccess_head *) msg->data;
401 if (hh->proto == IPAC_PROTO_IPACCESS) {
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200402 ret = ipaccess_rcvmsg(line, msg, bfd);
403 /* BIG FAT WARNING: bfd might no longer exist here (ret != 0),
404 * since ipaccess_rcvmsg() might have free'd it !!! */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200405 msgb_free(msg);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200406 return ret != 0 ? -EBADF : 0;
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100407 } else if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
408 /* this sign link is not know yet.. complain. */
409 LOGP(DLINP, LOGL_ERROR, "Timeslot is not configured.\n");
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100410 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200411 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200412
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200413 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
414 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200415 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200416 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200417 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200418 }
419 msg->dst = link;
420
421 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200422 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200423 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200424 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200425 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200426 }
Maxc9fa25e2017-01-09 13:23:15 +0100427 rc = e1i_ts->line->ops->sign_link(msg);
428 if (rc < 0) {
Pablo Neira Ayusoa49c24d2012-10-16 11:24:08 +0200429 /* Don't close the signalling link if the upper layers report
430 * an error, that's too strict. BTW, the signalling layer is
431 * resposible for releasing the message.
432 */
Harald Weltecc2241b2011-07-19 16:06:06 +0200433 LOGP(DLINP, LOGL_ERROR, "Bad signalling message,"
Maxc9fa25e2017-01-09 13:23:15 +0100434 " sign_link returned error: %s.\n", strerror(-rc));
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200435 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200436
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200437 return rc;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200438err_msg:
439 msgb_free(msg);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200440err:
Harald Welte10b41302013-06-30 14:05:49 +0200441 ipaccess_drop(bfd, line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200442 return -EBADF;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200443}
444
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200445static int ts_want_write(struct e1inp_ts *e1i_ts)
446{
Harald Welte949b8a22020-10-18 23:01:53 +0200447 osmo_fd_write_enable(&e1i_ts->driver.ipaccess.fd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200448
449 return 0;
450}
451
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200452static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200453{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200454 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200455 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Eric Wildef1f3272019-07-10 18:10:31 +0200456 struct e1inp_line *line = e1i_ts->line;
Pau Espin Pedrol3d3e5af2022-09-16 19:10:41 +0200457 struct osmo_fsm_inst *ka_fsm = e1i_ts->driver.ipaccess.ka_fsm;
Eric Wildef1f3272019-07-10 18:10:31 +0200458
Pau Espin Pedrol3d3e5af2022-09-16 19:10:41 +0200459 /* depending on caller the fsm might be dead */
460 if (ka_fsm)
461 ipa_keepalive_fsm_stop(ka_fsm);
Eric Wildef1f3272019-07-10 18:10:31 +0200462
Pau Espin Pedrol8737ad42020-07-14 21:11:56 +0200463 e1inp_int_snd_event(e1i_ts, sign_link, S_L_INP_TEI_DN);
464 /* the first e1inp_sign_link_destroy call closes the socket. */
465 if (bfd->fd != -1) {
466 osmo_fd_unregister(bfd);
467 close(bfd->fd);
468 bfd->fd = -1;
469 /* If The bfd holds a reference to e1inp_line in ->data (BSC
470 * accepted() sockets), then release it */
471 if (bfd->data == line) {
472 bfd->data = NULL;
473 e1inp_line_put2(line, "ipa_bfd");
474 }
475 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200476}
477
Pau Espin Pedrol90df0352022-05-09 12:59:35 +0200478static bool e1i_ts_has_pending_tx_msgs(struct e1inp_ts *e1i_ts)
479{
480 struct e1inp_sign_link *link;
481 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
482 if (!llist_empty(&link->tx_list)) {
483 return true;
484 }
485 }
486 return false;
487}
488
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200489static void timeout_ts1_write(void *data)
490{
491 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
492
493 /* trigger write of ts1, due to tx delay timer */
Pau Espin Pedrol90df0352022-05-09 12:59:35 +0200494 if (e1i_ts_has_pending_tx_msgs(e1i_ts))
495 ts_want_write(e1i_ts);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200496}
497
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200498static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200499{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200500 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200501 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200502 struct e1inp_sign_link *sign_link;
503 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200504 int ret;
505
Eric Wildef1f3272019-07-10 18:10:31 +0200506 e1i_ts = ipaccess_line_ts(bfd, line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200507
508 /* get the next msg for this timeslot */
509 msg = e1inp_tx_ts(e1i_ts, &sign_link);
510 if (!msg) {
511 /* no message after tx delay timer */
Harald Welte28fea772022-05-06 19:38:33 +0000512 osmo_fd_write_disable(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200513 return 0;
514 }
515
516 switch (sign_link->type) {
517 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200518 case E1INP_SIGN_RSL:
Harald Welte46fc7e22014-08-18 19:04:26 +0200519 case E1INP_SIGN_OSMO:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200520 break;
521 default:
Pau Espin Pedrold20a10c2022-05-09 12:45:07 +0200522 /* leave WRITE flag enabled, come back for more msg */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200523 ret = -EINVAL;
524 goto out;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200525 }
526
527 msg->l2h = msg->data;
Harald Welteb65f58f2014-08-20 22:04:11 +0200528 ipa_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200529
Vadim Yanitskiy77a7cf42020-11-29 20:39:57 +0100530 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "TX %u: %s\n", ts_nr,
Pau Espin Pedrol3bdf59b2020-11-23 13:47:16 +0100531 osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200532
533 ret = send(bfd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200534 if (ret != msg->len) {
Pau Espin Pedrol3bdf59b2020-11-23 13:47:16 +0100535 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling "
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200536 "message. Reason: %s\n", strerror(errno));
537 goto err;
538 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200539
Harald Welte28fea772022-05-06 19:38:33 +0000540 /* this is some ancient code that apparently exists to slow down writes towards
541 * some even more ancient nanoBTS 900 units. See git commit
542 * d49fc5ae24fc9d44d2b284392ab619cc7a69a876 of openbsc.git (now osmo-bsc.git) */
543 if (e1i_ts->sign.delay) {
544 osmo_fd_write_disable(bfd);
545 /* set tx delay timer for next event */
546 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
547 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
Pau Espin Pedrol90df0352022-05-09 12:59:35 +0200548 } else {
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200549out:
Pau Espin Pedrol90df0352022-05-09 12:59:35 +0200550 if (!e1i_ts_has_pending_tx_msgs(e1i_ts))
551 osmo_fd_write_disable(bfd);
552 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200553 msgb_free(msg);
554 return ret;
555err:
Harald Welte10b41302013-06-30 14:05:49 +0200556 ipaccess_drop(bfd, line);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200557 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200558 return ret;
559}
560
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200561static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200562{
563 struct e1inp_line *line = bfd->data;
564
565 return __handle_ts1_write(bfd, line);
566}
567
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200568static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200569{
570 struct e1inp_line *line = link->line;
571
572 return __handle_ts1_write(link->ofd, line);
573}
574
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200575/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200576int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200577{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200578 int rc = 0;
579
Harald Weltede3959e2020-10-18 22:28:50 +0200580 if (what & OSMO_FD_READ)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200581 rc = handle_ts1_read(bfd);
Harald Weltede3959e2020-10-18 22:28:50 +0200582 if (rc != -EBADF && (what & OSMO_FD_WRITE))
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200583 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200584
585 return rc;
586}
587
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200588static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200589
590struct e1inp_driver ipaccess_driver = {
591 .name = "ipa",
592 .want_write = ts_want_write,
593 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200594 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200595 .default_delay = 0,
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100596 .has_keepalive = 1,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200597};
598
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100599static void update_fd_settings(struct e1inp_line *line, int fd)
600{
601 int ret;
602 int val;
603
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100604 if (line->keepalive_num_probes) {
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100605 /* Enable TCP keepalive to find out if the connection is gone */
606 val = 1;
607 ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
608 if (ret < 0)
Oliver Smith3d8426e2022-11-23 14:12:56 +0100609 LOGP(DLINP, LOGL_ERROR, "Failed to set keepalive: %s\n",
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100610 strerror(errno));
611 else
612 LOGP(DLINP, LOGL_NOTICE, "Keepalive is set: %i\n", ret);
613
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100614 /* The following options are not portable! */
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100615 val = line->keepalive_idle_timeout > 0 ?
616 line->keepalive_idle_timeout :
617 DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100618 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
619 &val, sizeof(val));
620 if (ret < 0)
Oliver Smith3d8426e2022-11-23 14:12:56 +0100621 LOGP(DLINP, LOGL_ERROR,
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100622 "Failed to set keepalive idle time: %s\n",
623 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100624 val = line->keepalive_probe_interval > -1 ?
625 line->keepalive_probe_interval :
626 DEFAULT_TCP_KEEPALIVE_INTERVAL;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100627 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
628 &val, sizeof(val));
629 if (ret < 0)
Oliver Smith3d8426e2022-11-23 14:12:56 +0100630 LOGP(DLINP, LOGL_ERROR,
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100631 "Failed to set keepalive interval: %s\n",
632 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100633 val = line->keepalive_num_probes > 0 ?
634 line->keepalive_num_probes :
635 DEFAULT_TCP_KEEPALIVE_RETRY_COUNT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100636 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
637 &val, sizeof(val));
638 if (ret < 0)
Oliver Smith3d8426e2022-11-23 14:12:56 +0100639 LOGP(DLINP, LOGL_ERROR,
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100640 "Failed to set keepalive count: %s\n",
641 strerror(errno));
Eric Wild6eb186c2019-06-21 15:26:25 +0200642 val = 1000 * line->keepalive_num_probes *
643 line->keepalive_probe_interval +
644 line->keepalive_idle_timeout;
645 ret = setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
646 &val, sizeof(val));
647 if (ret < 0)
Oliver Smith3d8426e2022-11-23 14:12:56 +0100648 LOGP(DLINP, LOGL_ERROR,
Eric Wild6eb186c2019-06-21 15:26:25 +0200649 "Failed to set user timoeut: %s\n",
650 strerror(errno));
Holger Hans Peter Freytherf465a4c2014-02-03 09:34:02 +0100651 }
Oliver Smith62725d02020-04-17 10:57:46 +0200652
653 val = 1;
654 ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
655 if (ret < 0)
656 LOGP(DLINP, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100657}
658
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200659/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200660static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200661{
662 int ret;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100663 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200664 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200665 struct e1inp_ts *e1i_ts;
666 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200667
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200668 /* clone virtual E1 line for this new OML link. */
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200669 line = e1inp_line_clone(tall_ipa_ctx, link->line, "ipa_bfd");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200670 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200671 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200672 return -ENOMEM;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200673 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200674
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200675 /* create virrtual E1 timeslots for signalling */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200676 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200677
678 /* initialize the fds */
679 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
680 line->ts[i].driver.ipaccess.fd.fd = -1;
681
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200682 e1i_ts = e1inp_line_ipa_oml_ts(line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200683
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200684 bfd = &e1i_ts->driver.ipaccess.fd;
Harald Weltede3959e2020-10-18 22:28:50 +0200685 osmo_fd_setup(bfd, fd, OSMO_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_OML);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200686 ret = osmo_fd_register(bfd);
687 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200688 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200689 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200690 }
Philipp Maierf5f31d32021-12-06 16:46:40 +0100691 osmo_stats_tcp_osmo_fd_register(bfd, "ipa-oml");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200692
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100693 update_fd_settings(line, bfd->fd);
Daniel Willmann85980722014-01-09 14:30:55 +0100694
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200695 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200696 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200697 if (ret < 0) {
698 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
699 strerror(errno));
700 goto err_socket;
701 }
702 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200703
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200704err_socket:
705 osmo_fd_unregister(bfd);
706err_line:
707 close(bfd->fd);
708 bfd->fd = -1;
Pau Espin Pedrola2ff7332020-07-14 21:12:50 +0200709 bfd->data = NULL;
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200710 e1inp_line_put2(line, "ipa_bfd");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200711 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200712}
713
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200714static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200715{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200716 struct e1inp_line *line;
717 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200718 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200719 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200720
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200721 /* We don't know yet which OML link to associate it with. Thus, we
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200722 * allocate a temporary E1 line until we have received ID. */
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200723 line = e1inp_line_clone(tall_ipa_ctx, link->line, "ipa_bfd");
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200724 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200725 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200726 return -ENOMEM;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200727 }
728 /* initialize the fds */
729 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
730 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200731
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200732 /* we need this to initialize this in case to avoid crashes in case
733 * that the socket is closed before we've seen an ID_RESP. */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200734 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200735
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200736 e1i_ts = e1inp_line_ipa_rsl_ts(line, 0);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200737
738 bfd = &e1i_ts->driver.ipaccess.fd;
Harald Weltede3959e2020-10-18 22:28:50 +0200739 osmo_fd_setup(bfd, fd, OSMO_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_RSL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200740 ret = osmo_fd_register(bfd);
741 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200742 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200743 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200744 }
Philipp Maierf5f31d32021-12-06 16:46:40 +0100745 osmo_stats_tcp_osmo_fd_register(bfd, "ipa-rsl");
746
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200747 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200748 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200749 if (ret < 0) {
750 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
751 strerror(errno));
752 goto err_socket;
753 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100754 update_fd_settings(line, bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200755 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200756
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200757err_socket:
758 osmo_fd_unregister(bfd);
759err_line:
760 close(bfd->fd);
761 bfd->fd = -1;
Pau Espin Pedrola2ff7332020-07-14 21:12:50 +0200762 bfd->data = NULL;
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200763 e1inp_line_put2(line, "ipa_bfd");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200764 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200765}
766
Harald Weltee416e2e2017-05-26 13:11:59 +0200767#define IPA_STRING_MAX 64
768
769static struct msgb *
Max71393522018-01-08 15:42:00 +0100770ipa_bts_id_resp(const struct ipaccess_unit *dev, uint8_t *data, int len, int trx_nr)
Harald Weltee416e2e2017-05-26 13:11:59 +0200771{
772 struct msgb *nmsg;
773 char str[IPA_STRING_MAX];
774 uint8_t *tag;
775
776 memset(str, 0, sizeof(str));
777
778 nmsg = ipa_msg_alloc(0);
779 if (!nmsg)
780 return NULL;
781
782 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
783 while (len) {
784 if (len < 2) {
785 LOGP(DLINP, LOGL_NOTICE,
786 "Short read of ipaccess tag\n");
787 msgb_free(nmsg);
788 return NULL;
789 }
790 switch (data[1]) {
791 case IPAC_IDTAG_UNIT:
792 snprintf(str, sizeof(str), "%u/%u/%u",
793 dev->site_id, dev->bts_id, trx_nr);
794 break;
795 case IPAC_IDTAG_MACADDR:
796 snprintf(str, sizeof(str),
797 "%02x:%02x:%02x:%02x:%02x:%02x",
798 dev->mac_addr[0], dev->mac_addr[1],
799 dev->mac_addr[2], dev->mac_addr[3],
800 dev->mac_addr[4], dev->mac_addr[5]);
801 break;
802 case IPAC_IDTAG_LOCATION1:
803 if (dev->location1)
Stefan Sperling961776a2018-12-06 13:09:48 +0100804 osmo_strlcpy(str, dev->location1, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200805 break;
806 case IPAC_IDTAG_LOCATION2:
807 if (dev->location2)
Stefan Sperling961776a2018-12-06 13:09:48 +0100808 osmo_strlcpy(str, dev->location2, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200809 break;
810 case IPAC_IDTAG_EQUIPVERS:
811 if (dev->equipvers)
Stefan Sperling961776a2018-12-06 13:09:48 +0100812 osmo_strlcpy(str, dev->equipvers, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200813 break;
814 case IPAC_IDTAG_SWVERSION:
815 if (dev->swversion)
Neels Hofmeyr4c57eef2018-07-26 17:28:24 +0200816 osmo_strlcpy(str, dev->swversion, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200817 break;
818 case IPAC_IDTAG_UNITNAME:
819 snprintf(str, sizeof(str),
820 "%s-%02x-%02x-%02x-%02x-%02x-%02x",
821 dev->unit_name,
822 dev->mac_addr[0], dev->mac_addr[1],
823 dev->mac_addr[2], dev->mac_addr[3],
824 dev->mac_addr[4], dev->mac_addr[5]);
825 break;
826 case IPAC_IDTAG_SERNR:
827 if (dev->serno)
Stefan Sperling961776a2018-12-06 13:09:48 +0100828 osmo_strlcpy(str, dev->serno, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200829 break;
830 default:
831 LOGP(DLINP, LOGL_NOTICE,
832 "Unknown ipaccess tag 0x%02x\n", *data);
833 msgb_free(nmsg);
834 return NULL;
835 }
Harald Weltee416e2e2017-05-26 13:11:59 +0200836
837 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
838 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
839 tag[0] = 0x00;
840 tag[1] = 1 + strlen(str) + 1;
841 tag[2] = data[1];
842 memcpy(tag + 3, str, strlen(str) + 1);
843 data += 2;
844 len -= 2;
845 }
846 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
847 return nmsg;
848}
849
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200850static struct msgb *ipa_bts_id_ack(void)
851{
852 struct msgb *nmsg2;
853
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200854 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200855 if (!nmsg2)
856 return NULL;
857
858 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200859 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200860
861 return nmsg2;
862}
863
Harald Welte51de9ca2013-06-30 20:13:16 +0200864static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up)
865{
866 struct e1inp_line *line = link->line;
Daniel Willmann8e380942022-12-13 19:15:15 +0100867 struct e1inp_ts *e1i_ts = ipaccess_line_ts(link->ofd, line);
Harald Welte51de9ca2013-06-30 20:13:16 +0200868
Eric Wildef1f3272019-07-10 18:10:31 +0200869 if (up) {
Daniel Willmann8e380942022-12-13 19:15:15 +0100870 struct osmo_fsm_inst *ka_fsm = e1i_ts->driver.ipaccess.ka_fsm;
Eric Wildef1f3272019-07-10 18:10:31 +0200871
872 update_fd_settings(line, link->ofd->fd);
873 if (ka_fsm && line->ipa_kap)
874 ipa_keepalive_fsm_start(ka_fsm);
875 return;
876 }
Harald Welte51de9ca2013-06-30 20:13:16 +0200877
Daniel Willmann8e380942022-12-13 19:15:15 +0100878 ipaccess_keepalive_fsm_cleanup(e1i_ts);
Harald Welte51de9ca2013-06-30 20:13:16 +0200879 if (line->ops->sign_link_down)
880 line->ops->sign_link_down(line);
881}
882
Harald Welte783715b2014-08-17 18:24:51 +0200883/* handle incoming message to BTS, check if it is an IPA CCM, and if yes,
884 * handle it accordingly (PING/PONG/ID_REQ/ID_RESP/ID_ACK) */
885int ipaccess_bts_handle_ccm(struct ipa_client_conn *link,
886 struct ipaccess_unit *dev, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200887{
888 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200889 struct msgb *rmsg;
890 int ret = 0;
Eric Wildef1f3272019-07-10 18:10:31 +0200891 /* line might not exist if != bsc||bts */
892 struct e1inp_line *line = link->line;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200893
894 /* special handling for IPA CCM. */
895 if (hh->proto == IPAC_PROTO_IPACCESS) {
896 uint8_t msg_type = *(msg->l2h);
Eric Wildef1f3272019-07-10 18:10:31 +0200897 struct osmo_fsm_inst* ka_fsm = NULL;
898
899 /* peek the pong for our keepalive fsm */
900 if (line && msg_type == IPAC_MSGT_PONG) {
901 ka_fsm = ipaccess_line_ts(link->ofd, line)->driver.ipaccess.ka_fsm;
902 ipa_keepalive_fsm_pong_received(ka_fsm);
903 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200904
905 /* ping, pong and acknowledgment cases. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200906 ret = ipa_ccm_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200907 if (ret < 0)
908 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200909
910 /* this is a request for identification from the BSC. */
911 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200912 uint8_t *data = msgb_l2(msg);
913 int len = msgb_l2len(msg);
Andreas Eversbergf422a752014-01-21 14:54:41 +0100914 int trx_nr = 0;
915
916 if (link->ofd->priv_nr >= E1INP_SIGN_RSL)
917 trx_nr = link->ofd->priv_nr - E1INP_SIGN_RSL;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200918
Vadim Yanitskiy69ae2382019-12-02 02:42:43 +0700919 LOGP(DLINP, LOGL_NOTICE, "received ID_GET for unit ID %u/%u/%u\n",
Pau Espin Pedroldd95eb62018-10-02 20:05:28 +0200920 dev->site_id, dev->bts_id, trx_nr);
Harald Weltee416e2e2017-05-26 13:11:59 +0200921 rmsg = ipa_bts_id_resp(dev, data + 1, len - 1, trx_nr);
Harald Welteb65f58f2014-08-20 22:04:11 +0200922 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200923 if (ret != rmsg->len) {
924 LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP "
925 "message. Reason: %s\n", strerror(errno));
926 goto err_rmsg;
927 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200928 msgb_free(rmsg);
929
930 /* send ID_ACK. */
931 rmsg = ipa_bts_id_ack();
Harald Welteb65f58f2014-08-20 22:04:11 +0200932 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200933 if (ret != rmsg->len) {
934 LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK "
935 "message. Reason: %s\n", strerror(errno));
936 goto err_rmsg;
937 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200938 msgb_free(rmsg);
Harald Welte783715b2014-08-17 18:24:51 +0200939 }
940 return 1;
941 }
Harald Welte6eddd472013-06-30 20:18:53 +0200942
Harald Welte783715b2014-08-17 18:24:51 +0200943 return 0;
944
945err_rmsg:
946 msgb_free(rmsg);
947err:
948 ipa_client_conn_close(link);
949 return -1;
950}
951
952static int ipaccess_bts_read_cb(struct ipa_client_conn *link, struct msgb *msg)
953{
954 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
955 struct e1inp_ts *e1i_ts = NULL;
956 struct e1inp_sign_link *sign_link;
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200957 uint8_t msg_type = *(msg->l2h);
Harald Welte783715b2014-08-17 18:24:51 +0200958 int ret = 0;
959
960 /* special handling for IPA CCM. */
961 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200962 /* this is a request for identification from the BSC. */
963 if (msg_type == IPAC_MSGT_ID_GET) {
964 if (!link->line->ops->sign_link_up) {
965 LOGP(DLINP, LOGL_ERROR,
966 "Unable to set signal link, "
967 "closing socket.\n");
Harald Welte783715b2014-08-17 18:24:51 +0200968 goto err;
969 }
970 }
971 }
972
973 /* core CCM handling */
974 ret = ipaccess_bts_handle_ccm(link, link->line->ops->cfg.ipa.dev, msg);
975 if (ret < 0)
976 goto err;
977
978 if (ret == 1 && hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200979 if (msg_type == IPAC_MSGT_ID_GET) {
Harald Welte6eddd472013-06-30 20:18:53 +0200980 sign_link = link->line->ops->sign_link_up(msg,
981 link->line,
982 link->ofd->priv_nr);
983 if (sign_link == NULL) {
984 LOGP(DLINP, LOGL_ERROR,
985 "Unable to set signal link, "
986 "closing socket.\n");
Harald Welte6eddd472013-06-30 20:18:53 +0200987 goto err;
988 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200989 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200990 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200991 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200992 } else if (link->port == IPA_TCP_PORT_OML)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200993 e1i_ts = e1inp_line_ipa_oml_ts(link->line);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200994 else if (link->port == IPA_TCP_PORT_RSL)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200995 e1i_ts = e1inp_line_ipa_rsl_ts(link->line, link->ofd->priv_nr - E1INP_SIGN_RSL);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200996
Pablo Neira Ayusob9487012013-07-05 14:38:43 +0200997 OSMO_ASSERT(e1i_ts != NULL);
998
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200999 if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
1000 LOGP(DLINP, LOGL_ERROR, "Signalling link not initialized. Discarding."
1001 " port=%u msg_type=%u\n", link->port, msg_type);
Neels Hofmeyrfe673112018-08-24 17:43:45 +02001002 goto err;
1003 }
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +02001004
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +02001005 /* look up for some existing signaling link. */
1006 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
1007 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001008 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +02001009 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +02001010 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +02001011 }
1012 msg->dst = sign_link;
1013
1014 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +02001015 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001016 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +02001017 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +02001018 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +02001019 }
Pau Espin Pedroled122f32018-08-28 18:25:02 +02001020 return link->line->ops->sign_link(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +02001021
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +02001022err:
Harald Welte783715b2014-08-17 18:24:51 +02001023 ipa_client_conn_close(link);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +02001024 msgb_free(msg);
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +02001025 return -EBADF;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +02001026}
1027
Pablo Neira Ayusod6216402011-08-31 16:47:44 +02001028struct ipaccess_line {
Oliver Smith65ae42c2020-01-22 11:24:52 +01001029 bool line_already_initialized;
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001030 struct ipa_client_conn *ipa_cli[NUM_E1_TS]; /* 0=OML, 1+N=TRX_N */
Pablo Neira Ayusod6216402011-08-31 16:47:44 +02001031};
1032
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001033static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001034{
1035 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +02001036 struct ipaccess_line *il;
1037
1038 if (!line->driver_data)
1039 line->driver_data = talloc_zero(line, struct ipaccess_line);
1040
1041 if (!line->driver_data) {
1042 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
1043 return -ENOMEM;
1044 }
1045 il = line->driver_data;
1046
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001047 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001048 case E1INP_LINE_R_BSC: {
Pau Espin Pedrold2d28d82021-09-29 14:15:56 +02001049 /* We only initialize this line once. */
1050 if (il->line_already_initialized)
1051 return 0;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001052 struct ipa_server_link *oml_link, *rsl_link;
Max4c4a1c22016-12-30 15:10:47 +01001053 const char *ipa = e1inp_ipa_get_bind_addr();
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001054
Max4c4a1c22016-12-30 15:10:47 +01001055 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode on %s "
1056 "with OML %u and RSL %u TCP ports\n", ipa,
1057 IPA_TCP_PORT_OML, IPA_TCP_PORT_RSL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001058
Max4c4a1c22016-12-30 15:10:47 +01001059 oml_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001060 IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +02001061 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001062 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001063 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001064 "BSC link: %s\n", strerror(errno));
1065 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +02001066 }
Harald Welte41547552021-04-28 18:23:21 +02001067 oml_link->dscp = g_e1inp_ipaccess_pars.oml.dscp;
1068 oml_link->priority = g_e1inp_ipaccess_pars.oml.priority;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001069 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001070 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001071 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001072 ipa_server_link_destroy(oml_link);
1073 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +02001074 }
Max4c4a1c22016-12-30 15:10:47 +01001075 rsl_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001076 IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +02001077 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001078 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001079 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001080 "BSC link: %s\n", strerror(errno));
1081 return -ENOMEM;
1082 }
Harald Welte41547552021-04-28 18:23:21 +02001083 rsl_link->dscp = g_e1inp_ipaccess_pars.rsl.dscp;
1084 rsl_link->priority = g_e1inp_ipaccess_pars.rsl.priority;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001085 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001086 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001087 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001088 ipa_server_link_destroy(rsl_link);
1089 return -EIO;
1090 }
1091 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001092 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001093 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001094 case E1INP_LINE_R_BTS: {
Harald Welte84f67b22013-06-30 13:13:59 +02001095 struct ipa_client_conn *link;
Daniel Willmann8e380942022-12-13 19:15:15 +01001096 struct e1inp_ts *e1i_ts = e1inp_line_ipa_oml_ts(line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001097
Max4c4a1c22016-12-30 15:10:47 +01001098 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode, "
1099 "OML connecting to %s:%u\n", line->ops->cfg.ipa.addr,
1100 IPA_TCP_PORT_OML);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001101
Pau Espin Pedrold2d28d82021-09-29 14:15:56 +02001102 /* Drop previous line */
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001103 if (il->ipa_cli[0]) {
1104 ipa_client_conn_close(il->ipa_cli[0]);
Daniel Willmann8e380942022-12-13 19:15:15 +01001105 ipaccess_keepalive_fsm_cleanup(e1i_ts);
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001106 ipa_client_conn_destroy(il->ipa_cli[0]);
1107 il->ipa_cli[0] = NULL;
Pau Espin Pedrold2d28d82021-09-29 14:15:56 +02001108 }
1109
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001110 link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +02001111 e1inp_line_ipa_oml_ts(line),
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +02001112 E1INP_SIGN_OML,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001113 NULL, 0,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001114 line->ops->cfg.ipa.addr,
1115 IPA_TCP_PORT_OML,
Harald Welte51de9ca2013-06-30 20:13:16 +02001116 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001117 ipaccess_bts_read_cb,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +02001118 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001119 line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001120 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001121 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001122 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001123 return -ENOMEM;
1124 }
Harald Welte41547552021-04-28 18:23:21 +02001125 link->dscp = g_e1inp_ipaccess_pars.oml.dscp;
1126 link->priority = g_e1inp_ipaccess_pars.oml.priority;
Daniel Willmannc20af052022-12-15 11:50:34 +01001127 if (ipa_client_conn_open2(link, line->connect_timeout) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001128 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001129 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001130 ipa_client_conn_close(link);
1131 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001132 return -EIO;
1133 }
Eric Wildef1f3272019-07-10 18:10:31 +02001134
Eric Wildef1f3272019-07-10 18:10:31 +02001135 ipaccess_bts_keepalive_fsm_alloc(e1i_ts, link, "oml_bts_to_bsc");
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001136 il->ipa_cli[0] = link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001137 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001138 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001139 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001140 default:
1141 break;
1142 }
Philipp Maierea0f1bd2021-05-20 21:01:23 +02001143
1144 il->line_already_initialized = true;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001145 return ret;
1146}
1147
Andreas Eversbergf422a752014-01-21 14:54:41 +01001148/* backwards compatibility */
Harald Welte84f67b22013-06-30 13:13:59 +02001149int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line,
1150 const char *rem_addr, uint16_t rem_port)
1151{
Andreas Eversbergf422a752014-01-21 14:54:41 +01001152 return e1inp_ipa_bts_rsl_connect_n(line, rem_addr, rem_port, 0);
1153}
1154
1155int e1inp_ipa_bts_rsl_connect_n(struct e1inp_line *line,
1156 const char *rem_addr, uint16_t rem_port,
1157 uint8_t trx_nr)
1158{
Harald Welte84f67b22013-06-30 13:13:59 +02001159 struct ipa_client_conn *rsl_link;
Eric Wildef1f3272019-07-10 18:10:31 +02001160 struct e1inp_ts *e1i_ts = e1inp_line_ipa_rsl_ts(line, trx_nr);
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001161 struct ipaccess_line *il;
Pau Espin Pedrol57a148f2022-09-15 18:54:21 +02001162 int rc;
Harald Welte84f67b22013-06-30 13:13:59 +02001163
Andreas Eversbergf422a752014-01-21 14:54:41 +01001164 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
1165 LOGP(DLINP, LOGL_ERROR, "cannot create RSL BTS link: "
1166 "trx_nr (%d) out of range\n", trx_nr);
1167 return -EINVAL;
1168 }
Andreas Eversbergf422a752014-01-21 14:54:41 +01001169
Pau Espin Pedrol57a148f2022-09-15 18:54:21 +02001170 /* Drop previous line */
1171 if ((rc = e1inp_ipa_bts_rsl_close_n(line, trx_nr)) < 0)
1172 return rc;
1173
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001174 if (!line->driver_data)
1175 line->driver_data = talloc_zero(line, struct ipaccess_line);
1176 il = line->driver_data;
1177
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001178 rsl_link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +02001179 e1inp_line_ipa_rsl_ts(line, trx_nr),
Andreas Eversbergf422a752014-01-21 14:54:41 +01001180 E1INP_SIGN_RSL+trx_nr,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001181 NULL, 0,
Harald Welte84f67b22013-06-30 13:13:59 +02001182 rem_addr, rem_port,
Harald Welte51de9ca2013-06-30 20:13:16 +02001183 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001184 ipaccess_bts_read_cb,
Harald Welte84f67b22013-06-30 13:13:59 +02001185 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001186 line);
Harald Welte84f67b22013-06-30 13:13:59 +02001187 if (rsl_link == NULL) {
1188 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
1189 "BTS link: %s\n", strerror(errno));
1190 return -ENOMEM;
1191 }
Harald Welte41547552021-04-28 18:23:21 +02001192 rsl_link->dscp = g_e1inp_ipaccess_pars.rsl.dscp;
1193 rsl_link->priority = g_e1inp_ipaccess_pars.rsl.priority;
Daniel Willmannc20af052022-12-15 11:50:34 +01001194 if (ipa_client_conn_open2(rsl_link, line->connect_timeout) < 0) {
Harald Welte84f67b22013-06-30 13:13:59 +02001195 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
1196 strerror(errno));
1197 ipa_client_conn_close(rsl_link);
1198 ipa_client_conn_destroy(rsl_link);
1199 return -EIO;
1200 }
Eric Wildef1f3272019-07-10 18:10:31 +02001201 ipaccess_bts_keepalive_fsm_alloc(e1i_ts, rsl_link, "rsl_bts_to_bsc");
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001202 il->ipa_cli[1 + trx_nr] = rsl_link;
Harald Welte84f67b22013-06-30 13:13:59 +02001203 return 0;
1204}
1205
Pau Espin Pedrol57a148f2022-09-15 18:54:21 +02001206/* Close the underlying IPA TCP socket of an RSL link */
1207int e1inp_ipa_bts_rsl_close_n(struct e1inp_line *line, uint8_t trx_nr)
1208{
1209 struct ipaccess_line *il;
Vadim Yanitskiyc2729a52023-05-05 22:28:46 +07001210 struct e1inp_ts *e1i_ts;
Pau Espin Pedrol57a148f2022-09-15 18:54:21 +02001211
1212 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
1213 LOGP(DLINP, LOGL_ERROR,
1214 "cannot close RSL BTS link: trx_nr (%d) out of range\n", trx_nr);
1215 return -EINVAL;
1216 }
1217
1218 il = line->driver_data;
1219 if (!il)
1220 return 0; /* Nothing to do, no lines created */
1221
Vadim Yanitskiyc2729a52023-05-05 22:28:46 +07001222 e1i_ts = e1inp_line_ipa_rsl_ts(line, trx_nr);
1223 ipaccess_keepalive_fsm_cleanup(e1i_ts);
1224
Pau Espin Pedrol57a148f2022-09-15 18:54:21 +02001225 if (il->ipa_cli[1 + trx_nr]) {
1226 ipa_client_conn_close(il->ipa_cli[1 + trx_nr]);
1227 ipa_client_conn_destroy(il->ipa_cli[1 + trx_nr]);
1228 il->ipa_cli[1 + trx_nr] = NULL;
1229 }
1230 return 0;
1231}
1232
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001233void e1inp_ipaccess_init(void)
1234{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +02001235 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001236 e1inp_driver_register(&ipaccess_driver);
1237}
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001238
1239void e1inp_ipa_set_bind_addr(const char *ip_bind_addr)
1240{
1241 talloc_free((char*)ipaccess_driver.bind_addr);
1242 ipaccess_driver.bind_addr = NULL;
1243
1244 if (ip_bind_addr)
1245 ipaccess_driver.bind_addr = talloc_strdup(tall_ipa_ctx,
1246 ip_bind_addr);
1247}
1248
1249const char *e1inp_ipa_get_bind_addr(void)
1250{
1251 return ipaccess_driver.bind_addr?
1252 ipaccess_driver.bind_addr
1253 : "0.0.0.0";
1254}