blob: ec4b2319303bdc24ed0f2c895c0187ea18881ff9 [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 Ayusodbd82fb2011-07-05 15:29:23 +0200389 e1i_ts->line->ops->sign_link(msg);
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 Ayusoadd3ec82011-07-05 14:45:46 +0200420static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200421{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200422 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200423 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200424 e1inp_event(e1i_ts, S_INP_TEI_DN, sign_link->tei, sign_link->sapi);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200425 osmo_fd_unregister(bfd);
426 close(bfd->fd);
427 bfd->fd = -1;
428}
429
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200430static void timeout_ts1_write(void *data)
431{
432 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
433
434 /* trigger write of ts1, due to tx delay timer */
435 ts_want_write(e1i_ts);
436}
437
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200438static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200439{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200440 unsigned int ts_nr = bfd->priv_nr;
441 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
442 struct e1inp_sign_link *sign_link;
443 struct msgb *msg;
444 uint8_t proto;
445 int ret;
446
447 bfd->when &= ~BSC_FD_WRITE;
448
449 /* get the next msg for this timeslot */
450 msg = e1inp_tx_ts(e1i_ts, &sign_link);
451 if (!msg) {
452 /* no message after tx delay timer */
453 return 0;
454 }
455
456 switch (sign_link->type) {
457 case E1INP_SIGN_OML:
458 proto = IPAC_PROTO_OML;
459 break;
460 case E1INP_SIGN_RSL:
461 proto = IPAC_PROTO_RSL;
462 break;
463 default:
464 msgb_free(msg);
465 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
466 return -EINVAL;
467 }
468
469 msg->l2h = msg->data;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200470 /* This is an IPA CCM, it already contains the header, skip. */
471 if (msgb_tailroom(msg) < sizeof(struct ipaccess_head))
472 ipaccess_prepend_header(msg, sign_link->tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200473
474 DEBUGP(DMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
475
476 ret = send(bfd->fd, msg->data, msg->len, 0);
477 msgb_free(msg);
478
479 /* set tx delay timer for next event */
480 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
481 e1i_ts->sign.tx_timer.data = e1i_ts;
482
483 /* Reducing this might break the nanoBTS 900 init. */
484 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
485
486 return ret;
487}
488
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200489int handle_ts1_write(struct osmo_fd *bfd)
490{
491 struct e1inp_line *line = bfd->data;
492
493 return __handle_ts1_write(bfd, line);
494}
495
496int ipaccess_bts_write_cb(struct ipa_client_link *link)
497{
498 struct e1inp_line *line = link->line;
499
500 return __handle_ts1_write(link->ofd, line);
501}
502
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200503/* callback from select.c in case one of the fd's can be read/written */
504static int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
505{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200506 int rc = 0;
507
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200508 if (what & BSC_FD_READ)
509 rc = handle_ts1_read(bfd);
510 if (what & BSC_FD_WRITE)
511 rc = handle_ts1_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200512
513 return rc;
514}
515
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200516static int ipaccess_line_update(struct e1inp_line *line,
517 enum e1inp_line_role role, const char *addr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200518
519struct e1inp_driver ipaccess_driver = {
520 .name = "ipa",
521 .want_write = ts_want_write,
522 .line_update = ipaccess_line_update,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200523 .close = ipaccess_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200524 .default_delay = 0,
525};
526
527/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200528static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200529{
530 int ret;
531 int idx = 0;
532 int i;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200533 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200534 struct e1inp_ts *e1i_ts;
535 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200536
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200537 /* clone virtual E1 line for this new OML link. */
538 line = talloc_zero(tall_ipa_ctx, struct e1inp_line);
539 if (line == NULL) {
540 LOGP(DINP, LOGL_ERROR, "could not clone E1 line\n");
541 return -1;
542 }
543 memcpy(line, link->line, sizeof(struct e1inp_line));
544
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200545 /* create virrtual E1 timeslots for signalling */
Pablo Neira Ayuso59301852011-06-27 15:44:23 +0200546 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML-1], line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200547
548 /* initialize the fds */
549 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
550 line->ts[i].driver.ipaccess.fd.fd = -1;
551
552 e1i_ts = &line->ts[idx];
553
Pablo Neira Ayuso93c62012011-06-26 19:12:47 +0200554 bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200555 bfd->fd = fd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200556 bfd->data = line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200557 bfd->priv_nr = E1INP_SIGN_OML;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200558 bfd->cb = ipaccess_fd_cb;
559 bfd->when = BSC_FD_READ;
560 ret = osmo_fd_register(bfd);
561 if (ret < 0) {
562 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
563 close(bfd->fd);
564 return ret;
565 }
566
567 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
568 ret = ipaccess_send_id_req(bfd->fd);
569
570 return ret;
571}
572
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200573static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200574{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200575 struct osmo_fd *bfd;
576 int ret;
577
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200578 /* We don't know yet which OML link to associate it with. Thus, we
579 * allocate a temporary bfd until we have received ID. */
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200580 bfd = talloc_zero(tall_ipa_ctx, struct osmo_fd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200581 if (!bfd)
582 return -ENOMEM;
583
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200584 bfd->fd = fd;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200585 /* This dummy line is replaced once we can attach it to the OML link */
586 bfd->data = link->line;
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200587 bfd->priv_nr = E1INP_SIGN_RSL;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200588 bfd->cb = ipaccess_fd_cb;
589 bfd->when = BSC_FD_READ;
590 ret = osmo_fd_register(bfd);
591 if (ret < 0) {
592 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
593 close(bfd->fd);
594 talloc_free(bfd);
595 return ret;
596 }
597 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
598 ret = ipaccess_send_id_req(bfd->fd);
599
600 return 0;
601}
602
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200603static struct msgb *abis_msgb_alloc(int headroom)
604{
605 struct msgb *nmsg;
606
607 headroom += sizeof(struct ipaccess_head);
608
609 nmsg = msgb_alloc_headroom(1200 + headroom, headroom, "dummy BTS");
610 if (!nmsg)
611 return NULL;
612 return nmsg;
613}
614
615static void abis_push_ipa(struct msgb *msg, uint8_t proto)
616{
617 struct ipaccess_head *nhh;
618
619 msg->l2h = msg->data;
620 nhh = (struct ipaccess_head *) msgb_push(msg, sizeof(*nhh));
621 nhh->proto = proto;
622 nhh->len = htons(msgb_l2len(msg));
623}
624
625static struct msgb *
626ipa_bts_id_resp(struct ipaccess_unit *dev, uint8_t *data, int len)
627{
628 struct msgb *nmsg;
629 char str[64];
630 uint8_t *tag;
631
632 nmsg = abis_msgb_alloc(0);
633 if (!nmsg)
634 return NULL;
635
636 *msgb_put(nmsg, 1) = IPAC_MSGT_ID_RESP;
637 while (len) {
638 if (len < 2) {
639 LOGP(DINP, LOGL_NOTICE,
640 "Short read of ipaccess tag\n");
641 msgb_free(nmsg);
642 return NULL;
643 }
644 switch (data[1]) {
645 case IPAC_IDTAG_UNIT:
646 sprintf(str, "%u/%u/%u",
647 dev->site_id, dev->bts_id, dev->trx_id);
648 break;
649 case IPAC_IDTAG_MACADDR:
650 sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
651 dev->mac_addr[0], dev->mac_addr[1],
652 dev->mac_addr[2], dev->mac_addr[3],
653 dev->mac_addr[4], dev->mac_addr[5]);
654 break;
655 case IPAC_IDTAG_LOCATION1:
656 strcpy(str, dev->location1);
657 break;
658 case IPAC_IDTAG_LOCATION2:
659 strcpy(str, dev->location2);
660 break;
661 case IPAC_IDTAG_EQUIPVERS:
662 strcpy(str, dev->equipvers);
663 break;
664 case IPAC_IDTAG_SWVERSION:
665 strcpy(str, dev->swversion);
666 break;
667 case IPAC_IDTAG_UNITNAME:
668 sprintf(str, "%s-%02x-%02x-%02x-%02x-%02x-%02x",
669 dev->unit_name,
670 dev->mac_addr[0], dev->mac_addr[1],
671 dev->mac_addr[2], dev->mac_addr[3],
672 dev->mac_addr[4], dev->mac_addr[5]);
673 break;
674 case IPAC_IDTAG_SERNR:
675 strcpy(str, dev->serno);
676 break;
677 default:
678 LOGP(DINP, LOGL_NOTICE,
679 "Unknown ipaccess tag 0x%02x\n", *data);
680 msgb_free(nmsg);
681 return NULL;
682 }
683 LOGP(DINP, LOGL_INFO, " tag %d: %s\n", data[1], str);
684 tag = msgb_put(nmsg, 3 + strlen(str) + 1);
685 tag[0] = 0x00;
686 tag[1] = 1 + strlen(str) + 1;
687 tag[2] = data[1];
688 memcpy(tag + 3, str, strlen(str) + 1);
689 data += 2;
690 len -= 2;
691 }
692 abis_push_ipa(nmsg, IPAC_PROTO_IPACCESS);
693 return nmsg;
694}
695
696static struct msgb *ipa_bts_id_ack(void)
697{
698 struct msgb *nmsg2;
699
700 nmsg2 = abis_msgb_alloc(0);
701 if (!nmsg2)
702 return NULL;
703
704 *msgb_put(nmsg2, 1) = IPAC_MSGT_ID_ACK;
705 abis_push_ipa(nmsg2, IPAC_PROTO_IPACCESS);
706
707 return nmsg2;
708}
709
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200710static int ipaccess_bts_cb(struct ipa_client_link *link, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200711{
712 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
713 struct e1inp_ts *e1i_ts = NULL;
714 struct e1inp_sign_link *sign_link;
715
716 /* special handling for IPA CCM. */
717 if (hh->proto == IPAC_PROTO_IPACCESS) {
718 uint8_t msg_type = *(msg->l2h);
719
720 /* ping, pong and acknowledgment cases. */
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200721 ipaccess_rcvmsg_bts_base(msg, link->ofd);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200722
723 /* this is a request for identification from the BSC. */
724 if (msg_type == IPAC_MSGT_ID_GET) {
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200725 struct e1inp_sign_link *sign_link;
726 struct msgb *rmsg;
727 uint8_t *data = msgb_l2(msg);
728 int len = msgb_l2len(msg);
729
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200730 LOGP(DINP, LOGL_NOTICE, "received ID get\n");
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200731 if (!link->line->ops->sign_link_up) {
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200732 LOGP(DINP, LOGL_ERROR,
733 "Unable to set signal link, "
734 "closing socket.\n");
735 osmo_fd_unregister(link->ofd);
736 close(link->ofd->fd);
737 link->ofd->fd = -1;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200738 return -EINVAL;
739 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200740 sign_link = link->line->ops->sign_link_up(msg,
741 link->line,
742 link->ofd->priv_nr);
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200743 if (sign_link == NULL) {
744 LOGP(DINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200745 "Unable to set signal link, "
746 "closing socket.\n");
747 osmo_fd_unregister(link->ofd);
748 close(link->ofd->fd);
749 link->ofd->fd = -1;
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200750 return -EINVAL;
751 }
752 rmsg = ipa_bts_id_resp(link->line->ops->data,
753 data + 1, len - 1);
754 ipaccess_send(link->ofd->fd, rmsg->data, rmsg->len);
755 msgb_free(rmsg);
756
757 /* send ID_ACK. */
758 rmsg = ipa_bts_id_ack();
759 ipaccess_send(link->ofd->fd, rmsg->data, rmsg->len);
760 msgb_free(rmsg);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200761 }
762 return 0;
763 } else if (link->port == IPA_TCP_PORT_OML)
764 e1i_ts = &link->line->ts[0];
765 else if (link->port == IPA_TCP_PORT_RSL)
766 e1i_ts = &link->line->ts[1];
767
768 /* look up for some existing signaling link. */
769 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
770 if (sign_link == NULL) {
771 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
772 "hh->proto=0x%02x\n", hh->proto);
773 msgb_free(msg);
774 return -EIO;
775 }
776 msg->dst = sign_link;
Pablo Neira Ayusoff663632011-06-26 19:10:56 +0200777 msg->trx = sign_link->trx;
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200778
779 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200780 if (!link->line->ops->sign_link) {
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200781 LOGP(DINP, LOGL_ERROR, "Fix your application, "
782 "no action set for signalling messages.\n");
783 return -ENOENT;
784 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200785 link->line->ops->sign_link(msg);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200786 return 0;
787}
788
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200789static int ipaccess_line_update(struct e1inp_line *line,
790 enum e1inp_line_role role, const char *addr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200791{
792 int ret = -ENOENT;
793
794 switch(role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200795 case E1INP_LINE_R_BSC: {
796 struct ipa_server_link *oml_link, *rsl_link;
797
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200798 LOGP(DINP, LOGL_NOTICE, "enabling ipaccess BSC mode\n");
799
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200800 oml_link = ipa_server_link_create(tall_ipa_ctx, line,
801 "0.0.0.0", IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200802 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200803 if (oml_link == NULL) {
804 LOGP(DINP, LOGL_ERROR, "cannot create OML "
805 "BSC link: %s\n", strerror(errno));
806 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200807 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200808 if (ipa_server_link_open(oml_link) < 0) {
809 LOGP(DINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
810 strerror(errno));
811 ipa_server_link_close(oml_link);
812 ipa_server_link_destroy(oml_link);
813 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200814 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200815 rsl_link = ipa_server_link_create(tall_ipa_ctx, line,
816 "0.0.0.0", IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200817 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200818 if (rsl_link == NULL) {
819 LOGP(DINP, LOGL_ERROR, "cannot create RSL "
820 "BSC link: %s\n", strerror(errno));
821 return -ENOMEM;
822 }
823 if (ipa_server_link_open(rsl_link) < 0) {
824 LOGP(DINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
825 strerror(errno));
826 ipa_server_link_close(rsl_link);
827 ipa_server_link_destroy(rsl_link);
828 return -EIO;
829 }
830 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200831 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200832 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200833 case E1INP_LINE_R_BTS: {
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200834 struct ipa_client_link *link, *rsl_link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200835
836 LOGP(DINP, LOGL_NOTICE, "enabling ipaccess BTS mode\n");
837
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200838 link = ipa_client_link_create(tall_ipa_ctx,
839 &line->ts[E1INP_SIGN_OML-1],
840 "ipa", E1INP_SIGN_OML,
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200841 addr, IPA_TCP_PORT_OML,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200842 ipaccess_bts_cb,
843 ipaccess_bts_write_cb,
844 NULL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200845 if (link == NULL) {
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200846 LOGP(DINP, LOGL_ERROR, "cannot create OML "
847 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200848 return -ENOMEM;
849 }
850 if (ipa_client_link_open(link) < 0) {
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200851 LOGP(DINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
852 strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200853 ipa_client_link_close(link);
854 ipa_client_link_destroy(link);
855 return -EIO;
856 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200857 rsl_link = ipa_client_link_create(tall_ipa_ctx,
858 &line->ts[E1INP_SIGN_RSL-1],
859 "ipa", E1INP_SIGN_RSL,
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200860 addr, IPA_TCP_PORT_RSL,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200861 ipaccess_bts_cb,
862 ipaccess_bts_write_cb,
863 NULL);
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200864 if (rsl_link == NULL) {
865 LOGP(DINP, LOGL_ERROR, "cannot create RSL "
866 "BTS link: %s\n", strerror(errno));
867 return -ENOMEM;
868 }
869 if (ipa_client_link_open(rsl_link) < 0) {
870 LOGP(DINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
871 strerror(errno));
872 ipa_client_link_close(rsl_link);
873 ipa_client_link_destroy(rsl_link);
874 return -EIO;
875 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200876 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200877 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200878 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200879 default:
880 break;
881 }
882 return ret;
883}
884
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200885void e1inp_ipaccess_init(void)
886{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200887 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200888 e1inp_driver_register(&ipaccess_driver);
889}