blob: 75d9693bd9e1dbb23a767f299d6c7467a38df3d6 [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;
Harald Welte1645a2a2024-02-21 17:14:02 +0100163 if (!ka_fsm) {
164 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Failed to allocate IPA keepalive FSM\n");
Eric Wildef1f3272019-07-10 18:10:31 +0200165 return;
Harald Welte1645a2a2024-02-21 17:14:02 +0100166 }
Eric Wildef1f3272019-07-10 18:10:31 +0200167
168 ipa_keepalive_fsm_set_timeout_cb(ka_fsm, ipa_bsc_keepalive_timeout_cb);
169 ipa_keepalive_fsm_set_send_cb(ka_fsm, ipa_bsc_keepalive_write_server_cb);
170 ipa_keepalive_fsm_start(ka_fsm);
171}
172
173static void ipa_bts_keepalive_write_client_cb(struct osmo_fsm_inst *fi, void *conn, struct msgb *msg) {
174 struct ipa_client_conn *link = (struct ipa_client_conn *)conn;
175 int ret = 0;
176
177 ret = ipa_send(link->ofd->fd, msg->data, msg->len);
178 if (ret != msg->len) {
179 LOGP(DLINP, LOGL_ERROR, "cannot send message. Reason: %s\n", strerror(errno));
180 }
181 msgb_free(msg);
182}
183
184static void update_fd_settings(struct e1inp_line *line, int fd);
185static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up);
186
187static int ipa_bts_keepalive_timeout_cb(struct osmo_fsm_inst *fi, void *conn) {
188 ipaccess_bts_updown_cb(conn, false);
189 return 1;
190}
191
192static void ipaccess_bts_keepalive_fsm_alloc(struct e1inp_ts *e1i_ts, struct ipa_client_conn *client, const char *id)
193{
194 struct e1inp_line *line = e1i_ts->line;
195 struct osmo_fsm_inst *ka_fsm;
196
Vadim Yanitskiyf6bde0f2023-05-05 22:28:46 +0700197 OSMO_ASSERT(e1i_ts->driver.ipaccess.ka_fsm == NULL);
Eric Wildef1f3272019-07-10 18:10:31 +0200198 if (!line->ipa_kap)
199 return;
200
201 ka_fsm = ipa_client_conn_alloc_keepalive_fsm(client, line->ipa_kap, id);
202 e1i_ts->driver.ipaccess.ka_fsm = ka_fsm;
Harald Welte1645a2a2024-02-21 17:14:02 +0100203 if (!ka_fsm) {
204 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Failed to allocate IPA keepalive FSM\n");
Eric Wildef1f3272019-07-10 18:10:31 +0200205 return;
Harald Welte1645a2a2024-02-21 17:14:02 +0100206 }
Eric Wildef1f3272019-07-10 18:10:31 +0200207
208 ipa_keepalive_fsm_set_timeout_cb(ka_fsm, ipa_bts_keepalive_timeout_cb);
209 ipa_keepalive_fsm_set_send_cb(ka_fsm, ipa_bts_keepalive_write_client_cb);
210}
211
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200212/* Returns -1 on error, and 0 or 1 on success. If -1 or 1 is returned, line has
213 * been released and should not be used anymore by the caller. */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200214static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
215 struct osmo_fd *bfd)
216{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200217 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200218 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200219 struct ipaccess_unit unit_data = {};
220 struct e1inp_sign_link *sign_link;
221 char *unitid;
222 int len, ret;
Eric Wildef1f3272019-07-10 18:10:31 +0200223 struct e1inp_ts *e1i_ts;
224 struct osmo_fsm_inst *ka_fsm;
225
226 /* peek the pong for our keepalive fsm */
227 e1i_ts = ipaccess_line_ts(bfd, line);
228 ka_fsm = e1i_ts->driver.ipaccess.ka_fsm;
229 if (ka_fsm && msg_type == IPAC_MSGT_PONG)
230 ipa_keepalive_fsm_pong_received(ka_fsm);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200231
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200232 /* Handle IPA PING, PONG and ID_ACK messages. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200233 ret = ipa_ccm_rcvmsg_base(msg, bfd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200234 switch(ret) {
235 case -1:
236 /* error in IPA control message handling */
237 goto err;
238 case 1:
239 /* this is an IPA control message, skip further processing */
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200240 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200241 case 0:
242 /* this is not an IPA control message, continue */
243 break;
244 default:
Harald Weltea87de3c2024-02-21 17:41:12 +0100245 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Unexpected return from ipa_ccm_rcvmsg_base (ret=%d)\n", ret);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200246 goto err;
247 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200248
249 switch (msg_type) {
250 case IPAC_MSGT_ID_RESP:
Harald Weltea87de3c2024-02-21 17:41:12 +0100251 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "ID_RESP ");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200252 /* parse tags, search for Unit ID */
Harald Welte82eb55e2018-08-01 13:22:55 +0200253 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 +0200254 DEBUGPC(DLMI, "\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200255 if (ret < 0) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100256 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "IPA response message with malformed TLVs\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200257 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200258 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200259 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100260 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "IPA response message 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 Weltea87de3c2024-02-21 17:41:12 +0100266 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "IPA response message with too small unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200267 goto err;
268 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200269 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
270 unitid[len - 1] = '\0';
Vadim Yanitskiy1c94f6a2019-12-02 02:16:07 +0700271 ret = ipa_parse_unitid(unitid, &unit_data);
272 if (ret) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100273 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Failed to parse unit ID '%s'\n", unitid);
Vadim Yanitskiy1c94f6a2019-12-02 02:16:07 +0700274 goto err;
275 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200276
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200277 if (!line->ops->sign_link_up) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100278 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Unable to set signal link, closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200279 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200280 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200281 /* the BSC creates the new sign links at this stage. */
282 if (bfd->priv_nr == E1INP_SIGN_OML) {
283 sign_link =
284 line->ops->sign_link_up(&unit_data, line,
285 E1INP_SIGN_OML);
286 if (sign_link == NULL) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100287 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Unable to set signal link, closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200288 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200289 }
Eric Wildef1f3272019-07-10 18:10:31 +0200290
291 ipaccess_bsc_keepalive_fsm_alloc(e1i_ts, bfd, "oml_bsc_to_bts");
292
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200293 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200294 struct e1inp_ts *ts;
Eric Wildef1f3272019-07-10 18:10:31 +0200295 struct osmo_fd *newbfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200296 struct e1inp_line *new_line;
Michael Iedemaee2589e2022-05-03 07:27:38 -0700297 char tcp_stat_name[64];
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200298
299 sign_link =
300 line->ops->sign_link_up(&unit_data, line,
301 E1INP_SIGN_RSL);
302 if (sign_link == NULL) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100303 LOGPIL(line, DLINP, LOGL_ERROR, "Unable to set signal link, closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200304 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200305 }
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200306 /* Finally, we know which OML link is associated with
307 * this RSL link, attach it to this socket. */
308 new_line = sign_link->ts->line;
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200309 /* this is a bugtrap, the BSC should be using the
310 * virtual E1 line used by OML for this RSL link. */
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200311 if (new_line == line) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100312 LOGPIL(line, DLINP, LOGL_ERROR, "Fix your BSC, you should use the "
313 "E1 line used by the OML link for your RSL link.\n");
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200314 return 0;
315 }
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200316 e1inp_line_get2(new_line, "ipa_bfd");
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200317 ts = e1inp_line_ipa_rsl_ts(new_line, unit_data.trx_id);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200318 newbfd = &ts->driver.ipaccess.fd;
Pau Espin Pedrol815117c2020-07-14 21:19:16 +0200319 OSMO_ASSERT(newbfd != bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200320
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200321 /* preserve 'newbfd->when' flags potentially set by sign_link_up() */
Pau Espin Pedrol67902bb2018-08-23 14:35:08 +0200322 osmo_fd_setup(newbfd, bfd->fd, newbfd->when | bfd->when, bfd->cb,
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200323 new_line, E1INP_SIGN_RSL + unit_data.trx_id);
324
325
326 /* now we can release the dummy RSL line (old temporary bfd). */
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200327 osmo_fd_unregister(bfd);
328 bfd->fd = -1;
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200329 /* bfd->data holds a reference to line, drop it */
330 OSMO_ASSERT(bfd->data == line);
331 bfd->data = NULL;
332 e1inp_line_put2(line, "ipa_bfd");
333
Harald Welteae3a9932016-11-26 09:25:23 +0100334 ret = osmo_fd_register(newbfd);
335 if (ret < 0) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100336 LOGPITS(ts, DLINP, LOGL_ERROR, "could not register FD\n");
Harald Welteae3a9932016-11-26 09:25:23 +0100337 goto err;
338 }
Michael Iedemaee2589e2022-05-03 07:27:38 -0700339 snprintf(tcp_stat_name, sizeof(tcp_stat_name), "site.%u.bts.%u.ipa-rsl.%u",
340 unit_data.site_id, unit_data.bts_id, unit_data.trx_id);
Michael Iedema29667862022-02-24 09:25:20 -0800341 osmo_stats_tcp_osmo_fd_register(newbfd, tcp_stat_name);
Eric Wildef1f3272019-07-10 18:10:31 +0200342
343 e1i_ts = ipaccess_line_ts(newbfd, new_line);
344 ipaccess_bsc_keepalive_fsm_alloc(e1i_ts, newbfd, "rsl_bsc_to_bts");
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200345 return 1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200346 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200347 break;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200348 default:
Harald Weltea87de3c2024-02-21 17:41:12 +0100349 LOGPIL(line, DLINP, LOGL_ERROR, "Unknown IPA message type\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200350 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200351 }
352 return 0;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200353err:
Harald Welteb4a7db02019-07-21 11:51:56 +0200354 if (bfd->fd != -1) {
Pau Espin Pedroldc55a5f2020-07-14 21:14:34 +0200355 osmo_fd_unregister(bfd);
Harald Welteb4a7db02019-07-21 11:51:56 +0200356 close(bfd->fd);
357 bfd->fd = -1;
Pau Espin Pedroldc55a5f2020-07-14 21:14:34 +0200358 /* This is a BSC accepted socket, bfd->data holds a reference to line, drop it */
359 OSMO_ASSERT(bfd->data == line);
360 bfd->data = NULL;
361 e1inp_line_put2(line, "ipa_bfd");
Harald Welteb4a7db02019-07-21 11:51:56 +0200362 }
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200363 return -1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200364}
365
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200366/* Returns -EBADF if bfd cannot be used by the caller anymore after return. */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200367static int handle_ts1_read(struct osmo_fd *bfd)
368{
369 struct e1inp_line *line = bfd->data;
370 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200371 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200372 struct e1inp_sign_link *link;
373 struct ipaccess_head *hh;
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200374 struct msgb *msg = NULL;
Maxc9fa25e2017-01-09 13:23:15 +0100375 int ret, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200376
Eric Wildef1f3272019-07-10 18:10:31 +0200377 e1i_ts = ipaccess_line_ts(bfd, line);
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200378 ret = ipa_msg_recv_buffered(bfd->fd, &msg, &e1i_ts->pending_msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200379 if (ret < 0) {
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200380 if (ret == -EAGAIN)
381 return 0;
Harald Weltea87de3c2024-02-21 17:41:12 +0100382 LOGPITS(e1i_ts, DLINP, LOGL_NOTICE, "Sign link problems, closing socket. Reason: %s\n",
383 strerror(-ret));
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200384 goto err;
385 } else if (ret == 0) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100386 LOGPITS(e1i_ts, DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200387 goto err;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200388 }
Harald Weltea87de3c2024-02-21 17:41:12 +0100389 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200390
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200391 hh = (struct ipaccess_head *) msg->data;
392 if (hh->proto == IPAC_PROTO_IPACCESS) {
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200393 ret = ipaccess_rcvmsg(line, msg, bfd);
394 /* BIG FAT WARNING: bfd might no longer exist here (ret != 0),
395 * since ipaccess_rcvmsg() might have free'd it !!! */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200396 msgb_free(msg);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200397 return ret != 0 ? -EBADF : 0;
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100398 } else if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
399 /* this sign link is not know yet.. complain. */
Harald Weltea87de3c2024-02-21 17:41:12 +0100400 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Timeslot is not configured.\n");
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100401 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200402 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200403
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200404 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
405 if (!link) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100406 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "no matching signalling link for hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200407 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200408 }
409 msg->dst = link;
410
411 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200412 if (!e1i_ts->line->ops->sign_link) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100413 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Fix your application, no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200414 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200415 }
Maxc9fa25e2017-01-09 13:23:15 +0100416 rc = e1i_ts->line->ops->sign_link(msg);
417 if (rc < 0) {
Pablo Neira Ayusoa49c24d2012-10-16 11:24:08 +0200418 /* Don't close the signalling link if the upper layers report
419 * an error, that's too strict. BTW, the signalling layer is
420 * resposible for releasing the message.
421 */
Harald Weltea87de3c2024-02-21 17:41:12 +0100422 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Bad signalling message, sign_link returned error: %s.\n",
423 strerror(-rc));
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200424 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200425
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200426 return rc;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200427err_msg:
428 msgb_free(msg);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200429err:
Harald Welte10b41302013-06-30 14:05:49 +0200430 ipaccess_drop(bfd, line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200431 return -EBADF;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200432}
433
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200434static int ts_want_write(struct e1inp_ts *e1i_ts)
435{
Harald Welte949b8a22020-10-18 23:01:53 +0200436 osmo_fd_write_enable(&e1i_ts->driver.ipaccess.fd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200437
438 return 0;
439}
440
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200441static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200442{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200443 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200444 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Eric Wildef1f3272019-07-10 18:10:31 +0200445 struct e1inp_line *line = e1i_ts->line;
Pau Espin Pedrol3d3e5af2022-09-16 19:10:41 +0200446 struct osmo_fsm_inst *ka_fsm = e1i_ts->driver.ipaccess.ka_fsm;
Eric Wildef1f3272019-07-10 18:10:31 +0200447
Pau Espin Pedrol3d3e5af2022-09-16 19:10:41 +0200448 /* depending on caller the fsm might be dead */
449 if (ka_fsm)
450 ipa_keepalive_fsm_stop(ka_fsm);
Eric Wildef1f3272019-07-10 18:10:31 +0200451
Pau Espin Pedrol8737ad42020-07-14 21:11:56 +0200452 e1inp_int_snd_event(e1i_ts, sign_link, S_L_INP_TEI_DN);
453 /* the first e1inp_sign_link_destroy call closes the socket. */
454 if (bfd->fd != -1) {
455 osmo_fd_unregister(bfd);
456 close(bfd->fd);
457 bfd->fd = -1;
458 /* If The bfd holds a reference to e1inp_line in ->data (BSC
459 * accepted() sockets), then release it */
460 if (bfd->data == line) {
461 bfd->data = NULL;
462 e1inp_line_put2(line, "ipa_bfd");
463 }
464 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200465}
466
Pau Espin Pedrol90df0352022-05-09 12:59:35 +0200467static bool e1i_ts_has_pending_tx_msgs(struct e1inp_ts *e1i_ts)
468{
469 struct e1inp_sign_link *link;
470 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
471 if (!llist_empty(&link->tx_list)) {
472 return true;
473 }
474 }
475 return false;
476}
477
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200478static void timeout_ts1_write(void *data)
479{
480 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
481
482 /* trigger write of ts1, due to tx delay timer */
Pau Espin Pedrol90df0352022-05-09 12:59:35 +0200483 if (e1i_ts_has_pending_tx_msgs(e1i_ts))
484 ts_want_write(e1i_ts);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200485}
486
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200487static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200488{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200489 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200490 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200491 struct e1inp_sign_link *sign_link;
492 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200493 int ret;
494
Eric Wildef1f3272019-07-10 18:10:31 +0200495 e1i_ts = ipaccess_line_ts(bfd, line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200496
497 /* get the next msg for this timeslot */
498 msg = e1inp_tx_ts(e1i_ts, &sign_link);
499 if (!msg) {
500 /* no message after tx delay timer */
Harald Welte28fea772022-05-06 19:38:33 +0000501 osmo_fd_write_disable(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200502 return 0;
503 }
504
505 switch (sign_link->type) {
506 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200507 case E1INP_SIGN_RSL:
Harald Welte46fc7e22014-08-18 19:04:26 +0200508 case E1INP_SIGN_OSMO:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200509 break;
510 default:
Pau Espin Pedrold20a10c2022-05-09 12:45:07 +0200511 /* leave WRITE flag enabled, come back for more msg */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200512 ret = -EINVAL;
513 goto out;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200514 }
515
516 msg->l2h = msg->data;
Harald Welteb65f58f2014-08-20 22:04:11 +0200517 ipa_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200518
Vadim Yanitskiy77a7cf42020-11-29 20:39:57 +0100519 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "TX %u: %s\n", ts_nr,
Pau Espin Pedrol3bdf59b2020-11-23 13:47:16 +0100520 osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200521
522 ret = send(bfd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200523 if (ret != msg->len) {
Pau Espin Pedrol3bdf59b2020-11-23 13:47:16 +0100524 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling "
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200525 "message. Reason: %s\n", strerror(errno));
526 goto err;
527 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200528
Harald Welte28fea772022-05-06 19:38:33 +0000529 /* this is some ancient code that apparently exists to slow down writes towards
530 * some even more ancient nanoBTS 900 units. See git commit
531 * d49fc5ae24fc9d44d2b284392ab619cc7a69a876 of openbsc.git (now osmo-bsc.git) */
532 if (e1i_ts->sign.delay) {
533 osmo_fd_write_disable(bfd);
534 /* set tx delay timer for next event */
535 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
536 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
Pau Espin Pedrol90df0352022-05-09 12:59:35 +0200537 } else {
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200538out:
Pau Espin Pedrol90df0352022-05-09 12:59:35 +0200539 if (!e1i_ts_has_pending_tx_msgs(e1i_ts))
540 osmo_fd_write_disable(bfd);
541 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200542 msgb_free(msg);
543 return ret;
544err:
Harald Welte10b41302013-06-30 14:05:49 +0200545 ipaccess_drop(bfd, line);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200546 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200547 return ret;
548}
549
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200550static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200551{
552 struct e1inp_line *line = bfd->data;
553
554 return __handle_ts1_write(bfd, line);
555}
556
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200557static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200558{
559 struct e1inp_line *line = link->line;
560
561 return __handle_ts1_write(link->ofd, line);
562}
563
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200564/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200565int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200566{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200567 int rc = 0;
568
Harald Weltede3959e2020-10-18 22:28:50 +0200569 if (what & OSMO_FD_READ)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200570 rc = handle_ts1_read(bfd);
Harald Weltede3959e2020-10-18 22:28:50 +0200571 if (rc != -EBADF && (what & OSMO_FD_WRITE))
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200572 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200573
574 return rc;
575}
576
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200577static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200578
579struct e1inp_driver ipaccess_driver = {
580 .name = "ipa",
581 .want_write = ts_want_write,
582 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200583 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200584 .default_delay = 0,
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100585 .has_keepalive = 1,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200586};
587
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100588static void update_fd_settings(struct e1inp_line *line, int fd)
589{
590 int ret;
591 int val;
592
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100593 if (line->keepalive_num_probes) {
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100594 /* Enable TCP keepalive to find out if the connection is gone */
595 val = 1;
596 ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
597 if (ret < 0)
Harald Welte71565372024-02-21 18:14:07 +0100598 LOGPIL(line, DLINP, LOGL_ERROR, "Failed to enable TCP keepalive: %s\n", strerror(errno));
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100599 else
Harald Welte71565372024-02-21 18:14:07 +0100600 LOGPIL(line, DLINP, LOGL_NOTICE, "TCP Keepalive is enabled\n");
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100601
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100602 /* The following options are not portable! */
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100603 val = line->keepalive_idle_timeout > 0 ?
604 line->keepalive_idle_timeout :
605 DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT;
Harald Weltec14bf382024-02-21 17:51:10 +0100606 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val));
Harald Welte71565372024-02-21 18:14:07 +0100607 if (ret < 0) {
608 LOGPIL(line, DLINP, LOGL_ERROR, "Failed to set TCP keepalive idle time: %s\n",
609 strerror(errno));
610 }
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100611 val = line->keepalive_probe_interval > -1 ?
612 line->keepalive_probe_interval :
613 DEFAULT_TCP_KEEPALIVE_INTERVAL;
Harald Weltec14bf382024-02-21 17:51:10 +0100614 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val));
Harald Welte71565372024-02-21 18:14:07 +0100615 if (ret < 0) {
616 LOGPIL(line, DLINP, LOGL_ERROR, "Failed to set TCP keepalive interval: %s\n",
617 strerror(errno));
618 }
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100619 val = line->keepalive_num_probes > 0 ?
620 line->keepalive_num_probes :
621 DEFAULT_TCP_KEEPALIVE_RETRY_COUNT;
Harald Weltec14bf382024-02-21 17:51:10 +0100622 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val));
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100623 if (ret < 0)
Harald Welte71565372024-02-21 18:14:07 +0100624 LOGPIL(line, DLINP, LOGL_ERROR, "Failed to set TCP keepalive count: %s\n", strerror(errno));
Harald Welte361488f2024-02-21 18:15:59 +0100625 val = 1000 * line->keepalive_num_probes *
626 line->keepalive_probe_interval +
627 line->keepalive_idle_timeout;
Harald Weltec14bf382024-02-21 17:51:10 +0100628 ret = setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &val, sizeof(val));
Harald Welte361488f2024-02-21 18:15:59 +0100629 if (ret < 0)
Harald Welte71565372024-02-21 18:14:07 +0100630 LOGPIL(line, DLINP, LOGL_ERROR, "Failed to set TCP user timeout: %s\n", strerror(errno));
Holger Hans Peter Freytherf465a4c2014-02-03 09:34:02 +0100631 }
Oliver Smith62725d02020-04-17 10:57:46 +0200632
633 val = 1;
634 ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
635 if (ret < 0)
Harald Weltea87de3c2024-02-21 17:41:12 +0100636 LOGPIL(line, DLINP, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100637}
638
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200639/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200640static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200641{
642 int ret;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100643 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200644 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200645 struct e1inp_ts *e1i_ts;
646 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200647
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200648 /* clone virtual E1 line for this new OML link. */
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200649 line = e1inp_line_clone(tall_ipa_ctx, link->line, "ipa_bfd");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200650 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200651 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200652 return -ENOMEM;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200653 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200654
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200655 /* create virrtual E1 timeslots for signalling */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200656 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200657
658 /* initialize the fds */
659 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
660 line->ts[i].driver.ipaccess.fd.fd = -1;
661
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200662 e1i_ts = e1inp_line_ipa_oml_ts(line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200663
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200664 bfd = &e1i_ts->driver.ipaccess.fd;
Harald Weltede3959e2020-10-18 22:28:50 +0200665 osmo_fd_setup(bfd, fd, OSMO_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_OML);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200666 ret = osmo_fd_register(bfd);
667 if (ret < 0) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100668 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200669 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200670 }
Philipp Maierf5f31d32021-12-06 16:46:40 +0100671 osmo_stats_tcp_osmo_fd_register(bfd, "ipa-oml");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200672
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100673 update_fd_settings(line, bfd->fd);
Daniel Willmann85980722014-01-09 14:30:55 +0100674
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200675 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200676 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200677 if (ret < 0) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100678 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n", strerror(errno));
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200679 goto err_socket;
680 }
681 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200682
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200683err_socket:
684 osmo_fd_unregister(bfd);
685err_line:
686 close(bfd->fd);
687 bfd->fd = -1;
Pau Espin Pedrola2ff7332020-07-14 21:12:50 +0200688 bfd->data = NULL;
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200689 e1inp_line_put2(line, "ipa_bfd");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200690 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200691}
692
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200693static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200694{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200695 struct e1inp_line *line;
696 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200697 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200698 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200699
Harald Welte361488f2024-02-21 18:15:59 +0100700 /* We don't know yet which OML link to associate it with. Thus, we
701 * allocate a temporary E1 line until we have received ID. */
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200702 line = e1inp_line_clone(tall_ipa_ctx, link->line, "ipa_bfd");
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200703 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200704 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200705 return -ENOMEM;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200706 }
707 /* initialize the fds */
708 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
709 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200710
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200711 /* we need this to initialize this in case to avoid crashes in case
712 * that the socket is closed before we've seen an ID_RESP. */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200713 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200714
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200715 e1i_ts = e1inp_line_ipa_rsl_ts(line, 0);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200716
717 bfd = &e1i_ts->driver.ipaccess.fd;
Harald Weltede3959e2020-10-18 22:28:50 +0200718 osmo_fd_setup(bfd, fd, OSMO_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_RSL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200719 ret = osmo_fd_register(bfd);
720 if (ret < 0) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100721 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200722 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200723 }
Philipp Maierf5f31d32021-12-06 16:46:40 +0100724 osmo_stats_tcp_osmo_fd_register(bfd, "ipa-rsl");
725
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200726 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200727 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200728 if (ret < 0) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100729 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n", strerror(errno));
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200730 goto err_socket;
731 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100732 update_fd_settings(line, bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200733 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200734
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200735err_socket:
736 osmo_fd_unregister(bfd);
737err_line:
738 close(bfd->fd);
739 bfd->fd = -1;
Pau Espin Pedrola2ff7332020-07-14 21:12:50 +0200740 bfd->data = NULL;
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200741 e1inp_line_put2(line, "ipa_bfd");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200742 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200743}
744
Harald Weltee416e2e2017-05-26 13:11:59 +0200745#define IPA_STRING_MAX 64
746
747static struct msgb *
Max71393522018-01-08 15:42:00 +0100748ipa_bts_id_resp(const struct ipaccess_unit *dev, uint8_t *data, int len, int trx_nr)
Harald Weltee416e2e2017-05-26 13:11:59 +0200749{
750 struct msgb *nmsg;
751 char str[IPA_STRING_MAX];
752 uint8_t *tag;
753
754 memset(str, 0, sizeof(str));
755
756 nmsg = ipa_msg_alloc(0);
757 if (!nmsg)
758 return NULL;
759
760 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
761 while (len) {
762 if (len < 2) {
763 LOGP(DLINP, LOGL_NOTICE,
764 "Short read of ipaccess tag\n");
765 msgb_free(nmsg);
766 return NULL;
767 }
768 switch (data[1]) {
769 case IPAC_IDTAG_UNIT:
770 snprintf(str, sizeof(str), "%u/%u/%u",
771 dev->site_id, dev->bts_id, trx_nr);
772 break;
773 case IPAC_IDTAG_MACADDR:
774 snprintf(str, sizeof(str),
775 "%02x:%02x:%02x:%02x:%02x:%02x",
776 dev->mac_addr[0], dev->mac_addr[1],
777 dev->mac_addr[2], dev->mac_addr[3],
778 dev->mac_addr[4], dev->mac_addr[5]);
779 break;
780 case IPAC_IDTAG_LOCATION1:
781 if (dev->location1)
Stefan Sperling961776a2018-12-06 13:09:48 +0100782 osmo_strlcpy(str, dev->location1, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200783 break;
784 case IPAC_IDTAG_LOCATION2:
785 if (dev->location2)
Stefan Sperling961776a2018-12-06 13:09:48 +0100786 osmo_strlcpy(str, dev->location2, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200787 break;
788 case IPAC_IDTAG_EQUIPVERS:
789 if (dev->equipvers)
Stefan Sperling961776a2018-12-06 13:09:48 +0100790 osmo_strlcpy(str, dev->equipvers, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200791 break;
792 case IPAC_IDTAG_SWVERSION:
793 if (dev->swversion)
Neels Hofmeyr4c57eef2018-07-26 17:28:24 +0200794 osmo_strlcpy(str, dev->swversion, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200795 break;
796 case IPAC_IDTAG_UNITNAME:
797 snprintf(str, sizeof(str),
798 "%s-%02x-%02x-%02x-%02x-%02x-%02x",
799 dev->unit_name,
800 dev->mac_addr[0], dev->mac_addr[1],
801 dev->mac_addr[2], dev->mac_addr[3],
802 dev->mac_addr[4], dev->mac_addr[5]);
803 break;
804 case IPAC_IDTAG_SERNR:
805 if (dev->serno)
Stefan Sperling961776a2018-12-06 13:09:48 +0100806 osmo_strlcpy(str, dev->serno, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200807 break;
808 default:
809 LOGP(DLINP, LOGL_NOTICE,
810 "Unknown ipaccess tag 0x%02x\n", *data);
811 msgb_free(nmsg);
812 return NULL;
813 }
Harald Weltee416e2e2017-05-26 13:11:59 +0200814
815 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
816 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
817 tag[0] = 0x00;
818 tag[1] = 1 + strlen(str) + 1;
819 tag[2] = data[1];
820 memcpy(tag + 3, str, strlen(str) + 1);
821 data += 2;
822 len -= 2;
823 }
824 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
825 return nmsg;
826}
827
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200828static struct msgb *ipa_bts_id_ack(void)
829{
830 struct msgb *nmsg2;
831
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200832 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200833 if (!nmsg2)
834 return NULL;
835
836 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200837 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200838
839 return nmsg2;
840}
841
Harald Welte51de9ca2013-06-30 20:13:16 +0200842static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up)
843{
844 struct e1inp_line *line = link->line;
Daniel Willmann8e380942022-12-13 19:15:15 +0100845 struct e1inp_ts *e1i_ts = ipaccess_line_ts(link->ofd, line);
Harald Welte51de9ca2013-06-30 20:13:16 +0200846
Eric Wildef1f3272019-07-10 18:10:31 +0200847 if (up) {
Daniel Willmann8e380942022-12-13 19:15:15 +0100848 struct osmo_fsm_inst *ka_fsm = e1i_ts->driver.ipaccess.ka_fsm;
Eric Wildef1f3272019-07-10 18:10:31 +0200849
850 update_fd_settings(line, link->ofd->fd);
851 if (ka_fsm && line->ipa_kap)
852 ipa_keepalive_fsm_start(ka_fsm);
853 return;
854 }
Harald Welte51de9ca2013-06-30 20:13:16 +0200855
Daniel Willmann8e380942022-12-13 19:15:15 +0100856 ipaccess_keepalive_fsm_cleanup(e1i_ts);
Harald Welte51de9ca2013-06-30 20:13:16 +0200857 if (line->ops->sign_link_down)
858 line->ops->sign_link_down(line);
859}
860
Harald Welte783715b2014-08-17 18:24:51 +0200861/* handle incoming message to BTS, check if it is an IPA CCM, and if yes,
862 * handle it accordingly (PING/PONG/ID_REQ/ID_RESP/ID_ACK) */
863int ipaccess_bts_handle_ccm(struct ipa_client_conn *link,
864 struct ipaccess_unit *dev, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200865{
866 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200867 struct msgb *rmsg;
868 int ret = 0;
Eric Wildef1f3272019-07-10 18:10:31 +0200869 /* line might not exist if != bsc||bts */
870 struct e1inp_line *line = link->line;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200871
872 /* special handling for IPA CCM. */
873 if (hh->proto == IPAC_PROTO_IPACCESS) {
874 uint8_t msg_type = *(msg->l2h);
Eric Wildef1f3272019-07-10 18:10:31 +0200875 struct osmo_fsm_inst* ka_fsm = NULL;
876
877 /* peek the pong for our keepalive fsm */
878 if (line && msg_type == IPAC_MSGT_PONG) {
879 ka_fsm = ipaccess_line_ts(link->ofd, line)->driver.ipaccess.ka_fsm;
880 ipa_keepalive_fsm_pong_received(ka_fsm);
881 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200882
883 /* ping, pong and acknowledgment cases. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200884 ret = ipa_ccm_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200885 if (ret < 0)
886 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200887
888 /* this is a request for identification from the BSC. */
889 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200890 uint8_t *data = msgb_l2(msg);
891 int len = msgb_l2len(msg);
Andreas Eversbergf422a752014-01-21 14:54:41 +0100892 int trx_nr = 0;
893
894 if (link->ofd->priv_nr >= E1INP_SIGN_RSL)
895 trx_nr = link->ofd->priv_nr - E1INP_SIGN_RSL;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200896
Harald Weltea87de3c2024-02-21 17:41:12 +0100897 LOGPIL(line, DLINP, LOGL_NOTICE, "received ID_GET for unit ID %u/%u/%u\n",
898 dev->site_id, dev->bts_id, trx_nr);
Harald Weltee416e2e2017-05-26 13:11:59 +0200899 rmsg = ipa_bts_id_resp(dev, data + 1, len - 1, trx_nr);
Harald Welteb65f58f2014-08-20 22:04:11 +0200900 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200901 if (ret != rmsg->len) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100902 LOGPIL(line, DLINP, LOGL_ERROR, "cannot send ID_RESP message. Reason: %s\n",
903 strerror(errno));
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200904 goto err_rmsg;
905 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200906 msgb_free(rmsg);
907
908 /* send ID_ACK. */
909 rmsg = ipa_bts_id_ack();
Harald Welteb65f58f2014-08-20 22:04:11 +0200910 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200911 if (ret != rmsg->len) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100912 LOGPIL(line, DLINP, LOGL_ERROR, "cannot send ID_ACK message. Reason: %s\n",
913 strerror(errno));
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200914 goto err_rmsg;
915 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200916 msgb_free(rmsg);
Harald Welte783715b2014-08-17 18:24:51 +0200917 }
918 return 1;
919 }
Harald Welte6eddd472013-06-30 20:18:53 +0200920
Harald Welte783715b2014-08-17 18:24:51 +0200921 return 0;
922
923err_rmsg:
924 msgb_free(rmsg);
925err:
926 ipa_client_conn_close(link);
927 return -1;
928}
929
930static int ipaccess_bts_read_cb(struct ipa_client_conn *link, struct msgb *msg)
931{
932 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
933 struct e1inp_ts *e1i_ts = NULL;
934 struct e1inp_sign_link *sign_link;
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200935 uint8_t msg_type = *(msg->l2h);
Harald Welte783715b2014-08-17 18:24:51 +0200936 int ret = 0;
937
938 /* special handling for IPA CCM. */
939 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200940 /* this is a request for identification from the BSC. */
941 if (msg_type == IPAC_MSGT_ID_GET) {
942 if (!link->line->ops->sign_link_up) {
943 LOGP(DLINP, LOGL_ERROR,
944 "Unable to set signal link, "
945 "closing socket.\n");
Harald Welte783715b2014-08-17 18:24:51 +0200946 goto err;
947 }
948 }
949 }
950
951 /* core CCM handling */
952 ret = ipaccess_bts_handle_ccm(link, link->line->ops->cfg.ipa.dev, msg);
953 if (ret < 0)
954 goto err;
955
956 if (ret == 1 && hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200957 if (msg_type == IPAC_MSGT_ID_GET) {
Harald Welte6eddd472013-06-30 20:18:53 +0200958 sign_link = link->line->ops->sign_link_up(msg,
959 link->line,
960 link->ofd->priv_nr);
961 if (sign_link == NULL) {
962 LOGP(DLINP, LOGL_ERROR,
963 "Unable to set signal link, "
964 "closing socket.\n");
Harald Welte6eddd472013-06-30 20:18:53 +0200965 goto err;
966 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200967 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200968 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200969 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200970 } else if (link->port == IPA_TCP_PORT_OML)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200971 e1i_ts = e1inp_line_ipa_oml_ts(link->line);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200972 else if (link->port == IPA_TCP_PORT_RSL)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200973 e1i_ts = e1inp_line_ipa_rsl_ts(link->line, link->ofd->priv_nr - E1INP_SIGN_RSL);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200974
Pablo Neira Ayusob9487012013-07-05 14:38:43 +0200975 OSMO_ASSERT(e1i_ts != NULL);
976
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200977 if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100978 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Signalling link not initialized. Discarding."
979 " port=%u msg_type=%u\n", link->port, msg_type);
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200980 goto err;
981 }
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +0200982
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200983 /* look up for some existing signaling link. */
984 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
985 if (sign_link == NULL) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100986 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200987 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200988 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200989 }
990 msg->dst = sign_link;
991
992 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200993 if (!link->line->ops->sign_link) {
Harald Weltea87de3c2024-02-21 17:41:12 +0100994 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200995 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200996 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200997 }
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200998 return link->line->ops->sign_link(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200999
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +02001000err:
Harald Welte783715b2014-08-17 18:24:51 +02001001 ipa_client_conn_close(link);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +02001002 msgb_free(msg);
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +02001003 return -EBADF;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +02001004}
1005
Pablo Neira Ayusod6216402011-08-31 16:47:44 +02001006struct ipaccess_line {
Oliver Smith65ae42c2020-01-22 11:24:52 +01001007 bool line_already_initialized;
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001008 struct ipa_client_conn *ipa_cli[NUM_E1_TS]; /* 0=OML, 1+N=TRX_N */
Pablo Neira Ayusod6216402011-08-31 16:47:44 +02001009};
1010
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001011static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001012{
1013 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +02001014 struct ipaccess_line *il;
1015
1016 if (!line->driver_data)
1017 line->driver_data = talloc_zero(line, struct ipaccess_line);
1018
1019 if (!line->driver_data) {
Harald Weltea87de3c2024-02-21 17:41:12 +01001020 LOGPIL(line, DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
Pablo Neira Ayusod6216402011-08-31 16:47:44 +02001021 return -ENOMEM;
1022 }
1023 il = line->driver_data;
1024
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001025 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001026 case E1INP_LINE_R_BSC: {
Pau Espin Pedrold2d28d82021-09-29 14:15:56 +02001027 /* We only initialize this line once. */
1028 if (il->line_already_initialized)
1029 return 0;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001030 struct ipa_server_link *oml_link, *rsl_link;
Max4c4a1c22016-12-30 15:10:47 +01001031 const char *ipa = e1inp_ipa_get_bind_addr();
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001032
Harald Weltea87de3c2024-02-21 17:41:12 +01001033 LOGPIL(line, DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode on %s "
1034 "with OML %u and RSL %u TCP ports\n", ipa, IPA_TCP_PORT_OML, IPA_TCP_PORT_RSL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001035
Max4c4a1c22016-12-30 15:10:47 +01001036 oml_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001037 IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +02001038 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001039 if (oml_link == NULL) {
Harald Weltea87de3c2024-02-21 17:41:12 +01001040 LOGPIL(line, DLINP, LOGL_ERROR, "cannot create OML BSC link: %s\n", strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001041 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +02001042 }
Harald Welte41547552021-04-28 18:23:21 +02001043 oml_link->dscp = g_e1inp_ipaccess_pars.oml.dscp;
1044 oml_link->priority = g_e1inp_ipaccess_pars.oml.priority;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001045 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltea87de3c2024-02-21 17:41:12 +01001046 LOGPIL(line, DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n", strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001047 ipa_server_link_destroy(oml_link);
1048 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +02001049 }
Max4c4a1c22016-12-30 15:10:47 +01001050 rsl_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001051 IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +02001052 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001053 if (rsl_link == NULL) {
Harald Weltea87de3c2024-02-21 17:41:12 +01001054 LOGPIL(line, DLINP, LOGL_ERROR, "cannot create RSL BSC link: %s\n", strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001055 return -ENOMEM;
1056 }
Harald Welte41547552021-04-28 18:23:21 +02001057 rsl_link->dscp = g_e1inp_ipaccess_pars.rsl.dscp;
1058 rsl_link->priority = g_e1inp_ipaccess_pars.rsl.priority;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001059 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltea87de3c2024-02-21 17:41:12 +01001060 LOGPIL(line, DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n", strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001061 ipa_server_link_destroy(rsl_link);
1062 return -EIO;
1063 }
1064 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001065 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001066 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001067 case E1INP_LINE_R_BTS: {
Harald Welte84f67b22013-06-30 13:13:59 +02001068 struct ipa_client_conn *link;
Daniel Willmann8e380942022-12-13 19:15:15 +01001069 struct e1inp_ts *e1i_ts = e1inp_line_ipa_oml_ts(line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001070
Harald Weltea87de3c2024-02-21 17:41:12 +01001071 LOGPITS(e1i_ts, DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode, "
1072 "OML connecting to %s:%u\n", line->ops->cfg.ipa.addr, IPA_TCP_PORT_OML);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001073
Pau Espin Pedrold2d28d82021-09-29 14:15:56 +02001074 /* Drop previous line */
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001075 if (il->ipa_cli[0]) {
1076 ipa_client_conn_close(il->ipa_cli[0]);
Daniel Willmann8e380942022-12-13 19:15:15 +01001077 ipaccess_keepalive_fsm_cleanup(e1i_ts);
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001078 ipa_client_conn_destroy(il->ipa_cli[0]);
1079 il->ipa_cli[0] = NULL;
Pau Espin Pedrold2d28d82021-09-29 14:15:56 +02001080 }
1081
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001082 link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +02001083 e1inp_line_ipa_oml_ts(line),
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +02001084 E1INP_SIGN_OML,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001085 NULL, 0,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001086 line->ops->cfg.ipa.addr,
1087 IPA_TCP_PORT_OML,
Harald Welte51de9ca2013-06-30 20:13:16 +02001088 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001089 ipaccess_bts_read_cb,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +02001090 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001091 line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001092 if (link == NULL) {
Harald Weltea87de3c2024-02-21 17:41:12 +01001093 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "cannot create OML BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001094 return -ENOMEM;
1095 }
Harald Welte41547552021-04-28 18:23:21 +02001096 link->dscp = g_e1inp_ipaccess_pars.oml.dscp;
1097 link->priority = g_e1inp_ipaccess_pars.oml.priority;
Daniel Willmannc20af052022-12-15 11:50:34 +01001098 if (ipa_client_conn_open2(link, line->connect_timeout) < 0) {
Harald Weltea87de3c2024-02-21 17:41:12 +01001099 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n", strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001100 ipa_client_conn_close(link);
1101 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001102 return -EIO;
1103 }
Eric Wildef1f3272019-07-10 18:10:31 +02001104
Eric Wildef1f3272019-07-10 18:10:31 +02001105 ipaccess_bts_keepalive_fsm_alloc(e1i_ts, link, "oml_bts_to_bsc");
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001106 il->ipa_cli[0] = link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001107 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001108 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001109 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001110 default:
1111 break;
1112 }
Philipp Maierea0f1bd2021-05-20 21:01:23 +02001113
1114 il->line_already_initialized = true;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001115 return ret;
1116}
1117
Andreas Eversbergf422a752014-01-21 14:54:41 +01001118/* backwards compatibility */
Harald Welte84f67b22013-06-30 13:13:59 +02001119int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line,
1120 const char *rem_addr, uint16_t rem_port)
1121{
Andreas Eversbergf422a752014-01-21 14:54:41 +01001122 return e1inp_ipa_bts_rsl_connect_n(line, rem_addr, rem_port, 0);
1123}
1124
1125int e1inp_ipa_bts_rsl_connect_n(struct e1inp_line *line,
1126 const char *rem_addr, uint16_t rem_port,
1127 uint8_t trx_nr)
1128{
Harald Welte84f67b22013-06-30 13:13:59 +02001129 struct ipa_client_conn *rsl_link;
Eric Wildef1f3272019-07-10 18:10:31 +02001130 struct e1inp_ts *e1i_ts = e1inp_line_ipa_rsl_ts(line, trx_nr);
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001131 struct ipaccess_line *il;
Pau Espin Pedrol57a148f2022-09-15 18:54:21 +02001132 int rc;
Harald Welte84f67b22013-06-30 13:13:59 +02001133
Andreas Eversbergf422a752014-01-21 14:54:41 +01001134 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
Harald Weltea87de3c2024-02-21 17:41:12 +01001135 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "cannot create RSL BTS link: "
Andreas Eversbergf422a752014-01-21 14:54:41 +01001136 "trx_nr (%d) out of range\n", trx_nr);
1137 return -EINVAL;
1138 }
Andreas Eversbergf422a752014-01-21 14:54:41 +01001139
Pau Espin Pedrol57a148f2022-09-15 18:54:21 +02001140 /* Drop previous line */
1141 if ((rc = e1inp_ipa_bts_rsl_close_n(line, trx_nr)) < 0)
1142 return rc;
1143
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001144 if (!line->driver_data)
1145 line->driver_data = talloc_zero(line, struct ipaccess_line);
1146 il = line->driver_data;
1147
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001148 rsl_link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +02001149 e1inp_line_ipa_rsl_ts(line, trx_nr),
Andreas Eversbergf422a752014-01-21 14:54:41 +01001150 E1INP_SIGN_RSL+trx_nr,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001151 NULL, 0,
Harald Welte84f67b22013-06-30 13:13:59 +02001152 rem_addr, rem_port,
Harald Welte51de9ca2013-06-30 20:13:16 +02001153 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001154 ipaccess_bts_read_cb,
Harald Welte84f67b22013-06-30 13:13:59 +02001155 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001156 line);
Harald Welte84f67b22013-06-30 13:13:59 +02001157 if (rsl_link == NULL) {
Harald Weltea87de3c2024-02-21 17:41:12 +01001158 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "cannot create RSL BTS link: %s\n", strerror(errno));
Harald Welte84f67b22013-06-30 13:13:59 +02001159 return -ENOMEM;
1160 }
Harald Welte41547552021-04-28 18:23:21 +02001161 rsl_link->dscp = g_e1inp_ipaccess_pars.rsl.dscp;
1162 rsl_link->priority = g_e1inp_ipaccess_pars.rsl.priority;
Daniel Willmannc20af052022-12-15 11:50:34 +01001163 if (ipa_client_conn_open2(rsl_link, line->connect_timeout) < 0) {
Harald Weltea87de3c2024-02-21 17:41:12 +01001164 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n", strerror(errno));
Harald Welte84f67b22013-06-30 13:13:59 +02001165 ipa_client_conn_close(rsl_link);
1166 ipa_client_conn_destroy(rsl_link);
1167 return -EIO;
1168 }
Eric Wildef1f3272019-07-10 18:10:31 +02001169 ipaccess_bts_keepalive_fsm_alloc(e1i_ts, rsl_link, "rsl_bts_to_bsc");
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001170 il->ipa_cli[1 + trx_nr] = rsl_link;
Harald Welte84f67b22013-06-30 13:13:59 +02001171 return 0;
1172}
1173
Pau Espin Pedrol57a148f2022-09-15 18:54:21 +02001174/* Close the underlying IPA TCP socket of an RSL link */
1175int e1inp_ipa_bts_rsl_close_n(struct e1inp_line *line, uint8_t trx_nr)
1176{
Vadim Yanitskiy0de8a852023-05-05 21:37:12 +07001177 struct ipa_client_conn *conn;
Pau Espin Pedrol57a148f2022-09-15 18:54:21 +02001178 struct ipaccess_line *il;
Vadim Yanitskiyf6bde0f2023-05-05 22:28:46 +07001179 struct e1inp_ts *e1i_ts;
Pau Espin Pedrol57a148f2022-09-15 18:54:21 +02001180
1181 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
Harald Weltea87de3c2024-02-21 17:41:12 +01001182 LOGPIL(line, DLINP, LOGL_ERROR, "cannot close RSL BTS link: trx_nr (%d) out of range\n", trx_nr);
Pau Espin Pedrol57a148f2022-09-15 18:54:21 +02001183 return -EINVAL;
1184 }
1185
1186 il = line->driver_data;
1187 if (!il)
1188 return 0; /* Nothing to do, no lines created */
1189
Vadim Yanitskiyf6bde0f2023-05-05 22:28:46 +07001190 e1i_ts = e1inp_line_ipa_rsl_ts(line, trx_nr);
1191 ipaccess_keepalive_fsm_cleanup(e1i_ts);
1192
Vadim Yanitskiy0de8a852023-05-05 21:37:12 +07001193 conn = il->ipa_cli[1 + trx_nr];
1194 if (conn != NULL) {
1195 ipa_client_conn_close(conn);
1196 ipa_client_conn_destroy(conn);
Pau Espin Pedrol57a148f2022-09-15 18:54:21 +02001197 il->ipa_cli[1 + trx_nr] = NULL;
1198 }
1199 return 0;
1200}
1201
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001202void e1inp_ipaccess_init(void)
1203{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +02001204 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001205 e1inp_driver_register(&ipaccess_driver);
1206}
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001207
1208void e1inp_ipa_set_bind_addr(const char *ip_bind_addr)
1209{
1210 talloc_free((char*)ipaccess_driver.bind_addr);
1211 ipaccess_driver.bind_addr = NULL;
1212
1213 if (ip_bind_addr)
1214 ipaccess_driver.bind_addr = talloc_strdup(tall_ipa_ctx,
1215 ip_bind_addr);
1216}
1217
1218const char *e1inp_ipa_get_bind_addr(void)
1219{
1220 return ipaccess_driver.bind_addr?
1221 ipaccess_driver.bind_addr
1222 : "0.0.0.0";
1223}