blob: fb2e30d487837b3ea81eac3f42e049d7fba3c879 [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
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200570static struct msgb *ipa_bts_id_ack(void)
571{
572 struct msgb *nmsg2;
573
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200574 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200575 if (!nmsg2)
576 return NULL;
577
578 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200579 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200580
581 return nmsg2;
582}
583
Harald Welte51de9ca2013-06-30 20:13:16 +0200584static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up)
585{
586 struct e1inp_line *line = link->line;
587
588 if (up)
589 return;
590
591 if (line->ops->sign_link_down)
592 line->ops->sign_link_down(line);
593}
594
Harald Welte783715b2014-08-17 18:24:51 +0200595/* handle incoming message to BTS, check if it is an IPA CCM, and if yes,
596 * handle it accordingly (PING/PONG/ID_REQ/ID_RESP/ID_ACK) */
597int ipaccess_bts_handle_ccm(struct ipa_client_conn *link,
598 struct ipaccess_unit *dev, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200599{
600 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200601 struct msgb *rmsg;
602 int ret = 0;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200603
604 /* special handling for IPA CCM. */
605 if (hh->proto == IPAC_PROTO_IPACCESS) {
606 uint8_t msg_type = *(msg->l2h);
607
608 /* ping, pong and acknowledgment cases. */
Harald Welteb65f58f2014-08-20 22:04:11 +0200609 ret = ipa_ccm_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200610 if (ret < 0)
611 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200612
613 /* this is a request for identification from the BSC. */
614 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200615 uint8_t *data = msgb_l2(msg);
616 int len = msgb_l2len(msg);
Harald Welted517db02017-04-15 19:12:01 +0200617 int old_trx_nr = dev->trx_id;
Andreas Eversbergf422a752014-01-21 14:54:41 +0100618 int trx_nr = 0;
619
620 if (link->ofd->priv_nr >= E1INP_SIGN_RSL)
621 trx_nr = link->ofd->priv_nr - E1INP_SIGN_RSL;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200622
Harald Weltecc2241b2011-07-19 16:06:06 +0200623 LOGP(DLINP, LOGL_NOTICE, "received ID get\n");
Harald Welted517db02017-04-15 19:12:01 +0200624 dev->trx_id = trx_nr;
625 rmsg = ipa_ccm_make_id_resp_from_req(dev, data + 1, len - 1);
626 dev->trx_id = old_trx_nr;
Harald Welteb65f58f2014-08-20 22:04:11 +0200627 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200628 if (ret != rmsg->len) {
629 LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP "
630 "message. Reason: %s\n", strerror(errno));
631 goto err_rmsg;
632 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200633 msgb_free(rmsg);
634
635 /* send ID_ACK. */
636 rmsg = ipa_bts_id_ack();
Harald Welteb65f58f2014-08-20 22:04:11 +0200637 ret = ipa_send(link->ofd->fd, rmsg->data, rmsg->len);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200638 if (ret != rmsg->len) {
639 LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK "
640 "message. Reason: %s\n", strerror(errno));
641 goto err_rmsg;
642 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200643 msgb_free(rmsg);
Harald Welte783715b2014-08-17 18:24:51 +0200644 }
645 return 1;
646 }
Harald Welte6eddd472013-06-30 20:18:53 +0200647
Harald Welte783715b2014-08-17 18:24:51 +0200648 return 0;
649
650err_rmsg:
651 msgb_free(rmsg);
652err:
653 ipa_client_conn_close(link);
654 return -1;
655}
656
657static int ipaccess_bts_read_cb(struct ipa_client_conn *link, struct msgb *msg)
658{
659 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
660 struct e1inp_ts *e1i_ts = NULL;
661 struct e1inp_sign_link *sign_link;
662 int ret = 0;
663
664 /* special handling for IPA CCM. */
665 if (hh->proto == IPAC_PROTO_IPACCESS) {
666 uint8_t msg_type = *(msg->l2h);
667
668 /* this is a request for identification from the BSC. */
669 if (msg_type == IPAC_MSGT_ID_GET) {
670 if (!link->line->ops->sign_link_up) {
671 LOGP(DLINP, LOGL_ERROR,
672 "Unable to set signal link, "
673 "closing socket.\n");
674 ret = -EINVAL;
675 goto err;
676 }
677 }
678 }
679
680 /* core CCM handling */
681 ret = ipaccess_bts_handle_ccm(link, link->line->ops->cfg.ipa.dev, msg);
682 if (ret < 0)
683 goto err;
684
685 if (ret == 1 && hh->proto == IPAC_PROTO_IPACCESS) {
686 uint8_t msg_type = *(msg->l2h);
687
688 if (msg_type == IPAC_MSGT_ID_GET) {
Harald Welte6eddd472013-06-30 20:18:53 +0200689 sign_link = link->line->ops->sign_link_up(msg,
690 link->line,
691 link->ofd->priv_nr);
692 if (sign_link == NULL) {
693 LOGP(DLINP, LOGL_ERROR,
694 "Unable to set signal link, "
695 "closing socket.\n");
696 ret = -EINVAL;
697 goto err;
698 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200699 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200700 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200701 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200702 } else if (link->port == IPA_TCP_PORT_OML)
703 e1i_ts = &link->line->ts[0];
704 else if (link->port == IPA_TCP_PORT_RSL)
Andreas Eversbergf422a752014-01-21 14:54:41 +0100705 e1i_ts = &link->line->ts[link->ofd->priv_nr-1];
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200706
Pablo Neira Ayusob9487012013-07-05 14:38:43 +0200707 OSMO_ASSERT(e1i_ts != NULL);
708
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200709 /* look up for some existing signaling link. */
710 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
711 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200712 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200713 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200714 ret = -EIO;
715 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200716 }
717 msg->dst = sign_link;
718
719 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200720 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200721 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200722 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200723 ret = -ENOENT;
724 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200725 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200726 link->line->ops->sign_link(msg);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200727 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200728
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200729err:
Harald Welte783715b2014-08-17 18:24:51 +0200730 ipa_client_conn_close(link);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200731 msgb_free(msg);
732 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200733}
734
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200735struct ipaccess_line {
736 int line_already_initialized;
737};
738
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200739static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200740{
741 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200742 struct ipaccess_line *il;
743
744 if (!line->driver_data)
745 line->driver_data = talloc_zero(line, struct ipaccess_line);
746
747 if (!line->driver_data) {
748 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
749 return -ENOMEM;
750 }
751 il = line->driver_data;
752
753 /* We only initialize this line once. */
754 if (il->line_already_initialized)
755 return 0;
756
757 il->line_already_initialized = 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200758
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200759 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200760 case E1INP_LINE_R_BSC: {
761 struct ipa_server_link *oml_link, *rsl_link;
Max4c4a1c22016-12-30 15:10:47 +0100762 const char *ipa = e1inp_ipa_get_bind_addr();
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200763
Max4c4a1c22016-12-30 15:10:47 +0100764 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode on %s "
765 "with OML %u and RSL %u TCP ports\n", ipa,
766 IPA_TCP_PORT_OML, IPA_TCP_PORT_RSL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200767
Max4c4a1c22016-12-30 15:10:47 +0100768 oml_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100769 IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200770 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200771 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200772 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200773 "BSC link: %s\n", strerror(errno));
774 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200775 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200776 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200777 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200778 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200779 ipa_server_link_destroy(oml_link);
780 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200781 }
Max4c4a1c22016-12-30 15:10:47 +0100782 rsl_link = ipa_server_link_create(tall_ipa_ctx, line, ipa,
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100783 IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200784 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200785 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200786 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200787 "BSC link: %s\n", strerror(errno));
788 return -ENOMEM;
789 }
790 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200791 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200792 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200793 ipa_server_link_destroy(rsl_link);
794 return -EIO;
795 }
796 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200797 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200798 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200799 case E1INP_LINE_R_BTS: {
Harald Welte84f67b22013-06-30 13:13:59 +0200800 struct ipa_client_conn *link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200801
Max4c4a1c22016-12-30 15:10:47 +0100802 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode, "
803 "OML connecting to %s:%u\n", line->ops->cfg.ipa.addr,
804 IPA_TCP_PORT_OML);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200805
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200806 link = ipa_client_conn_create(tall_ipa_ctx,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200807 &line->ts[E1INP_SIGN_OML-1],
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +0200808 E1INP_SIGN_OML,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200809 line->ops->cfg.ipa.addr,
810 IPA_TCP_PORT_OML,
Harald Welte51de9ca2013-06-30 20:13:16 +0200811 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +0200812 ipaccess_bts_read_cb,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200813 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +0200814 line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200815 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200816 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200817 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200818 return -ENOMEM;
819 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200820 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200821 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200822 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200823 ipa_client_conn_close(link);
824 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200825 return -EIO;
826 }
827 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200828 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200829 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200830 default:
831 break;
832 }
833 return ret;
834}
835
Andreas Eversbergf422a752014-01-21 14:54:41 +0100836
837/* backwards compatibility */
Harald Welte84f67b22013-06-30 13:13:59 +0200838int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line,
839 const char *rem_addr, uint16_t rem_port)
840{
Andreas Eversbergf422a752014-01-21 14:54:41 +0100841 return e1inp_ipa_bts_rsl_connect_n(line, rem_addr, rem_port, 0);
842}
843
844int e1inp_ipa_bts_rsl_connect_n(struct e1inp_line *line,
845 const char *rem_addr, uint16_t rem_port,
846 uint8_t trx_nr)
847{
Harald Welte84f67b22013-06-30 13:13:59 +0200848 struct ipa_client_conn *rsl_link;
849
Andreas Eversbergf422a752014-01-21 14:54:41 +0100850 if (E1INP_SIGN_RSL+trx_nr-1 >= NUM_E1_TS) {
851 LOGP(DLINP, LOGL_ERROR, "cannot create RSL BTS link: "
852 "trx_nr (%d) out of range\n", trx_nr);
853 return -EINVAL;
854 }
Andreas Eversbergf422a752014-01-21 14:54:41 +0100855
Harald Welte84f67b22013-06-30 13:13:59 +0200856 rsl_link = ipa_client_conn_create(tall_ipa_ctx,
Andreas Eversbergf422a752014-01-21 14:54:41 +0100857 &line->ts[E1INP_SIGN_RSL+trx_nr-1],
858 E1INP_SIGN_RSL+trx_nr,
Harald Welte84f67b22013-06-30 13:13:59 +0200859 rem_addr, rem_port,
Harald Welte51de9ca2013-06-30 20:13:16 +0200860 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +0200861 ipaccess_bts_read_cb,
Harald Welte84f67b22013-06-30 13:13:59 +0200862 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +0200863 line);
Harald Welte84f67b22013-06-30 13:13:59 +0200864 if (rsl_link == NULL) {
865 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
866 "BTS link: %s\n", strerror(errno));
867 return -ENOMEM;
868 }
869 if (ipa_client_conn_open(rsl_link) < 0) {
870 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
871 strerror(errno));
872 ipa_client_conn_close(rsl_link);
873 ipa_client_conn_destroy(rsl_link);
874 return -EIO;
875 }
876 return 0;
877}
878
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200879void e1inp_ipaccess_init(void)
880{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200881 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200882 e1inp_driver_register(&ipaccess_driver);
883}
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100884
885void e1inp_ipa_set_bind_addr(const char *ip_bind_addr)
886{
887 talloc_free((char*)ipaccess_driver.bind_addr);
888 ipaccess_driver.bind_addr = NULL;
889
890 if (ip_bind_addr)
891 ipaccess_driver.bind_addr = talloc_strdup(tall_ipa_ctx,
892 ip_bind_addr);
893}
894
895const char *e1inp_ipa_get_bind_addr(void)
896{
897 return ipaccess_driver.bind_addr?
898 ipaccess_driver.bind_addr
899 : "0.0.0.0";
900}