blob: dc25430ceda5526c1f1e8f49b859fe1db9884b9c [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 Ayusocd8d2e52011-07-02 17:05:21 +0200284 LOGP(DINP, LOGL_ERROR,
285 "Unable to set signal link, closing socket.\n");
286 osmo_fd_unregister(bfd);
287 close(bfd->fd);
288 bfd->fd = -1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200289 return -EINVAL;
290 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200291 /* the BSC creates the new sign links at this stage. */
292 if (bfd->priv_nr == E1INP_SIGN_OML) {
293 sign_link =
294 line->ops->sign_link_up(&unit_data, line,
295 E1INP_SIGN_OML);
296 if (sign_link == NULL) {
297 LOGP(DINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200298 "Unable to set signal link, "
299 "closing socket.\n");
300 osmo_fd_unregister(bfd);
301 close(bfd->fd);
302 bfd->fd = -1;
303 return -EINVAL;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200304 }
305 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
306 struct e1inp_ts *e1i_ts;
307 struct osmo_fd *newbfd;
308
309 sign_link =
310 line->ops->sign_link_up(&unit_data, line,
311 E1INP_SIGN_RSL);
312 if (sign_link == NULL) {
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200313 LOGP(DINP, LOGL_ERROR,
314 "Unable to set signal link, "
315 "closing socket.\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200316 osmo_fd_unregister(bfd);
317 close(bfd->fd);
318 bfd->fd = -1;
319 talloc_free(bfd);
320 return 0;
321 }
322 /* Finally, we know which OML link is associated with
323 * this RSL link, attach it to this socket. */
324 bfd->data = line = sign_link->ts->line;
325 e1i_ts = &line->ts[E1INP_SIGN_RSL+unit_data.trx_id-1];
326 newbfd = &e1i_ts->driver.ipaccess.fd;
327
328 /* get rid of our old temporary bfd */
329 memcpy(newbfd, bfd, sizeof(*newbfd));
330 newbfd->priv_nr = E1INP_SIGN_RSL + unit_data.trx_id;
331 osmo_fd_unregister(bfd);
332 bfd->fd = -1;
333 talloc_free(bfd);
334 osmo_fd_register(newbfd);
335 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200336 break;
337 }
338 return 0;
339}
340
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200341static int handle_ts1_read(struct osmo_fd *bfd)
342{
343 struct e1inp_line *line = bfd->data;
344 unsigned int ts_nr = bfd->priv_nr;
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200345 struct e1inp_ts *e1i_ts = NULL;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200346 struct e1inp_sign_link *link;
347 struct ipaccess_head *hh;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200348 struct msgb *msg;
349 int ret = 0, error;
350
Pablo Neira Ayuso7a249402011-06-21 14:15:46 +0200351 error = ipa_msg_recv(bfd->fd, &msg);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200352 if (error < 0)
353 return error;
354 else if (error == 0) {
355 ipaccess_drop(bfd);
356 LOGP(DINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200357 return error;
358 }
359 DEBUGP(DMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
360
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200361 /* Now that we're sure that we've got a line for socket. */
362 e1i_ts = &line->ts[ts_nr-1];
363
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200364 hh = (struct ipaccess_head *) msg->data;
365 if (hh->proto == IPAC_PROTO_IPACCESS) {
366 ipaccess_rcvmsg(line, msg, bfd);
367 msgb_free(msg);
368 return ret;
369 }
370 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
371 * might have free'd it !!! */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200372
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200373 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
374 if (!link) {
375 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
376 "hh->proto=0x%02x\n", hh->proto);
377 msgb_free(msg);
378 return -EIO;
379 }
380 msg->dst = link;
Pablo Neira Ayusoff663632011-06-26 19:10:56 +0200381 msg->trx = link->trx;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200382
383 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200384 if (!e1i_ts->line->ops->sign_link) {
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200385 LOGP(DINP, LOGL_ERROR, "Fix your application, "
386 "no action set for signalling messages.\n");
387 return -ENOENT;
388 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200389 e1i_ts->line->ops->sign_link(msg, link);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200390
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200391 return ret;
392}
393
394void ipaccess_prepend_header_ext(struct msgb *msg, int proto)
395{
396 struct ipaccess_head_ext *hh_ext;
397
398 /* prepend the osmo ip.access header extension */
399 hh_ext = (struct ipaccess_head_ext *) msgb_push(msg, sizeof(*hh_ext));
400 hh_ext->proto = proto;
401}
402
403void ipaccess_prepend_header(struct msgb *msg, int proto)
404{
405 struct ipaccess_head *hh;
406
407 /* prepend the ip.access header */
408 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
409 hh->len = htons(msg->len - sizeof(*hh));
410 hh->proto = proto;
411}
412
413static int ts_want_write(struct e1inp_ts *e1i_ts)
414{
415 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
416
417 return 0;
418}
419
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200420static void ipaccess_close(struct e1inp_ts *e1i_ts)
421{
422 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
423 osmo_fd_unregister(bfd);
424 close(bfd->fd);
425 bfd->fd = -1;
426}
427
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200428static void timeout_ts1_write(void *data)
429{
430 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
431
432 /* trigger write of ts1, due to tx delay timer */
433 ts_want_write(e1i_ts);
434}
435
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200436static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200437{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200438 unsigned int ts_nr = bfd->priv_nr;
439 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
440 struct e1inp_sign_link *sign_link;
441 struct msgb *msg;
442 uint8_t proto;
443 int ret;
444
445 bfd->when &= ~BSC_FD_WRITE;
446
447 /* get the next msg for this timeslot */
448 msg = e1inp_tx_ts(e1i_ts, &sign_link);
449 if (!msg) {
450 /* no message after tx delay timer */
451 return 0;
452 }
453
454 switch (sign_link->type) {
455 case E1INP_SIGN_OML:
456 proto = IPAC_PROTO_OML;
457 break;
458 case E1INP_SIGN_RSL:
459 proto = IPAC_PROTO_RSL;
460 break;
461 default:
462 msgb_free(msg);
463 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
464 return -EINVAL;
465 }
466
467 msg->l2h = msg->data;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200468 /* This is an IPA CCM, it already contains the header, skip. */
469 if (msgb_tailroom(msg) < sizeof(struct ipaccess_head))
470 ipaccess_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200471
472 DEBUGP(DMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
473
474 ret = send(bfd->fd, msg->data, msg->len, 0);
475 msgb_free(msg);
476
477 /* set tx delay timer for next event */
478 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
479 e1i_ts->sign.tx_timer.data = e1i_ts;
480
481 /* Reducing this might break the nanoBTS 900 init. */
482 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
483
484 return ret;
485}
486
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200487int handle_ts1_write(struct osmo_fd *bfd)
488{
489 struct e1inp_line *line = bfd->data;
490
491 return __handle_ts1_write(bfd, line);
492}
493
494int ipaccess_bts_write_cb(struct ipa_client_link *link)
495{
496 struct e1inp_line *line = link->line;
497
498 return __handle_ts1_write(link->ofd, line);
499}
500
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200501/* callback from select.c in case one of the fd's can be read/written */
502static int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
503{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200504 int rc = 0;
505
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200506 if (what & BSC_FD_READ)
507 rc = handle_ts1_read(bfd);
508 if (what & BSC_FD_WRITE)
509 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200510
511 return rc;
512}
513
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200514static int ipaccess_line_update(struct e1inp_line *line,
515 enum e1inp_line_role role, const char *addr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200516
517struct e1inp_driver ipaccess_driver = {
518 .name = "ipa",
519 .want_write = ts_want_write,
520 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200521 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200522 .default_delay = 0,
523};
524
525/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200526static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200527{
528 int ret;
529 int idx = 0;
530 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200531 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200532 struct e1inp_ts *e1i_ts;
533 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200534
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200535 /* clone virtual E1 line for this new OML link. */
536 line = talloc_zero(tall_ipa_ctx, struct e1inp_line);
537 if (line == NULL) {
538 LOGP(DINP, LOGL_ERROR, "could not clone E1 line\n");
539 return -1;
540 }
541 memcpy(line, link->line, sizeof(struct e1inp_line));
542
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200543 /* create virrtual E1 timeslots for signalling */
Pablo Neira Ayuso59301852011-06-27 15:44:23 +0200544 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200545
546 /* initialize the fds */
547 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
548 line->ts[i].driver.ipaccess.fd.fd = -1;
549
550 e1i_ts = &line->ts[idx];
551
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200552 bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200553 bfd->fd = fd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200554 bfd->data = line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200555 bfd->priv_nr = E1INP_SIGN_OML;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200556 bfd->cb = ipaccess_fd_cb;
557 bfd->when = BSC_FD_READ;
558 ret = osmo_fd_register(bfd);
559 if (ret < 0) {
560 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
561 close(bfd->fd);
562 return ret;
563 }
564
565 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
566 ret = ipaccess_send_id_req(bfd->fd);
567
568 return ret;
569}
570
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200571static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200572{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200573 struct osmo_fd *bfd;
574 int ret;
575
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200576 /* We don't know yet which OML link to associate it with. Thus, we
577 * allocate a temporary bfd until we have received ID. */
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200578 bfd = talloc_zero(tall_ipa_ctx, struct osmo_fd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200579 if (!bfd)
580 return -ENOMEM;
581
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200582 bfd->fd = fd;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200583 /* This dummy line is replaced once we can attach it to the OML link */
584 bfd->data = link->line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200585 bfd->priv_nr = E1INP_SIGN_RSL;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200586 bfd->cb = ipaccess_fd_cb;
587 bfd->when = BSC_FD_READ;
588 ret = osmo_fd_register(bfd);
589 if (ret < 0) {
590 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
591 close(bfd->fd);
592 talloc_free(bfd);
593 return ret;
594 }
595 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
596 ret = ipaccess_send_id_req(bfd->fd);
597
598 return 0;
599}
600
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200601static struct msgb *abis_msgb_alloc(int headroom)
602{
603 struct msgb *nmsg;
604
605 headroom += sizeof(struct ipaccess_head);
606
607 nmsg = msgb_alloc_headroom(1200 + headroom, headroom, "dummy BTS");
608 if (!nmsg)
609 return NULL;
610 return nmsg;
611}
612
613static void abis_push_ipa(struct msgb *msg, uint8_t proto)
614{
615 struct ipaccess_head *nhh;
616
617 msg->l2h = msg->data;
618 nhh = (struct ipaccess_head *) msgb_push(msg, sizeof(*nhh));
619 nhh->proto = proto;
620 nhh->len = htons(msgb_l2len(msg));
621}
622
623static struct msgb *
624ipa_bts_id_resp(struct ipaccess_unit *dev, uint8_t *data, int len)
625{
626 struct msgb *nmsg;
627 char str[64];
628 uint8_t *tag;
629
630 nmsg = abis_msgb_alloc(0);
631 if (!nmsg)
632 return NULL;
633
634 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
635 while (len) {
636 if (len < 2) {
637 LOGP(DINP, LOGL_NOTICE,
638 "Short read of ipaccess tag\n");
639 msgb_free(nmsg);
640 return NULL;
641 }
642 switch (data[1]) {
643 case IPAC_IDTAG_UNIT:
644 sprintf(str, "%u/%u/%u",
645 dev->site_id, dev->bts_id, dev->trx_id);
646 break;
647 case IPAC_IDTAG_MACADDR:
648 sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
649 dev->mac_addr[0], dev->mac_addr[1],
650 dev->mac_addr[2], dev->mac_addr[3],
651 dev->mac_addr[4], dev->mac_addr[5]);
652 break;
653 case IPAC_IDTAG_LOCATION1:
654 strcpy(str, dev->location1);
655 break;
656 case IPAC_IDTAG_LOCATION2:
657 strcpy(str, dev->location2);
658 break;
659 case IPAC_IDTAG_EQUIPVERS:
660 strcpy(str, dev->equipvers);
661 break;
662 case IPAC_IDTAG_SWVERSION:
663 strcpy(str, dev->swversion);
664 break;
665 case IPAC_IDTAG_UNITNAME:
666 sprintf(str, "%s-%02x-%02x-%02x-%02x-%02x-%02x",
667 dev->unit_name,
668 dev->mac_addr[0], dev->mac_addr[1],
669 dev->mac_addr[2], dev->mac_addr[3],
670 dev->mac_addr[4], dev->mac_addr[5]);
671 break;
672 case IPAC_IDTAG_SERNR:
673 strcpy(str, dev->serno);
674 break;
675 default:
676 LOGP(DINP, LOGL_NOTICE,
677 "Unknown ipaccess tag 0x%02x\n", *data);
678 msgb_free(nmsg);
679 return NULL;
680 }
681 LOGP(DINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
682 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
683 tag[0] = 0x00;
684 tag[1] = 1 + strlen(str) + 1;
685 tag[2] = data[1];
686 memcpy(tag + 3, str, strlen(str) + 1);
687 data += 2;
688 len -= 2;
689 }
690 abis_push_ipa(nmsg, IPAC_PROTO_IPACCESS);
691 return nmsg;
692}
693
694static struct msgb *ipa_bts_id_ack(void)
695{
696 struct msgb *nmsg2;
697
698 nmsg2 = abis_msgb_alloc(0);
699 if (!nmsg2)
700 return NULL;
701
702 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
703 abis_push_ipa(nmsg2, IPAC_PROTO_IPACCESS);
704
705 return nmsg2;
706}
707
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200708static int ipaccess_bts_cb(struct ipa_client_link *link, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200709{
710 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
711 struct e1inp_ts *e1i_ts = NULL;
712 struct e1inp_sign_link *sign_link;
713
714 /* special handling for IPA CCM. */
715 if (hh->proto == IPAC_PROTO_IPACCESS) {
716 uint8_t msg_type = *(msg->l2h);
717
718 /* ping, pong and acknowledgment cases. */
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200719 ipaccess_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200720
721 /* this is a request for identification from the BSC. */
722 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200723 struct e1inp_sign_link *sign_link;
724 struct msgb *rmsg;
725 uint8_t *data = msgb_l2(msg);
726 int len = msgb_l2len(msg);
727
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200728 LOGP(DINP, LOGL_NOTICE, "received ID get\n");
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200729 if (!link->line->ops->sign_link_up) {
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200730 LOGP(DINP, LOGL_ERROR,
731 "Unable to set signal link, "
732 "closing socket.\n");
733 osmo_fd_unregister(link->ofd);
734 close(link->ofd->fd);
735 link->ofd->fd = -1;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200736 return -EINVAL;
737 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200738 sign_link = link->line->ops->sign_link_up(msg,
739 link->line,
740 link->ofd->priv_nr);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200741 if (sign_link == NULL) {
742 LOGP(DINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200743 "Unable to set signal link, "
744 "closing socket.\n");
745 osmo_fd_unregister(link->ofd);
746 close(link->ofd->fd);
747 link->ofd->fd = -1;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200748 return -EINVAL;
749 }
750 rmsg = ipa_bts_id_resp(link->line->ops->data,
751 data + 1, len - 1);
752 ipaccess_send(link->ofd->fd, rmsg->data, rmsg->len);
753 msgb_free(rmsg);
754
755 /* send ID_ACK. */
756 rmsg = ipa_bts_id_ack();
757 ipaccess_send(link->ofd->fd, rmsg->data, rmsg->len);
758 msgb_free(rmsg);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200759 }
760 return 0;
761 } else if (link->port == IPA_TCP_PORT_OML)
762 e1i_ts = &link->line->ts[0];
763 else if (link->port == IPA_TCP_PORT_RSL)
764 e1i_ts = &link->line->ts[1];
765
766 /* look up for some existing signaling link. */
767 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
768 if (sign_link == NULL) {
769 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
770 "hh->proto=0x%02x\n", hh->proto);
771 msgb_free(msg);
772 return -EIO;
773 }
774 msg->dst = sign_link;
Pablo Neira Ayusoff663632011-06-26 19:10:56 +0200775 msg->trx = sign_link->trx;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200776
777 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200778 if (!link->line->ops->sign_link) {
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200779 LOGP(DINP, LOGL_ERROR, "Fix your application, "
780 "no action set for signalling messages.\n");
781 return -ENOENT;
782 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200783 link->line->ops->sign_link(msg, sign_link);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200784 return 0;
785}
786
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200787static int ipaccess_line_update(struct e1inp_line *line,
788 enum e1inp_line_role role, const char *addr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200789{
790 int ret = -ENOENT;
791
792 switch(role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200793 case E1INP_LINE_R_BSC: {
794 struct ipa_server_link *oml_link, *rsl_link;
795
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200796 LOGP(DINP, LOGL_NOTICE, "enabling ipaccess BSC mode\n");
797
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200798 oml_link = ipa_server_link_create(tall_ipa_ctx, line,
799 "0.0.0.0", IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200800 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200801 if (oml_link == NULL) {
802 LOGP(DINP, LOGL_ERROR, "cannot create OML "
803 "BSC link: %s\n", strerror(errno));
804 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200805 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200806 if (ipa_server_link_open(oml_link) < 0) {
807 LOGP(DINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
808 strerror(errno));
809 ipa_server_link_close(oml_link);
810 ipa_server_link_destroy(oml_link);
811 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200812 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200813 rsl_link = ipa_server_link_create(tall_ipa_ctx, line,
814 "0.0.0.0", IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200815 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200816 if (rsl_link == NULL) {
817 LOGP(DINP, LOGL_ERROR, "cannot create RSL "
818 "BSC link: %s\n", strerror(errno));
819 return -ENOMEM;
820 }
821 if (ipa_server_link_open(rsl_link) < 0) {
822 LOGP(DINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
823 strerror(errno));
824 ipa_server_link_close(rsl_link);
825 ipa_server_link_destroy(rsl_link);
826 return -EIO;
827 }
828 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200829 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200830 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200831 case E1INP_LINE_R_BTS: {
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200832 struct ipa_client_link *link, *rsl_link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200833
834 LOGP(DINP, LOGL_NOTICE, "enabling ipaccess BTS mode\n");
835
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200836 link = ipa_client_link_create(tall_ipa_ctx,
837 &line->ts[E1INP_SIGN_OML-1],
838 "ipa", E1INP_SIGN_OML,
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200839 addr, IPA_TCP_PORT_OML,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200840 ipaccess_bts_cb,
841 ipaccess_bts_write_cb,
842 NULL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200843 if (link == NULL) {
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200844 LOGP(DINP, LOGL_ERROR, "cannot create OML "
845 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200846 return -ENOMEM;
847 }
848 if (ipa_client_link_open(link) < 0) {
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200849 LOGP(DINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
850 strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200851 ipa_client_link_close(link);
852 ipa_client_link_destroy(link);
853 return -EIO;
854 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200855 rsl_link = ipa_client_link_create(tall_ipa_ctx,
856 &line->ts[E1INP_SIGN_RSL-1],
857 "ipa", E1INP_SIGN_RSL,
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200858 addr, IPA_TCP_PORT_RSL,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200859 ipaccess_bts_cb,
860 ipaccess_bts_write_cb,
861 NULL);
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200862 if (rsl_link == NULL) {
863 LOGP(DINP, LOGL_ERROR, "cannot create RSL "
864 "BTS link: %s\n", strerror(errno));
865 return -ENOMEM;
866 }
867 if (ipa_client_link_open(rsl_link) < 0) {
868 LOGP(DINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
869 strerror(errno));
870 ipa_client_link_close(rsl_link);
871 ipa_client_link_destroy(rsl_link);
872 return -EIO;
873 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200874 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200875 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200876 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200877 default:
878 break;
879 }
880 return ret;
881}
882
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200883void e1inp_ipaccess_init(void)
884{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200885 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200886 e1inp_driver_register(&ipaccess_driver);
887}