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