blob: cad39aab1a65679875871b9cd987f8ea1782ce61 [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"
Harald Welte3bc78852011-08-24 08:32:38 +020023#include "../config.h"
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020024
25#include <stdio.h>
26#include <unistd.h>
27#include <stdlib.h>
28#include <errno.h>
29#include <string.h>
30#include <time.h>
31#include <sys/fcntl.h>
32#include <sys/socket.h>
33#include <sys/ioctl.h>
34#include <arpa/inet.h>
35#include <mISDNif.h>
36
Harald Weltefd44a5f2011-08-21 00:48:54 +020037#include <osmocom/abis/lapd.h>
38
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020039//#define AF_COMPATIBILITY_FUNC
40//#include <compat_af_isdn.h>
41#ifndef AF_ISDN
42#define AF_ISDN 34
43#define PF_ISDN AF_ISDN
44#endif
45
Harald Weltef2737fc2011-08-16 14:30:10 +020046#include <osmocom/core/linuxlist.h>
47#include <osmocom/core/talloc.h>
48#include <osmocom/core/rate_ctr.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020049#include <osmocom/core/logging.h>
50#include <osmocom/core/signal.h>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020051#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020052
53#define NUM_E1_TS 32
54
55static void *tall_e1inp_ctx;
56
57/* list of all E1 drivers */
58LLIST_HEAD(e1inp_driver_list);
59
60/* list of all E1 lines */
61LLIST_HEAD(e1inp_line_list);
62
63static void *tall_sigl_ctx;
64
Harald Weltef2737fc2011-08-16 14:30:10 +020065static const struct rate_ctr_desc e1inp_ctr_d[] = {
66 [E1I_CTR_HDLC_ABORT] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020067 "hdlc.abort", "HDLC abort"
Harald Weltef2737fc2011-08-16 14:30:10 +020068 },
69 [E1I_CTR_HDLC_BADFCS] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020070 "hdlc.bad_fcs", "HLDC Bad FCS"
Harald Weltef2737fc2011-08-16 14:30:10 +020071 },
72 [E1I_CTR_HDLC_OVERR] = {
73 "hdlc.overrun", "HDLC Overrun"
74 },
75 [E1I_CTR_ALARM] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020076 "alarm", "Alarm"
Harald Weltef2737fc2011-08-16 14:30:10 +020077 },
78 [E1I_CTR_REMOVED] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020079 "removed", "Line removed"
Harald Weltef2737fc2011-08-16 14:30:10 +020080 },
81};
82
83static const struct rate_ctr_group_desc e1inp_ctr_g_d = {
84 .group_name_prefix = "e1inp",
85 .group_description = "E1 Input subsystem",
86 .num_ctr = ARRAY_SIZE(e1inp_ctr_d),
87 .ctr_desc = e1inp_ctr_d,
88};
89
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020090/*
91 * pcap writing of the misdn load
92 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
93 */
94#define DLT_LINUX_LAPD 177
95#define PCAP_INPUT 0
96#define PCAP_OUTPUT 1
97
98struct pcap_hdr {
99 uint32_t magic_number;
100 uint16_t version_major;
101 uint16_t version_minor;
102 int32_t thiszone;
103 uint32_t sigfigs;
104 uint32_t snaplen;
105 uint32_t network;
106} __attribute__((packed));
107
108struct pcaprec_hdr {
109 uint32_t ts_sec;
110 uint32_t ts_usec;
111 uint32_t incl_len;
112 uint32_t orig_len;
113} __attribute__((packed));
114
115struct fake_linux_lapd_header {
116 uint16_t pkttype;
117 uint16_t hatype;
118 uint16_t halen;
119 uint64_t addr;
120 int16_t protocol;
121} __attribute__((packed));
122
123struct lapd_header {
124 uint8_t ea1 : 1;
125 uint8_t cr : 1;
126 uint8_t sapi : 6;
127 uint8_t ea2 : 1;
128 uint8_t tei : 7;
129 uint8_t control_foo; /* fake UM's ... */
130} __attribute__((packed));
131
132osmo_static_assert(offsetof(struct fake_linux_lapd_header, hatype) == 2, hatype_offset);
133osmo_static_assert(offsetof(struct fake_linux_lapd_header, halen) == 4, halen_offset);
134osmo_static_assert(offsetof(struct fake_linux_lapd_header, addr) == 6, addr_offset);
135osmo_static_assert(offsetof(struct fake_linux_lapd_header, protocol) == 14, proto_offset);
136osmo_static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
137
138
139static int pcap_fd = -1;
140
141void e1_set_pcap_fd(int fd)
142{
143 int ret;
144 struct pcap_hdr header = {
145 .magic_number = 0xa1b2c3d4,
146 .version_major = 2,
147 .version_minor = 4,
148 .thiszone = 0,
149 .sigfigs = 0,
150 .snaplen = 65535,
151 .network = DLT_LINUX_LAPD,
152 };
153
154 pcap_fd = fd;
155 ret = write(pcap_fd, &header, sizeof(header));
156}
157
158/* This currently only works for the D-Channel */
159static void write_pcap_packet(int direction, int sapi, int tei,
160 struct msgb *msg) {
161 if (pcap_fd < 0)
162 return;
163
164 int ret;
165 time_t cur_time;
166 struct tm *tm;
167
168 struct fake_linux_lapd_header header = {
169 .pkttype = 4,
170 .hatype = 0,
171 .halen = 0,
172 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
173 .protocol = ntohs(48),
174 };
175
176 struct lapd_header lapd_header = {
177 .ea1 = 0,
178 .cr = direction == PCAP_OUTPUT ? 1 : 0,
179 .sapi = sapi & 0x3F,
180 .ea2 = 1,
181 .tei = tei & 0x7F,
182 .control_foo = 0x03 /* UI */,
183 };
184
185 struct pcaprec_hdr payload_header = {
186 .ts_sec = 0,
187 .ts_usec = 0,
188 .incl_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
189 + sizeof(struct lapd_header),
190 .orig_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
191 + sizeof(struct lapd_header),
192 };
193
194
195 cur_time = time(NULL);
196 tm = localtime(&cur_time);
197 payload_header.ts_sec = mktime(tm);
198
199 ret = write(pcap_fd, &payload_header, sizeof(payload_header));
200 ret = write(pcap_fd, &header, sizeof(header));
201 ret = write(pcap_fd, &lapd_header, sizeof(lapd_header));
202 ret = write(pcap_fd, msg->l2h, msgb_l2len(msg));
203}
204
205static const char *sign_types[] = {
206 [E1INP_SIGN_NONE] = "None",
207 [E1INP_SIGN_OML] = "OML",
208 [E1INP_SIGN_RSL] = "RSL",
209};
210const char *e1inp_signtype_name(enum e1inp_sign_type tp)
211{
212 if (tp >= ARRAY_SIZE(sign_types))
213 return "undefined";
214 return sign_types[tp];
215}
216
217static const char *ts_types[] = {
218 [E1INP_TS_TYPE_NONE] = "None",
219 [E1INP_TS_TYPE_SIGN] = "Signalling",
220 [E1INP_TS_TYPE_TRAU] = "TRAU",
221};
222
223const char *e1inp_tstype_name(enum e1inp_ts_type tp)
224{
225 if (tp >= ARRAY_SIZE(ts_types))
226 return "undefined";
227 return ts_types[tp];
228}
229
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200230int abis_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200231{
232 struct e1inp_sign_link *sign_link = msg->dst;
233 struct e1inp_driver *e1inp_driver;
234 struct e1inp_ts *e1i_ts;
235;
236 msg->l2h = msg->data;
237
238 /* don't know how to route this message. */
239 if (sign_link == NULL) {
Harald Welte40b0e8c2011-07-21 16:57:34 +0200240 LOGP(DLINP, LOGL_ERROR, "abis_sendmsg: msg->dst == NULL: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200241 osmo_hexdump(msg->data, msg->len));
242 talloc_free(msg);
243 return -EINVAL;
244 }
245 e1i_ts = sign_link->ts;
246 if (!osmo_timer_pending(&e1i_ts->sign.tx_timer)) {
247 /* notify the driver we have something to write */
248 e1inp_driver = sign_link->ts->line->driver;
249 e1inp_driver->want_write(e1i_ts);
250 }
251 msgb_enqueue(&sign_link->tx_list, msg);
252
253 /* dump it */
254 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
255
256 return 0;
257}
258
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200259int abis_rsl_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200260{
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200261 return abis_sendmsg(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200262}
263
264/* Timeslot */
265int e1inp_ts_config_trau(struct e1inp_ts *ts, struct e1inp_line *line,
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200266 int (*trau_rcv_cb)(struct subch_demux *dmx, int ch,
267 uint8_t *data, int len, void *_priv))
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200268{
269 if (ts->type == E1INP_TS_TYPE_TRAU && ts->line && line)
270 return 0;
271
272 ts->type = E1INP_TS_TYPE_TRAU;
273 ts->line = line;
274
275 subchan_mux_init(&ts->trau.mux);
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200276 ts->trau.demux.out_cb = trau_rcv_cb;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200277 ts->trau.demux.data = ts;
278 subch_demux_init(&ts->trau.demux);
279 return 0;
280}
281
282int e1inp_ts_config_sign(struct e1inp_ts *ts, struct e1inp_line *line)
283{
284 if (ts->type == E1INP_TS_TYPE_SIGN && ts->line && line)
285 return 0;
286
287 ts->type = E1INP_TS_TYPE_SIGN;
288 ts->line = line;
289
290 if (line && line->driver)
291 ts->sign.delay = line->driver->default_delay;
292 else
293 ts->sign.delay = 100000;
294 INIT_LLIST_HEAD(&ts->sign.sign_links);
295 return 0;
296}
297
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200298struct e1inp_line *e1inp_line_find(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200299{
300 struct e1inp_line *e1i_line;
301
302 /* iterate over global list of e1 lines */
303 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
304 if (e1i_line->num == e1_nr)
305 return e1i_line;
306 }
307 return NULL;
308}
309
310struct e1inp_line *
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200311e1inp_line_create(uint8_t e1_nr, const char *driver_name)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200312{
313 struct e1inp_driver *driver;
314 struct e1inp_line *line;
315 int i;
316
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200317 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200318 if (line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200319 LOGP(DLINP, LOGL_ERROR, "E1 Line %u already exists\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200320 e1_nr);
321 return NULL;
322 }
323
324 driver = e1inp_driver_find(driver_name);
325 if (!driver) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200326 LOGP(DLINP, LOGL_ERROR, "No such E1 driver '%s'\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200327 driver_name);
328 return NULL;
329 }
330
331 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
332 if (!line)
333 return NULL;
334
335 line->driver = driver;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200336 line->num = e1_nr;
Harald Weltef2737fc2011-08-16 14:30:10 +0200337
338 line->rate_ctr = rate_ctr_group_alloc(line, &e1inp_ctr_g_d, line->num);
339
Harald Weltec2889512011-09-13 23:49:04 +0100340 line->num_ts = NUM_E1_TS;
341 for (i = 0; i < line->num_ts; i++) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200342 line->ts[i].num = i+1;
343 line->ts[i].line = line;
344 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200345 line->refcnt++;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200346 llist_add_tail(&line->list, &e1inp_line_list);
347
348 return line;
349}
350
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200351struct e1inp_line *
352e1inp_line_clone(void *ctx, struct e1inp_line *line)
353{
354 struct e1inp_line *clone;
355
356 /* clone virtual E1 line for this new OML link. */
357 clone = talloc_zero(ctx, struct e1inp_line);
358 if (clone == NULL)
359 return NULL;
360
361 memcpy(clone, line, sizeof(struct e1inp_line));
362 clone->refcnt = 1;
363 return clone;
364}
365
366void e1inp_line_get(struct e1inp_line *line)
367{
368 line->refcnt++;
369}
370
371void e1inp_line_put(struct e1inp_line *line)
372{
373 line->refcnt--;
374 if (line->refcnt == 0)
375 talloc_free(line);
376}
377
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200378void
379e1inp_line_bind_ops(struct e1inp_line *line, const struct e1inp_line_ops *ops)
380{
381 line->ops = ops;
382}
383
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200384#if 0
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200385struct e1inp_line *e1inp_line_find_create(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200386{
387 struct e1inp_line *line;
388 int i;
389
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200390 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200391 if (line)
392 return line;
393
394 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
395 if (!line)
396 return NULL;
397
398 line->num = e1_nr;
399 for (i = 0; i < NUM_E1_TS; i++) {
400 line->ts[i].num = i+1;
401 line->ts[i].line = line;
402 }
403 llist_add_tail(&line->list, &e1inp_line_list);
404
405 return line;
406}
407#endif
408
409static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr)
410{
411 struct e1inp_line *e1i_line;
412
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200413 e1i_line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200414 if (!e1i_line)
415 return NULL;
416
417 return &e1i_line->ts[ts_nr-1];
418}
419
420struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr)
421{
422 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
423
424 if (!e1i_ts)
425 return NULL;
426
427 return &e1i_ts->trau.mux;
428}
429
430/* Signalling Link */
431
432struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
433 uint8_t tei, uint8_t sapi)
434{
435 struct e1inp_sign_link *link;
436
437 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
438 if (link->sapi == sapi && link->tei == tei)
439 return link;
440 }
441
442 return NULL;
443}
444
445/* create a new signalling link in a E1 timeslot */
446
447struct e1inp_sign_link *
448e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
449 struct gsm_bts_trx *trx, uint8_t tei,
450 uint8_t sapi)
451{
452 struct e1inp_sign_link *link;
453
454 if (ts->type != E1INP_TS_TYPE_SIGN)
455 return NULL;
456
457 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
458 if (!link)
459 return NULL;
460
461 link->ts = ts;
462 link->type = type;
463 INIT_LLIST_HEAD(&link->tx_list);
464 link->trx = trx;
465 link->tei = tei;
466 link->sapi = sapi;
467
468 llist_add_tail(&link->list, &ts->sign.sign_links);
469
470 return link;
471}
472
473void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
474{
475 struct msgb *msg;
476
477 llist_del(&link->list);
478 while (!llist_empty(&link->tx_list)) {
479 msg = msgb_dequeue(&link->tx_list);
480 msgb_free(msg);
481 }
482
483 if (link->ts->type == E1INP_TS_TYPE_SIGN)
484 osmo_timer_del(&link->ts->sign.tx_timer);
485
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200486 if (link->ts->line->driver->close)
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200487 link->ts->line->driver->close(link);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200488
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200489 talloc_free(link);
490}
491
492/* XXX */
493/* the E1 driver tells us he has received something on a TS */
494int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
495 uint8_t tei, uint8_t sapi)
496{
497 struct e1inp_sign_link *link;
498 int ret = 0;
499
500 switch (ts->type) {
501 case E1INP_TS_TYPE_SIGN:
502 /* consult the list of signalling links */
503 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
504 link = e1inp_lookup_sign_link(ts, tei, sapi);
505 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200506 LOGP(DLMI, LOGL_ERROR, "didn't find signalling link for "
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200507 "tei %d, sapi %d\n", tei, sapi);
508 return -EINVAL;
509 }
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200510 if (!ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200511 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200512 "no action set for signalling messages.\n");
513 return -ENOENT;
514 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200515 msg->dst = link;
516 ts->line->ops->sign_link(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200517 break;
518 case E1INP_TS_TYPE_TRAU:
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200519 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200520 break;
521 default:
522 ret = -EINVAL;
Harald Weltecc2241b2011-07-19 16:06:06 +0200523 LOGP(DLMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200524 break;
525 }
526
527 return ret;
528}
529
Harald Weltefd44a5f2011-08-21 00:48:54 +0200530/*! \brief Receive some data from the L1/HDLC into LAPD of a timeslot
531 * \param[in] e1i_ts E1 Timeslot data structure
532 * \param[in] msg Message buffer containing full LAPD message
533 *
534 * This is a wrapper around e1inp_rx_ts(), but feeding the incoming
535 * message first into our LAPD code. This allows a driver to read raw
536 * (HDLC decoded) data from the timeslot, instead of a LAPD stack
537 * present in any underlying driver.
538 */
539int e1inp_rx_ts_lapd(struct e1inp_ts *e1i_ts, struct msgb *msg)
540{
541 lapd_mph_type prim;
542 unsigned int sapi, tei;
543 int ilen, ret = 0, error = 0;
544 uint8_t *idata;
545
546 sapi = msg->data[0] >> 2;
547 tei = msg->data[1] >> 1;
548
549 DEBUGP(DLMI, "<= len = %d, sapi(%d) tei(%d)", msg->len, sapi, tei);
550
551 idata = lapd_receive(e1i_ts->lapd, msg->data, msg->len, &ilen, &prim, &error);
552 if (!idata) {
553 switch(error) {
554 case LAPD_ERR_UNKNOWN_TEI:
555 /* We don't know about this TEI, probably the BSC
556 * lost local states (it crashed or it was stopped),
557 * notify the driver to see if it can do anything to
558 * recover the existing signalling links with the BTS.
559 */
560 e1inp_event(e1i_ts, S_L_INP_TEI_UNKNOWN, tei, sapi);
561 return -EIO;
562 }
563 if (prim == 0)
564 return -EIO;
565 }
566
567 msgb_pull(msg, 2);
568
569 DEBUGP(DLMI, "prim %08x\n", prim);
570
571 switch (prim) {
572 case 0:
573 break;
574 case LAPD_MPH_ACTIVATE_IND:
575 DEBUGP(DLMI, "MPH_ACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei);
576 ret = e1inp_event(e1i_ts, S_L_INP_TEI_UP, tei, sapi);
577 break;
578 case LAPD_MPH_DEACTIVATE_IND:
579 DEBUGP(DLMI, "MPH_DEACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei);
580 ret = e1inp_event(e1i_ts, S_L_INP_TEI_DN, tei, sapi);
581 break;
582 case LAPD_DL_DATA_IND:
583 case LAPD_DL_UNITDATA_IND:
584 if (prim == LAPD_DL_DATA_IND)
585 msg->l2h = msg->data + 2;
586 else
587 msg->l2h = msg->data + 1;
588 DEBUGP(DLMI, "RX: %s\n", osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
589 ret = e1inp_rx_ts(e1i_ts, msg, tei, sapi);
590 break;
591 default:
592 printf("ERROR: unknown prim\n");
593 break;
594 }
595 return ret;
596}
597
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200598#define TSX_ALLOC_SIZE 4096
599
600/* called by driver if it wants to transmit on a given TS */
601struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
602 struct e1inp_sign_link **sign_link)
603{
604 struct e1inp_sign_link *link;
605 struct msgb *msg = NULL;
606 int len;
607
608 switch (e1i_ts->type) {
609 case E1INP_TS_TYPE_SIGN:
610 /* FIXME: implement this round robin */
611 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
612 msg = msgb_dequeue(&link->tx_list);
613 if (msg) {
614 if (sign_link)
615 *sign_link = link;
616 break;
617 }
618 }
619 break;
620 case E1INP_TS_TYPE_TRAU:
621 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
622 if (!msg)
623 return NULL;
624 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
625 msgb_put(msg, 40);
626 break;
627 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200628 LOGP(DLMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200629 return NULL;
630 }
631 return msg;
632}
633
634/* called by driver in case some kind of link state event */
635int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi)
636{
637 struct e1inp_sign_link *link;
638 struct input_signal_data isd;
639
640 link = e1inp_lookup_sign_link(ts, tei, sapi);
641 if (!link)
642 return -EINVAL;
643
Pablo Neira Ayuso31fe5f22011-08-09 23:15:38 +0200644 isd.line = ts->line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200645 isd.link_type = link->type;
646 isd.trx = link->trx;
647 isd.tei = tei;
648 isd.sapi = sapi;
649
650 /* report further upwards */
Harald Weltecc2241b2011-07-19 16:06:06 +0200651 osmo_signal_dispatch(SS_L_INPUT, evt, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200652 return 0;
653}
654
655/* register a driver with the E1 core */
656int e1inp_driver_register(struct e1inp_driver *drv)
657{
658 llist_add_tail(&drv->list, &e1inp_driver_list);
659 return 0;
660}
661
662struct e1inp_driver *e1inp_driver_find(const char *name)
663{
664 struct e1inp_driver *drv;
665
666 llist_for_each_entry(drv, &e1inp_driver_list, list) {
667 if (!strcasecmp(name, drv->name))
668 return drv;
669 }
670 return NULL;
671}
672
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200673int e1inp_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200674{
675 struct input_signal_data isd;
676 int rc;
677
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200678 e1inp_line_get(line);
679
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200680 if (line->driver && line->ops && line->driver->line_update) {
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200681 rc = line->driver->line_update(line);
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200682 } else
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200683 rc = 0;
684
685 /* Send a signal to anyone who is interested in new lines being
686 * configured */
687 memset(&isd, 0, sizeof(isd));
688 isd.line = line;
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200689 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_INIT, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200690
691 return rc;
692}
693
Pablo Neira Ayusoa20762a2011-07-02 19:01:58 +0200694static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200695 void *handler_data, void *signal_data)
696{
Harald Weltecc2241b2011-07-19 16:06:06 +0200697 if (subsys != SS_L_GLOBAL ||
698 signal != S_L_GLOBAL_SHUTDOWN)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200699 return 0;
700
701 if (pcap_fd) {
702 close(pcap_fd);
703 pcap_fd = -1;
704 }
705
706 return 0;
707}
708
709void e1inp_misdn_init(void);
710void e1inp_dahdi_init(void);
711void e1inp_ipaccess_init(void);
712void e1inp_hsl_init(void);
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200713void e1inp_rs232_init(void);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200714
715void e1inp_init(void)
716{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200717 tall_e1inp_ctx = talloc_named_const(libosmo_abis_ctx, 1, "e1inp");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200718 tall_sigl_ctx = talloc_named_const(tall_e1inp_ctx, 1,
719 "e1inp_sign_link");
Harald Weltecc2241b2011-07-19 16:06:06 +0200720 osmo_signal_register_handler(SS_L_GLOBAL, e1i_sig_cb, NULL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200721
722 e1inp_misdn_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200723#ifdef HAVE_DAHDI_USER_H
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200724 e1inp_dahdi_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200725#endif
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200726 e1inp_ipaccess_init();
727 e1inp_hsl_init();
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200728 e1inp_rs232_init();
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200729}