blob: 3b6644eff0188b7c0661c306f8fa45655c4fe38d [file] [log] [blame]
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001/* OpenBSC Abis interface to E1 */
2
3/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include <stdio.h>
23#include <unistd.h>
24#include <stdlib.h>
25#include <errno.h>
26#include <string.h>
27#include <time.h>
28#include <sys/fcntl.h>
29#include <sys/types.h>
30#include <sys/socket.h>
31#include <sys/ioctl.h>
32#include <arpa/inet.h>
33#include <mISDNif.h>
34
35//#define AF_COMPATIBILITY_FUNC
36//#include <compat_af_isdn.h>
37#ifndef AF_ISDN
38#define AF_ISDN 34
39#define PF_ISDN AF_ISDN
40#endif
41
42#include <osmocore/select.h>
43#include <osmocore/msgb.h>
44#include <openbsc/debug.h>
45#include <openbsc/gsm_data.h>
46#include <openbsc/e1_input.h>
47#include <openbsc/abis_nm.h>
48#include <openbsc/abis_rsl.h>
49#include <osmocore/linuxlist.h>
50#include <openbsc/subchan_demux.h>
51#include <openbsc/trau_frame.h>
52#include <openbsc/trau_mux.h>
53#include <osmocore/talloc.h>
54#include <openbsc/signal.h>
55#include <openbsc/misdn.h>
56
57#include "../../bscconfig.h"
58
59#define NUM_E1_TS 32
60
61/* list of all E1 drivers */
62LLIST_HEAD(e1inp_driver_list);
63
64/* list of all E1 lines */
65LLIST_HEAD(e1inp_line_list);
66
67static void *tall_sigl_ctx;
68
69/*
70 * pcap writing of the misdn load
71 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
72 */
73#define DLT_LINUX_LAPD 177
74#define PCAP_INPUT 0
75#define PCAP_OUTPUT 1
76
77struct pcap_hdr {
78 u_int32_t magic_number;
79 u_int16_t version_major;
80 u_int16_t version_minor;
81 int32_t thiszone;
82 u_int32_t sigfigs;
83 u_int32_t snaplen;
84 u_int32_t network;
85} __attribute__((packed));
86
87struct pcaprec_hdr {
88 u_int32_t ts_sec;
89 u_int32_t ts_usec;
90 u_int32_t incl_len;
91 u_int32_t orig_len;
92} __attribute__((packed));
93
94struct fake_linux_lapd_header {
95 u_int16_t pkttype;
96 u_int16_t hatype;
97 u_int16_t halen;
98 u_int64_t addr;
99 int16_t protocol;
100} __attribute__((packed));
101
102struct lapd_header {
103 u_int8_t ea1 : 1;
104 u_int8_t cr : 1;
105 u_int8_t sapi : 6;
106 u_int8_t ea2 : 1;
107 u_int8_t tei : 7;
108 u_int8_t control_foo; /* fake UM's ... */
109} __attribute__((packed));
110
111static_assert(offsetof(struct fake_linux_lapd_header, hatype) == 2, hatype_offset);
112static_assert(offsetof(struct fake_linux_lapd_header, halen) == 4, halen_offset);
113static_assert(offsetof(struct fake_linux_lapd_header, addr) == 6, addr_offset);
114static_assert(offsetof(struct fake_linux_lapd_header, protocol) == 14, proto_offset);
115static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
116
117
118static int pcap_fd = -1;
119
120void e1_set_pcap_fd(int fd)
121{
122 int ret;
123 struct pcap_hdr header = {
124 .magic_number = 0xa1b2c3d4,
125 .version_major = 2,
126 .version_minor = 4,
127 .thiszone = 0,
128 .sigfigs = 0,
129 .snaplen = 65535,
130 .network = DLT_LINUX_LAPD,
131 };
132
133 pcap_fd = fd;
134 ret = write(pcap_fd, &header, sizeof(header));
135}
136
137/* This currently only works for the D-Channel */
138static void write_pcap_packet(int direction, int sapi, int tei,
139 struct msgb *msg) {
140 if (pcap_fd < 0)
141 return;
142
143 int ret;
144 time_t cur_time;
145 struct tm *tm;
146
147 struct fake_linux_lapd_header header = {
148 .pkttype = 4,
149 .hatype = 0,
150 .halen = 0,
151 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
152 .protocol = ntohs(48),
153 };
154
155 struct lapd_header lapd_header = {
156 .ea1 = 0,
157 .cr = direction == PCAP_OUTPUT ? 1 : 0,
158 .sapi = sapi & 0x3F,
159 .ea2 = 1,
160 .tei = tei & 0x7F,
161 .control_foo = 0x03 /* UI */,
162 };
163
164 struct pcaprec_hdr payload_header = {
165 .ts_sec = 0,
166 .ts_usec = 0,
167 .incl_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
168 + sizeof(struct lapd_header),
169 .orig_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
170 + sizeof(struct lapd_header),
171 };
172
173
174 cur_time = time(NULL);
175 tm = localtime(&cur_time);
176 payload_header.ts_sec = mktime(tm);
177
178 ret = write(pcap_fd, &payload_header, sizeof(payload_header));
179 ret = write(pcap_fd, &header, sizeof(header));
180 ret = write(pcap_fd, &lapd_header, sizeof(lapd_header));
181 ret = write(pcap_fd, msg->l2h, msgb_l2len(msg));
182}
183
184static const char *sign_types[] = {
185 [E1INP_SIGN_NONE] = "None",
186 [E1INP_SIGN_OML] = "OML",
187 [E1INP_SIGN_RSL] = "RSL",
188};
189const char *e1inp_signtype_name(enum e1inp_sign_type tp)
190{
191 if (tp >= ARRAY_SIZE(sign_types))
192 return "undefined";
193 return sign_types[tp];
194}
195
196static const char *ts_types[] = {
197 [E1INP_TS_TYPE_NONE] = "None",
198 [E1INP_TS_TYPE_SIGN] = "Signalling",
199 [E1INP_TS_TYPE_TRAU] = "TRAU",
200};
201
202const char *e1inp_tstype_name(enum e1inp_ts_type tp)
203{
204 if (tp >= ARRAY_SIZE(ts_types))
205 return "undefined";
206 return ts_types[tp];
207}
208
209/* callback when a TRAU frame was received */
210static int subch_cb(struct subch_demux *dmx, int ch, u_int8_t *data, int len,
211 void *_priv)
212{
213 struct e1inp_ts *e1i_ts = _priv;
214 struct gsm_e1_subslot src_ss;
215
216 src_ss.e1_nr = e1i_ts->line->num;
217 src_ss.e1_ts = e1i_ts->num;
218 src_ss.e1_ts_ss = ch;
219
220 return trau_mux_input(&src_ss, data, len);
221}
222
223int abis_rsl_sendmsg(struct msgb *msg)
224{
225 struct e1inp_sign_link *sign_link;
226 struct e1inp_driver *e1inp_driver;
227 struct e1inp_ts *e1i_ts;
228
229 msg->l2h = msg->data;
230
231 if (!msg->trx) {
232 LOGP(DRSL, LOGL_ERROR, "rsl_sendmsg: msg->trx == NULL: %s\n",
233 hexdump(msg->data, msg->len));
234 talloc_free(msg);
235 return -EINVAL;
236 } else if (!msg->trx->rsl_link) {
237 LOGP(DRSL, LOGL_ERROR, "rsl_sendmsg: msg->trx->rsl_link == NULL: %s\n",
238 hexdump(msg->data, msg->len));
239 talloc_free(msg);
240 return -EIO;
241 }
242
243 sign_link = msg->trx->rsl_link;
244 e1i_ts = sign_link->ts;
245 if (!bsc_timer_pending(&e1i_ts->sign.tx_timer)) {
246 /* notify the driver we have something to write */
247 e1inp_driver = sign_link->ts->line->driver;
248 e1inp_driver->want_write(e1i_ts);
249 }
250 msgb_enqueue(&sign_link->tx_list, msg);
251
252 /* dump it */
253 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
254
255 return 0;
256}
257
258int _abis_nm_sendmsg(struct msgb *msg, int to_trx_oml)
259{
260 struct e1inp_sign_link *sign_link;
261 struct e1inp_driver *e1inp_driver;
262 struct e1inp_ts *e1i_ts;
263
264 msg->l2h = msg->data;
265
266 if (!msg->trx || !msg->trx->bts || !msg->trx->bts->oml_link) {
267 LOGP(DNM, LOGL_ERROR, "nm_sendmsg: msg->trx == NULL\n");
268 return -EINVAL;
269 }
270
271 /* Check for TRX-specific OML link first */
272 if (to_trx_oml) {
273 if (!msg->trx->oml_link)
274 return -ENODEV;
275 sign_link = msg->trx->oml_link;
276 } else
277 sign_link = msg->trx->bts->oml_link;
278
279 e1i_ts = sign_link->ts;
280 if (!bsc_timer_pending(&e1i_ts->sign.tx_timer)) {
281 /* notify the driver we have something to write */
282 e1inp_driver = sign_link->ts->line->driver;
283 e1inp_driver->want_write(e1i_ts);
284 }
285 msgb_enqueue(&sign_link->tx_list, msg);
286
287 /* dump it */
288 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
289
290 return 0;
291}
292
293/* Timeslot */
294
295/* configure and initialize one e1inp_ts */
296int e1inp_ts_config(struct e1inp_ts *ts, struct e1inp_line *line,
297 enum e1inp_ts_type type)
298{
299 if (ts->type == type && ts->line && line)
300 return 0;
301
302 ts->type = type;
303 ts->line = line;
304
305 switch (type) {
306 case E1INP_TS_TYPE_SIGN:
307 if (line && line->driver)
308 ts->sign.delay = line->driver->default_delay;
309 else
310 ts->sign.delay = 100000;
311 INIT_LLIST_HEAD(&ts->sign.sign_links);
312 break;
313 case E1INP_TS_TYPE_TRAU:
314 subchan_mux_init(&ts->trau.mux);
315 ts->trau.demux.out_cb = subch_cb;
316 ts->trau.demux.data = ts;
317 subch_demux_init(&ts->trau.demux);
318 break;
319 default:
320 LOGP(DMI, LOGL_ERROR, "unsupported E1 timeslot type %u\n",
321 ts->type);
322 return -EINVAL;
323 }
324 return 0;
325}
326
327struct e1inp_line *e1inp_line_get(u_int8_t e1_nr)
328{
329 struct e1inp_line *e1i_line;
330
331 /* iterate over global list of e1 lines */
332 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
333 if (e1i_line->num == e1_nr)
334 return e1i_line;
335 }
336 return NULL;
337}
338
339struct e1inp_line *e1inp_line_create(u_int8_t e1_nr, const char *driver_name)
340{
341 struct e1inp_driver *driver;
342 struct e1inp_line *line;
343 int i;
344
345 line = e1inp_line_get(e1_nr);
346 if (line) {
347 LOGP(DINP, LOGL_ERROR, "E1 Line %u already exists\n",
348 e1_nr);
349 return NULL;
350 }
351
352 driver = e1inp_driver_find(driver_name);
353 if (!driver) {
354 LOGP(DINP, LOGL_ERROR, "No such E1 driver '%s'\n",
355 driver_name);
356 return NULL;
357 }
358
359 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
360 if (!line)
361 return NULL;
362
363 line->driver = driver;
364
365 line->num = e1_nr;
366 for (i = 0; i < NUM_E1_TS; i++) {
367 line->ts[i].num = i+1;
368 line->ts[i].line = line;
369 }
370 llist_add_tail(&line->list, &e1inp_line_list);
371
372 return line;
373}
374
375#if 0
376struct e1inp_line *e1inp_line_get_create(u_int8_t e1_nr)
377{
378 struct e1inp_line *line;
379 int i;
380
381 line = e1inp_line_get(e1_nr);
382 if (line)
383 return line;
384
385 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
386 if (!line)
387 return NULL;
388
389 line->num = e1_nr;
390 for (i = 0; i < NUM_E1_TS; i++) {
391 line->ts[i].num = i+1;
392 line->ts[i].line = line;
393 }
394 llist_add_tail(&line->list, &e1inp_line_list);
395
396 return line;
397}
398#endif
399
400static struct e1inp_ts *e1inp_ts_get(u_int8_t e1_nr, u_int8_t ts_nr)
401{
402 struct e1inp_line *e1i_line;
403
404 e1i_line = e1inp_line_get(e1_nr);
405 if (!e1i_line)
406 return NULL;
407
408 return &e1i_line->ts[ts_nr-1];
409}
410
411struct subch_mux *e1inp_get_mux(u_int8_t e1_nr, u_int8_t ts_nr)
412{
413 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
414
415 if (!e1i_ts)
416 return NULL;
417
418 return &e1i_ts->trau.mux;
419}
420
421/* Signalling Link */
422
423struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
424 u_int8_t tei, u_int8_t sapi)
425{
426 struct e1inp_sign_link *link;
427
428 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
429 if (link->sapi == sapi && link->tei == tei)
430 return link;
431 }
432
433 return NULL;
434}
435
436/* create a new signalling link in a E1 timeslot */
437
438struct e1inp_sign_link *
439e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
440 struct gsm_bts_trx *trx, u_int8_t tei,
441 u_int8_t sapi)
442{
443 struct e1inp_sign_link *link;
444
445 if (ts->type != E1INP_TS_TYPE_SIGN)
446 return NULL;
447
448 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
449 if (!link)
450 return NULL;
451
452 link->ts = ts;
453 link->type = type;
454 INIT_LLIST_HEAD(&link->tx_list);
455 link->trx = trx;
456 link->tei = tei;
457 link->sapi = sapi;
458
459 llist_add_tail(&link->list, &ts->sign.sign_links);
460
461 return link;
462}
463
464void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
465{
466 struct msgb *msg;
467
468 llist_del(&link->list);
469 while (!llist_empty(&link->tx_list)) {
470 msg = msgb_dequeue(&link->tx_list);
471 msgb_free(msg);
472 }
473
474 if (link->ts->type == E1INP_TS_TYPE_SIGN)
475 bsc_del_timer(&link->ts->sign.tx_timer);
476
477 talloc_free(link);
478}
479
480/* the E1 driver tells us he has received something on a TS */
481int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
482 u_int8_t tei, u_int8_t sapi)
483{
484 struct e1inp_sign_link *link;
485 struct gsm_bts *bts;
486 int ret;
487
488 switch (ts->type) {
489 case E1INP_TS_TYPE_SIGN:
490 /* consult the list of signalling links */
491 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
492 link = e1inp_lookup_sign_link(ts, tei, sapi);
493 if (!link) {
494 LOGP(DMI, LOGL_ERROR, "didn't find signalling link for "
495 "tei %d, sapi %d\n", tei, sapi);
496 return -EINVAL;
497 }
498
499 log_set_context(BSC_CTX_BTS, link->trx->bts);
500 switch (link->type) {
501 case E1INP_SIGN_OML:
502 msg->trx = link->trx;
503 bts = msg->trx->bts;
504 ret = bts->model->oml_rcvmsg(msg);
505 break;
506 case E1INP_SIGN_RSL:
507 msg->trx = link->trx;
508 ret = abis_rsl_rcvmsg(msg);
509 break;
510 default:
511 ret = -EINVAL;
512 LOGP(DMI, LOGL_ERROR, "unknown link type %u\n", link->type);
513 break;
514 }
515 break;
516 case E1INP_TS_TYPE_TRAU:
517 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
518 break;
519 default:
520 ret = -EINVAL;
521 LOGP(DMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
522 break;
523 }
524
525 return ret;
526}
527
528#define TSX_ALLOC_SIZE 4096
529
530/* called by driver if it wants to transmit on a given TS */
531struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
532 struct e1inp_sign_link **sign_link)
533{
534 struct e1inp_sign_link *link;
535 struct msgb *msg = NULL;
536 int len;
537
538 switch (e1i_ts->type) {
539 case E1INP_TS_TYPE_SIGN:
540 /* FIXME: implement this round robin */
541 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
542 msg = msgb_dequeue(&link->tx_list);
543 if (msg) {
544 if (sign_link)
545 *sign_link = link;
546 break;
547 }
548 }
549 break;
550 case E1INP_TS_TYPE_TRAU:
551 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
552 if (!msg)
553 return NULL;
554 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
555 msgb_put(msg, 40);
556 break;
557 default:
558 LOGP(DMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
559 return NULL;
560 }
561 return msg;
562}
563
564/* called by driver in case some kind of link state event */
565int e1inp_event(struct e1inp_ts *ts, int evt, u_int8_t tei, u_int8_t sapi)
566{
567 struct e1inp_sign_link *link;
568 struct input_signal_data isd;
569
570 link = e1inp_lookup_sign_link(ts, tei, sapi);
571 if (!link)
572 return -EINVAL;
573
574 isd.link_type = link->type;
575 isd.trx = link->trx;
576 isd.tei = tei;
577 isd.sapi = sapi;
578
579 /* report further upwards */
580 dispatch_signal(SS_INPUT, evt, &isd);
581 return 0;
582}
583
584/* register a driver with the E1 core */
585int e1inp_driver_register(struct e1inp_driver *drv)
586{
587 llist_add_tail(&drv->list, &e1inp_driver_list);
588 return 0;
589}
590
591struct e1inp_driver *e1inp_driver_find(const char *name)
592{
593 struct e1inp_driver *drv;
594
595 llist_for_each_entry(drv, &e1inp_driver_list, list) {
596 if (!strcasecmp(name, drv->name))
597 return drv;
598 }
599 return NULL;
600}
601
602int e1inp_line_update(struct e1inp_line *line)
603{
604 struct input_signal_data isd;
605 int rc;
606
607 if (line->driver && line->driver->line_update)
608 rc = line->driver->line_update(line);
609 else
610 rc = 0;
611
612 /* Send a signal to anyone who is interested in new lines being
613 * configured */
614 memset(&isd, 0, sizeof(isd));
615 isd.line = line;
616 dispatch_signal(SS_INPUT, S_INP_LINE_INIT, &isd);
617
618 return rc;
619}
620
621static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
622 void *handler_data, void *signal_data)
623{
624 if (subsys != SS_GLOBAL ||
625 signal != S_GLOBAL_SHUTDOWN)
626 return 0;
627
628 if (pcap_fd) {
629 close(pcap_fd);
630 pcap_fd = -1;
631 }
632
633 return 0;
634}
635
636void e1inp_misdn_init(void);
637void e1inp_dahdi_init(void);
638
639void e1inp_init(void)
640{
641 tall_sigl_ctx = talloc_named_const(tall_bsc_ctx, 1,
642 "e1inp_sign_link");
643 register_signal_handler(SS_GLOBAL, e1i_sig_cb, NULL);
644
645 e1inp_misdn_init();
646#ifdef HAVE_DAHDI_USER_H
647 e1inp_dahdi_init();
648#endif
649}