blob: 9a80d8efad073869fed92390ffe47f7ba0093995 [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
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +020087static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
88 struct osmo_fd *bfd)
89{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020090 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +020091 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020092 struct ipaccess_unit unit_data = {};
93 struct e1inp_sign_link *sign_link;
94 char *unitid;
95 int len, ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +020096
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +020097 /* Handle IPA PING, PONG and ID_ACK messages. */
Harald Welteb65f58f2014-08-20 22:04:11 +020098 ret = ipa_ccm_rcvmsg_base(msg, bfd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +020099 switch(ret) {
100 case -1:
101 /* error in IPA control message handling */
102 goto err;
103 case 1:
104 /* this is an IPA control message, skip further processing */
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200105 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200106 case 0:
107 /* this is not an IPA control message, continue */
108 break;
109 default:
110 LOGP(DLINP, LOGL_ERROR, "Unexpected return from "
Harald Welteb65f58f2014-08-20 22:04:11 +0200111 "ipa_ccm_rcvmsg_base "
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200112 "(ret=%d)\n", ret);
113 goto err;
114 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200115
116 switch (msg_type) {
117 case IPAC_MSGT_ID_RESP:
Harald Weltecc2241b2011-07-19 16:06:06 +0200118 DEBUGP(DLMI, "ID_RESP\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200119 /* parse tags, search for Unit ID */
Harald Welteb65f58f2014-08-20 22:04:11 +0200120 ret = ipa_ccm_idtag_parse(&tlvp, (uint8_t *)msg->l2h + 2,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200121 msgb_l2len(msg)-2);
Harald Weltecc2241b2011-07-19 16:06:06 +0200122 DEBUGP(DLMI, "\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200123 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200124 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200125 "with malformed TLVs\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200126 ret = -EINVAL;
127 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");
132 ret = -EINVAL;
133 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200134
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200135 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200136 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200137 if (len < 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200138 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200139 "with too small unit ID\n");
140 ret = -EINVAL;
141 goto err;
142 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200143 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
144 unitid[len - 1] = '\0';
Harald Welteb65f58f2014-08-20 22:04:11 +0200145 ipa_parse_unitid(unitid, &unit_data);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200146
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200147 if (!line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200148 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200149 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200150 ret = -EINVAL;
151 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 ret = -EINVAL;
163 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200164 }
165 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200166 struct e1inp_ts *ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200167 struct osmo_fd *newbfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200168 struct e1inp_line *new_line;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200169
170 sign_link =
171 line->ops->sign_link_up(&unit_data, line,
172 E1INP_SIGN_RSL);
173 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200174 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200175 "Unable to set signal link, "
176 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200177 ret = -EINVAL;
178 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200179 }
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200180 /* this is a bugtrap, the BSC should be using the
181 * virtual E1 line used by OML for this RSL link. */
182 if (sign_link->ts->line == line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200183 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200184 "Fix your BSC, you should use the "
185 "E1 line used by the OML link for "
186 "your RSL link.\n");
187 return 0;
188 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200189 /* Finally, we know which OML link is associated with
190 * this RSL link, attach it to this socket. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200191 bfd->data = new_line = sign_link->ts->line;
192 e1inp_line_get(new_line);
193 ts = &new_line->ts[E1INP_SIGN_RSL+unit_data.trx_id-1];
194 newbfd = &ts->driver.ipaccess.fd;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200195
196 /* get rid of our old temporary bfd */
Stefan Sperling49917c12018-03-22 20:19:56 +0100197 memcpy(&newbfd->list, &bfd->list, sizeof(newbfd->list));
198 newbfd->fd = bfd->fd;
199 newbfd->when |= bfd->when; /* preserve 'newbfd->when' flags potentially set by sign_link_up() */
200 newbfd->cb = bfd->cb;
201 newbfd->data = bfd->data;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200202 newbfd->priv_nr = E1INP_SIGN_RSL + unit_data.trx_id;
203 osmo_fd_unregister(bfd);
204 bfd->fd = -1;
Harald Welteae3a9932016-11-26 09:25:23 +0100205 ret = osmo_fd_register(newbfd);
206 if (ret < 0) {
207 LOGP(DLINP, LOGL_ERROR,
208 "could not register FD\n");
209 goto err;
210 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200211 /* now we can release the dummy RSL line. */
212 e1inp_line_put(line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200213 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200214 break;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200215 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200216 LOGP(DLINP, LOGL_ERROR, "Unknown IPA message type\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200217 ret = -EINVAL;
218 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200219 }
220 return 0;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200221err:
222 osmo_fd_unregister(bfd);
223 close(bfd->fd);
224 bfd->fd = -1;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200225 e1inp_line_put(line);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200226 return ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200227}
228
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200229static int handle_ts1_read(struct osmo_fd *bfd)
230{
231 struct e1inp_line *line = bfd->data;
232 unsigned int ts_nr = bfd->priv_nr;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200233 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200234 struct e1inp_sign_link *link;
235 struct ipaccess_head *hh;
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200236 struct msgb *msg = NULL;
Maxc9fa25e2017-01-09 13:23:15 +0100237 int ret, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200238
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200239 ret = ipa_msg_recv_buffered(bfd->fd, &msg, &e1i_ts->pending_msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200240 if (ret < 0) {
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200241 if (ret == -EAGAIN)
242 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200243 LOGP(DLINP, LOGL_NOTICE, "Sign link problems, "
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200244 "closing socket. Reason: %s\n", strerror(-ret));
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200245 goto err;
246 } else if (ret == 0) {
247 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
248 goto err;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200249 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200250 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200251
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200252 hh = (struct ipaccess_head *) msg->data;
253 if (hh->proto == IPAC_PROTO_IPACCESS) {
254 ipaccess_rcvmsg(line, msg, bfd);
255 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200256 return 0;
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100257 } else if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
258 /* this sign link is not know yet.. complain. */
259 LOGP(DLINP, LOGL_ERROR, "Timeslot is not configured.\n");
260 ret = -EINVAL;
261 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200262 }
263 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
264 * might have free'd it !!! */
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 ret = -EINVAL;
271 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200272 }
273 msg->dst = link;
274
275 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200276 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200277 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200278 "no action set for signalling messages.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200279 ret = -EINVAL;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200280 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200281 }
Maxc9fa25e2017-01-09 13:23:15 +0100282 rc = e1i_ts->line->ops->sign_link(msg);
283 if (rc < 0) {
Pablo Neira Ayusoa49c24d2012-10-16 11:24:08 +0200284 /* Don't close the signalling link if the upper layers report
285 * an error, that's too strict. BTW, the signalling layer is
286 * resposible for releasing the message.
287 */
Harald Weltecc2241b2011-07-19 16:06:06 +0200288 LOGP(DLINP, LOGL_ERROR, "Bad signalling message,"
Maxc9fa25e2017-01-09 13:23:15 +0100289 " sign_link returned error: %s.\n", strerror(-rc));
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200290 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200291
292 return 0;
293err_msg:
294 msgb_free(msg);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200295err:
Harald Welte10b41302013-06-30 14:05:49 +0200296 ipaccess_drop(bfd, line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200297 return ret;
298}
299
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200300static int ts_want_write(struct e1inp_ts *e1i_ts)
301{
302 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
303
304 return 0;
305}
306
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200307static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200308{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200309 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200310 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100311 return e1inp_close_socket(e1i_ts, sign_link, bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200312}
313
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200314static void timeout_ts1_write(void *data)
315{
316 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
317
318 /* trigger write of ts1, due to tx delay timer */
319 ts_want_write(e1i_ts);
320}
321
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200322static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200323{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200324 unsigned int ts_nr = bfd->priv_nr;
325 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
326 struct e1inp_sign_link *sign_link;
327 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200328 int ret;
329
330 bfd->when &= ~BSC_FD_WRITE;
331
332 /* get the next msg for this timeslot */
333 msg = e1inp_tx_ts(e1i_ts, &sign_link);
334 if (!msg) {
335 /* no message after tx delay timer */
336 return 0;
337 }
338
339 switch (sign_link->type) {
340 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200341 case E1INP_SIGN_RSL:
Harald Welte46fc7e22014-08-18 19:04:26 +0200342 case E1INP_SIGN_OSMO:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200343 break;
344 default:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200345 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200346 ret = -EINVAL;
347 goto out;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200348 }
349
350 msg->l2h = msg->data;
Harald Welteb65f58f2014-08-20 22:04:11 +0200351 ipa_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200352
Harald Weltecc2241b2011-07-19 16:06:06 +0200353 DEBUGP(DLMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200354
355 ret = send(bfd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200356 if (ret != msg->len) {
357 LOGP(DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling "
358 "message. Reason: %s\n", strerror(errno));
359 goto err;
360 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200361
362 /* set tx delay timer for next event */
Pablo Neira Ayusob26f2fd2017-06-07 18:32:13 +0200363 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200364
365 /* Reducing this might break the nanoBTS 900 init. */
366 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
367
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200368out:
369 msgb_free(msg);
370 return ret;
371err:
Harald Welte10b41302013-06-30 14:05:49 +0200372 ipaccess_drop(bfd, line);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200373 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200374 return ret;
375}
376
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200377static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200378{
379 struct e1inp_line *line = bfd->data;
380
381 return __handle_ts1_write(bfd, line);
382}
383
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200384static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200385{
386 struct e1inp_line *line = link->line;
387
388 return __handle_ts1_write(link->ofd, line);
389}
390
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200391/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200392int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200393{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200394 int rc = 0;
395
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200396 if (what & BSC_FD_READ)
397 rc = handle_ts1_read(bfd);
398 if (what & BSC_FD_WRITE)
399 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200400
401 return rc;
402}
403
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200404static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200405
406struct e1inp_driver ipaccess_driver = {
407 .name = "ipa",
408 .want_write = ts_want_write,
409 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200410 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200411 .default_delay = 0,
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100412 .has_keepalive = 1,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200413};
414
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100415static void update_fd_settings(struct e1inp_line *line, int fd)
416{
417 int ret;
418 int val;
419
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100420 if (line->keepalive_num_probes) {
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100421 /* Enable TCP keepalive to find out if the connection is gone */
422 val = 1;
423 ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
424 if (ret < 0)
425 LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive: %s\n",
426 strerror(errno));
427 else
428 LOGP(DLINP, LOGL_NOTICE, "Keepalive is set: %i\n", ret);
429
430#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
431 /* The following options are not portable! */
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100432 val = line->keepalive_idle_timeout > 0 ?
433 line->keepalive_idle_timeout :
434 DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100435 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
436 &val, sizeof(val));
437 if (ret < 0)
438 LOGP(DLINP, LOGL_NOTICE,
439 "Failed to set keepalive idle time: %s\n",
440 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100441 val = line->keepalive_probe_interval > -1 ?
442 line->keepalive_probe_interval :
443 DEFAULT_TCP_KEEPALIVE_INTERVAL;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100444 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
445 &val, sizeof(val));
446 if (ret < 0)
447 LOGP(DLINP, LOGL_NOTICE,
448 "Failed to set keepalive interval: %s\n",
449 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100450 val = line->keepalive_num_probes > 0 ?
451 line->keepalive_num_probes :
452 DEFAULT_TCP_KEEPALIVE_RETRY_COUNT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100453 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
454 &val, sizeof(val));
455 if (ret < 0)
456 LOGP(DLINP, LOGL_NOTICE,
457 "Failed to set keepalive count: %s\n",
458 strerror(errno));
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100459#endif
Holger Hans Peter Freytherf465a4c2014-02-03 09:34:02 +0100460 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100461}
462
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200463/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200464static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200465{
466 int ret;
467 int idx = 0;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100468 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200469 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200470 struct e1inp_ts *e1i_ts;
471 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200472
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200473 /* clone virtual E1 line for this new OML link. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200474 line = e1inp_line_clone(tall_ipa_ctx, link->line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200475 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200476 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200477 return -ENOMEM;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200478 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200479
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200480 /* create virrtual E1 timeslots for signalling */
Pablo Neira Ayuso59301852011-06-27 15:44:23 +0200481 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200482
483 /* initialize the fds */
484 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
485 line->ts[i].driver.ipaccess.fd.fd = -1;
486
487 e1i_ts = &line->ts[idx];
488
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200489 bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200490 bfd->fd = fd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200491 bfd->data = line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200492 bfd->priv_nr = E1INP_SIGN_OML;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200493 bfd->cb = ipaccess_fd_cb;
494 bfd->when = BSC_FD_READ;
495 ret = osmo_fd_register(bfd);
496 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200497 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200498 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200499 }
500
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100501 update_fd_settings(line, bfd->fd);
Daniel Willmann85980722014-01-09 14:30:55 +0100502
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200503 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200504 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200505 if (ret < 0) {
506 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
507 strerror(errno));
508 goto err_socket;
509 }
510 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200511
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200512err_socket:
513 osmo_fd_unregister(bfd);
514err_line:
515 close(bfd->fd);
516 bfd->fd = -1;
517 e1inp_line_put(line);
518 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200519}
520
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200521static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200522{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200523 struct e1inp_line *line;
524 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200525 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200526 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200527
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200528 /* We don't know yet which OML link to associate it with. Thus, we
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200529 * allocate a temporary E1 line until we have received ID. */
530 line = e1inp_line_clone(tall_ipa_ctx, link->line);
531 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200532 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200533 return -ENOMEM;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200534 }
535 /* initialize the fds */
536 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
537 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200538
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200539 /* we need this to initialize this in case to avoid crashes in case
540 * that the socket is closed before we've seen an ID_RESP. */
541 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line);
542
543 e1i_ts = &line->ts[E1INP_SIGN_RSL-1];
544
545 bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200546 bfd->fd = fd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200547 bfd->data = line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200548 bfd->priv_nr = E1INP_SIGN_RSL;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200549 bfd->cb = ipaccess_fd_cb;
550 bfd->when = BSC_FD_READ;
551 ret = osmo_fd_register(bfd);
552 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200553 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200554 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200555 }
556 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200557 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200558 if (ret < 0) {
559 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
560 strerror(errno));
561 goto err_socket;
562 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100563 update_fd_settings(line, bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200564 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200565
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200566err_socket:
567 osmo_fd_unregister(bfd);
568err_line:
569 close(bfd->fd);
570 bfd->fd = -1;
571 e1inp_line_put(line);
572 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200573}
574
Harald Weltee416e2e2017-05-26 13:11:59 +0200575#define IPA_STRING_MAX 64
576
577static struct msgb *
Max71393522018-01-08 15:42:00 +0100578ipa_bts_id_resp(const struct ipaccess_unit *dev, uint8_t *data, int len, int trx_nr)
Harald Weltee416e2e2017-05-26 13:11:59 +0200579{
580 struct msgb *nmsg;
581 char str[IPA_STRING_MAX];
582 uint8_t *tag;
583
584 memset(str, 0, sizeof(str));
585
586 nmsg = ipa_msg_alloc(0);
587 if (!nmsg)
588 return NULL;
589
590 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
591 while (len) {
592 if (len < 2) {
593 LOGP(DLINP, LOGL_NOTICE,
594 "Short read of ipaccess tag\n");
595 msgb_free(nmsg);
596 return NULL;
597 }
598 switch (data[1]) {
599 case IPAC_IDTAG_UNIT:
600 snprintf(str, sizeof(str), "%u/%u/%u",
601 dev->site_id, dev->bts_id, trx_nr);
602 break;
603 case IPAC_IDTAG_MACADDR:
604 snprintf(str, sizeof(str),
605 "%02x:%02x:%02x:%02x:%02x:%02x",
606 dev->mac_addr[0], dev->mac_addr[1],
607 dev->mac_addr[2], dev->mac_addr[3],
608 dev->mac_addr[4], dev->mac_addr[5]);
609 break;
610 case IPAC_IDTAG_LOCATION1:
611 if (dev->location1)
612 strncpy(str, dev->location1, IPA_STRING_MAX);
613 break;
614 case IPAC_IDTAG_LOCATION2:
615 if (dev->location2)
616 strncpy(str, dev->location2, IPA_STRING_MAX);
617 break;
618 case IPAC_IDTAG_EQUIPVERS:
619 if (dev->equipvers)
620 strncpy(str, dev->equipvers, IPA_STRING_MAX);
621 break;
622 case IPAC_IDTAG_SWVERSION:
623 if (dev->swversion)
624 strncpy(str, dev->swversion, IPA_STRING_MAX);
625 break;
626 case IPAC_IDTAG_UNITNAME:
627 snprintf(str, sizeof(str),
628 "%s-%02x-%02x-%02x-%02x-%02x-%02x",
629 dev->unit_name,
630 dev->mac_addr[0], dev->mac_addr[1],
631 dev->mac_addr[2], dev->mac_addr[3],
632 dev->mac_addr[4], dev->mac_addr[5]);
633 break;
634 case IPAC_IDTAG_SERNR:
635 if (dev->serno)
636 strncpy(str, dev->serno, IPA_STRING_MAX);
637 break;
638 default:
639 LOGP(DLINP, LOGL_NOTICE,
640 "Unknown ipaccess tag 0x%02x\n", *data);
641 msgb_free(nmsg);
642 return NULL;
643 }
644 str[IPA_STRING_MAX-1] = '\0';
645
646 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
647 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
648 tag[0] = 0x00;
649 tag[1] = 1 + strlen(str) + 1;
650 tag[2] = data[1];
651 memcpy(tag + 3, str, strlen(str) + 1);
652 data += 2;
653 len -= 2;
654 }
655 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
656 return nmsg;
657}
658
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200659static struct msgb *ipa_bts_id_ack(void)
660{
661 struct msgb *nmsg2;
662
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200663 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200664 if (!nmsg2)
665 return NULL;
666
667 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200668 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200669
670 return nmsg2;
671}
672
Harald Welte51de9ca2013-06-30 20:13:16 +0200673static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up)
674{
675 struct e1inp_line *line = link->line;
676
677 if (up)
678 return;
679
680 if (line->ops->sign_link_down)
681 line->ops->sign_link_down(line);
682}
683
Harald Welte783715b2014-08-17 18:24:51 +0200684/* handle incoming message to BTS, check if it is an IPA CCM, and if yes,
685 * handle it accordingly (PING/PONG/ID_REQ/ID_RESP/ID_ACK) */
686int ipaccess_bts_handle_ccm(struct ipa_client_conn *link,
687 struct ipaccess_unit *dev, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200688{
689 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200690 struct msgb *rmsg;
691 int ret = 0;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200692
693 /* special handling for IPA CCM. */
694 if (hh->proto == IPAC_PROTO_IPACCESS) {
695 uint8_t msg_type = *(msg->l2h);
696
697 /* ping, pong and acknowledgment cases. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200698 ret = ipa_ccm_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200699 if (ret < 0)
700 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200701
702 /* this is a request for identification from the BSC. */
703 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200704 uint8_t *data = msgb_l2(msg);
705 int len = msgb_l2len(msg);
Andreas Eversbergf422a752014-01-21 14:54:41 +0100706 int trx_nr = 0;
707
708 if (link->ofd->priv_nr >= E1INP_SIGN_RSL)
709 trx_nr = link->ofd->priv_nr - E1INP_SIGN_RSL;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200710
Max71393522018-01-08 15:42:00 +0100711 LOGP(DLINP, LOGL_NOTICE, "received ID get from %u/%u/%u\n",
712 dev->site_id, dev->bts_id, dev->trx_id);
Harald Weltee416e2e2017-05-26 13:11:59 +0200713 rmsg = ipa_bts_id_resp(dev, data + 1, len - 1, trx_nr);
Harald Welteb65f58f2014-08-20 22:04:11 +0200714 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200715 if (ret != rmsg->len) {
716 LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP "
717 "message. Reason: %s\n", strerror(errno));
718 goto err_rmsg;
719 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200720 msgb_free(rmsg);
721
722 /* send ID_ACK. */
723 rmsg = ipa_bts_id_ack();
Harald Welteb65f58f2014-08-20 22:04:11 +0200724 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200725 if (ret != rmsg->len) {
726 LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK "
727 "message. Reason: %s\n", strerror(errno));
728 goto err_rmsg;
729 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200730 msgb_free(rmsg);
Harald Welte783715b2014-08-17 18:24:51 +0200731 }
732 return 1;
733 }
Harald Welte6eddd472013-06-30 20:18:53 +0200734
Harald Welte783715b2014-08-17 18:24:51 +0200735 return 0;
736
737err_rmsg:
738 msgb_free(rmsg);
739err:
740 ipa_client_conn_close(link);
741 return -1;
742}
743
744static int ipaccess_bts_read_cb(struct ipa_client_conn *link, struct msgb *msg)
745{
746 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
747 struct e1inp_ts *e1i_ts = NULL;
748 struct e1inp_sign_link *sign_link;
749 int ret = 0;
750
751 /* special handling for IPA CCM. */
752 if (hh->proto == IPAC_PROTO_IPACCESS) {
753 uint8_t msg_type = *(msg->l2h);
754
755 /* this is a request for identification from the BSC. */
756 if (msg_type == IPAC_MSGT_ID_GET) {
757 if (!link->line->ops->sign_link_up) {
758 LOGP(DLINP, LOGL_ERROR,
759 "Unable to set signal link, "
760 "closing socket.\n");
761 ret = -EINVAL;
762 goto err;
763 }
764 }
765 }
766
767 /* core CCM handling */
768 ret = ipaccess_bts_handle_ccm(link, link->line->ops->cfg.ipa.dev, msg);
769 if (ret < 0)
770 goto err;
771
772 if (ret == 1 && hh->proto == IPAC_PROTO_IPACCESS) {
773 uint8_t msg_type = *(msg->l2h);
774
775 if (msg_type == IPAC_MSGT_ID_GET) {
Harald Welte6eddd472013-06-30 20:18:53 +0200776 sign_link = link->line->ops->sign_link_up(msg,
777 link->line,
778 link->ofd->priv_nr);
779 if (sign_link == NULL) {
780 LOGP(DLINP, LOGL_ERROR,
781 "Unable to set signal link, "
782 "closing socket.\n");
783 ret = -EINVAL;
784 goto err;
785 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200786 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200787 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200788 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200789 } else if (link->port == IPA_TCP_PORT_OML)
790 e1i_ts = &link->line->ts[0];
791 else if (link->port == IPA_TCP_PORT_RSL)
Andreas Eversbergf422a752014-01-21 14:54:41 +0100792 e1i_ts = &link->line->ts[link->ofd->priv_nr-1];
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200793
Pablo Neira Ayusob9487012013-07-05 14:38:43 +0200794 OSMO_ASSERT(e1i_ts != NULL);
795
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200796 /* look up for some existing signaling link. */
797 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
798 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200799 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200800 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200801 ret = -EIO;
802 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200803 }
804 msg->dst = sign_link;
805
806 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200807 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200808 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200809 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200810 ret = -ENOENT;
811 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200812 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200813 link->line->ops->sign_link(msg);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200814 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200815
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200816err:
Harald Welte783715b2014-08-17 18:24:51 +0200817 ipa_client_conn_close(link);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200818 msgb_free(msg);
819 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200820}
821
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200822struct ipaccess_line {
823 int line_already_initialized;
824};
825
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200826static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200827{
828 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200829 struct ipaccess_line *il;
830
831 if (!line->driver_data)
832 line->driver_data = talloc_zero(line, struct ipaccess_line);
833
834 if (!line->driver_data) {
835 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
836 return -ENOMEM;
837 }
838 il = line->driver_data;
839
840 /* We only initialize this line once. */
841 if (il->line_already_initialized)
842 return 0;
843
844 il->line_already_initialized = 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200845
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200846 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200847 case E1INP_LINE_R_BSC: {
848 struct ipa_server_link *oml_link, *rsl_link;
Max4c4a1c22016-12-30 15:10:47 +0100849 const char *ipa = e1inp_ipa_get_bind_addr();
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200850
Max4c4a1c22016-12-30 15:10:47 +0100851 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode on %s "
852 "with OML %u and RSL %u TCP ports\n", ipa,
853 IPA_TCP_PORT_OML, IPA_TCP_PORT_RSL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200854
Max4c4a1c22016-12-30 15:10:47 +0100855 oml_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100856 IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200857 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200858 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200859 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200860 "BSC link: %s\n", strerror(errno));
861 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200862 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200863 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200864 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200865 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200866 ipa_server_link_destroy(oml_link);
867 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200868 }
Max4c4a1c22016-12-30 15:10:47 +0100869 rsl_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100870 IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200871 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200872 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200873 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200874 "BSC link: %s\n", strerror(errno));
875 return -ENOMEM;
876 }
877 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200878 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200879 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200880 ipa_server_link_destroy(rsl_link);
881 return -EIO;
882 }
883 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200884 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200885 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200886 case E1INP_LINE_R_BTS: {
Harald Welte84f67b22013-06-30 13:13:59 +0200887 struct ipa_client_conn *link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200888
Max4c4a1c22016-12-30 15:10:47 +0100889 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode, "
890 "OML connecting to %s:%u\n", line->ops->cfg.ipa.addr,
891 IPA_TCP_PORT_OML);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200892
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200893 link = ipa_client_conn_create(tall_ipa_ctx,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200894 &line->ts[E1INP_SIGN_OML-1],
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +0200895 E1INP_SIGN_OML,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200896 line->ops->cfg.ipa.addr,
897 IPA_TCP_PORT_OML,
Harald Welte51de9ca2013-06-30 20:13:16 +0200898 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +0200899 ipaccess_bts_read_cb,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200900 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +0200901 line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200902 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200903 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200904 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200905 return -ENOMEM;
906 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200907 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200908 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200909 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200910 ipa_client_conn_close(link);
911 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200912 return -EIO;
913 }
914 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200915 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200916 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200917 default:
918 break;
919 }
920 return ret;
921}
922
Andreas Eversbergf422a752014-01-21 14:54:41 +0100923
924/* backwards compatibility */
Harald Welte84f67b22013-06-30 13:13:59 +0200925int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line,
926 const char *rem_addr, uint16_t rem_port)
927{
Andreas Eversbergf422a752014-01-21 14:54:41 +0100928 return e1inp_ipa_bts_rsl_connect_n(line, rem_addr, rem_port, 0);
929}
930
931int e1inp_ipa_bts_rsl_connect_n(struct e1inp_line *line,
932 const char *rem_addr, uint16_t rem_port,
933 uint8_t trx_nr)
934{
Harald Welte84f67b22013-06-30 13:13:59 +0200935 struct ipa_client_conn *rsl_link;
936
Andreas Eversbergf422a752014-01-21 14:54:41 +0100937 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
938 LOGP(DLINP, LOGL_ERROR, "cannot create RSL BTS link: "
939 "trx_nr (%d) out of range\n", trx_nr);
940 return -EINVAL;
941 }
Andreas Eversbergf422a752014-01-21 14:54:41 +0100942
Harald Welte84f67b22013-06-30 13:13:59 +0200943 rsl_link = ipa_client_conn_create(tall_ipa_ctx,
Andreas Eversbergf422a752014-01-21 14:54:41 +0100944 &line->ts[E1INP_SIGN_RSL+trx_nr-1],
945 E1INP_SIGN_RSL+trx_nr,
Harald Welte84f67b22013-06-30 13:13:59 +0200946 rem_addr, rem_port,
Harald Welte51de9ca2013-06-30 20:13:16 +0200947 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +0200948 ipaccess_bts_read_cb,
Harald Welte84f67b22013-06-30 13:13:59 +0200949 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +0200950 line);
Harald Welte84f67b22013-06-30 13:13:59 +0200951 if (rsl_link == NULL) {
952 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
953 "BTS link: %s\n", strerror(errno));
954 return -ENOMEM;
955 }
956 if (ipa_client_conn_open(rsl_link) < 0) {
957 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
958 strerror(errno));
959 ipa_client_conn_close(rsl_link);
960 ipa_client_conn_destroy(rsl_link);
961 return -EIO;
962 }
963 return 0;
964}
965
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200966void e1inp_ipaccess_init(void)
967{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200968 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200969 e1inp_driver_register(&ipaccess_driver);
970}
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100971
972void e1inp_ipa_set_bind_addr(const char *ip_bind_addr)
973{
974 talloc_free((char*)ipaccess_driver.bind_addr);
975 ipaccess_driver.bind_addr = NULL;
976
977 if (ip_bind_addr)
978 ipaccess_driver.bind_addr = talloc_strdup(tall_ipa_ctx,
979 ip_bind_addr);
980}
981
982const char *e1inp_ipa_get_bind_addr(void)
983{
984 return ipaccess_driver.bind_addr?
985 ipaccess_driver.bind_addr
986 : "0.0.0.0";
987}