blob: 80047609a16353eacfe2db4d15ec1b635fcf59d9 [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;
137 if (unit_data->site_id)
138 unit_data->site_id = ul & 0xffff;
139
140 if (*endptr++ != '/')
141 return -EINVAL;
142
143 nptr = endptr;
144 ul = strtoul(nptr, &endptr, 10);
145 if (endptr <= nptr)
146 return -EINVAL;
147 if (unit_data->bts_id)
148 unit_data->bts_id = ul & 0xffff;
149
150 if (*endptr++ != '/')
151 return -EINVAL;
152
153 nptr = endptr;
154 ul = strtoul(nptr, &endptr, 10);
155 if (endptr <= nptr)
156 return -EINVAL;
157 if (unit_data->trx_id)
158 unit_data->trx_id = ul & 0xffff;
159
160 return 0;
161}
162
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200163static int ipaccess_send(int fd, const void *msg, size_t msglen)
164{
165 int ret;
166
167 ret = write(fd, msg, msglen);
168 if (ret < 0)
169 return ret;
170 if (ret < msglen) {
171 LOGP(DINP, LOGL_ERROR, "ipaccess_send: short write\n");
172 return -EIO;
173 }
174 return ret;
175}
176
177int ipaccess_send_pong(int fd)
178{
179 return ipaccess_send(fd, ipa_pong_msg, sizeof(ipa_pong_msg));
180}
181
182int ipaccess_send_id_ack(int fd)
183{
184 return ipaccess_send(fd, ipa_id_ack_msg, sizeof(ipa_id_ack_msg));
185}
186
187int ipaccess_send_id_req(int fd)
188{
189 return ipaccess_send(fd, ipa_id_req_msg, sizeof(ipa_id_req_msg));
190}
191
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200192/* base handling of the ip.access protocol */
193int ipaccess_rcvmsg_base(struct msgb *msg,
194 struct osmo_fd *bfd)
195{
196 uint8_t msg_type = *(msg->l2h);
197 int ret = 0;
198
199 switch (msg_type) {
200 case IPAC_MSGT_PING:
201 ret = ipaccess_send_pong(bfd->fd);
202 break;
203 case IPAC_MSGT_PONG:
204 DEBUGP(DMI, "PONG!\n");
205 break;
206 case IPAC_MSGT_ID_ACK:
207 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
208 ret = ipaccess_send_id_ack(bfd->fd);
209 break;
210 }
211 return 0;
212}
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:
226 DEBUGP(DMI, "PONG!\n");
227 break;
228 case IPAC_MSGT_ID_ACK:
229 DEBUGP(DMI, "ID_ACK\n");
230 break;
231 }
232 return 0;
233}
234
235static void ipaccess_drop(struct osmo_fd *bfd)
236{
237 struct e1inp_line *line = bfd->data;
238
239 /* e1inp_sign_link_destroy releases the socket descriptors for us. */
240 line->ops->sign_link_down(line);
241
242 /* RSL connection without ID_RESP, release temporary socket. */
243 if (line->ts[E1INP_SIGN_OML-1].type == E1INP_SIGN_NONE)
244 talloc_free(bfd);
245}
246
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200247static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
248 struct osmo_fd *bfd)
249{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200250 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200251 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200252 struct ipaccess_unit unit_data = {};
253 struct e1inp_sign_link *sign_link;
254 char *unitid;
255 int len, ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200256
257 /* handle base messages */
258 ipaccess_rcvmsg_base(msg, bfd);
259
260 switch (msg_type) {
261 case IPAC_MSGT_ID_RESP:
262 DEBUGP(DMI, "ID_RESP\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200263 /* parse tags, search for Unit ID */
264 ret = ipaccess_idtag_parse(&tlvp, (uint8_t *)msg->l2h + 2,
265 msgb_l2len(msg)-2);
266 DEBUGP(DMI, "\n");
267 if (ret < 0) {
268 LOGP(DINP, LOGL_ERROR, "ignoring IPA response message "
269 "with malformed TLVs\n");
270 return ret;
271 }
272 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT))
273 break;
274
275 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
276 if (len < 1)
277 break;
278
279 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
280 unitid[len - 1] = '\0';
281 ipaccess_parse_unitid(unitid, &unit_data);
282
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200283 if (!line->ops->sign_link_up) {
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200284 LOGP(DINP, LOGL_ERROR, "Fix your application, "
285 "no action set if the signalling link "
286 "becomes ready\n");
287 return -EINVAL;
288 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200289 /* the BSC creates the new sign links at this stage. */
290 if (bfd->priv_nr == E1INP_SIGN_OML) {
291 sign_link =
292 line->ops->sign_link_up(&unit_data, line,
293 E1INP_SIGN_OML);
294 if (sign_link == NULL) {
295 LOGP(DINP, LOGL_ERROR,
296 "No OML signal link set by BSC\n");
297 }
298 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
299 struct e1inp_ts *e1i_ts;
300 struct osmo_fd *newbfd;
301
302 sign_link =
303 line->ops->sign_link_up(&unit_data, line,
304 E1INP_SIGN_RSL);
305 if (sign_link == NULL) {
306 LOGP(DINP, LOGL_ERROR, "Don't know where "
307 "to attach this RSL link.\n");
308 osmo_fd_unregister(bfd);
309 close(bfd->fd);
310 bfd->fd = -1;
311 talloc_free(bfd);
312 return 0;
313 }
314 /* Finally, we know which OML link is associated with
315 * this RSL link, attach it to this socket. */
316 bfd->data = line = sign_link->ts->line;
317 e1i_ts = &line->ts[E1INP_SIGN_RSL+unit_data.trx_id-1];
318 newbfd = &e1i_ts->driver.ipaccess.fd;
319
320 /* get rid of our old temporary bfd */
321 memcpy(newbfd, bfd, sizeof(*newbfd));
322 newbfd->priv_nr = E1INP_SIGN_RSL + unit_data.trx_id;
323 osmo_fd_unregister(bfd);
324 bfd->fd = -1;
325 talloc_free(bfd);
326 osmo_fd_register(newbfd);
327 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200328 break;
329 }
330 return 0;
331}
332
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200333static int handle_ts1_read(struct osmo_fd *bfd)
334{
335 struct e1inp_line *line = bfd->data;
336 unsigned int ts_nr = bfd->priv_nr;
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200337 struct e1inp_ts *e1i_ts = NULL;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200338 struct e1inp_sign_link *link;
339 struct ipaccess_head *hh;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200340 struct msgb *msg;
341 int ret = 0, error;
342
Pablo Neira Ayuso7a249402011-06-21 14:15:46 +0200343 error = ipa_msg_recv(bfd->fd, &msg);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200344 if (error < 0)
345 return error;
346 else if (error == 0) {
347 ipaccess_drop(bfd);
348 LOGP(DINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200349 return error;
350 }
351 DEBUGP(DMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
352
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200353 /* Now that we're sure that we've got a line for socket. */
354 e1i_ts = &line->ts[ts_nr-1];
355
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200356 hh = (struct ipaccess_head *) msg->data;
357 if (hh->proto == IPAC_PROTO_IPACCESS) {
358 ipaccess_rcvmsg(line, msg, bfd);
359 msgb_free(msg);
360 return ret;
361 }
362 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
363 * might have free'd it !!! */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200364
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200365 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
366 if (!link) {
367 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
368 "hh->proto=0x%02x\n", hh->proto);
369 msgb_free(msg);
370 return -EIO;
371 }
372 msg->dst = link;
Pablo Neira Ayusoff663632011-06-26 19:10:56 +0200373 msg->trx = link->trx;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200374
375 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200376 if (!e1i_ts->line->ops->sign_link) {
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200377 LOGP(DINP, LOGL_ERROR, "Fix your application, "
378 "no action set for signalling messages.\n");
379 return -ENOENT;
380 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200381 e1i_ts->line->ops->sign_link(msg, link);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200382
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200383 return ret;
384}
385
386void ipaccess_prepend_header_ext(struct msgb *msg, int proto)
387{
388 struct ipaccess_head_ext *hh_ext;
389
390 /* prepend the osmo ip.access header extension */
391 hh_ext = (struct ipaccess_head_ext *) msgb_push(msg, sizeof(*hh_ext));
392 hh_ext->proto = proto;
393}
394
395void ipaccess_prepend_header(struct msgb *msg, int proto)
396{
397 struct ipaccess_head *hh;
398
399 /* prepend the ip.access header */
400 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
401 hh->len = htons(msg->len - sizeof(*hh));
402 hh->proto = proto;
403}
404
405static int ts_want_write(struct e1inp_ts *e1i_ts)
406{
407 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
408
409 return 0;
410}
411
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200412static void ipaccess_close(struct e1inp_ts *e1i_ts)
413{
414 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
415 osmo_fd_unregister(bfd);
416 close(bfd->fd);
417 bfd->fd = -1;
418}
419
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200420static void timeout_ts1_write(void *data)
421{
422 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
423
424 /* trigger write of ts1, due to tx delay timer */
425 ts_want_write(e1i_ts);
426}
427
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200428static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200429{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200430 unsigned int ts_nr = bfd->priv_nr;
431 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
432 struct e1inp_sign_link *sign_link;
433 struct msgb *msg;
434 uint8_t proto;
435 int ret;
436
437 bfd->when &= ~BSC_FD_WRITE;
438
439 /* get the next msg for this timeslot */
440 msg = e1inp_tx_ts(e1i_ts, &sign_link);
441 if (!msg) {
442 /* no message after tx delay timer */
443 return 0;
444 }
445
446 switch (sign_link->type) {
447 case E1INP_SIGN_OML:
448 proto = IPAC_PROTO_OML;
449 break;
450 case E1INP_SIGN_RSL:
451 proto = IPAC_PROTO_RSL;
452 break;
453 default:
454 msgb_free(msg);
455 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
456 return -EINVAL;
457 }
458
459 msg->l2h = msg->data;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200460 /* This is an IPA CCM, it already contains the header, skip. */
461 if (msgb_tailroom(msg) < sizeof(struct ipaccess_head))
462 ipaccess_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200463
464 DEBUGP(DMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
465
466 ret = send(bfd->fd, msg->data, msg->len, 0);
467 msgb_free(msg);
468
469 /* set tx delay timer for next event */
470 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
471 e1i_ts->sign.tx_timer.data = e1i_ts;
472
473 /* Reducing this might break the nanoBTS 900 init. */
474 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
475
476 return ret;
477}
478
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200479int handle_ts1_write(struct osmo_fd *bfd)
480{
481 struct e1inp_line *line = bfd->data;
482
483 return __handle_ts1_write(bfd, line);
484}
485
486int ipaccess_bts_write_cb(struct ipa_client_link *link)
487{
488 struct e1inp_line *line = link->line;
489
490 return __handle_ts1_write(link->ofd, line);
491}
492
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200493/* callback from select.c in case one of the fd's can be read/written */
494static int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
495{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200496 int rc = 0;
497
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200498 if (what & BSC_FD_READ)
499 rc = handle_ts1_read(bfd);
500 if (what & BSC_FD_WRITE)
501 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200502
503 return rc;
504}
505
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200506static int ipaccess_line_update(struct e1inp_line *line,
507 enum e1inp_line_role role, const char *addr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200508
509struct e1inp_driver ipaccess_driver = {
510 .name = "ipa",
511 .want_write = ts_want_write,
512 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200513 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200514 .default_delay = 0,
515};
516
517/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200518static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200519{
520 int ret;
521 int idx = 0;
522 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200523 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200524 struct e1inp_ts *e1i_ts;
525 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200526
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200527 /* clone virtual E1 line for this new OML link. */
528 line = talloc_zero(tall_ipa_ctx, struct e1inp_line);
529 if (line == NULL) {
530 LOGP(DINP, LOGL_ERROR, "could not clone E1 line\n");
531 return -1;
532 }
533 memcpy(line, link->line, sizeof(struct e1inp_line));
534
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200535 /* create virrtual E1 timeslots for signalling */
Pablo Neira Ayuso59301852011-06-27 15:44:23 +0200536 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200537
538 /* initialize the fds */
539 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
540 line->ts[i].driver.ipaccess.fd.fd = -1;
541
542 e1i_ts = &line->ts[idx];
543
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200544 bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200545 bfd->fd = fd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200546 bfd->data = line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200547 bfd->priv_nr = E1INP_SIGN_OML;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200548 bfd->cb = ipaccess_fd_cb;
549 bfd->when = BSC_FD_READ;
550 ret = osmo_fd_register(bfd);
551 if (ret < 0) {
552 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
553 close(bfd->fd);
554 return ret;
555 }
556
557 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
558 ret = ipaccess_send_id_req(bfd->fd);
559
560 return ret;
561}
562
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200563static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200564{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200565 struct osmo_fd *bfd;
566 int ret;
567
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200568 /* We don't know yet which OML link to associate it with. Thus, we
569 * allocate a temporary bfd until we have received ID. */
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200570 bfd = talloc_zero(tall_ipa_ctx, struct osmo_fd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200571 if (!bfd)
572 return -ENOMEM;
573
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200574 bfd->fd = fd;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200575 /* This dummy line is replaced once we can attach it to the OML link */
576 bfd->data = link->line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200577 bfd->priv_nr = E1INP_SIGN_RSL;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200578 bfd->cb = ipaccess_fd_cb;
579 bfd->when = BSC_FD_READ;
580 ret = osmo_fd_register(bfd);
581 if (ret < 0) {
582 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
583 close(bfd->fd);
584 talloc_free(bfd);
585 return ret;
586 }
587 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
588 ret = ipaccess_send_id_req(bfd->fd);
589
590 return 0;
591}
592
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200593static struct msgb *abis_msgb_alloc(int headroom)
594{
595 struct msgb *nmsg;
596
597 headroom += sizeof(struct ipaccess_head);
598
599 nmsg = msgb_alloc_headroom(1200 + headroom, headroom, "dummy BTS");
600 if (!nmsg)
601 return NULL;
602 return nmsg;
603}
604
605static void abis_push_ipa(struct msgb *msg, uint8_t proto)
606{
607 struct ipaccess_head *nhh;
608
609 msg->l2h = msg->data;
610 nhh = (struct ipaccess_head *) msgb_push(msg, sizeof(*nhh));
611 nhh->proto = proto;
612 nhh->len = htons(msgb_l2len(msg));
613}
614
615static struct msgb *
616ipa_bts_id_resp(struct ipaccess_unit *dev, uint8_t *data, int len)
617{
618 struct msgb *nmsg;
619 char str[64];
620 uint8_t *tag;
621
622 nmsg = abis_msgb_alloc(0);
623 if (!nmsg)
624 return NULL;
625
626 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
627 while (len) {
628 if (len < 2) {
629 LOGP(DINP, LOGL_NOTICE,
630 "Short read of ipaccess tag\n");
631 msgb_free(nmsg);
632 return NULL;
633 }
634 switch (data[1]) {
635 case IPAC_IDTAG_UNIT:
636 sprintf(str, "%u/%u/%u",
637 dev->site_id, dev->bts_id, dev->trx_id);
638 break;
639 case IPAC_IDTAG_MACADDR:
640 sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
641 dev->mac_addr[0], dev->mac_addr[1],
642 dev->mac_addr[2], dev->mac_addr[3],
643 dev->mac_addr[4], dev->mac_addr[5]);
644 break;
645 case IPAC_IDTAG_LOCATION1:
646 strcpy(str, dev->location1);
647 break;
648 case IPAC_IDTAG_LOCATION2:
649 strcpy(str, dev->location2);
650 break;
651 case IPAC_IDTAG_EQUIPVERS:
652 strcpy(str, dev->equipvers);
653 break;
654 case IPAC_IDTAG_SWVERSION:
655 strcpy(str, dev->swversion);
656 break;
657 case IPAC_IDTAG_UNITNAME:
658 sprintf(str, "%s-%02x-%02x-%02x-%02x-%02x-%02x",
659 dev->unit_name,
660 dev->mac_addr[0], dev->mac_addr[1],
661 dev->mac_addr[2], dev->mac_addr[3],
662 dev->mac_addr[4], dev->mac_addr[5]);
663 break;
664 case IPAC_IDTAG_SERNR:
665 strcpy(str, dev->serno);
666 break;
667 default:
668 LOGP(DINP, LOGL_NOTICE,
669 "Unknown ipaccess tag 0x%02x\n", *data);
670 msgb_free(nmsg);
671 return NULL;
672 }
673 LOGP(DINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
674 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
675 tag[0] = 0x00;
676 tag[1] = 1 + strlen(str) + 1;
677 tag[2] = data[1];
678 memcpy(tag + 3, str, strlen(str) + 1);
679 data += 2;
680 len -= 2;
681 }
682 abis_push_ipa(nmsg, IPAC_PROTO_IPACCESS);
683 return nmsg;
684}
685
686static struct msgb *ipa_bts_id_ack(void)
687{
688 struct msgb *nmsg2;
689
690 nmsg2 = abis_msgb_alloc(0);
691 if (!nmsg2)
692 return NULL;
693
694 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
695 abis_push_ipa(nmsg2, IPAC_PROTO_IPACCESS);
696
697 return nmsg2;
698}
699
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200700static int ipaccess_bts_cb(struct ipa_client_link *link, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200701{
702 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
703 struct e1inp_ts *e1i_ts = NULL;
704 struct e1inp_sign_link *sign_link;
705
706 /* special handling for IPA CCM. */
707 if (hh->proto == IPAC_PROTO_IPACCESS) {
708 uint8_t msg_type = *(msg->l2h);
709
710 /* ping, pong and acknowledgment cases. */
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200711 ipaccess_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200712
713 /* this is a request for identification from the BSC. */
714 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200715 struct e1inp_sign_link *sign_link;
716 struct msgb *rmsg;
717 uint8_t *data = msgb_l2(msg);
718 int len = msgb_l2len(msg);
719
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200720 LOGP(DINP, LOGL_NOTICE, "received ID get\n");
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200721 if (!link->line->ops->sign_link_up) {
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200722 LOGP(DINP, LOGL_ERROR, "Fix your application, "
723 "no action set if the signalling link "
724 "becomes ready\n");
725 return -EINVAL;
726 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200727 sign_link = link->line->ops->sign_link_up(msg,
728 link->line,
729 link->ofd->priv_nr);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200730 if (sign_link == NULL) {
731 LOGP(DINP, LOGL_ERROR,
732 "No sign link created\n");
733 return -EINVAL;
734 }
735 rmsg = ipa_bts_id_resp(link->line->ops->data,
736 data + 1, len - 1);
737 ipaccess_send(link->ofd->fd, rmsg->data, rmsg->len);
738 msgb_free(rmsg);
739
740 /* send ID_ACK. */
741 rmsg = ipa_bts_id_ack();
742 ipaccess_send(link->ofd->fd, rmsg->data, rmsg->len);
743 msgb_free(rmsg);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200744 }
745 return 0;
746 } else if (link->port == IPA_TCP_PORT_OML)
747 e1i_ts = &link->line->ts[0];
748 else if (link->port == IPA_TCP_PORT_RSL)
749 e1i_ts = &link->line->ts[1];
750
751 /* look up for some existing signaling link. */
752 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
753 if (sign_link == NULL) {
754 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
755 "hh->proto=0x%02x\n", hh->proto);
756 msgb_free(msg);
757 return -EIO;
758 }
759 msg->dst = sign_link;
Pablo Neira Ayusoff663632011-06-26 19:10:56 +0200760 msg->trx = sign_link->trx;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200761
762 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200763 if (!link->line->ops->sign_link) {
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200764 LOGP(DINP, LOGL_ERROR, "Fix your application, "
765 "no action set for signalling messages.\n");
766 return -ENOENT;
767 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200768 link->line->ops->sign_link(msg, sign_link);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200769 return 0;
770}
771
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200772static int ipaccess_line_update(struct e1inp_line *line,
773 enum e1inp_line_role role, const char *addr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200774{
775 int ret = -ENOENT;
776
777 switch(role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200778 case E1INP_LINE_R_BSC: {
779 struct ipa_server_link *oml_link, *rsl_link;
780
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200781 LOGP(DINP, LOGL_NOTICE, "enabling ipaccess BSC mode\n");
782
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200783 oml_link = ipa_server_link_create(tall_ipa_ctx, line,
784 "0.0.0.0", IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200785 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200786 if (oml_link == NULL) {
787 LOGP(DINP, LOGL_ERROR, "cannot create OML "
788 "BSC link: %s\n", strerror(errno));
789 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200790 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200791 if (ipa_server_link_open(oml_link) < 0) {
792 LOGP(DINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
793 strerror(errno));
794 ipa_server_link_close(oml_link);
795 ipa_server_link_destroy(oml_link);
796 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200797 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200798 rsl_link = ipa_server_link_create(tall_ipa_ctx, line,
799 "0.0.0.0", IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200800 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200801 if (rsl_link == NULL) {
802 LOGP(DINP, LOGL_ERROR, "cannot create RSL "
803 "BSC link: %s\n", strerror(errno));
804 return -ENOMEM;
805 }
806 if (ipa_server_link_open(rsl_link) < 0) {
807 LOGP(DINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
808 strerror(errno));
809 ipa_server_link_close(rsl_link);
810 ipa_server_link_destroy(rsl_link);
811 return -EIO;
812 }
813 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200814 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200815 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200816 case E1INP_LINE_R_BTS: {
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200817 struct ipa_client_link *link, *rsl_link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200818
819 LOGP(DINP, LOGL_NOTICE, "enabling ipaccess BTS mode\n");
820
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200821 link = ipa_client_link_create(tall_ipa_ctx,
822 &line->ts[E1INP_SIGN_OML-1],
823 "ipa", E1INP_SIGN_OML,
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200824 addr, IPA_TCP_PORT_OML,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200825 ipaccess_bts_cb,
826 ipaccess_bts_write_cb,
827 NULL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200828 if (link == NULL) {
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200829 LOGP(DINP, LOGL_ERROR, "cannot create OML "
830 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200831 return -ENOMEM;
832 }
833 if (ipa_client_link_open(link) < 0) {
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200834 LOGP(DINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
835 strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200836 ipa_client_link_close(link);
837 ipa_client_link_destroy(link);
838 return -EIO;
839 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200840 rsl_link = ipa_client_link_create(tall_ipa_ctx,
841 &line->ts[E1INP_SIGN_RSL-1],
842 "ipa", E1INP_SIGN_RSL,
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200843 addr, IPA_TCP_PORT_RSL,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200844 ipaccess_bts_cb,
845 ipaccess_bts_write_cb,
846 NULL);
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200847 if (rsl_link == NULL) {
848 LOGP(DINP, LOGL_ERROR, "cannot create RSL "
849 "BTS link: %s\n", strerror(errno));
850 return -ENOMEM;
851 }
852 if (ipa_client_link_open(rsl_link) < 0) {
853 LOGP(DINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
854 strerror(errno));
855 ipa_client_link_close(rsl_link);
856 ipa_client_link_destroy(rsl_link);
857 return -EIO;
858 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200859 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200860 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200861 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200862 default:
863 break;
864 }
865 return ret;
866}
867
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200868void e1inp_ipaccess_init(void)
869{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200870 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200871 e1inp_driver_register(&ipaccess_driver);
872}