blob: c454b4a641ba89b5a495127d2f3e3bea3a26a767 [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
Pablo Neira Ayuso2e11f5c2013-07-05 15:08:18 +0200141int e1_set_pcap_fd(int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200142{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200143 struct pcap_hdr header = {
144 .magic_number = 0xa1b2c3d4,
145 .version_major = 2,
146 .version_minor = 4,
147 .thiszone = 0,
148 .sigfigs = 0,
149 .snaplen = 65535,
150 .network = DLT_LINUX_LAPD,
151 };
152
153 pcap_fd = fd;
Pablo Neira Ayuso2e11f5c2013-07-05 15:08:18 +0200154 return write(pcap_fd, &header, sizeof(header));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200155}
156
157/* This currently only works for the D-Channel */
158static void write_pcap_packet(int direction, int sapi, int tei,
159 struct msgb *msg) {
160 if (pcap_fd < 0)
161 return;
162
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200163 time_t cur_time;
164 struct tm *tm;
165
166 struct fake_linux_lapd_header header = {
167 .pkttype = 4,
168 .hatype = 0,
169 .halen = 0,
170 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
171 .protocol = ntohs(48),
172 };
173
174 struct lapd_header lapd_header = {
175 .ea1 = 0,
176 .cr = direction == PCAP_OUTPUT ? 1 : 0,
177 .sapi = sapi & 0x3F,
178 .ea2 = 1,
179 .tei = tei & 0x7F,
180 .control_foo = 0x03 /* UI */,
181 };
182
183 struct pcaprec_hdr payload_header = {
184 .ts_sec = 0,
185 .ts_usec = 0,
186 .incl_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
187 + sizeof(struct lapd_header),
188 .orig_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
189 + sizeof(struct lapd_header),
190 };
191
192
193 cur_time = time(NULL);
194 tm = localtime(&cur_time);
195 payload_header.ts_sec = mktime(tm);
196
Harald Weltec9295ea2014-08-21 02:41:41 +0200197 write(pcap_fd, &payload_header, sizeof(payload_header));
198 write(pcap_fd, &header, sizeof(header));
199 write(pcap_fd, &lapd_header, sizeof(lapd_header));
200 write(pcap_fd, msg->l2h, msgb_l2len(msg));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200201}
202
203static const char *sign_types[] = {
204 [E1INP_SIGN_NONE] = "None",
205 [E1INP_SIGN_OML] = "OML",
206 [E1INP_SIGN_RSL] = "RSL",
Harald Welte46fc7e22014-08-18 19:04:26 +0200207 [E1INP_SIGN_OSMO] = "OSMO",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200208};
209const char *e1inp_signtype_name(enum e1inp_sign_type tp)
210{
211 if (tp >= ARRAY_SIZE(sign_types))
212 return "undefined";
213 return sign_types[tp];
214}
215
216static const char *ts_types[] = {
217 [E1INP_TS_TYPE_NONE] = "None",
218 [E1INP_TS_TYPE_SIGN] = "Signalling",
219 [E1INP_TS_TYPE_TRAU] = "TRAU",
220};
221
222const char *e1inp_tstype_name(enum e1inp_ts_type tp)
223{
224 if (tp >= ARRAY_SIZE(ts_types))
225 return "undefined";
226 return ts_types[tp];
227}
228
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200229int abis_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200230{
231 struct e1inp_sign_link *sign_link = msg->dst;
232 struct e1inp_driver *e1inp_driver;
233 struct e1inp_ts *e1i_ts;
234;
235 msg->l2h = msg->data;
236
237 /* don't know how to route this message. */
238 if (sign_link == NULL) {
Harald Welte40b0e8c2011-07-21 16:57:34 +0200239 LOGP(DLINP, LOGL_ERROR, "abis_sendmsg: msg->dst == NULL: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200240 osmo_hexdump(msg->data, msg->len));
241 talloc_free(msg);
242 return -EINVAL;
243 }
244 e1i_ts = sign_link->ts;
245 if (!osmo_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
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200258int abis_rsl_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200259{
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200260 return abis_sendmsg(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200261}
262
263/* Timeslot */
264int e1inp_ts_config_trau(struct e1inp_ts *ts, struct e1inp_line *line,
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200265 int (*trau_rcv_cb)(struct subch_demux *dmx, int ch,
266 uint8_t *data, int len, void *_priv))
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200267{
268 if (ts->type == E1INP_TS_TYPE_TRAU && ts->line && line)
269 return 0;
270
271 ts->type = E1INP_TS_TYPE_TRAU;
272 ts->line = line;
273
274 subchan_mux_init(&ts->trau.mux);
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200275 ts->trau.demux.out_cb = trau_rcv_cb;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200276 ts->trau.demux.data = ts;
277 subch_demux_init(&ts->trau.demux);
278 return 0;
279}
280
281int e1inp_ts_config_sign(struct e1inp_ts *ts, struct e1inp_line *line)
282{
283 if (ts->type == E1INP_TS_TYPE_SIGN && ts->line && line)
284 return 0;
285
286 ts->type = E1INP_TS_TYPE_SIGN;
287 ts->line = line;
288
289 if (line && line->driver)
290 ts->sign.delay = line->driver->default_delay;
291 else
292 ts->sign.delay = 100000;
293 INIT_LLIST_HEAD(&ts->sign.sign_links);
294 return 0;
295}
296
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200297struct e1inp_line *e1inp_line_find(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200298{
299 struct e1inp_line *e1i_line;
300
301 /* iterate over global list of e1 lines */
302 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
303 if (e1i_line->num == e1_nr)
304 return e1i_line;
305 }
306 return NULL;
307}
308
309struct e1inp_line *
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200310e1inp_line_create(uint8_t e1_nr, const char *driver_name)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200311{
312 struct e1inp_driver *driver;
313 struct e1inp_line *line;
314 int i;
315
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200316 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200317 if (line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200318 LOGP(DLINP, LOGL_ERROR, "E1 Line %u already exists\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200319 e1_nr);
320 return NULL;
321 }
322
323 driver = e1inp_driver_find(driver_name);
324 if (!driver) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200325 LOGP(DLINP, LOGL_ERROR, "No such E1 driver '%s'\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200326 driver_name);
327 return NULL;
328 }
329
330 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
331 if (!line)
332 return NULL;
333
334 line->driver = driver;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200335 line->num = e1_nr;
Harald Weltef2737fc2011-08-16 14:30:10 +0200336
337 line->rate_ctr = rate_ctr_group_alloc(line, &e1inp_ctr_g_d, line->num);
338
Harald Weltec2889512011-09-13 23:49:04 +0100339 line->num_ts = NUM_E1_TS;
340 for (i = 0; i < line->num_ts; i++) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200341 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 Ayuso6cc3f922012-08-22 13:57:58 +0200488 e1inp_line_put(link->ts->line);
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);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200508 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200509 return -EINVAL;
510 }
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200511 if (!ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200512 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200513 "no action set for signalling messages.\n");
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200514 msgb_free(msg);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200515 return -ENOENT;
516 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200517 msg->dst = link;
518 ts->line->ops->sign_link(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200519 break;
520 case E1INP_TS_TYPE_TRAU:
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200521 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200522 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200523 break;
524 default:
525 ret = -EINVAL;
Harald Weltecc2241b2011-07-19 16:06:06 +0200526 LOGP(DLMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200527 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200528 break;
529 }
530
531 return ret;
532}
533
Harald Weltefd44a5f2011-08-21 00:48:54 +0200534/*! \brief Receive some data from the L1/HDLC into LAPD of a timeslot
535 * \param[in] e1i_ts E1 Timeslot data structure
536 * \param[in] msg Message buffer containing full LAPD message
537 *
538 * This is a wrapper around e1inp_rx_ts(), but feeding the incoming
539 * message first into our LAPD code. This allows a driver to read raw
540 * (HDLC decoded) data from the timeslot, instead of a LAPD stack
541 * present in any underlying driver.
542 */
543int e1inp_rx_ts_lapd(struct e1inp_ts *e1i_ts, struct msgb *msg)
544{
Harald Weltefd44a5f2011-08-21 00:48:54 +0200545 unsigned int sapi, tei;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200546 int ret = 0, error = 0;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200547
548 sapi = msg->data[0] >> 2;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200549 if ((msg->data[0] & 0x1))
550 tei = 0;
551 else
552 tei = msg->data[1] >> 1;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200553
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200554 DEBUGP(DLMI, "<= len = %d, sapi(%d) tei(%d)\n", msg->len, sapi, tei);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200555
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200556 ret = lapd_receive(e1i_ts->lapd, msg, &error);
557 if (ret < 0) {
Harald Weltefd44a5f2011-08-21 00:48:54 +0200558 switch(error) {
559 case LAPD_ERR_UNKNOWN_TEI:
560 /* We don't know about this TEI, probably the BSC
561 * lost local states (it crashed or it was stopped),
562 * notify the driver to see if it can do anything to
563 * recover the existing signalling links with the BTS.
564 */
565 e1inp_event(e1i_ts, S_L_INP_TEI_UNKNOWN, tei, sapi);
566 return -EIO;
567 }
Harald Weltefd44a5f2011-08-21 00:48:54 +0200568 }
569
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200570 return 0;
571}
Harald Weltefd44a5f2011-08-21 00:48:54 +0200572
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200573void e1inp_dlsap_up(struct osmo_dlsap_prim *dp, uint8_t tei, uint8_t sapi,
574 void *rx_cbdata)
575{
576 struct e1inp_ts *e1i_ts = rx_cbdata;
577 struct msgb *msg = dp->oph.msg;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200578
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200579 switch (dp->oph.primitive) {
580 case PRIM_DL_EST:
581 DEBUGP(DLMI, "DL_EST: sapi(%d) tei(%d)\n", sapi, tei);
582 e1inp_event(e1i_ts, S_L_INP_TEI_UP, tei, sapi);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200583 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200584 case PRIM_DL_REL:
585 DEBUGP(DLMI, "DL_REL: sapi(%d) tei(%d)\n", sapi, tei);
586 e1inp_event(e1i_ts, S_L_INP_TEI_DN, tei, sapi);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200587 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200588 case PRIM_DL_DATA:
589 case PRIM_DL_UNIT_DATA:
590 if (dp->oph.operation == PRIM_OP_INDICATION) {
591 msg->l2h = msg->l3h;
592 DEBUGP(DLMI, "RX: %s sapi=%d tei=%d\n",
593 osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)),
594 sapi, tei);
595 e1inp_rx_ts(e1i_ts, msg, tei, sapi);
596 return;
597 }
Harald Weltefd44a5f2011-08-21 00:48:54 +0200598 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200599 case PRIM_MDL_ERROR:
600 DEBUGP(DLMI, "MDL_EERROR: cause(%d)\n", dp->u.error_ind.cause);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200601 break;
602 default:
603 printf("ERROR: unknown prim\n");
604 break;
605 }
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200606
607 msgb_free(msg);
608
609 return;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200610}
611
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200612#define TSX_ALLOC_SIZE 4096
613
614/* called by driver if it wants to transmit on a given TS */
615struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
616 struct e1inp_sign_link **sign_link)
617{
618 struct e1inp_sign_link *link;
619 struct msgb *msg = NULL;
620 int len;
621
622 switch (e1i_ts->type) {
623 case E1INP_TS_TYPE_SIGN:
624 /* FIXME: implement this round robin */
625 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
626 msg = msgb_dequeue(&link->tx_list);
627 if (msg) {
628 if (sign_link)
629 *sign_link = link;
630 break;
631 }
632 }
633 break;
634 case E1INP_TS_TYPE_TRAU:
635 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
636 if (!msg)
637 return NULL;
638 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
Pablo Neira Ayuso33f71752013-07-05 15:44:12 +0200639 if (len != 40) {
640 LOGP(DLMI, LOGL_ERROR,
641 "cannot transmit, failed to mux\n");
642 msgb_free(msg);
643 return NULL;
644 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200645 msgb_put(msg, 40);
646 break;
647 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200648 LOGP(DLMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200649 return NULL;
650 }
651 return msg;
652}
653
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100654static int e1inp_int_snd_event(struct e1inp_ts *ts,
655 struct e1inp_sign_link *link, int evt)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200656{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200657 struct input_signal_data isd;
Pablo Neira Ayuso31fe5f22011-08-09 23:15:38 +0200658 isd.line = ts->line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200659 isd.link_type = link->type;
660 isd.trx = link->trx;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100661 isd.tei = link->tei;
662 isd.sapi = link->sapi;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200663
664 /* report further upwards */
Harald Weltecc2241b2011-07-19 16:06:06 +0200665 osmo_signal_dispatch(SS_L_INPUT, evt, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200666 return 0;
667}
668
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100669
670/* called by driver in case some kind of link state event */
671int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi)
672{
673 struct e1inp_sign_link *link;
674
675 link = e1inp_lookup_sign_link(ts, tei, sapi);
676 if (!link)
677 return -EINVAL;
678 return e1inp_int_snd_event(ts, link, evt);
679}
680
681void e1inp_close_socket(struct e1inp_ts *ts,
682 struct e1inp_sign_link *sign_link,
683 struct osmo_fd *bfd)
684{
685 e1inp_int_snd_event(ts, sign_link, S_L_INP_TEI_DN);
686 /* the first e1inp_sign_link_destroy call closes the socket. */
687 if (bfd->fd != -1) {
688 osmo_fd_unregister(bfd);
689 close(bfd->fd);
690 bfd->fd = -1;
691 }
692}
693
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200694/* register a driver with the E1 core */
695int e1inp_driver_register(struct e1inp_driver *drv)
696{
697 llist_add_tail(&drv->list, &e1inp_driver_list);
698 return 0;
699}
700
701struct e1inp_driver *e1inp_driver_find(const char *name)
702{
703 struct e1inp_driver *drv;
704
705 llist_for_each_entry(drv, &e1inp_driver_list, list) {
706 if (!strcasecmp(name, drv->name))
707 return drv;
708 }
709 return NULL;
710}
711
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200712int e1inp_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200713{
714 struct input_signal_data isd;
715 int rc;
716
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200717 e1inp_line_get(line);
718
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200719 if (line->driver && line->ops && line->driver->line_update) {
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200720 rc = line->driver->line_update(line);
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200721 } else
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200722 rc = 0;
723
724 /* Send a signal to anyone who is interested in new lines being
725 * configured */
726 memset(&isd, 0, sizeof(isd));
727 isd.line = line;
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200728 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_INIT, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200729
730 return rc;
731}
732
Pablo Neira Ayusoa20762a2011-07-02 19:01:58 +0200733static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200734 void *handler_data, void *signal_data)
735{
Harald Weltecc2241b2011-07-19 16:06:06 +0200736 if (subsys != SS_L_GLOBAL ||
737 signal != S_L_GLOBAL_SHUTDOWN)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200738 return 0;
739
740 if (pcap_fd) {
741 close(pcap_fd);
742 pcap_fd = -1;
743 }
744
745 return 0;
746}
747
748void e1inp_misdn_init(void);
749void e1inp_dahdi_init(void);
750void e1inp_ipaccess_init(void);
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200751void e1inp_rs232_init(void);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200752
753void e1inp_init(void)
754{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200755 tall_e1inp_ctx = talloc_named_const(libosmo_abis_ctx, 1, "e1inp");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200756 tall_sigl_ctx = talloc_named_const(tall_e1inp_ctx, 1,
757 "e1inp_sign_link");
Harald Weltecc2241b2011-07-19 16:06:06 +0200758 osmo_signal_register_handler(SS_L_GLOBAL, e1i_sig_cb, NULL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200759
760 e1inp_misdn_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200761#ifdef HAVE_DAHDI_USER_H
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200762 e1inp_dahdi_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200763#endif
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200764 e1inp_ipaccess_init();
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200765 e1inp_rs232_init();
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200766}