blob: ee529cca41c87dd90862f4f7b798c17c4e75e5c5 [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>
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +020029#include <inttypes.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020030#include <stdlib.h>
31#include <errno.h>
32#include <string.h>
33#include <time.h>
Holger Hans Peter Freyther25474582017-01-23 19:49:07 +010034#include <fcntl.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020035#include <sys/socket.h>
36#include <sys/ioctl.h>
37#include <arpa/inet.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020038
Harald Weltefd44a5f2011-08-21 00:48:54 +020039#include <osmocom/abis/lapd.h>
40
Harald Weltef2737fc2011-08-16 14:30:10 +020041#include <osmocom/core/linuxlist.h>
42#include <osmocom/core/talloc.h>
43#include <osmocom/core/rate_ctr.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020044#include <osmocom/core/logging.h>
45#include <osmocom/core/signal.h>
Neels Hofmeyra160e4b2018-11-16 00:16:57 +010046#include <osmocom/core/endian.h>
Harald Welteb3952c62020-06-20 22:07:36 +020047#include <osmocom/gsm/i460_mux.h>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020048#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020049
50#define NUM_E1_TS 32
51
52static void *tall_e1inp_ctx;
53
54/* list of all E1 drivers */
55LLIST_HEAD(e1inp_driver_list);
56
57/* list of all E1 lines */
58LLIST_HEAD(e1inp_line_list);
59
60static void *tall_sigl_ctx;
61
Harald Weltef2737fc2011-08-16 14:30:10 +020062static const struct rate_ctr_desc e1inp_ctr_d[] = {
63 [E1I_CTR_HDLC_ABORT] = {
Pau Espin Pedrola2106842018-07-24 15:00:11 +020064 "hdlc:abort", "HDLC abort"
Harald Weltef2737fc2011-08-16 14:30:10 +020065 },
66 [E1I_CTR_HDLC_BADFCS] = {
Pau Espin Pedrola2106842018-07-24 15:00:11 +020067 "hdlc:bad_fcs", "HLDC Bad FCS"
Harald Weltef2737fc2011-08-16 14:30:10 +020068 },
69 [E1I_CTR_HDLC_OVERR] = {
Pau Espin Pedrola2106842018-07-24 15:00:11 +020070 "hdlc:overrun", "HDLC Overrun"
Harald Weltef2737fc2011-08-16 14:30:10 +020071 },
72 [E1I_CTR_ALARM] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020073 "alarm", "Alarm"
Harald Weltef2737fc2011-08-16 14:30:10 +020074 },
75 [E1I_CTR_REMOVED] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020076 "removed", "Line removed"
Harald Weltef2737fc2011-08-16 14:30:10 +020077 },
78};
79
80static const struct rate_ctr_group_desc e1inp_ctr_g_d = {
81 .group_name_prefix = "e1inp",
82 .group_description = "E1 Input subsystem",
83 .num_ctr = ARRAY_SIZE(e1inp_ctr_d),
84 .ctr_desc = e1inp_ctr_d,
85};
86
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020087/*
88 * pcap writing of the misdn load
89 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
90 */
91#define DLT_LINUX_LAPD 177
92#define PCAP_INPUT 0
93#define PCAP_OUTPUT 1
94
95struct pcap_hdr {
96 uint32_t magic_number;
97 uint16_t version_major;
98 uint16_t version_minor;
99 int32_t thiszone;
100 uint32_t sigfigs;
101 uint32_t snaplen;
102 uint32_t network;
103} __attribute__((packed));
104
105struct pcaprec_hdr {
106 uint32_t ts_sec;
107 uint32_t ts_usec;
108 uint32_t incl_len;
109 uint32_t orig_len;
110} __attribute__((packed));
111
112struct fake_linux_lapd_header {
113 uint16_t pkttype;
114 uint16_t hatype;
115 uint16_t halen;
116 uint64_t addr;
117 int16_t protocol;
118} __attribute__((packed));
119
120struct lapd_header {
Neels Hofmeyra160e4b2018-11-16 00:16:57 +0100121#if OSMO_IS_LITTLE_ENDIAN
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200122 uint8_t ea1 : 1;
123 uint8_t cr : 1;
124 uint8_t sapi : 6;
125 uint8_t ea2 : 1;
126 uint8_t tei : 7;
127 uint8_t control_foo; /* fake UM's ... */
Neels Hofmeyra160e4b2018-11-16 00:16:57 +0100128#elif OSMO_IS_BIG_ENDIAN
129/* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */
130 uint8_t sapi:6, cr:1, ea1:1;
131 uint8_t tei:7, ea2:1;
132 uint8_t control_foo;
133#endif
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200134} __attribute__((packed));
135
136osmo_static_assert(offsetof(struct fake_linux_lapd_header, hatype) == 2, hatype_offset);
137osmo_static_assert(offsetof(struct fake_linux_lapd_header, halen) == 4, halen_offset);
138osmo_static_assert(offsetof(struct fake_linux_lapd_header, addr) == 6, addr_offset);
139osmo_static_assert(offsetof(struct fake_linux_lapd_header, protocol) == 14, proto_offset);
140osmo_static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
141
Keith2672a2a2020-11-12 06:59:37 +0100142int e1_set_pcap_fd2(struct e1inp_line *line, int fd)
143{
144 static const struct pcap_hdr header = {
145 .magic_number = 0xa1b2c3d4,
146 .version_major = 2,
147 .version_minor = 4,
148 .thiszone = 0,
149 .sigfigs = 0,
150 .snaplen = 65535,
151 .network = DLT_LINUX_LAPD,
152 };
153 int i;
154
155 /* write header */
156 if (fd >= 0) {
157 int rc = write(fd, &header, sizeof(header));
158 if (rc < 0)
159 return rc;
160 }
161
162 /* Set the PCAP file descriptor for all timeslots that have
163 * software LAPD instances, to ensure the osmo_lapd_pcap code is
164 * used to write PCAP files (if requested) */
165 for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
166 struct e1inp_ts *e1i_ts = &line->ts[i];
167 if (e1i_ts->lapd)
168 e1i_ts->lapd->pcap_fd = fd;
169 }
170 /* close previous and update */
171 if (line->pcap_fd >= 0)
172 close(line->pcap_fd);
173 line->pcap_fd = fd;
174 return 0;
175}
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200176
177static int pcap_fd = -1;
178
Pablo Neira Ayuso2e11f5c2013-07-05 15:08:18 +0200179int e1_set_pcap_fd(int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200180{
Sylvain Munaut4b45e9d2020-05-08 09:40:44 +0200181 const struct pcap_hdr header = {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200182 .magic_number = 0xa1b2c3d4,
183 .version_major = 2,
184 .version_minor = 4,
185 .thiszone = 0,
186 .sigfigs = 0,
187 .snaplen = 65535,
188 .network = DLT_LINUX_LAPD,
189 };
Sylvain Munaut4b45e9d2020-05-08 09:40:44 +0200190 struct e1inp_line *line;
191 int i;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200192
Sylvain Munaut4b45e9d2020-05-08 09:40:44 +0200193 /* write header */
194 if (fd >= 0) {
195 int rc = write(fd, &header, sizeof(header));
196 if (rc < 0)
197 return rc;
198 }
199
200 /* update fd in all lines in our global list of e1 lines */
201 llist_for_each_entry(line, &e1inp_line_list, list) {
202 /* Set the PCAP file descriptor for all timeslots that have
203 * software LAPD instances, to ensure the osmo_lapd_pcap code is
204 * used to write PCAP files (if requested) */
205 for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
206 struct e1inp_ts *e1i_ts = &line->ts[i];
207 if (e1i_ts->lapd)
208 e1i_ts->lapd->pcap_fd = fd;
209 }
210 }
211
212 /* close previous and update global */
213 if (pcap_fd >= 0)
214 close(pcap_fd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200215 pcap_fd = fd;
Sylvain Munaut4b45e9d2020-05-08 09:40:44 +0200216
217 return 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200218}
219
220/* This currently only works for the D-Channel */
221static void write_pcap_packet(int direction, int sapi, int tei,
Keith2672a2a2020-11-12 06:59:37 +0100222 struct msgb *msg, int pcap_fd) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200223 if (pcap_fd < 0)
224 return;
225
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200226 time_t cur_time;
227 struct tm *tm;
228
229 struct fake_linux_lapd_header header = {
230 .pkttype = 4,
231 .hatype = 0,
232 .halen = 0,
233 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
234 .protocol = ntohs(48),
235 };
236
237 struct lapd_header lapd_header = {
238 .ea1 = 0,
239 .cr = direction == PCAP_OUTPUT ? 1 : 0,
240 .sapi = sapi & 0x3F,
241 .ea2 = 1,
242 .tei = tei & 0x7F,
243 .control_foo = 0x03 /* UI */,
244 };
245
246 struct pcaprec_hdr payload_header = {
247 .ts_sec = 0,
248 .ts_usec = 0,
249 .incl_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
250 + sizeof(struct lapd_header),
251 .orig_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
252 + sizeof(struct lapd_header),
253 };
254
255
256 cur_time = time(NULL);
257 tm = localtime(&cur_time);
258 payload_header.ts_sec = mktime(tm);
259
Harald Weltec9295ea2014-08-21 02:41:41 +0200260 write(pcap_fd, &payload_header, sizeof(payload_header));
261 write(pcap_fd, &header, sizeof(header));
262 write(pcap_fd, &lapd_header, sizeof(lapd_header));
263 write(pcap_fd, msg->l2h, msgb_l2len(msg));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200264}
265
Harald Welte4ca5c532016-07-27 23:05:03 +0200266const struct value_string e1inp_sign_type_names[5] = {
267 { E1INP_SIGN_NONE, "None" },
268 { E1INP_SIGN_OML, "OML" },
269 { E1INP_SIGN_RSL, "RSL" },
270 { E1INP_SIGN_OSMO, "OSMO" },
271 { 0, NULL }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200272};
Harald Welte4ca5c532016-07-27 23:05:03 +0200273
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200274const char *e1inp_signtype_name(enum e1inp_sign_type tp)
275{
Harald Welte4ca5c532016-07-27 23:05:03 +0200276 return get_value_string(e1inp_sign_type_names, tp);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200277}
278
Harald Welteb3952c62020-06-20 22:07:36 +0200279const struct value_string e1inp_ts_type_names[] = {
Harald Welte4ca5c532016-07-27 23:05:03 +0200280 { E1INP_TS_TYPE_NONE, "None" },
281 { E1INP_TS_TYPE_SIGN, "Signalling" },
282 { E1INP_TS_TYPE_TRAU, "TRAU" },
Harald Weltea0108e72016-07-27 21:44:50 +0200283 { E1INP_TS_TYPE_RAW, "RAW" },
Harald Welte7a228eb2016-07-28 11:09:31 +0200284 { E1INP_TS_TYPE_HDLC, "HDLC" },
Harald Welteb3952c62020-06-20 22:07:36 +0200285 { E1INP_TS_TYPE_I460, "I460" },
Harald Welte4ca5c532016-07-27 23:05:03 +0200286 { 0, NULL }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200287};
288
289const char *e1inp_tstype_name(enum e1inp_ts_type tp)
290{
Harald Welte4ca5c532016-07-27 23:05:03 +0200291 return get_value_string(e1inp_ts_type_names, tp);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200292}
293
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200294int abis_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200295{
296 struct e1inp_sign_link *sign_link = msg->dst;
297 struct e1inp_driver *e1inp_driver;
298 struct e1inp_ts *e1i_ts;
Alexander Couzens35daa672016-11-08 16:17:20 +0100299
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200300 msg->l2h = msg->data;
301
302 /* don't know how to route this message. */
303 if (sign_link == NULL) {
Harald Welte40b0e8c2011-07-21 16:57:34 +0200304 LOGP(DLINP, LOGL_ERROR, "abis_sendmsg: msg->dst == NULL: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200305 osmo_hexdump(msg->data, msg->len));
306 talloc_free(msg);
307 return -EINVAL;
308 }
309 e1i_ts = sign_link->ts;
310 if (!osmo_timer_pending(&e1i_ts->sign.tx_timer)) {
311 /* notify the driver we have something to write */
312 e1inp_driver = sign_link->ts->line->driver;
313 e1inp_driver->want_write(e1i_ts);
314 }
315 msgb_enqueue(&sign_link->tx_list, msg);
316
Harald Welte7f9d8512016-07-04 09:59:46 +0200317 /* we only need to write a 'Fake LAPD' packet here, if the
318 * underlying driver hides LAPD from us. If we use the
319 * libosmocore LAPD implementation, it will take care of writing
320 * the _actual_ LAPD packet */
321 if (!e1i_ts->lapd) {
322 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi,
Keith2672a2a2020-11-12 06:59:37 +0100323 sign_link->tei, msg, e1i_ts->line->pcap_fd);
Harald Welte7f9d8512016-07-04 09:59:46 +0200324 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200325
326 return 0;
327}
328
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200329int abis_rsl_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200330{
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200331 return abis_sendmsg(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200332}
333
334/* Timeslot */
335int e1inp_ts_config_trau(struct e1inp_ts *ts, struct e1inp_line *line,
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200336 int (*trau_rcv_cb)(struct subch_demux *dmx, int ch,
Harald Welte5226e5b2020-05-07 23:09:16 +0200337 const ubit_t *data, int len, void *_priv))
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200338{
339 if (ts->type == E1INP_TS_TYPE_TRAU && ts->line && line)
340 return 0;
341
342 ts->type = E1INP_TS_TYPE_TRAU;
343 ts->line = line;
344
345 subchan_mux_init(&ts->trau.mux);
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200346 ts->trau.demux.out_cb = trau_rcv_cb;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200347 ts->trau.demux.data = ts;
348 subch_demux_init(&ts->trau.demux);
349 return 0;
350}
351
Harald Welteb3952c62020-06-20 22:07:36 +0200352int e1inp_ts_config_i460(struct e1inp_ts *ts, struct e1inp_line *line)
353{
354 if (ts->type == E1INP_TS_TYPE_I460 && ts->line && line)
355 return 0;
356
357 ts->type = E1INP_TS_TYPE_I460;
358 ts->line = line;
359 osmo_i460_ts_init(&ts->i460.i460_ts);
360 return 0;
361}
362
Harald Welteb9031882020-05-02 21:09:15 +0200363void e1inp_ts_name(char *out, size_t out_len, const struct e1inp_ts *ts)
364{
365 if (ts->line->name)
366 snprintf(out, out_len, "%s:%u", ts->line->name, ts->num);
367 else
368 snprintf(out, out_len, "%u:%u", ts->line->num, ts->num);
369}
370
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200371int e1inp_ts_config_sign(struct e1inp_ts *ts, struct e1inp_line *line)
372{
373 if (ts->type == E1INP_TS_TYPE_SIGN && ts->line && line)
374 return 0;
375
376 ts->type = E1INP_TS_TYPE_SIGN;
377 ts->line = line;
378
379 if (line && line->driver)
380 ts->sign.delay = line->driver->default_delay;
381 else
382 ts->sign.delay = 100000;
383 INIT_LLIST_HEAD(&ts->sign.sign_links);
384 return 0;
385}
386
Harald Weltea0108e72016-07-27 21:44:50 +0200387int e1inp_ts_config_raw(struct e1inp_ts *ts, struct e1inp_line *line,
388 void (*raw_recv_cb)(struct e1inp_ts *ts,
389 struct msgb *msg))
390{
391 if (ts->type == E1INP_TS_TYPE_RAW && ts->line && line)
392 return 0;
393
394 ts->type = E1INP_TS_TYPE_RAW;
395 ts->line = line;
396 ts->raw.recv_cb = raw_recv_cb;
397 INIT_LLIST_HEAD(&ts->raw.tx_queue);
398
399 return 0;
400}
401
Harald Welte7a228eb2016-07-28 11:09:31 +0200402int e1inp_ts_config_hdlc(struct e1inp_ts *ts, struct e1inp_line *line,
403 void (*hdlc_recv_cb)(struct e1inp_ts *ts,
404 struct msgb *msg))
405{
406 if (ts->type == E1INP_TS_TYPE_HDLC && ts->line && line)
407 return 0;
408
409 ts->type = E1INP_TS_TYPE_HDLC;
410 ts->line = line;
411 ts->hdlc.recv_cb = hdlc_recv_cb;
412 INIT_LLIST_HEAD(&ts->hdlc.tx_queue);
413
414 return 0;
415}
416
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200417static int e1inp_line_use_cb(struct osmo_use_count_entry *use_count_entry, int32_t old_use_count,
418 const char *file, int file_line)
419{
420 char buf[512];
421 struct osmo_use_count *uc = use_count_entry->use_count;
422 struct e1inp_line *line = uc->talloc_object;
423
424 LOGPSRC(DLINP, LOGL_INFO, file, file_line,
425 "E1L(%u) Line (%p) reference count %s changed %" PRId32 " -> %" PRId32 " [%s]\n",
426 (line)->num, line, use_count_entry->use,
427 old_use_count, use_count_entry->count,
428 osmo_use_count_name_buf(buf, sizeof(buf), uc));
429
430 if (!use_count_entry->count)
431 osmo_use_count_free(use_count_entry);
432
433 if (osmo_use_count_total(uc) > 0)
434 return 0;
435
436 /* Remove our counter group from libosmocore's global counter
437 * list if we are freeing the last remaining talloc context.
438 * Otherwise we get a use-after-free when libosmocore's timer
439 * ticks again and attempts to update these counters (OS#3011).
440 *
441 * Note that talloc internally counts "secondary" references
442 * _in addition to_ the initial allocation context, so yes,
443 * we must check for *zero* remaining secondary contexts here. */
444 if (talloc_reference_count(line->rate_ctr) == 0) {
445 rate_ctr_group_free(line->rate_ctr);
446 } else {
447 /* We are not freeing the last talloc context.
448 * Instead of calling talloc_free(), unlink this 'line' pointer
449 * which serves as one of several talloc contexts for the rate
450 * counters and driver private state. */
451 talloc_unlink(line, line->rate_ctr);
452 if (line->driver_data)
453 talloc_unlink(line, line->driver_data);
454 }
Pau Espin Pedrol49924f02021-09-22 19:31:54 +0200455
456 llist_del(&line->list);
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200457 talloc_free(line);
458 return 0;
459}
460
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200461struct e1inp_line *e1inp_line_find(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200462{
463 struct e1inp_line *e1i_line;
464
465 /* iterate over global list of e1 lines */
466 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
467 if (e1i_line->num == e1_nr)
468 return e1i_line;
469 }
470 return NULL;
471}
472
Pau Espin Pedroldd48e252021-09-22 19:37:54 +0200473/*! Create a new e1inp line object.
474 * \param[in] e1_nr The line number of the new line to be created.
475 * \param[in] driver_name String identifying the driver (see e1inp_driver_register() for more info).
476 * \returns pointer to the new object created.
477 *
478 * The allocated object is returned with a count reference with name "ctor",
479 * which must be dropped in order to free the object [e1inp_line_put2(line, "ctor")].
480 */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200481struct e1inp_line *
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200482e1inp_line_create(uint8_t e1_nr, const char *driver_name)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200483{
484 struct e1inp_driver *driver;
485 struct e1inp_line *line;
486 int i;
487
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200488 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200489 if (line) {
Harald Welteb5af0992020-01-12 12:59:52 +0100490 LOGPIL(line, DLINP, LOGL_ERROR, "E1 Line %u already exists\n", e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200491 return NULL;
492 }
493
494 driver = e1inp_driver_find(driver_name);
495 if (!driver) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200496 LOGP(DLINP, LOGL_ERROR, "No such E1 driver '%s'\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200497 driver_name);
498 return NULL;
499 }
500
501 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
502 if (!line)
503 return NULL;
504
505 line->driver = driver;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200506 line->num = e1_nr;
Keith2672a2a2020-11-12 06:59:37 +0100507 line->pcap_fd = -1;
Harald Weltef2737fc2011-08-16 14:30:10 +0200508
Oliver Smithef0f7c22022-11-23 12:32:36 +0100509 line->keepalive_idle_timeout = E1INP_USE_DEFAULT;
510 line->keepalive_num_probes = E1INP_USE_DEFAULT;
511 line->keepalive_probe_interval = E1INP_USE_DEFAULT;
512
Harald Weltef2737fc2011-08-16 14:30:10 +0200513 line->rate_ctr = rate_ctr_group_alloc(line, &e1inp_ctr_g_d, line->num);
Harald Welteff8eed22017-07-12 00:38:25 +0200514 if (!line->rate_ctr) {
Harald Welteb5af0992020-01-12 12:59:52 +0100515 LOGPIL(line, DLINP, LOGL_ERROR, "Cannot allocate counter group\n");
Harald Welteff8eed22017-07-12 00:38:25 +0200516 talloc_free(line);
517 return NULL;
518 }
Harald Weltef2737fc2011-08-16 14:30:10 +0200519
Harald Weltec2889512011-09-13 23:49:04 +0100520 line->num_ts = NUM_E1_TS;
521 for (i = 0; i < line->num_ts; i++) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200522 line->ts[i].num = i+1;
523 line->ts[i].line = line;
524 }
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200525
526 line->use_count.talloc_object = line;
527 line->use_count.use_cb = e1inp_line_use_cb;
528 e1inp_line_get2(line, "ctor");
529
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200530 llist_add_tail(&line->list, &e1inp_line_list);
531
532 return line;
533}
534
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200535struct e1inp_line *
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200536e1inp_line_clone(void *ctx, struct e1inp_line *line, const char *use)
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200537{
538 struct e1inp_line *clone;
539
540 /* clone virtual E1 line for this new OML link. */
541 clone = talloc_zero(ctx, struct e1inp_line);
542 if (clone == NULL)
543 return NULL;
544
545 memcpy(clone, line, sizeof(struct e1inp_line));
Stefan Sperlingb0162072018-05-22 18:19:00 +0200546
547 if (line->name) {
548 clone->name = talloc_strdup(clone, line->name);
549 OSMO_ASSERT(clone->name);
550 }
551 if (line->sock_path) {
552 clone->sock_path = talloc_strdup(clone, line->sock_path);
553 OSMO_ASSERT(clone->sock_path);
554 }
555
556 /*
557 * Rate counters and driver data are shared between clones. These are pointers
558 * to dynamic memory so we use reference counting to avoid a double-free (see OS#3137).
559 */
560 OSMO_ASSERT(line->rate_ctr);
561 clone->rate_ctr = talloc_reference(clone, line->rate_ctr);
562 if (line->driver_data)
563 clone->driver_data = talloc_reference(clone, line->driver_data);
564
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200565 clone->use_count = (struct osmo_use_count) {
566 .talloc_object = clone,
567 .use_cb = e1inp_line_use_cb,
568 .use_counts = {0},
569 };
Pau Espin Pedrol57ff57a2021-10-13 16:37:59 +0200570 /* initialize list so it can be safely deleted without affecting original line */
571 INIT_LLIST_HEAD(&clone->list);
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200572 e1inp_line_get2(clone, use); /* Clone is used internally for bfd */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200573 return clone;
574}
575
576void e1inp_line_get(struct e1inp_line *line)
577{
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200578 e1inp_line_get2(line, "unknown");
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200579}
580
581void e1inp_line_put(struct e1inp_line *line)
582{
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200583 e1inp_line_put2(line, "unknown");
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200584}
585
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200586void
587e1inp_line_bind_ops(struct e1inp_line *line, const struct e1inp_line_ops *ops)
588{
589 line->ops = ops;
590}
591
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200592#if 0
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200593struct e1inp_line *e1inp_line_find_create(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200594{
595 struct e1inp_line *line;
596 int i;
597
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200598 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200599 if (line)
600 return line;
601
602 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
603 if (!line)
604 return NULL;
605
606 line->num = e1_nr;
607 for (i = 0; i < NUM_E1_TS; i++) {
608 line->ts[i].num = i+1;
609 line->ts[i].line = line;
610 }
611 llist_add_tail(&line->list, &e1inp_line_list);
612
613 return line;
614}
615#endif
616
617static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr)
618{
619 struct e1inp_line *e1i_line;
620
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200621 e1i_line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200622 if (!e1i_line)
623 return NULL;
624
625 return &e1i_line->ts[ts_nr-1];
626}
627
628struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr)
629{
630 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
631
632 if (!e1i_ts)
633 return NULL;
634
635 return &e1i_ts->trau.mux;
636}
637
638/* Signalling Link */
639
640struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
641 uint8_t tei, uint8_t sapi)
642{
643 struct e1inp_sign_link *link;
644
645 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
646 if (link->sapi == sapi && link->tei == tei)
647 return link;
648 }
649
650 return NULL;
651}
652
653/* create a new signalling link in a E1 timeslot */
654
655struct e1inp_sign_link *
656e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
657 struct gsm_bts_trx *trx, uint8_t tei,
658 uint8_t sapi)
659{
660 struct e1inp_sign_link *link;
661
662 if (ts->type != E1INP_TS_TYPE_SIGN)
663 return NULL;
664
665 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
666 if (!link)
667 return NULL;
668
669 link->ts = ts;
670 link->type = type;
671 INIT_LLIST_HEAD(&link->tx_list);
672 link->trx = trx;
673 link->tei = tei;
674 link->sapi = sapi;
675
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200676 e1inp_line_get2(link->ts->line, "e1inp_sign_link");
Pau Espin Pedrol89c6b8a2020-06-08 20:26:44 +0200677
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200678 llist_add_tail(&link->list, &ts->sign.sign_links);
679
680 return link;
681}
682
683void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
684{
685 struct msgb *msg;
686
687 llist_del(&link->list);
688 while (!llist_empty(&link->tx_list)) {
689 msg = msgb_dequeue(&link->tx_list);
690 msgb_free(msg);
691 }
692
693 if (link->ts->type == E1INP_TS_TYPE_SIGN)
694 osmo_timer_del(&link->ts->sign.tx_timer);
695
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200696 if (link->ts->line->driver->close)
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200697 link->ts->line->driver->close(link);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200698
Pau Espin Pedrol5196cd52020-07-14 17:52:09 +0200699 e1inp_line_put2(link->ts->line, "e1inp_sign_link");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200700 talloc_free(link);
701}
702
703/* XXX */
704/* the E1 driver tells us he has received something on a TS */
705int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
706 uint8_t tei, uint8_t sapi)
707{
708 struct e1inp_sign_link *link;
709 int ret = 0;
710
711 switch (ts->type) {
712 case E1INP_TS_TYPE_SIGN:
Harald Welte7f9d8512016-07-04 09:59:46 +0200713 /* we only need to write a 'Fake LAPD' packet here, if
714 * the underlying driver hides LAPD from us. If we use
715 * the libosmocore LAPD implementation, it will take
716 * care of writing the _actual_ LAPD packet */
717 if (!ts->lapd)
Keith2672a2a2020-11-12 06:59:37 +0100718 write_pcap_packet(PCAP_INPUT, sapi, tei, msg, ts->line->pcap_fd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200719 /* consult the list of signalling links */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200720 link = e1inp_lookup_sign_link(ts, tei, sapi);
721 if (!link) {
Harald Welteb5af0992020-01-12 12:59:52 +0100722 LOGPITS(ts, DLMI, LOGL_ERROR, "didn't find signalling link for "
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200723 "tei %d, sapi %d\n", tei, sapi);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200724 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200725 return -EINVAL;
726 }
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200727 if (!ts->line->ops->sign_link) {
Harald Welteb5af0992020-01-12 12:59:52 +0100728 LOGPITS(ts, DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200729 "no action set for signalling messages.\n");
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200730 msgb_free(msg);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200731 return -ENOENT;
732 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200733 msg->dst = link;
734 ts->line->ops->sign_link(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200735 break;
736 case E1INP_TS_TYPE_TRAU:
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200737 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200738 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200739 break;
Harald Weltea0108e72016-07-27 21:44:50 +0200740 case E1INP_TS_TYPE_RAW:
741 ts->raw.recv_cb(ts, msg);
742 break;
Harald Welte7a228eb2016-07-28 11:09:31 +0200743 case E1INP_TS_TYPE_HDLC:
744 ts->hdlc.recv_cb(ts, msg);
745 break;
Harald Welteb3952c62020-06-20 22:07:36 +0200746 case E1INP_TS_TYPE_I460:
747 osmo_i460_demux_in(&ts->i460.i460_ts, msg->l2h, msgb_l2len(msg));
748 msgb_free(msg);
749 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200750 default:
751 ret = -EINVAL;
Harald Welteb5af0992020-01-12 12:59:52 +0100752 LOGPITS(ts, DLMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200753 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200754 break;
755 }
756
757 return ret;
758}
759
Harald Weltefd44a5f2011-08-21 00:48:54 +0200760/*! \brief Receive some data from the L1/HDLC into LAPD of a timeslot
761 * \param[in] e1i_ts E1 Timeslot data structure
762 * \param[in] msg Message buffer containing full LAPD message
763 *
764 * This is a wrapper around e1inp_rx_ts(), but feeding the incoming
765 * message first into our LAPD code. This allows a driver to read raw
766 * (HDLC decoded) data from the timeslot, instead of a LAPD stack
767 * present in any underlying driver.
768 */
769int e1inp_rx_ts_lapd(struct e1inp_ts *e1i_ts, struct msgb *msg)
770{
Harald Weltefd44a5f2011-08-21 00:48:54 +0200771 unsigned int sapi, tei;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200772 int ret = 0, error = 0;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200773
774 sapi = msg->data[0] >> 2;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200775 if ((msg->data[0] & 0x1))
776 tei = 0;
777 else
778 tei = msg->data[1] >> 1;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200779
Harald Welteb5af0992020-01-12 12:59:52 +0100780 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "<= len = %d, sapi(%d) tei(%d)\n", msg->len, sapi, tei);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200781
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200782 ret = lapd_receive(e1i_ts->lapd, msg, &error);
783 if (ret < 0) {
Harald Weltefd44a5f2011-08-21 00:48:54 +0200784 switch(error) {
785 case LAPD_ERR_UNKNOWN_TEI:
786 /* We don't know about this TEI, probably the BSC
787 * lost local states (it crashed or it was stopped),
788 * notify the driver to see if it can do anything to
789 * recover the existing signalling links with the BTS.
790 */
791 e1inp_event(e1i_ts, S_L_INP_TEI_UNKNOWN, tei, sapi);
792 return -EIO;
793 }
Harald Weltefd44a5f2011-08-21 00:48:54 +0200794 }
795
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200796 return 0;
797}
Harald Weltefd44a5f2011-08-21 00:48:54 +0200798
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200799void e1inp_dlsap_up(struct osmo_dlsap_prim *dp, uint8_t tei, uint8_t sapi,
800 void *rx_cbdata)
801{
802 struct e1inp_ts *e1i_ts = rx_cbdata;
803 struct msgb *msg = dp->oph.msg;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200804
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200805 switch (dp->oph.primitive) {
806 case PRIM_DL_EST:
Harald Welteb5af0992020-01-12 12:59:52 +0100807 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "DL_EST: sapi(%d) tei(%d)\n", sapi, tei);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200808 e1inp_event(e1i_ts, S_L_INP_TEI_UP, tei, sapi);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200809 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200810 case PRIM_DL_REL:
Harald Welteb5af0992020-01-12 12:59:52 +0100811 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "DL_REL: sapi(%d) tei(%d)\n", sapi, tei);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200812 e1inp_event(e1i_ts, S_L_INP_TEI_DN, tei, sapi);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200813 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200814 case PRIM_DL_DATA:
815 case PRIM_DL_UNIT_DATA:
816 if (dp->oph.operation == PRIM_OP_INDICATION) {
817 msg->l2h = msg->l3h;
Harald Welteb5af0992020-01-12 12:59:52 +0100818 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "RX: %s sapi=%d tei=%d\n",
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200819 osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)),
820 sapi, tei);
821 e1inp_rx_ts(e1i_ts, msg, tei, sapi);
822 return;
823 }
Harald Weltefd44a5f2011-08-21 00:48:54 +0200824 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200825 case PRIM_MDL_ERROR:
Harald Welteb5af0992020-01-12 12:59:52 +0100826 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "MDL_EERROR: cause(%d)\n", dp->u.error_ind.cause);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200827 break;
828 default:
829 printf("ERROR: unknown prim\n");
830 break;
831 }
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200832
833 msgb_free(msg);
834
835 return;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200836}
837
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200838#define TSX_ALLOC_SIZE 4096
839
840/* called by driver if it wants to transmit on a given TS */
841struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
842 struct e1inp_sign_link **sign_link)
843{
844 struct e1inp_sign_link *link;
845 struct msgb *msg = NULL;
846 int len;
847
848 switch (e1i_ts->type) {
849 case E1INP_TS_TYPE_SIGN:
850 /* FIXME: implement this round robin */
851 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
852 msg = msgb_dequeue(&link->tx_list);
853 if (msg) {
854 if (sign_link)
855 *sign_link = link;
856 break;
857 }
858 }
859 break;
860 case E1INP_TS_TYPE_TRAU:
861 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
862 if (!msg)
863 return NULL;
864 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
Pablo Neira Ayuso33f71752013-07-05 15:44:12 +0200865 if (len != 40) {
Harald Welteb5af0992020-01-12 12:59:52 +0100866 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "cannot transmit, failed to mux\n");
Pablo Neira Ayuso33f71752013-07-05 15:44:12 +0200867 msgb_free(msg);
868 return NULL;
869 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200870 msgb_put(msg, 40);
871 break;
Harald Weltea0108e72016-07-27 21:44:50 +0200872 case E1INP_TS_TYPE_RAW:
873 /* Get msgb from tx_queue */
874 msg = msgb_dequeue(&e1i_ts->raw.tx_queue);
875 break;
Harald Welte7a228eb2016-07-28 11:09:31 +0200876 case E1INP_TS_TYPE_HDLC:
877 /* Get msgb from tx_queue */
878 msg = msgb_dequeue(&e1i_ts->hdlc.tx_queue);
879 break;
Harald Welteb3952c62020-06-20 22:07:36 +0200880 case E1INP_TS_TYPE_I460:
881 msg = msgb_alloc(TSX_ALLOC_SIZE, "I460_TX");
882 if (!msg)
883 return NULL;
884 len = osmo_i460_mux_out(&e1i_ts->i460.i460_ts, msg->data, 160);
885 msgb_put(msg, len);
886 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200887 default:
Harald Welteb5af0992020-01-12 12:59:52 +0100888 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200889 return NULL;
890 }
891 return msg;
892}
893
Pau Espin Pedrol8737ad42020-07-14 21:11:56 +0200894int e1inp_int_snd_event(struct e1inp_ts *ts, struct e1inp_sign_link *link, int evt)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200895{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200896 struct input_signal_data isd;
Pablo Neira Ayuso31fe5f22011-08-09 23:15:38 +0200897 isd.line = ts->line;
Harald Weltef35d8892016-10-16 15:33:52 +0200898 isd.ts_nr = ts->num;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200899 isd.link_type = link->type;
900 isd.trx = link->trx;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100901 isd.tei = link->tei;
902 isd.sapi = link->sapi;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200903
904 /* report further upwards */
Harald Weltecc2241b2011-07-19 16:06:06 +0200905 osmo_signal_dispatch(SS_L_INPUT, evt, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200906 return 0;
907}
908
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100909
910/* called by driver in case some kind of link state event */
911int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi)
912{
913 struct e1inp_sign_link *link;
914
915 link = e1inp_lookup_sign_link(ts, tei, sapi);
916 if (!link)
917 return -EINVAL;
918 return e1inp_int_snd_event(ts, link, evt);
919}
920
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200921/* register a driver with the E1 core */
922int e1inp_driver_register(struct e1inp_driver *drv)
923{
924 llist_add_tail(&drv->list, &e1inp_driver_list);
925 return 0;
926}
927
928struct e1inp_driver *e1inp_driver_find(const char *name)
929{
930 struct e1inp_driver *drv;
931
932 llist_for_each_entry(drv, &e1inp_driver_list, list) {
933 if (!strcasecmp(name, drv->name))
934 return drv;
935 }
936 return NULL;
937}
938
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200939int e1inp_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200940{
941 struct input_signal_data isd;
Harald Welte7f9d8512016-07-04 09:59:46 +0200942 int i, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200943
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200944 if (line->driver && line->ops && line->driver->line_update) {
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200945 rc = line->driver->line_update(line);
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200946 } else
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200947 rc = 0;
948
Harald Welte7f9d8512016-07-04 09:59:46 +0200949 /* Set the PCAP file descriptor for all timeslots that have
950 * software LAPD instances, to ensure the osmo_lapd_pcap code is
951 * used to write PCAP files (if requested) */
952 for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
953 struct e1inp_ts *e1i_ts = &line->ts[i];
954 if (e1i_ts->lapd)
Keith2672a2a2020-11-12 06:59:37 +0100955 e1i_ts->lapd->pcap_fd = line->pcap_fd;
Harald Welte7f9d8512016-07-04 09:59:46 +0200956 }
957
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200958 /* Send a signal to anyone who is interested in new lines being
959 * configured */
960 memset(&isd, 0, sizeof(isd));
961 isd.line = line;
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200962 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_INIT, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200963
964 return rc;
965}
966
Pablo Neira Ayusoa20762a2011-07-02 19:01:58 +0200967static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200968 void *handler_data, void *signal_data)
969{
Keith2672a2a2020-11-12 06:59:37 +0100970 struct e1inp_line *line;
971
Harald Weltecc2241b2011-07-19 16:06:06 +0200972 if (subsys != SS_L_GLOBAL ||
973 signal != S_L_GLOBAL_SHUTDOWN)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200974 return 0;
975
Keith2672a2a2020-11-12 06:59:37 +0100976 llist_for_each_entry(line, &e1inp_line_list, list) {
977 if (line->pcap_fd >=0)
978 close(line->pcap_fd);
979 line->pcap_fd = -1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200980 }
981
982 return 0;
983}
984
Harald Weltecac78fe2017-05-25 19:13:13 +0200985const struct value_string e1inp_signal_names[] = {
986 { S_L_INP_NONE, "NONE" },
987 { S_L_INP_TEI_UP, "TEI-UP" },
988 { S_L_INP_TEI_DN, "TEI-DOWN" },
989 { S_L_INP_TEI_UNKNOWN, "TEI-UNKNOWN" },
990 { S_L_INP_LINE_INIT, "LINE-INIT" },
991 { S_L_INP_LINE_ALARM, "LINE-ALARM" },
992 { S_L_INP_LINE_NOALARM, "LINE-NOALARM" },
993 { 0, NULL }
994};
995
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200996void e1inp_misdn_init(void);
997void e1inp_dahdi_init(void);
Harald Weltec6c70762019-05-12 21:54:59 +0200998void e1inp_e1d_init(void);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200999void e1inp_ipaccess_init(void);
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +02001000void e1inp_rs232_init(void);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +01001001void e1inp_unixsocket_init(void);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001002
1003void e1inp_init(void)
1004{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +02001005 tall_e1inp_ctx = talloc_named_const(libosmo_abis_ctx, 1, "e1inp");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001006 tall_sigl_ctx = talloc_named_const(tall_e1inp_ctx, 1,
1007 "e1inp_sign_link");
Harald Weltecc2241b2011-07-19 16:06:06 +02001008 osmo_signal_register_handler(SS_L_GLOBAL, e1i_sig_cb, NULL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001009
1010 e1inp_misdn_init();
Harald Welte3bc78852011-08-24 08:32:38 +02001011#ifdef HAVE_DAHDI_USER_H
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001012 e1inp_dahdi_init();
Harald Welte3bc78852011-08-24 08:32:38 +02001013#endif
Harald Welte52b0d452019-05-12 21:54:26 +02001014#ifdef HAVE_E1D
Sylvain Munautb559a532019-05-09 11:14:26 +02001015 e1inp_e1d_init();
1016#endif
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001017 e1inp_ipaccess_init();
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +02001018 e1inp_rs232_init();
Alexander Couzensbeb10ef2016-11-01 22:05:13 +01001019 e1inp_unixsocket_init();
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001020}