blob: 8dc100848d3ab0261da11b5e96a42bc036128fdc [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 Iedema29667862022-02-24 09:25:20 -0800299 char tcp_stat_name[32];
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 Iedema29667862022-02-24 09:25:20 -0800346 snprintf(tcp_stat_name, sizeof(tcp_stat_name), "bts.%u.ipa-rsl.%u",
347 unit_data.bts_id, unit_data.trx_id);
348 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
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200480static void timeout_ts1_write(void *data)
481{
482 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
483
484 /* trigger write of ts1, due to tx delay timer */
485 ts_want_write(e1i_ts);
486}
487
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200488static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200489{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200490 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200491 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200492 struct e1inp_sign_link *sign_link;
493 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200494 int ret;
495
Eric Wildef1f3272019-07-10 18:10:31 +0200496 e1i_ts = ipaccess_line_ts(bfd, line);
Harald Welte949b8a22020-10-18 23:01:53 +0200497 osmo_fd_write_disable(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200498
499 /* get the next msg for this timeslot */
500 msg = e1inp_tx_ts(e1i_ts, &sign_link);
501 if (!msg) {
502 /* no message after tx delay timer */
503 return 0;
504 }
505
506 switch (sign_link->type) {
507 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200508 case E1INP_SIGN_RSL:
Harald Welte46fc7e22014-08-18 19:04:26 +0200509 case E1INP_SIGN_OSMO:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200510 break;
511 default:
Harald Welte949b8a22020-10-18 23:01:53 +0200512 osmo_fd_write_enable(bfd); /* come back for more msg */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200513 ret = -EINVAL;
514 goto out;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200515 }
516
517 msg->l2h = msg->data;
Harald Welteb65f58f2014-08-20 22:04:11 +0200518 ipa_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200519
Vadim Yanitskiy77a7cf42020-11-29 20:39:57 +0100520 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "TX %u: %s\n", ts_nr,
Pau Espin Pedrol3bdf59b2020-11-23 13:47:16 +0100521 osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200522
523 ret = send(bfd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200524 if (ret != msg->len) {
Pau Espin Pedrol3bdf59b2020-11-23 13:47:16 +0100525 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling "
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200526 "message. Reason: %s\n", strerror(errno));
527 goto err;
528 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200529
530 /* set tx delay timer for next event */
Pablo Neira Ayusob26f2fd2017-06-07 18:32:13 +0200531 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200532
533 /* Reducing this might break the nanoBTS 900 init. */
534 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
535
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200536out:
537 msgb_free(msg);
538 return ret;
539err:
Harald Welte10b41302013-06-30 14:05:49 +0200540 ipaccess_drop(bfd, line);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200541 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200542 return ret;
543}
544
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200545static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200546{
547 struct e1inp_line *line = bfd->data;
548
549 return __handle_ts1_write(bfd, line);
550}
551
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200552static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200553{
554 struct e1inp_line *line = link->line;
555
556 return __handle_ts1_write(link->ofd, line);
557}
558
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200559/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200560int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200561{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200562 int rc = 0;
563
Harald Weltede3959e2020-10-18 22:28:50 +0200564 if (what & OSMO_FD_READ)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200565 rc = handle_ts1_read(bfd);
Harald Weltede3959e2020-10-18 22:28:50 +0200566 if (rc != -EBADF && (what & OSMO_FD_WRITE))
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200567 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200568
569 return rc;
570}
571
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200572static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200573
574struct e1inp_driver ipaccess_driver = {
575 .name = "ipa",
576 .want_write = ts_want_write,
577 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200578 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200579 .default_delay = 0,
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100580 .has_keepalive = 1,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200581};
582
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100583static void update_fd_settings(struct e1inp_line *line, int fd)
584{
585 int ret;
586 int val;
587
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100588 if (line->keepalive_num_probes) {
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100589 /* Enable TCP keepalive to find out if the connection is gone */
590 val = 1;
591 ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
592 if (ret < 0)
593 LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive: %s\n",
594 strerror(errno));
595 else
596 LOGP(DLINP, LOGL_NOTICE, "Keepalive is set: %i\n", ret);
597
598#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
599 /* The following options are not portable! */
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100600 val = line->keepalive_idle_timeout > 0 ?
601 line->keepalive_idle_timeout :
602 DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100603 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
604 &val, sizeof(val));
605 if (ret < 0)
606 LOGP(DLINP, LOGL_NOTICE,
607 "Failed to set keepalive idle time: %s\n",
608 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100609 val = line->keepalive_probe_interval > -1 ?
610 line->keepalive_probe_interval :
611 DEFAULT_TCP_KEEPALIVE_INTERVAL;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100612 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
613 &val, sizeof(val));
614 if (ret < 0)
615 LOGP(DLINP, LOGL_NOTICE,
616 "Failed to set keepalive interval: %s\n",
617 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100618 val = line->keepalive_num_probes > 0 ?
619 line->keepalive_num_probes :
620 DEFAULT_TCP_KEEPALIVE_RETRY_COUNT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100621 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
622 &val, sizeof(val));
623 if (ret < 0)
624 LOGP(DLINP, LOGL_NOTICE,
625 "Failed to set keepalive count: %s\n",
626 strerror(errno));
Eric Wild6eb186c2019-06-21 15:26:25 +0200627#if defined(TCP_USER_TIMEOUT)
628 val = 1000 * line->keepalive_num_probes *
629 line->keepalive_probe_interval +
630 line->keepalive_idle_timeout;
631 ret = setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
632 &val, sizeof(val));
633 if (ret < 0)
634 LOGP(DLINP, LOGL_NOTICE,
635 "Failed to set user timoeut: %s\n",
636 strerror(errno));
637#endif
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100638#endif
Holger Hans Peter Freytherf465a4c2014-02-03 09:34:02 +0100639 }
Oliver Smith62725d02020-04-17 10:57:46 +0200640
641 val = 1;
642 ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
643 if (ret < 0)
644 LOGP(DLINP, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100645}
646
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200647/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200648static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200649{
650 int ret;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100651 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200652 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200653 struct e1inp_ts *e1i_ts;
654 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200655
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200656 /* clone virtual E1 line for this new OML link. */
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200657 line = e1inp_line_clone(tall_ipa_ctx, link->line, "ipa_bfd");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200658 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200659 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200660 return -ENOMEM;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200661 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200662
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200663 /* create virrtual E1 timeslots for signalling */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200664 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200665
666 /* initialize the fds */
667 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
668 line->ts[i].driver.ipaccess.fd.fd = -1;
669
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200670 e1i_ts = e1inp_line_ipa_oml_ts(line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200671
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200672 bfd = &e1i_ts->driver.ipaccess.fd;
Harald Weltede3959e2020-10-18 22:28:50 +0200673 osmo_fd_setup(bfd, fd, OSMO_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_OML);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200674 ret = osmo_fd_register(bfd);
675 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200676 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200677 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200678 }
Philipp Maierf5f31d32021-12-06 16:46:40 +0100679 osmo_stats_tcp_osmo_fd_register(bfd, "ipa-oml");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200680
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100681 update_fd_settings(line, bfd->fd);
Daniel Willmann85980722014-01-09 14:30:55 +0100682
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200683 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200684 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200685 if (ret < 0) {
686 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
687 strerror(errno));
688 goto err_socket;
689 }
690 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200691
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200692err_socket:
693 osmo_fd_unregister(bfd);
694err_line:
695 close(bfd->fd);
696 bfd->fd = -1;
Pau Espin Pedrola2ff7332020-07-14 21:12:50 +0200697 bfd->data = NULL;
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200698 e1inp_line_put2(line, "ipa_bfd");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200699 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200700}
701
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200702static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200703{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200704 struct e1inp_line *line;
705 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200706 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200707 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200708
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200709 /* We don't know yet which OML link to associate it with. Thus, we
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200710 * allocate a temporary E1 line until we have received ID. */
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200711 line = e1inp_line_clone(tall_ipa_ctx, link->line, "ipa_bfd");
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200712 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200713 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200714 return -ENOMEM;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200715 }
716 /* initialize the fds */
717 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
718 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200719
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200720 /* we need this to initialize this in case to avoid crashes in case
721 * that the socket is closed before we've seen an ID_RESP. */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200722 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200723
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200724 e1i_ts = e1inp_line_ipa_rsl_ts(line, 0);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200725
726 bfd = &e1i_ts->driver.ipaccess.fd;
Harald Weltede3959e2020-10-18 22:28:50 +0200727 osmo_fd_setup(bfd, fd, OSMO_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_RSL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200728 ret = osmo_fd_register(bfd);
729 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200730 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200731 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200732 }
Philipp Maierf5f31d32021-12-06 16:46:40 +0100733 osmo_stats_tcp_osmo_fd_register(bfd, "ipa-rsl");
734
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200735 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200736 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200737 if (ret < 0) {
738 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
739 strerror(errno));
740 goto err_socket;
741 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100742 update_fd_settings(line, bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200743 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200744
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200745err_socket:
746 osmo_fd_unregister(bfd);
747err_line:
748 close(bfd->fd);
749 bfd->fd = -1;
Pau Espin Pedrola2ff7332020-07-14 21:12:50 +0200750 bfd->data = NULL;
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200751 e1inp_line_put2(line, "ipa_bfd");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200752 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200753}
754
Harald Weltee416e2e2017-05-26 13:11:59 +0200755#define IPA_STRING_MAX 64
756
757static struct msgb *
Max71393522018-01-08 15:42:00 +0100758ipa_bts_id_resp(const struct ipaccess_unit *dev, uint8_t *data, int len, int trx_nr)
Harald Weltee416e2e2017-05-26 13:11:59 +0200759{
760 struct msgb *nmsg;
761 char str[IPA_STRING_MAX];
762 uint8_t *tag;
763
764 memset(str, 0, sizeof(str));
765
766 nmsg = ipa_msg_alloc(0);
767 if (!nmsg)
768 return NULL;
769
770 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
771 while (len) {
772 if (len < 2) {
773 LOGP(DLINP, LOGL_NOTICE,
774 "Short read of ipaccess tag\n");
775 msgb_free(nmsg);
776 return NULL;
777 }
778 switch (data[1]) {
779 case IPAC_IDTAG_UNIT:
780 snprintf(str, sizeof(str), "%u/%u/%u",
781 dev->site_id, dev->bts_id, trx_nr);
782 break;
783 case IPAC_IDTAG_MACADDR:
784 snprintf(str, sizeof(str),
785 "%02x:%02x:%02x:%02x:%02x:%02x",
786 dev->mac_addr[0], dev->mac_addr[1],
787 dev->mac_addr[2], dev->mac_addr[3],
788 dev->mac_addr[4], dev->mac_addr[5]);
789 break;
790 case IPAC_IDTAG_LOCATION1:
791 if (dev->location1)
Stefan Sperling961776a2018-12-06 13:09:48 +0100792 osmo_strlcpy(str, dev->location1, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200793 break;
794 case IPAC_IDTAG_LOCATION2:
795 if (dev->location2)
Stefan Sperling961776a2018-12-06 13:09:48 +0100796 osmo_strlcpy(str, dev->location2, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200797 break;
798 case IPAC_IDTAG_EQUIPVERS:
799 if (dev->equipvers)
Stefan Sperling961776a2018-12-06 13:09:48 +0100800 osmo_strlcpy(str, dev->equipvers, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200801 break;
802 case IPAC_IDTAG_SWVERSION:
803 if (dev->swversion)
Neels Hofmeyr4c57eef2018-07-26 17:28:24 +0200804 osmo_strlcpy(str, dev->swversion, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200805 break;
806 case IPAC_IDTAG_UNITNAME:
807 snprintf(str, sizeof(str),
808 "%s-%02x-%02x-%02x-%02x-%02x-%02x",
809 dev->unit_name,
810 dev->mac_addr[0], dev->mac_addr[1],
811 dev->mac_addr[2], dev->mac_addr[3],
812 dev->mac_addr[4], dev->mac_addr[5]);
813 break;
814 case IPAC_IDTAG_SERNR:
815 if (dev->serno)
Stefan Sperling961776a2018-12-06 13:09:48 +0100816 osmo_strlcpy(str, dev->serno, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200817 break;
818 default:
819 LOGP(DLINP, LOGL_NOTICE,
820 "Unknown ipaccess tag 0x%02x\n", *data);
821 msgb_free(nmsg);
822 return NULL;
823 }
Harald Weltee416e2e2017-05-26 13:11:59 +0200824
825 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
826 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
827 tag[0] = 0x00;
828 tag[1] = 1 + strlen(str) + 1;
829 tag[2] = data[1];
830 memcpy(tag + 3, str, strlen(str) + 1);
831 data += 2;
832 len -= 2;
833 }
834 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
835 return nmsg;
836}
837
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200838static struct msgb *ipa_bts_id_ack(void)
839{
840 struct msgb *nmsg2;
841
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200842 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200843 if (!nmsg2)
844 return NULL;
845
846 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200847 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200848
849 return nmsg2;
850}
851
Harald Welte51de9ca2013-06-30 20:13:16 +0200852static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up)
853{
854 struct e1inp_line *line = link->line;
855
Eric Wildef1f3272019-07-10 18:10:31 +0200856 if (up) {
857 struct osmo_fsm_inst *ka_fsm = ipaccess_line_ts(link->ofd, line)->driver.ipaccess.ka_fsm;
858
859 update_fd_settings(line, link->ofd->fd);
860 if (ka_fsm && line->ipa_kap)
861 ipa_keepalive_fsm_start(ka_fsm);
862 return;
863 }
Harald Welte51de9ca2013-06-30 20:13:16 +0200864
865 if (line->ops->sign_link_down)
866 line->ops->sign_link_down(line);
867}
868
Harald Welte783715b2014-08-17 18:24:51 +0200869/* handle incoming message to BTS, check if it is an IPA CCM, and if yes,
870 * handle it accordingly (PING/PONG/ID_REQ/ID_RESP/ID_ACK) */
871int ipaccess_bts_handle_ccm(struct ipa_client_conn *link,
872 struct ipaccess_unit *dev, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200873{
874 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200875 struct msgb *rmsg;
876 int ret = 0;
Eric Wildef1f3272019-07-10 18:10:31 +0200877 /* line might not exist if != bsc||bts */
878 struct e1inp_line *line = link->line;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200879
880 /* special handling for IPA CCM. */
881 if (hh->proto == IPAC_PROTO_IPACCESS) {
882 uint8_t msg_type = *(msg->l2h);
Eric Wildef1f3272019-07-10 18:10:31 +0200883 struct osmo_fsm_inst* ka_fsm = NULL;
884
885 /* peek the pong for our keepalive fsm */
886 if (line && msg_type == IPAC_MSGT_PONG) {
887 ka_fsm = ipaccess_line_ts(link->ofd, line)->driver.ipaccess.ka_fsm;
888 ipa_keepalive_fsm_pong_received(ka_fsm);
889 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200890
891 /* ping, pong and acknowledgment cases. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200892 ret = ipa_ccm_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200893 if (ret < 0)
894 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200895
896 /* this is a request for identification from the BSC. */
897 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200898 uint8_t *data = msgb_l2(msg);
899 int len = msgb_l2len(msg);
Andreas Eversbergf422a752014-01-21 14:54:41 +0100900 int trx_nr = 0;
901
902 if (link->ofd->priv_nr >= E1INP_SIGN_RSL)
903 trx_nr = link->ofd->priv_nr - E1INP_SIGN_RSL;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200904
Vadim Yanitskiy69ae2382019-12-02 02:42:43 +0700905 LOGP(DLINP, LOGL_NOTICE, "received ID_GET for unit ID %u/%u/%u\n",
Pau Espin Pedroldd95eb62018-10-02 20:05:28 +0200906 dev->site_id, dev->bts_id, trx_nr);
Harald Weltee416e2e2017-05-26 13:11:59 +0200907 rmsg = ipa_bts_id_resp(dev, data + 1, len - 1, trx_nr);
Harald Welteb65f58f2014-08-20 22:04:11 +0200908 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200909 if (ret != rmsg->len) {
910 LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP "
911 "message. Reason: %s\n", strerror(errno));
912 goto err_rmsg;
913 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200914 msgb_free(rmsg);
915
916 /* send ID_ACK. */
917 rmsg = ipa_bts_id_ack();
Harald Welteb65f58f2014-08-20 22:04:11 +0200918 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200919 if (ret != rmsg->len) {
920 LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK "
921 "message. Reason: %s\n", strerror(errno));
922 goto err_rmsg;
923 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200924 msgb_free(rmsg);
Harald Welte783715b2014-08-17 18:24:51 +0200925 }
926 return 1;
927 }
Harald Welte6eddd472013-06-30 20:18:53 +0200928
Harald Welte783715b2014-08-17 18:24:51 +0200929 return 0;
930
931err_rmsg:
932 msgb_free(rmsg);
933err:
934 ipa_client_conn_close(link);
935 return -1;
936}
937
938static int ipaccess_bts_read_cb(struct ipa_client_conn *link, struct msgb *msg)
939{
940 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
941 struct e1inp_ts *e1i_ts = NULL;
942 struct e1inp_sign_link *sign_link;
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200943 uint8_t msg_type = *(msg->l2h);
Harald Welte783715b2014-08-17 18:24:51 +0200944 int ret = 0;
945
946 /* special handling for IPA CCM. */
947 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200948 /* this is a request for identification from the BSC. */
949 if (msg_type == IPAC_MSGT_ID_GET) {
950 if (!link->line->ops->sign_link_up) {
951 LOGP(DLINP, LOGL_ERROR,
952 "Unable to set signal link, "
953 "closing socket.\n");
Harald Welte783715b2014-08-17 18:24:51 +0200954 goto err;
955 }
956 }
957 }
958
959 /* core CCM handling */
960 ret = ipaccess_bts_handle_ccm(link, link->line->ops->cfg.ipa.dev, msg);
961 if (ret < 0)
962 goto err;
963
964 if (ret == 1 && hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200965 if (msg_type == IPAC_MSGT_ID_GET) {
Harald Welte6eddd472013-06-30 20:18:53 +0200966 sign_link = link->line->ops->sign_link_up(msg,
967 link->line,
968 link->ofd->priv_nr);
969 if (sign_link == NULL) {
970 LOGP(DLINP, LOGL_ERROR,
971 "Unable to set signal link, "
972 "closing socket.\n");
Harald Welte6eddd472013-06-30 20:18:53 +0200973 goto err;
974 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200975 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200976 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200977 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200978 } else if (link->port == IPA_TCP_PORT_OML)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200979 e1i_ts = e1inp_line_ipa_oml_ts(link->line);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200980 else if (link->port == IPA_TCP_PORT_RSL)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200981 e1i_ts = e1inp_line_ipa_rsl_ts(link->line, link->ofd->priv_nr - E1INP_SIGN_RSL);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200982
Pablo Neira Ayusob9487012013-07-05 14:38:43 +0200983 OSMO_ASSERT(e1i_ts != NULL);
984
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200985 if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
986 LOGP(DLINP, LOGL_ERROR, "Signalling link not initialized. Discarding."
987 " port=%u msg_type=%u\n", link->port, msg_type);
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200988 goto err;
989 }
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +0200990
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200991 /* look up for some existing signaling link. */
992 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
993 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200994 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200995 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200996 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200997 }
998 msg->dst = sign_link;
999
1000 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +02001001 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001002 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +02001003 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +02001004 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +02001005 }
Pau Espin Pedroled122f32018-08-28 18:25:02 +02001006 return link->line->ops->sign_link(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +02001007
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +02001008err:
Harald Welte783715b2014-08-17 18:24:51 +02001009 ipa_client_conn_close(link);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +02001010 msgb_free(msg);
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +02001011 return -EBADF;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +02001012}
1013
Pablo Neira Ayusod6216402011-08-31 16:47:44 +02001014struct ipaccess_line {
Oliver Smith65ae42c2020-01-22 11:24:52 +01001015 bool line_already_initialized;
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001016 struct ipa_client_conn *ipa_cli[NUM_E1_TS]; /* 0=OML, 1+N=TRX_N */
Pablo Neira Ayusod6216402011-08-31 16:47:44 +02001017};
1018
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001019static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001020{
1021 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +02001022 struct ipaccess_line *il;
1023
1024 if (!line->driver_data)
1025 line->driver_data = talloc_zero(line, struct ipaccess_line);
1026
1027 if (!line->driver_data) {
1028 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
1029 return -ENOMEM;
1030 }
1031 il = line->driver_data;
1032
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001033 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001034 case E1INP_LINE_R_BSC: {
Pau Espin Pedrold2d28d82021-09-29 14:15:56 +02001035 /* We only initialize this line once. */
1036 if (il->line_already_initialized)
1037 return 0;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001038 struct ipa_server_link *oml_link, *rsl_link;
Max4c4a1c22016-12-30 15:10:47 +01001039 const char *ipa = e1inp_ipa_get_bind_addr();
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001040
Max4c4a1c22016-12-30 15:10:47 +01001041 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode on %s "
1042 "with OML %u and RSL %u TCP ports\n", ipa,
1043 IPA_TCP_PORT_OML, IPA_TCP_PORT_RSL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001044
Max4c4a1c22016-12-30 15:10:47 +01001045 oml_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001046 IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +02001047 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001048 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001049 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001050 "BSC link: %s\n", strerror(errno));
1051 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +02001052 }
Harald Welte41547552021-04-28 18:23:21 +02001053 oml_link->dscp = g_e1inp_ipaccess_pars.oml.dscp;
1054 oml_link->priority = g_e1inp_ipaccess_pars.oml.priority;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001055 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001056 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001057 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001058 ipa_server_link_destroy(oml_link);
1059 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +02001060 }
Max4c4a1c22016-12-30 15:10:47 +01001061 rsl_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001062 IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +02001063 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001064 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001065 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001066 "BSC link: %s\n", strerror(errno));
1067 return -ENOMEM;
1068 }
Harald Welte41547552021-04-28 18:23:21 +02001069 rsl_link->dscp = g_e1inp_ipaccess_pars.rsl.dscp;
1070 rsl_link->priority = g_e1inp_ipaccess_pars.rsl.priority;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001071 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001072 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001073 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001074 ipa_server_link_destroy(rsl_link);
1075 return -EIO;
1076 }
1077 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001078 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001079 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001080 case E1INP_LINE_R_BTS: {
Harald Welte84f67b22013-06-30 13:13:59 +02001081 struct ipa_client_conn *link;
Eric Wildef1f3272019-07-10 18:10:31 +02001082 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001083
Max4c4a1c22016-12-30 15:10:47 +01001084 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode, "
1085 "OML connecting to %s:%u\n", line->ops->cfg.ipa.addr,
1086 IPA_TCP_PORT_OML);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001087
Pau Espin Pedrold2d28d82021-09-29 14:15:56 +02001088 /* Drop previous line */
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001089 if (il->ipa_cli[0]) {
1090 ipa_client_conn_close(il->ipa_cli[0]);
1091 ipa_client_conn_destroy(il->ipa_cli[0]);
1092 il->ipa_cli[0] = NULL;
Pau Espin Pedrold2d28d82021-09-29 14:15:56 +02001093 }
1094
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001095 link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +02001096 e1inp_line_ipa_oml_ts(line),
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +02001097 E1INP_SIGN_OML,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001098 NULL, 0,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001099 line->ops->cfg.ipa.addr,
1100 IPA_TCP_PORT_OML,
Harald Welte51de9ca2013-06-30 20:13:16 +02001101 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001102 ipaccess_bts_read_cb,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +02001103 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001104 line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001105 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001106 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001107 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001108 return -ENOMEM;
1109 }
Harald Welte41547552021-04-28 18:23:21 +02001110 link->dscp = g_e1inp_ipaccess_pars.oml.dscp;
1111 link->priority = g_e1inp_ipaccess_pars.oml.priority;
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001112 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001113 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001114 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001115 ipa_client_conn_close(link);
1116 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001117 return -EIO;
1118 }
Eric Wildef1f3272019-07-10 18:10:31 +02001119
1120 e1i_ts = e1inp_line_ipa_oml_ts(line);
1121 ipaccess_bts_keepalive_fsm_alloc(e1i_ts, link, "oml_bts_to_bsc");
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001122 il->ipa_cli[0] = link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001123 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001124 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001125 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001126 default:
1127 break;
1128 }
Philipp Maierea0f1bd2021-05-20 21:01:23 +02001129
1130 il->line_already_initialized = true;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001131 return ret;
1132}
1133
Andreas Eversbergf422a752014-01-21 14:54:41 +01001134
1135/* backwards compatibility */
Harald Welte84f67b22013-06-30 13:13:59 +02001136int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line,
1137 const char *rem_addr, uint16_t rem_port)
1138{
Andreas Eversbergf422a752014-01-21 14:54:41 +01001139 return e1inp_ipa_bts_rsl_connect_n(line, rem_addr, rem_port, 0);
1140}
1141
1142int e1inp_ipa_bts_rsl_connect_n(struct e1inp_line *line,
1143 const char *rem_addr, uint16_t rem_port,
1144 uint8_t trx_nr)
1145{
Harald Welte84f67b22013-06-30 13:13:59 +02001146 struct ipa_client_conn *rsl_link;
Eric Wildef1f3272019-07-10 18:10:31 +02001147 struct e1inp_ts *e1i_ts = e1inp_line_ipa_rsl_ts(line, trx_nr);
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001148 struct ipaccess_line *il;
Harald Welte84f67b22013-06-30 13:13:59 +02001149
Andreas Eversbergf422a752014-01-21 14:54:41 +01001150 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
1151 LOGP(DLINP, LOGL_ERROR, "cannot create RSL BTS link: "
1152 "trx_nr (%d) out of range\n", trx_nr);
1153 return -EINVAL;
1154 }
Andreas Eversbergf422a752014-01-21 14:54:41 +01001155
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001156 if (!line->driver_data)
1157 line->driver_data = talloc_zero(line, struct ipaccess_line);
1158 il = line->driver_data;
1159
1160 /* Drop previous line */
1161 if (il->ipa_cli[1 + trx_nr]) {
1162 ipa_client_conn_close(il->ipa_cli[1 + trx_nr]);
1163 ipa_client_conn_destroy(il->ipa_cli[1 + trx_nr]);
1164 il->ipa_cli[1 + trx_nr] = NULL;
1165 }
1166
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001167 rsl_link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +02001168 e1inp_line_ipa_rsl_ts(line, trx_nr),
Andreas Eversbergf422a752014-01-21 14:54:41 +01001169 E1INP_SIGN_RSL+trx_nr,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001170 NULL, 0,
Harald Welte84f67b22013-06-30 13:13:59 +02001171 rem_addr, rem_port,
Harald Welte51de9ca2013-06-30 20:13:16 +02001172 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001173 ipaccess_bts_read_cb,
Harald Welte84f67b22013-06-30 13:13:59 +02001174 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001175 line);
Harald Welte84f67b22013-06-30 13:13:59 +02001176 if (rsl_link == NULL) {
1177 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
1178 "BTS link: %s\n", strerror(errno));
1179 return -ENOMEM;
1180 }
Harald Welte41547552021-04-28 18:23:21 +02001181 rsl_link->dscp = g_e1inp_ipaccess_pars.rsl.dscp;
1182 rsl_link->priority = g_e1inp_ipaccess_pars.rsl.priority;
Harald Welte84f67b22013-06-30 13:13:59 +02001183 if (ipa_client_conn_open(rsl_link) < 0) {
1184 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
1185 strerror(errno));
1186 ipa_client_conn_close(rsl_link);
1187 ipa_client_conn_destroy(rsl_link);
1188 return -EIO;
1189 }
Eric Wildef1f3272019-07-10 18:10:31 +02001190 ipaccess_bts_keepalive_fsm_alloc(e1i_ts, rsl_link, "rsl_bts_to_bsc");
Pau Espin Pedrol92aee512021-10-11 12:14:36 +02001191 il->ipa_cli[1 + trx_nr] = rsl_link;
Harald Welte84f67b22013-06-30 13:13:59 +02001192 return 0;
1193}
1194
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001195void e1inp_ipaccess_init(void)
1196{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +02001197 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001198 e1inp_driver_register(&ipaccess_driver);
1199}
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001200
1201void e1inp_ipa_set_bind_addr(const char *ip_bind_addr)
1202{
1203 talloc_free((char*)ipaccess_driver.bind_addr);
1204 ipaccess_driver.bind_addr = NULL;
1205
1206 if (ip_bind_addr)
1207 ipaccess_driver.bind_addr = talloc_strdup(tall_ipa_ctx,
1208 ip_bind_addr);
1209}
1210
1211const char *e1inp_ipa_get_bind_addr(void)
1212{
1213 return ipaccess_driver.bind_addr?
1214 ipaccess_driver.bind_addr
1215 : "0.0.0.0";
1216}