blob: a584e0d6fb37da2f781eb04646657b9c51e31cad [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>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020053
Harald Welte41547552021-04-28 18:23:21 +020054/* global parameters of IPA input driver */
55struct ipa_pars g_e1inp_ipaccess_pars;
56
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +020057static void *tall_ipa_ctx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020058
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020059#define TS1_ALLOC_SIZE 900
60
Daniel Willmann85980722014-01-09 14:30:55 +010061#define DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT 30
62#define DEFAULT_TCP_KEEPALIVE_INTERVAL 3
63#define DEFAULT_TCP_KEEPALIVE_RETRY_COUNT 10
64
Eric Wildef1f3272019-07-10 18:10:31 +020065static inline struct e1inp_ts *ipaccess_line_ts(struct osmo_fd *bfd, struct e1inp_line *line)
66{
67 if (bfd->priv_nr == E1INP_SIGN_OML)
68 return e1inp_line_ipa_oml_ts(line);
69 else
70 return e1inp_line_ipa_rsl_ts(line, bfd->priv_nr - E1INP_SIGN_RSL);
71}
72
73static inline void ipaccess_keepalive_fsm_cleanup(struct e1inp_ts *e1i_ts)
74{
75 struct osmo_fsm_inst *ka_fsm;
76
77 ka_fsm = e1i_ts->driver.ipaccess.ka_fsm;
78 if (ka_fsm) {
79 ipa_keepalive_fsm_stop(ka_fsm);
80 e1i_ts->driver.ipaccess.ka_fsm = NULL;
81 }
82}
83
Harald Welte10b41302013-06-30 14:05:49 +020084static int ipaccess_drop(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020085{
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +020086 int ret = 1;
Eric Wildef1f3272019-07-10 18:10:31 +020087 struct e1inp_ts *e1i_ts = ipaccess_line_ts(bfd, line);
Pau Espin Pedroldeb5c4f2020-07-31 12:55:01 +020088 e1inp_line_get2(line, __func__);
Eric Wildef1f3272019-07-10 18:10:31 +020089
90 ipaccess_keepalive_fsm_cleanup(e1i_ts);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020091
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020092 /* Error case: we did not see any ID_RESP yet for this socket. */
93 if (bfd->fd != -1) {
Pau Espin Pedrol91314c12020-11-23 14:45:03 +010094 LOGPITS(e1i_ts, DLINP, LOGL_NOTICE, "Forcing socket shutdown\n");
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +020095 osmo_fd_unregister(bfd);
96 close(bfd->fd);
97 bfd->fd = -1;
Pau Espin Pedrol9af069d2020-11-23 14:54:00 +010098 switch (line->ops->cfg.ipa.role) {
99 case E1INP_LINE_R_BSC:
100 /* This is BSC code, ipaccess_drop() is only called for
101 accepted() sockets, hence the bfd holds a reference to
102 e1inp_line in ->data that needs to be released */
103 OSMO_ASSERT(bfd->data == line);
104 bfd->data = NULL;
105 e1inp_line_put2(line, "ipa_bfd");
106 break;
107 case E1INP_LINE_R_BTS:
108 /* BTS code: bfd->data contains pointer to struct
109 * ipa_client_conn. Leave it alive so it reconnects.
110 */
111 break;
112 default:
113 break;
114 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200115 ret = -ENOENT;
Pau Espin Pedrol91314c12020-11-23 14:45:03 +0100116 } else {
117 LOGPITS(e1i_ts, DLINP, LOGL_ERROR,
118 "Forcing socket shutdown with no signal link set\n");
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +0200119 }
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200120
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200121 msgb_free(e1i_ts->pending_msg);
122 e1i_ts->pending_msg = NULL;
123
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200124 /* e1inp_sign_link_destroy releases the socket descriptors for us. */
125 line->ops->sign_link_down(line);
126
Pau Espin Pedroldeb5c4f2020-07-31 12:55:01 +0200127 e1inp_line_put2(line, __func__);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200128 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200129}
130
Eric Wildef1f3272019-07-10 18:10:31 +0200131static void ipa_bsc_keepalive_write_server_cb(struct osmo_fsm_inst *fi, void *conn, struct msgb *msg)
132{
133 struct osmo_fd *bfd = (struct osmo_fd *)conn;
134 write(bfd->fd, msg->data, msg->len);
135 msgb_free(msg);
136}
137
138static int ipa_bsc_keepalive_timeout_cb(struct osmo_fsm_inst *fi, void *data)
139{
140 struct osmo_fd *bfd = (struct osmo_fd *)data;
141
142 if (bfd->fd == -1)
143 return 1;
144
145 ipaccess_drop(bfd, (struct e1inp_line *)bfd->data);
146 return 1;
147}
148
149static void ipaccess_bsc_keepalive_fsm_alloc(struct e1inp_ts *e1i_ts, struct osmo_fd *bfd, const char *id)
150{
151 struct e1inp_line *line = e1i_ts->line;
152 struct osmo_fsm_inst *ka_fsm;
153
154 ipaccess_keepalive_fsm_cleanup(e1i_ts);
155 if (!line->ipa_kap)
156 return;
157
158 ka_fsm = ipa_generic_conn_alloc_keepalive_fsm(tall_ipa_ctx, bfd, line->ipa_kap, id);
159 e1i_ts->driver.ipaccess.ka_fsm = ka_fsm;
160 if (!ka_fsm)
161 return;
162
163 ipa_keepalive_fsm_set_timeout_cb(ka_fsm, ipa_bsc_keepalive_timeout_cb);
164 ipa_keepalive_fsm_set_send_cb(ka_fsm, ipa_bsc_keepalive_write_server_cb);
165 ipa_keepalive_fsm_start(ka_fsm);
166}
167
168static void ipa_bts_keepalive_write_client_cb(struct osmo_fsm_inst *fi, void *conn, struct msgb *msg) {
169 struct ipa_client_conn *link = (struct ipa_client_conn *)conn;
170 int ret = 0;
171
172 ret = ipa_send(link->ofd->fd, msg->data, msg->len);
173 if (ret != msg->len) {
174 LOGP(DLINP, LOGL_ERROR, "cannot send message. Reason: %s\n", strerror(errno));
175 }
176 msgb_free(msg);
177}
178
179static void update_fd_settings(struct e1inp_line *line, int fd);
180static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up);
181
182static int ipa_bts_keepalive_timeout_cb(struct osmo_fsm_inst *fi, void *conn) {
183 ipaccess_bts_updown_cb(conn, false);
184 return 1;
185}
186
187static void ipaccess_bts_keepalive_fsm_alloc(struct e1inp_ts *e1i_ts, struct ipa_client_conn *client, const char *id)
188{
189 struct e1inp_line *line = e1i_ts->line;
190 struct osmo_fsm_inst *ka_fsm;
191
192 ipaccess_keepalive_fsm_cleanup(e1i_ts);
193 if (!line->ipa_kap)
194 return;
195
196 ka_fsm = ipa_client_conn_alloc_keepalive_fsm(client, line->ipa_kap, id);
197 e1i_ts->driver.ipaccess.ka_fsm = ka_fsm;
198 if (!ka_fsm)
199 return;
200
201 ipa_keepalive_fsm_set_timeout_cb(ka_fsm, ipa_bts_keepalive_timeout_cb);
202 ipa_keepalive_fsm_set_send_cb(ka_fsm, ipa_bts_keepalive_write_client_cb);
203}
204
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200205/* Returns -1 on error, and 0 or 1 on success. If -1 or 1 is returned, line has
206 * been released and should not be used anymore by the caller. */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200207static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
208 struct osmo_fd *bfd)
209{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200210 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200211 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200212 struct ipaccess_unit unit_data = {};
213 struct e1inp_sign_link *sign_link;
214 char *unitid;
215 int len, ret;
Eric Wildef1f3272019-07-10 18:10:31 +0200216 struct e1inp_ts *e1i_ts;
217 struct osmo_fsm_inst *ka_fsm;
218
219 /* peek the pong for our keepalive fsm */
220 e1i_ts = ipaccess_line_ts(bfd, line);
221 ka_fsm = e1i_ts->driver.ipaccess.ka_fsm;
222 if (ka_fsm && msg_type == IPAC_MSGT_PONG)
223 ipa_keepalive_fsm_pong_received(ka_fsm);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200224
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200225 /* Handle IPA PING, PONG and ID_ACK messages. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200226 ret = ipa_ccm_rcvmsg_base(msg, bfd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200227 switch(ret) {
228 case -1:
229 /* error in IPA control message handling */
230 goto err;
231 case 1:
232 /* this is an IPA control message, skip further processing */
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200233 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200234 case 0:
235 /* this is not an IPA control message, continue */
236 break;
237 default:
238 LOGP(DLINP, LOGL_ERROR, "Unexpected return from "
Harald Welteb65f58f2014-08-20 22:04:11 +0200239 "ipa_ccm_rcvmsg_base "
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200240 "(ret=%d)\n", ret);
241 goto err;
242 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200243
244 switch (msg_type) {
245 case IPAC_MSGT_ID_RESP:
Pau Espin Pedrol30e80d52020-07-14 13:23:05 +0200246 DEBUGP(DLMI, "ID_RESP ");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200247 /* parse tags, search for Unit ID */
Harald Welte82eb55e2018-08-01 13:22:55 +0200248 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 +0200249 DEBUGPC(DLMI, "\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200250 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200251 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200252 "with malformed TLVs\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200253 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200254 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200255 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200256 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200257 "without unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200258 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200259
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200260 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200261 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200262 if (len < 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200263 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200264 "with too small unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200265 goto err;
266 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200267 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
268 unitid[len - 1] = '\0';
Vadim Yanitskiy1c94f6a2019-12-02 02:16:07 +0700269 ret = ipa_parse_unitid(unitid, &unit_data);
270 if (ret) {
271 LOGP(DLINP, LOGL_ERROR, "Failed to parse unit ID '%s'\n", unitid);
272 goto err;
273 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200274
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200275 if (!line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200276 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200277 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200278 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200279 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200280 /* the BSC creates the new sign links at this stage. */
281 if (bfd->priv_nr == E1INP_SIGN_OML) {
282 sign_link =
283 line->ops->sign_link_up(&unit_data, line,
284 E1INP_SIGN_OML);
285 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200286 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200287 "Unable to set signal link, "
288 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200289 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200290 }
Eric Wildef1f3272019-07-10 18:10:31 +0200291
292 ipaccess_bsc_keepalive_fsm_alloc(e1i_ts, bfd, "oml_bsc_to_bts");
293
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200294 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200295 struct e1inp_ts *ts;
Eric Wildef1f3272019-07-10 18:10:31 +0200296 struct osmo_fd *newbfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200297 struct e1inp_line *new_line;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200298
299 sign_link =
300 line->ops->sign_link_up(&unit_data, line,
301 E1INP_SIGN_RSL);
302 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200303 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200304 "Unable to set signal link, "
305 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200306 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200307 }
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200308 /* Finally, we know which OML link is associated with
309 * this RSL link, attach it to this socket. */
310 new_line = sign_link->ts->line;
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200311 /* this is a bugtrap, the BSC should be using the
312 * virtual E1 line used by OML for this RSL link. */
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200313 if (new_line == line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200314 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200315 "Fix your BSC, you should use the "
316 "E1 line used by the OML link for "
317 "your RSL link.\n");
318 return 0;
319 }
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200320 e1inp_line_get2(new_line, "ipa_bfd");
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200321 ts = e1inp_line_ipa_rsl_ts(new_line, unit_data.trx_id);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200322 newbfd = &ts->driver.ipaccess.fd;
Pau Espin Pedrol815117c2020-07-14 21:19:16 +0200323 OSMO_ASSERT(newbfd != bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200324
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200325 /* preserve 'newbfd->when' flags potentially set by sign_link_up() */
Pau Espin Pedrol67902bb2018-08-23 14:35:08 +0200326 osmo_fd_setup(newbfd, bfd->fd, newbfd->when | bfd->when, bfd->cb,
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200327 new_line, E1INP_SIGN_RSL + unit_data.trx_id);
328
329
330 /* now we can release the dummy RSL line (old temporary bfd). */
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200331 osmo_fd_unregister(bfd);
332 bfd->fd = -1;
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200333 /* bfd->data holds a reference to line, drop it */
334 OSMO_ASSERT(bfd->data == line);
335 bfd->data = NULL;
336 e1inp_line_put2(line, "ipa_bfd");
337
Harald Welteae3a9932016-11-26 09:25:23 +0100338 ret = osmo_fd_register(newbfd);
339 if (ret < 0) {
340 LOGP(DLINP, LOGL_ERROR,
341 "could not register FD\n");
342 goto err;
343 }
Eric Wildef1f3272019-07-10 18:10:31 +0200344
345 e1i_ts = ipaccess_line_ts(newbfd, new_line);
346 ipaccess_bsc_keepalive_fsm_alloc(e1i_ts, newbfd, "rsl_bsc_to_bts");
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200347 return 1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200348 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200349 break;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200350 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200351 LOGP(DLINP, LOGL_ERROR, "Unknown IPA message type\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200352 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200353 }
354 return 0;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200355err:
Harald Welteb4a7db02019-07-21 11:51:56 +0200356 if (bfd->fd != -1) {
Pau Espin Pedroldc55a5f2020-07-14 21:14:34 +0200357 osmo_fd_unregister(bfd);
Harald Welteb4a7db02019-07-21 11:51:56 +0200358 close(bfd->fd);
359 bfd->fd = -1;
Pau Espin Pedroldc55a5f2020-07-14 21:14:34 +0200360 /* This is a BSC accepted socket, bfd->data holds a reference to line, drop it */
361 OSMO_ASSERT(bfd->data == line);
362 bfd->data = NULL;
363 e1inp_line_put2(line, "ipa_bfd");
Harald Welteb4a7db02019-07-21 11:51:56 +0200364 }
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200365 return -1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200366}
367
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200368/* Returns -EBADF if bfd cannot be used by the caller anymore after return. */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200369static int handle_ts1_read(struct osmo_fd *bfd)
370{
371 struct e1inp_line *line = bfd->data;
372 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200373 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200374 struct e1inp_sign_link *link;
375 struct ipaccess_head *hh;
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200376 struct msgb *msg = NULL;
Maxc9fa25e2017-01-09 13:23:15 +0100377 int ret, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200378
Eric Wildef1f3272019-07-10 18:10:31 +0200379 e1i_ts = ipaccess_line_ts(bfd, line);
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200380 ret = ipa_msg_recv_buffered(bfd->fd, &msg, &e1i_ts->pending_msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200381 if (ret < 0) {
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200382 if (ret == -EAGAIN)
383 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200384 LOGP(DLINP, LOGL_NOTICE, "Sign link problems, "
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200385 "closing socket. Reason: %s\n", strerror(-ret));
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200386 goto err;
387 } else if (ret == 0) {
388 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
389 goto err;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200390 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200391 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200392
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200393 hh = (struct ipaccess_head *) msg->data;
394 if (hh->proto == IPAC_PROTO_IPACCESS) {
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200395 ret = ipaccess_rcvmsg(line, msg, bfd);
396 /* BIG FAT WARNING: bfd might no longer exist here (ret != 0),
397 * since ipaccess_rcvmsg() might have free'd it !!! */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200398 msgb_free(msg);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200399 return ret != 0 ? -EBADF : 0;
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100400 } else if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
401 /* this sign link is not know yet.. complain. */
402 LOGP(DLINP, LOGL_ERROR, "Timeslot is not configured.\n");
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100403 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200404 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200405
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200406 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
407 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200408 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200409 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200410 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200411 }
412 msg->dst = link;
413
414 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200415 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200416 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200417 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200418 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200419 }
Maxc9fa25e2017-01-09 13:23:15 +0100420 rc = e1i_ts->line->ops->sign_link(msg);
421 if (rc < 0) {
Pablo Neira Ayusoa49c24d2012-10-16 11:24:08 +0200422 /* Don't close the signalling link if the upper layers report
423 * an error, that's too strict. BTW, the signalling layer is
424 * resposible for releasing the message.
425 */
Harald Weltecc2241b2011-07-19 16:06:06 +0200426 LOGP(DLINP, LOGL_ERROR, "Bad signalling message,"
Maxc9fa25e2017-01-09 13:23:15 +0100427 " sign_link returned error: %s.\n", strerror(-rc));
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200428 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200429
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200430 return rc;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200431err_msg:
432 msgb_free(msg);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200433err:
Harald Welte10b41302013-06-30 14:05:49 +0200434 ipaccess_drop(bfd, line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200435 return -EBADF;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200436}
437
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200438static int ts_want_write(struct e1inp_ts *e1i_ts)
439{
Harald Welte949b8a22020-10-18 23:01:53 +0200440 osmo_fd_write_enable(&e1i_ts->driver.ipaccess.fd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200441
442 return 0;
443}
444
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200445static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200446{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200447 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200448 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Eric Wildef1f3272019-07-10 18:10:31 +0200449 struct e1inp_line *line = e1i_ts->line;
450
451 /* line might not exist if != bsc||bts */
452 if (line) {
453 /* depending on caller the fsm might be dead */
454 struct osmo_fsm_inst* ka_fsm = ipaccess_line_ts(bfd, line)->driver.ipaccess.ka_fsm;
455 if (ka_fsm)
456 ipa_keepalive_fsm_stop(ka_fsm);
457
458 }
459
Pau Espin Pedrol8737ad42020-07-14 21:11:56 +0200460 e1inp_int_snd_event(e1i_ts, sign_link, S_L_INP_TEI_DN);
461 /* the first e1inp_sign_link_destroy call closes the socket. */
462 if (bfd->fd != -1) {
463 osmo_fd_unregister(bfd);
464 close(bfd->fd);
465 bfd->fd = -1;
466 /* If The bfd holds a reference to e1inp_line in ->data (BSC
467 * accepted() sockets), then release it */
468 if (bfd->data == line) {
469 bfd->data = NULL;
470 e1inp_line_put2(line, "ipa_bfd");
471 }
472 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200473}
474
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200475static void timeout_ts1_write(void *data)
476{
477 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
478
479 /* trigger write of ts1, due to tx delay timer */
480 ts_want_write(e1i_ts);
481}
482
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200483static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200484{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200485 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200486 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200487 struct e1inp_sign_link *sign_link;
488 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200489 int ret;
490
Eric Wildef1f3272019-07-10 18:10:31 +0200491 e1i_ts = ipaccess_line_ts(bfd, line);
Harald Welte949b8a22020-10-18 23:01:53 +0200492 osmo_fd_write_disable(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200493
494 /* get the next msg for this timeslot */
495 msg = e1inp_tx_ts(e1i_ts, &sign_link);
496 if (!msg) {
497 /* no message after tx delay timer */
498 return 0;
499 }
500
501 switch (sign_link->type) {
502 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200503 case E1INP_SIGN_RSL:
Harald Welte46fc7e22014-08-18 19:04:26 +0200504 case E1INP_SIGN_OSMO:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200505 break;
506 default:
Harald Welte949b8a22020-10-18 23:01:53 +0200507 osmo_fd_write_enable(bfd); /* come back for more msg */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200508 ret = -EINVAL;
509 goto out;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200510 }
511
512 msg->l2h = msg->data;
Harald Welteb65f58f2014-08-20 22:04:11 +0200513 ipa_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200514
Vadim Yanitskiy77a7cf42020-11-29 20:39:57 +0100515 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "TX %u: %s\n", ts_nr,
Pau Espin Pedrol3bdf59b2020-11-23 13:47:16 +0100516 osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200517
518 ret = send(bfd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200519 if (ret != msg->len) {
Pau Espin Pedrol3bdf59b2020-11-23 13:47:16 +0100520 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling "
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200521 "message. Reason: %s\n", strerror(errno));
522 goto err;
523 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200524
525 /* set tx delay timer for next event */
Pablo Neira Ayusob26f2fd2017-06-07 18:32:13 +0200526 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200527
528 /* Reducing this might break the nanoBTS 900 init. */
529 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
530
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200531out:
532 msgb_free(msg);
533 return ret;
534err:
Harald Welte10b41302013-06-30 14:05:49 +0200535 ipaccess_drop(bfd, line);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200536 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200537 return ret;
538}
539
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200540static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200541{
542 struct e1inp_line *line = bfd->data;
543
544 return __handle_ts1_write(bfd, line);
545}
546
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200547static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200548{
549 struct e1inp_line *line = link->line;
550
551 return __handle_ts1_write(link->ofd, line);
552}
553
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200554/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200555int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200556{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200557 int rc = 0;
558
Harald Weltede3959e2020-10-18 22:28:50 +0200559 if (what & OSMO_FD_READ)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200560 rc = handle_ts1_read(bfd);
Harald Weltede3959e2020-10-18 22:28:50 +0200561 if (rc != -EBADF && (what & OSMO_FD_WRITE))
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200562 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200563
564 return rc;
565}
566
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200567static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200568
569struct e1inp_driver ipaccess_driver = {
570 .name = "ipa",
571 .want_write = ts_want_write,
572 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200573 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200574 .default_delay = 0,
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100575 .has_keepalive = 1,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200576};
577
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100578static void update_fd_settings(struct e1inp_line *line, int fd)
579{
580 int ret;
581 int val;
582
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100583 if (line->keepalive_num_probes) {
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100584 /* Enable TCP keepalive to find out if the connection is gone */
585 val = 1;
586 ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
587 if (ret < 0)
588 LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive: %s\n",
589 strerror(errno));
590 else
591 LOGP(DLINP, LOGL_NOTICE, "Keepalive is set: %i\n", ret);
592
593#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
594 /* The following options are not portable! */
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100595 val = line->keepalive_idle_timeout > 0 ?
596 line->keepalive_idle_timeout :
597 DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100598 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
599 &val, sizeof(val));
600 if (ret < 0)
601 LOGP(DLINP, LOGL_NOTICE,
602 "Failed to set keepalive idle time: %s\n",
603 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100604 val = line->keepalive_probe_interval > -1 ?
605 line->keepalive_probe_interval :
606 DEFAULT_TCP_KEEPALIVE_INTERVAL;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100607 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
608 &val, sizeof(val));
609 if (ret < 0)
610 LOGP(DLINP, LOGL_NOTICE,
611 "Failed to set keepalive interval: %s\n",
612 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100613 val = line->keepalive_num_probes > 0 ?
614 line->keepalive_num_probes :
615 DEFAULT_TCP_KEEPALIVE_RETRY_COUNT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100616 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
617 &val, sizeof(val));
618 if (ret < 0)
619 LOGP(DLINP, LOGL_NOTICE,
620 "Failed to set keepalive count: %s\n",
621 strerror(errno));
Eric Wild6eb186c2019-06-21 15:26:25 +0200622#if defined(TCP_USER_TIMEOUT)
623 val = 1000 * line->keepalive_num_probes *
624 line->keepalive_probe_interval +
625 line->keepalive_idle_timeout;
626 ret = setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
627 &val, sizeof(val));
628 if (ret < 0)
629 LOGP(DLINP, LOGL_NOTICE,
630 "Failed to set user timoeut: %s\n",
631 strerror(errno));
632#endif
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100633#endif
Holger Hans Peter Freytherf465a4c2014-02-03 09:34:02 +0100634 }
Oliver Smith62725d02020-04-17 10:57:46 +0200635
636 val = 1;
637 ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
638 if (ret < 0)
639 LOGP(DLINP, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100640}
641
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200642/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200643static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200644{
645 int ret;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100646 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200647 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200648 struct e1inp_ts *e1i_ts;
649 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200650
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200651 /* clone virtual E1 line for this new OML link. */
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200652 line = e1inp_line_clone(tall_ipa_ctx, link->line, "ipa_bfd");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200653 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200654 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200655 return -ENOMEM;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200656 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200657
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200658 /* create virrtual E1 timeslots for signalling */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200659 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200660
661 /* initialize the fds */
662 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
663 line->ts[i].driver.ipaccess.fd.fd = -1;
664
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200665 e1i_ts = e1inp_line_ipa_oml_ts(line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200666
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200667 bfd = &e1i_ts->driver.ipaccess.fd;
Harald Weltede3959e2020-10-18 22:28:50 +0200668 osmo_fd_setup(bfd, fd, OSMO_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_OML);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200669 ret = osmo_fd_register(bfd);
670 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200671 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200672 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200673 }
674
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100675 update_fd_settings(line, bfd->fd);
Daniel Willmann85980722014-01-09 14:30:55 +0100676
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200677 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200678 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200679 if (ret < 0) {
680 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
681 strerror(errno));
682 goto err_socket;
683 }
684 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200685
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200686err_socket:
687 osmo_fd_unregister(bfd);
688err_line:
689 close(bfd->fd);
690 bfd->fd = -1;
Pau Espin Pedrola2ff7332020-07-14 21:12:50 +0200691 bfd->data = NULL;
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200692 e1inp_line_put2(line, "ipa_bfd");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200693 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200694}
695
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200696static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200697{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200698 struct e1inp_line *line;
699 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200700 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200701 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200702
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200703 /* We don't know yet which OML link to associate it with. Thus, we
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200704 * allocate a temporary E1 line until we have received ID. */
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200705 line = e1inp_line_clone(tall_ipa_ctx, link->line, "ipa_bfd");
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200706 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200707 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200708 return -ENOMEM;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200709 }
710 /* initialize the fds */
711 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
712 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200713
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200714 /* we need this to initialize this in case to avoid crashes in case
715 * that the socket is closed before we've seen an ID_RESP. */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200716 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200717
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200718 e1i_ts = e1inp_line_ipa_rsl_ts(line, 0);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200719
720 bfd = &e1i_ts->driver.ipaccess.fd;
Harald Weltede3959e2020-10-18 22:28:50 +0200721 osmo_fd_setup(bfd, fd, OSMO_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_RSL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200722 ret = osmo_fd_register(bfd);
723 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200724 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200725 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200726 }
727 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200728 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200729 if (ret < 0) {
730 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
731 strerror(errno));
732 goto err_socket;
733 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100734 update_fd_settings(line, bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200735 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200736
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200737err_socket:
738 osmo_fd_unregister(bfd);
739err_line:
740 close(bfd->fd);
741 bfd->fd = -1;
Pau Espin Pedrola2ff7332020-07-14 21:12:50 +0200742 bfd->data = NULL;
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200743 e1inp_line_put2(line, "ipa_bfd");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200744 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200745}
746
Harald Weltee416e2e2017-05-26 13:11:59 +0200747#define IPA_STRING_MAX 64
748
749static struct msgb *
Max71393522018-01-08 15:42:00 +0100750ipa_bts_id_resp(const struct ipaccess_unit *dev, uint8_t *data, int len, int trx_nr)
Harald Weltee416e2e2017-05-26 13:11:59 +0200751{
752 struct msgb *nmsg;
753 char str[IPA_STRING_MAX];
754 uint8_t *tag;
755
756 memset(str, 0, sizeof(str));
757
758 nmsg = ipa_msg_alloc(0);
759 if (!nmsg)
760 return NULL;
761
762 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
763 while (len) {
764 if (len < 2) {
765 LOGP(DLINP, LOGL_NOTICE,
766 "Short read of ipaccess tag\n");
767 msgb_free(nmsg);
768 return NULL;
769 }
770 switch (data[1]) {
771 case IPAC_IDTAG_UNIT:
772 snprintf(str, sizeof(str), "%u/%u/%u",
773 dev->site_id, dev->bts_id, trx_nr);
774 break;
775 case IPAC_IDTAG_MACADDR:
776 snprintf(str, sizeof(str),
777 "%02x:%02x:%02x:%02x:%02x:%02x",
778 dev->mac_addr[0], dev->mac_addr[1],
779 dev->mac_addr[2], dev->mac_addr[3],
780 dev->mac_addr[4], dev->mac_addr[5]);
781 break;
782 case IPAC_IDTAG_LOCATION1:
783 if (dev->location1)
Stefan Sperling961776a2018-12-06 13:09:48 +0100784 osmo_strlcpy(str, dev->location1, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200785 break;
786 case IPAC_IDTAG_LOCATION2:
787 if (dev->location2)
Stefan Sperling961776a2018-12-06 13:09:48 +0100788 osmo_strlcpy(str, dev->location2, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200789 break;
790 case IPAC_IDTAG_EQUIPVERS:
791 if (dev->equipvers)
Stefan Sperling961776a2018-12-06 13:09:48 +0100792 osmo_strlcpy(str, dev->equipvers, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200793 break;
794 case IPAC_IDTAG_SWVERSION:
795 if (dev->swversion)
Neels Hofmeyr4c57eef2018-07-26 17:28:24 +0200796 osmo_strlcpy(str, dev->swversion, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200797 break;
798 case IPAC_IDTAG_UNITNAME:
799 snprintf(str, sizeof(str),
800 "%s-%02x-%02x-%02x-%02x-%02x-%02x",
801 dev->unit_name,
802 dev->mac_addr[0], dev->mac_addr[1],
803 dev->mac_addr[2], dev->mac_addr[3],
804 dev->mac_addr[4], dev->mac_addr[5]);
805 break;
806 case IPAC_IDTAG_SERNR:
807 if (dev->serno)
Stefan Sperling961776a2018-12-06 13:09:48 +0100808 osmo_strlcpy(str, dev->serno, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200809 break;
810 default:
811 LOGP(DLINP, LOGL_NOTICE,
812 "Unknown ipaccess tag 0x%02x\n", *data);
813 msgb_free(nmsg);
814 return NULL;
815 }
Harald Weltee416e2e2017-05-26 13:11:59 +0200816
817 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
818 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
819 tag[0] = 0x00;
820 tag[1] = 1 + strlen(str) + 1;
821 tag[2] = data[1];
822 memcpy(tag + 3, str, strlen(str) + 1);
823 data += 2;
824 len -= 2;
825 }
826 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
827 return nmsg;
828}
829
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200830static struct msgb *ipa_bts_id_ack(void)
831{
832 struct msgb *nmsg2;
833
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200834 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200835 if (!nmsg2)
836 return NULL;
837
838 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200839 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200840
841 return nmsg2;
842}
843
Harald Welte51de9ca2013-06-30 20:13:16 +0200844static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up)
845{
846 struct e1inp_line *line = link->line;
847
Eric Wildef1f3272019-07-10 18:10:31 +0200848 if (up) {
849 struct osmo_fsm_inst *ka_fsm = ipaccess_line_ts(link->ofd, line)->driver.ipaccess.ka_fsm;
850
851 update_fd_settings(line, link->ofd->fd);
852 if (ka_fsm && line->ipa_kap)
853 ipa_keepalive_fsm_start(ka_fsm);
854 return;
855 }
Harald Welte51de9ca2013-06-30 20:13:16 +0200856
857 if (line->ops->sign_link_down)
858 line->ops->sign_link_down(line);
859}
860
Harald Welte783715b2014-08-17 18:24:51 +0200861/* handle incoming message to BTS, check if it is an IPA CCM, and if yes,
862 * handle it accordingly (PING/PONG/ID_REQ/ID_RESP/ID_ACK) */
863int ipaccess_bts_handle_ccm(struct ipa_client_conn *link,
864 struct ipaccess_unit *dev, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200865{
866 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200867 struct msgb *rmsg;
868 int ret = 0;
Eric Wildef1f3272019-07-10 18:10:31 +0200869 /* line might not exist if != bsc||bts */
870 struct e1inp_line *line = link->line;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200871
872 /* special handling for IPA CCM. */
873 if (hh->proto == IPAC_PROTO_IPACCESS) {
874 uint8_t msg_type = *(msg->l2h);
Eric Wildef1f3272019-07-10 18:10:31 +0200875 struct osmo_fsm_inst* ka_fsm = NULL;
876
877 /* peek the pong for our keepalive fsm */
878 if (line && msg_type == IPAC_MSGT_PONG) {
879 ka_fsm = ipaccess_line_ts(link->ofd, line)->driver.ipaccess.ka_fsm;
880 ipa_keepalive_fsm_pong_received(ka_fsm);
881 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200882
883 /* ping, pong and acknowledgment cases. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200884 ret = ipa_ccm_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200885 if (ret < 0)
886 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200887
888 /* this is a request for identification from the BSC. */
889 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200890 uint8_t *data = msgb_l2(msg);
891 int len = msgb_l2len(msg);
Andreas Eversbergf422a752014-01-21 14:54:41 +0100892 int trx_nr = 0;
893
894 if (link->ofd->priv_nr >= E1INP_SIGN_RSL)
895 trx_nr = link->ofd->priv_nr - E1INP_SIGN_RSL;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200896
Vadim Yanitskiy69ae2382019-12-02 02:42:43 +0700897 LOGP(DLINP, LOGL_NOTICE, "received ID_GET for unit ID %u/%u/%u\n",
Pau Espin Pedroldd95eb62018-10-02 20:05:28 +0200898 dev->site_id, dev->bts_id, trx_nr);
Harald Weltee416e2e2017-05-26 13:11:59 +0200899 rmsg = ipa_bts_id_resp(dev, data + 1, len - 1, trx_nr);
Harald Welteb65f58f2014-08-20 22:04:11 +0200900 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200901 if (ret != rmsg->len) {
902 LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP "
903 "message. Reason: %s\n", strerror(errno));
904 goto err_rmsg;
905 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200906 msgb_free(rmsg);
907
908 /* send ID_ACK. */
909 rmsg = ipa_bts_id_ack();
Harald Welteb65f58f2014-08-20 22:04:11 +0200910 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200911 if (ret != rmsg->len) {
912 LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK "
913 "message. Reason: %s\n", strerror(errno));
914 goto err_rmsg;
915 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200916 msgb_free(rmsg);
Harald Welte783715b2014-08-17 18:24:51 +0200917 }
918 return 1;
919 }
Harald Welte6eddd472013-06-30 20:18:53 +0200920
Harald Welte783715b2014-08-17 18:24:51 +0200921 return 0;
922
923err_rmsg:
924 msgb_free(rmsg);
925err:
926 ipa_client_conn_close(link);
927 return -1;
928}
929
930static int ipaccess_bts_read_cb(struct ipa_client_conn *link, struct msgb *msg)
931{
932 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
933 struct e1inp_ts *e1i_ts = NULL;
934 struct e1inp_sign_link *sign_link;
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200935 uint8_t msg_type = *(msg->l2h);
Harald Welte783715b2014-08-17 18:24:51 +0200936 int ret = 0;
937
938 /* special handling for IPA CCM. */
939 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200940 /* this is a request for identification from the BSC. */
941 if (msg_type == IPAC_MSGT_ID_GET) {
942 if (!link->line->ops->sign_link_up) {
943 LOGP(DLINP, LOGL_ERROR,
944 "Unable to set signal link, "
945 "closing socket.\n");
Harald Welte783715b2014-08-17 18:24:51 +0200946 goto err;
947 }
948 }
949 }
950
951 /* core CCM handling */
952 ret = ipaccess_bts_handle_ccm(link, link->line->ops->cfg.ipa.dev, msg);
953 if (ret < 0)
954 goto err;
955
956 if (ret == 1 && hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200957 if (msg_type == IPAC_MSGT_ID_GET) {
Harald Welte6eddd472013-06-30 20:18:53 +0200958 sign_link = link->line->ops->sign_link_up(msg,
959 link->line,
960 link->ofd->priv_nr);
961 if (sign_link == NULL) {
962 LOGP(DLINP, LOGL_ERROR,
963 "Unable to set signal link, "
964 "closing socket.\n");
Harald Welte6eddd472013-06-30 20:18:53 +0200965 goto err;
966 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200967 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200968 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200969 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200970 } else if (link->port == IPA_TCP_PORT_OML)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200971 e1i_ts = e1inp_line_ipa_oml_ts(link->line);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200972 else if (link->port == IPA_TCP_PORT_RSL)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200973 e1i_ts = e1inp_line_ipa_rsl_ts(link->line, link->ofd->priv_nr - E1INP_SIGN_RSL);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200974
Pablo Neira Ayusob9487012013-07-05 14:38:43 +0200975 OSMO_ASSERT(e1i_ts != NULL);
976
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200977 if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
978 LOGP(DLINP, LOGL_ERROR, "Signalling link not initialized. Discarding."
979 " port=%u msg_type=%u\n", link->port, msg_type);
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200980 goto err;
981 }
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +0200982
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200983 /* look up for some existing signaling link. */
984 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
985 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200986 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200987 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200988 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200989 }
990 msg->dst = sign_link;
991
992 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200993 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200994 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200995 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200996 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200997 }
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200998 return link->line->ops->sign_link(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200999
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +02001000err:
Harald Welte783715b2014-08-17 18:24:51 +02001001 ipa_client_conn_close(link);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +02001002 msgb_free(msg);
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +02001003 return -EBADF;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +02001004}
1005
Pablo Neira Ayusod6216402011-08-31 16:47:44 +02001006struct ipaccess_line {
Oliver Smith65ae42c2020-01-22 11:24:52 +01001007 bool line_already_initialized;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +02001008};
1009
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001010static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001011{
1012 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +02001013 struct ipaccess_line *il;
1014
1015 if (!line->driver_data)
1016 line->driver_data = talloc_zero(line, struct ipaccess_line);
1017
1018 if (!line->driver_data) {
1019 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
1020 return -ENOMEM;
1021 }
1022 il = line->driver_data;
1023
1024 /* We only initialize this line once. */
1025 if (il->line_already_initialized)
1026 return 0;
1027
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001028 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001029 case E1INP_LINE_R_BSC: {
1030 struct ipa_server_link *oml_link, *rsl_link;
Max4c4a1c22016-12-30 15:10:47 +01001031 const char *ipa = e1inp_ipa_get_bind_addr();
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001032
Max4c4a1c22016-12-30 15:10:47 +01001033 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode on %s "
1034 "with OML %u and RSL %u TCP ports\n", ipa,
1035 IPA_TCP_PORT_OML, IPA_TCP_PORT_RSL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001036
Max4c4a1c22016-12-30 15:10:47 +01001037 oml_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001038 IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +02001039 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001040 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001041 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001042 "BSC link: %s\n", strerror(errno));
1043 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +02001044 }
Harald Welte41547552021-04-28 18:23:21 +02001045 oml_link->dscp = g_e1inp_ipaccess_pars.oml.dscp;
1046 oml_link->priority = g_e1inp_ipaccess_pars.oml.priority;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001047 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001048 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001049 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001050 ipa_server_link_destroy(oml_link);
1051 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +02001052 }
Max4c4a1c22016-12-30 15:10:47 +01001053 rsl_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001054 IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +02001055 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001056 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001057 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001058 "BSC link: %s\n", strerror(errno));
1059 return -ENOMEM;
1060 }
Harald Welte41547552021-04-28 18:23:21 +02001061 rsl_link->dscp = g_e1inp_ipaccess_pars.rsl.dscp;
1062 rsl_link->priority = g_e1inp_ipaccess_pars.rsl.priority;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001063 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001064 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001065 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001066 ipa_server_link_destroy(rsl_link);
1067 return -EIO;
1068 }
1069 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001070 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001071 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001072 case E1INP_LINE_R_BTS: {
Harald Welte84f67b22013-06-30 13:13:59 +02001073 struct ipa_client_conn *link;
Eric Wildef1f3272019-07-10 18:10:31 +02001074 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001075
Max4c4a1c22016-12-30 15:10:47 +01001076 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode, "
1077 "OML connecting to %s:%u\n", line->ops->cfg.ipa.addr,
1078 IPA_TCP_PORT_OML);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001079
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001080 link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +02001081 e1inp_line_ipa_oml_ts(line),
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +02001082 E1INP_SIGN_OML,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001083 NULL, 0,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001084 line->ops->cfg.ipa.addr,
1085 IPA_TCP_PORT_OML,
Harald Welte51de9ca2013-06-30 20:13:16 +02001086 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001087 ipaccess_bts_read_cb,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +02001088 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001089 line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001090 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001091 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001092 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001093 return -ENOMEM;
1094 }
Harald Welte41547552021-04-28 18:23:21 +02001095 link->dscp = g_e1inp_ipaccess_pars.oml.dscp;
1096 link->priority = g_e1inp_ipaccess_pars.oml.priority;
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001097 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001098 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001099 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001100 ipa_client_conn_close(link);
1101 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001102 return -EIO;
1103 }
Eric Wildef1f3272019-07-10 18:10:31 +02001104
1105 e1i_ts = e1inp_line_ipa_oml_ts(line);
1106 ipaccess_bts_keepalive_fsm_alloc(e1i_ts, link, "oml_bts_to_bsc");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001107 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001108 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001109 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001110 default:
1111 break;
1112 }
Philipp Maierea0f1bd2021-05-20 21:01:23 +02001113
1114 il->line_already_initialized = true;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001115 return ret;
1116}
1117
Andreas Eversbergf422a752014-01-21 14:54:41 +01001118
1119/* backwards compatibility */
Harald Welte84f67b22013-06-30 13:13:59 +02001120int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line,
1121 const char *rem_addr, uint16_t rem_port)
1122{
Andreas Eversbergf422a752014-01-21 14:54:41 +01001123 return e1inp_ipa_bts_rsl_connect_n(line, rem_addr, rem_port, 0);
1124}
1125
1126int e1inp_ipa_bts_rsl_connect_n(struct e1inp_line *line,
1127 const char *rem_addr, uint16_t rem_port,
1128 uint8_t trx_nr)
1129{
Harald Welte84f67b22013-06-30 13:13:59 +02001130 struct ipa_client_conn *rsl_link;
Eric Wildef1f3272019-07-10 18:10:31 +02001131 struct e1inp_ts *e1i_ts = e1inp_line_ipa_rsl_ts(line, trx_nr);
Harald Welte84f67b22013-06-30 13:13:59 +02001132
Andreas Eversbergf422a752014-01-21 14:54:41 +01001133 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
1134 LOGP(DLINP, LOGL_ERROR, "cannot create RSL BTS link: "
1135 "trx_nr (%d) out of range\n", trx_nr);
1136 return -EINVAL;
1137 }
Andreas Eversbergf422a752014-01-21 14:54:41 +01001138
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001139 rsl_link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +02001140 e1inp_line_ipa_rsl_ts(line, trx_nr),
Andreas Eversbergf422a752014-01-21 14:54:41 +01001141 E1INP_SIGN_RSL+trx_nr,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001142 NULL, 0,
Harald Welte84f67b22013-06-30 13:13:59 +02001143 rem_addr, rem_port,
Harald Welte51de9ca2013-06-30 20:13:16 +02001144 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001145 ipaccess_bts_read_cb,
Harald Welte84f67b22013-06-30 13:13:59 +02001146 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001147 line);
Harald Welte84f67b22013-06-30 13:13:59 +02001148 if (rsl_link == NULL) {
1149 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
1150 "BTS link: %s\n", strerror(errno));
1151 return -ENOMEM;
1152 }
Harald Welte41547552021-04-28 18:23:21 +02001153 rsl_link->dscp = g_e1inp_ipaccess_pars.rsl.dscp;
1154 rsl_link->priority = g_e1inp_ipaccess_pars.rsl.priority;
Harald Welte84f67b22013-06-30 13:13:59 +02001155 if (ipa_client_conn_open(rsl_link) < 0) {
1156 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
1157 strerror(errno));
1158 ipa_client_conn_close(rsl_link);
1159 ipa_client_conn_destroy(rsl_link);
1160 return -EIO;
1161 }
Eric Wildef1f3272019-07-10 18:10:31 +02001162
1163 ipaccess_bts_keepalive_fsm_alloc(e1i_ts, rsl_link, "rsl_bts_to_bsc");
Harald Welte84f67b22013-06-30 13:13:59 +02001164 return 0;
1165}
1166
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001167void e1inp_ipaccess_init(void)
1168{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +02001169 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001170 e1inp_driver_register(&ipaccess_driver);
1171}
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001172
1173void e1inp_ipa_set_bind_addr(const char *ip_bind_addr)
1174{
1175 talloc_free((char*)ipaccess_driver.bind_addr);
1176 ipaccess_driver.bind_addr = NULL;
1177
1178 if (ip_bind_addr)
1179 ipaccess_driver.bind_addr = talloc_strdup(tall_ipa_ctx,
1180 ip_bind_addr);
1181}
1182
1183const char *e1inp_ipa_get_bind_addr(void)
1184{
1185 return ipaccess_driver.bind_addr?
1186 ipaccess_driver.bind_addr
1187 : "0.0.0.0";
1188}