blob: ac61e147a51e682a6b722111644e4692226d2d91 [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;
Jacob Erlbeck98af3c32014-03-31 10:53:32 +020065 unsigned int ts_nr = bfd->priv_nr;
66 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020067
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020068 /* Error case: we did not see any ID_RESP yet for this socket. */
69 if (bfd->fd != -1) {
Harald Weltecc2241b2011-07-19 16:06:06 +020070 LOGP(DLINP, LOGL_ERROR, "Forcing socket shutdown with "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020071 "no signal link set\n");
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +020072 osmo_fd_unregister(bfd);
73 close(bfd->fd);
74 bfd->fd = -1;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020075 ret = -ENOENT;
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +020076 }
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +020077
Jacob Erlbeck98af3c32014-03-31 10:53:32 +020078 msgb_free(e1i_ts->pending_msg);
79 e1i_ts->pending_msg = NULL;
80
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +020081 /* e1inp_sign_link_destroy releases the socket descriptors for us. */
82 line->ops->sign_link_down(line);
83
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020084 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020085}
86
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +020087/* Returns -1 on error, and 0 or 1 on success. If -1 or 1 is returned, line has
88 * been released and should not be used anymore by the caller. */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +020089static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
90 struct osmo_fd *bfd)
91{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020092 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +020093 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020094 struct ipaccess_unit unit_data = {};
95 struct e1inp_sign_link *sign_link;
96 char *unitid;
97 int len, ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +020098
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +020099 /* Handle IPA PING, PONG and ID_ACK messages. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200100 ret = ipa_ccm_rcvmsg_base(msg, bfd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200101 switch(ret) {
102 case -1:
103 /* error in IPA control message handling */
104 goto err;
105 case 1:
106 /* this is an IPA control message, skip further processing */
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200107 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200108 case 0:
109 /* this is not an IPA control message, continue */
110 break;
111 default:
112 LOGP(DLINP, LOGL_ERROR, "Unexpected return from "
Harald Welteb65f58f2014-08-20 22:04:11 +0200113 "ipa_ccm_rcvmsg_base "
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200114 "(ret=%d)\n", ret);
115 goto err;
116 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200117
118 switch (msg_type) {
119 case IPAC_MSGT_ID_RESP:
Harald Weltecc2241b2011-07-19 16:06:06 +0200120 DEBUGP(DLMI, "ID_RESP\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200121 /* parse tags, search for Unit ID */
Harald Welte82eb55e2018-08-01 13:22:55 +0200122 ret = ipa_ccm_id_resp_parse(&tlvp, (const uint8_t *)msg->l2h+1, msgb_l2len(msg)-1);
Harald Weltecc2241b2011-07-19 16:06:06 +0200123 DEBUGP(DLMI, "\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200124 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200125 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200126 "with malformed TLVs\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200127 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200128 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200129 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200130 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200131 "without unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200132 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200133
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200134 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200135 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200136 if (len < 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200137 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200138 "with too small unit ID\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200139 goto err;
140 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200141 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
142 unitid[len - 1] = '\0';
Harald Welteb65f58f2014-08-20 22:04:11 +0200143 ipa_parse_unitid(unitid, &unit_data);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200144
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200145 if (!line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200146 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200147 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200148 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200149 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200150 /* the BSC creates the new sign links at this stage. */
151 if (bfd->priv_nr == E1INP_SIGN_OML) {
152 sign_link =
153 line->ops->sign_link_up(&unit_data, line,
154 E1INP_SIGN_OML);
155 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200156 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200157 "Unable to set signal link, "
158 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200159 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200160 }
161 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200162 struct e1inp_ts *ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200163 struct osmo_fd *newbfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200164 struct e1inp_line *new_line;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200165
166 sign_link =
167 line->ops->sign_link_up(&unit_data, line,
168 E1INP_SIGN_RSL);
169 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200170 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200171 "Unable to set signal link, "
172 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200173 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200174 }
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200175 /* this is a bugtrap, the BSC should be using the
176 * virtual E1 line used by OML for this RSL link. */
177 if (sign_link->ts->line == line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200178 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200179 "Fix your BSC, you should use the "
180 "E1 line used by the OML link for "
181 "your RSL link.\n");
182 return 0;
183 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200184 /* Finally, we know which OML link is associated with
185 * this RSL link, attach it to this socket. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200186 bfd->data = new_line = sign_link->ts->line;
187 e1inp_line_get(new_line);
188 ts = &new_line->ts[E1INP_SIGN_RSL+unit_data.trx_id-1];
189 newbfd = &ts->driver.ipaccess.fd;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200190
191 /* get rid of our old temporary bfd */
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200192 /* preserve 'newbfd->when' flags potentially set by sign_link_up() */
Pau Espin Pedrol67902bb2018-08-23 14:35:08 +0200193 osmo_fd_setup(newbfd, bfd->fd, newbfd->when | bfd->when, bfd->cb,
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200194 bfd->data, E1INP_SIGN_RSL + unit_data.trx_id);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200195 osmo_fd_unregister(bfd);
196 bfd->fd = -1;
Harald Welteae3a9932016-11-26 09:25:23 +0100197 ret = osmo_fd_register(newbfd);
198 if (ret < 0) {
199 LOGP(DLINP, LOGL_ERROR,
200 "could not register FD\n");
201 goto err;
202 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200203 /* now we can release the dummy RSL line. */
204 e1inp_line_put(line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200205 return 1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200206 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200207 break;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200208 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200209 LOGP(DLINP, LOGL_ERROR, "Unknown IPA message type\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200210 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200211 }
212 return 0;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200213err:
214 osmo_fd_unregister(bfd);
215 close(bfd->fd);
216 bfd->fd = -1;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200217 e1inp_line_put(line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200218 return -1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200219}
220
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200221/* Returns -EBADF if bfd cannot be used by the caller anymore after return. */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200222static int handle_ts1_read(struct osmo_fd *bfd)
223{
224 struct e1inp_line *line = bfd->data;
225 unsigned int ts_nr = bfd->priv_nr;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200226 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200227 struct e1inp_sign_link *link;
228 struct ipaccess_head *hh;
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200229 struct msgb *msg = NULL;
Maxc9fa25e2017-01-09 13:23:15 +0100230 int ret, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200231
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200232 ret = ipa_msg_recv_buffered(bfd->fd, &msg, &e1i_ts->pending_msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200233 if (ret < 0) {
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200234 if (ret == -EAGAIN)
235 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200236 LOGP(DLINP, LOGL_NOTICE, "Sign link problems, "
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200237 "closing socket. Reason: %s\n", strerror(-ret));
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200238 goto err;
239 } else if (ret == 0) {
240 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
241 goto err;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200242 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200243 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200244
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200245 hh = (struct ipaccess_head *) msg->data;
246 if (hh->proto == IPAC_PROTO_IPACCESS) {
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200247 ret = ipaccess_rcvmsg(line, msg, bfd);
248 /* BIG FAT WARNING: bfd might no longer exist here (ret != 0),
249 * since ipaccess_rcvmsg() might have free'd it !!! */
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200250 msgb_free(msg);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200251 return ret != 0 ? -EBADF : 0;
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100252 } else if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
253 /* this sign link is not know yet.. complain. */
254 LOGP(DLINP, LOGL_ERROR, "Timeslot is not configured.\n");
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100255 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200256 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200257
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200258 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
259 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200260 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200261 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200262 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200263 }
264 msg->dst = link;
265
266 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200267 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200268 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200269 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200270 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200271 }
Maxc9fa25e2017-01-09 13:23:15 +0100272 rc = e1i_ts->line->ops->sign_link(msg);
273 if (rc < 0) {
Pablo Neira Ayusoa49c24d2012-10-16 11:24:08 +0200274 /* Don't close the signalling link if the upper layers report
275 * an error, that's too strict. BTW, the signalling layer is
276 * resposible for releasing the message.
277 */
Harald Weltecc2241b2011-07-19 16:06:06 +0200278 LOGP(DLINP, LOGL_ERROR, "Bad signalling message,"
Maxc9fa25e2017-01-09 13:23:15 +0100279 " sign_link returned error: %s.\n", strerror(-rc));
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200280 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200281
282 return 0;
283err_msg:
284 msgb_free(msg);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200285err:
Harald Welte10b41302013-06-30 14:05:49 +0200286 ipaccess_drop(bfd, line);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200287 return -EBADF;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200288}
289
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200290static int ts_want_write(struct e1inp_ts *e1i_ts)
291{
292 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
293
294 return 0;
295}
296
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200297static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200298{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200299 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200300 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100301 return e1inp_close_socket(e1i_ts, sign_link, bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200302}
303
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200304static void timeout_ts1_write(void *data)
305{
306 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
307
308 /* trigger write of ts1, due to tx delay timer */
309 ts_want_write(e1i_ts);
310}
311
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200312static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200313{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200314 unsigned int ts_nr = bfd->priv_nr;
315 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
316 struct e1inp_sign_link *sign_link;
317 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200318 int ret;
319
320 bfd->when &= ~BSC_FD_WRITE;
321
322 /* get the next msg for this timeslot */
323 msg = e1inp_tx_ts(e1i_ts, &sign_link);
324 if (!msg) {
325 /* no message after tx delay timer */
326 return 0;
327 }
328
329 switch (sign_link->type) {
330 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200331 case E1INP_SIGN_RSL:
Harald Welte46fc7e22014-08-18 19:04:26 +0200332 case E1INP_SIGN_OSMO:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200333 break;
334 default:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200335 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200336 ret = -EINVAL;
337 goto out;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200338 }
339
340 msg->l2h = msg->data;
Harald Welteb65f58f2014-08-20 22:04:11 +0200341 ipa_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200342
Harald Weltecc2241b2011-07-19 16:06:06 +0200343 DEBUGP(DLMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200344
345 ret = send(bfd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200346 if (ret != msg->len) {
347 LOGP(DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling "
348 "message. Reason: %s\n", strerror(errno));
349 goto err;
350 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200351
352 /* set tx delay timer for next event */
Pablo Neira Ayusob26f2fd2017-06-07 18:32:13 +0200353 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200354
355 /* Reducing this might break the nanoBTS 900 init. */
356 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
357
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200358out:
359 msgb_free(msg);
360 return ret;
361err:
Harald Welte10b41302013-06-30 14:05:49 +0200362 ipaccess_drop(bfd, line);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200363 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200364 return ret;
365}
366
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200367static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200368{
369 struct e1inp_line *line = bfd->data;
370
371 return __handle_ts1_write(bfd, line);
372}
373
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200374static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200375{
376 struct e1inp_line *line = link->line;
377
378 return __handle_ts1_write(link->ofd, line);
379}
380
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200381/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200382int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200383{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200384 int rc = 0;
385
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200386 if (what & BSC_FD_READ)
387 rc = handle_ts1_read(bfd);
Pau Espin Pedrol7edc25d2018-05-23 14:57:03 +0200388 if (rc != -EBADF && (what & BSC_FD_WRITE))
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200389 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200390
391 return rc;
392}
393
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200394static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200395
396struct e1inp_driver ipaccess_driver = {
397 .name = "ipa",
398 .want_write = ts_want_write,
399 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200400 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200401 .default_delay = 0,
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100402 .has_keepalive = 1,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200403};
404
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100405static void update_fd_settings(struct e1inp_line *line, int fd)
406{
407 int ret;
408 int val;
409
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100410 if (line->keepalive_num_probes) {
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100411 /* Enable TCP keepalive to find out if the connection is gone */
412 val = 1;
413 ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
414 if (ret < 0)
415 LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive: %s\n",
416 strerror(errno));
417 else
418 LOGP(DLINP, LOGL_NOTICE, "Keepalive is set: %i\n", ret);
419
420#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
421 /* The following options are not portable! */
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100422 val = line->keepalive_idle_timeout > 0 ?
423 line->keepalive_idle_timeout :
424 DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100425 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
426 &val, sizeof(val));
427 if (ret < 0)
428 LOGP(DLINP, LOGL_NOTICE,
429 "Failed to set keepalive idle time: %s\n",
430 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100431 val = line->keepalive_probe_interval > -1 ?
432 line->keepalive_probe_interval :
433 DEFAULT_TCP_KEEPALIVE_INTERVAL;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100434 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
435 &val, sizeof(val));
436 if (ret < 0)
437 LOGP(DLINP, LOGL_NOTICE,
438 "Failed to set keepalive interval: %s\n",
439 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100440 val = line->keepalive_num_probes > 0 ?
441 line->keepalive_num_probes :
442 DEFAULT_TCP_KEEPALIVE_RETRY_COUNT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100443 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
444 &val, sizeof(val));
445 if (ret < 0)
446 LOGP(DLINP, LOGL_NOTICE,
447 "Failed to set keepalive count: %s\n",
448 strerror(errno));
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100449#endif
Holger Hans Peter Freytherf465a4c2014-02-03 09:34:02 +0100450 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100451}
452
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200453/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200454static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200455{
456 int ret;
457 int idx = 0;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100458 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200459 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200460 struct e1inp_ts *e1i_ts;
461 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200462
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200463 /* clone virtual E1 line for this new OML link. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200464 line = e1inp_line_clone(tall_ipa_ctx, link->line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200465 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200466 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200467 return -ENOMEM;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200468 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200469
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200470 /* create virrtual E1 timeslots for signalling */
Pablo Neira Ayuso59301852011-06-27 15:44:23 +0200471 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200472
473 /* initialize the fds */
474 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
475 line->ts[i].driver.ipaccess.fd.fd = -1;
476
477 e1i_ts = &line->ts[idx];
478
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200479 bfd = &e1i_ts->driver.ipaccess.fd;
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200480 osmo_fd_setup(bfd, fd, BSC_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_OML);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200481 ret = osmo_fd_register(bfd);
482 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200483 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200484 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200485 }
486
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100487 update_fd_settings(line, bfd->fd);
Daniel Willmann85980722014-01-09 14:30:55 +0100488
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200489 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200490 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200491 if (ret < 0) {
492 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
493 strerror(errno));
494 goto err_socket;
495 }
496 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200497
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200498err_socket:
499 osmo_fd_unregister(bfd);
500err_line:
501 close(bfd->fd);
502 bfd->fd = -1;
503 e1inp_line_put(line);
504 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200505}
506
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200507static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200508{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200509 struct e1inp_line *line;
510 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200511 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200512 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200513
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200514 /* We don't know yet which OML link to associate it with. Thus, we
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200515 * allocate a temporary E1 line until we have received ID. */
516 line = e1inp_line_clone(tall_ipa_ctx, link->line);
517 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200518 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200519 return -ENOMEM;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200520 }
521 /* initialize the fds */
522 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
523 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200524
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200525 /* we need this to initialize this in case to avoid crashes in case
526 * that the socket is closed before we've seen an ID_RESP. */
527 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line);
528
529 e1i_ts = &line->ts[E1INP_SIGN_RSL-1];
530
531 bfd = &e1i_ts->driver.ipaccess.fd;
Pau Espin Pedrol56ae85f2018-08-22 13:11:19 +0200532 osmo_fd_setup(bfd, fd, BSC_FD_READ, ipaccess_fd_cb, line, E1INP_SIGN_RSL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200533 ret = osmo_fd_register(bfd);
534 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200535 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200536 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200537 }
538 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200539 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200540 if (ret < 0) {
541 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
542 strerror(errno));
543 goto err_socket;
544 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100545 update_fd_settings(line, bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200546 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200547
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200548err_socket:
549 osmo_fd_unregister(bfd);
550err_line:
551 close(bfd->fd);
552 bfd->fd = -1;
553 e1inp_line_put(line);
554 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200555}
556
Harald Weltee416e2e2017-05-26 13:11:59 +0200557#define IPA_STRING_MAX 64
558
559static struct msgb *
Max71393522018-01-08 15:42:00 +0100560ipa_bts_id_resp(const struct ipaccess_unit *dev, uint8_t *data, int len, int trx_nr)
Harald Weltee416e2e2017-05-26 13:11:59 +0200561{
562 struct msgb *nmsg;
563 char str[IPA_STRING_MAX];
564 uint8_t *tag;
565
566 memset(str, 0, sizeof(str));
567
568 nmsg = ipa_msg_alloc(0);
569 if (!nmsg)
570 return NULL;
571
572 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
573 while (len) {
574 if (len < 2) {
575 LOGP(DLINP, LOGL_NOTICE,
576 "Short read of ipaccess tag\n");
577 msgb_free(nmsg);
578 return NULL;
579 }
580 switch (data[1]) {
581 case IPAC_IDTAG_UNIT:
582 snprintf(str, sizeof(str), "%u/%u/%u",
583 dev->site_id, dev->bts_id, trx_nr);
584 break;
585 case IPAC_IDTAG_MACADDR:
586 snprintf(str, sizeof(str),
587 "%02x:%02x:%02x:%02x:%02x:%02x",
588 dev->mac_addr[0], dev->mac_addr[1],
589 dev->mac_addr[2], dev->mac_addr[3],
590 dev->mac_addr[4], dev->mac_addr[5]);
591 break;
592 case IPAC_IDTAG_LOCATION1:
593 if (dev->location1)
Neels Hofmeyr4c57eef2018-07-26 17:28:24 +0200594 osmo_strlcpy(str, dev->swversion, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200595 break;
596 case IPAC_IDTAG_LOCATION2:
597 if (dev->location2)
Neels Hofmeyr4c57eef2018-07-26 17:28:24 +0200598 osmo_strlcpy(str, dev->swversion, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200599 break;
600 case IPAC_IDTAG_EQUIPVERS:
601 if (dev->equipvers)
Neels Hofmeyr4c57eef2018-07-26 17:28:24 +0200602 osmo_strlcpy(str, dev->swversion, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200603 break;
604 case IPAC_IDTAG_SWVERSION:
605 if (dev->swversion)
Neels Hofmeyr4c57eef2018-07-26 17:28:24 +0200606 osmo_strlcpy(str, dev->swversion, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200607 break;
608 case IPAC_IDTAG_UNITNAME:
609 snprintf(str, sizeof(str),
610 "%s-%02x-%02x-%02x-%02x-%02x-%02x",
611 dev->unit_name,
612 dev->mac_addr[0], dev->mac_addr[1],
613 dev->mac_addr[2], dev->mac_addr[3],
614 dev->mac_addr[4], dev->mac_addr[5]);
615 break;
616 case IPAC_IDTAG_SERNR:
617 if (dev->serno)
Neels Hofmeyr4c57eef2018-07-26 17:28:24 +0200618 osmo_strlcpy(str, dev->swversion, sizeof(str));
Harald Weltee416e2e2017-05-26 13:11:59 +0200619 break;
620 default:
621 LOGP(DLINP, LOGL_NOTICE,
622 "Unknown ipaccess tag 0x%02x\n", *data);
623 msgb_free(nmsg);
624 return NULL;
625 }
Harald Weltee416e2e2017-05-26 13:11:59 +0200626
627 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
628 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
629 tag[0] = 0x00;
630 tag[1] = 1 + strlen(str) + 1;
631 tag[2] = data[1];
632 memcpy(tag + 3, str, strlen(str) + 1);
633 data += 2;
634 len -= 2;
635 }
636 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
637 return nmsg;
638}
639
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200640static struct msgb *ipa_bts_id_ack(void)
641{
642 struct msgb *nmsg2;
643
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200644 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200645 if (!nmsg2)
646 return NULL;
647
648 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200649 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200650
651 return nmsg2;
652}
653
Harald Welte51de9ca2013-06-30 20:13:16 +0200654static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up)
655{
656 struct e1inp_line *line = link->line;
657
658 if (up)
659 return;
660
661 if (line->ops->sign_link_down)
662 line->ops->sign_link_down(line);
663}
664
Harald Welte783715b2014-08-17 18:24:51 +0200665/* handle incoming message to BTS, check if it is an IPA CCM, and if yes,
666 * handle it accordingly (PING/PONG/ID_REQ/ID_RESP/ID_ACK) */
667int ipaccess_bts_handle_ccm(struct ipa_client_conn *link,
668 struct ipaccess_unit *dev, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200669{
670 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200671 struct msgb *rmsg;
672 int ret = 0;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200673
674 /* special handling for IPA CCM. */
675 if (hh->proto == IPAC_PROTO_IPACCESS) {
676 uint8_t msg_type = *(msg->l2h);
677
678 /* ping, pong and acknowledgment cases. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200679 ret = ipa_ccm_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200680 if (ret < 0)
681 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200682
683 /* this is a request for identification from the BSC. */
684 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200685 uint8_t *data = msgb_l2(msg);
686 int len = msgb_l2len(msg);
Andreas Eversbergf422a752014-01-21 14:54:41 +0100687 int trx_nr = 0;
688
689 if (link->ofd->priv_nr >= E1INP_SIGN_RSL)
690 trx_nr = link->ofd->priv_nr - E1INP_SIGN_RSL;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200691
Max71393522018-01-08 15:42:00 +0100692 LOGP(DLINP, LOGL_NOTICE, "received ID get from %u/%u/%u\n",
693 dev->site_id, dev->bts_id, dev->trx_id);
Harald Weltee416e2e2017-05-26 13:11:59 +0200694 rmsg = ipa_bts_id_resp(dev, data + 1, len - 1, trx_nr);
Harald Welteb65f58f2014-08-20 22:04:11 +0200695 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200696 if (ret != rmsg->len) {
697 LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP "
698 "message. Reason: %s\n", strerror(errno));
699 goto err_rmsg;
700 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200701 msgb_free(rmsg);
702
703 /* send ID_ACK. */
704 rmsg = ipa_bts_id_ack();
Harald Welteb65f58f2014-08-20 22:04:11 +0200705 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200706 if (ret != rmsg->len) {
707 LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK "
708 "message. Reason: %s\n", strerror(errno));
709 goto err_rmsg;
710 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200711 msgb_free(rmsg);
Harald Welte783715b2014-08-17 18:24:51 +0200712 }
713 return 1;
714 }
Harald Welte6eddd472013-06-30 20:18:53 +0200715
Harald Welte783715b2014-08-17 18:24:51 +0200716 return 0;
717
718err_rmsg:
719 msgb_free(rmsg);
720err:
721 ipa_client_conn_close(link);
722 return -1;
723}
724
725static int ipaccess_bts_read_cb(struct ipa_client_conn *link, struct msgb *msg)
726{
727 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
728 struct e1inp_ts *e1i_ts = NULL;
729 struct e1inp_sign_link *sign_link;
730 int ret = 0;
731
732 /* special handling for IPA CCM. */
733 if (hh->proto == IPAC_PROTO_IPACCESS) {
734 uint8_t msg_type = *(msg->l2h);
735
736 /* this is a request for identification from the BSC. */
737 if (msg_type == IPAC_MSGT_ID_GET) {
738 if (!link->line->ops->sign_link_up) {
739 LOGP(DLINP, LOGL_ERROR,
740 "Unable to set signal link, "
741 "closing socket.\n");
742 ret = -EINVAL;
743 goto err;
744 }
745 }
746 }
747
748 /* core CCM handling */
749 ret = ipaccess_bts_handle_ccm(link, link->line->ops->cfg.ipa.dev, msg);
750 if (ret < 0)
751 goto err;
752
753 if (ret == 1 && hh->proto == IPAC_PROTO_IPACCESS) {
754 uint8_t msg_type = *(msg->l2h);
755
756 if (msg_type == IPAC_MSGT_ID_GET) {
Harald Welte6eddd472013-06-30 20:18:53 +0200757 sign_link = link->line->ops->sign_link_up(msg,
758 link->line,
759 link->ofd->priv_nr);
760 if (sign_link == NULL) {
761 LOGP(DLINP, LOGL_ERROR,
762 "Unable to set signal link, "
763 "closing socket.\n");
764 ret = -EINVAL;
765 goto err;
766 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200767 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200768 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200769 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200770 } else if (link->port == IPA_TCP_PORT_OML)
771 e1i_ts = &link->line->ts[0];
772 else if (link->port == IPA_TCP_PORT_RSL)
Andreas Eversbergf422a752014-01-21 14:54:41 +0100773 e1i_ts = &link->line->ts[link->ofd->priv_nr-1];
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200774
Pablo Neira Ayusob9487012013-07-05 14:38:43 +0200775 OSMO_ASSERT(e1i_ts != NULL);
776
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200777 /* look up for some existing signaling link. */
778 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
779 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200780 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200781 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200782 ret = -EIO;
783 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200784 }
785 msg->dst = sign_link;
786
787 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200788 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200789 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200790 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200791 ret = -ENOENT;
792 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200793 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200794 link->line->ops->sign_link(msg);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200795 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200796
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200797err:
Harald Welte783715b2014-08-17 18:24:51 +0200798 ipa_client_conn_close(link);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200799 msgb_free(msg);
800 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200801}
802
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200803struct ipaccess_line {
804 int line_already_initialized;
805};
806
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200807static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200808{
809 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200810 struct ipaccess_line *il;
811
812 if (!line->driver_data)
813 line->driver_data = talloc_zero(line, struct ipaccess_line);
814
815 if (!line->driver_data) {
816 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
817 return -ENOMEM;
818 }
819 il = line->driver_data;
820
821 /* We only initialize this line once. */
822 if (il->line_already_initialized)
823 return 0;
824
825 il->line_already_initialized = 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200826
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200827 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200828 case E1INP_LINE_R_BSC: {
829 struct ipa_server_link *oml_link, *rsl_link;
Max4c4a1c22016-12-30 15:10:47 +0100830 const char *ipa = e1inp_ipa_get_bind_addr();
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200831
Max4c4a1c22016-12-30 15:10:47 +0100832 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode on %s "
833 "with OML %u and RSL %u TCP ports\n", ipa,
834 IPA_TCP_PORT_OML, IPA_TCP_PORT_RSL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200835
Max4c4a1c22016-12-30 15:10:47 +0100836 oml_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100837 IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200838 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200839 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200840 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200841 "BSC link: %s\n", strerror(errno));
842 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200843 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200844 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200845 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200846 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200847 ipa_server_link_destroy(oml_link);
848 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200849 }
Max4c4a1c22016-12-30 15:10:47 +0100850 rsl_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100851 IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200852 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200853 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200854 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200855 "BSC link: %s\n", strerror(errno));
856 return -ENOMEM;
857 }
858 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200859 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200860 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200861 ipa_server_link_destroy(rsl_link);
862 return -EIO;
863 }
864 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200865 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200866 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200867 case E1INP_LINE_R_BTS: {
Harald Welte84f67b22013-06-30 13:13:59 +0200868 struct ipa_client_conn *link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200869
Max4c4a1c22016-12-30 15:10:47 +0100870 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode, "
871 "OML connecting to %s:%u\n", line->ops->cfg.ipa.addr,
872 IPA_TCP_PORT_OML);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200873
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200874 link = ipa_client_conn_create(tall_ipa_ctx,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200875 &line->ts[E1INP_SIGN_OML-1],
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +0200876 E1INP_SIGN_OML,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200877 line->ops->cfg.ipa.addr,
878 IPA_TCP_PORT_OML,
Harald Welte51de9ca2013-06-30 20:13:16 +0200879 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +0200880 ipaccess_bts_read_cb,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200881 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +0200882 line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200883 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200884 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200885 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200886 return -ENOMEM;
887 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200888 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200889 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200890 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200891 ipa_client_conn_close(link);
892 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200893 return -EIO;
894 }
895 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200896 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200897 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200898 default:
899 break;
900 }
901 return ret;
902}
903
Andreas Eversbergf422a752014-01-21 14:54:41 +0100904
905/* backwards compatibility */
Harald Welte84f67b22013-06-30 13:13:59 +0200906int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line,
907 const char *rem_addr, uint16_t rem_port)
908{
Andreas Eversbergf422a752014-01-21 14:54:41 +0100909 return e1inp_ipa_bts_rsl_connect_n(line, rem_addr, rem_port, 0);
910}
911
912int e1inp_ipa_bts_rsl_connect_n(struct e1inp_line *line,
913 const char *rem_addr, uint16_t rem_port,
914 uint8_t trx_nr)
915{
Harald Welte84f67b22013-06-30 13:13:59 +0200916 struct ipa_client_conn *rsl_link;
917
Andreas Eversbergf422a752014-01-21 14:54:41 +0100918 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
919 LOGP(DLINP, LOGL_ERROR, "cannot create RSL BTS link: "
920 "trx_nr (%d) out of range\n", trx_nr);
921 return -EINVAL;
922 }
Andreas Eversbergf422a752014-01-21 14:54:41 +0100923
Harald Welte84f67b22013-06-30 13:13:59 +0200924 rsl_link = ipa_client_conn_create(tall_ipa_ctx,
Andreas Eversbergf422a752014-01-21 14:54:41 +0100925 &line->ts[E1INP_SIGN_RSL+trx_nr-1],
926 E1INP_SIGN_RSL+trx_nr,
Harald Welte84f67b22013-06-30 13:13:59 +0200927 rem_addr, rem_port,
Harald Welte51de9ca2013-06-30 20:13:16 +0200928 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +0200929 ipaccess_bts_read_cb,
Harald Welte84f67b22013-06-30 13:13:59 +0200930 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +0200931 line);
Harald Welte84f67b22013-06-30 13:13:59 +0200932 if (rsl_link == NULL) {
933 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
934 "BTS link: %s\n", strerror(errno));
935 return -ENOMEM;
936 }
937 if (ipa_client_conn_open(rsl_link) < 0) {
938 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
939 strerror(errno));
940 ipa_client_conn_close(rsl_link);
941 ipa_client_conn_destroy(rsl_link);
942 return -EIO;
943 }
944 return 0;
945}
946
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200947void e1inp_ipaccess_init(void)
948{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200949 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200950 e1inp_driver_register(&ipaccess_driver);
951}
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100952
953void e1inp_ipa_set_bind_addr(const char *ip_bind_addr)
954{
955 talloc_free((char*)ipaccess_driver.bind_addr);
956 ipaccess_driver.bind_addr = NULL;
957
958 if (ip_bind_addr)
959 ipaccess_driver.bind_addr = talloc_strdup(tall_ipa_ctx,
960 ip_bind_addr);
961}
962
963const char *e1inp_ipa_get_bind_addr(void)
964{
965 return ipaccess_driver.bind_addr?
966 ipaccess_driver.bind_addr
967 : "0.0.0.0";
968}