blob: c9ae8e64b7a3b6d69541692d2519e09046a35c2d [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
Harald Welte7f9d8512016-07-04 09:59:46 +0200252 /* we only need to write a 'Fake LAPD' packet here, if the
253 * underlying driver hides LAPD from us. If we use the
254 * libosmocore LAPD implementation, it will take care of writing
255 * the _actual_ LAPD packet */
256 if (!e1i_ts->lapd) {
257 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi,
258 sign_link->tei, msg);
259 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200260
261 return 0;
262}
263
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200264int abis_rsl_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200265{
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200266 return abis_sendmsg(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200267}
268
269/* Timeslot */
270int e1inp_ts_config_trau(struct e1inp_ts *ts, struct e1inp_line *line,
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200271 int (*trau_rcv_cb)(struct subch_demux *dmx, int ch,
272 uint8_t *data, int len, void *_priv))
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200273{
274 if (ts->type == E1INP_TS_TYPE_TRAU && ts->line && line)
275 return 0;
276
277 ts->type = E1INP_TS_TYPE_TRAU;
278 ts->line = line;
279
280 subchan_mux_init(&ts->trau.mux);
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200281 ts->trau.demux.out_cb = trau_rcv_cb;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200282 ts->trau.demux.data = ts;
283 subch_demux_init(&ts->trau.demux);
284 return 0;
285}
286
287int e1inp_ts_config_sign(struct e1inp_ts *ts, struct e1inp_line *line)
288{
289 if (ts->type == E1INP_TS_TYPE_SIGN && ts->line && line)
290 return 0;
291
292 ts->type = E1INP_TS_TYPE_SIGN;
293 ts->line = line;
294
295 if (line && line->driver)
296 ts->sign.delay = line->driver->default_delay;
297 else
298 ts->sign.delay = 100000;
299 INIT_LLIST_HEAD(&ts->sign.sign_links);
300 return 0;
301}
302
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200303struct e1inp_line *e1inp_line_find(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200304{
305 struct e1inp_line *e1i_line;
306
307 /* iterate over global list of e1 lines */
308 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
309 if (e1i_line->num == e1_nr)
310 return e1i_line;
311 }
312 return NULL;
313}
314
315struct e1inp_line *
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200316e1inp_line_create(uint8_t e1_nr, const char *driver_name)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200317{
318 struct e1inp_driver *driver;
319 struct e1inp_line *line;
320 int i;
321
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200322 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200323 if (line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200324 LOGP(DLINP, LOGL_ERROR, "E1 Line %u already exists\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200325 e1_nr);
326 return NULL;
327 }
328
329 driver = e1inp_driver_find(driver_name);
330 if (!driver) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200331 LOGP(DLINP, LOGL_ERROR, "No such E1 driver '%s'\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200332 driver_name);
333 return NULL;
334 }
335
336 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
337 if (!line)
338 return NULL;
339
340 line->driver = driver;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200341 line->num = e1_nr;
Harald Weltef2737fc2011-08-16 14:30:10 +0200342
343 line->rate_ctr = rate_ctr_group_alloc(line, &e1inp_ctr_g_d, line->num);
344
Harald Weltec2889512011-09-13 23:49:04 +0100345 line->num_ts = NUM_E1_TS;
346 for (i = 0; i < line->num_ts; i++) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200347 line->ts[i].num = i+1;
348 line->ts[i].line = line;
349 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200350 line->refcnt++;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200351 llist_add_tail(&line->list, &e1inp_line_list);
352
353 return line;
354}
355
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200356struct e1inp_line *
357e1inp_line_clone(void *ctx, struct e1inp_line *line)
358{
359 struct e1inp_line *clone;
360
361 /* clone virtual E1 line for this new OML link. */
362 clone = talloc_zero(ctx, struct e1inp_line);
363 if (clone == NULL)
364 return NULL;
365
366 memcpy(clone, line, sizeof(struct e1inp_line));
367 clone->refcnt = 1;
368 return clone;
369}
370
371void e1inp_line_get(struct e1inp_line *line)
372{
373 line->refcnt++;
374}
375
376void e1inp_line_put(struct e1inp_line *line)
377{
378 line->refcnt--;
379 if (line->refcnt == 0)
380 talloc_free(line);
381}
382
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200383void
384e1inp_line_bind_ops(struct e1inp_line *line, const struct e1inp_line_ops *ops)
385{
386 line->ops = ops;
387}
388
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200389#if 0
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200390struct e1inp_line *e1inp_line_find_create(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200391{
392 struct e1inp_line *line;
393 int i;
394
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200395 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200396 if (line)
397 return line;
398
399 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
400 if (!line)
401 return NULL;
402
403 line->num = e1_nr;
404 for (i = 0; i < NUM_E1_TS; i++) {
405 line->ts[i].num = i+1;
406 line->ts[i].line = line;
407 }
408 llist_add_tail(&line->list, &e1inp_line_list);
409
410 return line;
411}
412#endif
413
414static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr)
415{
416 struct e1inp_line *e1i_line;
417
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200418 e1i_line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200419 if (!e1i_line)
420 return NULL;
421
422 return &e1i_line->ts[ts_nr-1];
423}
424
425struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr)
426{
427 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
428
429 if (!e1i_ts)
430 return NULL;
431
432 return &e1i_ts->trau.mux;
433}
434
435/* Signalling Link */
436
437struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
438 uint8_t tei, uint8_t sapi)
439{
440 struct e1inp_sign_link *link;
441
442 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
443 if (link->sapi == sapi && link->tei == tei)
444 return link;
445 }
446
447 return NULL;
448}
449
450/* create a new signalling link in a E1 timeslot */
451
452struct e1inp_sign_link *
453e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
454 struct gsm_bts_trx *trx, uint8_t tei,
455 uint8_t sapi)
456{
457 struct e1inp_sign_link *link;
458
459 if (ts->type != E1INP_TS_TYPE_SIGN)
460 return NULL;
461
462 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
463 if (!link)
464 return NULL;
465
466 link->ts = ts;
467 link->type = type;
468 INIT_LLIST_HEAD(&link->tx_list);
469 link->trx = trx;
470 link->tei = tei;
471 link->sapi = sapi;
472
473 llist_add_tail(&link->list, &ts->sign.sign_links);
474
475 return link;
476}
477
478void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
479{
480 struct msgb *msg;
481
482 llist_del(&link->list);
483 while (!llist_empty(&link->tx_list)) {
484 msg = msgb_dequeue(&link->tx_list);
485 msgb_free(msg);
486 }
487
488 if (link->ts->type == E1INP_TS_TYPE_SIGN)
489 osmo_timer_del(&link->ts->sign.tx_timer);
490
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200491 if (link->ts->line->driver->close)
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200492 link->ts->line->driver->close(link);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200493
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200494 e1inp_line_put(link->ts->line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200495 talloc_free(link);
496}
497
498/* XXX */
499/* the E1 driver tells us he has received something on a TS */
500int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
501 uint8_t tei, uint8_t sapi)
502{
503 struct e1inp_sign_link *link;
504 int ret = 0;
505
506 switch (ts->type) {
507 case E1INP_TS_TYPE_SIGN:
Harald Welte7f9d8512016-07-04 09:59:46 +0200508 /* we only need to write a 'Fake LAPD' packet here, if
509 * the underlying driver hides LAPD from us. If we use
510 * the libosmocore LAPD implementation, it will take
511 * care of writing the _actual_ LAPD packet */
512 if (!ts->lapd)
513 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200514 /* consult the list of signalling links */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200515 link = e1inp_lookup_sign_link(ts, tei, sapi);
516 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200517 LOGP(DLMI, LOGL_ERROR, "didn't find signalling link for "
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200518 "tei %d, sapi %d\n", tei, sapi);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200519 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200520 return -EINVAL;
521 }
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200522 if (!ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200523 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200524 "no action set for signalling messages.\n");
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200525 msgb_free(msg);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200526 return -ENOENT;
527 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200528 msg->dst = link;
529 ts->line->ops->sign_link(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200530 break;
531 case E1INP_TS_TYPE_TRAU:
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200532 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200533 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200534 break;
535 default:
536 ret = -EINVAL;
Harald Weltecc2241b2011-07-19 16:06:06 +0200537 LOGP(DLMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200538 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200539 break;
540 }
541
542 return ret;
543}
544
Harald Weltefd44a5f2011-08-21 00:48:54 +0200545/*! \brief Receive some data from the L1/HDLC into LAPD of a timeslot
546 * \param[in] e1i_ts E1 Timeslot data structure
547 * \param[in] msg Message buffer containing full LAPD message
548 *
549 * This is a wrapper around e1inp_rx_ts(), but feeding the incoming
550 * message first into our LAPD code. This allows a driver to read raw
551 * (HDLC decoded) data from the timeslot, instead of a LAPD stack
552 * present in any underlying driver.
553 */
554int e1inp_rx_ts_lapd(struct e1inp_ts *e1i_ts, struct msgb *msg)
555{
Harald Weltefd44a5f2011-08-21 00:48:54 +0200556 unsigned int sapi, tei;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200557 int ret = 0, error = 0;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200558
559 sapi = msg->data[0] >> 2;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200560 if ((msg->data[0] & 0x1))
561 tei = 0;
562 else
563 tei = msg->data[1] >> 1;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200564
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200565 DEBUGP(DLMI, "<= len = %d, sapi(%d) tei(%d)\n", msg->len, sapi, tei);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200566
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200567 ret = lapd_receive(e1i_ts->lapd, msg, &error);
568 if (ret < 0) {
Harald Weltefd44a5f2011-08-21 00:48:54 +0200569 switch(error) {
570 case LAPD_ERR_UNKNOWN_TEI:
571 /* We don't know about this TEI, probably the BSC
572 * lost local states (it crashed or it was stopped),
573 * notify the driver to see if it can do anything to
574 * recover the existing signalling links with the BTS.
575 */
576 e1inp_event(e1i_ts, S_L_INP_TEI_UNKNOWN, tei, sapi);
577 return -EIO;
578 }
Harald Weltefd44a5f2011-08-21 00:48:54 +0200579 }
580
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200581 return 0;
582}
Harald Weltefd44a5f2011-08-21 00:48:54 +0200583
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200584void e1inp_dlsap_up(struct osmo_dlsap_prim *dp, uint8_t tei, uint8_t sapi,
585 void *rx_cbdata)
586{
587 struct e1inp_ts *e1i_ts = rx_cbdata;
588 struct msgb *msg = dp->oph.msg;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200589
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200590 switch (dp->oph.primitive) {
591 case PRIM_DL_EST:
592 DEBUGP(DLMI, "DL_EST: sapi(%d) tei(%d)\n", sapi, tei);
593 e1inp_event(e1i_ts, S_L_INP_TEI_UP, tei, sapi);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200594 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200595 case PRIM_DL_REL:
596 DEBUGP(DLMI, "DL_REL: sapi(%d) tei(%d)\n", sapi, tei);
597 e1inp_event(e1i_ts, S_L_INP_TEI_DN, tei, sapi);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200598 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200599 case PRIM_DL_DATA:
600 case PRIM_DL_UNIT_DATA:
601 if (dp->oph.operation == PRIM_OP_INDICATION) {
602 msg->l2h = msg->l3h;
603 DEBUGP(DLMI, "RX: %s sapi=%d tei=%d\n",
604 osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)),
605 sapi, tei);
606 e1inp_rx_ts(e1i_ts, msg, tei, sapi);
607 return;
608 }
Harald Weltefd44a5f2011-08-21 00:48:54 +0200609 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200610 case PRIM_MDL_ERROR:
611 DEBUGP(DLMI, "MDL_EERROR: cause(%d)\n", dp->u.error_ind.cause);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200612 break;
613 default:
614 printf("ERROR: unknown prim\n");
615 break;
616 }
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200617
618 msgb_free(msg);
619
620 return;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200621}
622
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200623#define TSX_ALLOC_SIZE 4096
624
625/* called by driver if it wants to transmit on a given TS */
626struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
627 struct e1inp_sign_link **sign_link)
628{
629 struct e1inp_sign_link *link;
630 struct msgb *msg = NULL;
631 int len;
632
633 switch (e1i_ts->type) {
634 case E1INP_TS_TYPE_SIGN:
635 /* FIXME: implement this round robin */
636 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
637 msg = msgb_dequeue(&link->tx_list);
638 if (msg) {
639 if (sign_link)
640 *sign_link = link;
641 break;
642 }
643 }
644 break;
645 case E1INP_TS_TYPE_TRAU:
646 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
647 if (!msg)
648 return NULL;
649 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
Pablo Neira Ayuso33f71752013-07-05 15:44:12 +0200650 if (len != 40) {
651 LOGP(DLMI, LOGL_ERROR,
652 "cannot transmit, failed to mux\n");
653 msgb_free(msg);
654 return NULL;
655 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200656 msgb_put(msg, 40);
657 break;
658 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200659 LOGP(DLMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200660 return NULL;
661 }
662 return msg;
663}
664
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100665static int e1inp_int_snd_event(struct e1inp_ts *ts,
666 struct e1inp_sign_link *link, int evt)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200667{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200668 struct input_signal_data isd;
Pablo Neira Ayuso31fe5f22011-08-09 23:15:38 +0200669 isd.line = ts->line;
Harald Weltef35d8892016-10-16 15:33:52 +0200670 isd.ts_nr = ts->num;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200671 isd.link_type = link->type;
672 isd.trx = link->trx;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100673 isd.tei = link->tei;
674 isd.sapi = link->sapi;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200675
676 /* report further upwards */
Harald Weltecc2241b2011-07-19 16:06:06 +0200677 osmo_signal_dispatch(SS_L_INPUT, evt, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200678 return 0;
679}
680
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100681
682/* called by driver in case some kind of link state event */
683int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi)
684{
685 struct e1inp_sign_link *link;
686
687 link = e1inp_lookup_sign_link(ts, tei, sapi);
688 if (!link)
689 return -EINVAL;
690 return e1inp_int_snd_event(ts, link, evt);
691}
692
693void e1inp_close_socket(struct e1inp_ts *ts,
694 struct e1inp_sign_link *sign_link,
695 struct osmo_fd *bfd)
696{
697 e1inp_int_snd_event(ts, sign_link, S_L_INP_TEI_DN);
698 /* the first e1inp_sign_link_destroy call closes the socket. */
699 if (bfd->fd != -1) {
700 osmo_fd_unregister(bfd);
701 close(bfd->fd);
702 bfd->fd = -1;
703 }
704}
705
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200706/* register a driver with the E1 core */
707int e1inp_driver_register(struct e1inp_driver *drv)
708{
709 llist_add_tail(&drv->list, &e1inp_driver_list);
710 return 0;
711}
712
713struct e1inp_driver *e1inp_driver_find(const char *name)
714{
715 struct e1inp_driver *drv;
716
717 llist_for_each_entry(drv, &e1inp_driver_list, list) {
718 if (!strcasecmp(name, drv->name))
719 return drv;
720 }
721 return NULL;
722}
723
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200724int e1inp_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200725{
726 struct input_signal_data isd;
Harald Welte7f9d8512016-07-04 09:59:46 +0200727 int i, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200728
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200729 e1inp_line_get(line);
730
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200731 if (line->driver && line->ops && line->driver->line_update) {
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200732 rc = line->driver->line_update(line);
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200733 } else
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200734 rc = 0;
735
Harald Welte7f9d8512016-07-04 09:59:46 +0200736 /* Set the PCAP file descriptor for all timeslots that have
737 * software LAPD instances, to ensure the osmo_lapd_pcap code is
738 * used to write PCAP files (if requested) */
739 for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
740 struct e1inp_ts *e1i_ts = &line->ts[i];
741 if (e1i_ts->lapd)
742 e1i_ts->lapd->pcap_fd = pcap_fd;
743 }
744
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200745 /* Send a signal to anyone who is interested in new lines being
746 * configured */
747 memset(&isd, 0, sizeof(isd));
748 isd.line = line;
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200749 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_INIT, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200750
751 return rc;
752}
753
Pablo Neira Ayusoa20762a2011-07-02 19:01:58 +0200754static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200755 void *handler_data, void *signal_data)
756{
Harald Weltecc2241b2011-07-19 16:06:06 +0200757 if (subsys != SS_L_GLOBAL ||
758 signal != S_L_GLOBAL_SHUTDOWN)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200759 return 0;
760
761 if (pcap_fd) {
762 close(pcap_fd);
763 pcap_fd = -1;
764 }
765
766 return 0;
767}
768
769void e1inp_misdn_init(void);
770void e1inp_dahdi_init(void);
771void e1inp_ipaccess_init(void);
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200772void e1inp_rs232_init(void);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200773
774void e1inp_init(void)
775{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200776 tall_e1inp_ctx = talloc_named_const(libosmo_abis_ctx, 1, "e1inp");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200777 tall_sigl_ctx = talloc_named_const(tall_e1inp_ctx, 1,
778 "e1inp_sign_link");
Harald Weltecc2241b2011-07-19 16:06:06 +0200779 osmo_signal_register_handler(SS_L_GLOBAL, e1i_sig_cb, NULL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200780
781 e1inp_misdn_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200782#ifdef HAVE_DAHDI_USER_H
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200783 e1inp_dahdi_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200784#endif
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200785 e1inp_ipaccess_init();
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200786 e1inp_rs232_init();
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200787}