blob: 8f9865ea131fb5be42c09fe70c7675390f94bae2 [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>
34#include <sys/fcntl.h>
35#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>
42#include <osmocom/core/logging.h>
Harald Welte71d87b22011-07-18 14:49:56 +020043#include <osmocom/core/talloc.h>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020044#include <osmocom/abis/e1_input.h>
45#include <osmocom/abis/ipaccess.h>
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +020046#include <osmocom/core/socket.h>
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020047#include <osmocom/abis/ipa.h>
Pablo Neira Ayusoef132692013-07-08 01:17:27 +020048#include <osmocom/core/backtrace.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020049
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +020050static void *tall_ipa_ctx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020051
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020052#define TS1_ALLOC_SIZE 900
53
Daniel Willmann85980722014-01-09 14:30:55 +010054#define DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT 30
55#define DEFAULT_TCP_KEEPALIVE_INTERVAL 3
56#define DEFAULT_TCP_KEEPALIVE_RETRY_COUNT 10
57
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020058/*
59 * Common propietary IPA messages:
60 * - PONG: in reply to PING.
61 * - ID_REQUEST: first messages once OML has been established.
62 * - ID_ACK: in reply to ID_ACK.
63 */
64const uint8_t ipa_pong_msg[] = {
65 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG
66};
67
68const uint8_t ipa_id_ack_msg[] = {
69 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK
70};
71
72const uint8_t ipa_id_req_msg[] = {
73 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET,
74 0x01, IPAC_IDTAG_UNIT,
75 0x01, IPAC_IDTAG_MACADDR,
76 0x01, IPAC_IDTAG_LOCATION1,
77 0x01, IPAC_IDTAG_LOCATION2,
78 0x01, IPAC_IDTAG_EQUIPVERS,
79 0x01, IPAC_IDTAG_SWVERSION,
80 0x01, IPAC_IDTAG_UNITNAME,
81 0x01, IPAC_IDTAG_SERNR,
82};
83
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020084static const char *idtag_names[] = {
85 [IPAC_IDTAG_SERNR] = "Serial_Number",
86 [IPAC_IDTAG_UNITNAME] = "Unit_Name",
87 [IPAC_IDTAG_LOCATION1] = "Location_1",
88 [IPAC_IDTAG_LOCATION2] = "Location_2",
89 [IPAC_IDTAG_EQUIPVERS] = "Equipment_Version",
90 [IPAC_IDTAG_SWVERSION] = "Software_Version",
91 [IPAC_IDTAG_IPADDR] = "IP_Address",
92 [IPAC_IDTAG_MACADDR] = "MAC_Address",
93 [IPAC_IDTAG_UNIT] = "Unit_ID",
94};
95
96const char *ipaccess_idtag_name(uint8_t tag)
97{
98 if (tag >= ARRAY_SIZE(idtag_names))
99 return "unknown";
100
101 return idtag_names[tag];
102}
103
104int ipaccess_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len)
105{
106 uint8_t t_len;
107 uint8_t t_tag;
108 uint8_t *cur = buf;
109
110 memset(dec, 0, sizeof(*dec));
111
112 while (len >= 2) {
113 len -= 2;
114 t_len = *cur++;
115 t_tag = *cur++;
116
117 if (t_len > len + 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200118 LOGP(DLMI, LOGL_ERROR, "The tag does not fit: %d\n", t_len);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200119 return -EINVAL;
120 }
121
Harald Weltecc2241b2011-07-19 16:06:06 +0200122 DEBUGPC(DLMI, "%s='%s' ", ipaccess_idtag_name(t_tag), cur);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200123
124 dec->lv[t_tag].len = t_len;
125 dec->lv[t_tag].val = cur;
126
127 cur += t_len;
128 len -= t_len;
129 }
130 return 0;
131}
132
133int ipaccess_parse_unitid(const char *str, struct ipaccess_unit *unit_data)
134{
135 unsigned long ul;
136 char *endptr;
137 const char *nptr;
138
139 nptr = str;
140 ul = strtoul(nptr, &endptr, 10);
141 if (endptr <= nptr)
142 return -EINVAL;
Pablo Neira Ayuso62d345a2011-07-05 16:37:37 +0200143 unit_data->site_id = ul & 0xffff;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200144
145 if (*endptr++ != '/')
146 return -EINVAL;
147
148 nptr = endptr;
149 ul = strtoul(nptr, &endptr, 10);
150 if (endptr <= nptr)
151 return -EINVAL;
Pablo Neira Ayuso62d345a2011-07-05 16:37:37 +0200152 unit_data->bts_id = ul & 0xffff;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200153
154 if (*endptr++ != '/')
155 return -EINVAL;
156
157 nptr = endptr;
158 ul = strtoul(nptr, &endptr, 10);
159 if (endptr <= nptr)
160 return -EINVAL;
Pablo Neira Ayuso62d345a2011-07-05 16:37:37 +0200161 unit_data->trx_id = ul & 0xffff;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200162
163 return 0;
164}
165
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200166static int ipaccess_send(int fd, const void *msg, size_t msglen)
167{
168 int ret;
169
170 ret = write(fd, msg, msglen);
171 if (ret < 0)
172 return ret;
173 if (ret < msglen) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200174 LOGP(DLINP, LOGL_ERROR, "ipaccess_send: short write\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200175 return -EIO;
176 }
177 return ret;
178}
179
180int ipaccess_send_pong(int fd)
181{
182 return ipaccess_send(fd, ipa_pong_msg, sizeof(ipa_pong_msg));
183}
184
185int ipaccess_send_id_ack(int fd)
186{
187 return ipaccess_send(fd, ipa_id_ack_msg, sizeof(ipa_id_ack_msg));
188}
189
190int ipaccess_send_id_req(int fd)
191{
192 return ipaccess_send(fd, ipa_id_req_msg, sizeof(ipa_id_req_msg));
193}
194
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200195/* base handling of the ip.access protocol */
Harald Weltebc25bca2011-08-19 22:32:38 +0200196int ipaccess_rcvmsg_base(struct msgb *msg, struct osmo_fd *bfd)
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200197{
198 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200199 int ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200200
201 switch (msg_type) {
202 case IPAC_MSGT_PING:
203 ret = ipaccess_send_pong(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200204 if (ret < 0) {
205 LOGP(DLINP, LOGL_ERROR, "Cannot send PING "
206 "message. Reason: %s\n", strerror(errno));
207 break;
208 }
209 ret = 1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200210 break;
211 case IPAC_MSGT_PONG:
Harald Weltecc2241b2011-07-19 16:06:06 +0200212 DEBUGP(DLMI, "PONG!\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200213 ret = 1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200214 break;
215 case IPAC_MSGT_ID_ACK:
Harald Weltecc2241b2011-07-19 16:06:06 +0200216 DEBUGP(DLMI, "ID_ACK? -> ACK!\n");
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200217 ret = ipaccess_send_id_ack(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200218 if (ret < 0) {
219 LOGP(DLINP, LOGL_ERROR, "Cannot send ID_ACK "
220 "message. Reason: %s\n", strerror(errno));
221 break;
222 }
223 ret = 1;
224 break;
225 default:
226 /* This is not an IPA PING, PONG or ID_ACK message */
227 ret = 0;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200228 break;
229 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200230 return ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200231}
232
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200233/* base handling of the ip.access protocol */
234int ipaccess_rcvmsg_bts_base(struct msgb *msg,
235 struct osmo_fd *bfd)
236{
237 uint8_t msg_type = *(msg->l2h);
238 int ret = 0;
239
240 switch (msg_type) {
241 case IPAC_MSGT_PING:
242 ret = ipaccess_send_pong(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200243 if (ret < 0) {
244 LOGP(DLINP, LOGL_ERROR, "Cannot send PONG "
245 "message. Reason: %s\n", strerror(errno));
246 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200247 break;
248 case IPAC_MSGT_PONG:
Harald Weltecc2241b2011-07-19 16:06:06 +0200249 DEBUGP(DLMI, "PONG!\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200250 break;
251 case IPAC_MSGT_ID_ACK:
Harald Weltecc2241b2011-07-19 16:06:06 +0200252 DEBUGP(DLMI, "ID_ACK\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200253 break;
254 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200255 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200256}
257
Harald Welte10b41302013-06-30 14:05:49 +0200258static int ipaccess_drop(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200259{
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200260 int ret = 1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200261
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200262 /* Error case: we did not see any ID_RESP yet for this socket. */
263 if (bfd->fd != -1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200264 LOGP(DLINP, LOGL_ERROR, "Forcing socket shutdown with "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200265 "no signal link set\n");
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +0200266 osmo_fd_unregister(bfd);
267 close(bfd->fd);
268 bfd->fd = -1;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200269 ret = -ENOENT;
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +0200270 }
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200271
272 /* e1inp_sign_link_destroy releases the socket descriptors for us. */
273 line->ops->sign_link_down(line);
274
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200275 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200276}
277
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200278static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
279 struct osmo_fd *bfd)
280{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200281 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200282 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200283 struct ipaccess_unit unit_data = {};
284 struct e1inp_sign_link *sign_link;
285 char *unitid;
286 int len, ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200287
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200288 /* Handle IPA PING, PONG and ID_ACK messages. */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200289 ret = ipaccess_rcvmsg_base(msg, bfd);
290 switch(ret) {
291 case -1:
292 /* error in IPA control message handling */
293 goto err;
294 case 1:
295 /* this is an IPA control message, skip further processing */
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200296 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200297 case 0:
298 /* this is not an IPA control message, continue */
299 break;
300 default:
301 LOGP(DLINP, LOGL_ERROR, "Unexpected return from "
302 "ipaccess_rcvmsg_base "
303 "(ret=%d)\n", ret);
304 goto err;
305 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200306
307 switch (msg_type) {
308 case IPAC_MSGT_ID_RESP:
Harald Weltecc2241b2011-07-19 16:06:06 +0200309 DEBUGP(DLMI, "ID_RESP\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200310 /* parse tags, search for Unit ID */
311 ret = ipaccess_idtag_parse(&tlvp, (uint8_t *)msg->l2h + 2,
312 msgb_l2len(msg)-2);
Harald Weltecc2241b2011-07-19 16:06:06 +0200313 DEBUGP(DLMI, "\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200314 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200315 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200316 "with malformed TLVs\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200317 ret = -EINVAL;
318 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200319 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200320 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200321 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200322 "without unit ID\n");
323 ret = -EINVAL;
324 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200325
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200326 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200327 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200328 if (len < 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200329 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200330 "with too small unit ID\n");
331 ret = -EINVAL;
332 goto err;
333 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200334 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
335 unitid[len - 1] = '\0';
336 ipaccess_parse_unitid(unitid, &unit_data);
337
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200338 if (!line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200339 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200340 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200341 ret = -EINVAL;
342 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200343 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200344 /* the BSC creates the new sign links at this stage. */
345 if (bfd->priv_nr == E1INP_SIGN_OML) {
346 sign_link =
347 line->ops->sign_link_up(&unit_data, line,
348 E1INP_SIGN_OML);
349 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200350 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200351 "Unable to set signal link, "
352 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200353 ret = -EINVAL;
354 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200355 }
356 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200357 struct e1inp_ts *ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200358 struct osmo_fd *newbfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200359 struct e1inp_line *new_line;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200360
361 sign_link =
362 line->ops->sign_link_up(&unit_data, line,
363 E1INP_SIGN_RSL);
364 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200365 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200366 "Unable to set signal link, "
367 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200368 ret = -EINVAL;
369 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200370 }
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200371 /* this is a bugtrap, the BSC should be using the
372 * virtual E1 line used by OML for this RSL link. */
373 if (sign_link->ts->line == line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200374 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200375 "Fix your BSC, you should use the "
376 "E1 line used by the OML link for "
377 "your RSL link.\n");
378 return 0;
379 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200380 /* Finally, we know which OML link is associated with
381 * this RSL link, attach it to this socket. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200382 bfd->data = new_line = sign_link->ts->line;
383 e1inp_line_get(new_line);
384 ts = &new_line->ts[E1INP_SIGN_RSL+unit_data.trx_id-1];
385 newbfd = &ts->driver.ipaccess.fd;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200386
387 /* get rid of our old temporary bfd */
388 memcpy(newbfd, bfd, sizeof(*newbfd));
389 newbfd->priv_nr = E1INP_SIGN_RSL + unit_data.trx_id;
390 osmo_fd_unregister(bfd);
391 bfd->fd = -1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200392 osmo_fd_register(newbfd);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200393 /* now we can release the dummy RSL line. */
394 e1inp_line_put(line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200395 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200396 break;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200397 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200398 LOGP(DLINP, LOGL_ERROR, "Unknown IPA message type\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200399 ret = -EINVAL;
400 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200401 }
402 return 0;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200403err:
404 osmo_fd_unregister(bfd);
405 close(bfd->fd);
406 bfd->fd = -1;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200407 e1inp_line_put(line);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200408 return ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200409}
410
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200411static int handle_ts1_read(struct osmo_fd *bfd)
412{
413 struct e1inp_line *line = bfd->data;
414 unsigned int ts_nr = bfd->priv_nr;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200415 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200416 struct e1inp_sign_link *link;
417 struct ipaccess_head *hh;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200418 struct msgb *msg;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200419 int ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200420
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200421 ret = ipa_msg_recv(bfd->fd, &msg);
422 if (ret < 0) {
423 LOGP(DLINP, LOGL_NOTICE, "Sign link problems, "
424 "closing socket. Reason: %s\n", strerror(errno));
425 goto err;
426 } else if (ret == 0) {
427 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
428 goto err;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200429 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200430 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200431
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200432 hh = (struct ipaccess_head *) msg->data;
433 if (hh->proto == IPAC_PROTO_IPACCESS) {
434 ipaccess_rcvmsg(line, msg, bfd);
435 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200436 return 0;
Holger Hans Peter Freyther63ddf462013-12-28 21:19:19 +0100437 } else if (e1i_ts->type == E1INP_TS_TYPE_NONE) {
438 /* this sign link is not know yet.. complain. */
439 LOGP(DLINP, LOGL_ERROR, "Timeslot is not configured.\n");
440 ret = -EINVAL;
441 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200442 }
443 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
444 * might have free'd it !!! */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200445
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200446 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
447 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200448 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200449 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200450 ret = -EINVAL;
451 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200452 }
453 msg->dst = link;
454
455 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200456 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200457 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200458 "no action set for signalling messages.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200459 ret = -EINVAL;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200460 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200461 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200462 if (e1i_ts->line->ops->sign_link(msg) < 0) {
Pablo Neira Ayusoa49c24d2012-10-16 11:24:08 +0200463 /* Don't close the signalling link if the upper layers report
464 * an error, that's too strict. BTW, the signalling layer is
465 * resposible for releasing the message.
466 */
Harald Weltecc2241b2011-07-19 16:06:06 +0200467 LOGP(DLINP, LOGL_ERROR, "Bad signalling message,"
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200468 "sign_link returned error\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200469 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200470
471 return 0;
472err_msg:
473 msgb_free(msg);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200474err:
Harald Welte10b41302013-06-30 14:05:49 +0200475 ipaccess_drop(bfd, line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200476 return ret;
477}
478
479void ipaccess_prepend_header_ext(struct msgb *msg, int proto)
480{
481 struct ipaccess_head_ext *hh_ext;
482
483 /* prepend the osmo ip.access header extension */
484 hh_ext = (struct ipaccess_head_ext *) msgb_push(msg, sizeof(*hh_ext));
485 hh_ext->proto = proto;
486}
487
488void ipaccess_prepend_header(struct msgb *msg, int proto)
489{
490 struct ipaccess_head *hh;
491
492 /* prepend the ip.access header */
493 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
494 hh->len = htons(msg->len - sizeof(*hh));
495 hh->proto = proto;
496}
497
498static int ts_want_write(struct e1inp_ts *e1i_ts)
499{
500 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
501
502 return 0;
503}
504
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200505static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200506{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200507 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200508 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100509 return e1inp_close_socket(e1i_ts, sign_link, bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200510}
511
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200512static void timeout_ts1_write(void *data)
513{
514 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
515
516 /* trigger write of ts1, due to tx delay timer */
517 ts_want_write(e1i_ts);
518}
519
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200520static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200521{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200522 unsigned int ts_nr = bfd->priv_nr;
523 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
524 struct e1inp_sign_link *sign_link;
525 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200526 int ret;
527
528 bfd->when &= ~BSC_FD_WRITE;
529
530 /* get the next msg for this timeslot */
531 msg = e1inp_tx_ts(e1i_ts, &sign_link);
532 if (!msg) {
533 /* no message after tx delay timer */
534 return 0;
535 }
536
537 switch (sign_link->type) {
538 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200539 break;
540 case E1INP_SIGN_RSL:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200541 break;
542 default:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200543 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200544 ret = -EINVAL;
545 goto out;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200546 }
547
548 msg->l2h = msg->data;
Pablo Neira Ayusob9ed7e32011-07-05 18:31:59 +0200549 ipaccess_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200550
Harald Weltecc2241b2011-07-19 16:06:06 +0200551 DEBUGP(DLMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200552
553 ret = send(bfd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200554 if (ret != msg->len) {
555 LOGP(DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling "
556 "message. Reason: %s\n", strerror(errno));
557 goto err;
558 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200559
560 /* set tx delay timer for next event */
561 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
562 e1i_ts->sign.tx_timer.data = e1i_ts;
563
564 /* Reducing this might break the nanoBTS 900 init. */
565 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
566
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200567out:
568 msgb_free(msg);
569 return ret;
570err:
Harald Welte10b41302013-06-30 14:05:49 +0200571 ipaccess_drop(bfd, line);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200572 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200573 return ret;
574}
575
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200576static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200577{
578 struct e1inp_line *line = bfd->data;
579
580 return __handle_ts1_write(bfd, line);
581}
582
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200583static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200584{
585 struct e1inp_line *line = link->line;
586
587 return __handle_ts1_write(link->ofd, line);
588}
589
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200590/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200591int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200592{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200593 int rc = 0;
594
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200595 if (what & BSC_FD_READ)
596 rc = handle_ts1_read(bfd);
597 if (what & BSC_FD_WRITE)
598 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200599
600 return rc;
601}
602
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200603static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200604
605struct e1inp_driver ipaccess_driver = {
606 .name = "ipa",
607 .want_write = ts_want_write,
608 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200609 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200610 .default_delay = 0,
611};
612
613/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200614static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200615{
616 int ret;
617 int idx = 0;
Daniel Willmann85980722014-01-09 14:30:55 +0100618 int i, val;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200619 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200620 struct e1inp_ts *e1i_ts;
621 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200622
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200623 /* clone virtual E1 line for this new OML link. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200624 line = e1inp_line_clone(tall_ipa_ctx, link->line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200625 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200626 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200627 return -ENOMEM;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200628 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200629
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200630 /* create virrtual E1 timeslots for signalling */
Pablo Neira Ayuso59301852011-06-27 15:44:23 +0200631 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200632
633 /* initialize the fds */
634 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
635 line->ts[i].driver.ipaccess.fd.fd = -1;
636
637 e1i_ts = &line->ts[idx];
638
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200639 bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200640 bfd->fd = fd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200641 bfd->data = line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200642 bfd->priv_nr = E1INP_SIGN_OML;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200643 bfd->cb = ipaccess_fd_cb;
644 bfd->when = BSC_FD_READ;
645 ret = osmo_fd_register(bfd);
646 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200647 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200648 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200649 }
650
Daniel Willmann85980722014-01-09 14:30:55 +0100651 /* Enable TCP keepalive to find out if the connection is gone */
652 val = 1;
653 ret = setsockopt(bfd->fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
654 if (ret < 0)
655 LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive: %s\n",
656 strerror(errno));
657 else
658 LOGP(DLINP, LOGL_NOTICE, "Keepalive is set: %i\n", ret);
659
660#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
661 /* The following options are not portable! */
662 val = DEFAULT_TCP_KEEPALIVE_IDLE_TIMEOUT;
663 ret = setsockopt(bfd->fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val));
664 if (ret < 0)
665 LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive idle time: %s\n",
666 strerror(errno));
667 val = DEFAULT_TCP_KEEPALIVE_INTERVAL;
668 ret = setsockopt(bfd->fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val));
669 if (ret < 0)
670 LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive interval: %s\n",
671 strerror(errno));
672 val = DEFAULT_TCP_KEEPALIVE_RETRY_COUNT;
673 ret = setsockopt(bfd->fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val));
674 if (ret < 0)
675 LOGP(DLINP, LOGL_NOTICE, "Failed to set keepalive count: %s\n",
676 strerror(errno));
677#endif
678
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200679 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
680 ret = ipaccess_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200681 if (ret < 0) {
682 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
683 strerror(errno));
684 goto err_socket;
685 }
686 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200687
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200688err_socket:
689 osmo_fd_unregister(bfd);
690err_line:
691 close(bfd->fd);
692 bfd->fd = -1;
693 e1inp_line_put(line);
694 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200695}
696
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200697static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200698{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200699 struct e1inp_line *line;
700 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200701 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200702 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200703
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200704 /* We don't know yet which OML link to associate it with. Thus, we
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200705 * allocate a temporary E1 line until we have received ID. */
706 line = e1inp_line_clone(tall_ipa_ctx, link->line);
707 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200708 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200709 return -ENOMEM;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200710 }
711 /* initialize the fds */
712 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
713 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200714
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200715 /* we need this to initialize this in case to avoid crashes in case
716 * that the socket is closed before we've seen an ID_RESP. */
717 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line);
718
719 e1i_ts = &line->ts[E1INP_SIGN_RSL-1];
720
721 bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200722 bfd->fd = fd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200723 bfd->data = line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200724 bfd->priv_nr = E1INP_SIGN_RSL;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200725 bfd->cb = ipaccess_fd_cb;
726 bfd->when = BSC_FD_READ;
727 ret = osmo_fd_register(bfd);
728 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200729 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200730 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200731 }
732 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
733 ret = ipaccess_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200734 if (ret < 0) {
735 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
736 strerror(errno));
737 goto err_socket;
738 }
739 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200740
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200741err_socket:
742 osmo_fd_unregister(bfd);
743err_line:
744 close(bfd->fd);
745 bfd->fd = -1;
746 e1inp_line_put(line);
747 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200748}
749
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200750#define IPA_STRING_MAX 64
751
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200752static struct msgb *
753ipa_bts_id_resp(struct ipaccess_unit *dev, uint8_t *data, int len)
754{
755 struct msgb *nmsg;
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200756 char str[IPA_STRING_MAX];
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200757 uint8_t *tag;
758
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200759 nmsg = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200760 if (!nmsg)
761 return NULL;
762
763 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
764 while (len) {
765 if (len < 2) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200766 LOGP(DLINP, LOGL_NOTICE,
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200767 "Short read of ipaccess tag\n");
768 msgb_free(nmsg);
769 return NULL;
770 }
771 switch (data[1]) {
772 case IPAC_IDTAG_UNIT:
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200773 snprintf(str, sizeof(str), "%u/%u/%u",
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200774 dev->site_id, dev->bts_id, dev->trx_id);
775 break;
776 case IPAC_IDTAG_MACADDR:
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200777 snprintf(str, sizeof(str),
778 "%02x:%02x:%02x:%02x:%02x:%02x",
779 dev->mac_addr[0], dev->mac_addr[1],
780 dev->mac_addr[2], dev->mac_addr[3],
781 dev->mac_addr[4], dev->mac_addr[5]);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200782 break;
783 case IPAC_IDTAG_LOCATION1:
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200784 strncpy(str, dev->location1, IPA_STRING_MAX);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200785 break;
786 case IPAC_IDTAG_LOCATION2:
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200787 strncpy(str, dev->location2, IPA_STRING_MAX);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200788 break;
789 case IPAC_IDTAG_EQUIPVERS:
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200790 strncpy(str, dev->equipvers, IPA_STRING_MAX);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200791 break;
792 case IPAC_IDTAG_SWVERSION:
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200793 strncpy(str, dev->swversion, IPA_STRING_MAX);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200794 break;
795 case IPAC_IDTAG_UNITNAME:
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200796 snprintf(str, sizeof(str),
797 "%s-%02x-%02x-%02x-%02x-%02x-%02x",
798 dev->unit_name,
799 dev->mac_addr[0], dev->mac_addr[1],
800 dev->mac_addr[2], dev->mac_addr[3],
801 dev->mac_addr[4], dev->mac_addr[5]);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200802 break;
803 case IPAC_IDTAG_SERNR:
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200804 strncpy(str, dev->serno, IPA_STRING_MAX);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200805 break;
806 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200807 LOGP(DLINP, LOGL_NOTICE,
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200808 "Unknown ipaccess tag 0x%02x\n", *data);
809 msgb_free(nmsg);
810 return NULL;
811 }
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200812 str[IPA_STRING_MAX-1] = '\0';
813
Harald Weltecc2241b2011-07-19 16:06:06 +0200814 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200815 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
816 tag[0] = 0x00;
817 tag[1] = 1 + strlen(str) + 1;
818 tag[2] = data[1];
819 memcpy(tag + 3, str, strlen(str) + 1);
820 data += 2;
821 len -= 2;
822 }
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200823 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200824 return nmsg;
825}
826
827static struct msgb *ipa_bts_id_ack(void)
828{
829 struct msgb *nmsg2;
830
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200831 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200832 if (!nmsg2)
833 return NULL;
834
835 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200836 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200837
838 return nmsg2;
839}
840
Harald Welte51de9ca2013-06-30 20:13:16 +0200841static void ipaccess_bts_updown_cb(struct ipa_client_conn *link, int up)
842{
843 struct e1inp_line *line = link->line;
844
845 if (up)
846 return;
847
848 if (line->ops->sign_link_down)
849 line->ops->sign_link_down(line);
850}
851
Harald Weltea3e9dd52013-06-30 14:25:07 +0200852static int ipaccess_bts_read_cb(struct ipa_client_conn *link, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200853{
854 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
855 struct e1inp_ts *e1i_ts = NULL;
856 struct e1inp_sign_link *sign_link;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200857 struct msgb *rmsg;
858 int ret = 0;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200859
860 /* special handling for IPA CCM. */
861 if (hh->proto == IPAC_PROTO_IPACCESS) {
862 uint8_t msg_type = *(msg->l2h);
863
864 /* ping, pong and acknowledgment cases. */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200865 ret = ipaccess_rcvmsg_bts_base(msg, link->ofd);
866 if (ret < 0)
867 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200868
869 /* this is a request for identification from the BSC. */
870 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200871 struct e1inp_sign_link *sign_link;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200872 uint8_t *data = msgb_l2(msg);
873 int len = msgb_l2len(msg);
874
Harald Weltecc2241b2011-07-19 16:06:06 +0200875 LOGP(DLINP, LOGL_NOTICE, "received ID get\n");
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200876 if (!link->line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200877 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200878 "Unable to set signal link, "
879 "closing socket.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200880 ret = -EINVAL;
881 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200882 }
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200883 rmsg = ipa_bts_id_resp(link->line->ops->cfg.ipa.dev,
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200884 data + 1, len - 1);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200885 ret = ipaccess_send(link->ofd->fd, rmsg->data,
886 rmsg->len);
887 if (ret != rmsg->len) {
888 LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP "
889 "message. Reason: %s\n", strerror(errno));
890 goto err_rmsg;
891 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200892 msgb_free(rmsg);
893
894 /* send ID_ACK. */
895 rmsg = ipa_bts_id_ack();
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200896 ret = ipaccess_send(link->ofd->fd, rmsg->data,
897 rmsg->len);
898 if (ret != rmsg->len) {
899 LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK "
900 "message. Reason: %s\n", strerror(errno));
901 goto err_rmsg;
902 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200903 msgb_free(rmsg);
Harald Welte6eddd472013-06-30 20:18:53 +0200904
905 sign_link = link->line->ops->sign_link_up(msg,
906 link->line,
907 link->ofd->priv_nr);
908 if (sign_link == NULL) {
909 LOGP(DLINP, LOGL_ERROR,
910 "Unable to set signal link, "
911 "closing socket.\n");
912 ret = -EINVAL;
913 goto err;
914 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200915 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200916 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200917 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200918 } else if (link->port == IPA_TCP_PORT_OML)
919 e1i_ts = &link->line->ts[0];
920 else if (link->port == IPA_TCP_PORT_RSL)
921 e1i_ts = &link->line->ts[1];
922
Pablo Neira Ayusob9487012013-07-05 14:38:43 +0200923 OSMO_ASSERT(e1i_ts != NULL);
924
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200925 /* look up for some existing signaling link. */
926 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
927 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200928 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200929 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200930 ret = -EIO;
931 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200932 }
933 msg->dst = sign_link;
934
935 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200936 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200937 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200938 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200939 ret = -ENOENT;
940 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200941 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200942 link->line->ops->sign_link(msg);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200943 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200944
945err_rmsg:
946 msgb_free(rmsg);
947err:
948 osmo_fd_unregister(link->ofd);
949 close(link->ofd->fd);
950 link->ofd->fd = -1;
951 msgb_free(msg);
952 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200953}
954
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200955struct ipaccess_line {
956 int line_already_initialized;
957};
958
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200959static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200960{
961 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200962 struct ipaccess_line *il;
963
964 if (!line->driver_data)
965 line->driver_data = talloc_zero(line, struct ipaccess_line);
966
967 if (!line->driver_data) {
968 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
969 return -ENOMEM;
970 }
971 il = line->driver_data;
972
973 /* We only initialize this line once. */
974 if (il->line_already_initialized)
975 return 0;
976
977 il->line_already_initialized = 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200978
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200979 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200980 case E1INP_LINE_R_BSC: {
981 struct ipa_server_link *oml_link, *rsl_link;
982
Harald Weltecc2241b2011-07-19 16:06:06 +0200983 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode\n");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200984
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200985 oml_link = ipa_server_link_create(tall_ipa_ctx, line,
986 "0.0.0.0", IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200987 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200988 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200989 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200990 "BSC link: %s\n", strerror(errno));
991 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200992 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200993 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200994 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200995 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200996 ipa_server_link_destroy(oml_link);
997 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200998 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200999 rsl_link = ipa_server_link_create(tall_ipa_ctx, line,
1000 "0.0.0.0", IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +02001001 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001002 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001003 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001004 "BSC link: %s\n", strerror(errno));
1005 return -ENOMEM;
1006 }
1007 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001008 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001009 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001010 ipa_server_link_destroy(rsl_link);
1011 return -EIO;
1012 }
1013 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001014 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +02001015 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001016 case E1INP_LINE_R_BTS: {
Harald Welte84f67b22013-06-30 13:13:59 +02001017 struct ipa_client_conn *link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001018
Harald Weltecc2241b2011-07-19 16:06:06 +02001019 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode\n");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001020
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001021 link = ipa_client_conn_create(tall_ipa_ctx,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +02001022 &line->ts[E1INP_SIGN_OML-1],
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +02001023 E1INP_SIGN_OML,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +02001024 line->ops->cfg.ipa.addr,
1025 IPA_TCP_PORT_OML,
Harald Welte51de9ca2013-06-30 20:13:16 +02001026 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001027 ipaccess_bts_read_cb,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +02001028 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001029 line);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001030 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001031 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001032 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001033 return -ENOMEM;
1034 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001035 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +02001036 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001037 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001038 ipa_client_conn_close(link);
1039 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001040 return -EIO;
1041 }
1042 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001043 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001044 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001045 default:
1046 break;
1047 }
1048 return ret;
1049}
1050
Harald Welte84f67b22013-06-30 13:13:59 +02001051int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line,
1052 const char *rem_addr, uint16_t rem_port)
1053{
1054 struct ipa_client_conn *rsl_link;
1055
1056 rsl_link = ipa_client_conn_create(tall_ipa_ctx,
1057 &line->ts[E1INP_SIGN_RSL-1],
1058 E1INP_SIGN_RSL,
1059 rem_addr, rem_port,
Harald Welte51de9ca2013-06-30 20:13:16 +02001060 ipaccess_bts_updown_cb,
Harald Weltea3e9dd52013-06-30 14:25:07 +02001061 ipaccess_bts_read_cb,
Harald Welte84f67b22013-06-30 13:13:59 +02001062 ipaccess_bts_write_cb,
Harald Welte10b41302013-06-30 14:05:49 +02001063 line);
Harald Welte84f67b22013-06-30 13:13:59 +02001064 if (rsl_link == NULL) {
1065 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
1066 "BTS link: %s\n", strerror(errno));
1067 return -ENOMEM;
1068 }
1069 if (ipa_client_conn_open(rsl_link) < 0) {
1070 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
1071 strerror(errno));
1072 ipa_client_conn_close(rsl_link);
1073 ipa_client_conn_destroy(rsl_link);
1074 return -EIO;
1075 }
1076 return 0;
1077}
1078
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001079void e1inp_ipaccess_init(void)
1080{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +02001081 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001082 e1inp_driver_register(&ipaccess_driver);
1083}