blob: 5ecd2e66945c241ed592ea8a3445649219e793b9 [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 Ayuso96e81282011-06-09 15:06:11 +020045#include <osmocom/abis/ipa.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020046
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +020047static void *tall_ipa_ctx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020048
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020049#define TS1_ALLOC_SIZE 900
50
51/*
52 * Common propietary IPA messages:
53 * - PONG: in reply to PING.
54 * - ID_REQUEST: first messages once OML has been established.
55 * - ID_ACK: in reply to ID_ACK.
56 */
57const uint8_t ipa_pong_msg[] = {
58 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG
59};
60
61const uint8_t ipa_id_ack_msg[] = {
62 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK
63};
64
65const uint8_t ipa_id_req_msg[] = {
66 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET,
67 0x01, IPAC_IDTAG_UNIT,
68 0x01, IPAC_IDTAG_MACADDR,
69 0x01, IPAC_IDTAG_LOCATION1,
70 0x01, IPAC_IDTAG_LOCATION2,
71 0x01, IPAC_IDTAG_EQUIPVERS,
72 0x01, IPAC_IDTAG_SWVERSION,
73 0x01, IPAC_IDTAG_UNITNAME,
74 0x01, IPAC_IDTAG_SERNR,
75};
76
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020077static const char *idtag_names[] = {
78 [IPAC_IDTAG_SERNR] = "Serial_Number",
79 [IPAC_IDTAG_UNITNAME] = "Unit_Name",
80 [IPAC_IDTAG_LOCATION1] = "Location_1",
81 [IPAC_IDTAG_LOCATION2] = "Location_2",
82 [IPAC_IDTAG_EQUIPVERS] = "Equipment_Version",
83 [IPAC_IDTAG_SWVERSION] = "Software_Version",
84 [IPAC_IDTAG_IPADDR] = "IP_Address",
85 [IPAC_IDTAG_MACADDR] = "MAC_Address",
86 [IPAC_IDTAG_UNIT] = "Unit_ID",
87};
88
89const char *ipaccess_idtag_name(uint8_t tag)
90{
91 if (tag >= ARRAY_SIZE(idtag_names))
92 return "unknown";
93
94 return idtag_names[tag];
95}
96
97int ipaccess_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len)
98{
99 uint8_t t_len;
100 uint8_t t_tag;
101 uint8_t *cur = buf;
102
103 memset(dec, 0, sizeof(*dec));
104
105 while (len >= 2) {
106 len -= 2;
107 t_len = *cur++;
108 t_tag = *cur++;
109
110 if (t_len > len + 1) {
111 LOGP(DMI, LOGL_ERROR, "The tag does not fit: %d\n", t_len);
112 return -EINVAL;
113 }
114
115 DEBUGPC(DMI, "%s='%s' ", ipaccess_idtag_name(t_tag), cur);
116
117 dec->lv[t_tag].len = t_len;
118 dec->lv[t_tag].val = cur;
119
120 cur += t_len;
121 len -= t_len;
122 }
123 return 0;
124}
125
126int ipaccess_parse_unitid(const char *str, struct ipaccess_unit *unit_data)
127{
128 unsigned long ul;
129 char *endptr;
130 const char *nptr;
131
132 nptr = str;
133 ul = strtoul(nptr, &endptr, 10);
134 if (endptr <= nptr)
135 return -EINVAL;
Pablo Neira Ayuso62d345a2011-07-05 16:37:37 +0200136 unit_data->site_id = ul & 0xffff;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200137
138 if (*endptr++ != '/')
139 return -EINVAL;
140
141 nptr = endptr;
142 ul = strtoul(nptr, &endptr, 10);
143 if (endptr <= nptr)
144 return -EINVAL;
Pablo Neira Ayuso62d345a2011-07-05 16:37:37 +0200145 unit_data->bts_id = ul & 0xffff;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200146
147 if (*endptr++ != '/')
148 return -EINVAL;
149
150 nptr = endptr;
151 ul = strtoul(nptr, &endptr, 10);
152 if (endptr <= nptr)
153 return -EINVAL;
Pablo Neira Ayuso62d345a2011-07-05 16:37:37 +0200154 unit_data->trx_id = ul & 0xffff;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200155
156 return 0;
157}
158
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200159static int ipaccess_send(int fd, const void *msg, size_t msglen)
160{
161 int ret;
162
163 ret = write(fd, msg, msglen);
164 if (ret < 0)
165 return ret;
166 if (ret < msglen) {
167 LOGP(DINP, LOGL_ERROR, "ipaccess_send: short write\n");
168 return -EIO;
169 }
170 return ret;
171}
172
173int ipaccess_send_pong(int fd)
174{
175 return ipaccess_send(fd, ipa_pong_msg, sizeof(ipa_pong_msg));
176}
177
178int ipaccess_send_id_ack(int fd)
179{
180 return ipaccess_send(fd, ipa_id_ack_msg, sizeof(ipa_id_ack_msg));
181}
182
183int ipaccess_send_id_req(int fd)
184{
185 return ipaccess_send(fd, ipa_id_req_msg, sizeof(ipa_id_req_msg));
186}
187
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200188/* base handling of the ip.access protocol */
189int ipaccess_rcvmsg_base(struct msgb *msg,
190 struct osmo_fd *bfd)
191{
192 uint8_t msg_type = *(msg->l2h);
193 int ret = 0;
194
195 switch (msg_type) {
196 case IPAC_MSGT_PING:
197 ret = ipaccess_send_pong(bfd->fd);
198 break;
199 case IPAC_MSGT_PONG:
200 DEBUGP(DMI, "PONG!\n");
201 break;
202 case IPAC_MSGT_ID_ACK:
203 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
204 ret = ipaccess_send_id_ack(bfd->fd);
205 break;
206 }
207 return 0;
208}
209
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200210/* base handling of the ip.access protocol */
211int ipaccess_rcvmsg_bts_base(struct msgb *msg,
212 struct osmo_fd *bfd)
213{
214 uint8_t msg_type = *(msg->l2h);
215 int ret = 0;
216
217 switch (msg_type) {
218 case IPAC_MSGT_PING:
219 ret = ipaccess_send_pong(bfd->fd);
220 break;
221 case IPAC_MSGT_PONG:
222 DEBUGP(DMI, "PONG!\n");
223 break;
224 case IPAC_MSGT_ID_ACK:
225 DEBUGP(DMI, "ID_ACK\n");
226 break;
227 }
228 return 0;
229}
230
231static void ipaccess_drop(struct osmo_fd *bfd)
232{
233 struct e1inp_line *line = bfd->data;
234
235 /* e1inp_sign_link_destroy releases the socket descriptors for us. */
236 line->ops->sign_link_down(line);
237
238 /* RSL connection without ID_RESP, release temporary socket. */
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +0200239 if (line->ts[E1INP_SIGN_OML-1].type == E1INP_SIGN_NONE) {
240 osmo_fd_unregister(bfd);
241 close(bfd->fd);
242 bfd->fd = -1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200243 talloc_free(bfd);
Pablo Neira Ayuso9621b412011-07-07 16:19:21 +0200244 }
Pablo Neira Ayusod2fba902011-07-07 17:42:07 +0200245 /* release the virtual E1 line that we cloned for this socket,
246 * OML and RSL links should have been closed after sign_link_down. */
247 talloc_free(line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200248}
249
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200250static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
251 struct osmo_fd *bfd)
252{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200253 struct tlv_parsed tlvp;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200254 uint8_t msg_type = *(msg->l2h);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200255 struct ipaccess_unit unit_data = {};
256 struct e1inp_sign_link *sign_link;
257 char *unitid;
258 int len, ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200259
260 /* handle base messages */
261 ipaccess_rcvmsg_base(msg, bfd);
262
263 switch (msg_type) {
264 case IPAC_MSGT_ID_RESP:
265 DEBUGP(DMI, "ID_RESP\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200266 /* parse tags, search for Unit ID */
267 ret = ipaccess_idtag_parse(&tlvp, (uint8_t *)msg->l2h + 2,
268 msgb_l2len(msg)-2);
269 DEBUGP(DMI, "\n");
270 if (ret < 0) {
271 LOGP(DINP, LOGL_ERROR, "ignoring IPA response message "
272 "with malformed TLVs\n");
273 return ret;
274 }
275 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT))
276 break;
277
278 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
279 if (len < 1)
280 break;
281
282 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
283 unitid[len - 1] = '\0';
284 ipaccess_parse_unitid(unitid, &unit_data);
285
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200286 if (!line->ops->sign_link_up) {
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200287 LOGP(DINP, LOGL_ERROR,
288 "Unable to set signal link, closing socket.\n");
289 osmo_fd_unregister(bfd);
290 close(bfd->fd);
291 bfd->fd = -1;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200292 return -EINVAL;
293 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200294 /* the BSC creates the new sign links at this stage. */
295 if (bfd->priv_nr == E1INP_SIGN_OML) {
296 sign_link =
297 line->ops->sign_link_up(&unit_data, line,
298 E1INP_SIGN_OML);
299 if (sign_link == NULL) {
300 LOGP(DINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200301 "Unable to set signal link, "
302 "closing socket.\n");
303 osmo_fd_unregister(bfd);
304 close(bfd->fd);
305 bfd->fd = -1;
306 return -EINVAL;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200307 }
308 } else if (bfd->priv_nr == E1INP_SIGN_RSL) {
309 struct e1inp_ts *e1i_ts;
310 struct osmo_fd *newbfd;
311
312 sign_link =
313 line->ops->sign_link_up(&unit_data, line,
314 E1INP_SIGN_RSL);
315 if (sign_link == NULL) {
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200316 LOGP(DINP, LOGL_ERROR,
317 "Unable to set signal link, "
318 "closing socket.\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200319 osmo_fd_unregister(bfd);
320 close(bfd->fd);
321 bfd->fd = -1;
322 talloc_free(bfd);
323 return 0;
324 }
325 /* Finally, we know which OML link is associated with
326 * this RSL link, attach it to this socket. */
327 bfd->data = line = sign_link->ts->line;
328 e1i_ts = &line->ts[E1INP_SIGN_RSL+unit_data.trx_id-1];
329 newbfd = &e1i_ts->driver.ipaccess.fd;
330
331 /* get rid of our old temporary bfd */
332 memcpy(newbfd, bfd, sizeof(*newbfd));
333 newbfd->priv_nr = E1INP_SIGN_RSL + unit_data.trx_id;
334 osmo_fd_unregister(bfd);
335 bfd->fd = -1;
336 talloc_free(bfd);
337 osmo_fd_register(newbfd);
338 }
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200339 break;
340 }
341 return 0;
342}
343
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200344static int handle_ts1_read(struct osmo_fd *bfd)
345{
346 struct e1inp_line *line = bfd->data;
347 unsigned int ts_nr = bfd->priv_nr;
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200348 struct e1inp_ts *e1i_ts = NULL;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200349 struct e1inp_sign_link *link;
350 struct ipaccess_head *hh;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200351 struct msgb *msg;
352 int ret = 0, error;
353
Pablo Neira Ayuso7a249402011-06-21 14:15:46 +0200354 error = ipa_msg_recv(bfd->fd, &msg);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200355 if (error < 0)
356 return error;
357 else if (error == 0) {
358 ipaccess_drop(bfd);
359 LOGP(DINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200360 return error;
361 }
362 DEBUGP(DMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
363
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200364 /* Now that we're sure that we've got a line for socket. */
365 e1i_ts = &line->ts[ts_nr-1];
366
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200367 hh = (struct ipaccess_head *) msg->data;
368 if (hh->proto == IPAC_PROTO_IPACCESS) {
369 ipaccess_rcvmsg(line, msg, bfd);
370 msgb_free(msg);
371 return ret;
372 }
373 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
374 * might have free'd it !!! */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200375
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200376 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
377 if (!link) {
378 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
379 "hh->proto=0x%02x\n", hh->proto);
380 msgb_free(msg);
381 return -EIO;
382 }
383 msg->dst = link;
384
385 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200386 if (!e1i_ts->line->ops->sign_link) {
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200387 LOGP(DINP, LOGL_ERROR, "Fix your application, "
388 "no action set for signalling messages.\n");
389 return -ENOENT;
390 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200391 e1i_ts->line->ops->sign_link(msg);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200392
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200393 return ret;
394}
395
396void ipaccess_prepend_header_ext(struct msgb *msg, int proto)
397{
398 struct ipaccess_head_ext *hh_ext;
399
400 /* prepend the osmo ip.access header extension */
401 hh_ext = (struct ipaccess_head_ext *) msgb_push(msg, sizeof(*hh_ext));
402 hh_ext->proto = proto;
403}
404
405void ipaccess_prepend_header(struct msgb *msg, int proto)
406{
407 struct ipaccess_head *hh;
408
409 /* prepend the ip.access header */
410 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
411 hh->len = htons(msg->len - sizeof(*hh));
412 hh->proto = proto;
413}
414
415static int ts_want_write(struct e1inp_ts *e1i_ts)
416{
417 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
418
419 return 0;
420}
421
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200422static void ipaccess_close(struct e1inp_sign_link *sign_link)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200423{
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200424 struct e1inp_ts *e1i_ts = sign_link->ts;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200425 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200426 e1inp_event(e1i_ts, S_INP_TEI_DN, sign_link->tei, sign_link->sapi);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200427 osmo_fd_unregister(bfd);
428 close(bfd->fd);
429 bfd->fd = -1;
430}
431
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200432static void timeout_ts1_write(void *data)
433{
434 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
435
436 /* trigger write of ts1, due to tx delay timer */
437 ts_want_write(e1i_ts);
438}
439
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200440static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200441{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200442 unsigned int ts_nr = bfd->priv_nr;
443 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
444 struct e1inp_sign_link *sign_link;
445 struct msgb *msg;
446 uint8_t proto;
447 int ret;
448
449 bfd->when &= ~BSC_FD_WRITE;
450
451 /* get the next msg for this timeslot */
452 msg = e1inp_tx_ts(e1i_ts, &sign_link);
453 if (!msg) {
454 /* no message after tx delay timer */
455 return 0;
456 }
457
458 switch (sign_link->type) {
459 case E1INP_SIGN_OML:
460 proto = IPAC_PROTO_OML;
461 break;
462 case E1INP_SIGN_RSL:
463 proto = IPAC_PROTO_RSL;
464 break;
465 default:
466 msgb_free(msg);
467 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
468 return -EINVAL;
469 }
470
471 msg->l2h = msg->data;
Pablo Neira Ayusob9ed7e32011-07-05 18:31:59 +0200472 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;
777
778 /* XXX better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200779 if (!link->line->ops->sign_link) {
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200780 LOGP(DINP, LOGL_ERROR, "Fix your application, "
781 "no action set for signalling messages.\n");
782 return -ENOENT;
783 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200784 link->line->ops->sign_link(msg);
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200785 return 0;
786}
787
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200788static int ipaccess_line_update(struct e1inp_line *line,
789 enum e1inp_line_role role, const char *addr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200790{
791 int ret = -ENOENT;
792
793 switch(role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200794 case E1INP_LINE_R_BSC: {
795 struct ipa_server_link *oml_link, *rsl_link;
796
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200797 LOGP(DINP, LOGL_NOTICE, "enabling ipaccess BSC mode\n");
798
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200799 oml_link = ipa_server_link_create(tall_ipa_ctx, line,
800 "0.0.0.0", IPA_TCP_PORT_OML,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200801 ipaccess_bsc_oml_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200802 if (oml_link == NULL) {
803 LOGP(DINP, LOGL_ERROR, "cannot create OML "
804 "BSC link: %s\n", strerror(errno));
805 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200806 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200807 if (ipa_server_link_open(oml_link) < 0) {
808 LOGP(DINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
809 strerror(errno));
810 ipa_server_link_close(oml_link);
811 ipa_server_link_destroy(oml_link);
812 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200813 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200814 rsl_link = ipa_server_link_create(tall_ipa_ctx, line,
815 "0.0.0.0", IPA_TCP_PORT_RSL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200816 ipaccess_bsc_rsl_cb, NULL);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200817 if (rsl_link == NULL) {
818 LOGP(DINP, LOGL_ERROR, "cannot create RSL "
819 "BSC link: %s\n", strerror(errno));
820 return -ENOMEM;
821 }
822 if (ipa_server_link_open(rsl_link) < 0) {
823 LOGP(DINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
824 strerror(errno));
825 ipa_server_link_close(rsl_link);
826 ipa_server_link_destroy(rsl_link);
827 return -EIO;
828 }
829 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200830 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200831 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200832 case E1INP_LINE_R_BTS: {
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200833 struct ipa_client_link *link, *rsl_link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200834
835 LOGP(DINP, LOGL_NOTICE, "enabling ipaccess BTS mode\n");
836
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200837 link = ipa_client_link_create(tall_ipa_ctx,
838 &line->ts[E1INP_SIGN_OML-1],
839 "ipa", E1INP_SIGN_OML,
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200840 addr, IPA_TCP_PORT_OML,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200841 ipaccess_bts_cb,
842 ipaccess_bts_write_cb,
843 NULL);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200844 if (link == NULL) {
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200845 LOGP(DINP, LOGL_ERROR, "cannot create OML "
846 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200847 return -ENOMEM;
848 }
849 if (ipa_client_link_open(link) < 0) {
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200850 LOGP(DINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
851 strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200852 ipa_client_link_close(link);
853 ipa_client_link_destroy(link);
854 return -EIO;
855 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200856 rsl_link = ipa_client_link_create(tall_ipa_ctx,
857 &line->ts[E1INP_SIGN_RSL-1],
858 "ipa", E1INP_SIGN_RSL,
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200859 addr, IPA_TCP_PORT_RSL,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200860 ipaccess_bts_cb,
861 ipaccess_bts_write_cb,
862 NULL);
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200863 if (rsl_link == NULL) {
864 LOGP(DINP, LOGL_ERROR, "cannot create RSL "
865 "BTS link: %s\n", strerror(errno));
866 return -ENOMEM;
867 }
868 if (ipa_client_link_open(rsl_link) < 0) {
869 LOGP(DINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
870 strerror(errno));
871 ipa_client_link_close(rsl_link);
872 ipa_client_link_destroy(rsl_link);
873 return -EIO;
874 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200875 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200876 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200877 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200878 default:
879 break;
880 }
881 return ret;
882}
883
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200884void e1inp_ipaccess_init(void)
885{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200886 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200887 e1inp_driver_register(&ipaccess_driver);
888}