blob: 18cf7f5575dd3adef39209330adff094bab5c00a [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);
Pau Espin Pedroldeb5c4f2020-07-31 12:55:01 +020085 e1inp_line_get2(line, __func__);
Eric Wildef1f3272019-07-10 18:10:31 +020086
87 ipaccess_keepalive_fsm_cleanup(e1i_ts);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020088
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020089 /* Error case: we did not see any ID_RESP yet for this socket. */
90 if (bfd->fd != -1) {
Harald Weltecc2241b2011-07-19 16:06:06 +020091 LOGP(DLINP, LOGL_ERROR, "Forcing socket shutdown with "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020092 "no signal link set\n");
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +020093 osmo_fd_unregister(bfd);
94 close(bfd->fd);
95 bfd->fd = -1;
Pau Espin Pedrolb8ea0ff2020-07-14 13:28:27 +020096 /* This is BSC code, ipaccess_drop() is only called for
97 accepted() sockets, hence the bfd holds a reference to
98 e1inp_line in ->data that needs to be released */
99 OSMO_ASSERT(bfd->data == line);
100 bfd->data = NULL;
101 e1inp_line_put2(line, "ipa_bfd");
102
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200103 ret = -ENOENT;
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +0200104 }
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200105
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200106 msgb_free(e1i_ts->pending_msg);
107 e1i_ts->pending_msg = NULL;
108
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200109 /* e1inp_sign_link_destroy releases the socket descriptors for us. */
110 line->ops->sign_link_down(line);
111
Pau Espin Pedroldeb5c4f2020-07-31 12:55:01 +0200112 e1inp_line_put2(line, __func__);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200113 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200114}
115
Eric Wildef1f3272019-07-10 18:10:31 +0200116static void ipa_bsc_keepalive_write_server_cb(struct osmo_fsm_inst *fi, void *conn, struct msgb *msg)
117{
118 struct osmo_fd *bfd = (struct osmo_fd *)conn;
119 write(bfd->fd, msg->data, msg->len);
120 msgb_free(msg);
121}
122
123static int ipa_bsc_keepalive_timeout_cb(struct osmo_fsm_inst *fi, void *data)
124{
125 struct osmo_fd *bfd = (struct osmo_fd *)data;
126
127 if (bfd->fd == -1)
128 return 1;
129
130 ipaccess_drop(bfd, (struct e1inp_line *)bfd->data);
131 return 1;
132}
133
134static void ipaccess_bsc_keepalive_fsm_alloc(struct e1inp_ts *e1i_ts, struct osmo_fd *bfd, const char *id)
135{
136 struct e1inp_line *line = e1i_ts->line;
137 struct osmo_fsm_inst *ka_fsm;
138
139 ipaccess_keepalive_fsm_cleanup(e1i_ts);
140 if (!line->ipa_kap)
141 return;
142
143 ka_fsm = ipa_generic_conn_alloc_keepalive_fsm(tall_ipa_ctx, bfd, line->ipa_kap, id);
144 e1i_ts->driver.ipaccess.ka_fsm = ka_fsm;
145 if (!ka_fsm)
146 return;
147
148 ipa_keepalive_fsm_set_timeout_cb(ka_fsm, ipa_bsc_keepalive_timeout_cb);
149 ipa_keepalive_fsm_set_send_cb(ka_fsm, ipa_bsc_keepalive_write_server_cb);
150 ipa_keepalive_fsm_start(ka_fsm);
151}
152
153static void ipa_bts_keepalive_write_client_cb(struct osmo_fsm_inst *fi, void *conn, struct msgb *msg) {
154 struct ipa_client_conn *link = (struct ipa_client_conn *)conn;
155 int ret = 0;
156
157 ret = ipa_send(link->ofd->fd, msg->data, msg->len);
158 if (ret != msg->len) {
159 LOGP(DLINP, LOGL_ERROR, "cannot send message. Reason: %s\n", strerror(errno));
160 }
161 msgb_free(msg);
162}
163
164static void update_fd_settings(struct e1inp_line *line, int fd);
165static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up);
166
167static int ipa_bts_keepalive_timeout_cb(struct osmo_fsm_inst *fi, void *conn) {
168 ipaccess_bts_updown_cb(conn, false);
169 return 1;
170}
171
172static void ipaccess_bts_keepalive_fsm_alloc(struct e1inp_ts *e1i_ts, struct ipa_client_conn *client, const char *id)
173{
174 struct e1inp_line *line = e1i_ts->line;
175 struct osmo_fsm_inst *ka_fsm;
176
177 ipaccess_keepalive_fsm_cleanup(e1i_ts);
178 if (!line->ipa_kap)
179 return;
180
181 ka_fsm = ipa_client_conn_alloc_keepalive_fsm(client, line->ipa_kap, id);
182 e1i_ts->driver.ipaccess.ka_fsm = ka_fsm;
183 if (!ka_fsm)
184 return;
185
186 ipa_keepalive_fsm_set_timeout_cb(ka_fsm, ipa_bts_keepalive_timeout_cb);
187 ipa_keepalive_fsm_set_send_cb(ka_fsm, ipa_bts_keepalive_write_client_cb);
188}
189
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200190/* Returns -1 on error, and 0 or 1 on success. If -1 or 1 is returned, line has
191 * been released and should not be used anymore by the caller. */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200192static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
193 struct osmo_fd *bfd)
194{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200195 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200196 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200197 struct ipaccess_unit unit_data = {};
198 struct e1inp_sign_link *sign_link;
199 char *unitid;
200 int len, ret;
Eric Wildef1f3272019-07-10 18:10:31 +0200201 struct e1inp_ts *e1i_ts;
202 struct osmo_fsm_inst *ka_fsm;
203
204 /* peek the pong for our keepalive fsm */
205 e1i_ts = ipaccess_line_ts(bfd, line);
206 ka_fsm = e1i_ts->driver.ipaccess.ka_fsm;
207 if (ka_fsm && msg_type == IPAC_MSGT_PONG)
208 ipa_keepalive_fsm_pong_received(ka_fsm);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200209
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200210 /* Handle IPA PING, PONG and ID_ACK messages. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200211 ret = ipa_ccm_rcvmsg_base(msg, bfd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200212 switch(ret) {
213 case -1:
214 /* error in IPA control message handling */
215 goto err;
216 case 1:
217 /* this is an IPA control message, skip further processing */
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200218 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200219 case 0:
220 /* this is not an IPA control message, continue */
221 break;
222 default:
223 LOGP(DLINP, LOGL_ERROR, "Unexpected return from "
Harald Welteb65f58f2014-08-20 22:04:11 +0200224 "ipa_ccm_rcvmsg_base "
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200225 "(ret=%d)\n", ret);
226 goto err;
227 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200228
229 switch (msg_type) {
230 case IPAC_MSGT_ID_RESP:
Pau Espin Pedrol30e80d52020-07-14 13:23:05 +0200231 DEBUGP(DLMI, "ID_RESP ");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200232 /* parse tags, search for Unit ID */
Harald Welte82eb55e2018-08-01 13:22:55 +0200233 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 +0200234 DEBUGPC(DLMI, "\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200235 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200236 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200237 "with malformed TLVs\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200238 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200239 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200240 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200241 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200242 "without unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200243 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200244
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200245 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200246 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200247 if (len < 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200248 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200249 "with too small unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200250 goto err;
251 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200252 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
253 unitid[len - 1] = '\0';
Vadim Yanitskiy1c94f6a2019-12-02 02:16:07 +0700254 ret = ipa_parse_unitid(unitid, &unit_data);
255 if (ret) {
256 LOGP(DLINP, LOGL_ERROR, "Failed to parse unit ID '%s'\n", unitid);
257 goto err;
258 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200259
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200260 if (!line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200261 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200262 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200263 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200264 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200265 /* the BSC creates the new sign links at this stage. */
266 if (bfd->priv_nr == E1INP_SIGN_OML) {
267 sign_link =
268 line->ops->sign_link_up(&unit_data, line,
269 E1INP_SIGN_OML);
270 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200271 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200272 "Unable to set signal link, "
273 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200274 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200275 }
Eric Wildef1f3272019-07-10 18:10:31 +0200276
277 ipaccess_bsc_keepalive_fsm_alloc(e1i_ts, bfd, "oml_bsc_to_bts");
278
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200279 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200280 struct e1inp_ts *ts;
Eric Wildef1f3272019-07-10 18:10:31 +0200281 struct osmo_fd *newbfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200282 struct e1inp_line *new_line;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200283
284 sign_link =
285 line->ops->sign_link_up(&unit_data, line,
286 E1INP_SIGN_RSL);
287 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200288 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200289 "Unable to set signal link, "
290 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200291 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200292 }
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200293 /* Finally, we know which OML link is associated with
294 * this RSL link, attach it to this socket. */
295 new_line = sign_link->ts->line;
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200296 /* this is a bugtrap, the BSC should be using the
297 * virtual E1 line used by OML for this RSL link. */
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200298 if (new_line == line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200299 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200300 "Fix your BSC, you should use the "
301 "E1 line used by the OML link for "
302 "your RSL link.\n");
303 return 0;
304 }
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200305 e1inp_line_get2(new_line, "ipa_bfd");
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200306 ts = e1inp_line_ipa_rsl_ts(new_line, unit_data.trx_id);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200307 newbfd = &ts->driver.ipaccess.fd;
Pau Espin Pedrol815117c2020-07-14 21:19:16 +0200308 OSMO_ASSERT(newbfd != bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200309
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200310 /* preserve 'newbfd->when' flags potentially set by sign_link_up() */
Pau Espin Pedrol67902bb2018-08-23 14:35:08 +0200311 osmo_fd_setup(newbfd, bfd->fd, newbfd->when | bfd->when, bfd->cb,
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200312 new_line, E1INP_SIGN_RSL + unit_data.trx_id);
313
314
315 /* now we can release the dummy RSL line (old temporary bfd). */
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200316 osmo_fd_unregister(bfd);
317 bfd->fd = -1;
Pau Espin Pedrol8a4e3d92020-07-14 21:20:55 +0200318 /* bfd->data holds a reference to line, drop it */
319 OSMO_ASSERT(bfd->data == line);
320 bfd->data = NULL;
321 e1inp_line_put2(line, "ipa_bfd");
322
Harald Welteae3a9932016-11-26 09:25:23 +0100323 ret = osmo_fd_register(newbfd);
324 if (ret < 0) {
325 LOGP(DLINP, LOGL_ERROR,
326 "could not register FD\n");
327 goto err;
328 }
Eric Wildef1f3272019-07-10 18:10:31 +0200329
330 e1i_ts = ipaccess_line_ts(newbfd, new_line);
331 ipaccess_bsc_keepalive_fsm_alloc(e1i_ts, newbfd, "rsl_bsc_to_bts");
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200332 return 1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200333 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200334 break;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200335 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200336 LOGP(DLINP, LOGL_ERROR, "Unknown IPA message type\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200337 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200338 }
339 return 0;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200340err:
Harald Welteb4a7db02019-07-21 11:51:56 +0200341 if (bfd->fd != -1) {
Pau Espin Pedroldc55a5f2020-07-14 21:14:34 +0200342 osmo_fd_unregister(bfd);
Harald Welteb4a7db02019-07-21 11:51:56 +0200343 close(bfd->fd);
344 bfd->fd = -1;
Pau Espin Pedroldc55a5f2020-07-14 21:14:34 +0200345 /* This is a BSC accepted socket, bfd->data holds a reference to line, drop it */
346 OSMO_ASSERT(bfd->data == line);
347 bfd->data = NULL;
348 e1inp_line_put2(line, "ipa_bfd");
Harald Welteb4a7db02019-07-21 11:51:56 +0200349 }
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200350 return -1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200351}
352
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200353/* Returns -EBADF if bfd cannot be used by the caller anymore after return. */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200354static int handle_ts1_read(struct osmo_fd *bfd)
355{
356 struct e1inp_line *line = bfd->data;
357 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200358 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200359 struct e1inp_sign_link *link;
360 struct ipaccess_head *hh;
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200361 struct msgb *msg = NULL;
Maxc9fa25e2017-01-09 13:23:15 +0100362 int ret, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200363
Eric Wildef1f3272019-07-10 18:10:31 +0200364 e1i_ts = ipaccess_line_ts(bfd, line);
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200365 ret = ipa_msg_recv_buffered(bfd->fd, &msg, &e1i_ts->pending_msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200366 if (ret < 0) {
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200367 if (ret == -EAGAIN)
368 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200369 LOGP(DLINP, LOGL_NOTICE, "Sign link problems, "
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200370 "closing socket. Reason: %s\n", strerror(-ret));
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200371 goto err;
372 } else if (ret == 0) {
373 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
374 goto err;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200375 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200376 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200377
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200378 hh = (struct ipaccess_head *) msg->data;
379 if (hh->proto == IPAC_PROTO_IPACCESS) {
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200380 ret = ipaccess_rcvmsg(line, msg, bfd);
381 /* BIG FAT WARNING: bfd might no longer exist here (ret != 0),
382 * since ipaccess_rcvmsg() might have free'd it !!! */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200383 msgb_free(msg);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200384 return ret != 0 ? -EBADF : 0;
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100385 } else if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
386 /* this sign link is not know yet.. complain. */
387 LOGP(DLINP, LOGL_ERROR, "Timeslot is not configured.\n");
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100388 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200389 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200390
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200391 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
392 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200393 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200394 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200395 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200396 }
397 msg->dst = link;
398
399 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200400 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200401 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200402 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200403 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200404 }
Maxc9fa25e2017-01-09 13:23:15 +0100405 rc = e1i_ts->line->ops->sign_link(msg);
406 if (rc < 0) {
Pablo Neira Ayusoa49c24d2012-10-16 11:24:08 +0200407 /* Don't close the signalling link if the upper layers report
408 * an error, that's too strict. BTW, the signalling layer is
409 * resposible for releasing the message.
410 */
Harald Weltecc2241b2011-07-19 16:06:06 +0200411 LOGP(DLINP, LOGL_ERROR, "Bad signalling message,"
Maxc9fa25e2017-01-09 13:23:15 +0100412 " sign_link returned error: %s.\n", strerror(-rc));
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200413 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200414
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200415 return rc;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200416err_msg:
417 msgb_free(msg);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200418err:
Harald Welte10b41302013-06-30 14:05:49 +0200419 ipaccess_drop(bfd, line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200420 return -EBADF;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200421}
422
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200423static int ts_want_write(struct e1inp_ts *e1i_ts)
424{
Harald Weltede3959e2020-10-18 22:28:50 +0200425 e1i_ts->driver.ipaccess.fd.when |= OSMO_FD_WRITE;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200426
427 return 0;
428}
429
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200430static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200431{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200432 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200433 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Eric Wildef1f3272019-07-10 18:10:31 +0200434 struct e1inp_line *line = e1i_ts->line;
435
436 /* line might not exist if != bsc||bts */
437 if (line) {
438 /* depending on caller the fsm might be dead */
439 struct osmo_fsm_inst* ka_fsm = ipaccess_line_ts(bfd, line)->driver.ipaccess.ka_fsm;
440 if (ka_fsm)
441 ipa_keepalive_fsm_stop(ka_fsm);
442
443 }
444
Pau Espin Pedrol8737ad42020-07-14 21:11:56 +0200445 e1inp_int_snd_event(e1i_ts, sign_link, S_L_INP_TEI_DN);
446 /* the first e1inp_sign_link_destroy call closes the socket. */
447 if (bfd->fd != -1) {
448 osmo_fd_unregister(bfd);
449 close(bfd->fd);
450 bfd->fd = -1;
451 /* If The bfd holds a reference to e1inp_line in ->data (BSC
452 * accepted() sockets), then release it */
453 if (bfd->data == line) {
454 bfd->data = NULL;
455 e1inp_line_put2(line, "ipa_bfd");
456 }
457 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200458}
459
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200460static void timeout_ts1_write(void *data)
461{
462 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
463
464 /* trigger write of ts1, due to tx delay timer */
465 ts_want_write(e1i_ts);
466}
467
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200468static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200469{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200470 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200471 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200472 struct e1inp_sign_link *sign_link;
473 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200474 int ret;
475
Eric Wildef1f3272019-07-10 18:10:31 +0200476 e1i_ts = ipaccess_line_ts(bfd, line);
Harald Weltede3959e2020-10-18 22:28:50 +0200477 bfd->when &= ~OSMO_FD_WRITE;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200478
479 /* get the next msg for this timeslot */
480 msg = e1inp_tx_ts(e1i_ts, &sign_link);
481 if (!msg) {
482 /* no message after tx delay timer */
483 return 0;
484 }
485
486 switch (sign_link->type) {
487 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200488 case E1INP_SIGN_RSL:
Harald Welte46fc7e22014-08-18 19:04:26 +0200489 case E1INP_SIGN_OSMO:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200490 break;
491 default:
Harald Weltede3959e2020-10-18 22:28:50 +0200492 bfd->when |= OSMO_FD_WRITE; /* come back for more msg */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200493 ret = -EINVAL;
494 goto out;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200495 }
496
497 msg->l2h = msg->data;
Harald Welteb65f58f2014-08-20 22:04:11 +0200498 ipa_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200499
Harald Weltecc2241b2011-07-19 16:06:06 +0200500 DEBUGP(DLMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200501
502 ret = send(bfd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200503 if (ret != msg->len) {
504 LOGP(DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling "
505 "message. Reason: %s\n", strerror(errno));
506 goto err;
507 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200508
509 /* set tx delay timer for next event */
Pablo Neira Ayusob26f2fd2017-06-07 18:32:13 +0200510 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200511
512 /* Reducing this might break the nanoBTS 900 init. */
513 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
514
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200515out:
516 msgb_free(msg);
517 return ret;
518err:
Harald Welte10b41302013-06-30 14:05:49 +0200519 ipaccess_drop(bfd, line);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200520 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200521 return ret;
522}
523
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200524static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200525{
526 struct e1inp_line *line = bfd->data;
527
528 return __handle_ts1_write(bfd, line);
529}
530
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200531static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200532{
533 struct e1inp_line *line = link->line;
534
535 return __handle_ts1_write(link->ofd, line);
536}
537
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200538/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200539int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200540{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200541 int rc = 0;
542
Harald Weltede3959e2020-10-18 22:28:50 +0200543 if (what & OSMO_FD_READ)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200544 rc = handle_ts1_read(bfd);
Harald Weltede3959e2020-10-18 22:28:50 +0200545 if (rc != -EBADF && (what & OSMO_FD_WRITE))
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200546 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200547
548 return rc;
549}
550
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200551static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200552
553struct e1inp_driver ipaccess_driver = {
554 .name = "ipa",
555 .want_write = ts_want_write,
556 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200557 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200558 .default_delay = 0,
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100559 .has_keepalive = 1,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200560};
561
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100562static void update_fd_settings(struct e1inp_line *line, int fd)
563{
564 int ret;
565 int val;
566
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100567 if (line->keepalive_num_probes) {
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100568 /* Enable TCP keepalive to find out if the connection is gone */
569 val = 1;
570 ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
571 if (ret < 0)
572 LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive: %s\n",
573 strerror(errno));
574 else
575 LOGP(DLINP, LOGL_NOTICE, "Keepalive is set: %i\n", ret);
576
577#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
578 /* The following options are not portable! */
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100579 val = line->keepalive_idle_timeout > 0 ?
580 line->keepalive_idle_timeout :
581 DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100582 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
583 &val, sizeof(val));
584 if (ret < 0)
585 LOGP(DLINP, LOGL_NOTICE,
586 "Failed to set keepalive idle time: %s\n",
587 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100588 val = line->keepalive_probe_interval > -1 ?
589 line->keepalive_probe_interval :
590 DEFAULT_TCP_KEEPALIVE_INTERVAL;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100591 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
592 &val, sizeof(val));
593 if (ret < 0)
594 LOGP(DLINP, LOGL_NOTICE,
595 "Failed to set keepalive interval: %s\n",
596 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100597 val = line->keepalive_num_probes > 0 ?
598 line->keepalive_num_probes :
599 DEFAULT_TCP_KEEPALIVE_RETRY_COUNT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100600 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
601 &val, sizeof(val));
602 if (ret < 0)
603 LOGP(DLINP, LOGL_NOTICE,
604 "Failed to set keepalive count: %s\n",
605 strerror(errno));
Eric Wild6eb186c2019-06-21 15:26:25 +0200606#if defined(TCP_USER_TIMEOUT)
607 val = 1000 * line->keepalive_num_probes *
608 line->keepalive_probe_interval +
609 line->keepalive_idle_timeout;
610 ret = setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
611 &val, sizeof(val));
612 if (ret < 0)
613 LOGP(DLINP, LOGL_NOTICE,
614 "Failed to set user timoeut: %s\n",
615 strerror(errno));
616#endif
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100617#endif
Holger Hans Peter Freytherf465a4c2014-02-03 09:34:02 +0100618 }
Oliver Smith62725d02020-04-17 10:57:46 +0200619
620 val = 1;
621 ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
622 if (ret < 0)
623 LOGP(DLINP, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100624}
625
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200626/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200627static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200628{
629 int ret;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100630 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200631 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200632 struct e1inp_ts *e1i_ts;
633 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200634
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200635 /* clone virtual E1 line for this new OML link. */
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200636 line = e1inp_line_clone(tall_ipa_ctx, link->line, "ipa_bfd");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200637 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200638 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200639 return -ENOMEM;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200640 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200641
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200642 /* create virrtual E1 timeslots for signalling */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200643 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200644
645 /* initialize the fds */
646 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
647 line->ts[i].driver.ipaccess.fd.fd = -1;
648
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200649 e1i_ts = e1inp_line_ipa_oml_ts(line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200650
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200651 bfd = &e1i_ts->driver.ipaccess.fd;
Harald Weltede3959e2020-10-18 22:28:50 +0200652 osmo_fd_setup(bfd, fd, OSMO_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_OML);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200653 ret = osmo_fd_register(bfd);
654 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200655 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200656 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200657 }
658
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100659 update_fd_settings(line, bfd->fd);
Daniel Willmann85980722014-01-09 14:30:55 +0100660
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200661 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200662 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200663 if (ret < 0) {
664 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
665 strerror(errno));
666 goto err_socket;
667 }
668 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200669
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200670err_socket:
671 osmo_fd_unregister(bfd);
672err_line:
673 close(bfd->fd);
674 bfd->fd = -1;
Pau Espin Pedrola2ff7332020-07-14 21:12:50 +0200675 bfd->data = NULL;
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200676 e1inp_line_put2(line, "ipa_bfd");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200677 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200678}
679
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200680static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200681{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200682 struct e1inp_line *line;
683 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200684 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200685 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200686
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200687 /* We don't know yet which OML link to associate it with. Thus, we
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200688 * allocate a temporary E1 line until we have received ID. */
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200689 line = e1inp_line_clone(tall_ipa_ctx, link->line, "ipa_bfd");
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200690 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200691 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200692 return -ENOMEM;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200693 }
694 /* initialize the fds */
695 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
696 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200697
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200698 /* we need this to initialize this in case to avoid crashes in case
699 * that the socket is closed before we've seen an ID_RESP. */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200700 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200701
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200702 e1i_ts = e1inp_line_ipa_rsl_ts(line, 0);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200703
704 bfd = &e1i_ts->driver.ipaccess.fd;
Harald Weltede3959e2020-10-18 22:28:50 +0200705 osmo_fd_setup(bfd, fd, OSMO_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_RSL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200706 ret = osmo_fd_register(bfd);
707 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200708 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200709 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200710 }
711 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200712 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200713 if (ret < 0) {
714 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
715 strerror(errno));
716 goto err_socket;
717 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100718 update_fd_settings(line, bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200719 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200720
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200721err_socket:
722 osmo_fd_unregister(bfd);
723err_line:
724 close(bfd->fd);
725 bfd->fd = -1;
Pau Espin Pedrola2ff7332020-07-14 21:12:50 +0200726 bfd->data = NULL;
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200727 e1inp_line_put2(line, "ipa_bfd");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200728 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200729}
730
Harald Weltee416e2e2017-05-26 13:11:59 +0200731#define IPA_STRING_MAX 64
732
733static struct msgb *
Max71393522018-01-08 15:42:00 +0100734ipa_bts_id_resp(const struct ipaccess_unit *dev, uint8_t *data, int len, int trx_nr)
Harald Weltee416e2e2017-05-26 13:11:59 +0200735{
736 struct msgb *nmsg;
737 char str[IPA_STRING_MAX];
738 uint8_t *tag;
739
740 memset(str, 0, sizeof(str));
741
742 nmsg = ipa_msg_alloc(0);
743 if (!nmsg)
744 return NULL;
745
746 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
747 while (len) {
748 if (len < 2) {
749 LOGP(DLINP, LOGL_NOTICE,
750 "Short read of ipaccess tag\n");
751 msgb_free(nmsg);
752 return NULL;
753 }
754 switch (data[1]) {
755 case IPAC_IDTAG_UNIT:
756 snprintf(str, sizeof(str), "%u/%u/%u",
757 dev->site_id, dev->bts_id, trx_nr);
758 break;
759 case IPAC_IDTAG_MACADDR:
760 snprintf(str, sizeof(str),
761 "%02x:%02x:%02x:%02x:%02x:%02x",
762 dev->mac_addr[0], dev->mac_addr[1],
763 dev->mac_addr[2], dev->mac_addr[3],
764 dev->mac_addr[4], dev->mac_addr[5]);
765 break;
766 case IPAC_IDTAG_LOCATION1:
767 if (dev->location1)
Stefan Sperling961776a2018-12-06 13:09:48 +0100768 osmo_strlcpy(str, dev->location1, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200769 break;
770 case IPAC_IDTAG_LOCATION2:
771 if (dev->location2)
Stefan Sperling961776a2018-12-06 13:09:48 +0100772 osmo_strlcpy(str, dev->location2, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200773 break;
774 case IPAC_IDTAG_EQUIPVERS:
775 if (dev->equipvers)
Stefan Sperling961776a2018-12-06 13:09:48 +0100776 osmo_strlcpy(str, dev->equipvers, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200777 break;
778 case IPAC_IDTAG_SWVERSION:
779 if (dev->swversion)
Neels Hofmeyr4c57eef2018-07-26 17:28:24 +0200780 osmo_strlcpy(str, dev->swversion, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200781 break;
782 case IPAC_IDTAG_UNITNAME:
783 snprintf(str, sizeof(str),
784 "%s-%02x-%02x-%02x-%02x-%02x-%02x",
785 dev->unit_name,
786 dev->mac_addr[0], dev->mac_addr[1],
787 dev->mac_addr[2], dev->mac_addr[3],
788 dev->mac_addr[4], dev->mac_addr[5]);
789 break;
790 case IPAC_IDTAG_SERNR:
791 if (dev->serno)
Stefan Sperling961776a2018-12-06 13:09:48 +0100792 osmo_strlcpy(str, dev->serno, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200793 break;
794 default:
795 LOGP(DLINP, LOGL_NOTICE,
796 "Unknown ipaccess tag 0x%02x\n", *data);
797 msgb_free(nmsg);
798 return NULL;
799 }
Harald Weltee416e2e2017-05-26 13:11:59 +0200800
801 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
802 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
803 tag[0] = 0x00;
804 tag[1] = 1 + strlen(str) + 1;
805 tag[2] = data[1];
806 memcpy(tag + 3, str, strlen(str) + 1);
807 data += 2;
808 len -= 2;
809 }
810 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
811 return nmsg;
812}
813
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200814static struct msgb *ipa_bts_id_ack(void)
815{
816 struct msgb *nmsg2;
817
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200818 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200819 if (!nmsg2)
820 return NULL;
821
822 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200823 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200824
825 return nmsg2;
826}
827
Harald Welte51de9ca2013-06-30 20:13:16 +0200828static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up)
829{
830 struct e1inp_line *line = link->line;
831
Eric Wildef1f3272019-07-10 18:10:31 +0200832 if (up) {
833 struct osmo_fsm_inst *ka_fsm = ipaccess_line_ts(link->ofd, line)->driver.ipaccess.ka_fsm;
834
835 update_fd_settings(line, link->ofd->fd);
836 if (ka_fsm && line->ipa_kap)
837 ipa_keepalive_fsm_start(ka_fsm);
838 return;
839 }
Harald Welte51de9ca2013-06-30 20:13:16 +0200840
841 if (line->ops->sign_link_down)
842 line->ops->sign_link_down(line);
843}
844
Harald Welte783715b2014-08-17 18:24:51 +0200845/* handle incoming message to BTS, check if it is an IPA CCM, and if yes,
846 * handle it accordingly (PING/PONG/ID_REQ/ID_RESP/ID_ACK) */
847int ipaccess_bts_handle_ccm(struct ipa_client_conn *link,
848 struct ipaccess_unit *dev, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200849{
850 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200851 struct msgb *rmsg;
852 int ret = 0;
Eric Wildef1f3272019-07-10 18:10:31 +0200853 /* line might not exist if != bsc||bts */
854 struct e1inp_line *line = link->line;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200855
856 /* special handling for IPA CCM. */
857 if (hh->proto == IPAC_PROTO_IPACCESS) {
858 uint8_t msg_type = *(msg->l2h);
Eric Wildef1f3272019-07-10 18:10:31 +0200859 struct osmo_fsm_inst* ka_fsm = NULL;
860
861 /* peek the pong for our keepalive fsm */
862 if (line && msg_type == IPAC_MSGT_PONG) {
863 ka_fsm = ipaccess_line_ts(link->ofd, line)->driver.ipaccess.ka_fsm;
864 ipa_keepalive_fsm_pong_received(ka_fsm);
865 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200866
867 /* ping, pong and acknowledgment cases. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200868 ret = ipa_ccm_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200869 if (ret < 0)
870 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200871
872 /* this is a request for identification from the BSC. */
873 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200874 uint8_t *data = msgb_l2(msg);
875 int len = msgb_l2len(msg);
Andreas Eversbergf422a752014-01-21 14:54:41 +0100876 int trx_nr = 0;
877
878 if (link->ofd->priv_nr >= E1INP_SIGN_RSL)
879 trx_nr = link->ofd->priv_nr - E1INP_SIGN_RSL;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200880
Vadim Yanitskiy69ae2382019-12-02 02:42:43 +0700881 LOGP(DLINP, LOGL_NOTICE, "received ID_GET for unit ID %u/%u/%u\n",
Pau Espin Pedroldd95eb62018-10-02 20:05:28 +0200882 dev->site_id, dev->bts_id, trx_nr);
Harald Weltee416e2e2017-05-26 13:11:59 +0200883 rmsg = ipa_bts_id_resp(dev, data + 1, len - 1, trx_nr);
Harald Welteb65f58f2014-08-20 22:04:11 +0200884 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200885 if (ret != rmsg->len) {
886 LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP "
887 "message. Reason: %s\n", strerror(errno));
888 goto err_rmsg;
889 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200890 msgb_free(rmsg);
891
892 /* send ID_ACK. */
893 rmsg = ipa_bts_id_ack();
Harald Welteb65f58f2014-08-20 22:04:11 +0200894 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200895 if (ret != rmsg->len) {
896 LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK "
897 "message. Reason: %s\n", strerror(errno));
898 goto err_rmsg;
899 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200900 msgb_free(rmsg);
Harald Welte783715b2014-08-17 18:24:51 +0200901 }
902 return 1;
903 }
Harald Welte6eddd472013-06-30 20:18:53 +0200904
Harald Welte783715b2014-08-17 18:24:51 +0200905 return 0;
906
907err_rmsg:
908 msgb_free(rmsg);
909err:
910 ipa_client_conn_close(link);
911 return -1;
912}
913
914static int ipaccess_bts_read_cb(struct ipa_client_conn *link, struct msgb *msg)
915{
916 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
917 struct e1inp_ts *e1i_ts = NULL;
918 struct e1inp_sign_link *sign_link;
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200919 uint8_t msg_type = *(msg->l2h);
Harald Welte783715b2014-08-17 18:24:51 +0200920 int ret = 0;
921
922 /* special handling for IPA CCM. */
923 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200924 /* this is a request for identification from the BSC. */
925 if (msg_type == IPAC_MSGT_ID_GET) {
926 if (!link->line->ops->sign_link_up) {
927 LOGP(DLINP, LOGL_ERROR,
928 "Unable to set signal link, "
929 "closing socket.\n");
Harald Welte783715b2014-08-17 18:24:51 +0200930 goto err;
931 }
932 }
933 }
934
935 /* core CCM handling */
936 ret = ipaccess_bts_handle_ccm(link, link->line->ops->cfg.ipa.dev, msg);
937 if (ret < 0)
938 goto err;
939
940 if (ret == 1 && hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200941 if (msg_type == IPAC_MSGT_ID_GET) {
Harald Welte6eddd472013-06-30 20:18:53 +0200942 sign_link = link->line->ops->sign_link_up(msg,
943 link->line,
944 link->ofd->priv_nr);
945 if (sign_link == NULL) {
946 LOGP(DLINP, LOGL_ERROR,
947 "Unable to set signal link, "
948 "closing socket.\n");
Harald Welte6eddd472013-06-30 20:18:53 +0200949 goto err;
950 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200951 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200952 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200953 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200954 } else if (link->port == IPA_TCP_PORT_OML)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200955 e1i_ts = e1inp_line_ipa_oml_ts(link->line);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200956 else if (link->port == IPA_TCP_PORT_RSL)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200957 e1i_ts = e1inp_line_ipa_rsl_ts(link->line, link->ofd->priv_nr - E1INP_SIGN_RSL);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200958
Pablo Neira Ayusob9487012013-07-05 14:38:43 +0200959 OSMO_ASSERT(e1i_ts != NULL);
960
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200961 if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
962 LOGP(DLINP, LOGL_ERROR, "Signalling link not initialized. Discarding."
963 " port=%u msg_type=%u\n", link->port, msg_type);
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200964 goto err;
965 }
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +0200966
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200967 /* look up for some existing signaling link. */
968 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
969 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200970 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200971 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200972 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200973 }
974 msg->dst = sign_link;
975
976 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200977 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200978 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200979 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200980 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200981 }
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200982 return link->line->ops->sign_link(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200983
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200984err:
Harald Welte783715b2014-08-17 18:24:51 +0200985 ipa_client_conn_close(link);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200986 msgb_free(msg);
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +0200987 return -EBADF;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200988}
989
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200990struct ipaccess_line {
Oliver Smith65ae42c2020-01-22 11:24:52 +0100991 bool line_already_initialized;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200992};
993
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200994static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200995{
996 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200997 struct ipaccess_line *il;
998
999 if (!line->driver_data)
1000 line->driver_data = talloc_zero(line, struct ipaccess_line);
1001
1002 if (!line->driver_data) {
1003 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
1004 return -ENOMEM;
1005 }
1006 il = line->driver_data;
1007
1008 /* We only initialize this line once. */
1009 if (il->line_already_initialized)
1010 return 0;
1011
Oliver Smith65ae42c2020-01-22 11:24:52 +01001012 il->line_already_initialized = true;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001013
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001014 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001015 case E1INP_LINE_R_BSC: {
1016 struct ipa_server_link *oml_link, *rsl_link;
Max4c4a1c22016-12-30 15:10:47 +01001017 const char *ipa = e1inp_ipa_get_bind_addr();
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001018
Max4c4a1c22016-12-30 15:10:47 +01001019 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode on %s "
1020 "with OML %u and RSL %u TCP ports\n", ipa,
1021 IPA_TCP_PORT_OML, IPA_TCP_PORT_RSL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001022
Max4c4a1c22016-12-30 15:10:47 +01001023 oml_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001024 IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +02001025 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001026 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001027 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001028 "BSC link: %s\n", strerror(errno));
1029 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +02001030 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001031 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001032 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001033 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001034 ipa_server_link_destroy(oml_link);
1035 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +02001036 }
Max4c4a1c22016-12-30 15:10:47 +01001037 rsl_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001038 IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +02001039 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001040 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001041 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001042 "BSC link: %s\n", strerror(errno));
1043 return -ENOMEM;
1044 }
1045 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001046 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001047 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001048 ipa_server_link_destroy(rsl_link);
1049 return -EIO;
1050 }
1051 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001052 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001053 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001054 case E1INP_LINE_R_BTS: {
Harald Welte84f67b22013-06-30 13:13:59 +02001055 struct ipa_client_conn *link;
Eric Wildef1f3272019-07-10 18:10:31 +02001056 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001057
Max4c4a1c22016-12-30 15:10:47 +01001058 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode, "
1059 "OML connecting to %s:%u\n", line->ops->cfg.ipa.addr,
1060 IPA_TCP_PORT_OML);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001061
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001062 link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +02001063 e1inp_line_ipa_oml_ts(line),
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +02001064 E1INP_SIGN_OML,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001065 NULL, 0,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001066 line->ops->cfg.ipa.addr,
1067 IPA_TCP_PORT_OML,
Harald Welte51de9ca2013-06-30 20:13:16 +02001068 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001069 ipaccess_bts_read_cb,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +02001070 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001071 line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001072 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001073 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001074 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001075 return -ENOMEM;
1076 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001077 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001078 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001079 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001080 ipa_client_conn_close(link);
1081 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001082 return -EIO;
1083 }
Eric Wildef1f3272019-07-10 18:10:31 +02001084
1085 e1i_ts = e1inp_line_ipa_oml_ts(line);
1086 ipaccess_bts_keepalive_fsm_alloc(e1i_ts, link, "oml_bts_to_bsc");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001087 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001088 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001089 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001090 default:
1091 break;
1092 }
1093 return ret;
1094}
1095
Andreas Eversbergf422a752014-01-21 14:54:41 +01001096
1097/* backwards compatibility */
Harald Welte84f67b22013-06-30 13:13:59 +02001098int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line,
1099 const char *rem_addr, uint16_t rem_port)
1100{
Andreas Eversbergf422a752014-01-21 14:54:41 +01001101 return e1inp_ipa_bts_rsl_connect_n(line, rem_addr, rem_port, 0);
1102}
1103
1104int e1inp_ipa_bts_rsl_connect_n(struct e1inp_line *line,
1105 const char *rem_addr, uint16_t rem_port,
1106 uint8_t trx_nr)
1107{
Harald Welte84f67b22013-06-30 13:13:59 +02001108 struct ipa_client_conn *rsl_link;
Eric Wildef1f3272019-07-10 18:10:31 +02001109 struct e1inp_ts *e1i_ts = e1inp_line_ipa_rsl_ts(line, trx_nr);
Harald Welte84f67b22013-06-30 13:13:59 +02001110
Andreas Eversbergf422a752014-01-21 14:54:41 +01001111 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
1112 LOGP(DLINP, LOGL_ERROR, "cannot create RSL BTS link: "
1113 "trx_nr (%d) out of range\n", trx_nr);
1114 return -EINVAL;
1115 }
Andreas Eversbergf422a752014-01-21 14:54:41 +01001116
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001117 rsl_link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +02001118 e1inp_line_ipa_rsl_ts(line, trx_nr),
Andreas Eversbergf422a752014-01-21 14:54:41 +01001119 E1INP_SIGN_RSL+trx_nr,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001120 NULL, 0,
Harald Welte84f67b22013-06-30 13:13:59 +02001121 rem_addr, rem_port,
Harald Welte51de9ca2013-06-30 20:13:16 +02001122 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001123 ipaccess_bts_read_cb,
Harald Welte84f67b22013-06-30 13:13:59 +02001124 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001125 line);
Harald Welte84f67b22013-06-30 13:13:59 +02001126 if (rsl_link == NULL) {
1127 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
1128 "BTS link: %s\n", strerror(errno));
1129 return -ENOMEM;
1130 }
1131 if (ipa_client_conn_open(rsl_link) < 0) {
1132 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
1133 strerror(errno));
1134 ipa_client_conn_close(rsl_link);
1135 ipa_client_conn_destroy(rsl_link);
1136 return -EIO;
1137 }
Eric Wildef1f3272019-07-10 18:10:31 +02001138
1139 ipaccess_bts_keepalive_fsm_alloc(e1i_ts, rsl_link, "rsl_bts_to_bsc");
Harald Welte84f67b22013-06-30 13:13:59 +02001140 return 0;
1141}
1142
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001143void e1inp_ipaccess_init(void)
1144{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +02001145 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001146 e1inp_driver_register(&ipaccess_driver);
1147}
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001148
1149void e1inp_ipa_set_bind_addr(const char *ip_bind_addr)
1150{
1151 talloc_free((char*)ipaccess_driver.bind_addr);
1152 ipaccess_driver.bind_addr = NULL;
1153
1154 if (ip_bind_addr)
1155 ipaccess_driver.bind_addr = talloc_strdup(tall_ipa_ctx,
1156 ip_bind_addr);
1157}
1158
1159const char *e1inp_ipa_get_bind_addr(void)
1160{
1161 return ipaccess_driver.bind_addr?
1162 ipaccess_driver.bind_addr
1163 : "0.0.0.0";
1164}