blob: 29ba440265162d8104e5bb5477b86a3f711b8a73 [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 *
Harald Welte323d39d2017-11-13 01:09:21 +09007 * SPDX-License-Identifier: AGPL-3.0+
8 *
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include "internal.h"
Harald Welte3bc78852011-08-24 08:32:38 +020025#include "../config.h"
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020026
27#include <stdio.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <errno.h>
31#include <string.h>
32#include <time.h>
Holger Hans Peter Freyther25474582017-01-23 19:49:07 +010033#include <fcntl.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020034#include <sys/socket.h>
35#include <sys/ioctl.h>
36#include <arpa/inet.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020037
Harald Weltefd44a5f2011-08-21 00:48:54 +020038#include <osmocom/abis/lapd.h>
39
Harald Weltef2737fc2011-08-16 14:30:10 +020040#include <osmocom/core/linuxlist.h>
41#include <osmocom/core/talloc.h>
42#include <osmocom/core/rate_ctr.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020043#include <osmocom/core/logging.h>
44#include <osmocom/core/signal.h>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020045#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020046
47#define NUM_E1_TS 32
48
49static void *tall_e1inp_ctx;
50
51/* list of all E1 drivers */
52LLIST_HEAD(e1inp_driver_list);
53
54/* list of all E1 lines */
55LLIST_HEAD(e1inp_line_list);
56
57static void *tall_sigl_ctx;
58
Harald Weltef2737fc2011-08-16 14:30:10 +020059static const struct rate_ctr_desc e1inp_ctr_d[] = {
60 [E1I_CTR_HDLC_ABORT] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020061 "hdlc.abort", "HDLC abort"
Harald Weltef2737fc2011-08-16 14:30:10 +020062 },
63 [E1I_CTR_HDLC_BADFCS] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020064 "hdlc.bad_fcs", "HLDC Bad FCS"
Harald Weltef2737fc2011-08-16 14:30:10 +020065 },
66 [E1I_CTR_HDLC_OVERR] = {
67 "hdlc.overrun", "HDLC Overrun"
68 },
69 [E1I_CTR_ALARM] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020070 "alarm", "Alarm"
Harald Weltef2737fc2011-08-16 14:30:10 +020071 },
72 [E1I_CTR_REMOVED] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020073 "removed", "Line removed"
Harald Weltef2737fc2011-08-16 14:30:10 +020074 },
75};
76
77static const struct rate_ctr_group_desc e1inp_ctr_g_d = {
78 .group_name_prefix = "e1inp",
79 .group_description = "E1 Input subsystem",
80 .num_ctr = ARRAY_SIZE(e1inp_ctr_d),
81 .ctr_desc = e1inp_ctr_d,
82};
83
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020084/*
85 * pcap writing of the misdn load
86 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
87 */
88#define DLT_LINUX_LAPD 177
89#define PCAP_INPUT 0
90#define PCAP_OUTPUT 1
91
92struct pcap_hdr {
93 uint32_t magic_number;
94 uint16_t version_major;
95 uint16_t version_minor;
96 int32_t thiszone;
97 uint32_t sigfigs;
98 uint32_t snaplen;
99 uint32_t network;
100} __attribute__((packed));
101
102struct pcaprec_hdr {
103 uint32_t ts_sec;
104 uint32_t ts_usec;
105 uint32_t incl_len;
106 uint32_t orig_len;
107} __attribute__((packed));
108
109struct fake_linux_lapd_header {
110 uint16_t pkttype;
111 uint16_t hatype;
112 uint16_t halen;
113 uint64_t addr;
114 int16_t protocol;
115} __attribute__((packed));
116
117struct lapd_header {
118 uint8_t ea1 : 1;
119 uint8_t cr : 1;
120 uint8_t sapi : 6;
121 uint8_t ea2 : 1;
122 uint8_t tei : 7;
123 uint8_t control_foo; /* fake UM's ... */
124} __attribute__((packed));
125
126osmo_static_assert(offsetof(struct fake_linux_lapd_header, hatype) == 2, hatype_offset);
127osmo_static_assert(offsetof(struct fake_linux_lapd_header, halen) == 4, halen_offset);
128osmo_static_assert(offsetof(struct fake_linux_lapd_header, addr) == 6, addr_offset);
129osmo_static_assert(offsetof(struct fake_linux_lapd_header, protocol) == 14, proto_offset);
130osmo_static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
131
132
133static int pcap_fd = -1;
134
Pablo Neira Ayuso2e11f5c2013-07-05 15:08:18 +0200135int e1_set_pcap_fd(int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200136{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200137 struct pcap_hdr header = {
138 .magic_number = 0xa1b2c3d4,
139 .version_major = 2,
140 .version_minor = 4,
141 .thiszone = 0,
142 .sigfigs = 0,
143 .snaplen = 65535,
144 .network = DLT_LINUX_LAPD,
145 };
146
147 pcap_fd = fd;
Pablo Neira Ayuso2e11f5c2013-07-05 15:08:18 +0200148 return write(pcap_fd, &header, sizeof(header));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200149}
150
151/* This currently only works for the D-Channel */
152static void write_pcap_packet(int direction, int sapi, int tei,
153 struct msgb *msg) {
154 if (pcap_fd < 0)
155 return;
156
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200157 time_t cur_time;
158 struct tm *tm;
159
160 struct fake_linux_lapd_header header = {
161 .pkttype = 4,
162 .hatype = 0,
163 .halen = 0,
164 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
165 .protocol = ntohs(48),
166 };
167
168 struct lapd_header lapd_header = {
169 .ea1 = 0,
170 .cr = direction == PCAP_OUTPUT ? 1 : 0,
171 .sapi = sapi & 0x3F,
172 .ea2 = 1,
173 .tei = tei & 0x7F,
174 .control_foo = 0x03 /* UI */,
175 };
176
177 struct pcaprec_hdr payload_header = {
178 .ts_sec = 0,
179 .ts_usec = 0,
180 .incl_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
181 + sizeof(struct lapd_header),
182 .orig_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
183 + sizeof(struct lapd_header),
184 };
185
186
187 cur_time = time(NULL);
188 tm = localtime(&cur_time);
189 payload_header.ts_sec = mktime(tm);
190
Harald Weltec9295ea2014-08-21 02:41:41 +0200191 write(pcap_fd, &payload_header, sizeof(payload_header));
192 write(pcap_fd, &header, sizeof(header));
193 write(pcap_fd, &lapd_header, sizeof(lapd_header));
194 write(pcap_fd, msg->l2h, msgb_l2len(msg));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200195}
196
Harald Welte4ca5c532016-07-27 23:05:03 +0200197const struct value_string e1inp_sign_type_names[5] = {
198 { E1INP_SIGN_NONE, "None" },
199 { E1INP_SIGN_OML, "OML" },
200 { E1INP_SIGN_RSL, "RSL" },
201 { E1INP_SIGN_OSMO, "OSMO" },
202 { 0, NULL }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200203};
Harald Welte4ca5c532016-07-27 23:05:03 +0200204
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200205const char *e1inp_signtype_name(enum e1inp_sign_type tp)
206{
Harald Welte4ca5c532016-07-27 23:05:03 +0200207 return get_value_string(e1inp_sign_type_names, tp);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200208}
209
Harald Welte7a228eb2016-07-28 11:09:31 +0200210const struct value_string e1inp_ts_type_names[6] = {
Harald Welte4ca5c532016-07-27 23:05:03 +0200211 { E1INP_TS_TYPE_NONE, "None" },
212 { E1INP_TS_TYPE_SIGN, "Signalling" },
213 { E1INP_TS_TYPE_TRAU, "TRAU" },
Harald Weltea0108e72016-07-27 21:44:50 +0200214 { E1INP_TS_TYPE_RAW, "RAW" },
Harald Welte7a228eb2016-07-28 11:09:31 +0200215 { E1INP_TS_TYPE_HDLC, "HDLC" },
Harald Welte4ca5c532016-07-27 23:05:03 +0200216 { 0, NULL }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200217};
218
219const char *e1inp_tstype_name(enum e1inp_ts_type tp)
220{
Harald Welte4ca5c532016-07-27 23:05:03 +0200221 return get_value_string(e1inp_ts_type_names, tp);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200222}
223
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200224int abis_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200225{
226 struct e1inp_sign_link *sign_link = msg->dst;
227 struct e1inp_driver *e1inp_driver;
228 struct e1inp_ts *e1i_ts;
Alexander Couzens35daa672016-11-08 16:17:20 +0100229
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200230 msg->l2h = msg->data;
231
232 /* don't know how to route this message. */
233 if (sign_link == NULL) {
Harald Welte40b0e8c2011-07-21 16:57:34 +0200234 LOGP(DLINP, LOGL_ERROR, "abis_sendmsg: msg->dst == NULL: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200235 osmo_hexdump(msg->data, msg->len));
236 talloc_free(msg);
237 return -EINVAL;
238 }
239 e1i_ts = sign_link->ts;
240 if (!osmo_timer_pending(&e1i_ts->sign.tx_timer)) {
241 /* notify the driver we have something to write */
242 e1inp_driver = sign_link->ts->line->driver;
243 e1inp_driver->want_write(e1i_ts);
244 }
245 msgb_enqueue(&sign_link->tx_list, msg);
246
Harald Welte7f9d8512016-07-04 09:59:46 +0200247 /* we only need to write a 'Fake LAPD' packet here, if the
248 * underlying driver hides LAPD from us. If we use the
249 * libosmocore LAPD implementation, it will take care of writing
250 * the _actual_ LAPD packet */
251 if (!e1i_ts->lapd) {
252 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi,
253 sign_link->tei, msg);
254 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200255
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
Harald Weltea0108e72016-07-27 21:44:50 +0200298int e1inp_ts_config_raw(struct e1inp_ts *ts, struct e1inp_line *line,
299 void (*raw_recv_cb)(struct e1inp_ts *ts,
300 struct msgb *msg))
301{
302 if (ts->type == E1INP_TS_TYPE_RAW && ts->line && line)
303 return 0;
304
305 ts->type = E1INP_TS_TYPE_RAW;
306 ts->line = line;
307 ts->raw.recv_cb = raw_recv_cb;
308 INIT_LLIST_HEAD(&ts->raw.tx_queue);
309
310 return 0;
311}
312
Harald Welte7a228eb2016-07-28 11:09:31 +0200313int e1inp_ts_config_hdlc(struct e1inp_ts *ts, struct e1inp_line *line,
314 void (*hdlc_recv_cb)(struct e1inp_ts *ts,
315 struct msgb *msg))
316{
317 if (ts->type == E1INP_TS_TYPE_HDLC && ts->line && line)
318 return 0;
319
320 ts->type = E1INP_TS_TYPE_HDLC;
321 ts->line = line;
322 ts->hdlc.recv_cb = hdlc_recv_cb;
323 INIT_LLIST_HEAD(&ts->hdlc.tx_queue);
324
325 return 0;
326}
327
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200328struct e1inp_line *e1inp_line_find(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200329{
330 struct e1inp_line *e1i_line;
331
332 /* iterate over global list of e1 lines */
333 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
334 if (e1i_line->num == e1_nr)
335 return e1i_line;
336 }
337 return NULL;
338}
339
340struct e1inp_line *
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200341e1inp_line_create(uint8_t e1_nr, const char *driver_name)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200342{
343 struct e1inp_driver *driver;
344 struct e1inp_line *line;
345 int i;
346
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200347 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200348 if (line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200349 LOGP(DLINP, LOGL_ERROR, "E1 Line %u already exists\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200350 e1_nr);
351 return NULL;
352 }
353
354 driver = e1inp_driver_find(driver_name);
355 if (!driver) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200356 LOGP(DLINP, LOGL_ERROR, "No such E1 driver '%s'\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200357 driver_name);
358 return NULL;
359 }
360
361 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
362 if (!line)
363 return NULL;
364
365 line->driver = driver;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200366 line->num = e1_nr;
Harald Weltef2737fc2011-08-16 14:30:10 +0200367
368 line->rate_ctr = rate_ctr_group_alloc(line, &e1inp_ctr_g_d, line->num);
Harald Welteff8eed22017-07-12 00:38:25 +0200369 if (!line->rate_ctr) {
370 LOGP(DLINP, LOGL_ERROR, "Cannot allocate counter group\n");
371 talloc_free(line);
372 return NULL;
373 }
Harald Weltef2737fc2011-08-16 14:30:10 +0200374
Harald Weltec2889512011-09-13 23:49:04 +0100375 line->num_ts = NUM_E1_TS;
376 for (i = 0; i < line->num_ts; i++) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200377 line->ts[i].num = i+1;
378 line->ts[i].line = line;
379 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200380 line->refcnt++;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200381 llist_add_tail(&line->list, &e1inp_line_list);
382
383 return line;
384}
385
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200386struct e1inp_line *
387e1inp_line_clone(void *ctx, struct e1inp_line *line)
388{
389 struct e1inp_line *clone;
390
391 /* clone virtual E1 line for this new OML link. */
392 clone = talloc_zero(ctx, struct e1inp_line);
393 if (clone == NULL)
394 return NULL;
395
396 memcpy(clone, line, sizeof(struct e1inp_line));
397 clone->refcnt = 1;
398 return clone;
399}
400
401void e1inp_line_get(struct e1inp_line *line)
402{
403 line->refcnt++;
404}
405
406void e1inp_line_put(struct e1inp_line *line)
407{
408 line->refcnt--;
409 if (line->refcnt == 0)
410 talloc_free(line);
411}
412
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200413void
414e1inp_line_bind_ops(struct e1inp_line *line, const struct e1inp_line_ops *ops)
415{
416 line->ops = ops;
417}
418
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200419#if 0
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200420struct e1inp_line *e1inp_line_find_create(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200421{
422 struct e1inp_line *line;
423 int i;
424
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200425 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200426 if (line)
427 return line;
428
429 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
430 if (!line)
431 return NULL;
432
433 line->num = e1_nr;
434 for (i = 0; i < NUM_E1_TS; i++) {
435 line->ts[i].num = i+1;
436 line->ts[i].line = line;
437 }
438 llist_add_tail(&line->list, &e1inp_line_list);
439
440 return line;
441}
442#endif
443
444static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr)
445{
446 struct e1inp_line *e1i_line;
447
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200448 e1i_line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200449 if (!e1i_line)
450 return NULL;
451
452 return &e1i_line->ts[ts_nr-1];
453}
454
455struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr)
456{
457 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
458
459 if (!e1i_ts)
460 return NULL;
461
462 return &e1i_ts->trau.mux;
463}
464
465/* Signalling Link */
466
467struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
468 uint8_t tei, uint8_t sapi)
469{
470 struct e1inp_sign_link *link;
471
472 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
473 if (link->sapi == sapi && link->tei == tei)
474 return link;
475 }
476
477 return NULL;
478}
479
480/* create a new signalling link in a E1 timeslot */
481
482struct e1inp_sign_link *
483e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
484 struct gsm_bts_trx *trx, uint8_t tei,
485 uint8_t sapi)
486{
487 struct e1inp_sign_link *link;
488
489 if (ts->type != E1INP_TS_TYPE_SIGN)
490 return NULL;
491
492 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
493 if (!link)
494 return NULL;
495
496 link->ts = ts;
497 link->type = type;
498 INIT_LLIST_HEAD(&link->tx_list);
499 link->trx = trx;
500 link->tei = tei;
501 link->sapi = sapi;
502
503 llist_add_tail(&link->list, &ts->sign.sign_links);
504
505 return link;
506}
507
508void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
509{
510 struct msgb *msg;
511
512 llist_del(&link->list);
513 while (!llist_empty(&link->tx_list)) {
514 msg = msgb_dequeue(&link->tx_list);
515 msgb_free(msg);
516 }
517
518 if (link->ts->type == E1INP_TS_TYPE_SIGN)
519 osmo_timer_del(&link->ts->sign.tx_timer);
520
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200521 if (link->ts->line->driver->close)
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200522 link->ts->line->driver->close(link);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200523
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200524 e1inp_line_put(link->ts->line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200525 talloc_free(link);
526}
527
528/* XXX */
529/* the E1 driver tells us he has received something on a TS */
530int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
531 uint8_t tei, uint8_t sapi)
532{
533 struct e1inp_sign_link *link;
534 int ret = 0;
535
536 switch (ts->type) {
537 case E1INP_TS_TYPE_SIGN:
Harald Welte7f9d8512016-07-04 09:59:46 +0200538 /* we only need to write a 'Fake LAPD' packet here, if
539 * the underlying driver hides LAPD from us. If we use
540 * the libosmocore LAPD implementation, it will take
541 * care of writing the _actual_ LAPD packet */
542 if (!ts->lapd)
543 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200544 /* consult the list of signalling links */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200545 link = e1inp_lookup_sign_link(ts, tei, sapi);
546 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200547 LOGP(DLMI, LOGL_ERROR, "didn't find signalling link for "
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200548 "tei %d, sapi %d\n", tei, sapi);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200549 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200550 return -EINVAL;
551 }
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200552 if (!ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200553 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200554 "no action set for signalling messages.\n");
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200555 msgb_free(msg);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200556 return -ENOENT;
557 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200558 msg->dst = link;
559 ts->line->ops->sign_link(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200560 break;
561 case E1INP_TS_TYPE_TRAU:
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200562 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200563 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200564 break;
Harald Weltea0108e72016-07-27 21:44:50 +0200565 case E1INP_TS_TYPE_RAW:
566 ts->raw.recv_cb(ts, msg);
567 break;
Harald Welte7a228eb2016-07-28 11:09:31 +0200568 case E1INP_TS_TYPE_HDLC:
569 ts->hdlc.recv_cb(ts, msg);
570 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200571 default:
572 ret = -EINVAL;
Harald Weltecc2241b2011-07-19 16:06:06 +0200573 LOGP(DLMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200574 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200575 break;
576 }
577
578 return ret;
579}
580
Harald Weltefd44a5f2011-08-21 00:48:54 +0200581/*! \brief Receive some data from the L1/HDLC into LAPD of a timeslot
582 * \param[in] e1i_ts E1 Timeslot data structure
583 * \param[in] msg Message buffer containing full LAPD message
584 *
585 * This is a wrapper around e1inp_rx_ts(), but feeding the incoming
586 * message first into our LAPD code. This allows a driver to read raw
587 * (HDLC decoded) data from the timeslot, instead of a LAPD stack
588 * present in any underlying driver.
589 */
590int e1inp_rx_ts_lapd(struct e1inp_ts *e1i_ts, struct msgb *msg)
591{
Harald Weltefd44a5f2011-08-21 00:48:54 +0200592 unsigned int sapi, tei;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200593 int ret = 0, error = 0;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200594
595 sapi = msg->data[0] >> 2;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200596 if ((msg->data[0] & 0x1))
597 tei = 0;
598 else
599 tei = msg->data[1] >> 1;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200600
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200601 DEBUGP(DLMI, "<= len = %d, sapi(%d) tei(%d)\n", msg->len, sapi, tei);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200602
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200603 ret = lapd_receive(e1i_ts->lapd, msg, &error);
604 if (ret < 0) {
Harald Weltefd44a5f2011-08-21 00:48:54 +0200605 switch(error) {
606 case LAPD_ERR_UNKNOWN_TEI:
607 /* We don't know about this TEI, probably the BSC
608 * lost local states (it crashed or it was stopped),
609 * notify the driver to see if it can do anything to
610 * recover the existing signalling links with the BTS.
611 */
612 e1inp_event(e1i_ts, S_L_INP_TEI_UNKNOWN, tei, sapi);
613 return -EIO;
614 }
Harald Weltefd44a5f2011-08-21 00:48:54 +0200615 }
616
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200617 return 0;
618}
Harald Weltefd44a5f2011-08-21 00:48:54 +0200619
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200620void e1inp_dlsap_up(struct osmo_dlsap_prim *dp, uint8_t tei, uint8_t sapi,
621 void *rx_cbdata)
622{
623 struct e1inp_ts *e1i_ts = rx_cbdata;
624 struct msgb *msg = dp->oph.msg;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200625
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200626 switch (dp->oph.primitive) {
627 case PRIM_DL_EST:
628 DEBUGP(DLMI, "DL_EST: sapi(%d) tei(%d)\n", sapi, tei);
629 e1inp_event(e1i_ts, S_L_INP_TEI_UP, tei, sapi);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200630 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200631 case PRIM_DL_REL:
632 DEBUGP(DLMI, "DL_REL: sapi(%d) tei(%d)\n", sapi, tei);
633 e1inp_event(e1i_ts, S_L_INP_TEI_DN, tei, sapi);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200634 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200635 case PRIM_DL_DATA:
636 case PRIM_DL_UNIT_DATA:
637 if (dp->oph.operation == PRIM_OP_INDICATION) {
638 msg->l2h = msg->l3h;
639 DEBUGP(DLMI, "RX: %s sapi=%d tei=%d\n",
640 osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)),
641 sapi, tei);
642 e1inp_rx_ts(e1i_ts, msg, tei, sapi);
643 return;
644 }
Harald Weltefd44a5f2011-08-21 00:48:54 +0200645 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200646 case PRIM_MDL_ERROR:
647 DEBUGP(DLMI, "MDL_EERROR: cause(%d)\n", dp->u.error_ind.cause);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200648 break;
649 default:
650 printf("ERROR: unknown prim\n");
651 break;
652 }
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200653
654 msgb_free(msg);
655
656 return;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200657}
658
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200659#define TSX_ALLOC_SIZE 4096
660
661/* called by driver if it wants to transmit on a given TS */
662struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
663 struct e1inp_sign_link **sign_link)
664{
665 struct e1inp_sign_link *link;
666 struct msgb *msg = NULL;
667 int len;
668
669 switch (e1i_ts->type) {
670 case E1INP_TS_TYPE_SIGN:
671 /* FIXME: implement this round robin */
672 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
673 msg = msgb_dequeue(&link->tx_list);
674 if (msg) {
675 if (sign_link)
676 *sign_link = link;
677 break;
678 }
679 }
680 break;
681 case E1INP_TS_TYPE_TRAU:
682 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
683 if (!msg)
684 return NULL;
685 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
Pablo Neira Ayuso33f71752013-07-05 15:44:12 +0200686 if (len != 40) {
687 LOGP(DLMI, LOGL_ERROR,
688 "cannot transmit, failed to mux\n");
689 msgb_free(msg);
690 return NULL;
691 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200692 msgb_put(msg, 40);
693 break;
Harald Weltea0108e72016-07-27 21:44:50 +0200694 case E1INP_TS_TYPE_RAW:
695 /* Get msgb from tx_queue */
696 msg = msgb_dequeue(&e1i_ts->raw.tx_queue);
697 break;
Harald Welte7a228eb2016-07-28 11:09:31 +0200698 case E1INP_TS_TYPE_HDLC:
699 /* Get msgb from tx_queue */
700 msg = msgb_dequeue(&e1i_ts->hdlc.tx_queue);
701 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200702 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200703 LOGP(DLMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200704 return NULL;
705 }
706 return msg;
707}
708
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100709static int e1inp_int_snd_event(struct e1inp_ts *ts,
710 struct e1inp_sign_link *link, int evt)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200711{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200712 struct input_signal_data isd;
Pablo Neira Ayuso31fe5f22011-08-09 23:15:38 +0200713 isd.line = ts->line;
Harald Weltef35d8892016-10-16 15:33:52 +0200714 isd.ts_nr = ts->num;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200715 isd.link_type = link->type;
716 isd.trx = link->trx;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100717 isd.tei = link->tei;
718 isd.sapi = link->sapi;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200719
720 /* report further upwards */
Harald Weltecc2241b2011-07-19 16:06:06 +0200721 osmo_signal_dispatch(SS_L_INPUT, evt, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200722 return 0;
723}
724
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100725
726/* called by driver in case some kind of link state event */
727int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi)
728{
729 struct e1inp_sign_link *link;
730
731 link = e1inp_lookup_sign_link(ts, tei, sapi);
732 if (!link)
733 return -EINVAL;
734 return e1inp_int_snd_event(ts, link, evt);
735}
736
737void e1inp_close_socket(struct e1inp_ts *ts,
738 struct e1inp_sign_link *sign_link,
739 struct osmo_fd *bfd)
740{
741 e1inp_int_snd_event(ts, sign_link, S_L_INP_TEI_DN);
742 /* the first e1inp_sign_link_destroy call closes the socket. */
743 if (bfd->fd != -1) {
744 osmo_fd_unregister(bfd);
745 close(bfd->fd);
746 bfd->fd = -1;
747 }
748}
749
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200750/* register a driver with the E1 core */
751int e1inp_driver_register(struct e1inp_driver *drv)
752{
753 llist_add_tail(&drv->list, &e1inp_driver_list);
754 return 0;
755}
756
757struct e1inp_driver *e1inp_driver_find(const char *name)
758{
759 struct e1inp_driver *drv;
760
761 llist_for_each_entry(drv, &e1inp_driver_list, list) {
762 if (!strcasecmp(name, drv->name))
763 return drv;
764 }
765 return NULL;
766}
767
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200768int e1inp_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200769{
770 struct input_signal_data isd;
Harald Welte7f9d8512016-07-04 09:59:46 +0200771 int i, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200772
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200773 e1inp_line_get(line);
774
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200775 if (line->driver && line->ops && line->driver->line_update) {
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200776 rc = line->driver->line_update(line);
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200777 } else
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200778 rc = 0;
779
Harald Welte7f9d8512016-07-04 09:59:46 +0200780 /* Set the PCAP file descriptor for all timeslots that have
781 * software LAPD instances, to ensure the osmo_lapd_pcap code is
782 * used to write PCAP files (if requested) */
783 for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
784 struct e1inp_ts *e1i_ts = &line->ts[i];
785 if (e1i_ts->lapd)
786 e1i_ts->lapd->pcap_fd = pcap_fd;
787 }
788
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200789 /* Send a signal to anyone who is interested in new lines being
790 * configured */
791 memset(&isd, 0, sizeof(isd));
792 isd.line = line;
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200793 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_INIT, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200794
795 return rc;
796}
797
Pablo Neira Ayusoa20762a2011-07-02 19:01:58 +0200798static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200799 void *handler_data, void *signal_data)
800{
Harald Weltecc2241b2011-07-19 16:06:06 +0200801 if (subsys != SS_L_GLOBAL ||
802 signal != S_L_GLOBAL_SHUTDOWN)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200803 return 0;
804
805 if (pcap_fd) {
806 close(pcap_fd);
807 pcap_fd = -1;
808 }
809
810 return 0;
811}
812
Harald Weltecac78fe2017-05-25 19:13:13 +0200813const struct value_string e1inp_signal_names[] = {
814 { S_L_INP_NONE, "NONE" },
815 { S_L_INP_TEI_UP, "TEI-UP" },
816 { S_L_INP_TEI_DN, "TEI-DOWN" },
817 { S_L_INP_TEI_UNKNOWN, "TEI-UNKNOWN" },
818 { S_L_INP_LINE_INIT, "LINE-INIT" },
819 { S_L_INP_LINE_ALARM, "LINE-ALARM" },
820 { S_L_INP_LINE_NOALARM, "LINE-NOALARM" },
821 { 0, NULL }
822};
823
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200824void e1inp_misdn_init(void);
825void e1inp_dahdi_init(void);
826void e1inp_ipaccess_init(void);
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200827void e1inp_rs232_init(void);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100828void e1inp_unixsocket_init(void);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200829
830void e1inp_init(void)
831{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200832 tall_e1inp_ctx = talloc_named_const(libosmo_abis_ctx, 1, "e1inp");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200833 tall_sigl_ctx = talloc_named_const(tall_e1inp_ctx, 1,
834 "e1inp_sign_link");
Harald Weltecc2241b2011-07-19 16:06:06 +0200835 osmo_signal_register_handler(SS_L_GLOBAL, e1i_sig_cb, NULL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200836
837 e1inp_misdn_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200838#ifdef HAVE_DAHDI_USER_H
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200839 e1inp_dahdi_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200840#endif
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200841 e1inp_ipaccess_init();
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200842 e1inp_rs232_init();
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100843 e1inp_unixsocket_init();
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200844}