blob: 96828ebaca820149e9b52a3f8ed440a37fbdf439 [file] [log] [blame]
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001/* OpenBSC Abis input driver for ip.access */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
4 * (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
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +020054static void *tall_ipa_ctx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020055
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020056#define TS1_ALLOC_SIZE 900
57
Daniel Willmann85980722014-01-09 14:30:55 +010058#define DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT 30
59#define DEFAULT_TCP_KEEPALIVE_INTERVAL 3
60#define DEFAULT_TCP_KEEPALIVE_RETRY_COUNT 10
61
Eric Wildef1f3272019-07-10 18:10:31 +020062static inline struct e1inp_ts *ipaccess_line_ts(struct osmo_fd *bfd, struct e1inp_line *line)
63{
64 if (bfd->priv_nr == E1INP_SIGN_OML)
65 return e1inp_line_ipa_oml_ts(line);
66 else
67 return e1inp_line_ipa_rsl_ts(line, bfd->priv_nr - E1INP_SIGN_RSL);
68}
69
70static inline void ipaccess_keepalive_fsm_cleanup(struct e1inp_ts *e1i_ts)
71{
72 struct osmo_fsm_inst *ka_fsm;
73
74 ka_fsm = e1i_ts->driver.ipaccess.ka_fsm;
75 if (ka_fsm) {
76 ipa_keepalive_fsm_stop(ka_fsm);
77 e1i_ts->driver.ipaccess.ka_fsm = NULL;
78 }
79}
80
Harald Welte10b41302013-06-30 14:05:49 +020081static int ipaccess_drop(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020082{
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +020083 int ret = 1;
Eric Wildef1f3272019-07-10 18:10:31 +020084 struct e1inp_ts *e1i_ts = ipaccess_line_ts(bfd, line);
85
86 ipaccess_keepalive_fsm_cleanup(e1i_ts);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020087
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020088 /* Error case: we did not see any ID_RESP yet for this socket. */
89 if (bfd->fd != -1) {
Harald Weltecc2241b2011-07-19 16:06:06 +020090 LOGP(DLINP, LOGL_ERROR, "Forcing socket shutdown with "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020091 "no signal link set\n");
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +020092 osmo_fd_unregister(bfd);
93 close(bfd->fd);
94 bfd->fd = -1;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020095 ret = -ENOENT;
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +020096 }
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +020097
Jacob Erlbeck98af3c32014-03-31 10:53:32 +020098 msgb_free(e1i_ts->pending_msg);
99 e1i_ts->pending_msg = NULL;
100
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200101 /* e1inp_sign_link_destroy releases the socket descriptors for us. */
102 line->ops->sign_link_down(line);
103
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200104 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200105}
106
Eric Wildef1f3272019-07-10 18:10:31 +0200107static void ipa_bsc_keepalive_write_server_cb(struct osmo_fsm_inst *fi, void *conn, struct msgb *msg)
108{
109 struct osmo_fd *bfd = (struct osmo_fd *)conn;
110 write(bfd->fd, msg->data, msg->len);
111 msgb_free(msg);
112}
113
114static int ipa_bsc_keepalive_timeout_cb(struct osmo_fsm_inst *fi, void *data)
115{
116 struct osmo_fd *bfd = (struct osmo_fd *)data;
117
118 if (bfd->fd == -1)
119 return 1;
120
121 ipaccess_drop(bfd, (struct e1inp_line *)bfd->data);
122 return 1;
123}
124
125static void ipaccess_bsc_keepalive_fsm_alloc(struct e1inp_ts *e1i_ts, struct osmo_fd *bfd, const char *id)
126{
127 struct e1inp_line *line = e1i_ts->line;
128 struct osmo_fsm_inst *ka_fsm;
129
130 ipaccess_keepalive_fsm_cleanup(e1i_ts);
131 if (!line->ipa_kap)
132 return;
133
134 ka_fsm = ipa_generic_conn_alloc_keepalive_fsm(tall_ipa_ctx, bfd, line->ipa_kap, id);
135 e1i_ts->driver.ipaccess.ka_fsm = ka_fsm;
136 if (!ka_fsm)
137 return;
138
139 ipa_keepalive_fsm_set_timeout_cb(ka_fsm, ipa_bsc_keepalive_timeout_cb);
140 ipa_keepalive_fsm_set_send_cb(ka_fsm, ipa_bsc_keepalive_write_server_cb);
141 ipa_keepalive_fsm_start(ka_fsm);
142}
143
144static void ipa_bts_keepalive_write_client_cb(struct osmo_fsm_inst *fi, void *conn, struct msgb *msg) {
145 struct ipa_client_conn *link = (struct ipa_client_conn *)conn;
146 int ret = 0;
147
148 ret = ipa_send(link->ofd->fd, msg->data, msg->len);
149 if (ret != msg->len) {
150 LOGP(DLINP, LOGL_ERROR, "cannot send message. Reason: %s\n", strerror(errno));
151 }
152 msgb_free(msg);
153}
154
155static void update_fd_settings(struct e1inp_line *line, int fd);
156static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up);
157
158static int ipa_bts_keepalive_timeout_cb(struct osmo_fsm_inst *fi, void *conn) {
159 ipaccess_bts_updown_cb(conn, false);
160 return 1;
161}
162
163static void ipaccess_bts_keepalive_fsm_alloc(struct e1inp_ts *e1i_ts, struct ipa_client_conn *client, const char *id)
164{
165 struct e1inp_line *line = e1i_ts->line;
166 struct osmo_fsm_inst *ka_fsm;
167
168 ipaccess_keepalive_fsm_cleanup(e1i_ts);
169 if (!line->ipa_kap)
170 return;
171
172 ka_fsm = ipa_client_conn_alloc_keepalive_fsm(client, line->ipa_kap, id);
173 e1i_ts->driver.ipaccess.ka_fsm = ka_fsm;
174 if (!ka_fsm)
175 return;
176
177 ipa_keepalive_fsm_set_timeout_cb(ka_fsm, ipa_bts_keepalive_timeout_cb);
178 ipa_keepalive_fsm_set_send_cb(ka_fsm, ipa_bts_keepalive_write_client_cb);
179}
180
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200181/* Returns -1 on error, and 0 or 1 on success. If -1 or 1 is returned, line has
182 * been released and should not be used anymore by the caller. */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200183static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
184 struct osmo_fd *bfd)
185{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200186 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200187 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200188 struct ipaccess_unit unit_data = {};
189 struct e1inp_sign_link *sign_link;
190 char *unitid;
191 int len, ret;
Eric Wildef1f3272019-07-10 18:10:31 +0200192 struct e1inp_ts *e1i_ts;
193 struct osmo_fsm_inst *ka_fsm;
194
195 /* peek the pong for our keepalive fsm */
196 e1i_ts = ipaccess_line_ts(bfd, line);
197 ka_fsm = e1i_ts->driver.ipaccess.ka_fsm;
198 if (ka_fsm && msg_type == IPAC_MSGT_PONG)
199 ipa_keepalive_fsm_pong_received(ka_fsm);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200200
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200201 /* Handle IPA PING, PONG and ID_ACK messages. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200202 ret = ipa_ccm_rcvmsg_base(msg, bfd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200203 switch(ret) {
204 case -1:
205 /* error in IPA control message handling */
206 goto err;
207 case 1:
208 /* this is an IPA control message, skip further processing */
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200209 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200210 case 0:
211 /* this is not an IPA control message, continue */
212 break;
213 default:
214 LOGP(DLINP, LOGL_ERROR, "Unexpected return from "
Harald Welteb65f58f2014-08-20 22:04:11 +0200215 "ipa_ccm_rcvmsg_base "
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200216 "(ret=%d)\n", ret);
217 goto err;
218 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200219
220 switch (msg_type) {
221 case IPAC_MSGT_ID_RESP:
Harald Weltecc2241b2011-07-19 16:06:06 +0200222 DEBUGP(DLMI, "ID_RESP\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200223 /* parse tags, search for Unit ID */
Harald Welte82eb55e2018-08-01 13:22:55 +0200224 ret = ipa_ccm_id_resp_parse(&tlvp, (const uint8_t *)msg->l2h+1, msgb_l2len(msg)-1);
Harald Weltecc2241b2011-07-19 16:06:06 +0200225 DEBUGP(DLMI, "\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200226 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200227 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200228 "with malformed TLVs\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200229 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200230 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200231 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200232 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200233 "without unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200234 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200235
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200236 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200237 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200238 if (len < 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200239 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200240 "with too small unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200241 goto err;
242 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200243 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
244 unitid[len - 1] = '\0';
Vadim Yanitskiy1c94f6a2019-12-02 02:16:07 +0700245 ret = ipa_parse_unitid(unitid, &unit_data);
246 if (ret) {
247 LOGP(DLINP, LOGL_ERROR, "Failed to parse unit ID '%s'\n", unitid);
248 goto err;
249 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200250
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200251 if (!line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200252 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200253 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200254 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200255 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200256 /* the BSC creates the new sign links at this stage. */
257 if (bfd->priv_nr == E1INP_SIGN_OML) {
258 sign_link =
259 line->ops->sign_link_up(&unit_data, line,
260 E1INP_SIGN_OML);
261 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200262 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200263 "Unable to set signal link, "
264 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200265 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200266 }
Eric Wildef1f3272019-07-10 18:10:31 +0200267
268 ipaccess_bsc_keepalive_fsm_alloc(e1i_ts, bfd, "oml_bsc_to_bts");
269
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200270 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200271 struct e1inp_ts *ts;
Eric Wildef1f3272019-07-10 18:10:31 +0200272 struct osmo_fd *newbfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200273 struct e1inp_line *new_line;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200274
275 sign_link =
276 line->ops->sign_link_up(&unit_data, line,
277 E1INP_SIGN_RSL);
278 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200279 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200280 "Unable to set signal link, "
281 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200282 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200283 }
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200284 /* this is a bugtrap, the BSC should be using the
285 * virtual E1 line used by OML for this RSL link. */
286 if (sign_link->ts->line == line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200287 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200288 "Fix your BSC, you should use the "
289 "E1 line used by the OML link for "
290 "your RSL link.\n");
291 return 0;
292 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200293 /* Finally, we know which OML link is associated with
294 * this RSL link, attach it to this socket. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200295 bfd->data = new_line = sign_link->ts->line;
296 e1inp_line_get(new_line);
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200297 ts = e1inp_line_ipa_rsl_ts(new_line, unit_data.trx_id);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200298 newbfd = &ts->driver.ipaccess.fd;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200299
300 /* get rid of our old temporary bfd */
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200301 /* preserve 'newbfd->when' flags potentially set by sign_link_up() */
Pau Espin Pedrol67902bb2018-08-23 14:35:08 +0200302 osmo_fd_setup(newbfd, bfd->fd, newbfd->when | bfd->when, bfd->cb,
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200303 bfd->data, E1INP_SIGN_RSL + unit_data.trx_id);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200304 osmo_fd_unregister(bfd);
305 bfd->fd = -1;
Harald Welteae3a9932016-11-26 09:25:23 +0100306 ret = osmo_fd_register(newbfd);
307 if (ret < 0) {
308 LOGP(DLINP, LOGL_ERROR,
309 "could not register FD\n");
310 goto err;
311 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200312 /* now we can release the dummy RSL line. */
313 e1inp_line_put(line);
Eric Wildef1f3272019-07-10 18:10:31 +0200314
315 e1i_ts = ipaccess_line_ts(newbfd, new_line);
316 ipaccess_bsc_keepalive_fsm_alloc(e1i_ts, newbfd, "rsl_bsc_to_bts");
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200317 return 1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200318 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200319 break;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200320 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200321 LOGP(DLINP, LOGL_ERROR, "Unknown IPA message type\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200322 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200323 }
324 return 0;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200325err:
326 osmo_fd_unregister(bfd);
Harald Welteb4a7db02019-07-21 11:51:56 +0200327 if (bfd->fd != -1) {
328 close(bfd->fd);
329 bfd->fd = -1;
330 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200331 e1inp_line_put(line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200332 return -1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200333}
334
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200335/* Returns -EBADF if bfd cannot be used by the caller anymore after return. */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200336static int handle_ts1_read(struct osmo_fd *bfd)
337{
338 struct e1inp_line *line = bfd->data;
339 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200340 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200341 struct e1inp_sign_link *link;
342 struct ipaccess_head *hh;
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200343 struct msgb *msg = NULL;
Maxc9fa25e2017-01-09 13:23:15 +0100344 int ret, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200345
Eric Wildef1f3272019-07-10 18:10:31 +0200346 e1i_ts = ipaccess_line_ts(bfd, line);
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200347 ret = ipa_msg_recv_buffered(bfd->fd, &msg, &e1i_ts->pending_msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200348 if (ret < 0) {
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200349 if (ret == -EAGAIN)
350 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200351 LOGP(DLINP, LOGL_NOTICE, "Sign link problems, "
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200352 "closing socket. Reason: %s\n", strerror(-ret));
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200353 goto err;
354 } else if (ret == 0) {
355 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
356 goto err;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200357 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200358 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200359
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200360 hh = (struct ipaccess_head *) msg->data;
361 if (hh->proto == IPAC_PROTO_IPACCESS) {
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200362 ret = ipaccess_rcvmsg(line, msg, bfd);
363 /* BIG FAT WARNING: bfd might no longer exist here (ret != 0),
364 * since ipaccess_rcvmsg() might have free'd it !!! */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200365 msgb_free(msg);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200366 return ret != 0 ? -EBADF : 0;
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100367 } else if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
368 /* this sign link is not know yet.. complain. */
369 LOGP(DLINP, LOGL_ERROR, "Timeslot is not configured.\n");
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100370 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200371 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200372
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200373 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
374 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200375 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200376 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200377 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200378 }
379 msg->dst = link;
380
381 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200382 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200383 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200384 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200385 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200386 }
Maxc9fa25e2017-01-09 13:23:15 +0100387 rc = e1i_ts->line->ops->sign_link(msg);
388 if (rc < 0) {
Pablo Neira Ayusoa49c24d2012-10-16 11:24:08 +0200389 /* Don't close the signalling link if the upper layers report
390 * an error, that's too strict. BTW, the signalling layer is
391 * resposible for releasing the message.
392 */
Harald Weltecc2241b2011-07-19 16:06:06 +0200393 LOGP(DLINP, LOGL_ERROR, "Bad signalling message,"
Maxc9fa25e2017-01-09 13:23:15 +0100394 " sign_link returned error: %s.\n", strerror(-rc));
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200395 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200396
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200397 return rc;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200398err_msg:
399 msgb_free(msg);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200400err:
Harald Welte10b41302013-06-30 14:05:49 +0200401 ipaccess_drop(bfd, line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200402 return -EBADF;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200403}
404
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200405static int ts_want_write(struct e1inp_ts *e1i_ts)
406{
407 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
408
409 return 0;
410}
411
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200412static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200413{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200414 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200415 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Eric Wildef1f3272019-07-10 18:10:31 +0200416 struct e1inp_line *line = e1i_ts->line;
417
418 /* line might not exist if != bsc||bts */
419 if (line) {
420 /* depending on caller the fsm might be dead */
421 struct osmo_fsm_inst* ka_fsm = ipaccess_line_ts(bfd, line)->driver.ipaccess.ka_fsm;
422 if (ka_fsm)
423 ipa_keepalive_fsm_stop(ka_fsm);
424
425 }
426
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100427 return e1inp_close_socket(e1i_ts, sign_link, bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200428}
429
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200430static void timeout_ts1_write(void *data)
431{
432 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
433
434 /* trigger write of ts1, due to tx delay timer */
435 ts_want_write(e1i_ts);
436}
437
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200438static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200439{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200440 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200441 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200442 struct e1inp_sign_link *sign_link;
443 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200444 int ret;
445
Eric Wildef1f3272019-07-10 18:10:31 +0200446 e1i_ts = ipaccess_line_ts(bfd, line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200447 bfd->when &= ~BSC_FD_WRITE;
448
449 /* get the next msg for this timeslot */
450 msg = e1inp_tx_ts(e1i_ts, &sign_link);
451 if (!msg) {
452 /* no message after tx delay timer */
453 return 0;
454 }
455
456 switch (sign_link->type) {
457 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200458 case E1INP_SIGN_RSL:
Harald Welte46fc7e22014-08-18 19:04:26 +0200459 case E1INP_SIGN_OSMO:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200460 break;
461 default:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200462 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200463 ret = -EINVAL;
464 goto out;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200465 }
466
467 msg->l2h = msg->data;
Harald Welteb65f58f2014-08-20 22:04:11 +0200468 ipa_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200469
Harald Weltecc2241b2011-07-19 16:06:06 +0200470 DEBUGP(DLMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200471
472 ret = send(bfd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200473 if (ret != msg->len) {
474 LOGP(DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling "
475 "message. Reason: %s\n", strerror(errno));
476 goto err;
477 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200478
479 /* set tx delay timer for next event */
Pablo Neira Ayusob26f2fd2017-06-07 18:32:13 +0200480 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200481
482 /* Reducing this might break the nanoBTS 900 init. */
483 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
484
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200485out:
486 msgb_free(msg);
487 return ret;
488err:
Eric Wildef1f3272019-07-10 18:10:31 +0200489 ipaccess_keepalive_fsm_cleanup(e1i_ts);
Harald Welte10b41302013-06-30 14:05:49 +0200490 ipaccess_drop(bfd, line);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200491 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200492 return ret;
493}
494
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200495static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200496{
497 struct e1inp_line *line = bfd->data;
498
499 return __handle_ts1_write(bfd, line);
500}
501
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200502static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200503{
504 struct e1inp_line *line = link->line;
505
506 return __handle_ts1_write(link->ofd, line);
507}
508
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200509/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200510int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200511{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200512 int rc = 0;
513
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200514 if (what & BSC_FD_READ)
515 rc = handle_ts1_read(bfd);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200516 if (rc != -EBADF && (what & BSC_FD_WRITE))
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200517 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200518
519 return rc;
520}
521
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200522static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200523
524struct e1inp_driver ipaccess_driver = {
525 .name = "ipa",
526 .want_write = ts_want_write,
527 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200528 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200529 .default_delay = 0,
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100530 .has_keepalive = 1,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200531};
532
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100533static void update_fd_settings(struct e1inp_line *line, int fd)
534{
535 int ret;
536 int val;
537
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100538 if (line->keepalive_num_probes) {
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100539 /* Enable TCP keepalive to find out if the connection is gone */
540 val = 1;
541 ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
542 if (ret < 0)
543 LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive: %s\n",
544 strerror(errno));
545 else
546 LOGP(DLINP, LOGL_NOTICE, "Keepalive is set: %i\n", ret);
547
548#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
549 /* The following options are not portable! */
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100550 val = line->keepalive_idle_timeout > 0 ?
551 line->keepalive_idle_timeout :
552 DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100553 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
554 &val, sizeof(val));
555 if (ret < 0)
556 LOGP(DLINP, LOGL_NOTICE,
557 "Failed to set keepalive idle time: %s\n",
558 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100559 val = line->keepalive_probe_interval > -1 ?
560 line->keepalive_probe_interval :
561 DEFAULT_TCP_KEEPALIVE_INTERVAL;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100562 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
563 &val, sizeof(val));
564 if (ret < 0)
565 LOGP(DLINP, LOGL_NOTICE,
566 "Failed to set keepalive interval: %s\n",
567 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100568 val = line->keepalive_num_probes > 0 ?
569 line->keepalive_num_probes :
570 DEFAULT_TCP_KEEPALIVE_RETRY_COUNT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100571 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
572 &val, sizeof(val));
573 if (ret < 0)
574 LOGP(DLINP, LOGL_NOTICE,
575 "Failed to set keepalive count: %s\n",
576 strerror(errno));
Eric Wild6eb186c2019-06-21 15:26:25 +0200577#if defined(TCP_USER_TIMEOUT)
578 val = 1000 * line->keepalive_num_probes *
579 line->keepalive_probe_interval +
580 line->keepalive_idle_timeout;
581 ret = setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
582 &val, sizeof(val));
583 if (ret < 0)
584 LOGP(DLINP, LOGL_NOTICE,
585 "Failed to set user timoeut: %s\n",
586 strerror(errno));
587#endif
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100588#endif
Holger Hans Peter Freytherf465a4c2014-02-03 09:34:02 +0100589 }
Oliver Smith62725d02020-04-17 10:57:46 +0200590
591 val = 1;
592 ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
593 if (ret < 0)
594 LOGP(DLINP, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100595}
596
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200597/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200598static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200599{
600 int ret;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100601 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200602 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200603 struct e1inp_ts *e1i_ts;
604 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200605
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200606 /* clone virtual E1 line for this new OML link. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200607 line = e1inp_line_clone(tall_ipa_ctx, link->line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200608 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200609 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200610 return -ENOMEM;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200611 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200612
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200613 /* create virrtual E1 timeslots for signalling */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200614 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200615
616 /* initialize the fds */
617 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
618 line->ts[i].driver.ipaccess.fd.fd = -1;
619
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200620 e1i_ts = e1inp_line_ipa_oml_ts(line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200621
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200622 bfd = &e1i_ts->driver.ipaccess.fd;
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200623 osmo_fd_setup(bfd, fd, BSC_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_OML);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200624 ret = osmo_fd_register(bfd);
625 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200626 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200627 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200628 }
629
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100630 update_fd_settings(line, bfd->fd);
Daniel Willmann85980722014-01-09 14:30:55 +0100631
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200632 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200633 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200634 if (ret < 0) {
635 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
636 strerror(errno));
637 goto err_socket;
638 }
639 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200640
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200641err_socket:
642 osmo_fd_unregister(bfd);
643err_line:
644 close(bfd->fd);
645 bfd->fd = -1;
646 e1inp_line_put(line);
647 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200648}
649
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200650static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200651{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200652 struct e1inp_line *line;
653 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200654 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200655 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200656
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200657 /* We don't know yet which OML link to associate it with. Thus, we
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200658 * allocate a temporary E1 line until we have received ID. */
659 line = e1inp_line_clone(tall_ipa_ctx, link->line);
660 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200661 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200662 return -ENOMEM;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200663 }
664 /* initialize the fds */
665 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
666 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200667
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200668 /* we need this to initialize this in case to avoid crashes in case
669 * that the socket is closed before we've seen an ID_RESP. */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200670 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200671
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200672 e1i_ts = e1inp_line_ipa_rsl_ts(line, 0);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200673
674 bfd = &e1i_ts->driver.ipaccess.fd;
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200675 osmo_fd_setup(bfd, fd, BSC_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_RSL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200676 ret = osmo_fd_register(bfd);
677 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200678 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200679 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200680 }
681 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200682 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200683 if (ret < 0) {
684 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
685 strerror(errno));
686 goto err_socket;
687 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100688 update_fd_settings(line, bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200689 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200690
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200691err_socket:
692 osmo_fd_unregister(bfd);
693err_line:
694 close(bfd->fd);
695 bfd->fd = -1;
696 e1inp_line_put(line);
697 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200698}
699
Harald Weltee416e2e2017-05-26 13:11:59 +0200700#define IPA_STRING_MAX 64
701
702static struct msgb *
Max71393522018-01-08 15:42:00 +0100703ipa_bts_id_resp(const struct ipaccess_unit *dev, uint8_t *data, int len, int trx_nr)
Harald Weltee416e2e2017-05-26 13:11:59 +0200704{
705 struct msgb *nmsg;
706 char str[IPA_STRING_MAX];
707 uint8_t *tag;
708
709 memset(str, 0, sizeof(str));
710
711 nmsg = ipa_msg_alloc(0);
712 if (!nmsg)
713 return NULL;
714
715 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
716 while (len) {
717 if (len < 2) {
718 LOGP(DLINP, LOGL_NOTICE,
719 "Short read of ipaccess tag\n");
720 msgb_free(nmsg);
721 return NULL;
722 }
723 switch (data[1]) {
724 case IPAC_IDTAG_UNIT:
725 snprintf(str, sizeof(str), "%u/%u/%u",
726 dev->site_id, dev->bts_id, trx_nr);
727 break;
728 case IPAC_IDTAG_MACADDR:
729 snprintf(str, sizeof(str),
730 "%02x:%02x:%02x:%02x:%02x:%02x",
731 dev->mac_addr[0], dev->mac_addr[1],
732 dev->mac_addr[2], dev->mac_addr[3],
733 dev->mac_addr[4], dev->mac_addr[5]);
734 break;
735 case IPAC_IDTAG_LOCATION1:
736 if (dev->location1)
Stefan Sperling961776a2018-12-06 13:09:48 +0100737 osmo_strlcpy(str, dev->location1, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200738 break;
739 case IPAC_IDTAG_LOCATION2:
740 if (dev->location2)
Stefan Sperling961776a2018-12-06 13:09:48 +0100741 osmo_strlcpy(str, dev->location2, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200742 break;
743 case IPAC_IDTAG_EQUIPVERS:
744 if (dev->equipvers)
Stefan Sperling961776a2018-12-06 13:09:48 +0100745 osmo_strlcpy(str, dev->equipvers, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200746 break;
747 case IPAC_IDTAG_SWVERSION:
748 if (dev->swversion)
Neels Hofmeyr4c57eef2018-07-26 17:28:24 +0200749 osmo_strlcpy(str, dev->swversion, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200750 break;
751 case IPAC_IDTAG_UNITNAME:
752 snprintf(str, sizeof(str),
753 "%s-%02x-%02x-%02x-%02x-%02x-%02x",
754 dev->unit_name,
755 dev->mac_addr[0], dev->mac_addr[1],
756 dev->mac_addr[2], dev->mac_addr[3],
757 dev->mac_addr[4], dev->mac_addr[5]);
758 break;
759 case IPAC_IDTAG_SERNR:
760 if (dev->serno)
Stefan Sperling961776a2018-12-06 13:09:48 +0100761 osmo_strlcpy(str, dev->serno, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200762 break;
763 default:
764 LOGP(DLINP, LOGL_NOTICE,
765 "Unknown ipaccess tag 0x%02x\n", *data);
766 msgb_free(nmsg);
767 return NULL;
768 }
Harald Weltee416e2e2017-05-26 13:11:59 +0200769
770 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
771 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
772 tag[0] = 0x00;
773 tag[1] = 1 + strlen(str) + 1;
774 tag[2] = data[1];
775 memcpy(tag + 3, str, strlen(str) + 1);
776 data += 2;
777 len -= 2;
778 }
779 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
780 return nmsg;
781}
782
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200783static struct msgb *ipa_bts_id_ack(void)
784{
785 struct msgb *nmsg2;
786
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200787 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200788 if (!nmsg2)
789 return NULL;
790
791 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200792 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200793
794 return nmsg2;
795}
796
Harald Welte51de9ca2013-06-30 20:13:16 +0200797static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up)
798{
799 struct e1inp_line *line = link->line;
800
Eric Wildef1f3272019-07-10 18:10:31 +0200801 if (up) {
802 struct osmo_fsm_inst *ka_fsm = ipaccess_line_ts(link->ofd, line)->driver.ipaccess.ka_fsm;
803
804 update_fd_settings(line, link->ofd->fd);
805 if (ka_fsm && line->ipa_kap)
806 ipa_keepalive_fsm_start(ka_fsm);
807 return;
808 }
Harald Welte51de9ca2013-06-30 20:13:16 +0200809
810 if (line->ops->sign_link_down)
811 line->ops->sign_link_down(line);
812}
813
Harald Welte783715b2014-08-17 18:24:51 +0200814/* handle incoming message to BTS, check if it is an IPA CCM, and if yes,
815 * handle it accordingly (PING/PONG/ID_REQ/ID_RESP/ID_ACK) */
816int ipaccess_bts_handle_ccm(struct ipa_client_conn *link,
817 struct ipaccess_unit *dev, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200818{
819 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200820 struct msgb *rmsg;
821 int ret = 0;
Eric Wildef1f3272019-07-10 18:10:31 +0200822 /* line might not exist if != bsc||bts */
823 struct e1inp_line *line = link->line;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200824
825 /* special handling for IPA CCM. */
826 if (hh->proto == IPAC_PROTO_IPACCESS) {
827 uint8_t msg_type = *(msg->l2h);
Eric Wildef1f3272019-07-10 18:10:31 +0200828 struct osmo_fsm_inst* ka_fsm = NULL;
829
830 /* peek the pong for our keepalive fsm */
831 if (line && msg_type == IPAC_MSGT_PONG) {
832 ka_fsm = ipaccess_line_ts(link->ofd, line)->driver.ipaccess.ka_fsm;
833 ipa_keepalive_fsm_pong_received(ka_fsm);
834 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200835
836 /* ping, pong and acknowledgment cases. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200837 ret = ipa_ccm_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200838 if (ret < 0)
839 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200840
841 /* this is a request for identification from the BSC. */
842 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200843 uint8_t *data = msgb_l2(msg);
844 int len = msgb_l2len(msg);
Andreas Eversbergf422a752014-01-21 14:54:41 +0100845 int trx_nr = 0;
846
847 if (link->ofd->priv_nr >= E1INP_SIGN_RSL)
848 trx_nr = link->ofd->priv_nr - E1INP_SIGN_RSL;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200849
Vadim Yanitskiy69ae2382019-12-02 02:42:43 +0700850 LOGP(DLINP, LOGL_NOTICE, "received ID_GET for unit ID %u/%u/%u\n",
Pau Espin Pedroldd95eb62018-10-02 20:05:28 +0200851 dev->site_id, dev->bts_id, trx_nr);
Harald Weltee416e2e2017-05-26 13:11:59 +0200852 rmsg = ipa_bts_id_resp(dev, data + 1, len - 1, trx_nr);
Harald Welteb65f58f2014-08-20 22:04:11 +0200853 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200854 if (ret != rmsg->len) {
855 LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP "
856 "message. Reason: %s\n", strerror(errno));
857 goto err_rmsg;
858 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200859 msgb_free(rmsg);
860
861 /* send ID_ACK. */
862 rmsg = ipa_bts_id_ack();
Harald Welteb65f58f2014-08-20 22:04:11 +0200863 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200864 if (ret != rmsg->len) {
865 LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK "
866 "message. Reason: %s\n", strerror(errno));
867 goto err_rmsg;
868 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200869 msgb_free(rmsg);
Harald Welte783715b2014-08-17 18:24:51 +0200870 }
871 return 1;
872 }
Harald Welte6eddd472013-06-30 20:18:53 +0200873
Harald Welte783715b2014-08-17 18:24:51 +0200874 return 0;
875
876err_rmsg:
877 msgb_free(rmsg);
878err:
879 ipa_client_conn_close(link);
880 return -1;
881}
882
883static int ipaccess_bts_read_cb(struct ipa_client_conn *link, struct msgb *msg)
884{
885 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
886 struct e1inp_ts *e1i_ts = NULL;
887 struct e1inp_sign_link *sign_link;
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200888 uint8_t msg_type = *(msg->l2h);
Harald Welte783715b2014-08-17 18:24:51 +0200889 int ret = 0;
890
891 /* special handling for IPA CCM. */
892 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200893 /* this is a request for identification from the BSC. */
894 if (msg_type == IPAC_MSGT_ID_GET) {
895 if (!link->line->ops->sign_link_up) {
896 LOGP(DLINP, LOGL_ERROR,
897 "Unable to set signal link, "
898 "closing socket.\n");
Harald Welte783715b2014-08-17 18:24:51 +0200899 goto err;
900 }
901 }
902 }
903
904 /* core CCM handling */
905 ret = ipaccess_bts_handle_ccm(link, link->line->ops->cfg.ipa.dev, msg);
906 if (ret < 0)
907 goto err;
908
909 if (ret == 1 && hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200910 if (msg_type == IPAC_MSGT_ID_GET) {
Harald Welte6eddd472013-06-30 20:18:53 +0200911 sign_link = link->line->ops->sign_link_up(msg,
912 link->line,
913 link->ofd->priv_nr);
914 if (sign_link == NULL) {
915 LOGP(DLINP, LOGL_ERROR,
916 "Unable to set signal link, "
917 "closing socket.\n");
Harald Welte6eddd472013-06-30 20:18:53 +0200918 goto err;
919 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200920 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200921 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200922 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200923 } else if (link->port == IPA_TCP_PORT_OML)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200924 e1i_ts = e1inp_line_ipa_oml_ts(link->line);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200925 else if (link->port == IPA_TCP_PORT_RSL)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200926 e1i_ts = e1inp_line_ipa_rsl_ts(link->line, link->ofd->priv_nr - E1INP_SIGN_RSL);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200927
Pablo Neira Ayusob9487012013-07-05 14:38:43 +0200928 OSMO_ASSERT(e1i_ts != NULL);
929
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200930 if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
931 LOGP(DLINP, LOGL_ERROR, "Signalling link not initialized. Discarding."
932 " port=%u msg_type=%u\n", link->port, msg_type);
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200933 goto err;
934 }
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +0200935
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200936 /* look up for some existing signaling link. */
937 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
938 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200939 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200940 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200941 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200942 }
943 msg->dst = sign_link;
944
945 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200946 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200947 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200948 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200949 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200950 }
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200951 return link->line->ops->sign_link(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200952
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200953err:
Harald Welte783715b2014-08-17 18:24:51 +0200954 ipa_client_conn_close(link);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200955 msgb_free(msg);
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +0200956 return -EBADF;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200957}
958
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200959struct ipaccess_line {
Oliver Smith65ae42c2020-01-22 11:24:52 +0100960 bool line_already_initialized;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200961};
962
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200963static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200964{
965 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200966 struct ipaccess_line *il;
967
968 if (!line->driver_data)
969 line->driver_data = talloc_zero(line, struct ipaccess_line);
970
971 if (!line->driver_data) {
972 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
973 return -ENOMEM;
974 }
975 il = line->driver_data;
976
977 /* We only initialize this line once. */
978 if (il->line_already_initialized)
979 return 0;
980
Oliver Smith65ae42c2020-01-22 11:24:52 +0100981 il->line_already_initialized = true;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200982
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200983 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200984 case E1INP_LINE_R_BSC: {
985 struct ipa_server_link *oml_link, *rsl_link;
Max4c4a1c22016-12-30 15:10:47 +0100986 const char *ipa = e1inp_ipa_get_bind_addr();
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200987
Max4c4a1c22016-12-30 15:10:47 +0100988 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode on %s "
989 "with OML %u and RSL %u TCP ports\n", ipa,
990 IPA_TCP_PORT_OML, IPA_TCP_PORT_RSL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200991
Max4c4a1c22016-12-30 15:10:47 +0100992 oml_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100993 IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200994 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200995 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200996 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200997 "BSC link: %s\n", strerror(errno));
998 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200999 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001000 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001001 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001002 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001003 ipa_server_link_destroy(oml_link);
1004 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +02001005 }
Max4c4a1c22016-12-30 15:10:47 +01001006 rsl_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001007 IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +02001008 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001009 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001010 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001011 "BSC link: %s\n", strerror(errno));
1012 return -ENOMEM;
1013 }
1014 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001015 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001016 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001017 ipa_server_link_destroy(rsl_link);
1018 return -EIO;
1019 }
1020 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001021 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001022 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001023 case E1INP_LINE_R_BTS: {
Harald Welte84f67b22013-06-30 13:13:59 +02001024 struct ipa_client_conn *link;
Eric Wildef1f3272019-07-10 18:10:31 +02001025 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001026
Max4c4a1c22016-12-30 15:10:47 +01001027 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode, "
1028 "OML connecting to %s:%u\n", line->ops->cfg.ipa.addr,
1029 IPA_TCP_PORT_OML);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001030
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001031 link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +02001032 e1inp_line_ipa_oml_ts(line),
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +02001033 E1INP_SIGN_OML,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001034 NULL, 0,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001035 line->ops->cfg.ipa.addr,
1036 IPA_TCP_PORT_OML,
Harald Welte51de9ca2013-06-30 20:13:16 +02001037 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001038 ipaccess_bts_read_cb,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +02001039 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001040 line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001041 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001042 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001043 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001044 return -ENOMEM;
1045 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001046 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001047 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001048 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001049 ipa_client_conn_close(link);
1050 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001051 return -EIO;
1052 }
Eric Wildef1f3272019-07-10 18:10:31 +02001053
1054 e1i_ts = e1inp_line_ipa_oml_ts(line);
1055 ipaccess_bts_keepalive_fsm_alloc(e1i_ts, link, "oml_bts_to_bsc");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001056 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001057 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001058 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001059 default:
1060 break;
1061 }
1062 return ret;
1063}
1064
Andreas Eversbergf422a752014-01-21 14:54:41 +01001065
1066/* backwards compatibility */
Harald Welte84f67b22013-06-30 13:13:59 +02001067int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line,
1068 const char *rem_addr, uint16_t rem_port)
1069{
Andreas Eversbergf422a752014-01-21 14:54:41 +01001070 return e1inp_ipa_bts_rsl_connect_n(line, rem_addr, rem_port, 0);
1071}
1072
1073int e1inp_ipa_bts_rsl_connect_n(struct e1inp_line *line,
1074 const char *rem_addr, uint16_t rem_port,
1075 uint8_t trx_nr)
1076{
Harald Welte84f67b22013-06-30 13:13:59 +02001077 struct ipa_client_conn *rsl_link;
Eric Wildef1f3272019-07-10 18:10:31 +02001078 struct e1inp_ts *e1i_ts = e1inp_line_ipa_rsl_ts(line, trx_nr);
Harald Welte84f67b22013-06-30 13:13:59 +02001079
Andreas Eversbergf422a752014-01-21 14:54:41 +01001080 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
1081 LOGP(DLINP, LOGL_ERROR, "cannot create RSL BTS link: "
1082 "trx_nr (%d) out of range\n", trx_nr);
1083 return -EINVAL;
1084 }
Andreas Eversbergf422a752014-01-21 14:54:41 +01001085
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001086 rsl_link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +02001087 e1inp_line_ipa_rsl_ts(line, trx_nr),
Andreas Eversbergf422a752014-01-21 14:54:41 +01001088 E1INP_SIGN_RSL+trx_nr,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001089 NULL, 0,
Harald Welte84f67b22013-06-30 13:13:59 +02001090 rem_addr, rem_port,
Harald Welte51de9ca2013-06-30 20:13:16 +02001091 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001092 ipaccess_bts_read_cb,
Harald Welte84f67b22013-06-30 13:13:59 +02001093 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001094 line);
Harald Welte84f67b22013-06-30 13:13:59 +02001095 if (rsl_link == NULL) {
1096 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
1097 "BTS link: %s\n", strerror(errno));
1098 return -ENOMEM;
1099 }
1100 if (ipa_client_conn_open(rsl_link) < 0) {
1101 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
1102 strerror(errno));
1103 ipa_client_conn_close(rsl_link);
1104 ipa_client_conn_destroy(rsl_link);
1105 return -EIO;
1106 }
Eric Wildef1f3272019-07-10 18:10:31 +02001107
1108 ipaccess_bts_keepalive_fsm_alloc(e1i_ts, rsl_link, "rsl_bts_to_bsc");
Harald Welte84f67b22013-06-30 13:13:59 +02001109 return 0;
1110}
1111
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001112void e1inp_ipaccess_init(void)
1113{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +02001114 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001115 e1inp_driver_register(&ipaccess_driver);
1116}
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001117
1118void e1inp_ipa_set_bind_addr(const char *ip_bind_addr)
1119{
1120 talloc_free((char*)ipaccess_driver.bind_addr);
1121 ipaccess_driver.bind_addr = NULL;
1122
1123 if (ip_bind_addr)
1124 ipaccess_driver.bind_addr = talloc_strdup(tall_ipa_ctx,
1125 ip_bind_addr);
1126}
1127
1128const char *e1inp_ipa_get_bind_addr(void)
1129{
1130 return ipaccess_driver.bind_addr?
1131 ipaccess_driver.bind_addr
1132 : "0.0.0.0";
1133}