blob: 7e1891e17f2d0f6f0d101c0be3a89031ee29a40c [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>
31#include <string.h>
32#include <time.h>
33#include <sys/fcntl.h>
34#include <sys/socket.h>
35#include <sys/ioctl.h>
36#include <arpa/inet.h>
37
38#include <osmocom/core/select.h>
39#include <osmocom/gsm/tlv.h>
40#include <osmocom/core/msgb.h>
41#include <osmocom/core/logging.h>
Harald Welte71d87b22011-07-18 14:49:56 +020042#include <osmocom/core/talloc.h>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020043#include <osmocom/abis/e1_input.h>
44#include <osmocom/abis/ipaccess.h>
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +020045#include <osmocom/core/socket.h>
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020046#include <osmocom/abis/ipa.h>
Pablo Neira Ayusoef132692013-07-08 01:17:27 +020047#include <osmocom/core/backtrace.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020048
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +020049static void *tall_ipa_ctx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020050
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020051#define TS1_ALLOC_SIZE 900
52
53/*
54 * Common propietary IPA messages:
55 * - PONG: in reply to PING.
56 * - ID_REQUEST: first messages once OML has been established.
57 * - ID_ACK: in reply to ID_ACK.
58 */
59const uint8_t ipa_pong_msg[] = {
60 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG
61};
62
63const uint8_t ipa_id_ack_msg[] = {
64 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK
65};
66
67const uint8_t ipa_id_req_msg[] = {
68 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET,
69 0x01, IPAC_IDTAG_UNIT,
70 0x01, IPAC_IDTAG_MACADDR,
71 0x01, IPAC_IDTAG_LOCATION1,
72 0x01, IPAC_IDTAG_LOCATION2,
73 0x01, IPAC_IDTAG_EQUIPVERS,
74 0x01, IPAC_IDTAG_SWVERSION,
75 0x01, IPAC_IDTAG_UNITNAME,
76 0x01, IPAC_IDTAG_SERNR,
77};
78
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020079static const char *idtag_names[] = {
80 [IPAC_IDTAG_SERNR] = "Serial_Number",
81 [IPAC_IDTAG_UNITNAME] = "Unit_Name",
82 [IPAC_IDTAG_LOCATION1] = "Location_1",
83 [IPAC_IDTAG_LOCATION2] = "Location_2",
84 [IPAC_IDTAG_EQUIPVERS] = "Equipment_Version",
85 [IPAC_IDTAG_SWVERSION] = "Software_Version",
86 [IPAC_IDTAG_IPADDR] = "IP_Address",
87 [IPAC_IDTAG_MACADDR] = "MAC_Address",
88 [IPAC_IDTAG_UNIT] = "Unit_ID",
89};
90
91const char *ipaccess_idtag_name(uint8_t tag)
92{
93 if (tag >= ARRAY_SIZE(idtag_names))
94 return "unknown";
95
96 return idtag_names[tag];
97}
98
99int ipaccess_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len)
100{
101 uint8_t t_len;
102 uint8_t t_tag;
103 uint8_t *cur = buf;
104
105 memset(dec, 0, sizeof(*dec));
106
107 while (len >= 2) {
108 len -= 2;
109 t_len = *cur++;
110 t_tag = *cur++;
111
112 if (t_len > len + 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200113 LOGP(DLMI, LOGL_ERROR, "The tag does not fit: %d\n", t_len);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200114 return -EINVAL;
115 }
116
Harald Weltecc2241b2011-07-19 16:06:06 +0200117 DEBUGPC(DLMI, "%s='%s' ", ipaccess_idtag_name(t_tag), cur);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200118
119 dec->lv[t_tag].len = t_len;
120 dec->lv[t_tag].val = cur;
121
122 cur += t_len;
123 len -= t_len;
124 }
125 return 0;
126}
127
128int ipaccess_parse_unitid(const char *str, struct ipaccess_unit *unit_data)
129{
130 unsigned long ul;
131 char *endptr;
132 const char *nptr;
133
134 nptr = str;
135 ul = strtoul(nptr, &endptr, 10);
136 if (endptr <= nptr)
137 return -EINVAL;
Pablo Neira Ayuso62d345a2011-07-05 16:37:37 +0200138 unit_data->site_id = ul & 0xffff;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200139
140 if (*endptr++ != '/')
141 return -EINVAL;
142
143 nptr = endptr;
144 ul = strtoul(nptr, &endptr, 10);
145 if (endptr <= nptr)
146 return -EINVAL;
Pablo Neira Ayuso62d345a2011-07-05 16:37:37 +0200147 unit_data->bts_id = ul & 0xffff;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200148
149 if (*endptr++ != '/')
150 return -EINVAL;
151
152 nptr = endptr;
153 ul = strtoul(nptr, &endptr, 10);
154 if (endptr <= nptr)
155 return -EINVAL;
Pablo Neira Ayuso62d345a2011-07-05 16:37:37 +0200156 unit_data->trx_id = ul & 0xffff;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200157
158 return 0;
159}
160
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200161static int ipaccess_send(int fd, const void *msg, size_t msglen)
162{
163 int ret;
164
165 ret = write(fd, msg, msglen);
166 if (ret < 0)
167 return ret;
168 if (ret < msglen) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200169 LOGP(DLINP, LOGL_ERROR, "ipaccess_send: short write\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200170 return -EIO;
171 }
172 return ret;
173}
174
175int ipaccess_send_pong(int fd)
176{
177 return ipaccess_send(fd, ipa_pong_msg, sizeof(ipa_pong_msg));
178}
179
180int ipaccess_send_id_ack(int fd)
181{
182 return ipaccess_send(fd, ipa_id_ack_msg, sizeof(ipa_id_ack_msg));
183}
184
185int ipaccess_send_id_req(int fd)
186{
187 return ipaccess_send(fd, ipa_id_req_msg, sizeof(ipa_id_req_msg));
188}
189
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200190/* base handling of the ip.access protocol */
Harald Weltebc25bca2011-08-19 22:32:38 +0200191int ipaccess_rcvmsg_base(struct msgb *msg, struct osmo_fd *bfd)
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200192{
193 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200194 int ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200195
196 switch (msg_type) {
197 case IPAC_MSGT_PING:
198 ret = ipaccess_send_pong(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200199 if (ret < 0) {
200 LOGP(DLINP, LOGL_ERROR, "Cannot send PING "
201 "message. Reason: %s\n", strerror(errno));
202 break;
203 }
204 ret = 1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200205 break;
206 case IPAC_MSGT_PONG:
Harald Weltecc2241b2011-07-19 16:06:06 +0200207 DEBUGP(DLMI, "PONG!\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200208 ret = 1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200209 break;
210 case IPAC_MSGT_ID_ACK:
Harald Weltecc2241b2011-07-19 16:06:06 +0200211 DEBUGP(DLMI, "ID_ACK? -> ACK!\n");
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200212 ret = ipaccess_send_id_ack(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200213 if (ret < 0) {
214 LOGP(DLINP, LOGL_ERROR, "Cannot send ID_ACK "
215 "message. Reason: %s\n", strerror(errno));
216 break;
217 }
218 ret = 1;
219 break;
220 default:
221 /* This is not an IPA PING, PONG or ID_ACK message */
222 ret = 0;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200223 break;
224 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200225 return ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200226}
227
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200228/* base handling of the ip.access protocol */
229int ipaccess_rcvmsg_bts_base(struct msgb *msg,
230 struct osmo_fd *bfd)
231{
232 uint8_t msg_type = *(msg->l2h);
233 int ret = 0;
234
235 switch (msg_type) {
236 case IPAC_MSGT_PING:
237 ret = ipaccess_send_pong(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200238 if (ret < 0) {
239 LOGP(DLINP, LOGL_ERROR, "Cannot send PONG "
240 "message. Reason: %s\n", strerror(errno));
241 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200242 break;
243 case IPAC_MSGT_PONG:
Harald Weltecc2241b2011-07-19 16:06:06 +0200244 DEBUGP(DLMI, "PONG!\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200245 break;
246 case IPAC_MSGT_ID_ACK:
Harald Weltecc2241b2011-07-19 16:06:06 +0200247 DEBUGP(DLMI, "ID_ACK\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200248 break;
249 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200250 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200251}
252
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200253static int ipaccess_drop(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200254{
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200255 int ret = 1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200256 struct e1inp_line *line = bfd->data;
257
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200258 /* Error case: we did not see any ID_RESP yet for this socket. */
259 if (bfd->fd != -1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200260 LOGP(DLINP, LOGL_ERROR, "Forcing socket shutdown with "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200261 "no signal link set\n");
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +0200262 osmo_fd_unregister(bfd);
263 close(bfd->fd);
264 bfd->fd = -1;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200265 ret = -ENOENT;
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +0200266 }
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200267
268 /* e1inp_sign_link_destroy releases the socket descriptors for us. */
269 line->ops->sign_link_down(line);
270
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200271 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200272}
273
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200274static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
275 struct osmo_fd *bfd)
276{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200277 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200278 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200279 struct ipaccess_unit unit_data = {};
280 struct e1inp_sign_link *sign_link;
281 char *unitid;
282 int len, ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200283
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200284 /* Handle IPA PING, PONG and ID_ACK messages. */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200285 ret = ipaccess_rcvmsg_base(msg, bfd);
286 switch(ret) {
287 case -1:
288 /* error in IPA control message handling */
289 goto err;
290 case 1:
291 /* this is an IPA control message, skip further processing */
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200292 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200293 case 0:
294 /* this is not an IPA control message, continue */
295 break;
296 default:
297 LOGP(DLINP, LOGL_ERROR, "Unexpected return from "
298 "ipaccess_rcvmsg_base "
299 "(ret=%d)\n", ret);
300 goto err;
301 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200302
303 switch (msg_type) {
304 case IPAC_MSGT_ID_RESP:
Harald Weltecc2241b2011-07-19 16:06:06 +0200305 DEBUGP(DLMI, "ID_RESP\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200306 /* parse tags, search for Unit ID */
307 ret = ipaccess_idtag_parse(&tlvp, (uint8_t *)msg->l2h + 2,
308 msgb_l2len(msg)-2);
Harald Weltecc2241b2011-07-19 16:06:06 +0200309 DEBUGP(DLMI, "\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200310 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200311 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200312 "with malformed TLVs\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200313 ret = -EINVAL;
314 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200315 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200316 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200317 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200318 "without unit ID\n");
319 ret = -EINVAL;
320 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200321
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200322 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200323 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200324 if (len < 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200325 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200326 "with too small unit ID\n");
327 ret = -EINVAL;
328 goto err;
329 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200330 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
331 unitid[len - 1] = '\0';
332 ipaccess_parse_unitid(unitid, &unit_data);
333
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200334 if (!line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200335 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200336 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200337 ret = -EINVAL;
338 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200339 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200340 /* the BSC creates the new sign links at this stage. */
341 if (bfd->priv_nr == E1INP_SIGN_OML) {
342 sign_link =
343 line->ops->sign_link_up(&unit_data, line,
344 E1INP_SIGN_OML);
345 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200346 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200347 "Unable to set signal link, "
348 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200349 ret = -EINVAL;
350 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200351 }
352 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200353 struct e1inp_ts *ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200354 struct osmo_fd *newbfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200355 struct e1inp_line *new_line;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200356
357 sign_link =
358 line->ops->sign_link_up(&unit_data, line,
359 E1INP_SIGN_RSL);
360 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200361 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200362 "Unable to set signal link, "
363 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200364 ret = -EINVAL;
365 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200366 }
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200367 /* this is a bugtrap, the BSC should be using the
368 * virtual E1 line used by OML for this RSL link. */
369 if (sign_link->ts->line == line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200370 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200371 "Fix your BSC, you should use the "
372 "E1 line used by the OML link for "
373 "your RSL link.\n");
374 return 0;
375 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200376 /* Finally, we know which OML link is associated with
377 * this RSL link, attach it to this socket. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200378 bfd->data = new_line = sign_link->ts->line;
379 e1inp_line_get(new_line);
380 ts = &new_line->ts[E1INP_SIGN_RSL+unit_data.trx_id-1];
381 newbfd = &ts->driver.ipaccess.fd;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200382
383 /* get rid of our old temporary bfd */
384 memcpy(newbfd, bfd, sizeof(*newbfd));
385 newbfd->priv_nr = E1INP_SIGN_RSL + unit_data.trx_id;
386 osmo_fd_unregister(bfd);
387 bfd->fd = -1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200388 osmo_fd_register(newbfd);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200389 /* now we can release the dummy RSL line. */
390 e1inp_line_put(line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200391 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200392 break;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200393 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200394 LOGP(DLINP, LOGL_ERROR, "Unknown IPA message type\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200395 ret = -EINVAL;
396 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200397 }
398 return 0;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200399err:
400 osmo_fd_unregister(bfd);
401 close(bfd->fd);
402 bfd->fd = -1;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200403 e1inp_line_put(line);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200404 return ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200405}
406
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200407static int handle_ts1_read(struct osmo_fd *bfd)
408{
409 struct e1inp_line *line = bfd->data;
410 unsigned int ts_nr = bfd->priv_nr;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200411 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200412 struct e1inp_sign_link *link;
413 struct ipaccess_head *hh;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200414 struct msgb *msg;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200415 int ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200416
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200417 ret = ipa_msg_recv(bfd->fd, &msg);
418 if (ret < 0) {
419 LOGP(DLINP, LOGL_NOTICE, "Sign link problems, "
420 "closing socket. Reason: %s\n", strerror(errno));
421 goto err;
422 } else if (ret == 0) {
423 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
424 goto err;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200425 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200426 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200427
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200428 hh = (struct ipaccess_head *) msg->data;
429 if (hh->proto == IPAC_PROTO_IPACCESS) {
430 ipaccess_rcvmsg(line, msg, bfd);
431 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200432 return 0;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200433 }
434 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
435 * might have free'd it !!! */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200436
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200437 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
438 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200439 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200440 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200441 ret = -EINVAL;
442 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200443 }
444 msg->dst = link;
445
446 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200447 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200448 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200449 "no action set for signalling messages.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200450 ret = -EINVAL;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200451 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200452 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200453 if (e1i_ts->line->ops->sign_link(msg) < 0) {
Pablo Neira Ayusoa49c24d2012-10-16 11:24:08 +0200454 /* Don't close the signalling link if the upper layers report
455 * an error, that's too strict. BTW, the signalling layer is
456 * resposible for releasing the message.
457 */
Harald Weltecc2241b2011-07-19 16:06:06 +0200458 LOGP(DLINP, LOGL_ERROR, "Bad signalling message,"
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200459 "sign_link returned error\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200460 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200461
462 return 0;
463err_msg:
464 msgb_free(msg);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200465err:
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200466 ipaccess_drop(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200467 return ret;
468}
469
470void ipaccess_prepend_header_ext(struct msgb *msg, int proto)
471{
472 struct ipaccess_head_ext *hh_ext;
473
474 /* prepend the osmo ip.access header extension */
475 hh_ext = (struct ipaccess_head_ext *) msgb_push(msg, sizeof(*hh_ext));
476 hh_ext->proto = proto;
477}
478
479void ipaccess_prepend_header(struct msgb *msg, int proto)
480{
481 struct ipaccess_head *hh;
482
483 /* prepend the ip.access header */
484 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
485 hh->len = htons(msg->len - sizeof(*hh));
486 hh->proto = proto;
487}
488
489static int ts_want_write(struct e1inp_ts *e1i_ts)
490{
491 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
492
493 return 0;
494}
495
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200496static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200497{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200498 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200499 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100500 return e1inp_close_socket(e1i_ts, sign_link, bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200501}
502
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200503static void timeout_ts1_write(void *data)
504{
505 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
506
507 /* trigger write of ts1, due to tx delay timer */
508 ts_want_write(e1i_ts);
509}
510
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200511static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200512{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200513 unsigned int ts_nr = bfd->priv_nr;
514 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
515 struct e1inp_sign_link *sign_link;
516 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200517 int ret;
518
519 bfd->when &= ~BSC_FD_WRITE;
520
521 /* get the next msg for this timeslot */
522 msg = e1inp_tx_ts(e1i_ts, &sign_link);
523 if (!msg) {
524 /* no message after tx delay timer */
525 return 0;
526 }
527
528 switch (sign_link->type) {
529 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200530 break;
531 case E1INP_SIGN_RSL:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200532 break;
533 default:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200534 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200535 ret = -EINVAL;
536 goto out;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200537 }
538
539 msg->l2h = msg->data;
Pablo Neira Ayusob9ed7e32011-07-05 18:31:59 +0200540 ipaccess_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200541
Harald Weltecc2241b2011-07-19 16:06:06 +0200542 DEBUGP(DLMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200543
544 ret = send(bfd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200545 if (ret != msg->len) {
546 LOGP(DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling "
547 "message. Reason: %s\n", strerror(errno));
548 goto err;
549 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200550
551 /* set tx delay timer for next event */
552 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
553 e1i_ts->sign.tx_timer.data = e1i_ts;
554
555 /* Reducing this might break the nanoBTS 900 init. */
556 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
557
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200558out:
559 msgb_free(msg);
560 return ret;
561err:
562 ipaccess_drop(bfd);
563 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200564 return ret;
565}
566
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200567static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200568{
569 struct e1inp_line *line = bfd->data;
570
571 return __handle_ts1_write(bfd, line);
572}
573
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200574static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200575{
576 struct e1inp_line *line = link->line;
577
578 return __handle_ts1_write(link->ofd, line);
579}
580
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200581/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200582int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200583{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200584 int rc = 0;
585
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200586 if (what & BSC_FD_READ)
587 rc = handle_ts1_read(bfd);
588 if (what & BSC_FD_WRITE)
589 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200590
591 return rc;
592}
593
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200594static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200595
596struct e1inp_driver ipaccess_driver = {
597 .name = "ipa",
598 .want_write = ts_want_write,
599 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200600 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200601 .default_delay = 0,
602};
603
604/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200605static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200606{
607 int ret;
608 int idx = 0;
609 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200610 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200611 struct e1inp_ts *e1i_ts;
612 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200613
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200614 /* clone virtual E1 line for this new OML link. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200615 line = e1inp_line_clone(tall_ipa_ctx, link->line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200616 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200617 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200618 return -ENOMEM;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200619 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200620
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200621 /* create virrtual E1 timeslots for signalling */
Pablo Neira Ayuso59301852011-06-27 15:44:23 +0200622 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200623
624 /* initialize the fds */
625 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
626 line->ts[i].driver.ipaccess.fd.fd = -1;
627
628 e1i_ts = &line->ts[idx];
629
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200630 bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200631 bfd->fd = fd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200632 bfd->data = line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200633 bfd->priv_nr = E1INP_SIGN_OML;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200634 bfd->cb = ipaccess_fd_cb;
635 bfd->when = BSC_FD_READ;
636 ret = osmo_fd_register(bfd);
637 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200638 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200639 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200640 }
641
642 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
643 ret = ipaccess_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200644 if (ret < 0) {
645 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
646 strerror(errno));
647 goto err_socket;
648 }
649 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200650
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200651err_socket:
652 osmo_fd_unregister(bfd);
653err_line:
654 close(bfd->fd);
655 bfd->fd = -1;
656 e1inp_line_put(line);
657 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200658}
659
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200660static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200661{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200662 struct e1inp_line *line;
663 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200664 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200665 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200666
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200667 /* We don't know yet which OML link to associate it with. Thus, we
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200668 * allocate a temporary E1 line until we have received ID. */
669 line = e1inp_line_clone(tall_ipa_ctx, link->line);
670 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200671 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200672 return -ENOMEM;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200673 }
674 /* initialize the fds */
675 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
676 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200677
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200678 /* we need this to initialize this in case to avoid crashes in case
679 * that the socket is closed before we've seen an ID_RESP. */
680 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line);
681
682 e1i_ts = &line->ts[E1INP_SIGN_RSL-1];
683
684 bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200685 bfd->fd = fd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200686 bfd->data = line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200687 bfd->priv_nr = E1INP_SIGN_RSL;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200688 bfd->cb = ipaccess_fd_cb;
689 bfd->when = BSC_FD_READ;
690 ret = osmo_fd_register(bfd);
691 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200692 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200693 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200694 }
695 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
696 ret = ipaccess_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200697 if (ret < 0) {
698 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
699 strerror(errno));
700 goto err_socket;
701 }
702 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200703
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200704err_socket:
705 osmo_fd_unregister(bfd);
706err_line:
707 close(bfd->fd);
708 bfd->fd = -1;
709 e1inp_line_put(line);
710 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200711}
712
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200713#define IPA_STRING_MAX 64
714
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200715static struct msgb *
716ipa_bts_id_resp(struct ipaccess_unit *dev, uint8_t *data, int len)
717{
718 struct msgb *nmsg;
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200719 char str[IPA_STRING_MAX];
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200720 uint8_t *tag;
721
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200722 nmsg = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200723 if (!nmsg)
724 return NULL;
725
726 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
727 while (len) {
728 if (len < 2) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200729 LOGP(DLINP, LOGL_NOTICE,
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200730 "Short read of ipaccess tag\n");
731 msgb_free(nmsg);
732 return NULL;
733 }
734 switch (data[1]) {
735 case IPAC_IDTAG_UNIT:
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200736 snprintf(str, sizeof(str), "%u/%u/%u",
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200737 dev->site_id, dev->bts_id, dev->trx_id);
738 break;
739 case IPAC_IDTAG_MACADDR:
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200740 snprintf(str, sizeof(str),
741 "%02x:%02x:%02x:%02x:%02x:%02x",
742 dev->mac_addr[0], dev->mac_addr[1],
743 dev->mac_addr[2], dev->mac_addr[3],
744 dev->mac_addr[4], dev->mac_addr[5]);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200745 break;
746 case IPAC_IDTAG_LOCATION1:
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200747 strncpy(str, dev->location1, IPA_STRING_MAX);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200748 break;
749 case IPAC_IDTAG_LOCATION2:
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200750 strncpy(str, dev->location2, IPA_STRING_MAX);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200751 break;
752 case IPAC_IDTAG_EQUIPVERS:
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200753 strncpy(str, dev->equipvers, IPA_STRING_MAX);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200754 break;
755 case IPAC_IDTAG_SWVERSION:
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200756 strncpy(str, dev->swversion, IPA_STRING_MAX);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200757 break;
758 case IPAC_IDTAG_UNITNAME:
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200759 snprintf(str, sizeof(str),
760 "%s-%02x-%02x-%02x-%02x-%02x-%02x",
761 dev->unit_name,
762 dev->mac_addr[0], dev->mac_addr[1],
763 dev->mac_addr[2], dev->mac_addr[3],
764 dev->mac_addr[4], dev->mac_addr[5]);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200765 break;
766 case IPAC_IDTAG_SERNR:
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200767 strncpy(str, dev->serno, IPA_STRING_MAX);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200768 break;
769 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200770 LOGP(DLINP, LOGL_NOTICE,
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200771 "Unknown ipaccess tag 0x%02x\n", *data);
772 msgb_free(nmsg);
773 return NULL;
774 }
Pablo Neira Ayuso4bab3bf2013-07-05 15:00:13 +0200775 str[IPA_STRING_MAX-1] = '\0';
776
Harald Weltecc2241b2011-07-19 16:06:06 +0200777 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200778 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
779 tag[0] = 0x00;
780 tag[1] = 1 + strlen(str) + 1;
781 tag[2] = data[1];
782 memcpy(tag + 3, str, strlen(str) + 1);
783 data += 2;
784 len -= 2;
785 }
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200786 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200787 return nmsg;
788}
789
790static struct msgb *ipa_bts_id_ack(void)
791{
792 struct msgb *nmsg2;
793
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200794 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200795 if (!nmsg2)
796 return NULL;
797
798 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200799 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200800
801 return nmsg2;
802}
803
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200804static int ipaccess_bts_cb(struct ipa_client_conn *link, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200805{
806 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
807 struct e1inp_ts *e1i_ts = NULL;
808 struct e1inp_sign_link *sign_link;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200809 struct msgb *rmsg;
810 int ret = 0;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200811
812 /* special handling for IPA CCM. */
813 if (hh->proto == IPAC_PROTO_IPACCESS) {
814 uint8_t msg_type = *(msg->l2h);
815
816 /* ping, pong and acknowledgment cases. */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200817 ret = ipaccess_rcvmsg_bts_base(msg, link->ofd);
818 if (ret < 0)
819 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200820
821 /* this is a request for identification from the BSC. */
822 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200823 struct e1inp_sign_link *sign_link;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200824 uint8_t *data = msgb_l2(msg);
825 int len = msgb_l2len(msg);
826
Harald Weltecc2241b2011-07-19 16:06:06 +0200827 LOGP(DLINP, LOGL_NOTICE, "received ID get\n");
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200828 if (!link->line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200829 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200830 "Unable to set signal link, "
831 "closing socket.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200832 ret = -EINVAL;
833 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200834 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200835 sign_link = link->line->ops->sign_link_up(msg,
836 link->line,
837 link->ofd->priv_nr);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200838 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200839 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200840 "Unable to set signal link, "
841 "closing socket.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200842 ret = -EINVAL;
843 goto err;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200844 }
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200845 rmsg = ipa_bts_id_resp(link->line->ops->cfg.ipa.dev,
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200846 data + 1, len - 1);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200847 ret = ipaccess_send(link->ofd->fd, rmsg->data,
848 rmsg->len);
849 if (ret != rmsg->len) {
850 LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP "
851 "message. Reason: %s\n", strerror(errno));
852 goto err_rmsg;
853 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200854 msgb_free(rmsg);
855
856 /* send ID_ACK. */
857 rmsg = ipa_bts_id_ack();
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200858 ret = ipaccess_send(link->ofd->fd, rmsg->data,
859 rmsg->len);
860 if (ret != rmsg->len) {
861 LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK "
862 "message. Reason: %s\n", strerror(errno));
863 goto err_rmsg;
864 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200865 msgb_free(rmsg);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200866 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200867 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200868 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200869 } else if (link->port == IPA_TCP_PORT_OML)
870 e1i_ts = &link->line->ts[0];
871 else if (link->port == IPA_TCP_PORT_RSL)
872 e1i_ts = &link->line->ts[1];
873
Pablo Neira Ayusob9487012013-07-05 14:38:43 +0200874 OSMO_ASSERT(e1i_ts != NULL);
875
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200876 /* look up for some existing signaling link. */
877 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
878 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200879 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200880 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200881 ret = -EIO;
882 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200883 }
884 msg->dst = sign_link;
885
886 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200887 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200888 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200889 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200890 ret = -ENOENT;
891 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200892 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200893 link->line->ops->sign_link(msg);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200894 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200895
896err_rmsg:
897 msgb_free(rmsg);
898err:
899 osmo_fd_unregister(link->ofd);
900 close(link->ofd->fd);
901 link->ofd->fd = -1;
902 msgb_free(msg);
903 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200904}
905
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200906struct ipaccess_line {
907 int line_already_initialized;
908};
909
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200910static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200911{
912 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200913 struct ipaccess_line *il;
914
915 if (!line->driver_data)
916 line->driver_data = talloc_zero(line, struct ipaccess_line);
917
918 if (!line->driver_data) {
919 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
920 return -ENOMEM;
921 }
922 il = line->driver_data;
923
924 /* We only initialize this line once. */
925 if (il->line_already_initialized)
926 return 0;
927
928 il->line_already_initialized = 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200929
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200930 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200931 case E1INP_LINE_R_BSC: {
932 struct ipa_server_link *oml_link, *rsl_link;
933
Harald Weltecc2241b2011-07-19 16:06:06 +0200934 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode\n");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200935
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200936 oml_link = ipa_server_link_create(tall_ipa_ctx, line,
937 "0.0.0.0", IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200938 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200939 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200940 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200941 "BSC link: %s\n", strerror(errno));
942 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200943 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200944 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200945 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200946 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200947 ipa_server_link_destroy(oml_link);
948 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200949 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200950 rsl_link = ipa_server_link_create(tall_ipa_ctx, line,
951 "0.0.0.0", IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200952 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200953 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200954 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200955 "BSC link: %s\n", strerror(errno));
956 return -ENOMEM;
957 }
958 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200959 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200960 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200961 ipa_server_link_destroy(rsl_link);
962 return -EIO;
963 }
964 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200965 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200966 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200967 case E1INP_LINE_R_BTS: {
Harald Welte84f67b22013-06-30 13:13:59 +0200968 struct ipa_client_conn *link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200969
Harald Weltecc2241b2011-07-19 16:06:06 +0200970 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode\n");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200971
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200972 link = ipa_client_conn_create(tall_ipa_ctx,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200973 &line->ts[E1INP_SIGN_OML-1],
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +0200974 E1INP_SIGN_OML,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200975 line->ops->cfg.ipa.addr,
976 IPA_TCP_PORT_OML,
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200977 NULL,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200978 ipaccess_bts_cb,
979 ipaccess_bts_write_cb,
980 NULL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200981 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200982 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200983 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200984 return -ENOMEM;
985 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200986 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200987 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200988 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200989 ipa_client_conn_close(link);
990 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200991 return -EIO;
992 }
993 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200994 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200995 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200996 default:
997 break;
998 }
999 return ret;
1000}
1001
Harald Welte84f67b22013-06-30 13:13:59 +02001002int e1inp_ipa_bts_rsl_connect(struct e1inp_line *line,
1003 const char *rem_addr, uint16_t rem_port)
1004{
1005 struct ipa_client_conn *rsl_link;
1006
1007 rsl_link = ipa_client_conn_create(tall_ipa_ctx,
1008 &line->ts[E1INP_SIGN_RSL-1],
1009 E1INP_SIGN_RSL,
1010 rem_addr, rem_port,
1011 NULL,
1012 ipaccess_bts_cb,
1013 ipaccess_bts_write_cb,
1014 NULL);
1015 if (rsl_link == NULL) {
1016 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
1017 "BTS link: %s\n", strerror(errno));
1018 return -ENOMEM;
1019 }
1020 if (ipa_client_conn_open(rsl_link) < 0) {
1021 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
1022 strerror(errno));
1023 ipa_client_conn_close(rsl_link);
1024 ipa_client_conn_destroy(rsl_link);
1025 return -EIO;
1026 }
1027 return 0;
1028}
1029
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001030void e1inp_ipaccess_init(void)
1031{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +02001032 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001033 e1inp_driver_register(&ipaccess_driver);
1034}