blob: 5e76a095d8f7e225a57537d9790a732373f63634 [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 Ayuso0ba77d52011-06-05 18:32:44 +020047
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +020048static void *tall_ipa_ctx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020049
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020050#define TS1_ALLOC_SIZE 900
51
52/*
53 * Common propietary IPA messages:
54 * - PONG: in reply to PING.
55 * - ID_REQUEST: first messages once OML has been established.
56 * - ID_ACK: in reply to ID_ACK.
57 */
58const uint8_t ipa_pong_msg[] = {
59 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG
60};
61
62const uint8_t ipa_id_ack_msg[] = {
63 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK
64};
65
66const uint8_t ipa_id_req_msg[] = {
67 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET,
68 0x01, IPAC_IDTAG_UNIT,
69 0x01, IPAC_IDTAG_MACADDR,
70 0x01, IPAC_IDTAG_LOCATION1,
71 0x01, IPAC_IDTAG_LOCATION2,
72 0x01, IPAC_IDTAG_EQUIPVERS,
73 0x01, IPAC_IDTAG_SWVERSION,
74 0x01, IPAC_IDTAG_UNITNAME,
75 0x01, IPAC_IDTAG_SERNR,
76};
77
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020078static const char *idtag_names[] = {
79 [IPAC_IDTAG_SERNR] = "Serial_Number",
80 [IPAC_IDTAG_UNITNAME] = "Unit_Name",
81 [IPAC_IDTAG_LOCATION1] = "Location_1",
82 [IPAC_IDTAG_LOCATION2] = "Location_2",
83 [IPAC_IDTAG_EQUIPVERS] = "Equipment_Version",
84 [IPAC_IDTAG_SWVERSION] = "Software_Version",
85 [IPAC_IDTAG_IPADDR] = "IP_Address",
86 [IPAC_IDTAG_MACADDR] = "MAC_Address",
87 [IPAC_IDTAG_UNIT] = "Unit_ID",
88};
89
90const char *ipaccess_idtag_name(uint8_t tag)
91{
92 if (tag >= ARRAY_SIZE(idtag_names))
93 return "unknown";
94
95 return idtag_names[tag];
96}
97
98int ipaccess_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len)
99{
100 uint8_t t_len;
101 uint8_t t_tag;
102 uint8_t *cur = buf;
103
104 memset(dec, 0, sizeof(*dec));
105
106 while (len >= 2) {
107 len -= 2;
108 t_len = *cur++;
109 t_tag = *cur++;
110
111 if (t_len > len + 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200112 LOGP(DLMI, LOGL_ERROR, "The tag does not fit: %d\n", t_len);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200113 return -EINVAL;
114 }
115
Harald Weltecc2241b2011-07-19 16:06:06 +0200116 DEBUGPC(DLMI, "%s='%s' ", ipaccess_idtag_name(t_tag), cur);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200117
118 dec->lv[t_tag].len = t_len;
119 dec->lv[t_tag].val = cur;
120
121 cur += t_len;
122 len -= t_len;
123 }
124 return 0;
125}
126
127int ipaccess_parse_unitid(const char *str, struct ipaccess_unit *unit_data)
128{
129 unsigned long ul;
130 char *endptr;
131 const char *nptr;
132
133 nptr = str;
134 ul = strtoul(nptr, &endptr, 10);
135 if (endptr <= nptr)
136 return -EINVAL;
Pablo Neira Ayuso62d345a2011-07-05 16:37:37 +0200137 unit_data->site_id = ul & 0xffff;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200138
139 if (*endptr++ != '/')
140 return -EINVAL;
141
142 nptr = endptr;
143 ul = strtoul(nptr, &endptr, 10);
144 if (endptr <= nptr)
145 return -EINVAL;
Pablo Neira Ayuso62d345a2011-07-05 16:37:37 +0200146 unit_data->bts_id = ul & 0xffff;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200147
148 if (*endptr++ != '/')
149 return -EINVAL;
150
151 nptr = endptr;
152 ul = strtoul(nptr, &endptr, 10);
153 if (endptr <= nptr)
154 return -EINVAL;
Pablo Neira Ayuso62d345a2011-07-05 16:37:37 +0200155 unit_data->trx_id = ul & 0xffff;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200156
157 return 0;
158}
159
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200160static int ipaccess_send(int fd, const void *msg, size_t msglen)
161{
162 int ret;
163
164 ret = write(fd, msg, msglen);
165 if (ret < 0)
166 return ret;
167 if (ret < msglen) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200168 LOGP(DLINP, LOGL_ERROR, "ipaccess_send: short write\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200169 return -EIO;
170 }
171 return ret;
172}
173
174int ipaccess_send_pong(int fd)
175{
176 return ipaccess_send(fd, ipa_pong_msg, sizeof(ipa_pong_msg));
177}
178
179int ipaccess_send_id_ack(int fd)
180{
181 return ipaccess_send(fd, ipa_id_ack_msg, sizeof(ipa_id_ack_msg));
182}
183
184int ipaccess_send_id_req(int fd)
185{
186 return ipaccess_send(fd, ipa_id_req_msg, sizeof(ipa_id_req_msg));
187}
188
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200189/* base handling of the ip.access protocol */
Harald Weltebc25bca2011-08-19 22:32:38 +0200190int ipaccess_rcvmsg_base(struct msgb *msg, struct osmo_fd *bfd)
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200191{
192 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200193 int ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200194
195 switch (msg_type) {
196 case IPAC_MSGT_PING:
197 ret = ipaccess_send_pong(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200198 if (ret < 0) {
199 LOGP(DLINP, LOGL_ERROR, "Cannot send PING "
200 "message. Reason: %s\n", strerror(errno));
201 break;
202 }
203 ret = 1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200204 break;
205 case IPAC_MSGT_PONG:
Harald Weltecc2241b2011-07-19 16:06:06 +0200206 DEBUGP(DLMI, "PONG!\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200207 ret = 1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200208 break;
209 case IPAC_MSGT_ID_ACK:
Harald Weltecc2241b2011-07-19 16:06:06 +0200210 DEBUGP(DLMI, "ID_ACK? -> ACK!\n");
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200211 ret = ipaccess_send_id_ack(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200212 if (ret < 0) {
213 LOGP(DLINP, LOGL_ERROR, "Cannot send ID_ACK "
214 "message. Reason: %s\n", strerror(errno));
215 break;
216 }
217 ret = 1;
218 break;
219 default:
220 /* This is not an IPA PING, PONG or ID_ACK message */
221 ret = 0;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200222 break;
223 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200224 return ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200225}
226
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200227/* base handling of the ip.access protocol */
228int ipaccess_rcvmsg_bts_base(struct msgb *msg,
229 struct osmo_fd *bfd)
230{
231 uint8_t msg_type = *(msg->l2h);
232 int ret = 0;
233
234 switch (msg_type) {
235 case IPAC_MSGT_PING:
236 ret = ipaccess_send_pong(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200237 if (ret < 0) {
238 LOGP(DLINP, LOGL_ERROR, "Cannot send PONG "
239 "message. Reason: %s\n", strerror(errno));
240 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200241 break;
242 case IPAC_MSGT_PONG:
Harald Weltecc2241b2011-07-19 16:06:06 +0200243 DEBUGP(DLMI, "PONG!\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200244 break;
245 case IPAC_MSGT_ID_ACK:
Harald Weltecc2241b2011-07-19 16:06:06 +0200246 DEBUGP(DLMI, "ID_ACK\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200247 break;
248 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200249 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200250}
251
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200252static int ipaccess_drop(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200253{
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200254 int ret = 1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200255 struct e1inp_line *line = bfd->data;
256
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200257 /* Error case: we did not see any ID_RESP yet for this socket. */
258 if (bfd->fd != -1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200259 LOGP(DLINP, LOGL_ERROR, "Forcing socket shutdown with "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200260 "no signal link set\n");
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +0200261 osmo_fd_unregister(bfd);
262 close(bfd->fd);
263 bfd->fd = -1;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200264 ret = -ENOENT;
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +0200265 }
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200266
267 /* e1inp_sign_link_destroy releases the socket descriptors for us. */
268 line->ops->sign_link_down(line);
269
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200270 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200271}
272
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200273static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
274 struct osmo_fd *bfd)
275{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200276 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200277 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200278 struct ipaccess_unit unit_data = {};
279 struct e1inp_sign_link *sign_link;
280 char *unitid;
281 int len, ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200282
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200283 /* Handle IPA PING, PONG and ID_ACK messages. */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200284 ret = ipaccess_rcvmsg_base(msg, bfd);
285 switch(ret) {
286 case -1:
287 /* error in IPA control message handling */
288 goto err;
289 case 1:
290 /* this is an IPA control message, skip further processing */
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200291 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200292 case 0:
293 /* this is not an IPA control message, continue */
294 break;
295 default:
296 LOGP(DLINP, LOGL_ERROR, "Unexpected return from "
297 "ipaccess_rcvmsg_base "
298 "(ret=%d)\n", ret);
299 goto err;
300 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200301
302 switch (msg_type) {
303 case IPAC_MSGT_ID_RESP:
Harald Weltecc2241b2011-07-19 16:06:06 +0200304 DEBUGP(DLMI, "ID_RESP\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200305 /* parse tags, search for Unit ID */
306 ret = ipaccess_idtag_parse(&tlvp, (uint8_t *)msg->l2h + 2,
307 msgb_l2len(msg)-2);
Harald Weltecc2241b2011-07-19 16:06:06 +0200308 DEBUGP(DLMI, "\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200309 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200310 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200311 "with malformed TLVs\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200312 ret = -EINVAL;
313 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200314 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200315 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200316 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200317 "without unit ID\n");
318 ret = -EINVAL;
319 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200320
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200321 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200322 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200323 if (len < 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200324 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200325 "with too small unit ID\n");
326 ret = -EINVAL;
327 goto err;
328 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200329 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
330 unitid[len - 1] = '\0';
331 ipaccess_parse_unitid(unitid, &unit_data);
332
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200333 if (!line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200334 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200335 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200336 ret = -EINVAL;
337 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200338 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200339 /* the BSC creates the new sign links at this stage. */
340 if (bfd->priv_nr == E1INP_SIGN_OML) {
341 sign_link =
342 line->ops->sign_link_up(&unit_data, line,
343 E1INP_SIGN_OML);
344 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200345 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200346 "Unable to set signal link, "
347 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200348 ret = -EINVAL;
349 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200350 }
351 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200352 struct e1inp_ts *ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200353 struct osmo_fd *newbfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200354 struct e1inp_line *new_line;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200355
356 sign_link =
357 line->ops->sign_link_up(&unit_data, line,
358 E1INP_SIGN_RSL);
359 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200360 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200361 "Unable to set signal link, "
362 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200363 ret = -EINVAL;
364 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200365 }
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200366 /* this is a bugtrap, the BSC should be using the
367 * virtual E1 line used by OML for this RSL link. */
368 if (sign_link->ts->line == line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200369 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200370 "Fix your BSC, you should use the "
371 "E1 line used by the OML link for "
372 "your RSL link.\n");
373 return 0;
374 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200375 /* Finally, we know which OML link is associated with
376 * this RSL link, attach it to this socket. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200377 bfd->data = new_line = sign_link->ts->line;
378 e1inp_line_get(new_line);
379 ts = &new_line->ts[E1INP_SIGN_RSL+unit_data.trx_id-1];
380 newbfd = &ts->driver.ipaccess.fd;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200381
382 /* get rid of our old temporary bfd */
383 memcpy(newbfd, bfd, sizeof(*newbfd));
384 newbfd->priv_nr = E1INP_SIGN_RSL + unit_data.trx_id;
385 osmo_fd_unregister(bfd);
386 bfd->fd = -1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200387 osmo_fd_register(newbfd);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200388 /* now we can release the dummy RSL line. */
389 e1inp_line_put(line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200390 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200391 break;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200392 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200393 LOGP(DLINP, LOGL_ERROR, "Unknown IPA message type\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200394 ret = -EINVAL;
395 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200396 }
397 return 0;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200398err:
399 osmo_fd_unregister(bfd);
400 close(bfd->fd);
401 bfd->fd = -1;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200402 e1inp_line_put(line);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200403 return ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200404}
405
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200406static int handle_ts1_read(struct osmo_fd *bfd)
407{
408 struct e1inp_line *line = bfd->data;
409 unsigned int ts_nr = bfd->priv_nr;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200410 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200411 struct e1inp_sign_link *link;
412 struct ipaccess_head *hh;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200413 struct msgb *msg;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200414 int ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200415
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200416 ret = ipa_msg_recv(bfd->fd, &msg);
417 if (ret < 0) {
418 LOGP(DLINP, LOGL_NOTICE, "Sign link problems, "
419 "closing socket. Reason: %s\n", strerror(errno));
420 goto err;
421 } else if (ret == 0) {
422 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
423 goto err;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200424 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200425 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200426
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200427 hh = (struct ipaccess_head *) msg->data;
428 if (hh->proto == IPAC_PROTO_IPACCESS) {
429 ipaccess_rcvmsg(line, msg, bfd);
430 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200431 return 0;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200432 }
433 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
434 * might have free'd it !!! */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200435
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200436 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
437 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200438 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200439 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200440 ret = -EINVAL;
441 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200442 }
443 msg->dst = link;
444
445 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200446 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200447 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200448 "no action set for signalling messages.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200449 ret = -EINVAL;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200450 goto err_msg;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200451 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200452 if (e1i_ts->line->ops->sign_link(msg) < 0) {
Pablo Neira Ayusoa49c24d2012-10-16 11:24:08 +0200453 /* Don't close the signalling link if the upper layers report
454 * an error, that's too strict. BTW, the signalling layer is
455 * resposible for releasing the message.
456 */
Harald Weltecc2241b2011-07-19 16:06:06 +0200457 LOGP(DLINP, LOGL_ERROR, "Bad signalling message,"
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200458 "sign_link returned error\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200459 }
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200460
461 return 0;
462err_msg:
463 msgb_free(msg);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200464err:
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200465 ipaccess_drop(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200466 return ret;
467}
468
469void ipaccess_prepend_header_ext(struct msgb *msg, int proto)
470{
471 struct ipaccess_head_ext *hh_ext;
472
473 /* prepend the osmo ip.access header extension */
474 hh_ext = (struct ipaccess_head_ext *) msgb_push(msg, sizeof(*hh_ext));
475 hh_ext->proto = proto;
476}
477
478void ipaccess_prepend_header(struct msgb *msg, int proto)
479{
480 struct ipaccess_head *hh;
481
482 /* prepend the ip.access header */
483 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
484 hh->len = htons(msg->len - sizeof(*hh));
485 hh->proto = proto;
486}
487
488static int ts_want_write(struct e1inp_ts *e1i_ts)
489{
490 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
491
492 return 0;
493}
494
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200495static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200496{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200497 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200498 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100499 return e1inp_close_socket(e1i_ts, sign_link, bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200500}
501
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200502static void timeout_ts1_write(void *data)
503{
504 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
505
506 /* trigger write of ts1, due to tx delay timer */
507 ts_want_write(e1i_ts);
508}
509
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200510static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200511{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200512 unsigned int ts_nr = bfd->priv_nr;
513 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
514 struct e1inp_sign_link *sign_link;
515 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200516 int ret;
517
518 bfd->when &= ~BSC_FD_WRITE;
519
520 /* get the next msg for this timeslot */
521 msg = e1inp_tx_ts(e1i_ts, &sign_link);
522 if (!msg) {
523 /* no message after tx delay timer */
524 return 0;
525 }
526
527 switch (sign_link->type) {
528 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200529 break;
530 case E1INP_SIGN_RSL:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200531 break;
532 default:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200533 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200534 ret = -EINVAL;
535 goto out;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200536 }
537
538 msg->l2h = msg->data;
Pablo Neira Ayusob9ed7e32011-07-05 18:31:59 +0200539 ipaccess_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200540
Harald Weltecc2241b2011-07-19 16:06:06 +0200541 DEBUGP(DLMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200542
543 ret = send(bfd->fd, msg->data, msg->len, 0);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200544 if (ret != msg->len) {
545 LOGP(DLINP, LOGL_ERROR, "failed to send A-bis IPA signalling "
546 "message. Reason: %s\n", strerror(errno));
547 goto err;
548 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200549
550 /* set tx delay timer for next event */
551 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
552 e1i_ts->sign.tx_timer.data = e1i_ts;
553
554 /* Reducing this might break the nanoBTS 900 init. */
555 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
556
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200557out:
558 msgb_free(msg);
559 return ret;
560err:
561 ipaccess_drop(bfd);
562 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200563 return ret;
564}
565
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200566static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200567{
568 struct e1inp_line *line = bfd->data;
569
570 return __handle_ts1_write(bfd, line);
571}
572
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200573static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200574{
575 struct e1inp_line *line = link->line;
576
577 return __handle_ts1_write(link->ofd, line);
578}
579
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200580/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200581int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200582{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200583 int rc = 0;
584
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200585 if (what & BSC_FD_READ)
586 rc = handle_ts1_read(bfd);
587 if (what & BSC_FD_WRITE)
588 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200589
590 return rc;
591}
592
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200593static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200594
595struct e1inp_driver ipaccess_driver = {
596 .name = "ipa",
597 .want_write = ts_want_write,
598 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200599 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200600 .default_delay = 0,
601};
602
603/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200604static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200605{
606 int ret;
607 int idx = 0;
608 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200609 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200610 struct e1inp_ts *e1i_ts;
611 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200612
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200613 /* clone virtual E1 line for this new OML link. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200614 line = e1inp_line_clone(tall_ipa_ctx, link->line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200615 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200616 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200617 return -ENOMEM;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200618 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200619
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200620 /* create virrtual E1 timeslots for signalling */
Pablo Neira Ayuso59301852011-06-27 15:44:23 +0200621 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200622
623 /* initialize the fds */
624 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
625 line->ts[i].driver.ipaccess.fd.fd = -1;
626
627 e1i_ts = &line->ts[idx];
628
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200629 bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200630 bfd->fd = fd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200631 bfd->data = line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200632 bfd->priv_nr = E1INP_SIGN_OML;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200633 bfd->cb = ipaccess_fd_cb;
634 bfd->when = BSC_FD_READ;
635 ret = osmo_fd_register(bfd);
636 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200637 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200638 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200639 }
640
641 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
642 ret = ipaccess_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200643 if (ret < 0) {
644 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
645 strerror(errno));
646 goto err_socket;
647 }
648 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200649
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200650err_socket:
651 osmo_fd_unregister(bfd);
652err_line:
653 close(bfd->fd);
654 bfd->fd = -1;
655 e1inp_line_put(line);
656 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200657}
658
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200659static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200660{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200661 struct e1inp_line *line;
662 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200663 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200664 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200665
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200666 /* We don't know yet which OML link to associate it with. Thus, we
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200667 * allocate a temporary E1 line until we have received ID. */
668 line = e1inp_line_clone(tall_ipa_ctx, link->line);
669 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200670 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200671 return -ENOMEM;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200672 }
673 /* initialize the fds */
674 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
675 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200676
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200677 /* we need this to initialize this in case to avoid crashes in case
678 * that the socket is closed before we've seen an ID_RESP. */
679 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line);
680
681 e1i_ts = &line->ts[E1INP_SIGN_RSL-1];
682
683 bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200684 bfd->fd = fd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200685 bfd->data = line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200686 bfd->priv_nr = E1INP_SIGN_RSL;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200687 bfd->cb = ipaccess_fd_cb;
688 bfd->when = BSC_FD_READ;
689 ret = osmo_fd_register(bfd);
690 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200691 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200692 goto err_line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200693 }
694 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
695 ret = ipaccess_send_id_req(bfd->fd);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200696 if (ret < 0) {
697 LOGP(DLINP, LOGL_ERROR, "could not send ID REQ. Reason: %s\n",
698 strerror(errno));
699 goto err_socket;
700 }
701 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200702
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200703err_socket:
704 osmo_fd_unregister(bfd);
705err_line:
706 close(bfd->fd);
707 bfd->fd = -1;
708 e1inp_line_put(line);
709 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200710}
711
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200712static struct msgb *
713ipa_bts_id_resp(struct ipaccess_unit *dev, uint8_t *data, int len)
714{
715 struct msgb *nmsg;
716 char str[64];
717 uint8_t *tag;
718
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200719 nmsg = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200720 if (!nmsg)
721 return NULL;
722
723 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
724 while (len) {
725 if (len < 2) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200726 LOGP(DLINP, LOGL_NOTICE,
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200727 "Short read of ipaccess tag\n");
728 msgb_free(nmsg);
729 return NULL;
730 }
731 switch (data[1]) {
732 case IPAC_IDTAG_UNIT:
733 sprintf(str, "%u/%u/%u",
734 dev->site_id, dev->bts_id, dev->trx_id);
735 break;
736 case IPAC_IDTAG_MACADDR:
737 sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
738 dev->mac_addr[0], dev->mac_addr[1],
739 dev->mac_addr[2], dev->mac_addr[3],
740 dev->mac_addr[4], dev->mac_addr[5]);
741 break;
742 case IPAC_IDTAG_LOCATION1:
743 strcpy(str, dev->location1);
744 break;
745 case IPAC_IDTAG_LOCATION2:
746 strcpy(str, dev->location2);
747 break;
748 case IPAC_IDTAG_EQUIPVERS:
749 strcpy(str, dev->equipvers);
750 break;
751 case IPAC_IDTAG_SWVERSION:
752 strcpy(str, dev->swversion);
753 break;
754 case IPAC_IDTAG_UNITNAME:
755 sprintf(str, "%s-%02x-%02x-%02x-%02x-%02x-%02x",
756 dev->unit_name,
757 dev->mac_addr[0], dev->mac_addr[1],
758 dev->mac_addr[2], dev->mac_addr[3],
759 dev->mac_addr[4], dev->mac_addr[5]);
760 break;
761 case IPAC_IDTAG_SERNR:
762 strcpy(str, dev->serno);
763 break;
764 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200765 LOGP(DLINP, LOGL_NOTICE,
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200766 "Unknown ipaccess tag 0x%02x\n", *data);
767 msgb_free(nmsg);
768 return NULL;
769 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200770 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200771 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
772 tag[0] = 0x00;
773 tag[1] = 1 + strlen(str) + 1;
774 tag[2] = data[1];
775 memcpy(tag + 3, str, strlen(str) + 1);
776 data += 2;
777 len -= 2;
778 }
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200779 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200780 return nmsg;
781}
782
783static struct msgb *ipa_bts_id_ack(void)
784{
785 struct msgb *nmsg2;
786
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200787 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200788 if (!nmsg2)
789 return NULL;
790
791 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200792 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200793
794 return nmsg2;
795}
796
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200797static int ipaccess_bts_cb(struct ipa_client_conn *link, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200798{
799 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
800 struct e1inp_ts *e1i_ts = NULL;
801 struct e1inp_sign_link *sign_link;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200802 struct msgb *rmsg;
803 int ret = 0;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200804
805 /* special handling for IPA CCM. */
806 if (hh->proto == IPAC_PROTO_IPACCESS) {
807 uint8_t msg_type = *(msg->l2h);
808
809 /* ping, pong and acknowledgment cases. */
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200810 ret = ipaccess_rcvmsg_bts_base(msg, link->ofd);
811 if (ret < 0)
812 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200813
814 /* this is a request for identification from the BSC. */
815 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200816 struct e1inp_sign_link *sign_link;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200817 uint8_t *data = msgb_l2(msg);
818 int len = msgb_l2len(msg);
819
Harald Weltecc2241b2011-07-19 16:06:06 +0200820 LOGP(DLINP, LOGL_NOTICE, "received ID get\n");
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200821 if (!link->line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200822 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200823 "Unable to set signal link, "
824 "closing socket.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200825 ret = -EINVAL;
826 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200827 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200828 sign_link = link->line->ops->sign_link_up(msg,
829 link->line,
830 link->ofd->priv_nr);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200831 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200832 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200833 "Unable to set signal link, "
834 "closing socket.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200835 ret = -EINVAL;
836 goto err;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200837 }
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200838 rmsg = ipa_bts_id_resp(link->line->ops->cfg.ipa.dev,
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200839 data + 1, len - 1);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200840 ret = ipaccess_send(link->ofd->fd, rmsg->data,
841 rmsg->len);
842 if (ret != rmsg->len) {
843 LOGP(DLINP, LOGL_ERROR, "cannot send ID_RESP "
844 "message. Reason: %s\n", strerror(errno));
845 goto err_rmsg;
846 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200847 msgb_free(rmsg);
848
849 /* send ID_ACK. */
850 rmsg = ipa_bts_id_ack();
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200851 ret = ipaccess_send(link->ofd->fd, rmsg->data,
852 rmsg->len);
853 if (ret != rmsg->len) {
854 LOGP(DLINP, LOGL_ERROR, "cannot send ID_ACK "
855 "message. Reason: %s\n", strerror(errno));
856 goto err_rmsg;
857 }
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200858 msgb_free(rmsg);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200859 }
Pablo Neira Ayuso84e5cb92012-08-23 23:41:54 +0200860 msgb_free(msg);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200861 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200862 } else if (link->port == IPA_TCP_PORT_OML)
863 e1i_ts = &link->line->ts[0];
864 else if (link->port == IPA_TCP_PORT_RSL)
865 e1i_ts = &link->line->ts[1];
866
867 /* look up for some existing signaling link. */
868 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
869 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200870 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200871 "hh->proto=0x%02x\n", hh->proto);
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200872 ret = -EIO;
873 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200874 }
875 msg->dst = sign_link;
876
877 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200878 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200879 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200880 "no action set for signalling messages.\n");
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200881 ret = -ENOENT;
882 goto err;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200883 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200884 link->line->ops->sign_link(msg);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200885 return 0;
Pablo Neira Ayuso1e401942012-08-22 14:16:24 +0200886
887err_rmsg:
888 msgb_free(rmsg);
889err:
890 osmo_fd_unregister(link->ofd);
891 close(link->ofd->fd);
892 link->ofd->fd = -1;
893 msgb_free(msg);
894 return ret;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200895}
896
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200897struct ipaccess_line {
898 int line_already_initialized;
899};
900
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200901static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200902{
903 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200904 struct ipaccess_line *il;
905
906 if (!line->driver_data)
907 line->driver_data = talloc_zero(line, struct ipaccess_line);
908
909 if (!line->driver_data) {
910 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
911 return -ENOMEM;
912 }
913 il = line->driver_data;
914
915 /* We only initialize this line once. */
916 if (il->line_already_initialized)
917 return 0;
918
919 il->line_already_initialized = 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200920
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200921 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200922 case E1INP_LINE_R_BSC: {
923 struct ipa_server_link *oml_link, *rsl_link;
924
Harald Weltecc2241b2011-07-19 16:06:06 +0200925 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode\n");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200926
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200927 oml_link = ipa_server_link_create(tall_ipa_ctx, line,
928 "0.0.0.0", IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200929 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200930 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200931 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200932 "BSC link: %s\n", strerror(errno));
933 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200934 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200935 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200936 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200937 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200938 ipa_server_link_destroy(oml_link);
939 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200940 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200941 rsl_link = ipa_server_link_create(tall_ipa_ctx, line,
942 "0.0.0.0", IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200943 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200944 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200945 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200946 "BSC link: %s\n", strerror(errno));
947 return -ENOMEM;
948 }
949 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200950 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200951 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200952 ipa_server_link_destroy(rsl_link);
953 return -EIO;
954 }
955 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200956 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200957 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200958 case E1INP_LINE_R_BTS: {
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200959 struct ipa_client_conn *link, *rsl_link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200960
Harald Weltecc2241b2011-07-19 16:06:06 +0200961 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode\n");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200962
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200963 link = ipa_client_conn_create(tall_ipa_ctx,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200964 &line->ts[E1INP_SIGN_OML-1],
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +0200965 E1INP_SIGN_OML,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200966 line->ops->cfg.ipa.addr,
967 IPA_TCP_PORT_OML,
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200968 NULL,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200969 ipaccess_bts_cb,
970 ipaccess_bts_write_cb,
971 NULL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200972 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200973 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200974 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200975 return -ENOMEM;
976 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200977 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200978 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200979 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200980 ipa_client_conn_close(link);
981 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200982 return -EIO;
983 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200984 rsl_link = ipa_client_conn_create(tall_ipa_ctx,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200985 &line->ts[E1INP_SIGN_RSL-1],
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +0200986 E1INP_SIGN_RSL,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200987 line->ops->cfg.ipa.addr,
988 IPA_TCP_PORT_RSL,
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200989 NULL,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200990 ipaccess_bts_cb,
991 ipaccess_bts_write_cb,
992 NULL);
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200993 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200994 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200995 "BTS link: %s\n", strerror(errno));
996 return -ENOMEM;
997 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200998 if (ipa_client_conn_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200999 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001000 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +02001001 ipa_client_conn_close(rsl_link);
1002 ipa_client_conn_destroy(rsl_link);
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +02001003 return -EIO;
1004 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001005 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001006 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001007 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001008 default:
1009 break;
1010 }
1011 return ret;
1012}
1013
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001014void e1inp_ipaccess_init(void)
1015{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +02001016 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001017 e1inp_driver_register(&ipaccess_driver);
1018}