blob: ac84ffc907e40bab8ec7f9ba25566db4143cb9ad [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 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include "internal.h"
25
26#include <stdio.h>
27#include <unistd.h>
28#include <stdlib.h>
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +020029#include <stdbool.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020030#include <errno.h>
Daniel Willmann85980722014-01-09 14:30:55 +010031#include <netinet/tcp.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020032#include <string.h>
33#include <time.h>
Holger Hans Peter Freyther25474582017-01-23 19:49:07 +010034#include <fcntl.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020035#include <sys/socket.h>
36#include <sys/ioctl.h>
37#include <arpa/inet.h>
38
39#include <osmocom/core/select.h>
40#include <osmocom/gsm/tlv.h>
41#include <osmocom/core/msgb.h>
Harald Weltef5efba42014-08-18 17:30:36 +020042#include <osmocom/core/macaddr.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020043#include <osmocom/core/logging.h>
Harald Welte71d87b22011-07-18 14:49:56 +020044#include <osmocom/core/talloc.h>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020045#include <osmocom/abis/e1_input.h>
46#include <osmocom/abis/ipaccess.h>
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +020047#include <osmocom/core/socket.h>
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020048#include <osmocom/abis/ipa.h>
Pablo Neira Ayusoef132692013-07-08 01:17:27 +020049#include <osmocom/core/backtrace.h>
Harald Welteb65f58f2014-08-20 22:04:11 +020050#include <osmocom/gsm/ipa.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020051
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +020052static void *tall_ipa_ctx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020053
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020054#define TS1_ALLOC_SIZE 900
55
Daniel Willmann85980722014-01-09 14:30:55 +010056#define DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT 30
57#define DEFAULT_TCP_KEEPALIVE_INTERVAL 3
58#define DEFAULT_TCP_KEEPALIVE_RETRY_COUNT 10
59
Harald Welte10b41302013-06-30 14:05:49 +020060static int ipaccess_drop(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020061{
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +020062 int ret = 1;
Jacob Erlbeck98af3c32014-03-31 10:53:32 +020063 unsigned int ts_nr = bfd->priv_nr;
64 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020065
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020066 /* Error case: we did not see any ID_RESP yet for this socket. */
67 if (bfd->fd != -1) {
Harald Weltecc2241b2011-07-19 16:06:06 +020068 LOGP(DLINP, LOGL_ERROR, "Forcing socket shutdown with "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020069 "no signal link set\n");
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +020070 osmo_fd_unregister(bfd);
71 close(bfd->fd);
72 bfd->fd = -1;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020073 ret = -ENOENT;
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +020074 }
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +020075
Jacob Erlbeck98af3c32014-03-31 10:53:32 +020076 msgb_free(e1i_ts->pending_msg);
77 e1i_ts->pending_msg = NULL;
78
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +020079 /* e1inp_sign_link_destroy releases the socket descriptors for us. */
80 line->ops->sign_link_down(line);
81
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +020082 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020083}
84
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +020085static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
86 struct osmo_fd *bfd)
87{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020088 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +020089 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020090 struct ipaccess_unit unit_data = {};
91 struct e1inp_sign_link *sign_link;
92 char *unitid;
93 int len, ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +020094
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +020095 /* Handle IPA PING, PONG and ID_ACK messages. */
Harald Welteb65f58f2014-08-20 22:04:11 +020096 ret = ipa_ccm_rcvmsg_base(msg, bfd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +020097 switch(ret) {
98 case -1:
99 /* error in IPA control message handling */
100 goto err;
101 case 1:
102 /* this is an IPA control message, skip further processing */
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200103 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200104 case 0:
105 /* this is not an IPA control message, continue */
106 break;
107 default:
108 LOGP(DLINP, LOGL_ERROR, "Unexpected return from "
Harald Welteb65f58f2014-08-20 22:04:11 +0200109 "ipa_ccm_rcvmsg_base "
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200110 "(ret=%d)\n", ret);
111 goto err;
112 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200113
114 switch (msg_type) {
115 case IPAC_MSGT_ID_RESP:
Harald Weltecc2241b2011-07-19 16:06:06 +0200116 DEBUGP(DLMI, "ID_RESP\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200117 /* parse tags, search for Unit ID */
Harald Welteb65f58f2014-08-20 22:04:11 +0200118 ret = ipa_ccm_idtag_parse(&tlvp, (uint8_t *)msg->l2h + 2,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200119 msgb_l2len(msg)-2);
Harald Weltecc2241b2011-07-19 16:06:06 +0200120 DEBUGP(DLMI, "\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200121 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200122 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200123 "with malformed TLVs\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200124 ret = -EINVAL;
125 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200126 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200127 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200128 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200129 "without unit ID\n");
130 ret = -EINVAL;
131 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200132
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200133 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200134 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200135 if (len < 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200136 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200137 "with too small unit ID\n");
138 ret = -EINVAL;
139 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 ret = -EINVAL;
149 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200150 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200151 /* the BSC creates the new sign links at this stage. */
152 if (bfd->priv_nr == E1INP_SIGN_OML) {
153 sign_link =
154 line->ops->sign_link_up(&unit_data, line,
155 E1INP_SIGN_OML);
156 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200157 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200158 "Unable to set signal link, "
159 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200160 ret = -EINVAL;
161 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200162 }
163 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200164 struct e1inp_ts *ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200165 struct osmo_fd *newbfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200166 struct e1inp_line *new_line;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200167
168 sign_link =
169 line->ops->sign_link_up(&unit_data, line,
170 E1INP_SIGN_RSL);
171 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200172 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200173 "Unable to set signal link, "
174 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200175 ret = -EINVAL;
176 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200177 }
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200178 /* this is a bugtrap, the BSC should be using the
179 * virtual E1 line used by OML for this RSL link. */
180 if (sign_link->ts->line == line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200181 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200182 "Fix your BSC, you should use the "
183 "E1 line used by the OML link for "
184 "your RSL link.\n");
185 return 0;
186 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200187 /* Finally, we know which OML link is associated with
188 * this RSL link, attach it to this socket. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200189 bfd->data = new_line = sign_link->ts->line;
190 e1inp_line_get(new_line);
191 ts = &new_line->ts[E1INP_SIGN_RSL+unit_data.trx_id-1];
192 newbfd = &ts->driver.ipaccess.fd;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200193
194 /* get rid of our old temporary bfd */
195 memcpy(newbfd, bfd, sizeof(*newbfd));
196 newbfd->priv_nr = E1INP_SIGN_RSL + unit_data.trx_id;
197 osmo_fd_unregister(bfd);
198 bfd->fd = -1;
Harald Welteae3a9932016-11-26 09:25:23 +0100199 ret = osmo_fd_register(newbfd);
200 if (ret < 0) {
201 LOGP(DLINP, LOGL_ERROR,
202 "could not register FD\n");
203 goto err;
204 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200205 /* now we can release the dummy RSL line. */
206 e1inp_line_put(line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200207 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200208 break;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200209 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200210 LOGP(DLINP, LOGL_ERROR, "Unknown IPA message type\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200211 ret = -EINVAL;
212 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200213 }
214 return 0;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200215err:
216 osmo_fd_unregister(bfd);
217 close(bfd->fd);
218 bfd->fd = -1;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200219 e1inp_line_put(line);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200220 return ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200221}
222
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200223static int handle_ts1_read(struct osmo_fd *bfd)
224{
225 struct e1inp_line *line = bfd->data;
226 unsigned int ts_nr = bfd->priv_nr;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200227 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200228 struct e1inp_sign_link *link;
229 struct ipaccess_head *hh;
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200230 struct msgb *msg = NULL;
Maxc9fa25e2017-01-09 13:23:15 +0100231 int ret, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200232
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200233 ret = ipa_msg_recv_buffered(bfd->fd, &msg, &e1i_ts->pending_msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200234 if (ret < 0) {
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200235 if (ret == -EAGAIN)
236 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200237 LOGP(DLINP, LOGL_NOTICE, "Sign link problems, "
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200238 "closing socket. Reason: %s\n", strerror(-ret));
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200239 goto err;
240 } else if (ret == 0) {
241 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
242 goto err;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200243 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200244 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200245
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200246 hh = (struct ipaccess_head *) msg->data;
247 if (hh->proto == IPAC_PROTO_IPACCESS) {
248 ipaccess_rcvmsg(line, msg, bfd);
249 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200250 return 0;
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100251 } else if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
252 /* this sign link is not know yet.. complain. */
253 LOGP(DLINP, LOGL_ERROR, "Timeslot is not configured.\n");
254 ret = -EINVAL;
255 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200256 }
257 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
258 * might have free'd it !!! */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200259
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200260 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
261 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200262 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200263 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200264 ret = -EINVAL;
265 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200266 }
267 msg->dst = link;
268
269 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200270 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200271 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200272 "no action set for signalling messages.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200273 ret = -EINVAL;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200274 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200275 }
Maxc9fa25e2017-01-09 13:23:15 +0100276 rc = e1i_ts->line->ops->sign_link(msg);
277 if (rc < 0) {
Pablo Neira Ayusoa49c24d2012-10-16 11:24:08 +0200278 /* Don't close the signalling link if the upper layers report
279 * an error, that's too strict. BTW, the signalling layer is
280 * resposible for releasing the message.
281 */
Harald Weltecc2241b2011-07-19 16:06:06 +0200282 LOGP(DLINP, LOGL_ERROR, "Bad signalling message,"
Maxc9fa25e2017-01-09 13:23:15 +0100283 " sign_link returned error: %s.\n", strerror(-rc));
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200284 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200285
286 return 0;
287err_msg:
288 msgb_free(msg);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200289err:
Harald Welte10b41302013-06-30 14:05:49 +0200290 ipaccess_drop(bfd, line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200291 return ret;
292}
293
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200294static int ts_want_write(struct e1inp_ts *e1i_ts)
295{
296 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
297
298 return 0;
299}
300
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200301static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200302{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200303 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200304 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100305 return e1inp_close_socket(e1i_ts, sign_link, bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200306}
307
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200308static void timeout_ts1_write(void *data)
309{
310 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
311
312 /* trigger write of ts1, due to tx delay timer */
313 ts_want_write(e1i_ts);
314}
315
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200316static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200317{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200318 unsigned int ts_nr = bfd->priv_nr;
319 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
320 struct e1inp_sign_link *sign_link;
321 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200322 int ret;
323
324 bfd->when &= ~BSC_FD_WRITE;
325
326 /* get the next msg for this timeslot */
327 msg = e1inp_tx_ts(e1i_ts, &sign_link);
328 if (!msg) {
329 /* no message after tx delay timer */
330 return 0;
331 }
332
333 switch (sign_link->type) {
334 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200335 case E1INP_SIGN_RSL:
Harald Welte46fc7e22014-08-18 19:04:26 +0200336 case E1INP_SIGN_OSMO:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200337 break;
338 default:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200339 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200340 ret = -EINVAL;
341 goto out;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200342 }
343
344 msg->l2h = msg->data;
Harald Welteb65f58f2014-08-20 22:04:11 +0200345 ipa_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200346
Harald Weltecc2241b2011-07-19 16:06:06 +0200347 DEBUGP(DLMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200348
349 ret = send(bfd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200350 if (ret != msg->len) {
351 LOGP(DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling "
352 "message. Reason: %s\n", strerror(errno));
353 goto err;
354 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200355
356 /* set tx delay timer for next event */
357 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
358 e1i_ts->sign.tx_timer.data = e1i_ts;
359
360 /* Reducing this might break the nanoBTS 900 init. */
361 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
362
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200363out:
364 msgb_free(msg);
365 return ret;
366err:
Harald Welte10b41302013-06-30 14:05:49 +0200367 ipaccess_drop(bfd, line);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200368 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200369 return ret;
370}
371
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200372static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200373{
374 struct e1inp_line *line = bfd->data;
375
376 return __handle_ts1_write(bfd, line);
377}
378
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200379static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200380{
381 struct e1inp_line *line = link->line;
382
383 return __handle_ts1_write(link->ofd, line);
384}
385
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200386/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200387int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200388{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200389 int rc = 0;
390
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200391 if (what & BSC_FD_READ)
392 rc = handle_ts1_read(bfd);
393 if (what & BSC_FD_WRITE)
394 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200395
396 return rc;
397}
398
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200399static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200400
401struct e1inp_driver ipaccess_driver = {
402 .name = "ipa",
403 .want_write = ts_want_write,
404 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200405 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200406 .default_delay = 0,
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100407 .has_keepalive = 1,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200408};
409
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100410static void update_fd_settings(struct e1inp_line *line, int fd)
411{
412 int ret;
413 int val;
414
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100415 if (line->keepalive_num_probes) {
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100416 /* Enable TCP keepalive to find out if the connection is gone */
417 val = 1;
418 ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
419 if (ret < 0)
420 LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive: %s\n",
421 strerror(errno));
422 else
423 LOGP(DLINP, LOGL_NOTICE, "Keepalive is set: %i\n", ret);
424
425#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
426 /* The following options are not portable! */
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100427 val = line->keepalive_idle_timeout > 0 ?
428 line->keepalive_idle_timeout :
429 DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100430 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
431 &val, sizeof(val));
432 if (ret < 0)
433 LOGP(DLINP, LOGL_NOTICE,
434 "Failed to set keepalive idle time: %s\n",
435 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100436 val = line->keepalive_probe_interval > -1 ?
437 line->keepalive_probe_interval :
438 DEFAULT_TCP_KEEPALIVE_INTERVAL;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100439 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
440 &val, sizeof(val));
441 if (ret < 0)
442 LOGP(DLINP, LOGL_NOTICE,
443 "Failed to set keepalive interval: %s\n",
444 strerror(errno));
Jacob Erlbeck8fe15712014-01-09 14:30:58 +0100445 val = line->keepalive_num_probes > 0 ?
446 line->keepalive_num_probes :
447 DEFAULT_TCP_KEEPALIVE_RETRY_COUNT;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100448 ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
449 &val, sizeof(val));
450 if (ret < 0)
451 LOGP(DLINP, LOGL_NOTICE,
452 "Failed to set keepalive count: %s\n",
453 strerror(errno));
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100454#endif
Holger Hans Peter Freytherf465a4c2014-02-03 09:34:02 +0100455 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100456}
457
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200458/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200459static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200460{
461 int ret;
462 int idx = 0;
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100463 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200464 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200465 struct e1inp_ts *e1i_ts;
466 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200467
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200468 /* clone virtual E1 line for this new OML link. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200469 line = e1inp_line_clone(tall_ipa_ctx, link->line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200470 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200471 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200472 return -ENOMEM;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200473 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200474
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200475 /* create virrtual E1 timeslots for signalling */
Pablo Neira Ayuso59301852011-06-27 15:44:23 +0200476 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200477
478 /* initialize the fds */
479 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
480 line->ts[i].driver.ipaccess.fd.fd = -1;
481
482 e1i_ts = &line->ts[idx];
483
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200484 bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200485 bfd->fd = fd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200486 bfd->data = line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200487 bfd->priv_nr = E1INP_SIGN_OML;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200488 bfd->cb = ipaccess_fd_cb;
489 bfd->when = BSC_FD_READ;
490 ret = osmo_fd_register(bfd);
491 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200492 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200493 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200494 }
495
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100496 update_fd_settings(line, bfd->fd);
Daniel Willmann85980722014-01-09 14:30:55 +0100497
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200498 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200499 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200500 if (ret < 0) {
501 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
502 strerror(errno));
503 goto err_socket;
504 }
505 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200506
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200507err_socket:
508 osmo_fd_unregister(bfd);
509err_line:
510 close(bfd->fd);
511 bfd->fd = -1;
512 e1inp_line_put(line);
513 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200514}
515
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200516static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200517{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200518 struct e1inp_line *line;
519 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200520 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200521 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200522
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200523 /* We don't know yet which OML link to associate it with. Thus, we
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200524 * allocate a temporary E1 line until we have received ID. */
525 line = e1inp_line_clone(tall_ipa_ctx, link->line);
526 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200527 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200528 return -ENOMEM;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200529 }
530 /* initialize the fds */
531 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
532 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200533
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200534 /* we need this to initialize this in case to avoid crashes in case
535 * that the socket is closed before we've seen an ID_RESP. */
536 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line);
537
538 e1i_ts = &line->ts[E1INP_SIGN_RSL-1];
539
540 bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200541 bfd->fd = fd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200542 bfd->data = line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200543 bfd->priv_nr = E1INP_SIGN_RSL;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200544 bfd->cb = ipaccess_fd_cb;
545 bfd->when = BSC_FD_READ;
546 ret = osmo_fd_register(bfd);
547 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200548 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200549 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200550 }
551 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welteb65f58f2014-08-20 22:04:11 +0200552 ret = ipa_ccm_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200553 if (ret < 0) {
554 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
555 strerror(errno));
556 goto err_socket;
557 }
Jacob Erlbecka4ec51e2014-01-09 14:30:56 +0100558 update_fd_settings(line, bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200559 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200560
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200561err_socket:
562 osmo_fd_unregister(bfd);
563err_line:
564 close(bfd->fd);
565 bfd->fd = -1;
566 e1inp_line_put(line);
567 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200568}
569
Harald Weltee416e2e2017-05-26 13:11:59 +0200570#define IPA_STRING_MAX 64
571
572static struct msgb *
573ipa_bts_id_resp(struct ipaccess_unit *dev, uint8_t *data, int len, int trx_nr)
574{
575 struct msgb *nmsg;
576 char str[IPA_STRING_MAX];
577 uint8_t *tag;
578
579 memset(str, 0, sizeof(str));
580
581 nmsg = ipa_msg_alloc(0);
582 if (!nmsg)
583 return NULL;
584
585 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
586 while (len) {
587 if (len < 2) {
588 LOGP(DLINP, LOGL_NOTICE,
589 "Short read of ipaccess tag\n");
590 msgb_free(nmsg);
591 return NULL;
592 }
593 switch (data[1]) {
594 case IPAC_IDTAG_UNIT:
595 snprintf(str, sizeof(str), "%u/%u/%u",
596 dev->site_id, dev->bts_id, trx_nr);
597 break;
598 case IPAC_IDTAG_MACADDR:
599 snprintf(str, sizeof(str),
600 "%02x:%02x:%02x:%02x:%02x:%02x",
601 dev->mac_addr[0], dev->mac_addr[1],
602 dev->mac_addr[2], dev->mac_addr[3],
603 dev->mac_addr[4], dev->mac_addr[5]);
604 break;
605 case IPAC_IDTAG_LOCATION1:
606 if (dev->location1)
607 strncpy(str, dev->location1, IPA_STRING_MAX);
608 break;
609 case IPAC_IDTAG_LOCATION2:
610 if (dev->location2)
611 strncpy(str, dev->location2, IPA_STRING_MAX);
612 break;
613 case IPAC_IDTAG_EQUIPVERS:
614 if (dev->equipvers)
615 strncpy(str, dev->equipvers, IPA_STRING_MAX);
616 break;
617 case IPAC_IDTAG_SWVERSION:
618 if (dev->swversion)
619 strncpy(str, dev->swversion, IPA_STRING_MAX);
620 break;
621 case IPAC_IDTAG_UNITNAME:
622 snprintf(str, sizeof(str),
623 "%s-%02x-%02x-%02x-%02x-%02x-%02x",
624 dev->unit_name,
625 dev->mac_addr[0], dev->mac_addr[1],
626 dev->mac_addr[2], dev->mac_addr[3],
627 dev->mac_addr[4], dev->mac_addr[5]);
628 break;
629 case IPAC_IDTAG_SERNR:
630 if (dev->serno)
631 strncpy(str, dev->serno, IPA_STRING_MAX);
632 break;
633 default:
634 LOGP(DLINP, LOGL_NOTICE,
635 "Unknown ipaccess tag 0x%02x\n", *data);
636 msgb_free(nmsg);
637 return NULL;
638 }
639 str[IPA_STRING_MAX-1] = '\0';
640
641 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
642 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
643 tag[0] = 0x00;
644 tag[1] = 1 + strlen(str) + 1;
645 tag[2] = data[1];
646 memcpy(tag + 3, str, strlen(str) + 1);
647 data += 2;
648 len -= 2;
649 }
650 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
651 return nmsg;
652}
653
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200654static struct msgb *ipa_bts_id_ack(void)
655{
656 struct msgb *nmsg2;
657
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200658 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200659 if (!nmsg2)
660 return NULL;
661
662 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200663 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200664
665 return nmsg2;
666}
667
Harald Welte51de9ca2013-06-30 20:13:16 +0200668static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up)
669{
670 struct e1inp_line *line = link->line;
671
672 if (up)
673 return;
674
675 if (line->ops->sign_link_down)
676 line->ops->sign_link_down(line);
677}
678
Harald Welte783715b2014-08-17 18:24:51 +0200679/* handle incoming message to BTS, check if it is an IPA CCM, and if yes,
680 * handle it accordingly (PING/PONG/ID_REQ/ID_RESP/ID_ACK) */
681int ipaccess_bts_handle_ccm(struct ipa_client_conn *link,
682 struct ipaccess_unit *dev, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200683{
684 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200685 struct msgb *rmsg;
686 int ret = 0;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200687
688 /* special handling for IPA CCM. */
689 if (hh->proto == IPAC_PROTO_IPACCESS) {
690 uint8_t msg_type = *(msg->l2h);
691
692 /* ping, pong and acknowledgment cases. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200693 ret = ipa_ccm_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200694 if (ret < 0)
695 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200696
697 /* this is a request for identification from the BSC. */
698 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200699 uint8_t *data = msgb_l2(msg);
700 int len = msgb_l2len(msg);
Andreas Eversbergf422a752014-01-21 14:54:41 +0100701 int trx_nr = 0;
702
703 if (link->ofd->priv_nr >= E1INP_SIGN_RSL)
704 trx_nr = link->ofd->priv_nr - E1INP_SIGN_RSL;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200705
Harald Weltecc2241b2011-07-19 16:06:06 +0200706 LOGP(DLINP, LOGL_NOTICE, "received ID get\n");
Harald Weltee416e2e2017-05-26 13:11:59 +0200707 rmsg = ipa_bts_id_resp(dev, data + 1, len - 1, trx_nr);
Harald Welteb65f58f2014-08-20 22:04:11 +0200708 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200709 if (ret != rmsg->len) {
710 LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP "
711 "message. Reason: %s\n", strerror(errno));
712 goto err_rmsg;
713 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200714 msgb_free(rmsg);
715
716 /* send ID_ACK. */
717 rmsg = ipa_bts_id_ack();
Harald Welteb65f58f2014-08-20 22:04:11 +0200718 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200719 if (ret != rmsg->len) {
720 LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK "
721 "message. Reason: %s\n", strerror(errno));
722 goto err_rmsg;
723 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200724 msgb_free(rmsg);
Harald Welte783715b2014-08-17 18:24:51 +0200725 }
726 return 1;
727 }
Harald Welte6eddd472013-06-30 20:18:53 +0200728
Harald Welte783715b2014-08-17 18:24:51 +0200729 return 0;
730
731err_rmsg:
732 msgb_free(rmsg);
733err:
734 ipa_client_conn_close(link);
735 return -1;
736}
737
738static int ipaccess_bts_read_cb(struct ipa_client_conn *link, struct msgb *msg)
739{
740 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
741 struct e1inp_ts *e1i_ts = NULL;
742 struct e1inp_sign_link *sign_link;
743 int ret = 0;
744
745 /* special handling for IPA CCM. */
746 if (hh->proto == IPAC_PROTO_IPACCESS) {
747 uint8_t msg_type = *(msg->l2h);
748
749 /* this is a request for identification from the BSC. */
750 if (msg_type == IPAC_MSGT_ID_GET) {
751 if (!link->line->ops->sign_link_up) {
752 LOGP(DLINP, LOGL_ERROR,
753 "Unable to set signal link, "
754 "closing socket.\n");
755 ret = -EINVAL;
756 goto err;
757 }
758 }
759 }
760
761 /* core CCM handling */
762 ret = ipaccess_bts_handle_ccm(link, link->line->ops->cfg.ipa.dev, msg);
763 if (ret < 0)
764 goto err;
765
766 if (ret == 1 && hh->proto == IPAC_PROTO_IPACCESS) {
767 uint8_t msg_type = *(msg->l2h);
768
769 if (msg_type == IPAC_MSGT_ID_GET) {
Harald Welte6eddd472013-06-30 20:18:53 +0200770 sign_link = link->line->ops->sign_link_up(msg,
771 link->line,
772 link->ofd->priv_nr);
773 if (sign_link == NULL) {
774 LOGP(DLINP, LOGL_ERROR,
775 "Unable to set signal link, "
776 "closing socket.\n");
777 ret = -EINVAL;
778 goto err;
779 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200780 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200781 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200782 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200783 } else if (link->port == IPA_TCP_PORT_OML)
784 e1i_ts = &link->line->ts[0];
785 else if (link->port == IPA_TCP_PORT_RSL)
Andreas Eversbergf422a752014-01-21 14:54:41 +0100786 e1i_ts = &link->line->ts[link->ofd->priv_nr-1];
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200787
Pablo Neira Ayusob9487012013-07-05 14:38:43 +0200788 OSMO_ASSERT(e1i_ts != NULL);
789
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200790 /* look up for some existing signaling link. */
791 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
792 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200793 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200794 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200795 ret = -EIO;
796 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200797 }
798 msg->dst = sign_link;
799
800 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200801 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200802 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200803 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200804 ret = -ENOENT;
805 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200806 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200807 link->line->ops->sign_link(msg);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200808 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200809
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200810err:
Harald Welte783715b2014-08-17 18:24:51 +0200811 ipa_client_conn_close(link);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200812 msgb_free(msg);
813 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200814}
815
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200816struct ipaccess_line {
817 int line_already_initialized;
818};
819
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200820static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200821{
822 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200823 struct ipaccess_line *il;
824
825 if (!line->driver_data)
826 line->driver_data = talloc_zero(line, struct ipaccess_line);
827
828 if (!line->driver_data) {
829 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
830 return -ENOMEM;
831 }
832 il = line->driver_data;
833
834 /* We only initialize this line once. */
835 if (il->line_already_initialized)
836 return 0;
837
838 il->line_already_initialized = 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200839
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200840 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200841 case E1INP_LINE_R_BSC: {
842 struct ipa_server_link *oml_link, *rsl_link;
Max4c4a1c22016-12-30 15:10:47 +0100843 const char *ipa = e1inp_ipa_get_bind_addr();
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200844
Max4c4a1c22016-12-30 15:10:47 +0100845 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode on %s "
846 "with OML %u and RSL %u TCP ports\n", ipa,
847 IPA_TCP_PORT_OML, IPA_TCP_PORT_RSL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200848
Max4c4a1c22016-12-30 15:10:47 +0100849 oml_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100850 IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200851 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200852 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200853 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200854 "BSC link: %s\n", strerror(errno));
855 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200856 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200857 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200858 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200859 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200860 ipa_server_link_destroy(oml_link);
861 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200862 }
Max4c4a1c22016-12-30 15:10:47 +0100863 rsl_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100864 IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200865 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200866 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200867 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200868 "BSC link: %s\n", strerror(errno));
869 return -ENOMEM;
870 }
871 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200872 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200873 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200874 ipa_server_link_destroy(rsl_link);
875 return -EIO;
876 }
877 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200878 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200879 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200880 case E1INP_LINE_R_BTS: {
Harald Welte84f67b22013-06-30 13:13:59 +0200881 struct ipa_client_conn *link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200882
Max4c4a1c22016-12-30 15:10:47 +0100883 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode, "
884 "OML connecting to %s:%u\n", line->ops->cfg.ipa.addr,
885 IPA_TCP_PORT_OML);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200886
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200887 link = ipa_client_conn_create(tall_ipa_ctx,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200888 &line->ts[E1INP_SIGN_OML-1],
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +0200889 E1INP_SIGN_OML,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200890 line->ops->cfg.ipa.addr,
891 IPA_TCP_PORT_OML,
Harald Welte51de9ca2013-06-30 20:13:16 +0200892 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +0200893 ipaccess_bts_read_cb,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200894 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +0200895 line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200896 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200897 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200898 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200899 return -ENOMEM;
900 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200901 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200902 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200903 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200904 ipa_client_conn_close(link);
905 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200906 return -EIO;
907 }
908 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200909 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200910 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200911 default:
912 break;
913 }
914 return ret;
915}
916
Andreas Eversbergf422a752014-01-21 14:54:41 +0100917
918/* backwards compatibility */
Harald Welte84f67b22013-06-30 13:13:59 +0200919int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line,
920 const char *rem_addr, uint16_t rem_port)
921{
Andreas Eversbergf422a752014-01-21 14:54:41 +0100922 return e1inp_ipa_bts_rsl_connect_n(line, rem_addr, rem_port, 0);
923}
924
925int e1inp_ipa_bts_rsl_connect_n(struct e1inp_line *line,
926 const char *rem_addr, uint16_t rem_port,
927 uint8_t trx_nr)
928{
Harald Welte84f67b22013-06-30 13:13:59 +0200929 struct ipa_client_conn *rsl_link;
930
Andreas Eversbergf422a752014-01-21 14:54:41 +0100931 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
932 LOGP(DLINP, LOGL_ERROR, "cannot create RSL BTS link: "
933 "trx_nr (%d) out of range\n", trx_nr);
934 return -EINVAL;
935 }
Andreas Eversbergf422a752014-01-21 14:54:41 +0100936
Harald Welte84f67b22013-06-30 13:13:59 +0200937 rsl_link = ipa_client_conn_create(tall_ipa_ctx,
Andreas Eversbergf422a752014-01-21 14:54:41 +0100938 &line->ts[E1INP_SIGN_RSL+trx_nr-1],
939 E1INP_SIGN_RSL+trx_nr,
Harald Welte84f67b22013-06-30 13:13:59 +0200940 rem_addr, rem_port,
Harald Welte51de9ca2013-06-30 20:13:16 +0200941 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +0200942 ipaccess_bts_read_cb,
Harald Welte84f67b22013-06-30 13:13:59 +0200943 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +0200944 line);
Harald Welte84f67b22013-06-30 13:13:59 +0200945 if (rsl_link == NULL) {
946 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
947 "BTS link: %s\n", strerror(errno));
948 return -ENOMEM;
949 }
950 if (ipa_client_conn_open(rsl_link) < 0) {
951 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
952 strerror(errno));
953 ipa_client_conn_close(rsl_link);
954 ipa_client_conn_destroy(rsl_link);
955 return -EIO;
956 }
957 return 0;
958}
959
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200960void e1inp_ipaccess_init(void)
961{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200962 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200963 e1inp_driver_register(&ipaccess_driver);
964}
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100965
966void e1inp_ipa_set_bind_addr(const char *ip_bind_addr)
967{
968 talloc_free((char*)ipaccess_driver.bind_addr);
969 ipaccess_driver.bind_addr = NULL;
970
971 if (ip_bind_addr)
972 ipaccess_driver.bind_addr = talloc_strdup(tall_ipa_ctx,
973 ip_bind_addr);
974}
975
976const char *e1inp_ipa_get_bind_addr(void)
977{
978 return ipaccess_driver.bind_addr?
979 ipaccess_driver.bind_addr
980 : "0.0.0.0";
981}