blob: c83dc0431b21a09d2f2a175918ab88808411c307 [file] [log] [blame]
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001/* 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 "internal.h"
23
24#include <stdio.h>
25#include <unistd.h>
26#include <stdlib.h>
27#include <errno.h>
28#include <string.h>
29#include <time.h>
30#include <sys/fcntl.h>
31#include <sys/socket.h>
32#include <sys/ioctl.h>
33#include <arpa/inet.h>
34#include <mISDNif.h>
35
36//#define AF_COMPATIBILITY_FUNC
37//#include <compat_af_isdn.h>
38#ifndef AF_ISDN
39#define AF_ISDN 34
40#define PF_ISDN AF_ISDN
41#endif
42
43#include <osmocom/core/select.h>
44#include <osmocom/core/msgb.h>
45#include <osmocom/core/logging.h>
46#include <osmocom/core/signal.h>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020047#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020048#include <osmocom/core/linuxlist.h>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020049#include <osmocom/abis/subchan_demux.h>
Pablo Neira Ayuso0b099b22011-06-09 13:14:11 +020050#include <osmocom/abis/logging.h>
51#include <osmocom/abis/signal.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020052#include <talloc.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020053
54#define NUM_E1_TS 32
55
56static void *tall_e1inp_ctx;
57
58/* list of all E1 drivers */
59LLIST_HEAD(e1inp_driver_list);
60
61/* list of all E1 lines */
62LLIST_HEAD(e1inp_line_list);
63
64static void *tall_sigl_ctx;
65
66/*
67 * pcap writing of the misdn load
68 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
69 */
70#define DLT_LINUX_LAPD 177
71#define PCAP_INPUT 0
72#define PCAP_OUTPUT 1
73
74struct pcap_hdr {
75 uint32_t magic_number;
76 uint16_t version_major;
77 uint16_t version_minor;
78 int32_t thiszone;
79 uint32_t sigfigs;
80 uint32_t snaplen;
81 uint32_t network;
82} __attribute__((packed));
83
84struct pcaprec_hdr {
85 uint32_t ts_sec;
86 uint32_t ts_usec;
87 uint32_t incl_len;
88 uint32_t orig_len;
89} __attribute__((packed));
90
91struct fake_linux_lapd_header {
92 uint16_t pkttype;
93 uint16_t hatype;
94 uint16_t halen;
95 uint64_t addr;
96 int16_t protocol;
97} __attribute__((packed));
98
99struct lapd_header {
100 uint8_t ea1 : 1;
101 uint8_t cr : 1;
102 uint8_t sapi : 6;
103 uint8_t ea2 : 1;
104 uint8_t tei : 7;
105 uint8_t control_foo; /* fake UM's ... */
106} __attribute__((packed));
107
108osmo_static_assert(offsetof(struct fake_linux_lapd_header, hatype) == 2, hatype_offset);
109osmo_static_assert(offsetof(struct fake_linux_lapd_header, halen) == 4, halen_offset);
110osmo_static_assert(offsetof(struct fake_linux_lapd_header, addr) == 6, addr_offset);
111osmo_static_assert(offsetof(struct fake_linux_lapd_header, protocol) == 14, proto_offset);
112osmo_static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
113
114
115static int pcap_fd = -1;
116
117void e1_set_pcap_fd(int fd)
118{
119 int ret;
120 struct pcap_hdr header = {
121 .magic_number = 0xa1b2c3d4,
122 .version_major = 2,
123 .version_minor = 4,
124 .thiszone = 0,
125 .sigfigs = 0,
126 .snaplen = 65535,
127 .network = DLT_LINUX_LAPD,
128 };
129
130 pcap_fd = fd;
131 ret = write(pcap_fd, &header, sizeof(header));
132}
133
134/* This currently only works for the D-Channel */
135static void write_pcap_packet(int direction, int sapi, int tei,
136 struct msgb *msg) {
137 if (pcap_fd < 0)
138 return;
139
140 int ret;
141 time_t cur_time;
142 struct tm *tm;
143
144 struct fake_linux_lapd_header header = {
145 .pkttype = 4,
146 .hatype = 0,
147 .halen = 0,
148 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
149 .protocol = ntohs(48),
150 };
151
152 struct lapd_header lapd_header = {
153 .ea1 = 0,
154 .cr = direction == PCAP_OUTPUT ? 1 : 0,
155 .sapi = sapi & 0x3F,
156 .ea2 = 1,
157 .tei = tei & 0x7F,
158 .control_foo = 0x03 /* UI */,
159 };
160
161 struct pcaprec_hdr payload_header = {
162 .ts_sec = 0,
163 .ts_usec = 0,
164 .incl_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
165 + sizeof(struct lapd_header),
166 .orig_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
167 + sizeof(struct lapd_header),
168 };
169
170
171 cur_time = time(NULL);
172 tm = localtime(&cur_time);
173 payload_header.ts_sec = mktime(tm);
174
175 ret = write(pcap_fd, &payload_header, sizeof(payload_header));
176 ret = write(pcap_fd, &header, sizeof(header));
177 ret = write(pcap_fd, &lapd_header, sizeof(lapd_header));
178 ret = write(pcap_fd, msg->l2h, msgb_l2len(msg));
179}
180
181static const char *sign_types[] = {
182 [E1INP_SIGN_NONE] = "None",
183 [E1INP_SIGN_OML] = "OML",
184 [E1INP_SIGN_RSL] = "RSL",
185};
186const char *e1inp_signtype_name(enum e1inp_sign_type tp)
187{
188 if (tp >= ARRAY_SIZE(sign_types))
189 return "undefined";
190 return sign_types[tp];
191}
192
193static const char *ts_types[] = {
194 [E1INP_TS_TYPE_NONE] = "None",
195 [E1INP_TS_TYPE_SIGN] = "Signalling",
196 [E1INP_TS_TYPE_TRAU] = "TRAU",
197};
198
199const char *e1inp_tstype_name(enum e1inp_ts_type tp)
200{
201 if (tp >= ARRAY_SIZE(ts_types))
202 return "undefined";
203 return ts_types[tp];
204}
205
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200206int abis_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200207{
208 struct e1inp_sign_link *sign_link = msg->dst;
209 struct e1inp_driver *e1inp_driver;
210 struct e1inp_ts *e1i_ts;
211;
212 msg->l2h = msg->data;
213
214 /* don't know how to route this message. */
215 if (sign_link == NULL) {
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200216 LOGP(DRSL, LOGL_ERROR, "abis_sendmsg: msg->dst == NULL: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200217 osmo_hexdump(msg->data, msg->len));
218 talloc_free(msg);
219 return -EINVAL;
220 }
221 e1i_ts = sign_link->ts;
222 if (!osmo_timer_pending(&e1i_ts->sign.tx_timer)) {
223 /* notify the driver we have something to write */
224 e1inp_driver = sign_link->ts->line->driver;
225 e1inp_driver->want_write(e1i_ts);
226 }
227 msgb_enqueue(&sign_link->tx_list, msg);
228
229 /* dump it */
230 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
231
232 return 0;
233}
234
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200235int abis_rsl_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200236{
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200237 return abis_sendmsg(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200238}
239
240/* Timeslot */
241int e1inp_ts_config_trau(struct e1inp_ts *ts, struct e1inp_line *line,
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200242 int (*trau_rcv_cb)(struct subch_demux *dmx, int ch,
243 uint8_t *data, int len, void *_priv))
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200244{
245 if (ts->type == E1INP_TS_TYPE_TRAU && ts->line && line)
246 return 0;
247
248 ts->type = E1INP_TS_TYPE_TRAU;
249 ts->line = line;
250
251 subchan_mux_init(&ts->trau.mux);
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200252 ts->trau.demux.out_cb = trau_rcv_cb;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200253 ts->trau.demux.data = ts;
254 subch_demux_init(&ts->trau.demux);
255 return 0;
256}
257
258int e1inp_ts_config_sign(struct e1inp_ts *ts, struct e1inp_line *line)
259{
260 if (ts->type == E1INP_TS_TYPE_SIGN && ts->line && line)
261 return 0;
262
263 ts->type = E1INP_TS_TYPE_SIGN;
264 ts->line = line;
265
266 if (line && line->driver)
267 ts->sign.delay = line->driver->default_delay;
268 else
269 ts->sign.delay = 100000;
270 INIT_LLIST_HEAD(&ts->sign.sign_links);
271 return 0;
272}
273
274struct e1inp_line *e1inp_line_get(uint8_t e1_nr)
275{
276 struct e1inp_line *e1i_line;
277
278 /* iterate over global list of e1 lines */
279 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
280 if (e1i_line->num == e1_nr)
281 return e1i_line;
282 }
283 return NULL;
284}
285
286struct e1inp_line *
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200287e1inp_line_create(uint8_t e1_nr, const char *driver_name)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200288{
289 struct e1inp_driver *driver;
290 struct e1inp_line *line;
291 int i;
292
293 line = e1inp_line_get(e1_nr);
294 if (line) {
295 LOGP(DINP, LOGL_ERROR, "E1 Line %u already exists\n",
296 e1_nr);
297 return NULL;
298 }
299
300 driver = e1inp_driver_find(driver_name);
301 if (!driver) {
302 LOGP(DINP, LOGL_ERROR, "No such E1 driver '%s'\n",
303 driver_name);
304 return NULL;
305 }
306
307 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
308 if (!line)
309 return NULL;
310
311 line->driver = driver;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200312
313 line->num = e1_nr;
314 for (i = 0; i < NUM_E1_TS; i++) {
315 line->ts[i].num = i+1;
316 line->ts[i].line = line;
317 }
318 llist_add_tail(&line->list, &e1inp_line_list);
319
320 return line;
321}
322
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200323void
324e1inp_line_bind_ops(struct e1inp_line *line, const struct e1inp_line_ops *ops)
325{
326 line->ops = ops;
327}
328
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200329#if 0
330struct e1inp_line *e1inp_line_get_create(uint8_t e1_nr)
331{
332 struct e1inp_line *line;
333 int i;
334
335 line = e1inp_line_get(e1_nr);
336 if (line)
337 return line;
338
339 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
340 if (!line)
341 return NULL;
342
343 line->num = e1_nr;
344 for (i = 0; i < NUM_E1_TS; i++) {
345 line->ts[i].num = i+1;
346 line->ts[i].line = line;
347 }
348 llist_add_tail(&line->list, &e1inp_line_list);
349
350 return line;
351}
352#endif
353
354static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr)
355{
356 struct e1inp_line *e1i_line;
357
358 e1i_line = e1inp_line_get(e1_nr);
359 if (!e1i_line)
360 return NULL;
361
362 return &e1i_line->ts[ts_nr-1];
363}
364
365struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr)
366{
367 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
368
369 if (!e1i_ts)
370 return NULL;
371
372 return &e1i_ts->trau.mux;
373}
374
375/* Signalling Link */
376
377struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
378 uint8_t tei, uint8_t sapi)
379{
380 struct e1inp_sign_link *link;
381
382 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
383 if (link->sapi == sapi && link->tei == tei)
384 return link;
385 }
386
387 return NULL;
388}
389
390/* create a new signalling link in a E1 timeslot */
391
392struct e1inp_sign_link *
393e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
394 struct gsm_bts_trx *trx, uint8_t tei,
395 uint8_t sapi)
396{
397 struct e1inp_sign_link *link;
398
399 if (ts->type != E1INP_TS_TYPE_SIGN)
400 return NULL;
401
402 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
403 if (!link)
404 return NULL;
405
406 link->ts = ts;
407 link->type = type;
408 INIT_LLIST_HEAD(&link->tx_list);
409 link->trx = trx;
410 link->tei = tei;
411 link->sapi = sapi;
412
413 llist_add_tail(&link->list, &ts->sign.sign_links);
414
415 return link;
416}
417
418void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
419{
420 struct msgb *msg;
421
422 llist_del(&link->list);
423 while (!llist_empty(&link->tx_list)) {
424 msg = msgb_dequeue(&link->tx_list);
425 msgb_free(msg);
426 }
427
428 if (link->ts->type == E1INP_TS_TYPE_SIGN)
429 osmo_timer_del(&link->ts->sign.tx_timer);
430
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200431 if (link->ts->line->driver->close)
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200432 link->ts->line->driver->close(link);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200433
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200434 talloc_free(link);
435}
436
437/* XXX */
438/* the E1 driver tells us he has received something on a TS */
439int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
440 uint8_t tei, uint8_t sapi)
441{
442 struct e1inp_sign_link *link;
443 int ret = 0;
444
445 switch (ts->type) {
446 case E1INP_TS_TYPE_SIGN:
447 /* consult the list of signalling links */
448 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
449 link = e1inp_lookup_sign_link(ts, tei, sapi);
450 if (!link) {
451 LOGP(DMI, LOGL_ERROR, "didn't find signalling link for "
452 "tei %d, sapi %d\n", tei, sapi);
453 return -EINVAL;
454 }
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200455 if (!ts->line->ops->sign_link) {
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200456 LOGP(DINP, LOGL_ERROR, "Fix your application, "
457 "no action set for signalling messages.\n");
458 return -ENOENT;
459 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200460 msg->dst = link;
461 ts->line->ops->sign_link(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200462 break;
463 case E1INP_TS_TYPE_TRAU:
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200464 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200465 break;
466 default:
467 ret = -EINVAL;
468 LOGP(DMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
469 break;
470 }
471
472 return ret;
473}
474
475#define TSX_ALLOC_SIZE 4096
476
477/* called by driver if it wants to transmit on a given TS */
478struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
479 struct e1inp_sign_link **sign_link)
480{
481 struct e1inp_sign_link *link;
482 struct msgb *msg = NULL;
483 int len;
484
485 switch (e1i_ts->type) {
486 case E1INP_TS_TYPE_SIGN:
487 /* FIXME: implement this round robin */
488 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
489 msg = msgb_dequeue(&link->tx_list);
490 if (msg) {
491 if (sign_link)
492 *sign_link = link;
493 break;
494 }
495 }
496 break;
497 case E1INP_TS_TYPE_TRAU:
498 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
499 if (!msg)
500 return NULL;
501 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
502 msgb_put(msg, 40);
503 break;
504 default:
505 LOGP(DMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
506 return NULL;
507 }
508 return msg;
509}
510
511/* called by driver in case some kind of link state event */
512int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi)
513{
514 struct e1inp_sign_link *link;
515 struct input_signal_data isd;
516
517 link = e1inp_lookup_sign_link(ts, tei, sapi);
518 if (!link)
519 return -EINVAL;
520
521 isd.link_type = link->type;
522 isd.trx = link->trx;
523 isd.tei = tei;
524 isd.sapi = sapi;
525
526 /* report further upwards */
527 osmo_signal_dispatch(SS_INPUT, evt, &isd);
528 return 0;
529}
530
531/* register a driver with the E1 core */
532int e1inp_driver_register(struct e1inp_driver *drv)
533{
534 llist_add_tail(&drv->list, &e1inp_driver_list);
535 return 0;
536}
537
538struct e1inp_driver *e1inp_driver_find(const char *name)
539{
540 struct e1inp_driver *drv;
541
542 llist_for_each_entry(drv, &e1inp_driver_list, list) {
543 if (!strcasecmp(name, drv->name))
544 return drv;
545 }
546 return NULL;
547}
548
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200549int e1inp_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200550{
551 struct input_signal_data isd;
552 int rc;
553
554 /* This line has been already initialized, skip this. */
555 if (++line->refcnt > 1)
556 return 0;
557
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200558 if (line->driver && line->ops && line->driver->line_update) {
559 rc = line->driver->line_update(line, line->ops->role,
560 line->ops->addr);
561 } else
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200562 rc = 0;
563
564 /* Send a signal to anyone who is interested in new lines being
565 * configured */
566 memset(&isd, 0, sizeof(isd));
567 isd.line = line;
568 osmo_signal_dispatch(SS_INPUT, S_INP_LINE_INIT, &isd);
569
570 return rc;
571}
572
Pablo Neira Ayusoe19c70a2011-06-12 15:15:30 +0200573static int e1i_sig_cb(uint8_t subsys, unsigned int signal,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200574 void *handler_data, void *signal_data)
575{
576 if (subsys != SS_GLOBAL ||
577 signal != S_GLOBAL_SHUTDOWN)
578 return 0;
579
580 if (pcap_fd) {
581 close(pcap_fd);
582 pcap_fd = -1;
583 }
584
585 return 0;
586}
587
588void e1inp_misdn_init(void);
589void e1inp_dahdi_init(void);
590void e1inp_ipaccess_init(void);
591void e1inp_hsl_init(void);
592
593void e1inp_init(void)
594{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200595 tall_e1inp_ctx = talloc_named_const(libosmo_abis_ctx, 1, "e1inp");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200596 tall_sigl_ctx = talloc_named_const(tall_e1inp_ctx, 1,
597 "e1inp_sign_link");
598 osmo_signal_register_handler(SS_GLOBAL, e1i_sig_cb, NULL);
599
600 e1inp_misdn_init();
601#ifdef HAVE_DAHDI_USER_H
602 e1inp_dahdi_init();
603#endif
604 e1inp_ipaccess_init();
605 e1inp_hsl_init();
606}