blob: 5da9b2c14bee4bcb4734b7977d53b2a2da1e2167 [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>
29#include <errno.h>
30#include <string.h>
31#include <time.h>
32#include <sys/fcntl.h>
33#include <sys/socket.h>
34#include <sys/ioctl.h>
35#include <arpa/inet.h>
36
37#include <osmocom/core/select.h>
38#include <osmocom/gsm/tlv.h>
39#include <osmocom/core/msgb.h>
40#include <osmocom/core/logging.h>
41#include <talloc.h>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020042#include <osmocom/abis/e1_input.h>
43#include <osmocom/abis/ipaccess.h>
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +020044#include <osmocom/core/socket.h>
Pablo Neira Ayuso0b099b22011-06-09 13:14:11 +020045#include <osmocom/abis/logging.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) {
112 LOGP(DMI, LOGL_ERROR, "The tag does not fit: %d\n", t_len);
113 return -EINVAL;
114 }
115
116 DEBUGPC(DMI, "%s='%s' ", ipaccess_idtag_name(t_tag), cur);
117
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) {
168 LOGP(DINP, LOGL_ERROR, "ipaccess_send: short write\n");
169 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 */
190int ipaccess_rcvmsg_base(struct msgb *msg,
191 struct osmo_fd *bfd)
192{
193 uint8_t msg_type = *(msg->l2h);
194 int ret = 0;
195
196 switch (msg_type) {
197 case IPAC_MSGT_PING:
198 ret = ipaccess_send_pong(bfd->fd);
199 break;
200 case IPAC_MSGT_PONG:
201 DEBUGP(DMI, "PONG!\n");
202 break;
203 case IPAC_MSGT_ID_ACK:
204 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
205 ret = ipaccess_send_id_ack(bfd->fd);
206 break;
207 }
208 return 0;
209}
210
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200211/* base handling of the ip.access protocol */
212int ipaccess_rcvmsg_bts_base(struct msgb *msg,
213 struct osmo_fd *bfd)
214{
215 uint8_t msg_type = *(msg->l2h);
216 int ret = 0;
217
218 switch (msg_type) {
219 case IPAC_MSGT_PING:
220 ret = ipaccess_send_pong(bfd->fd);
221 break;
222 case IPAC_MSGT_PONG:
223 DEBUGP(DMI, "PONG!\n");
224 break;
225 case IPAC_MSGT_ID_ACK:
226 DEBUGP(DMI, "ID_ACK\n");
227 break;
228 }
229 return 0;
230}
231
232static void ipaccess_drop(struct osmo_fd *bfd)
233{
234 struct e1inp_line *line = bfd->data;
235
236 /* e1inp_sign_link_destroy releases the socket descriptors for us. */
237 line->ops->sign_link_down(line);
238
239 /* RSL connection without ID_RESP, release temporary socket. */
240 if (line->ts[E1INP_SIGN_OML-1].type == E1INP_SIGN_NONE)
241 talloc_free(bfd);
242}
243
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200244static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
245 struct osmo_fd *bfd)
246{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200247 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200248 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200249 struct ipaccess_unit unit_data = {};
250 struct e1inp_sign_link *sign_link;
251 char *unitid;
252 int len, ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200253
254 /* handle base messages */
255 ipaccess_rcvmsg_base(msg, bfd);
256
257 switch (msg_type) {
258 case IPAC_MSGT_ID_RESP:
259 DEBUGP(DMI, "ID_RESP\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200260 /* parse tags, search for Unit ID */
261 ret = ipaccess_idtag_parse(&tlvp, (uint8_t *)msg->l2h + 2,
262 msgb_l2len(msg)-2);
263 DEBUGP(DMI, "\n");
264 if (ret < 0) {
265 LOGP(DINP, LOGL_ERROR, "ignoring IPA response message "
266 "with malformed TLVs\n");
267 return ret;
268 }
269 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT))
270 break;
271
272 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
273 if (len < 1)
274 break;
275
276 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
277 unitid[len - 1] = '\0';
278 ipaccess_parse_unitid(unitid, &unit_data);
279
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200280 if (!line->ops->sign_link_up) {
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200281 LOGP(DINP, LOGL_ERROR,
282 "Unable to set signal link, closing socket.\n");
283 osmo_fd_unregister(bfd);
284 close(bfd->fd);
285 bfd->fd = -1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200286 return -EINVAL;
287 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200288 /* the BSC creates the new sign links at this stage. */
289 if (bfd->priv_nr == E1INP_SIGN_OML) {
290 sign_link =
291 line->ops->sign_link_up(&unit_data, line,
292 E1INP_SIGN_OML);
293 if (sign_link == NULL) {
294 LOGP(DINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200295 "Unable to set signal link, "
296 "closing socket.\n");
297 osmo_fd_unregister(bfd);
298 close(bfd->fd);
299 bfd->fd = -1;
300 return -EINVAL;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200301 }
302 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
303 struct e1inp_ts *e1i_ts;
304 struct osmo_fd *newbfd;
305
306 sign_link =
307 line->ops->sign_link_up(&unit_data, line,
308 E1INP_SIGN_RSL);
309 if (sign_link == NULL) {
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200310 LOGP(DINP, LOGL_ERROR,
311 "Unable to set signal link, "
312 "closing socket.\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200313 osmo_fd_unregister(bfd);
314 close(bfd->fd);
315 bfd->fd = -1;
316 talloc_free(bfd);
317 return 0;
318 }
319 /* Finally, we know which OML link is associated with
320 * this RSL link, attach it to this socket. */
321 bfd->data = line = sign_link->ts->line;
322 e1i_ts = &line->ts[E1INP_SIGN_RSL+unit_data.trx_id-1];
323 newbfd = &e1i_ts->driver.ipaccess.fd;
324
325 /* get rid of our old temporary bfd */
326 memcpy(newbfd, bfd, sizeof(*newbfd));
327 newbfd->priv_nr = E1INP_SIGN_RSL + unit_data.trx_id;
328 osmo_fd_unregister(bfd);
329 bfd->fd = -1;
330 talloc_free(bfd);
331 osmo_fd_register(newbfd);
332 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200333 break;
334 }
335 return 0;
336}
337
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200338static int handle_ts1_read(struct osmo_fd *bfd)
339{
340 struct e1inp_line *line = bfd->data;
341 unsigned int ts_nr = bfd->priv_nr;
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200342 struct e1inp_ts *e1i_ts = NULL;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200343 struct e1inp_sign_link *link;
344 struct ipaccess_head *hh;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200345 struct msgb *msg;
346 int ret = 0, error;
347
Pablo Neira Ayuso7a249402011-06-21 14:15:46 +0200348 error = ipa_msg_recv(bfd->fd, &msg);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200349 if (error < 0)
350 return error;
351 else if (error == 0) {
352 ipaccess_drop(bfd);
353 LOGP(DINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200354 return error;
355 }
356 DEBUGP(DMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
357
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200358 /* Now that we're sure that we've got a line for socket. */
359 e1i_ts = &line->ts[ts_nr-1];
360
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200361 hh = (struct ipaccess_head *) msg->data;
362 if (hh->proto == IPAC_PROTO_IPACCESS) {
363 ipaccess_rcvmsg(line, msg, bfd);
364 msgb_free(msg);
365 return ret;
366 }
367 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
368 * might have free'd it !!! */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200369
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200370 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
371 if (!link) {
372 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
373 "hh->proto=0x%02x\n", hh->proto);
374 msgb_free(msg);
375 return -EIO;
376 }
377 msg->dst = link;
378
379 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200380 if (!e1i_ts->line->ops->sign_link) {
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200381 LOGP(DINP, LOGL_ERROR, "Fix your application, "
382 "no action set for signalling messages.\n");
383 return -ENOENT;
384 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200385 e1i_ts->line->ops->sign_link(msg);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200386
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200387 return ret;
388}
389
390void ipaccess_prepend_header_ext(struct msgb *msg, int proto)
391{
392 struct ipaccess_head_ext *hh_ext;
393
394 /* prepend the osmo ip.access header extension */
395 hh_ext = (struct ipaccess_head_ext *) msgb_push(msg, sizeof(*hh_ext));
396 hh_ext->proto = proto;
397}
398
399void ipaccess_prepend_header(struct msgb *msg, int proto)
400{
401 struct ipaccess_head *hh;
402
403 /* prepend the ip.access header */
404 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
405 hh->len = htons(msg->len - sizeof(*hh));
406 hh->proto = proto;
407}
408
409static int ts_want_write(struct e1inp_ts *e1i_ts)
410{
411 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
412
413 return 0;
414}
415
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200416static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200417{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200418 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200419 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200420 e1inp_event(e1i_ts, S_INP_TEI_DN, sign_link->tei, sign_link->sapi);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200421 osmo_fd_unregister(bfd);
422 close(bfd->fd);
423 bfd->fd = -1;
424}
425
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200426static void timeout_ts1_write(void *data)
427{
428 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
429
430 /* trigger write of ts1, due to tx delay timer */
431 ts_want_write(e1i_ts);
432}
433
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200434static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200435{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200436 unsigned int ts_nr = bfd->priv_nr;
437 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
438 struct e1inp_sign_link *sign_link;
439 struct msgb *msg;
440 uint8_t proto;
441 int ret;
442
443 bfd->when &= ~BSC_FD_WRITE;
444
445 /* get the next msg for this timeslot */
446 msg = e1inp_tx_ts(e1i_ts, &sign_link);
447 if (!msg) {
448 /* no message after tx delay timer */
449 return 0;
450 }
451
452 switch (sign_link->type) {
453 case E1INP_SIGN_OML:
454 proto = IPAC_PROTO_OML;
455 break;
456 case E1INP_SIGN_RSL:
457 proto = IPAC_PROTO_RSL;
458 break;
459 default:
460 msgb_free(msg);
461 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
462 return -EINVAL;
463 }
464
465 msg->l2h = msg->data;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200466 /* This is an IPA CCM, it already contains the header, skip. */
467 if (msgb_tailroom(msg) < sizeof(struct ipaccess_head))
468 ipaccess_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200469
470 DEBUGP(DMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
471
472 ret = send(bfd->fd, msg->data, msg->len, 0);
473 msgb_free(msg);
474
475 /* set tx delay timer for next event */
476 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
477 e1i_ts->sign.tx_timer.data = e1i_ts;
478
479 /* Reducing this might break the nanoBTS 900 init. */
480 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
481
482 return ret;
483}
484
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200485int handle_ts1_write(struct osmo_fd *bfd)
486{
487 struct e1inp_line *line = bfd->data;
488
489 return __handle_ts1_write(bfd, line);
490}
491
492int ipaccess_bts_write_cb(struct ipa_client_link *link)
493{
494 struct e1inp_line *line = link->line;
495
496 return __handle_ts1_write(link->ofd, line);
497}
498
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200499/* callback from select.c in case one of the fd's can be read/written */
500static int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
501{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200502 int rc = 0;
503
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200504 if (what & BSC_FD_READ)
505 rc = handle_ts1_read(bfd);
506 if (what & BSC_FD_WRITE)
507 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200508
509 return rc;
510}
511
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200512static int ipaccess_line_update(struct e1inp_line *line,
513 enum e1inp_line_role role, const char *addr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200514
515struct e1inp_driver ipaccess_driver = {
516 .name = "ipa",
517 .want_write = ts_want_write,
518 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200519 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200520 .default_delay = 0,
521};
522
523/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200524static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200525{
526 int ret;
527 int idx = 0;
528 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200529 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200530 struct e1inp_ts *e1i_ts;
531 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200532
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200533 /* clone virtual E1 line for this new OML link. */
534 line = talloc_zero(tall_ipa_ctx, struct e1inp_line);
535 if (line == NULL) {
536 LOGP(DINP, LOGL_ERROR, "could not clone E1 line\n");
537 return -1;
538 }
539 memcpy(line, link->line, sizeof(struct e1inp_line));
540
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200541 /* create virrtual E1 timeslots for signalling */
Pablo Neira Ayuso59301852011-06-27 15:44:23 +0200542 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200543
544 /* initialize the fds */
545 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
546 line->ts[i].driver.ipaccess.fd.fd = -1;
547
548 e1i_ts = &line->ts[idx];
549
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200550 bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200551 bfd->fd = fd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200552 bfd->data = line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200553 bfd->priv_nr = E1INP_SIGN_OML;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200554 bfd->cb = ipaccess_fd_cb;
555 bfd->when = BSC_FD_READ;
556 ret = osmo_fd_register(bfd);
557 if (ret < 0) {
558 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
559 close(bfd->fd);
560 return ret;
561 }
562
563 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
564 ret = ipaccess_send_id_req(bfd->fd);
565
566 return ret;
567}
568
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200569static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200570{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200571 struct osmo_fd *bfd;
572 int ret;
573
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200574 /* We don't know yet which OML link to associate it with. Thus, we
575 * allocate a temporary bfd until we have received ID. */
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200576 bfd = talloc_zero(tall_ipa_ctx, struct osmo_fd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200577 if (!bfd)
578 return -ENOMEM;
579
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200580 bfd->fd = fd;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200581 /* This dummy line is replaced once we can attach it to the OML link */
582 bfd->data = link->line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200583 bfd->priv_nr = E1INP_SIGN_RSL;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200584 bfd->cb = ipaccess_fd_cb;
585 bfd->when = BSC_FD_READ;
586 ret = osmo_fd_register(bfd);
587 if (ret < 0) {
588 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
589 close(bfd->fd);
590 talloc_free(bfd);
591 return ret;
592 }
593 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
594 ret = ipaccess_send_id_req(bfd->fd);
595
596 return 0;
597}
598
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200599static struct msgb *abis_msgb_alloc(int headroom)
600{
601 struct msgb *nmsg;
602
603 headroom += sizeof(struct ipaccess_head);
604
605 nmsg = msgb_alloc_headroom(1200 + headroom, headroom, "dummy BTS");
606 if (!nmsg)
607 return NULL;
608 return nmsg;
609}
610
611static void abis_push_ipa(struct msgb *msg, uint8_t proto)
612{
613 struct ipaccess_head *nhh;
614
615 msg->l2h = msg->data;
616 nhh = (struct ipaccess_head *) msgb_push(msg, sizeof(*nhh));
617 nhh->proto = proto;
618 nhh->len = htons(msgb_l2len(msg));
619}
620
621static struct msgb *
622ipa_bts_id_resp(struct ipaccess_unit *dev, uint8_t *data, int len)
623{
624 struct msgb *nmsg;
625 char str[64];
626 uint8_t *tag;
627
628 nmsg = abis_msgb_alloc(0);
629 if (!nmsg)
630 return NULL;
631
632 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
633 while (len) {
634 if (len < 2) {
635 LOGP(DINP, LOGL_NOTICE,
636 "Short read of ipaccess tag\n");
637 msgb_free(nmsg);
638 return NULL;
639 }
640 switch (data[1]) {
641 case IPAC_IDTAG_UNIT:
642 sprintf(str, "%u/%u/%u",
643 dev->site_id, dev->bts_id, dev->trx_id);
644 break;
645 case IPAC_IDTAG_MACADDR:
646 sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
647 dev->mac_addr[0], dev->mac_addr[1],
648 dev->mac_addr[2], dev->mac_addr[3],
649 dev->mac_addr[4], dev->mac_addr[5]);
650 break;
651 case IPAC_IDTAG_LOCATION1:
652 strcpy(str, dev->location1);
653 break;
654 case IPAC_IDTAG_LOCATION2:
655 strcpy(str, dev->location2);
656 break;
657 case IPAC_IDTAG_EQUIPVERS:
658 strcpy(str, dev->equipvers);
659 break;
660 case IPAC_IDTAG_SWVERSION:
661 strcpy(str, dev->swversion);
662 break;
663 case IPAC_IDTAG_UNITNAME:
664 sprintf(str, "%s-%02x-%02x-%02x-%02x-%02x-%02x",
665 dev->unit_name,
666 dev->mac_addr[0], dev->mac_addr[1],
667 dev->mac_addr[2], dev->mac_addr[3],
668 dev->mac_addr[4], dev->mac_addr[5]);
669 break;
670 case IPAC_IDTAG_SERNR:
671 strcpy(str, dev->serno);
672 break;
673 default:
674 LOGP(DINP, LOGL_NOTICE,
675 "Unknown ipaccess tag 0x%02x\n", *data);
676 msgb_free(nmsg);
677 return NULL;
678 }
679 LOGP(DINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
680 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
681 tag[0] = 0x00;
682 tag[1] = 1 + strlen(str) + 1;
683 tag[2] = data[1];
684 memcpy(tag + 3, str, strlen(str) + 1);
685 data += 2;
686 len -= 2;
687 }
688 abis_push_ipa(nmsg, IPAC_PROTO_IPACCESS);
689 return nmsg;
690}
691
692static struct msgb *ipa_bts_id_ack(void)
693{
694 struct msgb *nmsg2;
695
696 nmsg2 = abis_msgb_alloc(0);
697 if (!nmsg2)
698 return NULL;
699
700 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
701 abis_push_ipa(nmsg2, IPAC_PROTO_IPACCESS);
702
703 return nmsg2;
704}
705
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200706static int ipaccess_bts_cb(struct ipa_client_link *link, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200707{
708 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
709 struct e1inp_ts *e1i_ts = NULL;
710 struct e1inp_sign_link *sign_link;
711
712 /* special handling for IPA CCM. */
713 if (hh->proto == IPAC_PROTO_IPACCESS) {
714 uint8_t msg_type = *(msg->l2h);
715
716 /* ping, pong and acknowledgment cases. */
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200717 ipaccess_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200718
719 /* this is a request for identification from the BSC. */
720 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200721 struct e1inp_sign_link *sign_link;
722 struct msgb *rmsg;
723 uint8_t *data = msgb_l2(msg);
724 int len = msgb_l2len(msg);
725
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200726 LOGP(DINP, LOGL_NOTICE, "received ID get\n");
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200727 if (!link->line->ops->sign_link_up) {
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200728 LOGP(DINP, LOGL_ERROR,
729 "Unable to set signal link, "
730 "closing socket.\n");
731 osmo_fd_unregister(link->ofd);
732 close(link->ofd->fd);
733 link->ofd->fd = -1;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200734 return -EINVAL;
735 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200736 sign_link = link->line->ops->sign_link_up(msg,
737 link->line,
738 link->ofd->priv_nr);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200739 if (sign_link == NULL) {
740 LOGP(DINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200741 "Unable to set signal link, "
742 "closing socket.\n");
743 osmo_fd_unregister(link->ofd);
744 close(link->ofd->fd);
745 link->ofd->fd = -1;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200746 return -EINVAL;
747 }
748 rmsg = ipa_bts_id_resp(link->line->ops->data,
749 data + 1, len - 1);
750 ipaccess_send(link->ofd->fd, rmsg->data, rmsg->len);
751 msgb_free(rmsg);
752
753 /* send ID_ACK. */
754 rmsg = ipa_bts_id_ack();
755 ipaccess_send(link->ofd->fd, rmsg->data, rmsg->len);
756 msgb_free(rmsg);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200757 }
758 return 0;
759 } else if (link->port == IPA_TCP_PORT_OML)
760 e1i_ts = &link->line->ts[0];
761 else if (link->port == IPA_TCP_PORT_RSL)
762 e1i_ts = &link->line->ts[1];
763
764 /* look up for some existing signaling link. */
765 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
766 if (sign_link == NULL) {
767 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
768 "hh->proto=0x%02x\n", hh->proto);
769 msgb_free(msg);
770 return -EIO;
771 }
772 msg->dst = sign_link;
773
774 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200775 if (!link->line->ops->sign_link) {
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200776 LOGP(DINP, LOGL_ERROR, "Fix your application, "
777 "no action set for signalling messages.\n");
778 return -ENOENT;
779 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200780 link->line->ops->sign_link(msg);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200781 return 0;
782}
783
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200784static int ipaccess_line_update(struct e1inp_line *line,
785 enum e1inp_line_role role, const char *addr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200786{
787 int ret = -ENOENT;
788
789 switch(role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200790 case E1INP_LINE_R_BSC: {
791 struct ipa_server_link *oml_link, *rsl_link;
792
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200793 LOGP(DINP, LOGL_NOTICE, "enabling ipaccess BSC mode\n");
794
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200795 oml_link = ipa_server_link_create(tall_ipa_ctx, line,
796 "0.0.0.0", IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200797 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200798 if (oml_link == NULL) {
799 LOGP(DINP, LOGL_ERROR, "cannot create OML "
800 "BSC link: %s\n", strerror(errno));
801 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200802 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200803 if (ipa_server_link_open(oml_link) < 0) {
804 LOGP(DINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
805 strerror(errno));
806 ipa_server_link_close(oml_link);
807 ipa_server_link_destroy(oml_link);
808 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200809 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200810 rsl_link = ipa_server_link_create(tall_ipa_ctx, line,
811 "0.0.0.0", IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200812 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200813 if (rsl_link == NULL) {
814 LOGP(DINP, LOGL_ERROR, "cannot create RSL "
815 "BSC link: %s\n", strerror(errno));
816 return -ENOMEM;
817 }
818 if (ipa_server_link_open(rsl_link) < 0) {
819 LOGP(DINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
820 strerror(errno));
821 ipa_server_link_close(rsl_link);
822 ipa_server_link_destroy(rsl_link);
823 return -EIO;
824 }
825 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200826 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200827 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200828 case E1INP_LINE_R_BTS: {
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200829 struct ipa_client_link *link, *rsl_link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200830
831 LOGP(DINP, LOGL_NOTICE, "enabling ipaccess BTS mode\n");
832
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200833 link = ipa_client_link_create(tall_ipa_ctx,
834 &line->ts[E1INP_SIGN_OML-1],
835 "ipa", E1INP_SIGN_OML,
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200836 addr, IPA_TCP_PORT_OML,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200837 ipaccess_bts_cb,
838 ipaccess_bts_write_cb,
839 NULL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200840 if (link == NULL) {
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200841 LOGP(DINP, LOGL_ERROR, "cannot create OML "
842 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200843 return -ENOMEM;
844 }
845 if (ipa_client_link_open(link) < 0) {
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200846 LOGP(DINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
847 strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200848 ipa_client_link_close(link);
849 ipa_client_link_destroy(link);
850 return -EIO;
851 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200852 rsl_link = ipa_client_link_create(tall_ipa_ctx,
853 &line->ts[E1INP_SIGN_RSL-1],
854 "ipa", E1INP_SIGN_RSL,
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200855 addr, IPA_TCP_PORT_RSL,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200856 ipaccess_bts_cb,
857 ipaccess_bts_write_cb,
858 NULL);
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200859 if (rsl_link == NULL) {
860 LOGP(DINP, LOGL_ERROR, "cannot create RSL "
861 "BTS link: %s\n", strerror(errno));
862 return -ENOMEM;
863 }
864 if (ipa_client_link_open(rsl_link) < 0) {
865 LOGP(DINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
866 strerror(errno));
867 ipa_client_link_close(rsl_link);
868 ipa_client_link_destroy(rsl_link);
869 return -EIO;
870 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200871 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200872 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200873 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200874 default:
875 break;
876 }
877 return ret;
878}
879
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200880void e1inp_ipaccess_init(void)
881{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200882 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200883 e1inp_driver_register(&ipaccess_driver);
884}