blob: 92bfb242aa990e89987c66edac7ac7c7ce937838 [file] [log] [blame]
Harald Welte1fa60c82009-02-09 18:13:26 +00001/* 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
Harald Welte9af6ddf2011-01-01 15:25:50 +01008 * 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
Harald Welte1fa60c82009-02-09 18:13:26 +000010 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte1fa60c82009-02-09 18:13:26 +000016 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * 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/>.
Harald Welte1fa60c82009-02-09 18:13:26 +000019 *
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>
Holger Freyther66e092b2009-03-31 12:13:50 +000037#ifndef AF_ISDN
Harald Welte1fa60c82009-02-09 18:13:26 +000038#define AF_ISDN 34
39#define PF_ISDN AF_ISDN
Holger Freyther66e092b2009-03-31 12:13:50 +000040#endif
Harald Welte1fa60c82009-02-09 18:13:26 +000041
Harald Weltedfe6c7d2010-02-20 16:24:02 +010042#include <osmocore/select.h>
43#include <osmocore/msgb.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000044#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>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010049#include <osmocore/linuxlist.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000050#include <openbsc/subchan_demux.h>
Harald Welte45b407a2009-05-23 15:51:12 +000051#include <openbsc/trau_frame.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000052#include <openbsc/trau_mux.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010053#include <osmocore/talloc.h>
Harald Welte29b9cf82009-11-30 19:43:54 +010054#include <openbsc/signal.h>
Holger Hans Peter Freyther34e97492009-08-10 07:54:02 +020055#include <openbsc/misdn.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000056
Harald Welteca17ef82011-02-05 15:13:27 +010057#include "../bscconfig.h"
58
Harald Welte1fa60c82009-02-09 18:13:26 +000059#define NUM_E1_TS 32
60
61/* list of all E1 drivers */
Harald Welte44d542e2009-03-10 18:24:35 +000062LLIST_HEAD(e1inp_driver_list);
Harald Welte1fa60c82009-02-09 18:13:26 +000063
64/* list of all E1 lines */
Harald Welte44d542e2009-03-10 18:24:35 +000065LLIST_HEAD(e1inp_line_list);
Harald Welte1fa60c82009-02-09 18:13:26 +000066
Harald Welte2cf161b2009-06-20 22:36:41 +020067static void *tall_sigl_ctx;
68
Harald Welte1fa60c82009-02-09 18:13:26 +000069/*
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
Holger Hans Peter Freyther0b369c52010-11-09 23:10:16 +0100111static_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);
Harald Welte1fa60c82009-02-09 18:13:26 +0000116
117
118static int pcap_fd = -1;
119
Holger Freyther0469cf62009-03-31 12:14:16 +0000120void e1_set_pcap_fd(int fd)
Harald Welte1fa60c82009-02-09 18:13:26 +0000121{
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 */
Holger Freyther0469cf62009-03-31 12:14:16 +0000138static void write_pcap_packet(int direction, int sapi, int tei,
Harald Welte1fa60c82009-02-09 18:13:26 +0000139 struct msgb *msg) {
140 if (pcap_fd < 0)
141 return;
142
143 int ret;
144 time_t cur_time;
145 struct tm *tm;
Andreas Eversberg20152a32009-06-10 14:47:33 +0200146 int mi_head = (direction==PCAP_INPUT) ? MISDN_HEADER_LEN : 0;
Harald Welte1fa60c82009-02-09 18:13:26 +0000147
148 struct fake_linux_lapd_header header = {
149 .pkttype = 4,
150 .hatype = 0,
151 .halen = 0,
152 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
153 .protocol = ntohs(48),
154 };
155
156 struct lapd_header lapd_header = {
157 .ea1 = 0,
158 .cr = direction == PCAP_OUTPUT ? 1 : 0,
Holger Freyther0469cf62009-03-31 12:14:16 +0000159 .sapi = sapi & 0x3F,
Harald Welte1fa60c82009-02-09 18:13:26 +0000160 .ea2 = 1,
Holger Freyther0469cf62009-03-31 12:14:16 +0000161 .tei = tei & 0x7F,
Harald Welte1fa60c82009-02-09 18:13:26 +0000162 .control_foo = 0x03 /* UI */,
163 };
164
165 struct pcaprec_hdr payload_header = {
166 .ts_sec = 0,
167 .ts_usec = 0,
168 .incl_len = msg->len + sizeof(struct fake_linux_lapd_header)
169 + sizeof(struct lapd_header)
Andreas Eversberg20152a32009-06-10 14:47:33 +0200170 - mi_head,
Harald Welte1fa60c82009-02-09 18:13:26 +0000171 .orig_len = msg->len + sizeof(struct fake_linux_lapd_header)
172 + sizeof(struct lapd_header)
Andreas Eversberg20152a32009-06-10 14:47:33 +0200173 - mi_head,
Harald Welte1fa60c82009-02-09 18:13:26 +0000174 };
175
176
177 cur_time = time(NULL);
178 tm = localtime(&cur_time);
179 payload_header.ts_sec = mktime(tm);
180
181 ret = write(pcap_fd, &payload_header, sizeof(payload_header));
182 ret = write(pcap_fd, &header, sizeof(header));
183 ret = write(pcap_fd, &lapd_header, sizeof(lapd_header));
Andreas Eversberg20152a32009-06-10 14:47:33 +0200184 ret = write(pcap_fd, msg->data + mi_head,
185 msg->len - mi_head);
Harald Welte1fa60c82009-02-09 18:13:26 +0000186}
Harald Welte1fa60c82009-02-09 18:13:26 +0000187
Harald Welted256d4f2009-03-10 19:44:48 +0000188static const char *sign_types[] = {
189 [E1INP_SIGN_NONE] = "None",
190 [E1INP_SIGN_OML] = "OML",
191 [E1INP_SIGN_RSL] = "RSL",
192};
193const char *e1inp_signtype_name(enum e1inp_sign_type tp)
194{
195 if (tp >= ARRAY_SIZE(sign_types))
196 return "undefined";
197 return sign_types[tp];
198}
199
200static const char *ts_types[] = {
201 [E1INP_TS_TYPE_NONE] = "None",
202 [E1INP_TS_TYPE_SIGN] = "Signalling",
203 [E1INP_TS_TYPE_TRAU] = "TRAU",
204};
205
206const char *e1inp_tstype_name(enum e1inp_ts_type tp)
207{
208 if (tp >= ARRAY_SIZE(ts_types))
209 return "undefined";
210 return ts_types[tp];
211}
212
Harald Welte1fa60c82009-02-09 18:13:26 +0000213/* callback when a TRAU frame was received */
214static int subch_cb(struct subch_demux *dmx, int ch, u_int8_t *data, int len,
215 void *_priv)
216{
217 struct e1inp_ts *e1i_ts = _priv;
218 struct gsm_e1_subslot src_ss;
219
220 src_ss.e1_nr = e1i_ts->line->num;
221 src_ss.e1_ts = e1i_ts->num;
222 src_ss.e1_ts_ss = ch;
223
224 return trau_mux_input(&src_ss, data, len);
225}
226
227int abis_rsl_sendmsg(struct msgb *msg)
228{
229 struct e1inp_sign_link *sign_link;
230 struct e1inp_driver *e1inp_driver;
Harald Weltef55b49f2009-05-23 06:20:41 +0000231 struct e1inp_ts *e1i_ts;
Harald Welte1fa60c82009-02-09 18:13:26 +0000232
233 msg->l2h = msg->data;
234
Harald Welte (local)5fe33182009-12-28 17:05:43 +0100235 if (!msg->trx) {
236 LOGP(DRSL, LOGL_ERROR, "rsl_sendmsg: msg->trx == NULL: %s\n",
237 hexdump(msg->data, msg->len));
Harald Welteeab33352009-06-27 03:09:08 +0200238 talloc_free(msg);
Harald Welte1fa60c82009-02-09 18:13:26 +0000239 return -EINVAL;
Harald Welte (local)5fe33182009-12-28 17:05:43 +0100240 } else if (!msg->trx->rsl_link) {
241 LOGP(DRSL, LOGL_ERROR, "rsl_sendmsg: msg->trx->rsl_link == NULL: %s\n",
242 hexdump(msg->data, msg->len));
243 talloc_free(msg);
244 return -EIO;
Harald Welte1fa60c82009-02-09 18:13:26 +0000245 }
246
247 sign_link = msg->trx->rsl_link;
Harald Weltef55b49f2009-05-23 06:20:41 +0000248 e1i_ts = sign_link->ts;
249 if (!bsc_timer_pending(&e1i_ts->sign.tx_timer)) {
250 /* notify the driver we have something to write */
251 e1inp_driver = sign_link->ts->line->driver;
252 e1inp_driver->want_write(e1i_ts);
253 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000254 msgb_enqueue(&sign_link->tx_list, msg);
255
Holger Freyther0469cf62009-03-31 12:14:16 +0000256 /* dump it */
257 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
258
Harald Welte1fa60c82009-02-09 18:13:26 +0000259 return 0;
260}
261
262int _abis_nm_sendmsg(struct msgb *msg)
263{
264 struct e1inp_sign_link *sign_link;
265 struct e1inp_driver *e1inp_driver;
Harald Weltef55b49f2009-05-23 06:20:41 +0000266 struct e1inp_ts *e1i_ts;
Harald Welte1fa60c82009-02-09 18:13:26 +0000267
268 msg->l2h = msg->data;
269
270 if (!msg->trx || !msg->trx->bts || !msg->trx->bts->oml_link) {
Harald Weltef933de92011-02-05 20:18:18 +0100271 LOGP(DNM, LOGL_ERROR, "nm_sendmsg: msg->trx == NULL\n");
Harald Welte1fa60c82009-02-09 18:13:26 +0000272 return -EINVAL;
273 }
Holger Freytherfb3f5192009-02-09 23:09:15 +0000274
Harald Welte1fa60c82009-02-09 18:13:26 +0000275 sign_link = msg->trx->bts->oml_link;
Harald Weltef55b49f2009-05-23 06:20:41 +0000276 e1i_ts = sign_link->ts;
277 if (!bsc_timer_pending(&e1i_ts->sign.tx_timer)) {
278 /* notify the driver we have something to write */
279 e1inp_driver = sign_link->ts->line->driver;
280 e1inp_driver->want_write(e1i_ts);
281 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000282 msgb_enqueue(&sign_link->tx_list, msg);
283
Holger Freyther0469cf62009-03-31 12:14:16 +0000284 /* dump it */
285 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
286
Harald Welte1fa60c82009-02-09 18:13:26 +0000287 return 0;
288}
289
290/* Timeslot */
291
292/* configure and initialize one e1inp_ts */
293int e1inp_ts_config(struct e1inp_ts *ts, struct e1inp_line *line,
294 enum e1inp_ts_type type)
295{
Harald Welte42581822009-08-08 16:12:58 +0200296 if (ts->type == type && ts->line && line)
297 return 0;
298
Harald Welte1fa60c82009-02-09 18:13:26 +0000299 ts->type = type;
300 ts->line = line;
301
302 switch (type) {
303 case E1INP_TS_TYPE_SIGN:
Holger Hans Peter Freyther91f587e2011-01-16 18:12:13 +0100304 if (line && line->driver)
Holger Hans Peter Freytherd85642a2010-04-26 16:02:04 +0800305 ts->sign.delay = line->driver->default_delay;
306 else
307 ts->sign.delay = 100000;
Harald Welte1fa60c82009-02-09 18:13:26 +0000308 INIT_LLIST_HEAD(&ts->sign.sign_links);
309 break;
310 case E1INP_TS_TYPE_TRAU:
311 subchan_mux_init(&ts->trau.mux);
312 ts->trau.demux.out_cb = subch_cb;
313 ts->trau.demux.data = ts;
314 subch_demux_init(&ts->trau.demux);
315 break;
316 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100317 LOGP(DMI, LOGL_ERROR, "unsupported E1 timeslot type %u\n",
Harald Welte1fa60c82009-02-09 18:13:26 +0000318 ts->type);
319 return -EINVAL;
320 }
321 return 0;
322}
323
Harald Welte3016d9f2011-02-05 13:54:41 +0100324struct e1inp_line *e1inp_line_get(u_int8_t e1_nr)
Harald Welte1fa60c82009-02-09 18:13:26 +0000325{
326 struct e1inp_line *e1i_line;
327
328 /* iterate over global list of e1 lines */
Harald Welte44d542e2009-03-10 18:24:35 +0000329 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
Harald Welte1fa60c82009-02-09 18:13:26 +0000330 if (e1i_line->num == e1_nr)
331 return e1i_line;
332 }
333 return NULL;
334}
335
Harald Welte3016d9f2011-02-05 13:54:41 +0100336struct e1inp_line *e1inp_line_create(u_int8_t e1_nr, const char *driver_name)
337{
338 struct e1inp_driver *driver;
339 struct e1inp_line *line;
340 int i;
341
342 line = e1inp_line_get(e1_nr);
Harald Welte07bb0da2011-02-05 15:57:42 +0100343 if (line) {
344 LOGP(DINP, LOGL_ERROR, "E1 Line %u already exists\n",
345 e1_nr);
Harald Welte3016d9f2011-02-05 13:54:41 +0100346 return NULL;
Harald Welte07bb0da2011-02-05 15:57:42 +0100347 }
Harald Welte3016d9f2011-02-05 13:54:41 +0100348
349 driver = e1inp_driver_find(driver_name);
Harald Welte07bb0da2011-02-05 15:57:42 +0100350 if (!driver) {
351 LOGP(DINP, LOGL_ERROR, "No such E1 driver '%s'\n",
352 driver_name);
Harald Welte3016d9f2011-02-05 13:54:41 +0100353 return NULL;
Harald Welte07bb0da2011-02-05 15:57:42 +0100354 }
Harald Welte3016d9f2011-02-05 13:54:41 +0100355
356 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
357 if (!line)
358 return NULL;
359
360 line->driver = driver;
361
362 line->num = e1_nr;
363 for (i = 0; i < NUM_E1_TS; i++) {
364 line->ts[i].num = i+1;
365 line->ts[i].line = line;
366 }
367 llist_add_tail(&line->list, &e1inp_line_list);
368
369 return line;
370}
371
372#if 0
Harald Welte42581822009-08-08 16:12:58 +0200373struct e1inp_line *e1inp_line_get_create(u_int8_t e1_nr)
374{
375 struct e1inp_line *line;
376 int i;
377
378 line = e1inp_line_get(e1_nr);
379 if (line)
380 return line;
381
382 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
383 if (!line)
384 return NULL;
385
386 line->num = e1_nr;
387 for (i = 0; i < NUM_E1_TS; i++) {
388 line->ts[i].num = i+1;
389 line->ts[i].line = line;
390 }
391 llist_add_tail(&line->list, &e1inp_line_list);
392
393 return line;
394}
Harald Welte3016d9f2011-02-05 13:54:41 +0100395#endif
Harald Welte42581822009-08-08 16:12:58 +0200396
Harald Welte1fa60c82009-02-09 18:13:26 +0000397static struct e1inp_ts *e1inp_ts_get(u_int8_t e1_nr, u_int8_t ts_nr)
398{
399 struct e1inp_line *e1i_line;
400
401 e1i_line = e1inp_line_get(e1_nr);
402 if (!e1i_line)
403 return NULL;
404
405 return &e1i_line->ts[ts_nr-1];
406}
407
408struct subch_mux *e1inp_get_mux(u_int8_t e1_nr, u_int8_t ts_nr)
409{
410 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
411
412 if (!e1i_ts)
413 return NULL;
414
415 return &e1i_ts->trau.mux;
416}
417
418/* Signalling Link */
419
420struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
421 u_int8_t tei, u_int8_t sapi)
422{
423 struct e1inp_sign_link *link;
424
425 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
426 if (link->sapi == sapi && link->tei == tei)
427 return link;
428 }
429
430 return NULL;
431}
432
433/* create a new signalling link in a E1 timeslot */
434
435struct e1inp_sign_link *
436e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
437 struct gsm_bts_trx *trx, u_int8_t tei,
438 u_int8_t sapi)
439{
440 struct e1inp_sign_link *link;
441
442 if (ts->type != E1INP_TS_TYPE_SIGN)
443 return NULL;
444
Harald Welte470ec292009-06-26 20:25:23 +0200445 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
Harald Welte1fa60c82009-02-09 18:13:26 +0000446 if (!link)
447 return NULL;
448
Harald Welte1fa60c82009-02-09 18:13:26 +0000449 link->ts = ts;
450 link->type = type;
451 INIT_LLIST_HEAD(&link->tx_list);
452 link->trx = trx;
Holger Freytherfb3f5192009-02-09 23:09:15 +0000453 link->tei = tei;
454 link->sapi = sapi;
Harald Welte1fa60c82009-02-09 18:13:26 +0000455
456 llist_add_tail(&link->list, &ts->sign.sign_links);
457
458 return link;
459}
460
Harald Welte42581822009-08-08 16:12:58 +0200461void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
462{
Holger Hans Peter Freyther4ac100e2010-04-11 14:13:10 +0200463 struct msgb *msg;
464
Harald Welte42581822009-08-08 16:12:58 +0200465 llist_del(&link->list);
Holger Hans Peter Freyther4ac100e2010-04-11 14:13:10 +0200466 while (!llist_empty(&link->tx_list)) {
467 msg = msgb_dequeue(&link->tx_list);
468 msgb_free(msg);
469 }
470
Holger Hans Peter Freytherf51b9002010-04-11 17:18:02 +0200471 if (link->ts->type == E1INP_TS_TYPE_SIGN)
472 bsc_del_timer(&link->ts->sign.tx_timer);
473
Harald Welte42581822009-08-08 16:12:58 +0200474 talloc_free(link);
475}
476
Harald Welte1fa60c82009-02-09 18:13:26 +0000477/* the E1 driver tells us he has received something on a TS */
478int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
479 u_int8_t tei, u_int8_t sapi)
480{
481 struct e1inp_sign_link *link;
482 int ret;
483
484 switch (ts->type) {
485 case E1INP_TS_TYPE_SIGN:
Harald Welte1fa60c82009-02-09 18:13:26 +0000486 /* consult the list of signalling links */
Holger Freyther0469cf62009-03-31 12:14:16 +0000487 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
Harald Welte1fa60c82009-02-09 18:13:26 +0000488 link = e1inp_lookup_sign_link(ts, tei, sapi);
489 if (!link) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100490 LOGP(DMI, LOGL_ERROR, "didn't find signalling link for "
Harald Welte1fa60c82009-02-09 18:13:26 +0000491 "tei %d, sapi %d\n", tei, sapi);
492 return -EINVAL;
493 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100494
Harald Weltedc5062b2010-03-26 21:28:59 +0800495 log_set_context(BSC_CTX_BTS, link->trx->bts);
Harald Welte1fa60c82009-02-09 18:13:26 +0000496 switch (link->type) {
497 case E1INP_SIGN_OML:
498 msg->trx = link->trx;
499 ret = abis_nm_rcvmsg(msg);
500 break;
501 case E1INP_SIGN_RSL:
502 msg->trx = link->trx;
503 ret = abis_rsl_rcvmsg(msg);
504 break;
505 default:
506 ret = -EINVAL;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100507 LOGP(DMI, LOGL_ERROR, "unknown link type %u\n", link->type);
Harald Welte1fa60c82009-02-09 18:13:26 +0000508 break;
509 }
510 break;
511 case E1INP_TS_TYPE_TRAU:
Harald Welte9e783312009-02-17 19:02:29 +0000512 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Harald Welte1fa60c82009-02-09 18:13:26 +0000513 break;
514 default:
515 ret = -EINVAL;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100516 LOGP(DMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Harald Welte1fa60c82009-02-09 18:13:26 +0000517 break;
518 }
519
520 return ret;
521}
522
523#define TSX_ALLOC_SIZE 4096
524
525/* called by driver if it wants to transmit on a given TS */
526struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
527 struct e1inp_sign_link **sign_link)
528{
529 struct e1inp_sign_link *link;
530 struct msgb *msg = NULL;
531 int len;
532
533 switch (e1i_ts->type) {
534 case E1INP_TS_TYPE_SIGN:
535 /* FIXME: implement this round robin */
536 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
537 msg = msgb_dequeue(&link->tx_list);
538 if (msg) {
539 if (sign_link)
540 *sign_link = link;
541 break;
542 }
543 }
544 break;
545 case E1INP_TS_TYPE_TRAU:
Harald Welte966636f2009-06-26 19:39:35 +0200546 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
Harald Welte1fa60c82009-02-09 18:13:26 +0000547 if (!msg)
548 return NULL;
549 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
550 msgb_put(msg, 40);
551 break;
552 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100553 LOGP(DMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Harald Welte1fa60c82009-02-09 18:13:26 +0000554 return NULL;
555 }
556 return msg;
557}
558
559/* called by driver in case some kind of link state event */
560int e1inp_event(struct e1inp_ts *ts, int evt, u_int8_t tei, u_int8_t sapi)
561{
562 struct e1inp_sign_link *link;
Harald Weltef338a032011-01-14 15:55:42 +0100563 struct input_signal_data isd;
Harald Welte1fa60c82009-02-09 18:13:26 +0000564
565 link = e1inp_lookup_sign_link(ts, tei, sapi);
566 if (!link)
567 return -EINVAL;
568
Harald Weltef338a032011-01-14 15:55:42 +0100569 isd.link_type = link->type;
570 isd.trx = link->trx;
571
572 /* report further upwards */
573 dispatch_signal(SS_INPUT, evt, &isd);
Holger Freytherff9592f2009-03-09 16:17:14 +0000574 return 0;
Harald Welte1fa60c82009-02-09 18:13:26 +0000575}
576
577/* register a driver with the E1 core */
578int e1inp_driver_register(struct e1inp_driver *drv)
579{
Harald Welte44d542e2009-03-10 18:24:35 +0000580 llist_add_tail(&drv->list, &e1inp_driver_list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000581 return 0;
582}
583
Harald Welte3016d9f2011-02-05 13:54:41 +0100584struct e1inp_driver *e1inp_driver_find(const char *name)
585{
586 struct e1inp_driver *drv;
587
Harald Welte3016d9f2011-02-05 13:54:41 +0100588 llist_for_each_entry(drv, &e1inp_driver_list, list) {
589 if (!strcasecmp(name, drv->name))
590 return drv;
591 }
592 return NULL;
593}
594
Harald Welte42581822009-08-08 16:12:58 +0200595int e1inp_line_update(struct e1inp_line *line)
Harald Welte1fa60c82009-02-09 18:13:26 +0000596{
Harald Welte54552432011-02-05 12:26:55 +0100597 if (line->driver && line->driver->line_update)
598 return line->driver->line_update(line);
599 else
600 return 0;
Harald Welte1fa60c82009-02-09 18:13:26 +0000601}
Harald Welte7bfc2672009-07-28 00:41:45 +0200602
Harald Welte29b9cf82009-11-30 19:43:54 +0100603static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
604 void *handler_data, void *signal_data)
605{
606 if (subsys != SS_GLOBAL ||
607 signal != S_GLOBAL_SHUTDOWN)
608 return 0;
609
610 if (pcap_fd) {
611 close(pcap_fd);
612 pcap_fd = -1;
613 }
614
615 return 0;
616}
617
Harald Welte3016d9f2011-02-05 13:54:41 +0100618void e1inp_misdn_init(void);
Harald Welte1dd68c32011-02-05 13:58:46 +0100619void e1inp_dahdi_init(void);
Harald Welte3016d9f2011-02-05 13:54:41 +0100620
621void e1inp_init(void)
Harald Welte7bfc2672009-07-28 00:41:45 +0200622{
623 tall_sigl_ctx = talloc_named_const(tall_bsc_ctx, 1,
624 "e1inp_sign_link");
Harald Welte29b9cf82009-11-30 19:43:54 +0100625 register_signal_handler(SS_GLOBAL, e1i_sig_cb, NULL);
Harald Welte3016d9f2011-02-05 13:54:41 +0100626
627 e1inp_misdn_init();
Harald Welteca17ef82011-02-05 15:13:27 +0100628#ifdef HAVE_DAHDI_USER_H
Harald Welte1dd68c32011-02-05 13:58:46 +0100629 e1inp_dahdi_init();
Harald Welteca17ef82011-02-05 15:13:27 +0100630#endif
Harald Welte7bfc2672009-07-28 00:41:45 +0200631}