blob: 09fea59138c526637281c162957e30019f28fd7d [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>
Holger Hans Peter Freyther25474582017-01-23 19:49:07 +010031#include <fcntl.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020032#include <sys/socket.h>
33#include <sys/ioctl.h>
34#include <arpa/inet.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020035
Harald Weltefd44a5f2011-08-21 00:48:54 +020036#include <osmocom/abis/lapd.h>
37
Harald Weltef2737fc2011-08-16 14:30:10 +020038#include <osmocom/core/linuxlist.h>
39#include <osmocom/core/talloc.h>
40#include <osmocom/core/rate_ctr.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020041#include <osmocom/core/logging.h>
42#include <osmocom/core/signal.h>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020043#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020044
45#define NUM_E1_TS 32
46
47static void *tall_e1inp_ctx;
48
49/* list of all E1 drivers */
50LLIST_HEAD(e1inp_driver_list);
51
52/* list of all E1 lines */
53LLIST_HEAD(e1inp_line_list);
54
55static void *tall_sigl_ctx;
56
Harald Weltef2737fc2011-08-16 14:30:10 +020057static const struct rate_ctr_desc e1inp_ctr_d[] = {
58 [E1I_CTR_HDLC_ABORT] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020059 "hdlc.abort", "HDLC abort"
Harald Weltef2737fc2011-08-16 14:30:10 +020060 },
61 [E1I_CTR_HDLC_BADFCS] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020062 "hdlc.bad_fcs", "HLDC Bad FCS"
Harald Weltef2737fc2011-08-16 14:30:10 +020063 },
64 [E1I_CTR_HDLC_OVERR] = {
65 "hdlc.overrun", "HDLC Overrun"
66 },
67 [E1I_CTR_ALARM] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020068 "alarm", "Alarm"
Harald Weltef2737fc2011-08-16 14:30:10 +020069 },
70 [E1I_CTR_REMOVED] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020071 "removed", "Line removed"
Harald Weltef2737fc2011-08-16 14:30:10 +020072 },
73};
74
75static const struct rate_ctr_group_desc e1inp_ctr_g_d = {
76 .group_name_prefix = "e1inp",
77 .group_description = "E1 Input subsystem",
78 .num_ctr = ARRAY_SIZE(e1inp_ctr_d),
79 .ctr_desc = e1inp_ctr_d,
80};
81
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020082/*
83 * pcap writing of the misdn load
84 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
85 */
86#define DLT_LINUX_LAPD 177
87#define PCAP_INPUT 0
88#define PCAP_OUTPUT 1
89
90struct pcap_hdr {
91 uint32_t magic_number;
92 uint16_t version_major;
93 uint16_t version_minor;
94 int32_t thiszone;
95 uint32_t sigfigs;
96 uint32_t snaplen;
97 uint32_t network;
98} __attribute__((packed));
99
100struct pcaprec_hdr {
101 uint32_t ts_sec;
102 uint32_t ts_usec;
103 uint32_t incl_len;
104 uint32_t orig_len;
105} __attribute__((packed));
106
107struct fake_linux_lapd_header {
108 uint16_t pkttype;
109 uint16_t hatype;
110 uint16_t halen;
111 uint64_t addr;
112 int16_t protocol;
113} __attribute__((packed));
114
115struct lapd_header {
116 uint8_t ea1 : 1;
117 uint8_t cr : 1;
118 uint8_t sapi : 6;
119 uint8_t ea2 : 1;
120 uint8_t tei : 7;
121 uint8_t control_foo; /* fake UM's ... */
122} __attribute__((packed));
123
124osmo_static_assert(offsetof(struct fake_linux_lapd_header, hatype) == 2, hatype_offset);
125osmo_static_assert(offsetof(struct fake_linux_lapd_header, halen) == 4, halen_offset);
126osmo_static_assert(offsetof(struct fake_linux_lapd_header, addr) == 6, addr_offset);
127osmo_static_assert(offsetof(struct fake_linux_lapd_header, protocol) == 14, proto_offset);
128osmo_static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
129
130
131static int pcap_fd = -1;
132
Pablo Neira Ayuso2e11f5c2013-07-05 15:08:18 +0200133int e1_set_pcap_fd(int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200134{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200135 struct pcap_hdr header = {
136 .magic_number = 0xa1b2c3d4,
137 .version_major = 2,
138 .version_minor = 4,
139 .thiszone = 0,
140 .sigfigs = 0,
141 .snaplen = 65535,
142 .network = DLT_LINUX_LAPD,
143 };
144
145 pcap_fd = fd;
Pablo Neira Ayuso2e11f5c2013-07-05 15:08:18 +0200146 return write(pcap_fd, &header, sizeof(header));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200147}
148
149/* This currently only works for the D-Channel */
150static void write_pcap_packet(int direction, int sapi, int tei,
151 struct msgb *msg) {
152 if (pcap_fd < 0)
153 return;
154
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200155 time_t cur_time;
156 struct tm *tm;
157
158 struct fake_linux_lapd_header header = {
159 .pkttype = 4,
160 .hatype = 0,
161 .halen = 0,
162 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
163 .protocol = ntohs(48),
164 };
165
166 struct lapd_header lapd_header = {
167 .ea1 = 0,
168 .cr = direction == PCAP_OUTPUT ? 1 : 0,
169 .sapi = sapi & 0x3F,
170 .ea2 = 1,
171 .tei = tei & 0x7F,
172 .control_foo = 0x03 /* UI */,
173 };
174
175 struct pcaprec_hdr payload_header = {
176 .ts_sec = 0,
177 .ts_usec = 0,
178 .incl_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
179 + sizeof(struct lapd_header),
180 .orig_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
181 + sizeof(struct lapd_header),
182 };
183
184
185 cur_time = time(NULL);
186 tm = localtime(&cur_time);
187 payload_header.ts_sec = mktime(tm);
188
Harald Weltec9295ea2014-08-21 02:41:41 +0200189 write(pcap_fd, &payload_header, sizeof(payload_header));
190 write(pcap_fd, &header, sizeof(header));
191 write(pcap_fd, &lapd_header, sizeof(lapd_header));
192 write(pcap_fd, msg->l2h, msgb_l2len(msg));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200193}
194
Harald Welte4ca5c532016-07-27 23:05:03 +0200195const struct value_string e1inp_sign_type_names[5] = {
196 { E1INP_SIGN_NONE, "None" },
197 { E1INP_SIGN_OML, "OML" },
198 { E1INP_SIGN_RSL, "RSL" },
199 { E1INP_SIGN_OSMO, "OSMO" },
200 { 0, NULL }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200201};
Harald Welte4ca5c532016-07-27 23:05:03 +0200202
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200203const char *e1inp_signtype_name(enum e1inp_sign_type tp)
204{
Harald Welte4ca5c532016-07-27 23:05:03 +0200205 return get_value_string(e1inp_sign_type_names, tp);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200206}
207
Harald Welte7a228eb2016-07-28 11:09:31 +0200208const struct value_string e1inp_ts_type_names[6] = {
Harald Welte4ca5c532016-07-27 23:05:03 +0200209 { E1INP_TS_TYPE_NONE, "None" },
210 { E1INP_TS_TYPE_SIGN, "Signalling" },
211 { E1INP_TS_TYPE_TRAU, "TRAU" },
Harald Weltea0108e72016-07-27 21:44:50 +0200212 { E1INP_TS_TYPE_RAW, "RAW" },
Harald Welte7a228eb2016-07-28 11:09:31 +0200213 { E1INP_TS_TYPE_HDLC, "HDLC" },
Harald Welte4ca5c532016-07-27 23:05:03 +0200214 { 0, NULL }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200215};
216
217const char *e1inp_tstype_name(enum e1inp_ts_type tp)
218{
Harald Welte4ca5c532016-07-27 23:05:03 +0200219 return get_value_string(e1inp_ts_type_names, tp);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200220}
221
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200222int abis_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200223{
224 struct e1inp_sign_link *sign_link = msg->dst;
225 struct e1inp_driver *e1inp_driver;
226 struct e1inp_ts *e1i_ts;
Alexander Couzens35daa672016-11-08 16:17:20 +0100227
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200228 msg->l2h = msg->data;
229
230 /* don't know how to route this message. */
231 if (sign_link == NULL) {
Harald Welte40b0e8c2011-07-21 16:57:34 +0200232 LOGP(DLINP, LOGL_ERROR, "abis_sendmsg: msg->dst == NULL: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200233 osmo_hexdump(msg->data, msg->len));
234 talloc_free(msg);
235 return -EINVAL;
236 }
237 e1i_ts = sign_link->ts;
238 if (!osmo_timer_pending(&e1i_ts->sign.tx_timer)) {
239 /* notify the driver we have something to write */
240 e1inp_driver = sign_link->ts->line->driver;
241 e1inp_driver->want_write(e1i_ts);
242 }
243 msgb_enqueue(&sign_link->tx_list, msg);
244
Harald Welte7f9d8512016-07-04 09:59:46 +0200245 /* we only need to write a 'Fake LAPD' packet here, if the
246 * underlying driver hides LAPD from us. If we use the
247 * libosmocore LAPD implementation, it will take care of writing
248 * the _actual_ LAPD packet */
249 if (!e1i_ts->lapd) {
250 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi,
251 sign_link->tei, msg);
252 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200253
254 return 0;
255}
256
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200257int abis_rsl_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200258{
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200259 return abis_sendmsg(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200260}
261
262/* Timeslot */
263int e1inp_ts_config_trau(struct e1inp_ts *ts, struct e1inp_line *line,
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200264 int (*trau_rcv_cb)(struct subch_demux *dmx, int ch,
265 uint8_t *data, int len, void *_priv))
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200266{
267 if (ts->type == E1INP_TS_TYPE_TRAU && ts->line && line)
268 return 0;
269
270 ts->type = E1INP_TS_TYPE_TRAU;
271 ts->line = line;
272
273 subchan_mux_init(&ts->trau.mux);
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200274 ts->trau.demux.out_cb = trau_rcv_cb;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200275 ts->trau.demux.data = ts;
276 subch_demux_init(&ts->trau.demux);
277 return 0;
278}
279
280int e1inp_ts_config_sign(struct e1inp_ts *ts, struct e1inp_line *line)
281{
282 if (ts->type == E1INP_TS_TYPE_SIGN && ts->line && line)
283 return 0;
284
285 ts->type = E1INP_TS_TYPE_SIGN;
286 ts->line = line;
287
288 if (line && line->driver)
289 ts->sign.delay = line->driver->default_delay;
290 else
291 ts->sign.delay = 100000;
292 INIT_LLIST_HEAD(&ts->sign.sign_links);
293 return 0;
294}
295
Harald Weltea0108e72016-07-27 21:44:50 +0200296int e1inp_ts_config_raw(struct e1inp_ts *ts, struct e1inp_line *line,
297 void (*raw_recv_cb)(struct e1inp_ts *ts,
298 struct msgb *msg))
299{
300 if (ts->type == E1INP_TS_TYPE_RAW && ts->line && line)
301 return 0;
302
303 ts->type = E1INP_TS_TYPE_RAW;
304 ts->line = line;
305 ts->raw.recv_cb = raw_recv_cb;
306 INIT_LLIST_HEAD(&ts->raw.tx_queue);
307
308 return 0;
309}
310
Harald Welte7a228eb2016-07-28 11:09:31 +0200311int e1inp_ts_config_hdlc(struct e1inp_ts *ts, struct e1inp_line *line,
312 void (*hdlc_recv_cb)(struct e1inp_ts *ts,
313 struct msgb *msg))
314{
315 if (ts->type == E1INP_TS_TYPE_HDLC && ts->line && line)
316 return 0;
317
318 ts->type = E1INP_TS_TYPE_HDLC;
319 ts->line = line;
320 ts->hdlc.recv_cb = hdlc_recv_cb;
321 INIT_LLIST_HEAD(&ts->hdlc.tx_queue);
322
323 return 0;
324}
325
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200326struct e1inp_line *e1inp_line_find(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200327{
328 struct e1inp_line *e1i_line;
329
330 /* iterate over global list of e1 lines */
331 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
332 if (e1i_line->num == e1_nr)
333 return e1i_line;
334 }
335 return NULL;
336}
337
338struct e1inp_line *
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200339e1inp_line_create(uint8_t e1_nr, const char *driver_name)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200340{
341 struct e1inp_driver *driver;
342 struct e1inp_line *line;
343 int i;
344
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200345 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200346 if (line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200347 LOGP(DLINP, LOGL_ERROR, "E1 Line %u already exists\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200348 e1_nr);
349 return NULL;
350 }
351
352 driver = e1inp_driver_find(driver_name);
353 if (!driver) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200354 LOGP(DLINP, LOGL_ERROR, "No such E1 driver '%s'\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200355 driver_name);
356 return NULL;
357 }
358
359 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
360 if (!line)
361 return NULL;
362
363 line->driver = driver;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200364 line->num = e1_nr;
Harald Weltef2737fc2011-08-16 14:30:10 +0200365
366 line->rate_ctr = rate_ctr_group_alloc(line, &e1inp_ctr_g_d, line->num);
367
Harald Weltec2889512011-09-13 23:49:04 +0100368 line->num_ts = NUM_E1_TS;
369 for (i = 0; i < line->num_ts; i++) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200370 line->ts[i].num = i+1;
371 line->ts[i].line = line;
372 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200373 line->refcnt++;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200374 llist_add_tail(&line->list, &e1inp_line_list);
375
376 return line;
377}
378
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200379struct e1inp_line *
380e1inp_line_clone(void *ctx, struct e1inp_line *line)
381{
382 struct e1inp_line *clone;
383
384 /* clone virtual E1 line for this new OML link. */
385 clone = talloc_zero(ctx, struct e1inp_line);
386 if (clone == NULL)
387 return NULL;
388
389 memcpy(clone, line, sizeof(struct e1inp_line));
390 clone->refcnt = 1;
391 return clone;
392}
393
394void e1inp_line_get(struct e1inp_line *line)
395{
396 line->refcnt++;
397}
398
399void e1inp_line_put(struct e1inp_line *line)
400{
401 line->refcnt--;
402 if (line->refcnt == 0)
403 talloc_free(line);
404}
405
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200406void
407e1inp_line_bind_ops(struct e1inp_line *line, const struct e1inp_line_ops *ops)
408{
409 line->ops = ops;
410}
411
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200412#if 0
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200413struct e1inp_line *e1inp_line_find_create(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200414{
415 struct e1inp_line *line;
416 int i;
417
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200418 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200419 if (line)
420 return line;
421
422 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
423 if (!line)
424 return NULL;
425
426 line->num = e1_nr;
427 for (i = 0; i < NUM_E1_TS; i++) {
428 line->ts[i].num = i+1;
429 line->ts[i].line = line;
430 }
431 llist_add_tail(&line->list, &e1inp_line_list);
432
433 return line;
434}
435#endif
436
437static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr)
438{
439 struct e1inp_line *e1i_line;
440
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200441 e1i_line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200442 if (!e1i_line)
443 return NULL;
444
445 return &e1i_line->ts[ts_nr-1];
446}
447
448struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr)
449{
450 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
451
452 if (!e1i_ts)
453 return NULL;
454
455 return &e1i_ts->trau.mux;
456}
457
458/* Signalling Link */
459
460struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
461 uint8_t tei, uint8_t sapi)
462{
463 struct e1inp_sign_link *link;
464
465 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
466 if (link->sapi == sapi && link->tei == tei)
467 return link;
468 }
469
470 return NULL;
471}
472
473/* create a new signalling link in a E1 timeslot */
474
475struct e1inp_sign_link *
476e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
477 struct gsm_bts_trx *trx, uint8_t tei,
478 uint8_t sapi)
479{
480 struct e1inp_sign_link *link;
481
482 if (ts->type != E1INP_TS_TYPE_SIGN)
483 return NULL;
484
485 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
486 if (!link)
487 return NULL;
488
489 link->ts = ts;
490 link->type = type;
491 INIT_LLIST_HEAD(&link->tx_list);
492 link->trx = trx;
493 link->tei = tei;
494 link->sapi = sapi;
495
496 llist_add_tail(&link->list, &ts->sign.sign_links);
497
498 return link;
499}
500
501void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
502{
503 struct msgb *msg;
504
505 llist_del(&link->list);
506 while (!llist_empty(&link->tx_list)) {
507 msg = msgb_dequeue(&link->tx_list);
508 msgb_free(msg);
509 }
510
511 if (link->ts->type == E1INP_TS_TYPE_SIGN)
512 osmo_timer_del(&link->ts->sign.tx_timer);
513
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200514 if (link->ts->line->driver->close)
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200515 link->ts->line->driver->close(link);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200516
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200517 e1inp_line_put(link->ts->line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200518 talloc_free(link);
519}
520
521/* XXX */
522/* the E1 driver tells us he has received something on a TS */
523int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
524 uint8_t tei, uint8_t sapi)
525{
526 struct e1inp_sign_link *link;
527 int ret = 0;
528
529 switch (ts->type) {
530 case E1INP_TS_TYPE_SIGN:
Harald Welte7f9d8512016-07-04 09:59:46 +0200531 /* we only need to write a 'Fake LAPD' packet here, if
532 * the underlying driver hides LAPD from us. If we use
533 * the libosmocore LAPD implementation, it will take
534 * care of writing the _actual_ LAPD packet */
535 if (!ts->lapd)
536 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200537 /* consult the list of signalling links */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200538 link = e1inp_lookup_sign_link(ts, tei, sapi);
539 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200540 LOGP(DLMI, LOGL_ERROR, "didn't find signalling link for "
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200541 "tei %d, sapi %d\n", tei, sapi);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200542 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200543 return -EINVAL;
544 }
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200545 if (!ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200546 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200547 "no action set for signalling messages.\n");
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200548 msgb_free(msg);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200549 return -ENOENT;
550 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200551 msg->dst = link;
552 ts->line->ops->sign_link(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200553 break;
554 case E1INP_TS_TYPE_TRAU:
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200555 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200556 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200557 break;
Harald Weltea0108e72016-07-27 21:44:50 +0200558 case E1INP_TS_TYPE_RAW:
559 ts->raw.recv_cb(ts, msg);
560 break;
Harald Welte7a228eb2016-07-28 11:09:31 +0200561 case E1INP_TS_TYPE_HDLC:
562 ts->hdlc.recv_cb(ts, msg);
563 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200564 default:
565 ret = -EINVAL;
Harald Weltecc2241b2011-07-19 16:06:06 +0200566 LOGP(DLMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200567 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200568 break;
569 }
570
571 return ret;
572}
573
Harald Weltefd44a5f2011-08-21 00:48:54 +0200574/*! \brief Receive some data from the L1/HDLC into LAPD of a timeslot
575 * \param[in] e1i_ts E1 Timeslot data structure
576 * \param[in] msg Message buffer containing full LAPD message
577 *
578 * This is a wrapper around e1inp_rx_ts(), but feeding the incoming
579 * message first into our LAPD code. This allows a driver to read raw
580 * (HDLC decoded) data from the timeslot, instead of a LAPD stack
581 * present in any underlying driver.
582 */
583int e1inp_rx_ts_lapd(struct e1inp_ts *e1i_ts, struct msgb *msg)
584{
Harald Weltefd44a5f2011-08-21 00:48:54 +0200585 unsigned int sapi, tei;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200586 int ret = 0, error = 0;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200587
588 sapi = msg->data[0] >> 2;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200589 if ((msg->data[0] & 0x1))
590 tei = 0;
591 else
592 tei = msg->data[1] >> 1;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200593
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200594 DEBUGP(DLMI, "<= len = %d, sapi(%d) tei(%d)\n", msg->len, sapi, tei);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200595
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200596 ret = lapd_receive(e1i_ts->lapd, msg, &error);
597 if (ret < 0) {
Harald Weltefd44a5f2011-08-21 00:48:54 +0200598 switch(error) {
599 case LAPD_ERR_UNKNOWN_TEI:
600 /* We don't know about this TEI, probably the BSC
601 * lost local states (it crashed or it was stopped),
602 * notify the driver to see if it can do anything to
603 * recover the existing signalling links with the BTS.
604 */
605 e1inp_event(e1i_ts, S_L_INP_TEI_UNKNOWN, tei, sapi);
606 return -EIO;
607 }
Harald Weltefd44a5f2011-08-21 00:48:54 +0200608 }
609
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200610 return 0;
611}
Harald Weltefd44a5f2011-08-21 00:48:54 +0200612
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200613void e1inp_dlsap_up(struct osmo_dlsap_prim *dp, uint8_t tei, uint8_t sapi,
614 void *rx_cbdata)
615{
616 struct e1inp_ts *e1i_ts = rx_cbdata;
617 struct msgb *msg = dp->oph.msg;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200618
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200619 switch (dp->oph.primitive) {
620 case PRIM_DL_EST:
621 DEBUGP(DLMI, "DL_EST: sapi(%d) tei(%d)\n", sapi, tei);
622 e1inp_event(e1i_ts, S_L_INP_TEI_UP, tei, sapi);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200623 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200624 case PRIM_DL_REL:
625 DEBUGP(DLMI, "DL_REL: sapi(%d) tei(%d)\n", sapi, tei);
626 e1inp_event(e1i_ts, S_L_INP_TEI_DN, tei, sapi);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200627 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200628 case PRIM_DL_DATA:
629 case PRIM_DL_UNIT_DATA:
630 if (dp->oph.operation == PRIM_OP_INDICATION) {
631 msg->l2h = msg->l3h;
632 DEBUGP(DLMI, "RX: %s sapi=%d tei=%d\n",
633 osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)),
634 sapi, tei);
635 e1inp_rx_ts(e1i_ts, msg, tei, sapi);
636 return;
637 }
Harald Weltefd44a5f2011-08-21 00:48:54 +0200638 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200639 case PRIM_MDL_ERROR:
640 DEBUGP(DLMI, "MDL_EERROR: cause(%d)\n", dp->u.error_ind.cause);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200641 break;
642 default:
643 printf("ERROR: unknown prim\n");
644 break;
645 }
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200646
647 msgb_free(msg);
648
649 return;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200650}
651
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200652#define TSX_ALLOC_SIZE 4096
653
654/* called by driver if it wants to transmit on a given TS */
655struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
656 struct e1inp_sign_link **sign_link)
657{
658 struct e1inp_sign_link *link;
659 struct msgb *msg = NULL;
660 int len;
661
662 switch (e1i_ts->type) {
663 case E1INP_TS_TYPE_SIGN:
664 /* FIXME: implement this round robin */
665 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
666 msg = msgb_dequeue(&link->tx_list);
667 if (msg) {
668 if (sign_link)
669 *sign_link = link;
670 break;
671 }
672 }
673 break;
674 case E1INP_TS_TYPE_TRAU:
675 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
676 if (!msg)
677 return NULL;
678 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
Pablo Neira Ayuso33f71752013-07-05 15:44:12 +0200679 if (len != 40) {
680 LOGP(DLMI, LOGL_ERROR,
681 "cannot transmit, failed to mux\n");
682 msgb_free(msg);
683 return NULL;
684 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200685 msgb_put(msg, 40);
686 break;
Harald Weltea0108e72016-07-27 21:44:50 +0200687 case E1INP_TS_TYPE_RAW:
688 /* Get msgb from tx_queue */
689 msg = msgb_dequeue(&e1i_ts->raw.tx_queue);
690 break;
Harald Welte7a228eb2016-07-28 11:09:31 +0200691 case E1INP_TS_TYPE_HDLC:
692 /* Get msgb from tx_queue */
693 msg = msgb_dequeue(&e1i_ts->hdlc.tx_queue);
694 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200695 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200696 LOGP(DLMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200697 return NULL;
698 }
699 return msg;
700}
701
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100702static int e1inp_int_snd_event(struct e1inp_ts *ts,
703 struct e1inp_sign_link *link, int evt)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200704{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200705 struct input_signal_data isd;
Pablo Neira Ayuso31fe5f22011-08-09 23:15:38 +0200706 isd.line = ts->line;
Harald Weltef35d8892016-10-16 15:33:52 +0200707 isd.ts_nr = ts->num;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200708 isd.link_type = link->type;
709 isd.trx = link->trx;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100710 isd.tei = link->tei;
711 isd.sapi = link->sapi;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200712
713 /* report further upwards */
Harald Weltecc2241b2011-07-19 16:06:06 +0200714 osmo_signal_dispatch(SS_L_INPUT, evt, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200715 return 0;
716}
717
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100718
719/* called by driver in case some kind of link state event */
720int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi)
721{
722 struct e1inp_sign_link *link;
723
724 link = e1inp_lookup_sign_link(ts, tei, sapi);
725 if (!link)
726 return -EINVAL;
727 return e1inp_int_snd_event(ts, link, evt);
728}
729
730void e1inp_close_socket(struct e1inp_ts *ts,
731 struct e1inp_sign_link *sign_link,
732 struct osmo_fd *bfd)
733{
734 e1inp_int_snd_event(ts, sign_link, S_L_INP_TEI_DN);
735 /* the first e1inp_sign_link_destroy call closes the socket. */
736 if (bfd->fd != -1) {
737 osmo_fd_unregister(bfd);
738 close(bfd->fd);
739 bfd->fd = -1;
740 }
741}
742
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200743/* register a driver with the E1 core */
744int e1inp_driver_register(struct e1inp_driver *drv)
745{
746 llist_add_tail(&drv->list, &e1inp_driver_list);
747 return 0;
748}
749
750struct e1inp_driver *e1inp_driver_find(const char *name)
751{
752 struct e1inp_driver *drv;
753
754 llist_for_each_entry(drv, &e1inp_driver_list, list) {
755 if (!strcasecmp(name, drv->name))
756 return drv;
757 }
758 return NULL;
759}
760
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200761int e1inp_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200762{
763 struct input_signal_data isd;
Harald Welte7f9d8512016-07-04 09:59:46 +0200764 int i, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200765
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200766 e1inp_line_get(line);
767
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200768 if (line->driver && line->ops && line->driver->line_update) {
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200769 rc = line->driver->line_update(line);
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200770 } else
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200771 rc = 0;
772
Harald Welte7f9d8512016-07-04 09:59:46 +0200773 /* Set the PCAP file descriptor for all timeslots that have
774 * software LAPD instances, to ensure the osmo_lapd_pcap code is
775 * used to write PCAP files (if requested) */
776 for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
777 struct e1inp_ts *e1i_ts = &line->ts[i];
778 if (e1i_ts->lapd)
779 e1i_ts->lapd->pcap_fd = pcap_fd;
780 }
781
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200782 /* Send a signal to anyone who is interested in new lines being
783 * configured */
784 memset(&isd, 0, sizeof(isd));
785 isd.line = line;
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200786 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_INIT, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200787
788 return rc;
789}
790
Pablo Neira Ayusoa20762a2011-07-02 19:01:58 +0200791static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200792 void *handler_data, void *signal_data)
793{
Harald Weltecc2241b2011-07-19 16:06:06 +0200794 if (subsys != SS_L_GLOBAL ||
795 signal != S_L_GLOBAL_SHUTDOWN)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200796 return 0;
797
798 if (pcap_fd) {
799 close(pcap_fd);
800 pcap_fd = -1;
801 }
802
803 return 0;
804}
805
806void e1inp_misdn_init(void);
807void e1inp_dahdi_init(void);
808void e1inp_ipaccess_init(void);
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200809void e1inp_rs232_init(void);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200810
811void e1inp_init(void)
812{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200813 tall_e1inp_ctx = talloc_named_const(libosmo_abis_ctx, 1, "e1inp");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200814 tall_sigl_ctx = talloc_named_const(tall_e1inp_ctx, 1,
815 "e1inp_sign_link");
Harald Weltecc2241b2011-07-19 16:06:06 +0200816 osmo_signal_register_handler(SS_L_GLOBAL, e1i_sig_cb, NULL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200817
818 e1inp_misdn_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200819#ifdef HAVE_DAHDI_USER_H
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200820 e1inp_dahdi_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200821#endif
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200822 e1inp_ipaccess_init();
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200823 e1inp_rs232_init();
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200824}