blob: ad0778a892e3db7776409a8243754e05ef3e92a8 [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
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200340 for (i = 0; i < NUM_E1_TS; i++) {
341 line->ts[i].num = i+1;
342 line->ts[i].line = line;
343 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200344 line->refcnt++;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200345 llist_add_tail(&line->list, &e1inp_line_list);
346
347 return line;
348}
349
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200350struct e1inp_line *
351e1inp_line_clone(void *ctx, struct e1inp_line *line)
352{
353 struct e1inp_line *clone;
354
355 /* clone virtual E1 line for this new OML link. */
356 clone = talloc_zero(ctx, struct e1inp_line);
357 if (clone == NULL)
358 return NULL;
359
360 memcpy(clone, line, sizeof(struct e1inp_line));
361 clone->refcnt = 1;
362 return clone;
363}
364
365void e1inp_line_get(struct e1inp_line *line)
366{
367 line->refcnt++;
368}
369
370void e1inp_line_put(struct e1inp_line *line)
371{
372 line->refcnt--;
373 if (line->refcnt == 0)
374 talloc_free(line);
375}
376
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200377void
378e1inp_line_bind_ops(struct e1inp_line *line, const struct e1inp_line_ops *ops)
379{
380 line->ops = ops;
381}
382
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200383#if 0
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200384struct e1inp_line *e1inp_line_find_create(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200385{
386 struct e1inp_line *line;
387 int i;
388
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200389 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200390 if (line)
391 return line;
392
393 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
394 if (!line)
395 return NULL;
396
397 line->num = e1_nr;
398 for (i = 0; i < NUM_E1_TS; i++) {
399 line->ts[i].num = i+1;
400 line->ts[i].line = line;
401 }
402 llist_add_tail(&line->list, &e1inp_line_list);
403
404 return line;
405}
406#endif
407
408static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr)
409{
410 struct e1inp_line *e1i_line;
411
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200412 e1i_line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200413 if (!e1i_line)
414 return NULL;
415
416 return &e1i_line->ts[ts_nr-1];
417}
418
419struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr)
420{
421 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
422
423 if (!e1i_ts)
424 return NULL;
425
426 return &e1i_ts->trau.mux;
427}
428
429/* Signalling Link */
430
431struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
432 uint8_t tei, uint8_t sapi)
433{
434 struct e1inp_sign_link *link;
435
436 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
437 if (link->sapi == sapi && link->tei == tei)
438 return link;
439 }
440
441 return NULL;
442}
443
444/* create a new signalling link in a E1 timeslot */
445
446struct e1inp_sign_link *
447e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
448 struct gsm_bts_trx *trx, uint8_t tei,
449 uint8_t sapi)
450{
451 struct e1inp_sign_link *link;
452
453 if (ts->type != E1INP_TS_TYPE_SIGN)
454 return NULL;
455
456 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
457 if (!link)
458 return NULL;
459
460 link->ts = ts;
461 link->type = type;
462 INIT_LLIST_HEAD(&link->tx_list);
463 link->trx = trx;
464 link->tei = tei;
465 link->sapi = sapi;
466
467 llist_add_tail(&link->list, &ts->sign.sign_links);
468
469 return link;
470}
471
472void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
473{
474 struct msgb *msg;
475
476 llist_del(&link->list);
477 while (!llist_empty(&link->tx_list)) {
478 msg = msgb_dequeue(&link->tx_list);
479 msgb_free(msg);
480 }
481
482 if (link->ts->type == E1INP_TS_TYPE_SIGN)
483 osmo_timer_del(&link->ts->sign.tx_timer);
484
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200485 if (link->ts->line->driver->close)
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200486 link->ts->line->driver->close(link);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200487
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200488 talloc_free(link);
489}
490
491/* XXX */
492/* the E1 driver tells us he has received something on a TS */
493int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
494 uint8_t tei, uint8_t sapi)
495{
496 struct e1inp_sign_link *link;
497 int ret = 0;
498
499 switch (ts->type) {
500 case E1INP_TS_TYPE_SIGN:
501 /* consult the list of signalling links */
502 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
503 link = e1inp_lookup_sign_link(ts, tei, sapi);
504 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200505 LOGP(DLMI, LOGL_ERROR, "didn't find signalling link for "
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200506 "tei %d, sapi %d\n", tei, sapi);
507 return -EINVAL;
508 }
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200509 if (!ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200510 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200511 "no action set for signalling messages.\n");
512 return -ENOENT;
513 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200514 msg->dst = link;
515 ts->line->ops->sign_link(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200516 break;
517 case E1INP_TS_TYPE_TRAU:
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200518 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200519 break;
520 default:
521 ret = -EINVAL;
Harald Weltecc2241b2011-07-19 16:06:06 +0200522 LOGP(DLMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200523 break;
524 }
525
526 return ret;
527}
528
Harald Weltefd44a5f2011-08-21 00:48:54 +0200529/*! \brief Receive some data from the L1/HDLC into LAPD of a timeslot
530 * \param[in] e1i_ts E1 Timeslot data structure
531 * \param[in] msg Message buffer containing full LAPD message
532 *
533 * This is a wrapper around e1inp_rx_ts(), but feeding the incoming
534 * message first into our LAPD code. This allows a driver to read raw
535 * (HDLC decoded) data from the timeslot, instead of a LAPD stack
536 * present in any underlying driver.
537 */
538int e1inp_rx_ts_lapd(struct e1inp_ts *e1i_ts, struct msgb *msg)
539{
540 lapd_mph_type prim;
541 unsigned int sapi, tei;
542 int ilen, ret = 0, error = 0;
543 uint8_t *idata;
544
545 sapi = msg->data[0] >> 2;
546 tei = msg->data[1] >> 1;
547
548 DEBUGP(DLMI, "<= len = %d, sapi(%d) tei(%d)", msg->len, sapi, tei);
549
550 idata = lapd_receive(e1i_ts->lapd, msg->data, msg->len, &ilen, &prim, &error);
551 if (!idata) {
552 switch(error) {
553 case LAPD_ERR_UNKNOWN_TEI:
554 /* We don't know about this TEI, probably the BSC
555 * lost local states (it crashed or it was stopped),
556 * notify the driver to see if it can do anything to
557 * recover the existing signalling links with the BTS.
558 */
559 e1inp_event(e1i_ts, S_L_INP_TEI_UNKNOWN, tei, sapi);
560 return -EIO;
561 }
562 if (prim == 0)
563 return -EIO;
564 }
565
566 msgb_pull(msg, 2);
567
568 DEBUGP(DLMI, "prim %08x\n", prim);
569
570 switch (prim) {
571 case 0:
572 break;
573 case LAPD_MPH_ACTIVATE_IND:
574 DEBUGP(DLMI, "MPH_ACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei);
575 ret = e1inp_event(e1i_ts, S_L_INP_TEI_UP, tei, sapi);
576 break;
577 case LAPD_MPH_DEACTIVATE_IND:
578 DEBUGP(DLMI, "MPH_DEACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei);
579 ret = e1inp_event(e1i_ts, S_L_INP_TEI_DN, tei, sapi);
580 break;
581 case LAPD_DL_DATA_IND:
582 case LAPD_DL_UNITDATA_IND:
583 if (prim == LAPD_DL_DATA_IND)
584 msg->l2h = msg->data + 2;
585 else
586 msg->l2h = msg->data + 1;
587 DEBUGP(DLMI, "RX: %s\n", osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
588 ret = e1inp_rx_ts(e1i_ts, msg, tei, sapi);
589 break;
590 default:
591 printf("ERROR: unknown prim\n");
592 break;
593 }
594 return ret;
595}
596
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200597#define TSX_ALLOC_SIZE 4096
598
599/* called by driver if it wants to transmit on a given TS */
600struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
601 struct e1inp_sign_link **sign_link)
602{
603 struct e1inp_sign_link *link;
604 struct msgb *msg = NULL;
605 int len;
606
607 switch (e1i_ts->type) {
608 case E1INP_TS_TYPE_SIGN:
609 /* FIXME: implement this round robin */
610 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
611 msg = msgb_dequeue(&link->tx_list);
612 if (msg) {
613 if (sign_link)
614 *sign_link = link;
615 break;
616 }
617 }
618 break;
619 case E1INP_TS_TYPE_TRAU:
620 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
621 if (!msg)
622 return NULL;
623 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
624 msgb_put(msg, 40);
625 break;
626 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200627 LOGP(DLMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200628 return NULL;
629 }
630 return msg;
631}
632
633/* called by driver in case some kind of link state event */
634int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi)
635{
636 struct e1inp_sign_link *link;
637 struct input_signal_data isd;
638
639 link = e1inp_lookup_sign_link(ts, tei, sapi);
640 if (!link)
641 return -EINVAL;
642
Pablo Neira Ayuso31fe5f22011-08-09 23:15:38 +0200643 isd.line = ts->line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200644 isd.link_type = link->type;
645 isd.trx = link->trx;
646 isd.tei = tei;
647 isd.sapi = sapi;
648
649 /* report further upwards */
Harald Weltecc2241b2011-07-19 16:06:06 +0200650 osmo_signal_dispatch(SS_L_INPUT, evt, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200651 return 0;
652}
653
654/* register a driver with the E1 core */
655int e1inp_driver_register(struct e1inp_driver *drv)
656{
657 llist_add_tail(&drv->list, &e1inp_driver_list);
658 return 0;
659}
660
661struct e1inp_driver *e1inp_driver_find(const char *name)
662{
663 struct e1inp_driver *drv;
664
665 llist_for_each_entry(drv, &e1inp_driver_list, list) {
666 if (!strcasecmp(name, drv->name))
667 return drv;
668 }
669 return NULL;
670}
671
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200672int e1inp_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200673{
674 struct input_signal_data isd;
675 int rc;
676
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200677 e1inp_line_get(line);
678
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200679 if (line->driver && line->ops && line->driver->line_update) {
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200680 rc = line->driver->line_update(line);
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200681 } else
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200682 rc = 0;
683
684 /* Send a signal to anyone who is interested in new lines being
685 * configured */
686 memset(&isd, 0, sizeof(isd));
687 isd.line = line;
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200688 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_INIT, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200689
690 return rc;
691}
692
Pablo Neira Ayusoa20762a2011-07-02 19:01:58 +0200693static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200694 void *handler_data, void *signal_data)
695{
Harald Weltecc2241b2011-07-19 16:06:06 +0200696 if (subsys != SS_L_GLOBAL ||
697 signal != S_L_GLOBAL_SHUTDOWN)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200698 return 0;
699
700 if (pcap_fd) {
701 close(pcap_fd);
702 pcap_fd = -1;
703 }
704
705 return 0;
706}
707
708void e1inp_misdn_init(void);
709void e1inp_dahdi_init(void);
710void e1inp_ipaccess_init(void);
711void e1inp_hsl_init(void);
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200712void e1inp_rs232_init(void);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200713
714void e1inp_init(void)
715{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200716 tall_e1inp_ctx = talloc_named_const(libosmo_abis_ctx, 1, "e1inp");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200717 tall_sigl_ctx = talloc_named_const(tall_e1inp_ctx, 1,
718 "e1inp_sign_link");
Harald Weltecc2241b2011-07-19 16:06:06 +0200719 osmo_signal_register_handler(SS_L_GLOBAL, e1i_sig_cb, NULL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200720
721 e1inp_misdn_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200722#ifdef HAVE_DAHDI_USER_H
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200723 e1inp_dahdi_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200724#endif
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200725 e1inp_ipaccess_init();
726 e1inp_hsl_init();
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200727 e1inp_rs232_init();
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200728}