blob: 1b8d8d04bc490e1a417e74012a8fec04ff1914b3 [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>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020054
Harald Welte41547552021-04-28 18:23:21 +020055/* global parameters of IPA input driver */
56struct ipa_pars g_e1inp_ipaccess_pars;
57
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +020058static void *tall_ipa_ctx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020059
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020060#define TS1_ALLOC_SIZE 900
61
Daniel Willmann85980722014-01-09 14:30:55 +010062#define DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT 30
63#define DEFAULT_TCP_KEEPALIVE_INTERVAL 3
64#define DEFAULT_TCP_KEEPALIVE_RETRY_COUNT 10
65
Eric Wildef1f3272019-07-10 18:10:31 +020066static inline struct e1inp_ts *ipaccess_line_ts(struct osmo_fd *bfd, struct e1inp_line *line)
67{
68 if (bfd->priv_nr == E1INP_SIGN_OML)
69 return e1inp_line_ipa_oml_ts(line);
70 else
71 return e1inp_line_ipa_rsl_ts(line, bfd->priv_nr - E1INP_SIGN_RSL);
72}
73
74static inline void ipaccess_keepalive_fsm_cleanup(struct e1inp_ts *e1i_ts)
75{
76 struct osmo_fsm_inst *ka_fsm;
77
78 ka_fsm = e1i_ts->driver.ipaccess.ka_fsm;
79 if (ka_fsm) {
80 ipa_keepalive_fsm_stop(ka_fsm);
81 e1i_ts->driver.ipaccess.ka_fsm = NULL;
82 }
83}
84
Harald Welte10b41302013-06-30 14:05:49 +020085static int ipaccess_drop(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020086{
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +020087 int ret = 1;
Eric Wildef1f3272019-07-10 18:10:31 +020088 struct e1inp_ts *e1i_ts = ipaccess_line_ts(bfd, line);
Pau Espin Pedroldeb5c4f2020-07-31 12:55:01 +020089 e1inp_line_get2(line, __func__);
Eric Wildef1f3272019-07-10 18:10:31 +020090
91 ipaccess_keepalive_fsm_cleanup(e1i_ts);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020092
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020093 /* Error case: we did not see any ID_RESP yet for this socket. */
94 if (bfd->fd != -1) {
Pau Espin Pedrol91314c12020-11-23 14:45:03 +010095 LOGPITS(e1i_ts, DLINP, LOGL_NOTICE, "Forcing socket shutdown\n");
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +020096 osmo_fd_unregister(bfd);
97 close(bfd->fd);
98 bfd->fd = -1;
Pau Espin Pedrol9af069d2020-11-23 14:54:00 +010099 switch (line->ops->cfg.ipa.role) {
100 case E1INP_LINE_R_BSC:
101 /* This is BSC code, ipaccess_drop() is only called for
102 accepted() sockets, hence the bfd holds a reference to
103 e1inp_line in ->data that needs to be released */
104 OSMO_ASSERT(bfd->data == line);
105 bfd->data = NULL;
106 e1inp_line_put2(line, "ipa_bfd");
107 break;
108 case E1INP_LINE_R_BTS:
109 /* BTS code: bfd->data contains pointer to struct
110 * ipa_client_conn. Leave it alive so it reconnects.
111 */
112 break;
113 default:
114 break;
115 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200116 ret = -ENOENT;
Pau Espin Pedrol91314c12020-11-23 14:45:03 +0100117 } else {
118 LOGPITS(e1i_ts, DLINP, LOGL_ERROR,
119 "Forcing socket shutdown with no signal link set\n");
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +0200120 }
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200121
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200122 msgb_free(e1i_ts->pending_msg);
123 e1i_ts->pending_msg = NULL;
124
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200125 /* e1inp_sign_link_destroy releases the socket descriptors for us. */
126 line->ops->sign_link_down(line);
127
Pau Espin Pedroldeb5c4f2020-07-31 12:55:01 +0200128 e1inp_line_put2(line, __func__);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200129 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200130}
131
Eric Wildef1f3272019-07-10 18:10:31 +0200132static void ipa_bsc_keepalive_write_server_cb(struct osmo_fsm_inst *fi, void *conn, struct msgb *msg)
133{
134 struct osmo_fd *bfd = (struct osmo_fd *)conn;
135 write(bfd->fd, msg->data, msg->len);
136 msgb_free(msg);
137}
138
139static int ipa_bsc_keepalive_timeout_cb(struct osmo_fsm_inst *fi, void *data)
140{
141 struct osmo_fd *bfd = (struct osmo_fd *)data;
142
143 if (bfd->fd == -1)
144 return 1;
145
146 ipaccess_drop(bfd, (struct e1inp_line *)bfd->data);
147 return 1;
148}
149
150static void ipaccess_bsc_keepalive_fsm_alloc(struct e1inp_ts *e1i_ts, struct osmo_fd *bfd, const char *id)
151{
152 struct e1inp_line *line = e1i_ts->line;
153 struct osmo_fsm_inst *ka_fsm;
154
155 ipaccess_keepalive_fsm_cleanup(e1i_ts);
156 if (!line->ipa_kap)
157 return;
158
159 ka_fsm = ipa_generic_conn_alloc_keepalive_fsm(tall_ipa_ctx, bfd, line->ipa_kap, id);
160 e1i_ts->driver.ipaccess.ka_fsm = ka_fsm;
161 if (!ka_fsm)
162 return;
163
164 ipa_keepalive_fsm_set_timeout_cb(ka_fsm, ipa_bsc_keepalive_timeout_cb);
165 ipa_keepalive_fsm_set_send_cb(ka_fsm, ipa_bsc_keepalive_write_server_cb);
166 ipa_keepalive_fsm_start(ka_fsm);
167}
168
169static void ipa_bts_keepalive_write_client_cb(struct osmo_fsm_inst *fi, void *conn, struct msgb *msg) {
170 struct ipa_client_conn *link = (struct ipa_client_conn *)conn;
171 int ret = 0;
172
173 ret = ipa_send(link->ofd->fd, msg->data, msg->len);
174 if (ret != msg->len) {
175 LOGP(DLINP, LOGL_ERROR, "cannot send message. Reason: %s\n", strerror(errno));
176 }
177 msgb_free(msg);
178}
179
180static void update_fd_settings(struct e1inp_line *line, int fd);
181static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up);
182
183static int ipa_bts_keepalive_timeout_cb(struct osmo_fsm_inst *fi, void *conn) {
184 ipaccess_bts_updown_cb(conn, false);
185 return 1;
186}
187
188static void ipaccess_bts_keepalive_fsm_alloc(struct e1inp_ts *e1i_ts, struct ipa_client_conn *client, const char *id)
189{
190 struct e1inp_line *line = e1i_ts->line;
191 struct osmo_fsm_inst *ka_fsm;
192
193 ipaccess_keepalive_fsm_cleanup(e1i_ts);
194 if (!line->ipa_kap)
195 return;
196
197 ka_fsm = ipa_client_conn_alloc_keepalive_fsm(client, line->ipa_kap, id);
198 e1i_ts->driver.ipaccess.ka_fsm = ka_fsm;
199 if (!ka_fsm)
200 return;
201
202 ipa_keepalive_fsm_set_timeout_cb(ka_fsm, ipa_bts_keepalive_timeout_cb);
203 ipa_keepalive_fsm_set_send_cb(ka_fsm, ipa_bts_keepalive_write_client_cb);
204}
205
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200206/* Returns -1 on error, and 0 or 1 on success. If -1 or 1 is returned, line has
207 * been released and should not be used anymore by the caller. */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200208static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
209 struct osmo_fd *bfd)
210{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200211 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200212 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200213 struct ipaccess_unit unit_data = {};
214 struct e1inp_sign_link *sign_link;
215 char *unitid;
216 int len, ret;
Eric Wildef1f3272019-07-10 18:10:31 +0200217 struct e1inp_ts *e1i_ts;
218 struct osmo_fsm_inst *ka_fsm;
219
220 /* peek the pong for our keepalive fsm */
221 e1i_ts = ipaccess_line_ts(bfd, line);
222 ka_fsm = e1i_ts->driver.ipaccess.ka_fsm;
223 if (ka_fsm && msg_type == IPAC_MSGT_PONG)
224 ipa_keepalive_fsm_pong_received(ka_fsm);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200225
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200226 /* Handle IPA PING, PONG and ID_ACK messages. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200227 ret = ipa_ccm_rcvmsg_base(msg, bfd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200228 switch(ret) {
229 case -1:
230 /* error in IPA control message handling */
231 goto err;
232 case 1:
233 /* this is an IPA control message, skip further processing */
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200234 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200235 case 0:
236 /* this is not an IPA control message, continue */
237 break;
238 default:
239 LOGP(DLINP, LOGL_ERROR, "Unexpected return from "
Harald Welteb65f58f2014-08-20 22:04:11 +0200240 "ipa_ccm_rcvmsg_base "
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200241 "(ret=%d)\n", ret);
242 goto err;
243 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200244
245 switch (msg_type) {
246 case IPAC_MSGT_ID_RESP:
Pau Espin Pedrol30e80d52020-07-14 13:23:05 +0200247 DEBUGP(DLMI, "ID_RESP ");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200248 /* parse tags, search for Unit ID */
Harald Welte82eb55e2018-08-01 13:22:55 +0200249 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 +0200250 DEBUGPC(DLMI, "\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200251 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200252 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200253 "with malformed TLVs\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200254 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200255 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200256 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200257 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200258 "without unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200259 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200260
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200261 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200262 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200263 if (len < 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200264 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200265 "with too small unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200266 goto err;
267 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200268 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
269 unitid[len - 1] = '\0';
Vadim Yanitskiy1c94f6a2019-12-02 02:16:07 +0700270 ret = ipa_parse_unitid(unitid, &unit_data);
271 if (ret) {
272 LOGP(DLINP, LOGL_ERROR, "Failed to parse unit ID '%s'\n", unitid);
273 goto err;
274 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200275
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200276 if (!line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200277 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200278 "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 Weltecc2241b2011-07-19 16:06:06 +0200287 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200288 "Unable to set signal link, "
289 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200290 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200291 }
Eric Wildef1f3272019-07-10 18:10:31 +0200292
293 ipaccess_bsc_keepalive_fsm_alloc(e1i_ts, bfd, "oml_bsc_to_bts");
294
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200295 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200296 struct e1inp_ts *ts;
Eric Wildef1f3272019-07-10 18:10:31 +0200297 struct osmo_fd *newbfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200298 struct e1inp_line *new_line;
Michael Iedemaee2589e2022-05-03 07:27:38 -0700299 char tcp_stat_name[64];
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200300
301 sign_link =
302 line->ops->sign_link_up(&unit_data, line,
303 E1INP_SIGN_RSL);
304 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200305 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200306 "Unable to set signal link, "
307 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200308 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200309 }
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200310 /* Finally, we know which OML link is associated with
311 * this RSL link, attach it to this socket. */
312 new_line = sign_link->ts->line;
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200313 /* this is a bugtrap, the BSC should be using the
314 * virtual E1 line used by OML for this RSL link. */
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200315 if (new_line == line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200316 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200317 "Fix your BSC, you should use the "
318 "E1 line used by the OML link for "
319 "your RSL link.\n");
320 return 0;
321 }
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200322 e1inp_line_get2(new_line, "ipa_bfd");
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200323 ts = e1inp_line_ipa_rsl_ts(new_line, unit_data.trx_id);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200324 newbfd = &ts->driver.ipaccess.fd;
Pau Espin Pedrol815117c2020-07-14 21:19:16 +0200325 OSMO_ASSERT(newbfd != bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200326
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200327 /* preserve 'newbfd->when' flags potentially set by sign_link_up() */
Pau Espin Pedrol67902bb2018-08-23 14:35:08 +0200328 osmo_fd_setup(newbfd, bfd->fd, newbfd->when | bfd->when, bfd->cb,
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200329 new_line, E1INP_SIGN_RSL + unit_data.trx_id);
330
331
332 /* now we can release the dummy RSL line (old temporary bfd). */
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200333 osmo_fd_unregister(bfd);
334 bfd->fd = -1;
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200335 /* bfd->data holds a reference to line, drop it */
336 OSMO_ASSERT(bfd->data == line);
337 bfd->data = NULL;
338 e1inp_line_put2(line, "ipa_bfd");
339
Harald Welteae3a9932016-11-26 09:25:23 +0100340 ret = osmo_fd_register(newbfd);
341 if (ret < 0) {
342 LOGP(DLINP, LOGL_ERROR,
343 "could not register FD\n");
344 goto err;
345 }
Michael Iedemaee2589e2022-05-03 07:27:38 -0700346 snprintf(tcp_stat_name, sizeof(tcp_stat_name), "site.%u.bts.%u.ipa-rsl.%u",
347 unit_data.site_id, unit_data.bts_id, unit_data.trx_id);
Michael Iedema29667862022-02-24 09:25:20 -0800348 osmo_stats_tcp_osmo_fd_register(newbfd, tcp_stat_name);
Eric Wildef1f3272019-07-10 18:10:31 +0200349
350 e1i_ts = ipaccess_line_ts(newbfd, new_line);
351 ipaccess_bsc_keepalive_fsm_alloc(e1i_ts, newbfd, "rsl_bsc_to_bts");
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200352 return 1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200353 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200354 break;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200355 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200356 LOGP(DLINP, LOGL_ERROR, "Unknown IPA message type\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200357 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200358 }
359 return 0;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200360err:
Harald Welteb4a7db02019-07-21 11:51:56 +0200361 if (bfd->fd != -1) {
Pau Espin Pedroldc55a5f2020-07-14 21:14:34 +0200362 osmo_fd_unregister(bfd);
Harald Welteb4a7db02019-07-21 11:51:56 +0200363 close(bfd->fd);
364 bfd->fd = -1;
Pau Espin Pedroldc55a5f2020-07-14 21:14:34 +0200365 /* This is a BSC accepted socket, bfd->data holds a reference to line, drop it */
366 OSMO_ASSERT(bfd->data == line);
367 bfd->data = NULL;
368 e1inp_line_put2(line, "ipa_bfd");
Harald Welteb4a7db02019-07-21 11:51:56 +0200369 }
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200370 return -1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200371}
372
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200373/* Returns -EBADF if bfd cannot be used by the caller anymore after return. */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200374static int handle_ts1_read(struct osmo_fd *bfd)
375{
376 struct e1inp_line *line = bfd->data;
377 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200378 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200379 struct e1inp_sign_link *link;
380 struct ipaccess_head *hh;
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200381 struct msgb *msg = NULL;
Maxc9fa25e2017-01-09 13:23:15 +0100382 int ret, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200383
Eric Wildef1f3272019-07-10 18:10:31 +0200384 e1i_ts = ipaccess_line_ts(bfd, line);
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200385 ret = ipa_msg_recv_buffered(bfd->fd, &msg, &e1i_ts->pending_msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200386 if (ret < 0) {
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200387 if (ret == -EAGAIN)
388 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200389 LOGP(DLINP, LOGL_NOTICE, "Sign link problems, "
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200390 "closing socket. Reason: %s\n", strerror(-ret));
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200391 goto err;
392 } else if (ret == 0) {
393 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
394 goto err;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200395 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200396 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200397
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200398 hh = (struct ipaccess_head *) msg->data;
399 if (hh->proto == IPAC_PROTO_IPACCESS) {
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200400 ret = ipaccess_rcvmsg(line, msg, bfd);
401 /* BIG FAT WARNING: bfd might no longer exist here (ret != 0),
402 * since ipaccess_rcvmsg() might have free'd it !!! */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200403 msgb_free(msg);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200404 return ret != 0 ? -EBADF : 0;
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100405 } else if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
406 /* this sign link is not know yet.. complain. */
407 LOGP(DLINP, LOGL_ERROR, "Timeslot is not configured.\n");
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100408 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200409 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200410
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200411 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
412 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200413 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200414 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200415 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200416 }
417 msg->dst = link;
418
419 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200420 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200421 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200422 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200423 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200424 }
Maxc9fa25e2017-01-09 13:23:15 +0100425 rc = e1i_ts->line->ops->sign_link(msg);
426 if (rc < 0) {
Pablo Neira Ayusoa49c24d2012-10-16 11:24:08 +0200427 /* Don't close the signalling link if the upper layers report
428 * an error, that's too strict. BTW, the signalling layer is
429 * resposible for releasing the message.
430 */
Harald Weltecc2241b2011-07-19 16:06:06 +0200431 LOGP(DLINP, LOGL_ERROR, "Bad signalling message,"
Maxc9fa25e2017-01-09 13:23:15 +0100432 " sign_link returned error: %s.\n", strerror(-rc));
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200433 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200434
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200435 return rc;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200436err_msg:
437 msgb_free(msg);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200438err:
Harald Welte10b41302013-06-30 14:05:49 +0200439 ipaccess_drop(bfd, line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200440 return -EBADF;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200441}
442
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200443static int ts_want_write(struct e1inp_ts *e1i_ts)
444{
Harald Welte949b8a22020-10-18 23:01:53 +0200445 osmo_fd_write_enable(&e1i_ts->driver.ipaccess.fd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200446
447 return 0;
448}
449
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200450static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200451{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200452 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200453 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Eric Wildef1f3272019-07-10 18:10:31 +0200454 struct e1inp_line *line = e1i_ts->line;
455
456 /* line might not exist if != bsc||bts */
457 if (line) {
458 /* depending on caller the fsm might be dead */
459 struct osmo_fsm_inst* ka_fsm = ipaccess_line_ts(bfd, line)->driver.ipaccess.ka_fsm;
460 if (ka_fsm)
461 ipa_keepalive_fsm_stop(ka_fsm);
462
463 }
464
Pau Espin Pedrol8737ad42020-07-14 21:11:56 +0200465 e1inp_int_snd_event(e1i_ts, sign_link, S_L_INP_TEI_DN);
466 /* the first e1inp_sign_link_destroy call closes the socket. */
467 if (bfd->fd != -1) {
468 osmo_fd_unregister(bfd);
469 close(bfd->fd);
470 bfd->fd = -1;
471 /* If The bfd holds a reference to e1inp_line in ->data (BSC
472 * accepted() sockets), then release it */
473 if (bfd->data == line) {
474 bfd->data = NULL;
475 e1inp_line_put2(line, "ipa_bfd");
476 }
477 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200478}
479
Pau Espin Pedrol90df0352022-05-09 12:59:35 +0200480static bool e1i_ts_has_pending_tx_msgs(struct e1inp_ts *e1i_ts)
481{
482 struct e1inp_sign_link *link;
483 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
484 if (!llist_empty(&link->tx_list)) {
485 return true;
486 }
487 }
488 return false;
489}
490
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200491static void timeout_ts1_write(void *data)
492{
493 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
494
495 /* trigger write of ts1, due to tx delay timer */
Pau Espin Pedrol90df0352022-05-09 12:59:35 +0200496 if (e1i_ts_has_pending_tx_msgs(e1i_ts))
497 ts_want_write(e1i_ts);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200498}
499
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200500static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200501{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200502 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200503 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200504 struct e1inp_sign_link *sign_link;
505 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200506 int ret;
507
Eric Wildef1f3272019-07-10 18:10:31 +0200508 e1i_ts = ipaccess_line_ts(bfd, line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200509
510 /* get the next msg for this timeslot */
511 msg = e1inp_tx_ts(e1i_ts, &sign_link);
512 if (!msg) {
513 /* no message after tx delay timer */
Harald Welte28fea772022-05-06 19:38:33 +0000514 osmo_fd_write_disable(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200515 return 0;
516 }
517
518 switch (sign_link->type) {
519 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200520 case E1INP_SIGN_RSL:
Harald Welte46fc7e22014-08-18 19:04:26 +0200521 case E1INP_SIGN_OSMO:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200522 break;
523 default:
Pau Espin Pedrold20a10c2022-05-09 12:45:07 +0200524 /* leave WRITE flag enabled, come back for more msg */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200525 ret = -EINVAL;
526 goto out;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200527 }
528
529 msg->l2h = msg->data;
Harald Welteb65f58f2014-08-20 22:04:11 +0200530 ipa_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200531
Vadim Yanitskiy77a7cf42020-11-29 20:39:57 +0100532 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "TX %u: %s\n", ts_nr,
Pau Espin Pedrol3bdf59b2020-11-23 13:47:16 +0100533 osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200534
535 ret = send(bfd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200536 if (ret != msg->len) {
Pau Espin Pedrol3bdf59b2020-11-23 13:47:16 +0100537 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling "
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200538 "message. Reason: %s\n", strerror(errno));
539 goto err;
540 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200541
Harald Welte28fea772022-05-06 19:38:33 +0000542 /* this is some ancient code that apparently exists to slow down writes towards
543 * some even more ancient nanoBTS 900 units. See git commit
544 * d49fc5ae24fc9d44d2b284392ab619cc7a69a876 of openbsc.git (now osmo-bsc.git) */
545 if (e1i_ts->sign.delay) {
546 osmo_fd_write_disable(bfd);
547 /* set tx delay timer for next event */
548 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
549 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
Pau Espin Pedrol90df0352022-05-09 12:59:35 +0200550 } else {
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200551out:
Pau Espin Pedrol90df0352022-05-09 12:59:35 +0200552 if (!e1i_ts_has_pending_tx_msgs(e1i_ts))
553 osmo_fd_write_disable(bfd);
554 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200555 msgb_free(msg);
556 return ret;
557err:
Harald Welte10b41302013-06-30 14:05:49 +0200558 ipaccess_drop(bfd, line);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200559 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200560 return ret;
561}
562
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200563static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200564{
565 struct e1inp_line *line = bfd->data;
566
567 return __handle_ts1_write(bfd, line);
568}
569
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200570static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200571{
572 struct e1inp_line *line = link->line;
573
574 return __handle_ts1_write(link->ofd, line);
575}
576
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200577/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200578int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200579{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200580 int rc = 0;
581
Harald Weltede3959e2020-10-18 22:28:50 +0200582 if (what & OSMO_FD_READ)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200583 rc = handle_ts1_read(bfd);
Harald Weltede3959e2020-10-18 22:28:50 +0200584 if (rc != -EBADF && (what & OSMO_FD_WRITE))
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200585 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200586
587 return rc;
588}
589
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200590static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200591
592struct e1inp_driver ipaccess_driver = {
593 .name = "ipa",
594 .want_write = ts_want_write,
595 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200596 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200597 .default_delay = 0,
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100598 .has_keepalive = 1,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200599};
600
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100601static void update_fd_settings(struct e1inp_line *line, int fd)
602{
603 int ret;
604 int val;
605
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100606 if (line->keepalive_num_probes) {
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100607 /* Enable TCP keepalive to find out if the connection is gone */
608 val = 1;
609 ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
610 if (ret < 0)
611 LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive: %s\n",
612 strerror(errno));
613 else
614 LOGP(DLINP, LOGL_NOTICE, "Keepalive is set: %i\n", ret);
615
616#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
617 /* The following options are not portable! */
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100618 val = line->keepalive_idle_timeout > 0 ?
619 line->keepalive_idle_timeout :
620 DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100621 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
622 &val, sizeof(val));
623 if (ret < 0)
624 LOGP(DLINP, LOGL_NOTICE,
625 "Failed to set keepalive idle time: %s\n",
626 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100627 val = line->keepalive_probe_interval > -1 ?
628 line->keepalive_probe_interval :
629 DEFAULT_TCP_KEEPALIVE_INTERVAL;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100630 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
631 &val, sizeof(val));
632 if (ret < 0)
633 LOGP(DLINP, LOGL_NOTICE,
634 "Failed to set keepalive interval: %s\n",
635 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100636 val = line->keepalive_num_probes > 0 ?
637 line->keepalive_num_probes :
638 DEFAULT_TCP_KEEPALIVE_RETRY_COUNT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100639 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
640 &val, sizeof(val));
641 if (ret < 0)
642 LOGP(DLINP, LOGL_NOTICE,
643 "Failed to set keepalive count: %s\n",
644 strerror(errno));
Eric Wild6eb186c2019-06-21 15:26:25 +0200645#if defined(TCP_USER_TIMEOUT)
646 val = 1000 * line->keepalive_num_probes *
647 line->keepalive_probe_interval +
648 line->keepalive_idle_timeout;
649 ret = setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
650 &val, sizeof(val));
651 if (ret < 0)
652 LOGP(DLINP, LOGL_NOTICE,
653 "Failed to set user timoeut: %s\n",
654 strerror(errno));
655#endif
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100656#endif
Holger Hans Peter Freytherf465a4c2014-02-03 09:34:02 +0100657 }
Oliver Smith62725d02020-04-17 10:57:46 +0200658
659 val = 1;
660 ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
661 if (ret < 0)
662 LOGP(DLINP, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100663}
664
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200665/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200666static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200667{
668 int ret;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100669 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200670 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200671 struct e1inp_ts *e1i_ts;
672 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200673
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200674 /* clone virtual E1 line for this new OML link. */
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200675 line = e1inp_line_clone(tall_ipa_ctx, link->line, "ipa_bfd");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200676 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200677 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200678 return -ENOMEM;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200679 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200680
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200681 /* create virrtual E1 timeslots for signalling */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200682 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200683
684 /* initialize the fds */
685 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
686 line->ts[i].driver.ipaccess.fd.fd = -1;
687
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200688 e1i_ts = e1inp_line_ipa_oml_ts(line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200689
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200690 bfd = &e1i_ts->driver.ipaccess.fd;
Harald Weltede3959e2020-10-18 22:28:50 +0200691 osmo_fd_setup(bfd, fd, OSMO_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_OML);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200692 ret = osmo_fd_register(bfd);
693 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200694 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200695 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200696 }
Philipp Maierf5f31d32021-12-06 16:46:40 +0100697 osmo_stats_tcp_osmo_fd_register(bfd, "ipa-oml");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200698
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100699 update_fd_settings(line, bfd->fd);
Daniel Willmann85980722014-01-09 14:30:55 +0100700
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200701 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200702 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200703 if (ret < 0) {
704 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
705 strerror(errno));
706 goto err_socket;
707 }
708 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200709
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200710err_socket:
711 osmo_fd_unregister(bfd);
712err_line:
713 close(bfd->fd);
714 bfd->fd = -1;
Pau Espin Pedrola2ff7332020-07-14 21:12:50 +0200715 bfd->data = NULL;
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200716 e1inp_line_put2(line, "ipa_bfd");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200717 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200718}
719
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200720static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200721{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200722 struct e1inp_line *line;
723 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200724 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200725 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200726
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200727 /* We don't know yet which OML link to associate it with. Thus, we
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200728 * allocate a temporary E1 line until we have received ID. */
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200729 line = e1inp_line_clone(tall_ipa_ctx, link->line, "ipa_bfd");
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200730 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200731 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200732 return -ENOMEM;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200733 }
734 /* initialize the fds */
735 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
736 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200737
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200738 /* we need this to initialize this in case to avoid crashes in case
739 * that the socket is closed before we've seen an ID_RESP. */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200740 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200741
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200742 e1i_ts = e1inp_line_ipa_rsl_ts(line, 0);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200743
744 bfd = &e1i_ts->driver.ipaccess.fd;
Harald Weltede3959e2020-10-18 22:28:50 +0200745 osmo_fd_setup(bfd, fd, OSMO_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_RSL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200746 ret = osmo_fd_register(bfd);
747 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200748 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200749 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200750 }
Philipp Maierf5f31d32021-12-06 16:46:40 +0100751 osmo_stats_tcp_osmo_fd_register(bfd, "ipa-rsl");
752
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200753 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200754 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200755 if (ret < 0) {
756 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
757 strerror(errno));
758 goto err_socket;
759 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100760 update_fd_settings(line, bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200761 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200762
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200763err_socket:
764 osmo_fd_unregister(bfd);
765err_line:
766 close(bfd->fd);
767 bfd->fd = -1;
Pau Espin Pedrola2ff7332020-07-14 21:12:50 +0200768 bfd->data = NULL;
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200769 e1inp_line_put2(line, "ipa_bfd");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200770 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200771}
772
Harald Weltee416e2e2017-05-26 13:11:59 +0200773#define IPA_STRING_MAX 64
774
775static struct msgb *
Max71393522018-01-08 15:42:00 +0100776ipa_bts_id_resp(const struct ipaccess_unit *dev, uint8_t *data, int len, int trx_nr)
Harald Weltee416e2e2017-05-26 13:11:59 +0200777{
778 struct msgb *nmsg;
779 char str[IPA_STRING_MAX];
780 uint8_t *tag;
781
782 memset(str, 0, sizeof(str));
783
784 nmsg = ipa_msg_alloc(0);
785 if (!nmsg)
786 return NULL;
787
788 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
789 while (len) {
790 if (len < 2) {
791 LOGP(DLINP, LOGL_NOTICE,
792 "Short read of ipaccess tag\n");
793 msgb_free(nmsg);
794 return NULL;
795 }
796 switch (data[1]) {
797 case IPAC_IDTAG_UNIT:
798 snprintf(str, sizeof(str), "%u/%u/%u",
799 dev->site_id, dev->bts_id, trx_nr);
800 break;
801 case IPAC_IDTAG_MACADDR:
802 snprintf(str, sizeof(str),
803 "%02x:%02x:%02x:%02x:%02x:%02x",
804 dev->mac_addr[0], dev->mac_addr[1],
805 dev->mac_addr[2], dev->mac_addr[3],
806 dev->mac_addr[4], dev->mac_addr[5]);
807 break;
808 case IPAC_IDTAG_LOCATION1:
809 if (dev->location1)
Stefan Sperling961776a2018-12-06 13:09:48 +0100810 osmo_strlcpy(str, dev->location1, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200811 break;
812 case IPAC_IDTAG_LOCATION2:
813 if (dev->location2)
Stefan Sperling961776a2018-12-06 13:09:48 +0100814 osmo_strlcpy(str, dev->location2, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200815 break;
816 case IPAC_IDTAG_EQUIPVERS:
817 if (dev->equipvers)
Stefan Sperling961776a2018-12-06 13:09:48 +0100818 osmo_strlcpy(str, dev->equipvers, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200819 break;
820 case IPAC_IDTAG_SWVERSION:
821 if (dev->swversion)
Neels Hofmeyr4c57eef2018-07-26 17:28:24 +0200822 osmo_strlcpy(str, dev->swversion, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200823 break;
824 case IPAC_IDTAG_UNITNAME:
825 snprintf(str, sizeof(str),
826 "%s-%02x-%02x-%02x-%02x-%02x-%02x",
827 dev->unit_name,
828 dev->mac_addr[0], dev->mac_addr[1],
829 dev->mac_addr[2], dev->mac_addr[3],
830 dev->mac_addr[4], dev->mac_addr[5]);
831 break;
832 case IPAC_IDTAG_SERNR:
833 if (dev->serno)
Stefan Sperling961776a2018-12-06 13:09:48 +0100834 osmo_strlcpy(str, dev->serno, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200835 break;
836 default:
837 LOGP(DLINP, LOGL_NOTICE,
838 "Unknown ipaccess tag 0x%02x\n", *data);
839 msgb_free(nmsg);
840 return NULL;
841 }
Harald Weltee416e2e2017-05-26 13:11:59 +0200842
843 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
844 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
845 tag[0] = 0x00;
846 tag[1] = 1 + strlen(str) + 1;
847 tag[2] = data[1];
848 memcpy(tag + 3, str, strlen(str) + 1);
849 data += 2;
850 len -= 2;
851 }
852 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
853 return nmsg;
854}
855
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200856static struct msgb *ipa_bts_id_ack(void)
857{
858 struct msgb *nmsg2;
859
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200860 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200861 if (!nmsg2)
862 return NULL;
863
864 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200865 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200866
867 return nmsg2;
868}
869
Harald Welte51de9ca2013-06-30 20:13:16 +0200870static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up)
871{
872 struct e1inp_line *line = link->line;
873
Eric Wildef1f3272019-07-10 18:10:31 +0200874 if (up) {
875 struct osmo_fsm_inst *ka_fsm = ipaccess_line_ts(link->ofd, line)->driver.ipaccess.ka_fsm;
876
877 update_fd_settings(line, link->ofd->fd);
878 if (ka_fsm && line->ipa_kap)
879 ipa_keepalive_fsm_start(ka_fsm);
880 return;
881 }
Harald Welte51de9ca2013-06-30 20:13:16 +0200882
883 if (line->ops->sign_link_down)
884 line->ops->sign_link_down(line);
885}
886
Harald Welte783715b2014-08-17 18:24:51 +0200887/* handle incoming message to BTS, check if it is an IPA CCM, and if yes,
888 * handle it accordingly (PING/PONG/ID_REQ/ID_RESP/ID_ACK) */
889int ipaccess_bts_handle_ccm(struct ipa_client_conn *link,
890 struct ipaccess_unit *dev, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200891{
892 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200893 struct msgb *rmsg;
894 int ret = 0;
Eric Wildef1f3272019-07-10 18:10:31 +0200895 /* line might not exist if != bsc||bts */
896 struct e1inp_line *line = link->line;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200897
898 /* special handling for IPA CCM. */
899 if (hh->proto == IPAC_PROTO_IPACCESS) {
900 uint8_t msg_type = *(msg->l2h);
Eric Wildef1f3272019-07-10 18:10:31 +0200901 struct osmo_fsm_inst* ka_fsm = NULL;
902
903 /* peek the pong for our keepalive fsm */
904 if (line && msg_type == IPAC_MSGT_PONG) {
905 ka_fsm = ipaccess_line_ts(link->ofd, line)->driver.ipaccess.ka_fsm;
906 ipa_keepalive_fsm_pong_received(ka_fsm);
907 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200908
909 /* ping, pong and acknowledgment cases. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200910 ret = ipa_ccm_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200911 if (ret < 0)
912 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200913
914 /* this is a request for identification from the BSC. */
915 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200916 uint8_t *data = msgb_l2(msg);
917 int len = msgb_l2len(msg);
Andreas Eversbergf422a752014-01-21 14:54:41 +0100918 int trx_nr = 0;
919
920 if (link->ofd->priv_nr >= E1INP_SIGN_RSL)
921 trx_nr = link->ofd->priv_nr - E1INP_SIGN_RSL;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200922
Vadim Yanitskiy69ae2382019-12-02 02:42:43 +0700923 LOGP(DLINP, LOGL_NOTICE, "received ID_GET for unit ID %u/%u/%u\n",
Pau Espin Pedroldd95eb62018-10-02 20:05:28 +0200924 dev->site_id, dev->bts_id, trx_nr);
Harald Weltee416e2e2017-05-26 13:11:59 +0200925 rmsg = ipa_bts_id_resp(dev, data + 1, len - 1, trx_nr);
Harald Welteb65f58f2014-08-20 22:04:11 +0200926 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200927 if (ret != rmsg->len) {
928 LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP "
929 "message. Reason: %s\n", strerror(errno));
930 goto err_rmsg;
931 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200932 msgb_free(rmsg);
933
934 /* send ID_ACK. */
935 rmsg = ipa_bts_id_ack();
Harald Welteb65f58f2014-08-20 22:04:11 +0200936 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200937 if (ret != rmsg->len) {
938 LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK "
939 "message. Reason: %s\n", strerror(errno));
940 goto err_rmsg;
941 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200942 msgb_free(rmsg);
Harald Welte783715b2014-08-17 18:24:51 +0200943 }
944 return 1;
945 }
Harald Welte6eddd472013-06-30 20:18:53 +0200946
Harald Welte783715b2014-08-17 18:24:51 +0200947 return 0;
948
949err_rmsg:
950 msgb_free(rmsg);
951err:
952 ipa_client_conn_close(link);
953 return -1;
954}
955
956static int ipaccess_bts_read_cb(struct ipa_client_conn *link, struct msgb *msg)
957{
958 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
959 struct e1inp_ts *e1i_ts = NULL;
960 struct e1inp_sign_link *sign_link;
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200961 uint8_t msg_type = *(msg->l2h);
Harald Welte783715b2014-08-17 18:24:51 +0200962 int ret = 0;
963
964 /* special handling for IPA CCM. */
965 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200966 /* this is a request for identification from the BSC. */
967 if (msg_type == IPAC_MSGT_ID_GET) {
968 if (!link->line->ops->sign_link_up) {
969 LOGP(DLINP, LOGL_ERROR,
970 "Unable to set signal link, "
971 "closing socket.\n");
Harald Welte783715b2014-08-17 18:24:51 +0200972 goto err;
973 }
974 }
975 }
976
977 /* core CCM handling */
978 ret = ipaccess_bts_handle_ccm(link, link->line->ops->cfg.ipa.dev, msg);
979 if (ret < 0)
980 goto err;
981
982 if (ret == 1 && hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200983 if (msg_type == IPAC_MSGT_ID_GET) {
Harald Welte6eddd472013-06-30 20:18:53 +0200984 sign_link = link->line->ops->sign_link_up(msg,
985 link->line,
986 link->ofd->priv_nr);
987 if (sign_link == NULL) {
988 LOGP(DLINP, LOGL_ERROR,
989 "Unable to set signal link, "
990 "closing socket.\n");
Harald Welte6eddd472013-06-30 20:18:53 +0200991 goto err;
992 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200993 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200994 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200995 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200996 } else if (link->port == IPA_TCP_PORT_OML)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200997 e1i_ts = e1inp_line_ipa_oml_ts(link->line);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200998 else if (link->port == IPA_TCP_PORT_RSL)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200999 e1i_ts = e1inp_line_ipa_rsl_ts(link->line, link->ofd->priv_nr - E1INP_SIGN_RSL);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +02001000
Pablo Neira Ayusob9487012013-07-05 14:38:43 +02001001 OSMO_ASSERT(e1i_ts != NULL);
1002
Neels Hofmeyrfe673112018-08-24 17:43:45 +02001003 if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
1004 LOGP(DLINP, LOGL_ERROR, "Signalling link not initialized. Discarding."
1005 " port=%u msg_type=%u\n", link->port, msg_type);
Neels Hofmeyrfe673112018-08-24 17:43:45 +02001006 goto err;
1007 }
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +02001008
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +02001009 /* look up for some existing signaling link. */
1010 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
1011 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001012 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +02001013 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +02001014 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +02001015 }
1016 msg->dst = sign_link;
1017
1018 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +02001019 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001020 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +02001021 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +02001022 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +02001023 }
Pau Espin Pedroled122f32018-08-28 18:25:02 +02001024 return link->line->ops->sign_link(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +02001025
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +02001026err:
Harald Welte783715b2014-08-17 18:24:51 +02001027 ipa_client_conn_close(link);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +02001028 msgb_free(msg);
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +02001029 return -EBADF;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +02001030}
1031
Pablo Neira Ayusod6216402011-08-31 16:47:44 +02001032struct ipaccess_line {
Oliver Smith65ae42c2020-01-22 11:24:52 +01001033 bool line_already_initialized;
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001034 struct ipa_client_conn *ipa_cli[NUM_E1_TS]; /* 0=OML, 1+N=TRX_N */
Pablo Neira Ayusod6216402011-08-31 16:47:44 +02001035};
1036
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001037static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001038{
1039 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +02001040 struct ipaccess_line *il;
1041
1042 if (!line->driver_data)
1043 line->driver_data = talloc_zero(line, struct ipaccess_line);
1044
1045 if (!line->driver_data) {
1046 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
1047 return -ENOMEM;
1048 }
1049 il = line->driver_data;
1050
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001051 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001052 case E1INP_LINE_R_BSC: {
Pau Espin Pedrold2d28d82021-09-29 14:15:56 +02001053 /* We only initialize this line once. */
1054 if (il->line_already_initialized)
1055 return 0;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001056 struct ipa_server_link *oml_link, *rsl_link;
Max4c4a1c22016-12-30 15:10:47 +01001057 const char *ipa = e1inp_ipa_get_bind_addr();
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001058
Max4c4a1c22016-12-30 15:10:47 +01001059 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode on %s "
1060 "with OML %u and RSL %u TCP ports\n", ipa,
1061 IPA_TCP_PORT_OML, IPA_TCP_PORT_RSL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001062
Max4c4a1c22016-12-30 15:10:47 +01001063 oml_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001064 IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +02001065 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001066 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001067 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001068 "BSC link: %s\n", strerror(errno));
1069 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +02001070 }
Harald Welte41547552021-04-28 18:23:21 +02001071 oml_link->dscp = g_e1inp_ipaccess_pars.oml.dscp;
1072 oml_link->priority = g_e1inp_ipaccess_pars.oml.priority;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001073 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001074 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001075 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001076 ipa_server_link_destroy(oml_link);
1077 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +02001078 }
Max4c4a1c22016-12-30 15:10:47 +01001079 rsl_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001080 IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +02001081 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001082 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001083 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001084 "BSC link: %s\n", strerror(errno));
1085 return -ENOMEM;
1086 }
Harald Welte41547552021-04-28 18:23:21 +02001087 rsl_link->dscp = g_e1inp_ipaccess_pars.rsl.dscp;
1088 rsl_link->priority = g_e1inp_ipaccess_pars.rsl.priority;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001089 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001090 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001091 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001092 ipa_server_link_destroy(rsl_link);
1093 return -EIO;
1094 }
1095 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001096 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001097 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001098 case E1INP_LINE_R_BTS: {
Harald Welte84f67b22013-06-30 13:13:59 +02001099 struct ipa_client_conn *link;
Eric Wildef1f3272019-07-10 18:10:31 +02001100 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001101
Max4c4a1c22016-12-30 15:10:47 +01001102 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode, "
1103 "OML connecting to %s:%u\n", line->ops->cfg.ipa.addr,
1104 IPA_TCP_PORT_OML);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001105
Pau Espin Pedrold2d28d82021-09-29 14:15:56 +02001106 /* Drop previous line */
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001107 if (il->ipa_cli[0]) {
1108 ipa_client_conn_close(il->ipa_cli[0]);
1109 ipa_client_conn_destroy(il->ipa_cli[0]);
1110 il->ipa_cli[0] = NULL;
Pau Espin Pedrold2d28d82021-09-29 14:15:56 +02001111 }
1112
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001113 link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +02001114 e1inp_line_ipa_oml_ts(line),
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +02001115 E1INP_SIGN_OML,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001116 NULL, 0,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001117 line->ops->cfg.ipa.addr,
1118 IPA_TCP_PORT_OML,
Harald Welte51de9ca2013-06-30 20:13:16 +02001119 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001120 ipaccess_bts_read_cb,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +02001121 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001122 line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001123 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001124 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001125 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001126 return -ENOMEM;
1127 }
Harald Welte41547552021-04-28 18:23:21 +02001128 link->dscp = g_e1inp_ipaccess_pars.oml.dscp;
1129 link->priority = g_e1inp_ipaccess_pars.oml.priority;
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001130 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001131 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001132 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001133 ipa_client_conn_close(link);
1134 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001135 return -EIO;
1136 }
Eric Wildef1f3272019-07-10 18:10:31 +02001137
1138 e1i_ts = e1inp_line_ipa_oml_ts(line);
1139 ipaccess_bts_keepalive_fsm_alloc(e1i_ts, link, "oml_bts_to_bsc");
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001140 il->ipa_cli[0] = link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001141 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001142 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001143 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001144 default:
1145 break;
1146 }
Philipp Maierea0f1bd2021-05-20 21:01:23 +02001147
1148 il->line_already_initialized = true;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001149 return ret;
1150}
1151
Andreas Eversbergf422a752014-01-21 14:54:41 +01001152/* backwards compatibility */
Harald Welte84f67b22013-06-30 13:13:59 +02001153int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line,
1154 const char *rem_addr, uint16_t rem_port)
1155{
Andreas Eversbergf422a752014-01-21 14:54:41 +01001156 return e1inp_ipa_bts_rsl_connect_n(line, rem_addr, rem_port, 0);
1157}
1158
1159int e1inp_ipa_bts_rsl_connect_n(struct e1inp_line *line,
1160 const char *rem_addr, uint16_t rem_port,
1161 uint8_t trx_nr)
1162{
Harald Welte84f67b22013-06-30 13:13:59 +02001163 struct ipa_client_conn *rsl_link;
Eric Wildef1f3272019-07-10 18:10:31 +02001164 struct e1inp_ts *e1i_ts = e1inp_line_ipa_rsl_ts(line, trx_nr);
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001165 struct ipaccess_line *il;
Pau Espin Pedrol57a148f2022-09-15 18:54:21 +02001166 int rc;
Harald Welte84f67b22013-06-30 13:13:59 +02001167
Andreas Eversbergf422a752014-01-21 14:54:41 +01001168 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
1169 LOGP(DLINP, LOGL_ERROR, "cannot create RSL BTS link: "
1170 "trx_nr (%d) out of range\n", trx_nr);
1171 return -EINVAL;
1172 }
Andreas Eversbergf422a752014-01-21 14:54:41 +01001173
Pau Espin Pedrol57a148f2022-09-15 18:54:21 +02001174 /* Drop previous line */
1175 if ((rc = e1inp_ipa_bts_rsl_close_n(line, trx_nr)) < 0)
1176 return rc;
1177
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001178 if (!line->driver_data)
1179 line->driver_data = talloc_zero(line, struct ipaccess_line);
1180 il = line->driver_data;
1181
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001182 rsl_link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +02001183 e1inp_line_ipa_rsl_ts(line, trx_nr),
Andreas Eversbergf422a752014-01-21 14:54:41 +01001184 E1INP_SIGN_RSL+trx_nr,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001185 NULL, 0,
Harald Welte84f67b22013-06-30 13:13:59 +02001186 rem_addr, rem_port,
Harald Welte51de9ca2013-06-30 20:13:16 +02001187 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001188 ipaccess_bts_read_cb,
Harald Welte84f67b22013-06-30 13:13:59 +02001189 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001190 line);
Harald Welte84f67b22013-06-30 13:13:59 +02001191 if (rsl_link == NULL) {
1192 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
1193 "BTS link: %s\n", strerror(errno));
1194 return -ENOMEM;
1195 }
Harald Welte41547552021-04-28 18:23:21 +02001196 rsl_link->dscp = g_e1inp_ipaccess_pars.rsl.dscp;
1197 rsl_link->priority = g_e1inp_ipaccess_pars.rsl.priority;
Harald Welte84f67b22013-06-30 13:13:59 +02001198 if (ipa_client_conn_open(rsl_link) < 0) {
1199 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
1200 strerror(errno));
1201 ipa_client_conn_close(rsl_link);
1202 ipa_client_conn_destroy(rsl_link);
1203 return -EIO;
1204 }
Eric Wildef1f3272019-07-10 18:10:31 +02001205 ipaccess_bts_keepalive_fsm_alloc(e1i_ts, rsl_link, "rsl_bts_to_bsc");
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001206 il->ipa_cli[1 + trx_nr] = rsl_link;
Harald Welte84f67b22013-06-30 13:13:59 +02001207 return 0;
1208}
1209
Pau Espin Pedrol57a148f2022-09-15 18:54:21 +02001210/* Close the underlying IPA TCP socket of an RSL link */
1211int e1inp_ipa_bts_rsl_close_n(struct e1inp_line *line, uint8_t trx_nr)
1212{
1213 struct ipaccess_line *il;
1214
1215 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
1216 LOGP(DLINP, LOGL_ERROR,
1217 "cannot close RSL BTS link: trx_nr (%d) out of range\n", trx_nr);
1218 return -EINVAL;
1219 }
1220
1221 il = line->driver_data;
1222 if (!il)
1223 return 0; /* Nothing to do, no lines created */
1224
1225 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}