blob: 6ae9ab309c3e420913f272377172fea4ccf32e6c [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{
Harald Weltebc25bca2011-08-19 22:32:38 +0200192 int ipa_ccm = 0;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200193 uint8_t msg_type = *(msg->l2h);
194 int ret = 0;
195
196 switch (msg_type) {
197 case IPAC_MSGT_PING:
Harald Weltebc25bca2011-08-19 22:32:38 +0200198 ipa_ccm = 1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200199 ret = ipaccess_send_pong(bfd->fd);
200 break;
201 case IPAC_MSGT_PONG:
Harald Weltecc2241b2011-07-19 16:06:06 +0200202 DEBUGP(DLMI, "PONG!\n");
Harald Weltebc25bca2011-08-19 22:32:38 +0200203 ipa_ccm = 1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200204 break;
205 case IPAC_MSGT_ID_ACK:
Harald Weltecc2241b2011-07-19 16:06:06 +0200206 DEBUGP(DLMI, "ID_ACK? -> ACK!\n");
Harald Weltebc25bca2011-08-19 22:32:38 +0200207 ipa_ccm = 1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200208 ret = ipaccess_send_id_ack(bfd->fd);
209 break;
210 }
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200211 return ipa_ccm;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200212}
213
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200214/* base handling of the ip.access protocol */
215int ipaccess_rcvmsg_bts_base(struct msgb *msg,
216 struct osmo_fd *bfd)
217{
218 uint8_t msg_type = *(msg->l2h);
219 int ret = 0;
220
221 switch (msg_type) {
222 case IPAC_MSGT_PING:
223 ret = ipaccess_send_pong(bfd->fd);
224 break;
225 case IPAC_MSGT_PONG:
Harald Weltecc2241b2011-07-19 16:06:06 +0200226 DEBUGP(DLMI, "PONG!\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200227 break;
228 case IPAC_MSGT_ID_ACK:
Harald Weltecc2241b2011-07-19 16:06:06 +0200229 DEBUGP(DLMI, "ID_ACK\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200230 break;
231 }
232 return 0;
233}
234
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200235static int ipaccess_drop(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200236{
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200237 int ret = 0;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200238 struct e1inp_line *line = bfd->data;
239
240 /* e1inp_sign_link_destroy releases the socket descriptors for us. */
241 line->ops->sign_link_down(line);
242
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200243 /* Error case: we did not see any ID_RESP yet for this socket. */
244 if (bfd->fd != -1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200245 LOGP(DLINP, LOGL_ERROR, "Forcing socket shutdown with "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200246 "no signal link set\n");
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +0200247 osmo_fd_unregister(bfd);
248 close(bfd->fd);
249 bfd->fd = -1;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200250 ret = -ENOENT;
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +0200251 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200252 /* put the virtual E1 line that we cloned for this socket, if
253 * it becomes unused, it gets released. */
254 e1inp_line_put(line);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200255 return ret;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200256}
257
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200258static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
259 struct osmo_fd *bfd)
260{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200261 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200262 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200263 struct ipaccess_unit unit_data = {};
264 struct e1inp_sign_link *sign_link;
265 char *unitid;
266 int len, ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200267
Pablo Neira Ayusoeb434132011-07-07 19:19:46 +0200268 /* Handle IPA PING, PONG and ID_ACK messages. */
269 if (ipaccess_rcvmsg_base(msg, bfd))
270 return 0;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200271
272 switch (msg_type) {
273 case IPAC_MSGT_ID_RESP:
Harald Weltecc2241b2011-07-19 16:06:06 +0200274 DEBUGP(DLMI, "ID_RESP\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200275 /* parse tags, search for Unit ID */
276 ret = ipaccess_idtag_parse(&tlvp, (uint8_t *)msg->l2h + 2,
277 msgb_l2len(msg)-2);
Harald Weltecc2241b2011-07-19 16:06:06 +0200278 DEBUGP(DLMI, "\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200279 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200280 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200281 "with malformed TLVs\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200282 ret = -EINVAL;
283 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200284 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200285 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200286 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200287 "without unit ID\n");
288 ret = -EINVAL;
289 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200290
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200291 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200292 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200293 if (len < 1) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200294 LOGP(DLINP, LOGL_ERROR, "IPA response message "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200295 "with too small unit ID\n");
296 ret = -EINVAL;
297 goto err;
298 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200299 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
300 unitid[len - 1] = '\0';
301 ipaccess_parse_unitid(unitid, &unit_data);
302
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200303 if (!line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200304 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200305 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200306 ret = -EINVAL;
307 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200308 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200309 /* the BSC creates the new sign links at this stage. */
310 if (bfd->priv_nr == E1INP_SIGN_OML) {
311 sign_link =
312 line->ops->sign_link_up(&unit_data, line,
313 E1INP_SIGN_OML);
314 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200315 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200316 "Unable to set signal link, "
317 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200318 ret = -EINVAL;
319 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200320 }
321 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200322 struct e1inp_ts *ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200323 struct osmo_fd *newbfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200324 struct e1inp_line *new_line;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200325
326 sign_link =
327 line->ops->sign_link_up(&unit_data, line,
328 E1INP_SIGN_RSL);
329 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200330 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200331 "Unable to set signal link, "
332 "closing socket.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200333 ret = -EINVAL;
334 goto err;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200335 }
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200336 /* this is a bugtrap, the BSC should be using the
337 * virtual E1 line used by OML for this RSL link. */
338 if (sign_link->ts->line == line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200339 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusodb1c8a72011-07-08 15:12:03 +0200340 "Fix your BSC, you should use the "
341 "E1 line used by the OML link for "
342 "your RSL link.\n");
343 return 0;
344 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200345 /* Finally, we know which OML link is associated with
346 * this RSL link, attach it to this socket. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200347 bfd->data = new_line = sign_link->ts->line;
348 e1inp_line_get(new_line);
349 ts = &new_line->ts[E1INP_SIGN_RSL+unit_data.trx_id-1];
350 newbfd = &ts->driver.ipaccess.fd;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200351
352 /* get rid of our old temporary bfd */
353 memcpy(newbfd, bfd, sizeof(*newbfd));
354 newbfd->priv_nr = E1INP_SIGN_RSL + unit_data.trx_id;
355 osmo_fd_unregister(bfd);
356 bfd->fd = -1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200357 osmo_fd_register(newbfd);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200358 /* now we can release the dummy RSL line. */
359 e1inp_line_put(line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200360 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200361 break;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200362 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200363 LOGP(DLINP, LOGL_ERROR, "Unknown IPA message type\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200364 ret = -EINVAL;
365 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200366 }
367 return 0;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200368err:
369 osmo_fd_unregister(bfd);
370 close(bfd->fd);
371 bfd->fd = -1;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200372 e1inp_line_put(line);
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200373 return ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200374}
375
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200376static int handle_ts1_read(struct osmo_fd *bfd)
377{
378 struct e1inp_line *line = bfd->data;
379 unsigned int ts_nr = bfd->priv_nr;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200380 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200381 struct e1inp_sign_link *link;
382 struct ipaccess_head *hh;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200383 struct msgb *msg;
384 int ret = 0, error;
385
Pablo Neira Ayuso7a249402011-06-21 14:15:46 +0200386 error = ipa_msg_recv(bfd->fd, &msg);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200387 if (error < 0)
388 return error;
389 else if (error == 0) {
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200390 if (ipaccess_drop(bfd) >= 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200391 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, "
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200392 "dead socket\n");
393 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200394 return error;
395 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200396 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200397
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200398 hh = (struct ipaccess_head *) msg->data;
399 if (hh->proto == IPAC_PROTO_IPACCESS) {
400 ipaccess_rcvmsg(line, msg, bfd);
401 msgb_free(msg);
402 return ret;
403 }
404 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
405 * might have free'd it !!! */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200406
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200407 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
408 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200409 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200410 "hh->proto=0x%02x\n", hh->proto);
411 msgb_free(msg);
412 return -EIO;
413 }
414 msg->dst = link;
415
416 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200417 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200418 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200419 "no action set for signalling messages.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200420 ret = -EINVAL;
421 goto err;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200422 }
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200423 if (e1i_ts->line->ops->sign_link(msg) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200424 LOGP(DLINP, LOGL_ERROR, "Bad signalling message,"
Holger Hans Peter Freythere194cd92011-11-07 13:37:56 +0100425 "sign_link returned error.\n");
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200426 ret = -EINVAL;
Pablo Neira Ayuso466c5462011-07-07 19:17:20 +0200427 }
428 return ret;
429err:
430 osmo_fd_unregister(bfd);
431 close(bfd->fd);
432 bfd->fd = -1;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200433 e1inp_line_put(line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200434 return ret;
435}
436
437void ipaccess_prepend_header_ext(struct msgb *msg, int proto)
438{
439 struct ipaccess_head_ext *hh_ext;
440
441 /* prepend the osmo ip.access header extension */
442 hh_ext = (struct ipaccess_head_ext *) msgb_push(msg, sizeof(*hh_ext));
443 hh_ext->proto = proto;
444}
445
446void ipaccess_prepend_header(struct msgb *msg, int proto)
447{
448 struct ipaccess_head *hh;
449
450 /* prepend the ip.access header */
451 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
452 hh->len = htons(msg->len - sizeof(*hh));
453 hh->proto = proto;
454}
455
456static int ts_want_write(struct e1inp_ts *e1i_ts)
457{
458 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
459
460 return 0;
461}
462
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200463static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200464{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200465 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200466 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100467 return e1inp_close_socket(e1i_ts, sign_link, bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200468}
469
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200470static void timeout_ts1_write(void *data)
471{
472 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
473
474 /* trigger write of ts1, due to tx delay timer */
475 ts_want_write(e1i_ts);
476}
477
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200478static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200479{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200480 unsigned int ts_nr = bfd->priv_nr;
481 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
482 struct e1inp_sign_link *sign_link;
483 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200484 int ret;
485
486 bfd->when &= ~BSC_FD_WRITE;
487
488 /* get the next msg for this timeslot */
489 msg = e1inp_tx_ts(e1i_ts, &sign_link);
490 if (!msg) {
491 /* no message after tx delay timer */
492 return 0;
493 }
494
495 switch (sign_link->type) {
496 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200497 break;
498 case E1INP_SIGN_RSL:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200499 break;
500 default:
501 msgb_free(msg);
502 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
503 return -EINVAL;
504 }
505
506 msg->l2h = msg->data;
Pablo Neira Ayusob9ed7e32011-07-05 18:31:59 +0200507 ipaccess_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200508
Harald Weltecc2241b2011-07-19 16:06:06 +0200509 DEBUGP(DLMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200510
511 ret = send(bfd->fd, msg->data, msg->len, 0);
512 msgb_free(msg);
513
514 /* set tx delay timer for next event */
515 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
516 e1i_ts->sign.tx_timer.data = e1i_ts;
517
518 /* Reducing this might break the nanoBTS 900 init. */
519 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
520
521 return ret;
522}
523
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200524static int handle_ts1_write(struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200525{
526 struct e1inp_line *line = bfd->data;
527
528 return __handle_ts1_write(bfd, line);
529}
530
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200531static int ipaccess_bts_write_cb(struct ipa_client_conn *link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200532{
533 struct e1inp_line *line = link->line;
534
535 return __handle_ts1_write(link->ofd, line);
536}
537
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200538/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso495ddb62011-07-08 21:04:11 +0200539int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200540{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200541 int rc = 0;
542
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200543 if (what & BSC_FD_READ)
544 rc = handle_ts1_read(bfd);
545 if (what & BSC_FD_WRITE)
546 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200547
548 return rc;
549}
550
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200551static int ipaccess_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200552
553struct e1inp_driver ipaccess_driver = {
554 .name = "ipa",
555 .want_write = ts_want_write,
556 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200557 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200558 .default_delay = 0,
559};
560
561/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200562static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200563{
564 int ret;
565 int idx = 0;
566 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200567 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200568 struct e1inp_ts *e1i_ts;
569 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200570
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200571 /* clone virtual E1 line for this new OML link. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200572 line = e1inp_line_clone(tall_ipa_ctx, link->line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200573 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200574 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200575 return -1;
576 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200577
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200578 /* create virrtual E1 timeslots for signalling */
Pablo Neira Ayuso59301852011-06-27 15:44:23 +0200579 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200580
581 /* initialize the fds */
582 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
583 line->ts[i].driver.ipaccess.fd.fd = -1;
584
585 e1i_ts = &line->ts[idx];
586
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200587 bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200588 bfd->fd = fd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200589 bfd->data = line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200590 bfd->priv_nr = E1INP_SIGN_OML;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200591 bfd->cb = ipaccess_fd_cb;
592 bfd->when = BSC_FD_READ;
593 ret = osmo_fd_register(bfd);
594 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200595 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200596 close(bfd->fd);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200597 e1inp_line_put(line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200598 return ret;
599 }
600
601 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
602 ret = ipaccess_send_id_req(bfd->fd);
603
604 return ret;
605}
606
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200607static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200608{
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200609 struct e1inp_line *line;
610 struct e1inp_ts *e1i_ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200611 struct osmo_fd *bfd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200612 int i, ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200613
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200614 /* We don't know yet which OML link to associate it with. Thus, we
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200615 * allocate a temporary E1 line until we have received ID. */
616 line = e1inp_line_clone(tall_ipa_ctx, link->line);
617 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200618 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200619 return -1;
620 }
621 /* initialize the fds */
622 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
623 line->ts[i].driver.ipaccess.fd.fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200624
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200625 /* we need this to initialize this in case to avoid crashes in case
626 * that the socket is closed before we've seen an ID_RESP. */
627 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line);
628
629 e1i_ts = &line->ts[E1INP_SIGN_RSL-1];
630
631 bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200632 bfd->fd = fd;
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200633 bfd->data = line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200634 bfd->priv_nr = E1INP_SIGN_RSL;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200635 bfd->cb = ipaccess_fd_cb;
636 bfd->when = BSC_FD_READ;
637 ret = osmo_fd_register(bfd);
638 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200639 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200640 close(bfd->fd);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200641 e1inp_line_put(line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200642 return ret;
643 }
644 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
645 ret = ipaccess_send_id_req(bfd->fd);
646
647 return 0;
648}
649
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200650static struct msgb *
651ipa_bts_id_resp(struct ipaccess_unit *dev, uint8_t *data, int len)
652{
653 struct msgb *nmsg;
654 char str[64];
655 uint8_t *tag;
656
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200657 nmsg = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200658 if (!nmsg)
659 return NULL;
660
661 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
662 while (len) {
663 if (len < 2) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200664 LOGP(DLINP, LOGL_NOTICE,
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200665 "Short read of ipaccess tag\n");
666 msgb_free(nmsg);
667 return NULL;
668 }
669 switch (data[1]) {
670 case IPAC_IDTAG_UNIT:
671 sprintf(str, "%u/%u/%u",
672 dev->site_id, dev->bts_id, dev->trx_id);
673 break;
674 case IPAC_IDTAG_MACADDR:
675 sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
676 dev->mac_addr[0], dev->mac_addr[1],
677 dev->mac_addr[2], dev->mac_addr[3],
678 dev->mac_addr[4], dev->mac_addr[5]);
679 break;
680 case IPAC_IDTAG_LOCATION1:
681 strcpy(str, dev->location1);
682 break;
683 case IPAC_IDTAG_LOCATION2:
684 strcpy(str, dev->location2);
685 break;
686 case IPAC_IDTAG_EQUIPVERS:
687 strcpy(str, dev->equipvers);
688 break;
689 case IPAC_IDTAG_SWVERSION:
690 strcpy(str, dev->swversion);
691 break;
692 case IPAC_IDTAG_UNITNAME:
693 sprintf(str, "%s-%02x-%02x-%02x-%02x-%02x-%02x",
694 dev->unit_name,
695 dev->mac_addr[0], dev->mac_addr[1],
696 dev->mac_addr[2], dev->mac_addr[3],
697 dev->mac_addr[4], dev->mac_addr[5]);
698 break;
699 case IPAC_IDTAG_SERNR:
700 strcpy(str, dev->serno);
701 break;
702 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200703 LOGP(DLINP, LOGL_NOTICE,
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200704 "Unknown ipaccess tag 0x%02x\n", *data);
705 msgb_free(nmsg);
706 return NULL;
707 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200708 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200709 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
710 tag[0] = 0x00;
711 tag[1] = 1 + strlen(str) + 1;
712 tag[2] = data[1];
713 memcpy(tag + 3, str, strlen(str) + 1);
714 data += 2;
715 len -= 2;
716 }
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200717 ipa_msg_push_header(nmsg, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200718 return nmsg;
719}
720
721static struct msgb *ipa_bts_id_ack(void)
722{
723 struct msgb *nmsg2;
724
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200725 nmsg2 = ipa_msg_alloc(0);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200726 if (!nmsg2)
727 return NULL;
728
729 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200730 ipa_msg_push_header(nmsg2, IPAC_PROTO_IPACCESS);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200731
732 return nmsg2;
733}
734
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200735static int ipaccess_bts_cb(struct ipa_client_conn *link, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200736{
737 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
738 struct e1inp_ts *e1i_ts = NULL;
739 struct e1inp_sign_link *sign_link;
740
741 /* special handling for IPA CCM. */
742 if (hh->proto == IPAC_PROTO_IPACCESS) {
743 uint8_t msg_type = *(msg->l2h);
744
745 /* ping, pong and acknowledgment cases. */
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200746 ipaccess_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200747
748 /* this is a request for identification from the BSC. */
749 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200750 struct e1inp_sign_link *sign_link;
751 struct msgb *rmsg;
752 uint8_t *data = msgb_l2(msg);
753 int len = msgb_l2len(msg);
754
Harald Weltecc2241b2011-07-19 16:06:06 +0200755 LOGP(DLINP, LOGL_NOTICE, "received ID get\n");
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200756 if (!link->line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200757 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200758 "Unable to set signal link, "
759 "closing socket.\n");
760 osmo_fd_unregister(link->ofd);
761 close(link->ofd->fd);
762 link->ofd->fd = -1;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200763 return -EINVAL;
764 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200765 sign_link = link->line->ops->sign_link_up(msg,
766 link->line,
767 link->ofd->priv_nr);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200768 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200769 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200770 "Unable to set signal link, "
771 "closing socket.\n");
772 osmo_fd_unregister(link->ofd);
773 close(link->ofd->fd);
774 link->ofd->fd = -1;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200775 return -EINVAL;
776 }
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200777 rmsg = ipa_bts_id_resp(link->line->ops->cfg.ipa.dev,
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200778 data + 1, len - 1);
779 ipaccess_send(link->ofd->fd, rmsg->data, rmsg->len);
780 msgb_free(rmsg);
781
782 /* send ID_ACK. */
783 rmsg = ipa_bts_id_ack();
784 ipaccess_send(link->ofd->fd, rmsg->data, rmsg->len);
785 msgb_free(rmsg);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200786 }
787 return 0;
788 } else if (link->port == IPA_TCP_PORT_OML)
789 e1i_ts = &link->line->ts[0];
790 else if (link->port == IPA_TCP_PORT_RSL)
791 e1i_ts = &link->line->ts[1];
792
793 /* look up for some existing signaling link. */
794 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
795 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200796 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200797 "hh->proto=0x%02x\n", hh->proto);
798 msgb_free(msg);
799 return -EIO;
800 }
801 msg->dst = sign_link;
802
803 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200804 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200805 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200806 "no action set for signalling messages.\n");
807 return -ENOENT;
808 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200809 link->line->ops->sign_link(msg);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200810 return 0;
811}
812
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200813struct ipaccess_line {
814 int line_already_initialized;
815};
816
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200817static int ipaccess_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200818{
819 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200820 struct ipaccess_line *il;
821
822 if (!line->driver_data)
823 line->driver_data = talloc_zero(line, struct ipaccess_line);
824
825 if (!line->driver_data) {
826 LOGP(DLINP, LOGL_ERROR, "ipaccess: OOM in line update\n");
827 return -ENOMEM;
828 }
829 il = line->driver_data;
830
831 /* We only initialize this line once. */
832 if (il->line_already_initialized)
833 return 0;
834
835 il->line_already_initialized = 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200836
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200837 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200838 case E1INP_LINE_R_BSC: {
839 struct ipa_server_link *oml_link, *rsl_link;
840
Harald Weltecc2241b2011-07-19 16:06:06 +0200841 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BSC mode\n");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200842
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200843 oml_link = ipa_server_link_create(tall_ipa_ctx, line,
844 "0.0.0.0", IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200845 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200846 if (oml_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200847 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200848 "BSC link: %s\n", strerror(errno));
849 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200850 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200851 if (ipa_server_link_open(oml_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200852 LOGP(DLINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200853 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200854 ipa_server_link_destroy(oml_link);
855 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200856 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200857 rsl_link = ipa_server_link_create(tall_ipa_ctx, line,
858 "0.0.0.0", IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200859 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200860 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200861 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200862 "BSC link: %s\n", strerror(errno));
863 return -ENOMEM;
864 }
865 if (ipa_server_link_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200866 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200867 strerror(errno));
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200868 ipa_server_link_destroy(rsl_link);
869 return -EIO;
870 }
871 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200872 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200873 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200874 case E1INP_LINE_R_BTS: {
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200875 struct ipa_client_conn *link, *rsl_link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200876
Harald Weltecc2241b2011-07-19 16:06:06 +0200877 LOGP(DLINP, LOGL_NOTICE, "enabling ipaccess BTS mode\n");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200878
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200879 link = ipa_client_conn_create(tall_ipa_ctx,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200880 &line->ts[E1INP_SIGN_OML-1],
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +0200881 E1INP_SIGN_OML,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200882 line->ops->cfg.ipa.addr,
883 IPA_TCP_PORT_OML,
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200884 NULL,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200885 ipaccess_bts_cb,
886 ipaccess_bts_write_cb,
887 NULL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200888 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200889 LOGP(DLINP, LOGL_ERROR, "cannot create OML "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200890 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200891 return -ENOMEM;
892 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200893 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200894 LOGP(DLINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200895 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200896 ipa_client_conn_close(link);
897 ipa_client_conn_destroy(link);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200898 return -EIO;
899 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200900 rsl_link = ipa_client_conn_create(tall_ipa_ctx,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200901 &line->ts[E1INP_SIGN_RSL-1],
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +0200902 E1INP_SIGN_RSL,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200903 line->ops->cfg.ipa.addr,
904 IPA_TCP_PORT_RSL,
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200905 NULL,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200906 ipaccess_bts_cb,
907 ipaccess_bts_write_cb,
908 NULL);
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200909 if (rsl_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200910 LOGP(DLINP, LOGL_ERROR, "cannot create RSL "
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200911 "BTS link: %s\n", strerror(errno));
912 return -ENOMEM;
913 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200914 if (ipa_client_conn_open(rsl_link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200915 LOGP(DLINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200916 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200917 ipa_client_conn_close(rsl_link);
918 ipa_client_conn_destroy(rsl_link);
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200919 return -EIO;
920 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200921 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200922 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200923 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200924 default:
925 break;
926 }
927 return ret;
928}
929
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200930void e1inp_ipaccess_init(void)
931{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200932 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200933 e1inp_driver_register(&ipaccess_driver);
934}