blob: fb6ad108304ba5b1f28042c5a7a751a08a611a91 [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
Harald Welte10b41302013-06-30 14:05:49 +020062static int ipaccess_drop(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020063{
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +020064 int ret = 1;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +020065 struct e1inp_ts *e1i_ts;
66 if (bfd->priv_nr == E1INP_SIGN_OML)
67 e1i_ts = e1inp_line_ipa_oml_ts(line);
68 else
69 e1i_ts = e1inp_line_ipa_rsl_ts(line, bfd->priv_nr - E1INP_SIGN_RSL);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020070
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020071 /* Error case: we did not see any ID_RESP yet for this socket. */
72 if (bfd->fd != -1) {
Harald Weltecc2241b2011-07-19 16:06:06 +020073 LOGP(DLINP, LOGL_ERROR, "Forcing socket shutdown with "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020074 "no signal link set\n");
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +020075 osmo_fd_unregister(bfd);
76 close(bfd->fd);
77 bfd->fd = -1;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020078 ret = -ENOENT;
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +020079 }
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +020080
Jacob Erlbeck98af3c32014-03-31 10:53:32 +020081 msgb_free(e1i_ts->pending_msg);
82 e1i_ts->pending_msg = NULL;
83
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +020084 /* e1inp_sign_link_destroy releases the socket descriptors for us. */
85 line->ops->sign_link_down(line);
86
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020087 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020088}
89
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +020090/* Returns -1 on error, and 0 or 1 on success. If -1 or 1 is returned, line has
91 * been released and should not be used anymore by the caller. */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +020092static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
93 struct osmo_fd *bfd)
94{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020095 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +020096 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020097 struct ipaccess_unit unit_data = {};
98 struct e1inp_sign_link *sign_link;
99 char *unitid;
100 int len, ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200101
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200102 /* Handle IPA PING, PONG and ID_ACK messages. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200103 ret = ipa_ccm_rcvmsg_base(msg, bfd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200104 switch(ret) {
105 case -1:
106 /* error in IPA control message handling */
107 goto err;
108 case 1:
109 /* this is an IPA control message, skip further processing */
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200110 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200111 case 0:
112 /* this is not an IPA control message, continue */
113 break;
114 default:
115 LOGP(DLINP, LOGL_ERROR, "Unexpected return from "
Harald Welteb65f58f2014-08-20 22:04:11 +0200116 "ipa_ccm_rcvmsg_base "
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200117 "(ret=%d)\n", ret);
118 goto err;
119 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200120
121 switch (msg_type) {
122 case IPAC_MSGT_ID_RESP:
Harald Weltecc2241b2011-07-19 16:06:06 +0200123 DEBUGP(DLMI, "ID_RESP\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200124 /* parse tags, search for Unit ID */
Harald Welte82eb55e2018-08-01 13:22:55 +0200125 ret = ipa_ccm_id_resp_parse(&tlvp, (const uint8_t *)msg->l2h+1, msgb_l2len(msg)-1);
Harald Weltecc2241b2011-07-19 16:06:06 +0200126 DEBUGP(DLMI, "\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200127 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200128 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200129 "with malformed TLVs\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200130 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200131 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200132 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200133 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200134 "without unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200135 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200136
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200137 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200138 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200139 if (len < 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200140 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200141 "with too small unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200142 goto err;
143 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200144 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
145 unitid[len - 1] = '\0';
Harald Welteb65f58f2014-08-20 22:04:11 +0200146 ipa_parse_unitid(unitid, &unit_data);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200147
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200148 if (!line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200149 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200150 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200151 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200152 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200153 /* the BSC creates the new sign links at this stage. */
154 if (bfd->priv_nr == E1INP_SIGN_OML) {
155 sign_link =
156 line->ops->sign_link_up(&unit_data, line,
157 E1INP_SIGN_OML);
158 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200159 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200160 "Unable to set signal link, "
161 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200162 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200163 }
164 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200165 struct e1inp_ts *ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200166 struct osmo_fd *newbfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200167 struct e1inp_line *new_line;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200168
169 sign_link =
170 line->ops->sign_link_up(&unit_data, line,
171 E1INP_SIGN_RSL);
172 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200173 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200174 "Unable to set signal link, "
175 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200176 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200177 }
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200178 /* this is a bugtrap, the BSC should be using the
179 * virtual E1 line used by OML for this RSL link. */
180 if (sign_link->ts->line == line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200181 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200182 "Fix your BSC, you should use the "
183 "E1 line used by the OML link for "
184 "your RSL link.\n");
185 return 0;
186 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200187 /* Finally, we know which OML link is associated with
188 * this RSL link, attach it to this socket. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200189 bfd->data = new_line = sign_link->ts->line;
190 e1inp_line_get(new_line);
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200191 ts = e1inp_line_ipa_rsl_ts(new_line, unit_data.trx_id);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200192 newbfd = &ts->driver.ipaccess.fd;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200193
194 /* get rid of our old temporary bfd */
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200195 /* preserve 'newbfd->when' flags potentially set by sign_link_up() */
Pau Espin Pedrol67902bb2018-08-23 14:35:08 +0200196 osmo_fd_setup(newbfd, bfd->fd, newbfd->when | bfd->when, bfd->cb,
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200197 bfd->data, E1INP_SIGN_RSL + unit_data.trx_id);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200198 osmo_fd_unregister(bfd);
199 bfd->fd = -1;
Harald Welteae3a9932016-11-26 09:25:23 +0100200 ret = osmo_fd_register(newbfd);
201 if (ret < 0) {
202 LOGP(DLINP, LOGL_ERROR,
203 "could not register FD\n");
204 goto err;
205 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200206 /* now we can release the dummy RSL line. */
207 e1inp_line_put(line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200208 return 1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200209 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200210 break;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200211 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200212 LOGP(DLINP, LOGL_ERROR, "Unknown IPA message type\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200213 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200214 }
215 return 0;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200216err:
217 osmo_fd_unregister(bfd);
218 close(bfd->fd);
219 bfd->fd = -1;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200220 e1inp_line_put(line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200221 return -1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200222}
223
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200224/* Returns -EBADF if bfd cannot be used by the caller anymore after return. */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200225static int handle_ts1_read(struct osmo_fd *bfd)
226{
227 struct e1inp_line *line = bfd->data;
228 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200229 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200230 struct e1inp_sign_link *link;
231 struct ipaccess_head *hh;
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200232 struct msgb *msg = NULL;
Maxc9fa25e2017-01-09 13:23:15 +0100233 int ret, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200234
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200235 if (bfd->priv_nr == E1INP_SIGN_OML)
236 e1i_ts = e1inp_line_ipa_oml_ts(line);
237 else
238 e1i_ts = e1inp_line_ipa_rsl_ts(line, bfd->priv_nr - E1INP_SIGN_RSL);
239
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200240 ret = ipa_msg_recv_buffered(bfd->fd, &msg, &e1i_ts->pending_msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200241 if (ret < 0) {
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200242 if (ret == -EAGAIN)
243 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200244 LOGP(DLINP, LOGL_NOTICE, "Sign link problems, "
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200245 "closing socket. Reason: %s\n", strerror(-ret));
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200246 goto err;
247 } else if (ret == 0) {
248 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
249 goto err;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200250 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200251 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200252
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200253 hh = (struct ipaccess_head *) msg->data;
254 if (hh->proto == IPAC_PROTO_IPACCESS) {
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200255 ret = ipaccess_rcvmsg(line, msg, bfd);
256 /* BIG FAT WARNING: bfd might no longer exist here (ret != 0),
257 * since ipaccess_rcvmsg() might have free'd it !!! */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200258 msgb_free(msg);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200259 return ret != 0 ? -EBADF : 0;
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100260 } else if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
261 /* this sign link is not know yet.. complain. */
262 LOGP(DLINP, LOGL_ERROR, "Timeslot is not configured.\n");
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100263 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200264 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200265
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200266 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
267 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200268 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200269 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200270 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200271 }
272 msg->dst = link;
273
274 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200275 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200276 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200277 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200278 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200279 }
Maxc9fa25e2017-01-09 13:23:15 +0100280 rc = e1i_ts->line->ops->sign_link(msg);
281 if (rc < 0) {
Pablo Neira Ayusoa49c24d2012-10-16 11:24:08 +0200282 /* Don't close the signalling link if the upper layers report
283 * an error, that's too strict. BTW, the signalling layer is
284 * resposible for releasing the message.
285 */
Harald Weltecc2241b2011-07-19 16:06:06 +0200286 LOGP(DLINP, LOGL_ERROR, "Bad signalling message,"
Maxc9fa25e2017-01-09 13:23:15 +0100287 " sign_link returned error: %s.\n", strerror(-rc));
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200288 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200289
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200290 return rc;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200291err_msg:
292 msgb_free(msg);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200293err:
Harald Welte10b41302013-06-30 14:05:49 +0200294 ipaccess_drop(bfd, line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200295 return -EBADF;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200296}
297
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200298static int ts_want_write(struct e1inp_ts *e1i_ts)
299{
300 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
301
302 return 0;
303}
304
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200305static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200306{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200307 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200308 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100309 return e1inp_close_socket(e1i_ts, sign_link, bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200310}
311
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200312static void timeout_ts1_write(void *data)
313{
314 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
315
316 /* trigger write of ts1, due to tx delay timer */
317 ts_want_write(e1i_ts);
318}
319
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200320static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200321{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200322 unsigned int ts_nr = bfd->priv_nr;
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200323 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200324 struct e1inp_sign_link *sign_link;
325 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200326 int ret;
327
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200328 if (bfd->priv_nr == E1INP_SIGN_OML)
329 e1i_ts = e1inp_line_ipa_oml_ts(line);
330 else
331 e1i_ts = e1inp_line_ipa_rsl_ts(line, bfd->priv_nr - E1INP_SIGN_RSL);
332
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200333 bfd->when &= ~BSC_FD_WRITE;
334
335 /* get the next msg for this timeslot */
336 msg = e1inp_tx_ts(e1i_ts, &sign_link);
337 if (!msg) {
338 /* no message after tx delay timer */
339 return 0;
340 }
341
342 switch (sign_link->type) {
343 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200344 case E1INP_SIGN_RSL:
Harald Welte46fc7e22014-08-18 19:04:26 +0200345 case E1INP_SIGN_OSMO:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200346 break;
347 default:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200348 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200349 ret = -EINVAL;
350 goto out;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200351 }
352
353 msg->l2h = msg->data;
Harald Welteb65f58f2014-08-20 22:04:11 +0200354 ipa_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200355
Harald Weltecc2241b2011-07-19 16:06:06 +0200356 DEBUGP(DLMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200357
358 ret = send(bfd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200359 if (ret != msg->len) {
360 LOGP(DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling "
361 "message. Reason: %s\n", strerror(errno));
362 goto err;
363 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200364
365 /* set tx delay timer for next event */
Pablo Neira Ayusob26f2fd2017-06-07 18:32:13 +0200366 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200367
368 /* Reducing this might break the nanoBTS 900 init. */
369 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
370
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200371out:
372 msgb_free(msg);
373 return ret;
374err:
Harald Welte10b41302013-06-30 14:05:49 +0200375 ipaccess_drop(bfd, line);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200376 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200377 return ret;
378}
379
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200380static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200381{
382 struct e1inp_line *line = bfd->data;
383
384 return __handle_ts1_write(bfd, line);
385}
386
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200387static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200388{
389 struct e1inp_line *line = link->line;
390
391 return __handle_ts1_write(link->ofd, line);
392}
393
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200394/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200395int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200396{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200397 int rc = 0;
398
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200399 if (what & BSC_FD_READ)
400 rc = handle_ts1_read(bfd);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200401 if (rc != -EBADF && (what & BSC_FD_WRITE))
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200402 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200403
404 return rc;
405}
406
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200407static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200408
409struct e1inp_driver ipaccess_driver = {
410 .name = "ipa",
411 .want_write = ts_want_write,
412 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200413 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200414 .default_delay = 0,
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100415 .has_keepalive = 1,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200416};
417
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100418static void update_fd_settings(struct e1inp_line *line, int fd)
419{
420 int ret;
421 int val;
422
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100423 if (line->keepalive_num_probes) {
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100424 /* Enable TCP keepalive to find out if the connection is gone */
425 val = 1;
426 ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
427 if (ret < 0)
428 LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive: %s\n",
429 strerror(errno));
430 else
431 LOGP(DLINP, LOGL_NOTICE, "Keepalive is set: %i\n", ret);
432
433#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
434 /* The following options are not portable! */
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100435 val = line->keepalive_idle_timeout > 0 ?
436 line->keepalive_idle_timeout :
437 DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100438 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
439 &val, sizeof(val));
440 if (ret < 0)
441 LOGP(DLINP, LOGL_NOTICE,
442 "Failed to set keepalive idle time: %s\n",
443 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100444 val = line->keepalive_probe_interval > -1 ?
445 line->keepalive_probe_interval :
446 DEFAULT_TCP_KEEPALIVE_INTERVAL;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100447 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
448 &val, sizeof(val));
449 if (ret < 0)
450 LOGP(DLINP, LOGL_NOTICE,
451 "Failed to set keepalive interval: %s\n",
452 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100453 val = line->keepalive_num_probes > 0 ?
454 line->keepalive_num_probes :
455 DEFAULT_TCP_KEEPALIVE_RETRY_COUNT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100456 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
457 &val, sizeof(val));
458 if (ret < 0)
459 LOGP(DLINP, LOGL_NOTICE,
460 "Failed to set keepalive count: %s\n",
461 strerror(errno));
Eric Wild6eb186c2019-06-21 15:26:25 +0200462#if defined(TCP_USER_TIMEOUT)
463 val = 1000 * line->keepalive_num_probes *
464 line->keepalive_probe_interval +
465 line->keepalive_idle_timeout;
466 ret = setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
467 &val, sizeof(val));
468 if (ret < 0)
469 LOGP(DLINP, LOGL_NOTICE,
470 "Failed to set user timoeut: %s\n",
471 strerror(errno));
472#endif
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100473#endif
Holger Hans Peter Freytherf465a4c2014-02-03 09:34:02 +0100474 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100475}
476
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200477/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200478static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200479{
480 int ret;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100481 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200482 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200483 struct e1inp_ts *e1i_ts;
484 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200485
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200486 /* clone virtual E1 line for this new OML link. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200487 line = e1inp_line_clone(tall_ipa_ctx, link->line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200488 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200489 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200490 return -ENOMEM;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200491 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200492
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200493 /* create virrtual E1 timeslots for signalling */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200494 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200495
496 /* initialize the fds */
497 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
498 line->ts[i].driver.ipaccess.fd.fd = -1;
499
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200500 e1i_ts = e1inp_line_ipa_oml_ts(line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200501
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200502 bfd = &e1i_ts->driver.ipaccess.fd;
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200503 osmo_fd_setup(bfd, fd, BSC_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_OML);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200504 ret = osmo_fd_register(bfd);
505 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200506 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200507 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200508 }
509
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100510 update_fd_settings(line, bfd->fd);
Daniel Willmann85980722014-01-09 14:30:55 +0100511
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200512 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200513 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200514 if (ret < 0) {
515 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
516 strerror(errno));
517 goto err_socket;
518 }
519 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200520
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200521err_socket:
522 osmo_fd_unregister(bfd);
523err_line:
524 close(bfd->fd);
525 bfd->fd = -1;
526 e1inp_line_put(line);
527 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200528}
529
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200530static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200531{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200532 struct e1inp_line *line;
533 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200534 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200535 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200536
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200537 /* We don't know yet which OML link to associate it with. Thus, we
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200538 * allocate a temporary E1 line until we have received ID. */
539 line = e1inp_line_clone(tall_ipa_ctx, link->line);
540 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200541 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200542 return -ENOMEM;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200543 }
544 /* initialize the fds */
545 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
546 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200547
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200548 /* we need this to initialize this in case to avoid crashes in case
549 * that the socket is closed before we've seen an ID_RESP. */
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200550 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200551
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200552 e1i_ts = e1inp_line_ipa_rsl_ts(line, 0);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200553
554 bfd = &e1i_ts->driver.ipaccess.fd;
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200555 osmo_fd_setup(bfd, fd, BSC_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_RSL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200556 ret = osmo_fd_register(bfd);
557 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200558 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200559 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200560 }
561 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200562 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200563 if (ret < 0) {
564 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
565 strerror(errno));
566 goto err_socket;
567 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100568 update_fd_settings(line, bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200569 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200570
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200571err_socket:
572 osmo_fd_unregister(bfd);
573err_line:
574 close(bfd->fd);
575 bfd->fd = -1;
576 e1inp_line_put(line);
577 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200578}
579
Harald Weltee416e2e2017-05-26 13:11:59 +0200580#define IPA_STRING_MAX 64
581
582static struct msgb *
Max71393522018-01-08 15:42:00 +0100583ipa_bts_id_resp(const struct ipaccess_unit *dev, uint8_t *data, int len, int trx_nr)
Harald Weltee416e2e2017-05-26 13:11:59 +0200584{
585 struct msgb *nmsg;
586 char str[IPA_STRING_MAX];
587 uint8_t *tag;
588
589 memset(str, 0, sizeof(str));
590
591 nmsg = ipa_msg_alloc(0);
592 if (!nmsg)
593 return NULL;
594
595 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
596 while (len) {
597 if (len < 2) {
598 LOGP(DLINP, LOGL_NOTICE,
599 "Short read of ipaccess tag\n");
600 msgb_free(nmsg);
601 return NULL;
602 }
603 switch (data[1]) {
604 case IPAC_IDTAG_UNIT:
605 snprintf(str, sizeof(str), "%u/%u/%u",
606 dev->site_id, dev->bts_id, trx_nr);
607 break;
608 case IPAC_IDTAG_MACADDR:
609 snprintf(str, sizeof(str),
610 "%02x:%02x:%02x:%02x:%02x:%02x",
611 dev->mac_addr[0], dev->mac_addr[1],
612 dev->mac_addr[2], dev->mac_addr[3],
613 dev->mac_addr[4], dev->mac_addr[5]);
614 break;
615 case IPAC_IDTAG_LOCATION1:
616 if (dev->location1)
Stefan Sperling961776a2018-12-06 13:09:48 +0100617 osmo_strlcpy(str, dev->location1, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200618 break;
619 case IPAC_IDTAG_LOCATION2:
620 if (dev->location2)
Stefan Sperling961776a2018-12-06 13:09:48 +0100621 osmo_strlcpy(str, dev->location2, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200622 break;
623 case IPAC_IDTAG_EQUIPVERS:
624 if (dev->equipvers)
Stefan Sperling961776a2018-12-06 13:09:48 +0100625 osmo_strlcpy(str, dev->equipvers, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200626 break;
627 case IPAC_IDTAG_SWVERSION:
628 if (dev->swversion)
Neels Hofmeyr4c57eef2018-07-26 17:28:24 +0200629 osmo_strlcpy(str, dev->swversion, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200630 break;
631 case IPAC_IDTAG_UNITNAME:
632 snprintf(str, sizeof(str),
633 "%s-%02x-%02x-%02x-%02x-%02x-%02x",
634 dev->unit_name,
635 dev->mac_addr[0], dev->mac_addr[1],
636 dev->mac_addr[2], dev->mac_addr[3],
637 dev->mac_addr[4], dev->mac_addr[5]);
638 break;
639 case IPAC_IDTAG_SERNR:
640 if (dev->serno)
Stefan Sperling961776a2018-12-06 13:09:48 +0100641 osmo_strlcpy(str, dev->serno, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200642 break;
643 default:
644 LOGP(DLINP, LOGL_NOTICE,
645 "Unknown ipaccess tag 0x%02x\n", *data);
646 msgb_free(nmsg);
647 return NULL;
648 }
Harald Weltee416e2e2017-05-26 13:11:59 +0200649
650 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
651 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
652 tag[0] = 0x00;
653 tag[1] = 1 + strlen(str) + 1;
654 tag[2] = data[1];
655 memcpy(tag + 3, str, strlen(str) + 1);
656 data += 2;
657 len -= 2;
658 }
659 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
660 return nmsg;
661}
662
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200663static struct msgb *ipa_bts_id_ack(void)
664{
665 struct msgb *nmsg2;
666
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200667 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200668 if (!nmsg2)
669 return NULL;
670
671 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200672 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200673
674 return nmsg2;
675}
676
Harald Welte51de9ca2013-06-30 20:13:16 +0200677static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up)
678{
679 struct e1inp_line *line = link->line;
680
Eric Wildb8242722019-06-21 13:52:06 +0200681 if (up) {
682 update_fd_settings(line, link->ofd->fd);
683 return;
684 }
Harald Welte51de9ca2013-06-30 20:13:16 +0200685
686 if (line->ops->sign_link_down)
687 line->ops->sign_link_down(line);
688}
689
Harald Welte783715b2014-08-17 18:24:51 +0200690/* handle incoming message to BTS, check if it is an IPA CCM, and if yes,
691 * handle it accordingly (PING/PONG/ID_REQ/ID_RESP/ID_ACK) */
692int ipaccess_bts_handle_ccm(struct ipa_client_conn *link,
693 struct ipaccess_unit *dev, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200694{
695 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200696 struct msgb *rmsg;
697 int ret = 0;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200698
699 /* special handling for IPA CCM. */
700 if (hh->proto == IPAC_PROTO_IPACCESS) {
701 uint8_t msg_type = *(msg->l2h);
702
703 /* ping, pong and acknowledgment cases. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200704 ret = ipa_ccm_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200705 if (ret < 0)
706 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200707
708 /* this is a request for identification from the BSC. */
709 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200710 uint8_t *data = msgb_l2(msg);
711 int len = msgb_l2len(msg);
Andreas Eversbergf422a752014-01-21 14:54:41 +0100712 int trx_nr = 0;
713
714 if (link->ofd->priv_nr >= E1INP_SIGN_RSL)
715 trx_nr = link->ofd->priv_nr - E1INP_SIGN_RSL;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200716
Max71393522018-01-08 15:42:00 +0100717 LOGP(DLINP, LOGL_NOTICE, "received ID get from %u/%u/%u\n",
Pau Espin Pedroldd95eb62018-10-02 20:05:28 +0200718 dev->site_id, dev->bts_id, trx_nr);
Harald Weltee416e2e2017-05-26 13:11:59 +0200719 rmsg = ipa_bts_id_resp(dev, data + 1, len - 1, trx_nr);
Harald Welteb65f58f2014-08-20 22:04:11 +0200720 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200721 if (ret != rmsg->len) {
722 LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP "
723 "message. Reason: %s\n", strerror(errno));
724 goto err_rmsg;
725 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200726 msgb_free(rmsg);
727
728 /* send ID_ACK. */
729 rmsg = ipa_bts_id_ack();
Harald Welteb65f58f2014-08-20 22:04:11 +0200730 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200731 if (ret != rmsg->len) {
732 LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK "
733 "message. Reason: %s\n", strerror(errno));
734 goto err_rmsg;
735 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200736 msgb_free(rmsg);
Harald Welte783715b2014-08-17 18:24:51 +0200737 }
738 return 1;
739 }
Harald Welte6eddd472013-06-30 20:18:53 +0200740
Harald Welte783715b2014-08-17 18:24:51 +0200741 return 0;
742
743err_rmsg:
744 msgb_free(rmsg);
745err:
746 ipa_client_conn_close(link);
747 return -1;
748}
749
750static int ipaccess_bts_read_cb(struct ipa_client_conn *link, struct msgb *msg)
751{
752 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
753 struct e1inp_ts *e1i_ts = NULL;
754 struct e1inp_sign_link *sign_link;
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200755 uint8_t msg_type = *(msg->l2h);
Harald Welte783715b2014-08-17 18:24:51 +0200756 int ret = 0;
757
758 /* special handling for IPA CCM. */
759 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200760 /* this is a request for identification from the BSC. */
761 if (msg_type == IPAC_MSGT_ID_GET) {
762 if (!link->line->ops->sign_link_up) {
763 LOGP(DLINP, LOGL_ERROR,
764 "Unable to set signal link, "
765 "closing socket.\n");
Harald Welte783715b2014-08-17 18:24:51 +0200766 goto err;
767 }
768 }
769 }
770
771 /* core CCM handling */
772 ret = ipaccess_bts_handle_ccm(link, link->line->ops->cfg.ipa.dev, msg);
773 if (ret < 0)
774 goto err;
775
776 if (ret == 1 && hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte783715b2014-08-17 18:24:51 +0200777 if (msg_type == IPAC_MSGT_ID_GET) {
Harald Welte6eddd472013-06-30 20:18:53 +0200778 sign_link = link->line->ops->sign_link_up(msg,
779 link->line,
780 link->ofd->priv_nr);
781 if (sign_link == NULL) {
782 LOGP(DLINP, LOGL_ERROR,
783 "Unable to set signal link, "
784 "closing socket.\n");
Harald Welte6eddd472013-06-30 20:18:53 +0200785 goto err;
786 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200787 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200788 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200789 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200790 } else if (link->port == IPA_TCP_PORT_OML)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200791 e1i_ts = e1inp_line_ipa_oml_ts(link->line);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200792 else if (link->port == IPA_TCP_PORT_RSL)
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200793 e1i_ts = e1inp_line_ipa_rsl_ts(link->line, link->ofd->priv_nr - E1INP_SIGN_RSL);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200794
Pablo Neira Ayusob9487012013-07-05 14:38:43 +0200795 OSMO_ASSERT(e1i_ts != NULL);
796
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200797 if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
798 LOGP(DLINP, LOGL_ERROR, "Signalling link not initialized. Discarding."
799 " port=%u msg_type=%u\n", link->port, msg_type);
Neels Hofmeyrfe673112018-08-24 17:43:45 +0200800 goto err;
801 }
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +0200802
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200803 /* look up for some existing signaling link. */
804 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
805 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200806 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200807 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200808 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200809 }
810 msg->dst = sign_link;
811
812 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200813 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200814 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200815 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200816 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200817 }
Pau Espin Pedroled122f32018-08-28 18:25:02 +0200818 return link->line->ops->sign_link(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200819
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200820err:
Harald Welte783715b2014-08-17 18:24:51 +0200821 ipa_client_conn_close(link);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200822 msgb_free(msg);
Pau Espin Pedrol7ad2b152018-08-28 17:37:59 +0200823 return -EBADF;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200824}
825
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200826struct ipaccess_line {
827 int line_already_initialized;
828};
829
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200830static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200831{
832 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200833 struct ipaccess_line *il;
834
835 if (!line->driver_data)
836 line->driver_data = talloc_zero(line, struct ipaccess_line);
837
838 if (!line->driver_data) {
839 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
840 return -ENOMEM;
841 }
842 il = line->driver_data;
843
844 /* We only initialize this line once. */
845 if (il->line_already_initialized)
846 return 0;
847
848 il->line_already_initialized = 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200849
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200850 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200851 case E1INP_LINE_R_BSC: {
852 struct ipa_server_link *oml_link, *rsl_link;
Max4c4a1c22016-12-30 15:10:47 +0100853 const char *ipa = e1inp_ipa_get_bind_addr();
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200854
Max4c4a1c22016-12-30 15:10:47 +0100855 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode on %s "
856 "with OML %u and RSL %u TCP ports\n", ipa,
857 IPA_TCP_PORT_OML, IPA_TCP_PORT_RSL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200858
Max4c4a1c22016-12-30 15:10:47 +0100859 oml_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100860 IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200861 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200862 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200863 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200864 "BSC link: %s\n", strerror(errno));
865 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200866 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200867 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200868 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200869 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200870 ipa_server_link_destroy(oml_link);
871 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200872 }
Max4c4a1c22016-12-30 15:10:47 +0100873 rsl_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100874 IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200875 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200876 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200877 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200878 "BSC link: %s\n", strerror(errno));
879 return -ENOMEM;
880 }
881 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200882 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200883 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200884 ipa_server_link_destroy(rsl_link);
885 return -EIO;
886 }
887 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200888 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200889 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200890 case E1INP_LINE_R_BTS: {
Harald Welte84f67b22013-06-30 13:13:59 +0200891 struct ipa_client_conn *link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200892
Max4c4a1c22016-12-30 15:10:47 +0100893 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode, "
894 "OML connecting to %s:%u\n", line->ops->cfg.ipa.addr,
895 IPA_TCP_PORT_OML);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200896
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200897 link = ipa_client_conn_create(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200898 e1inp_line_ipa_oml_ts(line),
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +0200899 E1INP_SIGN_OML,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200900 line->ops->cfg.ipa.addr,
901 IPA_TCP_PORT_OML,
Harald Welte51de9ca2013-06-30 20:13:16 +0200902 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +0200903 ipaccess_bts_read_cb,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200904 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +0200905 line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200906 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200907 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200908 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200909 return -ENOMEM;
910 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200911 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200912 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200913 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200914 ipa_client_conn_close(link);
915 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200916 return -EIO;
917 }
918 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200919 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200920 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200921 default:
922 break;
923 }
924 return ret;
925}
926
Andreas Eversbergf422a752014-01-21 14:54:41 +0100927
928/* backwards compatibility */
Harald Welte84f67b22013-06-30 13:13:59 +0200929int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line,
930 const char *rem_addr, uint16_t rem_port)
931{
Andreas Eversbergf422a752014-01-21 14:54:41 +0100932 return e1inp_ipa_bts_rsl_connect_n(line, rem_addr, rem_port, 0);
933}
934
935int e1inp_ipa_bts_rsl_connect_n(struct e1inp_line *line,
936 const char *rem_addr, uint16_t rem_port,
937 uint8_t trx_nr)
938{
Harald Welte84f67b22013-06-30 13:13:59 +0200939 struct ipa_client_conn *rsl_link;
940
Andreas Eversbergf422a752014-01-21 14:54:41 +0100941 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
942 LOGP(DLINP, LOGL_ERROR, "cannot create RSL BTS link: "
943 "trx_nr (%d) out of range\n", trx_nr);
944 return -EINVAL;
945 }
Andreas Eversbergf422a752014-01-21 14:54:41 +0100946
Harald Welte84f67b22013-06-30 13:13:59 +0200947 rsl_link = ipa_client_conn_create(tall_ipa_ctx,
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +0200948 e1inp_line_ipa_rsl_ts(line, trx_nr),
Andreas Eversbergf422a752014-01-21 14:54:41 +0100949 E1INP_SIGN_RSL+trx_nr,
Harald Welte84f67b22013-06-30 13:13:59 +0200950 rem_addr, rem_port,
Harald Welte51de9ca2013-06-30 20:13:16 +0200951 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +0200952 ipaccess_bts_read_cb,
Harald Welte84f67b22013-06-30 13:13:59 +0200953 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +0200954 line);
Harald Welte84f67b22013-06-30 13:13:59 +0200955 if (rsl_link == NULL) {
956 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
957 "BTS link: %s\n", strerror(errno));
958 return -ENOMEM;
959 }
960 if (ipa_client_conn_open(rsl_link) < 0) {
961 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
962 strerror(errno));
963 ipa_client_conn_close(rsl_link);
964 ipa_client_conn_destroy(rsl_link);
965 return -EIO;
966 }
967 return 0;
968}
969
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200970void e1inp_ipaccess_init(void)
971{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200972 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200973 e1inp_driver_register(&ipaccess_driver);
974}
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100975
976void e1inp_ipa_set_bind_addr(const char *ip_bind_addr)
977{
978 talloc_free((char*)ipaccess_driver.bind_addr);
979 ipaccess_driver.bind_addr = NULL;
980
981 if (ip_bind_addr)
982 ipaccess_driver.bind_addr = talloc_strdup(tall_ipa_ctx,
983 ip_bind_addr);
984}
985
986const char *e1inp_ipa_get_bind_addr(void)
987{
988 return ipaccess_driver.bind_addr?
989 ipaccess_driver.bind_addr
990 : "0.0.0.0";
991}