blob: 78b25334e145e9327c882c29a7620c5d3c0f00f1 [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 Ayuso0ba77d52011-06-05 18:32:44 +020046
47#define PRIV_OML 1
48#define PRIV_RSL 2
49
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +020050static void *tall_ipa_ctx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020051
52/* data structure for one E1 interface with A-bis */
53struct ia_e1_handle {
54 struct osmo_fd listen_fd;
55 struct osmo_fd rsl_listen_fd;
56 struct gsm_network *gsmnet;
57};
58
59static struct ia_e1_handle *e1h;
60
61
62#define TS1_ALLOC_SIZE 900
63
64/*
65 * Common propietary IPA messages:
66 * - PONG: in reply to PING.
67 * - ID_REQUEST: first messages once OML has been established.
68 * - ID_ACK: in reply to ID_ACK.
69 */
70const uint8_t ipa_pong_msg[] = {
71 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG
72};
73
74const uint8_t ipa_id_ack_msg[] = {
75 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK
76};
77
78const uint8_t ipa_id_req_msg[] = {
79 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET,
80 0x01, IPAC_IDTAG_UNIT,
81 0x01, IPAC_IDTAG_MACADDR,
82 0x01, IPAC_IDTAG_LOCATION1,
83 0x01, IPAC_IDTAG_LOCATION2,
84 0x01, IPAC_IDTAG_EQUIPVERS,
85 0x01, IPAC_IDTAG_SWVERSION,
86 0x01, IPAC_IDTAG_UNITNAME,
87 0x01, IPAC_IDTAG_SERNR,
88};
89
90static int ipaccess_send(int fd, const void *msg, size_t msglen)
91{
92 int ret;
93
94 ret = write(fd, msg, msglen);
95 if (ret < 0)
96 return ret;
97 if (ret < msglen) {
98 LOGP(DINP, LOGL_ERROR, "ipaccess_send: short write\n");
99 return -EIO;
100 }
101 return ret;
102}
103
104int ipaccess_send_pong(int fd)
105{
106 return ipaccess_send(fd, ipa_pong_msg, sizeof(ipa_pong_msg));
107}
108
109int ipaccess_send_id_ack(int fd)
110{
111 return ipaccess_send(fd, ipa_id_ack_msg, sizeof(ipa_id_ack_msg));
112}
113
114int ipaccess_send_id_req(int fd)
115{
116 return ipaccess_send(fd, ipa_id_req_msg, sizeof(ipa_id_req_msg));
117}
118
119/*
120 * read one ipa message from the socket
121 * return NULL in case of error
122 */
123struct msgb *ipaccess_read_msg(struct osmo_fd *bfd, int *error)
124{
125 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "Abis/IP");
126 struct ipaccess_head *hh;
127 int len, ret = 0;
128
129 if (!msg) {
130 *error = -ENOMEM;
131 return NULL;
132 }
133
134 /* first read our 3-byte header */
135 hh = (struct ipaccess_head *) msg->data;
136 ret = recv(bfd->fd, msg->data, sizeof(*hh), 0);
137 if (ret == 0) {
138 msgb_free(msg);
139 *error = ret;
140 return NULL;
141 } else if (ret != sizeof(*hh)) {
142 if (errno != EAGAIN)
143 LOGP(DINP, LOGL_ERROR, "recv error %d %s\n", ret, strerror(errno));
144 msgb_free(msg);
145 *error = ret;
146 return NULL;
147 }
148
149 msgb_put(msg, ret);
150
151 /* then read te length as specified in header */
152 msg->l2h = msg->data + sizeof(*hh);
153 len = ntohs(hh->len);
154
155 if (len < 0 || TS1_ALLOC_SIZE < len + sizeof(*hh)) {
156 LOGP(DINP, LOGL_ERROR, "Can not read this packet. %d avail\n", len);
157 msgb_free(msg);
158 *error = -EIO;
159 return NULL;
160 }
161
162 ret = recv(bfd->fd, msg->l2h, len, 0);
163 if (ret < len) {
164 LOGP(DINP, LOGL_ERROR, "short read! Got %d from %d\n", ret, len);
165 msgb_free(msg);
166 *error = -EIO;
167 return NULL;
168 }
169 msgb_put(msg, ret);
170
171 return msg;
172}
173
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200174/* base handling of the ip.access protocol */
175int ipaccess_rcvmsg_base(struct msgb *msg,
176 struct osmo_fd *bfd)
177{
178 uint8_t msg_type = *(msg->l2h);
179 int ret = 0;
180
181 switch (msg_type) {
182 case IPAC_MSGT_PING:
183 ret = ipaccess_send_pong(bfd->fd);
184 break;
185 case IPAC_MSGT_PONG:
186 DEBUGP(DMI, "PONG!\n");
187 break;
188 case IPAC_MSGT_ID_ACK:
189 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
190 ret = ipaccess_send_id_ack(bfd->fd);
191 break;
192 }
193 return 0;
194}
195
196static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
197 struct osmo_fd *bfd)
198{
199 uint8_t msg_type = *(msg->l2h);
200
201 /* handle base messages */
202 ipaccess_rcvmsg_base(msg, bfd);
203
204 switch (msg_type) {
205 case IPAC_MSGT_ID_RESP:
206 DEBUGP(DMI, "ID_RESP\n");
207 if (!line->ops.sign_link_up) {
208 LOGP(DINP, LOGL_ERROR, "Fix your application, "
209 "no action set if the signalling link "
210 "becomes ready\n");
211 return -EINVAL;
212 }
213 line->ops.sign_link_up(msg, line);
214 break;
215 }
216 return 0;
217}
218
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200219static int handle_ts1_read(struct osmo_fd *bfd)
220{
221 struct e1inp_line *line = bfd->data;
222 unsigned int ts_nr = bfd->priv_nr;
223 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200224 struct e1inp_sign_link *link;
225 struct ipaccess_head *hh;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200226 struct msgb *msg;
227 int ret = 0, error;
228
229 msg = ipaccess_read_msg(bfd, &error);
230 if (!msg) {
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200231 if (e1i_ts->line->ops.error)
232 e1i_ts->line->ops.error(NULL, error);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200233 if (error == 0) {
234 osmo_fd_unregister(bfd);
235 close(bfd->fd);
236 bfd->fd = -1;
237 }
238 return error;
239 }
240 DEBUGP(DMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
241
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200242 hh = (struct ipaccess_head *) msg->data;
243 if (hh->proto == IPAC_PROTO_IPACCESS) {
244 ipaccess_rcvmsg(line, msg, bfd);
245 msgb_free(msg);
246 return ret;
247 }
248 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
249 * might have free'd it !!! */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200250
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200251 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
252 if (!link) {
253 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
254 "hh->proto=0x%02x\n", hh->proto);
255 msgb_free(msg);
256 return -EIO;
257 }
258 msg->dst = link;
259
260 /* XXX better use e1inp_ts_rx? */
261 if (!e1i_ts->line->ops.sign_link) {
262 LOGP(DINP, LOGL_ERROR, "Fix your application, "
263 "no action set for signalling messages.\n");
264 return -ENOENT;
265 }
266 e1i_ts->line->ops.sign_link(msg, link);
267
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200268 return ret;
269}
270
271void ipaccess_prepend_header_ext(struct msgb *msg, int proto)
272{
273 struct ipaccess_head_ext *hh_ext;
274
275 /* prepend the osmo ip.access header extension */
276 hh_ext = (struct ipaccess_head_ext *) msgb_push(msg, sizeof(*hh_ext));
277 hh_ext->proto = proto;
278}
279
280void ipaccess_prepend_header(struct msgb *msg, int proto)
281{
282 struct ipaccess_head *hh;
283
284 /* prepend the ip.access header */
285 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
286 hh->len = htons(msg->len - sizeof(*hh));
287 hh->proto = proto;
288}
289
290static int ts_want_write(struct e1inp_ts *e1i_ts)
291{
292 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
293
294 return 0;
295}
296
297static void timeout_ts1_write(void *data)
298{
299 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
300
301 /* trigger write of ts1, due to tx delay timer */
302 ts_want_write(e1i_ts);
303}
304
305static int handle_ts1_write(struct osmo_fd *bfd)
306{
307 struct e1inp_line *line = bfd->data;
308 unsigned int ts_nr = bfd->priv_nr;
309 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
310 struct e1inp_sign_link *sign_link;
311 struct msgb *msg;
312 uint8_t proto;
313 int ret;
314
315 bfd->when &= ~BSC_FD_WRITE;
316
317 /* get the next msg for this timeslot */
318 msg = e1inp_tx_ts(e1i_ts, &sign_link);
319 if (!msg) {
320 /* no message after tx delay timer */
321 return 0;
322 }
323
324 switch (sign_link->type) {
325 case E1INP_SIGN_OML:
326 proto = IPAC_PROTO_OML;
327 break;
328 case E1INP_SIGN_RSL:
329 proto = IPAC_PROTO_RSL;
330 break;
331 default:
332 msgb_free(msg);
333 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
334 return -EINVAL;
335 }
336
337 msg->l2h = msg->data;
338 ipaccess_prepend_header(msg, sign_link->tei);
339
340 DEBUGP(DMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
341
342 ret = send(bfd->fd, msg->data, msg->len, 0);
343 msgb_free(msg);
344
345 /* set tx delay timer for next event */
346 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
347 e1i_ts->sign.tx_timer.data = e1i_ts;
348
349 /* Reducing this might break the nanoBTS 900 init. */
350 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
351
352 return ret;
353}
354
355/* callback from select.c in case one of the fd's can be read/written */
356static int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
357{
358 struct e1inp_line *line = bfd->data;
359 unsigned int ts_nr = bfd->priv_nr;
360 unsigned int idx = ts_nr-1;
361 struct e1inp_ts *e1i_ts;
362 int rc = 0;
363
364 /* In case of early RSL we might not yet have a line */
365
366 if (line)
367 e1i_ts = &line->ts[idx];
368
369 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
370 if (what & BSC_FD_READ)
371 rc = handle_ts1_read(bfd);
372 if (what & BSC_FD_WRITE)
373 rc = handle_ts1_write(bfd);
374 } else
375 LOGP(DINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type);
376
377 return rc;
378}
379
380static int
381ipaccess_line_update(struct e1inp_line *line, enum e1inp_line_role role);
382
383struct e1inp_driver ipaccess_driver = {
384 .name = "ipa",
385 .want_write = ts_want_write,
386 .line_update = ipaccess_line_update,
387 .default_delay = 0,
388};
389
390/* callback of the OML listening filedescriptor */
391static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
392{
393 int ret;
394 int idx = 0;
395 int i;
396 struct e1inp_line *line = listen_bfd->data;
397 struct e1inp_ts *e1i_ts;
398 struct osmo_fd *bfd;
399 struct sockaddr_in sa;
400 socklen_t sa_len = sizeof(sa);
401
402 if (!(what & BSC_FD_READ))
403 return 0;
404
405 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
406 if (ret < 0) {
407 perror("accept");
408 return ret;
409 }
410 LOGP(DINP, LOGL_NOTICE, "accept()ed new OML link from %s\n",
411 inet_ntoa(sa.sin_addr));
412
413 /* create virrtual E1 timeslots for signalling */
414 e1inp_ts_config_sign(&line->ts[1-1], line);
415
416 /* initialize the fds */
417 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
418 line->ts[i].driver.ipaccess.fd.fd = -1;
419
420 e1i_ts = &line->ts[idx];
421
422 bfd = &e1i_ts->driver.ipaccess.fd;
423 bfd->fd = ret;
424 bfd->data = line;
425 bfd->priv_nr = PRIV_OML;
426 bfd->cb = ipaccess_fd_cb;
427 bfd->when = BSC_FD_READ;
428 ret = osmo_fd_register(bfd);
429 if (ret < 0) {
430 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
431 close(bfd->fd);
432 return ret;
433 }
434
435 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
436 ret = ipaccess_send_id_req(bfd->fd);
437
438 return ret;
439}
440
441static int rsl_listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
442{
443 struct sockaddr_in sa;
444 socklen_t sa_len = sizeof(sa);
445 struct osmo_fd *bfd;
446 int ret;
447
448 if (!(what & BSC_FD_READ))
449 return 0;
450
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200451 bfd = talloc_zero(tall_ipa_ctx, struct osmo_fd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200452 if (!bfd)
453 return -ENOMEM;
454
455 /* Some BTS has connected to us, but we don't know yet which line
456 * (as created by the OML link) to associate it with. Thus, we
457 * allocate a temporary bfd until we have received ID from BTS */
458
459 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
460 if (bfd->fd < 0) {
461 perror("accept");
462 return bfd->fd;
463 }
464 LOGP(DINP, LOGL_NOTICE, "accept()ed new RSL link from %s\n", inet_ntoa(sa.sin_addr));
465 bfd->priv_nr = PRIV_RSL;
466 bfd->cb = ipaccess_fd_cb;
467 bfd->when = BSC_FD_READ;
468 ret = osmo_fd_register(bfd);
469 if (ret < 0) {
470 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
471 close(bfd->fd);
472 talloc_free(bfd);
473 return ret;
474 }
475 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
476 ret = ipaccess_send_id_req(bfd->fd);
477
478 return 0;
479}
480
481static int
482ipaccess_line_update(struct e1inp_line *line, enum e1inp_line_role role)
483{
484 int ret = -ENOENT;
485
486 switch(role) {
487 case E1INP_LINE_R_BSC:
488 /* Listen for OML connections */
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200489 ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
490 "0.0.0.0", IPA_TCP_PORT_OML, 1);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200491 if (ret < 0)
492 return ret;
493
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200494 e1h->listen_fd.fd = ret;
495 e1h->listen_fd.when |= BSC_FD_READ;
496 e1h->listen_fd.cb = listen_fd_cb;
497 e1h->listen_fd.data = line;
498
499 if (osmo_fd_register(&e1h->listen_fd) < 0) {
500 close(ret);
501 return ret;
502 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200503 /* Listen for RSL connections */
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200504 ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
505 "0.0.0.0", IPA_TCP_PORT_RSL, 1);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200506 if (ret < 0)
507 return ret;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200508
509 e1h->rsl_listen_fd.fd = ret;
510 e1h->rsl_listen_fd.when |= BSC_FD_READ;
511 e1h->rsl_listen_fd.cb = rsl_listen_fd_cb;
512 e1h->rsl_listen_fd.data = NULL;
513
514 if (osmo_fd_register(&e1h->rsl_listen_fd) < 0) {
515 close(ret);
516 return ret;
517 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200518 break;
519 case E1INP_LINE_R_BTS:
520 /* XXX: no implemented yet. */
521 break;
522 default:
523 break;
524 }
525 return ret;
526}
527
528/* Actively connect to a BTS. Currently used by ipaccess-config.c */
529int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa)
530{
531 struct e1inp_ts *e1i_ts = &line->ts[0];
532 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
533 int ret, on = 1;
534
535 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
536 bfd->cb = ipaccess_fd_cb;
537 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
538 bfd->data = line;
539 bfd->priv_nr = PRIV_OML;
540
541 if (bfd->fd < 0) {
542 LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n");
543 return -EIO;
544 }
545
546 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
547
548 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
549 if (ret < 0) {
550 LOGP(DINP, LOGL_ERROR, "could not connect socket\n");
551 close(bfd->fd);
552 return ret;
553 }
554
555 ret = osmo_fd_register(bfd);
556 if (ret < 0) {
557 close(bfd->fd);
558 return ret;
559 }
560
561 line->driver = &ipaccess_driver;
562
563 return ret;
564 //return e1inp_line_register(line);
565}
566
567int ipaccess_setup(struct gsm_network *gsmnet)
568{
569 e1h->gsmnet = gsmnet;
570 return 0;
571}
572
573void e1inp_ipaccess_init(void)
574{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200575 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
576
577 e1h = talloc_zero(tall_ipa_ctx, struct ia_e1_handle);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200578 if (!e1h)
579 return;
580
581 e1inp_driver_register(&ipaccess_driver);
582}