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