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