blob: 4d55e71fbafd3dd4c36392101ea492f3afd670ef [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;
Pau Espin Pedrolb8ea0ff2020-07-14 13:28:27 +020095 /* This is BSC code, ipaccess_drop() is only called for
96 accepted() sockets, hence the bfd holds a reference to
97 e1inp_line in ->data that needs to be released */
98 OSMO_ASSERT(bfd->data == line);
99 bfd->data = NULL;
100 e1inp_line_put2(line, "ipa_bfd");
101
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200102 ret = -ENOENT;
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +0200103 }
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200104
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200105 msgb_free(e1i_ts->pending_msg);
106 e1i_ts->pending_msg = NULL;
107
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200108 /* e1inp_sign_link_destroy releases the socket descriptors for us. */
109 line->ops->sign_link_down(line);
110
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200111 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200112}
113
Eric Wildef1f3272019-07-10 18:10:31 +0200114static void ipa_bsc_keepalive_write_server_cb(struct osmo_fsm_inst *fi, void *conn, struct msgb *msg)
115{
116 struct osmo_fd *bfd = (struct osmo_fd *)conn;
117 write(bfd->fd, msg->data, msg->len);
118 msgb_free(msg);
119}
120
121static int ipa_bsc_keepalive_timeout_cb(struct osmo_fsm_inst *fi, void *data)
122{
123 struct osmo_fd *bfd = (struct osmo_fd *)data;
124
125 if (bfd->fd == -1)
126 return 1;
127
128 ipaccess_drop(bfd, (struct e1inp_line *)bfd->data);
129 return 1;
130}
131
132static void ipaccess_bsc_keepalive_fsm_alloc(struct e1inp_ts *e1i_ts, struct osmo_fd *bfd, const char *id)
133{
134 struct e1inp_line *line = e1i_ts->line;
135 struct osmo_fsm_inst *ka_fsm;
136
137 ipaccess_keepalive_fsm_cleanup(e1i_ts);
138 if (!line->ipa_kap)
139 return;
140
141 ka_fsm = ipa_generic_conn_alloc_keepalive_fsm(tall_ipa_ctx, bfd, line->ipa_kap, id);
142 e1i_ts->driver.ipaccess.ka_fsm = ka_fsm;
143 if (!ka_fsm)
144 return;
145
146 ipa_keepalive_fsm_set_timeout_cb(ka_fsm, ipa_bsc_keepalive_timeout_cb);
147 ipa_keepalive_fsm_set_send_cb(ka_fsm, ipa_bsc_keepalive_write_server_cb);
148 ipa_keepalive_fsm_start(ka_fsm);
149}
150
151static void ipa_bts_keepalive_write_client_cb(struct osmo_fsm_inst *fi, void *conn, struct msgb *msg) {
152 struct ipa_client_conn *link = (struct ipa_client_conn *)conn;
153 int ret = 0;
154
155 ret = ipa_send(link->ofd->fd, msg->data, msg->len);
156 if (ret != msg->len) {
157 LOGP(DLINP, LOGL_ERROR, "cannot send message. Reason: %s\n", strerror(errno));
158 }
159 msgb_free(msg);
160}
161
162static void update_fd_settings(struct e1inp_line *line, int fd);
163static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up);
164
165static int ipa_bts_keepalive_timeout_cb(struct osmo_fsm_inst *fi, void *conn) {
166 ipaccess_bts_updown_cb(conn, false);
167 return 1;
168}
169
170static void ipaccess_bts_keepalive_fsm_alloc(struct e1inp_ts *e1i_ts, struct ipa_client_conn *client, const char *id)
171{
172 struct e1inp_line *line = e1i_ts->line;
173 struct osmo_fsm_inst *ka_fsm;
174
175 ipaccess_keepalive_fsm_cleanup(e1i_ts);
176 if (!line->ipa_kap)
177 return;
178
179 ka_fsm = ipa_client_conn_alloc_keepalive_fsm(client, line->ipa_kap, id);
180 e1i_ts->driver.ipaccess.ka_fsm = ka_fsm;
181 if (!ka_fsm)
182 return;
183
184 ipa_keepalive_fsm_set_timeout_cb(ka_fsm, ipa_bts_keepalive_timeout_cb);
185 ipa_keepalive_fsm_set_send_cb(ka_fsm, ipa_bts_keepalive_write_client_cb);
186}
187
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200188/* Returns -1 on error, and 0 or 1 on success. If -1 or 1 is returned, line has
189 * been released and should not be used anymore by the caller. */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200190static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
191 struct osmo_fd *bfd)
192{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200193 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200194 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200195 struct ipaccess_unit unit_data = {};
196 struct e1inp_sign_link *sign_link;
197 char *unitid;
198 int len, ret;
Eric Wildef1f3272019-07-10 18:10:31 +0200199 struct e1inp_ts *e1i_ts;
200 struct osmo_fsm_inst *ka_fsm;
201
202 /* peek the pong for our keepalive fsm */
203 e1i_ts = ipaccess_line_ts(bfd, line);
204 ka_fsm = e1i_ts->driver.ipaccess.ka_fsm;
205 if (ka_fsm && msg_type == IPAC_MSGT_PONG)
206 ipa_keepalive_fsm_pong_received(ka_fsm);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200207
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200208 /* Handle IPA PING, PONG and ID_ACK messages. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200209 ret = ipa_ccm_rcvmsg_base(msg, bfd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200210 switch(ret) {
211 case -1:
212 /* error in IPA control message handling */
213 goto err;
214 case 1:
215 /* this is an IPA control message, skip further processing */
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200216 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200217 case 0:
218 /* this is not an IPA control message, continue */
219 break;
220 default:
221 LOGP(DLINP, LOGL_ERROR, "Unexpected return from "
Harald Welteb65f58f2014-08-20 22:04:11 +0200222 "ipa_ccm_rcvmsg_base "
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200223 "(ret=%d)\n", ret);
224 goto err;
225 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200226
227 switch (msg_type) {
228 case IPAC_MSGT_ID_RESP:
Pau Espin Pedrol30e80d52020-07-14 13:23:05 +0200229 DEBUGP(DLMI, "ID_RESP ");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200230 /* parse tags, search for Unit ID */
Harald Welte82eb55e2018-08-01 13:22:55 +0200231 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 +0200232 DEBUGPC(DLMI, "\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200233 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200234 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200235 "with malformed TLVs\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200236 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200237 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200238 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200239 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200240 "without unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200241 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200242
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200243 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200244 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200245 if (len < 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200246 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200247 "with too small unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200248 goto err;
249 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200250 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
251 unitid[len - 1] = '\0';
Vadim Yanitskiy1c94f6a2019-12-02 02:16:07 +0700252 ret = ipa_parse_unitid(unitid, &unit_data);
253 if (ret) {
254 LOGP(DLINP, LOGL_ERROR, "Failed to parse unit ID '%s'\n", unitid);
255 goto err;
256 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200257
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200258 if (!line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200259 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200260 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200261 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200262 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200263 /* the BSC creates the new sign links at this stage. */
264 if (bfd->priv_nr == E1INP_SIGN_OML) {
265 sign_link =
266 line->ops->sign_link_up(&unit_data, line,
267 E1INP_SIGN_OML);
268 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200269 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200270 "Unable to set signal link, "
271 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200272 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200273 }
Eric Wildef1f3272019-07-10 18:10:31 +0200274
275 ipaccess_bsc_keepalive_fsm_alloc(e1i_ts, bfd, "oml_bsc_to_bts");
276
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200277 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200278 struct e1inp_ts *ts;
Eric Wildef1f3272019-07-10 18:10:31 +0200279 struct osmo_fd *newbfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200280 struct e1inp_line *new_line;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200281
282 sign_link =
283 line->ops->sign_link_up(&unit_data, line,
284 E1INP_SIGN_RSL);
285 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200286 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200287 "Unable to set signal link, "
288 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200289 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200290 }
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200291 /* this is a bugtrap, the BSC should be using the
292 * virtual E1 line used by OML for this RSL link. */
293 if (sign_link->ts->line == line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200294 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200295 "Fix your BSC, you should use the "
296 "E1 line used by the OML link for "
297 "your RSL link.\n");
298 return 0;
299 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200300 /* Finally, we know which OML link is associated with
301 * this RSL link, attach it to this socket. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200302 bfd->data = new_line = sign_link->ts->line;
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200303 e1inp_line_get2(new_line, "ipa_bfd");
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200304 ts = e1inp_line_ipa_rsl_ts(new_line, unit_data.trx_id);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200305 newbfd = &ts->driver.ipaccess.fd;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200306
307 /* get rid of our old temporary bfd */
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200308 /* preserve 'newbfd->when' flags potentially set by sign_link_up() */
Pau Espin Pedrol67902bb2018-08-23 14:35:08 +0200309 osmo_fd_setup(newbfd, bfd->fd, newbfd->when | bfd->when, bfd->cb,
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200310 bfd->data, E1INP_SIGN_RSL + unit_data.trx_id);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200311 osmo_fd_unregister(bfd);
312 bfd->fd = -1;
Harald Welteae3a9932016-11-26 09:25:23 +0100313 ret = osmo_fd_register(newbfd);
314 if (ret < 0) {
315 LOGP(DLINP, LOGL_ERROR,
316 "could not register FD\n");
317 goto err;
318 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200319 /* now we can release the dummy RSL line. */
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200320 e1inp_line_put2(line, "ipa_bfd");
Eric Wildef1f3272019-07-10 18:10:31 +0200321
322 e1i_ts = ipaccess_line_ts(newbfd, new_line);
323 ipaccess_bsc_keepalive_fsm_alloc(e1i_ts, newbfd, "rsl_bsc_to_bts");
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200324 return 1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200325 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200326 break;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200327 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200328 LOGP(DLINP, LOGL_ERROR, "Unknown IPA message type\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200329 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200330 }
331 return 0;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200332err:
333 osmo_fd_unregister(bfd);
Harald Welteb4a7db02019-07-21 11:51:56 +0200334 if (bfd->fd != -1) {
335 close(bfd->fd);
336 bfd->fd = -1;
337 }
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200338 e1inp_line_put2(line, "ipa_bfd");
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200339 return -1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200340}
341
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200342/* Returns -EBADF if bfd cannot be used by the caller anymore after return. */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200343static int handle_ts1_read(struct osmo_fd *bfd)
344{
345 struct e1inp_line *line = bfd->data;
346 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200347 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200348 struct e1inp_sign_link *link;
349 struct ipaccess_head *hh;
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200350 struct msgb *msg = NULL;
Maxc9fa25e2017-01-09 13:23:15 +0100351 int ret, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200352
Eric Wildef1f3272019-07-10 18:10:31 +0200353 e1i_ts = ipaccess_line_ts(bfd, line);
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200354 ret = ipa_msg_recv_buffered(bfd->fd, &msg, &e1i_ts->pending_msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200355 if (ret < 0) {
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200356 if (ret == -EAGAIN)
357 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200358 LOGP(DLINP, LOGL_NOTICE, "Sign link problems, "
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200359 "closing socket. Reason: %s\n", strerror(-ret));
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200360 goto err;
361 } else if (ret == 0) {
362 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
363 goto err;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200364 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200365 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200366
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200367 hh = (struct ipaccess_head *) msg->data;
368 if (hh->proto == IPAC_PROTO_IPACCESS) {
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200369 ret = ipaccess_rcvmsg(line, msg, bfd);
370 /* BIG FAT WARNING: bfd might no longer exist here (ret != 0),
371 * since ipaccess_rcvmsg() might have free'd it !!! */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200372 msgb_free(msg);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200373 return ret != 0 ? -EBADF : 0;
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100374 } else if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
375 /* this sign link is not know yet.. complain. */
376 LOGP(DLINP, LOGL_ERROR, "Timeslot is not configured.\n");
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100377 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200378 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200379
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200380 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
381 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200382 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200383 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200384 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200385 }
386 msg->dst = link;
387
388 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200389 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200390 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200391 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200392 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200393 }
Maxc9fa25e2017-01-09 13:23:15 +0100394 rc = e1i_ts->line->ops->sign_link(msg);
395 if (rc < 0) {
Pablo Neira Ayusoa49c24d2012-10-16 11:24:08 +0200396 /* Don't close the signalling link if the upper layers report
397 * an error, that's too strict. BTW, the signalling layer is
398 * resposible for releasing the message.
399 */
Harald Weltecc2241b2011-07-19 16:06:06 +0200400 LOGP(DLINP, LOGL_ERROR, "Bad signalling message,"
Maxc9fa25e2017-01-09 13:23:15 +0100401 " sign_link returned error: %s.\n", strerror(-rc));
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200402 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200403
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200404 return rc;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200405err_msg:
406 msgb_free(msg);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200407err:
Harald Welte10b41302013-06-30 14:05:49 +0200408 ipaccess_drop(bfd, line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200409 return -EBADF;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200410}
411
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200412static int ts_want_write(struct e1inp_ts *e1i_ts)
413{
414 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
415
416 return 0;
417}
418
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200419static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200420{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200421 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200422 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Eric Wildef1f3272019-07-10 18:10:31 +0200423 struct e1inp_line *line = e1i_ts->line;
424
425 /* line might not exist if != bsc||bts */
426 if (line) {
427 /* depending on caller the fsm might be dead */
428 struct osmo_fsm_inst* ka_fsm = ipaccess_line_ts(bfd, line)->driver.ipaccess.ka_fsm;
429 if (ka_fsm)
430 ipa_keepalive_fsm_stop(ka_fsm);
431
432 }
433
Pau Espin Pedrol8737ad42020-07-14 21:11:56 +0200434 e1inp_int_snd_event(e1i_ts, sign_link, S_L_INP_TEI_DN);
435 /* the first e1inp_sign_link_destroy call closes the socket. */
436 if (bfd->fd != -1) {
437 osmo_fd_unregister(bfd);
438 close(bfd->fd);
439 bfd->fd = -1;
440 /* If The bfd holds a reference to e1inp_line in ->data (BSC
441 * accepted() sockets), then release it */
442 if (bfd->data == line) {
443 bfd->data = NULL;
444 e1inp_line_put2(line, "ipa_bfd");
445 }
446 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200447}
448
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200449static void timeout_ts1_write(void *data)
450{
451 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
452
453 /* trigger write of ts1, due to tx delay timer */
454 ts_want_write(e1i_ts);
455}
456
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200457static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200458{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200459 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200460 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200461 struct e1inp_sign_link *sign_link;
462 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200463 int ret;
464
Eric Wildef1f3272019-07-10 18:10:31 +0200465 e1i_ts = ipaccess_line_ts(bfd, line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200466 bfd->when &= ~BSC_FD_WRITE;
467
468 /* get the next msg for this timeslot */
469 msg = e1inp_tx_ts(e1i_ts, &sign_link);
470 if (!msg) {
471 /* no message after tx delay timer */
472 return 0;
473 }
474
475 switch (sign_link->type) {
476 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200477 case E1INP_SIGN_RSL:
Harald Welte46fc7e22014-08-18 19:04:26 +0200478 case E1INP_SIGN_OSMO:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200479 break;
480 default:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200481 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200482 ret = -EINVAL;
483 goto out;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200484 }
485
486 msg->l2h = msg->data;
Harald Welteb65f58f2014-08-20 22:04:11 +0200487 ipa_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200488
Harald Weltecc2241b2011-07-19 16:06:06 +0200489 DEBUGP(DLMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200490
491 ret = send(bfd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200492 if (ret != msg->len) {
493 LOGP(DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling "
494 "message. Reason: %s\n", strerror(errno));
495 goto err;
496 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200497
498 /* set tx delay timer for next event */
Pablo Neira Ayusob26f2fd2017-06-07 18:32:13 +0200499 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200500
501 /* Reducing this might break the nanoBTS 900 init. */
502 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
503
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200504out:
505 msgb_free(msg);
506 return ret;
507err:
Harald Welte10b41302013-06-30 14:05:49 +0200508 ipaccess_drop(bfd, line);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200509 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200510 return ret;
511}
512
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200513static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200514{
515 struct e1inp_line *line = bfd->data;
516
517 return __handle_ts1_write(bfd, line);
518}
519
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200520static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200521{
522 struct e1inp_line *line = link->line;
523
524 return __handle_ts1_write(link->ofd, line);
525}
526
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200527/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200528int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200529{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200530 int rc = 0;
531
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200532 if (what & BSC_FD_READ)
533 rc = handle_ts1_read(bfd);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200534 if (rc != -EBADF && (what & BSC_FD_WRITE))
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200535 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200536
537 return rc;
538}
539
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200540static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200541
542struct e1inp_driver ipaccess_driver = {
543 .name = "ipa",
544 .want_write = ts_want_write,
545 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200546 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200547 .default_delay = 0,
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100548 .has_keepalive = 1,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200549};
550
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100551static void update_fd_settings(struct e1inp_line *line, int fd)
552{
553 int ret;
554 int val;
555
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100556 if (line->keepalive_num_probes) {
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100557 /* Enable TCP keepalive to find out if the connection is gone */
558 val = 1;
559 ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
560 if (ret < 0)
561 LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive: %s\n",
562 strerror(errno));
563 else
564 LOGP(DLINP, LOGL_NOTICE, "Keepalive is set: %i\n", ret);
565
566#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
567 /* The following options are not portable! */
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100568 val = line->keepalive_idle_timeout > 0 ?
569 line->keepalive_idle_timeout :
570 DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100571 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
572 &val, sizeof(val));
573 if (ret < 0)
574 LOGP(DLINP, LOGL_NOTICE,
575 "Failed to set keepalive idle time: %s\n",
576 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100577 val = line->keepalive_probe_interval > -1 ?
578 line->keepalive_probe_interval :
579 DEFAULT_TCP_KEEPALIVE_INTERVAL;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100580 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
581 &val, sizeof(val));
582 if (ret < 0)
583 LOGP(DLINP, LOGL_NOTICE,
584 "Failed to set keepalive interval: %s\n",
585 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100586 val = line->keepalive_num_probes > 0 ?
587 line->keepalive_num_probes :
588 DEFAULT_TCP_KEEPALIVE_RETRY_COUNT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100589 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
590 &val, sizeof(val));
591 if (ret < 0)
592 LOGP(DLINP, LOGL_NOTICE,
593 "Failed to set keepalive count: %s\n",
594 strerror(errno));
Eric Wild6eb186c2019-06-21 15:26:25 +0200595#if defined(TCP_USER_TIMEOUT)
596 val = 1000 * line->keepalive_num_probes *
597 line->keepalive_probe_interval +
598 line->keepalive_idle_timeout;
599 ret = setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
600 &val, sizeof(val));
601 if (ret < 0)
602 LOGP(DLINP, LOGL_NOTICE,
603 "Failed to set user timoeut: %s\n",
604 strerror(errno));
605#endif
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100606#endif
Holger Hans Peter Freytherf465a4c2014-02-03 09:34:02 +0100607 }
Oliver Smith62725d02020-04-17 10:57:46 +0200608
609 val = 1;
610 ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
611 if (ret < 0)
612 LOGP(DLINP, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100613}
614
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200615/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200616static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200617{
618 int ret;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100619 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200620 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200621 struct e1inp_ts *e1i_ts;
622 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200623
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200624 /* clone virtual E1 line for this new OML link. */
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200625 line = e1inp_line_clone(tall_ipa_ctx, link->line, "ipa_bfd");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200626 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200627 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200628 return -ENOMEM;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200629 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200630
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200631 /* create virrtual E1 timeslots for signalling */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200632 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200633
634 /* initialize the fds */
635 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
636 line->ts[i].driver.ipaccess.fd.fd = -1;
637
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200638 e1i_ts = e1inp_line_ipa_oml_ts(line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200639
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200640 bfd = &e1i_ts->driver.ipaccess.fd;
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200641 osmo_fd_setup(bfd, fd, BSC_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_OML);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200642 ret = osmo_fd_register(bfd);
643 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200644 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200645 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200646 }
647
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100648 update_fd_settings(line, bfd->fd);
Daniel Willmann85980722014-01-09 14:30:55 +0100649
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200650 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200651 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200652 if (ret < 0) {
653 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
654 strerror(errno));
655 goto err_socket;
656 }
657 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200658
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200659err_socket:
660 osmo_fd_unregister(bfd);
661err_line:
662 close(bfd->fd);
663 bfd->fd = -1;
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200664 e1inp_line_put2(line, "ipa_bfd");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200665 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200666}
667
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200668static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200669{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200670 struct e1inp_line *line;
671 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200672 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200673 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200674
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200675 /* We don't know yet which OML link to associate it with. Thus, we
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200676 * allocate a temporary E1 line until we have received ID. */
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200677 line = e1inp_line_clone(tall_ipa_ctx, link->line, "ipa_bfd");
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200678 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200679 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200680 return -ENOMEM;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200681 }
682 /* initialize the fds */
683 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
684 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200685
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200686 /* we need this to initialize this in case to avoid crashes in case
687 * that the socket is closed before we've seen an ID_RESP. */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200688 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200689
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200690 e1i_ts = e1inp_line_ipa_rsl_ts(line, 0);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200691
692 bfd = &e1i_ts->driver.ipaccess.fd;
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200693 osmo_fd_setup(bfd, fd, BSC_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_RSL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200694 ret = osmo_fd_register(bfd);
695 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200696 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200697 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200698 }
699 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200700 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200701 if (ret < 0) {
702 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
703 strerror(errno));
704 goto err_socket;
705 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100706 update_fd_settings(line, bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200707 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200708
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200709err_socket:
710 osmo_fd_unregister(bfd);
711err_line:
712 close(bfd->fd);
713 bfd->fd = -1;
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200714 e1inp_line_put2(line, "ipa_bfd");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200715 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200716}
717
Harald Weltee416e2e2017-05-26 13:11:59 +0200718#define IPA_STRING_MAX 64
719
720static struct msgb *
Max71393522018-01-08 15:42:00 +0100721ipa_bts_id_resp(const struct ipaccess_unit *dev, uint8_t *data, int len, int trx_nr)
Harald Weltee416e2e2017-05-26 13:11:59 +0200722{
723 struct msgb *nmsg;
724 char str[IPA_STRING_MAX];
725 uint8_t *tag;
726
727 memset(str, 0, sizeof(str));
728
729 nmsg = ipa_msg_alloc(0);
730 if (!nmsg)
731 return NULL;
732
733 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
734 while (len) {
735 if (len < 2) {
736 LOGP(DLINP, LOGL_NOTICE,
737 "Short read of ipaccess tag\n");
738 msgb_free(nmsg);
739 return NULL;
740 }
741 switch (data[1]) {
742 case IPAC_IDTAG_UNIT:
743 snprintf(str, sizeof(str), "%u/%u/%u",
744 dev->site_id, dev->bts_id, trx_nr);
745 break;
746 case IPAC_IDTAG_MACADDR:
747 snprintf(str, sizeof(str),
748 "%02x:%02x:%02x:%02x:%02x:%02x",
749 dev->mac_addr[0], dev->mac_addr[1],
750 dev->mac_addr[2], dev->mac_addr[3],
751 dev->mac_addr[4], dev->mac_addr[5]);
752 break;
753 case IPAC_IDTAG_LOCATION1:
754 if (dev->location1)
Stefan Sperling961776a2018-12-06 13:09:48 +0100755 osmo_strlcpy(str, dev->location1, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200756 break;
757 case IPAC_IDTAG_LOCATION2:
758 if (dev->location2)
Stefan Sperling961776a2018-12-06 13:09:48 +0100759 osmo_strlcpy(str, dev->location2, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200760 break;
761 case IPAC_IDTAG_EQUIPVERS:
762 if (dev->equipvers)
Stefan Sperling961776a2018-12-06 13:09:48 +0100763 osmo_strlcpy(str, dev->equipvers, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200764 break;
765 case IPAC_IDTAG_SWVERSION:
766 if (dev->swversion)
Neels Hofmeyr4c57eef2018-07-26 17:28:24 +0200767 osmo_strlcpy(str, dev->swversion, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200768 break;
769 case IPAC_IDTAG_UNITNAME:
770 snprintf(str, sizeof(str),
771 "%s-%02x-%02x-%02x-%02x-%02x-%02x",
772 dev->unit_name,
773 dev->mac_addr[0], dev->mac_addr[1],
774 dev->mac_addr[2], dev->mac_addr[3],
775 dev->mac_addr[4], dev->mac_addr[5]);
776 break;
777 case IPAC_IDTAG_SERNR:
778 if (dev->serno)
Stefan Sperling961776a2018-12-06 13:09:48 +0100779 osmo_strlcpy(str, dev->serno, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200780 break;
781 default:
782 LOGP(DLINP, LOGL_NOTICE,
783 "Unknown ipaccess tag 0x%02x\n", *data);
784 msgb_free(nmsg);
785 return NULL;
786 }
Harald Weltee416e2e2017-05-26 13:11:59 +0200787
788 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
789 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
790 tag[0] = 0x00;
791 tag[1] = 1 + strlen(str) + 1;
792 tag[2] = data[1];
793 memcpy(tag + 3, str, strlen(str) + 1);
794 data += 2;
795 len -= 2;
796 }
797 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
798 return nmsg;
799}
800
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200801static struct msgb *ipa_bts_id_ack(void)
802{
803 struct msgb *nmsg2;
804
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200805 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200806 if (!nmsg2)
807 return NULL;
808
809 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200810 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200811
812 return nmsg2;
813}
814
Harald Welte51de9ca2013-06-30 20:13:16 +0200815static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up)
816{
817 struct e1inp_line *line = link->line;
818
Eric Wildef1f3272019-07-10 18:10:31 +0200819 if (up) {
820 struct osmo_fsm_inst *ka_fsm = ipaccess_line_ts(link->ofd, line)->driver.ipaccess.ka_fsm;
821
822 update_fd_settings(line, link->ofd->fd);
823 if (ka_fsm && line->ipa_kap)
824 ipa_keepalive_fsm_start(ka_fsm);
825 return;
826 }
Harald Welte51de9ca2013-06-30 20:13:16 +0200827
828 if (line->ops->sign_link_down)
829 line->ops->sign_link_down(line);
830}
831
Harald Welte783715b2014-08-17 18:24:51 +0200832/* handle incoming message to BTS, check if it is an IPA CCM, and if yes,
833 * handle it accordingly (PING/PONG/ID_REQ/ID_RESP/ID_ACK) */
834int ipaccess_bts_handle_ccm(struct ipa_client_conn *link,
835 struct ipaccess_unit *dev, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200836{
837 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200838 struct msgb *rmsg;
839 int ret = 0;
Eric Wildef1f3272019-07-10 18:10:31 +0200840 /* line might not exist if != bsc||bts */
841 struct e1inp_line *line = link->line;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200842
843 /* special handling for IPA CCM. */
844 if (hh->proto == IPAC_PROTO_IPACCESS) {
845 uint8_t msg_type = *(msg->l2h);
Eric Wildef1f3272019-07-10 18:10:31 +0200846 struct osmo_fsm_inst* ka_fsm = NULL;
847
848 /* peek the pong for our keepalive fsm */
849 if (line && msg_type == IPAC_MSGT_PONG) {
850 ka_fsm = ipaccess_line_ts(link->ofd, line)->driver.ipaccess.ka_fsm;
851 ipa_keepalive_fsm_pong_received(ka_fsm);
852 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200853
854 /* ping, pong and acknowledgment cases. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200855 ret = ipa_ccm_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200856 if (ret < 0)
857 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200858
859 /* this is a request for identification from the BSC. */
860 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200861 uint8_t *data = msgb_l2(msg);
862 int len = msgb_l2len(msg);
Andreas Eversbergf422a752014-01-21 14:54:41 +0100863 int trx_nr = 0;
864
865 if (link->ofd->priv_nr >= E1INP_SIGN_RSL)
866 trx_nr = link->ofd->priv_nr - E1INP_SIGN_RSL;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200867
Vadim Yanitskiy69ae2382019-12-02 02:42:43 +0700868 LOGP(DLINP, LOGL_NOTICE, "received ID_GET for unit ID %u/%u/%u\n",
Pau Espin Pedroldd95eb62018-10-02 20:05:28 +0200869 dev->site_id, dev->bts_id, trx_nr);
Harald Weltee416e2e2017-05-26 13:11:59 +0200870 rmsg = ipa_bts_id_resp(dev, data + 1, len - 1, trx_nr);
Harald Welteb65f58f2014-08-20 22:04:11 +0200871 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200872 if (ret != rmsg->len) {
873 LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP "
874 "message. Reason: %s\n", strerror(errno));
875 goto err_rmsg;
876 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200877 msgb_free(rmsg);
878
879 /* send ID_ACK. */
880 rmsg = ipa_bts_id_ack();
Harald Welteb65f58f2014-08-20 22:04:11 +0200881 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200882 if (ret != rmsg->len) {
883 LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK "
884 "message. Reason: %s\n", strerror(errno));
885 goto err_rmsg;
886 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200887 msgb_free(rmsg);
Harald Welte783715b2014-08-17 18:24:51 +0200888 }
889 return 1;
890 }
Harald Welte6eddd472013-06-30 20:18:53 +0200891
Harald Welte783715b2014-08-17 18:24:51 +0200892 return 0;
893
894err_rmsg:
895 msgb_free(rmsg);
896err:
897 ipa_client_conn_close(link);
898 return -1;
899}
900
901static int ipaccess_bts_read_cb(struct ipa_client_conn *link, struct msgb *msg)
902{
903 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
904 struct e1inp_ts *e1i_ts = NULL;
905 struct e1inp_sign_link *sign_link;
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200906 uint8_t msg_type = *(msg->l2h);
Harald Welte783715b2014-08-17 18:24:51 +0200907 int ret = 0;
908
909 /* special handling for IPA CCM. */
910 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200911 /* this is a request for identification from the BSC. */
912 if (msg_type == IPAC_MSGT_ID_GET) {
913 if (!link->line->ops->sign_link_up) {
914 LOGP(DLINP, LOGL_ERROR,
915 "Unable to set signal link, "
916 "closing socket.\n");
Harald Welte783715b2014-08-17 18:24:51 +0200917 goto err;
918 }
919 }
920 }
921
922 /* core CCM handling */
923 ret = ipaccess_bts_handle_ccm(link, link->line->ops->cfg.ipa.dev, msg);
924 if (ret < 0)
925 goto err;
926
927 if (ret == 1 && hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200928 if (msg_type == IPAC_MSGT_ID_GET) {
Harald Welte6eddd472013-06-30 20:18:53 +0200929 sign_link = link->line->ops->sign_link_up(msg,
930 link->line,
931 link->ofd->priv_nr);
932 if (sign_link == NULL) {
933 LOGP(DLINP, LOGL_ERROR,
934 "Unable to set signal link, "
935 "closing socket.\n");
Harald Welte6eddd472013-06-30 20:18:53 +0200936 goto err;
937 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200938 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200939 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200940 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200941 } else if (link->port == IPA_TCP_PORT_OML)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200942 e1i_ts = e1inp_line_ipa_oml_ts(link->line);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200943 else if (link->port == IPA_TCP_PORT_RSL)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200944 e1i_ts = e1inp_line_ipa_rsl_ts(link->line, link->ofd->priv_nr - E1INP_SIGN_RSL);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200945
Pablo Neira Ayusob9487012013-07-05 14:38:43 +0200946 OSMO_ASSERT(e1i_ts != NULL);
947
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200948 if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
949 LOGP(DLINP, LOGL_ERROR, "Signalling link not initialized. Discarding."
950 " port=%u msg_type=%u\n", link->port, msg_type);
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200951 goto err;
952 }
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +0200953
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200954 /* look up for some existing signaling link. */
955 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
956 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200957 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200958 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200959 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200960 }
961 msg->dst = sign_link;
962
963 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200964 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200965 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200966 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200967 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200968 }
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200969 return link->line->ops->sign_link(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200970
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200971err:
Harald Welte783715b2014-08-17 18:24:51 +0200972 ipa_client_conn_close(link);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200973 msgb_free(msg);
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +0200974 return -EBADF;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200975}
976
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200977struct ipaccess_line {
Oliver Smith65ae42c2020-01-22 11:24:52 +0100978 bool line_already_initialized;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200979};
980
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200981static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200982{
983 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200984 struct ipaccess_line *il;
985
986 if (!line->driver_data)
987 line->driver_data = talloc_zero(line, struct ipaccess_line);
988
989 if (!line->driver_data) {
990 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
991 return -ENOMEM;
992 }
993 il = line->driver_data;
994
995 /* We only initialize this line once. */
996 if (il->line_already_initialized)
997 return 0;
998
Oliver Smith65ae42c2020-01-22 11:24:52 +0100999 il->line_already_initialized = true;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001000
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001001 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001002 case E1INP_LINE_R_BSC: {
1003 struct ipa_server_link *oml_link, *rsl_link;
Max4c4a1c22016-12-30 15:10:47 +01001004 const char *ipa = e1inp_ipa_get_bind_addr();
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001005
Max4c4a1c22016-12-30 15:10:47 +01001006 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode on %s "
1007 "with OML %u and RSL %u TCP ports\n", ipa,
1008 IPA_TCP_PORT_OML, IPA_TCP_PORT_RSL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001009
Max4c4a1c22016-12-30 15:10:47 +01001010 oml_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001011 IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +02001012 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001013 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001014 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001015 "BSC link: %s\n", strerror(errno));
1016 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +02001017 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001018 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001019 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001020 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001021 ipa_server_link_destroy(oml_link);
1022 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +02001023 }
Max4c4a1c22016-12-30 15:10:47 +01001024 rsl_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001025 IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +02001026 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001027 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001028 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001029 "BSC link: %s\n", strerror(errno));
1030 return -ENOMEM;
1031 }
1032 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001033 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001034 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001035 ipa_server_link_destroy(rsl_link);
1036 return -EIO;
1037 }
1038 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001039 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001040 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001041 case E1INP_LINE_R_BTS: {
Harald Welte84f67b22013-06-30 13:13:59 +02001042 struct ipa_client_conn *link;
Eric Wildef1f3272019-07-10 18:10:31 +02001043 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001044
Max4c4a1c22016-12-30 15:10:47 +01001045 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode, "
1046 "OML connecting to %s:%u\n", line->ops->cfg.ipa.addr,
1047 IPA_TCP_PORT_OML);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001048
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001049 link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +02001050 e1inp_line_ipa_oml_ts(line),
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +02001051 E1INP_SIGN_OML,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001052 NULL, 0,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001053 line->ops->cfg.ipa.addr,
1054 IPA_TCP_PORT_OML,
Harald Welte51de9ca2013-06-30 20:13:16 +02001055 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001056 ipaccess_bts_read_cb,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +02001057 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001058 line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001059 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001060 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001061 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001062 return -ENOMEM;
1063 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001064 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001065 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001066 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001067 ipa_client_conn_close(link);
1068 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001069 return -EIO;
1070 }
Eric Wildef1f3272019-07-10 18:10:31 +02001071
1072 e1i_ts = e1inp_line_ipa_oml_ts(line);
1073 ipaccess_bts_keepalive_fsm_alloc(e1i_ts, link, "oml_bts_to_bsc");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001074 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001075 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001076 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001077 default:
1078 break;
1079 }
1080 return ret;
1081}
1082
Andreas Eversbergf422a752014-01-21 14:54:41 +01001083
1084/* backwards compatibility */
Harald Welte84f67b22013-06-30 13:13:59 +02001085int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line,
1086 const char *rem_addr, uint16_t rem_port)
1087{
Andreas Eversbergf422a752014-01-21 14:54:41 +01001088 return e1inp_ipa_bts_rsl_connect_n(line, rem_addr, rem_port, 0);
1089}
1090
1091int e1inp_ipa_bts_rsl_connect_n(struct e1inp_line *line,
1092 const char *rem_addr, uint16_t rem_port,
1093 uint8_t trx_nr)
1094{
Harald Welte84f67b22013-06-30 13:13:59 +02001095 struct ipa_client_conn *rsl_link;
Eric Wildef1f3272019-07-10 18:10:31 +02001096 struct e1inp_ts *e1i_ts = e1inp_line_ipa_rsl_ts(line, trx_nr);
Harald Welte84f67b22013-06-30 13:13:59 +02001097
Andreas Eversbergf422a752014-01-21 14:54:41 +01001098 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
1099 LOGP(DLINP, LOGL_ERROR, "cannot create RSL BTS link: "
1100 "trx_nr (%d) out of range\n", trx_nr);
1101 return -EINVAL;
1102 }
Andreas Eversbergf422a752014-01-21 14:54:41 +01001103
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001104 rsl_link = ipa_client_conn_create2(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +02001105 e1inp_line_ipa_rsl_ts(line, trx_nr),
Andreas Eversbergf422a752014-01-21 14:54:41 +01001106 E1INP_SIGN_RSL+trx_nr,
Pau Espin Pedrolb6e28bf2019-11-08 17:30:28 +01001107 NULL, 0,
Harald Welte84f67b22013-06-30 13:13:59 +02001108 rem_addr, rem_port,
Harald Welte51de9ca2013-06-30 20:13:16 +02001109 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001110 ipaccess_bts_read_cb,
Harald Welte84f67b22013-06-30 13:13:59 +02001111 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001112 line);
Harald Welte84f67b22013-06-30 13:13:59 +02001113 if (rsl_link == NULL) {
1114 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
1115 "BTS link: %s\n", strerror(errno));
1116 return -ENOMEM;
1117 }
1118 if (ipa_client_conn_open(rsl_link) < 0) {
1119 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
1120 strerror(errno));
1121 ipa_client_conn_close(rsl_link);
1122 ipa_client_conn_destroy(rsl_link);
1123 return -EIO;
1124 }
Eric Wildef1f3272019-07-10 18:10:31 +02001125
1126 ipaccess_bts_keepalive_fsm_alloc(e1i_ts, rsl_link, "rsl_bts_to_bsc");
Harald Welte84f67b22013-06-30 13:13:59 +02001127 return 0;
1128}
1129
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001130void e1inp_ipaccess_init(void)
1131{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +02001132 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001133 e1inp_driver_register(&ipaccess_driver);
1134}
Neels Hofmeyr0db1d432016-02-22 13:29:09 +01001135
1136void e1inp_ipa_set_bind_addr(const char *ip_bind_addr)
1137{
1138 talloc_free((char*)ipaccess_driver.bind_addr);
1139 ipaccess_driver.bind_addr = NULL;
1140
1141 if (ip_bind_addr)
1142 ipaccess_driver.bind_addr = talloc_strdup(tall_ipa_ctx,
1143 ip_bind_addr);
1144}
1145
1146const char *e1inp_ipa_get_bind_addr(void)
1147{
1148 return ipaccess_driver.bind_addr?
1149 ipaccess_driver.bind_addr
1150 : "0.0.0.0";
1151}